1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2f46b5a66SChristoph Hellwig /* 3f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 4f46b5a66SChristoph Hellwig */ 5f46b5a66SChristoph Hellwig 6f46b5a66SChristoph Hellwig #include <linux/kernel.h> 7f46b5a66SChristoph Hellwig #include <linux/bio.h> 8f46b5a66SChristoph Hellwig #include <linux/file.h> 9f46b5a66SChristoph Hellwig #include <linux/fs.h> 10cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 11f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 12f46b5a66SChristoph Hellwig #include <linux/highmem.h> 13f46b5a66SChristoph Hellwig #include <linux/time.h> 14f46b5a66SChristoph Hellwig #include <linux/string.h> 15f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 16cb8e7090SChristoph Hellwig #include <linux/mount.h> 17cb8e7090SChristoph Hellwig #include <linux/namei.h> 18f46b5a66SChristoph Hellwig #include <linux/writeback.h> 19f46b5a66SChristoph Hellwig #include <linux/compat.h> 20cb8e7090SChristoph Hellwig #include <linux/security.h> 21f46b5a66SChristoph Hellwig #include <linux/xattr.h> 22f54de068SDavid Sterba #include <linux/mm.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 24f7039b1dSLi Dongyang #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2655e301fdSFilipe Brandenburger #include <linux/btrfs.h> 27416161dbSMark Fasheh #include <linux/uaccess.h> 28ae5e165dSJeff Layton #include <linux/iversion.h> 29f46b5a66SChristoph Hellwig #include "ctree.h" 30f46b5a66SChristoph Hellwig #include "disk-io.h" 31f46b5a66SChristoph Hellwig #include "transaction.h" 32f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 33f46b5a66SChristoph Hellwig #include "print-tree.h" 34f46b5a66SChristoph Hellwig #include "volumes.h" 35925baeddSChris Mason #include "locking.h" 36581bb050SLi Zefan #include "inode-map.h" 37d7728c96SJan Schmidt #include "backref.h" 38606686eeSJosef Bacik #include "rcu-string.h" 3931db9f7cSAlexander Block #include "send.h" 403f6bcfbdSStefan Behrens #include "dev-replace.h" 4163541927SFilipe David Borba Manana #include "props.h" 423b02a68aSJeff Mahoney #include "sysfs.h" 43fcebe456SJosef Bacik #include "qgroup.h" 441ec9a1aeSFilipe Manana #include "tree-log.h" 45ebb8765bSAnand Jain #include "compression.h" 46f46b5a66SChristoph Hellwig 47abccd00fSHugo Mills #ifdef CONFIG_64BIT 48abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 49abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 50abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 51abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 52abccd00fSHugo Mills */ 53abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 54abccd00fSHugo Mills __u64 sec; 55abccd00fSHugo Mills __u32 nsec; 56abccd00fSHugo Mills } __attribute__ ((__packed__)); 57abccd00fSHugo Mills 58abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 59abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 60abccd00fSHugo Mills __u64 stransid; /* in */ 61abccd00fSHugo Mills __u64 rtransid; /* out */ 62abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 63abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 64abccd00fSHugo Mills __u64 flags; /* in */ 65abccd00fSHugo Mills __u64 reserved[16]; /* in */ 66abccd00fSHugo Mills } __attribute__ ((__packed__)); 67abccd00fSHugo Mills 68abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 69abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 70abccd00fSHugo Mills #endif 71abccd00fSHugo Mills 722351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 732351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 742351f431SJosef Bacik __s64 send_fd; /* in */ 752351f431SJosef Bacik __u64 clone_sources_count; /* in */ 762351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 772351f431SJosef Bacik __u64 parent_root; /* in */ 782351f431SJosef Bacik __u64 flags; /* in */ 792351f431SJosef Bacik __u64 reserved[4]; /* in */ 802351f431SJosef Bacik } __attribute__ ((__packed__)); 812351f431SJosef Bacik 822351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 832351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 842351f431SJosef Bacik #endif 85abccd00fSHugo Mills 86416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 871c919a5eSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff, 881c919a5eSMark Fasheh int no_time_update); 89416161dbSMark Fasheh 906cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 911905a0f7SDavid Sterba static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, 921905a0f7SDavid Sterba unsigned int flags) 936cbff00fSChristoph Hellwig { 941905a0f7SDavid Sterba if (S_ISDIR(inode->i_mode)) 956cbff00fSChristoph Hellwig return flags; 961905a0f7SDavid Sterba else if (S_ISREG(inode->i_mode)) 976cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 986cbff00fSChristoph Hellwig else 996cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1006cbff00fSChristoph Hellwig } 101f46b5a66SChristoph Hellwig 1026cbff00fSChristoph Hellwig /* 103a157d4fdSDavid Sterba * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS 104a157d4fdSDavid Sterba * ioctl. 1056cbff00fSChristoph Hellwig */ 106a157d4fdSDavid Sterba static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) 1076cbff00fSChristoph Hellwig { 1086cbff00fSChristoph Hellwig unsigned int iflags = 0; 1096cbff00fSChristoph Hellwig 1106cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1116cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1126cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1136cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1146cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1156cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1166cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1176cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1186cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1196cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1206cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1216cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 122d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 123d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 124d0092bddSLi Zefan 12513f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 126d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 12713f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 12813f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1296cbff00fSChristoph Hellwig 1306cbff00fSChristoph Hellwig return iflags; 1316cbff00fSChristoph Hellwig } 1326cbff00fSChristoph Hellwig 1336cbff00fSChristoph Hellwig /* 1346cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1356cbff00fSChristoph Hellwig */ 1367b6a221eSDavid Sterba void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) 1376cbff00fSChristoph Hellwig { 1385c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1393cc79392SFilipe Manana unsigned int new_fl = 0; 1406cbff00fSChristoph Hellwig 1415c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_SYNC) 1423cc79392SFilipe Manana new_fl |= S_SYNC; 1435c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_IMMUTABLE) 1443cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1455c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_APPEND) 1463cc79392SFilipe Manana new_fl |= S_APPEND; 1475c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_NOATIME) 1483cc79392SFilipe Manana new_fl |= S_NOATIME; 1495c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_DIRSYNC) 1503cc79392SFilipe Manana new_fl |= S_DIRSYNC; 1513cc79392SFilipe Manana 1523cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 1533cc79392SFilipe Manana S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, 1543cc79392SFilipe Manana new_fl); 1556cbff00fSChristoph Hellwig } 1566cbff00fSChristoph Hellwig 1576cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1586cbff00fSChristoph Hellwig { 1595c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(file_inode(file)); 1605c57b8b6SDavid Sterba unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); 1616cbff00fSChristoph Hellwig 1626cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1636cbff00fSChristoph Hellwig return -EFAULT; 1646cbff00fSChristoph Hellwig return 0; 1656cbff00fSChristoph Hellwig } 1666cbff00fSChristoph Hellwig 1675ba76abfSDavid Sterba /* Check if @flags are a supported and valid set of FS_*_FL flags */ 1685ba76abfSDavid Sterba static int check_fsflags(unsigned int flags) 16975e7cb7fSLiu Bo { 17075e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 17175e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 17275e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 173e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 174e1e8fb6aSLi Zefan FS_NOCOW_FL)) 17575e7cb7fSLiu Bo return -EOPNOTSUPP; 17675e7cb7fSLiu Bo 17775e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 17875e7cb7fSLiu Bo return -EINVAL; 17975e7cb7fSLiu Bo 18075e7cb7fSLiu Bo return 0; 18175e7cb7fSLiu Bo } 18275e7cb7fSLiu Bo 1836cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1846cbff00fSChristoph Hellwig { 185496ad9aaSAl Viro struct inode *inode = file_inode(file); 1860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1875c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1885c57b8b6SDavid Sterba struct btrfs_root *root = binode->root; 1896cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1905c57b8b6SDavid Sterba unsigned int fsflags, old_fsflags; 1916cbff00fSChristoph Hellwig int ret; 1925c57b8b6SDavid Sterba u64 old_flags; 1935c57b8b6SDavid Sterba unsigned int old_i_flags; 1947e97b8daSDavid Sterba umode_t mode; 1956cbff00fSChristoph Hellwig 196bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 197bd60ea0fSDavid Sterba return -EPERM; 198bd60ea0fSDavid Sterba 199b83cc969SLi Zefan if (btrfs_root_readonly(root)) 200b83cc969SLi Zefan return -EROFS; 201b83cc969SLi Zefan 2025c57b8b6SDavid Sterba if (copy_from_user(&fsflags, arg, sizeof(fsflags))) 2036cbff00fSChristoph Hellwig return -EFAULT; 2046cbff00fSChristoph Hellwig 2055c57b8b6SDavid Sterba ret = check_fsflags(fsflags); 20675e7cb7fSLiu Bo if (ret) 20775e7cb7fSLiu Bo return ret; 2086cbff00fSChristoph Hellwig 209e7848683SJan Kara ret = mnt_want_write_file(file); 210e7848683SJan Kara if (ret) 211e7848683SJan Kara return ret; 212e7848683SJan Kara 2135955102cSAl Viro inode_lock(inode); 2146cbff00fSChristoph Hellwig 2155c57b8b6SDavid Sterba old_flags = binode->flags; 2165c57b8b6SDavid Sterba old_i_flags = inode->i_flags; 2177e97b8daSDavid Sterba mode = inode->i_mode; 218f062abf0SLi Zefan 2195c57b8b6SDavid Sterba fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); 2205c57b8b6SDavid Sterba old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); 2215c57b8b6SDavid Sterba if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2226cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2236cbff00fSChristoph Hellwig ret = -EPERM; 2246cbff00fSChristoph Hellwig goto out_unlock; 2256cbff00fSChristoph Hellwig } 2266cbff00fSChristoph Hellwig } 2276cbff00fSChristoph Hellwig 2285c57b8b6SDavid Sterba if (fsflags & FS_SYNC_FL) 2295c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_SYNC; 2306cbff00fSChristoph Hellwig else 2315c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_SYNC; 2325c57b8b6SDavid Sterba if (fsflags & FS_IMMUTABLE_FL) 2335c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_IMMUTABLE; 2346cbff00fSChristoph Hellwig else 2355c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_IMMUTABLE; 2365c57b8b6SDavid Sterba if (fsflags & FS_APPEND_FL) 2375c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_APPEND; 2386cbff00fSChristoph Hellwig else 2395c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_APPEND; 2405c57b8b6SDavid Sterba if (fsflags & FS_NODUMP_FL) 2415c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_NODUMP; 2426cbff00fSChristoph Hellwig else 2435c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_NODUMP; 2445c57b8b6SDavid Sterba if (fsflags & FS_NOATIME_FL) 2455c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_NOATIME; 2466cbff00fSChristoph Hellwig else 2475c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_NOATIME; 2485c57b8b6SDavid Sterba if (fsflags & FS_DIRSYNC_FL) 2495c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_DIRSYNC; 2506cbff00fSChristoph Hellwig else 2515c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_DIRSYNC; 2525c57b8b6SDavid Sterba if (fsflags & FS_NOCOW_FL) { 2537e97b8daSDavid Sterba if (S_ISREG(mode)) { 2547e97b8daSDavid Sterba /* 2557e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2567e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2577e97b8daSDavid Sterba * status of the file and will not set it. 2587e97b8daSDavid Sterba */ 2597e97b8daSDavid Sterba if (inode->i_size == 0) 2605c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_NODATACOW 2617e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2627e97b8daSDavid Sterba } else { 2635c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_NODATACOW; 2647e97b8daSDavid Sterba } 2657e97b8daSDavid Sterba } else { 2667e97b8daSDavid Sterba /* 26701327610SNicholas D Steeves * Revert back under same assumptions as above 2687e97b8daSDavid Sterba */ 2697e97b8daSDavid Sterba if (S_ISREG(mode)) { 2707e97b8daSDavid Sterba if (inode->i_size == 0) 2715c57b8b6SDavid Sterba binode->flags &= ~(BTRFS_INODE_NODATACOW 2727e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2737e97b8daSDavid Sterba } else { 2745c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_NODATACOW; 2757e97b8daSDavid Sterba } 2767e97b8daSDavid Sterba } 2776cbff00fSChristoph Hellwig 27875e7cb7fSLiu Bo /* 27975e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 28075e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 28175e7cb7fSLiu Bo * things smaller. 28275e7cb7fSLiu Bo */ 2835c57b8b6SDavid Sterba if (fsflags & FS_NOCOMP_FL) { 2845c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_COMPRESS; 2855c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_NOCOMPRESS; 28663541927SFilipe David Borba Manana 28763541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 28863541927SFilipe David Borba Manana if (ret && ret != -ENODATA) 28963541927SFilipe David Borba Manana goto out_drop; 2905c57b8b6SDavid Sterba } else if (fsflags & FS_COMPR_FL) { 29163541927SFilipe David Borba Manana const char *comp; 29263541927SFilipe David Borba Manana 293eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 294eede2bf3SOmar Sandoval ret = -ETXTBSY; 295eede2bf3SOmar Sandoval goto out_unlock; 296eede2bf3SOmar Sandoval } 297eede2bf3SOmar Sandoval 2985c57b8b6SDavid Sterba binode->flags |= BTRFS_INODE_COMPRESS; 2995c57b8b6SDavid Sterba binode->flags &= ~BTRFS_INODE_NOCOMPRESS; 30063541927SFilipe David Borba Manana 30193370509SDavid Sterba comp = btrfs_compress_type2str(fs_info->compress_type); 30293370509SDavid Sterba if (!comp || comp[0] == 0) 30393370509SDavid Sterba comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); 30493370509SDavid Sterba 30563541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", 30663541927SFilipe David Borba Manana comp, strlen(comp), 0); 30763541927SFilipe David Borba Manana if (ret) 30863541927SFilipe David Borba Manana goto out_drop; 30963541927SFilipe David Borba Manana 310ebcb904dSLi Zefan } else { 31178a017a2SFilipe Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 31278a017a2SFilipe Manana if (ret && ret != -ENODATA) 31378a017a2SFilipe Manana goto out_drop; 3145c57b8b6SDavid Sterba binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 31575e7cb7fSLiu Bo } 3166cbff00fSChristoph Hellwig 3174da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 318f062abf0SLi Zefan if (IS_ERR(trans)) { 319f062abf0SLi Zefan ret = PTR_ERR(trans); 320f062abf0SLi Zefan goto out_drop; 321f062abf0SLi Zefan } 3226cbff00fSChristoph Hellwig 3237b6a221eSDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 3240c4d2d95SJosef Bacik inode_inc_iversion(inode); 325c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3266cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3276cbff00fSChristoph Hellwig 3283a45bb20SJeff Mahoney btrfs_end_transaction(trans); 329f062abf0SLi Zefan out_drop: 330f062abf0SLi Zefan if (ret) { 3315c57b8b6SDavid Sterba binode->flags = old_flags; 3325c57b8b6SDavid Sterba inode->i_flags = old_i_flags; 333f062abf0SLi Zefan } 3346cbff00fSChristoph Hellwig 3356cbff00fSChristoph Hellwig out_unlock: 3365955102cSAl Viro inode_unlock(inode); 337e7848683SJan Kara mnt_drop_write_file(file); 3382d4e6f6aSliubo return ret; 3396cbff00fSChristoph Hellwig } 3406cbff00fSChristoph Hellwig 34119f93b3cSDavid Sterba /* 34219f93b3cSDavid Sterba * Translate btrfs internal inode flags to xflags as expected by the 34319f93b3cSDavid Sterba * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are 34419f93b3cSDavid Sterba * silently dropped. 34519f93b3cSDavid Sterba */ 34619f93b3cSDavid Sterba static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) 34719f93b3cSDavid Sterba { 34819f93b3cSDavid Sterba unsigned int xflags = 0; 34919f93b3cSDavid Sterba 35019f93b3cSDavid Sterba if (flags & BTRFS_INODE_APPEND) 35119f93b3cSDavid Sterba xflags |= FS_XFLAG_APPEND; 35219f93b3cSDavid Sterba if (flags & BTRFS_INODE_IMMUTABLE) 35319f93b3cSDavid Sterba xflags |= FS_XFLAG_IMMUTABLE; 35419f93b3cSDavid Sterba if (flags & BTRFS_INODE_NOATIME) 35519f93b3cSDavid Sterba xflags |= FS_XFLAG_NOATIME; 35619f93b3cSDavid Sterba if (flags & BTRFS_INODE_NODUMP) 35719f93b3cSDavid Sterba xflags |= FS_XFLAG_NODUMP; 35819f93b3cSDavid Sterba if (flags & BTRFS_INODE_SYNC) 35919f93b3cSDavid Sterba xflags |= FS_XFLAG_SYNC; 36019f93b3cSDavid Sterba 36119f93b3cSDavid Sterba return xflags; 36219f93b3cSDavid Sterba } 36319f93b3cSDavid Sterba 36419f93b3cSDavid Sterba /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ 36519f93b3cSDavid Sterba static int check_xflags(unsigned int flags) 36619f93b3cSDavid Sterba { 36719f93b3cSDavid Sterba if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | 36819f93b3cSDavid Sterba FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) 36919f93b3cSDavid Sterba return -EOPNOTSUPP; 37019f93b3cSDavid Sterba return 0; 37119f93b3cSDavid Sterba } 37219f93b3cSDavid Sterba 373e4202ac9SDavid Sterba /* 374e4202ac9SDavid Sterba * Set the xflags from the internal inode flags. The remaining items of fsxattr 375e4202ac9SDavid Sterba * are zeroed. 376e4202ac9SDavid Sterba */ 377e4202ac9SDavid Sterba static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) 378e4202ac9SDavid Sterba { 379e4202ac9SDavid Sterba struct btrfs_inode *binode = BTRFS_I(file_inode(file)); 380e4202ac9SDavid Sterba struct fsxattr fa; 381e4202ac9SDavid Sterba 382e4202ac9SDavid Sterba memset(&fa, 0, sizeof(fa)); 383e4202ac9SDavid Sterba fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags); 384e4202ac9SDavid Sterba 385e4202ac9SDavid Sterba if (copy_to_user(arg, &fa, sizeof(fa))) 386e4202ac9SDavid Sterba return -EFAULT; 387e4202ac9SDavid Sterba 388e4202ac9SDavid Sterba return 0; 389e4202ac9SDavid Sterba } 390e4202ac9SDavid Sterba 391025f2121SDavid Sterba static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) 392025f2121SDavid Sterba { 393025f2121SDavid Sterba struct inode *inode = file_inode(file); 394025f2121SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 395025f2121SDavid Sterba struct btrfs_root *root = binode->root; 396025f2121SDavid Sterba struct btrfs_trans_handle *trans; 397025f2121SDavid Sterba struct fsxattr fa; 398025f2121SDavid Sterba unsigned old_flags; 399025f2121SDavid Sterba unsigned old_i_flags; 400025f2121SDavid Sterba int ret = 0; 401025f2121SDavid Sterba 402025f2121SDavid Sterba if (!inode_owner_or_capable(inode)) 403025f2121SDavid Sterba return -EPERM; 404025f2121SDavid Sterba 405025f2121SDavid Sterba if (btrfs_root_readonly(root)) 406025f2121SDavid Sterba return -EROFS; 407025f2121SDavid Sterba 408025f2121SDavid Sterba memset(&fa, 0, sizeof(fa)); 409025f2121SDavid Sterba if (copy_from_user(&fa, arg, sizeof(fa))) 410025f2121SDavid Sterba return -EFAULT; 411025f2121SDavid Sterba 412025f2121SDavid Sterba ret = check_xflags(fa.fsx_xflags); 413025f2121SDavid Sterba if (ret) 414025f2121SDavid Sterba return ret; 415025f2121SDavid Sterba 416025f2121SDavid Sterba if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) 417025f2121SDavid Sterba return -EOPNOTSUPP; 418025f2121SDavid Sterba 419025f2121SDavid Sterba ret = mnt_want_write_file(file); 420025f2121SDavid Sterba if (ret) 421025f2121SDavid Sterba return ret; 422025f2121SDavid Sterba 423025f2121SDavid Sterba inode_lock(inode); 424025f2121SDavid Sterba 425025f2121SDavid Sterba old_flags = binode->flags; 426025f2121SDavid Sterba old_i_flags = inode->i_flags; 427025f2121SDavid Sterba 428025f2121SDavid Sterba /* We need the capabilities to change append-only or immutable inode */ 429025f2121SDavid Sterba if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) || 430025f2121SDavid Sterba (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) && 431025f2121SDavid Sterba !capable(CAP_LINUX_IMMUTABLE)) { 432025f2121SDavid Sterba ret = -EPERM; 433025f2121SDavid Sterba goto out_unlock; 434025f2121SDavid Sterba } 435025f2121SDavid Sterba 436025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_SYNC) 437025f2121SDavid Sterba binode->flags |= BTRFS_INODE_SYNC; 438025f2121SDavid Sterba else 439025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_SYNC; 440025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) 441025f2121SDavid Sterba binode->flags |= BTRFS_INODE_IMMUTABLE; 442025f2121SDavid Sterba else 443025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_IMMUTABLE; 444025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_APPEND) 445025f2121SDavid Sterba binode->flags |= BTRFS_INODE_APPEND; 446025f2121SDavid Sterba else 447025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_APPEND; 448025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_NODUMP) 449025f2121SDavid Sterba binode->flags |= BTRFS_INODE_NODUMP; 450025f2121SDavid Sterba else 451025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_NODUMP; 452025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_NOATIME) 453025f2121SDavid Sterba binode->flags |= BTRFS_INODE_NOATIME; 454025f2121SDavid Sterba else 455025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_NOATIME; 456025f2121SDavid Sterba 457025f2121SDavid Sterba /* 1 item for the inode */ 458025f2121SDavid Sterba trans = btrfs_start_transaction(root, 1); 459025f2121SDavid Sterba if (IS_ERR(trans)) { 460025f2121SDavid Sterba ret = PTR_ERR(trans); 461025f2121SDavid Sterba goto out_unlock; 462025f2121SDavid Sterba } 463025f2121SDavid Sterba 464025f2121SDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 465025f2121SDavid Sterba inode_inc_iversion(inode); 466025f2121SDavid Sterba inode->i_ctime = current_time(inode); 467025f2121SDavid Sterba ret = btrfs_update_inode(trans, root, inode); 468025f2121SDavid Sterba 469025f2121SDavid Sterba btrfs_end_transaction(trans); 470025f2121SDavid Sterba 471025f2121SDavid Sterba out_unlock: 472025f2121SDavid Sterba if (ret) { 473025f2121SDavid Sterba binode->flags = old_flags; 474025f2121SDavid Sterba inode->i_flags = old_i_flags; 475025f2121SDavid Sterba } 476025f2121SDavid Sterba 477025f2121SDavid Sterba inode_unlock(inode); 478025f2121SDavid Sterba mnt_drop_write_file(file); 479025f2121SDavid Sterba 480025f2121SDavid Sterba return ret; 481025f2121SDavid Sterba } 482025f2121SDavid Sterba 4836cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 4846cbff00fSChristoph Hellwig { 485496ad9aaSAl Viro struct inode *inode = file_inode(file); 4866cbff00fSChristoph Hellwig 4876cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 4886cbff00fSChristoph Hellwig } 489f46b5a66SChristoph Hellwig 490f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 491f7039b1dSLi Dongyang { 4920b246afaSJeff Mahoney struct inode *inode = file_inode(file); 4930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 494f7039b1dSLi Dongyang struct btrfs_device *device; 495f7039b1dSLi Dongyang struct request_queue *q; 496f7039b1dSLi Dongyang struct fstrim_range range; 497f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 498f7039b1dSLi Dongyang u64 num_devices = 0; 499f7039b1dSLi Dongyang int ret; 500f7039b1dSLi Dongyang 501f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 502f7039b1dSLi Dongyang return -EPERM; 503f7039b1dSLi Dongyang 5041f78160cSXiao Guangrong rcu_read_lock(); 5051f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 5061f78160cSXiao Guangrong dev_list) { 507f7039b1dSLi Dongyang if (!device->bdev) 508f7039b1dSLi Dongyang continue; 509f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 510f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 511f7039b1dSLi Dongyang num_devices++; 51250d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 513f7039b1dSLi Dongyang minlen); 514f7039b1dSLi Dongyang } 515f7039b1dSLi Dongyang } 5161f78160cSXiao Guangrong rcu_read_unlock(); 517f4c697e6SLukas Czerner 518f7039b1dSLi Dongyang if (!num_devices) 519f7039b1dSLi Dongyang return -EOPNOTSUPP; 520f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 521f7039b1dSLi Dongyang return -EFAULT; 5226ba9fc8eSQu Wenruo 5236ba9fc8eSQu Wenruo /* 5246ba9fc8eSQu Wenruo * NOTE: Don't truncate the range using super->total_bytes. Bytenr of 5256ba9fc8eSQu Wenruo * block group is in the logical address space, which can be any 5266ba9fc8eSQu Wenruo * sectorsize aligned bytenr in the range [0, U64_MAX]. 5276ba9fc8eSQu Wenruo */ 5286ba9fc8eSQu Wenruo if (range.len < fs_info->sb->s_blocksize) 529f4c697e6SLukas Czerner return -EINVAL; 530f7039b1dSLi Dongyang 531f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 5322ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 533f7039b1dSLi Dongyang if (ret < 0) 534f7039b1dSLi Dongyang return ret; 535f7039b1dSLi Dongyang 536f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 537f7039b1dSLi Dongyang return -EFAULT; 538f7039b1dSLi Dongyang 539f7039b1dSLi Dongyang return 0; 540f7039b1dSLi Dongyang } 541f7039b1dSLi Dongyang 542dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 543dd5f9615SStefan Behrens { 54446e0f66aSChris Mason int i; 54546e0f66aSChris Mason 54646e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 54746e0f66aSChris Mason if (uuid[i]) 54846e0f66aSChris Mason return 0; 54946e0f66aSChris Mason } 55046e0f66aSChris Mason return 1; 551dd5f9615SStefan Behrens } 552dd5f9615SStefan Behrens 553d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 554cb8e7090SChristoph Hellwig struct dentry *dentry, 55552f75f4fSDavid Sterba const char *name, int namelen, 5566f72c7e2SArne Jansen u64 *async_transid, 5578696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 558f46b5a66SChristoph Hellwig { 5590b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 560f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 561f46b5a66SChristoph Hellwig struct btrfs_key key; 56249a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 563f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 564f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 565d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 56676dda93cSYan, Zheng struct btrfs_root *new_root; 567d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 56895582b00SDeepa Dinamani struct timespec64 cur_time = current_time(dir); 5695662344bSTsutomu Itoh struct inode *inode; 570f46b5a66SChristoph Hellwig int ret; 571f46b5a66SChristoph Hellwig int err; 572f46b5a66SChristoph Hellwig u64 objectid; 573f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 5743de4586cSChris Mason u64 index = 0; 5758ea05e3aSAlexander Block uuid_le new_uuid; 576f46b5a66SChristoph Hellwig 57749a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 57849a3c4d9SDavid Sterba if (!root_item) 57949a3c4d9SDavid Sterba return -ENOMEM; 58049a3c4d9SDavid Sterba 5810b246afaSJeff Mahoney ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); 5822fbe8c8aSAl Viro if (ret) 58349a3c4d9SDavid Sterba goto fail_free; 5846a912213SJosef Bacik 585e09fe2d2SQu Wenruo /* 586e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 58701327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 588e09fe2d2SQu Wenruo */ 58949a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 59049a3c4d9SDavid Sterba ret = -ENOSPC; 59149a3c4d9SDavid Sterba goto fail_free; 59249a3c4d9SDavid Sterba } 593e09fe2d2SQu Wenruo 594d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 5959ed74f2dSJosef Bacik /* 596d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 597d5c12070SMiao Xie * of create_snapshot(). 5989ed74f2dSJosef Bacik */ 599c4c129dbSGu JinXiang ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); 600d5c12070SMiao Xie if (ret) 60149a3c4d9SDavid Sterba goto fail_free; 602f46b5a66SChristoph Hellwig 603d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 604d5c12070SMiao Xie if (IS_ERR(trans)) { 605d5c12070SMiao Xie ret = PTR_ERR(trans); 6067775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 60749a3c4d9SDavid Sterba goto fail_free; 608d5c12070SMiao Xie } 609d5c12070SMiao Xie trans->block_rsv = &block_rsv; 610d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 611f46b5a66SChristoph Hellwig 612a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); 6136f72c7e2SArne Jansen if (ret) 6146f72c7e2SArne Jansen goto fail; 6156f72c7e2SArne Jansen 6164d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 6178e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 6188e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 6198e8a1e31SJosef Bacik goto fail; 6208e8a1e31SJosef Bacik } 621f46b5a66SChristoph Hellwig 622f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 623f46b5a66SChristoph Hellwig 62449a3c4d9SDavid Sterba inode_item = &root_item->inode; 6253cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 6263cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 6273cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 628da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 6290b246afaSJeff Mahoney fs_info->nodesize); 6303cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 631f46b5a66SChristoph Hellwig 63249a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 63349a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 6343cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 63508fe4db1SLi Zefan 63649a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 63749a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 63849a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 63949a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 64049a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 64149a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 642f46b5a66SChristoph Hellwig 64349a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 64449a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 6458ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 64649a3c4d9SDavid Sterba memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 64749a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 64849a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 64949a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 65049a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 65149a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 652f46b5a66SChristoph Hellwig 653925baeddSChris Mason btrfs_tree_unlock(leaf); 654f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 655f46b5a66SChristoph Hellwig leaf = NULL; 656f46b5a66SChristoph Hellwig 65749a3c4d9SDavid Sterba btrfs_set_root_dirid(root_item, new_dirid); 658f46b5a66SChristoph Hellwig 659f46b5a66SChristoph Hellwig key.objectid = objectid; 6605d4f98a2SYan Zheng key.offset = 0; 661962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 6620b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 66349a3c4d9SDavid Sterba root_item); 664f46b5a66SChristoph Hellwig if (ret) 665f46b5a66SChristoph Hellwig goto fail; 666f46b5a66SChristoph Hellwig 66776dda93cSYan, Zheng key.offset = (u64)-1; 6680b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &key); 66979787eaaSJeff Mahoney if (IS_ERR(new_root)) { 67079787eaaSJeff Mahoney ret = PTR_ERR(new_root); 67166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 67279787eaaSJeff Mahoney goto fail; 67379787eaaSJeff Mahoney } 67476dda93cSYan, Zheng 67576dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 67676dda93cSYan, Zheng 67763541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 678ce598979SMark Fasheh if (ret) { 679ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 68066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 681ce598979SMark Fasheh goto fail; 682ce598979SMark Fasheh } 683ce598979SMark Fasheh 684f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 685f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 686f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 687f32e48e9SChandan Rajendra 688f46b5a66SChristoph Hellwig /* 689f46b5a66SChristoph Hellwig * insert the directory item 690f46b5a66SChristoph Hellwig */ 691877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 69279787eaaSJeff Mahoney if (ret) { 69366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 69479787eaaSJeff Mahoney goto fail; 69579787eaaSJeff Mahoney } 6963de4586cSChris Mason 697684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, 6983de4586cSChris Mason BTRFS_FT_DIR, index); 69979787eaaSJeff Mahoney if (ret) { 70066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 701f46b5a66SChristoph Hellwig goto fail; 70279787eaaSJeff Mahoney } 7030660b5afSChris Mason 7046ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 70552c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 70652c26179SYan Zheng BUG_ON(ret); 70752c26179SYan Zheng 7086025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, 7094a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 7100660b5afSChris Mason BUG_ON(ret); 7110660b5afSChris Mason 712cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, root_item->uuid, 7136bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 714dd5f9615SStefan Behrens if (ret) 71566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 716dd5f9615SStefan Behrens 717f46b5a66SChristoph Hellwig fail: 71849a3c4d9SDavid Sterba kfree(root_item); 719d5c12070SMiao Xie trans->block_rsv = NULL; 720d5c12070SMiao Xie trans->bytes_reserved = 0; 7217775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 722de6e8200SLiu Bo 72372fd032eSSage Weil if (async_transid) { 72472fd032eSSage Weil *async_transid = trans->transid; 7253a45bb20SJeff Mahoney err = btrfs_commit_transaction_async(trans, 1); 72600d71c9cSMiao Xie if (err) 7273a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 72872fd032eSSage Weil } else { 7293a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 73072fd032eSSage Weil } 731f46b5a66SChristoph Hellwig if (err && !ret) 732f46b5a66SChristoph Hellwig ret = err; 7331a65e24bSChris Mason 7345662344bSTsutomu Itoh if (!ret) { 7355662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 736de6e8200SLiu Bo if (IS_ERR(inode)) 737de6e8200SLiu Bo return PTR_ERR(inode); 7385662344bSTsutomu Itoh d_instantiate(dentry, inode); 7395662344bSTsutomu Itoh } 740f46b5a66SChristoph Hellwig return ret; 74149a3c4d9SDavid Sterba 74249a3c4d9SDavid Sterba fail_free: 74349a3c4d9SDavid Sterba kfree(root_item); 74449a3c4d9SDavid Sterba return ret; 745f46b5a66SChristoph Hellwig } 746f46b5a66SChristoph Hellwig 747e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 74861d7e4cbSDavid Sterba struct dentry *dentry, 749e9662f70SMiao Xie u64 *async_transid, bool readonly, 750e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 751f46b5a66SChristoph Hellwig { 7520b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 7532e4bfab9SYan, Zheng struct inode *inode; 754f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 755f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 7562e4bfab9SYan, Zheng int ret; 7578ecebf4dSRobbie Ko bool snapshot_force_cow = false; 758f46b5a66SChristoph Hellwig 75927cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 760f46b5a66SChristoph Hellwig return -EINVAL; 761f46b5a66SChristoph Hellwig 762eede2bf3SOmar Sandoval if (atomic_read(&root->nr_swapfiles)) { 763eede2bf3SOmar Sandoval btrfs_warn(fs_info, 764eede2bf3SOmar Sandoval "cannot snapshot subvolume with active swapfile"); 765eede2bf3SOmar Sandoval return -ETXTBSY; 766eede2bf3SOmar Sandoval } 767eede2bf3SOmar Sandoval 76823269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 769a1ee7362SDavid Sterba if (!pending_snapshot) 770a1ee7362SDavid Sterba return -ENOMEM; 771a1ee7362SDavid Sterba 772b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 77323269bf5SDavid Sterba GFP_KERNEL); 7748546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 7758546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 776b0c0ea63SDavid Sterba ret = -ENOMEM; 777b0c0ea63SDavid Sterba goto free_pending; 778b0c0ea63SDavid Sterba } 779b0c0ea63SDavid Sterba 7808ecebf4dSRobbie Ko /* 7818ecebf4dSRobbie Ko * Force new buffered writes to reserve space even when NOCOW is 7828ecebf4dSRobbie Ko * possible. This is to avoid later writeback (running dealloc) to 7838ecebf4dSRobbie Ko * fallback to COW mode and unexpectedly fail with ENOSPC. 7848ecebf4dSRobbie Ko */ 785ea14b57fSDavid Sterba atomic_inc(&root->will_be_snapshotted); 7864e857c58SPeter Zijlstra smp_mb__after_atomic(); 78745bac0f3SLiu Bo /* wait for no snapshot writes */ 78845bac0f3SLiu Bo wait_event(root->subv_writers->wait, 78945bac0f3SLiu Bo percpu_counter_sum(&root->subv_writers->counter) == 0); 7908257b2dcSMiao Xie 79176f32e24SNikolay Borisov ret = btrfs_start_delalloc_inodes(root); 7926a03843dSMiao Xie if (ret) 793a1ee7362SDavid Sterba goto dec_and_free; 7946a03843dSMiao Xie 7958ecebf4dSRobbie Ko /* 7968ecebf4dSRobbie Ko * All previous writes have started writeback in NOCOW mode, so now 7978ecebf4dSRobbie Ko * we force future writes to fallback to COW mode during snapshot 7988ecebf4dSRobbie Ko * creation. 7998ecebf4dSRobbie Ko */ 8008ecebf4dSRobbie Ko atomic_inc(&root->snapshot_force_cow); 8018ecebf4dSRobbie Ko snapshot_force_cow = true; 8028ecebf4dSRobbie Ko 8036374e57aSChris Mason btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 8046a03843dSMiao Xie 80566d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 80666d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 807d5c12070SMiao Xie /* 808d5c12070SMiao Xie * 1 - parent dir inode 809d5c12070SMiao Xie * 2 - dir entries 810d5c12070SMiao Xie * 1 - root item 811d5c12070SMiao Xie * 2 - root ref/backref 812d5c12070SMiao Xie * 1 - root of snapshot 813dd5f9615SStefan Behrens * 1 - UUID item 814d5c12070SMiao Xie */ 815d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 816dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 817ee3441b4SJeff Mahoney false); 818d5c12070SMiao Xie if (ret) 819a1ee7362SDavid Sterba goto dec_and_free; 820d5c12070SMiao Xie 821a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 822a22285a6SYan, Zheng pending_snapshot->root = root; 823b83cc969SLi Zefan pending_snapshot->readonly = readonly; 824e9662f70SMiao Xie pending_snapshot->dir = dir; 8258696c533SMiao Xie pending_snapshot->inherit = inherit; 826a22285a6SYan, Zheng 827d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 828a22285a6SYan, Zheng if (IS_ERR(trans)) { 829a22285a6SYan, Zheng ret = PTR_ERR(trans); 830a22285a6SYan, Zheng goto fail; 831a22285a6SYan, Zheng } 832a22285a6SYan, Zheng 8330b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 834a22285a6SYan, Zheng list_add(&pending_snapshot->list, 835a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 8360b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 83772fd032eSSage Weil if (async_transid) { 83872fd032eSSage Weil *async_transid = trans->transid; 8393a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 1); 84000d71c9cSMiao Xie if (ret) 8413a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 84272fd032eSSage Weil } else { 8433a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 84472fd032eSSage Weil } 845aec8030aSMiao Xie if (ret) 846c37b2b62SJosef Bacik goto fail; 847a22285a6SYan, Zheng 848a22285a6SYan, Zheng ret = pending_snapshot->error; 849f46b5a66SChristoph Hellwig if (ret) 8502e4bfab9SYan, Zheng goto fail; 851f46b5a66SChristoph Hellwig 852d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 853d3797308SChris Mason if (ret) 854d3797308SChris Mason goto fail; 855d3797308SChris Mason 8562b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 8572e4bfab9SYan, Zheng if (IS_ERR(inode)) { 8582e4bfab9SYan, Zheng ret = PTR_ERR(inode); 8592e4bfab9SYan, Zheng goto fail; 8602e4bfab9SYan, Zheng } 8615662344bSTsutomu Itoh 8622e4bfab9SYan, Zheng d_instantiate(dentry, inode); 8632e4bfab9SYan, Zheng ret = 0; 8642e4bfab9SYan, Zheng fail: 8657775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); 866a1ee7362SDavid Sterba dec_and_free: 8678ecebf4dSRobbie Ko if (snapshot_force_cow) 8688ecebf4dSRobbie Ko atomic_dec(&root->snapshot_force_cow); 869ea14b57fSDavid Sterba if (atomic_dec_and_test(&root->will_be_snapshotted)) 8704625956aSPeter Zijlstra wake_up_var(&root->will_be_snapshotted); 871b0c0ea63SDavid Sterba free_pending: 872b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 8738546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 874a1ee7362SDavid Sterba kfree(pending_snapshot); 875a1ee7362SDavid Sterba 876f46b5a66SChristoph Hellwig return ret; 877f46b5a66SChristoph Hellwig } 878f46b5a66SChristoph Hellwig 8794260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 8804260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 8814260f7c7SSage Weil * whether the type of victim is right. 8824260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 8834260f7c7SSage Weil * 2. We should have write and exec permissions on dir 8844260f7c7SSage Weil * 3. We can't remove anything from append-only dir 8854260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 8864260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 8874260f7c7SSage Weil * a. be owner of dir, or 8884260f7c7SSage Weil * b. be owner of victim, or 8894260f7c7SSage Weil * c. have CAP_FOWNER capability 89001327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 8914260f7c7SSage Weil * links pointing to it. 8924260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 8934260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 8944260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 8954260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 8964260f7c7SSage Weil * nfs_async_unlink(). 8974260f7c7SSage Weil */ 8984260f7c7SSage Weil 8994260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 9004260f7c7SSage Weil { 9014260f7c7SSage Weil int error; 9024260f7c7SSage Weil 9032b0143b5SDavid Howells if (d_really_is_negative(victim)) 9044260f7c7SSage Weil return -ENOENT; 9054260f7c7SSage Weil 9062b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 9074fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 9084260f7c7SSage Weil 9094260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 9104260f7c7SSage Weil if (error) 9114260f7c7SSage Weil return error; 9124260f7c7SSage Weil if (IS_APPEND(dir)) 9134260f7c7SSage Weil return -EPERM; 9142b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 9152b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 9164260f7c7SSage Weil return -EPERM; 9174260f7c7SSage Weil if (isdir) { 918e36cb0b8SDavid Howells if (!d_is_dir(victim)) 9194260f7c7SSage Weil return -ENOTDIR; 9204260f7c7SSage Weil if (IS_ROOT(victim)) 9214260f7c7SSage Weil return -EBUSY; 922e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 9234260f7c7SSage Weil return -EISDIR; 9244260f7c7SSage Weil if (IS_DEADDIR(dir)) 9254260f7c7SSage Weil return -ENOENT; 9264260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 9274260f7c7SSage Weil return -EBUSY; 9284260f7c7SSage Weil return 0; 9294260f7c7SSage Weil } 9304260f7c7SSage Weil 931cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 932cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 933cb8e7090SChristoph Hellwig { 9342b0143b5SDavid Howells if (d_really_is_positive(child)) 935cb8e7090SChristoph Hellwig return -EEXIST; 936cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 937cb8e7090SChristoph Hellwig return -ENOENT; 938cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 939cb8e7090SChristoph Hellwig } 940cb8e7090SChristoph Hellwig 941cb8e7090SChristoph Hellwig /* 942cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 943cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 944cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 945cb8e7090SChristoph Hellwig */ 94692872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 94752f75f4fSDavid Sterba const char *name, int namelen, 94872fd032eSSage Weil struct btrfs_root *snap_src, 9496f72c7e2SArne Jansen u64 *async_transid, bool readonly, 9508696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 951cb8e7090SChristoph Hellwig { 9522b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 9530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 954cb8e7090SChristoph Hellwig struct dentry *dentry; 955cb8e7090SChristoph Hellwig int error; 956cb8e7090SChristoph Hellwig 95700235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 95800235411SAl Viro if (error == -EINTR) 95900235411SAl Viro return error; 960cb8e7090SChristoph Hellwig 961cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 962cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 963cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 964cb8e7090SChristoph Hellwig goto out_unlock; 965cb8e7090SChristoph Hellwig 96676dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 967cb8e7090SChristoph Hellwig if (error) 968a874a63eSLiu Bo goto out_dput; 969cb8e7090SChristoph Hellwig 9709c52057cSChris Mason /* 9719c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 9729c52057cSChris Mason * check for them now when we can safely fail 9739c52057cSChris Mason */ 9749c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 9759c52057cSChris Mason dir->i_ino, name, 9769c52057cSChris Mason namelen); 9779c52057cSChris Mason if (error) 9789c52057cSChris Mason goto out_dput; 9799c52057cSChris Mason 9800b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 98176dda93cSYan, Zheng 98276dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 98376dda93cSYan, Zheng goto out_up_read; 98476dda93cSYan, Zheng 9853de4586cSChris Mason if (snap_src) { 98661d7e4cbSDavid Sterba error = create_snapshot(snap_src, dir, dentry, 9876f72c7e2SArne Jansen async_transid, readonly, inherit); 9883de4586cSChris Mason } else { 989d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 990d5c12070SMiao Xie async_transid, inherit); 9913de4586cSChris Mason } 99276dda93cSYan, Zheng if (!error) 99376dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 99476dda93cSYan, Zheng out_up_read: 9950b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 996cb8e7090SChristoph Hellwig out_dput: 997cb8e7090SChristoph Hellwig dput(dentry); 998cb8e7090SChristoph Hellwig out_unlock: 9995955102cSAl Viro inode_unlock(dir); 1000cb8e7090SChristoph Hellwig return error; 1001cb8e7090SChristoph Hellwig } 1002cb8e7090SChristoph Hellwig 10034cb5300bSChris Mason /* 10044cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 10054cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 10064cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 10074cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 10084cb5300bSChris Mason * part of the file 10094cb5300bSChris Mason */ 1010aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 10114cb5300bSChris Mason { 10124cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 10134cb5300bSChris Mason struct extent_map *em = NULL; 10144cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 10154cb5300bSChris Mason u64 end; 10164cb5300bSChris Mason 10174cb5300bSChris Mason read_lock(&em_tree->lock); 101809cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 10194cb5300bSChris Mason read_unlock(&em_tree->lock); 10204cb5300bSChris Mason 10214cb5300bSChris Mason if (em) { 10224cb5300bSChris Mason end = extent_map_end(em); 10234cb5300bSChris Mason free_extent_map(em); 10244cb5300bSChris Mason if (end - offset > thresh) 10254cb5300bSChris Mason return 0; 10264cb5300bSChris Mason } 10274cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 10284cb5300bSChris Mason thresh /= 2; 10294cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 10304cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 10314cb5300bSChris Mason if (end >= thresh) 10324cb5300bSChris Mason return 0; 10334cb5300bSChris Mason return 1; 10344cb5300bSChris Mason } 10354cb5300bSChris Mason 10364cb5300bSChris Mason /* 10374cb5300bSChris Mason * helper function to walk through a file and find extents 10384cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 10394cb5300bSChris Mason * 10404cb5300bSChris Mason * This is used by the defragging code to find new and small 10414cb5300bSChris Mason * extents 10424cb5300bSChris Mason */ 10434cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 10444cb5300bSChris Mason struct inode *inode, u64 newer_than, 1045aab110abSDavid Sterba u64 *off, u32 thresh) 10464cb5300bSChris Mason { 10474cb5300bSChris Mason struct btrfs_path *path; 10484cb5300bSChris Mason struct btrfs_key min_key; 10494cb5300bSChris Mason struct extent_buffer *leaf; 10504cb5300bSChris Mason struct btrfs_file_extent_item *extent; 10514cb5300bSChris Mason int type; 10524cb5300bSChris Mason int ret; 10534a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode)); 10544cb5300bSChris Mason 10554cb5300bSChris Mason path = btrfs_alloc_path(); 10564cb5300bSChris Mason if (!path) 10574cb5300bSChris Mason return -ENOMEM; 10584cb5300bSChris Mason 1059a4689d2bSDavid Sterba min_key.objectid = ino; 10604cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 10614cb5300bSChris Mason min_key.offset = *off; 10624cb5300bSChris Mason 10634cb5300bSChris Mason while (1) { 10646174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 10654cb5300bSChris Mason if (ret != 0) 10664cb5300bSChris Mason goto none; 1067f094c9bdSFilipe Manana process_slot: 1068a4689d2bSDavid Sterba if (min_key.objectid != ino) 10694cb5300bSChris Mason goto none; 10704cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 10714cb5300bSChris Mason goto none; 10724cb5300bSChris Mason 10734cb5300bSChris Mason leaf = path->nodes[0]; 10744cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 10754cb5300bSChris Mason struct btrfs_file_extent_item); 10764cb5300bSChris Mason 10774cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 10784cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 10794cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 10804cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 10814cb5300bSChris Mason *off = min_key.offset; 10824cb5300bSChris Mason btrfs_free_path(path); 10834cb5300bSChris Mason return 0; 10844cb5300bSChris Mason } 10854cb5300bSChris Mason 1086f094c9bdSFilipe Manana path->slots[0]++; 1087f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 1088f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 1089f094c9bdSFilipe Manana goto process_slot; 1090f094c9bdSFilipe Manana } 1091f094c9bdSFilipe Manana 10924cb5300bSChris Mason if (min_key.offset == (u64)-1) 10934cb5300bSChris Mason goto none; 10944cb5300bSChris Mason 10954cb5300bSChris Mason min_key.offset++; 10964cb5300bSChris Mason btrfs_release_path(path); 10974cb5300bSChris Mason } 10984cb5300bSChris Mason none: 10994cb5300bSChris Mason btrfs_free_path(path); 11004cb5300bSChris Mason return -ENOENT; 11014cb5300bSChris Mason } 11024cb5300bSChris Mason 11036c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 110417ce6ef8SLiu Bo { 110517ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1106940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 11076c282eb4SLi Zefan struct extent_map *em; 110809cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 1109940100a4SChris Mason 1110940100a4SChris Mason /* 1111940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1112940100a4SChris Mason * the full extent lock 1113940100a4SChris Mason */ 1114940100a4SChris Mason read_lock(&em_tree->lock); 1115940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 1116940100a4SChris Mason read_unlock(&em_tree->lock); 1117940100a4SChris Mason 1118940100a4SChris Mason if (!em) { 1119308d9800SFilipe Manana struct extent_state *cached = NULL; 1120308d9800SFilipe Manana u64 end = start + len - 1; 1121308d9800SFilipe Manana 1122940100a4SChris Mason /* get the big lock and read metadata off disk */ 1123ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 1124fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); 1125e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 1126940100a4SChris Mason 11276cf8bfbfSDan Carpenter if (IS_ERR(em)) 11286c282eb4SLi Zefan return NULL; 1129940100a4SChris Mason } 1130940100a4SChris Mason 11316c282eb4SLi Zefan return em; 11326c282eb4SLi Zefan } 11336c282eb4SLi Zefan 11346c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 11356c282eb4SLi Zefan { 11366c282eb4SLi Zefan struct extent_map *next; 11376c282eb4SLi Zefan bool ret = true; 11386c282eb4SLi Zefan 11396c282eb4SLi Zefan /* this is the last extent */ 11406c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 11416c282eb4SLi Zefan return false; 11426c282eb4SLi Zefan 11436c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1144e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1145e9512d72SChris Mason ret = false; 1146e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1147ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 11486c282eb4SLi Zefan ret = false; 11496c282eb4SLi Zefan 11506c282eb4SLi Zefan free_extent_map(next); 11516c282eb4SLi Zefan return ret; 11526c282eb4SLi Zefan } 11536c282eb4SLi Zefan 1154aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1155a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1156a43a2111SAndrew Mahone int compress) 11576c282eb4SLi Zefan { 11586c282eb4SLi Zefan struct extent_map *em; 11596c282eb4SLi Zefan int ret = 1; 11606c282eb4SLi Zefan bool next_mergeable = true; 11614a3560c4SLiu Bo bool prev_mergeable = true; 11626c282eb4SLi Zefan 11636c282eb4SLi Zefan /* 11646c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 11656c282eb4SLi Zefan * defragging it 11666c282eb4SLi Zefan */ 11676c282eb4SLi Zefan if (start < *defrag_end) 11686c282eb4SLi Zefan return 1; 11696c282eb4SLi Zefan 11706c282eb4SLi Zefan *skip = 0; 11716c282eb4SLi Zefan 11726c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 11736c282eb4SLi Zefan if (!em) 11746c282eb4SLi Zefan return 0; 11756c282eb4SLi Zefan 1176940100a4SChris Mason /* this will cover holes, and inline extents */ 117717ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1178940100a4SChris Mason ret = 0; 117917ce6ef8SLiu Bo goto out; 118017ce6ef8SLiu Bo } 118117ce6ef8SLiu Bo 11824a3560c4SLiu Bo if (!*defrag_end) 11834a3560c4SLiu Bo prev_mergeable = false; 11844a3560c4SLiu Bo 11856c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1186940100a4SChris Mason /* 11876c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 11886c282eb4SLi Zefan * real extent, don't bother defragging it 1189940100a4SChris Mason */ 1190a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 11914a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1192940100a4SChris Mason ret = 0; 119317ce6ef8SLiu Bo out: 1194940100a4SChris Mason /* 1195940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1196940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1197940100a4SChris Mason * so that the next tiny extent will force a defrag. 1198940100a4SChris Mason * 1199940100a4SChris Mason * The end result of this is that tiny extents before a single big 1200940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1201940100a4SChris Mason */ 1202940100a4SChris Mason if (ret) { 1203940100a4SChris Mason *defrag_end = extent_map_end(em); 1204940100a4SChris Mason } else { 1205940100a4SChris Mason *last_len = 0; 1206940100a4SChris Mason *skip = extent_map_end(em); 1207940100a4SChris Mason *defrag_end = 0; 1208940100a4SChris Mason } 1209940100a4SChris Mason 1210940100a4SChris Mason free_extent_map(em); 1211940100a4SChris Mason return ret; 1212940100a4SChris Mason } 1213940100a4SChris Mason 12144cb5300bSChris Mason /* 12154cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 12164cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 12174cb5300bSChris Mason * to COW and defrag. 12184cb5300bSChris Mason * 12194cb5300bSChris Mason * It also makes sure the delalloc code has enough 12204cb5300bSChris Mason * dirty data to avoid making new small extents as part 12214cb5300bSChris Mason * of the defrag 12224cb5300bSChris Mason * 12234cb5300bSChris Mason * It's a good idea to start RA on this range 12244cb5300bSChris Mason * before calling this. 12254cb5300bSChris Mason */ 12264cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 12274cb5300bSChris Mason struct page **pages, 12284cb5300bSChris Mason unsigned long start_index, 1229c41570c9SJustin Maggard unsigned long num_pages) 1230f46b5a66SChristoph Hellwig { 12314cb5300bSChris Mason unsigned long file_end; 12324cb5300bSChris Mason u64 isize = i_size_read(inode); 1233f46b5a66SChristoph Hellwig u64 page_start; 1234f46b5a66SChristoph Hellwig u64 page_end; 12351f12bd06SLiu Bo u64 page_cnt; 12364cb5300bSChris Mason int ret; 12374cb5300bSChris Mason int i; 12384cb5300bSChris Mason int i_done; 12394cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 12404cb5300bSChris Mason struct extent_state *cached_state = NULL; 1241600a45e1SMiao Xie struct extent_io_tree *tree; 1242364ecf36SQu Wenruo struct extent_changeset *data_reserved = NULL; 12433b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 12444cb5300bSChris Mason 124509cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 12461f12bd06SLiu Bo if (!isize || start_index > file_end) 12471f12bd06SLiu Bo return 0; 12481f12bd06SLiu Bo 12491f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 12504cb5300bSChris Mason 1251364ecf36SQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 125209cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 125309cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12544cb5300bSChris Mason if (ret) 12554cb5300bSChris Mason return ret; 12564cb5300bSChris Mason i_done = 0; 1257600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 12584cb5300bSChris Mason 12594cb5300bSChris Mason /* step one, lock all the pages */ 12601f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 12614cb5300bSChris Mason struct page *page; 1262600a45e1SMiao Xie again: 1263a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 12643b16a4e3SJosef Bacik start_index + i, mask); 12654cb5300bSChris Mason if (!page) 12664cb5300bSChris Mason break; 12674cb5300bSChris Mason 1268600a45e1SMiao Xie page_start = page_offset(page); 126909cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1270600a45e1SMiao Xie while (1) { 1271308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1272ff13db41SDavid Sterba &cached_state); 1273600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1274600a45e1SMiao Xie page_start); 1275308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1276e43bbe5eSDavid Sterba &cached_state); 1277600a45e1SMiao Xie if (!ordered) 1278600a45e1SMiao Xie break; 1279600a45e1SMiao Xie 1280600a45e1SMiao Xie unlock_page(page); 1281600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1282600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1283600a45e1SMiao Xie lock_page(page); 12841f12bd06SLiu Bo /* 12851f12bd06SLiu Bo * we unlocked the page above, so we need check if 12861f12bd06SLiu Bo * it was released or not. 12871f12bd06SLiu Bo */ 12881f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 12891f12bd06SLiu Bo unlock_page(page); 129009cbfeafSKirill A. Shutemov put_page(page); 12911f12bd06SLiu Bo goto again; 12921f12bd06SLiu Bo } 1293600a45e1SMiao Xie } 1294600a45e1SMiao Xie 12954cb5300bSChris Mason if (!PageUptodate(page)) { 12964cb5300bSChris Mason btrfs_readpage(NULL, page); 12974cb5300bSChris Mason lock_page(page); 12984cb5300bSChris Mason if (!PageUptodate(page)) { 12994cb5300bSChris Mason unlock_page(page); 130009cbfeafSKirill A. Shutemov put_page(page); 13014cb5300bSChris Mason ret = -EIO; 13024cb5300bSChris Mason break; 13034cb5300bSChris Mason } 13044cb5300bSChris Mason } 1305600a45e1SMiao Xie 1306600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1307600a45e1SMiao Xie unlock_page(page); 130809cbfeafSKirill A. Shutemov put_page(page); 1309600a45e1SMiao Xie goto again; 1310600a45e1SMiao Xie } 1311600a45e1SMiao Xie 13124cb5300bSChris Mason pages[i] = page; 13134cb5300bSChris Mason i_done++; 13144cb5300bSChris Mason } 13154cb5300bSChris Mason if (!i_done || ret) 13164cb5300bSChris Mason goto out; 13174cb5300bSChris Mason 13181751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 13194cb5300bSChris Mason goto out; 13204cb5300bSChris Mason 13214cb5300bSChris Mason /* 13224cb5300bSChris Mason * so now we have a nice long stream of locked 13234cb5300bSChris Mason * and up to date pages, lets wait on them 13244cb5300bSChris Mason */ 13254cb5300bSChris Mason for (i = 0; i < i_done; i++) 13264cb5300bSChris Mason wait_on_page_writeback(pages[i]); 13274cb5300bSChris Mason 13284cb5300bSChris Mason page_start = page_offset(pages[0]); 132909cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 13304cb5300bSChris Mason 13314cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1332ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 13334cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 13344cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 13359e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 1336ae0f1625SDavid Sterba &cached_state); 13374cb5300bSChris Mason 13381f12bd06SLiu Bo if (i_done != page_cnt) { 13399e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 134028c4a3e2SSu Yue btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); 13419e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 1342bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 134309cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 134443b18595SQu Wenruo (page_cnt - i_done) << PAGE_SHIFT, true); 13454cb5300bSChris Mason } 13464cb5300bSChris Mason 13474cb5300bSChris Mason 13489e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 1349018ed4f7SDavid Sterba &cached_state); 13504cb5300bSChris Mason 13514cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 1352e43bbe5eSDavid Sterba page_start, page_end - 1, &cached_state); 13534cb5300bSChris Mason 13544cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13554cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 13564cb5300bSChris Mason ClearPageChecked(pages[i]); 13574cb5300bSChris Mason set_page_extent_mapped(pages[i]); 13584cb5300bSChris Mason set_page_dirty(pages[i]); 13594cb5300bSChris Mason unlock_page(pages[i]); 136009cbfeafSKirill A. Shutemov put_page(pages[i]); 13614cb5300bSChris Mason } 136243b18595SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, 136343b18595SQu Wenruo false); 1364364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13654cb5300bSChris Mason return i_done; 13664cb5300bSChris Mason out: 13674cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13684cb5300bSChris Mason unlock_page(pages[i]); 136909cbfeafSKirill A. Shutemov put_page(pages[i]); 13704cb5300bSChris Mason } 1371bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 137209cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 137343b18595SQu Wenruo page_cnt << PAGE_SHIFT, true); 137443b18595SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, 137543b18595SQu Wenruo true); 1376364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13774cb5300bSChris Mason return ret; 13784cb5300bSChris Mason 13794cb5300bSChris Mason } 13804cb5300bSChris Mason 13814cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 13824cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 13834cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 13844cb5300bSChris Mason { 13850b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 13864cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 13874cb5300bSChris Mason struct file_ra_state *ra = NULL; 13884cb5300bSChris Mason unsigned long last_index; 1389151a31b2SLi Zefan u64 isize = i_size_read(inode); 1390940100a4SChris Mason u64 last_len = 0; 1391940100a4SChris Mason u64 skip = 0; 1392940100a4SChris Mason u64 defrag_end = 0; 13934cb5300bSChris Mason u64 newer_off = range->start; 1394f46b5a66SChristoph Hellwig unsigned long i; 1395008873eaSLi Zefan unsigned long ra_index = 0; 1396f46b5a66SChristoph Hellwig int ret; 13974cb5300bSChris Mason int defrag_count = 0; 13981a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1399aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 140009cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1401c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1402ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 14034cb5300bSChris Mason struct page **pages = NULL; 14041e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 14054cb5300bSChris Mason 14060abd5b17SLiu Bo if (isize == 0) 14070abd5b17SLiu Bo return 0; 14080abd5b17SLiu Bo 14090abd5b17SLiu Bo if (range->start >= isize) 14100abd5b17SLiu Bo return -EINVAL; 14111a419d85SLi Zefan 14121e2ef46dSDavid Sterba if (do_compress) { 14131a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 14141a419d85SLi Zefan return -EINVAL; 14151a419d85SLi Zefan if (range->compress_type) 14161a419d85SLi Zefan compress_type = range->compress_type; 14171a419d85SLi Zefan } 1418f46b5a66SChristoph Hellwig 14190abd5b17SLiu Bo if (extent_thresh == 0) 1420ee22184bSByongho Lee extent_thresh = SZ_256K; 1421f46b5a66SChristoph Hellwig 14224cb5300bSChris Mason /* 14230a52d108SDavid Sterba * If we were not given a file, allocate a readahead context. As 14240a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 14250a52d108SDavid Sterba * we don't error out. 14264cb5300bSChris Mason */ 14274cb5300bSChris Mason if (!file) { 142863e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 14290a52d108SDavid Sterba if (ra) 14304cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 14314cb5300bSChris Mason } else { 14324cb5300bSChris Mason ra = &file->f_ra; 14334cb5300bSChris Mason } 14344cb5300bSChris Mason 143563e727ecSDavid Sterba pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); 14364cb5300bSChris Mason if (!pages) { 14374cb5300bSChris Mason ret = -ENOMEM; 14384cb5300bSChris Mason goto out_ra; 14394cb5300bSChris Mason } 14404cb5300bSChris Mason 14414cb5300bSChris Mason /* find the last page to defrag */ 14421e701a32SChris Mason if (range->start + range->len > range->start) { 1443151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 144409cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 14451e701a32SChris Mason } else { 144609cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 14471e701a32SChris Mason } 14481e701a32SChris Mason 14494cb5300bSChris Mason if (newer_than) { 14504cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1451ee22184bSByongho Lee &newer_off, SZ_64K); 14524cb5300bSChris Mason if (!ret) { 14534cb5300bSChris Mason range->start = newer_off; 14544cb5300bSChris Mason /* 14554cb5300bSChris Mason * we always align our defrag to help keep 14564cb5300bSChris Mason * the extents in the file evenly spaced 14574cb5300bSChris Mason */ 145809cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14594cb5300bSChris Mason } else 14604cb5300bSChris Mason goto out_ra; 14614cb5300bSChris Mason } else { 146209cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 14634cb5300bSChris Mason } 14644cb5300bSChris Mason if (!max_to_defrag) 1465070034bdSchandan max_to_defrag = last_index - i + 1; 14664cb5300bSChris Mason 14672a0f7f57SLi Zefan /* 14682a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 14692a0f7f57SLi Zefan * written sequentially. 14702a0f7f57SLi Zefan */ 14712a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 14722a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 14732a0f7f57SLi Zefan 1474f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 147509cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 14764cb5300bSChris Mason /* 14774cb5300bSChris Mason * make sure we stop running if someone unmounts 14784cb5300bSChris Mason * the FS 14794cb5300bSChris Mason */ 14801751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 14814cb5300bSChris Mason break; 14824cb5300bSChris Mason 14830b246afaSJeff Mahoney if (btrfs_defrag_cancelled(fs_info)) { 14840b246afaSJeff Mahoney btrfs_debug(fs_info, "defrag_file cancelled"); 1485210549ebSDavid Sterba ret = -EAGAIN; 1486210549ebSDavid Sterba break; 1487210549ebSDavid Sterba } 1488210549ebSDavid Sterba 148909cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 14906c282eb4SLi Zefan extent_thresh, &last_len, &skip, 14911e2ef46dSDavid Sterba &defrag_end, do_compress)){ 1492940100a4SChris Mason unsigned long next; 1493940100a4SChris Mason /* 1494940100a4SChris Mason * the should_defrag function tells us how much to skip 1495940100a4SChris Mason * bump our counter by the suggested amount 1496940100a4SChris Mason */ 149709cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1498940100a4SChris Mason i = max(i + 1, next); 1499940100a4SChris Mason continue; 1500940100a4SChris Mason } 1501008873eaSLi Zefan 1502008873eaSLi Zefan if (!newer_than) { 150309cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 150409cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1505008873eaSLi Zefan cluster = min(cluster, max_cluster); 1506008873eaSLi Zefan } else { 1507008873eaSLi Zefan cluster = max_cluster; 1508008873eaSLi Zefan } 1509008873eaSLi Zefan 1510008873eaSLi Zefan if (i + cluster > ra_index) { 1511008873eaSLi Zefan ra_index = max(i, ra_index); 15120a52d108SDavid Sterba if (ra) 1513d3c0bab5SDavid Sterba page_cache_sync_readahead(inode->i_mapping, ra, 1514d3c0bab5SDavid Sterba file, ra_index, cluster); 1515e4826a5bSchandan ra_index += cluster; 1516008873eaSLi Zefan } 15174cb5300bSChris Mason 15185955102cSAl Viro inode_lock(inode); 1519eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 1520eede2bf3SOmar Sandoval ret = -ETXTBSY; 1521eede2bf3SOmar Sandoval } else { 15221e2ef46dSDavid Sterba if (do_compress) 1523eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 1524008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1525eede2bf3SOmar Sandoval } 1526ecb8bea8SLiu Bo if (ret < 0) { 15275955102cSAl Viro inode_unlock(inode); 15284cb5300bSChris Mason goto out_ra; 1529ecb8bea8SLiu Bo } 15304cb5300bSChris Mason 15314cb5300bSChris Mason defrag_count += ret; 1532d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 15335955102cSAl Viro inode_unlock(inode); 15344cb5300bSChris Mason 15354cb5300bSChris Mason if (newer_than) { 15364cb5300bSChris Mason if (newer_off == (u64)-1) 15374cb5300bSChris Mason break; 15384cb5300bSChris Mason 1539e1f041e1SLiu Bo if (ret > 0) 1540e1f041e1SLiu Bo i += ret; 1541e1f041e1SLiu Bo 15424cb5300bSChris Mason newer_off = max(newer_off + 1, 154309cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 15444cb5300bSChris Mason 1545ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1546ee22184bSByongho Lee &newer_off, SZ_64K); 15474cb5300bSChris Mason if (!ret) { 15484cb5300bSChris Mason range->start = newer_off; 154909cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 15504cb5300bSChris Mason } else { 15514cb5300bSChris Mason break; 1552940100a4SChris Mason } 15534cb5300bSChris Mason } else { 1554008873eaSLi Zefan if (ret > 0) { 1555cbcc8326SLi Zefan i += ret; 155609cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1557008873eaSLi Zefan } else { 1558940100a4SChris Mason i++; 1559008873eaSLi Zefan last_len = 0; 1560008873eaSLi Zefan } 1561f46b5a66SChristoph Hellwig } 15624cb5300bSChris Mason } 1563f46b5a66SChristoph Hellwig 1564dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 15651e701a32SChris Mason filemap_flush(inode->i_mapping); 1566dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1567dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1568dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1569dec8ef90SFilipe Manana } 15701e701a32SChris Mason 15711a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 15720b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 15735c1aab1dSNick Terrell } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { 15745c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 15751a419d85SLi Zefan } 15761a419d85SLi Zefan 157760ccf82fSDiego Calleja ret = defrag_count; 1578940100a4SChris Mason 15794cb5300bSChris Mason out_ra: 15801e2ef46dSDavid Sterba if (do_compress) { 15815955102cSAl Viro inode_lock(inode); 1582eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 15835955102cSAl Viro inode_unlock(inode); 1584633085c7SFilipe David Borba Manana } 15854cb5300bSChris Mason if (!file) 15864cb5300bSChris Mason kfree(ra); 15874cb5300bSChris Mason kfree(pages); 1588940100a4SChris Mason return ret; 1589f46b5a66SChristoph Hellwig } 1590f46b5a66SChristoph Hellwig 1591198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 159276dda93cSYan, Zheng void __user *arg) 1593f46b5a66SChristoph Hellwig { 15940b246afaSJeff Mahoney struct inode *inode = file_inode(file); 15950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1596f46b5a66SChristoph Hellwig u64 new_size; 1597f46b5a66SChristoph Hellwig u64 old_size; 1598f46b5a66SChristoph Hellwig u64 devid = 1; 15990b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1600f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1601f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1602f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1603f46b5a66SChristoph Hellwig char *sizestr; 16049a40f122SGui Hecheng char *retptr; 1605f46b5a66SChristoph Hellwig char *devstr = NULL; 1606f46b5a66SChristoph Hellwig int ret = 0; 1607f46b5a66SChristoph Hellwig int mod = 0; 1608f46b5a66SChristoph Hellwig 1609e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1610e441d54dSChris Mason return -EPERM; 1611e441d54dSChris Mason 1612198605a8SMiao Xie ret = mnt_want_write_file(file); 1613198605a8SMiao Xie if (ret) 1614198605a8SMiao Xie return ret; 1615198605a8SMiao Xie 1616171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 161797547676SMiao Xie mnt_drop_write_file(file); 1618e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1619c9e9f97bSIlya Dryomov } 1620c9e9f97bSIlya Dryomov 1621dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1622c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1623c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1624c9e9f97bSIlya Dryomov goto out; 1625c9e9f97bSIlya Dryomov } 16265516e595SMark Fasheh 16275516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1628f46b5a66SChristoph Hellwig 1629f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1630f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1631f46b5a66SChristoph Hellwig if (devstr) { 1632f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1633f46b5a66SChristoph Hellwig *devstr = '\0'; 1634f46b5a66SChristoph Hellwig devstr = vol_args->name; 163558dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 163658dfae63SZhangZhen if (ret) 163758dfae63SZhangZhen goto out_free; 1638dfd79829SMiao Xie if (!devid) { 1639dfd79829SMiao Xie ret = -EINVAL; 1640dfd79829SMiao Xie goto out_free; 1641dfd79829SMiao Xie } 16420b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1643f46b5a66SChristoph Hellwig } 1644dba60f3fSMiao Xie 16450b246afaSJeff Mahoney device = btrfs_find_device(fs_info, devid, NULL, NULL); 1646f46b5a66SChristoph Hellwig if (!device) { 16470b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1648c1c9ff7cSGeert Uytterhoeven devid); 1649dfd79829SMiao Xie ret = -ENODEV; 1650c9e9f97bSIlya Dryomov goto out_free; 1651f46b5a66SChristoph Hellwig } 1652dba60f3fSMiao Xie 1653ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 16540b246afaSJeff Mahoney btrfs_info(fs_info, 1655efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1656c1c9ff7cSGeert Uytterhoeven devid); 1657dfd79829SMiao Xie ret = -EPERM; 16584e42ae1bSLiu Bo goto out_free; 16594e42ae1bSLiu Bo } 16604e42ae1bSLiu Bo 1661f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1662f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1663f46b5a66SChristoph Hellwig else { 1664f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1665f46b5a66SChristoph Hellwig mod = -1; 1666f46b5a66SChristoph Hellwig sizestr++; 1667f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1668f46b5a66SChristoph Hellwig mod = 1; 1669f46b5a66SChristoph Hellwig sizestr++; 1670f46b5a66SChristoph Hellwig } 16719a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 16729a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1673f46b5a66SChristoph Hellwig ret = -EINVAL; 1674c9e9f97bSIlya Dryomov goto out_free; 1675f46b5a66SChristoph Hellwig } 1676f46b5a66SChristoph Hellwig } 1677f46b5a66SChristoph Hellwig 1678401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 1679dfd79829SMiao Xie ret = -EPERM; 168063a212abSStefan Behrens goto out_free; 168163a212abSStefan Behrens } 168263a212abSStefan Behrens 16837cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1684f46b5a66SChristoph Hellwig 1685f46b5a66SChristoph Hellwig if (mod < 0) { 1686f46b5a66SChristoph Hellwig if (new_size > old_size) { 1687f46b5a66SChristoph Hellwig ret = -EINVAL; 1688c9e9f97bSIlya Dryomov goto out_free; 1689f46b5a66SChristoph Hellwig } 1690f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1691f46b5a66SChristoph Hellwig } else if (mod > 0) { 1692eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1693902c68a4SGui Hecheng ret = -ERANGE; 1694eb8052e0SWenliang Fan goto out_free; 1695eb8052e0SWenliang Fan } 1696f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1697f46b5a66SChristoph Hellwig } 1698f46b5a66SChristoph Hellwig 1699ee22184bSByongho Lee if (new_size < SZ_256M) { 1700f46b5a66SChristoph Hellwig ret = -EINVAL; 1701c9e9f97bSIlya Dryomov goto out_free; 1702f46b5a66SChristoph Hellwig } 1703f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1704f46b5a66SChristoph Hellwig ret = -EFBIG; 1705c9e9f97bSIlya Dryomov goto out_free; 1706f46b5a66SChristoph Hellwig } 1707f46b5a66SChristoph Hellwig 170847f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1709f46b5a66SChristoph Hellwig 17100b246afaSJeff Mahoney btrfs_info_in_rcu(fs_info, "new size for %s is %llu", 1711c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1712f46b5a66SChristoph Hellwig 1713f46b5a66SChristoph Hellwig if (new_size > old_size) { 1714a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 171598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 171698d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1717c9e9f97bSIlya Dryomov goto out_free; 171898d5dc13STsutomu Itoh } 1719f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 17203a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1721ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1722f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 17230253f40eSjeff.liu } /* equal, nothing need to do */ 1724f46b5a66SChristoph Hellwig 1725c9e9f97bSIlya Dryomov out_free: 1726f46b5a66SChristoph Hellwig kfree(vol_args); 1727c9e9f97bSIlya Dryomov out: 1728171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 172918f39c41SIlya Dryomov mnt_drop_write_file(file); 1730f46b5a66SChristoph Hellwig return ret; 1731f46b5a66SChristoph Hellwig } 1732f46b5a66SChristoph Hellwig 173372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 173452f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 17356f72c7e2SArne Jansen u64 *transid, bool readonly, 17368696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1737f46b5a66SChristoph Hellwig { 1738f46b5a66SChristoph Hellwig int namelen; 17393de4586cSChris Mason int ret = 0; 1740f46b5a66SChristoph Hellwig 1741325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1742325c50e3SJeff Mahoney return -ENOTDIR; 1743325c50e3SJeff Mahoney 1744a874a63eSLiu Bo ret = mnt_want_write_file(file); 1745a874a63eSLiu Bo if (ret) 1746a874a63eSLiu Bo goto out; 1747a874a63eSLiu Bo 174872fd032eSSage Weil namelen = strlen(name); 174972fd032eSSage Weil if (strchr(name, '/')) { 1750f46b5a66SChristoph Hellwig ret = -EINVAL; 1751a874a63eSLiu Bo goto out_drop_write; 1752f46b5a66SChristoph Hellwig } 1753f46b5a66SChristoph Hellwig 175416780cabSChris Mason if (name[0] == '.' && 175516780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 175616780cabSChris Mason ret = -EEXIST; 1757a874a63eSLiu Bo goto out_drop_write; 175816780cabSChris Mason } 175916780cabSChris Mason 17603de4586cSChris Mason if (subvol) { 176172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 17626f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1763cb8e7090SChristoph Hellwig } else { 17642903ff01SAl Viro struct fd src = fdget(fd); 17653de4586cSChris Mason struct inode *src_inode; 17662903ff01SAl Viro if (!src.file) { 17673de4586cSChris Mason ret = -EINVAL; 1768a874a63eSLiu Bo goto out_drop_write; 17693de4586cSChris Mason } 17703de4586cSChris Mason 1771496ad9aaSAl Viro src_inode = file_inode(src.file); 1772496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1773c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1774efe120a0SFrank Holton "Snapshot src from another FS"); 177523ad5b17SKusanagi Kouichi ret = -EXDEV; 1776d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1777d0242061SDavid Sterba /* 1778d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1779d0242061SDavid Sterba * are limited to own subvolumes only 1780d0242061SDavid Sterba */ 1781d0242061SDavid Sterba ret = -EPERM; 1782ecd18815SAl Viro } else { 178372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 178472fd032eSSage Weil BTRFS_I(src_inode)->root, 17856f72c7e2SArne Jansen transid, readonly, inherit); 1786ecd18815SAl Viro } 17872903ff01SAl Viro fdput(src); 1788cb8e7090SChristoph Hellwig } 1789a874a63eSLiu Bo out_drop_write: 1790a874a63eSLiu Bo mnt_drop_write_file(file); 1791f46b5a66SChristoph Hellwig out: 179272fd032eSSage Weil return ret; 179372fd032eSSage Weil } 179472fd032eSSage Weil 179572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1796fa0d2b9bSLi Zefan void __user *arg, int subvol) 179772fd032eSSage Weil { 1798fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 179972fd032eSSage Weil int ret; 180072fd032eSSage Weil 1801325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1802325c50e3SJeff Mahoney return -ENOTDIR; 1803325c50e3SJeff Mahoney 1804fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1805fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1806fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1807fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1808fa0d2b9bSLi Zefan 1809fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1810b83cc969SLi Zefan vol_args->fd, subvol, 18116f72c7e2SArne Jansen NULL, false, NULL); 1812fa0d2b9bSLi Zefan 1813fa0d2b9bSLi Zefan kfree(vol_args); 1814fa0d2b9bSLi Zefan return ret; 1815fa0d2b9bSLi Zefan } 1816fa0d2b9bSLi Zefan 1817fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1818fa0d2b9bSLi Zefan void __user *arg, int subvol) 1819fa0d2b9bSLi Zefan { 1820fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1821fa0d2b9bSLi Zefan int ret; 1822fdfb1e4fSLi Zefan u64 transid = 0; 1823fdfb1e4fSLi Zefan u64 *ptr = NULL; 1824b83cc969SLi Zefan bool readonly = false; 18256f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 182672fd032eSSage Weil 1827325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1828325c50e3SJeff Mahoney return -ENOTDIR; 1829325c50e3SJeff Mahoney 1830fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1831fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1832fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1833fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1834fdfb1e4fSLi Zefan 1835b83cc969SLi Zefan if (vol_args->flags & 18366f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 18376f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1838b83cc969SLi Zefan ret = -EOPNOTSUPP; 1839c47ca32dSDan Carpenter goto free_args; 1840fdfb1e4fSLi Zefan } 1841fdfb1e4fSLi Zefan 1842fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1843fdfb1e4fSLi Zefan ptr = &transid; 1844b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1845b83cc969SLi Zefan readonly = true; 18466f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 184709cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 18486f72c7e2SArne Jansen ret = -EINVAL; 1849c47ca32dSDan Carpenter goto free_args; 18506f72c7e2SArne Jansen } 18516f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 18526f72c7e2SArne Jansen if (IS_ERR(inherit)) { 18536f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1854c47ca32dSDan Carpenter goto free_args; 18556f72c7e2SArne Jansen } 18566f72c7e2SArne Jansen } 185775eaa0e2SSage Weil 1858fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 18596f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 18608696c533SMiao Xie readonly, inherit); 1861c47ca32dSDan Carpenter if (ret) 1862c47ca32dSDan Carpenter goto free_inherit; 186375eaa0e2SSage Weil 1864c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1865fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1866c47ca32dSDan Carpenter transid), 1867c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 186875eaa0e2SSage Weil ret = -EFAULT; 1869c47ca32dSDan Carpenter 1870c47ca32dSDan Carpenter free_inherit: 18716f72c7e2SArne Jansen kfree(inherit); 1872c47ca32dSDan Carpenter free_args: 1873c47ca32dSDan Carpenter kfree(vol_args); 1874f46b5a66SChristoph Hellwig return ret; 1875f46b5a66SChristoph Hellwig } 1876f46b5a66SChristoph Hellwig 18770caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 18780caa102dSLi Zefan void __user *arg) 18790caa102dSLi Zefan { 1880496ad9aaSAl Viro struct inode *inode = file_inode(file); 18810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 18820caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 18830caa102dSLi Zefan int ret = 0; 18840caa102dSLi Zefan u64 flags = 0; 18850caa102dSLi Zefan 18864a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 18870caa102dSLi Zefan return -EINVAL; 18880caa102dSLi Zefan 18890b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 18900caa102dSLi Zefan if (btrfs_root_readonly(root)) 18910caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 18920b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 18930caa102dSLi Zefan 18940caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 18950caa102dSLi Zefan ret = -EFAULT; 18960caa102dSLi Zefan 18970caa102dSLi Zefan return ret; 18980caa102dSLi Zefan } 18990caa102dSLi Zefan 19000caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 19010caa102dSLi Zefan void __user *arg) 19020caa102dSLi Zefan { 1903496ad9aaSAl Viro struct inode *inode = file_inode(file); 19040b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 19050caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19060caa102dSLi Zefan struct btrfs_trans_handle *trans; 19070caa102dSLi Zefan u64 root_flags; 19080caa102dSLi Zefan u64 flags; 19090caa102dSLi Zefan int ret = 0; 19100caa102dSLi Zefan 1911bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1912bd60ea0fSDavid Sterba return -EPERM; 1913bd60ea0fSDavid Sterba 1914b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1915b9ca0664SLiu Bo if (ret) 1916b9ca0664SLiu Bo goto out; 19170caa102dSLi Zefan 19184a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1919b9ca0664SLiu Bo ret = -EINVAL; 1920b9ca0664SLiu Bo goto out_drop_write; 1921b9ca0664SLiu Bo } 19220caa102dSLi Zefan 1923b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1924b9ca0664SLiu Bo ret = -EFAULT; 1925b9ca0664SLiu Bo goto out_drop_write; 1926b9ca0664SLiu Bo } 19270caa102dSLi Zefan 1928b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1929b9ca0664SLiu Bo ret = -EINVAL; 1930b9ca0664SLiu Bo goto out_drop_write; 1931b9ca0664SLiu Bo } 19320caa102dSLi Zefan 1933b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1934b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1935b9ca0664SLiu Bo goto out_drop_write; 1936b9ca0664SLiu Bo } 19370caa102dSLi Zefan 19380b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 19390caa102dSLi Zefan 19400caa102dSLi Zefan /* nothing to do */ 19410caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1942b9ca0664SLiu Bo goto out_drop_sem; 19430caa102dSLi Zefan 19440caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 19452c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 19460caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19470caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 19482c686537SDavid Sterba } else { 19492c686537SDavid Sterba /* 19502c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 19512c686537SDavid Sterba * send 19522c686537SDavid Sterba */ 19532c686537SDavid Sterba spin_lock(&root->root_item_lock); 19542c686537SDavid Sterba if (root->send_in_progress == 0) { 19550caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19560caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 19572c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19582c686537SDavid Sterba } else { 19592c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19600b246afaSJeff Mahoney btrfs_warn(fs_info, 19612c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 19622c686537SDavid Sterba root->root_key.objectid); 19632c686537SDavid Sterba ret = -EPERM; 19642c686537SDavid Sterba goto out_drop_sem; 19652c686537SDavid Sterba } 19662c686537SDavid Sterba } 19670caa102dSLi Zefan 19680caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 19690caa102dSLi Zefan if (IS_ERR(trans)) { 19700caa102dSLi Zefan ret = PTR_ERR(trans); 19710caa102dSLi Zefan goto out_reset; 19720caa102dSLi Zefan } 19730caa102dSLi Zefan 19740b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 19750caa102dSLi Zefan &root->root_key, &root->root_item); 19769417ebc8SNikolay Borisov if (ret < 0) { 19779417ebc8SNikolay Borisov btrfs_end_transaction(trans); 19789417ebc8SNikolay Borisov goto out_reset; 19799417ebc8SNikolay Borisov } 19800caa102dSLi Zefan 19819417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 19829417ebc8SNikolay Borisov 19830caa102dSLi Zefan out_reset: 19840caa102dSLi Zefan if (ret) 19850caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1986b9ca0664SLiu Bo out_drop_sem: 19870b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 1988b9ca0664SLiu Bo out_drop_write: 1989b9ca0664SLiu Bo mnt_drop_write_file(file); 1990b9ca0664SLiu Bo out: 19910caa102dSLi Zefan return ret; 19920caa102dSLi Zefan } 19930caa102dSLi Zefan 1994ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1995ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1996ac8e9819SChris Mason { 1997abc6e134SChris Mason struct btrfs_key test; 1998abc6e134SChris Mason int ret; 1999abc6e134SChris Mason 2000abc6e134SChris Mason test.objectid = sk->min_objectid; 2001abc6e134SChris Mason test.type = sk->min_type; 2002abc6e134SChris Mason test.offset = sk->min_offset; 2003abc6e134SChris Mason 2004abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2005abc6e134SChris Mason if (ret < 0) 2006ac8e9819SChris Mason return 0; 2007abc6e134SChris Mason 2008abc6e134SChris Mason test.objectid = sk->max_objectid; 2009abc6e134SChris Mason test.type = sk->max_type; 2010abc6e134SChris Mason test.offset = sk->max_offset; 2011abc6e134SChris Mason 2012abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2013abc6e134SChris Mason if (ret > 0) 2014ac8e9819SChris Mason return 0; 2015ac8e9819SChris Mason return 1; 2016ac8e9819SChris Mason } 2017ac8e9819SChris Mason 2018df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 2019ac8e9819SChris Mason struct btrfs_key *key, 2020ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 20219b6e817dSGerhard Heift size_t *buf_size, 2022ba346b35SGerhard Heift char __user *ubuf, 2023ac8e9819SChris Mason unsigned long *sk_offset, 2024ac8e9819SChris Mason int *num_found) 2025ac8e9819SChris Mason { 2026ac8e9819SChris Mason u64 found_transid; 2027ac8e9819SChris Mason struct extent_buffer *leaf; 2028ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 2029dd81d459SNaohiro Aota struct btrfs_key test; 2030ac8e9819SChris Mason unsigned long item_off; 2031ac8e9819SChris Mason unsigned long item_len; 2032ac8e9819SChris Mason int nritems; 2033ac8e9819SChris Mason int i; 2034ac8e9819SChris Mason int slot; 2035ac8e9819SChris Mason int ret = 0; 2036ac8e9819SChris Mason 2037ac8e9819SChris Mason leaf = path->nodes[0]; 2038ac8e9819SChris Mason slot = path->slots[0]; 2039ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 2040ac8e9819SChris Mason 2041ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 2042ac8e9819SChris Mason i = nritems; 2043ac8e9819SChris Mason goto advance_key; 2044ac8e9819SChris Mason } 2045ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 2046ac8e9819SChris Mason 2047ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 2048ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 2049ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 2050ac8e9819SChris Mason 205103b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 205203b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 205303b71c6cSGabriel de Perthuis continue; 205403b71c6cSGabriel de Perthuis 20559b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 20568f5f6178SGerhard Heift if (*num_found) { 2057ac8e9819SChris Mason ret = 1; 20588f5f6178SGerhard Heift goto out; 20598f5f6178SGerhard Heift } 20608f5f6178SGerhard Heift 20618f5f6178SGerhard Heift /* 20628f5f6178SGerhard Heift * return one empty item back for v1, which does not 20638f5f6178SGerhard Heift * handle -EOVERFLOW 20648f5f6178SGerhard Heift */ 20658f5f6178SGerhard Heift 20669b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2067ac8e9819SChris Mason item_len = 0; 20688f5f6178SGerhard Heift ret = -EOVERFLOW; 20698f5f6178SGerhard Heift } 2070ac8e9819SChris Mason 20719b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2072ac8e9819SChris Mason ret = 1; 207325c9bc2eSGerhard Heift goto out; 2074ac8e9819SChris Mason } 2075ac8e9819SChris Mason 2076ac8e9819SChris Mason sh.objectid = key->objectid; 2077ac8e9819SChris Mason sh.offset = key->offset; 2078ac8e9819SChris Mason sh.type = key->type; 2079ac8e9819SChris Mason sh.len = item_len; 2080ac8e9819SChris Mason sh.transid = found_transid; 2081ac8e9819SChris Mason 2082ac8e9819SChris Mason /* copy search result header */ 2083ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 2084ba346b35SGerhard Heift ret = -EFAULT; 2085ba346b35SGerhard Heift goto out; 2086ba346b35SGerhard Heift } 2087ba346b35SGerhard Heift 2088ac8e9819SChris Mason *sk_offset += sizeof(sh); 2089ac8e9819SChris Mason 2090ac8e9819SChris Mason if (item_len) { 2091ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2092ac8e9819SChris Mason /* copy the item */ 2093ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2094ba346b35SGerhard Heift item_off, item_len)) { 2095ba346b35SGerhard Heift ret = -EFAULT; 2096ba346b35SGerhard Heift goto out; 2097ba346b35SGerhard Heift } 2098ba346b35SGerhard Heift 2099ac8e9819SChris Mason *sk_offset += item_len; 2100ac8e9819SChris Mason } 2101e2156867SHugo Mills (*num_found)++; 2102ac8e9819SChris Mason 21038f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 21048f5f6178SGerhard Heift goto out; 21058f5f6178SGerhard Heift 210625c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 210725c9bc2eSGerhard Heift ret = 1; 210825c9bc2eSGerhard Heift goto out; 210925c9bc2eSGerhard Heift } 2110ac8e9819SChris Mason } 2111ac8e9819SChris Mason advance_key: 2112ac8e9819SChris Mason ret = 0; 2113dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2114dd81d459SNaohiro Aota test.type = sk->max_type; 2115dd81d459SNaohiro Aota test.offset = sk->max_offset; 2116dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2117dd81d459SNaohiro Aota ret = 1; 2118dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2119abc6e134SChris Mason key->offset++; 2120dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2121abc6e134SChris Mason key->offset = 0; 2122abc6e134SChris Mason key->type++; 2123dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2124abc6e134SChris Mason key->offset = 0; 2125abc6e134SChris Mason key->type = 0; 2126abc6e134SChris Mason key->objectid++; 2127abc6e134SChris Mason } else 2128abc6e134SChris Mason ret = 1; 212925c9bc2eSGerhard Heift out: 2130ba346b35SGerhard Heift /* 2131ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2132ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2133ba346b35SGerhard Heift * * all items were found 2134ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2135ba346b35SGerhard Heift * leaf 2136ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2137ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2138ba346b35SGerhard Heift */ 2139ac8e9819SChris Mason return ret; 2140ac8e9819SChris Mason } 2141ac8e9819SChris Mason 2142ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 214312544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 21449b6e817dSGerhard Heift size_t *buf_size, 2145ba346b35SGerhard Heift char __user *ubuf) 2146ac8e9819SChris Mason { 21470b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2148ac8e9819SChris Mason struct btrfs_root *root; 2149ac8e9819SChris Mason struct btrfs_key key; 2150ac8e9819SChris Mason struct btrfs_path *path; 2151ac8e9819SChris Mason int ret; 2152ac8e9819SChris Mason int num_found = 0; 2153ac8e9819SChris Mason unsigned long sk_offset = 0; 2154ac8e9819SChris Mason 21559b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 21569b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 215712544442SGerhard Heift return -EOVERFLOW; 21589b6e817dSGerhard Heift } 215912544442SGerhard Heift 2160ac8e9819SChris Mason path = btrfs_alloc_path(); 2161ac8e9819SChris Mason if (!path) 2162ac8e9819SChris Mason return -ENOMEM; 2163ac8e9819SChris Mason 2164ac8e9819SChris Mason if (sk->tree_id == 0) { 2165ac8e9819SChris Mason /* search the root of the inode that was passed */ 2166ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2167ac8e9819SChris Mason } else { 2168ac8e9819SChris Mason key.objectid = sk->tree_id; 2169ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2170ac8e9819SChris Mason key.offset = (u64)-1; 2171ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2172ac8e9819SChris Mason if (IS_ERR(root)) { 2173ac8e9819SChris Mason btrfs_free_path(path); 2174ad1e3d56SMisono Tomohiro return PTR_ERR(root); 2175ac8e9819SChris Mason } 2176ac8e9819SChris Mason } 2177ac8e9819SChris Mason 2178ac8e9819SChris Mason key.objectid = sk->min_objectid; 2179ac8e9819SChris Mason key.type = sk->min_type; 2180ac8e9819SChris Mason key.offset = sk->min_offset; 2181ac8e9819SChris Mason 2182ac8e9819SChris Mason while (1) { 21836174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2184ac8e9819SChris Mason if (ret != 0) { 2185ac8e9819SChris Mason if (ret > 0) 2186ac8e9819SChris Mason ret = 0; 2187ac8e9819SChris Mason goto err; 2188ac8e9819SChris Mason } 2189df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2190ac8e9819SChris Mason &sk_offset, &num_found); 2191b3b4aa74SDavid Sterba btrfs_release_path(path); 219225c9bc2eSGerhard Heift if (ret) 2193ac8e9819SChris Mason break; 2194ac8e9819SChris Mason 2195ac8e9819SChris Mason } 21968f5f6178SGerhard Heift if (ret > 0) 2197ac8e9819SChris Mason ret = 0; 2198ac8e9819SChris Mason err: 2199ac8e9819SChris Mason sk->nr_items = num_found; 2200ac8e9819SChris Mason btrfs_free_path(path); 2201ac8e9819SChris Mason return ret; 2202ac8e9819SChris Mason } 2203ac8e9819SChris Mason 2204ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2205ac8e9819SChris Mason void __user *argp) 2206ac8e9819SChris Mason { 2207ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2208ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2209ac8e9819SChris Mason struct inode *inode; 2210ac8e9819SChris Mason int ret; 22119b6e817dSGerhard Heift size_t buf_size; 2212ac8e9819SChris Mason 2213ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2214ac8e9819SChris Mason return -EPERM; 2215ac8e9819SChris Mason 2216ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2217ac8e9819SChris Mason 2218ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2219ba346b35SGerhard Heift return -EFAULT; 2220ba346b35SGerhard Heift 2221ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2222ac8e9819SChris Mason 2223496ad9aaSAl Viro inode = file_inode(file); 2224ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 22258f5f6178SGerhard Heift 22268f5f6178SGerhard Heift /* 22278f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 22288f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 22298f5f6178SGerhard Heift */ 22308f5f6178SGerhard Heift if (ret == -EOVERFLOW) 22318f5f6178SGerhard Heift ret = 0; 22328f5f6178SGerhard Heift 2233ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2234ac8e9819SChris Mason ret = -EFAULT; 2235ac8e9819SChris Mason return ret; 2236ac8e9819SChris Mason } 2237ac8e9819SChris Mason 2238cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2239cc68a8a5SGerhard Heift void __user *argp) 2240cc68a8a5SGerhard Heift { 2241cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2242cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2243cc68a8a5SGerhard Heift struct inode *inode; 2244cc68a8a5SGerhard Heift int ret; 2245cc68a8a5SGerhard Heift size_t buf_size; 2246ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2247cc68a8a5SGerhard Heift 2248cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2249cc68a8a5SGerhard Heift return -EPERM; 2250cc68a8a5SGerhard Heift 2251cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2252cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2253cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2254cc68a8a5SGerhard Heift return -EFAULT; 2255cc68a8a5SGerhard Heift 2256cc68a8a5SGerhard Heift buf_size = args.buf_size; 2257cc68a8a5SGerhard Heift 2258cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2259cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2260cc68a8a5SGerhard Heift buf_size = buf_limit; 2261cc68a8a5SGerhard Heift 2262cc68a8a5SGerhard Heift inode = file_inode(file); 2263cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2264718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2265cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2266cc68a8a5SGerhard Heift ret = -EFAULT; 2267cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2268cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2269cc68a8a5SGerhard Heift ret = -EFAULT; 2270cc68a8a5SGerhard Heift 227176dda93cSYan, Zheng return ret; 227276dda93cSYan, Zheng } 227376dda93cSYan, Zheng 227498d377a0STARUISI Hiroaki /* 2275ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2276ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 227798d377a0STARUISI Hiroaki */ 227898d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 227998d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 228098d377a0STARUISI Hiroaki { 228198d377a0STARUISI Hiroaki struct btrfs_root *root; 228298d377a0STARUISI Hiroaki struct btrfs_key key; 2283ac8e9819SChris Mason char *ptr; 228498d377a0STARUISI Hiroaki int ret = -1; 228598d377a0STARUISI Hiroaki int slot; 228698d377a0STARUISI Hiroaki int len; 228798d377a0STARUISI Hiroaki int total_len = 0; 228898d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 228998d377a0STARUISI Hiroaki struct extent_buffer *l; 229098d377a0STARUISI Hiroaki struct btrfs_path *path; 229198d377a0STARUISI Hiroaki 229298d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 229398d377a0STARUISI Hiroaki name[0]='\0'; 229498d377a0STARUISI Hiroaki return 0; 229598d377a0STARUISI Hiroaki } 229698d377a0STARUISI Hiroaki 229798d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 229898d377a0STARUISI Hiroaki if (!path) 229998d377a0STARUISI Hiroaki return -ENOMEM; 230098d377a0STARUISI Hiroaki 2301c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 230298d377a0STARUISI Hiroaki 230398d377a0STARUISI Hiroaki key.objectid = tree_id; 230498d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 230598d377a0STARUISI Hiroaki key.offset = (u64)-1; 230698d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 230798d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2308ad1e3d56SMisono Tomohiro ret = PTR_ERR(root); 23098ad6fcabSChris Mason goto out; 231098d377a0STARUISI Hiroaki } 231198d377a0STARUISI Hiroaki 231298d377a0STARUISI Hiroaki key.objectid = dirid; 231398d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 23148ad6fcabSChris Mason key.offset = (u64)-1; 231598d377a0STARUISI Hiroaki 231698d377a0STARUISI Hiroaki while (1) { 231798d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 231898d377a0STARUISI Hiroaki if (ret < 0) 231998d377a0STARUISI Hiroaki goto out; 232018674c6cSFilipe David Borba Manana else if (ret > 0) { 232118674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 232218674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 232318674c6cSFilipe David Borba Manana if (ret < 0) 232418674c6cSFilipe David Borba Manana goto out; 232518674c6cSFilipe David Borba Manana else if (ret > 0) { 2326ac8e9819SChris Mason ret = -ENOENT; 232798d377a0STARUISI Hiroaki goto out; 2328ac8e9819SChris Mason } 232918674c6cSFilipe David Borba Manana } 233018674c6cSFilipe David Borba Manana 233118674c6cSFilipe David Borba Manana l = path->nodes[0]; 233218674c6cSFilipe David Borba Manana slot = path->slots[0]; 233318674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 233498d377a0STARUISI Hiroaki 233598d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 233698d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 233798d377a0STARUISI Hiroaki ptr -= len + 1; 233898d377a0STARUISI Hiroaki total_len += len + 1; 2339a696cf35SFilipe David Borba Manana if (ptr < name) { 2340a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 234198d377a0STARUISI Hiroaki goto out; 2342a696cf35SFilipe David Borba Manana } 234398d377a0STARUISI Hiroaki 234498d377a0STARUISI Hiroaki *(ptr + len) = '/'; 234598d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 234698d377a0STARUISI Hiroaki 234798d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 234898d377a0STARUISI Hiroaki break; 234998d377a0STARUISI Hiroaki 2350b3b4aa74SDavid Sterba btrfs_release_path(path); 235198d377a0STARUISI Hiroaki key.objectid = key.offset; 23528ad6fcabSChris Mason key.offset = (u64)-1; 235398d377a0STARUISI Hiroaki dirid = key.objectid; 235498d377a0STARUISI Hiroaki } 235577906a50SLi Zefan memmove(name, ptr, total_len); 235698d377a0STARUISI Hiroaki name[total_len] = '\0'; 235798d377a0STARUISI Hiroaki ret = 0; 235898d377a0STARUISI Hiroaki out: 235998d377a0STARUISI Hiroaki btrfs_free_path(path); 2360ac8e9819SChris Mason return ret; 2361ac8e9819SChris Mason } 2362ac8e9819SChris Mason 236323d0b79dSTomohiro Misono static int btrfs_search_path_in_tree_user(struct inode *inode, 236423d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args) 236523d0b79dSTomohiro Misono { 236623d0b79dSTomohiro Misono struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 236723d0b79dSTomohiro Misono struct super_block *sb = inode->i_sb; 236823d0b79dSTomohiro Misono struct btrfs_key upper_limit = BTRFS_I(inode)->location; 236923d0b79dSTomohiro Misono u64 treeid = BTRFS_I(inode)->root->root_key.objectid; 237023d0b79dSTomohiro Misono u64 dirid = args->dirid; 237123d0b79dSTomohiro Misono unsigned long item_off; 237223d0b79dSTomohiro Misono unsigned long item_len; 237323d0b79dSTomohiro Misono struct btrfs_inode_ref *iref; 237423d0b79dSTomohiro Misono struct btrfs_root_ref *rref; 237523d0b79dSTomohiro Misono struct btrfs_root *root; 237623d0b79dSTomohiro Misono struct btrfs_path *path; 237723d0b79dSTomohiro Misono struct btrfs_key key, key2; 237823d0b79dSTomohiro Misono struct extent_buffer *leaf; 237923d0b79dSTomohiro Misono struct inode *temp_inode; 238023d0b79dSTomohiro Misono char *ptr; 238123d0b79dSTomohiro Misono int slot; 238223d0b79dSTomohiro Misono int len; 238323d0b79dSTomohiro Misono int total_len = 0; 238423d0b79dSTomohiro Misono int ret; 238523d0b79dSTomohiro Misono 238623d0b79dSTomohiro Misono path = btrfs_alloc_path(); 238723d0b79dSTomohiro Misono if (!path) 238823d0b79dSTomohiro Misono return -ENOMEM; 238923d0b79dSTomohiro Misono 239023d0b79dSTomohiro Misono /* 239123d0b79dSTomohiro Misono * If the bottom subvolume does not exist directly under upper_limit, 239223d0b79dSTomohiro Misono * construct the path in from the bottom up. 239323d0b79dSTomohiro Misono */ 239423d0b79dSTomohiro Misono if (dirid != upper_limit.objectid) { 239523d0b79dSTomohiro Misono ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; 239623d0b79dSTomohiro Misono 239723d0b79dSTomohiro Misono key.objectid = treeid; 239823d0b79dSTomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 239923d0b79dSTomohiro Misono key.offset = (u64)-1; 240023d0b79dSTomohiro Misono root = btrfs_read_fs_root_no_name(fs_info, &key); 240123d0b79dSTomohiro Misono if (IS_ERR(root)) { 240223d0b79dSTomohiro Misono ret = PTR_ERR(root); 240323d0b79dSTomohiro Misono goto out; 240423d0b79dSTomohiro Misono } 240523d0b79dSTomohiro Misono 240623d0b79dSTomohiro Misono key.objectid = dirid; 240723d0b79dSTomohiro Misono key.type = BTRFS_INODE_REF_KEY; 240823d0b79dSTomohiro Misono key.offset = (u64)-1; 240923d0b79dSTomohiro Misono while (1) { 241023d0b79dSTomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 241123d0b79dSTomohiro Misono if (ret < 0) { 241223d0b79dSTomohiro Misono goto out; 241323d0b79dSTomohiro Misono } else if (ret > 0) { 241423d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 241523d0b79dSTomohiro Misono BTRFS_INODE_REF_KEY); 241623d0b79dSTomohiro Misono if (ret < 0) { 241723d0b79dSTomohiro Misono goto out; 241823d0b79dSTomohiro Misono } else if (ret > 0) { 241923d0b79dSTomohiro Misono ret = -ENOENT; 242023d0b79dSTomohiro Misono goto out; 242123d0b79dSTomohiro Misono } 242223d0b79dSTomohiro Misono } 242323d0b79dSTomohiro Misono 242423d0b79dSTomohiro Misono leaf = path->nodes[0]; 242523d0b79dSTomohiro Misono slot = path->slots[0]; 242623d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 242723d0b79dSTomohiro Misono 242823d0b79dSTomohiro Misono iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); 242923d0b79dSTomohiro Misono len = btrfs_inode_ref_name_len(leaf, iref); 243023d0b79dSTomohiro Misono ptr -= len + 1; 243123d0b79dSTomohiro Misono total_len += len + 1; 243223d0b79dSTomohiro Misono if (ptr < args->path) { 243323d0b79dSTomohiro Misono ret = -ENAMETOOLONG; 243423d0b79dSTomohiro Misono goto out; 243523d0b79dSTomohiro Misono } 243623d0b79dSTomohiro Misono 243723d0b79dSTomohiro Misono *(ptr + len) = '/'; 243823d0b79dSTomohiro Misono read_extent_buffer(leaf, ptr, 243923d0b79dSTomohiro Misono (unsigned long)(iref + 1), len); 244023d0b79dSTomohiro Misono 244123d0b79dSTomohiro Misono /* Check the read+exec permission of this directory */ 244223d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 244323d0b79dSTomohiro Misono BTRFS_INODE_ITEM_KEY); 244423d0b79dSTomohiro Misono if (ret < 0) { 244523d0b79dSTomohiro Misono goto out; 244623d0b79dSTomohiro Misono } else if (ret > 0) { 244723d0b79dSTomohiro Misono ret = -ENOENT; 244823d0b79dSTomohiro Misono goto out; 244923d0b79dSTomohiro Misono } 245023d0b79dSTomohiro Misono 245123d0b79dSTomohiro Misono leaf = path->nodes[0]; 245223d0b79dSTomohiro Misono slot = path->slots[0]; 245323d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key2, slot); 245423d0b79dSTomohiro Misono if (key2.objectid != dirid) { 245523d0b79dSTomohiro Misono ret = -ENOENT; 245623d0b79dSTomohiro Misono goto out; 245723d0b79dSTomohiro Misono } 245823d0b79dSTomohiro Misono 245923d0b79dSTomohiro Misono temp_inode = btrfs_iget(sb, &key2, root, NULL); 24603ca57bd6SMisono Tomohiro if (IS_ERR(temp_inode)) { 24613ca57bd6SMisono Tomohiro ret = PTR_ERR(temp_inode); 24623ca57bd6SMisono Tomohiro goto out; 24633ca57bd6SMisono Tomohiro } 246423d0b79dSTomohiro Misono ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); 246523d0b79dSTomohiro Misono iput(temp_inode); 246623d0b79dSTomohiro Misono if (ret) { 246723d0b79dSTomohiro Misono ret = -EACCES; 246823d0b79dSTomohiro Misono goto out; 246923d0b79dSTomohiro Misono } 247023d0b79dSTomohiro Misono 247123d0b79dSTomohiro Misono if (key.offset == upper_limit.objectid) 247223d0b79dSTomohiro Misono break; 247323d0b79dSTomohiro Misono if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { 247423d0b79dSTomohiro Misono ret = -EACCES; 247523d0b79dSTomohiro Misono goto out; 247623d0b79dSTomohiro Misono } 247723d0b79dSTomohiro Misono 247823d0b79dSTomohiro Misono btrfs_release_path(path); 247923d0b79dSTomohiro Misono key.objectid = key.offset; 248023d0b79dSTomohiro Misono key.offset = (u64)-1; 248123d0b79dSTomohiro Misono dirid = key.objectid; 248223d0b79dSTomohiro Misono } 248323d0b79dSTomohiro Misono 248423d0b79dSTomohiro Misono memmove(args->path, ptr, total_len); 248523d0b79dSTomohiro Misono args->path[total_len] = '\0'; 248623d0b79dSTomohiro Misono btrfs_release_path(path); 248723d0b79dSTomohiro Misono } 248823d0b79dSTomohiro Misono 248923d0b79dSTomohiro Misono /* Get the bottom subvolume's name from ROOT_REF */ 249023d0b79dSTomohiro Misono root = fs_info->tree_root; 249123d0b79dSTomohiro Misono key.objectid = treeid; 249223d0b79dSTomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 249323d0b79dSTomohiro Misono key.offset = args->treeid; 249423d0b79dSTomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 249523d0b79dSTomohiro Misono if (ret < 0) { 249623d0b79dSTomohiro Misono goto out; 249723d0b79dSTomohiro Misono } else if (ret > 0) { 249823d0b79dSTomohiro Misono ret = -ENOENT; 249923d0b79dSTomohiro Misono goto out; 250023d0b79dSTomohiro Misono } 250123d0b79dSTomohiro Misono 250223d0b79dSTomohiro Misono leaf = path->nodes[0]; 250323d0b79dSTomohiro Misono slot = path->slots[0]; 250423d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 250523d0b79dSTomohiro Misono 250623d0b79dSTomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot); 250723d0b79dSTomohiro Misono item_len = btrfs_item_size_nr(leaf, slot); 250823d0b79dSTomohiro Misono /* Check if dirid in ROOT_REF corresponds to passed dirid */ 250923d0b79dSTomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 251023d0b79dSTomohiro Misono if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { 251123d0b79dSTomohiro Misono ret = -EINVAL; 251223d0b79dSTomohiro Misono goto out; 251323d0b79dSTomohiro Misono } 251423d0b79dSTomohiro Misono 251523d0b79dSTomohiro Misono /* Copy subvolume's name */ 251623d0b79dSTomohiro Misono item_off += sizeof(struct btrfs_root_ref); 251723d0b79dSTomohiro Misono item_len -= sizeof(struct btrfs_root_ref); 251823d0b79dSTomohiro Misono read_extent_buffer(leaf, args->name, item_off, item_len); 251923d0b79dSTomohiro Misono args->name[item_len] = 0; 252023d0b79dSTomohiro Misono 252123d0b79dSTomohiro Misono out: 252223d0b79dSTomohiro Misono btrfs_free_path(path); 252323d0b79dSTomohiro Misono return ret; 252423d0b79dSTomohiro Misono } 252523d0b79dSTomohiro Misono 2526ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2527ac8e9819SChris Mason void __user *argp) 2528ac8e9819SChris Mason { 2529ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2530ac8e9819SChris Mason struct inode *inode; 253101b810b8SDavid Sterba int ret = 0; 2532ac8e9819SChris Mason 25332354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 25342354d08fSJulia Lawall if (IS_ERR(args)) 25352354d08fSJulia Lawall return PTR_ERR(args); 2536c2b96929SDan Carpenter 2537496ad9aaSAl Viro inode = file_inode(file); 2538ac8e9819SChris Mason 253901b810b8SDavid Sterba /* 254001b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 254101b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 254201b810b8SDavid Sterba */ 25431b53ac4dSChris Mason if (args->treeid == 0) 25441b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 25451b53ac4dSChris Mason 254601b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 254701b810b8SDavid Sterba args->name[0] = 0; 254801b810b8SDavid Sterba goto out; 254901b810b8SDavid Sterba } 255001b810b8SDavid Sterba 255101b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 255201b810b8SDavid Sterba ret = -EPERM; 255301b810b8SDavid Sterba goto out; 255401b810b8SDavid Sterba } 255501b810b8SDavid Sterba 2556ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2557ac8e9819SChris Mason args->treeid, args->objectid, 2558ac8e9819SChris Mason args->name); 2559ac8e9819SChris Mason 256001b810b8SDavid Sterba out: 2561ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2562ac8e9819SChris Mason ret = -EFAULT; 2563ac8e9819SChris Mason 2564ac8e9819SChris Mason kfree(args); 256598d377a0STARUISI Hiroaki return ret; 256698d377a0STARUISI Hiroaki } 256798d377a0STARUISI Hiroaki 256823d0b79dSTomohiro Misono /* 256923d0b79dSTomohiro Misono * Version of ino_lookup ioctl (unprivileged) 257023d0b79dSTomohiro Misono * 257123d0b79dSTomohiro Misono * The main differences from ino_lookup ioctl are: 257223d0b79dSTomohiro Misono * 257323d0b79dSTomohiro Misono * 1. Read + Exec permission will be checked using inode_permission() during 257423d0b79dSTomohiro Misono * path construction. -EACCES will be returned in case of failure. 257523d0b79dSTomohiro Misono * 2. Path construction will be stopped at the inode number which corresponds 257623d0b79dSTomohiro Misono * to the fd with which this ioctl is called. If constructed path does not 257723d0b79dSTomohiro Misono * exist under fd's inode, -EACCES will be returned. 257823d0b79dSTomohiro Misono * 3. The name of bottom subvolume is also searched and filled. 257923d0b79dSTomohiro Misono */ 258023d0b79dSTomohiro Misono static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) 258123d0b79dSTomohiro Misono { 258223d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args; 258323d0b79dSTomohiro Misono struct inode *inode; 258423d0b79dSTomohiro Misono int ret; 258523d0b79dSTomohiro Misono 258623d0b79dSTomohiro Misono args = memdup_user(argp, sizeof(*args)); 258723d0b79dSTomohiro Misono if (IS_ERR(args)) 258823d0b79dSTomohiro Misono return PTR_ERR(args); 258923d0b79dSTomohiro Misono 259023d0b79dSTomohiro Misono inode = file_inode(file); 259123d0b79dSTomohiro Misono 259223d0b79dSTomohiro Misono if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && 259323d0b79dSTomohiro Misono BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { 259423d0b79dSTomohiro Misono /* 259523d0b79dSTomohiro Misono * The subvolume does not exist under fd with which this is 259623d0b79dSTomohiro Misono * called 259723d0b79dSTomohiro Misono */ 259823d0b79dSTomohiro Misono kfree(args); 259923d0b79dSTomohiro Misono return -EACCES; 260023d0b79dSTomohiro Misono } 260123d0b79dSTomohiro Misono 260223d0b79dSTomohiro Misono ret = btrfs_search_path_in_tree_user(inode, args); 260323d0b79dSTomohiro Misono 260423d0b79dSTomohiro Misono if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 260523d0b79dSTomohiro Misono ret = -EFAULT; 260623d0b79dSTomohiro Misono 260723d0b79dSTomohiro Misono kfree(args); 260823d0b79dSTomohiro Misono return ret; 260923d0b79dSTomohiro Misono } 261023d0b79dSTomohiro Misono 2611b64ec075STomohiro Misono /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ 2612b64ec075STomohiro Misono static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) 2613b64ec075STomohiro Misono { 2614b64ec075STomohiro Misono struct btrfs_ioctl_get_subvol_info_args *subvol_info; 2615b64ec075STomohiro Misono struct btrfs_fs_info *fs_info; 2616b64ec075STomohiro Misono struct btrfs_root *root; 2617b64ec075STomohiro Misono struct btrfs_path *path; 2618b64ec075STomohiro Misono struct btrfs_key key; 2619b64ec075STomohiro Misono struct btrfs_root_item *root_item; 2620b64ec075STomohiro Misono struct btrfs_root_ref *rref; 2621b64ec075STomohiro Misono struct extent_buffer *leaf; 2622b64ec075STomohiro Misono unsigned long item_off; 2623b64ec075STomohiro Misono unsigned long item_len; 2624b64ec075STomohiro Misono struct inode *inode; 2625b64ec075STomohiro Misono int slot; 2626b64ec075STomohiro Misono int ret = 0; 2627b64ec075STomohiro Misono 2628b64ec075STomohiro Misono path = btrfs_alloc_path(); 2629b64ec075STomohiro Misono if (!path) 2630b64ec075STomohiro Misono return -ENOMEM; 2631b64ec075STomohiro Misono 2632b64ec075STomohiro Misono subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); 2633b64ec075STomohiro Misono if (!subvol_info) { 2634b64ec075STomohiro Misono btrfs_free_path(path); 2635b64ec075STomohiro Misono return -ENOMEM; 2636b64ec075STomohiro Misono } 2637b64ec075STomohiro Misono 2638b64ec075STomohiro Misono inode = file_inode(file); 2639b64ec075STomohiro Misono fs_info = BTRFS_I(inode)->root->fs_info; 2640b64ec075STomohiro Misono 2641b64ec075STomohiro Misono /* Get root_item of inode's subvolume */ 2642b64ec075STomohiro Misono key.objectid = BTRFS_I(inode)->root->root_key.objectid; 2643b64ec075STomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 2644b64ec075STomohiro Misono key.offset = (u64)-1; 2645b64ec075STomohiro Misono root = btrfs_read_fs_root_no_name(fs_info, &key); 2646b64ec075STomohiro Misono if (IS_ERR(root)) { 2647b64ec075STomohiro Misono ret = PTR_ERR(root); 2648b64ec075STomohiro Misono goto out; 2649b64ec075STomohiro Misono } 2650b64ec075STomohiro Misono root_item = &root->root_item; 2651b64ec075STomohiro Misono 2652b64ec075STomohiro Misono subvol_info->treeid = key.objectid; 2653b64ec075STomohiro Misono 2654b64ec075STomohiro Misono subvol_info->generation = btrfs_root_generation(root_item); 2655b64ec075STomohiro Misono subvol_info->flags = btrfs_root_flags(root_item); 2656b64ec075STomohiro Misono 2657b64ec075STomohiro Misono memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); 2658b64ec075STomohiro Misono memcpy(subvol_info->parent_uuid, root_item->parent_uuid, 2659b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2660b64ec075STomohiro Misono memcpy(subvol_info->received_uuid, root_item->received_uuid, 2661b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2662b64ec075STomohiro Misono 2663b64ec075STomohiro Misono subvol_info->ctransid = btrfs_root_ctransid(root_item); 2664b64ec075STomohiro Misono subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); 2665b64ec075STomohiro Misono subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); 2666b64ec075STomohiro Misono 2667b64ec075STomohiro Misono subvol_info->otransid = btrfs_root_otransid(root_item); 2668b64ec075STomohiro Misono subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); 2669b64ec075STomohiro Misono subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); 2670b64ec075STomohiro Misono 2671b64ec075STomohiro Misono subvol_info->stransid = btrfs_root_stransid(root_item); 2672b64ec075STomohiro Misono subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); 2673b64ec075STomohiro Misono subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); 2674b64ec075STomohiro Misono 2675b64ec075STomohiro Misono subvol_info->rtransid = btrfs_root_rtransid(root_item); 2676b64ec075STomohiro Misono subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); 2677b64ec075STomohiro Misono subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); 2678b64ec075STomohiro Misono 2679b64ec075STomohiro Misono if (key.objectid != BTRFS_FS_TREE_OBJECTID) { 2680b64ec075STomohiro Misono /* Search root tree for ROOT_BACKREF of this subvolume */ 2681b64ec075STomohiro Misono root = fs_info->tree_root; 2682b64ec075STomohiro Misono 2683b64ec075STomohiro Misono key.type = BTRFS_ROOT_BACKREF_KEY; 2684b64ec075STomohiro Misono key.offset = 0; 2685b64ec075STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2686b64ec075STomohiro Misono if (ret < 0) { 2687b64ec075STomohiro Misono goto out; 2688b64ec075STomohiro Misono } else if (path->slots[0] >= 2689b64ec075STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 2690b64ec075STomohiro Misono ret = btrfs_next_leaf(root, path); 2691b64ec075STomohiro Misono if (ret < 0) { 2692b64ec075STomohiro Misono goto out; 2693b64ec075STomohiro Misono } else if (ret > 0) { 2694b64ec075STomohiro Misono ret = -EUCLEAN; 2695b64ec075STomohiro Misono goto out; 2696b64ec075STomohiro Misono } 2697b64ec075STomohiro Misono } 2698b64ec075STomohiro Misono 2699b64ec075STomohiro Misono leaf = path->nodes[0]; 2700b64ec075STomohiro Misono slot = path->slots[0]; 2701b64ec075STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 2702b64ec075STomohiro Misono if (key.objectid == subvol_info->treeid && 2703b64ec075STomohiro Misono key.type == BTRFS_ROOT_BACKREF_KEY) { 2704b64ec075STomohiro Misono subvol_info->parent_id = key.offset; 2705b64ec075STomohiro Misono 2706b64ec075STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 2707b64ec075STomohiro Misono subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); 2708b64ec075STomohiro Misono 2709b64ec075STomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot) 2710b64ec075STomohiro Misono + sizeof(struct btrfs_root_ref); 2711b64ec075STomohiro Misono item_len = btrfs_item_size_nr(leaf, slot) 2712b64ec075STomohiro Misono - sizeof(struct btrfs_root_ref); 2713b64ec075STomohiro Misono read_extent_buffer(leaf, subvol_info->name, 2714b64ec075STomohiro Misono item_off, item_len); 2715b64ec075STomohiro Misono } else { 2716b64ec075STomohiro Misono ret = -ENOENT; 2717b64ec075STomohiro Misono goto out; 2718b64ec075STomohiro Misono } 2719b64ec075STomohiro Misono } 2720b64ec075STomohiro Misono 2721b64ec075STomohiro Misono if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) 2722b64ec075STomohiro Misono ret = -EFAULT; 2723b64ec075STomohiro Misono 2724b64ec075STomohiro Misono out: 2725b64ec075STomohiro Misono btrfs_free_path(path); 2726b64ec075STomohiro Misono kzfree(subvol_info); 2727b64ec075STomohiro Misono return ret; 2728b64ec075STomohiro Misono } 2729b64ec075STomohiro Misono 273042e4b520STomohiro Misono /* 273142e4b520STomohiro Misono * Return ROOT_REF information of the subvolume containing this inode 273242e4b520STomohiro Misono * except the subvolume name. 273342e4b520STomohiro Misono */ 273442e4b520STomohiro Misono static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) 273542e4b520STomohiro Misono { 273642e4b520STomohiro Misono struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; 273742e4b520STomohiro Misono struct btrfs_root_ref *rref; 273842e4b520STomohiro Misono struct btrfs_root *root; 273942e4b520STomohiro Misono struct btrfs_path *path; 274042e4b520STomohiro Misono struct btrfs_key key; 274142e4b520STomohiro Misono struct extent_buffer *leaf; 274242e4b520STomohiro Misono struct inode *inode; 274342e4b520STomohiro Misono u64 objectid; 274442e4b520STomohiro Misono int slot; 274542e4b520STomohiro Misono int ret; 274642e4b520STomohiro Misono u8 found; 274742e4b520STomohiro Misono 274842e4b520STomohiro Misono path = btrfs_alloc_path(); 274942e4b520STomohiro Misono if (!path) 275042e4b520STomohiro Misono return -ENOMEM; 275142e4b520STomohiro Misono 275242e4b520STomohiro Misono rootrefs = memdup_user(argp, sizeof(*rootrefs)); 275342e4b520STomohiro Misono if (IS_ERR(rootrefs)) { 275442e4b520STomohiro Misono btrfs_free_path(path); 275542e4b520STomohiro Misono return PTR_ERR(rootrefs); 275642e4b520STomohiro Misono } 275742e4b520STomohiro Misono 275842e4b520STomohiro Misono inode = file_inode(file); 275942e4b520STomohiro Misono root = BTRFS_I(inode)->root->fs_info->tree_root; 276042e4b520STomohiro Misono objectid = BTRFS_I(inode)->root->root_key.objectid; 276142e4b520STomohiro Misono 276242e4b520STomohiro Misono key.objectid = objectid; 276342e4b520STomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 276442e4b520STomohiro Misono key.offset = rootrefs->min_treeid; 276542e4b520STomohiro Misono found = 0; 276642e4b520STomohiro Misono 276742e4b520STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 276842e4b520STomohiro Misono if (ret < 0) { 276942e4b520STomohiro Misono goto out; 277042e4b520STomohiro Misono } else if (path->slots[0] >= 277142e4b520STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 277242e4b520STomohiro Misono ret = btrfs_next_leaf(root, path); 277342e4b520STomohiro Misono if (ret < 0) { 277442e4b520STomohiro Misono goto out; 277542e4b520STomohiro Misono } else if (ret > 0) { 277642e4b520STomohiro Misono ret = -EUCLEAN; 277742e4b520STomohiro Misono goto out; 277842e4b520STomohiro Misono } 277942e4b520STomohiro Misono } 278042e4b520STomohiro Misono while (1) { 278142e4b520STomohiro Misono leaf = path->nodes[0]; 278242e4b520STomohiro Misono slot = path->slots[0]; 278342e4b520STomohiro Misono 278442e4b520STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 278542e4b520STomohiro Misono if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { 278642e4b520STomohiro Misono ret = 0; 278742e4b520STomohiro Misono goto out; 278842e4b520STomohiro Misono } 278942e4b520STomohiro Misono 279042e4b520STomohiro Misono if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { 279142e4b520STomohiro Misono ret = -EOVERFLOW; 279242e4b520STomohiro Misono goto out; 279342e4b520STomohiro Misono } 279442e4b520STomohiro Misono 279542e4b520STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 279642e4b520STomohiro Misono rootrefs->rootref[found].treeid = key.offset; 279742e4b520STomohiro Misono rootrefs->rootref[found].dirid = 279842e4b520STomohiro Misono btrfs_root_ref_dirid(leaf, rref); 279942e4b520STomohiro Misono found++; 280042e4b520STomohiro Misono 280142e4b520STomohiro Misono ret = btrfs_next_item(root, path); 280242e4b520STomohiro Misono if (ret < 0) { 280342e4b520STomohiro Misono goto out; 280442e4b520STomohiro Misono } else if (ret > 0) { 280542e4b520STomohiro Misono ret = -EUCLEAN; 280642e4b520STomohiro Misono goto out; 280742e4b520STomohiro Misono } 280842e4b520STomohiro Misono } 280942e4b520STomohiro Misono 281042e4b520STomohiro Misono out: 281142e4b520STomohiro Misono if (!ret || ret == -EOVERFLOW) { 281242e4b520STomohiro Misono rootrefs->num_items = found; 281342e4b520STomohiro Misono /* update min_treeid for next search */ 281442e4b520STomohiro Misono if (found) 281542e4b520STomohiro Misono rootrefs->min_treeid = 281642e4b520STomohiro Misono rootrefs->rootref[found - 1].treeid + 1; 281742e4b520STomohiro Misono if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) 281842e4b520STomohiro Misono ret = -EFAULT; 281942e4b520STomohiro Misono } 282042e4b520STomohiro Misono 282142e4b520STomohiro Misono kfree(rootrefs); 282242e4b520STomohiro Misono btrfs_free_path(path); 282342e4b520STomohiro Misono 282442e4b520STomohiro Misono return ret; 282542e4b520STomohiro Misono } 282642e4b520STomohiro Misono 282776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 282876dda93cSYan, Zheng void __user *arg) 282976dda93cSYan, Zheng { 283054563d41SAl Viro struct dentry *parent = file->f_path.dentry; 28310b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 283276dda93cSYan, Zheng struct dentry *dentry; 28332b0143b5SDavid Howells struct inode *dir = d_inode(parent); 283476dda93cSYan, Zheng struct inode *inode; 283576dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 283676dda93cSYan, Zheng struct btrfs_root *dest = NULL; 283776dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 283876dda93cSYan, Zheng int namelen; 283976dda93cSYan, Zheng int err = 0; 284076dda93cSYan, Zheng 2841325c50e3SJeff Mahoney if (!S_ISDIR(dir->i_mode)) 2842325c50e3SJeff Mahoney return -ENOTDIR; 2843325c50e3SJeff Mahoney 284476dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 284576dda93cSYan, Zheng if (IS_ERR(vol_args)) 284676dda93cSYan, Zheng return PTR_ERR(vol_args); 284776dda93cSYan, Zheng 284876dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 284976dda93cSYan, Zheng namelen = strlen(vol_args->name); 285076dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 285176dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 285276dda93cSYan, Zheng err = -EINVAL; 285376dda93cSYan, Zheng goto out; 285476dda93cSYan, Zheng } 285576dda93cSYan, Zheng 2856a561be71SAl Viro err = mnt_want_write_file(file); 285776dda93cSYan, Zheng if (err) 285876dda93cSYan, Zheng goto out; 285976dda93cSYan, Zheng 2860521e0546SDavid Sterba 286100235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 286200235411SAl Viro if (err == -EINTR) 286300235411SAl Viro goto out_drop_write; 286476dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 286576dda93cSYan, Zheng if (IS_ERR(dentry)) { 286676dda93cSYan, Zheng err = PTR_ERR(dentry); 286776dda93cSYan, Zheng goto out_unlock_dir; 286876dda93cSYan, Zheng } 286976dda93cSYan, Zheng 28702b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 287176dda93cSYan, Zheng err = -ENOENT; 287276dda93cSYan, Zheng goto out_dput; 287376dda93cSYan, Zheng } 287476dda93cSYan, Zheng 28752b0143b5SDavid Howells inode = d_inode(dentry); 28764260f7c7SSage Weil dest = BTRFS_I(inode)->root; 28774260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 28784260f7c7SSage Weil /* 28794260f7c7SSage Weil * Regular user. Only allow this with a special mount 28804260f7c7SSage Weil * option, when the user has write+exec access to the 28814260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 28824260f7c7SSage Weil * allowed. 28834260f7c7SSage Weil * 28844260f7c7SSage Weil * Note that this is _not_ check that the subvol is 28854260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 28864260f7c7SSage Weil * otherwise be able to delete. 28874260f7c7SSage Weil * 28884260f7c7SSage Weil * Users who want to delete empty subvols should try 28894260f7c7SSage Weil * rmdir(2). 28904260f7c7SSage Weil */ 28914260f7c7SSage Weil err = -EPERM; 28920b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 28934260f7c7SSage Weil goto out_dput; 28944260f7c7SSage Weil 28954260f7c7SSage Weil /* 28964260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 28974260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 28984260f7c7SSage Weil * must be called on the dentry referencing the root 28994260f7c7SSage Weil * of the subvol, not a random directory contained 29004260f7c7SSage Weil * within it. 29014260f7c7SSage Weil */ 29024260f7c7SSage Weil err = -EINVAL; 29034260f7c7SSage Weil if (root == dest) 29044260f7c7SSage Weil goto out_dput; 29054260f7c7SSage Weil 29064260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 29074260f7c7SSage Weil if (err) 29084260f7c7SSage Weil goto out_dput; 29095c39da5bSMiao Xie } 29104260f7c7SSage Weil 29115c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 29124260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 29134260f7c7SSage Weil if (err) 29144260f7c7SSage Weil goto out_dput; 29154260f7c7SSage Weil 29164a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 291776dda93cSYan, Zheng err = -EINVAL; 291876dda93cSYan, Zheng goto out_dput; 291976dda93cSYan, Zheng } 292076dda93cSYan, Zheng 29215955102cSAl Viro inode_lock(inode); 2922f60a2364SMisono Tomohiro err = btrfs_delete_subvolume(dir, dentry); 29235955102cSAl Viro inode_unlock(inode); 2924f60a2364SMisono Tomohiro if (!err) 292576dda93cSYan, Zheng d_delete(dentry); 2926fa6ac876SLiu Bo 292776dda93cSYan, Zheng out_dput: 292876dda93cSYan, Zheng dput(dentry); 292976dda93cSYan, Zheng out_unlock_dir: 29305955102cSAl Viro inode_unlock(dir); 293100235411SAl Viro out_drop_write: 29322a79f17eSAl Viro mnt_drop_write_file(file); 293376dda93cSYan, Zheng out: 293476dda93cSYan, Zheng kfree(vol_args); 293576dda93cSYan, Zheng return err; 293676dda93cSYan, Zheng } 293776dda93cSYan, Zheng 29381e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2939f46b5a66SChristoph Hellwig { 2940496ad9aaSAl Viro struct inode *inode = file_inode(file); 2941f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 29421e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2943c146afadSYan Zheng int ret; 2944c146afadSYan Zheng 294525122d15SIlya Dryomov ret = mnt_want_write_file(file); 294625122d15SIlya Dryomov if (ret) 294725122d15SIlya Dryomov return ret; 2948b83cc969SLi Zefan 294925122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 295025122d15SIlya Dryomov ret = -EROFS; 295125122d15SIlya Dryomov goto out; 29525ac00addSStefan Behrens } 2953f46b5a66SChristoph Hellwig 2954f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2955f46b5a66SChristoph Hellwig case S_IFDIR: 2956e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2957e441d54dSChris Mason ret = -EPERM; 2958e441d54dSChris Mason goto out; 2959e441d54dSChris Mason } 2960de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 2961f46b5a66SChristoph Hellwig break; 2962f46b5a66SChristoph Hellwig case S_IFREG: 2963616d374eSAdam Borowski /* 2964616d374eSAdam Borowski * Note that this does not check the file descriptor for write 2965616d374eSAdam Borowski * access. This prevents defragmenting executables that are 2966616d374eSAdam Borowski * running and allows defrag on files open in read-only mode. 2967616d374eSAdam Borowski */ 2968616d374eSAdam Borowski if (!capable(CAP_SYS_ADMIN) && 2969616d374eSAdam Borowski inode_permission(inode, MAY_WRITE)) { 2970616d374eSAdam Borowski ret = -EPERM; 2971e441d54dSChris Mason goto out; 2972e441d54dSChris Mason } 29731e701a32SChris Mason 29741e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 29751e701a32SChris Mason if (!range) { 29761e701a32SChris Mason ret = -ENOMEM; 29771e701a32SChris Mason goto out; 29781e701a32SChris Mason } 29791e701a32SChris Mason 29801e701a32SChris Mason if (argp) { 29811e701a32SChris Mason if (copy_from_user(range, argp, 29821e701a32SChris Mason sizeof(*range))) { 29831e701a32SChris Mason ret = -EFAULT; 29841e701a32SChris Mason kfree(range); 2985683be16eSDan Carpenter goto out; 29861e701a32SChris Mason } 29871e701a32SChris Mason /* compression requires us to start the IO */ 29881e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 29891e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 29901e701a32SChris Mason range->extent_thresh = (u32)-1; 29911e701a32SChris Mason } 29921e701a32SChris Mason } else { 29931e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 29941e701a32SChris Mason range->len = (u64)-1; 29951e701a32SChris Mason } 2996496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 29977c829b72SAnand Jain range, BTRFS_OLDEST_GENERATION, 0); 29984cb5300bSChris Mason if (ret > 0) 29994cb5300bSChris Mason ret = 0; 30001e701a32SChris Mason kfree(range); 3001f46b5a66SChristoph Hellwig break; 30028929ecfaSYan, Zheng default: 30038929ecfaSYan, Zheng ret = -EINVAL; 3004f46b5a66SChristoph Hellwig } 3005e441d54dSChris Mason out: 300625122d15SIlya Dryomov mnt_drop_write_file(file); 3007e441d54dSChris Mason return ret; 3008f46b5a66SChristoph Hellwig } 3009f46b5a66SChristoph Hellwig 30102ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 3011f46b5a66SChristoph Hellwig { 3012f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3013f46b5a66SChristoph Hellwig int ret; 3014f46b5a66SChristoph Hellwig 3015e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3016e441d54dSChris Mason return -EPERM; 3017e441d54dSChris Mason 3018171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) 3019e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3020c9e9f97bSIlya Dryomov 3021dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3022c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3023c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3024c9e9f97bSIlya Dryomov goto out; 3025c9e9f97bSIlya Dryomov } 3026f46b5a66SChristoph Hellwig 30275516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 30282ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 3029f46b5a66SChristoph Hellwig 303043d20761SAnand Jain if (!ret) 30310b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 303243d20761SAnand Jain 3033f46b5a66SChristoph Hellwig kfree(vol_args); 3034c9e9f97bSIlya Dryomov out: 3035171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3036f46b5a66SChristoph Hellwig return ret; 3037f46b5a66SChristoph Hellwig } 3038f46b5a66SChristoph Hellwig 30396b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 3040f46b5a66SChristoph Hellwig { 30410b246afaSJeff Mahoney struct inode *inode = file_inode(file); 30420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 30436b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 3044f46b5a66SChristoph Hellwig int ret; 3045f46b5a66SChristoph Hellwig 3046e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3047e441d54dSChris Mason return -EPERM; 3048e441d54dSChris Mason 3049da24927bSMiao Xie ret = mnt_want_write_file(file); 3050da24927bSMiao Xie if (ret) 3051da24927bSMiao Xie return ret; 3052c146afadSYan Zheng 3053dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3054c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3055c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3056c47ca32dSDan Carpenter goto err_drop; 3057c9e9f97bSIlya Dryomov } 3058f46b5a66SChristoph Hellwig 30596b526ed7SAnand Jain /* Check for compatibility reject unknown flags */ 3060fd4e994bSOmar Sandoval if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) { 3061fd4e994bSOmar Sandoval ret = -EOPNOTSUPP; 3062fd4e994bSOmar Sandoval goto out; 3063fd4e994bSOmar Sandoval } 3064f46b5a66SChristoph Hellwig 3065171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3066183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3067183860f6SAnand Jain goto out; 3068183860f6SAnand Jain } 3069183860f6SAnand Jain 3070735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 30712ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); 30726b526ed7SAnand Jain } else { 30736b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 30742ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 30756b526ed7SAnand Jain } 3076171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3077183860f6SAnand Jain 30786b526ed7SAnand Jain if (!ret) { 3079735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 30800b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 30816b526ed7SAnand Jain vol_args->devid); 30826b526ed7SAnand Jain else 30830b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 30846b526ed7SAnand Jain vol_args->name); 30856b526ed7SAnand Jain } 3086183860f6SAnand Jain out: 3087183860f6SAnand Jain kfree(vol_args); 3088c47ca32dSDan Carpenter err_drop: 30894ac20c70SIlya Dryomov mnt_drop_write_file(file); 3090f46b5a66SChristoph Hellwig return ret; 3091f46b5a66SChristoph Hellwig } 3092f46b5a66SChristoph Hellwig 3093f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 3094f46b5a66SChristoph Hellwig { 30950b246afaSJeff Mahoney struct inode *inode = file_inode(file); 30960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3097f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3098f46b5a66SChristoph Hellwig int ret; 3099f46b5a66SChristoph Hellwig 3100f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3101f46b5a66SChristoph Hellwig return -EPERM; 3102f46b5a66SChristoph Hellwig 3103f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 3104f46b5a66SChristoph Hellwig if (ret) 3105f46b5a66SChristoph Hellwig return ret; 3106f46b5a66SChristoph Hellwig 3107171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3108f46b5a66SChristoph Hellwig ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 310958d7bbf8SDavid Sterba goto out_drop_write; 311058d7bbf8SDavid Sterba } 311158d7bbf8SDavid Sterba 311258d7bbf8SDavid Sterba vol_args = memdup_user(arg, sizeof(*vol_args)); 311358d7bbf8SDavid Sterba if (IS_ERR(vol_args)) { 311458d7bbf8SDavid Sterba ret = PTR_ERR(vol_args); 3115f46b5a66SChristoph Hellwig goto out; 3116f46b5a66SChristoph Hellwig } 3117f46b5a66SChristoph Hellwig 311858d7bbf8SDavid Sterba vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 31192ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 3120f46b5a66SChristoph Hellwig 3121f46b5a66SChristoph Hellwig if (!ret) 31220b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 3123f46b5a66SChristoph Hellwig kfree(vol_args); 312458d7bbf8SDavid Sterba out: 3125171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 312658d7bbf8SDavid Sterba out_drop_write: 3127f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 312858d7bbf8SDavid Sterba 3129f46b5a66SChristoph Hellwig return ret; 3130f46b5a66SChristoph Hellwig } 3131f46b5a66SChristoph Hellwig 31322ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 31332ff7e61eSJeff Mahoney void __user *arg) 3134475f6387SJan Schmidt { 3135027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 3136475f6387SJan Schmidt struct btrfs_device *device; 31370b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3138027ed2f0SLi Zefan int ret = 0; 3139475f6387SJan Schmidt 3140027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 3141027ed2f0SLi Zefan if (!fi_args) 3142027ed2f0SLi Zefan return -ENOMEM; 3143027ed2f0SLi Zefan 3144d03262c7SDavid Sterba rcu_read_lock(); 3145027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 3146475f6387SJan Schmidt 3147d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 3148027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 3149027ed2f0SLi Zefan fi_args->max_id = device->devid; 3150475f6387SJan Schmidt } 3151d03262c7SDavid Sterba rcu_read_unlock(); 3152475f6387SJan Schmidt 3153d03262c7SDavid Sterba memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid)); 3154bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 3155bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 3156bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 315780a773fbSDavid Sterba 3158027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 3159027ed2f0SLi Zefan ret = -EFAULT; 3160475f6387SJan Schmidt 3161027ed2f0SLi Zefan kfree(fi_args); 3162027ed2f0SLi Zefan return ret; 3163475f6387SJan Schmidt } 3164475f6387SJan Schmidt 31652ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 31662ff7e61eSJeff Mahoney void __user *arg) 3167475f6387SJan Schmidt { 3168475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 3169475f6387SJan Schmidt struct btrfs_device *dev; 3170475f6387SJan Schmidt int ret = 0; 3171475f6387SJan Schmidt char *s_uuid = NULL; 3172475f6387SJan Schmidt 3173475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 3174475f6387SJan Schmidt if (IS_ERR(di_args)) 3175475f6387SJan Schmidt return PTR_ERR(di_args); 3176475f6387SJan Schmidt 3177dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 3178475f6387SJan Schmidt s_uuid = di_args->uuid; 3179475f6387SJan Schmidt 3180c5593ca3SDavid Sterba rcu_read_lock(); 31810b246afaSJeff Mahoney dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL); 3182475f6387SJan Schmidt 3183475f6387SJan Schmidt if (!dev) { 3184475f6387SJan Schmidt ret = -ENODEV; 3185475f6387SJan Schmidt goto out; 3186475f6387SJan Schmidt } 3187475f6387SJan Schmidt 3188475f6387SJan Schmidt di_args->devid = dev->devid; 31897cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 31907cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 3191475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 3192a27202fbSJim Meyering if (dev->name) { 3193672d5990SMisono Tomohiro strncpy(di_args->path, rcu_str_deref(dev->name), 3194672d5990SMisono Tomohiro sizeof(di_args->path) - 1); 3195a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 3196a27202fbSJim Meyering } else { 319799ba55adSStefan Behrens di_args->path[0] = '\0'; 3198a27202fbSJim Meyering } 3199475f6387SJan Schmidt 3200475f6387SJan Schmidt out: 3201c5593ca3SDavid Sterba rcu_read_unlock(); 3202475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 3203475f6387SJan Schmidt ret = -EFAULT; 3204475f6387SJan Schmidt 3205475f6387SJan Schmidt kfree(di_args); 3206475f6387SJan Schmidt return ret; 3207475f6387SJan Schmidt } 3208475f6387SJan Schmidt 3209f4414602SMark Fasheh static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) 3210416161dbSMark Fasheh { 3211416161dbSMark Fasheh struct page *page; 3212416161dbSMark Fasheh 3213416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 3214416161dbSMark Fasheh if (!page) 321531314002SFilipe Manana return ERR_PTR(-ENOMEM); 3216416161dbSMark Fasheh 3217416161dbSMark Fasheh if (!PageUptodate(page)) { 321831314002SFilipe Manana int ret; 321931314002SFilipe Manana 322031314002SFilipe Manana ret = btrfs_readpage(NULL, page); 322131314002SFilipe Manana if (ret) 322231314002SFilipe Manana return ERR_PTR(ret); 3223416161dbSMark Fasheh lock_page(page); 3224416161dbSMark Fasheh if (!PageUptodate(page)) { 3225416161dbSMark Fasheh unlock_page(page); 322609cbfeafSKirill A. Shutemov put_page(page); 322731314002SFilipe Manana return ERR_PTR(-EIO); 3228416161dbSMark Fasheh } 322931314002SFilipe Manana if (page->mapping != inode->i_mapping) { 3230416161dbSMark Fasheh unlock_page(page); 323109cbfeafSKirill A. Shutemov put_page(page); 323231314002SFilipe Manana return ERR_PTR(-EAGAIN); 3233416161dbSMark Fasheh } 3234416161dbSMark Fasheh } 3235416161dbSMark Fasheh 3236416161dbSMark Fasheh return page; 3237416161dbSMark Fasheh } 3238416161dbSMark Fasheh 3239f4414602SMark Fasheh static int gather_extent_pages(struct inode *inode, struct page **pages, 3240f4414602SMark Fasheh int num_pages, u64 off) 3241f4414602SMark Fasheh { 3242f4414602SMark Fasheh int i; 324309cbfeafSKirill A. Shutemov pgoff_t index = off >> PAGE_SHIFT; 3244f4414602SMark Fasheh 3245f4414602SMark Fasheh for (i = 0; i < num_pages; i++) { 324631314002SFilipe Manana again: 3247f4414602SMark Fasheh pages[i] = extent_same_get_page(inode, index + i); 324831314002SFilipe Manana if (IS_ERR(pages[i])) { 324931314002SFilipe Manana int err = PTR_ERR(pages[i]); 325031314002SFilipe Manana 325131314002SFilipe Manana if (err == -EAGAIN) 325231314002SFilipe Manana goto again; 325331314002SFilipe Manana pages[i] = NULL; 325431314002SFilipe Manana return err; 325531314002SFilipe Manana } 3256f4414602SMark Fasheh } 3257f4414602SMark Fasheh return 0; 3258f4414602SMark Fasheh } 3259f4414602SMark Fasheh 3260e0bd70c6SFilipe Manana static int lock_extent_range(struct inode *inode, u64 off, u64 len, 3261e0bd70c6SFilipe Manana bool retry_range_locking) 326277fe20dcSMark Fasheh { 3263e0bd70c6SFilipe Manana /* 3264e0bd70c6SFilipe Manana * Do any pending delalloc/csum calculations on inode, one way or 3265e0bd70c6SFilipe Manana * another, and lock file content. 3266e0bd70c6SFilipe Manana * The locking order is: 3267e0bd70c6SFilipe Manana * 3268e0bd70c6SFilipe Manana * 1) pages 3269e0bd70c6SFilipe Manana * 2) range in the inode's io tree 3270e0bd70c6SFilipe Manana */ 327177fe20dcSMark Fasheh while (1) { 327277fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 327377fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 327477fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 327577fe20dcSMark Fasheh off + len - 1); 3276ff5df9b8SFilipe Manana if ((!ordered || 3277ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 3278ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 327977fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 3280ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 3281ff5df9b8SFilipe Manana if (ordered) 3282ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 328377fe20dcSMark Fasheh break; 3284ff5df9b8SFilipe Manana } 328577fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 328677fe20dcSMark Fasheh if (ordered) 328777fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 3288e0bd70c6SFilipe Manana if (!retry_range_locking) 3289e0bd70c6SFilipe Manana return -EAGAIN; 329077fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 329177fe20dcSMark Fasheh } 3292e0bd70c6SFilipe Manana return 0; 329377fe20dcSMark Fasheh } 329477fe20dcSMark Fasheh 3295f4414602SMark Fasheh static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) 3296416161dbSMark Fasheh { 32975955102cSAl Viro inode_unlock(inode1); 32985955102cSAl Viro inode_unlock(inode2); 3299416161dbSMark Fasheh } 3300416161dbSMark Fasheh 3301f4414602SMark Fasheh static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) 3302f4414602SMark Fasheh { 3303f4414602SMark Fasheh if (inode1 < inode2) 3304f4414602SMark Fasheh swap(inode1, inode2); 3305f4414602SMark Fasheh 33065955102cSAl Viro inode_lock_nested(inode1, I_MUTEX_PARENT); 33075955102cSAl Viro inode_lock_nested(inode2, I_MUTEX_CHILD); 3308f4414602SMark Fasheh } 3309f4414602SMark Fasheh 3310f4414602SMark Fasheh static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 3311f4414602SMark Fasheh struct inode *inode2, u64 loff2, u64 len) 3312f4414602SMark Fasheh { 3313f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 3314f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 3315f4414602SMark Fasheh } 3316f4414602SMark Fasheh 3317e0bd70c6SFilipe Manana static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 3318e0bd70c6SFilipe Manana struct inode *inode2, u64 loff2, u64 len, 3319e0bd70c6SFilipe Manana bool retry_range_locking) 3320416161dbSMark Fasheh { 3321e0bd70c6SFilipe Manana int ret; 3322e0bd70c6SFilipe Manana 3323416161dbSMark Fasheh if (inode1 < inode2) { 3324416161dbSMark Fasheh swap(inode1, inode2); 3325416161dbSMark Fasheh swap(loff1, loff2); 3326416161dbSMark Fasheh } 3327e0bd70c6SFilipe Manana ret = lock_extent_range(inode1, loff1, len, retry_range_locking); 3328e0bd70c6SFilipe Manana if (ret) 3329e0bd70c6SFilipe Manana return ret; 3330e0bd70c6SFilipe Manana ret = lock_extent_range(inode2, loff2, len, retry_range_locking); 3331e0bd70c6SFilipe Manana if (ret) 3332e0bd70c6SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, 3333e0bd70c6SFilipe Manana loff1 + len - 1); 3334e0bd70c6SFilipe Manana return ret; 3335416161dbSMark Fasheh } 3336f4414602SMark Fasheh 3337f4414602SMark Fasheh struct cmp_pages { 3338f4414602SMark Fasheh int num_pages; 3339f4414602SMark Fasheh struct page **src_pages; 3340f4414602SMark Fasheh struct page **dst_pages; 3341f4414602SMark Fasheh }; 3342f4414602SMark Fasheh 3343f4414602SMark Fasheh static void btrfs_cmp_data_free(struct cmp_pages *cmp) 3344f4414602SMark Fasheh { 3345f4414602SMark Fasheh int i; 3346f4414602SMark Fasheh struct page *pg; 3347f4414602SMark Fasheh 3348f4414602SMark Fasheh for (i = 0; i < cmp->num_pages; i++) { 3349f4414602SMark Fasheh pg = cmp->src_pages[i]; 3350e0bd70c6SFilipe Manana if (pg) { 3351e0bd70c6SFilipe Manana unlock_page(pg); 335209cbfeafSKirill A. Shutemov put_page(pg); 335397b19170SNaohiro Aota cmp->src_pages[i] = NULL; 3354e0bd70c6SFilipe Manana } 3355f4414602SMark Fasheh pg = cmp->dst_pages[i]; 3356e0bd70c6SFilipe Manana if (pg) { 3357e0bd70c6SFilipe Manana unlock_page(pg); 335809cbfeafSKirill A. Shutemov put_page(pg); 335997b19170SNaohiro Aota cmp->dst_pages[i] = NULL; 3360f4414602SMark Fasheh } 3361e0bd70c6SFilipe Manana } 3362f4414602SMark Fasheh } 3363f4414602SMark Fasheh 3364f4414602SMark Fasheh static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, 3365f4414602SMark Fasheh struct inode *dst, u64 dst_loff, 3366f4414602SMark Fasheh u64 len, struct cmp_pages *cmp) 3367f4414602SMark Fasheh { 3368f4414602SMark Fasheh int ret; 336909cbfeafSKirill A. Shutemov int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; 3370f4414602SMark Fasheh 3371f4414602SMark Fasheh cmp->num_pages = num_pages; 3372f4414602SMark Fasheh 337367b07bd4STimofey Titovets ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff); 3374f4414602SMark Fasheh if (ret) 3375f4414602SMark Fasheh goto out; 3376f4414602SMark Fasheh 337767b07bd4STimofey Titovets ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff); 3378f4414602SMark Fasheh 3379f4414602SMark Fasheh out: 3380f4414602SMark Fasheh if (ret) 3381f4414602SMark Fasheh btrfs_cmp_data_free(cmp); 338278ad4ce0SNaohiro Aota return ret; 3383416161dbSMark Fasheh } 3384416161dbSMark Fasheh 33851a287cfeSDavid Sterba static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) 3386416161dbSMark Fasheh { 3387416161dbSMark Fasheh int ret = 0; 3388f4414602SMark Fasheh int i; 3389416161dbSMark Fasheh struct page *src_page, *dst_page; 339009cbfeafSKirill A. Shutemov unsigned int cmp_len = PAGE_SIZE; 3391416161dbSMark Fasheh void *addr, *dst_addr; 3392416161dbSMark Fasheh 3393f4414602SMark Fasheh i = 0; 3394416161dbSMark Fasheh while (len) { 339509cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 3396416161dbSMark Fasheh cmp_len = len; 3397416161dbSMark Fasheh 3398f4414602SMark Fasheh BUG_ON(i >= cmp->num_pages); 3399f4414602SMark Fasheh 3400f4414602SMark Fasheh src_page = cmp->src_pages[i]; 3401f4414602SMark Fasheh dst_page = cmp->dst_pages[i]; 3402e0bd70c6SFilipe Manana ASSERT(PageLocked(src_page)); 3403e0bd70c6SFilipe Manana ASSERT(PageLocked(dst_page)); 3404f4414602SMark Fasheh 3405416161dbSMark Fasheh addr = kmap_atomic(src_page); 3406416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 3407416161dbSMark Fasheh 3408416161dbSMark Fasheh flush_dcache_page(src_page); 3409416161dbSMark Fasheh flush_dcache_page(dst_page); 3410416161dbSMark Fasheh 3411416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 34122b3909f8SDarrick J. Wong ret = -EBADE; 3413416161dbSMark Fasheh 3414416161dbSMark Fasheh kunmap_atomic(addr); 3415416161dbSMark Fasheh kunmap_atomic(dst_addr); 3416416161dbSMark Fasheh 3417416161dbSMark Fasheh if (ret) 3418416161dbSMark Fasheh break; 3419416161dbSMark Fasheh 3420416161dbSMark Fasheh len -= cmp_len; 3421f4414602SMark Fasheh i++; 3422416161dbSMark Fasheh } 3423416161dbSMark Fasheh 3424416161dbSMark Fasheh return ret; 3425416161dbSMark Fasheh } 3426416161dbSMark Fasheh 3427e1d227a4SMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, 3428e1d227a4SMark Fasheh u64 olen) 3429416161dbSMark Fasheh { 3430e1d227a4SMark Fasheh u64 len = *plen; 3431416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 3432416161dbSMark Fasheh 3433e1d227a4SMark Fasheh if (off + olen > inode->i_size || off + olen < off) 3434416161dbSMark Fasheh return -EINVAL; 3435e1d227a4SMark Fasheh 3436e1d227a4SMark Fasheh /* if we extend to eof, continue to block boundary */ 3437e1d227a4SMark Fasheh if (off + len == inode->i_size) 3438e1d227a4SMark Fasheh *plen = len = ALIGN(inode->i_size, bs) - off; 3439e1d227a4SMark Fasheh 3440416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 3441416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 3442416161dbSMark Fasheh return -EINVAL; 3443416161dbSMark Fasheh 3444416161dbSMark Fasheh return 0; 3445416161dbSMark Fasheh } 3446416161dbSMark Fasheh 34473973909dSTimofey Titovets static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen, 344867b07bd4STimofey Titovets struct inode *dst, u64 dst_loff, 344967b07bd4STimofey Titovets struct cmp_pages *cmp) 3450416161dbSMark Fasheh { 3451416161dbSMark Fasheh int ret; 3452e1d227a4SMark Fasheh u64 len = olen; 3453fc4badd9SOmar Sandoval bool same_inode = (src == dst); 34540efa9f48SMark Fasheh u64 same_lock_start = 0; 34550efa9f48SMark Fasheh u64 same_lock_len = 0; 3456416161dbSMark Fasheh 34570efa9f48SMark Fasheh ret = extent_same_check_offsets(src, loff, &len, olen); 34580efa9f48SMark Fasheh if (ret) 34593973909dSTimofey Titovets return ret; 3460fc4badd9SOmar Sandoval 3461fc4badd9SOmar Sandoval ret = extent_same_check_offsets(dst, dst_loff, &len, olen); 3462f4dfe687SFilipe Manana if (ret) 34633973909dSTimofey Titovets return ret; 34640efa9f48SMark Fasheh 3465fc4badd9SOmar Sandoval if (same_inode) { 34660efa9f48SMark Fasheh /* 34670efa9f48SMark Fasheh * Single inode case wants the same checks, except we 34680efa9f48SMark Fasheh * don't want our length pushed out past i_size as 34690efa9f48SMark Fasheh * comparing that data range makes no sense. 34700efa9f48SMark Fasheh * 34710efa9f48SMark Fasheh * extent_same_check_offsets() will do this for an 34720efa9f48SMark Fasheh * unaligned length at i_size, so catch it here and 34730efa9f48SMark Fasheh * reject the request. 34740efa9f48SMark Fasheh * 34750efa9f48SMark Fasheh * This effectively means we require aligned extents 34760efa9f48SMark Fasheh * for the single-inode case, whereas the other cases 34770efa9f48SMark Fasheh * allow an unaligned length so long as it ends at 34780efa9f48SMark Fasheh * i_size. 34790efa9f48SMark Fasheh */ 34803973909dSTimofey Titovets if (len != olen) 34813973909dSTimofey Titovets return -EINVAL; 34820efa9f48SMark Fasheh 34830efa9f48SMark Fasheh /* Check for overlapping ranges */ 34843973909dSTimofey Titovets if (dst_loff + len > loff && dst_loff < loff + len) 34853973909dSTimofey Titovets return -EINVAL; 34860efa9f48SMark Fasheh 34870efa9f48SMark Fasheh same_lock_start = min_t(u64, loff, dst_loff); 34880efa9f48SMark Fasheh same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; 3489de02b9f6SFilipe Manana } else { 3490de02b9f6SFilipe Manana /* 3491de02b9f6SFilipe Manana * If the source and destination inodes are different, the 3492de02b9f6SFilipe Manana * source's range end offset matches the source's i_size, that 3493de02b9f6SFilipe Manana * i_size is not a multiple of the sector size, and the 3494de02b9f6SFilipe Manana * destination range does not go past the destination's i_size, 3495de02b9f6SFilipe Manana * we must round down the length to the nearest sector size 3496de02b9f6SFilipe Manana * multiple. If we don't do this adjustment we end replacing 3497de02b9f6SFilipe Manana * with zeroes the bytes in the range that starts at the 3498de02b9f6SFilipe Manana * deduplication range's end offset and ends at the next sector 3499de02b9f6SFilipe Manana * size multiple. 3500de02b9f6SFilipe Manana */ 3501de02b9f6SFilipe Manana if (loff + olen == i_size_read(src) && 3502de02b9f6SFilipe Manana dst_loff + len < i_size_read(dst)) { 3503de02b9f6SFilipe Manana const u64 sz = BTRFS_I(src)->root->fs_info->sectorsize; 3504de02b9f6SFilipe Manana 3505de02b9f6SFilipe Manana len = round_down(i_size_read(src), sz) - loff; 350611023d3fSFilipe Manana if (len == 0) 350711023d3fSFilipe Manana return 0; 3508de02b9f6SFilipe Manana olen = len; 3509de02b9f6SFilipe Manana } 35100efa9f48SMark Fasheh } 3511416161dbSMark Fasheh 3512e0bd70c6SFilipe Manana again: 351367b07bd4STimofey Titovets ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp); 3514f4414602SMark Fasheh if (ret) 35153973909dSTimofey Titovets return ret; 3516f4414602SMark Fasheh 35170efa9f48SMark Fasheh if (same_inode) 3518e0bd70c6SFilipe Manana ret = lock_extent_range(src, same_lock_start, same_lock_len, 3519e0bd70c6SFilipe Manana false); 35200efa9f48SMark Fasheh else 3521e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, 3522e0bd70c6SFilipe Manana false); 3523e0bd70c6SFilipe Manana /* 3524e0bd70c6SFilipe Manana * If one of the inodes has dirty pages in the respective range or 3525e0bd70c6SFilipe Manana * ordered extents, we need to flush dellaloc and wait for all ordered 3526e0bd70c6SFilipe Manana * extents in the range. We must unlock the pages and the ranges in the 3527e0bd70c6SFilipe Manana * io trees to avoid deadlocks when flushing delalloc (requires locking 3528e0bd70c6SFilipe Manana * pages) and when waiting for ordered extents to complete (they require 3529e0bd70c6SFilipe Manana * range locking). 3530e0bd70c6SFilipe Manana */ 3531e0bd70c6SFilipe Manana if (ret == -EAGAIN) { 3532e0bd70c6SFilipe Manana /* 3533e0bd70c6SFilipe Manana * Ranges in the io trees already unlocked. Now unlock all 3534e0bd70c6SFilipe Manana * pages before waiting for all IO to complete. 3535e0bd70c6SFilipe Manana */ 353667b07bd4STimofey Titovets btrfs_cmp_data_free(cmp); 3537e0bd70c6SFilipe Manana if (same_inode) { 3538e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, same_lock_start, 3539e0bd70c6SFilipe Manana same_lock_len); 3540e0bd70c6SFilipe Manana } else { 3541e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, loff, len); 3542e0bd70c6SFilipe Manana btrfs_wait_ordered_range(dst, dst_loff, len); 3543e0bd70c6SFilipe Manana } 3544e0bd70c6SFilipe Manana goto again; 3545e0bd70c6SFilipe Manana } 3546e0bd70c6SFilipe Manana ASSERT(ret == 0); 3547e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3548e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 354967b07bd4STimofey Titovets btrfs_cmp_data_free(cmp); 3550e0bd70c6SFilipe Manana return ret; 3551e0bd70c6SFilipe Manana } 3552f4414602SMark Fasheh 3553207910ddSMark Fasheh /* pass original length for comparison so we stay within i_size */ 355467b07bd4STimofey Titovets ret = btrfs_cmp_data(olen, cmp); 3555416161dbSMark Fasheh if (ret == 0) 35561c919a5eSMark Fasheh ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); 3557416161dbSMark Fasheh 35580efa9f48SMark Fasheh if (same_inode) 35590efa9f48SMark Fasheh unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, 35600efa9f48SMark Fasheh same_lock_start + same_lock_len - 1); 35610efa9f48SMark Fasheh else 3562f4414602SMark Fasheh btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 3563f4414602SMark Fasheh 356467b07bd4STimofey Titovets btrfs_cmp_data_free(cmp); 35653973909dSTimofey Titovets 35663973909dSTimofey Titovets return ret; 35673973909dSTimofey Titovets } 35683973909dSTimofey Titovets 3569b6728768STimofey Titovets #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3570b6728768STimofey Titovets 35713973909dSTimofey Titovets static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 35723973909dSTimofey Titovets struct inode *dst, u64 dst_loff) 35733973909dSTimofey Titovets { 35743973909dSTimofey Titovets int ret; 357567b07bd4STimofey Titovets struct cmp_pages cmp; 357667b07bd4STimofey Titovets int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT; 35773973909dSTimofey Titovets bool same_inode = (src == dst); 3578b6728768STimofey Titovets u64 i, tail_len, chunk_count; 35793973909dSTimofey Titovets 35803973909dSTimofey Titovets if (olen == 0) 35813973909dSTimofey Titovets return 0; 35823973909dSTimofey Titovets 35833973909dSTimofey Titovets if (same_inode) 35843973909dSTimofey Titovets inode_lock(src); 35853973909dSTimofey Titovets else 35863973909dSTimofey Titovets btrfs_double_inode_lock(src, dst); 35873973909dSTimofey Titovets 35883973909dSTimofey Titovets /* don't make the dst file partly checksummed */ 35893973909dSTimofey Titovets if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 35903973909dSTimofey Titovets (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 35913973909dSTimofey Titovets ret = -EINVAL; 35923973909dSTimofey Titovets goto out_unlock; 35933973909dSTimofey Titovets } 35943973909dSTimofey Titovets 3595eede2bf3SOmar Sandoval if (IS_SWAPFILE(src) || IS_SWAPFILE(dst)) { 3596eede2bf3SOmar Sandoval ret = -ETXTBSY; 3597eede2bf3SOmar Sandoval goto out_unlock; 3598eede2bf3SOmar Sandoval } 3599eede2bf3SOmar Sandoval 3600b6728768STimofey Titovets tail_len = olen % BTRFS_MAX_DEDUPE_LEN; 3601b6728768STimofey Titovets chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); 360267b07bd4STimofey Titovets if (chunk_count == 0) 360367b07bd4STimofey Titovets num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT; 360467b07bd4STimofey Titovets 360567b07bd4STimofey Titovets /* 360667b07bd4STimofey Titovets * If deduping ranges in the same inode, locking rules make it 360767b07bd4STimofey Titovets * mandatory to always lock pages in ascending order to avoid deadlocks 360867b07bd4STimofey Titovets * with concurrent tasks (such as starting writeback/delalloc). 360967b07bd4STimofey Titovets */ 361067b07bd4STimofey Titovets if (same_inode && dst_loff < loff) 361167b07bd4STimofey Titovets swap(loff, dst_loff); 361267b07bd4STimofey Titovets 361367b07bd4STimofey Titovets /* 361467b07bd4STimofey Titovets * We must gather up all the pages before we initiate our extent 361567b07bd4STimofey Titovets * locking. We use an array for the page pointers. Size of the array is 361667b07bd4STimofey Titovets * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN. 361767b07bd4STimofey Titovets */ 3618bf5091c8SDavid Sterba cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *), 3619bf5091c8SDavid Sterba GFP_KERNEL | __GFP_ZERO); 3620bf5091c8SDavid Sterba cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *), 3621bf5091c8SDavid Sterba GFP_KERNEL | __GFP_ZERO); 362267b07bd4STimofey Titovets if (!cmp.src_pages || !cmp.dst_pages) { 3623bf5091c8SDavid Sterba ret = -ENOMEM; 3624bf5091c8SDavid Sterba goto out_free; 362567b07bd4STimofey Titovets } 3626b6728768STimofey Titovets 3627b6728768STimofey Titovets for (i = 0; i < chunk_count; i++) { 3628b6728768STimofey Titovets ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, 362967b07bd4STimofey Titovets dst, dst_loff, &cmp); 3630b6728768STimofey Titovets if (ret) 363122883ddcSLu Fengqi goto out_free; 3632b6728768STimofey Titovets 3633b6728768STimofey Titovets loff += BTRFS_MAX_DEDUPE_LEN; 3634b6728768STimofey Titovets dst_loff += BTRFS_MAX_DEDUPE_LEN; 3635b6728768STimofey Titovets } 3636b6728768STimofey Titovets 3637b6728768STimofey Titovets if (tail_len > 0) 363867b07bd4STimofey Titovets ret = btrfs_extent_same_range(src, loff, tail_len, dst, 363967b07bd4STimofey Titovets dst_loff, &cmp); 36403973909dSTimofey Titovets 364122883ddcSLu Fengqi out_free: 364222883ddcSLu Fengqi kvfree(cmp.src_pages); 364322883ddcSLu Fengqi kvfree(cmp.dst_pages); 364422883ddcSLu Fengqi 3645416161dbSMark Fasheh out_unlock: 36460efa9f48SMark Fasheh if (same_inode) 36475955102cSAl Viro inode_unlock(src); 36480efa9f48SMark Fasheh else 3649f4414602SMark Fasheh btrfs_double_inode_unlock(src, dst); 3650416161dbSMark Fasheh 3651416161dbSMark Fasheh return ret; 3652416161dbSMark Fasheh } 3653416161dbSMark Fasheh 3654f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3655f82a9901SFilipe Manana struct inode *inode, 3656f82a9901SFilipe Manana u64 endoff, 3657f82a9901SFilipe Manana const u64 destoff, 36581c919a5eSMark Fasheh const u64 olen, 36591c919a5eSMark Fasheh int no_time_update) 3660f82a9901SFilipe Manana { 3661f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3662f82a9901SFilipe Manana int ret; 3663f82a9901SFilipe Manana 3664f82a9901SFilipe Manana inode_inc_iversion(inode); 36651c919a5eSMark Fasheh if (!no_time_update) 3666c2050a45SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 3667f82a9901SFilipe Manana /* 3668f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3669f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3670f82a9901SFilipe Manana */ 3671f82a9901SFilipe Manana if (endoff > destoff + olen) 3672f82a9901SFilipe Manana endoff = destoff + olen; 3673f82a9901SFilipe Manana if (endoff > inode->i_size) 36746ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(inode), endoff); 3675f82a9901SFilipe Manana 3676f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3677f82a9901SFilipe Manana if (ret) { 367866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 36793a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3680f82a9901SFilipe Manana goto out; 3681f82a9901SFilipe Manana } 36823a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 3683f82a9901SFilipe Manana out: 3684f82a9901SFilipe Manana return ret; 3685f82a9901SFilipe Manana } 3686f82a9901SFilipe Manana 3687a2f392e4SNikolay Borisov static void clone_update_extent_map(struct btrfs_inode *inode, 36887ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 36897ffbb598SFilipe Manana const struct btrfs_path *path, 36907ffbb598SFilipe Manana const u64 hole_offset, 36917ffbb598SFilipe Manana const u64 hole_len) 36927ffbb598SFilipe Manana { 3693a2f392e4SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 36947ffbb598SFilipe Manana struct extent_map *em; 36957ffbb598SFilipe Manana int ret; 36967ffbb598SFilipe Manana 36977ffbb598SFilipe Manana em = alloc_extent_map(); 36987ffbb598SFilipe Manana if (!em) { 3699a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 37007ffbb598SFilipe Manana return; 37017ffbb598SFilipe Manana } 37027ffbb598SFilipe Manana 370314f59796SFilipe Manana if (path) { 370414f59796SFilipe Manana struct btrfs_file_extent_item *fi; 370514f59796SFilipe Manana 370614f59796SFilipe Manana fi = btrfs_item_ptr(path->nodes[0], path->slots[0], 370714f59796SFilipe Manana struct btrfs_file_extent_item); 3708a2f392e4SNikolay Borisov btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 37097ffbb598SFilipe Manana em->generation = -1; 37107ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 37117ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 37127ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 3713a2f392e4SNikolay Borisov &inode->runtime_flags); 37147ffbb598SFilipe Manana } else { 37157ffbb598SFilipe Manana em->start = hole_offset; 37167ffbb598SFilipe Manana em->len = hole_len; 37177ffbb598SFilipe Manana em->ram_bytes = em->len; 37187ffbb598SFilipe Manana em->orig_start = hole_offset; 37197ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 37207ffbb598SFilipe Manana em->block_len = 0; 37217ffbb598SFilipe Manana em->orig_block_len = 0; 37227ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 37237ffbb598SFilipe Manana em->generation = trans->transid; 37247ffbb598SFilipe Manana } 37257ffbb598SFilipe Manana 37267ffbb598SFilipe Manana while (1) { 37277ffbb598SFilipe Manana write_lock(&em_tree->lock); 37287ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 37297ffbb598SFilipe Manana write_unlock(&em_tree->lock); 37307ffbb598SFilipe Manana if (ret != -EEXIST) { 37317ffbb598SFilipe Manana free_extent_map(em); 37327ffbb598SFilipe Manana break; 37337ffbb598SFilipe Manana } 3734a2f392e4SNikolay Borisov btrfs_drop_extent_cache(inode, em->start, 37357ffbb598SFilipe Manana em->start + em->len - 1, 0); 37367ffbb598SFilipe Manana } 37377ffbb598SFilipe Manana 3738ee39b432SDavid Sterba if (ret) 3739a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 37407ffbb598SFilipe Manana } 37417ffbb598SFilipe Manana 37428039d87dSFilipe Manana /* 37438039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 37448039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 37458039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 37468039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 37478039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 37488039d87dSFilipe Manana * 37498039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 37508039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 37518039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 37528039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 37538039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 37548039d87dSFilipe Manana * i_mutex, which we are holding here). 37558039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 37568039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 37578039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 37588039d87dSFilipe Manana * 37598039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 37608039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 37618039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 37628039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 37638039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 37648039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 37658039d87dSFilipe Manana * the former. 37668039d87dSFilipe Manana */ 37674a0ab9d7SDavid Sterba static int clone_copy_inline_extent(struct inode *dst, 37688039d87dSFilipe Manana struct btrfs_trans_handle *trans, 37698039d87dSFilipe Manana struct btrfs_path *path, 37708039d87dSFilipe Manana struct btrfs_key *new_key, 37718039d87dSFilipe Manana const u64 drop_start, 37728039d87dSFilipe Manana const u64 datal, 37738039d87dSFilipe Manana const u64 skip, 37748039d87dSFilipe Manana const u64 size, 37758039d87dSFilipe Manana char *inline_data) 37768039d87dSFilipe Manana { 37770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); 37788039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 37798039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 37800b246afaSJeff Mahoney fs_info->sectorsize); 37818039d87dSFilipe Manana int ret; 37828039d87dSFilipe Manana struct btrfs_key key; 37838039d87dSFilipe Manana 37848039d87dSFilipe Manana if (new_key->offset > 0) 37858039d87dSFilipe Manana return -EOPNOTSUPP; 37868039d87dSFilipe Manana 37874a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(dst)); 37888039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 37898039d87dSFilipe Manana key.offset = 0; 37908039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 37918039d87dSFilipe Manana if (ret < 0) { 37928039d87dSFilipe Manana return ret; 37938039d87dSFilipe Manana } else if (ret > 0) { 37948039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 37958039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 37968039d87dSFilipe Manana if (ret < 0) 37978039d87dSFilipe Manana return ret; 37988039d87dSFilipe Manana else if (ret > 0) 37998039d87dSFilipe Manana goto copy_inline_extent; 38008039d87dSFilipe Manana } 38018039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 38024a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 38038039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 38048039d87dSFilipe Manana ASSERT(key.offset > 0); 38058039d87dSFilipe Manana return -EOPNOTSUPP; 38068039d87dSFilipe Manana } 38078039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 38088039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 38098039d87dSFilipe Manana u64 ext_len; 38108039d87dSFilipe Manana 38118039d87dSFilipe Manana /* 38128039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 38138039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 38148039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 38158039d87dSFilipe Manana */ 38168039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 38178039d87dSFilipe Manana struct btrfs_file_extent_item); 38188039d87dSFilipe Manana /* 38198039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 38208039d87dSFilipe Manana * following it. 38218039d87dSFilipe Manana */ 38228039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 38238039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 38248039d87dSFilipe Manana goto copy_inline_extent; 38258039d87dSFilipe Manana 38268039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 38278039d87dSFilipe Manana if (ext_len > aligned_end) 38288039d87dSFilipe Manana return -EOPNOTSUPP; 38298039d87dSFilipe Manana 38308039d87dSFilipe Manana ret = btrfs_next_item(root, path); 38318039d87dSFilipe Manana if (ret < 0) { 38328039d87dSFilipe Manana return ret; 38338039d87dSFilipe Manana } else if (ret == 0) { 38348039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 38358039d87dSFilipe Manana path->slots[0]); 38364a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 38378039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 38388039d87dSFilipe Manana return -EOPNOTSUPP; 38398039d87dSFilipe Manana } 38408039d87dSFilipe Manana } 38418039d87dSFilipe Manana 38428039d87dSFilipe Manana copy_inline_extent: 38438039d87dSFilipe Manana /* 38448039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 38458039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 38468039d87dSFilipe Manana */ 38478039d87dSFilipe Manana if (i_size_read(dst) > datal) { 38488039d87dSFilipe Manana /* 38498039d87dSFilipe Manana * If the destination inode has an inline extent... 38508039d87dSFilipe Manana * This would require copying the data from the source inline 38518039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 38528039d87dSFilipe Manana * But this is really complex, both extents can be compressed 38538039d87dSFilipe Manana * or just one of them, which would require decompressing and 38548039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 38558039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 38568039d87dSFilipe Manana * inline extent). 38578039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 38588039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 38598039d87dSFilipe Manana */ 38608039d87dSFilipe Manana return -EOPNOTSUPP; 38618039d87dSFilipe Manana } 38628039d87dSFilipe Manana 38638039d87dSFilipe Manana btrfs_release_path(path); 38648039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 38658039d87dSFilipe Manana if (ret) 38668039d87dSFilipe Manana return ret; 38678039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 38688039d87dSFilipe Manana if (ret) 38698039d87dSFilipe Manana return ret; 38708039d87dSFilipe Manana 38718039d87dSFilipe Manana if (skip) { 38728039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 38738039d87dSFilipe Manana 38748039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 38758039d87dSFilipe Manana } 38768039d87dSFilipe Manana 38778039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 38788039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 38798039d87dSFilipe Manana path->slots[0]), 38808039d87dSFilipe Manana size); 38818039d87dSFilipe Manana inode_add_bytes(dst, datal); 38828039d87dSFilipe Manana 38838039d87dSFilipe Manana return 0; 38848039d87dSFilipe Manana } 38858039d87dSFilipe Manana 388632b7c687SMark Fasheh /** 388732b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 388832b7c687SMark Fasheh * 388932b7c687SMark Fasheh * @src: Inode to clone from 389032b7c687SMark Fasheh * @inode: Inode to clone to 389132b7c687SMark Fasheh * @off: Offset within source to start clone from 389232b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 38931c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 389432b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 38951c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 389632b7c687SMark Fasheh */ 389732b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3898f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 38991c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3900f46b5a66SChristoph Hellwig { 39010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3902f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 390332b7c687SMark Fasheh struct btrfs_path *path = NULL; 3904f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 390532b7c687SMark Fasheh struct btrfs_trans_handle *trans; 390632b7c687SMark Fasheh char *buf = NULL; 3907ae01a0abSYan Zheng struct btrfs_key key; 3908f46b5a66SChristoph Hellwig u32 nritems; 3909f46b5a66SChristoph Hellwig int slot; 3910ae01a0abSYan Zheng int ret; 3911f82a9901SFilipe Manana const u64 len = olen_aligned; 3912f82a9901SFilipe Manana u64 last_dest_end = destoff; 3913ae01a0abSYan Zheng 3914ae01a0abSYan Zheng ret = -ENOMEM; 3915752ade68SMichal Hocko buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 3916ae01a0abSYan Zheng if (!buf) 391732b7c687SMark Fasheh return ret; 3918ae01a0abSYan Zheng 3919ae01a0abSYan Zheng path = btrfs_alloc_path(); 3920ae01a0abSYan Zheng if (!path) { 392115351955SDavid Sterba kvfree(buf); 392232b7c687SMark Fasheh return ret; 3923ae01a0abSYan Zheng } 392432b7c687SMark Fasheh 3925e4058b54SDavid Sterba path->reada = READA_FORWARD; 3926c5c9cd4dSSage Weil /* clone data */ 39274a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(src)); 3928ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 39292c463823SFilipe Manana key.offset = off; 3930f46b5a66SChristoph Hellwig 3931f46b5a66SChristoph Hellwig while (1) { 3932de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3933df858e76SFilipe Manana 3934f46b5a66SChristoph Hellwig /* 3935f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3936f46b5a66SChristoph Hellwig * tree. 3937f46b5a66SChristoph Hellwig */ 3938e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3939362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3940362a20c5SDavid Sterba 0, 0); 3941f46b5a66SChristoph Hellwig if (ret < 0) 3942f46b5a66SChristoph Hellwig goto out; 39432c463823SFilipe Manana /* 39442c463823SFilipe Manana * First search, if no extent item that starts at offset off was 39452c463823SFilipe Manana * found but the previous item is an extent item, it's possible 39462c463823SFilipe Manana * it might overlap our target range, therefore process it. 39472c463823SFilipe Manana */ 39482c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 39492c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 39502c463823SFilipe Manana path->slots[0] - 1); 39512c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 39522c463823SFilipe Manana path->slots[0]--; 39532c463823SFilipe Manana } 3954f46b5a66SChristoph Hellwig 3955ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3956e4355f34SFilipe David Borba Manana process_slot: 3957ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3958362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3959f46b5a66SChristoph Hellwig if (ret < 0) 3960f46b5a66SChristoph Hellwig goto out; 3961f46b5a66SChristoph Hellwig if (ret > 0) 3962f46b5a66SChristoph Hellwig break; 3963ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3964f46b5a66SChristoph Hellwig } 3965f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3966f46b5a66SChristoph Hellwig slot = path->slots[0]; 3967f46b5a66SChristoph Hellwig 3968ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3969962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 39704a0cc7caSNikolay Borisov key.objectid != btrfs_ino(BTRFS_I(src))) 3971f46b5a66SChristoph Hellwig break; 3972f46b5a66SChristoph Hellwig 3973962a298fSDavid Sterba if (key.type == BTRFS_EXTENT_DATA_KEY) { 3974c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3975c5c9cd4dSSage Weil int type; 397631840ae1SZheng Yan u32 size; 397731840ae1SZheng Yan struct btrfs_key new_key; 3978c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3979c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3980c5c9cd4dSSage Weil u8 comp; 3981f82a9901SFilipe Manana u64 drop_start; 398231840ae1SZheng Yan 3983c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3984c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3985c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3986c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3987c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3988c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3989d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3990d397712bSChris Mason extent); 3991d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3992d397712bSChris Mason extent); 3993c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3994d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3995d397712bSChris Mason extent); 3996c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3997c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3998c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3999c5c9cd4dSSage Weil extent); 4000c5c9cd4dSSage Weil } 400131840ae1SZheng Yan 40022c463823SFilipe Manana /* 40032c463823SFilipe Manana * The first search might have left us at an extent 40042c463823SFilipe Manana * item that ends before our target range's start, can 40052c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 40062c463823SFilipe Manana */ 40072c463823SFilipe Manana if (key.offset + datal <= off) { 4008e4355f34SFilipe David Borba Manana path->slots[0]++; 4009e4355f34SFilipe David Borba Manana goto process_slot; 40102c463823SFilipe Manana } else if (key.offset >= off + len) { 40112c463823SFilipe Manana break; 4012e4355f34SFilipe David Borba Manana } 4013df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 4014e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 4015e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 4016e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 4017e4355f34SFilipe David Borba Manana size); 4018e4355f34SFilipe David Borba Manana 4019e4355f34SFilipe David Borba Manana btrfs_release_path(path); 4020e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 4021c5c9cd4dSSage Weil 402231840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 40234a0cc7caSNikolay Borisov new_key.objectid = btrfs_ino(BTRFS_I(inode)); 40244d728ec7SLi Zefan if (off <= key.offset) 4025c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 40264d728ec7SLi Zefan else 40274d728ec7SLi Zefan new_key.offset = destoff; 4028c5c9cd4dSSage Weil 4029b6f3409bSSage Weil /* 4030f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 4031f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 4032f82a9901SFilipe Manana * This hole is either in the middle of the cloning 4033f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 4034f82a9901SFilipe Manana * partially overlaps it). 4035f82a9901SFilipe Manana */ 4036f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 4037f82a9901SFilipe Manana drop_start = last_dest_end; 4038f82a9901SFilipe Manana else 4039f82a9901SFilipe Manana drop_start = new_key.offset; 4040f82a9901SFilipe Manana 4041f82a9901SFilipe Manana /* 4042b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 4043b6f3409bSSage Weil * 1 - add new extent 4044b6f3409bSSage Weil * 1 - inode update 4045b6f3409bSSage Weil */ 4046b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 4047a22285a6SYan, Zheng if (IS_ERR(trans)) { 4048a22285a6SYan, Zheng ret = PTR_ERR(trans); 4049a22285a6SYan, Zheng goto out; 4050a22285a6SYan, Zheng } 4051a22285a6SYan, Zheng 4052c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 4053c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 4054d72c0842SLi Zefan /* 4055d72c0842SLi Zefan * a | --- range to clone ---| b 4056d72c0842SLi Zefan * | ------------- extent ------------- | 4057d72c0842SLi Zefan */ 4058d72c0842SLi Zefan 405993915584SAntonio Ospite /* subtract range b */ 4060d72c0842SLi Zefan if (key.offset + datal > off + len) 4061d72c0842SLi Zefan datal = off + len - key.offset; 4062d72c0842SLi Zefan 406393915584SAntonio Ospite /* subtract range a */ 4064a22285a6SYan, Zheng if (off > key.offset) { 4065a22285a6SYan, Zheng datao += off - key.offset; 4066a22285a6SYan, Zheng datal -= off - key.offset; 4067a22285a6SYan, Zheng } 4068a22285a6SYan, Zheng 40695dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 4070f82a9901SFilipe Manana drop_start, 4071a22285a6SYan, Zheng new_key.offset + datal, 40722671485dSJosef Bacik 1); 407379787eaaSJeff Mahoney if (ret) { 40743f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 407500fdf13aSLiu Bo btrfs_abort_transaction(trans, 407666642832SJeff Mahoney ret); 40773a45bb20SJeff Mahoney btrfs_end_transaction(trans); 407879787eaaSJeff Mahoney goto out; 407979787eaaSJeff Mahoney } 4080a22285a6SYan, Zheng 408131840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 408231840ae1SZheng Yan &new_key, size); 408379787eaaSJeff Mahoney if (ret) { 408466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 40853a45bb20SJeff Mahoney btrfs_end_transaction(trans); 408679787eaaSJeff Mahoney goto out; 408779787eaaSJeff Mahoney } 408831840ae1SZheng Yan 408931840ae1SZheng Yan leaf = path->nodes[0]; 409031840ae1SZheng Yan slot = path->slots[0]; 409131840ae1SZheng Yan write_extent_buffer(leaf, buf, 409231840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 409331840ae1SZheng Yan size); 4094ae01a0abSYan Zheng 4095f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 4096f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 4097c5c9cd4dSSage Weil 4098c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 4099c5c9cd4dSSage Weil if (!disko) 4100c5c9cd4dSSage Weil datao = 0; 4101c5c9cd4dSSage Weil 4102c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 4103c5c9cd4dSSage Weil datao); 4104c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 4105c5c9cd4dSSage Weil datal); 4106fcebe456SJosef Bacik 4107c5c9cd4dSSage Weil if (disko) { 4108c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 41092ff7e61eSJeff Mahoney ret = btrfs_inc_extent_ref(trans, 411084f7d8e6SJosef Bacik root, 41115d4f98a2SYan Zheng disko, diskl, 0, 4112f46b5a66SChristoph Hellwig root->root_key.objectid, 41134a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), 4114b06c4bf5SFilipe Manana new_key.offset - datao); 411579787eaaSJeff Mahoney if (ret) { 411679787eaaSJeff Mahoney btrfs_abort_transaction(trans, 411779787eaaSJeff Mahoney ret); 41183a45bb20SJeff Mahoney btrfs_end_transaction(trans); 411979787eaaSJeff Mahoney goto out; 412079787eaaSJeff Mahoney 412179787eaaSJeff Mahoney } 4122f46b5a66SChristoph Hellwig } 4123c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 4124c5c9cd4dSSage Weil u64 skip = 0; 4125c5c9cd4dSSage Weil u64 trim = 0; 4126ed958762SFilipe Manana 4127c5c9cd4dSSage Weil if (off > key.offset) { 4128c5c9cd4dSSage Weil skip = off - key.offset; 4129c5c9cd4dSSage Weil new_key.offset += skip; 413031840ae1SZheng Yan } 4131d397712bSChris Mason 4132c5c9cd4dSSage Weil if (key.offset + datal > off + len) 4133c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 4134d397712bSChris Mason 4135c5c9cd4dSSage Weil if (comp && (skip || trim)) { 4136c5c9cd4dSSage Weil ret = -EINVAL; 41373a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4138c5c9cd4dSSage Weil goto out; 413931840ae1SZheng Yan } 4140c5c9cd4dSSage Weil size -= skip + trim; 4141c5c9cd4dSSage Weil datal -= skip + trim; 4142a22285a6SYan, Zheng 41434a0ab9d7SDavid Sterba ret = clone_copy_inline_extent(inode, 41448039d87dSFilipe Manana trans, path, 41458039d87dSFilipe Manana &new_key, 4146f82a9901SFilipe Manana drop_start, 41478039d87dSFilipe Manana datal, 41488039d87dSFilipe Manana skip, size, buf); 414979787eaaSJeff Mahoney if (ret) { 41503f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 41513a29bc09SChris Mason btrfs_abort_transaction(trans, 415279787eaaSJeff Mahoney ret); 41533a45bb20SJeff Mahoney btrfs_end_transaction(trans); 415479787eaaSJeff Mahoney goto out; 415579787eaaSJeff Mahoney } 4156c5c9cd4dSSage Weil leaf = path->nodes[0]; 4157c5c9cd4dSSage Weil slot = path->slots[0]; 4158c5c9cd4dSSage Weil } 4159c5c9cd4dSSage Weil 41607ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 41617ffbb598SFilipe Manana if (drop_start < new_key.offset) 4162a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 416314f59796SFilipe Manana NULL, drop_start, 41647ffbb598SFilipe Manana new_key.offset - drop_start); 41657ffbb598SFilipe Manana 4166a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 4167a2f392e4SNikolay Borisov path, 0, 0); 41687ffbb598SFilipe Manana 4169c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 4170b3b4aa74SDavid Sterba btrfs_release_path(path); 4171c5c9cd4dSSage Weil 417262e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 41730b246afaSJeff Mahoney fs_info->sectorsize); 4174f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 4175f82a9901SFilipe Manana last_dest_end, 41761c919a5eSMark Fasheh destoff, olen, 41771c919a5eSMark Fasheh no_time_update); 4178f82a9901SFilipe Manana if (ret) 417979787eaaSJeff Mahoney goto out; 41802c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 41812c463823SFilipe Manana break; 4182a22285a6SYan, Zheng } 4183b3b4aa74SDavid Sterba btrfs_release_path(path); 4184df858e76SFilipe Manana key.offset = next_key_min_offset; 418569ae5e44SWang Xiaoguang 418669ae5e44SWang Xiaoguang if (fatal_signal_pending(current)) { 418769ae5e44SWang Xiaoguang ret = -EINTR; 418869ae5e44SWang Xiaoguang goto out; 418969ae5e44SWang Xiaoguang } 4190ae01a0abSYan Zheng } 4191f46b5a66SChristoph Hellwig ret = 0; 419232b7c687SMark Fasheh 4193f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 4194f82a9901SFilipe Manana /* 4195f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 4196f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 4197f82a9901SFilipe Manana */ 4198b3b4aa74SDavid Sterba btrfs_release_path(path); 4199f82a9901SFilipe Manana 4200f82a9901SFilipe Manana /* 4201f82a9901SFilipe Manana * 1 - remove extent(s) 4202f82a9901SFilipe Manana * 1 - inode update 4203f82a9901SFilipe Manana */ 4204f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 4205f82a9901SFilipe Manana if (IS_ERR(trans)) { 4206f82a9901SFilipe Manana ret = PTR_ERR(trans); 4207f82a9901SFilipe Manana goto out; 4208f82a9901SFilipe Manana } 4209f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 4210f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 4211f82a9901SFilipe Manana if (ret) { 4212f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 421366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 42143a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4215f82a9901SFilipe Manana goto out; 4216f82a9901SFilipe Manana } 4217a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, NULL, 4218a2f392e4SNikolay Borisov last_dest_end, 421914f59796SFilipe Manana destoff + len - last_dest_end); 4220f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 42211c919a5eSMark Fasheh destoff, olen, no_time_update); 4222f82a9901SFilipe Manana } 4223f82a9901SFilipe Manana 4224f46b5a66SChristoph Hellwig out: 422532b7c687SMark Fasheh btrfs_free_path(path); 422615351955SDavid Sterba kvfree(buf); 422732b7c687SMark Fasheh return ret; 422832b7c687SMark Fasheh } 422932b7c687SMark Fasheh 42303db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 423132b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 423232b7c687SMark Fasheh { 423354563d41SAl Viro struct inode *inode = file_inode(file); 42343db11b2eSZach Brown struct inode *src = file_inode(file_src); 42350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 423632b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 423732b7c687SMark Fasheh int ret; 423832b7c687SMark Fasheh u64 len = olen; 42390b246afaSJeff Mahoney u64 bs = fs_info->sb->s_blocksize; 42403db11b2eSZach Brown int same_inode = src == inode; 424132b7c687SMark Fasheh 424232b7c687SMark Fasheh /* 424332b7c687SMark Fasheh * TODO: 424432b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 424532b7c687SMark Fasheh * decompress into destination's address_space (the file offset 424632b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 424732b7c687SMark Fasheh * otherwise reinsert) a subrange. 424800fdf13aSLiu Bo * 424900fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 425000fdf13aSLiu Bo * be either compressed or non-compressed. 425132b7c687SMark Fasheh */ 425232b7c687SMark Fasheh 425332b7c687SMark Fasheh if (btrfs_root_readonly(root)) 425432b7c687SMark Fasheh return -EROFS; 425532b7c687SMark Fasheh 42563db11b2eSZach Brown if (file_src->f_path.mnt != file->f_path.mnt || 42573db11b2eSZach Brown src->i_sb != inode->i_sb) 42583db11b2eSZach Brown return -EXDEV; 425932b7c687SMark Fasheh 426032b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 42613db11b2eSZach Brown return -EISDIR; 426232b7c687SMark Fasheh 426332b7c687SMark Fasheh if (!same_inode) { 4264293a8489SMark Fasheh btrfs_double_inode_lock(src, inode); 426532b7c687SMark Fasheh } else { 42665955102cSAl Viro inode_lock(src); 426732b7c687SMark Fasheh } 426832b7c687SMark Fasheh 4269b5c40d59SOmar Sandoval /* don't make the dst file partly checksummed */ 4270b5c40d59SOmar Sandoval if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 4271b5c40d59SOmar Sandoval (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 4272b5c40d59SOmar Sandoval ret = -EINVAL; 4273b5c40d59SOmar Sandoval goto out_unlock; 4274b5c40d59SOmar Sandoval } 4275b5c40d59SOmar Sandoval 4276eede2bf3SOmar Sandoval if (IS_SWAPFILE(src) || IS_SWAPFILE(inode)) { 4277eede2bf3SOmar Sandoval ret = -ETXTBSY; 4278eede2bf3SOmar Sandoval goto out_unlock; 4279eede2bf3SOmar Sandoval } 4280eede2bf3SOmar Sandoval 428132b7c687SMark Fasheh /* determine range to clone */ 428232b7c687SMark Fasheh ret = -EINVAL; 428332b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 428432b7c687SMark Fasheh goto out_unlock; 428532b7c687SMark Fasheh if (len == 0) 428632b7c687SMark Fasheh olen = len = src->i_size - off; 4287ac765f83SFilipe Manana /* 4288ac765f83SFilipe Manana * If we extend to eof, continue to block boundary if and only if the 4289ac765f83SFilipe Manana * destination end offset matches the destination file's size, otherwise 4290ac765f83SFilipe Manana * we would be corrupting data by placing the eof block into the middle 4291ac765f83SFilipe Manana * of a file. 4292ac765f83SFilipe Manana */ 4293ac765f83SFilipe Manana if (off + len == src->i_size) { 4294ac765f83SFilipe Manana if (!IS_ALIGNED(len, bs) && destoff + len < inode->i_size) 4295ac765f83SFilipe Manana goto out_unlock; 429632b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 4297ac765f83SFilipe Manana } 429832b7c687SMark Fasheh 4299ccccf3d6SFilipe Manana if (len == 0) { 4300ccccf3d6SFilipe Manana ret = 0; 4301ccccf3d6SFilipe Manana goto out_unlock; 4302ccccf3d6SFilipe Manana } 4303ccccf3d6SFilipe Manana 430432b7c687SMark Fasheh /* verify the end result is block aligned */ 430532b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 430632b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 430732b7c687SMark Fasheh goto out_unlock; 430832b7c687SMark Fasheh 430932b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 431032b7c687SMark Fasheh if (same_inode) { 431132b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 431232b7c687SMark Fasheh goto out_unlock; 431332b7c687SMark Fasheh } 431432b7c687SMark Fasheh 431532b7c687SMark Fasheh if (destoff > inode->i_size) { 431632b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 431732b7c687SMark Fasheh if (ret) 431832b7c687SMark Fasheh goto out_unlock; 431932b7c687SMark Fasheh } 432032b7c687SMark Fasheh 4321c125b8bfSFilipe Manana /* 4322c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 4323c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 4324c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 4325c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 4326c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 4327c125b8bfSFilipe Manana */ 4328c125b8bfSFilipe Manana if (same_inode) { 4329c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 4330c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 433132b7c687SMark Fasheh 4332e0bd70c6SFilipe Manana ret = lock_extent_range(src, lock_start, lock_len, true); 4333c125b8bfSFilipe Manana } else { 4334e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, off, inode, destoff, len, 4335e0bd70c6SFilipe Manana true); 4336e0bd70c6SFilipe Manana } 4337e0bd70c6SFilipe Manana ASSERT(ret == 0); 4338e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 4339e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 4340e0bd70c6SFilipe Manana goto out_unlock; 4341c125b8bfSFilipe Manana } 434232b7c687SMark Fasheh 43431c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 434432b7c687SMark Fasheh 4345c125b8bfSFilipe Manana if (same_inode) { 4346c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 4347c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 4348c125b8bfSFilipe Manana 4349c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 4350c125b8bfSFilipe Manana } else { 4351293a8489SMark Fasheh btrfs_double_extent_unlock(src, off, inode, destoff, len); 4352c125b8bfSFilipe Manana } 4353c125b8bfSFilipe Manana /* 4354c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 4355c125b8bfSFilipe Manana * data immediately and not the previous data. 4356c125b8bfSFilipe Manana */ 435765bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 435809cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 435909cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 4360f46b5a66SChristoph Hellwig out_unlock: 4361293a8489SMark Fasheh if (!same_inode) 4362293a8489SMark Fasheh btrfs_double_inode_unlock(src, inode); 4363293a8489SMark Fasheh else 43645955102cSAl Viro inode_unlock(src); 43653db11b2eSZach Brown return ret; 43663db11b2eSZach Brown } 43673db11b2eSZach Brown 436842ec3d4cSDarrick J. Wong loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, 436942ec3d4cSDarrick J. Wong struct file *dst_file, loff_t destoff, loff_t len, 43702e5dfc99SDarrick J. Wong unsigned int remap_flags) 43713db11b2eSZach Brown { 437242ec3d4cSDarrick J. Wong int ret; 437342ec3d4cSDarrick J. Wong 43742e5dfc99SDarrick J. Wong if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) 43752e5dfc99SDarrick J. Wong return -EINVAL; 43762e5dfc99SDarrick J. Wong 43772e5dfc99SDarrick J. Wong if (remap_flags & REMAP_FILE_DEDUP) { 43782e5dfc99SDarrick J. Wong struct inode *src = file_inode(src_file); 43792e5dfc99SDarrick J. Wong struct inode *dst = file_inode(dst_file); 43802e5dfc99SDarrick J. Wong u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 43812e5dfc99SDarrick J. Wong 43822e5dfc99SDarrick J. Wong if (WARN_ON_ONCE(bs < PAGE_SIZE)) { 43832e5dfc99SDarrick J. Wong /* 43842e5dfc99SDarrick J. Wong * Btrfs does not support blocksize < page_size. As a 43852e5dfc99SDarrick J. Wong * result, btrfs_cmp_data() won't correctly handle 43862e5dfc99SDarrick J. Wong * this situation without an update. 43872e5dfc99SDarrick J. Wong */ 43882e5dfc99SDarrick J. Wong return -EINVAL; 43892e5dfc99SDarrick J. Wong } 43902e5dfc99SDarrick J. Wong 439142ec3d4cSDarrick J. Wong ret = btrfs_extent_same(src, off, len, dst, destoff); 439242ec3d4cSDarrick J. Wong } else { 439342ec3d4cSDarrick J. Wong ret = btrfs_clone_files(dst_file, src_file, off, len, destoff); 43942e5dfc99SDarrick J. Wong } 439542ec3d4cSDarrick J. Wong return ret < 0 ? ret : len; 4396c5c9cd4dSSage Weil } 4397c5c9cd4dSSage Weil 43986ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 43996ef5ed0dSJosef Bacik { 4400496ad9aaSAl Viro struct inode *inode = file_inode(file); 44010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 44026ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 44036ef5ed0dSJosef Bacik struct btrfs_root *new_root; 44046ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 44056ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 44066ef5ed0dSJosef Bacik struct btrfs_path *path; 44076ef5ed0dSJosef Bacik struct btrfs_key location; 44086ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 44096ef5ed0dSJosef Bacik u64 objectid = 0; 44106ef5ed0dSJosef Bacik u64 dir_id; 44113c04ce01SMiao Xie int ret; 44126ef5ed0dSJosef Bacik 44136ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 44146ef5ed0dSJosef Bacik return -EPERM; 44156ef5ed0dSJosef Bacik 44163c04ce01SMiao Xie ret = mnt_want_write_file(file); 44173c04ce01SMiao Xie if (ret) 44183c04ce01SMiao Xie return ret; 44193c04ce01SMiao Xie 44203c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 44213c04ce01SMiao Xie ret = -EFAULT; 44223c04ce01SMiao Xie goto out; 44233c04ce01SMiao Xie } 44246ef5ed0dSJosef Bacik 44256ef5ed0dSJosef Bacik if (!objectid) 44261cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 44276ef5ed0dSJosef Bacik 44286ef5ed0dSJosef Bacik location.objectid = objectid; 44296ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 44306ef5ed0dSJosef Bacik location.offset = (u64)-1; 44316ef5ed0dSJosef Bacik 44320b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &location); 44333c04ce01SMiao Xie if (IS_ERR(new_root)) { 44343c04ce01SMiao Xie ret = PTR_ERR(new_root); 44353c04ce01SMiao Xie goto out; 44363c04ce01SMiao Xie } 44374fd786e6SMisono Tomohiro if (!is_fstree(new_root->root_key.objectid)) { 44386d6d2829Ssatoru takeuchi ret = -ENOENT; 44396d6d2829Ssatoru takeuchi goto out; 44406d6d2829Ssatoru takeuchi } 44416ef5ed0dSJosef Bacik 44426ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 44433c04ce01SMiao Xie if (!path) { 44443c04ce01SMiao Xie ret = -ENOMEM; 44453c04ce01SMiao Xie goto out; 44463c04ce01SMiao Xie } 44476ef5ed0dSJosef Bacik path->leave_spinning = 1; 44486ef5ed0dSJosef Bacik 44496ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 445098d5dc13STsutomu Itoh if (IS_ERR(trans)) { 44516ef5ed0dSJosef Bacik btrfs_free_path(path); 44523c04ce01SMiao Xie ret = PTR_ERR(trans); 44533c04ce01SMiao Xie goto out; 44546ef5ed0dSJosef Bacik } 44556ef5ed0dSJosef Bacik 44560b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 44570b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 44586ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4459cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 44606ef5ed0dSJosef Bacik btrfs_free_path(path); 44613a45bb20SJeff Mahoney btrfs_end_transaction(trans); 44620b246afaSJeff Mahoney btrfs_err(fs_info, 44635d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 44643c04ce01SMiao Xie ret = -ENOENT; 44653c04ce01SMiao Xie goto out; 44666ef5ed0dSJosef Bacik } 44676ef5ed0dSJosef Bacik 44686ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 44696ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 44706ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 44716ef5ed0dSJosef Bacik btrfs_free_path(path); 44726ef5ed0dSJosef Bacik 44730b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 44743a45bb20SJeff Mahoney btrfs_end_transaction(trans); 44753c04ce01SMiao Xie out: 44763c04ce01SMiao Xie mnt_drop_write_file(file); 44773c04ce01SMiao Xie return ret; 44786ef5ed0dSJosef Bacik } 44796ef5ed0dSJosef Bacik 4480c065f5b1SSu Yue static void get_block_group_info(struct list_head *groups_list, 4481bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4482bf5fc093SJosef Bacik { 4483bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 4484bf5fc093SJosef Bacik 4485bf5fc093SJosef Bacik space->total_bytes = 0; 4486bf5fc093SJosef Bacik space->used_bytes = 0; 4487bf5fc093SJosef Bacik space->flags = 0; 4488bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4489bf5fc093SJosef Bacik space->flags = block_group->flags; 4490bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 4491bf5fc093SJosef Bacik space->used_bytes += 4492bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 4493bf5fc093SJosef Bacik } 4494bf5fc093SJosef Bacik } 4495bf5fc093SJosef Bacik 44962ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 44972ff7e61eSJeff Mahoney void __user *arg) 44981406e432SJosef Bacik { 44991406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 45001406e432SJosef Bacik struct btrfs_ioctl_space_info space; 45011406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 45027fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 450313f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 45041406e432SJosef Bacik struct btrfs_space_info *info; 4505315d8e98SColin Ian King static const u64 types[] = { 4506315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 4507bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4508bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4509315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 4510315d8e98SColin Ian King }; 4511bf5fc093SJosef Bacik int num_types = 4; 45127fde62bfSChris Mason int alloc_size; 45131406e432SJosef Bacik int ret = 0; 451451788b1bSDan Rosenberg u64 slot_count = 0; 4515bf5fc093SJosef Bacik int i, c; 45161406e432SJosef Bacik 45171406e432SJosef Bacik if (copy_from_user(&space_args, 45181406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 45191406e432SJosef Bacik sizeof(space_args))) 45201406e432SJosef Bacik return -EFAULT; 45211406e432SJosef Bacik 4522bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4523bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4524bf5fc093SJosef Bacik 4525bf5fc093SJosef Bacik info = NULL; 45267fde62bfSChris Mason rcu_read_lock(); 45270b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4528bf5fc093SJosef Bacik list) { 4529bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4530bf5fc093SJosef Bacik info = tmp; 4531bf5fc093SJosef Bacik break; 4532bf5fc093SJosef Bacik } 4533bf5fc093SJosef Bacik } 45347fde62bfSChris Mason rcu_read_unlock(); 45351406e432SJosef Bacik 4536bf5fc093SJosef Bacik if (!info) 4537bf5fc093SJosef Bacik continue; 4538bf5fc093SJosef Bacik 4539bf5fc093SJosef Bacik down_read(&info->groups_sem); 4540bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4541bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4542bf5fc093SJosef Bacik slot_count++; 4543bf5fc093SJosef Bacik } 4544bf5fc093SJosef Bacik up_read(&info->groups_sem); 4545bf5fc093SJosef Bacik } 4546bf5fc093SJosef Bacik 454736523e95SDavid Sterba /* 454836523e95SDavid Sterba * Global block reserve, exported as a space_info 454936523e95SDavid Sterba */ 455036523e95SDavid Sterba slot_count++; 455136523e95SDavid Sterba 45527fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 45537fde62bfSChris Mason if (space_args.space_slots == 0) { 45547fde62bfSChris Mason space_args.total_spaces = slot_count; 45557fde62bfSChris Mason goto out; 45567fde62bfSChris Mason } 4557bf5fc093SJosef Bacik 455851788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4559bf5fc093SJosef Bacik 45607fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4561bf5fc093SJosef Bacik 45627fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 45637fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 45647fde62bfSChris Mason */ 456509cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 45667fde62bfSChris Mason return -ENOMEM; 45677fde62bfSChris Mason 45687fde62bfSChris Mason space_args.total_spaces = 0; 45698d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 45707fde62bfSChris Mason if (!dest) 45717fde62bfSChris Mason return -ENOMEM; 45727fde62bfSChris Mason dest_orig = dest; 45737fde62bfSChris Mason 45747fde62bfSChris Mason /* now we have a buffer to copy into */ 4575bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4576bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4577bf5fc093SJosef Bacik 457851788b1bSDan Rosenberg if (!slot_count) 457951788b1bSDan Rosenberg break; 458051788b1bSDan Rosenberg 4581bf5fc093SJosef Bacik info = NULL; 45821406e432SJosef Bacik rcu_read_lock(); 45830b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4584bf5fc093SJosef Bacik list) { 4585bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4586bf5fc093SJosef Bacik info = tmp; 45877fde62bfSChris Mason break; 4588bf5fc093SJosef Bacik } 4589bf5fc093SJosef Bacik } 4590bf5fc093SJosef Bacik rcu_read_unlock(); 45917fde62bfSChris Mason 4592bf5fc093SJosef Bacik if (!info) 4593bf5fc093SJosef Bacik continue; 4594bf5fc093SJosef Bacik down_read(&info->groups_sem); 4595bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4596bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 4597c065f5b1SSu Yue get_block_group_info(&info->block_groups[c], 4598c065f5b1SSu Yue &space); 45997fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 46001406e432SJosef Bacik dest++; 46011406e432SJosef Bacik space_args.total_spaces++; 460251788b1bSDan Rosenberg slot_count--; 46031406e432SJosef Bacik } 460451788b1bSDan Rosenberg if (!slot_count) 460551788b1bSDan Rosenberg break; 4606bf5fc093SJosef Bacik } 4607bf5fc093SJosef Bacik up_read(&info->groups_sem); 4608bf5fc093SJosef Bacik } 46091406e432SJosef Bacik 461036523e95SDavid Sterba /* 461136523e95SDavid Sterba * Add global block reserve 461236523e95SDavid Sterba */ 461336523e95SDavid Sterba if (slot_count) { 46140b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 461536523e95SDavid Sterba 461636523e95SDavid Sterba spin_lock(&block_rsv->lock); 461736523e95SDavid Sterba space.total_bytes = block_rsv->size; 461836523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 461936523e95SDavid Sterba spin_unlock(&block_rsv->lock); 462036523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 462136523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 462236523e95SDavid Sterba space_args.total_spaces++; 462336523e95SDavid Sterba } 462436523e95SDavid Sterba 46252eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 46267fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 46277fde62bfSChris Mason 46287fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 46297fde62bfSChris Mason ret = -EFAULT; 46307fde62bfSChris Mason 46317fde62bfSChris Mason kfree(dest_orig); 46327fde62bfSChris Mason out: 46337fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 46341406e432SJosef Bacik ret = -EFAULT; 46351406e432SJosef Bacik 46361406e432SJosef Bacik return ret; 46371406e432SJosef Bacik } 46381406e432SJosef Bacik 46399a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 46409a8c28beSMiao Xie void __user *argp) 464146204592SSage Weil { 464246204592SSage Weil struct btrfs_trans_handle *trans; 464346204592SSage Weil u64 transid; 4644db5b493aSTsutomu Itoh int ret; 464546204592SSage Weil 4646d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4647ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4648ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 464998d5dc13STsutomu Itoh return PTR_ERR(trans); 4650ff7c1d33SMiao Xie 4651ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4652ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4653ff7c1d33SMiao Xie goto out; 4654ff7c1d33SMiao Xie } 465546204592SSage Weil transid = trans->transid; 46563a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 0); 46578b2b2d3cSTsutomu Itoh if (ret) { 46583a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4659db5b493aSTsutomu Itoh return ret; 46608b2b2d3cSTsutomu Itoh } 4661ff7c1d33SMiao Xie out: 466246204592SSage Weil if (argp) 466346204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 466446204592SSage Weil return -EFAULT; 466546204592SSage Weil return 0; 466646204592SSage Weil } 466746204592SSage Weil 46682ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 46699a8c28beSMiao Xie void __user *argp) 467046204592SSage Weil { 467146204592SSage Weil u64 transid; 467246204592SSage Weil 467346204592SSage Weil if (argp) { 467446204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 467546204592SSage Weil return -EFAULT; 467646204592SSage Weil } else { 467746204592SSage Weil transid = 0; /* current trans */ 467846204592SSage Weil } 46792ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 468046204592SSage Weil } 468146204592SSage Weil 4682b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4683475f6387SJan Schmidt { 46840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4685475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4686b8e95489SMiao Xie int ret; 4687475f6387SJan Schmidt 4688475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4689475f6387SJan Schmidt return -EPERM; 4690475f6387SJan Schmidt 4691475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4692475f6387SJan Schmidt if (IS_ERR(sa)) 4693475f6387SJan Schmidt return PTR_ERR(sa); 4694475f6387SJan Schmidt 4695b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4696b8e95489SMiao Xie ret = mnt_want_write_file(file); 4697b8e95489SMiao Xie if (ret) 4698b8e95489SMiao Xie goto out; 4699b8e95489SMiao Xie } 4700b8e95489SMiao Xie 47010b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 470263a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 470363a212abSStefan Behrens 0); 4704475f6387SJan Schmidt 4705475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4706475f6387SJan Schmidt ret = -EFAULT; 4707475f6387SJan Schmidt 4708b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4709b8e95489SMiao Xie mnt_drop_write_file(file); 4710b8e95489SMiao Xie out: 4711475f6387SJan Schmidt kfree(sa); 4712475f6387SJan Schmidt return ret; 4713475f6387SJan Schmidt } 4714475f6387SJan Schmidt 47152ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4716475f6387SJan Schmidt { 4717475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4718475f6387SJan Schmidt return -EPERM; 4719475f6387SJan Schmidt 47202ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4721475f6387SJan Schmidt } 4722475f6387SJan Schmidt 47232ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4724475f6387SJan Schmidt void __user *arg) 4725475f6387SJan Schmidt { 4726475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4727475f6387SJan Schmidt int ret; 4728475f6387SJan Schmidt 4729475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4730475f6387SJan Schmidt return -EPERM; 4731475f6387SJan Schmidt 4732475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4733475f6387SJan Schmidt if (IS_ERR(sa)) 4734475f6387SJan Schmidt return PTR_ERR(sa); 4735475f6387SJan Schmidt 47362ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4737475f6387SJan Schmidt 4738475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4739475f6387SJan Schmidt ret = -EFAULT; 4740475f6387SJan Schmidt 4741475f6387SJan Schmidt kfree(sa); 4742475f6387SJan Schmidt return ret; 4743475f6387SJan Schmidt } 4744475f6387SJan Schmidt 47452ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4746b27f7c0cSDavid Sterba void __user *arg) 4747c11d2c23SStefan Behrens { 4748c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4749c11d2c23SStefan Behrens int ret; 4750c11d2c23SStefan Behrens 4751c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4752c11d2c23SStefan Behrens if (IS_ERR(sa)) 4753c11d2c23SStefan Behrens return PTR_ERR(sa); 4754c11d2c23SStefan Behrens 4755b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4756b27f7c0cSDavid Sterba kfree(sa); 4757b27f7c0cSDavid Sterba return -EPERM; 4758b27f7c0cSDavid Sterba } 4759b27f7c0cSDavid Sterba 47602ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4761c11d2c23SStefan Behrens 4762c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4763c11d2c23SStefan Behrens ret = -EFAULT; 4764c11d2c23SStefan Behrens 4765c11d2c23SStefan Behrens kfree(sa); 4766c11d2c23SStefan Behrens return ret; 4767c11d2c23SStefan Behrens } 4768c11d2c23SStefan Behrens 47692ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 47702ff7e61eSJeff Mahoney void __user *arg) 47713f6bcfbdSStefan Behrens { 47723f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 47733f6bcfbdSStefan Behrens int ret; 47743f6bcfbdSStefan Behrens 47753f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 47763f6bcfbdSStefan Behrens return -EPERM; 47773f6bcfbdSStefan Behrens 47783f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 47793f6bcfbdSStefan Behrens if (IS_ERR(p)) 47803f6bcfbdSStefan Behrens return PTR_ERR(p); 47813f6bcfbdSStefan Behrens 47823f6bcfbdSStefan Behrens switch (p->cmd) { 47833f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4784bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4785adfa97cbSIlya Dryomov ret = -EROFS; 4786adfa97cbSIlya Dryomov goto out; 4787adfa97cbSIlya Dryomov } 4788171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4789e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 47903f6bcfbdSStefan Behrens } else { 47912ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4792171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 47933f6bcfbdSStefan Behrens } 47943f6bcfbdSStefan Behrens break; 47953f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 47960b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 47973f6bcfbdSStefan Behrens ret = 0; 47983f6bcfbdSStefan Behrens break; 47993f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 480017d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 480197282031SAnand Jain ret = 0; 48023f6bcfbdSStefan Behrens break; 48033f6bcfbdSStefan Behrens default: 48043f6bcfbdSStefan Behrens ret = -EINVAL; 48053f6bcfbdSStefan Behrens break; 48063f6bcfbdSStefan Behrens } 48073f6bcfbdSStefan Behrens 48083f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 48093f6bcfbdSStefan Behrens ret = -EFAULT; 4810adfa97cbSIlya Dryomov out: 48113f6bcfbdSStefan Behrens kfree(p); 48123f6bcfbdSStefan Behrens return ret; 48133f6bcfbdSStefan Behrens } 48143f6bcfbdSStefan Behrens 4815d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4816d7728c96SJan Schmidt { 4817d7728c96SJan Schmidt int ret = 0; 4818d7728c96SJan Schmidt int i; 4819740c3d22SChris Mason u64 rel_ptr; 4820d7728c96SJan Schmidt int size; 4821806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4822d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4823d7728c96SJan Schmidt struct btrfs_path *path; 4824d7728c96SJan Schmidt 482582b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4826d7728c96SJan Schmidt return -EPERM; 4827d7728c96SJan Schmidt 4828d7728c96SJan Schmidt path = btrfs_alloc_path(); 4829d7728c96SJan Schmidt if (!path) { 4830d7728c96SJan Schmidt ret = -ENOMEM; 4831d7728c96SJan Schmidt goto out; 4832d7728c96SJan Schmidt } 4833d7728c96SJan Schmidt 4834d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4835d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4836d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4837d7728c96SJan Schmidt ipa = NULL; 4838d7728c96SJan Schmidt goto out; 4839d7728c96SJan Schmidt } 4840d7728c96SJan Schmidt 4841d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4842d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4843d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4844d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4845d7728c96SJan Schmidt ipath = NULL; 4846d7728c96SJan Schmidt goto out; 4847d7728c96SJan Schmidt } 4848d7728c96SJan Schmidt 4849d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4850d7728c96SJan Schmidt if (ret < 0) 4851d7728c96SJan Schmidt goto out; 4852d7728c96SJan Schmidt 4853d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4854745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4855745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4856740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4857d7728c96SJan Schmidt } 4858d7728c96SJan Schmidt 4859718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4860718dc5faSOmar Sandoval ipath->fspath, size); 4861d7728c96SJan Schmidt if (ret) { 4862d7728c96SJan Schmidt ret = -EFAULT; 4863d7728c96SJan Schmidt goto out; 4864d7728c96SJan Schmidt } 4865d7728c96SJan Schmidt 4866d7728c96SJan Schmidt out: 4867d7728c96SJan Schmidt btrfs_free_path(path); 4868d7728c96SJan Schmidt free_ipath(ipath); 4869d7728c96SJan Schmidt kfree(ipa); 4870d7728c96SJan Schmidt 4871d7728c96SJan Schmidt return ret; 4872d7728c96SJan Schmidt } 4873d7728c96SJan Schmidt 4874d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4875d7728c96SJan Schmidt { 4876d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4877d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4878d7728c96SJan Schmidt 4879d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4880d7728c96SJan Schmidt inodes->bytes_left -= c; 4881d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4882d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4883d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4884d7728c96SJan Schmidt inodes->elem_cnt += 3; 4885d7728c96SJan Schmidt } else { 4886d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4887d7728c96SJan Schmidt inodes->bytes_left = 0; 4888d7728c96SJan Schmidt inodes->elem_missed += 3; 4889d7728c96SJan Schmidt } 4890d7728c96SJan Schmidt 4891d7728c96SJan Schmidt return 0; 4892d7728c96SJan Schmidt } 4893d7728c96SJan Schmidt 48942ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4895d24a67b2SZygo Blaxell void __user *arg, int version) 4896d7728c96SJan Schmidt { 4897d7728c96SJan Schmidt int ret = 0; 4898d7728c96SJan Schmidt int size; 4899d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4900d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4901d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4902d24a67b2SZygo Blaxell bool ignore_offset; 4903d7728c96SJan Schmidt 4904d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4905d7728c96SJan Schmidt return -EPERM; 4906d7728c96SJan Schmidt 4907d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 49087b9ea627SShailendra Verma if (IS_ERR(loi)) 49097b9ea627SShailendra Verma return PTR_ERR(loi); 4910d7728c96SJan Schmidt 4911d24a67b2SZygo Blaxell if (version == 1) { 4912d24a67b2SZygo Blaxell ignore_offset = false; 4913b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4914d24a67b2SZygo Blaxell } else { 4915d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4916d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4917d24a67b2SZygo Blaxell ret = -EINVAL; 4918d24a67b2SZygo Blaxell goto out_loi; 4919d24a67b2SZygo Blaxell } 4920d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4921d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4922d24a67b2SZygo Blaxell ret = -EINVAL; 4923d24a67b2SZygo Blaxell goto out_loi; 4924d24a67b2SZygo Blaxell } 4925d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4926b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4927d24a67b2SZygo Blaxell } 4928d24a67b2SZygo Blaxell 4929d7728c96SJan Schmidt path = btrfs_alloc_path(); 4930d7728c96SJan Schmidt if (!path) { 4931d7728c96SJan Schmidt ret = -ENOMEM; 4932d7728c96SJan Schmidt goto out; 4933d7728c96SJan Schmidt } 4934d7728c96SJan Schmidt 4935d7728c96SJan Schmidt inodes = init_data_container(size); 4936d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4937d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4938d7728c96SJan Schmidt inodes = NULL; 4939d7728c96SJan Schmidt goto out; 4940d7728c96SJan Schmidt } 4941d7728c96SJan Schmidt 49422ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4943d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4944df031f07SLiu Bo if (ret == -EINVAL) 4945d7728c96SJan Schmidt ret = -ENOENT; 4946d7728c96SJan Schmidt if (ret < 0) 4947d7728c96SJan Schmidt goto out; 4948d7728c96SJan Schmidt 4949718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4950718dc5faSOmar Sandoval size); 4951d7728c96SJan Schmidt if (ret) 4952d7728c96SJan Schmidt ret = -EFAULT; 4953d7728c96SJan Schmidt 4954d7728c96SJan Schmidt out: 4955d7728c96SJan Schmidt btrfs_free_path(path); 4956f54de068SDavid Sterba kvfree(inodes); 4957d24a67b2SZygo Blaxell out_loi: 4958d7728c96SJan Schmidt kfree(loi); 4959d7728c96SJan Schmidt 4960d7728c96SJan Schmidt return ret; 4961d7728c96SJan Schmidt } 4962d7728c96SJan Schmidt 4963008ef096SDavid Sterba void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 4964c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4965c9e9f97bSIlya Dryomov { 4966c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4967c9e9f97bSIlya Dryomov 4968c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4969c9e9f97bSIlya Dryomov 49703009a62fSDavid Sterba if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) 4971837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4972837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4973837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4974a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4975a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4976837d5b6eSIlya Dryomov 4977c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4978c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4979c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 498019a39dceSIlya Dryomov 498119a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 498219a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 498319a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 4984c9e9f97bSIlya Dryomov } 4985c9e9f97bSIlya Dryomov 49869ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4987c9e9f97bSIlya Dryomov { 4988496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4989c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4990c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4991c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4992ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4993c9e9f97bSIlya Dryomov int ret; 4994c9e9f97bSIlya Dryomov 4995c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4996c9e9f97bSIlya Dryomov return -EPERM; 4997c9e9f97bSIlya Dryomov 4998e54bfa31SLiu Bo ret = mnt_want_write_file(file); 49999ba1f6e4SLiu Bo if (ret) 50009ba1f6e4SLiu Bo return ret; 50019ba1f6e4SLiu Bo 5002ed0fb78fSIlya Dryomov again: 5003171938e5SDavid Sterba if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 5004c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 5005ed0fb78fSIlya Dryomov need_unlock = true; 5006ed0fb78fSIlya Dryomov goto locked; 5007ed0fb78fSIlya Dryomov } 5008ed0fb78fSIlya Dryomov 5009ed0fb78fSIlya Dryomov /* 501001327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 5011ed0fb78fSIlya Dryomov * (1) some other op is running 5012ed0fb78fSIlya Dryomov * (2) balance is running 5013ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 5014ed0fb78fSIlya Dryomov */ 5015ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 5016ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 5017ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 50183009a62fSDavid Sterba if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 5019ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 5020dccdb07bSDavid Sterba /* 5021dccdb07bSDavid Sterba * Lock released to allow other waiters to continue, 5022dccdb07bSDavid Sterba * we'll reexamine the status again. 5023dccdb07bSDavid Sterba */ 5024ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 5025ed0fb78fSIlya Dryomov 5026ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 50273009a62fSDavid Sterba !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 5028ed0fb78fSIlya Dryomov /* this is (3) */ 5029ed0fb78fSIlya Dryomov need_unlock = false; 5030ed0fb78fSIlya Dryomov goto locked; 5031ed0fb78fSIlya Dryomov } 5032ed0fb78fSIlya Dryomov 5033ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 5034ed0fb78fSIlya Dryomov goto again; 5035ed0fb78fSIlya Dryomov } else { 5036ed0fb78fSIlya Dryomov /* this is (2) */ 5037ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 5038ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 5039ed0fb78fSIlya Dryomov goto out; 5040ed0fb78fSIlya Dryomov } 5041ed0fb78fSIlya Dryomov } else { 5042ed0fb78fSIlya Dryomov /* this is (1) */ 5043ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 5044e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 5045ed0fb78fSIlya Dryomov goto out; 5046ed0fb78fSIlya Dryomov } 5047ed0fb78fSIlya Dryomov 5048ed0fb78fSIlya Dryomov locked: 5049171938e5SDavid Sterba BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); 5050c9e9f97bSIlya Dryomov 5051c9e9f97bSIlya Dryomov if (arg) { 5052c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 5053c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 5054c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 5055ed0fb78fSIlya Dryomov goto out_unlock; 5056c9e9f97bSIlya Dryomov } 5057de322263SIlya Dryomov 5058de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 5059de322263SIlya Dryomov if (!fs_info->balance_ctl) { 5060de322263SIlya Dryomov ret = -ENOTCONN; 5061de322263SIlya Dryomov goto out_bargs; 5062de322263SIlya Dryomov } 5063de322263SIlya Dryomov 5064de322263SIlya Dryomov bctl = fs_info->balance_ctl; 5065de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 5066de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 5067de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 5068de322263SIlya Dryomov 5069de322263SIlya Dryomov goto do_balance; 5070de322263SIlya Dryomov } 5071c9e9f97bSIlya Dryomov } else { 5072c9e9f97bSIlya Dryomov bargs = NULL; 5073c9e9f97bSIlya Dryomov } 5074c9e9f97bSIlya Dryomov 5075ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 5076837d5b6eSIlya Dryomov ret = -EINPROGRESS; 5077837d5b6eSIlya Dryomov goto out_bargs; 5078837d5b6eSIlya Dryomov } 5079837d5b6eSIlya Dryomov 50808d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 5081c9e9f97bSIlya Dryomov if (!bctl) { 5082c9e9f97bSIlya Dryomov ret = -ENOMEM; 5083c9e9f97bSIlya Dryomov goto out_bargs; 5084c9e9f97bSIlya Dryomov } 5085c9e9f97bSIlya Dryomov 5086c9e9f97bSIlya Dryomov if (arg) { 5087c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 5088c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 5089c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 5090c9e9f97bSIlya Dryomov 5091c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 5092f43ffb60SIlya Dryomov } else { 5093f43ffb60SIlya Dryomov /* balance everything - no filters */ 5094f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 5095c9e9f97bSIlya Dryomov } 5096c9e9f97bSIlya Dryomov 50978eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 50988eb93459SDavid Sterba ret = -EINVAL; 50990f89abf5SChristian Engelmayer goto out_bctl; 51008eb93459SDavid Sterba } 51018eb93459SDavid Sterba 5102de322263SIlya Dryomov do_balance: 5103c9e9f97bSIlya Dryomov /* 5104149196a2SDavid Sterba * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to 5105149196a2SDavid Sterba * btrfs_balance. bctl is freed in reset_balance_state, or, if 5106149196a2SDavid Sterba * restriper was paused all the way until unmount, in free_fs_info. 5107149196a2SDavid Sterba * The flag should be cleared after reset_balance_state. 5108c9e9f97bSIlya Dryomov */ 5109ed0fb78fSIlya Dryomov need_unlock = false; 5110ed0fb78fSIlya Dryomov 51116fcf6e2bSDavid Sterba ret = btrfs_balance(fs_info, bctl, bargs); 51120f89abf5SChristian Engelmayer bctl = NULL; 5113ed0fb78fSIlya Dryomov 5114c9e9f97bSIlya Dryomov if (arg) { 5115c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 5116c9e9f97bSIlya Dryomov ret = -EFAULT; 5117c9e9f97bSIlya Dryomov } 5118c9e9f97bSIlya Dryomov 51190f89abf5SChristian Engelmayer out_bctl: 51200f89abf5SChristian Engelmayer kfree(bctl); 5121c9e9f97bSIlya Dryomov out_bargs: 5122c9e9f97bSIlya Dryomov kfree(bargs); 5123ed0fb78fSIlya Dryomov out_unlock: 5124c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 5125ed0fb78fSIlya Dryomov if (need_unlock) 5126171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 5127ed0fb78fSIlya Dryomov out: 5128e54bfa31SLiu Bo mnt_drop_write_file(file); 5129c9e9f97bSIlya Dryomov return ret; 5130c9e9f97bSIlya Dryomov } 5131c9e9f97bSIlya Dryomov 51322ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 5133837d5b6eSIlya Dryomov { 5134837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 5135837d5b6eSIlya Dryomov return -EPERM; 5136837d5b6eSIlya Dryomov 5137837d5b6eSIlya Dryomov switch (cmd) { 5138837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 51390b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 5140a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 51410b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 5142837d5b6eSIlya Dryomov } 5143837d5b6eSIlya Dryomov 5144837d5b6eSIlya Dryomov return -EINVAL; 5145837d5b6eSIlya Dryomov } 5146837d5b6eSIlya Dryomov 51472ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 514819a39dceSIlya Dryomov void __user *arg) 514919a39dceSIlya Dryomov { 515019a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 515119a39dceSIlya Dryomov int ret = 0; 515219a39dceSIlya Dryomov 515319a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 515419a39dceSIlya Dryomov return -EPERM; 515519a39dceSIlya Dryomov 515619a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 515719a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 515819a39dceSIlya Dryomov ret = -ENOTCONN; 515919a39dceSIlya Dryomov goto out; 516019a39dceSIlya Dryomov } 516119a39dceSIlya Dryomov 51628d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 516319a39dceSIlya Dryomov if (!bargs) { 516419a39dceSIlya Dryomov ret = -ENOMEM; 516519a39dceSIlya Dryomov goto out; 516619a39dceSIlya Dryomov } 516719a39dceSIlya Dryomov 5168008ef096SDavid Sterba btrfs_update_ioctl_balance_args(fs_info, bargs); 516919a39dceSIlya Dryomov 517019a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 517119a39dceSIlya Dryomov ret = -EFAULT; 517219a39dceSIlya Dryomov 517319a39dceSIlya Dryomov kfree(bargs); 517419a39dceSIlya Dryomov out: 517519a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 517619a39dceSIlya Dryomov return ret; 517719a39dceSIlya Dryomov } 517819a39dceSIlya Dryomov 5179905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 51805d13a37bSArne Jansen { 51810b246afaSJeff Mahoney struct inode *inode = file_inode(file); 51820b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 51835d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 51845d13a37bSArne Jansen int ret; 51855d13a37bSArne Jansen 51865d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 51875d13a37bSArne Jansen return -EPERM; 51885d13a37bSArne Jansen 5189905b0ddaSMiao Xie ret = mnt_want_write_file(file); 5190905b0ddaSMiao Xie if (ret) 5191905b0ddaSMiao Xie return ret; 51925d13a37bSArne Jansen 51935d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 5194905b0ddaSMiao Xie if (IS_ERR(sa)) { 5195905b0ddaSMiao Xie ret = PTR_ERR(sa); 5196905b0ddaSMiao Xie goto drop_write; 5197905b0ddaSMiao Xie } 51985d13a37bSArne Jansen 51990b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 52005d13a37bSArne Jansen 52015d13a37bSArne Jansen switch (sa->cmd) { 52025d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 5203340f1aa2SNikolay Borisov ret = btrfs_quota_enable(fs_info); 52045d13a37bSArne Jansen break; 52055d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 5206340f1aa2SNikolay Borisov ret = btrfs_quota_disable(fs_info); 52075d13a37bSArne Jansen break; 52085d13a37bSArne Jansen default: 52095d13a37bSArne Jansen ret = -EINVAL; 52105d13a37bSArne Jansen break; 52115d13a37bSArne Jansen } 52125d13a37bSArne Jansen 52135d13a37bSArne Jansen kfree(sa); 52140b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5215905b0ddaSMiao Xie drop_write: 5216905b0ddaSMiao Xie mnt_drop_write_file(file); 52175d13a37bSArne Jansen return ret; 52185d13a37bSArne Jansen } 52195d13a37bSArne Jansen 5220905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 52215d13a37bSArne Jansen { 52220b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 52240b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 52255d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 52265d13a37bSArne Jansen struct btrfs_trans_handle *trans; 52275d13a37bSArne Jansen int ret; 52285d13a37bSArne Jansen int err; 52295d13a37bSArne Jansen 52305d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 52315d13a37bSArne Jansen return -EPERM; 52325d13a37bSArne Jansen 5233905b0ddaSMiao Xie ret = mnt_want_write_file(file); 5234905b0ddaSMiao Xie if (ret) 5235905b0ddaSMiao Xie return ret; 52365d13a37bSArne Jansen 52375d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 5238905b0ddaSMiao Xie if (IS_ERR(sa)) { 5239905b0ddaSMiao Xie ret = PTR_ERR(sa); 5240905b0ddaSMiao Xie goto drop_write; 5241905b0ddaSMiao Xie } 52425d13a37bSArne Jansen 52435d13a37bSArne Jansen trans = btrfs_join_transaction(root); 52445d13a37bSArne Jansen if (IS_ERR(trans)) { 52455d13a37bSArne Jansen ret = PTR_ERR(trans); 52465d13a37bSArne Jansen goto out; 52475d13a37bSArne Jansen } 52485d13a37bSArne Jansen 52495d13a37bSArne Jansen if (sa->assign) { 52509f8a6ce6SLu Fengqi ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); 52515d13a37bSArne Jansen } else { 525239616c27SLu Fengqi ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); 52535d13a37bSArne Jansen } 52545d13a37bSArne Jansen 5255e082f563SQu Wenruo /* update qgroup status and info */ 5256280f8bd2SLu Fengqi err = btrfs_run_qgroups(trans); 5257e082f563SQu Wenruo if (err < 0) 52580b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 5259ad8403dfSAnand Jain "failed to update qgroup status and info"); 52603a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 52615d13a37bSArne Jansen if (err && !ret) 52625d13a37bSArne Jansen ret = err; 52635d13a37bSArne Jansen 52645d13a37bSArne Jansen out: 52655d13a37bSArne Jansen kfree(sa); 5266905b0ddaSMiao Xie drop_write: 5267905b0ddaSMiao Xie mnt_drop_write_file(file); 52685d13a37bSArne Jansen return ret; 52695d13a37bSArne Jansen } 52705d13a37bSArne Jansen 5271905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 52725d13a37bSArne Jansen { 52730b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52740b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 52755d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 52765d13a37bSArne Jansen struct btrfs_trans_handle *trans; 52775d13a37bSArne Jansen int ret; 52785d13a37bSArne Jansen int err; 52795d13a37bSArne Jansen 52805d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 52815d13a37bSArne Jansen return -EPERM; 52825d13a37bSArne Jansen 5283905b0ddaSMiao Xie ret = mnt_want_write_file(file); 5284905b0ddaSMiao Xie if (ret) 5285905b0ddaSMiao Xie return ret; 52865d13a37bSArne Jansen 52875d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 5288905b0ddaSMiao Xie if (IS_ERR(sa)) { 5289905b0ddaSMiao Xie ret = PTR_ERR(sa); 5290905b0ddaSMiao Xie goto drop_write; 5291905b0ddaSMiao Xie } 52925d13a37bSArne Jansen 5293d86e56cfSMiao Xie if (!sa->qgroupid) { 5294d86e56cfSMiao Xie ret = -EINVAL; 5295d86e56cfSMiao Xie goto out; 5296d86e56cfSMiao Xie } 5297d86e56cfSMiao Xie 52985d13a37bSArne Jansen trans = btrfs_join_transaction(root); 52995d13a37bSArne Jansen if (IS_ERR(trans)) { 53005d13a37bSArne Jansen ret = PTR_ERR(trans); 53015d13a37bSArne Jansen goto out; 53025d13a37bSArne Jansen } 53035d13a37bSArne Jansen 53045d13a37bSArne Jansen if (sa->create) { 530549a05ecdSLu Fengqi ret = btrfs_create_qgroup(trans, sa->qgroupid); 53065d13a37bSArne Jansen } else { 53073efbee1dSLu Fengqi ret = btrfs_remove_qgroup(trans, sa->qgroupid); 53085d13a37bSArne Jansen } 53095d13a37bSArne Jansen 53103a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 53115d13a37bSArne Jansen if (err && !ret) 53125d13a37bSArne Jansen ret = err; 53135d13a37bSArne Jansen 53145d13a37bSArne Jansen out: 53155d13a37bSArne Jansen kfree(sa); 5316905b0ddaSMiao Xie drop_write: 5317905b0ddaSMiao Xie mnt_drop_write_file(file); 53185d13a37bSArne Jansen return ret; 53195d13a37bSArne Jansen } 53205d13a37bSArne Jansen 5321905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 53225d13a37bSArne Jansen { 53230b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53240b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 53255d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 53265d13a37bSArne Jansen struct btrfs_trans_handle *trans; 53275d13a37bSArne Jansen int ret; 53285d13a37bSArne Jansen int err; 53295d13a37bSArne Jansen u64 qgroupid; 53305d13a37bSArne Jansen 53315d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 53325d13a37bSArne Jansen return -EPERM; 53335d13a37bSArne Jansen 5334905b0ddaSMiao Xie ret = mnt_want_write_file(file); 5335905b0ddaSMiao Xie if (ret) 5336905b0ddaSMiao Xie return ret; 53375d13a37bSArne Jansen 53385d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 5339905b0ddaSMiao Xie if (IS_ERR(sa)) { 5340905b0ddaSMiao Xie ret = PTR_ERR(sa); 5341905b0ddaSMiao Xie goto drop_write; 5342905b0ddaSMiao Xie } 53435d13a37bSArne Jansen 53445d13a37bSArne Jansen trans = btrfs_join_transaction(root); 53455d13a37bSArne Jansen if (IS_ERR(trans)) { 53465d13a37bSArne Jansen ret = PTR_ERR(trans); 53475d13a37bSArne Jansen goto out; 53485d13a37bSArne Jansen } 53495d13a37bSArne Jansen 53505d13a37bSArne Jansen qgroupid = sa->qgroupid; 53515d13a37bSArne Jansen if (!qgroupid) { 53525d13a37bSArne Jansen /* take the current subvol as qgroup */ 53535d13a37bSArne Jansen qgroupid = root->root_key.objectid; 53545d13a37bSArne Jansen } 53555d13a37bSArne Jansen 5356f0042d5eSLu Fengqi ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); 53575d13a37bSArne Jansen 53583a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 53595d13a37bSArne Jansen if (err && !ret) 53605d13a37bSArne Jansen ret = err; 53615d13a37bSArne Jansen 53625d13a37bSArne Jansen out: 53635d13a37bSArne Jansen kfree(sa); 5364905b0ddaSMiao Xie drop_write: 5365905b0ddaSMiao Xie mnt_drop_write_file(file); 53665d13a37bSArne Jansen return ret; 53675d13a37bSArne Jansen } 53685d13a37bSArne Jansen 53692f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 53702f232036SJan Schmidt { 53710b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53720b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53732f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 53742f232036SJan Schmidt int ret; 53752f232036SJan Schmidt 53762f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 53772f232036SJan Schmidt return -EPERM; 53782f232036SJan Schmidt 53792f232036SJan Schmidt ret = mnt_want_write_file(file); 53802f232036SJan Schmidt if (ret) 53812f232036SJan Schmidt return ret; 53822f232036SJan Schmidt 53832f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 53842f232036SJan Schmidt if (IS_ERR(qsa)) { 53852f232036SJan Schmidt ret = PTR_ERR(qsa); 53862f232036SJan Schmidt goto drop_write; 53872f232036SJan Schmidt } 53882f232036SJan Schmidt 53892f232036SJan Schmidt if (qsa->flags) { 53902f232036SJan Schmidt ret = -EINVAL; 53912f232036SJan Schmidt goto out; 53922f232036SJan Schmidt } 53932f232036SJan Schmidt 53940b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 53952f232036SJan Schmidt 53962f232036SJan Schmidt out: 53972f232036SJan Schmidt kfree(qsa); 53982f232036SJan Schmidt drop_write: 53992f232036SJan Schmidt mnt_drop_write_file(file); 54002f232036SJan Schmidt return ret; 54012f232036SJan Schmidt } 54022f232036SJan Schmidt 54032f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 54042f232036SJan Schmidt { 54050b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54060b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54072f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 54082f232036SJan Schmidt int ret = 0; 54092f232036SJan Schmidt 54102f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 54112f232036SJan Schmidt return -EPERM; 54122f232036SJan Schmidt 54138d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 54142f232036SJan Schmidt if (!qsa) 54152f232036SJan Schmidt return -ENOMEM; 54162f232036SJan Schmidt 54170b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 54182f232036SJan Schmidt qsa->flags = 1; 54190b246afaSJeff Mahoney qsa->progress = fs_info->qgroup_rescan_progress.objectid; 54202f232036SJan Schmidt } 54212f232036SJan Schmidt 54222f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 54232f232036SJan Schmidt ret = -EFAULT; 54242f232036SJan Schmidt 54252f232036SJan Schmidt kfree(qsa); 54262f232036SJan Schmidt return ret; 54272f232036SJan Schmidt } 54282f232036SJan Schmidt 542957254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 543057254b6eSJan Schmidt { 54310b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54320b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 543357254b6eSJan Schmidt 543457254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 543557254b6eSJan Schmidt return -EPERM; 543657254b6eSJan Schmidt 54370b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 543857254b6eSJan Schmidt } 543957254b6eSJan Schmidt 5440abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 5441abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 54428ea05e3aSAlexander Block { 5443496ad9aaSAl Viro struct inode *inode = file_inode(file); 54440b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54458ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 54468ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 54478ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 544895582b00SDeepa Dinamani struct timespec64 ct = current_time(inode); 54498ea05e3aSAlexander Block int ret = 0; 5450dd5f9615SStefan Behrens int received_uuid_changed; 54518ea05e3aSAlexander Block 5452bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5453bd60ea0fSDavid Sterba return -EPERM; 5454bd60ea0fSDavid Sterba 54558ea05e3aSAlexander Block ret = mnt_want_write_file(file); 54568ea05e3aSAlexander Block if (ret < 0) 54578ea05e3aSAlexander Block return ret; 54588ea05e3aSAlexander Block 54590b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 54608ea05e3aSAlexander Block 54614a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 54628ea05e3aSAlexander Block ret = -EINVAL; 54638ea05e3aSAlexander Block goto out; 54648ea05e3aSAlexander Block } 54658ea05e3aSAlexander Block 54668ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 54678ea05e3aSAlexander Block ret = -EROFS; 54688ea05e3aSAlexander Block goto out; 54698ea05e3aSAlexander Block } 54708ea05e3aSAlexander Block 5471dd5f9615SStefan Behrens /* 5472dd5f9615SStefan Behrens * 1 - root item 5473dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5474dd5f9615SStefan Behrens */ 5475dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 54768ea05e3aSAlexander Block if (IS_ERR(trans)) { 54778ea05e3aSAlexander Block ret = PTR_ERR(trans); 54788ea05e3aSAlexander Block trans = NULL; 54798ea05e3aSAlexander Block goto out; 54808ea05e3aSAlexander Block } 54818ea05e3aSAlexander Block 54828ea05e3aSAlexander Block sa->rtransid = trans->transid; 54838ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 54848ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 54858ea05e3aSAlexander Block 5486dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5487dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5488dd5f9615SStefan Behrens if (received_uuid_changed && 5489d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 5490d1957791SLu Fengqi ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, 5491dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5492dd5f9615SStefan Behrens root->root_key.objectid); 5493d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 5494d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 5495d87ff758SNikolay Borisov btrfs_end_transaction(trans); 5496d87ff758SNikolay Borisov goto out; 5497d87ff758SNikolay Borisov } 5498d87ff758SNikolay Borisov } 54998ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 55008ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 55018ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 55023cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 55033cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 55043cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 55053cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 55068ea05e3aSAlexander Block 55070b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 55088ea05e3aSAlexander Block &root->root_key, &root->root_item); 55098ea05e3aSAlexander Block if (ret < 0) { 55103a45bb20SJeff Mahoney btrfs_end_transaction(trans); 55118ea05e3aSAlexander Block goto out; 5512dd5f9615SStefan Behrens } 5513dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 5514cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, sa->uuid, 5515dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5516dd5f9615SStefan Behrens root->root_key.objectid); 5517dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 551866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 5519efd38150SNikolay Borisov btrfs_end_transaction(trans); 5520dd5f9615SStefan Behrens goto out; 5521dd5f9615SStefan Behrens } 5522dd5f9615SStefan Behrens } 55233a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5524abccd00fSHugo Mills out: 55250b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5526abccd00fSHugo Mills mnt_drop_write_file(file); 5527abccd00fSHugo Mills return ret; 5528abccd00fSHugo Mills } 5529abccd00fSHugo Mills 5530abccd00fSHugo Mills #ifdef CONFIG_64BIT 5531abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5532abccd00fSHugo Mills void __user *arg) 5533abccd00fSHugo Mills { 5534abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5535abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5536abccd00fSHugo Mills int ret = 0; 5537abccd00fSHugo Mills 5538abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 55397b9ea627SShailendra Verma if (IS_ERR(args32)) 55407b9ea627SShailendra Verma return PTR_ERR(args32); 5541abccd00fSHugo Mills 55428d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 554384dbeb87SDan Carpenter if (!args64) { 554484dbeb87SDan Carpenter ret = -ENOMEM; 5545abccd00fSHugo Mills goto out; 5546abccd00fSHugo Mills } 5547abccd00fSHugo Mills 5548abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5549abccd00fSHugo Mills args64->stransid = args32->stransid; 5550abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5551abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5552abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5553abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5554abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5555abccd00fSHugo Mills args64->flags = args32->flags; 5556abccd00fSHugo Mills 5557abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5558abccd00fSHugo Mills if (ret) 5559abccd00fSHugo Mills goto out; 5560abccd00fSHugo Mills 5561abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5562abccd00fSHugo Mills args32->stransid = args64->stransid; 5563abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5564abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5565abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5566abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5567abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5568abccd00fSHugo Mills args32->flags = args64->flags; 5569abccd00fSHugo Mills 5570abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5571abccd00fSHugo Mills if (ret) 5572abccd00fSHugo Mills ret = -EFAULT; 5573abccd00fSHugo Mills 5574abccd00fSHugo Mills out: 5575abccd00fSHugo Mills kfree(args32); 5576abccd00fSHugo Mills kfree(args64); 5577abccd00fSHugo Mills return ret; 5578abccd00fSHugo Mills } 5579abccd00fSHugo Mills #endif 5580abccd00fSHugo Mills 5581abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5582abccd00fSHugo Mills void __user *arg) 5583abccd00fSHugo Mills { 5584abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5585abccd00fSHugo Mills int ret = 0; 5586abccd00fSHugo Mills 5587abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 55887b9ea627SShailendra Verma if (IS_ERR(sa)) 55897b9ea627SShailendra Verma return PTR_ERR(sa); 5590abccd00fSHugo Mills 5591abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5592abccd00fSHugo Mills 5593abccd00fSHugo Mills if (ret) 5594abccd00fSHugo Mills goto out; 5595abccd00fSHugo Mills 55968ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 55978ea05e3aSAlexander Block if (ret) 55988ea05e3aSAlexander Block ret = -EFAULT; 55998ea05e3aSAlexander Block 56008ea05e3aSAlexander Block out: 56018ea05e3aSAlexander Block kfree(sa); 56028ea05e3aSAlexander Block return ret; 56038ea05e3aSAlexander Block } 56048ea05e3aSAlexander Block 5605867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 5606867ab667Sjeff.liu { 56070b246afaSJeff Mahoney struct inode *inode = file_inode(file); 56080b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 5609a1b83ac5SAnand Jain size_t len; 5610867ab667Sjeff.liu int ret; 5611a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5612a1b83ac5SAnand Jain 56130b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 56140b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 56150b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 5616a1b83ac5SAnand Jain 5617a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5618867ab667Sjeff.liu 5619867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 56200b246afaSJeff Mahoney btrfs_warn(fs_info, 56210b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 56220b246afaSJeff Mahoney --len); 5623867ab667Sjeff.liu } 5624867ab667Sjeff.liu 5625867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5626867ab667Sjeff.liu 5627867ab667Sjeff.liu return ret ? -EFAULT : 0; 5628867ab667Sjeff.liu } 5629867ab667Sjeff.liu 5630a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5631a8bfd4abSjeff.liu { 56320b246afaSJeff Mahoney struct inode *inode = file_inode(file); 56330b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 56340b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 56350b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 5636a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5637a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5638a8bfd4abSjeff.liu int ret; 5639a8bfd4abSjeff.liu 5640a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5641a8bfd4abSjeff.liu return -EPERM; 5642a8bfd4abSjeff.liu 5643a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5644a8bfd4abSjeff.liu return -EFAULT; 5645a8bfd4abSjeff.liu 5646a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 56470b246afaSJeff Mahoney btrfs_err(fs_info, 56485d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 5649a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5650a8bfd4abSjeff.liu return -EINVAL; 5651a8bfd4abSjeff.liu } 5652a8bfd4abSjeff.liu 5653a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5654a8bfd4abSjeff.liu if (ret) 5655a8bfd4abSjeff.liu return ret; 5656a8bfd4abSjeff.liu 5657a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5658a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5659a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5660a8bfd4abSjeff.liu goto out_unlock; 5661a8bfd4abSjeff.liu } 5662a8bfd4abSjeff.liu 56630b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5664a8bfd4abSjeff.liu strcpy(super_block->label, label); 56650b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 56663a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5667a8bfd4abSjeff.liu 5668a8bfd4abSjeff.liu out_unlock: 5669a8bfd4abSjeff.liu mnt_drop_write_file(file); 5670a8bfd4abSjeff.liu return ret; 5671a8bfd4abSjeff.liu } 5672a8bfd4abSjeff.liu 56732eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 56742eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 56752eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 56762eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 56772eaa055fSJeff Mahoney 5678d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 56792eaa055fSJeff Mahoney { 56804d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 56812eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 56822eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 56832eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 56842eaa055fSJeff Mahoney }; 56852eaa055fSJeff Mahoney 56862eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 56872eaa055fSJeff Mahoney return -EFAULT; 56882eaa055fSJeff Mahoney 56892eaa055fSJeff Mahoney return 0; 56902eaa055fSJeff Mahoney } 56912eaa055fSJeff Mahoney 56922eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 56932eaa055fSJeff Mahoney { 56940b246afaSJeff Mahoney struct inode *inode = file_inode(file); 56950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 56960b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 56972eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 56982eaa055fSJeff Mahoney 56992eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 57002eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 57012eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 57022eaa055fSJeff Mahoney 57032eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 57042eaa055fSJeff Mahoney return -EFAULT; 57052eaa055fSJeff Mahoney 57062eaa055fSJeff Mahoney return 0; 57072eaa055fSJeff Mahoney } 57082eaa055fSJeff Mahoney 57092ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 57103b02a68aSJeff Mahoney enum btrfs_feature_set set, 57112eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 57122eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 57132eaa055fSJeff Mahoney { 57143b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 57153b02a68aSJeff Mahoney char *names; 57162eaa055fSJeff Mahoney u64 disallowed, unsupported; 57172eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 57182eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 57192eaa055fSJeff Mahoney 57202eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 57212eaa055fSJeff Mahoney if (unsupported) { 57223b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 57233b02a68aSJeff Mahoney if (names) { 57240b246afaSJeff Mahoney btrfs_warn(fs_info, 57253b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 57263b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 57273b02a68aSJeff Mahoney kfree(names); 57283b02a68aSJeff Mahoney } else 57290b246afaSJeff Mahoney btrfs_warn(fs_info, 57302eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 57312eaa055fSJeff Mahoney type, unsupported); 57322eaa055fSJeff Mahoney return -EOPNOTSUPP; 57332eaa055fSJeff Mahoney } 57342eaa055fSJeff Mahoney 57352eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 57362eaa055fSJeff Mahoney if (disallowed) { 57373b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 57383b02a68aSJeff Mahoney if (names) { 57390b246afaSJeff Mahoney btrfs_warn(fs_info, 57403b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 57413b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 57423b02a68aSJeff Mahoney kfree(names); 57433b02a68aSJeff Mahoney } else 57440b246afaSJeff Mahoney btrfs_warn(fs_info, 57452eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 57462eaa055fSJeff Mahoney type, disallowed); 57472eaa055fSJeff Mahoney return -EPERM; 57482eaa055fSJeff Mahoney } 57492eaa055fSJeff Mahoney 57502eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 57512eaa055fSJeff Mahoney if (disallowed) { 57523b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 57533b02a68aSJeff Mahoney if (names) { 57540b246afaSJeff Mahoney btrfs_warn(fs_info, 57553b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 57563b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 57573b02a68aSJeff Mahoney kfree(names); 57583b02a68aSJeff Mahoney } else 57590b246afaSJeff Mahoney btrfs_warn(fs_info, 57602eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 57612eaa055fSJeff Mahoney type, disallowed); 57622eaa055fSJeff Mahoney return -EPERM; 57632eaa055fSJeff Mahoney } 57642eaa055fSJeff Mahoney 57652eaa055fSJeff Mahoney return 0; 57662eaa055fSJeff Mahoney } 57672eaa055fSJeff Mahoney 57682ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 57692ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 57702eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 57712eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 57722eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 57732eaa055fSJeff Mahoney 57742eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 57752eaa055fSJeff Mahoney { 57760b246afaSJeff Mahoney struct inode *inode = file_inode(file); 57770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 57780b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 57790b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 57802eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 57812eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 57822eaa055fSJeff Mahoney u64 newflags; 57832eaa055fSJeff Mahoney int ret; 57842eaa055fSJeff Mahoney 57852eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 57862eaa055fSJeff Mahoney return -EPERM; 57872eaa055fSJeff Mahoney 57882eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 57892eaa055fSJeff Mahoney return -EFAULT; 57902eaa055fSJeff Mahoney 57912eaa055fSJeff Mahoney /* Nothing to do */ 57922eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 57932eaa055fSJeff Mahoney !flags[0].incompat_flags) 57942eaa055fSJeff Mahoney return 0; 57952eaa055fSJeff Mahoney 57962ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 57972eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 57982eaa055fSJeff Mahoney if (ret) 57992eaa055fSJeff Mahoney return ret; 58002eaa055fSJeff Mahoney 58012ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 58022eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 58032eaa055fSJeff Mahoney if (ret) 58042eaa055fSJeff Mahoney return ret; 58052eaa055fSJeff Mahoney 58062ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 58072eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 58082eaa055fSJeff Mahoney if (ret) 58092eaa055fSJeff Mahoney return ret; 58102eaa055fSJeff Mahoney 58117ab19625SDavid Sterba ret = mnt_want_write_file(file); 58127ab19625SDavid Sterba if (ret) 58137ab19625SDavid Sterba return ret; 58147ab19625SDavid Sterba 58158051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 58167ab19625SDavid Sterba if (IS_ERR(trans)) { 58177ab19625SDavid Sterba ret = PTR_ERR(trans); 58187ab19625SDavid Sterba goto out_drop_write; 58197ab19625SDavid Sterba } 58202eaa055fSJeff Mahoney 58210b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 58222eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 58232eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 58242eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 58252eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 58262eaa055fSJeff Mahoney 58272eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 58282eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 58292eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 58302eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 58312eaa055fSJeff Mahoney 58322eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 58332eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 58342eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 58352eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 58360b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 58372eaa055fSJeff Mahoney 58383a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 58397ab19625SDavid Sterba out_drop_write: 58407ab19625SDavid Sterba mnt_drop_write_file(file); 58417ab19625SDavid Sterba 58427ab19625SDavid Sterba return ret; 58432eaa055fSJeff Mahoney } 58442eaa055fSJeff Mahoney 58452351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 58462351f431SJosef Bacik { 58472351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 58482351f431SJosef Bacik int ret; 58492351f431SJosef Bacik 58502351f431SJosef Bacik if (compat) { 58512351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 58522351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 58532351f431SJosef Bacik 58542351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 58552351f431SJosef Bacik if (ret) 58562351f431SJosef Bacik return -EFAULT; 58572351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 58582351f431SJosef Bacik if (!arg) 58592351f431SJosef Bacik return -ENOMEM; 58602351f431SJosef Bacik arg->send_fd = args32.send_fd; 58612351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 58622351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 58632351f431SJosef Bacik arg->parent_root = args32.parent_root; 58642351f431SJosef Bacik arg->flags = args32.flags; 58652351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 58662351f431SJosef Bacik sizeof(args32.reserved)); 58672351f431SJosef Bacik #else 58682351f431SJosef Bacik return -ENOTTY; 58692351f431SJosef Bacik #endif 58702351f431SJosef Bacik } else { 58712351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 58722351f431SJosef Bacik if (IS_ERR(arg)) 58732351f431SJosef Bacik return PTR_ERR(arg); 58742351f431SJosef Bacik } 58752351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 58762351f431SJosef Bacik kfree(arg); 58772351f431SJosef Bacik return ret; 58782351f431SJosef Bacik } 58792351f431SJosef Bacik 5880f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5881f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5882f46b5a66SChristoph Hellwig { 58830b246afaSJeff Mahoney struct inode *inode = file_inode(file); 58840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 58850b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 58864bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5887f46b5a66SChristoph Hellwig 5888f46b5a66SChristoph Hellwig switch (cmd) { 58896cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 58906cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 58916cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 58926cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 58936cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 58946cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5895f7039b1dSLi Dongyang case FITRIM: 5896f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5897f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5898fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5899fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5900fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 59013de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5902fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 59036f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 59046f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 590576dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 590676dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 59070caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 59080caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 59090caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 59100caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 59116ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 59126ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5913f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 59141e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 59151e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 59161e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5917f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5918198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5919f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 59202ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5921f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5922da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 59236b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 59246b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5925475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 59262ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5927475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 59282ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5929f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 59309ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5931ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5932ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5933cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5934cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5935ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5936ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5937d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5938d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5939d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5940d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5941d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5942d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 59431406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 59442ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 59459b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 59469b199859SFilipe David Borba Manana int ret; 59479b199859SFilipe David Borba Manana 594882b3e53bSNikolay Borisov ret = btrfs_start_delalloc_roots(fs_info, -1); 59499b199859SFilipe David Borba Manana if (ret) 59509b199859SFilipe David Borba Manana return ret; 59510b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 59522fad4e83SDavid Sterba /* 59532fad4e83SDavid Sterba * The transaction thread may want to do more work, 595401327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 59552fad4e83SDavid Sterba * processing uncleaned subvols. 59562fad4e83SDavid Sterba */ 59570b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 59589b199859SFilipe David Borba Manana return ret; 59599b199859SFilipe David Borba Manana } 596046204592SSage Weil case BTRFS_IOC_START_SYNC: 59619a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 596246204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 59632ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5964475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5965b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5966475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 59672ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5968475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 59692ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5970c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 59719ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5972837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 59732ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 597419a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 59752ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 59768ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 59778ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5978abccd00fSHugo Mills #ifdef CONFIG_64BIT 5979abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5980abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5981abccd00fSHugo Mills #endif 598231db9f7cSAlexander Block case BTRFS_IOC_SEND: 59832351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 59842351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 59852351f431SJosef Bacik case BTRFS_IOC_SEND_32: 59862351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 59872351f431SJosef Bacik #endif 5988c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 59892ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 59905d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5991905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 59925d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5993905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 59945d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5995905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 59965d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5997905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 59982f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 59992f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 60002f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 60012f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 600257254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 600357254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 60043f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 60052ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 6006867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 6007867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 6008a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 6009a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 60102eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 6011d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 60122eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 60132eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 60142eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 60152eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 6016e4202ac9SDavid Sterba case FS_IOC_FSGETXATTR: 6017e4202ac9SDavid Sterba return btrfs_ioctl_fsgetxattr(file, argp); 6018025f2121SDavid Sterba case FS_IOC_FSSETXATTR: 6019025f2121SDavid Sterba return btrfs_ioctl_fssetxattr(file, argp); 6020b64ec075STomohiro Misono case BTRFS_IOC_GET_SUBVOL_INFO: 6021b64ec075STomohiro Misono return btrfs_ioctl_get_subvol_info(file, argp); 602242e4b520STomohiro Misono case BTRFS_IOC_GET_SUBVOL_ROOTREF: 602342e4b520STomohiro Misono return btrfs_ioctl_get_subvol_rootref(file, argp); 602423d0b79dSTomohiro Misono case BTRFS_IOC_INO_LOOKUP_USER: 602523d0b79dSTomohiro Misono return btrfs_ioctl_ino_lookup_user(file, argp); 6026f46b5a66SChristoph Hellwig } 6027f46b5a66SChristoph Hellwig 6028f46b5a66SChristoph Hellwig return -ENOTTY; 6029f46b5a66SChristoph Hellwig } 60304c63c245SLuke Dashjr 60314c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 60324c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 60334c63c245SLuke Dashjr { 60342a362249SJeff Mahoney /* 60352a362249SJeff Mahoney * These all access 32-bit values anyway so no further 60362a362249SJeff Mahoney * handling is necessary. 60372a362249SJeff Mahoney */ 60384c63c245SLuke Dashjr switch (cmd) { 60394c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 60404c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 60414c63c245SLuke Dashjr break; 60424c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 60434c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 60444c63c245SLuke Dashjr break; 60454c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 60464c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 60474c63c245SLuke Dashjr break; 60484c63c245SLuke Dashjr } 60494c63c245SLuke Dashjr 60504c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 60514c63c245SLuke Dashjr } 60524c63c245SLuke Dashjr #endif 6053