nilfs2: add local variable to cache the number of clean segments
[safe/jmp/linux-2.6] / fs / nilfs2 / ioctl.c
index 9e4d9e6..f6af760 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/smp_lock.h>    /* lock_kernel(), unlock_kernel() */
 #include <linux/capability.h>  /* capable() */
 #include <linux/uaccess.h>     /* copy_from_user(), copy_to_user() */
+#include <linux/vmalloc.h>
 #include <linux/nilfs2_fs.h>
 #include "nilfs.h"
 #include "segment.h"
@@ -41,6 +42,7 @@ static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
                                                   void *, size_t, size_t))
 {
        void *buf;
+       void __user *base = (void __user *)(unsigned long)argv->v_base;
        size_t maxmembs, total, n;
        ssize_t nr;
        int ret, i;
@@ -64,9 +66,8 @@ static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
                n = (argv->v_nmembs - i < maxmembs) ?
                        argv->v_nmembs - i : maxmembs;
                if ((dir & _IOC_WRITE) &&
-                   copy_from_user(buf,
-                           (void __user *)argv->v_base + argv->v_size * i,
-                           argv->v_size * n)) {
+                   copy_from_user(buf, base + argv->v_size * i,
+                                  argv->v_size * n)) {
                        ret = -EFAULT;
                        break;
                }
@@ -78,9 +79,8 @@ static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
                        break;
                }
                if ((dir & _IOC_READ) &&
-                   copy_to_user(
-                           (void __user *)argv->v_base + argv->v_size * i,
-                           buf, argv->v_size * nr)) {
+                   copy_to_user(base + argv->v_size * i, buf,
+                                argv->v_size * nr)) {
                        ret = -EFAULT;
                        break;
                }
@@ -99,7 +99,8 @@ static int nilfs_ioctl_wrap_copy(struct the_nilfs *nilfs,
 static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
                                     unsigned int cmd, void __user *argp)
 {
-       struct inode *cpfile = NILFS_SB(inode->i_sb)->s_nilfs->ns_cpfile;
+       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
+       struct inode *cpfile = nilfs->ns_cpfile;
        struct nilfs_transaction_info ti;
        struct nilfs_cpmode cpmode;
        int ret;
@@ -109,14 +110,17 @@ static int nilfs_ioctl_change_cpmode(struct inode *inode, struct file *filp,
        if (copy_from_user(&cpmode, argp, sizeof(cpmode)))
                return -EFAULT;
 
+       mutex_lock(&nilfs->ns_mount_mutex);
        nilfs_transaction_begin(inode->i_sb, &ti, 0);
        ret = nilfs_cpfile_change_cpmode(
                cpfile, cpmode.cm_cno, cpmode.cm_mode);
        if (unlikely(ret < 0)) {
                nilfs_transaction_abort(inode->i_sb);
+               mutex_unlock(&nilfs->ns_mount_mutex);
                return ret;
        }
        nilfs_transaction_commit(inode->i_sb); /* never fails */
+       mutex_unlock(&nilfs->ns_mount_mutex);
        return ret;
 }
 
@@ -148,29 +152,12 @@ static ssize_t
 nilfs_ioctl_do_get_cpinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
                          void *buf, size_t size, size_t nmembs)
 {
-       return nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
-                                      nmembs);
-}
-
-static int nilfs_ioctl_get_cpinfo(struct inode *inode, struct file *filp,
-                                 unsigned int cmd, void __user *argp)
-{
-       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
-       struct nilfs_argv argv;
        int ret;
 
-       if (copy_from_user(&argv, argp, sizeof(argv)))
-               return -EFAULT;
-
        down_read(&nilfs->ns_segctor_sem);
-       ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
-                                   nilfs_ioctl_do_get_cpinfo);
+       ret = nilfs_cpfile_get_cpinfo(nilfs->ns_cpfile, posp, flags, buf,
+                                     size, nmembs);
        up_read(&nilfs->ns_segctor_sem);
-       if (ret < 0)
-               return ret;
-
-       if (copy_to_user(argp, &argv, sizeof(argv)))
-               ret = -EFAULT;
        return ret;
 }
 
