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; 6690b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &key); 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 } 67576dda93cSYan, Zheng 67676dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 67776dda93cSYan, Zheng 67863541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 679ce598979SMark Fasheh if (ret) { 680ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 68166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 682ce598979SMark Fasheh goto fail; 683ce598979SMark Fasheh } 684ce598979SMark Fasheh 685f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 686f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 687f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 688f32e48e9SChandan Rajendra 689f46b5a66SChristoph Hellwig /* 690f46b5a66SChristoph Hellwig * insert the directory item 691f46b5a66SChristoph Hellwig */ 692877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 69379787eaaSJeff Mahoney if (ret) { 69466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 69579787eaaSJeff Mahoney goto fail; 69679787eaaSJeff Mahoney } 6973de4586cSChris Mason 698684572dfSLu Fengqi ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, 6993de4586cSChris Mason BTRFS_FT_DIR, index); 70079787eaaSJeff Mahoney if (ret) { 70166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 702f46b5a66SChristoph Hellwig goto fail; 70379787eaaSJeff Mahoney } 7040660b5afSChris Mason 7056ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 70652c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 70752c26179SYan Zheng BUG_ON(ret); 70852c26179SYan Zheng 7096025c19fSLu Fengqi ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, 7104a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 7110660b5afSChris Mason BUG_ON(ret); 7120660b5afSChris Mason 713cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, root_item->uuid, 7146bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 715dd5f9615SStefan Behrens if (ret) 71666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 717dd5f9615SStefan Behrens 718f46b5a66SChristoph Hellwig fail: 71949a3c4d9SDavid Sterba kfree(root_item); 720d5c12070SMiao Xie trans->block_rsv = NULL; 721d5c12070SMiao Xie trans->bytes_reserved = 0; 7227775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 723de6e8200SLiu Bo 72472fd032eSSage Weil if (async_transid) { 72572fd032eSSage Weil *async_transid = trans->transid; 7263a45bb20SJeff Mahoney err = btrfs_commit_transaction_async(trans, 1); 72700d71c9cSMiao Xie if (err) 7283a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 72972fd032eSSage Weil } else { 7303a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 73172fd032eSSage Weil } 732f46b5a66SChristoph Hellwig if (err && !ret) 733f46b5a66SChristoph Hellwig ret = err; 7341a65e24bSChris Mason 7355662344bSTsutomu Itoh if (!ret) { 7365662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 737de6e8200SLiu Bo if (IS_ERR(inode)) 738de6e8200SLiu Bo return PTR_ERR(inode); 7395662344bSTsutomu Itoh d_instantiate(dentry, inode); 7405662344bSTsutomu Itoh } 741f46b5a66SChristoph Hellwig return ret; 74249a3c4d9SDavid Sterba 74349a3c4d9SDavid Sterba fail_free: 74449a3c4d9SDavid Sterba kfree(root_item); 74549a3c4d9SDavid Sterba return ret; 746f46b5a66SChristoph Hellwig } 747f46b5a66SChristoph Hellwig 748e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 74961d7e4cbSDavid Sterba struct dentry *dentry, 750e9662f70SMiao Xie u64 *async_transid, bool readonly, 751e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 752f46b5a66SChristoph Hellwig { 7530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 7542e4bfab9SYan, Zheng struct inode *inode; 755f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 756f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 7572e4bfab9SYan, Zheng int ret; 7588ecebf4dSRobbie Ko bool snapshot_force_cow = false; 759f46b5a66SChristoph Hellwig 76027cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 761f46b5a66SChristoph Hellwig return -EINVAL; 762f46b5a66SChristoph Hellwig 763eede2bf3SOmar Sandoval if (atomic_read(&root->nr_swapfiles)) { 764eede2bf3SOmar Sandoval btrfs_warn(fs_info, 765eede2bf3SOmar Sandoval "cannot snapshot subvolume with active swapfile"); 766eede2bf3SOmar Sandoval return -ETXTBSY; 767eede2bf3SOmar Sandoval } 768eede2bf3SOmar Sandoval 76923269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 770a1ee7362SDavid Sterba if (!pending_snapshot) 771a1ee7362SDavid Sterba return -ENOMEM; 772a1ee7362SDavid Sterba 773b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 77423269bf5SDavid Sterba GFP_KERNEL); 7758546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 7768546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 777b0c0ea63SDavid Sterba ret = -ENOMEM; 778b0c0ea63SDavid Sterba goto free_pending; 779b0c0ea63SDavid Sterba } 780b0c0ea63SDavid Sterba 7818ecebf4dSRobbie Ko /* 7828ecebf4dSRobbie Ko * Force new buffered writes to reserve space even when NOCOW is 7838ecebf4dSRobbie Ko * possible. This is to avoid later writeback (running dealloc) to 7848ecebf4dSRobbie Ko * fallback to COW mode and unexpectedly fail with ENOSPC. 7858ecebf4dSRobbie Ko */ 786ea14b57fSDavid Sterba atomic_inc(&root->will_be_snapshotted); 7874e857c58SPeter Zijlstra smp_mb__after_atomic(); 78845bac0f3SLiu Bo /* wait for no snapshot writes */ 78945bac0f3SLiu Bo wait_event(root->subv_writers->wait, 79045bac0f3SLiu Bo percpu_counter_sum(&root->subv_writers->counter) == 0); 7918257b2dcSMiao Xie 7923cd24c69SEthan Lien ret = btrfs_start_delalloc_snapshot(root); 7936a03843dSMiao Xie if (ret) 794a1ee7362SDavid Sterba goto dec_and_free; 7956a03843dSMiao Xie 7968ecebf4dSRobbie Ko /* 7978ecebf4dSRobbie Ko * All previous writes have started writeback in NOCOW mode, so now 7988ecebf4dSRobbie Ko * we force future writes to fallback to COW mode during snapshot 7998ecebf4dSRobbie Ko * creation. 8008ecebf4dSRobbie Ko */ 8018ecebf4dSRobbie Ko atomic_inc(&root->snapshot_force_cow); 8028ecebf4dSRobbie Ko snapshot_force_cow = true; 8038ecebf4dSRobbie Ko 8046374e57aSChris Mason btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 8056a03843dSMiao Xie 80666d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 80766d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 808d5c12070SMiao Xie /* 809d5c12070SMiao Xie * 1 - parent dir inode 810d5c12070SMiao Xie * 2 - dir entries 811d5c12070SMiao Xie * 1 - root item 812d5c12070SMiao Xie * 2 - root ref/backref 813d5c12070SMiao Xie * 1 - root of snapshot 814dd5f9615SStefan Behrens * 1 - UUID item 815d5c12070SMiao Xie */ 816d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 817dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 818ee3441b4SJeff Mahoney false); 819d5c12070SMiao Xie if (ret) 820a1ee7362SDavid Sterba goto dec_and_free; 821d5c12070SMiao Xie 822a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 823a22285a6SYan, Zheng pending_snapshot->root = root; 824b83cc969SLi Zefan pending_snapshot->readonly = readonly; 825e9662f70SMiao Xie pending_snapshot->dir = dir; 8268696c533SMiao Xie pending_snapshot->inherit = inherit; 827a22285a6SYan, Zheng 828d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 829a22285a6SYan, Zheng if (IS_ERR(trans)) { 830a22285a6SYan, Zheng ret = PTR_ERR(trans); 831a22285a6SYan, Zheng goto fail; 832a22285a6SYan, Zheng } 833a22285a6SYan, Zheng 8340b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 835a22285a6SYan, Zheng list_add(&pending_snapshot->list, 836a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 8370b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 83872fd032eSSage Weil if (async_transid) { 83972fd032eSSage Weil *async_transid = trans->transid; 8403a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 1); 84100d71c9cSMiao Xie if (ret) 8423a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 84372fd032eSSage Weil } else { 8443a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 84572fd032eSSage Weil } 846aec8030aSMiao Xie if (ret) 847c37b2b62SJosef Bacik goto fail; 848a22285a6SYan, Zheng 849a22285a6SYan, Zheng ret = pending_snapshot->error; 850f46b5a66SChristoph Hellwig if (ret) 8512e4bfab9SYan, Zheng goto fail; 852f46b5a66SChristoph Hellwig 853d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 854d3797308SChris Mason if (ret) 855d3797308SChris Mason goto fail; 856d3797308SChris Mason 8572b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 8582e4bfab9SYan, Zheng if (IS_ERR(inode)) { 8592e4bfab9SYan, Zheng ret = PTR_ERR(inode); 8602e4bfab9SYan, Zheng goto fail; 8612e4bfab9SYan, Zheng } 8625662344bSTsutomu Itoh 8632e4bfab9SYan, Zheng d_instantiate(dentry, inode); 8642e4bfab9SYan, Zheng ret = 0; 8652e4bfab9SYan, Zheng fail: 8667775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); 867a1ee7362SDavid Sterba dec_and_free: 8688ecebf4dSRobbie Ko if (snapshot_force_cow) 8698ecebf4dSRobbie Ko atomic_dec(&root->snapshot_force_cow); 870ea14b57fSDavid Sterba if (atomic_dec_and_test(&root->will_be_snapshotted)) 8714625956aSPeter Zijlstra wake_up_var(&root->will_be_snapshotted); 872b0c0ea63SDavid Sterba free_pending: 873b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 8748546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 875a1ee7362SDavid Sterba kfree(pending_snapshot); 876a1ee7362SDavid Sterba 877f46b5a66SChristoph Hellwig return ret; 878f46b5a66SChristoph Hellwig } 879f46b5a66SChristoph Hellwig 8804260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 8814260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 8824260f7c7SSage Weil * whether the type of victim is right. 8834260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 8844260f7c7SSage Weil * 2. We should have write and exec permissions on dir 8854260f7c7SSage Weil * 3. We can't remove anything from append-only dir 8864260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 8874260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 8884260f7c7SSage Weil * a. be owner of dir, or 8894260f7c7SSage Weil * b. be owner of victim, or 8904260f7c7SSage Weil * c. have CAP_FOWNER capability 89101327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 8924260f7c7SSage Weil * links pointing to it. 8934260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 8944260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 8954260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 8964260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 8974260f7c7SSage Weil * nfs_async_unlink(). 8984260f7c7SSage Weil */ 8994260f7c7SSage Weil 9004260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 9014260f7c7SSage Weil { 9024260f7c7SSage Weil int error; 9034260f7c7SSage Weil 9042b0143b5SDavid Howells if (d_really_is_negative(victim)) 9054260f7c7SSage Weil return -ENOENT; 9064260f7c7SSage Weil 9072b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 9084fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 9094260f7c7SSage Weil 9104260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 9114260f7c7SSage Weil if (error) 9124260f7c7SSage Weil return error; 9134260f7c7SSage Weil if (IS_APPEND(dir)) 9144260f7c7SSage Weil return -EPERM; 9152b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 9162b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 9174260f7c7SSage Weil return -EPERM; 9184260f7c7SSage Weil if (isdir) { 919e36cb0b8SDavid Howells if (!d_is_dir(victim)) 9204260f7c7SSage Weil return -ENOTDIR; 9214260f7c7SSage Weil if (IS_ROOT(victim)) 9224260f7c7SSage Weil return -EBUSY; 923e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 9244260f7c7SSage Weil return -EISDIR; 9254260f7c7SSage Weil if (IS_DEADDIR(dir)) 9264260f7c7SSage Weil return -ENOENT; 9274260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 9284260f7c7SSage Weil return -EBUSY; 9294260f7c7SSage Weil return 0; 9304260f7c7SSage Weil } 9314260f7c7SSage Weil 932cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 933cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 934cb8e7090SChristoph Hellwig { 9352b0143b5SDavid Howells if (d_really_is_positive(child)) 936cb8e7090SChristoph Hellwig return -EEXIST; 937cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 938cb8e7090SChristoph Hellwig return -ENOENT; 939cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 940cb8e7090SChristoph Hellwig } 941cb8e7090SChristoph Hellwig 942cb8e7090SChristoph Hellwig /* 943cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 944cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 945cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 946cb8e7090SChristoph Hellwig */ 94792872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 94852f75f4fSDavid Sterba const char *name, int namelen, 94972fd032eSSage Weil struct btrfs_root *snap_src, 9506f72c7e2SArne Jansen u64 *async_transid, bool readonly, 9518696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 952cb8e7090SChristoph Hellwig { 9532b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 9540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 955cb8e7090SChristoph Hellwig struct dentry *dentry; 956cb8e7090SChristoph Hellwig int error; 957cb8e7090SChristoph Hellwig 95800235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 95900235411SAl Viro if (error == -EINTR) 96000235411SAl Viro return error; 961cb8e7090SChristoph Hellwig 962cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 963cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 964cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 965cb8e7090SChristoph Hellwig goto out_unlock; 966cb8e7090SChristoph Hellwig 96776dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 968cb8e7090SChristoph Hellwig if (error) 969a874a63eSLiu Bo goto out_dput; 970cb8e7090SChristoph Hellwig 9719c52057cSChris Mason /* 9729c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 9739c52057cSChris Mason * check for them now when we can safely fail 9749c52057cSChris Mason */ 9759c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 9769c52057cSChris Mason dir->i_ino, name, 9779c52057cSChris Mason namelen); 9789c52057cSChris Mason if (error) 9799c52057cSChris Mason goto out_dput; 9809c52057cSChris Mason 9810b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 98276dda93cSYan, Zheng 98376dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 98476dda93cSYan, Zheng goto out_up_read; 98576dda93cSYan, Zheng 9863de4586cSChris Mason if (snap_src) { 98761d7e4cbSDavid Sterba error = create_snapshot(snap_src, dir, dentry, 9886f72c7e2SArne Jansen async_transid, readonly, inherit); 9893de4586cSChris Mason } else { 990d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 991d5c12070SMiao Xie async_transid, inherit); 9923de4586cSChris Mason } 99376dda93cSYan, Zheng if (!error) 99476dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 99576dda93cSYan, Zheng out_up_read: 9960b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 997cb8e7090SChristoph Hellwig out_dput: 998cb8e7090SChristoph Hellwig dput(dentry); 999cb8e7090SChristoph Hellwig out_unlock: 10005955102cSAl Viro inode_unlock(dir); 1001cb8e7090SChristoph Hellwig return error; 1002cb8e7090SChristoph Hellwig } 1003cb8e7090SChristoph Hellwig 10044cb5300bSChris Mason /* 10054cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 10064cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 10074cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 10084cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 10094cb5300bSChris Mason * part of the file 10104cb5300bSChris Mason */ 1011aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 10124cb5300bSChris Mason { 10134cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 10144cb5300bSChris Mason struct extent_map *em = NULL; 10154cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 10164cb5300bSChris Mason u64 end; 10174cb5300bSChris Mason 10184cb5300bSChris Mason read_lock(&em_tree->lock); 101909cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 10204cb5300bSChris Mason read_unlock(&em_tree->lock); 10214cb5300bSChris Mason 10224cb5300bSChris Mason if (em) { 10234cb5300bSChris Mason end = extent_map_end(em); 10244cb5300bSChris Mason free_extent_map(em); 10254cb5300bSChris Mason if (end - offset > thresh) 10264cb5300bSChris Mason return 0; 10274cb5300bSChris Mason } 10284cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 10294cb5300bSChris Mason thresh /= 2; 10304cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 10314cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 10324cb5300bSChris Mason if (end >= thresh) 10334cb5300bSChris Mason return 0; 10344cb5300bSChris Mason return 1; 10354cb5300bSChris Mason } 10364cb5300bSChris Mason 10374cb5300bSChris Mason /* 10384cb5300bSChris Mason * helper function to walk through a file and find extents 10394cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 10404cb5300bSChris Mason * 10414cb5300bSChris Mason * This is used by the defragging code to find new and small 10424cb5300bSChris Mason * extents 10434cb5300bSChris Mason */ 10444cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 10454cb5300bSChris Mason struct inode *inode, u64 newer_than, 1046aab110abSDavid Sterba u64 *off, u32 thresh) 10474cb5300bSChris Mason { 10484cb5300bSChris Mason struct btrfs_path *path; 10494cb5300bSChris Mason struct btrfs_key min_key; 10504cb5300bSChris Mason struct extent_buffer *leaf; 10514cb5300bSChris Mason struct btrfs_file_extent_item *extent; 10524cb5300bSChris Mason int type; 10534cb5300bSChris Mason int ret; 10544a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode)); 10554cb5300bSChris Mason 10564cb5300bSChris Mason path = btrfs_alloc_path(); 10574cb5300bSChris Mason if (!path) 10584cb5300bSChris Mason return -ENOMEM; 10594cb5300bSChris Mason 1060a4689d2bSDavid Sterba min_key.objectid = ino; 10614cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 10624cb5300bSChris Mason min_key.offset = *off; 10634cb5300bSChris Mason 10644cb5300bSChris Mason while (1) { 10656174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 10664cb5300bSChris Mason if (ret != 0) 10674cb5300bSChris Mason goto none; 1068f094c9bdSFilipe Manana process_slot: 1069a4689d2bSDavid Sterba if (min_key.objectid != ino) 10704cb5300bSChris Mason goto none; 10714cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 10724cb5300bSChris Mason goto none; 10734cb5300bSChris Mason 10744cb5300bSChris Mason leaf = path->nodes[0]; 10754cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 10764cb5300bSChris Mason struct btrfs_file_extent_item); 10774cb5300bSChris Mason 10784cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 10794cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 10804cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 10814cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 10824cb5300bSChris Mason *off = min_key.offset; 10834cb5300bSChris Mason btrfs_free_path(path); 10844cb5300bSChris Mason return 0; 10854cb5300bSChris Mason } 10864cb5300bSChris Mason 1087f094c9bdSFilipe Manana path->slots[0]++; 1088f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 1089f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 1090f094c9bdSFilipe Manana goto process_slot; 1091f094c9bdSFilipe Manana } 1092f094c9bdSFilipe Manana 10934cb5300bSChris Mason if (min_key.offset == (u64)-1) 10944cb5300bSChris Mason goto none; 10954cb5300bSChris Mason 10964cb5300bSChris Mason min_key.offset++; 10974cb5300bSChris Mason btrfs_release_path(path); 10984cb5300bSChris Mason } 10994cb5300bSChris Mason none: 11004cb5300bSChris Mason btrfs_free_path(path); 11014cb5300bSChris Mason return -ENOENT; 11024cb5300bSChris Mason } 11034cb5300bSChris Mason 11046c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 110517ce6ef8SLiu Bo { 110617ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1107940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 11086c282eb4SLi Zefan struct extent_map *em; 110909cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 1110940100a4SChris Mason 1111940100a4SChris Mason /* 1112940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1113940100a4SChris Mason * the full extent lock 1114940100a4SChris Mason */ 1115940100a4SChris Mason read_lock(&em_tree->lock); 1116940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 1117940100a4SChris Mason read_unlock(&em_tree->lock); 1118940100a4SChris Mason 1119940100a4SChris Mason if (!em) { 1120308d9800SFilipe Manana struct extent_state *cached = NULL; 1121308d9800SFilipe Manana u64 end = start + len - 1; 1122308d9800SFilipe Manana 1123940100a4SChris Mason /* get the big lock and read metadata off disk */ 1124ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 1125fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); 1126e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 1127940100a4SChris Mason 11286cf8bfbfSDan Carpenter if (IS_ERR(em)) 11296c282eb4SLi Zefan return NULL; 1130940100a4SChris Mason } 1131940100a4SChris Mason 11326c282eb4SLi Zefan return em; 11336c282eb4SLi Zefan } 11346c282eb4SLi Zefan 11356c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 11366c282eb4SLi Zefan { 11376c282eb4SLi Zefan struct extent_map *next; 11386c282eb4SLi Zefan bool ret = true; 11396c282eb4SLi Zefan 11406c282eb4SLi Zefan /* this is the last extent */ 11416c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 11426c282eb4SLi Zefan return false; 11436c282eb4SLi Zefan 11446c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1145e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1146e9512d72SChris Mason ret = false; 1147e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1148ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 11496c282eb4SLi Zefan ret = false; 11506c282eb4SLi Zefan 11516c282eb4SLi Zefan free_extent_map(next); 11526c282eb4SLi Zefan return ret; 11536c282eb4SLi Zefan } 11546c282eb4SLi Zefan 1155aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1156a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1157a43a2111SAndrew Mahone int compress) 11586c282eb4SLi Zefan { 11596c282eb4SLi Zefan struct extent_map *em; 11606c282eb4SLi Zefan int ret = 1; 11616c282eb4SLi Zefan bool next_mergeable = true; 11624a3560c4SLiu Bo bool prev_mergeable = true; 11636c282eb4SLi Zefan 11646c282eb4SLi Zefan /* 11656c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 11666c282eb4SLi Zefan * defragging it 11676c282eb4SLi Zefan */ 11686c282eb4SLi Zefan if (start < *defrag_end) 11696c282eb4SLi Zefan return 1; 11706c282eb4SLi Zefan 11716c282eb4SLi Zefan *skip = 0; 11726c282eb4SLi Zefan 11736c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 11746c282eb4SLi Zefan if (!em) 11756c282eb4SLi Zefan return 0; 11766c282eb4SLi Zefan 1177940100a4SChris Mason /* this will cover holes, and inline extents */ 117817ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1179940100a4SChris Mason ret = 0; 118017ce6ef8SLiu Bo goto out; 118117ce6ef8SLiu Bo } 118217ce6ef8SLiu Bo 11834a3560c4SLiu Bo if (!*defrag_end) 11844a3560c4SLiu Bo prev_mergeable = false; 11854a3560c4SLiu Bo 11866c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1187940100a4SChris Mason /* 11886c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 11896c282eb4SLi Zefan * real extent, don't bother defragging it 1190940100a4SChris Mason */ 1191a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 11924a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1193940100a4SChris Mason ret = 0; 119417ce6ef8SLiu Bo out: 1195940100a4SChris Mason /* 1196940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1197940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1198940100a4SChris Mason * so that the next tiny extent will force a defrag. 1199940100a4SChris Mason * 1200940100a4SChris Mason * The end result of this is that tiny extents before a single big 1201940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1202940100a4SChris Mason */ 1203940100a4SChris Mason if (ret) { 1204940100a4SChris Mason *defrag_end = extent_map_end(em); 1205940100a4SChris Mason } else { 1206940100a4SChris Mason *last_len = 0; 1207940100a4SChris Mason *skip = extent_map_end(em); 1208940100a4SChris Mason *defrag_end = 0; 1209940100a4SChris Mason } 1210940100a4SChris Mason 1211940100a4SChris Mason free_extent_map(em); 1212940100a4SChris Mason return ret; 1213940100a4SChris Mason } 1214940100a4SChris Mason 12154cb5300bSChris Mason /* 12164cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 12174cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 12184cb5300bSChris Mason * to COW and defrag. 12194cb5300bSChris Mason * 12204cb5300bSChris Mason * It also makes sure the delalloc code has enough 12214cb5300bSChris Mason * dirty data to avoid making new small extents as part 12224cb5300bSChris Mason * of the defrag 12234cb5300bSChris Mason * 12244cb5300bSChris Mason * It's a good idea to start RA on this range 12254cb5300bSChris Mason * before calling this. 12264cb5300bSChris Mason */ 12274cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 12284cb5300bSChris Mason struct page **pages, 12294cb5300bSChris Mason unsigned long start_index, 1230c41570c9SJustin Maggard unsigned long num_pages) 1231f46b5a66SChristoph Hellwig { 12324cb5300bSChris Mason unsigned long file_end; 12334cb5300bSChris Mason u64 isize = i_size_read(inode); 1234f46b5a66SChristoph Hellwig u64 page_start; 1235f46b5a66SChristoph Hellwig u64 page_end; 12361f12bd06SLiu Bo u64 page_cnt; 12374cb5300bSChris Mason int ret; 12384cb5300bSChris Mason int i; 12394cb5300bSChris Mason int i_done; 12404cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 12414cb5300bSChris Mason struct extent_state *cached_state = NULL; 1242600a45e1SMiao Xie struct extent_io_tree *tree; 1243364ecf36SQu Wenruo struct extent_changeset *data_reserved = NULL; 12443b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 12454cb5300bSChris Mason 124609cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 12471f12bd06SLiu Bo if (!isize || start_index > file_end) 12481f12bd06SLiu Bo return 0; 12491f12bd06SLiu Bo 12501f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 12514cb5300bSChris Mason 1252364ecf36SQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 125309cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 125409cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12554cb5300bSChris Mason if (ret) 12564cb5300bSChris Mason return ret; 12574cb5300bSChris Mason i_done = 0; 1258600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 12594cb5300bSChris Mason 12604cb5300bSChris Mason /* step one, lock all the pages */ 12611f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 12624cb5300bSChris Mason struct page *page; 1263600a45e1SMiao Xie again: 1264a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 12653b16a4e3SJosef Bacik start_index + i, mask); 12664cb5300bSChris Mason if (!page) 12674cb5300bSChris Mason break; 12684cb5300bSChris Mason 1269600a45e1SMiao Xie page_start = page_offset(page); 127009cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1271600a45e1SMiao Xie while (1) { 1272308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1273ff13db41SDavid Sterba &cached_state); 1274600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1275600a45e1SMiao Xie page_start); 1276308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1277e43bbe5eSDavid Sterba &cached_state); 1278600a45e1SMiao Xie if (!ordered) 1279600a45e1SMiao Xie break; 1280600a45e1SMiao Xie 1281600a45e1SMiao Xie unlock_page(page); 1282600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1283600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1284600a45e1SMiao Xie lock_page(page); 12851f12bd06SLiu Bo /* 12861f12bd06SLiu Bo * we unlocked the page above, so we need check if 12871f12bd06SLiu Bo * it was released or not. 12881f12bd06SLiu Bo */ 12891f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 12901f12bd06SLiu Bo unlock_page(page); 129109cbfeafSKirill A. Shutemov put_page(page); 12921f12bd06SLiu Bo goto again; 12931f12bd06SLiu Bo } 1294600a45e1SMiao Xie } 1295600a45e1SMiao Xie 12964cb5300bSChris Mason if (!PageUptodate(page)) { 12974cb5300bSChris Mason btrfs_readpage(NULL, page); 12984cb5300bSChris Mason lock_page(page); 12994cb5300bSChris Mason if (!PageUptodate(page)) { 13004cb5300bSChris Mason unlock_page(page); 130109cbfeafSKirill A. Shutemov put_page(page); 13024cb5300bSChris Mason ret = -EIO; 13034cb5300bSChris Mason break; 13044cb5300bSChris Mason } 13054cb5300bSChris Mason } 1306600a45e1SMiao Xie 1307600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1308600a45e1SMiao Xie unlock_page(page); 130909cbfeafSKirill A. Shutemov put_page(page); 1310600a45e1SMiao Xie goto again; 1311600a45e1SMiao Xie } 1312600a45e1SMiao Xie 13134cb5300bSChris Mason pages[i] = page; 13144cb5300bSChris Mason i_done++; 13154cb5300bSChris Mason } 13164cb5300bSChris Mason if (!i_done || ret) 13174cb5300bSChris Mason goto out; 13184cb5300bSChris Mason 13191751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 13204cb5300bSChris Mason goto out; 13214cb5300bSChris Mason 13224cb5300bSChris Mason /* 13234cb5300bSChris Mason * so now we have a nice long stream of locked 13244cb5300bSChris Mason * and up to date pages, lets wait on them 13254cb5300bSChris Mason */ 13264cb5300bSChris Mason for (i = 0; i < i_done; i++) 13274cb5300bSChris Mason wait_on_page_writeback(pages[i]); 13284cb5300bSChris Mason 13294cb5300bSChris Mason page_start = page_offset(pages[0]); 133009cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 13314cb5300bSChris Mason 13324cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1333ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 13344cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 1335e182163dSOmar Sandoval page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | 1336e182163dSOmar Sandoval EXTENT_DEFRAG, 0, 0, &cached_state); 13374cb5300bSChris Mason 13381f12bd06SLiu Bo if (i_done != page_cnt) { 13399e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 134028c4a3e2SSu Yue btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); 13419e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 1342bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 134309cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 134443b18595SQu Wenruo (page_cnt - i_done) << PAGE_SHIFT, true); 13454cb5300bSChris Mason } 13464cb5300bSChris Mason 13474cb5300bSChris Mason 13489e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 1349018ed4f7SDavid Sterba &cached_state); 13504cb5300bSChris Mason 13514cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 1352e43bbe5eSDavid Sterba page_start, page_end - 1, &cached_state); 13534cb5300bSChris Mason 13544cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13554cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 13564cb5300bSChris Mason ClearPageChecked(pages[i]); 13574cb5300bSChris Mason set_page_extent_mapped(pages[i]); 13584cb5300bSChris Mason set_page_dirty(pages[i]); 13594cb5300bSChris Mason unlock_page(pages[i]); 136009cbfeafSKirill A. Shutemov put_page(pages[i]); 13614cb5300bSChris Mason } 13628702ba93SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1363364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13644cb5300bSChris Mason return i_done; 13654cb5300bSChris Mason out: 13664cb5300bSChris Mason for (i = 0; i < i_done; i++) { 13674cb5300bSChris Mason unlock_page(pages[i]); 136809cbfeafSKirill A. Shutemov put_page(pages[i]); 13694cb5300bSChris Mason } 1370bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 137109cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 137243b18595SQu Wenruo page_cnt << PAGE_SHIFT, true); 13738702ba93SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1374364ecf36SQu Wenruo extent_changeset_free(data_reserved); 13754cb5300bSChris Mason return ret; 13764cb5300bSChris Mason 13774cb5300bSChris Mason } 13784cb5300bSChris Mason 13794cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 13804cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 13814cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 13824cb5300bSChris Mason { 13830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 13844cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 13854cb5300bSChris Mason struct file_ra_state *ra = NULL; 13864cb5300bSChris Mason unsigned long last_index; 1387151a31b2SLi Zefan u64 isize = i_size_read(inode); 1388940100a4SChris Mason u64 last_len = 0; 1389940100a4SChris Mason u64 skip = 0; 1390940100a4SChris Mason u64 defrag_end = 0; 13914cb5300bSChris Mason u64 newer_off = range->start; 1392f46b5a66SChristoph Hellwig unsigned long i; 1393008873eaSLi Zefan unsigned long ra_index = 0; 1394f46b5a66SChristoph Hellwig int ret; 13954cb5300bSChris Mason int defrag_count = 0; 13961a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1397aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 139809cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1399c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1400ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 14014cb5300bSChris Mason struct page **pages = NULL; 14021e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 14034cb5300bSChris Mason 14040abd5b17SLiu Bo if (isize == 0) 14050abd5b17SLiu Bo return 0; 14060abd5b17SLiu Bo 14070abd5b17SLiu Bo if (range->start >= isize) 14080abd5b17SLiu Bo return -EINVAL; 14091a419d85SLi Zefan 14101e2ef46dSDavid Sterba if (do_compress) { 1411ce96b7ffSChengguang Xu if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) 14121a419d85SLi Zefan return -EINVAL; 14131a419d85SLi Zefan if (range->compress_type) 14141a419d85SLi Zefan compress_type = range->compress_type; 14151a419d85SLi Zefan } 1416f46b5a66SChristoph Hellwig 14170abd5b17SLiu Bo if (extent_thresh == 0) 1418ee22184bSByongho Lee extent_thresh = SZ_256K; 1419f46b5a66SChristoph Hellwig 14204cb5300bSChris Mason /* 14210a52d108SDavid Sterba * If we were not given a file, allocate a readahead context. As 14220a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 14230a52d108SDavid Sterba * we don't error out. 14244cb5300bSChris Mason */ 14254cb5300bSChris Mason if (!file) { 142663e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 14270a52d108SDavid Sterba if (ra) 14284cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 14294cb5300bSChris Mason } else { 14304cb5300bSChris Mason ra = &file->f_ra; 14314cb5300bSChris Mason } 14324cb5300bSChris Mason 143363e727ecSDavid Sterba pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); 14344cb5300bSChris Mason if (!pages) { 14354cb5300bSChris Mason ret = -ENOMEM; 14364cb5300bSChris Mason goto out_ra; 14374cb5300bSChris Mason } 14384cb5300bSChris Mason 14394cb5300bSChris Mason /* find the last page to defrag */ 14401e701a32SChris Mason if (range->start + range->len > range->start) { 1441151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 144209cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 14431e701a32SChris Mason } else { 144409cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 14451e701a32SChris Mason } 14461e701a32SChris Mason 14474cb5300bSChris Mason if (newer_than) { 14484cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1449ee22184bSByongho Lee &newer_off, SZ_64K); 14504cb5300bSChris Mason if (!ret) { 14514cb5300bSChris Mason range->start = newer_off; 14524cb5300bSChris Mason /* 14534cb5300bSChris Mason * we always align our defrag to help keep 14544cb5300bSChris Mason * the extents in the file evenly spaced 14554cb5300bSChris Mason */ 145609cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14574cb5300bSChris Mason } else 14584cb5300bSChris Mason goto out_ra; 14594cb5300bSChris Mason } else { 146009cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 14614cb5300bSChris Mason } 14624cb5300bSChris Mason if (!max_to_defrag) 1463070034bdSchandan max_to_defrag = last_index - i + 1; 14644cb5300bSChris Mason 14652a0f7f57SLi Zefan /* 14662a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 14672a0f7f57SLi Zefan * written sequentially. 14682a0f7f57SLi Zefan */ 14692a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 14702a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 14712a0f7f57SLi Zefan 1472f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 147309cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 14744cb5300bSChris Mason /* 14754cb5300bSChris Mason * make sure we stop running if someone unmounts 14764cb5300bSChris Mason * the FS 14774cb5300bSChris Mason */ 14781751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 14794cb5300bSChris Mason break; 14804cb5300bSChris Mason 14810b246afaSJeff Mahoney if (btrfs_defrag_cancelled(fs_info)) { 14820b246afaSJeff Mahoney btrfs_debug(fs_info, "defrag_file cancelled"); 1483210549ebSDavid Sterba ret = -EAGAIN; 1484210549ebSDavid Sterba break; 1485210549ebSDavid Sterba } 1486210549ebSDavid Sterba 148709cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 14886c282eb4SLi Zefan extent_thresh, &last_len, &skip, 14891e2ef46dSDavid Sterba &defrag_end, do_compress)){ 1490940100a4SChris Mason unsigned long next; 1491940100a4SChris Mason /* 1492940100a4SChris Mason * the should_defrag function tells us how much to skip 1493940100a4SChris Mason * bump our counter by the suggested amount 1494940100a4SChris Mason */ 149509cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1496940100a4SChris Mason i = max(i + 1, next); 1497940100a4SChris Mason continue; 1498940100a4SChris Mason } 1499008873eaSLi Zefan 1500008873eaSLi Zefan if (!newer_than) { 150109cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 150209cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1503008873eaSLi Zefan cluster = min(cluster, max_cluster); 1504008873eaSLi Zefan } else { 1505008873eaSLi Zefan cluster = max_cluster; 1506008873eaSLi Zefan } 1507008873eaSLi Zefan 1508008873eaSLi Zefan if (i + cluster > ra_index) { 1509008873eaSLi Zefan ra_index = max(i, ra_index); 15100a52d108SDavid Sterba if (ra) 1511d3c0bab5SDavid Sterba page_cache_sync_readahead(inode->i_mapping, ra, 1512d3c0bab5SDavid Sterba file, ra_index, cluster); 1513e4826a5bSchandan ra_index += cluster; 1514008873eaSLi Zefan } 15154cb5300bSChris Mason 15165955102cSAl Viro inode_lock(inode); 1517eede2bf3SOmar Sandoval if (IS_SWAPFILE(inode)) { 1518eede2bf3SOmar Sandoval ret = -ETXTBSY; 1519eede2bf3SOmar Sandoval } else { 15201e2ef46dSDavid Sterba if (do_compress) 1521eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 1522008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1523eede2bf3SOmar Sandoval } 1524ecb8bea8SLiu Bo if (ret < 0) { 15255955102cSAl Viro inode_unlock(inode); 15264cb5300bSChris Mason goto out_ra; 1527ecb8bea8SLiu Bo } 15284cb5300bSChris Mason 15294cb5300bSChris Mason defrag_count += ret; 1530d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 15315955102cSAl Viro inode_unlock(inode); 15324cb5300bSChris Mason 15334cb5300bSChris Mason if (newer_than) { 15344cb5300bSChris Mason if (newer_off == (u64)-1) 15354cb5300bSChris Mason break; 15364cb5300bSChris Mason 1537e1f041e1SLiu Bo if (ret > 0) 1538e1f041e1SLiu Bo i += ret; 1539e1f041e1SLiu Bo 15404cb5300bSChris Mason newer_off = max(newer_off + 1, 154109cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 15424cb5300bSChris Mason 1543ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1544ee22184bSByongho Lee &newer_off, SZ_64K); 15454cb5300bSChris Mason if (!ret) { 15464cb5300bSChris Mason range->start = newer_off; 154709cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 15484cb5300bSChris Mason } else { 15494cb5300bSChris Mason break; 1550940100a4SChris Mason } 15514cb5300bSChris Mason } else { 1552008873eaSLi Zefan if (ret > 0) { 1553cbcc8326SLi Zefan i += ret; 155409cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1555008873eaSLi Zefan } else { 1556940100a4SChris Mason i++; 1557008873eaSLi Zefan last_len = 0; 1558008873eaSLi Zefan } 1559f46b5a66SChristoph Hellwig } 15604cb5300bSChris Mason } 1561f46b5a66SChristoph Hellwig 1562dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 15631e701a32SChris Mason filemap_flush(inode->i_mapping); 1564dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1565dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1566dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1567dec8ef90SFilipe Manana } 15681e701a32SChris Mason 15691a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 15700b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 15715c1aab1dSNick Terrell } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { 15725c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 15731a419d85SLi Zefan } 15741a419d85SLi Zefan 157560ccf82fSDiego Calleja ret = defrag_count; 1576940100a4SChris Mason 15774cb5300bSChris Mason out_ra: 15781e2ef46dSDavid Sterba if (do_compress) { 15795955102cSAl Viro inode_lock(inode); 1580eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 15815955102cSAl Viro inode_unlock(inode); 1582633085c7SFilipe David Borba Manana } 15834cb5300bSChris Mason if (!file) 15844cb5300bSChris Mason kfree(ra); 15854cb5300bSChris Mason kfree(pages); 1586940100a4SChris Mason return ret; 1587f46b5a66SChristoph Hellwig } 1588f46b5a66SChristoph Hellwig 1589198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 159076dda93cSYan, Zheng void __user *arg) 1591f46b5a66SChristoph Hellwig { 15920b246afaSJeff Mahoney struct inode *inode = file_inode(file); 15930b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1594f46b5a66SChristoph Hellwig u64 new_size; 1595f46b5a66SChristoph Hellwig u64 old_size; 1596f46b5a66SChristoph Hellwig u64 devid = 1; 15970b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1598f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1599f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1600f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1601f46b5a66SChristoph Hellwig char *sizestr; 16029a40f122SGui Hecheng char *retptr; 1603f46b5a66SChristoph Hellwig char *devstr = NULL; 1604f46b5a66SChristoph Hellwig int ret = 0; 1605f46b5a66SChristoph Hellwig int mod = 0; 1606f46b5a66SChristoph Hellwig 1607e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1608e441d54dSChris Mason return -EPERM; 1609e441d54dSChris Mason 1610198605a8SMiao Xie ret = mnt_want_write_file(file); 1611198605a8SMiao Xie if (ret) 1612198605a8SMiao Xie return ret; 1613198605a8SMiao Xie 1614171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 161597547676SMiao Xie mnt_drop_write_file(file); 1616e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1617c9e9f97bSIlya Dryomov } 1618c9e9f97bSIlya Dryomov 1619dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1620c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1621c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1622c9e9f97bSIlya Dryomov goto out; 1623c9e9f97bSIlya Dryomov } 16245516e595SMark Fasheh 16255516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1626f46b5a66SChristoph Hellwig 1627f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1628f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1629f46b5a66SChristoph Hellwig if (devstr) { 1630f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1631f46b5a66SChristoph Hellwig *devstr = '\0'; 1632f46b5a66SChristoph Hellwig devstr = vol_args->name; 163358dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 163458dfae63SZhangZhen if (ret) 163558dfae63SZhangZhen goto out_free; 1636dfd79829SMiao Xie if (!devid) { 1637dfd79829SMiao Xie ret = -EINVAL; 1638dfd79829SMiao Xie goto out_free; 1639dfd79829SMiao Xie } 16400b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1641f46b5a66SChristoph Hellwig } 1642dba60f3fSMiao Xie 164309ba3bc9SAnand Jain device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); 1644f46b5a66SChristoph Hellwig if (!device) { 16450b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1646c1c9ff7cSGeert Uytterhoeven devid); 1647dfd79829SMiao Xie ret = -ENODEV; 1648c9e9f97bSIlya Dryomov goto out_free; 1649f46b5a66SChristoph Hellwig } 1650dba60f3fSMiao Xie 1651ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 16520b246afaSJeff Mahoney btrfs_info(fs_info, 1653efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1654c1c9ff7cSGeert Uytterhoeven devid); 1655dfd79829SMiao Xie ret = -EPERM; 16564e42ae1bSLiu Bo goto out_free; 16574e42ae1bSLiu Bo } 16584e42ae1bSLiu Bo 1659f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1660f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1661f46b5a66SChristoph Hellwig else { 1662f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1663f46b5a66SChristoph Hellwig mod = -1; 1664f46b5a66SChristoph Hellwig sizestr++; 1665f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1666f46b5a66SChristoph Hellwig mod = 1; 1667f46b5a66SChristoph Hellwig sizestr++; 1668f46b5a66SChristoph Hellwig } 16699a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 16709a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1671f46b5a66SChristoph Hellwig ret = -EINVAL; 1672c9e9f97bSIlya Dryomov goto out_free; 1673f46b5a66SChristoph Hellwig } 1674f46b5a66SChristoph Hellwig } 1675f46b5a66SChristoph Hellwig 1676401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 1677dfd79829SMiao Xie ret = -EPERM; 167863a212abSStefan Behrens goto out_free; 167963a212abSStefan Behrens } 168063a212abSStefan Behrens 16817cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1682f46b5a66SChristoph Hellwig 1683f46b5a66SChristoph Hellwig if (mod < 0) { 1684f46b5a66SChristoph Hellwig if (new_size > old_size) { 1685f46b5a66SChristoph Hellwig ret = -EINVAL; 1686c9e9f97bSIlya Dryomov goto out_free; 1687f46b5a66SChristoph Hellwig } 1688f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1689f46b5a66SChristoph Hellwig } else if (mod > 0) { 1690eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1691902c68a4SGui Hecheng ret = -ERANGE; 1692eb8052e0SWenliang Fan goto out_free; 1693eb8052e0SWenliang Fan } 1694f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1695f46b5a66SChristoph Hellwig } 1696f46b5a66SChristoph Hellwig 1697ee22184bSByongho Lee if (new_size < SZ_256M) { 1698f46b5a66SChristoph Hellwig ret = -EINVAL; 1699c9e9f97bSIlya Dryomov goto out_free; 1700f46b5a66SChristoph Hellwig } 1701f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1702f46b5a66SChristoph Hellwig ret = -EFBIG; 1703c9e9f97bSIlya Dryomov goto out_free; 1704f46b5a66SChristoph Hellwig } 1705f46b5a66SChristoph Hellwig 170647f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1707f46b5a66SChristoph Hellwig 17080b246afaSJeff Mahoney btrfs_info_in_rcu(fs_info, "new size for %s is %llu", 1709c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1710f46b5a66SChristoph Hellwig 1711f46b5a66SChristoph Hellwig if (new_size > old_size) { 1712a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 171398d5dc13STsutomu Itoh if (IS_ERR(trans)) { 171498d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1715c9e9f97bSIlya Dryomov goto out_free; 171698d5dc13STsutomu Itoh } 1717f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 17183a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1719ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1720f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 17210253f40eSjeff.liu } /* equal, nothing need to do */ 1722f46b5a66SChristoph Hellwig 1723c9e9f97bSIlya Dryomov out_free: 1724f46b5a66SChristoph Hellwig kfree(vol_args); 1725c9e9f97bSIlya Dryomov out: 1726171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 172718f39c41SIlya Dryomov mnt_drop_write_file(file); 1728f46b5a66SChristoph Hellwig return ret; 1729f46b5a66SChristoph Hellwig } 1730f46b5a66SChristoph Hellwig 173172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 173252f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 17336f72c7e2SArne Jansen u64 *transid, bool readonly, 17348696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1735f46b5a66SChristoph Hellwig { 1736f46b5a66SChristoph Hellwig int namelen; 17373de4586cSChris Mason int ret = 0; 1738f46b5a66SChristoph Hellwig 1739325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1740325c50e3SJeff Mahoney return -ENOTDIR; 1741325c50e3SJeff Mahoney 1742a874a63eSLiu Bo ret = mnt_want_write_file(file); 1743a874a63eSLiu Bo if (ret) 1744a874a63eSLiu Bo goto out; 1745a874a63eSLiu Bo 174672fd032eSSage Weil namelen = strlen(name); 174772fd032eSSage Weil if (strchr(name, '/')) { 1748f46b5a66SChristoph Hellwig ret = -EINVAL; 1749a874a63eSLiu Bo goto out_drop_write; 1750f46b5a66SChristoph Hellwig } 1751f46b5a66SChristoph Hellwig 175216780cabSChris Mason if (name[0] == '.' && 175316780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 175416780cabSChris Mason ret = -EEXIST; 1755a874a63eSLiu Bo goto out_drop_write; 175616780cabSChris Mason } 175716780cabSChris Mason 17583de4586cSChris Mason if (subvol) { 175972fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 17606f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1761cb8e7090SChristoph Hellwig } else { 17622903ff01SAl Viro struct fd src = fdget(fd); 17633de4586cSChris Mason struct inode *src_inode; 17642903ff01SAl Viro if (!src.file) { 17653de4586cSChris Mason ret = -EINVAL; 1766a874a63eSLiu Bo goto out_drop_write; 17673de4586cSChris Mason } 17683de4586cSChris Mason 1769496ad9aaSAl Viro src_inode = file_inode(src.file); 1770496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1771c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1772efe120a0SFrank Holton "Snapshot src from another FS"); 177323ad5b17SKusanagi Kouichi ret = -EXDEV; 1774d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1775d0242061SDavid Sterba /* 1776d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1777d0242061SDavid Sterba * are limited to own subvolumes only 1778d0242061SDavid Sterba */ 1779d0242061SDavid Sterba ret = -EPERM; 1780ecd18815SAl Viro } else { 178172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 178272fd032eSSage Weil BTRFS_I(src_inode)->root, 17836f72c7e2SArne Jansen transid, readonly, inherit); 1784ecd18815SAl Viro } 17852903ff01SAl Viro fdput(src); 1786cb8e7090SChristoph Hellwig } 1787a874a63eSLiu Bo out_drop_write: 1788a874a63eSLiu Bo mnt_drop_write_file(file); 1789f46b5a66SChristoph Hellwig out: 179072fd032eSSage Weil return ret; 179172fd032eSSage Weil } 179272fd032eSSage Weil 179372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1794fa0d2b9bSLi Zefan void __user *arg, int subvol) 179572fd032eSSage Weil { 1796fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 179772fd032eSSage Weil int ret; 179872fd032eSSage Weil 1799325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1800325c50e3SJeff Mahoney return -ENOTDIR; 1801325c50e3SJeff Mahoney 1802fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1803fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1804fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1805fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1806fa0d2b9bSLi Zefan 1807fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1808b83cc969SLi Zefan vol_args->fd, subvol, 18096f72c7e2SArne Jansen NULL, false, NULL); 1810fa0d2b9bSLi Zefan 1811fa0d2b9bSLi Zefan kfree(vol_args); 1812fa0d2b9bSLi Zefan return ret; 1813fa0d2b9bSLi Zefan } 1814fa0d2b9bSLi Zefan 1815fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1816fa0d2b9bSLi Zefan void __user *arg, int subvol) 1817fa0d2b9bSLi Zefan { 1818fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1819fa0d2b9bSLi Zefan int ret; 1820fdfb1e4fSLi Zefan u64 transid = 0; 1821fdfb1e4fSLi Zefan u64 *ptr = NULL; 1822b83cc969SLi Zefan bool readonly = false; 18236f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 182472fd032eSSage Weil 1825325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1826325c50e3SJeff Mahoney return -ENOTDIR; 1827325c50e3SJeff Mahoney 1828fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1829fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1830fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1831fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1832fdfb1e4fSLi Zefan 1833b83cc969SLi Zefan if (vol_args->flags & 18346f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 18356f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1836b83cc969SLi Zefan ret = -EOPNOTSUPP; 1837c47ca32dSDan Carpenter goto free_args; 1838fdfb1e4fSLi Zefan } 1839fdfb1e4fSLi Zefan 1840ebc87351SNikolay Borisov if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1841ebc87351SNikolay Borisov struct inode *inode = file_inode(file); 1842ebc87351SNikolay Borisov struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1843ebc87351SNikolay Borisov 1844ebc87351SNikolay Borisov btrfs_warn(fs_info, 1845ebc87351SNikolay Borisov "SNAP_CREATE_V2 ioctl with CREATE_ASYNC is deprecated and will be removed in kernel 5.7"); 1846ebc87351SNikolay Borisov 1847fdfb1e4fSLi Zefan ptr = &transid; 1848ebc87351SNikolay Borisov } 1849b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1850b83cc969SLi Zefan readonly = true; 18516f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 185209cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 18536f72c7e2SArne Jansen ret = -EINVAL; 1854c47ca32dSDan Carpenter goto free_args; 18556f72c7e2SArne Jansen } 18566f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 18576f72c7e2SArne Jansen if (IS_ERR(inherit)) { 18586f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1859c47ca32dSDan Carpenter goto free_args; 18606f72c7e2SArne Jansen } 18616f72c7e2SArne Jansen } 186275eaa0e2SSage Weil 1863fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 18646f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 18658696c533SMiao Xie readonly, inherit); 1866c47ca32dSDan Carpenter if (ret) 1867c47ca32dSDan Carpenter goto free_inherit; 186875eaa0e2SSage Weil 1869c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1870fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1871c47ca32dSDan Carpenter transid), 1872c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 187375eaa0e2SSage Weil ret = -EFAULT; 1874c47ca32dSDan Carpenter 1875c47ca32dSDan Carpenter free_inherit: 18766f72c7e2SArne Jansen kfree(inherit); 1877c47ca32dSDan Carpenter free_args: 1878c47ca32dSDan Carpenter kfree(vol_args); 1879f46b5a66SChristoph Hellwig return ret; 1880f46b5a66SChristoph Hellwig } 1881f46b5a66SChristoph Hellwig 18820caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 18830caa102dSLi Zefan void __user *arg) 18840caa102dSLi Zefan { 1885496ad9aaSAl Viro struct inode *inode = file_inode(file); 18860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 18870caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 18880caa102dSLi Zefan int ret = 0; 18890caa102dSLi Zefan u64 flags = 0; 18900caa102dSLi Zefan 18914a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 18920caa102dSLi Zefan return -EINVAL; 18930caa102dSLi Zefan 18940b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 18950caa102dSLi Zefan if (btrfs_root_readonly(root)) 18960caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 18970b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 18980caa102dSLi Zefan 18990caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 19000caa102dSLi Zefan ret = -EFAULT; 19010caa102dSLi Zefan 19020caa102dSLi Zefan return ret; 19030caa102dSLi Zefan } 19040caa102dSLi Zefan 19050caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 19060caa102dSLi Zefan void __user *arg) 19070caa102dSLi Zefan { 1908496ad9aaSAl Viro struct inode *inode = file_inode(file); 19090b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 19100caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 19110caa102dSLi Zefan struct btrfs_trans_handle *trans; 19120caa102dSLi Zefan u64 root_flags; 19130caa102dSLi Zefan u64 flags; 19140caa102dSLi Zefan int ret = 0; 19150caa102dSLi Zefan 1916bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1917bd60ea0fSDavid Sterba return -EPERM; 1918bd60ea0fSDavid Sterba 1919b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1920b9ca0664SLiu Bo if (ret) 1921b9ca0664SLiu Bo goto out; 19220caa102dSLi Zefan 19234a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1924b9ca0664SLiu Bo ret = -EINVAL; 1925b9ca0664SLiu Bo goto out_drop_write; 1926b9ca0664SLiu Bo } 19270caa102dSLi Zefan 1928b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1929b9ca0664SLiu Bo ret = -EFAULT; 1930b9ca0664SLiu Bo goto out_drop_write; 1931b9ca0664SLiu Bo } 19320caa102dSLi Zefan 1933b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1934b9ca0664SLiu Bo ret = -EINVAL; 1935b9ca0664SLiu Bo goto out_drop_write; 1936b9ca0664SLiu Bo } 19370caa102dSLi Zefan 1938b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1939b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1940b9ca0664SLiu Bo goto out_drop_write; 1941b9ca0664SLiu Bo } 19420caa102dSLi Zefan 19430b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 19440caa102dSLi Zefan 19450caa102dSLi Zefan /* nothing to do */ 19460caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1947b9ca0664SLiu Bo goto out_drop_sem; 19480caa102dSLi Zefan 19490caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 19502c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 19510caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19520caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 19532c686537SDavid Sterba } else { 19542c686537SDavid Sterba /* 19552c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 19562c686537SDavid Sterba * send 19572c686537SDavid Sterba */ 19582c686537SDavid Sterba spin_lock(&root->root_item_lock); 19592c686537SDavid Sterba if (root->send_in_progress == 0) { 19600caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 19610caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 19622c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19632c686537SDavid Sterba } else { 19642c686537SDavid Sterba spin_unlock(&root->root_item_lock); 19650b246afaSJeff Mahoney btrfs_warn(fs_info, 19662c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 19672c686537SDavid Sterba root->root_key.objectid); 19682c686537SDavid Sterba ret = -EPERM; 19692c686537SDavid Sterba goto out_drop_sem; 19702c686537SDavid Sterba } 19712c686537SDavid Sterba } 19720caa102dSLi Zefan 19730caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 19740caa102dSLi Zefan if (IS_ERR(trans)) { 19750caa102dSLi Zefan ret = PTR_ERR(trans); 19760caa102dSLi Zefan goto out_reset; 19770caa102dSLi Zefan } 19780caa102dSLi Zefan 19790b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 19800caa102dSLi Zefan &root->root_key, &root->root_item); 19819417ebc8SNikolay Borisov if (ret < 0) { 19829417ebc8SNikolay Borisov btrfs_end_transaction(trans); 19839417ebc8SNikolay Borisov goto out_reset; 19849417ebc8SNikolay Borisov } 19850caa102dSLi Zefan 19869417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 19879417ebc8SNikolay Borisov 19880caa102dSLi Zefan out_reset: 19890caa102dSLi Zefan if (ret) 19900caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1991b9ca0664SLiu Bo out_drop_sem: 19920b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 1993b9ca0664SLiu Bo out_drop_write: 1994b9ca0664SLiu Bo mnt_drop_write_file(file); 1995b9ca0664SLiu Bo out: 19960caa102dSLi Zefan return ret; 19970caa102dSLi Zefan } 19980caa102dSLi Zefan 1999ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 2000ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 2001ac8e9819SChris Mason { 2002abc6e134SChris Mason struct btrfs_key test; 2003abc6e134SChris Mason int ret; 2004abc6e134SChris Mason 2005abc6e134SChris Mason test.objectid = sk->min_objectid; 2006abc6e134SChris Mason test.type = sk->min_type; 2007abc6e134SChris Mason test.offset = sk->min_offset; 2008abc6e134SChris Mason 2009abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2010abc6e134SChris Mason if (ret < 0) 2011ac8e9819SChris Mason return 0; 2012abc6e134SChris Mason 2013abc6e134SChris Mason test.objectid = sk->max_objectid; 2014abc6e134SChris Mason test.type = sk->max_type; 2015abc6e134SChris Mason test.offset = sk->max_offset; 2016abc6e134SChris Mason 2017abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 2018abc6e134SChris Mason if (ret > 0) 2019ac8e9819SChris Mason return 0; 2020ac8e9819SChris Mason return 1; 2021ac8e9819SChris Mason } 2022ac8e9819SChris Mason 2023df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 2024ac8e9819SChris Mason struct btrfs_key *key, 2025ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 20269b6e817dSGerhard Heift size_t *buf_size, 2027ba346b35SGerhard Heift char __user *ubuf, 2028ac8e9819SChris Mason unsigned long *sk_offset, 2029ac8e9819SChris Mason int *num_found) 2030ac8e9819SChris Mason { 2031ac8e9819SChris Mason u64 found_transid; 2032ac8e9819SChris Mason struct extent_buffer *leaf; 2033ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 2034dd81d459SNaohiro Aota struct btrfs_key test; 2035ac8e9819SChris Mason unsigned long item_off; 2036ac8e9819SChris Mason unsigned long item_len; 2037ac8e9819SChris Mason int nritems; 2038ac8e9819SChris Mason int i; 2039ac8e9819SChris Mason int slot; 2040ac8e9819SChris Mason int ret = 0; 2041ac8e9819SChris Mason 2042ac8e9819SChris Mason leaf = path->nodes[0]; 2043ac8e9819SChris Mason slot = path->slots[0]; 2044ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 2045ac8e9819SChris Mason 2046ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 2047ac8e9819SChris Mason i = nritems; 2048ac8e9819SChris Mason goto advance_key; 2049ac8e9819SChris Mason } 2050ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 2051ac8e9819SChris Mason 2052ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 2053ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 2054ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 2055ac8e9819SChris Mason 205603b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 205703b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 205803b71c6cSGabriel de Perthuis continue; 205903b71c6cSGabriel de Perthuis 20609b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 20618f5f6178SGerhard Heift if (*num_found) { 2062ac8e9819SChris Mason ret = 1; 20638f5f6178SGerhard Heift goto out; 20648f5f6178SGerhard Heift } 20658f5f6178SGerhard Heift 20668f5f6178SGerhard Heift /* 20678f5f6178SGerhard Heift * return one empty item back for v1, which does not 20688f5f6178SGerhard Heift * handle -EOVERFLOW 20698f5f6178SGerhard Heift */ 20708f5f6178SGerhard Heift 20719b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2072ac8e9819SChris Mason item_len = 0; 20738f5f6178SGerhard Heift ret = -EOVERFLOW; 20748f5f6178SGerhard Heift } 2075ac8e9819SChris Mason 20769b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2077ac8e9819SChris Mason ret = 1; 207825c9bc2eSGerhard Heift goto out; 2079ac8e9819SChris Mason } 2080ac8e9819SChris Mason 2081ac8e9819SChris Mason sh.objectid = key->objectid; 2082ac8e9819SChris Mason sh.offset = key->offset; 2083ac8e9819SChris Mason sh.type = key->type; 2084ac8e9819SChris Mason sh.len = item_len; 2085ac8e9819SChris Mason sh.transid = found_transid; 2086ac8e9819SChris Mason 2087ac8e9819SChris Mason /* copy search result header */ 2088ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 2089ba346b35SGerhard Heift ret = -EFAULT; 2090ba346b35SGerhard Heift goto out; 2091ba346b35SGerhard Heift } 2092ba346b35SGerhard Heift 2093ac8e9819SChris Mason *sk_offset += sizeof(sh); 2094ac8e9819SChris Mason 2095ac8e9819SChris Mason if (item_len) { 2096ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2097ac8e9819SChris Mason /* copy the item */ 2098ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2099ba346b35SGerhard Heift item_off, item_len)) { 2100ba346b35SGerhard Heift ret = -EFAULT; 2101ba346b35SGerhard Heift goto out; 2102ba346b35SGerhard Heift } 2103ba346b35SGerhard Heift 2104ac8e9819SChris Mason *sk_offset += item_len; 2105ac8e9819SChris Mason } 2106e2156867SHugo Mills (*num_found)++; 2107ac8e9819SChris Mason 21088f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 21098f5f6178SGerhard Heift goto out; 21108f5f6178SGerhard Heift 211125c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 211225c9bc2eSGerhard Heift ret = 1; 211325c9bc2eSGerhard Heift goto out; 211425c9bc2eSGerhard Heift } 2115ac8e9819SChris Mason } 2116ac8e9819SChris Mason advance_key: 2117ac8e9819SChris Mason ret = 0; 2118dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2119dd81d459SNaohiro Aota test.type = sk->max_type; 2120dd81d459SNaohiro Aota test.offset = sk->max_offset; 2121dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2122dd81d459SNaohiro Aota ret = 1; 2123dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2124abc6e134SChris Mason key->offset++; 2125dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2126abc6e134SChris Mason key->offset = 0; 2127abc6e134SChris Mason key->type++; 2128dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2129abc6e134SChris Mason key->offset = 0; 2130abc6e134SChris Mason key->type = 0; 2131abc6e134SChris Mason key->objectid++; 2132abc6e134SChris Mason } else 2133abc6e134SChris Mason ret = 1; 213425c9bc2eSGerhard Heift out: 2135ba346b35SGerhard Heift /* 2136ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2137ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2138ba346b35SGerhard Heift * * all items were found 2139ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2140ba346b35SGerhard Heift * leaf 2141ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2142ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2143ba346b35SGerhard Heift */ 2144ac8e9819SChris Mason return ret; 2145ac8e9819SChris Mason } 2146ac8e9819SChris Mason 2147ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 214812544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 21499b6e817dSGerhard Heift size_t *buf_size, 2150ba346b35SGerhard Heift char __user *ubuf) 2151ac8e9819SChris Mason { 21520b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2153ac8e9819SChris Mason struct btrfs_root *root; 2154ac8e9819SChris Mason struct btrfs_key key; 2155ac8e9819SChris Mason struct btrfs_path *path; 2156ac8e9819SChris Mason int ret; 2157ac8e9819SChris Mason int num_found = 0; 2158ac8e9819SChris Mason unsigned long sk_offset = 0; 2159ac8e9819SChris Mason 21609b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 21619b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 216212544442SGerhard Heift return -EOVERFLOW; 21639b6e817dSGerhard Heift } 216412544442SGerhard Heift 2165ac8e9819SChris Mason path = btrfs_alloc_path(); 2166ac8e9819SChris Mason if (!path) 2167ac8e9819SChris Mason return -ENOMEM; 2168ac8e9819SChris Mason 2169ac8e9819SChris Mason if (sk->tree_id == 0) { 2170ac8e9819SChris Mason /* search the root of the inode that was passed */ 2171ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2172ac8e9819SChris Mason } else { 2173ac8e9819SChris Mason key.objectid = sk->tree_id; 2174ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2175ac8e9819SChris Mason key.offset = (u64)-1; 2176ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2177ac8e9819SChris Mason if (IS_ERR(root)) { 2178ac8e9819SChris Mason btrfs_free_path(path); 2179ad1e3d56SMisono Tomohiro return PTR_ERR(root); 2180ac8e9819SChris Mason } 2181ac8e9819SChris Mason } 2182ac8e9819SChris Mason 2183ac8e9819SChris Mason key.objectid = sk->min_objectid; 2184ac8e9819SChris Mason key.type = sk->min_type; 2185ac8e9819SChris Mason key.offset = sk->min_offset; 2186ac8e9819SChris Mason 2187ac8e9819SChris Mason while (1) { 21886174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2189ac8e9819SChris Mason if (ret != 0) { 2190ac8e9819SChris Mason if (ret > 0) 2191ac8e9819SChris Mason ret = 0; 2192ac8e9819SChris Mason goto err; 2193ac8e9819SChris Mason } 2194df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2195ac8e9819SChris Mason &sk_offset, &num_found); 2196b3b4aa74SDavid Sterba btrfs_release_path(path); 219725c9bc2eSGerhard Heift if (ret) 2198ac8e9819SChris Mason break; 2199ac8e9819SChris Mason 2200ac8e9819SChris Mason } 22018f5f6178SGerhard Heift if (ret > 0) 2202ac8e9819SChris Mason ret = 0; 2203ac8e9819SChris Mason err: 2204ac8e9819SChris Mason sk->nr_items = num_found; 2205ac8e9819SChris Mason btrfs_free_path(path); 2206ac8e9819SChris Mason return ret; 2207ac8e9819SChris Mason } 2208ac8e9819SChris Mason 2209ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2210ac8e9819SChris Mason void __user *argp) 2211ac8e9819SChris Mason { 2212ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2213ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2214ac8e9819SChris Mason struct inode *inode; 2215ac8e9819SChris Mason int ret; 22169b6e817dSGerhard Heift size_t buf_size; 2217ac8e9819SChris Mason 2218ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2219ac8e9819SChris Mason return -EPERM; 2220ac8e9819SChris Mason 2221ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2222ac8e9819SChris Mason 2223ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2224ba346b35SGerhard Heift return -EFAULT; 2225ba346b35SGerhard Heift 2226ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2227ac8e9819SChris Mason 2228496ad9aaSAl Viro inode = file_inode(file); 2229ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 22308f5f6178SGerhard Heift 22318f5f6178SGerhard Heift /* 22328f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 22338f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 22348f5f6178SGerhard Heift */ 22358f5f6178SGerhard Heift if (ret == -EOVERFLOW) 22368f5f6178SGerhard Heift ret = 0; 22378f5f6178SGerhard Heift 2238ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2239ac8e9819SChris Mason ret = -EFAULT; 2240ac8e9819SChris Mason return ret; 2241ac8e9819SChris Mason } 2242ac8e9819SChris Mason 2243cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2244cc68a8a5SGerhard Heift void __user *argp) 2245cc68a8a5SGerhard Heift { 2246cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2247cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2248cc68a8a5SGerhard Heift struct inode *inode; 2249cc68a8a5SGerhard Heift int ret; 2250cc68a8a5SGerhard Heift size_t buf_size; 2251ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2252cc68a8a5SGerhard Heift 2253cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2254cc68a8a5SGerhard Heift return -EPERM; 2255cc68a8a5SGerhard Heift 2256cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2257cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2258cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2259cc68a8a5SGerhard Heift return -EFAULT; 2260cc68a8a5SGerhard Heift 2261cc68a8a5SGerhard Heift buf_size = args.buf_size; 2262cc68a8a5SGerhard Heift 2263cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2264cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2265cc68a8a5SGerhard Heift buf_size = buf_limit; 2266cc68a8a5SGerhard Heift 2267cc68a8a5SGerhard Heift inode = file_inode(file); 2268cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2269718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2270cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2271cc68a8a5SGerhard Heift ret = -EFAULT; 2272cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2273cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2274cc68a8a5SGerhard Heift ret = -EFAULT; 2275cc68a8a5SGerhard Heift 227676dda93cSYan, Zheng return ret; 227776dda93cSYan, Zheng } 227876dda93cSYan, Zheng 227998d377a0STARUISI Hiroaki /* 2280ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2281ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 228298d377a0STARUISI Hiroaki */ 228398d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 228498d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 228598d377a0STARUISI Hiroaki { 228698d377a0STARUISI Hiroaki struct btrfs_root *root; 228798d377a0STARUISI Hiroaki struct btrfs_key key; 2288ac8e9819SChris Mason char *ptr; 228998d377a0STARUISI Hiroaki int ret = -1; 229098d377a0STARUISI Hiroaki int slot; 229198d377a0STARUISI Hiroaki int len; 229298d377a0STARUISI Hiroaki int total_len = 0; 229398d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 229498d377a0STARUISI Hiroaki struct extent_buffer *l; 229598d377a0STARUISI Hiroaki struct btrfs_path *path; 229698d377a0STARUISI Hiroaki 229798d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 229898d377a0STARUISI Hiroaki name[0]='\0'; 229998d377a0STARUISI Hiroaki return 0; 230098d377a0STARUISI Hiroaki } 230198d377a0STARUISI Hiroaki 230298d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 230398d377a0STARUISI Hiroaki if (!path) 230498d377a0STARUISI Hiroaki return -ENOMEM; 230598d377a0STARUISI Hiroaki 2306c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 230798d377a0STARUISI Hiroaki 230898d377a0STARUISI Hiroaki key.objectid = tree_id; 230998d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 231098d377a0STARUISI Hiroaki key.offset = (u64)-1; 231198d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 231298d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2313ad1e3d56SMisono Tomohiro ret = PTR_ERR(root); 23148ad6fcabSChris Mason goto out; 231598d377a0STARUISI Hiroaki } 231698d377a0STARUISI Hiroaki 231798d377a0STARUISI Hiroaki key.objectid = dirid; 231898d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 23198ad6fcabSChris Mason key.offset = (u64)-1; 232098d377a0STARUISI Hiroaki 232198d377a0STARUISI Hiroaki while (1) { 232298d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 232398d377a0STARUISI Hiroaki if (ret < 0) 232498d377a0STARUISI Hiroaki goto out; 232518674c6cSFilipe David Borba Manana else if (ret > 0) { 232618674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 232718674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 232818674c6cSFilipe David Borba Manana if (ret < 0) 232918674c6cSFilipe David Borba Manana goto out; 233018674c6cSFilipe David Borba Manana else if (ret > 0) { 2331ac8e9819SChris Mason ret = -ENOENT; 233298d377a0STARUISI Hiroaki goto out; 2333ac8e9819SChris Mason } 233418674c6cSFilipe David Borba Manana } 233518674c6cSFilipe David Borba Manana 233618674c6cSFilipe David Borba Manana l = path->nodes[0]; 233718674c6cSFilipe David Borba Manana slot = path->slots[0]; 233818674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 233998d377a0STARUISI Hiroaki 234098d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 234198d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 234298d377a0STARUISI Hiroaki ptr -= len + 1; 234398d377a0STARUISI Hiroaki total_len += len + 1; 2344a696cf35SFilipe David Borba Manana if (ptr < name) { 2345a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 234698d377a0STARUISI Hiroaki goto out; 2347a696cf35SFilipe David Borba Manana } 234898d377a0STARUISI Hiroaki 234998d377a0STARUISI Hiroaki *(ptr + len) = '/'; 235098d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 235198d377a0STARUISI Hiroaki 235298d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 235398d377a0STARUISI Hiroaki break; 235498d377a0STARUISI Hiroaki 2355b3b4aa74SDavid Sterba btrfs_release_path(path); 235698d377a0STARUISI Hiroaki key.objectid = key.offset; 23578ad6fcabSChris Mason key.offset = (u64)-1; 235898d377a0STARUISI Hiroaki dirid = key.objectid; 235998d377a0STARUISI Hiroaki } 236077906a50SLi Zefan memmove(name, ptr, total_len); 236198d377a0STARUISI Hiroaki name[total_len] = '\0'; 236298d377a0STARUISI Hiroaki ret = 0; 236398d377a0STARUISI Hiroaki out: 236498d377a0STARUISI Hiroaki btrfs_free_path(path); 2365ac8e9819SChris Mason return ret; 2366ac8e9819SChris Mason } 2367ac8e9819SChris Mason 236823d0b79dSTomohiro Misono static int btrfs_search_path_in_tree_user(struct inode *inode, 236923d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args) 237023d0b79dSTomohiro Misono { 237123d0b79dSTomohiro Misono struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 237223d0b79dSTomohiro Misono struct super_block *sb = inode->i_sb; 237323d0b79dSTomohiro Misono struct btrfs_key upper_limit = BTRFS_I(inode)->location; 237423d0b79dSTomohiro Misono u64 treeid = BTRFS_I(inode)->root->root_key.objectid; 237523d0b79dSTomohiro Misono u64 dirid = args->dirid; 237623d0b79dSTomohiro Misono unsigned long item_off; 237723d0b79dSTomohiro Misono unsigned long item_len; 237823d0b79dSTomohiro Misono struct btrfs_inode_ref *iref; 237923d0b79dSTomohiro Misono struct btrfs_root_ref *rref; 238023d0b79dSTomohiro Misono struct btrfs_root *root; 238123d0b79dSTomohiro Misono struct btrfs_path *path; 238223d0b79dSTomohiro Misono struct btrfs_key key, key2; 238323d0b79dSTomohiro Misono struct extent_buffer *leaf; 238423d0b79dSTomohiro Misono struct inode *temp_inode; 238523d0b79dSTomohiro Misono char *ptr; 238623d0b79dSTomohiro Misono int slot; 238723d0b79dSTomohiro Misono int len; 238823d0b79dSTomohiro Misono int total_len = 0; 238923d0b79dSTomohiro Misono int ret; 239023d0b79dSTomohiro Misono 239123d0b79dSTomohiro Misono path = btrfs_alloc_path(); 239223d0b79dSTomohiro Misono if (!path) 239323d0b79dSTomohiro Misono return -ENOMEM; 239423d0b79dSTomohiro Misono 239523d0b79dSTomohiro Misono /* 239623d0b79dSTomohiro Misono * If the bottom subvolume does not exist directly under upper_limit, 239723d0b79dSTomohiro Misono * construct the path in from the bottom up. 239823d0b79dSTomohiro Misono */ 239923d0b79dSTomohiro Misono if (dirid != upper_limit.objectid) { 240023d0b79dSTomohiro Misono ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; 240123d0b79dSTomohiro Misono 240223d0b79dSTomohiro Misono key.objectid = treeid; 240323d0b79dSTomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 240423d0b79dSTomohiro Misono key.offset = (u64)-1; 240523d0b79dSTomohiro Misono root = btrfs_read_fs_root_no_name(fs_info, &key); 240623d0b79dSTomohiro Misono if (IS_ERR(root)) { 240723d0b79dSTomohiro Misono ret = PTR_ERR(root); 240823d0b79dSTomohiro Misono goto out; 240923d0b79dSTomohiro Misono } 241023d0b79dSTomohiro Misono 241123d0b79dSTomohiro Misono key.objectid = dirid; 241223d0b79dSTomohiro Misono key.type = BTRFS_INODE_REF_KEY; 241323d0b79dSTomohiro Misono key.offset = (u64)-1; 241423d0b79dSTomohiro Misono while (1) { 241523d0b79dSTomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 241623d0b79dSTomohiro Misono if (ret < 0) { 241723d0b79dSTomohiro Misono goto out; 241823d0b79dSTomohiro Misono } else if (ret > 0) { 241923d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 242023d0b79dSTomohiro Misono BTRFS_INODE_REF_KEY); 242123d0b79dSTomohiro Misono if (ret < 0) { 242223d0b79dSTomohiro Misono goto out; 242323d0b79dSTomohiro Misono } else if (ret > 0) { 242423d0b79dSTomohiro Misono ret = -ENOENT; 242523d0b79dSTomohiro Misono goto out; 242623d0b79dSTomohiro Misono } 242723d0b79dSTomohiro Misono } 242823d0b79dSTomohiro Misono 242923d0b79dSTomohiro Misono leaf = path->nodes[0]; 243023d0b79dSTomohiro Misono slot = path->slots[0]; 243123d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 243223d0b79dSTomohiro Misono 243323d0b79dSTomohiro Misono iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); 243423d0b79dSTomohiro Misono len = btrfs_inode_ref_name_len(leaf, iref); 243523d0b79dSTomohiro Misono ptr -= len + 1; 243623d0b79dSTomohiro Misono total_len += len + 1; 243723d0b79dSTomohiro Misono if (ptr < args->path) { 243823d0b79dSTomohiro Misono ret = -ENAMETOOLONG; 243923d0b79dSTomohiro Misono goto out; 244023d0b79dSTomohiro Misono } 244123d0b79dSTomohiro Misono 244223d0b79dSTomohiro Misono *(ptr + len) = '/'; 244323d0b79dSTomohiro Misono read_extent_buffer(leaf, ptr, 244423d0b79dSTomohiro Misono (unsigned long)(iref + 1), len); 244523d0b79dSTomohiro Misono 244623d0b79dSTomohiro Misono /* Check the read+exec permission of this directory */ 244723d0b79dSTomohiro Misono ret = btrfs_previous_item(root, path, dirid, 244823d0b79dSTomohiro Misono BTRFS_INODE_ITEM_KEY); 244923d0b79dSTomohiro Misono if (ret < 0) { 245023d0b79dSTomohiro Misono goto out; 245123d0b79dSTomohiro Misono } else if (ret > 0) { 245223d0b79dSTomohiro Misono ret = -ENOENT; 245323d0b79dSTomohiro Misono goto out; 245423d0b79dSTomohiro Misono } 245523d0b79dSTomohiro Misono 245623d0b79dSTomohiro Misono leaf = path->nodes[0]; 245723d0b79dSTomohiro Misono slot = path->slots[0]; 245823d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key2, slot); 245923d0b79dSTomohiro Misono if (key2.objectid != dirid) { 246023d0b79dSTomohiro Misono ret = -ENOENT; 246123d0b79dSTomohiro Misono goto out; 246223d0b79dSTomohiro Misono } 246323d0b79dSTomohiro Misono 24644c66e0d4SDavid Sterba temp_inode = btrfs_iget(sb, &key2, root); 24653ca57bd6SMisono Tomohiro if (IS_ERR(temp_inode)) { 24663ca57bd6SMisono Tomohiro ret = PTR_ERR(temp_inode); 24673ca57bd6SMisono Tomohiro goto out; 24683ca57bd6SMisono Tomohiro } 246923d0b79dSTomohiro Misono ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); 247023d0b79dSTomohiro Misono iput(temp_inode); 247123d0b79dSTomohiro Misono if (ret) { 247223d0b79dSTomohiro Misono ret = -EACCES; 247323d0b79dSTomohiro Misono goto out; 247423d0b79dSTomohiro Misono } 247523d0b79dSTomohiro Misono 247623d0b79dSTomohiro Misono if (key.offset == upper_limit.objectid) 247723d0b79dSTomohiro Misono break; 247823d0b79dSTomohiro Misono if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { 247923d0b79dSTomohiro Misono ret = -EACCES; 248023d0b79dSTomohiro Misono goto out; 248123d0b79dSTomohiro Misono } 248223d0b79dSTomohiro Misono 248323d0b79dSTomohiro Misono btrfs_release_path(path); 248423d0b79dSTomohiro Misono key.objectid = key.offset; 248523d0b79dSTomohiro Misono key.offset = (u64)-1; 248623d0b79dSTomohiro Misono dirid = key.objectid; 248723d0b79dSTomohiro Misono } 248823d0b79dSTomohiro Misono 248923d0b79dSTomohiro Misono memmove(args->path, ptr, total_len); 249023d0b79dSTomohiro Misono args->path[total_len] = '\0'; 249123d0b79dSTomohiro Misono btrfs_release_path(path); 249223d0b79dSTomohiro Misono } 249323d0b79dSTomohiro Misono 249423d0b79dSTomohiro Misono /* Get the bottom subvolume's name from ROOT_REF */ 249523d0b79dSTomohiro Misono root = fs_info->tree_root; 249623d0b79dSTomohiro Misono key.objectid = treeid; 249723d0b79dSTomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 249823d0b79dSTomohiro Misono key.offset = args->treeid; 249923d0b79dSTomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 250023d0b79dSTomohiro Misono if (ret < 0) { 250123d0b79dSTomohiro Misono goto out; 250223d0b79dSTomohiro Misono } else if (ret > 0) { 250323d0b79dSTomohiro Misono ret = -ENOENT; 250423d0b79dSTomohiro Misono goto out; 250523d0b79dSTomohiro Misono } 250623d0b79dSTomohiro Misono 250723d0b79dSTomohiro Misono leaf = path->nodes[0]; 250823d0b79dSTomohiro Misono slot = path->slots[0]; 250923d0b79dSTomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 251023d0b79dSTomohiro Misono 251123d0b79dSTomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot); 251223d0b79dSTomohiro Misono item_len = btrfs_item_size_nr(leaf, slot); 251323d0b79dSTomohiro Misono /* Check if dirid in ROOT_REF corresponds to passed dirid */ 251423d0b79dSTomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 251523d0b79dSTomohiro Misono if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { 251623d0b79dSTomohiro Misono ret = -EINVAL; 251723d0b79dSTomohiro Misono goto out; 251823d0b79dSTomohiro Misono } 251923d0b79dSTomohiro Misono 252023d0b79dSTomohiro Misono /* Copy subvolume's name */ 252123d0b79dSTomohiro Misono item_off += sizeof(struct btrfs_root_ref); 252223d0b79dSTomohiro Misono item_len -= sizeof(struct btrfs_root_ref); 252323d0b79dSTomohiro Misono read_extent_buffer(leaf, args->name, item_off, item_len); 252423d0b79dSTomohiro Misono args->name[item_len] = 0; 252523d0b79dSTomohiro Misono 252623d0b79dSTomohiro Misono out: 252723d0b79dSTomohiro Misono btrfs_free_path(path); 252823d0b79dSTomohiro Misono return ret; 252923d0b79dSTomohiro Misono } 253023d0b79dSTomohiro Misono 2531ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2532ac8e9819SChris Mason void __user *argp) 2533ac8e9819SChris Mason { 2534ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2535ac8e9819SChris Mason struct inode *inode; 253601b810b8SDavid Sterba int ret = 0; 2537ac8e9819SChris Mason 25382354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 25392354d08fSJulia Lawall if (IS_ERR(args)) 25402354d08fSJulia Lawall return PTR_ERR(args); 2541c2b96929SDan Carpenter 2542496ad9aaSAl Viro inode = file_inode(file); 2543ac8e9819SChris Mason 254401b810b8SDavid Sterba /* 254501b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 254601b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 254701b810b8SDavid Sterba */ 25481b53ac4dSChris Mason if (args->treeid == 0) 25491b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 25501b53ac4dSChris Mason 255101b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 255201b810b8SDavid Sterba args->name[0] = 0; 255301b810b8SDavid Sterba goto out; 255401b810b8SDavid Sterba } 255501b810b8SDavid Sterba 255601b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 255701b810b8SDavid Sterba ret = -EPERM; 255801b810b8SDavid Sterba goto out; 255901b810b8SDavid Sterba } 256001b810b8SDavid Sterba 2561ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2562ac8e9819SChris Mason args->treeid, args->objectid, 2563ac8e9819SChris Mason args->name); 2564ac8e9819SChris Mason 256501b810b8SDavid Sterba out: 2566ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2567ac8e9819SChris Mason ret = -EFAULT; 2568ac8e9819SChris Mason 2569ac8e9819SChris Mason kfree(args); 257098d377a0STARUISI Hiroaki return ret; 257198d377a0STARUISI Hiroaki } 257298d377a0STARUISI Hiroaki 257323d0b79dSTomohiro Misono /* 257423d0b79dSTomohiro Misono * Version of ino_lookup ioctl (unprivileged) 257523d0b79dSTomohiro Misono * 257623d0b79dSTomohiro Misono * The main differences from ino_lookup ioctl are: 257723d0b79dSTomohiro Misono * 257823d0b79dSTomohiro Misono * 1. Read + Exec permission will be checked using inode_permission() during 257923d0b79dSTomohiro Misono * path construction. -EACCES will be returned in case of failure. 258023d0b79dSTomohiro Misono * 2. Path construction will be stopped at the inode number which corresponds 258123d0b79dSTomohiro Misono * to the fd with which this ioctl is called. If constructed path does not 258223d0b79dSTomohiro Misono * exist under fd's inode, -EACCES will be returned. 258323d0b79dSTomohiro Misono * 3. The name of bottom subvolume is also searched and filled. 258423d0b79dSTomohiro Misono */ 258523d0b79dSTomohiro Misono static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) 258623d0b79dSTomohiro Misono { 258723d0b79dSTomohiro Misono struct btrfs_ioctl_ino_lookup_user_args *args; 258823d0b79dSTomohiro Misono struct inode *inode; 258923d0b79dSTomohiro Misono int ret; 259023d0b79dSTomohiro Misono 259123d0b79dSTomohiro Misono args = memdup_user(argp, sizeof(*args)); 259223d0b79dSTomohiro Misono if (IS_ERR(args)) 259323d0b79dSTomohiro Misono return PTR_ERR(args); 259423d0b79dSTomohiro Misono 259523d0b79dSTomohiro Misono inode = file_inode(file); 259623d0b79dSTomohiro Misono 259723d0b79dSTomohiro Misono if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && 259823d0b79dSTomohiro Misono BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { 259923d0b79dSTomohiro Misono /* 260023d0b79dSTomohiro Misono * The subvolume does not exist under fd with which this is 260123d0b79dSTomohiro Misono * called 260223d0b79dSTomohiro Misono */ 260323d0b79dSTomohiro Misono kfree(args); 260423d0b79dSTomohiro Misono return -EACCES; 260523d0b79dSTomohiro Misono } 260623d0b79dSTomohiro Misono 260723d0b79dSTomohiro Misono ret = btrfs_search_path_in_tree_user(inode, args); 260823d0b79dSTomohiro Misono 260923d0b79dSTomohiro Misono if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 261023d0b79dSTomohiro Misono ret = -EFAULT; 261123d0b79dSTomohiro Misono 261223d0b79dSTomohiro Misono kfree(args); 261323d0b79dSTomohiro Misono return ret; 261423d0b79dSTomohiro Misono } 261523d0b79dSTomohiro Misono 2616b64ec075STomohiro Misono /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ 2617b64ec075STomohiro Misono static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) 2618b64ec075STomohiro Misono { 2619b64ec075STomohiro Misono struct btrfs_ioctl_get_subvol_info_args *subvol_info; 2620b64ec075STomohiro Misono struct btrfs_fs_info *fs_info; 2621b64ec075STomohiro Misono struct btrfs_root *root; 2622b64ec075STomohiro Misono struct btrfs_path *path; 2623b64ec075STomohiro Misono struct btrfs_key key; 2624b64ec075STomohiro Misono struct btrfs_root_item *root_item; 2625b64ec075STomohiro Misono struct btrfs_root_ref *rref; 2626b64ec075STomohiro Misono struct extent_buffer *leaf; 2627b64ec075STomohiro Misono unsigned long item_off; 2628b64ec075STomohiro Misono unsigned long item_len; 2629b64ec075STomohiro Misono struct inode *inode; 2630b64ec075STomohiro Misono int slot; 2631b64ec075STomohiro Misono int ret = 0; 2632b64ec075STomohiro Misono 2633b64ec075STomohiro Misono path = btrfs_alloc_path(); 2634b64ec075STomohiro Misono if (!path) 2635b64ec075STomohiro Misono return -ENOMEM; 2636b64ec075STomohiro Misono 2637b64ec075STomohiro Misono subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); 2638b64ec075STomohiro Misono if (!subvol_info) { 2639b64ec075STomohiro Misono btrfs_free_path(path); 2640b64ec075STomohiro Misono return -ENOMEM; 2641b64ec075STomohiro Misono } 2642b64ec075STomohiro Misono 2643b64ec075STomohiro Misono inode = file_inode(file); 2644b64ec075STomohiro Misono fs_info = BTRFS_I(inode)->root->fs_info; 2645b64ec075STomohiro Misono 2646b64ec075STomohiro Misono /* Get root_item of inode's subvolume */ 2647b64ec075STomohiro Misono key.objectid = BTRFS_I(inode)->root->root_key.objectid; 2648b64ec075STomohiro Misono key.type = BTRFS_ROOT_ITEM_KEY; 2649b64ec075STomohiro Misono key.offset = (u64)-1; 2650b64ec075STomohiro Misono root = btrfs_read_fs_root_no_name(fs_info, &key); 2651b64ec075STomohiro Misono if (IS_ERR(root)) { 2652b64ec075STomohiro Misono ret = PTR_ERR(root); 2653b64ec075STomohiro Misono goto out; 2654b64ec075STomohiro Misono } 2655b64ec075STomohiro Misono root_item = &root->root_item; 2656b64ec075STomohiro Misono 2657b64ec075STomohiro Misono subvol_info->treeid = key.objectid; 2658b64ec075STomohiro Misono 2659b64ec075STomohiro Misono subvol_info->generation = btrfs_root_generation(root_item); 2660b64ec075STomohiro Misono subvol_info->flags = btrfs_root_flags(root_item); 2661b64ec075STomohiro Misono 2662b64ec075STomohiro Misono memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); 2663b64ec075STomohiro Misono memcpy(subvol_info->parent_uuid, root_item->parent_uuid, 2664b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2665b64ec075STomohiro Misono memcpy(subvol_info->received_uuid, root_item->received_uuid, 2666b64ec075STomohiro Misono BTRFS_UUID_SIZE); 2667b64ec075STomohiro Misono 2668b64ec075STomohiro Misono subvol_info->ctransid = btrfs_root_ctransid(root_item); 2669b64ec075STomohiro Misono subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); 2670b64ec075STomohiro Misono subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); 2671b64ec075STomohiro Misono 2672b64ec075STomohiro Misono subvol_info->otransid = btrfs_root_otransid(root_item); 2673b64ec075STomohiro Misono subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); 2674b64ec075STomohiro Misono subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); 2675b64ec075STomohiro Misono 2676b64ec075STomohiro Misono subvol_info->stransid = btrfs_root_stransid(root_item); 2677b64ec075STomohiro Misono subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); 2678b64ec075STomohiro Misono subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); 2679b64ec075STomohiro Misono 2680b64ec075STomohiro Misono subvol_info->rtransid = btrfs_root_rtransid(root_item); 2681b64ec075STomohiro Misono subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); 2682b64ec075STomohiro Misono subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); 2683b64ec075STomohiro Misono 2684b64ec075STomohiro Misono if (key.objectid != BTRFS_FS_TREE_OBJECTID) { 2685b64ec075STomohiro Misono /* Search root tree for ROOT_BACKREF of this subvolume */ 2686b64ec075STomohiro Misono root = fs_info->tree_root; 2687b64ec075STomohiro Misono 2688b64ec075STomohiro Misono key.type = BTRFS_ROOT_BACKREF_KEY; 2689b64ec075STomohiro Misono key.offset = 0; 2690b64ec075STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2691b64ec075STomohiro Misono if (ret < 0) { 2692b64ec075STomohiro Misono goto out; 2693b64ec075STomohiro Misono } else if (path->slots[0] >= 2694b64ec075STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 2695b64ec075STomohiro Misono ret = btrfs_next_leaf(root, path); 2696b64ec075STomohiro Misono if (ret < 0) { 2697b64ec075STomohiro Misono goto out; 2698b64ec075STomohiro Misono } else if (ret > 0) { 2699b64ec075STomohiro Misono ret = -EUCLEAN; 2700b64ec075STomohiro Misono goto out; 2701b64ec075STomohiro Misono } 2702b64ec075STomohiro Misono } 2703b64ec075STomohiro Misono 2704b64ec075STomohiro Misono leaf = path->nodes[0]; 2705b64ec075STomohiro Misono slot = path->slots[0]; 2706b64ec075STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 2707b64ec075STomohiro Misono if (key.objectid == subvol_info->treeid && 2708b64ec075STomohiro Misono key.type == BTRFS_ROOT_BACKREF_KEY) { 2709b64ec075STomohiro Misono subvol_info->parent_id = key.offset; 2710b64ec075STomohiro Misono 2711b64ec075STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 2712b64ec075STomohiro Misono subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); 2713b64ec075STomohiro Misono 2714b64ec075STomohiro Misono item_off = btrfs_item_ptr_offset(leaf, slot) 2715b64ec075STomohiro Misono + sizeof(struct btrfs_root_ref); 2716b64ec075STomohiro Misono item_len = btrfs_item_size_nr(leaf, slot) 2717b64ec075STomohiro Misono - sizeof(struct btrfs_root_ref); 2718b64ec075STomohiro Misono read_extent_buffer(leaf, subvol_info->name, 2719b64ec075STomohiro Misono item_off, item_len); 2720b64ec075STomohiro Misono } else { 2721b64ec075STomohiro Misono ret = -ENOENT; 2722b64ec075STomohiro Misono goto out; 2723b64ec075STomohiro Misono } 2724b64ec075STomohiro Misono } 2725b64ec075STomohiro Misono 2726b64ec075STomohiro Misono if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) 2727b64ec075STomohiro Misono ret = -EFAULT; 2728b64ec075STomohiro Misono 2729b64ec075STomohiro Misono out: 2730b64ec075STomohiro Misono btrfs_free_path(path); 2731b64ec075STomohiro Misono kzfree(subvol_info); 2732b64ec075STomohiro Misono return ret; 2733b64ec075STomohiro Misono } 2734b64ec075STomohiro Misono 273542e4b520STomohiro Misono /* 273642e4b520STomohiro Misono * Return ROOT_REF information of the subvolume containing this inode 273742e4b520STomohiro Misono * except the subvolume name. 273842e4b520STomohiro Misono */ 273942e4b520STomohiro Misono static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) 274042e4b520STomohiro Misono { 274142e4b520STomohiro Misono struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; 274242e4b520STomohiro Misono struct btrfs_root_ref *rref; 274342e4b520STomohiro Misono struct btrfs_root *root; 274442e4b520STomohiro Misono struct btrfs_path *path; 274542e4b520STomohiro Misono struct btrfs_key key; 274642e4b520STomohiro Misono struct extent_buffer *leaf; 274742e4b520STomohiro Misono struct inode *inode; 274842e4b520STomohiro Misono u64 objectid; 274942e4b520STomohiro Misono int slot; 275042e4b520STomohiro Misono int ret; 275142e4b520STomohiro Misono u8 found; 275242e4b520STomohiro Misono 275342e4b520STomohiro Misono path = btrfs_alloc_path(); 275442e4b520STomohiro Misono if (!path) 275542e4b520STomohiro Misono return -ENOMEM; 275642e4b520STomohiro Misono 275742e4b520STomohiro Misono rootrefs = memdup_user(argp, sizeof(*rootrefs)); 275842e4b520STomohiro Misono if (IS_ERR(rootrefs)) { 275942e4b520STomohiro Misono btrfs_free_path(path); 276042e4b520STomohiro Misono return PTR_ERR(rootrefs); 276142e4b520STomohiro Misono } 276242e4b520STomohiro Misono 276342e4b520STomohiro Misono inode = file_inode(file); 276442e4b520STomohiro Misono root = BTRFS_I(inode)->root->fs_info->tree_root; 276542e4b520STomohiro Misono objectid = BTRFS_I(inode)->root->root_key.objectid; 276642e4b520STomohiro Misono 276742e4b520STomohiro Misono key.objectid = objectid; 276842e4b520STomohiro Misono key.type = BTRFS_ROOT_REF_KEY; 276942e4b520STomohiro Misono key.offset = rootrefs->min_treeid; 277042e4b520STomohiro Misono found = 0; 277142e4b520STomohiro Misono 277242e4b520STomohiro Misono ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 277342e4b520STomohiro Misono if (ret < 0) { 277442e4b520STomohiro Misono goto out; 277542e4b520STomohiro Misono } else if (path->slots[0] >= 277642e4b520STomohiro Misono btrfs_header_nritems(path->nodes[0])) { 277742e4b520STomohiro Misono ret = btrfs_next_leaf(root, path); 277842e4b520STomohiro Misono if (ret < 0) { 277942e4b520STomohiro Misono goto out; 278042e4b520STomohiro Misono } else if (ret > 0) { 278142e4b520STomohiro Misono ret = -EUCLEAN; 278242e4b520STomohiro Misono goto out; 278342e4b520STomohiro Misono } 278442e4b520STomohiro Misono } 278542e4b520STomohiro Misono while (1) { 278642e4b520STomohiro Misono leaf = path->nodes[0]; 278742e4b520STomohiro Misono slot = path->slots[0]; 278842e4b520STomohiro Misono 278942e4b520STomohiro Misono btrfs_item_key_to_cpu(leaf, &key, slot); 279042e4b520STomohiro Misono if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { 279142e4b520STomohiro Misono ret = 0; 279242e4b520STomohiro Misono goto out; 279342e4b520STomohiro Misono } 279442e4b520STomohiro Misono 279542e4b520STomohiro Misono if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { 279642e4b520STomohiro Misono ret = -EOVERFLOW; 279742e4b520STomohiro Misono goto out; 279842e4b520STomohiro Misono } 279942e4b520STomohiro Misono 280042e4b520STomohiro Misono rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); 280142e4b520STomohiro Misono rootrefs->rootref[found].treeid = key.offset; 280242e4b520STomohiro Misono rootrefs->rootref[found].dirid = 280342e4b520STomohiro Misono btrfs_root_ref_dirid(leaf, rref); 280442e4b520STomohiro Misono found++; 280542e4b520STomohiro Misono 280642e4b520STomohiro Misono ret = btrfs_next_item(root, path); 280742e4b520STomohiro Misono if (ret < 0) { 280842e4b520STomohiro Misono goto out; 280942e4b520STomohiro Misono } else if (ret > 0) { 281042e4b520STomohiro Misono ret = -EUCLEAN; 281142e4b520STomohiro Misono goto out; 281242e4b520STomohiro Misono } 281342e4b520STomohiro Misono } 281442e4b520STomohiro Misono 281542e4b520STomohiro Misono out: 281642e4b520STomohiro Misono if (!ret || ret == -EOVERFLOW) { 281742e4b520STomohiro Misono rootrefs->num_items = found; 281842e4b520STomohiro Misono /* update min_treeid for next search */ 281942e4b520STomohiro Misono if (found) 282042e4b520STomohiro Misono rootrefs->min_treeid = 282142e4b520STomohiro Misono rootrefs->rootref[found - 1].treeid + 1; 282242e4b520STomohiro Misono if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) 282342e4b520STomohiro Misono ret = -EFAULT; 282442e4b520STomohiro Misono } 282542e4b520STomohiro Misono 282642e4b520STomohiro Misono kfree(rootrefs); 282742e4b520STomohiro Misono btrfs_free_path(path); 282842e4b520STomohiro Misono 282942e4b520STomohiro Misono return ret; 283042e4b520STomohiro Misono } 283142e4b520STomohiro Misono 283276dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 283376dda93cSYan, Zheng void __user *arg) 283476dda93cSYan, Zheng { 283554563d41SAl Viro struct dentry *parent = file->f_path.dentry; 28360b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 283776dda93cSYan, Zheng struct dentry *dentry; 28382b0143b5SDavid Howells struct inode *dir = d_inode(parent); 283976dda93cSYan, Zheng struct inode *inode; 284076dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 284176dda93cSYan, Zheng struct btrfs_root *dest = NULL; 284276dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 284376dda93cSYan, Zheng int namelen; 284476dda93cSYan, Zheng int err = 0; 284576dda93cSYan, Zheng 2846325c50e3SJeff Mahoney if (!S_ISDIR(dir->i_mode)) 2847325c50e3SJeff Mahoney return -ENOTDIR; 2848325c50e3SJeff Mahoney 284976dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 285076dda93cSYan, Zheng if (IS_ERR(vol_args)) 285176dda93cSYan, Zheng return PTR_ERR(vol_args); 285276dda93cSYan, Zheng 285376dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 285476dda93cSYan, Zheng namelen = strlen(vol_args->name); 285576dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 285676dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 285776dda93cSYan, Zheng err = -EINVAL; 285876dda93cSYan, Zheng goto out; 285976dda93cSYan, Zheng } 286076dda93cSYan, Zheng 2861a561be71SAl Viro err = mnt_want_write_file(file); 286276dda93cSYan, Zheng if (err) 286376dda93cSYan, Zheng goto out; 286476dda93cSYan, Zheng 2865521e0546SDavid Sterba 286600235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 286700235411SAl Viro if (err == -EINTR) 286800235411SAl Viro goto out_drop_write; 286976dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 287076dda93cSYan, Zheng if (IS_ERR(dentry)) { 287176dda93cSYan, Zheng err = PTR_ERR(dentry); 287276dda93cSYan, Zheng goto out_unlock_dir; 287376dda93cSYan, Zheng } 287476dda93cSYan, Zheng 28752b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 287676dda93cSYan, Zheng err = -ENOENT; 287776dda93cSYan, Zheng goto out_dput; 287876dda93cSYan, Zheng } 287976dda93cSYan, Zheng 28802b0143b5SDavid Howells inode = d_inode(dentry); 28814260f7c7SSage Weil dest = BTRFS_I(inode)->root; 28824260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 28834260f7c7SSage Weil /* 28844260f7c7SSage Weil * Regular user. Only allow this with a special mount 28854260f7c7SSage Weil * option, when the user has write+exec access to the 28864260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 28874260f7c7SSage Weil * allowed. 28884260f7c7SSage Weil * 28894260f7c7SSage Weil * Note that this is _not_ check that the subvol is 28904260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 28914260f7c7SSage Weil * otherwise be able to delete. 28924260f7c7SSage Weil * 28934260f7c7SSage Weil * Users who want to delete empty subvols should try 28944260f7c7SSage Weil * rmdir(2). 28954260f7c7SSage Weil */ 28964260f7c7SSage Weil err = -EPERM; 28970b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 28984260f7c7SSage Weil goto out_dput; 28994260f7c7SSage Weil 29004260f7c7SSage Weil /* 29014260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 29024260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 29034260f7c7SSage Weil * must be called on the dentry referencing the root 29044260f7c7SSage Weil * of the subvol, not a random directory contained 29054260f7c7SSage Weil * within it. 29064260f7c7SSage Weil */ 29074260f7c7SSage Weil err = -EINVAL; 29084260f7c7SSage Weil if (root == dest) 29094260f7c7SSage Weil goto out_dput; 29104260f7c7SSage Weil 29114260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 29124260f7c7SSage Weil if (err) 29134260f7c7SSage Weil goto out_dput; 29145c39da5bSMiao Xie } 29154260f7c7SSage Weil 29165c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 29174260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 29184260f7c7SSage Weil if (err) 29194260f7c7SSage Weil goto out_dput; 29204260f7c7SSage Weil 29214a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 292276dda93cSYan, Zheng err = -EINVAL; 292376dda93cSYan, Zheng goto out_dput; 292476dda93cSYan, Zheng } 292576dda93cSYan, Zheng 29265955102cSAl Viro inode_lock(inode); 2927f60a2364SMisono Tomohiro err = btrfs_delete_subvolume(dir, dentry); 29285955102cSAl Viro inode_unlock(inode); 292946008d9dSAmir Goldstein if (!err) { 293046008d9dSAmir Goldstein fsnotify_rmdir(dir, dentry); 293176dda93cSYan, Zheng d_delete(dentry); 293246008d9dSAmir Goldstein } 2933fa6ac876SLiu Bo 293476dda93cSYan, Zheng out_dput: 293576dda93cSYan, Zheng dput(dentry); 293676dda93cSYan, Zheng out_unlock_dir: 29375955102cSAl Viro inode_unlock(dir); 293800235411SAl Viro out_drop_write: 29392a79f17eSAl Viro mnt_drop_write_file(file); 294076dda93cSYan, Zheng out: 294176dda93cSYan, Zheng kfree(vol_args); 294276dda93cSYan, Zheng return err; 294376dda93cSYan, Zheng } 294476dda93cSYan, Zheng 29451e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2946f46b5a66SChristoph Hellwig { 2947496ad9aaSAl Viro struct inode *inode = file_inode(file); 2948f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 29491e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2950c146afadSYan Zheng int ret; 2951c146afadSYan Zheng 295225122d15SIlya Dryomov ret = mnt_want_write_file(file); 295325122d15SIlya Dryomov if (ret) 295425122d15SIlya Dryomov return ret; 2955b83cc969SLi Zefan 295625122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 295725122d15SIlya Dryomov ret = -EROFS; 295825122d15SIlya Dryomov goto out; 29595ac00addSStefan Behrens } 2960f46b5a66SChristoph Hellwig 2961f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2962f46b5a66SChristoph Hellwig case S_IFDIR: 2963e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2964e441d54dSChris Mason ret = -EPERM; 2965e441d54dSChris Mason goto out; 2966e441d54dSChris Mason } 2967de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 2968f46b5a66SChristoph Hellwig break; 2969f46b5a66SChristoph Hellwig case S_IFREG: 2970616d374eSAdam Borowski /* 2971616d374eSAdam Borowski * Note that this does not check the file descriptor for write 2972616d374eSAdam Borowski * access. This prevents defragmenting executables that are 2973616d374eSAdam Borowski * running and allows defrag on files open in read-only mode. 2974616d374eSAdam Borowski */ 2975616d374eSAdam Borowski if (!capable(CAP_SYS_ADMIN) && 2976616d374eSAdam Borowski inode_permission(inode, MAY_WRITE)) { 2977616d374eSAdam Borowski ret = -EPERM; 2978e441d54dSChris Mason goto out; 2979e441d54dSChris Mason } 29801e701a32SChris Mason 29811e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 29821e701a32SChris Mason if (!range) { 29831e701a32SChris Mason ret = -ENOMEM; 29841e701a32SChris Mason goto out; 29851e701a32SChris Mason } 29861e701a32SChris Mason 29871e701a32SChris Mason if (argp) { 29881e701a32SChris Mason if (copy_from_user(range, argp, 29891e701a32SChris Mason sizeof(*range))) { 29901e701a32SChris Mason ret = -EFAULT; 29911e701a32SChris Mason kfree(range); 2992683be16eSDan Carpenter goto out; 29931e701a32SChris Mason } 29941e701a32SChris Mason /* compression requires us to start the IO */ 29951e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 29961e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 29971e701a32SChris Mason range->extent_thresh = (u32)-1; 29981e701a32SChris Mason } 29991e701a32SChris Mason } else { 30001e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 30011e701a32SChris Mason range->len = (u64)-1; 30021e701a32SChris Mason } 3003496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 30047c829b72SAnand Jain range, BTRFS_OLDEST_GENERATION, 0); 30054cb5300bSChris Mason if (ret > 0) 30064cb5300bSChris Mason ret = 0; 30071e701a32SChris Mason kfree(range); 3008f46b5a66SChristoph Hellwig break; 30098929ecfaSYan, Zheng default: 30108929ecfaSYan, Zheng ret = -EINVAL; 3011f46b5a66SChristoph Hellwig } 3012e441d54dSChris Mason out: 301325122d15SIlya Dryomov mnt_drop_write_file(file); 3014e441d54dSChris Mason return ret; 3015f46b5a66SChristoph Hellwig } 3016f46b5a66SChristoph Hellwig 30172ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 3018f46b5a66SChristoph Hellwig { 3019f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3020f46b5a66SChristoph Hellwig int ret; 3021f46b5a66SChristoph Hellwig 3022e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3023e441d54dSChris Mason return -EPERM; 3024e441d54dSChris Mason 3025171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) 3026e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3027c9e9f97bSIlya Dryomov 3028dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3029c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3030c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3031c9e9f97bSIlya Dryomov goto out; 3032c9e9f97bSIlya Dryomov } 3033f46b5a66SChristoph Hellwig 30345516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 30352ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 3036f46b5a66SChristoph Hellwig 303743d20761SAnand Jain if (!ret) 30380b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 303943d20761SAnand Jain 3040f46b5a66SChristoph Hellwig kfree(vol_args); 3041c9e9f97bSIlya Dryomov out: 3042171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3043f46b5a66SChristoph Hellwig return ret; 3044f46b5a66SChristoph Hellwig } 3045f46b5a66SChristoph Hellwig 30466b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 3047f46b5a66SChristoph Hellwig { 30480b246afaSJeff Mahoney struct inode *inode = file_inode(file); 30490b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 30506b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 3051f46b5a66SChristoph Hellwig int ret; 3052f46b5a66SChristoph Hellwig 3053e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 3054e441d54dSChris Mason return -EPERM; 3055e441d54dSChris Mason 3056da24927bSMiao Xie ret = mnt_want_write_file(file); 3057da24927bSMiao Xie if (ret) 3058da24927bSMiao Xie return ret; 3059c146afadSYan Zheng 3060dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 3061c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 3062c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 3063c47ca32dSDan Carpenter goto err_drop; 3064c9e9f97bSIlya Dryomov } 3065f46b5a66SChristoph Hellwig 30666b526ed7SAnand Jain /* Check for compatibility reject unknown flags */ 3067fd4e994bSOmar Sandoval if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) { 3068fd4e994bSOmar Sandoval ret = -EOPNOTSUPP; 3069fd4e994bSOmar Sandoval goto out; 3070fd4e994bSOmar Sandoval } 3071f46b5a66SChristoph Hellwig 3072171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3073183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3074183860f6SAnand Jain goto out; 3075183860f6SAnand Jain } 3076183860f6SAnand Jain 3077735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 30782ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); 30796b526ed7SAnand Jain } else { 30806b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 30812ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 30826b526ed7SAnand Jain } 3083171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 3084183860f6SAnand Jain 30856b526ed7SAnand Jain if (!ret) { 3086735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 30870b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 30886b526ed7SAnand Jain vol_args->devid); 30896b526ed7SAnand Jain else 30900b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 30916b526ed7SAnand Jain vol_args->name); 30926b526ed7SAnand Jain } 3093183860f6SAnand Jain out: 3094183860f6SAnand Jain kfree(vol_args); 3095c47ca32dSDan Carpenter err_drop: 30964ac20c70SIlya Dryomov mnt_drop_write_file(file); 3097f46b5a66SChristoph Hellwig return ret; 3098f46b5a66SChristoph Hellwig } 3099f46b5a66SChristoph Hellwig 3100f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 3101f46b5a66SChristoph Hellwig { 31020b246afaSJeff Mahoney struct inode *inode = file_inode(file); 31030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3104f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 3105f46b5a66SChristoph Hellwig int ret; 3106f46b5a66SChristoph Hellwig 3107f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3108f46b5a66SChristoph Hellwig return -EPERM; 3109f46b5a66SChristoph Hellwig 3110f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 3111f46b5a66SChristoph Hellwig if (ret) 3112f46b5a66SChristoph Hellwig return ret; 3113f46b5a66SChristoph Hellwig 3114171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 3115f46b5a66SChristoph Hellwig ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 311658d7bbf8SDavid Sterba goto out_drop_write; 311758d7bbf8SDavid Sterba } 311858d7bbf8SDavid Sterba 311958d7bbf8SDavid Sterba vol_args = memdup_user(arg, sizeof(*vol_args)); 312058d7bbf8SDavid Sterba if (IS_ERR(vol_args)) { 312158d7bbf8SDavid Sterba ret = PTR_ERR(vol_args); 3122f46b5a66SChristoph Hellwig goto out; 3123f46b5a66SChristoph Hellwig } 3124f46b5a66SChristoph Hellwig 312558d7bbf8SDavid Sterba vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 31262ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 3127f46b5a66SChristoph Hellwig 3128f46b5a66SChristoph Hellwig if (!ret) 31290b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 3130f46b5a66SChristoph Hellwig kfree(vol_args); 313158d7bbf8SDavid Sterba out: 3132171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 313358d7bbf8SDavid Sterba out_drop_write: 3134f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 313558d7bbf8SDavid Sterba 3136f46b5a66SChristoph Hellwig return ret; 3137f46b5a66SChristoph Hellwig } 3138f46b5a66SChristoph Hellwig 31392ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 31402ff7e61eSJeff Mahoney void __user *arg) 3141475f6387SJan Schmidt { 3142027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 3143475f6387SJan Schmidt struct btrfs_device *device; 31440b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 3145027ed2f0SLi Zefan int ret = 0; 3146475f6387SJan Schmidt 3147027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 3148027ed2f0SLi Zefan if (!fi_args) 3149027ed2f0SLi Zefan return -ENOMEM; 3150027ed2f0SLi Zefan 3151d03262c7SDavid Sterba rcu_read_lock(); 3152027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 3153475f6387SJan Schmidt 3154d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 3155027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 3156027ed2f0SLi Zefan fi_args->max_id = device->devid; 3157475f6387SJan Schmidt } 3158d03262c7SDavid Sterba rcu_read_unlock(); 3159475f6387SJan Schmidt 3160de37aa51SNikolay Borisov memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); 3161bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 3162bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 3163bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 316480a773fbSDavid Sterba 3165027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 3166027ed2f0SLi Zefan ret = -EFAULT; 3167475f6387SJan Schmidt 3168027ed2f0SLi Zefan kfree(fi_args); 3169027ed2f0SLi Zefan return ret; 3170475f6387SJan Schmidt } 3171475f6387SJan Schmidt 31722ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 31732ff7e61eSJeff Mahoney void __user *arg) 3174475f6387SJan Schmidt { 3175475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 3176475f6387SJan Schmidt struct btrfs_device *dev; 3177475f6387SJan Schmidt int ret = 0; 3178475f6387SJan Schmidt char *s_uuid = NULL; 3179475f6387SJan Schmidt 3180475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 3181475f6387SJan Schmidt if (IS_ERR(di_args)) 3182475f6387SJan Schmidt return PTR_ERR(di_args); 3183475f6387SJan Schmidt 3184dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 3185475f6387SJan Schmidt s_uuid = di_args->uuid; 3186475f6387SJan Schmidt 3187c5593ca3SDavid Sterba rcu_read_lock(); 3188e4319cd9SAnand Jain dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid, 318909ba3bc9SAnand Jain NULL, true); 3190475f6387SJan Schmidt 3191475f6387SJan Schmidt if (!dev) { 3192475f6387SJan Schmidt ret = -ENODEV; 3193475f6387SJan Schmidt goto out; 3194475f6387SJan Schmidt } 3195475f6387SJan Schmidt 3196475f6387SJan Schmidt di_args->devid = dev->devid; 31977cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 31987cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 3199475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 3200a27202fbSJim Meyering if (dev->name) { 3201672d5990SMisono Tomohiro strncpy(di_args->path, rcu_str_deref(dev->name), 3202672d5990SMisono Tomohiro sizeof(di_args->path) - 1); 3203a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 3204a27202fbSJim Meyering } else { 320599ba55adSStefan Behrens di_args->path[0] = '\0'; 3206a27202fbSJim Meyering } 3207475f6387SJan Schmidt 3208475f6387SJan Schmidt out: 3209c5593ca3SDavid Sterba rcu_read_unlock(); 3210475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 3211475f6387SJan Schmidt ret = -EFAULT; 3212475f6387SJan Schmidt 3213475f6387SJan Schmidt kfree(di_args); 3214475f6387SJan Schmidt return ret; 3215475f6387SJan Schmidt } 3216475f6387SJan Schmidt 3217d8b55242SFilipe Manana static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 3218d8b55242SFilipe Manana struct inode *inode2, u64 loff2, u64 len) 3219d8b55242SFilipe Manana { 3220d8b55242SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 3221d8b55242SFilipe Manana unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 3222d8b55242SFilipe Manana } 3223d8b55242SFilipe Manana 3224d8b55242SFilipe Manana static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 3225d8b55242SFilipe Manana struct inode *inode2, u64 loff2, u64 len) 3226d8b55242SFilipe Manana { 3227d8b55242SFilipe Manana if (inode1 < inode2) { 3228d8b55242SFilipe Manana swap(inode1, inode2); 3229d8b55242SFilipe Manana swap(loff1, loff2); 3230d8b55242SFilipe Manana } else if (inode1 == inode2 && loff2 < loff1) { 3231d8b55242SFilipe Manana swap(loff1, loff2); 3232d8b55242SFilipe Manana } 3233d8b55242SFilipe Manana lock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 3234d8b55242SFilipe Manana lock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 3235d8b55242SFilipe Manana } 3236d8b55242SFilipe Manana 323757a50e25SFilipe Manana static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 len, 323834a28e3dSFilipe Manana struct inode *dst, u64 dst_loff) 3239416161dbSMark Fasheh { 3240416161dbSMark Fasheh int ret; 32410efa9f48SMark Fasheh 3242de02b9f6SFilipe Manana /* 3243d8b55242SFilipe Manana * Lock destination range to serialize with concurrent readpages() and 3244d8b55242SFilipe Manana * source range to serialize with relocation. 3245de02b9f6SFilipe Manana */ 3246d8b55242SFilipe Manana btrfs_double_extent_lock(src, loff, dst, dst_loff, len); 324757a50e25SFilipe Manana ret = btrfs_clone(src, dst, loff, len, len, dst_loff, 1); 3248d8b55242SFilipe Manana btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 32493973909dSTimofey Titovets 32503973909dSTimofey Titovets return ret; 32513973909dSTimofey Titovets } 32523973909dSTimofey Titovets 3253b6728768STimofey Titovets #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3254b6728768STimofey Titovets 32553973909dSTimofey Titovets static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 32563973909dSTimofey Titovets struct inode *dst, u64 dst_loff) 32573973909dSTimofey Titovets { 32583973909dSTimofey Titovets int ret; 3259b6728768STimofey Titovets u64 i, tail_len, chunk_count; 326062d54f3aSFilipe Manana struct btrfs_root *root_dst = BTRFS_I(dst)->root; 326162d54f3aSFilipe Manana 326262d54f3aSFilipe Manana spin_lock(&root_dst->root_item_lock); 326362d54f3aSFilipe Manana if (root_dst->send_in_progress) { 326462d54f3aSFilipe Manana btrfs_warn_rl(root_dst->fs_info, 326562d54f3aSFilipe Manana "cannot deduplicate to root %llu while send operations are using it (%d in progress)", 326662d54f3aSFilipe Manana root_dst->root_key.objectid, 326762d54f3aSFilipe Manana root_dst->send_in_progress); 326862d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 326962d54f3aSFilipe Manana return -EAGAIN; 327062d54f3aSFilipe Manana } 327162d54f3aSFilipe Manana root_dst->dedupe_in_progress++; 327262d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 32733973909dSTimofey Titovets 3274b6728768STimofey Titovets tail_len = olen % BTRFS_MAX_DEDUPE_LEN; 3275b6728768STimofey Titovets chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); 327667b07bd4STimofey Titovets 3277b6728768STimofey Titovets for (i = 0; i < chunk_count; i++) { 3278b6728768STimofey Titovets ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, 327934a28e3dSFilipe Manana dst, dst_loff); 3280b6728768STimofey Titovets if (ret) 328162d54f3aSFilipe Manana goto out; 3282b6728768STimofey Titovets 3283b6728768STimofey Titovets loff += BTRFS_MAX_DEDUPE_LEN; 3284b6728768STimofey Titovets dst_loff += BTRFS_MAX_DEDUPE_LEN; 3285b6728768STimofey Titovets } 3286b6728768STimofey Titovets 3287b6728768STimofey Titovets if (tail_len > 0) 328867b07bd4STimofey Titovets ret = btrfs_extent_same_range(src, loff, tail_len, dst, 328934a28e3dSFilipe Manana dst_loff); 329062d54f3aSFilipe Manana out: 329162d54f3aSFilipe Manana spin_lock(&root_dst->root_item_lock); 329262d54f3aSFilipe Manana root_dst->dedupe_in_progress--; 329362d54f3aSFilipe Manana spin_unlock(&root_dst->root_item_lock); 3294416161dbSMark Fasheh 3295416161dbSMark Fasheh return ret; 3296416161dbSMark Fasheh } 3297416161dbSMark Fasheh 3298f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3299f82a9901SFilipe Manana struct inode *inode, 3300f82a9901SFilipe Manana u64 endoff, 3301f82a9901SFilipe Manana const u64 destoff, 33021c919a5eSMark Fasheh const u64 olen, 33031c919a5eSMark Fasheh int no_time_update) 3304f82a9901SFilipe Manana { 3305f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3306f82a9901SFilipe Manana int ret; 3307f82a9901SFilipe Manana 3308f82a9901SFilipe Manana inode_inc_iversion(inode); 33091c919a5eSMark Fasheh if (!no_time_update) 3310c2050a45SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 3311f82a9901SFilipe Manana /* 3312f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3313f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3314f82a9901SFilipe Manana */ 3315f82a9901SFilipe Manana if (endoff > destoff + olen) 3316f82a9901SFilipe Manana endoff = destoff + olen; 3317f82a9901SFilipe Manana if (endoff > inode->i_size) 33186ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(inode), endoff); 3319f82a9901SFilipe Manana 3320f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3321f82a9901SFilipe Manana if (ret) { 332266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 33233a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3324f82a9901SFilipe Manana goto out; 3325f82a9901SFilipe Manana } 33263a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 3327f82a9901SFilipe Manana out: 3328f82a9901SFilipe Manana return ret; 3329f82a9901SFilipe Manana } 3330f82a9901SFilipe Manana 33318039d87dSFilipe Manana /* 33328039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 33338039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 33348039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 33358039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 33368039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 33378039d87dSFilipe Manana * 33388039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 33398039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 33408039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 33418039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 33428039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 33438039d87dSFilipe Manana * i_mutex, which we are holding here). 33448039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 33458039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 33468039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 33478039d87dSFilipe Manana * 33488039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 33498039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 33508039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 33518039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 33528039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 33538039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 33548039d87dSFilipe Manana * the former. 33558039d87dSFilipe Manana */ 33564a0ab9d7SDavid Sterba static int clone_copy_inline_extent(struct inode *dst, 33578039d87dSFilipe Manana struct btrfs_trans_handle *trans, 33588039d87dSFilipe Manana struct btrfs_path *path, 33598039d87dSFilipe Manana struct btrfs_key *new_key, 33608039d87dSFilipe Manana const u64 drop_start, 33618039d87dSFilipe Manana const u64 datal, 33628039d87dSFilipe Manana const u64 skip, 33638039d87dSFilipe Manana const u64 size, 33648039d87dSFilipe Manana char *inline_data) 33658039d87dSFilipe Manana { 33660b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); 33678039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 33688039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 33690b246afaSJeff Mahoney fs_info->sectorsize); 33708039d87dSFilipe Manana int ret; 33718039d87dSFilipe Manana struct btrfs_key key; 33728039d87dSFilipe Manana 33738039d87dSFilipe Manana if (new_key->offset > 0) 33748039d87dSFilipe Manana return -EOPNOTSUPP; 33758039d87dSFilipe Manana 33764a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(dst)); 33778039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 33788039d87dSFilipe Manana key.offset = 0; 33798039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 33808039d87dSFilipe Manana if (ret < 0) { 33818039d87dSFilipe Manana return ret; 33828039d87dSFilipe Manana } else if (ret > 0) { 33838039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 33848039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 33858039d87dSFilipe Manana if (ret < 0) 33868039d87dSFilipe Manana return ret; 33878039d87dSFilipe Manana else if (ret > 0) 33888039d87dSFilipe Manana goto copy_inline_extent; 33898039d87dSFilipe Manana } 33908039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 33914a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 33928039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 33938039d87dSFilipe Manana ASSERT(key.offset > 0); 33948039d87dSFilipe Manana return -EOPNOTSUPP; 33958039d87dSFilipe Manana } 33968039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 33978039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 33988039d87dSFilipe Manana u64 ext_len; 33998039d87dSFilipe Manana 34008039d87dSFilipe Manana /* 34018039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 34028039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 34038039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 34048039d87dSFilipe Manana */ 34058039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 34068039d87dSFilipe Manana struct btrfs_file_extent_item); 34078039d87dSFilipe Manana /* 34088039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 34098039d87dSFilipe Manana * following it. 34108039d87dSFilipe Manana */ 34118039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 34128039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 34138039d87dSFilipe Manana goto copy_inline_extent; 34148039d87dSFilipe Manana 34158039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 34168039d87dSFilipe Manana if (ext_len > aligned_end) 34178039d87dSFilipe Manana return -EOPNOTSUPP; 34188039d87dSFilipe Manana 34198039d87dSFilipe Manana ret = btrfs_next_item(root, path); 34208039d87dSFilipe Manana if (ret < 0) { 34218039d87dSFilipe Manana return ret; 34228039d87dSFilipe Manana } else if (ret == 0) { 34238039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 34248039d87dSFilipe Manana path->slots[0]); 34254a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 34268039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 34278039d87dSFilipe Manana return -EOPNOTSUPP; 34288039d87dSFilipe Manana } 34298039d87dSFilipe Manana } 34308039d87dSFilipe Manana 34318039d87dSFilipe Manana copy_inline_extent: 34328039d87dSFilipe Manana /* 34338039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 34348039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 34358039d87dSFilipe Manana */ 34368039d87dSFilipe Manana if (i_size_read(dst) > datal) { 34378039d87dSFilipe Manana /* 34388039d87dSFilipe Manana * If the destination inode has an inline extent... 34398039d87dSFilipe Manana * This would require copying the data from the source inline 34408039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 34418039d87dSFilipe Manana * But this is really complex, both extents can be compressed 34428039d87dSFilipe Manana * or just one of them, which would require decompressing and 34438039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 34448039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 34458039d87dSFilipe Manana * inline extent). 34468039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 34478039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 34488039d87dSFilipe Manana */ 34498039d87dSFilipe Manana return -EOPNOTSUPP; 34508039d87dSFilipe Manana } 34518039d87dSFilipe Manana 34528039d87dSFilipe Manana btrfs_release_path(path); 34538039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 34548039d87dSFilipe Manana if (ret) 34558039d87dSFilipe Manana return ret; 34568039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 34578039d87dSFilipe Manana if (ret) 34588039d87dSFilipe Manana return ret; 34598039d87dSFilipe Manana 34608039d87dSFilipe Manana if (skip) { 34618039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 34628039d87dSFilipe Manana 34638039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 34648039d87dSFilipe Manana } 34658039d87dSFilipe Manana 34668039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 34678039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 34688039d87dSFilipe Manana path->slots[0]), 34698039d87dSFilipe Manana size); 34708039d87dSFilipe Manana inode_add_bytes(dst, datal); 3471690a5dbfSFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(dst)->runtime_flags); 34728039d87dSFilipe Manana 34738039d87dSFilipe Manana return 0; 34748039d87dSFilipe Manana } 34758039d87dSFilipe Manana 347632b7c687SMark Fasheh /** 347732b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 347832b7c687SMark Fasheh * 347932b7c687SMark Fasheh * @src: Inode to clone from 348032b7c687SMark Fasheh * @inode: Inode to clone to 348132b7c687SMark Fasheh * @off: Offset within source to start clone from 348232b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 34831c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 348432b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 34851c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 348632b7c687SMark Fasheh */ 348732b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3488f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 34891c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3490f46b5a66SChristoph Hellwig { 34910b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3492f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 349332b7c687SMark Fasheh struct btrfs_path *path = NULL; 3494f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 349532b7c687SMark Fasheh struct btrfs_trans_handle *trans; 349632b7c687SMark Fasheh char *buf = NULL; 3497ae01a0abSYan Zheng struct btrfs_key key; 3498f46b5a66SChristoph Hellwig u32 nritems; 3499f46b5a66SChristoph Hellwig int slot; 3500ae01a0abSYan Zheng int ret; 3501f82a9901SFilipe Manana const u64 len = olen_aligned; 3502f82a9901SFilipe Manana u64 last_dest_end = destoff; 3503ae01a0abSYan Zheng 3504ae01a0abSYan Zheng ret = -ENOMEM; 3505752ade68SMichal Hocko buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 3506ae01a0abSYan Zheng if (!buf) 350732b7c687SMark Fasheh return ret; 3508ae01a0abSYan Zheng 3509ae01a0abSYan Zheng path = btrfs_alloc_path(); 3510ae01a0abSYan Zheng if (!path) { 351115351955SDavid Sterba kvfree(buf); 351232b7c687SMark Fasheh return ret; 3513ae01a0abSYan Zheng } 351432b7c687SMark Fasheh 3515e4058b54SDavid Sterba path->reada = READA_FORWARD; 3516c5c9cd4dSSage Weil /* clone data */ 35174a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(src)); 3518ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 35192c463823SFilipe Manana key.offset = off; 3520f46b5a66SChristoph Hellwig 3521f46b5a66SChristoph Hellwig while (1) { 3522de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3523b64119b5SFilipe Manana struct btrfs_file_extent_item *extent; 3524b64119b5SFilipe Manana int type; 3525b64119b5SFilipe Manana u32 size; 3526b64119b5SFilipe Manana struct btrfs_key new_key; 3527b64119b5SFilipe Manana u64 disko = 0, diskl = 0; 3528b64119b5SFilipe Manana u64 datao = 0, datal = 0; 3529b64119b5SFilipe Manana u8 comp; 3530b64119b5SFilipe Manana u64 drop_start; 3531df858e76SFilipe Manana 3532f46b5a66SChristoph Hellwig /* 3533f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3534f46b5a66SChristoph Hellwig * tree. 3535f46b5a66SChristoph Hellwig */ 3536e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3537362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3538362a20c5SDavid Sterba 0, 0); 3539f46b5a66SChristoph Hellwig if (ret < 0) 3540f46b5a66SChristoph Hellwig goto out; 35412c463823SFilipe Manana /* 35422c463823SFilipe Manana * First search, if no extent item that starts at offset off was 35432c463823SFilipe Manana * found but the previous item is an extent item, it's possible 35442c463823SFilipe Manana * it might overlap our target range, therefore process it. 35452c463823SFilipe Manana */ 35462c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 35472c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 35482c463823SFilipe Manana path->slots[0] - 1); 35492c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 35502c463823SFilipe Manana path->slots[0]--; 35512c463823SFilipe Manana } 3552f46b5a66SChristoph Hellwig 3553ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3554e4355f34SFilipe David Borba Manana process_slot: 3555ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3556362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3557f46b5a66SChristoph Hellwig if (ret < 0) 3558f46b5a66SChristoph Hellwig goto out; 3559f46b5a66SChristoph Hellwig if (ret > 0) 3560f46b5a66SChristoph Hellwig break; 3561ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3562f46b5a66SChristoph Hellwig } 3563f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3564f46b5a66SChristoph Hellwig slot = path->slots[0]; 3565f46b5a66SChristoph Hellwig 3566ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3567962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 35684a0cc7caSNikolay Borisov key.objectid != btrfs_ino(BTRFS_I(src))) 3569f46b5a66SChristoph Hellwig break; 3570f46b5a66SChristoph Hellwig 3571b64119b5SFilipe Manana ASSERT(key.type == BTRFS_EXTENT_DATA_KEY); 357231840ae1SZheng Yan 3573c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3574c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3575c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3576c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3577c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3578c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3579b64119b5SFilipe Manana disko = btrfs_file_extent_disk_bytenr(leaf, extent); 3580b64119b5SFilipe Manana diskl = btrfs_file_extent_disk_num_bytes(leaf, extent); 3581c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3582b64119b5SFilipe Manana datal = btrfs_file_extent_num_bytes(leaf, extent); 3583c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3584b64119b5SFilipe Manana /* Take upper bound, may be compressed */ 3585b64119b5SFilipe Manana datal = btrfs_file_extent_ram_bytes(leaf, extent); 3586c5c9cd4dSSage Weil } 358731840ae1SZheng Yan 35882c463823SFilipe Manana /* 3589b64119b5SFilipe Manana * The first search might have left us at an extent item that 3590b64119b5SFilipe Manana * ends before our target range's start, can happen if we have 3591b64119b5SFilipe Manana * holes and NO_HOLES feature enabled. 35922c463823SFilipe Manana */ 35932c463823SFilipe Manana if (key.offset + datal <= off) { 3594e4355f34SFilipe David Borba Manana path->slots[0]++; 3595e4355f34SFilipe David Borba Manana goto process_slot; 35962c463823SFilipe Manana } else if (key.offset >= off + len) { 35972c463823SFilipe Manana break; 3598e4355f34SFilipe David Borba Manana } 3599df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 3600e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3601b64119b5SFilipe Manana read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot), 3602e4355f34SFilipe David Borba Manana size); 3603e4355f34SFilipe David Borba Manana 3604e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3605e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3606c5c9cd4dSSage Weil 360731840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 36084a0cc7caSNikolay Borisov new_key.objectid = btrfs_ino(BTRFS_I(inode)); 36094d728ec7SLi Zefan if (off <= key.offset) 3610c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 36114d728ec7SLi Zefan else 36124d728ec7SLi Zefan new_key.offset = destoff; 3613c5c9cd4dSSage Weil 3614b6f3409bSSage Weil /* 3615b64119b5SFilipe Manana * Deal with a hole that doesn't have an extent item that 3616b64119b5SFilipe Manana * represents it (NO_HOLES feature enabled). 3617b64119b5SFilipe Manana * This hole is either in the middle of the cloning range or at 3618b64119b5SFilipe Manana * the beginning (fully overlaps it or partially overlaps it). 3619f82a9901SFilipe Manana */ 3620f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3621f82a9901SFilipe Manana drop_start = last_dest_end; 3622f82a9901SFilipe Manana else 3623f82a9901SFilipe Manana drop_start = new_key.offset; 3624f82a9901SFilipe Manana 3625c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3626c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3627690a5dbfSFilipe Manana struct btrfs_clone_extent_info clone_info; 3628690a5dbfSFilipe Manana 3629d72c0842SLi Zefan /* 3630d72c0842SLi Zefan * a | --- range to clone ---| b 3631d72c0842SLi Zefan * | ------------- extent ------------- | 3632d72c0842SLi Zefan */ 3633d72c0842SLi Zefan 3634b64119b5SFilipe Manana /* Subtract range b */ 3635d72c0842SLi Zefan if (key.offset + datal > off + len) 3636d72c0842SLi Zefan datal = off + len - key.offset; 3637d72c0842SLi Zefan 3638b64119b5SFilipe Manana /* Subtract range a */ 3639a22285a6SYan, Zheng if (off > key.offset) { 3640a22285a6SYan, Zheng datao += off - key.offset; 3641a22285a6SYan, Zheng datal -= off - key.offset; 3642a22285a6SYan, Zheng } 3643a22285a6SYan, Zheng 3644690a5dbfSFilipe Manana clone_info.disk_offset = disko; 3645690a5dbfSFilipe Manana clone_info.disk_len = diskl; 3646690a5dbfSFilipe Manana clone_info.data_offset = datao; 3647690a5dbfSFilipe Manana clone_info.data_len = datal; 3648690a5dbfSFilipe Manana clone_info.file_offset = new_key.offset; 3649690a5dbfSFilipe Manana clone_info.extent_buf = buf; 3650690a5dbfSFilipe Manana clone_info.item_size = size; 3651690a5dbfSFilipe Manana ret = btrfs_punch_hole_range(inode, path, 3652f82a9901SFilipe Manana drop_start, 3653690a5dbfSFilipe Manana new_key.offset + datal - 1, 3654690a5dbfSFilipe Manana &clone_info, &trans); 3655690a5dbfSFilipe Manana if (ret) 365679787eaaSJeff Mahoney goto out; 3657c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3658c5c9cd4dSSage Weil u64 skip = 0; 3659c5c9cd4dSSage Weil u64 trim = 0; 3660ed958762SFilipe Manana 3661c5c9cd4dSSage Weil if (off > key.offset) { 3662c5c9cd4dSSage Weil skip = off - key.offset; 3663c5c9cd4dSSage Weil new_key.offset += skip; 366431840ae1SZheng Yan } 3665d397712bSChris Mason 3666c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3667c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3668d397712bSChris Mason 3669c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3670c5c9cd4dSSage Weil ret = -EINVAL; 3671c5c9cd4dSSage Weil goto out; 367231840ae1SZheng Yan } 3673c5c9cd4dSSage Weil size -= skip + trim; 3674c5c9cd4dSSage Weil datal -= skip + trim; 3675a22285a6SYan, Zheng 3676690a5dbfSFilipe Manana /* 3677b64119b5SFilipe Manana * If our extent is inline, we know we will drop or 3678b64119b5SFilipe Manana * adjust at most 1 extent item in the destination root. 3679690a5dbfSFilipe Manana * 3680b64119b5SFilipe Manana * 1 - adjusting old extent (we may have to split it) 3681690a5dbfSFilipe Manana * 1 - add new extent 3682690a5dbfSFilipe Manana * 1 - inode update 3683690a5dbfSFilipe Manana */ 3684690a5dbfSFilipe Manana trans = btrfs_start_transaction(root, 3); 3685690a5dbfSFilipe Manana if (IS_ERR(trans)) { 3686690a5dbfSFilipe Manana ret = PTR_ERR(trans); 3687690a5dbfSFilipe Manana goto out; 3688690a5dbfSFilipe Manana } 3689690a5dbfSFilipe Manana 3690b64119b5SFilipe Manana ret = clone_copy_inline_extent(inode, trans, path, 3691b64119b5SFilipe Manana &new_key, drop_start, 3692b64119b5SFilipe Manana datal, skip, size, buf); 369379787eaaSJeff Mahoney if (ret) { 36943f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 3695b64119b5SFilipe Manana btrfs_abort_transaction(trans, ret); 36963a45bb20SJeff Mahoney btrfs_end_transaction(trans); 369779787eaaSJeff Mahoney goto out; 369879787eaaSJeff Mahoney } 3699c5c9cd4dSSage Weil } 3700c5c9cd4dSSage Weil 3701b3b4aa74SDavid Sterba btrfs_release_path(path); 3702c5c9cd4dSSage Weil 370362e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 37040b246afaSJeff Mahoney fs_info->sectorsize); 3705b64119b5SFilipe Manana ret = clone_finish_inode_update(trans, inode, last_dest_end, 3706b64119b5SFilipe Manana destoff, olen, no_time_update); 3707f82a9901SFilipe Manana if (ret) 370879787eaaSJeff Mahoney goto out; 37092c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 37102c463823SFilipe Manana break; 3711b64119b5SFilipe Manana 3712b3b4aa74SDavid Sterba btrfs_release_path(path); 3713df858e76SFilipe Manana key.offset = next_key_min_offset; 371469ae5e44SWang Xiaoguang 371569ae5e44SWang Xiaoguang if (fatal_signal_pending(current)) { 371669ae5e44SWang Xiaoguang ret = -EINTR; 371769ae5e44SWang Xiaoguang goto out; 371869ae5e44SWang Xiaoguang } 3719ae01a0abSYan Zheng } 3720f46b5a66SChristoph Hellwig ret = 0; 372132b7c687SMark Fasheh 3722f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3723690a5dbfSFilipe Manana struct btrfs_clone_extent_info clone_info = { 0 }; 3724f82a9901SFilipe Manana /* 3725f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3726f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3727f82a9901SFilipe Manana */ 3728b3b4aa74SDavid Sterba btrfs_release_path(path); 3729690a5dbfSFilipe Manana path->leave_spinning = 0; 3730f82a9901SFilipe Manana 3731f82a9901SFilipe Manana /* 3732690a5dbfSFilipe Manana * We are dealing with a hole and our clone_info already has a 3733690a5dbfSFilipe Manana * disk_offset of 0, we only need to fill the data length and 3734690a5dbfSFilipe Manana * file offset. 3735f82a9901SFilipe Manana */ 3736690a5dbfSFilipe Manana clone_info.data_len = destoff + len - last_dest_end; 3737690a5dbfSFilipe Manana clone_info.file_offset = last_dest_end; 3738690a5dbfSFilipe Manana ret = btrfs_punch_hole_range(inode, path, 3739690a5dbfSFilipe Manana last_dest_end, destoff + len - 1, 3740690a5dbfSFilipe Manana &clone_info, &trans); 3741690a5dbfSFilipe Manana if (ret) 3742f82a9901SFilipe Manana goto out; 3743690a5dbfSFilipe Manana 3744f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 37451c919a5eSMark Fasheh destoff, olen, no_time_update); 3746f82a9901SFilipe Manana } 3747f82a9901SFilipe Manana 3748f46b5a66SChristoph Hellwig out: 374932b7c687SMark Fasheh btrfs_free_path(path); 375015351955SDavid Sterba kvfree(buf); 375132b7c687SMark Fasheh return ret; 375232b7c687SMark Fasheh } 375332b7c687SMark Fasheh 37543db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 375532b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 375632b7c687SMark Fasheh { 375754563d41SAl Viro struct inode *inode = file_inode(file); 37583db11b2eSZach Brown struct inode *src = file_inode(file_src); 37590b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 376032b7c687SMark Fasheh int ret; 376132b7c687SMark Fasheh u64 len = olen; 37620b246afaSJeff Mahoney u64 bs = fs_info->sb->s_blocksize; 376332b7c687SMark Fasheh 376432b7c687SMark Fasheh /* 376532b7c687SMark Fasheh * TODO: 376632b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 376732b7c687SMark Fasheh * decompress into destination's address_space (the file offset 376832b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 376932b7c687SMark Fasheh * otherwise reinsert) a subrange. 377000fdf13aSLiu Bo * 377100fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 377200fdf13aSLiu Bo * be either compressed or non-compressed. 377332b7c687SMark Fasheh */ 377432b7c687SMark Fasheh 3775ac765f83SFilipe Manana /* 377634a28e3dSFilipe Manana * VFS's generic_remap_file_range_prep() protects us from cloning the 377734a28e3dSFilipe Manana * eof block into the middle of a file, which would result in corruption 377834a28e3dSFilipe Manana * if the file size is not blocksize aligned. So we don't need to check 377934a28e3dSFilipe Manana * for that case here. 3780ac765f83SFilipe Manana */ 378134a28e3dSFilipe Manana if (off + len == src->i_size) 378232b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 378332b7c687SMark Fasheh 378432b7c687SMark Fasheh if (destoff > inode->i_size) { 3785f7fa1107SFilipe Manana const u64 wb_start = ALIGN_DOWN(inode->i_size, bs); 3786f7fa1107SFilipe Manana 378732b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 378832b7c687SMark Fasheh if (ret) 378934a28e3dSFilipe Manana return ret; 3790f7fa1107SFilipe Manana /* 3791f7fa1107SFilipe Manana * We may have truncated the last block if the inode's size is 3792f7fa1107SFilipe Manana * not sector size aligned, so we need to wait for writeback to 3793f7fa1107SFilipe Manana * complete before proceeding further, otherwise we can race 3794f7fa1107SFilipe Manana * with cloning and attempt to increment a reference to an 3795f7fa1107SFilipe Manana * extent that no longer exists (writeback completed right after 3796f7fa1107SFilipe Manana * we found the previous extent covering eof and before we 3797f7fa1107SFilipe Manana * attempted to increment its reference count). 3798f7fa1107SFilipe Manana */ 3799f7fa1107SFilipe Manana ret = btrfs_wait_ordered_range(inode, wb_start, 3800f7fa1107SFilipe Manana destoff - wb_start); 3801f7fa1107SFilipe Manana if (ret) 3802f7fa1107SFilipe Manana return ret; 380332b7c687SMark Fasheh } 380432b7c687SMark Fasheh 3805c125b8bfSFilipe Manana /* 3806d8b55242SFilipe Manana * Lock destination range to serialize with concurrent readpages() and 3807d8b55242SFilipe Manana * source range to serialize with relocation. 3808c125b8bfSFilipe Manana */ 3809d8b55242SFilipe Manana btrfs_double_extent_lock(src, off, inode, destoff, len); 38101c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 3811d8b55242SFilipe Manana btrfs_double_extent_unlock(src, off, inode, destoff, len); 3812c125b8bfSFilipe Manana /* 3813c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3814c125b8bfSFilipe Manana * data immediately and not the previous data. 3815c125b8bfSFilipe Manana */ 381665bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 381709cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 381809cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 381934a28e3dSFilipe Manana 382034a28e3dSFilipe Manana return ret; 382134a28e3dSFilipe Manana } 382234a28e3dSFilipe Manana 382334a28e3dSFilipe Manana static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, 382434a28e3dSFilipe Manana struct file *file_out, loff_t pos_out, 382534a28e3dSFilipe Manana loff_t *len, unsigned int remap_flags) 382634a28e3dSFilipe Manana { 382734a28e3dSFilipe Manana struct inode *inode_in = file_inode(file_in); 382834a28e3dSFilipe Manana struct inode *inode_out = file_inode(file_out); 382934a28e3dSFilipe Manana u64 bs = BTRFS_I(inode_out)->root->fs_info->sb->s_blocksize; 383034a28e3dSFilipe Manana bool same_inode = inode_out == inode_in; 383134a28e3dSFilipe Manana u64 wb_len; 383234a28e3dSFilipe Manana int ret; 383334a28e3dSFilipe Manana 383434a28e3dSFilipe Manana if (!(remap_flags & REMAP_FILE_DEDUP)) { 383534a28e3dSFilipe Manana struct btrfs_root *root_out = BTRFS_I(inode_out)->root; 383634a28e3dSFilipe Manana 383734a28e3dSFilipe Manana if (btrfs_root_readonly(root_out)) 383834a28e3dSFilipe Manana return -EROFS; 383934a28e3dSFilipe Manana 384034a28e3dSFilipe Manana if (file_in->f_path.mnt != file_out->f_path.mnt || 384134a28e3dSFilipe Manana inode_in->i_sb != inode_out->i_sb) 384234a28e3dSFilipe Manana return -EXDEV; 384334a28e3dSFilipe Manana } 384434a28e3dSFilipe Manana 3845500710d3SFilipe Manana /* don't make the dst file partly checksummed */ 3846500710d3SFilipe Manana if ((BTRFS_I(inode_in)->flags & BTRFS_INODE_NODATASUM) != 3847500710d3SFilipe Manana (BTRFS_I(inode_out)->flags & BTRFS_INODE_NODATASUM)) { 38487984ae52SGoldwyn Rodrigues return -EINVAL; 3849500710d3SFilipe Manana } 3850500710d3SFilipe Manana 385134a28e3dSFilipe Manana /* 385234a28e3dSFilipe Manana * Now that the inodes are locked, we need to start writeback ourselves 385334a28e3dSFilipe Manana * and can not rely on the writeback from the VFS's generic helper 385434a28e3dSFilipe Manana * generic_remap_file_range_prep() because: 385534a28e3dSFilipe Manana * 385634a28e3dSFilipe Manana * 1) For compression we must call filemap_fdatawrite_range() range 385734a28e3dSFilipe Manana * twice (btrfs_fdatawrite_range() does it for us), and the generic 385834a28e3dSFilipe Manana * helper only calls it once; 385934a28e3dSFilipe Manana * 386034a28e3dSFilipe Manana * 2) filemap_fdatawrite_range(), called by the generic helper only 386134a28e3dSFilipe Manana * waits for the writeback to complete, i.e. for IO to be done, and 386234a28e3dSFilipe Manana * not for the ordered extents to complete. We need to wait for them 386334a28e3dSFilipe Manana * to complete so that new file extent items are in the fs tree. 386434a28e3dSFilipe Manana */ 386534a28e3dSFilipe Manana if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) 386634a28e3dSFilipe Manana wb_len = ALIGN(inode_in->i_size, bs) - ALIGN_DOWN(pos_in, bs); 386734a28e3dSFilipe Manana else 386834a28e3dSFilipe Manana wb_len = ALIGN(*len, bs); 386934a28e3dSFilipe Manana 387034a28e3dSFilipe Manana /* 387134a28e3dSFilipe Manana * Since we don't lock ranges, wait for ongoing lockless dio writes (as 387234a28e3dSFilipe Manana * any in progress could create its ordered extents after we wait for 387334a28e3dSFilipe Manana * existing ordered extents below). 387434a28e3dSFilipe Manana */ 387534a28e3dSFilipe Manana inode_dio_wait(inode_in); 387634a28e3dSFilipe Manana if (!same_inode) 387734a28e3dSFilipe Manana inode_dio_wait(inode_out); 387834a28e3dSFilipe Manana 3879a94d1d0cSQu Wenruo /* 3880a94d1d0cSQu Wenruo * Workaround to make sure NOCOW buffered write reach disk as NOCOW. 3881a94d1d0cSQu Wenruo * 3882a94d1d0cSQu Wenruo * Btrfs' back references do not have a block level granularity, they 3883a94d1d0cSQu Wenruo * work at the whole extent level. 3884a94d1d0cSQu Wenruo * NOCOW buffered write without data space reserved may not be able 3885a94d1d0cSQu Wenruo * to fall back to CoW due to lack of data space, thus could cause 3886a94d1d0cSQu Wenruo * data loss. 3887a94d1d0cSQu Wenruo * 3888a94d1d0cSQu Wenruo * Here we take a shortcut by flushing the whole inode, so that all 3889a94d1d0cSQu Wenruo * nocow write should reach disk as nocow before we increase the 3890a94d1d0cSQu Wenruo * reference of the extent. We could do better by only flushing NOCOW 3891a94d1d0cSQu Wenruo * data, but that needs extra accounting. 3892a94d1d0cSQu Wenruo * 3893a94d1d0cSQu Wenruo * Also we don't need to check ASYNC_EXTENT, as async extent will be 3894a94d1d0cSQu Wenruo * CoWed anyway, not affecting nocow part. 3895a94d1d0cSQu Wenruo */ 3896a94d1d0cSQu Wenruo ret = filemap_flush(inode_in->i_mapping); 3897a94d1d0cSQu Wenruo if (ret < 0) 3898a94d1d0cSQu Wenruo return ret; 3899a94d1d0cSQu Wenruo 390034a28e3dSFilipe Manana ret = btrfs_wait_ordered_range(inode_in, ALIGN_DOWN(pos_in, bs), 390134a28e3dSFilipe Manana wb_len); 390234a28e3dSFilipe Manana if (ret < 0) 39037984ae52SGoldwyn Rodrigues return ret; 390434a28e3dSFilipe Manana ret = btrfs_wait_ordered_range(inode_out, ALIGN_DOWN(pos_out, bs), 390534a28e3dSFilipe Manana wb_len); 390634a28e3dSFilipe Manana if (ret < 0) 39073db11b2eSZach Brown return ret; 39087984ae52SGoldwyn Rodrigues 39097984ae52SGoldwyn Rodrigues return generic_remap_file_range_prep(file_in, pos_in, file_out, pos_out, 39107984ae52SGoldwyn Rodrigues len, remap_flags); 39113db11b2eSZach Brown } 39123db11b2eSZach Brown 391342ec3d4cSDarrick J. Wong loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, 391442ec3d4cSDarrick J. Wong struct file *dst_file, loff_t destoff, loff_t len, 39152e5dfc99SDarrick J. Wong unsigned int remap_flags) 39163db11b2eSZach Brown { 391734a28e3dSFilipe Manana struct inode *src_inode = file_inode(src_file); 391834a28e3dSFilipe Manana struct inode *dst_inode = file_inode(dst_file); 391934a28e3dSFilipe Manana bool same_inode = dst_inode == src_inode; 392042ec3d4cSDarrick J. Wong int ret; 392142ec3d4cSDarrick J. Wong 39222e5dfc99SDarrick J. Wong if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) 39232e5dfc99SDarrick J. Wong return -EINVAL; 39242e5dfc99SDarrick J. Wong 39257984ae52SGoldwyn Rodrigues if (same_inode) 39267984ae52SGoldwyn Rodrigues inode_lock(src_inode); 39277984ae52SGoldwyn Rodrigues else 39287984ae52SGoldwyn Rodrigues lock_two_nondirectories(src_inode, dst_inode); 39297984ae52SGoldwyn Rodrigues 393034a28e3dSFilipe Manana ret = btrfs_remap_file_range_prep(src_file, off, dst_file, destoff, 393134a28e3dSFilipe Manana &len, remap_flags); 393234a28e3dSFilipe Manana if (ret < 0 || len == 0) 39337984ae52SGoldwyn Rodrigues goto out_unlock; 39342e5dfc99SDarrick J. Wong 393534a28e3dSFilipe Manana if (remap_flags & REMAP_FILE_DEDUP) 393634a28e3dSFilipe Manana ret = btrfs_extent_same(src_inode, off, len, dst_inode, destoff); 393734a28e3dSFilipe Manana else 393842ec3d4cSDarrick J. Wong ret = btrfs_clone_files(dst_file, src_file, off, len, destoff); 393934a28e3dSFilipe Manana 39407984ae52SGoldwyn Rodrigues out_unlock: 394134a28e3dSFilipe Manana if (same_inode) 394234a28e3dSFilipe Manana inode_unlock(src_inode); 394334a28e3dSFilipe Manana else 39444ea748e1SFilipe Manana unlock_two_nondirectories(src_inode, dst_inode); 394534a28e3dSFilipe Manana 394642ec3d4cSDarrick J. Wong return ret < 0 ? ret : len; 3947c5c9cd4dSSage Weil } 3948c5c9cd4dSSage Weil 39496ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 39506ef5ed0dSJosef Bacik { 3951496ad9aaSAl Viro struct inode *inode = file_inode(file); 39520b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 39536ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 39546ef5ed0dSJosef Bacik struct btrfs_root *new_root; 39556ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 39566ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 39576ef5ed0dSJosef Bacik struct btrfs_path *path; 39586ef5ed0dSJosef Bacik struct btrfs_key location; 39596ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 39606ef5ed0dSJosef Bacik u64 objectid = 0; 39616ef5ed0dSJosef Bacik u64 dir_id; 39623c04ce01SMiao Xie int ret; 39636ef5ed0dSJosef Bacik 39646ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 39656ef5ed0dSJosef Bacik return -EPERM; 39666ef5ed0dSJosef Bacik 39673c04ce01SMiao Xie ret = mnt_want_write_file(file); 39683c04ce01SMiao Xie if (ret) 39693c04ce01SMiao Xie return ret; 39703c04ce01SMiao Xie 39713c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 39723c04ce01SMiao Xie ret = -EFAULT; 39733c04ce01SMiao Xie goto out; 39743c04ce01SMiao Xie } 39756ef5ed0dSJosef Bacik 39766ef5ed0dSJosef Bacik if (!objectid) 39771cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 39786ef5ed0dSJosef Bacik 39796ef5ed0dSJosef Bacik location.objectid = objectid; 39806ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 39816ef5ed0dSJosef Bacik location.offset = (u64)-1; 39826ef5ed0dSJosef Bacik 39830b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &location); 39843c04ce01SMiao Xie if (IS_ERR(new_root)) { 39853c04ce01SMiao Xie ret = PTR_ERR(new_root); 39863c04ce01SMiao Xie goto out; 39873c04ce01SMiao Xie } 39884fd786e6SMisono Tomohiro if (!is_fstree(new_root->root_key.objectid)) { 39896d6d2829Ssatoru takeuchi ret = -ENOENT; 39906d6d2829Ssatoru takeuchi goto out; 39916d6d2829Ssatoru takeuchi } 39926ef5ed0dSJosef Bacik 39936ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 39943c04ce01SMiao Xie if (!path) { 39953c04ce01SMiao Xie ret = -ENOMEM; 39963c04ce01SMiao Xie goto out; 39973c04ce01SMiao Xie } 39986ef5ed0dSJosef Bacik path->leave_spinning = 1; 39996ef5ed0dSJosef Bacik 40006ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 400198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 40026ef5ed0dSJosef Bacik btrfs_free_path(path); 40033c04ce01SMiao Xie ret = PTR_ERR(trans); 40043c04ce01SMiao Xie goto out; 40056ef5ed0dSJosef Bacik } 40066ef5ed0dSJosef Bacik 40070b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 40080b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 40096ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4010cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 40116ef5ed0dSJosef Bacik btrfs_free_path(path); 40123a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40130b246afaSJeff Mahoney btrfs_err(fs_info, 40145d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 40153c04ce01SMiao Xie ret = -ENOENT; 40163c04ce01SMiao Xie goto out; 40176ef5ed0dSJosef Bacik } 40186ef5ed0dSJosef Bacik 40196ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 40206ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 40216ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 40226ef5ed0dSJosef Bacik btrfs_free_path(path); 40236ef5ed0dSJosef Bacik 40240b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 40253a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40263c04ce01SMiao Xie out: 40273c04ce01SMiao Xie mnt_drop_write_file(file); 40283c04ce01SMiao Xie return ret; 40296ef5ed0dSJosef Bacik } 40306ef5ed0dSJosef Bacik 4031c065f5b1SSu Yue static void get_block_group_info(struct list_head *groups_list, 4032bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4033bf5fc093SJosef Bacik { 4034bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 4035bf5fc093SJosef Bacik 4036bf5fc093SJosef Bacik space->total_bytes = 0; 4037bf5fc093SJosef Bacik space->used_bytes = 0; 4038bf5fc093SJosef Bacik space->flags = 0; 4039bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4040bf5fc093SJosef Bacik space->flags = block_group->flags; 4041b3470b5dSDavid Sterba space->total_bytes += block_group->length; 4042bf38be65SDavid Sterba space->used_bytes += block_group->used; 4043bf5fc093SJosef Bacik } 4044bf5fc093SJosef Bacik } 4045bf5fc093SJosef Bacik 40462ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 40472ff7e61eSJeff Mahoney void __user *arg) 40481406e432SJosef Bacik { 40491406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 40501406e432SJosef Bacik struct btrfs_ioctl_space_info space; 40511406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 40527fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 405313f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 40541406e432SJosef Bacik struct btrfs_space_info *info; 4055315d8e98SColin Ian King static const u64 types[] = { 4056315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 4057bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4058bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4059315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 4060315d8e98SColin Ian King }; 4061bf5fc093SJosef Bacik int num_types = 4; 40627fde62bfSChris Mason int alloc_size; 40631406e432SJosef Bacik int ret = 0; 406451788b1bSDan Rosenberg u64 slot_count = 0; 4065bf5fc093SJosef Bacik int i, c; 40661406e432SJosef Bacik 40671406e432SJosef Bacik if (copy_from_user(&space_args, 40681406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 40691406e432SJosef Bacik sizeof(space_args))) 40701406e432SJosef Bacik return -EFAULT; 40711406e432SJosef Bacik 4072bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4073bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4074bf5fc093SJosef Bacik 4075bf5fc093SJosef Bacik info = NULL; 40767fde62bfSChris Mason rcu_read_lock(); 40770b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4078bf5fc093SJosef Bacik list) { 4079bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4080bf5fc093SJosef Bacik info = tmp; 4081bf5fc093SJosef Bacik break; 4082bf5fc093SJosef Bacik } 4083bf5fc093SJosef Bacik } 40847fde62bfSChris Mason rcu_read_unlock(); 40851406e432SJosef Bacik 4086bf5fc093SJosef Bacik if (!info) 4087bf5fc093SJosef Bacik continue; 4088bf5fc093SJosef Bacik 4089bf5fc093SJosef Bacik down_read(&info->groups_sem); 4090bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4091bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4092bf5fc093SJosef Bacik slot_count++; 4093bf5fc093SJosef Bacik } 4094bf5fc093SJosef Bacik up_read(&info->groups_sem); 4095bf5fc093SJosef Bacik } 4096bf5fc093SJosef Bacik 409736523e95SDavid Sterba /* 409836523e95SDavid Sterba * Global block reserve, exported as a space_info 409936523e95SDavid Sterba */ 410036523e95SDavid Sterba slot_count++; 410136523e95SDavid Sterba 41027fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 41037fde62bfSChris Mason if (space_args.space_slots == 0) { 41047fde62bfSChris Mason space_args.total_spaces = slot_count; 41057fde62bfSChris Mason goto out; 41067fde62bfSChris Mason } 4107bf5fc093SJosef Bacik 410851788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4109bf5fc093SJosef Bacik 41107fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4111bf5fc093SJosef Bacik 41127fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 41137fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 41147fde62bfSChris Mason */ 411509cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 41167fde62bfSChris Mason return -ENOMEM; 41177fde62bfSChris Mason 41187fde62bfSChris Mason space_args.total_spaces = 0; 41198d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 41207fde62bfSChris Mason if (!dest) 41217fde62bfSChris Mason return -ENOMEM; 41227fde62bfSChris Mason dest_orig = dest; 41237fde62bfSChris Mason 41247fde62bfSChris Mason /* now we have a buffer to copy into */ 4125bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4126bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4127bf5fc093SJosef Bacik 412851788b1bSDan Rosenberg if (!slot_count) 412951788b1bSDan Rosenberg break; 413051788b1bSDan Rosenberg 4131bf5fc093SJosef Bacik info = NULL; 41321406e432SJosef Bacik rcu_read_lock(); 41330b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4134bf5fc093SJosef Bacik list) { 4135bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4136bf5fc093SJosef Bacik info = tmp; 41377fde62bfSChris Mason break; 4138bf5fc093SJosef Bacik } 4139bf5fc093SJosef Bacik } 4140bf5fc093SJosef Bacik rcu_read_unlock(); 41417fde62bfSChris Mason 4142bf5fc093SJosef Bacik if (!info) 4143bf5fc093SJosef Bacik continue; 4144bf5fc093SJosef Bacik down_read(&info->groups_sem); 4145bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4146bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 4147c065f5b1SSu Yue get_block_group_info(&info->block_groups[c], 4148c065f5b1SSu Yue &space); 41497fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 41501406e432SJosef Bacik dest++; 41511406e432SJosef Bacik space_args.total_spaces++; 415251788b1bSDan Rosenberg slot_count--; 41531406e432SJosef Bacik } 415451788b1bSDan Rosenberg if (!slot_count) 415551788b1bSDan Rosenberg break; 4156bf5fc093SJosef Bacik } 4157bf5fc093SJosef Bacik up_read(&info->groups_sem); 4158bf5fc093SJosef Bacik } 41591406e432SJosef Bacik 416036523e95SDavid Sterba /* 416136523e95SDavid Sterba * Add global block reserve 416236523e95SDavid Sterba */ 416336523e95SDavid Sterba if (slot_count) { 41640b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 416536523e95SDavid Sterba 416636523e95SDavid Sterba spin_lock(&block_rsv->lock); 416736523e95SDavid Sterba space.total_bytes = block_rsv->size; 416836523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 416936523e95SDavid Sterba spin_unlock(&block_rsv->lock); 417036523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 417136523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 417236523e95SDavid Sterba space_args.total_spaces++; 417336523e95SDavid Sterba } 417436523e95SDavid Sterba 41752eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 41767fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 41777fde62bfSChris Mason 41787fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 41797fde62bfSChris Mason ret = -EFAULT; 41807fde62bfSChris Mason 41817fde62bfSChris Mason kfree(dest_orig); 41827fde62bfSChris Mason out: 41837fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 41841406e432SJosef Bacik ret = -EFAULT; 41851406e432SJosef Bacik 41861406e432SJosef Bacik return ret; 41871406e432SJosef Bacik } 41881406e432SJosef Bacik 41899a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 41909a8c28beSMiao Xie void __user *argp) 419146204592SSage Weil { 419246204592SSage Weil struct btrfs_trans_handle *trans; 419346204592SSage Weil u64 transid; 4194db5b493aSTsutomu Itoh int ret; 419546204592SSage Weil 4196d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4197ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4198ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 419998d5dc13STsutomu Itoh return PTR_ERR(trans); 4200ff7c1d33SMiao Xie 4201ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4202ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4203ff7c1d33SMiao Xie goto out; 4204ff7c1d33SMiao Xie } 420546204592SSage Weil transid = trans->transid; 42063a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 0); 42078b2b2d3cSTsutomu Itoh if (ret) { 42083a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4209db5b493aSTsutomu Itoh return ret; 42108b2b2d3cSTsutomu Itoh } 4211ff7c1d33SMiao Xie out: 421246204592SSage Weil if (argp) 421346204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 421446204592SSage Weil return -EFAULT; 421546204592SSage Weil return 0; 421646204592SSage Weil } 421746204592SSage Weil 42182ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 42199a8c28beSMiao Xie void __user *argp) 422046204592SSage Weil { 422146204592SSage Weil u64 transid; 422246204592SSage Weil 422346204592SSage Weil if (argp) { 422446204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 422546204592SSage Weil return -EFAULT; 422646204592SSage Weil } else { 422746204592SSage Weil transid = 0; /* current trans */ 422846204592SSage Weil } 42292ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 423046204592SSage Weil } 423146204592SSage Weil 4232b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4233475f6387SJan Schmidt { 42340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4235475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4236b8e95489SMiao Xie int ret; 4237475f6387SJan Schmidt 4238475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4239475f6387SJan Schmidt return -EPERM; 4240475f6387SJan Schmidt 4241475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4242475f6387SJan Schmidt if (IS_ERR(sa)) 4243475f6387SJan Schmidt return PTR_ERR(sa); 4244475f6387SJan Schmidt 4245b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4246b8e95489SMiao Xie ret = mnt_want_write_file(file); 4247b8e95489SMiao Xie if (ret) 4248b8e95489SMiao Xie goto out; 4249b8e95489SMiao Xie } 4250b8e95489SMiao Xie 42510b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 425263a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 425363a212abSStefan Behrens 0); 4254475f6387SJan Schmidt 425506fe39abSFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4256475f6387SJan Schmidt ret = -EFAULT; 4257475f6387SJan Schmidt 4258b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4259b8e95489SMiao Xie mnt_drop_write_file(file); 4260b8e95489SMiao Xie out: 4261475f6387SJan Schmidt kfree(sa); 4262475f6387SJan Schmidt return ret; 4263475f6387SJan Schmidt } 4264475f6387SJan Schmidt 42652ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4266475f6387SJan Schmidt { 4267475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4268475f6387SJan Schmidt return -EPERM; 4269475f6387SJan Schmidt 42702ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4271475f6387SJan Schmidt } 4272475f6387SJan Schmidt 42732ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4274475f6387SJan Schmidt void __user *arg) 4275475f6387SJan Schmidt { 4276475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4277475f6387SJan Schmidt int ret; 4278475f6387SJan Schmidt 4279475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4280475f6387SJan Schmidt return -EPERM; 4281475f6387SJan Schmidt 4282475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4283475f6387SJan Schmidt if (IS_ERR(sa)) 4284475f6387SJan Schmidt return PTR_ERR(sa); 4285475f6387SJan Schmidt 42862ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4287475f6387SJan Schmidt 42884fa99b00SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4289475f6387SJan Schmidt ret = -EFAULT; 4290475f6387SJan Schmidt 4291475f6387SJan Schmidt kfree(sa); 4292475f6387SJan Schmidt return ret; 4293475f6387SJan Schmidt } 4294475f6387SJan Schmidt 42952ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4296b27f7c0cSDavid Sterba void __user *arg) 4297c11d2c23SStefan Behrens { 4298c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4299c11d2c23SStefan Behrens int ret; 4300c11d2c23SStefan Behrens 4301c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4302c11d2c23SStefan Behrens if (IS_ERR(sa)) 4303c11d2c23SStefan Behrens return PTR_ERR(sa); 4304c11d2c23SStefan Behrens 4305b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4306b27f7c0cSDavid Sterba kfree(sa); 4307b27f7c0cSDavid Sterba return -EPERM; 4308b27f7c0cSDavid Sterba } 4309b27f7c0cSDavid Sterba 43102ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4311c11d2c23SStefan Behrens 4312eee99577SFilipe Manana if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) 4313c11d2c23SStefan Behrens ret = -EFAULT; 4314c11d2c23SStefan Behrens 4315c11d2c23SStefan Behrens kfree(sa); 4316c11d2c23SStefan Behrens return ret; 4317c11d2c23SStefan Behrens } 4318c11d2c23SStefan Behrens 43192ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 43202ff7e61eSJeff Mahoney void __user *arg) 43213f6bcfbdSStefan Behrens { 43223f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 43233f6bcfbdSStefan Behrens int ret; 43243f6bcfbdSStefan Behrens 43253f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 43263f6bcfbdSStefan Behrens return -EPERM; 43273f6bcfbdSStefan Behrens 43283f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 43293f6bcfbdSStefan Behrens if (IS_ERR(p)) 43303f6bcfbdSStefan Behrens return PTR_ERR(p); 43313f6bcfbdSStefan Behrens 43323f6bcfbdSStefan Behrens switch (p->cmd) { 43333f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4334bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4335adfa97cbSIlya Dryomov ret = -EROFS; 4336adfa97cbSIlya Dryomov goto out; 4337adfa97cbSIlya Dryomov } 4338171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4339e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 43403f6bcfbdSStefan Behrens } else { 43412ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4342171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 43433f6bcfbdSStefan Behrens } 43443f6bcfbdSStefan Behrens break; 43453f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 43460b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 43473f6bcfbdSStefan Behrens ret = 0; 43483f6bcfbdSStefan Behrens break; 43493f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 435017d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 435197282031SAnand Jain ret = 0; 43523f6bcfbdSStefan Behrens break; 43533f6bcfbdSStefan Behrens default: 43543f6bcfbdSStefan Behrens ret = -EINVAL; 43553f6bcfbdSStefan Behrens break; 43563f6bcfbdSStefan Behrens } 43573f6bcfbdSStefan Behrens 4358d3a53286SFilipe Manana if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) 43593f6bcfbdSStefan Behrens ret = -EFAULT; 4360adfa97cbSIlya Dryomov out: 43613f6bcfbdSStefan Behrens kfree(p); 43623f6bcfbdSStefan Behrens return ret; 43633f6bcfbdSStefan Behrens } 43643f6bcfbdSStefan Behrens 4365d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4366d7728c96SJan Schmidt { 4367d7728c96SJan Schmidt int ret = 0; 4368d7728c96SJan Schmidt int i; 4369740c3d22SChris Mason u64 rel_ptr; 4370d7728c96SJan Schmidt int size; 4371806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4372d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4373d7728c96SJan Schmidt struct btrfs_path *path; 4374d7728c96SJan Schmidt 437582b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4376d7728c96SJan Schmidt return -EPERM; 4377d7728c96SJan Schmidt 4378d7728c96SJan Schmidt path = btrfs_alloc_path(); 4379d7728c96SJan Schmidt if (!path) { 4380d7728c96SJan Schmidt ret = -ENOMEM; 4381d7728c96SJan Schmidt goto out; 4382d7728c96SJan Schmidt } 4383d7728c96SJan Schmidt 4384d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4385d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4386d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4387d7728c96SJan Schmidt ipa = NULL; 4388d7728c96SJan Schmidt goto out; 4389d7728c96SJan Schmidt } 4390d7728c96SJan Schmidt 4391d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4392d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4393d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4394d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4395d7728c96SJan Schmidt ipath = NULL; 4396d7728c96SJan Schmidt goto out; 4397d7728c96SJan Schmidt } 4398d7728c96SJan Schmidt 4399d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4400d7728c96SJan Schmidt if (ret < 0) 4401d7728c96SJan Schmidt goto out; 4402d7728c96SJan Schmidt 4403d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4404745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4405745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4406740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4407d7728c96SJan Schmidt } 4408d7728c96SJan Schmidt 4409718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4410718dc5faSOmar Sandoval ipath->fspath, size); 4411d7728c96SJan Schmidt if (ret) { 4412d7728c96SJan Schmidt ret = -EFAULT; 4413d7728c96SJan Schmidt goto out; 4414d7728c96SJan Schmidt } 4415d7728c96SJan Schmidt 4416d7728c96SJan Schmidt out: 4417d7728c96SJan Schmidt btrfs_free_path(path); 4418d7728c96SJan Schmidt free_ipath(ipath); 4419d7728c96SJan Schmidt kfree(ipa); 4420d7728c96SJan Schmidt 4421d7728c96SJan Schmidt return ret; 4422d7728c96SJan Schmidt } 4423d7728c96SJan Schmidt 4424d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4425d7728c96SJan Schmidt { 4426d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4427d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4428d7728c96SJan Schmidt 4429d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4430d7728c96SJan Schmidt inodes->bytes_left -= c; 4431d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4432d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4433d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4434d7728c96SJan Schmidt inodes->elem_cnt += 3; 4435d7728c96SJan Schmidt } else { 4436d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4437d7728c96SJan Schmidt inodes->bytes_left = 0; 4438d7728c96SJan Schmidt inodes->elem_missed += 3; 4439d7728c96SJan Schmidt } 4440d7728c96SJan Schmidt 4441d7728c96SJan Schmidt return 0; 4442d7728c96SJan Schmidt } 4443d7728c96SJan Schmidt 44442ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4445d24a67b2SZygo Blaxell void __user *arg, int version) 4446d7728c96SJan Schmidt { 4447d7728c96SJan Schmidt int ret = 0; 4448d7728c96SJan Schmidt int size; 4449d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4450d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4451d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4452d24a67b2SZygo Blaxell bool ignore_offset; 4453d7728c96SJan Schmidt 4454d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4455d7728c96SJan Schmidt return -EPERM; 4456d7728c96SJan Schmidt 4457d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 44587b9ea627SShailendra Verma if (IS_ERR(loi)) 44597b9ea627SShailendra Verma return PTR_ERR(loi); 4460d7728c96SJan Schmidt 4461d24a67b2SZygo Blaxell if (version == 1) { 4462d24a67b2SZygo Blaxell ignore_offset = false; 4463b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4464d24a67b2SZygo Blaxell } else { 4465d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4466d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4467d24a67b2SZygo Blaxell ret = -EINVAL; 4468d24a67b2SZygo Blaxell goto out_loi; 4469d24a67b2SZygo Blaxell } 4470d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4471d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4472d24a67b2SZygo Blaxell ret = -EINVAL; 4473d24a67b2SZygo Blaxell goto out_loi; 4474d24a67b2SZygo Blaxell } 4475d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4476b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4477d24a67b2SZygo Blaxell } 4478d24a67b2SZygo Blaxell 4479d7728c96SJan Schmidt path = btrfs_alloc_path(); 4480d7728c96SJan Schmidt if (!path) { 4481d7728c96SJan Schmidt ret = -ENOMEM; 4482d7728c96SJan Schmidt goto out; 4483d7728c96SJan Schmidt } 4484d7728c96SJan Schmidt 4485d7728c96SJan Schmidt inodes = init_data_container(size); 4486d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4487d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4488d7728c96SJan Schmidt inodes = NULL; 4489d7728c96SJan Schmidt goto out; 4490d7728c96SJan Schmidt } 4491d7728c96SJan Schmidt 44922ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4493d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4494df031f07SLiu Bo if (ret == -EINVAL) 4495d7728c96SJan Schmidt ret = -ENOENT; 4496d7728c96SJan Schmidt if (ret < 0) 4497d7728c96SJan Schmidt goto out; 4498d7728c96SJan Schmidt 4499718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4500718dc5faSOmar Sandoval size); 4501d7728c96SJan Schmidt if (ret) 4502d7728c96SJan Schmidt ret = -EFAULT; 4503d7728c96SJan Schmidt 4504d7728c96SJan Schmidt out: 4505d7728c96SJan Schmidt btrfs_free_path(path); 4506f54de068SDavid Sterba kvfree(inodes); 4507d24a67b2SZygo Blaxell out_loi: 4508d7728c96SJan Schmidt kfree(loi); 4509d7728c96SJan Schmidt 4510d7728c96SJan Schmidt return ret; 4511d7728c96SJan Schmidt } 4512d7728c96SJan Schmidt 4513008ef096SDavid Sterba void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 4514c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4515c9e9f97bSIlya Dryomov { 4516c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4517c9e9f97bSIlya Dryomov 4518c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4519c9e9f97bSIlya Dryomov 45203009a62fSDavid Sterba if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) 4521837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4522837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4523837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4524a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4525a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4526837d5b6eSIlya Dryomov 4527c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4528c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4529c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 453019a39dceSIlya Dryomov 453119a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 453219a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 453319a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 4534c9e9f97bSIlya Dryomov } 4535c9e9f97bSIlya Dryomov 45369ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4537c9e9f97bSIlya Dryomov { 4538496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4539c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4540c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4541c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4542ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4543c9e9f97bSIlya Dryomov int ret; 4544c9e9f97bSIlya Dryomov 4545c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4546c9e9f97bSIlya Dryomov return -EPERM; 4547c9e9f97bSIlya Dryomov 4548e54bfa31SLiu Bo ret = mnt_want_write_file(file); 45499ba1f6e4SLiu Bo if (ret) 45509ba1f6e4SLiu Bo return ret; 45519ba1f6e4SLiu Bo 4552ed0fb78fSIlya Dryomov again: 4553171938e5SDavid Sterba if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4554c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4555ed0fb78fSIlya Dryomov need_unlock = true; 4556ed0fb78fSIlya Dryomov goto locked; 4557ed0fb78fSIlya Dryomov } 4558ed0fb78fSIlya Dryomov 4559ed0fb78fSIlya Dryomov /* 456001327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4561ed0fb78fSIlya Dryomov * (1) some other op is running 4562ed0fb78fSIlya Dryomov * (2) balance is running 4563ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4564ed0fb78fSIlya Dryomov */ 4565ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4566ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4567ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 45683009a62fSDavid Sterba if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4569ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4570dccdb07bSDavid Sterba /* 4571dccdb07bSDavid Sterba * Lock released to allow other waiters to continue, 4572dccdb07bSDavid Sterba * we'll reexamine the status again. 4573dccdb07bSDavid Sterba */ 4574ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4575ed0fb78fSIlya Dryomov 4576ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 45773009a62fSDavid Sterba !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { 4578ed0fb78fSIlya Dryomov /* this is (3) */ 4579ed0fb78fSIlya Dryomov need_unlock = false; 4580ed0fb78fSIlya Dryomov goto locked; 4581ed0fb78fSIlya Dryomov } 4582ed0fb78fSIlya Dryomov 4583ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4584ed0fb78fSIlya Dryomov goto again; 4585ed0fb78fSIlya Dryomov } else { 4586ed0fb78fSIlya Dryomov /* this is (2) */ 4587ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4588ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4589ed0fb78fSIlya Dryomov goto out; 4590ed0fb78fSIlya Dryomov } 4591ed0fb78fSIlya Dryomov } else { 4592ed0fb78fSIlya Dryomov /* this is (1) */ 4593ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4594e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4595ed0fb78fSIlya Dryomov goto out; 4596ed0fb78fSIlya Dryomov } 4597ed0fb78fSIlya Dryomov 4598ed0fb78fSIlya Dryomov locked: 4599171938e5SDavid Sterba BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); 4600c9e9f97bSIlya Dryomov 4601c9e9f97bSIlya Dryomov if (arg) { 4602c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4603c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4604c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4605ed0fb78fSIlya Dryomov goto out_unlock; 4606c9e9f97bSIlya Dryomov } 4607de322263SIlya Dryomov 4608de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4609de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4610de322263SIlya Dryomov ret = -ENOTCONN; 4611de322263SIlya Dryomov goto out_bargs; 4612de322263SIlya Dryomov } 4613de322263SIlya Dryomov 4614de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4615de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4616de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4617de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4618de322263SIlya Dryomov 4619de322263SIlya Dryomov goto do_balance; 4620de322263SIlya Dryomov } 4621c9e9f97bSIlya Dryomov } else { 4622c9e9f97bSIlya Dryomov bargs = NULL; 4623c9e9f97bSIlya Dryomov } 4624c9e9f97bSIlya Dryomov 4625ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4626837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4627837d5b6eSIlya Dryomov goto out_bargs; 4628837d5b6eSIlya Dryomov } 4629837d5b6eSIlya Dryomov 46308d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4631c9e9f97bSIlya Dryomov if (!bctl) { 4632c9e9f97bSIlya Dryomov ret = -ENOMEM; 4633c9e9f97bSIlya Dryomov goto out_bargs; 4634c9e9f97bSIlya Dryomov } 4635c9e9f97bSIlya Dryomov 4636c9e9f97bSIlya Dryomov if (arg) { 4637c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4638c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4639c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4640c9e9f97bSIlya Dryomov 4641c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4642f43ffb60SIlya Dryomov } else { 4643f43ffb60SIlya Dryomov /* balance everything - no filters */ 4644f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4645c9e9f97bSIlya Dryomov } 4646c9e9f97bSIlya Dryomov 46478eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 46488eb93459SDavid Sterba ret = -EINVAL; 46490f89abf5SChristian Engelmayer goto out_bctl; 46508eb93459SDavid Sterba } 46518eb93459SDavid Sterba 4652de322263SIlya Dryomov do_balance: 4653c9e9f97bSIlya Dryomov /* 4654149196a2SDavid Sterba * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to 4655149196a2SDavid Sterba * btrfs_balance. bctl is freed in reset_balance_state, or, if 4656149196a2SDavid Sterba * restriper was paused all the way until unmount, in free_fs_info. 4657149196a2SDavid Sterba * The flag should be cleared after reset_balance_state. 4658c9e9f97bSIlya Dryomov */ 4659ed0fb78fSIlya Dryomov need_unlock = false; 4660ed0fb78fSIlya Dryomov 46616fcf6e2bSDavid Sterba ret = btrfs_balance(fs_info, bctl, bargs); 46620f89abf5SChristian Engelmayer bctl = NULL; 4663ed0fb78fSIlya Dryomov 4664d00c2d9cSFilipe Manana if ((ret == 0 || ret == -ECANCELED) && arg) { 4665c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4666c9e9f97bSIlya Dryomov ret = -EFAULT; 4667c9e9f97bSIlya Dryomov } 4668c9e9f97bSIlya Dryomov 46690f89abf5SChristian Engelmayer out_bctl: 46700f89abf5SChristian Engelmayer kfree(bctl); 4671c9e9f97bSIlya Dryomov out_bargs: 4672c9e9f97bSIlya Dryomov kfree(bargs); 4673ed0fb78fSIlya Dryomov out_unlock: 4674c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4675ed0fb78fSIlya Dryomov if (need_unlock) 4676171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 4677ed0fb78fSIlya Dryomov out: 4678e54bfa31SLiu Bo mnt_drop_write_file(file); 4679c9e9f97bSIlya Dryomov return ret; 4680c9e9f97bSIlya Dryomov } 4681c9e9f97bSIlya Dryomov 46822ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4683837d5b6eSIlya Dryomov { 4684837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4685837d5b6eSIlya Dryomov return -EPERM; 4686837d5b6eSIlya Dryomov 4687837d5b6eSIlya Dryomov switch (cmd) { 4688837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 46890b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4690a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 46910b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4692837d5b6eSIlya Dryomov } 4693837d5b6eSIlya Dryomov 4694837d5b6eSIlya Dryomov return -EINVAL; 4695837d5b6eSIlya Dryomov } 4696837d5b6eSIlya Dryomov 46972ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 469819a39dceSIlya Dryomov void __user *arg) 469919a39dceSIlya Dryomov { 470019a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 470119a39dceSIlya Dryomov int ret = 0; 470219a39dceSIlya Dryomov 470319a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 470419a39dceSIlya Dryomov return -EPERM; 470519a39dceSIlya Dryomov 470619a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 470719a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 470819a39dceSIlya Dryomov ret = -ENOTCONN; 470919a39dceSIlya Dryomov goto out; 471019a39dceSIlya Dryomov } 471119a39dceSIlya Dryomov 47128d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 471319a39dceSIlya Dryomov if (!bargs) { 471419a39dceSIlya Dryomov ret = -ENOMEM; 471519a39dceSIlya Dryomov goto out; 471619a39dceSIlya Dryomov } 471719a39dceSIlya Dryomov 4718008ef096SDavid Sterba btrfs_update_ioctl_balance_args(fs_info, bargs); 471919a39dceSIlya Dryomov 472019a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 472119a39dceSIlya Dryomov ret = -EFAULT; 472219a39dceSIlya Dryomov 472319a39dceSIlya Dryomov kfree(bargs); 472419a39dceSIlya Dryomov out: 472519a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 472619a39dceSIlya Dryomov return ret; 472719a39dceSIlya Dryomov } 472819a39dceSIlya Dryomov 4729905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 47305d13a37bSArne Jansen { 47310b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47320b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47335d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 47345d13a37bSArne Jansen int ret; 47355d13a37bSArne Jansen 47365d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47375d13a37bSArne Jansen return -EPERM; 47385d13a37bSArne Jansen 4739905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4740905b0ddaSMiao Xie if (ret) 4741905b0ddaSMiao Xie return ret; 47425d13a37bSArne Jansen 47435d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4744905b0ddaSMiao Xie if (IS_ERR(sa)) { 4745905b0ddaSMiao Xie ret = PTR_ERR(sa); 4746905b0ddaSMiao Xie goto drop_write; 4747905b0ddaSMiao Xie } 47485d13a37bSArne Jansen 47490b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 47505d13a37bSArne Jansen 47515d13a37bSArne Jansen switch (sa->cmd) { 47525d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 4753340f1aa2SNikolay Borisov ret = btrfs_quota_enable(fs_info); 47545d13a37bSArne Jansen break; 47555d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 4756340f1aa2SNikolay Borisov ret = btrfs_quota_disable(fs_info); 47575d13a37bSArne Jansen break; 47585d13a37bSArne Jansen default: 47595d13a37bSArne Jansen ret = -EINVAL; 47605d13a37bSArne Jansen break; 47615d13a37bSArne Jansen } 47625d13a37bSArne Jansen 47635d13a37bSArne Jansen kfree(sa); 47640b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4765905b0ddaSMiao Xie drop_write: 4766905b0ddaSMiao Xie mnt_drop_write_file(file); 47675d13a37bSArne Jansen return ret; 47685d13a37bSArne Jansen } 47695d13a37bSArne Jansen 4770905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 47715d13a37bSArne Jansen { 47720b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47730b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47740b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 47755d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 47765d13a37bSArne Jansen struct btrfs_trans_handle *trans; 47775d13a37bSArne Jansen int ret; 47785d13a37bSArne Jansen int err; 47795d13a37bSArne Jansen 47805d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47815d13a37bSArne Jansen return -EPERM; 47825d13a37bSArne Jansen 4783905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4784905b0ddaSMiao Xie if (ret) 4785905b0ddaSMiao Xie return ret; 47865d13a37bSArne Jansen 47875d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4788905b0ddaSMiao Xie if (IS_ERR(sa)) { 4789905b0ddaSMiao Xie ret = PTR_ERR(sa); 4790905b0ddaSMiao Xie goto drop_write; 4791905b0ddaSMiao Xie } 47925d13a37bSArne Jansen 47935d13a37bSArne Jansen trans = btrfs_join_transaction(root); 47945d13a37bSArne Jansen if (IS_ERR(trans)) { 47955d13a37bSArne Jansen ret = PTR_ERR(trans); 47965d13a37bSArne Jansen goto out; 47975d13a37bSArne Jansen } 47985d13a37bSArne Jansen 47995d13a37bSArne Jansen if (sa->assign) { 48009f8a6ce6SLu Fengqi ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); 48015d13a37bSArne Jansen } else { 480239616c27SLu Fengqi ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); 48035d13a37bSArne Jansen } 48045d13a37bSArne Jansen 4805e082f563SQu Wenruo /* update qgroup status and info */ 4806280f8bd2SLu Fengqi err = btrfs_run_qgroups(trans); 4807e082f563SQu Wenruo if (err < 0) 48080b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4809ad8403dfSAnand Jain "failed to update qgroup status and info"); 48103a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 48115d13a37bSArne Jansen if (err && !ret) 48125d13a37bSArne Jansen ret = err; 48135d13a37bSArne Jansen 48145d13a37bSArne Jansen out: 48155d13a37bSArne Jansen kfree(sa); 4816905b0ddaSMiao Xie drop_write: 4817905b0ddaSMiao Xie mnt_drop_write_file(file); 48185d13a37bSArne Jansen return ret; 48195d13a37bSArne Jansen } 48205d13a37bSArne Jansen 4821905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 48225d13a37bSArne Jansen { 48230b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48240b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48255d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 48265d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48275d13a37bSArne Jansen int ret; 48285d13a37bSArne Jansen int err; 48295d13a37bSArne Jansen 48305d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48315d13a37bSArne Jansen return -EPERM; 48325d13a37bSArne Jansen 4833905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4834905b0ddaSMiao Xie if (ret) 4835905b0ddaSMiao Xie return ret; 48365d13a37bSArne Jansen 48375d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4838905b0ddaSMiao Xie if (IS_ERR(sa)) { 4839905b0ddaSMiao Xie ret = PTR_ERR(sa); 4840905b0ddaSMiao Xie goto drop_write; 4841905b0ddaSMiao Xie } 48425d13a37bSArne Jansen 4843d86e56cfSMiao Xie if (!sa->qgroupid) { 4844d86e56cfSMiao Xie ret = -EINVAL; 4845d86e56cfSMiao Xie goto out; 4846d86e56cfSMiao Xie } 4847d86e56cfSMiao Xie 48485d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48495d13a37bSArne Jansen if (IS_ERR(trans)) { 48505d13a37bSArne Jansen ret = PTR_ERR(trans); 48515d13a37bSArne Jansen goto out; 48525d13a37bSArne Jansen } 48535d13a37bSArne Jansen 48545d13a37bSArne Jansen if (sa->create) { 485549a05ecdSLu Fengqi ret = btrfs_create_qgroup(trans, sa->qgroupid); 48565d13a37bSArne Jansen } else { 48573efbee1dSLu Fengqi ret = btrfs_remove_qgroup(trans, sa->qgroupid); 48585d13a37bSArne Jansen } 48595d13a37bSArne Jansen 48603a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 48615d13a37bSArne Jansen if (err && !ret) 48625d13a37bSArne Jansen ret = err; 48635d13a37bSArne Jansen 48645d13a37bSArne Jansen out: 48655d13a37bSArne Jansen kfree(sa); 4866905b0ddaSMiao Xie drop_write: 4867905b0ddaSMiao Xie mnt_drop_write_file(file); 48685d13a37bSArne Jansen return ret; 48695d13a37bSArne Jansen } 48705d13a37bSArne Jansen 4871905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 48725d13a37bSArne Jansen { 48730b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48740b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48755d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 48765d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48775d13a37bSArne Jansen int ret; 48785d13a37bSArne Jansen int err; 48795d13a37bSArne Jansen u64 qgroupid; 48805d13a37bSArne Jansen 48815d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48825d13a37bSArne Jansen return -EPERM; 48835d13a37bSArne Jansen 4884905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4885905b0ddaSMiao Xie if (ret) 4886905b0ddaSMiao Xie return ret; 48875d13a37bSArne Jansen 48885d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4889905b0ddaSMiao Xie if (IS_ERR(sa)) { 4890905b0ddaSMiao Xie ret = PTR_ERR(sa); 4891905b0ddaSMiao Xie goto drop_write; 4892905b0ddaSMiao Xie } 48935d13a37bSArne Jansen 48945d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48955d13a37bSArne Jansen if (IS_ERR(trans)) { 48965d13a37bSArne Jansen ret = PTR_ERR(trans); 48975d13a37bSArne Jansen goto out; 48985d13a37bSArne Jansen } 48995d13a37bSArne Jansen 49005d13a37bSArne Jansen qgroupid = sa->qgroupid; 49015d13a37bSArne Jansen if (!qgroupid) { 49025d13a37bSArne Jansen /* take the current subvol as qgroup */ 49035d13a37bSArne Jansen qgroupid = root->root_key.objectid; 49045d13a37bSArne Jansen } 49055d13a37bSArne Jansen 4906f0042d5eSLu Fengqi ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); 49075d13a37bSArne Jansen 49083a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49095d13a37bSArne Jansen if (err && !ret) 49105d13a37bSArne Jansen ret = err; 49115d13a37bSArne Jansen 49125d13a37bSArne Jansen out: 49135d13a37bSArne Jansen kfree(sa); 4914905b0ddaSMiao Xie drop_write: 4915905b0ddaSMiao Xie mnt_drop_write_file(file); 49165d13a37bSArne Jansen return ret; 49175d13a37bSArne Jansen } 49185d13a37bSArne Jansen 49192f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 49202f232036SJan Schmidt { 49210b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49220b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49232f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49242f232036SJan Schmidt int ret; 49252f232036SJan Schmidt 49262f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49272f232036SJan Schmidt return -EPERM; 49282f232036SJan Schmidt 49292f232036SJan Schmidt ret = mnt_want_write_file(file); 49302f232036SJan Schmidt if (ret) 49312f232036SJan Schmidt return ret; 49322f232036SJan Schmidt 49332f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 49342f232036SJan Schmidt if (IS_ERR(qsa)) { 49352f232036SJan Schmidt ret = PTR_ERR(qsa); 49362f232036SJan Schmidt goto drop_write; 49372f232036SJan Schmidt } 49382f232036SJan Schmidt 49392f232036SJan Schmidt if (qsa->flags) { 49402f232036SJan Schmidt ret = -EINVAL; 49412f232036SJan Schmidt goto out; 49422f232036SJan Schmidt } 49432f232036SJan Schmidt 49440b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 49452f232036SJan Schmidt 49462f232036SJan Schmidt out: 49472f232036SJan Schmidt kfree(qsa); 49482f232036SJan Schmidt drop_write: 49492f232036SJan Schmidt mnt_drop_write_file(file); 49502f232036SJan Schmidt return ret; 49512f232036SJan Schmidt } 49522f232036SJan Schmidt 4953b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, 4954b929c1d8SMarcos Paulo de Souza void __user *arg) 49552f232036SJan Schmidt { 49562f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49572f232036SJan Schmidt int ret = 0; 49582f232036SJan Schmidt 49592f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49602f232036SJan Schmidt return -EPERM; 49612f232036SJan Schmidt 49628d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 49632f232036SJan Schmidt if (!qsa) 49642f232036SJan Schmidt return -ENOMEM; 49652f232036SJan Schmidt 49660b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 49672f232036SJan Schmidt qsa->flags = 1; 49680b246afaSJeff Mahoney qsa->progress = fs_info->qgroup_rescan_progress.objectid; 49692f232036SJan Schmidt } 49702f232036SJan Schmidt 49712f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 49722f232036SJan Schmidt ret = -EFAULT; 49732f232036SJan Schmidt 49742f232036SJan Schmidt kfree(qsa); 49752f232036SJan Schmidt return ret; 49762f232036SJan Schmidt } 49772f232036SJan Schmidt 4978b929c1d8SMarcos Paulo de Souza static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, 4979b929c1d8SMarcos Paulo de Souza void __user *arg) 498057254b6eSJan Schmidt { 498157254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 498257254b6eSJan Schmidt return -EPERM; 498357254b6eSJan Schmidt 49840b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 498557254b6eSJan Schmidt } 498657254b6eSJan Schmidt 4987abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4988abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 49898ea05e3aSAlexander Block { 4990496ad9aaSAl Viro struct inode *inode = file_inode(file); 49910b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49928ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 49938ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 49948ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 499595582b00SDeepa Dinamani struct timespec64 ct = current_time(inode); 49968ea05e3aSAlexander Block int ret = 0; 4997dd5f9615SStefan Behrens int received_uuid_changed; 49988ea05e3aSAlexander Block 4999bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5000bd60ea0fSDavid Sterba return -EPERM; 5001bd60ea0fSDavid Sterba 50028ea05e3aSAlexander Block ret = mnt_want_write_file(file); 50038ea05e3aSAlexander Block if (ret < 0) 50048ea05e3aSAlexander Block return ret; 50058ea05e3aSAlexander Block 50060b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 50078ea05e3aSAlexander Block 50084a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 50098ea05e3aSAlexander Block ret = -EINVAL; 50108ea05e3aSAlexander Block goto out; 50118ea05e3aSAlexander Block } 50128ea05e3aSAlexander Block 50138ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 50148ea05e3aSAlexander Block ret = -EROFS; 50158ea05e3aSAlexander Block goto out; 50168ea05e3aSAlexander Block } 50178ea05e3aSAlexander Block 5018dd5f9615SStefan Behrens /* 5019dd5f9615SStefan Behrens * 1 - root item 5020dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5021dd5f9615SStefan Behrens */ 5022dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 50238ea05e3aSAlexander Block if (IS_ERR(trans)) { 50248ea05e3aSAlexander Block ret = PTR_ERR(trans); 50258ea05e3aSAlexander Block trans = NULL; 50268ea05e3aSAlexander Block goto out; 50278ea05e3aSAlexander Block } 50288ea05e3aSAlexander Block 50298ea05e3aSAlexander Block sa->rtransid = trans->transid; 50308ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 50318ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 50328ea05e3aSAlexander Block 5033dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5034dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5035dd5f9615SStefan Behrens if (received_uuid_changed && 5036d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 5037d1957791SLu Fengqi ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, 5038dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5039dd5f9615SStefan Behrens root->root_key.objectid); 5040d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 5041d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 5042d87ff758SNikolay Borisov btrfs_end_transaction(trans); 5043d87ff758SNikolay Borisov goto out; 5044d87ff758SNikolay Borisov } 5045d87ff758SNikolay Borisov } 50468ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 50478ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 50488ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 50493cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 50503cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 50513cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 50523cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 50538ea05e3aSAlexander Block 50540b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 50558ea05e3aSAlexander Block &root->root_key, &root->root_item); 50568ea05e3aSAlexander Block if (ret < 0) { 50573a45bb20SJeff Mahoney btrfs_end_transaction(trans); 50588ea05e3aSAlexander Block goto out; 5059dd5f9615SStefan Behrens } 5060dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 5061cdb345a8SLu Fengqi ret = btrfs_uuid_tree_add(trans, sa->uuid, 5062dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5063dd5f9615SStefan Behrens root->root_key.objectid); 5064dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 506566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 5066efd38150SNikolay Borisov btrfs_end_transaction(trans); 5067dd5f9615SStefan Behrens goto out; 5068dd5f9615SStefan Behrens } 5069dd5f9615SStefan Behrens } 50703a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5071abccd00fSHugo Mills out: 50720b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5073abccd00fSHugo Mills mnt_drop_write_file(file); 5074abccd00fSHugo Mills return ret; 5075abccd00fSHugo Mills } 5076abccd00fSHugo Mills 5077abccd00fSHugo Mills #ifdef CONFIG_64BIT 5078abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5079abccd00fSHugo Mills void __user *arg) 5080abccd00fSHugo Mills { 5081abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5082abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5083abccd00fSHugo Mills int ret = 0; 5084abccd00fSHugo Mills 5085abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 50867b9ea627SShailendra Verma if (IS_ERR(args32)) 50877b9ea627SShailendra Verma return PTR_ERR(args32); 5088abccd00fSHugo Mills 50898d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 509084dbeb87SDan Carpenter if (!args64) { 509184dbeb87SDan Carpenter ret = -ENOMEM; 5092abccd00fSHugo Mills goto out; 5093abccd00fSHugo Mills } 5094abccd00fSHugo Mills 5095abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5096abccd00fSHugo Mills args64->stransid = args32->stransid; 5097abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5098abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5099abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5100abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5101abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5102abccd00fSHugo Mills args64->flags = args32->flags; 5103abccd00fSHugo Mills 5104abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5105abccd00fSHugo Mills if (ret) 5106abccd00fSHugo Mills goto out; 5107abccd00fSHugo Mills 5108abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5109abccd00fSHugo Mills args32->stransid = args64->stransid; 5110abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5111abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5112abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5113abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5114abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5115abccd00fSHugo Mills args32->flags = args64->flags; 5116abccd00fSHugo Mills 5117abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5118abccd00fSHugo Mills if (ret) 5119abccd00fSHugo Mills ret = -EFAULT; 5120abccd00fSHugo Mills 5121abccd00fSHugo Mills out: 5122abccd00fSHugo Mills kfree(args32); 5123abccd00fSHugo Mills kfree(args64); 5124abccd00fSHugo Mills return ret; 5125abccd00fSHugo Mills } 5126abccd00fSHugo Mills #endif 5127abccd00fSHugo Mills 5128abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5129abccd00fSHugo Mills void __user *arg) 5130abccd00fSHugo Mills { 5131abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5132abccd00fSHugo Mills int ret = 0; 5133abccd00fSHugo Mills 5134abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 51357b9ea627SShailendra Verma if (IS_ERR(sa)) 51367b9ea627SShailendra Verma return PTR_ERR(sa); 5137abccd00fSHugo Mills 5138abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5139abccd00fSHugo Mills 5140abccd00fSHugo Mills if (ret) 5141abccd00fSHugo Mills goto out; 5142abccd00fSHugo Mills 51438ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 51448ea05e3aSAlexander Block if (ret) 51458ea05e3aSAlexander Block ret = -EFAULT; 51468ea05e3aSAlexander Block 51478ea05e3aSAlexander Block out: 51488ea05e3aSAlexander Block kfree(sa); 51498ea05e3aSAlexander Block return ret; 51508ea05e3aSAlexander Block } 51518ea05e3aSAlexander Block 5152b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, 5153b929c1d8SMarcos Paulo de Souza void __user *arg) 5154867ab667Sjeff.liu { 5155a1b83ac5SAnand Jain size_t len; 5156867ab667Sjeff.liu int ret; 5157a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5158a1b83ac5SAnand Jain 51590b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 51600b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 51610b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 5162a1b83ac5SAnand Jain 5163a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5164867ab667Sjeff.liu 5165867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 51660b246afaSJeff Mahoney btrfs_warn(fs_info, 51670b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 51680b246afaSJeff Mahoney --len); 5169867ab667Sjeff.liu } 5170867ab667Sjeff.liu 5171867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5172867ab667Sjeff.liu 5173867ab667Sjeff.liu return ret ? -EFAULT : 0; 5174867ab667Sjeff.liu } 5175867ab667Sjeff.liu 5176a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5177a8bfd4abSjeff.liu { 51780b246afaSJeff Mahoney struct inode *inode = file_inode(file); 51790b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 51800b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 51810b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 5182a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5183a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5184a8bfd4abSjeff.liu int ret; 5185a8bfd4abSjeff.liu 5186a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5187a8bfd4abSjeff.liu return -EPERM; 5188a8bfd4abSjeff.liu 5189a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5190a8bfd4abSjeff.liu return -EFAULT; 5191a8bfd4abSjeff.liu 5192a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 51930b246afaSJeff Mahoney btrfs_err(fs_info, 51945d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 5195a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5196a8bfd4abSjeff.liu return -EINVAL; 5197a8bfd4abSjeff.liu } 5198a8bfd4abSjeff.liu 5199a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5200a8bfd4abSjeff.liu if (ret) 5201a8bfd4abSjeff.liu return ret; 5202a8bfd4abSjeff.liu 5203a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5204a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5205a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5206a8bfd4abSjeff.liu goto out_unlock; 5207a8bfd4abSjeff.liu } 5208a8bfd4abSjeff.liu 52090b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5210a8bfd4abSjeff.liu strcpy(super_block->label, label); 52110b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 52123a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5213a8bfd4abSjeff.liu 5214a8bfd4abSjeff.liu out_unlock: 5215a8bfd4abSjeff.liu mnt_drop_write_file(file); 5216a8bfd4abSjeff.liu return ret; 5217a8bfd4abSjeff.liu } 5218a8bfd4abSjeff.liu 52192eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 52202eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 52212eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 52222eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 52232eaa055fSJeff Mahoney 5224d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 52252eaa055fSJeff Mahoney { 52264d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 52272eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 52282eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 52292eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 52302eaa055fSJeff Mahoney }; 52312eaa055fSJeff Mahoney 52322eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52332eaa055fSJeff Mahoney return -EFAULT; 52342eaa055fSJeff Mahoney 52352eaa055fSJeff Mahoney return 0; 52362eaa055fSJeff Mahoney } 52372eaa055fSJeff Mahoney 5238b929c1d8SMarcos Paulo de Souza static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, 5239b929c1d8SMarcos Paulo de Souza void __user *arg) 52402eaa055fSJeff Mahoney { 52410b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 52422eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 52432eaa055fSJeff Mahoney 52442eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 52452eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 52462eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 52472eaa055fSJeff Mahoney 52482eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52492eaa055fSJeff Mahoney return -EFAULT; 52502eaa055fSJeff Mahoney 52512eaa055fSJeff Mahoney return 0; 52522eaa055fSJeff Mahoney } 52532eaa055fSJeff Mahoney 52542ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 52553b02a68aSJeff Mahoney enum btrfs_feature_set set, 52562eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 52572eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 52582eaa055fSJeff Mahoney { 5259f10152bcSDavid Sterba const char *type = btrfs_feature_set_name(set); 52603b02a68aSJeff Mahoney char *names; 52612eaa055fSJeff Mahoney u64 disallowed, unsupported; 52622eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 52632eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 52642eaa055fSJeff Mahoney 52652eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 52662eaa055fSJeff Mahoney if (unsupported) { 52673b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 52683b02a68aSJeff Mahoney if (names) { 52690b246afaSJeff Mahoney btrfs_warn(fs_info, 52703b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 52713b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 52723b02a68aSJeff Mahoney kfree(names); 52733b02a68aSJeff Mahoney } else 52740b246afaSJeff Mahoney btrfs_warn(fs_info, 52752eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 52762eaa055fSJeff Mahoney type, unsupported); 52772eaa055fSJeff Mahoney return -EOPNOTSUPP; 52782eaa055fSJeff Mahoney } 52792eaa055fSJeff Mahoney 52802eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 52812eaa055fSJeff Mahoney if (disallowed) { 52823b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 52833b02a68aSJeff Mahoney if (names) { 52840b246afaSJeff Mahoney btrfs_warn(fs_info, 52853b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 52863b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 52873b02a68aSJeff Mahoney kfree(names); 52883b02a68aSJeff Mahoney } else 52890b246afaSJeff Mahoney btrfs_warn(fs_info, 52902eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 52912eaa055fSJeff Mahoney type, disallowed); 52922eaa055fSJeff Mahoney return -EPERM; 52932eaa055fSJeff Mahoney } 52942eaa055fSJeff Mahoney 52952eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 52962eaa055fSJeff Mahoney if (disallowed) { 52973b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 52983b02a68aSJeff Mahoney if (names) { 52990b246afaSJeff Mahoney btrfs_warn(fs_info, 53003b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 53013b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53023b02a68aSJeff Mahoney kfree(names); 53033b02a68aSJeff Mahoney } else 53040b246afaSJeff Mahoney btrfs_warn(fs_info, 53052eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 53062eaa055fSJeff Mahoney type, disallowed); 53072eaa055fSJeff Mahoney return -EPERM; 53082eaa055fSJeff Mahoney } 53092eaa055fSJeff Mahoney 53102eaa055fSJeff Mahoney return 0; 53112eaa055fSJeff Mahoney } 53122eaa055fSJeff Mahoney 53132ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 53142ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 53152eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 53162eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 53172eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 53182eaa055fSJeff Mahoney 53192eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 53202eaa055fSJeff Mahoney { 53210b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53220b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53230b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 53240b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 53252eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 53262eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 53272eaa055fSJeff Mahoney u64 newflags; 53282eaa055fSJeff Mahoney int ret; 53292eaa055fSJeff Mahoney 53302eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 53312eaa055fSJeff Mahoney return -EPERM; 53322eaa055fSJeff Mahoney 53332eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 53342eaa055fSJeff Mahoney return -EFAULT; 53352eaa055fSJeff Mahoney 53362eaa055fSJeff Mahoney /* Nothing to do */ 53372eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 53382eaa055fSJeff Mahoney !flags[0].incompat_flags) 53392eaa055fSJeff Mahoney return 0; 53402eaa055fSJeff Mahoney 53412ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 53422eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 53432eaa055fSJeff Mahoney if (ret) 53442eaa055fSJeff Mahoney return ret; 53452eaa055fSJeff Mahoney 53462ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 53472eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 53482eaa055fSJeff Mahoney if (ret) 53492eaa055fSJeff Mahoney return ret; 53502eaa055fSJeff Mahoney 53512ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 53522eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 53532eaa055fSJeff Mahoney if (ret) 53542eaa055fSJeff Mahoney return ret; 53552eaa055fSJeff Mahoney 53567ab19625SDavid Sterba ret = mnt_want_write_file(file); 53577ab19625SDavid Sterba if (ret) 53587ab19625SDavid Sterba return ret; 53597ab19625SDavid Sterba 53608051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 53617ab19625SDavid Sterba if (IS_ERR(trans)) { 53627ab19625SDavid Sterba ret = PTR_ERR(trans); 53637ab19625SDavid Sterba goto out_drop_write; 53647ab19625SDavid Sterba } 53652eaa055fSJeff Mahoney 53660b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 53672eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 53682eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 53692eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 53702eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 53712eaa055fSJeff Mahoney 53722eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 53732eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 53742eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 53752eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 53762eaa055fSJeff Mahoney 53772eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 53782eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 53792eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 53802eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 53810b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 53822eaa055fSJeff Mahoney 53833a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 53847ab19625SDavid Sterba out_drop_write: 53857ab19625SDavid Sterba mnt_drop_write_file(file); 53867ab19625SDavid Sterba 53877ab19625SDavid Sterba return ret; 53882eaa055fSJeff Mahoney } 53892eaa055fSJeff Mahoney 53902351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 53912351f431SJosef Bacik { 53922351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 53932351f431SJosef Bacik int ret; 53942351f431SJosef Bacik 53952351f431SJosef Bacik if (compat) { 53962351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 53972351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 53982351f431SJosef Bacik 53992351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 54002351f431SJosef Bacik if (ret) 54012351f431SJosef Bacik return -EFAULT; 54022351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 54032351f431SJosef Bacik if (!arg) 54042351f431SJosef Bacik return -ENOMEM; 54052351f431SJosef Bacik arg->send_fd = args32.send_fd; 54062351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 54072351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 54082351f431SJosef Bacik arg->parent_root = args32.parent_root; 54092351f431SJosef Bacik arg->flags = args32.flags; 54102351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 54112351f431SJosef Bacik sizeof(args32.reserved)); 54122351f431SJosef Bacik #else 54132351f431SJosef Bacik return -ENOTTY; 54142351f431SJosef Bacik #endif 54152351f431SJosef Bacik } else { 54162351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 54172351f431SJosef Bacik if (IS_ERR(arg)) 54182351f431SJosef Bacik return PTR_ERR(arg); 54192351f431SJosef Bacik } 54202351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 54212351f431SJosef Bacik kfree(arg); 54222351f431SJosef Bacik return ret; 54232351f431SJosef Bacik } 54242351f431SJosef Bacik 5425f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5426f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5427f46b5a66SChristoph Hellwig { 54280b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54300b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 54314bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5432f46b5a66SChristoph Hellwig 5433f46b5a66SChristoph Hellwig switch (cmd) { 54346cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 54356cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 54366cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 54376cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 54386cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 54396cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 544040cf931fSEric Sandeen case FS_IOC_GETFSLABEL: 5441b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_fslabel(fs_info, argp); 544240cf931fSEric Sandeen case FS_IOC_SETFSLABEL: 544340cf931fSEric Sandeen return btrfs_ioctl_set_fslabel(file, argp); 5444f7039b1dSLi Dongyang case FITRIM: 5445b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_fitrim(fs_info, argp); 5446f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5447fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5448fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5449fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 54503de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5451fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 54526f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 54536f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 545476dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 545576dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 54560caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 54570caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 54580caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 54590caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 54606ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 54616ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5462f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 54631e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 54641e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 54651e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5466f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5467198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5468f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 54692ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5470f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5471da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 54726b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 54736b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5474475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 54752ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5476475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 54772ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5478f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 54799ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5480ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5481ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5482cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5483cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5484ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5485ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5486d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5487d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5488d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5489d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5490d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5491d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 54921406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 54932ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 54949b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 54959b199859SFilipe David Borba Manana int ret; 54969b199859SFilipe David Borba Manana 549782b3e53bSNikolay Borisov ret = btrfs_start_delalloc_roots(fs_info, -1); 54989b199859SFilipe David Borba Manana if (ret) 54999b199859SFilipe David Borba Manana return ret; 55000b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 55012fad4e83SDavid Sterba /* 55022fad4e83SDavid Sterba * The transaction thread may want to do more work, 550301327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 55042fad4e83SDavid Sterba * processing uncleaned subvols. 55052fad4e83SDavid Sterba */ 55060b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 55079b199859SFilipe David Borba Manana return ret; 55089b199859SFilipe David Borba Manana } 550946204592SSage Weil case BTRFS_IOC_START_SYNC: 55109a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 551146204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 55122ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5513475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5514b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5515475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 55162ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5517475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 55182ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5519c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 55209ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5521837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 55222ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 552319a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 55242ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 55258ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 55268ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5527abccd00fSHugo Mills #ifdef CONFIG_64BIT 5528abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5529abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5530abccd00fSHugo Mills #endif 553131db9f7cSAlexander Block case BTRFS_IOC_SEND: 55322351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 55332351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 55342351f431SJosef Bacik case BTRFS_IOC_SEND_32: 55352351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 55362351f431SJosef Bacik #endif 5537c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 55382ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 55395d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5540905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 55415d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5542905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 55435d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5544905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 55455d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5546905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 55472f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 55482f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 55492f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 5550b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_status(fs_info, argp); 555157254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 5552b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_quota_rescan_wait(fs_info, argp); 55533f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 55542ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 55552eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5556d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 55572eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 5558b929c1d8SMarcos Paulo de Souza return btrfs_ioctl_get_features(fs_info, argp); 55592eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 55602eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5561e4202ac9SDavid Sterba case FS_IOC_FSGETXATTR: 5562e4202ac9SDavid Sterba return btrfs_ioctl_fsgetxattr(file, argp); 5563025f2121SDavid Sterba case FS_IOC_FSSETXATTR: 5564025f2121SDavid Sterba return btrfs_ioctl_fssetxattr(file, argp); 5565b64ec075STomohiro Misono case BTRFS_IOC_GET_SUBVOL_INFO: 5566b64ec075STomohiro Misono return btrfs_ioctl_get_subvol_info(file, argp); 556742e4b520STomohiro Misono case BTRFS_IOC_GET_SUBVOL_ROOTREF: 556842e4b520STomohiro Misono return btrfs_ioctl_get_subvol_rootref(file, argp); 556923d0b79dSTomohiro Misono case BTRFS_IOC_INO_LOOKUP_USER: 557023d0b79dSTomohiro Misono return btrfs_ioctl_ino_lookup_user(file, argp); 5571f46b5a66SChristoph Hellwig } 5572f46b5a66SChristoph Hellwig 5573f46b5a66SChristoph Hellwig return -ENOTTY; 5574f46b5a66SChristoph Hellwig } 55754c63c245SLuke Dashjr 55764c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 55774c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 55784c63c245SLuke Dashjr { 55792a362249SJeff Mahoney /* 55802a362249SJeff Mahoney * These all access 32-bit values anyway so no further 55812a362249SJeff Mahoney * handling is necessary. 55822a362249SJeff Mahoney */ 55834c63c245SLuke Dashjr switch (cmd) { 55844c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 55854c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 55864c63c245SLuke Dashjr break; 55874c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 55884c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 55894c63c245SLuke Dashjr break; 55904c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 55914c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 55924c63c245SLuke Dashjr break; 55934c63c245SLuke Dashjr } 55944c63c245SLuke Dashjr 55954c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 55964c63c245SLuke Dashjr } 55974c63c245SLuke Dashjr #endif 5598