blob: 2624b53ea7830d54c89ea92145c7e9382775755a [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/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040042#include <linux/vmalloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.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
Josef Bacik6a632092009-02-20 11:00:09 -050073 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074 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
Yan Zheng52c26172009-01-05 15:43:43 -0500149 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
150 ret = btrfs_update_inode(trans, root, dir);
151 BUG_ON(ret);
152
Chris Mason0660b5a2008-11-17 20:37:39 -0500153 /* add the backref first */
154 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
155 objectid, BTRFS_ROOT_BACKREF_KEY,
156 root->root_key.objectid,
157 dir->i_ino, index, name, namelen);
158
159 BUG_ON(ret);
160
161 /* now add the forward ref */
162 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
163 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
164 objectid,
165 dir->i_ino, index, name, namelen);
166
167 BUG_ON(ret);
168
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400169 ret = btrfs_commit_transaction(trans, root);
170 if (ret)
171 goto fail_commit;
172
Chris Mason3de45862008-11-17 21:02:50 -0500173 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400174 BUG_ON(!new_root);
175
176 trans = btrfs_start_transaction(new_root, 1);
177 BUG_ON(!trans);
178
Yan Zhengd2fb3432008-12-11 16:30:39 -0500179 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400180 BTRFS_I(dir)->block_group);
181 if (ret)
182 goto fail;
183
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400184fail:
185 nr = trans->blocks_used;
186 err = btrfs_commit_transaction(trans, new_root);
187 if (err && !ret)
188 ret = err;
189fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400190 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400191 return ret;
192}
193
Chris Mason3de45862008-11-17 21:02:50 -0500194static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
195 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400196{
197 struct btrfs_pending_snapshot *pending_snapshot;
198 struct btrfs_trans_handle *trans;
Chris Mason3de45862008-11-17 21:02:50 -0500199 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400200 int err;
201 unsigned long nr = 0;
202
203 if (!root->ref_cows)
204 return -EINVAL;
205
Josef Bacik6a632092009-02-20 11:00:09 -0500206 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400207 if (ret)
208 goto fail_unlock;
209
Chris Mason3de45862008-11-17 21:02:50 -0500210 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400211 if (!pending_snapshot) {
212 ret = -ENOMEM;
213 goto fail_unlock;
214 }
215 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
216 if (!pending_snapshot->name) {
217 ret = -ENOMEM;
218 kfree(pending_snapshot);
219 goto fail_unlock;
220 }
221 memcpy(pending_snapshot->name, name, namelen);
222 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500223 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400224 trans = btrfs_start_transaction(root, 1);
225 BUG_ON(!trans);
226 pending_snapshot->root = root;
227 list_add(&pending_snapshot->list,
228 &trans->transaction->pending_snapshots);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400229 err = btrfs_commit_transaction(trans, root);
230
231fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400232 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400233 return ret;
234}
235
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400236/* copy of may_create in fs/namei.c() */
237static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
238{
239 if (child->d_inode)
240 return -EEXIST;
241 if (IS_DEADDIR(dir))
242 return -ENOENT;
243 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
244}
245
246/*
247 * Create a new subvolume below @parent. This is largely modeled after
248 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
249 * inside this filesystem so it's quite a bit simpler.
250 */
251static noinline int btrfs_mksubvol(struct path *parent, char *name,
Chris Mason3de45862008-11-17 21:02:50 -0500252 int mode, int namelen,
253 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400254{
255 struct dentry *dentry;
256 int error;
257
258 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
259
260 dentry = lookup_one_len(name, parent->dentry, namelen);
261 error = PTR_ERR(dentry);
262 if (IS_ERR(dentry))
263 goto out_unlock;
264
265 error = -EEXIST;
266 if (dentry->d_inode)
267 goto out_dput;
268
269 if (!IS_POSIXACL(parent->dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -0400270 mode &= ~current_umask();
Chris Mason3de45862008-11-17 21:02:50 -0500271
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400272 error = mnt_want_write(parent->mnt);
273 if (error)
274 goto out_dput;
275
276 error = btrfs_may_create(parent->dentry->d_inode, dentry);
277 if (error)
278 goto out_drop_write;
279
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400280 /*
281 * Actually perform the low-level subvolume creation after all
282 * this VFS fuzz.
283 *
284 * Eventually we want to pass in an inode under which we create this
285 * subvolume, but for now all are under the filesystem root.
286 *
287 * Also we should pass on the mode eventually to allow creating new
288 * subvolume with specific mode bits.
289 */
Chris Mason3de45862008-11-17 21:02:50 -0500290 if (snap_src) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500291 struct dentry *dir = dentry->d_parent;
292 struct dentry *test = dir->d_parent;
293 struct btrfs_path *path = btrfs_alloc_path();
294 int ret;
295 u64 test_oid;
296 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
297
298 test_oid = snap_src->root_key.objectid;
299
300 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
301 path, parent_oid, test_oid);
302 if (ret == 0)
303 goto create;
304 btrfs_release_path(snap_src->fs_info->tree_root, path);
305
306 /* we need to make sure we aren't creating a directory loop
307 * by taking a snapshot of something that has our current
308 * subvol in its directory tree. So, this loops through
309 * the dentries and checks the forward refs for each subvolume
310 * to see if is references the subvolume where we are
311 * placing this new snapshot.
312 */
Chris Masond3977122009-01-05 21:25:51 -0500313 while (1) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500314 if (!test ||
315 dir == snap_src->fs_info->sb->s_root ||
316 test == snap_src->fs_info->sb->s_root ||
317 test->d_inode->i_sb != snap_src->fs_info->sb) {
318 break;
319 }
320 if (S_ISLNK(test->d_inode->i_mode)) {
Chris Masond3977122009-01-05 21:25:51 -0500321 printk(KERN_INFO "Btrfs symlink in snapshot "
322 "path, failed\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500323 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) {
Chris Masond3977122009-01-05 21:25:51 -0500332 printk(KERN_INFO "Btrfs snapshot creation "
333 "failed, looping\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500334 error = -EMLINK;
335 btrfs_free_path(path);
336 goto out_drop_write;
337 }
338 btrfs_release_path(snap_src->fs_info->tree_root, path);
339 test = test->d_parent;
340 }
341create:
342 btrfs_free_path(path);
Chris Mason3de45862008-11-17 21:02:50 -0500343 error = create_snapshot(snap_src, dentry, name, namelen);
344 } else {
345 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
346 dentry, name, namelen);
347 }
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400348 if (error)
349 goto out_drop_write;
350
351 fsnotify_mkdir(parent->dentry->d_inode, dentry);
352out_drop_write:
353 mnt_drop_write(parent->mnt);
354out_dput:
355 dput(dentry);
356out_unlock:
357 mutex_unlock(&parent->dentry->d_inode->i_mutex);
358 return error;
359}
360
361
Christoph Hellwigb2950862008-12-02 09:54:17 -0500362static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400363{
364 struct inode *inode = fdentry(file)->d_inode;
365 struct btrfs_root *root = BTRFS_I(inode)->root;
366 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400367 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368 struct page *page;
369 unsigned long last_index;
370 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
371 unsigned long total_read = 0;
372 u64 page_start;
373 u64 page_end;
374 unsigned long i;
375 int ret;
376
Josef Bacik6a632092009-02-20 11:00:09 -0500377 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 if (ret)
379 return -ENOSPC;
380
381 mutex_lock(&inode->i_mutex);
382 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
383 for (i = 0; i <= last_index; i++) {
384 if (total_read % ra_pages == 0) {
385 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
386 min(last_index, i + ra_pages - 1));
387 }
388 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400389again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400390 page = grab_cache_page(inode->i_mapping, i);
391 if (!page)
392 goto out_unlock;
393 if (!PageUptodate(page)) {
394 btrfs_readpage(NULL, page);
395 lock_page(page);
396 if (!PageUptodate(page)) {
397 unlock_page(page);
398 page_cache_release(page);
399 goto out_unlock;
400 }
401 }
402
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404
405 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
406 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400407 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400408
409 ordered = btrfs_lookup_ordered_extent(inode, page_start);
410 if (ordered) {
411 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
412 unlock_page(page);
413 page_cache_release(page);
414 btrfs_start_ordered_extent(inode, ordered, 1);
415 btrfs_put_ordered_extent(ordered);
416 goto again;
417 }
418 set_page_extent_mapped(page);
419
Chris Masonf87f0572008-08-01 11:27:23 -0400420 /*
421 * this makes sure page_mkwrite is called on the
422 * page if it is dirtied again later
423 */
424 clear_page_dirty_for_io(page);
425
Chris Masonea8c2812008-08-04 23:17:27 -0400426 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400427
428 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
429 set_page_dirty(page);
430 unlock_page(page);
431 page_cache_release(page);
432 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
433 }
434
435out_unlock:
436 mutex_unlock(&inode->i_mutex);
437 return 0;
438}
439
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
441{
442 u64 new_size;
443 u64 old_size;
444 u64 devid = 1;
445 struct btrfs_ioctl_vol_args *vol_args;
446 struct btrfs_trans_handle *trans;
447 struct btrfs_device *device = NULL;
448 char *sizestr;
449 char *devstr = NULL;
450 int ret = 0;
451 int namelen;
452 int mod = 0;
453
Yan Zhengc146afa2008-11-12 14:34:12 -0500454 if (root->fs_info->sb->s_flags & MS_RDONLY)
455 return -EROFS;
456
Chris Masone441d542009-01-05 16:57:23 -0500457 if (!capable(CAP_SYS_ADMIN))
458 return -EPERM;
459
Li Zefandae7b662009-04-08 15:06:54 +0800460 vol_args = memdup_user(arg, sizeof(*vol_args));
461 if (IS_ERR(vol_args))
462 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400463
464 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466
Chris Mason7d9eb122008-07-08 14:19:17 -0400467 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400468 sizestr = vol_args->name;
469 devstr = strchr(sizestr, ':');
470 if (devstr) {
471 char *end;
472 sizestr = devstr + 1;
473 *devstr = '\0';
474 devstr = vol_args->name;
475 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700476 printk(KERN_INFO "resizing devid %llu\n",
477 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478 }
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) {
Joel Becker21380932009-04-21 12:38:29 -0700481 printk(KERN_INFO "resizer unable to find device %llu\n",
482 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400483 ret = -EINVAL;
484 goto out_unlock;
485 }
486 if (!strcmp(sizestr, "max"))
487 new_size = device->bdev->bd_inode->i_size;
488 else {
489 if (sizestr[0] == '-') {
490 mod = -1;
491 sizestr++;
492 } else if (sizestr[0] == '+') {
493 mod = 1;
494 sizestr++;
495 }
496 new_size = btrfs_parse_size(sizestr);
497 if (new_size == 0) {
498 ret = -EINVAL;
499 goto out_unlock;
500 }
501 }
502
503 old_size = device->total_bytes;
504
505 if (mod < 0) {
506 if (new_size > old_size) {
507 ret = -EINVAL;
508 goto out_unlock;
509 }
510 new_size = old_size - new_size;
511 } else if (mod > 0) {
512 new_size = old_size + new_size;
513 }
514
515 if (new_size < 256 * 1024 * 1024) {
516 ret = -EINVAL;
517 goto out_unlock;
518 }
519 if (new_size > device->bdev->bd_inode->i_size) {
520 ret = -EFBIG;
521 goto out_unlock;
522 }
523
524 do_div(new_size, root->sectorsize);
525 new_size *= root->sectorsize;
526
527 printk(KERN_INFO "new size for %s is %llu\n",
528 device->name, (unsigned long long)new_size);
529
530 if (new_size > old_size) {
531 trans = btrfs_start_transaction(root, 1);
532 ret = btrfs_grow_device(trans, device, new_size);
533 btrfs_commit_transaction(trans, root);
534 } else {
535 ret = btrfs_shrink_device(device, new_size);
536 }
537
538out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400539 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400540 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
Li Zefandae7b662009-04-08 15:06:54 +0800559 vol_args = memdup_user(arg, sizeof(*vol_args));
560 if (IS_ERR(vol_args))
561 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400562
Mark Fasheh5516e592008-07-24 12:20:14 -0400563 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400565 if (strchr(vol_args->name, '/')) {
566 ret = -EINVAL;
567 goto out;
568 }
569
570 path = btrfs_alloc_path();
571 if (!path) {
572 ret = -ENOMEM;
573 goto out;
574 }
575
576 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400577 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
578 path, root_dirid,
579 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580 btrfs_free_path(path);
581
582 if (di && !IS_ERR(di)) {
583 ret = -EEXIST;
584 goto out;
585 }
586
587 if (IS_ERR(di)) {
588 ret = PTR_ERR(di);
589 goto out;
590 }
591
Chris Mason3de45862008-11-17 21:02:50 -0500592 if (subvol) {
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400593 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
594 file->f_path.dentry->d_inode->i_mode,
Chris Mason3de45862008-11-17 21:02:50 -0500595 namelen, NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400596 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500597 struct inode *src_inode;
598 src_file = fget(vol_args->fd);
599 if (!src_file) {
600 ret = -EINVAL;
601 goto out;
602 }
603
604 src_inode = src_file->f_path.dentry->d_inode;
605 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500606 printk(KERN_INFO "btrfs: Snapshot src from "
607 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500608 ret = -EINVAL;
609 fput(src_file);
610 goto out;
611 }
612 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
613 file->f_path.dentry->d_inode->i_mode,
614 namelen, BTRFS_I(src_inode)->root);
615 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400616 }
617
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400618out:
619 kfree(vol_args);
620 return ret;
621}
622
623static int btrfs_ioctl_defrag(struct file *file)
624{
625 struct inode *inode = fdentry(file)->d_inode;
626 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500627 int ret;
628
629 ret = mnt_want_write(file->f_path.mnt);
630 if (ret)
631 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632
633 switch (inode->i_mode & S_IFMT) {
634 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500635 if (!capable(CAP_SYS_ADMIN)) {
636 ret = -EPERM;
637 goto out;
638 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639 btrfs_defrag_root(root, 0);
640 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400641 break;
642 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500643 if (!(file->f_mode & FMODE_WRITE)) {
644 ret = -EINVAL;
645 goto out;
646 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400647 btrfs_defrag_file(file);
648 break;
649 }
Chris Masone441d542009-01-05 16:57:23 -0500650out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500651 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500652 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653}
654
Christoph Hellwigb2950862008-12-02 09:54:17 -0500655static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656{
657 struct btrfs_ioctl_vol_args *vol_args;
658 int ret;
659
Chris Masone441d542009-01-05 16:57:23 -0500660 if (!capable(CAP_SYS_ADMIN))
661 return -EPERM;
662
Li Zefandae7b662009-04-08 15:06:54 +0800663 vol_args = memdup_user(arg, sizeof(*vol_args));
664 if (IS_ERR(vol_args))
665 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666
Mark Fasheh5516e592008-07-24 12:20:14 -0400667 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400668 ret = btrfs_init_new_device(root, vol_args->name);
669
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400670 kfree(vol_args);
671 return ret;
672}
673
Christoph Hellwigb2950862008-12-02 09:54:17 -0500674static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400675{
676 struct btrfs_ioctl_vol_args *vol_args;
677 int ret;
678
Chris Masone441d542009-01-05 16:57:23 -0500679 if (!capable(CAP_SYS_ADMIN))
680 return -EPERM;
681
Yan Zhengc146afa2008-11-12 14:34:12 -0500682 if (root->fs_info->sb->s_flags & MS_RDONLY)
683 return -EROFS;
684
Li Zefandae7b662009-04-08 15:06:54 +0800685 vol_args = memdup_user(arg, sizeof(*vol_args));
686 if (IS_ERR(vol_args))
687 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400688
Mark Fasheh5516e592008-07-24 12:20:14 -0400689 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400690 ret = btrfs_rm_device(root, vol_args->name);
691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400692 kfree(vol_args);
693 return ret;
694}
695
Christoph Hellwigb2950862008-12-02 09:54:17 -0500696static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
697 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400698{
699 struct inode *inode = fdentry(file)->d_inode;
700 struct btrfs_root *root = BTRFS_I(inode)->root;
701 struct file *src_file;
702 struct inode *src;
703 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400704 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400705 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400706 char *buf;
707 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 u32 nritems;
709 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400710 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500711 u64 len = olen;
712 u64 bs = root->fs_info->sb->s_blocksize;
713 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -0500714
Sage Weilc5c9cd42008-11-12 14:32:25 -0500715 /*
716 * TODO:
717 * - split compressed inline extents. annoying: we need to
718 * decompress into destination's address_space (the file offset
719 * may change, so source mapping won't do), then recompress (or
720 * otherwise reinsert) a subrange.
721 * - allow ranges within the same file to be cloned (provided
722 * they don't overlap)?
723 */
724
Chris Masone441d542009-01-05 16:57:23 -0500725 /* the destination must be opened for writing */
726 if (!(file->f_mode & FMODE_WRITE))
727 return -EINVAL;
728
Yan Zhengc146afa2008-11-12 14:34:12 -0500729 ret = mnt_want_write(file->f_path.mnt);
730 if (ret)
731 return ret;
732
Sage Weilc5c9cd42008-11-12 14:32:25 -0500733 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500734 if (!src_file) {
735 ret = -EBADF;
736 goto out_drop_write;
737 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 src = src_file->f_dentry->d_inode;
739
Sage Weilc5c9cd42008-11-12 14:32:25 -0500740 ret = -EINVAL;
741 if (src == inode)
742 goto out_fput;
743
Yan Zhengae01a0a2008-08-04 23:23:47 -0400744 ret = -EISDIR;
745 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400746 goto out_fput;
747
Yan Zhengae01a0a2008-08-04 23:23:47 -0400748 ret = -EXDEV;
749 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
750 goto out_fput;
751
752 ret = -ENOMEM;
753 buf = vmalloc(btrfs_level_size(root, 0));
754 if (!buf)
755 goto out_fput;
756
757 path = btrfs_alloc_path();
758 if (!path) {
759 vfree(buf);
760 goto out_fput;
761 }
762 path->reada = 2;
763
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764 if (inode < src) {
765 mutex_lock(&inode->i_mutex);
766 mutex_lock(&src->i_mutex);
767 } else {
768 mutex_lock(&src->i_mutex);
769 mutex_lock(&inode->i_mutex);
770 }
771
Sage Weilc5c9cd42008-11-12 14:32:25 -0500772 /* determine range to clone */
773 ret = -EINVAL;
774 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400775 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500776 if (len == 0)
777 olen = len = src->i_size - off;
778 /* if we extend to eof, continue to block boundary */
779 if (off + len == src->i_size)
780 len = ((src->i_size + bs-1) & ~(bs-1))
781 - off;
782
783 /* verify the end result is block aligned */
784 if ((off & (bs-1)) ||
785 ((off + len) & (bs-1)))
786 goto out_unlock;
787
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400788 /* do any pending delalloc/csum calc on src, one way or
789 another, and lock file content */
790 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400791 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500792 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
793 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400794 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400795 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500796 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400797 if (ordered)
798 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500799 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400800 }
801
Yan Zhengae01a0a2008-08-04 23:23:47 -0400802 trans = btrfs_start_transaction(root, 1);
803 BUG_ON(!trans);
804
Sage Weilc5c9cd42008-11-12 14:32:25 -0500805 /* punch hole in destination first */
Chris Masone980b502009-04-24 14:39:24 -0400806 btrfs_drop_extents(trans, root, inode, off, off + len,
807 off + len, 0, &hint_byte);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500808
809 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400810 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400811 key.type = BTRFS_EXTENT_DATA_KEY;
812 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400813
814 while (1) {
815 /*
816 * note the key will change type as we walk through the
817 * tree.
818 */
819 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
820 if (ret < 0)
821 goto out;
822
Yan Zhengae01a0a2008-08-04 23:23:47 -0400823 nritems = btrfs_header_nritems(path->nodes[0]);
824 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400825 ret = btrfs_next_leaf(root, path);
826 if (ret < 0)
827 goto out;
828 if (ret > 0)
829 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400830 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400831 }
832 leaf = path->nodes[0];
833 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400834
Yan Zhengae01a0a2008-08-04 23:23:47 -0400835 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500836 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400837 key.objectid != src->i_ino)
838 break;
839
Sage Weilc5c9cd42008-11-12 14:32:25 -0500840 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
841 struct btrfs_file_extent_item *extent;
842 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -0400843 u32 size;
844 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500845 u64 disko = 0, diskl = 0;
846 u64 datao = 0, datal = 0;
847 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -0400848
849 size = btrfs_item_size_nr(leaf, slot);
850 read_extent_buffer(leaf, buf,
851 btrfs_item_ptr_offset(leaf, slot),
852 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500853
854 extent = btrfs_item_ptr(leaf, slot,
855 struct btrfs_file_extent_item);
856 comp = btrfs_file_extent_compression(leaf, extent);
857 type = btrfs_file_extent_type(leaf, extent);
858 if (type == BTRFS_FILE_EXTENT_REG) {
Chris Masond3977122009-01-05 21:25:51 -0500859 disko = btrfs_file_extent_disk_bytenr(leaf,
860 extent);
861 diskl = btrfs_file_extent_disk_num_bytes(leaf,
862 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500863 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -0500864 datal = btrfs_file_extent_num_bytes(leaf,
865 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500866 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
867 /* take upper bound, may be compressed */
868 datal = btrfs_file_extent_ram_bytes(leaf,
869 extent);
870 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400871 btrfs_release_path(root, path);
872
Sage Weilc5c9cd42008-11-12 14:32:25 -0500873 if (key.offset + datal < off ||
874 key.offset >= off+len)
875 goto next;
876
Zheng Yan31840ae2008-09-23 13:14:14 -0400877 memcpy(&new_key, &key, sizeof(new_key));
878 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500879 new_key.offset = key.offset + destoff - off;
880
881 if (type == BTRFS_FILE_EXTENT_REG) {
882 ret = btrfs_insert_empty_item(trans, root, path,
883 &new_key, size);
884 if (ret)
885 goto out;
886
887 leaf = path->nodes[0];
888 slot = path->slots[0];
889 write_extent_buffer(leaf, buf,
890 btrfs_item_ptr_offset(leaf, slot),
891 size);
892
893 extent = btrfs_item_ptr(leaf, slot,
894 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500895
896 if (off > key.offset) {
897 datao += off - key.offset;
898 datal -= off - key.offset;
899 }
900 if (key.offset + datao + datal + key.offset >
901 off + len)
902 datal = off + len - key.offset - datao;
903 /* disko == 0 means it's a hole */
904 if (!disko)
905 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500906
907 btrfs_set_file_extent_offset(leaf, extent,
908 datao);
909 btrfs_set_file_extent_num_bytes(leaf, extent,
910 datal);
911 if (disko) {
912 inode_add_bytes(inode, datal);
913 ret = btrfs_inc_extent_ref(trans, root,
914 disko, diskl, leaf->start,
915 root->root_key.objectid,
916 trans->transid,
917 inode->i_ino);
918 BUG_ON(ret);
919 }
920 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
921 u64 skip = 0;
922 u64 trim = 0;
923 if (off > key.offset) {
924 skip = off - key.offset;
925 new_key.offset += skip;
926 }
Chris Masond3977122009-01-05 21:25:51 -0500927
Sage Weilc5c9cd42008-11-12 14:32:25 -0500928 if (key.offset + datal > off+len)
929 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -0500930
Sage Weilc5c9cd42008-11-12 14:32:25 -0500931 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -0500932 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) {
Chris Masond3977122009-01-05 21:25:51 -0500943 u32 start =
944 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500945 memmove(buf+start, buf+start+skip,
946 datal);
947 }
948
949 leaf = path->nodes[0];
950 slot = path->slots[0];
951 write_extent_buffer(leaf, buf,
952 btrfs_item_ptr_offset(leaf, slot),
953 size);
954 inode_add_bytes(inode, datal);
955 }
956
957 btrfs_mark_buffer_dirty(leaf);
958 }
959
Chris Masond3977122009-01-05 21:25:51 -0500960next:
Zheng Yan31840ae2008-09-23 13:14:14 -0400961 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400962 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400963 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400964 ret = 0;
965out:
Yan Zhengae01a0a2008-08-04 23:23:47 -0400966 btrfs_release_path(root, path);
967 if (ret == 0) {
968 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500969 if (destoff + olen > inode->i_size)
970 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400971 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
972 ret = btrfs_update_inode(trans, root, inode);
973 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400974 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500975 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400976 if (ret)
977 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400978out_unlock:
979 mutex_unlock(&src->i_mutex);
980 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400981 vfree(buf);
982 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400983out_fput:
984 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500985out_drop_write:
986 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400987 return ret;
988}
989
Christoph Hellwig7a865e82008-12-02 09:52:24 -0500990static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -0500991{
992 struct btrfs_ioctl_clone_range_args args;
993
Christoph Hellwig7a865e82008-12-02 09:52:24 -0500994 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -0500995 return -EFAULT;
996 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
997 args.src_length, args.dest_offset);
998}
999
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001000/*
1001 * there are many ways the trans_start and trans_end ioctls can lead
1002 * to deadlocks. They should only be used by applications that
1003 * basically own the machine, and have a very in depth understanding
1004 * of all the possible deadlocks and enospc problems.
1005 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001006static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001007{
1008 struct inode *inode = fdentry(file)->d_inode;
1009 struct btrfs_root *root = BTRFS_I(inode)->root;
1010 struct btrfs_trans_handle *trans;
1011 int ret = 0;
1012
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001013 if (!capable(CAP_SYS_ADMIN))
1014 return -EPERM;
1015
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001016 if (file->private_data) {
1017 ret = -EINPROGRESS;
1018 goto out;
1019 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04001020
Yan Zhengc146afa2008-11-12 14:34:12 -05001021 ret = mnt_want_write(file->f_path.mnt);
1022 if (ret)
1023 goto out;
1024
Sage Weil9ca9ee02008-08-04 10:41:27 -04001025 mutex_lock(&root->fs_info->trans_mutex);
1026 root->fs_info->open_ioctl_trans++;
1027 mutex_unlock(&root->fs_info->trans_mutex);
1028
1029 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001030 if (trans)
1031 file->private_data = trans;
1032 else
1033 ret = -ENOMEM;
1034 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1035out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001036 return ret;
1037}
1038
1039/*
1040 * there are many ways the trans_start and trans_end ioctls can lead
1041 * to deadlocks. They should only be used by applications that
1042 * basically own the machine, and have a very in depth understanding
1043 * of all the possible deadlocks and enospc problems.
1044 */
1045long btrfs_ioctl_trans_end(struct file *file)
1046{
1047 struct inode *inode = fdentry(file)->d_inode;
1048 struct btrfs_root *root = BTRFS_I(inode)->root;
1049 struct btrfs_trans_handle *trans;
1050 int ret = 0;
1051
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001052 trans = file->private_data;
1053 if (!trans) {
1054 ret = -EINVAL;
1055 goto out;
1056 }
1057 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -04001058 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001059
1060 mutex_lock(&root->fs_info->trans_mutex);
1061 root->fs_info->open_ioctl_trans--;
1062 mutex_unlock(&root->fs_info->trans_mutex);
1063
Sage Weilcfc8ea82008-12-11 16:30:06 -05001064 mnt_drop_write(file->f_path.mnt);
1065
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001066out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001067 return ret;
1068}
1069
1070long btrfs_ioctl(struct file *file, unsigned int
1071 cmd, unsigned long arg)
1072{
1073 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001074 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001075
1076 switch (cmd) {
1077 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001078 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001079 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001080 return btrfs_ioctl_snap_create(file, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001081 case BTRFS_IOC_DEFRAG:
1082 return btrfs_ioctl_defrag(file);
1083 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001084 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001085 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001086 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001087 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001088 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001089 case BTRFS_IOC_BALANCE:
1090 return btrfs_balance(root->fs_info->dev_root);
1091 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001092 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1093 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001094 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001095 case BTRFS_IOC_TRANS_START:
1096 return btrfs_ioctl_trans_start(file);
1097 case BTRFS_IOC_TRANS_END:
1098 return btrfs_ioctl_trans_end(file);
1099 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001100 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1101 return 0;
1102 }
1103
1104 return -ENOTTY;
1105}