@@ -196,28 +183,12 @@ static ssize_t
 nilfs_ioctl_do_get_suinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
                          void *buf, size_t size, size_t nmembs)
 {
-       return nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs);
-}
-
-static int nilfs_ioctl_get_suinfo(struct inode *inode, struct file *filp,
-                                 unsigned int cmd, void __user *argp)
-{
-       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
-       struct nilfs_argv argv;
        int ret;
 
-       if (copy_from_user(&argv, argp, sizeof(argv)))
-               return -EFAULT;
-
        down_read(&nilfs->ns_segctor_sem);
-       ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
-                                   nilfs_ioctl_do_get_suinfo);
+       ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, size,
+                                     nmembs);
        up_read(&nilfs->ns_segctor_sem);
-       if (ret < 0)
-               return ret;
-
-       if (copy_to_user(argp, &argv, sizeof(argv)))
-               ret = -EFAULT;
        return ret;
 }
 
@@ -243,28 +214,11 @@ static ssize_t
 nilfs_ioctl_do_get_vinfo(struct the_nilfs *nilfs, __u64 *posp, int flags,
                         void *buf, size_t size, size_t nmembs)
 {
-       return nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs);
-}
-
-static int nilfs_ioctl_get_vinfo(struct inode *inode, struct file *filp,
-                                unsigned int cmd, void __user *argp)
-{
-       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
-       struct nilfs_argv argv;
        int ret;
 
-       if (copy_from_user(&argv, argp, sizeof(argv)))
-               return -EFAULT;
-
        down_read(&nilfs->ns_segctor_sem);
-       ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
-                                   nilfs_ioctl_do_get_vinfo);
+       ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, size, nmembs);
        up_read(&nilfs->ns_segctor_sem);
-       if (ret < 0)
-               return ret;
-
-       if (copy_to_user(argp, &argv, sizeof(argv)))
-               ret = -EFAULT;
        return ret;
 }
 
@@ -277,17 +231,21 @@ nilfs_ioctl_do_get_bdescs(struct the_nilfs *nilfs, __u64 *posp, int flags,
        struct nilfs_bdesc *bdescs = buf;
        int ret, i;
 
+       down_read(&nilfs->ns_segctor_sem);
        for (i = 0; i < nmembs; i++) {
                ret = nilfs_bmap_lookup_at_level(bmap,
                                                 bdescs[i].bd_offset,
                                                 bdescs[i].bd_level + 1,
                                                 &bdescs[i].bd_blocknr);
                if (ret < 0) {
-                       if (ret != -ENOENT)
+                       if (ret != -ENOENT) {
+                               up_read(&nilfs->ns_segctor_sem);
                                return ret;
+                       }
                        bdescs[i].bd_blocknr = 0;
                }
        }
+       up_read(&nilfs->ns_segctor_sem);
        return nmembs;
 }
 
@@ -301,10 +259,11 @@ static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp,
        if (copy_from_user(&argv, argp, sizeof(argv)))
                return -EFAULT;
 
-       down_read(&nilfs->ns_segctor_sem);
+       if (argv.v_size != sizeof(struct nilfs_bdesc))
+               return -EINVAL;
+
        ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd),
                                    nilfs_ioctl_do_get_bdescs);
-       up_read(&nilfs->ns_segctor_sem);
        if (ret < 0)
                return ret;
 
@@ -342,15 +301,26 @@ static int nilfs_ioctl_move_inode_block(struct inode *inode,
                               (unsigned long long)vdesc->vd_vblocknr);
                return ret;
        }
-       bh->b_private = vdesc;
+       if (unlikely(!list_empty(&bh->b_assoc_buffers))) {
+               printk(KERN_CRIT "%s: conflicting %s buffer: ino=%llu, "
+                      "cno=%llu, offset=%llu, blocknr=%llu, vblocknr=%llu\n",
+                      __func__, vdesc->vd_flags ? "node" : "data",
+                      (unsigned long long)vdesc->vd_ino,
+                      (unsigned long long)vdesc->vd_cno,
+                      (unsigned long long)vdesc->vd_offset,
+                      (unsigned long long)vdesc->vd_blocknr,
+                      (unsigned long long)vdesc->vd_vblocknr);
+               brelse(bh);
+               return -EEXIST;
+       }
        list_add_tail(&bh->b_assoc_buffers, buffers);
        return 0;
 }
 
