19 #include <linux/kernel.h>
27 #include <linux/time.h>
29 #include <linux/string.h>
40 #include <linux/xattr.h>
42 #include <linux/slab.h>
44 #include <linux/uuid.h>
73 static unsigned int btrfs_flags_to_ioctl(
unsigned int flags)
75 unsigned int iflags = 0;
94 else if (flags & BTRFS_INODE_NOCOMPRESS)
109 if (ip->
flags & BTRFS_INODE_SYNC)
111 if (ip->
flags & BTRFS_INODE_IMMUTABLE)
113 if (ip->
flags & BTRFS_INODE_APPEND)
115 if (ip->
flags & BTRFS_INODE_NOATIME)
117 if (ip->
flags & BTRFS_INODE_DIRSYNC)
133 flags = BTRFS_I(dir)->flags;
135 if (flags & BTRFS_INODE_NOCOMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 }
else if (flags & BTRFS_INODE_COMPRESS) {
139 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
143 if (flags & BTRFS_INODE_NODATACOW)
149 static int btrfs_ioctl_getflags(
struct file *
file,
void __user *
arg)
152 unsigned int flags = btrfs_flags_to_ioctl(ip->
flags);
159 static int check_flags(
unsigned int flags)
174 static int btrfs_ioctl_setflags(
struct file *file,
void __user *arg)
180 unsigned int flags, oldflags;
183 unsigned int i_oldflags;
186 if (btrfs_root_readonly(root))
192 ret = check_flags(flags);
205 ip_oldflags = ip->
flags;
209 flags = btrfs_mask_flags(inode->
i_mode, flags);
210 oldflags = btrfs_flags_to_ioctl(ip->
flags);
221 ip->
flags &= ~BTRFS_INODE_SYNC;
225 ip->
flags &= ~BTRFS_INODE_IMMUTABLE;
229 ip->
flags &= ~BTRFS_INODE_APPEND;
233 ip->
flags &= ~BTRFS_INODE_NODUMP;
237 ip->
flags &= ~BTRFS_INODE_NOATIME;
241 ip->
flags &= ~BTRFS_INODE_DIRSYNC;
250 ip->
flags |= BTRFS_INODE_NODATACOW
261 ip->
flags &= ~(BTRFS_INODE_NODATACOW
264 ip->
flags &= ~BTRFS_INODE_NODATACOW;
273 if (flags & FS_NOCOMP_FL) {
274 ip->
flags &= ~BTRFS_INODE_COMPRESS;
278 ip->
flags &= ~BTRFS_INODE_NOCOMPRESS;
285 ret = PTR_ERR(trans);
290 inode_inc_iversion(inode);
297 ip->
flags = ip_oldflags;
307 static int btrfs_ioctl_getversion(
struct file *file,
int __user *arg)
309 struct inode *inode = file->
f_path.dentry->d_inode;
314 static noinline int btrfs_ioctl_fitrim(
struct file *file,
void __user *arg)
329 list_for_each_entry_rcu(device, &fs_info->
fs_devices->devices,
333 q = bdev_get_queue(device->
bdev);
334 if (blk_queue_discard(q)) {
336 minlen =
min((
u64)q->limits.discard_granularity,
347 range.len < fs_info->
sb->s_blocksize)
398 return PTR_ERR(trans);
401 inherit ? *inherit :
NULL);
406 0, objectid,
NULL, 0, 0, 0);
413 btrfs_set_header_bytenr(leaf, leaf->
start);
414 btrfs_set_header_generation(leaf, trans->
transid);
416 btrfs_set_header_owner(leaf, objectid);
419 (
unsigned long)btrfs_header_fsid(leaf),
422 (
unsigned long)btrfs_header_chunk_tree_uuid(leaf),
426 memset(&root_item, 0,
sizeof(root_item));
428 inode_item = &root_item.inode;
436 root_item.byte_limit = 0;
439 btrfs_set_root_bytenr(&root_item, leaf->
start);
440 btrfs_set_root_generation(&root_item, trans->
transid);
441 btrfs_set_root_level(&root_item, 0);
442 btrfs_set_root_refs(&root_item, 1);
443 btrfs_set_root_used(&root_item, leaf->
len);
444 btrfs_set_root_last_snapshot(&root_item, 0);
446 btrfs_set_root_generation_v2(&root_item,
447 btrfs_root_generation(&root_item));
452 root_item.ctime = root_item.otime;
453 btrfs_set_root_ctransid(&root_item, trans->
transid);
454 btrfs_set_root_otransid(&root_item, trans->
transid);
460 btrfs_set_root_dirid(&root_item, new_dirid);
472 if (IS_ERR(new_root)) {
474 ret = PTR_ERR(new_root);
497 name, namelen, dir, &
key,
504 btrfs_i_size_write(dir, dir->
i_size + namelen * 2);
510 btrfs_ino(dir), index, name, namelen);
517 *async_transid = trans->
transid;
527 static int create_snapshot(
struct btrfs_root *root,
struct dentry *dentry,
528 char *name,
int namelen,
u64 *async_transid,
539 pending_snapshot = kzalloc(
sizeof(*pending_snapshot),
GFP_NOFS);
540 if (!pending_snapshot)
545 pending_snapshot->
dentry = dentry;
549 pending_snapshot->
inherit = *inherit;
555 ret = PTR_ERR(trans);
562 spin_lock(&root->
fs_info->trans_lock);
563 list_add(&pending_snapshot->
list,
565 spin_unlock(&root->
fs_info->trans_lock);
567 *async_transid = trans->
transid;
569 root->
fs_info->extent_root, 1);
577 ret = pending_snapshot->
error;
587 ret = PTR_ERR(inode);
594 kfree(pending_snapshot);
602 static inline int btrfs_check_sticky(
struct inode *dir,
struct inode *inode)
608 if (uid_eq(inode->
i_uid, fsuid))
610 if (uid_eq(dir->
i_uid, fsuid))
635 static int btrfs_may_delete(
struct inode *dir,
struct dentry *victim,
int isdir)
650 if (btrfs_check_sticky(dir, victim->
d_inode)||
669 static inline int btrfs_may_create(
struct inode *dir,
struct dentry *
child)
684 char *name,
int namelen,
686 u64 *async_transid,
bool readonly,
689 struct inode *dir = parent->
dentry->d_inode;
690 struct dentry *dentry;
696 error = PTR_ERR(dentry);
704 error = btrfs_may_create(dir, dentry);
710 if (btrfs_root_refs(&BTRFS_I(dir)->root->
root_item) == 0)
714 error = create_snapshot(snap_src, dentry, name, namelen,
715 async_transid, readonly, inherit);
717 error = create_subvol(BTRFS_I(dir)->root, dentry,
718 name, namelen, async_transid, inherit);
721 fsnotify_mkdir(dir, dentry);
738 static int check_defrag_in_cache(
struct inode *inode,
u64 offset,
int thresh)
750 end = extent_map_end(em);
752 if (end - offset > thresh)
771 static int find_new_extents(
struct btrfs_root *root,
772 struct inode *inode,
u64 newer_than,
773 u64 *off,
int thresh)
782 u64 ino = btrfs_ino(inode);
788 min_key.objectid =
ino;
790 min_key.offset = *off;
792 max_key.objectid =
ino;
793 max_key.type = (
u8)-1;
794 max_key.offset = (
u64)-1;
800 path, 0, newer_than);
803 if (min_key.objectid != ino)
808 leaf = path->
nodes[0];
812 type = btrfs_file_extent_type(leaf, extent);
814 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
815 check_defrag_in_cache(inode, min_key.offset, thresh)) {
816 *off = min_key.offset;
821 if (min_key.offset == (
u64)-1)
860 static bool defrag_check_next_extent(
struct inode *inode,
struct extent_map *em)
866 if (em->
start + em->
len >= i_size_read(inode))
869 next = defrag_lookup_extent(inode, em->
start + em->
len);
877 static int should_defrag_range(
struct inode *inode,
u64 start,
int thresh,
883 bool next_mergeable =
true;
889 if (start < *defrag_end)
894 em = defrag_lookup_extent(inode, start);
904 next_mergeable = defrag_check_next_extent(inode, em);
910 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
911 (em->
len >= thresh || !next_mergeable))
923 *defrag_end = extent_map_end(em);
926 *skip = extent_map_end(em);
946 static int cluster_pages_for_defrag(
struct inode *inode,
948 unsigned long start_index,
951 unsigned long file_end;
952 u64 isize = i_size_read(inode);
965 if (!isize || start_index > file_end)
968 page_cnt =
min_t(
u64, (
u64)num_pages, (
u64)file_end - start_index + 1);
975 tree = &BTRFS_I(inode)->io_tree;
978 for (i = 0; i < page_cnt; i++) {
982 start_index + i, mask);
1011 if (!PageUptodate(page)) {
1014 if (!PageUptodate(page)) {
1041 for (i = 0; i < i_done; i++)
1042 wait_on_page_writeback(pages[i]);
1048 page_start, page_end - 1, 0, &cached_state);
1054 if (i_done != page_cnt) {
1055 spin_lock(&BTRFS_I(inode)->lock);
1056 BTRFS_I(inode)->outstanding_extents++;
1057 spin_unlock(&BTRFS_I(inode)->lock);
1067 page_start, page_end - 1, &cached_state,
1070 for (i = 0; i < i_done; i++) {
1072 ClearPageChecked(pages[i]);
1080 for (i = 0; i < i_done; i++) {
1091 u64 newer_than,
unsigned long max_to_defrag)
1093 struct btrfs_root *root = BTRFS_I(inode)->root;
1095 unsigned long last_index;
1096 u64 isize = i_size_read(inode);
1102 unsigned long ra_index = 0;
1104 int defrag_count = 0;
1109 u64 new_align = ~((
u64)128 * 1024 - 1);
1110 struct page **pages =
NULL;
1112 if (extent_thresh == 0)
1113 extent_thresh = 256 * 1024;
1130 ra = kzalloc(
sizeof(*ra),
GFP_NOFS);
1138 pages =
kmalloc(
sizeof(
struct page *) * max_cluster,
1154 ret = find_new_extents(root, inode, newer_than,
1155 &newer_off, 64 * 1024);
1157 range->
start = newer_off;
1169 max_to_defrag = last_index + 1;
1175 if (i < inode->i_mapping->writeback_index)
1178 while (i <= last_index && defrag_count < max_to_defrag &&
1179 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1189 extent_thresh, &last_len, &skip,
1190 &defrag_end, range->
flags &
1198 i =
max(i + 1, next);
1205 cluster =
min(cluster, max_cluster);
1207 cluster = max_cluster;
1211 BTRFS_I(inode)->force_compress = compress_type;
1213 if (i + cluster > ra_index) {
1214 ra_index =
max(i, ra_index);
1215 btrfs_force_ra(inode->
i_mapping, ra, file, ra_index,
1217 ra_index += max_cluster;
1221 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1227 defrag_count +=
ret;
1232 if (newer_off == (
u64)-1)
1238 newer_off =
max(newer_off + 1,
1241 ret = find_new_extents(root, inode,
1242 newer_than, &newer_off,
1245 range->
start = newer_off;
1306 char *devstr =
NULL;
1317 if (root->
fs_info->balance_ctl) {
1324 if (IS_ERR(vol_args)) {
1325 ret = PTR_ERR(vol_args);
1331 sizestr = vol_args->
name;
1332 devstr =
strchr(sizestr,
':');
1335 sizestr = devstr + 1;
1337 devstr = vol_args->
name;
1340 (
unsigned long long)devid);
1345 (
unsigned long long)devid);
1351 "seeding device %llu\n",
1352 (
unsigned long long)devid);
1357 if (!
strcmp(sizestr,
"max"))
1358 new_size = device->
bdev->bd_inode->i_size;
1360 if (sizestr[0] ==
'-') {
1363 }
else if (sizestr[0] ==
'+') {
1368 if (new_size == 0) {
1377 if (new_size > old_size) {
1381 new_size = old_size - new_size;
1382 }
else if (mod > 0) {
1383 new_size = old_size + new_size;
1386 if (new_size < 256 * 1024 * 1024) {
1390 if (new_size > device->
bdev->bd_inode->i_size) {
1400 (
unsigned long long)new_size);
1402 if (new_size > old_size) {
1404 if (IS_ERR(trans)) {
1405 ret = PTR_ERR(trans);
1410 }
else if (new_size < old_size) {
1421 static noinline int btrfs_ioctl_snap_create_transid(
struct file *file,
1422 char *name,
unsigned long fd,
int subvol,
1436 goto out_drop_write;
1439 if (name[0] ==
'.' &&
1440 (namelen == 1 || (name[1] ==
'.' && namelen == 2))) {
1442 goto out_drop_write;
1446 ret = btrfs_mksubvol(&file->
f_path, name, namelen,
1447 NULL, transid, readonly, inherit);
1449 struct fd
src = fdget(fd);
1450 struct inode *src_inode;
1453 goto out_drop_write;
1456 src_inode = src.
file->f_path.dentry->d_inode;
1457 if (src_inode->
i_sb != file->
f_path.dentry->d_inode->i_sb) {
1462 ret = btrfs_mksubvol(&file->
f_path, name, namelen,
1463 BTRFS_I(src_inode)->root,
1464 transid, readonly, inherit);
1474 static noinline int btrfs_ioctl_snap_create(
struct file *file,
1475 void __user *arg,
int subvol)
1481 if (IS_ERR(vol_args))
1482 return PTR_ERR(vol_args);
1485 ret = btrfs_ioctl_snap_create_transid(file, vol_args->
name,
1486 vol_args->
fd, subvol,
1493 static noinline int btrfs_ioctl_snap_create_v2(
struct file *file,
1494 void __user *arg,
int subvol)
1500 bool readonly =
false;
1504 if (IS_ERR(vol_args))
1505 return PTR_ERR(vol_args);
1508 if (vol_args->
flags &
1520 if (vol_args->
size > PAGE_CACHE_SIZE) {
1525 if (IS_ERR(inherit)) {
1526 ret = PTR_ERR(inherit);
1531 ret = btrfs_ioctl_snap_create_transid(file, vol_args->
name,
1532 vol_args->
fd, subvol, ptr,
1533 readonly, &inherit);
1535 if (ret == 0 && ptr &&
1538 transid), ptr,
sizeof(*ptr)))
1546 static noinline int btrfs_ioctl_subvol_getflags(
struct file *file,
1549 struct inode *inode = fdentry(file)->d_inode;
1550 struct btrfs_root *root = BTRFS_I(inode)->root;
1558 if (btrfs_root_readonly(root))
1568 static noinline int btrfs_ioctl_subvol_setflags(
struct file *file,
1571 struct inode *inode = fdentry(file)->d_inode;
1572 struct btrfs_root *root = BTRFS_I(inode)->root;
1584 goto out_drop_write;
1589 goto out_drop_write;
1594 goto out_drop_write;
1599 goto out_drop_write;
1604 goto out_drop_write;
1613 root_flags = btrfs_root_flags(&root->
root_item);
1614 if (flags & BTRFS_SUBVOL_RDONLY)
1622 if (IS_ERR(trans)) {
1623 ret = PTR_ERR(trans);
1633 btrfs_set_root_flags(&root->
root_item, root_flags);
1666 if (path->
slots[0] > 0) {
1668 btrfs_item_key_to_cpu(path->
nodes[0], &
key, path->
slots[0]);
1707 unsigned long *sk_offset,
1713 unsigned long item_off;
1714 unsigned long item_len;
1720 leaf = path->
nodes[0];
1721 slot = path->
slots[0];
1722 nritems = btrfs_header_nritems(leaf);
1724 if (btrfs_header_generation(leaf) > sk->
max_transid) {
1728 found_transid = btrfs_header_generation(leaf);
1730 for (i = slot; i <
nritems; i++) {
1732 item_len = btrfs_item_size_nr(leaf, i);
1737 if (
sizeof(sh) + item_len + *sk_offset >
1743 btrfs_item_key_to_cpu(leaf, key, i);
1744 if (!key_in_sk(key, sk))
1749 sh.type = key->
type;
1751 sh.transid = found_transid;
1754 memcpy(buf + *sk_offset, &sh,
sizeof(sh));
1755 *sk_offset +=
sizeof(sh);
1758 char *
p = buf + *sk_offset;
1761 item_off, item_len);
1762 *sk_offset += item_len;
1786 static noinline int search_ioctl(
struct inode *inode,
1797 unsigned long sk_offset = 0;
1805 root = BTRFS_I(inode)->root;
1837 ret = copy_to_sk(root, path, &key, sk, args->
buf,
1838 &sk_offset, &num_found);
1840 if (ret || num_found >= sk->
nr_items)
1851 static noinline int btrfs_ioctl_tree_search(
struct file *file,
1855 struct inode *
inode;
1863 return PTR_ERR(args);
1865 inode = fdentry(file)->d_inode;
1866 ret = search_ioctl(inode, args);
1867 if (ret == 0 &&
copy_to_user(argp, args,
sizeof(*args)))
1922 slot = path->
slots[0];
1923 if (ret > 0 && slot > 0)
1925 btrfs_item_key_to_cpu(l, &key, slot);
1927 if (ret > 0 && (key.
objectid != dirid ||
1934 len = btrfs_inode_ref_name_len(l, iref);
1936 total_len += len + 1;
1953 memmove(name, ptr, total_len);
1961 static noinline int btrfs_ioctl_ino_lookup(
struct file *file,
1965 struct inode *
inode;
1973 return PTR_ERR(args);
1975 inode = fdentry(file)->d_inode;
1978 args->
treeid = BTRFS_I(inode)->root->root_key.objectid;
1980 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->
fs_info,
1984 if (ret == 0 &&
copy_to_user(argp, args,
sizeof(*args)))
1991 static noinline int btrfs_ioctl_snap_destroy(
struct file *file,
1994 struct dentry *parent = fdentry(file);
1995 struct dentry *dentry;
1996 struct inode *dir = parent->
d_inode;
1997 struct inode *
inode;
1998 struct btrfs_root *root = BTRFS_I(dir)->root;
2007 if (IS_ERR(vol_args))
2008 return PTR_ERR(vol_args);
2024 if (IS_ERR(dentry)) {
2025 err = PTR_ERR(dentry);
2026 goto out_unlock_dir;
2035 dest = BTRFS_I(inode)->root;
2070 err = btrfs_may_delete(dir, dentry, 1);
2087 err = may_destroy_subvol(dest);
2092 if (IS_ERR(trans)) {
2093 err = PTR_ERR(trans);
2113 btrfs_set_root_refs(&dest->
root_item, 0);
2149 static int btrfs_ioctl_defrag(
struct file *file,
void __user *argp)
2151 struct inode *inode = fdentry(file)->d_inode;
2152 struct btrfs_root *root = BTRFS_I(inode)->root;
2156 if (btrfs_root_readonly(root))
2216 static long btrfs_ioctl_add_dev(
struct btrfs_root *root,
void __user *arg)
2225 if (root->
fs_info->balance_ctl) {
2232 if (IS_ERR(vol_args)) {
2233 ret = PTR_ERR(vol_args);
2246 static long btrfs_ioctl_rm_dev(
struct btrfs_root *root,
void __user *arg)
2258 if (root->
fs_info->balance_ctl) {
2265 if (IS_ERR(vol_args)) {
2266 ret = PTR_ERR(vol_args);
2279 static long btrfs_ioctl_fs_info(
struct btrfs_root *root,
void __user *arg)
2290 fi_args = kzalloc(
sizeof(*fi_args),
GFP_KERNEL);
2311 static long btrfs_ioctl_dev_info(
struct btrfs_root *root,
void __user *arg)
2324 if (IS_ERR(di_args))
2325 return PTR_ERR(di_args);
2328 s_uuid = di_args->
uuid;
2350 di_args->
path[
sizeof(di_args->
path) - 1] = 0;
2352 di_args->
path[0] =
'\0';
2356 if (ret == 0 &&
copy_to_user(arg, di_args,
sizeof(*di_args)))
2363 static noinline long btrfs_ioctl_clone(
struct file *file,
unsigned long srcfd,
2366 struct inode *inode = fdentry(file)->d_inode;
2367 struct btrfs_root *root = BTRFS_I(inode)->root;
2395 if (btrfs_root_readonly(root))
2402 src_file = fdget(srcfd);
2403 if (!src_file.file) {
2405 goto out_drop_write;
2409 if (src_file.file->f_path.mnt != file->
f_path.mnt)
2412 src = src_file.file->f_dentry->d_inode;
2424 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2436 buf =
vmalloc(btrfs_level_size(root, 0));
2457 if (off + len > src->
i_size || off + len < off)
2460 olen = len = src->
i_size - off;
2462 if (off + len == src->
i_size)
2470 if (destoff > inode->
i_size) {
2484 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2511 nritems = btrfs_header_nritems(path->
nodes[0]);
2512 if (path->
slots[0] >= nritems) {
2518 nritems = btrfs_header_nritems(path->
nodes[0]);
2520 leaf = path->
nodes[0];
2521 slot = path->
slots[0];
2523 btrfs_item_key_to_cpu(leaf, &key, slot);
2533 u64 disko = 0, diskl = 0;
2538 size = btrfs_item_size_nr(leaf, slot);
2545 comp = btrfs_file_extent_compression(leaf, extent);
2546 type = btrfs_file_extent_type(leaf, extent);
2549 disko = btrfs_file_extent_disk_bytenr(leaf,
2551 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2553 datao = btrfs_file_extent_offset(leaf, extent);
2554 datal = btrfs_file_extent_num_bytes(leaf,
2558 datal = btrfs_file_extent_ram_bytes(leaf,
2563 if (key.
offset + datal <= off ||
2564 key.
offset >= off + len - 1)
2567 memcpy(&new_key, &key,
sizeof(new_key));
2568 new_key.objectid = btrfs_ino(inode);
2570 new_key.offset = key.
offset + destoff - off;
2572 new_key.offset = destoff;
2580 if (IS_ERR(trans)) {
2581 ret = PTR_ERR(trans);
2593 if (key.
offset + datal > off + len)
2594 datal = off + len - key.
offset;
2598 datao += off - key.
offset;
2599 datal -= off - key.
offset;
2604 new_key.offset + datal,
2613 ret = btrfs_insert_empty_item(trans, root, path,
2622 leaf = path->
nodes[0];
2623 slot = path->
slots[0];
2635 btrfs_set_file_extent_offset(leaf, extent,
2637 btrfs_set_file_extent_num_bytes(leaf, extent,
2645 new_key.offset - datao,
2662 new_key.offset +=
skip;
2665 if (key.
offset + datal > off + len)
2666 trim = key.
offset + datal - (off + len);
2668 if (comp && (skip || trim)) {
2673 size -= skip + trim;
2674 datal -= skip + trim;
2678 new_key.offset + datal,
2687 ret = btrfs_insert_empty_item(trans, root, path,
2698 btrfs_file_extent_calc_inline_size(0);
2699 memmove(buf+start, buf+start+skip,
2703 leaf = path->
nodes[0];
2704 slot = path->
slots[0];
2714 inode_inc_iversion(inode);
2722 endoff = new_key.offset + datal;
2723 if (endoff > destoff+olen)
2724 endoff = destoff+olen;
2725 if (endoff > inode->
i_size)
2726 btrfs_i_size_write(inode, endoff);
2756 static long btrfs_ioctl_clone_range(
struct file *file,
void __user *argp)
2762 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2763 args.src_length, args.dest_offset);
2772 static long btrfs_ioctl_trans_start(
struct file *file)
2774 struct inode *inode = fdentry(file)->d_inode;
2775 struct btrfs_root *root = BTRFS_I(inode)->root;
2788 if (btrfs_root_readonly(root))
2812 static long btrfs_ioctl_default_subvol(
struct file *file,
void __user *argp)
2814 struct inode *inode = fdentry(file)->d_inode;
2815 struct btrfs_root *root = BTRFS_I(inode)->root;
2832 objectid = root->
root_key.objectid;
2839 if (IS_ERR(new_root))
2840 return PTR_ERR(new_root);
2842 if (btrfs_root_refs(&new_root->
root_item) == 0)
2851 if (IS_ERR(trans)) {
2853 return PTR_ERR(trans);
2856 dir_id = btrfs_super_root_dir(root->
fs_info->super_copy);
2858 dir_id,
"default", 7, 1);
2859 if (IS_ERR_OR_NULL(di)) {
2863 "this isn't going to work\n");
2867 btrfs_cpu_key_to_disk(&disk_key, &new_root->
root_key);
2868 btrfs_set_dir_item_key(path->
nodes[0], di, &disk_key);
2890 btrfs_block_group_used(&block_group->
item);
2914 sizeof(space_args)))
2917 for (i = 0; i < num_types; i++) {
2922 list_for_each_entry_rcu(tmp, &root->
fs_info->space_info,
2924 if (tmp->
flags == types[i]) {
2950 alloc_size =
sizeof(*dest) * slot_count;
2955 if (alloc_size > PAGE_CACHE_SIZE)
2965 for (i = 0; i < num_types; i++) {
2973 list_for_each_entry_rcu(tmp, &root->
fs_info->space_info,
2975 if (tmp->
flags == types[i]) {
2989 memcpy(dest, &space,
sizeof(space));
3008 if (ret == 0 &&
copy_to_user(arg, &space_args,
sizeof(space_args)))
3022 struct inode *inode = fdentry(file)->d_inode;
3023 struct btrfs_root *root = BTRFS_I(inode)->root;
3039 static noinline long btrfs_ioctl_start_sync(
struct file *file,
void __user *argp)
3041 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3048 return PTR_ERR(trans);
3062 static noinline long btrfs_ioctl_wait_sync(
struct file *file,
void __user *argp)
3064 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3076 static long btrfs_ioctl_scrub(
struct btrfs_root *root,
void __user *arg)
3098 static long btrfs_ioctl_scrub_cancel(
struct btrfs_root *root,
void __user *arg)
3106 static long btrfs_ioctl_scrub_progress(
struct btrfs_root *root,
3152 static long btrfs_ioctl_ino_to_path(
struct btrfs_root *root,
void __user *arg)
3180 if (IS_ERR(ipath)) {
3181 ret = PTR_ERR(ipath);
3190 for (i = 0; i < ipath->
fspath->elem_cnt; ++
i) {
3191 rel_ptr = ipath->
fspath->val[
i] -
3193 ipath->
fspath->val[
i] = rel_ptr;
3197 (
void *)(
unsigned long)ipath->
fspath, size);
3214 const size_t c = 3 *
sizeof(
u64);
3231 static long btrfs_ioctl_logical_to_ino(
struct btrfs_root *root,
3258 if (IS_ERR(inodes)) {
3259 ret = PTR_ERR(inodes);
3265 build_ino_list, inodes);
3272 (
void *)(
unsigned long)inodes, size);
3311 static long btrfs_ioctl_balance(
struct file *file,
void __user *arg)
3313 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3331 if (IS_ERR(bargs)) {
3332 ret = PTR_ERR(bargs);
3358 bctl = kzalloc(
sizeof(*bctl),
GFP_NOFS);
3396 static long btrfs_ioctl_balance_ctl(
struct btrfs_root *root,
int cmd)
3411 static long btrfs_ioctl_balance_progress(
struct btrfs_root *root,
3427 bargs = kzalloc(
sizeof(*bargs),
GFP_NOFS);
3444 static long btrfs_ioctl_quota_ctl(
struct btrfs_root *root,
void __user *arg)
3463 if (IS_ERR(trans)) {
3464 ret = PTR_ERR(trans);
3498 static long btrfs_ioctl_qgroup_assign(
struct btrfs_root *root,
void __user *arg)
3516 if (IS_ERR(trans)) {
3517 ret = PTR_ERR(trans);
3539 static long btrfs_ioctl_qgroup_create(
struct btrfs_root *root,
void __user *arg)
3557 if (IS_ERR(trans)) {
3558 ret = PTR_ERR(trans);
3579 static long btrfs_ioctl_qgroup_limit(
struct btrfs_root *root,
void __user *arg)
3598 if (IS_ERR(trans)) {
3599 ret = PTR_ERR(trans);
3606 qgroupid = root->
root_key.objectid;
3621 static long btrfs_ioctl_set_received_subvol(
struct file *file,
3625 struct inode *inode = fdentry(file)->d_inode;
3626 struct btrfs_root *root = BTRFS_I(inode)->root;
3643 if (btrfs_root_readonly(root)) {
3661 if (IS_ERR(trans)) {
3662 ret = PTR_ERR(trans);
3672 btrfs_set_root_stransid(root_item, sa->
stransid);
3673 btrfs_set_root_rtransid(root_item, sa->
rtransid);
3703 cmd,
unsigned long arg)
3705 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3710 return btrfs_ioctl_getflags(file, argp);
3712 return btrfs_ioctl_setflags(file, argp);
3714 return btrfs_ioctl_getversion(file, argp);
3716 return btrfs_ioctl_fitrim(file, argp);
3718 return btrfs_ioctl_snap_create(file, argp, 0);
3720 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3722 return btrfs_ioctl_snap_create(file, argp, 1);
3724 return btrfs_ioctl_snap_create_v2(file, argp, 1);
3726 return btrfs_ioctl_snap_destroy(file, argp);
3728 return btrfs_ioctl_subvol_getflags(file, argp);
3730 return btrfs_ioctl_subvol_setflags(file, argp);
3732 return btrfs_ioctl_default_subvol(file, argp);
3734 return btrfs_ioctl_defrag(file,
NULL);
3736 return btrfs_ioctl_defrag(file, argp);
3738 return btrfs_ioctl_resize(root, argp);
3740 return btrfs_ioctl_add_dev(root, argp);
3742 return btrfs_ioctl_rm_dev(root, argp);
3744 return btrfs_ioctl_fs_info(root, argp);
3746 return btrfs_ioctl_dev_info(root, argp);
3748 return btrfs_ioctl_balance(file,
NULL);
3750 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3752 return btrfs_ioctl_clone_range(file, argp);
3754 return btrfs_ioctl_trans_start(file);
3758 return btrfs_ioctl_tree_search(file, argp);
3760 return btrfs_ioctl_ino_lookup(file, argp);
3762 return btrfs_ioctl_ino_to_path(root, argp);
3764 return btrfs_ioctl_logical_to_ino(root, argp);
3771 return btrfs_ioctl_start_sync(file, argp);
3773 return btrfs_ioctl_wait_sync(file, argp);
3775 return btrfs_ioctl_scrub(root, argp);
3777 return btrfs_ioctl_scrub_cancel(root, argp);
3779 return btrfs_ioctl_scrub_progress(root, argp);
3781 return btrfs_ioctl_balance(file, argp);
3783 return btrfs_ioctl_balance_ctl(root, arg);
3785 return btrfs_ioctl_balance_progress(root, argp);
3787 return btrfs_ioctl_set_received_subvol(file, argp);
3793 return btrfs_ioctl_quota_ctl(root, argp);
3795 return btrfs_ioctl_qgroup_assign(root, argp);
3797 return btrfs_ioctl_qgroup_create(root, argp);
3799 return btrfs_ioctl_qgroup_limit(root, argp);