1f46b5a66SChristoph Hellwig /* 2f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 3f46b5a66SChristoph Hellwig * 4f46b5a66SChristoph Hellwig * This program is free software; you can redistribute it and/or 5f46b5a66SChristoph Hellwig * modify it under the terms of the GNU General Public 6f46b5a66SChristoph Hellwig * License v2 as published by the Free Software Foundation. 7f46b5a66SChristoph Hellwig * 8f46b5a66SChristoph Hellwig * This program is distributed in the hope that it will be useful, 9f46b5a66SChristoph Hellwig * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f46b5a66SChristoph Hellwig * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11f46b5a66SChristoph Hellwig * General Public License for more details. 12f46b5a66SChristoph Hellwig * 13f46b5a66SChristoph Hellwig * You should have received a copy of the GNU General Public 14f46b5a66SChristoph Hellwig * License along with this program; if not, write to the 15f46b5a66SChristoph Hellwig * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16f46b5a66SChristoph Hellwig * Boston, MA 021110-1307, USA. 17f46b5a66SChristoph Hellwig */ 18f46b5a66SChristoph Hellwig 19f46b5a66SChristoph Hellwig #include <linux/kernel.h> 20f46b5a66SChristoph Hellwig #include <linux/bio.h> 21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h> 22f46b5a66SChristoph Hellwig #include <linux/file.h> 23f46b5a66SChristoph Hellwig #include <linux/fs.h> 24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 25f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 26f46b5a66SChristoph Hellwig #include <linux/highmem.h> 27f46b5a66SChristoph Hellwig #include <linux/time.h> 28f46b5a66SChristoph Hellwig #include <linux/init.h> 29f46b5a66SChristoph Hellwig #include <linux/string.h> 30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 31cb8e7090SChristoph Hellwig #include <linux/mount.h> 32f46b5a66SChristoph Hellwig #include <linux/mpage.h> 33cb8e7090SChristoph Hellwig #include <linux/namei.h> 34f46b5a66SChristoph Hellwig #include <linux/swap.h> 35f46b5a66SChristoph Hellwig #include <linux/writeback.h> 36f46b5a66SChristoph Hellwig #include <linux/statfs.h> 37f46b5a66SChristoph Hellwig #include <linux/compat.h> 38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h> 39cb8e7090SChristoph Hellwig #include <linux/security.h> 40f46b5a66SChristoph Hellwig #include <linux/xattr.h> 417ea394f1SYan Zheng #include <linux/vmalloc.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 43f7039b1dSLi Dongyang #include <linux/blkdev.h> 448ea05e3aSAlexander Block #include <linux/uuid.h> 4555e301fdSFilipe Brandenburger #include <linux/btrfs.h> 46416161dbSMark Fasheh #include <linux/uaccess.h> 47f46b5a66SChristoph Hellwig #include "ctree.h" 48f46b5a66SChristoph Hellwig #include "disk-io.h" 49f46b5a66SChristoph Hellwig #include "transaction.h" 50f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 51f46b5a66SChristoph Hellwig #include "print-tree.h" 52f46b5a66SChristoph Hellwig #include "volumes.h" 53925baeddSChris Mason #include "locking.h" 54581bb050SLi Zefan #include "inode-map.h" 55d7728c96SJan Schmidt #include "backref.h" 56606686eeSJosef Bacik #include "rcu-string.h" 5731db9f7cSAlexander Block #include "send.h" 583f6bcfbdSStefan Behrens #include "dev-replace.h" 5963541927SFilipe David Borba Manana #include "props.h" 603b02a68aSJeff Mahoney #include "sysfs.h" 61fcebe456SJosef Bacik #include "qgroup.h" 621ec9a1aeSFilipe Manana #include "tree-log.h" 63ebb8765bSAnand Jain #include "compression.h" 64f46b5a66SChristoph Hellwig 65abccd00fSHugo Mills #ifdef CONFIG_64BIT 66abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 67abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 68abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 69abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 70abccd00fSHugo Mills */ 71abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 72abccd00fSHugo Mills __u64 sec; 73abccd00fSHugo Mills __u32 nsec; 74abccd00fSHugo Mills } __attribute__ ((__packed__)); 75abccd00fSHugo Mills 76abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 77abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 78abccd00fSHugo Mills __u64 stransid; /* in */ 79abccd00fSHugo Mills __u64 rtransid; /* out */ 80abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 81abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 82abccd00fSHugo Mills __u64 flags; /* in */ 83abccd00fSHugo Mills __u64 reserved[16]; /* in */ 84abccd00fSHugo Mills } __attribute__ ((__packed__)); 85abccd00fSHugo Mills 86abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 87abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 88abccd00fSHugo Mills #endif 89abccd00fSHugo Mills 90abccd00fSHugo Mills 91416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 921c919a5eSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff, 931c919a5eSMark Fasheh int no_time_update); 94416161dbSMark Fasheh 956cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 966cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 976cbff00fSChristoph Hellwig { 986cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 996cbff00fSChristoph Hellwig return flags; 1006cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 1016cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 1026cbff00fSChristoph Hellwig else 1036cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1046cbff00fSChristoph Hellwig } 105f46b5a66SChristoph Hellwig 1066cbff00fSChristoph Hellwig /* 1076cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 1086cbff00fSChristoph Hellwig */ 1096cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(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 128d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 129d0092bddSLi Zefan iflags |= FS_COMPR_FL; 130d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 131d0092bddSLi Zefan iflags |= FS_NOCOMP_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 */ 1396cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1406cbff00fSChristoph Hellwig { 1416cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1423cc79392SFilipe Manana unsigned int new_fl = 0; 1436cbff00fSChristoph Hellwig 1446cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1453cc79392SFilipe Manana new_fl |= S_SYNC; 1466cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1473cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1486cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1493cc79392SFilipe Manana new_fl |= S_APPEND; 1506cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1513cc79392SFilipe Manana new_fl |= S_NOATIME; 1526cbff00fSChristoph Hellwig if (ip->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 /* 1616cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1626cbff00fSChristoph Hellwig * 163e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1646cbff00fSChristoph Hellwig */ 1656cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1666cbff00fSChristoph Hellwig { 1670b4dcea5SChris Mason unsigned int flags; 1680b4dcea5SChris Mason 1690b4dcea5SChris Mason if (!dir) 1700b4dcea5SChris Mason return; 1710b4dcea5SChris Mason 1720b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1736cbff00fSChristoph Hellwig 174e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 175e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 176e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 177e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 178e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 179e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 180e27425d6SJosef Bacik } 1816cbff00fSChristoph Hellwig 182213490b3SLiu Bo if (flags & BTRFS_INODE_NODATACOW) { 183e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 184213490b3SLiu Bo if (S_ISREG(inode->i_mode)) 185213490b3SLiu Bo BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 186213490b3SLiu Bo } 187e27425d6SJosef Bacik 1886cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1896cbff00fSChristoph Hellwig } 1906cbff00fSChristoph Hellwig 1916cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1926cbff00fSChristoph Hellwig { 193496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1946cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1956cbff00fSChristoph Hellwig 1966cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1976cbff00fSChristoph Hellwig return -EFAULT; 1986cbff00fSChristoph Hellwig return 0; 1996cbff00fSChristoph Hellwig } 2006cbff00fSChristoph Hellwig 20175e7cb7fSLiu Bo static int check_flags(unsigned int flags) 20275e7cb7fSLiu Bo { 20375e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 20475e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 20575e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 206e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 207e1e8fb6aSLi Zefan FS_NOCOW_FL)) 20875e7cb7fSLiu Bo return -EOPNOTSUPP; 20975e7cb7fSLiu Bo 21075e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 21175e7cb7fSLiu Bo return -EINVAL; 21275e7cb7fSLiu Bo 21375e7cb7fSLiu Bo return 0; 21475e7cb7fSLiu Bo } 21575e7cb7fSLiu Bo 2166cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 2176cbff00fSChristoph Hellwig { 218496ad9aaSAl Viro struct inode *inode = file_inode(file); 2196cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 2206cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 2216cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2226cbff00fSChristoph Hellwig unsigned int flags, oldflags; 2236cbff00fSChristoph Hellwig int ret; 224f062abf0SLi Zefan u64 ip_oldflags; 225f062abf0SLi Zefan unsigned int i_oldflags; 2267e97b8daSDavid Sterba umode_t mode; 2276cbff00fSChristoph Hellwig 228bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 229bd60ea0fSDavid Sterba return -EPERM; 230bd60ea0fSDavid Sterba 231b83cc969SLi Zefan if (btrfs_root_readonly(root)) 232b83cc969SLi Zefan return -EROFS; 233b83cc969SLi Zefan 2346cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 2356cbff00fSChristoph Hellwig return -EFAULT; 2366cbff00fSChristoph Hellwig 23775e7cb7fSLiu Bo ret = check_flags(flags); 23875e7cb7fSLiu Bo if (ret) 23975e7cb7fSLiu Bo return ret; 2406cbff00fSChristoph Hellwig 241e7848683SJan Kara ret = mnt_want_write_file(file); 242e7848683SJan Kara if (ret) 243e7848683SJan Kara return ret; 244e7848683SJan Kara 2455955102cSAl Viro inode_lock(inode); 2466cbff00fSChristoph Hellwig 247f062abf0SLi Zefan ip_oldflags = ip->flags; 248f062abf0SLi Zefan i_oldflags = inode->i_flags; 2497e97b8daSDavid Sterba mode = inode->i_mode; 250f062abf0SLi Zefan 2516cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2526cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2536cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2546cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2556cbff00fSChristoph Hellwig ret = -EPERM; 2566cbff00fSChristoph Hellwig goto out_unlock; 2576cbff00fSChristoph Hellwig } 2586cbff00fSChristoph Hellwig } 2596cbff00fSChristoph Hellwig 2606cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2616cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2626cbff00fSChristoph Hellwig else 2636cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2646cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2656cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2666cbff00fSChristoph Hellwig else 2676cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2686cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2696cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2706cbff00fSChristoph Hellwig else 2716cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2726cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2736cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2746cbff00fSChristoph Hellwig else 2756cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2766cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2776cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2786cbff00fSChristoph Hellwig else 2796cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2806cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2816cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2826cbff00fSChristoph Hellwig else 2836cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2847e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2857e97b8daSDavid Sterba if (S_ISREG(mode)) { 2867e97b8daSDavid Sterba /* 2877e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2887e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2897e97b8daSDavid Sterba * status of the file and will not set it. 2907e97b8daSDavid Sterba */ 2917e97b8daSDavid Sterba if (inode->i_size == 0) 2927e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2937e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2947e97b8daSDavid Sterba } else { 295e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2967e97b8daSDavid Sterba } 2977e97b8daSDavid Sterba } else { 2987e97b8daSDavid Sterba /* 2997e97b8daSDavid Sterba * Revert back under same assuptions as above 3007e97b8daSDavid Sterba */ 3017e97b8daSDavid Sterba if (S_ISREG(mode)) { 3027e97b8daSDavid Sterba if (inode->i_size == 0) 3037e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 3047e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 3057e97b8daSDavid Sterba } else { 306e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 3077e97b8daSDavid Sterba } 3087e97b8daSDavid Sterba } 3096cbff00fSChristoph Hellwig 31075e7cb7fSLiu Bo /* 31175e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 31275e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 31375e7cb7fSLiu Bo * things smaller. 31475e7cb7fSLiu Bo */ 31575e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 31675e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 31775e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 31863541927SFilipe David Borba Manana 31963541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 32063541927SFilipe David Borba Manana if (ret && ret != -ENODATA) 32163541927SFilipe David Borba Manana goto out_drop; 32275e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 32363541927SFilipe David Borba Manana const char *comp; 32463541927SFilipe David Borba Manana 32575e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 32675e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 32763541927SFilipe David Borba Manana 32863541927SFilipe David Borba Manana if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 32963541927SFilipe David Borba Manana comp = "lzo"; 33063541927SFilipe David Borba Manana else 33163541927SFilipe David Borba Manana comp = "zlib"; 33263541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", 33363541927SFilipe David Borba Manana comp, strlen(comp), 0); 33463541927SFilipe David Borba Manana if (ret) 33563541927SFilipe David Borba Manana goto out_drop; 33663541927SFilipe David Borba Manana 337ebcb904dSLi Zefan } else { 33878a017a2SFilipe Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 33978a017a2SFilipe Manana if (ret && ret != -ENODATA) 34078a017a2SFilipe Manana goto out_drop; 341ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 34275e7cb7fSLiu Bo } 3436cbff00fSChristoph Hellwig 3444da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 345f062abf0SLi Zefan if (IS_ERR(trans)) { 346f062abf0SLi Zefan ret = PTR_ERR(trans); 347f062abf0SLi Zefan goto out_drop; 348f062abf0SLi Zefan } 3496cbff00fSChristoph Hellwig 350306424ccSLi Zefan btrfs_update_iflags(inode); 3510c4d2d95SJosef Bacik inode_inc_iversion(inode); 35204b285f3SDeepa Dinamani inode->i_ctime = current_fs_time(inode->i_sb); 3536cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3546cbff00fSChristoph Hellwig 3556cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 356f062abf0SLi Zefan out_drop: 357f062abf0SLi Zefan if (ret) { 358f062abf0SLi Zefan ip->flags = ip_oldflags; 359f062abf0SLi Zefan inode->i_flags = i_oldflags; 360f062abf0SLi Zefan } 3616cbff00fSChristoph Hellwig 3626cbff00fSChristoph Hellwig out_unlock: 3635955102cSAl Viro inode_unlock(inode); 364e7848683SJan Kara mnt_drop_write_file(file); 3652d4e6f6aSliubo return ret; 3666cbff00fSChristoph Hellwig } 3676cbff00fSChristoph Hellwig 3686cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3696cbff00fSChristoph Hellwig { 370496ad9aaSAl Viro struct inode *inode = file_inode(file); 3716cbff00fSChristoph Hellwig 3726cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3736cbff00fSChristoph Hellwig } 374f46b5a66SChristoph Hellwig 375f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 376f7039b1dSLi Dongyang { 37754563d41SAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 378f7039b1dSLi Dongyang struct btrfs_device *device; 379f7039b1dSLi Dongyang struct request_queue *q; 380f7039b1dSLi Dongyang struct fstrim_range range; 381f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 382f7039b1dSLi Dongyang u64 num_devices = 0; 383815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 384f7039b1dSLi Dongyang int ret; 385f7039b1dSLi Dongyang 386f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 387f7039b1dSLi Dongyang return -EPERM; 388f7039b1dSLi Dongyang 3891f78160cSXiao Guangrong rcu_read_lock(); 3901f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3911f78160cSXiao Guangrong dev_list) { 392f7039b1dSLi Dongyang if (!device->bdev) 393f7039b1dSLi Dongyang continue; 394f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 395f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 396f7039b1dSLi Dongyang num_devices++; 397f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 398f7039b1dSLi Dongyang minlen); 399f7039b1dSLi Dongyang } 400f7039b1dSLi Dongyang } 4011f78160cSXiao Guangrong rcu_read_unlock(); 402f4c697e6SLukas Czerner 403f7039b1dSLi Dongyang if (!num_devices) 404f7039b1dSLi Dongyang return -EOPNOTSUPP; 405f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 406f7039b1dSLi Dongyang return -EFAULT; 407e515c18bSLukas Czerner if (range.start > total_bytes || 408e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 409f4c697e6SLukas Czerner return -EINVAL; 410f7039b1dSLi Dongyang 411f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 412f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 413815745cfSAl Viro ret = btrfs_trim_fs(fs_info->tree_root, &range); 414f7039b1dSLi Dongyang if (ret < 0) 415f7039b1dSLi Dongyang return ret; 416f7039b1dSLi Dongyang 417f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 418f7039b1dSLi Dongyang return -EFAULT; 419f7039b1dSLi Dongyang 420f7039b1dSLi Dongyang return 0; 421f7039b1dSLi Dongyang } 422f7039b1dSLi Dongyang 423dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 424dd5f9615SStefan Behrens { 42546e0f66aSChris Mason int i; 42646e0f66aSChris Mason 42746e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 42846e0f66aSChris Mason if (uuid[i]) 42946e0f66aSChris Mason return 0; 43046e0f66aSChris Mason } 43146e0f66aSChris Mason return 1; 432dd5f9615SStefan Behrens } 433dd5f9615SStefan Behrens 434d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 435cb8e7090SChristoph Hellwig struct dentry *dentry, 43672fd032eSSage Weil char *name, int namelen, 4376f72c7e2SArne Jansen u64 *async_transid, 4388696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 439f46b5a66SChristoph Hellwig { 440f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 441f46b5a66SChristoph Hellwig struct btrfs_key key; 442f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 443f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 444f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 445d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 44676dda93cSYan, Zheng struct btrfs_root *new_root; 447d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 44804b285f3SDeepa Dinamani struct timespec cur_time = current_fs_time(dir->i_sb); 4495662344bSTsutomu Itoh struct inode *inode; 450f46b5a66SChristoph Hellwig int ret; 451f46b5a66SChristoph Hellwig int err; 452f46b5a66SChristoph Hellwig u64 objectid; 453f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 4543de4586cSChris Mason u64 index = 0; 455d5c12070SMiao Xie u64 qgroup_reserved; 4568ea05e3aSAlexander Block uuid_le new_uuid; 457f46b5a66SChristoph Hellwig 458581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 4592fbe8c8aSAl Viro if (ret) 460a22285a6SYan, Zheng return ret; 4616a912213SJosef Bacik 462e09fe2d2SQu Wenruo /* 463e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 464e09fe2d2SQu Wenruo * screwed up since it assume subvolme qgroup's level to be 0. 465e09fe2d2SQu Wenruo */ 466e09fe2d2SQu Wenruo if (btrfs_qgroup_level(objectid)) 467e09fe2d2SQu Wenruo return -ENOSPC; 468e09fe2d2SQu Wenruo 469d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 4709ed74f2dSJosef Bacik /* 471d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 472d5c12070SMiao Xie * of create_snapshot(). 4739ed74f2dSJosef Bacik */ 474d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 475dd5f9615SStefan Behrens 8, &qgroup_reserved, false); 476d5c12070SMiao Xie if (ret) 477d5c12070SMiao Xie return ret; 478f46b5a66SChristoph Hellwig 479d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 480d5c12070SMiao Xie if (IS_ERR(trans)) { 481d5c12070SMiao Xie ret = PTR_ERR(trans); 482de6e8200SLiu Bo btrfs_subvolume_release_metadata(root, &block_rsv, 483de6e8200SLiu Bo qgroup_reserved); 484de6e8200SLiu Bo return ret; 485d5c12070SMiao Xie } 486d5c12070SMiao Xie trans->block_rsv = &block_rsv; 487d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 488f46b5a66SChristoph Hellwig 4898696c533SMiao Xie ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 4906f72c7e2SArne Jansen if (ret) 4916f72c7e2SArne Jansen goto fail; 4926f72c7e2SArne Jansen 4934d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 4948e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4958e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4968e8a1e31SJosef Bacik goto fail; 4978e8a1e31SJosef Bacik } 498f46b5a66SChristoph Hellwig 4995d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 500f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 501f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 5025d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 503f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 504f46b5a66SChristoph Hellwig 5050a4e5586SRoss Kirk write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(), 506f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 5075d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 508b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 5095d4f98a2SYan Zheng BTRFS_UUID_SIZE); 510f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 511f46b5a66SChristoph Hellwig 5128ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 5138ea05e3aSAlexander Block 514f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 5153cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 5163cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 5173cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 518707e8a07SDavid Sterba btrfs_set_stack_inode_nbytes(inode_item, root->nodesize); 5193cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 520f46b5a66SChristoph Hellwig 5213cae210fSQu Wenruo btrfs_set_root_flags(&root_item, 0); 5223cae210fSQu Wenruo btrfs_set_root_limit(&root_item, 0); 5233cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 52408fe4db1SLi Zefan 525f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 52684234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 527f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 528f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 52986b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 53080ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 531f46b5a66SChristoph Hellwig 5328ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 5338ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 5348ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 5358ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 5363cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); 5373cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); 5388ea05e3aSAlexander Block root_item.ctime = root_item.otime; 5398ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 5408ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 541f46b5a66SChristoph Hellwig 542925baeddSChris Mason btrfs_tree_unlock(leaf); 543f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 544f46b5a66SChristoph Hellwig leaf = NULL; 545f46b5a66SChristoph Hellwig 546f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 547f46b5a66SChristoph Hellwig 548f46b5a66SChristoph Hellwig key.objectid = objectid; 5495d4f98a2SYan Zheng key.offset = 0; 550962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 551f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 552f46b5a66SChristoph Hellwig &root_item); 553f46b5a66SChristoph Hellwig if (ret) 554f46b5a66SChristoph Hellwig goto fail; 555f46b5a66SChristoph Hellwig 55676dda93cSYan, Zheng key.offset = (u64)-1; 55776dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 55879787eaaSJeff Mahoney if (IS_ERR(new_root)) { 55979787eaaSJeff Mahoney ret = PTR_ERR(new_root); 5606d13f549SDavid Sterba btrfs_abort_transaction(trans, root, ret); 56179787eaaSJeff Mahoney goto fail; 56279787eaaSJeff Mahoney } 56376dda93cSYan, Zheng 56476dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 56576dda93cSYan, Zheng 56663541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 567ce598979SMark Fasheh if (ret) { 568ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 56979787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 570ce598979SMark Fasheh goto fail; 571ce598979SMark Fasheh } 572ce598979SMark Fasheh 573f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 574f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 575f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 576f32e48e9SChandan Rajendra 577f46b5a66SChristoph Hellwig /* 578f46b5a66SChristoph Hellwig * insert the directory item 579f46b5a66SChristoph Hellwig */ 5803de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 58179787eaaSJeff Mahoney if (ret) { 58279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 58379787eaaSJeff Mahoney goto fail; 58479787eaaSJeff Mahoney } 5853de4586cSChris Mason 5863de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 58716cdcec7SMiao Xie name, namelen, dir, &key, 5883de4586cSChris Mason BTRFS_FT_DIR, index); 58979787eaaSJeff Mahoney if (ret) { 59079787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 591f46b5a66SChristoph Hellwig goto fail; 59279787eaaSJeff Mahoney } 5930660b5afSChris Mason 59452c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 59552c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 59652c26179SYan Zheng BUG_ON(ret); 59752c26179SYan Zheng 5980660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5994df27c4dSYan, Zheng objectid, root->root_key.objectid, 60033345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 6010660b5afSChris Mason BUG_ON(ret); 6020660b5afSChris Mason 603dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 604dd5f9615SStefan Behrens root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 605dd5f9615SStefan Behrens objectid); 606dd5f9615SStefan Behrens if (ret) 607dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 608dd5f9615SStefan Behrens 609f46b5a66SChristoph Hellwig fail: 610d5c12070SMiao Xie trans->block_rsv = NULL; 611d5c12070SMiao Xie trans->bytes_reserved = 0; 612de6e8200SLiu Bo btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 613de6e8200SLiu Bo 61472fd032eSSage Weil if (async_transid) { 61572fd032eSSage Weil *async_transid = trans->transid; 61672fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 61700d71c9cSMiao Xie if (err) 61800d71c9cSMiao Xie err = btrfs_commit_transaction(trans, root); 61972fd032eSSage Weil } else { 62076dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 62172fd032eSSage Weil } 622f46b5a66SChristoph Hellwig if (err && !ret) 623f46b5a66SChristoph Hellwig ret = err; 6241a65e24bSChris Mason 6255662344bSTsutomu Itoh if (!ret) { 6265662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 627de6e8200SLiu Bo if (IS_ERR(inode)) 628de6e8200SLiu Bo return PTR_ERR(inode); 6295662344bSTsutomu Itoh d_instantiate(dentry, inode); 6305662344bSTsutomu Itoh } 631f46b5a66SChristoph Hellwig return ret; 632f46b5a66SChristoph Hellwig } 633f46b5a66SChristoph Hellwig 6349ea24bbeSFilipe Manana static void btrfs_wait_for_no_snapshoting_writes(struct btrfs_root *root) 6358257b2dcSMiao Xie { 6368257b2dcSMiao Xie s64 writers; 6378257b2dcSMiao Xie DEFINE_WAIT(wait); 6388257b2dcSMiao Xie 6398257b2dcSMiao Xie do { 6408257b2dcSMiao Xie prepare_to_wait(&root->subv_writers->wait, &wait, 6418257b2dcSMiao Xie TASK_UNINTERRUPTIBLE); 6428257b2dcSMiao Xie 6438257b2dcSMiao Xie writers = percpu_counter_sum(&root->subv_writers->counter); 6448257b2dcSMiao Xie if (writers) 6458257b2dcSMiao Xie schedule(); 6468257b2dcSMiao Xie 6478257b2dcSMiao Xie finish_wait(&root->subv_writers->wait, &wait); 6488257b2dcSMiao Xie } while (writers); 6498257b2dcSMiao Xie } 6508257b2dcSMiao Xie 651e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 652e9662f70SMiao Xie struct dentry *dentry, char *name, int namelen, 653e9662f70SMiao Xie u64 *async_transid, bool readonly, 654e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 655f46b5a66SChristoph Hellwig { 6562e4bfab9SYan, Zheng struct inode *inode; 657f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 658f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 6592e4bfab9SYan, Zheng int ret; 660f46b5a66SChristoph Hellwig 66127cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 662f46b5a66SChristoph Hellwig return -EINVAL; 663f46b5a66SChristoph Hellwig 664a1ee7362SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 665a1ee7362SDavid Sterba if (!pending_snapshot) 666a1ee7362SDavid Sterba return -ENOMEM; 667a1ee7362SDavid Sterba 668b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 669b0c0ea63SDavid Sterba GFP_NOFS); 6708546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 6718546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 672b0c0ea63SDavid Sterba ret = -ENOMEM; 673b0c0ea63SDavid Sterba goto free_pending; 674b0c0ea63SDavid Sterba } 675b0c0ea63SDavid Sterba 6768257b2dcSMiao Xie atomic_inc(&root->will_be_snapshoted); 6774e857c58SPeter Zijlstra smp_mb__after_atomic(); 6789ea24bbeSFilipe Manana btrfs_wait_for_no_snapshoting_writes(root); 6798257b2dcSMiao Xie 6806a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 6816a03843dSMiao Xie if (ret) 682a1ee7362SDavid Sterba goto dec_and_free; 6836a03843dSMiao Xie 684b0244199SMiao Xie btrfs_wait_ordered_extents(root, -1); 6856a03843dSMiao Xie 68666d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 68766d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 688d5c12070SMiao Xie /* 689d5c12070SMiao Xie * 1 - parent dir inode 690d5c12070SMiao Xie * 2 - dir entries 691d5c12070SMiao Xie * 1 - root item 692d5c12070SMiao Xie * 2 - root ref/backref 693d5c12070SMiao Xie * 1 - root of snapshot 694dd5f9615SStefan Behrens * 1 - UUID item 695d5c12070SMiao Xie */ 696d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 697dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 698ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 699ee3441b4SJeff Mahoney false); 700d5c12070SMiao Xie if (ret) 701a1ee7362SDavid Sterba goto dec_and_free; 702d5c12070SMiao Xie 703a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 704a22285a6SYan, Zheng pending_snapshot->root = root; 705b83cc969SLi Zefan pending_snapshot->readonly = readonly; 706e9662f70SMiao Xie pending_snapshot->dir = dir; 7078696c533SMiao Xie pending_snapshot->inherit = inherit; 708a22285a6SYan, Zheng 709d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 710a22285a6SYan, Zheng if (IS_ERR(trans)) { 711a22285a6SYan, Zheng ret = PTR_ERR(trans); 712a22285a6SYan, Zheng goto fail; 713a22285a6SYan, Zheng } 714a22285a6SYan, Zheng 7158351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 716a22285a6SYan, Zheng list_add(&pending_snapshot->list, 717a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 7188351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 71972fd032eSSage Weil if (async_transid) { 72072fd032eSSage Weil *async_transid = trans->transid; 72172fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 72272fd032eSSage Weil root->fs_info->extent_root, 1); 72300d71c9cSMiao Xie if (ret) 72400d71c9cSMiao Xie ret = btrfs_commit_transaction(trans, root); 72572fd032eSSage Weil } else { 72672fd032eSSage Weil ret = btrfs_commit_transaction(trans, 72772fd032eSSage Weil root->fs_info->extent_root); 72872fd032eSSage Weil } 729aec8030aSMiao Xie if (ret) 730c37b2b62SJosef Bacik goto fail; 731a22285a6SYan, Zheng 732a22285a6SYan, Zheng ret = pending_snapshot->error; 733f46b5a66SChristoph Hellwig if (ret) 7342e4bfab9SYan, Zheng goto fail; 735f46b5a66SChristoph Hellwig 736d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 737d3797308SChris Mason if (ret) 738d3797308SChris Mason goto fail; 739d3797308SChris Mason 7402b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 7412e4bfab9SYan, Zheng if (IS_ERR(inode)) { 7422e4bfab9SYan, Zheng ret = PTR_ERR(inode); 7432e4bfab9SYan, Zheng goto fail; 7442e4bfab9SYan, Zheng } 7455662344bSTsutomu Itoh 7462e4bfab9SYan, Zheng d_instantiate(dentry, inode); 7472e4bfab9SYan, Zheng ret = 0; 7482e4bfab9SYan, Zheng fail: 749d5c12070SMiao Xie btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, 750d5c12070SMiao Xie &pending_snapshot->block_rsv, 751d5c12070SMiao Xie pending_snapshot->qgroup_reserved); 752a1ee7362SDavid Sterba dec_and_free: 7539ea24bbeSFilipe Manana if (atomic_dec_and_test(&root->will_be_snapshoted)) 7549ea24bbeSFilipe Manana wake_up_atomic_t(&root->will_be_snapshoted); 755b0c0ea63SDavid Sterba free_pending: 756b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 7578546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 758a1ee7362SDavid Sterba kfree(pending_snapshot); 759a1ee7362SDavid Sterba 760f46b5a66SChristoph Hellwig return ret; 761f46b5a66SChristoph Hellwig } 762f46b5a66SChristoph Hellwig 7634260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 7644260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 7654260f7c7SSage Weil * whether the type of victim is right. 7664260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 7674260f7c7SSage Weil * 2. We should have write and exec permissions on dir 7684260f7c7SSage Weil * 3. We can't remove anything from append-only dir 7694260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 7704260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 7714260f7c7SSage Weil * a. be owner of dir, or 7724260f7c7SSage Weil * b. be owner of victim, or 7734260f7c7SSage Weil * c. have CAP_FOWNER capability 7744260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 7754260f7c7SSage Weil * links pointing to it. 7764260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 7774260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 7784260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 7794260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 7804260f7c7SSage Weil * nfs_async_unlink(). 7814260f7c7SSage Weil */ 7824260f7c7SSage Weil 7834260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 7844260f7c7SSage Weil { 7854260f7c7SSage Weil int error; 7864260f7c7SSage Weil 7872b0143b5SDavid Howells if (d_really_is_negative(victim)) 7884260f7c7SSage Weil return -ENOENT; 7894260f7c7SSage Weil 7902b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 7914fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 7924260f7c7SSage Weil 7934260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 7944260f7c7SSage Weil if (error) 7954260f7c7SSage Weil return error; 7964260f7c7SSage Weil if (IS_APPEND(dir)) 7974260f7c7SSage Weil return -EPERM; 7982b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 7992b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 8004260f7c7SSage Weil return -EPERM; 8014260f7c7SSage Weil if (isdir) { 802e36cb0b8SDavid Howells if (!d_is_dir(victim)) 8034260f7c7SSage Weil return -ENOTDIR; 8044260f7c7SSage Weil if (IS_ROOT(victim)) 8054260f7c7SSage Weil return -EBUSY; 806e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 8074260f7c7SSage Weil return -EISDIR; 8084260f7c7SSage Weil if (IS_DEADDIR(dir)) 8094260f7c7SSage Weil return -ENOENT; 8104260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 8114260f7c7SSage Weil return -EBUSY; 8124260f7c7SSage Weil return 0; 8134260f7c7SSage Weil } 8144260f7c7SSage Weil 815cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 816cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 817cb8e7090SChristoph Hellwig { 8182b0143b5SDavid Howells if (d_really_is_positive(child)) 819cb8e7090SChristoph Hellwig return -EEXIST; 820cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 821cb8e7090SChristoph Hellwig return -ENOENT; 822cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 823cb8e7090SChristoph Hellwig } 824cb8e7090SChristoph Hellwig 825cb8e7090SChristoph Hellwig /* 826cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 827cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 828cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 829cb8e7090SChristoph Hellwig */ 83076dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 83176dda93cSYan, Zheng char *name, int namelen, 83272fd032eSSage Weil struct btrfs_root *snap_src, 8336f72c7e2SArne Jansen u64 *async_transid, bool readonly, 8348696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 835cb8e7090SChristoph Hellwig { 8362b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 837cb8e7090SChristoph Hellwig struct dentry *dentry; 838cb8e7090SChristoph Hellwig int error; 839cb8e7090SChristoph Hellwig 8405c50c9b8SDavid Sterba error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 8415c50c9b8SDavid Sterba if (error == -EINTR) 8425c50c9b8SDavid Sterba return error; 843cb8e7090SChristoph Hellwig 844cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 845cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 846cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 847cb8e7090SChristoph Hellwig goto out_unlock; 848cb8e7090SChristoph Hellwig 84976dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 850cb8e7090SChristoph Hellwig if (error) 851a874a63eSLiu Bo goto out_dput; 852cb8e7090SChristoph Hellwig 8539c52057cSChris Mason /* 8549c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 8559c52057cSChris Mason * check for them now when we can safely fail 8569c52057cSChris Mason */ 8579c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 8589c52057cSChris Mason dir->i_ino, name, 8599c52057cSChris Mason namelen); 8609c52057cSChris Mason if (error) 8619c52057cSChris Mason goto out_dput; 8629c52057cSChris Mason 86376dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 86476dda93cSYan, Zheng 86576dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 86676dda93cSYan, Zheng goto out_up_read; 86776dda93cSYan, Zheng 8683de4586cSChris Mason if (snap_src) { 869e9662f70SMiao Xie error = create_snapshot(snap_src, dir, dentry, name, namelen, 8706f72c7e2SArne Jansen async_transid, readonly, inherit); 8713de4586cSChris Mason } else { 872d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 873d5c12070SMiao Xie async_transid, inherit); 8743de4586cSChris Mason } 87576dda93cSYan, Zheng if (!error) 87676dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 87776dda93cSYan, Zheng out_up_read: 87876dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 879cb8e7090SChristoph Hellwig out_dput: 880cb8e7090SChristoph Hellwig dput(dentry); 881cb8e7090SChristoph Hellwig out_unlock: 8825955102cSAl Viro inode_unlock(dir); 883cb8e7090SChristoph Hellwig return error; 884cb8e7090SChristoph Hellwig } 885cb8e7090SChristoph Hellwig 8864cb5300bSChris Mason /* 8874cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 8884cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 8894cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 8904cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 8914cb5300bSChris Mason * part of the file 8924cb5300bSChris Mason */ 893aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 8944cb5300bSChris Mason { 8954cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8964cb5300bSChris Mason struct extent_map *em = NULL; 8974cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 8984cb5300bSChris Mason u64 end; 8994cb5300bSChris Mason 9004cb5300bSChris Mason read_lock(&em_tree->lock); 90109cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 9024cb5300bSChris Mason read_unlock(&em_tree->lock); 9034cb5300bSChris Mason 9044cb5300bSChris Mason if (em) { 9054cb5300bSChris Mason end = extent_map_end(em); 9064cb5300bSChris Mason free_extent_map(em); 9074cb5300bSChris Mason if (end - offset > thresh) 9084cb5300bSChris Mason return 0; 9094cb5300bSChris Mason } 9104cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 9114cb5300bSChris Mason thresh /= 2; 9124cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 9134cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 9144cb5300bSChris Mason if (end >= thresh) 9154cb5300bSChris Mason return 0; 9164cb5300bSChris Mason return 1; 9174cb5300bSChris Mason } 9184cb5300bSChris Mason 9194cb5300bSChris Mason /* 9204cb5300bSChris Mason * helper function to walk through a file and find extents 9214cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 9224cb5300bSChris Mason * 9234cb5300bSChris Mason * This is used by the defragging code to find new and small 9244cb5300bSChris Mason * extents 9254cb5300bSChris Mason */ 9264cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 9274cb5300bSChris Mason struct inode *inode, u64 newer_than, 928aab110abSDavid Sterba u64 *off, u32 thresh) 9294cb5300bSChris Mason { 9304cb5300bSChris Mason struct btrfs_path *path; 9314cb5300bSChris Mason struct btrfs_key min_key; 9324cb5300bSChris Mason struct extent_buffer *leaf; 9334cb5300bSChris Mason struct btrfs_file_extent_item *extent; 9344cb5300bSChris Mason int type; 9354cb5300bSChris Mason int ret; 936a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 9374cb5300bSChris Mason 9384cb5300bSChris Mason path = btrfs_alloc_path(); 9394cb5300bSChris Mason if (!path) 9404cb5300bSChris Mason return -ENOMEM; 9414cb5300bSChris Mason 942a4689d2bSDavid Sterba min_key.objectid = ino; 9434cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 9444cb5300bSChris Mason min_key.offset = *off; 9454cb5300bSChris Mason 9464cb5300bSChris Mason while (1) { 9476174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 9484cb5300bSChris Mason if (ret != 0) 9494cb5300bSChris Mason goto none; 950f094c9bdSFilipe Manana process_slot: 951a4689d2bSDavid Sterba if (min_key.objectid != ino) 9524cb5300bSChris Mason goto none; 9534cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 9544cb5300bSChris Mason goto none; 9554cb5300bSChris Mason 9564cb5300bSChris Mason leaf = path->nodes[0]; 9574cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 9584cb5300bSChris Mason struct btrfs_file_extent_item); 9594cb5300bSChris Mason 9604cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 9614cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 9624cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 9634cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 9644cb5300bSChris Mason *off = min_key.offset; 9654cb5300bSChris Mason btrfs_free_path(path); 9664cb5300bSChris Mason return 0; 9674cb5300bSChris Mason } 9684cb5300bSChris Mason 969f094c9bdSFilipe Manana path->slots[0]++; 970f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 971f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 972f094c9bdSFilipe Manana goto process_slot; 973f094c9bdSFilipe Manana } 974f094c9bdSFilipe Manana 9754cb5300bSChris Mason if (min_key.offset == (u64)-1) 9764cb5300bSChris Mason goto none; 9774cb5300bSChris Mason 9784cb5300bSChris Mason min_key.offset++; 9794cb5300bSChris Mason btrfs_release_path(path); 9804cb5300bSChris Mason } 9814cb5300bSChris Mason none: 9824cb5300bSChris Mason btrfs_free_path(path); 9834cb5300bSChris Mason return -ENOENT; 9844cb5300bSChris Mason } 9854cb5300bSChris Mason 9866c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 98717ce6ef8SLiu Bo { 98817ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 989940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 9906c282eb4SLi Zefan struct extent_map *em; 99109cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 992940100a4SChris Mason 993940100a4SChris Mason /* 994940100a4SChris Mason * hopefully we have this extent in the tree already, try without 995940100a4SChris Mason * the full extent lock 996940100a4SChris Mason */ 997940100a4SChris Mason read_lock(&em_tree->lock); 998940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 999940100a4SChris Mason read_unlock(&em_tree->lock); 1000940100a4SChris Mason 1001940100a4SChris Mason if (!em) { 1002308d9800SFilipe Manana struct extent_state *cached = NULL; 1003308d9800SFilipe Manana u64 end = start + len - 1; 1004308d9800SFilipe Manana 1005940100a4SChris Mason /* get the big lock and read metadata off disk */ 1006ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 1007940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 1008308d9800SFilipe Manana unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS); 1009940100a4SChris Mason 10106cf8bfbfSDan Carpenter if (IS_ERR(em)) 10116c282eb4SLi Zefan return NULL; 1012940100a4SChris Mason } 1013940100a4SChris Mason 10146c282eb4SLi Zefan return em; 10156c282eb4SLi Zefan } 10166c282eb4SLi Zefan 10176c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 10186c282eb4SLi Zefan { 10196c282eb4SLi Zefan struct extent_map *next; 10206c282eb4SLi Zefan bool ret = true; 10216c282eb4SLi Zefan 10226c282eb4SLi Zefan /* this is the last extent */ 10236c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 10246c282eb4SLi Zefan return false; 10256c282eb4SLi Zefan 10266c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1027e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1028e9512d72SChris Mason ret = false; 1029e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1030ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 10316c282eb4SLi Zefan ret = false; 10326c282eb4SLi Zefan 10336c282eb4SLi Zefan free_extent_map(next); 10346c282eb4SLi Zefan return ret; 10356c282eb4SLi Zefan } 10366c282eb4SLi Zefan 1037aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1038a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1039a43a2111SAndrew Mahone int compress) 10406c282eb4SLi Zefan { 10416c282eb4SLi Zefan struct extent_map *em; 10426c282eb4SLi Zefan int ret = 1; 10436c282eb4SLi Zefan bool next_mergeable = true; 10444a3560c4SLiu Bo bool prev_mergeable = true; 10456c282eb4SLi Zefan 10466c282eb4SLi Zefan /* 10476c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 10486c282eb4SLi Zefan * defragging it 10496c282eb4SLi Zefan */ 10506c282eb4SLi Zefan if (start < *defrag_end) 10516c282eb4SLi Zefan return 1; 10526c282eb4SLi Zefan 10536c282eb4SLi Zefan *skip = 0; 10546c282eb4SLi Zefan 10556c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 10566c282eb4SLi Zefan if (!em) 10576c282eb4SLi Zefan return 0; 10586c282eb4SLi Zefan 1059940100a4SChris Mason /* this will cover holes, and inline extents */ 106017ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1061940100a4SChris Mason ret = 0; 106217ce6ef8SLiu Bo goto out; 106317ce6ef8SLiu Bo } 106417ce6ef8SLiu Bo 10654a3560c4SLiu Bo if (!*defrag_end) 10664a3560c4SLiu Bo prev_mergeable = false; 10674a3560c4SLiu Bo 10686c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1069940100a4SChris Mason /* 10706c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 10716c282eb4SLi Zefan * real extent, don't bother defragging it 1072940100a4SChris Mason */ 1073a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 10744a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1075940100a4SChris Mason ret = 0; 107617ce6ef8SLiu Bo out: 1077940100a4SChris Mason /* 1078940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1079940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1080940100a4SChris Mason * so that the next tiny extent will force a defrag. 1081940100a4SChris Mason * 1082940100a4SChris Mason * The end result of this is that tiny extents before a single big 1083940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1084940100a4SChris Mason */ 1085940100a4SChris Mason if (ret) { 1086940100a4SChris Mason *defrag_end = extent_map_end(em); 1087940100a4SChris Mason } else { 1088940100a4SChris Mason *last_len = 0; 1089940100a4SChris Mason *skip = extent_map_end(em); 1090940100a4SChris Mason *defrag_end = 0; 1091940100a4SChris Mason } 1092940100a4SChris Mason 1093940100a4SChris Mason free_extent_map(em); 1094940100a4SChris Mason return ret; 1095940100a4SChris Mason } 1096940100a4SChris Mason 10974cb5300bSChris Mason /* 10984cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 10994cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 11004cb5300bSChris Mason * to COW and defrag. 11014cb5300bSChris Mason * 11024cb5300bSChris Mason * It also makes sure the delalloc code has enough 11034cb5300bSChris Mason * dirty data to avoid making new small extents as part 11044cb5300bSChris Mason * of the defrag 11054cb5300bSChris Mason * 11064cb5300bSChris Mason * It's a good idea to start RA on this range 11074cb5300bSChris Mason * before calling this. 11084cb5300bSChris Mason */ 11094cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 11104cb5300bSChris Mason struct page **pages, 11114cb5300bSChris Mason unsigned long start_index, 1112c41570c9SJustin Maggard unsigned long num_pages) 1113f46b5a66SChristoph Hellwig { 11144cb5300bSChris Mason unsigned long file_end; 11154cb5300bSChris Mason u64 isize = i_size_read(inode); 1116f46b5a66SChristoph Hellwig u64 page_start; 1117f46b5a66SChristoph Hellwig u64 page_end; 11181f12bd06SLiu Bo u64 page_cnt; 11194cb5300bSChris Mason int ret; 11204cb5300bSChris Mason int i; 11214cb5300bSChris Mason int i_done; 11224cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 11234cb5300bSChris Mason struct extent_state *cached_state = NULL; 1124600a45e1SMiao Xie struct extent_io_tree *tree; 11253b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 11264cb5300bSChris Mason 112709cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 11281f12bd06SLiu Bo if (!isize || start_index > file_end) 11291f12bd06SLiu Bo return 0; 11301f12bd06SLiu Bo 11311f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 11324cb5300bSChris Mason 11334cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 113409cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 113509cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 11364cb5300bSChris Mason if (ret) 11374cb5300bSChris Mason return ret; 11384cb5300bSChris Mason i_done = 0; 1139600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 11404cb5300bSChris Mason 11414cb5300bSChris Mason /* step one, lock all the pages */ 11421f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 11434cb5300bSChris Mason struct page *page; 1144600a45e1SMiao Xie again: 1145a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 11463b16a4e3SJosef Bacik start_index + i, mask); 11474cb5300bSChris Mason if (!page) 11484cb5300bSChris Mason break; 11494cb5300bSChris Mason 1150600a45e1SMiao Xie page_start = page_offset(page); 115109cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1152600a45e1SMiao Xie while (1) { 1153308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1154ff13db41SDavid Sterba &cached_state); 1155600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1156600a45e1SMiao Xie page_start); 1157308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1158308d9800SFilipe Manana &cached_state, GFP_NOFS); 1159600a45e1SMiao Xie if (!ordered) 1160600a45e1SMiao Xie break; 1161600a45e1SMiao Xie 1162600a45e1SMiao Xie unlock_page(page); 1163600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1164600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1165600a45e1SMiao Xie lock_page(page); 11661f12bd06SLiu Bo /* 11671f12bd06SLiu Bo * we unlocked the page above, so we need check if 11681f12bd06SLiu Bo * it was released or not. 11691f12bd06SLiu Bo */ 11701f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 11711f12bd06SLiu Bo unlock_page(page); 117209cbfeafSKirill A. Shutemov put_page(page); 11731f12bd06SLiu Bo goto again; 11741f12bd06SLiu Bo } 1175600a45e1SMiao Xie } 1176600a45e1SMiao Xie 11774cb5300bSChris Mason if (!PageUptodate(page)) { 11784cb5300bSChris Mason btrfs_readpage(NULL, page); 11794cb5300bSChris Mason lock_page(page); 11804cb5300bSChris Mason if (!PageUptodate(page)) { 11814cb5300bSChris Mason unlock_page(page); 118209cbfeafSKirill A. Shutemov put_page(page); 11834cb5300bSChris Mason ret = -EIO; 11844cb5300bSChris Mason break; 11854cb5300bSChris Mason } 11864cb5300bSChris Mason } 1187600a45e1SMiao Xie 1188600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1189600a45e1SMiao Xie unlock_page(page); 119009cbfeafSKirill A. Shutemov put_page(page); 1191600a45e1SMiao Xie goto again; 1192600a45e1SMiao Xie } 1193600a45e1SMiao Xie 11944cb5300bSChris Mason pages[i] = page; 11954cb5300bSChris Mason i_done++; 11964cb5300bSChris Mason } 11974cb5300bSChris Mason if (!i_done || ret) 11984cb5300bSChris Mason goto out; 11994cb5300bSChris Mason 12004cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 12014cb5300bSChris Mason goto out; 12024cb5300bSChris Mason 12034cb5300bSChris Mason /* 12044cb5300bSChris Mason * so now we have a nice long stream of locked 12054cb5300bSChris Mason * and up to date pages, lets wait on them 12064cb5300bSChris Mason */ 12074cb5300bSChris Mason for (i = 0; i < i_done; i++) 12084cb5300bSChris Mason wait_on_page_writeback(pages[i]); 12094cb5300bSChris Mason 12104cb5300bSChris Mason page_start = page_offset(pages[0]); 121109cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 12124cb5300bSChris Mason 12134cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1214ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 12154cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 12164cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 12179e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 12189e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 12194cb5300bSChris Mason 12201f12bd06SLiu Bo if (i_done != page_cnt) { 12219e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 12229e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 12239e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 12244cb5300bSChris Mason btrfs_delalloc_release_space(inode, 122509cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 122609cbfeafSKirill A. Shutemov (page_cnt - i_done) << PAGE_SHIFT); 12274cb5300bSChris Mason } 12284cb5300bSChris Mason 12294cb5300bSChris Mason 12309e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 12319e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 12324cb5300bSChris Mason 12334cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 12344cb5300bSChris Mason page_start, page_end - 1, &cached_state, 12354cb5300bSChris Mason GFP_NOFS); 12364cb5300bSChris Mason 12374cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12384cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 12394cb5300bSChris Mason ClearPageChecked(pages[i]); 12404cb5300bSChris Mason set_page_extent_mapped(pages[i]); 12414cb5300bSChris Mason set_page_dirty(pages[i]); 12424cb5300bSChris Mason unlock_page(pages[i]); 124309cbfeafSKirill A. Shutemov put_page(pages[i]); 12444cb5300bSChris Mason } 12454cb5300bSChris Mason return i_done; 12464cb5300bSChris Mason out: 12474cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12484cb5300bSChris Mason unlock_page(pages[i]); 124909cbfeafSKirill A. Shutemov put_page(pages[i]); 12504cb5300bSChris Mason } 12517cf5b976SQu Wenruo btrfs_delalloc_release_space(inode, 125209cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 125309cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12544cb5300bSChris Mason return ret; 12554cb5300bSChris Mason 12564cb5300bSChris Mason } 12574cb5300bSChris Mason 12584cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 12594cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 12604cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 12614cb5300bSChris Mason { 12624cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 12634cb5300bSChris Mason struct file_ra_state *ra = NULL; 12644cb5300bSChris Mason unsigned long last_index; 1265151a31b2SLi Zefan u64 isize = i_size_read(inode); 1266940100a4SChris Mason u64 last_len = 0; 1267940100a4SChris Mason u64 skip = 0; 1268940100a4SChris Mason u64 defrag_end = 0; 12694cb5300bSChris Mason u64 newer_off = range->start; 1270f46b5a66SChristoph Hellwig unsigned long i; 1271008873eaSLi Zefan unsigned long ra_index = 0; 1272f46b5a66SChristoph Hellwig int ret; 12734cb5300bSChris Mason int defrag_count = 0; 12741a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1275aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 127609cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1277c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1278ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 12794cb5300bSChris Mason struct page **pages = NULL; 12804cb5300bSChris Mason 12810abd5b17SLiu Bo if (isize == 0) 12820abd5b17SLiu Bo return 0; 12830abd5b17SLiu Bo 12840abd5b17SLiu Bo if (range->start >= isize) 12850abd5b17SLiu Bo return -EINVAL; 12861a419d85SLi Zefan 12871a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 12881a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 12891a419d85SLi Zefan return -EINVAL; 12901a419d85SLi Zefan if (range->compress_type) 12911a419d85SLi Zefan compress_type = range->compress_type; 12921a419d85SLi Zefan } 1293f46b5a66SChristoph Hellwig 12940abd5b17SLiu Bo if (extent_thresh == 0) 1295ee22184bSByongho Lee extent_thresh = SZ_256K; 1296f46b5a66SChristoph Hellwig 12974cb5300bSChris Mason /* 12984cb5300bSChris Mason * if we were not given a file, allocate a readahead 12994cb5300bSChris Mason * context 13004cb5300bSChris Mason */ 13014cb5300bSChris Mason if (!file) { 13024cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 13034cb5300bSChris Mason if (!ra) 13044cb5300bSChris Mason return -ENOMEM; 13054cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 13064cb5300bSChris Mason } else { 13074cb5300bSChris Mason ra = &file->f_ra; 13084cb5300bSChris Mason } 13094cb5300bSChris Mason 1310d9b0d9baSDulshani Gunawardhana pages = kmalloc_array(max_cluster, sizeof(struct page *), 13114cb5300bSChris Mason GFP_NOFS); 13124cb5300bSChris Mason if (!pages) { 13134cb5300bSChris Mason ret = -ENOMEM; 13144cb5300bSChris Mason goto out_ra; 13154cb5300bSChris Mason } 13164cb5300bSChris Mason 13174cb5300bSChris Mason /* find the last page to defrag */ 13181e701a32SChris Mason if (range->start + range->len > range->start) { 1319151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 132009cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 13211e701a32SChris Mason } else { 132209cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 13231e701a32SChris Mason } 13241e701a32SChris Mason 13254cb5300bSChris Mason if (newer_than) { 13264cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1327ee22184bSByongho Lee &newer_off, SZ_64K); 13284cb5300bSChris Mason if (!ret) { 13294cb5300bSChris Mason range->start = newer_off; 13304cb5300bSChris Mason /* 13314cb5300bSChris Mason * we always align our defrag to help keep 13324cb5300bSChris Mason * the extents in the file evenly spaced 13334cb5300bSChris Mason */ 133409cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 13354cb5300bSChris Mason } else 13364cb5300bSChris Mason goto out_ra; 13374cb5300bSChris Mason } else { 133809cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 13394cb5300bSChris Mason } 13404cb5300bSChris Mason if (!max_to_defrag) 1341070034bdSchandan max_to_defrag = last_index - i + 1; 13424cb5300bSChris Mason 13432a0f7f57SLi Zefan /* 13442a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 13452a0f7f57SLi Zefan * written sequentially. 13462a0f7f57SLi Zefan */ 13472a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 13482a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 13492a0f7f57SLi Zefan 1350f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 135109cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 13524cb5300bSChris Mason /* 13534cb5300bSChris Mason * make sure we stop running if someone unmounts 13544cb5300bSChris Mason * the FS 13554cb5300bSChris Mason */ 13564cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 13574cb5300bSChris Mason break; 13584cb5300bSChris Mason 1359210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1360f14d104dSDavid Sterba btrfs_debug(root->fs_info, "defrag_file cancelled"); 1361210549ebSDavid Sterba ret = -EAGAIN; 1362210549ebSDavid Sterba break; 1363210549ebSDavid Sterba } 1364210549ebSDavid Sterba 136509cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 13666c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1367a43a2111SAndrew Mahone &defrag_end, range->flags & 1368a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1369940100a4SChris Mason unsigned long next; 1370940100a4SChris Mason /* 1371940100a4SChris Mason * the should_defrag function tells us how much to skip 1372940100a4SChris Mason * bump our counter by the suggested amount 1373940100a4SChris Mason */ 137409cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1375940100a4SChris Mason i = max(i + 1, next); 1376940100a4SChris Mason continue; 1377940100a4SChris Mason } 1378008873eaSLi Zefan 1379008873eaSLi Zefan if (!newer_than) { 138009cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 138109cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1382008873eaSLi Zefan cluster = min(cluster, max_cluster); 1383008873eaSLi Zefan } else { 1384008873eaSLi Zefan cluster = max_cluster; 1385008873eaSLi Zefan } 1386008873eaSLi Zefan 1387008873eaSLi Zefan if (i + cluster > ra_index) { 1388008873eaSLi Zefan ra_index = max(i, ra_index); 1389008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1390008873eaSLi Zefan cluster); 1391e4826a5bSchandan ra_index += cluster; 1392008873eaSLi Zefan } 13934cb5300bSChris Mason 13945955102cSAl Viro inode_lock(inode); 1395633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 1396633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = compress_type; 1397008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1398ecb8bea8SLiu Bo if (ret < 0) { 13995955102cSAl Viro inode_unlock(inode); 14004cb5300bSChris Mason goto out_ra; 1401ecb8bea8SLiu Bo } 14024cb5300bSChris Mason 14034cb5300bSChris Mason defrag_count += ret; 1404d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 14055955102cSAl Viro inode_unlock(inode); 14064cb5300bSChris Mason 14074cb5300bSChris Mason if (newer_than) { 14084cb5300bSChris Mason if (newer_off == (u64)-1) 14094cb5300bSChris Mason break; 14104cb5300bSChris Mason 1411e1f041e1SLiu Bo if (ret > 0) 1412e1f041e1SLiu Bo i += ret; 1413e1f041e1SLiu Bo 14144cb5300bSChris Mason newer_off = max(newer_off + 1, 141509cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 14164cb5300bSChris Mason 1417ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1418ee22184bSByongho Lee &newer_off, SZ_64K); 14194cb5300bSChris Mason if (!ret) { 14204cb5300bSChris Mason range->start = newer_off; 142109cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14224cb5300bSChris Mason } else { 14234cb5300bSChris Mason break; 1424940100a4SChris Mason } 14254cb5300bSChris Mason } else { 1426008873eaSLi Zefan if (ret > 0) { 1427cbcc8326SLi Zefan i += ret; 142809cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1429008873eaSLi Zefan } else { 1430940100a4SChris Mason i++; 1431008873eaSLi Zefan last_len = 0; 1432008873eaSLi Zefan } 1433f46b5a66SChristoph Hellwig } 14344cb5300bSChris Mason } 1435f46b5a66SChristoph Hellwig 1436dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 14371e701a32SChris Mason filemap_flush(inode->i_mapping); 1438dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1439dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1440dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1441dec8ef90SFilipe Manana } 14421e701a32SChris Mason 14431e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 14441e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 14451e701a32SChris Mason * we have to make sure the IO is actually started and that 14461e701a32SChris Mason * ordered extents get created before we return 14471e701a32SChris Mason */ 14481e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 14491e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 14501e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 14511e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 14521e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 14531e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 14541e701a32SChris Mason } 14551e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 14561e701a32SChris Mason } 14571e701a32SChris Mason 14581a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 14592b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 14601a419d85SLi Zefan } 14611a419d85SLi Zefan 146260ccf82fSDiego Calleja ret = defrag_count; 1463940100a4SChris Mason 14644cb5300bSChris Mason out_ra: 1465633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 14665955102cSAl Viro inode_lock(inode); 1467633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 14685955102cSAl Viro inode_unlock(inode); 1469633085c7SFilipe David Borba Manana } 14704cb5300bSChris Mason if (!file) 14714cb5300bSChris Mason kfree(ra); 14724cb5300bSChris Mason kfree(pages); 1473940100a4SChris Mason return ret; 1474f46b5a66SChristoph Hellwig } 1475f46b5a66SChristoph Hellwig 1476198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 147776dda93cSYan, Zheng void __user *arg) 1478f46b5a66SChristoph Hellwig { 1479f46b5a66SChristoph Hellwig u64 new_size; 1480f46b5a66SChristoph Hellwig u64 old_size; 1481f46b5a66SChristoph Hellwig u64 devid = 1; 1482496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 1483f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1484f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1485f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1486f46b5a66SChristoph Hellwig char *sizestr; 14879a40f122SGui Hecheng char *retptr; 1488f46b5a66SChristoph Hellwig char *devstr = NULL; 1489f46b5a66SChristoph Hellwig int ret = 0; 1490f46b5a66SChristoph Hellwig int mod = 0; 1491f46b5a66SChristoph Hellwig 1492e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1493e441d54dSChris Mason return -EPERM; 1494e441d54dSChris Mason 1495198605a8SMiao Xie ret = mnt_want_write_file(file); 1496198605a8SMiao Xie if (ret) 1497198605a8SMiao Xie return ret; 1498198605a8SMiao Xie 14995ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 15005ac00addSStefan Behrens 1)) { 150197547676SMiao Xie mnt_drop_write_file(file); 1502e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1503c9e9f97bSIlya Dryomov } 1504c9e9f97bSIlya Dryomov 15055ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1506dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1507c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1508c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1509c9e9f97bSIlya Dryomov goto out; 1510c9e9f97bSIlya Dryomov } 15115516e595SMark Fasheh 15125516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1513f46b5a66SChristoph Hellwig 1514f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1515f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1516f46b5a66SChristoph Hellwig if (devstr) { 1517f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1518f46b5a66SChristoph Hellwig *devstr = '\0'; 1519f46b5a66SChristoph Hellwig devstr = vol_args->name; 152058dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 152158dfae63SZhangZhen if (ret) 152258dfae63SZhangZhen goto out_free; 1523dfd79829SMiao Xie if (!devid) { 1524dfd79829SMiao Xie ret = -EINVAL; 1525dfd79829SMiao Xie goto out_free; 1526dfd79829SMiao Xie } 1527efe120a0SFrank Holton btrfs_info(root->fs_info, "resizing devid %llu", devid); 1528f46b5a66SChristoph Hellwig } 1529dba60f3fSMiao Xie 1530aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1531f46b5a66SChristoph Hellwig if (!device) { 1532efe120a0SFrank Holton btrfs_info(root->fs_info, "resizer unable to find device %llu", 1533c1c9ff7cSGeert Uytterhoeven devid); 1534dfd79829SMiao Xie ret = -ENODEV; 1535c9e9f97bSIlya Dryomov goto out_free; 1536f46b5a66SChristoph Hellwig } 1537dba60f3fSMiao Xie 1538dba60f3fSMiao Xie if (!device->writeable) { 1539efe120a0SFrank Holton btrfs_info(root->fs_info, 1540efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1541c1c9ff7cSGeert Uytterhoeven devid); 1542dfd79829SMiao Xie ret = -EPERM; 15434e42ae1bSLiu Bo goto out_free; 15444e42ae1bSLiu Bo } 15454e42ae1bSLiu Bo 1546f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1547f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1548f46b5a66SChristoph Hellwig else { 1549f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1550f46b5a66SChristoph Hellwig mod = -1; 1551f46b5a66SChristoph Hellwig sizestr++; 1552f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1553f46b5a66SChristoph Hellwig mod = 1; 1554f46b5a66SChristoph Hellwig sizestr++; 1555f46b5a66SChristoph Hellwig } 15569a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 15579a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1558f46b5a66SChristoph Hellwig ret = -EINVAL; 1559c9e9f97bSIlya Dryomov goto out_free; 1560f46b5a66SChristoph Hellwig } 1561f46b5a66SChristoph Hellwig } 1562f46b5a66SChristoph Hellwig 156363a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1564dfd79829SMiao Xie ret = -EPERM; 156563a212abSStefan Behrens goto out_free; 156663a212abSStefan Behrens } 156763a212abSStefan Behrens 15687cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1569f46b5a66SChristoph Hellwig 1570f46b5a66SChristoph Hellwig if (mod < 0) { 1571f46b5a66SChristoph Hellwig if (new_size > old_size) { 1572f46b5a66SChristoph Hellwig ret = -EINVAL; 1573c9e9f97bSIlya Dryomov goto out_free; 1574f46b5a66SChristoph Hellwig } 1575f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1576f46b5a66SChristoph Hellwig } else if (mod > 0) { 1577eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1578902c68a4SGui Hecheng ret = -ERANGE; 1579eb8052e0SWenliang Fan goto out_free; 1580eb8052e0SWenliang Fan } 1581f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1582f46b5a66SChristoph Hellwig } 1583f46b5a66SChristoph Hellwig 1584ee22184bSByongho Lee if (new_size < SZ_256M) { 1585f46b5a66SChristoph Hellwig ret = -EINVAL; 1586c9e9f97bSIlya Dryomov goto out_free; 1587f46b5a66SChristoph Hellwig } 1588f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1589f46b5a66SChristoph Hellwig ret = -EFBIG; 1590c9e9f97bSIlya Dryomov goto out_free; 1591f46b5a66SChristoph Hellwig } 1592f46b5a66SChristoph Hellwig 1593b8b93addSDavid Sterba new_size = div_u64(new_size, root->sectorsize); 1594f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1595f46b5a66SChristoph Hellwig 1596ecaeb14bSDavid Sterba btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu", 1597c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1598f46b5a66SChristoph Hellwig 1599f46b5a66SChristoph Hellwig if (new_size > old_size) { 1600a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 160198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 160298d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1603c9e9f97bSIlya Dryomov goto out_free; 160498d5dc13STsutomu Itoh } 1605f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1606f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1607ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1608f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 16090253f40eSjeff.liu } /* equal, nothing need to do */ 1610f46b5a66SChristoph Hellwig 1611c9e9f97bSIlya Dryomov out_free: 1612f46b5a66SChristoph Hellwig kfree(vol_args); 1613c9e9f97bSIlya Dryomov out: 1614c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 16155ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 161618f39c41SIlya Dryomov mnt_drop_write_file(file); 1617f46b5a66SChristoph Hellwig return ret; 1618f46b5a66SChristoph Hellwig } 1619f46b5a66SChristoph Hellwig 162072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 16216f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 16226f72c7e2SArne Jansen u64 *transid, bool readonly, 16238696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1624f46b5a66SChristoph Hellwig { 1625f46b5a66SChristoph Hellwig int namelen; 16263de4586cSChris Mason int ret = 0; 1627f46b5a66SChristoph Hellwig 1628a874a63eSLiu Bo ret = mnt_want_write_file(file); 1629a874a63eSLiu Bo if (ret) 1630a874a63eSLiu Bo goto out; 1631a874a63eSLiu Bo 163272fd032eSSage Weil namelen = strlen(name); 163372fd032eSSage Weil if (strchr(name, '/')) { 1634f46b5a66SChristoph Hellwig ret = -EINVAL; 1635a874a63eSLiu Bo goto out_drop_write; 1636f46b5a66SChristoph Hellwig } 1637f46b5a66SChristoph Hellwig 163816780cabSChris Mason if (name[0] == '.' && 163916780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 164016780cabSChris Mason ret = -EEXIST; 1641a874a63eSLiu Bo goto out_drop_write; 164216780cabSChris Mason } 164316780cabSChris Mason 16443de4586cSChris Mason if (subvol) { 164572fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 16466f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1647cb8e7090SChristoph Hellwig } else { 16482903ff01SAl Viro struct fd src = fdget(fd); 16493de4586cSChris Mason struct inode *src_inode; 16502903ff01SAl Viro if (!src.file) { 16513de4586cSChris Mason ret = -EINVAL; 1652a874a63eSLiu Bo goto out_drop_write; 16533de4586cSChris Mason } 16543de4586cSChris Mason 1655496ad9aaSAl Viro src_inode = file_inode(src.file); 1656496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1657c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1658efe120a0SFrank Holton "Snapshot src from another FS"); 165923ad5b17SKusanagi Kouichi ret = -EXDEV; 1660d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1661d0242061SDavid Sterba /* 1662d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1663d0242061SDavid Sterba * are limited to own subvolumes only 1664d0242061SDavid Sterba */ 1665d0242061SDavid Sterba ret = -EPERM; 1666ecd18815SAl Viro } else { 166772fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 166872fd032eSSage Weil BTRFS_I(src_inode)->root, 16696f72c7e2SArne Jansen transid, readonly, inherit); 1670ecd18815SAl Viro } 16712903ff01SAl Viro fdput(src); 1672cb8e7090SChristoph Hellwig } 1673a874a63eSLiu Bo out_drop_write: 1674a874a63eSLiu Bo mnt_drop_write_file(file); 1675f46b5a66SChristoph Hellwig out: 167672fd032eSSage Weil return ret; 167772fd032eSSage Weil } 167872fd032eSSage Weil 167972fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1680fa0d2b9bSLi Zefan void __user *arg, int subvol) 168172fd032eSSage Weil { 1682fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 168372fd032eSSage Weil int ret; 168472fd032eSSage Weil 1685fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1686fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1687fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1688fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1689fa0d2b9bSLi Zefan 1690fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1691b83cc969SLi Zefan vol_args->fd, subvol, 16926f72c7e2SArne Jansen NULL, false, NULL); 1693fa0d2b9bSLi Zefan 1694fa0d2b9bSLi Zefan kfree(vol_args); 1695fa0d2b9bSLi Zefan return ret; 1696fa0d2b9bSLi Zefan } 1697fa0d2b9bSLi Zefan 1698fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1699fa0d2b9bSLi Zefan void __user *arg, int subvol) 1700fa0d2b9bSLi Zefan { 1701fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1702fa0d2b9bSLi Zefan int ret; 1703fdfb1e4fSLi Zefan u64 transid = 0; 1704fdfb1e4fSLi Zefan u64 *ptr = NULL; 1705b83cc969SLi Zefan bool readonly = false; 17066f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 170772fd032eSSage Weil 1708fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1709fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1710fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1711fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1712fdfb1e4fSLi Zefan 1713b83cc969SLi Zefan if (vol_args->flags & 17146f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 17156f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1716b83cc969SLi Zefan ret = -EOPNOTSUPP; 1717c47ca32dSDan Carpenter goto free_args; 1718fdfb1e4fSLi Zefan } 1719fdfb1e4fSLi Zefan 1720fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1721fdfb1e4fSLi Zefan ptr = &transid; 1722b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1723b83cc969SLi Zefan readonly = true; 17246f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 172509cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 17266f72c7e2SArne Jansen ret = -EINVAL; 1727c47ca32dSDan Carpenter goto free_args; 17286f72c7e2SArne Jansen } 17296f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 17306f72c7e2SArne Jansen if (IS_ERR(inherit)) { 17316f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1732c47ca32dSDan Carpenter goto free_args; 17336f72c7e2SArne Jansen } 17346f72c7e2SArne Jansen } 173575eaa0e2SSage Weil 1736fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 17376f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 17388696c533SMiao Xie readonly, inherit); 1739c47ca32dSDan Carpenter if (ret) 1740c47ca32dSDan Carpenter goto free_inherit; 174175eaa0e2SSage Weil 1742c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1743fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1744c47ca32dSDan Carpenter transid), 1745c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 174675eaa0e2SSage Weil ret = -EFAULT; 1747c47ca32dSDan Carpenter 1748c47ca32dSDan Carpenter free_inherit: 17496f72c7e2SArne Jansen kfree(inherit); 1750c47ca32dSDan Carpenter free_args: 1751c47ca32dSDan Carpenter kfree(vol_args); 1752f46b5a66SChristoph Hellwig return ret; 1753f46b5a66SChristoph Hellwig } 1754f46b5a66SChristoph Hellwig 17550caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 17560caa102dSLi Zefan void __user *arg) 17570caa102dSLi Zefan { 1758496ad9aaSAl Viro struct inode *inode = file_inode(file); 17590caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17600caa102dSLi Zefan int ret = 0; 17610caa102dSLi Zefan u64 flags = 0; 17620caa102dSLi Zefan 176333345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 17640caa102dSLi Zefan return -EINVAL; 17650caa102dSLi Zefan 17660caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 17670caa102dSLi Zefan if (btrfs_root_readonly(root)) 17680caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 17690caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 17700caa102dSLi Zefan 17710caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 17720caa102dSLi Zefan ret = -EFAULT; 17730caa102dSLi Zefan 17740caa102dSLi Zefan return ret; 17750caa102dSLi Zefan } 17760caa102dSLi Zefan 17770caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 17780caa102dSLi Zefan void __user *arg) 17790caa102dSLi Zefan { 1780496ad9aaSAl Viro struct inode *inode = file_inode(file); 17810caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17820caa102dSLi Zefan struct btrfs_trans_handle *trans; 17830caa102dSLi Zefan u64 root_flags; 17840caa102dSLi Zefan u64 flags; 17850caa102dSLi Zefan int ret = 0; 17860caa102dSLi Zefan 1787bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1788bd60ea0fSDavid Sterba return -EPERM; 1789bd60ea0fSDavid Sterba 1790b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1791b9ca0664SLiu Bo if (ret) 1792b9ca0664SLiu Bo goto out; 17930caa102dSLi Zefan 1794b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1795b9ca0664SLiu Bo ret = -EINVAL; 1796b9ca0664SLiu Bo goto out_drop_write; 1797b9ca0664SLiu Bo } 17980caa102dSLi Zefan 1799b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1800b9ca0664SLiu Bo ret = -EFAULT; 1801b9ca0664SLiu Bo goto out_drop_write; 1802b9ca0664SLiu Bo } 18030caa102dSLi Zefan 1804b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1805b9ca0664SLiu Bo ret = -EINVAL; 1806b9ca0664SLiu Bo goto out_drop_write; 1807b9ca0664SLiu Bo } 18080caa102dSLi Zefan 1809b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1810b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1811b9ca0664SLiu Bo goto out_drop_write; 1812b9ca0664SLiu Bo } 18130caa102dSLi Zefan 18140caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 18150caa102dSLi Zefan 18160caa102dSLi Zefan /* nothing to do */ 18170caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1818b9ca0664SLiu Bo goto out_drop_sem; 18190caa102dSLi Zefan 18200caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 18212c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 18220caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18230caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 18242c686537SDavid Sterba } else { 18252c686537SDavid Sterba /* 18262c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 18272c686537SDavid Sterba * send 18282c686537SDavid Sterba */ 18292c686537SDavid Sterba spin_lock(&root->root_item_lock); 18302c686537SDavid Sterba if (root->send_in_progress == 0) { 18310caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18320caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 18332c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18342c686537SDavid Sterba } else { 18352c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18362c686537SDavid Sterba btrfs_warn(root->fs_info, 18372c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 18382c686537SDavid Sterba root->root_key.objectid); 18392c686537SDavid Sterba ret = -EPERM; 18402c686537SDavid Sterba goto out_drop_sem; 18412c686537SDavid Sterba } 18422c686537SDavid Sterba } 18430caa102dSLi Zefan 18440caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 18450caa102dSLi Zefan if (IS_ERR(trans)) { 18460caa102dSLi Zefan ret = PTR_ERR(trans); 18470caa102dSLi Zefan goto out_reset; 18480caa102dSLi Zefan } 18490caa102dSLi Zefan 1850b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 18510caa102dSLi Zefan &root->root_key, &root->root_item); 18520caa102dSLi Zefan 18530caa102dSLi Zefan btrfs_commit_transaction(trans, root); 18540caa102dSLi Zefan out_reset: 18550caa102dSLi Zefan if (ret) 18560caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1857b9ca0664SLiu Bo out_drop_sem: 18580caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1859b9ca0664SLiu Bo out_drop_write: 1860b9ca0664SLiu Bo mnt_drop_write_file(file); 1861b9ca0664SLiu Bo out: 18620caa102dSLi Zefan return ret; 18630caa102dSLi Zefan } 18640caa102dSLi Zefan 186576dda93cSYan, Zheng /* 186676dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 186776dda93cSYan, Zheng */ 186876dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 186976dda93cSYan, Zheng { 187076dda93cSYan, Zheng struct btrfs_path *path; 1871175a2b87SJosef Bacik struct btrfs_dir_item *di; 187276dda93cSYan, Zheng struct btrfs_key key; 1873175a2b87SJosef Bacik u64 dir_id; 187476dda93cSYan, Zheng int ret; 187576dda93cSYan, Zheng 187676dda93cSYan, Zheng path = btrfs_alloc_path(); 187776dda93cSYan, Zheng if (!path) 187876dda93cSYan, Zheng return -ENOMEM; 187976dda93cSYan, Zheng 1880175a2b87SJosef Bacik /* Make sure this root isn't set as the default subvol */ 1881175a2b87SJosef Bacik dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 1882175a2b87SJosef Bacik di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, 1883175a2b87SJosef Bacik dir_id, "default", 7, 0); 1884175a2b87SJosef Bacik if (di && !IS_ERR(di)) { 1885175a2b87SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1886175a2b87SJosef Bacik if (key.objectid == root->root_key.objectid) { 188772de6b53SGuangyu Sun ret = -EPERM; 188872de6b53SGuangyu Sun btrfs_err(root->fs_info, "deleting default subvolume " 188972de6b53SGuangyu Sun "%llu is not allowed", key.objectid); 1890175a2b87SJosef Bacik goto out; 1891175a2b87SJosef Bacik } 1892175a2b87SJosef Bacik btrfs_release_path(path); 1893175a2b87SJosef Bacik } 1894175a2b87SJosef Bacik 189576dda93cSYan, Zheng key.objectid = root->root_key.objectid; 189676dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 189776dda93cSYan, Zheng key.offset = (u64)-1; 189876dda93cSYan, Zheng 189976dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 190076dda93cSYan, Zheng &key, path, 0, 0); 190176dda93cSYan, Zheng if (ret < 0) 190276dda93cSYan, Zheng goto out; 190376dda93cSYan, Zheng BUG_ON(ret == 0); 190476dda93cSYan, Zheng 190576dda93cSYan, Zheng ret = 0; 190676dda93cSYan, Zheng if (path->slots[0] > 0) { 190776dda93cSYan, Zheng path->slots[0]--; 190876dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 190976dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 191076dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 191176dda93cSYan, Zheng ret = -ENOTEMPTY; 191276dda93cSYan, Zheng } 191376dda93cSYan, Zheng out: 191476dda93cSYan, Zheng btrfs_free_path(path); 191576dda93cSYan, Zheng return ret; 191676dda93cSYan, Zheng } 191776dda93cSYan, Zheng 1918ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1919ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1920ac8e9819SChris Mason { 1921abc6e134SChris Mason struct btrfs_key test; 1922abc6e134SChris Mason int ret; 1923abc6e134SChris Mason 1924abc6e134SChris Mason test.objectid = sk->min_objectid; 1925abc6e134SChris Mason test.type = sk->min_type; 1926abc6e134SChris Mason test.offset = sk->min_offset; 1927abc6e134SChris Mason 1928abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1929abc6e134SChris Mason if (ret < 0) 1930ac8e9819SChris Mason return 0; 1931abc6e134SChris Mason 1932abc6e134SChris Mason test.objectid = sk->max_objectid; 1933abc6e134SChris Mason test.type = sk->max_type; 1934abc6e134SChris Mason test.offset = sk->max_offset; 1935abc6e134SChris Mason 1936abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1937abc6e134SChris Mason if (ret > 0) 1938ac8e9819SChris Mason return 0; 1939ac8e9819SChris Mason return 1; 1940ac8e9819SChris Mason } 1941ac8e9819SChris Mason 1942ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1943ac8e9819SChris Mason struct btrfs_path *path, 1944ac8e9819SChris Mason struct btrfs_key *key, 1945ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 19469b6e817dSGerhard Heift size_t *buf_size, 1947ba346b35SGerhard Heift char __user *ubuf, 1948ac8e9819SChris Mason unsigned long *sk_offset, 1949ac8e9819SChris Mason int *num_found) 1950ac8e9819SChris Mason { 1951ac8e9819SChris Mason u64 found_transid; 1952ac8e9819SChris Mason struct extent_buffer *leaf; 1953ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1954dd81d459SNaohiro Aota struct btrfs_key test; 1955ac8e9819SChris Mason unsigned long item_off; 1956ac8e9819SChris Mason unsigned long item_len; 1957ac8e9819SChris Mason int nritems; 1958ac8e9819SChris Mason int i; 1959ac8e9819SChris Mason int slot; 1960ac8e9819SChris Mason int ret = 0; 1961ac8e9819SChris Mason 1962ac8e9819SChris Mason leaf = path->nodes[0]; 1963ac8e9819SChris Mason slot = path->slots[0]; 1964ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1965ac8e9819SChris Mason 1966ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1967ac8e9819SChris Mason i = nritems; 1968ac8e9819SChris Mason goto advance_key; 1969ac8e9819SChris Mason } 1970ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1971ac8e9819SChris Mason 1972ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1973ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1974ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1975ac8e9819SChris Mason 197603b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 197703b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 197803b71c6cSGabriel de Perthuis continue; 197903b71c6cSGabriel de Perthuis 19809b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 19818f5f6178SGerhard Heift if (*num_found) { 1982ac8e9819SChris Mason ret = 1; 19838f5f6178SGerhard Heift goto out; 19848f5f6178SGerhard Heift } 19858f5f6178SGerhard Heift 19868f5f6178SGerhard Heift /* 19878f5f6178SGerhard Heift * return one empty item back for v1, which does not 19888f5f6178SGerhard Heift * handle -EOVERFLOW 19898f5f6178SGerhard Heift */ 19908f5f6178SGerhard Heift 19919b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 1992ac8e9819SChris Mason item_len = 0; 19938f5f6178SGerhard Heift ret = -EOVERFLOW; 19948f5f6178SGerhard Heift } 1995ac8e9819SChris Mason 19969b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 1997ac8e9819SChris Mason ret = 1; 199825c9bc2eSGerhard Heift goto out; 1999ac8e9819SChris Mason } 2000ac8e9819SChris Mason 2001ac8e9819SChris Mason sh.objectid = key->objectid; 2002ac8e9819SChris Mason sh.offset = key->offset; 2003ac8e9819SChris Mason sh.type = key->type; 2004ac8e9819SChris Mason sh.len = item_len; 2005ac8e9819SChris Mason sh.transid = found_transid; 2006ac8e9819SChris Mason 2007ac8e9819SChris Mason /* copy search result header */ 2008ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 2009ba346b35SGerhard Heift ret = -EFAULT; 2010ba346b35SGerhard Heift goto out; 2011ba346b35SGerhard Heift } 2012ba346b35SGerhard Heift 2013ac8e9819SChris Mason *sk_offset += sizeof(sh); 2014ac8e9819SChris Mason 2015ac8e9819SChris Mason if (item_len) { 2016ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2017ac8e9819SChris Mason /* copy the item */ 2018ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2019ba346b35SGerhard Heift item_off, item_len)) { 2020ba346b35SGerhard Heift ret = -EFAULT; 2021ba346b35SGerhard Heift goto out; 2022ba346b35SGerhard Heift } 2023ba346b35SGerhard Heift 2024ac8e9819SChris Mason *sk_offset += item_len; 2025ac8e9819SChris Mason } 2026e2156867SHugo Mills (*num_found)++; 2027ac8e9819SChris Mason 20288f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 20298f5f6178SGerhard Heift goto out; 20308f5f6178SGerhard Heift 203125c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 203225c9bc2eSGerhard Heift ret = 1; 203325c9bc2eSGerhard Heift goto out; 203425c9bc2eSGerhard Heift } 2035ac8e9819SChris Mason } 2036ac8e9819SChris Mason advance_key: 2037ac8e9819SChris Mason ret = 0; 2038dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2039dd81d459SNaohiro Aota test.type = sk->max_type; 2040dd81d459SNaohiro Aota test.offset = sk->max_offset; 2041dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2042dd81d459SNaohiro Aota ret = 1; 2043dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2044abc6e134SChris Mason key->offset++; 2045dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2046abc6e134SChris Mason key->offset = 0; 2047abc6e134SChris Mason key->type++; 2048dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2049abc6e134SChris Mason key->offset = 0; 2050abc6e134SChris Mason key->type = 0; 2051abc6e134SChris Mason key->objectid++; 2052abc6e134SChris Mason } else 2053abc6e134SChris Mason ret = 1; 205425c9bc2eSGerhard Heift out: 2055ba346b35SGerhard Heift /* 2056ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2057ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2058ba346b35SGerhard Heift * * all items were found 2059ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2060ba346b35SGerhard Heift * leaf 2061ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2062ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2063ba346b35SGerhard Heift */ 2064ac8e9819SChris Mason return ret; 2065ac8e9819SChris Mason } 2066ac8e9819SChris Mason 2067ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 206812544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 20699b6e817dSGerhard Heift size_t *buf_size, 2070ba346b35SGerhard Heift char __user *ubuf) 2071ac8e9819SChris Mason { 2072ac8e9819SChris Mason struct btrfs_root *root; 2073ac8e9819SChris Mason struct btrfs_key key; 2074ac8e9819SChris Mason struct btrfs_path *path; 2075ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 2076ac8e9819SChris Mason int ret; 2077ac8e9819SChris Mason int num_found = 0; 2078ac8e9819SChris Mason unsigned long sk_offset = 0; 2079ac8e9819SChris Mason 20809b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 20819b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 208212544442SGerhard Heift return -EOVERFLOW; 20839b6e817dSGerhard Heift } 208412544442SGerhard Heift 2085ac8e9819SChris Mason path = btrfs_alloc_path(); 2086ac8e9819SChris Mason if (!path) 2087ac8e9819SChris Mason return -ENOMEM; 2088ac8e9819SChris Mason 2089ac8e9819SChris Mason if (sk->tree_id == 0) { 2090ac8e9819SChris Mason /* search the root of the inode that was passed */ 2091ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2092ac8e9819SChris Mason } else { 2093ac8e9819SChris Mason key.objectid = sk->tree_id; 2094ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2095ac8e9819SChris Mason key.offset = (u64)-1; 2096ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2097ac8e9819SChris Mason if (IS_ERR(root)) { 2098ac8e9819SChris Mason btrfs_free_path(path); 2099ac8e9819SChris Mason return -ENOENT; 2100ac8e9819SChris Mason } 2101ac8e9819SChris Mason } 2102ac8e9819SChris Mason 2103ac8e9819SChris Mason key.objectid = sk->min_objectid; 2104ac8e9819SChris Mason key.type = sk->min_type; 2105ac8e9819SChris Mason key.offset = sk->min_offset; 2106ac8e9819SChris Mason 2107ac8e9819SChris Mason while (1) { 21086174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2109ac8e9819SChris Mason if (ret != 0) { 2110ac8e9819SChris Mason if (ret > 0) 2111ac8e9819SChris Mason ret = 0; 2112ac8e9819SChris Mason goto err; 2113ac8e9819SChris Mason } 2114ba346b35SGerhard Heift ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf, 2115ac8e9819SChris Mason &sk_offset, &num_found); 2116b3b4aa74SDavid Sterba btrfs_release_path(path); 211725c9bc2eSGerhard Heift if (ret) 2118ac8e9819SChris Mason break; 2119ac8e9819SChris Mason 2120ac8e9819SChris Mason } 21218f5f6178SGerhard Heift if (ret > 0) 2122ac8e9819SChris Mason ret = 0; 2123ac8e9819SChris Mason err: 2124ac8e9819SChris Mason sk->nr_items = num_found; 2125ac8e9819SChris Mason btrfs_free_path(path); 2126ac8e9819SChris Mason return ret; 2127ac8e9819SChris Mason } 2128ac8e9819SChris Mason 2129ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2130ac8e9819SChris Mason void __user *argp) 2131ac8e9819SChris Mason { 2132ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2133ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2134ac8e9819SChris Mason struct inode *inode; 2135ac8e9819SChris Mason int ret; 21369b6e817dSGerhard Heift size_t buf_size; 2137ac8e9819SChris Mason 2138ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2139ac8e9819SChris Mason return -EPERM; 2140ac8e9819SChris Mason 2141ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2142ac8e9819SChris Mason 2143ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2144ba346b35SGerhard Heift return -EFAULT; 2145ba346b35SGerhard Heift 2146ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2147ac8e9819SChris Mason 2148496ad9aaSAl Viro inode = file_inode(file); 2149ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 21508f5f6178SGerhard Heift 21518f5f6178SGerhard Heift /* 21528f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 21538f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 21548f5f6178SGerhard Heift */ 21558f5f6178SGerhard Heift if (ret == -EOVERFLOW) 21568f5f6178SGerhard Heift ret = 0; 21578f5f6178SGerhard Heift 2158ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2159ac8e9819SChris Mason ret = -EFAULT; 2160ac8e9819SChris Mason return ret; 2161ac8e9819SChris Mason } 2162ac8e9819SChris Mason 2163cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2164cc68a8a5SGerhard Heift void __user *argp) 2165cc68a8a5SGerhard Heift { 2166cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2167cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2168cc68a8a5SGerhard Heift struct inode *inode; 2169cc68a8a5SGerhard Heift int ret; 2170cc68a8a5SGerhard Heift size_t buf_size; 2171ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2172cc68a8a5SGerhard Heift 2173cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2174cc68a8a5SGerhard Heift return -EPERM; 2175cc68a8a5SGerhard Heift 2176cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2177cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2178cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2179cc68a8a5SGerhard Heift return -EFAULT; 2180cc68a8a5SGerhard Heift 2181cc68a8a5SGerhard Heift buf_size = args.buf_size; 2182cc68a8a5SGerhard Heift 2183cc68a8a5SGerhard Heift if (buf_size < sizeof(struct btrfs_ioctl_search_header)) 2184cc68a8a5SGerhard Heift return -EOVERFLOW; 2185cc68a8a5SGerhard Heift 2186cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2187cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2188cc68a8a5SGerhard Heift buf_size = buf_limit; 2189cc68a8a5SGerhard Heift 2190cc68a8a5SGerhard Heift inode = file_inode(file); 2191cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2192cc68a8a5SGerhard Heift (char *)(&uarg->buf[0])); 2193cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2194cc68a8a5SGerhard Heift ret = -EFAULT; 2195cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2196cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2197cc68a8a5SGerhard Heift ret = -EFAULT; 2198cc68a8a5SGerhard Heift 219976dda93cSYan, Zheng return ret; 220076dda93cSYan, Zheng } 220176dda93cSYan, Zheng 220298d377a0STARUISI Hiroaki /* 2203ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2204ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 220598d377a0STARUISI Hiroaki */ 220698d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 220798d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 220898d377a0STARUISI Hiroaki { 220998d377a0STARUISI Hiroaki struct btrfs_root *root; 221098d377a0STARUISI Hiroaki struct btrfs_key key; 2211ac8e9819SChris Mason char *ptr; 221298d377a0STARUISI Hiroaki int ret = -1; 221398d377a0STARUISI Hiroaki int slot; 221498d377a0STARUISI Hiroaki int len; 221598d377a0STARUISI Hiroaki int total_len = 0; 221698d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 221798d377a0STARUISI Hiroaki struct extent_buffer *l; 221898d377a0STARUISI Hiroaki struct btrfs_path *path; 221998d377a0STARUISI Hiroaki 222098d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 222198d377a0STARUISI Hiroaki name[0]='\0'; 222298d377a0STARUISI Hiroaki return 0; 222398d377a0STARUISI Hiroaki } 222498d377a0STARUISI Hiroaki 222598d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 222698d377a0STARUISI Hiroaki if (!path) 222798d377a0STARUISI Hiroaki return -ENOMEM; 222898d377a0STARUISI Hiroaki 2229ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 223098d377a0STARUISI Hiroaki 223198d377a0STARUISI Hiroaki key.objectid = tree_id; 223298d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 223398d377a0STARUISI Hiroaki key.offset = (u64)-1; 223498d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 223598d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2236f14d104dSDavid Sterba btrfs_err(info, "could not find root %llu", tree_id); 22378ad6fcabSChris Mason ret = -ENOENT; 22388ad6fcabSChris Mason goto out; 223998d377a0STARUISI Hiroaki } 224098d377a0STARUISI Hiroaki 224198d377a0STARUISI Hiroaki key.objectid = dirid; 224298d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 22438ad6fcabSChris Mason key.offset = (u64)-1; 224498d377a0STARUISI Hiroaki 224598d377a0STARUISI Hiroaki while (1) { 224698d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 224798d377a0STARUISI Hiroaki if (ret < 0) 224898d377a0STARUISI Hiroaki goto out; 224918674c6cSFilipe David Borba Manana else if (ret > 0) { 225018674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 225118674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 225218674c6cSFilipe David Borba Manana if (ret < 0) 225318674c6cSFilipe David Borba Manana goto out; 225418674c6cSFilipe David Borba Manana else if (ret > 0) { 2255ac8e9819SChris Mason ret = -ENOENT; 225698d377a0STARUISI Hiroaki goto out; 2257ac8e9819SChris Mason } 225818674c6cSFilipe David Borba Manana } 225918674c6cSFilipe David Borba Manana 226018674c6cSFilipe David Borba Manana l = path->nodes[0]; 226118674c6cSFilipe David Borba Manana slot = path->slots[0]; 226218674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 226398d377a0STARUISI Hiroaki 226498d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 226598d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 226698d377a0STARUISI Hiroaki ptr -= len + 1; 226798d377a0STARUISI Hiroaki total_len += len + 1; 2268a696cf35SFilipe David Borba Manana if (ptr < name) { 2269a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 227098d377a0STARUISI Hiroaki goto out; 2271a696cf35SFilipe David Borba Manana } 227298d377a0STARUISI Hiroaki 227398d377a0STARUISI Hiroaki *(ptr + len) = '/'; 227498d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 227598d377a0STARUISI Hiroaki 227698d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 227798d377a0STARUISI Hiroaki break; 227898d377a0STARUISI Hiroaki 2279b3b4aa74SDavid Sterba btrfs_release_path(path); 228098d377a0STARUISI Hiroaki key.objectid = key.offset; 22818ad6fcabSChris Mason key.offset = (u64)-1; 228298d377a0STARUISI Hiroaki dirid = key.objectid; 228398d377a0STARUISI Hiroaki } 228477906a50SLi Zefan memmove(name, ptr, total_len); 228598d377a0STARUISI Hiroaki name[total_len] = '\0'; 228698d377a0STARUISI Hiroaki ret = 0; 228798d377a0STARUISI Hiroaki out: 228898d377a0STARUISI Hiroaki btrfs_free_path(path); 2289ac8e9819SChris Mason return ret; 2290ac8e9819SChris Mason } 2291ac8e9819SChris Mason 2292ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2293ac8e9819SChris Mason void __user *argp) 2294ac8e9819SChris Mason { 2295ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2296ac8e9819SChris Mason struct inode *inode; 229701b810b8SDavid Sterba int ret = 0; 2298ac8e9819SChris Mason 22992354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 23002354d08fSJulia Lawall if (IS_ERR(args)) 23012354d08fSJulia Lawall return PTR_ERR(args); 2302c2b96929SDan Carpenter 2303496ad9aaSAl Viro inode = file_inode(file); 2304ac8e9819SChris Mason 230501b810b8SDavid Sterba /* 230601b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 230701b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 230801b810b8SDavid Sterba */ 23091b53ac4dSChris Mason if (args->treeid == 0) 23101b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 23111b53ac4dSChris Mason 231201b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 231301b810b8SDavid Sterba args->name[0] = 0; 231401b810b8SDavid Sterba goto out; 231501b810b8SDavid Sterba } 231601b810b8SDavid Sterba 231701b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 231801b810b8SDavid Sterba ret = -EPERM; 231901b810b8SDavid Sterba goto out; 232001b810b8SDavid Sterba } 232101b810b8SDavid Sterba 2322ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2323ac8e9819SChris Mason args->treeid, args->objectid, 2324ac8e9819SChris Mason args->name); 2325ac8e9819SChris Mason 232601b810b8SDavid Sterba out: 2327ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2328ac8e9819SChris Mason ret = -EFAULT; 2329ac8e9819SChris Mason 2330ac8e9819SChris Mason kfree(args); 233198d377a0STARUISI Hiroaki return ret; 233298d377a0STARUISI Hiroaki } 233398d377a0STARUISI Hiroaki 233476dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 233576dda93cSYan, Zheng void __user *arg) 233676dda93cSYan, Zheng { 233754563d41SAl Viro struct dentry *parent = file->f_path.dentry; 233876dda93cSYan, Zheng struct dentry *dentry; 23392b0143b5SDavid Howells struct inode *dir = d_inode(parent); 234076dda93cSYan, Zheng struct inode *inode; 234176dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 234276dda93cSYan, Zheng struct btrfs_root *dest = NULL; 234376dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 234476dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2345c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2346521e0546SDavid Sterba u64 root_flags; 2347c58aaad2SMiao Xie u64 qgroup_reserved; 234876dda93cSYan, Zheng int namelen; 234976dda93cSYan, Zheng int ret; 235076dda93cSYan, Zheng int err = 0; 235176dda93cSYan, Zheng 235276dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 235376dda93cSYan, Zheng if (IS_ERR(vol_args)) 235476dda93cSYan, Zheng return PTR_ERR(vol_args); 235576dda93cSYan, Zheng 235676dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 235776dda93cSYan, Zheng namelen = strlen(vol_args->name); 235876dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 235976dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 236076dda93cSYan, Zheng err = -EINVAL; 236176dda93cSYan, Zheng goto out; 236276dda93cSYan, Zheng } 236376dda93cSYan, Zheng 2364a561be71SAl Viro err = mnt_want_write_file(file); 236576dda93cSYan, Zheng if (err) 236676dda93cSYan, Zheng goto out; 236776dda93cSYan, Zheng 2368521e0546SDavid Sterba 23695c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 23705c50c9b8SDavid Sterba if (err == -EINTR) 2371e43f998eSDavid Sterba goto out_drop_write; 237276dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 237376dda93cSYan, Zheng if (IS_ERR(dentry)) { 237476dda93cSYan, Zheng err = PTR_ERR(dentry); 237576dda93cSYan, Zheng goto out_unlock_dir; 237676dda93cSYan, Zheng } 237776dda93cSYan, Zheng 23782b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 237976dda93cSYan, Zheng err = -ENOENT; 238076dda93cSYan, Zheng goto out_dput; 238176dda93cSYan, Zheng } 238276dda93cSYan, Zheng 23832b0143b5SDavid Howells inode = d_inode(dentry); 23844260f7c7SSage Weil dest = BTRFS_I(inode)->root; 23854260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 23864260f7c7SSage Weil /* 23874260f7c7SSage Weil * Regular user. Only allow this with a special mount 23884260f7c7SSage Weil * option, when the user has write+exec access to the 23894260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23904260f7c7SSage Weil * allowed. 23914260f7c7SSage Weil * 23924260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23934260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23944260f7c7SSage Weil * otherwise be able to delete. 23954260f7c7SSage Weil * 23964260f7c7SSage Weil * Users who want to delete empty subvols should try 23974260f7c7SSage Weil * rmdir(2). 23984260f7c7SSage Weil */ 23994260f7c7SSage Weil err = -EPERM; 24004260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 24014260f7c7SSage Weil goto out_dput; 24024260f7c7SSage Weil 24034260f7c7SSage Weil /* 24044260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 24054260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 24064260f7c7SSage Weil * must be called on the dentry referencing the root 24074260f7c7SSage Weil * of the subvol, not a random directory contained 24084260f7c7SSage Weil * within it. 24094260f7c7SSage Weil */ 24104260f7c7SSage Weil err = -EINVAL; 24114260f7c7SSage Weil if (root == dest) 24124260f7c7SSage Weil goto out_dput; 24134260f7c7SSage Weil 24144260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 24154260f7c7SSage Weil if (err) 24164260f7c7SSage Weil goto out_dput; 24175c39da5bSMiao Xie } 24184260f7c7SSage Weil 24195c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 24204260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 24214260f7c7SSage Weil if (err) 24224260f7c7SSage Weil goto out_dput; 24234260f7c7SSage Weil 242433345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 242576dda93cSYan, Zheng err = -EINVAL; 242676dda93cSYan, Zheng goto out_dput; 242776dda93cSYan, Zheng } 242876dda93cSYan, Zheng 24295955102cSAl Viro inode_lock(inode); 2430521e0546SDavid Sterba 2431521e0546SDavid Sterba /* 2432521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2433521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2434521e0546SDavid Sterba * again is not run concurrently. 2435521e0546SDavid Sterba */ 2436521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2437c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2438c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2439c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2440521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2441521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2442521e0546SDavid Sterba } else { 2443521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2444521e0546SDavid Sterba btrfs_warn(root->fs_info, 2445521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2446c55bfa67SFilipe Manana dest->root_key.objectid); 2447521e0546SDavid Sterba err = -EPERM; 2448909e26dcSOmar Sandoval goto out_unlock_inode; 2449521e0546SDavid Sterba } 2450521e0546SDavid Sterba 245176dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 245276dda93cSYan, Zheng 245376dda93cSYan, Zheng err = may_destroy_subvol(dest); 245476dda93cSYan, Zheng if (err) 245576dda93cSYan, Zheng goto out_up_write; 245676dda93cSYan, Zheng 2457c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2458c58aaad2SMiao Xie /* 2459c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2460c58aaad2SMiao Xie * ref/backref. 2461c58aaad2SMiao Xie */ 2462c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2463ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2464c58aaad2SMiao Xie if (err) 2465c58aaad2SMiao Xie goto out_up_write; 2466c58aaad2SMiao Xie 2467a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2468a22285a6SYan, Zheng if (IS_ERR(trans)) { 2469a22285a6SYan, Zheng err = PTR_ERR(trans); 2470c58aaad2SMiao Xie goto out_release; 2471a22285a6SYan, Zheng } 2472c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2473c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2474a22285a6SYan, Zheng 24752be63d5cSFilipe Manana btrfs_record_snapshot_destroy(trans, dir); 24762be63d5cSFilipe Manana 247776dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 247876dda93cSYan, Zheng dest->root_key.objectid, 247976dda93cSYan, Zheng dentry->d_name.name, 248076dda93cSYan, Zheng dentry->d_name.len); 248179787eaaSJeff Mahoney if (ret) { 248279787eaaSJeff Mahoney err = ret; 248379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 248479787eaaSJeff Mahoney goto out_end_trans; 248579787eaaSJeff Mahoney } 248676dda93cSYan, Zheng 248776dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 248876dda93cSYan, Zheng 248976dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 249076dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 249176dda93cSYan, Zheng dest->root_item.drop_level = 0; 249276dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 249376dda93cSYan, Zheng 249427cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 249576dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 249676dda93cSYan, Zheng root->fs_info->tree_root, 249776dda93cSYan, Zheng dest->root_key.objectid); 249879787eaaSJeff Mahoney if (ret) { 249979787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 250079787eaaSJeff Mahoney err = ret; 250179787eaaSJeff Mahoney goto out_end_trans; 2502d68fc57bSYan, Zheng } 250379787eaaSJeff Mahoney } 2504dd5f9615SStefan Behrens 2505dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2506dd5f9615SStefan Behrens dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 2507dd5f9615SStefan Behrens dest->root_key.objectid); 2508dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2509dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2510dd5f9615SStefan Behrens err = ret; 2511dd5f9615SStefan Behrens goto out_end_trans; 2512dd5f9615SStefan Behrens } 2513dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2514dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2515dd5f9615SStefan Behrens dest->root_item.received_uuid, 2516dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2517dd5f9615SStefan Behrens dest->root_key.objectid); 2518dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2519dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2520dd5f9615SStefan Behrens err = ret; 2521dd5f9615SStefan Behrens goto out_end_trans; 2522dd5f9615SStefan Behrens } 2523dd5f9615SStefan Behrens } 2524dd5f9615SStefan Behrens 252579787eaaSJeff Mahoney out_end_trans: 2526c58aaad2SMiao Xie trans->block_rsv = NULL; 2527c58aaad2SMiao Xie trans->bytes_reserved = 0; 2528531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 252979787eaaSJeff Mahoney if (ret && !err) 253079787eaaSJeff Mahoney err = ret; 253176dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2532c58aaad2SMiao Xie out_release: 2533c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 253476dda93cSYan, Zheng out_up_write: 253576dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 2536521e0546SDavid Sterba if (err) { 2537521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2538c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2539c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2540521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2541521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2542521e0546SDavid Sterba } 2543909e26dcSOmar Sandoval out_unlock_inode: 25445955102cSAl Viro inode_unlock(inode); 254576dda93cSYan, Zheng if (!err) { 254664ad6c48SOmar Sandoval d_invalidate(dentry); 254776dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 254876dda93cSYan, Zheng d_delete(dentry); 254961155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2550fa6ac876SLiu Bo 2551fa6ac876SLiu Bo /* the last ref */ 255257cdc8dbSDavid Sterba if (dest->ino_cache_inode) { 255357cdc8dbSDavid Sterba iput(dest->ino_cache_inode); 255457cdc8dbSDavid Sterba dest->ino_cache_inode = NULL; 2555fa6ac876SLiu Bo } 255676dda93cSYan, Zheng } 255776dda93cSYan, Zheng out_dput: 255876dda93cSYan, Zheng dput(dentry); 255976dda93cSYan, Zheng out_unlock_dir: 25605955102cSAl Viro inode_unlock(dir); 2561e43f998eSDavid Sterba out_drop_write: 25622a79f17eSAl Viro mnt_drop_write_file(file); 256376dda93cSYan, Zheng out: 256476dda93cSYan, Zheng kfree(vol_args); 256576dda93cSYan, Zheng return err; 256676dda93cSYan, Zheng } 256776dda93cSYan, Zheng 25681e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2569f46b5a66SChristoph Hellwig { 2570496ad9aaSAl Viro struct inode *inode = file_inode(file); 2571f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 25721e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2573c146afadSYan Zheng int ret; 2574c146afadSYan Zheng 257525122d15SIlya Dryomov ret = mnt_want_write_file(file); 257625122d15SIlya Dryomov if (ret) 257725122d15SIlya Dryomov return ret; 2578b83cc969SLi Zefan 257925122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 258025122d15SIlya Dryomov ret = -EROFS; 258125122d15SIlya Dryomov goto out; 25825ac00addSStefan Behrens } 2583f46b5a66SChristoph Hellwig 2584f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2585f46b5a66SChristoph Hellwig case S_IFDIR: 2586e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2587e441d54dSChris Mason ret = -EPERM; 2588e441d54dSChris Mason goto out; 2589e441d54dSChris Mason } 2590de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 25918929ecfaSYan, Zheng if (ret) 25928929ecfaSYan, Zheng goto out; 2593de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2594f46b5a66SChristoph Hellwig break; 2595f46b5a66SChristoph Hellwig case S_IFREG: 2596e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2597e441d54dSChris Mason ret = -EINVAL; 2598e441d54dSChris Mason goto out; 2599e441d54dSChris Mason } 26001e701a32SChris Mason 26011e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 26021e701a32SChris Mason if (!range) { 26031e701a32SChris Mason ret = -ENOMEM; 26041e701a32SChris Mason goto out; 26051e701a32SChris Mason } 26061e701a32SChris Mason 26071e701a32SChris Mason if (argp) { 26081e701a32SChris Mason if (copy_from_user(range, argp, 26091e701a32SChris Mason sizeof(*range))) { 26101e701a32SChris Mason ret = -EFAULT; 26111e701a32SChris Mason kfree(range); 2612683be16eSDan Carpenter goto out; 26131e701a32SChris Mason } 26141e701a32SChris Mason /* compression requires us to start the IO */ 26151e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 26161e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 26171e701a32SChris Mason range->extent_thresh = (u32)-1; 26181e701a32SChris Mason } 26191e701a32SChris Mason } else { 26201e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 26211e701a32SChris Mason range->len = (u64)-1; 26221e701a32SChris Mason } 2623496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 26244cb5300bSChris Mason range, 0, 0); 26254cb5300bSChris Mason if (ret > 0) 26264cb5300bSChris Mason ret = 0; 26271e701a32SChris Mason kfree(range); 2628f46b5a66SChristoph Hellwig break; 26298929ecfaSYan, Zheng default: 26308929ecfaSYan, Zheng ret = -EINVAL; 2631f46b5a66SChristoph Hellwig } 2632e441d54dSChris Mason out: 263325122d15SIlya Dryomov mnt_drop_write_file(file); 2634e441d54dSChris Mason return ret; 2635f46b5a66SChristoph Hellwig } 2636f46b5a66SChristoph Hellwig 2637b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2638f46b5a66SChristoph Hellwig { 2639f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2640f46b5a66SChristoph Hellwig int ret; 2641f46b5a66SChristoph Hellwig 2642e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2643e441d54dSChris Mason return -EPERM; 2644e441d54dSChris Mason 26455ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 26465ac00addSStefan Behrens 1)) { 2647e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2648c9e9f97bSIlya Dryomov } 2649c9e9f97bSIlya Dryomov 26505ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2651dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2652c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2653c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2654c9e9f97bSIlya Dryomov goto out; 2655c9e9f97bSIlya Dryomov } 2656f46b5a66SChristoph Hellwig 26575516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2658f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2659f46b5a66SChristoph Hellwig 266043d20761SAnand Jain if (!ret) 266143d20761SAnand Jain btrfs_info(root->fs_info, "disk added %s",vol_args->name); 266243d20761SAnand Jain 2663f46b5a66SChristoph Hellwig kfree(vol_args); 2664c9e9f97bSIlya Dryomov out: 2665c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26665ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2667f46b5a66SChristoph Hellwig return ret; 2668f46b5a66SChristoph Hellwig } 2669f46b5a66SChristoph Hellwig 2670da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2671f46b5a66SChristoph Hellwig { 2672496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2673f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2674f46b5a66SChristoph Hellwig int ret; 2675f46b5a66SChristoph Hellwig 2676e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2677e441d54dSChris Mason return -EPERM; 2678e441d54dSChris Mason 2679da24927bSMiao Xie ret = mnt_want_write_file(file); 2680da24927bSMiao Xie if (ret) 2681da24927bSMiao Xie return ret; 2682c146afadSYan Zheng 2683dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2684c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2685c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2686c47ca32dSDan Carpenter goto err_drop; 2687c9e9f97bSIlya Dryomov } 2688f46b5a66SChristoph Hellwig 26895516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2690f46b5a66SChristoph Hellwig 2691183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2692183860f6SAnand Jain 1)) { 2693183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2694183860f6SAnand Jain goto out; 2695183860f6SAnand Jain } 2696183860f6SAnand Jain 2697183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2698183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2699c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 27005ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2701183860f6SAnand Jain 2702ec95d491SAnand Jain if (!ret) 2703ec95d491SAnand Jain btrfs_info(root->fs_info, "disk deleted %s",vol_args->name); 2704ec95d491SAnand Jain 2705183860f6SAnand Jain out: 2706183860f6SAnand Jain kfree(vol_args); 2707c47ca32dSDan Carpenter err_drop: 27084ac20c70SIlya Dryomov mnt_drop_write_file(file); 2709f46b5a66SChristoph Hellwig return ret; 2710f46b5a66SChristoph Hellwig } 2711f46b5a66SChristoph Hellwig 2712475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2713475f6387SJan Schmidt { 2714027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2715475f6387SJan Schmidt struct btrfs_device *device; 2716475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2717027ed2f0SLi Zefan int ret = 0; 2718475f6387SJan Schmidt 2719027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2720027ed2f0SLi Zefan if (!fi_args) 2721027ed2f0SLi Zefan return -ENOMEM; 2722027ed2f0SLi Zefan 2723f7171750SFilipe David Borba Manana mutex_lock(&fs_devices->device_list_mutex); 2724027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2725027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2726475f6387SJan Schmidt 2727d7641a49SByongho Lee list_for_each_entry(device, &fs_devices->devices, dev_list) { 2728027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2729027ed2f0SLi Zefan fi_args->max_id = device->devid; 2730475f6387SJan Schmidt } 2731475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2732475f6387SJan Schmidt 273380a773fbSDavid Sterba fi_args->nodesize = root->fs_info->super_copy->nodesize; 273480a773fbSDavid Sterba fi_args->sectorsize = root->fs_info->super_copy->sectorsize; 273580a773fbSDavid Sterba fi_args->clone_alignment = root->fs_info->super_copy->sectorsize; 273680a773fbSDavid Sterba 2737027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2738027ed2f0SLi Zefan ret = -EFAULT; 2739475f6387SJan Schmidt 2740027ed2f0SLi Zefan kfree(fi_args); 2741027ed2f0SLi Zefan return ret; 2742475f6387SJan Schmidt } 2743475f6387SJan Schmidt 2744475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2745475f6387SJan Schmidt { 2746475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2747475f6387SJan Schmidt struct btrfs_device *dev; 2748475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2749475f6387SJan Schmidt int ret = 0; 2750475f6387SJan Schmidt char *s_uuid = NULL; 2751475f6387SJan Schmidt 2752475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2753475f6387SJan Schmidt if (IS_ERR(di_args)) 2754475f6387SJan Schmidt return PTR_ERR(di_args); 2755475f6387SJan Schmidt 2756dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2757475f6387SJan Schmidt s_uuid = di_args->uuid; 2758475f6387SJan Schmidt 2759475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2760aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2761475f6387SJan Schmidt 2762475f6387SJan Schmidt if (!dev) { 2763475f6387SJan Schmidt ret = -ENODEV; 2764475f6387SJan Schmidt goto out; 2765475f6387SJan Schmidt } 2766475f6387SJan Schmidt 2767475f6387SJan Schmidt di_args->devid = dev->devid; 27687cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 27697cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 2770475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2771a27202fbSJim Meyering if (dev->name) { 2772606686eeSJosef Bacik struct rcu_string *name; 2773606686eeSJosef Bacik 2774606686eeSJosef Bacik rcu_read_lock(); 2775606686eeSJosef Bacik name = rcu_dereference(dev->name); 2776606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2777606686eeSJosef Bacik rcu_read_unlock(); 2778a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2779a27202fbSJim Meyering } else { 278099ba55adSStefan Behrens di_args->path[0] = '\0'; 2781a27202fbSJim Meyering } 2782475f6387SJan Schmidt 2783475f6387SJan Schmidt out: 278455793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2785475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2786475f6387SJan Schmidt ret = -EFAULT; 2787475f6387SJan Schmidt 2788475f6387SJan Schmidt kfree(di_args); 2789475f6387SJan Schmidt return ret; 2790475f6387SJan Schmidt } 2791475f6387SJan Schmidt 2792f4414602SMark Fasheh static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) 2793416161dbSMark Fasheh { 2794416161dbSMark Fasheh struct page *page; 2795416161dbSMark Fasheh 2796416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2797416161dbSMark Fasheh if (!page) 279831314002SFilipe Manana return ERR_PTR(-ENOMEM); 2799416161dbSMark Fasheh 2800416161dbSMark Fasheh if (!PageUptodate(page)) { 280131314002SFilipe Manana int ret; 280231314002SFilipe Manana 280331314002SFilipe Manana ret = btrfs_readpage(NULL, page); 280431314002SFilipe Manana if (ret) 280531314002SFilipe Manana return ERR_PTR(ret); 2806416161dbSMark Fasheh lock_page(page); 2807416161dbSMark Fasheh if (!PageUptodate(page)) { 2808416161dbSMark Fasheh unlock_page(page); 280909cbfeafSKirill A. Shutemov put_page(page); 281031314002SFilipe Manana return ERR_PTR(-EIO); 2811416161dbSMark Fasheh } 281231314002SFilipe Manana if (page->mapping != inode->i_mapping) { 2813416161dbSMark Fasheh unlock_page(page); 281409cbfeafSKirill A. Shutemov put_page(page); 281531314002SFilipe Manana return ERR_PTR(-EAGAIN); 2816416161dbSMark Fasheh } 2817416161dbSMark Fasheh } 2818416161dbSMark Fasheh 2819416161dbSMark Fasheh return page; 2820416161dbSMark Fasheh } 2821416161dbSMark Fasheh 2822f4414602SMark Fasheh static int gather_extent_pages(struct inode *inode, struct page **pages, 2823f4414602SMark Fasheh int num_pages, u64 off) 2824f4414602SMark Fasheh { 2825f4414602SMark Fasheh int i; 282609cbfeafSKirill A. Shutemov pgoff_t index = off >> PAGE_SHIFT; 2827f4414602SMark Fasheh 2828f4414602SMark Fasheh for (i = 0; i < num_pages; i++) { 282931314002SFilipe Manana again: 2830f4414602SMark Fasheh pages[i] = extent_same_get_page(inode, index + i); 283131314002SFilipe Manana if (IS_ERR(pages[i])) { 283231314002SFilipe Manana int err = PTR_ERR(pages[i]); 283331314002SFilipe Manana 283431314002SFilipe Manana if (err == -EAGAIN) 283531314002SFilipe Manana goto again; 283631314002SFilipe Manana pages[i] = NULL; 283731314002SFilipe Manana return err; 283831314002SFilipe Manana } 2839f4414602SMark Fasheh } 2840f4414602SMark Fasheh return 0; 2841f4414602SMark Fasheh } 2842f4414602SMark Fasheh 2843e0bd70c6SFilipe Manana static int lock_extent_range(struct inode *inode, u64 off, u64 len, 2844e0bd70c6SFilipe Manana bool retry_range_locking) 284577fe20dcSMark Fasheh { 2846e0bd70c6SFilipe Manana /* 2847e0bd70c6SFilipe Manana * Do any pending delalloc/csum calculations on inode, one way or 2848e0bd70c6SFilipe Manana * another, and lock file content. 2849e0bd70c6SFilipe Manana * The locking order is: 2850e0bd70c6SFilipe Manana * 2851e0bd70c6SFilipe Manana * 1) pages 2852e0bd70c6SFilipe Manana * 2) range in the inode's io tree 2853e0bd70c6SFilipe Manana */ 285477fe20dcSMark Fasheh while (1) { 285577fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 285677fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 285777fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 285877fe20dcSMark Fasheh off + len - 1); 2859ff5df9b8SFilipe Manana if ((!ordered || 2860ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2861ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 286277fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2863ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2864ff5df9b8SFilipe Manana if (ordered) 2865ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 286677fe20dcSMark Fasheh break; 2867ff5df9b8SFilipe Manana } 286877fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 286977fe20dcSMark Fasheh if (ordered) 287077fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 2871e0bd70c6SFilipe Manana if (!retry_range_locking) 2872e0bd70c6SFilipe Manana return -EAGAIN; 287377fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 287477fe20dcSMark Fasheh } 2875e0bd70c6SFilipe Manana return 0; 287677fe20dcSMark Fasheh } 287777fe20dcSMark Fasheh 2878f4414602SMark Fasheh static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) 2879416161dbSMark Fasheh { 28805955102cSAl Viro inode_unlock(inode1); 28815955102cSAl Viro inode_unlock(inode2); 2882416161dbSMark Fasheh } 2883416161dbSMark Fasheh 2884f4414602SMark Fasheh static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) 2885f4414602SMark Fasheh { 2886f4414602SMark Fasheh if (inode1 < inode2) 2887f4414602SMark Fasheh swap(inode1, inode2); 2888f4414602SMark Fasheh 28895955102cSAl Viro inode_lock_nested(inode1, I_MUTEX_PARENT); 28905955102cSAl Viro inode_lock_nested(inode2, I_MUTEX_CHILD); 2891f4414602SMark Fasheh } 2892f4414602SMark Fasheh 2893f4414602SMark Fasheh static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 2894f4414602SMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2895f4414602SMark Fasheh { 2896f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2897f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2898f4414602SMark Fasheh } 2899f4414602SMark Fasheh 2900e0bd70c6SFilipe Manana static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 2901e0bd70c6SFilipe Manana struct inode *inode2, u64 loff2, u64 len, 2902e0bd70c6SFilipe Manana bool retry_range_locking) 2903416161dbSMark Fasheh { 2904e0bd70c6SFilipe Manana int ret; 2905e0bd70c6SFilipe Manana 2906416161dbSMark Fasheh if (inode1 < inode2) { 2907416161dbSMark Fasheh swap(inode1, inode2); 2908416161dbSMark Fasheh swap(loff1, loff2); 2909416161dbSMark Fasheh } 2910e0bd70c6SFilipe Manana ret = lock_extent_range(inode1, loff1, len, retry_range_locking); 2911e0bd70c6SFilipe Manana if (ret) 2912e0bd70c6SFilipe Manana return ret; 2913e0bd70c6SFilipe Manana ret = lock_extent_range(inode2, loff2, len, retry_range_locking); 2914e0bd70c6SFilipe Manana if (ret) 2915e0bd70c6SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, 2916e0bd70c6SFilipe Manana loff1 + len - 1); 2917e0bd70c6SFilipe Manana return ret; 2918416161dbSMark Fasheh } 2919f4414602SMark Fasheh 2920f4414602SMark Fasheh struct cmp_pages { 2921f4414602SMark Fasheh int num_pages; 2922f4414602SMark Fasheh struct page **src_pages; 2923f4414602SMark Fasheh struct page **dst_pages; 2924f4414602SMark Fasheh }; 2925f4414602SMark Fasheh 2926f4414602SMark Fasheh static void btrfs_cmp_data_free(struct cmp_pages *cmp) 2927f4414602SMark Fasheh { 2928f4414602SMark Fasheh int i; 2929f4414602SMark Fasheh struct page *pg; 2930f4414602SMark Fasheh 2931f4414602SMark Fasheh for (i = 0; i < cmp->num_pages; i++) { 2932f4414602SMark Fasheh pg = cmp->src_pages[i]; 2933e0bd70c6SFilipe Manana if (pg) { 2934e0bd70c6SFilipe Manana unlock_page(pg); 293509cbfeafSKirill A. Shutemov put_page(pg); 2936e0bd70c6SFilipe Manana } 2937f4414602SMark Fasheh pg = cmp->dst_pages[i]; 2938e0bd70c6SFilipe Manana if (pg) { 2939e0bd70c6SFilipe Manana unlock_page(pg); 294009cbfeafSKirill A. Shutemov put_page(pg); 2941f4414602SMark Fasheh } 2942e0bd70c6SFilipe Manana } 2943f4414602SMark Fasheh kfree(cmp->src_pages); 2944f4414602SMark Fasheh kfree(cmp->dst_pages); 2945f4414602SMark Fasheh } 2946f4414602SMark Fasheh 2947f4414602SMark Fasheh static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, 2948f4414602SMark Fasheh struct inode *dst, u64 dst_loff, 2949f4414602SMark Fasheh u64 len, struct cmp_pages *cmp) 2950f4414602SMark Fasheh { 2951f4414602SMark Fasheh int ret; 295209cbfeafSKirill A. Shutemov int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; 2953f4414602SMark Fasheh struct page **src_pgarr, **dst_pgarr; 2954f4414602SMark Fasheh 2955f4414602SMark Fasheh /* 2956f4414602SMark Fasheh * We must gather up all the pages before we initiate our 2957f4414602SMark Fasheh * extent locking. We use an array for the page pointers. Size 2958f4414602SMark Fasheh * of the array is bounded by len, which is in turn bounded by 2959f4414602SMark Fasheh * BTRFS_MAX_DEDUPE_LEN. 2960f4414602SMark Fasheh */ 296166722f7cSDavid Sterba src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 296266722f7cSDavid Sterba dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 2963f4414602SMark Fasheh if (!src_pgarr || !dst_pgarr) { 2964f4414602SMark Fasheh kfree(src_pgarr); 2965f4414602SMark Fasheh kfree(dst_pgarr); 2966f4414602SMark Fasheh return -ENOMEM; 2967f4414602SMark Fasheh } 2968f4414602SMark Fasheh cmp->num_pages = num_pages; 2969f4414602SMark Fasheh cmp->src_pages = src_pgarr; 2970f4414602SMark Fasheh cmp->dst_pages = dst_pgarr; 2971f4414602SMark Fasheh 2972f4414602SMark Fasheh ret = gather_extent_pages(src, cmp->src_pages, cmp->num_pages, loff); 2973f4414602SMark Fasheh if (ret) 2974f4414602SMark Fasheh goto out; 2975f4414602SMark Fasheh 2976f4414602SMark Fasheh ret = gather_extent_pages(dst, cmp->dst_pages, cmp->num_pages, dst_loff); 2977f4414602SMark Fasheh 2978f4414602SMark Fasheh out: 2979f4414602SMark Fasheh if (ret) 2980f4414602SMark Fasheh btrfs_cmp_data_free(cmp); 2981f4414602SMark Fasheh return 0; 2982416161dbSMark Fasheh } 2983416161dbSMark Fasheh 2984416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, 2985f4414602SMark Fasheh u64 dst_loff, u64 len, struct cmp_pages *cmp) 2986416161dbSMark Fasheh { 2987416161dbSMark Fasheh int ret = 0; 2988f4414602SMark Fasheh int i; 2989416161dbSMark Fasheh struct page *src_page, *dst_page; 299009cbfeafSKirill A. Shutemov unsigned int cmp_len = PAGE_SIZE; 2991416161dbSMark Fasheh void *addr, *dst_addr; 2992416161dbSMark Fasheh 2993f4414602SMark Fasheh i = 0; 2994416161dbSMark Fasheh while (len) { 299509cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 2996416161dbSMark Fasheh cmp_len = len; 2997416161dbSMark Fasheh 2998f4414602SMark Fasheh BUG_ON(i >= cmp->num_pages); 2999f4414602SMark Fasheh 3000f4414602SMark Fasheh src_page = cmp->src_pages[i]; 3001f4414602SMark Fasheh dst_page = cmp->dst_pages[i]; 3002e0bd70c6SFilipe Manana ASSERT(PageLocked(src_page)); 3003e0bd70c6SFilipe Manana ASSERT(PageLocked(dst_page)); 3004f4414602SMark Fasheh 3005416161dbSMark Fasheh addr = kmap_atomic(src_page); 3006416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 3007416161dbSMark Fasheh 3008416161dbSMark Fasheh flush_dcache_page(src_page); 3009416161dbSMark Fasheh flush_dcache_page(dst_page); 3010416161dbSMark Fasheh 3011416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 30122b3909f8SDarrick J. Wong ret = -EBADE; 3013416161dbSMark Fasheh 3014416161dbSMark Fasheh kunmap_atomic(addr); 3015416161dbSMark Fasheh kunmap_atomic(dst_addr); 3016416161dbSMark Fasheh 3017416161dbSMark Fasheh if (ret) 3018416161dbSMark Fasheh break; 3019416161dbSMark Fasheh 3020416161dbSMark Fasheh len -= cmp_len; 3021f4414602SMark Fasheh i++; 3022416161dbSMark Fasheh } 3023416161dbSMark Fasheh 3024416161dbSMark Fasheh return ret; 3025416161dbSMark Fasheh } 3026416161dbSMark Fasheh 3027e1d227a4SMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, 3028e1d227a4SMark Fasheh u64 olen) 3029416161dbSMark Fasheh { 3030e1d227a4SMark Fasheh u64 len = *plen; 3031416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 3032416161dbSMark Fasheh 3033e1d227a4SMark Fasheh if (off + olen > inode->i_size || off + olen < off) 3034416161dbSMark Fasheh return -EINVAL; 3035e1d227a4SMark Fasheh 3036e1d227a4SMark Fasheh /* if we extend to eof, continue to block boundary */ 3037e1d227a4SMark Fasheh if (off + len == inode->i_size) 3038e1d227a4SMark Fasheh *plen = len = ALIGN(inode->i_size, bs) - off; 3039e1d227a4SMark Fasheh 3040416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 3041416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 3042416161dbSMark Fasheh return -EINVAL; 3043416161dbSMark Fasheh 3044416161dbSMark Fasheh return 0; 3045416161dbSMark Fasheh } 3046416161dbSMark Fasheh 3047e1d227a4SMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 3048416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 3049416161dbSMark Fasheh { 3050416161dbSMark Fasheh int ret; 3051e1d227a4SMark Fasheh u64 len = olen; 3052f4414602SMark Fasheh struct cmp_pages cmp; 30530efa9f48SMark Fasheh int same_inode = 0; 30540efa9f48SMark Fasheh u64 same_lock_start = 0; 30550efa9f48SMark Fasheh u64 same_lock_len = 0; 3056416161dbSMark Fasheh 3057416161dbSMark Fasheh if (src == dst) 30580efa9f48SMark Fasheh same_inode = 1; 3059416161dbSMark Fasheh 3060113e8283SFilipe Manana if (len == 0) 3061113e8283SFilipe Manana return 0; 3062113e8283SFilipe Manana 30630efa9f48SMark Fasheh if (same_inode) { 30645955102cSAl Viro inode_lock(src); 30650efa9f48SMark Fasheh 30660efa9f48SMark Fasheh ret = extent_same_check_offsets(src, loff, &len, olen); 30670efa9f48SMark Fasheh if (ret) 30680efa9f48SMark Fasheh goto out_unlock; 3069f4dfe687SFilipe Manana ret = extent_same_check_offsets(src, dst_loff, &len, olen); 3070f4dfe687SFilipe Manana if (ret) 3071f4dfe687SFilipe Manana goto out_unlock; 30720efa9f48SMark Fasheh 30730efa9f48SMark Fasheh /* 30740efa9f48SMark Fasheh * Single inode case wants the same checks, except we 30750efa9f48SMark Fasheh * don't want our length pushed out past i_size as 30760efa9f48SMark Fasheh * comparing that data range makes no sense. 30770efa9f48SMark Fasheh * 30780efa9f48SMark Fasheh * extent_same_check_offsets() will do this for an 30790efa9f48SMark Fasheh * unaligned length at i_size, so catch it here and 30800efa9f48SMark Fasheh * reject the request. 30810efa9f48SMark Fasheh * 30820efa9f48SMark Fasheh * This effectively means we require aligned extents 30830efa9f48SMark Fasheh * for the single-inode case, whereas the other cases 30840efa9f48SMark Fasheh * allow an unaligned length so long as it ends at 30850efa9f48SMark Fasheh * i_size. 30860efa9f48SMark Fasheh */ 30870efa9f48SMark Fasheh if (len != olen) { 30880efa9f48SMark Fasheh ret = -EINVAL; 30890efa9f48SMark Fasheh goto out_unlock; 30900efa9f48SMark Fasheh } 30910efa9f48SMark Fasheh 30920efa9f48SMark Fasheh /* Check for overlapping ranges */ 30930efa9f48SMark Fasheh if (dst_loff + len > loff && dst_loff < loff + len) { 30940efa9f48SMark Fasheh ret = -EINVAL; 30950efa9f48SMark Fasheh goto out_unlock; 30960efa9f48SMark Fasheh } 30970efa9f48SMark Fasheh 30980efa9f48SMark Fasheh same_lock_start = min_t(u64, loff, dst_loff); 30990efa9f48SMark Fasheh same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; 31000efa9f48SMark Fasheh } else { 3101f4414602SMark Fasheh btrfs_double_inode_lock(src, dst); 3102416161dbSMark Fasheh 3103e1d227a4SMark Fasheh ret = extent_same_check_offsets(src, loff, &len, olen); 3104416161dbSMark Fasheh if (ret) 3105416161dbSMark Fasheh goto out_unlock; 3106416161dbSMark Fasheh 3107e1d227a4SMark Fasheh ret = extent_same_check_offsets(dst, dst_loff, &len, olen); 3108416161dbSMark Fasheh if (ret) 3109416161dbSMark Fasheh goto out_unlock; 31100efa9f48SMark Fasheh } 3111416161dbSMark Fasheh 3112416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 3113416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 3114416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 3115416161dbSMark Fasheh ret = -EINVAL; 3116416161dbSMark Fasheh goto out_unlock; 3117416161dbSMark Fasheh } 3118416161dbSMark Fasheh 3119e0bd70c6SFilipe Manana again: 3120f4414602SMark Fasheh ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); 3121f4414602SMark Fasheh if (ret) 3122f4414602SMark Fasheh goto out_unlock; 3123f4414602SMark Fasheh 31240efa9f48SMark Fasheh if (same_inode) 3125e0bd70c6SFilipe Manana ret = lock_extent_range(src, same_lock_start, same_lock_len, 3126e0bd70c6SFilipe Manana false); 31270efa9f48SMark Fasheh else 3128e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, 3129e0bd70c6SFilipe Manana false); 3130e0bd70c6SFilipe Manana /* 3131e0bd70c6SFilipe Manana * If one of the inodes has dirty pages in the respective range or 3132e0bd70c6SFilipe Manana * ordered extents, we need to flush dellaloc and wait for all ordered 3133e0bd70c6SFilipe Manana * extents in the range. We must unlock the pages and the ranges in the 3134e0bd70c6SFilipe Manana * io trees to avoid deadlocks when flushing delalloc (requires locking 3135e0bd70c6SFilipe Manana * pages) and when waiting for ordered extents to complete (they require 3136e0bd70c6SFilipe Manana * range locking). 3137e0bd70c6SFilipe Manana */ 3138e0bd70c6SFilipe Manana if (ret == -EAGAIN) { 3139e0bd70c6SFilipe Manana /* 3140e0bd70c6SFilipe Manana * Ranges in the io trees already unlocked. Now unlock all 3141e0bd70c6SFilipe Manana * pages before waiting for all IO to complete. 3142e0bd70c6SFilipe Manana */ 3143e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3144e0bd70c6SFilipe Manana if (same_inode) { 3145e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, same_lock_start, 3146e0bd70c6SFilipe Manana same_lock_len); 3147e0bd70c6SFilipe Manana } else { 3148e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, loff, len); 3149e0bd70c6SFilipe Manana btrfs_wait_ordered_range(dst, dst_loff, len); 3150e0bd70c6SFilipe Manana } 3151e0bd70c6SFilipe Manana goto again; 3152e0bd70c6SFilipe Manana } 3153e0bd70c6SFilipe Manana ASSERT(ret == 0); 3154e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3155e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3156e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3157e0bd70c6SFilipe Manana return ret; 3158e0bd70c6SFilipe Manana } 3159f4414602SMark Fasheh 3160207910ddSMark Fasheh /* pass original length for comparison so we stay within i_size */ 3161f4414602SMark Fasheh ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp); 3162416161dbSMark Fasheh if (ret == 0) 31631c919a5eSMark Fasheh ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); 3164416161dbSMark Fasheh 31650efa9f48SMark Fasheh if (same_inode) 31660efa9f48SMark Fasheh unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, 31670efa9f48SMark Fasheh same_lock_start + same_lock_len - 1); 31680efa9f48SMark Fasheh else 3169f4414602SMark Fasheh btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 3170f4414602SMark Fasheh 3171f4414602SMark Fasheh btrfs_cmp_data_free(&cmp); 3172416161dbSMark Fasheh out_unlock: 31730efa9f48SMark Fasheh if (same_inode) 31745955102cSAl Viro inode_unlock(src); 31750efa9f48SMark Fasheh else 3176f4414602SMark Fasheh btrfs_double_inode_unlock(src, dst); 3177416161dbSMark Fasheh 3178416161dbSMark Fasheh return ret; 3179416161dbSMark Fasheh } 3180416161dbSMark Fasheh 3181ee22184bSByongho Lee #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3182416161dbSMark Fasheh 31832b3909f8SDarrick J. Wong ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen, 31842b3909f8SDarrick J. Wong struct file *dst_file, u64 dst_loff) 3185416161dbSMark Fasheh { 31862b3909f8SDarrick J. Wong struct inode *src = file_inode(src_file); 31872b3909f8SDarrick J. Wong struct inode *dst = file_inode(dst_file); 3188416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 31892b3909f8SDarrick J. Wong ssize_t res; 3190416161dbSMark Fasheh 31912b3909f8SDarrick J. Wong if (olen > BTRFS_MAX_DEDUPE_LEN) 31922b3909f8SDarrick J. Wong olen = BTRFS_MAX_DEDUPE_LEN; 3193416161dbSMark Fasheh 319409cbfeafSKirill A. Shutemov if (WARN_ON_ONCE(bs < PAGE_SIZE)) { 3195416161dbSMark Fasheh /* 3196416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 3197416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 3198416161dbSMark Fasheh * this situation without an update. 3199416161dbSMark Fasheh */ 32002b3909f8SDarrick J. Wong return -EINVAL; 3201416161dbSMark Fasheh } 3202416161dbSMark Fasheh 32032b3909f8SDarrick J. Wong res = btrfs_extent_same(src, loff, olen, dst, dst_loff); 32042b3909f8SDarrick J. Wong if (res) 32052b3909f8SDarrick J. Wong return res; 32062b3909f8SDarrick J. Wong return olen; 3207416161dbSMark Fasheh } 3208416161dbSMark Fasheh 3209f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3210f82a9901SFilipe Manana struct inode *inode, 3211f82a9901SFilipe Manana u64 endoff, 3212f82a9901SFilipe Manana const u64 destoff, 32131c919a5eSMark Fasheh const u64 olen, 32141c919a5eSMark Fasheh int no_time_update) 3215f82a9901SFilipe Manana { 3216f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3217f82a9901SFilipe Manana int ret; 3218f82a9901SFilipe Manana 3219f82a9901SFilipe Manana inode_inc_iversion(inode); 32201c919a5eSMark Fasheh if (!no_time_update) 322104b285f3SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb); 3222f82a9901SFilipe Manana /* 3223f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3224f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3225f82a9901SFilipe Manana */ 3226f82a9901SFilipe Manana if (endoff > destoff + olen) 3227f82a9901SFilipe Manana endoff = destoff + olen; 3228f82a9901SFilipe Manana if (endoff > inode->i_size) 3229f82a9901SFilipe Manana btrfs_i_size_write(inode, endoff); 3230f82a9901SFilipe Manana 3231f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3232f82a9901SFilipe Manana if (ret) { 3233f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3234f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3235f82a9901SFilipe Manana goto out; 3236f82a9901SFilipe Manana } 3237f82a9901SFilipe Manana ret = btrfs_end_transaction(trans, root); 3238f82a9901SFilipe Manana out: 3239f82a9901SFilipe Manana return ret; 3240f82a9901SFilipe Manana } 3241f82a9901SFilipe Manana 32427ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode, 32437ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 32447ffbb598SFilipe Manana const struct btrfs_path *path, 32457ffbb598SFilipe Manana const u64 hole_offset, 32467ffbb598SFilipe Manana const u64 hole_len) 32477ffbb598SFilipe Manana { 32487ffbb598SFilipe Manana struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 32497ffbb598SFilipe Manana struct extent_map *em; 32507ffbb598SFilipe Manana int ret; 32517ffbb598SFilipe Manana 32527ffbb598SFilipe Manana em = alloc_extent_map(); 32537ffbb598SFilipe Manana if (!em) { 32547ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 32557ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 32567ffbb598SFilipe Manana return; 32577ffbb598SFilipe Manana } 32587ffbb598SFilipe Manana 325914f59796SFilipe Manana if (path) { 326014f59796SFilipe Manana struct btrfs_file_extent_item *fi; 326114f59796SFilipe Manana 326214f59796SFilipe Manana fi = btrfs_item_ptr(path->nodes[0], path->slots[0], 326314f59796SFilipe Manana struct btrfs_file_extent_item); 32647ffbb598SFilipe Manana btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 32657ffbb598SFilipe Manana em->generation = -1; 32667ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 32677ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 32687ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 32697ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 32707ffbb598SFilipe Manana } else { 32717ffbb598SFilipe Manana em->start = hole_offset; 32727ffbb598SFilipe Manana em->len = hole_len; 32737ffbb598SFilipe Manana em->ram_bytes = em->len; 32747ffbb598SFilipe Manana em->orig_start = hole_offset; 32757ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 32767ffbb598SFilipe Manana em->block_len = 0; 32777ffbb598SFilipe Manana em->orig_block_len = 0; 32787ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 32797ffbb598SFilipe Manana em->generation = trans->transid; 32807ffbb598SFilipe Manana } 32817ffbb598SFilipe Manana 32827ffbb598SFilipe Manana while (1) { 32837ffbb598SFilipe Manana write_lock(&em_tree->lock); 32847ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 32857ffbb598SFilipe Manana write_unlock(&em_tree->lock); 32867ffbb598SFilipe Manana if (ret != -EEXIST) { 32877ffbb598SFilipe Manana free_extent_map(em); 32887ffbb598SFilipe Manana break; 32897ffbb598SFilipe Manana } 32907ffbb598SFilipe Manana btrfs_drop_extent_cache(inode, em->start, 32917ffbb598SFilipe Manana em->start + em->len - 1, 0); 32927ffbb598SFilipe Manana } 32937ffbb598SFilipe Manana 3294ee39b432SDavid Sterba if (ret) 32957ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 32967ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 32977ffbb598SFilipe Manana } 32987ffbb598SFilipe Manana 32998039d87dSFilipe Manana /* 33008039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 33018039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 33028039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 33038039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 33048039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 33058039d87dSFilipe Manana * 33068039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 33078039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 33088039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 33098039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 33108039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 33118039d87dSFilipe Manana * i_mutex, which we are holding here). 33128039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 33138039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 33148039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 33158039d87dSFilipe Manana * 33168039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 33178039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 33188039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 33198039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 33208039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 33218039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 33228039d87dSFilipe Manana * the former. 33238039d87dSFilipe Manana */ 33248039d87dSFilipe Manana static int clone_copy_inline_extent(struct inode *src, 33258039d87dSFilipe Manana struct inode *dst, 33268039d87dSFilipe Manana struct btrfs_trans_handle *trans, 33278039d87dSFilipe Manana struct btrfs_path *path, 33288039d87dSFilipe Manana struct btrfs_key *new_key, 33298039d87dSFilipe Manana const u64 drop_start, 33308039d87dSFilipe Manana const u64 datal, 33318039d87dSFilipe Manana const u64 skip, 33328039d87dSFilipe Manana const u64 size, 33338039d87dSFilipe Manana char *inline_data) 33348039d87dSFilipe Manana { 33358039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 33368039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 33378039d87dSFilipe Manana root->sectorsize); 33388039d87dSFilipe Manana int ret; 33398039d87dSFilipe Manana struct btrfs_key key; 33408039d87dSFilipe Manana 33418039d87dSFilipe Manana if (new_key->offset > 0) 33428039d87dSFilipe Manana return -EOPNOTSUPP; 33438039d87dSFilipe Manana 33448039d87dSFilipe Manana key.objectid = btrfs_ino(dst); 33458039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 33468039d87dSFilipe Manana key.offset = 0; 33478039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 33488039d87dSFilipe Manana if (ret < 0) { 33498039d87dSFilipe Manana return ret; 33508039d87dSFilipe Manana } else if (ret > 0) { 33518039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 33528039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 33538039d87dSFilipe Manana if (ret < 0) 33548039d87dSFilipe Manana return ret; 33558039d87dSFilipe Manana else if (ret > 0) 33568039d87dSFilipe Manana goto copy_inline_extent; 33578039d87dSFilipe Manana } 33588039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 33598039d87dSFilipe Manana if (key.objectid == btrfs_ino(dst) && 33608039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 33618039d87dSFilipe Manana ASSERT(key.offset > 0); 33628039d87dSFilipe Manana return -EOPNOTSUPP; 33638039d87dSFilipe Manana } 33648039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 33658039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 33668039d87dSFilipe Manana u64 ext_len; 33678039d87dSFilipe Manana 33688039d87dSFilipe Manana /* 33698039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 33708039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 33718039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 33728039d87dSFilipe Manana */ 33738039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 33748039d87dSFilipe Manana struct btrfs_file_extent_item); 33758039d87dSFilipe Manana /* 33768039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 33778039d87dSFilipe Manana * following it. 33788039d87dSFilipe Manana */ 33798039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 33808039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 33818039d87dSFilipe Manana goto copy_inline_extent; 33828039d87dSFilipe Manana 33838039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 33848039d87dSFilipe Manana if (ext_len > aligned_end) 33858039d87dSFilipe Manana return -EOPNOTSUPP; 33868039d87dSFilipe Manana 33878039d87dSFilipe Manana ret = btrfs_next_item(root, path); 33888039d87dSFilipe Manana if (ret < 0) { 33898039d87dSFilipe Manana return ret; 33908039d87dSFilipe Manana } else if (ret == 0) { 33918039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 33928039d87dSFilipe Manana path->slots[0]); 33938039d87dSFilipe Manana if (key.objectid == btrfs_ino(dst) && 33948039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 33958039d87dSFilipe Manana return -EOPNOTSUPP; 33968039d87dSFilipe Manana } 33978039d87dSFilipe Manana } 33988039d87dSFilipe Manana 33998039d87dSFilipe Manana copy_inline_extent: 34008039d87dSFilipe Manana /* 34018039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 34028039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 34038039d87dSFilipe Manana */ 34048039d87dSFilipe Manana if (i_size_read(dst) > datal) { 34058039d87dSFilipe Manana /* 34068039d87dSFilipe Manana * If the destination inode has an inline extent... 34078039d87dSFilipe Manana * This would require copying the data from the source inline 34088039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 34098039d87dSFilipe Manana * But this is really complex, both extents can be compressed 34108039d87dSFilipe Manana * or just one of them, which would require decompressing and 34118039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 34128039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 34138039d87dSFilipe Manana * inline extent). 34148039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 34158039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 34168039d87dSFilipe Manana */ 34178039d87dSFilipe Manana return -EOPNOTSUPP; 34188039d87dSFilipe Manana } 34198039d87dSFilipe Manana 34208039d87dSFilipe Manana btrfs_release_path(path); 34218039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 34228039d87dSFilipe Manana if (ret) 34238039d87dSFilipe Manana return ret; 34248039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 34258039d87dSFilipe Manana if (ret) 34268039d87dSFilipe Manana return ret; 34278039d87dSFilipe Manana 34288039d87dSFilipe Manana if (skip) { 34298039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 34308039d87dSFilipe Manana 34318039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 34328039d87dSFilipe Manana } 34338039d87dSFilipe Manana 34348039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 34358039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 34368039d87dSFilipe Manana path->slots[0]), 34378039d87dSFilipe Manana size); 34388039d87dSFilipe Manana inode_add_bytes(dst, datal); 34398039d87dSFilipe Manana 34408039d87dSFilipe Manana return 0; 34418039d87dSFilipe Manana } 34428039d87dSFilipe Manana 344332b7c687SMark Fasheh /** 344432b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 344532b7c687SMark Fasheh * 344632b7c687SMark Fasheh * @src: Inode to clone from 344732b7c687SMark Fasheh * @inode: Inode to clone to 344832b7c687SMark Fasheh * @off: Offset within source to start clone from 344932b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 34501c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 345132b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 34521c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 345332b7c687SMark Fasheh */ 345432b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3455f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 34561c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3457f46b5a66SChristoph Hellwig { 3458f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 345932b7c687SMark Fasheh struct btrfs_path *path = NULL; 3460f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 346132b7c687SMark Fasheh struct btrfs_trans_handle *trans; 346232b7c687SMark Fasheh char *buf = NULL; 3463ae01a0abSYan Zheng struct btrfs_key key; 3464f46b5a66SChristoph Hellwig u32 nritems; 3465f46b5a66SChristoph Hellwig int slot; 3466ae01a0abSYan Zheng int ret; 3467f82a9901SFilipe Manana const u64 len = olen_aligned; 3468f82a9901SFilipe Manana u64 last_dest_end = destoff; 3469ae01a0abSYan Zheng 3470ae01a0abSYan Zheng ret = -ENOMEM; 3471707e8a07SDavid Sterba buf = vmalloc(root->nodesize); 3472ae01a0abSYan Zheng if (!buf) 347332b7c687SMark Fasheh return ret; 3474ae01a0abSYan Zheng 3475ae01a0abSYan Zheng path = btrfs_alloc_path(); 3476ae01a0abSYan Zheng if (!path) { 3477ae01a0abSYan Zheng vfree(buf); 347832b7c687SMark Fasheh return ret; 3479ae01a0abSYan Zheng } 348032b7c687SMark Fasheh 3481e4058b54SDavid Sterba path->reada = READA_FORWARD; 3482c5c9cd4dSSage Weil /* clone data */ 348333345d01SLi Zefan key.objectid = btrfs_ino(src); 3484ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 34852c463823SFilipe Manana key.offset = off; 3486f46b5a66SChristoph Hellwig 3487f46b5a66SChristoph Hellwig while (1) { 3488de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3489df858e76SFilipe Manana 3490f46b5a66SChristoph Hellwig /* 3491f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3492f46b5a66SChristoph Hellwig * tree. 3493f46b5a66SChristoph Hellwig */ 3494e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3495362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3496362a20c5SDavid Sterba 0, 0); 3497f46b5a66SChristoph Hellwig if (ret < 0) 3498f46b5a66SChristoph Hellwig goto out; 34992c463823SFilipe Manana /* 35002c463823SFilipe Manana * First search, if no extent item that starts at offset off was 35012c463823SFilipe Manana * found but the previous item is an extent item, it's possible 35022c463823SFilipe Manana * it might overlap our target range, therefore process it. 35032c463823SFilipe Manana */ 35042c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 35052c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 35062c463823SFilipe Manana path->slots[0] - 1); 35072c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 35082c463823SFilipe Manana path->slots[0]--; 35092c463823SFilipe Manana } 3510f46b5a66SChristoph Hellwig 3511ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3512e4355f34SFilipe David Borba Manana process_slot: 3513ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3514362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3515f46b5a66SChristoph Hellwig if (ret < 0) 3516f46b5a66SChristoph Hellwig goto out; 3517f46b5a66SChristoph Hellwig if (ret > 0) 3518f46b5a66SChristoph Hellwig break; 3519ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3520f46b5a66SChristoph Hellwig } 3521f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3522f46b5a66SChristoph Hellwig slot = path->slots[0]; 3523f46b5a66SChristoph Hellwig 3524ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3525962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 352633345d01SLi Zefan key.objectid != btrfs_ino(src)) 3527f46b5a66SChristoph Hellwig break; 3528f46b5a66SChristoph Hellwig 3529962a298fSDavid Sterba if (key.type == BTRFS_EXTENT_DATA_KEY) { 3530c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3531c5c9cd4dSSage Weil int type; 353231840ae1SZheng Yan u32 size; 353331840ae1SZheng Yan struct btrfs_key new_key; 3534c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3535c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3536c5c9cd4dSSage Weil u8 comp; 3537f82a9901SFilipe Manana u64 drop_start; 353831840ae1SZheng Yan 3539c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3540c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3541c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3542c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3543c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3544c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3545d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3546d397712bSChris Mason extent); 3547d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3548d397712bSChris Mason extent); 3549c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3550d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3551d397712bSChris Mason extent); 3552c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3553c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3554c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3555c5c9cd4dSSage Weil extent); 3556c5c9cd4dSSage Weil } 355731840ae1SZheng Yan 35582c463823SFilipe Manana /* 35592c463823SFilipe Manana * The first search might have left us at an extent 35602c463823SFilipe Manana * item that ends before our target range's start, can 35612c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 35622c463823SFilipe Manana */ 35632c463823SFilipe Manana if (key.offset + datal <= off) { 3564e4355f34SFilipe David Borba Manana path->slots[0]++; 3565e4355f34SFilipe David Borba Manana goto process_slot; 35662c463823SFilipe Manana } else if (key.offset >= off + len) { 35672c463823SFilipe Manana break; 3568e4355f34SFilipe David Borba Manana } 3569df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 3570e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3571e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3572e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3573e4355f34SFilipe David Borba Manana size); 3574e4355f34SFilipe David Borba Manana 3575e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3576e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3577c5c9cd4dSSage Weil 357831840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 357933345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 35804d728ec7SLi Zefan if (off <= key.offset) 3581c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 35824d728ec7SLi Zefan else 35834d728ec7SLi Zefan new_key.offset = destoff; 3584c5c9cd4dSSage Weil 3585b6f3409bSSage Weil /* 3586f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3587f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3588f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3589f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3590f82a9901SFilipe Manana * partially overlaps it). 3591f82a9901SFilipe Manana */ 3592f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3593f82a9901SFilipe Manana drop_start = last_dest_end; 3594f82a9901SFilipe Manana else 3595f82a9901SFilipe Manana drop_start = new_key.offset; 3596f82a9901SFilipe Manana 3597f82a9901SFilipe Manana /* 3598b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3599b6f3409bSSage Weil * 1 - add new extent 3600b6f3409bSSage Weil * 1 - inode update 3601b6f3409bSSage Weil */ 3602b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3603a22285a6SYan, Zheng if (IS_ERR(trans)) { 3604a22285a6SYan, Zheng ret = PTR_ERR(trans); 3605a22285a6SYan, Zheng goto out; 3606a22285a6SYan, Zheng } 3607a22285a6SYan, Zheng 3608c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3609c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3610d72c0842SLi Zefan /* 3611d72c0842SLi Zefan * a | --- range to clone ---| b 3612d72c0842SLi Zefan * | ------------- extent ------------- | 3613d72c0842SLi Zefan */ 3614d72c0842SLi Zefan 361593915584SAntonio Ospite /* subtract range b */ 3616d72c0842SLi Zefan if (key.offset + datal > off + len) 3617d72c0842SLi Zefan datal = off + len - key.offset; 3618d72c0842SLi Zefan 361993915584SAntonio Ospite /* subtract range a */ 3620a22285a6SYan, Zheng if (off > key.offset) { 3621a22285a6SYan, Zheng datao += off - key.offset; 3622a22285a6SYan, Zheng datal -= off - key.offset; 3623a22285a6SYan, Zheng } 3624a22285a6SYan, Zheng 36255dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3626f82a9901SFilipe Manana drop_start, 3627a22285a6SYan, Zheng new_key.offset + datal, 36282671485dSJosef Bacik 1); 362979787eaaSJeff Mahoney if (ret) { 36303f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 363100fdf13aSLiu Bo btrfs_abort_transaction(trans, 363200fdf13aSLiu Bo root, ret); 363379787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 363479787eaaSJeff Mahoney goto out; 363579787eaaSJeff Mahoney } 3636a22285a6SYan, Zheng 363731840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 363831840ae1SZheng Yan &new_key, size); 363979787eaaSJeff Mahoney if (ret) { 364079787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 364179787eaaSJeff Mahoney ret); 364279787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 364379787eaaSJeff Mahoney goto out; 364479787eaaSJeff Mahoney } 364531840ae1SZheng Yan 364631840ae1SZheng Yan leaf = path->nodes[0]; 364731840ae1SZheng Yan slot = path->slots[0]; 364831840ae1SZheng Yan write_extent_buffer(leaf, buf, 364931840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 365031840ae1SZheng Yan size); 3651ae01a0abSYan Zheng 3652f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3653f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3654c5c9cd4dSSage Weil 3655c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3656c5c9cd4dSSage Weil if (!disko) 3657c5c9cd4dSSage Weil datao = 0; 3658c5c9cd4dSSage Weil 3659c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3660c5c9cd4dSSage Weil datao); 3661c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3662c5c9cd4dSSage Weil datal); 3663fcebe456SJosef Bacik 3664c5c9cd4dSSage Weil if (disko) { 3665c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3666ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 36675d4f98a2SYan Zheng disko, diskl, 0, 3668f46b5a66SChristoph Hellwig root->root_key.objectid, 366933345d01SLi Zefan btrfs_ino(inode), 3670b06c4bf5SFilipe Manana new_key.offset - datao); 367179787eaaSJeff Mahoney if (ret) { 367279787eaaSJeff Mahoney btrfs_abort_transaction(trans, 367379787eaaSJeff Mahoney root, 367479787eaaSJeff Mahoney ret); 367579787eaaSJeff Mahoney btrfs_end_transaction(trans, 367679787eaaSJeff Mahoney root); 367779787eaaSJeff Mahoney goto out; 367879787eaaSJeff Mahoney 367979787eaaSJeff Mahoney } 3680f46b5a66SChristoph Hellwig } 3681c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3682c5c9cd4dSSage Weil u64 skip = 0; 3683c5c9cd4dSSage Weil u64 trim = 0; 3684ed958762SFilipe Manana 3685c5c9cd4dSSage Weil if (off > key.offset) { 3686c5c9cd4dSSage Weil skip = off - key.offset; 3687c5c9cd4dSSage Weil new_key.offset += skip; 368831840ae1SZheng Yan } 3689d397712bSChris Mason 3690c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3691c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3692d397712bSChris Mason 3693c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3694c5c9cd4dSSage Weil ret = -EINVAL; 3695a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 3696c5c9cd4dSSage Weil goto out; 369731840ae1SZheng Yan } 3698c5c9cd4dSSage Weil size -= skip + trim; 3699c5c9cd4dSSage Weil datal -= skip + trim; 3700a22285a6SYan, Zheng 37018039d87dSFilipe Manana ret = clone_copy_inline_extent(src, inode, 37028039d87dSFilipe Manana trans, path, 37038039d87dSFilipe Manana &new_key, 3704f82a9901SFilipe Manana drop_start, 37058039d87dSFilipe Manana datal, 37068039d87dSFilipe Manana skip, size, buf); 370779787eaaSJeff Mahoney if (ret) { 37083f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 37093a29bc09SChris Mason btrfs_abort_transaction(trans, 37108039d87dSFilipe Manana root, 371179787eaaSJeff Mahoney ret); 371279787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 371379787eaaSJeff Mahoney goto out; 371479787eaaSJeff Mahoney } 3715c5c9cd4dSSage Weil leaf = path->nodes[0]; 3716c5c9cd4dSSage Weil slot = path->slots[0]; 3717c5c9cd4dSSage Weil } 3718c5c9cd4dSSage Weil 37197ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 37207ffbb598SFilipe Manana if (drop_start < new_key.offset) 37217ffbb598SFilipe Manana clone_update_extent_map(inode, trans, 372214f59796SFilipe Manana NULL, drop_start, 37237ffbb598SFilipe Manana new_key.offset - drop_start); 37247ffbb598SFilipe Manana 372514f59796SFilipe Manana clone_update_extent_map(inode, trans, path, 0, 0); 37267ffbb598SFilipe Manana 3727c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3728b3b4aa74SDavid Sterba btrfs_release_path(path); 3729c5c9cd4dSSage Weil 373062e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 373162e2390eSFilipe Manana root->sectorsize); 3732f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3733f82a9901SFilipe Manana last_dest_end, 37341c919a5eSMark Fasheh destoff, olen, 37351c919a5eSMark Fasheh no_time_update); 3736f82a9901SFilipe Manana if (ret) 373779787eaaSJeff Mahoney goto out; 37382c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 37392c463823SFilipe Manana break; 3740a22285a6SYan, Zheng } 3741b3b4aa74SDavid Sterba btrfs_release_path(path); 3742df858e76SFilipe Manana key.offset = next_key_min_offset; 3743ae01a0abSYan Zheng } 3744f46b5a66SChristoph Hellwig ret = 0; 374532b7c687SMark Fasheh 3746f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3747f82a9901SFilipe Manana /* 3748f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3749f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3750f82a9901SFilipe Manana */ 3751b3b4aa74SDavid Sterba btrfs_release_path(path); 3752f82a9901SFilipe Manana 3753f82a9901SFilipe Manana /* 3754f82a9901SFilipe Manana * 1 - remove extent(s) 3755f82a9901SFilipe Manana * 1 - inode update 3756f82a9901SFilipe Manana */ 3757f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3758f82a9901SFilipe Manana if (IS_ERR(trans)) { 3759f82a9901SFilipe Manana ret = PTR_ERR(trans); 3760f82a9901SFilipe Manana goto out; 3761f82a9901SFilipe Manana } 3762f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3763f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3764f82a9901SFilipe Manana if (ret) { 3765f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 3766f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3767f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3768f82a9901SFilipe Manana goto out; 3769f82a9901SFilipe Manana } 377014f59796SFilipe Manana clone_update_extent_map(inode, trans, NULL, last_dest_end, 377114f59796SFilipe Manana destoff + len - last_dest_end); 3772f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 37731c919a5eSMark Fasheh destoff, olen, no_time_update); 3774f82a9901SFilipe Manana } 3775f82a9901SFilipe Manana 3776f46b5a66SChristoph Hellwig out: 377732b7c687SMark Fasheh btrfs_free_path(path); 377832b7c687SMark Fasheh vfree(buf); 377932b7c687SMark Fasheh return ret; 378032b7c687SMark Fasheh } 378132b7c687SMark Fasheh 37823db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 378332b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 378432b7c687SMark Fasheh { 378554563d41SAl Viro struct inode *inode = file_inode(file); 37863db11b2eSZach Brown struct inode *src = file_inode(file_src); 378732b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 378832b7c687SMark Fasheh int ret; 378932b7c687SMark Fasheh u64 len = olen; 379032b7c687SMark Fasheh u64 bs = root->fs_info->sb->s_blocksize; 37913db11b2eSZach Brown int same_inode = src == inode; 379232b7c687SMark Fasheh 379332b7c687SMark Fasheh /* 379432b7c687SMark Fasheh * TODO: 379532b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 379632b7c687SMark Fasheh * decompress into destination's address_space (the file offset 379732b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 379832b7c687SMark Fasheh * otherwise reinsert) a subrange. 379900fdf13aSLiu Bo * 380000fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 380100fdf13aSLiu Bo * be either compressed or non-compressed. 380232b7c687SMark Fasheh */ 380332b7c687SMark Fasheh 380432b7c687SMark Fasheh if (btrfs_root_readonly(root)) 380532b7c687SMark Fasheh return -EROFS; 380632b7c687SMark Fasheh 38073db11b2eSZach Brown if (file_src->f_path.mnt != file->f_path.mnt || 38083db11b2eSZach Brown src->i_sb != inode->i_sb) 38093db11b2eSZach Brown return -EXDEV; 381032b7c687SMark Fasheh 381132b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 381232b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 381332b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 38143db11b2eSZach Brown return -EINVAL; 381532b7c687SMark Fasheh 381632b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 38173db11b2eSZach Brown return -EISDIR; 381832b7c687SMark Fasheh 381932b7c687SMark Fasheh if (!same_inode) { 3820293a8489SMark Fasheh btrfs_double_inode_lock(src, inode); 382132b7c687SMark Fasheh } else { 38225955102cSAl Viro inode_lock(src); 382332b7c687SMark Fasheh } 382432b7c687SMark Fasheh 382532b7c687SMark Fasheh /* determine range to clone */ 382632b7c687SMark Fasheh ret = -EINVAL; 382732b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 382832b7c687SMark Fasheh goto out_unlock; 382932b7c687SMark Fasheh if (len == 0) 383032b7c687SMark Fasheh olen = len = src->i_size - off; 383132b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 383232b7c687SMark Fasheh if (off + len == src->i_size) 383332b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 383432b7c687SMark Fasheh 3835ccccf3d6SFilipe Manana if (len == 0) { 3836ccccf3d6SFilipe Manana ret = 0; 3837ccccf3d6SFilipe Manana goto out_unlock; 3838ccccf3d6SFilipe Manana } 3839ccccf3d6SFilipe Manana 384032b7c687SMark Fasheh /* verify the end result is block aligned */ 384132b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 384232b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 384332b7c687SMark Fasheh goto out_unlock; 384432b7c687SMark Fasheh 384532b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 384632b7c687SMark Fasheh if (same_inode) { 384732b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 384832b7c687SMark Fasheh goto out_unlock; 384932b7c687SMark Fasheh } 385032b7c687SMark Fasheh 385132b7c687SMark Fasheh if (destoff > inode->i_size) { 385232b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 385332b7c687SMark Fasheh if (ret) 385432b7c687SMark Fasheh goto out_unlock; 385532b7c687SMark Fasheh } 385632b7c687SMark Fasheh 3857c125b8bfSFilipe Manana /* 3858c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3859c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3860c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3861c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3862c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3863c125b8bfSFilipe Manana */ 3864c125b8bfSFilipe Manana if (same_inode) { 3865c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3866c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 386732b7c687SMark Fasheh 3868e0bd70c6SFilipe Manana ret = lock_extent_range(src, lock_start, lock_len, true); 3869c125b8bfSFilipe Manana } else { 3870e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, off, inode, destoff, len, 3871e0bd70c6SFilipe Manana true); 3872e0bd70c6SFilipe Manana } 3873e0bd70c6SFilipe Manana ASSERT(ret == 0); 3874e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3875e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3876e0bd70c6SFilipe Manana goto out_unlock; 3877c125b8bfSFilipe Manana } 387832b7c687SMark Fasheh 38791c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 388032b7c687SMark Fasheh 3881c125b8bfSFilipe Manana if (same_inode) { 3882c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3883c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3884c125b8bfSFilipe Manana 3885c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3886c125b8bfSFilipe Manana } else { 3887293a8489SMark Fasheh btrfs_double_extent_unlock(src, off, inode, destoff, len); 3888c125b8bfSFilipe Manana } 3889c125b8bfSFilipe Manana /* 3890c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3891c125b8bfSFilipe Manana * data immediately and not the previous data. 3892c125b8bfSFilipe Manana */ 389365bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 389409cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 389509cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 3896f46b5a66SChristoph Hellwig out_unlock: 3897293a8489SMark Fasheh if (!same_inode) 3898293a8489SMark Fasheh btrfs_double_inode_unlock(src, inode); 3899293a8489SMark Fasheh else 39005955102cSAl Viro inode_unlock(src); 39013db11b2eSZach Brown return ret; 39023db11b2eSZach Brown } 39033db11b2eSZach Brown 39043db11b2eSZach Brown ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in, 39053db11b2eSZach Brown struct file *file_out, loff_t pos_out, 39063db11b2eSZach Brown size_t len, unsigned int flags) 39073db11b2eSZach Brown { 39083db11b2eSZach Brown ssize_t ret; 39093db11b2eSZach Brown 39103db11b2eSZach Brown ret = btrfs_clone_files(file_out, file_in, pos_in, len, pos_out); 39113db11b2eSZach Brown if (ret == 0) 39123db11b2eSZach Brown ret = len; 39133db11b2eSZach Brown return ret; 39143db11b2eSZach Brown } 39153db11b2eSZach Brown 391604b38d60SChristoph Hellwig int btrfs_clone_file_range(struct file *src_file, loff_t off, 391704b38d60SChristoph Hellwig struct file *dst_file, loff_t destoff, u64 len) 39183db11b2eSZach Brown { 391904b38d60SChristoph Hellwig return btrfs_clone_files(dst_file, src_file, off, len, destoff); 3920c5c9cd4dSSage Weil } 3921c5c9cd4dSSage Weil 3922f46b5a66SChristoph Hellwig /* 3923f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3924f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3925f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3926f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3927f46b5a66SChristoph Hellwig */ 3928b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3929f46b5a66SChristoph Hellwig { 3930496ad9aaSAl Viro struct inode *inode = file_inode(file); 3931f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3932f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 39331ab86aedSSage Weil int ret; 3934f46b5a66SChristoph Hellwig 39351ab86aedSSage Weil ret = -EPERM; 3936df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3937f46b5a66SChristoph Hellwig goto out; 39381ab86aedSSage Weil 39391ab86aedSSage Weil ret = -EINPROGRESS; 39401ab86aedSSage Weil if (file->private_data) 39411ab86aedSSage Weil goto out; 39429ca9ee09SSage Weil 3943b83cc969SLi Zefan ret = -EROFS; 3944b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3945b83cc969SLi Zefan goto out; 3946b83cc969SLi Zefan 3947a561be71SAl Viro ret = mnt_want_write_file(file); 3948c146afadSYan Zheng if (ret) 3949c146afadSYan Zheng goto out; 3950c146afadSYan Zheng 3951a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 39529ca9ee09SSage Weil 3953f46b5a66SChristoph Hellwig ret = -ENOMEM; 39547a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3955abd30bb0STsutomu Itoh if (IS_ERR(trans)) 39561ab86aedSSage Weil goto out_drop; 39571ab86aedSSage Weil 39581ab86aedSSage Weil file->private_data = trans; 39591ab86aedSSage Weil return 0; 39601ab86aedSSage Weil 39611ab86aedSSage Weil out_drop: 3962a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 39632a79f17eSAl Viro mnt_drop_write_file(file); 3964f46b5a66SChristoph Hellwig out: 3965f46b5a66SChristoph Hellwig return ret; 3966f46b5a66SChristoph Hellwig } 3967f46b5a66SChristoph Hellwig 39686ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 39696ef5ed0dSJosef Bacik { 3970496ad9aaSAl Viro struct inode *inode = file_inode(file); 39716ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 39726ef5ed0dSJosef Bacik struct btrfs_root *new_root; 39736ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 39746ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 39756ef5ed0dSJosef Bacik struct btrfs_path *path; 39766ef5ed0dSJosef Bacik struct btrfs_key location; 39776ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 39786ef5ed0dSJosef Bacik u64 objectid = 0; 39796ef5ed0dSJosef Bacik u64 dir_id; 39803c04ce01SMiao Xie int ret; 39816ef5ed0dSJosef Bacik 39826ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 39836ef5ed0dSJosef Bacik return -EPERM; 39846ef5ed0dSJosef Bacik 39853c04ce01SMiao Xie ret = mnt_want_write_file(file); 39863c04ce01SMiao Xie if (ret) 39873c04ce01SMiao Xie return ret; 39883c04ce01SMiao Xie 39893c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 39903c04ce01SMiao Xie ret = -EFAULT; 39913c04ce01SMiao Xie goto out; 39923c04ce01SMiao Xie } 39936ef5ed0dSJosef Bacik 39946ef5ed0dSJosef Bacik if (!objectid) 39951cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 39966ef5ed0dSJosef Bacik 39976ef5ed0dSJosef Bacik location.objectid = objectid; 39986ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 39996ef5ed0dSJosef Bacik location.offset = (u64)-1; 40006ef5ed0dSJosef Bacik 40016ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 40023c04ce01SMiao Xie if (IS_ERR(new_root)) { 40033c04ce01SMiao Xie ret = PTR_ERR(new_root); 40043c04ce01SMiao Xie goto out; 40053c04ce01SMiao Xie } 40066ef5ed0dSJosef Bacik 40076ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 40083c04ce01SMiao Xie if (!path) { 40093c04ce01SMiao Xie ret = -ENOMEM; 40103c04ce01SMiao Xie goto out; 40113c04ce01SMiao Xie } 40126ef5ed0dSJosef Bacik path->leave_spinning = 1; 40136ef5ed0dSJosef Bacik 40146ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 401598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 40166ef5ed0dSJosef Bacik btrfs_free_path(path); 40173c04ce01SMiao Xie ret = PTR_ERR(trans); 40183c04ce01SMiao Xie goto out; 40196ef5ed0dSJosef Bacik } 40206ef5ed0dSJosef Bacik 40216c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 40226ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 40236ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4024cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 40256ef5ed0dSJosef Bacik btrfs_free_path(path); 40266ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 4027efe120a0SFrank Holton btrfs_err(new_root->fs_info, "Umm, you don't have the default dir" 4028efe120a0SFrank Holton "item, this isn't going to work"); 40293c04ce01SMiao Xie ret = -ENOENT; 40303c04ce01SMiao Xie goto out; 40316ef5ed0dSJosef Bacik } 40326ef5ed0dSJosef Bacik 40336ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 40346ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 40356ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 40366ef5ed0dSJosef Bacik btrfs_free_path(path); 40376ef5ed0dSJosef Bacik 40382b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 40396ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 40403c04ce01SMiao Xie out: 40413c04ce01SMiao Xie mnt_drop_write_file(file); 40423c04ce01SMiao Xie return ret; 40436ef5ed0dSJosef Bacik } 40446ef5ed0dSJosef Bacik 40455af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 4046bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4047bf5fc093SJosef Bacik { 4048bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 4049bf5fc093SJosef Bacik 4050bf5fc093SJosef Bacik space->total_bytes = 0; 4051bf5fc093SJosef Bacik space->used_bytes = 0; 4052bf5fc093SJosef Bacik space->flags = 0; 4053bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4054bf5fc093SJosef Bacik space->flags = block_group->flags; 4055bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 4056bf5fc093SJosef Bacik space->used_bytes += 4057bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 4058bf5fc093SJosef Bacik } 4059bf5fc093SJosef Bacik } 4060bf5fc093SJosef Bacik 406148a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 40621406e432SJosef Bacik { 40631406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 40641406e432SJosef Bacik struct btrfs_ioctl_space_info space; 40651406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 40667fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 406713f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 40681406e432SJosef Bacik struct btrfs_space_info *info; 4069bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 4070bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4071bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4072bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 4073bf5fc093SJosef Bacik int num_types = 4; 40747fde62bfSChris Mason int alloc_size; 40751406e432SJosef Bacik int ret = 0; 407651788b1bSDan Rosenberg u64 slot_count = 0; 4077bf5fc093SJosef Bacik int i, c; 40781406e432SJosef Bacik 40791406e432SJosef Bacik if (copy_from_user(&space_args, 40801406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 40811406e432SJosef Bacik sizeof(space_args))) 40821406e432SJosef Bacik return -EFAULT; 40831406e432SJosef Bacik 4084bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4085bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4086bf5fc093SJosef Bacik 4087bf5fc093SJosef Bacik info = NULL; 40887fde62bfSChris Mason rcu_read_lock(); 4089bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 4090bf5fc093SJosef Bacik list) { 4091bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4092bf5fc093SJosef Bacik info = tmp; 4093bf5fc093SJosef Bacik break; 4094bf5fc093SJosef Bacik } 4095bf5fc093SJosef Bacik } 40967fde62bfSChris Mason rcu_read_unlock(); 40971406e432SJosef Bacik 4098bf5fc093SJosef Bacik if (!info) 4099bf5fc093SJosef Bacik continue; 4100bf5fc093SJosef Bacik 4101bf5fc093SJosef Bacik down_read(&info->groups_sem); 4102bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4103bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4104bf5fc093SJosef Bacik slot_count++; 4105bf5fc093SJosef Bacik } 4106bf5fc093SJosef Bacik up_read(&info->groups_sem); 4107bf5fc093SJosef Bacik } 4108bf5fc093SJosef Bacik 410936523e95SDavid Sterba /* 411036523e95SDavid Sterba * Global block reserve, exported as a space_info 411136523e95SDavid Sterba */ 411236523e95SDavid Sterba slot_count++; 411336523e95SDavid Sterba 41147fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 41157fde62bfSChris Mason if (space_args.space_slots == 0) { 41167fde62bfSChris Mason space_args.total_spaces = slot_count; 41177fde62bfSChris Mason goto out; 41187fde62bfSChris Mason } 4119bf5fc093SJosef Bacik 412051788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4121bf5fc093SJosef Bacik 41227fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4123bf5fc093SJosef Bacik 41247fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 41257fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 41267fde62bfSChris Mason */ 412709cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 41287fde62bfSChris Mason return -ENOMEM; 41297fde62bfSChris Mason 41307fde62bfSChris Mason space_args.total_spaces = 0; 41318d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 41327fde62bfSChris Mason if (!dest) 41337fde62bfSChris Mason return -ENOMEM; 41347fde62bfSChris Mason dest_orig = dest; 41357fde62bfSChris Mason 41367fde62bfSChris Mason /* now we have a buffer to copy into */ 4137bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4138bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4139bf5fc093SJosef Bacik 414051788b1bSDan Rosenberg if (!slot_count) 414151788b1bSDan Rosenberg break; 414251788b1bSDan Rosenberg 4143bf5fc093SJosef Bacik info = NULL; 41441406e432SJosef Bacik rcu_read_lock(); 4145bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 4146bf5fc093SJosef Bacik list) { 4147bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4148bf5fc093SJosef Bacik info = tmp; 41497fde62bfSChris Mason break; 4150bf5fc093SJosef Bacik } 4151bf5fc093SJosef Bacik } 4152bf5fc093SJosef Bacik rcu_read_unlock(); 41537fde62bfSChris Mason 4154bf5fc093SJosef Bacik if (!info) 4155bf5fc093SJosef Bacik continue; 4156bf5fc093SJosef Bacik down_read(&info->groups_sem); 4157bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4158bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 41595af3e8ccSStefan Behrens btrfs_get_block_group_info( 41605af3e8ccSStefan Behrens &info->block_groups[c], &space); 41617fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 41621406e432SJosef Bacik dest++; 41631406e432SJosef Bacik space_args.total_spaces++; 416451788b1bSDan Rosenberg slot_count--; 41651406e432SJosef Bacik } 416651788b1bSDan Rosenberg if (!slot_count) 416751788b1bSDan Rosenberg break; 4168bf5fc093SJosef Bacik } 4169bf5fc093SJosef Bacik up_read(&info->groups_sem); 4170bf5fc093SJosef Bacik } 41711406e432SJosef Bacik 417236523e95SDavid Sterba /* 417336523e95SDavid Sterba * Add global block reserve 417436523e95SDavid Sterba */ 417536523e95SDavid Sterba if (slot_count) { 417636523e95SDavid Sterba struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 417736523e95SDavid Sterba 417836523e95SDavid Sterba spin_lock(&block_rsv->lock); 417936523e95SDavid Sterba space.total_bytes = block_rsv->size; 418036523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 418136523e95SDavid Sterba spin_unlock(&block_rsv->lock); 418236523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 418336523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 418436523e95SDavid Sterba space_args.total_spaces++; 418536523e95SDavid Sterba } 418636523e95SDavid Sterba 41872eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 41887fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 41897fde62bfSChris Mason 41907fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 41917fde62bfSChris Mason ret = -EFAULT; 41927fde62bfSChris Mason 41937fde62bfSChris Mason kfree(dest_orig); 41947fde62bfSChris Mason out: 41957fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 41961406e432SJosef Bacik ret = -EFAULT; 41971406e432SJosef Bacik 41981406e432SJosef Bacik return ret; 41991406e432SJosef Bacik } 42001406e432SJosef Bacik 4201f46b5a66SChristoph Hellwig /* 4202f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 4203f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 4204f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 4205f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 4206f46b5a66SChristoph Hellwig */ 4207f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 4208f46b5a66SChristoph Hellwig { 4209496ad9aaSAl Viro struct inode *inode = file_inode(file); 4210f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 4211f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 4212f46b5a66SChristoph Hellwig 4213f46b5a66SChristoph Hellwig trans = file->private_data; 42141ab86aedSSage Weil if (!trans) 42151ab86aedSSage Weil return -EINVAL; 4216b214107eSChristoph Hellwig file->private_data = NULL; 42179ca9ee09SSage Weil 42181ab86aedSSage Weil btrfs_end_transaction(trans, root); 42191ab86aedSSage Weil 4220a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 42219ca9ee09SSage Weil 42222a79f17eSAl Viro mnt_drop_write_file(file); 42231ab86aedSSage Weil return 0; 4224f46b5a66SChristoph Hellwig } 4225f46b5a66SChristoph Hellwig 42269a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 42279a8c28beSMiao Xie void __user *argp) 422846204592SSage Weil { 422946204592SSage Weil struct btrfs_trans_handle *trans; 423046204592SSage Weil u64 transid; 4231db5b493aSTsutomu Itoh int ret; 423246204592SSage Weil 4233d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4234ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4235ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 423698d5dc13STsutomu Itoh return PTR_ERR(trans); 4237ff7c1d33SMiao Xie 4238ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4239ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4240ff7c1d33SMiao Xie goto out; 4241ff7c1d33SMiao Xie } 424246204592SSage Weil transid = trans->transid; 4243db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 42448b2b2d3cSTsutomu Itoh if (ret) { 42458b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 4246db5b493aSTsutomu Itoh return ret; 42478b2b2d3cSTsutomu Itoh } 4248ff7c1d33SMiao Xie out: 424946204592SSage Weil if (argp) 425046204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 425146204592SSage Weil return -EFAULT; 425246204592SSage Weil return 0; 425346204592SSage Weil } 425446204592SSage Weil 42559a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 42569a8c28beSMiao Xie void __user *argp) 425746204592SSage Weil { 425846204592SSage Weil u64 transid; 425946204592SSage Weil 426046204592SSage Weil if (argp) { 426146204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 426246204592SSage Weil return -EFAULT; 426346204592SSage Weil } else { 426446204592SSage Weil transid = 0; /* current trans */ 426546204592SSage Weil } 426646204592SSage Weil return btrfs_wait_for_commit(root, transid); 426746204592SSage Weil } 426846204592SSage Weil 4269b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4270475f6387SJan Schmidt { 4271496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4272475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4273b8e95489SMiao Xie int ret; 4274475f6387SJan Schmidt 4275475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4276475f6387SJan Schmidt return -EPERM; 4277475f6387SJan Schmidt 4278475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4279475f6387SJan Schmidt if (IS_ERR(sa)) 4280475f6387SJan Schmidt return PTR_ERR(sa); 4281475f6387SJan Schmidt 4282b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4283b8e95489SMiao Xie ret = mnt_want_write_file(file); 4284b8e95489SMiao Xie if (ret) 4285b8e95489SMiao Xie goto out; 4286b8e95489SMiao Xie } 4287b8e95489SMiao Xie 4288aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 428963a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 429063a212abSStefan Behrens 0); 4291475f6387SJan Schmidt 4292475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4293475f6387SJan Schmidt ret = -EFAULT; 4294475f6387SJan Schmidt 4295b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4296b8e95489SMiao Xie mnt_drop_write_file(file); 4297b8e95489SMiao Xie out: 4298475f6387SJan Schmidt kfree(sa); 4299475f6387SJan Schmidt return ret; 4300475f6387SJan Schmidt } 4301475f6387SJan Schmidt 4302475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 4303475f6387SJan Schmidt { 4304475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4305475f6387SJan Schmidt return -EPERM; 4306475f6387SJan Schmidt 4307aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 4308475f6387SJan Schmidt } 4309475f6387SJan Schmidt 4310475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 4311475f6387SJan Schmidt void __user *arg) 4312475f6387SJan Schmidt { 4313475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4314475f6387SJan Schmidt int ret; 4315475f6387SJan Schmidt 4316475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4317475f6387SJan Schmidt return -EPERM; 4318475f6387SJan Schmidt 4319475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4320475f6387SJan Schmidt if (IS_ERR(sa)) 4321475f6387SJan Schmidt return PTR_ERR(sa); 4322475f6387SJan Schmidt 4323475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 4324475f6387SJan Schmidt 4325475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4326475f6387SJan Schmidt ret = -EFAULT; 4327475f6387SJan Schmidt 4328475f6387SJan Schmidt kfree(sa); 4329475f6387SJan Schmidt return ret; 4330475f6387SJan Schmidt } 4331475f6387SJan Schmidt 4332c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 4333b27f7c0cSDavid Sterba void __user *arg) 4334c11d2c23SStefan Behrens { 4335c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4336c11d2c23SStefan Behrens int ret; 4337c11d2c23SStefan Behrens 4338c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4339c11d2c23SStefan Behrens if (IS_ERR(sa)) 4340c11d2c23SStefan Behrens return PTR_ERR(sa); 4341c11d2c23SStefan Behrens 4342b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4343b27f7c0cSDavid Sterba kfree(sa); 4344b27f7c0cSDavid Sterba return -EPERM; 4345b27f7c0cSDavid Sterba } 4346b27f7c0cSDavid Sterba 4347b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 4348c11d2c23SStefan Behrens 4349c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4350c11d2c23SStefan Behrens ret = -EFAULT; 4351c11d2c23SStefan Behrens 4352c11d2c23SStefan Behrens kfree(sa); 4353c11d2c23SStefan Behrens return ret; 4354c11d2c23SStefan Behrens } 4355c11d2c23SStefan Behrens 43563f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 43573f6bcfbdSStefan Behrens { 43583f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 43593f6bcfbdSStefan Behrens int ret; 43603f6bcfbdSStefan Behrens 43613f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 43623f6bcfbdSStefan Behrens return -EPERM; 43633f6bcfbdSStefan Behrens 43643f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 43653f6bcfbdSStefan Behrens if (IS_ERR(p)) 43663f6bcfbdSStefan Behrens return PTR_ERR(p); 43673f6bcfbdSStefan Behrens 43683f6bcfbdSStefan Behrens switch (p->cmd) { 43693f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4370adfa97cbSIlya Dryomov if (root->fs_info->sb->s_flags & MS_RDONLY) { 4371adfa97cbSIlya Dryomov ret = -EROFS; 4372adfa97cbSIlya Dryomov goto out; 4373adfa97cbSIlya Dryomov } 43743f6bcfbdSStefan Behrens if (atomic_xchg( 43753f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 43763f6bcfbdSStefan Behrens 1)) { 4377e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 43783f6bcfbdSStefan Behrens } else { 43793f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 43803f6bcfbdSStefan Behrens atomic_set( 43813f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 43823f6bcfbdSStefan Behrens 0); 43833f6bcfbdSStefan Behrens } 43843f6bcfbdSStefan Behrens break; 43853f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 43863f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 43873f6bcfbdSStefan Behrens ret = 0; 43883f6bcfbdSStefan Behrens break; 43893f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 43903f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 43913f6bcfbdSStefan Behrens break; 43923f6bcfbdSStefan Behrens default: 43933f6bcfbdSStefan Behrens ret = -EINVAL; 43943f6bcfbdSStefan Behrens break; 43953f6bcfbdSStefan Behrens } 43963f6bcfbdSStefan Behrens 43973f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 43983f6bcfbdSStefan Behrens ret = -EFAULT; 4399adfa97cbSIlya Dryomov out: 44003f6bcfbdSStefan Behrens kfree(p); 44013f6bcfbdSStefan Behrens return ret; 44023f6bcfbdSStefan Behrens } 44033f6bcfbdSStefan Behrens 4404d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4405d7728c96SJan Schmidt { 4406d7728c96SJan Schmidt int ret = 0; 4407d7728c96SJan Schmidt int i; 4408740c3d22SChris Mason u64 rel_ptr; 4409d7728c96SJan Schmidt int size; 4410806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4411d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4412d7728c96SJan Schmidt struct btrfs_path *path; 4413d7728c96SJan Schmidt 441482b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4415d7728c96SJan Schmidt return -EPERM; 4416d7728c96SJan Schmidt 4417d7728c96SJan Schmidt path = btrfs_alloc_path(); 4418d7728c96SJan Schmidt if (!path) { 4419d7728c96SJan Schmidt ret = -ENOMEM; 4420d7728c96SJan Schmidt goto out; 4421d7728c96SJan Schmidt } 4422d7728c96SJan Schmidt 4423d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4424d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4425d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4426d7728c96SJan Schmidt ipa = NULL; 4427d7728c96SJan Schmidt goto out; 4428d7728c96SJan Schmidt } 4429d7728c96SJan Schmidt 4430d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4431d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4432d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4433d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4434d7728c96SJan Schmidt ipath = NULL; 4435d7728c96SJan Schmidt goto out; 4436d7728c96SJan Schmidt } 4437d7728c96SJan Schmidt 4438d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4439d7728c96SJan Schmidt if (ret < 0) 4440d7728c96SJan Schmidt goto out; 4441d7728c96SJan Schmidt 4442d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4443745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4444745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4445740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4446d7728c96SJan Schmidt } 4447d7728c96SJan Schmidt 4448745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 4449745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 4450d7728c96SJan Schmidt if (ret) { 4451d7728c96SJan Schmidt ret = -EFAULT; 4452d7728c96SJan Schmidt goto out; 4453d7728c96SJan Schmidt } 4454d7728c96SJan Schmidt 4455d7728c96SJan Schmidt out: 4456d7728c96SJan Schmidt btrfs_free_path(path); 4457d7728c96SJan Schmidt free_ipath(ipath); 4458d7728c96SJan Schmidt kfree(ipa); 4459d7728c96SJan Schmidt 4460d7728c96SJan Schmidt return ret; 4461d7728c96SJan Schmidt } 4462d7728c96SJan Schmidt 4463d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4464d7728c96SJan Schmidt { 4465d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4466d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4467d7728c96SJan Schmidt 4468d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4469d7728c96SJan Schmidt inodes->bytes_left -= c; 4470d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4471d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4472d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4473d7728c96SJan Schmidt inodes->elem_cnt += 3; 4474d7728c96SJan Schmidt } else { 4475d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4476d7728c96SJan Schmidt inodes->bytes_left = 0; 4477d7728c96SJan Schmidt inodes->elem_missed += 3; 4478d7728c96SJan Schmidt } 4479d7728c96SJan Schmidt 4480d7728c96SJan Schmidt return 0; 4481d7728c96SJan Schmidt } 4482d7728c96SJan Schmidt 4483d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 4484d7728c96SJan Schmidt void __user *arg) 4485d7728c96SJan Schmidt { 4486d7728c96SJan Schmidt int ret = 0; 4487d7728c96SJan Schmidt int size; 4488d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4489d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4490d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4491d7728c96SJan Schmidt 4492d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4493d7728c96SJan Schmidt return -EPERM; 4494d7728c96SJan Schmidt 4495d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 4496d7728c96SJan Schmidt if (IS_ERR(loi)) { 4497d7728c96SJan Schmidt ret = PTR_ERR(loi); 4498d7728c96SJan Schmidt loi = NULL; 4499d7728c96SJan Schmidt goto out; 4500d7728c96SJan Schmidt } 4501d7728c96SJan Schmidt 4502d7728c96SJan Schmidt path = btrfs_alloc_path(); 4503d7728c96SJan Schmidt if (!path) { 4504d7728c96SJan Schmidt ret = -ENOMEM; 4505d7728c96SJan Schmidt goto out; 4506d7728c96SJan Schmidt } 4507d7728c96SJan Schmidt 4508ee22184bSByongho Lee size = min_t(u32, loi->size, SZ_64K); 4509d7728c96SJan Schmidt inodes = init_data_container(size); 4510d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4511d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4512d7728c96SJan Schmidt inodes = NULL; 4513d7728c96SJan Schmidt goto out; 4514d7728c96SJan Schmidt } 4515d7728c96SJan Schmidt 4516df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 4517df031f07SLiu Bo build_ino_list, inodes); 4518df031f07SLiu Bo if (ret == -EINVAL) 4519d7728c96SJan Schmidt ret = -ENOENT; 4520d7728c96SJan Schmidt if (ret < 0) 4521d7728c96SJan Schmidt goto out; 4522d7728c96SJan Schmidt 4523745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 4524745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 4525d7728c96SJan Schmidt if (ret) 4526d7728c96SJan Schmidt ret = -EFAULT; 4527d7728c96SJan Schmidt 4528d7728c96SJan Schmidt out: 4529d7728c96SJan Schmidt btrfs_free_path(path); 4530425d17a2SLiu Bo vfree(inodes); 4531d7728c96SJan Schmidt kfree(loi); 4532d7728c96SJan Schmidt 4533d7728c96SJan Schmidt return ret; 4534d7728c96SJan Schmidt } 4535d7728c96SJan Schmidt 453619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4537c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4538c9e9f97bSIlya Dryomov { 4539c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4540c9e9f97bSIlya Dryomov 4541c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4542c9e9f97bSIlya Dryomov 4543837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4544837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4545837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4546837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4547a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4548a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4549837d5b6eSIlya Dryomov 4550c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4551c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4552c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 455319a39dceSIlya Dryomov 455419a39dceSIlya Dryomov if (lock) { 455519a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 455619a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 455719a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 455819a39dceSIlya Dryomov } else { 455919a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 456019a39dceSIlya Dryomov } 4561c9e9f97bSIlya Dryomov } 4562c9e9f97bSIlya Dryomov 45639ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4564c9e9f97bSIlya Dryomov { 4565496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4566c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4567c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4568c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4569ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4570c9e9f97bSIlya Dryomov int ret; 4571c9e9f97bSIlya Dryomov 4572c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4573c9e9f97bSIlya Dryomov return -EPERM; 4574c9e9f97bSIlya Dryomov 4575e54bfa31SLiu Bo ret = mnt_want_write_file(file); 45769ba1f6e4SLiu Bo if (ret) 45779ba1f6e4SLiu Bo return ret; 45789ba1f6e4SLiu Bo 4579ed0fb78fSIlya Dryomov again: 4580ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 4581c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4582c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4583ed0fb78fSIlya Dryomov need_unlock = true; 4584ed0fb78fSIlya Dryomov goto locked; 4585ed0fb78fSIlya Dryomov } 4586ed0fb78fSIlya Dryomov 4587ed0fb78fSIlya Dryomov /* 4588ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 4589ed0fb78fSIlya Dryomov * (1) some other op is running 4590ed0fb78fSIlya Dryomov * (2) balance is running 4591ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4592ed0fb78fSIlya Dryomov */ 4593ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4594ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4595ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4596ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4597ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4598ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4599ed0fb78fSIlya Dryomov goto again; 4600ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4601ed0fb78fSIlya Dryomov 4602ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4603ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4604ed0fb78fSIlya Dryomov /* this is (3) */ 4605ed0fb78fSIlya Dryomov need_unlock = false; 4606ed0fb78fSIlya Dryomov goto locked; 4607ed0fb78fSIlya Dryomov } 4608ed0fb78fSIlya Dryomov 4609ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4610ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4611ed0fb78fSIlya Dryomov goto again; 4612ed0fb78fSIlya Dryomov } else { 4613ed0fb78fSIlya Dryomov /* this is (2) */ 4614ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4615ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4616ed0fb78fSIlya Dryomov goto out; 4617ed0fb78fSIlya Dryomov } 4618ed0fb78fSIlya Dryomov } else { 4619ed0fb78fSIlya Dryomov /* this is (1) */ 4620ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4621e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4622ed0fb78fSIlya Dryomov goto out; 4623ed0fb78fSIlya Dryomov } 4624ed0fb78fSIlya Dryomov 4625ed0fb78fSIlya Dryomov locked: 4626ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 4627c9e9f97bSIlya Dryomov 4628c9e9f97bSIlya Dryomov if (arg) { 4629c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4630c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4631c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4632ed0fb78fSIlya Dryomov goto out_unlock; 4633c9e9f97bSIlya Dryomov } 4634de322263SIlya Dryomov 4635de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4636de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4637de322263SIlya Dryomov ret = -ENOTCONN; 4638de322263SIlya Dryomov goto out_bargs; 4639de322263SIlya Dryomov } 4640de322263SIlya Dryomov 4641de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4642de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4643de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4644de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4645de322263SIlya Dryomov 4646de322263SIlya Dryomov goto do_balance; 4647de322263SIlya Dryomov } 4648c9e9f97bSIlya Dryomov } else { 4649c9e9f97bSIlya Dryomov bargs = NULL; 4650c9e9f97bSIlya Dryomov } 4651c9e9f97bSIlya Dryomov 4652ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4653837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4654837d5b6eSIlya Dryomov goto out_bargs; 4655837d5b6eSIlya Dryomov } 4656837d5b6eSIlya Dryomov 46578d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4658c9e9f97bSIlya Dryomov if (!bctl) { 4659c9e9f97bSIlya Dryomov ret = -ENOMEM; 4660c9e9f97bSIlya Dryomov goto out_bargs; 4661c9e9f97bSIlya Dryomov } 4662c9e9f97bSIlya Dryomov 4663c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4664c9e9f97bSIlya Dryomov if (arg) { 4665c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4666c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4667c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4668c9e9f97bSIlya Dryomov 4669c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4670f43ffb60SIlya Dryomov } else { 4671f43ffb60SIlya Dryomov /* balance everything - no filters */ 4672f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4673c9e9f97bSIlya Dryomov } 4674c9e9f97bSIlya Dryomov 46758eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 46768eb93459SDavid Sterba ret = -EINVAL; 46770f89abf5SChristian Engelmayer goto out_bctl; 46788eb93459SDavid Sterba } 46798eb93459SDavid Sterba 4680de322263SIlya Dryomov do_balance: 4681c9e9f97bSIlya Dryomov /* 4682ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 4683ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4684ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4685ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 4686ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 4687c9e9f97bSIlya Dryomov */ 4688ed0fb78fSIlya Dryomov need_unlock = false; 4689ed0fb78fSIlya Dryomov 4690ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 46910f89abf5SChristian Engelmayer bctl = NULL; 4692ed0fb78fSIlya Dryomov 4693c9e9f97bSIlya Dryomov if (arg) { 4694c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4695c9e9f97bSIlya Dryomov ret = -EFAULT; 4696c9e9f97bSIlya Dryomov } 4697c9e9f97bSIlya Dryomov 46980f89abf5SChristian Engelmayer out_bctl: 46990f89abf5SChristian Engelmayer kfree(bctl); 4700c9e9f97bSIlya Dryomov out_bargs: 4701c9e9f97bSIlya Dryomov kfree(bargs); 4702ed0fb78fSIlya Dryomov out_unlock: 4703c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4704c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4705ed0fb78fSIlya Dryomov if (need_unlock) 4706ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 4707ed0fb78fSIlya Dryomov out: 4708e54bfa31SLiu Bo mnt_drop_write_file(file); 4709c9e9f97bSIlya Dryomov return ret; 4710c9e9f97bSIlya Dryomov } 4711c9e9f97bSIlya Dryomov 4712837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4713837d5b6eSIlya Dryomov { 4714837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4715837d5b6eSIlya Dryomov return -EPERM; 4716837d5b6eSIlya Dryomov 4717837d5b6eSIlya Dryomov switch (cmd) { 4718837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 4719837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 4720a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 4721a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 4722837d5b6eSIlya Dryomov } 4723837d5b6eSIlya Dryomov 4724837d5b6eSIlya Dryomov return -EINVAL; 4725837d5b6eSIlya Dryomov } 4726837d5b6eSIlya Dryomov 472719a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 472819a39dceSIlya Dryomov void __user *arg) 472919a39dceSIlya Dryomov { 473019a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 473119a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 473219a39dceSIlya Dryomov int ret = 0; 473319a39dceSIlya Dryomov 473419a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 473519a39dceSIlya Dryomov return -EPERM; 473619a39dceSIlya Dryomov 473719a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 473819a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 473919a39dceSIlya Dryomov ret = -ENOTCONN; 474019a39dceSIlya Dryomov goto out; 474119a39dceSIlya Dryomov } 474219a39dceSIlya Dryomov 47438d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 474419a39dceSIlya Dryomov if (!bargs) { 474519a39dceSIlya Dryomov ret = -ENOMEM; 474619a39dceSIlya Dryomov goto out; 474719a39dceSIlya Dryomov } 474819a39dceSIlya Dryomov 474919a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 475019a39dceSIlya Dryomov 475119a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 475219a39dceSIlya Dryomov ret = -EFAULT; 475319a39dceSIlya Dryomov 475419a39dceSIlya Dryomov kfree(bargs); 475519a39dceSIlya Dryomov out: 475619a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 475719a39dceSIlya Dryomov return ret; 475819a39dceSIlya Dryomov } 475919a39dceSIlya Dryomov 4760905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 47615d13a37bSArne Jansen { 4762496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47635d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 47645d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 47655d13a37bSArne Jansen int ret; 47665d13a37bSArne Jansen int err; 47675d13a37bSArne Jansen 47685d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47695d13a37bSArne Jansen return -EPERM; 47705d13a37bSArne Jansen 4771905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4772905b0ddaSMiao Xie if (ret) 4773905b0ddaSMiao Xie return ret; 47745d13a37bSArne Jansen 47755d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4776905b0ddaSMiao Xie if (IS_ERR(sa)) { 4777905b0ddaSMiao Xie ret = PTR_ERR(sa); 4778905b0ddaSMiao Xie goto drop_write; 4779905b0ddaSMiao Xie } 47805d13a37bSArne Jansen 47817708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 478292f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 47835d13a37bSArne Jansen if (IS_ERR(trans)) { 47845d13a37bSArne Jansen ret = PTR_ERR(trans); 47855d13a37bSArne Jansen goto out; 47865d13a37bSArne Jansen } 47875d13a37bSArne Jansen 47885d13a37bSArne Jansen switch (sa->cmd) { 47895d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 47905d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 47915d13a37bSArne Jansen break; 47925d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 47935d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 47945d13a37bSArne Jansen break; 47955d13a37bSArne Jansen default: 47965d13a37bSArne Jansen ret = -EINVAL; 47975d13a37bSArne Jansen break; 47985d13a37bSArne Jansen } 47995d13a37bSArne Jansen 480092f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 48015d13a37bSArne Jansen if (err && !ret) 48025d13a37bSArne Jansen ret = err; 48035d13a37bSArne Jansen out: 48045d13a37bSArne Jansen kfree(sa); 48057708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 4806905b0ddaSMiao Xie drop_write: 4807905b0ddaSMiao Xie mnt_drop_write_file(file); 48085d13a37bSArne Jansen return ret; 48095d13a37bSArne Jansen } 48105d13a37bSArne Jansen 4811905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 48125d13a37bSArne Jansen { 4813496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 48145d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 48155d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48165d13a37bSArne Jansen int ret; 48175d13a37bSArne Jansen int err; 48185d13a37bSArne Jansen 48195d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48205d13a37bSArne Jansen return -EPERM; 48215d13a37bSArne Jansen 4822905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4823905b0ddaSMiao Xie if (ret) 4824905b0ddaSMiao Xie return ret; 48255d13a37bSArne Jansen 48265d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4827905b0ddaSMiao Xie if (IS_ERR(sa)) { 4828905b0ddaSMiao Xie ret = PTR_ERR(sa); 4829905b0ddaSMiao Xie goto drop_write; 4830905b0ddaSMiao Xie } 48315d13a37bSArne Jansen 48325d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48335d13a37bSArne Jansen if (IS_ERR(trans)) { 48345d13a37bSArne Jansen ret = PTR_ERR(trans); 48355d13a37bSArne Jansen goto out; 48365d13a37bSArne Jansen } 48375d13a37bSArne Jansen 48385d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 48395d13a37bSArne Jansen if (sa->assign) { 48405d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 48415d13a37bSArne Jansen sa->src, sa->dst); 48425d13a37bSArne Jansen } else { 48435d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 48445d13a37bSArne Jansen sa->src, sa->dst); 48455d13a37bSArne Jansen } 48465d13a37bSArne Jansen 4847e082f563SQu Wenruo /* update qgroup status and info */ 4848e082f563SQu Wenruo err = btrfs_run_qgroups(trans, root->fs_info); 4849e082f563SQu Wenruo if (err < 0) 4850a4553fefSAnand Jain btrfs_std_error(root->fs_info, ret, 4851e082f563SQu Wenruo "failed to update qgroup status and info\n"); 48525d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 48535d13a37bSArne Jansen if (err && !ret) 48545d13a37bSArne Jansen ret = err; 48555d13a37bSArne Jansen 48565d13a37bSArne Jansen out: 48575d13a37bSArne Jansen kfree(sa); 4858905b0ddaSMiao Xie drop_write: 4859905b0ddaSMiao Xie mnt_drop_write_file(file); 48605d13a37bSArne Jansen return ret; 48615d13a37bSArne Jansen } 48625d13a37bSArne Jansen 4863905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 48645d13a37bSArne Jansen { 4865496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 48665d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 48675d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48685d13a37bSArne Jansen int ret; 48695d13a37bSArne Jansen int err; 48705d13a37bSArne Jansen 48715d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48725d13a37bSArne Jansen return -EPERM; 48735d13a37bSArne Jansen 4874905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4875905b0ddaSMiao Xie if (ret) 4876905b0ddaSMiao Xie return ret; 48775d13a37bSArne Jansen 48785d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4879905b0ddaSMiao Xie if (IS_ERR(sa)) { 4880905b0ddaSMiao Xie ret = PTR_ERR(sa); 4881905b0ddaSMiao Xie goto drop_write; 4882905b0ddaSMiao Xie } 48835d13a37bSArne Jansen 4884d86e56cfSMiao Xie if (!sa->qgroupid) { 4885d86e56cfSMiao Xie ret = -EINVAL; 4886d86e56cfSMiao Xie goto out; 4887d86e56cfSMiao Xie } 4888d86e56cfSMiao Xie 48895d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48905d13a37bSArne Jansen if (IS_ERR(trans)) { 48915d13a37bSArne Jansen ret = PTR_ERR(trans); 48925d13a37bSArne Jansen goto out; 48935d13a37bSArne Jansen } 48945d13a37bSArne Jansen 48955d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 48965d13a37bSArne Jansen if (sa->create) { 48974087cf24SDongsheng Yang ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid); 48985d13a37bSArne Jansen } else { 48995d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 49005d13a37bSArne Jansen } 49015d13a37bSArne Jansen 49025d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 49035d13a37bSArne Jansen if (err && !ret) 49045d13a37bSArne Jansen ret = err; 49055d13a37bSArne Jansen 49065d13a37bSArne Jansen out: 49075d13a37bSArne Jansen kfree(sa); 4908905b0ddaSMiao Xie drop_write: 4909905b0ddaSMiao Xie mnt_drop_write_file(file); 49105d13a37bSArne Jansen return ret; 49115d13a37bSArne Jansen } 49125d13a37bSArne Jansen 4913905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 49145d13a37bSArne Jansen { 4915496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 49165d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 49175d13a37bSArne Jansen struct btrfs_trans_handle *trans; 49185d13a37bSArne Jansen int ret; 49195d13a37bSArne Jansen int err; 49205d13a37bSArne Jansen u64 qgroupid; 49215d13a37bSArne Jansen 49225d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 49235d13a37bSArne Jansen return -EPERM; 49245d13a37bSArne Jansen 4925905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4926905b0ddaSMiao Xie if (ret) 4927905b0ddaSMiao Xie return ret; 49285d13a37bSArne Jansen 49295d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4930905b0ddaSMiao Xie if (IS_ERR(sa)) { 4931905b0ddaSMiao Xie ret = PTR_ERR(sa); 4932905b0ddaSMiao Xie goto drop_write; 4933905b0ddaSMiao Xie } 49345d13a37bSArne Jansen 49355d13a37bSArne Jansen trans = btrfs_join_transaction(root); 49365d13a37bSArne Jansen if (IS_ERR(trans)) { 49375d13a37bSArne Jansen ret = PTR_ERR(trans); 49385d13a37bSArne Jansen goto out; 49395d13a37bSArne Jansen } 49405d13a37bSArne Jansen 49415d13a37bSArne Jansen qgroupid = sa->qgroupid; 49425d13a37bSArne Jansen if (!qgroupid) { 49435d13a37bSArne Jansen /* take the current subvol as qgroup */ 49445d13a37bSArne Jansen qgroupid = root->root_key.objectid; 49455d13a37bSArne Jansen } 49465d13a37bSArne Jansen 49475d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 49485d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 49495d13a37bSArne Jansen 49505d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 49515d13a37bSArne Jansen if (err && !ret) 49525d13a37bSArne Jansen ret = err; 49535d13a37bSArne Jansen 49545d13a37bSArne Jansen out: 49555d13a37bSArne Jansen kfree(sa); 4956905b0ddaSMiao Xie drop_write: 4957905b0ddaSMiao Xie mnt_drop_write_file(file); 49585d13a37bSArne Jansen return ret; 49595d13a37bSArne Jansen } 49605d13a37bSArne Jansen 49612f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 49622f232036SJan Schmidt { 49636d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 49642f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49652f232036SJan Schmidt int ret; 49662f232036SJan Schmidt 49672f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49682f232036SJan Schmidt return -EPERM; 49692f232036SJan Schmidt 49702f232036SJan Schmidt ret = mnt_want_write_file(file); 49712f232036SJan Schmidt if (ret) 49722f232036SJan Schmidt return ret; 49732f232036SJan Schmidt 49742f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 49752f232036SJan Schmidt if (IS_ERR(qsa)) { 49762f232036SJan Schmidt ret = PTR_ERR(qsa); 49772f232036SJan Schmidt goto drop_write; 49782f232036SJan Schmidt } 49792f232036SJan Schmidt 49802f232036SJan Schmidt if (qsa->flags) { 49812f232036SJan Schmidt ret = -EINVAL; 49822f232036SJan Schmidt goto out; 49832f232036SJan Schmidt } 49842f232036SJan Schmidt 49852f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 49862f232036SJan Schmidt 49872f232036SJan Schmidt out: 49882f232036SJan Schmidt kfree(qsa); 49892f232036SJan Schmidt drop_write: 49902f232036SJan Schmidt mnt_drop_write_file(file); 49912f232036SJan Schmidt return ret; 49922f232036SJan Schmidt } 49932f232036SJan Schmidt 49942f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 49952f232036SJan Schmidt { 49966d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 49972f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49982f232036SJan Schmidt int ret = 0; 49992f232036SJan Schmidt 50002f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 50012f232036SJan Schmidt return -EPERM; 50022f232036SJan Schmidt 50038d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 50042f232036SJan Schmidt if (!qsa) 50052f232036SJan Schmidt return -ENOMEM; 50062f232036SJan Schmidt 50072f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 50082f232036SJan Schmidt qsa->flags = 1; 50092f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 50102f232036SJan Schmidt } 50112f232036SJan Schmidt 50122f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 50132f232036SJan Schmidt ret = -EFAULT; 50142f232036SJan Schmidt 50152f232036SJan Schmidt kfree(qsa); 50162f232036SJan Schmidt return ret; 50172f232036SJan Schmidt } 50182f232036SJan Schmidt 501957254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 502057254b6eSJan Schmidt { 502154563d41SAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 502257254b6eSJan Schmidt 502357254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 502457254b6eSJan Schmidt return -EPERM; 502557254b6eSJan Schmidt 502657254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 502757254b6eSJan Schmidt } 502857254b6eSJan Schmidt 5029abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 5030abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 50318ea05e3aSAlexander Block { 5032496ad9aaSAl Viro struct inode *inode = file_inode(file); 50338ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 50348ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 50358ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 503604b285f3SDeepa Dinamani struct timespec ct = current_fs_time(inode->i_sb); 50378ea05e3aSAlexander Block int ret = 0; 5038dd5f9615SStefan Behrens int received_uuid_changed; 50398ea05e3aSAlexander Block 5040bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5041bd60ea0fSDavid Sterba return -EPERM; 5042bd60ea0fSDavid Sterba 50438ea05e3aSAlexander Block ret = mnt_want_write_file(file); 50448ea05e3aSAlexander Block if (ret < 0) 50458ea05e3aSAlexander Block return ret; 50468ea05e3aSAlexander Block 50478ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 50488ea05e3aSAlexander Block 50498ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 50508ea05e3aSAlexander Block ret = -EINVAL; 50518ea05e3aSAlexander Block goto out; 50528ea05e3aSAlexander Block } 50538ea05e3aSAlexander Block 50548ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 50558ea05e3aSAlexander Block ret = -EROFS; 50568ea05e3aSAlexander Block goto out; 50578ea05e3aSAlexander Block } 50588ea05e3aSAlexander Block 5059dd5f9615SStefan Behrens /* 5060dd5f9615SStefan Behrens * 1 - root item 5061dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5062dd5f9615SStefan Behrens */ 5063dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 50648ea05e3aSAlexander Block if (IS_ERR(trans)) { 50658ea05e3aSAlexander Block ret = PTR_ERR(trans); 50668ea05e3aSAlexander Block trans = NULL; 50678ea05e3aSAlexander Block goto out; 50688ea05e3aSAlexander Block } 50698ea05e3aSAlexander Block 50708ea05e3aSAlexander Block sa->rtransid = trans->transid; 50718ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 50728ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 50738ea05e3aSAlexander Block 5074dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5075dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5076dd5f9615SStefan Behrens if (received_uuid_changed && 5077dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 5078dd5f9615SStefan Behrens btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 5079dd5f9615SStefan Behrens root_item->received_uuid, 5080dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5081dd5f9615SStefan Behrens root->root_key.objectid); 50828ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 50838ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 50848ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 50853cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 50863cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 50873cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 50883cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 50898ea05e3aSAlexander Block 50908ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 50918ea05e3aSAlexander Block &root->root_key, &root->root_item); 50928ea05e3aSAlexander Block if (ret < 0) { 50938ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 50948ea05e3aSAlexander Block goto out; 5095dd5f9615SStefan Behrens } 5096dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 5097dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 5098dd5f9615SStefan Behrens sa->uuid, 5099dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5100dd5f9615SStefan Behrens root->root_key.objectid); 5101dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 5102dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 5103dd5f9615SStefan Behrens goto out; 5104dd5f9615SStefan Behrens } 5105dd5f9615SStefan Behrens } 51068ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 5107dd5f9615SStefan Behrens if (ret < 0) { 5108dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 51098ea05e3aSAlexander Block goto out; 51108ea05e3aSAlexander Block } 51118ea05e3aSAlexander Block 5112abccd00fSHugo Mills out: 5113abccd00fSHugo Mills up_write(&root->fs_info->subvol_sem); 5114abccd00fSHugo Mills mnt_drop_write_file(file); 5115abccd00fSHugo Mills return ret; 5116abccd00fSHugo Mills } 5117abccd00fSHugo Mills 5118abccd00fSHugo Mills #ifdef CONFIG_64BIT 5119abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5120abccd00fSHugo Mills void __user *arg) 5121abccd00fSHugo Mills { 5122abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5123abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5124abccd00fSHugo Mills int ret = 0; 5125abccd00fSHugo Mills 5126abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 5127abccd00fSHugo Mills if (IS_ERR(args32)) { 5128abccd00fSHugo Mills ret = PTR_ERR(args32); 5129abccd00fSHugo Mills args32 = NULL; 5130abccd00fSHugo Mills goto out; 5131abccd00fSHugo Mills } 5132abccd00fSHugo Mills 51338d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 513484dbeb87SDan Carpenter if (!args64) { 513584dbeb87SDan Carpenter ret = -ENOMEM; 5136abccd00fSHugo Mills goto out; 5137abccd00fSHugo Mills } 5138abccd00fSHugo Mills 5139abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5140abccd00fSHugo Mills args64->stransid = args32->stransid; 5141abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5142abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5143abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5144abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5145abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5146abccd00fSHugo Mills args64->flags = args32->flags; 5147abccd00fSHugo Mills 5148abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5149abccd00fSHugo Mills if (ret) 5150abccd00fSHugo Mills goto out; 5151abccd00fSHugo Mills 5152abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5153abccd00fSHugo Mills args32->stransid = args64->stransid; 5154abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5155abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5156abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5157abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5158abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5159abccd00fSHugo Mills args32->flags = args64->flags; 5160abccd00fSHugo Mills 5161abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5162abccd00fSHugo Mills if (ret) 5163abccd00fSHugo Mills ret = -EFAULT; 5164abccd00fSHugo Mills 5165abccd00fSHugo Mills out: 5166abccd00fSHugo Mills kfree(args32); 5167abccd00fSHugo Mills kfree(args64); 5168abccd00fSHugo Mills return ret; 5169abccd00fSHugo Mills } 5170abccd00fSHugo Mills #endif 5171abccd00fSHugo Mills 5172abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5173abccd00fSHugo Mills void __user *arg) 5174abccd00fSHugo Mills { 5175abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5176abccd00fSHugo Mills int ret = 0; 5177abccd00fSHugo Mills 5178abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 5179abccd00fSHugo Mills if (IS_ERR(sa)) { 5180abccd00fSHugo Mills ret = PTR_ERR(sa); 5181abccd00fSHugo Mills sa = NULL; 5182abccd00fSHugo Mills goto out; 5183abccd00fSHugo Mills } 5184abccd00fSHugo Mills 5185abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5186abccd00fSHugo Mills 5187abccd00fSHugo Mills if (ret) 5188abccd00fSHugo Mills goto out; 5189abccd00fSHugo Mills 51908ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 51918ea05e3aSAlexander Block if (ret) 51928ea05e3aSAlexander Block ret = -EFAULT; 51938ea05e3aSAlexander Block 51948ea05e3aSAlexander Block out: 51958ea05e3aSAlexander Block kfree(sa); 51968ea05e3aSAlexander Block return ret; 51978ea05e3aSAlexander Block } 51988ea05e3aSAlexander Block 5199867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 5200867ab667Sjeff.liu { 52016d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5202a1b83ac5SAnand Jain size_t len; 5203867ab667Sjeff.liu int ret; 5204a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5205a1b83ac5SAnand Jain 5206a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 5207a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 5208a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 5209a1b83ac5SAnand Jain 5210a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5211867ab667Sjeff.liu 5212867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 5213efe120a0SFrank Holton btrfs_warn(root->fs_info, 5214efe120a0SFrank Holton "label is too long, return the first %zu bytes", --len); 5215867ab667Sjeff.liu } 5216867ab667Sjeff.liu 5217867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5218867ab667Sjeff.liu 5219867ab667Sjeff.liu return ret ? -EFAULT : 0; 5220867ab667Sjeff.liu } 5221867ab667Sjeff.liu 5222a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5223a8bfd4abSjeff.liu { 52246d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5225a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 5226a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5227a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5228a8bfd4abSjeff.liu int ret; 5229a8bfd4abSjeff.liu 5230a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5231a8bfd4abSjeff.liu return -EPERM; 5232a8bfd4abSjeff.liu 5233a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5234a8bfd4abSjeff.liu return -EFAULT; 5235a8bfd4abSjeff.liu 5236a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 5237efe120a0SFrank Holton btrfs_err(root->fs_info, "unable to set label with more than %d bytes", 5238a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5239a8bfd4abSjeff.liu return -EINVAL; 5240a8bfd4abSjeff.liu } 5241a8bfd4abSjeff.liu 5242a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5243a8bfd4abSjeff.liu if (ret) 5244a8bfd4abSjeff.liu return ret; 5245a8bfd4abSjeff.liu 5246a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5247a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5248a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5249a8bfd4abSjeff.liu goto out_unlock; 5250a8bfd4abSjeff.liu } 5251a8bfd4abSjeff.liu 5252a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 5253a8bfd4abSjeff.liu strcpy(super_block->label, label); 5254a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 5255d0270acaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 5256a8bfd4abSjeff.liu 5257a8bfd4abSjeff.liu out_unlock: 5258a8bfd4abSjeff.liu mnt_drop_write_file(file); 5259a8bfd4abSjeff.liu return ret; 5260a8bfd4abSjeff.liu } 5261a8bfd4abSjeff.liu 52622eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 52632eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 52642eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 52652eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 52662eaa055fSJeff Mahoney 5267d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 52682eaa055fSJeff Mahoney { 52694d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 52702eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 52712eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 52722eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 52732eaa055fSJeff Mahoney }; 52742eaa055fSJeff Mahoney 52752eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52762eaa055fSJeff Mahoney return -EFAULT; 52772eaa055fSJeff Mahoney 52782eaa055fSJeff Mahoney return 0; 52792eaa055fSJeff Mahoney } 52802eaa055fSJeff Mahoney 52812eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 52822eaa055fSJeff Mahoney { 52832eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 52842eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 52852eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 52862eaa055fSJeff Mahoney 52872eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 52882eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 52892eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 52902eaa055fSJeff Mahoney 52912eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52922eaa055fSJeff Mahoney return -EFAULT; 52932eaa055fSJeff Mahoney 52942eaa055fSJeff Mahoney return 0; 52952eaa055fSJeff Mahoney } 52962eaa055fSJeff Mahoney 52973b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root, 52983b02a68aSJeff Mahoney enum btrfs_feature_set set, 52992eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 53002eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 53012eaa055fSJeff Mahoney { 53023b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 53033b02a68aSJeff Mahoney char *names; 53042eaa055fSJeff Mahoney u64 disallowed, unsupported; 53052eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 53062eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 53072eaa055fSJeff Mahoney 53082eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 53092eaa055fSJeff Mahoney if (unsupported) { 53103b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 53113b02a68aSJeff Mahoney if (names) { 53123b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 53133b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 53143b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53153b02a68aSJeff Mahoney kfree(names); 53163b02a68aSJeff Mahoney } else 53172eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 53182eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 53192eaa055fSJeff Mahoney type, unsupported); 53202eaa055fSJeff Mahoney return -EOPNOTSUPP; 53212eaa055fSJeff Mahoney } 53222eaa055fSJeff Mahoney 53232eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 53242eaa055fSJeff Mahoney if (disallowed) { 53253b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53263b02a68aSJeff Mahoney if (names) { 53273b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 53283b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 53293b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53303b02a68aSJeff Mahoney kfree(names); 53313b02a68aSJeff Mahoney } else 53322eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 53332eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 53342eaa055fSJeff Mahoney type, disallowed); 53352eaa055fSJeff Mahoney return -EPERM; 53362eaa055fSJeff Mahoney } 53372eaa055fSJeff Mahoney 53382eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 53392eaa055fSJeff Mahoney if (disallowed) { 53403b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53413b02a68aSJeff Mahoney if (names) { 53423b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 53433b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 53443b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53453b02a68aSJeff Mahoney kfree(names); 53463b02a68aSJeff Mahoney } else 53472eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 53482eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 53492eaa055fSJeff Mahoney type, disallowed); 53502eaa055fSJeff Mahoney return -EPERM; 53512eaa055fSJeff Mahoney } 53522eaa055fSJeff Mahoney 53532eaa055fSJeff Mahoney return 0; 53542eaa055fSJeff Mahoney } 53552eaa055fSJeff Mahoney 53562eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base) \ 53573b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 53582eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 53592eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 53602eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 53612eaa055fSJeff Mahoney 53622eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 53632eaa055fSJeff Mahoney { 53642eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 53652eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 53662eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 53672eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 53682eaa055fSJeff Mahoney u64 newflags; 53692eaa055fSJeff Mahoney int ret; 53702eaa055fSJeff Mahoney 53712eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 53722eaa055fSJeff Mahoney return -EPERM; 53732eaa055fSJeff Mahoney 53742eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 53752eaa055fSJeff Mahoney return -EFAULT; 53762eaa055fSJeff Mahoney 53772eaa055fSJeff Mahoney /* Nothing to do */ 53782eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 53792eaa055fSJeff Mahoney !flags[0].incompat_flags) 53802eaa055fSJeff Mahoney return 0; 53812eaa055fSJeff Mahoney 53822eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_flags, 53832eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 53842eaa055fSJeff Mahoney if (ret) 53852eaa055fSJeff Mahoney return ret; 53862eaa055fSJeff Mahoney 53872eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_ro_flags, 53882eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 53892eaa055fSJeff Mahoney if (ret) 53902eaa055fSJeff Mahoney return ret; 53912eaa055fSJeff Mahoney 53922eaa055fSJeff Mahoney ret = check_feature(root, flags[0].incompat_flags, 53932eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 53942eaa055fSJeff Mahoney if (ret) 53952eaa055fSJeff Mahoney return ret; 53962eaa055fSJeff Mahoney 53978051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 53982eaa055fSJeff Mahoney if (IS_ERR(trans)) 53992eaa055fSJeff Mahoney return PTR_ERR(trans); 54002eaa055fSJeff Mahoney 54012eaa055fSJeff Mahoney spin_lock(&root->fs_info->super_lock); 54022eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 54032eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 54042eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 54052eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 54062eaa055fSJeff Mahoney 54072eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 54082eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 54092eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 54102eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 54112eaa055fSJeff Mahoney 54122eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 54132eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 54142eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 54152eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 54162eaa055fSJeff Mahoney spin_unlock(&root->fs_info->super_lock); 54172eaa055fSJeff Mahoney 5418d0270acaSJeff Mahoney return btrfs_commit_transaction(trans, root); 54192eaa055fSJeff Mahoney } 54202eaa055fSJeff Mahoney 5421f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5422f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5423f46b5a66SChristoph Hellwig { 5424496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 54254bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5426f46b5a66SChristoph Hellwig 5427f46b5a66SChristoph Hellwig switch (cmd) { 54286cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 54296cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 54306cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 54316cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 54326cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 54336cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5434f7039b1dSLi Dongyang case FITRIM: 5435f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5436f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5437fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5438fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5439fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 54403de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5441fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 54426f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 54436f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 544476dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 544576dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 54460caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 54470caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 54480caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 54490caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 54506ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 54516ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5452f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 54531e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 54541e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 54551e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5456f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5457198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5458f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 54594bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 5460f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5461da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 5462475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 5463475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 5464475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 5465475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 5466f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 54679ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5468f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 5469f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 5470f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 5471f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 5472ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5473ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5474cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5475cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5476ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5477ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5478d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5479d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5480d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5481d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 54821406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 54831406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 54849b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 54859b199859SFilipe David Borba Manana int ret; 54869b199859SFilipe David Borba Manana 54876c255e67SMiao Xie ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 54889b199859SFilipe David Borba Manana if (ret) 54899b199859SFilipe David Borba Manana return ret; 5490ddb52f4fSAl Viro ret = btrfs_sync_fs(file_inode(file)->i_sb, 1); 54912fad4e83SDavid Sterba /* 54922fad4e83SDavid Sterba * The transaction thread may want to do more work, 54932fad4e83SDavid Sterba * namely it pokes the cleaner ktread that will start 54942fad4e83SDavid Sterba * processing uncleaned subvols. 54952fad4e83SDavid Sterba */ 54962fad4e83SDavid Sterba wake_up_process(root->fs_info->transaction_kthread); 54979b199859SFilipe David Borba Manana return ret; 54989b199859SFilipe David Borba Manana } 549946204592SSage Weil case BTRFS_IOC_START_SYNC: 55009a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 550146204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 55029a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 5503475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5504b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5505475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 5506475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 5507475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 5508475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 5509c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 55109ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5511837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 5512837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 551319a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 551419a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 55158ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 55168ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5517abccd00fSHugo Mills #ifdef CONFIG_64BIT 5518abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5519abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5520abccd00fSHugo Mills #endif 552131db9f7cSAlexander Block case BTRFS_IOC_SEND: 552231db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 5523c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 5524b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 55255d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5526905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 55275d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5528905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 55295d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5530905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 55315d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5532905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 55332f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 55342f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 55352f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 55362f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 553757254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 553857254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 55393f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 55403f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 5541867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5542867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5543a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5544a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 55452eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5546d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 55472eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 55482eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 55492eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 55502eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5551f46b5a66SChristoph Hellwig } 5552f46b5a66SChristoph Hellwig 5553f46b5a66SChristoph Hellwig return -ENOTTY; 5554f46b5a66SChristoph Hellwig } 55554c63c245SLuke Dashjr 55564c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 55574c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 55584c63c245SLuke Dashjr { 55594c63c245SLuke Dashjr switch (cmd) { 55604c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 55614c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 55624c63c245SLuke Dashjr break; 55634c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 55644c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 55654c63c245SLuke Dashjr break; 55664c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 55674c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 55684c63c245SLuke Dashjr break; 55694c63c245SLuke Dashjr default: 55704c63c245SLuke Dashjr return -ENOIOCTLCMD; 55714c63c245SLuke Dashjr } 55724c63c245SLuke Dashjr 55734c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 55744c63c245SLuke Dashjr } 55754c63c245SLuke Dashjr #endif 5576