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. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Koji Sato <koji@osrg.net>.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/wait.h>
  24. #include <linux/slab.h>
  25. #include <linux/capability.h> /* capable() */
  26. #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
  27. #include <linux/vmalloc.h>
  28. #include <linux/compat.h> /* compat_ptr() */
  29. #include <linux/mount.h> /* mnt_want_write_file(), mnt_drop_write_file() */
  30. #include <linux/buffer_head.h>
  31. #include <linux/nilfs2_fs.h>
  32. #include "nilfs.h"
  33. #include "segment.h"
  34. #include "bmap.h"
  35. #include "cpfile.h"
  36. #include "sufile.h"
  37. #include "dat.h"
  38. /**
  39. * nilfs_ioctl_wrap_copy - wrapping function of get/set metadata info
  40. * @nilfs: nilfs object
  41. * @argv: vector of arguments from userspace
  42. * @dir: set of direction flags
  43. * @dofunc: concrete function of get/set metadata info
  44. *
  45. * Description: nilfs_ioctl_wrap_copy() gets/sets metadata info by means of
  46. * calling dofunc() function on the basis of @argv argument.
  47. *
  48. * Return Value: On success, 0 is returned and requested metadata info
  49. * is copied into userspace. On error, one of the following
  50. * negative error codes is returned.
  51. *
  52. * %-EINVAL - Invalid arguments from userspace.
  53. *
  54. * %-ENOMEM - Insufficient amount of memory available.
  55. *
  56. * %-EFAULT - Failure during execution of requested operation.
  57. */
  58. static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
  59. struct nilfs_argv *argv, int dir,
  60. ssize_t (*dofunc)(struct the_nilfs *,
  61. __u64 *, int,
  62. void *, size_t, size_t))
  63. {
  64. void *buf;
  65. void __user *base = (void __user *)(unsigned long)argv->v_base;
  66. size_t maxmembs, total, n;
  67. ssize_t nr;
  68. int ret, i;
  69. __u64 pos, ppos;
  70. if (argv->v_nmembs == 0)
  71. return 0;
  72. if (argv->v_size > PAGE_SIZE)
  73. return -EINVAL;
  74. /*
  75. * Reject pairs of a start item position (argv->v_index) and a
  76. * total count (argv->v_nmembs) which leads position 'pos' to
  77. * overflow by the increment at the end of the loop.
  78. */
  79. if (argv->v_index > ~(__u64)0 - argv->v_nmembs)
  80. return -EINVAL;
  81. buf = (void *)__get_free_pages(GFP_NOFS, 0);
  82. if (unlikely(!buf))
  83. return -ENOMEM;
  84. maxmembs = PAGE_SIZE / argv->v_size;
  85. ret = 0;
  86. total = 0;
  87. pos = argv->v_index;
  88. for (i = 0; i < argv->v_nmembs; i += n) {
  89. n = (argv->v_nmembs - i < maxmembs) ?
  90. argv->v_nmembs - i : maxmembs;
  91. if ((dir & _IOC_WRITE) &&
  92. copy_from_user(buf, base + argv->v_size * i,
  93. argv->v_size * n)) {
  94. ret = -EFAULT;
  95. break;
  96. }
  97. ppos = pos;
  98. nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
  99. n);
  100. if (nr < 0) {
  101. ret = nr;
  102. break;
  103. }
  104. if ((dir & _IOC_READ) &&
  105. copy_to_user(base + argv->v_size * i, buf,
  106. argv->v_size * nr)) {
  107. ret = -EFAULT;
  108. break;
  109. }
  110. total += nr;
  111. if ((size_t)nr < n)
  112. break;
  113. if (pos == ppos)
  114. pos += n;
  115. }
  116. argv->v_nmembs = total;
  117. free_pages((unsigned long)buf, 0);
  118. return ret;
  119. }
  120. /**
  121. * nilfs_ioctl_getflags - ioctl to support lsattr
  122. */
  123. static int nilfs_ioctl_getflags(struct inode *inode, void __user *argp)
  124. {
  125. unsigned int flags = NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE;
  126. return put_user(flags, (int __user *)argp);
  127. }
  128. /**
  129. * nilfs_ioctl_setflags - ioctl to support chattr
  130. */
  131. static int nilfs_ioctl_setflags(struct inode *inode, struct file *filp,
  132. void __user *argp)
  133. {
  134. struct nilfs_transaction_info ti;
  135. unsigned int flags, oldflags;
  136. int ret;
  137. if (!inode_owner_or_capable(inode))
  138. return -EACCES;
  139. if (get_user(flags, (int __user *)argp))
  140. return -EFAULT;
  141. ret = mnt_want_write_file(filp);
  142. if (ret)
  143. return ret;
  144. flags = nilfs_mask_flags(inode->i_mode, flags);
  145. mutex_lock(&inode->i_mutex);
  146. oldflags = NILFS_I(inode)->i_flags;
  147. /*
  148. * The IMMUTABLE and APPEND_ONLY flags can only be changed by the
  149. * relevant capability.
  150. */
  151. ret = -EPERM;
  152. if (((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
  153. !capable(CAP_LINUX_IMMUTABLE))
  154. goto out;
  155. ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
  156. if (ret)
  157. goto out;
  158. NILFS_I(inode)->i_flags = (oldflags & ~FS_FL_USER_MODIFIABLE) |
  159. (flags & FS_FL_USER_MODIFIABLE);
  160. nilfs_set_inode_flags(inode);
  161. inode->i_ctime = CURRENT_TIME;
  162. if (IS_SYNC(inode))
  163. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  164. nilfs_mark_inode_dirty(inode);
  165. ret = nilfs_transaction_commit(inode->i_sb);
  166. out:
  167. mutex_unlock(&inode->i_mutex);
  168. mnt_drop_write_file(filp);
  169. return ret;
  170. }
  171. /**
  172. * nilfs_ioctl_getversion - get info about a file's version (generation number)
  173. */
  174. static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
  175. {
  176. return put_user(inode->i_generation, (int __user *)argp);
  177. }
  178. /**
  179. * nilfs_ioctl_change_cpmode - change checkpoint mode (checkpoint/snapshot)
  180. * @inode: inode object
  181. * @filp: file object
  182. * @cmd: ioctl's request code
  183. * @argp: pointer on argument from userspace
  184. *
  185. * Description: nilfs_ioctl_change_cpmode() function changes mode of
  186. * given checkpoint between checkpoint and snapshot state. This ioctl
  187. * is used in chcp and mkcp utilities.
  188. *
  189. * Return Value: On success, 0 is returned and mode of a checkpoint is
  190. * changed. On error, one of the following negative error codes
  191. * is returned.
  192. *
  193. * %-EPERM - Operation not permitted.
  194. *
  195. * %-EFAULT - Failure during checkpoint mode changing.
  196. */
  197. static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
  198. unsigned int cmd, void __user *argp)
  199. {
  200. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  201. struct nilfs_transaction_info ti;
  202. struct nilfs_cpmode cpmode;
  203. int ret;
  204. if (!capable(CAP_SYS_ADMIN))
  205. return -EPERM;
  206. ret = mnt_want_write_file(filp);
  207. if (ret)
  208. return ret;
  209. ret = -EFAULT;
  210. if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
  211. goto out;
  212. mutex_lock(&nilfs->ns_snapshot_mount_mutex);
  213. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  214. ret = nilfs_cpfile_change_cpmode(
  215. nilfs->ns_cpfile, cpmode.cm_cno, cpmode.cm_mode);
  216. if (unlikely(ret < 0))
  217. nilfs_transaction_abort(inode->i_sb);
  218. else
  219. nilfs_transaction_commit(inode->i_sb); /* never fails */
  220. mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
  221. out:
  222. mnt_drop_write_file(filp);
  223. return ret;
  224. }
  225. /**
  226. * nilfs_ioctl_delete_checkpoint - remove checkpoint
  227. * @inode: inode object
  228. * @filp: file object
  229. * @cmd: ioctl's request code
  230. * @argp: pointer on argument from userspace
  231. *
  232. * Description: nilfs_ioctl_delete_checkpoint() function removes
  233. * checkpoint from NILFS2 file system. This ioctl is used in rmcp
  234. * utility.
  235. *
  236. * Return Value: On success, 0 is returned and a checkpoint is
  237. * removed. On error, one of the following negative error codes
  238. * is returned.
  239. *
  240. * %-EPERM - Operation not permitted.
  241. *
  242. * %-EFAULT - Failure during checkpoint removing.
  243. */
  244. static int
  245. nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
  246. unsigned int cmd, void __user *argp)
  247. {
  248. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  249. struct nilfs_transaction_info ti;
  250. __u64 cno;
  251. int ret;
  252. if (!capable(CAP_SYS_ADMIN))
  253. return -EPERM;
  254. ret = mnt_want_write_file(filp);
  255. if (ret)
  256. return ret;
  257. ret = -EFAULT;
  258. if (copy_from_user(&cno, argp, sizeof(cno)))
  259. goto out;
  260. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  261. ret = nilfs_cpfile_delete_checkpoint(nilfs->ns_cpfile, cno);
  262. if (unlikely(ret < 0))
  263. nilfs_transaction_abort(inode->i_sb);
  264. else
  265. nilfs_transaction_commit(inode->i_sb); /* never fails */
  266. out:
  267. mnt_drop_write_file(filp);
  268. return ret;
  269. }
  270. /**
  271. * nilfs_ioctl_do_get_cpinfo - callback method getting info about checkpoints
  272. * @nilfs: nilfs object
  273. * @posp: pointer on array of checkpoint's numbers
  274. * @flags: checkpoint mode (checkpoint or snapshot)
  275. * @buf: buffer for storing checkponts' info
  276. * @size: size in bytes of one checkpoint info item in array
  277. * @nmembs: number of checkpoints in array (numbers and infos)
  278. *
  279. * Description: nilfs_ioctl_do_get_cpinfo() function returns info about
  280. * requested checkpoints. The NILFS_IOCTL_GET_CPINFO ioctl is used in
  281. * lscp utility and by nilfs_cleanerd daemon.
  282. *
  283. * Return value: count of nilfs_cpinfo structures in output buffer.
  284. */
  285. static ssize_t
  286. nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  287. void *buf, size_t size, size_t nmembs)
  288. {
  289. int ret;
  290. down_read(&nilfs->ns_segctor_sem);
  291. ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
  292. size, nmembs);
  293. up_read(&nilfs->ns_segctor_sem);
  294. return ret;
  295. }
  296. /**
  297. * nilfs_ioctl_get_cpstat - get checkpoints statistics
  298. * @inode: inode object
  299. * @filp: file object
  300. * @cmd: ioctl's request code
  301. * @argp: pointer on argument from userspace
  302. *
  303. * Description: nilfs_ioctl_get_cpstat() returns information about checkpoints.
  304. * The NILFS_IOCTL_GET_CPSTAT ioctl is used by lscp, rmcp utilities
  305. * and by nilfs_cleanerd daemon.
  306. *
  307. * Return Value: On success, 0 is returned, and checkpoints information is
  308. * copied into userspace pointer @argp. On error, one of the following
  309. * negative error codes is returned.
  310. *
  311. * %-EIO - I/O error.
  312. *
  313. * %-ENOMEM - Insufficient amount of memory available.
  314. *
  315. * %-EFAULT - Failure during getting checkpoints statistics.
  316. */
  317. static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
  318. unsigned int cmd, void __user *argp)
  319. {
  320. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  321. struct nilfs_cpstat cpstat;
  322. int ret;
  323. down_read(&nilfs->ns_segctor_sem);
  324. ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  325. up_read(&nilfs->ns_segctor_sem);
  326. if (ret < 0)
  327. return ret;
  328. if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
  329. ret = -EFAULT;
  330. return ret;
  331. }
  332. /**
  333. * nilfs_ioctl_do_get_suinfo - callback method getting segment usage info
  334. * @nilfs: nilfs object
  335. * @posp: pointer on array of segment numbers
  336. * @flags: *not used*
  337. * @buf: buffer for storing suinfo array
  338. * @size: size in bytes of one suinfo item in array
  339. * @nmembs: count of segment numbers and suinfos in array
  340. *
  341. * Description: nilfs_ioctl_do_get_suinfo() function returns segment usage
  342. * info about requested segments. The NILFS_IOCTL_GET_SUINFO ioctl is used
  343. * in lssu, nilfs_resize utilities and by nilfs_cleanerd daemon.
  344. *
  345. * Return value: count of nilfs_suinfo structures in output buffer.
  346. */
  347. static ssize_t
  348. nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  349. void *buf, size_t size, size_t nmembs)
  350. {
  351. int ret;
  352. down_read(&nilfs->ns_segctor_sem);
  353. ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
  354. nmembs);
  355. up_read(&nilfs->ns_segctor_sem);
  356. return ret;
  357. }
  358. /**
  359. * nilfs_ioctl_get_sustat - get segment usage statistics
  360. * @inode: inode object
  361. * @filp: file object
  362. * @cmd: ioctl's request code
  363. * @argp: pointer on argument from userspace
  364. *
  365. * Description: nilfs_ioctl_get_sustat() returns segment usage statistics.
  366. * The NILFS_IOCTL_GET_SUSTAT ioctl is used in lssu, nilfs_resize utilities
  367. * and by nilfs_cleanerd daemon.
  368. *
  369. * Return Value: On success, 0 is returned, and segment usage information is
  370. * copied into userspace pointer @argp. On error, one of the following
  371. * negative error codes is returned.
  372. *
  373. * %-EIO - I/O error.
  374. *
  375. * %-ENOMEM - Insufficient amount of memory available.
  376. *
  377. * %-EFAULT - Failure during getting segment usage statistics.
  378. */
  379. static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
  380. unsigned int cmd, void __user *argp)
  381. {
  382. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  383. struct nilfs_sustat sustat;
  384. int ret;
  385. down_read(&nilfs->ns_segctor_sem);
  386. ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
  387. up_read(&nilfs->ns_segctor_sem);
  388. if (ret < 0)
  389. return ret;
  390. if (copy_to_user(argp, &sustat, sizeof(sustat)))
  391. ret = -EFAULT;
  392. return ret;
  393. }
  394. /**
  395. * nilfs_ioctl_do_get_vinfo - callback method getting virtual blocks info
  396. * @nilfs: nilfs object
  397. * @posp: *not used*
  398. * @flags: *not used*
  399. * @buf: buffer for storing array of nilfs_vinfo structures
  400. * @size: size in bytes of one vinfo item in array
  401. * @nmembs: count of vinfos in array
  402. *
  403. * Description: nilfs_ioctl_do_get_vinfo() function returns information
  404. * on virtual block addresses. The NILFS_IOCTL_GET_VINFO ioctl is used
  405. * by nilfs_cleanerd daemon.
  406. *
  407. * Return value: count of nilfs_vinfo structures in output buffer.
  408. */
  409. static ssize_t
  410. nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  411. void *buf, size_t size, size_t nmembs)
  412. {
  413. int ret;
  414. down_read(&nilfs->ns_segctor_sem);
  415. ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
  416. up_read(&nilfs->ns_segctor_sem);
  417. return ret;
  418. }
  419. /**
  420. * nilfs_ioctl_do_get_bdescs - callback method getting disk block descriptors
  421. * @nilfs: nilfs object
  422. * @posp: *not used*
  423. * @flags: *not used*
  424. * @buf: buffer for storing array of nilfs_bdesc structures
  425. * @size: size in bytes of one bdesc item in array
  426. * @nmembs: count of bdescs in array
  427. *
  428. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  429. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  430. * is used by nilfs_cleanerd daemon.
  431. *
  432. * Return value: count of nilfs_bdescs structures in output buffer.
  433. */
  434. static ssize_t
  435. nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
  436. void *buf, size_t size, size_t nmembs)
  437. {
  438. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  439. struct nilfs_bdesc *bdescs = buf;
  440. int ret, i;
  441. down_read(&nilfs->ns_segctor_sem);
  442. for (i = 0; i < nmembs; i++) {
  443. ret = nilfs_bmap_lookup_at_level(bmap,
  444. bdescs[i].bd_offset,
  445. bdescs[i].bd_level + 1,
  446. &bdescs[i].bd_blocknr);
  447. if (ret < 0) {
  448. if (ret != -ENOENT) {
  449. up_read(&nilfs->ns_segctor_sem);
  450. return ret;
  451. }
  452. bdescs[i].bd_blocknr = 0;
  453. }
  454. }
  455. up_read(&nilfs->ns_segctor_sem);
  456. return nmembs;
  457. }
  458. /**
  459. * nilfs_ioctl_get_bdescs - get disk block descriptors
  460. * @inode: inode object
  461. * @filp: file object
  462. * @cmd: ioctl's request code
  463. * @argp: pointer on argument from userspace
  464. *
  465. * Description: nilfs_ioctl_do_get_bdescs() function returns information
  466. * about descriptors of disk block numbers. The NILFS_IOCTL_GET_BDESCS ioctl
  467. * is used by nilfs_cleanerd daemon.
  468. *
  469. * Return Value: On success, 0 is returned, and disk block descriptors are
  470. * copied into userspace pointer @argp. On error, one of the following
  471. * negative error codes is returned.
  472. *
  473. * %-EINVAL - Invalid arguments from userspace.
  474. *
  475. * %-EIO - I/O error.
  476. *
  477. * %-ENOMEM - Insufficient amount of memory available.
  478. *
  479. * %-EFAULT - Failure during getting disk block descriptors.
  480. */
  481. static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
  482. unsigned int cmd, void __user *argp)
  483. {
  484. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  485. struct nilfs_argv argv;
  486. int ret;
  487. if (copy_from_user(&argv, argp, sizeof(argv)))
  488. return -EFAULT;
  489. if (argv.v_size != sizeof(struct nilfs_bdesc))
  490. return -EINVAL;
  491. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
  492. nilfs_ioctl_do_get_bdescs);
  493. if (ret < 0)
  494. return ret;
  495. if (copy_to_user(argp, &argv, sizeof(argv)))
  496. ret = -EFAULT;
  497. return ret;
  498. }
  499. /**
  500. * nilfs_ioctl_move_inode_block - prepare data/node block for moving by GC
  501. * @inode: inode object
  502. * @vdesc: descriptor of virtual block number
  503. * @buffers: list of moving buffers
  504. *
  505. * Description: nilfs_ioctl_move_inode_block() function registers data/node
  506. * buffer in the GC pagecache and submit read request.
  507. *
  508. * Return Value: On success, 0 is returned. On error, one of the following
  509. * negative error codes is returned.
  510. *
  511. * %-EIO - I/O error.
  512. *
  513. * %-ENOMEM - Insufficient amount of memory available.
  514. *
  515. * %-ENOENT - Requested block doesn't exist.
  516. *
  517. * %-EEXIST - Blocks conflict is detected.
  518. */
  519. static int nilfs_ioctl_move_inode_block(struct inode *inode,
  520. struct nilfs_vdesc *vdesc,
  521. struct list_head *buffers)
  522. {
  523. struct buffer_head *bh;
  524. int ret;
  525. if (vdesc->vd_flags == 0)
  526. ret = nilfs_gccache_submit_read_data(
  527. inode, vdesc->vd_offset, vdesc->vd_blocknr,
  528. vdesc->vd_vblocknr, &bh);
  529. else
  530. ret = nilfs_gccache_submit_read_node(
  531. inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
  532. if (unlikely(ret < 0)) {
  533. if (ret == -ENOENT)
  534. printk(KERN_CRIT
  535. "%s: invalid virtual block address (%s): "
  536. "ino=%llu, cno=%llu, offset=%llu, "
  537. "blocknr=%llu, vblocknr=%llu\n",
  538. __func__, vdesc->vd_flags ? "node" : "data",
  539. (unsigned long long)vdesc->vd_ino,
  540. (unsigned long long)vdesc->vd_cno,
  541. (unsigned long long)vdesc->vd_offset,
  542. (unsigned long long)vdesc->vd_blocknr,
  543. (unsigned long long)vdesc->vd_vblocknr);
  544. return ret;
  545. }
  546. if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
  547. printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
  548. "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
  549. __func__, vdesc->vd_flags ? "node" : "data",
  550. (unsigned long long)vdesc->vd_ino,
  551. (unsigned long long)vdesc->vd_cno,
  552. (unsigned long long)vdesc->vd_offset,
  553. (unsigned long long)vdesc->vd_blocknr,
  554. (unsigned long long)vdesc->vd_vblocknr);
  555. brelse(bh);
  556. return -EEXIST;
  557. }
  558. list_add_tail(&bh->b_assoc_buffers, buffers);
  559. return 0;
  560. }
  561. /**
  562. * nilfs_ioctl_move_blocks - move valid inode's blocks during garbage collection
  563. * @sb: superblock object
  564. * @argv: vector of arguments from userspace
  565. * @buf: array of nilfs_vdesc structures
  566. *
  567. * Description: nilfs_ioctl_move_blocks() function reads valid data/node
  568. * blocks that garbage collector specified with the array of nilfs_vdesc
  569. * structures and stores them into page caches of GC inodes.
  570. *
  571. * Return Value: Number of processed nilfs_vdesc structures or
  572. * error code, otherwise.
  573. */
  574. static int nilfs_ioctl_move_blocks(struct super_block *sb,
  575. struct nilfs_argv *argv, void *buf)
  576. {
  577. size_t nmembs = argv->v_nmembs;
  578. struct the_nilfs *nilfs = sb->s_fs_info;
  579. struct inode *inode;
  580. struct nilfs_vdesc *vdesc;
  581. struct buffer_head *bh, *n;
  582. LIST_HEAD(buffers);
  583. ino_t ino;
  584. __u64 cno;
  585. int i, ret;
  586. for (i = 0, vdesc = buf; i < nmembs; ) {
  587. ino = vdesc->vd_ino;
  588. cno = vdesc->vd_cno;
  589. inode = nilfs_iget_for_gc(sb, ino, cno);
  590. if (IS_ERR(inode)) {
  591. ret = PTR_ERR(inode);
  592. goto failed;
  593. }
  594. if (list_empty(&NILFS_I(inode)->i_dirty)) {
  595. /*
  596. * Add the inode to GC inode list. Garbage Collection
  597. * is serialized and no two processes manipulate the
  598. * list simultaneously.
  599. */
  600. igrab(inode);
  601. list_add(&NILFS_I(inode)->i_dirty,
  602. &nilfs->ns_gc_inodes);
  603. }
  604. do {
  605. ret = nilfs_ioctl_move_inode_block(inode, vdesc,
  606. &buffers);
  607. if (unlikely(ret < 0)) {
  608. iput(inode);
  609. goto failed;
  610. }
  611. vdesc++;
  612. } while (++i < nmembs &&
  613. vdesc->vd_ino == ino && vdesc->vd_cno == cno);
  614. iput(inode); /* The inode still remains in GC inode list */
  615. }
  616. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  617. ret = nilfs_gccache_wait_and_mark_dirty(bh);
  618. if (unlikely(ret < 0)) {
  619. WARN_ON(ret == -EEXIST);
  620. goto failed;
  621. }
  622. list_del_init(&bh->b_assoc_buffers);
  623. brelse(bh);
  624. }
  625. return nmembs;
  626. failed:
  627. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  628. list_del_init(&bh->b_assoc_buffers);
  629. brelse(bh);
  630. }
  631. return ret;
  632. }
  633. /**
  634. * nilfs_ioctl_delete_checkpoints - delete checkpoints
  635. * @nilfs: nilfs object
  636. * @argv: vector of arguments from userspace
  637. * @buf: array of periods of checkpoints numbers
  638. *
  639. * Description: nilfs_ioctl_delete_checkpoints() function deletes checkpoints
  640. * in the period from p_start to p_end, excluding p_end itself. The checkpoints
  641. * which have been already deleted are ignored.
  642. *
  643. * Return Value: Number of processed nilfs_period structures or
  644. * error code, otherwise.
  645. *
  646. * %-EIO - I/O error.
  647. *
  648. * %-ENOMEM - Insufficient amount of memory available.
  649. *
  650. * %-EINVAL - invalid checkpoints.
  651. */
  652. static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
  653. struct nilfs_argv *argv, void *buf)
  654. {
  655. size_t nmembs = argv->v_nmembs;
  656. struct inode *cpfile = nilfs->ns_cpfile;
  657. struct nilfs_period *periods = buf;
  658. int ret, i;
  659. for (i = 0; i < nmembs; i++) {
  660. ret = nilfs_cpfile_delete_checkpoints(
  661. cpfile, periods[i].p_start, periods[i].p_end);
  662. if (ret < 0)
  663. return ret;
  664. }
  665. return nmembs;
  666. }
  667. /**
  668. * nilfs_ioctl_free_vblocknrs - free virtual block numbers
  669. * @nilfs: nilfs object
  670. * @argv: vector of arguments from userspace
  671. * @buf: array of virtual block numbers
  672. *
  673. * Description: nilfs_ioctl_free_vblocknrs() function frees
  674. * the virtual block numbers specified by @buf and @argv->v_nmembs.
  675. *
  676. * Return Value: Number of processed virtual block numbers or
  677. * error code, otherwise.
  678. *
  679. * %-EIO - I/O error.
  680. *
  681. * %-ENOMEM - Insufficient amount of memory available.
  682. *
  683. * %-ENOENT - The virtual block number have not been allocated.
  684. */
  685. static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
  686. struct nilfs_argv *argv, void *buf)
  687. {
  688. size_t nmembs = argv->v_nmembs;
  689. int ret;
  690. ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
  691. return (ret < 0) ? ret : nmembs;
  692. }
  693. /**
  694. * nilfs_ioctl_mark_blocks_dirty - mark blocks dirty
  695. * @nilfs: nilfs object
  696. * @argv: vector of arguments from userspace
  697. * @buf: array of block descriptors
  698. *
  699. * Description: nilfs_ioctl_mark_blocks_dirty() function marks
  700. * metadata file or data blocks as dirty.
  701. *
  702. * Return Value: Number of processed block descriptors or
  703. * error code, otherwise.
  704. *
  705. * %-ENOMEM - Insufficient memory available.
  706. *
  707. * %-EIO - I/O error
  708. *
  709. * %-ENOENT - the specified block does not exist (hole block)
  710. */
  711. static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
  712. struct nilfs_argv *argv, void *buf)
  713. {
  714. size_t nmembs = argv->v_nmembs;
  715. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  716. struct nilfs_bdesc *bdescs = buf;
  717. int ret, i;
  718. for (i = 0; i < nmembs; i++) {
  719. /* XXX: use macro or inline func to check liveness */
  720. ret = nilfs_bmap_lookup_at_level(bmap,
  721. bdescs[i].bd_offset,
  722. bdescs[i].bd_level + 1,
  723. &bdescs[i].bd_blocknr);
  724. if (ret < 0) {
  725. if (ret != -ENOENT)
  726. return ret;
  727. bdescs[i].bd_blocknr = 0;
  728. }
  729. if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
  730. /* skip dead block */
  731. continue;
  732. if (bdescs[i].bd_level == 0) {
  733. ret = nilfs_mdt_mark_block_dirty(nilfs->ns_dat,
  734. bdescs[i].bd_offset);
  735. if (ret < 0) {
  736. WARN_ON(ret == -ENOENT);
  737. return ret;
  738. }
  739. } else {
  740. ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
  741. bdescs[i].bd_level);
  742. if (ret < 0) {
  743. WARN_ON(ret == -ENOENT);
  744. return ret;
  745. }
  746. }
  747. }
  748. return nmembs;
  749. }
  750. int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
  751. struct nilfs_argv *argv, void **kbufs)
  752. {
  753. const char *msg;
  754. int ret;
  755. ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
  756. if (ret < 0) {
  757. /*
  758. * can safely abort because checkpoints can be removed
  759. * independently.
  760. */
  761. msg = "cannot delete checkpoints";
  762. goto failed;
  763. }
  764. ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
  765. if (ret < 0) {
  766. /*
  767. * can safely abort because DAT file is updated atomically
  768. * using a copy-on-write technique.
  769. */
  770. msg = "cannot delete virtual blocks from DAT file";
  771. goto failed;
  772. }
  773. ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
  774. if (ret < 0) {
  775. /*
  776. * can safely abort because the operation is nondestructive.
  777. */
  778. msg = "cannot mark copying blocks dirty";
  779. goto failed;
  780. }
  781. return 0;
  782. failed:
  783. printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
  784. msg, ret);
  785. return ret;
  786. }
  787. /**
  788. * nilfs_ioctl_clean_segments - clean segments
  789. * @inode: inode object
  790. * @filp: file object
  791. * @cmd: ioctl's request code
  792. * @argp: pointer on argument from userspace
  793. *
  794. * Description: nilfs_ioctl_clean_segments() function makes garbage
  795. * collection operation in the environment of requested parameters
  796. * from userspace. The NILFS_IOCTL_CLEAN_SEGMENTS ioctl is used by
  797. * nilfs_cleanerd daemon.
  798. *
  799. * Return Value: On success, 0 is returned or error code, otherwise.
  800. */
  801. static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
  802. unsigned int cmd, void __user *argp)
  803. {
  804. struct nilfs_argv argv[5];
  805. static const size_t argsz[5] = {
  806. sizeof(struct nilfs_vdesc),
  807. sizeof(struct nilfs_period),
  808. sizeof(__u64),
  809. sizeof(struct nilfs_bdesc),
  810. sizeof(__u64),
  811. };
  812. void __user *base;
  813. void *kbufs[5];
  814. struct the_nilfs *nilfs;
  815. size_t len, nsegs;
  816. int n, ret;
  817. if (!capable(CAP_SYS_ADMIN))
  818. return -EPERM;
  819. ret = mnt_want_write_file(filp);
  820. if (ret)
  821. return ret;
  822. ret = -EFAULT;
  823. if (copy_from_user(argv, argp, sizeof(argv)))
  824. goto out;
  825. ret = -EINVAL;
  826. nsegs = argv[4].v_nmembs;
  827. if (argv[4].v_size != argsz[4])
  828. goto out;
  829. if (nsegs > UINT_MAX / sizeof(__u64))
  830. goto out;
  831. /*
  832. * argv[4] points to segment numbers this ioctl cleans. We
  833. * use kmalloc() for its buffer because memory used for the
  834. * segment numbers is enough small.
  835. */
  836. kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
  837. nsegs * sizeof(__u64));
  838. if (IS_ERR(kbufs[4])) {
  839. ret = PTR_ERR(kbufs[4]);
  840. goto out;
  841. }
  842. nilfs = inode->i_sb->s_fs_info;
  843. for (n = 0; n < 4; n++) {
  844. ret = -EINVAL;
  845. if (argv[n].v_size != argsz[n])
  846. goto out_free;
  847. if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
  848. goto out_free;
  849. if (argv[n].v_nmembs >= UINT_MAX / argv[n].v_size)
  850. goto out_free;
  851. len = argv[n].v_size * argv[n].v_nmembs;
  852. base = (void __user *)(unsigned long)argv[n].v_base;
  853. if (len == 0) {
  854. kbufs[n] = NULL;
  855. continue;
  856. }
  857. kbufs[n] = vmalloc(len);
  858. if (!kbufs[n]) {
  859. ret = -ENOMEM;
  860. goto out_free;
  861. }
  862. if (copy_from_user(kbufs[n], base, len)) {
  863. ret = -EFAULT;
  864. vfree(kbufs[n]);
  865. goto out_free;
  866. }
  867. }
  868. /*
  869. * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
  870. * which will operates an inode list without blocking.
  871. * To protect the list from concurrent operations,
  872. * nilfs_ioctl_move_blocks should be atomic operation.
  873. */
  874. if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
  875. ret = -EBUSY;
  876. goto out_free;
  877. }
  878. ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
  879. if (ret < 0)
  880. printk(KERN_ERR "NILFS: GC failed during preparation: "
  881. "cannot read source blocks: err=%d\n", 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