ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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(), mnt_drop_write() */
  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. static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
  39. struct nilfs_argv *argv, int dir,
  40. ssize_t (*dofunc)(struct the_nilfs *,
  41. __u64 *, int,
  42. void *, size_t, size_t))
  43. {
  44. void *buf;
  45. void __user *base = (void __user *)(unsigned long)argv->v_base;
  46. size_t maxmembs, total, n;
  47. ssize_t nr;
  48. int ret, i;
  49. __u64 pos, ppos;
  50. if (argv->v_nmembs == 0)
  51. return 0;
  52. if (argv->v_size > PAGE_SIZE)
  53. return -EINVAL;
  54. buf = (void *)__get_free_pages(GFP_NOFS, 0);
  55. if (unlikely(!buf))
  56. return -ENOMEM;
  57. maxmembs = PAGE_SIZE / argv->v_size;
  58. ret = 0;
  59. total = 0;
  60. pos = argv->v_index;
  61. for (i = 0; i < argv->v_nmembs; i += n) {
  62. n = (argv->v_nmembs - i < maxmembs) ?
  63. argv->v_nmembs - i : maxmembs;
  64. if ((dir & _IOC_WRITE) &&
  65. copy_from_user(buf, base + argv->v_size * i,
  66. argv->v_size * n)) {
  67. ret = -EFAULT;
  68. break;
  69. }
  70. ppos = pos;
  71. nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
  72. n);
  73. if (nr < 0) {
  74. ret = nr;
  75. break;
  76. }
  77. if ((dir & _IOC_READ) &&
  78. copy_to_user(base + argv->v_size * i, buf,
  79. argv->v_size * nr)) {
  80. ret = -EFAULT;
  81. break;
  82. }
  83. total += nr;
  84. if ((size_t)nr < n)
  85. break;
  86. if (pos == ppos)
  87. pos += n;
  88. }
  89. argv->v_nmembs = total;
  90. free_pages((unsigned long)buf, 0);
  91. return ret;
  92. }
  93. static int nilfs_ioctl_getflags(struct inode *inode, void __user *argp)
  94. {
  95. unsigned int flags = NILFS_I(inode)->i_flags & FS_FL_USER_VISIBLE;
  96. return put_user(flags, (int __user *)argp);
  97. }
  98. static int nilfs_ioctl_setflags(struct inode *inode, struct file *filp,
  99. void __user *argp)
  100. {
  101. struct nilfs_transaction_info ti;
  102. unsigned int flags, oldflags;
  103. int ret;
  104. if (!inode_owner_or_capable(inode))
  105. return -EACCES;
  106. if (get_user(flags, (int __user *)argp))
  107. return -EFAULT;
  108. ret = mnt_want_write(filp->f_path.mnt);
  109. if (ret)
  110. return ret;
  111. flags = nilfs_mask_flags(inode->i_mode, flags);
  112. mutex_lock(&inode->i_mutex);
  113. oldflags = NILFS_I(inode)->i_flags;
  114. /*
  115. * The IMMUTABLE and APPEND_ONLY flags can only be changed by the
  116. * relevant capability.
  117. */
  118. ret = -EPERM;
  119. if (((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
  120. !capable(CAP_LINUX_IMMUTABLE))
  121. goto out;
  122. ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
  123. if (ret)
  124. goto out;
  125. NILFS_I(inode)->i_flags = (oldflags & ~FS_FL_USER_MODIFIABLE) |
  126. (flags & FS_FL_USER_MODIFIABLE);
  127. nilfs_set_inode_flags(inode);
  128. inode->i_ctime = CURRENT_TIME;
  129. if (IS_SYNC(inode))
  130. nilfs_set_transaction_flag(NILFS_TI_SYNC);
  131. nilfs_mark_inode_dirty(inode);
  132. ret = nilfs_transaction_commit(inode->i_sb);
  133. out:
  134. mutex_unlock(&inode->i_mutex);
  135. mnt_drop_write(filp->f_path.mnt);
  136. return ret;
  137. }
  138. static int nilfs_ioctl_getversion(struct inode *inode, void __user *argp)
  139. {
  140. return put_user(inode->i_generation, (int __user *)argp);
  141. }
  142. static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
  143. unsigned int cmd, void __user *argp)
  144. {
  145. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  146. struct nilfs_transaction_info ti;
  147. struct nilfs_cpmode cpmode;
  148. int ret;
  149. if (!capable(CAP_SYS_ADMIN))
  150. return -EPERM;
  151. ret = mnt_want_write(filp->f_path.mnt);
  152. if (ret)
  153. return ret;
  154. ret = -EFAULT;
  155. if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
  156. goto out;
  157. down_read(&inode->i_sb->s_umount);
  158. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  159. ret = nilfs_cpfile_change_cpmode(
  160. nilfs->ns_cpfile, cpmode.cm_cno, cpmode.cm_mode);
  161. if (unlikely(ret < 0))
  162. nilfs_transaction_abort(inode->i_sb);
  163. else
  164. nilfs_transaction_commit(inode->i_sb); /* never fails */
  165. up_read(&inode->i_sb->s_umount);
  166. out:
  167. mnt_drop_write(filp->f_path.mnt);
  168. return ret;
  169. }
  170. static int
  171. nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
  172. unsigned int cmd, void __user *argp)
  173. {
  174. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  175. struct nilfs_transaction_info ti;
  176. __u64 cno;
  177. int ret;
  178. if (!capable(CAP_SYS_ADMIN))
  179. return -EPERM;
  180. ret = mnt_want_write(filp->f_path.mnt);
  181. if (ret)
  182. return ret;
  183. ret = -EFAULT;
  184. if (copy_from_user(&cno, argp, sizeof(cno)))
  185. goto out;
  186. nilfs_transaction_begin(inode->i_sb, &ti, 0);
  187. ret = nilfs_cpfile_delete_checkpoint(nilfs->ns_cpfile, cno);
  188. if (unlikely(ret < 0))
  189. nilfs_transaction_abort(inode->i_sb);
  190. else
  191. nilfs_transaction_commit(inode->i_sb); /* never fails */
  192. out:
  193. mnt_drop_write(filp->f_path.mnt);
  194. return ret;
  195. }
  196. static ssize_t
  197. nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  198. void *buf, size_t size, size_t nmembs)
  199. {
  200. int ret;
  201. down_read(&nilfs->ns_segctor_sem);
  202. ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
  203. size, nmembs);
  204. up_read(&nilfs->ns_segctor_sem);
  205. return ret;
  206. }
  207. static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
  208. unsigned int cmd, void __user *argp)
  209. {
  210. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  211. struct nilfs_cpstat cpstat;
  212. int ret;
  213. down_read(&nilfs->ns_segctor_sem);
  214. ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
  215. up_read(&nilfs->ns_segctor_sem);
  216. if (ret < 0)
  217. return ret;
  218. if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
  219. ret = -EFAULT;
  220. return ret;
  221. }
  222. static ssize_t
  223. nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  224. void *buf, size_t size, size_t nmembs)
  225. {
  226. int ret;
  227. down_read(&nilfs->ns_segctor_sem);
  228. ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
  229. nmembs);
  230. up_read(&nilfs->ns_segctor_sem);
  231. return ret;
  232. }
  233. static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
  234. unsigned int cmd, void __user *argp)
  235. {
  236. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  237. struct nilfs_sustat sustat;
  238. int ret;
  239. down_read(&nilfs->ns_segctor_sem);
  240. ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
  241. up_read(&nilfs->ns_segctor_sem);
  242. if (ret < 0)
  243. return ret;
  244. if (copy_to_user(argp, &sustat, sizeof(sustat)))
  245. ret = -EFAULT;
  246. return ret;
  247. }
  248. static ssize_t
  249. nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
  250. void *buf, size_t size, size_t nmembs)
  251. {
  252. int ret;
  253. down_read(&nilfs->ns_segctor_sem);
  254. ret = nilfs_dat_get_vinfo(nilfs->ns_dat, buf, size, nmembs);
  255. up_read(&nilfs->ns_segctor_sem);
  256. return ret;
  257. }
  258. static ssize_t
  259. nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
  260. void *buf, size_t size, size_t nmembs)
  261. {
  262. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  263. struct nilfs_bdesc *bdescs = buf;
  264. int ret, i;
  265. down_read(&nilfs->ns_segctor_sem);
  266. for (i = 0; i < nmembs; i++) {
  267. ret = nilfs_bmap_lookup_at_level(bmap,
  268. bdescs[i].bd_offset,
  269. bdescs[i].bd_level + 1,
  270. &bdescs[i].bd_blocknr);
  271. if (ret < 0) {
  272. if (ret != -ENOENT) {
  273. up_read(&nilfs->ns_segctor_sem);
  274. return ret;
  275. }
  276. bdescs[i].bd_blocknr = 0;
  277. }
  278. }
  279. up_read(&nilfs->ns_segctor_sem);
  280. return nmembs;
  281. }
  282. static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
  283. unsigned int cmd, void __user *argp)
  284. {
  285. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  286. struct nilfs_argv argv;
  287. int ret;
  288. if (copy_from_user(&argv, argp, sizeof(argv)))
  289. return -EFAULT;
  290. if (argv.v_size != sizeof(struct nilfs_bdesc))
  291. return -EINVAL;
  292. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
  293. nilfs_ioctl_do_get_bdescs);
  294. if (ret < 0)
  295. return ret;
  296. if (copy_to_user(argp, &argv, sizeof(argv)))
  297. ret = -EFAULT;
  298. return ret;
  299. }
  300. static int nilfs_ioctl_move_inode_block(struct inode *inode,
  301. struct nilfs_vdesc *vdesc,
  302. struct list_head *buffers)
  303. {
  304. struct buffer_head *bh;
  305. int ret;
  306. if (vdesc->vd_flags == 0)
  307. ret = nilfs_gccache_submit_read_data(
  308. inode, vdesc->vd_offset, vdesc->vd_blocknr,
  309. vdesc->vd_vblocknr, &bh);
  310. else
  311. ret = nilfs_gccache_submit_read_node(
  312. inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
  313. if (unlikely(ret < 0)) {
  314. if (ret == -ENOENT)
  315. printk(KERN_CRIT
  316. "%s: invalid virtual block address (%s): "
  317. "ino=%llu, cno=%llu, offset=%llu, "
  318. "blocknr=%llu, vblocknr=%llu\n",
  319. __func__, vdesc->vd_flags ? "node" : "data",
  320. (unsigned long long)vdesc->vd_ino,
  321. (unsigned long long)vdesc->vd_cno,
  322. (unsigned long long)vdesc->vd_offset,
  323. (unsigned long long)vdesc->vd_blocknr,
  324. (unsigned long long)vdesc->vd_vblocknr);
  325. return ret;
  326. }
  327. if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
  328. printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
  329. "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
  330. __func__, vdesc->vd_flags ? "node" : "data",
  331. (unsigned long long)vdesc->vd_ino,
  332. (unsigned long long)vdesc->vd_cno,
  333. (unsigned long long)vdesc->vd_offset,
  334. (unsigned long long)vdesc->vd_blocknr,
  335. (unsigned long long)vdesc->vd_vblocknr);
  336. brelse(bh);
  337. return -EEXIST;
  338. }
  339. list_add_tail(&bh->b_assoc_buffers, buffers);
  340. return 0;
  341. }
  342. static int nilfs_ioctl_move_blocks(struct super_block *sb,
  343. struct nilfs_argv *argv, void *buf)
  344. {
  345. size_t nmembs = argv->v_nmembs;
  346. struct the_nilfs *nilfs = sb->s_fs_info;
  347. struct inode *inode;
  348. struct nilfs_vdesc *vdesc;
  349. struct buffer_head *bh, *n;
  350. LIST_HEAD(buffers);
  351. ino_t ino;
  352. __u64 cno;
  353. int i, ret;
  354. for (i = 0, vdesc = buf; i < nmembs; ) {
  355. ino = vdesc->vd_ino;
  356. cno = vdesc->vd_cno;
  357. inode = nilfs_iget_for_gc(sb, ino, cno);
  358. if (IS_ERR(inode)) {
  359. ret = PTR_ERR(inode);
  360. goto failed;
  361. }
  362. if (list_empty(&NILFS_I(inode)->i_dirty)) {
  363. /*
  364. * Add the inode to GC inode list. Garbage Collection
  365. * is serialized and no two processes manipulate the
  366. * list simultaneously.
  367. */
  368. igrab(inode);
  369. list_add(&NILFS_I(inode)->i_dirty,
  370. &nilfs->ns_gc_inodes);
  371. }
  372. do {
  373. ret = nilfs_ioctl_move_inode_block(inode, vdesc,
  374. &buffers);
  375. if (unlikely(ret < 0)) {
  376. iput(inode);
  377. goto failed;
  378. }
  379. vdesc++;
  380. } while (++i < nmembs &&
  381. vdesc->vd_ino == ino && vdesc->vd_cno == cno);
  382. iput(inode); /* The inode still remains in GC inode list */
  383. }
  384. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  385. ret = nilfs_gccache_wait_and_mark_dirty(bh);
  386. if (unlikely(ret < 0)) {
  387. WARN_ON(ret == -EEXIST);
  388. goto failed;
  389. }
  390. list_del_init(&bh->b_assoc_buffers);
  391. brelse(bh);
  392. }
  393. return nmembs;
  394. failed:
  395. list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
  396. list_del_init(&bh->b_assoc_buffers);
  397. brelse(bh);
  398. }
  399. return ret;
  400. }
  401. static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
  402. struct nilfs_argv *argv, void *buf)
  403. {
  404. size_t nmembs = argv->v_nmembs;
  405. struct inode *cpfile = nilfs->ns_cpfile;
  406. struct nilfs_period *periods = buf;
  407. int ret, i;
  408. for (i = 0; i < nmembs; i++) {
  409. ret = nilfs_cpfile_delete_checkpoints(
  410. cpfile, periods[i].p_start, periods[i].p_end);
  411. if (ret < 0)
  412. return ret;
  413. }
  414. return nmembs;
  415. }
  416. static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
  417. struct nilfs_argv *argv, void *buf)
  418. {
  419. size_t nmembs = argv->v_nmembs;
  420. int ret;
  421. ret = nilfs_dat_freev(nilfs->ns_dat, buf, nmembs);
  422. return (ret < 0) ? ret : nmembs;
  423. }
  424. static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
  425. struct nilfs_argv *argv, void *buf)
  426. {
  427. size_t nmembs = argv->v_nmembs;
  428. struct nilfs_bmap *bmap = NILFS_I(nilfs->ns_dat)->i_bmap;
  429. struct nilfs_bdesc *bdescs = buf;
  430. int ret, i;
  431. for (i = 0; i < nmembs; i++) {
  432. /* XXX: use macro or inline func to check liveness */
  433. ret = nilfs_bmap_lookup_at_level(bmap,
  434. bdescs[i].bd_offset,
  435. bdescs[i].bd_level + 1,
  436. &bdescs[i].bd_blocknr);
  437. if (ret < 0) {
  438. if (ret != -ENOENT)
  439. return ret;
  440. bdescs[i].bd_blocknr = 0;
  441. }
  442. if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
  443. /* skip dead block */
  444. continue;
  445. if (bdescs[i].bd_level == 0) {
  446. ret = nilfs_mdt_mark_block_dirty(nilfs->ns_dat,
  447. bdescs[i].bd_offset);
  448. if (ret < 0) {
  449. WARN_ON(ret == -ENOENT);
  450. return ret;
  451. }
  452. } else {
  453. ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
  454. bdescs[i].bd_level);
  455. if (ret < 0) {
  456. WARN_ON(ret == -ENOENT);
  457. return ret;
  458. }
  459. }
  460. }
  461. return nmembs;
  462. }
  463. int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
  464. struct nilfs_argv *argv, void **kbufs)
  465. {
  466. const char *msg;
  467. int ret;
  468. ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
  469. if (ret < 0) {
  470. /*
  471. * can safely abort because checkpoints can be removed
  472. * independently.
  473. */
  474. msg = "cannot delete checkpoints";
  475. goto failed;
  476. }
  477. ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
  478. if (ret < 0) {
  479. /*
  480. * can safely abort because DAT file is updated atomically
  481. * using a copy-on-write technique.
  482. */
  483. msg = "cannot delete virtual blocks from DAT file";
  484. goto failed;
  485. }
  486. ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
  487. if (ret < 0) {
  488. /*
  489. * can safely abort because the operation is nondestructive.
  490. */
  491. msg = "cannot mark copying blocks dirty";
  492. goto failed;
  493. }
  494. return 0;
  495. failed:
  496. printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
  497. msg, ret);
  498. return ret;
  499. }
  500. static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
  501. unsigned int cmd, void __user *argp)
  502. {
  503. struct nilfs_argv argv[5];
  504. static const size_t argsz[5] = {
  505. sizeof(struct nilfs_vdesc),
  506. sizeof(struct nilfs_period),
  507. sizeof(__u64),
  508. sizeof(struct nilfs_bdesc),
  509. sizeof(__u64),
  510. };
  511. void __user *base;
  512. void *kbufs[5];
  513. struct the_nilfs *nilfs;
  514. size_t len, nsegs;
  515. int n, ret;
  516. if (!capable(CAP_SYS_ADMIN))
  517. return -EPERM;
  518. ret = mnt_want_write(filp->f_path.mnt);
  519. if (ret)
  520. return ret;
  521. ret = -EFAULT;
  522. if (copy_from_user(argv, argp, sizeof(argv)))
  523. goto out;
  524. ret = -EINVAL;
  525. nsegs = argv[4].v_nmembs;
  526. if (argv[4].v_size != argsz[4])
  527. goto out;
  528. /*
  529. * argv[4] points to segment numbers this ioctl cleans. We
  530. * use kmalloc() for its buffer because memory used for the
  531. * segment numbers is enough small.
  532. */
  533. kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
  534. nsegs * sizeof(__u64));
  535. if (IS_ERR(kbufs[4])) {
  536. ret = PTR_ERR(kbufs[4]);
  537. goto out;
  538. }
  539. nilfs = inode->i_sb->s_fs_info;
  540. for (n = 0; n < 4; n++) {
  541. ret = -EINVAL;
  542. if (argv[n].v_size != argsz[n])
  543. goto out_free;
  544. if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
  545. goto out_free;
  546. len = argv[n].v_size * argv[n].v_nmembs;
  547. base = (void __user *)(unsigned long)argv[n].v_base;
  548. if (len == 0) {
  549. kbufs[n] = NULL;
  550. continue;
  551. }
  552. kbufs[n] = vmalloc(len);
  553. if (!kbufs[n]) {
  554. ret = -ENOMEM;
  555. goto out_free;
  556. }
  557. if (copy_from_user(kbufs[n], base, len)) {
  558. ret = -EFAULT;
  559. vfree(kbufs[n]);
  560. goto out_free;
  561. }
  562. }
  563. /*
  564. * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
  565. * which will operates an inode list without blocking.
  566. * To protect the list from concurrent operations,
  567. * nilfs_ioctl_move_blocks should be atomic operation.
  568. */
  569. if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
  570. ret = -EBUSY;
  571. goto out_free;
  572. }
  573. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  574. ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
  575. if (ret < 0)
  576. printk(KERN_ERR "NILFS: GC failed during preparation: "
  577. "cannot read source blocks: err=%d\n", ret);
  578. else
  579. ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
  580. nilfs_remove_all_gcinodes(nilfs);
  581. clear_nilfs_gc_running(nilfs);
  582. out_free:
  583. while (--n >= 0)
  584. vfree(kbufs[n]);
  585. kfree(kbufs[4]);
  586. out:
  587. mnt_drop_write(filp->f_path.mnt);
  588. return ret;
  589. }
  590. static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
  591. unsigned int cmd, void __user *argp)
  592. {
  593. __u64 cno;
  594. int ret;
  595. struct the_nilfs *nilfs;
  596. ret = nilfs_construct_segment(inode->i_sb);
  597. if (ret < 0)
  598. return ret;
  599. if (argp != NULL) {
  600. nilfs = inode->i_sb->s_fs_info;
  601. down_read(&nilfs->ns_segctor_sem);
  602. cno = nilfs->ns_cno - 1;
  603. up_read(&nilfs->ns_segctor_sem);
  604. if (copy_to_user(argp, &cno, sizeof(cno)))
  605. return -EFAULT;
  606. }
  607. return 0;
  608. }
  609. static int nilfs_ioctl_resize(struct inode *inode, struct file *filp,
  610. void __user *argp)
  611. {
  612. __u64 newsize;
  613. int ret = -EPERM;
  614. if (!capable(CAP_SYS_ADMIN))
  615. goto out;
  616. ret = mnt_want_write(filp->f_path.mnt);
  617. if (ret)
  618. goto out;
  619. ret = -EFAULT;
  620. if (copy_from_user(&newsize, argp, sizeof(newsize)))
  621. goto out_drop_write;
  622. ret = nilfs_resize_fs(inode->i_sb, newsize);
  623. out_drop_write:
  624. mnt_drop_write(filp->f_path.mnt);
  625. out:
  626. return ret;
  627. }
  628. static int nilfs_ioctl_set_alloc_range(struct inode *inode, void __user *argp)
  629. {
  630. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  631. __u64 range[2];
  632. __u64 minseg, maxseg;
  633. unsigned long segbytes;
  634. int ret = -EPERM;
  635. if (!capable(CAP_SYS_ADMIN))
  636. goto out;
  637. ret = -EFAULT;
  638. if (copy_from_user(range, argp, sizeof(__u64[2])))
  639. goto out;
  640. ret = -ERANGE;
  641. if (range[1] > i_size_read(inode->i_sb->s_bdev->bd_inode))
  642. goto out;
  643. segbytes = nilfs->ns_blocks_per_segment * nilfs->ns_blocksize;
  644. minseg = range[0] + segbytes - 1;
  645. do_div(minseg, segbytes);
  646. maxseg = NILFS_SB2_OFFSET_BYTES(range[1]);
  647. do_div(maxseg, segbytes);
  648. maxseg--;
  649. ret = nilfs_sufile_set_alloc_range(nilfs->ns_sufile, minseg, maxseg);
  650. out:
  651. return ret;
  652. }
  653. static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
  654. unsigned int cmd, void __user *argp,
  655. size_t membsz,
  656. ssize_t (*dofunc)(struct the_nilfs *,
  657. __u64 *, int,
  658. void *, size_t, size_t))
  659. {
  660. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  661. struct nilfs_argv argv;
  662. int ret;
  663. if (copy_from_user(&argv, argp, sizeof(argv)))
  664. return -EFAULT;
  665. if (argv.v_size < membsz)
  666. return -EINVAL;
  667. ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
  668. if (ret < 0)
  669. return ret;
  670. if (copy_to_user(argp, &argv, sizeof(argv)))
  671. ret = -EFAULT;
  672. return ret;
  673. }
  674. long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  675. {
  676. struct inode *inode = filp->f_dentry->d_inode;
  677. void __user *argp = (void __user *)arg;
  678. switch (cmd) {
  679. case FS_IOC_GETFLAGS:
  680. return nilfs_ioctl_getflags(inode, argp);
  681. case FS_IOC_SETFLAGS:
  682. return nilfs_ioctl_setflags(inode, filp, argp);
  683. case FS_IOC_GETVERSION:
  684. return nilfs_ioctl_getversion(inode, argp);
  685. case NILFS_IOCTL_CHANGE_CPMODE:
  686. return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
  687. case NILFS_IOCTL_DELETE_CHECKPOINT:
  688. return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
  689. case NILFS_IOCTL_GET_CPINFO:
  690. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  691. sizeof(struct nilfs_cpinfo),
  692. nilfs_ioctl_do_get_cpinfo);
  693. case NILFS_IOCTL_GET_CPSTAT:
  694. return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
  695. case NILFS_IOCTL_GET_SUINFO:
  696. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  697. sizeof(struct nilfs_suinfo),
  698. nilfs_ioctl_do_get_suinfo);
  699. case NILFS_IOCTL_GET_SUSTAT:
  700. return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
  701. case NILFS_IOCTL_GET_VINFO:
  702. return nilfs_ioctl_get_info(inode, filp, cmd, argp,
  703. sizeof(struct nilfs_vinfo),
  704. nilfs_ioctl_do_get_vinfo);
  705. case NILFS_IOCTL_GET_BDESCS:
  706. return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
  707. case NILFS_IOCTL_CLEAN_SEGMENTS:
  708. return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
  709. case NILFS_IOCTL_SYNC:
  710. return nilfs_ioctl_sync(inode, filp, cmd, argp);
  711. case NILFS_IOCTL_RESIZE:
  712. return nilfs_ioctl_resize(inode, filp, argp);
  713. case NILFS_IOCTL_SET_ALLOC_RANGE:
  714. return nilfs_ioctl_set_alloc_range(inode, argp);
  715. default:
  716. return -ENOTTY;
  717. }
  718. }
  719. #ifdef CONFIG_COMPAT
  720. long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  721. {
  722. switch (cmd) {
  723. case FS_IOC32_GETFLAGS:
  724. cmd = FS_IOC_GETFLAGS;
  725. break;
  726. case FS_IOC32_SETFLAGS:
  727. cmd = FS_IOC_SETFLAGS;
  728. break;
  729. case FS_IOC32_GETVERSION:
  730. cmd = FS_IOC_GETVERSION;
  731. break;
  732. case NILFS_IOCTL_CHANGE_CPMODE:
  733. case NILFS_IOCTL_DELETE_CHECKPOINT:
  734. case NILFS_IOCTL_GET_CPINFO:
  735. case NILFS_IOCTL_GET_CPSTAT:
  736. case NILFS_IOCTL_GET_SUINFO:
  737. case NILFS_IOCTL_GET_SUSTAT:
  738. case NILFS_IOCTL_GET_VINFO:
  739. case NILFS_IOCTL_GET_BDESCS:
  740. case NILFS_IOCTL_CLEAN_SEGMENTS:
  741. case NILFS_IOCTL_SYNC:
  742. case NILFS_IOCTL_RESIZE:
  743. case NILFS_IOCTL_SET_ALLOC_RANGE:
  744. break;
  745. default:
  746. return -ENOIOCTLCMD;
  747. }
  748. return nilfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  749. }
  750. #endif