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, |
| 155 | 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 | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 185 | ret = nilfs_sufile_get_suinfo(nilfs->ns_sufile, *posp, buf, nmembs); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 186 | up_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | static int nilfs_ioctl_get_sustat(struct inode *inode, struct file *filp, |
| 191 | unsigned int cmd, void __user *argp) |
| 192 | { |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 193 | struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 194 | struct nilfs_sustat sustat; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 195 | int ret; |
| 196 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 197 | down_read(&nilfs->ns_segctor_sem); |
| 198 | ret = nilfs_sufile_get_stat(nilfs->ns_sufile, &sustat); |
| 199 | up_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | |
| 203 | if (copy_to_user(argp, &sustat, sizeof(sustat))) |
| 204 | ret = -EFAULT; |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static ssize_t |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 209 | 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] | 210 | void *buf, size_t size, size_t nmembs) |
| 211 | { |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 212 | int ret; |
| 213 | |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 214 | down_read(&nilfs->ns_segctor_sem); |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 215 | ret = nilfs_dat_get_vinfo(nilfs_dat_inode(nilfs), buf, nmembs); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 216 | up_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static ssize_t |
Ryusuke Konishi | b028fcf | 2009-04-06 19:01:47 -0700 | [diff] [blame] | 221 | 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] | 222 | void *buf, size_t size, size_t nmembs) |
| 223 | { |
| 224 | struct inode *dat = nilfs_dat_inode(nilfs); |
| 225 | struct nilfs_bmap *bmap = NILFS_I(dat)->i_bmap; |
| 226 | struct nilfs_bdesc *bdescs = buf; |
| 227 | int ret, i; |
| 228 | |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 229 | down_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 230 | for (i = 0; i < nmembs; i++) { |
| 231 | ret = nilfs_bmap_lookup_at_level(bmap, |
| 232 | bdescs[i].bd_offset, |
| 233 | bdescs[i].bd_level + 1, |
| 234 | &bdescs[i].bd_blocknr); |
| 235 | if (ret < 0) { |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 236 | if (ret != -ENOENT) { |
| 237 | up_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 238 | return ret; |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 239 | } |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 240 | bdescs[i].bd_blocknr = 0; |
| 241 | } |
| 242 | } |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 243 | up_read(&nilfs->ns_segctor_sem); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 244 | return nmembs; |
| 245 | } |
| 246 | |
| 247 | static int nilfs_ioctl_get_bdescs(struct inode *inode, struct file *filp, |
| 248 | unsigned int cmd, void __user *argp) |
| 249 | { |
| 250 | struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs; |
| 251 | struct nilfs_argv argv; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 252 | int ret; |
| 253 | |
| 254 | if (copy_from_user(&argv, argp, sizeof(argv))) |
| 255 | return -EFAULT; |
| 256 | |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 257 | ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), |
| 258 | nilfs_ioctl_do_get_bdescs); |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 259 | if (ret < 0) |
| 260 | return ret; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 261 | |
| 262 | if (copy_to_user(argp, &argv, sizeof(argv))) |
| 263 | ret = -EFAULT; |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int nilfs_ioctl_move_inode_block(struct inode *inode, |
| 268 | struct nilfs_vdesc *vdesc, |
| 269 | struct list_head *buffers) |
| 270 | { |
| 271 | struct buffer_head *bh; |
| 272 | int ret; |
| 273 | |
| 274 | if (vdesc->vd_flags == 0) |
| 275 | ret = nilfs_gccache_submit_read_data( |
| 276 | inode, vdesc->vd_offset, vdesc->vd_blocknr, |
| 277 | vdesc->vd_vblocknr, &bh); |
| 278 | else |
| 279 | ret = nilfs_gccache_submit_read_node( |
| 280 | inode, vdesc->vd_blocknr, vdesc->vd_vblocknr, &bh); |
| 281 | |
| 282 | if (unlikely(ret < 0)) { |
| 283 | if (ret == -ENOENT) |
| 284 | printk(KERN_CRIT |
| 285 | "%s: invalid virtual block address (%s): " |
| 286 | "ino=%llu, cno=%llu, offset=%llu, " |
| 287 | "blocknr=%llu, vblocknr=%llu\n", |
| 288 | __func__, vdesc->vd_flags ? "node" : "data", |
| 289 | (unsigned long long)vdesc->vd_ino, |
| 290 | (unsigned long long)vdesc->vd_cno, |
| 291 | (unsigned long long)vdesc->vd_offset, |
| 292 | (unsigned long long)vdesc->vd_blocknr, |
| 293 | (unsigned long long)vdesc->vd_vblocknr); |
| 294 | return ret; |
| 295 | } |
| 296 | bh->b_private = vdesc; |
| 297 | list_add_tail(&bh->b_assoc_buffers, buffers); |
| 298 | return 0; |
| 299 | } |
| 300 | |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 301 | static int nilfs_ioctl_move_blocks(struct the_nilfs *nilfs, |
| 302 | struct nilfs_argv *argv, void *buf) |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 303 | { |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 304 | size_t nmembs = argv->v_nmembs; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 305 | struct inode *inode; |
| 306 | struct nilfs_vdesc *vdesc; |
| 307 | struct buffer_head *bh, *n; |
| 308 | LIST_HEAD(buffers); |
| 309 | ino_t ino; |
| 310 | __u64 cno; |
| 311 | int i, ret; |
| 312 | |
| 313 | for (i = 0, vdesc = buf; i < nmembs; ) { |
| 314 | ino = vdesc->vd_ino; |
| 315 | cno = vdesc->vd_cno; |
| 316 | inode = nilfs_gc_iget(nilfs, ino, cno); |
| 317 | if (unlikely(inode == NULL)) { |
| 318 | ret = -ENOMEM; |
| 319 | goto failed; |
| 320 | } |
| 321 | do { |
| 322 | ret = nilfs_ioctl_move_inode_block(inode, vdesc, |
| 323 | &buffers); |
| 324 | if (unlikely(ret < 0)) |
| 325 | goto failed; |
| 326 | vdesc++; |
| 327 | } while (++i < nmembs && |
| 328 | vdesc->vd_ino == ino && vdesc->vd_cno == cno); |
| 329 | } |
| 330 | |
| 331 | list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) { |
| 332 | ret = nilfs_gccache_wait_and_mark_dirty(bh); |
| 333 | if (unlikely(ret < 0)) { |
| 334 | if (ret == -EEXIST) { |
| 335 | vdesc = bh->b_private; |
| 336 | printk(KERN_CRIT |
| 337 | "%s: conflicting %s buffer: " |
| 338 | "ino=%llu, cno=%llu, offset=%llu, " |
| 339 | "blocknr=%llu, vblocknr=%llu\n", |
| 340 | __func__, |
| 341 | vdesc->vd_flags ? "node" : "data", |
| 342 | (unsigned long long)vdesc->vd_ino, |
| 343 | (unsigned long long)vdesc->vd_cno, |
| 344 | (unsigned long long)vdesc->vd_offset, |
| 345 | (unsigned long long)vdesc->vd_blocknr, |
| 346 | (unsigned long long)vdesc->vd_vblocknr); |
| 347 | } |
| 348 | goto failed; |
| 349 | } |
| 350 | list_del_init(&bh->b_assoc_buffers); |
| 351 | bh->b_private = NULL; |
| 352 | brelse(bh); |
| 353 | } |
| 354 | return nmembs; |
| 355 | |
| 356 | failed: |
| 357 | list_for_each_entry_safe(bh, n, &buffers, b_assoc_buffers) { |
| 358 | list_del_init(&bh->b_assoc_buffers); |
| 359 | bh->b_private = NULL; |
| 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 | |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 435 | static int nilfs_ioctl_free_segments(struct the_nilfs *nilfs, |
| 436 | struct nilfs_argv *argv, void *buf) |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 437 | { |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 438 | size_t nmembs = argv->v_nmembs; |
Ryusuke Konishi | 201913e | 2009-04-28 21:04:59 +0900 | [diff] [blame] | 439 | struct nilfs_sb_info *sbi = nilfs->ns_writer; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 440 | int ret; |
| 441 | |
Ryusuke Konishi | 201913e | 2009-04-28 21:04:59 +0900 | [diff] [blame] | 442 | if (unlikely(!sbi)) { |
| 443 | /* never happens because called for a writable mount */ |
| 444 | WARN_ON(1); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 445 | return -EROFS; |
Ryusuke Konishi | 201913e | 2009-04-28 21:04:59 +0900 | [diff] [blame] | 446 | } |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 447 | ret = nilfs_segctor_add_segments_to_be_freed( |
| 448 | NILFS_SC(sbi), buf, nmembs); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 449 | |
| 450 | return (ret < 0) ? ret : nmembs; |
| 451 | } |
| 452 | |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 453 | int nilfs_ioctl_prepare_clean_segments(struct the_nilfs *nilfs, |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 454 | struct nilfs_argv *argv, void **kbufs) |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 455 | { |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 456 | const char *msg; |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 457 | int ret; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 458 | |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 459 | ret = nilfs_ioctl_move_blocks(nilfs, &argv[0], kbufs[0]); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 460 | if (ret < 0) { |
| 461 | msg = "cannot read source blocks"; |
| 462 | goto failed; |
| 463 | } |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 464 | |
| 465 | ret = nilfs_ioctl_delete_checkpoints(nilfs, &argv[1], kbufs[1]); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 466 | if (ret < 0) { |
| 467 | /* |
| 468 | * can safely abort because checkpoints can be removed |
| 469 | * independently. |
| 470 | */ |
| 471 | msg = "cannot delete checkpoints"; |
| 472 | goto failed; |
| 473 | } |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 474 | ret = nilfs_ioctl_free_vblocknrs(nilfs, &argv[2], kbufs[2]); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 475 | if (ret < 0) { |
| 476 | /* |
| 477 | * can safely abort because DAT file is updated atomically |
| 478 | * using a copy-on-write technique. |
| 479 | */ |
| 480 | msg = "cannot delete virtual blocks from DAT file"; |
| 481 | goto failed; |
| 482 | } |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 483 | ret = nilfs_ioctl_mark_blocks_dirty(nilfs, &argv[3], kbufs[3]); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 484 | if (ret < 0) { |
| 485 | /* |
| 486 | * can safely abort because the operation is nondestructive. |
| 487 | */ |
| 488 | msg = "cannot mark copying blocks dirty"; |
| 489 | goto failed; |
| 490 | } |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 491 | ret = nilfs_ioctl_free_segments(nilfs, &argv[4], kbufs[4]); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 492 | if (ret < 0) { |
| 493 | /* |
| 494 | * can safely abort because this operation is atomic. |
| 495 | */ |
| 496 | msg = "cannot set segments to be freed"; |
| 497 | goto failed; |
| 498 | } |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 499 | return 0; |
| 500 | |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 501 | failed: |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 502 | nilfs_remove_all_gcinode(nilfs); |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 503 | printk(KERN_ERR "NILFS: GC failed during preparation: %s: err=%d\n", |
| 504 | msg, ret); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp, |
| 509 | unsigned int cmd, void __user *argp) |
| 510 | { |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 511 | struct nilfs_argv argv[5]; |
| 512 | const static size_t argsz[5] = { |
| 513 | sizeof(struct nilfs_vdesc), |
| 514 | sizeof(struct nilfs_period), |
| 515 | sizeof(__u64), |
| 516 | sizeof(struct nilfs_bdesc), |
| 517 | sizeof(__u64), |
| 518 | }; |
| 519 | void __user *base; |
| 520 | void *kbufs[5]; |
| 521 | struct the_nilfs *nilfs; |
| 522 | size_t len, nsegs; |
| 523 | int n, ret; |
| 524 | |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 525 | if (!capable(CAP_SYS_ADMIN)) |
| 526 | return -EPERM; |
Ryusuke Konishi | 4f6b828 | 2009-05-10 22:41:43 +0900 | [diff] [blame^] | 527 | |
| 528 | if (copy_from_user(argv, argp, sizeof(argv))) |
| 529 | return -EFAULT; |
| 530 | |
| 531 | nsegs = argv[4].v_nmembs; |
| 532 | if (argv[4].v_size != argsz[4]) |
| 533 | return -EINVAL; |
| 534 | /* |
| 535 | * argv[4] points to segment numbers this ioctl cleans. We |
| 536 | * use kmalloc() for its buffer because memory used for the |
| 537 | * segment numbers is enough small. |
| 538 | */ |
| 539 | kbufs[4] = memdup_user((void __user *)(unsigned long)argv[4].v_base, |
| 540 | nsegs * sizeof(__u64)); |
| 541 | if (IS_ERR(kbufs[4])) |
| 542 | return PTR_ERR(kbufs[4]); |
| 543 | |
| 544 | nilfs = NILFS_SB(inode->i_sb)->s_nilfs; |
| 545 | |
| 546 | for (n = 0; n < 4; n++) { |
| 547 | ret = -EINVAL; |
| 548 | if (argv[n].v_size != argsz[n]) |
| 549 | goto out_free; |
| 550 | |
| 551 | if (argv[n].v_nmembs > nsegs * nilfs->ns_blocks_per_segment) |
| 552 | goto out_free; |
| 553 | |
| 554 | len = argv[n].v_size * argv[n].v_nmembs; |
| 555 | base = (void __user *)(unsigned long)argv[n].v_base; |
| 556 | if (len == 0) { |
| 557 | kbufs[n] = NULL; |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | kbufs[n] = vmalloc(len); |
| 562 | if (!kbufs[n]) { |
| 563 | ret = -ENOMEM; |
| 564 | goto out_free; |
| 565 | } |
| 566 | if (copy_from_user(kbufs[n], base, len)) { |
| 567 | ret = -EFAULT; |
| 568 | vfree(kbufs[n]); |
| 569 | goto out_free; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | ret = nilfs_clean_segments(inode->i_sb, argv, kbufs); |
| 574 | |
| 575 | out_free: |
| 576 | while (--n > 0) |
| 577 | vfree(kbufs[n]); |
| 578 | kfree(kbufs[4]); |
| 579 | return ret; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static int nilfs_ioctl_sync(struct inode *inode, struct file *filp, |
| 583 | unsigned int cmd, void __user *argp) |
| 584 | { |
| 585 | __u64 cno; |
| 586 | int ret; |
| 587 | |
| 588 | ret = nilfs_construct_segment(inode->i_sb); |
| 589 | if (ret < 0) |
| 590 | return ret; |
| 591 | |
| 592 | if (argp != NULL) { |
| 593 | cno = NILFS_SB(inode->i_sb)->s_nilfs->ns_cno - 1; |
| 594 | if (copy_to_user(argp, &cno, sizeof(cno))) |
| 595 | return -EFAULT; |
| 596 | } |
| 597 | return 0; |
| 598 | } |
| 599 | |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 600 | static int nilfs_ioctl_get_info(struct inode *inode, struct file *filp, |
| 601 | unsigned int cmd, void __user *argp, |
| 602 | ssize_t (*dofunc)(struct the_nilfs *, |
| 603 | __u64 *, int, |
| 604 | void *, size_t, size_t)) |
| 605 | |
| 606 | { |
| 607 | struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs; |
| 608 | struct nilfs_argv argv; |
| 609 | int ret; |
| 610 | |
| 611 | if (copy_from_user(&argv, argp, sizeof(argv))) |
| 612 | return -EFAULT; |
| 613 | |
| 614 | ret = nilfs_ioctl_wrap_copy(nilfs, &argv, _IOC_DIR(cmd), dofunc); |
| 615 | if (ret < 0) |
| 616 | return ret; |
| 617 | |
| 618 | if (copy_to_user(argp, &argv, sizeof(argv))) |
| 619 | ret = -EFAULT; |
| 620 | return ret; |
| 621 | } |
| 622 | |
Ryusuke Konishi | 7a94619 | 2009-04-06 19:01:53 -0700 | [diff] [blame] | 623 | long nilfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 624 | { |
Ryusuke Konishi | 7a94619 | 2009-04-06 19:01:53 -0700 | [diff] [blame] | 625 | struct inode *inode = filp->f_dentry->d_inode; |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 626 | void __user *argp = (void * __user *)arg; |
| 627 | |
| 628 | switch (cmd) { |
| 629 | case NILFS_IOCTL_CHANGE_CPMODE: |
| 630 | return nilfs_ioctl_change_cpmode(inode, filp, cmd, argp); |
| 631 | case NILFS_IOCTL_DELETE_CHECKPOINT: |
| 632 | return nilfs_ioctl_delete_checkpoint(inode, filp, cmd, argp); |
| 633 | case NILFS_IOCTL_GET_CPINFO: |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 634 | return nilfs_ioctl_get_info(inode, filp, cmd, argp, |
| 635 | nilfs_ioctl_do_get_cpinfo); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 636 | case NILFS_IOCTL_GET_CPSTAT: |
| 637 | return nilfs_ioctl_get_cpstat(inode, filp, cmd, argp); |
| 638 | case NILFS_IOCTL_GET_SUINFO: |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 639 | return nilfs_ioctl_get_info(inode, filp, cmd, argp, |
| 640 | nilfs_ioctl_do_get_suinfo); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 641 | case NILFS_IOCTL_GET_SUSTAT: |
| 642 | return nilfs_ioctl_get_sustat(inode, filp, cmd, argp); |
| 643 | case NILFS_IOCTL_GET_VINFO: |
Ryusuke Konishi | 47eb6b9 | 2009-04-30 02:21:00 +0900 | [diff] [blame] | 644 | return nilfs_ioctl_get_info(inode, filp, cmd, argp, |
| 645 | nilfs_ioctl_do_get_vinfo); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 646 | case NILFS_IOCTL_GET_BDESCS: |
| 647 | return nilfs_ioctl_get_bdescs(inode, filp, cmd, argp); |
| 648 | case NILFS_IOCTL_CLEAN_SEGMENTS: |
| 649 | return nilfs_ioctl_clean_segments(inode, filp, cmd, argp); |
Koji Sato | 7942b91 | 2009-04-06 19:01:41 -0700 | [diff] [blame] | 650 | case NILFS_IOCTL_SYNC: |
| 651 | return nilfs_ioctl_sync(inode, filp, cmd, argp); |
| 652 | default: |
| 653 | return -ENOTTY; |
| 654 | } |
| 655 | } |