Btrfs: add permission checks to the ioctls
[linux-2.6-block.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
30#include <linux/smp_lock.h>
31#include <linux/backing-dev.h>
cb8e7090 32#include <linux/mount.h>
f46b5a66 33#include <linux/mpage.h>
cb8e7090 34#include <linux/namei.h>
f46b5a66
CH
35#include <linux/swap.h>
36#include <linux/writeback.h>
37#include <linux/statfs.h>
38#include <linux/compat.h>
39#include <linux/bit_spinlock.h>
cb8e7090 40#include <linux/security.h>
f46b5a66
CH
41#include <linux/version.h>
42#include <linux/xattr.h>
7ea394f1 43#include <linux/vmalloc.h>
4b4e25f2 44#include "compat.h"
f46b5a66
CH
45#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
925baedd 52#include "locking.h"
f46b5a66
CH
53
54
55
cb8e7090
CH
56static noinline int create_subvol(struct btrfs_root *root,
57 struct dentry *dentry,
58 char *name, int namelen)
f46b5a66
CH
59{
60 struct btrfs_trans_handle *trans;
61 struct btrfs_key key;
62 struct btrfs_root_item root_item;
63 struct btrfs_inode_item *inode_item;
64 struct extent_buffer *leaf;
65 struct btrfs_root *new_root = root;
66 struct inode *dir;
67 int ret;
68 int err;
69 u64 objectid;
70 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 71 u64 index = 0;
f46b5a66
CH
72 unsigned long nr = 1;
73
f46b5a66
CH
74 ret = btrfs_check_free_space(root, 1, 0);
75 if (ret)
76 goto fail_commit;
77
78 trans = btrfs_start_transaction(root, 1);
79 BUG_ON(!trans);
80
81 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
82 0, &objectid);
83 if (ret)
84 goto fail;
85
31840ae1
ZY
86 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
87 objectid, trans->transid, 0, 0, 0);
8e8a1e31
JB
88 if (IS_ERR(leaf)) {
89 ret = PTR_ERR(leaf);
90 goto fail;
91 }
f46b5a66
CH
92
93 btrfs_set_header_nritems(leaf, 0);
94 btrfs_set_header_level(leaf, 0);
95 btrfs_set_header_bytenr(leaf, leaf->start);
96 btrfs_set_header_generation(leaf, trans->transid);
97 btrfs_set_header_owner(leaf, objectid);
98
99 write_extent_buffer(leaf, root->fs_info->fsid,
100 (unsigned long)btrfs_header_fsid(leaf),
101 BTRFS_FSID_SIZE);
102 btrfs_mark_buffer_dirty(leaf);
103
104 inode_item = &root_item.inode;
105 memset(inode_item, 0, sizeof(*inode_item));
106 inode_item->generation = cpu_to_le64(1);
107 inode_item->size = cpu_to_le64(3);
108 inode_item->nlink = cpu_to_le32(1);
a76a3cd4 109 inode_item->nbytes = cpu_to_le64(root->leafsize);
f46b5a66
CH
110 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
111
112 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 113 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
114 btrfs_set_root_level(&root_item, 0);
115 btrfs_set_root_refs(&root_item, 1);
116 btrfs_set_root_used(&root_item, 0);
80ff3856 117 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66
CH
118
119 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
120 root_item.drop_level = 0;
121
925baedd 122 btrfs_tree_unlock(leaf);
f46b5a66
CH
123 free_extent_buffer(leaf);
124 leaf = NULL;
125
126 btrfs_set_root_dirid(&root_item, new_dirid);
127
128 key.objectid = objectid;
129 key.offset = 1;
130 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
131 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
132 &root_item);
133 if (ret)
134 goto fail;
135
136 /*
137 * insert the directory item
138 */
139 key.offset = (u64)-1;
3de4586c
CM
140 dir = dentry->d_parent->d_inode;
141 ret = btrfs_set_inode_index(dir, &index);
142 BUG_ON(ret);
143
144 ret = btrfs_insert_dir_item(trans, root,
f46b5a66 145 name, namelen, dir->i_ino, &key,
3de4586c 146 BTRFS_FT_DIR, index);
f46b5a66
CH
147 if (ret)
148 goto fail;
0660b5af
CM
149
150 /* add the backref first */
151 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
152 objectid, BTRFS_ROOT_BACKREF_KEY,
153 root->root_key.objectid,
154 dir->i_ino, index, name, namelen);
155
156 BUG_ON(ret);
157
158 /* now add the forward ref */
159 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
160 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
161 objectid,
162 dir->i_ino, index, name, namelen);
163
164 BUG_ON(ret);
165
f46b5a66
CH
166 ret = btrfs_commit_transaction(trans, root);
167 if (ret)
168 goto fail_commit;
169
3de4586c 170 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
f46b5a66
CH
171 BUG_ON(!new_root);
172
173 trans = btrfs_start_transaction(new_root, 1);
174 BUG_ON(!trans);
175
d2fb3437 176 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
f46b5a66
CH
177 BTRFS_I(dir)->block_group);
178 if (ret)
179 goto fail;
180
f46b5a66
CH
181fail:
182 nr = trans->blocks_used;
183 err = btrfs_commit_transaction(trans, new_root);
184 if (err && !ret)
185 ret = err;
186fail_commit:
f46b5a66 187 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
188 return ret;
189}
190
3de4586c
CM
191static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
192 char *name, int namelen)
f46b5a66
CH
193{
194 struct btrfs_pending_snapshot *pending_snapshot;
195 struct btrfs_trans_handle *trans;
3de4586c 196 int ret = 0;
f46b5a66
CH
197 int err;
198 unsigned long nr = 0;
199
200 if (!root->ref_cows)
201 return -EINVAL;
202
f46b5a66
CH
203 ret = btrfs_check_free_space(root, 1, 0);
204 if (ret)
205 goto fail_unlock;
206
3de4586c 207 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
f46b5a66
CH
208 if (!pending_snapshot) {
209 ret = -ENOMEM;
210 goto fail_unlock;
211 }
212 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
213 if (!pending_snapshot->name) {
214 ret = -ENOMEM;
215 kfree(pending_snapshot);
216 goto fail_unlock;
217 }
218 memcpy(pending_snapshot->name, name, namelen);
219 pending_snapshot->name[namelen] = '\0';
3de4586c 220 pending_snapshot->dentry = dentry;
f46b5a66
CH
221 trans = btrfs_start_transaction(root, 1);
222 BUG_ON(!trans);
223 pending_snapshot->root = root;
224 list_add(&pending_snapshot->list,
225 &trans->transaction->pending_snapshots);
f46b5a66
CH
226 err = btrfs_commit_transaction(trans, root);
227
228fail_unlock:
f46b5a66 229 btrfs_btree_balance_dirty(root, nr);
f46b5a66
CH
230 return ret;
231}
232
cb8e7090
CH
233/* copy of may_create in fs/namei.c() */
234static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
235{
236 if (child->d_inode)
237 return -EEXIST;
238 if (IS_DEADDIR(dir))
239 return -ENOENT;
240 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
241}
242
243/*
244 * Create a new subvolume below @parent. This is largely modeled after
245 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
246 * inside this filesystem so it's quite a bit simpler.
247 */
248static noinline int btrfs_mksubvol(struct path *parent, char *name,
3de4586c
CM
249 int mode, int namelen,
250 struct btrfs_root *snap_src)
cb8e7090
CH
251{
252 struct dentry *dentry;
253 int error;
254
255 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
256
257 dentry = lookup_one_len(name, parent->dentry, namelen);
258 error = PTR_ERR(dentry);
259 if (IS_ERR(dentry))
260 goto out_unlock;
261
262 error = -EEXIST;
263 if (dentry->d_inode)
264 goto out_dput;
265
266 if (!IS_POSIXACL(parent->dentry->d_inode))
267 mode &= ~current->fs->umask;
3de4586c 268
cb8e7090
CH
269 error = mnt_want_write(parent->mnt);
270 if (error)
271 goto out_dput;
272
273 error = btrfs_may_create(parent->dentry->d_inode, dentry);
274 if (error)
275 goto out_drop_write;
276
cb8e7090
CH
277 /*
278 * Actually perform the low-level subvolume creation after all
279 * this VFS fuzz.
280 *
281 * Eventually we want to pass in an inode under which we create this
282 * subvolume, but for now all are under the filesystem root.
283 *
284 * Also we should pass on the mode eventually to allow creating new
285 * subvolume with specific mode bits.
286 */
3de4586c 287 if (snap_src) {
ea9e8b11
CM
288 struct dentry *dir = dentry->d_parent;
289 struct dentry *test = dir->d_parent;
290 struct btrfs_path *path = btrfs_alloc_path();
291 int ret;
292 u64 test_oid;
293 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
294
295 test_oid = snap_src->root_key.objectid;
296
297 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
298 path, parent_oid, test_oid);
299 if (ret == 0)
300 goto create;
301 btrfs_release_path(snap_src->fs_info->tree_root, path);
302
303 /* we need to make sure we aren't creating a directory loop
304 * by taking a snapshot of something that has our current
305 * subvol in its directory tree. So, this loops through
306 * the dentries and checks the forward refs for each subvolume
307 * to see if is references the subvolume where we are
308 * placing this new snapshot.
309 */
310 while(1) {
311 if (!test ||
312 dir == snap_src->fs_info->sb->s_root ||
313 test == snap_src->fs_info->sb->s_root ||
314 test->d_inode->i_sb != snap_src->fs_info->sb) {
315 break;
316 }
317 if (S_ISLNK(test->d_inode->i_mode)) {
318 printk("Symlink in snapshot path, failed\n");
319 error = -EMLINK;
320 btrfs_free_path(path);
321 goto out_drop_write;
322 }
323 test_oid =
324 BTRFS_I(test->d_inode)->root->root_key.objectid;
325 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
326 path, test_oid, parent_oid);
327 if (ret == 0) {
328 printk("Snapshot creation failed, looping\n");
329 error = -EMLINK;
330 btrfs_free_path(path);
331 goto out_drop_write;
332 }
333 btrfs_release_path(snap_src->fs_info->tree_root, path);
334 test = test->d_parent;
335 }
336create:
337 btrfs_free_path(path);
3de4586c
CM
338 error = create_snapshot(snap_src, dentry, name, namelen);
339 } else {
340 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
341 dentry, name, namelen);
342 }
cb8e7090
CH
343 if (error)
344 goto out_drop_write;
345
346 fsnotify_mkdir(parent->dentry->d_inode, dentry);
347out_drop_write:
348 mnt_drop_write(parent->mnt);
349out_dput:
350 dput(dentry);
351out_unlock:
352 mutex_unlock(&parent->dentry->d_inode->i_mutex);
353 return error;
354}
355
356
b2950863 357static int btrfs_defrag_file(struct file *file)
f46b5a66
CH
358{
359 struct inode *inode = fdentry(file)->d_inode;
360 struct btrfs_root *root = BTRFS_I(inode)->root;
361 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3eaa2885 362 struct btrfs_ordered_extent *ordered;
f46b5a66
CH
363 struct page *page;
364 unsigned long last_index;
365 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
366 unsigned long total_read = 0;
367 u64 page_start;
368 u64 page_end;
369 unsigned long i;
370 int ret;
371
f46b5a66 372 ret = btrfs_check_free_space(root, inode->i_size, 0);
f46b5a66
CH
373 if (ret)
374 return -ENOSPC;
375
376 mutex_lock(&inode->i_mutex);
377 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
378 for (i = 0; i <= last_index; i++) {
379 if (total_read % ra_pages == 0) {
380 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
381 min(last_index, i + ra_pages - 1));
382 }
383 total_read++;
3eaa2885 384again:
f46b5a66
CH
385 page = grab_cache_page(inode->i_mapping, i);
386 if (!page)
387 goto out_unlock;
388 if (!PageUptodate(page)) {
389 btrfs_readpage(NULL, page);
390 lock_page(page);
391 if (!PageUptodate(page)) {
392 unlock_page(page);
393 page_cache_release(page);
394 goto out_unlock;
395 }
396 }
397
f46b5a66 398 wait_on_page_writeback(page);
f46b5a66
CH
399
400 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
401 page_end = page_start + PAGE_CACHE_SIZE - 1;
f46b5a66 402 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3eaa2885
CM
403
404 ordered = btrfs_lookup_ordered_extent(inode, page_start);
405 if (ordered) {
406 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
407 unlock_page(page);
408 page_cache_release(page);
409 btrfs_start_ordered_extent(inode, ordered, 1);
410 btrfs_put_ordered_extent(ordered);
411 goto again;
412 }
413 set_page_extent_mapped(page);
414
f87f057b
CM
415 /*
416 * this makes sure page_mkwrite is called on the
417 * page if it is dirtied again later
418 */
419 clear_page_dirty_for_io(page);
420
ea8c2819 421 btrfs_set_extent_delalloc(inode, page_start, page_end);
f46b5a66
CH
422
423 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
424 set_page_dirty(page);
425 unlock_page(page);
426 page_cache_release(page);
427 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
428 }
429
430out_unlock:
431 mutex_unlock(&inode->i_mutex);
432 return 0;
433}
434
435/*
436 * Called inside transaction, so use GFP_NOFS
437 */
438
439static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
440{
441 u64 new_size;
442 u64 old_size;
443 u64 devid = 1;
444 struct btrfs_ioctl_vol_args *vol_args;
445 struct btrfs_trans_handle *trans;
446 struct btrfs_device *device = NULL;
447 char *sizestr;
448 char *devstr = NULL;
449 int ret = 0;
450 int namelen;
451 int mod = 0;
452
c146afad
YZ
453 if (root->fs_info->sb->s_flags & MS_RDONLY)
454 return -EROFS;
455
e441d54d
CM
456 if (!capable(CAP_SYS_ADMIN))
457 return -EPERM;
458
f46b5a66
CH
459 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
460
461 if (!vol_args)
462 return -ENOMEM;
463
464 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
465 ret = -EFAULT;
466 goto out;
467 }
5516e595
MF
468
469 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 470 namelen = strlen(vol_args->name);
f46b5a66 471
7d9eb12c 472 mutex_lock(&root->fs_info->volume_mutex);
f46b5a66
CH
473 sizestr = vol_args->name;
474 devstr = strchr(sizestr, ':');
475 if (devstr) {
476 char *end;
477 sizestr = devstr + 1;
478 *devstr = '\0';
479 devstr = vol_args->name;
480 devid = simple_strtoull(devstr, &end, 10);
481 printk(KERN_INFO "resizing devid %llu\n", devid);
482 }
2b82032c 483 device = btrfs_find_device(root, devid, NULL, NULL);
f46b5a66
CH
484 if (!device) {
485 printk(KERN_INFO "resizer unable to find device %llu\n", devid);
486 ret = -EINVAL;
487 goto out_unlock;
488 }
489 if (!strcmp(sizestr, "max"))
490 new_size = device->bdev->bd_inode->i_size;
491 else {
492 if (sizestr[0] == '-') {
493 mod = -1;
494 sizestr++;
495 } else if (sizestr[0] == '+') {
496 mod = 1;
497 sizestr++;
498 }
499 new_size = btrfs_parse_size(sizestr);
500 if (new_size == 0) {
501 ret = -EINVAL;
502 goto out_unlock;
503 }
504 }
505
506 old_size = device->total_bytes;
507
508 if (mod < 0) {
509 if (new_size > old_size) {
510 ret = -EINVAL;
511 goto out_unlock;
512 }
513 new_size = old_size - new_size;
514 } else if (mod > 0) {
515 new_size = old_size + new_size;
516 }
517
518 if (new_size < 256 * 1024 * 1024) {
519 ret = -EINVAL;
520 goto out_unlock;
521 }
522 if (new_size > device->bdev->bd_inode->i_size) {
523 ret = -EFBIG;
524 goto out_unlock;
525 }
526
527 do_div(new_size, root->sectorsize);
528 new_size *= root->sectorsize;
529
530 printk(KERN_INFO "new size for %s is %llu\n",
531 device->name, (unsigned long long)new_size);
532
533 if (new_size > old_size) {
534 trans = btrfs_start_transaction(root, 1);
535 ret = btrfs_grow_device(trans, device, new_size);
536 btrfs_commit_transaction(trans, root);
537 } else {
538 ret = btrfs_shrink_device(device, new_size);
539 }
540
541out_unlock:
7d9eb12c 542 mutex_unlock(&root->fs_info->volume_mutex);
f46b5a66
CH
543out:
544 kfree(vol_args);
545 return ret;
546}
547
cb8e7090 548static noinline int btrfs_ioctl_snap_create(struct file *file,
3de4586c 549 void __user *arg, int subvol)
f46b5a66 550{
cb8e7090 551 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
f46b5a66
CH
552 struct btrfs_ioctl_vol_args *vol_args;
553 struct btrfs_dir_item *di;
554 struct btrfs_path *path;
3de4586c 555 struct file *src_file;
f46b5a66
CH
556 u64 root_dirid;
557 int namelen;
3de4586c 558 int ret = 0;
f46b5a66 559
c146afad
YZ
560 if (root->fs_info->sb->s_flags & MS_RDONLY)
561 return -EROFS;
562
f46b5a66
CH
563 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
564
565 if (!vol_args)
566 return -ENOMEM;
567
568 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
569 ret = -EFAULT;
570 goto out;
571 }
572
5516e595 573 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 574 namelen = strlen(vol_args->name);
f46b5a66
CH
575 if (strchr(vol_args->name, '/')) {
576 ret = -EINVAL;
577 goto out;
578 }
579
580 path = btrfs_alloc_path();
581 if (!path) {
582 ret = -ENOMEM;
583 goto out;
584 }
585
586 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
f46b5a66
CH
587 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
588 path, root_dirid,
589 vol_args->name, namelen, 0);
f46b5a66
CH
590 btrfs_free_path(path);
591
592 if (di && !IS_ERR(di)) {
593 ret = -EEXIST;
594 goto out;
595 }
596
597 if (IS_ERR(di)) {
598 ret = PTR_ERR(di);
599 goto out;
600 }
601
3de4586c 602 if (subvol) {
cb8e7090
CH
603 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
604 file->f_path.dentry->d_inode->i_mode,
3de4586c 605 namelen, NULL);
cb8e7090 606 } else {
3de4586c
CM
607 struct inode *src_inode;
608 src_file = fget(vol_args->fd);
609 if (!src_file) {
610 ret = -EINVAL;
611 goto out;
612 }
613
614 src_inode = src_file->f_path.dentry->d_inode;
615 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
616 printk("btrfs: Snapshot src from another FS\n");
617 ret = -EINVAL;
618 fput(src_file);
619 goto out;
620 }
621 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
622 file->f_path.dentry->d_inode->i_mode,
623 namelen, BTRFS_I(src_inode)->root);
624 fput(src_file);
cb8e7090
CH
625 }
626
f46b5a66
CH
627out:
628 kfree(vol_args);
629 return ret;
630}
631
632static int btrfs_ioctl_defrag(struct file *file)
633{
634 struct inode *inode = fdentry(file)->d_inode;
635 struct btrfs_root *root = BTRFS_I(inode)->root;
c146afad
YZ
636 int ret;
637
638 ret = mnt_want_write(file->f_path.mnt);
639 if (ret)
640 return ret;
f46b5a66
CH
641
642 switch (inode->i_mode & S_IFMT) {
643 case S_IFDIR:
e441d54d
CM
644 if (!capable(CAP_SYS_ADMIN)) {
645 ret = -EPERM;
646 goto out;
647 }
f46b5a66
CH
648 btrfs_defrag_root(root, 0);
649 btrfs_defrag_root(root->fs_info->extent_root, 0);
f46b5a66
CH
650 break;
651 case S_IFREG:
e441d54d
CM
652 if (!(file->f_mode & FMODE_WRITE)) {
653 ret = -EINVAL;
654 goto out;
655 }
f46b5a66
CH
656 btrfs_defrag_file(file);
657 break;
658 }
e441d54d 659out:
ab67b7c1 660 mnt_drop_write(file->f_path.mnt);
e441d54d 661 return ret;
f46b5a66
CH
662}
663
b2950863 664static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
665{
666 struct btrfs_ioctl_vol_args *vol_args;
667 int ret;
668
e441d54d
CM
669 if (!capable(CAP_SYS_ADMIN))
670 return -EPERM;
671
f46b5a66
CH
672 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
673
674 if (!vol_args)
675 return -ENOMEM;
676
677 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
678 ret = -EFAULT;
679 goto out;
680 }
5516e595 681 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
682 ret = btrfs_init_new_device(root, vol_args->name);
683
684out:
685 kfree(vol_args);
686 return ret;
687}
688
b2950863 689static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
690{
691 struct btrfs_ioctl_vol_args *vol_args;
692 int ret;
693
e441d54d
CM
694 if (!capable(CAP_SYS_ADMIN))
695 return -EPERM;
696
c146afad
YZ
697 if (root->fs_info->sb->s_flags & MS_RDONLY)
698 return -EROFS;
699
f46b5a66
CH
700 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
701
702 if (!vol_args)
703 return -ENOMEM;
704
705 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
706 ret = -EFAULT;
707 goto out;
708 }
5516e595 709 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
710 ret = btrfs_rm_device(root, vol_args->name);
711
712out:
713 kfree(vol_args);
714 return ret;
715}
716
b2950863
CH
717static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
718 u64 off, u64 olen, u64 destoff)
f46b5a66
CH
719{
720 struct inode *inode = fdentry(file)->d_inode;
721 struct btrfs_root *root = BTRFS_I(inode)->root;
722 struct file *src_file;
723 struct inode *src;
724 struct btrfs_trans_handle *trans;
f46b5a66 725 struct btrfs_path *path;
f46b5a66 726 struct extent_buffer *leaf;
ae01a0ab
YZ
727 char *buf;
728 struct btrfs_key key;
f46b5a66
CH
729 u32 nritems;
730 int slot;
ae01a0ab 731 int ret;
c5c9cd4d
SW
732 u64 len = olen;
733 u64 bs = root->fs_info->sb->s_blocksize;
734 u64 hint_byte;
d20f7043 735
c5c9cd4d
SW
736 /*
737 * TODO:
738 * - split compressed inline extents. annoying: we need to
739 * decompress into destination's address_space (the file offset
740 * may change, so source mapping won't do), then recompress (or
741 * otherwise reinsert) a subrange.
742 * - allow ranges within the same file to be cloned (provided
743 * they don't overlap)?
744 */
745
e441d54d
CM
746 /* the destination must be opened for writing */
747 if (!(file->f_mode & FMODE_WRITE))
748 return -EINVAL;
749
c146afad
YZ
750 ret = mnt_want_write(file->f_path.mnt);
751 if (ret)
752 return ret;
753
c5c9cd4d 754 src_file = fget(srcfd);
ab67b7c1
YZ
755 if (!src_file) {
756 ret = -EBADF;
757 goto out_drop_write;
758 }
f46b5a66
CH
759 src = src_file->f_dentry->d_inode;
760
c5c9cd4d
SW
761 ret = -EINVAL;
762 if (src == inode)
763 goto out_fput;
764
ae01a0ab
YZ
765 ret = -EISDIR;
766 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
767 goto out_fput;
768
f46b5a66 769 ret = -EXDEV;
ae01a0ab
YZ
770 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
771 goto out_fput;
772
773 ret = -ENOMEM;
774 buf = vmalloc(btrfs_level_size(root, 0));
775 if (!buf)
776 goto out_fput;
777
778 path = btrfs_alloc_path();
779 if (!path) {
780 vfree(buf);
f46b5a66 781 goto out_fput;
ae01a0ab
YZ
782 }
783 path->reada = 2;
f46b5a66
CH
784
785 if (inode < src) {
786 mutex_lock(&inode->i_mutex);
787 mutex_lock(&src->i_mutex);
788 } else {
789 mutex_lock(&src->i_mutex);
790 mutex_lock(&inode->i_mutex);
791 }
792
c5c9cd4d
SW
793 /* determine range to clone */
794 ret = -EINVAL;
795 if (off >= src->i_size || off + len > src->i_size)
f46b5a66 796 goto out_unlock;
c5c9cd4d
SW
797 if (len == 0)
798 olen = len = src->i_size - off;
799 /* if we extend to eof, continue to block boundary */
800 if (off + len == src->i_size)
801 len = ((src->i_size + bs-1) & ~(bs-1))
802 - off;
803
804 /* verify the end result is block aligned */
805 if ((off & (bs-1)) ||
806 ((off + len) & (bs-1)))
807 goto out_unlock;
808
809 printk("final src extent is %llu~%llu\n", off, len);
810 printk("final dst extent is %llu~%llu\n", destoff, len);
f46b5a66
CH
811
812 /* do any pending delalloc/csum calc on src, one way or
813 another, and lock file content */
814 while (1) {
31840ae1 815 struct btrfs_ordered_extent *ordered;
c5c9cd4d
SW
816 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
817 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
ae01a0ab 818 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
f46b5a66 819 break;
c5c9cd4d 820 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
821 if (ordered)
822 btrfs_put_ordered_extent(ordered);
c5c9cd4d 823 btrfs_wait_ordered_range(src, off, off+len);
f46b5a66
CH
824 }
825
ae01a0ab
YZ
826 trans = btrfs_start_transaction(root, 1);
827 BUG_ON(!trans);
828
c5c9cd4d
SW
829 /* punch hole in destination first */
830 btrfs_drop_extents(trans, root, inode, off, off+len, 0, &hint_byte);
831
832 /* clone data */
f46b5a66 833 key.objectid = src->i_ino;
ae01a0ab
YZ
834 key.type = BTRFS_EXTENT_DATA_KEY;
835 key.offset = 0;
f46b5a66
CH
836
837 while (1) {
838 /*
839 * note the key will change type as we walk through the
840 * tree.
841 */
842 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
843 if (ret < 0)
844 goto out;
845
ae01a0ab
YZ
846 nritems = btrfs_header_nritems(path->nodes[0]);
847 if (path->slots[0] >= nritems) {
f46b5a66
CH
848 ret = btrfs_next_leaf(root, path);
849 if (ret < 0)
850 goto out;
851 if (ret > 0)
852 break;
ae01a0ab 853 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
854 }
855 leaf = path->nodes[0];
856 slot = path->slots[0];
f46b5a66 857
ae01a0ab 858 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 859 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
f46b5a66
CH
860 key.objectid != src->i_ino)
861 break;
862
c5c9cd4d
SW
863 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
864 struct btrfs_file_extent_item *extent;
865 int type;
31840ae1
ZY
866 u32 size;
867 struct btrfs_key new_key;
c5c9cd4d
SW
868 u64 disko = 0, diskl = 0;
869 u64 datao = 0, datal = 0;
870 u8 comp;
31840ae1
ZY
871
872 size = btrfs_item_size_nr(leaf, slot);
873 read_extent_buffer(leaf, buf,
874 btrfs_item_ptr_offset(leaf, slot),
875 size);
c5c9cd4d
SW
876
877 extent = btrfs_item_ptr(leaf, slot,
878 struct btrfs_file_extent_item);
879 comp = btrfs_file_extent_compression(leaf, extent);
880 type = btrfs_file_extent_type(leaf, extent);
881 if (type == BTRFS_FILE_EXTENT_REG) {
882 disko = btrfs_file_extent_disk_bytenr(leaf, extent);
883 diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
884 datao = btrfs_file_extent_offset(leaf, extent);
885 datal = btrfs_file_extent_num_bytes(leaf, extent);
886 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
887 /* take upper bound, may be compressed */
888 datal = btrfs_file_extent_ram_bytes(leaf,
889 extent);
890 }
31840ae1
ZY
891 btrfs_release_path(root, path);
892
c5c9cd4d
SW
893 if (key.offset + datal < off ||
894 key.offset >= off+len)
895 goto next;
896
31840ae1
ZY
897 memcpy(&new_key, &key, sizeof(new_key));
898 new_key.objectid = inode->i_ino;
c5c9cd4d 899 new_key.offset = key.offset + destoff - off;
31840ae1 900
c5c9cd4d
SW
901 if (type == BTRFS_FILE_EXTENT_REG) {
902 ret = btrfs_insert_empty_item(trans, root, path,
903 &new_key, size);
904 if (ret)
905 goto out;
906
907 leaf = path->nodes[0];
908 slot = path->slots[0];
909 write_extent_buffer(leaf, buf,
31840ae1
ZY
910 btrfs_item_ptr_offset(leaf, slot),
911 size);
ae01a0ab 912
c5c9cd4d 913 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 914 struct btrfs_file_extent_item);
c5c9cd4d
SW
915 printk(" orig disk %llu~%llu data %llu~%llu\n",
916 disko, diskl, datao, datal);
917
918 if (off > key.offset) {
919 datao += off - key.offset;
920 datal -= off - key.offset;
921 }
922 if (key.offset + datao + datal + key.offset >
923 off + len)
924 datal = off + len - key.offset - datao;
925 /* disko == 0 means it's a hole */
926 if (!disko)
927 datao = 0;
928 printk(" final disk %llu~%llu data %llu~%llu\n",
929 disko, diskl, datao, datal);
930
931 btrfs_set_file_extent_offset(leaf, extent,
932 datao);
933 btrfs_set_file_extent_num_bytes(leaf, extent,
934 datal);
935 if (disko) {
936 inode_add_bytes(inode, datal);
ae01a0ab 937 ret = btrfs_inc_extent_ref(trans, root,
c5c9cd4d
SW
938 disko, diskl, leaf->start,
939 root->root_key.objectid,
940 trans->transid,
941 inode->i_ino);
31840ae1 942 BUG_ON(ret);
f46b5a66 943 }
c5c9cd4d
SW
944 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
945 u64 skip = 0;
946 u64 trim = 0;
947 if (off > key.offset) {
948 skip = off - key.offset;
949 new_key.offset += skip;
950 }
951 if (key.offset + datal > off+len)
952 trim = key.offset + datal - (off+len);
953 printk("len %lld skip %lld trim %lld\n",
954 datal, skip, trim);
955 if (comp && (skip || trim)) {
956 printk("btrfs clone_range can't split compressed inline extents yet\n");
957 ret = -EINVAL;
958 goto out;
959 }
960 size -= skip + trim;
961 datal -= skip + trim;
962 ret = btrfs_insert_empty_item(trans, root, path,
963 &new_key, size);
964 if (ret)
965 goto out;
966
967 if (skip) {
968 u32 start = btrfs_file_extent_calc_inline_size(0);
969 memmove(buf+start, buf+start+skip,
970 datal);
971 }
972
973 leaf = path->nodes[0];
974 slot = path->slots[0];
975 write_extent_buffer(leaf, buf,
976 btrfs_item_ptr_offset(leaf, slot),
977 size);
978 inode_add_bytes(inode, datal);
f46b5a66 979 }
c5c9cd4d
SW
980
981 btrfs_mark_buffer_dirty(leaf);
ae01a0ab 982 }
c5c9cd4d 983
c5c9cd4d 984 next:
31840ae1 985 btrfs_release_path(root, path);
f46b5a66 986 key.offset++;
f46b5a66 987 }
f46b5a66
CH
988 ret = 0;
989out:
ae01a0ab
YZ
990 btrfs_release_path(root, path);
991 if (ret == 0) {
992 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
c5c9cd4d
SW
993 if (destoff + olen > inode->i_size)
994 btrfs_i_size_write(inode, destoff + olen);
ae01a0ab
YZ
995 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
996 ret = btrfs_update_inode(trans, root, inode);
997 }
f46b5a66 998 btrfs_end_transaction(trans, root);
c5c9cd4d 999 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
ae01a0ab
YZ
1000 if (ret)
1001 vmtruncate(inode, 0);
f46b5a66
CH
1002out_unlock:
1003 mutex_unlock(&src->i_mutex);
1004 mutex_unlock(&inode->i_mutex);
ae01a0ab
YZ
1005 vfree(buf);
1006 btrfs_free_path(path);
f46b5a66
CH
1007out_fput:
1008 fput(src_file);
ab67b7c1
YZ
1009out_drop_write:
1010 mnt_drop_write(file->f_path.mnt);
f46b5a66
CH
1011 return ret;
1012}
1013
7a865e8a 1014static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
1015{
1016 struct btrfs_ioctl_clone_range_args args;
1017
7a865e8a 1018 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
1019 return -EFAULT;
1020 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1021 args.src_length, args.dest_offset);
1022}
1023
f46b5a66
CH
1024/*
1025 * there are many ways the trans_start and trans_end ioctls can lead
1026 * to deadlocks. They should only be used by applications that
1027 * basically own the machine, and have a very in depth understanding
1028 * of all the possible deadlocks and enospc problems.
1029 */
b2950863 1030static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66
CH
1031{
1032 struct inode *inode = fdentry(file)->d_inode;
1033 struct btrfs_root *root = BTRFS_I(inode)->root;
1034 struct btrfs_trans_handle *trans;
1035 int ret = 0;
1036
df5b5520
CH
1037 if (!capable(CAP_SYS_ADMIN))
1038 return -EPERM;
1039
f46b5a66
CH
1040 if (file->private_data) {
1041 ret = -EINPROGRESS;
1042 goto out;
1043 }
9ca9ee09 1044
c146afad
YZ
1045 ret = mnt_want_write(file->f_path.mnt);
1046 if (ret)
1047 goto out;
1048
9ca9ee09
SW
1049 mutex_lock(&root->fs_info->trans_mutex);
1050 root->fs_info->open_ioctl_trans++;
1051 mutex_unlock(&root->fs_info->trans_mutex);
1052
1053 trans = btrfs_start_ioctl_transaction(root, 0);
f46b5a66
CH
1054 if (trans)
1055 file->private_data = trans;
1056 else
1057 ret = -ENOMEM;
1058 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1059out:
f46b5a66
CH
1060 return ret;
1061}
1062
1063/*
1064 * there are many ways the trans_start and trans_end ioctls can lead
1065 * to deadlocks. They should only be used by applications that
1066 * basically own the machine, and have a very in depth understanding
1067 * of all the possible deadlocks and enospc problems.
1068 */
1069long btrfs_ioctl_trans_end(struct file *file)
1070{
1071 struct inode *inode = fdentry(file)->d_inode;
1072 struct btrfs_root *root = BTRFS_I(inode)->root;
1073 struct btrfs_trans_handle *trans;
1074 int ret = 0;
1075
f46b5a66
CH
1076 trans = file->private_data;
1077 if (!trans) {
1078 ret = -EINVAL;
1079 goto out;
1080 }
1081 btrfs_end_transaction(trans, root);
b214107e 1082 file->private_data = NULL;
9ca9ee09
SW
1083
1084 mutex_lock(&root->fs_info->trans_mutex);
1085 root->fs_info->open_ioctl_trans--;
1086 mutex_unlock(&root->fs_info->trans_mutex);
1087
cfc8ea87
SW
1088 mnt_drop_write(file->f_path.mnt);
1089
f46b5a66 1090out:
f46b5a66
CH
1091 return ret;
1092}
1093
1094long btrfs_ioctl(struct file *file, unsigned int
1095 cmd, unsigned long arg)
1096{
1097 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4bcabaa3 1098 void __user *argp = (void __user *)arg;
f46b5a66
CH
1099
1100 switch (cmd) {
1101 case BTRFS_IOC_SNAP_CREATE:
4bcabaa3 1102 return btrfs_ioctl_snap_create(file, argp, 0);
3de4586c 1103 case BTRFS_IOC_SUBVOL_CREATE:
4bcabaa3 1104 return btrfs_ioctl_snap_create(file, argp, 1);
f46b5a66
CH
1105 case BTRFS_IOC_DEFRAG:
1106 return btrfs_ioctl_defrag(file);
1107 case BTRFS_IOC_RESIZE:
4bcabaa3 1108 return btrfs_ioctl_resize(root, argp);
f46b5a66 1109 case BTRFS_IOC_ADD_DEV:
4bcabaa3 1110 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 1111 case BTRFS_IOC_RM_DEV:
4bcabaa3 1112 return btrfs_ioctl_rm_dev(root, argp);
f46b5a66
CH
1113 case BTRFS_IOC_BALANCE:
1114 return btrfs_balance(root->fs_info->dev_root);
1115 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
1116 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1117 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 1118 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
1119 case BTRFS_IOC_TRANS_START:
1120 return btrfs_ioctl_trans_start(file);
1121 case BTRFS_IOC_TRANS_END:
1122 return btrfs_ioctl_trans_end(file);
1123 case BTRFS_IOC_SYNC:
1124 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1125 return 0;
1126 }
1127
1128 return -ENOTTY;
1129}