2 * ioctl.c - NILFS ioctl operations.
4 * Copyright (C) 2007, 2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Koji Sato <koji@osrg.net>.
24 #include <linux/wait.h>
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>
39 static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
40 struct nilfs_argv *argv, int dir,
41 ssize_t (*dofunc)(struct the_nilfs *,
43 void *, size_t, size_t))
46 void __user *base = (void __user *)(unsigned long)argv->v_base;
47 size_t maxmembs, total, n;
52 if (argv->v_nmembs == 0)
55 if (argv->v_size > PAGE_SIZE)
58 buf = (void *)__get_free_pages(GFP_NOFS, 0);
61 maxmembs = PAGE_SIZE / argv->v_size;
66 for (i = 0; i < argv->v_nmembs; i += n) {
67 n = (argv->v_nmembs - i < maxmembs) ?
68 argv->v_nmembs - i : maxmembs;
69 if ((dir & _IOC_WRITE) &&
70 copy_from_user(buf, base + argv->v_size * i,
76 nr = dofunc(nilfs, &pos, argv->v_flags, buf, argv->v_size,
82 if ((dir & _IOC_READ) &&
83 copy_to_user(base + argv->v_size * i, buf,
94 argv->v_nmembs = total;
96 free_pages((unsigned long)buf, 0);
100 static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
101 unsigned int cmd, void __user *argp)
103 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
104 struct inode *cpfile = nilfs->ns_cpfile;
105 struct nilfs_transaction_info ti;
106 struct nilfs_cpmode cpmode;
109 if (!capable(CAP_SYS_ADMIN))
112 ret = mnt_want_write(filp->f_path.mnt);
117 if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
120 down_read(&inode->i_sb->s_umount);
122 nilfs_transaction_begin(inode->i_sb, &ti, 0);
123 ret = nilfs_cpfile_change_cpmode(
124 cpfile, cpmode.cm_cno, cpmode.cm_mode);
125 if (unlikely(ret < 0))
126 nilfs_transaction_abort(inode->i_sb);
128 nilfs_transaction_commit(inode->i_sb); /* never fails */
130 up_read(&inode->i_sb->s_umount);
132 mnt_drop_write(filp->f_path.mnt);
137 nilfs_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
138 unsigned int cmd, void __user *argp)
140 struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
141 struct nilfs_transaction_info ti;
145 if (!capable(CAP_SYS_ADMIN))
148 ret = mnt_want_write(filp->f_path.mnt);
153 if (copy_from_user(&cno, argp, sizeof(cno)))
156 nilfs_transaction_begin(inode->i_sb, &ti, 0);
157 ret = nilfs_cpfile_delete_checkpoint(cpfile, cno);
158 if (unlikely(ret < 0))
159 nilfs_transaction_abort(inode->i_sb);
161 nilfs_transaction_commit(inode->i_sb); /* never fails */
163 mnt_drop_write(filp->f_path.mnt);
168 nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
169 void *buf, size_t size, size_t nmembs)
173 down_read(&nilfs->ns_segctor_sem);
174 ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
176 up_read(&nilfs->ns_segctor_sem);
180 static int nilfs_ioctl_get_cpstat(struct inode *inode, struct file *filp,
181 unsigned int cmd, void __user *argp)
183 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
184 struct nilfs_cpstat cpstat;
187 down_read(&nilfs->ns_segctor_sem);
188 ret = nilfs_cpfile_get_stat(nilfs->ns_cpfile, &cpstat);
189 up_read(&nilfs->ns_segctor_sem);
193 if (copy_to_user(argp, &cpstat, sizeof(cpstat)))
199 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
200 void *buf, size_t size, size_t nmembs)
204 down_read(&nilfs->ns_segctor_sem);
205 ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
207 up_read(&nilfs->ns_segctor_sem);
211 static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp,
212 unsigned int cmd, void __user *argp)
214 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
215 struct nilfs_sustat sustat;
218 down_read(&nilfs->ns_segctor_sem);
219 ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat);
220 up_read(&nilfs->ns_segctor_sem);
224 if (copy_to_user(argp, &sustat, sizeof(sustat)))
230 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
231 void *buf, size_t size, size_t nmembs)
235 down_read(&nilfs->ns_segctor_sem);
236 ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
237 up_read(&nilfs->ns_segctor_sem);
242 nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
243 void *buf, size_t size, size_t nmembs)
245 struct inode *dat = nilfs_dat_inode(nilfs);
246 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
247 struct nilfs_bdesc *bdescs = buf;
250 down_read(&nilfs->ns_segctor_sem);
251 for (i = 0; i < nmembs; i++) {
252 ret = nilfs_bmap_lookup_at_level(bmap,
254 bdescs[i].bd_level + 1,
255 &bdescs[i].bd_blocknr);
257 if (ret != -ENOENT) {
258 up_read(&nilfs->ns_segctor_sem);
261 bdescs[i].bd_blocknr = 0;
264 up_read(&nilfs->ns_segctor_sem);
268 static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
269 unsigned int cmd, void __user *argp)
271 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
272 struct nilfs_argv argv;
275 if (copy_from_user(&argv, argp, sizeof(argv)))
278 if (argv.v_size != sizeof(struct nilfs_bdesc))
281 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
282 nilfs_ioctl_do_get_bdescs);
286 if (copy_to_user(argp, &argv, sizeof(argv)))
291 static int nilfs_ioctl_move_inode_block(struct inode *inode,
292 struct nilfs_vdesc *vdesc,
293 struct list_head *buffers)
295 struct buffer_head *bh;
298 if (vdesc->vd_flags == 0)
299 ret = nilfs_gccache_submit_read_data(
300 inode, vdesc->vd_offset, vdesc->vd_blocknr,
301 vdesc->vd_vblocknr, &bh);
303 ret = nilfs_gccache_submit_read_node(
304 inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh);
306 if (unlikely(ret < 0)) {
309 "%s: invalid virtual block address (%s): "
310 "ino=%llu, cno=%llu, offset=%llu, "
311 "blocknr=%llu, vblocknr=%llu\n",
312 __func__, vdesc->vd_flags ? "node" : "data",
313 (unsigned long long)vdesc->vd_ino,
314 (unsigned long long)vdesc->vd_cno,
315 (unsigned long long)vdesc->vd_offset,
316 (unsigned long long)vdesc->vd_blocknr,
317 (unsigned long long)vdesc->vd_vblocknr);
320 if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
321 printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
322 "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
323 __func__, vdesc->vd_flags ? "node" : "data",
324 (unsigned long long)vdesc->vd_ino,
325 (unsigned long long)vdesc->vd_cno,
326 (unsigned long long)vdesc->vd_offset,
327 (unsigned long long)vdesc->vd_blocknr,
328 (unsigned long long)vdesc->vd_vblocknr);
332 list_add_tail(&bh->b_assoc_buffers, buffers);
336 static int nilfs_ioctl_move_blocks(struct super_block *sb,
337 struct nilfs_argv *argv, void *buf)
339 size_t nmembs = argv->v_nmembs;
341 struct nilfs_vdesc *vdesc;
342 struct buffer_head *bh, *n;
348 for (i = 0, vdesc = buf; i < nmembs; ) {
351 inode = nilfs_iget_for_gc(sb, ino, cno);
353 ret = PTR_ERR(inode);
357 ret = nilfs_ioctl_move_inode_block(inode, vdesc,
359 if (unlikely(ret < 0)) {
364 } while (++i < nmembs &&
365 vdesc->vd_ino == ino && vdesc->vd_cno == cno);
367 iput(inode); /* The inode still remains in GC inode list */
370 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
371 ret = nilfs_gccache_wait_and_mark_dirty(bh);
372 if (unlikely(ret < 0)) {
373 WARN_ON(ret == -EEXIST);
376 list_del_init(&bh->b_assoc_buffers);
382 list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
383 list_del_init(&bh->b_assoc_buffers);
389 static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
390 struct nilfs_argv *argv, void *buf)
392 size_t nmembs = argv->v_nmembs;
393 struct inode *cpfile = nilfs->ns_cpfile;
394 struct nilfs_period *periods = buf;
397 for (i = 0; i < nmembs; i++) {
398 ret = nilfs_cpfile_delete_checkpoints(
399 cpfile, periods[i].p_start, periods[i].p_end);
406 static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
407 struct nilfs_argv *argv, void *buf)
409 size_t nmembs = argv->v_nmembs;
412 ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
414 return (ret < 0) ? ret : nmembs;
417 static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
418 struct nilfs_argv *argv, void *buf)
420 size_t nmembs = argv->v_nmembs;
421 struct inode *dat = nilfs_dat_inode(nilfs);
422 struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
423 struct nilfs_bdesc *bdescs = buf;
426 for (i = 0; i < nmembs; i++) {
427 /* XXX: use macro or inline func to check liveness */
428 ret = nilfs_bmap_lookup_at_level(bmap,
430 bdescs[i].bd_level + 1,
431 &bdescs[i].bd_blocknr);
435 bdescs[i].bd_blocknr = 0;
437 if (bdescs[i].bd_blocknr != bdescs[i].bd_oblocknr)
438 /* skip dead block */
440 if (bdescs[i].bd_level == 0) {
441 ret = nilfs_mdt_mark_block_dirty(dat,
442 bdescs[i].bd_offset);
444 WARN_ON(ret == -ENOENT);
448 ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
451 WARN_ON(ret == -ENOENT);
459 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
460 struct nilfs_argv *argv, void **kbufs)
465 ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
468 * can safely abort because checkpoints can be removed
471 msg = "cannot delete checkpoints";
474 ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
477 * can safely abort because DAT file is updated atomically
478 * using a copy-on-write technique.
480 msg = "cannot delete virtual blocks from DAT file";
483 ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
486 * can safely abort because the operation is nondestructive.
488 msg = "cannot mark copying blocks dirty";
494 printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
499 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
500 unsigned int cmd, void __user *argp)
502 struct nilfs_argv argv[5];
503 static const size_t argsz[5] = {
504 sizeof(struct nilfs_vdesc),
505 sizeof(struct nilfs_period),
507 sizeof(struct nilfs_bdesc),
512 struct the_nilfs *nilfs;
516 if (!capable(CAP_SYS_ADMIN))
519 ret = mnt_want_write(filp->f_path.mnt);
524 if (copy_from_user(argv, argp, sizeof(argv)))
528 nsegs = argv[4].v_nmembs;
529 if (argv[4].v_size != argsz[4])
533 * argv[4] points to segment numbers this ioctl cleans. We
534 * use kmalloc() for its buffer because memory used for the
535 * segment numbers is enough small.
537 kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
538 nsegs * sizeof(__u64));
539 if (IS_ERR(kbufs[4])) {
540 ret = PTR_ERR(kbufs[4]);
543 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
545 for (n = 0; n < 4; n++) {
547 if (argv[n].v_size != argsz[n])
550 if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
553 len = argv[n].v_size * argv[n].v_nmembs;
554 base = (void __user *)(unsigned long)argv[n].v_base;
560 kbufs[n] = vmalloc(len);
565 if (copy_from_user(kbufs[n], base, len)) {
573 * nilfs_ioctl_move_blocks() will call nilfs_iget_for_gc(),
574 * which will operates an inode list without blocking.
575 * To protect the list from concurrent operations,
576 * nilfs_ioctl_move_blocks should be atomic operation.
578 if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
583 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
585 ret = nilfs_ioctl_move_blocks(inode->i_sb, &argv[0], kbufs[0]);
587 printk(KERN_ERR "NILFS: GC failed during preparation: "
588 "cannot read source blocks: err=%d\n", ret);
590 ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
592 nilfs_remove_all_gcinodes(nilfs);
593 clear_nilfs_gc_running(nilfs);
600 mnt_drop_write(filp->f_path.mnt);
604 static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
605 unsigned int cmd, void __user *argp)
609 struct the_nilfs *nilfs;
611 ret = nilfs_construct_segment(inode->i_sb);
616 nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
617 down_read(&nilfs->ns_segctor_sem);
618 cno = nilfs->ns_cno - 1;
619 up_read(&nilfs->ns_segctor_sem);
620 if (copy_to_user(argp, &cno, sizeof(cno)))
626 static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
627 unsigned int cmd, void __user *argp,
629 ssize_t (*dofunc)(struct the_nilfs *,
631 void *, size_t, size_t))
634 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
635 struct nilfs_argv argv;
638 if (copy_from_user(&argv, argp, sizeof(argv)))
641 if (argv.v_size < membsz)
644 ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
648 if (copy_to_user(argp, &argv, sizeof(argv)))
653 long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
655 struct inode *inode = filp->f_dentry->d_inode;
656 void __user *argp = (void __user *)arg;
659 case NILFS_IOCTL_CHANGE_CPMODE:
660 return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp);
661 case NILFS_IOCTL_DELETE_CHECKPOINT:
662 return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
663 case NILFS_IOCTL_GET_CPINFO:
664 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
665 sizeof(struct nilfs_cpinfo),
666 nilfs_ioctl_do_get_cpinfo);
667 case NILFS_IOCTL_GET_CPSTAT:
668 return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
669 case NILFS_IOCTL_GET_SUINFO:
670 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
671 sizeof(struct nilfs_suinfo),
672 nilfs_ioctl_do_get_suinfo);
673 case NILFS_IOCTL_GET_SUSTAT:
674 return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
675 case NILFS_IOCTL_GET_VINFO:
676 return nilfs_ioctl_get_info(inode, filp, cmd, argp,
677 sizeof(struct nilfs_vinfo),
678 nilfs_ioctl_do_get_vinfo);
679 case NILFS_IOCTL_GET_BDESCS:
680 return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
681 case NILFS_IOCTL_CLEAN_SEGMENTS:
682 return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
683 case NILFS_IOCTL_SYNC:
684 return nilfs_ioctl_sync(inode, filp, cmd, argp);