ioctl.c 36 KB

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