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