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 | |
| 257 | mode &= (S_IRWXUGO|S_ISVTX); |
| 258 | error = security_inode_mkdir(parent->dentry->d_inode, dentry, mode); |
| 259 | if (error) |
| 260 | goto out_drop_write; |
| 261 | |
| 262 | /* |
| 263 | * Actually perform the low-level subvolume creation after all |
| 264 | * this VFS fuzz. |
| 265 | * |
| 266 | * Eventually we want to pass in an inode under which we create this |
| 267 | * subvolume, but for now all are under the filesystem root. |
| 268 | * |
| 269 | * Also we should pass on the mode eventually to allow creating new |
| 270 | * subvolume with specific mode bits. |
| 271 | */ |
| 272 | error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry, |
| 273 | name, namelen); |
| 274 | if (error) |
| 275 | goto out_drop_write; |
| 276 | |
| 277 | fsnotify_mkdir(parent->dentry->d_inode, dentry); |
| 278 | out_drop_write: |
| 279 | mnt_drop_write(parent->mnt); |
| 280 | out_dput: |
| 281 | dput(dentry); |
| 282 | out_unlock: |
| 283 | mutex_unlock(&parent->dentry->d_inode->i_mutex); |
| 284 | return error; |
| 285 | } |
| 286 | |
| 287 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 288 | int btrfs_defrag_file(struct file *file) |
| 289 | { |
| 290 | struct inode *inode = fdentry(file)->d_inode; |
| 291 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 292 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 293 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 294 | struct page *page; |
| 295 | unsigned long last_index; |
| 296 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; |
| 297 | unsigned long total_read = 0; |
| 298 | u64 page_start; |
| 299 | u64 page_end; |
| 300 | unsigned long i; |
| 301 | int ret; |
| 302 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 303 | ret = btrfs_check_free_space(root, inode->i_size, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 304 | if (ret) |
| 305 | return -ENOSPC; |
| 306 | |
| 307 | mutex_lock(&inode->i_mutex); |
| 308 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; |
| 309 | for (i = 0; i <= last_index; i++) { |
| 310 | if (total_read % ra_pages == 0) { |
| 311 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, |
| 312 | min(last_index, i + ra_pages - 1)); |
| 313 | } |
| 314 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 315 | again: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 316 | page = grab_cache_page(inode->i_mapping, i); |
| 317 | if (!page) |
| 318 | goto out_unlock; |
| 319 | if (!PageUptodate(page)) { |
| 320 | btrfs_readpage(NULL, page); |
| 321 | lock_page(page); |
| 322 | if (!PageUptodate(page)) { |
| 323 | unlock_page(page); |
| 324 | page_cache_release(page); |
| 325 | goto out_unlock; |
| 326 | } |
| 327 | } |
| 328 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 329 | wait_on_page_writeback(page); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 330 | |
| 331 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 332 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 333 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 334 | |
| 335 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 336 | if (ordered) { |
| 337 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 338 | unlock_page(page); |
| 339 | page_cache_release(page); |
| 340 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 341 | btrfs_put_ordered_extent(ordered); |
| 342 | goto again; |
| 343 | } |
| 344 | set_page_extent_mapped(page); |
| 345 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 346 | /* |
| 347 | * this makes sure page_mkwrite is called on the |
| 348 | * page if it is dirtied again later |
| 349 | */ |
| 350 | clear_page_dirty_for_io(page); |
| 351 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 352 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 353 | |
| 354 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 355 | set_page_dirty(page); |
| 356 | unlock_page(page); |
| 357 | page_cache_release(page); |
| 358 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); |
| 359 | } |
| 360 | |
| 361 | out_unlock: |
| 362 | mutex_unlock(&inode->i_mutex); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Called inside transaction, so use GFP_NOFS |
| 368 | */ |
| 369 | |
| 370 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
| 371 | { |
| 372 | u64 new_size; |
| 373 | u64 old_size; |
| 374 | u64 devid = 1; |
| 375 | struct btrfs_ioctl_vol_args *vol_args; |
| 376 | struct btrfs_trans_handle *trans; |
| 377 | struct btrfs_device *device = NULL; |
| 378 | char *sizestr; |
| 379 | char *devstr = NULL; |
| 380 | int ret = 0; |
| 381 | int namelen; |
| 382 | int mod = 0; |
| 383 | |
| 384 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 385 | |
| 386 | if (!vol_args) |
| 387 | return -ENOMEM; |
| 388 | |
| 389 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 390 | ret = -EFAULT; |
| 391 | goto out; |
| 392 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 393 | |
| 394 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 395 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 396 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 397 | mutex_lock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 398 | sizestr = vol_args->name; |
| 399 | devstr = strchr(sizestr, ':'); |
| 400 | if (devstr) { |
| 401 | char *end; |
| 402 | sizestr = devstr + 1; |
| 403 | *devstr = '\0'; |
| 404 | devstr = vol_args->name; |
| 405 | devid = simple_strtoull(devstr, &end, 10); |
| 406 | printk(KERN_INFO "resizing devid %llu\n", devid); |
| 407 | } |
| 408 | device = btrfs_find_device(root, devid, NULL); |
| 409 | if (!device) { |
| 410 | printk(KERN_INFO "resizer unable to find device %llu\n", devid); |
| 411 | ret = -EINVAL; |
| 412 | goto out_unlock; |
| 413 | } |
| 414 | if (!strcmp(sizestr, "max")) |
| 415 | new_size = device->bdev->bd_inode->i_size; |
| 416 | else { |
| 417 | if (sizestr[0] == '-') { |
| 418 | mod = -1; |
| 419 | sizestr++; |
| 420 | } else if (sizestr[0] == '+') { |
| 421 | mod = 1; |
| 422 | sizestr++; |
| 423 | } |
| 424 | new_size = btrfs_parse_size(sizestr); |
| 425 | if (new_size == 0) { |
| 426 | ret = -EINVAL; |
| 427 | goto out_unlock; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | old_size = device->total_bytes; |
| 432 | |
| 433 | if (mod < 0) { |
| 434 | if (new_size > old_size) { |
| 435 | ret = -EINVAL; |
| 436 | goto out_unlock; |
| 437 | } |
| 438 | new_size = old_size - new_size; |
| 439 | } else if (mod > 0) { |
| 440 | new_size = old_size + new_size; |
| 441 | } |
| 442 | |
| 443 | if (new_size < 256 * 1024 * 1024) { |
| 444 | ret = -EINVAL; |
| 445 | goto out_unlock; |
| 446 | } |
| 447 | if (new_size > device->bdev->bd_inode->i_size) { |
| 448 | ret = -EFBIG; |
| 449 | goto out_unlock; |
| 450 | } |
| 451 | |
| 452 | do_div(new_size, root->sectorsize); |
| 453 | new_size *= root->sectorsize; |
| 454 | |
| 455 | printk(KERN_INFO "new size for %s is %llu\n", |
| 456 | device->name, (unsigned long long)new_size); |
| 457 | |
| 458 | if (new_size > old_size) { |
| 459 | trans = btrfs_start_transaction(root, 1); |
| 460 | ret = btrfs_grow_device(trans, device, new_size); |
| 461 | btrfs_commit_transaction(trans, root); |
| 462 | } else { |
| 463 | ret = btrfs_shrink_device(device, new_size); |
| 464 | } |
| 465 | |
| 466 | out_unlock: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 467 | mutex_unlock(&root->fs_info->volume_mutex); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 468 | out: |
| 469 | kfree(vol_args); |
| 470 | return ret; |
| 471 | } |
| 472 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 473 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 474 | void __user *arg) |
| 475 | { |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 476 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 477 | struct btrfs_ioctl_vol_args *vol_args; |
| 478 | struct btrfs_dir_item *di; |
| 479 | struct btrfs_path *path; |
| 480 | u64 root_dirid; |
| 481 | int namelen; |
| 482 | int ret; |
| 483 | |
| 484 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 485 | |
| 486 | if (!vol_args) |
| 487 | return -ENOMEM; |
| 488 | |
| 489 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 490 | ret = -EFAULT; |
| 491 | goto out; |
| 492 | } |
| 493 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 494 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 495 | namelen = strlen(vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 496 | if (strchr(vol_args->name, '/')) { |
| 497 | ret = -EINVAL; |
| 498 | goto out; |
| 499 | } |
| 500 | |
| 501 | path = btrfs_alloc_path(); |
| 502 | if (!path) { |
| 503 | ret = -ENOMEM; |
| 504 | goto out; |
| 505 | } |
| 506 | |
| 507 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 508 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 509 | path, root_dirid, |
| 510 | vol_args->name, namelen, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 511 | btrfs_free_path(path); |
| 512 | |
| 513 | if (di && !IS_ERR(di)) { |
| 514 | ret = -EEXIST; |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | if (IS_ERR(di)) { |
| 519 | ret = PTR_ERR(di); |
| 520 | goto out; |
| 521 | } |
| 522 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 523 | if (root == root->fs_info->tree_root) { |
| 524 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, |
| 525 | file->f_path.dentry->d_inode->i_mode, |
| 526 | namelen); |
| 527 | } else { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 528 | ret = create_snapshot(root, vol_args->name, namelen); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 529 | } |
| 530 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 531 | out: |
| 532 | kfree(vol_args); |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static int btrfs_ioctl_defrag(struct file *file) |
| 537 | { |
| 538 | struct inode *inode = fdentry(file)->d_inode; |
| 539 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 540 | |
| 541 | switch (inode->i_mode & S_IFMT) { |
| 542 | case S_IFDIR: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 543 | btrfs_defrag_root(root, 0); |
| 544 | btrfs_defrag_root(root->fs_info->extent_root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 545 | break; |
| 546 | case S_IFREG: |
| 547 | btrfs_defrag_file(file); |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
| 555 | { |
| 556 | struct btrfs_ioctl_vol_args *vol_args; |
| 557 | int ret; |
| 558 | |
| 559 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 560 | |
| 561 | if (!vol_args) |
| 562 | return -ENOMEM; |
| 563 | |
| 564 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 565 | ret = -EFAULT; |
| 566 | goto out; |
| 567 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 568 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 569 | ret = btrfs_init_new_device(root, vol_args->name); |
| 570 | |
| 571 | out: |
| 572 | kfree(vol_args); |
| 573 | return ret; |
| 574 | } |
| 575 | |
| 576 | long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
| 577 | { |
| 578 | struct btrfs_ioctl_vol_args *vol_args; |
| 579 | int ret; |
| 580 | |
| 581 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
| 582 | |
| 583 | if (!vol_args) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { |
| 587 | ret = -EFAULT; |
| 588 | goto out; |
| 589 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 590 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 591 | ret = btrfs_rm_device(root, vol_args->name); |
| 592 | |
| 593 | out: |
| 594 | kfree(vol_args); |
| 595 | return ret; |
| 596 | } |
| 597 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 598 | long btrfs_ioctl_clone(struct file *file, unsigned long src_fd) |
| 599 | { |
| 600 | struct inode *inode = fdentry(file)->d_inode; |
| 601 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 602 | struct file *src_file; |
| 603 | struct inode *src; |
| 604 | struct btrfs_trans_handle *trans; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 605 | struct btrfs_path *path; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 606 | struct extent_buffer *leaf; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 607 | char *buf; |
| 608 | struct btrfs_key key; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 609 | u32 nritems; |
| 610 | int slot; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 611 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 612 | |
| 613 | src_file = fget(src_fd); |
| 614 | if (!src_file) |
| 615 | return -EBADF; |
| 616 | src = src_file->f_dentry->d_inode; |
| 617 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 618 | ret = -EISDIR; |
| 619 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 620 | goto out_fput; |
| 621 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 622 | ret = -EXDEV; |
| 623 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
| 624 | goto out_fput; |
| 625 | |
| 626 | ret = -ENOMEM; |
| 627 | buf = vmalloc(btrfs_level_size(root, 0)); |
| 628 | if (!buf) |
| 629 | goto out_fput; |
| 630 | |
| 631 | path = btrfs_alloc_path(); |
| 632 | if (!path) { |
| 633 | vfree(buf); |
| 634 | goto out_fput; |
| 635 | } |
| 636 | path->reada = 2; |
| 637 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 638 | if (inode < src) { |
| 639 | mutex_lock(&inode->i_mutex); |
| 640 | mutex_lock(&src->i_mutex); |
| 641 | } else { |
| 642 | mutex_lock(&src->i_mutex); |
| 643 | mutex_lock(&inode->i_mutex); |
| 644 | } |
| 645 | |
| 646 | ret = -ENOTEMPTY; |
| 647 | if (inode->i_size) |
| 648 | goto out_unlock; |
| 649 | |
| 650 | /* do any pending delalloc/csum calc on src, one way or |
| 651 | another, and lock file content */ |
| 652 | while (1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 653 | struct btrfs_ordered_extent *ordered; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 654 | lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 655 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
| 656 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 657 | break; |
| 658 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 659 | if (ordered) |
| 660 | btrfs_put_ordered_extent(ordered); |
| 661 | btrfs_wait_ordered_range(src, 0, (u64)-1); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 662 | } |
| 663 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 664 | trans = btrfs_start_transaction(root, 1); |
| 665 | BUG_ON(!trans); |
| 666 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 667 | key.objectid = src->i_ino; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 668 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 669 | key.offset = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 670 | |
| 671 | while (1) { |
| 672 | /* |
| 673 | * note the key will change type as we walk through the |
| 674 | * tree. |
| 675 | */ |
| 676 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); |
| 677 | if (ret < 0) |
| 678 | goto out; |
| 679 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 680 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 681 | if (path->slots[0] >= nritems) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 682 | ret = btrfs_next_leaf(root, path); |
| 683 | if (ret < 0) |
| 684 | goto out; |
| 685 | if (ret > 0) |
| 686 | break; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 687 | nritems = btrfs_header_nritems(path->nodes[0]); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 688 | } |
| 689 | leaf = path->nodes[0]; |
| 690 | slot = path->slots[0]; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 691 | |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 692 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 693 | if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY || |
| 694 | key.objectid != src->i_ino) |
| 695 | break; |
| 696 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 697 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY || |
| 698 | btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) { |
| 699 | u32 size; |
| 700 | struct btrfs_key new_key; |
| 701 | |
| 702 | size = btrfs_item_size_nr(leaf, slot); |
| 703 | read_extent_buffer(leaf, buf, |
| 704 | btrfs_item_ptr_offset(leaf, slot), |
| 705 | size); |
| 706 | btrfs_release_path(root, path); |
| 707 | |
| 708 | memcpy(&new_key, &key, sizeof(new_key)); |
| 709 | new_key.objectid = inode->i_ino; |
| 710 | ret = btrfs_insert_empty_item(trans, root, path, |
| 711 | &new_key, size); |
| 712 | if (ret) |
| 713 | goto out; |
| 714 | |
| 715 | leaf = path->nodes[0]; |
| 716 | slot = path->slots[0]; |
| 717 | write_extent_buffer(leaf, buf, |
| 718 | btrfs_item_ptr_offset(leaf, slot), |
| 719 | size); |
| 720 | btrfs_mark_buffer_dirty(leaf); |
| 721 | } |
| 722 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 723 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 724 | struct btrfs_file_extent_item *extent; |
| 725 | int found_type; |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 726 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 727 | extent = btrfs_item_ptr(leaf, slot, |
| 728 | struct btrfs_file_extent_item); |
| 729 | found_type = btrfs_file_extent_type(leaf, extent); |
| 730 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 731 | u64 ds = btrfs_file_extent_disk_bytenr(leaf, |
| 732 | extent); |
| 733 | u64 dl = btrfs_file_extent_disk_num_bytes(leaf, |
| 734 | extent); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 735 | /* ds == 0 means there's a hole */ |
| 736 | if (ds != 0) { |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 737 | ret = btrfs_inc_extent_ref(trans, root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 738 | ds, dl, leaf->start, |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 739 | root->root_key.objectid, |
| 740 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 741 | inode->i_ino); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 742 | BUG_ON(ret); |
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 | } |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 745 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 746 | btrfs_release_path(root, path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 747 | key.offset++; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 748 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 749 | ret = 0; |
| 750 | out: |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 751 | btrfs_release_path(root, path); |
| 752 | if (ret == 0) { |
| 753 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 754 | inode_set_bytes(inode, inode_get_bytes(src)); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 755 | btrfs_i_size_write(inode, src->i_size); |
| 756 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
| 757 | ret = btrfs_update_inode(trans, root, inode); |
| 758 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 759 | btrfs_end_transaction(trans, root); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 760 | unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); |
| 761 | if (ret) |
| 762 | vmtruncate(inode, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 763 | out_unlock: |
| 764 | mutex_unlock(&src->i_mutex); |
| 765 | mutex_unlock(&inode->i_mutex); |
Yan Zheng | ae01a0a | 2008-08-04 23:23:47 -0400 | [diff] [blame] | 766 | vfree(buf); |
| 767 | btrfs_free_path(path); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 768 | out_fput: |
| 769 | fput(src_file); |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * there are many ways the trans_start and trans_end ioctls can lead |
| 775 | * to deadlocks. They should only be used by applications that |
| 776 | * basically own the machine, and have a very in depth understanding |
| 777 | * of all the possible deadlocks and enospc problems. |
| 778 | */ |
| 779 | long btrfs_ioctl_trans_start(struct file *file) |
| 780 | { |
| 781 | struct inode *inode = fdentry(file)->d_inode; |
| 782 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 783 | struct btrfs_trans_handle *trans; |
| 784 | int ret = 0; |
| 785 | |
Christoph Hellwig | df5b552 | 2008-06-11 21:53:58 -0400 | [diff] [blame] | 786 | if (!capable(CAP_SYS_ADMIN)) |
| 787 | return -EPERM; |
| 788 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 789 | if (file->private_data) { |
| 790 | ret = -EINPROGRESS; |
| 791 | goto out; |
| 792 | } |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 793 | |
| 794 | mutex_lock(&root->fs_info->trans_mutex); |
| 795 | root->fs_info->open_ioctl_trans++; |
| 796 | mutex_unlock(&root->fs_info->trans_mutex); |
| 797 | |
| 798 | trans = btrfs_start_ioctl_transaction(root, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 799 | if (trans) |
| 800 | file->private_data = trans; |
| 801 | else |
| 802 | ret = -ENOMEM; |
| 803 | /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ |
| 804 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * there are many ways the trans_start and trans_end ioctls can lead |
| 810 | * to deadlocks. They should only be used by applications that |
| 811 | * basically own the machine, and have a very in depth understanding |
| 812 | * of all the possible deadlocks and enospc problems. |
| 813 | */ |
| 814 | long btrfs_ioctl_trans_end(struct file *file) |
| 815 | { |
| 816 | struct inode *inode = fdentry(file)->d_inode; |
| 817 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 818 | struct btrfs_trans_handle *trans; |
| 819 | int ret = 0; |
| 820 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 821 | trans = file->private_data; |
| 822 | if (!trans) { |
| 823 | ret = -EINVAL; |
| 824 | goto out; |
| 825 | } |
| 826 | btrfs_end_transaction(trans, root); |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 827 | file->private_data = NULL; |
Sage Weil | 9ca9ee0 | 2008-08-04 10:41:27 -0400 | [diff] [blame] | 828 | |
| 829 | mutex_lock(&root->fs_info->trans_mutex); |
| 830 | root->fs_info->open_ioctl_trans--; |
| 831 | mutex_unlock(&root->fs_info->trans_mutex); |
| 832 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 833 | out: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | long btrfs_ioctl(struct file *file, unsigned int |
| 838 | cmd, unsigned long arg) |
| 839 | { |
| 840 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
| 841 | |
| 842 | switch (cmd) { |
| 843 | case BTRFS_IOC_SNAP_CREATE: |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame^] | 844 | return btrfs_ioctl_snap_create(file, (void __user *)arg); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 845 | case BTRFS_IOC_DEFRAG: |
| 846 | return btrfs_ioctl_defrag(file); |
| 847 | case BTRFS_IOC_RESIZE: |
| 848 | return btrfs_ioctl_resize(root, (void __user *)arg); |
| 849 | case BTRFS_IOC_ADD_DEV: |
| 850 | return btrfs_ioctl_add_dev(root, (void __user *)arg); |
| 851 | case BTRFS_IOC_RM_DEV: |
| 852 | return btrfs_ioctl_rm_dev(root, (void __user *)arg); |
| 853 | case BTRFS_IOC_BALANCE: |
| 854 | return btrfs_balance(root->fs_info->dev_root); |
| 855 | case BTRFS_IOC_CLONE: |
| 856 | return btrfs_ioctl_clone(file, arg); |
| 857 | case BTRFS_IOC_TRANS_START: |
| 858 | return btrfs_ioctl_trans_start(file); |
| 859 | case BTRFS_IOC_TRANS_END: |
| 860 | return btrfs_ioctl_trans_end(file); |
| 861 | case BTRFS_IOC_SYNC: |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 862 | btrfs_start_delalloc_inodes(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 863 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | return -ENOTTY; |
| 868 | } |