blob: 8828109fa58efa9475d52cb2383015e71eadcfd0 [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>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040044#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 Mason925baed2008-06-25 16:01:30 -040051#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
53
54
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040055static noinline int create_subvol(struct btrfs_root *root,
56 struct dentry *dentry,
57 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040058{
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;
Chris Mason3de45862008-11-17 21:02:50 -050070 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040071 unsigned long nr = 1;
72
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040073 ret = btrfs_check_free_space(root, 1, 0);
74 if (ret)
75 goto fail_commit;
76
77 trans = btrfs_start_transaction(root, 1);
78 BUG_ON(!trans);
79
80 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
81 0, &objectid);
82 if (ret)
83 goto fail;
84
Zheng Yan31840ae2008-09-23 13:14:14 -040085 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
86 objectid, trans->transid, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -040087 if (IS_ERR(leaf)) {
88 ret = PTR_ERR(leaf);
89 goto fail;
90 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040091
92 btrfs_set_header_nritems(leaf, 0);
93 btrfs_set_header_level(leaf, 0);
94 btrfs_set_header_bytenr(leaf, leaf->start);
95 btrfs_set_header_generation(leaf, trans->transid);
96 btrfs_set_header_owner(leaf, objectid);
97
98 write_extent_buffer(leaf, root->fs_info->fsid,
99 (unsigned long)btrfs_header_fsid(leaf),
100 BTRFS_FSID_SIZE);
101 btrfs_mark_buffer_dirty(leaf);
102
103 inode_item = &root_item.inode;
104 memset(inode_item, 0, sizeof(*inode_item));
105 inode_item->generation = cpu_to_le64(1);
106 inode_item->size = cpu_to_le64(3);
107 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400108 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400109 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
110
111 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400112 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400113 btrfs_set_root_level(&root_item, 0);
114 btrfs_set_root_refs(&root_item, 1);
115 btrfs_set_root_used(&root_item, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400116 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400117
118 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
119 root_item.drop_level = 0;
120
Chris Mason925baed2008-06-25 16:01:30 -0400121 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400122 free_extent_buffer(leaf);
123 leaf = NULL;
124
125 btrfs_set_root_dirid(&root_item, new_dirid);
126
127 key.objectid = objectid;
128 key.offset = 1;
129 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
130 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
131 &root_item);
132 if (ret)
133 goto fail;
134
135 /*
136 * insert the directory item
137 */
138 key.offset = (u64)-1;
Chris Mason3de45862008-11-17 21:02:50 -0500139 dir = dentry->d_parent->d_inode;
140 ret = btrfs_set_inode_index(dir, &index);
141 BUG_ON(ret);
142
143 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400144 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500145 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400146 if (ret)
147 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500148
149 /* add the backref first */
150 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
151 objectid, BTRFS_ROOT_BACKREF_KEY,
152 root->root_key.objectid,
153 dir->i_ino, index, name, namelen);
154
155 BUG_ON(ret);
156
157 /* now add the forward ref */
158 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
159 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
160 objectid,
161 dir->i_ino, index, name, namelen);
162
163 BUG_ON(ret);
164
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400165 ret = btrfs_commit_transaction(trans, root);
166 if (ret)
167 goto fail_commit;
168
Chris Mason3de45862008-11-17 21:02:50 -0500169 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400170 BUG_ON(!new_root);
171
172 trans = btrfs_start_transaction(new_root, 1);
173 BUG_ON(!trans);
174
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400175 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400176 BTRFS_I(dir)->block_group);
177 if (ret)
178 goto fail;
179
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400180fail:
181 nr = trans->blocks_used;
182 err = btrfs_commit_transaction(trans, new_root);
183 if (err && !ret)
184 ret = err;
185fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400186 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400187 return ret;
188}
189
Chris Mason3de45862008-11-17 21:02:50 -0500190static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
191 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400192{
193 struct btrfs_pending_snapshot *pending_snapshot;
194 struct btrfs_trans_handle *trans;
Chris Mason3de45862008-11-17 21:02:50 -0500195 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400196 int err;
197 unsigned long nr = 0;
198
199 if (!root->ref_cows)
200 return -EINVAL;
201
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400202 ret = btrfs_check_free_space(root, 1, 0);
203 if (ret)
204 goto fail_unlock;
205
Chris Mason3de45862008-11-17 21:02:50 -0500206 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400207 if (!pending_snapshot) {
208 ret = -ENOMEM;
209 goto fail_unlock;
210 }
211 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
212 if (!pending_snapshot->name) {
213 ret = -ENOMEM;
214 kfree(pending_snapshot);
215 goto fail_unlock;
216 }
217 memcpy(pending_snapshot->name, name, namelen);
218 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500219 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400220 trans = btrfs_start_transaction(root, 1);
221 BUG_ON(!trans);
222 pending_snapshot->root = root;
223 list_add(&pending_snapshot->list,
224 &trans->transaction->pending_snapshots);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400225 err = btrfs_commit_transaction(trans, root);
226
227fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400228 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400229 return ret;
230}
231
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400232/* copy of may_create in fs/namei.c() */
233static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
234{
235 if (child->d_inode)
236 return -EEXIST;
237 if (IS_DEADDIR(dir))
238 return -ENOENT;
239 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
240}
241
242/*
243 * Create a new subvolume below @parent. This is largely modeled after
244 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
245 * inside this filesystem so it's quite a bit simpler.
246 */
247static noinline int btrfs_mksubvol(struct path *parent, char *name,
Chris Mason3de45862008-11-17 21:02:50 -0500248 int mode, int namelen,
249 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400250{
251 struct dentry *dentry;
252 int error;
253
254 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
255
256 dentry = lookup_one_len(name, parent->dentry, namelen);
257 error = PTR_ERR(dentry);
258 if (IS_ERR(dentry))
259 goto out_unlock;
260
261 error = -EEXIST;
262 if (dentry->d_inode)
263 goto out_dput;
264
265 if (!IS_POSIXACL(parent->dentry->d_inode))
266 mode &= ~current->fs->umask;
Chris Mason3de45862008-11-17 21:02:50 -0500267
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400268 error = mnt_want_write(parent->mnt);
269 if (error)
270 goto out_dput;
271
272 error = btrfs_may_create(parent->dentry->d_inode, dentry);
273 if (error)
274 goto out_drop_write;
275
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400276 /*
277 * Actually perform the low-level subvolume creation after all
278 * this VFS fuzz.
279 *
280 * Eventually we want to pass in an inode under which we create this
281 * subvolume, but for now all are under the filesystem root.
282 *
283 * Also we should pass on the mode eventually to allow creating new
284 * subvolume with specific mode bits.
285 */
Chris Mason3de45862008-11-17 21:02:50 -0500286 if (snap_src) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500287 struct dentry *dir = dentry->d_parent;
288 struct dentry *test = dir->d_parent;
289 struct btrfs_path *path = btrfs_alloc_path();
290 int ret;
291 u64 test_oid;
292 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
293
294 test_oid = snap_src->root_key.objectid;
295
296 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
297 path, parent_oid, test_oid);
298 if (ret == 0)
299 goto create;
300 btrfs_release_path(snap_src->fs_info->tree_root, path);
301
302 /* we need to make sure we aren't creating a directory loop
303 * by taking a snapshot of something that has our current
304 * subvol in its directory tree. So, this loops through
305 * the dentries and checks the forward refs for each subvolume
306 * to see if is references the subvolume where we are
307 * placing this new snapshot.
308 */
309 while(1) {
310 if (!test ||
311 dir == snap_src->fs_info->sb->s_root ||
312 test == snap_src->fs_info->sb->s_root ||
313 test->d_inode->i_sb != snap_src->fs_info->sb) {
314 break;
315 }
316 if (S_ISLNK(test->d_inode->i_mode)) {
317 printk("Symlink in snapshot path, failed\n");
318 error = -EMLINK;
319 btrfs_free_path(path);
320 goto out_drop_write;
321 }
322 test_oid =
323 BTRFS_I(test->d_inode)->root->root_key.objectid;
324 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
325 path, test_oid, parent_oid);
326 if (ret == 0) {
327 printk("Snapshot creation failed, looping\n");
328 error = -EMLINK;
329 btrfs_free_path(path);
330 goto out_drop_write;
331 }
332 btrfs_release_path(snap_src->fs_info->tree_root, path);
333 test = test->d_parent;
334 }
335create:
336 btrfs_free_path(path);
Chris Mason3de45862008-11-17 21:02:50 -0500337 error = create_snapshot(snap_src, dentry, name, namelen);
338 } else {
339 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
340 dentry, name, namelen);
341 }
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400342 if (error)
343 goto out_drop_write;
344
345 fsnotify_mkdir(parent->dentry->d_inode, dentry);
346out_drop_write:
347 mnt_drop_write(parent->mnt);
348out_dput:
349 dput(dentry);
350out_unlock:
351 mutex_unlock(&parent->dentry->d_inode->i_mutex);
352 return error;
353}
354
355
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400356int btrfs_defrag_file(struct file *file)
357{
358 struct inode *inode = fdentry(file)->d_inode;
359 struct btrfs_root *root = BTRFS_I(inode)->root;
360 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400361 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400362 struct page *page;
363 unsigned long last_index;
364 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
365 unsigned long total_read = 0;
366 u64 page_start;
367 u64 page_end;
368 unsigned long i;
369 int ret;
370
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 ret = btrfs_check_free_space(root, inode->i_size, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372 if (ret)
373 return -ENOSPC;
374
375 mutex_lock(&inode->i_mutex);
376 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
377 for (i = 0; i <= last_index; i++) {
378 if (total_read % ra_pages == 0) {
379 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
380 min(last_index, i + ra_pages - 1));
381 }
382 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400383again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400384 page = grab_cache_page(inode->i_mapping, i);
385 if (!page)
386 goto out_unlock;
387 if (!PageUptodate(page)) {
388 btrfs_readpage(NULL, page);
389 lock_page(page);
390 if (!PageUptodate(page)) {
391 unlock_page(page);
392 page_cache_release(page);
393 goto out_unlock;
394 }
395 }
396
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398
399 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
400 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400401 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400402
403 ordered = btrfs_lookup_ordered_extent(inode, page_start);
404 if (ordered) {
405 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
406 unlock_page(page);
407 page_cache_release(page);
408 btrfs_start_ordered_extent(inode, ordered, 1);
409 btrfs_put_ordered_extent(ordered);
410 goto again;
411 }
412 set_page_extent_mapped(page);
413
Chris Masonf87f0572008-08-01 11:27:23 -0400414 /*
415 * this makes sure page_mkwrite is called on the
416 * page if it is dirtied again later
417 */
418 clear_page_dirty_for_io(page);
419
Chris Masonea8c2812008-08-04 23:17:27 -0400420 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
422 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
423 set_page_dirty(page);
424 unlock_page(page);
425 page_cache_release(page);
426 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
427 }
428
429out_unlock:
430 mutex_unlock(&inode->i_mutex);
431 return 0;
432}
433
434/*
435 * Called inside transaction, so use GFP_NOFS
436 */
437
438static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
439{
440 u64 new_size;
441 u64 old_size;
442 u64 devid = 1;
443 struct btrfs_ioctl_vol_args *vol_args;
444 struct btrfs_trans_handle *trans;
445 struct btrfs_device *device = NULL;
446 char *sizestr;
447 char *devstr = NULL;
448 int ret = 0;
449 int namelen;
450 int mod = 0;
451
Yan Zhengc146afa2008-11-12 14:34:12 -0500452 if (root->fs_info->sb->s_flags & MS_RDONLY)
453 return -EROFS;
454
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
456
457 if (!vol_args)
458 return -ENOMEM;
459
460 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
461 ret = -EFAULT;
462 goto out;
463 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400464
465 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400467
Chris Mason7d9eb122008-07-08 14:19:17 -0400468 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400469 sizestr = vol_args->name;
470 devstr = strchr(sizestr, ':');
471 if (devstr) {
472 char *end;
473 sizestr = devstr + 1;
474 *devstr = '\0';
475 devstr = vol_args->name;
476 devid = simple_strtoull(devstr, &end, 10);
477 printk(KERN_INFO "resizing devid %llu\n", devid);
478 }
Yan Zheng2b820322008-11-17 21:11:30 -0500479 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400480 if (!device) {
481 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
482 ret = -EINVAL;
483 goto out_unlock;
484 }
485 if (!strcmp(sizestr, "max"))
486 new_size = device->bdev->bd_inode->i_size;
487 else {
488 if (sizestr[0] == '-') {
489 mod = -1;
490 sizestr++;
491 } else if (sizestr[0] == '+') {
492 mod = 1;
493 sizestr++;
494 }
495 new_size = btrfs_parse_size(sizestr);
496 if (new_size == 0) {
497 ret = -EINVAL;
498 goto out_unlock;
499 }
500 }
501
502 old_size = device->total_bytes;
503
504 if (mod < 0) {
505 if (new_size > old_size) {
506 ret = -EINVAL;
507 goto out_unlock;
508 }
509 new_size = old_size - new_size;
510 } else if (mod > 0) {
511 new_size = old_size + new_size;
512 }
513
514 if (new_size < 256 * 1024 * 1024) {
515 ret = -EINVAL;
516 goto out_unlock;
517 }
518 if (new_size > device->bdev->bd_inode->i_size) {
519 ret = -EFBIG;
520 goto out_unlock;
521 }
522
523 do_div(new_size, root->sectorsize);
524 new_size *= root->sectorsize;
525
526 printk(KERN_INFO "new size for %s is %llu\n",
527 device->name, (unsigned long long)new_size);
528
529 if (new_size > old_size) {
530 trans = btrfs_start_transaction(root, 1);
531 ret = btrfs_grow_device(trans, device, new_size);
532 btrfs_commit_transaction(trans, root);
533 } else {
534 ret = btrfs_shrink_device(device, new_size);
535 }
536
537out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400538 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539out:
540 kfree(vol_args);
541 return ret;
542}
543
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400544static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500545 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400547 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548 struct btrfs_ioctl_vol_args *vol_args;
549 struct btrfs_dir_item *di;
550 struct btrfs_path *path;
Chris Mason3de45862008-11-17 21:02:50 -0500551 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552 u64 root_dirid;
553 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500554 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555
Yan Zhengc146afa2008-11-12 14:34:12 -0500556 if (root->fs_info->sb->s_flags & MS_RDONLY)
557 return -EROFS;
558
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400559 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 }
568
Mark Fasheh5516e592008-07-24 12:20:14 -0400569 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571 if (strchr(vol_args->name, '/')) {
572 ret = -EINVAL;
573 goto out;
574 }
575
576 path = btrfs_alloc_path();
577 if (!path) {
578 ret = -ENOMEM;
579 goto out;
580 }
581
582 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400583 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
584 path, root_dirid,
585 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400586 btrfs_free_path(path);
587
588 if (di && !IS_ERR(di)) {
589 ret = -EEXIST;
590 goto out;
591 }
592
593 if (IS_ERR(di)) {
594 ret = PTR_ERR(di);
595 goto out;
596 }
597
Chris Mason3de45862008-11-17 21:02:50 -0500598 if (subvol) {
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400599 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
600 file->f_path.dentry->d_inode->i_mode,
Chris Mason3de45862008-11-17 21:02:50 -0500601 namelen, NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400602 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500603 struct inode *src_inode;
604 src_file = fget(vol_args->fd);
605 if (!src_file) {
606 ret = -EINVAL;
607 goto out;
608 }
609
610 src_inode = src_file->f_path.dentry->d_inode;
611 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
612 printk("btrfs: Snapshot src from another FS\n");
613 ret = -EINVAL;
614 fput(src_file);
615 goto out;
616 }
617 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
618 file->f_path.dentry->d_inode->i_mode,
619 namelen, BTRFS_I(src_inode)->root);
620 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400621 }
622
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623out:
624 kfree(vol_args);
625 return ret;
626}
627
628static int btrfs_ioctl_defrag(struct file *file)
629{
630 struct inode *inode = fdentry(file)->d_inode;
631 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500632 int ret;
633
634 ret = mnt_want_write(file->f_path.mnt);
635 if (ret)
636 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400637
638 switch (inode->i_mode & S_IFMT) {
639 case S_IFDIR:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640 btrfs_defrag_root(root, 0);
641 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400642 break;
643 case S_IFREG:
644 btrfs_defrag_file(file);
645 break;
646 }
647
648 return 0;
649}
650
651long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
652{
653 struct btrfs_ioctl_vol_args *vol_args;
654 int ret;
655
656 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
657
658 if (!vol_args)
659 return -ENOMEM;
660
661 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
662 ret = -EFAULT;
663 goto out;
664 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400665 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666 ret = btrfs_init_new_device(root, vol_args->name);
667
668out:
669 kfree(vol_args);
670 return ret;
671}
672
673long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
674{
675 struct btrfs_ioctl_vol_args *vol_args;
676 int ret;
677
Yan Zhengc146afa2008-11-12 14:34:12 -0500678 if (root->fs_info->sb->s_flags & MS_RDONLY)
679 return -EROFS;
680
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400681 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
682
683 if (!vol_args)
684 return -ENOMEM;
685
686 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
687 ret = -EFAULT;
688 goto out;
689 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400690 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400691 ret = btrfs_rm_device(root, vol_args->name);
692
693out:
694 kfree(vol_args);
695 return ret;
696}
697
Sage Weilc5c9cd42008-11-12 14:32:25 -0500698long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, u64 off,
699 u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400700{
701 struct inode *inode = fdentry(file)->d_inode;
702 struct btrfs_root *root = BTRFS_I(inode)->root;
703 struct file *src_file;
704 struct inode *src;
705 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400706 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400707 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400708 char *buf;
709 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400710 u32 nritems;
711 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400712 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500713 u64 len = olen;
714 u64 bs = root->fs_info->sb->s_blocksize;
715 u64 hint_byte;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400716
Sage Weilc5c9cd42008-11-12 14:32:25 -0500717 /*
718 * TODO:
719 * - split compressed inline extents. annoying: we need to
720 * decompress into destination's address_space (the file offset
721 * may change, so source mapping won't do), then recompress (or
722 * otherwise reinsert) a subrange.
723 * - allow ranges within the same file to be cloned (provided
724 * they don't overlap)?
725 */
726
Yan Zhengc146afa2008-11-12 14:34:12 -0500727 ret = mnt_want_write(file->f_path.mnt);
728 if (ret)
729 return ret;
730
Sage Weilc5c9cd42008-11-12 14:32:25 -0500731 src_file = fget(srcfd);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732 if (!src_file)
733 return -EBADF;
734 src = src_file->f_dentry->d_inode;
735
Sage Weilc5c9cd42008-11-12 14:32:25 -0500736 ret = -EINVAL;
737 if (src == inode)
738 goto out_fput;
739
Yan Zhengae01a0a2008-08-04 23:23:47 -0400740 ret = -EISDIR;
741 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400742 goto out_fput;
743
Yan Zhengae01a0a2008-08-04 23:23:47 -0400744 ret = -EXDEV;
745 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
746 goto out_fput;
747
748 ret = -ENOMEM;
749 buf = vmalloc(btrfs_level_size(root, 0));
750 if (!buf)
751 goto out_fput;
752
753 path = btrfs_alloc_path();
754 if (!path) {
755 vfree(buf);
756 goto out_fput;
757 }
758 path->reada = 2;
759
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760 if (inode < src) {
761 mutex_lock(&inode->i_mutex);
762 mutex_lock(&src->i_mutex);
763 } else {
764 mutex_lock(&src->i_mutex);
765 mutex_lock(&inode->i_mutex);
766 }
767
Sage Weilc5c9cd42008-11-12 14:32:25 -0500768 /* determine range to clone */
769 ret = -EINVAL;
770 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400771 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500772 if (len == 0)
773 olen = len = src->i_size - off;
774 /* if we extend to eof, continue to block boundary */
775 if (off + len == src->i_size)
776 len = ((src->i_size + bs-1) & ~(bs-1))
777 - off;
778
779 /* verify the end result is block aligned */
780 if ((off & (bs-1)) ||
781 ((off + len) & (bs-1)))
782 goto out_unlock;
783
784 printk("final src extent is %llu~%llu\n", off, len);
785 printk("final dst extent is %llu~%llu\n", destoff, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400786
787 /* do any pending delalloc/csum calc on src, one way or
788 another, and lock file content */
789 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400790 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500791 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
792 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400793 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400794 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500795 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400796 if (ordered)
797 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500798 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400799 }
800
Yan Zhengae01a0a2008-08-04 23:23:47 -0400801 trans = btrfs_start_transaction(root, 1);
802 BUG_ON(!trans);
803
Sage Weilc5c9cd42008-11-12 14:32:25 -0500804 /* punch hole in destination first */
805 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
806
807 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400808 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400809 key.type = BTRFS_EXTENT_DATA_KEY;
810 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400811
812 while (1) {
813 /*
814 * note the key will change type as we walk through the
815 * tree.
816 */
817 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
818 if (ret < 0)
819 goto out;
820
Yan Zhengae01a0a2008-08-04 23:23:47 -0400821 nritems = btrfs_header_nritems(path->nodes[0]);
822 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400823 ret = btrfs_next_leaf(root, path);
824 if (ret < 0)
825 goto out;
826 if (ret > 0)
827 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400828 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400829 }
830 leaf = path->nodes[0];
831 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400832
Yan Zhengae01a0a2008-08-04 23:23:47 -0400833 btrfs_item_key_to_cpu(leaf, &key, slot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400834 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
835 key.objectid != src->i_ino)
836 break;
837
Sage Weilc5c9cd42008-11-12 14:32:25 -0500838 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
839 struct btrfs_file_extent_item *extent;
840 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -0400841 u32 size;
842 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500843 u64 disko = 0, diskl = 0;
844 u64 datao = 0, datal = 0;
845 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -0400846
847 size = btrfs_item_size_nr(leaf, slot);
848 read_extent_buffer(leaf, buf,
849 btrfs_item_ptr_offset(leaf, slot),
850 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500851
852 extent = btrfs_item_ptr(leaf, slot,
853 struct btrfs_file_extent_item);
854 comp = btrfs_file_extent_compression(leaf, extent);
855 type = btrfs_file_extent_type(leaf, extent);
856 if (type == BTRFS_FILE_EXTENT_REG) {
857 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
858 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
859 datao = btrfs_file_extent_offset(leaf, extent);
860 datal = btrfs_file_extent_num_bytes(leaf, extent);
861 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
862 /* take upper bound, may be compressed */
863 datal = btrfs_file_extent_ram_bytes(leaf,
864 extent);
865 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400866 btrfs_release_path(root, path);
867
Sage Weilc5c9cd42008-11-12 14:32:25 -0500868 if (key.offset + datal < off ||
869 key.offset >= off+len)
870 goto next;
871
Zheng Yan31840ae2008-09-23 13:14:14 -0400872 memcpy(&new_key, &key, sizeof(new_key));
873 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500874 new_key.offset = key.offset + destoff - off;
875
876 if (type == BTRFS_FILE_EXTENT_REG) {
877 ret = btrfs_insert_empty_item(trans, root, path,
878 &new_key, size);
879 if (ret)
880 goto out;
881
882 leaf = path->nodes[0];
883 slot = path->slots[0];
884 write_extent_buffer(leaf, buf,
885 btrfs_item_ptr_offset(leaf, slot),
886 size);
887
888 extent = btrfs_item_ptr(leaf, slot,
889 struct btrfs_file_extent_item);
890 printk(" orig disk %llu~%llu data %llu~%llu\n",
891 disko, diskl, datao, datal);
892
893 if (off > key.offset) {
894 datao += off - key.offset;
895 datal -= off - key.offset;
896 }
897 if (key.offset + datao + datal + key.offset >
898 off + len)
899 datal = off + len - key.offset - datao;
900 /* disko == 0 means it's a hole */
901 if (!disko)
902 datao = 0;
903 printk(" final disk %llu~%llu data %llu~%llu\n",
904 disko, diskl, datao, datal);
905
906 btrfs_set_file_extent_offset(leaf, extent,
907 datao);
908 btrfs_set_file_extent_num_bytes(leaf, extent,
909 datal);
910 if (disko) {
911 inode_add_bytes(inode, datal);
912 ret = btrfs_inc_extent_ref(trans, root,
913 disko, diskl, leaf->start,
914 root->root_key.objectid,
915 trans->transid,
916 inode->i_ino);
917 BUG_ON(ret);
918 }
919 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
920 u64 skip = 0;
921 u64 trim = 0;
922 if (off > key.offset) {
923 skip = off - key.offset;
924 new_key.offset += skip;
925 }
926 if (key.offset + datal > off+len)
927 trim = key.offset + datal - (off+len);
928 printk("len %lld skip %lld trim %lld\n",
929 datal, skip, trim);
930 if (comp && (skip || trim)) {
931 printk("btrfs clone_range can't split compressed inline extents yet\n");
932 ret = -EINVAL;
933 goto out;
934 }
935 size -= skip + trim;
936 datal -= skip + trim;
937 ret = btrfs_insert_empty_item(trans, root, path,
938 &new_key, size);
939 if (ret)
940 goto out;
941
942 if (skip) {
943 u32 start = btrfs_file_extent_calc_inline_size(0);
944 memmove(buf+start, buf+start+skip,
945 datal);
946 }
947
948 leaf = path->nodes[0];
949 slot = path->slots[0];
950 write_extent_buffer(leaf, buf,
951 btrfs_item_ptr_offset(leaf, slot),
952 size);
953 inode_add_bytes(inode, datal);
954 }
955
956 btrfs_mark_buffer_dirty(leaf);
957 }
958
959 if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
960 u32 size;
961 struct btrfs_key new_key;
962 u64 coverslen;
963 int coff, clen;
964
965 size = btrfs_item_size_nr(leaf, slot);
966 coverslen = (size / BTRFS_CRC32_SIZE) <<
967 root->fs_info->sb->s_blocksize_bits;
968 printk("csums for %llu~%llu\n",
969 key.offset, coverslen);
970 if (key.offset + coverslen < off ||
971 key.offset >= off+len)
972 goto next;
973
974 read_extent_buffer(leaf, buf,
975 btrfs_item_ptr_offset(leaf, slot),
976 size);
977 btrfs_release_path(root, path);
978
979 coff = 0;
980 if (off > key.offset)
981 coff = ((off - key.offset) >>
982 root->fs_info->sb->s_blocksize_bits) *
983 BTRFS_CRC32_SIZE;
984 clen = size - coff;
985 if (key.offset + coverslen > off+len)
986 clen -= ((key.offset+coverslen-off-len) >>
987 root->fs_info->sb->s_blocksize_bits) *
988 BTRFS_CRC32_SIZE;
989 printk(" will dup %d~%d of %d\n",
990 coff, clen, size);
991
992 memcpy(&new_key, &key, sizeof(new_key));
993 new_key.objectid = inode->i_ino;
994 new_key.offset = key.offset + destoff - off;
995
Zheng Yan31840ae2008-09-23 13:14:14 -0400996 ret = btrfs_insert_empty_item(trans, root, path,
Sage Weilc5c9cd42008-11-12 14:32:25 -0500997 &new_key, clen);
Zheng Yan31840ae2008-09-23 13:14:14 -0400998 if (ret)
999 goto out;
1000
1001 leaf = path->nodes[0];
1002 slot = path->slots[0];
Sage Weilc5c9cd42008-11-12 14:32:25 -05001003 write_extent_buffer(leaf, buf + coff,
Zheng Yan31840ae2008-09-23 13:14:14 -04001004 btrfs_item_ptr_offset(leaf, slot),
Sage Weilc5c9cd42008-11-12 14:32:25 -05001005 clen);
Zheng Yan31840ae2008-09-23 13:14:14 -04001006 btrfs_mark_buffer_dirty(leaf);
1007 }
1008
Sage Weilc5c9cd42008-11-12 14:32:25 -05001009 next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001010 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001011 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001012 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001013 ret = 0;
1014out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001015 btrfs_release_path(root, path);
1016 if (ret == 0) {
1017 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001018 if (destoff + olen > inode->i_size)
1019 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001020 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1021 ret = btrfs_update_inode(trans, root, inode);
1022 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001023 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001024 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001025 if (ret)
1026 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001027out_unlock:
1028 mutex_unlock(&src->i_mutex);
1029 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001030 vfree(buf);
1031 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001032out_fput:
1033 fput(src_file);
1034 return ret;
1035}
1036
Sage Weilc5c9cd42008-11-12 14:32:25 -05001037long btrfs_ioctl_clone_range(struct file *file, unsigned long argptr)
1038{
1039 struct btrfs_ioctl_clone_range_args args;
1040
1041 if (copy_from_user(&args, (void *)argptr, sizeof(args)))
1042 return -EFAULT;
1043 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1044 args.src_length, args.dest_offset);
1045}
1046
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001047/*
1048 * there are many ways the trans_start and trans_end ioctls can lead
1049 * to deadlocks. They should only be used by applications that
1050 * basically own the machine, and have a very in depth understanding
1051 * of all the possible deadlocks and enospc problems.
1052 */
1053long btrfs_ioctl_trans_start(struct file *file)
1054{
1055 struct inode *inode = fdentry(file)->d_inode;
1056 struct btrfs_root *root = BTRFS_I(inode)->root;
1057 struct btrfs_trans_handle *trans;
1058 int ret = 0;
1059
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001060 if (!capable(CAP_SYS_ADMIN))
1061 return -EPERM;
1062
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001063 if (file->private_data) {
1064 ret = -EINPROGRESS;
1065 goto out;
1066 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04001067
Yan Zhengc146afa2008-11-12 14:34:12 -05001068 ret = mnt_want_write(file->f_path.mnt);
1069 if (ret)
1070 goto out;
1071
Sage Weil9ca9ee02008-08-04 10:41:27 -04001072 mutex_lock(&root->fs_info->trans_mutex);
1073 root->fs_info->open_ioctl_trans++;
1074 mutex_unlock(&root->fs_info->trans_mutex);
1075
1076 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001077 if (trans)
1078 file->private_data = trans;
1079 else
1080 ret = -ENOMEM;
1081 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1082out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001083 return ret;
1084}
1085
1086/*
1087 * there are many ways the trans_start and trans_end ioctls can lead
1088 * to deadlocks. They should only be used by applications that
1089 * basically own the machine, and have a very in depth understanding
1090 * of all the possible deadlocks and enospc problems.
1091 */
1092long btrfs_ioctl_trans_end(struct file *file)
1093{
1094 struct inode *inode = fdentry(file)->d_inode;
1095 struct btrfs_root *root = BTRFS_I(inode)->root;
1096 struct btrfs_trans_handle *trans;
1097 int ret = 0;
1098
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001099 trans = file->private_data;
1100 if (!trans) {
1101 ret = -EINVAL;
1102 goto out;
1103 }
1104 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -04001105 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001106
1107 mutex_lock(&root->fs_info->trans_mutex);
1108 root->fs_info->open_ioctl_trans--;
1109 mutex_unlock(&root->fs_info->trans_mutex);
1110
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001111out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001112 return ret;
1113}
1114
1115long btrfs_ioctl(struct file *file, unsigned int
1116 cmd, unsigned long arg)
1117{
1118 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1119
1120 switch (cmd) {
1121 case BTRFS_IOC_SNAP_CREATE:
Chris Mason3de45862008-11-17 21:02:50 -05001122 return btrfs_ioctl_snap_create(file, (void __user *)arg, 0);
1123 case BTRFS_IOC_SUBVOL_CREATE:
1124 return btrfs_ioctl_snap_create(file, (void __user *)arg, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001125 case BTRFS_IOC_DEFRAG:
1126 return btrfs_ioctl_defrag(file);
1127 case BTRFS_IOC_RESIZE:
1128 return btrfs_ioctl_resize(root, (void __user *)arg);
1129 case BTRFS_IOC_ADD_DEV:
1130 return btrfs_ioctl_add_dev(root, (void __user *)arg);
1131 case BTRFS_IOC_RM_DEV:
1132 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
1133 case BTRFS_IOC_BALANCE:
1134 return btrfs_balance(root->fs_info->dev_root);
1135 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001136 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1137 case BTRFS_IOC_CLONE_RANGE:
1138 return btrfs_ioctl_clone_range(file, arg);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001139 case BTRFS_IOC_TRANS_START:
1140 return btrfs_ioctl_trans_start(file);
1141 case BTRFS_IOC_TRANS_END:
1142 return btrfs_ioctl_trans_end(file);
1143 case BTRFS_IOC_SYNC:
Chris Masonea8c2812008-08-04 23:17:27 -04001144 btrfs_start_delalloc_inodes(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001145 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1146 return 0;
1147 }
1148
1149 return -ENOTTY;
1150}