ioctl.c 17 KB

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