aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ioctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/ioctl.c')
-rw-r--r--fs/btrfs/ioctl.c486
1 files changed, 400 insertions, 86 deletions
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 0e92e5763005..bc2f6ffff3cf 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -41,6 +41,7 @@
41#include <linux/vmalloc.h> 41#include <linux/vmalloc.h>
42#include <linux/slab.h> 42#include <linux/slab.h>
43#include <linux/blkdev.h> 43#include <linux/blkdev.h>
44#include <linux/uuid.h>
44#include "compat.h" 45#include "compat.h"
45#include "ctree.h" 46#include "ctree.h"
46#include "disk-io.h" 47#include "disk-io.h"
@@ -53,6 +54,7 @@
53#include "inode-map.h" 54#include "inode-map.h"
54#include "backref.h" 55#include "backref.h"
55#include "rcu-string.h" 56#include "rcu-string.h"
57#include "send.h"
56 58
57/* Mask out flags that are inappropriate for the given type of inode. */ 59/* Mask out flags that are inappropriate for the given type of inode. */
58static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 60static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
@@ -193,6 +195,10 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
193 if (!inode_owner_or_capable(inode)) 195 if (!inode_owner_or_capable(inode))
194 return -EACCES; 196 return -EACCES;
195 197
198 ret = mnt_want_write_file(file);
199 if (ret)
200 return ret;
201
196 mutex_lock(&inode->i_mutex); 202 mutex_lock(&inode->i_mutex);
197 203
198 ip_oldflags = ip->flags; 204 ip_oldflags = ip->flags;
@@ -207,10 +213,6 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
207 } 213 }
208 } 214 }
209 215
210 ret = mnt_want_write_file(file);
211 if (ret)
212 goto out_unlock;
213
214 if (flags & FS_SYNC_FL) 216 if (flags & FS_SYNC_FL)
215 ip->flags |= BTRFS_INODE_SYNC; 217 ip->flags |= BTRFS_INODE_SYNC;
216 else 218 else
@@ -273,9 +275,9 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
273 inode->i_flags = i_oldflags; 275 inode->i_flags = i_oldflags;
274 } 276 }
275 277
276 mnt_drop_write_file(file);
277 out_unlock: 278 out_unlock:
278 mutex_unlock(&inode->i_mutex); 279 mutex_unlock(&inode->i_mutex);
280 mnt_drop_write_file(file);
279 return ret; 281 return ret;
280} 282}
281 283
@@ -336,7 +338,8 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
336static noinline int create_subvol(struct btrfs_root *root, 338static noinline int create_subvol(struct btrfs_root *root,
337 struct dentry *dentry, 339 struct dentry *dentry,
338 char *name, int namelen, 340 char *name, int namelen,
339 u64 *async_transid) 341 u64 *async_transid,
342 struct btrfs_qgroup_inherit **inherit)
340{ 343{
341 struct btrfs_trans_handle *trans; 344 struct btrfs_trans_handle *trans;
342 struct btrfs_key key; 345 struct btrfs_key key;
@@ -346,11 +349,13 @@ static noinline int create_subvol(struct btrfs_root *root,
346 struct btrfs_root *new_root; 349 struct btrfs_root *new_root;
347 struct dentry *parent = dentry->d_parent; 350 struct dentry *parent = dentry->d_parent;
348 struct inode *dir; 351 struct inode *dir;
352 struct timespec cur_time = CURRENT_TIME;
349 int ret; 353 int ret;
350 int err; 354 int err;
351 u64 objectid; 355 u64 objectid;
352 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 356 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
353 u64 index = 0; 357 u64 index = 0;
358 uuid_le new_uuid;
354 359
355 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 360 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
356 if (ret) 361 if (ret)
@@ -368,6 +373,11 @@ static noinline int create_subvol(struct btrfs_root *root,
368 if (IS_ERR(trans)) 373 if (IS_ERR(trans))
369 return PTR_ERR(trans); 374 return PTR_ERR(trans);
370 375
376 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
377 inherit ? *inherit : NULL);
378 if (ret)
379 goto fail;
380
371 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 381 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
372 0, objectid, NULL, 0, 0, 0); 382 0, objectid, NULL, 0, 0, 0);
373 if (IS_ERR(leaf)) { 383 if (IS_ERR(leaf)) {
@@ -389,8 +399,9 @@ static noinline int create_subvol(struct btrfs_root *root,
389 BTRFS_UUID_SIZE); 399 BTRFS_UUID_SIZE);
390 btrfs_mark_buffer_dirty(leaf); 400 btrfs_mark_buffer_dirty(leaf);
391 401
402 memset(&root_item, 0, sizeof(root_item));
403
392 inode_item = &root_item.inode; 404 inode_item = &root_item.inode;
393 memset(inode_item, 0, sizeof(*inode_item));
394 inode_item->generation = cpu_to_le64(1); 405 inode_item->generation = cpu_to_le64(1);
395 inode_item->size = cpu_to_le64(3); 406 inode_item->size = cpu_to_le64(3);
396 inode_item->nlink = cpu_to_le32(1); 407 inode_item->nlink = cpu_to_le32(1);
@@ -408,8 +419,15 @@ static noinline int create_subvol(struct btrfs_root *root,
408 btrfs_set_root_used(&root_item, leaf->len); 419 btrfs_set_root_used(&root_item, leaf->len);
409 btrfs_set_root_last_snapshot(&root_item, 0); 420 btrfs_set_root_last_snapshot(&root_item, 0);
410 421
411 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 422 btrfs_set_root_generation_v2(&root_item,
412 root_item.drop_level = 0; 423 btrfs_root_generation(&root_item));
424 uuid_le_gen(&new_uuid);
425 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
426 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
427 root_item.otime.nsec = cpu_to_le64(cur_time.tv_nsec);
428 root_item.ctime = root_item.otime;
429 btrfs_set_root_ctransid(&root_item, trans->transid);
430 btrfs_set_root_otransid(&root_item, trans->transid);
413 431
414 btrfs_tree_unlock(leaf); 432 btrfs_tree_unlock(leaf);
415 free_extent_buffer(leaf); 433 free_extent_buffer(leaf);
@@ -484,7 +502,7 @@ fail:
484 502
485static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 503static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
486 char *name, int namelen, u64 *async_transid, 504 char *name, int namelen, u64 *async_transid,
487 bool readonly) 505 bool readonly, struct btrfs_qgroup_inherit **inherit)
488{ 506{
489 struct inode *inode; 507 struct inode *inode;
490 struct btrfs_pending_snapshot *pending_snapshot; 508 struct btrfs_pending_snapshot *pending_snapshot;
@@ -502,6 +520,10 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
502 pending_snapshot->dentry = dentry; 520 pending_snapshot->dentry = dentry;
503 pending_snapshot->root = root; 521 pending_snapshot->root = root;
504 pending_snapshot->readonly = readonly; 522 pending_snapshot->readonly = readonly;
523 if (inherit) {
524 pending_snapshot->inherit = *inherit;
525 *inherit = NULL; /* take responsibility to free it */
526 }
505 527
506 trans = btrfs_start_transaction(root->fs_info->extent_root, 5); 528 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
507 if (IS_ERR(trans)) { 529 if (IS_ERR(trans)) {
@@ -635,12 +657,17 @@ static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
635static noinline int btrfs_mksubvol(struct path *parent, 657static noinline int btrfs_mksubvol(struct path *parent,
636 char *name, int namelen, 658 char *name, int namelen,
637 struct btrfs_root *snap_src, 659 struct btrfs_root *snap_src,
638 u64 *async_transid, bool readonly) 660 u64 *async_transid, bool readonly,
661 struct btrfs_qgroup_inherit **inherit)
639{ 662{
640 struct inode *dir = parent->dentry->d_inode; 663 struct inode *dir = parent->dentry->d_inode;
641 struct dentry *dentry; 664 struct dentry *dentry;
642 int error; 665 int error;
643 666
667 error = mnt_want_write(parent->mnt);
668 if (error)
669 return error;
670
644 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 671 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
645 672
646 dentry = lookup_one_len(name, parent->dentry, namelen); 673 dentry = lookup_one_len(name, parent->dentry, namelen);
@@ -652,13 +679,9 @@ static noinline int btrfs_mksubvol(struct path *parent,
652 if (dentry->d_inode) 679 if (dentry->d_inode)
653 goto out_dput; 680 goto out_dput;
654 681
655 error = mnt_want_write(parent->mnt);
656 if (error)
657 goto out_dput;
658
659 error = btrfs_may_create(dir, dentry); 682 error = btrfs_may_create(dir, dentry);
660 if (error) 683 if (error)
661 goto out_drop_write; 684 goto out_dput;
662 685
663 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 686 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
664 687
@@ -666,22 +689,21 @@ static noinline int btrfs_mksubvol(struct path *parent,
666 goto out_up_read; 689 goto out_up_read;
667 690
668 if (snap_src) { 691 if (snap_src) {
669 error = create_snapshot(snap_src, dentry, 692 error = create_snapshot(snap_src, dentry, name, namelen,
670 name, namelen, async_transid, readonly); 693 async_transid, readonly, inherit);
671 } else { 694 } else {
672 error = create_subvol(BTRFS_I(dir)->root, dentry, 695 error = create_subvol(BTRFS_I(dir)->root, dentry,
673 name, namelen, async_transid); 696 name, namelen, async_transid, inherit);
674 } 697 }
675 if (!error) 698 if (!error)
676 fsnotify_mkdir(dir, dentry); 699 fsnotify_mkdir(dir, dentry);
677out_up_read: 700out_up_read:
678 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 701 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
679out_drop_write:
680 mnt_drop_write(parent->mnt);
681out_dput: 702out_dput:
682 dput(dentry); 703 dput(dentry);
683out_unlock: 704out_unlock:
684 mutex_unlock(&dir->i_mutex); 705 mutex_unlock(&dir->i_mutex);
706 mnt_drop_write(parent->mnt);
685 return error; 707 return error;
686} 708}
687 709
@@ -832,7 +854,8 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
832} 854}
833 855
834static int should_defrag_range(struct inode *inode, u64 start, int thresh, 856static int should_defrag_range(struct inode *inode, u64 start, int thresh,
835 u64 *last_len, u64 *skip, u64 *defrag_end) 857 u64 *last_len, u64 *skip, u64 *defrag_end,
858 int compress)
836{ 859{
837 struct extent_map *em; 860 struct extent_map *em;
838 int ret = 1; 861 int ret = 1;
@@ -863,7 +886,7 @@ static int should_defrag_range(struct inode *inode, u64 start, int thresh,
863 * we hit a real extent, if it is big or the next extent is not a 886 * we hit a real extent, if it is big or the next extent is not a
864 * real extent, don't bother defragging it 887 * real extent, don't bother defragging it
865 */ 888 */
866 if ((*last_len == 0 || *last_len >= thresh) && 889 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
867 (em->len >= thresh || !next_mergeable)) 890 (em->len >= thresh || !next_mergeable))
868 ret = 0; 891 ret = 0;
869out: 892out:
@@ -1047,11 +1070,9 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1047 u64 newer_than, unsigned long max_to_defrag) 1070 u64 newer_than, unsigned long max_to_defrag)
1048{ 1071{
1049 struct btrfs_root *root = BTRFS_I(inode)->root; 1072 struct btrfs_root *root = BTRFS_I(inode)->root;
1050 struct btrfs_super_block *disk_super;
1051 struct file_ra_state *ra = NULL; 1073 struct file_ra_state *ra = NULL;
1052 unsigned long last_index; 1074 unsigned long last_index;
1053 u64 isize = i_size_read(inode); 1075 u64 isize = i_size_read(inode);
1054 u64 features;
1055 u64 last_len = 0; 1076 u64 last_len = 0;
1056 u64 skip = 0; 1077 u64 skip = 0;
1057 u64 defrag_end = 0; 1078 u64 defrag_end = 0;
@@ -1145,7 +1166,8 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1145 1166
1146 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 1167 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1147 extent_thresh, &last_len, &skip, 1168 extent_thresh, &last_len, &skip,
1148 &defrag_end)) { 1169 &defrag_end, range->flags &
1170 BTRFS_DEFRAG_RANGE_COMPRESS)) {
1149 unsigned long next; 1171 unsigned long next;
1150 /* 1172 /*
1151 * the should_defrag function tells us how much to skip 1173 * the should_defrag function tells us how much to skip
@@ -1237,11 +1259,8 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1237 mutex_unlock(&inode->i_mutex); 1259 mutex_unlock(&inode->i_mutex);
1238 } 1260 }
1239 1261
1240 disk_super = root->fs_info->super_copy;
1241 features = btrfs_super_incompat_flags(disk_super);
1242 if (range->compress_type == BTRFS_COMPRESS_LZO) { 1262 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1243 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 1263 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1244 btrfs_set_super_incompat_flags(disk_super, features);
1245 } 1264 }
1246 1265
1247 ret = defrag_count; 1266 ret = defrag_count;
@@ -1379,41 +1398,39 @@ out:
1379} 1398}
1380 1399
1381static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 1400static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1382 char *name, 1401 char *name, unsigned long fd, int subvol,
1383 unsigned long fd, 1402 u64 *transid, bool readonly,
1384 int subvol, 1403 struct btrfs_qgroup_inherit **inherit)
1385 u64 *transid,
1386 bool readonly)
1387{ 1404{
1388 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1389 struct file *src_file; 1405 struct file *src_file;
1390 int namelen; 1406 int namelen;
1391 int ret = 0; 1407 int ret = 0;
1392 1408
1393 if (root->fs_info->sb->s_flags & MS_RDONLY) 1409 ret = mnt_want_write_file(file);
1394 return -EROFS; 1410 if (ret)
1411 goto out;
1395 1412
1396 namelen = strlen(name); 1413 namelen = strlen(name);
1397 if (strchr(name, '/')) { 1414 if (strchr(name, '/')) {
1398 ret = -EINVAL; 1415 ret = -EINVAL;
1399 goto out; 1416 goto out_drop_write;
1400 } 1417 }
1401 1418
1402 if (name[0] == '.' && 1419 if (name[0] == '.' &&
1403 (namelen == 1 || (name[1] == '.' && namelen == 2))) { 1420 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1404 ret = -EEXIST; 1421 ret = -EEXIST;
1405 goto out; 1422 goto out_drop_write;
1406 } 1423 }
1407 1424
1408 if (subvol) { 1425 if (subvol) {
1409 ret = btrfs_mksubvol(&file->f_path, name, namelen, 1426 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1410 NULL, transid, readonly); 1427 NULL, transid, readonly, inherit);
1411 } else { 1428 } else {
1412 struct inode *src_inode; 1429 struct inode *src_inode;
1413 src_file = fget(fd); 1430 src_file = fget(fd);
1414 if (!src_file) { 1431 if (!src_file) {
1415 ret = -EINVAL; 1432 ret = -EINVAL;
1416 goto out; 1433 goto out_drop_write;
1417 } 1434 }
1418 1435
1419 src_inode = src_file->f_path.dentry->d_inode; 1436 src_inode = src_file->f_path.dentry->d_inode;
@@ -1422,13 +1439,15 @@ static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1422 "another FS\n"); 1439 "another FS\n");
1423 ret = -EINVAL; 1440 ret = -EINVAL;
1424 fput(src_file); 1441 fput(src_file);
1425 goto out; 1442 goto out_drop_write;
1426 } 1443 }
1427 ret = btrfs_mksubvol(&file->f_path, name, namelen, 1444 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1428 BTRFS_I(src_inode)->root, 1445 BTRFS_I(src_inode)->root,
1429 transid, readonly); 1446 transid, readonly, inherit);
1430 fput(src_file); 1447 fput(src_file);
1431 } 1448 }
1449out_drop_write:
1450 mnt_drop_write_file(file);
1432out: 1451out:
1433 return ret; 1452 return ret;
1434} 1453}
@@ -1446,7 +1465,7 @@ static noinline int btrfs_ioctl_snap_create(struct file *file,
1446 1465
1447 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1466 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1448 vol_args->fd, subvol, 1467 vol_args->fd, subvol,
1449 NULL, false); 1468 NULL, false, NULL);
1450 1469
1451 kfree(vol_args); 1470 kfree(vol_args);
1452 return ret; 1471 return ret;
@@ -1460,6 +1479,7 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1460 u64 transid = 0; 1479 u64 transid = 0;
1461 u64 *ptr = NULL; 1480 u64 *ptr = NULL;
1462 bool readonly = false; 1481 bool readonly = false;
1482 struct btrfs_qgroup_inherit *inherit = NULL;
1463 1483
1464 vol_args = memdup_user(arg, sizeof(*vol_args)); 1484 vol_args = memdup_user(arg, sizeof(*vol_args));
1465 if (IS_ERR(vol_args)) 1485 if (IS_ERR(vol_args))
@@ -1467,7 +1487,8 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1467 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1487 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1468 1488
1469 if (vol_args->flags & 1489 if (vol_args->flags &
1470 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { 1490 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1491 BTRFS_SUBVOL_QGROUP_INHERIT)) {
1471 ret = -EOPNOTSUPP; 1492 ret = -EOPNOTSUPP;
1472 goto out; 1493 goto out;
1473 } 1494 }
@@ -1476,10 +1497,21 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1476 ptr = &transid; 1497 ptr = &transid;
1477 if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1498 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1478 readonly = true; 1499 readonly = true;
1500 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1501 if (vol_args->size > PAGE_CACHE_SIZE) {
1502 ret = -EINVAL;
1503 goto out;
1504 }
1505 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1506 if (IS_ERR(inherit)) {
1507 ret = PTR_ERR(inherit);
1508 goto out;
1509 }
1510 }
1479 1511
1480 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1512 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1481 vol_args->fd, subvol, 1513 vol_args->fd, subvol, ptr,
1482 ptr, readonly); 1514 readonly, &inherit);
1483 1515
1484 if (ret == 0 && ptr && 1516 if (ret == 0 && ptr &&
1485 copy_to_user(arg + 1517 copy_to_user(arg +
@@ -1488,6 +1520,7 @@ static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1488 ret = -EFAULT; 1520 ret = -EFAULT;
1489out: 1521out:
1490 kfree(vol_args); 1522 kfree(vol_args);
1523 kfree(inherit);
1491 return ret; 1524 return ret;
1492} 1525}
1493 1526
@@ -1523,29 +1556,40 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1523 u64 flags; 1556 u64 flags;
1524 int ret = 0; 1557 int ret = 0;
1525 1558
1526 if (root->fs_info->sb->s_flags & MS_RDONLY) 1559 ret = mnt_want_write_file(file);
1527 return -EROFS; 1560 if (ret)
1561 goto out;
1528 1562
1529 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 1563 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1530 return -EINVAL; 1564 ret = -EINVAL;
1565 goto out_drop_write;
1566 }
1531 1567
1532 if (copy_from_user(&flags, arg, sizeof(flags))) 1568 if (copy_from_user(&flags, arg, sizeof(flags))) {
1533 return -EFAULT; 1569 ret = -EFAULT;
1570 goto out_drop_write;
1571 }
1534 1572
1535 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) 1573 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1536 return -EINVAL; 1574 ret = -EINVAL;
1575 goto out_drop_write;
1576 }
1537 1577
1538 if (flags & ~BTRFS_SUBVOL_RDONLY) 1578 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1539 return -EOPNOTSUPP; 1579 ret = -EOPNOTSUPP;
1580 goto out_drop_write;
1581 }
1540 1582
1541 if (!inode_owner_or_capable(inode)) 1583 if (!inode_owner_or_capable(inode)) {
1542 return -EACCES; 1584 ret = -EACCES;
1585 goto out_drop_write;
1586 }
1543 1587
1544 down_write(&root->fs_info->subvol_sem); 1588 down_write(&root->fs_info->subvol_sem);
1545 1589
1546 /* nothing to do */ 1590 /* nothing to do */
1547 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1591 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1548 goto out; 1592 goto out_drop_sem;
1549 1593
1550 root_flags = btrfs_root_flags(&root->root_item); 1594 root_flags = btrfs_root_flags(&root->root_item);
1551 if (flags & BTRFS_SUBVOL_RDONLY) 1595 if (flags & BTRFS_SUBVOL_RDONLY)
@@ -1568,8 +1612,11 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1568out_reset: 1612out_reset:
1569 if (ret) 1613 if (ret)
1570 btrfs_set_root_flags(&root->root_item, root_flags); 1614 btrfs_set_root_flags(&root->root_item, root_flags);
1571out: 1615out_drop_sem:
1572 up_write(&root->fs_info->subvol_sem); 1616 up_write(&root->fs_info->subvol_sem);
1617out_drop_write:
1618 mnt_drop_write_file(file);
1619out:
1573 return ret; 1620 return ret;
1574} 1621}
1575 1622
@@ -2340,6 +2387,10 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2340 goto out_drop_write; 2387 goto out_drop_write;
2341 } 2388 }
2342 2389
2390 ret = -EXDEV;
2391 if (src_file->f_path.mnt != file->f_path.mnt)
2392 goto out_fput;
2393
2343 src = src_file->f_dentry->d_inode; 2394 src = src_file->f_dentry->d_inode;
2344 2395
2345 ret = -EINVAL; 2396 ret = -EINVAL;
@@ -2360,7 +2411,7 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2360 goto out_fput; 2411 goto out_fput;
2361 2412
2362 ret = -EXDEV; 2413 ret = -EXDEV;
2363 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) 2414 if (src->i_sb != inode->i_sb)
2364 goto out_fput; 2415 goto out_fput;
2365 2416
2366 ret = -ENOMEM; 2417 ret = -ENOMEM;
@@ -2434,13 +2485,14 @@ static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2434 * note the key will change type as we walk through the 2485 * note the key will change type as we walk through the
2435 * tree. 2486 * tree.
2436 */ 2487 */
2437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2488 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2489 0, 0);
2438 if (ret < 0) 2490 if (ret < 0)
2439 goto out; 2491 goto out;
2440 2492
2441 nritems = btrfs_header_nritems(path->nodes[0]); 2493 nritems = btrfs_header_nritems(path->nodes[0]);
2442 if (path->slots[0] >= nritems) { 2494 if (path->slots[0] >= nritems) {
2443 ret = btrfs_next_leaf(root, path); 2495 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
2444 if (ret < 0) 2496 if (ret < 0)
2445 goto out; 2497 goto out;
2446 if (ret > 0) 2498 if (ret > 0)
@@ -2749,8 +2801,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2749 struct btrfs_path *path; 2801 struct btrfs_path *path;
2750 struct btrfs_key location; 2802 struct btrfs_key location;
2751 struct btrfs_disk_key disk_key; 2803 struct btrfs_disk_key disk_key;
2752 struct btrfs_super_block *disk_super;
2753 u64 features;
2754 u64 objectid = 0; 2804 u64 objectid = 0;
2755 u64 dir_id; 2805 u64 dir_id;
2756 2806
@@ -2801,12 +2851,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2801 btrfs_mark_buffer_dirty(path->nodes[0]); 2851 btrfs_mark_buffer_dirty(path->nodes[0]);
2802 btrfs_free_path(path); 2852 btrfs_free_path(path);
2803 2853
2804 disk_super = root->fs_info->super_copy; 2854 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
2805 features = btrfs_super_incompat_flags(disk_super);
2806 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2807 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2808 btrfs_set_super_incompat_flags(disk_super, features);
2809 }
2810 btrfs_end_transaction(trans, root); 2855 btrfs_end_transaction(trans, root);
2811 2856
2812 return 0; 2857 return 0;
@@ -3063,19 +3108,21 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3063} 3108}
3064 3109
3065static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 3110static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3066 void __user *arg, int reset_after_read) 3111 void __user *arg)
3067{ 3112{
3068 struct btrfs_ioctl_get_dev_stats *sa; 3113 struct btrfs_ioctl_get_dev_stats *sa;
3069 int ret; 3114 int ret;
3070 3115
3071 if (reset_after_read && !capable(CAP_SYS_ADMIN))
3072 return -EPERM;
3073
3074 sa = memdup_user(arg, sizeof(*sa)); 3116 sa = memdup_user(arg, sizeof(*sa));
3075 if (IS_ERR(sa)) 3117 if (IS_ERR(sa))
3076 return PTR_ERR(sa); 3118 return PTR_ERR(sa);
3077 3119
3078 ret = btrfs_get_dev_stats(root, sa, reset_after_read); 3120 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3121 kfree(sa);
3122 return -EPERM;
3123 }
3124
3125 ret = btrfs_get_dev_stats(root, sa);
3079 3126
3080 if (copy_to_user(arg, sa, sizeof(*sa))) 3127 if (copy_to_user(arg, sa, sizeof(*sa)))
3081 ret = -EFAULT; 3128 ret = -EFAULT;
@@ -3265,10 +3312,7 @@ static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3265 if (!capable(CAP_SYS_ADMIN)) 3312 if (!capable(CAP_SYS_ADMIN))
3266 return -EPERM; 3313 return -EPERM;
3267 3314
3268 if (fs_info->sb->s_flags & MS_RDONLY) 3315 ret = mnt_want_write_file(file);
3269 return -EROFS;
3270
3271 ret = mnt_want_write(file->f_path.mnt);
3272 if (ret) 3316 if (ret)
3273 return ret; 3317 return ret;
3274 3318
@@ -3338,7 +3382,7 @@ out_bargs:
3338out: 3382out:
3339 mutex_unlock(&fs_info->balance_mutex); 3383 mutex_unlock(&fs_info->balance_mutex);
3340 mutex_unlock(&fs_info->volume_mutex); 3384 mutex_unlock(&fs_info->volume_mutex);
3341 mnt_drop_write(file->f_path.mnt); 3385 mnt_drop_write_file(file);
3342 return ret; 3386 return ret;
3343} 3387}
3344 3388
@@ -3390,6 +3434,264 @@ out:
3390 return ret; 3434 return ret;
3391} 3435}
3392 3436
3437static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3438{
3439 struct btrfs_ioctl_quota_ctl_args *sa;
3440 struct btrfs_trans_handle *trans = NULL;
3441 int ret;
3442 int err;
3443
3444 if (!capable(CAP_SYS_ADMIN))
3445 return -EPERM;
3446
3447 if (root->fs_info->sb->s_flags & MS_RDONLY)
3448 return -EROFS;
3449
3450 sa = memdup_user(arg, sizeof(*sa));
3451 if (IS_ERR(sa))
3452 return PTR_ERR(sa);
3453
3454 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3455 trans = btrfs_start_transaction(root, 2);
3456 if (IS_ERR(trans)) {
3457 ret = PTR_ERR(trans);
3458 goto out;
3459 }
3460 }
3461
3462 switch (sa->cmd) {
3463 case BTRFS_QUOTA_CTL_ENABLE:
3464 ret = btrfs_quota_enable(trans, root->fs_info);
3465 break;
3466 case BTRFS_QUOTA_CTL_DISABLE:
3467 ret = btrfs_quota_disable(trans, root->fs_info);
3468 break;
3469 case BTRFS_QUOTA_CTL_RESCAN:
3470 ret = btrfs_quota_rescan(root->fs_info);
3471 break;
3472 default:
3473 ret = -EINVAL;
3474 break;
3475 }
3476
3477 if (copy_to_user(arg, sa, sizeof(*sa)))
3478 ret = -EFAULT;
3479
3480 if (trans) {
3481 err = btrfs_commit_transaction(trans, root);
3482 if (err && !ret)
3483 ret = err;
3484 }
3485
3486out:
3487 kfree(sa);
3488 return ret;
3489}
3490
3491static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3492{
3493 struct btrfs_ioctl_qgroup_assign_args *sa;
3494 struct btrfs_trans_handle *trans;
3495 int ret;
3496 int err;
3497
3498 if (!capable(CAP_SYS_ADMIN))
3499 return -EPERM;
3500
3501 if (root->fs_info->sb->s_flags & MS_RDONLY)
3502 return -EROFS;
3503
3504 sa = memdup_user(arg, sizeof(*sa));
3505 if (IS_ERR(sa))
3506 return PTR_ERR(sa);
3507
3508 trans = btrfs_join_transaction(root);
3509 if (IS_ERR(trans)) {
3510 ret = PTR_ERR(trans);
3511 goto out;
3512 }
3513
3514 /* FIXME: check if the IDs really exist */
3515 if (sa->assign) {
3516 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3517 sa->src, sa->dst);
3518 } else {
3519 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3520 sa->src, sa->dst);
3521 }
3522
3523 err = btrfs_end_transaction(trans, root);
3524 if (err && !ret)
3525 ret = err;
3526
3527out:
3528 kfree(sa);
3529 return ret;
3530}
3531
3532static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3533{
3534 struct btrfs_ioctl_qgroup_create_args *sa;
3535 struct btrfs_trans_handle *trans;
3536 int ret;
3537 int err;
3538
3539 if (!capable(CAP_SYS_ADMIN))
3540 return -EPERM;
3541
3542 if (root->fs_info->sb->s_flags & MS_RDONLY)
3543 return -EROFS;
3544
3545 sa = memdup_user(arg, sizeof(*sa));
3546 if (IS_ERR(sa))
3547 return PTR_ERR(sa);
3548
3549 trans = btrfs_join_transaction(root);
3550 if (IS_ERR(trans)) {
3551 ret = PTR_ERR(trans);
3552 goto out;
3553 }
3554
3555 /* FIXME: check if the IDs really exist */
3556 if (sa->create) {
3557 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3558 NULL);
3559 } else {
3560 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3561 }
3562
3563 err = btrfs_end_transaction(trans, root);
3564 if (err && !ret)
3565 ret = err;
3566
3567out:
3568 kfree(sa);
3569 return ret;
3570}
3571
3572static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3573{
3574 struct btrfs_ioctl_qgroup_limit_args *sa;
3575 struct btrfs_trans_handle *trans;
3576 int ret;
3577 int err;
3578 u64 qgroupid;
3579
3580 if (!capable(CAP_SYS_ADMIN))
3581 return -EPERM;
3582
3583 if (root->fs_info->sb->s_flags & MS_RDONLY)
3584 return -EROFS;
3585
3586 sa = memdup_user(arg, sizeof(*sa));
3587 if (IS_ERR(sa))
3588 return PTR_ERR(sa);
3589
3590 trans = btrfs_join_transaction(root);
3591 if (IS_ERR(trans)) {
3592 ret = PTR_ERR(trans);
3593 goto out;
3594 }
3595
3596 qgroupid = sa->qgroupid;
3597 if (!qgroupid) {
3598 /* take the current subvol as qgroup */
3599 qgroupid = root->root_key.objectid;
3600 }
3601
3602 /* FIXME: check if the IDs really exist */
3603 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3604
3605 err = btrfs_end_transaction(trans, root);
3606 if (err && !ret)
3607 ret = err;
3608
3609out:
3610 kfree(sa);
3611 return ret;
3612}
3613
3614static long btrfs_ioctl_set_received_subvol(struct file *file,
3615 void __user *arg)
3616{
3617 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3618 struct inode *inode = fdentry(file)->d_inode;
3619 struct btrfs_root *root = BTRFS_I(inode)->root;
3620 struct btrfs_root_item *root_item = &root->root_item;
3621 struct btrfs_trans_handle *trans;
3622 struct timespec ct = CURRENT_TIME;
3623 int ret = 0;
3624
3625 ret = mnt_want_write_file(file);
3626 if (ret < 0)
3627 return ret;
3628
3629 down_write(&root->fs_info->subvol_sem);
3630
3631 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3632 ret = -EINVAL;
3633 goto out;
3634 }
3635
3636 if (btrfs_root_readonly(root)) {
3637 ret = -EROFS;
3638 goto out;
3639 }
3640
3641 if (!inode_owner_or_capable(inode)) {
3642 ret = -EACCES;
3643 goto out;
3644 }
3645
3646 sa = memdup_user(arg, sizeof(*sa));
3647 if (IS_ERR(sa)) {
3648 ret = PTR_ERR(sa);
3649 sa = NULL;
3650 goto out;
3651 }
3652
3653 trans = btrfs_start_transaction(root, 1);
3654 if (IS_ERR(trans)) {
3655 ret = PTR_ERR(trans);
3656 trans = NULL;
3657 goto out;
3658 }
3659
3660 sa->rtransid = trans->transid;
3661 sa->rtime.sec = ct.tv_sec;
3662 sa->rtime.nsec = ct.tv_nsec;
3663
3664 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3665 btrfs_set_root_stransid(root_item, sa->stransid);
3666 btrfs_set_root_rtransid(root_item, sa->rtransid);
3667 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3668 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3669 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3670 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3671
3672 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3673 &root->root_key, &root->root_item);
3674 if (ret < 0) {
3675 btrfs_end_transaction(trans, root);
3676 trans = NULL;
3677 goto out;
3678 } else {
3679 ret = btrfs_commit_transaction(trans, root);
3680 if (ret < 0)
3681 goto out;
3682 }
3683
3684 ret = copy_to_user(arg, sa, sizeof(*sa));
3685 if (ret)
3686 ret = -EFAULT;
3687
3688out:
3689 kfree(sa);
3690 up_write(&root->fs_info->subvol_sem);
3691 mnt_drop_write_file(file);
3692 return ret;
3693}
3694
3393long btrfs_ioctl(struct file *file, unsigned int 3695long btrfs_ioctl(struct file *file, unsigned int
3394 cmd, unsigned long arg) 3696 cmd, unsigned long arg)
3395{ 3697{
@@ -3411,6 +3713,8 @@ long btrfs_ioctl(struct file *file, unsigned int
3411 return btrfs_ioctl_snap_create_v2(file, argp, 0); 3713 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3412 case BTRFS_IOC_SUBVOL_CREATE: 3714 case BTRFS_IOC_SUBVOL_CREATE:
3413 return btrfs_ioctl_snap_create(file, argp, 1); 3715 return btrfs_ioctl_snap_create(file, argp, 1);
3716 case BTRFS_IOC_SUBVOL_CREATE_V2:
3717 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3414 case BTRFS_IOC_SNAP_DESTROY: 3718 case BTRFS_IOC_SNAP_DESTROY:
3415 return btrfs_ioctl_snap_destroy(file, argp); 3719 return btrfs_ioctl_snap_destroy(file, argp);
3416 case BTRFS_IOC_SUBVOL_GETFLAGS: 3720 case BTRFS_IOC_SUBVOL_GETFLAGS:
@@ -3472,10 +3776,20 @@ long btrfs_ioctl(struct file *file, unsigned int
3472 return btrfs_ioctl_balance_ctl(root, arg); 3776 return btrfs_ioctl_balance_ctl(root, arg);
3473 case BTRFS_IOC_BALANCE_PROGRESS: 3777 case BTRFS_IOC_BALANCE_PROGRESS:
3474 return btrfs_ioctl_balance_progress(root, argp); 3778 return btrfs_ioctl_balance_progress(root, argp);
3779 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3780 return btrfs_ioctl_set_received_subvol(file, argp);
3781 case BTRFS_IOC_SEND:
3782 return btrfs_ioctl_send(file, argp);
3475 case BTRFS_IOC_GET_DEV_STATS: 3783 case BTRFS_IOC_GET_DEV_STATS:
3476 return btrfs_ioctl_get_dev_stats(root, argp, 0); 3784 return btrfs_ioctl_get_dev_stats(root, argp);
3477 case BTRFS_IOC_GET_AND_RESET_DEV_STATS: 3785 case BTRFS_IOC_QUOTA_CTL:
3478 return btrfs_ioctl_get_dev_stats(root, argp, 1); 3786 return btrfs_ioctl_quota_ctl(root, argp);
3787 case BTRFS_IOC_QGROUP_ASSIGN:
3788 return btrfs_ioctl_qgroup_assign(root, argp);
3789 case BTRFS_IOC_QGROUP_CREATE:
3790 return btrfs_ioctl_qgroup_create(root, argp);
3791 case BTRFS_IOC_QGROUP_LIMIT:
3792 return btrfs_ioctl_qgroup_limit(root, argp);
3479 } 3793 }
3480 3794
3481 return -ENOTTY; 3795 return -ENOTTY;