blob: fd3c8b5676c13ba1114a38ab5125208fc7022b0d [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;
70 unsigned long nr = 1;
71
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040072 ret = btrfs_check_free_space(root, 1, 0);
73 if (ret)
74 goto fail_commit;
75
76 trans = btrfs_start_transaction(root, 1);
77 BUG_ON(!trans);
78
79 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
80 0, &objectid);
81 if (ret)
82 goto fail;
83
Zheng Yan31840ae2008-09-23 13:14:14 -040084 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
85 objectid, trans->transid, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -040086 if (IS_ERR(leaf)) {
87 ret = PTR_ERR(leaf);
88 goto fail;
89 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040090
91 btrfs_set_header_nritems(leaf, 0);
92 btrfs_set_header_level(leaf, 0);
93 btrfs_set_header_bytenr(leaf, leaf->start);
94 btrfs_set_header_generation(leaf, trans->transid);
95 btrfs_set_header_owner(leaf, objectid);
96
97 write_extent_buffer(leaf, root->fs_info->fsid,
98 (unsigned long)btrfs_header_fsid(leaf),
99 BTRFS_FSID_SIZE);
100 btrfs_mark_buffer_dirty(leaf);
101
102 inode_item = &root_item.inode;
103 memset(inode_item, 0, sizeof(*inode_item));
104 inode_item->generation = cpu_to_le64(1);
105 inode_item->size = cpu_to_le64(3);
106 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400107 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400108 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
109
110 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400111 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400112 btrfs_set_root_level(&root_item, 0);
113 btrfs_set_root_refs(&root_item, 1);
114 btrfs_set_root_used(&root_item, 0);
115
116 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
117 root_item.drop_level = 0;
118
Chris Mason925baed2008-06-25 16:01:30 -0400119 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400120 free_extent_buffer(leaf);
121 leaf = NULL;
122
123 btrfs_set_root_dirid(&root_item, new_dirid);
124
125 key.objectid = objectid;
126 key.offset = 1;
127 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
128 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
129 &root_item);
130 if (ret)
131 goto fail;
132
133 /*
134 * insert the directory item
135 */
136 key.offset = (u64)-1;
137 dir = root->fs_info->sb->s_root->d_inode;
138 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
139 name, namelen, dir->i_ino, &key,
Josef Bacikaec74772008-07-24 12:12:38 -0400140 BTRFS_FT_DIR, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400141 if (ret)
142 goto fail;
143
144 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
145 name, namelen, objectid,
Josef Bacikaec74772008-07-24 12:12:38 -0400146 root->fs_info->sb->s_root->d_inode->i_ino, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400147 if (ret)
148 goto fail;
149
150 ret = btrfs_commit_transaction(trans, root);
151 if (ret)
152 goto fail_commit;
153
154 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
155 BUG_ON(!new_root);
156
157 trans = btrfs_start_transaction(new_root, 1);
158 BUG_ON(!trans);
159
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400160 ret = btrfs_create_subvol_root(new_root, dentry, trans, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400161 BTRFS_I(dir)->block_group);
162 if (ret)
163 goto fail;
164
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400165fail:
166 nr = trans->blocks_used;
167 err = btrfs_commit_transaction(trans, new_root);
168 if (err && !ret)
169 ret = err;
170fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400171 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400172 return ret;
173}
174
175static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
176{
177 struct btrfs_pending_snapshot *pending_snapshot;
178 struct btrfs_trans_handle *trans;
179 int ret;
180 int err;
181 unsigned long nr = 0;
182
183 if (!root->ref_cows)
184 return -EINVAL;
185
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400186 ret = btrfs_check_free_space(root, 1, 0);
187 if (ret)
188 goto fail_unlock;
189
190 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
191 if (!pending_snapshot) {
192 ret = -ENOMEM;
193 goto fail_unlock;
194 }
195 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
196 if (!pending_snapshot->name) {
197 ret = -ENOMEM;
198 kfree(pending_snapshot);
199 goto fail_unlock;
200 }
201 memcpy(pending_snapshot->name, name, namelen);
202 pending_snapshot->name[namelen] = '\0';
203 trans = btrfs_start_transaction(root, 1);
204 BUG_ON(!trans);
205 pending_snapshot->root = root;
206 list_add(&pending_snapshot->list,
207 &trans->transaction->pending_snapshots);
208 ret = btrfs_update_inode(trans, root, root->inode);
209 err = btrfs_commit_transaction(trans, root);
210
211fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400212 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400213 return ret;
214}
215
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400216/* copy of may_create in fs/namei.c() */
217static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
218{
219 if (child->d_inode)
220 return -EEXIST;
221 if (IS_DEADDIR(dir))
222 return -ENOENT;
223 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
224}
225
226/*
227 * Create a new subvolume below @parent. This is largely modeled after
228 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
229 * inside this filesystem so it's quite a bit simpler.
230 */
231static noinline int btrfs_mksubvol(struct path *parent, char *name,
232 int mode, int namelen)
233{
234 struct dentry *dentry;
235 int error;
236
237 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
238
239 dentry = lookup_one_len(name, parent->dentry, namelen);
240 error = PTR_ERR(dentry);
241 if (IS_ERR(dentry))
242 goto out_unlock;
243
244 error = -EEXIST;
245 if (dentry->d_inode)
246 goto out_dput;
247
248 if (!IS_POSIXACL(parent->dentry->d_inode))
249 mode &= ~current->fs->umask;
250 error = mnt_want_write(parent->mnt);
251 if (error)
252 goto out_dput;
253
254 error = btrfs_may_create(parent->dentry->d_inode, dentry);
255 if (error)
256 goto out_drop_write;
257
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400258 /*
259 * Actually perform the low-level subvolume creation after all
260 * this VFS fuzz.
261 *
262 * Eventually we want to pass in an inode under which we create this
263 * subvolume, but for now all are under the filesystem root.
264 *
265 * Also we should pass on the mode eventually to allow creating new
266 * subvolume with specific mode bits.
267 */
268 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root, dentry,
269 name, namelen);
270 if (error)
271 goto out_drop_write;
272
273 fsnotify_mkdir(parent->dentry->d_inode, dentry);
274out_drop_write:
275 mnt_drop_write(parent->mnt);
276out_dput:
277 dput(dentry);
278out_unlock:
279 mutex_unlock(&parent->dentry->d_inode->i_mutex);
280 return error;
281}
282
283
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400284int btrfs_defrag_file(struct file *file)
285{
286 struct inode *inode = fdentry(file)->d_inode;
287 struct btrfs_root *root = BTRFS_I(inode)->root;
288 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400289 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400290 struct page *page;
291 unsigned long last_index;
292 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
293 unsigned long total_read = 0;
294 u64 page_start;
295 u64 page_end;
296 unsigned long i;
297 int ret;
298
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400299 ret = btrfs_check_free_space(root, inode->i_size, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400300 if (ret)
301 return -ENOSPC;
302
303 mutex_lock(&inode->i_mutex);
304 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
305 for (i = 0; i <= last_index; i++) {
306 if (total_read % ra_pages == 0) {
307 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
308 min(last_index, i + ra_pages - 1));
309 }
310 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400311again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400312 page = grab_cache_page(inode->i_mapping, i);
313 if (!page)
314 goto out_unlock;
315 if (!PageUptodate(page)) {
316 btrfs_readpage(NULL, page);
317 lock_page(page);
318 if (!PageUptodate(page)) {
319 unlock_page(page);
320 page_cache_release(page);
321 goto out_unlock;
322 }
323 }
324
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400325 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400326
327 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
328 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400329 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400330
331 ordered = btrfs_lookup_ordered_extent(inode, page_start);
332 if (ordered) {
333 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
334 unlock_page(page);
335 page_cache_release(page);
336 btrfs_start_ordered_extent(inode, ordered, 1);
337 btrfs_put_ordered_extent(ordered);
338 goto again;
339 }
340 set_page_extent_mapped(page);
341
Chris Masonf87f0572008-08-01 11:27:23 -0400342 /*
343 * this makes sure page_mkwrite is called on the
344 * page if it is dirtied again later
345 */
346 clear_page_dirty_for_io(page);
347
Chris Masonea8c2812008-08-04 23:17:27 -0400348 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400349
350 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
351 set_page_dirty(page);
352 unlock_page(page);
353 page_cache_release(page);
354 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
355 }
356
357out_unlock:
358 mutex_unlock(&inode->i_mutex);
359 return 0;
360}
361
362/*
363 * Called inside transaction, so use GFP_NOFS
364 */
365
366static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
367{
368 u64 new_size;
369 u64 old_size;
370 u64 devid = 1;
371 struct btrfs_ioctl_vol_args *vol_args;
372 struct btrfs_trans_handle *trans;
373 struct btrfs_device *device = NULL;
374 char *sizestr;
375 char *devstr = NULL;
376 int ret = 0;
377 int namelen;
378 int mod = 0;
379
380 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
381
382 if (!vol_args)
383 return -ENOMEM;
384
385 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
386 ret = -EFAULT;
387 goto out;
388 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400389
390 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400391 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392
Chris Mason7d9eb122008-07-08 14:19:17 -0400393 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400394 sizestr = vol_args->name;
395 devstr = strchr(sizestr, ':');
396 if (devstr) {
397 char *end;
398 sizestr = devstr + 1;
399 *devstr = '\0';
400 devstr = vol_args->name;
401 devid = simple_strtoull(devstr, &end, 10);
402 printk(KERN_INFO "resizing devid %llu\n", devid);
403 }
404 device = btrfs_find_device(root, devid, NULL);
405 if (!device) {
406 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
407 ret = -EINVAL;
408 goto out_unlock;
409 }
410 if (!strcmp(sizestr, "max"))
411 new_size = device->bdev->bd_inode->i_size;
412 else {
413 if (sizestr[0] == '-') {
414 mod = -1;
415 sizestr++;
416 } else if (sizestr[0] == '+') {
417 mod = 1;
418 sizestr++;
419 }
420 new_size = btrfs_parse_size(sizestr);
421 if (new_size == 0) {
422 ret = -EINVAL;
423 goto out_unlock;
424 }
425 }
426
427 old_size = device->total_bytes;
428
429 if (mod < 0) {
430 if (new_size > old_size) {
431 ret = -EINVAL;
432 goto out_unlock;
433 }
434 new_size = old_size - new_size;
435 } else if (mod > 0) {
436 new_size = old_size + new_size;
437 }
438
439 if (new_size < 256 * 1024 * 1024) {
440 ret = -EINVAL;
441 goto out_unlock;
442 }
443 if (new_size > device->bdev->bd_inode->i_size) {
444 ret = -EFBIG;
445 goto out_unlock;
446 }
447
448 do_div(new_size, root->sectorsize);
449 new_size *= root->sectorsize;
450
451 printk(KERN_INFO "new size for %s is %llu\n",
452 device->name, (unsigned long long)new_size);
453
454 if (new_size > old_size) {
455 trans = btrfs_start_transaction(root, 1);
456 ret = btrfs_grow_device(trans, device, new_size);
457 btrfs_commit_transaction(trans, root);
458 } else {
459 ret = btrfs_shrink_device(device, new_size);
460 }
461
462out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400463 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464out:
465 kfree(vol_args);
466 return ret;
467}
468
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400469static noinline int btrfs_ioctl_snap_create(struct file *file,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400470 void __user *arg)
471{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400472 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400473 struct btrfs_ioctl_vol_args *vol_args;
474 struct btrfs_dir_item *di;
475 struct btrfs_path *path;
476 u64 root_dirid;
477 int namelen;
478 int ret;
479
480 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
481
482 if (!vol_args)
483 return -ENOMEM;
484
485 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
486 ret = -EFAULT;
487 goto out;
488 }
489
Mark Fasheh5516e592008-07-24 12:20:14 -0400490 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492 if (strchr(vol_args->name, '/')) {
493 ret = -EINVAL;
494 goto out;
495 }
496
497 path = btrfs_alloc_path();
498 if (!path) {
499 ret = -ENOMEM;
500 goto out;
501 }
502
503 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
505 path, root_dirid,
506 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507 btrfs_free_path(path);
508
509 if (di && !IS_ERR(di)) {
510 ret = -EEXIST;
511 goto out;
512 }
513
514 if (IS_ERR(di)) {
515 ret = PTR_ERR(di);
516 goto out;
517 }
518
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400519 if (root == root->fs_info->tree_root) {
520 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
521 file->f_path.dentry->d_inode->i_mode,
522 namelen);
523 } else {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524 ret = create_snapshot(root, vol_args->name, namelen);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400525 }
526
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400527out:
528 kfree(vol_args);
529 return ret;
530}
531
532static int btrfs_ioctl_defrag(struct file *file)
533{
534 struct inode *inode = fdentry(file)->d_inode;
535 struct btrfs_root *root = BTRFS_I(inode)->root;
536
537 switch (inode->i_mode & S_IFMT) {
538 case S_IFDIR:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539 btrfs_defrag_root(root, 0);
540 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400541 break;
542 case S_IFREG:
543 btrfs_defrag_file(file);
544 break;
545 }
546
547 return 0;
548}
549
550long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
551{
552 struct btrfs_ioctl_vol_args *vol_args;
553 int ret;
554
555 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
556
557 if (!vol_args)
558 return -ENOMEM;
559
560 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
561 ret = -EFAULT;
562 goto out;
563 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400564 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400565 ret = btrfs_init_new_device(root, vol_args->name);
566
567out:
568 kfree(vol_args);
569 return ret;
570}
571
572long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
573{
574 struct btrfs_ioctl_vol_args *vol_args;
575 int ret;
576
577 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
578
579 if (!vol_args)
580 return -ENOMEM;
581
582 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
583 ret = -EFAULT;
584 goto out;
585 }
Mark Fasheh5516e592008-07-24 12:20:14 -0400586 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400587 ret = btrfs_rm_device(root, vol_args->name);
588
589out:
590 kfree(vol_args);
591 return ret;
592}
593
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
595{
596 struct inode *inode = fdentry(file)->d_inode;
597 struct btrfs_root *root = BTRFS_I(inode)->root;
598 struct file *src_file;
599 struct inode *src;
600 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400601 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400602 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400603 char *buf;
604 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400605 u32 nritems;
606 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400607 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608
609 src_file = fget(src_fd);
610 if (!src_file)
611 return -EBADF;
612 src = src_file->f_dentry->d_inode;
613
Yan Zhengae01a0a2008-08-04 23:23:47 -0400614 ret = -EISDIR;
615 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400616 goto out_fput;
617
Yan Zhengae01a0a2008-08-04 23:23:47 -0400618 ret = -EXDEV;
619 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
620 goto out_fput;
621
622 ret = -ENOMEM;
623 buf = vmalloc(btrfs_level_size(root, 0));
624 if (!buf)
625 goto out_fput;
626
627 path = btrfs_alloc_path();
628 if (!path) {
629 vfree(buf);
630 goto out_fput;
631 }
632 path->reada = 2;
633
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634 if (inode < src) {
635 mutex_lock(&inode->i_mutex);
636 mutex_lock(&src->i_mutex);
637 } else {
638 mutex_lock(&src->i_mutex);
639 mutex_lock(&inode->i_mutex);
640 }
641
642 ret = -ENOTEMPTY;
643 if (inode->i_size)
644 goto out_unlock;
645
646 /* do any pending delalloc/csum calc on src, one way or
647 another, and lock file content */
648 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400649 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400650 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400651 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
652 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653 break;
654 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400655 if (ordered)
656 btrfs_put_ordered_extent(ordered);
657 btrfs_wait_ordered_range(src, 0, (u64)-1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658 }
659
Yan Zhengae01a0a2008-08-04 23:23:47 -0400660 trans = btrfs_start_transaction(root, 1);
661 BUG_ON(!trans);
662
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400663 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400664 key.type = BTRFS_EXTENT_DATA_KEY;
665 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666
667 while (1) {
668 /*
669 * note the key will change type as we walk through the
670 * tree.
671 */
672 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
673 if (ret < 0)
674 goto out;
675
Yan Zhengae01a0a2008-08-04 23:23:47 -0400676 nritems = btrfs_header_nritems(path->nodes[0]);
677 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400678 ret = btrfs_next_leaf(root, path);
679 if (ret < 0)
680 goto out;
681 if (ret > 0)
682 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400683 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400684 }
685 leaf = path->nodes[0];
686 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400687
Yan Zhengae01a0a2008-08-04 23:23:47 -0400688 btrfs_item_key_to_cpu(leaf, &key, slot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400689 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
690 key.objectid != src->i_ino)
691 break;
692
Zheng Yan31840ae2008-09-23 13:14:14 -0400693 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY ||
694 btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
695 u32 size;
696 struct btrfs_key new_key;
697
698 size = btrfs_item_size_nr(leaf, slot);
699 read_extent_buffer(leaf, buf,
700 btrfs_item_ptr_offset(leaf, slot),
701 size);
702 btrfs_release_path(root, path);
703
704 memcpy(&new_key, &key, sizeof(new_key));
705 new_key.objectid = inode->i_ino;
706 ret = btrfs_insert_empty_item(trans, root, path,
707 &new_key, size);
708 if (ret)
709 goto out;
710
711 leaf = path->nodes[0];
712 slot = path->slots[0];
713 write_extent_buffer(leaf, buf,
714 btrfs_item_ptr_offset(leaf, slot),
715 size);
716 btrfs_mark_buffer_dirty(leaf);
717 }
718
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400719 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
720 struct btrfs_file_extent_item *extent;
721 int found_type;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400722
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723 extent = btrfs_item_ptr(leaf, slot,
724 struct btrfs_file_extent_item);
725 found_type = btrfs_file_extent_type(leaf, extent);
726 if (found_type == BTRFS_FILE_EXTENT_REG) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
728 extent);
729 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
730 extent);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731 /* ds == 0 means there's a hole */
732 if (ds != 0) {
Yan Zhengae01a0a2008-08-04 23:23:47 -0400733 ret = btrfs_inc_extent_ref(trans, root,
Zheng Yan31840ae2008-09-23 13:14:14 -0400734 ds, dl, leaf->start,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400735 root->root_key.objectid,
736 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400737 inode->i_ino);
Zheng Yan31840ae2008-09-23 13:14:14 -0400738 BUG_ON(ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400739 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 }
Yan Zhengae01a0a2008-08-04 23:23:47 -0400741 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400742 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400744 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745 ret = 0;
746out:
Yan Zhengae01a0a2008-08-04 23:23:47 -0400747 btrfs_release_path(root, path);
748 if (ret == 0) {
749 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400750 inode_set_bytes(inode, inode_get_bytes(src));
Yan Zhengae01a0a2008-08-04 23:23:47 -0400751 btrfs_i_size_write(inode, src->i_size);
752 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
753 ret = btrfs_update_inode(trans, root, inode);
754 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755 btrfs_end_transaction(trans, root);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400756 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
757 if (ret)
758 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400759out_unlock:
760 mutex_unlock(&src->i_mutex);
761 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400762 vfree(buf);
763 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764out_fput:
765 fput(src_file);
766 return ret;
767}
768
769/*
770 * there are many ways the trans_start and trans_end ioctls can lead
771 * to deadlocks. They should only be used by applications that
772 * basically own the machine, and have a very in depth understanding
773 * of all the possible deadlocks and enospc problems.
774 */
775long btrfs_ioctl_trans_start(struct file *file)
776{
777 struct inode *inode = fdentry(file)->d_inode;
778 struct btrfs_root *root = BTRFS_I(inode)->root;
779 struct btrfs_trans_handle *trans;
780 int ret = 0;
781
Christoph Hellwigdf5b5522008-06-11 21:53:58 -0400782 if (!capable(CAP_SYS_ADMIN))
783 return -EPERM;
784
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400785 if (file->private_data) {
786 ret = -EINPROGRESS;
787 goto out;
788 }
Sage Weil9ca9ee02008-08-04 10:41:27 -0400789
790 mutex_lock(&root->fs_info->trans_mutex);
791 root->fs_info->open_ioctl_trans++;
792 mutex_unlock(&root->fs_info->trans_mutex);
793
794 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400795 if (trans)
796 file->private_data = trans;
797 else
798 ret = -ENOMEM;
799 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
800out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400801 return ret;
802}
803
804/*
805 * there are many ways the trans_start and trans_end ioctls can lead
806 * to deadlocks. They should only be used by applications that
807 * basically own the machine, and have a very in depth understanding
808 * of all the possible deadlocks and enospc problems.
809 */
810long btrfs_ioctl_trans_end(struct file *file)
811{
812 struct inode *inode = fdentry(file)->d_inode;
813 struct btrfs_root *root = BTRFS_I(inode)->root;
814 struct btrfs_trans_handle *trans;
815 int ret = 0;
816
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400817 trans = file->private_data;
818 if (!trans) {
819 ret = -EINVAL;
820 goto out;
821 }
822 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -0400823 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -0400824
825 mutex_lock(&root->fs_info->trans_mutex);
826 root->fs_info->open_ioctl_trans--;
827 mutex_unlock(&root->fs_info->trans_mutex);
828
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400829out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400830 return ret;
831}
832
833long btrfs_ioctl(struct file *file, unsigned int
834 cmd, unsigned long arg)
835{
836 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
837
838 switch (cmd) {
839 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400840 return btrfs_ioctl_snap_create(file, (void __user *)arg);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400841 case BTRFS_IOC_DEFRAG:
842 return btrfs_ioctl_defrag(file);
843 case BTRFS_IOC_RESIZE:
844 return btrfs_ioctl_resize(root, (void __user *)arg);
845 case BTRFS_IOC_ADD_DEV:
846 return btrfs_ioctl_add_dev(root, (void __user *)arg);
847 case BTRFS_IOC_RM_DEV:
848 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
849 case BTRFS_IOC_BALANCE:
850 return btrfs_balance(root->fs_info->dev_root);
851 case BTRFS_IOC_CLONE:
852 return btrfs_ioctl_clone(file, arg);
853 case BTRFS_IOC_TRANS_START:
854 return btrfs_ioctl_trans_start(file);
855 case BTRFS_IOC_TRANS_END:
856 return btrfs_ioctl_trans_end(file);
857 case BTRFS_IOC_SYNC:
Chris Masonea8c2812008-08-04 23:17:27 -0400858 btrfs_start_delalloc_inodes(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400859 btrfs_sync_fs(file->f_dentry->d_sb, 1);
860 return 0;
861 }
862
863 return -ENOTTY;
864}