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