ioctl.c 36 KB

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