-static ssize_t
-nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, __u64 *posp, int flags,
-                          void *buf, size_t size, size_t nmembs)
+static int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
+                                  struct nilfs_argv *argv, void *buf)
 {
+       size_t nmembs = argv->v_nmembs;
        struct inode *inode;
        struct nilfs_vdesc *vdesc;
        struct buffer_head *bh, *n;
@@ -380,24 +350,10 @@ nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, __u64 *posp, int flags,
        list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
                ret = nilfs_gccache_wait_and_mark_dirty(bh);
                if (unlikely(ret < 0)) {
-                       if (ret == -EEXIST) {
-                               vdesc = bh->b_private;
-                               printk(KERN_CRIT
-                                      "%s: conflicting %s buffer: "
-                                      "ino=%llu, cno=%llu, offset=%llu, "
-                                      "blocknr=%llu, vblocknr=%llu\n",
-                                      __func__,
-                                      vdesc->vd_flags ? "node" : "data",
-                                      (unsigned long long)vdesc->vd_ino,
-                                      (unsigned long long)vdesc->vd_cno,
-                                      (unsigned long long)vdesc->vd_offset,
-                                      (unsigned long long)vdesc->vd_blocknr,
-                                      (unsigned long long)vdesc->vd_vblocknr);
-                       }
+                       WARN_ON(ret == -EEXIST);
                        goto failed;
                }
                list_del_init(&bh->b_assoc_buffers);
-               bh->b_private = NULL;
                brelse(bh);
        }
        return nmembs;
@@ -405,25 +361,15 @@ nilfs_ioctl_do_move_blocks(struct the_nilfs *nilfs, __u64 *posp, int flags,
  failed:
        list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) {
                list_del_init(&bh->b_assoc_buffers);
-               bh->b_private = NULL;
                brelse(bh);
        }
        return ret;
 }
 
-static inline int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs,
-                                         struct nilfs_argv *argv,
-                                         int dir)
-{
-       return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
-                                    nilfs_ioctl_do_move_blocks);
-}
-
-static ssize_t
-nilfs_ioctl_do_delete_checkpoints(struct the_nilfs *nilfs, __u64 *posp,
-                                 int flags, void *buf, size_t size,
-                                 size_t nmembs)
+static int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
+                                         struct nilfs_argv *argv, void *buf)
 {
+       size_t nmembs = argv->v_nmembs;
        struct inode *cpfile = nilfs->ns_cpfile;
        struct nilfs_period *periods = buf;
        int ret, i;
@@ -437,36 +383,21 @@ nilfs_ioctl_do_delete_checkpoints(struct the_nilfs *nilfs, __u64 *posp,
        return nmembs;
 }
 
-static inline int nilfs_ioctl_delete_checkpoints(struct the_nilfs *nilfs,
-                                                struct nilfs_argv *argv,
-                                                int dir)
+static int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
+                                     struct nilfs_argv *argv, void *buf)
 {
-       return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
-                                    nilfs_ioctl_do_delete_checkpoints);
-}
+       size_t nmembs = argv->v_nmembs;
+       int ret;
 
-static ssize_t
-nilfs_ioctl_do_free_vblocknrs(struct the_nilfs *nilfs, __u64 *posp, int flags,
-                             void *buf, size_t size, size_t nmembs)
-{
-       int ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
+       ret = nilfs_dat_freev(nilfs_dat_inode(nilfs), buf, nmembs);
 
        return (ret < 0) ? ret : nmembs;
 }
 
-static inline int nilfs_ioctl_free_vblocknrs(struct the_nilfs *nilfs,
-                                            struct nilfs_argv *argv,
-                                            int dir)
-{
-       return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
-                                    nilfs_ioctl_do_free_vblocknrs);
-}
-
-static ssize_t
-nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, __u64 *posp,
-                                int flags, void *buf, size_t size,
-                                size_t nmembs)
+static int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
+                                        struct nilfs_argv *argv, void *buf)
 {
+       size_t nmembs = argv->v_nmembs;
        struct inode *dat = nilfs_dat_inode(nilfs);
        struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap;
        struct nilfs_bdesc *bdescs = buf;
@@ -490,14 +421,14 @@ nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, __u64 *posp,
                        ret = nilfs_mdt_mark_block_dirty(dat,
                                                         bdescs[i].bd_offset);
                        if (ret < 0) {
-                               BUG_ON(ret == -ENOENT);
+                               WARN_ON(ret == -ENOENT);
                                return ret;
                        }
                } else {
                        ret = nilfs_bmap_mark(bmap, bdescs[i].bd_offset,
                                              bdescs[i].bd_level);
                        if (ret < 0) {
-                               BUG_ON(ret == -ENOENT);
+                               WARN_ON(ret == -ENOENT);
                                return ret;
                        }
                }
