1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2f46b5a66SChristoph Hellwig /* 3f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 4f46b5a66SChristoph Hellwig */ 5f46b5a66SChristoph Hellwig 6f46b5a66SChristoph Hellwig #include <linux/kernel.h> 7f46b5a66SChristoph Hellwig #include <linux/bio.h> 8f46b5a66SChristoph Hellwig #include <linux/file.h> 9f46b5a66SChristoph Hellwig #include <linux/fs.h> 10cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 11f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 12f46b5a66SChristoph Hellwig #include <linux/highmem.h> 13f46b5a66SChristoph Hellwig #include <linux/time.h> 14f46b5a66SChristoph Hellwig #include <linux/string.h> 15f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 16cb8e7090SChristoph Hellwig #include <linux/mount.h> 17cb8e7090SChristoph Hellwig #include <linux/namei.h> 18f46b5a66SChristoph Hellwig #include <linux/writeback.h> 19f46b5a66SChristoph Hellwig #include <linux/compat.h> 20cb8e7090SChristoph Hellwig #include <linux/security.h> 21f46b5a66SChristoph Hellwig #include <linux/xattr.h> 22f54de068SDavid Sterba #include <linux/mm.h> 235a0e3ad6STejun Heo #include <linux/slab.h> 24f7039b1dSLi Dongyang #include <linux/blkdev.h> 258ea05e3aSAlexander Block #include <linux/uuid.h> 2655e301fdSFilipe Brandenburger #include <linux/btrfs.h> 27416161dbSMark Fasheh #include <linux/uaccess.h> 28ae5e165dSJeff Layton #include <linux/iversion.h> 29f46b5a66SChristoph Hellwig #include "ctree.h" 30f46b5a66SChristoph Hellwig #include "disk-io.h" 31f46b5a66SChristoph Hellwig #include "transaction.h" 32f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 33f46b5a66SChristoph Hellwig #include "print-tree.h" 34f46b5a66SChristoph Hellwig #include "volumes.h" 35925baeddSChris Mason #include "locking.h" 36581bb050SLi Zefan #include "inode-map.h" 37d7728c96SJan Schmidt #include "backref.h" 38606686eeSJosef Bacik #include "rcu-string.h" 3931db9f7cSAlexander Block #include "send.h" 403f6bcfbdSStefan Behrens #include "dev-replace.h" 4163541927SFilipe David Borba Manana #include "props.h" 423b02a68aSJeff Mahoney #include "sysfs.h" 43fcebe456SJosef Bacik #include "qgroup.h" 441ec9a1aeSFilipe Manana #include "tree-log.h" 45ebb8765bSAnand Jain #include "compression.h" 468719aaaeSJosef Bacik #include "space-info.h" 4786736342SJosef Bacik #include "delalloc-space.h" 48aac0023cSJosef Bacik #include "block-group.h" 49f46b5a66SChristoph Hellwig 50abccd00fSHugo Mills #ifdef CONFIG_64BIT 51abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 52abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 53abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 54abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 55abccd00fSHugo Mills */ 56abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 57abccd00fSHugo Mills __u64 sec; 58abccd00fSHugo Mills __u32 nsec; 59abccd00fSHugo Mills } __attribute__ ((__packed__)); 60abccd00fSHugo Mills 61abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 62abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 63abccd00fSHugo Mills __u64 stransid; /* in */ 64abccd00fSHugo Mills __u64 rtransid; /* out */ 65abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 66abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 67abccd00fSHugo Mills __u64 flags; /* in */ 68abccd00fSHugo Mills __u64 reserved[16]; /* in */ 69abccd00fSHugo Mills } __attribute__ ((__packed__)); 70abccd00fSHugo Mills 71abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 72abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 73abccd00fSHugo Mills #endif 74abccd00fSHugo Mills 752351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 762351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 772351f431SJosef Bacik __s64 send_fd; /* in */ 782351f431SJosef Bacik __u64 clone_sources_count; /* in */ 792351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 802351f431SJosef Bacik __u64 parent_root; /* in */ 812351f431SJosef Bacik __u64 flags; /* in */ 822351f431SJosef Bacik __u64 reserved[4]; /* in */ 832351f431SJosef Bacik } __attribute__ ((__packed__)); 842351f431SJosef Bacik 852351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 862351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 872351f431SJosef Bacik #endif 88abccd00fSHugo Mills 89416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 901c919a5eSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff, 911c919a5eSMark Fasheh int no_time_update); 92416161dbSMark Fasheh 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 */ 109a157d4fdSDavid Sterba static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) 1106cbff00fSChristoph Hellwig { 1116cbff00fSChristoph Hellwig unsigned int iflags = 0; 1126cbff00fSChristoph Hellwig 1136cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1146cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1156cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1166cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1176cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1186cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1196cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1206cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1216cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1226cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1236cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1246cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 125d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 126d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 127d0092bddSLi Zefan 12813f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 129d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 13013f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 13113f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1326cbff00fSChristoph Hellwig 1336cbff00fSChristoph Hellwig return iflags; 1346cbff00fSChristoph Hellwig } 1356cbff00fSChristoph Hellwig 1366cbff00fSChristoph Hellwig /* 1376cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1386cbff00fSChristoph Hellwig */ 1397b6a221eSDavid Sterba void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) 1406cbff00fSChristoph Hellwig { 1415c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1423cc79392SFilipe Manana unsigned int new_fl = 0; 1436cbff00fSChristoph Hellwig 1445c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_SYNC) 1453cc79392SFilipe Manana new_fl |= S_SYNC; 1465c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_IMMUTABLE) 1473cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1485c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_APPEND) 1493cc79392SFilipe Manana new_fl |= S_APPEND; 1505c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_NOATIME) 1513cc79392SFilipe Manana new_fl |= S_NOATIME; 1525c57b8b6SDavid Sterba if (binode->flags & BTRFS_INODE_DIRSYNC) 1533cc79392SFilipe Manana new_fl |= S_DIRSYNC; 1543cc79392SFilipe Manana 1553cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 1563cc79392SFilipe Manana S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, 1573cc79392SFilipe Manana new_fl); 1586cbff00fSChristoph Hellwig } 1596cbff00fSChristoph Hellwig 1606cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1616cbff00fSChristoph Hellwig { 1625c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(file_inode(file)); 1635c57b8b6SDavid Sterba unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); 1646cbff00fSChristoph Hellwig 1656cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1666cbff00fSChristoph Hellwig return -EFAULT; 1676cbff00fSChristoph Hellwig return 0; 1686cbff00fSChristoph Hellwig } 1696cbff00fSChristoph Hellwig 1705ba76abfSDavid Sterba /* Check if @flags are a supported and valid set of FS_*_FL flags */ 1715ba76abfSDavid Sterba static int check_fsflags(unsigned int flags) 17275e7cb7fSLiu Bo { 17375e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 17475e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 17575e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 176e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 177e1e8fb6aSLi Zefan FS_NOCOW_FL)) 17875e7cb7fSLiu Bo return -EOPNOTSUPP; 17975e7cb7fSLiu Bo 18075e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 18175e7cb7fSLiu Bo return -EINVAL; 18275e7cb7fSLiu Bo 18375e7cb7fSLiu Bo return 0; 18475e7cb7fSLiu Bo } 18575e7cb7fSLiu Bo 1866cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1876cbff00fSChristoph Hellwig { 188496ad9aaSAl Viro struct inode *inode = file_inode(file); 1890b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1905c57b8b6SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 1915c57b8b6SDavid Sterba struct btrfs_root *root = binode->root; 1926cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1935aca2842SDarrick J. Wong unsigned int fsflags, old_fsflags; 1946cbff00fSChristoph Hellwig int ret; 195ff9fef55SAnand Jain const char *comp = NULL; 196d2b8fcfeSAnand Jain u32 binode_flags = binode->flags; 1976cbff00fSChristoph Hellwig 198bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 199bd60ea0fSDavid Sterba return -EPERM; 200bd60ea0fSDavid Sterba 201b83cc969SLi Zefan if (btrfs_root_readonly(root)) 202b83cc969SLi Zefan return -EROFS; 203b83cc969SLi Zefan 2045c57b8b6SDavid Sterba if (copy_from_user(&fsflags, arg, sizeof(fsflags))) 2056cbff00fSChristoph Hellwig return -EFAULT; 2066cbff00fSChristoph Hellwig 2075c57b8b6SDavid Sterba ret = check_fsflags(fsflags); 20875e7cb7fSLiu Bo if (ret) 20975e7cb7fSLiu Bo return ret; 2106cbff00fSChristoph Hellwig 211e7848683SJan Kara ret = mnt_want_write_file(file); 212e7848683SJan Kara if (ret) 213e7848683SJan Kara return ret; 214e7848683SJan Kara 2155955102cSAl Viro inode_lock(inode); 2166cbff00fSChristoph Hellwig 2175c57b8b6SDavid Sterba fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); 2185aca2842SDarrick J. Wong old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); 2195aca2842SDarrick J. Wong ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags); 2205aca2842SDarrick J. Wong if (ret) 2216cbff00fSChristoph Hellwig goto out_unlock; 2226cbff00fSChristoph Hellwig 2235c57b8b6SDavid Sterba if (fsflags & FS_SYNC_FL) 224d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_SYNC; 2256cbff00fSChristoph Hellwig else 226d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_SYNC; 2275c57b8b6SDavid Sterba if (fsflags & FS_IMMUTABLE_FL) 228d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_IMMUTABLE; 2296cbff00fSChristoph Hellwig else 230d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_IMMUTABLE; 2315c57b8b6SDavid Sterba if (fsflags & FS_APPEND_FL) 232d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_APPEND; 2336cbff00fSChristoph Hellwig else 234d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_APPEND; 2355c57b8b6SDavid Sterba if (fsflags & FS_NODUMP_FL) 236d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODUMP; 2376cbff00fSChristoph Hellwig else 238d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODUMP; 2395c57b8b6SDavid Sterba if (fsflags & FS_NOATIME_FL) 240d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOATIME; 2416cbff00fSChristoph Hellwig else 242d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOATIME; 2435c57b8b6SDavid Sterba if (fsflags & FS_DIRSYNC_FL) 244d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_DIRSYNC; 2456cbff00fSChristoph Hellwig else 246d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_DIRSYNC; 2475c57b8b6SDavid Sterba if (fsflags & FS_NOCOW_FL) { 24844e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 2497e97b8daSDavid Sterba /* 2507e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2517e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2527e97b8daSDavid Sterba * status of the file and will not set it. 2537e97b8daSDavid Sterba */ 2547e97b8daSDavid Sterba if (inode->i_size == 0) 255d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW | 256d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM; 2577e97b8daSDavid Sterba } else { 258d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NODATACOW; 2597e97b8daSDavid Sterba } 2607e97b8daSDavid Sterba } else { 2617e97b8daSDavid Sterba /* 26201327610SNicholas D Steeves * Revert back under same assumptions as above 2637e97b8daSDavid Sterba */ 26444e5194bSAnand Jain if (S_ISREG(inode->i_mode)) { 2657e97b8daSDavid Sterba if (inode->i_size == 0) 266d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_NODATACOW | 267d2b8fcfeSAnand Jain BTRFS_INODE_NODATASUM); 2687e97b8daSDavid Sterba } else { 269d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NODATACOW; 2707e97b8daSDavid Sterba } 2717e97b8daSDavid Sterba } 2726cbff00fSChristoph Hellwig 27375e7cb7fSLiu Bo /* 27475e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 27575e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 27675e7cb7fSLiu Bo * things smaller. 27775e7cb7fSLiu Bo */ 2785c57b8b6SDavid Sterba if (fsflags & FS_NOCOMP_FL) { 279d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_COMPRESS; 280d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_NOCOMPRESS; 2815c57b8b6SDavid Sterba } else if (fsflags & FS_COMPR_FL) { 28263541927SFilipe David Borba Manana 283eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 284eede2bf3SOmar Sandoval ret = -ETXTBSY; 285eede2bf3SOmar Sandoval goto out_unlock; 286eede2bf3SOmar Sandoval } 287eede2bf3SOmar Sandoval 288d2b8fcfeSAnand Jain binode_flags |= BTRFS_INODE_COMPRESS; 289d2b8fcfeSAnand Jain binode_flags &= ~BTRFS_INODE_NOCOMPRESS; 29063541927SFilipe David Borba Manana 29193370509SDavid Sterba comp = btrfs_compress_type2str(fs_info->compress_type); 29293370509SDavid Sterba if (!comp || comp[0] == 0) 29393370509SDavid Sterba comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); 294ebcb904dSLi Zefan } else { 295d2b8fcfeSAnand Jain binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 29675e7cb7fSLiu Bo } 2976cbff00fSChristoph Hellwig 298ff9fef55SAnand Jain /* 299ff9fef55SAnand Jain * 1 for inode item 300ff9fef55SAnand Jain * 2 for properties 301ff9fef55SAnand Jain */ 302ff9fef55SAnand Jain trans = btrfs_start_transaction(root, 3); 303f062abf0SLi Zefan if (IS_ERR(trans)) { 304f062abf0SLi Zefan ret = PTR_ERR(trans); 305d2b8fcfeSAnand Jain goto out_unlock; 306f062abf0SLi Zefan } 3076cbff00fSChristoph Hellwig 308ff9fef55SAnand Jain if (comp) { 309ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, 310ff9fef55SAnand Jain strlen(comp), 0); 311ff9fef55SAnand Jain if (ret) { 312ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 313ff9fef55SAnand Jain goto out_end_trans; 314ff9fef55SAnand Jain } 315ff9fef55SAnand Jain } else { 316ff9fef55SAnand Jain ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 317ff9fef55SAnand Jain 0, 0); 318ff9fef55SAnand Jain if (ret && ret != -ENODATA) { 319ff9fef55SAnand Jain btrfs_abort_transaction(trans, ret); 320ff9fef55SAnand Jain goto out_end_trans; 321ff9fef55SAnand Jain } 322ff9fef55SAnand Jain } 323ff9fef55SAnand Jain 324d2b8fcfeSAnand Jain binode->flags = binode_flags; 3257b6a221eSDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 3260c4d2d95SJosef Bacik inode_inc_iversion(inode); 327c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3286cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3296cbff00fSChristoph Hellwig 330ff9fef55SAnand Jain out_end_trans: 3313a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3326cbff00fSChristoph Hellwig out_unlock: 3335955102cSAl Viro inode_unlock(inode); 334e7848683SJan Kara mnt_drop_write_file(file); 3352d4e6f6aSliubo return ret; 3366cbff00fSChristoph Hellwig } 3376cbff00fSChristoph Hellwig 33819f93b3cSDavid Sterba /* 33919f93b3cSDavid Sterba * Translate btrfs internal inode flags to xflags as expected by the 34019f93b3cSDavid Sterba * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are 34119f93b3cSDavid Sterba * silently dropped. 34219f93b3cSDavid Sterba */ 34319f93b3cSDavid Sterba static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) 34419f93b3cSDavid Sterba { 34519f93b3cSDavid Sterba unsigned int xflags = 0; 34619f93b3cSDavid Sterba 34719f93b3cSDavid Sterba if (flags & BTRFS_INODE_APPEND) 34819f93b3cSDavid Sterba xflags |= FS_XFLAG_APPEND; 34919f93b3cSDavid Sterba if (flags & BTRFS_INODE_IMMUTABLE) 35019f93b3cSDavid Sterba xflags |= FS_XFLAG_IMMUTABLE; 35119f93b3cSDavid Sterba if (flags & BTRFS_INODE_NOATIME) 35219f93b3cSDavid Sterba xflags |= FS_XFLAG_NOATIME; 35319f93b3cSDavid Sterba if (flags & BTRFS_INODE_NODUMP) 35419f93b3cSDavid Sterba xflags |= FS_XFLAG_NODUMP; 35519f93b3cSDavid Sterba if (flags & BTRFS_INODE_SYNC) 35619f93b3cSDavid Sterba xflags |= FS_XFLAG_SYNC; 35719f93b3cSDavid Sterba 35819f93b3cSDavid Sterba return xflags; 35919f93b3cSDavid Sterba } 36019f93b3cSDavid Sterba 36119f93b3cSDavid Sterba /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ 36219f93b3cSDavid Sterba static int check_xflags(unsigned int flags) 36319f93b3cSDavid Sterba { 36419f93b3cSDavid Sterba if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | 36519f93b3cSDavid Sterba FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) 36619f93b3cSDavid Sterba return -EOPNOTSUPP; 36719f93b3cSDavid Sterba return 0; 36819f93b3cSDavid Sterba } 36919f93b3cSDavid Sterba 370e4202ac9SDavid Sterba /* 371e4202ac9SDavid Sterba * Set the xflags from the internal inode flags. The remaining items of fsxattr 372e4202ac9SDavid Sterba * are zeroed. 373e4202ac9SDavid Sterba */ 374e4202ac9SDavid Sterba static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) 375e4202ac9SDavid Sterba { 376e4202ac9SDavid Sterba struct btrfs_inode *binode = BTRFS_I(file_inode(file)); 377e4202ac9SDavid Sterba struct fsxattr fa; 378e4202ac9SDavid Sterba 3797b0e492eSDarrick J. Wong simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags)); 380e4202ac9SDavid Sterba if (copy_to_user(arg, &fa, sizeof(fa))) 381e4202ac9SDavid Sterba return -EFAULT; 382e4202ac9SDavid Sterba 383e4202ac9SDavid Sterba return 0; 384e4202ac9SDavid Sterba } 385e4202ac9SDavid Sterba 386025f2121SDavid Sterba static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) 387025f2121SDavid Sterba { 388025f2121SDavid Sterba struct inode *inode = file_inode(file); 389025f2121SDavid Sterba struct btrfs_inode *binode = BTRFS_I(inode); 390025f2121SDavid Sterba struct btrfs_root *root = binode->root; 391025f2121SDavid Sterba struct btrfs_trans_handle *trans; 3927b0e492eSDarrick J. Wong struct fsxattr fa, old_fa; 393025f2121SDavid Sterba unsigned old_flags; 394025f2121SDavid Sterba unsigned old_i_flags; 395025f2121SDavid Sterba int ret = 0; 396025f2121SDavid Sterba 397025f2121SDavid Sterba if (!inode_owner_or_capable(inode)) 398025f2121SDavid Sterba return -EPERM; 399025f2121SDavid Sterba 400025f2121SDavid Sterba if (btrfs_root_readonly(root)) 401025f2121SDavid Sterba return -EROFS; 402025f2121SDavid Sterba 403025f2121SDavid Sterba if (copy_from_user(&fa, arg, sizeof(fa))) 404025f2121SDavid Sterba return -EFAULT; 405025f2121SDavid Sterba 406025f2121SDavid Sterba ret = check_xflags(fa.fsx_xflags); 407025f2121SDavid Sterba if (ret) 408025f2121SDavid Sterba return ret; 409025f2121SDavid Sterba 410025f2121SDavid Sterba if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) 411025f2121SDavid Sterba return -EOPNOTSUPP; 412025f2121SDavid Sterba 413025f2121SDavid Sterba ret = mnt_want_write_file(file); 414025f2121SDavid Sterba if (ret) 415025f2121SDavid Sterba return ret; 416025f2121SDavid Sterba 417025f2121SDavid Sterba inode_lock(inode); 418025f2121SDavid Sterba 419025f2121SDavid Sterba old_flags = binode->flags; 420025f2121SDavid Sterba old_i_flags = inode->i_flags; 421025f2121SDavid Sterba 4227b0e492eSDarrick J. Wong simple_fill_fsxattr(&old_fa, 4237b0e492eSDarrick J. Wong btrfs_inode_flags_to_xflags(binode->flags)); 4247b0e492eSDarrick J. Wong ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); 4257b0e492eSDarrick J. Wong if (ret) 426025f2121SDavid Sterba goto out_unlock; 427025f2121SDavid Sterba 428025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_SYNC) 429025f2121SDavid Sterba binode->flags |= BTRFS_INODE_SYNC; 430025f2121SDavid Sterba else 431025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_SYNC; 432025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) 433025f2121SDavid Sterba binode->flags |= BTRFS_INODE_IMMUTABLE; 434025f2121SDavid Sterba else 435025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_IMMUTABLE; 436025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_APPEND) 437025f2121SDavid Sterba binode->flags |= BTRFS_INODE_APPEND; 438025f2121SDavid Sterba else 439025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_APPEND; 440025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_NODUMP) 441025f2121SDavid Sterba binode->flags |= BTRFS_INODE_NODUMP; 442025f2121SDavid Sterba else 443025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_NODUMP; 444025f2121SDavid Sterba if (fa.fsx_xflags & FS_XFLAG_NOATIME) 445025f2121SDavid Sterba binode->flags |= BTRFS_INODE_NOATIME; 446025f2121SDavid Sterba else 447025f2121SDavid Sterba binode->flags &= ~BTRFS_INODE_NOATIME; 448025f2121SDavid Sterba 449025f2121SDavid Sterba /* 1 item for the inode */ 450025f2121SDavid Sterba trans = btrfs_start_transaction(root, 1); 451025f2121SDavid Sterba if (IS_ERR(trans)) { 452025f2121SDavid Sterba ret = PTR_ERR(trans); 453025f2121SDavid Sterba goto out_unlock; 454025f2121SDavid Sterba } 455025f2121SDavid Sterba 456025f2121SDavid Sterba btrfs_sync_inode_flags_to_i_flags(inode); 457025f2121SDavid Sterba inode_inc_iversion(inode); 458025f2121SDavid Sterba inode->i_ctime = current_time(inode); 459025f2121SDavid Sterba ret = btrfs_update_inode(trans, root, inode); 460025f2121SDavid Sterba 461025f2121SDavid Sterba btrfs_end_transaction(trans); 462025f2121SDavid Sterba 463025f2121SDavid Sterba out_unlock: 464025f2121SDavid Sterba if (ret) { 465025f2121SDavid Sterba binode->flags = old_flags; 466025f2121SDavid Sterba inode->i_flags = old_i_flags; 467025f2121SDavid Sterba } 468025f2121SDavid Sterba 469025f2121SDavid Sterba inode_unlock(inode); 470025f2121SDavid Sterba mnt_drop_write_file(file); 471025f2121SDavid Sterba 472025f2121SDavid Sterba return ret; 473025f2121SDavid Sterba } 474025f2121SDavid Sterba 4756cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 4766cbff00fSChristoph Hellwig { 477496ad9aaSAl Viro struct inode *inode = file_inode(file); 4786cbff00fSChristoph Hellwig 4796cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 4806cbff00fSChristoph Hellwig } 481f46b5a66SChristoph Hellwig 482b929c1d8SMarcos Paulo de Souza static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, 483b929c1d8SMarcos Paulo de Souza void __user *arg) 484f7039b1dSLi Dongyang { 485f7039b1dSLi Dongyang struct btrfs_device *device; 486f7039b1dSLi Dongyang struct request_queue *q; 487f7039b1dSLi Dongyang struct fstrim_range range; 488f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 489f7039b1dSLi Dongyang u64 num_devices = 0; 490f7039b1dSLi Dongyang int ret; 491f7039b1dSLi Dongyang 492f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 493f7039b1dSLi Dongyang return -EPERM; 494f7039b1dSLi Dongyang 495f35f06c3SFilipe Manana /* 496f35f06c3SFilipe Manana * If the fs is mounted with nologreplay, which requires it to be 497f35f06c3SFilipe Manana * mounted in RO mode as well, we can not allow discard on free space 498f35f06c3SFilipe Manana * inside block groups, because log trees refer to extents that are not 499f35f06c3SFilipe Manana * pinned in a block group's free space cache (pinning the extents is 500f35f06c3SFilipe Manana * precisely the first phase of replaying a log tree). 501f35f06c3SFilipe Manana */ 502f35f06c3SFilipe Manana if (btrfs_test_opt(fs_info, NOLOGREPLAY)) 503f35f06c3SFilipe Manana return -EROFS; 504f35f06c3SFilipe Manana 5051f78160cSXiao Guangrong rcu_read_lock(); 5061f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 5071f78160cSXiao Guangrong dev_list) { 508f7039b1dSLi Dongyang if (!device->bdev) 509f7039b1dSLi Dongyang continue; 510f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 511f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 512f7039b1dSLi Dongyang num_devices++; 51350d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 514f7039b1dSLi Dongyang minlen); 515f7039b1dSLi Dongyang } 516f7039b1dSLi Dongyang } 5171f78160cSXiao Guangrong rcu_read_unlock(); 518f4c697e6SLukas Czerner 519f7039b1dSLi Dongyang if (!num_devices) 520f7039b1dSLi Dongyang return -EOPNOTSUPP; 521f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 522f7039b1dSLi Dongyang return -EFAULT; 5236ba9fc8eSQu Wenruo 5246ba9fc8eSQu Wenruo /* 5256ba9fc8eSQu Wenruo * NOTE: Don't truncate the range using super->total_bytes. Bytenr of 5266ba9fc8eSQu Wenruo * block group is in the logical address space, which can be any 5276ba9fc8eSQu Wenruo * sectorsize aligned bytenr in the range [0, U64_MAX]. 5286ba9fc8eSQu Wenruo */ 5296ba9fc8eSQu Wenruo if (range.len < fs_info->sb->s_blocksize) 530f4c697e6SLukas Czerner return -EINVAL; 531f7039b1dSLi Dongyang 532f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 5332ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 534f7039b1dSLi Dongyang if (ret < 0) 535f7039b1dSLi Dongyang return ret; 536f7039b1dSLi Dongyang 537f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 538f7039b1dSLi Dongyang return -EFAULT; 539f7039b1dSLi Dongyang 540f7039b1dSLi Dongyang return 0; 541f7039b1dSLi Dongyang } 542f7039b1dSLi Dongyang 543e1f60a65SDavid Sterba int __pure btrfs_is_empty_uuid(u8 *uuid) 544dd5f9615SStefan Behrens { 54546e0f66aSChris Mason int i; 54646e0f66aSChris Mason 54746e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 54846e0f66aSChris Mason if (uuid[i]) 54946e0f66aSChris Mason return 0; 55046e0f66aSChris Mason } 55146e0f66aSChris Mason return 1; 552dd5f9615SStefan Behrens } 553dd5f9615SStefan Behrens 554d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 555cb8e7090SChristoph Hellwig struct dentry *dentry, 55652f75f4fSDavid Sterba const char *name, int namelen, 5576f72c7e2SArne Jansen u64 *async_transid, 5588696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 559f46b5a66SChristoph Hellwig { 5600b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 561f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 562f46b5a66SChristoph Hellwig struct btrfs_key key; 56349a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 564f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 565f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 566d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 56776dda93cSYan, Zheng struct btrfs_root *new_root; 568d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 56995582b00SDeepa Dinamani struct timespec64 cur_time = current_time(dir); 5705662344bSTsutomu Itoh struct inode *inode; 571f46b5a66SChristoph Hellwig int ret; 572f46b5a66SChristoph Hellwig int err; 573f46b5a66SChristoph Hellwig u64 objectid; 574f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 5753de4586cSChris Mason u64 index = 0; 5768ea05e3aSAlexander Block uuid_le new_uuid; 577f46b5a66SChristoph Hellwig 57849a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 57949a3c4d9SDavid Sterba if (!root_item) 58049a3c4d9SDavid Sterba return -ENOMEM; 58149a3c4d9SDavid Sterba 5820b246afaSJeff Mahoney ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); 5832fbe8c8aSAl Viro if (ret) 58449a3c4d9SDavid Sterba goto fail_free; 5856a912213SJosef Bacik 586e09fe2d2SQu Wenruo /* 587e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 58801327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 589e09fe2d2SQu Wenruo */ 59049a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 59149a3c4d9SDavid Sterba ret = -ENOSPC; 59249a3c4d9SDavid Sterba goto fail_free; 59349a3c4d9SDavid Sterba } 594e09fe2d2SQu Wenruo 595d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 5969ed74f2dSJosef Bacik /* 597d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 598d5c12070SMiao Xie * of create_snapshot(). 5999ed74f2dSJosef Bacik */ 600c4c129dbSGu JinXiang ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); 601d5c12070SMiao Xie if (ret) 60249a3c4d9SDavid Sterba goto fail_free; 603f46b5a66SChristoph Hellwig 604d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 605d5c12070SMiao Xie if (IS_ERR(trans)) { 606d5c12070SMiao Xie ret = PTR_ERR(trans); 6077775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 60849a3c4d9SDavid Sterba goto fail_free; 609d5c12070SMiao Xie } 610d5c12070SMiao Xie trans->block_rsv = &block_rsv; 611d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 612f46b5a66SChristoph Hellwig 613a9377422SLu Fengqi ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); 6146f72c7e2SArne Jansen if (ret) 6156f72c7e2SArne Jansen goto fail; 6166f72c7e2SArne Jansen 6174d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 6188e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 6198e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 6208e8a1e31SJosef Bacik goto fail; 6218e8a1e31SJosef Bacik } 622f46b5a66SChristoph Hellwig 623f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 624f46b5a66SChristoph Hellwig 62549a3c4d9SDavid Sterba inode_item = &root_item->inode; 6263cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 6273cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 6283cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 629da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 6300b246afaSJeff Mahoney fs_info->nodesize); 6313cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 632f46b5a66SChristoph Hellwig 63349a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 63449a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 6353cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 63608fe4db1SLi Zefan 63749a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 63849a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 63949a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 64049a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 64149a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 64249a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 643f46b5a66SChristoph Hellwig 64449a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 64549a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 6468ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 64749a3c4d9SDavid Sterba memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 64849a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 64949a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 65049a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 65149a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 65249a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 653f46b5a66SChristoph Hellwig 654925baeddSChris Mason btrfs_tree_unlock(leaf); 655f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 656f46b5a66SChristoph Hellwig leaf = NULL; 657f46b5a66SChristoph Hellwig 65849a3c4d9SDavid Sterba btrfs_set_root_dirid(root_item, new_dirid); 659f46b5a66SChristoph Hellwig 660f46b5a66SChristoph Hellwig key.objectid = objectid; 6615d4f98a2SYan Zheng key.offset = 0; 662962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 6630b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 66449a3c4d9SDavid Sterba root_item); 665f46b5a66SChristoph Hellwig if (ret) 666f46b5a66SChristoph Hellwig goto fail; 667f46b5a66SChristoph Hellwig 66876dda93cSYan, Zheng key.offset = (u64)-1; 6693619c94fSJosef Bacik new_root = btrfs_get_fs_root(fs_info, &key, true); 67079787eaaSJeff Mahoney if (IS_ERR(new_root)) { 67179787eaaSJeff Mahoney ret = PTR_ERR(new_root); 67266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 67379787eaaSJeff Mahoney goto fail; 67479787eaaSJeff Mahoney } 675fc92f798SJosef Bacik if (!btrfs_grab_fs_root(new_root)) { 676fc92f798SJosef Bacik ret = -ENOENT; 677fc92f798SJosef Bacik btrfs_abort_transaction(trans, ret); 678fc92f798SJosef Bacik goto fail; 679fc92f798SJosef Bacik } 68076dda93cSYan, Zheng 68176dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 68276dda93cSYan, Zheng 68363541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 684fc92f798SJosef Bacik btrfs_put_fs_root(new_root); 685ce598979SMark Fasheh if (ret) { 686ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 68766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 688ce598979SMark Fasheh goto fail; 689ce598979SMark Fasheh } 690ce598979SMark Fasheh 691f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 692f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 693f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 694f32e48e9SChandan Rajendra 695f46b5a66SChristoph Hellwig /* 696f46b5a66SChristoph Hellwig * insert the directory item 697f46b5a66SChristoph Hellwig */ 698877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 69979787eaaSJeff Mahoney if (ret) { 70066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 70179787eaaSJeff Mahoney goto fail; 70279787eaaSJeff Mahoney } 7033de4586cSChris Mason 704684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, 7053de4586cSChris Mason BTRFS_FT_DIR, index); 70679787eaaSJeff Mahoney if (ret) { 70766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 708f46b5a66SChristoph Hellwig goto fail; 70979787eaaSJeff Mahoney } 7100660b5afSChris Mason 7116ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 71252c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 713c7e54b51SJosef Bacik if (ret) { 714c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 715c7e54b51SJosef Bacik goto fail; 716c7e54b51SJosef Bacik } 71752c26179SYan Zheng 7186025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, 7194a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 720c7e54b51SJosef Bacik if (ret) { 721c7e54b51SJosef Bacik btrfs_abort_transaction(trans, ret); 722c7e54b51SJosef Bacik goto fail; 723c7e54b51SJosef Bacik } 7240660b5afSChris Mason 725cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, root_item->uuid, 7266bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 727dd5f9615SStefan Behrens if (ret) 72866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 729dd5f9615SStefan Behrens 730f46b5a66SChristoph Hellwig fail: 73149a3c4d9SDavid Sterba kfree(root_item); 732d5c12070SMiao Xie trans->block_rsv = NULL; 733d5c12070SMiao Xie trans->bytes_reserved = 0; 7347775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 735de6e8200SLiu Bo 73672fd032eSSage Weil if (async_transid) { 73772fd032eSSage Weil *async_transid = trans->transid; 7383a45bb20SJeff Mahoney err = btrfs_commit_transaction_async(trans, 1); 73900d71c9cSMiao Xie if (err) 7403a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 74172fd032eSSage Weil } else { 7423a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 74372fd032eSSage Weil } 744f46b5a66SChristoph Hellwig if (err && !ret) 745f46b5a66SChristoph Hellwig ret = err; 7461a65e24bSChris Mason 7475662344bSTsutomu Itoh if (!ret) { 7485662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 749de6e8200SLiu Bo if (IS_ERR(inode)) 750de6e8200SLiu Bo return PTR_ERR(inode); 7515662344bSTsutomu Itoh d_instantiate(dentry, inode); 7525662344bSTsutomu Itoh } 753f46b5a66SChristoph Hellwig return ret; 75449a3c4d9SDavid Sterba 75549a3c4d9SDavid Sterba fail_free: 75649a3c4d9SDavid Sterba kfree(root_item); 75749a3c4d9SDavid Sterba return ret; 758f46b5a66SChristoph Hellwig } 759f46b5a66SChristoph Hellwig 760e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 76161d7e4cbSDavid Sterba struct dentry *dentry, 762e9662f70SMiao Xie u64 *async_transid, bool readonly, 763e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 764f46b5a66SChristoph Hellwig { 7650b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 7662e4bfab9SYan, Zheng struct inode *inode; 767f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 768f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 7692e4bfab9SYan, Zheng int ret; 7708ecebf4dSRobbie Ko bool snapshot_force_cow = false; 771f46b5a66SChristoph Hellwig 77227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 773f46b5a66SChristoph Hellwig return -EINVAL; 774f46b5a66SChristoph Hellwig 775eede2bf3SOmar Sandoval if (atomic_read(&root->nr_swapfiles)) { 776eede2bf3SOmar Sandoval btrfs_warn(fs_info, 777eede2bf3SOmar Sandoval "cannot snapshot subvolume with active swapfile"); 778eede2bf3SOmar Sandoval return -ETXTBSY; 779eede2bf3SOmar Sandoval } 780eede2bf3SOmar Sandoval 78123269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 782a1ee7362SDavid Sterba if (!pending_snapshot) 783a1ee7362SDavid Sterba return -ENOMEM; 784a1ee7362SDavid Sterba 785b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 78623269bf5SDavid Sterba GFP_KERNEL); 7878546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 7888546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 789b0c0ea63SDavid Sterba ret = -ENOMEM; 790b0c0ea63SDavid Sterba goto free_pending; 791b0c0ea63SDavid Sterba } 792b0c0ea63SDavid Sterba 7938ecebf4dSRobbie Ko /* 7948ecebf4dSRobbie Ko * Force new buffered writes to reserve space even when NOCOW is 7958ecebf4dSRobbie Ko * possible. This is to avoid later writeback (running dealloc) to 7968ecebf4dSRobbie Ko * fallback to COW mode and unexpectedly fail with ENOSPC. 7978ecebf4dSRobbie Ko */ 798ea14b57fSDavid Sterba atomic_inc(&root->will_be_snapshotted); 7994e857c58SPeter Zijlstra smp_mb__after_atomic(); 80045bac0f3SLiu Bo /* wait for no snapshot writes */ 80145bac0f3SLiu Bo wait_event(root->subv_writers->wait, 80245bac0f3SLiu Bo percpu_counter_sum(&root->subv_writers->counter) == 0); 8038257b2dcSMiao Xie 8043cd24c69SEthan Lien ret = btrfs_start_delalloc_snapshot(root); 8056a03843dSMiao Xie if (ret) 806a1ee7362SDavid Sterba goto dec_and_free; 8076a03843dSMiao Xie 8088ecebf4dSRobbie Ko /* 8098ecebf4dSRobbie Ko * All previous writes have started writeback in NOCOW mode, so now 8108ecebf4dSRobbie Ko * we force future writes to fallback to COW mode during snapshot 8118ecebf4dSRobbie Ko * creation. 8128ecebf4dSRobbie Ko */ 8138ecebf4dSRobbie Ko atomic_inc(&root->snapshot_force_cow); 8148ecebf4dSRobbie Ko snapshot_force_cow = true; 8158ecebf4dSRobbie Ko 8166374e57aSChris Mason btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 8176a03843dSMiao Xie 81866d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 81966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 820d5c12070SMiao Xie /* 821d5c12070SMiao Xie * 1 - parent dir inode 822d5c12070SMiao Xie * 2 - dir entries 823d5c12070SMiao Xie * 1 - root item 824d5c12070SMiao Xie * 2 - root ref/backref 825d5c12070SMiao Xie * 1 - root of snapshot 826dd5f9615SStefan Behrens * 1 - UUID item 827d5c12070SMiao Xie */ 828d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 829dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 830ee3441b4SJeff Mahoney false); 831d5c12070SMiao Xie if (ret) 832a1ee7362SDavid Sterba goto dec_and_free; 833d5c12070SMiao Xie 834a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 835a22285a6SYan, Zheng pending_snapshot->root = root; 836b83cc969SLi Zefan pending_snapshot->readonly = readonly; 837e9662f70SMiao Xie pending_snapshot->dir = dir; 8388696c533SMiao Xie pending_snapshot->inherit = inherit; 839a22285a6SYan, Zheng 840d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 841a22285a6SYan, Zheng if (IS_ERR(trans)) { 842a22285a6SYan, Zheng ret = PTR_ERR(trans); 843a22285a6SYan, Zheng goto fail; 844a22285a6SYan, Zheng } 845a22285a6SYan, Zheng 8460b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 847a22285a6SYan, Zheng list_add(&pending_snapshot->list, 848a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 8490b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 85072fd032eSSage Weil if (async_transid) { 85172fd032eSSage Weil *async_transid = trans->transid; 8523a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 1); 85300d71c9cSMiao Xie if (ret) 8543a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 85572fd032eSSage Weil } else { 8563a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 85772fd032eSSage Weil } 858aec8030aSMiao Xie if (ret) 859c37b2b62SJosef Bacik goto fail; 860a22285a6SYan, Zheng 861a22285a6SYan, Zheng ret = pending_snapshot->error; 862f46b5a66SChristoph Hellwig if (ret) 8632e4bfab9SYan, Zheng goto fail; 864f46b5a66SChristoph Hellwig 865d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 866d3797308SChris Mason if (ret) 867d3797308SChris Mason goto fail; 868d3797308SChris Mason 8692b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 8702e4bfab9SYan, Zheng if (IS_ERR(inode)) { 8712e4bfab9SYan, Zheng ret = PTR_ERR(inode); 8722e4bfab9SYan, Zheng goto fail; 8732e4bfab9SYan, Zheng } 8745662344bSTsutomu Itoh 8752e4bfab9SYan, Zheng d_instantiate(dentry, inode); 8762e4bfab9SYan, Zheng ret = 0; 8772e4bfab9SYan, Zheng fail: 8787775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); 879a1ee7362SDavid Sterba dec_and_free: 8808ecebf4dSRobbie Ko if (snapshot_force_cow) 8818ecebf4dSRobbie Ko atomic_dec(&root->snapshot_force_cow); 882ea14b57fSDavid Sterba if (atomic_dec_and_test(&root->will_be_snapshotted)) 8834625956aSPeter Zijlstra wake_up_var(&root->will_be_snapshotted); 884b0c0ea63SDavid Sterba free_pending: 885b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 8868546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 887a1ee7362SDavid Sterba kfree(pending_snapshot); 888a1ee7362SDavid Sterba 889f46b5a66SChristoph Hellwig return ret; 890f46b5a66SChristoph Hellwig } 891f46b5a66SChristoph Hellwig 8924260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 8934260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 8944260f7c7SSage Weil * whether the type of victim is right. 8954260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 8964260f7c7SSage Weil * 2. We should have write and exec permissions on dir 8974260f7c7SSage Weil * 3. We can't remove anything from append-only dir 8984260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 8994260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 9004260f7c7SSage Weil * a. be owner of dir, or 9014260f7c7SSage Weil * b. be owner of victim, or 9024260f7c7SSage Weil * c. have CAP_FOWNER capability 90301327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 9044260f7c7SSage Weil * links pointing to it. 9054260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 9064260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 9074260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 9084260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 9094260f7c7SSage Weil * nfs_async_unlink(). 9104260f7c7SSage Weil */ 9114260f7c7SSage Weil 9124260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 9134260f7c7SSage Weil { 9144260f7c7SSage Weil int error; 9154260f7c7SSage Weil 9162b0143b5SDavid Howells if (d_really_is_negative(victim)) 9174260f7c7SSage Weil return -ENOENT; 9184260f7c7SSage Weil 9192b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 9204fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 9214260f7c7SSage Weil 9224260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 9234260f7c7SSage Weil if (error) 9244260f7c7SSage Weil return error; 9254260f7c7SSage Weil if (IS_APPEND(dir)) 9264260f7c7SSage Weil return -EPERM; 9272b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 9282b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 9294260f7c7SSage Weil return -EPERM; 9304260f7c7SSage Weil if (isdir) { 931e36cb0b8SDavid Howells if (!d_is_dir(victim)) 9324260f7c7SSage Weil return -ENOTDIR; 9334260f7c7SSage Weil if (IS_ROOT(victim)) 9344260f7c7SSage Weil return -EBUSY; 935e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 9364260f7c7SSage Weil return -EISDIR; 9374260f7c7SSage Weil if (IS_DEADDIR(dir)) 9384260f7c7SSage Weil return -ENOENT; 9394260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 9404260f7c7SSage Weil return -EBUSY; 9414260f7c7SSage Weil return 0; 9424260f7c7SSage Weil } 9434260f7c7SSage Weil 944cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 945cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 946cb8e7090SChristoph Hellwig { 9472b0143b5SDavid Howells if (d_really_is_positive(child)) 948cb8e7090SChristoph Hellwig return -EEXIST; 949cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 950cb8e7090SChristoph Hellwig return -ENOENT; 951cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 952cb8e7090SChristoph Hellwig } 953cb8e7090SChristoph Hellwig 954cb8e7090SChristoph Hellwig /* 955cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 956cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 957cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 958cb8e7090SChristoph Hellwig */ 95992872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 96052f75f4fSDavid Sterba const char *name, int namelen, 96172fd032eSSage Weil struct btrfs_root *snap_src, 9626f72c7e2SArne Jansen u64 *async_transid, bool readonly, 9638696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 964cb8e7090SChristoph Hellwig { 9652b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 9660b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 967cb8e7090SChristoph Hellwig struct dentry *dentry; 968cb8e7090SChristoph Hellwig int error; 969cb8e7090SChristoph Hellwig 97000235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 97100235411SAl Viro if (error == -EINTR) 97200235411SAl Viro return error; 973cb8e7090SChristoph Hellwig 974cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 975cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 976cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 977cb8e7090SChristoph Hellwig goto out_unlock; 978cb8e7090SChristoph Hellwig 97976dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 980cb8e7090SChristoph Hellwig if (error) 981a874a63eSLiu Bo goto out_dput; 982cb8e7090SChristoph Hellwig 9839c52057cSChris Mason /* 9849c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 9859c52057cSChris Mason * check for them now when we can safely fail 9869c52057cSChris Mason */ 9879c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 9889c52057cSChris Mason dir->i_ino, name, 9899c52057cSChris Mason namelen); 9909c52057cSChris Mason if (error) 9919c52057cSChris Mason goto out_dput; 9929c52057cSChris Mason 9930b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 99476dda93cSYan, Zheng 99576dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 99676dda93cSYan, Zheng goto out_up_read; 99776dda93cSYan, Zheng 9983de4586cSChris Mason if (snap_src) { 99961d7e4cbSDavid Sterba error = create_snapshot(snap_src, dir, dentry, 10006f72c7e2SArne Jansen async_transid, readonly, inherit); 10013de4586cSChris Mason } else { 1002d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 1003d5c12070SMiao Xie async_transid, inherit); 10043de4586cSChris Mason } 100576dda93cSYan, Zheng if (!error) 100676dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 100776dda93cSYan, Zheng out_up_read: 10080b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 1009cb8e7090SChristoph Hellwig out_dput: 1010cb8e7090SChristoph Hellwig dput(dentry); 1011cb8e7090SChristoph Hellwig out_unlock: 10125955102cSAl Viro inode_unlock(dir); 1013cb8e7090SChristoph Hellwig return error; 1014cb8e7090SChristoph Hellwig } 1015cb8e7090SChristoph Hellwig 10164cb5300bSChris Mason /* 10174cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 10184cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 10194cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 10204cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 10214cb5300bSChris Mason * part of the file 10224cb5300bSChris Mason */ 1023aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 10244cb5300bSChris Mason { 10254cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 10264cb5300bSChris Mason struct extent_map *em = NULL; 10274cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 10284cb5300bSChris Mason u64 end; 10294cb5300bSChris Mason 10304cb5300bSChris Mason read_lock(&em_tree->lock); 103109cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 10324cb5300bSChris Mason read_unlock(&em_tree->lock); 10334cb5300bSChris Mason 10344cb5300bSChris Mason if (em) { 10354cb5300bSChris Mason end = extent_map_end(em); 10364cb5300bSChris Mason free_extent_map(em); 10374cb5300bSChris Mason if (end - offset > thresh) 10384cb5300bSChris Mason return 0; 10394cb5300bSChris Mason } 10404cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 10414cb5300bSChris Mason thresh /= 2; 10424cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 10434cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 10444cb5300bSChris Mason if (end >= thresh) 10454cb5300bSChris Mason return 0; 10464cb5300bSChris Mason return 1; 10474cb5300bSChris Mason } 10484cb5300bSChris Mason 10494cb5300bSChris Mason /* 10504cb5300bSChris Mason * helper function to walk through a file and find extents 10514cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 10524cb5300bSChris Mason * 10534cb5300bSChris Mason * This is used by the defragging code to find new and small 10544cb5300bSChris Mason * extents 10554cb5300bSChris Mason */ 10564cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 10574cb5300bSChris Mason struct inode *inode, u64 newer_than, 1058aab110abSDavid Sterba u64 *off, u32 thresh) 10594cb5300bSChris Mason { 10604cb5300bSChris Mason struct btrfs_path *path; 10614cb5300bSChris Mason struct btrfs_key min_key; 10624cb5300bSChris Mason struct extent_buffer *leaf; 10634cb5300bSChris Mason struct btrfs_file_extent_item *extent; 10644cb5300bSChris Mason int type; 10654cb5300bSChris Mason int ret; 10664a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode)); 10674cb5300bSChris Mason 10684cb5300bSChris Mason path = btrfs_alloc_path(); 10694cb5300bSChris Mason if (!path) 10704cb5300bSChris Mason return -ENOMEM; 10714cb5300bSChris Mason 1072a4689d2bSDavid Sterba min_key.objectid = ino; 10734cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 10744cb5300bSChris Mason min_key.offset = *off; 10754cb5300bSChris Mason 10764cb5300bSChris Mason while (1) { 10776174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 10784cb5300bSChris Mason if (ret != 0) 10794cb5300bSChris Mason goto none; 1080f094c9bdSFilipe Manana process_slot: 1081a4689d2bSDavid Sterba if (min_key.objectid != ino) 10824cb5300bSChris Mason goto none; 10834cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 10844cb5300bSChris Mason goto none; 10854cb5300bSChris Mason 10864cb5300bSChris Mason leaf = path->nodes[0]; 10874cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 10884cb5300bSChris Mason struct btrfs_file_extent_item); 10894cb5300bSChris Mason 10904cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 10914cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 10924cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 10934cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 10944cb5300bSChris Mason *off = min_key.offset; 10954cb5300bSChris Mason btrfs_free_path(path); 10964cb5300bSChris Mason return 0; 10974cb5300bSChris Mason } 10984cb5300bSChris Mason 1099f094c9bdSFilipe Manana path->slots[0]++; 1100f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 1101f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 1102f094c9bdSFilipe Manana goto process_slot; 1103f094c9bdSFilipe Manana } 1104f094c9bdSFilipe Manana 11054cb5300bSChris Mason if (min_key.offset == (u64)-1) 11064cb5300bSChris Mason goto none; 11074cb5300bSChris Mason 11084cb5300bSChris Mason min_key.offset++; 11094cb5300bSChris Mason btrfs_release_path(path); 11104cb5300bSChris Mason } 11114cb5300bSChris Mason none: 11124cb5300bSChris Mason btrfs_free_path(path); 11134cb5300bSChris Mason return -ENOENT; 11144cb5300bSChris Mason } 11154cb5300bSChris Mason 11166c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 111717ce6ef8SLiu Bo { 111817ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1119940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 11206c282eb4SLi Zefan struct extent_map *em; 112109cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 1122940100a4SChris Mason 1123940100a4SChris Mason /* 1124940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1125940100a4SChris Mason * the full extent lock 1126940100a4SChris Mason */ 1127940100a4SChris Mason read_lock(&em_tree->lock); 1128940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 1129940100a4SChris Mason read_unlock(&em_tree->lock); 1130940100a4SChris Mason 1131940100a4SChris Mason if (!em) { 1132308d9800SFilipe Manana struct extent_state *cached = NULL; 1133308d9800SFilipe Manana u64 end = start + len - 1; 1134308d9800SFilipe Manana 1135940100a4SChris Mason /* get the big lock and read metadata off disk */ 1136ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 113739b07b5dSOmar Sandoval em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len); 1138e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 1139940100a4SChris Mason 11406cf8bfbfSDan Carpenter if (IS_ERR(em)) 11416c282eb4SLi Zefan return NULL; 1142940100a4SChris Mason } 1143940100a4SChris Mason 11446c282eb4SLi Zefan return em; 11456c282eb4SLi Zefan } 11466c282eb4SLi Zefan 11476c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 11486c282eb4SLi Zefan { 11496c282eb4SLi Zefan struct extent_map *next; 11506c282eb4SLi Zefan bool ret = true; 11516c282eb4SLi Zefan 11526c282eb4SLi Zefan /* this is the last extent */ 11536c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 11546c282eb4SLi Zefan return false; 11556c282eb4SLi Zefan 11566c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1157e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1158e9512d72SChris Mason ret = false; 1159e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1160ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 11616c282eb4SLi Zefan ret = false; 11626c282eb4SLi Zefan 11636c282eb4SLi Zefan free_extent_map(next); 11646c282eb4SLi Zefan return ret; 11656c282eb4SLi Zefan } 11666c282eb4SLi Zefan 1167aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1168a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1169a43a2111SAndrew Mahone int compress) 11706c282eb4SLi Zefan { 11716c282eb4SLi Zefan struct extent_map *em; 11726c282eb4SLi Zefan int ret = 1; 11736c282eb4SLi Zefan bool next_mergeable = true; 11744a3560c4SLiu Bo bool prev_mergeable = true; 11756c282eb4SLi Zefan 11766c282eb4SLi Zefan /* 11776c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 11786c282eb4SLi Zefan * defragging it 11796c282eb4SLi Zefan */ 11806c282eb4SLi Zefan if (start < *defrag_end) 11816c282eb4SLi Zefan return 1; 11826c282eb4SLi Zefan 11836c282eb4SLi Zefan *skip = 0; 11846c282eb4SLi Zefan 11856c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 11866c282eb4SLi Zefan if (!em) 11876c282eb4SLi Zefan return 0; 11886c282eb4SLi Zefan 1189940100a4SChris Mason /* this will cover holes, and inline extents */ 119017ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1191940100a4SChris Mason ret = 0; 119217ce6ef8SLiu Bo goto out; 119317ce6ef8SLiu Bo } 119417ce6ef8SLiu Bo 11954a3560c4SLiu Bo if (!*defrag_end) 11964a3560c4SLiu Bo prev_mergeable = false; 11974a3560c4SLiu Bo 11986c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1199940100a4SChris Mason /* 12006c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 12016c282eb4SLi Zefan * real extent, don't bother defragging it 1202940100a4SChris Mason */ 1203a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 12044a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1205940100a4SChris Mason ret = 0; 120617ce6ef8SLiu Bo out: 1207940100a4SChris Mason /* 1208940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1209940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1210940100a4SChris Mason * so that the next tiny extent will force a defrag. 1211940100a4SChris Mason * 1212940100a4SChris Mason * The end result of this is that tiny extents before a single big 1213940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1214940100a4SChris Mason */ 1215940100a4SChris Mason if (ret) { 1216940100a4SChris Mason *defrag_end = extent_map_end(em); 1217940100a4SChris Mason } else { 1218940100a4SChris Mason *last_len = 0; 1219940100a4SChris Mason *skip = extent_map_end(em); 1220940100a4SChris Mason *defrag_end = 0; 1221940100a4SChris Mason } 1222940100a4SChris Mason 1223940100a4SChris Mason free_extent_map(em); 1224940100a4SChris Mason return ret; 1225940100a4SChris Mason } 1226940100a4SChris Mason 12274cb5300bSChris Mason /* 12284cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 12294cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 12304cb5300bSChris Mason * to COW and defrag. 12314cb5300bSChris Mason * 12324cb5300bSChris Mason * It also makes sure the delalloc code has enough 12334cb5300bSChris Mason * dirty data to avoid making new small extents as part 12344cb5300bSChris Mason * of the defrag 12354cb5300bSChris Mason * 12364cb5300bSChris Mason * It's a good idea to start RA on this range 12374cb5300bSChris Mason * before calling this. 12384cb5300bSChris Mason */ 12394cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 12404cb5300bSChris Mason struct page **pages, 12414cb5300bSChris Mason unsigned long start_index, 1242c41570c9SJustin Maggard unsigned long num_pages) 1243f46b5a66SChristoph Hellwig { 12444cb5300bSChris Mason unsigned long file_end; 12454cb5300bSChris Mason u64 isize = i_size_read(inode); 1246f46b5a66SChristoph Hellwig u64 page_start; 1247f46b5a66SChristoph Hellwig u64 page_end; 12481f12bd06SLiu Bo u64 page_cnt; 12494cb5300bSChris Mason int ret; 12504cb5300bSChris Mason int i; 12514cb5300bSChris Mason int i_done; 12524cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 12534cb5300bSChris Mason struct extent_state *cached_state = NULL; 1254600a45e1SMiao Xie struct extent_io_tree *tree; 1255364ecf36SQu Wenruo struct extent_changeset *data_reserved = NULL; 12563b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 12574cb5300bSChris Mason 125809cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 12591f12bd06SLiu Bo if (!isize || start_index > file_end) 12601f12bd06SLiu Bo return 0; 12611f12bd06SLiu Bo 12621f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 12634cb5300bSChris Mason 1264364ecf36SQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 126509cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 126609cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12674cb5300bSChris Mason if (ret) 12684cb5300bSChris Mason return ret; 12694cb5300bSChris Mason i_done = 0; 1270600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 12714cb5300bSChris Mason 12724cb5300bSChris Mason /* step one, lock all the pages */ 12731f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 12744cb5300bSChris Mason struct page *page; 1275600a45e1SMiao Xie again: 1276a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 12773b16a4e3SJosef Bacik start_index + i, mask); 12784cb5300bSChris Mason if (!page) 12794cb5300bSChris Mason break; 12804cb5300bSChris Mason 1281600a45e1SMiao Xie page_start = page_offset(page); 128209cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1283600a45e1SMiao Xie while (1) { 1284308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1285ff13db41SDavid Sterba &cached_state); 1286600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1287600a45e1SMiao Xie page_start); 1288308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1289e43bbe5eSDavid Sterba &cached_state); 1290600a45e1SMiao Xie if (!ordered) 1291600a45e1SMiao Xie break; 1292600a45e1SMiao Xie 1293600a45e1SMiao Xie unlock_page(page); 1294600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1295600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1296600a45e1SMiao Xie lock_page(page); 12971f12bd06SLiu Bo /* 12981f12bd06SLiu Bo * we unlocked the page above, so we need check if 12991f12bd06SLiu Bo * it was released or not. 13001f12bd06SLiu Bo */ 13011f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 13021f12bd06SLiu Bo unlock_page(page); 130309cbfeafSKirill A. Shutemov put_page(page); 13041f12bd06SLiu Bo goto again; 13051f12bd06SLiu Bo } 1306600a45e1SMiao Xie } 1307600a45e1SMiao Xie 13084cb5300bSChris Mason if (!PageUptodate(page)) { 13094cb5300bSChris Mason btrfs_readpage(NULL, page); 13104cb5300bSChris Mason lock_page(page); 13114cb5300bSChris Mason if (!PageUptodate(page)) { 13124cb5300bSChris Mason unlock_page(page); 131309cbfeafSKirill A. Shutemov put_page(page); 13144cb5300bSChris Mason ret = -EIO; 13154cb5300bSChris Mason break; 13164cb5300bSChris Mason } 13174cb5300bSChris Mason } 1318600a45e1SMiao Xie 1319600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1320600a45e1SMiao Xie unlock_page(page); 132109cbfeafSKirill A. Shutemov put_page(page); 1322600a45e1SMiao Xie goto again; 1323600a45e1SMiao Xie } 1324600a45e1SMiao Xie 13254cb5300bSChris Mason pages[i] = page; 13264cb5300bSChris Mason i_done++; 13274cb5300bSChris Mason } 13284cb5300bSChris Mason if (!i_done || ret) 13294cb5300bSChris Mason goto out; 13304cb5300bSChris Mason 13311751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 13324cb5300bSChris Mason goto out; 13334cb5300bSChris Mason 13344cb5300bSChris Mason /* 13354cb5300bSChris Mason * so now we have a nice long stream of locked 13364cb5300bSChris Mason * and up to date pages, lets wait on them 13374cb5300bSChris Mason */ 13384cb5300bSChris Mason for (i = 0; i < i_done; i++) 13394cb5300bSChris Mason wait_on_page_writeback(pages[i]); 13404cb5300bSChris Mason 13414cb5300bSChris Mason page_start = page_offset(pages[0]); 134209cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 13434cb5300bSChris Mason 13444cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1345ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 13464cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 1347e182163dSOmar Sandoval page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | 1348e182163dSOmar Sandoval EXTENT_DEFRAG, 0, 0, &cached_state); 13494cb5300bSChris Mason 13501f12bd06SLiu Bo if (i_done != page_cnt) { 13519e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 135228c4a3e2SSu Yue btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); 13539e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 1354bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 135509cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 135643b18595SQu Wenruo (page_cnt - i_done) << PAGE_SHIFT, true); 13574cb5300bSChris Mason } 13584cb5300bSChris Mason 13594cb5300bSChris Mason 13609e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 1361018ed4f7SDavid Sterba &cached_state); 13624cb5300bSChris Mason 13634cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 1364e43bbe5eSDavid Sterba page_start, page_end - 1, &cached_state); 13654cb5300bSChris Mason 13664cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13674cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 13684cb5300bSChris Mason ClearPageChecked(pages[i]); 13694cb5300bSChris Mason set_page_extent_mapped(pages[i]); 13704cb5300bSChris Mason set_page_dirty(pages[i]); 13714cb5300bSChris Mason unlock_page(pages[i]); 137209cbfeafSKirill A. Shutemov put_page(pages[i]); 13734cb5300bSChris Mason } 13748702ba93SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1375364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13764cb5300bSChris Mason return i_done; 13774cb5300bSChris Mason out: 13784cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13794cb5300bSChris Mason unlock_page(pages[i]); 138009cbfeafSKirill A. Shutemov put_page(pages[i]); 13814cb5300bSChris Mason } 1382bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 138309cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 138443b18595SQu Wenruo page_cnt << PAGE_SHIFT, true); 13858702ba93SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1386364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13874cb5300bSChris Mason return ret; 13884cb5300bSChris Mason 13894cb5300bSChris Mason } 13904cb5300bSChris Mason 13914cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 13924cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 13934cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 13944cb5300bSChris Mason { 13950b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 13964cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 13974cb5300bSChris Mason struct file_ra_state *ra = NULL; 13984cb5300bSChris Mason unsigned long last_index; 1399151a31b2SLi Zefan u64 isize = i_size_read(inode); 1400940100a4SChris Mason u64 last_len = 0; 1401940100a4SChris Mason u64 skip = 0; 1402940100a4SChris Mason u64 defrag_end = 0; 14034cb5300bSChris Mason u64 newer_off = range->start; 1404f46b5a66SChristoph Hellwig unsigned long i; 1405008873eaSLi Zefan unsigned long ra_index = 0; 1406f46b5a66SChristoph Hellwig int ret; 14074cb5300bSChris Mason int defrag_count = 0; 14081a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1409aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 141009cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1411c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1412ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 14134cb5300bSChris Mason struct page **pages = NULL; 14141e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 14154cb5300bSChris Mason 14160abd5b17SLiu Bo if (isize == 0) 14170abd5b17SLiu Bo return 0; 14180abd5b17SLiu Bo 14190abd5b17SLiu Bo if (range->start >= isize) 14200abd5b17SLiu Bo return -EINVAL; 14211a419d85SLi Zefan 14221e2ef46dSDavid Sterba if (do_compress) { 1423ce96b7ffSChengguang Xu if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) 14241a419d85SLi Zefan return -EINVAL; 14251a419d85SLi Zefan if (range->compress_type) 14261a419d85SLi Zefan compress_type = range->compress_type; 14271a419d85SLi Zefan } 1428f46b5a66SChristoph Hellwig 14290abd5b17SLiu Bo if (extent_thresh == 0) 1430ee22184bSByongho Lee extent_thresh = SZ_256K; 1431f46b5a66SChristoph Hellwig 14324cb5300bSChris Mason /* 14330a52d108SDavid Sterba * If we were not given a file, allocate a readahead context. As 14340a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 14350a52d108SDavid Sterba * we don't error out. 14364cb5300bSChris Mason */ 14374cb5300bSChris Mason if (!file) { 143863e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 14390a52d108SDavid Sterba if (ra) 14404cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 14414cb5300bSChris Mason } else { 14424cb5300bSChris Mason ra = &file->f_ra; 14434cb5300bSChris Mason } 14444cb5300bSChris Mason 144563e727ecSDavid Sterba pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); 14464cb5300bSChris Mason if (!pages) { 14474cb5300bSChris Mason ret = -ENOMEM; 14484cb5300bSChris Mason goto out_ra; 14494cb5300bSChris Mason } 14504cb5300bSChris Mason 14514cb5300bSChris Mason /* find the last page to defrag */ 14521e701a32SChris Mason if (range->start + range->len > range->start) { 1453151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 145409cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 14551e701a32SChris Mason } else { 145609cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 14571e701a32SChris Mason } 14581e701a32SChris Mason 14594cb5300bSChris Mason if (newer_than) { 14604cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1461ee22184bSByongho Lee &newer_off, SZ_64K); 14624cb5300bSChris Mason if (!ret) { 14634cb5300bSChris Mason range->start = newer_off; 14644cb5300bSChris Mason /* 14654cb5300bSChris Mason * we always align our defrag to help keep 14664cb5300bSChris Mason * the extents in the file evenly spaced 14674cb5300bSChris Mason */ 146809cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14694cb5300bSChris Mason } else 14704cb5300bSChris Mason goto out_ra; 14714cb5300bSChris Mason } else { 147209cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 14734cb5300bSChris Mason } 14744cb5300bSChris Mason if (!max_to_defrag) 1475070034bdSchandan max_to_defrag = last_index - i + 1; 14764cb5300bSChris Mason 14772a0f7f57SLi Zefan /* 14782a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 14792a0f7f57SLi Zefan * written sequentially. 14802a0f7f57SLi Zefan */ 14812a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 14822a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 14832a0f7f57SLi Zefan 1484f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 148509cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 14864cb5300bSChris Mason /* 14874cb5300bSChris Mason * make sure we stop running if someone unmounts 14884cb5300bSChris Mason * the FS 14894cb5300bSChris Mason */ 14901751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 14914cb5300bSChris Mason break; 14924cb5300bSChris Mason 14930b246afaSJeff Mahoney if (btrfs_defrag_cancelled(fs_info)) { 14940b246afaSJeff Mahoney btrfs_debug(fs_info, "defrag_file cancelled"); 1495210549ebSDavid Sterba ret = -EAGAIN; 1496210549ebSDavid Sterba break; 1497210549ebSDavid Sterba } 1498210549ebSDavid Sterba 149909cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 15006c282eb4SLi Zefan extent_thresh, &last_len, &skip, 15011e2ef46dSDavid Sterba &defrag_end, do_compress)){ 1502940100a4SChris Mason unsigned long next; 1503940100a4SChris Mason /* 1504940100a4SChris Mason * the should_defrag function tells us how much to skip 1505940100a4SChris Mason * bump our counter by the suggested amount 1506940100a4SChris Mason */ 150709cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1508940100a4SChris Mason i = max(i + 1, next); 1509940100a4SChris Mason continue; 1510940100a4SChris Mason } 1511008873eaSLi Zefan 1512008873eaSLi Zefan if (!newer_than) { 151309cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 151409cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1515008873eaSLi Zefan cluster = min(cluster, max_cluster); 1516008873eaSLi Zefan } else { 1517008873eaSLi Zefan cluster = max_cluster; 1518008873eaSLi Zefan } 1519008873eaSLi Zefan 1520008873eaSLi Zefan if (i + cluster > ra_index) { 1521008873eaSLi Zefan ra_index = max(i, ra_index); 15220a52d108SDavid Sterba if (ra) 1523d3c0bab5SDavid Sterba page_cache_sync_readahead(inode->i_mapping, ra, 1524d3c0bab5SDavid Sterba file, ra_index, cluster); 1525e4826a5bSchandan ra_index += cluster; 1526008873eaSLi Zefan } 15274cb5300bSChris Mason 15285955102cSAl Viro inode_lock(inode); 1529eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 1530eede2bf3SOmar Sandoval ret = -ETXTBSY; 1531eede2bf3SOmar Sandoval } else { 15321e2ef46dSDavid Sterba if (do_compress) 1533eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 1534008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1535eede2bf3SOmar Sandoval } 1536ecb8bea8SLiu Bo if (ret < 0) { 15375955102cSAl Viro inode_unlock(inode); 15384cb5300bSChris Mason goto out_ra; 1539ecb8bea8SLiu Bo } 15404cb5300bSChris Mason 15414cb5300bSChris Mason defrag_count += ret; 1542d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 15435955102cSAl Viro inode_unlock(inode); 15444cb5300bSChris Mason 15454cb5300bSChris Mason if (newer_than) { 15464cb5300bSChris Mason if (newer_off == (u64)-1) 15474cb5300bSChris Mason break; 15484cb5300bSChris Mason 1549e1f041e1SLiu Bo if (ret > 0) 1550e1f041e1SLiu Bo i += ret; 1551e1f041e1SLiu Bo 15524cb5300bSChris Mason newer_off = max(newer_off + 1, 155309cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 15544cb5300bSChris Mason 1555ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1556ee22184bSByongho Lee &newer_off, SZ_64K); 15574cb5300bSChris Mason if (!ret) { 15584cb5300bSChris Mason range->start = newer_off; 155909cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 15604cb5300bSChris Mason } else { 15614cb5300bSChris Mason break; 1562940100a4SChris Mason } 15634cb5300bSChris Mason } else { 1564008873eaSLi Zefan if (ret > 0) { 1565cbcc8326SLi Zefan i += ret; 156609cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1567008873eaSLi Zefan } else { 1568940100a4SChris Mason i++; 1569008873eaSLi Zefan last_len = 0; 1570008873eaSLi Zefan } 1571f46b5a66SChristoph Hellwig } 15724cb5300bSChris Mason } 1573f46b5a66SChristoph Hellwig 1574dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 15751e701a32SChris Mason filemap_flush(inode->i_mapping); 1576dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1577dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1578dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1579dec8ef90SFilipe Manana } 15801e701a32SChris Mason 15811a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 15820b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 15835c1aab1dSNick Terrell } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { 15845c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 15851a419d85SLi Zefan } 15861a419d85SLi Zefan 158760ccf82fSDiego Calleja ret = defrag_count; 1588940100a4SChris Mason 15894cb5300bSChris Mason out_ra: 15901e2ef46dSDavid Sterba if (do_compress) { 15915955102cSAl Viro inode_lock(inode); 1592eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 15935955102cSAl Viro inode_unlock(inode); 1594633085c7SFilipe David Borba Manana } 15954cb5300bSChris Mason if (!file) 15964cb5300bSChris Mason kfree(ra); 15974cb5300bSChris Mason kfree(pages); 1598940100a4SChris Mason return ret; 1599f46b5a66SChristoph Hellwig } 1600f46b5a66SChristoph Hellwig 1601198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 160276dda93cSYan, Zheng void __user *arg) 1603f46b5a66SChristoph Hellwig { 16040b246afaSJeff Mahoney struct inode *inode = file_inode(file); 16050b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1606f46b5a66SChristoph Hellwig u64 new_size; 1607f46b5a66SChristoph Hellwig u64 old_size; 1608f46b5a66SChristoph Hellwig u64 devid = 1; 16090b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1610f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1611f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1612f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1613f46b5a66SChristoph Hellwig char *sizestr; 16149a40f122SGui Hecheng char *retptr; 1615f46b5a66SChristoph Hellwig char *devstr = NULL; 1616f46b5a66SChristoph Hellwig int ret = 0; 1617f46b5a66SChristoph Hellwig int mod = 0; 1618f46b5a66SChristoph Hellwig 1619e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1620e441d54dSChris Mason return -EPERM; 1621e441d54dSChris Mason 1622198605a8SMiao Xie ret = mnt_want_write_file(file); 1623198605a8SMiao Xie if (ret) 1624198605a8SMiao Xie return ret; 1625198605a8SMiao Xie 1626171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 162797547676SMiao Xie mnt_drop_write_file(file); 1628e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1629c9e9f97bSIlya Dryomov } 1630c9e9f97bSIlya Dryomov 1631dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1632c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1633c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1634c9e9f97bSIlya Dryomov goto out; 1635c9e9f97bSIlya Dryomov } 16365516e595SMark Fasheh 16375516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1638f46b5a66SChristoph Hellwig 1639f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1640f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1641f46b5a66SChristoph Hellwig if (devstr) { 1642f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1643f46b5a66SChristoph Hellwig *devstr = '\0'; 1644f46b5a66SChristoph Hellwig devstr = vol_args->name; 164558dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 164658dfae63SZhangZhen if (ret) 164758dfae63SZhangZhen goto out_free; 1648dfd79829SMiao Xie if (!devid) { 1649dfd79829SMiao Xie ret = -EINVAL; 1650dfd79829SMiao Xie goto out_free; 1651dfd79829SMiao Xie } 16520b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1653f46b5a66SChristoph Hellwig } 1654dba60f3fSMiao Xie 165509ba3bc9SAnand Jain device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); 1656f46b5a66SChristoph Hellwig if (!device) { 16570b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1658c1c9ff7cSGeert Uytterhoeven devid); 1659dfd79829SMiao Xie ret = -ENODEV; 1660c9e9f97bSIlya Dryomov goto out_free; 1661f46b5a66SChristoph Hellwig } 1662dba60f3fSMiao Xie 1663ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 16640b246afaSJeff Mahoney btrfs_info(fs_info, 1665efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1666c1c9ff7cSGeert Uytterhoeven devid); 1667dfd79829SMiao Xie ret = -EPERM; 16684e42ae1bSLiu Bo goto out_free; 16694e42ae1bSLiu Bo } 16704e42ae1bSLiu Bo 1671f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1672f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1673f46b5a66SChristoph Hellwig else { 1674f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1675f46b5a66SChristoph Hellwig mod = -1; 1676f46b5a66SChristoph Hellwig sizestr++; 1677f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1678f46b5a66SChristoph Hellwig mod = 1; 1679f46b5a66SChristoph Hellwig sizestr++; 1680f46b5a66SChristoph Hellwig } 16819a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 16829a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1683f46b5a66SChristoph Hellwig ret = -EINVAL; 1684c9e9f97bSIlya Dryomov goto out_free; 1685f46b5a66SChristoph Hellwig } 1686f46b5a66SChristoph Hellwig } 1687f46b5a66SChristoph Hellwig 1688401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 1689dfd79829SMiao Xie ret = -EPERM; 169063a212abSStefan Behrens goto out_free; 169163a212abSStefan Behrens } 169263a212abSStefan Behrens 16937cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1694f46b5a66SChristoph Hellwig 1695f46b5a66SChristoph Hellwig if (mod < 0) { 1696f46b5a66SChristoph Hellwig if (new_size > old_size) { 1697f46b5a66SChristoph Hellwig ret = -EINVAL; 1698c9e9f97bSIlya Dryomov goto out_free; 1699f46b5a66SChristoph Hellwig } 1700f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1701f46b5a66SChristoph Hellwig } else if (mod > 0) { 1702eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1703902c68a4SGui Hecheng ret = -ERANGE; 1704eb8052e0SWenliang Fan goto out_free; 1705eb8052e0SWenliang Fan } 1706f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1707f46b5a66SChristoph Hellwig } 1708f46b5a66SChristoph Hellwig 1709ee22184bSByongho Lee if (new_size < SZ_256M) { 1710f46b5a66SChristoph Hellwig ret = -EINVAL; 1711c9e9f97bSIlya Dryomov goto out_free; 1712f46b5a66SChristoph Hellwig } 1713f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1714f46b5a66SChristoph Hellwig ret = -EFBIG; 1715c9e9f97bSIlya Dryomov goto out_free; 1716f46b5a66SChristoph Hellwig } 1717f46b5a66SChristoph Hellwig 171847f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1719f46b5a66SChristoph Hellwig 17200b246afaSJeff Mahoney btrfs_info_in_rcu(fs_info, "new size for %s is %llu", 1721c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1722f46b5a66SChristoph Hellwig 1723f46b5a66SChristoph Hellwig if (new_size > old_size) { 1724a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 172598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 172698d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1727c9e9f97bSIlya Dryomov goto out_free; 172898d5dc13STsutomu Itoh } 1729f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 17303a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1731ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1732f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 17330253f40eSjeff.liu } /* equal, nothing need to do */ 1734f46b5a66SChristoph Hellwig 1735c9e9f97bSIlya Dryomov out_free: 1736f46b5a66SChristoph Hellwig kfree(vol_args); 1737c9e9f97bSIlya Dryomov out: 1738171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 173918f39c41SIlya Dryomov mnt_drop_write_file(file); 1740f46b5a66SChristoph Hellwig return ret; 1741f46b5a66SChristoph Hellwig } 1742f46b5a66SChristoph Hellwig 174372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 174452f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 17456f72c7e2SArne Jansen u64 *transid, bool readonly, 17468696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1747f46b5a66SChristoph Hellwig { 1748f46b5a66SChristoph Hellwig int namelen; 17493de4586cSChris Mason int ret = 0; 1750f46b5a66SChristoph Hellwig 1751325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1752325c50e3SJeff Mahoney return -ENOTDIR; 1753325c50e3SJeff Mahoney 1754a874a63eSLiu Bo ret = mnt_want_write_file(file); 1755a874a63eSLiu Bo if (ret) 1756a874a63eSLiu Bo goto out; 1757a874a63eSLiu Bo 175872fd032eSSage Weil namelen = strlen(name); 175972fd032eSSage Weil if (strchr(name, '/')) { 1760f46b5a66SChristoph Hellwig ret = -EINVAL; 1761a874a63eSLiu Bo goto out_drop_write; 1762f46b5a66SChristoph Hellwig } 1763f46b5a66SChristoph Hellwig 176416780cabSChris Mason if (name[0] == '.' && 176516780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 176616780cabSChris Mason ret = -EEXIST; 1767a874a63eSLiu Bo goto out_drop_write; 176816780cabSChris Mason } 176916780cabSChris Mason 17703de4586cSChris Mason if (subvol) { 177172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 17726f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1773cb8e7090SChristoph Hellwig } else { 17742903ff01SAl Viro struct fd src = fdget(fd); 17753de4586cSChris Mason struct inode *src_inode; 17762903ff01SAl Viro if (!src.file) { 17773de4586cSChris Mason ret = -EINVAL; 1778a874a63eSLiu Bo goto out_drop_write; 17793de4586cSChris Mason } 17803de4586cSChris Mason 1781496ad9aaSAl Viro src_inode = file_inode(src.file); 1782496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1783c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1784efe120a0SFrank Holton "Snapshot src from another FS"); 178523ad5b17SKusanagi Kouichi ret = -EXDEV; 1786d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1787d0242061SDavid Sterba /* 1788d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1789d0242061SDavid Sterba * are limited to own subvolumes only 1790d0242061SDavid Sterba */ 1791d0242061SDavid Sterba ret = -EPERM; 1792ecd18815SAl Viro } else { 179372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 179472fd032eSSage Weil BTRFS_I(src_inode)->root, 17956f72c7e2SArne Jansen transid, readonly, inherit); 1796ecd18815SAl Viro } 17972903ff01SAl Viro fdput(src); 1798cb8e7090SChristoph Hellwig } 1799a874a63eSLiu Bo out_drop_write: 1800a874a63eSLiu Bo mnt_drop_write_file(file); 1801f46b5a66SChristoph Hellwig out: 180272fd032eSSage Weil return ret; 180372fd032eSSage Weil } 180472fd032eSSage Weil 180572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1806fa0d2b9bSLi Zefan void __user *arg, int subvol) 180772fd032eSSage Weil { 1808fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 180972fd032eSSage Weil int ret; 181072fd032eSSage Weil 1811325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1812325c50e3SJeff Mahoney return -ENOTDIR; 1813325c50e3SJeff Mahoney 1814fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1815fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1816fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1817fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1818fa0d2b9bSLi Zefan 1819fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1820b83cc969SLi Zefan vol_args->fd, subvol, 18216f72c7e2SArne Jansen NULL, false, NULL); 1822fa0d2b9bSLi Zefan 1823fa0d2b9bSLi Zefan kfree(vol_args); 1824fa0d2b9bSLi Zefan return ret; 1825fa0d2b9bSLi Zefan } 1826fa0d2b9bSLi Zefan 1827fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1828fa0d2b9bSLi Zefan void __user *arg, int subvol) 1829fa0d2b9bSLi Zefan { 1830fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1831fa0d2b9bSLi Zefan int ret; 1832fdfb1e4fSLi Zefan u64 transid = 0; 1833fdfb1e4fSLi Zefan u64 *ptr = NULL; 1834b83cc969SLi Zefan bool readonly = false; 18356f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 183672fd032eSSage Weil 1837325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1838325c50e3SJeff Mahoney return -ENOTDIR; 1839325c50e3SJeff Mahoney 1840fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1841fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1842fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1843fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1844fdfb1e4fSLi Zefan 1845b83cc969SLi Zefan if (vol_args->flags & 18466f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 18476f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1848b83cc969SLi Zefan ret = -EOPNOTSUPP; 1849c47ca32dSDan Carpenter goto free_args; 1850fdfb1e4fSLi Zefan } 1851fdfb1e4fSLi Zefan 1852ebc87351SNikolay Borisov if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1853ebc87351SNikolay Borisov struct inode *inode = file_inode(file); 1854ebc87351SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1855ebc87351SNikolay Borisov 1856ebc87351SNikolay Borisov btrfs_warn(fs_info, 1857ebc87351SNikolay Borisov "SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7"); 1858ebc87351SNikolay Borisov 1859fdfb1e4fSLi Zefan ptr = &transid; 1860ebc87351SNikolay Borisov } 1861b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1862b83cc969SLi Zefan readonly = true; 18636f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 186409cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 18656f72c7e2SArne Jansen ret = -EINVAL; 1866c47ca32dSDan Carpenter goto free_args; 18676f72c7e2SArne Jansen } 18686f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 18696f72c7e2SArne Jansen if (IS_ERR(inherit)) { 18706f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1871c47ca32dSDan Carpenter goto free_args; 18726f72c7e2SArne Jansen } 18736f72c7e2SArne Jansen } 187475eaa0e2SSage Weil 1875fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 18766f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 18778696c533SMiao Xie readonly, inherit); 1878c47ca32dSDan Carpenter if (ret) 1879c47ca32dSDan Carpenter goto free_inherit; 188075eaa0e2SSage Weil 1881c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1882fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1883c47ca32dSDan Carpenter transid), 1884c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 188575eaa0e2SSage Weil ret = -EFAULT; 1886c47ca32dSDan Carpenter 1887c47ca32dSDan Carpenter free_inherit: 18886f72c7e2SArne Jansen kfree(inherit); 1889c47ca32dSDan Carpenter free_args: 1890c47ca32dSDan Carpenter kfree(vol_args); 1891f46b5a66SChristoph Hellwig return ret; 1892f46b5a66SChristoph Hellwig } 1893f46b5a66SChristoph Hellwig 18940caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 18950caa102dSLi Zefan void __user *arg) 18960caa102dSLi Zefan { 1897496ad9aaSAl Viro struct inode *inode = file_inode(file); 18980b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 18990caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19000caa102dSLi Zefan int ret = 0; 19010caa102dSLi Zefan u64 flags = 0; 19020caa102dSLi Zefan 19034a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 19040caa102dSLi Zefan return -EINVAL; 19050caa102dSLi Zefan 19060b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 19070caa102dSLi Zefan if (btrfs_root_readonly(root)) 19080caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 19090b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 19100caa102dSLi Zefan 19110caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 19120caa102dSLi Zefan ret = -EFAULT; 19130caa102dSLi Zefan 19140caa102dSLi Zefan return ret; 19150caa102dSLi Zefan } 19160caa102dSLi Zefan 19170caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 19180caa102dSLi Zefan void __user *arg) 19190caa102dSLi Zefan { 1920496ad9aaSAl Viro struct inode *inode = file_inode(file); 19210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 19220caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19230caa102dSLi Zefan struct btrfs_trans_handle *trans; 19240caa102dSLi Zefan u64 root_flags; 19250caa102dSLi Zefan u64 flags; 19260caa102dSLi Zefan int ret = 0; 19270caa102dSLi Zefan 1928bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1929bd60ea0fSDavid Sterba return -EPERM; 1930bd60ea0fSDavid Sterba 1931b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1932b9ca0664SLiu Bo if (ret) 1933b9ca0664SLiu Bo goto out; 19340caa102dSLi Zefan 19354a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1936b9ca0664SLiu Bo ret = -EINVAL; 1937b9ca0664SLiu Bo goto out_drop_write; 1938b9ca0664SLiu Bo } 19390caa102dSLi Zefan 1940b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1941b9ca0664SLiu Bo ret = -EFAULT; 1942b9ca0664SLiu Bo goto out_drop_write; 1943b9ca0664SLiu Bo } 19440caa102dSLi Zefan 1945b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1946b9ca0664SLiu Bo ret = -EINVAL; 1947b9ca0664SLiu Bo goto out_drop_write; 1948b9ca0664SLiu Bo } 19490caa102dSLi Zefan 1950b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1951b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1952b9ca0664SLiu Bo goto out_drop_write; 1953b9ca0664SLiu Bo } 19540caa102dSLi Zefan 19550b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 19560caa102dSLi Zefan 19570caa102dSLi Zefan /* nothing to do */ 19580caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1959b9ca0664SLiu Bo goto out_drop_sem; 19600caa102dSLi Zefan 19610caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 19622c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 19630caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19640caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 19652c686537SDavid Sterba } else { 19662c686537SDavid Sterba /* 19672c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 19682c686537SDavid Sterba * send 19692c686537SDavid Sterba */ 19702c686537SDavid Sterba spin_lock(&root->root_item_lock); 19712c686537SDavid Sterba if (root->send_in_progress == 0) { 19720caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19730caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 19742c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19752c686537SDavid Sterba } else { 19762c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19770b246afaSJeff Mahoney btrfs_warn(fs_info, 19782c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 19792c686537SDavid Sterba root->root_key.objectid); 19802c686537SDavid Sterba ret = -EPERM; 19812c686537SDavid Sterba goto out_drop_sem; 19822c686537SDavid Sterba } 19832c686537SDavid Sterba } 19840caa102dSLi Zefan 19850caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 19860caa102dSLi Zefan if (IS_ERR(trans)) { 19870caa102dSLi Zefan ret = PTR_ERR(trans); 19880caa102dSLi Zefan goto out_reset; 19890caa102dSLi Zefan } 19900caa102dSLi Zefan 19910b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 19920caa102dSLi Zefan &root->root_key, &root->root_item); 19939417ebc8SNikolay Borisov if (ret < 0) { 19949417ebc8SNikolay Borisov btrfs_end_transaction(trans); 19959417ebc8SNikolay Borisov goto out_reset; 19969417ebc8SNikolay Borisov } 19970caa102dSLi Zefan 19989417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 19999417ebc8SNikolay Borisov 20000caa102dSLi Zefan out_reset: 20010caa102dSLi Zefan if (ret) 20020caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 2003b9ca0664SLiu Bo out_drop_sem: 20040b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 2005b9ca0664SLiu Bo out_drop_write: 2006b9ca0664SLiu Bo mnt_drop_write_file(file); 2007b9ca0664SLiu Bo out: 20080caa102dSLi Zefan return ret; 20090caa102dSLi Zefan } 20100caa102dSLi Zefan 2011ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 2012ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 2013ac8e9819SChris Mason { 2014abc6e134SChris Mason struct btrfs_key test; 2015abc6e134SChris Mason int ret; 2016abc6e134SChris Mason 2017abc6e134SChris Mason test.objectid = sk->min_objectid; 2018abc6e134SChris Mason test.type = sk->min_type; 2019abc6e134SChris Mason test.offset = sk->min_offset; 2020abc6e134SChris Mason 2021abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2022abc6e134SChris Mason if (ret < 0) 2023ac8e9819SChris Mason return 0; 2024abc6e134SChris Mason 2025abc6e134SChris Mason test.objectid = sk->max_objectid; 2026abc6e134SChris Mason test.type = sk->max_type; 2027abc6e134SChris Mason test.offset = sk->max_offset; 2028abc6e134SChris Mason 2029abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2030abc6e134SChris Mason if (ret > 0) 2031ac8e9819SChris Mason return 0; 2032ac8e9819SChris Mason return 1; 2033ac8e9819SChris Mason } 2034ac8e9819SChris Mason 2035df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 2036ac8e9819SChris Mason struct btrfs_key *key, 2037ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 20389b6e817dSGerhard Heift size_t *buf_size, 2039ba346b35SGerhard Heift char __user *ubuf, 2040ac8e9819SChris Mason unsigned long *sk_offset, 2041ac8e9819SChris Mason int *num_found) 2042ac8e9819SChris Mason { 2043ac8e9819SChris Mason u64 found_transid; 2044ac8e9819SChris Mason struct extent_buffer *leaf; 2045ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 2046dd81d459SNaohiro Aota struct btrfs_key test; 2047ac8e9819SChris Mason unsigned long item_off; 2048ac8e9819SChris Mason unsigned long item_len; 2049ac8e9819SChris Mason int nritems; 2050ac8e9819SChris Mason int i; 2051ac8e9819SChris Mason int slot; 2052ac8e9819SChris Mason int ret = 0; 2053ac8e9819SChris Mason 2054ac8e9819SChris Mason leaf = path->nodes[0]; 2055ac8e9819SChris Mason slot = path->slots[0]; 2056ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 2057ac8e9819SChris Mason 2058ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 2059ac8e9819SChris Mason i = nritems; 2060ac8e9819SChris Mason goto advance_key; 2061ac8e9819SChris Mason } 2062ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 2063ac8e9819SChris Mason 2064ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 2065ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 2066ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 2067ac8e9819SChris Mason 206803b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 206903b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 207003b71c6cSGabriel de Perthuis continue; 207103b71c6cSGabriel de Perthuis 20729b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 20738f5f6178SGerhard Heift if (*num_found) { 2074ac8e9819SChris Mason ret = 1; 20758f5f6178SGerhard Heift goto out; 20768f5f6178SGerhard Heift } 20778f5f6178SGerhard Heift 20788f5f6178SGerhard Heift /* 20798f5f6178SGerhard Heift * return one empty item back for v1, which does not 20808f5f6178SGerhard Heift * handle -EOVERFLOW 20818f5f6178SGerhard Heift */ 20828f5f6178SGerhard Heift 20839b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2084ac8e9819SChris Mason item_len = 0; 20858f5f6178SGerhard Heift ret = -EOVERFLOW; 20868f5f6178SGerhard Heift } 2087ac8e9819SChris Mason 20889b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2089ac8e9819SChris Mason ret = 1; 209025c9bc2eSGerhard Heift goto out; 2091ac8e9819SChris Mason } 2092ac8e9819SChris Mason 2093ac8e9819SChris Mason sh.objectid = key->objectid; 2094ac8e9819SChris Mason sh.offset = key->offset; 2095ac8e9819SChris Mason sh.type = key->type; 2096ac8e9819SChris Mason sh.len = item_len; 2097ac8e9819SChris Mason sh.transid = found_transid; 2098ac8e9819SChris Mason 2099ac8e9819SChris Mason /* copy search result header */ 2100ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 2101ba346b35SGerhard Heift ret = -EFAULT; 2102ba346b35SGerhard Heift goto out; 2103ba346b35SGerhard Heift } 2104ba346b35SGerhard Heift 2105ac8e9819SChris Mason *sk_offset += sizeof(sh); 2106ac8e9819SChris Mason 2107ac8e9819SChris Mason if (item_len) { 2108ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2109ac8e9819SChris Mason /* copy the item */ 2110ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2111ba346b35SGerhard Heift item_off, item_len)) { 2112ba346b35SGerhard Heift ret = -EFAULT; 2113ba346b35SGerhard Heift goto out; 2114ba346b35SGerhard Heift } 2115ba346b35SGerhard Heift 2116ac8e9819SChris Mason *sk_offset += item_len; 2117ac8e9819SChris Mason } 2118e2156867SHugo Mills (*num_found)++; 2119ac8e9819SChris Mason 21208f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 21218f5f6178SGerhard Heift goto out; 21228f5f6178SGerhard Heift 212325c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 212425c9bc2eSGerhard Heift ret = 1; 212525c9bc2eSGerhard Heift goto out; 212625c9bc2eSGerhard Heift } 2127ac8e9819SChris Mason } 2128ac8e9819SChris Mason advance_key: 2129ac8e9819SChris Mason ret = 0; 2130dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2131dd81d459SNaohiro Aota test.type = sk->max_type; 2132dd81d459SNaohiro Aota test.offset = sk->max_offset; 2133dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2134dd81d459SNaohiro Aota ret = 1; 2135dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2136abc6e134SChris Mason key->offset++; 2137dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2138abc6e134SChris Mason key->offset = 0; 2139abc6e134SChris Mason key->type++; 2140dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2141abc6e134SChris Mason key->offset = 0; 2142abc6e134SChris Mason key->type = 0; 2143abc6e134SChris Mason key->objectid++; 2144abc6e134SChris Mason } else 2145abc6e134SChris Mason ret = 1; 214625c9bc2eSGerhard Heift out: 2147ba346b35SGerhard Heift /* 2148ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2149ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2150ba346b35SGerhard Heift * * all items were found 2151ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2152ba346b35SGerhard Heift * leaf 2153ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2154ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2155ba346b35SGerhard Heift */ 2156ac8e9819SChris Mason return ret; 2157ac8e9819SChris Mason } 2158ac8e9819SChris Mason 2159ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 216012544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 21619b6e817dSGerhard Heift size_t *buf_size, 2162ba346b35SGerhard Heift char __user *ubuf) 2163ac8e9819SChris Mason { 21640b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2165ac8e9819SChris Mason struct btrfs_root *root; 2166ac8e9819SChris Mason struct btrfs_key key; 2167ac8e9819SChris Mason struct btrfs_path *path; 2168ac8e9819SChris Mason int ret; 2169ac8e9819SChris Mason int num_found = 0; 2170ac8e9819SChris Mason unsigned long sk_offset = 0; 2171ac8e9819SChris Mason 21729b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 21739b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 217412544442SGerhard Heift return -EOVERFLOW; 21759b6e817dSGerhard Heift } 217612544442SGerhard Heift 2177ac8e9819SChris Mason path = btrfs_alloc_path(); 2178ac8e9819SChris Mason if (!path) 2179ac8e9819SChris Mason return -ENOMEM; 2180ac8e9819SChris Mason 2181ac8e9819SChris Mason if (sk->tree_id == 0) { 2182ac8e9819SChris Mason /* search the root of the inode that was passed */ 21833ca35e83SJosef Bacik root = btrfs_grab_fs_root(BTRFS_I(inode)->root); 2184ac8e9819SChris Mason } else { 2185ac8e9819SChris Mason key.objectid = sk->tree_id; 2186ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2187ac8e9819SChris Mason key.offset = (u64)-1; 21883619c94fSJosef Bacik root = btrfs_get_fs_root(info, &key, true); 2189ac8e9819SChris Mason if (IS_ERR(root)) { 2190ac8e9819SChris Mason btrfs_free_path(path); 2191ad1e3d56SMisono Tomohiro return PTR_ERR(root); 2192ac8e9819SChris Mason } 21933ca35e83SJosef Bacik if (!btrfs_grab_fs_root(root)) { 21943ca35e83SJosef Bacik btrfs_free_path(path); 21953ca35e83SJosef Bacik return -ENOENT; 21963ca35e83SJosef Bacik } 2197ac8e9819SChris Mason } 2198ac8e9819SChris Mason 2199ac8e9819SChris Mason key.objectid = sk->min_objectid; 2200ac8e9819SChris Mason key.type = sk->min_type; 2201ac8e9819SChris Mason key.offset = sk->min_offset; 2202ac8e9819SChris Mason 2203ac8e9819SChris Mason while (1) { 22046174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2205ac8e9819SChris Mason if (ret != 0) { 2206ac8e9819SChris Mason if (ret > 0) 2207ac8e9819SChris Mason ret = 0; 2208ac8e9819SChris Mason goto err; 2209ac8e9819SChris Mason } 2210df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2211ac8e9819SChris Mason &sk_offset, &num_found); 2212b3b4aa74SDavid Sterba btrfs_release_path(path); 221325c9bc2eSGerhard Heift if (ret) 2214ac8e9819SChris Mason break; 2215ac8e9819SChris Mason 2216ac8e9819SChris Mason } 22178f5f6178SGerhard Heift if (ret > 0) 2218ac8e9819SChris Mason ret = 0; 2219ac8e9819SChris Mason err: 2220ac8e9819SChris Mason sk->nr_items = num_found; 22213ca35e83SJosef Bacik btrfs_put_fs_root(root); 2222ac8e9819SChris Mason btrfs_free_path(path); 2223ac8e9819SChris Mason return ret; 2224ac8e9819SChris Mason } 2225ac8e9819SChris Mason 2226ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2227ac8e9819SChris Mason void __user *argp) 2228ac8e9819SChris Mason { 2229ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2230ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2231ac8e9819SChris Mason struct inode *inode; 2232ac8e9819SChris Mason int ret; 22339b6e817dSGerhard Heift size_t buf_size; 2234ac8e9819SChris Mason 2235ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2236ac8e9819SChris Mason return -EPERM; 2237ac8e9819SChris Mason 2238ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2239ac8e9819SChris Mason 2240ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2241ba346b35SGerhard Heift return -EFAULT; 2242ba346b35SGerhard Heift 2243ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2244ac8e9819SChris Mason 2245496ad9aaSAl Viro inode = file_inode(file); 2246ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 22478f5f6178SGerhard Heift 22488f5f6178SGerhard Heift /* 22498f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 22508f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 22518f5f6178SGerhard Heift */ 22528f5f6178SGerhard Heift if (ret == -EOVERFLOW) 22538f5f6178SGerhard Heift ret = 0; 22548f5f6178SGerhard Heift 2255ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2256ac8e9819SChris Mason ret = -EFAULT; 2257ac8e9819SChris Mason return ret; 2258ac8e9819SChris Mason } 2259ac8e9819SChris Mason 2260cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2261cc68a8a5SGerhard Heift void __user *argp) 2262cc68a8a5SGerhard Heift { 2263cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2264cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2265cc68a8a5SGerhard Heift struct inode *inode; 2266cc68a8a5SGerhard Heift int ret; 2267cc68a8a5SGerhard Heift size_t buf_size; 2268ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2269cc68a8a5SGerhard Heift 2270cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2271cc68a8a5SGerhard Heift return -EPERM; 2272cc68a8a5SGerhard Heift 2273cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2274cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2275cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2276cc68a8a5SGerhard Heift return -EFAULT; 2277cc68a8a5SGerhard Heift 2278cc68a8a5SGerhard Heift buf_size = args.buf_size; 2279cc68a8a5SGerhard Heift 2280cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2281cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2282cc68a8a5SGerhard Heift buf_size = buf_limit; 2283cc68a8a5SGerhard Heift 2284cc68a8a5SGerhard Heift inode = file_inode(file); 2285cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2286718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2287cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2288cc68a8a5SGerhard Heift ret = -EFAULT; 2289cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2290cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2291cc68a8a5SGerhard Heift ret = -EFAULT; 2292cc68a8a5SGerhard Heift 229376dda93cSYan, Zheng return ret; 229476dda93cSYan, Zheng } 229576dda93cSYan, Zheng 229698d377a0STARUISI Hiroaki /* 2297ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2298ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 229998d377a0STARUISI Hiroaki */ 230098d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 230198d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 230298d377a0STARUISI Hiroaki { 230398d377a0STARUISI Hiroaki struct btrfs_root *root; 230498d377a0STARUISI Hiroaki struct btrfs_key key; 2305ac8e9819SChris Mason char *ptr; 230698d377a0STARUISI Hiroaki int ret = -1; 230798d377a0STARUISI Hiroaki int slot; 230898d377a0STARUISI Hiroaki int len; 230998d377a0STARUISI Hiroaki int total_len = 0; 231098d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 231198d377a0STARUISI Hiroaki struct extent_buffer *l; 231298d377a0STARUISI Hiroaki struct btrfs_path *path; 231398d377a0STARUISI Hiroaki 231498d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 231598d377a0STARUISI Hiroaki name[0]='\0'; 231698d377a0STARUISI Hiroaki return 0; 231798d377a0STARUISI Hiroaki } 231898d377a0STARUISI Hiroaki 231998d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 232098d377a0STARUISI Hiroaki if (!path) 232198d377a0STARUISI Hiroaki return -ENOMEM; 232298d377a0STARUISI Hiroaki 2323c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 232498d377a0STARUISI Hiroaki 232598d377a0STARUISI Hiroaki key.objectid = tree_id; 232698d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 232798d377a0STARUISI Hiroaki key.offset = (u64)-1; 23283619c94fSJosef Bacik root = btrfs_get_fs_root(info, &key, true); 232998d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2330ad1e3d56SMisono Tomohiro ret = PTR_ERR(root); 233188234012SJosef Bacik root = NULL; 233288234012SJosef Bacik goto out; 233388234012SJosef Bacik } 233488234012SJosef Bacik if (!btrfs_grab_fs_root(root)) { 233588234012SJosef Bacik ret = -ENOENT; 233688234012SJosef Bacik root = NULL; 23378ad6fcabSChris Mason goto out; 233898d377a0STARUISI Hiroaki } 233998d377a0STARUISI Hiroaki 234098d377a0STARUISI Hiroaki key.objectid = dirid; 234198d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 23428ad6fcabSChris Mason key.offset = (u64)-1; 234398d377a0STARUISI Hiroaki 234498d377a0STARUISI Hiroaki while (1) { 234598d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 234698d377a0STARUISI Hiroaki if (ret < 0) 234798d377a0STARUISI Hiroaki goto out; 234818674c6cSFilipe David Borba Manana else if (ret > 0) { 234918674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 235018674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 235118674c6cSFilipe David Borba Manana if (ret < 0) 235218674c6cSFilipe David Borba Manana goto out; 235318674c6cSFilipe David Borba Manana else if (ret > 0) { 2354ac8e9819SChris Mason ret = -ENOENT; 235598d377a0STARUISI Hiroaki goto out; 2356ac8e9819SChris Mason } 235718674c6cSFilipe David Borba Manana } 235818674c6cSFilipe David Borba Manana 235918674c6cSFilipe David Borba Manana l = path->nodes[0]; 236018674c6cSFilipe David Borba Manana slot = path->slots[0]; 236118674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 236298d377a0STARUISI Hiroaki 236398d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 236498d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 236598d377a0STARUISI Hiroaki ptr -= len + 1; 236698d377a0STARUISI Hiroaki total_len += len + 1; 2367a696cf35SFilipe David Borba Manana if (ptr < name) { 2368a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 236998d377a0STARUISI Hiroaki goto out; 2370a696cf35SFilipe David Borba Manana } 237198d377a0STARUISI Hiroaki 237298d377a0STARUISI Hiroaki *(ptr + len) = '/'; 237398d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 237498d377a0STARUISI Hiroaki 237598d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 237698d377a0STARUISI Hiroaki break; 237798d377a0STARUISI Hiroaki 2378b3b4aa74SDavid Sterba btrfs_release_path(path); 237998d377a0STARUISI Hiroaki key.objectid = key.offset; 23808ad6fcabSChris Mason key.offset = (u64)-1; 238198d377a0STARUISI Hiroaki dirid = key.objectid; 238298d377a0STARUISI Hiroaki } 238377906a50SLi Zefan memmove(name, ptr, total_len); 238498d377a0STARUISI Hiroaki name[total_len] = '\0'; 238598d377a0STARUISI Hiroaki ret = 0; 238698d377a0STARUISI Hiroaki out: 238788234012SJosef Bacik btrfs_put_fs_root(root); 238898d377a0STARUISI Hiroaki btrfs_free_path(path); 2389ac8e9819SChris Mason return ret; 2390ac8e9819SChris Mason } 2391ac8e9819SChris Mason 239223d0b79dSTomohiro Misono static int btrfs_search_path_in_tree_user(struct inode *inode, 239323d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args) 239423d0b79dSTomohiro Misono { 239523d0b79dSTomohiro Misono struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 239623d0b79dSTomohiro Misono struct super_block *sb = inode->i_sb; 239723d0b79dSTomohiro Misono struct btrfs_key upper_limit = BTRFS_I(inode)->location; 239823d0b79dSTomohiro Misono u64 treeid = BTRFS_I(inode)->root->root_key.objectid; 239923d0b79dSTomohiro Misono u64 dirid = args->dirid; 240023d0b79dSTomohiro Misono unsigned long item_off; 240123d0b79dSTomohiro Misono unsigned long item_len; 240223d0b79dSTomohiro Misono struct btrfs_inode_ref *iref; 240323d0b79dSTomohiro Misono struct btrfs_root_ref *rref; 2404b8a49ae1SJosef Bacik struct btrfs_root *root = NULL; 240523d0b79dSTomohiro Misono struct btrfs_path *path; 240623d0b79dSTomohiro Misono struct btrfs_key key, key2; 240723d0b79dSTomohiro Misono struct extent_buffer *leaf; 240823d0b79dSTomohiro Misono struct inode *temp_inode; 240923d0b79dSTomohiro Misono char *ptr; 241023d0b79dSTomohiro Misono int slot; 241123d0b79dSTomohiro Misono int len; 241223d0b79dSTomohiro Misono int total_len = 0; 241323d0b79dSTomohiro Misono int ret; 241423d0b79dSTomohiro Misono 241523d0b79dSTomohiro Misono path = btrfs_alloc_path(); 241623d0b79dSTomohiro Misono if (!path) 241723d0b79dSTomohiro Misono return -ENOMEM; 241823d0b79dSTomohiro Misono 241923d0b79dSTomohiro Misono /* 242023d0b79dSTomohiro Misono * If the bottom subvolume does not exist directly under upper_limit, 242123d0b79dSTomohiro Misono * construct the path in from the bottom up. 242223d0b79dSTomohiro Misono */ 242323d0b79dSTomohiro Misono if (dirid != upper_limit.objectid) { 242423d0b79dSTomohiro Misono ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; 242523d0b79dSTomohiro Misono 242623d0b79dSTomohiro Misono key.objectid = treeid; 242723d0b79dSTomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 242823d0b79dSTomohiro Misono key.offset = (u64)-1; 24293619c94fSJosef Bacik root = btrfs_get_fs_root(fs_info, &key, true); 243023d0b79dSTomohiro Misono if (IS_ERR(root)) { 243123d0b79dSTomohiro Misono ret = PTR_ERR(root); 243223d0b79dSTomohiro Misono goto out; 243323d0b79dSTomohiro Misono } 2434b8a49ae1SJosef Bacik if (!btrfs_grab_fs_root(root)) { 2435b8a49ae1SJosef Bacik ret = -ENOENT; 2436b8a49ae1SJosef Bacik goto out; 2437b8a49ae1SJosef Bacik } 243823d0b79dSTomohiro Misono 243923d0b79dSTomohiro Misono key.objectid = dirid; 244023d0b79dSTomohiro Misono key.type = BTRFS_INODE_REF_KEY; 244123d0b79dSTomohiro Misono key.offset = (u64)-1; 244223d0b79dSTomohiro Misono while (1) { 244323d0b79dSTomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 244423d0b79dSTomohiro Misono if (ret < 0) { 2445b8a49ae1SJosef Bacik goto out_put; 244623d0b79dSTomohiro Misono } else if (ret > 0) { 244723d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 244823d0b79dSTomohiro Misono BTRFS_INODE_REF_KEY); 244923d0b79dSTomohiro Misono if (ret < 0) { 2450b8a49ae1SJosef Bacik goto out_put; 245123d0b79dSTomohiro Misono } else if (ret > 0) { 245223d0b79dSTomohiro Misono ret = -ENOENT; 2453b8a49ae1SJosef Bacik goto out_put; 245423d0b79dSTomohiro Misono } 245523d0b79dSTomohiro Misono } 245623d0b79dSTomohiro Misono 245723d0b79dSTomohiro Misono leaf = path->nodes[0]; 245823d0b79dSTomohiro Misono slot = path->slots[0]; 245923d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 246023d0b79dSTomohiro Misono 246123d0b79dSTomohiro Misono iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); 246223d0b79dSTomohiro Misono len = btrfs_inode_ref_name_len(leaf, iref); 246323d0b79dSTomohiro Misono ptr -= len + 1; 246423d0b79dSTomohiro Misono total_len += len + 1; 246523d0b79dSTomohiro Misono if (ptr < args->path) { 246623d0b79dSTomohiro Misono ret = -ENAMETOOLONG; 2467b8a49ae1SJosef Bacik goto out_put; 246823d0b79dSTomohiro Misono } 246923d0b79dSTomohiro Misono 247023d0b79dSTomohiro Misono *(ptr + len) = '/'; 247123d0b79dSTomohiro Misono read_extent_buffer(leaf, ptr, 247223d0b79dSTomohiro Misono (unsigned long)(iref + 1), len); 247323d0b79dSTomohiro Misono 247423d0b79dSTomohiro Misono /* Check the read+exec permission of this directory */ 247523d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 247623d0b79dSTomohiro Misono BTRFS_INODE_ITEM_KEY); 247723d0b79dSTomohiro Misono if (ret < 0) { 2478b8a49ae1SJosef Bacik goto out_put; 247923d0b79dSTomohiro Misono } else if (ret > 0) { 248023d0b79dSTomohiro Misono ret = -ENOENT; 2481b8a49ae1SJosef Bacik goto out_put; 248223d0b79dSTomohiro Misono } 248323d0b79dSTomohiro Misono 248423d0b79dSTomohiro Misono leaf = path->nodes[0]; 248523d0b79dSTomohiro Misono slot = path->slots[0]; 248623d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key2, slot); 248723d0b79dSTomohiro Misono if (key2.objectid != dirid) { 248823d0b79dSTomohiro Misono ret = -ENOENT; 2489b8a49ae1SJosef Bacik goto out_put; 249023d0b79dSTomohiro Misono } 249123d0b79dSTomohiro Misono 24924c66e0d4SDavid Sterba temp_inode = btrfs_iget(sb, &key2, root); 24933ca57bd6SMisono Tomohiro if (IS_ERR(temp_inode)) { 24943ca57bd6SMisono Tomohiro ret = PTR_ERR(temp_inode); 2495b8a49ae1SJosef Bacik goto out_put; 24963ca57bd6SMisono Tomohiro } 249723d0b79dSTomohiro Misono ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); 249823d0b79dSTomohiro Misono iput(temp_inode); 249923d0b79dSTomohiro Misono if (ret) { 250023d0b79dSTomohiro Misono ret = -EACCES; 2501b8a49ae1SJosef Bacik goto out_put; 250223d0b79dSTomohiro Misono } 250323d0b79dSTomohiro Misono 250423d0b79dSTomohiro Misono if (key.offset == upper_limit.objectid) 250523d0b79dSTomohiro Misono break; 250623d0b79dSTomohiro Misono if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { 250723d0b79dSTomohiro Misono ret = -EACCES; 2508b8a49ae1SJosef Bacik goto out_put; 250923d0b79dSTomohiro Misono } 251023d0b79dSTomohiro Misono 251123d0b79dSTomohiro Misono btrfs_release_path(path); 251223d0b79dSTomohiro Misono key.objectid = key.offset; 251323d0b79dSTomohiro Misono key.offset = (u64)-1; 251423d0b79dSTomohiro Misono dirid = key.objectid; 251523d0b79dSTomohiro Misono } 251623d0b79dSTomohiro Misono 251723d0b79dSTomohiro Misono memmove(args->path, ptr, total_len); 251823d0b79dSTomohiro Misono args->path[total_len] = '\0'; 2519b8a49ae1SJosef Bacik btrfs_put_fs_root(root); 2520b8a49ae1SJosef Bacik root = NULL; 252123d0b79dSTomohiro Misono btrfs_release_path(path); 252223d0b79dSTomohiro Misono } 252323d0b79dSTomohiro Misono 252423d0b79dSTomohiro Misono /* Get the bottom subvolume's name from ROOT_REF */ 252523d0b79dSTomohiro Misono key.objectid = treeid; 252623d0b79dSTomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 252723d0b79dSTomohiro Misono key.offset = args->treeid; 2528b8a49ae1SJosef Bacik ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 252923d0b79dSTomohiro Misono if (ret < 0) { 253023d0b79dSTomohiro Misono goto out; 253123d0b79dSTomohiro Misono } else if (ret > 0) { 253223d0b79dSTomohiro Misono ret = -ENOENT; 253323d0b79dSTomohiro Misono goto out; 253423d0b79dSTomohiro Misono } 253523d0b79dSTomohiro Misono 253623d0b79dSTomohiro Misono leaf = path->nodes[0]; 253723d0b79dSTomohiro Misono slot = path->slots[0]; 253823d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 253923d0b79dSTomohiro Misono 254023d0b79dSTomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot); 254123d0b79dSTomohiro Misono item_len = btrfs_item_size_nr(leaf, slot); 254223d0b79dSTomohiro Misono /* Check if dirid in ROOT_REF corresponds to passed dirid */ 254323d0b79dSTomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 254423d0b79dSTomohiro Misono if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { 254523d0b79dSTomohiro Misono ret = -EINVAL; 254623d0b79dSTomohiro Misono goto out; 254723d0b79dSTomohiro Misono } 254823d0b79dSTomohiro Misono 254923d0b79dSTomohiro Misono /* Copy subvolume's name */ 255023d0b79dSTomohiro Misono item_off += sizeof(struct btrfs_root_ref); 255123d0b79dSTomohiro Misono item_len -= sizeof(struct btrfs_root_ref); 255223d0b79dSTomohiro Misono read_extent_buffer(leaf, args->name, item_off, item_len); 255323d0b79dSTomohiro Misono args->name[item_len] = 0; 255423d0b79dSTomohiro Misono 2555b8a49ae1SJosef Bacik out_put: 2556b8a49ae1SJosef Bacik btrfs_put_fs_root(root); 255723d0b79dSTomohiro Misono out: 255823d0b79dSTomohiro Misono btrfs_free_path(path); 255923d0b79dSTomohiro Misono return ret; 256023d0b79dSTomohiro Misono } 256123d0b79dSTomohiro Misono 2562ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2563ac8e9819SChris Mason void __user *argp) 2564ac8e9819SChris Mason { 2565ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2566ac8e9819SChris Mason struct inode *inode; 256701b810b8SDavid Sterba int ret = 0; 2568ac8e9819SChris Mason 25692354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 25702354d08fSJulia Lawall if (IS_ERR(args)) 25712354d08fSJulia Lawall return PTR_ERR(args); 2572c2b96929SDan Carpenter 2573496ad9aaSAl Viro inode = file_inode(file); 2574ac8e9819SChris Mason 257501b810b8SDavid Sterba /* 257601b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 257701b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 257801b810b8SDavid Sterba */ 25791b53ac4dSChris Mason if (args->treeid == 0) 25801b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 25811b53ac4dSChris Mason 258201b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 258301b810b8SDavid Sterba args->name[0] = 0; 258401b810b8SDavid Sterba goto out; 258501b810b8SDavid Sterba } 258601b810b8SDavid Sterba 258701b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 258801b810b8SDavid Sterba ret = -EPERM; 258901b810b8SDavid Sterba goto out; 259001b810b8SDavid Sterba } 259101b810b8SDavid Sterba 2592ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2593ac8e9819SChris Mason args->treeid, args->objectid, 2594ac8e9819SChris Mason args->name); 2595ac8e9819SChris Mason 259601b810b8SDavid Sterba out: 2597ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2598ac8e9819SChris Mason ret = -EFAULT; 2599ac8e9819SChris Mason 2600ac8e9819SChris Mason kfree(args); 260198d377a0STARUISI Hiroaki return ret; 260298d377a0STARUISI Hiroaki } 260398d377a0STARUISI Hiroaki 260423d0b79dSTomohiro Misono /* 260523d0b79dSTomohiro Misono * Version of ino_lookup ioctl (unprivileged) 260623d0b79dSTomohiro Misono * 260723d0b79dSTomohiro Misono * The main differences from ino_lookup ioctl are: 260823d0b79dSTomohiro Misono * 260923d0b79dSTomohiro Misono * 1. Read + Exec permission will be checked using inode_permission() during 261023d0b79dSTomohiro Misono * path construction. -EACCES will be returned in case of failure. 261123d0b79dSTomohiro Misono * 2. Path construction will be stopped at the inode number which corresponds 261223d0b79dSTomohiro Misono * to the fd with which this ioctl is called. If constructed path does not 261323d0b79dSTomohiro Misono * exist under fd's inode, -EACCES will be returned. 261423d0b79dSTomohiro Misono * 3. The name of bottom subvolume is also searched and filled. 261523d0b79dSTomohiro Misono */ 261623d0b79dSTomohiro Misono static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) 261723d0b79dSTomohiro Misono { 261823d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args; 261923d0b79dSTomohiro Misono struct inode *inode; 262023d0b79dSTomohiro Misono int ret; 262123d0b79dSTomohiro Misono 262223d0b79dSTomohiro Misono args = memdup_user(argp, sizeof(*args)); 262323d0b79dSTomohiro Misono if (IS_ERR(args)) 262423d0b79dSTomohiro Misono return PTR_ERR(args); 262523d0b79dSTomohiro Misono 262623d0b79dSTomohiro Misono inode = file_inode(file); 262723d0b79dSTomohiro Misono 262823d0b79dSTomohiro Misono if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && 262923d0b79dSTomohiro Misono BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { 263023d0b79dSTomohiro Misono /* 263123d0b79dSTomohiro Misono * The subvolume does not exist under fd with which this is 263223d0b79dSTomohiro Misono * called 263323d0b79dSTomohiro Misono */ 263423d0b79dSTomohiro Misono kfree(args); 263523d0b79dSTomohiro Misono return -EACCES; 263623d0b79dSTomohiro Misono } 263723d0b79dSTomohiro Misono 263823d0b79dSTomohiro Misono ret = btrfs_search_path_in_tree_user(inode, args); 263923d0b79dSTomohiro Misono 264023d0b79dSTomohiro Misono if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 264123d0b79dSTomohiro Misono ret = -EFAULT; 264223d0b79dSTomohiro Misono 264323d0b79dSTomohiro Misono kfree(args); 264423d0b79dSTomohiro Misono return ret; 264523d0b79dSTomohiro Misono } 264623d0b79dSTomohiro Misono 2647b64ec075STomohiro Misono /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ 2648b64ec075STomohiro Misono static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) 2649b64ec075STomohiro Misono { 2650b64ec075STomohiro Misono struct btrfs_ioctl_get_subvol_info_args *subvol_info; 2651b64ec075STomohiro Misono struct btrfs_fs_info *fs_info; 2652b64ec075STomohiro Misono struct btrfs_root *root; 2653b64ec075STomohiro Misono struct btrfs_path *path; 2654b64ec075STomohiro Misono struct btrfs_key key; 2655b64ec075STomohiro Misono struct btrfs_root_item *root_item; 2656b64ec075STomohiro Misono struct btrfs_root_ref *rref; 2657b64ec075STomohiro Misono struct extent_buffer *leaf; 2658b64ec075STomohiro Misono unsigned long item_off; 2659b64ec075STomohiro Misono unsigned long item_len; 2660b64ec075STomohiro Misono struct inode *inode; 2661b64ec075STomohiro Misono int slot; 2662b64ec075STomohiro Misono int ret = 0; 2663b64ec075STomohiro Misono 2664b64ec075STomohiro Misono path = btrfs_alloc_path(); 2665b64ec075STomohiro Misono if (!path) 2666b64ec075STomohiro Misono return -ENOMEM; 2667b64ec075STomohiro Misono 2668b64ec075STomohiro Misono subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); 2669b64ec075STomohiro Misono if (!subvol_info) { 2670b64ec075STomohiro Misono btrfs_free_path(path); 2671b64ec075STomohiro Misono return -ENOMEM; 2672b64ec075STomohiro Misono } 2673b64ec075STomohiro Misono 2674b64ec075STomohiro Misono inode = file_inode(file); 2675b64ec075STomohiro Misono fs_info = BTRFS_I(inode)->root->fs_info; 2676b64ec075STomohiro Misono 2677b64ec075STomohiro Misono /* Get root_item of inode's subvolume */ 2678b64ec075STomohiro Misono key.objectid = BTRFS_I(inode)->root->root_key.objectid; 2679b64ec075STomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 2680b64ec075STomohiro Misono key.offset = (u64)-1; 26813619c94fSJosef Bacik root = btrfs_get_fs_root(fs_info, &key, true); 2682b64ec075STomohiro Misono if (IS_ERR(root)) { 2683b64ec075STomohiro Misono ret = PTR_ERR(root); 2684b64ec075STomohiro Misono goto out; 2685b64ec075STomohiro Misono } 2686b64ec075STomohiro Misono root_item = &root->root_item; 2687b64ec075STomohiro Misono 2688b64ec075STomohiro Misono subvol_info->treeid = key.objectid; 2689b64ec075STomohiro Misono 2690b64ec075STomohiro Misono subvol_info->generation = btrfs_root_generation(root_item); 2691b64ec075STomohiro Misono subvol_info->flags = btrfs_root_flags(root_item); 2692b64ec075STomohiro Misono 2693b64ec075STomohiro Misono memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); 2694b64ec075STomohiro Misono memcpy(subvol_info->parent_uuid, root_item->parent_uuid, 2695b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2696b64ec075STomohiro Misono memcpy(subvol_info->received_uuid, root_item->received_uuid, 2697b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2698b64ec075STomohiro Misono 2699b64ec075STomohiro Misono subvol_info->ctransid = btrfs_root_ctransid(root_item); 2700b64ec075STomohiro Misono subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); 2701b64ec075STomohiro Misono subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); 2702b64ec075STomohiro Misono 2703b64ec075STomohiro Misono subvol_info->otransid = btrfs_root_otransid(root_item); 2704b64ec075STomohiro Misono subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); 2705b64ec075STomohiro Misono subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); 2706b64ec075STomohiro Misono 2707b64ec075STomohiro Misono subvol_info->stransid = btrfs_root_stransid(root_item); 2708b64ec075STomohiro Misono subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); 2709b64ec075STomohiro Misono subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); 2710b64ec075STomohiro Misono 2711b64ec075STomohiro Misono subvol_info->rtransid = btrfs_root_rtransid(root_item); 2712b64ec075STomohiro Misono subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); 2713b64ec075STomohiro Misono subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); 2714b64ec075STomohiro Misono 2715b64ec075STomohiro Misono if (key.objectid != BTRFS_FS_TREE_OBJECTID) { 2716b64ec075STomohiro Misono /* Search root tree for ROOT_BACKREF of this subvolume */ 2717b64ec075STomohiro Misono root = fs_info->tree_root; 2718b64ec075STomohiro Misono 2719b64ec075STomohiro Misono key.type = BTRFS_ROOT_BACKREF_KEY; 2720b64ec075STomohiro Misono key.offset = 0; 2721b64ec075STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2722b64ec075STomohiro Misono if (ret < 0) { 2723b64ec075STomohiro Misono goto out; 2724b64ec075STomohiro Misono } else if (path->slots[0] >= 2725b64ec075STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 2726b64ec075STomohiro Misono ret = btrfs_next_leaf(root, path); 2727b64ec075STomohiro Misono if (ret < 0) { 2728b64ec075STomohiro Misono goto out; 2729b64ec075STomohiro Misono } else if (ret > 0) { 2730b64ec075STomohiro Misono ret = -EUCLEAN; 2731b64ec075STomohiro Misono goto out; 2732b64ec075STomohiro Misono } 2733b64ec075STomohiro Misono } 2734b64ec075STomohiro Misono 2735b64ec075STomohiro Misono leaf = path->nodes[0]; 2736b64ec075STomohiro Misono slot = path->slots[0]; 2737b64ec075STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 2738b64ec075STomohiro Misono if (key.objectid == subvol_info->treeid && 2739b64ec075STomohiro Misono key.type == BTRFS_ROOT_BACKREF_KEY) { 2740b64ec075STomohiro Misono subvol_info->parent_id = key.offset; 2741b64ec075STomohiro Misono 2742b64ec075STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 2743b64ec075STomohiro Misono subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); 2744b64ec075STomohiro Misono 2745b64ec075STomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot) 2746b64ec075STomohiro Misono + sizeof(struct btrfs_root_ref); 2747b64ec075STomohiro Misono item_len = btrfs_item_size_nr(leaf, slot) 2748b64ec075STomohiro Misono - sizeof(struct btrfs_root_ref); 2749b64ec075STomohiro Misono read_extent_buffer(leaf, subvol_info->name, 2750b64ec075STomohiro Misono item_off, item_len); 2751b64ec075STomohiro Misono } else { 2752b64ec075STomohiro Misono ret = -ENOENT; 2753b64ec075STomohiro Misono goto out; 2754b64ec075STomohiro Misono } 2755b64ec075STomohiro Misono } 2756b64ec075STomohiro Misono 2757b64ec075STomohiro Misono if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) 2758b64ec075STomohiro Misono ret = -EFAULT; 2759b64ec075STomohiro Misono 2760b64ec075STomohiro Misono out: 2761b64ec075STomohiro Misono btrfs_free_path(path); 2762b64ec075STomohiro Misono kzfree(subvol_info); 2763b64ec075STomohiro Misono return ret; 2764b64ec075STomohiro Misono } 2765b64ec075STomohiro Misono 276642e4b520STomohiro Misono /* 276742e4b520STomohiro Misono * Return ROOT_REF information of the subvolume containing this inode 276842e4b520STomohiro Misono * except the subvolume name. 276942e4b520STomohiro Misono */ 277042e4b520STomohiro Misono static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) 277142e4b520STomohiro Misono { 277242e4b520STomohiro Misono struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; 277342e4b520STomohiro Misono struct btrfs_root_ref *rref; 277442e4b520STomohiro Misono struct btrfs_root *root; 277542e4b520STomohiro Misono struct btrfs_path *path; 277642e4b520STomohiro Misono struct btrfs_key key; 277742e4b520STomohiro Misono struct extent_buffer *leaf; 277842e4b520STomohiro Misono struct inode *inode; 277942e4b520STomohiro Misono u64 objectid; 278042e4b520STomohiro Misono int slot; 278142e4b520STomohiro Misono int ret; 278242e4b520STomohiro Misono u8 found; 278342e4b520STomohiro Misono 278442e4b520STomohiro Misono path = btrfs_alloc_path(); 278542e4b520STomohiro Misono if (!path) 278642e4b520STomohiro Misono return -ENOMEM; 278742e4b520STomohiro Misono 278842e4b520STomohiro Misono rootrefs = memdup_user(argp, sizeof(*rootrefs)); 278942e4b520STomohiro Misono if (IS_ERR(rootrefs)) { 279042e4b520STomohiro Misono btrfs_free_path(path); 279142e4b520STomohiro Misono return PTR_ERR(rootrefs); 279242e4b520STomohiro Misono } 279342e4b520STomohiro Misono 279442e4b520STomohiro Misono inode = file_inode(file); 279542e4b520STomohiro Misono root = BTRFS_I(inode)->root->fs_info->tree_root; 279642e4b520STomohiro Misono objectid = BTRFS_I(inode)->root->root_key.objectid; 279742e4b520STomohiro Misono 279842e4b520STomohiro Misono key.objectid = objectid; 279942e4b520STomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 280042e4b520STomohiro Misono key.offset = rootrefs->min_treeid; 280142e4b520STomohiro Misono found = 0; 280242e4b520STomohiro Misono 280342e4b520STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 280442e4b520STomohiro Misono if (ret < 0) { 280542e4b520STomohiro Misono goto out; 280642e4b520STomohiro Misono } else if (path->slots[0] >= 280742e4b520STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 280842e4b520STomohiro Misono ret = btrfs_next_leaf(root, path); 280942e4b520STomohiro Misono if (ret < 0) { 281042e4b520STomohiro Misono goto out; 281142e4b520STomohiro Misono } else if (ret > 0) { 281242e4b520STomohiro Misono ret = -EUCLEAN; 281342e4b520STomohiro Misono goto out; 281442e4b520STomohiro Misono } 281542e4b520STomohiro Misono } 281642e4b520STomohiro Misono while (1) { 281742e4b520STomohiro Misono leaf = path->nodes[0]; 281842e4b520STomohiro Misono slot = path->slots[0]; 281942e4b520STomohiro Misono 282042e4b520STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 282142e4b520STomohiro Misono if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { 282242e4b520STomohiro Misono ret = 0; 282342e4b520STomohiro Misono goto out; 282442e4b520STomohiro Misono } 282542e4b520STomohiro Misono 282642e4b520STomohiro Misono if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { 282742e4b520STomohiro Misono ret = -EOVERFLOW; 282842e4b520STomohiro Misono goto out; 282942e4b520STomohiro Misono } 283042e4b520STomohiro Misono 283142e4b520STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 283242e4b520STomohiro Misono rootrefs->rootref[found].treeid = key.offset; 283342e4b520STomohiro Misono rootrefs->rootref[found].dirid = 283442e4b520STomohiro Misono btrfs_root_ref_dirid(leaf, rref); 283542e4b520STomohiro Misono found++; 283642e4b520STomohiro Misono 283742e4b520STomohiro Misono ret = btrfs_next_item(root, path); 283842e4b520STomohiro Misono if (ret < 0) { 283942e4b520STomohiro Misono goto out; 284042e4b520STomohiro Misono } else if (ret > 0) { 284142e4b520STomohiro Misono ret = -EUCLEAN; 284242e4b520STomohiro Misono goto out; 284342e4b520STomohiro Misono } 284442e4b520STomohiro Misono } 284542e4b520STomohiro Misono 284642e4b520STomohiro Misono out: 284742e4b520STomohiro Misono if (!ret || ret == -EOVERFLOW) { 284842e4b520STomohiro Misono rootrefs->num_items = found; 284942e4b520STomohiro Misono /* update min_treeid for next search */ 285042e4b520STomohiro Misono if (found) 285142e4b520STomohiro Misono rootrefs->min_treeid = 285242e4b520STomohiro Misono rootrefs->rootref[found - 1].treeid + 1; 285342e4b520STomohiro Misono if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) 285442e4b520STomohiro Misono ret = -EFAULT; 285542e4b520STomohiro Misono } 285642e4b520STomohiro Misono 285742e4b520STomohiro Misono kfree(rootrefs); 285842e4b520STomohiro Misono btrfs_free_path(path); 285942e4b520STomohiro Misono 286042e4b520STomohiro Misono return ret; 286142e4b520STomohiro Misono } 286242e4b520STomohiro Misono 286376dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 286476dda93cSYan, Zheng void __user *arg) 286576dda93cSYan, Zheng { 286654563d41SAl Viro struct dentry *parent = file->f_path.dentry; 28670b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 286876dda93cSYan, Zheng struct dentry *dentry; 28692b0143b5SDavid Howells struct inode *dir = d_inode(parent); 287076dda93cSYan, Zheng struct inode *inode; 287176dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 287276dda93cSYan, Zheng struct btrfs_root *dest = NULL; 287376dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 287476dda93cSYan, Zheng int namelen; 287576dda93cSYan, Zheng int err = 0; 287676dda93cSYan, Zheng 2877325c50e3SJeff Mahoney if (!S_ISDIR(dir->i_mode)) 2878325c50e3SJeff Mahoney return -ENOTDIR; 2879325c50e3SJeff Mahoney 288076dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 288176dda93cSYan, Zheng if (IS_ERR(vol_args)) 288276dda93cSYan, Zheng return PTR_ERR(vol_args); 288376dda93cSYan, Zheng 288476dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 288576dda93cSYan, Zheng namelen = strlen(vol_args->name); 288676dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 288776dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 288876dda93cSYan, Zheng err = -EINVAL; 288976dda93cSYan, Zheng goto out; 289076dda93cSYan, Zheng } 289176dda93cSYan, Zheng 2892a561be71SAl Viro err = mnt_want_write_file(file); 289376dda93cSYan, Zheng if (err) 289476dda93cSYan, Zheng goto out; 289576dda93cSYan, Zheng 2896521e0546SDavid Sterba 289700235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 289800235411SAl Viro if (err == -EINTR) 289900235411SAl Viro goto out_drop_write; 290076dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 290176dda93cSYan, Zheng if (IS_ERR(dentry)) { 290276dda93cSYan, Zheng err = PTR_ERR(dentry); 290376dda93cSYan, Zheng goto out_unlock_dir; 290476dda93cSYan, Zheng } 290576dda93cSYan, Zheng 29062b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 290776dda93cSYan, Zheng err = -ENOENT; 290876dda93cSYan, Zheng goto out_dput; 290976dda93cSYan, Zheng } 291076dda93cSYan, Zheng 29112b0143b5SDavid Howells inode = d_inode(dentry); 29124260f7c7SSage Weil dest = BTRFS_I(inode)->root; 29134260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 29144260f7c7SSage Weil /* 29154260f7c7SSage Weil * Regular user. Only allow this with a special mount 29164260f7c7SSage Weil * option, when the user has write+exec access to the 29174260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 29184260f7c7SSage Weil * allowed. 29194260f7c7SSage Weil * 29204260f7c7SSage Weil * Note that this is _not_ check that the subvol is 29214260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 29224260f7c7SSage Weil * otherwise be able to delete. 29234260f7c7SSage Weil * 29244260f7c7SSage Weil * Users who want to delete empty subvols should try 29254260f7c7SSage Weil * rmdir(2). 29264260f7c7SSage Weil */ 29274260f7c7SSage Weil err = -EPERM; 29280b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 29294260f7c7SSage Weil goto out_dput; 29304260f7c7SSage Weil 29314260f7c7SSage Weil /* 29324260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 29334260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 29344260f7c7SSage Weil * must be called on the dentry referencing the root 29354260f7c7SSage Weil * of the subvol, not a random directory contained 29364260f7c7SSage Weil * within it. 29374260f7c7SSage Weil */ 29384260f7c7SSage Weil err = -EINVAL; 29394260f7c7SSage Weil if (root == dest) 29404260f7c7SSage Weil goto out_dput; 29414260f7c7SSage Weil 29424260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 29434260f7c7SSage Weil if (err) 29444260f7c7SSage Weil goto out_dput; 29455c39da5bSMiao Xie } 29464260f7c7SSage Weil 29475c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 29484260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 29494260f7c7SSage Weil if (err) 29504260f7c7SSage Weil goto out_dput; 29514260f7c7SSage Weil 29524a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 295376dda93cSYan, Zheng err = -EINVAL; 295476dda93cSYan, Zheng goto out_dput; 295576dda93cSYan, Zheng } 295676dda93cSYan, Zheng 29575955102cSAl Viro inode_lock(inode); 2958f60a2364SMisono Tomohiro err = btrfs_delete_subvolume(dir, dentry); 29595955102cSAl Viro inode_unlock(inode); 296046008d9dSAmir Goldstein if (!err) { 296146008d9dSAmir Goldstein fsnotify_rmdir(dir, dentry); 296276dda93cSYan, Zheng d_delete(dentry); 296346008d9dSAmir Goldstein } 2964fa6ac876SLiu Bo 296576dda93cSYan, Zheng out_dput: 296676dda93cSYan, Zheng dput(dentry); 296776dda93cSYan, Zheng out_unlock_dir: 29685955102cSAl Viro inode_unlock(dir); 296900235411SAl Viro out_drop_write: 29702a79f17eSAl Viro mnt_drop_write_file(file); 297176dda93cSYan, Zheng out: 297276dda93cSYan, Zheng kfree(vol_args); 297376dda93cSYan, Zheng return err; 297476dda93cSYan, Zheng } 297576dda93cSYan, Zheng 29761e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2977f46b5a66SChristoph Hellwig { 2978496ad9aaSAl Viro struct inode *inode = file_inode(file); 2979f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 29801e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2981c146afadSYan Zheng int ret; 2982c146afadSYan Zheng 298325122d15SIlya Dryomov ret = mnt_want_write_file(file); 298425122d15SIlya Dryomov if (ret) 298525122d15SIlya Dryomov return ret; 2986b83cc969SLi Zefan 298725122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 298825122d15SIlya Dryomov ret = -EROFS; 298925122d15SIlya Dryomov goto out; 29905ac00addSStefan Behrens } 2991f46b5a66SChristoph Hellwig 2992f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2993f46b5a66SChristoph Hellwig case S_IFDIR: 2994e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2995e441d54dSChris Mason ret = -EPERM; 2996e441d54dSChris Mason goto out; 2997e441d54dSChris Mason } 2998de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 2999f46b5a66SChristoph Hellwig break; 3000f46b5a66SChristoph Hellwig case S_IFREG: 3001616d374eSAdam Borowski /* 3002616d374eSAdam Borowski * Note that this does not check the file descriptor for write 3003616d374eSAdam Borowski * access. This prevents defragmenting executables that are 3004616d374eSAdam Borowski * running and allows defrag on files open in read-only mode. 3005616d374eSAdam Borowski */ 3006616d374eSAdam Borowski if (!capable(CAP_SYS_ADMIN) && 3007616d374eSAdam Borowski inode_permission(inode, MAY_WRITE)) { 3008616d374eSAdam Borowski ret = -EPERM; 3009e441d54dSChris Mason goto out; 3010e441d54dSChris Mason } 30111e701a32SChris Mason 30121e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 30131e701a32SChris Mason if (!range) { 30141e701a32SChris Mason ret = -ENOMEM; 30151e701a32SChris Mason goto out; 30161e701a32SChris Mason } 30171e701a32SChris Mason 30181e701a32SChris Mason if (argp) { 30191e701a32SChris Mason if (copy_from_user(range, argp, 30201e701a32SChris Mason sizeof(*range))) { 30211e701a32SChris Mason ret = -EFAULT; 30221e701a32SChris Mason kfree(range); 3023683be16eSDan Carpenter goto out; 30241e701a32SChris Mason } 30251e701a32SChris Mason /* compression requires us to start the IO */ 30261e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 30271e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 30281e701a32SChris Mason range->extent_thresh = (u32)-1; 30291e701a32SChris Mason } 30301e701a32SChris Mason } else { 30311e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 30321e701a32SChris Mason range->len = (u64)-1; 30331e701a32SChris Mason } 3034496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 30357c829b72SAnand Jain range, BTRFS_OLDEST_GENERATION, 0); 30364cb5300bSChris Mason if (ret > 0) 30374cb5300bSChris Mason ret = 0; 30381e701a32SChris Mason kfree(range); 3039f46b5a66SChristoph Hellwig break; 30408929ecfaSYan, Zheng default: 30418929ecfaSYan, Zheng ret = -EINVAL; 3042f46b5a66SChristoph Hellwig } 3043e441d54dSChris Mason out: 304425122d15SIlya Dryomov mnt_drop_write_file(file); 3045e441d54dSChris Mason return ret; 3046f46b5a66SChristoph Hellwig } 3047f46b5a66SChristoph Hellwig 30482ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 3049f46b5a66SChristoph Hellwig { 3050f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3051f46b5a66SChristoph Hellwig int ret; 3052f46b5a66SChristoph Hellwig 3053e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3054e441d54dSChris Mason return -EPERM; 3055e441d54dSChris Mason 3056171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) 3057e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3058c9e9f97bSIlya Dryomov 3059dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3060c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3061c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3062c9e9f97bSIlya Dryomov goto out; 3063c9e9f97bSIlya Dryomov } 3064f46b5a66SChristoph Hellwig 30655516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 30662ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 3067f46b5a66SChristoph Hellwig 306843d20761SAnand Jain if (!ret) 30690b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 307043d20761SAnand Jain 3071f46b5a66SChristoph Hellwig kfree(vol_args); 3072c9e9f97bSIlya Dryomov out: 3073171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3074f46b5a66SChristoph Hellwig return ret; 3075f46b5a66SChristoph Hellwig } 3076f46b5a66SChristoph Hellwig 30776b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 3078f46b5a66SChristoph Hellwig { 30790b246afaSJeff Mahoney struct inode *inode = file_inode(file); 30800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 30816b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 3082f46b5a66SChristoph Hellwig int ret; 3083f46b5a66SChristoph Hellwig 3084e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3085e441d54dSChris Mason return -EPERM; 3086e441d54dSChris Mason 3087da24927bSMiao Xie ret = mnt_want_write_file(file); 3088da24927bSMiao Xie if (ret) 3089da24927bSMiao Xie return ret; 3090c146afadSYan Zheng 3091dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3092c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3093c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3094c47ca32dSDan Carpenter goto err_drop; 3095c9e9f97bSIlya Dryomov } 3096f46b5a66SChristoph Hellwig 30976b526ed7SAnand Jain /* Check for compatibility reject unknown flags */ 3098fd4e994bSOmar Sandoval if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) { 3099fd4e994bSOmar Sandoval ret = -EOPNOTSUPP; 3100fd4e994bSOmar Sandoval goto out; 3101fd4e994bSOmar Sandoval } 3102f46b5a66SChristoph Hellwig 3103171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3104183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3105183860f6SAnand Jain goto out; 3106183860f6SAnand Jain } 3107183860f6SAnand Jain 3108735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 31092ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); 31106b526ed7SAnand Jain } else { 31116b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 31122ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 31136b526ed7SAnand Jain } 3114171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3115183860f6SAnand Jain 31166b526ed7SAnand Jain if (!ret) { 3117735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 31180b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 31196b526ed7SAnand Jain vol_args->devid); 31206b526ed7SAnand Jain else 31210b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 31226b526ed7SAnand Jain vol_args->name); 31236b526ed7SAnand Jain } 3124183860f6SAnand Jain out: 3125183860f6SAnand Jain kfree(vol_args); 3126c47ca32dSDan Carpenter err_drop: 31274ac20c70SIlya Dryomov mnt_drop_write_file(file); 3128f46b5a66SChristoph Hellwig return ret; 3129f46b5a66SChristoph Hellwig } 3130f46b5a66SChristoph Hellwig 3131f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 3132f46b5a66SChristoph Hellwig { 31330b246afaSJeff Mahoney struct inode *inode = file_inode(file); 31340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3135f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3136f46b5a66SChristoph Hellwig int ret; 3137f46b5a66SChristoph Hellwig 3138f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3139f46b5a66SChristoph Hellwig return -EPERM; 3140f46b5a66SChristoph Hellwig 3141f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 3142f46b5a66SChristoph Hellwig if (ret) 3143f46b5a66SChristoph Hellwig return ret; 3144f46b5a66SChristoph Hellwig 3145171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3146f46b5a66SChristoph Hellwig ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 314758d7bbf8SDavid Sterba goto out_drop_write; 314858d7bbf8SDavid Sterba } 314958d7bbf8SDavid Sterba 315058d7bbf8SDavid Sterba vol_args = memdup_user(arg, sizeof(*vol_args)); 315158d7bbf8SDavid Sterba if (IS_ERR(vol_args)) { 315258d7bbf8SDavid Sterba ret = PTR_ERR(vol_args); 3153f46b5a66SChristoph Hellwig goto out; 3154f46b5a66SChristoph Hellwig } 3155f46b5a66SChristoph Hellwig 315658d7bbf8SDavid Sterba vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 31572ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 3158f46b5a66SChristoph Hellwig 3159f46b5a66SChristoph Hellwig if (!ret) 31600b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 3161f46b5a66SChristoph Hellwig kfree(vol_args); 316258d7bbf8SDavid Sterba out: 3163171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 316458d7bbf8SDavid Sterba out_drop_write: 3165f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 316658d7bbf8SDavid Sterba 3167f46b5a66SChristoph Hellwig return ret; 3168f46b5a66SChristoph Hellwig } 3169f46b5a66SChristoph Hellwig 31702ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 31712ff7e61eSJeff Mahoney void __user *arg) 3172475f6387SJan Schmidt { 3173027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 3174475f6387SJan Schmidt struct btrfs_device *device; 31750b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3176027ed2f0SLi Zefan int ret = 0; 3177475f6387SJan Schmidt 3178027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 3179027ed2f0SLi Zefan if (!fi_args) 3180027ed2f0SLi Zefan return -ENOMEM; 3181027ed2f0SLi Zefan 3182d03262c7SDavid Sterba rcu_read_lock(); 3183027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 3184475f6387SJan Schmidt 3185d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 3186027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 3187027ed2f0SLi Zefan fi_args->max_id = device->devid; 3188475f6387SJan Schmidt } 3189d03262c7SDavid Sterba rcu_read_unlock(); 3190475f6387SJan Schmidt 3191de37aa51SNikolay Borisov memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); 3192bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 3193bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 3194bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 319580a773fbSDavid Sterba 3196027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 3197027ed2f0SLi Zefan ret = -EFAULT; 3198475f6387SJan Schmidt 3199027ed2f0SLi Zefan kfree(fi_args); 3200027ed2f0SLi Zefan return ret; 3201475f6387SJan Schmidt } 3202475f6387SJan Schmidt 32032ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 32042ff7e61eSJeff Mahoney void __user *arg) 3205475f6387SJan Schmidt { 3206475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 3207475f6387SJan Schmidt struct btrfs_device *dev; 3208475f6387SJan Schmidt int ret = 0; 3209475f6387SJan Schmidt char *s_uuid = NULL; 3210475f6387SJan Schmidt 3211475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 3212475f6387SJan Schmidt if (IS_ERR(di_args)) 3213475f6387SJan Schmidt return PTR_ERR(di_args); 3214475f6387SJan Schmidt 3215dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 3216475f6387SJan Schmidt s_uuid = di_args->uuid; 3217475f6387SJan Schmidt 3218c5593ca3SDavid Sterba rcu_read_lock(); 3219e4319cd9SAnand Jain dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid, 322009ba3bc9SAnand Jain NULL, true); 3221475f6387SJan Schmidt 3222475f6387SJan Schmidt if (!dev) { 3223475f6387SJan Schmidt ret = -ENODEV; 3224475f6387SJan Schmidt goto out; 3225475f6387SJan Schmidt } 3226475f6387SJan Schmidt 3227475f6387SJan Schmidt di_args->devid = dev->devid; 32287cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 32297cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 3230475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 3231a27202fbSJim Meyering if (dev->name) { 3232672d5990SMisono Tomohiro strncpy(di_args->path, rcu_str_deref(dev->name), 3233672d5990SMisono Tomohiro sizeof(di_args->path) - 1); 3234a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 3235a27202fbSJim Meyering } else { 323699ba55adSStefan Behrens di_args->path[0] = '\0'; 3237a27202fbSJim Meyering } 3238475f6387SJan Schmidt 3239475f6387SJan Schmidt out: 3240c5593ca3SDavid Sterba rcu_read_unlock(); 3241475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 3242475f6387SJan Schmidt ret = -EFAULT; 3243475f6387SJan Schmidt 3244475f6387SJan Schmidt kfree(di_args); 3245475f6387SJan Schmidt return ret; 3246475f6387SJan Schmidt } 3247475f6387SJan Schmidt 3248d8b55242SFilipe Manana static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 3249d8b55242SFilipe Manana struct inode *inode2, u64 loff2, u64 len) 3250d8b55242SFilipe Manana { 3251d8b55242SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 3252d8b55242SFilipe Manana unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 3253d8b55242SFilipe Manana } 3254d8b55242SFilipe Manana 3255d8b55242SFilipe Manana static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 3256d8b55242SFilipe Manana struct inode *inode2, u64 loff2, u64 len) 3257d8b55242SFilipe Manana { 3258d8b55242SFilipe Manana if (inode1 < inode2) { 3259d8b55242SFilipe Manana swap(inode1, inode2); 3260d8b55242SFilipe Manana swap(loff1, loff2); 3261d8b55242SFilipe Manana } else if (inode1 == inode2 && loff2 < loff1) { 3262d8b55242SFilipe Manana swap(loff1, loff2); 3263d8b55242SFilipe Manana } 3264d8b55242SFilipe Manana lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 3265d8b55242SFilipe Manana lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 3266d8b55242SFilipe Manana } 3267d8b55242SFilipe Manana 326857a50e25SFilipe Manana static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len, 326934a28e3dSFilipe Manana struct inode *dst, u64 dst_loff) 3270416161dbSMark Fasheh { 3271831d2fa2SFilipe Manana const u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 3272416161dbSMark Fasheh int ret; 32730efa9f48SMark Fasheh 3274de02b9f6SFilipe Manana /* 3275d8b55242SFilipe Manana * Lock destination range to serialize with concurrent readpages() and 3276d8b55242SFilipe Manana * source range to serialize with relocation. 3277de02b9f6SFilipe Manana */ 3278d8b55242SFilipe Manana btrfs_double_extent_lock(src, loff, dst, dst_loff, len); 3279831d2fa2SFilipe Manana ret = btrfs_clone(src, dst, loff, len, ALIGN(len, bs), dst_loff, 1); 3280d8b55242SFilipe Manana btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 32813973909dSTimofey Titovets 32823973909dSTimofey Titovets return ret; 32833973909dSTimofey Titovets } 32843973909dSTimofey Titovets 3285b6728768STimofey Titovets #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3286b6728768STimofey Titovets 32873973909dSTimofey Titovets static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 32883973909dSTimofey Titovets struct inode *dst, u64 dst_loff) 32893973909dSTimofey Titovets { 32903973909dSTimofey Titovets int ret; 3291b6728768STimofey Titovets u64 i, tail_len, chunk_count; 329262d54f3aSFilipe Manana struct btrfs_root *root_dst = BTRFS_I(dst)->root; 329362d54f3aSFilipe Manana 329462d54f3aSFilipe Manana spin_lock(&root_dst->root_item_lock); 329562d54f3aSFilipe Manana if (root_dst->send_in_progress) { 329662d54f3aSFilipe Manana btrfs_warn_rl(root_dst->fs_info, 329762d54f3aSFilipe Manana "cannot deduplicate to root %llu while send operations are using it (%d in progress)", 329862d54f3aSFilipe Manana root_dst->root_key.objectid, 329962d54f3aSFilipe Manana root_dst->send_in_progress); 330062d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 330162d54f3aSFilipe Manana return -EAGAIN; 330262d54f3aSFilipe Manana } 330362d54f3aSFilipe Manana root_dst->dedupe_in_progress++; 330462d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 33053973909dSTimofey Titovets 3306b6728768STimofey Titovets tail_len = olen % BTRFS_MAX_DEDUPE_LEN; 3307b6728768STimofey Titovets chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); 330867b07bd4STimofey Titovets 3309b6728768STimofey Titovets for (i = 0; i < chunk_count; i++) { 3310b6728768STimofey Titovets ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, 331134a28e3dSFilipe Manana dst, dst_loff); 3312b6728768STimofey Titovets if (ret) 331362d54f3aSFilipe Manana goto out; 3314b6728768STimofey Titovets 3315b6728768STimofey Titovets loff += BTRFS_MAX_DEDUPE_LEN; 3316b6728768STimofey Titovets dst_loff += BTRFS_MAX_DEDUPE_LEN; 3317b6728768STimofey Titovets } 3318b6728768STimofey Titovets 3319b6728768STimofey Titovets if (tail_len > 0) 332067b07bd4STimofey Titovets ret = btrfs_extent_same_range(src, loff, tail_len, dst, 332134a28e3dSFilipe Manana dst_loff); 332262d54f3aSFilipe Manana out: 332362d54f3aSFilipe Manana spin_lock(&root_dst->root_item_lock); 332462d54f3aSFilipe Manana root_dst->dedupe_in_progress--; 332562d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 3326416161dbSMark Fasheh 3327416161dbSMark Fasheh return ret; 3328416161dbSMark Fasheh } 3329416161dbSMark Fasheh 3330f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3331f82a9901SFilipe Manana struct inode *inode, 3332f82a9901SFilipe Manana u64 endoff, 3333f82a9901SFilipe Manana const u64 destoff, 33341c919a5eSMark Fasheh const u64 olen, 33351c919a5eSMark Fasheh int no_time_update) 3336f82a9901SFilipe Manana { 3337f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3338f82a9901SFilipe Manana int ret; 3339f82a9901SFilipe Manana 3340f82a9901SFilipe Manana inode_inc_iversion(inode); 33411c919a5eSMark Fasheh if (!no_time_update) 3342c2050a45SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 3343f82a9901SFilipe Manana /* 3344f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3345f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3346f82a9901SFilipe Manana */ 3347f82a9901SFilipe Manana if (endoff > destoff + olen) 3348f82a9901SFilipe Manana endoff = destoff + olen; 3349790a1d44SJosef Bacik if (endoff > inode->i_size) { 3350790a1d44SJosef Bacik i_size_write(inode, endoff); 3351d923afe9SJosef Bacik btrfs_inode_safe_disk_i_size_write(inode, 0); 3352790a1d44SJosef Bacik } 3353f82a9901SFilipe Manana 3354f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3355f82a9901SFilipe Manana if (ret) { 335666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 33573a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3358f82a9901SFilipe Manana goto out; 3359f82a9901SFilipe Manana } 33603a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 3361f82a9901SFilipe Manana out: 3362f82a9901SFilipe Manana return ret; 3363f82a9901SFilipe Manana } 3364f82a9901SFilipe Manana 33658039d87dSFilipe Manana /* 33668039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 33678039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 33688039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 33698039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 33708039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 33718039d87dSFilipe Manana * 33728039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 33738039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 33748039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 33758039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 33768039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 33778039d87dSFilipe Manana * i_mutex, which we are holding here). 33788039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 33798039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 33808039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 33818039d87dSFilipe Manana * 33828039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 33838039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 33848039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 33858039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 33868039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 33878039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 33888039d87dSFilipe Manana * the former. 33898039d87dSFilipe Manana */ 33904a0ab9d7SDavid Sterba static int clone_copy_inline_extent(struct inode *dst, 33918039d87dSFilipe Manana struct btrfs_trans_handle *trans, 33928039d87dSFilipe Manana struct btrfs_path *path, 33938039d87dSFilipe Manana struct btrfs_key *new_key, 33948039d87dSFilipe Manana const u64 drop_start, 33958039d87dSFilipe Manana const u64 datal, 33968039d87dSFilipe Manana const u64 skip, 33978039d87dSFilipe Manana const u64 size, 33988039d87dSFilipe Manana char *inline_data) 33998039d87dSFilipe Manana { 34000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); 34018039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 34028039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 34030b246afaSJeff Mahoney fs_info->sectorsize); 34048039d87dSFilipe Manana int ret; 34058039d87dSFilipe Manana struct btrfs_key key; 34068039d87dSFilipe Manana 34078039d87dSFilipe Manana if (new_key->offset > 0) 34088039d87dSFilipe Manana return -EOPNOTSUPP; 34098039d87dSFilipe Manana 34104a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(dst)); 34118039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 34128039d87dSFilipe Manana key.offset = 0; 34138039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 34148039d87dSFilipe Manana if (ret < 0) { 34158039d87dSFilipe Manana return ret; 34168039d87dSFilipe Manana } else if (ret > 0) { 34178039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 34188039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 34198039d87dSFilipe Manana if (ret < 0) 34208039d87dSFilipe Manana return ret; 34218039d87dSFilipe Manana else if (ret > 0) 34228039d87dSFilipe Manana goto copy_inline_extent; 34238039d87dSFilipe Manana } 34248039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 34254a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 34268039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 34278039d87dSFilipe Manana ASSERT(key.offset > 0); 34288039d87dSFilipe Manana return -EOPNOTSUPP; 34298039d87dSFilipe Manana } 34308039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 34318039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 34328039d87dSFilipe Manana u64 ext_len; 34338039d87dSFilipe Manana 34348039d87dSFilipe Manana /* 34358039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 34368039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 34378039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 34388039d87dSFilipe Manana */ 34398039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 34408039d87dSFilipe Manana struct btrfs_file_extent_item); 34418039d87dSFilipe Manana /* 34428039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 34438039d87dSFilipe Manana * following it. 34448039d87dSFilipe Manana */ 34458039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 34468039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 34478039d87dSFilipe Manana goto copy_inline_extent; 34488039d87dSFilipe Manana 34498039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 34508039d87dSFilipe Manana if (ext_len > aligned_end) 34518039d87dSFilipe Manana return -EOPNOTSUPP; 34528039d87dSFilipe Manana 34538039d87dSFilipe Manana ret = btrfs_next_item(root, path); 34548039d87dSFilipe Manana if (ret < 0) { 34558039d87dSFilipe Manana return ret; 34568039d87dSFilipe Manana } else if (ret == 0) { 34578039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 34588039d87dSFilipe Manana path->slots[0]); 34594a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 34608039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 34618039d87dSFilipe Manana return -EOPNOTSUPP; 34628039d87dSFilipe Manana } 34638039d87dSFilipe Manana } 34648039d87dSFilipe Manana 34658039d87dSFilipe Manana copy_inline_extent: 34668039d87dSFilipe Manana /* 34678039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 34688039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 34698039d87dSFilipe Manana */ 34708039d87dSFilipe Manana if (i_size_read(dst) > datal) { 34718039d87dSFilipe Manana /* 34728039d87dSFilipe Manana * If the destination inode has an inline extent... 34738039d87dSFilipe Manana * This would require copying the data from the source inline 34748039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 34758039d87dSFilipe Manana * But this is really complex, both extents can be compressed 34768039d87dSFilipe Manana * or just one of them, which would require decompressing and 34778039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 34788039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 34798039d87dSFilipe Manana * inline extent). 34808039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 34818039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 34828039d87dSFilipe Manana */ 34838039d87dSFilipe Manana return -EOPNOTSUPP; 34848039d87dSFilipe Manana } 34858039d87dSFilipe Manana 34868039d87dSFilipe Manana btrfs_release_path(path); 34878039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 34888039d87dSFilipe Manana if (ret) 34898039d87dSFilipe Manana return ret; 34908039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 34918039d87dSFilipe Manana if (ret) 34928039d87dSFilipe Manana return ret; 34938039d87dSFilipe Manana 34948039d87dSFilipe Manana if (skip) { 34958039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 34968039d87dSFilipe Manana 34978039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 34988039d87dSFilipe Manana } 34998039d87dSFilipe Manana 35008039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 35018039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 35028039d87dSFilipe Manana path->slots[0]), 35038039d87dSFilipe Manana size); 35048039d87dSFilipe Manana inode_add_bytes(dst, datal); 3505690a5dbfSFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags); 35068039d87dSFilipe Manana 35078039d87dSFilipe Manana return 0; 35088039d87dSFilipe Manana } 35098039d87dSFilipe Manana 351032b7c687SMark Fasheh /** 351132b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 351232b7c687SMark Fasheh * 351332b7c687SMark Fasheh * @src: Inode to clone from 351432b7c687SMark Fasheh * @inode: Inode to clone to 351532b7c687SMark Fasheh * @off: Offset within source to start clone from 351632b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 35171c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 351832b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 35191c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 352032b7c687SMark Fasheh */ 352132b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3522f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 35231c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3524f46b5a66SChristoph Hellwig { 35250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3526f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 352732b7c687SMark Fasheh struct btrfs_path *path = NULL; 3528f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 352932b7c687SMark Fasheh struct btrfs_trans_handle *trans; 353032b7c687SMark Fasheh char *buf = NULL; 3531ae01a0abSYan Zheng struct btrfs_key key; 3532f46b5a66SChristoph Hellwig u32 nritems; 3533f46b5a66SChristoph Hellwig int slot; 3534ae01a0abSYan Zheng int ret; 3535f82a9901SFilipe Manana const u64 len = olen_aligned; 3536f82a9901SFilipe Manana u64 last_dest_end = destoff; 3537ae01a0abSYan Zheng 3538ae01a0abSYan Zheng ret = -ENOMEM; 3539752ade68SMichal Hocko buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 3540ae01a0abSYan Zheng if (!buf) 354132b7c687SMark Fasheh return ret; 3542ae01a0abSYan Zheng 3543ae01a0abSYan Zheng path = btrfs_alloc_path(); 3544ae01a0abSYan Zheng if (!path) { 354515351955SDavid Sterba kvfree(buf); 354632b7c687SMark Fasheh return ret; 3547ae01a0abSYan Zheng } 354832b7c687SMark Fasheh 3549e4058b54SDavid Sterba path->reada = READA_FORWARD; 3550c5c9cd4dSSage Weil /* clone data */ 35514a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(src)); 3552ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 35532c463823SFilipe Manana key.offset = off; 3554f46b5a66SChristoph Hellwig 3555f46b5a66SChristoph Hellwig while (1) { 3556de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3557b64119b5SFilipe Manana struct btrfs_file_extent_item *extent; 3558b64119b5SFilipe Manana int type; 3559b64119b5SFilipe Manana u32 size; 3560b64119b5SFilipe Manana struct btrfs_key new_key; 3561b64119b5SFilipe Manana u64 disko = 0, diskl = 0; 3562b64119b5SFilipe Manana u64 datao = 0, datal = 0; 3563b64119b5SFilipe Manana u8 comp; 3564b64119b5SFilipe Manana u64 drop_start; 3565df858e76SFilipe Manana 3566f46b5a66SChristoph Hellwig /* 3567f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3568f46b5a66SChristoph Hellwig * tree. 3569f46b5a66SChristoph Hellwig */ 3570e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3571362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3572362a20c5SDavid Sterba 0, 0); 3573f46b5a66SChristoph Hellwig if (ret < 0) 3574f46b5a66SChristoph Hellwig goto out; 35752c463823SFilipe Manana /* 35762c463823SFilipe Manana * First search, if no extent item that starts at offset off was 35772c463823SFilipe Manana * found but the previous item is an extent item, it's possible 35782c463823SFilipe Manana * it might overlap our target range, therefore process it. 35792c463823SFilipe Manana */ 35802c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 35812c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 35822c463823SFilipe Manana path->slots[0] - 1); 35832c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 35842c463823SFilipe Manana path->slots[0]--; 35852c463823SFilipe Manana } 3586f46b5a66SChristoph Hellwig 3587ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3588e4355f34SFilipe David Borba Manana process_slot: 3589ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3590362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3591f46b5a66SChristoph Hellwig if (ret < 0) 3592f46b5a66SChristoph Hellwig goto out; 3593f46b5a66SChristoph Hellwig if (ret > 0) 3594f46b5a66SChristoph Hellwig break; 3595ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3596f46b5a66SChristoph Hellwig } 3597f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3598f46b5a66SChristoph Hellwig slot = path->slots[0]; 3599f46b5a66SChristoph Hellwig 3600ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3601962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 36024a0cc7caSNikolay Borisov key.objectid != btrfs_ino(BTRFS_I(src))) 3603f46b5a66SChristoph Hellwig break; 3604f46b5a66SChristoph Hellwig 3605b64119b5SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 360631840ae1SZheng Yan 3607c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3608c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3609c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3610c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3611c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3612c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3613b64119b5SFilipe Manana disko = btrfs_file_extent_disk_bytenr(leaf, extent); 3614b64119b5SFilipe Manana diskl = btrfs_file_extent_disk_num_bytes(leaf, extent); 3615c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3616b64119b5SFilipe Manana datal = btrfs_file_extent_num_bytes(leaf, extent); 3617c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3618b64119b5SFilipe Manana /* Take upper bound, may be compressed */ 3619b64119b5SFilipe Manana datal = btrfs_file_extent_ram_bytes(leaf, extent); 3620c5c9cd4dSSage Weil } 362131840ae1SZheng Yan 36222c463823SFilipe Manana /* 3623b64119b5SFilipe Manana * The first search might have left us at an extent item that 3624b64119b5SFilipe Manana * ends before our target range's start, can happen if we have 3625b64119b5SFilipe Manana * holes and NO_HOLES feature enabled. 36262c463823SFilipe Manana */ 36272c463823SFilipe Manana if (key.offset + datal <= off) { 3628e4355f34SFilipe David Borba Manana path->slots[0]++; 3629e4355f34SFilipe David Borba Manana goto process_slot; 36302c463823SFilipe Manana } else if (key.offset >= off + len) { 36312c463823SFilipe Manana break; 3632e4355f34SFilipe David Borba Manana } 3633df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 3634e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3635b64119b5SFilipe Manana read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot), 3636e4355f34SFilipe David Borba Manana size); 3637e4355f34SFilipe David Borba Manana 3638e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3639e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3640c5c9cd4dSSage Weil 364131840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 36424a0cc7caSNikolay Borisov new_key.objectid = btrfs_ino(BTRFS_I(inode)); 36434d728ec7SLi Zefan if (off <= key.offset) 3644c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 36454d728ec7SLi Zefan else 36464d728ec7SLi Zefan new_key.offset = destoff; 3647c5c9cd4dSSage Weil 3648b6f3409bSSage Weil /* 3649b64119b5SFilipe Manana * Deal with a hole that doesn't have an extent item that 3650b64119b5SFilipe Manana * represents it (NO_HOLES feature enabled). 3651b64119b5SFilipe Manana * This hole is either in the middle of the cloning range or at 3652b64119b5SFilipe Manana * the beginning (fully overlaps it or partially overlaps it). 3653f82a9901SFilipe Manana */ 3654f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3655f82a9901SFilipe Manana drop_start = last_dest_end; 3656f82a9901SFilipe Manana else 3657f82a9901SFilipe Manana drop_start = new_key.offset; 3658f82a9901SFilipe Manana 3659c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3660c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3661690a5dbfSFilipe Manana struct btrfs_clone_extent_info clone_info; 3662690a5dbfSFilipe Manana 3663d72c0842SLi Zefan /* 3664d72c0842SLi Zefan * a | --- range to clone ---| b 3665d72c0842SLi Zefan * | ------------- extent ------------- | 3666d72c0842SLi Zefan */ 3667d72c0842SLi Zefan 3668b64119b5SFilipe Manana /* Subtract range b */ 3669d72c0842SLi Zefan if (key.offset + datal > off + len) 3670d72c0842SLi Zefan datal = off + len - key.offset; 3671d72c0842SLi Zefan 3672b64119b5SFilipe Manana /* Subtract range a */ 3673a22285a6SYan, Zheng if (off > key.offset) { 3674a22285a6SYan, Zheng datao += off - key.offset; 3675a22285a6SYan, Zheng datal -= off - key.offset; 3676a22285a6SYan, Zheng } 3677a22285a6SYan, Zheng 3678690a5dbfSFilipe Manana clone_info.disk_offset = disko; 3679690a5dbfSFilipe Manana clone_info.disk_len = diskl; 3680690a5dbfSFilipe Manana clone_info.data_offset = datao; 3681690a5dbfSFilipe Manana clone_info.data_len = datal; 3682690a5dbfSFilipe Manana clone_info.file_offset = new_key.offset; 3683690a5dbfSFilipe Manana clone_info.extent_buf = buf; 3684690a5dbfSFilipe Manana clone_info.item_size = size; 3685690a5dbfSFilipe Manana ret = btrfs_punch_hole_range(inode, path, 3686f82a9901SFilipe Manana drop_start, 3687690a5dbfSFilipe Manana new_key.offset + datal - 1, 3688690a5dbfSFilipe Manana &clone_info, &trans); 3689690a5dbfSFilipe Manana if (ret) 369079787eaaSJeff Mahoney goto out; 3691c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3692c5c9cd4dSSage Weil u64 skip = 0; 3693c5c9cd4dSSage Weil u64 trim = 0; 3694ed958762SFilipe Manana 3695c5c9cd4dSSage Weil if (off > key.offset) { 3696c5c9cd4dSSage Weil skip = off - key.offset; 3697c5c9cd4dSSage Weil new_key.offset += skip; 369831840ae1SZheng Yan } 3699d397712bSChris Mason 3700c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3701c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3702d397712bSChris Mason 3703c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3704c5c9cd4dSSage Weil ret = -EINVAL; 3705c5c9cd4dSSage Weil goto out; 370631840ae1SZheng Yan } 3707c5c9cd4dSSage Weil size -= skip + trim; 3708c5c9cd4dSSage Weil datal -= skip + trim; 3709a22285a6SYan, Zheng 3710690a5dbfSFilipe Manana /* 3711b64119b5SFilipe Manana * If our extent is inline, we know we will drop or 3712b64119b5SFilipe Manana * adjust at most 1 extent item in the destination root. 3713690a5dbfSFilipe Manana * 3714b64119b5SFilipe Manana * 1 - adjusting old extent (we may have to split it) 3715690a5dbfSFilipe Manana * 1 - add new extent 3716690a5dbfSFilipe Manana * 1 - inode update 3717690a5dbfSFilipe Manana */ 3718690a5dbfSFilipe Manana trans = btrfs_start_transaction(root, 3); 3719690a5dbfSFilipe Manana if (IS_ERR(trans)) { 3720690a5dbfSFilipe Manana ret = PTR_ERR(trans); 3721690a5dbfSFilipe Manana goto out; 3722690a5dbfSFilipe Manana } 3723690a5dbfSFilipe Manana 3724b64119b5SFilipe Manana ret = clone_copy_inline_extent(inode, trans, path, 3725b64119b5SFilipe Manana &new_key, drop_start, 3726b64119b5SFilipe Manana datal, skip, size, buf); 372779787eaaSJeff Mahoney if (ret) { 37283f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 3729b64119b5SFilipe Manana btrfs_abort_transaction(trans, ret); 37303a45bb20SJeff Mahoney btrfs_end_transaction(trans); 373179787eaaSJeff Mahoney goto out; 373279787eaaSJeff Mahoney } 3733c5c9cd4dSSage Weil } 3734c5c9cd4dSSage Weil 3735b3b4aa74SDavid Sterba btrfs_release_path(path); 3736c5c9cd4dSSage Weil 373762e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 37380b246afaSJeff Mahoney fs_info->sectorsize); 3739b64119b5SFilipe Manana ret = clone_finish_inode_update(trans, inode, last_dest_end, 3740b64119b5SFilipe Manana destoff, olen, no_time_update); 3741f82a9901SFilipe Manana if (ret) 374279787eaaSJeff Mahoney goto out; 37432c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 37442c463823SFilipe Manana break; 3745b64119b5SFilipe Manana 3746b3b4aa74SDavid Sterba btrfs_release_path(path); 3747df858e76SFilipe Manana key.offset = next_key_min_offset; 374869ae5e44SWang Xiaoguang 374969ae5e44SWang Xiaoguang if (fatal_signal_pending(current)) { 375069ae5e44SWang Xiaoguang ret = -EINTR; 375169ae5e44SWang Xiaoguang goto out; 375269ae5e44SWang Xiaoguang } 3753ae01a0abSYan Zheng } 3754f46b5a66SChristoph Hellwig ret = 0; 375532b7c687SMark Fasheh 3756f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3757f82a9901SFilipe Manana /* 3758147271e3SFilipe Manana * We have an implicit hole that fully or partially overlaps our 3759147271e3SFilipe Manana * cloning range at its end. This means that we either have the 3760147271e3SFilipe Manana * NO_HOLES feature enabled or the implicit hole happened due to 3761147271e3SFilipe Manana * mixing buffered and direct IO writes against this file. 3762f82a9901SFilipe Manana */ 3763b3b4aa74SDavid Sterba btrfs_release_path(path); 3764690a5dbfSFilipe Manana path->leave_spinning = 0; 3765f82a9901SFilipe Manana 3766690a5dbfSFilipe Manana ret = btrfs_punch_hole_range(inode, path, 3767690a5dbfSFilipe Manana last_dest_end, destoff + len - 1, 3768147271e3SFilipe Manana NULL, &trans); 3769690a5dbfSFilipe Manana if (ret) 3770f82a9901SFilipe Manana goto out; 3771690a5dbfSFilipe Manana 3772f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 37731c919a5eSMark Fasheh destoff, olen, no_time_update); 3774f82a9901SFilipe Manana } 3775f82a9901SFilipe Manana 3776f46b5a66SChristoph Hellwig out: 377732b7c687SMark Fasheh btrfs_free_path(path); 377815351955SDavid Sterba kvfree(buf); 377932b7c687SMark Fasheh return ret; 378032b7c687SMark Fasheh } 378132b7c687SMark Fasheh 37823db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 378332b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 378432b7c687SMark Fasheh { 378554563d41SAl Viro struct inode *inode = file_inode(file); 37863db11b2eSZach Brown struct inode *src = file_inode(file_src); 37870b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 378832b7c687SMark Fasheh int ret; 378932b7c687SMark Fasheh u64 len = olen; 37900b246afaSJeff Mahoney u64 bs = fs_info->sb->s_blocksize; 379132b7c687SMark Fasheh 379232b7c687SMark Fasheh /* 379332b7c687SMark Fasheh * TODO: 379432b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 379532b7c687SMark Fasheh * decompress into destination's address_space (the file offset 379632b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 379732b7c687SMark Fasheh * otherwise reinsert) a subrange. 379800fdf13aSLiu Bo * 379900fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 380000fdf13aSLiu Bo * be either compressed or non-compressed. 380132b7c687SMark Fasheh */ 380232b7c687SMark Fasheh 3803ac765f83SFilipe Manana /* 380434a28e3dSFilipe Manana * VFS's generic_remap_file_range_prep() protects us from cloning the 380534a28e3dSFilipe Manana * eof block into the middle of a file, which would result in corruption 380634a28e3dSFilipe Manana * if the file size is not blocksize aligned. So we don't need to check 380734a28e3dSFilipe Manana * for that case here. 3808ac765f83SFilipe Manana */ 380934a28e3dSFilipe Manana if (off + len == src->i_size) 381032b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 381132b7c687SMark Fasheh 381232b7c687SMark Fasheh if (destoff > inode->i_size) { 3813f7fa1107SFilipe Manana const u64 wb_start = ALIGN_DOWN(inode->i_size, bs); 3814f7fa1107SFilipe Manana 381532b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 381632b7c687SMark Fasheh if (ret) 381734a28e3dSFilipe Manana return ret; 3818f7fa1107SFilipe Manana /* 3819f7fa1107SFilipe Manana * We may have truncated the last block if the inode's size is 3820f7fa1107SFilipe Manana * not sector size aligned, so we need to wait for writeback to 3821f7fa1107SFilipe Manana * complete before proceeding further, otherwise we can race 3822f7fa1107SFilipe Manana * with cloning and attempt to increment a reference to an 3823f7fa1107SFilipe Manana * extent that no longer exists (writeback completed right after 3824f7fa1107SFilipe Manana * we found the previous extent covering eof and before we 3825f7fa1107SFilipe Manana * attempted to increment its reference count). 3826f7fa1107SFilipe Manana */ 3827f7fa1107SFilipe Manana ret = btrfs_wait_ordered_range(inode, wb_start, 3828f7fa1107SFilipe Manana destoff - wb_start); 3829f7fa1107SFilipe Manana if (ret) 3830f7fa1107SFilipe Manana return ret; 383132b7c687SMark Fasheh } 383232b7c687SMark Fasheh 3833c125b8bfSFilipe Manana /* 3834d8b55242SFilipe Manana * Lock destination range to serialize with concurrent readpages() and 3835d8b55242SFilipe Manana * source range to serialize with relocation. 3836c125b8bfSFilipe Manana */ 3837d8b55242SFilipe Manana btrfs_double_extent_lock(src, off, inode, destoff, len); 38381c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 3839d8b55242SFilipe Manana btrfs_double_extent_unlock(src, off, inode, destoff, len); 3840c125b8bfSFilipe Manana /* 3841c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3842c125b8bfSFilipe Manana * data immediately and not the previous data. 3843c125b8bfSFilipe Manana */ 384465bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 384509cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 384609cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 384734a28e3dSFilipe Manana 384834a28e3dSFilipe Manana return ret; 384934a28e3dSFilipe Manana } 385034a28e3dSFilipe Manana 385134a28e3dSFilipe Manana static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, 385234a28e3dSFilipe Manana struct file *file_out, loff_t pos_out, 385334a28e3dSFilipe Manana loff_t *len, unsigned int remap_flags) 385434a28e3dSFilipe Manana { 385534a28e3dSFilipe Manana struct inode *inode_in = file_inode(file_in); 385634a28e3dSFilipe Manana struct inode *inode_out = file_inode(file_out); 385734a28e3dSFilipe Manana u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize; 385834a28e3dSFilipe Manana bool same_inode = inode_out == inode_in; 385934a28e3dSFilipe Manana u64 wb_len; 386034a28e3dSFilipe Manana int ret; 386134a28e3dSFilipe Manana 386234a28e3dSFilipe Manana if (!(remap_flags & REMAP_FILE_DEDUP)) { 386334a28e3dSFilipe Manana struct btrfs_root *root_out = BTRFS_I(inode_out)->root; 386434a28e3dSFilipe Manana 386534a28e3dSFilipe Manana if (btrfs_root_readonly(root_out)) 386634a28e3dSFilipe Manana return -EROFS; 386734a28e3dSFilipe Manana 386834a28e3dSFilipe Manana if (file_in->f_path.mnt != file_out->f_path.mnt || 386934a28e3dSFilipe Manana inode_in->i_sb != inode_out->i_sb) 387034a28e3dSFilipe Manana return -EXDEV; 387134a28e3dSFilipe Manana } 387234a28e3dSFilipe Manana 3873500710d3SFilipe Manana /* don't make the dst file partly checksummed */ 3874500710d3SFilipe Manana if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) != 3875500710d3SFilipe Manana (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) { 38767984ae52SGoldwyn Rodrigues return -EINVAL; 3877500710d3SFilipe Manana } 3878500710d3SFilipe Manana 387934a28e3dSFilipe Manana /* 388034a28e3dSFilipe Manana * Now that the inodes are locked, we need to start writeback ourselves 388134a28e3dSFilipe Manana * and can not rely on the writeback from the VFS's generic helper 388234a28e3dSFilipe Manana * generic_remap_file_range_prep() because: 388334a28e3dSFilipe Manana * 388434a28e3dSFilipe Manana * 1) For compression we must call filemap_fdatawrite_range() range 388534a28e3dSFilipe Manana * twice (btrfs_fdatawrite_range() does it for us), and the generic 388634a28e3dSFilipe Manana * helper only calls it once; 388734a28e3dSFilipe Manana * 388834a28e3dSFilipe Manana * 2) filemap_fdatawrite_range(), called by the generic helper only 388934a28e3dSFilipe Manana * waits for the writeback to complete, i.e. for IO to be done, and 389034a28e3dSFilipe Manana * not for the ordered extents to complete. We need to wait for them 389134a28e3dSFilipe Manana * to complete so that new file extent items are in the fs tree. 389234a28e3dSFilipe Manana */ 389334a28e3dSFilipe Manana if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) 389434a28e3dSFilipe Manana wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs); 389534a28e3dSFilipe Manana else 389634a28e3dSFilipe Manana wb_len = ALIGN(*len, bs); 389734a28e3dSFilipe Manana 389834a28e3dSFilipe Manana /* 389934a28e3dSFilipe Manana * Since we don't lock ranges, wait for ongoing lockless dio writes (as 390034a28e3dSFilipe Manana * any in progress could create its ordered extents after we wait for 390134a28e3dSFilipe Manana * existing ordered extents below). 390234a28e3dSFilipe Manana */ 390334a28e3dSFilipe Manana inode_dio_wait(inode_in); 390434a28e3dSFilipe Manana if (!same_inode) 390534a28e3dSFilipe Manana inode_dio_wait(inode_out); 390634a28e3dSFilipe Manana 3907a94d1d0cSQu Wenruo /* 3908a94d1d0cSQu Wenruo * Workaround to make sure NOCOW buffered write reach disk as NOCOW. 3909a94d1d0cSQu Wenruo * 3910a94d1d0cSQu Wenruo * Btrfs' back references do not have a block level granularity, they 3911a94d1d0cSQu Wenruo * work at the whole extent level. 3912a94d1d0cSQu Wenruo * NOCOW buffered write without data space reserved may not be able 3913a94d1d0cSQu Wenruo * to fall back to CoW due to lack of data space, thus could cause 3914a94d1d0cSQu Wenruo * data loss. 3915a94d1d0cSQu Wenruo * 3916a94d1d0cSQu Wenruo * Here we take a shortcut by flushing the whole inode, so that all 3917a94d1d0cSQu Wenruo * nocow write should reach disk as nocow before we increase the 3918a94d1d0cSQu Wenruo * reference of the extent. We could do better by only flushing NOCOW 3919a94d1d0cSQu Wenruo * data, but that needs extra accounting. 3920a94d1d0cSQu Wenruo * 3921a94d1d0cSQu Wenruo * Also we don't need to check ASYNC_EXTENT, as async extent will be 3922a94d1d0cSQu Wenruo * CoWed anyway, not affecting nocow part. 3923a94d1d0cSQu Wenruo */ 3924a94d1d0cSQu Wenruo ret = filemap_flush(inode_in->i_mapping); 3925a94d1d0cSQu Wenruo if (ret < 0) 3926a94d1d0cSQu Wenruo return ret; 3927a94d1d0cSQu Wenruo 392834a28e3dSFilipe Manana ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs), 392934a28e3dSFilipe Manana wb_len); 393034a28e3dSFilipe Manana if (ret < 0) 39317984ae52SGoldwyn Rodrigues return ret; 393234a28e3dSFilipe Manana ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs), 393334a28e3dSFilipe Manana wb_len); 393434a28e3dSFilipe Manana if (ret < 0) 39353db11b2eSZach Brown return ret; 39367984ae52SGoldwyn Rodrigues 39377984ae52SGoldwyn Rodrigues return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out, 39387984ae52SGoldwyn Rodrigues len, remap_flags); 39393db11b2eSZach Brown } 39403db11b2eSZach Brown 394142ec3d4cSDarrick J. Wong loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, 394242ec3d4cSDarrick J. Wong struct file *dst_file, loff_t destoff, loff_t len, 39432e5dfc99SDarrick J. Wong unsigned int remap_flags) 39443db11b2eSZach Brown { 394534a28e3dSFilipe Manana struct inode *src_inode = file_inode(src_file); 394634a28e3dSFilipe Manana struct inode *dst_inode = file_inode(dst_file); 394734a28e3dSFilipe Manana bool same_inode = dst_inode == src_inode; 394842ec3d4cSDarrick J. Wong int ret; 394942ec3d4cSDarrick J. Wong 39502e5dfc99SDarrick J. Wong if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) 39512e5dfc99SDarrick J. Wong return -EINVAL; 39522e5dfc99SDarrick J. Wong 39537984ae52SGoldwyn Rodrigues if (same_inode) 39547984ae52SGoldwyn Rodrigues inode_lock(src_inode); 39557984ae52SGoldwyn Rodrigues else 39567984ae52SGoldwyn Rodrigues lock_two_nondirectories(src_inode, dst_inode); 39577984ae52SGoldwyn Rodrigues 395834a28e3dSFilipe Manana ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff, 395934a28e3dSFilipe Manana &len, remap_flags); 396034a28e3dSFilipe Manana if (ret < 0 || len == 0) 39617984ae52SGoldwyn Rodrigues goto out_unlock; 39622e5dfc99SDarrick J. Wong 396334a28e3dSFilipe Manana if (remap_flags & REMAP_FILE_DEDUP) 396434a28e3dSFilipe Manana ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff); 396534a28e3dSFilipe Manana else 396642ec3d4cSDarrick J. Wong ret = btrfs_clone_files(dst_file, src_file, off, len, destoff); 396734a28e3dSFilipe Manana 39687984ae52SGoldwyn Rodrigues out_unlock: 396934a28e3dSFilipe Manana if (same_inode) 397034a28e3dSFilipe Manana inode_unlock(src_inode); 397134a28e3dSFilipe Manana else 39724ea748e1SFilipe Manana unlock_two_nondirectories(src_inode, dst_inode); 397334a28e3dSFilipe Manana 397442ec3d4cSDarrick J. Wong return ret < 0 ? ret : len; 3975c5c9cd4dSSage Weil } 3976c5c9cd4dSSage Weil 39776ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 39786ef5ed0dSJosef Bacik { 3979496ad9aaSAl Viro struct inode *inode = file_inode(file); 39800b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 39816ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 39826ef5ed0dSJosef Bacik struct btrfs_root *new_root; 39836ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 39846ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 39856ef5ed0dSJosef Bacik struct btrfs_path *path; 39866ef5ed0dSJosef Bacik struct btrfs_key location; 39876ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 39886ef5ed0dSJosef Bacik u64 objectid = 0; 39896ef5ed0dSJosef Bacik u64 dir_id; 39903c04ce01SMiao Xie int ret; 39916ef5ed0dSJosef Bacik 39926ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 39936ef5ed0dSJosef Bacik return -EPERM; 39946ef5ed0dSJosef Bacik 39953c04ce01SMiao Xie ret = mnt_want_write_file(file); 39963c04ce01SMiao Xie if (ret) 39973c04ce01SMiao Xie return ret; 39983c04ce01SMiao Xie 39993c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 40003c04ce01SMiao Xie ret = -EFAULT; 40013c04ce01SMiao Xie goto out; 40023c04ce01SMiao Xie } 40036ef5ed0dSJosef Bacik 40046ef5ed0dSJosef Bacik if (!objectid) 40051cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 40066ef5ed0dSJosef Bacik 40076ef5ed0dSJosef Bacik location.objectid = objectid; 40086ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 40096ef5ed0dSJosef Bacik location.offset = (u64)-1; 40106ef5ed0dSJosef Bacik 40113619c94fSJosef Bacik new_root = btrfs_get_fs_root(fs_info, &location, true); 40123c04ce01SMiao Xie if (IS_ERR(new_root)) { 40133c04ce01SMiao Xie ret = PTR_ERR(new_root); 40143c04ce01SMiao Xie goto out; 40153c04ce01SMiao Xie } 40164fd786e6SMisono Tomohiro if (!is_fstree(new_root->root_key.objectid)) { 40176d6d2829Ssatoru takeuchi ret = -ENOENT; 40186d6d2829Ssatoru takeuchi goto out; 40196d6d2829Ssatoru takeuchi } 40206ef5ed0dSJosef Bacik 40216ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 40223c04ce01SMiao Xie if (!path) { 40233c04ce01SMiao Xie ret = -ENOMEM; 40243c04ce01SMiao Xie goto out; 40253c04ce01SMiao Xie } 40266ef5ed0dSJosef Bacik path->leave_spinning = 1; 40276ef5ed0dSJosef Bacik 40286ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 402998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 40306ef5ed0dSJosef Bacik btrfs_free_path(path); 40313c04ce01SMiao Xie ret = PTR_ERR(trans); 40323c04ce01SMiao Xie goto out; 40336ef5ed0dSJosef Bacik } 40346ef5ed0dSJosef Bacik 40350b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 40360b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 40376ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4038cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 40396ef5ed0dSJosef Bacik btrfs_free_path(path); 40403a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40410b246afaSJeff Mahoney btrfs_err(fs_info, 40425d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 40433c04ce01SMiao Xie ret = -ENOENT; 40443c04ce01SMiao Xie goto out; 40456ef5ed0dSJosef Bacik } 40466ef5ed0dSJosef Bacik 40476ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 40486ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 40496ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 40506ef5ed0dSJosef Bacik btrfs_free_path(path); 40516ef5ed0dSJosef Bacik 40520b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 40533a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40543c04ce01SMiao Xie out: 40553c04ce01SMiao Xie mnt_drop_write_file(file); 40563c04ce01SMiao Xie return ret; 40576ef5ed0dSJosef Bacik } 40586ef5ed0dSJosef Bacik 4059c065f5b1SSu Yue static void get_block_group_info(struct list_head *groups_list, 4060bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4061bf5fc093SJosef Bacik { 406232da5386SDavid Sterba struct btrfs_block_group *block_group; 4063bf5fc093SJosef Bacik 4064bf5fc093SJosef Bacik space->total_bytes = 0; 4065bf5fc093SJosef Bacik space->used_bytes = 0; 4066bf5fc093SJosef Bacik space->flags = 0; 4067bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4068bf5fc093SJosef Bacik space->flags = block_group->flags; 4069b3470b5dSDavid Sterba space->total_bytes += block_group->length; 4070bf38be65SDavid Sterba space->used_bytes += block_group->used; 4071bf5fc093SJosef Bacik } 4072bf5fc093SJosef Bacik } 4073bf5fc093SJosef Bacik 40742ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 40752ff7e61eSJeff Mahoney void __user *arg) 40761406e432SJosef Bacik { 40771406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 40781406e432SJosef Bacik struct btrfs_ioctl_space_info space; 40791406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 40807fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 408113f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 40821406e432SJosef Bacik struct btrfs_space_info *info; 4083315d8e98SColin Ian King static const u64 types[] = { 4084315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 4085bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4086bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4087315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 4088315d8e98SColin Ian King }; 4089bf5fc093SJosef Bacik int num_types = 4; 40907fde62bfSChris Mason int alloc_size; 40911406e432SJosef Bacik int ret = 0; 409251788b1bSDan Rosenberg u64 slot_count = 0; 4093bf5fc093SJosef Bacik int i, c; 40941406e432SJosef Bacik 40951406e432SJosef Bacik if (copy_from_user(&space_args, 40961406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 40971406e432SJosef Bacik sizeof(space_args))) 40981406e432SJosef Bacik return -EFAULT; 40991406e432SJosef Bacik 4100bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4101bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4102bf5fc093SJosef Bacik 4103bf5fc093SJosef Bacik info = NULL; 41047fde62bfSChris Mason rcu_read_lock(); 41050b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4106bf5fc093SJosef Bacik list) { 4107bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4108bf5fc093SJosef Bacik info = tmp; 4109bf5fc093SJosef Bacik break; 4110bf5fc093SJosef Bacik } 4111bf5fc093SJosef Bacik } 41127fde62bfSChris Mason rcu_read_unlock(); 41131406e432SJosef Bacik 4114bf5fc093SJosef Bacik if (!info) 4115bf5fc093SJosef Bacik continue; 4116bf5fc093SJosef Bacik 4117bf5fc093SJosef Bacik down_read(&info->groups_sem); 4118bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4119bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4120bf5fc093SJosef Bacik slot_count++; 4121bf5fc093SJosef Bacik } 4122bf5fc093SJosef Bacik up_read(&info->groups_sem); 4123bf5fc093SJosef Bacik } 4124bf5fc093SJosef Bacik 412536523e95SDavid Sterba /* 412636523e95SDavid Sterba * Global block reserve, exported as a space_info 412736523e95SDavid Sterba */ 412836523e95SDavid Sterba slot_count++; 412936523e95SDavid Sterba 41307fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 41317fde62bfSChris Mason if (space_args.space_slots == 0) { 41327fde62bfSChris Mason space_args.total_spaces = slot_count; 41337fde62bfSChris Mason goto out; 41347fde62bfSChris Mason } 4135bf5fc093SJosef Bacik 413651788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4137bf5fc093SJosef Bacik 41387fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4139bf5fc093SJosef Bacik 41407fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 41417fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 41427fde62bfSChris Mason */ 414309cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 41447fde62bfSChris Mason return -ENOMEM; 41457fde62bfSChris Mason 41467fde62bfSChris Mason space_args.total_spaces = 0; 41478d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 41487fde62bfSChris Mason if (!dest) 41497fde62bfSChris Mason return -ENOMEM; 41507fde62bfSChris Mason dest_orig = dest; 41517fde62bfSChris Mason 41527fde62bfSChris Mason /* now we have a buffer to copy into */ 4153bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4154bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4155bf5fc093SJosef Bacik 415651788b1bSDan Rosenberg if (!slot_count) 415751788b1bSDan Rosenberg break; 415851788b1bSDan Rosenberg 4159bf5fc093SJosef Bacik info = NULL; 41601406e432SJosef Bacik rcu_read_lock(); 41610b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4162bf5fc093SJosef Bacik list) { 4163bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4164bf5fc093SJosef Bacik info = tmp; 41657fde62bfSChris Mason break; 4166bf5fc093SJosef Bacik } 4167bf5fc093SJosef Bacik } 4168bf5fc093SJosef Bacik rcu_read_unlock(); 41697fde62bfSChris Mason 4170bf5fc093SJosef Bacik if (!info) 4171bf5fc093SJosef Bacik continue; 4172bf5fc093SJosef Bacik down_read(&info->groups_sem); 4173bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4174bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 4175c065f5b1SSu Yue get_block_group_info(&info->block_groups[c], 4176c065f5b1SSu Yue &space); 41777fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 41781406e432SJosef Bacik dest++; 41791406e432SJosef Bacik space_args.total_spaces++; 418051788b1bSDan Rosenberg slot_count--; 41811406e432SJosef Bacik } 418251788b1bSDan Rosenberg if (!slot_count) 418351788b1bSDan Rosenberg break; 4184bf5fc093SJosef Bacik } 4185bf5fc093SJosef Bacik up_read(&info->groups_sem); 4186bf5fc093SJosef Bacik } 41871406e432SJosef Bacik 418836523e95SDavid Sterba /* 418936523e95SDavid Sterba * Add global block reserve 419036523e95SDavid Sterba */ 419136523e95SDavid Sterba if (slot_count) { 41920b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 419336523e95SDavid Sterba 419436523e95SDavid Sterba spin_lock(&block_rsv->lock); 419536523e95SDavid Sterba space.total_bytes = block_rsv->size; 419636523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 419736523e95SDavid Sterba spin_unlock(&block_rsv->lock); 419836523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 419936523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 420036523e95SDavid Sterba space_args.total_spaces++; 420136523e95SDavid Sterba } 420236523e95SDavid Sterba 42032eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 42047fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 42057fde62bfSChris Mason 42067fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 42077fde62bfSChris Mason ret = -EFAULT; 42087fde62bfSChris Mason 42097fde62bfSChris Mason kfree(dest_orig); 42107fde62bfSChris Mason out: 42117fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 42121406e432SJosef Bacik ret = -EFAULT; 42131406e432SJosef Bacik 42141406e432SJosef Bacik return ret; 42151406e432SJosef Bacik } 42161406e432SJosef Bacik 42179a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 42189a8c28beSMiao Xie void __user *argp) 421946204592SSage Weil { 422046204592SSage Weil struct btrfs_trans_handle *trans; 422146204592SSage Weil u64 transid; 4222db5b493aSTsutomu Itoh int ret; 422346204592SSage Weil 4224d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4225ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4226ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 422798d5dc13STsutomu Itoh return PTR_ERR(trans); 4228ff7c1d33SMiao Xie 4229ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4230ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4231ff7c1d33SMiao Xie goto out; 4232ff7c1d33SMiao Xie } 423346204592SSage Weil transid = trans->transid; 42343a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 0); 42358b2b2d3cSTsutomu Itoh if (ret) { 42363a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4237db5b493aSTsutomu Itoh return ret; 42388b2b2d3cSTsutomu Itoh } 4239ff7c1d33SMiao Xie out: 424046204592SSage Weil if (argp) 424146204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 424246204592SSage Weil return -EFAULT; 424346204592SSage Weil return 0; 424446204592SSage Weil } 424546204592SSage Weil 42462ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 42479a8c28beSMiao Xie void __user *argp) 424846204592SSage Weil { 424946204592SSage Weil u64 transid; 425046204592SSage Weil 425146204592SSage Weil if (argp) { 425246204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 425346204592SSage Weil return -EFAULT; 425446204592SSage Weil } else { 425546204592SSage Weil transid = 0; /* current trans */ 425646204592SSage Weil } 42572ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 425846204592SSage Weil } 425946204592SSage Weil 4260b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4261475f6387SJan Schmidt { 42620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4263475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4264b8e95489SMiao Xie int ret; 4265475f6387SJan Schmidt 4266475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4267475f6387SJan Schmidt return -EPERM; 4268475f6387SJan Schmidt 4269475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4270475f6387SJan Schmidt if (IS_ERR(sa)) 4271475f6387SJan Schmidt return PTR_ERR(sa); 4272475f6387SJan Schmidt 4273b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4274b8e95489SMiao Xie ret = mnt_want_write_file(file); 4275b8e95489SMiao Xie if (ret) 4276b8e95489SMiao Xie goto out; 4277b8e95489SMiao Xie } 4278b8e95489SMiao Xie 42790b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 428063a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 428163a212abSStefan Behrens 0); 4282475f6387SJan Schmidt 42835afe6ce7SFilipe Manana /* 42845afe6ce7SFilipe Manana * Copy scrub args to user space even if btrfs_scrub_dev() returned an 42855afe6ce7SFilipe Manana * error. This is important as it allows user space to know how much 42865afe6ce7SFilipe Manana * progress scrub has done. For example, if scrub is canceled we get 42875afe6ce7SFilipe Manana * -ECANCELED from btrfs_scrub_dev() and return that error back to user 42885afe6ce7SFilipe Manana * space. Later user space can inspect the progress from the structure 42895afe6ce7SFilipe Manana * btrfs_ioctl_scrub_args and resume scrub from where it left off 42905afe6ce7SFilipe Manana * previously (btrfs-progs does this). 42915afe6ce7SFilipe Manana * If we fail to copy the btrfs_ioctl_scrub_args structure to user space 42925afe6ce7SFilipe Manana * then return -EFAULT to signal the structure was not copied or it may 42935afe6ce7SFilipe Manana * be corrupt and unreliable due to a partial copy. 42945afe6ce7SFilipe Manana */ 42955afe6ce7SFilipe Manana if (copy_to_user(arg, sa, sizeof(*sa))) 4296475f6387SJan Schmidt ret = -EFAULT; 4297475f6387SJan Schmidt 4298b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4299b8e95489SMiao Xie mnt_drop_write_file(file); 4300b8e95489SMiao Xie out: 4301475f6387SJan Schmidt kfree(sa); 4302475f6387SJan Schmidt return ret; 4303475f6387SJan Schmidt } 4304475f6387SJan Schmidt 43052ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4306475f6387SJan Schmidt { 4307475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4308475f6387SJan Schmidt return -EPERM; 4309475f6387SJan Schmidt 43102ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4311475f6387SJan Schmidt } 4312475f6387SJan Schmidt 43132ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4314475f6387SJan Schmidt void __user *arg) 4315475f6387SJan Schmidt { 4316475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4317475f6387SJan Schmidt int ret; 4318475f6387SJan Schmidt 4319475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4320475f6387SJan Schmidt return -EPERM; 4321475f6387SJan Schmidt 4322475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4323475f6387SJan Schmidt if (IS_ERR(sa)) 4324475f6387SJan Schmidt return PTR_ERR(sa); 4325475f6387SJan Schmidt 43262ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4327475f6387SJan Schmidt 43284fa99b00SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4329475f6387SJan Schmidt ret = -EFAULT; 4330475f6387SJan Schmidt 4331475f6387SJan Schmidt kfree(sa); 4332475f6387SJan Schmidt return ret; 4333475f6387SJan Schmidt } 4334475f6387SJan Schmidt 43352ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4336b27f7c0cSDavid Sterba void __user *arg) 4337c11d2c23SStefan Behrens { 4338c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4339c11d2c23SStefan Behrens int ret; 4340c11d2c23SStefan Behrens 4341c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4342c11d2c23SStefan Behrens if (IS_ERR(sa)) 4343c11d2c23SStefan Behrens return PTR_ERR(sa); 4344c11d2c23SStefan Behrens 4345b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4346b27f7c0cSDavid Sterba kfree(sa); 4347b27f7c0cSDavid Sterba return -EPERM; 4348b27f7c0cSDavid Sterba } 4349b27f7c0cSDavid Sterba 43502ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4351c11d2c23SStefan Behrens 4352eee99577SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4353c11d2c23SStefan Behrens ret = -EFAULT; 4354c11d2c23SStefan Behrens 4355c11d2c23SStefan Behrens kfree(sa); 4356c11d2c23SStefan Behrens return ret; 4357c11d2c23SStefan Behrens } 4358c11d2c23SStefan Behrens 43592ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 43602ff7e61eSJeff Mahoney void __user *arg) 43613f6bcfbdSStefan Behrens { 43623f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 43633f6bcfbdSStefan Behrens int ret; 43643f6bcfbdSStefan Behrens 43653f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 43663f6bcfbdSStefan Behrens return -EPERM; 43673f6bcfbdSStefan Behrens 43683f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 43693f6bcfbdSStefan Behrens if (IS_ERR(p)) 43703f6bcfbdSStefan Behrens return PTR_ERR(p); 43713f6bcfbdSStefan Behrens 43723f6bcfbdSStefan Behrens switch (p->cmd) { 43733f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4374bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4375adfa97cbSIlya Dryomov ret = -EROFS; 4376adfa97cbSIlya Dryomov goto out; 4377adfa97cbSIlya Dryomov } 4378171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4379e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 43803f6bcfbdSStefan Behrens } else { 43812ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4382171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 43833f6bcfbdSStefan Behrens } 43843f6bcfbdSStefan Behrens break; 43853f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 43860b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 43873f6bcfbdSStefan Behrens ret = 0; 43883f6bcfbdSStefan Behrens break; 43893f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 439017d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 439197282031SAnand Jain ret = 0; 43923f6bcfbdSStefan Behrens break; 43933f6bcfbdSStefan Behrens default: 43943f6bcfbdSStefan Behrens ret = -EINVAL; 43953f6bcfbdSStefan Behrens break; 43963f6bcfbdSStefan Behrens } 43973f6bcfbdSStefan Behrens 4398d3a53286SFilipe Manana if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) 43993f6bcfbdSStefan Behrens ret = -EFAULT; 4400adfa97cbSIlya Dryomov out: 44013f6bcfbdSStefan Behrens kfree(p); 44023f6bcfbdSStefan Behrens return ret; 44033f6bcfbdSStefan Behrens } 44043f6bcfbdSStefan Behrens 4405d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4406d7728c96SJan Schmidt { 4407d7728c96SJan Schmidt int ret = 0; 4408d7728c96SJan Schmidt int i; 4409740c3d22SChris Mason u64 rel_ptr; 4410d7728c96SJan Schmidt int size; 4411806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4412d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4413d7728c96SJan Schmidt struct btrfs_path *path; 4414d7728c96SJan Schmidt 441582b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4416d7728c96SJan Schmidt return -EPERM; 4417d7728c96SJan Schmidt 4418d7728c96SJan Schmidt path = btrfs_alloc_path(); 4419d7728c96SJan Schmidt if (!path) { 4420d7728c96SJan Schmidt ret = -ENOMEM; 4421d7728c96SJan Schmidt goto out; 4422d7728c96SJan Schmidt } 4423d7728c96SJan Schmidt 4424d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4425d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4426d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4427d7728c96SJan Schmidt ipa = NULL; 4428d7728c96SJan Schmidt goto out; 4429d7728c96SJan Schmidt } 4430d7728c96SJan Schmidt 4431d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4432d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4433d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4434d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4435d7728c96SJan Schmidt ipath = NULL; 4436d7728c96SJan Schmidt goto out; 4437d7728c96SJan Schmidt } 4438d7728c96SJan Schmidt 4439d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4440d7728c96SJan Schmidt if (ret < 0) 4441d7728c96SJan Schmidt goto out; 4442d7728c96SJan Schmidt 4443d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4444745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4445745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4446740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4447d7728c96SJan Schmidt } 4448d7728c96SJan Schmidt 4449718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4450718dc5faSOmar Sandoval ipath->fspath, size); 4451d7728c96SJan Schmidt if (ret) { 4452d7728c96SJan Schmidt ret = -EFAULT; 4453d7728c96SJan Schmidt goto out; 4454d7728c96SJan Schmidt } 4455d7728c96SJan Schmidt 4456d7728c96SJan Schmidt out: 4457d7728c96SJan Schmidt btrfs_free_path(path); 4458d7728c96SJan Schmidt free_ipath(ipath); 4459d7728c96SJan Schmidt kfree(ipa); 4460d7728c96SJan Schmidt 4461d7728c96SJan Schmidt return ret; 4462d7728c96SJan Schmidt } 4463d7728c96SJan Schmidt 4464d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4465d7728c96SJan Schmidt { 4466d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4467d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4468d7728c96SJan Schmidt 4469d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4470d7728c96SJan Schmidt inodes->bytes_left -= c; 4471d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4472d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4473d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4474d7728c96SJan Schmidt inodes->elem_cnt += 3; 4475d7728c96SJan Schmidt } else { 4476d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4477d7728c96SJan Schmidt inodes->bytes_left = 0; 4478d7728c96SJan Schmidt inodes->elem_missed += 3; 4479d7728c96SJan Schmidt } 4480d7728c96SJan Schmidt 4481d7728c96SJan Schmidt return 0; 4482d7728c96SJan Schmidt } 4483d7728c96SJan Schmidt 44842ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4485d24a67b2SZygo Blaxell void __user *arg, int version) 4486d7728c96SJan Schmidt { 4487d7728c96SJan Schmidt int ret = 0; 4488d7728c96SJan Schmidt int size; 4489d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4490d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4491d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4492d24a67b2SZygo Blaxell bool ignore_offset; 4493d7728c96SJan Schmidt 4494d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4495d7728c96SJan Schmidt return -EPERM; 4496d7728c96SJan Schmidt 4497d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 44987b9ea627SShailendra Verma if (IS_ERR(loi)) 44997b9ea627SShailendra Verma return PTR_ERR(loi); 4500d7728c96SJan Schmidt 4501d24a67b2SZygo Blaxell if (version == 1) { 4502d24a67b2SZygo Blaxell ignore_offset = false; 4503b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4504d24a67b2SZygo Blaxell } else { 4505d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4506d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4507d24a67b2SZygo Blaxell ret = -EINVAL; 4508d24a67b2SZygo Blaxell goto out_loi; 4509d24a67b2SZygo Blaxell } 4510d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4511d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4512d24a67b2SZygo Blaxell ret = -EINVAL; 4513d24a67b2SZygo Blaxell goto out_loi; 4514d24a67b2SZygo Blaxell } 4515d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4516b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4517d24a67b2SZygo Blaxell } 4518d24a67b2SZygo Blaxell 4519d7728c96SJan Schmidt path = btrfs_alloc_path(); 4520d7728c96SJan Schmidt if (!path) { 4521d7728c96SJan Schmidt ret = -ENOMEM; 4522d7728c96SJan Schmidt goto out; 4523d7728c96SJan Schmidt } 4524d7728c96SJan Schmidt 4525d7728c96SJan Schmidt inodes = init_data_container(size); 4526d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4527d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4528d7728c96SJan Schmidt inodes = NULL; 4529d7728c96SJan Schmidt goto out; 4530d7728c96SJan Schmidt } 4531d7728c96SJan Schmidt 45322ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4533d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4534df031f07SLiu Bo if (ret == -EINVAL) 4535d7728c96SJan Schmidt ret = -ENOENT; 4536d7728c96SJan Schmidt if (ret < 0) 4537d7728c96SJan Schmidt goto out; 4538d7728c96SJan Schmidt 4539718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4540718dc5faSOmar Sandoval size); 4541d7728c96SJan Schmidt if (ret) 4542d7728c96SJan Schmidt ret = -EFAULT; 4543d7728c96SJan Schmidt 4544d7728c96SJan Schmidt out: 4545d7728c96SJan Schmidt btrfs_free_path(path); 4546f54de068SDavid Sterba kvfree(inodes); 4547d24a67b2SZygo Blaxell out_loi: 4548d7728c96SJan Schmidt kfree(loi); 4549d7728c96SJan Schmidt 4550d7728c96SJan Schmidt return ret; 4551d7728c96SJan Schmidt } 4552d7728c96SJan Schmidt 4553008ef096SDavid Sterba void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 4554c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4555c9e9f97bSIlya Dryomov { 4556c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4557c9e9f97bSIlya Dryomov 4558c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4559c9e9f97bSIlya Dryomov 45603009a62fSDavid Sterba if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) 4561837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4562837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4563837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4564a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4565a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4566837d5b6eSIlya Dryomov 4567c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4568c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4569c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 457019a39dceSIlya Dryomov 457119a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 457219a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 457319a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 4574c9e9f97bSIlya Dryomov } 4575c9e9f97bSIlya Dryomov 45769ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4577c9e9f97bSIlya Dryomov { 4578496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4579c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4580c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4581c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4582ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4583c9e9f97bSIlya Dryomov int ret; 4584c9e9f97bSIlya Dryomov 4585c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4586c9e9f97bSIlya Dryomov return -EPERM; 4587c9e9f97bSIlya Dryomov 4588e54bfa31SLiu Bo ret = mnt_want_write_file(file); 45899ba1f6e4SLiu Bo if (ret) 45909ba1f6e4SLiu Bo return ret; 45919ba1f6e4SLiu Bo 4592ed0fb78fSIlya Dryomov again: 4593171938e5SDavid Sterba if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4594c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4595ed0fb78fSIlya Dryomov need_unlock = true; 4596ed0fb78fSIlya Dryomov goto locked; 4597ed0fb78fSIlya Dryomov } 4598ed0fb78fSIlya Dryomov 4599ed0fb78fSIlya Dryomov /* 460001327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4601ed0fb78fSIlya Dryomov * (1) some other op is running 4602ed0fb78fSIlya Dryomov * (2) balance is running 4603ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4604ed0fb78fSIlya Dryomov */ 4605ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4606ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4607ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 46083009a62fSDavid Sterba if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4609ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4610dccdb07bSDavid Sterba /* 4611dccdb07bSDavid Sterba * Lock released to allow other waiters to continue, 4612dccdb07bSDavid Sterba * we'll reexamine the status again. 4613dccdb07bSDavid Sterba */ 4614ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4615ed0fb78fSIlya Dryomov 4616ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 46173009a62fSDavid Sterba !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4618ed0fb78fSIlya Dryomov /* this is (3) */ 4619ed0fb78fSIlya Dryomov need_unlock = false; 4620ed0fb78fSIlya Dryomov goto locked; 4621ed0fb78fSIlya Dryomov } 4622ed0fb78fSIlya Dryomov 4623ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4624ed0fb78fSIlya Dryomov goto again; 4625ed0fb78fSIlya Dryomov } else { 4626ed0fb78fSIlya Dryomov /* this is (2) */ 4627ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4628ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4629ed0fb78fSIlya Dryomov goto out; 4630ed0fb78fSIlya Dryomov } 4631ed0fb78fSIlya Dryomov } else { 4632ed0fb78fSIlya Dryomov /* this is (1) */ 4633ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4634e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4635ed0fb78fSIlya Dryomov goto out; 4636ed0fb78fSIlya Dryomov } 4637ed0fb78fSIlya Dryomov 4638ed0fb78fSIlya Dryomov locked: 4639171938e5SDavid Sterba BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); 4640c9e9f97bSIlya Dryomov 4641c9e9f97bSIlya Dryomov if (arg) { 4642c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4643c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4644c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4645ed0fb78fSIlya Dryomov goto out_unlock; 4646c9e9f97bSIlya Dryomov } 4647de322263SIlya Dryomov 4648de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4649de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4650de322263SIlya Dryomov ret = -ENOTCONN; 4651de322263SIlya Dryomov goto out_bargs; 4652de322263SIlya Dryomov } 4653de322263SIlya Dryomov 4654de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4655de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4656de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4657de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4658de322263SIlya Dryomov 4659de322263SIlya Dryomov goto do_balance; 4660de322263SIlya Dryomov } 4661c9e9f97bSIlya Dryomov } else { 4662c9e9f97bSIlya Dryomov bargs = NULL; 4663c9e9f97bSIlya Dryomov } 4664c9e9f97bSIlya Dryomov 4665ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4666837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4667837d5b6eSIlya Dryomov goto out_bargs; 4668837d5b6eSIlya Dryomov } 4669837d5b6eSIlya Dryomov 46708d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4671c9e9f97bSIlya Dryomov if (!bctl) { 4672c9e9f97bSIlya Dryomov ret = -ENOMEM; 4673c9e9f97bSIlya Dryomov goto out_bargs; 4674c9e9f97bSIlya Dryomov } 4675c9e9f97bSIlya Dryomov 4676c9e9f97bSIlya Dryomov if (arg) { 4677c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4678c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4679c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4680c9e9f97bSIlya Dryomov 4681c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4682f43ffb60SIlya Dryomov } else { 4683f43ffb60SIlya Dryomov /* balance everything - no filters */ 4684f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4685c9e9f97bSIlya Dryomov } 4686c9e9f97bSIlya Dryomov 46878eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 46888eb93459SDavid Sterba ret = -EINVAL; 46890f89abf5SChristian Engelmayer goto out_bctl; 46908eb93459SDavid Sterba } 46918eb93459SDavid Sterba 4692de322263SIlya Dryomov do_balance: 4693c9e9f97bSIlya Dryomov /* 4694149196a2SDavid Sterba * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to 4695149196a2SDavid Sterba * btrfs_balance. bctl is freed in reset_balance_state, or, if 4696149196a2SDavid Sterba * restriper was paused all the way until unmount, in free_fs_info. 4697149196a2SDavid Sterba * The flag should be cleared after reset_balance_state. 4698c9e9f97bSIlya Dryomov */ 4699ed0fb78fSIlya Dryomov need_unlock = false; 4700ed0fb78fSIlya Dryomov 47016fcf6e2bSDavid Sterba ret = btrfs_balance(fs_info, bctl, bargs); 47020f89abf5SChristian Engelmayer bctl = NULL; 4703ed0fb78fSIlya Dryomov 4704d00c2d9cSFilipe Manana if ((ret == 0 || ret == -ECANCELED) && arg) { 4705c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4706c9e9f97bSIlya Dryomov ret = -EFAULT; 4707c9e9f97bSIlya Dryomov } 4708c9e9f97bSIlya Dryomov 47090f89abf5SChristian Engelmayer out_bctl: 47100f89abf5SChristian Engelmayer kfree(bctl); 4711c9e9f97bSIlya Dryomov out_bargs: 4712c9e9f97bSIlya Dryomov kfree(bargs); 4713ed0fb78fSIlya Dryomov out_unlock: 4714c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4715ed0fb78fSIlya Dryomov if (need_unlock) 4716171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 4717ed0fb78fSIlya Dryomov out: 4718e54bfa31SLiu Bo mnt_drop_write_file(file); 4719c9e9f97bSIlya Dryomov return ret; 4720c9e9f97bSIlya Dryomov } 4721c9e9f97bSIlya Dryomov 47222ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4723837d5b6eSIlya Dryomov { 4724837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4725837d5b6eSIlya Dryomov return -EPERM; 4726837d5b6eSIlya Dryomov 4727837d5b6eSIlya Dryomov switch (cmd) { 4728837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 47290b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4730a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 47310b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4732837d5b6eSIlya Dryomov } 4733837d5b6eSIlya Dryomov 4734837d5b6eSIlya Dryomov return -EINVAL; 4735837d5b6eSIlya Dryomov } 4736837d5b6eSIlya Dryomov 47372ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 473819a39dceSIlya Dryomov void __user *arg) 473919a39dceSIlya Dryomov { 474019a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 474119a39dceSIlya Dryomov int ret = 0; 474219a39dceSIlya Dryomov 474319a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 474419a39dceSIlya Dryomov return -EPERM; 474519a39dceSIlya Dryomov 474619a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 474719a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 474819a39dceSIlya Dryomov ret = -ENOTCONN; 474919a39dceSIlya Dryomov goto out; 475019a39dceSIlya Dryomov } 475119a39dceSIlya Dryomov 47528d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 475319a39dceSIlya Dryomov if (!bargs) { 475419a39dceSIlya Dryomov ret = -ENOMEM; 475519a39dceSIlya Dryomov goto out; 475619a39dceSIlya Dryomov } 475719a39dceSIlya Dryomov 4758008ef096SDavid Sterba btrfs_update_ioctl_balance_args(fs_info, bargs); 475919a39dceSIlya Dryomov 476019a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 476119a39dceSIlya Dryomov ret = -EFAULT; 476219a39dceSIlya Dryomov 476319a39dceSIlya Dryomov kfree(bargs); 476419a39dceSIlya Dryomov out: 476519a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 476619a39dceSIlya Dryomov return ret; 476719a39dceSIlya Dryomov } 476819a39dceSIlya Dryomov 4769905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 47705d13a37bSArne Jansen { 47710b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47720b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47735d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 47745d13a37bSArne Jansen int ret; 47755d13a37bSArne Jansen 47765d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47775d13a37bSArne Jansen return -EPERM; 47785d13a37bSArne Jansen 4779905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4780905b0ddaSMiao Xie if (ret) 4781905b0ddaSMiao Xie return ret; 47825d13a37bSArne Jansen 47835d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4784905b0ddaSMiao Xie if (IS_ERR(sa)) { 4785905b0ddaSMiao Xie ret = PTR_ERR(sa); 4786905b0ddaSMiao Xie goto drop_write; 4787905b0ddaSMiao Xie } 47885d13a37bSArne Jansen 47890b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 47905d13a37bSArne Jansen 47915d13a37bSArne Jansen switch (sa->cmd) { 47925d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 4793340f1aa2SNikolay Borisov ret = btrfs_quota_enable(fs_info); 47945d13a37bSArne Jansen break; 47955d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 4796340f1aa2SNikolay Borisov ret = btrfs_quota_disable(fs_info); 47975d13a37bSArne Jansen break; 47985d13a37bSArne Jansen default: 47995d13a37bSArne Jansen ret = -EINVAL; 48005d13a37bSArne Jansen break; 48015d13a37bSArne Jansen } 48025d13a37bSArne Jansen 48035d13a37bSArne Jansen kfree(sa); 48040b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4805905b0ddaSMiao Xie drop_write: 4806905b0ddaSMiao Xie mnt_drop_write_file(file); 48075d13a37bSArne Jansen return ret; 48085d13a37bSArne Jansen } 48095d13a37bSArne Jansen 4810905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 48115d13a37bSArne Jansen { 48120b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48130b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48140b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48155d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 48165d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48175d13a37bSArne Jansen int ret; 48185d13a37bSArne Jansen int err; 48195d13a37bSArne Jansen 48205d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48215d13a37bSArne Jansen return -EPERM; 48225d13a37bSArne Jansen 4823905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4824905b0ddaSMiao Xie if (ret) 4825905b0ddaSMiao Xie return ret; 48265d13a37bSArne Jansen 48275d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4828905b0ddaSMiao Xie if (IS_ERR(sa)) { 4829905b0ddaSMiao Xie ret = PTR_ERR(sa); 4830905b0ddaSMiao Xie goto drop_write; 4831905b0ddaSMiao Xie } 48325d13a37bSArne Jansen 48335d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48345d13a37bSArne Jansen if (IS_ERR(trans)) { 48355d13a37bSArne Jansen ret = PTR_ERR(trans); 48365d13a37bSArne Jansen goto out; 48375d13a37bSArne Jansen } 48385d13a37bSArne Jansen 48395d13a37bSArne Jansen if (sa->assign) { 48409f8a6ce6SLu Fengqi ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); 48415d13a37bSArne Jansen } else { 484239616c27SLu Fengqi ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); 48435d13a37bSArne Jansen } 48445d13a37bSArne Jansen 4845e082f563SQu Wenruo /* update qgroup status and info */ 4846280f8bd2SLu Fengqi err = btrfs_run_qgroups(trans); 4847e082f563SQu Wenruo if (err < 0) 48480b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4849ad8403dfSAnand Jain "failed to update qgroup status and info"); 48503a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 48515d13a37bSArne Jansen if (err && !ret) 48525d13a37bSArne Jansen ret = err; 48535d13a37bSArne Jansen 48545d13a37bSArne Jansen out: 48555d13a37bSArne Jansen kfree(sa); 4856905b0ddaSMiao Xie drop_write: 4857905b0ddaSMiao Xie mnt_drop_write_file(file); 48585d13a37bSArne Jansen return ret; 48595d13a37bSArne Jansen } 48605d13a37bSArne Jansen 4861905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 48625d13a37bSArne Jansen { 48630b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48640b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48655d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 48665d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48675d13a37bSArne Jansen int ret; 48685d13a37bSArne Jansen int err; 48695d13a37bSArne Jansen 48705d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48715d13a37bSArne Jansen return -EPERM; 48725d13a37bSArne Jansen 4873905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4874905b0ddaSMiao Xie if (ret) 4875905b0ddaSMiao Xie return ret; 48765d13a37bSArne Jansen 48775d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4878905b0ddaSMiao Xie if (IS_ERR(sa)) { 4879905b0ddaSMiao Xie ret = PTR_ERR(sa); 4880905b0ddaSMiao Xie goto drop_write; 4881905b0ddaSMiao Xie } 48825d13a37bSArne Jansen 4883d86e56cfSMiao Xie if (!sa->qgroupid) { 4884d86e56cfSMiao Xie ret = -EINVAL; 4885d86e56cfSMiao Xie goto out; 4886d86e56cfSMiao Xie } 4887d86e56cfSMiao Xie 48885d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48895d13a37bSArne Jansen if (IS_ERR(trans)) { 48905d13a37bSArne Jansen ret = PTR_ERR(trans); 48915d13a37bSArne Jansen goto out; 48925d13a37bSArne Jansen } 48935d13a37bSArne Jansen 48945d13a37bSArne Jansen if (sa->create) { 489549a05ecdSLu Fengqi ret = btrfs_create_qgroup(trans, sa->qgroupid); 48965d13a37bSArne Jansen } else { 48973efbee1dSLu Fengqi ret = btrfs_remove_qgroup(trans, sa->qgroupid); 48985d13a37bSArne Jansen } 48995d13a37bSArne Jansen 49003a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49015d13a37bSArne Jansen if (err && !ret) 49025d13a37bSArne Jansen ret = err; 49035d13a37bSArne Jansen 49045d13a37bSArne Jansen out: 49055d13a37bSArne Jansen kfree(sa); 4906905b0ddaSMiao Xie drop_write: 4907905b0ddaSMiao Xie mnt_drop_write_file(file); 49085d13a37bSArne Jansen return ret; 49095d13a37bSArne Jansen } 49105d13a37bSArne Jansen 4911905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 49125d13a37bSArne Jansen { 49130b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49140b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 49155d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 49165d13a37bSArne Jansen struct btrfs_trans_handle *trans; 49175d13a37bSArne Jansen int ret; 49185d13a37bSArne Jansen int err; 49195d13a37bSArne Jansen u64 qgroupid; 49205d13a37bSArne Jansen 49215d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 49225d13a37bSArne Jansen return -EPERM; 49235d13a37bSArne Jansen 4924905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4925905b0ddaSMiao Xie if (ret) 4926905b0ddaSMiao Xie return ret; 49275d13a37bSArne Jansen 49285d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4929905b0ddaSMiao Xie if (IS_ERR(sa)) { 4930905b0ddaSMiao Xie ret = PTR_ERR(sa); 4931905b0ddaSMiao Xie goto drop_write; 4932905b0ddaSMiao Xie } 49335d13a37bSArne Jansen 49345d13a37bSArne Jansen trans = btrfs_join_transaction(root); 49355d13a37bSArne Jansen if (IS_ERR(trans)) { 49365d13a37bSArne Jansen ret = PTR_ERR(trans); 49375d13a37bSArne Jansen goto out; 49385d13a37bSArne Jansen } 49395d13a37bSArne Jansen 49405d13a37bSArne Jansen qgroupid = sa->qgroupid; 49415d13a37bSArne Jansen if (!qgroupid) { 49425d13a37bSArne Jansen /* take the current subvol as qgroup */ 49435d13a37bSArne Jansen qgroupid = root->root_key.objectid; 49445d13a37bSArne Jansen } 49455d13a37bSArne Jansen 4946f0042d5eSLu Fengqi ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); 49475d13a37bSArne Jansen 49483a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49495d13a37bSArne Jansen if (err && !ret) 49505d13a37bSArne Jansen ret = err; 49515d13a37bSArne Jansen 49525d13a37bSArne Jansen out: 49535d13a37bSArne Jansen kfree(sa); 4954905b0ddaSMiao Xie drop_write: 4955905b0ddaSMiao Xie mnt_drop_write_file(file); 49565d13a37bSArne Jansen return ret; 49575d13a37bSArne Jansen } 49585d13a37bSArne Jansen 49592f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 49602f232036SJan Schmidt { 49610b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49632f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49642f232036SJan Schmidt int ret; 49652f232036SJan Schmidt 49662f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49672f232036SJan Schmidt return -EPERM; 49682f232036SJan Schmidt 49692f232036SJan Schmidt ret = mnt_want_write_file(file); 49702f232036SJan Schmidt if (ret) 49712f232036SJan Schmidt return ret; 49722f232036SJan Schmidt 49732f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 49742f232036SJan Schmidt if (IS_ERR(qsa)) { 49752f232036SJan Schmidt ret = PTR_ERR(qsa); 49762f232036SJan Schmidt goto drop_write; 49772f232036SJan Schmidt } 49782f232036SJan Schmidt 49792f232036SJan Schmidt if (qsa->flags) { 49802f232036SJan Schmidt ret = -EINVAL; 49812f232036SJan Schmidt goto out; 49822f232036SJan Schmidt } 49832f232036SJan Schmidt 49840b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 49852f232036SJan Schmidt 49862f232036SJan Schmidt out: 49872f232036SJan Schmidt kfree(qsa); 49882f232036SJan Schmidt drop_write: 49892f232036SJan Schmidt mnt_drop_write_file(file); 49902f232036SJan Schmidt return ret; 49912f232036SJan Schmidt } 49922f232036SJan Schmidt 4993b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, 4994b929c1d8SMarcos Paulo de Souza void __user *arg) 49952f232036SJan Schmidt { 49962f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49972f232036SJan Schmidt int ret = 0; 49982f232036SJan Schmidt 49992f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 50002f232036SJan Schmidt return -EPERM; 50012f232036SJan Schmidt 50028d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 50032f232036SJan Schmidt if (!qsa) 50042f232036SJan Schmidt return -ENOMEM; 50052f232036SJan Schmidt 50060b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 50072f232036SJan Schmidt qsa->flags = 1; 50080b246afaSJeff Mahoney qsa->progress = fs_info->qgroup_rescan_progress.objectid; 50092f232036SJan Schmidt } 50102f232036SJan Schmidt 50112f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 50122f232036SJan Schmidt ret = -EFAULT; 50132f232036SJan Schmidt 50142f232036SJan Schmidt kfree(qsa); 50152f232036SJan Schmidt return ret; 50162f232036SJan Schmidt } 50172f232036SJan Schmidt 5018b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, 5019b929c1d8SMarcos Paulo de Souza void __user *arg) 502057254b6eSJan Schmidt { 502157254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 502257254b6eSJan Schmidt return -EPERM; 502357254b6eSJan Schmidt 50240b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 502557254b6eSJan Schmidt } 502657254b6eSJan Schmidt 5027abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 5028abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 50298ea05e3aSAlexander Block { 5030496ad9aaSAl Viro struct inode *inode = file_inode(file); 50310b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 50328ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 50338ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 50348ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 503595582b00SDeepa Dinamani struct timespec64 ct = current_time(inode); 50368ea05e3aSAlexander Block int ret = 0; 5037dd5f9615SStefan Behrens int received_uuid_changed; 50388ea05e3aSAlexander Block 5039bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5040bd60ea0fSDavid Sterba return -EPERM; 5041bd60ea0fSDavid Sterba 50428ea05e3aSAlexander Block ret = mnt_want_write_file(file); 50438ea05e3aSAlexander Block if (ret < 0) 50448ea05e3aSAlexander Block return ret; 50458ea05e3aSAlexander Block 50460b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 50478ea05e3aSAlexander Block 50484a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 50498ea05e3aSAlexander Block ret = -EINVAL; 50508ea05e3aSAlexander Block goto out; 50518ea05e3aSAlexander Block } 50528ea05e3aSAlexander Block 50538ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 50548ea05e3aSAlexander Block ret = -EROFS; 50558ea05e3aSAlexander Block goto out; 50568ea05e3aSAlexander Block } 50578ea05e3aSAlexander Block 5058dd5f9615SStefan Behrens /* 5059dd5f9615SStefan Behrens * 1 - root item 5060dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5061dd5f9615SStefan Behrens */ 5062dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 50638ea05e3aSAlexander Block if (IS_ERR(trans)) { 50648ea05e3aSAlexander Block ret = PTR_ERR(trans); 50658ea05e3aSAlexander Block trans = NULL; 50668ea05e3aSAlexander Block goto out; 50678ea05e3aSAlexander Block } 50688ea05e3aSAlexander Block 50698ea05e3aSAlexander Block sa->rtransid = trans->transid; 50708ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 50718ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 50728ea05e3aSAlexander Block 5073dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5074dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5075dd5f9615SStefan Behrens if (received_uuid_changed && 5076d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 5077d1957791SLu Fengqi ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, 5078dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5079dd5f9615SStefan Behrens root->root_key.objectid); 5080d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 5081d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 5082d87ff758SNikolay Borisov btrfs_end_transaction(trans); 5083d87ff758SNikolay Borisov goto out; 5084d87ff758SNikolay Borisov } 5085d87ff758SNikolay Borisov } 50868ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 50878ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 50888ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 50893cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 50903cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 50913cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 50923cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 50938ea05e3aSAlexander Block 50940b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 50958ea05e3aSAlexander Block &root->root_key, &root->root_item); 50968ea05e3aSAlexander Block if (ret < 0) { 50973a45bb20SJeff Mahoney btrfs_end_transaction(trans); 50988ea05e3aSAlexander Block goto out; 5099dd5f9615SStefan Behrens } 5100dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 5101cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, sa->uuid, 5102dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5103dd5f9615SStefan Behrens root->root_key.objectid); 5104dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 510566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 5106efd38150SNikolay Borisov btrfs_end_transaction(trans); 5107dd5f9615SStefan Behrens goto out; 5108dd5f9615SStefan Behrens } 5109dd5f9615SStefan Behrens } 51103a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5111abccd00fSHugo Mills out: 51120b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5113abccd00fSHugo Mills mnt_drop_write_file(file); 5114abccd00fSHugo Mills return ret; 5115abccd00fSHugo Mills } 5116abccd00fSHugo Mills 5117abccd00fSHugo Mills #ifdef CONFIG_64BIT 5118abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5119abccd00fSHugo Mills void __user *arg) 5120abccd00fSHugo Mills { 5121abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5122abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5123abccd00fSHugo Mills int ret = 0; 5124abccd00fSHugo Mills 5125abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 51267b9ea627SShailendra Verma if (IS_ERR(args32)) 51277b9ea627SShailendra Verma return PTR_ERR(args32); 5128abccd00fSHugo Mills 51298d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 513084dbeb87SDan Carpenter if (!args64) { 513184dbeb87SDan Carpenter ret = -ENOMEM; 5132abccd00fSHugo Mills goto out; 5133abccd00fSHugo Mills } 5134abccd00fSHugo Mills 5135abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5136abccd00fSHugo Mills args64->stransid = args32->stransid; 5137abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5138abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5139abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5140abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5141abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5142abccd00fSHugo Mills args64->flags = args32->flags; 5143abccd00fSHugo Mills 5144abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5145abccd00fSHugo Mills if (ret) 5146abccd00fSHugo Mills goto out; 5147abccd00fSHugo Mills 5148abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5149abccd00fSHugo Mills args32->stransid = args64->stransid; 5150abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5151abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5152abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5153abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5154abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5155abccd00fSHugo Mills args32->flags = args64->flags; 5156abccd00fSHugo Mills 5157abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5158abccd00fSHugo Mills if (ret) 5159abccd00fSHugo Mills ret = -EFAULT; 5160abccd00fSHugo Mills 5161abccd00fSHugo Mills out: 5162abccd00fSHugo Mills kfree(args32); 5163abccd00fSHugo Mills kfree(args64); 5164abccd00fSHugo Mills return ret; 5165abccd00fSHugo Mills } 5166abccd00fSHugo Mills #endif 5167abccd00fSHugo Mills 5168abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5169abccd00fSHugo Mills void __user *arg) 5170abccd00fSHugo Mills { 5171abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5172abccd00fSHugo Mills int ret = 0; 5173abccd00fSHugo Mills 5174abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 51757b9ea627SShailendra Verma if (IS_ERR(sa)) 51767b9ea627SShailendra Verma return PTR_ERR(sa); 5177abccd00fSHugo Mills 5178abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5179abccd00fSHugo Mills 5180abccd00fSHugo Mills if (ret) 5181abccd00fSHugo Mills goto out; 5182abccd00fSHugo Mills 51838ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 51848ea05e3aSAlexander Block if (ret) 51858ea05e3aSAlexander Block ret = -EFAULT; 51868ea05e3aSAlexander Block 51878ea05e3aSAlexander Block out: 51888ea05e3aSAlexander Block kfree(sa); 51898ea05e3aSAlexander Block return ret; 51908ea05e3aSAlexander Block } 51918ea05e3aSAlexander Block 5192b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, 5193b929c1d8SMarcos Paulo de Souza void __user *arg) 5194867ab667Sjeff.liu { 5195a1b83ac5SAnand Jain size_t len; 5196867ab667Sjeff.liu int ret; 5197a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5198a1b83ac5SAnand Jain 51990b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 52000b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 52010b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 5202a1b83ac5SAnand Jain 5203a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5204867ab667Sjeff.liu 5205867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 52060b246afaSJeff Mahoney btrfs_warn(fs_info, 52070b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 52080b246afaSJeff Mahoney --len); 5209867ab667Sjeff.liu } 5210867ab667Sjeff.liu 5211867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5212867ab667Sjeff.liu 5213867ab667Sjeff.liu return ret ? -EFAULT : 0; 5214867ab667Sjeff.liu } 5215867ab667Sjeff.liu 5216a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5217a8bfd4abSjeff.liu { 52180b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 52200b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 52210b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 5222a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5223a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5224a8bfd4abSjeff.liu int ret; 5225a8bfd4abSjeff.liu 5226a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5227a8bfd4abSjeff.liu return -EPERM; 5228a8bfd4abSjeff.liu 5229a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5230a8bfd4abSjeff.liu return -EFAULT; 5231a8bfd4abSjeff.liu 5232a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 52330b246afaSJeff Mahoney btrfs_err(fs_info, 52345d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 5235a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5236a8bfd4abSjeff.liu return -EINVAL; 5237a8bfd4abSjeff.liu } 5238a8bfd4abSjeff.liu 5239a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5240a8bfd4abSjeff.liu if (ret) 5241a8bfd4abSjeff.liu return ret; 5242a8bfd4abSjeff.liu 5243a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5244a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5245a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5246a8bfd4abSjeff.liu goto out_unlock; 5247a8bfd4abSjeff.liu } 5248a8bfd4abSjeff.liu 52490b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5250a8bfd4abSjeff.liu strcpy(super_block->label, label); 52510b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 52523a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5253a8bfd4abSjeff.liu 5254a8bfd4abSjeff.liu out_unlock: 5255a8bfd4abSjeff.liu mnt_drop_write_file(file); 5256a8bfd4abSjeff.liu return ret; 5257a8bfd4abSjeff.liu } 5258a8bfd4abSjeff.liu 52592eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 52602eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 52612eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 52622eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 52632eaa055fSJeff Mahoney 5264d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 52652eaa055fSJeff Mahoney { 52664d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 52672eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 52682eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 52692eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 52702eaa055fSJeff Mahoney }; 52712eaa055fSJeff Mahoney 52722eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52732eaa055fSJeff Mahoney return -EFAULT; 52742eaa055fSJeff Mahoney 52752eaa055fSJeff Mahoney return 0; 52762eaa055fSJeff Mahoney } 52772eaa055fSJeff Mahoney 5278b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, 5279b929c1d8SMarcos Paulo de Souza void __user *arg) 52802eaa055fSJeff Mahoney { 52810b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 52822eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 52832eaa055fSJeff Mahoney 52842eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 52852eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 52862eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 52872eaa055fSJeff Mahoney 52882eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52892eaa055fSJeff Mahoney return -EFAULT; 52902eaa055fSJeff Mahoney 52912eaa055fSJeff Mahoney return 0; 52922eaa055fSJeff Mahoney } 52932eaa055fSJeff Mahoney 52942ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 52953b02a68aSJeff Mahoney enum btrfs_feature_set set, 52962eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 52972eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 52982eaa055fSJeff Mahoney { 5299f10152bcSDavid Sterba const char *type = btrfs_feature_set_name(set); 53003b02a68aSJeff Mahoney char *names; 53012eaa055fSJeff Mahoney u64 disallowed, unsupported; 53022eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 53032eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 53042eaa055fSJeff Mahoney 53052eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 53062eaa055fSJeff Mahoney if (unsupported) { 53073b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 53083b02a68aSJeff Mahoney if (names) { 53090b246afaSJeff Mahoney btrfs_warn(fs_info, 53103b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 53113b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53123b02a68aSJeff Mahoney kfree(names); 53133b02a68aSJeff Mahoney } else 53140b246afaSJeff Mahoney btrfs_warn(fs_info, 53152eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 53162eaa055fSJeff Mahoney type, unsupported); 53172eaa055fSJeff Mahoney return -EOPNOTSUPP; 53182eaa055fSJeff Mahoney } 53192eaa055fSJeff Mahoney 53202eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 53212eaa055fSJeff Mahoney if (disallowed) { 53223b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53233b02a68aSJeff Mahoney if (names) { 53240b246afaSJeff Mahoney btrfs_warn(fs_info, 53253b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 53263b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53273b02a68aSJeff Mahoney kfree(names); 53283b02a68aSJeff Mahoney } else 53290b246afaSJeff Mahoney btrfs_warn(fs_info, 53302eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 53312eaa055fSJeff Mahoney type, disallowed); 53322eaa055fSJeff Mahoney return -EPERM; 53332eaa055fSJeff Mahoney } 53342eaa055fSJeff Mahoney 53352eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 53362eaa055fSJeff Mahoney if (disallowed) { 53373b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53383b02a68aSJeff Mahoney if (names) { 53390b246afaSJeff Mahoney btrfs_warn(fs_info, 53403b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 53413b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53423b02a68aSJeff Mahoney kfree(names); 53433b02a68aSJeff Mahoney } else 53440b246afaSJeff Mahoney btrfs_warn(fs_info, 53452eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 53462eaa055fSJeff Mahoney type, disallowed); 53472eaa055fSJeff Mahoney return -EPERM; 53482eaa055fSJeff Mahoney } 53492eaa055fSJeff Mahoney 53502eaa055fSJeff Mahoney return 0; 53512eaa055fSJeff Mahoney } 53522eaa055fSJeff Mahoney 53532ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 53542ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 53552eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 53562eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 53572eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 53582eaa055fSJeff Mahoney 53592eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 53602eaa055fSJeff Mahoney { 53610b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53630b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 53640b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 53652eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 53662eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 53672eaa055fSJeff Mahoney u64 newflags; 53682eaa055fSJeff Mahoney int ret; 53692eaa055fSJeff Mahoney 53702eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 53712eaa055fSJeff Mahoney return -EPERM; 53722eaa055fSJeff Mahoney 53732eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 53742eaa055fSJeff Mahoney return -EFAULT; 53752eaa055fSJeff Mahoney 53762eaa055fSJeff Mahoney /* Nothing to do */ 53772eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 53782eaa055fSJeff Mahoney !flags[0].incompat_flags) 53792eaa055fSJeff Mahoney return 0; 53802eaa055fSJeff Mahoney 53812ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 53822eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 53832eaa055fSJeff Mahoney if (ret) 53842eaa055fSJeff Mahoney return ret; 53852eaa055fSJeff Mahoney 53862ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 53872eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 53882eaa055fSJeff Mahoney if (ret) 53892eaa055fSJeff Mahoney return ret; 53902eaa055fSJeff Mahoney 53912ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 53922eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 53932eaa055fSJeff Mahoney if (ret) 53942eaa055fSJeff Mahoney return ret; 53952eaa055fSJeff Mahoney 53967ab19625SDavid Sterba ret = mnt_want_write_file(file); 53977ab19625SDavid Sterba if (ret) 53987ab19625SDavid Sterba return ret; 53997ab19625SDavid Sterba 54008051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 54017ab19625SDavid Sterba if (IS_ERR(trans)) { 54027ab19625SDavid Sterba ret = PTR_ERR(trans); 54037ab19625SDavid Sterba goto out_drop_write; 54047ab19625SDavid Sterba } 54052eaa055fSJeff Mahoney 54060b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 54072eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 54082eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 54092eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 54102eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 54112eaa055fSJeff Mahoney 54122eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 54132eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 54142eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 54152eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 54162eaa055fSJeff Mahoney 54172eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 54182eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 54192eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 54202eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 54210b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 54222eaa055fSJeff Mahoney 54233a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 54247ab19625SDavid Sterba out_drop_write: 54257ab19625SDavid Sterba mnt_drop_write_file(file); 54267ab19625SDavid Sterba 54277ab19625SDavid Sterba return ret; 54282eaa055fSJeff Mahoney } 54292eaa055fSJeff Mahoney 54302351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 54312351f431SJosef Bacik { 54322351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 54332351f431SJosef Bacik int ret; 54342351f431SJosef Bacik 54352351f431SJosef Bacik if (compat) { 54362351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 54372351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 54382351f431SJosef Bacik 54392351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 54402351f431SJosef Bacik if (ret) 54412351f431SJosef Bacik return -EFAULT; 54422351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 54432351f431SJosef Bacik if (!arg) 54442351f431SJosef Bacik return -ENOMEM; 54452351f431SJosef Bacik arg->send_fd = args32.send_fd; 54462351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 54472351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 54482351f431SJosef Bacik arg->parent_root = args32.parent_root; 54492351f431SJosef Bacik arg->flags = args32.flags; 54502351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 54512351f431SJosef Bacik sizeof(args32.reserved)); 54522351f431SJosef Bacik #else 54532351f431SJosef Bacik return -ENOTTY; 54542351f431SJosef Bacik #endif 54552351f431SJosef Bacik } else { 54562351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 54572351f431SJosef Bacik if (IS_ERR(arg)) 54582351f431SJosef Bacik return PTR_ERR(arg); 54592351f431SJosef Bacik } 54602351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 54612351f431SJosef Bacik kfree(arg); 54622351f431SJosef Bacik return ret; 54632351f431SJosef Bacik } 54642351f431SJosef Bacik 5465f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5466f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5467f46b5a66SChristoph Hellwig { 54680b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54690b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54700b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 54714bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5472f46b5a66SChristoph Hellwig 5473f46b5a66SChristoph Hellwig switch (cmd) { 54746cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 54756cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 54766cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 54776cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 54786cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 54796cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 548040cf931fSEric Sandeen case FS_IOC_GETFSLABEL: 5481b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_fslabel(fs_info, argp); 548240cf931fSEric Sandeen case FS_IOC_SETFSLABEL: 548340cf931fSEric Sandeen return btrfs_ioctl_set_fslabel(file, argp); 5484f7039b1dSLi Dongyang case FITRIM: 5485b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_fitrim(fs_info, argp); 5486f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5487fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5488fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5489fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 54903de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5491fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 54926f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 54936f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 549476dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 549576dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 54960caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 54970caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 54980caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 54990caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 55006ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 55016ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5502f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 55031e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 55041e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 55051e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5506f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5507198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5508f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 55092ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5510f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5511da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 55126b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 55136b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5514475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 55152ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5516475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 55172ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5518f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 55199ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5520ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5521ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5522cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5523cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5524ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5525ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5526d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5527d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5528d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5529d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5530d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5531d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 55321406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 55332ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 55349b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 55359b199859SFilipe David Borba Manana int ret; 55369b199859SFilipe David Borba Manana 553782b3e53bSNikolay Borisov ret = btrfs_start_delalloc_roots(fs_info, -1); 55389b199859SFilipe David Borba Manana if (ret) 55399b199859SFilipe David Borba Manana return ret; 55400b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 55412fad4e83SDavid Sterba /* 55422fad4e83SDavid Sterba * The transaction thread may want to do more work, 554301327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 55442fad4e83SDavid Sterba * processing uncleaned subvols. 55452fad4e83SDavid Sterba */ 55460b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 55479b199859SFilipe David Borba Manana return ret; 55489b199859SFilipe David Borba Manana } 554946204592SSage Weil case BTRFS_IOC_START_SYNC: 55509a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 555146204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 55522ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5553475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5554b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5555475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 55562ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5557475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 55582ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5559c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 55609ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5561837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 55622ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 556319a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 55642ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 55658ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 55668ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5567abccd00fSHugo Mills #ifdef CONFIG_64BIT 5568abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5569abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5570abccd00fSHugo Mills #endif 557131db9f7cSAlexander Block case BTRFS_IOC_SEND: 55722351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 55732351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 55742351f431SJosef Bacik case BTRFS_IOC_SEND_32: 55752351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 55762351f431SJosef Bacik #endif 5577c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 55782ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 55795d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5580905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 55815d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5582905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 55835d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5584905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 55855d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5586905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 55872f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 55882f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 55892f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 5590b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_status(fs_info, argp); 559157254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 5592b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_wait(fs_info, argp); 55933f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 55942ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 55952eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5596d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 55972eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 5598b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_features(fs_info, argp); 55992eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 56002eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5601e4202ac9SDavid Sterba case FS_IOC_FSGETXATTR: 5602e4202ac9SDavid Sterba return btrfs_ioctl_fsgetxattr(file, argp); 5603025f2121SDavid Sterba case FS_IOC_FSSETXATTR: 5604025f2121SDavid Sterba return btrfs_ioctl_fssetxattr(file, argp); 5605b64ec075STomohiro Misono case BTRFS_IOC_GET_SUBVOL_INFO: 5606b64ec075STomohiro Misono return btrfs_ioctl_get_subvol_info(file, argp); 560742e4b520STomohiro Misono case BTRFS_IOC_GET_SUBVOL_ROOTREF: 560842e4b520STomohiro Misono return btrfs_ioctl_get_subvol_rootref(file, argp); 560923d0b79dSTomohiro Misono case BTRFS_IOC_INO_LOOKUP_USER: 561023d0b79dSTomohiro Misono return btrfs_ioctl_ino_lookup_user(file, argp); 5611f46b5a66SChristoph Hellwig } 5612f46b5a66SChristoph Hellwig 5613f46b5a66SChristoph Hellwig return -ENOTTY; 5614f46b5a66SChristoph Hellwig } 56154c63c245SLuke Dashjr 56164c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 56174c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 56184c63c245SLuke Dashjr { 56192a362249SJeff Mahoney /* 56202a362249SJeff Mahoney * These all access 32-bit values anyway so no further 56212a362249SJeff Mahoney * handling is necessary. 56222a362249SJeff Mahoney */ 56234c63c245SLuke Dashjr switch (cmd) { 56244c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 56254c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 56264c63c245SLuke Dashjr break; 56274c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 56284c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 56294c63c245SLuke Dashjr break; 56304c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 56314c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 56324c63c245SLuke Dashjr break; 56334c63c245SLuke Dashjr } 56344c63c245SLuke Dashjr 56354c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 56364c63c245SLuke Dashjr } 56374c63c245SLuke Dashjr #endif 5638