ioctl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NILFS ioctl operations.
  4. *
  5. * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Koji Sato.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/wait.h>
  11. #include <linux/slab.h>
  12. #include <linux/capability.h> /* capable() */
  13. #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
  14. #include <linux/vmalloc.h>
  15. #include <linux/compat.h> /* compat_ptr() */
  16. #include <linux/mount.h> /* mnt_want_write_file(), mnt_drop_write_file() */
  17. #include <linux/buffer_head.h>
  18. #include <linux/fileattr.h>
  19. #include <linux/string.h>
  20. #include "nilfs.h"
  21. #include "segment.h"
  22. #include "bmap.h"
  23. #include "cpfile.h"
  24. #include "sufile.h"
  25. #include "dat.h"
  26. /**
  27. * nilfs_ioctl_wrap_copy - wrapping function of get/set metadata info
  28. * @nilfs: nilfs object
  29. * @argv: vector of arguments from userspace
  30. * @dir: set of direction flags
  31. * @dofunc: concrete function of get/set metadata info
  32. *
  33. * Description: nilfs_ioctl_wrap_copy() gets/sets metadata info by means of
  34. * calling dofunc() function on the basis of @argv argument.
  35. *
  36. * Return Value: On success, 0 is returned and requested metadata info
  37. * is copied into userspace. On error, one of the following
  38. * negative error codes is returned.
  39. *
  40. * %-EINVAL - Invalid arguments from userspace.
  41. *
  42. * %-ENOMEM - Insufficient amount of memory available.
  43. *
  44. * %-EFAULT - Failure during execution of requested operation.
  45. */
  46. static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
  47. struct nilfs_argv *argv, int dir,
  48. ssize_t (*dofunc)(struct the_nilfs *,
  49. __u64 *, int,
  50. void *, size_t, size_t))
  51. {
  52. void *buf;
  53. void __user *base = (void __user *)(unsigned long)argv->v_base;
  54. size_t maxmembs, total, n;
  55. ssize_t nr;
  56. int ret, i;
  57. __u64 pos, ppos;
  58. if (argv->v_nmembs == 0)
  59. return 0;
  60. if ((size_t)argv->v_size > PAGE_SIZE)
  61. return -EINVAL;
  62. /*
  63. * Reject pairs of a start item position (argv->v_index) and a
  64. * total count (argv->v_nmembs) which leads position 'pos' to
  65. * overflow by the increment at the end of the loop.
  66. */
  67. if (argv->v_index > ~(__u64)0 - argv->v_nmembs)
  68. return -EINVAL;
  69. buf = (void *)get_zeroed_page(GFP_NOFS);
  70. if (unlikely(!buf))
  71. return -ENOMEM;
  72. maxmembs = PAGE_SIZE / argv->v_size;
  73. ret = 0;
  74. total = 0;
  75. pos = argv->v_index;
  76. for (i = 0; i < argv->v_nmembs; i += n) {
  77. n = (argv->v_nmembs - i < maxmembs) ?
  78. argv->v_nmembs - i : maxmembs;
  79. if ((dir & _IOC_WRITE) &&
  80. copy_from_user(buf, base + argv->v_size * i,
  81. argv->v_size * n)) {
  82. ret = -EFAULT;
  83. break;
  84. }
  85. ppos = pos;
  86. nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
  87. n);
  88. if (nr < 0) {
  89. ret = nr;
  90. break;
  91. }
  92. if ((dir & _IOC_READ) &&
  93. copy_to_user(base + argv->v_size * i, buf,
  94. argv->v_size * nr)) {
  95. ret = -EFAULT;
  96. break;
  97. }
  98. total += nr;
  99. if ((size_t)nr < n)
  100. break;
  101. if (pos == ppos)
  102. pos += n;
  103. }
  104. argv->v_nmembs = total;
  105. free_pages((unsigned long)buf, 0);
  106. return ret;
  107. }
  108. /**
  109. * nilfs_fileattr_get - retrieve miscellaneous file attributes
  110. * @dentry: the object to retrieve from
  111. * @fa: fileattr pointer
  112. *
  113. * Return: always 0 as success.
  114. */
  115. int nilfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  116. {
  117. struct inode *inode = d_inode(dentry);
  118. fileattr_fill_flags(fa, NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE);
  119. return 0;
  120. }
  121. /**
  122. * nilfs_fileattr_set - change miscellaneous file attributes
  123. * @idmap: idmap of the mount
  124. * @dentry: the object to change
  125. * @fa: fileattr pointer
  126. *
  127. * Return: 0 on success, or a negative error code on failure.
  128. */
  129. int nilfs_fileattr_set(struct mnt_idmap *idmap,
  130. struct dentry *dentry, struct fileattr *fa)
  131. {
  132. struct inode *inode = d_inode(dentry);
  133. struct nilfs_transaction_info ti;
  134. unsigned int flags, oldflags;
  135. int ret;
  136. if (fileattr_has_fsx(fa))
  137. return -EOPNOTSUPP;
  138. flags = nilfs_mask_flags(inode->i_mode, fa->flags);
  139. ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
  140. if (ret)
  141. return ret;
  142. oldflags = NILFS_I(inode)->i_flags & ~FS_FL_USER_MODIFIABLE;
  143. NILFS_I(inode)->i_flags = oldflags | (flags & FS_FL_USER_MODIFIABLE);
  144. nilfs_set_inode_flags(inode);
  145. inode_set_ctime_current(inode);
  146. if (IS_SYNC(inode))
  147. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  148. nilfs_mark_inode_dirty(inode);
  149. return nilfs_transaction_commit(inode->i_sb);
  150. }
  151. /**
  152. * nilfs_ioctl_getversion - get info about a file's version (generation number)
  153. * @inode: inode object
  154. * @argp: userspace memory where the generation number of @inode is stored
  155. *
  156. * Return: 0 on success, or %-EFAULT on error.
  157. */
  158. static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
  159. {
  160. return put_user(inode->i_generation, (int __user *)argp);
  161. }
  162. /**
  163. * nilfs_ioctl_change_cpmode - change checkpoint mode (checkpoint/snapshot)
  164. * @inode: inode object
  165. * @filp: file object
  166. * @cmd: ioctl's request code
  167. * @argp: pointer on argument from userspace
  168. *
  169. * Description: nilfs_ioctl_change_cpmode() function changes mode of
  170. * given checkpoint between checkpoint and snapshot state. This ioctl
  171. * is used in chcp and mkcp utilities.
  172. *
  173. * Return Value: On success, 0 is returned and mode of a checkpoint is
  174. * changed. On error, one of the following negative error codes
  175. * is returned.
  176. *
  177. * %-EPERM - Operation not permitted.
  178. *
  179. * %-EFAULT - Failure during checkpoint mode changing.
  180. */
  181. static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
  182. unsigned int cmd, void __user *argp)
  183. {
  184. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  185. struct nilfs_transaction_info ti;
  186. struct nilfs_cpmode cpmode;
  187. int ret;
  188. if (!capable(CAP_SYS_ADMIN))
  189. return -EPERM;
  190. ret = mnt_want_write_file(filp);
  191. if (ret)
  192. return ret;
  193. ret = -EFAULT;
  194. if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
  195. goto out;
  196. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  197. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  198. ret = nilfs_cpfile_change_cpmode(
  199. nilfs->ns_cpfile, cpmode.cm_cno, cpmode.cm_mode);
  200. if (unlikely(ret < 0))
  201. nilfs_transaction_abort(inode->i_sb);
  202. else
  203. nilfs_transaction_commit(inode->i_sb); /* never fails */
  204. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  205. out:
  206. mnt_drop_write_file(filp);
  207. return ret;
  208. }
  209. /**
  210. * nilfs_ioctl_delete_checkpoint - remove checkpoint
  211. * @inode: inode object
  212. * @filp: file object
  213. * @cmd: ioctl's request code
  214. * @argp: pointer on argument from userspace
  215. *
  216. * Description: nilfs_ioctl_delete_checkpoint() function removes
  217. * checkpoint from NILFS2 file system. This ioctl is used in rmcp
  218. * utility.
  219. *
  220. * Return Value: On success, 0 is returned and a checkpoint is
  221. * removed. On error, one of the following negative error codes
  222. * is returned.
  223. *
  224. * %-EPERM - Operation not permitted.
  225. *
  226. * %-EFAULT - Failure during checkpoint removing.
  227. */
  228. static int
  229. nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
  230. unsigned int cmd, void __user *argp)
  231. {
  232. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  233. struct nilfs_transaction_info ti;
  234. __u64 cno;
  235. int ret;
  236. if (!capable(CAP_SYS_ADMIN))
  237. return -EPERM;
  238. ret = mnt_want_write_file(filp);
  239. if (ret)
  240. return ret;
  241. ret = -EFAULT;
  242. if (copy_from_user(&cno, argp, sizeof(cno)))
  243. goto out;
  244. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  245. ret = nilfs_cpfile_delete_checkpoint(nilfs->ns_cpfile, cno);
  246. if (unlikely(ret < 0))
  247. nilfs_transaction_abort(inode->i_sb);
  248. else
  249. nilfs_transaction_commit(inode->i_sb); /* never fails */
  250. out:
  251. mnt_drop_write_file(filp);
  252. return ret;
  253. }
  254. /**
  255. * nilfs_ioctl_do_get_cpinfo - callback method getting info about checkpoints
  256. * @nilfs: nilfs object
  257. * @posp: pointer on array of checkpoint's numbers
  258. * @flags: checkpoint mode (checkpoint or snapshot)
  259. * @buf: buffer for storing checkponts' info
  260. * @size: size in bytes of one checkpoint info item in array
  261. * @nmembs: number of checkpoints in array (numbers and infos)
  262. *
  263. * Description: nilfs_ioctl_do_get_cpinfo() function returns info about
  264. * requested checkpoints. The NILFS_IOCTL_GET_CPINFO ioctl is used in
  265. * lscp utility and by nilfs_cleanerd daemon.
  266. *
  267. * Return value: count of nilfs_cpinfo structures in output buffer.
  268. */
  269. static ssize_t
  270. nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  271. void *buf, size_t size, size_t nmembs)
  272. {
  273. int ret;
  274. down_read(&nilfs->ns_segctor_sem);
  275. ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
  276. size, nmembs);
  277. up_read(&nilfs->ns_segctor_sem);
  278. return ret;
  279. }
  280. /**
  281. * nilfs_ioctl_get_cpstat - get checkpoints statistics
  282. * @inode: inode object
  283. * @filp: file object
  284. * @cmd: ioctl's request code
  285. * @argp: pointer on argument from userspace
  286. *
  287. * Description: nilfs_ioctl_get_cpstat() returns information about checkpoints.
  288. * The NILFS_IOCTL_GET_CPSTAT ioctl is used by lscp, rmcp utilities
  289. * and by nilfs_cleanerd daemon.
  290. *
  291. * Return Value: On success, 0 is returned, and checkpoints information is
  292. * copied into userspace pointer @argp. On error, one of the following
  293. * negative error codes is returned.
  294. *
  295. * %-EIO - I/O error.
  296. *
  297. * %-ENOMEM - Insufficient amount of memory available.
  298. *
  299. * %-EFAULT - Failure during getting checkpoints statistics.
  300. */
  301. static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
  302. unsigned int cmd, void __user *argp)
  303. {
  304. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  305. struct nilfs_cpstat cpstat;
  306. int ret;
  307. down_read(&nilfs->ns_segctor_sem);
  308. ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  309. up_read(&nilfs->ns_segctor_sem);
  310. if (ret < 0)
  311. return ret;
  312. if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
  313. ret = -EFAULT;
  314. return ret;
  315. }
  316. /**
  317. * nilfs_ioctl_do_get_suinfo - callback method getting segment usage info
  318. * @nilfs: nilfs object
  319. * @posp: pointer on array of segment numbers
  320. * @flags: *not used*
  321. * @buf: buffer for storing suinfo array
  322. * @size: size in bytes of one suinfo item in array
  323. * @nmembs: count of segment numbers and suinfos in array
  324. *
  325. * Description: nilfs_ioctl_do_get_suinfo() function returns segment usage
  326. * info about requested segments. The NILFS_IOCTL_GET_SUINFO ioctl is used
  327. * in lssu, nilfs_resize utilities and by nilfs_cleanerd daemon.
  328. *
  329. * Return value: count of nilfs_suinfo structures in output buffer.
  330. */
  331. static ssize_t
  332. nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  333. void *buf, size_t size, size_t nmembs)
  334. {
  335. int ret;
  336. down_read(&nilfs->ns_segctor_sem);
  337. ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
  338. nmembs);
  339. up_read(&nilfs->ns_segctor_sem);
  340. return ret;
  341. }
  342. /**
  343. * nilfs_ioctl_get_sustat - get segment usage statistics
  344. * @inode: inode object
  345. * @filp: file object
  346. * @cmd: ioctl's request code
  347. * @argp: pointer on argument from userspace
  348. *
  349. * Description: nilfs_ioctl_get_sustat() returns segment usage statistics.
  350. * The NILFS_IOCTL_GET_SUSTAT ioctl is used in lssu, nilfs_resize utilities
  351. * and by nilfs_cleanerd daemon.
  352. *
  353. * Return Value: On success, 0 is returned, and segment usage information is
  354. * copied into userspace pointer @argp. On error, one of the following
  355. * negative error codes is returned.
  356. *
  357. * %-EIO - I/O error.
  358. *
  359. * %-ENOMEM - Insufficient amount of memory available.
  360. *
  361. * %-EFAULT - Failure during getting segment usage statistics.
  362. */
  363. static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
  364. unsigned int cmd, void __user *argp)
  365. {
  366. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  367. struct nilfs_sustat sustat;
  368. int ret;
  369. down_read(&nilfs->ns_segctor_sem);
  370. ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
  371. up_read(&nilfs->ns_segctor_sem);
  372. if (ret < 0)
  373. return ret;
  374. if (copy_to_user(argp, &sustat, sizeof(sustat)))
  375. ret = -EFAULT;
  376. return ret;
  377. }
  378. /**
  379. * nilfs_ioctl_do_get_vinfo - callback method getting virtual blocks info
  380. * @nilfs: nilfs object
  381. * @posp: *not used*
  382. * @flags: *not used*
  383. * @buf: buffer for storing array of nilfs_vinfo structures
  384. * @size: size in bytes of one vinfo item in array
  385. * @nmembs: count of vinfos in array
  386. *
  387. * Description: nilfs_ioctl_do_get_vinfo() function returns information
  388. * on virtual block addresses. The NILFS_IOCTL_GET_VINFO ioctl is used
  389. * by nilfs_cleanerd daemon.
  390. *
  391. * Return value: count of nilfs_vinfo structures in output buffer.
  392. */
  393. static ssize_t
  394. nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  395. void *buf, size_t size, size_t nmembs)
  396. {
  397. int ret;
  398. down_read(&nilfs->ns_segctor_sem);
  399. ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
  400. up_read(&nilfs->ns_segctor_sem);
  401. return ret;
  402. }
  403. /**
  404. * nilfs_ioctl_do_get_bdescs - callback method getting disk block descriptors
  405. * @nilfs: nilfs object
  406. * @posp: *not used*
  407. * @flags: *not used*
  408. * @buf: buffer for storing array of nilfs_bdesc structures
  409. * @size: size in bytes of one bdesc item in array
  410. * @nmembs: count of bdescs in array
  411. *
  412. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  413. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  414. * is used by nilfs_cleanerd daemon.
  415. *
  416. * Return value: count of nilfs_bdescs structures in output buffer.
  417. */
  418. static ssize_t
  419. nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
  420. void *buf, size_t size, size_t nmembs)
  421. {
  422. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  423. struct nilfs_bdesc *bdescs = buf;
  424. int ret, i;
  425. down_read(&nilfs->ns_segctor_sem);
  426. for (i = 0; i < nmembs; i++) {
  427. ret = nilfs_bmap_lookup_at_level(bmap,
  428. bdescs[i].bd_offset,
  429. bdescs[i].bd_level + 1,
  430. &bdescs[i].bd_blocknr);
  431. if (ret < 0) {
  432. if (ret != -ENOENT) {
  433. up_read(&nilfs->ns_segctor_sem);
  434. return ret;
  435. }
  436. bdescs[i].bd_blocknr = 0;
  437. }
  438. }
  439. up_read(&nilfs->ns_segctor_sem);
  440. return nmembs;
  441. }
  442. /**
  443. * nilfs_ioctl_get_bdescs - get disk block descriptors
  444. * @inode: inode object
  445. * @filp: file object
  446. * @cmd: ioctl's request code
  447. * @argp: pointer on argument from userspace
  448. *
  449. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  450. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  451. * is used by nilfs_cleanerd daemon.
  452. *
  453. * Return Value: On success, 0 is returned, and disk block descriptors are
  454. * copied into userspace pointer @argp. On error, one of the following
  455. * negative error codes is returned.
  456. *
  457. * %-EINVAL - Invalid arguments from userspace.
  458. *
  459. * %-EIO - I/O error.
  460. *
  461. * %-ENOMEM - Insufficient amount of memory available.
  462. *
  463. * %-EFAULT - Failure during getting disk block descriptors.
  464. */
  465. static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
  466. unsigned int cmd, void __user *argp)
  467. {
  468. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  469. struct nilfs_argv argv;
  470. int ret;
  471. if (copy_from_user(&argv, argp, sizeof(argv)))
  472. return -EFAULT;
  473. if (argv.v_size != sizeof(struct nilfs_bdesc))
  474. return -EINVAL;
  475. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
  476. nilfs_ioctl_do_get_bdescs);
  477. if (ret < 0)
  478. return ret;
  479. if (copy_to_user(argp, &argv, sizeof(argv)))
  480. ret = -EFAULT;
  481. return ret;
  482. }
  483. /**
  484. * nilfs_ioctl_move_inode_block - prepare data/node block for moving by GC
  485. * @inode: inode object
  486. * @vdesc: descriptor of virtual block number
  487. * @buffers: list of moving buffers
  488. *
  489. * Description: nilfs_ioctl_move_inode_block() function registers data/node
  490. * buffer in the GC pagecache and submit read request.
  491. *
  492. * Return Value: On success, 0 is returned. On error, one of the following
  493. * negative error codes is returned.
  494. *
  495. * %-EIO - I/O error.
  496. *
  497. * %-ENOMEM - Insufficient amount of memory available.
  498. *
  499. * %-ENOENT - Requested block doesn't exist.
  500. *
  501. * %-EEXIST - Blocks conflict is detected.
  502. */
  503. static int nilfs_ioctl_move_inode_block(struct inode *inode,
  504. struct nilfs_vdesc *vdesc,
  505. struct list_head *buffers)
  506. {
  507. struct buffer_head *bh;
  508. int ret;
  509. if (vdesc->vd_flags == 0)
  510. ret = nilfs_gccache_submit_read_data(
  511. inode, vdesc->vd_offset, vdesc->vd_blocknr,
  512. vdesc->vd_vblocknr, &bh);
  513. else
  514. ret = nilfs_gccache_submit_read_node(
  515. inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
  516. if (unlikely(ret < 0)) {
  517. if (ret == -ENOENT)
  518. nilfs_crit(inode->i_sb,
  519. "%s: invalid virtual block address (%s): ino=%llu, cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu",
  520. __func__, vdesc->vd_flags ? "node" : "data",
  521. (unsigned long long)vdesc->vd_ino,
  522. (unsigned long long)vdesc->vd_cno,
  523. (unsigned long long)vdesc->vd_offset,
  524. (unsigned long long)vdesc->vd_blocknr,
  525. (unsigned long long)vdesc->vd_vblocknr);
  526. return ret;
  527. }
  528. if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
  529. nilfs_crit(inode->i_sb,
  530. "%s: conflicting %s buffer: ino=%llu, cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu",
  531. __func__, vdesc->vd_flags ? "node" : "data",
  532. (unsigned long long)vdesc->vd_ino,
  533. (unsigned long long)vdesc->vd_cno,
  534. (unsigned long long)vdesc->vd_offset,
  535. (unsigned long long)vdesc->vd_blocknr,
  536. (unsigned long long)vdesc->vd_vblocknr);
  537. brelse(bh);
  538. return -EEXIST;
  539. }
  540. list_add_tail(&bh->b_assoc_buffers, buffers);
  541. return 0;
  542. }
  543. /**
  544. * nilfs_ioctl_move_blocks - move valid inode's blocks during garbage collection
  545. * @sb: superblock object
  546. * @argv: vector of arguments from userspace
  547. * @buf: array of nilfs_vdesc structures
  548. *
  549. * Description: nilfs_ioctl_move_blocks() function reads valid data/node
  550. * blocks that garbage collector specified with the array of nilfs_vdesc
  551. * structures and stores them into page caches of GC inodes.
  552. *
  553. * Return Value: Number of processed nilfs_vdesc structures or
  554. * error code, otherwise.
  555. */
  556. static int nilfs_ioctl_move_blocks(struct super_block *sb,
  557. struct nilfs_argv *argv, void *buf)
  558. {
  559. size_t nmembs = argv->v_nmembs;
  560. struct the_nilfs *nilfs = sb->s_fs_info;
  561. struct inode *inode;
  562. struct nilfs_vdesc *vdesc;
  563. struct buffer_head *bh, *n;
  564. LIST_HEAD(buffers);
  565. ino_t ino;
  566. __u64 cno;
  567. int i, ret;
  568. for (i = 0, vdesc = buf; i < nmembs; ) {
  569. ino = vdesc->vd_ino;
  570. cno = vdesc->vd_cno;
  571. inode = nilfs_iget_for_gc(sb, ino, cno);
  572. if (IS_ERR(inode)) {
  573. ret = PTR_ERR(inode);
  574. goto failed;
  575. }
  576. if (list_empty(&NILFS_I(inode)->i_dirty)) {
  577. /*
  578. * Add the inode to GC inode list. Garbage Collection
  579. * is serialized and no two processes manipulate the
  580. * list simultaneously.
  581. */
  582. igrab(inode);
  583. list_add(&NILFS_I(inode)->i_dirty,
  584. &nilfs->ns_gc_inodes);
  585. }
  586. do {
  587. ret = nilfs_ioctl_move_inode_block(inode, vdesc,
  588. &buffers);
  589. if (unlikely(ret < 0)) {
  590. iput(inode);
  591. goto failed;
  592. }
  593. vdesc++;
  594. } while (++i < nmembs &&
  595. vdesc->vd_ino == ino && vdesc->vd_cno == cno);
  596. iput(inode); /* The inode still remains in GC inode list */
  597. }
  598. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  599. ret = nilfs_gccache_wait_and_mark_dirty(bh);
  600. if (unlikely(ret < 0)) {
  601. WARN_ON(ret == -EEXIST);
  602. goto failed;
  603. }
  604. list_del_init(&bh->b_assoc_buffers);
  605. brelse(bh);
  606. }
  607. return nmembs;
  608. failed:
  609. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  610. list_del_init(&bh->b_assoc_buffers);
  611. brelse(bh);
  612. }
  613. return ret;
  614. }
  615. /**
  616. * nilfs_ioctl_delete_checkpoints - delete checkpoints
  617. * @nilfs: nilfs object
  618. * @argv: vector of arguments from userspace
  619. * @buf: array of periods of checkpoints numbers
  620. *
  621. * Description: nilfs_ioctl_delete_checkpoints() function deletes checkpoints
  622. * in the period from p_start to p_end, excluding p_end itself. The checkpoints
  623. * which have been already deleted are ignored.
  624. *
  625. * Return Value: Number of processed nilfs_period structures or
  626. * error code, otherwise.
  627. *
  628. * %-EIO - I/O error.
  629. *
  630. * %-ENOMEM - Insufficient amount of memory available.
  631. *
  632. * %-EINVAL - invalid checkpoints.
  633. */
  634. static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
  635. struct nilfs_argv *argv, void *buf)
  636. {
  637. size_t nmembs = argv->v_nmembs;
  638. struct inode *cpfile = nilfs->ns_cpfile;
  639. struct nilfs_period *periods = buf;
  640. int ret, i;
  641. for (i = 0; i < nmembs; i++) {
  642. ret = nilfs_cpfile_delete_checkpoints(
  643. cpfile, periods[i].p_start, periods[i].p_end);
  644. if (ret < 0)
  645. return ret;
  646. }
  647. return nmembs;
  648. }
  649. /**
  650. * nilfs_ioctl_free_vblocknrs - free virtual block numbers
  651. * @nilfs: nilfs object
  652. * @argv: vector of arguments from userspace
  653. * @buf: array of virtual block numbers
  654. *
  655. * Description: nilfs_ioctl_free_vblocknrs() function frees
  656. * the virtual block numbers specified by @buf and @argv->v_nmembs.
  657. *
  658. * Return Value: Number of processed virtual block numbers or
  659. * error code, otherwise.
  660. *
  661. * %-EIO - I/O error.
  662. *
  663. * %-ENOMEM - Insufficient amount of memory available.
  664. *
  665. * %-ENOENT - The virtual block number have not been allocated.
  666. */
  667. static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
  668. struct nilfs_argv *argv, void *buf)
  669. {
  670. size_t nmembs = argv->v_nmembs;
  671. int ret;
  672. ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
  673. return (ret < 0) ? ret : nmembs;
  674. }
  675. /**
  676. * nilfs_ioctl_mark_blocks_dirty - mark blocks dirty
  677. * @nilfs: nilfs object
  678. * @argv: vector of arguments from userspace
  679. * @buf: array of block descriptors
  680. *
  681. * Description: nilfs_ioctl_mark_blocks_dirty() function marks
  682. * metadata file or data blocks as dirty.
  683. *
  684. * Return Value: Number of processed block descriptors or
  685. * error code, otherwise.
  686. *
  687. * %-ENOMEM - Insufficient memory available.
  688. *
  689. * %-EIO - I/O error
  690. *
  691. * %-ENOENT - the specified block does not exist (hole block)
  692. */
  693. static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
  694. struct nilfs_argv *argv, void *buf)
  695. {
  696. size_t nmembs = argv->v_nmembs;
  697. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  698. struct nilfs_bdesc *bdescs = buf;
  699. struct buffer_head *bh;
  700. int ret, i;
  701. for (i = 0; i < nmembs; i++) {
  702. /* XXX: use macro or inline func to check liveness */
  703. ret = nilfs_bmap_lookup_at_level(bmap,
  704. bdescs[i].bd_offset,
  705. bdescs[i].bd_level + 1,
  706. &bdescs[i].bd_blocknr);
  707. if (ret < 0) {
  708. if (ret != -ENOENT)
  709. return ret;
  710. bdescs[i].bd_blocknr = 0;
  711. }
  712. if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
  713. /* skip dead block */
  714. continue;
  715. if (bdescs[i].bd_level == 0) {
  716. ret = nilfs_mdt_get_block(nilfs->ns_dat,
  717. bdescs[i].bd_offset,
  718. false, NULL, &bh);
  719. if (unlikely(ret)) {
  720. WARN_ON(ret == -ENOENT);
  721. return ret;
  722. }
  723. mark_buffer_dirty(bh);
  724. nilfs_mdt_mark_dirty(nilfs->ns_dat);
  725. put_bh(bh);
  726. } else {
  727. ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
  728. bdescs[i].bd_level);
  729. if (ret < 0) {
  730. WARN_ON(ret == -ENOENT);
  731. return ret;
  732. }
  733. }
  734. }
  735. return nmembs;
  736. }
  737. int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
  738. struct nilfs_argv *argv, void **kbufs)
  739. {
  740. const char *msg;
  741. int ret;
  742. ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
  743. if (ret < 0) {
  744. /*
  745. * can safely abort because checkpoints can be removed
  746. * independently.
  747. */
  748. msg = "cannot delete checkpoints";
  749. goto failed;
  750. }
  751. ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
  752. if (ret < 0) {
  753. /*
  754. * can safely abort because DAT file is updated atomically
  755. * using a copy-on-write technique.
  756. */
  757. msg = "cannot delete virtual blocks from DAT file";
  758. goto failed;
  759. }
  760. ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
  761. if (ret < 0) {
  762. /*
  763. * can safely abort because the operation is nondestructive.
  764. */
  765. msg = "cannot mark copying blocks dirty";
  766. goto failed;
  767. }
  768. return 0;
  769. failed:
  770. nilfs_err(nilfs->ns_sb, "error %d preparing GC: %s", ret, msg);
  771. return ret;
  772. }
  773. /**
  774. * nilfs_ioctl_clean_segments - clean segments
  775. * @inode: inode object
  776. * @filp: file object
  777. * @cmd: ioctl's request code
  778. * @argp: pointer on argument from userspace
  779. *
  780. * Description: nilfs_ioctl_clean_segments() function makes garbage
  781. * collection operation in the environment of requested parameters
  782. * from userspace. The NILFS_IOCTL_CLEAN_SEGMENTS ioctl is used by
  783. * nilfs_cleanerd daemon.
  784. *
  785. * Return Value: On success, 0 is returned or error code, otherwise.
  786. */
  787. static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
  788. unsigned int cmd, void __user *argp)
  789. {
  790. struct nilfs_argv argv[5];
  791. static const size_t argsz[5] = {
  792. sizeof(struct nilfs_vdesc),
  793. sizeof(struct nilfs_period),
  794. sizeof(__u64),
  795. sizeof(struct nilfs_bdesc),
  796. sizeof(__u64),
  797. };
  798. void __user *base;
  799. void *kbufs[5];
  800. struct the_nilfs *nilfs;
  801. size_t len, nsegs;
  802. int n, ret;
  803. if (!capable(CAP_SYS_ADMIN))
  804. return -EPERM;
  805. ret = mnt_want_write_file(filp);
  806. if (ret)
  807. return ret;
  808. ret = -EFAULT;
  809. if (copy_from_user(argv, argp, sizeof(argv)))
  810. goto out;
  811. ret = -EINVAL;
  812. nsegs = argv[4].v_nmembs;
  813. if (argv[4].v_size != argsz[4])
  814. goto out;
  815. /*
  816. * argv[4] points to segment numbers this ioctl cleans. We
  817. * use kmalloc() for its buffer because the memory used for the
  818. * segment numbers is small enough.
  819. */
  820. kbufs[4] = memdup_array_user((void __user *)(unsigned long)argv[4].v_base,
  821. nsegs, sizeof(__u64));
  822. if (IS_ERR(kbufs[4])) {
  823. ret = PTR_ERR(kbufs[4]);
  824. goto out;
  825. }
  826. nilfs = inode->i_sb->s_fs_info;
  827. for (n = 0; n < 4; n++) {
  828. ret = -EINVAL;
  829. if (argv[n].v_size != argsz[n])
  830. goto out_free;
  831. if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
  832. goto out_free;
  833. if (argv[n].v_nmembs >= UINT_MAX / argv[n].v_size)
  834. goto out_free;
  835. len = argv[n].v_size * argv[n].v_nmembs;
  836. base = (void __user *)(unsigned long)argv[n].v_base;
  837. if (len == 0) {
  838. kbufs[n] = NULL;
  839. continue;
  840. }
  841. kbufs[n] = vmalloc(len);
  842. if (!kbufs[n]) {
  843. ret = -ENOMEM;
  844. goto out_free;
  845. }
  846. if (copy_from_user(kbufs[n], base, len)) {
  847. ret = -EFAULT;
  848. vfree(kbufs[n]);
  849. goto out_free;
  850. }
  851. }
  852. /*
  853. * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
  854. * which will operates an inode list without blocking.
  855. * To protect the list from concurrent operations,
  856. * nilfs_ioctl_move_blocks should be atomic operation.
  857. */
  858. if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
  859. ret = -EBUSY;
  860. goto out_free;
  861. }
  862. ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
  863. if (ret < 0) {
  864. nilfs_err(inode->i_sb,
  865. "error %d preparing GC: cannot read source blocks",
  866. ret);
  867. } else {
  868. if (nilfs_sb_need_update(nilfs))
  869. set_nilfs_discontinued(nilfs);
  870. ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
  871. }
  872. nilfs_remove_all_gcinodes(nilfs);
  873. clear_nilfs_gc_running(nilfs);
  874. out_free:
  875. while (--n >= 0)
  876. vfree(kbufs[n]);
  877. kfree(kbufs[4]);
  878. out:
  879. mnt_drop_write_file(filp);
  880. return ret;
  881. }
  882. /**
  883. * nilfs_ioctl_sync - make a checkpoint
  884. * @inode: inode object
  885. * @filp: file object
  886. * @cmd: ioctl's request code
  887. * @argp: pointer on argument from userspace
  888. *
  889. * Description: nilfs_ioctl_sync() function constructs a logical segment
  890. * for checkpointing. This function guarantees that all modified data
  891. * and metadata are written out to the device when it successfully
  892. * returned.
  893. *
  894. * Return Value: On success, 0 is retured. On errors, one of the following
  895. * negative error code is returned.
  896. *
  897. * %-EROFS - Read only filesystem.
  898. *
  899. * %-EIO - I/O error
  900. *
  901. * %-ENOSPC - No space left on device (only in a panic state).
  902. *
  903. * %-ERESTARTSYS - Interrupted.
  904. *
  905. * %-ENOMEM - Insufficient memory available.
  906. *
  907. * %-EFAULT - Failure during execution of requested operation.
  908. */
  909. static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
  910. unsigned int cmd, void __user *argp)
  911. {
  912. __u64 cno;
  913. int ret;
  914. struct the_nilfs *nilfs;
  915. ret = nilfs_construct_segment(inode->i_sb);
  916. if (ret < 0)
  917. return ret;
  918. nilfs = inode->i_sb->s_fs_info;
  919. ret = nilfs_flush_device(nilfs);
  920. if (ret < 0)
  921. return ret;
  922. if (argp != NULL) {
  923. down_read(&nilfs->ns_segctor_sem);
  924. cno = nilfs->ns_cno - 1;
  925. up_read(&nilfs->ns_segctor_sem);
  926. if (copy_to_user(argp, &cno, sizeof(cno)))
  927. return -EFAULT;
  928. }
  929. return 0;
  930. }
  931. /**
  932. * nilfs_ioctl_resize - resize NILFS2 volume
  933. * @inode: inode object
  934. * @filp: file object
  935. * @argp: pointer on argument from userspace
  936. *
  937. * Return Value: On success, 0 is returned or error code, otherwise.
  938. */
  939. static int nilfs_ioctl_resize(struct inode *inode, struct file *filp,
  940. void __user *argp)
  941. {
  942. __u64 newsize;
  943. int ret = -EPERM;
  944. if (!capable(CAP_SYS_ADMIN))
  945. goto out;
  946. ret = mnt_want_write_file(filp);
  947. if (ret)
  948. goto out;
  949. ret = -EFAULT;
  950. if (copy_from_user(&newsize, argp, sizeof(newsize)))
  951. goto out_drop_write;
  952. ret = nilfs_resize_fs(inode->i_sb, newsize);
  953. out_drop_write:
  954. mnt_drop_write_file(filp);
  955. out:
  956. return ret;
  957. }
  958. /**
  959. * nilfs_ioctl_trim_fs() - trim ioctl handle function
  960. * @inode: inode object
  961. * @argp: pointer on argument from userspace
  962. *
  963. * Description: nilfs_ioctl_trim_fs is the FITRIM ioctl handle function. It
  964. * checks the arguments from userspace and calls nilfs_sufile_trim_fs, which
  965. * performs the actual trim operation.
  966. *
  967. * Return Value: On success, 0 is returned or negative error code, otherwise.
  968. */
  969. static int nilfs_ioctl_trim_fs(struct inode *inode, void __user *argp)
  970. {
  971. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  972. struct fstrim_range range;
  973. int ret;
  974. if (!capable(CAP_SYS_ADMIN))
  975. return -EPERM;
  976. if (!bdev_max_discard_sectors(nilfs->ns_bdev))
  977. return -EOPNOTSUPP;
  978. if (copy_from_user(&range, argp, sizeof(range)))
  979. return -EFAULT;
  980. range.minlen = max_t(u64, range.minlen,
  981. bdev_discard_granularity(nilfs->ns_bdev));
  982. down_read(&nilfs->ns_segctor_sem);
  983. ret = nilfs_sufile_trim_fs(nilfs->ns_sufile, &range);
  984. up_read(&nilfs->ns_segctor_sem);
  985. if (ret < 0)
  986. return ret;
  987. if (copy_to_user(argp, &range, sizeof(range)))
  988. return -EFAULT;
  989. return 0;
  990. }
  991. /**
  992. * nilfs_ioctl_set_alloc_range - limit range of segments to be allocated
  993. * @inode: inode object
  994. * @argp: pointer on argument from userspace
  995. *
  996. * Description: nilfs_ioctl_set_alloc_range() function defines lower limit
  997. * of segments in bytes and upper limit of segments in bytes.
  998. * The NILFS_IOCTL_SET_ALLOC_RANGE is used by nilfs_resize utility.
  999. *
  1000. * Return Value: On success, 0 is returned or error code, otherwise.
  1001. */
  1002. static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
  1003. {
  1004. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1005. __u64 range[2];
  1006. __u64 minseg, maxseg;
  1007. unsigned long segbytes;
  1008. int ret = -EPERM;
  1009. if (!capable(CAP_SYS_ADMIN))
  1010. goto out;
  1011. ret = -EFAULT;
  1012. if (copy_from_user(range, argp, sizeof(__u64[2])))
  1013. goto out;
  1014. ret = -ERANGE;
  1015. if (range[1] > bdev_nr_bytes(inode->i_sb->s_bdev))
  1016. goto out;
  1017. segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
  1018. minseg = range[0] + segbytes - 1;
  1019. minseg = div64_ul(minseg, segbytes);
  1020. if (range[1] < 4096)
  1021. goto out;
  1022. maxseg = NILFS_SB2_OFFSET_BYTES(range[1]);
  1023. if (maxseg < segbytes)
  1024. goto out;
  1025. maxseg = div64_ul(maxseg, segbytes);
  1026. maxseg--;
  1027. ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
  1028. out:
  1029. return ret;
  1030. }
  1031. /**
  1032. * nilfs_ioctl_get_info - wrapping function of get metadata info
  1033. * @inode: inode object
  1034. * @filp: file object
  1035. * @cmd: ioctl's request code
  1036. * @argp: pointer on argument from userspace
  1037. * @membsz: size of an item in bytes
  1038. * @dofunc: concrete function of getting metadata info
  1039. *
  1040. * Description: nilfs_ioctl_get_info() gets metadata info by means of
  1041. * calling dofunc() function.
  1042. *
  1043. * Return Value: On success, 0 is returned and requested metadata info
  1044. * is copied into userspace. On error, one of the following
  1045. * negative error codes is returned.
  1046. *
  1047. * %-EINVAL - Invalid arguments from userspace.
  1048. *
  1049. * %-ENOMEM - Insufficient amount of memory available.
  1050. *
  1051. * %-EFAULT - Failure during execution of requested operation.
  1052. */
  1053. static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
  1054. unsigned int cmd, void __user *argp,
  1055. size_t membsz,
  1056. ssize_t (*dofunc)(struct the_nilfs *,
  1057. __u64 *, int,
  1058. void *, size_t, size_t))
  1059. {
  1060. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1061. struct nilfs_argv argv;
  1062. int ret;
  1063. if (copy_from_user(&argv, argp, sizeof(argv)))
  1064. return -EFAULT;
  1065. if (argv.v_size < membsz)
  1066. return -EINVAL;
  1067. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
  1068. if (ret < 0)
  1069. return ret;
  1070. if (copy_to_user(argp, &argv, sizeof(argv)))
  1071. ret = -EFAULT;
  1072. return ret;
  1073. }
  1074. /**
  1075. * nilfs_ioctl_set_suinfo - set segment usage info
  1076. * @inode: inode object
  1077. * @filp: file object
  1078. * @cmd: ioctl's request code
  1079. * @argp: pointer on argument from userspace
  1080. *
  1081. * Description: Expects an array of nilfs_suinfo_update structures
  1082. * encapsulated in nilfs_argv and updates the segment usage info
  1083. * according to the flags in nilfs_suinfo_update.
  1084. *
  1085. * Return Value: On success, 0 is returned. On error, one of the
  1086. * following negative error codes is returned.
  1087. *
  1088. * %-EPERM - Not enough permissions
  1089. *
  1090. * %-EFAULT - Error copying input data
  1091. *
  1092. * %-EIO - I/O error.
  1093. *
  1094. * %-ENOMEM - Insufficient amount of memory available.
  1095. *
  1096. * %-EINVAL - Invalid values in input (segment number, flags or nblocks)
  1097. */
  1098. static int nilfs_ioctl_set_suinfo(struct inode *inode, struct file *filp,
  1099. unsigned int cmd, void __user *argp)
  1100. {
  1101. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  1102. struct nilfs_transaction_info ti;
  1103. struct nilfs_argv argv;
  1104. size_t len;
  1105. void __user *base;
  1106. void *kbuf;
  1107. int ret;
  1108. if (!capable(CAP_SYS_ADMIN))
  1109. return -EPERM;
  1110. ret = mnt_want_write_file(filp);
  1111. if (ret)
  1112. return ret;
  1113. ret = -EFAULT;
  1114. if (copy_from_user(&argv, argp, sizeof(argv)))
  1115. goto out;
  1116. ret = -EINVAL;
  1117. if (argv.v_size < sizeof(struct nilfs_suinfo_update))
  1118. goto out;
  1119. if (argv.v_nmembs > nilfs->ns_nsegments)
  1120. goto out;
  1121. if (argv.v_nmembs >= UINT_MAX / argv.v_size)
  1122. goto out;
  1123. len = argv.v_size * argv.v_nmembs;
  1124. if (!len) {
  1125. ret = 0;
  1126. goto out;
  1127. }
  1128. base = (void __user *)(unsigned long)argv.v_base;
  1129. kbuf = vmalloc(len);
  1130. if (!kbuf) {
  1131. ret = -ENOMEM;
  1132. goto out;
  1133. }
  1134. if (copy_from_user(kbuf, base, len)) {
  1135. ret = -EFAULT;
  1136. goto out_free;
  1137. }
  1138. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  1139. ret = nilfs_sufile_set_suinfo(nilfs->ns_sufile, kbuf, argv.v_size,
  1140. argv.v_nmembs);
  1141. if (unlikely(ret < 0))
  1142. nilfs_transaction_abort(inode->i_sb);
  1143. else
  1144. nilfs_transaction_commit(inode->i_sb); /* never fails */
  1145. out_free:
  1146. vfree(kbuf);
  1147. out:
  1148. mnt_drop_write_file(filp);
  1149. return ret;
  1150. }
  1151. /**
  1152. * nilfs_ioctl_get_fslabel - get the volume name of the file system
  1153. * @sb: super block instance
  1154. * @argp: pointer to userspace memory where the volume name should be stored
  1155. *
  1156. * Return: 0 on success, %-EFAULT if copying to userspace memory fails.
  1157. */
  1158. static int nilfs_ioctl_get_fslabel(struct super_block *sb, void __user *argp)
  1159. {
  1160. struct the_nilfs *nilfs = sb->s_fs_info;
  1161. char label[NILFS_MAX_VOLUME_NAME + 1];
  1162. BUILD_BUG_ON(NILFS_MAX_VOLUME_NAME >= FSLABEL_MAX);
  1163. down_read(&nilfs->ns_sem);
  1164. memtostr_pad(label, nilfs->ns_sbp[0]->s_volume_name);
  1165. up_read(&nilfs->ns_sem);
  1166. if (copy_to_user(argp, label, sizeof(label)))
  1167. return -EFAULT;
  1168. return 0;
  1169. }
  1170. /**
  1171. * nilfs_ioctl_set_fslabel - set the volume name of the file system
  1172. * @sb: super block instance
  1173. * @filp: file object
  1174. * @argp: pointer to userspace memory that contains the volume name
  1175. *
  1176. * Return: 0 on success, or the following negative error code on failure.
  1177. * * %-EFAULT - Error copying input data.
  1178. * * %-EINVAL - Label length exceeds record size in superblock.
  1179. * * %-EIO - I/O error.
  1180. * * %-EPERM - Operation not permitted (insufficient permissions).
  1181. * * %-EROFS - Read only file system.
  1182. */
  1183. static int nilfs_ioctl_set_fslabel(struct super_block *sb, struct file *filp,
  1184. void __user *argp)
  1185. {
  1186. char label[NILFS_MAX_VOLUME_NAME + 1];
  1187. struct the_nilfs *nilfs = sb->s_fs_info;
  1188. struct nilfs_super_block **sbp;
  1189. size_t len;
  1190. int ret;
  1191. if (!capable(CAP_SYS_ADMIN))
  1192. return -EPERM;
  1193. ret = mnt_want_write_file(filp);
  1194. if (ret)
  1195. return ret;
  1196. if (copy_from_user(label, argp, NILFS_MAX_VOLUME_NAME + 1)) {
  1197. ret = -EFAULT;
  1198. goto out_drop_write;
  1199. }
  1200. len = strnlen(label, NILFS_MAX_VOLUME_NAME + 1);
  1201. if (len > NILFS_MAX_VOLUME_NAME) {
  1202. nilfs_err(sb, "unable to set label with more than %zu bytes",
  1203. NILFS_MAX_VOLUME_NAME);
  1204. ret = -EINVAL;
  1205. goto out_drop_write;
  1206. }
  1207. down_write(&nilfs->ns_sem);
  1208. sbp = nilfs_prepare_super(sb, false);
  1209. if (unlikely(!sbp)) {
  1210. ret = -EIO;
  1211. goto out_unlock;
  1212. }
  1213. strtomem_pad(sbp[0]->s_volume_name, label, 0);
  1214. if (sbp[1])
  1215. strtomem_pad(sbp[1]->s_volume_name, label, 0);
  1216. ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
  1217. out_unlock:
  1218. up_write(&nilfs->ns_sem);
  1219. out_drop_write:
  1220. mnt_drop_write_file(filp);
  1221. return ret;
  1222. }
  1223. long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1224. {
  1225. struct inode *inode = file_inode(filp);
  1226. void __user *argp = (void __user *)arg;
  1227. switch (cmd) {
  1228. case FS_IOC_GETVERSION:
  1229. return nilfs_ioctl_getversion(inode, argp);
  1230. case NILFS_IOCTL_CHANGE_CPMODE:
  1231. return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
  1232. case NILFS_IOCTL_DELETE_CHECKPOINT:
  1233. return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
  1234. case NILFS_IOCTL_GET_CPINFO:
  1235. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1236. sizeof(struct nilfs_cpinfo),
  1237. nilfs_ioctl_do_get_cpinfo);
  1238. case NILFS_IOCTL_GET_CPSTAT:
  1239. return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
  1240. case NILFS_IOCTL_GET_SUINFO:
  1241. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1242. sizeof(struct nilfs_suinfo),
  1243. nilfs_ioctl_do_get_suinfo);
  1244. case NILFS_IOCTL_SET_SUINFO:
  1245. return nilfs_ioctl_set_suinfo(inode, filp, cmd, argp);
  1246. case NILFS_IOCTL_GET_SUSTAT:
  1247. return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
  1248. case NILFS_IOCTL_GET_VINFO:
  1249. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  1250. sizeof(struct nilfs_vinfo),
  1251. nilfs_ioctl_do_get_vinfo);
  1252. case NILFS_IOCTL_GET_BDESCS:
  1253. return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
  1254. case NILFS_IOCTL_CLEAN_SEGMENTS:
  1255. return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
  1256. case NILFS_IOCTL_SYNC:
  1257. return nilfs_ioctl_sync(inode, filp, cmd, argp);
  1258. case NILFS_IOCTL_RESIZE:
  1259. return nilfs_ioctl_resize(inode, filp, argp);
  1260. case NILFS_IOCTL_SET_ALLOC_RANGE:
  1261. return nilfs_ioctl_set_alloc_range(inode, argp);
  1262. case FITRIM:
  1263. return nilfs_ioctl_trim_fs(inode, argp);
  1264. case FS_IOC_GETFSLABEL:
  1265. return nilfs_ioctl_get_fslabel(inode->i_sb, argp);
  1266. case FS_IOC_SETFSLABEL:
  1267. return nilfs_ioctl_set_fslabel(inode->i_sb, filp, argp);
  1268. default:
  1269. return -ENOTTY;
  1270. }
  1271. }
  1272. #ifdef CONFIG_COMPAT
  1273. long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1274. {
  1275. switch (cmd) {
  1276. case FS_IOC32_GETVERSION:
  1277. cmd = FS_IOC_GETVERSION;
  1278. break;
  1279. case NILFS_IOCTL_CHANGE_CPMODE:
  1280. case NILFS_IOCTL_DELETE_CHECKPOINT:
  1281. case NILFS_IOCTL_GET_CPINFO:
  1282. case NILFS_IOCTL_GET_CPSTAT:
  1283. case NILFS_IOCTL_GET_SUINFO:
  1284. case NILFS_IOCTL_SET_SUINFO:
  1285. case NILFS_IOCTL_GET_SUSTAT:
  1286. case NILFS_IOCTL_GET_VINFO:
  1287. case NILFS_IOCTL_GET_BDESCS:
  1288. case NILFS_IOCTL_CLEAN_SEGMENTS:
  1289. case NILFS_IOCTL_SYNC:
  1290. case NILFS_IOCTL_RESIZE:
  1291. case NILFS_IOCTL_SET_ALLOC_RANGE:
  1292. case FITRIM:
  1293. case FS_IOC_GETFSLABEL:
  1294. case FS_IOC_SETFSLABEL:
  1295. break;
  1296. default:
  1297. return -ENOIOCTLCMD;
  1298. }
  1299. return nilfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  1300. }
  1301. #endif