@@ -505,135 +436,138 @@ nilfs_ioctl_do_mark_blocks_dirty(struct the_nilfs *nilfs, __u64 *posp,
        return nmembs;
 }
 
-static inline int nilfs_ioctl_mark_blocks_dirty(struct the_nilfs *nilfs,
-                                               struct nilfs_argv *argv,
-                                               int dir)
-{
-       return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
-                                    nilfs_ioctl_do_mark_blocks_dirty);
-}
-
-static ssize_t
-nilfs_ioctl_do_free_segments(struct the_nilfs *nilfs, __u64 *posp, int flags,
-                            void *buf, size_t size, size_t nmembs)
-{
-       struct nilfs_sb_info *sbi = nilfs_get_writer(nilfs);
-       int ret;
-
-       BUG_ON(!sbi);
-       ret = nilfs_segctor_add_segments_to_be_freed(
-               NILFS_SC(sbi), buf, nmembs);
-       nilfs_put_writer(nilfs);
-
-       return (ret < 0) ? ret : nmembs;
-}
-
-static inline int nilfs_ioctl_free_segments(struct the_nilfs *nilfs,
-                                            struct nilfs_argv *argv,
-                                            int dir)
-{
-       return nilfs_ioctl_wrap_copy(nilfs, argv, dir,
-                                    nilfs_ioctl_do_free_segments);
-}
-
 int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs,
-                                      void __user *argp)
+                                      struct nilfs_argv *argv, void **kbufs)
 {
-       struct nilfs_argv argv[5];
-       int dir, ret;
-
-       if (copy_from_user(argv, argp, sizeof(argv)))
-               return -EFAULT;
-
-       dir = _IOC_WRITE;
-       ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], dir);
-       if (ret < 0)
-               goto out_move_blks;
-       ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], dir);
-       if (ret < 0)
-               goto out_del_cps;
-       ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], dir);
-       if (ret < 0)
-               goto out_free_vbns;
-       ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], dir);
-       if (ret < 0)
-               goto out_free_vbns;
-       ret = nilfs_ioctl_free_segments(nilfs, &argv[4], dir);
-       if (ret < 0)
-               goto out_free_segs;
+       const char *msg;
+       int ret;
 
+       ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]);
+       if (ret < 0) {
+               /*
+                * can safely abort because checkpoints can be removed
+                * independently.
+                */
+               msg = "cannot delete checkpoints";
+               goto failed;
+       }
+       ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]);
+       if (ret < 0) {
+               /*
+                * can safely abort because DAT file is updated atomically
+                * using a copy-on-write technique.
+                */
+               msg = "cannot delete virtual blocks from DAT file";
+               goto failed;
+       }
+       ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]);
+       if (ret < 0) {
+               /*
+                * can safely abort because the operation is nondestructive.
+                */
+               msg = "cannot mark copying blocks dirty";
+               goto failed;
+       }
        return 0;
 
- out_free_segs:
-       BUG(); /* XXX: not implemented yet */
- out_free_vbns:
-       BUG();/* XXX: not implemented yet */
- out_del_cps:
-       BUG();/* XXX: not implemented yet */
- out_move_blks:
-       nilfs_remove_all_gcinode(nilfs);
+ failed:
+       printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n",
+              msg, ret);
        return ret;
 }
 
 static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp,
                                      unsigned int cmd, void __user *argp)
 {
-       int ret;
+       struct nilfs_argv argv[5];
+       const static size_t argsz[5] = {
+               sizeof(struct nilfs_vdesc),
+               sizeof(struct nilfs_period),
+               sizeof(__u64),
+               sizeof(struct nilfs_bdesc),
+               sizeof(__u64),
+       };
+       void __user *base;
+       void *kbufs[5];
+       struct the_nilfs *nilfs;
+       size_t len, nsegs;
+       int n, ret;
 
        if (!capable(CAP_SYS_ADMIN))
                return -EPERM;
 
-       ret = nilfs_clean_segments(inode->i_sb, argp);
-       clear_nilfs_cond_nongc_write(NILFS_SB(inode->i_sb)->s_nilfs);
-       return ret;
-}
+       if (copy_from_user(argv, argp, sizeof(argv)))
+               return -EFAULT;
 
