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