Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/fs.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/smp_lock.h> |
| 31 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 32 | #include <linux/mount.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 33 | #include <linux/mpage.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 34 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 35 | #include <linux/swap.h> |
| 36 | #include <linux/writeback.h> |
| 37 | #include <linux/statfs.h> |
| 38 | #include <linux/compat.h> |
| 39 | #include <linux/bit_spinlock.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 40 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 41 | #include <linux/version.h> |
| 42 | #include <linux/xattr.h> |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 43 | #include <linux/vmalloc.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 44 | #include "compat.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 45 | #include "ctree.h" |
| 46 | #include "disk-io.h" |
| 47 | #include "transaction.h" |
| 48 | #include "btrfs_inode.h" |
| 49 | #include "ioctl.h" |
| 50 | #include "print-tree.h" |
| 51 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 52 | #include "locking.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 53 | |
| 54 | |
| 55 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 56 | static noinline int create_subvol(struct btrfs_root *root, |
| 57 | struct dentry *dentry, |
| 58 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 59 | { |
| 60 | struct btrfs_trans_handle *trans; |
| 61 | struct btrfs_key key; |
| 62 | struct btrfs_root_item root_item; |
| 63 | struct btrfs_inode_item *inode_item; |
| 64 | struct extent_buffer *leaf; |
| 65 | struct btrfs_root *new_root = root; |
| 66 | struct inode *dir; |
| 67 | int ret; |
| 68 | int err; |
| 69 | u64 objectid; |
| 70 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 71 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 72 | unsigned long nr = 1; |
| 73 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 74 | ret = btrfs_check_free_space(root, 1, 0); |
| 75 | if (ret) |
| 76 | goto fail_commit; |
| 77 | |
| 78 | trans = btrfs_start_transaction(root, 1); |
| 79 | BUG_ON(!trans); |
| 80 | |
| 81 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 82 | 0, &objectid); |
| 83 | if (ret) |
| 84 | goto fail; |
| 85 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 86 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, |
| 87 | objectid, trans->transid, 0, 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 88 | if (IS_ERR(leaf)) { |
| 89 | ret = PTR_ERR(leaf); |
| 90 | goto fail; |
| 91 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 92 | |
| 93 | btrfs_set_header_nritems(leaf, 0); |
| 94 | btrfs_set_header_level(leaf, 0); |
| 95 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 96 | btrfs_set_header_generation(leaf, trans->transid); |
| 97 | btrfs_set_header_owner(leaf, objectid); |
| 98 | |
| 99 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 100 | (unsigned long)btrfs_header_fsid(leaf), |
| 101 | BTRFS_FSID_SIZE); |
| 102 | btrfs_mark_buffer_dirty(leaf); |
| 103 | |
| 104 | inode_item = &root_item.inode; |
| 105 | memset(inode_item, 0, sizeof(*inode_item)); |
| 106 | inode_item->generation = cpu_to_le64(1); |
| 107 | inode_item->size = cpu_to_le64(3); |
| 108 | inode_item->nlink = cpu_to_le32(1); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 109 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 110 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 111 | |
| 112 | btrfs_set_root_bytenr(&root_item, leaf->start); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 113 | btrfs_set_root_generation(&root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 114 | btrfs_set_root_level(&root_item, 0); |
| 115 | btrfs_set_root_refs(&root_item, 1); |
| 116 | btrfs_set_root_used(&root_item, 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 117 | btrfs_set_root_last_snapshot(&root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 118 | |
| 119 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 120 | root_item.drop_level = 0; |
| 121 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 122 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 123 | free_extent_buffer(leaf); |
| 124 | leaf = NULL; |
| 125 | |
| 126 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 127 | |
| 128 | key.objectid = objectid; |
| 129 | key.offset = 1; |
| 130 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 131 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 132 | &root_item); |
| 133 | if (ret) |
| 134 | goto fail; |
| 135 | |
| 136 | /* |
| 137 | * insert the directory item |
| 138 | */ |
| 139 | key.offset = (u64)-1; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 140 | dir = dentry->d_parent->d_inode; |
| 141 | ret = btrfs_set_inode_index(dir, &index); |
| 142 | BUG_ON(ret); |
| 143 | |
| 144 | ret = btrfs_insert_dir_item(trans, root, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 145 | name, namelen, dir->i_ino, &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 146 | BTRFS_FT_DIR, index); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 147 | if (ret) |
| 148 | goto fail; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 149 | |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 150 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
| 151 | ret = btrfs_update_inode(trans, root, dir); |
| 152 | BUG_ON(ret); |
| 153 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 154 | /* add the backref first */ |
| 155 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 156 | objectid, BTRFS_ROOT_BACKREF_KEY, |
| 157 | root->root_key.objectid, |
| 158 | dir->i_ino, index, name, namelen); |
| 159 | |
| 160 | BUG_ON(ret); |
| 161 | |
| 162 | /* now add the forward ref */ |
| 163 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
| 164 | root->root_key.objectid, BTRFS_ROOT_REF_KEY, |
| 165 | objectid, |
| 166 | dir->i_ino, index, name, namelen); |
| 167 | |
| 168 | BUG_ON(ret); |
| 169 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 170 | ret = btrfs_commit_transaction(trans, root); |
| 171 | if (ret) |
| 172 | goto fail_commit; |
| 173 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 174 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 175 | BUG_ON(!new_root); |
| 176 | |
| 177 | trans = btrfs_start_transaction(new_root, 1); |
| 178 | BUG_ON(!trans); |
| 179 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 180 | ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 181 | BTRFS_I(dir)->block_group); |
| 182 | if (ret) |
| 183 | goto fail; |
| 184 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 185 | fail: |
| 186 | nr = trans->blocks_used; |
| 187 | err = btrfs_commit_transaction(trans, new_root); |
| 188 | if (err && !ret) |
| 189 | ret = err; |
| 190 | fail_commit: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 191 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 192 | return ret; |
| 193 | } |
| 194 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 195 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
| 196 | char *name, int namelen) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 197 | { |
| 198 | struct btrfs_pending_snapshot *pending_snapshot; |
| 199 | struct btrfs_trans_handle *trans; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 200 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 201 | int err; |
| 202 | unsigned long nr = 0; |
| 203 | |
| 204 | if (!root->ref_cows) |
| 205 | return -EINVAL; |
| 206 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 207 | ret = btrfs_check_free_space(root, 1, 0); |
| 208 | if (ret) |
| 209 | goto fail_unlock; |
| 210 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 211 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 212 | if (!pending_snapshot) { |
| 213 | ret = -ENOMEM; |
| 214 | goto fail_unlock; |
| 215 | } |
| 216 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
| 217 | if (!pending_snapshot->name) { |
| 218 | ret = -ENOMEM; |
| 219 | kfree(pending_snapshot); |
| 220 | goto fail_unlock; |
| 221 | } |
| 222 | memcpy(pending_snapshot->name, name, namelen); |
| 223 | pending_snapshot->name[namelen] = '\0'; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 224 | pending_snapshot->dentry = dentry; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 225 | trans = btrfs_start_transaction(root, 1); |
| 226 | BUG_ON(!trans); |
| 227 | pending_snapshot->root = root; |
| 228 | list_add(&pending_snapshot->list, |
| 229 | &trans->transaction->pending_snapshots); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 230 | err = btrfs_commit_transaction(trans, root); |
| 231 | |
| 232 | fail_unlock: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 233 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 237 | /* copy of may_create in fs/namei.c() */ |
| 238 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
| 239 | { |
| 240 | if (child->d_inode) |
| 241 | return -EEXIST; |
| 242 | if (IS_DEADDIR(dir)) |
| 243 | return -ENOENT; |
| 244 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Create a new subvolume below @parent. This is largely modeled after |
| 249 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 250 | * inside this filesystem so it's quite a bit simpler. |
| 251 | */ |
| 252 | static noinline int btrfs_mksubvol(struct path *parent, char *name, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 253 | int mode, int namelen, |
| 254 | struct btrfs_root *snap_src) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 255 | { |
| 256 | struct dentry *dentry; |
| 257 | int error; |
| 258 | |
| 259 | mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 260 | |
| 261 | dentry = lookup_one_len(name, parent->dentry, namelen); |
| 262 | error = PTR_ERR(dentry); |
| 263 | if (IS_ERR(dentry)) |
| 264 | goto out_unlock; |
| 265 | |
| 266 | error = -EEXIST; |
| 267 | if (dentry->d_inode) |
| 268 | goto out_dput; |
| 269 | |
| 270 | if (!IS_POSIXACL(parent->dentry->d_inode)) |
| 271 | mode &= ~current->fs->umask; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 272 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 273 | error = mnt_want_write(parent->mnt); |
| 274 | if (error) |
| 275 | goto out_dput; |
| 276 | |
| 277 | error = btrfs_may_create(parent->dentry->d_inode, dentry); |
| 278 | if (error) |
| 279 | goto out_drop_write; |
| 280 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 281 | /* |
| 282 | * Actually perform the low-level subvolume creation after all |
| 283 | * this VFS fuzz. |
| 284 | * |
| 285 | * Eventually we want to pass in an inode under which we create this |
| 286 | * subvolume, but for now all are under the filesystem root. |
| 287 | * |
| 288 | * Also we should pass on the mode eventually to allow creating new |
| 289 | * subvolume with specific mode bits. |
| 290 | */ |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 291 | if (snap_src) { |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 292 | struct dentry *dir = dentry->d_parent; |
| 293 | struct dentry *test = dir->d_parent; |
| 294 | struct btrfs_path *path = btrfs_alloc_path(); |
| 295 | int ret; |
| 296 | u64 test_oid; |
| 297 | u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid; |
| 298 | |
| 299 | test_oid = snap_src->root_key.objectid; |
| 300 | |
| 301 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, |
| 302 | path, parent_oid, test_oid); |
| 303 | if (ret == 0) |
| 304 | goto create; |
| 305 | btrfs_release_path(snap_src->fs_info->tree_root, path); |
| 306 | |
| 307 | /* we need to make sure we aren't creating a directory loop |
| 308 | * by taking a snapshot of something that has our current |
| 309 | * subvol in its directory tree. So, this loops through |
| 310 | * the dentries and checks the forward refs for each subvolume |
| 311 | * to see if is references the subvolume where we are |
| 312 | * placing this new snapshot. |
| 313 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 314 | while (1) { |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 315 | if (!test || |
| 316 | dir == snap_src->fs_info->sb->s_root || |
| 317 | test == snap_src->fs_info->sb->s_root || |
| 318 | test->d_inode->i_sb != snap_src->fs_info->sb) { |
| 319 | break; |
| 320 | } |
| 321 | if (S_ISLNK(test->d_inode->i_mode)) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 322 | printk(KERN_INFO "Btrfs symlink in snapshot " |
| 323 | "path, failed\n"); |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 324 | error = -EMLINK; |
| 325 | btrfs_free_path(path); |
| 326 | goto out_drop_write; |
| 327 | } |
| 328 | test_oid = |
| 329 | BTRFS_I(test->d_inode)->root->root_key.objectid; |
| 330 | ret = btrfs_find_root_ref(snap_src->fs_info->tree_root, |
| 331 | path, test_oid, parent_oid); |
| 332 | if (ret == 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 333 | printk(KERN_INFO "Btrfs snapshot creation " |
| 334 | "failed, looping\n"); |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 335 | error = -EMLINK; |
| 336 | btrfs_free_path(path); |
| 337 | goto out_drop_write; |
| 338 | } |
| 339 | btrfs_release_path(snap_src->fs_info->tree_root, path); |
| 340 | test = test->d_parent; |
| 341 | } |
| 342 | create: |
| 343 | btrfs_free_path(path); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 344 | error = create_snapshot(snap_src, dentry, name, namelen); |
| 345 | } else { |
| 346 | error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, |
| 347 | dentry, name, namelen); |
| 348 | } |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 349 | if (error) |
| 350 | goto out_drop_write; |
| 351 | |
| 352 | fsnotify_mkdir(parent->dentry->d_inode, dentry); |
| 353 | out_drop_write: |
| 354 | mnt_drop_write(parent->mnt); |
| 355 | out_dput: |
| 356 | dput(dentry); |
| 357 | out_unlock: |
| 358 | mutex_unlock(&parent->dentry->d_inode->i_mutex); |
| 359 | return error; |
| 360 | } |
| 361 | |
| 362 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 363 | static int btrfs_defrag_file(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 364 | { |
| 365 | struct inode *inode = fdentry(file)->d_inode; |
| 366 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 367 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 368 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 369 | struct page *page; |
| 370 | unsigned long last_index; |
| 371 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 372 | unsigned long total_read = 0; |
| 373 | u64 page_start; |
| 374 | u64 page_end; |
| 375 | unsigned long i; |
| 376 | int ret; |
| 377 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 378 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 379 | if (ret) |
| 380 | return -ENOSPC; |
| 381 | |
| 382 | mutex_lock(&inode->i_mutex); |
| 383 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 384 | for (i = 0; i <= last_index; i++) { |
| 385 | if (total_read % ra_pages == 0) { |
| 386 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 387 | min(last_index, i + ra_pages - 1)); |
| 388 | } |
| 389 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 390 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 391 | page = grab_cache_page(inode->i_mapping, i); |
| 392 | if (!page) |
| 393 | goto out_unlock; |
| 394 | if (!PageUptodate(page)) { |
| 395 | btrfs_readpage(NULL, page); |
| 396 | lock_page(page); |
| 397 | if (!PageUptodate(page)) { |
| 398 | unlock_page(page); |
| 399 | page_cache_release(page); |
| 400 | goto out_unlock; |
| 401 | } |
| 402 | } |
| 403 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 404 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 405 | |
| 406 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 407 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 408 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 409 | |
| 410 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 411 | if (ordered) { |
| 412 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 413 | unlock_page(page); |
| 414 | page_cache_release(page); |
| 415 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 416 | btrfs_put_ordered_extent(ordered); |
| 417 | goto again; |
| 418 | } |
| 419 | set_page_extent_mapped(page); |
| 420 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 421 | /* |
| 422 | * this makes sure page_mkwrite is called on the |
| 423 | * page if it is dirtied again later |
| 424 | */ |
| 425 | clear_page_dirty_for_io(page); |
| 426 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 427 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 428 | |
| 429 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 430 | set_page_dirty(page); |
| 431 | unlock_page(page); |
| 432 | page_cache_release(page); |
| 433 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 434 | } |
| 435 | |
| 436 | out_unlock: |
| 437 | mutex_unlock(&inode->i_mutex); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Called inside transaction, so use GFP_NOFS |
| 443 | */ |
| 444 | |
| 445 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 446 | { |
| 447 | u64 new_size; |
| 448 | u64 old_size; |
| 449 | u64 devid = 1; |
| 450 | struct btrfs_ioctl_vol_args *vol_args; |
| 451 | struct btrfs_trans_handle *trans; |
| 452 | struct btrfs_device *device = NULL; |
| 453 | char *sizestr; |
| 454 | char *devstr = NULL; |
| 455 | int ret = 0; |
| 456 | int namelen; |
| 457 | int mod = 0; |
| 458 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 459 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 460 | return -EROFS; |
| 461 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 462 | if (!capable(CAP_SYS_ADMIN)) |
| 463 | return -EPERM; |
| 464 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 465 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 466 | |
| 467 | if (!vol_args) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 471 | ret = -EFAULT; |
| 472 | goto out; |
| 473 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 474 | |
| 475 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 476 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 477 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 478 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 479 | sizestr = vol_args->name; |
| 480 | devstr = strchr(sizestr, ':'); |
| 481 | if (devstr) { |
| 482 | char *end; |
| 483 | sizestr = devstr + 1; |
| 484 | *devstr = '\0'; |
| 485 | devstr = vol_args->name; |
| 486 | devid = simple_strtoull(devstr, &end, 10); |
| 487 | printk(KERN_INFO "resizing devid %llu\n", devid); |
| 488 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 489 | device = btrfs_find_device(root, devid, NULL, NULL); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 490 | if (!device) { |
| 491 | printk(KERN_INFO "resizer unable to find device %llu\n", devid); |
| 492 | ret = -EINVAL; |
| 493 | goto out_unlock; |
| 494 | } |
| 495 | if (!strcmp(sizestr, "max")) |
| 496 | new_size = device->bdev->bd_inode->i_size; |
| 497 | else { |
| 498 | if (sizestr[0] == '-') { |
| 499 | mod = -1; |
| 500 | sizestr++; |
| 501 | } else if (sizestr[0] == '+') { |
| 502 | mod = 1; |
| 503 | sizestr++; |
| 504 | } |
| 505 | new_size = btrfs_parse_size(sizestr); |
| 506 | if (new_size == 0) { |
| 507 | ret = -EINVAL; |
| 508 | goto out_unlock; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | old_size = device->total_bytes; |
| 513 | |
| 514 | if (mod < 0) { |
| 515 | if (new_size > old_size) { |
| 516 | ret = -EINVAL; |
| 517 | goto out_unlock; |
| 518 | } |
| 519 | new_size = old_size - new_size; |
| 520 | } else if (mod > 0) { |
| 521 | new_size = old_size + new_size; |
| 522 | } |
| 523 | |
| 524 | if (new_size < 256 * 1024 * 1024) { |
| 525 | ret = -EINVAL; |
| 526 | goto out_unlock; |
| 527 | } |
| 528 | if (new_size > device->bdev->bd_inode->i_size) { |
| 529 | ret = -EFBIG; |
| 530 | goto out_unlock; |
| 531 | } |
| 532 | |
| 533 | do_div(new_size, root->sectorsize); |
| 534 | new_size *= root->sectorsize; |
| 535 | |
| 536 | printk(KERN_INFO "new size for %s is %llu\n", |
| 537 | device->name, (unsigned long long)new_size); |
| 538 | |
| 539 | if (new_size > old_size) { |
| 540 | trans = btrfs_start_transaction(root, 1); |
| 541 | ret = btrfs_grow_device(trans, device, new_size); |
| 542 | btrfs_commit_transaction(trans, root); |
| 543 | } else { |
| 544 | ret = btrfs_shrink_device(device, new_size); |
| 545 | } |
| 546 | |
| 547 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 548 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 549 | out: |
| 550 | kfree(vol_args); |
| 551 | return ret; |
| 552 | } |
| 553 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 554 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 555 | void __user *arg, int subvol) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 556 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 557 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 558 | struct btrfs_ioctl_vol_args *vol_args; |
| 559 | struct btrfs_dir_item *di; |
| 560 | struct btrfs_path *path; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 561 | struct file *src_file; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 562 | u64 root_dirid; |
| 563 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 564 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 565 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 566 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 567 | return -EROFS; |
| 568 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 569 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 570 | |
| 571 | if (!vol_args) |
| 572 | return -ENOMEM; |
| 573 | |
| 574 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 575 | ret = -EFAULT; |
| 576 | goto out; |
| 577 | } |
| 578 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 579 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 580 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 581 | if (strchr(vol_args->name, '/')) { |
| 582 | ret = -EINVAL; |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | path = btrfs_alloc_path(); |
| 587 | if (!path) { |
| 588 | ret = -ENOMEM; |
| 589 | goto out; |
| 590 | } |
| 591 | |
| 592 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 593 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 594 | path, root_dirid, |
| 595 | vol_args->name, namelen, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 596 | btrfs_free_path(path); |
| 597 | |
| 598 | if (di && !IS_ERR(di)) { |
| 599 | ret = -EEXIST; |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | if (IS_ERR(di)) { |
| 604 | ret = PTR_ERR(di); |
| 605 | goto out; |
| 606 | } |
| 607 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 608 | if (subvol) { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 609 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
| 610 | file->f_path.dentry->d_inode->i_mode, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 611 | namelen, NULL); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 612 | } else { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 613 | struct inode *src_inode; |
| 614 | src_file = fget(vol_args->fd); |
| 615 | if (!src_file) { |
| 616 | ret = -EINVAL; |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | src_inode = src_file->f_path.dentry->d_inode; |
| 621 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 622 | printk(KERN_INFO "btrfs: Snapshot src from " |
| 623 | "another FS\n"); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 624 | ret = -EINVAL; |
| 625 | fput(src_file); |
| 626 | goto out; |
| 627 | } |
| 628 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
| 629 | file->f_path.dentry->d_inode->i_mode, |
| 630 | namelen, BTRFS_I(src_inode)->root); |
| 631 | fput(src_file); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 632 | } |
| 633 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 634 | out: |
| 635 | kfree(vol_args); |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static int btrfs_ioctl_defrag(struct file *file) |
| 640 | { |
| 641 | struct inode *inode = fdentry(file)->d_inode; |
| 642 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 643 | int ret; |
| 644 | |
| 645 | ret = mnt_want_write(file->f_path.mnt); |
| 646 | if (ret) |
| 647 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 648 | |
| 649 | switch (inode->i_mode & S_IFMT) { |
| 650 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 651 | if (!capable(CAP_SYS_ADMIN)) { |
| 652 | ret = -EPERM; |
| 653 | goto out; |
| 654 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 655 | btrfs_defrag_root(root, 0); |
| 656 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 657 | break; |
| 658 | case S_IFREG: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 659 | if (!(file->f_mode & FMODE_WRITE)) { |
| 660 | ret = -EINVAL; |
| 661 | goto out; |
| 662 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 663 | btrfs_defrag_file(file); |
| 664 | break; |
| 665 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 666 | out: |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 667 | mnt_drop_write(file->f_path.mnt); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 668 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 669 | } |
| 670 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 671 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 672 | { |
| 673 | struct btrfs_ioctl_vol_args *vol_args; |
| 674 | int ret; |
| 675 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 676 | if (!capable(CAP_SYS_ADMIN)) |
| 677 | return -EPERM; |
| 678 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 679 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 680 | |
| 681 | if (!vol_args) |
| 682 | return -ENOMEM; |
| 683 | |
| 684 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 685 | ret = -EFAULT; |
| 686 | goto out; |
| 687 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 688 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 689 | ret = btrfs_init_new_device(root, vol_args->name); |
| 690 | |
| 691 | out: |
| 692 | kfree(vol_args); |
| 693 | return ret; |
| 694 | } |
| 695 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 696 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 697 | { |
| 698 | struct btrfs_ioctl_vol_args *vol_args; |
| 699 | int ret; |
| 700 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 701 | if (!capable(CAP_SYS_ADMIN)) |
| 702 | return -EPERM; |
| 703 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 704 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
| 705 | return -EROFS; |
| 706 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 707 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 708 | |
| 709 | if (!vol_args) |
| 710 | return -ENOMEM; |
| 711 | |
| 712 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 713 | ret = -EFAULT; |
| 714 | goto out; |
| 715 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 716 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 717 | ret = btrfs_rm_device(root, vol_args->name); |
| 718 | |
| 719 | out: |
| 720 | kfree(vol_args); |
| 721 | return ret; |
| 722 | } |
| 723 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 724 | static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
| 725 | u64 off, u64 olen, u64 destoff) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 726 | { |
| 727 | struct inode *inode = fdentry(file)->d_inode; |
| 728 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 729 | struct file *src_file; |
| 730 | struct inode *src; |
| 731 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 732 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 733 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 734 | char *buf; |
| 735 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 736 | u32 nritems; |
| 737 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 738 | int ret; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 739 | u64 len = olen; |
| 740 | u64 bs = root->fs_info->sb->s_blocksize; |
| 741 | u64 hint_byte; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 742 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 743 | /* |
| 744 | * TODO: |
| 745 | * - split compressed inline extents. annoying: we need to |
| 746 | * decompress into destination's address_space (the file offset |
| 747 | * may change, so source mapping won't do), then recompress (or |
| 748 | * otherwise reinsert) a subrange. |
| 749 | * - allow ranges within the same file to be cloned (provided |
| 750 | * they don't overlap)? |
| 751 | */ |
| 752 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 753 | /* the destination must be opened for writing */ |
| 754 | if (!(file->f_mode & FMODE_WRITE)) |
| 755 | return -EINVAL; |
| 756 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 757 | ret = mnt_want_write(file->f_path.mnt); |
| 758 | if (ret) |
| 759 | return ret; |
| 760 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 761 | src_file = fget(srcfd); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 762 | if (!src_file) { |
| 763 | ret = -EBADF; |
| 764 | goto out_drop_write; |
| 765 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 766 | src = src_file->f_dentry->d_inode; |
| 767 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 768 | ret = -EINVAL; |
| 769 | if (src == inode) |
| 770 | goto out_fput; |
| 771 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 772 | ret = -EISDIR; |
| 773 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 774 | goto out_fput; |
| 775 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 776 | ret = -EXDEV; |
| 777 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 778 | goto out_fput; |
| 779 | |
| 780 | ret = -ENOMEM; |
| 781 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 782 | if (!buf) |
| 783 | goto out_fput; |
| 784 | |
| 785 | path = btrfs_alloc_path(); |
| 786 | if (!path) { |
| 787 | vfree(buf); |
| 788 | goto out_fput; |
| 789 | } |
| 790 | path->reada = 2; |
| 791 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 792 | if (inode < src) { |
| 793 | mutex_lock(&inode->i_mutex); |
| 794 | mutex_lock(&src->i_mutex); |
| 795 | } else { |
| 796 | mutex_lock(&src->i_mutex); |
| 797 | mutex_lock(&inode->i_mutex); |
| 798 | } |
| 799 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 800 | /* determine range to clone */ |
| 801 | ret = -EINVAL; |
| 802 | if (off >= src->i_size || off + len > src->i_size) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 803 | goto out_unlock; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 804 | if (len == 0) |
| 805 | olen = len = src->i_size - off; |
| 806 | /* if we extend to eof, continue to block boundary */ |
| 807 | if (off + len == src->i_size) |
| 808 | len = ((src->i_size + bs-1) & ~(bs-1)) |
| 809 | - off; |
| 810 | |
| 811 | /* verify the end result is block aligned */ |
| 812 | if ((off & (bs-1)) || |
| 813 | ((off + len) & (bs-1))) |
| 814 | goto out_unlock; |
| 815 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 816 | /* do any pending delalloc/csum calc on src, one way or |
| 817 | another, and lock file content */ |
| 818 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 819 | struct btrfs_ordered_extent *ordered; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 820 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
| 821 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 822 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 823 | break; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 824 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 825 | if (ordered) |
| 826 | btrfs_put_ordered_extent(ordered); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 827 | btrfs_wait_ordered_range(src, off, off+len); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 830 | trans = btrfs_start_transaction(root, 1); |
| 831 | BUG_ON(!trans); |
| 832 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 833 | /* punch hole in destination first */ |
| 834 | btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte); |
| 835 | |
| 836 | /* clone data */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 837 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 838 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 839 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 840 | |
| 841 | while (1) { |
| 842 | /* |
| 843 | * note the key will change type as we walk through the |
| 844 | * tree. |
| 845 | */ |
| 846 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 847 | if (ret < 0) |
| 848 | goto out; |
| 849 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 850 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 851 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 852 | ret = btrfs_next_leaf(root, path); |
| 853 | if (ret < 0) |
| 854 | goto out; |
| 855 | if (ret > 0) |
| 856 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 857 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 858 | } |
| 859 | leaf = path->nodes[0]; |
| 860 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 861 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 862 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 863 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 864 | key.objectid != src->i_ino) |
| 865 | break; |
| 866 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 867 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 868 | struct btrfs_file_extent_item *extent; |
| 869 | int type; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 870 | u32 size; |
| 871 | struct btrfs_key new_key; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 872 | u64 disko = 0, diskl = 0; |
| 873 | u64 datao = 0, datal = 0; |
| 874 | u8 comp; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 875 | |
| 876 | size = btrfs_item_size_nr(leaf, slot); |
| 877 | read_extent_buffer(leaf, buf, |
| 878 | btrfs_item_ptr_offset(leaf, slot), |
| 879 | size); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 880 | |
| 881 | extent = btrfs_item_ptr(leaf, slot, |
| 882 | struct btrfs_file_extent_item); |
| 883 | comp = btrfs_file_extent_compression(leaf, extent); |
| 884 | type = btrfs_file_extent_type(leaf, extent); |
| 885 | if (type == BTRFS_FILE_EXTENT_REG) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 886 | disko = btrfs_file_extent_disk_bytenr(leaf, |
| 887 | extent); |
| 888 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
| 889 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 890 | datao = btrfs_file_extent_offset(leaf, extent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 891 | datal = btrfs_file_extent_num_bytes(leaf, |
| 892 | extent); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 893 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 894 | /* take upper bound, may be compressed */ |
| 895 | datal = btrfs_file_extent_ram_bytes(leaf, |
| 896 | extent); |
| 897 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 898 | btrfs_release_path(root, path); |
| 899 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 900 | if (key.offset + datal < off || |
| 901 | key.offset >= off+len) |
| 902 | goto next; |
| 903 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 904 | memcpy(&new_key, &key, sizeof(new_key)); |
| 905 | new_key.objectid = inode->i_ino; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 906 | new_key.offset = key.offset + destoff - off; |
| 907 | |
| 908 | if (type == BTRFS_FILE_EXTENT_REG) { |
| 909 | ret = btrfs_insert_empty_item(trans, root, path, |
| 910 | &new_key, size); |
| 911 | if (ret) |
| 912 | goto out; |
| 913 | |
| 914 | leaf = path->nodes[0]; |
| 915 | slot = path->slots[0]; |
| 916 | write_extent_buffer(leaf, buf, |
| 917 | btrfs_item_ptr_offset(leaf, slot), |
| 918 | size); |
| 919 | |
| 920 | extent = btrfs_item_ptr(leaf, slot, |
| 921 | struct btrfs_file_extent_item); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 922 | |
| 923 | if (off > key.offset) { |
| 924 | datao += off - key.offset; |
| 925 | datal -= off - key.offset; |
| 926 | } |
| 927 | if (key.offset + datao + datal + key.offset > |
| 928 | off + len) |
| 929 | datal = off + len - key.offset - datao; |
| 930 | /* disko == 0 means it's a hole */ |
| 931 | if (!disko) |
| 932 | datao = 0; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 933 | |
| 934 | btrfs_set_file_extent_offset(leaf, extent, |
| 935 | datao); |
| 936 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 937 | datal); |
| 938 | if (disko) { |
| 939 | inode_add_bytes(inode, datal); |
| 940 | ret = btrfs_inc_extent_ref(trans, root, |
| 941 | disko, diskl, leaf->start, |
| 942 | root->root_key.objectid, |
| 943 | trans->transid, |
| 944 | inode->i_ino); |
| 945 | BUG_ON(ret); |
| 946 | } |
| 947 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 948 | u64 skip = 0; |
| 949 | u64 trim = 0; |
| 950 | if (off > key.offset) { |
| 951 | skip = off - key.offset; |
| 952 | new_key.offset += skip; |
| 953 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 954 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 955 | if (key.offset + datal > off+len) |
| 956 | trim = key.offset + datal - (off+len); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 957 | |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 958 | if (comp && (skip || trim)) { |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 959 | ret = -EINVAL; |
| 960 | goto out; |
| 961 | } |
| 962 | size -= skip + trim; |
| 963 | datal -= skip + trim; |
| 964 | ret = btrfs_insert_empty_item(trans, root, path, |
| 965 | &new_key, size); |
| 966 | if (ret) |
| 967 | goto out; |
| 968 | |
| 969 | if (skip) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 970 | u32 start = |
| 971 | btrfs_file_extent_calc_inline_size(0); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 972 | memmove(buf+start, buf+start+skip, |
| 973 | datal); |
| 974 | } |
| 975 | |
| 976 | leaf = path->nodes[0]; |
| 977 | slot = path->slots[0]; |
| 978 | write_extent_buffer(leaf, buf, |
| 979 | btrfs_item_ptr_offset(leaf, slot), |
| 980 | size); |
| 981 | inode_add_bytes(inode, datal); |
| 982 | } |
| 983 | |
| 984 | btrfs_mark_buffer_dirty(leaf); |
| 985 | } |
| 986 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame^] | 987 | next: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 988 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 989 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 990 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 991 | ret = 0; |
| 992 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 993 | btrfs_release_path(root, path); |
| 994 | if (ret == 0) { |
| 995 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 996 | if (destoff + olen > inode->i_size) |
| 997 | btrfs_i_size_write(inode, destoff + olen); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 998 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 999 | ret = btrfs_update_inode(trans, root, inode); |
| 1000 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1001 | btrfs_end_transaction(trans, root); |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1002 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1003 | if (ret) |
| 1004 | vmtruncate(inode, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1005 | out_unlock: |
| 1006 | mutex_unlock(&src->i_mutex); |
| 1007 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 1008 | vfree(buf); |
| 1009 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1010 | out_fput: |
| 1011 | fput(src_file); |
Yan Zheng | ab67b7c | 2008-12-19 10:58:39 -0500 | [diff] [blame] | 1012 | out_drop_write: |
| 1013 | mnt_drop_write(file->f_path.mnt); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1014 | return ret; |
| 1015 | } |
| 1016 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1017 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1018 | { |
| 1019 | struct btrfs_ioctl_clone_range_args args; |
| 1020 | |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1021 | if (copy_from_user(&args, argp, sizeof(args))) |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1022 | return -EFAULT; |
| 1023 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
| 1024 | args.src_length, args.dest_offset); |
| 1025 | } |
| 1026 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1027 | /* |
| 1028 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1029 | * to deadlocks. They should only be used by applications that |
| 1030 | * basically own the machine, and have a very in depth understanding |
| 1031 | * of all the possible deadlocks and enospc problems. |
| 1032 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1033 | static long btrfs_ioctl_trans_start(struct file *file) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1034 | { |
| 1035 | struct inode *inode = fdentry(file)->d_inode; |
| 1036 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1037 | struct btrfs_trans_handle *trans; |
| 1038 | int ret = 0; |
| 1039 | |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 1040 | if (!capable(CAP_SYS_ADMIN)) |
| 1041 | return -EPERM; |
| 1042 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1043 | if (file->private_data) { |
| 1044 | ret = -EINPROGRESS; |
| 1045 | goto out; |
| 1046 | } |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1047 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1048 | ret = mnt_want_write(file->f_path.mnt); |
| 1049 | if (ret) |
| 1050 | goto out; |
| 1051 | |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1052 | mutex_lock(&root->fs_info->trans_mutex); |
| 1053 | root->fs_info->open_ioctl_trans++; |
| 1054 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1055 | |
| 1056 | trans = btrfs_start_ioctl_transaction(root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1057 | if (trans) |
| 1058 | file->private_data = trans; |
| 1059 | else |
| 1060 | ret = -ENOMEM; |
| 1061 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ |
| 1062 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * there are many ways the trans_start and trans_end ioctls can lead |
| 1068 | * to deadlocks. They should only be used by applications that |
| 1069 | * basically own the machine, and have a very in depth understanding |
| 1070 | * of all the possible deadlocks and enospc problems. |
| 1071 | */ |
| 1072 | long btrfs_ioctl_trans_end(struct file *file) |
| 1073 | { |
| 1074 | struct inode *inode = fdentry(file)->d_inode; |
| 1075 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1076 | struct btrfs_trans_handle *trans; |
| 1077 | int ret = 0; |
| 1078 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1079 | trans = file->private_data; |
| 1080 | if (!trans) { |
| 1081 | ret = -EINVAL; |
| 1082 | goto out; |
| 1083 | } |
| 1084 | btrfs_end_transaction(trans, root); |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 1085 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 1086 | |
| 1087 | mutex_lock(&root->fs_info->trans_mutex); |
| 1088 | root->fs_info->open_ioctl_trans--; |
| 1089 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1090 | |
Sage Weil | cfc8ea8 | 2008-12-11 16:30:06 -0500 | [diff] [blame] | 1091 | mnt_drop_write(file->f_path.mnt); |
| 1092 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1093 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | long btrfs_ioctl(struct file *file, unsigned int |
| 1098 | cmd, unsigned long arg) |
| 1099 | { |
| 1100 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1101 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1102 | |
| 1103 | switch (cmd) { |
| 1104 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1105 | return btrfs_ioctl_snap_create(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1106 | case BTRFS_IOC_SUBVOL_CREATE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1107 | return btrfs_ioctl_snap_create(file, argp, 1); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1108 | case BTRFS_IOC_DEFRAG: |
| 1109 | return btrfs_ioctl_defrag(file); |
| 1110 | case BTRFS_IOC_RESIZE: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1111 | return btrfs_ioctl_resize(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1112 | case BTRFS_IOC_ADD_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1113 | return btrfs_ioctl_add_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1114 | case BTRFS_IOC_RM_DEV: |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 1115 | return btrfs_ioctl_rm_dev(root, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1116 | case BTRFS_IOC_BALANCE: |
| 1117 | return btrfs_balance(root->fs_info->dev_root); |
| 1118 | case BTRFS_IOC_CLONE: |
Sage Weil | c5c9cd4 | 2008-11-12 14:32:25 -0500 | [diff] [blame] | 1119 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
| 1120 | case BTRFS_IOC_CLONE_RANGE: |
Christoph Hellwig | 7a865e8 | 2008-12-02 09:52:24 -0500 | [diff] [blame] | 1121 | return btrfs_ioctl_clone_range(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1122 | case BTRFS_IOC_TRANS_START: |
| 1123 | return btrfs_ioctl_trans_start(file); |
| 1124 | case BTRFS_IOC_TRANS_END: |
| 1125 | return btrfs_ioctl_trans_end(file); |
| 1126 | case BTRFS_IOC_SYNC: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1127 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | return -ENOTTY; |
| 1132 | } |