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> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/smp_lock.h> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/mpage.h> |
| 32 | #include <linux/swap.h> |
| 33 | #include <linux/writeback.h> |
| 34 | #include <linux/statfs.h> |
| 35 | #include <linux/compat.h> |
| 36 | #include <linux/bit_spinlock.h> |
| 37 | #include <linux/version.h> |
| 38 | #include <linux/xattr.h> |
| 39 | #include "ctree.h" |
| 40 | #include "disk-io.h" |
| 41 | #include "transaction.h" |
| 42 | #include "btrfs_inode.h" |
| 43 | #include "ioctl.h" |
| 44 | #include "print-tree.h" |
| 45 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 46 | #include "locking.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 47 | |
| 48 | |
| 49 | |
| 50 | static noinline int create_subvol(struct btrfs_root *root, char *name, |
| 51 | int namelen) |
| 52 | { |
| 53 | struct btrfs_trans_handle *trans; |
| 54 | struct btrfs_key key; |
| 55 | struct btrfs_root_item root_item; |
| 56 | struct btrfs_inode_item *inode_item; |
| 57 | struct extent_buffer *leaf; |
| 58 | struct btrfs_root *new_root = root; |
| 59 | struct inode *dir; |
| 60 | int ret; |
| 61 | int err; |
| 62 | u64 objectid; |
| 63 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 64 | unsigned long nr = 1; |
| 65 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 66 | ret = btrfs_check_free_space(root, 1, 0); |
| 67 | if (ret) |
| 68 | goto fail_commit; |
| 69 | |
| 70 | trans = btrfs_start_transaction(root, 1); |
| 71 | BUG_ON(!trans); |
| 72 | |
| 73 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 74 | 0, &objectid); |
| 75 | if (ret) |
| 76 | goto fail; |
| 77 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 78 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
| 79 | objectid, trans->transid, 0, 0, |
| 80 | 0, 0); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 81 | if (IS_ERR(leaf)) { |
| 82 | ret = PTR_ERR(leaf); |
| 83 | goto fail; |
| 84 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 85 | |
| 86 | btrfs_set_header_nritems(leaf, 0); |
| 87 | btrfs_set_header_level(leaf, 0); |
| 88 | btrfs_set_header_bytenr(leaf, leaf->start); |
| 89 | btrfs_set_header_generation(leaf, trans->transid); |
| 90 | btrfs_set_header_owner(leaf, objectid); |
| 91 | |
| 92 | write_extent_buffer(leaf, root->fs_info->fsid, |
| 93 | (unsigned long)btrfs_header_fsid(leaf), |
| 94 | BTRFS_FSID_SIZE); |
| 95 | btrfs_mark_buffer_dirty(leaf); |
| 96 | |
| 97 | inode_item = &root_item.inode; |
| 98 | memset(inode_item, 0, sizeof(*inode_item)); |
| 99 | inode_item->generation = cpu_to_le64(1); |
| 100 | inode_item->size = cpu_to_le64(3); |
| 101 | inode_item->nlink = cpu_to_le32(1); |
| 102 | inode_item->nblocks = cpu_to_le64(1); |
| 103 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
| 104 | |
| 105 | btrfs_set_root_bytenr(&root_item, leaf->start); |
| 106 | btrfs_set_root_level(&root_item, 0); |
| 107 | btrfs_set_root_refs(&root_item, 1); |
| 108 | btrfs_set_root_used(&root_item, 0); |
| 109 | |
| 110 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 111 | root_item.drop_level = 0; |
| 112 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 113 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 114 | free_extent_buffer(leaf); |
| 115 | leaf = NULL; |
| 116 | |
| 117 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 118 | |
| 119 | key.objectid = objectid; |
| 120 | key.offset = 1; |
| 121 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 122 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 123 | &root_item); |
| 124 | if (ret) |
| 125 | goto fail; |
| 126 | |
| 127 | /* |
| 128 | * insert the directory item |
| 129 | */ |
| 130 | key.offset = (u64)-1; |
| 131 | dir = root->fs_info->sb->s_root->d_inode; |
| 132 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 133 | name, namelen, dir->i_ino, &key, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 134 | BTRFS_FT_DIR, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 135 | if (ret) |
| 136 | goto fail; |
| 137 | |
| 138 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
| 139 | name, namelen, objectid, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 140 | root->fs_info->sb->s_root->d_inode->i_ino, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 141 | if (ret) |
| 142 | goto fail; |
| 143 | |
| 144 | ret = btrfs_commit_transaction(trans, root); |
| 145 | if (ret) |
| 146 | goto fail_commit; |
| 147 | |
| 148 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); |
| 149 | BUG_ON(!new_root); |
| 150 | |
| 151 | trans = btrfs_start_transaction(new_root, 1); |
| 152 | BUG_ON(!trans); |
| 153 | |
| 154 | ret = btrfs_create_subvol_root(new_root, trans, new_dirid, |
| 155 | BTRFS_I(dir)->block_group); |
| 156 | if (ret) |
| 157 | goto fail; |
| 158 | |
| 159 | /* Invalidate existing dcache entry for new subvolume. */ |
| 160 | btrfs_invalidate_dcache_root(root, name, namelen); |
| 161 | |
| 162 | fail: |
| 163 | nr = trans->blocks_used; |
| 164 | err = btrfs_commit_transaction(trans, new_root); |
| 165 | if (err && !ret) |
| 166 | ret = err; |
| 167 | fail_commit: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 168 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 173 | { |
| 174 | struct btrfs_pending_snapshot *pending_snapshot; |
| 175 | struct btrfs_trans_handle *trans; |
| 176 | int ret; |
| 177 | int err; |
| 178 | unsigned long nr = 0; |
| 179 | |
| 180 | if (!root->ref_cows) |
| 181 | return -EINVAL; |
| 182 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 183 | ret = btrfs_check_free_space(root, 1, 0); |
| 184 | if (ret) |
| 185 | goto fail_unlock; |
| 186 | |
| 187 | pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS); |
| 188 | if (!pending_snapshot) { |
| 189 | ret = -ENOMEM; |
| 190 | goto fail_unlock; |
| 191 | } |
| 192 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
| 193 | if (!pending_snapshot->name) { |
| 194 | ret = -ENOMEM; |
| 195 | kfree(pending_snapshot); |
| 196 | goto fail_unlock; |
| 197 | } |
| 198 | memcpy(pending_snapshot->name, name, namelen); |
| 199 | pending_snapshot->name[namelen] = '\0'; |
| 200 | trans = btrfs_start_transaction(root, 1); |
| 201 | BUG_ON(!trans); |
| 202 | pending_snapshot->root = root; |
| 203 | list_add(&pending_snapshot->list, |
| 204 | &trans->transaction->pending_snapshots); |
| 205 | ret = btrfs_update_inode(trans, root, root->inode); |
| 206 | err = btrfs_commit_transaction(trans, root); |
| 207 | |
| 208 | fail_unlock: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 209 | btrfs_btree_balance_dirty(root, nr); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | int btrfs_defrag_file(struct file *file) |
| 214 | { |
| 215 | struct inode *inode = fdentry(file)->d_inode; |
| 216 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 217 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 218 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 219 | struct page *page; |
| 220 | unsigned long last_index; |
| 221 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 222 | unsigned long total_read = 0; |
| 223 | u64 page_start; |
| 224 | u64 page_end; |
| 225 | unsigned long i; |
| 226 | int ret; |
| 227 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 228 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 229 | if (ret) |
| 230 | return -ENOSPC; |
| 231 | |
| 232 | mutex_lock(&inode->i_mutex); |
| 233 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 234 | for (i = 0; i <= last_index; i++) { |
| 235 | if (total_read % ra_pages == 0) { |
| 236 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 237 | min(last_index, i + ra_pages - 1)); |
| 238 | } |
| 239 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 240 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 241 | page = grab_cache_page(inode->i_mapping, i); |
| 242 | if (!page) |
| 243 | goto out_unlock; |
| 244 | if (!PageUptodate(page)) { |
| 245 | btrfs_readpage(NULL, page); |
| 246 | lock_page(page); |
| 247 | if (!PageUptodate(page)) { |
| 248 | unlock_page(page); |
| 249 | page_cache_release(page); |
| 250 | goto out_unlock; |
| 251 | } |
| 252 | } |
| 253 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 254 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 255 | |
| 256 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 257 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 258 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 259 | |
| 260 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 261 | if (ordered) { |
| 262 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 263 | unlock_page(page); |
| 264 | page_cache_release(page); |
| 265 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 266 | btrfs_put_ordered_extent(ordered); |
| 267 | goto again; |
| 268 | } |
| 269 | set_page_extent_mapped(page); |
| 270 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 271 | /* |
| 272 | * this makes sure page_mkwrite is called on the |
| 273 | * page if it is dirtied again later |
| 274 | */ |
| 275 | clear_page_dirty_for_io(page); |
| 276 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame^] | 277 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 278 | |
| 279 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 280 | set_page_dirty(page); |
| 281 | unlock_page(page); |
| 282 | page_cache_release(page); |
| 283 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 284 | } |
| 285 | |
| 286 | out_unlock: |
| 287 | mutex_unlock(&inode->i_mutex); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Called inside transaction, so use GFP_NOFS |
| 293 | */ |
| 294 | |
| 295 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 296 | { |
| 297 | u64 new_size; |
| 298 | u64 old_size; |
| 299 | u64 devid = 1; |
| 300 | struct btrfs_ioctl_vol_args *vol_args; |
| 301 | struct btrfs_trans_handle *trans; |
| 302 | struct btrfs_device *device = NULL; |
| 303 | char *sizestr; |
| 304 | char *devstr = NULL; |
| 305 | int ret = 0; |
| 306 | int namelen; |
| 307 | int mod = 0; |
| 308 | |
| 309 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 310 | |
| 311 | if (!vol_args) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 315 | ret = -EFAULT; |
| 316 | goto out; |
| 317 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 318 | |
| 319 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 320 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 321 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 322 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 323 | sizestr = vol_args->name; |
| 324 | devstr = strchr(sizestr, ':'); |
| 325 | if (devstr) { |
| 326 | char *end; |
| 327 | sizestr = devstr + 1; |
| 328 | *devstr = '\0'; |
| 329 | devstr = vol_args->name; |
| 330 | devid = simple_strtoull(devstr, &end, 10); |
| 331 | printk(KERN_INFO "resizing devid %llu\n", devid); |
| 332 | } |
| 333 | device = btrfs_find_device(root, devid, NULL); |
| 334 | if (!device) { |
| 335 | printk(KERN_INFO "resizer unable to find device %llu\n", devid); |
| 336 | ret = -EINVAL; |
| 337 | goto out_unlock; |
| 338 | } |
| 339 | if (!strcmp(sizestr, "max")) |
| 340 | new_size = device->bdev->bd_inode->i_size; |
| 341 | else { |
| 342 | if (sizestr[0] == '-') { |
| 343 | mod = -1; |
| 344 | sizestr++; |
| 345 | } else if (sizestr[0] == '+') { |
| 346 | mod = 1; |
| 347 | sizestr++; |
| 348 | } |
| 349 | new_size = btrfs_parse_size(sizestr); |
| 350 | if (new_size == 0) { |
| 351 | ret = -EINVAL; |
| 352 | goto out_unlock; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | old_size = device->total_bytes; |
| 357 | |
| 358 | if (mod < 0) { |
| 359 | if (new_size > old_size) { |
| 360 | ret = -EINVAL; |
| 361 | goto out_unlock; |
| 362 | } |
| 363 | new_size = old_size - new_size; |
| 364 | } else if (mod > 0) { |
| 365 | new_size = old_size + new_size; |
| 366 | } |
| 367 | |
| 368 | if (new_size < 256 * 1024 * 1024) { |
| 369 | ret = -EINVAL; |
| 370 | goto out_unlock; |
| 371 | } |
| 372 | if (new_size > device->bdev->bd_inode->i_size) { |
| 373 | ret = -EFBIG; |
| 374 | goto out_unlock; |
| 375 | } |
| 376 | |
| 377 | do_div(new_size, root->sectorsize); |
| 378 | new_size *= root->sectorsize; |
| 379 | |
| 380 | printk(KERN_INFO "new size for %s is %llu\n", |
| 381 | device->name, (unsigned long long)new_size); |
| 382 | |
| 383 | if (new_size > old_size) { |
| 384 | trans = btrfs_start_transaction(root, 1); |
| 385 | ret = btrfs_grow_device(trans, device, new_size); |
| 386 | btrfs_commit_transaction(trans, root); |
| 387 | } else { |
| 388 | ret = btrfs_shrink_device(device, new_size); |
| 389 | } |
| 390 | |
| 391 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 392 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 393 | out: |
| 394 | kfree(vol_args); |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root, |
| 399 | void __user *arg) |
| 400 | { |
| 401 | struct btrfs_ioctl_vol_args *vol_args; |
| 402 | struct btrfs_dir_item *di; |
| 403 | struct btrfs_path *path; |
| 404 | u64 root_dirid; |
| 405 | int namelen; |
| 406 | int ret; |
| 407 | |
| 408 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 409 | |
| 410 | if (!vol_args) |
| 411 | return -ENOMEM; |
| 412 | |
| 413 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 414 | ret = -EFAULT; |
| 415 | goto out; |
| 416 | } |
| 417 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 418 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 419 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 420 | if (strchr(vol_args->name, '/')) { |
| 421 | ret = -EINVAL; |
| 422 | goto out; |
| 423 | } |
| 424 | |
| 425 | path = btrfs_alloc_path(); |
| 426 | if (!path) { |
| 427 | ret = -ENOMEM; |
| 428 | goto out; |
| 429 | } |
| 430 | |
| 431 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 432 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 433 | path, root_dirid, |
| 434 | vol_args->name, namelen, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 435 | btrfs_free_path(path); |
| 436 | |
| 437 | if (di && !IS_ERR(di)) { |
| 438 | ret = -EEXIST; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | if (IS_ERR(di)) { |
| 443 | ret = PTR_ERR(di); |
| 444 | goto out; |
| 445 | } |
| 446 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 447 | mutex_lock(&root->fs_info->drop_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 448 | if (root == root->fs_info->tree_root) |
| 449 | ret = create_subvol(root, vol_args->name, namelen); |
| 450 | else |
| 451 | ret = create_snapshot(root, vol_args->name, namelen); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 452 | mutex_unlock(&root->fs_info->drop_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 453 | out: |
| 454 | kfree(vol_args); |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static int btrfs_ioctl_defrag(struct file *file) |
| 459 | { |
| 460 | struct inode *inode = fdentry(file)->d_inode; |
| 461 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 462 | |
| 463 | switch (inode->i_mode & S_IFMT) { |
| 464 | case S_IFDIR: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 465 | btrfs_defrag_root(root, 0); |
| 466 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 467 | break; |
| 468 | case S_IFREG: |
| 469 | btrfs_defrag_file(file); |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
| 477 | { |
| 478 | struct btrfs_ioctl_vol_args *vol_args; |
| 479 | int ret; |
| 480 | |
| 481 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 482 | |
| 483 | if (!vol_args) |
| 484 | return -ENOMEM; |
| 485 | |
| 486 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 487 | ret = -EFAULT; |
| 488 | goto out; |
| 489 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 490 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 491 | ret = btrfs_init_new_device(root, vol_args->name); |
| 492 | |
| 493 | out: |
| 494 | kfree(vol_args); |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
| 499 | { |
| 500 | struct btrfs_ioctl_vol_args *vol_args; |
| 501 | int ret; |
| 502 | |
| 503 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 504 | |
| 505 | if (!vol_args) |
| 506 | return -ENOMEM; |
| 507 | |
| 508 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 509 | ret = -EFAULT; |
| 510 | goto out; |
| 511 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 512 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 513 | ret = btrfs_rm_device(root, vol_args->name); |
| 514 | |
| 515 | out: |
| 516 | kfree(vol_args); |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | int dup_item_to_inode(struct btrfs_trans_handle *trans, |
| 521 | struct btrfs_root *root, |
| 522 | struct btrfs_path *path, |
| 523 | struct extent_buffer *leaf, |
| 524 | int slot, |
| 525 | struct btrfs_key *key, |
| 526 | u64 destino) |
| 527 | { |
| 528 | char *dup; |
| 529 | int len = btrfs_item_size_nr(leaf, slot); |
| 530 | struct btrfs_key ckey = *key; |
| 531 | int ret = 0; |
| 532 | |
| 533 | dup = kmalloc(len, GFP_NOFS); |
| 534 | if (!dup) |
| 535 | return -ENOMEM; |
| 536 | |
| 537 | read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len); |
| 538 | btrfs_release_path(root, path); |
| 539 | |
| 540 | ckey.objectid = destino; |
| 541 | ret = btrfs_insert_item(trans, root, &ckey, dup, len); |
| 542 | kfree(dup); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | long btrfs_ioctl_clone(struct file *file, unsigned long src_fd) |
| 547 | { |
| 548 | struct inode *inode = fdentry(file)->d_inode; |
| 549 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 550 | struct file *src_file; |
| 551 | struct inode *src; |
| 552 | struct btrfs_trans_handle *trans; |
| 553 | int ret; |
| 554 | u64 pos; |
| 555 | struct btrfs_path *path; |
| 556 | struct btrfs_key key; |
| 557 | struct extent_buffer *leaf; |
| 558 | u32 nritems; |
| 559 | int slot; |
| 560 | |
| 561 | src_file = fget(src_fd); |
| 562 | if (!src_file) |
| 563 | return -EBADF; |
| 564 | src = src_file->f_dentry->d_inode; |
| 565 | |
| 566 | ret = -EXDEV; |
| 567 | if (src->i_sb != inode->i_sb) |
| 568 | goto out_fput; |
| 569 | |
| 570 | if (inode < src) { |
| 571 | mutex_lock(&inode->i_mutex); |
| 572 | mutex_lock(&src->i_mutex); |
| 573 | } else { |
| 574 | mutex_lock(&src->i_mutex); |
| 575 | mutex_lock(&inode->i_mutex); |
| 576 | } |
| 577 | |
| 578 | ret = -ENOTEMPTY; |
| 579 | if (inode->i_size) |
| 580 | goto out_unlock; |
| 581 | |
| 582 | /* do any pending delalloc/csum calc on src, one way or |
| 583 | another, and lock file content */ |
| 584 | while (1) { |
| 585 | filemap_write_and_wait(src->i_mapping); |
| 586 | lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
| 587 | if (BTRFS_I(src)->delalloc_bytes == 0) |
| 588 | break; |
| 589 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
| 590 | } |
| 591 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 592 | trans = btrfs_start_transaction(root, 0); |
| 593 | path = btrfs_alloc_path(); |
| 594 | if (!path) { |
| 595 | ret = -ENOMEM; |
| 596 | goto out; |
| 597 | } |
| 598 | key.offset = 0; |
| 599 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 600 | key.objectid = src->i_ino; |
| 601 | pos = 0; |
| 602 | path->reada = 2; |
| 603 | |
| 604 | while (1) { |
| 605 | /* |
| 606 | * note the key will change type as we walk through the |
| 607 | * tree. |
| 608 | */ |
| 609 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 610 | if (ret < 0) |
| 611 | goto out; |
| 612 | |
| 613 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 614 | ret = btrfs_next_leaf(root, path); |
| 615 | if (ret < 0) |
| 616 | goto out; |
| 617 | if (ret > 0) |
| 618 | break; |
| 619 | } |
| 620 | leaf = path->nodes[0]; |
| 621 | slot = path->slots[0]; |
| 622 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 623 | nritems = btrfs_header_nritems(leaf); |
| 624 | |
| 625 | if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY || |
| 626 | key.objectid != src->i_ino) |
| 627 | break; |
| 628 | |
| 629 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 630 | struct btrfs_file_extent_item *extent; |
| 631 | int found_type; |
| 632 | pos = key.offset; |
| 633 | extent = btrfs_item_ptr(leaf, slot, |
| 634 | struct btrfs_file_extent_item); |
| 635 | found_type = btrfs_file_extent_type(leaf, extent); |
| 636 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 637 | u64 len = btrfs_file_extent_num_bytes(leaf, |
| 638 | extent); |
| 639 | u64 ds = btrfs_file_extent_disk_bytenr(leaf, |
| 640 | extent); |
| 641 | u64 dl = btrfs_file_extent_disk_num_bytes(leaf, |
| 642 | extent); |
| 643 | u64 off = btrfs_file_extent_offset(leaf, |
| 644 | extent); |
| 645 | btrfs_insert_file_extent(trans, root, |
| 646 | inode->i_ino, pos, |
| 647 | ds, dl, len, off); |
| 648 | /* ds == 0 means there's a hole */ |
| 649 | if (ds != 0) { |
| 650 | btrfs_inc_extent_ref(trans, root, |
| 651 | ds, dl, |
| 652 | root->root_key.objectid, |
| 653 | trans->transid, |
| 654 | inode->i_ino, pos); |
| 655 | } |
| 656 | pos = key.offset + len; |
| 657 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 658 | ret = dup_item_to_inode(trans, root, path, |
| 659 | leaf, slot, &key, |
| 660 | inode->i_ino); |
| 661 | if (ret) |
| 662 | goto out; |
| 663 | pos = key.offset + btrfs_item_size_nr(leaf, |
| 664 | slot); |
| 665 | } |
| 666 | } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) { |
| 667 | ret = dup_item_to_inode(trans, root, path, leaf, |
| 668 | slot, &key, inode->i_ino); |
| 669 | |
| 670 | if (ret) |
| 671 | goto out; |
| 672 | } |
| 673 | key.offset++; |
| 674 | btrfs_release_path(root, path); |
| 675 | } |
| 676 | |
| 677 | ret = 0; |
| 678 | out: |
| 679 | btrfs_free_path(path); |
| 680 | |
| 681 | inode->i_blocks = src->i_blocks; |
| 682 | i_size_write(inode, src->i_size); |
| 683 | btrfs_update_inode(trans, root, inode); |
| 684 | |
| 685 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
| 686 | |
| 687 | btrfs_end_transaction(trans, root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 688 | |
| 689 | out_unlock: |
| 690 | mutex_unlock(&src->i_mutex); |
| 691 | mutex_unlock(&inode->i_mutex); |
| 692 | out_fput: |
| 693 | fput(src_file); |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * there are many ways the trans_start and trans_end ioctls can lead |
| 699 | * to deadlocks. They should only be used by applications that |
| 700 | * basically own the machine, and have a very in depth understanding |
| 701 | * of all the possible deadlocks and enospc problems. |
| 702 | */ |
| 703 | long btrfs_ioctl_trans_start(struct file *file) |
| 704 | { |
| 705 | struct inode *inode = fdentry(file)->d_inode; |
| 706 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 707 | struct btrfs_trans_handle *trans; |
| 708 | int ret = 0; |
| 709 | |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 710 | if (!capable(CAP_SYS_ADMIN)) |
| 711 | return -EPERM; |
| 712 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 713 | if (file->private_data) { |
| 714 | ret = -EINPROGRESS; |
| 715 | goto out; |
| 716 | } |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 717 | |
| 718 | mutex_lock(&root->fs_info->trans_mutex); |
| 719 | root->fs_info->open_ioctl_trans++; |
| 720 | mutex_unlock(&root->fs_info->trans_mutex); |
| 721 | |
| 722 | trans = btrfs_start_ioctl_transaction(root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 723 | if (trans) |
| 724 | file->private_data = trans; |
| 725 | else |
| 726 | ret = -ENOMEM; |
| 727 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ |
| 728 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * there are many ways the trans_start and trans_end ioctls can lead |
| 734 | * to deadlocks. They should only be used by applications that |
| 735 | * basically own the machine, and have a very in depth understanding |
| 736 | * of all the possible deadlocks and enospc problems. |
| 737 | */ |
| 738 | long btrfs_ioctl_trans_end(struct file *file) |
| 739 | { |
| 740 | struct inode *inode = fdentry(file)->d_inode; |
| 741 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 742 | struct btrfs_trans_handle *trans; |
| 743 | int ret = 0; |
| 744 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 745 | trans = file->private_data; |
| 746 | if (!trans) { |
| 747 | ret = -EINVAL; |
| 748 | goto out; |
| 749 | } |
| 750 | btrfs_end_transaction(trans, root); |
| 751 | file->private_data = 0; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 752 | |
| 753 | mutex_lock(&root->fs_info->trans_mutex); |
| 754 | root->fs_info->open_ioctl_trans--; |
| 755 | mutex_unlock(&root->fs_info->trans_mutex); |
| 756 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 757 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | long btrfs_ioctl(struct file *file, unsigned int |
| 762 | cmd, unsigned long arg) |
| 763 | { |
| 764 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
| 765 | |
| 766 | switch (cmd) { |
| 767 | case BTRFS_IOC_SNAP_CREATE: |
| 768 | return btrfs_ioctl_snap_create(root, (void __user *)arg); |
| 769 | case BTRFS_IOC_DEFRAG: |
| 770 | return btrfs_ioctl_defrag(file); |
| 771 | case BTRFS_IOC_RESIZE: |
| 772 | return btrfs_ioctl_resize(root, (void __user *)arg); |
| 773 | case BTRFS_IOC_ADD_DEV: |
| 774 | return btrfs_ioctl_add_dev(root, (void __user *)arg); |
| 775 | case BTRFS_IOC_RM_DEV: |
| 776 | return btrfs_ioctl_rm_dev(root, (void __user *)arg); |
| 777 | case BTRFS_IOC_BALANCE: |
| 778 | return btrfs_balance(root->fs_info->dev_root); |
| 779 | case BTRFS_IOC_CLONE: |
| 780 | return btrfs_ioctl_clone(file, arg); |
| 781 | case BTRFS_IOC_TRANS_START: |
| 782 | return btrfs_ioctl_trans_start(file); |
| 783 | case BTRFS_IOC_TRANS_END: |
| 784 | return btrfs_ioctl_trans_end(file); |
| 785 | case BTRFS_IOC_SYNC: |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame^] | 786 | btrfs_start_delalloc_inodes(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 787 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | return -ENOTTY; |
| 792 | } |