ioctl.c 36 KB

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