ioctl.c 38 KB

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