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> 2997fc2977SMiklos Szeredi #include <linux/fileattr.h> 3014605409SBoris Burkov #include <linux/fsverity.h> 31*1881fba8SOmar Sandoval #include <linux/sched/xacct.h> 32f46b5a66SChristoph Hellwig #include "ctree.h" 33f46b5a66SChristoph Hellwig #include "disk-io.h" 34949964c9SMarcos Paulo de Souza #include "export.h" 35f46b5a66SChristoph Hellwig #include "transaction.h" 36f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 37f46b5a66SChristoph Hellwig #include "print-tree.h" 38f46b5a66SChristoph Hellwig #include "volumes.h" 39925baeddSChris Mason #include "locking.h" 40d7728c96SJan Schmidt #include "backref.h" 41606686eeSJosef Bacik #include "rcu-string.h" 4231db9f7cSAlexander Block #include "send.h" 433f6bcfbdSStefan Behrens #include "dev-replace.h" 4463541927SFilipe David Borba Manana #include "props.h" 453b02a68aSJeff Mahoney #include "sysfs.h" 46fcebe456SJosef Bacik #include "qgroup.h" 471ec9a1aeSFilipe Manana #include "tree-log.h" 48ebb8765bSAnand Jain #include "compression.h" 498719aaaeSJosef Bacik #include "space-info.h" 5086736342SJosef Bacik #include "delalloc-space.h" 51aac0023cSJosef Bacik #include "block-group.h" 5222b398eeSQu Wenruo #include "subpage.h" 53f46b5a66SChristoph Hellwig 54abccd00fSHugo Mills #ifdef CONFIG_64BIT 55abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 56abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 57abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 58abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 59abccd00fSHugo Mills */ 60abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 61abccd00fSHugo Mills __u64 sec; 62abccd00fSHugo Mills __u32 nsec; 63abccd00fSHugo Mills } __attribute__ ((__packed__)); 64abccd00fSHugo Mills 65abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 66abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 67abccd00fSHugo Mills __u64 stransid; /* in */ 68abccd00fSHugo Mills __u64 rtransid; /* out */ 69abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 70abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 71abccd00fSHugo Mills __u64 flags; /* in */ 72abccd00fSHugo Mills __u64 reserved[16]; /* in */ 73abccd00fSHugo Mills } __attribute__ ((__packed__)); 74abccd00fSHugo Mills 75abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 76abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 77abccd00fSHugo Mills #endif 78abccd00fSHugo Mills 792351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 802351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 812351f431SJosef Bacik __s64 send_fd; /* in */ 822351f431SJosef Bacik __u64 clone_sources_count; /* in */ 832351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 842351f431SJosef Bacik __u64 parent_root; /* in */ 852351f431SJosef Bacik __u64 flags; /* in */ 86e77fbf99SDavid Sterba __u32 version; /* in */ 87e77fbf99SDavid Sterba __u8 reserved[28]; /* in */ 882351f431SJosef Bacik } __attribute__ ((__packed__)); 892351f431SJosef Bacik 902351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 912351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 92*1881fba8SOmar Sandoval 93*1881fba8SOmar Sandoval struct btrfs_ioctl_encoded_io_args_32 { 94*1881fba8SOmar Sandoval compat_uptr_t iov; 95*1881fba8SOmar Sandoval compat_ulong_t iovcnt; 96*1881fba8SOmar Sandoval __s64 offset; 97*1881fba8SOmar Sandoval __u64 flags; 98*1881fba8SOmar Sandoval __u64 len; 99*1881fba8SOmar Sandoval __u64 unencoded_len; 100*1881fba8SOmar Sandoval __u64 unencoded_offset; 101*1881fba8SOmar Sandoval __u32 compression; 102*1881fba8SOmar Sandoval __u32 encryption; 103*1881fba8SOmar Sandoval __u8 reserved[64]; 104*1881fba8SOmar Sandoval }; 105*1881fba8SOmar Sandoval 106*1881fba8SOmar Sandoval #define BTRFS_IOC_ENCODED_READ_32 _IOR(BTRFS_IOCTL_MAGIC, 64, \ 107*1881fba8SOmar Sandoval struct btrfs_ioctl_encoded_io_args_32) 1082351f431SJosef Bacik #endif 109abccd00fSHugo Mills 1106cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 1111905a0f7SDavid Sterba static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, 1121905a0f7SDavid Sterba unsigned int flags) 1136cbff00fSChristoph Hellwig { 1141905a0f7SDavid Sterba if (S_ISDIR(inode->i_mode)) 1156cbff00fSChristoph Hellwig return flags; 1161905a0f7SDavid Sterba else if (S_ISREG(inode->i_mode)) 1176cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 1186cbff00fSChristoph Hellwig else 1196cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1206cbff00fSChristoph Hellwig } 121f46b5a66SChristoph Hellwig 1226cbff00fSChristoph Hellwig /* 123a157d4fdSDavid Sterba * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS 124a157d4fdSDavid Sterba * ioctl. 1256cbff00fSChristoph Hellwig */ 12677eea05eSBoris Burkov static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode) 1276cbff00fSChristoph Hellwig { 1286cbff00fSChristoph Hellwig unsigned int iflags = 0; 12977eea05eSBoris Burkov u32 flags = binode->flags; 13014605409SBoris Burkov u32 ro_flags = binode->ro_flags; 1316cbff00fSChristoph Hellwig 1326cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1336cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1346cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1356cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1366cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1376cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1386cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1396cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1406cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1416cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1426cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1436cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 144d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 145d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 14614605409SBoris Burkov if (ro_flags & BTRFS_INODE_RO_VERITY) 14714605409SBoris Burkov iflags |= FS_VERITY_FL; 148d0092bddSLi Zefan 14913f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 150d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 15113f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 15213f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1536cbff00fSChristoph Hellwig 1546cbff00fSChristoph Hellwig return iflags; 1556cbff00fSChristoph Hellwig } 1566cbff00fSChristoph Hellwig 1576cbff00fSChristoph Hellwig /* 1586cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1596cbff00fSChristoph Hellwig */ 1607b6a221eSDavid Sterba void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) 1616cbff00fSChristoph Hellwig { 1625c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1633cc79392SFilipe Manana unsigned int new_fl = 0; 1646cbff00fSChristoph Hellwig 1655c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_SYNC) 1663cc79392SFilipe Manana new_fl |= S_SYNC; 1675c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_IMMUTABLE) 1683cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1695c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_APPEND) 1703cc79392SFilipe Manana new_fl |= S_APPEND; 1715c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_NOATIME) 1723cc79392SFilipe Manana new_fl |= S_NOATIME; 1735c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_DIRSYNC) 1743cc79392SFilipe Manana new_fl |= S_DIRSYNC; 17514605409SBoris Burkov if (binode->ro_flags & BTRFS_INODE_RO_VERITY) 17614605409SBoris Burkov new_fl |= S_VERITY; 1773cc79392SFilipe Manana 1783cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 17914605409SBoris Burkov S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC | 18014605409SBoris Burkov S_VERITY, new_fl); 1816cbff00fSChristoph Hellwig } 1826cbff00fSChristoph Hellwig 183f37c563bSDavid Sterba /* 184f37c563bSDavid Sterba * Check if @flags are a supported and valid set of FS_*_FL flags and that 185f37c563bSDavid Sterba * the old and new flags are not conflicting 186f37c563bSDavid Sterba */ 187f37c563bSDavid Sterba static int check_fsflags(unsigned int old_flags, unsigned int flags) 18875e7cb7fSLiu Bo { 18975e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 19075e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 19175e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 192e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 193e1e8fb6aSLi Zefan FS_NOCOW_FL)) 19475e7cb7fSLiu Bo return -EOPNOTSUPP; 19575e7cb7fSLiu Bo 196f37c563bSDavid Sterba /* COMPR and NOCOMP on new/old are valid */ 19775e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 19875e7cb7fSLiu Bo return -EINVAL; 19975e7cb7fSLiu Bo 200f37c563bSDavid Sterba if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL)) 201f37c563bSDavid Sterba return -EINVAL; 202f37c563bSDavid Sterba 203f37c563bSDavid Sterba /* NOCOW and compression options are mutually exclusive */ 204f37c563bSDavid Sterba if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL))) 205f37c563bSDavid Sterba return -EINVAL; 206f37c563bSDavid Sterba if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL))) 207f37c563bSDavid Sterba return -EINVAL; 208f37c563bSDavid Sterba 20975e7cb7fSLiu Bo return 0; 21075e7cb7fSLiu Bo } 21175e7cb7fSLiu Bo 212d206e9c9SNaohiro Aota static int check_fsflags_compatible(struct btrfs_fs_info *fs_info, 213d206e9c9SNaohiro Aota unsigned int flags) 214d206e9c9SNaohiro Aota { 215d206e9c9SNaohiro Aota if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL)) 216d206e9c9SNaohiro Aota return -EPERM; 217d206e9c9SNaohiro Aota 218d206e9c9SNaohiro Aota return 0; 219d206e9c9SNaohiro Aota } 220d206e9c9SNaohiro Aota 22197fc2977SMiklos Szeredi /* 22297fc2977SMiklos Szeredi * Set flags/xflags from the internal inode flags. The remaining items of 22397fc2977SMiklos Szeredi * fsxattr are zeroed. 22497fc2977SMiklos Szeredi */ 22597fc2977SMiklos Szeredi int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) 2266cbff00fSChristoph Hellwig { 22797fc2977SMiklos Szeredi struct btrfs_inode *binode = BTRFS_I(d_inode(dentry)); 22897fc2977SMiklos Szeredi 22977eea05eSBoris Burkov fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode)); 23097fc2977SMiklos Szeredi return 0; 23197fc2977SMiklos Szeredi } 23297fc2977SMiklos Szeredi 23397fc2977SMiklos Szeredi int btrfs_fileattr_set(struct user_namespace *mnt_userns, 23497fc2977SMiklos Szeredi struct dentry *dentry, struct fileattr *fa) 23597fc2977SMiklos Szeredi { 23697fc2977SMiklos Szeredi struct inode *inode = d_inode(dentry); 2370b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2385c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 2395c57b8b6SDavid Sterba struct btrfs_root *root = binode->root; 2406cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2415aca2842SDarrick J. Wong unsigned int fsflags, old_fsflags; 2426cbff00fSChristoph Hellwig int ret; 243ff9fef55SAnand Jain const char *comp = NULL; 244f37c563bSDavid Sterba u32 binode_flags; 2456cbff00fSChristoph Hellwig 246b83cc969SLi Zefan if (btrfs_root_readonly(root)) 247b83cc969SLi Zefan return -EROFS; 248b83cc969SLi Zefan 24997fc2977SMiklos Szeredi if (fileattr_has_fsx(fa)) 25097fc2977SMiklos Szeredi return -EOPNOTSUPP; 2516cbff00fSChristoph Hellwig 25297fc2977SMiklos Szeredi fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags); 25377eea05eSBoris Burkov old_fsflags = btrfs_inode_flags_to_fsflags(binode); 25497fc2977SMiklos Szeredi ret = check_fsflags(old_fsflags, fsflags); 255e7848683SJan Kara if (ret) 256e7848683SJan Kara return ret; 257e7848683SJan Kara 258d206e9c9SNaohiro Aota ret = check_fsflags_compatible(fs_info, fsflags); 259d206e9c9SNaohiro Aota if (ret) 26097fc2977SMiklos Szeredi return ret; 261d206e9c9SNaohiro Aota 262f37c563bSDavid Sterba binode_flags = binode->flags; 2635c57b8b6SDavid Sterba if (fsflags & FS_SYNC_FL) 264d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_SYNC; 2656cbff00fSChristoph Hellwig else 266d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_SYNC; 2675c57b8b6SDavid Sterba if (fsflags & FS_IMMUTABLE_FL) 268d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_IMMUTABLE; 2696cbff00fSChristoph Hellwig else 270d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_IMMUTABLE; 2715c57b8b6SDavid Sterba if (fsflags & FS_APPEND_FL) 272d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_APPEND; 2736cbff00fSChristoph Hellwig else 274d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_APPEND; 2755c57b8b6SDavid Sterba if (fsflags & FS_NODUMP_FL) 276d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODUMP; 2776cbff00fSChristoph Hellwig else 278d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODUMP; 2795c57b8b6SDavid Sterba if (fsflags & FS_NOATIME_FL) 280d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOATIME; 2816cbff00fSChristoph Hellwig else 282d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOATIME; 28397fc2977SMiklos Szeredi 28497fc2977SMiklos Szeredi /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */ 28597fc2977SMiklos Szeredi if (!fa->flags_valid) { 28697fc2977SMiklos Szeredi /* 1 item for the inode */ 28797fc2977SMiklos Szeredi trans = btrfs_start_transaction(root, 1); 2889b8a233bSRitesh Harjani if (IS_ERR(trans)) 2899b8a233bSRitesh Harjani return PTR_ERR(trans); 29097fc2977SMiklos Szeredi goto update_flags; 29197fc2977SMiklos Szeredi } 29297fc2977SMiklos Szeredi 2935c57b8b6SDavid Sterba if (fsflags & FS_DIRSYNC_FL) 294d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_DIRSYNC; 2956cbff00fSChristoph Hellwig else 296d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_DIRSYNC; 2975c57b8b6SDavid Sterba if (fsflags & FS_NOCOW_FL) { 29844e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 2997e97b8daSDavid Sterba /* 3007e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 3017e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 3027e97b8daSDavid Sterba * status of the file and will not set it. 3037e97b8daSDavid Sterba */ 3047e97b8daSDavid Sterba if (inode->i_size == 0) 305d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW | 306d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM; 3077e97b8daSDavid Sterba } else { 308d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW; 3097e97b8daSDavid Sterba } 3107e97b8daSDavid Sterba } else { 3117e97b8daSDavid Sterba /* 31201327610SNicholas D Steeves * Revert back under same assumptions as above 3137e97b8daSDavid Sterba */ 31444e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 3157e97b8daSDavid Sterba if (inode->i_size == 0) 316d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_NODATACOW | 317d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM); 3187e97b8daSDavid Sterba } else { 319d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODATACOW; 3207e97b8daSDavid Sterba } 3217e97b8daSDavid Sterba } 3226cbff00fSChristoph Hellwig 32375e7cb7fSLiu Bo /* 32475e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 32575e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 32675e7cb7fSLiu Bo * things smaller. 32775e7cb7fSLiu Bo */ 3285c57b8b6SDavid Sterba if (fsflags & FS_NOCOMP_FL) { 329d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_COMPRESS; 330d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOCOMPRESS; 3315c57b8b6SDavid Sterba } else if (fsflags & FS_COMPR_FL) { 33263541927SFilipe David Borba Manana 33397fc2977SMiklos Szeredi if (IS_SWAPFILE(inode)) 33497fc2977SMiklos Szeredi return -ETXTBSY; 335eede2bf3SOmar Sandoval 336d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_COMPRESS; 337d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOCOMPRESS; 33863541927SFilipe David Borba Manana 33993370509SDavid Sterba comp = btrfs_compress_type2str(fs_info->compress_type); 34093370509SDavid Sterba if (!comp || comp[0] == 0) 34193370509SDavid Sterba comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); 342ebcb904dSLi Zefan } else { 343d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 34475e7cb7fSLiu Bo } 3456cbff00fSChristoph Hellwig 346ff9fef55SAnand Jain /* 347ff9fef55SAnand Jain * 1 for inode item 348ff9fef55SAnand Jain * 2 for properties 349ff9fef55SAnand Jain */ 350ff9fef55SAnand Jain trans = btrfs_start_transaction(root, 3); 35197fc2977SMiklos Szeredi if (IS_ERR(trans)) 35297fc2977SMiklos Szeredi return PTR_ERR(trans); 3536cbff00fSChristoph Hellwig 354ff9fef55SAnand Jain if (comp) { 355ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, 356ff9fef55SAnand Jain strlen(comp), 0); 357ff9fef55SAnand Jain if (ret) { 358ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 359ff9fef55SAnand Jain goto out_end_trans; 360ff9fef55SAnand Jain } 361ff9fef55SAnand Jain } else { 362ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 363ff9fef55SAnand Jain 0, 0); 364ff9fef55SAnand Jain if (ret && ret != -ENODATA) { 365ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 366ff9fef55SAnand Jain goto out_end_trans; 367ff9fef55SAnand Jain } 368ff9fef55SAnand Jain } 369ff9fef55SAnand Jain 37097fc2977SMiklos Szeredi update_flags: 371d2b8fcfeSAnand Jain binode->flags = binode_flags; 3727b6a221eSDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 3730c4d2d95SJosef Bacik inode_inc_iversion(inode); 374c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3759a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 3766cbff00fSChristoph Hellwig 377ff9fef55SAnand Jain out_end_trans: 3783a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3792d4e6f6aSliubo return ret; 3806cbff00fSChristoph Hellwig } 3816cbff00fSChristoph Hellwig 3820d7ed32cSDavid Sterba /* 3830d7ed32cSDavid Sterba * Start exclusive operation @type, return true on success 3840d7ed32cSDavid Sterba */ 385c3e1f96cSGoldwyn Rodrigues bool btrfs_exclop_start(struct btrfs_fs_info *fs_info, 386c3e1f96cSGoldwyn Rodrigues enum btrfs_exclusive_operation type) 387c3e1f96cSGoldwyn Rodrigues { 3880d7ed32cSDavid Sterba bool ret = false; 3890d7ed32cSDavid Sterba 3900d7ed32cSDavid Sterba spin_lock(&fs_info->super_lock); 3910d7ed32cSDavid Sterba if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) { 3920d7ed32cSDavid Sterba fs_info->exclusive_operation = type; 3930d7ed32cSDavid Sterba ret = true; 3940d7ed32cSDavid Sterba } 3950d7ed32cSDavid Sterba spin_unlock(&fs_info->super_lock); 3960d7ed32cSDavid Sterba 3970d7ed32cSDavid Sterba return ret; 398c3e1f96cSGoldwyn Rodrigues } 399c3e1f96cSGoldwyn Rodrigues 400578bda9eSDavid Sterba /* 401578bda9eSDavid Sterba * Conditionally allow to enter the exclusive operation in case it's compatible 402578bda9eSDavid Sterba * with the running one. This must be paired with btrfs_exclop_start_unlock and 403578bda9eSDavid Sterba * btrfs_exclop_finish. 404578bda9eSDavid Sterba * 405578bda9eSDavid Sterba * Compatibility: 406578bda9eSDavid Sterba * - the same type is already running 407621a1ee1SNikolay Borisov * - when trying to add a device and balance has been paused 408578bda9eSDavid Sterba * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller 409578bda9eSDavid Sterba * must check the condition first that would allow none -> @type 410578bda9eSDavid Sterba */ 411578bda9eSDavid Sterba bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info, 412578bda9eSDavid Sterba enum btrfs_exclusive_operation type) 413578bda9eSDavid Sterba { 414578bda9eSDavid Sterba spin_lock(&fs_info->super_lock); 415621a1ee1SNikolay Borisov if (fs_info->exclusive_operation == type || 416621a1ee1SNikolay Borisov (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED && 417621a1ee1SNikolay Borisov type == BTRFS_EXCLOP_DEV_ADD)) 418578bda9eSDavid Sterba return true; 419578bda9eSDavid Sterba 420578bda9eSDavid Sterba spin_unlock(&fs_info->super_lock); 421578bda9eSDavid Sterba return false; 422578bda9eSDavid Sterba } 423578bda9eSDavid Sterba 424578bda9eSDavid Sterba void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info) 425578bda9eSDavid Sterba { 426578bda9eSDavid Sterba spin_unlock(&fs_info->super_lock); 427578bda9eSDavid Sterba } 428578bda9eSDavid Sterba 429c3e1f96cSGoldwyn Rodrigues void btrfs_exclop_finish(struct btrfs_fs_info *fs_info) 430c3e1f96cSGoldwyn Rodrigues { 4310d7ed32cSDavid Sterba spin_lock(&fs_info->super_lock); 432c3e1f96cSGoldwyn Rodrigues WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE); 4330d7ed32cSDavid Sterba spin_unlock(&fs_info->super_lock); 43466a2823cSGoldwyn Rodrigues sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation"); 435c3e1f96cSGoldwyn Rodrigues } 436c3e1f96cSGoldwyn Rodrigues 437efc0e69cSNikolay Borisov void btrfs_exclop_balance(struct btrfs_fs_info *fs_info, 438efc0e69cSNikolay Borisov enum btrfs_exclusive_operation op) 439efc0e69cSNikolay Borisov { 440efc0e69cSNikolay Borisov switch (op) { 441efc0e69cSNikolay Borisov case BTRFS_EXCLOP_BALANCE_PAUSED: 442efc0e69cSNikolay Borisov spin_lock(&fs_info->super_lock); 443efc0e69cSNikolay Borisov ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE || 444efc0e69cSNikolay Borisov fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD); 445efc0e69cSNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED; 446efc0e69cSNikolay Borisov spin_unlock(&fs_info->super_lock); 447efc0e69cSNikolay Borisov break; 448efc0e69cSNikolay Borisov case BTRFS_EXCLOP_BALANCE: 449efc0e69cSNikolay Borisov spin_lock(&fs_info->super_lock); 450efc0e69cSNikolay Borisov ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 451efc0e69cSNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; 452efc0e69cSNikolay Borisov spin_unlock(&fs_info->super_lock); 453efc0e69cSNikolay Borisov break; 454efc0e69cSNikolay Borisov default: 455efc0e69cSNikolay Borisov btrfs_warn(fs_info, 456efc0e69cSNikolay Borisov "invalid exclop balance operation %d requested", op); 457efc0e69cSNikolay Borisov } 458efc0e69cSNikolay Borisov } 459efc0e69cSNikolay Borisov 460dc408ccdSSahil Kang static int btrfs_ioctl_getversion(struct inode *inode, int __user *arg) 4616cbff00fSChristoph Hellwig { 4626cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 4636cbff00fSChristoph Hellwig } 464f46b5a66SChristoph Hellwig 465b929c1d8SMarcos Paulo de Souza static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, 466b929c1d8SMarcos Paulo de Souza void __user *arg) 467f7039b1dSLi Dongyang { 468f7039b1dSLi Dongyang struct btrfs_device *device; 469f7039b1dSLi Dongyang struct request_queue *q; 470f7039b1dSLi Dongyang struct fstrim_range range; 471f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 472f7039b1dSLi Dongyang u64 num_devices = 0; 473f7039b1dSLi Dongyang int ret; 474f7039b1dSLi Dongyang 475f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 476f7039b1dSLi Dongyang return -EPERM; 477f7039b1dSLi Dongyang 478f35f06c3SFilipe Manana /* 4791cb3dc3fSNaohiro Aota * btrfs_trim_block_group() depends on space cache, which is not 4801cb3dc3fSNaohiro Aota * available in zoned filesystem. So, disallow fitrim on a zoned 4811cb3dc3fSNaohiro Aota * filesystem for now. 4821cb3dc3fSNaohiro Aota */ 4831cb3dc3fSNaohiro Aota if (btrfs_is_zoned(fs_info)) 4841cb3dc3fSNaohiro Aota return -EOPNOTSUPP; 4851cb3dc3fSNaohiro Aota 4861cb3dc3fSNaohiro Aota /* 487f35f06c3SFilipe Manana * If the fs is mounted with nologreplay, which requires it to be 488f35f06c3SFilipe Manana * mounted in RO mode as well, we can not allow discard on free space 489f35f06c3SFilipe Manana * inside block groups, because log trees refer to extents that are not 490f35f06c3SFilipe Manana * pinned in a block group's free space cache (pinning the extents is 491f35f06c3SFilipe Manana * precisely the first phase of replaying a log tree). 492f35f06c3SFilipe Manana */ 493f35f06c3SFilipe Manana if (btrfs_test_opt(fs_info, NOLOGREPLAY)) 494f35f06c3SFilipe Manana return -EROFS; 495f35f06c3SFilipe Manana 4961f78160cSXiao Guangrong rcu_read_lock(); 4971f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 4981f78160cSXiao Guangrong dev_list) { 499f7039b1dSLi Dongyang if (!device->bdev) 500f7039b1dSLi Dongyang continue; 501f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 502f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 503f7039b1dSLi Dongyang num_devices++; 50450d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 505f7039b1dSLi Dongyang minlen); 506f7039b1dSLi Dongyang } 507f7039b1dSLi Dongyang } 5081f78160cSXiao Guangrong rcu_read_unlock(); 509f4c697e6SLukas Czerner 510f7039b1dSLi Dongyang if (!num_devices) 511f7039b1dSLi Dongyang return -EOPNOTSUPP; 512f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 513f7039b1dSLi Dongyang return -EFAULT; 5146ba9fc8eSQu Wenruo 5156ba9fc8eSQu Wenruo /* 5166ba9fc8eSQu Wenruo * NOTE: Don't truncate the range using super->total_bytes. Bytenr of 5176ba9fc8eSQu Wenruo * block group is in the logical address space, which can be any 5186ba9fc8eSQu Wenruo * sectorsize aligned bytenr in the range [0, U64_MAX]. 5196ba9fc8eSQu Wenruo */ 5206ba9fc8eSQu Wenruo if (range.len < fs_info->sb->s_blocksize) 521f4c697e6SLukas Czerner return -EINVAL; 522f7039b1dSLi Dongyang 523f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 5242ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 525f7039b1dSLi Dongyang if (ret < 0) 526f7039b1dSLi Dongyang return ret; 527f7039b1dSLi Dongyang 528f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 529f7039b1dSLi Dongyang return -EFAULT; 530f7039b1dSLi Dongyang 531f7039b1dSLi Dongyang return 0; 532f7039b1dSLi Dongyang } 533f7039b1dSLi Dongyang 534e1f60a65SDavid Sterba int __pure btrfs_is_empty_uuid(u8 *uuid) 535dd5f9615SStefan Behrens { 53646e0f66aSChris Mason int i; 53746e0f66aSChris Mason 53846e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 53946e0f66aSChris Mason if (uuid[i]) 54046e0f66aSChris Mason return 0; 54146e0f66aSChris Mason } 54246e0f66aSChris Mason return 1; 543dd5f9615SStefan Behrens } 544dd5f9615SStefan Behrens 5454d4340c9SChristian Brauner static noinline int create_subvol(struct user_namespace *mnt_userns, 5464d4340c9SChristian Brauner struct inode *dir, struct dentry *dentry, 54752f75f4fSDavid Sterba const char *name, int namelen, 5488696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 549f46b5a66SChristoph Hellwig { 5500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 551f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 552f46b5a66SChristoph Hellwig struct btrfs_key key; 55349a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 554f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 555f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 556d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 55776dda93cSYan, Zheng struct btrfs_root *new_root; 558d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 55995582b00SDeepa Dinamani struct timespec64 cur_time = current_time(dir); 5605662344bSTsutomu Itoh struct inode *inode; 561f46b5a66SChristoph Hellwig int ret; 5622dfb1e43SQu Wenruo dev_t anon_dev = 0; 563f46b5a66SChristoph Hellwig u64 objectid; 5643de4586cSChris Mason u64 index = 0; 565f46b5a66SChristoph Hellwig 56649a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 56749a3c4d9SDavid Sterba if (!root_item) 56849a3c4d9SDavid Sterba return -ENOMEM; 56949a3c4d9SDavid Sterba 570543068a2SNikolay Borisov ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid); 5712fbe8c8aSAl Viro if (ret) 57249a3c4d9SDavid Sterba goto fail_free; 5736a912213SJosef Bacik 5742dfb1e43SQu Wenruo ret = get_anon_bdev(&anon_dev); 5752dfb1e43SQu Wenruo if (ret < 0) 5762dfb1e43SQu Wenruo goto fail_free; 5772dfb1e43SQu Wenruo 578e09fe2d2SQu Wenruo /* 579e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 58001327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 581e09fe2d2SQu Wenruo */ 58249a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 58349a3c4d9SDavid Sterba ret = -ENOSPC; 58449a3c4d9SDavid Sterba goto fail_free; 58549a3c4d9SDavid Sterba } 586e09fe2d2SQu Wenruo 587d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 5889ed74f2dSJosef Bacik /* 589d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 590d5c12070SMiao Xie * of create_snapshot(). 5919ed74f2dSJosef Bacik */ 592c4c129dbSGu JinXiang ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); 593d5c12070SMiao Xie if (ret) 59449a3c4d9SDavid Sterba goto fail_free; 595f46b5a66SChristoph Hellwig 596d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 597d5c12070SMiao Xie if (IS_ERR(trans)) { 598d5c12070SMiao Xie ret = PTR_ERR(trans); 599e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &block_rsv); 60049a3c4d9SDavid Sterba goto fail_free; 601d5c12070SMiao Xie } 602d5c12070SMiao Xie trans->block_rsv = &block_rsv; 603d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 604f46b5a66SChristoph Hellwig 605a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); 6066f72c7e2SArne Jansen if (ret) 6076f72c7e2SArne Jansen goto fail; 6086f72c7e2SArne Jansen 6099631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 6109631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 6118e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 6128e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 6138e8a1e31SJosef Bacik goto fail; 6148e8a1e31SJosef Bacik } 615f46b5a66SChristoph Hellwig 616f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 617f46b5a66SChristoph Hellwig 61849a3c4d9SDavid Sterba inode_item = &root_item->inode; 6193cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 6203cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 6213cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 622da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 6230b246afaSJeff Mahoney fs_info->nodesize); 6243cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 625f46b5a66SChristoph Hellwig 62649a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 62749a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 6283cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 62908fe4db1SLi Zefan 63049a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 63149a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 63249a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 63349a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 63449a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 63549a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 636f46b5a66SChristoph Hellwig 63749a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 63849a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 639807fc790SAndy Shevchenko generate_random_guid(root_item->uuid); 64049a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 64149a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 64249a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 64349a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 64449a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 645f46b5a66SChristoph Hellwig 646925baeddSChris Mason btrfs_tree_unlock(leaf); 647f46b5a66SChristoph Hellwig 64869948022SNikolay Borisov btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID); 649f46b5a66SChristoph Hellwig 650f46b5a66SChristoph Hellwig key.objectid = objectid; 6515d4f98a2SYan Zheng key.offset = 0; 652962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 6530b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 65449a3c4d9SDavid Sterba root_item); 65567addf29SFilipe Manana if (ret) { 65667addf29SFilipe Manana /* 65767addf29SFilipe Manana * Since we don't abort the transaction in this case, free the 65867addf29SFilipe Manana * tree block so that we don't leak space and leave the 65967addf29SFilipe Manana * filesystem in an inconsistent state (an extent item in the 6607a163608SFilipe Manana * extent tree with a backreference for a root that does not 661212a58fdSFilipe Manana * exists). 66267addf29SFilipe Manana */ 663212a58fdSFilipe Manana btrfs_tree_lock(leaf); 664212a58fdSFilipe Manana btrfs_clean_tree_block(leaf); 665212a58fdSFilipe Manana btrfs_tree_unlock(leaf); 6667a163608SFilipe Manana btrfs_free_tree_block(trans, objectid, leaf, 0, 1); 66767addf29SFilipe Manana free_extent_buffer(leaf); 668f46b5a66SChristoph Hellwig goto fail; 66967addf29SFilipe Manana } 67067addf29SFilipe Manana 67167addf29SFilipe Manana free_extent_buffer(leaf); 67267addf29SFilipe Manana leaf = NULL; 673f46b5a66SChristoph Hellwig 67476dda93cSYan, Zheng key.offset = (u64)-1; 6752dfb1e43SQu Wenruo new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev); 67679787eaaSJeff Mahoney if (IS_ERR(new_root)) { 6772dfb1e43SQu Wenruo free_anon_bdev(anon_dev); 67879787eaaSJeff Mahoney ret = PTR_ERR(new_root); 67966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 68079787eaaSJeff Mahoney goto fail; 68179787eaaSJeff Mahoney } 6822dfb1e43SQu Wenruo /* Freeing will be done in btrfs_put_root() of new_root */ 6832dfb1e43SQu Wenruo anon_dev = 0; 68476dda93cSYan, Zheng 685221581e4SJosef Bacik ret = btrfs_record_root_in_trans(trans, new_root); 686221581e4SJosef Bacik if (ret) { 687221581e4SJosef Bacik btrfs_put_root(new_root); 688221581e4SJosef Bacik btrfs_abort_transaction(trans, ret); 689221581e4SJosef Bacik goto fail; 690221581e4SJosef Bacik } 69176dda93cSYan, Zheng 6924d4340c9SChristian Brauner ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns); 69300246528SJosef Bacik btrfs_put_root(new_root); 694ce598979SMark Fasheh if (ret) { 695ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 69666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 697ce598979SMark Fasheh goto fail; 698ce598979SMark Fasheh } 699ce598979SMark Fasheh 700f46b5a66SChristoph Hellwig /* 701f46b5a66SChristoph Hellwig * insert the directory item 702f46b5a66SChristoph Hellwig */ 703877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 70479787eaaSJeff Mahoney if (ret) { 70566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 70679787eaaSJeff Mahoney goto fail; 70779787eaaSJeff Mahoney } 7083de4586cSChris Mason 709684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, 7103de4586cSChris Mason BTRFS_FT_DIR, index); 71179787eaaSJeff Mahoney if (ret) { 71266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 713f46b5a66SChristoph Hellwig goto fail; 71479787eaaSJeff Mahoney } 7150660b5afSChris Mason 7166ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 7179a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(dir)); 718c7e54b51SJosef Bacik if (ret) { 719c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 720c7e54b51SJosef Bacik goto fail; 721c7e54b51SJosef Bacik } 72252c26179SYan Zheng 7236025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, 7244a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 725c7e54b51SJosef Bacik if (ret) { 726c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 727c7e54b51SJosef Bacik goto fail; 728c7e54b51SJosef Bacik } 7290660b5afSChris Mason 730cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, root_item->uuid, 7316bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 732dd5f9615SStefan Behrens if (ret) 73366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 734dd5f9615SStefan Behrens 735f46b5a66SChristoph Hellwig fail: 73649a3c4d9SDavid Sterba kfree(root_item); 737d5c12070SMiao Xie trans->block_rsv = NULL; 738d5c12070SMiao Xie trans->bytes_reserved = 0; 739e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &block_rsv); 740de6e8200SLiu Bo 7411b58ae0eSFilipe Manana if (ret) 7421b58ae0eSFilipe Manana btrfs_end_transaction(trans); 7431b58ae0eSFilipe Manana else 7441b58ae0eSFilipe Manana ret = btrfs_commit_transaction(trans); 7451a65e24bSChris Mason 7465662344bSTsutomu Itoh if (!ret) { 7475662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 748de6e8200SLiu Bo if (IS_ERR(inode)) 749de6e8200SLiu Bo return PTR_ERR(inode); 7505662344bSTsutomu Itoh d_instantiate(dentry, inode); 7515662344bSTsutomu Itoh } 752f46b5a66SChristoph Hellwig return ret; 75349a3c4d9SDavid Sterba 75449a3c4d9SDavid Sterba fail_free: 7552dfb1e43SQu Wenruo if (anon_dev) 7562dfb1e43SQu Wenruo free_anon_bdev(anon_dev); 75749a3c4d9SDavid Sterba kfree(root_item); 75849a3c4d9SDavid Sterba return ret; 759f46b5a66SChristoph Hellwig } 760f46b5a66SChristoph Hellwig 761e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 7629babda9fSNikolay Borisov struct dentry *dentry, bool readonly, 763e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 764f46b5a66SChristoph Hellwig { 7650b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 7662e4bfab9SYan, Zheng struct inode *inode; 767f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 768f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 7692e4bfab9SYan, Zheng int ret; 770f46b5a66SChristoph Hellwig 771813febdbSJosef Bacik /* We do not support snapshotting right now. */ 772813febdbSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 773813febdbSJosef Bacik btrfs_warn(fs_info, 774813febdbSJosef Bacik "extent tree v2 doesn't support snapshotting yet"); 775813febdbSJosef Bacik return -EOPNOTSUPP; 776813febdbSJosef Bacik } 777813febdbSJosef Bacik 77892a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 779f46b5a66SChristoph Hellwig return -EINVAL; 780f46b5a66SChristoph Hellwig 781eede2bf3SOmar Sandoval if (atomic_read(&root->nr_swapfiles)) { 782eede2bf3SOmar Sandoval btrfs_warn(fs_info, 783eede2bf3SOmar Sandoval "cannot snapshot subvolume with active swapfile"); 784eede2bf3SOmar Sandoval return -ETXTBSY; 785eede2bf3SOmar Sandoval } 786eede2bf3SOmar Sandoval 78723269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 788a1ee7362SDavid Sterba if (!pending_snapshot) 789a1ee7362SDavid Sterba return -ENOMEM; 790a1ee7362SDavid Sterba 7912dfb1e43SQu Wenruo ret = get_anon_bdev(&pending_snapshot->anon_dev); 7922dfb1e43SQu Wenruo if (ret < 0) 7932dfb1e43SQu Wenruo goto free_pending; 794b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 79523269bf5SDavid Sterba GFP_KERNEL); 7968546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 7978546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 798b0c0ea63SDavid Sterba ret = -ENOMEM; 799b0c0ea63SDavid Sterba goto free_pending; 800b0c0ea63SDavid Sterba } 801b0c0ea63SDavid Sterba 80266d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 80366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 804d5c12070SMiao Xie /* 805d5c12070SMiao Xie * 1 - parent dir inode 806d5c12070SMiao Xie * 2 - dir entries 807d5c12070SMiao Xie * 1 - root item 808d5c12070SMiao Xie * 2 - root ref/backref 809d5c12070SMiao Xie * 1 - root of snapshot 810dd5f9615SStefan Behrens * 1 - UUID item 811d5c12070SMiao Xie */ 812d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 813dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 814ee3441b4SJeff Mahoney false); 815d5c12070SMiao Xie if (ret) 816c11fbb6eSRobbie Ko goto free_pending; 817d5c12070SMiao Xie 818a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 819a22285a6SYan, Zheng pending_snapshot->root = root; 820b83cc969SLi Zefan pending_snapshot->readonly = readonly; 821e9662f70SMiao Xie pending_snapshot->dir = dir; 8228696c533SMiao Xie pending_snapshot->inherit = inherit; 823a22285a6SYan, Zheng 824d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 825a22285a6SYan, Zheng if (IS_ERR(trans)) { 826a22285a6SYan, Zheng ret = PTR_ERR(trans); 827a22285a6SYan, Zheng goto fail; 828a22285a6SYan, Zheng } 829a22285a6SYan, Zheng 83028b21c55SFilipe Manana trans->pending_snapshot = pending_snapshot; 8319babda9fSNikolay Borisov 8323a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 833aec8030aSMiao Xie if (ret) 834c37b2b62SJosef Bacik goto fail; 835a22285a6SYan, Zheng 836a22285a6SYan, Zheng ret = pending_snapshot->error; 837f46b5a66SChristoph Hellwig if (ret) 8382e4bfab9SYan, Zheng goto fail; 839f46b5a66SChristoph Hellwig 840d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 841d3797308SChris Mason if (ret) 842d3797308SChris Mason goto fail; 843d3797308SChris Mason 8442b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 8452e4bfab9SYan, Zheng if (IS_ERR(inode)) { 8462e4bfab9SYan, Zheng ret = PTR_ERR(inode); 8472e4bfab9SYan, Zheng goto fail; 8482e4bfab9SYan, Zheng } 8495662344bSTsutomu Itoh 8502e4bfab9SYan, Zheng d_instantiate(dentry, inode); 8512e4bfab9SYan, Zheng ret = 0; 8522dfb1e43SQu Wenruo pending_snapshot->anon_dev = 0; 8532e4bfab9SYan, Zheng fail: 8542dfb1e43SQu Wenruo /* Prevent double freeing of anon_dev */ 8552dfb1e43SQu Wenruo if (ret && pending_snapshot->snap) 8562dfb1e43SQu Wenruo pending_snapshot->snap->anon_dev = 0; 85700246528SJosef Bacik btrfs_put_root(pending_snapshot->snap); 858e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv); 859b0c0ea63SDavid Sterba free_pending: 8602dfb1e43SQu Wenruo if (pending_snapshot->anon_dev) 8612dfb1e43SQu Wenruo free_anon_bdev(pending_snapshot->anon_dev); 862b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 8638546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 864a1ee7362SDavid Sterba kfree(pending_snapshot); 865a1ee7362SDavid Sterba 866f46b5a66SChristoph Hellwig return ret; 867f46b5a66SChristoph Hellwig } 868f46b5a66SChristoph Hellwig 8694260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 8704260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 8714260f7c7SSage Weil * whether the type of victim is right. 8724260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 8734260f7c7SSage Weil * 2. We should have write and exec permissions on dir 8744260f7c7SSage Weil * 3. We can't remove anything from append-only dir 8754260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 8764260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 8774260f7c7SSage Weil * a. be owner of dir, or 8784260f7c7SSage Weil * b. be owner of victim, or 8794260f7c7SSage Weil * c. have CAP_FOWNER capability 88001327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 8814260f7c7SSage Weil * links pointing to it. 8824260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 8834260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 8844260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 8854260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 8864260f7c7SSage Weil * nfs_async_unlink(). 8874260f7c7SSage Weil */ 8884260f7c7SSage Weil 889c4ed533bSChristian Brauner static int btrfs_may_delete(struct user_namespace *mnt_userns, 890c4ed533bSChristian Brauner struct inode *dir, struct dentry *victim, int isdir) 8914260f7c7SSage Weil { 8924260f7c7SSage Weil int error; 8934260f7c7SSage Weil 8942b0143b5SDavid Howells if (d_really_is_negative(victim)) 8954260f7c7SSage Weil return -ENOENT; 8964260f7c7SSage Weil 8972b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 8984fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 8994260f7c7SSage Weil 900c4ed533bSChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 9014260f7c7SSage Weil if (error) 9024260f7c7SSage Weil return error; 9034260f7c7SSage Weil if (IS_APPEND(dir)) 9044260f7c7SSage Weil return -EPERM; 905c4ed533bSChristian Brauner if (check_sticky(mnt_userns, dir, d_inode(victim)) || 906ba73d987SChristian Brauner IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) || 907ba73d987SChristian Brauner IS_SWAPFILE(d_inode(victim))) 9084260f7c7SSage Weil return -EPERM; 9094260f7c7SSage Weil if (isdir) { 910e36cb0b8SDavid Howells if (!d_is_dir(victim)) 9114260f7c7SSage Weil return -ENOTDIR; 9124260f7c7SSage Weil if (IS_ROOT(victim)) 9134260f7c7SSage Weil return -EBUSY; 914e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 9154260f7c7SSage Weil return -EISDIR; 9164260f7c7SSage Weil if (IS_DEADDIR(dir)) 9174260f7c7SSage Weil return -ENOENT; 9184260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 9194260f7c7SSage Weil return -EBUSY; 9204260f7c7SSage Weil return 0; 9214260f7c7SSage Weil } 9224260f7c7SSage Weil 923cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 9244d4340c9SChristian Brauner static inline int btrfs_may_create(struct user_namespace *mnt_userns, 9254d4340c9SChristian Brauner struct inode *dir, struct dentry *child) 926cb8e7090SChristoph Hellwig { 9272b0143b5SDavid Howells if (d_really_is_positive(child)) 928cb8e7090SChristoph Hellwig return -EEXIST; 929cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 930cb8e7090SChristoph Hellwig return -ENOENT; 9314d4340c9SChristian Brauner if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) 9325474bf40SChristian Brauner return -EOVERFLOW; 9334d4340c9SChristian Brauner return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 934cb8e7090SChristoph Hellwig } 935cb8e7090SChristoph Hellwig 936cb8e7090SChristoph Hellwig /* 937cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 938cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 939cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 940cb8e7090SChristoph Hellwig */ 94192872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 9424d4340c9SChristian Brauner struct user_namespace *mnt_userns, 94352f75f4fSDavid Sterba const char *name, int namelen, 94472fd032eSSage Weil struct btrfs_root *snap_src, 9459babda9fSNikolay Borisov bool readonly, 9468696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 947cb8e7090SChristoph Hellwig { 9482b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 9490b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 950cb8e7090SChristoph Hellwig struct dentry *dentry; 951cb8e7090SChristoph Hellwig int error; 952cb8e7090SChristoph Hellwig 95300235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 95400235411SAl Viro if (error == -EINTR) 95500235411SAl Viro return error; 956cb8e7090SChristoph Hellwig 9574d4340c9SChristian Brauner dentry = lookup_one(mnt_userns, name, parent->dentry, namelen); 958cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 959cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 960cb8e7090SChristoph Hellwig goto out_unlock; 961cb8e7090SChristoph Hellwig 9624d4340c9SChristian Brauner error = btrfs_may_create(mnt_userns, dir, dentry); 963cb8e7090SChristoph Hellwig if (error) 964a874a63eSLiu Bo goto out_dput; 965cb8e7090SChristoph Hellwig 9669c52057cSChris Mason /* 9679c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 9689c52057cSChris Mason * check for them now when we can safely fail 9699c52057cSChris Mason */ 9709c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 9719c52057cSChris Mason dir->i_ino, name, 9729c52057cSChris Mason namelen); 9739c52057cSChris Mason if (error) 9749c52057cSChris Mason goto out_dput; 9759c52057cSChris Mason 9760b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 97776dda93cSYan, Zheng 97876dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 97976dda93cSYan, Zheng goto out_up_read; 98076dda93cSYan, Zheng 9819babda9fSNikolay Borisov if (snap_src) 9829babda9fSNikolay Borisov error = create_snapshot(snap_src, dir, dentry, readonly, inherit); 9839babda9fSNikolay Borisov else 9844d4340c9SChristian Brauner error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit); 9859babda9fSNikolay Borisov 98676dda93cSYan, Zheng if (!error) 98776dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 98876dda93cSYan, Zheng out_up_read: 9890b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 990cb8e7090SChristoph Hellwig out_dput: 991cb8e7090SChristoph Hellwig dput(dentry); 992cb8e7090SChristoph Hellwig out_unlock: 99364708539SJosef Bacik btrfs_inode_unlock(dir, 0); 994cb8e7090SChristoph Hellwig return error; 995cb8e7090SChristoph Hellwig } 996cb8e7090SChristoph Hellwig 997c11fbb6eSRobbie Ko static noinline int btrfs_mksnapshot(const struct path *parent, 9984d4340c9SChristian Brauner struct user_namespace *mnt_userns, 999c11fbb6eSRobbie Ko const char *name, int namelen, 1000c11fbb6eSRobbie Ko struct btrfs_root *root, 1001c11fbb6eSRobbie Ko bool readonly, 1002c11fbb6eSRobbie Ko struct btrfs_qgroup_inherit *inherit) 1003c11fbb6eSRobbie Ko { 1004c11fbb6eSRobbie Ko int ret; 1005c11fbb6eSRobbie Ko bool snapshot_force_cow = false; 1006c11fbb6eSRobbie Ko 1007c11fbb6eSRobbie Ko /* 1008c11fbb6eSRobbie Ko * Force new buffered writes to reserve space even when NOCOW is 1009c11fbb6eSRobbie Ko * possible. This is to avoid later writeback (running dealloc) to 1010c11fbb6eSRobbie Ko * fallback to COW mode and unexpectedly fail with ENOSPC. 1011c11fbb6eSRobbie Ko */ 1012c11fbb6eSRobbie Ko btrfs_drew_read_lock(&root->snapshot_lock); 1013c11fbb6eSRobbie Ko 1014f9baa501SFilipe Manana ret = btrfs_start_delalloc_snapshot(root, false); 1015c11fbb6eSRobbie Ko if (ret) 1016c11fbb6eSRobbie Ko goto out; 1017c11fbb6eSRobbie Ko 1018c11fbb6eSRobbie Ko /* 1019c11fbb6eSRobbie Ko * All previous writes have started writeback in NOCOW mode, so now 1020c11fbb6eSRobbie Ko * we force future writes to fallback to COW mode during snapshot 1021c11fbb6eSRobbie Ko * creation. 1022c11fbb6eSRobbie Ko */ 1023c11fbb6eSRobbie Ko atomic_inc(&root->snapshot_force_cow); 1024c11fbb6eSRobbie Ko snapshot_force_cow = true; 1025c11fbb6eSRobbie Ko 1026c11fbb6eSRobbie Ko btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 1027c11fbb6eSRobbie Ko 10284d4340c9SChristian Brauner ret = btrfs_mksubvol(parent, mnt_userns, name, namelen, 1029c11fbb6eSRobbie Ko root, readonly, inherit); 1030c11fbb6eSRobbie Ko out: 1031c11fbb6eSRobbie Ko if (snapshot_force_cow) 1032c11fbb6eSRobbie Ko atomic_dec(&root->snapshot_force_cow); 1033c11fbb6eSRobbie Ko btrfs_drew_read_unlock(&root->snapshot_lock); 1034c11fbb6eSRobbie Ko return ret; 1035c11fbb6eSRobbie Ko } 1036c11fbb6eSRobbie Ko 1037d5633b0dSQu Wenruo /* 1038d5633b0dSQu Wenruo * Defrag specific helper to get an extent map. 1039d5633b0dSQu Wenruo * 1040d5633b0dSQu Wenruo * Differences between this and btrfs_get_extent() are: 1041d5633b0dSQu Wenruo * 1042d5633b0dSQu Wenruo * - No extent_map will be added to inode->extent_tree 1043d5633b0dSQu Wenruo * To reduce memory usage in the long run. 1044d5633b0dSQu Wenruo * 1045d5633b0dSQu Wenruo * - Extra optimization to skip file extents older than @newer_than 1046d5633b0dSQu Wenruo * By using btrfs_search_forward() we can skip entire file ranges that 1047d5633b0dSQu Wenruo * have extents created in past transactions, because btrfs_search_forward() 1048d5633b0dSQu Wenruo * will not visit leaves and nodes with a generation smaller than given 1049d5633b0dSQu Wenruo * minimal generation threshold (@newer_than). 1050d5633b0dSQu Wenruo * 1051d5633b0dSQu Wenruo * Return valid em if we find a file extent matching the requirement. 1052d5633b0dSQu Wenruo * Return NULL if we can not find a file extent matching the requirement. 1053d5633b0dSQu Wenruo * 1054d5633b0dSQu Wenruo * Return ERR_PTR() for error. 1055d5633b0dSQu Wenruo */ 1056d5633b0dSQu Wenruo static struct extent_map *defrag_get_extent(struct btrfs_inode *inode, 1057d5633b0dSQu Wenruo u64 start, u64 newer_than) 1058d5633b0dSQu Wenruo { 1059d5633b0dSQu Wenruo struct btrfs_root *root = inode->root; 1060d5633b0dSQu Wenruo struct btrfs_file_extent_item *fi; 1061d5633b0dSQu Wenruo struct btrfs_path path = { 0 }; 1062d5633b0dSQu Wenruo struct extent_map *em; 1063d5633b0dSQu Wenruo struct btrfs_key key; 1064d5633b0dSQu Wenruo u64 ino = btrfs_ino(inode); 1065d5633b0dSQu Wenruo int ret; 1066d5633b0dSQu Wenruo 1067d5633b0dSQu Wenruo em = alloc_extent_map(); 1068d5633b0dSQu Wenruo if (!em) { 1069d5633b0dSQu Wenruo ret = -ENOMEM; 1070d5633b0dSQu Wenruo goto err; 1071d5633b0dSQu Wenruo } 1072d5633b0dSQu Wenruo 1073d5633b0dSQu Wenruo key.objectid = ino; 1074d5633b0dSQu Wenruo key.type = BTRFS_EXTENT_DATA_KEY; 1075d5633b0dSQu Wenruo key.offset = start; 1076d5633b0dSQu Wenruo 1077d5633b0dSQu Wenruo if (newer_than) { 1078d5633b0dSQu Wenruo ret = btrfs_search_forward(root, &key, &path, newer_than); 1079d5633b0dSQu Wenruo if (ret < 0) 1080d5633b0dSQu Wenruo goto err; 1081d5633b0dSQu Wenruo /* Can't find anything newer */ 1082d5633b0dSQu Wenruo if (ret > 0) 1083d5633b0dSQu Wenruo goto not_found; 1084d5633b0dSQu Wenruo } else { 1085d5633b0dSQu Wenruo ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); 1086d5633b0dSQu Wenruo if (ret < 0) 1087d5633b0dSQu Wenruo goto err; 1088d5633b0dSQu Wenruo } 1089d5633b0dSQu Wenruo if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) { 1090d5633b0dSQu Wenruo /* 1091d5633b0dSQu Wenruo * If btrfs_search_slot() makes path to point beyond nritems, 1092d5633b0dSQu Wenruo * we should not have an empty leaf, as this inode must at 1093d5633b0dSQu Wenruo * least have its INODE_ITEM. 1094d5633b0dSQu Wenruo */ 1095d5633b0dSQu Wenruo ASSERT(btrfs_header_nritems(path.nodes[0])); 1096d5633b0dSQu Wenruo path.slots[0] = btrfs_header_nritems(path.nodes[0]) - 1; 1097d5633b0dSQu Wenruo } 1098d5633b0dSQu Wenruo btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); 1099d5633b0dSQu Wenruo /* Perfect match, no need to go one slot back */ 1100d5633b0dSQu Wenruo if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY && 1101d5633b0dSQu Wenruo key.offset == start) 1102d5633b0dSQu Wenruo goto iterate; 1103d5633b0dSQu Wenruo 1104d5633b0dSQu Wenruo /* We didn't find a perfect match, needs to go one slot back */ 1105d5633b0dSQu Wenruo if (path.slots[0] > 0) { 1106d5633b0dSQu Wenruo btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); 1107d5633b0dSQu Wenruo if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY) 1108d5633b0dSQu Wenruo path.slots[0]--; 1109d5633b0dSQu Wenruo } 1110d5633b0dSQu Wenruo 1111d5633b0dSQu Wenruo iterate: 1112d5633b0dSQu Wenruo /* Iterate through the path to find a file extent covering @start */ 1113d5633b0dSQu Wenruo while (true) { 1114d5633b0dSQu Wenruo u64 extent_end; 1115d5633b0dSQu Wenruo 1116d5633b0dSQu Wenruo if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) 1117d5633b0dSQu Wenruo goto next; 1118d5633b0dSQu Wenruo 1119d5633b0dSQu Wenruo btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); 1120d5633b0dSQu Wenruo 1121d5633b0dSQu Wenruo /* 1122d5633b0dSQu Wenruo * We may go one slot back to INODE_REF/XATTR item, then 1123d5633b0dSQu Wenruo * need to go forward until we reach an EXTENT_DATA. 1124d5633b0dSQu Wenruo * But we should still has the correct ino as key.objectid. 1125d5633b0dSQu Wenruo */ 1126d5633b0dSQu Wenruo if (WARN_ON(key.objectid < ino) || key.type < BTRFS_EXTENT_DATA_KEY) 1127d5633b0dSQu Wenruo goto next; 1128d5633b0dSQu Wenruo 1129d5633b0dSQu Wenruo /* It's beyond our target range, definitely not extent found */ 1130d5633b0dSQu Wenruo if (key.objectid > ino || key.type > BTRFS_EXTENT_DATA_KEY) 1131d5633b0dSQu Wenruo goto not_found; 1132d5633b0dSQu Wenruo 1133d5633b0dSQu Wenruo /* 1134d5633b0dSQu Wenruo * | |<- File extent ->| 1135d5633b0dSQu Wenruo * \- start 1136d5633b0dSQu Wenruo * 1137d5633b0dSQu Wenruo * This means there is a hole between start and key.offset. 1138d5633b0dSQu Wenruo */ 1139d5633b0dSQu Wenruo if (key.offset > start) { 1140d5633b0dSQu Wenruo em->start = start; 1141d5633b0dSQu Wenruo em->orig_start = start; 1142d5633b0dSQu Wenruo em->block_start = EXTENT_MAP_HOLE; 1143d5633b0dSQu Wenruo em->len = key.offset - start; 1144d5633b0dSQu Wenruo break; 1145d5633b0dSQu Wenruo } 1146d5633b0dSQu Wenruo 1147d5633b0dSQu Wenruo fi = btrfs_item_ptr(path.nodes[0], path.slots[0], 1148d5633b0dSQu Wenruo struct btrfs_file_extent_item); 1149d5633b0dSQu Wenruo extent_end = btrfs_file_extent_end(&path); 1150d5633b0dSQu Wenruo 1151d5633b0dSQu Wenruo /* 1152d5633b0dSQu Wenruo * |<- file extent ->| | 1153d5633b0dSQu Wenruo * \- start 1154d5633b0dSQu Wenruo * 1155d5633b0dSQu Wenruo * We haven't reached start, search next slot. 1156d5633b0dSQu Wenruo */ 1157d5633b0dSQu Wenruo if (extent_end <= start) 1158d5633b0dSQu Wenruo goto next; 1159d5633b0dSQu Wenruo 1160d5633b0dSQu Wenruo /* Now this extent covers @start, convert it to em */ 1161d5633b0dSQu Wenruo btrfs_extent_item_to_extent_map(inode, &path, fi, false, em); 1162d5633b0dSQu Wenruo break; 1163d5633b0dSQu Wenruo next: 1164d5633b0dSQu Wenruo ret = btrfs_next_item(root, &path); 1165d5633b0dSQu Wenruo if (ret < 0) 1166d5633b0dSQu Wenruo goto err; 1167d5633b0dSQu Wenruo if (ret > 0) 1168d5633b0dSQu Wenruo goto not_found; 1169d5633b0dSQu Wenruo } 1170d5633b0dSQu Wenruo btrfs_release_path(&path); 1171d5633b0dSQu Wenruo return em; 1172d5633b0dSQu Wenruo 1173d5633b0dSQu Wenruo not_found: 1174d5633b0dSQu Wenruo btrfs_release_path(&path); 1175d5633b0dSQu Wenruo free_extent_map(em); 1176d5633b0dSQu Wenruo return NULL; 1177d5633b0dSQu Wenruo 1178d5633b0dSQu Wenruo err: 1179d5633b0dSQu Wenruo btrfs_release_path(&path); 1180d5633b0dSQu Wenruo free_extent_map(em); 1181d5633b0dSQu Wenruo return ERR_PTR(ret); 1182d5633b0dSQu Wenruo } 1183d5633b0dSQu Wenruo 1184e9eec721SQu Wenruo static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, 1185d5633b0dSQu Wenruo u64 newer_than, bool locked) 118617ce6ef8SLiu Bo { 118717ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1188940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 11896c282eb4SLi Zefan struct extent_map *em; 119076068caeSQu Wenruo const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize; 1191940100a4SChris Mason 1192940100a4SChris Mason /* 1193940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1194940100a4SChris Mason * the full extent lock 1195940100a4SChris Mason */ 1196940100a4SChris Mason read_lock(&em_tree->lock); 119776068caeSQu Wenruo em = lookup_extent_mapping(em_tree, start, sectorsize); 1198940100a4SChris Mason read_unlock(&em_tree->lock); 1199940100a4SChris Mason 1200199257a7SQu Wenruo /* 1201199257a7SQu Wenruo * We can get a merged extent, in that case, we need to re-search 1202199257a7SQu Wenruo * tree to get the original em for defrag. 1203199257a7SQu Wenruo * 1204199257a7SQu Wenruo * If @newer_than is 0 or em::generation < newer_than, we can trust 1205199257a7SQu Wenruo * this em, as either we don't care about the generation, or the 1206199257a7SQu Wenruo * merged extent map will be rejected anyway. 1207199257a7SQu Wenruo */ 1208199257a7SQu Wenruo if (em && test_bit(EXTENT_FLAG_MERGED, &em->flags) && 1209199257a7SQu Wenruo newer_than && em->generation >= newer_than) { 1210199257a7SQu Wenruo free_extent_map(em); 1211199257a7SQu Wenruo em = NULL; 1212199257a7SQu Wenruo } 1213199257a7SQu Wenruo 1214940100a4SChris Mason if (!em) { 1215308d9800SFilipe Manana struct extent_state *cached = NULL; 121676068caeSQu Wenruo u64 end = start + sectorsize - 1; 1217308d9800SFilipe Manana 1218940100a4SChris Mason /* get the big lock and read metadata off disk */ 1219e9eec721SQu Wenruo if (!locked) 1220ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 1221d5633b0dSQu Wenruo em = defrag_get_extent(BTRFS_I(inode), start, newer_than); 1222e9eec721SQu Wenruo if (!locked) 1223e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 1224940100a4SChris Mason 12256cf8bfbfSDan Carpenter if (IS_ERR(em)) 12266c282eb4SLi Zefan return NULL; 1227940100a4SChris Mason } 1228940100a4SChris Mason 12296c282eb4SLi Zefan return em; 12306c282eb4SLi Zefan } 12316c282eb4SLi Zefan 1232979b25c3SQu Wenruo static u32 get_extent_max_capacity(const struct extent_map *em) 1233979b25c3SQu Wenruo { 1234979b25c3SQu Wenruo if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) 1235979b25c3SQu Wenruo return BTRFS_MAX_COMPRESSED; 1236979b25c3SQu Wenruo return BTRFS_MAX_EXTENT_SIZE; 1237979b25c3SQu Wenruo } 1238979b25c3SQu Wenruo 1239e9eec721SQu Wenruo static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, 1240e9eec721SQu Wenruo bool locked) 12416c282eb4SLi Zefan { 12426c282eb4SLi Zefan struct extent_map *next; 12437093f152SQu Wenruo bool ret = false; 12446c282eb4SLi Zefan 12456c282eb4SLi Zefan /* this is the last extent */ 12466c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 12476c282eb4SLi Zefan return false; 12486c282eb4SLi Zefan 1249d5633b0dSQu Wenruo /* 1250d5633b0dSQu Wenruo * We want to check if the next extent can be merged with the current 1251d5633b0dSQu Wenruo * one, which can be an extent created in a past generation, so we pass 1252d5633b0dSQu Wenruo * a minimum generation of 0 to defrag_lookup_extent(). 1253d5633b0dSQu Wenruo */ 1254d5633b0dSQu Wenruo next = defrag_lookup_extent(inode, em->start + em->len, 0, locked); 12557093f152SQu Wenruo /* No more em or hole */ 1256e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 12577093f152SQu Wenruo goto out; 12587093f152SQu Wenruo if (test_bit(EXTENT_FLAG_PREALLOC, &next->flags)) 12597093f152SQu Wenruo goto out; 1260979b25c3SQu Wenruo /* 1261979b25c3SQu Wenruo * If the next extent is at its max capacity, defragging current extent 1262979b25c3SQu Wenruo * makes no sense, as the total number of extents won't change. 1263979b25c3SQu Wenruo */ 1264979b25c3SQu Wenruo if (next->len >= get_extent_max_capacity(em)) 1265979b25c3SQu Wenruo goto out; 12667093f152SQu Wenruo ret = true; 12677093f152SQu Wenruo out: 12686c282eb4SLi Zefan free_extent_map(next); 12696c282eb4SLi Zefan return ret; 12706c282eb4SLi Zefan } 12716c282eb4SLi Zefan 12724cb5300bSChris Mason /* 12735767b50cSQu Wenruo * Prepare one page to be defragged. 12745767b50cSQu Wenruo * 12755767b50cSQu Wenruo * This will ensure: 12765767b50cSQu Wenruo * 12775767b50cSQu Wenruo * - Returned page is locked and has been set up properly. 12785767b50cSQu Wenruo * - No ordered extent exists in the page. 12795767b50cSQu Wenruo * - The page is uptodate. 12805767b50cSQu Wenruo * 12815767b50cSQu Wenruo * NOTE: Caller should also wait for page writeback after the cluster is 12825767b50cSQu Wenruo * prepared, here we don't do writeback wait for each page. 12835767b50cSQu Wenruo */ 12845767b50cSQu Wenruo static struct page *defrag_prepare_one_page(struct btrfs_inode *inode, 12855767b50cSQu Wenruo pgoff_t index) 1286940100a4SChris Mason { 12875767b50cSQu Wenruo struct address_space *mapping = inode->vfs_inode.i_mapping; 12885767b50cSQu Wenruo gfp_t mask = btrfs_alloc_write_mask(mapping); 12895767b50cSQu Wenruo u64 page_start = (u64)index << PAGE_SHIFT; 12905767b50cSQu Wenruo u64 page_end = page_start + PAGE_SIZE - 1; 12914cb5300bSChris Mason struct extent_state *cached_state = NULL; 12924cb5300bSChris Mason struct page *page; 12935767b50cSQu Wenruo int ret; 12945767b50cSQu Wenruo 1295600a45e1SMiao Xie again: 12965767b50cSQu Wenruo page = find_or_create_page(mapping, index, mask); 12974cb5300bSChris Mason if (!page) 12985767b50cSQu Wenruo return ERR_PTR(-ENOMEM); 12995767b50cSQu Wenruo 130024bcb454SOmar Sandoval /* 130124bcb454SOmar Sandoval * Since we can defragment files opened read-only, we can encounter 130224bcb454SOmar Sandoval * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We 130324bcb454SOmar Sandoval * can't do I/O using huge pages yet, so return an error for now. 130424bcb454SOmar Sandoval * Filesystem transparent huge pages are typically only used for 130524bcb454SOmar Sandoval * executables that explicitly enable them, so this isn't very 130624bcb454SOmar Sandoval * restrictive. 130724bcb454SOmar Sandoval */ 130824bcb454SOmar Sandoval if (PageCompound(page)) { 130924bcb454SOmar Sandoval unlock_page(page); 131024bcb454SOmar Sandoval put_page(page); 131124bcb454SOmar Sandoval return ERR_PTR(-ETXTBSY); 131224bcb454SOmar Sandoval } 13134cb5300bSChris Mason 131432443de3SQu Wenruo ret = set_page_extent_mapped(page); 131532443de3SQu Wenruo if (ret < 0) { 131632443de3SQu Wenruo unlock_page(page); 131732443de3SQu Wenruo put_page(page); 13185767b50cSQu Wenruo return ERR_PTR(ret); 131932443de3SQu Wenruo } 132032443de3SQu Wenruo 13215767b50cSQu Wenruo /* Wait for any existing ordered extent in the range */ 1322600a45e1SMiao Xie while (1) { 13235767b50cSQu Wenruo struct btrfs_ordered_extent *ordered; 13245767b50cSQu Wenruo 13255767b50cSQu Wenruo lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); 13265767b50cSQu Wenruo ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); 13275767b50cSQu Wenruo unlock_extent_cached(&inode->io_tree, page_start, page_end, 1328e43bbe5eSDavid Sterba &cached_state); 1329600a45e1SMiao Xie if (!ordered) 1330600a45e1SMiao Xie break; 1331600a45e1SMiao Xie 1332600a45e1SMiao Xie unlock_page(page); 1333c0a43603SNikolay Borisov btrfs_start_ordered_extent(ordered, 1); 1334600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1335600a45e1SMiao Xie lock_page(page); 13361f12bd06SLiu Bo /* 13375767b50cSQu Wenruo * We unlocked the page above, so we need check if it was 13385767b50cSQu Wenruo * released or not. 13391f12bd06SLiu Bo */ 13405767b50cSQu Wenruo if (page->mapping != mapping || !PagePrivate(page)) { 13411f12bd06SLiu Bo unlock_page(page); 134209cbfeafSKirill A. Shutemov put_page(page); 13431f12bd06SLiu Bo goto again; 13441f12bd06SLiu Bo } 1345600a45e1SMiao Xie } 1346600a45e1SMiao Xie 13475767b50cSQu Wenruo /* 13485767b50cSQu Wenruo * Now the page range has no ordered extent any more. Read the page to 13495767b50cSQu Wenruo * make it uptodate. 13505767b50cSQu Wenruo */ 13514cb5300bSChris Mason if (!PageUptodate(page)) { 13524cb5300bSChris Mason btrfs_readpage(NULL, page); 13534cb5300bSChris Mason lock_page(page); 13545767b50cSQu Wenruo if (page->mapping != mapping || !PagePrivate(page)) { 1355600a45e1SMiao Xie unlock_page(page); 135609cbfeafSKirill A. Shutemov put_page(page); 1357600a45e1SMiao Xie goto again; 1358600a45e1SMiao Xie } 13595767b50cSQu Wenruo if (!PageUptodate(page)) { 13605767b50cSQu Wenruo unlock_page(page); 13615767b50cSQu Wenruo put_page(page); 13625767b50cSQu Wenruo return ERR_PTR(-EIO); 13634cb5300bSChris Mason } 13645767b50cSQu Wenruo } 13655767b50cSQu Wenruo return page; 13665767b50cSQu Wenruo } 13674cb5300bSChris Mason 1368eb793cf8SQu Wenruo struct defrag_target_range { 1369eb793cf8SQu Wenruo struct list_head list; 1370eb793cf8SQu Wenruo u64 start; 1371eb793cf8SQu Wenruo u64 len; 1372eb793cf8SQu Wenruo }; 13734cb5300bSChris Mason 13744cb5300bSChris Mason /* 1375eb793cf8SQu Wenruo * Collect all valid target extents. 1376eb793cf8SQu Wenruo * 1377eb793cf8SQu Wenruo * @start: file offset to lookup 1378eb793cf8SQu Wenruo * @len: length to lookup 1379eb793cf8SQu Wenruo * @extent_thresh: file extent size threshold, any extent size >= this value 1380eb793cf8SQu Wenruo * will be ignored 1381eb793cf8SQu Wenruo * @newer_than: only defrag extents newer than this value 1382eb793cf8SQu Wenruo * @do_compress: whether the defrag is doing compression 1383eb793cf8SQu Wenruo * if true, @extent_thresh will be ignored and all regular 1384eb793cf8SQu Wenruo * file extents meeting @newer_than will be targets. 1385e9eec721SQu Wenruo * @locked: if the range has already held extent lock 1386eb793cf8SQu Wenruo * @target_list: list of targets file extents 13874cb5300bSChris Mason */ 1388eb793cf8SQu Wenruo static int defrag_collect_targets(struct btrfs_inode *inode, 1389eb793cf8SQu Wenruo u64 start, u64 len, u32 extent_thresh, 1390eb793cf8SQu Wenruo u64 newer_than, bool do_compress, 1391966d879bSQu Wenruo bool locked, struct list_head *target_list, 1392966d879bSQu Wenruo u64 *last_scanned_ret) 1393eb793cf8SQu Wenruo { 1394966d879bSQu Wenruo bool last_is_target = false; 1395eb793cf8SQu Wenruo u64 cur = start; 1396eb793cf8SQu Wenruo int ret = 0; 1397eb793cf8SQu Wenruo 1398eb793cf8SQu Wenruo while (cur < start + len) { 1399eb793cf8SQu Wenruo struct extent_map *em; 1400eb793cf8SQu Wenruo struct defrag_target_range *new; 1401eb793cf8SQu Wenruo bool next_mergeable = true; 1402eb793cf8SQu Wenruo u64 range_len; 1403eb793cf8SQu Wenruo 1404966d879bSQu Wenruo last_is_target = false; 1405d5633b0dSQu Wenruo em = defrag_lookup_extent(&inode->vfs_inode, cur, 1406d5633b0dSQu Wenruo newer_than, locked); 1407eb793cf8SQu Wenruo if (!em) 1408eb793cf8SQu Wenruo break; 1409eb793cf8SQu Wenruo 1410eb793cf8SQu Wenruo /* Skip hole/inline/preallocated extents */ 1411eb793cf8SQu Wenruo if (em->block_start >= EXTENT_MAP_LAST_BYTE || 1412eb793cf8SQu Wenruo test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) 1413eb793cf8SQu Wenruo goto next; 1414eb793cf8SQu Wenruo 1415eb793cf8SQu Wenruo /* Skip older extent */ 1416eb793cf8SQu Wenruo if (em->generation < newer_than) 1417eb793cf8SQu Wenruo goto next; 1418eb793cf8SQu Wenruo 14190d1ffa22SQu Wenruo /* This em is under writeback, no need to defrag */ 14200d1ffa22SQu Wenruo if (em->generation == (u64)-1) 14210d1ffa22SQu Wenruo goto next; 14220d1ffa22SQu Wenruo 1423eb793cf8SQu Wenruo /* 14240cb5950fSFilipe Manana * Our start offset might be in the middle of an existing extent 14250cb5950fSFilipe Manana * map, so take that into account. 14260cb5950fSFilipe Manana */ 14270cb5950fSFilipe Manana range_len = em->len - (cur - em->start); 14280cb5950fSFilipe Manana /* 14290cb5950fSFilipe Manana * If this range of the extent map is already flagged for delalloc, 14300cb5950fSFilipe Manana * skip it, because: 14310cb5950fSFilipe Manana * 14320cb5950fSFilipe Manana * 1) We could deadlock later, when trying to reserve space for 14330cb5950fSFilipe Manana * delalloc, because in case we can't immediately reserve space 14340cb5950fSFilipe Manana * the flusher can start delalloc and wait for the respective 14350cb5950fSFilipe Manana * ordered extents to complete. The deadlock would happen 14360cb5950fSFilipe Manana * because we do the space reservation while holding the range 14370cb5950fSFilipe Manana * locked, and starting writeback, or finishing an ordered 14380cb5950fSFilipe Manana * extent, requires locking the range; 14390cb5950fSFilipe Manana * 14400cb5950fSFilipe Manana * 2) If there's delalloc there, it means there's dirty pages for 14410cb5950fSFilipe Manana * which writeback has not started yet (we clean the delalloc 14420cb5950fSFilipe Manana * flag when starting writeback and after creating an ordered 14430cb5950fSFilipe Manana * extent). If we mark pages in an adjacent range for defrag, 14440cb5950fSFilipe Manana * then we will have a larger contiguous range for delalloc, 14450cb5950fSFilipe Manana * very likely resulting in a larger extent after writeback is 14460cb5950fSFilipe Manana * triggered (except in a case of free space fragmentation). 14470cb5950fSFilipe Manana */ 14480cb5950fSFilipe Manana if (test_range_bit(&inode->io_tree, cur, cur + range_len - 1, 14490cb5950fSFilipe Manana EXTENT_DELALLOC, 0, NULL)) 14500cb5950fSFilipe Manana goto next; 14510cb5950fSFilipe Manana 14520cb5950fSFilipe Manana /* 1453eb793cf8SQu Wenruo * For do_compress case, we want to compress all valid file 1454eb793cf8SQu Wenruo * extents, thus no @extent_thresh or mergeable check. 1455eb793cf8SQu Wenruo */ 1456eb793cf8SQu Wenruo if (do_compress) 1457eb793cf8SQu Wenruo goto add; 1458eb793cf8SQu Wenruo 1459eb793cf8SQu Wenruo /* Skip too large extent */ 14600cb5950fSFilipe Manana if (range_len >= extent_thresh) 1461eb793cf8SQu Wenruo goto next; 1462eb793cf8SQu Wenruo 1463979b25c3SQu Wenruo /* 1464979b25c3SQu Wenruo * Skip extents already at its max capacity, this is mostly for 1465979b25c3SQu Wenruo * compressed extents, which max cap is only 128K. 1466979b25c3SQu Wenruo */ 1467979b25c3SQu Wenruo if (em->len >= get_extent_max_capacity(em)) 1468979b25c3SQu Wenruo goto next; 1469979b25c3SQu Wenruo 1470e9eec721SQu Wenruo next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em, 1471e9eec721SQu Wenruo locked); 1472eb793cf8SQu Wenruo if (!next_mergeable) { 1473eb793cf8SQu Wenruo struct defrag_target_range *last; 1474eb793cf8SQu Wenruo 1475eb793cf8SQu Wenruo /* Empty target list, no way to merge with last entry */ 1476eb793cf8SQu Wenruo if (list_empty(target_list)) 1477eb793cf8SQu Wenruo goto next; 1478eb793cf8SQu Wenruo last = list_entry(target_list->prev, 1479eb793cf8SQu Wenruo struct defrag_target_range, list); 1480eb793cf8SQu Wenruo /* Not mergeable with last entry */ 1481eb793cf8SQu Wenruo if (last->start + last->len != cur) 1482eb793cf8SQu Wenruo goto next; 1483eb793cf8SQu Wenruo 1484eb793cf8SQu Wenruo /* Mergeable, fall through to add it to @target_list. */ 1485eb793cf8SQu Wenruo } 1486eb793cf8SQu Wenruo 1487eb793cf8SQu Wenruo add: 1488966d879bSQu Wenruo last_is_target = true; 1489eb793cf8SQu Wenruo range_len = min(extent_map_end(em), start + len) - cur; 1490eb793cf8SQu Wenruo /* 1491eb793cf8SQu Wenruo * This one is a good target, check if it can be merged into 1492eb793cf8SQu Wenruo * last range of the target list. 1493eb793cf8SQu Wenruo */ 1494eb793cf8SQu Wenruo if (!list_empty(target_list)) { 1495eb793cf8SQu Wenruo struct defrag_target_range *last; 1496eb793cf8SQu Wenruo 1497eb793cf8SQu Wenruo last = list_entry(target_list->prev, 1498eb793cf8SQu Wenruo struct defrag_target_range, list); 1499eb793cf8SQu Wenruo ASSERT(last->start + last->len <= cur); 1500eb793cf8SQu Wenruo if (last->start + last->len == cur) { 1501eb793cf8SQu Wenruo /* Mergeable, enlarge the last entry */ 1502eb793cf8SQu Wenruo last->len += range_len; 1503eb793cf8SQu Wenruo goto next; 1504eb793cf8SQu Wenruo } 1505eb793cf8SQu Wenruo /* Fall through to allocate a new entry */ 1506eb793cf8SQu Wenruo } 1507eb793cf8SQu Wenruo 1508eb793cf8SQu Wenruo /* Allocate new defrag_target_range */ 1509eb793cf8SQu Wenruo new = kmalloc(sizeof(*new), GFP_NOFS); 1510eb793cf8SQu Wenruo if (!new) { 1511eb793cf8SQu Wenruo free_extent_map(em); 1512eb793cf8SQu Wenruo ret = -ENOMEM; 1513eb793cf8SQu Wenruo break; 1514eb793cf8SQu Wenruo } 1515eb793cf8SQu Wenruo new->start = cur; 1516eb793cf8SQu Wenruo new->len = range_len; 1517eb793cf8SQu Wenruo list_add_tail(&new->list, target_list); 1518eb793cf8SQu Wenruo 1519eb793cf8SQu Wenruo next: 1520eb793cf8SQu Wenruo cur = extent_map_end(em); 1521eb793cf8SQu Wenruo free_extent_map(em); 1522eb793cf8SQu Wenruo } 1523eb793cf8SQu Wenruo if (ret < 0) { 1524eb793cf8SQu Wenruo struct defrag_target_range *entry; 1525eb793cf8SQu Wenruo struct defrag_target_range *tmp; 1526eb793cf8SQu Wenruo 1527eb793cf8SQu Wenruo list_for_each_entry_safe(entry, tmp, target_list, list) { 1528eb793cf8SQu Wenruo list_del_init(&entry->list); 1529eb793cf8SQu Wenruo kfree(entry); 1530eb793cf8SQu Wenruo } 1531eb793cf8SQu Wenruo } 1532966d879bSQu Wenruo if (!ret && last_scanned_ret) { 1533966d879bSQu Wenruo /* 1534966d879bSQu Wenruo * If the last extent is not a target, the caller can skip to 1535966d879bSQu Wenruo * the end of that extent. 1536966d879bSQu Wenruo * Otherwise, we can only go the end of the specified range. 1537966d879bSQu Wenruo */ 1538966d879bSQu Wenruo if (!last_is_target) 1539966d879bSQu Wenruo *last_scanned_ret = max(cur, *last_scanned_ret); 1540966d879bSQu Wenruo else 1541966d879bSQu Wenruo *last_scanned_ret = max(start + len, *last_scanned_ret); 1542966d879bSQu Wenruo } 1543eb793cf8SQu Wenruo return ret; 1544eb793cf8SQu Wenruo } 1545eb793cf8SQu Wenruo 154622b398eeSQu Wenruo #define CLUSTER_SIZE (SZ_256K) 1547a55e65b8SDavid Sterba static_assert(IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE)); 154822b398eeSQu Wenruo 154922b398eeSQu Wenruo /* 155022b398eeSQu Wenruo * Defrag one contiguous target range. 155122b398eeSQu Wenruo * 155222b398eeSQu Wenruo * @inode: target inode 155322b398eeSQu Wenruo * @target: target range to defrag 155422b398eeSQu Wenruo * @pages: locked pages covering the defrag range 155522b398eeSQu Wenruo * @nr_pages: number of locked pages 155622b398eeSQu Wenruo * 155722b398eeSQu Wenruo * Caller should ensure: 155822b398eeSQu Wenruo * 155922b398eeSQu Wenruo * - Pages are prepared 156022b398eeSQu Wenruo * Pages should be locked, no ordered extent in the pages range, 156122b398eeSQu Wenruo * no writeback. 156222b398eeSQu Wenruo * 156322b398eeSQu Wenruo * - Extent bits are locked 156422b398eeSQu Wenruo */ 156522b398eeSQu Wenruo static int defrag_one_locked_target(struct btrfs_inode *inode, 156622b398eeSQu Wenruo struct defrag_target_range *target, 156722b398eeSQu Wenruo struct page **pages, int nr_pages, 156822b398eeSQu Wenruo struct extent_state **cached_state) 156922b398eeSQu Wenruo { 157022b398eeSQu Wenruo struct btrfs_fs_info *fs_info = inode->root->fs_info; 157122b398eeSQu Wenruo struct extent_changeset *data_reserved = NULL; 157222b398eeSQu Wenruo const u64 start = target->start; 157322b398eeSQu Wenruo const u64 len = target->len; 157422b398eeSQu Wenruo unsigned long last_index = (start + len - 1) >> PAGE_SHIFT; 157522b398eeSQu Wenruo unsigned long start_index = start >> PAGE_SHIFT; 157622b398eeSQu Wenruo unsigned long first_index = page_index(pages[0]); 157722b398eeSQu Wenruo int ret = 0; 157822b398eeSQu Wenruo int i; 157922b398eeSQu Wenruo 158022b398eeSQu Wenruo ASSERT(last_index - first_index + 1 <= nr_pages); 158122b398eeSQu Wenruo 158222b398eeSQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len); 158322b398eeSQu Wenruo if (ret < 0) 158422b398eeSQu Wenruo return ret; 158522b398eeSQu Wenruo clear_extent_bit(&inode->io_tree, start, start + len - 1, 158622b398eeSQu Wenruo EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | 158722b398eeSQu Wenruo EXTENT_DEFRAG, 0, 0, cached_state); 158822b398eeSQu Wenruo set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state); 158922b398eeSQu Wenruo 159022b398eeSQu Wenruo /* Update the page status */ 159122b398eeSQu Wenruo for (i = start_index - first_index; i <= last_index - first_index; i++) { 159222b398eeSQu Wenruo ClearPageChecked(pages[i]); 159322b398eeSQu Wenruo btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len); 159422b398eeSQu Wenruo } 159522b398eeSQu Wenruo btrfs_delalloc_release_extents(inode, len); 159622b398eeSQu Wenruo extent_changeset_free(data_reserved); 159722b398eeSQu Wenruo 159822b398eeSQu Wenruo return ret; 159922b398eeSQu Wenruo } 160022b398eeSQu Wenruo 1601e9eec721SQu Wenruo static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, 1602966d879bSQu Wenruo u32 extent_thresh, u64 newer_than, bool do_compress, 1603966d879bSQu Wenruo u64 *last_scanned_ret) 1604e9eec721SQu Wenruo { 1605e9eec721SQu Wenruo struct extent_state *cached_state = NULL; 1606e9eec721SQu Wenruo struct defrag_target_range *entry; 1607e9eec721SQu Wenruo struct defrag_target_range *tmp; 1608e9eec721SQu Wenruo LIST_HEAD(target_list); 1609e9eec721SQu Wenruo struct page **pages; 1610e9eec721SQu Wenruo const u32 sectorsize = inode->root->fs_info->sectorsize; 1611e9eec721SQu Wenruo u64 last_index = (start + len - 1) >> PAGE_SHIFT; 1612e9eec721SQu Wenruo u64 start_index = start >> PAGE_SHIFT; 1613e9eec721SQu Wenruo unsigned int nr_pages = last_index - start_index + 1; 1614e9eec721SQu Wenruo int ret = 0; 1615e9eec721SQu Wenruo int i; 1616e9eec721SQu Wenruo 1617e9eec721SQu Wenruo ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE); 1618e9eec721SQu Wenruo ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize)); 1619e9eec721SQu Wenruo 1620e9eec721SQu Wenruo pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); 1621e9eec721SQu Wenruo if (!pages) 1622e9eec721SQu Wenruo return -ENOMEM; 1623e9eec721SQu Wenruo 1624e9eec721SQu Wenruo /* Prepare all pages */ 1625e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) { 1626e9eec721SQu Wenruo pages[i] = defrag_prepare_one_page(inode, start_index + i); 1627e9eec721SQu Wenruo if (IS_ERR(pages[i])) { 1628e9eec721SQu Wenruo ret = PTR_ERR(pages[i]); 1629e9eec721SQu Wenruo pages[i] = NULL; 1630e9eec721SQu Wenruo goto free_pages; 1631e9eec721SQu Wenruo } 1632e9eec721SQu Wenruo } 1633e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) 16344cb5300bSChris Mason wait_on_page_writeback(pages[i]); 16354cb5300bSChris Mason 1636e9eec721SQu Wenruo /* Lock the pages range */ 1637e9eec721SQu Wenruo lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT, 1638e9eec721SQu Wenruo (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, 1639e9eec721SQu Wenruo &cached_state); 1640e9eec721SQu Wenruo /* 1641e9eec721SQu Wenruo * Now we have a consistent view about the extent map, re-check 1642e9eec721SQu Wenruo * which range really needs to be defragged. 1643e9eec721SQu Wenruo * 1644e9eec721SQu Wenruo * And this time we have extent locked already, pass @locked = true 1645e9eec721SQu Wenruo * so that we won't relock the extent range and cause deadlock. 1646e9eec721SQu Wenruo */ 1647e9eec721SQu Wenruo ret = defrag_collect_targets(inode, start, len, extent_thresh, 1648e9eec721SQu Wenruo newer_than, do_compress, true, 1649966d879bSQu Wenruo &target_list, last_scanned_ret); 1650e9eec721SQu Wenruo if (ret < 0) 1651e9eec721SQu Wenruo goto unlock_extent; 16524cb5300bSChris Mason 1653e9eec721SQu Wenruo list_for_each_entry(entry, &target_list, list) { 1654e9eec721SQu Wenruo ret = defrag_one_locked_target(inode, entry, pages, nr_pages, 1655e9eec721SQu Wenruo &cached_state); 1656e9eec721SQu Wenruo if (ret < 0) 1657e9eec721SQu Wenruo break; 1658e9eec721SQu Wenruo } 1659e9eec721SQu Wenruo 1660e9eec721SQu Wenruo list_for_each_entry_safe(entry, tmp, &target_list, list) { 1661e9eec721SQu Wenruo list_del_init(&entry->list); 1662e9eec721SQu Wenruo kfree(entry); 1663e9eec721SQu Wenruo } 1664e9eec721SQu Wenruo unlock_extent: 1665e9eec721SQu Wenruo unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT, 1666e9eec721SQu Wenruo (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, 1667e9eec721SQu Wenruo &cached_state); 1668e9eec721SQu Wenruo free_pages: 1669e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) { 1670e9eec721SQu Wenruo if (pages[i]) { 1671e9eec721SQu Wenruo unlock_page(pages[i]); 1672e9eec721SQu Wenruo put_page(pages[i]); 1673e9eec721SQu Wenruo } 1674e9eec721SQu Wenruo } 1675e9eec721SQu Wenruo kfree(pages); 1676e9eec721SQu Wenruo return ret; 1677e9eec721SQu Wenruo } 1678e9eec721SQu Wenruo 1679b18c3ab2SQu Wenruo static int defrag_one_cluster(struct btrfs_inode *inode, 1680b18c3ab2SQu Wenruo struct file_ra_state *ra, 1681b18c3ab2SQu Wenruo u64 start, u32 len, u32 extent_thresh, 1682b18c3ab2SQu Wenruo u64 newer_than, bool do_compress, 1683b18c3ab2SQu Wenruo unsigned long *sectors_defragged, 1684966d879bSQu Wenruo unsigned long max_sectors, 1685966d879bSQu Wenruo u64 *last_scanned_ret) 1686b18c3ab2SQu Wenruo { 1687b18c3ab2SQu Wenruo const u32 sectorsize = inode->root->fs_info->sectorsize; 1688b18c3ab2SQu Wenruo struct defrag_target_range *entry; 1689b18c3ab2SQu Wenruo struct defrag_target_range *tmp; 1690b18c3ab2SQu Wenruo LIST_HEAD(target_list); 1691b18c3ab2SQu Wenruo int ret; 1692b18c3ab2SQu Wenruo 1693b18c3ab2SQu Wenruo ret = defrag_collect_targets(inode, start, len, extent_thresh, 1694b18c3ab2SQu Wenruo newer_than, do_compress, false, 1695966d879bSQu Wenruo &target_list, NULL); 1696b18c3ab2SQu Wenruo if (ret < 0) 1697b18c3ab2SQu Wenruo goto out; 1698b18c3ab2SQu Wenruo 1699b18c3ab2SQu Wenruo list_for_each_entry(entry, &target_list, list) { 1700b18c3ab2SQu Wenruo u32 range_len = entry->len; 1701b18c3ab2SQu Wenruo 1702484167daSQu Wenruo /* Reached or beyond the limit */ 1703c080b414SQu Wenruo if (max_sectors && *sectors_defragged >= max_sectors) { 1704c080b414SQu Wenruo ret = 1; 1705b18c3ab2SQu Wenruo break; 1706c080b414SQu Wenruo } 1707b18c3ab2SQu Wenruo 1708b18c3ab2SQu Wenruo if (max_sectors) 1709b18c3ab2SQu Wenruo range_len = min_t(u32, range_len, 1710b18c3ab2SQu Wenruo (max_sectors - *sectors_defragged) * sectorsize); 1711b18c3ab2SQu Wenruo 1712966d879bSQu Wenruo /* 1713966d879bSQu Wenruo * If defrag_one_range() has updated last_scanned_ret, 1714966d879bSQu Wenruo * our range may already be invalid (e.g. hole punched). 1715966d879bSQu Wenruo * Skip if our range is before last_scanned_ret, as there is 1716966d879bSQu Wenruo * no need to defrag the range anymore. 1717966d879bSQu Wenruo */ 1718966d879bSQu Wenruo if (entry->start + range_len <= *last_scanned_ret) 1719966d879bSQu Wenruo continue; 1720966d879bSQu Wenruo 1721b18c3ab2SQu Wenruo if (ra) 1722b18c3ab2SQu Wenruo page_cache_sync_readahead(inode->vfs_inode.i_mapping, 1723b18c3ab2SQu Wenruo ra, NULL, entry->start >> PAGE_SHIFT, 1724b18c3ab2SQu Wenruo ((entry->start + range_len - 1) >> PAGE_SHIFT) - 1725b18c3ab2SQu Wenruo (entry->start >> PAGE_SHIFT) + 1); 1726b18c3ab2SQu Wenruo /* 1727b18c3ab2SQu Wenruo * Here we may not defrag any range if holes are punched before 1728b18c3ab2SQu Wenruo * we locked the pages. 1729b18c3ab2SQu Wenruo * But that's fine, it only affects the @sectors_defragged 1730b18c3ab2SQu Wenruo * accounting. 1731b18c3ab2SQu Wenruo */ 1732b18c3ab2SQu Wenruo ret = defrag_one_range(inode, entry->start, range_len, 1733966d879bSQu Wenruo extent_thresh, newer_than, do_compress, 1734966d879bSQu Wenruo last_scanned_ret); 1735b18c3ab2SQu Wenruo if (ret < 0) 1736b18c3ab2SQu Wenruo break; 1737484167daSQu Wenruo *sectors_defragged += range_len >> 1738484167daSQu Wenruo inode->root->fs_info->sectorsize_bits; 1739b18c3ab2SQu Wenruo } 1740b18c3ab2SQu Wenruo out: 1741b18c3ab2SQu Wenruo list_for_each_entry_safe(entry, tmp, &target_list, list) { 1742b18c3ab2SQu Wenruo list_del_init(&entry->list); 1743b18c3ab2SQu Wenruo kfree(entry); 1744b18c3ab2SQu Wenruo } 1745966d879bSQu Wenruo if (ret >= 0) 1746966d879bSQu Wenruo *last_scanned_ret = max(*last_scanned_ret, start + len); 1747b18c3ab2SQu Wenruo return ret; 1748b18c3ab2SQu Wenruo } 17497f458a38SFilipe Manana 17507f458a38SFilipe Manana /* 17511ccc2e8aSQu Wenruo * Entry point to file defragmentation. 17521ccc2e8aSQu Wenruo * 17531ccc2e8aSQu Wenruo * @inode: inode to be defragged 17541ccc2e8aSQu Wenruo * @ra: readahead state (can be NUL) 17551ccc2e8aSQu Wenruo * @range: defrag options including range and flags 17561ccc2e8aSQu Wenruo * @newer_than: minimum transid to defrag 17571ccc2e8aSQu Wenruo * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode 17581ccc2e8aSQu Wenruo * will be defragged. 1759484167daSQu Wenruo * 1760484167daSQu Wenruo * Return <0 for error. 1761c080b414SQu Wenruo * Return >=0 for the number of sectors defragged, and range->start will be updated 1762c080b414SQu Wenruo * to indicate the file offset where next defrag should be started at. 1763c080b414SQu Wenruo * (Mostly for autodefrag, which sets @max_to_defrag thus we may exit early without 1764c080b414SQu Wenruo * defragging all the range). 17657f458a38SFilipe Manana */ 17661ccc2e8aSQu Wenruo int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, 17674cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 17684cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 17694cb5300bSChris Mason { 17700b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 17717b508037SQu Wenruo unsigned long sectors_defragged = 0; 1772151a31b2SLi Zefan u64 isize = i_size_read(inode); 17737b508037SQu Wenruo u64 cur; 17747b508037SQu Wenruo u64 last_byte; 17751e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 17761ccc2e8aSQu Wenruo bool ra_allocated = false; 17777b508037SQu Wenruo int compress_type = BTRFS_COMPRESS_ZLIB; 17787b508037SQu Wenruo int ret = 0; 17797b508037SQu Wenruo u32 extent_thresh = range->extent_thresh; 178027cdfde1SFilipe Manana pgoff_t start_index; 17814cb5300bSChris Mason 17820abd5b17SLiu Bo if (isize == 0) 17830abd5b17SLiu Bo return 0; 17840abd5b17SLiu Bo 17850abd5b17SLiu Bo if (range->start >= isize) 17860abd5b17SLiu Bo return -EINVAL; 17871a419d85SLi Zefan 17881e2ef46dSDavid Sterba if (do_compress) { 1789ce96b7ffSChengguang Xu if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) 17901a419d85SLi Zefan return -EINVAL; 17911a419d85SLi Zefan if (range->compress_type) 17921a419d85SLi Zefan compress_type = range->compress_type; 17931a419d85SLi Zefan } 1794f46b5a66SChristoph Hellwig 17950abd5b17SLiu Bo if (extent_thresh == 0) 1796ee22184bSByongho Lee extent_thresh = SZ_256K; 1797f46b5a66SChristoph Hellwig 17987b508037SQu Wenruo if (range->start + range->len > range->start) { 17997b508037SQu Wenruo /* Got a specific range */ 18006b34cd8eSFilipe Manana last_byte = min(isize, range->start + range->len); 18017b508037SQu Wenruo } else { 18027b508037SQu Wenruo /* Defrag until file end */ 18036b34cd8eSFilipe Manana last_byte = isize; 18047b508037SQu Wenruo } 18057b508037SQu Wenruo 18066b34cd8eSFilipe Manana /* Align the range */ 18076b34cd8eSFilipe Manana cur = round_down(range->start, fs_info->sectorsize); 18086b34cd8eSFilipe Manana last_byte = round_up(last_byte, fs_info->sectorsize) - 1; 18096b34cd8eSFilipe Manana 18104cb5300bSChris Mason /* 18111ccc2e8aSQu Wenruo * If we were not given a ra, allocate a readahead context. As 18120a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 18130a52d108SDavid Sterba * we don't error out. 18144cb5300bSChris Mason */ 18151ccc2e8aSQu Wenruo if (!ra) { 18161ccc2e8aSQu Wenruo ra_allocated = true; 181763e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 18180a52d108SDavid Sterba if (ra) 18194cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 18204cb5300bSChris Mason } 18214cb5300bSChris Mason 182227cdfde1SFilipe Manana /* 182327cdfde1SFilipe Manana * Make writeback start from the beginning of the range, so that the 182427cdfde1SFilipe Manana * defrag range can be written sequentially. 182527cdfde1SFilipe Manana */ 182627cdfde1SFilipe Manana start_index = cur >> PAGE_SHIFT; 182727cdfde1SFilipe Manana if (start_index < inode->i_mapping->writeback_index) 182827cdfde1SFilipe Manana inode->i_mapping->writeback_index = start_index; 182927cdfde1SFilipe Manana 18307b508037SQu Wenruo while (cur < last_byte) { 18313c9d31c7SFilipe Manana const unsigned long prev_sectors_defragged = sectors_defragged; 1832966d879bSQu Wenruo u64 last_scanned = cur; 18337b508037SQu Wenruo u64 cluster_end; 18341e701a32SChris Mason 1835b767c2fcSFilipe Manana if (btrfs_defrag_cancelled(fs_info)) { 1836b767c2fcSFilipe Manana ret = -EAGAIN; 1837b767c2fcSFilipe Manana break; 1838b767c2fcSFilipe Manana } 1839b767c2fcSFilipe Manana 18407b508037SQu Wenruo /* We want the cluster end at page boundary when possible */ 18417b508037SQu Wenruo cluster_end = (((cur >> PAGE_SHIFT) + 18427b508037SQu Wenruo (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1; 18437b508037SQu Wenruo cluster_end = min(cluster_end, last_byte); 18444cb5300bSChris Mason 184564708539SJosef Bacik btrfs_inode_lock(inode, 0); 1846eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 1847eede2bf3SOmar Sandoval ret = -ETXTBSY; 18487b508037SQu Wenruo btrfs_inode_unlock(inode, 0); 18497b508037SQu Wenruo break; 18507b508037SQu Wenruo } 18517b508037SQu Wenruo if (!(inode->i_sb->s_flags & SB_ACTIVE)) { 18527b508037SQu Wenruo btrfs_inode_unlock(inode, 0); 18537b508037SQu Wenruo break; 18547b508037SQu Wenruo } 18551e2ef46dSDavid Sterba if (do_compress) 1856eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 18577b508037SQu Wenruo ret = defrag_one_cluster(BTRFS_I(inode), ra, cur, 18587b508037SQu Wenruo cluster_end + 1 - cur, extent_thresh, 1859966d879bSQu Wenruo newer_than, do_compress, §ors_defragged, 1860966d879bSQu Wenruo max_to_defrag, &last_scanned); 18613c9d31c7SFilipe Manana 18623c9d31c7SFilipe Manana if (sectors_defragged > prev_sectors_defragged) 18633c9d31c7SFilipe Manana balance_dirty_pages_ratelimited(inode->i_mapping); 18643c9d31c7SFilipe Manana 186564708539SJosef Bacik btrfs_inode_unlock(inode, 0); 18667b508037SQu Wenruo if (ret < 0) 18674cb5300bSChris Mason break; 1868966d879bSQu Wenruo cur = max(cluster_end + 1, last_scanned); 1869c080b414SQu Wenruo if (ret > 0) { 1870c080b414SQu Wenruo ret = 0; 1871c080b414SQu Wenruo break; 1872c080b414SQu Wenruo } 1873ea0eba69SQu Wenruo cond_resched(); 18744cb5300bSChris Mason } 1875f46b5a66SChristoph Hellwig 18767b508037SQu Wenruo if (ra_allocated) 18777b508037SQu Wenruo kfree(ra); 1878c080b414SQu Wenruo /* 1879c080b414SQu Wenruo * Update range.start for autodefrag, this will indicate where to start 1880c080b414SQu Wenruo * in next run. 1881c080b414SQu Wenruo */ 1882c080b414SQu Wenruo range->start = cur; 18837b508037SQu Wenruo if (sectors_defragged) { 18847b508037SQu Wenruo /* 18857b508037SQu Wenruo * We have defragged some sectors, for compression case they 18867b508037SQu Wenruo * need to be written back immediately. 18877b508037SQu Wenruo */ 18887b508037SQu Wenruo if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) { 18891e701a32SChris Mason filemap_flush(inode->i_mapping); 1890dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1891dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1892dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1893dec8ef90SFilipe Manana } 18947b508037SQu Wenruo if (range->compress_type == BTRFS_COMPRESS_LZO) 18950b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 18967b508037SQu Wenruo else if (range->compress_type == BTRFS_COMPRESS_ZSTD) 18975c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 18987b508037SQu Wenruo ret = sectors_defragged; 18991a419d85SLi Zefan } 19001e2ef46dSDavid Sterba if (do_compress) { 190164708539SJosef Bacik btrfs_inode_lock(inode, 0); 1902eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 190364708539SJosef Bacik btrfs_inode_unlock(inode, 0); 1904633085c7SFilipe David Borba Manana } 1905940100a4SChris Mason return ret; 1906f46b5a66SChristoph Hellwig } 1907f46b5a66SChristoph Hellwig 190817aaa434SDavid Sterba /* 190917aaa434SDavid Sterba * Try to start exclusive operation @type or cancel it if it's running. 191017aaa434SDavid Sterba * 191117aaa434SDavid Sterba * Return: 191217aaa434SDavid Sterba * 0 - normal mode, newly claimed op started 191317aaa434SDavid Sterba * >0 - normal mode, something else is running, 191417aaa434SDavid Sterba * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space 191517aaa434SDavid Sterba * ECANCELED - cancel mode, successful cancel 191617aaa434SDavid Sterba * ENOTCONN - cancel mode, operation not running anymore 191717aaa434SDavid Sterba */ 191817aaa434SDavid Sterba static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info, 191917aaa434SDavid Sterba enum btrfs_exclusive_operation type, bool cancel) 192017aaa434SDavid Sterba { 192117aaa434SDavid Sterba if (!cancel) { 192217aaa434SDavid Sterba /* Start normal op */ 192317aaa434SDavid Sterba if (!btrfs_exclop_start(fs_info, type)) 192417aaa434SDavid Sterba return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 192517aaa434SDavid Sterba /* Exclusive operation is now claimed */ 192617aaa434SDavid Sterba return 0; 192717aaa434SDavid Sterba } 192817aaa434SDavid Sterba 192917aaa434SDavid Sterba /* Cancel running op */ 193017aaa434SDavid Sterba if (btrfs_exclop_start_try_lock(fs_info, type)) { 193117aaa434SDavid Sterba /* 193217aaa434SDavid Sterba * This blocks any exclop finish from setting it to NONE, so we 193317aaa434SDavid Sterba * request cancellation. Either it runs and we will wait for it, 193417aaa434SDavid Sterba * or it has finished and no waiting will happen. 193517aaa434SDavid Sterba */ 193617aaa434SDavid Sterba atomic_inc(&fs_info->reloc_cancel_req); 193717aaa434SDavid Sterba btrfs_exclop_start_unlock(fs_info); 193817aaa434SDavid Sterba 193917aaa434SDavid Sterba if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) 194017aaa434SDavid Sterba wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING, 194117aaa434SDavid Sterba TASK_INTERRUPTIBLE); 194217aaa434SDavid Sterba 194317aaa434SDavid Sterba return -ECANCELED; 194417aaa434SDavid Sterba } 194517aaa434SDavid Sterba 194617aaa434SDavid Sterba /* Something else is running or none */ 194717aaa434SDavid Sterba return -ENOTCONN; 194817aaa434SDavid Sterba } 194917aaa434SDavid Sterba 1950198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 195176dda93cSYan, Zheng void __user *arg) 1952f46b5a66SChristoph Hellwig { 1953562d7b15SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 19540b246afaSJeff Mahoney struct inode *inode = file_inode(file); 19550b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1956f46b5a66SChristoph Hellwig u64 new_size; 1957f46b5a66SChristoph Hellwig u64 old_size; 1958f46b5a66SChristoph Hellwig u64 devid = 1; 19590b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1960f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1961f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1962f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1963f46b5a66SChristoph Hellwig char *sizestr; 19649a40f122SGui Hecheng char *retptr; 1965f46b5a66SChristoph Hellwig char *devstr = NULL; 1966f46b5a66SChristoph Hellwig int ret = 0; 1967f46b5a66SChristoph Hellwig int mod = 0; 1968bb059a37SDavid Sterba bool cancel; 1969f46b5a66SChristoph Hellwig 1970e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1971e441d54dSChris Mason return -EPERM; 1972e441d54dSChris Mason 1973198605a8SMiao Xie ret = mnt_want_write_file(file); 1974198605a8SMiao Xie if (ret) 1975198605a8SMiao Xie return ret; 1976198605a8SMiao Xie 1977bb059a37SDavid Sterba /* 1978bb059a37SDavid Sterba * Read the arguments before checking exclusivity to be able to 1979bb059a37SDavid Sterba * distinguish regular resize and cancel 1980bb059a37SDavid Sterba */ 1981dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1982c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1983c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1984bb059a37SDavid Sterba goto out_drop; 1985c9e9f97bSIlya Dryomov } 19865516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1987f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1988bb059a37SDavid Sterba cancel = (strcmp("cancel", sizestr) == 0); 1989bb059a37SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel); 1990bb059a37SDavid Sterba if (ret) 1991bb059a37SDavid Sterba goto out_free; 1992bb059a37SDavid Sterba /* Exclusive operation is now claimed */ 1993bb059a37SDavid Sterba 1994f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1995f46b5a66SChristoph Hellwig if (devstr) { 1996f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1997f46b5a66SChristoph Hellwig *devstr = '\0'; 1998f46b5a66SChristoph Hellwig devstr = vol_args->name; 199958dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 200058dfae63SZhangZhen if (ret) 2001bb059a37SDavid Sterba goto out_finish; 2002dfd79829SMiao Xie if (!devid) { 2003dfd79829SMiao Xie ret = -EINVAL; 2004bb059a37SDavid Sterba goto out_finish; 2005dfd79829SMiao Xie } 20060b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 2007f46b5a66SChristoph Hellwig } 2008dba60f3fSMiao Xie 2009562d7b15SJosef Bacik args.devid = devid; 2010562d7b15SJosef Bacik device = btrfs_find_device(fs_info->fs_devices, &args); 2011f46b5a66SChristoph Hellwig if (!device) { 20120b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 2013c1c9ff7cSGeert Uytterhoeven devid); 2014dfd79829SMiao Xie ret = -ENODEV; 2015bb059a37SDavid Sterba goto out_finish; 2016f46b5a66SChristoph Hellwig } 2017dba60f3fSMiao Xie 2018ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 20190b246afaSJeff Mahoney btrfs_info(fs_info, 2020efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 2021c1c9ff7cSGeert Uytterhoeven devid); 2022dfd79829SMiao Xie ret = -EPERM; 2023bb059a37SDavid Sterba goto out_finish; 20244e42ae1bSLiu Bo } 20254e42ae1bSLiu Bo 2026f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 2027cda00ebaSChristoph Hellwig new_size = bdev_nr_bytes(device->bdev); 2028f46b5a66SChristoph Hellwig else { 2029f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 2030f46b5a66SChristoph Hellwig mod = -1; 2031f46b5a66SChristoph Hellwig sizestr++; 2032f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 2033f46b5a66SChristoph Hellwig mod = 1; 2034f46b5a66SChristoph Hellwig sizestr++; 2035f46b5a66SChristoph Hellwig } 20369a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 20379a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 2038f46b5a66SChristoph Hellwig ret = -EINVAL; 2039bb059a37SDavid Sterba goto out_finish; 2040f46b5a66SChristoph Hellwig } 2041f46b5a66SChristoph Hellwig } 2042f46b5a66SChristoph Hellwig 2043401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 2044dfd79829SMiao Xie ret = -EPERM; 2045bb059a37SDavid Sterba goto out_finish; 204663a212abSStefan Behrens } 204763a212abSStefan Behrens 20487cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 2049f46b5a66SChristoph Hellwig 2050f46b5a66SChristoph Hellwig if (mod < 0) { 2051f46b5a66SChristoph Hellwig if (new_size > old_size) { 2052f46b5a66SChristoph Hellwig ret = -EINVAL; 2053bb059a37SDavid Sterba goto out_finish; 2054f46b5a66SChristoph Hellwig } 2055f46b5a66SChristoph Hellwig new_size = old_size - new_size; 2056f46b5a66SChristoph Hellwig } else if (mod > 0) { 2057eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 2058902c68a4SGui Hecheng ret = -ERANGE; 2059bb059a37SDavid Sterba goto out_finish; 2060eb8052e0SWenliang Fan } 2061f46b5a66SChristoph Hellwig new_size = old_size + new_size; 2062f46b5a66SChristoph Hellwig } 2063f46b5a66SChristoph Hellwig 2064ee22184bSByongho Lee if (new_size < SZ_256M) { 2065f46b5a66SChristoph Hellwig ret = -EINVAL; 2066bb059a37SDavid Sterba goto out_finish; 2067f46b5a66SChristoph Hellwig } 2068cda00ebaSChristoph Hellwig if (new_size > bdev_nr_bytes(device->bdev)) { 2069f46b5a66SChristoph Hellwig ret = -EFBIG; 2070bb059a37SDavid Sterba goto out_finish; 2071f46b5a66SChristoph Hellwig } 2072f46b5a66SChristoph Hellwig 207347f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 2074f46b5a66SChristoph Hellwig 2075f46b5a66SChristoph Hellwig if (new_size > old_size) { 2076a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 207798d5dc13STsutomu Itoh if (IS_ERR(trans)) { 207898d5dc13STsutomu Itoh ret = PTR_ERR(trans); 2079bb059a37SDavid Sterba goto out_finish; 208098d5dc13STsutomu Itoh } 2081f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 20823a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 2083ece7d20eSMike Fleetwood } else if (new_size < old_size) { 2084f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 20850253f40eSjeff.liu } /* equal, nothing need to do */ 2086f46b5a66SChristoph Hellwig 2087faf8f7b9SMarcos Paulo de Souza if (ret == 0 && new_size != old_size) 2088faf8f7b9SMarcos Paulo de Souza btrfs_info_in_rcu(fs_info, 2089faf8f7b9SMarcos Paulo de Souza "resize device %s (devid %llu) from %llu to %llu", 2090faf8f7b9SMarcos Paulo de Souza rcu_str_deref(device->name), device->devid, 2091faf8f7b9SMarcos Paulo de Souza old_size, new_size); 2092bb059a37SDavid Sterba out_finish: 2093bb059a37SDavid Sterba btrfs_exclop_finish(fs_info); 2094c9e9f97bSIlya Dryomov out_free: 2095f46b5a66SChristoph Hellwig kfree(vol_args); 2096bb059a37SDavid Sterba out_drop: 209718f39c41SIlya Dryomov mnt_drop_write_file(file); 2098f46b5a66SChristoph Hellwig return ret; 2099f46b5a66SChristoph Hellwig } 2100f46b5a66SChristoph Hellwig 21015d54c67eSNikolay Borisov static noinline int __btrfs_ioctl_snap_create(struct file *file, 21024d4340c9SChristian Brauner struct user_namespace *mnt_userns, 210352f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 21045d54c67eSNikolay Borisov bool readonly, 21058696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 2106f46b5a66SChristoph Hellwig { 2107f46b5a66SChristoph Hellwig int namelen; 21083de4586cSChris Mason int ret = 0; 2109f46b5a66SChristoph Hellwig 2110325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 2111325c50e3SJeff Mahoney return -ENOTDIR; 2112325c50e3SJeff Mahoney 2113a874a63eSLiu Bo ret = mnt_want_write_file(file); 2114a874a63eSLiu Bo if (ret) 2115a874a63eSLiu Bo goto out; 2116a874a63eSLiu Bo 211772fd032eSSage Weil namelen = strlen(name); 211872fd032eSSage Weil if (strchr(name, '/')) { 2119f46b5a66SChristoph Hellwig ret = -EINVAL; 2120a874a63eSLiu Bo goto out_drop_write; 2121f46b5a66SChristoph Hellwig } 2122f46b5a66SChristoph Hellwig 212316780cabSChris Mason if (name[0] == '.' && 212416780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 212516780cabSChris Mason ret = -EEXIST; 2126a874a63eSLiu Bo goto out_drop_write; 212716780cabSChris Mason } 212816780cabSChris Mason 21293de4586cSChris Mason if (subvol) { 21304d4340c9SChristian Brauner ret = btrfs_mksubvol(&file->f_path, mnt_userns, name, 21314d4340c9SChristian Brauner namelen, NULL, readonly, inherit); 2132cb8e7090SChristoph Hellwig } else { 21332903ff01SAl Viro struct fd src = fdget(fd); 21343de4586cSChris Mason struct inode *src_inode; 21352903ff01SAl Viro if (!src.file) { 21363de4586cSChris Mason ret = -EINVAL; 2137a874a63eSLiu Bo goto out_drop_write; 21383de4586cSChris Mason } 21393de4586cSChris Mason 2140496ad9aaSAl Viro src_inode = file_inode(src.file); 2141496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 2142c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 2143efe120a0SFrank Holton "Snapshot src from another FS"); 214423ad5b17SKusanagi Kouichi ret = -EXDEV; 21454d4340c9SChristian Brauner } else if (!inode_owner_or_capable(mnt_userns, src_inode)) { 2146d0242061SDavid Sterba /* 2147d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 2148d0242061SDavid Sterba * are limited to own subvolumes only 2149d0242061SDavid Sterba */ 2150d0242061SDavid Sterba ret = -EPERM; 2151ecd18815SAl Viro } else { 21524d4340c9SChristian Brauner ret = btrfs_mksnapshot(&file->f_path, mnt_userns, 21534d4340c9SChristian Brauner name, namelen, 215472fd032eSSage Weil BTRFS_I(src_inode)->root, 21555d54c67eSNikolay Borisov readonly, inherit); 2156ecd18815SAl Viro } 21572903ff01SAl Viro fdput(src); 2158cb8e7090SChristoph Hellwig } 2159a874a63eSLiu Bo out_drop_write: 2160a874a63eSLiu Bo mnt_drop_write_file(file); 2161f46b5a66SChristoph Hellwig out: 216272fd032eSSage Weil return ret; 216372fd032eSSage Weil } 216472fd032eSSage Weil 216572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 2166fa0d2b9bSLi Zefan void __user *arg, int subvol) 216772fd032eSSage Weil { 2168fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 216972fd032eSSage Weil int ret; 217072fd032eSSage Weil 2171325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 2172325c50e3SJeff Mahoney return -ENOTDIR; 2173325c50e3SJeff Mahoney 2174fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2175fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 2176fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 2177fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2178fa0d2b9bSLi Zefan 21794d4340c9SChristian Brauner ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), 21804d4340c9SChristian Brauner vol_args->name, vol_args->fd, subvol, 21814d4340c9SChristian Brauner false, NULL); 2182fa0d2b9bSLi Zefan 2183fa0d2b9bSLi Zefan kfree(vol_args); 2184fa0d2b9bSLi Zefan return ret; 2185fa0d2b9bSLi Zefan } 2186fa0d2b9bSLi Zefan 2187fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 2188fa0d2b9bSLi Zefan void __user *arg, int subvol) 2189fa0d2b9bSLi Zefan { 2190fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 2191fa0d2b9bSLi Zefan int ret; 2192b83cc969SLi Zefan bool readonly = false; 21936f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 219472fd032eSSage Weil 2195325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 2196325c50e3SJeff Mahoney return -ENOTDIR; 2197325c50e3SJeff Mahoney 2198fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2199fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 2200fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 2201fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 2202fdfb1e4fSLi Zefan 2203673990dbSDavid Sterba if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { 2204b83cc969SLi Zefan ret = -EOPNOTSUPP; 2205c47ca32dSDan Carpenter goto free_args; 2206fdfb1e4fSLi Zefan } 2207fdfb1e4fSLi Zefan 2208b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 2209b83cc969SLi Zefan readonly = true; 22106f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 22115011c5a6SDan Carpenter u64 nums; 22125011c5a6SDan Carpenter 22135011c5a6SDan Carpenter if (vol_args->size < sizeof(*inherit) || 22145011c5a6SDan Carpenter vol_args->size > PAGE_SIZE) { 22156f72c7e2SArne Jansen ret = -EINVAL; 2216c47ca32dSDan Carpenter goto free_args; 22176f72c7e2SArne Jansen } 22186f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 22196f72c7e2SArne Jansen if (IS_ERR(inherit)) { 22206f72c7e2SArne Jansen ret = PTR_ERR(inherit); 2221c47ca32dSDan Carpenter goto free_args; 22226f72c7e2SArne Jansen } 22235011c5a6SDan Carpenter 22245011c5a6SDan Carpenter if (inherit->num_qgroups > PAGE_SIZE || 22255011c5a6SDan Carpenter inherit->num_ref_copies > PAGE_SIZE || 22265011c5a6SDan Carpenter inherit->num_excl_copies > PAGE_SIZE) { 22275011c5a6SDan Carpenter ret = -EINVAL; 22285011c5a6SDan Carpenter goto free_inherit; 22295011c5a6SDan Carpenter } 22305011c5a6SDan Carpenter 22315011c5a6SDan Carpenter nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + 22325011c5a6SDan Carpenter 2 * inherit->num_excl_copies; 22335011c5a6SDan Carpenter if (vol_args->size != struct_size(inherit, qgroups, nums)) { 22345011c5a6SDan Carpenter ret = -EINVAL; 22355011c5a6SDan Carpenter goto free_inherit; 22365011c5a6SDan Carpenter } 22376f72c7e2SArne Jansen } 223875eaa0e2SSage Weil 22394d4340c9SChristian Brauner ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), 22404d4340c9SChristian Brauner vol_args->name, vol_args->fd, subvol, 22414d4340c9SChristian Brauner readonly, inherit); 2242c47ca32dSDan Carpenter if (ret) 2243c47ca32dSDan Carpenter goto free_inherit; 2244c47ca32dSDan Carpenter free_inherit: 22456f72c7e2SArne Jansen kfree(inherit); 2246c47ca32dSDan Carpenter free_args: 2247c47ca32dSDan Carpenter kfree(vol_args); 2248f46b5a66SChristoph Hellwig return ret; 2249f46b5a66SChristoph Hellwig } 2250f46b5a66SChristoph Hellwig 22519ad12305SSahil Kang static noinline int btrfs_ioctl_subvol_getflags(struct inode *inode, 22520caa102dSLi Zefan void __user *arg) 22530caa102dSLi Zefan { 22540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 22550caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 22560caa102dSLi Zefan int ret = 0; 22570caa102dSLi Zefan u64 flags = 0; 22580caa102dSLi Zefan 22594a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 22600caa102dSLi Zefan return -EINVAL; 22610caa102dSLi Zefan 22620b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 22630caa102dSLi Zefan if (btrfs_root_readonly(root)) 22640caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 22650b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 22660caa102dSLi Zefan 22670caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 22680caa102dSLi Zefan ret = -EFAULT; 22690caa102dSLi Zefan 22700caa102dSLi Zefan return ret; 22710caa102dSLi Zefan } 22720caa102dSLi Zefan 22730caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 22740caa102dSLi Zefan void __user *arg) 22750caa102dSLi Zefan { 2276496ad9aaSAl Viro struct inode *inode = file_inode(file); 22770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 22780caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 22790caa102dSLi Zefan struct btrfs_trans_handle *trans; 22800caa102dSLi Zefan u64 root_flags; 22810caa102dSLi Zefan u64 flags; 22820caa102dSLi Zefan int ret = 0; 22830caa102dSLi Zefan 228439e1674fSChristian Brauner if (!inode_owner_or_capable(file_mnt_user_ns(file), inode)) 2285bd60ea0fSDavid Sterba return -EPERM; 2286bd60ea0fSDavid Sterba 2287b9ca0664SLiu Bo ret = mnt_want_write_file(file); 2288b9ca0664SLiu Bo if (ret) 2289b9ca0664SLiu Bo goto out; 22900caa102dSLi Zefan 22914a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 2292b9ca0664SLiu Bo ret = -EINVAL; 2293b9ca0664SLiu Bo goto out_drop_write; 2294b9ca0664SLiu Bo } 22950caa102dSLi Zefan 2296b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 2297b9ca0664SLiu Bo ret = -EFAULT; 2298b9ca0664SLiu Bo goto out_drop_write; 2299b9ca0664SLiu Bo } 23000caa102dSLi Zefan 2301b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 2302b9ca0664SLiu Bo ret = -EOPNOTSUPP; 2303b9ca0664SLiu Bo goto out_drop_write; 2304b9ca0664SLiu Bo } 23050caa102dSLi Zefan 23060b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 23070caa102dSLi Zefan 23080caa102dSLi Zefan /* nothing to do */ 23090caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 2310b9ca0664SLiu Bo goto out_drop_sem; 23110caa102dSLi Zefan 23120caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 23132c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 23140caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 23150caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 23162c686537SDavid Sterba } else { 23172c686537SDavid Sterba /* 23182c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 23192c686537SDavid Sterba * send 23202c686537SDavid Sterba */ 23212c686537SDavid Sterba spin_lock(&root->root_item_lock); 23222c686537SDavid Sterba if (root->send_in_progress == 0) { 23230caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 23240caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 23252c686537SDavid Sterba spin_unlock(&root->root_item_lock); 23262c686537SDavid Sterba } else { 23272c686537SDavid Sterba spin_unlock(&root->root_item_lock); 23280b246afaSJeff Mahoney btrfs_warn(fs_info, 23292c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 23302c686537SDavid Sterba root->root_key.objectid); 23312c686537SDavid Sterba ret = -EPERM; 23322c686537SDavid Sterba goto out_drop_sem; 23332c686537SDavid Sterba } 23342c686537SDavid Sterba } 23350caa102dSLi Zefan 23360caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 23370caa102dSLi Zefan if (IS_ERR(trans)) { 23380caa102dSLi Zefan ret = PTR_ERR(trans); 23390caa102dSLi Zefan goto out_reset; 23400caa102dSLi Zefan } 23410caa102dSLi Zefan 23420b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 23430caa102dSLi Zefan &root->root_key, &root->root_item); 23449417ebc8SNikolay Borisov if (ret < 0) { 23459417ebc8SNikolay Borisov btrfs_end_transaction(trans); 23469417ebc8SNikolay Borisov goto out_reset; 23479417ebc8SNikolay Borisov } 23480caa102dSLi Zefan 23499417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 23509417ebc8SNikolay Borisov 23510caa102dSLi Zefan out_reset: 23520caa102dSLi Zefan if (ret) 23530caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 2354b9ca0664SLiu Bo out_drop_sem: 23550b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 2356b9ca0664SLiu Bo out_drop_write: 2357b9ca0664SLiu Bo mnt_drop_write_file(file); 2358b9ca0664SLiu Bo out: 23590caa102dSLi Zefan return ret; 23600caa102dSLi Zefan } 23610caa102dSLi Zefan 2362ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 2363ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 2364ac8e9819SChris Mason { 2365abc6e134SChris Mason struct btrfs_key test; 2366abc6e134SChris Mason int ret; 2367abc6e134SChris Mason 2368abc6e134SChris Mason test.objectid = sk->min_objectid; 2369abc6e134SChris Mason test.type = sk->min_type; 2370abc6e134SChris Mason test.offset = sk->min_offset; 2371abc6e134SChris Mason 2372abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2373abc6e134SChris Mason if (ret < 0) 2374ac8e9819SChris Mason return 0; 2375abc6e134SChris Mason 2376abc6e134SChris Mason test.objectid = sk->max_objectid; 2377abc6e134SChris Mason test.type = sk->max_type; 2378abc6e134SChris Mason test.offset = sk->max_offset; 2379abc6e134SChris Mason 2380abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2381abc6e134SChris Mason if (ret > 0) 2382ac8e9819SChris Mason return 0; 2383ac8e9819SChris Mason return 1; 2384ac8e9819SChris Mason } 2385ac8e9819SChris Mason 2386df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 2387ac8e9819SChris Mason struct btrfs_key *key, 2388ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 23899b6e817dSGerhard Heift size_t *buf_size, 2390ba346b35SGerhard Heift char __user *ubuf, 2391ac8e9819SChris Mason unsigned long *sk_offset, 2392ac8e9819SChris Mason int *num_found) 2393ac8e9819SChris Mason { 2394ac8e9819SChris Mason u64 found_transid; 2395ac8e9819SChris Mason struct extent_buffer *leaf; 2396ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 2397dd81d459SNaohiro Aota struct btrfs_key test; 2398ac8e9819SChris Mason unsigned long item_off; 2399ac8e9819SChris Mason unsigned long item_len; 2400ac8e9819SChris Mason int nritems; 2401ac8e9819SChris Mason int i; 2402ac8e9819SChris Mason int slot; 2403ac8e9819SChris Mason int ret = 0; 2404ac8e9819SChris Mason 2405ac8e9819SChris Mason leaf = path->nodes[0]; 2406ac8e9819SChris Mason slot = path->slots[0]; 2407ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 2408ac8e9819SChris Mason 2409ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 2410ac8e9819SChris Mason i = nritems; 2411ac8e9819SChris Mason goto advance_key; 2412ac8e9819SChris Mason } 2413ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 2414ac8e9819SChris Mason 2415ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 2416ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 24173212fa14SJosef Bacik item_len = btrfs_item_size(leaf, i); 2418ac8e9819SChris Mason 241903b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 242003b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 242103b71c6cSGabriel de Perthuis continue; 242203b71c6cSGabriel de Perthuis 24239b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 24248f5f6178SGerhard Heift if (*num_found) { 2425ac8e9819SChris Mason ret = 1; 24268f5f6178SGerhard Heift goto out; 24278f5f6178SGerhard Heift } 24288f5f6178SGerhard Heift 24298f5f6178SGerhard Heift /* 24308f5f6178SGerhard Heift * return one empty item back for v1, which does not 24318f5f6178SGerhard Heift * handle -EOVERFLOW 24328f5f6178SGerhard Heift */ 24338f5f6178SGerhard Heift 24349b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2435ac8e9819SChris Mason item_len = 0; 24368f5f6178SGerhard Heift ret = -EOVERFLOW; 24378f5f6178SGerhard Heift } 2438ac8e9819SChris Mason 24399b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2440ac8e9819SChris Mason ret = 1; 244125c9bc2eSGerhard Heift goto out; 2442ac8e9819SChris Mason } 2443ac8e9819SChris Mason 2444ac8e9819SChris Mason sh.objectid = key->objectid; 2445ac8e9819SChris Mason sh.offset = key->offset; 2446ac8e9819SChris Mason sh.type = key->type; 2447ac8e9819SChris Mason sh.len = item_len; 2448ac8e9819SChris Mason sh.transid = found_transid; 2449ac8e9819SChris Mason 2450a48b73ecSJosef Bacik /* 2451a48b73ecSJosef Bacik * Copy search result header. If we fault then loop again so we 2452a48b73ecSJosef Bacik * can fault in the pages and -EFAULT there if there's a 2453a48b73ecSJosef Bacik * problem. Otherwise we'll fault and then copy the buffer in 2454a48b73ecSJosef Bacik * properly this next time through 2455a48b73ecSJosef Bacik */ 2456a48b73ecSJosef Bacik if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) { 2457a48b73ecSJosef Bacik ret = 0; 2458ba346b35SGerhard Heift goto out; 2459ba346b35SGerhard Heift } 2460ba346b35SGerhard Heift 2461ac8e9819SChris Mason *sk_offset += sizeof(sh); 2462ac8e9819SChris Mason 2463ac8e9819SChris Mason if (item_len) { 2464ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2465a48b73ecSJosef Bacik /* 2466a48b73ecSJosef Bacik * Copy the item, same behavior as above, but reset the 2467a48b73ecSJosef Bacik * * sk_offset so we copy the full thing again. 2468a48b73ecSJosef Bacik */ 2469a48b73ecSJosef Bacik if (read_extent_buffer_to_user_nofault(leaf, up, 2470ba346b35SGerhard Heift item_off, item_len)) { 2471a48b73ecSJosef Bacik ret = 0; 2472a48b73ecSJosef Bacik *sk_offset -= sizeof(sh); 2473ba346b35SGerhard Heift goto out; 2474ba346b35SGerhard Heift } 2475ba346b35SGerhard Heift 2476ac8e9819SChris Mason *sk_offset += item_len; 2477ac8e9819SChris Mason } 2478e2156867SHugo Mills (*num_found)++; 2479ac8e9819SChris Mason 24808f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 24818f5f6178SGerhard Heift goto out; 24828f5f6178SGerhard Heift 248325c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 248425c9bc2eSGerhard Heift ret = 1; 248525c9bc2eSGerhard Heift goto out; 248625c9bc2eSGerhard Heift } 2487ac8e9819SChris Mason } 2488ac8e9819SChris Mason advance_key: 2489ac8e9819SChris Mason ret = 0; 2490dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2491dd81d459SNaohiro Aota test.type = sk->max_type; 2492dd81d459SNaohiro Aota test.offset = sk->max_offset; 2493dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2494dd81d459SNaohiro Aota ret = 1; 2495dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2496abc6e134SChris Mason key->offset++; 2497dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2498abc6e134SChris Mason key->offset = 0; 2499abc6e134SChris Mason key->type++; 2500dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2501abc6e134SChris Mason key->offset = 0; 2502abc6e134SChris Mason key->type = 0; 2503abc6e134SChris Mason key->objectid++; 2504abc6e134SChris Mason } else 2505abc6e134SChris Mason ret = 1; 250625c9bc2eSGerhard Heift out: 2507ba346b35SGerhard Heift /* 2508ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2509ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2510ba346b35SGerhard Heift * * all items were found 2511ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2512ba346b35SGerhard Heift * leaf 2513ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2514ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2515ba346b35SGerhard Heift */ 2516ac8e9819SChris Mason return ret; 2517ac8e9819SChris Mason } 2518ac8e9819SChris Mason 2519ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 252012544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 25219b6e817dSGerhard Heift size_t *buf_size, 2522ba346b35SGerhard Heift char __user *ubuf) 2523ac8e9819SChris Mason { 25240b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2525ac8e9819SChris Mason struct btrfs_root *root; 2526ac8e9819SChris Mason struct btrfs_key key; 2527ac8e9819SChris Mason struct btrfs_path *path; 2528ac8e9819SChris Mason int ret; 2529ac8e9819SChris Mason int num_found = 0; 2530ac8e9819SChris Mason unsigned long sk_offset = 0; 2531ac8e9819SChris Mason 25329b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 25339b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 253412544442SGerhard Heift return -EOVERFLOW; 25359b6e817dSGerhard Heift } 253612544442SGerhard Heift 2537ac8e9819SChris Mason path = btrfs_alloc_path(); 2538ac8e9819SChris Mason if (!path) 2539ac8e9819SChris Mason return -ENOMEM; 2540ac8e9819SChris Mason 2541ac8e9819SChris Mason if (sk->tree_id == 0) { 2542ac8e9819SChris Mason /* search the root of the inode that was passed */ 254300246528SJosef Bacik root = btrfs_grab_root(BTRFS_I(inode)->root); 2544ac8e9819SChris Mason } else { 254556e9357aSDavid Sterba root = btrfs_get_fs_root(info, sk->tree_id, true); 2546ac8e9819SChris Mason if (IS_ERR(root)) { 2547ac8e9819SChris Mason btrfs_free_path(path); 2548ad1e3d56SMisono Tomohiro return PTR_ERR(root); 2549ac8e9819SChris Mason } 2550ac8e9819SChris Mason } 2551ac8e9819SChris Mason 2552ac8e9819SChris Mason key.objectid = sk->min_objectid; 2553ac8e9819SChris Mason key.type = sk->min_type; 2554ac8e9819SChris Mason key.offset = sk->min_offset; 2555ac8e9819SChris Mason 2556ac8e9819SChris Mason while (1) { 2557bb523b40SAndreas Gruenbacher ret = -EFAULT; 2558bb523b40SAndreas Gruenbacher if (fault_in_writeable(ubuf + sk_offset, *buf_size - sk_offset)) 2559a48b73ecSJosef Bacik break; 2560a48b73ecSJosef Bacik 25616174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2562ac8e9819SChris Mason if (ret != 0) { 2563ac8e9819SChris Mason if (ret > 0) 2564ac8e9819SChris Mason ret = 0; 2565ac8e9819SChris Mason goto err; 2566ac8e9819SChris Mason } 2567df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2568ac8e9819SChris Mason &sk_offset, &num_found); 2569b3b4aa74SDavid Sterba btrfs_release_path(path); 257025c9bc2eSGerhard Heift if (ret) 2571ac8e9819SChris Mason break; 2572ac8e9819SChris Mason 2573ac8e9819SChris Mason } 25748f5f6178SGerhard Heift if (ret > 0) 2575ac8e9819SChris Mason ret = 0; 2576ac8e9819SChris Mason err: 2577ac8e9819SChris Mason sk->nr_items = num_found; 257800246528SJosef Bacik btrfs_put_root(root); 2579ac8e9819SChris Mason btrfs_free_path(path); 2580ac8e9819SChris Mason return ret; 2581ac8e9819SChris Mason } 2582ac8e9819SChris Mason 25839ad12305SSahil Kang static noinline int btrfs_ioctl_tree_search(struct inode *inode, 2584ac8e9819SChris Mason void __user *argp) 2585ac8e9819SChris Mason { 2586ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2587ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2588ac8e9819SChris Mason int ret; 25899b6e817dSGerhard Heift size_t buf_size; 2590ac8e9819SChris Mason 2591ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2592ac8e9819SChris Mason return -EPERM; 2593ac8e9819SChris Mason 2594ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2595ac8e9819SChris Mason 2596ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2597ba346b35SGerhard Heift return -EFAULT; 2598ba346b35SGerhard Heift 2599ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2600ac8e9819SChris Mason 2601ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 26028f5f6178SGerhard Heift 26038f5f6178SGerhard Heift /* 26048f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 26058f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 26068f5f6178SGerhard Heift */ 26078f5f6178SGerhard Heift if (ret == -EOVERFLOW) 26088f5f6178SGerhard Heift ret = 0; 26098f5f6178SGerhard Heift 2610ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2611ac8e9819SChris Mason ret = -EFAULT; 2612ac8e9819SChris Mason return ret; 2613ac8e9819SChris Mason } 2614ac8e9819SChris Mason 26159ad12305SSahil Kang static noinline int btrfs_ioctl_tree_search_v2(struct inode *inode, 2616cc68a8a5SGerhard Heift void __user *argp) 2617cc68a8a5SGerhard Heift { 2618cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2619cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2620cc68a8a5SGerhard Heift int ret; 2621cc68a8a5SGerhard Heift size_t buf_size; 2622ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2623cc68a8a5SGerhard Heift 2624cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2625cc68a8a5SGerhard Heift return -EPERM; 2626cc68a8a5SGerhard Heift 2627cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2628cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2629cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2630cc68a8a5SGerhard Heift return -EFAULT; 2631cc68a8a5SGerhard Heift 2632cc68a8a5SGerhard Heift buf_size = args.buf_size; 2633cc68a8a5SGerhard Heift 2634cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2635cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2636cc68a8a5SGerhard Heift buf_size = buf_limit; 2637cc68a8a5SGerhard Heift 2638cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2639718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2640cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2641cc68a8a5SGerhard Heift ret = -EFAULT; 2642cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2643cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2644cc68a8a5SGerhard Heift ret = -EFAULT; 2645cc68a8a5SGerhard Heift 264676dda93cSYan, Zheng return ret; 264776dda93cSYan, Zheng } 264876dda93cSYan, Zheng 264998d377a0STARUISI Hiroaki /* 2650ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2651ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 265298d377a0STARUISI Hiroaki */ 265398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 265498d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 265598d377a0STARUISI Hiroaki { 265698d377a0STARUISI Hiroaki struct btrfs_root *root; 265798d377a0STARUISI Hiroaki struct btrfs_key key; 2658ac8e9819SChris Mason char *ptr; 265998d377a0STARUISI Hiroaki int ret = -1; 266098d377a0STARUISI Hiroaki int slot; 266198d377a0STARUISI Hiroaki int len; 266298d377a0STARUISI Hiroaki int total_len = 0; 266398d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 266498d377a0STARUISI Hiroaki struct extent_buffer *l; 266598d377a0STARUISI Hiroaki struct btrfs_path *path; 266698d377a0STARUISI Hiroaki 266798d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 266898d377a0STARUISI Hiroaki name[0]='\0'; 266998d377a0STARUISI Hiroaki return 0; 267098d377a0STARUISI Hiroaki } 267198d377a0STARUISI Hiroaki 267298d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 267398d377a0STARUISI Hiroaki if (!path) 267498d377a0STARUISI Hiroaki return -ENOMEM; 267598d377a0STARUISI Hiroaki 2676c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 267798d377a0STARUISI Hiroaki 267856e9357aSDavid Sterba root = btrfs_get_fs_root(info, tree_id, true); 267998d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2680ad1e3d56SMisono Tomohiro ret = PTR_ERR(root); 268188234012SJosef Bacik root = NULL; 268288234012SJosef Bacik goto out; 268388234012SJosef Bacik } 268498d377a0STARUISI Hiroaki 268598d377a0STARUISI Hiroaki key.objectid = dirid; 268698d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 26878ad6fcabSChris Mason key.offset = (u64)-1; 268898d377a0STARUISI Hiroaki 268998d377a0STARUISI Hiroaki while (1) { 26900ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(root, &key, path); 269118674c6cSFilipe David Borba Manana if (ret < 0) 269218674c6cSFilipe David Borba Manana goto out; 269318674c6cSFilipe David Borba Manana else if (ret > 0) { 2694ac8e9819SChris Mason ret = -ENOENT; 269598d377a0STARUISI Hiroaki goto out; 2696ac8e9819SChris Mason } 269718674c6cSFilipe David Borba Manana 269818674c6cSFilipe David Borba Manana l = path->nodes[0]; 269918674c6cSFilipe David Borba Manana slot = path->slots[0]; 270098d377a0STARUISI Hiroaki 270198d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 270298d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 270398d377a0STARUISI Hiroaki ptr -= len + 1; 270498d377a0STARUISI Hiroaki total_len += len + 1; 2705a696cf35SFilipe David Borba Manana if (ptr < name) { 2706a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 270798d377a0STARUISI Hiroaki goto out; 2708a696cf35SFilipe David Borba Manana } 270998d377a0STARUISI Hiroaki 271098d377a0STARUISI Hiroaki *(ptr + len) = '/'; 271198d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 271298d377a0STARUISI Hiroaki 271398d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 271498d377a0STARUISI Hiroaki break; 271598d377a0STARUISI Hiroaki 2716b3b4aa74SDavid Sterba btrfs_release_path(path); 271798d377a0STARUISI Hiroaki key.objectid = key.offset; 27188ad6fcabSChris Mason key.offset = (u64)-1; 271998d377a0STARUISI Hiroaki dirid = key.objectid; 272098d377a0STARUISI Hiroaki } 272177906a50SLi Zefan memmove(name, ptr, total_len); 272298d377a0STARUISI Hiroaki name[total_len] = '\0'; 272398d377a0STARUISI Hiroaki ret = 0; 272498d377a0STARUISI Hiroaki out: 272500246528SJosef Bacik btrfs_put_root(root); 272698d377a0STARUISI Hiroaki btrfs_free_path(path); 2727ac8e9819SChris Mason return ret; 2728ac8e9819SChris Mason } 2729ac8e9819SChris Mason 27306623d9a0SChristian Brauner static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns, 27316623d9a0SChristian Brauner struct inode *inode, 273223d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args) 273323d0b79dSTomohiro Misono { 273423d0b79dSTomohiro Misono struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 273523d0b79dSTomohiro Misono struct super_block *sb = inode->i_sb; 273623d0b79dSTomohiro Misono struct btrfs_key upper_limit = BTRFS_I(inode)->location; 273723d0b79dSTomohiro Misono u64 treeid = BTRFS_I(inode)->root->root_key.objectid; 273823d0b79dSTomohiro Misono u64 dirid = args->dirid; 273923d0b79dSTomohiro Misono unsigned long item_off; 274023d0b79dSTomohiro Misono unsigned long item_len; 274123d0b79dSTomohiro Misono struct btrfs_inode_ref *iref; 274223d0b79dSTomohiro Misono struct btrfs_root_ref *rref; 2743b8a49ae1SJosef Bacik struct btrfs_root *root = NULL; 274423d0b79dSTomohiro Misono struct btrfs_path *path; 274523d0b79dSTomohiro Misono struct btrfs_key key, key2; 274623d0b79dSTomohiro Misono struct extent_buffer *leaf; 274723d0b79dSTomohiro Misono struct inode *temp_inode; 274823d0b79dSTomohiro Misono char *ptr; 274923d0b79dSTomohiro Misono int slot; 275023d0b79dSTomohiro Misono int len; 275123d0b79dSTomohiro Misono int total_len = 0; 275223d0b79dSTomohiro Misono int ret; 275323d0b79dSTomohiro Misono 275423d0b79dSTomohiro Misono path = btrfs_alloc_path(); 275523d0b79dSTomohiro Misono if (!path) 275623d0b79dSTomohiro Misono return -ENOMEM; 275723d0b79dSTomohiro Misono 275823d0b79dSTomohiro Misono /* 275923d0b79dSTomohiro Misono * If the bottom subvolume does not exist directly under upper_limit, 276023d0b79dSTomohiro Misono * construct the path in from the bottom up. 276123d0b79dSTomohiro Misono */ 276223d0b79dSTomohiro Misono if (dirid != upper_limit.objectid) { 276323d0b79dSTomohiro Misono ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; 276423d0b79dSTomohiro Misono 276556e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, treeid, true); 276623d0b79dSTomohiro Misono if (IS_ERR(root)) { 276723d0b79dSTomohiro Misono ret = PTR_ERR(root); 276823d0b79dSTomohiro Misono goto out; 276923d0b79dSTomohiro Misono } 277023d0b79dSTomohiro Misono 277123d0b79dSTomohiro Misono key.objectid = dirid; 277223d0b79dSTomohiro Misono key.type = BTRFS_INODE_REF_KEY; 277323d0b79dSTomohiro Misono key.offset = (u64)-1; 277423d0b79dSTomohiro Misono while (1) { 27750ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(root, &key, path); 27760ff40a91SMarcos Paulo de Souza if (ret < 0) 2777b8a49ae1SJosef Bacik goto out_put; 27780ff40a91SMarcos Paulo de Souza else if (ret > 0) { 277923d0b79dSTomohiro Misono ret = -ENOENT; 2780b8a49ae1SJosef Bacik goto out_put; 278123d0b79dSTomohiro Misono } 278223d0b79dSTomohiro Misono 278323d0b79dSTomohiro Misono leaf = path->nodes[0]; 278423d0b79dSTomohiro Misono slot = path->slots[0]; 278523d0b79dSTomohiro Misono 278623d0b79dSTomohiro Misono iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); 278723d0b79dSTomohiro Misono len = btrfs_inode_ref_name_len(leaf, iref); 278823d0b79dSTomohiro Misono ptr -= len + 1; 278923d0b79dSTomohiro Misono total_len += len + 1; 279023d0b79dSTomohiro Misono if (ptr < args->path) { 279123d0b79dSTomohiro Misono ret = -ENAMETOOLONG; 2792b8a49ae1SJosef Bacik goto out_put; 279323d0b79dSTomohiro Misono } 279423d0b79dSTomohiro Misono 279523d0b79dSTomohiro Misono *(ptr + len) = '/'; 279623d0b79dSTomohiro Misono read_extent_buffer(leaf, ptr, 279723d0b79dSTomohiro Misono (unsigned long)(iref + 1), len); 279823d0b79dSTomohiro Misono 279923d0b79dSTomohiro Misono /* Check the read+exec permission of this directory */ 280023d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 280123d0b79dSTomohiro Misono BTRFS_INODE_ITEM_KEY); 280223d0b79dSTomohiro Misono if (ret < 0) { 2803b8a49ae1SJosef Bacik goto out_put; 280423d0b79dSTomohiro Misono } else if (ret > 0) { 280523d0b79dSTomohiro Misono ret = -ENOENT; 2806b8a49ae1SJosef Bacik goto out_put; 280723d0b79dSTomohiro Misono } 280823d0b79dSTomohiro Misono 280923d0b79dSTomohiro Misono leaf = path->nodes[0]; 281023d0b79dSTomohiro Misono slot = path->slots[0]; 281123d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key2, slot); 281223d0b79dSTomohiro Misono if (key2.objectid != dirid) { 281323d0b79dSTomohiro Misono ret = -ENOENT; 2814b8a49ae1SJosef Bacik goto out_put; 281523d0b79dSTomohiro Misono } 281623d0b79dSTomohiro Misono 28170202e83fSDavid Sterba temp_inode = btrfs_iget(sb, key2.objectid, root); 28183ca57bd6SMisono Tomohiro if (IS_ERR(temp_inode)) { 28193ca57bd6SMisono Tomohiro ret = PTR_ERR(temp_inode); 2820b8a49ae1SJosef Bacik goto out_put; 28213ca57bd6SMisono Tomohiro } 28226623d9a0SChristian Brauner ret = inode_permission(mnt_userns, temp_inode, 282347291baaSChristian Brauner MAY_READ | MAY_EXEC); 282423d0b79dSTomohiro Misono iput(temp_inode); 282523d0b79dSTomohiro Misono if (ret) { 282623d0b79dSTomohiro Misono ret = -EACCES; 2827b8a49ae1SJosef Bacik goto out_put; 282823d0b79dSTomohiro Misono } 282923d0b79dSTomohiro Misono 283023d0b79dSTomohiro Misono if (key.offset == upper_limit.objectid) 283123d0b79dSTomohiro Misono break; 283223d0b79dSTomohiro Misono if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { 283323d0b79dSTomohiro Misono ret = -EACCES; 2834b8a49ae1SJosef Bacik goto out_put; 283523d0b79dSTomohiro Misono } 283623d0b79dSTomohiro Misono 283723d0b79dSTomohiro Misono btrfs_release_path(path); 283823d0b79dSTomohiro Misono key.objectid = key.offset; 283923d0b79dSTomohiro Misono key.offset = (u64)-1; 284023d0b79dSTomohiro Misono dirid = key.objectid; 284123d0b79dSTomohiro Misono } 284223d0b79dSTomohiro Misono 284323d0b79dSTomohiro Misono memmove(args->path, ptr, total_len); 284423d0b79dSTomohiro Misono args->path[total_len] = '\0'; 284500246528SJosef Bacik btrfs_put_root(root); 2846b8a49ae1SJosef Bacik root = NULL; 284723d0b79dSTomohiro Misono btrfs_release_path(path); 284823d0b79dSTomohiro Misono } 284923d0b79dSTomohiro Misono 285023d0b79dSTomohiro Misono /* Get the bottom subvolume's name from ROOT_REF */ 285123d0b79dSTomohiro Misono key.objectid = treeid; 285223d0b79dSTomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 285323d0b79dSTomohiro Misono key.offset = args->treeid; 2854b8a49ae1SJosef Bacik ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 285523d0b79dSTomohiro Misono if (ret < 0) { 285623d0b79dSTomohiro Misono goto out; 285723d0b79dSTomohiro Misono } else if (ret > 0) { 285823d0b79dSTomohiro Misono ret = -ENOENT; 285923d0b79dSTomohiro Misono goto out; 286023d0b79dSTomohiro Misono } 286123d0b79dSTomohiro Misono 286223d0b79dSTomohiro Misono leaf = path->nodes[0]; 286323d0b79dSTomohiro Misono slot = path->slots[0]; 286423d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 286523d0b79dSTomohiro Misono 286623d0b79dSTomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot); 28673212fa14SJosef Bacik item_len = btrfs_item_size(leaf, slot); 286823d0b79dSTomohiro Misono /* Check if dirid in ROOT_REF corresponds to passed dirid */ 286923d0b79dSTomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 287023d0b79dSTomohiro Misono if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { 287123d0b79dSTomohiro Misono ret = -EINVAL; 287223d0b79dSTomohiro Misono goto out; 287323d0b79dSTomohiro Misono } 287423d0b79dSTomohiro Misono 287523d0b79dSTomohiro Misono /* Copy subvolume's name */ 287623d0b79dSTomohiro Misono item_off += sizeof(struct btrfs_root_ref); 287723d0b79dSTomohiro Misono item_len -= sizeof(struct btrfs_root_ref); 287823d0b79dSTomohiro Misono read_extent_buffer(leaf, args->name, item_off, item_len); 287923d0b79dSTomohiro Misono args->name[item_len] = 0; 288023d0b79dSTomohiro Misono 2881b8a49ae1SJosef Bacik out_put: 288200246528SJosef Bacik btrfs_put_root(root); 288323d0b79dSTomohiro Misono out: 288423d0b79dSTomohiro Misono btrfs_free_path(path); 288523d0b79dSTomohiro Misono return ret; 288623d0b79dSTomohiro Misono } 288723d0b79dSTomohiro Misono 2888dc408ccdSSahil Kang static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root, 2889ac8e9819SChris Mason void __user *argp) 2890ac8e9819SChris Mason { 2891ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 289201b810b8SDavid Sterba int ret = 0; 2893ac8e9819SChris Mason 28942354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 28952354d08fSJulia Lawall if (IS_ERR(args)) 28962354d08fSJulia Lawall return PTR_ERR(args); 2897c2b96929SDan Carpenter 289801b810b8SDavid Sterba /* 289901b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 290001b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 290101b810b8SDavid Sterba */ 29021b53ac4dSChris Mason if (args->treeid == 0) 2903dc408ccdSSahil Kang args->treeid = root->root_key.objectid; 29041b53ac4dSChris Mason 290501b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 290601b810b8SDavid Sterba args->name[0] = 0; 290701b810b8SDavid Sterba goto out; 290801b810b8SDavid Sterba } 290901b810b8SDavid Sterba 291001b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 291101b810b8SDavid Sterba ret = -EPERM; 291201b810b8SDavid Sterba goto out; 291301b810b8SDavid Sterba } 291401b810b8SDavid Sterba 2915dc408ccdSSahil Kang ret = btrfs_search_path_in_tree(root->fs_info, 2916ac8e9819SChris Mason args->treeid, args->objectid, 2917ac8e9819SChris Mason args->name); 2918ac8e9819SChris Mason 291901b810b8SDavid Sterba out: 2920ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2921ac8e9819SChris Mason ret = -EFAULT; 2922ac8e9819SChris Mason 2923ac8e9819SChris Mason kfree(args); 292498d377a0STARUISI Hiroaki return ret; 292598d377a0STARUISI Hiroaki } 292698d377a0STARUISI Hiroaki 292723d0b79dSTomohiro Misono /* 292823d0b79dSTomohiro Misono * Version of ino_lookup ioctl (unprivileged) 292923d0b79dSTomohiro Misono * 293023d0b79dSTomohiro Misono * The main differences from ino_lookup ioctl are: 293123d0b79dSTomohiro Misono * 293223d0b79dSTomohiro Misono * 1. Read + Exec permission will be checked using inode_permission() during 293323d0b79dSTomohiro Misono * path construction. -EACCES will be returned in case of failure. 293423d0b79dSTomohiro Misono * 2. Path construction will be stopped at the inode number which corresponds 293523d0b79dSTomohiro Misono * to the fd with which this ioctl is called. If constructed path does not 293623d0b79dSTomohiro Misono * exist under fd's inode, -EACCES will be returned. 293723d0b79dSTomohiro Misono * 3. The name of bottom subvolume is also searched and filled. 293823d0b79dSTomohiro Misono */ 293923d0b79dSTomohiro Misono static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) 294023d0b79dSTomohiro Misono { 294123d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args; 294223d0b79dSTomohiro Misono struct inode *inode; 294323d0b79dSTomohiro Misono int ret; 294423d0b79dSTomohiro Misono 294523d0b79dSTomohiro Misono args = memdup_user(argp, sizeof(*args)); 294623d0b79dSTomohiro Misono if (IS_ERR(args)) 294723d0b79dSTomohiro Misono return PTR_ERR(args); 294823d0b79dSTomohiro Misono 294923d0b79dSTomohiro Misono inode = file_inode(file); 295023d0b79dSTomohiro Misono 295123d0b79dSTomohiro Misono if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && 295223d0b79dSTomohiro Misono BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { 295323d0b79dSTomohiro Misono /* 295423d0b79dSTomohiro Misono * The subvolume does not exist under fd with which this is 295523d0b79dSTomohiro Misono * called 295623d0b79dSTomohiro Misono */ 295723d0b79dSTomohiro Misono kfree(args); 295823d0b79dSTomohiro Misono return -EACCES; 295923d0b79dSTomohiro Misono } 296023d0b79dSTomohiro Misono 29616623d9a0SChristian Brauner ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args); 296223d0b79dSTomohiro Misono 296323d0b79dSTomohiro Misono if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 296423d0b79dSTomohiro Misono ret = -EFAULT; 296523d0b79dSTomohiro Misono 296623d0b79dSTomohiro Misono kfree(args); 296723d0b79dSTomohiro Misono return ret; 296823d0b79dSTomohiro Misono } 296923d0b79dSTomohiro Misono 2970b64ec075STomohiro Misono /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ 29719ad12305SSahil Kang static int btrfs_ioctl_get_subvol_info(struct inode *inode, void __user *argp) 2972b64ec075STomohiro Misono { 2973b64ec075STomohiro Misono struct btrfs_ioctl_get_subvol_info_args *subvol_info; 2974b64ec075STomohiro Misono struct btrfs_fs_info *fs_info; 2975b64ec075STomohiro Misono struct btrfs_root *root; 2976b64ec075STomohiro Misono struct btrfs_path *path; 2977b64ec075STomohiro Misono struct btrfs_key key; 2978b64ec075STomohiro Misono struct btrfs_root_item *root_item; 2979b64ec075STomohiro Misono struct btrfs_root_ref *rref; 2980b64ec075STomohiro Misono struct extent_buffer *leaf; 2981b64ec075STomohiro Misono unsigned long item_off; 2982b64ec075STomohiro Misono unsigned long item_len; 2983b64ec075STomohiro Misono int slot; 2984b64ec075STomohiro Misono int ret = 0; 2985b64ec075STomohiro Misono 2986b64ec075STomohiro Misono path = btrfs_alloc_path(); 2987b64ec075STomohiro Misono if (!path) 2988b64ec075STomohiro Misono return -ENOMEM; 2989b64ec075STomohiro Misono 2990b64ec075STomohiro Misono subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); 2991b64ec075STomohiro Misono if (!subvol_info) { 2992b64ec075STomohiro Misono btrfs_free_path(path); 2993b64ec075STomohiro Misono return -ENOMEM; 2994b64ec075STomohiro Misono } 2995b64ec075STomohiro Misono 2996b64ec075STomohiro Misono fs_info = BTRFS_I(inode)->root->fs_info; 2997b64ec075STomohiro Misono 2998b64ec075STomohiro Misono /* Get root_item of inode's subvolume */ 2999b64ec075STomohiro Misono key.objectid = BTRFS_I(inode)->root->root_key.objectid; 300056e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, key.objectid, true); 3001b64ec075STomohiro Misono if (IS_ERR(root)) { 3002b64ec075STomohiro Misono ret = PTR_ERR(root); 300304734e84SJosef Bacik goto out_free; 300404734e84SJosef Bacik } 3005b64ec075STomohiro Misono root_item = &root->root_item; 3006b64ec075STomohiro Misono 3007b64ec075STomohiro Misono subvol_info->treeid = key.objectid; 3008b64ec075STomohiro Misono 3009b64ec075STomohiro Misono subvol_info->generation = btrfs_root_generation(root_item); 3010b64ec075STomohiro Misono subvol_info->flags = btrfs_root_flags(root_item); 3011b64ec075STomohiro Misono 3012b64ec075STomohiro Misono memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); 3013b64ec075STomohiro Misono memcpy(subvol_info->parent_uuid, root_item->parent_uuid, 3014b64ec075STomohiro Misono BTRFS_UUID_SIZE); 3015b64ec075STomohiro Misono memcpy(subvol_info->received_uuid, root_item->received_uuid, 3016b64ec075STomohiro Misono BTRFS_UUID_SIZE); 3017b64ec075STomohiro Misono 3018b64ec075STomohiro Misono subvol_info->ctransid = btrfs_root_ctransid(root_item); 3019b64ec075STomohiro Misono subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); 3020b64ec075STomohiro Misono subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); 3021b64ec075STomohiro Misono 3022b64ec075STomohiro Misono subvol_info->otransid = btrfs_root_otransid(root_item); 3023b64ec075STomohiro Misono subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); 3024b64ec075STomohiro Misono subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); 3025b64ec075STomohiro Misono 3026b64ec075STomohiro Misono subvol_info->stransid = btrfs_root_stransid(root_item); 3027b64ec075STomohiro Misono subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); 3028b64ec075STomohiro Misono subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); 3029b64ec075STomohiro Misono 3030b64ec075STomohiro Misono subvol_info->rtransid = btrfs_root_rtransid(root_item); 3031b64ec075STomohiro Misono subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); 3032b64ec075STomohiro Misono subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); 3033b64ec075STomohiro Misono 3034b64ec075STomohiro Misono if (key.objectid != BTRFS_FS_TREE_OBJECTID) { 3035b64ec075STomohiro Misono /* Search root tree for ROOT_BACKREF of this subvolume */ 3036b64ec075STomohiro Misono key.type = BTRFS_ROOT_BACKREF_KEY; 3037b64ec075STomohiro Misono key.offset = 0; 303804734e84SJosef Bacik ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 3039b64ec075STomohiro Misono if (ret < 0) { 3040b64ec075STomohiro Misono goto out; 3041b64ec075STomohiro Misono } else if (path->slots[0] >= 3042b64ec075STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 304304734e84SJosef Bacik ret = btrfs_next_leaf(fs_info->tree_root, path); 3044b64ec075STomohiro Misono if (ret < 0) { 3045b64ec075STomohiro Misono goto out; 3046b64ec075STomohiro Misono } else if (ret > 0) { 3047b64ec075STomohiro Misono ret = -EUCLEAN; 3048b64ec075STomohiro Misono goto out; 3049b64ec075STomohiro Misono } 3050b64ec075STomohiro Misono } 3051b64ec075STomohiro Misono 3052b64ec075STomohiro Misono leaf = path->nodes[0]; 3053b64ec075STomohiro Misono slot = path->slots[0]; 3054b64ec075STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 3055b64ec075STomohiro Misono if (key.objectid == subvol_info->treeid && 3056b64ec075STomohiro Misono key.type == BTRFS_ROOT_BACKREF_KEY) { 3057b64ec075STomohiro Misono subvol_info->parent_id = key.offset; 3058b64ec075STomohiro Misono 3059b64ec075STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 3060b64ec075STomohiro Misono subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); 3061b64ec075STomohiro Misono 3062b64ec075STomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot) 3063b64ec075STomohiro Misono + sizeof(struct btrfs_root_ref); 30643212fa14SJosef Bacik item_len = btrfs_item_size(leaf, slot) 3065b64ec075STomohiro Misono - sizeof(struct btrfs_root_ref); 3066b64ec075STomohiro Misono read_extent_buffer(leaf, subvol_info->name, 3067b64ec075STomohiro Misono item_off, item_len); 3068b64ec075STomohiro Misono } else { 3069b64ec075STomohiro Misono ret = -ENOENT; 3070b64ec075STomohiro Misono goto out; 3071b64ec075STomohiro Misono } 3072b64ec075STomohiro Misono } 3073b64ec075STomohiro Misono 3074b64ec075STomohiro Misono if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) 3075b64ec075STomohiro Misono ret = -EFAULT; 3076b64ec075STomohiro Misono 3077b64ec075STomohiro Misono out: 307800246528SJosef Bacik btrfs_put_root(root); 307904734e84SJosef Bacik out_free: 3080b64ec075STomohiro Misono btrfs_free_path(path); 3081b091f7feSWaiman Long kfree(subvol_info); 3082b64ec075STomohiro Misono return ret; 3083b64ec075STomohiro Misono } 3084b64ec075STomohiro Misono 308542e4b520STomohiro Misono /* 308642e4b520STomohiro Misono * Return ROOT_REF information of the subvolume containing this inode 308742e4b520STomohiro Misono * except the subvolume name. 308842e4b520STomohiro Misono */ 30899ad12305SSahil Kang static int btrfs_ioctl_get_subvol_rootref(struct btrfs_root *root, 3090dc408ccdSSahil Kang void __user *argp) 309142e4b520STomohiro Misono { 309242e4b520STomohiro Misono struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; 309342e4b520STomohiro Misono struct btrfs_root_ref *rref; 309442e4b520STomohiro Misono struct btrfs_path *path; 309542e4b520STomohiro Misono struct btrfs_key key; 309642e4b520STomohiro Misono struct extent_buffer *leaf; 309742e4b520STomohiro Misono u64 objectid; 309842e4b520STomohiro Misono int slot; 309942e4b520STomohiro Misono int ret; 310042e4b520STomohiro Misono u8 found; 310142e4b520STomohiro Misono 310242e4b520STomohiro Misono path = btrfs_alloc_path(); 310342e4b520STomohiro Misono if (!path) 310442e4b520STomohiro Misono return -ENOMEM; 310542e4b520STomohiro Misono 310642e4b520STomohiro Misono rootrefs = memdup_user(argp, sizeof(*rootrefs)); 310742e4b520STomohiro Misono if (IS_ERR(rootrefs)) { 310842e4b520STomohiro Misono btrfs_free_path(path); 310942e4b520STomohiro Misono return PTR_ERR(rootrefs); 311042e4b520STomohiro Misono } 311142e4b520STomohiro Misono 31129ad12305SSahil Kang objectid = root->root_key.objectid; 311342e4b520STomohiro Misono key.objectid = objectid; 311442e4b520STomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 311542e4b520STomohiro Misono key.offset = rootrefs->min_treeid; 311642e4b520STomohiro Misono found = 0; 311742e4b520STomohiro Misono 31189ad12305SSahil Kang root = root->fs_info->tree_root; 311942e4b520STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 312042e4b520STomohiro Misono if (ret < 0) { 312142e4b520STomohiro Misono goto out; 312242e4b520STomohiro Misono } else if (path->slots[0] >= 312342e4b520STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 312442e4b520STomohiro Misono ret = btrfs_next_leaf(root, path); 312542e4b520STomohiro Misono if (ret < 0) { 312642e4b520STomohiro Misono goto out; 312742e4b520STomohiro Misono } else if (ret > 0) { 312842e4b520STomohiro Misono ret = -EUCLEAN; 312942e4b520STomohiro Misono goto out; 313042e4b520STomohiro Misono } 313142e4b520STomohiro Misono } 313242e4b520STomohiro Misono while (1) { 313342e4b520STomohiro Misono leaf = path->nodes[0]; 313442e4b520STomohiro Misono slot = path->slots[0]; 313542e4b520STomohiro Misono 313642e4b520STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 313742e4b520STomohiro Misono if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { 313842e4b520STomohiro Misono ret = 0; 313942e4b520STomohiro Misono goto out; 314042e4b520STomohiro Misono } 314142e4b520STomohiro Misono 314242e4b520STomohiro Misono if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { 314342e4b520STomohiro Misono ret = -EOVERFLOW; 314442e4b520STomohiro Misono goto out; 314542e4b520STomohiro Misono } 314642e4b520STomohiro Misono 314742e4b520STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 314842e4b520STomohiro Misono rootrefs->rootref[found].treeid = key.offset; 314942e4b520STomohiro Misono rootrefs->rootref[found].dirid = 315042e4b520STomohiro Misono btrfs_root_ref_dirid(leaf, rref); 315142e4b520STomohiro Misono found++; 315242e4b520STomohiro Misono 315342e4b520STomohiro Misono ret = btrfs_next_item(root, path); 315442e4b520STomohiro Misono if (ret < 0) { 315542e4b520STomohiro Misono goto out; 315642e4b520STomohiro Misono } else if (ret > 0) { 315742e4b520STomohiro Misono ret = -EUCLEAN; 315842e4b520STomohiro Misono goto out; 315942e4b520STomohiro Misono } 316042e4b520STomohiro Misono } 316142e4b520STomohiro Misono 316242e4b520STomohiro Misono out: 316342e4b520STomohiro Misono if (!ret || ret == -EOVERFLOW) { 316442e4b520STomohiro Misono rootrefs->num_items = found; 316542e4b520STomohiro Misono /* update min_treeid for next search */ 316642e4b520STomohiro Misono if (found) 316742e4b520STomohiro Misono rootrefs->min_treeid = 316842e4b520STomohiro Misono rootrefs->rootref[found - 1].treeid + 1; 316942e4b520STomohiro Misono if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) 317042e4b520STomohiro Misono ret = -EFAULT; 317142e4b520STomohiro Misono } 317242e4b520STomohiro Misono 317342e4b520STomohiro Misono kfree(rootrefs); 317442e4b520STomohiro Misono btrfs_free_path(path); 317542e4b520STomohiro Misono 317642e4b520STomohiro Misono return ret; 317742e4b520STomohiro Misono } 317842e4b520STomohiro Misono 317976dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 3180949964c9SMarcos Paulo de Souza void __user *arg, 3181949964c9SMarcos Paulo de Souza bool destroy_v2) 318276dda93cSYan, Zheng { 318354563d41SAl Viro struct dentry *parent = file->f_path.dentry; 31840b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 318576dda93cSYan, Zheng struct dentry *dentry; 31862b0143b5SDavid Howells struct inode *dir = d_inode(parent); 318776dda93cSYan, Zheng struct inode *inode; 318876dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 318976dda93cSYan, Zheng struct btrfs_root *dest = NULL; 3190949964c9SMarcos Paulo de Souza struct btrfs_ioctl_vol_args *vol_args = NULL; 3191949964c9SMarcos Paulo de Souza struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; 3192c4ed533bSChristian Brauner struct user_namespace *mnt_userns = file_mnt_user_ns(file); 3193949964c9SMarcos Paulo de Souza char *subvol_name, *subvol_name_ptr = NULL; 3194949964c9SMarcos Paulo de Souza int subvol_namelen; 319576dda93cSYan, Zheng int err = 0; 3196949964c9SMarcos Paulo de Souza bool destroy_parent = false; 319776dda93cSYan, Zheng 3198813febdbSJosef Bacik /* We don't support snapshots with extent tree v2 yet. */ 3199813febdbSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 3200813febdbSJosef Bacik btrfs_err(fs_info, 3201813febdbSJosef Bacik "extent tree v2 doesn't support snapshot deletion yet"); 3202813febdbSJosef Bacik return -EOPNOTSUPP; 3203813febdbSJosef Bacik } 3204813febdbSJosef Bacik 3205949964c9SMarcos Paulo de Souza if (destroy_v2) { 3206949964c9SMarcos Paulo de Souza vol_args2 = memdup_user(arg, sizeof(*vol_args2)); 3207949964c9SMarcos Paulo de Souza if (IS_ERR(vol_args2)) 3208949964c9SMarcos Paulo de Souza return PTR_ERR(vol_args2); 3209325c50e3SJeff Mahoney 3210949964c9SMarcos Paulo de Souza if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { 3211949964c9SMarcos Paulo de Souza err = -EOPNOTSUPP; 3212949964c9SMarcos Paulo de Souza goto out; 3213949964c9SMarcos Paulo de Souza } 321476dda93cSYan, Zheng 3215949964c9SMarcos Paulo de Souza /* 3216949964c9SMarcos Paulo de Souza * If SPEC_BY_ID is not set, we are looking for the subvolume by 3217949964c9SMarcos Paulo de Souza * name, same as v1 currently does. 3218949964c9SMarcos Paulo de Souza */ 3219949964c9SMarcos Paulo de Souza if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { 3220949964c9SMarcos Paulo de Souza vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; 3221949964c9SMarcos Paulo de Souza subvol_name = vol_args2->name; 3222949964c9SMarcos Paulo de Souza 3223949964c9SMarcos Paulo de Souza err = mnt_want_write_file(file); 3224949964c9SMarcos Paulo de Souza if (err) 3225949964c9SMarcos Paulo de Souza goto out; 3226949964c9SMarcos Paulo de Souza } else { 3227aabb34e7SChristian Brauner struct inode *old_dir; 3228c4ed533bSChristian Brauner 3229949964c9SMarcos Paulo de Souza if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { 323076dda93cSYan, Zheng err = -EINVAL; 323176dda93cSYan, Zheng goto out; 323276dda93cSYan, Zheng } 323376dda93cSYan, Zheng 3234a561be71SAl Viro err = mnt_want_write_file(file); 323576dda93cSYan, Zheng if (err) 323676dda93cSYan, Zheng goto out; 323776dda93cSYan, Zheng 3238949964c9SMarcos Paulo de Souza dentry = btrfs_get_dentry(fs_info->sb, 3239949964c9SMarcos Paulo de Souza BTRFS_FIRST_FREE_OBJECTID, 3240949964c9SMarcos Paulo de Souza vol_args2->subvolid, 0, 0); 3241949964c9SMarcos Paulo de Souza if (IS_ERR(dentry)) { 3242949964c9SMarcos Paulo de Souza err = PTR_ERR(dentry); 3243949964c9SMarcos Paulo de Souza goto out_drop_write; 3244949964c9SMarcos Paulo de Souza } 3245949964c9SMarcos Paulo de Souza 3246949964c9SMarcos Paulo de Souza /* 3247949964c9SMarcos Paulo de Souza * Change the default parent since the subvolume being 3248949964c9SMarcos Paulo de Souza * deleted can be outside of the current mount point. 3249949964c9SMarcos Paulo de Souza */ 3250949964c9SMarcos Paulo de Souza parent = btrfs_get_parent(dentry); 3251949964c9SMarcos Paulo de Souza 3252949964c9SMarcos Paulo de Souza /* 3253949964c9SMarcos Paulo de Souza * At this point dentry->d_name can point to '/' if the 3254949964c9SMarcos Paulo de Souza * subvolume we want to destroy is outsite of the 3255949964c9SMarcos Paulo de Souza * current mount point, so we need to release the 3256949964c9SMarcos Paulo de Souza * current dentry and execute the lookup to return a new 3257949964c9SMarcos Paulo de Souza * one with ->d_name pointing to the 3258949964c9SMarcos Paulo de Souza * <mount point>/subvol_name. 3259949964c9SMarcos Paulo de Souza */ 3260949964c9SMarcos Paulo de Souza dput(dentry); 3261949964c9SMarcos Paulo de Souza if (IS_ERR(parent)) { 3262949964c9SMarcos Paulo de Souza err = PTR_ERR(parent); 3263949964c9SMarcos Paulo de Souza goto out_drop_write; 3264949964c9SMarcos Paulo de Souza } 3265aabb34e7SChristian Brauner old_dir = dir; 3266949964c9SMarcos Paulo de Souza dir = d_inode(parent); 3267949964c9SMarcos Paulo de Souza 3268949964c9SMarcos Paulo de Souza /* 3269949964c9SMarcos Paulo de Souza * If v2 was used with SPEC_BY_ID, a new parent was 3270949964c9SMarcos Paulo de Souza * allocated since the subvolume can be outside of the 3271949964c9SMarcos Paulo de Souza * current mount point. Later on we need to release this 3272949964c9SMarcos Paulo de Souza * new parent dentry. 3273949964c9SMarcos Paulo de Souza */ 3274949964c9SMarcos Paulo de Souza destroy_parent = true; 3275949964c9SMarcos Paulo de Souza 3276aabb34e7SChristian Brauner /* 3277aabb34e7SChristian Brauner * On idmapped mounts, deletion via subvolid is 3278aabb34e7SChristian Brauner * restricted to subvolumes that are immediate 3279aabb34e7SChristian Brauner * ancestors of the inode referenced by the file 3280aabb34e7SChristian Brauner * descriptor in the ioctl. Otherwise the idmapping 3281aabb34e7SChristian Brauner * could potentially be abused to delete subvolumes 3282aabb34e7SChristian Brauner * anywhere in the filesystem the user wouldn't be able 3283aabb34e7SChristian Brauner * to delete without an idmapped mount. 3284aabb34e7SChristian Brauner */ 3285aabb34e7SChristian Brauner if (old_dir != dir && mnt_userns != &init_user_ns) { 3286aabb34e7SChristian Brauner err = -EOPNOTSUPP; 3287aabb34e7SChristian Brauner goto free_parent; 3288aabb34e7SChristian Brauner } 3289aabb34e7SChristian Brauner 3290949964c9SMarcos Paulo de Souza subvol_name_ptr = btrfs_get_subvol_name_from_objectid( 3291949964c9SMarcos Paulo de Souza fs_info, vol_args2->subvolid); 3292949964c9SMarcos Paulo de Souza if (IS_ERR(subvol_name_ptr)) { 3293949964c9SMarcos Paulo de Souza err = PTR_ERR(subvol_name_ptr); 3294949964c9SMarcos Paulo de Souza goto free_parent; 3295949964c9SMarcos Paulo de Souza } 32961a9fd417SDavid Sterba /* subvol_name_ptr is already nul terminated */ 3297949964c9SMarcos Paulo de Souza subvol_name = (char *)kbasename(subvol_name_ptr); 3298949964c9SMarcos Paulo de Souza } 3299949964c9SMarcos Paulo de Souza } else { 3300949964c9SMarcos Paulo de Souza vol_args = memdup_user(arg, sizeof(*vol_args)); 3301949964c9SMarcos Paulo de Souza if (IS_ERR(vol_args)) 3302949964c9SMarcos Paulo de Souza return PTR_ERR(vol_args); 3303949964c9SMarcos Paulo de Souza 3304949964c9SMarcos Paulo de Souza vol_args->name[BTRFS_PATH_NAME_MAX] = 0; 3305949964c9SMarcos Paulo de Souza subvol_name = vol_args->name; 3306949964c9SMarcos Paulo de Souza 3307949964c9SMarcos Paulo de Souza err = mnt_want_write_file(file); 3308949964c9SMarcos Paulo de Souza if (err) 3309949964c9SMarcos Paulo de Souza goto out; 3310949964c9SMarcos Paulo de Souza } 3311949964c9SMarcos Paulo de Souza 3312949964c9SMarcos Paulo de Souza subvol_namelen = strlen(subvol_name); 3313949964c9SMarcos Paulo de Souza 3314949964c9SMarcos Paulo de Souza if (strchr(subvol_name, '/') || 3315949964c9SMarcos Paulo de Souza strncmp(subvol_name, "..", subvol_namelen) == 0) { 3316949964c9SMarcos Paulo de Souza err = -EINVAL; 3317949964c9SMarcos Paulo de Souza goto free_subvol_name; 3318949964c9SMarcos Paulo de Souza } 3319949964c9SMarcos Paulo de Souza 3320949964c9SMarcos Paulo de Souza if (!S_ISDIR(dir->i_mode)) { 3321949964c9SMarcos Paulo de Souza err = -ENOTDIR; 3322949964c9SMarcos Paulo de Souza goto free_subvol_name; 3323949964c9SMarcos Paulo de Souza } 3324521e0546SDavid Sterba 332500235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 332600235411SAl Viro if (err == -EINTR) 3327949964c9SMarcos Paulo de Souza goto free_subvol_name; 3328c4ed533bSChristian Brauner dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen); 332976dda93cSYan, Zheng if (IS_ERR(dentry)) { 333076dda93cSYan, Zheng err = PTR_ERR(dentry); 333176dda93cSYan, Zheng goto out_unlock_dir; 333276dda93cSYan, Zheng } 333376dda93cSYan, Zheng 33342b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 333576dda93cSYan, Zheng err = -ENOENT; 333676dda93cSYan, Zheng goto out_dput; 333776dda93cSYan, Zheng } 333876dda93cSYan, Zheng 33392b0143b5SDavid Howells inode = d_inode(dentry); 33404260f7c7SSage Weil dest = BTRFS_I(inode)->root; 33414260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 33424260f7c7SSage Weil /* 33434260f7c7SSage Weil * Regular user. Only allow this with a special mount 33444260f7c7SSage Weil * option, when the user has write+exec access to the 33454260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 33464260f7c7SSage Weil * allowed. 33474260f7c7SSage Weil * 33484260f7c7SSage Weil * Note that this is _not_ check that the subvol is 33494260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 33504260f7c7SSage Weil * otherwise be able to delete. 33514260f7c7SSage Weil * 33524260f7c7SSage Weil * Users who want to delete empty subvols should try 33534260f7c7SSage Weil * rmdir(2). 33544260f7c7SSage Weil */ 33554260f7c7SSage Weil err = -EPERM; 33560b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 33574260f7c7SSage Weil goto out_dput; 33584260f7c7SSage Weil 33594260f7c7SSage Weil /* 33604260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 33614260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 33624260f7c7SSage Weil * must be called on the dentry referencing the root 33634260f7c7SSage Weil * of the subvol, not a random directory contained 33644260f7c7SSage Weil * within it. 33654260f7c7SSage Weil */ 33664260f7c7SSage Weil err = -EINVAL; 33674260f7c7SSage Weil if (root == dest) 33684260f7c7SSage Weil goto out_dput; 33694260f7c7SSage Weil 3370c4ed533bSChristian Brauner err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC); 33714260f7c7SSage Weil if (err) 33724260f7c7SSage Weil goto out_dput; 33735c39da5bSMiao Xie } 33744260f7c7SSage Weil 33755c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 3376c4ed533bSChristian Brauner err = btrfs_may_delete(mnt_userns, dir, dentry, 1); 33774260f7c7SSage Weil if (err) 33784260f7c7SSage Weil goto out_dput; 33794260f7c7SSage Weil 33804a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 338176dda93cSYan, Zheng err = -EINVAL; 338276dda93cSYan, Zheng goto out_dput; 338376dda93cSYan, Zheng } 338476dda93cSYan, Zheng 338564708539SJosef Bacik btrfs_inode_lock(inode, 0); 3386f60a2364SMisono Tomohiro err = btrfs_delete_subvolume(dir, dentry); 338764708539SJosef Bacik btrfs_inode_unlock(inode, 0); 3388a37d9a17SAmir Goldstein if (!err) 3389a37d9a17SAmir Goldstein d_delete_notify(dir, dentry); 3390fa6ac876SLiu Bo 339176dda93cSYan, Zheng out_dput: 339276dda93cSYan, Zheng dput(dentry); 339376dda93cSYan, Zheng out_unlock_dir: 339464708539SJosef Bacik btrfs_inode_unlock(dir, 0); 3395949964c9SMarcos Paulo de Souza free_subvol_name: 3396949964c9SMarcos Paulo de Souza kfree(subvol_name_ptr); 3397949964c9SMarcos Paulo de Souza free_parent: 3398949964c9SMarcos Paulo de Souza if (destroy_parent) 3399949964c9SMarcos Paulo de Souza dput(parent); 340000235411SAl Viro out_drop_write: 34012a79f17eSAl Viro mnt_drop_write_file(file); 340276dda93cSYan, Zheng out: 3403949964c9SMarcos Paulo de Souza kfree(vol_args2); 340476dda93cSYan, Zheng kfree(vol_args); 340576dda93cSYan, Zheng return err; 340676dda93cSYan, Zheng } 340776dda93cSYan, Zheng 34081e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 3409f46b5a66SChristoph Hellwig { 3410496ad9aaSAl Viro struct inode *inode = file_inode(file); 3411f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3412c853a578SGoldwyn Rodrigues struct btrfs_ioctl_defrag_range_args range = {0}; 3413c146afadSYan Zheng int ret; 3414c146afadSYan Zheng 341525122d15SIlya Dryomov ret = mnt_want_write_file(file); 341625122d15SIlya Dryomov if (ret) 341725122d15SIlya Dryomov return ret; 3418b83cc969SLi Zefan 341925122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 342025122d15SIlya Dryomov ret = -EROFS; 342125122d15SIlya Dryomov goto out; 34225ac00addSStefan Behrens } 3423f46b5a66SChristoph Hellwig 3424f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 3425f46b5a66SChristoph Hellwig case S_IFDIR: 3426e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 3427e441d54dSChris Mason ret = -EPERM; 3428e441d54dSChris Mason goto out; 3429e441d54dSChris Mason } 3430de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 3431f46b5a66SChristoph Hellwig break; 3432f46b5a66SChristoph Hellwig case S_IFREG: 3433616d374eSAdam Borowski /* 3434616d374eSAdam Borowski * Note that this does not check the file descriptor for write 3435616d374eSAdam Borowski * access. This prevents defragmenting executables that are 3436616d374eSAdam Borowski * running and allows defrag on files open in read-only mode. 3437616d374eSAdam Borowski */ 3438616d374eSAdam Borowski if (!capable(CAP_SYS_ADMIN) && 343947291baaSChristian Brauner inode_permission(&init_user_ns, inode, MAY_WRITE)) { 3440616d374eSAdam Borowski ret = -EPERM; 3441e441d54dSChris Mason goto out; 3442e441d54dSChris Mason } 34431e701a32SChris Mason 34441e701a32SChris Mason if (argp) { 3445c853a578SGoldwyn Rodrigues if (copy_from_user(&range, argp, sizeof(range))) { 34461e701a32SChris Mason ret = -EFAULT; 3447683be16eSDan Carpenter goto out; 34481e701a32SChris Mason } 34491e701a32SChris Mason /* compression requires us to start the IO */ 3450c853a578SGoldwyn Rodrigues if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 3451c853a578SGoldwyn Rodrigues range.flags |= BTRFS_DEFRAG_RANGE_START_IO; 3452c853a578SGoldwyn Rodrigues range.extent_thresh = (u32)-1; 34531e701a32SChris Mason } 34541e701a32SChris Mason } else { 34551e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 3456c853a578SGoldwyn Rodrigues range.len = (u64)-1; 34571e701a32SChris Mason } 34581ccc2e8aSQu Wenruo ret = btrfs_defrag_file(file_inode(file), &file->f_ra, 3459c853a578SGoldwyn Rodrigues &range, BTRFS_OLDEST_GENERATION, 0); 34604cb5300bSChris Mason if (ret > 0) 34614cb5300bSChris Mason ret = 0; 3462f46b5a66SChristoph Hellwig break; 34638929ecfaSYan, Zheng default: 34648929ecfaSYan, Zheng ret = -EINVAL; 3465f46b5a66SChristoph Hellwig } 3466e441d54dSChris Mason out: 346725122d15SIlya Dryomov mnt_drop_write_file(file); 3468e441d54dSChris Mason return ret; 3469f46b5a66SChristoph Hellwig } 3470f46b5a66SChristoph Hellwig 34712ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 3472f46b5a66SChristoph Hellwig { 3473f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3474a174c0a2SNikolay Borisov bool restore_op = false; 3475f46b5a66SChristoph Hellwig int ret; 3476f46b5a66SChristoph Hellwig 3477e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3478e441d54dSChris Mason return -EPERM; 3479e441d54dSChris Mason 3480914a519bSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 3481914a519bSJosef Bacik btrfs_err(fs_info, "device add not supported on extent tree v2 yet"); 3482914a519bSJosef Bacik return -EINVAL; 3483914a519bSJosef Bacik } 3484914a519bSJosef Bacik 3485a174c0a2SNikolay Borisov if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) { 3486a174c0a2SNikolay Borisov if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD)) 3487e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3488c9e9f97bSIlya Dryomov 3489a174c0a2SNikolay Borisov /* 3490a174c0a2SNikolay Borisov * We can do the device add because we have a paused balanced, 3491a174c0a2SNikolay Borisov * change the exclusive op type and remember we should bring 3492a174c0a2SNikolay Borisov * back the paused balance 3493a174c0a2SNikolay Borisov */ 3494a174c0a2SNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD; 3495a174c0a2SNikolay Borisov btrfs_exclop_start_unlock(fs_info); 3496a174c0a2SNikolay Borisov restore_op = true; 3497a174c0a2SNikolay Borisov } 3498a174c0a2SNikolay Borisov 3499dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3500c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3501c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3502c9e9f97bSIlya Dryomov goto out; 3503c9e9f97bSIlya Dryomov } 3504f46b5a66SChristoph Hellwig 35055516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 35062ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 3507f46b5a66SChristoph Hellwig 350843d20761SAnand Jain if (!ret) 35090b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 351043d20761SAnand Jain 3511f46b5a66SChristoph Hellwig kfree(vol_args); 3512c9e9f97bSIlya Dryomov out: 3513a174c0a2SNikolay Borisov if (restore_op) 3514a174c0a2SNikolay Borisov btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED); 3515a174c0a2SNikolay Borisov else 3516c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 3517f46b5a66SChristoph Hellwig return ret; 3518f46b5a66SChristoph Hellwig } 3519f46b5a66SChristoph Hellwig 35206b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 3521f46b5a66SChristoph Hellwig { 35221a15eb72SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 35230b246afaSJeff Mahoney struct inode *inode = file_inode(file); 35240b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 35256b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 35263fa421deSJosef Bacik struct block_device *bdev = NULL; 35273fa421deSJosef Bacik fmode_t mode; 3528f46b5a66SChristoph Hellwig int ret; 352967ae34b6SDavid Sterba bool cancel = false; 3530f46b5a66SChristoph Hellwig 3531e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3532e441d54dSChris Mason return -EPERM; 3533e441d54dSChris Mason 3534dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3535d815b3f2SDan Carpenter if (IS_ERR(vol_args)) 3536d815b3f2SDan Carpenter return PTR_ERR(vol_args); 3537f46b5a66SChristoph Hellwig 3538748449cdSDavid Sterba if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { 3539fd4e994bSOmar Sandoval ret = -EOPNOTSUPP; 3540fd4e994bSOmar Sandoval goto out; 3541fd4e994bSOmar Sandoval } 35421a15eb72SJosef Bacik 35436b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 35441a15eb72SJosef Bacik if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 35451a15eb72SJosef Bacik args.devid = vol_args->devid; 35461a15eb72SJosef Bacik } else if (!strcmp("cancel", vol_args->name)) { 354767ae34b6SDavid Sterba cancel = true; 35481a15eb72SJosef Bacik } else { 35491a15eb72SJosef Bacik ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); 35501a15eb72SJosef Bacik if (ret) 35511a15eb72SJosef Bacik goto out; 35521a15eb72SJosef Bacik } 35531a15eb72SJosef Bacik 35541a15eb72SJosef Bacik ret = mnt_want_write_file(file); 35551a15eb72SJosef Bacik if (ret) 35561a15eb72SJosef Bacik goto out; 355767ae34b6SDavid Sterba 355867ae34b6SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, 355967ae34b6SDavid Sterba cancel); 356067ae34b6SDavid Sterba if (ret) 35611a15eb72SJosef Bacik goto err_drop; 356267ae34b6SDavid Sterba 35631a15eb72SJosef Bacik /* Exclusive operation is now claimed */ 35641a15eb72SJosef Bacik ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); 356567ae34b6SDavid Sterba 3566c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 3567183860f6SAnand Jain 35686b526ed7SAnand Jain if (!ret) { 3569735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 35700b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 35716b526ed7SAnand Jain vol_args->devid); 35726b526ed7SAnand Jain else 35730b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 35746b526ed7SAnand Jain vol_args->name); 35756b526ed7SAnand Jain } 3576c47ca32dSDan Carpenter err_drop: 35774ac20c70SIlya Dryomov mnt_drop_write_file(file); 35783fa421deSJosef Bacik if (bdev) 35793fa421deSJosef Bacik blkdev_put(bdev, mode); 35801a15eb72SJosef Bacik out: 35811a15eb72SJosef Bacik btrfs_put_dev_args_from_path(&args); 35821a15eb72SJosef Bacik kfree(vol_args); 3583f46b5a66SChristoph Hellwig return ret; 3584f46b5a66SChristoph Hellwig } 3585f46b5a66SChristoph Hellwig 3586f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 3587f46b5a66SChristoph Hellwig { 35881a15eb72SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 35890b246afaSJeff Mahoney struct inode *inode = file_inode(file); 35900b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3591f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 35923fa421deSJosef Bacik struct block_device *bdev = NULL; 35933fa421deSJosef Bacik fmode_t mode; 3594f46b5a66SChristoph Hellwig int ret; 359537b45995STom Rix bool cancel = false; 3596f46b5a66SChristoph Hellwig 3597f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3598f46b5a66SChristoph Hellwig return -EPERM; 3599f46b5a66SChristoph Hellwig 36001a15eb72SJosef Bacik vol_args = memdup_user(arg, sizeof(*vol_args)); 36011a15eb72SJosef Bacik if (IS_ERR(vol_args)) 36021a15eb72SJosef Bacik return PTR_ERR(vol_args); 36031a15eb72SJosef Bacik 36041a15eb72SJosef Bacik vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 36051a15eb72SJosef Bacik if (!strcmp("cancel", vol_args->name)) { 36061a15eb72SJosef Bacik cancel = true; 36071a15eb72SJosef Bacik } else { 36081a15eb72SJosef Bacik ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); 36091a15eb72SJosef Bacik if (ret) 36101a15eb72SJosef Bacik goto out; 36111a15eb72SJosef Bacik } 36121a15eb72SJosef Bacik 3613f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 3614f46b5a66SChristoph Hellwig if (ret) 36151a15eb72SJosef Bacik goto out; 3616f46b5a66SChristoph Hellwig 361767ae34b6SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, 361867ae34b6SDavid Sterba cancel); 361967ae34b6SDavid Sterba if (ret == 0) { 36201a15eb72SJosef Bacik ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); 3621f46b5a66SChristoph Hellwig if (!ret) 36220b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 3623c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 362467ae34b6SDavid Sterba } 362567ae34b6SDavid Sterba 3626f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 36273fa421deSJosef Bacik if (bdev) 36283fa421deSJosef Bacik blkdev_put(bdev, mode); 36291a15eb72SJosef Bacik out: 36301a15eb72SJosef Bacik btrfs_put_dev_args_from_path(&args); 36311a15eb72SJosef Bacik kfree(vol_args); 3632f46b5a66SChristoph Hellwig return ret; 3633f46b5a66SChristoph Hellwig } 3634f46b5a66SChristoph Hellwig 36352ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 36362ff7e61eSJeff Mahoney void __user *arg) 3637475f6387SJan Schmidt { 3638027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 3639475f6387SJan Schmidt struct btrfs_device *device; 36400b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3641137c5418SJohannes Thumshirn u64 flags_in; 3642027ed2f0SLi Zefan int ret = 0; 3643475f6387SJan Schmidt 3644137c5418SJohannes Thumshirn fi_args = memdup_user(arg, sizeof(*fi_args)); 3645137c5418SJohannes Thumshirn if (IS_ERR(fi_args)) 3646137c5418SJohannes Thumshirn return PTR_ERR(fi_args); 3647137c5418SJohannes Thumshirn 3648137c5418SJohannes Thumshirn flags_in = fi_args->flags; 3649137c5418SJohannes Thumshirn memset(fi_args, 0, sizeof(*fi_args)); 3650027ed2f0SLi Zefan 3651d03262c7SDavid Sterba rcu_read_lock(); 3652027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 3653475f6387SJan Schmidt 3654d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 3655027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 3656027ed2f0SLi Zefan fi_args->max_id = device->devid; 3657475f6387SJan Schmidt } 3658d03262c7SDavid Sterba rcu_read_unlock(); 3659475f6387SJan Schmidt 3660de37aa51SNikolay Borisov memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); 3661bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 3662bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 3663bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 366480a773fbSDavid Sterba 3665137c5418SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) { 3666137c5418SJohannes Thumshirn fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy); 3667137c5418SJohannes Thumshirn fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy); 3668137c5418SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO; 3669137c5418SJohannes Thumshirn } 3670137c5418SJohannes Thumshirn 36710fb408a5SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) { 36720fb408a5SJohannes Thumshirn fi_args->generation = fs_info->generation; 36730fb408a5SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION; 36740fb408a5SJohannes Thumshirn } 36750fb408a5SJohannes Thumshirn 367649bac897SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) { 367749bac897SJohannes Thumshirn memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid, 367849bac897SJohannes Thumshirn sizeof(fi_args->metadata_uuid)); 367949bac897SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID; 368049bac897SJohannes Thumshirn } 368149bac897SJohannes Thumshirn 3682027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 3683027ed2f0SLi Zefan ret = -EFAULT; 3684475f6387SJan Schmidt 3685027ed2f0SLi Zefan kfree(fi_args); 3686027ed2f0SLi Zefan return ret; 3687475f6387SJan Schmidt } 3688475f6387SJan Schmidt 36892ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 36902ff7e61eSJeff Mahoney void __user *arg) 3691475f6387SJan Schmidt { 3692562d7b15SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 3693475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 3694475f6387SJan Schmidt struct btrfs_device *dev; 3695475f6387SJan Schmidt int ret = 0; 3696475f6387SJan Schmidt 3697475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 3698475f6387SJan Schmidt if (IS_ERR(di_args)) 3699475f6387SJan Schmidt return PTR_ERR(di_args); 3700475f6387SJan Schmidt 3701562d7b15SJosef Bacik args.devid = di_args->devid; 3702dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 3703562d7b15SJosef Bacik args.uuid = di_args->uuid; 3704475f6387SJan Schmidt 3705c5593ca3SDavid Sterba rcu_read_lock(); 3706562d7b15SJosef Bacik dev = btrfs_find_device(fs_info->fs_devices, &args); 3707475f6387SJan Schmidt if (!dev) { 3708475f6387SJan Schmidt ret = -ENODEV; 3709475f6387SJan Schmidt goto out; 3710475f6387SJan Schmidt } 3711475f6387SJan Schmidt 3712475f6387SJan Schmidt di_args->devid = dev->devid; 37137cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 37147cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 3715475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 3716a27202fbSJim Meyering if (dev->name) { 3717672d5990SMisono Tomohiro strncpy(di_args->path, rcu_str_deref(dev->name), 3718672d5990SMisono Tomohiro sizeof(di_args->path) - 1); 3719a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 3720a27202fbSJim Meyering } else { 372199ba55adSStefan Behrens di_args->path[0] = '\0'; 3722a27202fbSJim Meyering } 3723475f6387SJan Schmidt 3724475f6387SJan Schmidt out: 3725c5593ca3SDavid Sterba rcu_read_unlock(); 3726475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 3727475f6387SJan Schmidt ret = -EFAULT; 3728475f6387SJan Schmidt 3729475f6387SJan Schmidt kfree(di_args); 3730475f6387SJan Schmidt return ret; 3731475f6387SJan Schmidt } 3732475f6387SJan Schmidt 37336ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 37346ef5ed0dSJosef Bacik { 3735496ad9aaSAl Viro struct inode *inode = file_inode(file); 37360b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 37376ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 37386ef5ed0dSJosef Bacik struct btrfs_root *new_root; 37396ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 37406ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 37412a2b5d62SJosef Bacik struct btrfs_path *path = NULL; 37426ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 37436ef5ed0dSJosef Bacik u64 objectid = 0; 37446ef5ed0dSJosef Bacik u64 dir_id; 37453c04ce01SMiao Xie int ret; 37466ef5ed0dSJosef Bacik 37476ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 37486ef5ed0dSJosef Bacik return -EPERM; 37496ef5ed0dSJosef Bacik 37503c04ce01SMiao Xie ret = mnt_want_write_file(file); 37513c04ce01SMiao Xie if (ret) 37523c04ce01SMiao Xie return ret; 37533c04ce01SMiao Xie 37543c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 37553c04ce01SMiao Xie ret = -EFAULT; 37563c04ce01SMiao Xie goto out; 37573c04ce01SMiao Xie } 37586ef5ed0dSJosef Bacik 37596ef5ed0dSJosef Bacik if (!objectid) 37601cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 37616ef5ed0dSJosef Bacik 376256e9357aSDavid Sterba new_root = btrfs_get_fs_root(fs_info, objectid, true); 37633c04ce01SMiao Xie if (IS_ERR(new_root)) { 37643c04ce01SMiao Xie ret = PTR_ERR(new_root); 37653c04ce01SMiao Xie goto out; 37663c04ce01SMiao Xie } 37672a2b5d62SJosef Bacik if (!is_fstree(new_root->root_key.objectid)) { 37682a2b5d62SJosef Bacik ret = -ENOENT; 37692a2b5d62SJosef Bacik goto out_free; 37702a2b5d62SJosef Bacik } 37716ef5ed0dSJosef Bacik 37726ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 37733c04ce01SMiao Xie if (!path) { 37743c04ce01SMiao Xie ret = -ENOMEM; 37752a2b5d62SJosef Bacik goto out_free; 37763c04ce01SMiao Xie } 37776ef5ed0dSJosef Bacik 37786ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 377998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 37803c04ce01SMiao Xie ret = PTR_ERR(trans); 37812a2b5d62SJosef Bacik goto out_free; 37826ef5ed0dSJosef Bacik } 37836ef5ed0dSJosef Bacik 37840b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 37850b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 37866ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3787cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 37882a2b5d62SJosef Bacik btrfs_release_path(path); 37893a45bb20SJeff Mahoney btrfs_end_transaction(trans); 37900b246afaSJeff Mahoney btrfs_err(fs_info, 37915d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 37923c04ce01SMiao Xie ret = -ENOENT; 37932a2b5d62SJosef Bacik goto out_free; 37946ef5ed0dSJosef Bacik } 37956ef5ed0dSJosef Bacik 37966ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 37976ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 37986ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 37992a2b5d62SJosef Bacik btrfs_release_path(path); 38006ef5ed0dSJosef Bacik 38010b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 38023a45bb20SJeff Mahoney btrfs_end_transaction(trans); 38032a2b5d62SJosef Bacik out_free: 380400246528SJosef Bacik btrfs_put_root(new_root); 38052a2b5d62SJosef Bacik btrfs_free_path(path); 38063c04ce01SMiao Xie out: 38073c04ce01SMiao Xie mnt_drop_write_file(file); 38083c04ce01SMiao Xie return ret; 38096ef5ed0dSJosef Bacik } 38106ef5ed0dSJosef Bacik 3811c065f5b1SSu Yue static void get_block_group_info(struct list_head *groups_list, 3812bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3813bf5fc093SJosef Bacik { 381432da5386SDavid Sterba struct btrfs_block_group *block_group; 3815bf5fc093SJosef Bacik 3816bf5fc093SJosef Bacik space->total_bytes = 0; 3817bf5fc093SJosef Bacik space->used_bytes = 0; 3818bf5fc093SJosef Bacik space->flags = 0; 3819bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3820bf5fc093SJosef Bacik space->flags = block_group->flags; 3821b3470b5dSDavid Sterba space->total_bytes += block_group->length; 3822bf38be65SDavid Sterba space->used_bytes += block_group->used; 3823bf5fc093SJosef Bacik } 3824bf5fc093SJosef Bacik } 3825bf5fc093SJosef Bacik 38262ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 38272ff7e61eSJeff Mahoney void __user *arg) 38281406e432SJosef Bacik { 38291406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 38301406e432SJosef Bacik struct btrfs_ioctl_space_info space; 38311406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 38327fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 383313f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 38341406e432SJosef Bacik struct btrfs_space_info *info; 3835315d8e98SColin Ian King static const u64 types[] = { 3836315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 3837bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3838bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3839315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 3840315d8e98SColin Ian King }; 3841bf5fc093SJosef Bacik int num_types = 4; 38427fde62bfSChris Mason int alloc_size; 38431406e432SJosef Bacik int ret = 0; 384451788b1bSDan Rosenberg u64 slot_count = 0; 3845bf5fc093SJosef Bacik int i, c; 38461406e432SJosef Bacik 38471406e432SJosef Bacik if (copy_from_user(&space_args, 38481406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 38491406e432SJosef Bacik sizeof(space_args))) 38501406e432SJosef Bacik return -EFAULT; 38511406e432SJosef Bacik 3852bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3853bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3854bf5fc093SJosef Bacik 3855bf5fc093SJosef Bacik info = NULL; 385672804905SJosef Bacik list_for_each_entry(tmp, &fs_info->space_info, list) { 3857bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3858bf5fc093SJosef Bacik info = tmp; 3859bf5fc093SJosef Bacik break; 3860bf5fc093SJosef Bacik } 3861bf5fc093SJosef Bacik } 38621406e432SJosef Bacik 3863bf5fc093SJosef Bacik if (!info) 3864bf5fc093SJosef Bacik continue; 3865bf5fc093SJosef Bacik 3866bf5fc093SJosef Bacik down_read(&info->groups_sem); 3867bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3868bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3869bf5fc093SJosef Bacik slot_count++; 3870bf5fc093SJosef Bacik } 3871bf5fc093SJosef Bacik up_read(&info->groups_sem); 3872bf5fc093SJosef Bacik } 3873bf5fc093SJosef Bacik 387436523e95SDavid Sterba /* 387536523e95SDavid Sterba * Global block reserve, exported as a space_info 387636523e95SDavid Sterba */ 387736523e95SDavid Sterba slot_count++; 387836523e95SDavid Sterba 38797fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 38807fde62bfSChris Mason if (space_args.space_slots == 0) { 38817fde62bfSChris Mason space_args.total_spaces = slot_count; 38827fde62bfSChris Mason goto out; 38837fde62bfSChris Mason } 3884bf5fc093SJosef Bacik 388551788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3886bf5fc093SJosef Bacik 38877fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3888bf5fc093SJosef Bacik 38897fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 38907fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 38917fde62bfSChris Mason */ 389209cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 38937fde62bfSChris Mason return -ENOMEM; 38947fde62bfSChris Mason 38957fde62bfSChris Mason space_args.total_spaces = 0; 38968d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 38977fde62bfSChris Mason if (!dest) 38987fde62bfSChris Mason return -ENOMEM; 38997fde62bfSChris Mason dest_orig = dest; 39007fde62bfSChris Mason 39017fde62bfSChris Mason /* now we have a buffer to copy into */ 3902bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3903bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3904bf5fc093SJosef Bacik 390551788b1bSDan Rosenberg if (!slot_count) 390651788b1bSDan Rosenberg break; 390751788b1bSDan Rosenberg 3908bf5fc093SJosef Bacik info = NULL; 390972804905SJosef Bacik list_for_each_entry(tmp, &fs_info->space_info, list) { 3910bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3911bf5fc093SJosef Bacik info = tmp; 39127fde62bfSChris Mason break; 3913bf5fc093SJosef Bacik } 3914bf5fc093SJosef Bacik } 39157fde62bfSChris Mason 3916bf5fc093SJosef Bacik if (!info) 3917bf5fc093SJosef Bacik continue; 3918bf5fc093SJosef Bacik down_read(&info->groups_sem); 3919bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3920bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 3921c065f5b1SSu Yue get_block_group_info(&info->block_groups[c], 3922c065f5b1SSu Yue &space); 39237fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 39241406e432SJosef Bacik dest++; 39251406e432SJosef Bacik space_args.total_spaces++; 392651788b1bSDan Rosenberg slot_count--; 39271406e432SJosef Bacik } 392851788b1bSDan Rosenberg if (!slot_count) 392951788b1bSDan Rosenberg break; 3930bf5fc093SJosef Bacik } 3931bf5fc093SJosef Bacik up_read(&info->groups_sem); 3932bf5fc093SJosef Bacik } 39331406e432SJosef Bacik 393436523e95SDavid Sterba /* 393536523e95SDavid Sterba * Add global block reserve 393636523e95SDavid Sterba */ 393736523e95SDavid Sterba if (slot_count) { 39380b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 393936523e95SDavid Sterba 394036523e95SDavid Sterba spin_lock(&block_rsv->lock); 394136523e95SDavid Sterba space.total_bytes = block_rsv->size; 394236523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 394336523e95SDavid Sterba spin_unlock(&block_rsv->lock); 394436523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 394536523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 394636523e95SDavid Sterba space_args.total_spaces++; 394736523e95SDavid Sterba } 394836523e95SDavid Sterba 39492eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 39507fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 39517fde62bfSChris Mason 39527fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 39537fde62bfSChris Mason ret = -EFAULT; 39547fde62bfSChris Mason 39557fde62bfSChris Mason kfree(dest_orig); 39567fde62bfSChris Mason out: 39577fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 39581406e432SJosef Bacik ret = -EFAULT; 39591406e432SJosef Bacik 39601406e432SJosef Bacik return ret; 39611406e432SJosef Bacik } 39621406e432SJosef Bacik 39639a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 39649a8c28beSMiao Xie void __user *argp) 396546204592SSage Weil { 396646204592SSage Weil struct btrfs_trans_handle *trans; 396746204592SSage Weil u64 transid; 396846204592SSage Weil 3969d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3970ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3971ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 397298d5dc13STsutomu Itoh return PTR_ERR(trans); 3973ff7c1d33SMiao Xie 3974ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3975ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3976ff7c1d33SMiao Xie goto out; 3977ff7c1d33SMiao Xie } 397846204592SSage Weil transid = trans->transid; 3979fdfbf020SJosef Bacik btrfs_commit_transaction_async(trans); 3980ff7c1d33SMiao Xie out: 398146204592SSage Weil if (argp) 398246204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 398346204592SSage Weil return -EFAULT; 398446204592SSage Weil return 0; 398546204592SSage Weil } 398646204592SSage Weil 39872ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 39889a8c28beSMiao Xie void __user *argp) 398946204592SSage Weil { 399046204592SSage Weil u64 transid; 399146204592SSage Weil 399246204592SSage Weil if (argp) { 399346204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 399446204592SSage Weil return -EFAULT; 399546204592SSage Weil } else { 399646204592SSage Weil transid = 0; /* current trans */ 399746204592SSage Weil } 39982ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 399946204592SSage Weil } 400046204592SSage Weil 4001b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4002475f6387SJan Schmidt { 40030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4004475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4005b8e95489SMiao Xie int ret; 4006475f6387SJan Schmidt 4007475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4008475f6387SJan Schmidt return -EPERM; 4009475f6387SJan Schmidt 4010da32c6d5SJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 4011da32c6d5SJosef Bacik btrfs_err(fs_info, "scrub is not supported on extent tree v2 yet"); 4012da32c6d5SJosef Bacik return -EINVAL; 4013da32c6d5SJosef Bacik } 4014da32c6d5SJosef Bacik 4015475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4016475f6387SJan Schmidt if (IS_ERR(sa)) 4017475f6387SJan Schmidt return PTR_ERR(sa); 4018475f6387SJan Schmidt 4019b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4020b8e95489SMiao Xie ret = mnt_want_write_file(file); 4021b8e95489SMiao Xie if (ret) 4022b8e95489SMiao Xie goto out; 4023b8e95489SMiao Xie } 4024b8e95489SMiao Xie 40250b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 402663a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 402763a212abSStefan Behrens 0); 4028475f6387SJan Schmidt 40295afe6ce7SFilipe Manana /* 40305afe6ce7SFilipe Manana * Copy scrub args to user space even if btrfs_scrub_dev() returned an 40315afe6ce7SFilipe Manana * error. This is important as it allows user space to know how much 40325afe6ce7SFilipe Manana * progress scrub has done. For example, if scrub is canceled we get 40335afe6ce7SFilipe Manana * -ECANCELED from btrfs_scrub_dev() and return that error back to user 40345afe6ce7SFilipe Manana * space. Later user space can inspect the progress from the structure 40355afe6ce7SFilipe Manana * btrfs_ioctl_scrub_args and resume scrub from where it left off 40365afe6ce7SFilipe Manana * previously (btrfs-progs does this). 40375afe6ce7SFilipe Manana * If we fail to copy the btrfs_ioctl_scrub_args structure to user space 40385afe6ce7SFilipe Manana * then return -EFAULT to signal the structure was not copied or it may 40395afe6ce7SFilipe Manana * be corrupt and unreliable due to a partial copy. 40405afe6ce7SFilipe Manana */ 40415afe6ce7SFilipe Manana if (copy_to_user(arg, sa, sizeof(*sa))) 4042475f6387SJan Schmidt ret = -EFAULT; 4043475f6387SJan Schmidt 4044b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4045b8e95489SMiao Xie mnt_drop_write_file(file); 4046b8e95489SMiao Xie out: 4047475f6387SJan Schmidt kfree(sa); 4048475f6387SJan Schmidt return ret; 4049475f6387SJan Schmidt } 4050475f6387SJan Schmidt 40512ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4052475f6387SJan Schmidt { 4053475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4054475f6387SJan Schmidt return -EPERM; 4055475f6387SJan Schmidt 40562ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4057475f6387SJan Schmidt } 4058475f6387SJan Schmidt 40592ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4060475f6387SJan Schmidt void __user *arg) 4061475f6387SJan Schmidt { 4062475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4063475f6387SJan Schmidt int ret; 4064475f6387SJan Schmidt 4065475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4066475f6387SJan Schmidt return -EPERM; 4067475f6387SJan Schmidt 4068475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4069475f6387SJan Schmidt if (IS_ERR(sa)) 4070475f6387SJan Schmidt return PTR_ERR(sa); 4071475f6387SJan Schmidt 40722ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4073475f6387SJan Schmidt 40744fa99b00SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4075475f6387SJan Schmidt ret = -EFAULT; 4076475f6387SJan Schmidt 4077475f6387SJan Schmidt kfree(sa); 4078475f6387SJan Schmidt return ret; 4079475f6387SJan Schmidt } 4080475f6387SJan Schmidt 40812ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4082b27f7c0cSDavid Sterba void __user *arg) 4083c11d2c23SStefan Behrens { 4084c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4085c11d2c23SStefan Behrens int ret; 4086c11d2c23SStefan Behrens 4087c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4088c11d2c23SStefan Behrens if (IS_ERR(sa)) 4089c11d2c23SStefan Behrens return PTR_ERR(sa); 4090c11d2c23SStefan Behrens 4091b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4092b27f7c0cSDavid Sterba kfree(sa); 4093b27f7c0cSDavid Sterba return -EPERM; 4094b27f7c0cSDavid Sterba } 4095b27f7c0cSDavid Sterba 40962ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4097c11d2c23SStefan Behrens 4098eee99577SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4099c11d2c23SStefan Behrens ret = -EFAULT; 4100c11d2c23SStefan Behrens 4101c11d2c23SStefan Behrens kfree(sa); 4102c11d2c23SStefan Behrens return ret; 4103c11d2c23SStefan Behrens } 4104c11d2c23SStefan Behrens 41052ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 41062ff7e61eSJeff Mahoney void __user *arg) 41073f6bcfbdSStefan Behrens { 41083f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 41093f6bcfbdSStefan Behrens int ret; 41103f6bcfbdSStefan Behrens 41113f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 41123f6bcfbdSStefan Behrens return -EPERM; 41133f6bcfbdSStefan Behrens 4114914a519bSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 4115914a519bSJosef Bacik btrfs_err(fs_info, "device replace not supported on extent tree v2 yet"); 4116914a519bSJosef Bacik return -EINVAL; 4117914a519bSJosef Bacik } 4118914a519bSJosef Bacik 41193f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 41203f6bcfbdSStefan Behrens if (IS_ERR(p)) 41213f6bcfbdSStefan Behrens return PTR_ERR(p); 41223f6bcfbdSStefan Behrens 41233f6bcfbdSStefan Behrens switch (p->cmd) { 41243f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4125bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4126adfa97cbSIlya Dryomov ret = -EROFS; 4127adfa97cbSIlya Dryomov goto out; 4128adfa97cbSIlya Dryomov } 4129c3e1f96cSGoldwyn Rodrigues if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) { 4130e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 41313f6bcfbdSStefan Behrens } else { 41322ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4133c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 41343f6bcfbdSStefan Behrens } 41353f6bcfbdSStefan Behrens break; 41363f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 41370b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 41383f6bcfbdSStefan Behrens ret = 0; 41393f6bcfbdSStefan Behrens break; 41403f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 414117d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 414297282031SAnand Jain ret = 0; 41433f6bcfbdSStefan Behrens break; 41443f6bcfbdSStefan Behrens default: 41453f6bcfbdSStefan Behrens ret = -EINVAL; 41463f6bcfbdSStefan Behrens break; 41473f6bcfbdSStefan Behrens } 41483f6bcfbdSStefan Behrens 4149d3a53286SFilipe Manana if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) 41503f6bcfbdSStefan Behrens ret = -EFAULT; 4151adfa97cbSIlya Dryomov out: 41523f6bcfbdSStefan Behrens kfree(p); 41533f6bcfbdSStefan Behrens return ret; 41543f6bcfbdSStefan Behrens } 41553f6bcfbdSStefan Behrens 4156d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4157d7728c96SJan Schmidt { 4158d7728c96SJan Schmidt int ret = 0; 4159d7728c96SJan Schmidt int i; 4160740c3d22SChris Mason u64 rel_ptr; 4161d7728c96SJan Schmidt int size; 4162806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4163d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4164d7728c96SJan Schmidt struct btrfs_path *path; 4165d7728c96SJan Schmidt 416682b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4167d7728c96SJan Schmidt return -EPERM; 4168d7728c96SJan Schmidt 4169d7728c96SJan Schmidt path = btrfs_alloc_path(); 4170d7728c96SJan Schmidt if (!path) { 4171d7728c96SJan Schmidt ret = -ENOMEM; 4172d7728c96SJan Schmidt goto out; 4173d7728c96SJan Schmidt } 4174d7728c96SJan Schmidt 4175d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4176d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4177d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4178d7728c96SJan Schmidt ipa = NULL; 4179d7728c96SJan Schmidt goto out; 4180d7728c96SJan Schmidt } 4181d7728c96SJan Schmidt 4182d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4183d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4184d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4185d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4186d7728c96SJan Schmidt ipath = NULL; 4187d7728c96SJan Schmidt goto out; 4188d7728c96SJan Schmidt } 4189d7728c96SJan Schmidt 4190d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4191d7728c96SJan Schmidt if (ret < 0) 4192d7728c96SJan Schmidt goto out; 4193d7728c96SJan Schmidt 4194d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4195745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4196745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4197740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4198d7728c96SJan Schmidt } 4199d7728c96SJan Schmidt 4200718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4201718dc5faSOmar Sandoval ipath->fspath, size); 4202d7728c96SJan Schmidt if (ret) { 4203d7728c96SJan Schmidt ret = -EFAULT; 4204d7728c96SJan Schmidt goto out; 4205d7728c96SJan Schmidt } 4206d7728c96SJan Schmidt 4207d7728c96SJan Schmidt out: 4208d7728c96SJan Schmidt btrfs_free_path(path); 4209d7728c96SJan Schmidt free_ipath(ipath); 4210d7728c96SJan Schmidt kfree(ipa); 4211d7728c96SJan Schmidt 4212d7728c96SJan Schmidt return ret; 4213d7728c96SJan Schmidt } 4214d7728c96SJan Schmidt 4215d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4216d7728c96SJan Schmidt { 4217d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4218d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4219d7728c96SJan Schmidt 4220d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4221d7728c96SJan Schmidt inodes->bytes_left -= c; 4222d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4223d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4224d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4225d7728c96SJan Schmidt inodes->elem_cnt += 3; 4226d7728c96SJan Schmidt } else { 4227d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4228d7728c96SJan Schmidt inodes->bytes_left = 0; 4229d7728c96SJan Schmidt inodes->elem_missed += 3; 4230d7728c96SJan Schmidt } 4231d7728c96SJan Schmidt 4232d7728c96SJan Schmidt return 0; 4233d7728c96SJan Schmidt } 4234d7728c96SJan Schmidt 42352ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4236d24a67b2SZygo Blaxell void __user *arg, int version) 4237d7728c96SJan Schmidt { 4238d7728c96SJan Schmidt int ret = 0; 4239d7728c96SJan Schmidt int size; 4240d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4241d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4242d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4243d24a67b2SZygo Blaxell bool ignore_offset; 4244d7728c96SJan Schmidt 4245d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4246d7728c96SJan Schmidt return -EPERM; 4247d7728c96SJan Schmidt 4248d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 42497b9ea627SShailendra Verma if (IS_ERR(loi)) 42507b9ea627SShailendra Verma return PTR_ERR(loi); 4251d7728c96SJan Schmidt 4252d24a67b2SZygo Blaxell if (version == 1) { 4253d24a67b2SZygo Blaxell ignore_offset = false; 4254b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4255d24a67b2SZygo Blaxell } else { 4256d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4257d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4258d24a67b2SZygo Blaxell ret = -EINVAL; 4259d24a67b2SZygo Blaxell goto out_loi; 4260d24a67b2SZygo Blaxell } 4261d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4262d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4263d24a67b2SZygo Blaxell ret = -EINVAL; 4264d24a67b2SZygo Blaxell goto out_loi; 4265d24a67b2SZygo Blaxell } 4266d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4267b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4268d24a67b2SZygo Blaxell } 4269d24a67b2SZygo Blaxell 4270d7728c96SJan Schmidt path = btrfs_alloc_path(); 4271d7728c96SJan Schmidt if (!path) { 4272d7728c96SJan Schmidt ret = -ENOMEM; 4273d7728c96SJan Schmidt goto out; 4274d7728c96SJan Schmidt } 4275d7728c96SJan Schmidt 4276d7728c96SJan Schmidt inodes = init_data_container(size); 4277d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4278d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4279d7728c96SJan Schmidt inodes = NULL; 4280d7728c96SJan Schmidt goto out; 4281d7728c96SJan Schmidt } 4282d7728c96SJan Schmidt 42832ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4284d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4285df031f07SLiu Bo if (ret == -EINVAL) 4286d7728c96SJan Schmidt ret = -ENOENT; 4287d7728c96SJan Schmidt if (ret < 0) 4288d7728c96SJan Schmidt goto out; 4289d7728c96SJan Schmidt 4290718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4291718dc5faSOmar Sandoval size); 4292d7728c96SJan Schmidt if (ret) 4293d7728c96SJan Schmidt ret = -EFAULT; 4294d7728c96SJan Schmidt 4295d7728c96SJan Schmidt out: 4296d7728c96SJan Schmidt btrfs_free_path(path); 4297f54de068SDavid Sterba kvfree(inodes); 4298d24a67b2SZygo Blaxell out_loi: 4299d7728c96SJan Schmidt kfree(loi); 4300d7728c96SJan Schmidt 4301d7728c96SJan Schmidt return ret; 4302d7728c96SJan Schmidt } 4303d7728c96SJan Schmidt 4304008ef096SDavid Sterba void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 4305c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4306c9e9f97bSIlya Dryomov { 4307c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4308c9e9f97bSIlya Dryomov 4309c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4310c9e9f97bSIlya Dryomov 43113009a62fSDavid Sterba if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) 4312837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4313837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4314837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4315a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4316a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4317837d5b6eSIlya Dryomov 4318c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4319c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4320c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 432119a39dceSIlya Dryomov 432219a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 432319a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 432419a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 4325c9e9f97bSIlya Dryomov } 4326c9e9f97bSIlya Dryomov 43279ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4328c9e9f97bSIlya Dryomov { 4329496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4330c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4331c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4332c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4333ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4334c9e9f97bSIlya Dryomov int ret; 4335c9e9f97bSIlya Dryomov 43366c405b24SNikolay Borisov if (!arg) 43376c405b24SNikolay Borisov btrfs_warn(fs_info, 43386c405b24SNikolay Borisov "IOC_BALANCE ioctl (v1) is deprecated and will be removed in kernel 5.18"); 43396c405b24SNikolay Borisov 4340c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4341c9e9f97bSIlya Dryomov return -EPERM; 4342c9e9f97bSIlya Dryomov 4343e54bfa31SLiu Bo ret = mnt_want_write_file(file); 43449ba1f6e4SLiu Bo if (ret) 43459ba1f6e4SLiu Bo return ret; 43469ba1f6e4SLiu Bo 4347ed0fb78fSIlya Dryomov again: 4348c3e1f96cSGoldwyn Rodrigues if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) { 4349c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4350ed0fb78fSIlya Dryomov need_unlock = true; 4351ed0fb78fSIlya Dryomov goto locked; 4352ed0fb78fSIlya Dryomov } 4353ed0fb78fSIlya Dryomov 4354ed0fb78fSIlya Dryomov /* 435501327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4356ed0fb78fSIlya Dryomov * (1) some other op is running 4357ed0fb78fSIlya Dryomov * (2) balance is running 4358ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4359ed0fb78fSIlya Dryomov */ 4360ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4361ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4362ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 43633009a62fSDavid Sterba if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4364ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4365dccdb07bSDavid Sterba /* 4366dccdb07bSDavid Sterba * Lock released to allow other waiters to continue, 4367dccdb07bSDavid Sterba * we'll reexamine the status again. 4368dccdb07bSDavid Sterba */ 4369ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4370ed0fb78fSIlya Dryomov 4371ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 43723009a62fSDavid Sterba !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4373ed0fb78fSIlya Dryomov /* this is (3) */ 4374ed0fb78fSIlya Dryomov need_unlock = false; 4375ed0fb78fSIlya Dryomov goto locked; 4376ed0fb78fSIlya Dryomov } 4377ed0fb78fSIlya Dryomov 4378ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4379ed0fb78fSIlya Dryomov goto again; 4380ed0fb78fSIlya Dryomov } else { 4381ed0fb78fSIlya Dryomov /* this is (2) */ 4382ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4383ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4384ed0fb78fSIlya Dryomov goto out; 4385ed0fb78fSIlya Dryomov } 4386ed0fb78fSIlya Dryomov } else { 4387ed0fb78fSIlya Dryomov /* this is (1) */ 4388ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4389e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4390ed0fb78fSIlya Dryomov goto out; 4391ed0fb78fSIlya Dryomov } 4392ed0fb78fSIlya Dryomov 4393ed0fb78fSIlya Dryomov locked: 4394c9e9f97bSIlya Dryomov 4395c9e9f97bSIlya Dryomov if (arg) { 4396c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4397c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4398c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4399ed0fb78fSIlya Dryomov goto out_unlock; 4400c9e9f97bSIlya Dryomov } 4401de322263SIlya Dryomov 4402de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4403de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4404de322263SIlya Dryomov ret = -ENOTCONN; 4405de322263SIlya Dryomov goto out_bargs; 4406de322263SIlya Dryomov } 4407de322263SIlya Dryomov 4408de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4409de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4410de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4411de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4412efc0e69cSNikolay Borisov btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE); 4413de322263SIlya Dryomov 4414de322263SIlya Dryomov goto do_balance; 4415de322263SIlya Dryomov } 4416c9e9f97bSIlya Dryomov } else { 4417c9e9f97bSIlya Dryomov bargs = NULL; 4418c9e9f97bSIlya Dryomov } 4419c9e9f97bSIlya Dryomov 4420ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4421837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4422837d5b6eSIlya Dryomov goto out_bargs; 4423837d5b6eSIlya Dryomov } 4424837d5b6eSIlya Dryomov 44258d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4426c9e9f97bSIlya Dryomov if (!bctl) { 4427c9e9f97bSIlya Dryomov ret = -ENOMEM; 4428c9e9f97bSIlya Dryomov goto out_bargs; 4429c9e9f97bSIlya Dryomov } 4430c9e9f97bSIlya Dryomov 4431c9e9f97bSIlya Dryomov if (arg) { 4432c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4433c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4434c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4435c9e9f97bSIlya Dryomov 4436c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4437f43ffb60SIlya Dryomov } else { 4438f43ffb60SIlya Dryomov /* balance everything - no filters */ 4439f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4440c9e9f97bSIlya Dryomov } 4441c9e9f97bSIlya Dryomov 44428eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 44438eb93459SDavid Sterba ret = -EINVAL; 44440f89abf5SChristian Engelmayer goto out_bctl; 44458eb93459SDavid Sterba } 44468eb93459SDavid Sterba 4447de322263SIlya Dryomov do_balance: 4448c9e9f97bSIlya Dryomov /* 4449c3e1f96cSGoldwyn Rodrigues * Ownership of bctl and exclusive operation goes to btrfs_balance. 4450c3e1f96cSGoldwyn Rodrigues * bctl is freed in reset_balance_state, or, if restriper was paused 4451c3e1f96cSGoldwyn Rodrigues * all the way until unmount, in free_fs_info. The flag should be 4452c3e1f96cSGoldwyn Rodrigues * cleared after reset_balance_state. 4453c9e9f97bSIlya Dryomov */ 4454ed0fb78fSIlya Dryomov need_unlock = false; 4455ed0fb78fSIlya Dryomov 44566fcf6e2bSDavid Sterba ret = btrfs_balance(fs_info, bctl, bargs); 44570f89abf5SChristian Engelmayer bctl = NULL; 4458ed0fb78fSIlya Dryomov 4459d00c2d9cSFilipe Manana if ((ret == 0 || ret == -ECANCELED) && arg) { 4460c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4461c9e9f97bSIlya Dryomov ret = -EFAULT; 4462c9e9f97bSIlya Dryomov } 4463c9e9f97bSIlya Dryomov 44640f89abf5SChristian Engelmayer out_bctl: 44650f89abf5SChristian Engelmayer kfree(bctl); 4466c9e9f97bSIlya Dryomov out_bargs: 4467c9e9f97bSIlya Dryomov kfree(bargs); 4468ed0fb78fSIlya Dryomov out_unlock: 4469c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4470ed0fb78fSIlya Dryomov if (need_unlock) 4471c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 4472ed0fb78fSIlya Dryomov out: 4473e54bfa31SLiu Bo mnt_drop_write_file(file); 4474c9e9f97bSIlya Dryomov return ret; 4475c9e9f97bSIlya Dryomov } 4476c9e9f97bSIlya Dryomov 44772ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4478837d5b6eSIlya Dryomov { 4479837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4480837d5b6eSIlya Dryomov return -EPERM; 4481837d5b6eSIlya Dryomov 4482837d5b6eSIlya Dryomov switch (cmd) { 4483837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 44840b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4485a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 44860b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4487837d5b6eSIlya Dryomov } 4488837d5b6eSIlya Dryomov 4489837d5b6eSIlya Dryomov return -EINVAL; 4490837d5b6eSIlya Dryomov } 4491837d5b6eSIlya Dryomov 44922ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 449319a39dceSIlya Dryomov void __user *arg) 449419a39dceSIlya Dryomov { 449519a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 449619a39dceSIlya Dryomov int ret = 0; 449719a39dceSIlya Dryomov 449819a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 449919a39dceSIlya Dryomov return -EPERM; 450019a39dceSIlya Dryomov 450119a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 450219a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 450319a39dceSIlya Dryomov ret = -ENOTCONN; 450419a39dceSIlya Dryomov goto out; 450519a39dceSIlya Dryomov } 450619a39dceSIlya Dryomov 45078d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 450819a39dceSIlya Dryomov if (!bargs) { 450919a39dceSIlya Dryomov ret = -ENOMEM; 451019a39dceSIlya Dryomov goto out; 451119a39dceSIlya Dryomov } 451219a39dceSIlya Dryomov 4513008ef096SDavid Sterba btrfs_update_ioctl_balance_args(fs_info, bargs); 451419a39dceSIlya Dryomov 451519a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 451619a39dceSIlya Dryomov ret = -EFAULT; 451719a39dceSIlya Dryomov 451819a39dceSIlya Dryomov kfree(bargs); 451919a39dceSIlya Dryomov out: 452019a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 452119a39dceSIlya Dryomov return ret; 452219a39dceSIlya Dryomov } 452319a39dceSIlya Dryomov 4524905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 45255d13a37bSArne Jansen { 45260b246afaSJeff Mahoney struct inode *inode = file_inode(file); 45270b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 45285d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 45295d13a37bSArne Jansen int ret; 45305d13a37bSArne Jansen 45315d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45325d13a37bSArne Jansen return -EPERM; 45335d13a37bSArne Jansen 4534905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4535905b0ddaSMiao Xie if (ret) 4536905b0ddaSMiao Xie return ret; 45375d13a37bSArne Jansen 45385d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4539905b0ddaSMiao Xie if (IS_ERR(sa)) { 4540905b0ddaSMiao Xie ret = PTR_ERR(sa); 4541905b0ddaSMiao Xie goto drop_write; 4542905b0ddaSMiao Xie } 45435d13a37bSArne Jansen 45440b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 45455d13a37bSArne Jansen 45465d13a37bSArne Jansen switch (sa->cmd) { 45475d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 4548340f1aa2SNikolay Borisov ret = btrfs_quota_enable(fs_info); 45495d13a37bSArne Jansen break; 45505d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 4551340f1aa2SNikolay Borisov ret = btrfs_quota_disable(fs_info); 45525d13a37bSArne Jansen break; 45535d13a37bSArne Jansen default: 45545d13a37bSArne Jansen ret = -EINVAL; 45555d13a37bSArne Jansen break; 45565d13a37bSArne Jansen } 45575d13a37bSArne Jansen 45585d13a37bSArne Jansen kfree(sa); 45590b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4560905b0ddaSMiao Xie drop_write: 4561905b0ddaSMiao Xie mnt_drop_write_file(file); 45625d13a37bSArne Jansen return ret; 45635d13a37bSArne Jansen } 45645d13a37bSArne Jansen 4565905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 45665d13a37bSArne Jansen { 45670b246afaSJeff Mahoney struct inode *inode = file_inode(file); 45680b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 45690b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 45705d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 45715d13a37bSArne Jansen struct btrfs_trans_handle *trans; 45725d13a37bSArne Jansen int ret; 45735d13a37bSArne Jansen int err; 45745d13a37bSArne Jansen 45755d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45765d13a37bSArne Jansen return -EPERM; 45775d13a37bSArne Jansen 4578905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4579905b0ddaSMiao Xie if (ret) 4580905b0ddaSMiao Xie return ret; 45815d13a37bSArne Jansen 45825d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4583905b0ddaSMiao Xie if (IS_ERR(sa)) { 4584905b0ddaSMiao Xie ret = PTR_ERR(sa); 4585905b0ddaSMiao Xie goto drop_write; 4586905b0ddaSMiao Xie } 45875d13a37bSArne Jansen 45885d13a37bSArne Jansen trans = btrfs_join_transaction(root); 45895d13a37bSArne Jansen if (IS_ERR(trans)) { 45905d13a37bSArne Jansen ret = PTR_ERR(trans); 45915d13a37bSArne Jansen goto out; 45925d13a37bSArne Jansen } 45935d13a37bSArne Jansen 45945d13a37bSArne Jansen if (sa->assign) { 45959f8a6ce6SLu Fengqi ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); 45965d13a37bSArne Jansen } else { 459739616c27SLu Fengqi ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); 45985d13a37bSArne Jansen } 45995d13a37bSArne Jansen 4600e082f563SQu Wenruo /* update qgroup status and info */ 4601280f8bd2SLu Fengqi err = btrfs_run_qgroups(trans); 4602e082f563SQu Wenruo if (err < 0) 46030b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4604ad8403dfSAnand Jain "failed to update qgroup status and info"); 46053a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 46065d13a37bSArne Jansen if (err && !ret) 46075d13a37bSArne Jansen ret = err; 46085d13a37bSArne Jansen 46095d13a37bSArne Jansen out: 46105d13a37bSArne Jansen kfree(sa); 4611905b0ddaSMiao Xie drop_write: 4612905b0ddaSMiao Xie mnt_drop_write_file(file); 46135d13a37bSArne Jansen return ret; 46145d13a37bSArne Jansen } 46155d13a37bSArne Jansen 4616905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 46175d13a37bSArne Jansen { 46180b246afaSJeff Mahoney struct inode *inode = file_inode(file); 46190b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 46205d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 46215d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46225d13a37bSArne Jansen int ret; 46235d13a37bSArne Jansen int err; 46245d13a37bSArne Jansen 46255d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46265d13a37bSArne Jansen return -EPERM; 46275d13a37bSArne Jansen 4628905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4629905b0ddaSMiao Xie if (ret) 4630905b0ddaSMiao Xie return ret; 46315d13a37bSArne Jansen 46325d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4633905b0ddaSMiao Xie if (IS_ERR(sa)) { 4634905b0ddaSMiao Xie ret = PTR_ERR(sa); 4635905b0ddaSMiao Xie goto drop_write; 4636905b0ddaSMiao Xie } 46375d13a37bSArne Jansen 4638d86e56cfSMiao Xie if (!sa->qgroupid) { 4639d86e56cfSMiao Xie ret = -EINVAL; 4640d86e56cfSMiao Xie goto out; 4641d86e56cfSMiao Xie } 4642d86e56cfSMiao Xie 46435d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46445d13a37bSArne Jansen if (IS_ERR(trans)) { 46455d13a37bSArne Jansen ret = PTR_ERR(trans); 46465d13a37bSArne Jansen goto out; 46475d13a37bSArne Jansen } 46485d13a37bSArne Jansen 46495d13a37bSArne Jansen if (sa->create) { 465049a05ecdSLu Fengqi ret = btrfs_create_qgroup(trans, sa->qgroupid); 46515d13a37bSArne Jansen } else { 46523efbee1dSLu Fengqi ret = btrfs_remove_qgroup(trans, sa->qgroupid); 46535d13a37bSArne Jansen } 46545d13a37bSArne Jansen 46553a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 46565d13a37bSArne Jansen if (err && !ret) 46575d13a37bSArne Jansen ret = err; 46585d13a37bSArne Jansen 46595d13a37bSArne Jansen out: 46605d13a37bSArne Jansen kfree(sa); 4661905b0ddaSMiao Xie drop_write: 4662905b0ddaSMiao Xie mnt_drop_write_file(file); 46635d13a37bSArne Jansen return ret; 46645d13a37bSArne Jansen } 46655d13a37bSArne Jansen 4666905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 46675d13a37bSArne Jansen { 46680b246afaSJeff Mahoney struct inode *inode = file_inode(file); 46690b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 46705d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 46715d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46725d13a37bSArne Jansen int ret; 46735d13a37bSArne Jansen int err; 46745d13a37bSArne Jansen u64 qgroupid; 46755d13a37bSArne Jansen 46765d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46775d13a37bSArne Jansen return -EPERM; 46785d13a37bSArne Jansen 4679905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4680905b0ddaSMiao Xie if (ret) 4681905b0ddaSMiao Xie return ret; 46825d13a37bSArne Jansen 46835d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4684905b0ddaSMiao Xie if (IS_ERR(sa)) { 4685905b0ddaSMiao Xie ret = PTR_ERR(sa); 4686905b0ddaSMiao Xie goto drop_write; 4687905b0ddaSMiao Xie } 46885d13a37bSArne Jansen 46895d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46905d13a37bSArne Jansen if (IS_ERR(trans)) { 46915d13a37bSArne Jansen ret = PTR_ERR(trans); 46925d13a37bSArne Jansen goto out; 46935d13a37bSArne Jansen } 46945d13a37bSArne Jansen 46955d13a37bSArne Jansen qgroupid = sa->qgroupid; 46965d13a37bSArne Jansen if (!qgroupid) { 46975d13a37bSArne Jansen /* take the current subvol as qgroup */ 46985d13a37bSArne Jansen qgroupid = root->root_key.objectid; 46995d13a37bSArne Jansen } 47005d13a37bSArne Jansen 4701f0042d5eSLu Fengqi ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); 47025d13a37bSArne Jansen 47033a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 47045d13a37bSArne Jansen if (err && !ret) 47055d13a37bSArne Jansen ret = err; 47065d13a37bSArne Jansen 47075d13a37bSArne Jansen out: 47085d13a37bSArne Jansen kfree(sa); 4709905b0ddaSMiao Xie drop_write: 4710905b0ddaSMiao Xie mnt_drop_write_file(file); 47115d13a37bSArne Jansen return ret; 47125d13a37bSArne Jansen } 47135d13a37bSArne Jansen 47142f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 47152f232036SJan Schmidt { 47160b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47170b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47182f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47192f232036SJan Schmidt int ret; 47202f232036SJan Schmidt 47212f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47222f232036SJan Schmidt return -EPERM; 47232f232036SJan Schmidt 47242f232036SJan Schmidt ret = mnt_want_write_file(file); 47252f232036SJan Schmidt if (ret) 47262f232036SJan Schmidt return ret; 47272f232036SJan Schmidt 47282f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 47292f232036SJan Schmidt if (IS_ERR(qsa)) { 47302f232036SJan Schmidt ret = PTR_ERR(qsa); 47312f232036SJan Schmidt goto drop_write; 47322f232036SJan Schmidt } 47332f232036SJan Schmidt 47342f232036SJan Schmidt if (qsa->flags) { 47352f232036SJan Schmidt ret = -EINVAL; 47362f232036SJan Schmidt goto out; 47372f232036SJan Schmidt } 47382f232036SJan Schmidt 47390b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 47402f232036SJan Schmidt 47412f232036SJan Schmidt out: 47422f232036SJan Schmidt kfree(qsa); 47432f232036SJan Schmidt drop_write: 47442f232036SJan Schmidt mnt_drop_write_file(file); 47452f232036SJan Schmidt return ret; 47462f232036SJan Schmidt } 47472f232036SJan Schmidt 4748b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, 4749b929c1d8SMarcos Paulo de Souza void __user *arg) 47502f232036SJan Schmidt { 47510afb603aSGoldwyn Rodrigues struct btrfs_ioctl_quota_rescan_args qsa = {0}; 47522f232036SJan Schmidt 47532f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47542f232036SJan Schmidt return -EPERM; 47552f232036SJan Schmidt 47560b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 47570afb603aSGoldwyn Rodrigues qsa.flags = 1; 47580afb603aSGoldwyn Rodrigues qsa.progress = fs_info->qgroup_rescan_progress.objectid; 47592f232036SJan Schmidt } 47602f232036SJan Schmidt 47610afb603aSGoldwyn Rodrigues if (copy_to_user(arg, &qsa, sizeof(qsa))) 4762991a3daeSAnand Jain return -EFAULT; 47632f232036SJan Schmidt 4764991a3daeSAnand Jain return 0; 47652f232036SJan Schmidt } 47662f232036SJan Schmidt 4767b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, 4768b929c1d8SMarcos Paulo de Souza void __user *arg) 476957254b6eSJan Schmidt { 477057254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 477157254b6eSJan Schmidt return -EPERM; 477257254b6eSJan Schmidt 47730b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 477457254b6eSJan Schmidt } 477557254b6eSJan Schmidt 4776abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4777e4fed17aSChristian Brauner struct user_namespace *mnt_userns, 4778abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 47798ea05e3aSAlexander Block { 4780496ad9aaSAl Viro struct inode *inode = file_inode(file); 47810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47828ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 47838ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 47848ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 478595582b00SDeepa Dinamani struct timespec64 ct = current_time(inode); 47868ea05e3aSAlexander Block int ret = 0; 4787dd5f9615SStefan Behrens int received_uuid_changed; 47888ea05e3aSAlexander Block 4789e4fed17aSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 4790bd60ea0fSDavid Sterba return -EPERM; 4791bd60ea0fSDavid Sterba 47928ea05e3aSAlexander Block ret = mnt_want_write_file(file); 47938ea05e3aSAlexander Block if (ret < 0) 47948ea05e3aSAlexander Block return ret; 47958ea05e3aSAlexander Block 47960b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 47978ea05e3aSAlexander Block 47984a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 47998ea05e3aSAlexander Block ret = -EINVAL; 48008ea05e3aSAlexander Block goto out; 48018ea05e3aSAlexander Block } 48028ea05e3aSAlexander Block 48038ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 48048ea05e3aSAlexander Block ret = -EROFS; 48058ea05e3aSAlexander Block goto out; 48068ea05e3aSAlexander Block } 48078ea05e3aSAlexander Block 4808dd5f9615SStefan Behrens /* 4809dd5f9615SStefan Behrens * 1 - root item 4810dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4811dd5f9615SStefan Behrens */ 4812dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 48138ea05e3aSAlexander Block if (IS_ERR(trans)) { 48148ea05e3aSAlexander Block ret = PTR_ERR(trans); 48158ea05e3aSAlexander Block trans = NULL; 48168ea05e3aSAlexander Block goto out; 48178ea05e3aSAlexander Block } 48188ea05e3aSAlexander Block 48198ea05e3aSAlexander Block sa->rtransid = trans->transid; 48208ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 48218ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 48228ea05e3aSAlexander Block 4823dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4824dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4825dd5f9615SStefan Behrens if (received_uuid_changed && 4826d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 4827d1957791SLu Fengqi ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, 4828dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4829dd5f9615SStefan Behrens root->root_key.objectid); 4830d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 4831d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 4832d87ff758SNikolay Borisov btrfs_end_transaction(trans); 4833d87ff758SNikolay Borisov goto out; 4834d87ff758SNikolay Borisov } 4835d87ff758SNikolay Borisov } 48368ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 48378ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 48388ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 48393cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 48403cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 48413cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 48423cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 48438ea05e3aSAlexander Block 48440b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 48458ea05e3aSAlexander Block &root->root_key, &root->root_item); 48468ea05e3aSAlexander Block if (ret < 0) { 48473a45bb20SJeff Mahoney btrfs_end_transaction(trans); 48488ea05e3aSAlexander Block goto out; 4849dd5f9615SStefan Behrens } 4850dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4851cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, sa->uuid, 4852dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4853dd5f9615SStefan Behrens root->root_key.objectid); 4854dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 485566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 4856efd38150SNikolay Borisov btrfs_end_transaction(trans); 4857dd5f9615SStefan Behrens goto out; 4858dd5f9615SStefan Behrens } 4859dd5f9615SStefan Behrens } 48603a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 4861abccd00fSHugo Mills out: 48620b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4863abccd00fSHugo Mills mnt_drop_write_file(file); 4864abccd00fSHugo Mills return ret; 4865abccd00fSHugo Mills } 4866abccd00fSHugo Mills 4867abccd00fSHugo Mills #ifdef CONFIG_64BIT 4868abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 4869abccd00fSHugo Mills void __user *arg) 4870abccd00fSHugo Mills { 4871abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 4872abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 4873abccd00fSHugo Mills int ret = 0; 4874abccd00fSHugo Mills 4875abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 48767b9ea627SShailendra Verma if (IS_ERR(args32)) 48777b9ea627SShailendra Verma return PTR_ERR(args32); 4878abccd00fSHugo Mills 48798d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 488084dbeb87SDan Carpenter if (!args64) { 488184dbeb87SDan Carpenter ret = -ENOMEM; 4882abccd00fSHugo Mills goto out; 4883abccd00fSHugo Mills } 4884abccd00fSHugo Mills 4885abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 4886abccd00fSHugo Mills args64->stransid = args32->stransid; 4887abccd00fSHugo Mills args64->rtransid = args32->rtransid; 4888abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 4889abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 4890abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 4891abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 4892abccd00fSHugo Mills args64->flags = args32->flags; 4893abccd00fSHugo Mills 4894e4fed17aSChristian Brauner ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64); 4895abccd00fSHugo Mills if (ret) 4896abccd00fSHugo Mills goto out; 4897abccd00fSHugo Mills 4898abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 4899abccd00fSHugo Mills args32->stransid = args64->stransid; 4900abccd00fSHugo Mills args32->rtransid = args64->rtransid; 4901abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 4902abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 4903abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 4904abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 4905abccd00fSHugo Mills args32->flags = args64->flags; 4906abccd00fSHugo Mills 4907abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 4908abccd00fSHugo Mills if (ret) 4909abccd00fSHugo Mills ret = -EFAULT; 4910abccd00fSHugo Mills 4911abccd00fSHugo Mills out: 4912abccd00fSHugo Mills kfree(args32); 4913abccd00fSHugo Mills kfree(args64); 4914abccd00fSHugo Mills return ret; 4915abccd00fSHugo Mills } 4916abccd00fSHugo Mills #endif 4917abccd00fSHugo Mills 4918abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 4919abccd00fSHugo Mills void __user *arg) 4920abccd00fSHugo Mills { 4921abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 4922abccd00fSHugo Mills int ret = 0; 4923abccd00fSHugo Mills 4924abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 49257b9ea627SShailendra Verma if (IS_ERR(sa)) 49267b9ea627SShailendra Verma return PTR_ERR(sa); 4927abccd00fSHugo Mills 4928e4fed17aSChristian Brauner ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa); 4929abccd00fSHugo Mills 4930abccd00fSHugo Mills if (ret) 4931abccd00fSHugo Mills goto out; 4932abccd00fSHugo Mills 49338ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 49348ea05e3aSAlexander Block if (ret) 49358ea05e3aSAlexander Block ret = -EFAULT; 49368ea05e3aSAlexander Block 49378ea05e3aSAlexander Block out: 49388ea05e3aSAlexander Block kfree(sa); 49398ea05e3aSAlexander Block return ret; 49408ea05e3aSAlexander Block } 49418ea05e3aSAlexander Block 4942b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, 4943b929c1d8SMarcos Paulo de Souza void __user *arg) 4944867ab667Sjeff.liu { 4945a1b83ac5SAnand Jain size_t len; 4946867ab667Sjeff.liu int ret; 4947a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4948a1b83ac5SAnand Jain 49490b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 49500b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 49510b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 4952a1b83ac5SAnand Jain 4953a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4954867ab667Sjeff.liu 4955867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 49560b246afaSJeff Mahoney btrfs_warn(fs_info, 49570b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 49580b246afaSJeff Mahoney --len); 4959867ab667Sjeff.liu } 4960867ab667Sjeff.liu 4961867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4962867ab667Sjeff.liu 4963867ab667Sjeff.liu return ret ? -EFAULT : 0; 4964867ab667Sjeff.liu } 4965867ab667Sjeff.liu 4966a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4967a8bfd4abSjeff.liu { 49680b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49700b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 49710b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 4972a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4973a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4974a8bfd4abSjeff.liu int ret; 4975a8bfd4abSjeff.liu 4976a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4977a8bfd4abSjeff.liu return -EPERM; 4978a8bfd4abSjeff.liu 4979a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4980a8bfd4abSjeff.liu return -EFAULT; 4981a8bfd4abSjeff.liu 4982a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 49830b246afaSJeff Mahoney btrfs_err(fs_info, 49845d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 4985a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4986a8bfd4abSjeff.liu return -EINVAL; 4987a8bfd4abSjeff.liu } 4988a8bfd4abSjeff.liu 4989a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4990a8bfd4abSjeff.liu if (ret) 4991a8bfd4abSjeff.liu return ret; 4992a8bfd4abSjeff.liu 4993a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4994a8bfd4abSjeff.liu if (IS_ERR(trans)) { 4995a8bfd4abSjeff.liu ret = PTR_ERR(trans); 4996a8bfd4abSjeff.liu goto out_unlock; 4997a8bfd4abSjeff.liu } 4998a8bfd4abSjeff.liu 49990b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5000a8bfd4abSjeff.liu strcpy(super_block->label, label); 50010b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 50023a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5003a8bfd4abSjeff.liu 5004a8bfd4abSjeff.liu out_unlock: 5005a8bfd4abSjeff.liu mnt_drop_write_file(file); 5006a8bfd4abSjeff.liu return ret; 5007a8bfd4abSjeff.liu } 5008a8bfd4abSjeff.liu 50092eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 50102eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 50112eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 50122eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 50132eaa055fSJeff Mahoney 5014d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 50152eaa055fSJeff Mahoney { 50164d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 50172eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 50182eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 50192eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 50202eaa055fSJeff Mahoney }; 50212eaa055fSJeff Mahoney 50222eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50232eaa055fSJeff Mahoney return -EFAULT; 50242eaa055fSJeff Mahoney 50252eaa055fSJeff Mahoney return 0; 50262eaa055fSJeff Mahoney } 50272eaa055fSJeff Mahoney 5028b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, 5029b929c1d8SMarcos Paulo de Souza void __user *arg) 50302eaa055fSJeff Mahoney { 50310b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 50322eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 50332eaa055fSJeff Mahoney 50342eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 50352eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 50362eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 50372eaa055fSJeff Mahoney 50382eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50392eaa055fSJeff Mahoney return -EFAULT; 50402eaa055fSJeff Mahoney 50412eaa055fSJeff Mahoney return 0; 50422eaa055fSJeff Mahoney } 50432eaa055fSJeff Mahoney 50442ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 50453b02a68aSJeff Mahoney enum btrfs_feature_set set, 50462eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 50472eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 50482eaa055fSJeff Mahoney { 5049f10152bcSDavid Sterba const char *type = btrfs_feature_set_name(set); 50503b02a68aSJeff Mahoney char *names; 50512eaa055fSJeff Mahoney u64 disallowed, unsupported; 50522eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 50532eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 50542eaa055fSJeff Mahoney 50552eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 50562eaa055fSJeff Mahoney if (unsupported) { 50573b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 50583b02a68aSJeff Mahoney if (names) { 50590b246afaSJeff Mahoney btrfs_warn(fs_info, 50603b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 50613b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50623b02a68aSJeff Mahoney kfree(names); 50633b02a68aSJeff Mahoney } else 50640b246afaSJeff Mahoney btrfs_warn(fs_info, 50652eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 50662eaa055fSJeff Mahoney type, unsupported); 50672eaa055fSJeff Mahoney return -EOPNOTSUPP; 50682eaa055fSJeff Mahoney } 50692eaa055fSJeff Mahoney 50702eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 50712eaa055fSJeff Mahoney if (disallowed) { 50723b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50733b02a68aSJeff Mahoney if (names) { 50740b246afaSJeff Mahoney btrfs_warn(fs_info, 50753b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 50763b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50773b02a68aSJeff Mahoney kfree(names); 50783b02a68aSJeff Mahoney } else 50790b246afaSJeff Mahoney btrfs_warn(fs_info, 50802eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 50812eaa055fSJeff Mahoney type, disallowed); 50822eaa055fSJeff Mahoney return -EPERM; 50832eaa055fSJeff Mahoney } 50842eaa055fSJeff Mahoney 50852eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 50862eaa055fSJeff Mahoney if (disallowed) { 50873b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50883b02a68aSJeff Mahoney if (names) { 50890b246afaSJeff Mahoney btrfs_warn(fs_info, 50903b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 50913b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50923b02a68aSJeff Mahoney kfree(names); 50933b02a68aSJeff Mahoney } else 50940b246afaSJeff Mahoney btrfs_warn(fs_info, 50952eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 50962eaa055fSJeff Mahoney type, disallowed); 50972eaa055fSJeff Mahoney return -EPERM; 50982eaa055fSJeff Mahoney } 50992eaa055fSJeff Mahoney 51002eaa055fSJeff Mahoney return 0; 51012eaa055fSJeff Mahoney } 51022eaa055fSJeff Mahoney 51032ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 51042ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 51052eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 51062eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 51072eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 51082eaa055fSJeff Mahoney 51092eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 51102eaa055fSJeff Mahoney { 51110b246afaSJeff Mahoney struct inode *inode = file_inode(file); 51120b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 51130b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 51140b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 51152eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 51162eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 51172eaa055fSJeff Mahoney u64 newflags; 51182eaa055fSJeff Mahoney int ret; 51192eaa055fSJeff Mahoney 51202eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 51212eaa055fSJeff Mahoney return -EPERM; 51222eaa055fSJeff Mahoney 51232eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 51242eaa055fSJeff Mahoney return -EFAULT; 51252eaa055fSJeff Mahoney 51262eaa055fSJeff Mahoney /* Nothing to do */ 51272eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 51282eaa055fSJeff Mahoney !flags[0].incompat_flags) 51292eaa055fSJeff Mahoney return 0; 51302eaa055fSJeff Mahoney 51312ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 51322eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 51332eaa055fSJeff Mahoney if (ret) 51342eaa055fSJeff Mahoney return ret; 51352eaa055fSJeff Mahoney 51362ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 51372eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 51382eaa055fSJeff Mahoney if (ret) 51392eaa055fSJeff Mahoney return ret; 51402eaa055fSJeff Mahoney 51412ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 51422eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 51432eaa055fSJeff Mahoney if (ret) 51442eaa055fSJeff Mahoney return ret; 51452eaa055fSJeff Mahoney 51467ab19625SDavid Sterba ret = mnt_want_write_file(file); 51477ab19625SDavid Sterba if (ret) 51487ab19625SDavid Sterba return ret; 51497ab19625SDavid Sterba 51508051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 51517ab19625SDavid Sterba if (IS_ERR(trans)) { 51527ab19625SDavid Sterba ret = PTR_ERR(trans); 51537ab19625SDavid Sterba goto out_drop_write; 51547ab19625SDavid Sterba } 51552eaa055fSJeff Mahoney 51560b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 51572eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 51582eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 51592eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 51602eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 51612eaa055fSJeff Mahoney 51622eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 51632eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 51642eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 51652eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 51662eaa055fSJeff Mahoney 51672eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 51682eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 51692eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 51702eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 51710b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 51722eaa055fSJeff Mahoney 51733a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 51747ab19625SDavid Sterba out_drop_write: 51757ab19625SDavid Sterba mnt_drop_write_file(file); 51767ab19625SDavid Sterba 51777ab19625SDavid Sterba return ret; 51782eaa055fSJeff Mahoney } 51792eaa055fSJeff Mahoney 51809ad12305SSahil Kang static int _btrfs_ioctl_send(struct inode *inode, void __user *argp, bool compat) 51812351f431SJosef Bacik { 51822351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 51832351f431SJosef Bacik int ret; 51842351f431SJosef Bacik 51852351f431SJosef Bacik if (compat) { 51862351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 51872351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 51882351f431SJosef Bacik 51892351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 51902351f431SJosef Bacik if (ret) 51912351f431SJosef Bacik return -EFAULT; 51922351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 51932351f431SJosef Bacik if (!arg) 51942351f431SJosef Bacik return -ENOMEM; 51952351f431SJosef Bacik arg->send_fd = args32.send_fd; 51962351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 51972351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 51982351f431SJosef Bacik arg->parent_root = args32.parent_root; 51992351f431SJosef Bacik arg->flags = args32.flags; 52002351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 52012351f431SJosef Bacik sizeof(args32.reserved)); 52022351f431SJosef Bacik #else 52032351f431SJosef Bacik return -ENOTTY; 52042351f431SJosef Bacik #endif 52052351f431SJosef Bacik } else { 52062351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 52072351f431SJosef Bacik if (IS_ERR(arg)) 52082351f431SJosef Bacik return PTR_ERR(arg); 52092351f431SJosef Bacik } 52109ad12305SSahil Kang ret = btrfs_ioctl_send(inode, arg); 52112351f431SJosef Bacik kfree(arg); 52122351f431SJosef Bacik return ret; 52132351f431SJosef Bacik } 52142351f431SJosef Bacik 5215*1881fba8SOmar Sandoval static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, 5216*1881fba8SOmar Sandoval bool compat) 5217*1881fba8SOmar Sandoval { 5218*1881fba8SOmar Sandoval struct btrfs_ioctl_encoded_io_args args = { 0 }; 5219*1881fba8SOmar Sandoval size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, 5220*1881fba8SOmar Sandoval flags); 5221*1881fba8SOmar Sandoval size_t copy_end; 5222*1881fba8SOmar Sandoval struct iovec iovstack[UIO_FASTIOV]; 5223*1881fba8SOmar Sandoval struct iovec *iov = iovstack; 5224*1881fba8SOmar Sandoval struct iov_iter iter; 5225*1881fba8SOmar Sandoval loff_t pos; 5226*1881fba8SOmar Sandoval struct kiocb kiocb; 5227*1881fba8SOmar Sandoval ssize_t ret; 5228*1881fba8SOmar Sandoval 5229*1881fba8SOmar Sandoval if (!capable(CAP_SYS_ADMIN)) { 5230*1881fba8SOmar Sandoval ret = -EPERM; 5231*1881fba8SOmar Sandoval goto out_acct; 5232*1881fba8SOmar Sandoval } 5233*1881fba8SOmar Sandoval 5234*1881fba8SOmar Sandoval if (compat) { 5235*1881fba8SOmar Sandoval #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 5236*1881fba8SOmar Sandoval struct btrfs_ioctl_encoded_io_args_32 args32; 5237*1881fba8SOmar Sandoval 5238*1881fba8SOmar Sandoval copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, 5239*1881fba8SOmar Sandoval flags); 5240*1881fba8SOmar Sandoval if (copy_from_user(&args32, argp, copy_end)) { 5241*1881fba8SOmar Sandoval ret = -EFAULT; 5242*1881fba8SOmar Sandoval goto out_acct; 5243*1881fba8SOmar Sandoval } 5244*1881fba8SOmar Sandoval args.iov = compat_ptr(args32.iov); 5245*1881fba8SOmar Sandoval args.iovcnt = args32.iovcnt; 5246*1881fba8SOmar Sandoval args.offset = args32.offset; 5247*1881fba8SOmar Sandoval args.flags = args32.flags; 5248*1881fba8SOmar Sandoval #else 5249*1881fba8SOmar Sandoval return -ENOTTY; 5250*1881fba8SOmar Sandoval #endif 5251*1881fba8SOmar Sandoval } else { 5252*1881fba8SOmar Sandoval copy_end = copy_end_kernel; 5253*1881fba8SOmar Sandoval if (copy_from_user(&args, argp, copy_end)) { 5254*1881fba8SOmar Sandoval ret = -EFAULT; 5255*1881fba8SOmar Sandoval goto out_acct; 5256*1881fba8SOmar Sandoval } 5257*1881fba8SOmar Sandoval } 5258*1881fba8SOmar Sandoval if (args.flags != 0) { 5259*1881fba8SOmar Sandoval ret = -EINVAL; 5260*1881fba8SOmar Sandoval goto out_acct; 5261*1881fba8SOmar Sandoval } 5262*1881fba8SOmar Sandoval 5263*1881fba8SOmar Sandoval ret = import_iovec(READ, args.iov, args.iovcnt, ARRAY_SIZE(iovstack), 5264*1881fba8SOmar Sandoval &iov, &iter); 5265*1881fba8SOmar Sandoval if (ret < 0) 5266*1881fba8SOmar Sandoval goto out_acct; 5267*1881fba8SOmar Sandoval 5268*1881fba8SOmar Sandoval if (iov_iter_count(&iter) == 0) { 5269*1881fba8SOmar Sandoval ret = 0; 5270*1881fba8SOmar Sandoval goto out_iov; 5271*1881fba8SOmar Sandoval } 5272*1881fba8SOmar Sandoval pos = args.offset; 5273*1881fba8SOmar Sandoval ret = rw_verify_area(READ, file, &pos, args.len); 5274*1881fba8SOmar Sandoval if (ret < 0) 5275*1881fba8SOmar Sandoval goto out_iov; 5276*1881fba8SOmar Sandoval 5277*1881fba8SOmar Sandoval init_sync_kiocb(&kiocb, file); 5278*1881fba8SOmar Sandoval kiocb.ki_pos = pos; 5279*1881fba8SOmar Sandoval 5280*1881fba8SOmar Sandoval ret = btrfs_encoded_read(&kiocb, &iter, &args); 5281*1881fba8SOmar Sandoval if (ret >= 0) { 5282*1881fba8SOmar Sandoval fsnotify_access(file); 5283*1881fba8SOmar Sandoval if (copy_to_user(argp + copy_end, 5284*1881fba8SOmar Sandoval (char *)&args + copy_end_kernel, 5285*1881fba8SOmar Sandoval sizeof(args) - copy_end_kernel)) 5286*1881fba8SOmar Sandoval ret = -EFAULT; 5287*1881fba8SOmar Sandoval } 5288*1881fba8SOmar Sandoval 5289*1881fba8SOmar Sandoval out_iov: 5290*1881fba8SOmar Sandoval kfree(iov); 5291*1881fba8SOmar Sandoval out_acct: 5292*1881fba8SOmar Sandoval if (ret > 0) 5293*1881fba8SOmar Sandoval add_rchar(current, ret); 5294*1881fba8SOmar Sandoval inc_syscr(current); 5295*1881fba8SOmar Sandoval return ret; 5296*1881fba8SOmar Sandoval } 5297*1881fba8SOmar Sandoval 5298f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5299f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5300f46b5a66SChristoph Hellwig { 53010b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53020b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53030b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 53044bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5305f46b5a66SChristoph Hellwig 5306f46b5a66SChristoph Hellwig switch (cmd) { 53076cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 5308dc408ccdSSahil Kang return btrfs_ioctl_getversion(inode, argp); 530940cf931fSEric Sandeen case FS_IOC_GETFSLABEL: 5310b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_fslabel(fs_info, argp); 531140cf931fSEric Sandeen case FS_IOC_SETFSLABEL: 531240cf931fSEric Sandeen return btrfs_ioctl_set_fslabel(file, argp); 5313f7039b1dSLi Dongyang case FITRIM: 5314b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_fitrim(fs_info, argp); 5315f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5316fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5317fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5318fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 53193de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5320fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 53216f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 53226f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 532376dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 5324949964c9SMarcos Paulo de Souza return btrfs_ioctl_snap_destroy(file, argp, false); 5325949964c9SMarcos Paulo de Souza case BTRFS_IOC_SNAP_DESTROY_V2: 5326949964c9SMarcos Paulo de Souza return btrfs_ioctl_snap_destroy(file, argp, true); 53270caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 53289ad12305SSahil Kang return btrfs_ioctl_subvol_getflags(inode, argp); 53290caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 53300caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 53316ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 53326ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5333f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 53341e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 53351e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 53361e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5337f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5338198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5339f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 53402ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5341f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5342da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 53436b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 53446b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5345475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 53462ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5347475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 53482ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5349f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 53509ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5351ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 53529ad12305SSahil Kang return btrfs_ioctl_tree_search(inode, argp); 5353cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 53549ad12305SSahil Kang return btrfs_ioctl_tree_search_v2(inode, argp); 5355ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5356dc408ccdSSahil Kang return btrfs_ioctl_ino_lookup(root, argp); 5357d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5358d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5359d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5360d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5361d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5362d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 53631406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 53642ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 53659b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 53669b199859SFilipe David Borba Manana int ret; 53679b199859SFilipe David Borba Manana 53689db4dc24SNikolay Borisov ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false); 53699b199859SFilipe David Borba Manana if (ret) 53709b199859SFilipe David Borba Manana return ret; 53710b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 53722fad4e83SDavid Sterba /* 53732fad4e83SDavid Sterba * The transaction thread may want to do more work, 537401327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 53752fad4e83SDavid Sterba * processing uncleaned subvols. 53762fad4e83SDavid Sterba */ 53770b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 53789b199859SFilipe David Borba Manana return ret; 53799b199859SFilipe David Borba Manana } 538046204592SSage Weil case BTRFS_IOC_START_SYNC: 53819a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 538246204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 53832ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5384475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5385b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5386475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 53872ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5388475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 53892ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5390c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 53919ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5392837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 53932ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 539419a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 53952ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 53968ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 53978ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5398abccd00fSHugo Mills #ifdef CONFIG_64BIT 5399abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5400abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5401abccd00fSHugo Mills #endif 540231db9f7cSAlexander Block case BTRFS_IOC_SEND: 54039ad12305SSahil Kang return _btrfs_ioctl_send(inode, argp, false); 54042351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 54052351f431SJosef Bacik case BTRFS_IOC_SEND_32: 54069ad12305SSahil Kang return _btrfs_ioctl_send(inode, argp, true); 54072351f431SJosef Bacik #endif 5408c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 54092ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 54105d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5411905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 54125d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5413905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 54145d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5415905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 54165d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5417905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 54182f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 54192f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 54202f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 5421b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_status(fs_info, argp); 542257254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 5423b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_wait(fs_info, argp); 54243f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 54252ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 54262eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5427d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 54282eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 5429b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_features(fs_info, argp); 54302eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 54312eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5432b64ec075STomohiro Misono case BTRFS_IOC_GET_SUBVOL_INFO: 54339ad12305SSahil Kang return btrfs_ioctl_get_subvol_info(inode, argp); 543442e4b520STomohiro Misono case BTRFS_IOC_GET_SUBVOL_ROOTREF: 5435dc408ccdSSahil Kang return btrfs_ioctl_get_subvol_rootref(root, argp); 543623d0b79dSTomohiro Misono case BTRFS_IOC_INO_LOOKUP_USER: 543723d0b79dSTomohiro Misono return btrfs_ioctl_ino_lookup_user(file, argp); 543814605409SBoris Burkov case FS_IOC_ENABLE_VERITY: 543914605409SBoris Burkov return fsverity_ioctl_enable(file, (const void __user *)argp); 544014605409SBoris Burkov case FS_IOC_MEASURE_VERITY: 544114605409SBoris Burkov return fsverity_ioctl_measure(file, argp); 5442*1881fba8SOmar Sandoval case BTRFS_IOC_ENCODED_READ: 5443*1881fba8SOmar Sandoval return btrfs_ioctl_encoded_read(file, argp, false); 5444*1881fba8SOmar Sandoval #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 5445*1881fba8SOmar Sandoval case BTRFS_IOC_ENCODED_READ_32: 5446*1881fba8SOmar Sandoval return btrfs_ioctl_encoded_read(file, argp, true); 5447*1881fba8SOmar Sandoval #endif 5448f46b5a66SChristoph Hellwig } 5449f46b5a66SChristoph Hellwig 5450f46b5a66SChristoph Hellwig return -ENOTTY; 5451f46b5a66SChristoph Hellwig } 54524c63c245SLuke Dashjr 54534c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 54544c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 54554c63c245SLuke Dashjr { 54562a362249SJeff Mahoney /* 54572a362249SJeff Mahoney * These all access 32-bit values anyway so no further 54582a362249SJeff Mahoney * handling is necessary. 54592a362249SJeff Mahoney */ 54604c63c245SLuke Dashjr switch (cmd) { 54614c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 54624c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 54634c63c245SLuke Dashjr break; 54644c63c245SLuke Dashjr } 54654c63c245SLuke Dashjr 54664c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 54674c63c245SLuke Dashjr } 54684c63c245SLuke Dashjr #endif 5469