-static int nilfs_ioctl_test_cond(struct the_nilfs *nilfs, int cond)
-{
-       return (cond & NILFS_TIMEDWAIT_SEG_WRITE) &&
-               nilfs_cond_nongc_write(nilfs);
-}
+       nsegs = argv[4].v_nmembs;
+       if (argv[4].v_size != argsz[4])
+               return -EINVAL;
+       /*
+        * argv[4] points to segment numbers this ioctl cleans.  We
+        * use kmalloc() for its buffer because memory used for the
+        * segment numbers is enough small.
+        */
+       kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base,
+                              nsegs * sizeof(__u64));
+       if (IS_ERR(kbufs[4]))
+               return PTR_ERR(kbufs[4]);
+
+       nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
+
+       for (n = 0; n < 4; n++) {
+               ret = -EINVAL;
+               if (argv[n].v_size != argsz[n])
+                       goto out_free;
+
+               if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment)
+                       goto out_free;
+
+               len = argv[n].v_size * argv[n].v_nmembs;
+               base = (void __user *)(unsigned long)argv[n].v_base;
+               if (len == 0) {
+                       kbufs[n] = NULL;
+                       continue;
+               }
 
-static void nilfs_ioctl_clear_cond(struct the_nilfs *nilfs, int cond)
-{
-       if (cond & NILFS_TIMEDWAIT_SEG_WRITE)
-               clear_nilfs_cond_nongc_write(nilfs);
-}
+               kbufs[n] = vmalloc(len);
+               if (!kbufs[n]) {
+                       ret = -ENOMEM;
+                       goto out_free;
+               }
+               if (copy_from_user(kbufs[n], base, len)) {
+                       ret = -EFAULT;
+                       vfree(kbufs[n]);
+                       goto out_free;
+               }
+       }
 
-static int nilfs_ioctl_timedwait(struct inode *inode, struct file *filp,
-                                unsigned int cmd, void __user *argp)
-{
-       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
-       struct nilfs_wait_cond wc;
-       long ret;
+       /*
+        * nilfs_ioctl_move_blocks() will call nilfs_gc_iget(),
+        * which will operates an inode list without blocking.
+        * To protect the list from concurrent operations,
+        * nilfs_ioctl_move_blocks should be atomic operation.
+        */
+       if (test_and_set_bit(THE_NILFS_GC_RUNNING, &nilfs->ns_flags)) {
+               ret = -EBUSY;
+               goto out_free;
+       }
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       if (copy_from_user(&wc, argp, sizeof(wc)))
-               return -EFAULT;
+       ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], kbufs[0]);
+       if (ret < 0)
+               printk(KERN_ERR "NILFS: GC failed during preparation: "
+                       "cannot read source blocks: err=%d\n", ret);
+       else
+               ret = nilfs_clean_segments(inode->i_sb, argv, kbufs);
 
-       unlock_kernel();
-       ret = wc.wc_flags ?
-               wait_event_interruptible_timeout(
-                       nilfs->ns_cleanerd_wq,
-                       nilfs_ioctl_test_cond(nilfs, wc.wc_cond),
-                       timespec_to_jiffies(&wc.wc_timeout)) :
-               wait_event_interruptible(
-                       nilfs->ns_cleanerd_wq,
-                       nilfs_ioctl_test_cond(nilfs, wc.wc_cond));
-       lock_kernel();
-       nilfs_ioctl_clear_cond(nilfs, wc.wc_cond);
-
-       if (ret > 0) {
-               jiffies_to_timespec(ret, &wc.wc_timeout);
-               if (copy_to_user(argp, &wc, sizeof(wc)))
-                       return -EFAULT;
-               return 0;
-       }
-       if (ret != 0)
-               return -EINTR;
+       if (ret < 0)
+               nilfs_remove_all_gcinode(nilfs);
+       clear_nilfs_gc_running(nilfs);
 
-       return wc.wc_flags ? -ETIME : 0;
+ out_free:
+       while (--n >= 0)
+               vfree(kbufs[n]);
+       kfree(kbufs[4]);
+       return ret;
 }
 
 static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
@@ -654,9 +588,36 @@ static int nilfs_ioctl_sync(struct inode *inode, struct file *filp,
        return 0;
 }
 
