ioctl.c 36 KB

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