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