-int nilfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
-               unsigned long arg)
+static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp,
+                               unsigned int cmd, void __user *argp,
+                               size_t membsz,
+                               ssize_t (*dofunc)(struct the_nilfs *,
+                                                 __u64 *, int,
+                                                 void *, size_t, size_t))
+
+{
+       struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
+       struct nilfs_argv argv;
+       int ret;
+
+       if (copy_from_user(&argv, argp, sizeof(argv)))
+               return -EFAULT;
+
+       if (argv.v_size < membsz)
+               return -EINVAL;
+
+       ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc);
+       if (ret < 0)
+               return ret;
+
+       if (copy_to_user(argp, &argv, sizeof(argv)))
+               ret = -EFAULT;
+       return ret;
+}
+
+long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
+       struct inode *inode = filp->f_dentry->d_inode;
        void __user *argp = (void * __user *)arg;
 
        switch (cmd) {
@@ -665,291 +626,28 @@ int nilfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
        case NILFS_IOCTL_DELETE_CHECKPOINT:
                return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp);
        case NILFS_IOCTL_GET_CPINFO:
-               return nilfs_ioctl_get_cpinfo(inode, filp, cmd, argp);
+               return nilfs_ioctl_get_info(inode, filp, cmd, argp,
+                                           sizeof(struct nilfs_cpinfo),
+                                           nilfs_ioctl_do_get_cpinfo);
        case NILFS_IOCTL_GET_CPSTAT:
                return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp);
        case NILFS_IOCTL_GET_SUINFO:
-               return nilfs_ioctl_get_suinfo(inode, filp, cmd, argp);
+               return nilfs_ioctl_get_info(inode, filp, cmd, argp,
+                                           sizeof(struct nilfs_suinfo),
+                                           nilfs_ioctl_do_get_suinfo);
        case NILFS_IOCTL_GET_SUSTAT:
                return nilfs_ioctl_get_sustat(inode, filp, cmd, argp);
        case NILFS_IOCTL_GET_VINFO:
-               /* XXX: rename to ??? */
-               return nilfs_ioctl_get_vinfo(inode, filp, cmd, argp);
+               return nilfs_ioctl_get_info(inode, filp, cmd, argp,
+                                           sizeof(struct nilfs_vinfo),
+                                           nilfs_ioctl_do_get_vinfo);
        case NILFS_IOCTL_GET_BDESCS:
                return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp);
        case NILFS_IOCTL_CLEAN_SEGMENTS:
                return nilfs_ioctl_clean_segments(inode, filp, cmd, argp);
-       case NILFS_IOCTL_TIMEDWAIT:
-               return nilfs_ioctl_timedwait(inode, filp, cmd, argp);
        case NILFS_IOCTL_SYNC:
                return nilfs_ioctl_sync(inode, filp, cmd, argp);
        default:
                return -ENOTTY;
        }
 }
