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