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> 31f46b5a66SChristoph Hellwig #include "ctree.h" 32f46b5a66SChristoph Hellwig #include "disk-io.h" 33949964c9SMarcos Paulo de Souza #include "export.h" 34f46b5a66SChristoph Hellwig #include "transaction.h" 35f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 36f46b5a66SChristoph Hellwig #include "print-tree.h" 37f46b5a66SChristoph Hellwig #include "volumes.h" 38925baeddSChris Mason #include "locking.h" 39d7728c96SJan Schmidt #include "backref.h" 40606686eeSJosef Bacik #include "rcu-string.h" 4131db9f7cSAlexander Block #include "send.h" 423f6bcfbdSStefan Behrens #include "dev-replace.h" 4363541927SFilipe David Borba Manana #include "props.h" 443b02a68aSJeff Mahoney #include "sysfs.h" 45fcebe456SJosef Bacik #include "qgroup.h" 461ec9a1aeSFilipe Manana #include "tree-log.h" 47ebb8765bSAnand Jain #include "compression.h" 488719aaaeSJosef Bacik #include "space-info.h" 4986736342SJosef Bacik #include "delalloc-space.h" 50aac0023cSJosef Bacik #include "block-group.h" 5122b398eeSQu Wenruo #include "subpage.h" 52f46b5a66SChristoph Hellwig 53abccd00fSHugo Mills #ifdef CONFIG_64BIT 54abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 55abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 56abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 57abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 58abccd00fSHugo Mills */ 59abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 60abccd00fSHugo Mills __u64 sec; 61abccd00fSHugo Mills __u32 nsec; 62abccd00fSHugo Mills } __attribute__ ((__packed__)); 63abccd00fSHugo Mills 64abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 65abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 66abccd00fSHugo Mills __u64 stransid; /* in */ 67abccd00fSHugo Mills __u64 rtransid; /* out */ 68abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 69abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 70abccd00fSHugo Mills __u64 flags; /* in */ 71abccd00fSHugo Mills __u64 reserved[16]; /* in */ 72abccd00fSHugo Mills } __attribute__ ((__packed__)); 73abccd00fSHugo Mills 74abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 75abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 76abccd00fSHugo Mills #endif 77abccd00fSHugo Mills 782351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 792351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 802351f431SJosef Bacik __s64 send_fd; /* in */ 812351f431SJosef Bacik __u64 clone_sources_count; /* in */ 822351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 832351f431SJosef Bacik __u64 parent_root; /* in */ 842351f431SJosef Bacik __u64 flags; /* in */ 85e77fbf99SDavid Sterba __u32 version; /* in */ 86e77fbf99SDavid Sterba __u8 reserved[28]; /* in */ 872351f431SJosef Bacik } __attribute__ ((__packed__)); 882351f431SJosef Bacik 892351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 902351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 912351f431SJosef Bacik #endif 92abccd00fSHugo Mills 936cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 941905a0f7SDavid Sterba static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, 951905a0f7SDavid Sterba unsigned int flags) 966cbff00fSChristoph Hellwig { 971905a0f7SDavid Sterba if (S_ISDIR(inode->i_mode)) 986cbff00fSChristoph Hellwig return flags; 991905a0f7SDavid Sterba else if (S_ISREG(inode->i_mode)) 1006cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 1016cbff00fSChristoph Hellwig else 1026cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1036cbff00fSChristoph Hellwig } 104f46b5a66SChristoph Hellwig 1056cbff00fSChristoph Hellwig /* 106a157d4fdSDavid Sterba * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS 107a157d4fdSDavid Sterba * ioctl. 1086cbff00fSChristoph Hellwig */ 10977eea05eSBoris Burkov static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode) 1106cbff00fSChristoph Hellwig { 1116cbff00fSChristoph Hellwig unsigned int iflags = 0; 11277eea05eSBoris Burkov u32 flags = binode->flags; 11314605409SBoris Burkov u32 ro_flags = binode->ro_flags; 1146cbff00fSChristoph Hellwig 1156cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1166cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1176cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1186cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1196cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1206cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1216cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1226cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1236cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1246cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1256cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1266cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 127d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 128d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 12914605409SBoris Burkov if (ro_flags & BTRFS_INODE_RO_VERITY) 13014605409SBoris Burkov iflags |= FS_VERITY_FL; 131d0092bddSLi Zefan 13213f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 133d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 13413f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 13513f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1366cbff00fSChristoph Hellwig 1376cbff00fSChristoph Hellwig return iflags; 1386cbff00fSChristoph Hellwig } 1396cbff00fSChristoph Hellwig 1406cbff00fSChristoph Hellwig /* 1416cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1426cbff00fSChristoph Hellwig */ 1437b6a221eSDavid Sterba void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) 1446cbff00fSChristoph Hellwig { 1455c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1463cc79392SFilipe Manana unsigned int new_fl = 0; 1476cbff00fSChristoph Hellwig 1485c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_SYNC) 1493cc79392SFilipe Manana new_fl |= S_SYNC; 1505c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_IMMUTABLE) 1513cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1525c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_APPEND) 1533cc79392SFilipe Manana new_fl |= S_APPEND; 1545c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_NOATIME) 1553cc79392SFilipe Manana new_fl |= S_NOATIME; 1565c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_DIRSYNC) 1573cc79392SFilipe Manana new_fl |= S_DIRSYNC; 15814605409SBoris Burkov if (binode->ro_flags & BTRFS_INODE_RO_VERITY) 15914605409SBoris Burkov new_fl |= S_VERITY; 1603cc79392SFilipe Manana 1613cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 16214605409SBoris Burkov S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC | 16314605409SBoris Burkov S_VERITY, new_fl); 1646cbff00fSChristoph Hellwig } 1656cbff00fSChristoph Hellwig 166f37c563bSDavid Sterba /* 167f37c563bSDavid Sterba * Check if @flags are a supported and valid set of FS_*_FL flags and that 168f37c563bSDavid Sterba * the old and new flags are not conflicting 169f37c563bSDavid Sterba */ 170f37c563bSDavid Sterba static int check_fsflags(unsigned int old_flags, unsigned int flags) 17175e7cb7fSLiu Bo { 17275e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 17375e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 17475e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 175e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 176e1e8fb6aSLi Zefan FS_NOCOW_FL)) 17775e7cb7fSLiu Bo return -EOPNOTSUPP; 17875e7cb7fSLiu Bo 179f37c563bSDavid Sterba /* COMPR and NOCOMP on new/old are valid */ 18075e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 18175e7cb7fSLiu Bo return -EINVAL; 18275e7cb7fSLiu Bo 183f37c563bSDavid Sterba if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL)) 184f37c563bSDavid Sterba return -EINVAL; 185f37c563bSDavid Sterba 186f37c563bSDavid Sterba /* NOCOW and compression options are mutually exclusive */ 187f37c563bSDavid Sterba if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL))) 188f37c563bSDavid Sterba return -EINVAL; 189f37c563bSDavid Sterba if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL))) 190f37c563bSDavid Sterba return -EINVAL; 191f37c563bSDavid Sterba 19275e7cb7fSLiu Bo return 0; 19375e7cb7fSLiu Bo } 19475e7cb7fSLiu Bo 195d206e9c9SNaohiro Aota static int check_fsflags_compatible(struct btrfs_fs_info *fs_info, 196d206e9c9SNaohiro Aota unsigned int flags) 197d206e9c9SNaohiro Aota { 198d206e9c9SNaohiro Aota if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL)) 199d206e9c9SNaohiro Aota return -EPERM; 200d206e9c9SNaohiro Aota 201d206e9c9SNaohiro Aota return 0; 202d206e9c9SNaohiro Aota } 203d206e9c9SNaohiro Aota 20497fc2977SMiklos Szeredi /* 20597fc2977SMiklos Szeredi * Set flags/xflags from the internal inode flags. The remaining items of 20697fc2977SMiklos Szeredi * fsxattr are zeroed. 20797fc2977SMiklos Szeredi */ 20897fc2977SMiklos Szeredi int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) 2096cbff00fSChristoph Hellwig { 21097fc2977SMiklos Szeredi struct btrfs_inode *binode = BTRFS_I(d_inode(dentry)); 21197fc2977SMiklos Szeredi 21277eea05eSBoris Burkov fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode)); 21397fc2977SMiklos Szeredi return 0; 21497fc2977SMiklos Szeredi } 21597fc2977SMiklos Szeredi 21697fc2977SMiklos Szeredi int btrfs_fileattr_set(struct user_namespace *mnt_userns, 21797fc2977SMiklos Szeredi struct dentry *dentry, struct fileattr *fa) 21897fc2977SMiklos Szeredi { 21997fc2977SMiklos Szeredi struct inode *inode = d_inode(dentry); 2200b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2215c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 2225c57b8b6SDavid Sterba struct btrfs_root *root = binode->root; 2236cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2245aca2842SDarrick J. Wong unsigned int fsflags, old_fsflags; 2256cbff00fSChristoph Hellwig int ret; 226ff9fef55SAnand Jain const char *comp = NULL; 227f37c563bSDavid Sterba u32 binode_flags; 2286cbff00fSChristoph Hellwig 229b83cc969SLi Zefan if (btrfs_root_readonly(root)) 230b83cc969SLi Zefan return -EROFS; 231b83cc969SLi Zefan 23297fc2977SMiklos Szeredi if (fileattr_has_fsx(fa)) 23397fc2977SMiklos Szeredi return -EOPNOTSUPP; 2346cbff00fSChristoph Hellwig 23597fc2977SMiklos Szeredi fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags); 23677eea05eSBoris Burkov old_fsflags = btrfs_inode_flags_to_fsflags(binode); 23797fc2977SMiklos Szeredi ret = check_fsflags(old_fsflags, fsflags); 238e7848683SJan Kara if (ret) 239e7848683SJan Kara return ret; 240e7848683SJan Kara 241d206e9c9SNaohiro Aota ret = check_fsflags_compatible(fs_info, fsflags); 242d206e9c9SNaohiro Aota if (ret) 24397fc2977SMiklos Szeredi return ret; 244d206e9c9SNaohiro Aota 245f37c563bSDavid Sterba binode_flags = binode->flags; 2465c57b8b6SDavid Sterba if (fsflags & FS_SYNC_FL) 247d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_SYNC; 2486cbff00fSChristoph Hellwig else 249d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_SYNC; 2505c57b8b6SDavid Sterba if (fsflags & FS_IMMUTABLE_FL) 251d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_IMMUTABLE; 2526cbff00fSChristoph Hellwig else 253d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_IMMUTABLE; 2545c57b8b6SDavid Sterba if (fsflags & FS_APPEND_FL) 255d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_APPEND; 2566cbff00fSChristoph Hellwig else 257d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_APPEND; 2585c57b8b6SDavid Sterba if (fsflags & FS_NODUMP_FL) 259d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODUMP; 2606cbff00fSChristoph Hellwig else 261d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODUMP; 2625c57b8b6SDavid Sterba if (fsflags & FS_NOATIME_FL) 263d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOATIME; 2646cbff00fSChristoph Hellwig else 265d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOATIME; 26697fc2977SMiklos Szeredi 26797fc2977SMiklos Szeredi /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */ 26897fc2977SMiklos Szeredi if (!fa->flags_valid) { 26997fc2977SMiklos Szeredi /* 1 item for the inode */ 27097fc2977SMiklos Szeredi trans = btrfs_start_transaction(root, 1); 2719b8a233bSRitesh Harjani if (IS_ERR(trans)) 2729b8a233bSRitesh Harjani return PTR_ERR(trans); 27397fc2977SMiklos Szeredi goto update_flags; 27497fc2977SMiklos Szeredi } 27597fc2977SMiklos Szeredi 2765c57b8b6SDavid Sterba if (fsflags & FS_DIRSYNC_FL) 277d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_DIRSYNC; 2786cbff00fSChristoph Hellwig else 279d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_DIRSYNC; 2805c57b8b6SDavid Sterba if (fsflags & FS_NOCOW_FL) { 28144e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 2827e97b8daSDavid Sterba /* 2837e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2847e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2857e97b8daSDavid Sterba * status of the file and will not set it. 2867e97b8daSDavid Sterba */ 2877e97b8daSDavid Sterba if (inode->i_size == 0) 288d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW | 289d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM; 2907e97b8daSDavid Sterba } else { 291d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW; 2927e97b8daSDavid Sterba } 2937e97b8daSDavid Sterba } else { 2947e97b8daSDavid Sterba /* 29501327610SNicholas D Steeves * Revert back under same assumptions as above 2967e97b8daSDavid Sterba */ 29744e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 2987e97b8daSDavid Sterba if (inode->i_size == 0) 299d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_NODATACOW | 300d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM); 3017e97b8daSDavid Sterba } else { 302d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODATACOW; 3037e97b8daSDavid Sterba } 3047e97b8daSDavid Sterba } 3056cbff00fSChristoph Hellwig 30675e7cb7fSLiu Bo /* 30775e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 30875e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 30975e7cb7fSLiu Bo * things smaller. 31075e7cb7fSLiu Bo */ 3115c57b8b6SDavid Sterba if (fsflags & FS_NOCOMP_FL) { 312d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_COMPRESS; 313d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOCOMPRESS; 3145c57b8b6SDavid Sterba } else if (fsflags & FS_COMPR_FL) { 31563541927SFilipe David Borba Manana 31697fc2977SMiklos Szeredi if (IS_SWAPFILE(inode)) 31797fc2977SMiklos Szeredi return -ETXTBSY; 318eede2bf3SOmar Sandoval 319d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_COMPRESS; 320d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOCOMPRESS; 32163541927SFilipe David Borba Manana 32293370509SDavid Sterba comp = btrfs_compress_type2str(fs_info->compress_type); 32393370509SDavid Sterba if (!comp || comp[0] == 0) 32493370509SDavid Sterba comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); 325ebcb904dSLi Zefan } else { 326d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 32775e7cb7fSLiu Bo } 3286cbff00fSChristoph Hellwig 329ff9fef55SAnand Jain /* 330ff9fef55SAnand Jain * 1 for inode item 331ff9fef55SAnand Jain * 2 for properties 332ff9fef55SAnand Jain */ 333ff9fef55SAnand Jain trans = btrfs_start_transaction(root, 3); 33497fc2977SMiklos Szeredi if (IS_ERR(trans)) 33597fc2977SMiklos Szeredi return PTR_ERR(trans); 3366cbff00fSChristoph Hellwig 337ff9fef55SAnand Jain if (comp) { 338ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, 339ff9fef55SAnand Jain strlen(comp), 0); 340ff9fef55SAnand Jain if (ret) { 341ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 342ff9fef55SAnand Jain goto out_end_trans; 343ff9fef55SAnand Jain } 344ff9fef55SAnand Jain } else { 345ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 346ff9fef55SAnand Jain 0, 0); 347ff9fef55SAnand Jain if (ret && ret != -ENODATA) { 348ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 349ff9fef55SAnand Jain goto out_end_trans; 350ff9fef55SAnand Jain } 351ff9fef55SAnand Jain } 352ff9fef55SAnand Jain 35397fc2977SMiklos Szeredi update_flags: 354d2b8fcfeSAnand Jain binode->flags = binode_flags; 3557b6a221eSDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 3560c4d2d95SJosef Bacik inode_inc_iversion(inode); 357c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3589a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); 3596cbff00fSChristoph Hellwig 360ff9fef55SAnand Jain out_end_trans: 3613a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3622d4e6f6aSliubo return ret; 3636cbff00fSChristoph Hellwig } 3646cbff00fSChristoph Hellwig 3650d7ed32cSDavid Sterba /* 3660d7ed32cSDavid Sterba * Start exclusive operation @type, return true on success 3670d7ed32cSDavid Sterba */ 368c3e1f96cSGoldwyn Rodrigues bool btrfs_exclop_start(struct btrfs_fs_info *fs_info, 369c3e1f96cSGoldwyn Rodrigues enum btrfs_exclusive_operation type) 370c3e1f96cSGoldwyn Rodrigues { 3710d7ed32cSDavid Sterba bool ret = false; 3720d7ed32cSDavid Sterba 3730d7ed32cSDavid Sterba spin_lock(&fs_info->super_lock); 3740d7ed32cSDavid Sterba if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) { 3750d7ed32cSDavid Sterba fs_info->exclusive_operation = type; 3760d7ed32cSDavid Sterba ret = true; 3770d7ed32cSDavid Sterba } 3780d7ed32cSDavid Sterba spin_unlock(&fs_info->super_lock); 3790d7ed32cSDavid Sterba 3800d7ed32cSDavid Sterba return ret; 381c3e1f96cSGoldwyn Rodrigues } 382c3e1f96cSGoldwyn Rodrigues 383578bda9eSDavid Sterba /* 384578bda9eSDavid Sterba * Conditionally allow to enter the exclusive operation in case it's compatible 385578bda9eSDavid Sterba * with the running one. This must be paired with btrfs_exclop_start_unlock and 386578bda9eSDavid Sterba * btrfs_exclop_finish. 387578bda9eSDavid Sterba * 388578bda9eSDavid Sterba * Compatibility: 389578bda9eSDavid Sterba * - the same type is already running 390621a1ee1SNikolay Borisov * - when trying to add a device and balance has been paused 391578bda9eSDavid Sterba * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller 392578bda9eSDavid Sterba * must check the condition first that would allow none -> @type 393578bda9eSDavid Sterba */ 394578bda9eSDavid Sterba bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info, 395578bda9eSDavid Sterba enum btrfs_exclusive_operation type) 396578bda9eSDavid Sterba { 397578bda9eSDavid Sterba spin_lock(&fs_info->super_lock); 398621a1ee1SNikolay Borisov if (fs_info->exclusive_operation == type || 399621a1ee1SNikolay Borisov (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED && 400621a1ee1SNikolay Borisov type == BTRFS_EXCLOP_DEV_ADD)) 401578bda9eSDavid Sterba return true; 402578bda9eSDavid Sterba 403578bda9eSDavid Sterba spin_unlock(&fs_info->super_lock); 404578bda9eSDavid Sterba return false; 405578bda9eSDavid Sterba } 406578bda9eSDavid Sterba 407578bda9eSDavid Sterba void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info) 408578bda9eSDavid Sterba { 409578bda9eSDavid Sterba spin_unlock(&fs_info->super_lock); 410578bda9eSDavid Sterba } 411578bda9eSDavid Sterba 412c3e1f96cSGoldwyn Rodrigues void btrfs_exclop_finish(struct btrfs_fs_info *fs_info) 413c3e1f96cSGoldwyn Rodrigues { 4140d7ed32cSDavid Sterba spin_lock(&fs_info->super_lock); 415c3e1f96cSGoldwyn Rodrigues WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE); 4160d7ed32cSDavid Sterba spin_unlock(&fs_info->super_lock); 41766a2823cSGoldwyn Rodrigues sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation"); 418c3e1f96cSGoldwyn Rodrigues } 419c3e1f96cSGoldwyn Rodrigues 420efc0e69cSNikolay Borisov void btrfs_exclop_balance(struct btrfs_fs_info *fs_info, 421efc0e69cSNikolay Borisov enum btrfs_exclusive_operation op) 422efc0e69cSNikolay Borisov { 423efc0e69cSNikolay Borisov switch (op) { 424efc0e69cSNikolay Borisov case BTRFS_EXCLOP_BALANCE_PAUSED: 425efc0e69cSNikolay Borisov spin_lock(&fs_info->super_lock); 426efc0e69cSNikolay Borisov ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE || 427efc0e69cSNikolay Borisov fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD); 428efc0e69cSNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED; 429efc0e69cSNikolay Borisov spin_unlock(&fs_info->super_lock); 430efc0e69cSNikolay Borisov break; 431efc0e69cSNikolay Borisov case BTRFS_EXCLOP_BALANCE: 432efc0e69cSNikolay Borisov spin_lock(&fs_info->super_lock); 433efc0e69cSNikolay Borisov ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); 434efc0e69cSNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; 435efc0e69cSNikolay Borisov spin_unlock(&fs_info->super_lock); 436efc0e69cSNikolay Borisov break; 437efc0e69cSNikolay Borisov default: 438efc0e69cSNikolay Borisov btrfs_warn(fs_info, 439efc0e69cSNikolay Borisov "invalid exclop balance operation %d requested", op); 440efc0e69cSNikolay Borisov } 441efc0e69cSNikolay Borisov } 442efc0e69cSNikolay Borisov 4436cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 4446cbff00fSChristoph Hellwig { 445496ad9aaSAl Viro struct inode *inode = file_inode(file); 4466cbff00fSChristoph Hellwig 4476cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 4486cbff00fSChristoph Hellwig } 449f46b5a66SChristoph Hellwig 450b929c1d8SMarcos Paulo de Souza static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, 451b929c1d8SMarcos Paulo de Souza void __user *arg) 452f7039b1dSLi Dongyang { 453f7039b1dSLi Dongyang struct btrfs_device *device; 454f7039b1dSLi Dongyang struct request_queue *q; 455f7039b1dSLi Dongyang struct fstrim_range range; 456f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 457f7039b1dSLi Dongyang u64 num_devices = 0; 458f7039b1dSLi Dongyang int ret; 459f7039b1dSLi Dongyang 460f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 461f7039b1dSLi Dongyang return -EPERM; 462f7039b1dSLi Dongyang 463f35f06c3SFilipe Manana /* 4641cb3dc3fSNaohiro Aota * btrfs_trim_block_group() depends on space cache, which is not 4651cb3dc3fSNaohiro Aota * available in zoned filesystem. So, disallow fitrim on a zoned 4661cb3dc3fSNaohiro Aota * filesystem for now. 4671cb3dc3fSNaohiro Aota */ 4681cb3dc3fSNaohiro Aota if (btrfs_is_zoned(fs_info)) 4691cb3dc3fSNaohiro Aota return -EOPNOTSUPP; 4701cb3dc3fSNaohiro Aota 4711cb3dc3fSNaohiro Aota /* 472f35f06c3SFilipe Manana * If the fs is mounted with nologreplay, which requires it to be 473f35f06c3SFilipe Manana * mounted in RO mode as well, we can not allow discard on free space 474f35f06c3SFilipe Manana * inside block groups, because log trees refer to extents that are not 475f35f06c3SFilipe Manana * pinned in a block group's free space cache (pinning the extents is 476f35f06c3SFilipe Manana * precisely the first phase of replaying a log tree). 477f35f06c3SFilipe Manana */ 478f35f06c3SFilipe Manana if (btrfs_test_opt(fs_info, NOLOGREPLAY)) 479f35f06c3SFilipe Manana return -EROFS; 480f35f06c3SFilipe Manana 4811f78160cSXiao Guangrong rcu_read_lock(); 4821f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 4831f78160cSXiao Guangrong dev_list) { 484f7039b1dSLi Dongyang if (!device->bdev) 485f7039b1dSLi Dongyang continue; 486f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 487f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 488f7039b1dSLi Dongyang num_devices++; 48950d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 490f7039b1dSLi Dongyang minlen); 491f7039b1dSLi Dongyang } 492f7039b1dSLi Dongyang } 4931f78160cSXiao Guangrong rcu_read_unlock(); 494f4c697e6SLukas Czerner 495f7039b1dSLi Dongyang if (!num_devices) 496f7039b1dSLi Dongyang return -EOPNOTSUPP; 497f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 498f7039b1dSLi Dongyang return -EFAULT; 4996ba9fc8eSQu Wenruo 5006ba9fc8eSQu Wenruo /* 5016ba9fc8eSQu Wenruo * NOTE: Don't truncate the range using super->total_bytes. Bytenr of 5026ba9fc8eSQu Wenruo * block group is in the logical address space, which can be any 5036ba9fc8eSQu Wenruo * sectorsize aligned bytenr in the range [0, U64_MAX]. 5046ba9fc8eSQu Wenruo */ 5056ba9fc8eSQu Wenruo if (range.len < fs_info->sb->s_blocksize) 506f4c697e6SLukas Czerner return -EINVAL; 507f7039b1dSLi Dongyang 508f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 5092ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 510f7039b1dSLi Dongyang if (ret < 0) 511f7039b1dSLi Dongyang return ret; 512f7039b1dSLi Dongyang 513f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 514f7039b1dSLi Dongyang return -EFAULT; 515f7039b1dSLi Dongyang 516f7039b1dSLi Dongyang return 0; 517f7039b1dSLi Dongyang } 518f7039b1dSLi Dongyang 519e1f60a65SDavid Sterba int __pure btrfs_is_empty_uuid(u8 *uuid) 520dd5f9615SStefan Behrens { 52146e0f66aSChris Mason int i; 52246e0f66aSChris Mason 52346e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 52446e0f66aSChris Mason if (uuid[i]) 52546e0f66aSChris Mason return 0; 52646e0f66aSChris Mason } 52746e0f66aSChris Mason return 1; 528dd5f9615SStefan Behrens } 529dd5f9615SStefan Behrens 5304d4340c9SChristian Brauner static noinline int create_subvol(struct user_namespace *mnt_userns, 5314d4340c9SChristian Brauner struct inode *dir, struct dentry *dentry, 53252f75f4fSDavid Sterba const char *name, int namelen, 5338696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 534f46b5a66SChristoph Hellwig { 5350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 536f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 537f46b5a66SChristoph Hellwig struct btrfs_key key; 53849a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 539f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 540f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 541d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 54276dda93cSYan, Zheng struct btrfs_root *new_root; 543d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 54495582b00SDeepa Dinamani struct timespec64 cur_time = current_time(dir); 5455662344bSTsutomu Itoh struct inode *inode; 546f46b5a66SChristoph Hellwig int ret; 547f46b5a66SChristoph Hellwig int err; 5482dfb1e43SQu Wenruo dev_t anon_dev = 0; 549f46b5a66SChristoph Hellwig u64 objectid; 5503de4586cSChris Mason u64 index = 0; 551f46b5a66SChristoph Hellwig 55249a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 55349a3c4d9SDavid Sterba if (!root_item) 55449a3c4d9SDavid Sterba return -ENOMEM; 55549a3c4d9SDavid Sterba 556543068a2SNikolay Borisov ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid); 5572fbe8c8aSAl Viro if (ret) 55849a3c4d9SDavid Sterba goto fail_free; 5596a912213SJosef Bacik 5602dfb1e43SQu Wenruo ret = get_anon_bdev(&anon_dev); 5612dfb1e43SQu Wenruo if (ret < 0) 5622dfb1e43SQu Wenruo goto fail_free; 5632dfb1e43SQu Wenruo 564e09fe2d2SQu Wenruo /* 565e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 56601327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 567e09fe2d2SQu Wenruo */ 56849a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 56949a3c4d9SDavid Sterba ret = -ENOSPC; 57049a3c4d9SDavid Sterba goto fail_free; 57149a3c4d9SDavid Sterba } 572e09fe2d2SQu Wenruo 573d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 5749ed74f2dSJosef Bacik /* 575d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 576d5c12070SMiao Xie * of create_snapshot(). 5779ed74f2dSJosef Bacik */ 578c4c129dbSGu JinXiang ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); 579d5c12070SMiao Xie if (ret) 58049a3c4d9SDavid Sterba goto fail_free; 581f46b5a66SChristoph Hellwig 582d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 583d5c12070SMiao Xie if (IS_ERR(trans)) { 584d5c12070SMiao Xie ret = PTR_ERR(trans); 585e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &block_rsv); 58649a3c4d9SDavid Sterba goto fail_free; 587d5c12070SMiao Xie } 588d5c12070SMiao Xie trans->block_rsv = &block_rsv; 589d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 590f46b5a66SChristoph Hellwig 591a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); 5926f72c7e2SArne Jansen if (ret) 5936f72c7e2SArne Jansen goto fail; 5946f72c7e2SArne Jansen 5959631e4ccSJosef Bacik leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, 5969631e4ccSJosef Bacik BTRFS_NESTING_NORMAL); 5978e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 5988e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 5998e8a1e31SJosef Bacik goto fail; 6008e8a1e31SJosef Bacik } 601f46b5a66SChristoph Hellwig 602f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 603f46b5a66SChristoph Hellwig 60449a3c4d9SDavid Sterba inode_item = &root_item->inode; 6053cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 6063cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 6073cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 608da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 6090b246afaSJeff Mahoney fs_info->nodesize); 6103cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 611f46b5a66SChristoph Hellwig 61249a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 61349a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 6143cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 61508fe4db1SLi Zefan 61649a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 61749a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 61849a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 61949a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 62049a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 62149a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 622f46b5a66SChristoph Hellwig 62349a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 62449a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 625807fc790SAndy Shevchenko generate_random_guid(root_item->uuid); 62649a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 62749a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 62849a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 62949a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 63049a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 631f46b5a66SChristoph Hellwig 632925baeddSChris Mason btrfs_tree_unlock(leaf); 633f46b5a66SChristoph Hellwig 63469948022SNikolay Borisov btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID); 635f46b5a66SChristoph Hellwig 636f46b5a66SChristoph Hellwig key.objectid = objectid; 6375d4f98a2SYan Zheng key.offset = 0; 638962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 6390b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 64049a3c4d9SDavid Sterba root_item); 64167addf29SFilipe Manana if (ret) { 64267addf29SFilipe Manana /* 64367addf29SFilipe Manana * Since we don't abort the transaction in this case, free the 64467addf29SFilipe Manana * tree block so that we don't leak space and leave the 64567addf29SFilipe Manana * filesystem in an inconsistent state (an extent item in the 6467a163608SFilipe Manana * extent tree with a backreference for a root that does not 647212a58fdSFilipe Manana * exists). 64867addf29SFilipe Manana */ 649212a58fdSFilipe Manana btrfs_tree_lock(leaf); 650212a58fdSFilipe Manana btrfs_clean_tree_block(leaf); 651212a58fdSFilipe Manana btrfs_tree_unlock(leaf); 6527a163608SFilipe Manana btrfs_free_tree_block(trans, objectid, leaf, 0, 1); 65367addf29SFilipe Manana free_extent_buffer(leaf); 654f46b5a66SChristoph Hellwig goto fail; 65567addf29SFilipe Manana } 65667addf29SFilipe Manana 65767addf29SFilipe Manana free_extent_buffer(leaf); 65867addf29SFilipe Manana leaf = NULL; 659f46b5a66SChristoph Hellwig 66076dda93cSYan, Zheng key.offset = (u64)-1; 6612dfb1e43SQu Wenruo new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev); 66279787eaaSJeff Mahoney if (IS_ERR(new_root)) { 6632dfb1e43SQu Wenruo free_anon_bdev(anon_dev); 66479787eaaSJeff Mahoney ret = PTR_ERR(new_root); 66566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 66679787eaaSJeff Mahoney goto fail; 66779787eaaSJeff Mahoney } 6682dfb1e43SQu Wenruo /* Freeing will be done in btrfs_put_root() of new_root */ 6692dfb1e43SQu Wenruo anon_dev = 0; 67076dda93cSYan, Zheng 671221581e4SJosef Bacik ret = btrfs_record_root_in_trans(trans, new_root); 672221581e4SJosef Bacik if (ret) { 673221581e4SJosef Bacik btrfs_put_root(new_root); 674221581e4SJosef Bacik btrfs_abort_transaction(trans, ret); 675221581e4SJosef Bacik goto fail; 676221581e4SJosef Bacik } 67776dda93cSYan, Zheng 6784d4340c9SChristian Brauner ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns); 67900246528SJosef Bacik btrfs_put_root(new_root); 680ce598979SMark Fasheh if (ret) { 681ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 68266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 683ce598979SMark Fasheh goto fail; 684ce598979SMark Fasheh } 685ce598979SMark Fasheh 686f46b5a66SChristoph Hellwig /* 687f46b5a66SChristoph Hellwig * insert the directory item 688f46b5a66SChristoph Hellwig */ 689877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 69079787eaaSJeff Mahoney if (ret) { 69166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 69279787eaaSJeff Mahoney goto fail; 69379787eaaSJeff Mahoney } 6943de4586cSChris Mason 695684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, 6963de4586cSChris Mason BTRFS_FT_DIR, index); 69779787eaaSJeff Mahoney if (ret) { 69866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 699f46b5a66SChristoph Hellwig goto fail; 70079787eaaSJeff Mahoney } 7010660b5afSChris Mason 7026ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 7039a56fcd1SNikolay Borisov ret = btrfs_update_inode(trans, root, BTRFS_I(dir)); 704c7e54b51SJosef Bacik if (ret) { 705c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 706c7e54b51SJosef Bacik goto fail; 707c7e54b51SJosef Bacik } 70852c26179SYan Zheng 7096025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, 7104a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 711c7e54b51SJosef Bacik if (ret) { 712c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 713c7e54b51SJosef Bacik goto fail; 714c7e54b51SJosef Bacik } 7150660b5afSChris Mason 716cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, root_item->uuid, 7176bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 718dd5f9615SStefan Behrens if (ret) 71966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 720dd5f9615SStefan Behrens 721f46b5a66SChristoph Hellwig fail: 72249a3c4d9SDavid Sterba kfree(root_item); 723d5c12070SMiao Xie trans->block_rsv = NULL; 724d5c12070SMiao Xie trans->bytes_reserved = 0; 725e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &block_rsv); 726de6e8200SLiu Bo 7273a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 728f46b5a66SChristoph Hellwig if (err && !ret) 729f46b5a66SChristoph Hellwig ret = err; 7301a65e24bSChris Mason 7315662344bSTsutomu Itoh if (!ret) { 7325662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 733de6e8200SLiu Bo if (IS_ERR(inode)) 734de6e8200SLiu Bo return PTR_ERR(inode); 7355662344bSTsutomu Itoh d_instantiate(dentry, inode); 7365662344bSTsutomu Itoh } 737f46b5a66SChristoph Hellwig return ret; 73849a3c4d9SDavid Sterba 73949a3c4d9SDavid Sterba fail_free: 7402dfb1e43SQu Wenruo if (anon_dev) 7412dfb1e43SQu Wenruo free_anon_bdev(anon_dev); 74249a3c4d9SDavid Sterba kfree(root_item); 74349a3c4d9SDavid Sterba return ret; 744f46b5a66SChristoph Hellwig } 745f46b5a66SChristoph Hellwig 746e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 7479babda9fSNikolay Borisov struct dentry *dentry, bool readonly, 748e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 749f46b5a66SChristoph Hellwig { 7500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 7512e4bfab9SYan, Zheng struct inode *inode; 752f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 753f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 7542e4bfab9SYan, Zheng int ret; 755f46b5a66SChristoph Hellwig 75692a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 757f46b5a66SChristoph Hellwig return -EINVAL; 758f46b5a66SChristoph Hellwig 759eede2bf3SOmar Sandoval if (atomic_read(&root->nr_swapfiles)) { 760eede2bf3SOmar Sandoval btrfs_warn(fs_info, 761eede2bf3SOmar Sandoval "cannot snapshot subvolume with active swapfile"); 762eede2bf3SOmar Sandoval return -ETXTBSY; 763eede2bf3SOmar Sandoval } 764eede2bf3SOmar Sandoval 76523269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 766a1ee7362SDavid Sterba if (!pending_snapshot) 767a1ee7362SDavid Sterba return -ENOMEM; 768a1ee7362SDavid Sterba 7692dfb1e43SQu Wenruo ret = get_anon_bdev(&pending_snapshot->anon_dev); 7702dfb1e43SQu Wenruo if (ret < 0) 7712dfb1e43SQu Wenruo goto free_pending; 772b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 77323269bf5SDavid Sterba GFP_KERNEL); 7748546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 7758546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 776b0c0ea63SDavid Sterba ret = -ENOMEM; 777b0c0ea63SDavid Sterba goto free_pending; 778b0c0ea63SDavid Sterba } 779b0c0ea63SDavid Sterba 78066d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 78166d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 782d5c12070SMiao Xie /* 783d5c12070SMiao Xie * 1 - parent dir inode 784d5c12070SMiao Xie * 2 - dir entries 785d5c12070SMiao Xie * 1 - root item 786d5c12070SMiao Xie * 2 - root ref/backref 787d5c12070SMiao Xie * 1 - root of snapshot 788dd5f9615SStefan Behrens * 1 - UUID item 789d5c12070SMiao Xie */ 790d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 791dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 792ee3441b4SJeff Mahoney false); 793d5c12070SMiao Xie if (ret) 794c11fbb6eSRobbie Ko goto free_pending; 795d5c12070SMiao Xie 796a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 797a22285a6SYan, Zheng pending_snapshot->root = root; 798b83cc969SLi Zefan pending_snapshot->readonly = readonly; 799e9662f70SMiao Xie pending_snapshot->dir = dir; 8008696c533SMiao Xie pending_snapshot->inherit = inherit; 801a22285a6SYan, Zheng 802d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 803a22285a6SYan, Zheng if (IS_ERR(trans)) { 804a22285a6SYan, Zheng ret = PTR_ERR(trans); 805a22285a6SYan, Zheng goto fail; 806a22285a6SYan, Zheng } 807a22285a6SYan, Zheng 8080b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 809a22285a6SYan, Zheng list_add(&pending_snapshot->list, 810a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 8110b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 8129babda9fSNikolay Borisov 8133a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 814aec8030aSMiao Xie if (ret) 815c37b2b62SJosef Bacik goto fail; 816a22285a6SYan, Zheng 817a22285a6SYan, Zheng ret = pending_snapshot->error; 818f46b5a66SChristoph Hellwig if (ret) 8192e4bfab9SYan, Zheng goto fail; 820f46b5a66SChristoph Hellwig 821d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 822d3797308SChris Mason if (ret) 823d3797308SChris Mason goto fail; 824d3797308SChris Mason 8252b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 8262e4bfab9SYan, Zheng if (IS_ERR(inode)) { 8272e4bfab9SYan, Zheng ret = PTR_ERR(inode); 8282e4bfab9SYan, Zheng goto fail; 8292e4bfab9SYan, Zheng } 8305662344bSTsutomu Itoh 8312e4bfab9SYan, Zheng d_instantiate(dentry, inode); 8322e4bfab9SYan, Zheng ret = 0; 8332dfb1e43SQu Wenruo pending_snapshot->anon_dev = 0; 8342e4bfab9SYan, Zheng fail: 8352dfb1e43SQu Wenruo /* Prevent double freeing of anon_dev */ 8362dfb1e43SQu Wenruo if (ret && pending_snapshot->snap) 8372dfb1e43SQu Wenruo pending_snapshot->snap->anon_dev = 0; 83800246528SJosef Bacik btrfs_put_root(pending_snapshot->snap); 839e85fde51SQu Wenruo btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv); 840b0c0ea63SDavid Sterba free_pending: 8412dfb1e43SQu Wenruo if (pending_snapshot->anon_dev) 8422dfb1e43SQu Wenruo free_anon_bdev(pending_snapshot->anon_dev); 843b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 8448546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 845a1ee7362SDavid Sterba kfree(pending_snapshot); 846a1ee7362SDavid Sterba 847f46b5a66SChristoph Hellwig return ret; 848f46b5a66SChristoph Hellwig } 849f46b5a66SChristoph Hellwig 8504260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 8514260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 8524260f7c7SSage Weil * whether the type of victim is right. 8534260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 8544260f7c7SSage Weil * 2. We should have write and exec permissions on dir 8554260f7c7SSage Weil * 3. We can't remove anything from append-only dir 8564260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 8574260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 8584260f7c7SSage Weil * a. be owner of dir, or 8594260f7c7SSage Weil * b. be owner of victim, or 8604260f7c7SSage Weil * c. have CAP_FOWNER capability 86101327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 8624260f7c7SSage Weil * links pointing to it. 8634260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 8644260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 8654260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 8664260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 8674260f7c7SSage Weil * nfs_async_unlink(). 8684260f7c7SSage Weil */ 8694260f7c7SSage Weil 870c4ed533bSChristian Brauner static int btrfs_may_delete(struct user_namespace *mnt_userns, 871c4ed533bSChristian Brauner struct inode *dir, struct dentry *victim, int isdir) 8724260f7c7SSage Weil { 8734260f7c7SSage Weil int error; 8744260f7c7SSage Weil 8752b0143b5SDavid Howells if (d_really_is_negative(victim)) 8764260f7c7SSage Weil return -ENOENT; 8774260f7c7SSage Weil 8782b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 8794fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 8804260f7c7SSage Weil 881c4ed533bSChristian Brauner error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 8824260f7c7SSage Weil if (error) 8834260f7c7SSage Weil return error; 8844260f7c7SSage Weil if (IS_APPEND(dir)) 8854260f7c7SSage Weil return -EPERM; 886c4ed533bSChristian Brauner if (check_sticky(mnt_userns, dir, d_inode(victim)) || 887ba73d987SChristian Brauner IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) || 888ba73d987SChristian Brauner IS_SWAPFILE(d_inode(victim))) 8894260f7c7SSage Weil return -EPERM; 8904260f7c7SSage Weil if (isdir) { 891e36cb0b8SDavid Howells if (!d_is_dir(victim)) 8924260f7c7SSage Weil return -ENOTDIR; 8934260f7c7SSage Weil if (IS_ROOT(victim)) 8944260f7c7SSage Weil return -EBUSY; 895e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 8964260f7c7SSage Weil return -EISDIR; 8974260f7c7SSage Weil if (IS_DEADDIR(dir)) 8984260f7c7SSage Weil return -ENOENT; 8994260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 9004260f7c7SSage Weil return -EBUSY; 9014260f7c7SSage Weil return 0; 9024260f7c7SSage Weil } 9034260f7c7SSage Weil 904cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 9054d4340c9SChristian Brauner static inline int btrfs_may_create(struct user_namespace *mnt_userns, 9064d4340c9SChristian Brauner struct inode *dir, struct dentry *child) 907cb8e7090SChristoph Hellwig { 9082b0143b5SDavid Howells if (d_really_is_positive(child)) 909cb8e7090SChristoph Hellwig return -EEXIST; 910cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 911cb8e7090SChristoph Hellwig return -ENOENT; 9124d4340c9SChristian Brauner if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) 9135474bf40SChristian Brauner return -EOVERFLOW; 9144d4340c9SChristian Brauner return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); 915cb8e7090SChristoph Hellwig } 916cb8e7090SChristoph Hellwig 917cb8e7090SChristoph Hellwig /* 918cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 919cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 920cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 921cb8e7090SChristoph Hellwig */ 92292872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 9234d4340c9SChristian Brauner struct user_namespace *mnt_userns, 92452f75f4fSDavid Sterba const char *name, int namelen, 92572fd032eSSage Weil struct btrfs_root *snap_src, 9269babda9fSNikolay Borisov bool readonly, 9278696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 928cb8e7090SChristoph Hellwig { 9292b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 9300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 931cb8e7090SChristoph Hellwig struct dentry *dentry; 932cb8e7090SChristoph Hellwig int error; 933cb8e7090SChristoph Hellwig 93400235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 93500235411SAl Viro if (error == -EINTR) 93600235411SAl Viro return error; 937cb8e7090SChristoph Hellwig 9384d4340c9SChristian Brauner dentry = lookup_one(mnt_userns, name, parent->dentry, namelen); 939cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 940cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 941cb8e7090SChristoph Hellwig goto out_unlock; 942cb8e7090SChristoph Hellwig 9434d4340c9SChristian Brauner error = btrfs_may_create(mnt_userns, dir, dentry); 944cb8e7090SChristoph Hellwig if (error) 945a874a63eSLiu Bo goto out_dput; 946cb8e7090SChristoph Hellwig 9479c52057cSChris Mason /* 9489c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 9499c52057cSChris Mason * check for them now when we can safely fail 9509c52057cSChris Mason */ 9519c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 9529c52057cSChris Mason dir->i_ino, name, 9539c52057cSChris Mason namelen); 9549c52057cSChris Mason if (error) 9559c52057cSChris Mason goto out_dput; 9569c52057cSChris Mason 9570b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 95876dda93cSYan, Zheng 95976dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 96076dda93cSYan, Zheng goto out_up_read; 96176dda93cSYan, Zheng 9629babda9fSNikolay Borisov if (snap_src) 9639babda9fSNikolay Borisov error = create_snapshot(snap_src, dir, dentry, readonly, inherit); 9649babda9fSNikolay Borisov else 9654d4340c9SChristian Brauner error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit); 9669babda9fSNikolay Borisov 96776dda93cSYan, Zheng if (!error) 96876dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 96976dda93cSYan, Zheng out_up_read: 9700b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 971cb8e7090SChristoph Hellwig out_dput: 972cb8e7090SChristoph Hellwig dput(dentry); 973cb8e7090SChristoph Hellwig out_unlock: 97464708539SJosef Bacik btrfs_inode_unlock(dir, 0); 975cb8e7090SChristoph Hellwig return error; 976cb8e7090SChristoph Hellwig } 977cb8e7090SChristoph Hellwig 978c11fbb6eSRobbie Ko static noinline int btrfs_mksnapshot(const struct path *parent, 9794d4340c9SChristian Brauner struct user_namespace *mnt_userns, 980c11fbb6eSRobbie Ko const char *name, int namelen, 981c11fbb6eSRobbie Ko struct btrfs_root *root, 982c11fbb6eSRobbie Ko bool readonly, 983c11fbb6eSRobbie Ko struct btrfs_qgroup_inherit *inherit) 984c11fbb6eSRobbie Ko { 985c11fbb6eSRobbie Ko int ret; 986c11fbb6eSRobbie Ko bool snapshot_force_cow = false; 987c11fbb6eSRobbie Ko 988c11fbb6eSRobbie Ko /* 989c11fbb6eSRobbie Ko * Force new buffered writes to reserve space even when NOCOW is 990c11fbb6eSRobbie Ko * possible. This is to avoid later writeback (running dealloc) to 991c11fbb6eSRobbie Ko * fallback to COW mode and unexpectedly fail with ENOSPC. 992c11fbb6eSRobbie Ko */ 993c11fbb6eSRobbie Ko btrfs_drew_read_lock(&root->snapshot_lock); 994c11fbb6eSRobbie Ko 995f9baa501SFilipe Manana ret = btrfs_start_delalloc_snapshot(root, false); 996c11fbb6eSRobbie Ko if (ret) 997c11fbb6eSRobbie Ko goto out; 998c11fbb6eSRobbie Ko 999c11fbb6eSRobbie Ko /* 1000c11fbb6eSRobbie Ko * All previous writes have started writeback in NOCOW mode, so now 1001c11fbb6eSRobbie Ko * we force future writes to fallback to COW mode during snapshot 1002c11fbb6eSRobbie Ko * creation. 1003c11fbb6eSRobbie Ko */ 1004c11fbb6eSRobbie Ko atomic_inc(&root->snapshot_force_cow); 1005c11fbb6eSRobbie Ko snapshot_force_cow = true; 1006c11fbb6eSRobbie Ko 1007c11fbb6eSRobbie Ko btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 1008c11fbb6eSRobbie Ko 10094d4340c9SChristian Brauner ret = btrfs_mksubvol(parent, mnt_userns, name, namelen, 1010c11fbb6eSRobbie Ko root, readonly, inherit); 1011c11fbb6eSRobbie Ko out: 1012c11fbb6eSRobbie Ko if (snapshot_force_cow) 1013c11fbb6eSRobbie Ko atomic_dec(&root->snapshot_force_cow); 1014c11fbb6eSRobbie Ko btrfs_drew_read_unlock(&root->snapshot_lock); 1015c11fbb6eSRobbie Ko return ret; 1016c11fbb6eSRobbie Ko } 1017c11fbb6eSRobbie Ko 1018e9eec721SQu Wenruo static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, 1019e9eec721SQu Wenruo bool locked) 102017ce6ef8SLiu Bo { 102117ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1022940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 10236c282eb4SLi Zefan struct extent_map *em; 102476068caeSQu Wenruo const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize; 1025940100a4SChris Mason 1026940100a4SChris Mason /* 1027940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1028940100a4SChris Mason * the full extent lock 1029940100a4SChris Mason */ 1030940100a4SChris Mason read_lock(&em_tree->lock); 103176068caeSQu Wenruo em = lookup_extent_mapping(em_tree, start, sectorsize); 1032940100a4SChris Mason read_unlock(&em_tree->lock); 1033940100a4SChris Mason 1034940100a4SChris Mason if (!em) { 1035308d9800SFilipe Manana struct extent_state *cached = NULL; 103676068caeSQu Wenruo u64 end = start + sectorsize - 1; 1037308d9800SFilipe Manana 1038940100a4SChris Mason /* get the big lock and read metadata off disk */ 1039e9eec721SQu Wenruo if (!locked) 1040ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 104176068caeSQu Wenruo em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize); 1042e9eec721SQu Wenruo if (!locked) 1043e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 1044940100a4SChris Mason 10456cf8bfbfSDan Carpenter if (IS_ERR(em)) 10466c282eb4SLi Zefan return NULL; 1047940100a4SChris Mason } 1048940100a4SChris Mason 10496c282eb4SLi Zefan return em; 10506c282eb4SLi Zefan } 10516c282eb4SLi Zefan 1052e9eec721SQu Wenruo static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, 1053e9eec721SQu Wenruo bool locked) 10546c282eb4SLi Zefan { 10556c282eb4SLi Zefan struct extent_map *next; 10566c282eb4SLi Zefan bool ret = true; 10576c282eb4SLi Zefan 10586c282eb4SLi Zefan /* this is the last extent */ 10596c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 10606c282eb4SLi Zefan return false; 10616c282eb4SLi Zefan 1062e9eec721SQu Wenruo next = defrag_lookup_extent(inode, em->start + em->len, locked); 1063e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1064e9512d72SChris Mason ret = false; 1065e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1066ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 10676c282eb4SLi Zefan ret = false; 10686c282eb4SLi Zefan 10696c282eb4SLi Zefan free_extent_map(next); 10706c282eb4SLi Zefan return ret; 10716c282eb4SLi Zefan } 10726c282eb4SLi Zefan 10734cb5300bSChris Mason /* 10745767b50cSQu Wenruo * Prepare one page to be defragged. 10755767b50cSQu Wenruo * 10765767b50cSQu Wenruo * This will ensure: 10775767b50cSQu Wenruo * 10785767b50cSQu Wenruo * - Returned page is locked and has been set up properly. 10795767b50cSQu Wenruo * - No ordered extent exists in the page. 10805767b50cSQu Wenruo * - The page is uptodate. 10815767b50cSQu Wenruo * 10825767b50cSQu Wenruo * NOTE: Caller should also wait for page writeback after the cluster is 10835767b50cSQu Wenruo * prepared, here we don't do writeback wait for each page. 10845767b50cSQu Wenruo */ 10855767b50cSQu Wenruo static struct page *defrag_prepare_one_page(struct btrfs_inode *inode, 10865767b50cSQu Wenruo pgoff_t index) 1087940100a4SChris Mason { 10885767b50cSQu Wenruo struct address_space *mapping = inode->vfs_inode.i_mapping; 10895767b50cSQu Wenruo gfp_t mask = btrfs_alloc_write_mask(mapping); 10905767b50cSQu Wenruo u64 page_start = (u64)index << PAGE_SHIFT; 10915767b50cSQu Wenruo u64 page_end = page_start + PAGE_SIZE - 1; 10924cb5300bSChris Mason struct extent_state *cached_state = NULL; 10934cb5300bSChris Mason struct page *page; 10945767b50cSQu Wenruo int ret; 10955767b50cSQu Wenruo 1096600a45e1SMiao Xie again: 10975767b50cSQu Wenruo page = find_or_create_page(mapping, index, mask); 10984cb5300bSChris Mason if (!page) 10995767b50cSQu Wenruo return ERR_PTR(-ENOMEM); 11005767b50cSQu Wenruo 110124bcb454SOmar Sandoval /* 110224bcb454SOmar Sandoval * Since we can defragment files opened read-only, we can encounter 110324bcb454SOmar Sandoval * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We 110424bcb454SOmar Sandoval * can't do I/O using huge pages yet, so return an error for now. 110524bcb454SOmar Sandoval * Filesystem transparent huge pages are typically only used for 110624bcb454SOmar Sandoval * executables that explicitly enable them, so this isn't very 110724bcb454SOmar Sandoval * restrictive. 110824bcb454SOmar Sandoval */ 110924bcb454SOmar Sandoval if (PageCompound(page)) { 111024bcb454SOmar Sandoval unlock_page(page); 111124bcb454SOmar Sandoval put_page(page); 111224bcb454SOmar Sandoval return ERR_PTR(-ETXTBSY); 111324bcb454SOmar Sandoval } 11144cb5300bSChris Mason 111532443de3SQu Wenruo ret = set_page_extent_mapped(page); 111632443de3SQu Wenruo if (ret < 0) { 111732443de3SQu Wenruo unlock_page(page); 111832443de3SQu Wenruo put_page(page); 11195767b50cSQu Wenruo return ERR_PTR(ret); 112032443de3SQu Wenruo } 112132443de3SQu Wenruo 11225767b50cSQu Wenruo /* Wait for any existing ordered extent in the range */ 1123600a45e1SMiao Xie while (1) { 11245767b50cSQu Wenruo struct btrfs_ordered_extent *ordered; 11255767b50cSQu Wenruo 11265767b50cSQu Wenruo lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); 11275767b50cSQu Wenruo ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); 11285767b50cSQu Wenruo unlock_extent_cached(&inode->io_tree, page_start, page_end, 1129e43bbe5eSDavid Sterba &cached_state); 1130600a45e1SMiao Xie if (!ordered) 1131600a45e1SMiao Xie break; 1132600a45e1SMiao Xie 1133600a45e1SMiao Xie unlock_page(page); 1134c0a43603SNikolay Borisov btrfs_start_ordered_extent(ordered, 1); 1135600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1136600a45e1SMiao Xie lock_page(page); 11371f12bd06SLiu Bo /* 11385767b50cSQu Wenruo * We unlocked the page above, so we need check if it was 11395767b50cSQu Wenruo * released or not. 11401f12bd06SLiu Bo */ 11415767b50cSQu Wenruo if (page->mapping != mapping || !PagePrivate(page)) { 11421f12bd06SLiu Bo unlock_page(page); 114309cbfeafSKirill A. Shutemov put_page(page); 11441f12bd06SLiu Bo goto again; 11451f12bd06SLiu Bo } 1146600a45e1SMiao Xie } 1147600a45e1SMiao Xie 11485767b50cSQu Wenruo /* 11495767b50cSQu Wenruo * Now the page range has no ordered extent any more. Read the page to 11505767b50cSQu Wenruo * make it uptodate. 11515767b50cSQu Wenruo */ 11524cb5300bSChris Mason if (!PageUptodate(page)) { 11534cb5300bSChris Mason btrfs_readpage(NULL, page); 11544cb5300bSChris Mason lock_page(page); 11555767b50cSQu Wenruo if (page->mapping != mapping || !PagePrivate(page)) { 1156600a45e1SMiao Xie unlock_page(page); 115709cbfeafSKirill A. Shutemov put_page(page); 1158600a45e1SMiao Xie goto again; 1159600a45e1SMiao Xie } 11605767b50cSQu Wenruo if (!PageUptodate(page)) { 11615767b50cSQu Wenruo unlock_page(page); 11625767b50cSQu Wenruo put_page(page); 11635767b50cSQu Wenruo return ERR_PTR(-EIO); 11644cb5300bSChris Mason } 11655767b50cSQu Wenruo } 11665767b50cSQu Wenruo return page; 11675767b50cSQu Wenruo } 11684cb5300bSChris Mason 1169eb793cf8SQu Wenruo struct defrag_target_range { 1170eb793cf8SQu Wenruo struct list_head list; 1171eb793cf8SQu Wenruo u64 start; 1172eb793cf8SQu Wenruo u64 len; 1173eb793cf8SQu Wenruo }; 11744cb5300bSChris Mason 11754cb5300bSChris Mason /* 1176eb793cf8SQu Wenruo * Collect all valid target extents. 1177eb793cf8SQu Wenruo * 1178eb793cf8SQu Wenruo * @start: file offset to lookup 1179eb793cf8SQu Wenruo * @len: length to lookup 1180eb793cf8SQu Wenruo * @extent_thresh: file extent size threshold, any extent size >= this value 1181eb793cf8SQu Wenruo * will be ignored 1182eb793cf8SQu Wenruo * @newer_than: only defrag extents newer than this value 1183eb793cf8SQu Wenruo * @do_compress: whether the defrag is doing compression 1184eb793cf8SQu Wenruo * if true, @extent_thresh will be ignored and all regular 1185eb793cf8SQu Wenruo * file extents meeting @newer_than will be targets. 1186e9eec721SQu Wenruo * @locked: if the range has already held extent lock 1187eb793cf8SQu Wenruo * @target_list: list of targets file extents 11884cb5300bSChris Mason */ 1189eb793cf8SQu Wenruo static int defrag_collect_targets(struct btrfs_inode *inode, 1190eb793cf8SQu Wenruo u64 start, u64 len, u32 extent_thresh, 1191eb793cf8SQu Wenruo u64 newer_than, bool do_compress, 1192e9eec721SQu Wenruo bool locked, struct list_head *target_list) 1193eb793cf8SQu Wenruo { 1194eb793cf8SQu Wenruo u64 cur = start; 1195eb793cf8SQu Wenruo int ret = 0; 1196eb793cf8SQu Wenruo 1197eb793cf8SQu Wenruo while (cur < start + len) { 1198eb793cf8SQu Wenruo struct extent_map *em; 1199eb793cf8SQu Wenruo struct defrag_target_range *new; 1200eb793cf8SQu Wenruo bool next_mergeable = true; 1201eb793cf8SQu Wenruo u64 range_len; 1202eb793cf8SQu Wenruo 1203e9eec721SQu Wenruo em = defrag_lookup_extent(&inode->vfs_inode, cur, locked); 1204eb793cf8SQu Wenruo if (!em) 1205eb793cf8SQu Wenruo break; 1206eb793cf8SQu Wenruo 1207eb793cf8SQu Wenruo /* Skip hole/inline/preallocated extents */ 1208eb793cf8SQu Wenruo if (em->block_start >= EXTENT_MAP_LAST_BYTE || 1209eb793cf8SQu Wenruo test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) 1210eb793cf8SQu Wenruo goto next; 1211eb793cf8SQu Wenruo 1212eb793cf8SQu Wenruo /* Skip older extent */ 1213eb793cf8SQu Wenruo if (em->generation < newer_than) 1214eb793cf8SQu Wenruo goto next; 1215eb793cf8SQu Wenruo 1216eb793cf8SQu Wenruo /* 1217eb793cf8SQu Wenruo * For do_compress case, we want to compress all valid file 1218eb793cf8SQu Wenruo * extents, thus no @extent_thresh or mergeable check. 1219eb793cf8SQu Wenruo */ 1220eb793cf8SQu Wenruo if (do_compress) 1221eb793cf8SQu Wenruo goto add; 1222eb793cf8SQu Wenruo 1223eb793cf8SQu Wenruo /* Skip too large extent */ 1224eb793cf8SQu Wenruo if (em->len >= extent_thresh) 1225eb793cf8SQu Wenruo goto next; 1226eb793cf8SQu Wenruo 1227e9eec721SQu Wenruo next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em, 1228e9eec721SQu Wenruo locked); 1229eb793cf8SQu Wenruo if (!next_mergeable) { 1230eb793cf8SQu Wenruo struct defrag_target_range *last; 1231eb793cf8SQu Wenruo 1232eb793cf8SQu Wenruo /* Empty target list, no way to merge with last entry */ 1233eb793cf8SQu Wenruo if (list_empty(target_list)) 1234eb793cf8SQu Wenruo goto next; 1235eb793cf8SQu Wenruo last = list_entry(target_list->prev, 1236eb793cf8SQu Wenruo struct defrag_target_range, list); 1237eb793cf8SQu Wenruo /* Not mergeable with last entry */ 1238eb793cf8SQu Wenruo if (last->start + last->len != cur) 1239eb793cf8SQu Wenruo goto next; 1240eb793cf8SQu Wenruo 1241eb793cf8SQu Wenruo /* Mergeable, fall through to add it to @target_list. */ 1242eb793cf8SQu Wenruo } 1243eb793cf8SQu Wenruo 1244eb793cf8SQu Wenruo add: 1245eb793cf8SQu Wenruo range_len = min(extent_map_end(em), start + len) - cur; 1246eb793cf8SQu Wenruo /* 1247eb793cf8SQu Wenruo * This one is a good target, check if it can be merged into 1248eb793cf8SQu Wenruo * last range of the target list. 1249eb793cf8SQu Wenruo */ 1250eb793cf8SQu Wenruo if (!list_empty(target_list)) { 1251eb793cf8SQu Wenruo struct defrag_target_range *last; 1252eb793cf8SQu Wenruo 1253eb793cf8SQu Wenruo last = list_entry(target_list->prev, 1254eb793cf8SQu Wenruo struct defrag_target_range, list); 1255eb793cf8SQu Wenruo ASSERT(last->start + last->len <= cur); 1256eb793cf8SQu Wenruo if (last->start + last->len == cur) { 1257eb793cf8SQu Wenruo /* Mergeable, enlarge the last entry */ 1258eb793cf8SQu Wenruo last->len += range_len; 1259eb793cf8SQu Wenruo goto next; 1260eb793cf8SQu Wenruo } 1261eb793cf8SQu Wenruo /* Fall through to allocate a new entry */ 1262eb793cf8SQu Wenruo } 1263eb793cf8SQu Wenruo 1264eb793cf8SQu Wenruo /* Allocate new defrag_target_range */ 1265eb793cf8SQu Wenruo new = kmalloc(sizeof(*new), GFP_NOFS); 1266eb793cf8SQu Wenruo if (!new) { 1267eb793cf8SQu Wenruo free_extent_map(em); 1268eb793cf8SQu Wenruo ret = -ENOMEM; 1269eb793cf8SQu Wenruo break; 1270eb793cf8SQu Wenruo } 1271eb793cf8SQu Wenruo new->start = cur; 1272eb793cf8SQu Wenruo new->len = range_len; 1273eb793cf8SQu Wenruo list_add_tail(&new->list, target_list); 1274eb793cf8SQu Wenruo 1275eb793cf8SQu Wenruo next: 1276eb793cf8SQu Wenruo cur = extent_map_end(em); 1277eb793cf8SQu Wenruo free_extent_map(em); 1278eb793cf8SQu Wenruo } 1279eb793cf8SQu Wenruo if (ret < 0) { 1280eb793cf8SQu Wenruo struct defrag_target_range *entry; 1281eb793cf8SQu Wenruo struct defrag_target_range *tmp; 1282eb793cf8SQu Wenruo 1283eb793cf8SQu Wenruo list_for_each_entry_safe(entry, tmp, target_list, list) { 1284eb793cf8SQu Wenruo list_del_init(&entry->list); 1285eb793cf8SQu Wenruo kfree(entry); 1286eb793cf8SQu Wenruo } 1287eb793cf8SQu Wenruo } 1288eb793cf8SQu Wenruo return ret; 1289eb793cf8SQu Wenruo } 1290eb793cf8SQu Wenruo 129122b398eeSQu Wenruo #define CLUSTER_SIZE (SZ_256K) 129222b398eeSQu Wenruo 129322b398eeSQu Wenruo /* 129422b398eeSQu Wenruo * Defrag one contiguous target range. 129522b398eeSQu Wenruo * 129622b398eeSQu Wenruo * @inode: target inode 129722b398eeSQu Wenruo * @target: target range to defrag 129822b398eeSQu Wenruo * @pages: locked pages covering the defrag range 129922b398eeSQu Wenruo * @nr_pages: number of locked pages 130022b398eeSQu Wenruo * 130122b398eeSQu Wenruo * Caller should ensure: 130222b398eeSQu Wenruo * 130322b398eeSQu Wenruo * - Pages are prepared 130422b398eeSQu Wenruo * Pages should be locked, no ordered extent in the pages range, 130522b398eeSQu Wenruo * no writeback. 130622b398eeSQu Wenruo * 130722b398eeSQu Wenruo * - Extent bits are locked 130822b398eeSQu Wenruo */ 130922b398eeSQu Wenruo static int defrag_one_locked_target(struct btrfs_inode *inode, 131022b398eeSQu Wenruo struct defrag_target_range *target, 131122b398eeSQu Wenruo struct page **pages, int nr_pages, 131222b398eeSQu Wenruo struct extent_state **cached_state) 131322b398eeSQu Wenruo { 131422b398eeSQu Wenruo struct btrfs_fs_info *fs_info = inode->root->fs_info; 131522b398eeSQu Wenruo struct extent_changeset *data_reserved = NULL; 131622b398eeSQu Wenruo const u64 start = target->start; 131722b398eeSQu Wenruo const u64 len = target->len; 131822b398eeSQu Wenruo unsigned long last_index = (start + len - 1) >> PAGE_SHIFT; 131922b398eeSQu Wenruo unsigned long start_index = start >> PAGE_SHIFT; 132022b398eeSQu Wenruo unsigned long first_index = page_index(pages[0]); 132122b398eeSQu Wenruo int ret = 0; 132222b398eeSQu Wenruo int i; 132322b398eeSQu Wenruo 132422b398eeSQu Wenruo ASSERT(last_index - first_index + 1 <= nr_pages); 132522b398eeSQu Wenruo 132622b398eeSQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len); 132722b398eeSQu Wenruo if (ret < 0) 132822b398eeSQu Wenruo return ret; 132922b398eeSQu Wenruo clear_extent_bit(&inode->io_tree, start, start + len - 1, 133022b398eeSQu Wenruo EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | 133122b398eeSQu Wenruo EXTENT_DEFRAG, 0, 0, cached_state); 133222b398eeSQu Wenruo set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state); 133322b398eeSQu Wenruo 133422b398eeSQu Wenruo /* Update the page status */ 133522b398eeSQu Wenruo for (i = start_index - first_index; i <= last_index - first_index; i++) { 133622b398eeSQu Wenruo ClearPageChecked(pages[i]); 133722b398eeSQu Wenruo btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len); 133822b398eeSQu Wenruo } 133922b398eeSQu Wenruo btrfs_delalloc_release_extents(inode, len); 134022b398eeSQu Wenruo extent_changeset_free(data_reserved); 134122b398eeSQu Wenruo 134222b398eeSQu Wenruo return ret; 134322b398eeSQu Wenruo } 134422b398eeSQu Wenruo 1345e9eec721SQu Wenruo static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, 1346e9eec721SQu Wenruo u32 extent_thresh, u64 newer_than, bool do_compress) 1347e9eec721SQu Wenruo { 1348e9eec721SQu Wenruo struct extent_state *cached_state = NULL; 1349e9eec721SQu Wenruo struct defrag_target_range *entry; 1350e9eec721SQu Wenruo struct defrag_target_range *tmp; 1351e9eec721SQu Wenruo LIST_HEAD(target_list); 1352e9eec721SQu Wenruo struct page **pages; 1353e9eec721SQu Wenruo const u32 sectorsize = inode->root->fs_info->sectorsize; 1354e9eec721SQu Wenruo u64 last_index = (start + len - 1) >> PAGE_SHIFT; 1355e9eec721SQu Wenruo u64 start_index = start >> PAGE_SHIFT; 1356e9eec721SQu Wenruo unsigned int nr_pages = last_index - start_index + 1; 1357e9eec721SQu Wenruo int ret = 0; 1358e9eec721SQu Wenruo int i; 1359e9eec721SQu Wenruo 1360e9eec721SQu Wenruo ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE); 1361e9eec721SQu Wenruo ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize)); 1362e9eec721SQu Wenruo 1363e9eec721SQu Wenruo pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); 1364e9eec721SQu Wenruo if (!pages) 1365e9eec721SQu Wenruo return -ENOMEM; 1366e9eec721SQu Wenruo 1367e9eec721SQu Wenruo /* Prepare all pages */ 1368e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) { 1369e9eec721SQu Wenruo pages[i] = defrag_prepare_one_page(inode, start_index + i); 1370e9eec721SQu Wenruo if (IS_ERR(pages[i])) { 1371e9eec721SQu Wenruo ret = PTR_ERR(pages[i]); 1372e9eec721SQu Wenruo pages[i] = NULL; 1373e9eec721SQu Wenruo goto free_pages; 1374e9eec721SQu Wenruo } 1375e9eec721SQu Wenruo } 1376e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) 13774cb5300bSChris Mason wait_on_page_writeback(pages[i]); 13784cb5300bSChris Mason 1379e9eec721SQu Wenruo /* Lock the pages range */ 1380e9eec721SQu Wenruo lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT, 1381e9eec721SQu Wenruo (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, 1382e9eec721SQu Wenruo &cached_state); 1383e9eec721SQu Wenruo /* 1384e9eec721SQu Wenruo * Now we have a consistent view about the extent map, re-check 1385e9eec721SQu Wenruo * which range really needs to be defragged. 1386e9eec721SQu Wenruo * 1387e9eec721SQu Wenruo * And this time we have extent locked already, pass @locked = true 1388e9eec721SQu Wenruo * so that we won't relock the extent range and cause deadlock. 1389e9eec721SQu Wenruo */ 1390e9eec721SQu Wenruo ret = defrag_collect_targets(inode, start, len, extent_thresh, 1391e9eec721SQu Wenruo newer_than, do_compress, true, 1392e9eec721SQu Wenruo &target_list); 1393e9eec721SQu Wenruo if (ret < 0) 1394e9eec721SQu Wenruo goto unlock_extent; 13954cb5300bSChris Mason 1396e9eec721SQu Wenruo list_for_each_entry(entry, &target_list, list) { 1397e9eec721SQu Wenruo ret = defrag_one_locked_target(inode, entry, pages, nr_pages, 1398e9eec721SQu Wenruo &cached_state); 1399e9eec721SQu Wenruo if (ret < 0) 1400e9eec721SQu Wenruo break; 1401e9eec721SQu Wenruo } 1402e9eec721SQu Wenruo 1403e9eec721SQu Wenruo list_for_each_entry_safe(entry, tmp, &target_list, list) { 1404e9eec721SQu Wenruo list_del_init(&entry->list); 1405e9eec721SQu Wenruo kfree(entry); 1406e9eec721SQu Wenruo } 1407e9eec721SQu Wenruo unlock_extent: 1408e9eec721SQu Wenruo unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT, 1409e9eec721SQu Wenruo (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, 1410e9eec721SQu Wenruo &cached_state); 1411e9eec721SQu Wenruo free_pages: 1412e9eec721SQu Wenruo for (i = 0; i < nr_pages; i++) { 1413e9eec721SQu Wenruo if (pages[i]) { 1414e9eec721SQu Wenruo unlock_page(pages[i]); 1415e9eec721SQu Wenruo put_page(pages[i]); 1416e9eec721SQu Wenruo } 1417e9eec721SQu Wenruo } 1418e9eec721SQu Wenruo kfree(pages); 1419e9eec721SQu Wenruo return ret; 1420e9eec721SQu Wenruo } 1421e9eec721SQu Wenruo 1422b18c3ab2SQu Wenruo static int defrag_one_cluster(struct btrfs_inode *inode, 1423b18c3ab2SQu Wenruo struct file_ra_state *ra, 1424b18c3ab2SQu Wenruo u64 start, u32 len, u32 extent_thresh, 1425b18c3ab2SQu Wenruo u64 newer_than, bool do_compress, 1426b18c3ab2SQu Wenruo unsigned long *sectors_defragged, 1427b18c3ab2SQu Wenruo unsigned long max_sectors) 1428b18c3ab2SQu Wenruo { 1429b18c3ab2SQu Wenruo const u32 sectorsize = inode->root->fs_info->sectorsize; 1430b18c3ab2SQu Wenruo struct defrag_target_range *entry; 1431b18c3ab2SQu Wenruo struct defrag_target_range *tmp; 1432b18c3ab2SQu Wenruo LIST_HEAD(target_list); 1433b18c3ab2SQu Wenruo int ret; 1434b18c3ab2SQu Wenruo 1435b18c3ab2SQu Wenruo BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE)); 1436b18c3ab2SQu Wenruo ret = defrag_collect_targets(inode, start, len, extent_thresh, 1437b18c3ab2SQu Wenruo newer_than, do_compress, false, 1438b18c3ab2SQu Wenruo &target_list); 1439b18c3ab2SQu Wenruo if (ret < 0) 1440b18c3ab2SQu Wenruo goto out; 1441b18c3ab2SQu Wenruo 1442b18c3ab2SQu Wenruo list_for_each_entry(entry, &target_list, list) { 1443b18c3ab2SQu Wenruo u32 range_len = entry->len; 1444b18c3ab2SQu Wenruo 1445b18c3ab2SQu Wenruo /* Reached the limit */ 1446b18c3ab2SQu Wenruo if (max_sectors && max_sectors == *sectors_defragged) 1447b18c3ab2SQu Wenruo break; 1448b18c3ab2SQu Wenruo 1449b18c3ab2SQu Wenruo if (max_sectors) 1450b18c3ab2SQu Wenruo range_len = min_t(u32, range_len, 1451b18c3ab2SQu Wenruo (max_sectors - *sectors_defragged) * sectorsize); 1452b18c3ab2SQu Wenruo 1453b18c3ab2SQu Wenruo if (ra) 1454b18c3ab2SQu Wenruo page_cache_sync_readahead(inode->vfs_inode.i_mapping, 1455b18c3ab2SQu Wenruo ra, NULL, entry->start >> PAGE_SHIFT, 1456b18c3ab2SQu Wenruo ((entry->start + range_len - 1) >> PAGE_SHIFT) - 1457b18c3ab2SQu Wenruo (entry->start >> PAGE_SHIFT) + 1); 1458b18c3ab2SQu Wenruo /* 1459b18c3ab2SQu Wenruo * Here we may not defrag any range if holes are punched before 1460b18c3ab2SQu Wenruo * we locked the pages. 1461b18c3ab2SQu Wenruo * But that's fine, it only affects the @sectors_defragged 1462b18c3ab2SQu Wenruo * accounting. 1463b18c3ab2SQu Wenruo */ 1464b18c3ab2SQu Wenruo ret = defrag_one_range(inode, entry->start, range_len, 1465b18c3ab2SQu Wenruo extent_thresh, newer_than, do_compress); 1466b18c3ab2SQu Wenruo if (ret < 0) 1467b18c3ab2SQu Wenruo break; 1468b18c3ab2SQu Wenruo *sectors_defragged += range_len; 1469b18c3ab2SQu Wenruo } 1470b18c3ab2SQu Wenruo out: 1471b18c3ab2SQu Wenruo list_for_each_entry_safe(entry, tmp, &target_list, list) { 1472b18c3ab2SQu Wenruo list_del_init(&entry->list); 1473b18c3ab2SQu Wenruo kfree(entry); 1474b18c3ab2SQu Wenruo } 1475b18c3ab2SQu Wenruo return ret; 1476b18c3ab2SQu Wenruo } 14777f458a38SFilipe Manana 14787f458a38SFilipe Manana /* 14791ccc2e8aSQu Wenruo * Entry point to file defragmentation. 14801ccc2e8aSQu Wenruo * 14811ccc2e8aSQu Wenruo * @inode: inode to be defragged 14821ccc2e8aSQu Wenruo * @ra: readahead state (can be NUL) 14831ccc2e8aSQu Wenruo * @range: defrag options including range and flags 14841ccc2e8aSQu Wenruo * @newer_than: minimum transid to defrag 14851ccc2e8aSQu Wenruo * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode 14861ccc2e8aSQu Wenruo * will be defragged. 14877f458a38SFilipe Manana */ 14881ccc2e8aSQu Wenruo int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, 14894cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 14904cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 14914cb5300bSChris Mason { 14920b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 14937b508037SQu Wenruo unsigned long sectors_defragged = 0; 1494151a31b2SLi Zefan u64 isize = i_size_read(inode); 14957b508037SQu Wenruo u64 cur; 14967b508037SQu Wenruo u64 last_byte; 14971e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 14981ccc2e8aSQu Wenruo bool ra_allocated = false; 14997b508037SQu Wenruo int compress_type = BTRFS_COMPRESS_ZLIB; 15007b508037SQu Wenruo int ret = 0; 15017b508037SQu Wenruo u32 extent_thresh = range->extent_thresh; 15024cb5300bSChris Mason 15030abd5b17SLiu Bo if (isize == 0) 15040abd5b17SLiu Bo return 0; 15050abd5b17SLiu Bo 15060abd5b17SLiu Bo if (range->start >= isize) 15070abd5b17SLiu Bo return -EINVAL; 15081a419d85SLi Zefan 15091e2ef46dSDavid Sterba if (do_compress) { 1510ce96b7ffSChengguang Xu if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) 15111a419d85SLi Zefan return -EINVAL; 15121a419d85SLi Zefan if (range->compress_type) 15131a419d85SLi Zefan compress_type = range->compress_type; 15141a419d85SLi Zefan } 1515f46b5a66SChristoph Hellwig 15160abd5b17SLiu Bo if (extent_thresh == 0) 1517ee22184bSByongho Lee extent_thresh = SZ_256K; 1518f46b5a66SChristoph Hellwig 15197b508037SQu Wenruo if (range->start + range->len > range->start) { 15207b508037SQu Wenruo /* Got a specific range */ 15217b508037SQu Wenruo last_byte = min(isize, range->start + range->len) - 1; 15227b508037SQu Wenruo } else { 15237b508037SQu Wenruo /* Defrag until file end */ 15247b508037SQu Wenruo last_byte = isize - 1; 15257b508037SQu Wenruo } 15267b508037SQu Wenruo 15274cb5300bSChris Mason /* 15281ccc2e8aSQu Wenruo * If we were not given a ra, allocate a readahead context. As 15290a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 15300a52d108SDavid Sterba * we don't error out. 15314cb5300bSChris Mason */ 15321ccc2e8aSQu Wenruo if (!ra) { 15331ccc2e8aSQu Wenruo ra_allocated = true; 153463e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 15350a52d108SDavid Sterba if (ra) 15364cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 15374cb5300bSChris Mason } 15384cb5300bSChris Mason 15397b508037SQu Wenruo /* Align the range */ 15407b508037SQu Wenruo cur = round_down(range->start, fs_info->sectorsize); 15417b508037SQu Wenruo last_byte = round_up(last_byte, fs_info->sectorsize) - 1; 15424cb5300bSChris Mason 15437b508037SQu Wenruo while (cur < last_byte) { 15447b508037SQu Wenruo u64 cluster_end; 15451e701a32SChris Mason 15467b508037SQu Wenruo /* The cluster size 256K should always be page aligned */ 15477b508037SQu Wenruo BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE)); 15484cb5300bSChris Mason 15497b508037SQu Wenruo /* We want the cluster end at page boundary when possible */ 15507b508037SQu Wenruo cluster_end = (((cur >> PAGE_SHIFT) + 15517b508037SQu Wenruo (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1; 15527b508037SQu Wenruo cluster_end = min(cluster_end, last_byte); 15534cb5300bSChris Mason 155464708539SJosef Bacik btrfs_inode_lock(inode, 0); 1555eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 1556eede2bf3SOmar Sandoval ret = -ETXTBSY; 15577b508037SQu Wenruo btrfs_inode_unlock(inode, 0); 15587b508037SQu Wenruo break; 15597b508037SQu Wenruo } 15607b508037SQu Wenruo if (!(inode->i_sb->s_flags & SB_ACTIVE)) { 15617b508037SQu Wenruo btrfs_inode_unlock(inode, 0); 15627b508037SQu Wenruo break; 15637b508037SQu Wenruo } 15641e2ef46dSDavid Sterba if (do_compress) 1565eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 15667b508037SQu Wenruo ret = defrag_one_cluster(BTRFS_I(inode), ra, cur, 15677b508037SQu Wenruo cluster_end + 1 - cur, extent_thresh, 15687b508037SQu Wenruo newer_than, do_compress, 15697b508037SQu Wenruo §ors_defragged, max_to_defrag); 157064708539SJosef Bacik btrfs_inode_unlock(inode, 0); 15717b508037SQu Wenruo if (ret < 0) 15724cb5300bSChris Mason break; 15737b508037SQu Wenruo cur = cluster_end + 1; 15744cb5300bSChris Mason } 1575f46b5a66SChristoph Hellwig 15767b508037SQu Wenruo if (ra_allocated) 15777b508037SQu Wenruo kfree(ra); 15787b508037SQu Wenruo if (sectors_defragged) { 15797b508037SQu Wenruo /* 15807b508037SQu Wenruo * We have defragged some sectors, for compression case they 15817b508037SQu Wenruo * need to be written back immediately. 15827b508037SQu Wenruo */ 15837b508037SQu Wenruo if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) { 15841e701a32SChris Mason filemap_flush(inode->i_mapping); 1585dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1586dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1587dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1588dec8ef90SFilipe Manana } 15897b508037SQu Wenruo if (range->compress_type == BTRFS_COMPRESS_LZO) 15900b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 15917b508037SQu Wenruo else if (range->compress_type == BTRFS_COMPRESS_ZSTD) 15925c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 15937b508037SQu Wenruo ret = sectors_defragged; 15941a419d85SLi Zefan } 15951e2ef46dSDavid Sterba if (do_compress) { 159664708539SJosef Bacik btrfs_inode_lock(inode, 0); 1597eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 159864708539SJosef Bacik btrfs_inode_unlock(inode, 0); 1599633085c7SFilipe David Borba Manana } 1600940100a4SChris Mason return ret; 1601f46b5a66SChristoph Hellwig } 1602f46b5a66SChristoph Hellwig 160317aaa434SDavid Sterba /* 160417aaa434SDavid Sterba * Try to start exclusive operation @type or cancel it if it's running. 160517aaa434SDavid Sterba * 160617aaa434SDavid Sterba * Return: 160717aaa434SDavid Sterba * 0 - normal mode, newly claimed op started 160817aaa434SDavid Sterba * >0 - normal mode, something else is running, 160917aaa434SDavid Sterba * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space 161017aaa434SDavid Sterba * ECANCELED - cancel mode, successful cancel 161117aaa434SDavid Sterba * ENOTCONN - cancel mode, operation not running anymore 161217aaa434SDavid Sterba */ 161317aaa434SDavid Sterba static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info, 161417aaa434SDavid Sterba enum btrfs_exclusive_operation type, bool cancel) 161517aaa434SDavid Sterba { 161617aaa434SDavid Sterba if (!cancel) { 161717aaa434SDavid Sterba /* Start normal op */ 161817aaa434SDavid Sterba if (!btrfs_exclop_start(fs_info, type)) 161917aaa434SDavid Sterba return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 162017aaa434SDavid Sterba /* Exclusive operation is now claimed */ 162117aaa434SDavid Sterba return 0; 162217aaa434SDavid Sterba } 162317aaa434SDavid Sterba 162417aaa434SDavid Sterba /* Cancel running op */ 162517aaa434SDavid Sterba if (btrfs_exclop_start_try_lock(fs_info, type)) { 162617aaa434SDavid Sterba /* 162717aaa434SDavid Sterba * This blocks any exclop finish from setting it to NONE, so we 162817aaa434SDavid Sterba * request cancellation. Either it runs and we will wait for it, 162917aaa434SDavid Sterba * or it has finished and no waiting will happen. 163017aaa434SDavid Sterba */ 163117aaa434SDavid Sterba atomic_inc(&fs_info->reloc_cancel_req); 163217aaa434SDavid Sterba btrfs_exclop_start_unlock(fs_info); 163317aaa434SDavid Sterba 163417aaa434SDavid Sterba if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) 163517aaa434SDavid Sterba wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING, 163617aaa434SDavid Sterba TASK_INTERRUPTIBLE); 163717aaa434SDavid Sterba 163817aaa434SDavid Sterba return -ECANCELED; 163917aaa434SDavid Sterba } 164017aaa434SDavid Sterba 164117aaa434SDavid Sterba /* Something else is running or none */ 164217aaa434SDavid Sterba return -ENOTCONN; 164317aaa434SDavid Sterba } 164417aaa434SDavid Sterba 1645198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 164676dda93cSYan, Zheng void __user *arg) 1647f46b5a66SChristoph Hellwig { 1648562d7b15SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 16490b246afaSJeff Mahoney struct inode *inode = file_inode(file); 16500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1651f46b5a66SChristoph Hellwig u64 new_size; 1652f46b5a66SChristoph Hellwig u64 old_size; 1653f46b5a66SChristoph Hellwig u64 devid = 1; 16540b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1655f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1656f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1657f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1658f46b5a66SChristoph Hellwig char *sizestr; 16599a40f122SGui Hecheng char *retptr; 1660f46b5a66SChristoph Hellwig char *devstr = NULL; 1661f46b5a66SChristoph Hellwig int ret = 0; 1662f46b5a66SChristoph Hellwig int mod = 0; 1663bb059a37SDavid Sterba bool cancel; 1664f46b5a66SChristoph Hellwig 1665e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1666e441d54dSChris Mason return -EPERM; 1667e441d54dSChris Mason 1668198605a8SMiao Xie ret = mnt_want_write_file(file); 1669198605a8SMiao Xie if (ret) 1670198605a8SMiao Xie return ret; 1671198605a8SMiao Xie 1672bb059a37SDavid Sterba /* 1673bb059a37SDavid Sterba * Read the arguments before checking exclusivity to be able to 1674bb059a37SDavid Sterba * distinguish regular resize and cancel 1675bb059a37SDavid Sterba */ 1676dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1677c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1678c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1679bb059a37SDavid Sterba goto out_drop; 1680c9e9f97bSIlya Dryomov } 16815516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1682f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1683bb059a37SDavid Sterba cancel = (strcmp("cancel", sizestr) == 0); 1684bb059a37SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel); 1685bb059a37SDavid Sterba if (ret) 1686bb059a37SDavid Sterba goto out_free; 1687bb059a37SDavid Sterba /* Exclusive operation is now claimed */ 1688bb059a37SDavid Sterba 1689f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1690f46b5a66SChristoph Hellwig if (devstr) { 1691f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1692f46b5a66SChristoph Hellwig *devstr = '\0'; 1693f46b5a66SChristoph Hellwig devstr = vol_args->name; 169458dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 169558dfae63SZhangZhen if (ret) 1696bb059a37SDavid Sterba goto out_finish; 1697dfd79829SMiao Xie if (!devid) { 1698dfd79829SMiao Xie ret = -EINVAL; 1699bb059a37SDavid Sterba goto out_finish; 1700dfd79829SMiao Xie } 17010b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1702f46b5a66SChristoph Hellwig } 1703dba60f3fSMiao Xie 1704562d7b15SJosef Bacik args.devid = devid; 1705562d7b15SJosef Bacik device = btrfs_find_device(fs_info->fs_devices, &args); 1706f46b5a66SChristoph Hellwig if (!device) { 17070b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1708c1c9ff7cSGeert Uytterhoeven devid); 1709dfd79829SMiao Xie ret = -ENODEV; 1710bb059a37SDavid Sterba goto out_finish; 1711f46b5a66SChristoph Hellwig } 1712dba60f3fSMiao Xie 1713ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 17140b246afaSJeff Mahoney btrfs_info(fs_info, 1715efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1716c1c9ff7cSGeert Uytterhoeven devid); 1717dfd79829SMiao Xie ret = -EPERM; 1718bb059a37SDavid Sterba goto out_finish; 17194e42ae1bSLiu Bo } 17204e42ae1bSLiu Bo 1721f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1722cda00ebaSChristoph Hellwig new_size = bdev_nr_bytes(device->bdev); 1723f46b5a66SChristoph Hellwig else { 1724f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1725f46b5a66SChristoph Hellwig mod = -1; 1726f46b5a66SChristoph Hellwig sizestr++; 1727f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1728f46b5a66SChristoph Hellwig mod = 1; 1729f46b5a66SChristoph Hellwig sizestr++; 1730f46b5a66SChristoph Hellwig } 17319a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 17329a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1733f46b5a66SChristoph Hellwig ret = -EINVAL; 1734bb059a37SDavid Sterba goto out_finish; 1735f46b5a66SChristoph Hellwig } 1736f46b5a66SChristoph Hellwig } 1737f46b5a66SChristoph Hellwig 1738401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 1739dfd79829SMiao Xie ret = -EPERM; 1740bb059a37SDavid Sterba goto out_finish; 174163a212abSStefan Behrens } 174263a212abSStefan Behrens 17437cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1744f46b5a66SChristoph Hellwig 1745f46b5a66SChristoph Hellwig if (mod < 0) { 1746f46b5a66SChristoph Hellwig if (new_size > old_size) { 1747f46b5a66SChristoph Hellwig ret = -EINVAL; 1748bb059a37SDavid Sterba goto out_finish; 1749f46b5a66SChristoph Hellwig } 1750f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1751f46b5a66SChristoph Hellwig } else if (mod > 0) { 1752eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1753902c68a4SGui Hecheng ret = -ERANGE; 1754bb059a37SDavid Sterba goto out_finish; 1755eb8052e0SWenliang Fan } 1756f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1757f46b5a66SChristoph Hellwig } 1758f46b5a66SChristoph Hellwig 1759ee22184bSByongho Lee if (new_size < SZ_256M) { 1760f46b5a66SChristoph Hellwig ret = -EINVAL; 1761bb059a37SDavid Sterba goto out_finish; 1762f46b5a66SChristoph Hellwig } 1763cda00ebaSChristoph Hellwig if (new_size > bdev_nr_bytes(device->bdev)) { 1764f46b5a66SChristoph Hellwig ret = -EFBIG; 1765bb059a37SDavid Sterba goto out_finish; 1766f46b5a66SChristoph Hellwig } 1767f46b5a66SChristoph Hellwig 176847f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1769f46b5a66SChristoph Hellwig 1770f46b5a66SChristoph Hellwig if (new_size > old_size) { 1771a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 177298d5dc13STsutomu Itoh if (IS_ERR(trans)) { 177398d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1774bb059a37SDavid Sterba goto out_finish; 177598d5dc13STsutomu Itoh } 1776f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 17773a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1778ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1779f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 17800253f40eSjeff.liu } /* equal, nothing need to do */ 1781f46b5a66SChristoph Hellwig 1782faf8f7b9SMarcos Paulo de Souza if (ret == 0 && new_size != old_size) 1783faf8f7b9SMarcos Paulo de Souza btrfs_info_in_rcu(fs_info, 1784faf8f7b9SMarcos Paulo de Souza "resize device %s (devid %llu) from %llu to %llu", 1785faf8f7b9SMarcos Paulo de Souza rcu_str_deref(device->name), device->devid, 1786faf8f7b9SMarcos Paulo de Souza old_size, new_size); 1787bb059a37SDavid Sterba out_finish: 1788bb059a37SDavid Sterba btrfs_exclop_finish(fs_info); 1789c9e9f97bSIlya Dryomov out_free: 1790f46b5a66SChristoph Hellwig kfree(vol_args); 1791bb059a37SDavid Sterba out_drop: 179218f39c41SIlya Dryomov mnt_drop_write_file(file); 1793f46b5a66SChristoph Hellwig return ret; 1794f46b5a66SChristoph Hellwig } 1795f46b5a66SChristoph Hellwig 17965d54c67eSNikolay Borisov static noinline int __btrfs_ioctl_snap_create(struct file *file, 17974d4340c9SChristian Brauner struct user_namespace *mnt_userns, 179852f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 17995d54c67eSNikolay Borisov bool readonly, 18008696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1801f46b5a66SChristoph Hellwig { 1802f46b5a66SChristoph Hellwig int namelen; 18033de4586cSChris Mason int ret = 0; 1804f46b5a66SChristoph Hellwig 1805325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1806325c50e3SJeff Mahoney return -ENOTDIR; 1807325c50e3SJeff Mahoney 1808a874a63eSLiu Bo ret = mnt_want_write_file(file); 1809a874a63eSLiu Bo if (ret) 1810a874a63eSLiu Bo goto out; 1811a874a63eSLiu Bo 181272fd032eSSage Weil namelen = strlen(name); 181372fd032eSSage Weil if (strchr(name, '/')) { 1814f46b5a66SChristoph Hellwig ret = -EINVAL; 1815a874a63eSLiu Bo goto out_drop_write; 1816f46b5a66SChristoph Hellwig } 1817f46b5a66SChristoph Hellwig 181816780cabSChris Mason if (name[0] == '.' && 181916780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 182016780cabSChris Mason ret = -EEXIST; 1821a874a63eSLiu Bo goto out_drop_write; 182216780cabSChris Mason } 182316780cabSChris Mason 18243de4586cSChris Mason if (subvol) { 18254d4340c9SChristian Brauner ret = btrfs_mksubvol(&file->f_path, mnt_userns, name, 18264d4340c9SChristian Brauner namelen, NULL, readonly, inherit); 1827cb8e7090SChristoph Hellwig } else { 18282903ff01SAl Viro struct fd src = fdget(fd); 18293de4586cSChris Mason struct inode *src_inode; 18302903ff01SAl Viro if (!src.file) { 18313de4586cSChris Mason ret = -EINVAL; 1832a874a63eSLiu Bo goto out_drop_write; 18333de4586cSChris Mason } 18343de4586cSChris Mason 1835496ad9aaSAl Viro src_inode = file_inode(src.file); 1836496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1837c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1838efe120a0SFrank Holton "Snapshot src from another FS"); 183923ad5b17SKusanagi Kouichi ret = -EXDEV; 18404d4340c9SChristian Brauner } else if (!inode_owner_or_capable(mnt_userns, src_inode)) { 1841d0242061SDavid Sterba /* 1842d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1843d0242061SDavid Sterba * are limited to own subvolumes only 1844d0242061SDavid Sterba */ 1845d0242061SDavid Sterba ret = -EPERM; 1846ecd18815SAl Viro } else { 18474d4340c9SChristian Brauner ret = btrfs_mksnapshot(&file->f_path, mnt_userns, 18484d4340c9SChristian Brauner name, namelen, 184972fd032eSSage Weil BTRFS_I(src_inode)->root, 18505d54c67eSNikolay Borisov readonly, inherit); 1851ecd18815SAl Viro } 18522903ff01SAl Viro fdput(src); 1853cb8e7090SChristoph Hellwig } 1854a874a63eSLiu Bo out_drop_write: 1855a874a63eSLiu Bo mnt_drop_write_file(file); 1856f46b5a66SChristoph Hellwig out: 185772fd032eSSage Weil return ret; 185872fd032eSSage Weil } 185972fd032eSSage Weil 186072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1861fa0d2b9bSLi Zefan void __user *arg, int subvol) 186272fd032eSSage Weil { 1863fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 186472fd032eSSage Weil int ret; 186572fd032eSSage Weil 1866325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1867325c50e3SJeff Mahoney return -ENOTDIR; 1868325c50e3SJeff Mahoney 1869fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1870fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1871fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1872fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1873fa0d2b9bSLi Zefan 18744d4340c9SChristian Brauner ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), 18754d4340c9SChristian Brauner vol_args->name, vol_args->fd, subvol, 18764d4340c9SChristian Brauner false, NULL); 1877fa0d2b9bSLi Zefan 1878fa0d2b9bSLi Zefan kfree(vol_args); 1879fa0d2b9bSLi Zefan return ret; 1880fa0d2b9bSLi Zefan } 1881fa0d2b9bSLi Zefan 1882fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1883fa0d2b9bSLi Zefan void __user *arg, int subvol) 1884fa0d2b9bSLi Zefan { 1885fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1886fa0d2b9bSLi Zefan int ret; 1887b83cc969SLi Zefan bool readonly = false; 18886f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 188972fd032eSSage Weil 1890325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1891325c50e3SJeff Mahoney return -ENOTDIR; 1892325c50e3SJeff Mahoney 1893fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1894fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1895fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1896fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1897fdfb1e4fSLi Zefan 1898673990dbSDavid Sterba if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { 1899b83cc969SLi Zefan ret = -EOPNOTSUPP; 1900c47ca32dSDan Carpenter goto free_args; 1901fdfb1e4fSLi Zefan } 1902fdfb1e4fSLi Zefan 1903b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1904b83cc969SLi Zefan readonly = true; 19056f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 19065011c5a6SDan Carpenter u64 nums; 19075011c5a6SDan Carpenter 19085011c5a6SDan Carpenter if (vol_args->size < sizeof(*inherit) || 19095011c5a6SDan Carpenter vol_args->size > PAGE_SIZE) { 19106f72c7e2SArne Jansen ret = -EINVAL; 1911c47ca32dSDan Carpenter goto free_args; 19126f72c7e2SArne Jansen } 19136f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 19146f72c7e2SArne Jansen if (IS_ERR(inherit)) { 19156f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1916c47ca32dSDan Carpenter goto free_args; 19176f72c7e2SArne Jansen } 19185011c5a6SDan Carpenter 19195011c5a6SDan Carpenter if (inherit->num_qgroups > PAGE_SIZE || 19205011c5a6SDan Carpenter inherit->num_ref_copies > PAGE_SIZE || 19215011c5a6SDan Carpenter inherit->num_excl_copies > PAGE_SIZE) { 19225011c5a6SDan Carpenter ret = -EINVAL; 19235011c5a6SDan Carpenter goto free_inherit; 19245011c5a6SDan Carpenter } 19255011c5a6SDan Carpenter 19265011c5a6SDan Carpenter nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + 19275011c5a6SDan Carpenter 2 * inherit->num_excl_copies; 19285011c5a6SDan Carpenter if (vol_args->size != struct_size(inherit, qgroups, nums)) { 19295011c5a6SDan Carpenter ret = -EINVAL; 19305011c5a6SDan Carpenter goto free_inherit; 19315011c5a6SDan Carpenter } 19326f72c7e2SArne Jansen } 193375eaa0e2SSage Weil 19344d4340c9SChristian Brauner ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), 19354d4340c9SChristian Brauner vol_args->name, vol_args->fd, subvol, 19364d4340c9SChristian Brauner readonly, inherit); 1937c47ca32dSDan Carpenter if (ret) 1938c47ca32dSDan Carpenter goto free_inherit; 1939c47ca32dSDan Carpenter free_inherit: 19406f72c7e2SArne Jansen kfree(inherit); 1941c47ca32dSDan Carpenter free_args: 1942c47ca32dSDan Carpenter kfree(vol_args); 1943f46b5a66SChristoph Hellwig return ret; 1944f46b5a66SChristoph Hellwig } 1945f46b5a66SChristoph Hellwig 19460caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 19470caa102dSLi Zefan void __user *arg) 19480caa102dSLi Zefan { 1949496ad9aaSAl Viro struct inode *inode = file_inode(file); 19500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 19510caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19520caa102dSLi Zefan int ret = 0; 19530caa102dSLi Zefan u64 flags = 0; 19540caa102dSLi Zefan 19554a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 19560caa102dSLi Zefan return -EINVAL; 19570caa102dSLi Zefan 19580b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 19590caa102dSLi Zefan if (btrfs_root_readonly(root)) 19600caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 19610b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 19620caa102dSLi Zefan 19630caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 19640caa102dSLi Zefan ret = -EFAULT; 19650caa102dSLi Zefan 19660caa102dSLi Zefan return ret; 19670caa102dSLi Zefan } 19680caa102dSLi Zefan 19690caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 19700caa102dSLi Zefan void __user *arg) 19710caa102dSLi Zefan { 1972496ad9aaSAl Viro struct inode *inode = file_inode(file); 19730b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 19740caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19750caa102dSLi Zefan struct btrfs_trans_handle *trans; 19760caa102dSLi Zefan u64 root_flags; 19770caa102dSLi Zefan u64 flags; 19780caa102dSLi Zefan int ret = 0; 19790caa102dSLi Zefan 198039e1674fSChristian Brauner if (!inode_owner_or_capable(file_mnt_user_ns(file), inode)) 1981bd60ea0fSDavid Sterba return -EPERM; 1982bd60ea0fSDavid Sterba 1983b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1984b9ca0664SLiu Bo if (ret) 1985b9ca0664SLiu Bo goto out; 19860caa102dSLi Zefan 19874a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1988b9ca0664SLiu Bo ret = -EINVAL; 1989b9ca0664SLiu Bo goto out_drop_write; 1990b9ca0664SLiu Bo } 19910caa102dSLi Zefan 1992b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1993b9ca0664SLiu Bo ret = -EFAULT; 1994b9ca0664SLiu Bo goto out_drop_write; 1995b9ca0664SLiu Bo } 19960caa102dSLi Zefan 1997b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1998b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1999b9ca0664SLiu Bo goto out_drop_write; 2000b9ca0664SLiu Bo } 20010caa102dSLi Zefan 20020b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 20030caa102dSLi Zefan 20040caa102dSLi Zefan /* nothing to do */ 20050caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 2006b9ca0664SLiu Bo goto out_drop_sem; 20070caa102dSLi Zefan 20080caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 20092c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 20100caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 20110caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 20122c686537SDavid Sterba } else { 20132c686537SDavid Sterba /* 20142c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 20152c686537SDavid Sterba * send 20162c686537SDavid Sterba */ 20172c686537SDavid Sterba spin_lock(&root->root_item_lock); 20182c686537SDavid Sterba if (root->send_in_progress == 0) { 20190caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 20200caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 20212c686537SDavid Sterba spin_unlock(&root->root_item_lock); 20222c686537SDavid Sterba } else { 20232c686537SDavid Sterba spin_unlock(&root->root_item_lock); 20240b246afaSJeff Mahoney btrfs_warn(fs_info, 20252c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 20262c686537SDavid Sterba root->root_key.objectid); 20272c686537SDavid Sterba ret = -EPERM; 20282c686537SDavid Sterba goto out_drop_sem; 20292c686537SDavid Sterba } 20302c686537SDavid Sterba } 20310caa102dSLi Zefan 20320caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 20330caa102dSLi Zefan if (IS_ERR(trans)) { 20340caa102dSLi Zefan ret = PTR_ERR(trans); 20350caa102dSLi Zefan goto out_reset; 20360caa102dSLi Zefan } 20370caa102dSLi Zefan 20380b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 20390caa102dSLi Zefan &root->root_key, &root->root_item); 20409417ebc8SNikolay Borisov if (ret < 0) { 20419417ebc8SNikolay Borisov btrfs_end_transaction(trans); 20429417ebc8SNikolay Borisov goto out_reset; 20439417ebc8SNikolay Borisov } 20440caa102dSLi Zefan 20459417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 20469417ebc8SNikolay Borisov 20470caa102dSLi Zefan out_reset: 20480caa102dSLi Zefan if (ret) 20490caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 2050b9ca0664SLiu Bo out_drop_sem: 20510b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 2052b9ca0664SLiu Bo out_drop_write: 2053b9ca0664SLiu Bo mnt_drop_write_file(file); 2054b9ca0664SLiu Bo out: 20550caa102dSLi Zefan return ret; 20560caa102dSLi Zefan } 20570caa102dSLi Zefan 2058ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 2059ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 2060ac8e9819SChris Mason { 2061abc6e134SChris Mason struct btrfs_key test; 2062abc6e134SChris Mason int ret; 2063abc6e134SChris Mason 2064abc6e134SChris Mason test.objectid = sk->min_objectid; 2065abc6e134SChris Mason test.type = sk->min_type; 2066abc6e134SChris Mason test.offset = sk->min_offset; 2067abc6e134SChris Mason 2068abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2069abc6e134SChris Mason if (ret < 0) 2070ac8e9819SChris Mason return 0; 2071abc6e134SChris Mason 2072abc6e134SChris Mason test.objectid = sk->max_objectid; 2073abc6e134SChris Mason test.type = sk->max_type; 2074abc6e134SChris Mason test.offset = sk->max_offset; 2075abc6e134SChris Mason 2076abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2077abc6e134SChris Mason if (ret > 0) 2078ac8e9819SChris Mason return 0; 2079ac8e9819SChris Mason return 1; 2080ac8e9819SChris Mason } 2081ac8e9819SChris Mason 2082df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 2083ac8e9819SChris Mason struct btrfs_key *key, 2084ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 20859b6e817dSGerhard Heift size_t *buf_size, 2086ba346b35SGerhard Heift char __user *ubuf, 2087ac8e9819SChris Mason unsigned long *sk_offset, 2088ac8e9819SChris Mason int *num_found) 2089ac8e9819SChris Mason { 2090ac8e9819SChris Mason u64 found_transid; 2091ac8e9819SChris Mason struct extent_buffer *leaf; 2092ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 2093dd81d459SNaohiro Aota struct btrfs_key test; 2094ac8e9819SChris Mason unsigned long item_off; 2095ac8e9819SChris Mason unsigned long item_len; 2096ac8e9819SChris Mason int nritems; 2097ac8e9819SChris Mason int i; 2098ac8e9819SChris Mason int slot; 2099ac8e9819SChris Mason int ret = 0; 2100ac8e9819SChris Mason 2101ac8e9819SChris Mason leaf = path->nodes[0]; 2102ac8e9819SChris Mason slot = path->slots[0]; 2103ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 2104ac8e9819SChris Mason 2105ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 2106ac8e9819SChris Mason i = nritems; 2107ac8e9819SChris Mason goto advance_key; 2108ac8e9819SChris Mason } 2109ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 2110ac8e9819SChris Mason 2111ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 2112ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 21133212fa14SJosef Bacik item_len = btrfs_item_size(leaf, i); 2114ac8e9819SChris Mason 211503b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 211603b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 211703b71c6cSGabriel de Perthuis continue; 211803b71c6cSGabriel de Perthuis 21199b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 21208f5f6178SGerhard Heift if (*num_found) { 2121ac8e9819SChris Mason ret = 1; 21228f5f6178SGerhard Heift goto out; 21238f5f6178SGerhard Heift } 21248f5f6178SGerhard Heift 21258f5f6178SGerhard Heift /* 21268f5f6178SGerhard Heift * return one empty item back for v1, which does not 21278f5f6178SGerhard Heift * handle -EOVERFLOW 21288f5f6178SGerhard Heift */ 21298f5f6178SGerhard Heift 21309b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2131ac8e9819SChris Mason item_len = 0; 21328f5f6178SGerhard Heift ret = -EOVERFLOW; 21338f5f6178SGerhard Heift } 2134ac8e9819SChris Mason 21359b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2136ac8e9819SChris Mason ret = 1; 213725c9bc2eSGerhard Heift goto out; 2138ac8e9819SChris Mason } 2139ac8e9819SChris Mason 2140ac8e9819SChris Mason sh.objectid = key->objectid; 2141ac8e9819SChris Mason sh.offset = key->offset; 2142ac8e9819SChris Mason sh.type = key->type; 2143ac8e9819SChris Mason sh.len = item_len; 2144ac8e9819SChris Mason sh.transid = found_transid; 2145ac8e9819SChris Mason 2146a48b73ecSJosef Bacik /* 2147a48b73ecSJosef Bacik * Copy search result header. If we fault then loop again so we 2148a48b73ecSJosef Bacik * can fault in the pages and -EFAULT there if there's a 2149a48b73ecSJosef Bacik * problem. Otherwise we'll fault and then copy the buffer in 2150a48b73ecSJosef Bacik * properly this next time through 2151a48b73ecSJosef Bacik */ 2152a48b73ecSJosef Bacik if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) { 2153a48b73ecSJosef Bacik ret = 0; 2154ba346b35SGerhard Heift goto out; 2155ba346b35SGerhard Heift } 2156ba346b35SGerhard Heift 2157ac8e9819SChris Mason *sk_offset += sizeof(sh); 2158ac8e9819SChris Mason 2159ac8e9819SChris Mason if (item_len) { 2160ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2161a48b73ecSJosef Bacik /* 2162a48b73ecSJosef Bacik * Copy the item, same behavior as above, but reset the 2163a48b73ecSJosef Bacik * * sk_offset so we copy the full thing again. 2164a48b73ecSJosef Bacik */ 2165a48b73ecSJosef Bacik if (read_extent_buffer_to_user_nofault(leaf, up, 2166ba346b35SGerhard Heift item_off, item_len)) { 2167a48b73ecSJosef Bacik ret = 0; 2168a48b73ecSJosef Bacik *sk_offset -= sizeof(sh); 2169ba346b35SGerhard Heift goto out; 2170ba346b35SGerhard Heift } 2171ba346b35SGerhard Heift 2172ac8e9819SChris Mason *sk_offset += item_len; 2173ac8e9819SChris Mason } 2174e2156867SHugo Mills (*num_found)++; 2175ac8e9819SChris Mason 21768f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 21778f5f6178SGerhard Heift goto out; 21788f5f6178SGerhard Heift 217925c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 218025c9bc2eSGerhard Heift ret = 1; 218125c9bc2eSGerhard Heift goto out; 218225c9bc2eSGerhard Heift } 2183ac8e9819SChris Mason } 2184ac8e9819SChris Mason advance_key: 2185ac8e9819SChris Mason ret = 0; 2186dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2187dd81d459SNaohiro Aota test.type = sk->max_type; 2188dd81d459SNaohiro Aota test.offset = sk->max_offset; 2189dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2190dd81d459SNaohiro Aota ret = 1; 2191dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2192abc6e134SChris Mason key->offset++; 2193dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2194abc6e134SChris Mason key->offset = 0; 2195abc6e134SChris Mason key->type++; 2196dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2197abc6e134SChris Mason key->offset = 0; 2198abc6e134SChris Mason key->type = 0; 2199abc6e134SChris Mason key->objectid++; 2200abc6e134SChris Mason } else 2201abc6e134SChris Mason ret = 1; 220225c9bc2eSGerhard Heift out: 2203ba346b35SGerhard Heift /* 2204ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2205ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2206ba346b35SGerhard Heift * * all items were found 2207ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2208ba346b35SGerhard Heift * leaf 2209ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2210ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2211ba346b35SGerhard Heift */ 2212ac8e9819SChris Mason return ret; 2213ac8e9819SChris Mason } 2214ac8e9819SChris Mason 2215ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 221612544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 22179b6e817dSGerhard Heift size_t *buf_size, 2218ba346b35SGerhard Heift char __user *ubuf) 2219ac8e9819SChris Mason { 22200b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2221ac8e9819SChris Mason struct btrfs_root *root; 2222ac8e9819SChris Mason struct btrfs_key key; 2223ac8e9819SChris Mason struct btrfs_path *path; 2224ac8e9819SChris Mason int ret; 2225ac8e9819SChris Mason int num_found = 0; 2226ac8e9819SChris Mason unsigned long sk_offset = 0; 2227ac8e9819SChris Mason 22289b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 22299b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 223012544442SGerhard Heift return -EOVERFLOW; 22319b6e817dSGerhard Heift } 223212544442SGerhard Heift 2233ac8e9819SChris Mason path = btrfs_alloc_path(); 2234ac8e9819SChris Mason if (!path) 2235ac8e9819SChris Mason return -ENOMEM; 2236ac8e9819SChris Mason 2237ac8e9819SChris Mason if (sk->tree_id == 0) { 2238ac8e9819SChris Mason /* search the root of the inode that was passed */ 223900246528SJosef Bacik root = btrfs_grab_root(BTRFS_I(inode)->root); 2240ac8e9819SChris Mason } else { 224156e9357aSDavid Sterba root = btrfs_get_fs_root(info, sk->tree_id, true); 2242ac8e9819SChris Mason if (IS_ERR(root)) { 2243ac8e9819SChris Mason btrfs_free_path(path); 2244ad1e3d56SMisono Tomohiro return PTR_ERR(root); 2245ac8e9819SChris Mason } 2246ac8e9819SChris Mason } 2247ac8e9819SChris Mason 2248ac8e9819SChris Mason key.objectid = sk->min_objectid; 2249ac8e9819SChris Mason key.type = sk->min_type; 2250ac8e9819SChris Mason key.offset = sk->min_offset; 2251ac8e9819SChris Mason 2252ac8e9819SChris Mason while (1) { 2253bb523b40SAndreas Gruenbacher ret = -EFAULT; 2254bb523b40SAndreas Gruenbacher if (fault_in_writeable(ubuf + sk_offset, *buf_size - sk_offset)) 2255a48b73ecSJosef Bacik break; 2256a48b73ecSJosef Bacik 22576174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2258ac8e9819SChris Mason if (ret != 0) { 2259ac8e9819SChris Mason if (ret > 0) 2260ac8e9819SChris Mason ret = 0; 2261ac8e9819SChris Mason goto err; 2262ac8e9819SChris Mason } 2263df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2264ac8e9819SChris Mason &sk_offset, &num_found); 2265b3b4aa74SDavid Sterba btrfs_release_path(path); 226625c9bc2eSGerhard Heift if (ret) 2267ac8e9819SChris Mason break; 2268ac8e9819SChris Mason 2269ac8e9819SChris Mason } 22708f5f6178SGerhard Heift if (ret > 0) 2271ac8e9819SChris Mason ret = 0; 2272ac8e9819SChris Mason err: 2273ac8e9819SChris Mason sk->nr_items = num_found; 227400246528SJosef Bacik btrfs_put_root(root); 2275ac8e9819SChris Mason btrfs_free_path(path); 2276ac8e9819SChris Mason return ret; 2277ac8e9819SChris Mason } 2278ac8e9819SChris Mason 2279ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2280ac8e9819SChris Mason void __user *argp) 2281ac8e9819SChris Mason { 2282ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2283ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2284ac8e9819SChris Mason struct inode *inode; 2285ac8e9819SChris Mason int ret; 22869b6e817dSGerhard Heift size_t buf_size; 2287ac8e9819SChris Mason 2288ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2289ac8e9819SChris Mason return -EPERM; 2290ac8e9819SChris Mason 2291ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2292ac8e9819SChris Mason 2293ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2294ba346b35SGerhard Heift return -EFAULT; 2295ba346b35SGerhard Heift 2296ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2297ac8e9819SChris Mason 2298496ad9aaSAl Viro inode = file_inode(file); 2299ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 23008f5f6178SGerhard Heift 23018f5f6178SGerhard Heift /* 23028f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 23038f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 23048f5f6178SGerhard Heift */ 23058f5f6178SGerhard Heift if (ret == -EOVERFLOW) 23068f5f6178SGerhard Heift ret = 0; 23078f5f6178SGerhard Heift 2308ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2309ac8e9819SChris Mason ret = -EFAULT; 2310ac8e9819SChris Mason return ret; 2311ac8e9819SChris Mason } 2312ac8e9819SChris Mason 2313cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2314cc68a8a5SGerhard Heift void __user *argp) 2315cc68a8a5SGerhard Heift { 2316cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2317cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2318cc68a8a5SGerhard Heift struct inode *inode; 2319cc68a8a5SGerhard Heift int ret; 2320cc68a8a5SGerhard Heift size_t buf_size; 2321ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2322cc68a8a5SGerhard Heift 2323cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2324cc68a8a5SGerhard Heift return -EPERM; 2325cc68a8a5SGerhard Heift 2326cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2327cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2328cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2329cc68a8a5SGerhard Heift return -EFAULT; 2330cc68a8a5SGerhard Heift 2331cc68a8a5SGerhard Heift buf_size = args.buf_size; 2332cc68a8a5SGerhard Heift 2333cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2334cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2335cc68a8a5SGerhard Heift buf_size = buf_limit; 2336cc68a8a5SGerhard Heift 2337cc68a8a5SGerhard Heift inode = file_inode(file); 2338cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2339718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2340cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2341cc68a8a5SGerhard Heift ret = -EFAULT; 2342cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2343cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2344cc68a8a5SGerhard Heift ret = -EFAULT; 2345cc68a8a5SGerhard Heift 234676dda93cSYan, Zheng return ret; 234776dda93cSYan, Zheng } 234876dda93cSYan, Zheng 234998d377a0STARUISI Hiroaki /* 2350ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2351ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 235298d377a0STARUISI Hiroaki */ 235398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 235498d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 235598d377a0STARUISI Hiroaki { 235698d377a0STARUISI Hiroaki struct btrfs_root *root; 235798d377a0STARUISI Hiroaki struct btrfs_key key; 2358ac8e9819SChris Mason char *ptr; 235998d377a0STARUISI Hiroaki int ret = -1; 236098d377a0STARUISI Hiroaki int slot; 236198d377a0STARUISI Hiroaki int len; 236298d377a0STARUISI Hiroaki int total_len = 0; 236398d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 236498d377a0STARUISI Hiroaki struct extent_buffer *l; 236598d377a0STARUISI Hiroaki struct btrfs_path *path; 236698d377a0STARUISI Hiroaki 236798d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 236898d377a0STARUISI Hiroaki name[0]='\0'; 236998d377a0STARUISI Hiroaki return 0; 237098d377a0STARUISI Hiroaki } 237198d377a0STARUISI Hiroaki 237298d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 237398d377a0STARUISI Hiroaki if (!path) 237498d377a0STARUISI Hiroaki return -ENOMEM; 237598d377a0STARUISI Hiroaki 2376c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 237798d377a0STARUISI Hiroaki 237856e9357aSDavid Sterba root = btrfs_get_fs_root(info, tree_id, true); 237998d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2380ad1e3d56SMisono Tomohiro ret = PTR_ERR(root); 238188234012SJosef Bacik root = NULL; 238288234012SJosef Bacik goto out; 238388234012SJosef Bacik } 238498d377a0STARUISI Hiroaki 238598d377a0STARUISI Hiroaki key.objectid = dirid; 238698d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 23878ad6fcabSChris Mason key.offset = (u64)-1; 238898d377a0STARUISI Hiroaki 238998d377a0STARUISI Hiroaki while (1) { 23900ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(root, &key, path); 239118674c6cSFilipe David Borba Manana if (ret < 0) 239218674c6cSFilipe David Borba Manana goto out; 239318674c6cSFilipe David Borba Manana else if (ret > 0) { 2394ac8e9819SChris Mason ret = -ENOENT; 239598d377a0STARUISI Hiroaki goto out; 2396ac8e9819SChris Mason } 239718674c6cSFilipe David Borba Manana 239818674c6cSFilipe David Borba Manana l = path->nodes[0]; 239918674c6cSFilipe David Borba Manana slot = path->slots[0]; 240098d377a0STARUISI Hiroaki 240198d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 240298d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 240398d377a0STARUISI Hiroaki ptr -= len + 1; 240498d377a0STARUISI Hiroaki total_len += len + 1; 2405a696cf35SFilipe David Borba Manana if (ptr < name) { 2406a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 240798d377a0STARUISI Hiroaki goto out; 2408a696cf35SFilipe David Borba Manana } 240998d377a0STARUISI Hiroaki 241098d377a0STARUISI Hiroaki *(ptr + len) = '/'; 241198d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 241298d377a0STARUISI Hiroaki 241398d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 241498d377a0STARUISI Hiroaki break; 241598d377a0STARUISI Hiroaki 2416b3b4aa74SDavid Sterba btrfs_release_path(path); 241798d377a0STARUISI Hiroaki key.objectid = key.offset; 24188ad6fcabSChris Mason key.offset = (u64)-1; 241998d377a0STARUISI Hiroaki dirid = key.objectid; 242098d377a0STARUISI Hiroaki } 242177906a50SLi Zefan memmove(name, ptr, total_len); 242298d377a0STARUISI Hiroaki name[total_len] = '\0'; 242398d377a0STARUISI Hiroaki ret = 0; 242498d377a0STARUISI Hiroaki out: 242500246528SJosef Bacik btrfs_put_root(root); 242698d377a0STARUISI Hiroaki btrfs_free_path(path); 2427ac8e9819SChris Mason return ret; 2428ac8e9819SChris Mason } 2429ac8e9819SChris Mason 24306623d9a0SChristian Brauner static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns, 24316623d9a0SChristian Brauner struct inode *inode, 243223d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args) 243323d0b79dSTomohiro Misono { 243423d0b79dSTomohiro Misono struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 243523d0b79dSTomohiro Misono struct super_block *sb = inode->i_sb; 243623d0b79dSTomohiro Misono struct btrfs_key upper_limit = BTRFS_I(inode)->location; 243723d0b79dSTomohiro Misono u64 treeid = BTRFS_I(inode)->root->root_key.objectid; 243823d0b79dSTomohiro Misono u64 dirid = args->dirid; 243923d0b79dSTomohiro Misono unsigned long item_off; 244023d0b79dSTomohiro Misono unsigned long item_len; 244123d0b79dSTomohiro Misono struct btrfs_inode_ref *iref; 244223d0b79dSTomohiro Misono struct btrfs_root_ref *rref; 2443b8a49ae1SJosef Bacik struct btrfs_root *root = NULL; 244423d0b79dSTomohiro Misono struct btrfs_path *path; 244523d0b79dSTomohiro Misono struct btrfs_key key, key2; 244623d0b79dSTomohiro Misono struct extent_buffer *leaf; 244723d0b79dSTomohiro Misono struct inode *temp_inode; 244823d0b79dSTomohiro Misono char *ptr; 244923d0b79dSTomohiro Misono int slot; 245023d0b79dSTomohiro Misono int len; 245123d0b79dSTomohiro Misono int total_len = 0; 245223d0b79dSTomohiro Misono int ret; 245323d0b79dSTomohiro Misono 245423d0b79dSTomohiro Misono path = btrfs_alloc_path(); 245523d0b79dSTomohiro Misono if (!path) 245623d0b79dSTomohiro Misono return -ENOMEM; 245723d0b79dSTomohiro Misono 245823d0b79dSTomohiro Misono /* 245923d0b79dSTomohiro Misono * If the bottom subvolume does not exist directly under upper_limit, 246023d0b79dSTomohiro Misono * construct the path in from the bottom up. 246123d0b79dSTomohiro Misono */ 246223d0b79dSTomohiro Misono if (dirid != upper_limit.objectid) { 246323d0b79dSTomohiro Misono ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; 246423d0b79dSTomohiro Misono 246556e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, treeid, true); 246623d0b79dSTomohiro Misono if (IS_ERR(root)) { 246723d0b79dSTomohiro Misono ret = PTR_ERR(root); 246823d0b79dSTomohiro Misono goto out; 246923d0b79dSTomohiro Misono } 247023d0b79dSTomohiro Misono 247123d0b79dSTomohiro Misono key.objectid = dirid; 247223d0b79dSTomohiro Misono key.type = BTRFS_INODE_REF_KEY; 247323d0b79dSTomohiro Misono key.offset = (u64)-1; 247423d0b79dSTomohiro Misono while (1) { 24750ff40a91SMarcos Paulo de Souza ret = btrfs_search_backwards(root, &key, path); 24760ff40a91SMarcos Paulo de Souza if (ret < 0) 2477b8a49ae1SJosef Bacik goto out_put; 24780ff40a91SMarcos Paulo de Souza else if (ret > 0) { 247923d0b79dSTomohiro Misono ret = -ENOENT; 2480b8a49ae1SJosef Bacik goto out_put; 248123d0b79dSTomohiro Misono } 248223d0b79dSTomohiro Misono 248323d0b79dSTomohiro Misono leaf = path->nodes[0]; 248423d0b79dSTomohiro Misono slot = path->slots[0]; 248523d0b79dSTomohiro Misono 248623d0b79dSTomohiro Misono iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); 248723d0b79dSTomohiro Misono len = btrfs_inode_ref_name_len(leaf, iref); 248823d0b79dSTomohiro Misono ptr -= len + 1; 248923d0b79dSTomohiro Misono total_len += len + 1; 249023d0b79dSTomohiro Misono if (ptr < args->path) { 249123d0b79dSTomohiro Misono ret = -ENAMETOOLONG; 2492b8a49ae1SJosef Bacik goto out_put; 249323d0b79dSTomohiro Misono } 249423d0b79dSTomohiro Misono 249523d0b79dSTomohiro Misono *(ptr + len) = '/'; 249623d0b79dSTomohiro Misono read_extent_buffer(leaf, ptr, 249723d0b79dSTomohiro Misono (unsigned long)(iref + 1), len); 249823d0b79dSTomohiro Misono 249923d0b79dSTomohiro Misono /* Check the read+exec permission of this directory */ 250023d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 250123d0b79dSTomohiro Misono BTRFS_INODE_ITEM_KEY); 250223d0b79dSTomohiro Misono if (ret < 0) { 2503b8a49ae1SJosef Bacik goto out_put; 250423d0b79dSTomohiro Misono } else if (ret > 0) { 250523d0b79dSTomohiro Misono ret = -ENOENT; 2506b8a49ae1SJosef Bacik goto out_put; 250723d0b79dSTomohiro Misono } 250823d0b79dSTomohiro Misono 250923d0b79dSTomohiro Misono leaf = path->nodes[0]; 251023d0b79dSTomohiro Misono slot = path->slots[0]; 251123d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key2, slot); 251223d0b79dSTomohiro Misono if (key2.objectid != dirid) { 251323d0b79dSTomohiro Misono ret = -ENOENT; 2514b8a49ae1SJosef Bacik goto out_put; 251523d0b79dSTomohiro Misono } 251623d0b79dSTomohiro Misono 25170202e83fSDavid Sterba temp_inode = btrfs_iget(sb, key2.objectid, root); 25183ca57bd6SMisono Tomohiro if (IS_ERR(temp_inode)) { 25193ca57bd6SMisono Tomohiro ret = PTR_ERR(temp_inode); 2520b8a49ae1SJosef Bacik goto out_put; 25213ca57bd6SMisono Tomohiro } 25226623d9a0SChristian Brauner ret = inode_permission(mnt_userns, temp_inode, 252347291baaSChristian Brauner MAY_READ | MAY_EXEC); 252423d0b79dSTomohiro Misono iput(temp_inode); 252523d0b79dSTomohiro Misono if (ret) { 252623d0b79dSTomohiro Misono ret = -EACCES; 2527b8a49ae1SJosef Bacik goto out_put; 252823d0b79dSTomohiro Misono } 252923d0b79dSTomohiro Misono 253023d0b79dSTomohiro Misono if (key.offset == upper_limit.objectid) 253123d0b79dSTomohiro Misono break; 253223d0b79dSTomohiro Misono if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { 253323d0b79dSTomohiro Misono ret = -EACCES; 2534b8a49ae1SJosef Bacik goto out_put; 253523d0b79dSTomohiro Misono } 253623d0b79dSTomohiro Misono 253723d0b79dSTomohiro Misono btrfs_release_path(path); 253823d0b79dSTomohiro Misono key.objectid = key.offset; 253923d0b79dSTomohiro Misono key.offset = (u64)-1; 254023d0b79dSTomohiro Misono dirid = key.objectid; 254123d0b79dSTomohiro Misono } 254223d0b79dSTomohiro Misono 254323d0b79dSTomohiro Misono memmove(args->path, ptr, total_len); 254423d0b79dSTomohiro Misono args->path[total_len] = '\0'; 254500246528SJosef Bacik btrfs_put_root(root); 2546b8a49ae1SJosef Bacik root = NULL; 254723d0b79dSTomohiro Misono btrfs_release_path(path); 254823d0b79dSTomohiro Misono } 254923d0b79dSTomohiro Misono 255023d0b79dSTomohiro Misono /* Get the bottom subvolume's name from ROOT_REF */ 255123d0b79dSTomohiro Misono key.objectid = treeid; 255223d0b79dSTomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 255323d0b79dSTomohiro Misono key.offset = args->treeid; 2554b8a49ae1SJosef Bacik ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 255523d0b79dSTomohiro Misono if (ret < 0) { 255623d0b79dSTomohiro Misono goto out; 255723d0b79dSTomohiro Misono } else if (ret > 0) { 255823d0b79dSTomohiro Misono ret = -ENOENT; 255923d0b79dSTomohiro Misono goto out; 256023d0b79dSTomohiro Misono } 256123d0b79dSTomohiro Misono 256223d0b79dSTomohiro Misono leaf = path->nodes[0]; 256323d0b79dSTomohiro Misono slot = path->slots[0]; 256423d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 256523d0b79dSTomohiro Misono 256623d0b79dSTomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot); 25673212fa14SJosef Bacik item_len = btrfs_item_size(leaf, slot); 256823d0b79dSTomohiro Misono /* Check if dirid in ROOT_REF corresponds to passed dirid */ 256923d0b79dSTomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 257023d0b79dSTomohiro Misono if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { 257123d0b79dSTomohiro Misono ret = -EINVAL; 257223d0b79dSTomohiro Misono goto out; 257323d0b79dSTomohiro Misono } 257423d0b79dSTomohiro Misono 257523d0b79dSTomohiro Misono /* Copy subvolume's name */ 257623d0b79dSTomohiro Misono item_off += sizeof(struct btrfs_root_ref); 257723d0b79dSTomohiro Misono item_len -= sizeof(struct btrfs_root_ref); 257823d0b79dSTomohiro Misono read_extent_buffer(leaf, args->name, item_off, item_len); 257923d0b79dSTomohiro Misono args->name[item_len] = 0; 258023d0b79dSTomohiro Misono 2581b8a49ae1SJosef Bacik out_put: 258200246528SJosef Bacik btrfs_put_root(root); 258323d0b79dSTomohiro Misono out: 258423d0b79dSTomohiro Misono btrfs_free_path(path); 258523d0b79dSTomohiro Misono return ret; 258623d0b79dSTomohiro Misono } 258723d0b79dSTomohiro Misono 2588ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2589ac8e9819SChris Mason void __user *argp) 2590ac8e9819SChris Mason { 2591ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2592ac8e9819SChris Mason struct inode *inode; 259301b810b8SDavid Sterba int ret = 0; 2594ac8e9819SChris Mason 25952354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 25962354d08fSJulia Lawall if (IS_ERR(args)) 25972354d08fSJulia Lawall return PTR_ERR(args); 2598c2b96929SDan Carpenter 2599496ad9aaSAl Viro inode = file_inode(file); 2600ac8e9819SChris Mason 260101b810b8SDavid Sterba /* 260201b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 260301b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 260401b810b8SDavid Sterba */ 26051b53ac4dSChris Mason if (args->treeid == 0) 26061b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 26071b53ac4dSChris Mason 260801b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 260901b810b8SDavid Sterba args->name[0] = 0; 261001b810b8SDavid Sterba goto out; 261101b810b8SDavid Sterba } 261201b810b8SDavid Sterba 261301b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 261401b810b8SDavid Sterba ret = -EPERM; 261501b810b8SDavid Sterba goto out; 261601b810b8SDavid Sterba } 261701b810b8SDavid Sterba 2618ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2619ac8e9819SChris Mason args->treeid, args->objectid, 2620ac8e9819SChris Mason args->name); 2621ac8e9819SChris Mason 262201b810b8SDavid Sterba out: 2623ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2624ac8e9819SChris Mason ret = -EFAULT; 2625ac8e9819SChris Mason 2626ac8e9819SChris Mason kfree(args); 262798d377a0STARUISI Hiroaki return ret; 262898d377a0STARUISI Hiroaki } 262998d377a0STARUISI Hiroaki 263023d0b79dSTomohiro Misono /* 263123d0b79dSTomohiro Misono * Version of ino_lookup ioctl (unprivileged) 263223d0b79dSTomohiro Misono * 263323d0b79dSTomohiro Misono * The main differences from ino_lookup ioctl are: 263423d0b79dSTomohiro Misono * 263523d0b79dSTomohiro Misono * 1. Read + Exec permission will be checked using inode_permission() during 263623d0b79dSTomohiro Misono * path construction. -EACCES will be returned in case of failure. 263723d0b79dSTomohiro Misono * 2. Path construction will be stopped at the inode number which corresponds 263823d0b79dSTomohiro Misono * to the fd with which this ioctl is called. If constructed path does not 263923d0b79dSTomohiro Misono * exist under fd's inode, -EACCES will be returned. 264023d0b79dSTomohiro Misono * 3. The name of bottom subvolume is also searched and filled. 264123d0b79dSTomohiro Misono */ 264223d0b79dSTomohiro Misono static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) 264323d0b79dSTomohiro Misono { 264423d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args; 264523d0b79dSTomohiro Misono struct inode *inode; 264623d0b79dSTomohiro Misono int ret; 264723d0b79dSTomohiro Misono 264823d0b79dSTomohiro Misono args = memdup_user(argp, sizeof(*args)); 264923d0b79dSTomohiro Misono if (IS_ERR(args)) 265023d0b79dSTomohiro Misono return PTR_ERR(args); 265123d0b79dSTomohiro Misono 265223d0b79dSTomohiro Misono inode = file_inode(file); 265323d0b79dSTomohiro Misono 265423d0b79dSTomohiro Misono if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && 265523d0b79dSTomohiro Misono BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { 265623d0b79dSTomohiro Misono /* 265723d0b79dSTomohiro Misono * The subvolume does not exist under fd with which this is 265823d0b79dSTomohiro Misono * called 265923d0b79dSTomohiro Misono */ 266023d0b79dSTomohiro Misono kfree(args); 266123d0b79dSTomohiro Misono return -EACCES; 266223d0b79dSTomohiro Misono } 266323d0b79dSTomohiro Misono 26646623d9a0SChristian Brauner ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args); 266523d0b79dSTomohiro Misono 266623d0b79dSTomohiro Misono if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 266723d0b79dSTomohiro Misono ret = -EFAULT; 266823d0b79dSTomohiro Misono 266923d0b79dSTomohiro Misono kfree(args); 267023d0b79dSTomohiro Misono return ret; 267123d0b79dSTomohiro Misono } 267223d0b79dSTomohiro Misono 2673b64ec075STomohiro Misono /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ 2674b64ec075STomohiro Misono static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) 2675b64ec075STomohiro Misono { 2676b64ec075STomohiro Misono struct btrfs_ioctl_get_subvol_info_args *subvol_info; 2677b64ec075STomohiro Misono struct btrfs_fs_info *fs_info; 2678b64ec075STomohiro Misono struct btrfs_root *root; 2679b64ec075STomohiro Misono struct btrfs_path *path; 2680b64ec075STomohiro Misono struct btrfs_key key; 2681b64ec075STomohiro Misono struct btrfs_root_item *root_item; 2682b64ec075STomohiro Misono struct btrfs_root_ref *rref; 2683b64ec075STomohiro Misono struct extent_buffer *leaf; 2684b64ec075STomohiro Misono unsigned long item_off; 2685b64ec075STomohiro Misono unsigned long item_len; 2686b64ec075STomohiro Misono struct inode *inode; 2687b64ec075STomohiro Misono int slot; 2688b64ec075STomohiro Misono int ret = 0; 2689b64ec075STomohiro Misono 2690b64ec075STomohiro Misono path = btrfs_alloc_path(); 2691b64ec075STomohiro Misono if (!path) 2692b64ec075STomohiro Misono return -ENOMEM; 2693b64ec075STomohiro Misono 2694b64ec075STomohiro Misono subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); 2695b64ec075STomohiro Misono if (!subvol_info) { 2696b64ec075STomohiro Misono btrfs_free_path(path); 2697b64ec075STomohiro Misono return -ENOMEM; 2698b64ec075STomohiro Misono } 2699b64ec075STomohiro Misono 2700b64ec075STomohiro Misono inode = file_inode(file); 2701b64ec075STomohiro Misono fs_info = BTRFS_I(inode)->root->fs_info; 2702b64ec075STomohiro Misono 2703b64ec075STomohiro Misono /* Get root_item of inode's subvolume */ 2704b64ec075STomohiro Misono key.objectid = BTRFS_I(inode)->root->root_key.objectid; 270556e9357aSDavid Sterba root = btrfs_get_fs_root(fs_info, key.objectid, true); 2706b64ec075STomohiro Misono if (IS_ERR(root)) { 2707b64ec075STomohiro Misono ret = PTR_ERR(root); 270804734e84SJosef Bacik goto out_free; 270904734e84SJosef Bacik } 2710b64ec075STomohiro Misono root_item = &root->root_item; 2711b64ec075STomohiro Misono 2712b64ec075STomohiro Misono subvol_info->treeid = key.objectid; 2713b64ec075STomohiro Misono 2714b64ec075STomohiro Misono subvol_info->generation = btrfs_root_generation(root_item); 2715b64ec075STomohiro Misono subvol_info->flags = btrfs_root_flags(root_item); 2716b64ec075STomohiro Misono 2717b64ec075STomohiro Misono memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); 2718b64ec075STomohiro Misono memcpy(subvol_info->parent_uuid, root_item->parent_uuid, 2719b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2720b64ec075STomohiro Misono memcpy(subvol_info->received_uuid, root_item->received_uuid, 2721b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2722b64ec075STomohiro Misono 2723b64ec075STomohiro Misono subvol_info->ctransid = btrfs_root_ctransid(root_item); 2724b64ec075STomohiro Misono subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); 2725b64ec075STomohiro Misono subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); 2726b64ec075STomohiro Misono 2727b64ec075STomohiro Misono subvol_info->otransid = btrfs_root_otransid(root_item); 2728b64ec075STomohiro Misono subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); 2729b64ec075STomohiro Misono subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); 2730b64ec075STomohiro Misono 2731b64ec075STomohiro Misono subvol_info->stransid = btrfs_root_stransid(root_item); 2732b64ec075STomohiro Misono subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); 2733b64ec075STomohiro Misono subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); 2734b64ec075STomohiro Misono 2735b64ec075STomohiro Misono subvol_info->rtransid = btrfs_root_rtransid(root_item); 2736b64ec075STomohiro Misono subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); 2737b64ec075STomohiro Misono subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); 2738b64ec075STomohiro Misono 2739b64ec075STomohiro Misono if (key.objectid != BTRFS_FS_TREE_OBJECTID) { 2740b64ec075STomohiro Misono /* Search root tree for ROOT_BACKREF of this subvolume */ 2741b64ec075STomohiro Misono key.type = BTRFS_ROOT_BACKREF_KEY; 2742b64ec075STomohiro Misono key.offset = 0; 274304734e84SJosef Bacik ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 2744b64ec075STomohiro Misono if (ret < 0) { 2745b64ec075STomohiro Misono goto out; 2746b64ec075STomohiro Misono } else if (path->slots[0] >= 2747b64ec075STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 274804734e84SJosef Bacik ret = btrfs_next_leaf(fs_info->tree_root, path); 2749b64ec075STomohiro Misono if (ret < 0) { 2750b64ec075STomohiro Misono goto out; 2751b64ec075STomohiro Misono } else if (ret > 0) { 2752b64ec075STomohiro Misono ret = -EUCLEAN; 2753b64ec075STomohiro Misono goto out; 2754b64ec075STomohiro Misono } 2755b64ec075STomohiro Misono } 2756b64ec075STomohiro Misono 2757b64ec075STomohiro Misono leaf = path->nodes[0]; 2758b64ec075STomohiro Misono slot = path->slots[0]; 2759b64ec075STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 2760b64ec075STomohiro Misono if (key.objectid == subvol_info->treeid && 2761b64ec075STomohiro Misono key.type == BTRFS_ROOT_BACKREF_KEY) { 2762b64ec075STomohiro Misono subvol_info->parent_id = key.offset; 2763b64ec075STomohiro Misono 2764b64ec075STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 2765b64ec075STomohiro Misono subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); 2766b64ec075STomohiro Misono 2767b64ec075STomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot) 2768b64ec075STomohiro Misono + sizeof(struct btrfs_root_ref); 27693212fa14SJosef Bacik item_len = btrfs_item_size(leaf, slot) 2770b64ec075STomohiro Misono - sizeof(struct btrfs_root_ref); 2771b64ec075STomohiro Misono read_extent_buffer(leaf, subvol_info->name, 2772b64ec075STomohiro Misono item_off, item_len); 2773b64ec075STomohiro Misono } else { 2774b64ec075STomohiro Misono ret = -ENOENT; 2775b64ec075STomohiro Misono goto out; 2776b64ec075STomohiro Misono } 2777b64ec075STomohiro Misono } 2778b64ec075STomohiro Misono 2779b64ec075STomohiro Misono if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) 2780b64ec075STomohiro Misono ret = -EFAULT; 2781b64ec075STomohiro Misono 2782b64ec075STomohiro Misono out: 278300246528SJosef Bacik btrfs_put_root(root); 278404734e84SJosef Bacik out_free: 2785b64ec075STomohiro Misono btrfs_free_path(path); 2786b091f7feSWaiman Long kfree(subvol_info); 2787b64ec075STomohiro Misono return ret; 2788b64ec075STomohiro Misono } 2789b64ec075STomohiro Misono 279042e4b520STomohiro Misono /* 279142e4b520STomohiro Misono * Return ROOT_REF information of the subvolume containing this inode 279242e4b520STomohiro Misono * except the subvolume name. 279342e4b520STomohiro Misono */ 279442e4b520STomohiro Misono static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) 279542e4b520STomohiro Misono { 279642e4b520STomohiro Misono struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; 279742e4b520STomohiro Misono struct btrfs_root_ref *rref; 279842e4b520STomohiro Misono struct btrfs_root *root; 279942e4b520STomohiro Misono struct btrfs_path *path; 280042e4b520STomohiro Misono struct btrfs_key key; 280142e4b520STomohiro Misono struct extent_buffer *leaf; 280242e4b520STomohiro Misono struct inode *inode; 280342e4b520STomohiro Misono u64 objectid; 280442e4b520STomohiro Misono int slot; 280542e4b520STomohiro Misono int ret; 280642e4b520STomohiro Misono u8 found; 280742e4b520STomohiro Misono 280842e4b520STomohiro Misono path = btrfs_alloc_path(); 280942e4b520STomohiro Misono if (!path) 281042e4b520STomohiro Misono return -ENOMEM; 281142e4b520STomohiro Misono 281242e4b520STomohiro Misono rootrefs = memdup_user(argp, sizeof(*rootrefs)); 281342e4b520STomohiro Misono if (IS_ERR(rootrefs)) { 281442e4b520STomohiro Misono btrfs_free_path(path); 281542e4b520STomohiro Misono return PTR_ERR(rootrefs); 281642e4b520STomohiro Misono } 281742e4b520STomohiro Misono 281842e4b520STomohiro Misono inode = file_inode(file); 281942e4b520STomohiro Misono root = BTRFS_I(inode)->root->fs_info->tree_root; 282042e4b520STomohiro Misono objectid = BTRFS_I(inode)->root->root_key.objectid; 282142e4b520STomohiro Misono 282242e4b520STomohiro Misono key.objectid = objectid; 282342e4b520STomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 282442e4b520STomohiro Misono key.offset = rootrefs->min_treeid; 282542e4b520STomohiro Misono found = 0; 282642e4b520STomohiro Misono 282742e4b520STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 282842e4b520STomohiro Misono if (ret < 0) { 282942e4b520STomohiro Misono goto out; 283042e4b520STomohiro Misono } else if (path->slots[0] >= 283142e4b520STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 283242e4b520STomohiro Misono ret = btrfs_next_leaf(root, path); 283342e4b520STomohiro Misono if (ret < 0) { 283442e4b520STomohiro Misono goto out; 283542e4b520STomohiro Misono } else if (ret > 0) { 283642e4b520STomohiro Misono ret = -EUCLEAN; 283742e4b520STomohiro Misono goto out; 283842e4b520STomohiro Misono } 283942e4b520STomohiro Misono } 284042e4b520STomohiro Misono while (1) { 284142e4b520STomohiro Misono leaf = path->nodes[0]; 284242e4b520STomohiro Misono slot = path->slots[0]; 284342e4b520STomohiro Misono 284442e4b520STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 284542e4b520STomohiro Misono if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { 284642e4b520STomohiro Misono ret = 0; 284742e4b520STomohiro Misono goto out; 284842e4b520STomohiro Misono } 284942e4b520STomohiro Misono 285042e4b520STomohiro Misono if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { 285142e4b520STomohiro Misono ret = -EOVERFLOW; 285242e4b520STomohiro Misono goto out; 285342e4b520STomohiro Misono } 285442e4b520STomohiro Misono 285542e4b520STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 285642e4b520STomohiro Misono rootrefs->rootref[found].treeid = key.offset; 285742e4b520STomohiro Misono rootrefs->rootref[found].dirid = 285842e4b520STomohiro Misono btrfs_root_ref_dirid(leaf, rref); 285942e4b520STomohiro Misono found++; 286042e4b520STomohiro Misono 286142e4b520STomohiro Misono ret = btrfs_next_item(root, path); 286242e4b520STomohiro Misono if (ret < 0) { 286342e4b520STomohiro Misono goto out; 286442e4b520STomohiro Misono } else if (ret > 0) { 286542e4b520STomohiro Misono ret = -EUCLEAN; 286642e4b520STomohiro Misono goto out; 286742e4b520STomohiro Misono } 286842e4b520STomohiro Misono } 286942e4b520STomohiro Misono 287042e4b520STomohiro Misono out: 287142e4b520STomohiro Misono if (!ret || ret == -EOVERFLOW) { 287242e4b520STomohiro Misono rootrefs->num_items = found; 287342e4b520STomohiro Misono /* update min_treeid for next search */ 287442e4b520STomohiro Misono if (found) 287542e4b520STomohiro Misono rootrefs->min_treeid = 287642e4b520STomohiro Misono rootrefs->rootref[found - 1].treeid + 1; 287742e4b520STomohiro Misono if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) 287842e4b520STomohiro Misono ret = -EFAULT; 287942e4b520STomohiro Misono } 288042e4b520STomohiro Misono 288142e4b520STomohiro Misono kfree(rootrefs); 288242e4b520STomohiro Misono btrfs_free_path(path); 288342e4b520STomohiro Misono 288442e4b520STomohiro Misono return ret; 288542e4b520STomohiro Misono } 288642e4b520STomohiro Misono 288776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 2888949964c9SMarcos Paulo de Souza void __user *arg, 2889949964c9SMarcos Paulo de Souza bool destroy_v2) 289076dda93cSYan, Zheng { 289154563d41SAl Viro struct dentry *parent = file->f_path.dentry; 28920b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 289376dda93cSYan, Zheng struct dentry *dentry; 28942b0143b5SDavid Howells struct inode *dir = d_inode(parent); 289576dda93cSYan, Zheng struct inode *inode; 289676dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 289776dda93cSYan, Zheng struct btrfs_root *dest = NULL; 2898949964c9SMarcos Paulo de Souza struct btrfs_ioctl_vol_args *vol_args = NULL; 2899949964c9SMarcos Paulo de Souza struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; 2900c4ed533bSChristian Brauner struct user_namespace *mnt_userns = file_mnt_user_ns(file); 2901949964c9SMarcos Paulo de Souza char *subvol_name, *subvol_name_ptr = NULL; 2902949964c9SMarcos Paulo de Souza int subvol_namelen; 290376dda93cSYan, Zheng int err = 0; 2904949964c9SMarcos Paulo de Souza bool destroy_parent = false; 290576dda93cSYan, Zheng 2906949964c9SMarcos Paulo de Souza if (destroy_v2) { 2907949964c9SMarcos Paulo de Souza vol_args2 = memdup_user(arg, sizeof(*vol_args2)); 2908949964c9SMarcos Paulo de Souza if (IS_ERR(vol_args2)) 2909949964c9SMarcos Paulo de Souza return PTR_ERR(vol_args2); 2910325c50e3SJeff Mahoney 2911949964c9SMarcos Paulo de Souza if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { 2912949964c9SMarcos Paulo de Souza err = -EOPNOTSUPP; 2913949964c9SMarcos Paulo de Souza goto out; 2914949964c9SMarcos Paulo de Souza } 291576dda93cSYan, Zheng 2916949964c9SMarcos Paulo de Souza /* 2917949964c9SMarcos Paulo de Souza * If SPEC_BY_ID is not set, we are looking for the subvolume by 2918949964c9SMarcos Paulo de Souza * name, same as v1 currently does. 2919949964c9SMarcos Paulo de Souza */ 2920949964c9SMarcos Paulo de Souza if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { 2921949964c9SMarcos Paulo de Souza vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; 2922949964c9SMarcos Paulo de Souza subvol_name = vol_args2->name; 2923949964c9SMarcos Paulo de Souza 2924949964c9SMarcos Paulo de Souza err = mnt_want_write_file(file); 2925949964c9SMarcos Paulo de Souza if (err) 2926949964c9SMarcos Paulo de Souza goto out; 2927949964c9SMarcos Paulo de Souza } else { 2928aabb34e7SChristian Brauner struct inode *old_dir; 2929c4ed533bSChristian Brauner 2930949964c9SMarcos Paulo de Souza if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { 293176dda93cSYan, Zheng err = -EINVAL; 293276dda93cSYan, Zheng goto out; 293376dda93cSYan, Zheng } 293476dda93cSYan, Zheng 2935a561be71SAl Viro err = mnt_want_write_file(file); 293676dda93cSYan, Zheng if (err) 293776dda93cSYan, Zheng goto out; 293876dda93cSYan, Zheng 2939949964c9SMarcos Paulo de Souza dentry = btrfs_get_dentry(fs_info->sb, 2940949964c9SMarcos Paulo de Souza BTRFS_FIRST_FREE_OBJECTID, 2941949964c9SMarcos Paulo de Souza vol_args2->subvolid, 0, 0); 2942949964c9SMarcos Paulo de Souza if (IS_ERR(dentry)) { 2943949964c9SMarcos Paulo de Souza err = PTR_ERR(dentry); 2944949964c9SMarcos Paulo de Souza goto out_drop_write; 2945949964c9SMarcos Paulo de Souza } 2946949964c9SMarcos Paulo de Souza 2947949964c9SMarcos Paulo de Souza /* 2948949964c9SMarcos Paulo de Souza * Change the default parent since the subvolume being 2949949964c9SMarcos Paulo de Souza * deleted can be outside of the current mount point. 2950949964c9SMarcos Paulo de Souza */ 2951949964c9SMarcos Paulo de Souza parent = btrfs_get_parent(dentry); 2952949964c9SMarcos Paulo de Souza 2953949964c9SMarcos Paulo de Souza /* 2954949964c9SMarcos Paulo de Souza * At this point dentry->d_name can point to '/' if the 2955949964c9SMarcos Paulo de Souza * subvolume we want to destroy is outsite of the 2956949964c9SMarcos Paulo de Souza * current mount point, so we need to release the 2957949964c9SMarcos Paulo de Souza * current dentry and execute the lookup to return a new 2958949964c9SMarcos Paulo de Souza * one with ->d_name pointing to the 2959949964c9SMarcos Paulo de Souza * <mount point>/subvol_name. 2960949964c9SMarcos Paulo de Souza */ 2961949964c9SMarcos Paulo de Souza dput(dentry); 2962949964c9SMarcos Paulo de Souza if (IS_ERR(parent)) { 2963949964c9SMarcos Paulo de Souza err = PTR_ERR(parent); 2964949964c9SMarcos Paulo de Souza goto out_drop_write; 2965949964c9SMarcos Paulo de Souza } 2966aabb34e7SChristian Brauner old_dir = dir; 2967949964c9SMarcos Paulo de Souza dir = d_inode(parent); 2968949964c9SMarcos Paulo de Souza 2969949964c9SMarcos Paulo de Souza /* 2970949964c9SMarcos Paulo de Souza * If v2 was used with SPEC_BY_ID, a new parent was 2971949964c9SMarcos Paulo de Souza * allocated since the subvolume can be outside of the 2972949964c9SMarcos Paulo de Souza * current mount point. Later on we need to release this 2973949964c9SMarcos Paulo de Souza * new parent dentry. 2974949964c9SMarcos Paulo de Souza */ 2975949964c9SMarcos Paulo de Souza destroy_parent = true; 2976949964c9SMarcos Paulo de Souza 2977aabb34e7SChristian Brauner /* 2978aabb34e7SChristian Brauner * On idmapped mounts, deletion via subvolid is 2979aabb34e7SChristian Brauner * restricted to subvolumes that are immediate 2980aabb34e7SChristian Brauner * ancestors of the inode referenced by the file 2981aabb34e7SChristian Brauner * descriptor in the ioctl. Otherwise the idmapping 2982aabb34e7SChristian Brauner * could potentially be abused to delete subvolumes 2983aabb34e7SChristian Brauner * anywhere in the filesystem the user wouldn't be able 2984aabb34e7SChristian Brauner * to delete without an idmapped mount. 2985aabb34e7SChristian Brauner */ 2986aabb34e7SChristian Brauner if (old_dir != dir && mnt_userns != &init_user_ns) { 2987aabb34e7SChristian Brauner err = -EOPNOTSUPP; 2988aabb34e7SChristian Brauner goto free_parent; 2989aabb34e7SChristian Brauner } 2990aabb34e7SChristian Brauner 2991949964c9SMarcos Paulo de Souza subvol_name_ptr = btrfs_get_subvol_name_from_objectid( 2992949964c9SMarcos Paulo de Souza fs_info, vol_args2->subvolid); 2993949964c9SMarcos Paulo de Souza if (IS_ERR(subvol_name_ptr)) { 2994949964c9SMarcos Paulo de Souza err = PTR_ERR(subvol_name_ptr); 2995949964c9SMarcos Paulo de Souza goto free_parent; 2996949964c9SMarcos Paulo de Souza } 29971a9fd417SDavid Sterba /* subvol_name_ptr is already nul terminated */ 2998949964c9SMarcos Paulo de Souza subvol_name = (char *)kbasename(subvol_name_ptr); 2999949964c9SMarcos Paulo de Souza } 3000949964c9SMarcos Paulo de Souza } else { 3001949964c9SMarcos Paulo de Souza vol_args = memdup_user(arg, sizeof(*vol_args)); 3002949964c9SMarcos Paulo de Souza if (IS_ERR(vol_args)) 3003949964c9SMarcos Paulo de Souza return PTR_ERR(vol_args); 3004949964c9SMarcos Paulo de Souza 3005949964c9SMarcos Paulo de Souza vol_args->name[BTRFS_PATH_NAME_MAX] = 0; 3006949964c9SMarcos Paulo de Souza subvol_name = vol_args->name; 3007949964c9SMarcos Paulo de Souza 3008949964c9SMarcos Paulo de Souza err = mnt_want_write_file(file); 3009949964c9SMarcos Paulo de Souza if (err) 3010949964c9SMarcos Paulo de Souza goto out; 3011949964c9SMarcos Paulo de Souza } 3012949964c9SMarcos Paulo de Souza 3013949964c9SMarcos Paulo de Souza subvol_namelen = strlen(subvol_name); 3014949964c9SMarcos Paulo de Souza 3015949964c9SMarcos Paulo de Souza if (strchr(subvol_name, '/') || 3016949964c9SMarcos Paulo de Souza strncmp(subvol_name, "..", subvol_namelen) == 0) { 3017949964c9SMarcos Paulo de Souza err = -EINVAL; 3018949964c9SMarcos Paulo de Souza goto free_subvol_name; 3019949964c9SMarcos Paulo de Souza } 3020949964c9SMarcos Paulo de Souza 3021949964c9SMarcos Paulo de Souza if (!S_ISDIR(dir->i_mode)) { 3022949964c9SMarcos Paulo de Souza err = -ENOTDIR; 3023949964c9SMarcos Paulo de Souza goto free_subvol_name; 3024949964c9SMarcos Paulo de Souza } 3025521e0546SDavid Sterba 302600235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 302700235411SAl Viro if (err == -EINTR) 3028949964c9SMarcos Paulo de Souza goto free_subvol_name; 3029c4ed533bSChristian Brauner dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen); 303076dda93cSYan, Zheng if (IS_ERR(dentry)) { 303176dda93cSYan, Zheng err = PTR_ERR(dentry); 303276dda93cSYan, Zheng goto out_unlock_dir; 303376dda93cSYan, Zheng } 303476dda93cSYan, Zheng 30352b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 303676dda93cSYan, Zheng err = -ENOENT; 303776dda93cSYan, Zheng goto out_dput; 303876dda93cSYan, Zheng } 303976dda93cSYan, Zheng 30402b0143b5SDavid Howells inode = d_inode(dentry); 30414260f7c7SSage Weil dest = BTRFS_I(inode)->root; 30424260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 30434260f7c7SSage Weil /* 30444260f7c7SSage Weil * Regular user. Only allow this with a special mount 30454260f7c7SSage Weil * option, when the user has write+exec access to the 30464260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 30474260f7c7SSage Weil * allowed. 30484260f7c7SSage Weil * 30494260f7c7SSage Weil * Note that this is _not_ check that the subvol is 30504260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 30514260f7c7SSage Weil * otherwise be able to delete. 30524260f7c7SSage Weil * 30534260f7c7SSage Weil * Users who want to delete empty subvols should try 30544260f7c7SSage Weil * rmdir(2). 30554260f7c7SSage Weil */ 30564260f7c7SSage Weil err = -EPERM; 30570b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 30584260f7c7SSage Weil goto out_dput; 30594260f7c7SSage Weil 30604260f7c7SSage Weil /* 30614260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 30624260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 30634260f7c7SSage Weil * must be called on the dentry referencing the root 30644260f7c7SSage Weil * of the subvol, not a random directory contained 30654260f7c7SSage Weil * within it. 30664260f7c7SSage Weil */ 30674260f7c7SSage Weil err = -EINVAL; 30684260f7c7SSage Weil if (root == dest) 30694260f7c7SSage Weil goto out_dput; 30704260f7c7SSage Weil 3071c4ed533bSChristian Brauner err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC); 30724260f7c7SSage Weil if (err) 30734260f7c7SSage Weil goto out_dput; 30745c39da5bSMiao Xie } 30754260f7c7SSage Weil 30765c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 3077c4ed533bSChristian Brauner err = btrfs_may_delete(mnt_userns, dir, dentry, 1); 30784260f7c7SSage Weil if (err) 30794260f7c7SSage Weil goto out_dput; 30804260f7c7SSage Weil 30814a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 308276dda93cSYan, Zheng err = -EINVAL; 308376dda93cSYan, Zheng goto out_dput; 308476dda93cSYan, Zheng } 308576dda93cSYan, Zheng 308664708539SJosef Bacik btrfs_inode_lock(inode, 0); 3087f60a2364SMisono Tomohiro err = btrfs_delete_subvolume(dir, dentry); 308864708539SJosef Bacik btrfs_inode_unlock(inode, 0); 308946008d9dSAmir Goldstein if (!err) { 309046008d9dSAmir Goldstein fsnotify_rmdir(dir, dentry); 309176dda93cSYan, Zheng d_delete(dentry); 309246008d9dSAmir Goldstein } 3093fa6ac876SLiu Bo 309476dda93cSYan, Zheng out_dput: 309576dda93cSYan, Zheng dput(dentry); 309676dda93cSYan, Zheng out_unlock_dir: 309764708539SJosef Bacik btrfs_inode_unlock(dir, 0); 3098949964c9SMarcos Paulo de Souza free_subvol_name: 3099949964c9SMarcos Paulo de Souza kfree(subvol_name_ptr); 3100949964c9SMarcos Paulo de Souza free_parent: 3101949964c9SMarcos Paulo de Souza if (destroy_parent) 3102949964c9SMarcos Paulo de Souza dput(parent); 310300235411SAl Viro out_drop_write: 31042a79f17eSAl Viro mnt_drop_write_file(file); 310576dda93cSYan, Zheng out: 3106949964c9SMarcos Paulo de Souza kfree(vol_args2); 310776dda93cSYan, Zheng kfree(vol_args); 310876dda93cSYan, Zheng return err; 310976dda93cSYan, Zheng } 311076dda93cSYan, Zheng 31111e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 3112f46b5a66SChristoph Hellwig { 3113496ad9aaSAl Viro struct inode *inode = file_inode(file); 3114f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3115c853a578SGoldwyn Rodrigues struct btrfs_ioctl_defrag_range_args range = {0}; 3116c146afadSYan Zheng int ret; 3117c146afadSYan Zheng 311825122d15SIlya Dryomov ret = mnt_want_write_file(file); 311925122d15SIlya Dryomov if (ret) 312025122d15SIlya Dryomov return ret; 3121b83cc969SLi Zefan 312225122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 312325122d15SIlya Dryomov ret = -EROFS; 312425122d15SIlya Dryomov goto out; 31255ac00addSStefan Behrens } 3126f46b5a66SChristoph Hellwig 3127f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 3128f46b5a66SChristoph Hellwig case S_IFDIR: 3129e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 3130e441d54dSChris Mason ret = -EPERM; 3131e441d54dSChris Mason goto out; 3132e441d54dSChris Mason } 3133de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 3134f46b5a66SChristoph Hellwig break; 3135f46b5a66SChristoph Hellwig case S_IFREG: 3136616d374eSAdam Borowski /* 3137616d374eSAdam Borowski * Note that this does not check the file descriptor for write 3138616d374eSAdam Borowski * access. This prevents defragmenting executables that are 3139616d374eSAdam Borowski * running and allows defrag on files open in read-only mode. 3140616d374eSAdam Borowski */ 3141616d374eSAdam Borowski if (!capable(CAP_SYS_ADMIN) && 314247291baaSChristian Brauner inode_permission(&init_user_ns, inode, MAY_WRITE)) { 3143616d374eSAdam Borowski ret = -EPERM; 3144e441d54dSChris Mason goto out; 3145e441d54dSChris Mason } 31461e701a32SChris Mason 31471e701a32SChris Mason if (argp) { 3148c853a578SGoldwyn Rodrigues if (copy_from_user(&range, argp, sizeof(range))) { 31491e701a32SChris Mason ret = -EFAULT; 3150683be16eSDan Carpenter goto out; 31511e701a32SChris Mason } 31521e701a32SChris Mason /* compression requires us to start the IO */ 3153c853a578SGoldwyn Rodrigues if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 3154c853a578SGoldwyn Rodrigues range.flags |= BTRFS_DEFRAG_RANGE_START_IO; 3155c853a578SGoldwyn Rodrigues range.extent_thresh = (u32)-1; 31561e701a32SChris Mason } 31571e701a32SChris Mason } else { 31581e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 3159c853a578SGoldwyn Rodrigues range.len = (u64)-1; 31601e701a32SChris Mason } 31611ccc2e8aSQu Wenruo ret = btrfs_defrag_file(file_inode(file), &file->f_ra, 3162c853a578SGoldwyn Rodrigues &range, BTRFS_OLDEST_GENERATION, 0); 31634cb5300bSChris Mason if (ret > 0) 31644cb5300bSChris Mason ret = 0; 3165f46b5a66SChristoph Hellwig break; 31668929ecfaSYan, Zheng default: 31678929ecfaSYan, Zheng ret = -EINVAL; 3168f46b5a66SChristoph Hellwig } 3169e441d54dSChris Mason out: 317025122d15SIlya Dryomov mnt_drop_write_file(file); 3171e441d54dSChris Mason return ret; 3172f46b5a66SChristoph Hellwig } 3173f46b5a66SChristoph Hellwig 31742ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 3175f46b5a66SChristoph Hellwig { 3176f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3177*a174c0a2SNikolay Borisov bool restore_op = false; 3178f46b5a66SChristoph Hellwig int ret; 3179f46b5a66SChristoph Hellwig 3180e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3181e441d54dSChris Mason return -EPERM; 3182e441d54dSChris Mason 3183*a174c0a2SNikolay Borisov if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) { 3184*a174c0a2SNikolay Borisov if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD)) 3185e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3186c9e9f97bSIlya Dryomov 3187*a174c0a2SNikolay Borisov /* 3188*a174c0a2SNikolay Borisov * We can do the device add because we have a paused balanced, 3189*a174c0a2SNikolay Borisov * change the exclusive op type and remember we should bring 3190*a174c0a2SNikolay Borisov * back the paused balance 3191*a174c0a2SNikolay Borisov */ 3192*a174c0a2SNikolay Borisov fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD; 3193*a174c0a2SNikolay Borisov btrfs_exclop_start_unlock(fs_info); 3194*a174c0a2SNikolay Borisov restore_op = true; 3195*a174c0a2SNikolay Borisov } 3196*a174c0a2SNikolay Borisov 3197dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3198c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3199c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3200c9e9f97bSIlya Dryomov goto out; 3201c9e9f97bSIlya Dryomov } 3202f46b5a66SChristoph Hellwig 32035516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 32042ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 3205f46b5a66SChristoph Hellwig 320643d20761SAnand Jain if (!ret) 32070b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 320843d20761SAnand Jain 3209f46b5a66SChristoph Hellwig kfree(vol_args); 3210c9e9f97bSIlya Dryomov out: 3211*a174c0a2SNikolay Borisov if (restore_op) 3212*a174c0a2SNikolay Borisov btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED); 3213*a174c0a2SNikolay Borisov else 3214c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 3215f46b5a66SChristoph Hellwig return ret; 3216f46b5a66SChristoph Hellwig } 3217f46b5a66SChristoph Hellwig 32186b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 3219f46b5a66SChristoph Hellwig { 32201a15eb72SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 32210b246afaSJeff Mahoney struct inode *inode = file_inode(file); 32220b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 32236b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 32243fa421deSJosef Bacik struct block_device *bdev = NULL; 32253fa421deSJosef Bacik fmode_t mode; 3226f46b5a66SChristoph Hellwig int ret; 322767ae34b6SDavid Sterba bool cancel = false; 3228f46b5a66SChristoph Hellwig 3229e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3230e441d54dSChris Mason return -EPERM; 3231e441d54dSChris Mason 3232dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3233d815b3f2SDan Carpenter if (IS_ERR(vol_args)) 3234d815b3f2SDan Carpenter return PTR_ERR(vol_args); 3235f46b5a66SChristoph Hellwig 3236748449cdSDavid Sterba if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { 3237fd4e994bSOmar Sandoval ret = -EOPNOTSUPP; 3238fd4e994bSOmar Sandoval goto out; 3239fd4e994bSOmar Sandoval } 32401a15eb72SJosef Bacik 32416b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 32421a15eb72SJosef Bacik if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 32431a15eb72SJosef Bacik args.devid = vol_args->devid; 32441a15eb72SJosef Bacik } else if (!strcmp("cancel", vol_args->name)) { 324567ae34b6SDavid Sterba cancel = true; 32461a15eb72SJosef Bacik } else { 32471a15eb72SJosef Bacik ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); 32481a15eb72SJosef Bacik if (ret) 32491a15eb72SJosef Bacik goto out; 32501a15eb72SJosef Bacik } 32511a15eb72SJosef Bacik 32521a15eb72SJosef Bacik ret = mnt_want_write_file(file); 32531a15eb72SJosef Bacik if (ret) 32541a15eb72SJosef Bacik goto out; 325567ae34b6SDavid Sterba 325667ae34b6SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, 325767ae34b6SDavid Sterba cancel); 325867ae34b6SDavid Sterba if (ret) 32591a15eb72SJosef Bacik goto err_drop; 326067ae34b6SDavid Sterba 32611a15eb72SJosef Bacik /* Exclusive operation is now claimed */ 32621a15eb72SJosef Bacik ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); 326367ae34b6SDavid Sterba 3264c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 3265183860f6SAnand Jain 32666b526ed7SAnand Jain if (!ret) { 3267735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 32680b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 32696b526ed7SAnand Jain vol_args->devid); 32706b526ed7SAnand Jain else 32710b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 32726b526ed7SAnand Jain vol_args->name); 32736b526ed7SAnand Jain } 3274c47ca32dSDan Carpenter err_drop: 32754ac20c70SIlya Dryomov mnt_drop_write_file(file); 32763fa421deSJosef Bacik if (bdev) 32773fa421deSJosef Bacik blkdev_put(bdev, mode); 32781a15eb72SJosef Bacik out: 32791a15eb72SJosef Bacik btrfs_put_dev_args_from_path(&args); 32801a15eb72SJosef Bacik kfree(vol_args); 3281f46b5a66SChristoph Hellwig return ret; 3282f46b5a66SChristoph Hellwig } 3283f46b5a66SChristoph Hellwig 3284f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 3285f46b5a66SChristoph Hellwig { 32861a15eb72SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 32870b246afaSJeff Mahoney struct inode *inode = file_inode(file); 32880b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3289f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 32903fa421deSJosef Bacik struct block_device *bdev = NULL; 32913fa421deSJosef Bacik fmode_t mode; 3292f46b5a66SChristoph Hellwig int ret; 329367ae34b6SDavid Sterba bool cancel; 3294f46b5a66SChristoph Hellwig 3295f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3296f46b5a66SChristoph Hellwig return -EPERM; 3297f46b5a66SChristoph Hellwig 32981a15eb72SJosef Bacik vol_args = memdup_user(arg, sizeof(*vol_args)); 32991a15eb72SJosef Bacik if (IS_ERR(vol_args)) 33001a15eb72SJosef Bacik return PTR_ERR(vol_args); 33011a15eb72SJosef Bacik 33021a15eb72SJosef Bacik vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 33031a15eb72SJosef Bacik if (!strcmp("cancel", vol_args->name)) { 33041a15eb72SJosef Bacik cancel = true; 33051a15eb72SJosef Bacik } else { 33061a15eb72SJosef Bacik ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); 33071a15eb72SJosef Bacik if (ret) 33081a15eb72SJosef Bacik goto out; 33091a15eb72SJosef Bacik } 33101a15eb72SJosef Bacik 3311f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 3312f46b5a66SChristoph Hellwig if (ret) 33131a15eb72SJosef Bacik goto out; 3314f46b5a66SChristoph Hellwig 331567ae34b6SDavid Sterba ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, 331667ae34b6SDavid Sterba cancel); 331767ae34b6SDavid Sterba if (ret == 0) { 33181a15eb72SJosef Bacik ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); 3319f46b5a66SChristoph Hellwig if (!ret) 33200b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 3321c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 332267ae34b6SDavid Sterba } 332367ae34b6SDavid Sterba 3324f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 33253fa421deSJosef Bacik if (bdev) 33263fa421deSJosef Bacik blkdev_put(bdev, mode); 33271a15eb72SJosef Bacik out: 33281a15eb72SJosef Bacik btrfs_put_dev_args_from_path(&args); 33291a15eb72SJosef Bacik kfree(vol_args); 3330f46b5a66SChristoph Hellwig return ret; 3331f46b5a66SChristoph Hellwig } 3332f46b5a66SChristoph Hellwig 33332ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 33342ff7e61eSJeff Mahoney void __user *arg) 3335475f6387SJan Schmidt { 3336027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 3337475f6387SJan Schmidt struct btrfs_device *device; 33380b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3339137c5418SJohannes Thumshirn u64 flags_in; 3340027ed2f0SLi Zefan int ret = 0; 3341475f6387SJan Schmidt 3342137c5418SJohannes Thumshirn fi_args = memdup_user(arg, sizeof(*fi_args)); 3343137c5418SJohannes Thumshirn if (IS_ERR(fi_args)) 3344137c5418SJohannes Thumshirn return PTR_ERR(fi_args); 3345137c5418SJohannes Thumshirn 3346137c5418SJohannes Thumshirn flags_in = fi_args->flags; 3347137c5418SJohannes Thumshirn memset(fi_args, 0, sizeof(*fi_args)); 3348027ed2f0SLi Zefan 3349d03262c7SDavid Sterba rcu_read_lock(); 3350027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 3351475f6387SJan Schmidt 3352d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 3353027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 3354027ed2f0SLi Zefan fi_args->max_id = device->devid; 3355475f6387SJan Schmidt } 3356d03262c7SDavid Sterba rcu_read_unlock(); 3357475f6387SJan Schmidt 3358de37aa51SNikolay Borisov memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); 3359bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 3360bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 3361bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 336280a773fbSDavid Sterba 3363137c5418SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) { 3364137c5418SJohannes Thumshirn fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy); 3365137c5418SJohannes Thumshirn fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy); 3366137c5418SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO; 3367137c5418SJohannes Thumshirn } 3368137c5418SJohannes Thumshirn 33690fb408a5SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) { 33700fb408a5SJohannes Thumshirn fi_args->generation = fs_info->generation; 33710fb408a5SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION; 33720fb408a5SJohannes Thumshirn } 33730fb408a5SJohannes Thumshirn 337449bac897SJohannes Thumshirn if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) { 337549bac897SJohannes Thumshirn memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid, 337649bac897SJohannes Thumshirn sizeof(fi_args->metadata_uuid)); 337749bac897SJohannes Thumshirn fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID; 337849bac897SJohannes Thumshirn } 337949bac897SJohannes Thumshirn 3380027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 3381027ed2f0SLi Zefan ret = -EFAULT; 3382475f6387SJan Schmidt 3383027ed2f0SLi Zefan kfree(fi_args); 3384027ed2f0SLi Zefan return ret; 3385475f6387SJan Schmidt } 3386475f6387SJan Schmidt 33872ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 33882ff7e61eSJeff Mahoney void __user *arg) 3389475f6387SJan Schmidt { 3390562d7b15SJosef Bacik BTRFS_DEV_LOOKUP_ARGS(args); 3391475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 3392475f6387SJan Schmidt struct btrfs_device *dev; 3393475f6387SJan Schmidt int ret = 0; 3394475f6387SJan Schmidt 3395475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 3396475f6387SJan Schmidt if (IS_ERR(di_args)) 3397475f6387SJan Schmidt return PTR_ERR(di_args); 3398475f6387SJan Schmidt 3399562d7b15SJosef Bacik args.devid = di_args->devid; 3400dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 3401562d7b15SJosef Bacik args.uuid = di_args->uuid; 3402475f6387SJan Schmidt 3403c5593ca3SDavid Sterba rcu_read_lock(); 3404562d7b15SJosef Bacik dev = btrfs_find_device(fs_info->fs_devices, &args); 3405475f6387SJan Schmidt if (!dev) { 3406475f6387SJan Schmidt ret = -ENODEV; 3407475f6387SJan Schmidt goto out; 3408475f6387SJan Schmidt } 3409475f6387SJan Schmidt 3410475f6387SJan Schmidt di_args->devid = dev->devid; 34117cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 34127cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 3413475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 3414a27202fbSJim Meyering if (dev->name) { 3415672d5990SMisono Tomohiro strncpy(di_args->path, rcu_str_deref(dev->name), 3416672d5990SMisono Tomohiro sizeof(di_args->path) - 1); 3417a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 3418a27202fbSJim Meyering } else { 341999ba55adSStefan Behrens di_args->path[0] = '\0'; 3420a27202fbSJim Meyering } 3421475f6387SJan Schmidt 3422475f6387SJan Schmidt out: 3423c5593ca3SDavid Sterba rcu_read_unlock(); 3424475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 3425475f6387SJan Schmidt ret = -EFAULT; 3426475f6387SJan Schmidt 3427475f6387SJan Schmidt kfree(di_args); 3428475f6387SJan Schmidt return ret; 3429475f6387SJan Schmidt } 3430475f6387SJan Schmidt 34316ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 34326ef5ed0dSJosef Bacik { 3433496ad9aaSAl Viro struct inode *inode = file_inode(file); 34340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 34356ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 34366ef5ed0dSJosef Bacik struct btrfs_root *new_root; 34376ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 34386ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 34392a2b5d62SJosef Bacik struct btrfs_path *path = NULL; 34406ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 34416ef5ed0dSJosef Bacik u64 objectid = 0; 34426ef5ed0dSJosef Bacik u64 dir_id; 34433c04ce01SMiao Xie int ret; 34446ef5ed0dSJosef Bacik 34456ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 34466ef5ed0dSJosef Bacik return -EPERM; 34476ef5ed0dSJosef Bacik 34483c04ce01SMiao Xie ret = mnt_want_write_file(file); 34493c04ce01SMiao Xie if (ret) 34503c04ce01SMiao Xie return ret; 34513c04ce01SMiao Xie 34523c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 34533c04ce01SMiao Xie ret = -EFAULT; 34543c04ce01SMiao Xie goto out; 34553c04ce01SMiao Xie } 34566ef5ed0dSJosef Bacik 34576ef5ed0dSJosef Bacik if (!objectid) 34581cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 34596ef5ed0dSJosef Bacik 346056e9357aSDavid Sterba new_root = btrfs_get_fs_root(fs_info, objectid, true); 34613c04ce01SMiao Xie if (IS_ERR(new_root)) { 34623c04ce01SMiao Xie ret = PTR_ERR(new_root); 34633c04ce01SMiao Xie goto out; 34643c04ce01SMiao Xie } 34652a2b5d62SJosef Bacik if (!is_fstree(new_root->root_key.objectid)) { 34662a2b5d62SJosef Bacik ret = -ENOENT; 34672a2b5d62SJosef Bacik goto out_free; 34682a2b5d62SJosef Bacik } 34696ef5ed0dSJosef Bacik 34706ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 34713c04ce01SMiao Xie if (!path) { 34723c04ce01SMiao Xie ret = -ENOMEM; 34732a2b5d62SJosef Bacik goto out_free; 34743c04ce01SMiao Xie } 34756ef5ed0dSJosef Bacik 34766ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 347798d5dc13STsutomu Itoh if (IS_ERR(trans)) { 34783c04ce01SMiao Xie ret = PTR_ERR(trans); 34792a2b5d62SJosef Bacik goto out_free; 34806ef5ed0dSJosef Bacik } 34816ef5ed0dSJosef Bacik 34820b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 34830b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 34846ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3485cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 34862a2b5d62SJosef Bacik btrfs_release_path(path); 34873a45bb20SJeff Mahoney btrfs_end_transaction(trans); 34880b246afaSJeff Mahoney btrfs_err(fs_info, 34895d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 34903c04ce01SMiao Xie ret = -ENOENT; 34912a2b5d62SJosef Bacik goto out_free; 34926ef5ed0dSJosef Bacik } 34936ef5ed0dSJosef Bacik 34946ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 34956ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 34966ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 34972a2b5d62SJosef Bacik btrfs_release_path(path); 34986ef5ed0dSJosef Bacik 34990b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 35003a45bb20SJeff Mahoney btrfs_end_transaction(trans); 35012a2b5d62SJosef Bacik out_free: 350200246528SJosef Bacik btrfs_put_root(new_root); 35032a2b5d62SJosef Bacik btrfs_free_path(path); 35043c04ce01SMiao Xie out: 35053c04ce01SMiao Xie mnt_drop_write_file(file); 35063c04ce01SMiao Xie return ret; 35076ef5ed0dSJosef Bacik } 35086ef5ed0dSJosef Bacik 3509c065f5b1SSu Yue static void get_block_group_info(struct list_head *groups_list, 3510bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3511bf5fc093SJosef Bacik { 351232da5386SDavid Sterba struct btrfs_block_group *block_group; 3513bf5fc093SJosef Bacik 3514bf5fc093SJosef Bacik space->total_bytes = 0; 3515bf5fc093SJosef Bacik space->used_bytes = 0; 3516bf5fc093SJosef Bacik space->flags = 0; 3517bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3518bf5fc093SJosef Bacik space->flags = block_group->flags; 3519b3470b5dSDavid Sterba space->total_bytes += block_group->length; 3520bf38be65SDavid Sterba space->used_bytes += block_group->used; 3521bf5fc093SJosef Bacik } 3522bf5fc093SJosef Bacik } 3523bf5fc093SJosef Bacik 35242ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 35252ff7e61eSJeff Mahoney void __user *arg) 35261406e432SJosef Bacik { 35271406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 35281406e432SJosef Bacik struct btrfs_ioctl_space_info space; 35291406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 35307fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 353113f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 35321406e432SJosef Bacik struct btrfs_space_info *info; 3533315d8e98SColin Ian King static const u64 types[] = { 3534315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 3535bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3536bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3537315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 3538315d8e98SColin Ian King }; 3539bf5fc093SJosef Bacik int num_types = 4; 35407fde62bfSChris Mason int alloc_size; 35411406e432SJosef Bacik int ret = 0; 354251788b1bSDan Rosenberg u64 slot_count = 0; 3543bf5fc093SJosef Bacik int i, c; 35441406e432SJosef Bacik 35451406e432SJosef Bacik if (copy_from_user(&space_args, 35461406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 35471406e432SJosef Bacik sizeof(space_args))) 35481406e432SJosef Bacik return -EFAULT; 35491406e432SJosef Bacik 3550bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3551bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3552bf5fc093SJosef Bacik 3553bf5fc093SJosef Bacik info = NULL; 355472804905SJosef Bacik list_for_each_entry(tmp, &fs_info->space_info, list) { 3555bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3556bf5fc093SJosef Bacik info = tmp; 3557bf5fc093SJosef Bacik break; 3558bf5fc093SJosef Bacik } 3559bf5fc093SJosef Bacik } 35601406e432SJosef Bacik 3561bf5fc093SJosef Bacik if (!info) 3562bf5fc093SJosef Bacik continue; 3563bf5fc093SJosef Bacik 3564bf5fc093SJosef Bacik down_read(&info->groups_sem); 3565bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3566bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3567bf5fc093SJosef Bacik slot_count++; 3568bf5fc093SJosef Bacik } 3569bf5fc093SJosef Bacik up_read(&info->groups_sem); 3570bf5fc093SJosef Bacik } 3571bf5fc093SJosef Bacik 357236523e95SDavid Sterba /* 357336523e95SDavid Sterba * Global block reserve, exported as a space_info 357436523e95SDavid Sterba */ 357536523e95SDavid Sterba slot_count++; 357636523e95SDavid Sterba 35777fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 35787fde62bfSChris Mason if (space_args.space_slots == 0) { 35797fde62bfSChris Mason space_args.total_spaces = slot_count; 35807fde62bfSChris Mason goto out; 35817fde62bfSChris Mason } 3582bf5fc093SJosef Bacik 358351788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3584bf5fc093SJosef Bacik 35857fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3586bf5fc093SJosef Bacik 35877fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 35887fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 35897fde62bfSChris Mason */ 359009cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 35917fde62bfSChris Mason return -ENOMEM; 35927fde62bfSChris Mason 35937fde62bfSChris Mason space_args.total_spaces = 0; 35948d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 35957fde62bfSChris Mason if (!dest) 35967fde62bfSChris Mason return -ENOMEM; 35977fde62bfSChris Mason dest_orig = dest; 35987fde62bfSChris Mason 35997fde62bfSChris Mason /* now we have a buffer to copy into */ 3600bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3601bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3602bf5fc093SJosef Bacik 360351788b1bSDan Rosenberg if (!slot_count) 360451788b1bSDan Rosenberg break; 360551788b1bSDan Rosenberg 3606bf5fc093SJosef Bacik info = NULL; 360772804905SJosef Bacik list_for_each_entry(tmp, &fs_info->space_info, list) { 3608bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3609bf5fc093SJosef Bacik info = tmp; 36107fde62bfSChris Mason break; 3611bf5fc093SJosef Bacik } 3612bf5fc093SJosef Bacik } 36137fde62bfSChris Mason 3614bf5fc093SJosef Bacik if (!info) 3615bf5fc093SJosef Bacik continue; 3616bf5fc093SJosef Bacik down_read(&info->groups_sem); 3617bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3618bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 3619c065f5b1SSu Yue get_block_group_info(&info->block_groups[c], 3620c065f5b1SSu Yue &space); 36217fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 36221406e432SJosef Bacik dest++; 36231406e432SJosef Bacik space_args.total_spaces++; 362451788b1bSDan Rosenberg slot_count--; 36251406e432SJosef Bacik } 362651788b1bSDan Rosenberg if (!slot_count) 362751788b1bSDan Rosenberg break; 3628bf5fc093SJosef Bacik } 3629bf5fc093SJosef Bacik up_read(&info->groups_sem); 3630bf5fc093SJosef Bacik } 36311406e432SJosef Bacik 363236523e95SDavid Sterba /* 363336523e95SDavid Sterba * Add global block reserve 363436523e95SDavid Sterba */ 363536523e95SDavid Sterba if (slot_count) { 36360b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 363736523e95SDavid Sterba 363836523e95SDavid Sterba spin_lock(&block_rsv->lock); 363936523e95SDavid Sterba space.total_bytes = block_rsv->size; 364036523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 364136523e95SDavid Sterba spin_unlock(&block_rsv->lock); 364236523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 364336523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 364436523e95SDavid Sterba space_args.total_spaces++; 364536523e95SDavid Sterba } 364636523e95SDavid Sterba 36472eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 36487fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 36497fde62bfSChris Mason 36507fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 36517fde62bfSChris Mason ret = -EFAULT; 36527fde62bfSChris Mason 36537fde62bfSChris Mason kfree(dest_orig); 36547fde62bfSChris Mason out: 36557fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 36561406e432SJosef Bacik ret = -EFAULT; 36571406e432SJosef Bacik 36581406e432SJosef Bacik return ret; 36591406e432SJosef Bacik } 36601406e432SJosef Bacik 36619a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 36629a8c28beSMiao Xie void __user *argp) 366346204592SSage Weil { 366446204592SSage Weil struct btrfs_trans_handle *trans; 366546204592SSage Weil u64 transid; 366646204592SSage Weil 3667d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3668ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3669ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 367098d5dc13STsutomu Itoh return PTR_ERR(trans); 3671ff7c1d33SMiao Xie 3672ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3673ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3674ff7c1d33SMiao Xie goto out; 3675ff7c1d33SMiao Xie } 367646204592SSage Weil transid = trans->transid; 3677fdfbf020SJosef Bacik btrfs_commit_transaction_async(trans); 3678ff7c1d33SMiao Xie out: 367946204592SSage Weil if (argp) 368046204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 368146204592SSage Weil return -EFAULT; 368246204592SSage Weil return 0; 368346204592SSage Weil } 368446204592SSage Weil 36852ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 36869a8c28beSMiao Xie void __user *argp) 368746204592SSage Weil { 368846204592SSage Weil u64 transid; 368946204592SSage Weil 369046204592SSage Weil if (argp) { 369146204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 369246204592SSage Weil return -EFAULT; 369346204592SSage Weil } else { 369446204592SSage Weil transid = 0; /* current trans */ 369546204592SSage Weil } 36962ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 369746204592SSage Weil } 369846204592SSage Weil 3699b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 3700475f6387SJan Schmidt { 37010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 3702475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3703b8e95489SMiao Xie int ret; 3704475f6387SJan Schmidt 3705475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3706475f6387SJan Schmidt return -EPERM; 3707475f6387SJan Schmidt 3708475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3709475f6387SJan Schmidt if (IS_ERR(sa)) 3710475f6387SJan Schmidt return PTR_ERR(sa); 3711475f6387SJan Schmidt 3712b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 3713b8e95489SMiao Xie ret = mnt_want_write_file(file); 3714b8e95489SMiao Xie if (ret) 3715b8e95489SMiao Xie goto out; 3716b8e95489SMiao Xie } 3717b8e95489SMiao Xie 37180b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 371963a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 372063a212abSStefan Behrens 0); 3721475f6387SJan Schmidt 37225afe6ce7SFilipe Manana /* 37235afe6ce7SFilipe Manana * Copy scrub args to user space even if btrfs_scrub_dev() returned an 37245afe6ce7SFilipe Manana * error. This is important as it allows user space to know how much 37255afe6ce7SFilipe Manana * progress scrub has done. For example, if scrub is canceled we get 37265afe6ce7SFilipe Manana * -ECANCELED from btrfs_scrub_dev() and return that error back to user 37275afe6ce7SFilipe Manana * space. Later user space can inspect the progress from the structure 37285afe6ce7SFilipe Manana * btrfs_ioctl_scrub_args and resume scrub from where it left off 37295afe6ce7SFilipe Manana * previously (btrfs-progs does this). 37305afe6ce7SFilipe Manana * If we fail to copy the btrfs_ioctl_scrub_args structure to user space 37315afe6ce7SFilipe Manana * then return -EFAULT to signal the structure was not copied or it may 37325afe6ce7SFilipe Manana * be corrupt and unreliable due to a partial copy. 37335afe6ce7SFilipe Manana */ 37345afe6ce7SFilipe Manana if (copy_to_user(arg, sa, sizeof(*sa))) 3735475f6387SJan Schmidt ret = -EFAULT; 3736475f6387SJan Schmidt 3737b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 3738b8e95489SMiao Xie mnt_drop_write_file(file); 3739b8e95489SMiao Xie out: 3740475f6387SJan Schmidt kfree(sa); 3741475f6387SJan Schmidt return ret; 3742475f6387SJan Schmidt } 3743475f6387SJan Schmidt 37442ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 3745475f6387SJan Schmidt { 3746475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3747475f6387SJan Schmidt return -EPERM; 3748475f6387SJan Schmidt 37492ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 3750475f6387SJan Schmidt } 3751475f6387SJan Schmidt 37522ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 3753475f6387SJan Schmidt void __user *arg) 3754475f6387SJan Schmidt { 3755475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3756475f6387SJan Schmidt int ret; 3757475f6387SJan Schmidt 3758475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3759475f6387SJan Schmidt return -EPERM; 3760475f6387SJan Schmidt 3761475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3762475f6387SJan Schmidt if (IS_ERR(sa)) 3763475f6387SJan Schmidt return PTR_ERR(sa); 3764475f6387SJan Schmidt 37652ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 3766475f6387SJan Schmidt 37674fa99b00SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 3768475f6387SJan Schmidt ret = -EFAULT; 3769475f6387SJan Schmidt 3770475f6387SJan Schmidt kfree(sa); 3771475f6387SJan Schmidt return ret; 3772475f6387SJan Schmidt } 3773475f6387SJan Schmidt 37742ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 3775b27f7c0cSDavid Sterba void __user *arg) 3776c11d2c23SStefan Behrens { 3777c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 3778c11d2c23SStefan Behrens int ret; 3779c11d2c23SStefan Behrens 3780c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 3781c11d2c23SStefan Behrens if (IS_ERR(sa)) 3782c11d2c23SStefan Behrens return PTR_ERR(sa); 3783c11d2c23SStefan Behrens 3784b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 3785b27f7c0cSDavid Sterba kfree(sa); 3786b27f7c0cSDavid Sterba return -EPERM; 3787b27f7c0cSDavid Sterba } 3788b27f7c0cSDavid Sterba 37892ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 3790c11d2c23SStefan Behrens 3791eee99577SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 3792c11d2c23SStefan Behrens ret = -EFAULT; 3793c11d2c23SStefan Behrens 3794c11d2c23SStefan Behrens kfree(sa); 3795c11d2c23SStefan Behrens return ret; 3796c11d2c23SStefan Behrens } 3797c11d2c23SStefan Behrens 37982ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 37992ff7e61eSJeff Mahoney void __user *arg) 38003f6bcfbdSStefan Behrens { 38013f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 38023f6bcfbdSStefan Behrens int ret; 38033f6bcfbdSStefan Behrens 38043f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 38053f6bcfbdSStefan Behrens return -EPERM; 38063f6bcfbdSStefan Behrens 38073f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 38083f6bcfbdSStefan Behrens if (IS_ERR(p)) 38093f6bcfbdSStefan Behrens return PTR_ERR(p); 38103f6bcfbdSStefan Behrens 38113f6bcfbdSStefan Behrens switch (p->cmd) { 38123f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 3813bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 3814adfa97cbSIlya Dryomov ret = -EROFS; 3815adfa97cbSIlya Dryomov goto out; 3816adfa97cbSIlya Dryomov } 3817c3e1f96cSGoldwyn Rodrigues if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) { 3818e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 38193f6bcfbdSStefan Behrens } else { 38202ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 3821c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 38223f6bcfbdSStefan Behrens } 38233f6bcfbdSStefan Behrens break; 38243f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 38250b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 38263f6bcfbdSStefan Behrens ret = 0; 38273f6bcfbdSStefan Behrens break; 38283f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 382917d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 383097282031SAnand Jain ret = 0; 38313f6bcfbdSStefan Behrens break; 38323f6bcfbdSStefan Behrens default: 38333f6bcfbdSStefan Behrens ret = -EINVAL; 38343f6bcfbdSStefan Behrens break; 38353f6bcfbdSStefan Behrens } 38363f6bcfbdSStefan Behrens 3837d3a53286SFilipe Manana if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) 38383f6bcfbdSStefan Behrens ret = -EFAULT; 3839adfa97cbSIlya Dryomov out: 38403f6bcfbdSStefan Behrens kfree(p); 38413f6bcfbdSStefan Behrens return ret; 38423f6bcfbdSStefan Behrens } 38433f6bcfbdSStefan Behrens 3844d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 3845d7728c96SJan Schmidt { 3846d7728c96SJan Schmidt int ret = 0; 3847d7728c96SJan Schmidt int i; 3848740c3d22SChris Mason u64 rel_ptr; 3849d7728c96SJan Schmidt int size; 3850806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 3851d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 3852d7728c96SJan Schmidt struct btrfs_path *path; 3853d7728c96SJan Schmidt 385482b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 3855d7728c96SJan Schmidt return -EPERM; 3856d7728c96SJan Schmidt 3857d7728c96SJan Schmidt path = btrfs_alloc_path(); 3858d7728c96SJan Schmidt if (!path) { 3859d7728c96SJan Schmidt ret = -ENOMEM; 3860d7728c96SJan Schmidt goto out; 3861d7728c96SJan Schmidt } 3862d7728c96SJan Schmidt 3863d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 3864d7728c96SJan Schmidt if (IS_ERR(ipa)) { 3865d7728c96SJan Schmidt ret = PTR_ERR(ipa); 3866d7728c96SJan Schmidt ipa = NULL; 3867d7728c96SJan Schmidt goto out; 3868d7728c96SJan Schmidt } 3869d7728c96SJan Schmidt 3870d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 3871d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 3872d7728c96SJan Schmidt if (IS_ERR(ipath)) { 3873d7728c96SJan Schmidt ret = PTR_ERR(ipath); 3874d7728c96SJan Schmidt ipath = NULL; 3875d7728c96SJan Schmidt goto out; 3876d7728c96SJan Schmidt } 3877d7728c96SJan Schmidt 3878d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 3879d7728c96SJan Schmidt if (ret < 0) 3880d7728c96SJan Schmidt goto out; 3881d7728c96SJan Schmidt 3882d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 3883745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 3884745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 3885740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 3886d7728c96SJan Schmidt } 3887d7728c96SJan Schmidt 3888718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 3889718dc5faSOmar Sandoval ipath->fspath, size); 3890d7728c96SJan Schmidt if (ret) { 3891d7728c96SJan Schmidt ret = -EFAULT; 3892d7728c96SJan Schmidt goto out; 3893d7728c96SJan Schmidt } 3894d7728c96SJan Schmidt 3895d7728c96SJan Schmidt out: 3896d7728c96SJan Schmidt btrfs_free_path(path); 3897d7728c96SJan Schmidt free_ipath(ipath); 3898d7728c96SJan Schmidt kfree(ipa); 3899d7728c96SJan Schmidt 3900d7728c96SJan Schmidt return ret; 3901d7728c96SJan Schmidt } 3902d7728c96SJan Schmidt 3903d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 3904d7728c96SJan Schmidt { 3905d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 3906d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 3907d7728c96SJan Schmidt 3908d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 3909d7728c96SJan Schmidt inodes->bytes_left -= c; 3910d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 3911d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 3912d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 3913d7728c96SJan Schmidt inodes->elem_cnt += 3; 3914d7728c96SJan Schmidt } else { 3915d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 3916d7728c96SJan Schmidt inodes->bytes_left = 0; 3917d7728c96SJan Schmidt inodes->elem_missed += 3; 3918d7728c96SJan Schmidt } 3919d7728c96SJan Schmidt 3920d7728c96SJan Schmidt return 0; 3921d7728c96SJan Schmidt } 3922d7728c96SJan Schmidt 39232ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 3924d24a67b2SZygo Blaxell void __user *arg, int version) 3925d7728c96SJan Schmidt { 3926d7728c96SJan Schmidt int ret = 0; 3927d7728c96SJan Schmidt int size; 3928d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3929d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3930d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3931d24a67b2SZygo Blaxell bool ignore_offset; 3932d7728c96SJan Schmidt 3933d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3934d7728c96SJan Schmidt return -EPERM; 3935d7728c96SJan Schmidt 3936d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 39377b9ea627SShailendra Verma if (IS_ERR(loi)) 39387b9ea627SShailendra Verma return PTR_ERR(loi); 3939d7728c96SJan Schmidt 3940d24a67b2SZygo Blaxell if (version == 1) { 3941d24a67b2SZygo Blaxell ignore_offset = false; 3942b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 3943d24a67b2SZygo Blaxell } else { 3944d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 3945d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 3946d24a67b2SZygo Blaxell ret = -EINVAL; 3947d24a67b2SZygo Blaxell goto out_loi; 3948d24a67b2SZygo Blaxell } 3949d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 3950d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 3951d24a67b2SZygo Blaxell ret = -EINVAL; 3952d24a67b2SZygo Blaxell goto out_loi; 3953d24a67b2SZygo Blaxell } 3954d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 3955b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 3956d24a67b2SZygo Blaxell } 3957d24a67b2SZygo Blaxell 3958d7728c96SJan Schmidt path = btrfs_alloc_path(); 3959d7728c96SJan Schmidt if (!path) { 3960d7728c96SJan Schmidt ret = -ENOMEM; 3961d7728c96SJan Schmidt goto out; 3962d7728c96SJan Schmidt } 3963d7728c96SJan Schmidt 3964d7728c96SJan Schmidt inodes = init_data_container(size); 3965d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3966d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3967d7728c96SJan Schmidt inodes = NULL; 3968d7728c96SJan Schmidt goto out; 3969d7728c96SJan Schmidt } 3970d7728c96SJan Schmidt 39712ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 3972d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 3973df031f07SLiu Bo if (ret == -EINVAL) 3974d7728c96SJan Schmidt ret = -ENOENT; 3975d7728c96SJan Schmidt if (ret < 0) 3976d7728c96SJan Schmidt goto out; 3977d7728c96SJan Schmidt 3978718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 3979718dc5faSOmar Sandoval size); 3980d7728c96SJan Schmidt if (ret) 3981d7728c96SJan Schmidt ret = -EFAULT; 3982d7728c96SJan Schmidt 3983d7728c96SJan Schmidt out: 3984d7728c96SJan Schmidt btrfs_free_path(path); 3985f54de068SDavid Sterba kvfree(inodes); 3986d24a67b2SZygo Blaxell out_loi: 3987d7728c96SJan Schmidt kfree(loi); 3988d7728c96SJan Schmidt 3989d7728c96SJan Schmidt return ret; 3990d7728c96SJan Schmidt } 3991d7728c96SJan Schmidt 3992008ef096SDavid Sterba void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 3993c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3994c9e9f97bSIlya Dryomov { 3995c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3996c9e9f97bSIlya Dryomov 3997c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3998c9e9f97bSIlya Dryomov 39993009a62fSDavid Sterba if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) 4000837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4001837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4002837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4003a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4004a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4005837d5b6eSIlya Dryomov 4006c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4007c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4008c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 400919a39dceSIlya Dryomov 401019a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 401119a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 401219a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 4013c9e9f97bSIlya Dryomov } 4014c9e9f97bSIlya Dryomov 40159ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4016c9e9f97bSIlya Dryomov { 4017496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4018c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4019c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4020c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4021ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4022c9e9f97bSIlya Dryomov int ret; 4023c9e9f97bSIlya Dryomov 40246c405b24SNikolay Borisov if (!arg) 40256c405b24SNikolay Borisov btrfs_warn(fs_info, 40266c405b24SNikolay Borisov "IOC_BALANCE ioctl (v1) is deprecated and will be removed in kernel 5.18"); 40276c405b24SNikolay Borisov 4028c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4029c9e9f97bSIlya Dryomov return -EPERM; 4030c9e9f97bSIlya Dryomov 4031e54bfa31SLiu Bo ret = mnt_want_write_file(file); 40329ba1f6e4SLiu Bo if (ret) 40339ba1f6e4SLiu Bo return ret; 40349ba1f6e4SLiu Bo 4035ed0fb78fSIlya Dryomov again: 4036c3e1f96cSGoldwyn Rodrigues if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) { 4037c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4038ed0fb78fSIlya Dryomov need_unlock = true; 4039ed0fb78fSIlya Dryomov goto locked; 4040ed0fb78fSIlya Dryomov } 4041ed0fb78fSIlya Dryomov 4042ed0fb78fSIlya Dryomov /* 404301327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4044ed0fb78fSIlya Dryomov * (1) some other op is running 4045ed0fb78fSIlya Dryomov * (2) balance is running 4046ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4047ed0fb78fSIlya Dryomov */ 4048ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4049ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4050ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 40513009a62fSDavid Sterba if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4052ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4053dccdb07bSDavid Sterba /* 4054dccdb07bSDavid Sterba * Lock released to allow other waiters to continue, 4055dccdb07bSDavid Sterba * we'll reexamine the status again. 4056dccdb07bSDavid Sterba */ 4057ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4058ed0fb78fSIlya Dryomov 4059ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 40603009a62fSDavid Sterba !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4061ed0fb78fSIlya Dryomov /* this is (3) */ 4062ed0fb78fSIlya Dryomov need_unlock = false; 4063ed0fb78fSIlya Dryomov goto locked; 4064ed0fb78fSIlya Dryomov } 4065ed0fb78fSIlya Dryomov 4066ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4067ed0fb78fSIlya Dryomov goto again; 4068ed0fb78fSIlya Dryomov } else { 4069ed0fb78fSIlya Dryomov /* this is (2) */ 4070ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4071ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4072ed0fb78fSIlya Dryomov goto out; 4073ed0fb78fSIlya Dryomov } 4074ed0fb78fSIlya Dryomov } else { 4075ed0fb78fSIlya Dryomov /* this is (1) */ 4076ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4077e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4078ed0fb78fSIlya Dryomov goto out; 4079ed0fb78fSIlya Dryomov } 4080ed0fb78fSIlya Dryomov 4081ed0fb78fSIlya Dryomov locked: 4082c9e9f97bSIlya Dryomov 4083c9e9f97bSIlya Dryomov if (arg) { 4084c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4085c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4086c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4087ed0fb78fSIlya Dryomov goto out_unlock; 4088c9e9f97bSIlya Dryomov } 4089de322263SIlya Dryomov 4090de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4091de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4092de322263SIlya Dryomov ret = -ENOTCONN; 4093de322263SIlya Dryomov goto out_bargs; 4094de322263SIlya Dryomov } 4095de322263SIlya Dryomov 4096de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4097de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4098de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4099de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4100efc0e69cSNikolay Borisov btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE); 4101de322263SIlya Dryomov 4102de322263SIlya Dryomov goto do_balance; 4103de322263SIlya Dryomov } 4104c9e9f97bSIlya Dryomov } else { 4105c9e9f97bSIlya Dryomov bargs = NULL; 4106c9e9f97bSIlya Dryomov } 4107c9e9f97bSIlya Dryomov 4108ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4109837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4110837d5b6eSIlya Dryomov goto out_bargs; 4111837d5b6eSIlya Dryomov } 4112837d5b6eSIlya Dryomov 41138d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4114c9e9f97bSIlya Dryomov if (!bctl) { 4115c9e9f97bSIlya Dryomov ret = -ENOMEM; 4116c9e9f97bSIlya Dryomov goto out_bargs; 4117c9e9f97bSIlya Dryomov } 4118c9e9f97bSIlya Dryomov 4119c9e9f97bSIlya Dryomov if (arg) { 4120c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4121c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4122c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4123c9e9f97bSIlya Dryomov 4124c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4125f43ffb60SIlya Dryomov } else { 4126f43ffb60SIlya Dryomov /* balance everything - no filters */ 4127f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4128c9e9f97bSIlya Dryomov } 4129c9e9f97bSIlya Dryomov 41308eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 41318eb93459SDavid Sterba ret = -EINVAL; 41320f89abf5SChristian Engelmayer goto out_bctl; 41338eb93459SDavid Sterba } 41348eb93459SDavid Sterba 4135de322263SIlya Dryomov do_balance: 4136c9e9f97bSIlya Dryomov /* 4137c3e1f96cSGoldwyn Rodrigues * Ownership of bctl and exclusive operation goes to btrfs_balance. 4138c3e1f96cSGoldwyn Rodrigues * bctl is freed in reset_balance_state, or, if restriper was paused 4139c3e1f96cSGoldwyn Rodrigues * all the way until unmount, in free_fs_info. The flag should be 4140c3e1f96cSGoldwyn Rodrigues * cleared after reset_balance_state. 4141c9e9f97bSIlya Dryomov */ 4142ed0fb78fSIlya Dryomov need_unlock = false; 4143ed0fb78fSIlya Dryomov 41446fcf6e2bSDavid Sterba ret = btrfs_balance(fs_info, bctl, bargs); 41450f89abf5SChristian Engelmayer bctl = NULL; 4146ed0fb78fSIlya Dryomov 4147d00c2d9cSFilipe Manana if ((ret == 0 || ret == -ECANCELED) && arg) { 4148c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4149c9e9f97bSIlya Dryomov ret = -EFAULT; 4150c9e9f97bSIlya Dryomov } 4151c9e9f97bSIlya Dryomov 41520f89abf5SChristian Engelmayer out_bctl: 41530f89abf5SChristian Engelmayer kfree(bctl); 4154c9e9f97bSIlya Dryomov out_bargs: 4155c9e9f97bSIlya Dryomov kfree(bargs); 4156ed0fb78fSIlya Dryomov out_unlock: 4157c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4158ed0fb78fSIlya Dryomov if (need_unlock) 4159c3e1f96cSGoldwyn Rodrigues btrfs_exclop_finish(fs_info); 4160ed0fb78fSIlya Dryomov out: 4161e54bfa31SLiu Bo mnt_drop_write_file(file); 4162c9e9f97bSIlya Dryomov return ret; 4163c9e9f97bSIlya Dryomov } 4164c9e9f97bSIlya Dryomov 41652ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4166837d5b6eSIlya Dryomov { 4167837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4168837d5b6eSIlya Dryomov return -EPERM; 4169837d5b6eSIlya Dryomov 4170837d5b6eSIlya Dryomov switch (cmd) { 4171837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 41720b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4173a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 41740b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4175837d5b6eSIlya Dryomov } 4176837d5b6eSIlya Dryomov 4177837d5b6eSIlya Dryomov return -EINVAL; 4178837d5b6eSIlya Dryomov } 4179837d5b6eSIlya Dryomov 41802ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 418119a39dceSIlya Dryomov void __user *arg) 418219a39dceSIlya Dryomov { 418319a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 418419a39dceSIlya Dryomov int ret = 0; 418519a39dceSIlya Dryomov 418619a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 418719a39dceSIlya Dryomov return -EPERM; 418819a39dceSIlya Dryomov 418919a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 419019a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 419119a39dceSIlya Dryomov ret = -ENOTCONN; 419219a39dceSIlya Dryomov goto out; 419319a39dceSIlya Dryomov } 419419a39dceSIlya Dryomov 41958d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 419619a39dceSIlya Dryomov if (!bargs) { 419719a39dceSIlya Dryomov ret = -ENOMEM; 419819a39dceSIlya Dryomov goto out; 419919a39dceSIlya Dryomov } 420019a39dceSIlya Dryomov 4201008ef096SDavid Sterba btrfs_update_ioctl_balance_args(fs_info, bargs); 420219a39dceSIlya Dryomov 420319a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 420419a39dceSIlya Dryomov ret = -EFAULT; 420519a39dceSIlya Dryomov 420619a39dceSIlya Dryomov kfree(bargs); 420719a39dceSIlya Dryomov out: 420819a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 420919a39dceSIlya Dryomov return ret; 421019a39dceSIlya Dryomov } 421119a39dceSIlya Dryomov 4212905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 42135d13a37bSArne Jansen { 42140b246afaSJeff Mahoney struct inode *inode = file_inode(file); 42150b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 42165d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 42175d13a37bSArne Jansen int ret; 42185d13a37bSArne Jansen 42195d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 42205d13a37bSArne Jansen return -EPERM; 42215d13a37bSArne Jansen 4222905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4223905b0ddaSMiao Xie if (ret) 4224905b0ddaSMiao Xie return ret; 42255d13a37bSArne Jansen 42265d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4227905b0ddaSMiao Xie if (IS_ERR(sa)) { 4228905b0ddaSMiao Xie ret = PTR_ERR(sa); 4229905b0ddaSMiao Xie goto drop_write; 4230905b0ddaSMiao Xie } 42315d13a37bSArne Jansen 42320b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 42335d13a37bSArne Jansen 42345d13a37bSArne Jansen switch (sa->cmd) { 42355d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 4236340f1aa2SNikolay Borisov ret = btrfs_quota_enable(fs_info); 42375d13a37bSArne Jansen break; 42385d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 4239340f1aa2SNikolay Borisov ret = btrfs_quota_disable(fs_info); 42405d13a37bSArne Jansen break; 42415d13a37bSArne Jansen default: 42425d13a37bSArne Jansen ret = -EINVAL; 42435d13a37bSArne Jansen break; 42445d13a37bSArne Jansen } 42455d13a37bSArne Jansen 42465d13a37bSArne Jansen kfree(sa); 42470b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4248905b0ddaSMiao Xie drop_write: 4249905b0ddaSMiao Xie mnt_drop_write_file(file); 42505d13a37bSArne Jansen return ret; 42515d13a37bSArne Jansen } 42525d13a37bSArne Jansen 4253905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 42545d13a37bSArne Jansen { 42550b246afaSJeff Mahoney struct inode *inode = file_inode(file); 42560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 42570b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 42585d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 42595d13a37bSArne Jansen struct btrfs_trans_handle *trans; 42605d13a37bSArne Jansen int ret; 42615d13a37bSArne Jansen int err; 42625d13a37bSArne Jansen 42635d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 42645d13a37bSArne Jansen return -EPERM; 42655d13a37bSArne Jansen 4266905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4267905b0ddaSMiao Xie if (ret) 4268905b0ddaSMiao Xie return ret; 42695d13a37bSArne Jansen 42705d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4271905b0ddaSMiao Xie if (IS_ERR(sa)) { 4272905b0ddaSMiao Xie ret = PTR_ERR(sa); 4273905b0ddaSMiao Xie goto drop_write; 4274905b0ddaSMiao Xie } 42755d13a37bSArne Jansen 42765d13a37bSArne Jansen trans = btrfs_join_transaction(root); 42775d13a37bSArne Jansen if (IS_ERR(trans)) { 42785d13a37bSArne Jansen ret = PTR_ERR(trans); 42795d13a37bSArne Jansen goto out; 42805d13a37bSArne Jansen } 42815d13a37bSArne Jansen 42825d13a37bSArne Jansen if (sa->assign) { 42839f8a6ce6SLu Fengqi ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); 42845d13a37bSArne Jansen } else { 428539616c27SLu Fengqi ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); 42865d13a37bSArne Jansen } 42875d13a37bSArne Jansen 4288e082f563SQu Wenruo /* update qgroup status and info */ 4289280f8bd2SLu Fengqi err = btrfs_run_qgroups(trans); 4290e082f563SQu Wenruo if (err < 0) 42910b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4292ad8403dfSAnand Jain "failed to update qgroup status and info"); 42933a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 42945d13a37bSArne Jansen if (err && !ret) 42955d13a37bSArne Jansen ret = err; 42965d13a37bSArne Jansen 42975d13a37bSArne Jansen out: 42985d13a37bSArne Jansen kfree(sa); 4299905b0ddaSMiao Xie drop_write: 4300905b0ddaSMiao Xie mnt_drop_write_file(file); 43015d13a37bSArne Jansen return ret; 43025d13a37bSArne Jansen } 43035d13a37bSArne Jansen 4304905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 43055d13a37bSArne Jansen { 43060b246afaSJeff Mahoney struct inode *inode = file_inode(file); 43070b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 43085d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 43095d13a37bSArne Jansen struct btrfs_trans_handle *trans; 43105d13a37bSArne Jansen int ret; 43115d13a37bSArne Jansen int err; 43125d13a37bSArne Jansen 43135d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 43145d13a37bSArne Jansen return -EPERM; 43155d13a37bSArne Jansen 4316905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4317905b0ddaSMiao Xie if (ret) 4318905b0ddaSMiao Xie return ret; 43195d13a37bSArne Jansen 43205d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4321905b0ddaSMiao Xie if (IS_ERR(sa)) { 4322905b0ddaSMiao Xie ret = PTR_ERR(sa); 4323905b0ddaSMiao Xie goto drop_write; 4324905b0ddaSMiao Xie } 43255d13a37bSArne Jansen 4326d86e56cfSMiao Xie if (!sa->qgroupid) { 4327d86e56cfSMiao Xie ret = -EINVAL; 4328d86e56cfSMiao Xie goto out; 4329d86e56cfSMiao Xie } 4330d86e56cfSMiao Xie 43315d13a37bSArne Jansen trans = btrfs_join_transaction(root); 43325d13a37bSArne Jansen if (IS_ERR(trans)) { 43335d13a37bSArne Jansen ret = PTR_ERR(trans); 43345d13a37bSArne Jansen goto out; 43355d13a37bSArne Jansen } 43365d13a37bSArne Jansen 43375d13a37bSArne Jansen if (sa->create) { 433849a05ecdSLu Fengqi ret = btrfs_create_qgroup(trans, sa->qgroupid); 43395d13a37bSArne Jansen } else { 43403efbee1dSLu Fengqi ret = btrfs_remove_qgroup(trans, sa->qgroupid); 43415d13a37bSArne Jansen } 43425d13a37bSArne Jansen 43433a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 43445d13a37bSArne Jansen if (err && !ret) 43455d13a37bSArne Jansen ret = err; 43465d13a37bSArne Jansen 43475d13a37bSArne Jansen out: 43485d13a37bSArne Jansen kfree(sa); 4349905b0ddaSMiao Xie drop_write: 4350905b0ddaSMiao Xie mnt_drop_write_file(file); 43515d13a37bSArne Jansen return ret; 43525d13a37bSArne Jansen } 43535d13a37bSArne Jansen 4354905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 43555d13a37bSArne Jansen { 43560b246afaSJeff Mahoney struct inode *inode = file_inode(file); 43570b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 43585d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 43595d13a37bSArne Jansen struct btrfs_trans_handle *trans; 43605d13a37bSArne Jansen int ret; 43615d13a37bSArne Jansen int err; 43625d13a37bSArne Jansen u64 qgroupid; 43635d13a37bSArne Jansen 43645d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 43655d13a37bSArne Jansen return -EPERM; 43665d13a37bSArne Jansen 4367905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4368905b0ddaSMiao Xie if (ret) 4369905b0ddaSMiao Xie return ret; 43705d13a37bSArne Jansen 43715d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4372905b0ddaSMiao Xie if (IS_ERR(sa)) { 4373905b0ddaSMiao Xie ret = PTR_ERR(sa); 4374905b0ddaSMiao Xie goto drop_write; 4375905b0ddaSMiao Xie } 43765d13a37bSArne Jansen 43775d13a37bSArne Jansen trans = btrfs_join_transaction(root); 43785d13a37bSArne Jansen if (IS_ERR(trans)) { 43795d13a37bSArne Jansen ret = PTR_ERR(trans); 43805d13a37bSArne Jansen goto out; 43815d13a37bSArne Jansen } 43825d13a37bSArne Jansen 43835d13a37bSArne Jansen qgroupid = sa->qgroupid; 43845d13a37bSArne Jansen if (!qgroupid) { 43855d13a37bSArne Jansen /* take the current subvol as qgroup */ 43865d13a37bSArne Jansen qgroupid = root->root_key.objectid; 43875d13a37bSArne Jansen } 43885d13a37bSArne Jansen 4389f0042d5eSLu Fengqi ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); 43905d13a37bSArne Jansen 43913a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 43925d13a37bSArne Jansen if (err && !ret) 43935d13a37bSArne Jansen ret = err; 43945d13a37bSArne Jansen 43955d13a37bSArne Jansen out: 43965d13a37bSArne Jansen kfree(sa); 4397905b0ddaSMiao Xie drop_write: 4398905b0ddaSMiao Xie mnt_drop_write_file(file); 43995d13a37bSArne Jansen return ret; 44005d13a37bSArne Jansen } 44015d13a37bSArne Jansen 44022f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 44032f232036SJan Schmidt { 44040b246afaSJeff Mahoney struct inode *inode = file_inode(file); 44050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 44062f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 44072f232036SJan Schmidt int ret; 44082f232036SJan Schmidt 44092f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 44102f232036SJan Schmidt return -EPERM; 44112f232036SJan Schmidt 44122f232036SJan Schmidt ret = mnt_want_write_file(file); 44132f232036SJan Schmidt if (ret) 44142f232036SJan Schmidt return ret; 44152f232036SJan Schmidt 44162f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 44172f232036SJan Schmidt if (IS_ERR(qsa)) { 44182f232036SJan Schmidt ret = PTR_ERR(qsa); 44192f232036SJan Schmidt goto drop_write; 44202f232036SJan Schmidt } 44212f232036SJan Schmidt 44222f232036SJan Schmidt if (qsa->flags) { 44232f232036SJan Schmidt ret = -EINVAL; 44242f232036SJan Schmidt goto out; 44252f232036SJan Schmidt } 44262f232036SJan Schmidt 44270b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 44282f232036SJan Schmidt 44292f232036SJan Schmidt out: 44302f232036SJan Schmidt kfree(qsa); 44312f232036SJan Schmidt drop_write: 44322f232036SJan Schmidt mnt_drop_write_file(file); 44332f232036SJan Schmidt return ret; 44342f232036SJan Schmidt } 44352f232036SJan Schmidt 4436b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, 4437b929c1d8SMarcos Paulo de Souza void __user *arg) 44382f232036SJan Schmidt { 44390afb603aSGoldwyn Rodrigues struct btrfs_ioctl_quota_rescan_args qsa = {0}; 44402f232036SJan Schmidt 44412f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 44422f232036SJan Schmidt return -EPERM; 44432f232036SJan Schmidt 44440b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 44450afb603aSGoldwyn Rodrigues qsa.flags = 1; 44460afb603aSGoldwyn Rodrigues qsa.progress = fs_info->qgroup_rescan_progress.objectid; 44472f232036SJan Schmidt } 44482f232036SJan Schmidt 44490afb603aSGoldwyn Rodrigues if (copy_to_user(arg, &qsa, sizeof(qsa))) 4450991a3daeSAnand Jain return -EFAULT; 44512f232036SJan Schmidt 4452991a3daeSAnand Jain return 0; 44532f232036SJan Schmidt } 44542f232036SJan Schmidt 4455b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, 4456b929c1d8SMarcos Paulo de Souza void __user *arg) 445757254b6eSJan Schmidt { 445857254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 445957254b6eSJan Schmidt return -EPERM; 446057254b6eSJan Schmidt 44610b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 446257254b6eSJan Schmidt } 446357254b6eSJan Schmidt 4464abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4465e4fed17aSChristian Brauner struct user_namespace *mnt_userns, 4466abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 44678ea05e3aSAlexander Block { 4468496ad9aaSAl Viro struct inode *inode = file_inode(file); 44690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 44708ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 44718ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 44728ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 447395582b00SDeepa Dinamani struct timespec64 ct = current_time(inode); 44748ea05e3aSAlexander Block int ret = 0; 4475dd5f9615SStefan Behrens int received_uuid_changed; 44768ea05e3aSAlexander Block 4477e4fed17aSChristian Brauner if (!inode_owner_or_capable(mnt_userns, inode)) 4478bd60ea0fSDavid Sterba return -EPERM; 4479bd60ea0fSDavid Sterba 44808ea05e3aSAlexander Block ret = mnt_want_write_file(file); 44818ea05e3aSAlexander Block if (ret < 0) 44828ea05e3aSAlexander Block return ret; 44838ea05e3aSAlexander Block 44840b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 44858ea05e3aSAlexander Block 44864a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 44878ea05e3aSAlexander Block ret = -EINVAL; 44888ea05e3aSAlexander Block goto out; 44898ea05e3aSAlexander Block } 44908ea05e3aSAlexander Block 44918ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 44928ea05e3aSAlexander Block ret = -EROFS; 44938ea05e3aSAlexander Block goto out; 44948ea05e3aSAlexander Block } 44958ea05e3aSAlexander Block 4496dd5f9615SStefan Behrens /* 4497dd5f9615SStefan Behrens * 1 - root item 4498dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4499dd5f9615SStefan Behrens */ 4500dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 45018ea05e3aSAlexander Block if (IS_ERR(trans)) { 45028ea05e3aSAlexander Block ret = PTR_ERR(trans); 45038ea05e3aSAlexander Block trans = NULL; 45048ea05e3aSAlexander Block goto out; 45058ea05e3aSAlexander Block } 45068ea05e3aSAlexander Block 45078ea05e3aSAlexander Block sa->rtransid = trans->transid; 45088ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 45098ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 45108ea05e3aSAlexander Block 4511dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4512dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4513dd5f9615SStefan Behrens if (received_uuid_changed && 4514d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 4515d1957791SLu Fengqi ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, 4516dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4517dd5f9615SStefan Behrens root->root_key.objectid); 4518d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 4519d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 4520d87ff758SNikolay Borisov btrfs_end_transaction(trans); 4521d87ff758SNikolay Borisov goto out; 4522d87ff758SNikolay Borisov } 4523d87ff758SNikolay Borisov } 45248ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 45258ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 45268ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 45273cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 45283cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 45293cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 45303cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 45318ea05e3aSAlexander Block 45320b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 45338ea05e3aSAlexander Block &root->root_key, &root->root_item); 45348ea05e3aSAlexander Block if (ret < 0) { 45353a45bb20SJeff Mahoney btrfs_end_transaction(trans); 45368ea05e3aSAlexander Block goto out; 4537dd5f9615SStefan Behrens } 4538dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4539cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, sa->uuid, 4540dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4541dd5f9615SStefan Behrens root->root_key.objectid); 4542dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 454366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 4544efd38150SNikolay Borisov btrfs_end_transaction(trans); 4545dd5f9615SStefan Behrens goto out; 4546dd5f9615SStefan Behrens } 4547dd5f9615SStefan Behrens } 45483a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 4549abccd00fSHugo Mills out: 45500b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4551abccd00fSHugo Mills mnt_drop_write_file(file); 4552abccd00fSHugo Mills return ret; 4553abccd00fSHugo Mills } 4554abccd00fSHugo Mills 4555abccd00fSHugo Mills #ifdef CONFIG_64BIT 4556abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 4557abccd00fSHugo Mills void __user *arg) 4558abccd00fSHugo Mills { 4559abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 4560abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 4561abccd00fSHugo Mills int ret = 0; 4562abccd00fSHugo Mills 4563abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 45647b9ea627SShailendra Verma if (IS_ERR(args32)) 45657b9ea627SShailendra Verma return PTR_ERR(args32); 4566abccd00fSHugo Mills 45678d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 456884dbeb87SDan Carpenter if (!args64) { 456984dbeb87SDan Carpenter ret = -ENOMEM; 4570abccd00fSHugo Mills goto out; 4571abccd00fSHugo Mills } 4572abccd00fSHugo Mills 4573abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 4574abccd00fSHugo Mills args64->stransid = args32->stransid; 4575abccd00fSHugo Mills args64->rtransid = args32->rtransid; 4576abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 4577abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 4578abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 4579abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 4580abccd00fSHugo Mills args64->flags = args32->flags; 4581abccd00fSHugo Mills 4582e4fed17aSChristian Brauner ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64); 4583abccd00fSHugo Mills if (ret) 4584abccd00fSHugo Mills goto out; 4585abccd00fSHugo Mills 4586abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 4587abccd00fSHugo Mills args32->stransid = args64->stransid; 4588abccd00fSHugo Mills args32->rtransid = args64->rtransid; 4589abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 4590abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 4591abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 4592abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 4593abccd00fSHugo Mills args32->flags = args64->flags; 4594abccd00fSHugo Mills 4595abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 4596abccd00fSHugo Mills if (ret) 4597abccd00fSHugo Mills ret = -EFAULT; 4598abccd00fSHugo Mills 4599abccd00fSHugo Mills out: 4600abccd00fSHugo Mills kfree(args32); 4601abccd00fSHugo Mills kfree(args64); 4602abccd00fSHugo Mills return ret; 4603abccd00fSHugo Mills } 4604abccd00fSHugo Mills #endif 4605abccd00fSHugo Mills 4606abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 4607abccd00fSHugo Mills void __user *arg) 4608abccd00fSHugo Mills { 4609abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 4610abccd00fSHugo Mills int ret = 0; 4611abccd00fSHugo Mills 4612abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 46137b9ea627SShailendra Verma if (IS_ERR(sa)) 46147b9ea627SShailendra Verma return PTR_ERR(sa); 4615abccd00fSHugo Mills 4616e4fed17aSChristian Brauner ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa); 4617abccd00fSHugo Mills 4618abccd00fSHugo Mills if (ret) 4619abccd00fSHugo Mills goto out; 4620abccd00fSHugo Mills 46218ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 46228ea05e3aSAlexander Block if (ret) 46238ea05e3aSAlexander Block ret = -EFAULT; 46248ea05e3aSAlexander Block 46258ea05e3aSAlexander Block out: 46268ea05e3aSAlexander Block kfree(sa); 46278ea05e3aSAlexander Block return ret; 46288ea05e3aSAlexander Block } 46298ea05e3aSAlexander Block 4630b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, 4631b929c1d8SMarcos Paulo de Souza void __user *arg) 4632867ab667Sjeff.liu { 4633a1b83ac5SAnand Jain size_t len; 4634867ab667Sjeff.liu int ret; 4635a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4636a1b83ac5SAnand Jain 46370b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 46380b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 46390b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 4640a1b83ac5SAnand Jain 4641a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4642867ab667Sjeff.liu 4643867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 46440b246afaSJeff Mahoney btrfs_warn(fs_info, 46450b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 46460b246afaSJeff Mahoney --len); 4647867ab667Sjeff.liu } 4648867ab667Sjeff.liu 4649867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4650867ab667Sjeff.liu 4651867ab667Sjeff.liu return ret ? -EFAULT : 0; 4652867ab667Sjeff.liu } 4653867ab667Sjeff.liu 4654a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4655a8bfd4abSjeff.liu { 46560b246afaSJeff Mahoney struct inode *inode = file_inode(file); 46570b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 46580b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 46590b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 4660a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4661a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4662a8bfd4abSjeff.liu int ret; 4663a8bfd4abSjeff.liu 4664a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4665a8bfd4abSjeff.liu return -EPERM; 4666a8bfd4abSjeff.liu 4667a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4668a8bfd4abSjeff.liu return -EFAULT; 4669a8bfd4abSjeff.liu 4670a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 46710b246afaSJeff Mahoney btrfs_err(fs_info, 46725d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 4673a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4674a8bfd4abSjeff.liu return -EINVAL; 4675a8bfd4abSjeff.liu } 4676a8bfd4abSjeff.liu 4677a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4678a8bfd4abSjeff.liu if (ret) 4679a8bfd4abSjeff.liu return ret; 4680a8bfd4abSjeff.liu 4681a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4682a8bfd4abSjeff.liu if (IS_ERR(trans)) { 4683a8bfd4abSjeff.liu ret = PTR_ERR(trans); 4684a8bfd4abSjeff.liu goto out_unlock; 4685a8bfd4abSjeff.liu } 4686a8bfd4abSjeff.liu 46870b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 4688a8bfd4abSjeff.liu strcpy(super_block->label, label); 46890b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 46903a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 4691a8bfd4abSjeff.liu 4692a8bfd4abSjeff.liu out_unlock: 4693a8bfd4abSjeff.liu mnt_drop_write_file(file); 4694a8bfd4abSjeff.liu return ret; 4695a8bfd4abSjeff.liu } 4696a8bfd4abSjeff.liu 46972eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 46982eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 46992eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 47002eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 47012eaa055fSJeff Mahoney 4702d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 47032eaa055fSJeff Mahoney { 47044d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 47052eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 47062eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 47072eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 47082eaa055fSJeff Mahoney }; 47092eaa055fSJeff Mahoney 47102eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 47112eaa055fSJeff Mahoney return -EFAULT; 47122eaa055fSJeff Mahoney 47132eaa055fSJeff Mahoney return 0; 47142eaa055fSJeff Mahoney } 47152eaa055fSJeff Mahoney 4716b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, 4717b929c1d8SMarcos Paulo de Souza void __user *arg) 47182eaa055fSJeff Mahoney { 47190b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 47202eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 47212eaa055fSJeff Mahoney 47222eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 47232eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 47242eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 47252eaa055fSJeff Mahoney 47262eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 47272eaa055fSJeff Mahoney return -EFAULT; 47282eaa055fSJeff Mahoney 47292eaa055fSJeff Mahoney return 0; 47302eaa055fSJeff Mahoney } 47312eaa055fSJeff Mahoney 47322ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 47333b02a68aSJeff Mahoney enum btrfs_feature_set set, 47342eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 47352eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 47362eaa055fSJeff Mahoney { 4737f10152bcSDavid Sterba const char *type = btrfs_feature_set_name(set); 47383b02a68aSJeff Mahoney char *names; 47392eaa055fSJeff Mahoney u64 disallowed, unsupported; 47402eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 47412eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 47422eaa055fSJeff Mahoney 47432eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 47442eaa055fSJeff Mahoney if (unsupported) { 47453b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 47463b02a68aSJeff Mahoney if (names) { 47470b246afaSJeff Mahoney btrfs_warn(fs_info, 47483b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 47493b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 47503b02a68aSJeff Mahoney kfree(names); 47513b02a68aSJeff Mahoney } else 47520b246afaSJeff Mahoney btrfs_warn(fs_info, 47532eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 47542eaa055fSJeff Mahoney type, unsupported); 47552eaa055fSJeff Mahoney return -EOPNOTSUPP; 47562eaa055fSJeff Mahoney } 47572eaa055fSJeff Mahoney 47582eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 47592eaa055fSJeff Mahoney if (disallowed) { 47603b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 47613b02a68aSJeff Mahoney if (names) { 47620b246afaSJeff Mahoney btrfs_warn(fs_info, 47633b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 47643b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 47653b02a68aSJeff Mahoney kfree(names); 47663b02a68aSJeff Mahoney } else 47670b246afaSJeff Mahoney btrfs_warn(fs_info, 47682eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 47692eaa055fSJeff Mahoney type, disallowed); 47702eaa055fSJeff Mahoney return -EPERM; 47712eaa055fSJeff Mahoney } 47722eaa055fSJeff Mahoney 47732eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 47742eaa055fSJeff Mahoney if (disallowed) { 47753b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 47763b02a68aSJeff Mahoney if (names) { 47770b246afaSJeff Mahoney btrfs_warn(fs_info, 47783b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 47793b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 47803b02a68aSJeff Mahoney kfree(names); 47813b02a68aSJeff Mahoney } else 47820b246afaSJeff Mahoney btrfs_warn(fs_info, 47832eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 47842eaa055fSJeff Mahoney type, disallowed); 47852eaa055fSJeff Mahoney return -EPERM; 47862eaa055fSJeff Mahoney } 47872eaa055fSJeff Mahoney 47882eaa055fSJeff Mahoney return 0; 47892eaa055fSJeff Mahoney } 47902eaa055fSJeff Mahoney 47912ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 47922ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 47932eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 47942eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 47952eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 47962eaa055fSJeff Mahoney 47972eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 47982eaa055fSJeff Mahoney { 47990b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48010b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48020b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 48032eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 48042eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 48052eaa055fSJeff Mahoney u64 newflags; 48062eaa055fSJeff Mahoney int ret; 48072eaa055fSJeff Mahoney 48082eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 48092eaa055fSJeff Mahoney return -EPERM; 48102eaa055fSJeff Mahoney 48112eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 48122eaa055fSJeff Mahoney return -EFAULT; 48132eaa055fSJeff Mahoney 48142eaa055fSJeff Mahoney /* Nothing to do */ 48152eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 48162eaa055fSJeff Mahoney !flags[0].incompat_flags) 48172eaa055fSJeff Mahoney return 0; 48182eaa055fSJeff Mahoney 48192ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 48202eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 48212eaa055fSJeff Mahoney if (ret) 48222eaa055fSJeff Mahoney return ret; 48232eaa055fSJeff Mahoney 48242ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 48252eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 48262eaa055fSJeff Mahoney if (ret) 48272eaa055fSJeff Mahoney return ret; 48282eaa055fSJeff Mahoney 48292ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 48302eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 48312eaa055fSJeff Mahoney if (ret) 48322eaa055fSJeff Mahoney return ret; 48332eaa055fSJeff Mahoney 48347ab19625SDavid Sterba ret = mnt_want_write_file(file); 48357ab19625SDavid Sterba if (ret) 48367ab19625SDavid Sterba return ret; 48377ab19625SDavid Sterba 48388051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 48397ab19625SDavid Sterba if (IS_ERR(trans)) { 48407ab19625SDavid Sterba ret = PTR_ERR(trans); 48417ab19625SDavid Sterba goto out_drop_write; 48427ab19625SDavid Sterba } 48432eaa055fSJeff Mahoney 48440b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 48452eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 48462eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 48472eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 48482eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 48492eaa055fSJeff Mahoney 48502eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 48512eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 48522eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 48532eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 48542eaa055fSJeff Mahoney 48552eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 48562eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 48572eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 48582eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 48590b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 48602eaa055fSJeff Mahoney 48613a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 48627ab19625SDavid Sterba out_drop_write: 48637ab19625SDavid Sterba mnt_drop_write_file(file); 48647ab19625SDavid Sterba 48657ab19625SDavid Sterba return ret; 48662eaa055fSJeff Mahoney } 48672eaa055fSJeff Mahoney 48682351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 48692351f431SJosef Bacik { 48702351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 48712351f431SJosef Bacik int ret; 48722351f431SJosef Bacik 48732351f431SJosef Bacik if (compat) { 48742351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 48752351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 48762351f431SJosef Bacik 48772351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 48782351f431SJosef Bacik if (ret) 48792351f431SJosef Bacik return -EFAULT; 48802351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 48812351f431SJosef Bacik if (!arg) 48822351f431SJosef Bacik return -ENOMEM; 48832351f431SJosef Bacik arg->send_fd = args32.send_fd; 48842351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 48852351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 48862351f431SJosef Bacik arg->parent_root = args32.parent_root; 48872351f431SJosef Bacik arg->flags = args32.flags; 48882351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 48892351f431SJosef Bacik sizeof(args32.reserved)); 48902351f431SJosef Bacik #else 48912351f431SJosef Bacik return -ENOTTY; 48922351f431SJosef Bacik #endif 48932351f431SJosef Bacik } else { 48942351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 48952351f431SJosef Bacik if (IS_ERR(arg)) 48962351f431SJosef Bacik return PTR_ERR(arg); 48972351f431SJosef Bacik } 48982351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 48992351f431SJosef Bacik kfree(arg); 49002351f431SJosef Bacik return ret; 49012351f431SJosef Bacik } 49022351f431SJosef Bacik 4903f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 4904f46b5a66SChristoph Hellwig cmd, unsigned long arg) 4905f46b5a66SChristoph Hellwig { 49060b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49070b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49080b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 49094bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 4910f46b5a66SChristoph Hellwig 4911f46b5a66SChristoph Hellwig switch (cmd) { 49126cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 49136cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 491440cf931fSEric Sandeen case FS_IOC_GETFSLABEL: 4915b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_fslabel(fs_info, argp); 491640cf931fSEric Sandeen case FS_IOC_SETFSLABEL: 491740cf931fSEric Sandeen return btrfs_ioctl_set_fslabel(file, argp); 4918f7039b1dSLi Dongyang case FITRIM: 4919b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_fitrim(fs_info, argp); 4920f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 4921fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 4922fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 4923fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 49243de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 4925fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 49266f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 49276f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 492876dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 4929949964c9SMarcos Paulo de Souza return btrfs_ioctl_snap_destroy(file, argp, false); 4930949964c9SMarcos Paulo de Souza case BTRFS_IOC_SNAP_DESTROY_V2: 4931949964c9SMarcos Paulo de Souza return btrfs_ioctl_snap_destroy(file, argp, true); 49320caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 49330caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 49340caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 49350caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 49366ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 49376ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 4938f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 49391e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 49401e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 49411e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 4942f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 4943198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 4944f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 49452ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 4946f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 4947da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 49486b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 49496b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 4950475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 49512ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 4952475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 49532ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 4954f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 49559ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 4956ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 4957ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 4958cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 4959cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 4960ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 4961ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 4962d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 4963d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 4964d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 4965d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 4966d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 4967d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 49681406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 49692ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 49709b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 49719b199859SFilipe David Borba Manana int ret; 49729b199859SFilipe David Borba Manana 49739db4dc24SNikolay Borisov ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false); 49749b199859SFilipe David Borba Manana if (ret) 49759b199859SFilipe David Borba Manana return ret; 49760b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 49772fad4e83SDavid Sterba /* 49782fad4e83SDavid Sterba * The transaction thread may want to do more work, 497901327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 49802fad4e83SDavid Sterba * processing uncleaned subvols. 49812fad4e83SDavid Sterba */ 49820b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 49839b199859SFilipe David Borba Manana return ret; 49849b199859SFilipe David Borba Manana } 498546204592SSage Weil case BTRFS_IOC_START_SYNC: 49869a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 498746204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 49882ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 4989475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 4990b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 4991475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 49922ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 4993475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 49942ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 4995c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 49969ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 4997837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 49982ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 499919a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 50002ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 50018ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 50028ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5003abccd00fSHugo Mills #ifdef CONFIG_64BIT 5004abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5005abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5006abccd00fSHugo Mills #endif 500731db9f7cSAlexander Block case BTRFS_IOC_SEND: 50082351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 50092351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 50102351f431SJosef Bacik case BTRFS_IOC_SEND_32: 50112351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 50122351f431SJosef Bacik #endif 5013c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 50142ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 50155d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5016905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 50175d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5018905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 50195d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5020905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 50215d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5022905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 50232f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 50242f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 50252f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 5026b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_status(fs_info, argp); 502757254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 5028b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_wait(fs_info, argp); 50293f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 50302ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 50312eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5032d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 50332eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 5034b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_features(fs_info, argp); 50352eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 50362eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5037b64ec075STomohiro Misono case BTRFS_IOC_GET_SUBVOL_INFO: 5038b64ec075STomohiro Misono return btrfs_ioctl_get_subvol_info(file, argp); 503942e4b520STomohiro Misono case BTRFS_IOC_GET_SUBVOL_ROOTREF: 504042e4b520STomohiro Misono return btrfs_ioctl_get_subvol_rootref(file, argp); 504123d0b79dSTomohiro Misono case BTRFS_IOC_INO_LOOKUP_USER: 504223d0b79dSTomohiro Misono return btrfs_ioctl_ino_lookup_user(file, argp); 504314605409SBoris Burkov case FS_IOC_ENABLE_VERITY: 504414605409SBoris Burkov return fsverity_ioctl_enable(file, (const void __user *)argp); 504514605409SBoris Burkov case FS_IOC_MEASURE_VERITY: 504614605409SBoris Burkov return fsverity_ioctl_measure(file, argp); 5047f46b5a66SChristoph Hellwig } 5048f46b5a66SChristoph Hellwig 5049f46b5a66SChristoph Hellwig return -ENOTTY; 5050f46b5a66SChristoph Hellwig } 50514c63c245SLuke Dashjr 50524c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 50534c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 50544c63c245SLuke Dashjr { 50552a362249SJeff Mahoney /* 50562a362249SJeff Mahoney * These all access 32-bit values anyway so no further 50572a362249SJeff Mahoney * handling is necessary. 50582a362249SJeff Mahoney */ 50594c63c245SLuke Dashjr switch (cmd) { 50604c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 50614c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 50624c63c245SLuke Dashjr break; 50634c63c245SLuke Dashjr } 50644c63c245SLuke Dashjr 50654c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 50664c63c245SLuke Dashjr } 50674c63c245SLuke Dashjr #endif 5068