ioctl.c 17 KB

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