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