-
-/* compat_ioctl */
-#ifdef CONFIG_COMPAT
-#include <linux/compat.h>
-
-static int nilfs_compat_locked_ioctl(struct inode *inode, struct file *filp,
-                                    unsigned int cmd, unsigned long arg)
-{
-       int ret;
-
-       lock_kernel();
-       ret = nilfs_ioctl(inode, filp, cmd, arg);
-       unlock_kernel();
-       return ret;
-}
-
-static int
-nilfs_compat_ioctl_uargv32_to_uargv(struct nilfs_argv32 __user *uargv32,
-                                   struct nilfs_argv __user *uargv)
-{
-       compat_uptr_t base;
-       compat_size_t nmembs, size;
-       compat_int_t index, flags;
-
-       if (get_user(base, &uargv32->v_base) ||
-           put_user(compat_ptr(base), &uargv->v_base) ||
-           get_user(nmembs, &uargv32->v_nmembs) ||
-           put_user(nmembs, &uargv->v_nmembs) ||
-           get_user(size, &uargv32->v_size) ||
-           put_user(size, &uargv->v_size) ||
-           get_user(index, &uargv32->v_index) ||
-           put_user(index, &uargv->v_index) ||
-           get_user(flags, &uargv32->v_flags) ||
-           put_user(flags, &uargv->v_flags))
-               return -EFAULT;
-       return 0;
-}
-
-static int
-nilfs_compat_ioctl_uargv_to_uargv32(struct nilfs_argv __user *uargv,
-                                   struct nilfs_argv32 __user *uargv32)
-{
-       size_t nmembs;
-
-       if (get_user(nmembs, &uargv->v_nmembs) ||
-           put_user(nmembs, &uargv32->v_nmembs))
-               return -EFAULT;
-       return 0;
-}
-
-static int
-nilfs_compat_ioctl_get_by_argv(struct inode *inode, struct file *filp,
-                              unsigned int cmd, unsigned long arg)
-{
-       struct nilfs_argv __user *uargv;
-       struct nilfs_argv32 __user *uargv32;
-       int ret;
-
-       uargv = compat_alloc_user_space(sizeof(struct nilfs_argv));
-       uargv32 = compat_ptr(arg);
-       ret = nilfs_compat_ioctl_uargv32_to_uargv(uargv32, uargv);
-       if (ret < 0)
-               return ret;
-
-       ret = nilfs_compat_locked_ioctl(inode, filp, cmd, (unsigned long)uargv);
-       if (ret < 0)
-               return ret;
-
-       return nilfs_compat_ioctl_uargv_to_uargv32(uargv, uargv32);
-}
-
-static int
-nilfs_compat_ioctl_change_cpmode(struct inode *inode, struct file *filp,
-                                unsigned int cmd, unsigned long arg)
-{
-       struct nilfs_cpmode __user *ucpmode;
-       struct nilfs_cpmode32 __user *ucpmode32;
-       int mode;
-
-       ucpmode = compat_alloc_user_space(sizeof(struct nilfs_cpmode));
-       ucpmode32 = compat_ptr(arg);
-       if (copy_in_user(&ucpmode->cm_cno, &ucpmode32->cm_cno,
-                        sizeof(__u64)) ||
-           get_user(mode, &ucpmode32->cm_mode) ||
-           put_user(mode, &ucpmode->cm_mode))
-               return -EFAULT;
-
-       return nilfs_compat_locked_ioctl(
-               inode, filp, cmd, (unsigned long)ucpmode);
-}
-
-
-static inline int
-nilfs_compat_ioctl_delete_checkpoint(struct inode *inode, struct file *filp,
-                                    unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
-}
-
-static inline int
-nilfs_compat_ioctl_get_cpinfo(struct inode *inode, struct file *filp,
-                             unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
-}
-
-static inline int
-nilfs_compat_ioctl_get_cpstat(struct inode *inode, struct file *filp,
-                             unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
-}
-
-static inline int
-nilfs_compat_ioctl_get_suinfo(struct inode *inode, struct file *filp,
-                             unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
-}
-
-static int
-nilfs_compat_ioctl_get_sustat(struct inode *inode, struct file *filp,
-                             unsigned int cmd, unsigned long arg)
-{
-       struct nilfs_sustat __user *usustat;
-       struct nilfs_sustat32 __user *usustat32;
-       time_t ctime, nongc_ctime;
-       int ret;
-
-       usustat = compat_alloc_user_space(sizeof(struct nilfs_sustat));
-       ret = nilfs_compat_locked_ioctl(inode, filp, cmd,
-                                       (unsigned long)usustat);
-       if (ret < 0)
-               return ret;
-
-       usustat32 = compat_ptr(arg);
-       if (copy_in_user(&usustat32->ss_nsegs, &usustat->ss_nsegs,
-                        sizeof(__u64)) ||
-           copy_in_user(&usustat32->ss_ncleansegs, &usustat->ss_ncleansegs,
-                        sizeof(__u64)) ||
-           copy_in_user(&usustat32->ss_ndirtysegs, &usustat->ss_ndirtysegs,
-                        sizeof(__u64)) ||
-           get_user(ctime, &usustat->ss_ctime) ||
-           put_user(ctime, &usustat32->ss_ctime) ||
-           get_user(nongc_ctime, &usustat->ss_nongc_ctime) ||
-           put_user(nongc_ctime, &usustat32->ss_nongc_ctime))
-               return -EFAULT;
-       return 0;
-}
-
-static inline int
-nilfs_compat_ioctl_get_vinfo(struct inode *inode, struct file *filp,
-                             unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
-}
-
-static inline int
-nilfs_compat_ioctl_get_bdescs(struct inode *inode, struct file *filp,
-                            unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_ioctl_get_by_argv(inode, filp, cmd, arg);
-}
-
-static int
-nilfs_compat_ioctl_clean_segments(struct inode *inode, struct file *filp,
-                                 unsigned int cmd, unsigned long arg)
-{
-       struct nilfs_argv __user *uargv;
-       struct nilfs_argv32 __user *uargv32;
-       int i, ret;
-
-       uargv = compat_alloc_user_space(sizeof(struct nilfs_argv) * 5);
-       uargv32 = compat_ptr(arg);
-       for (i = 0; i < 5; i++) {
-               ret = nilfs_compat_ioctl_uargv32_to_uargv(&uargv32[i],
-                                                         &uargv[i]);
-               if (ret < 0)
-                       return ret;
-       }
-       return nilfs_compat_locked_ioctl(
-               inode, filp, cmd, (unsigned long)uargv);
-}
-
-static int
-nilfs_compat_ioctl_timedwait(struct inode *inode, struct file *filp,
-                            unsigned int cmd, unsigned long arg)
-{
-       struct nilfs_wait_cond __user *uwcond;
-       struct nilfs_wait_cond32 __user *uwcond32;
-       struct timespec ts;
-       int cond, flags, ret;
-
-       uwcond = compat_alloc_user_space(sizeof(struct nilfs_wait_cond));
-       uwcond32 = compat_ptr(arg);
-       if (get_user(cond, &uwcond32->wc_cond) ||
-           put_user(cond, &uwcond->wc_cond) ||
-           get_user(flags, &uwcond32->wc_flags) ||
-           put_user(flags, &uwcond->wc_flags) ||
-           get_user(ts.tv_sec, &uwcond32->wc_timeout.tv_sec) ||
-           get_user(ts.tv_nsec, &uwcond32->wc_timeout.tv_nsec) ||
-           put_user(ts.tv_sec, &uwcond->wc_timeout.tv_sec) ||
-           put_user(ts.tv_nsec, &uwcond->wc_timeout.tv_nsec))
-               return -EFAULT;
-
-       ret = nilfs_compat_locked_ioctl(inode, filp, cmd,
-                                       (unsigned long)uwcond);
-       if (ret < 0)
-               return ret;
-
-       if (get_user(ts.tv_sec, &uwcond->wc_timeout.tv_sec) ||
-           get_user(ts.tv_nsec, &uwcond->wc_timeout.tv_nsec) ||
-           put_user(ts.tv_sec, &uwcond32->wc_timeout.tv_sec) ||
-           put_user(ts.tv_nsec, &uwcond32->wc_timeout.tv_nsec))
-               return -EFAULT;
-
-       return 0;
-}
-
-static int nilfs_compat_ioctl_sync(struct inode *inode, struct file *filp,
-                                  unsigned int cmd, unsigned long arg)
-{
-       return nilfs_compat_locked_ioctl(inode, filp, cmd, arg);
-}
-
-long nilfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
-{
-       struct inode *inode = filp->f_dentry->d_inode;
-
-       switch (cmd) {
-       case NILFS_IOCTL32_CHANGE_CPMODE:
-               return nilfs_compat_ioctl_change_cpmode(
-                       inode, filp, NILFS_IOCTL_CHANGE_CPMODE, arg);
-       case NILFS_IOCTL_DELETE_CHECKPOINT:
-               return nilfs_compat_ioctl_delete_checkpoint(
-                       inode, filp, cmd, arg);
-       case NILFS_IOCTL32_GET_CPINFO:
-               return nilfs_compat_ioctl_get_cpinfo(
-                       inode, filp, NILFS_IOCTL_GET_CPINFO, arg);
-       case NILFS_IOCTL_GET_CPSTAT:
-               return nilfs_compat_ioctl_get_cpstat(inode, filp, cmd, arg);
-       case NILFS_IOCTL32_GET_SUINFO:
-               return nilfs_compat_ioctl_get_suinfo(
-                       inode, filp, NILFS_IOCTL_GET_SUINFO, arg);
-       case NILFS_IOCTL32_GET_SUSTAT:
-               return nilfs_compat_ioctl_get_sustat(
-                       inode, filp, NILFS_IOCTL_GET_SUSTAT, arg);
-       case NILFS_IOCTL32_GET_VINFO:
-               return nilfs_compat_ioctl_get_vinfo(
-                       inode, filp, NILFS_IOCTL_GET_VINFO, arg);
-       case NILFS_IOCTL32_GET_BDESCS:
-               return nilfs_compat_ioctl_get_bdescs(
-                       inode, filp, NILFS_IOCTL_GET_BDESCS, arg);
-       case NILFS_IOCTL32_CLEAN_SEGMENTS:
-               return nilfs_compat_ioctl_clean_segments(
-                       inode, filp, NILFS_IOCTL_CLEAN_SEGMENTS, arg);
-       case NILFS_IOCTL32_TIMEDWAIT:
-               return nilfs_compat_ioctl_timedwait(
-                       inode, filp, NILFS_IOCTL_TIMEDWAIT, arg);
-       case NILFS_IOCTL_SYNC:
-               return nilfs_compat_ioctl_sync(inode, filp, cmd, arg);
-       default:
-               return -ENOIOCTLCMD;
-       }
-}
-#endif