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