blob: ba484aac1b9c38a6ea8942bf15d0f11cc9d47b41 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
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 Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#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 Hellwigcb8e7092008-10-09 13:39:39 -040032#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040034#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040035#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 Hellwigcb8e7092008-10-09 13:39:39 -040040#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040041#include <linux/version.h>
42#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040043#include <linux/vmalloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040053
54
55
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040056static noinline int create_subvol(struct btrfs_root *root,
57 struct dentry *dentry,
58 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040059{
60 struct btrfs_trans_handle *trans;
61 struct btrfs_key key;
62 struct btrfs_root_item root_item;
63 struct btrfs_inode_item *inode_item;
64 struct extent_buffer *leaf;
65 struct btrfs_root *new_root = root;
66 struct inode *dir;
67 int ret;
68 int err;
69 u64 objectid;
70 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -050071 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040072 unsigned long nr = 1;
73
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074 ret = btrfs_check_free_space(root, 1, 0);
75 if (ret)
76 goto fail_commit;
77
78 trans = btrfs_start_transaction(root, 1);
79 BUG_ON(!trans);
80
81 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
82 0, &objectid);
83 if (ret)
84 goto fail;
85
Zheng Yan31840ae2008-09-23 13:14:14 -040086 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
87 objectid, trans->transid, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -040088 if (IS_ERR(leaf)) {
89 ret = PTR_ERR(leaf);
90 goto fail;
91 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040092
93 btrfs_set_header_nritems(leaf, 0);
94 btrfs_set_header_level(leaf, 0);
95 btrfs_set_header_bytenr(leaf, leaf->start);
96 btrfs_set_header_generation(leaf, trans->transid);
97 btrfs_set_header_owner(leaf, objectid);
98
99 write_extent_buffer(leaf, root->fs_info->fsid,
100 (unsigned long)btrfs_header_fsid(leaf),
101 BTRFS_FSID_SIZE);
102 btrfs_mark_buffer_dirty(leaf);
103
104 inode_item = &root_item.inode;
105 memset(inode_item, 0, sizeof(*inode_item));
106 inode_item->generation = cpu_to_le64(1);
107 inode_item->size = cpu_to_le64(3);
108 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400109 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400110 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
111
112 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400113 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400114 btrfs_set_root_level(&root_item, 0);
115 btrfs_set_root_refs(&root_item, 1);
116 btrfs_set_root_used(&root_item, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400117 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400118
119 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
120 root_item.drop_level = 0;
121
Chris Mason925baed2008-06-25 16:01:30 -0400122 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400123 free_extent_buffer(leaf);
124 leaf = NULL;
125
126 btrfs_set_root_dirid(&root_item, new_dirid);
127
128 key.objectid = objectid;
129 key.offset = 1;
130 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
131 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
132 &root_item);
133 if (ret)
134 goto fail;
135
136 /*
137 * insert the directory item
138 */
139 key.offset = (u64)-1;
Chris Mason3de45862008-11-17 21:02:50 -0500140 dir = dentry->d_parent->d_inode;
141 ret = btrfs_set_inode_index(dir, &index);
142 BUG_ON(ret);
143
144 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400145 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500146 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400147 if (ret)
148 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500149
Yan Zheng52c26172009-01-05 15:43:43 -0500150 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
151 ret = btrfs_update_inode(trans, root, dir);
152 BUG_ON(ret);
153
Chris Mason0660b5a2008-11-17 20:37:39 -0500154 /* add the backref first */
155 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
156 objectid, BTRFS_ROOT_BACKREF_KEY,
157 root->root_key.objectid,
158 dir->i_ino, index, name, namelen);
159
160 BUG_ON(ret);
161
162 /* now add the forward ref */
163 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
164 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
165 objectid,
166 dir->i_ino, index, name, namelen);
167
168 BUG_ON(ret);
169
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400170 ret = btrfs_commit_transaction(trans, root);
171 if (ret)
172 goto fail_commit;
173
Chris Mason3de45862008-11-17 21:02:50 -0500174 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400175 BUG_ON(!new_root);
176
177 trans = btrfs_start_transaction(new_root, 1);
178 BUG_ON(!trans);
179
Yan Zhengd2fb3432008-12-11 16:30:39 -0500180 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400181 BTRFS_I(dir)->block_group);
182 if (ret)
183 goto fail;
184
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400185fail:
186 nr = trans->blocks_used;
187 err = btrfs_commit_transaction(trans, new_root);
188 if (err && !ret)
189 ret = err;
190fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400191 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400192 return ret;
193}
194
Chris Mason3de45862008-11-17 21:02:50 -0500195static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
196 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400197{
198 struct btrfs_pending_snapshot *pending_snapshot;
199 struct btrfs_trans_handle *trans;
Chris Mason3de45862008-11-17 21:02:50 -0500200 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400201 int err;
202 unsigned long nr = 0;
203
204 if (!root->ref_cows)
205 return -EINVAL;
206
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400207 ret = btrfs_check_free_space(root, 1, 0);
208 if (ret)
209 goto fail_unlock;
210
Chris Mason3de45862008-11-17 21:02:50 -0500211 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400212 if (!pending_snapshot) {
213 ret = -ENOMEM;
214 goto fail_unlock;
215 }
216 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
217 if (!pending_snapshot->name) {
218 ret = -ENOMEM;
219 kfree(pending_snapshot);
220 goto fail_unlock;
221 }
222 memcpy(pending_snapshot->name, name, namelen);
223 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500224 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400225 trans = btrfs_start_transaction(root, 1);
226 BUG_ON(!trans);
227 pending_snapshot->root = root;
228 list_add(&pending_snapshot->list,
229 &trans->transaction->pending_snapshots);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400230 err = btrfs_commit_transaction(trans, root);
231
232fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400233 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400234 return ret;
235}
236
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400237/* copy of may_create in fs/namei.c() */
238static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
239{
240 if (child->d_inode)
241 return -EEXIST;
242 if (IS_DEADDIR(dir))
243 return -ENOENT;
244 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
245}
246
247/*
248 * Create a new subvolume below @parent. This is largely modeled after
249 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
250 * inside this filesystem so it's quite a bit simpler.
251 */
252static noinline int btrfs_mksubvol(struct path *parent, char *name,
Chris Mason3de45862008-11-17 21:02:50 -0500253 int mode, int namelen,
254 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400255{
256 struct dentry *dentry;
257 int error;
258
259 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
260
261 dentry = lookup_one_len(name, parent->dentry, namelen);
262 error = PTR_ERR(dentry);
263 if (IS_ERR(dentry))
264 goto out_unlock;
265
266 error = -EEXIST;
267 if (dentry->d_inode)
268 goto out_dput;
269
270 if (!IS_POSIXACL(parent->dentry->d_inode))
271 mode &= ~current->fs->umask;
Chris Mason3de45862008-11-17 21:02:50 -0500272
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400273 error = mnt_want_write(parent->mnt);
274 if (error)
275 goto out_dput;
276
277 error = btrfs_may_create(parent->dentry->d_inode, dentry);
278 if (error)
279 goto out_drop_write;
280
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400281 /*
282 * Actually perform the low-level subvolume creation after all
283 * this VFS fuzz.
284 *
285 * Eventually we want to pass in an inode under which we create this
286 * subvolume, but for now all are under the filesystem root.
287 *
288 * Also we should pass on the mode eventually to allow creating new
289 * subvolume with specific mode bits.
290 */
Chris Mason3de45862008-11-17 21:02:50 -0500291 if (snap_src) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500292 struct dentry *dir = dentry->d_parent;
293 struct dentry *test = dir->d_parent;
294 struct btrfs_path *path = btrfs_alloc_path();
295 int ret;
296 u64 test_oid;
297 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
298
299 test_oid = snap_src->root_key.objectid;
300
301 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
302 path, parent_oid, test_oid);
303 if (ret == 0)
304 goto create;
305 btrfs_release_path(snap_src->fs_info->tree_root, path);
306
307 /* we need to make sure we aren't creating a directory loop
308 * by taking a snapshot of something that has our current
309 * subvol in its directory tree. So, this loops through
310 * the dentries and checks the forward refs for each subvolume
311 * to see if is references the subvolume where we are
312 * placing this new snapshot.
313 */
314 while(1) {
315 if (!test ||
316 dir == snap_src->fs_info->sb->s_root ||
317 test == snap_src->fs_info->sb->s_root ||
318 test->d_inode->i_sb != snap_src->fs_info->sb) {
319 break;
320 }
321 if (S_ISLNK(test->d_inode->i_mode)) {
322 printk("Symlink in snapshot path, failed\n");
323 error = -EMLINK;
324 btrfs_free_path(path);
325 goto out_drop_write;
326 }
327 test_oid =
328 BTRFS_I(test->d_inode)->root->root_key.objectid;
329 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
330 path, test_oid, parent_oid);
331 if (ret == 0) {
332 printk("Snapshot creation failed, looping\n");
333 error = -EMLINK;
334 btrfs_free_path(path);
335 goto out_drop_write;
336 }
337 btrfs_release_path(snap_src->fs_info->tree_root, path);
338 test = test->d_parent;
339 }
340create:
341 btrfs_free_path(path);
Chris Mason3de45862008-11-17 21:02:50 -0500342 error = create_snapshot(snap_src, dentry, name, namelen);
343 } else {
344 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
345 dentry, name, namelen);
346 }
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400347 if (error)
348 goto out_drop_write;
349
350 fsnotify_mkdir(parent->dentry->d_inode, dentry);
351out_drop_write:
352 mnt_drop_write(parent->mnt);
353out_dput:
354 dput(dentry);
355out_unlock:
356 mutex_unlock(&parent->dentry->d_inode->i_mutex);
357 return error;
358}
359
360
Christoph Hellwigb2950862008-12-02 09:54:17 -0500361static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400362{
363 struct inode *inode = fdentry(file)->d_inode;
364 struct btrfs_root *root = BTRFS_I(inode)->root;
365 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400366 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400367 struct page *page;
368 unsigned long last_index;
369 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
370 unsigned long total_read = 0;
371 u64 page_start;
372 u64 page_end;
373 unsigned long i;
374 int ret;
375
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376 ret = btrfs_check_free_space(root, inode->i_size, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377 if (ret)
378 return -ENOSPC;
379
380 mutex_lock(&inode->i_mutex);
381 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
382 for (i = 0; i <= last_index; i++) {
383 if (total_read % ra_pages == 0) {
384 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
385 min(last_index, i + ra_pages - 1));
386 }
387 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400388again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 page = grab_cache_page(inode->i_mapping, i);
390 if (!page)
391 goto out_unlock;
392 if (!PageUptodate(page)) {
393 btrfs_readpage(NULL, page);
394 lock_page(page);
395 if (!PageUptodate(page)) {
396 unlock_page(page);
397 page_cache_release(page);
398 goto out_unlock;
399 }
400 }
401
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403
404 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
405 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400407
408 ordered = btrfs_lookup_ordered_extent(inode, page_start);
409 if (ordered) {
410 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
411 unlock_page(page);
412 page_cache_release(page);
413 btrfs_start_ordered_extent(inode, ordered, 1);
414 btrfs_put_ordered_extent(ordered);
415 goto again;
416 }
417 set_page_extent_mapped(page);
418
Chris Masonf87f0572008-08-01 11:27:23 -0400419 /*
420 * this makes sure page_mkwrite is called on the
421 * page if it is dirtied again later
422 */
423 clear_page_dirty_for_io(page);
424
Chris Masonea8c2812008-08-04 23:17:27 -0400425 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426
427 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
428 set_page_dirty(page);
429 unlock_page(page);
430 page_cache_release(page);
431 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
432 }
433
434out_unlock:
435 mutex_unlock(&inode->i_mutex);
436 return 0;
437}
438
439/*
440 * Called inside transaction, so use GFP_NOFS
441 */
442
443static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
444{
445 u64 new_size;
446 u64 old_size;
447 u64 devid = 1;
448 struct btrfs_ioctl_vol_args *vol_args;
449 struct btrfs_trans_handle *trans;
450 struct btrfs_device *device = NULL;
451 char *sizestr;
452 char *devstr = NULL;
453 int ret = 0;
454 int namelen;
455 int mod = 0;
456
Yan Zhengc146afa2008-11-12 14:34:12 -0500457 if (root->fs_info->sb->s_flags & MS_RDONLY)
458 return -EROFS;
459
Chris Masone441d542009-01-05 16:57:23 -0500460 if (!capable(CAP_SYS_ADMIN))
461 return -EPERM;
462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
464
465 if (!vol_args)
466 return -ENOMEM;
467
468 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
469 ret = -EFAULT;
470 goto out;
471 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400472
473 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475
Chris Mason7d9eb122008-07-08 14:19:17 -0400476 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477 sizestr = vol_args->name;
478 devstr = strchr(sizestr, ':');
479 if (devstr) {
480 char *end;
481 sizestr = devstr + 1;
482 *devstr = '\0';
483 devstr = vol_args->name;
484 devid = simple_strtoull(devstr, &end, 10);
485 printk(KERN_INFO "resizing devid %llu\n", devid);
486 }
Yan Zheng2b820322008-11-17 21:11:30 -0500487 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488 if (!device) {
489 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
490 ret = -EINVAL;
491 goto out_unlock;
492 }
493 if (!strcmp(sizestr, "max"))
494 new_size = device->bdev->bd_inode->i_size;
495 else {
496 if (sizestr[0] == '-') {
497 mod = -1;
498 sizestr++;
499 } else if (sizestr[0] == '+') {
500 mod = 1;
501 sizestr++;
502 }
503 new_size = btrfs_parse_size(sizestr);
504 if (new_size == 0) {
505 ret = -EINVAL;
506 goto out_unlock;
507 }
508 }
509
510 old_size = device->total_bytes;
511
512 if (mod < 0) {
513 if (new_size > old_size) {
514 ret = -EINVAL;
515 goto out_unlock;
516 }
517 new_size = old_size - new_size;
518 } else if (mod > 0) {
519 new_size = old_size + new_size;
520 }
521
522 if (new_size < 256 * 1024 * 1024) {
523 ret = -EINVAL;
524 goto out_unlock;
525 }
526 if (new_size > device->bdev->bd_inode->i_size) {
527 ret = -EFBIG;
528 goto out_unlock;
529 }
530
531 do_div(new_size, root->sectorsize);
532 new_size *= root->sectorsize;
533
534 printk(KERN_INFO "new size for %s is %llu\n",
535 device->name, (unsigned long long)new_size);
536
537 if (new_size > old_size) {
538 trans = btrfs_start_transaction(root, 1);
539 ret = btrfs_grow_device(trans, device, new_size);
540 btrfs_commit_transaction(trans, root);
541 } else {
542 ret = btrfs_shrink_device(device, new_size);
543 }
544
545out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400546 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400547out:
548 kfree(vol_args);
549 return ret;
550}
551
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400552static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500553 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400555 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400556 struct btrfs_ioctl_vol_args *vol_args;
557 struct btrfs_dir_item *di;
558 struct btrfs_path *path;
Chris Mason3de45862008-11-17 21:02:50 -0500559 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 u64 root_dirid;
561 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500562 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563
Yan Zhengc146afa2008-11-12 14:34:12 -0500564 if (root->fs_info->sb->s_flags & MS_RDONLY)
565 return -EROFS;
566
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
568
569 if (!vol_args)
570 return -ENOMEM;
571
572 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
573 ret = -EFAULT;
574 goto out;
575 }
576
Mark Fasheh5516e592008-07-24 12:20:14 -0400577 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400578 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400579 if (strchr(vol_args->name, '/')) {
580 ret = -EINVAL;
581 goto out;
582 }
583
584 path = btrfs_alloc_path();
585 if (!path) {
586 ret = -ENOMEM;
587 goto out;
588 }
589
590 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400591 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
592 path, root_dirid,
593 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 btrfs_free_path(path);
595
596 if (di && !IS_ERR(di)) {
597 ret = -EEXIST;
598 goto out;
599 }
600
601 if (IS_ERR(di)) {
602 ret = PTR_ERR(di);
603 goto out;
604 }
605
Chris Mason3de45862008-11-17 21:02:50 -0500606 if (subvol) {
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400607 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
608 file->f_path.dentry->d_inode->i_mode,
Chris Mason3de45862008-11-17 21:02:50 -0500609 namelen, NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400610 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500611 struct inode *src_inode;
612 src_file = fget(vol_args->fd);
613 if (!src_file) {
614 ret = -EINVAL;
615 goto out;
616 }
617
618 src_inode = src_file->f_path.dentry->d_inode;
619 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
620 printk("btrfs: Snapshot src from another FS\n");
621 ret = -EINVAL;
622 fput(src_file);
623 goto out;
624 }
625 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
626 file->f_path.dentry->d_inode->i_mode,
627 namelen, BTRFS_I(src_inode)->root);
628 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400629 }
630
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631out:
632 kfree(vol_args);
633 return ret;
634}
635
636static int btrfs_ioctl_defrag(struct file *file)
637{
638 struct inode *inode = fdentry(file)->d_inode;
639 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500640 int ret;
641
642 ret = mnt_want_write(file->f_path.mnt);
643 if (ret)
644 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400645
646 switch (inode->i_mode & S_IFMT) {
647 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500648 if (!capable(CAP_SYS_ADMIN)) {
649 ret = -EPERM;
650 goto out;
651 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652 btrfs_defrag_root(root, 0);
653 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654 break;
655 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500656 if (!(file->f_mode & FMODE_WRITE)) {
657 ret = -EINVAL;
658 goto out;
659 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660 btrfs_defrag_file(file);
661 break;
662 }
Chris Masone441d542009-01-05 16:57:23 -0500663out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500664 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500665 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666}
667
Christoph Hellwigb2950862008-12-02 09:54:17 -0500668static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400669{
670 struct btrfs_ioctl_vol_args *vol_args;
671 int ret;
672
Chris Masone441d542009-01-05 16:57:23 -0500673 if (!capable(CAP_SYS_ADMIN))
674 return -EPERM;
675
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400676 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
677
678 if (!vol_args)
679 return -ENOMEM;
680
681 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
682 ret = -EFAULT;
683 goto out;
684 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400685 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400686 ret = btrfs_init_new_device(root, vol_args->name);
687
688out:
689 kfree(vol_args);
690 return ret;
691}
692
Christoph Hellwigb2950862008-12-02 09:54:17 -0500693static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400694{
695 struct btrfs_ioctl_vol_args *vol_args;
696 int ret;
697
Chris Masone441d542009-01-05 16:57:23 -0500698 if (!capable(CAP_SYS_ADMIN))
699 return -EPERM;
700
Yan Zhengc146afa2008-11-12 14:34:12 -0500701 if (root->fs_info->sb->s_flags & MS_RDONLY)
702 return -EROFS;
703
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400704 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
705
706 if (!vol_args)
707 return -ENOMEM;
708
709 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
710 ret = -EFAULT;
711 goto out;
712 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400713 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400714 ret = btrfs_rm_device(root, vol_args->name);
715
716out:
717 kfree(vol_args);
718 return ret;
719}
720
Christoph Hellwigb2950862008-12-02 09:54:17 -0500721static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
722 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723{
724 struct inode *inode = fdentry(file)->d_inode;
725 struct btrfs_root *root = BTRFS_I(inode)->root;
726 struct file *src_file;
727 struct inode *src;
728 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400730 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400731 char *buf;
732 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733 u32 nritems;
734 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400735 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500736 u64 len = olen;
737 u64 bs = root->fs_info->sb->s_blocksize;
738 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -0500739
Sage Weilc5c9cd42008-11-12 14:32:25 -0500740 /*
741 * TODO:
742 * - split compressed inline extents. annoying: we need to
743 * decompress into destination's address_space (the file offset
744 * may change, so source mapping won't do), then recompress (or
745 * otherwise reinsert) a subrange.
746 * - allow ranges within the same file to be cloned (provided
747 * they don't overlap)?
748 */
749
Chris Masone441d542009-01-05 16:57:23 -0500750 /* the destination must be opened for writing */
751 if (!(file->f_mode & FMODE_WRITE))
752 return -EINVAL;
753
Yan Zhengc146afa2008-11-12 14:34:12 -0500754 ret = mnt_want_write(file->f_path.mnt);
755 if (ret)
756 return ret;
757
Sage Weilc5c9cd42008-11-12 14:32:25 -0500758 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500759 if (!src_file) {
760 ret = -EBADF;
761 goto out_drop_write;
762 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 src = src_file->f_dentry->d_inode;
764
Sage Weilc5c9cd42008-11-12 14:32:25 -0500765 ret = -EINVAL;
766 if (src == inode)
767 goto out_fput;
768
Yan Zhengae01a0a2008-08-04 23:23:47 -0400769 ret = -EISDIR;
770 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400771 goto out_fput;
772
Yan Zhengae01a0a2008-08-04 23:23:47 -0400773 ret = -EXDEV;
774 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
775 goto out_fput;
776
777 ret = -ENOMEM;
778 buf = vmalloc(btrfs_level_size(root, 0));
779 if (!buf)
780 goto out_fput;
781
782 path = btrfs_alloc_path();
783 if (!path) {
784 vfree(buf);
785 goto out_fput;
786 }
787 path->reada = 2;
788
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400789 if (inode < src) {
790 mutex_lock(&inode->i_mutex);
791 mutex_lock(&src->i_mutex);
792 } else {
793 mutex_lock(&src->i_mutex);
794 mutex_lock(&inode->i_mutex);
795 }
796
Sage Weilc5c9cd42008-11-12 14:32:25 -0500797 /* determine range to clone */
798 ret = -EINVAL;
799 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400800 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500801 if (len == 0)
802 olen = len = src->i_size - off;
803 /* if we extend to eof, continue to block boundary */
804 if (off + len == src->i_size)
805 len = ((src->i_size + bs-1) & ~(bs-1))
806 - off;
807
808 /* verify the end result is block aligned */
809 if ((off & (bs-1)) ||
810 ((off + len) & (bs-1)))
811 goto out_unlock;
812
813 printk("final src extent is %llu~%llu\n", off, len);
814 printk("final dst extent is %llu~%llu\n", destoff, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400815
816 /* do any pending delalloc/csum calc on src, one way or
817 another, and lock file content */
818 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400819 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500820 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
821 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400822 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400823 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500824 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400825 if (ordered)
826 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500827 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400828 }
829
Yan Zhengae01a0a2008-08-04 23:23:47 -0400830 trans = btrfs_start_transaction(root, 1);
831 BUG_ON(!trans);
832
Sage Weilc5c9cd42008-11-12 14:32:25 -0500833 /* punch hole in destination first */
834 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
835
836 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400837 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400838 key.type = BTRFS_EXTENT_DATA_KEY;
839 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400840
841 while (1) {
842 /*
843 * note the key will change type as we walk through the
844 * tree.
845 */
846 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
847 if (ret < 0)
848 goto out;
849
Yan Zhengae01a0a2008-08-04 23:23:47 -0400850 nritems = btrfs_header_nritems(path->nodes[0]);
851 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400852 ret = btrfs_next_leaf(root, path);
853 if (ret < 0)
854 goto out;
855 if (ret > 0)
856 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400857 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 }
859 leaf = path->nodes[0];
860 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861
Yan Zhengae01a0a2008-08-04 23:23:47 -0400862 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500863 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 key.objectid != src->i_ino)
865 break;
866
Sage Weilc5c9cd42008-11-12 14:32:25 -0500867 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
868 struct btrfs_file_extent_item *extent;
869 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -0400870 u32 size;
871 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500872 u64 disko = 0, diskl = 0;
873 u64 datao = 0, datal = 0;
874 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -0400875
876 size = btrfs_item_size_nr(leaf, slot);
877 read_extent_buffer(leaf, buf,
878 btrfs_item_ptr_offset(leaf, slot),
879 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500880
881 extent = btrfs_item_ptr(leaf, slot,
882 struct btrfs_file_extent_item);
883 comp = btrfs_file_extent_compression(leaf, extent);
884 type = btrfs_file_extent_type(leaf, extent);
885 if (type == BTRFS_FILE_EXTENT_REG) {
886 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
887 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
888 datao = btrfs_file_extent_offset(leaf, extent);
889 datal = btrfs_file_extent_num_bytes(leaf, extent);
890 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
891 /* take upper bound, may be compressed */
892 datal = btrfs_file_extent_ram_bytes(leaf,
893 extent);
894 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400895 btrfs_release_path(root, path);
896
Sage Weilc5c9cd42008-11-12 14:32:25 -0500897 if (key.offset + datal < off ||
898 key.offset >= off+len)
899 goto next;
900
Zheng Yan31840ae2008-09-23 13:14:14 -0400901 memcpy(&new_key, &key, sizeof(new_key));
902 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500903 new_key.offset = key.offset + destoff - off;
904
905 if (type == BTRFS_FILE_EXTENT_REG) {
906 ret = btrfs_insert_empty_item(trans, root, path,
907 &new_key, size);
908 if (ret)
909 goto out;
910
911 leaf = path->nodes[0];
912 slot = path->slots[0];
913 write_extent_buffer(leaf, buf,
914 btrfs_item_ptr_offset(leaf, slot),
915 size);
916
917 extent = btrfs_item_ptr(leaf, slot,
918 struct btrfs_file_extent_item);
919 printk(" orig disk %llu~%llu data %llu~%llu\n",
920 disko, diskl, datao, datal);
921
922 if (off > key.offset) {
923 datao += off - key.offset;
924 datal -= off - key.offset;
925 }
926 if (key.offset + datao + datal + key.offset >
927 off + len)
928 datal = off + len - key.offset - datao;
929 /* disko == 0 means it's a hole */
930 if (!disko)
931 datao = 0;
932 printk(" final disk %llu~%llu data %llu~%llu\n",
933 disko, diskl, datao, datal);
934
935 btrfs_set_file_extent_offset(leaf, extent,
936 datao);
937 btrfs_set_file_extent_num_bytes(leaf, extent,
938 datal);
939 if (disko) {
940 inode_add_bytes(inode, datal);
941 ret = btrfs_inc_extent_ref(trans, root,
942 disko, diskl, leaf->start,
943 root->root_key.objectid,
944 trans->transid,
945 inode->i_ino);
946 BUG_ON(ret);
947 }
948 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
949 u64 skip = 0;
950 u64 trim = 0;
951 if (off > key.offset) {
952 skip = off - key.offset;
953 new_key.offset += skip;
954 }
955 if (key.offset + datal > off+len)
956 trim = key.offset + datal - (off+len);
957 printk("len %lld skip %lld trim %lld\n",
958 datal, skip, trim);
959 if (comp && (skip || trim)) {
960 printk("btrfs clone_range can't split compressed inline extents yet\n");
961 ret = -EINVAL;
962 goto out;
963 }
964 size -= skip + trim;
965 datal -= skip + trim;
966 ret = btrfs_insert_empty_item(trans, root, path,
967 &new_key, size);
968 if (ret)
969 goto out;
970
971 if (skip) {
972 u32 start = btrfs_file_extent_calc_inline_size(0);
973 memmove(buf+start, buf+start+skip,
974 datal);
975 }
976
977 leaf = path->nodes[0];
978 slot = path->slots[0];
979 write_extent_buffer(leaf, buf,
980 btrfs_item_ptr_offset(leaf, slot),
981 size);
982 inode_add_bytes(inode, datal);
983 }
984
985 btrfs_mark_buffer_dirty(leaf);
986 }
987
Sage Weilc5c9cd42008-11-12 14:32:25 -0500988 next:
Zheng Yan31840ae2008-09-23 13:14:14 -0400989 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400990 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400991 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400992 ret = 0;
993out:
Yan Zhengae01a0a2008-08-04 23:23:47 -0400994 btrfs_release_path(root, path);
995 if (ret == 0) {
996 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500997 if (destoff + olen > inode->i_size)
998 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400999 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1000 ret = btrfs_update_inode(trans, root, inode);
1001 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001002 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001003 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001004 if (ret)
1005 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006out_unlock:
1007 mutex_unlock(&src->i_mutex);
1008 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001009 vfree(buf);
1010 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001011out_fput:
1012 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001013out_drop_write:
1014 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001015 return ret;
1016}
1017
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001018static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001019{
1020 struct btrfs_ioctl_clone_range_args args;
1021
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001022 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001023 return -EFAULT;
1024 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1025 args.src_length, args.dest_offset);
1026}
1027
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001028/*
1029 * there are many ways the trans_start and trans_end ioctls can lead
1030 * to deadlocks. They should only be used by applications that
1031 * basically own the machine, and have a very in depth understanding
1032 * of all the possible deadlocks and enospc problems.
1033 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001034static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001035{
1036 struct inode *inode = fdentry(file)->d_inode;
1037 struct btrfs_root *root = BTRFS_I(inode)->root;
1038 struct btrfs_trans_handle *trans;
1039 int ret = 0;
1040
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001041 if (!capable(CAP_SYS_ADMIN))
1042 return -EPERM;
1043
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001044 if (file->private_data) {
1045 ret = -EINPROGRESS;
1046 goto out;
1047 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04001048
Yan Zhengc146afa2008-11-12 14:34:12 -05001049 ret = mnt_want_write(file->f_path.mnt);
1050 if (ret)
1051 goto out;
1052
Sage Weil9ca9ee02008-08-04 10:41:27 -04001053 mutex_lock(&root->fs_info->trans_mutex);
1054 root->fs_info->open_ioctl_trans++;
1055 mutex_unlock(&root->fs_info->trans_mutex);
1056
1057 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001058 if (trans)
1059 file->private_data = trans;
1060 else
1061 ret = -ENOMEM;
1062 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1063out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001064 return ret;
1065}
1066
1067/*
1068 * there are many ways the trans_start and trans_end ioctls can lead
1069 * to deadlocks. They should only be used by applications that
1070 * basically own the machine, and have a very in depth understanding
1071 * of all the possible deadlocks and enospc problems.
1072 */
1073long btrfs_ioctl_trans_end(struct file *file)
1074{
1075 struct inode *inode = fdentry(file)->d_inode;
1076 struct btrfs_root *root = BTRFS_I(inode)->root;
1077 struct btrfs_trans_handle *trans;
1078 int ret = 0;
1079
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001080 trans = file->private_data;
1081 if (!trans) {
1082 ret = -EINVAL;
1083 goto out;
1084 }
1085 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -04001086 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001087
1088 mutex_lock(&root->fs_info->trans_mutex);
1089 root->fs_info->open_ioctl_trans--;
1090 mutex_unlock(&root->fs_info->trans_mutex);
1091
Sage Weilcfc8ea82008-12-11 16:30:06 -05001092 mnt_drop_write(file->f_path.mnt);
1093
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001094out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001095 return ret;
1096}
1097
1098long btrfs_ioctl(struct file *file, unsigned int
1099 cmd, unsigned long arg)
1100{
1101 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001102 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001103
1104 switch (cmd) {
1105 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001106 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001107 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001108 return btrfs_ioctl_snap_create(file, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001109 case BTRFS_IOC_DEFRAG:
1110 return btrfs_ioctl_defrag(file);
1111 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001112 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001113 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001114 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001115 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001116 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001117 case BTRFS_IOC_BALANCE:
1118 return btrfs_balance(root->fs_info->dev_root);
1119 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001120 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1121 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001122 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001123 case BTRFS_IOC_TRANS_START:
1124 return btrfs_ioctl_trans_start(file);
1125 case BTRFS_IOC_TRANS_END:
1126 return btrfs_ioctl_trans_end(file);
1127 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001128 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1129 return 0;
1130 }
1131
1132 return -ENOTTY;
1133}