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" 62f46b5a66SChristoph Hellwig 63abccd00fSHugo Mills #ifdef CONFIG_64BIT 64abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 65abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 66abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 67abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 68abccd00fSHugo Mills */ 69abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 70abccd00fSHugo Mills __u64 sec; 71abccd00fSHugo Mills __u32 nsec; 72abccd00fSHugo Mills } __attribute__ ((__packed__)); 73abccd00fSHugo Mills 74abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 75abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 76abccd00fSHugo Mills __u64 stransid; /* in */ 77abccd00fSHugo Mills __u64 rtransid; /* out */ 78abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 79abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 80abccd00fSHugo Mills __u64 flags; /* in */ 81abccd00fSHugo Mills __u64 reserved[16]; /* in */ 82abccd00fSHugo Mills } __attribute__ ((__packed__)); 83abccd00fSHugo Mills 84abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 85abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 86abccd00fSHugo Mills #endif 87abccd00fSHugo Mills 88abccd00fSHugo Mills 89416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 90416161dbSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff); 91416161dbSMark Fasheh 926cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 936cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 946cbff00fSChristoph Hellwig { 956cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 966cbff00fSChristoph Hellwig return flags; 976cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 986cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 996cbff00fSChristoph Hellwig else 1006cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1016cbff00fSChristoph Hellwig } 102f46b5a66SChristoph Hellwig 1036cbff00fSChristoph Hellwig /* 1046cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 1056cbff00fSChristoph Hellwig */ 1066cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 1076cbff00fSChristoph Hellwig { 1086cbff00fSChristoph Hellwig unsigned int iflags = 0; 1096cbff00fSChristoph Hellwig 1106cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1116cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1126cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1136cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1146cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1156cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1166cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1176cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1186cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1196cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1206cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1216cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 122d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 123d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 124d0092bddSLi Zefan 125d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 126d0092bddSLi Zefan iflags |= FS_COMPR_FL; 127d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 128d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 1296cbff00fSChristoph Hellwig 1306cbff00fSChristoph Hellwig return iflags; 1316cbff00fSChristoph Hellwig } 1326cbff00fSChristoph Hellwig 1336cbff00fSChristoph Hellwig /* 1346cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1356cbff00fSChristoph Hellwig */ 1366cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1376cbff00fSChristoph Hellwig { 1386cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1396cbff00fSChristoph Hellwig 1406cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 1416cbff00fSChristoph Hellwig 1426cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1436cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 1446cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1456cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1466cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1476cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1486cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1496cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1506cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1516cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1526cbff00fSChristoph Hellwig } 1536cbff00fSChristoph Hellwig 1546cbff00fSChristoph Hellwig /* 1556cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1566cbff00fSChristoph Hellwig * 157e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1586cbff00fSChristoph Hellwig */ 1596cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1606cbff00fSChristoph Hellwig { 1610b4dcea5SChris Mason unsigned int flags; 1620b4dcea5SChris Mason 1630b4dcea5SChris Mason if (!dir) 1640b4dcea5SChris Mason return; 1650b4dcea5SChris Mason 1660b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1676cbff00fSChristoph Hellwig 168e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 169e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 170e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 171e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 172e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 173e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 174e27425d6SJosef Bacik } 1756cbff00fSChristoph Hellwig 176213490b3SLiu Bo if (flags & BTRFS_INODE_NODATACOW) { 177e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 178213490b3SLiu Bo if (S_ISREG(inode->i_mode)) 179213490b3SLiu Bo BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 180213490b3SLiu Bo } 181e27425d6SJosef Bacik 1826cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1836cbff00fSChristoph Hellwig } 1846cbff00fSChristoph Hellwig 1856cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1866cbff00fSChristoph Hellwig { 187496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1886cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1896cbff00fSChristoph Hellwig 1906cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1916cbff00fSChristoph Hellwig return -EFAULT; 1926cbff00fSChristoph Hellwig return 0; 1936cbff00fSChristoph Hellwig } 1946cbff00fSChristoph Hellwig 19575e7cb7fSLiu Bo static int check_flags(unsigned int flags) 19675e7cb7fSLiu Bo { 19775e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 19875e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 19975e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 200e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 201e1e8fb6aSLi Zefan FS_NOCOW_FL)) 20275e7cb7fSLiu Bo return -EOPNOTSUPP; 20375e7cb7fSLiu Bo 20475e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 20575e7cb7fSLiu Bo return -EINVAL; 20675e7cb7fSLiu Bo 20775e7cb7fSLiu Bo return 0; 20875e7cb7fSLiu Bo } 20975e7cb7fSLiu Bo 2106cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 2116cbff00fSChristoph Hellwig { 212496ad9aaSAl Viro struct inode *inode = file_inode(file); 2136cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 2146cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 2156cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2166cbff00fSChristoph Hellwig unsigned int flags, oldflags; 2176cbff00fSChristoph Hellwig int ret; 218f062abf0SLi Zefan u64 ip_oldflags; 219f062abf0SLi Zefan unsigned int i_oldflags; 2207e97b8daSDavid Sterba umode_t mode; 2216cbff00fSChristoph Hellwig 222bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 223bd60ea0fSDavid Sterba return -EPERM; 224bd60ea0fSDavid Sterba 225b83cc969SLi Zefan if (btrfs_root_readonly(root)) 226b83cc969SLi Zefan return -EROFS; 227b83cc969SLi Zefan 2286cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 2296cbff00fSChristoph Hellwig return -EFAULT; 2306cbff00fSChristoph Hellwig 23175e7cb7fSLiu Bo ret = check_flags(flags); 23275e7cb7fSLiu Bo if (ret) 23375e7cb7fSLiu Bo return ret; 2346cbff00fSChristoph Hellwig 235e7848683SJan Kara ret = mnt_want_write_file(file); 236e7848683SJan Kara if (ret) 237e7848683SJan Kara return ret; 238e7848683SJan Kara 2396cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 2406cbff00fSChristoph Hellwig 241f062abf0SLi Zefan ip_oldflags = ip->flags; 242f062abf0SLi Zefan i_oldflags = inode->i_flags; 2437e97b8daSDavid Sterba mode = inode->i_mode; 244f062abf0SLi Zefan 2456cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2466cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2476cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2486cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2496cbff00fSChristoph Hellwig ret = -EPERM; 2506cbff00fSChristoph Hellwig goto out_unlock; 2516cbff00fSChristoph Hellwig } 2526cbff00fSChristoph Hellwig } 2536cbff00fSChristoph Hellwig 2546cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2556cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2566cbff00fSChristoph Hellwig else 2576cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2586cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2596cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2606cbff00fSChristoph Hellwig else 2616cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2626cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2636cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2646cbff00fSChristoph Hellwig else 2656cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2666cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2676cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2686cbff00fSChristoph Hellwig else 2696cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2706cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2716cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2726cbff00fSChristoph Hellwig else 2736cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2746cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2756cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2766cbff00fSChristoph Hellwig else 2776cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2787e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2797e97b8daSDavid Sterba if (S_ISREG(mode)) { 2807e97b8daSDavid Sterba /* 2817e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2827e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2837e97b8daSDavid Sterba * status of the file and will not set it. 2847e97b8daSDavid Sterba */ 2857e97b8daSDavid Sterba if (inode->i_size == 0) 2867e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2877e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2887e97b8daSDavid Sterba } else { 289e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2907e97b8daSDavid Sterba } 2917e97b8daSDavid Sterba } else { 2927e97b8daSDavid Sterba /* 2937e97b8daSDavid Sterba * Revert back under same assuptions as above 2947e97b8daSDavid Sterba */ 2957e97b8daSDavid Sterba if (S_ISREG(mode)) { 2967e97b8daSDavid Sterba if (inode->i_size == 0) 2977e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2987e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2997e97b8daSDavid Sterba } else { 300e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 3017e97b8daSDavid Sterba } 3027e97b8daSDavid Sterba } 3036cbff00fSChristoph Hellwig 30475e7cb7fSLiu Bo /* 30575e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 30675e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 30775e7cb7fSLiu Bo * things smaller. 30875e7cb7fSLiu Bo */ 30975e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 31075e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 31175e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 31263541927SFilipe David Borba Manana 31363541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 31463541927SFilipe David Borba Manana if (ret && ret != -ENODATA) 31563541927SFilipe David Borba Manana goto out_drop; 31675e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 31763541927SFilipe David Borba Manana const char *comp; 31863541927SFilipe David Borba Manana 31975e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 32075e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 32163541927SFilipe David Borba Manana 32263541927SFilipe David Borba Manana if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 32363541927SFilipe David Borba Manana comp = "lzo"; 32463541927SFilipe David Borba Manana else 32563541927SFilipe David Borba Manana comp = "zlib"; 32663541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", 32763541927SFilipe David Borba Manana comp, strlen(comp), 0); 32863541927SFilipe David Borba Manana if (ret) 32963541927SFilipe David Borba Manana goto out_drop; 33063541927SFilipe David Borba Manana 331ebcb904dSLi Zefan } else { 332ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 33375e7cb7fSLiu Bo } 3346cbff00fSChristoph Hellwig 3354da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 336f062abf0SLi Zefan if (IS_ERR(trans)) { 337f062abf0SLi Zefan ret = PTR_ERR(trans); 338f062abf0SLi Zefan goto out_drop; 339f062abf0SLi Zefan } 3406cbff00fSChristoph Hellwig 341306424ccSLi Zefan btrfs_update_iflags(inode); 3420c4d2d95SJosef Bacik inode_inc_iversion(inode); 343306424ccSLi Zefan inode->i_ctime = CURRENT_TIME; 3446cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3456cbff00fSChristoph Hellwig 3466cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 347f062abf0SLi Zefan out_drop: 348f062abf0SLi Zefan if (ret) { 349f062abf0SLi Zefan ip->flags = ip_oldflags; 350f062abf0SLi Zefan inode->i_flags = i_oldflags; 351f062abf0SLi Zefan } 3526cbff00fSChristoph Hellwig 3536cbff00fSChristoph Hellwig out_unlock: 3546cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 355e7848683SJan Kara mnt_drop_write_file(file); 3562d4e6f6aSliubo return ret; 3576cbff00fSChristoph Hellwig } 3586cbff00fSChristoph Hellwig 3596cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3606cbff00fSChristoph Hellwig { 361496ad9aaSAl Viro struct inode *inode = file_inode(file); 3626cbff00fSChristoph Hellwig 3636cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3646cbff00fSChristoph Hellwig } 365f46b5a66SChristoph Hellwig 366f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 367f7039b1dSLi Dongyang { 36854563d41SAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 369f7039b1dSLi Dongyang struct btrfs_device *device; 370f7039b1dSLi Dongyang struct request_queue *q; 371f7039b1dSLi Dongyang struct fstrim_range range; 372f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 373f7039b1dSLi Dongyang u64 num_devices = 0; 374815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 375f7039b1dSLi Dongyang int ret; 376f7039b1dSLi Dongyang 377f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 378f7039b1dSLi Dongyang return -EPERM; 379f7039b1dSLi Dongyang 3801f78160cSXiao Guangrong rcu_read_lock(); 3811f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3821f78160cSXiao Guangrong dev_list) { 383f7039b1dSLi Dongyang if (!device->bdev) 384f7039b1dSLi Dongyang continue; 385f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 386f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 387f7039b1dSLi Dongyang num_devices++; 388f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 389f7039b1dSLi Dongyang minlen); 390f7039b1dSLi Dongyang } 391f7039b1dSLi Dongyang } 3921f78160cSXiao Guangrong rcu_read_unlock(); 393f4c697e6SLukas Czerner 394f7039b1dSLi Dongyang if (!num_devices) 395f7039b1dSLi Dongyang return -EOPNOTSUPP; 396f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 397f7039b1dSLi Dongyang return -EFAULT; 398e515c18bSLukas Czerner if (range.start > total_bytes || 399e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 400f4c697e6SLukas Czerner return -EINVAL; 401f7039b1dSLi Dongyang 402f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 403f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 404815745cfSAl Viro ret = btrfs_trim_fs(fs_info->tree_root, &range); 405f7039b1dSLi Dongyang if (ret < 0) 406f7039b1dSLi Dongyang return ret; 407f7039b1dSLi Dongyang 408f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 409f7039b1dSLi Dongyang return -EFAULT; 410f7039b1dSLi Dongyang 411f7039b1dSLi Dongyang return 0; 412f7039b1dSLi Dongyang } 413f7039b1dSLi Dongyang 414dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 415dd5f9615SStefan Behrens { 41646e0f66aSChris Mason int i; 41746e0f66aSChris Mason 41846e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 41946e0f66aSChris Mason if (uuid[i]) 42046e0f66aSChris Mason return 0; 42146e0f66aSChris Mason } 42246e0f66aSChris Mason return 1; 423dd5f9615SStefan Behrens } 424dd5f9615SStefan Behrens 425d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 426cb8e7090SChristoph Hellwig struct dentry *dentry, 42772fd032eSSage Weil char *name, int namelen, 4286f72c7e2SArne Jansen u64 *async_transid, 4298696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 430f46b5a66SChristoph Hellwig { 431f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 432f46b5a66SChristoph Hellwig struct btrfs_key key; 433f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 434f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 435f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 436d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 43776dda93cSYan, Zheng struct btrfs_root *new_root; 438d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 4398ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 4405662344bSTsutomu Itoh struct inode *inode; 441f46b5a66SChristoph Hellwig int ret; 442f46b5a66SChristoph Hellwig int err; 443f46b5a66SChristoph Hellwig u64 objectid; 444f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 4453de4586cSChris Mason u64 index = 0; 446d5c12070SMiao Xie u64 qgroup_reserved; 4478ea05e3aSAlexander Block uuid_le new_uuid; 448f46b5a66SChristoph Hellwig 449581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 4502fbe8c8aSAl Viro if (ret) 451a22285a6SYan, Zheng return ret; 4526a912213SJosef Bacik 453d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 4549ed74f2dSJosef Bacik /* 455d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 456d5c12070SMiao Xie * of create_snapshot(). 4579ed74f2dSJosef Bacik */ 458d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 459dd5f9615SStefan Behrens 8, &qgroup_reserved, false); 460d5c12070SMiao Xie if (ret) 461d5c12070SMiao Xie return ret; 462f46b5a66SChristoph Hellwig 463d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 464d5c12070SMiao Xie if (IS_ERR(trans)) { 465d5c12070SMiao Xie ret = PTR_ERR(trans); 466de6e8200SLiu Bo btrfs_subvolume_release_metadata(root, &block_rsv, 467de6e8200SLiu Bo qgroup_reserved); 468de6e8200SLiu Bo return ret; 469d5c12070SMiao Xie } 470d5c12070SMiao Xie trans->block_rsv = &block_rsv; 471d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 472f46b5a66SChristoph Hellwig 4738696c533SMiao Xie ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 4746f72c7e2SArne Jansen if (ret) 4756f72c7e2SArne Jansen goto fail; 4766f72c7e2SArne Jansen 4775d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 4785581a51aSJan Schmidt 0, objectid, NULL, 0, 0, 0); 4798e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4808e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4818e8a1e31SJosef Bacik goto fail; 4828e8a1e31SJosef Bacik } 483f46b5a66SChristoph Hellwig 4845d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 485f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 486f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4875d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 488f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 489f46b5a66SChristoph Hellwig 4900a4e5586SRoss Kirk write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(), 491f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 4925d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 493b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 4945d4f98a2SYan Zheng BTRFS_UUID_SIZE); 495f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 496f46b5a66SChristoph Hellwig 4978ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 4988ea05e3aSAlexander Block 499f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 5003cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 5013cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 5023cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 5033cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 5043cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 505f46b5a66SChristoph Hellwig 5063cae210fSQu Wenruo btrfs_set_root_flags(&root_item, 0); 5073cae210fSQu Wenruo btrfs_set_root_limit(&root_item, 0); 5083cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 50908fe4db1SLi Zefan 510f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 51184234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 512f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 513f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 51486b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 51580ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 516f46b5a66SChristoph Hellwig 5178ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 5188ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 5198ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 5208ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 5213cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); 5223cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); 5238ea05e3aSAlexander Block root_item.ctime = root_item.otime; 5248ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 5258ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 526f46b5a66SChristoph Hellwig 527925baeddSChris Mason btrfs_tree_unlock(leaf); 528f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 529f46b5a66SChristoph Hellwig leaf = NULL; 530f46b5a66SChristoph Hellwig 531f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 532f46b5a66SChristoph Hellwig 533f46b5a66SChristoph Hellwig key.objectid = objectid; 5345d4f98a2SYan Zheng key.offset = 0; 535f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 536f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 537f46b5a66SChristoph Hellwig &root_item); 538f46b5a66SChristoph Hellwig if (ret) 539f46b5a66SChristoph Hellwig goto fail; 540f46b5a66SChristoph Hellwig 54176dda93cSYan, Zheng key.offset = (u64)-1; 54276dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 54379787eaaSJeff Mahoney if (IS_ERR(new_root)) { 54479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); 54579787eaaSJeff Mahoney ret = PTR_ERR(new_root); 54679787eaaSJeff Mahoney goto fail; 54779787eaaSJeff Mahoney } 54876dda93cSYan, Zheng 54976dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 55076dda93cSYan, Zheng 55163541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 552ce598979SMark Fasheh if (ret) { 553ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 55479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 555ce598979SMark Fasheh goto fail; 556ce598979SMark Fasheh } 557ce598979SMark Fasheh 558f46b5a66SChristoph Hellwig /* 559f46b5a66SChristoph Hellwig * insert the directory item 560f46b5a66SChristoph Hellwig */ 5613de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 56279787eaaSJeff Mahoney if (ret) { 56379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 56479787eaaSJeff Mahoney goto fail; 56579787eaaSJeff Mahoney } 5663de4586cSChris Mason 5673de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 56816cdcec7SMiao Xie name, namelen, dir, &key, 5693de4586cSChris Mason BTRFS_FT_DIR, index); 57079787eaaSJeff Mahoney if (ret) { 57179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 572f46b5a66SChristoph Hellwig goto fail; 57379787eaaSJeff Mahoney } 5740660b5afSChris Mason 57552c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 57652c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 57752c26179SYan Zheng BUG_ON(ret); 57852c26179SYan Zheng 5790660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5804df27c4dSYan, Zheng objectid, root->root_key.objectid, 58133345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 5820660b5afSChris Mason BUG_ON(ret); 5830660b5afSChris Mason 584dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 585dd5f9615SStefan Behrens root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 586dd5f9615SStefan Behrens objectid); 587dd5f9615SStefan Behrens if (ret) 588dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 589dd5f9615SStefan Behrens 590f46b5a66SChristoph Hellwig fail: 591d5c12070SMiao Xie trans->block_rsv = NULL; 592d5c12070SMiao Xie trans->bytes_reserved = 0; 593de6e8200SLiu Bo btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 594de6e8200SLiu Bo 59572fd032eSSage Weil if (async_transid) { 59672fd032eSSage Weil *async_transid = trans->transid; 59772fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 59800d71c9cSMiao Xie if (err) 59900d71c9cSMiao Xie err = btrfs_commit_transaction(trans, root); 60072fd032eSSage Weil } else { 60176dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 60272fd032eSSage Weil } 603f46b5a66SChristoph Hellwig if (err && !ret) 604f46b5a66SChristoph Hellwig ret = err; 6051a65e24bSChris Mason 6065662344bSTsutomu Itoh if (!ret) { 6075662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 608de6e8200SLiu Bo if (IS_ERR(inode)) 609de6e8200SLiu Bo return PTR_ERR(inode); 6105662344bSTsutomu Itoh d_instantiate(dentry, inode); 6115662344bSTsutomu Itoh } 612f46b5a66SChristoph Hellwig return ret; 613f46b5a66SChristoph Hellwig } 614f46b5a66SChristoph Hellwig 6158257b2dcSMiao Xie static void btrfs_wait_nocow_write(struct btrfs_root *root) 6168257b2dcSMiao Xie { 6178257b2dcSMiao Xie s64 writers; 6188257b2dcSMiao Xie DEFINE_WAIT(wait); 6198257b2dcSMiao Xie 6208257b2dcSMiao Xie do { 6218257b2dcSMiao Xie prepare_to_wait(&root->subv_writers->wait, &wait, 6228257b2dcSMiao Xie TASK_UNINTERRUPTIBLE); 6238257b2dcSMiao Xie 6248257b2dcSMiao Xie writers = percpu_counter_sum(&root->subv_writers->counter); 6258257b2dcSMiao Xie if (writers) 6268257b2dcSMiao Xie schedule(); 6278257b2dcSMiao Xie 6288257b2dcSMiao Xie finish_wait(&root->subv_writers->wait, &wait); 6298257b2dcSMiao Xie } while (writers); 6308257b2dcSMiao Xie } 6318257b2dcSMiao Xie 632e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 633e9662f70SMiao Xie struct dentry *dentry, char *name, int namelen, 634e9662f70SMiao Xie u64 *async_transid, bool readonly, 635e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 636f46b5a66SChristoph Hellwig { 6372e4bfab9SYan, Zheng struct inode *inode; 638f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 639f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 6402e4bfab9SYan, Zheng int ret; 641f46b5a66SChristoph Hellwig 64227cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 643f46b5a66SChristoph Hellwig return -EINVAL; 644f46b5a66SChristoph Hellwig 6458257b2dcSMiao Xie atomic_inc(&root->will_be_snapshoted); 6468257b2dcSMiao Xie smp_mb__after_atomic_inc(); 6478257b2dcSMiao Xie btrfs_wait_nocow_write(root); 6488257b2dcSMiao Xie 6496a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 6506a03843dSMiao Xie if (ret) 6518257b2dcSMiao Xie goto out; 6526a03843dSMiao Xie 653b0244199SMiao Xie btrfs_wait_ordered_extents(root, -1); 6546a03843dSMiao Xie 655a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 6568257b2dcSMiao Xie if (!pending_snapshot) { 6578257b2dcSMiao Xie ret = -ENOMEM; 6588257b2dcSMiao Xie goto out; 6598257b2dcSMiao Xie } 660a22285a6SYan, Zheng 66166d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 66266d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 663d5c12070SMiao Xie /* 664d5c12070SMiao Xie * 1 - parent dir inode 665d5c12070SMiao Xie * 2 - dir entries 666d5c12070SMiao Xie * 1 - root item 667d5c12070SMiao Xie * 2 - root ref/backref 668d5c12070SMiao Xie * 1 - root of snapshot 669dd5f9615SStefan Behrens * 1 - UUID item 670d5c12070SMiao Xie */ 671d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 672dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 673ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 674ee3441b4SJeff Mahoney false); 675d5c12070SMiao Xie if (ret) 6768257b2dcSMiao Xie goto free; 677d5c12070SMiao Xie 678a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 679a22285a6SYan, Zheng pending_snapshot->root = root; 680b83cc969SLi Zefan pending_snapshot->readonly = readonly; 681e9662f70SMiao Xie pending_snapshot->dir = dir; 6828696c533SMiao Xie pending_snapshot->inherit = inherit; 683a22285a6SYan, Zheng 684d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 685a22285a6SYan, Zheng if (IS_ERR(trans)) { 686a22285a6SYan, Zheng ret = PTR_ERR(trans); 687a22285a6SYan, Zheng goto fail; 688a22285a6SYan, Zheng } 689a22285a6SYan, Zheng 6908351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 691a22285a6SYan, Zheng list_add(&pending_snapshot->list, 692a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 6938351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 69472fd032eSSage Weil if (async_transid) { 69572fd032eSSage Weil *async_transid = trans->transid; 69672fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 69772fd032eSSage Weil root->fs_info->extent_root, 1); 69800d71c9cSMiao Xie if (ret) 69900d71c9cSMiao Xie ret = btrfs_commit_transaction(trans, root); 70072fd032eSSage Weil } else { 70172fd032eSSage Weil ret = btrfs_commit_transaction(trans, 70272fd032eSSage Weil root->fs_info->extent_root); 70372fd032eSSage Weil } 704aec8030aSMiao Xie if (ret) 705c37b2b62SJosef Bacik goto fail; 706a22285a6SYan, Zheng 707a22285a6SYan, Zheng ret = pending_snapshot->error; 708f46b5a66SChristoph Hellwig if (ret) 7092e4bfab9SYan, Zheng goto fail; 710f46b5a66SChristoph Hellwig 71166b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 71266b4ffd1SJosef Bacik if (ret) 71366b4ffd1SJosef Bacik goto fail; 714f46b5a66SChristoph Hellwig 7153821f348SFilipe Manana /* 7163821f348SFilipe Manana * If orphan cleanup did remove any orphans, it means the tree was 7173821f348SFilipe Manana * modified and therefore the commit root is not the same as the 7183821f348SFilipe Manana * current root anymore. This is a problem, because send uses the 7193821f348SFilipe Manana * commit root and therefore can see inode items that don't exist 7203821f348SFilipe Manana * in the current root anymore, and for example make calls to 7213821f348SFilipe Manana * btrfs_iget, which will do tree lookups based on the current root 7223821f348SFilipe Manana * and not on the commit root. Those lookups will fail, returning a 7233821f348SFilipe Manana * -ESTALE error, and making send fail with that error. So make sure 7243821f348SFilipe Manana * a send does not see any orphans we have just removed, and that it 7253821f348SFilipe Manana * will see the same inodes regardless of whether a transaction 7263821f348SFilipe Manana * commit happened before it started (meaning that the commit root 7273821f348SFilipe Manana * will be the same as the current root) or not. 7283821f348SFilipe Manana */ 7293821f348SFilipe Manana if (readonly && pending_snapshot->snap->node != 7303821f348SFilipe Manana pending_snapshot->snap->commit_root) { 7313821f348SFilipe Manana trans = btrfs_join_transaction(pending_snapshot->snap); 7323821f348SFilipe Manana if (IS_ERR(trans) && PTR_ERR(trans) != -ENOENT) { 7333821f348SFilipe Manana ret = PTR_ERR(trans); 7343821f348SFilipe Manana goto fail; 7353821f348SFilipe Manana } 7363821f348SFilipe Manana if (!IS_ERR(trans)) { 7373821f348SFilipe Manana ret = btrfs_commit_transaction(trans, 7383821f348SFilipe Manana pending_snapshot->snap); 7393821f348SFilipe Manana if (ret) 7403821f348SFilipe Manana goto fail; 7413821f348SFilipe Manana } 7423821f348SFilipe Manana } 7433821f348SFilipe Manana 7442fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 7452e4bfab9SYan, Zheng if (IS_ERR(inode)) { 7462e4bfab9SYan, Zheng ret = PTR_ERR(inode); 7472e4bfab9SYan, Zheng goto fail; 7482e4bfab9SYan, Zheng } 7495662344bSTsutomu Itoh 7502e4bfab9SYan, Zheng d_instantiate(dentry, inode); 7512e4bfab9SYan, Zheng ret = 0; 7522e4bfab9SYan, Zheng fail: 753d5c12070SMiao Xie btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, 754d5c12070SMiao Xie &pending_snapshot->block_rsv, 755d5c12070SMiao Xie pending_snapshot->qgroup_reserved); 7568257b2dcSMiao Xie free: 757a22285a6SYan, Zheng kfree(pending_snapshot); 7588257b2dcSMiao Xie out: 7598257b2dcSMiao Xie atomic_dec(&root->will_be_snapshoted); 760f46b5a66SChristoph Hellwig return ret; 761f46b5a66SChristoph Hellwig } 762f46b5a66SChristoph Hellwig 7634260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 7644260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 7654260f7c7SSage Weil * minimal. 7664260f7c7SSage Weil */ 7674260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 7684260f7c7SSage Weil { 7692f2f43d3SEric W. Biederman kuid_t fsuid = current_fsuid(); 7704260f7c7SSage Weil 7714260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 7724260f7c7SSage Weil return 0; 7732f2f43d3SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 7744260f7c7SSage Weil return 0; 7752f2f43d3SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 7764260f7c7SSage Weil return 0; 7774260f7c7SSage Weil return !capable(CAP_FOWNER); 7784260f7c7SSage Weil } 7794260f7c7SSage Weil 7804260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 7814260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 7824260f7c7SSage Weil * whether the type of victim is right. 7834260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 7844260f7c7SSage Weil * 2. We should have write and exec permissions on dir 7854260f7c7SSage Weil * 3. We can't remove anything from append-only dir 7864260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 7874260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 7884260f7c7SSage Weil * a. be owner of dir, or 7894260f7c7SSage Weil * b. be owner of victim, or 7904260f7c7SSage Weil * c. have CAP_FOWNER capability 7914260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 7924260f7c7SSage Weil * links pointing to it. 7934260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 7944260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 7954260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 7964260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 7974260f7c7SSage Weil * nfs_async_unlink(). 7984260f7c7SSage Weil */ 7994260f7c7SSage Weil 8004260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 8014260f7c7SSage Weil { 8024260f7c7SSage Weil int error; 8034260f7c7SSage Weil 8044260f7c7SSage Weil if (!victim->d_inode) 8054260f7c7SSage Weil return -ENOENT; 8064260f7c7SSage Weil 8074260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 8084fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 8094260f7c7SSage Weil 8104260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 8114260f7c7SSage Weil if (error) 8124260f7c7SSage Weil return error; 8134260f7c7SSage Weil if (IS_APPEND(dir)) 8144260f7c7SSage Weil return -EPERM; 8154260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 8164260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 8174260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 8184260f7c7SSage Weil return -EPERM; 8194260f7c7SSage Weil if (isdir) { 8204260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 8214260f7c7SSage Weil return -ENOTDIR; 8224260f7c7SSage Weil if (IS_ROOT(victim)) 8234260f7c7SSage Weil return -EBUSY; 8244260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 8254260f7c7SSage Weil return -EISDIR; 8264260f7c7SSage Weil if (IS_DEADDIR(dir)) 8274260f7c7SSage Weil return -ENOENT; 8284260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 8294260f7c7SSage Weil return -EBUSY; 8304260f7c7SSage Weil return 0; 8314260f7c7SSage Weil } 8324260f7c7SSage Weil 833cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 834cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 835cb8e7090SChristoph Hellwig { 836cb8e7090SChristoph Hellwig if (child->d_inode) 837cb8e7090SChristoph Hellwig return -EEXIST; 838cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 839cb8e7090SChristoph Hellwig return -ENOENT; 840cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 841cb8e7090SChristoph Hellwig } 842cb8e7090SChristoph Hellwig 843cb8e7090SChristoph Hellwig /* 844cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 845cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 846cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 847cb8e7090SChristoph Hellwig */ 84876dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 84976dda93cSYan, Zheng char *name, int namelen, 85072fd032eSSage Weil struct btrfs_root *snap_src, 8516f72c7e2SArne Jansen u64 *async_transid, bool readonly, 8528696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 853cb8e7090SChristoph Hellwig { 85476dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 855cb8e7090SChristoph Hellwig struct dentry *dentry; 856cb8e7090SChristoph Hellwig int error; 857cb8e7090SChristoph Hellwig 8585c50c9b8SDavid Sterba error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 8595c50c9b8SDavid Sterba if (error == -EINTR) 8605c50c9b8SDavid Sterba return error; 861cb8e7090SChristoph Hellwig 862cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 863cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 864cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 865cb8e7090SChristoph Hellwig goto out_unlock; 866cb8e7090SChristoph Hellwig 867cb8e7090SChristoph Hellwig error = -EEXIST; 868cb8e7090SChristoph Hellwig if (dentry->d_inode) 869cb8e7090SChristoph Hellwig goto out_dput; 870cb8e7090SChristoph Hellwig 87176dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 872cb8e7090SChristoph Hellwig if (error) 873a874a63eSLiu Bo goto out_dput; 874cb8e7090SChristoph Hellwig 8759c52057cSChris Mason /* 8769c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 8779c52057cSChris Mason * check for them now when we can safely fail 8789c52057cSChris Mason */ 8799c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 8809c52057cSChris Mason dir->i_ino, name, 8819c52057cSChris Mason namelen); 8829c52057cSChris Mason if (error) 8839c52057cSChris Mason goto out_dput; 8849c52057cSChris Mason 88576dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 88676dda93cSYan, Zheng 88776dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 88876dda93cSYan, Zheng goto out_up_read; 88976dda93cSYan, Zheng 8903de4586cSChris Mason if (snap_src) { 891e9662f70SMiao Xie error = create_snapshot(snap_src, dir, dentry, name, namelen, 8926f72c7e2SArne Jansen async_transid, readonly, inherit); 8933de4586cSChris Mason } else { 894d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 895d5c12070SMiao Xie async_transid, inherit); 8963de4586cSChris Mason } 89776dda93cSYan, Zheng if (!error) 89876dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 89976dda93cSYan, Zheng out_up_read: 90076dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 901cb8e7090SChristoph Hellwig out_dput: 902cb8e7090SChristoph Hellwig dput(dentry); 903cb8e7090SChristoph Hellwig out_unlock: 90476dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 905cb8e7090SChristoph Hellwig return error; 906cb8e7090SChristoph Hellwig } 907cb8e7090SChristoph Hellwig 9084cb5300bSChris Mason /* 9094cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 9104cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 9114cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 9124cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 9134cb5300bSChris Mason * part of the file 9144cb5300bSChris Mason */ 9154cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 9164cb5300bSChris Mason { 9174cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 9184cb5300bSChris Mason struct extent_map *em = NULL; 9194cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 9204cb5300bSChris Mason u64 end; 9214cb5300bSChris Mason 9224cb5300bSChris Mason read_lock(&em_tree->lock); 9234cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 9244cb5300bSChris Mason read_unlock(&em_tree->lock); 9254cb5300bSChris Mason 9264cb5300bSChris Mason if (em) { 9274cb5300bSChris Mason end = extent_map_end(em); 9284cb5300bSChris Mason free_extent_map(em); 9294cb5300bSChris Mason if (end - offset > thresh) 9304cb5300bSChris Mason return 0; 9314cb5300bSChris Mason } 9324cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 9334cb5300bSChris Mason thresh /= 2; 9344cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 9354cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 9364cb5300bSChris Mason if (end >= thresh) 9374cb5300bSChris Mason return 0; 9384cb5300bSChris Mason return 1; 9394cb5300bSChris Mason } 9404cb5300bSChris Mason 9414cb5300bSChris Mason /* 9424cb5300bSChris Mason * helper function to walk through a file and find extents 9434cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 9444cb5300bSChris Mason * 9454cb5300bSChris Mason * This is used by the defragging code to find new and small 9464cb5300bSChris Mason * extents 9474cb5300bSChris Mason */ 9484cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 9494cb5300bSChris Mason struct inode *inode, u64 newer_than, 9504cb5300bSChris Mason u64 *off, int thresh) 9514cb5300bSChris Mason { 9524cb5300bSChris Mason struct btrfs_path *path; 9534cb5300bSChris Mason struct btrfs_key min_key; 9544cb5300bSChris Mason struct extent_buffer *leaf; 9554cb5300bSChris Mason struct btrfs_file_extent_item *extent; 9564cb5300bSChris Mason int type; 9574cb5300bSChris Mason int ret; 958a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 9594cb5300bSChris Mason 9604cb5300bSChris Mason path = btrfs_alloc_path(); 9614cb5300bSChris Mason if (!path) 9624cb5300bSChris Mason return -ENOMEM; 9634cb5300bSChris Mason 964a4689d2bSDavid Sterba min_key.objectid = ino; 9654cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 9664cb5300bSChris Mason min_key.offset = *off; 9674cb5300bSChris Mason 9684cb5300bSChris Mason while (1) { 969f094c9bdSFilipe Manana path->keep_locks = 1; 9706174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 9714cb5300bSChris Mason if (ret != 0) 9724cb5300bSChris Mason goto none; 973f094c9bdSFilipe Manana path->keep_locks = 0; 974f094c9bdSFilipe Manana btrfs_unlock_up_safe(path, 1); 975f094c9bdSFilipe Manana process_slot: 976a4689d2bSDavid Sterba if (min_key.objectid != ino) 9774cb5300bSChris Mason goto none; 9784cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 9794cb5300bSChris Mason goto none; 9804cb5300bSChris Mason 9814cb5300bSChris Mason leaf = path->nodes[0]; 9824cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 9834cb5300bSChris Mason struct btrfs_file_extent_item); 9844cb5300bSChris Mason 9854cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 9864cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 9874cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 9884cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 9894cb5300bSChris Mason *off = min_key.offset; 9904cb5300bSChris Mason btrfs_free_path(path); 9914cb5300bSChris Mason return 0; 9924cb5300bSChris Mason } 9934cb5300bSChris Mason 994f094c9bdSFilipe Manana path->slots[0]++; 995f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 996f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 997f094c9bdSFilipe Manana goto process_slot; 998f094c9bdSFilipe Manana } 999f094c9bdSFilipe Manana 10004cb5300bSChris Mason if (min_key.offset == (u64)-1) 10014cb5300bSChris Mason goto none; 10024cb5300bSChris Mason 10034cb5300bSChris Mason min_key.offset++; 10044cb5300bSChris Mason btrfs_release_path(path); 10054cb5300bSChris Mason } 10064cb5300bSChris Mason none: 10074cb5300bSChris Mason btrfs_free_path(path); 10084cb5300bSChris Mason return -ENOENT; 10094cb5300bSChris Mason } 10104cb5300bSChris Mason 10116c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 101217ce6ef8SLiu Bo { 101317ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 1014940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 10156c282eb4SLi Zefan struct extent_map *em; 10166c282eb4SLi Zefan u64 len = PAGE_CACHE_SIZE; 1017940100a4SChris Mason 1018940100a4SChris Mason /* 1019940100a4SChris Mason * hopefully we have this extent in the tree already, try without 1020940100a4SChris Mason * the full extent lock 1021940100a4SChris Mason */ 1022940100a4SChris Mason read_lock(&em_tree->lock); 1023940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 1024940100a4SChris Mason read_unlock(&em_tree->lock); 1025940100a4SChris Mason 1026940100a4SChris Mason if (!em) { 1027308d9800SFilipe Manana struct extent_state *cached = NULL; 1028308d9800SFilipe Manana u64 end = start + len - 1; 1029308d9800SFilipe Manana 1030940100a4SChris Mason /* get the big lock and read metadata off disk */ 1031308d9800SFilipe Manana lock_extent_bits(io_tree, start, end, 0, &cached); 1032940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 1033308d9800SFilipe Manana unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS); 1034940100a4SChris Mason 10356cf8bfbfSDan Carpenter if (IS_ERR(em)) 10366c282eb4SLi Zefan return NULL; 1037940100a4SChris Mason } 1038940100a4SChris Mason 10396c282eb4SLi Zefan return em; 10406c282eb4SLi Zefan } 10416c282eb4SLi Zefan 10426c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 10436c282eb4SLi Zefan { 10446c282eb4SLi Zefan struct extent_map *next; 10456c282eb4SLi Zefan bool ret = true; 10466c282eb4SLi Zefan 10476c282eb4SLi Zefan /* this is the last extent */ 10486c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 10496c282eb4SLi Zefan return false; 10506c282eb4SLi Zefan 10516c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1052e2127cf0SFilipe Manana if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE || 1053e2127cf0SFilipe Manana (em->block_start + em->block_len == next->block_start)) 10546c282eb4SLi Zefan ret = false; 10556c282eb4SLi Zefan 10566c282eb4SLi Zefan free_extent_map(next); 10576c282eb4SLi Zefan return ret; 10586c282eb4SLi Zefan } 10596c282eb4SLi Zefan 10606c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh, 1061a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1062a43a2111SAndrew Mahone int compress) 10636c282eb4SLi Zefan { 10646c282eb4SLi Zefan struct extent_map *em; 10656c282eb4SLi Zefan int ret = 1; 10666c282eb4SLi Zefan bool next_mergeable = true; 10676c282eb4SLi Zefan 10686c282eb4SLi Zefan /* 10696c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 10706c282eb4SLi Zefan * defragging it 10716c282eb4SLi Zefan */ 10726c282eb4SLi Zefan if (start < *defrag_end) 10736c282eb4SLi Zefan return 1; 10746c282eb4SLi Zefan 10756c282eb4SLi Zefan *skip = 0; 10766c282eb4SLi Zefan 10776c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 10786c282eb4SLi Zefan if (!em) 10796c282eb4SLi Zefan return 0; 10806c282eb4SLi Zefan 1081940100a4SChris Mason /* this will cover holes, and inline extents */ 108217ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1083940100a4SChris Mason ret = 0; 108417ce6ef8SLiu Bo goto out; 108517ce6ef8SLiu Bo } 108617ce6ef8SLiu Bo 10876c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1088940100a4SChris Mason 1089940100a4SChris Mason /* 10906c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 10916c282eb4SLi Zefan * real extent, don't bother defragging it 1092940100a4SChris Mason */ 1093a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 10946c282eb4SLi Zefan (em->len >= thresh || !next_mergeable)) 1095940100a4SChris Mason ret = 0; 109617ce6ef8SLiu Bo out: 1097940100a4SChris Mason /* 1098940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1099940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1100940100a4SChris Mason * so that the next tiny extent will force a defrag. 1101940100a4SChris Mason * 1102940100a4SChris Mason * The end result of this is that tiny extents before a single big 1103940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1104940100a4SChris Mason */ 1105940100a4SChris Mason if (ret) { 1106940100a4SChris Mason *defrag_end = extent_map_end(em); 1107940100a4SChris Mason } else { 1108940100a4SChris Mason *last_len = 0; 1109940100a4SChris Mason *skip = extent_map_end(em); 1110940100a4SChris Mason *defrag_end = 0; 1111940100a4SChris Mason } 1112940100a4SChris Mason 1113940100a4SChris Mason free_extent_map(em); 1114940100a4SChris Mason return ret; 1115940100a4SChris Mason } 1116940100a4SChris Mason 11174cb5300bSChris Mason /* 11184cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 11194cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 11204cb5300bSChris Mason * to COW and defrag. 11214cb5300bSChris Mason * 11224cb5300bSChris Mason * It also makes sure the delalloc code has enough 11234cb5300bSChris Mason * dirty data to avoid making new small extents as part 11244cb5300bSChris Mason * of the defrag 11254cb5300bSChris Mason * 11264cb5300bSChris Mason * It's a good idea to start RA on this range 11274cb5300bSChris Mason * before calling this. 11284cb5300bSChris Mason */ 11294cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 11304cb5300bSChris Mason struct page **pages, 11314cb5300bSChris Mason unsigned long start_index, 1132c41570c9SJustin Maggard unsigned long num_pages) 1133f46b5a66SChristoph Hellwig { 11344cb5300bSChris Mason unsigned long file_end; 11354cb5300bSChris Mason u64 isize = i_size_read(inode); 1136f46b5a66SChristoph Hellwig u64 page_start; 1137f46b5a66SChristoph Hellwig u64 page_end; 11381f12bd06SLiu Bo u64 page_cnt; 11394cb5300bSChris Mason int ret; 11404cb5300bSChris Mason int i; 11414cb5300bSChris Mason int i_done; 11424cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 11434cb5300bSChris Mason struct extent_state *cached_state = NULL; 1144600a45e1SMiao Xie struct extent_io_tree *tree; 11453b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 11464cb5300bSChris Mason 11474cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 11481f12bd06SLiu Bo if (!isize || start_index > file_end) 11491f12bd06SLiu Bo return 0; 11501f12bd06SLiu Bo 11511f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 11524cb5300bSChris Mason 11534cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 11541f12bd06SLiu Bo page_cnt << PAGE_CACHE_SHIFT); 11554cb5300bSChris Mason if (ret) 11564cb5300bSChris Mason return ret; 11574cb5300bSChris Mason i_done = 0; 1158600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 11594cb5300bSChris Mason 11604cb5300bSChris Mason /* step one, lock all the pages */ 11611f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 11624cb5300bSChris Mason struct page *page; 1163600a45e1SMiao Xie again: 1164a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 11653b16a4e3SJosef Bacik start_index + i, mask); 11664cb5300bSChris Mason if (!page) 11674cb5300bSChris Mason break; 11684cb5300bSChris Mason 1169600a45e1SMiao Xie page_start = page_offset(page); 1170600a45e1SMiao Xie page_end = page_start + PAGE_CACHE_SIZE - 1; 1171600a45e1SMiao Xie while (1) { 1172308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1173308d9800SFilipe Manana 0, &cached_state); 1174600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1175600a45e1SMiao Xie page_start); 1176308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1177308d9800SFilipe Manana &cached_state, GFP_NOFS); 1178600a45e1SMiao Xie if (!ordered) 1179600a45e1SMiao Xie break; 1180600a45e1SMiao Xie 1181600a45e1SMiao Xie unlock_page(page); 1182600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1183600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1184600a45e1SMiao Xie lock_page(page); 11851f12bd06SLiu Bo /* 11861f12bd06SLiu Bo * we unlocked the page above, so we need check if 11871f12bd06SLiu Bo * it was released or not. 11881f12bd06SLiu Bo */ 11891f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 11901f12bd06SLiu Bo unlock_page(page); 11911f12bd06SLiu Bo page_cache_release(page); 11921f12bd06SLiu Bo goto again; 11931f12bd06SLiu Bo } 1194600a45e1SMiao Xie } 1195600a45e1SMiao Xie 11964cb5300bSChris Mason if (!PageUptodate(page)) { 11974cb5300bSChris Mason btrfs_readpage(NULL, page); 11984cb5300bSChris Mason lock_page(page); 11994cb5300bSChris Mason if (!PageUptodate(page)) { 12004cb5300bSChris Mason unlock_page(page); 12014cb5300bSChris Mason page_cache_release(page); 12024cb5300bSChris Mason ret = -EIO; 12034cb5300bSChris Mason break; 12044cb5300bSChris Mason } 12054cb5300bSChris Mason } 1206600a45e1SMiao Xie 1207600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1208600a45e1SMiao Xie unlock_page(page); 1209600a45e1SMiao Xie page_cache_release(page); 1210600a45e1SMiao Xie goto again; 1211600a45e1SMiao Xie } 1212600a45e1SMiao Xie 12134cb5300bSChris Mason pages[i] = page; 12144cb5300bSChris Mason i_done++; 12154cb5300bSChris Mason } 12164cb5300bSChris Mason if (!i_done || ret) 12174cb5300bSChris Mason goto out; 12184cb5300bSChris Mason 12194cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 12204cb5300bSChris Mason goto out; 12214cb5300bSChris Mason 12224cb5300bSChris Mason /* 12234cb5300bSChris Mason * so now we have a nice long stream of locked 12244cb5300bSChris Mason * and up to date pages, lets wait on them 12254cb5300bSChris Mason */ 12264cb5300bSChris Mason for (i = 0; i < i_done; i++) 12274cb5300bSChris Mason wait_on_page_writeback(pages[i]); 12284cb5300bSChris Mason 12294cb5300bSChris Mason page_start = page_offset(pages[0]); 12304cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 12314cb5300bSChris Mason 12324cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1233d0082371SJeff Mahoney page_start, page_end - 1, 0, &cached_state); 12344cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 12354cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 12369e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 12379e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 12384cb5300bSChris Mason 12391f12bd06SLiu Bo if (i_done != page_cnt) { 12409e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 12419e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 12429e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 12434cb5300bSChris Mason btrfs_delalloc_release_space(inode, 12441f12bd06SLiu Bo (page_cnt - i_done) << PAGE_CACHE_SHIFT); 12454cb5300bSChris Mason } 12464cb5300bSChris Mason 12474cb5300bSChris Mason 12489e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 12499e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 12504cb5300bSChris Mason 12514cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 12524cb5300bSChris Mason page_start, page_end - 1, &cached_state, 12534cb5300bSChris Mason GFP_NOFS); 12544cb5300bSChris Mason 12554cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12564cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 12574cb5300bSChris Mason ClearPageChecked(pages[i]); 12584cb5300bSChris Mason set_page_extent_mapped(pages[i]); 12594cb5300bSChris Mason set_page_dirty(pages[i]); 12604cb5300bSChris Mason unlock_page(pages[i]); 12614cb5300bSChris Mason page_cache_release(pages[i]); 12624cb5300bSChris Mason } 12634cb5300bSChris Mason return i_done; 12644cb5300bSChris Mason out: 12654cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12664cb5300bSChris Mason unlock_page(pages[i]); 12674cb5300bSChris Mason page_cache_release(pages[i]); 12684cb5300bSChris Mason } 12691f12bd06SLiu Bo btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); 12704cb5300bSChris Mason return ret; 12714cb5300bSChris Mason 12724cb5300bSChris Mason } 12734cb5300bSChris Mason 12744cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 12754cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 12764cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 12774cb5300bSChris Mason { 12784cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 12794cb5300bSChris Mason struct file_ra_state *ra = NULL; 12804cb5300bSChris Mason unsigned long last_index; 1281151a31b2SLi Zefan u64 isize = i_size_read(inode); 1282940100a4SChris Mason u64 last_len = 0; 1283940100a4SChris Mason u64 skip = 0; 1284940100a4SChris Mason u64 defrag_end = 0; 12854cb5300bSChris Mason u64 newer_off = range->start; 1286f46b5a66SChristoph Hellwig unsigned long i; 1287008873eaSLi Zefan unsigned long ra_index = 0; 1288f46b5a66SChristoph Hellwig int ret; 12894cb5300bSChris Mason int defrag_count = 0; 12901a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 12914cb5300bSChris Mason int extent_thresh = range->extent_thresh; 1292c41570c9SJustin Maggard unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1293c41570c9SJustin Maggard unsigned long cluster = max_cluster; 12944cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 12954cb5300bSChris Mason struct page **pages = NULL; 12964cb5300bSChris Mason 12970abd5b17SLiu Bo if (isize == 0) 12980abd5b17SLiu Bo return 0; 12990abd5b17SLiu Bo 13000abd5b17SLiu Bo if (range->start >= isize) 13010abd5b17SLiu Bo return -EINVAL; 13021a419d85SLi Zefan 13031a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 13041a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 13051a419d85SLi Zefan return -EINVAL; 13061a419d85SLi Zefan if (range->compress_type) 13071a419d85SLi Zefan compress_type = range->compress_type; 13081a419d85SLi Zefan } 1309f46b5a66SChristoph Hellwig 13100abd5b17SLiu Bo if (extent_thresh == 0) 13110abd5b17SLiu Bo extent_thresh = 256 * 1024; 1312f46b5a66SChristoph Hellwig 13134cb5300bSChris Mason /* 13144cb5300bSChris Mason * if we were not given a file, allocate a readahead 13154cb5300bSChris Mason * context 13164cb5300bSChris Mason */ 13174cb5300bSChris Mason if (!file) { 13184cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 13194cb5300bSChris Mason if (!ra) 13204cb5300bSChris Mason return -ENOMEM; 13214cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 13224cb5300bSChris Mason } else { 13234cb5300bSChris Mason ra = &file->f_ra; 13244cb5300bSChris Mason } 13254cb5300bSChris Mason 1326d9b0d9baSDulshani Gunawardhana pages = kmalloc_array(max_cluster, sizeof(struct page *), 13274cb5300bSChris Mason GFP_NOFS); 13284cb5300bSChris Mason if (!pages) { 13294cb5300bSChris Mason ret = -ENOMEM; 13304cb5300bSChris Mason goto out_ra; 13314cb5300bSChris Mason } 13324cb5300bSChris Mason 13334cb5300bSChris Mason /* find the last page to defrag */ 13341e701a32SChris Mason if (range->start + range->len > range->start) { 1335151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 13361e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 13371e701a32SChris Mason } else { 1338151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 13391e701a32SChris Mason } 13401e701a32SChris Mason 13414cb5300bSChris Mason if (newer_than) { 13424cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 13434cb5300bSChris Mason &newer_off, 64 * 1024); 13444cb5300bSChris Mason if (!ret) { 13454cb5300bSChris Mason range->start = newer_off; 13464cb5300bSChris Mason /* 13474cb5300bSChris Mason * we always align our defrag to help keep 13484cb5300bSChris Mason * the extents in the file evenly spaced 13494cb5300bSChris Mason */ 13504cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 13514cb5300bSChris Mason } else 13524cb5300bSChris Mason goto out_ra; 13534cb5300bSChris Mason } else { 13541e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 13554cb5300bSChris Mason } 13564cb5300bSChris Mason if (!max_to_defrag) 13577ec31b54SLiu Bo max_to_defrag = last_index + 1; 13584cb5300bSChris Mason 13592a0f7f57SLi Zefan /* 13602a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 13612a0f7f57SLi Zefan * written sequentially. 13622a0f7f57SLi Zefan */ 13632a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 13642a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 13652a0f7f57SLi Zefan 1366f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1367f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1368f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 13694cb5300bSChris Mason /* 13704cb5300bSChris Mason * make sure we stop running if someone unmounts 13714cb5300bSChris Mason * the FS 13724cb5300bSChris Mason */ 13734cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 13744cb5300bSChris Mason break; 13754cb5300bSChris Mason 1376210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1377efe120a0SFrank Holton printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n"); 1378210549ebSDavid Sterba ret = -EAGAIN; 1379210549ebSDavid Sterba break; 1380210549ebSDavid Sterba } 1381210549ebSDavid Sterba 138266c26892SLiu Bo if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 13836c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1384a43a2111SAndrew Mahone &defrag_end, range->flags & 1385a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1386940100a4SChris Mason unsigned long next; 1387940100a4SChris Mason /* 1388940100a4SChris Mason * the should_defrag function tells us how much to skip 1389940100a4SChris Mason * bump our counter by the suggested amount 1390940100a4SChris Mason */ 1391940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1392940100a4SChris Mason i = max(i + 1, next); 1393940100a4SChris Mason continue; 1394940100a4SChris Mason } 1395008873eaSLi Zefan 1396008873eaSLi Zefan if (!newer_than) { 1397008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1398008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1399008873eaSLi Zefan cluster = min(cluster, max_cluster); 1400008873eaSLi Zefan } else { 1401008873eaSLi Zefan cluster = max_cluster; 1402008873eaSLi Zefan } 1403008873eaSLi Zefan 1404008873eaSLi Zefan if (i + cluster > ra_index) { 1405008873eaSLi Zefan ra_index = max(i, ra_index); 1406008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1407008873eaSLi Zefan cluster); 1408008873eaSLi Zefan ra_index += max_cluster; 1409008873eaSLi Zefan } 14104cb5300bSChris Mason 1411ecb8bea8SLiu Bo mutex_lock(&inode->i_mutex); 1412633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 1413633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = compress_type; 1414008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1415ecb8bea8SLiu Bo if (ret < 0) { 1416ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 14174cb5300bSChris Mason goto out_ra; 1418ecb8bea8SLiu Bo } 14194cb5300bSChris Mason 14204cb5300bSChris Mason defrag_count += ret; 1421d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 1422ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 14234cb5300bSChris Mason 14244cb5300bSChris Mason if (newer_than) { 14254cb5300bSChris Mason if (newer_off == (u64)-1) 14264cb5300bSChris Mason break; 14274cb5300bSChris Mason 1428e1f041e1SLiu Bo if (ret > 0) 1429e1f041e1SLiu Bo i += ret; 1430e1f041e1SLiu Bo 14314cb5300bSChris Mason newer_off = max(newer_off + 1, 14324cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 14334cb5300bSChris Mason 14344cb5300bSChris Mason ret = find_new_extents(root, inode, 14354cb5300bSChris Mason newer_than, &newer_off, 14364cb5300bSChris Mason 64 * 1024); 14374cb5300bSChris Mason if (!ret) { 14384cb5300bSChris Mason range->start = newer_off; 14394cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 14404cb5300bSChris Mason } else { 14414cb5300bSChris Mason break; 1442940100a4SChris Mason } 14434cb5300bSChris Mason } else { 1444008873eaSLi Zefan if (ret > 0) { 1445cbcc8326SLi Zefan i += ret; 1446008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1447008873eaSLi Zefan } else { 1448940100a4SChris Mason i++; 1449008873eaSLi Zefan last_len = 0; 1450008873eaSLi Zefan } 1451f46b5a66SChristoph Hellwig } 14524cb5300bSChris Mason } 1453f46b5a66SChristoph Hellwig 1454dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 14551e701a32SChris Mason filemap_flush(inode->i_mapping); 1456dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1457dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1458dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1459dec8ef90SFilipe Manana } 14601e701a32SChris Mason 14611e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 14621e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 14631e701a32SChris Mason * we have to make sure the IO is actually started and that 14641e701a32SChris Mason * ordered extents get created before we return 14651e701a32SChris Mason */ 14661e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 14671e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 14681e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 14691e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 14701e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 14711e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 14721e701a32SChris Mason } 14731e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 14741e701a32SChris Mason } 14751e701a32SChris Mason 14761a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 14772b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 14781a419d85SLi Zefan } 14791a419d85SLi Zefan 148060ccf82fSDiego Calleja ret = defrag_count; 1481940100a4SChris Mason 14824cb5300bSChris Mason out_ra: 1483633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 1484633085c7SFilipe David Borba Manana mutex_lock(&inode->i_mutex); 1485633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 1486633085c7SFilipe David Borba Manana mutex_unlock(&inode->i_mutex); 1487633085c7SFilipe David Borba Manana } 14884cb5300bSChris Mason if (!file) 14894cb5300bSChris Mason kfree(ra); 14904cb5300bSChris Mason kfree(pages); 1491940100a4SChris Mason return ret; 1492f46b5a66SChristoph Hellwig } 1493f46b5a66SChristoph Hellwig 1494198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 149576dda93cSYan, Zheng void __user *arg) 1496f46b5a66SChristoph Hellwig { 1497f46b5a66SChristoph Hellwig u64 new_size; 1498f46b5a66SChristoph Hellwig u64 old_size; 1499f46b5a66SChristoph Hellwig u64 devid = 1; 1500496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 1501f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1502f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1503f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1504f46b5a66SChristoph Hellwig char *sizestr; 15059a40f122SGui Hecheng char *retptr; 1506f46b5a66SChristoph Hellwig char *devstr = NULL; 1507f46b5a66SChristoph Hellwig int ret = 0; 1508f46b5a66SChristoph Hellwig int mod = 0; 1509f46b5a66SChristoph Hellwig 1510e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1511e441d54dSChris Mason return -EPERM; 1512e441d54dSChris Mason 1513198605a8SMiao Xie ret = mnt_want_write_file(file); 1514198605a8SMiao Xie if (ret) 1515198605a8SMiao Xie return ret; 1516198605a8SMiao Xie 15175ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 15185ac00addSStefan Behrens 1)) { 151997547676SMiao Xie mnt_drop_write_file(file); 1520e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1521c9e9f97bSIlya Dryomov } 1522c9e9f97bSIlya Dryomov 15235ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1524dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1525c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1526c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1527c9e9f97bSIlya Dryomov goto out; 1528c9e9f97bSIlya Dryomov } 15295516e595SMark Fasheh 15305516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1531f46b5a66SChristoph Hellwig 1532f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1533f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1534f46b5a66SChristoph Hellwig if (devstr) { 1535f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1536f46b5a66SChristoph Hellwig *devstr = '\0'; 1537f46b5a66SChristoph Hellwig devstr = vol_args->name; 153858dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 153958dfae63SZhangZhen if (ret) 154058dfae63SZhangZhen goto out_free; 1541dfd79829SMiao Xie if (!devid) { 1542dfd79829SMiao Xie ret = -EINVAL; 1543dfd79829SMiao Xie goto out_free; 1544dfd79829SMiao Xie } 1545efe120a0SFrank Holton btrfs_info(root->fs_info, "resizing devid %llu", devid); 1546f46b5a66SChristoph Hellwig } 1547dba60f3fSMiao Xie 1548aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1549f46b5a66SChristoph Hellwig if (!device) { 1550efe120a0SFrank Holton btrfs_info(root->fs_info, "resizer unable to find device %llu", 1551c1c9ff7cSGeert Uytterhoeven devid); 1552dfd79829SMiao Xie ret = -ENODEV; 1553c9e9f97bSIlya Dryomov goto out_free; 1554f46b5a66SChristoph Hellwig } 1555dba60f3fSMiao Xie 1556dba60f3fSMiao Xie if (!device->writeable) { 1557efe120a0SFrank Holton btrfs_info(root->fs_info, 1558efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1559c1c9ff7cSGeert Uytterhoeven devid); 1560dfd79829SMiao Xie ret = -EPERM; 15614e42ae1bSLiu Bo goto out_free; 15624e42ae1bSLiu Bo } 15634e42ae1bSLiu Bo 1564f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1565f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1566f46b5a66SChristoph Hellwig else { 1567f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1568f46b5a66SChristoph Hellwig mod = -1; 1569f46b5a66SChristoph Hellwig sizestr++; 1570f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1571f46b5a66SChristoph Hellwig mod = 1; 1572f46b5a66SChristoph Hellwig sizestr++; 1573f46b5a66SChristoph Hellwig } 15749a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 15759a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1576f46b5a66SChristoph Hellwig ret = -EINVAL; 1577c9e9f97bSIlya Dryomov goto out_free; 1578f46b5a66SChristoph Hellwig } 1579f46b5a66SChristoph Hellwig } 1580f46b5a66SChristoph Hellwig 158163a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1582dfd79829SMiao Xie ret = -EPERM; 158363a212abSStefan Behrens goto out_free; 158463a212abSStefan Behrens } 158563a212abSStefan Behrens 1586f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1587f46b5a66SChristoph Hellwig 1588f46b5a66SChristoph Hellwig if (mod < 0) { 1589f46b5a66SChristoph Hellwig if (new_size > old_size) { 1590f46b5a66SChristoph Hellwig ret = -EINVAL; 1591c9e9f97bSIlya Dryomov goto out_free; 1592f46b5a66SChristoph Hellwig } 1593f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1594f46b5a66SChristoph Hellwig } else if (mod > 0) { 1595eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1596902c68a4SGui Hecheng ret = -ERANGE; 1597eb8052e0SWenliang Fan goto out_free; 1598eb8052e0SWenliang Fan } 1599f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1600f46b5a66SChristoph Hellwig } 1601f46b5a66SChristoph Hellwig 1602f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1603f46b5a66SChristoph Hellwig ret = -EINVAL; 1604c9e9f97bSIlya Dryomov goto out_free; 1605f46b5a66SChristoph Hellwig } 1606f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1607f46b5a66SChristoph Hellwig ret = -EFBIG; 1608c9e9f97bSIlya Dryomov goto out_free; 1609f46b5a66SChristoph Hellwig } 1610f46b5a66SChristoph Hellwig 1611f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1612f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1613f46b5a66SChristoph Hellwig 1614efe120a0SFrank Holton printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n", 1615c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1616f46b5a66SChristoph Hellwig 1617f46b5a66SChristoph Hellwig if (new_size > old_size) { 1618a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 161998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 162098d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1621c9e9f97bSIlya Dryomov goto out_free; 162298d5dc13STsutomu Itoh } 1623f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1624f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1625ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1626f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 16270253f40eSjeff.liu } /* equal, nothing need to do */ 1628f46b5a66SChristoph Hellwig 1629c9e9f97bSIlya Dryomov out_free: 1630f46b5a66SChristoph Hellwig kfree(vol_args); 1631c9e9f97bSIlya Dryomov out: 1632c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 16335ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 163418f39c41SIlya Dryomov mnt_drop_write_file(file); 1635f46b5a66SChristoph Hellwig return ret; 1636f46b5a66SChristoph Hellwig } 1637f46b5a66SChristoph Hellwig 163872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 16396f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 16406f72c7e2SArne Jansen u64 *transid, bool readonly, 16418696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1642f46b5a66SChristoph Hellwig { 1643f46b5a66SChristoph Hellwig int namelen; 16443de4586cSChris Mason int ret = 0; 1645f46b5a66SChristoph Hellwig 1646a874a63eSLiu Bo ret = mnt_want_write_file(file); 1647a874a63eSLiu Bo if (ret) 1648a874a63eSLiu Bo goto out; 1649a874a63eSLiu Bo 165072fd032eSSage Weil namelen = strlen(name); 165172fd032eSSage Weil if (strchr(name, '/')) { 1652f46b5a66SChristoph Hellwig ret = -EINVAL; 1653a874a63eSLiu Bo goto out_drop_write; 1654f46b5a66SChristoph Hellwig } 1655f46b5a66SChristoph Hellwig 165616780cabSChris Mason if (name[0] == '.' && 165716780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 165816780cabSChris Mason ret = -EEXIST; 1659a874a63eSLiu Bo goto out_drop_write; 166016780cabSChris Mason } 166116780cabSChris Mason 16623de4586cSChris Mason if (subvol) { 166372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 16646f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1665cb8e7090SChristoph Hellwig } else { 16662903ff01SAl Viro struct fd src = fdget(fd); 16673de4586cSChris Mason struct inode *src_inode; 16682903ff01SAl Viro if (!src.file) { 16693de4586cSChris Mason ret = -EINVAL; 1670a874a63eSLiu Bo goto out_drop_write; 16713de4586cSChris Mason } 16723de4586cSChris Mason 1673496ad9aaSAl Viro src_inode = file_inode(src.file); 1674496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1675efe120a0SFrank Holton btrfs_info(BTRFS_I(src_inode)->root->fs_info, 1676efe120a0SFrank Holton "Snapshot src from another FS"); 167723ad5b17SKusanagi Kouichi ret = -EXDEV; 1678d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1679d0242061SDavid Sterba /* 1680d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1681d0242061SDavid Sterba * are limited to own subvolumes only 1682d0242061SDavid Sterba */ 1683d0242061SDavid Sterba ret = -EPERM; 1684ecd18815SAl Viro } else { 168572fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 168672fd032eSSage Weil BTRFS_I(src_inode)->root, 16876f72c7e2SArne Jansen transid, readonly, inherit); 1688ecd18815SAl Viro } 16892903ff01SAl Viro fdput(src); 1690cb8e7090SChristoph Hellwig } 1691a874a63eSLiu Bo out_drop_write: 1692a874a63eSLiu Bo mnt_drop_write_file(file); 1693f46b5a66SChristoph Hellwig out: 169472fd032eSSage Weil return ret; 169572fd032eSSage Weil } 169672fd032eSSage Weil 169772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1698fa0d2b9bSLi Zefan void __user *arg, int subvol) 169972fd032eSSage Weil { 1700fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 170172fd032eSSage Weil int ret; 170272fd032eSSage Weil 1703fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1704fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1705fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1706fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1707fa0d2b9bSLi Zefan 1708fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1709b83cc969SLi Zefan vol_args->fd, subvol, 17106f72c7e2SArne Jansen NULL, false, NULL); 1711fa0d2b9bSLi Zefan 1712fa0d2b9bSLi Zefan kfree(vol_args); 1713fa0d2b9bSLi Zefan return ret; 1714fa0d2b9bSLi Zefan } 1715fa0d2b9bSLi Zefan 1716fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1717fa0d2b9bSLi Zefan void __user *arg, int subvol) 1718fa0d2b9bSLi Zefan { 1719fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1720fa0d2b9bSLi Zefan int ret; 1721fdfb1e4fSLi Zefan u64 transid = 0; 1722fdfb1e4fSLi Zefan u64 *ptr = NULL; 1723b83cc969SLi Zefan bool readonly = false; 17246f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 172572fd032eSSage Weil 1726fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1727fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1728fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1729fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1730fdfb1e4fSLi Zefan 1731b83cc969SLi Zefan if (vol_args->flags & 17326f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 17336f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1734b83cc969SLi Zefan ret = -EOPNOTSUPP; 1735fdfb1e4fSLi Zefan goto out; 1736fdfb1e4fSLi Zefan } 1737fdfb1e4fSLi Zefan 1738fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1739fdfb1e4fSLi Zefan ptr = &transid; 1740b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1741b83cc969SLi Zefan readonly = true; 17426f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 17436f72c7e2SArne Jansen if (vol_args->size > PAGE_CACHE_SIZE) { 17446f72c7e2SArne Jansen ret = -EINVAL; 17456f72c7e2SArne Jansen goto out; 17466f72c7e2SArne Jansen } 17476f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 17486f72c7e2SArne Jansen if (IS_ERR(inherit)) { 17496f72c7e2SArne Jansen ret = PTR_ERR(inherit); 17506f72c7e2SArne Jansen goto out; 17516f72c7e2SArne Jansen } 17526f72c7e2SArne Jansen } 175375eaa0e2SSage Weil 1754fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 17556f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 17568696c533SMiao Xie readonly, inherit); 175775eaa0e2SSage Weil 1758fdfb1e4fSLi Zefan if (ret == 0 && ptr && 175975eaa0e2SSage Weil copy_to_user(arg + 1760fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1761fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 176275eaa0e2SSage Weil ret = -EFAULT; 1763fdfb1e4fSLi Zefan out: 1764f46b5a66SChristoph Hellwig kfree(vol_args); 17656f72c7e2SArne Jansen kfree(inherit); 1766f46b5a66SChristoph Hellwig return ret; 1767f46b5a66SChristoph Hellwig } 1768f46b5a66SChristoph Hellwig 17690caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 17700caa102dSLi Zefan void __user *arg) 17710caa102dSLi Zefan { 1772496ad9aaSAl Viro struct inode *inode = file_inode(file); 17730caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17740caa102dSLi Zefan int ret = 0; 17750caa102dSLi Zefan u64 flags = 0; 17760caa102dSLi Zefan 177733345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 17780caa102dSLi Zefan return -EINVAL; 17790caa102dSLi Zefan 17800caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 17810caa102dSLi Zefan if (btrfs_root_readonly(root)) 17820caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 17830caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 17840caa102dSLi Zefan 17850caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 17860caa102dSLi Zefan ret = -EFAULT; 17870caa102dSLi Zefan 17880caa102dSLi Zefan return ret; 17890caa102dSLi Zefan } 17900caa102dSLi Zefan 17910caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 17920caa102dSLi Zefan void __user *arg) 17930caa102dSLi Zefan { 1794496ad9aaSAl Viro struct inode *inode = file_inode(file); 17950caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17960caa102dSLi Zefan struct btrfs_trans_handle *trans; 17970caa102dSLi Zefan u64 root_flags; 17980caa102dSLi Zefan u64 flags; 17990caa102dSLi Zefan int ret = 0; 18000caa102dSLi Zefan 1801bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1802bd60ea0fSDavid Sterba return -EPERM; 1803bd60ea0fSDavid Sterba 1804b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1805b9ca0664SLiu Bo if (ret) 1806b9ca0664SLiu Bo goto out; 18070caa102dSLi Zefan 1808b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1809b9ca0664SLiu Bo ret = -EINVAL; 1810b9ca0664SLiu Bo goto out_drop_write; 1811b9ca0664SLiu Bo } 18120caa102dSLi Zefan 1813b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1814b9ca0664SLiu Bo ret = -EFAULT; 1815b9ca0664SLiu Bo goto out_drop_write; 1816b9ca0664SLiu Bo } 18170caa102dSLi Zefan 1818b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1819b9ca0664SLiu Bo ret = -EINVAL; 1820b9ca0664SLiu Bo goto out_drop_write; 1821b9ca0664SLiu Bo } 18220caa102dSLi Zefan 1823b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1824b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1825b9ca0664SLiu Bo goto out_drop_write; 1826b9ca0664SLiu Bo } 18270caa102dSLi Zefan 18280caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 18290caa102dSLi Zefan 18300caa102dSLi Zefan /* nothing to do */ 18310caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1832b9ca0664SLiu Bo goto out_drop_sem; 18330caa102dSLi Zefan 18340caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 18352c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 18360caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18370caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 18382c686537SDavid Sterba } else { 18392c686537SDavid Sterba /* 18402c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 18412c686537SDavid Sterba * send 18422c686537SDavid Sterba */ 18432c686537SDavid Sterba spin_lock(&root->root_item_lock); 18442c686537SDavid Sterba if (root->send_in_progress == 0) { 18450caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18460caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 18472c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18482c686537SDavid Sterba } else { 18492c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18502c686537SDavid Sterba btrfs_warn(root->fs_info, 18512c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 18522c686537SDavid Sterba root->root_key.objectid); 18532c686537SDavid Sterba ret = -EPERM; 18542c686537SDavid Sterba goto out_drop_sem; 18552c686537SDavid Sterba } 18562c686537SDavid Sterba } 18570caa102dSLi Zefan 18580caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 18590caa102dSLi Zefan if (IS_ERR(trans)) { 18600caa102dSLi Zefan ret = PTR_ERR(trans); 18610caa102dSLi Zefan goto out_reset; 18620caa102dSLi Zefan } 18630caa102dSLi Zefan 1864b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 18650caa102dSLi Zefan &root->root_key, &root->root_item); 18660caa102dSLi Zefan 18670caa102dSLi Zefan btrfs_commit_transaction(trans, root); 18680caa102dSLi Zefan out_reset: 18690caa102dSLi Zefan if (ret) 18700caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1871b9ca0664SLiu Bo out_drop_sem: 18720caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1873b9ca0664SLiu Bo out_drop_write: 1874b9ca0664SLiu Bo mnt_drop_write_file(file); 1875b9ca0664SLiu Bo out: 18760caa102dSLi Zefan return ret; 18770caa102dSLi Zefan } 18780caa102dSLi Zefan 187976dda93cSYan, Zheng /* 188076dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 188176dda93cSYan, Zheng */ 188276dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 188376dda93cSYan, Zheng { 188476dda93cSYan, Zheng struct btrfs_path *path; 1885175a2b87SJosef Bacik struct btrfs_dir_item *di; 188676dda93cSYan, Zheng struct btrfs_key key; 1887175a2b87SJosef Bacik u64 dir_id; 188876dda93cSYan, Zheng int ret; 188976dda93cSYan, Zheng 189076dda93cSYan, Zheng path = btrfs_alloc_path(); 189176dda93cSYan, Zheng if (!path) 189276dda93cSYan, Zheng return -ENOMEM; 189376dda93cSYan, Zheng 1894175a2b87SJosef Bacik /* Make sure this root isn't set as the default subvol */ 1895175a2b87SJosef Bacik dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 1896175a2b87SJosef Bacik di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, 1897175a2b87SJosef Bacik dir_id, "default", 7, 0); 1898175a2b87SJosef Bacik if (di && !IS_ERR(di)) { 1899175a2b87SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1900175a2b87SJosef Bacik if (key.objectid == root->root_key.objectid) { 190172de6b53SGuangyu Sun ret = -EPERM; 190272de6b53SGuangyu Sun btrfs_err(root->fs_info, "deleting default subvolume " 190372de6b53SGuangyu Sun "%llu is not allowed", key.objectid); 1904175a2b87SJosef Bacik goto out; 1905175a2b87SJosef Bacik } 1906175a2b87SJosef Bacik btrfs_release_path(path); 1907175a2b87SJosef Bacik } 1908175a2b87SJosef Bacik 190976dda93cSYan, Zheng key.objectid = root->root_key.objectid; 191076dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 191176dda93cSYan, Zheng key.offset = (u64)-1; 191276dda93cSYan, Zheng 191376dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 191476dda93cSYan, Zheng &key, path, 0, 0); 191576dda93cSYan, Zheng if (ret < 0) 191676dda93cSYan, Zheng goto out; 191776dda93cSYan, Zheng BUG_ON(ret == 0); 191876dda93cSYan, Zheng 191976dda93cSYan, Zheng ret = 0; 192076dda93cSYan, Zheng if (path->slots[0] > 0) { 192176dda93cSYan, Zheng path->slots[0]--; 192276dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 192376dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 192476dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 192576dda93cSYan, Zheng ret = -ENOTEMPTY; 192676dda93cSYan, Zheng } 192776dda93cSYan, Zheng out: 192876dda93cSYan, Zheng btrfs_free_path(path); 192976dda93cSYan, Zheng return ret; 193076dda93cSYan, Zheng } 193176dda93cSYan, Zheng 1932ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1933ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1934ac8e9819SChris Mason { 1935abc6e134SChris Mason struct btrfs_key test; 1936abc6e134SChris Mason int ret; 1937abc6e134SChris Mason 1938abc6e134SChris Mason test.objectid = sk->min_objectid; 1939abc6e134SChris Mason test.type = sk->min_type; 1940abc6e134SChris Mason test.offset = sk->min_offset; 1941abc6e134SChris Mason 1942abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1943abc6e134SChris Mason if (ret < 0) 1944ac8e9819SChris Mason return 0; 1945abc6e134SChris Mason 1946abc6e134SChris Mason test.objectid = sk->max_objectid; 1947abc6e134SChris Mason test.type = sk->max_type; 1948abc6e134SChris Mason test.offset = sk->max_offset; 1949abc6e134SChris Mason 1950abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1951abc6e134SChris Mason if (ret > 0) 1952ac8e9819SChris Mason return 0; 1953ac8e9819SChris Mason return 1; 1954ac8e9819SChris Mason } 1955ac8e9819SChris Mason 1956ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1957ac8e9819SChris Mason struct btrfs_path *path, 1958ac8e9819SChris Mason struct btrfs_key *key, 1959ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 19609b6e817dSGerhard Heift size_t *buf_size, 1961ba346b35SGerhard Heift char __user *ubuf, 1962ac8e9819SChris Mason unsigned long *sk_offset, 1963ac8e9819SChris Mason int *num_found) 1964ac8e9819SChris Mason { 1965ac8e9819SChris Mason u64 found_transid; 1966ac8e9819SChris Mason struct extent_buffer *leaf; 1967ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1968ac8e9819SChris Mason unsigned long item_off; 1969ac8e9819SChris Mason unsigned long item_len; 1970ac8e9819SChris Mason int nritems; 1971ac8e9819SChris Mason int i; 1972ac8e9819SChris Mason int slot; 1973ac8e9819SChris Mason int ret = 0; 1974ac8e9819SChris Mason 1975ac8e9819SChris Mason leaf = path->nodes[0]; 1976ac8e9819SChris Mason slot = path->slots[0]; 1977ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1978ac8e9819SChris Mason 1979ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1980ac8e9819SChris Mason i = nritems; 1981ac8e9819SChris Mason goto advance_key; 1982ac8e9819SChris Mason } 1983ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1984ac8e9819SChris Mason 1985ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1986ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1987ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1988ac8e9819SChris Mason 198903b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 199003b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 199103b71c6cSGabriel de Perthuis continue; 199203b71c6cSGabriel de Perthuis 19939b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 19948f5f6178SGerhard Heift if (*num_found) { 19958f5f6178SGerhard Heift ret = 1; 19968f5f6178SGerhard Heift goto out; 19978f5f6178SGerhard Heift } 19988f5f6178SGerhard Heift 19998f5f6178SGerhard Heift /* 20008f5f6178SGerhard Heift * return one empty item back for v1, which does not 20018f5f6178SGerhard Heift * handle -EOVERFLOW 20028f5f6178SGerhard Heift */ 20038f5f6178SGerhard Heift 20049b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 2005ac8e9819SChris Mason item_len = 0; 20068f5f6178SGerhard Heift ret = -EOVERFLOW; 20078f5f6178SGerhard Heift } 2008ac8e9819SChris Mason 20099b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 2010ac8e9819SChris Mason ret = 1; 201125c9bc2eSGerhard Heift goto out; 2012ac8e9819SChris Mason } 2013ac8e9819SChris Mason 2014ac8e9819SChris Mason sh.objectid = key->objectid; 2015ac8e9819SChris Mason sh.offset = key->offset; 2016ac8e9819SChris Mason sh.type = key->type; 2017ac8e9819SChris Mason sh.len = item_len; 2018ac8e9819SChris Mason sh.transid = found_transid; 2019ac8e9819SChris Mason 2020ac8e9819SChris Mason /* copy search result header */ 2021ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 2022ba346b35SGerhard Heift ret = -EFAULT; 2023ba346b35SGerhard Heift goto out; 2024ba346b35SGerhard Heift } 2025ba346b35SGerhard Heift 2026ac8e9819SChris Mason *sk_offset += sizeof(sh); 2027ac8e9819SChris Mason 2028ac8e9819SChris Mason if (item_len) { 2029ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2030ac8e9819SChris Mason /* copy the item */ 2031ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2032ba346b35SGerhard Heift item_off, item_len)) { 2033ba346b35SGerhard Heift ret = -EFAULT; 2034ba346b35SGerhard Heift goto out; 2035ba346b35SGerhard Heift } 2036ba346b35SGerhard Heift 2037ac8e9819SChris Mason *sk_offset += item_len; 2038ac8e9819SChris Mason } 2039e2156867SHugo Mills (*num_found)++; 2040ac8e9819SChris Mason 20418f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 20428f5f6178SGerhard Heift goto out; 20438f5f6178SGerhard Heift 204425c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 204525c9bc2eSGerhard Heift ret = 1; 204625c9bc2eSGerhard Heift goto out; 204725c9bc2eSGerhard Heift } 2048ac8e9819SChris Mason } 2049ac8e9819SChris Mason advance_key: 2050ac8e9819SChris Mason ret = 0; 2051abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 2052abc6e134SChris Mason key->offset++; 2053abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 2054abc6e134SChris Mason key->offset = 0; 2055abc6e134SChris Mason key->type++; 2056abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 2057abc6e134SChris Mason key->offset = 0; 2058abc6e134SChris Mason key->type = 0; 2059abc6e134SChris Mason key->objectid++; 2060abc6e134SChris Mason } else 2061abc6e134SChris Mason ret = 1; 206225c9bc2eSGerhard Heift out: 2063ba346b35SGerhard Heift /* 2064ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2065ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2066ba346b35SGerhard Heift * * all items were found 2067ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2068ba346b35SGerhard Heift * leaf 2069ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2070ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2071ba346b35SGerhard Heift */ 2072ac8e9819SChris Mason return ret; 2073ac8e9819SChris Mason } 2074ac8e9819SChris Mason 2075ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 207612544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 20779b6e817dSGerhard Heift size_t *buf_size, 2078ba346b35SGerhard Heift char __user *ubuf) 2079ac8e9819SChris Mason { 2080ac8e9819SChris Mason struct btrfs_root *root; 2081ac8e9819SChris Mason struct btrfs_key key; 2082ac8e9819SChris Mason struct btrfs_path *path; 2083ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 2084ac8e9819SChris Mason int ret; 2085ac8e9819SChris Mason int num_found = 0; 2086ac8e9819SChris Mason unsigned long sk_offset = 0; 2087ac8e9819SChris Mason 20889b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 20899b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 209012544442SGerhard Heift return -EOVERFLOW; 20919b6e817dSGerhard Heift } 209212544442SGerhard Heift 2093ac8e9819SChris Mason path = btrfs_alloc_path(); 2094ac8e9819SChris Mason if (!path) 2095ac8e9819SChris Mason return -ENOMEM; 2096ac8e9819SChris Mason 2097ac8e9819SChris Mason if (sk->tree_id == 0) { 2098ac8e9819SChris Mason /* search the root of the inode that was passed */ 2099ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2100ac8e9819SChris Mason } else { 2101ac8e9819SChris Mason key.objectid = sk->tree_id; 2102ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2103ac8e9819SChris Mason key.offset = (u64)-1; 2104ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2105ac8e9819SChris Mason if (IS_ERR(root)) { 2106efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", 2107ac8e9819SChris Mason sk->tree_id); 2108ac8e9819SChris Mason btrfs_free_path(path); 2109ac8e9819SChris Mason return -ENOENT; 2110ac8e9819SChris Mason } 2111ac8e9819SChris Mason } 2112ac8e9819SChris Mason 2113ac8e9819SChris Mason key.objectid = sk->min_objectid; 2114ac8e9819SChris Mason key.type = sk->min_type; 2115ac8e9819SChris Mason key.offset = sk->min_offset; 2116ac8e9819SChris Mason 2117ac8e9819SChris Mason path->keep_locks = 1; 2118ac8e9819SChris Mason 2119ac8e9819SChris Mason while (1) { 21206174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2121ac8e9819SChris Mason if (ret != 0) { 2122ac8e9819SChris Mason if (ret > 0) 2123ac8e9819SChris Mason ret = 0; 2124ac8e9819SChris Mason goto err; 2125ac8e9819SChris Mason } 2126ba346b35SGerhard Heift ret = copy_to_sk(root, path, &key, sk, buf_size, ubuf, 2127ac8e9819SChris Mason &sk_offset, &num_found); 2128b3b4aa74SDavid Sterba btrfs_release_path(path); 212925c9bc2eSGerhard Heift if (ret) 2130ac8e9819SChris Mason break; 2131ac8e9819SChris Mason 2132ac8e9819SChris Mason } 21338f5f6178SGerhard Heift if (ret > 0) 2134ac8e9819SChris Mason ret = 0; 2135ac8e9819SChris Mason err: 2136ac8e9819SChris Mason sk->nr_items = num_found; 2137ac8e9819SChris Mason btrfs_free_path(path); 2138ac8e9819SChris Mason return ret; 2139ac8e9819SChris Mason } 2140ac8e9819SChris Mason 2141ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2142ac8e9819SChris Mason void __user *argp) 2143ac8e9819SChris Mason { 2144ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2145ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2146ac8e9819SChris Mason struct inode *inode; 2147ac8e9819SChris Mason int ret; 21489b6e817dSGerhard Heift size_t buf_size; 2149ac8e9819SChris Mason 2150ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2151ac8e9819SChris Mason return -EPERM; 2152ac8e9819SChris Mason 2153ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2154ac8e9819SChris Mason 2155ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2156ba346b35SGerhard Heift return -EFAULT; 2157ba346b35SGerhard Heift 2158ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 21599b6e817dSGerhard Heift 2160496ad9aaSAl Viro inode = file_inode(file); 2161ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 21628f5f6178SGerhard Heift 21638f5f6178SGerhard Heift /* 21648f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 21658f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 21668f5f6178SGerhard Heift */ 21678f5f6178SGerhard Heift if (ret == -EOVERFLOW) 21688f5f6178SGerhard Heift ret = 0; 21698f5f6178SGerhard Heift 2170ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2171ac8e9819SChris Mason ret = -EFAULT; 2172ac8e9819SChris Mason return ret; 2173ac8e9819SChris Mason } 2174ac8e9819SChris Mason 217598d377a0STARUISI Hiroaki /* 2176ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2177ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 217898d377a0STARUISI Hiroaki */ 217998d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 218098d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 218198d377a0STARUISI Hiroaki { 218298d377a0STARUISI Hiroaki struct btrfs_root *root; 218398d377a0STARUISI Hiroaki struct btrfs_key key; 2184ac8e9819SChris Mason char *ptr; 218598d377a0STARUISI Hiroaki int ret = -1; 218698d377a0STARUISI Hiroaki int slot; 218798d377a0STARUISI Hiroaki int len; 218898d377a0STARUISI Hiroaki int total_len = 0; 218998d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 219098d377a0STARUISI Hiroaki struct extent_buffer *l; 219198d377a0STARUISI Hiroaki struct btrfs_path *path; 219298d377a0STARUISI Hiroaki 219398d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 219498d377a0STARUISI Hiroaki name[0]='\0'; 219598d377a0STARUISI Hiroaki return 0; 219698d377a0STARUISI Hiroaki } 219798d377a0STARUISI Hiroaki 219898d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 219998d377a0STARUISI Hiroaki if (!path) 220098d377a0STARUISI Hiroaki return -ENOMEM; 220198d377a0STARUISI Hiroaki 2202ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 220398d377a0STARUISI Hiroaki 220498d377a0STARUISI Hiroaki key.objectid = tree_id; 220598d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 220698d377a0STARUISI Hiroaki key.offset = (u64)-1; 220798d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 220898d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2209efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id); 22108ad6fcabSChris Mason ret = -ENOENT; 22118ad6fcabSChris Mason goto out; 221298d377a0STARUISI Hiroaki } 221398d377a0STARUISI Hiroaki 221498d377a0STARUISI Hiroaki key.objectid = dirid; 221598d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 22168ad6fcabSChris Mason key.offset = (u64)-1; 221798d377a0STARUISI Hiroaki 221898d377a0STARUISI Hiroaki while (1) { 221998d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 222098d377a0STARUISI Hiroaki if (ret < 0) 222198d377a0STARUISI Hiroaki goto out; 222218674c6cSFilipe David Borba Manana else if (ret > 0) { 222318674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 222418674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 222518674c6cSFilipe David Borba Manana if (ret < 0) 222618674c6cSFilipe David Borba Manana goto out; 222718674c6cSFilipe David Borba Manana else if (ret > 0) { 2228ac8e9819SChris Mason ret = -ENOENT; 222998d377a0STARUISI Hiroaki goto out; 2230ac8e9819SChris Mason } 223118674c6cSFilipe David Borba Manana } 223218674c6cSFilipe David Borba Manana 223318674c6cSFilipe David Borba Manana l = path->nodes[0]; 223418674c6cSFilipe David Borba Manana slot = path->slots[0]; 223518674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 223698d377a0STARUISI Hiroaki 223798d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 223898d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 223998d377a0STARUISI Hiroaki ptr -= len + 1; 224098d377a0STARUISI Hiroaki total_len += len + 1; 2241a696cf35SFilipe David Borba Manana if (ptr < name) { 2242a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 224398d377a0STARUISI Hiroaki goto out; 2244a696cf35SFilipe David Borba Manana } 224598d377a0STARUISI Hiroaki 224698d377a0STARUISI Hiroaki *(ptr + len) = '/'; 224798d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 224898d377a0STARUISI Hiroaki 224998d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 225098d377a0STARUISI Hiroaki break; 225198d377a0STARUISI Hiroaki 2252b3b4aa74SDavid Sterba btrfs_release_path(path); 225398d377a0STARUISI Hiroaki key.objectid = key.offset; 22548ad6fcabSChris Mason key.offset = (u64)-1; 225598d377a0STARUISI Hiroaki dirid = key.objectid; 225698d377a0STARUISI Hiroaki } 225777906a50SLi Zefan memmove(name, ptr, total_len); 225898d377a0STARUISI Hiroaki name[total_len] = '\0'; 225998d377a0STARUISI Hiroaki ret = 0; 226098d377a0STARUISI Hiroaki out: 226198d377a0STARUISI Hiroaki btrfs_free_path(path); 2262ac8e9819SChris Mason return ret; 2263ac8e9819SChris Mason } 2264ac8e9819SChris Mason 2265ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2266ac8e9819SChris Mason void __user *argp) 2267ac8e9819SChris Mason { 2268ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2269ac8e9819SChris Mason struct inode *inode; 2270ac8e9819SChris Mason int ret; 2271ac8e9819SChris Mason 2272ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2273ac8e9819SChris Mason return -EPERM; 2274ac8e9819SChris Mason 22752354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 22762354d08fSJulia Lawall if (IS_ERR(args)) 22772354d08fSJulia Lawall return PTR_ERR(args); 2278c2b96929SDan Carpenter 2279496ad9aaSAl Viro inode = file_inode(file); 2280ac8e9819SChris Mason 22811b53ac4dSChris Mason if (args->treeid == 0) 22821b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 22831b53ac4dSChris Mason 2284ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2285ac8e9819SChris Mason args->treeid, args->objectid, 2286ac8e9819SChris Mason args->name); 2287ac8e9819SChris Mason 2288ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2289ac8e9819SChris Mason ret = -EFAULT; 2290ac8e9819SChris Mason 2291ac8e9819SChris Mason kfree(args); 229298d377a0STARUISI Hiroaki return ret; 229398d377a0STARUISI Hiroaki } 229498d377a0STARUISI Hiroaki 229576dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 229676dda93cSYan, Zheng void __user *arg) 229776dda93cSYan, Zheng { 229854563d41SAl Viro struct dentry *parent = file->f_path.dentry; 229976dda93cSYan, Zheng struct dentry *dentry; 230076dda93cSYan, Zheng struct inode *dir = parent->d_inode; 230176dda93cSYan, Zheng struct inode *inode; 230276dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 230376dda93cSYan, Zheng struct btrfs_root *dest = NULL; 230476dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 230576dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2306c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2307521e0546SDavid Sterba u64 root_flags; 2308c58aaad2SMiao Xie u64 qgroup_reserved; 230976dda93cSYan, Zheng int namelen; 231076dda93cSYan, Zheng int ret; 231176dda93cSYan, Zheng int err = 0; 231276dda93cSYan, Zheng 231376dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 231476dda93cSYan, Zheng if (IS_ERR(vol_args)) 231576dda93cSYan, Zheng return PTR_ERR(vol_args); 231676dda93cSYan, Zheng 231776dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 231876dda93cSYan, Zheng namelen = strlen(vol_args->name); 231976dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 232076dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 232176dda93cSYan, Zheng err = -EINVAL; 232276dda93cSYan, Zheng goto out; 232376dda93cSYan, Zheng } 232476dda93cSYan, Zheng 2325a561be71SAl Viro err = mnt_want_write_file(file); 232676dda93cSYan, Zheng if (err) 232776dda93cSYan, Zheng goto out; 232876dda93cSYan, Zheng 2329521e0546SDavid Sterba 23305c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 23315c50c9b8SDavid Sterba if (err == -EINTR) 2332e43f998eSDavid Sterba goto out_drop_write; 233376dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 233476dda93cSYan, Zheng if (IS_ERR(dentry)) { 233576dda93cSYan, Zheng err = PTR_ERR(dentry); 233676dda93cSYan, Zheng goto out_unlock_dir; 233776dda93cSYan, Zheng } 233876dda93cSYan, Zheng 233976dda93cSYan, Zheng if (!dentry->d_inode) { 234076dda93cSYan, Zheng err = -ENOENT; 234176dda93cSYan, Zheng goto out_dput; 234276dda93cSYan, Zheng } 234376dda93cSYan, Zheng 234476dda93cSYan, Zheng inode = dentry->d_inode; 23454260f7c7SSage Weil dest = BTRFS_I(inode)->root; 23464260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 23474260f7c7SSage Weil /* 23484260f7c7SSage Weil * Regular user. Only allow this with a special mount 23494260f7c7SSage Weil * option, when the user has write+exec access to the 23504260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23514260f7c7SSage Weil * allowed. 23524260f7c7SSage Weil * 23534260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23544260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23554260f7c7SSage Weil * otherwise be able to delete. 23564260f7c7SSage Weil * 23574260f7c7SSage Weil * Users who want to delete empty subvols should try 23584260f7c7SSage Weil * rmdir(2). 23594260f7c7SSage Weil */ 23604260f7c7SSage Weil err = -EPERM; 23614260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 23624260f7c7SSage Weil goto out_dput; 23634260f7c7SSage Weil 23644260f7c7SSage Weil /* 23654260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 23664260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 23674260f7c7SSage Weil * must be called on the dentry referencing the root 23684260f7c7SSage Weil * of the subvol, not a random directory contained 23694260f7c7SSage Weil * within it. 23704260f7c7SSage Weil */ 23714260f7c7SSage Weil err = -EINVAL; 23724260f7c7SSage Weil if (root == dest) 23734260f7c7SSage Weil goto out_dput; 23744260f7c7SSage Weil 23754260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 23764260f7c7SSage Weil if (err) 23774260f7c7SSage Weil goto out_dput; 23785c39da5bSMiao Xie } 23794260f7c7SSage Weil 23805c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 23814260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 23824260f7c7SSage Weil if (err) 23834260f7c7SSage Weil goto out_dput; 23844260f7c7SSage Weil 238533345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 238676dda93cSYan, Zheng err = -EINVAL; 238776dda93cSYan, Zheng goto out_dput; 238876dda93cSYan, Zheng } 238976dda93cSYan, Zheng 239076dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 2391521e0546SDavid Sterba 2392521e0546SDavid Sterba /* 2393521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2394521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2395521e0546SDavid Sterba * again is not run concurrently. 2396521e0546SDavid Sterba */ 2397521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2398c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2399c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2400c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2401521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2402521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2403521e0546SDavid Sterba } else { 2404521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2405521e0546SDavid Sterba btrfs_warn(root->fs_info, 2406521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2407c55bfa67SFilipe Manana dest->root_key.objectid); 2408521e0546SDavid Sterba err = -EPERM; 2409521e0546SDavid Sterba goto out_dput; 2410521e0546SDavid Sterba } 2411521e0546SDavid Sterba 241276dda93cSYan, Zheng err = d_invalidate(dentry); 241376dda93cSYan, Zheng if (err) 241476dda93cSYan, Zheng goto out_unlock; 241576dda93cSYan, Zheng 241676dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 241776dda93cSYan, Zheng 241876dda93cSYan, Zheng err = may_destroy_subvol(dest); 241976dda93cSYan, Zheng if (err) 242076dda93cSYan, Zheng goto out_up_write; 242176dda93cSYan, Zheng 2422c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2423c58aaad2SMiao Xie /* 2424c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2425c58aaad2SMiao Xie * ref/backref. 2426c58aaad2SMiao Xie */ 2427c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2428ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2429c58aaad2SMiao Xie if (err) 2430c58aaad2SMiao Xie goto out_up_write; 2431c58aaad2SMiao Xie 2432a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2433a22285a6SYan, Zheng if (IS_ERR(trans)) { 2434a22285a6SYan, Zheng err = PTR_ERR(trans); 2435c58aaad2SMiao Xie goto out_release; 2436a22285a6SYan, Zheng } 2437c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2438c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2439a22285a6SYan, Zheng 244076dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 244176dda93cSYan, Zheng dest->root_key.objectid, 244276dda93cSYan, Zheng dentry->d_name.name, 244376dda93cSYan, Zheng dentry->d_name.len); 244479787eaaSJeff Mahoney if (ret) { 244579787eaaSJeff Mahoney err = ret; 244679787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 244779787eaaSJeff Mahoney goto out_end_trans; 244879787eaaSJeff Mahoney } 244976dda93cSYan, Zheng 245076dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 245176dda93cSYan, Zheng 245276dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 245376dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 245476dda93cSYan, Zheng dest->root_item.drop_level = 0; 245576dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 245676dda93cSYan, Zheng 245727cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 245876dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 245976dda93cSYan, Zheng root->fs_info->tree_root, 246076dda93cSYan, Zheng dest->root_key.objectid); 246179787eaaSJeff Mahoney if (ret) { 246279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 246379787eaaSJeff Mahoney err = ret; 246479787eaaSJeff Mahoney goto out_end_trans; 2465d68fc57bSYan, Zheng } 246679787eaaSJeff Mahoney } 2467dd5f9615SStefan Behrens 2468dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2469dd5f9615SStefan Behrens dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 2470dd5f9615SStefan Behrens dest->root_key.objectid); 2471dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2472dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2473dd5f9615SStefan Behrens err = ret; 2474dd5f9615SStefan Behrens goto out_end_trans; 2475dd5f9615SStefan Behrens } 2476dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2477dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2478dd5f9615SStefan Behrens dest->root_item.received_uuid, 2479dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2480dd5f9615SStefan Behrens dest->root_key.objectid); 2481dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2482dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2483dd5f9615SStefan Behrens err = ret; 2484dd5f9615SStefan Behrens goto out_end_trans; 2485dd5f9615SStefan Behrens } 2486dd5f9615SStefan Behrens } 2487dd5f9615SStefan Behrens 248879787eaaSJeff Mahoney out_end_trans: 2489c58aaad2SMiao Xie trans->block_rsv = NULL; 2490c58aaad2SMiao Xie trans->bytes_reserved = 0; 2491531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 249279787eaaSJeff Mahoney if (ret && !err) 249379787eaaSJeff Mahoney err = ret; 249476dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2495c58aaad2SMiao Xie out_release: 2496c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 249776dda93cSYan, Zheng out_up_write: 249876dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 249976dda93cSYan, Zheng out_unlock: 2500521e0546SDavid Sterba if (err) { 2501521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2502c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2503c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2504521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2505521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2506521e0546SDavid Sterba } 250776dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 250876dda93cSYan, Zheng if (!err) { 2509efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 251076dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 251176dda93cSYan, Zheng d_delete(dentry); 251261155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2513fa6ac876SLiu Bo 2514fa6ac876SLiu Bo /* the last ref */ 2515fa6ac876SLiu Bo if (dest->cache_inode) { 2516fa6ac876SLiu Bo iput(dest->cache_inode); 2517fa6ac876SLiu Bo dest->cache_inode = NULL; 2518fa6ac876SLiu Bo } 251976dda93cSYan, Zheng } 252076dda93cSYan, Zheng out_dput: 252176dda93cSYan, Zheng dput(dentry); 252276dda93cSYan, Zheng out_unlock_dir: 252376dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 2524e43f998eSDavid Sterba out_drop_write: 25252a79f17eSAl Viro mnt_drop_write_file(file); 252676dda93cSYan, Zheng out: 252776dda93cSYan, Zheng kfree(vol_args); 252876dda93cSYan, Zheng return err; 252976dda93cSYan, Zheng } 253076dda93cSYan, Zheng 25311e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2532f46b5a66SChristoph Hellwig { 2533496ad9aaSAl Viro struct inode *inode = file_inode(file); 2534f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 25351e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2536c146afadSYan Zheng int ret; 2537c146afadSYan Zheng 253825122d15SIlya Dryomov ret = mnt_want_write_file(file); 253925122d15SIlya Dryomov if (ret) 254025122d15SIlya Dryomov return ret; 2541b83cc969SLi Zefan 254225122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 254325122d15SIlya Dryomov ret = -EROFS; 254425122d15SIlya Dryomov goto out; 25455ac00addSStefan Behrens } 2546f46b5a66SChristoph Hellwig 2547f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2548f46b5a66SChristoph Hellwig case S_IFDIR: 2549e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2550e441d54dSChris Mason ret = -EPERM; 2551e441d54dSChris Mason goto out; 2552e441d54dSChris Mason } 2553de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 25548929ecfaSYan, Zheng if (ret) 25558929ecfaSYan, Zheng goto out; 2556de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2557f46b5a66SChristoph Hellwig break; 2558f46b5a66SChristoph Hellwig case S_IFREG: 2559e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2560e441d54dSChris Mason ret = -EINVAL; 2561e441d54dSChris Mason goto out; 2562e441d54dSChris Mason } 25631e701a32SChris Mason 25641e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 25651e701a32SChris Mason if (!range) { 25661e701a32SChris Mason ret = -ENOMEM; 25671e701a32SChris Mason goto out; 25681e701a32SChris Mason } 25691e701a32SChris Mason 25701e701a32SChris Mason if (argp) { 25711e701a32SChris Mason if (copy_from_user(range, argp, 25721e701a32SChris Mason sizeof(*range))) { 25731e701a32SChris Mason ret = -EFAULT; 25741e701a32SChris Mason kfree(range); 2575683be16eSDan Carpenter goto out; 25761e701a32SChris Mason } 25771e701a32SChris Mason /* compression requires us to start the IO */ 25781e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 25791e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 25801e701a32SChris Mason range->extent_thresh = (u32)-1; 25811e701a32SChris Mason } 25821e701a32SChris Mason } else { 25831e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 25841e701a32SChris Mason range->len = (u64)-1; 25851e701a32SChris Mason } 2586496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 25874cb5300bSChris Mason range, 0, 0); 25884cb5300bSChris Mason if (ret > 0) 25894cb5300bSChris Mason ret = 0; 25901e701a32SChris Mason kfree(range); 2591f46b5a66SChristoph Hellwig break; 25928929ecfaSYan, Zheng default: 25938929ecfaSYan, Zheng ret = -EINVAL; 2594f46b5a66SChristoph Hellwig } 2595e441d54dSChris Mason out: 259625122d15SIlya Dryomov mnt_drop_write_file(file); 2597e441d54dSChris Mason return ret; 2598f46b5a66SChristoph Hellwig } 2599f46b5a66SChristoph Hellwig 2600b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2601f46b5a66SChristoph Hellwig { 2602f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2603f46b5a66SChristoph Hellwig int ret; 2604f46b5a66SChristoph Hellwig 2605e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2606e441d54dSChris Mason return -EPERM; 2607e441d54dSChris Mason 26085ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 26095ac00addSStefan Behrens 1)) { 2610e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2611c9e9f97bSIlya Dryomov } 2612c9e9f97bSIlya Dryomov 26135ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2614dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2615c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2616c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2617c9e9f97bSIlya Dryomov goto out; 2618c9e9f97bSIlya Dryomov } 2619f46b5a66SChristoph Hellwig 26205516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2621f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2622f46b5a66SChristoph Hellwig 2623f46b5a66SChristoph Hellwig kfree(vol_args); 2624c9e9f97bSIlya Dryomov out: 2625c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26265ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2627f46b5a66SChristoph Hellwig return ret; 2628f46b5a66SChristoph Hellwig } 2629f46b5a66SChristoph Hellwig 2630da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2631f46b5a66SChristoph Hellwig { 2632496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2633f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2634f46b5a66SChristoph Hellwig int ret; 2635f46b5a66SChristoph Hellwig 2636e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2637e441d54dSChris Mason return -EPERM; 2638e441d54dSChris Mason 2639da24927bSMiao Xie ret = mnt_want_write_file(file); 2640da24927bSMiao Xie if (ret) 2641da24927bSMiao Xie return ret; 2642c146afadSYan Zheng 2643dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2644c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2645c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2646c9e9f97bSIlya Dryomov goto out; 2647c9e9f97bSIlya Dryomov } 2648f46b5a66SChristoph Hellwig 26495516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2650f46b5a66SChristoph Hellwig 2651183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2652183860f6SAnand Jain 1)) { 2653183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2654183860f6SAnand Jain goto out; 2655183860f6SAnand Jain } 2656183860f6SAnand Jain 2657183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2658183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2659c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26605ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2661183860f6SAnand Jain 2662183860f6SAnand Jain out: 2663183860f6SAnand Jain kfree(vol_args); 26644ac20c70SIlya Dryomov mnt_drop_write_file(file); 2665f46b5a66SChristoph Hellwig return ret; 2666f46b5a66SChristoph Hellwig } 2667f46b5a66SChristoph Hellwig 2668475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2669475f6387SJan Schmidt { 2670027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2671475f6387SJan Schmidt struct btrfs_device *device; 2672475f6387SJan Schmidt struct btrfs_device *next; 2673475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2674027ed2f0SLi Zefan int ret = 0; 2675475f6387SJan Schmidt 2676027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2677027ed2f0SLi Zefan if (!fi_args) 2678027ed2f0SLi Zefan return -ENOMEM; 2679027ed2f0SLi Zefan 2680f7171750SFilipe David Borba Manana mutex_lock(&fs_devices->device_list_mutex); 2681027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2682027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2683475f6387SJan Schmidt 2684475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2685027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2686027ed2f0SLi Zefan fi_args->max_id = device->devid; 2687475f6387SJan Schmidt } 2688475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2689475f6387SJan Schmidt 269080a773fbSDavid Sterba fi_args->nodesize = root->fs_info->super_copy->nodesize; 269180a773fbSDavid Sterba fi_args->sectorsize = root->fs_info->super_copy->sectorsize; 269280a773fbSDavid Sterba fi_args->clone_alignment = root->fs_info->super_copy->sectorsize; 269380a773fbSDavid Sterba 2694027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2695027ed2f0SLi Zefan ret = -EFAULT; 2696475f6387SJan Schmidt 2697027ed2f0SLi Zefan kfree(fi_args); 2698027ed2f0SLi Zefan return ret; 2699475f6387SJan Schmidt } 2700475f6387SJan Schmidt 2701475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2702475f6387SJan Schmidt { 2703475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2704475f6387SJan Schmidt struct btrfs_device *dev; 2705475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2706475f6387SJan Schmidt int ret = 0; 2707475f6387SJan Schmidt char *s_uuid = NULL; 2708475f6387SJan Schmidt 2709475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2710475f6387SJan Schmidt if (IS_ERR(di_args)) 2711475f6387SJan Schmidt return PTR_ERR(di_args); 2712475f6387SJan Schmidt 2713dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2714475f6387SJan Schmidt s_uuid = di_args->uuid; 2715475f6387SJan Schmidt 2716475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2717aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2718475f6387SJan Schmidt 2719475f6387SJan Schmidt if (!dev) { 2720475f6387SJan Schmidt ret = -ENODEV; 2721475f6387SJan Schmidt goto out; 2722475f6387SJan Schmidt } 2723475f6387SJan Schmidt 2724475f6387SJan Schmidt di_args->devid = dev->devid; 2725475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2726475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2727475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2728a27202fbSJim Meyering if (dev->name) { 2729606686eeSJosef Bacik struct rcu_string *name; 2730606686eeSJosef Bacik 2731606686eeSJosef Bacik rcu_read_lock(); 2732606686eeSJosef Bacik name = rcu_dereference(dev->name); 2733606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2734606686eeSJosef Bacik rcu_read_unlock(); 2735a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2736a27202fbSJim Meyering } else { 273799ba55adSStefan Behrens di_args->path[0] = '\0'; 2738a27202fbSJim Meyering } 2739475f6387SJan Schmidt 2740475f6387SJan Schmidt out: 274155793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2742475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2743475f6387SJan Schmidt ret = -EFAULT; 2744475f6387SJan Schmidt 2745475f6387SJan Schmidt kfree(di_args); 2746475f6387SJan Schmidt return ret; 2747475f6387SJan Schmidt } 2748475f6387SJan Schmidt 2749416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off) 2750416161dbSMark Fasheh { 2751416161dbSMark Fasheh struct page *page; 2752416161dbSMark Fasheh pgoff_t index; 2753416161dbSMark Fasheh struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 2754416161dbSMark Fasheh 2755416161dbSMark Fasheh index = off >> PAGE_CACHE_SHIFT; 2756416161dbSMark Fasheh 2757416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2758416161dbSMark Fasheh if (!page) 2759416161dbSMark Fasheh return NULL; 2760416161dbSMark Fasheh 2761416161dbSMark Fasheh if (!PageUptodate(page)) { 2762416161dbSMark Fasheh if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, 2763416161dbSMark Fasheh 0)) 2764416161dbSMark Fasheh return NULL; 2765416161dbSMark Fasheh lock_page(page); 2766416161dbSMark Fasheh if (!PageUptodate(page)) { 2767416161dbSMark Fasheh unlock_page(page); 2768416161dbSMark Fasheh page_cache_release(page); 2769416161dbSMark Fasheh return NULL; 2770416161dbSMark Fasheh } 2771416161dbSMark Fasheh } 2772416161dbSMark Fasheh unlock_page(page); 2773416161dbSMark Fasheh 2774416161dbSMark Fasheh return page; 2775416161dbSMark Fasheh } 2776416161dbSMark Fasheh 277777fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) 277877fe20dcSMark Fasheh { 277977fe20dcSMark Fasheh /* do any pending delalloc/csum calc on src, one way or 278077fe20dcSMark Fasheh another, and lock file content */ 278177fe20dcSMark Fasheh while (1) { 278277fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 278377fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 278477fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 278577fe20dcSMark Fasheh off + len - 1); 2786ff5df9b8SFilipe Manana if ((!ordered || 2787ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2788ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 278977fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2790ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2791ff5df9b8SFilipe Manana if (ordered) 2792ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 279377fe20dcSMark Fasheh break; 2794ff5df9b8SFilipe Manana } 279577fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 279677fe20dcSMark Fasheh if (ordered) 279777fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 279877fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 279977fe20dcSMark Fasheh } 280077fe20dcSMark Fasheh } 280177fe20dcSMark Fasheh 2802416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1, 2803416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2804416161dbSMark Fasheh { 2805416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2806416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2807416161dbSMark Fasheh 2808416161dbSMark Fasheh mutex_unlock(&inode1->i_mutex); 2809416161dbSMark Fasheh mutex_unlock(&inode2->i_mutex); 2810416161dbSMark Fasheh } 2811416161dbSMark Fasheh 2812416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1, 2813416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2814416161dbSMark Fasheh { 2815416161dbSMark Fasheh if (inode1 < inode2) { 2816416161dbSMark Fasheh swap(inode1, inode2); 2817416161dbSMark Fasheh swap(loff1, loff2); 2818416161dbSMark Fasheh } 2819416161dbSMark Fasheh 2820416161dbSMark Fasheh mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); 2821416161dbSMark Fasheh lock_extent_range(inode1, loff1, len); 2822416161dbSMark Fasheh if (inode1 != inode2) { 2823416161dbSMark Fasheh mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); 2824416161dbSMark Fasheh lock_extent_range(inode2, loff2, len); 2825416161dbSMark Fasheh } 2826416161dbSMark Fasheh } 2827416161dbSMark Fasheh 2828416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, 2829416161dbSMark Fasheh u64 dst_loff, u64 len) 2830416161dbSMark Fasheh { 2831416161dbSMark Fasheh int ret = 0; 2832416161dbSMark Fasheh struct page *src_page, *dst_page; 2833416161dbSMark Fasheh unsigned int cmp_len = PAGE_CACHE_SIZE; 2834416161dbSMark Fasheh void *addr, *dst_addr; 2835416161dbSMark Fasheh 2836416161dbSMark Fasheh while (len) { 2837416161dbSMark Fasheh if (len < PAGE_CACHE_SIZE) 2838416161dbSMark Fasheh cmp_len = len; 2839416161dbSMark Fasheh 2840416161dbSMark Fasheh src_page = extent_same_get_page(src, loff); 2841416161dbSMark Fasheh if (!src_page) 2842416161dbSMark Fasheh return -EINVAL; 2843416161dbSMark Fasheh dst_page = extent_same_get_page(dst, dst_loff); 2844416161dbSMark Fasheh if (!dst_page) { 2845416161dbSMark Fasheh page_cache_release(src_page); 2846416161dbSMark Fasheh return -EINVAL; 2847416161dbSMark Fasheh } 2848416161dbSMark Fasheh addr = kmap_atomic(src_page); 2849416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 2850416161dbSMark Fasheh 2851416161dbSMark Fasheh flush_dcache_page(src_page); 2852416161dbSMark Fasheh flush_dcache_page(dst_page); 2853416161dbSMark Fasheh 2854416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 2855416161dbSMark Fasheh ret = BTRFS_SAME_DATA_DIFFERS; 2856416161dbSMark Fasheh 2857416161dbSMark Fasheh kunmap_atomic(addr); 2858416161dbSMark Fasheh kunmap_atomic(dst_addr); 2859416161dbSMark Fasheh page_cache_release(src_page); 2860416161dbSMark Fasheh page_cache_release(dst_page); 2861416161dbSMark Fasheh 2862416161dbSMark Fasheh if (ret) 2863416161dbSMark Fasheh break; 2864416161dbSMark Fasheh 2865416161dbSMark Fasheh loff += cmp_len; 2866416161dbSMark Fasheh dst_loff += cmp_len; 2867416161dbSMark Fasheh len -= cmp_len; 2868416161dbSMark Fasheh } 2869416161dbSMark Fasheh 2870416161dbSMark Fasheh return ret; 2871416161dbSMark Fasheh } 2872416161dbSMark Fasheh 2873416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) 2874416161dbSMark Fasheh { 2875416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 2876416161dbSMark Fasheh 2877416161dbSMark Fasheh if (off + len > inode->i_size || off + len < off) 2878416161dbSMark Fasheh return -EINVAL; 2879416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 2880416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 2881416161dbSMark Fasheh return -EINVAL; 2882416161dbSMark Fasheh 2883416161dbSMark Fasheh return 0; 2884416161dbSMark Fasheh } 2885416161dbSMark Fasheh 2886416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, 2887416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 2888416161dbSMark Fasheh { 2889416161dbSMark Fasheh int ret; 2890416161dbSMark Fasheh 2891416161dbSMark Fasheh /* 2892416161dbSMark Fasheh * btrfs_clone() can't handle extents in the same file 2893416161dbSMark Fasheh * yet. Once that works, we can drop this check and replace it 2894416161dbSMark Fasheh * with a check for the same inode, but overlapping extents. 2895416161dbSMark Fasheh */ 2896416161dbSMark Fasheh if (src == dst) 2897416161dbSMark Fasheh return -EINVAL; 2898416161dbSMark Fasheh 2899416161dbSMark Fasheh btrfs_double_lock(src, loff, dst, dst_loff, len); 2900416161dbSMark Fasheh 2901416161dbSMark Fasheh ret = extent_same_check_offsets(src, loff, len); 2902416161dbSMark Fasheh if (ret) 2903416161dbSMark Fasheh goto out_unlock; 2904416161dbSMark Fasheh 2905416161dbSMark Fasheh ret = extent_same_check_offsets(dst, dst_loff, len); 2906416161dbSMark Fasheh if (ret) 2907416161dbSMark Fasheh goto out_unlock; 2908416161dbSMark Fasheh 2909416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 2910416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 2911416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 2912416161dbSMark Fasheh ret = -EINVAL; 2913416161dbSMark Fasheh goto out_unlock; 2914416161dbSMark Fasheh } 2915416161dbSMark Fasheh 2916416161dbSMark Fasheh ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); 2917416161dbSMark Fasheh if (ret == 0) 2918416161dbSMark Fasheh ret = btrfs_clone(src, dst, loff, len, len, dst_loff); 2919416161dbSMark Fasheh 2920416161dbSMark Fasheh out_unlock: 2921416161dbSMark Fasheh btrfs_double_unlock(src, loff, dst, dst_loff, len); 2922416161dbSMark Fasheh 2923416161dbSMark Fasheh return ret; 2924416161dbSMark Fasheh } 2925416161dbSMark Fasheh 2926416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) 2927416161dbSMark Fasheh 2928416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file, 29291c1c8747SAl Viro struct btrfs_ioctl_same_args __user *argp) 2930416161dbSMark Fasheh { 2931cbf8b8caSMark Fasheh struct btrfs_ioctl_same_args *same; 2932cbf8b8caSMark Fasheh struct btrfs_ioctl_same_extent_info *info; 29331c1c8747SAl Viro struct inode *src = file_inode(file); 2934416161dbSMark Fasheh u64 off; 2935416161dbSMark Fasheh u64 len; 2936416161dbSMark Fasheh int i; 2937416161dbSMark Fasheh int ret; 2938cbf8b8caSMark Fasheh unsigned long size; 2939416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 2940416161dbSMark Fasheh bool is_admin = capable(CAP_SYS_ADMIN); 29411c1c8747SAl Viro u16 count; 2942416161dbSMark Fasheh 2943416161dbSMark Fasheh if (!(file->f_mode & FMODE_READ)) 2944416161dbSMark Fasheh return -EINVAL; 2945416161dbSMark Fasheh 2946416161dbSMark Fasheh ret = mnt_want_write_file(file); 2947416161dbSMark Fasheh if (ret) 2948416161dbSMark Fasheh return ret; 2949416161dbSMark Fasheh 29501c1c8747SAl Viro if (get_user(count, &argp->dest_count)) { 2951416161dbSMark Fasheh ret = -EFAULT; 2952416161dbSMark Fasheh goto out; 2953416161dbSMark Fasheh } 2954416161dbSMark Fasheh 29551c1c8747SAl Viro size = offsetof(struct btrfs_ioctl_same_args __user, info[count]); 2956cbf8b8caSMark Fasheh 29571c1c8747SAl Viro same = memdup_user(argp, size); 2958cbf8b8caSMark Fasheh 2959229eed43SGeyslan G. Bem if (IS_ERR(same)) { 2960229eed43SGeyslan G. Bem ret = PTR_ERR(same); 2961cbf8b8caSMark Fasheh goto out; 2962cbf8b8caSMark Fasheh } 2963cbf8b8caSMark Fasheh 2964cbf8b8caSMark Fasheh off = same->logical_offset; 2965cbf8b8caSMark Fasheh len = same->length; 2966416161dbSMark Fasheh 2967416161dbSMark Fasheh /* 2968416161dbSMark Fasheh * Limit the total length we will dedupe for each operation. 2969416161dbSMark Fasheh * This is intended to bound the total time spent in this 2970416161dbSMark Fasheh * ioctl to something sane. 2971416161dbSMark Fasheh */ 2972416161dbSMark Fasheh if (len > BTRFS_MAX_DEDUPE_LEN) 2973416161dbSMark Fasheh len = BTRFS_MAX_DEDUPE_LEN; 2974416161dbSMark Fasheh 2975416161dbSMark Fasheh if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { 2976416161dbSMark Fasheh /* 2977416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 2978416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 2979416161dbSMark Fasheh * this situation without an update. 2980416161dbSMark Fasheh */ 2981416161dbSMark Fasheh ret = -EINVAL; 2982416161dbSMark Fasheh goto out; 2983416161dbSMark Fasheh } 2984416161dbSMark Fasheh 2985416161dbSMark Fasheh ret = -EISDIR; 2986416161dbSMark Fasheh if (S_ISDIR(src->i_mode)) 2987416161dbSMark Fasheh goto out; 2988416161dbSMark Fasheh 2989416161dbSMark Fasheh ret = -EACCES; 2990416161dbSMark Fasheh if (!S_ISREG(src->i_mode)) 2991416161dbSMark Fasheh goto out; 2992416161dbSMark Fasheh 2993cbf8b8caSMark Fasheh /* pre-format output fields to sane values */ 29941c1c8747SAl Viro for (i = 0; i < count; i++) { 2995cbf8b8caSMark Fasheh same->info[i].bytes_deduped = 0ULL; 2996cbf8b8caSMark Fasheh same->info[i].status = 0; 2997416161dbSMark Fasheh } 2998416161dbSMark Fasheh 29991c1c8747SAl Viro for (i = 0, info = same->info; i < count; i++, info++) { 30001c1c8747SAl Viro struct inode *dst; 30011c1c8747SAl Viro struct fd dst_file = fdget(info->fd); 30021c1c8747SAl Viro if (!dst_file.file) { 3003cbf8b8caSMark Fasheh info->status = -EBADF; 30041c1c8747SAl Viro continue; 3005416161dbSMark Fasheh } 30061c1c8747SAl Viro dst = file_inode(dst_file.file); 3007416161dbSMark Fasheh 30081c1c8747SAl Viro if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) { 3009cbf8b8caSMark Fasheh info->status = -EINVAL; 30101c1c8747SAl Viro } else if (file->f_path.mnt != dst_file.file->f_path.mnt) { 3011cbf8b8caSMark Fasheh info->status = -EXDEV; 30121c1c8747SAl Viro } else if (S_ISDIR(dst->i_mode)) { 3013cbf8b8caSMark Fasheh info->status = -EISDIR; 30141c1c8747SAl Viro } else if (!S_ISREG(dst->i_mode)) { 3015cbf8b8caSMark Fasheh info->status = -EACCES; 30161c1c8747SAl Viro } else { 3017cbf8b8caSMark Fasheh info->status = btrfs_extent_same(src, off, len, dst, 3018cbf8b8caSMark Fasheh info->logical_offset); 3019cbf8b8caSMark Fasheh if (info->status == 0) 3020cbf8b8caSMark Fasheh info->bytes_deduped += len; 30211c1c8747SAl Viro } 30221c1c8747SAl Viro fdput(dst_file); 3023cbf8b8caSMark Fasheh } 3024416161dbSMark Fasheh 3025cbf8b8caSMark Fasheh ret = copy_to_user(argp, same, size); 3026cbf8b8caSMark Fasheh if (ret) 3027416161dbSMark Fasheh ret = -EFAULT; 3028416161dbSMark Fasheh 3029416161dbSMark Fasheh out: 3030416161dbSMark Fasheh mnt_drop_write_file(file); 3031416161dbSMark Fasheh return ret; 3032416161dbSMark Fasheh } 3033416161dbSMark Fasheh 3034fcebe456SJosef Bacik /* Helper to check and see if this root currently has a ref on the given disk 3035fcebe456SJosef Bacik * bytenr. If it does then we need to update the quota for this root. This 3036fcebe456SJosef Bacik * doesn't do anything if quotas aren't enabled. 3037fcebe456SJosef Bacik */ 3038fcebe456SJosef Bacik static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3039fcebe456SJosef Bacik u64 disko) 3040fcebe456SJosef Bacik { 3041fcebe456SJosef Bacik struct seq_list tree_mod_seq_elem = {}; 3042fcebe456SJosef Bacik struct ulist *roots; 3043fcebe456SJosef Bacik struct ulist_iterator uiter; 3044fcebe456SJosef Bacik struct ulist_node *root_node = NULL; 3045fcebe456SJosef Bacik int ret; 3046fcebe456SJosef Bacik 3047fcebe456SJosef Bacik if (!root->fs_info->quota_enabled) 3048fcebe456SJosef Bacik return 1; 3049fcebe456SJosef Bacik 3050fcebe456SJosef Bacik btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3051fcebe456SJosef Bacik ret = btrfs_find_all_roots(trans, root->fs_info, disko, 3052fcebe456SJosef Bacik tree_mod_seq_elem.seq, &roots); 3053fcebe456SJosef Bacik if (ret < 0) 3054fcebe456SJosef Bacik goto out; 3055fcebe456SJosef Bacik ret = 0; 3056fcebe456SJosef Bacik ULIST_ITER_INIT(&uiter); 3057fcebe456SJosef Bacik while ((root_node = ulist_next(roots, &uiter))) { 3058fcebe456SJosef Bacik if (root_node->val == root->objectid) { 3059fcebe456SJosef Bacik ret = 1; 3060fcebe456SJosef Bacik break; 3061fcebe456SJosef Bacik } 3062fcebe456SJosef Bacik } 3063fcebe456SJosef Bacik ulist_free(roots); 3064fcebe456SJosef Bacik out: 3065fcebe456SJosef Bacik btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3066fcebe456SJosef Bacik return ret; 3067fcebe456SJosef Bacik } 3068fcebe456SJosef Bacik 3069f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3070f82a9901SFilipe Manana struct inode *inode, 3071f82a9901SFilipe Manana u64 endoff, 3072f82a9901SFilipe Manana const u64 destoff, 3073f82a9901SFilipe Manana const u64 olen) 3074f82a9901SFilipe Manana { 3075f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3076f82a9901SFilipe Manana int ret; 3077f82a9901SFilipe Manana 3078f82a9901SFilipe Manana inode_inc_iversion(inode); 3079f82a9901SFilipe Manana inode->i_mtime = inode->i_ctime = CURRENT_TIME; 3080f82a9901SFilipe Manana /* 3081f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3082f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3083f82a9901SFilipe Manana */ 3084f82a9901SFilipe Manana if (endoff > destoff + olen) 3085f82a9901SFilipe Manana endoff = destoff + olen; 3086f82a9901SFilipe Manana if (endoff > inode->i_size) 3087f82a9901SFilipe Manana btrfs_i_size_write(inode, endoff); 3088f82a9901SFilipe Manana 3089f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3090f82a9901SFilipe Manana if (ret) { 3091f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3092f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3093f82a9901SFilipe Manana goto out; 3094f82a9901SFilipe Manana } 3095f82a9901SFilipe Manana ret = btrfs_end_transaction(trans, root); 3096f82a9901SFilipe Manana out: 3097f82a9901SFilipe Manana return ret; 3098f82a9901SFilipe Manana } 3099f82a9901SFilipe Manana 31007ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode, 31017ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 31027ffbb598SFilipe Manana const struct btrfs_path *path, 31037ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 31047ffbb598SFilipe Manana const u64 hole_offset, 31057ffbb598SFilipe Manana const u64 hole_len) 31067ffbb598SFilipe Manana { 31077ffbb598SFilipe Manana struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 31087ffbb598SFilipe Manana struct extent_map *em; 31097ffbb598SFilipe Manana int ret; 31107ffbb598SFilipe Manana 31117ffbb598SFilipe Manana em = alloc_extent_map(); 31127ffbb598SFilipe Manana if (!em) { 31137ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31147ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31157ffbb598SFilipe Manana return; 31167ffbb598SFilipe Manana } 31177ffbb598SFilipe Manana 31187ffbb598SFilipe Manana if (fi) { 31197ffbb598SFilipe Manana btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 31207ffbb598SFilipe Manana em->generation = -1; 31217ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 31227ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 31237ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31247ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31257ffbb598SFilipe Manana } else { 31267ffbb598SFilipe Manana em->start = hole_offset; 31277ffbb598SFilipe Manana em->len = hole_len; 31287ffbb598SFilipe Manana em->ram_bytes = em->len; 31297ffbb598SFilipe Manana em->orig_start = hole_offset; 31307ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 31317ffbb598SFilipe Manana em->block_len = 0; 31327ffbb598SFilipe Manana em->orig_block_len = 0; 31337ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 31347ffbb598SFilipe Manana em->generation = trans->transid; 31357ffbb598SFilipe Manana } 31367ffbb598SFilipe Manana 31377ffbb598SFilipe Manana while (1) { 31387ffbb598SFilipe Manana write_lock(&em_tree->lock); 31397ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 31407ffbb598SFilipe Manana write_unlock(&em_tree->lock); 31417ffbb598SFilipe Manana if (ret != -EEXIST) { 31427ffbb598SFilipe Manana free_extent_map(em); 31437ffbb598SFilipe Manana break; 31447ffbb598SFilipe Manana } 31457ffbb598SFilipe Manana btrfs_drop_extent_cache(inode, em->start, 31467ffbb598SFilipe Manana em->start + em->len - 1, 0); 31477ffbb598SFilipe Manana } 31487ffbb598SFilipe Manana 31497ffbb598SFilipe Manana if (unlikely(ret)) 31507ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31517ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31527ffbb598SFilipe Manana } 31537ffbb598SFilipe Manana 315432b7c687SMark Fasheh /** 315532b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 315632b7c687SMark Fasheh * 315732b7c687SMark Fasheh * @src: Inode to clone from 315832b7c687SMark Fasheh * @inode: Inode to clone to 315932b7c687SMark Fasheh * @off: Offset within source to start clone from 316032b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 316132b7c687SMark Fasheh * @olen_aligned: Block-aligned value of olen, extent_same uses 316232b7c687SMark Fasheh * identical values here 316332b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 316432b7c687SMark Fasheh */ 316532b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3166f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 3167f82a9901SFilipe Manana const u64 destoff) 3168f46b5a66SChristoph Hellwig { 3169f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 317032b7c687SMark Fasheh struct btrfs_path *path = NULL; 3171f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 317232b7c687SMark Fasheh struct btrfs_trans_handle *trans; 317332b7c687SMark Fasheh char *buf = NULL; 3174ae01a0abSYan Zheng struct btrfs_key key; 3175f46b5a66SChristoph Hellwig u32 nritems; 3176f46b5a66SChristoph Hellwig int slot; 3177ae01a0abSYan Zheng int ret; 3178fcebe456SJosef Bacik int no_quota; 3179f82a9901SFilipe Manana const u64 len = olen_aligned; 3180fcebe456SJosef Bacik u64 last_disko = 0; 3181f82a9901SFilipe Manana u64 last_dest_end = destoff; 3182ae01a0abSYan Zheng 3183ae01a0abSYan Zheng ret = -ENOMEM; 3184ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 3185ae01a0abSYan Zheng if (!buf) 318632b7c687SMark Fasheh return ret; 3187ae01a0abSYan Zheng 3188ae01a0abSYan Zheng path = btrfs_alloc_path(); 3189ae01a0abSYan Zheng if (!path) { 3190ae01a0abSYan Zheng vfree(buf); 319132b7c687SMark Fasheh return ret; 3192ae01a0abSYan Zheng } 319332b7c687SMark Fasheh 3194ae01a0abSYan Zheng path->reada = 2; 3195c5c9cd4dSSage Weil /* clone data */ 319633345d01SLi Zefan key.objectid = btrfs_ino(src); 3197ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 31982c463823SFilipe Manana key.offset = off; 3199f46b5a66SChristoph Hellwig 3200f46b5a66SChristoph Hellwig while (1) { 3201f46b5a66SChristoph Hellwig /* 3202f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3203f46b5a66SChristoph Hellwig * tree. 3204f46b5a66SChristoph Hellwig */ 3205e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3206362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3207362a20c5SDavid Sterba 0, 0); 3208f46b5a66SChristoph Hellwig if (ret < 0) 3209f46b5a66SChristoph Hellwig goto out; 32102c463823SFilipe Manana /* 32112c463823SFilipe Manana * First search, if no extent item that starts at offset off was 32122c463823SFilipe Manana * found but the previous item is an extent item, it's possible 32132c463823SFilipe Manana * it might overlap our target range, therefore process it. 32142c463823SFilipe Manana */ 32152c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 32162c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 32172c463823SFilipe Manana path->slots[0] - 1); 32182c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 32192c463823SFilipe Manana path->slots[0]--; 32202c463823SFilipe Manana } 3221f46b5a66SChristoph Hellwig 3222ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3223e4355f34SFilipe David Borba Manana process_slot: 3224fcebe456SJosef Bacik no_quota = 1; 3225ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3226362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3227f46b5a66SChristoph Hellwig if (ret < 0) 3228f46b5a66SChristoph Hellwig goto out; 3229f46b5a66SChristoph Hellwig if (ret > 0) 3230f46b5a66SChristoph Hellwig break; 3231ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3232f46b5a66SChristoph Hellwig } 3233f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3234f46b5a66SChristoph Hellwig slot = path->slots[0]; 3235f46b5a66SChristoph Hellwig 3236ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3237d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 323833345d01SLi Zefan key.objectid != btrfs_ino(src)) 3239f46b5a66SChristoph Hellwig break; 3240f46b5a66SChristoph Hellwig 3241c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 3242c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3243c5c9cd4dSSage Weil int type; 324431840ae1SZheng Yan u32 size; 324531840ae1SZheng Yan struct btrfs_key new_key; 3246c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3247c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3248c5c9cd4dSSage Weil u8 comp; 3249f82a9901SFilipe Manana u64 drop_start; 325031840ae1SZheng Yan 3251c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3252c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3253c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3254c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3255c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3256c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3257d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3258d397712bSChris Mason extent); 3259d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3260d397712bSChris Mason extent); 3261c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3262d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3263d397712bSChris Mason extent); 3264c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3265c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3266c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3267c5c9cd4dSSage Weil extent); 3268c5c9cd4dSSage Weil } 326931840ae1SZheng Yan 32702c463823SFilipe Manana /* 32712c463823SFilipe Manana * The first search might have left us at an extent 32722c463823SFilipe Manana * item that ends before our target range's start, can 32732c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 32742c463823SFilipe Manana */ 32752c463823SFilipe Manana if (key.offset + datal <= off) { 3276e4355f34SFilipe David Borba Manana path->slots[0]++; 3277e4355f34SFilipe David Borba Manana goto process_slot; 32782c463823SFilipe Manana } else if (key.offset >= off + len) { 32792c463823SFilipe Manana break; 3280e4355f34SFilipe David Borba Manana } 3281e4355f34SFilipe David Borba Manana 3282e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3283e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3284e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3285e4355f34SFilipe David Borba Manana size); 3286e4355f34SFilipe David Borba Manana 3287e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3288e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3289c5c9cd4dSSage Weil 329031840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 329133345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 32924d728ec7SLi Zefan if (off <= key.offset) 3293c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 32944d728ec7SLi Zefan else 32954d728ec7SLi Zefan new_key.offset = destoff; 3296c5c9cd4dSSage Weil 3297b6f3409bSSage Weil /* 3298f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3299f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3300f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3301f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3302f82a9901SFilipe Manana * partially overlaps it). 3303f82a9901SFilipe Manana */ 3304f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3305f82a9901SFilipe Manana drop_start = last_dest_end; 3306f82a9901SFilipe Manana else 3307f82a9901SFilipe Manana drop_start = new_key.offset; 3308f82a9901SFilipe Manana 3309f82a9901SFilipe Manana /* 3310b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3311b6f3409bSSage Weil * 1 - add new extent 3312b6f3409bSSage Weil * 1 - inode update 3313b6f3409bSSage Weil */ 3314b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3315a22285a6SYan, Zheng if (IS_ERR(trans)) { 3316a22285a6SYan, Zheng ret = PTR_ERR(trans); 3317a22285a6SYan, Zheng goto out; 3318a22285a6SYan, Zheng } 3319a22285a6SYan, Zheng 3320c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3321c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3322d72c0842SLi Zefan /* 3323d72c0842SLi Zefan * a | --- range to clone ---| b 3324d72c0842SLi Zefan * | ------------- extent ------------- | 3325d72c0842SLi Zefan */ 3326d72c0842SLi Zefan 332793915584SAntonio Ospite /* subtract range b */ 3328d72c0842SLi Zefan if (key.offset + datal > off + len) 3329d72c0842SLi Zefan datal = off + len - key.offset; 3330d72c0842SLi Zefan 333193915584SAntonio Ospite /* subtract range a */ 3332a22285a6SYan, Zheng if (off > key.offset) { 3333a22285a6SYan, Zheng datao += off - key.offset; 3334a22285a6SYan, Zheng datal -= off - key.offset; 3335a22285a6SYan, Zheng } 3336a22285a6SYan, Zheng 33375dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3338f82a9901SFilipe Manana drop_start, 3339a22285a6SYan, Zheng new_key.offset + datal, 33402671485dSJosef Bacik 1); 334179787eaaSJeff Mahoney if (ret) { 33423f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 334300fdf13aSLiu Bo btrfs_abort_transaction(trans, 334400fdf13aSLiu Bo root, ret); 334579787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 334679787eaaSJeff Mahoney goto out; 334779787eaaSJeff Mahoney } 3348a22285a6SYan, Zheng 334931840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 335031840ae1SZheng Yan &new_key, size); 335179787eaaSJeff Mahoney if (ret) { 335279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 335379787eaaSJeff Mahoney ret); 335479787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 335579787eaaSJeff Mahoney goto out; 335679787eaaSJeff Mahoney } 335731840ae1SZheng Yan 335831840ae1SZheng Yan leaf = path->nodes[0]; 335931840ae1SZheng Yan slot = path->slots[0]; 336031840ae1SZheng Yan write_extent_buffer(leaf, buf, 336131840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 336231840ae1SZheng Yan size); 3363ae01a0abSYan Zheng 3364f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3365f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3366c5c9cd4dSSage Weil 3367c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3368c5c9cd4dSSage Weil if (!disko) 3369c5c9cd4dSSage Weil datao = 0; 3370c5c9cd4dSSage Weil 3371c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3372c5c9cd4dSSage Weil datao); 3373c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3374c5c9cd4dSSage Weil datal); 3375fcebe456SJosef Bacik 3376fcebe456SJosef Bacik /* 3377fcebe456SJosef Bacik * We need to look up the roots that point at 3378fcebe456SJosef Bacik * this bytenr and see if the new root does. If 3379fcebe456SJosef Bacik * it does not we need to make sure we update 3380fcebe456SJosef Bacik * quotas appropriately. 3381fcebe456SJosef Bacik */ 3382fcebe456SJosef Bacik if (disko && root != BTRFS_I(src)->root && 3383fcebe456SJosef Bacik disko != last_disko) { 3384fcebe456SJosef Bacik no_quota = check_ref(trans, root, 3385fcebe456SJosef Bacik disko); 3386fcebe456SJosef Bacik if (no_quota < 0) { 3387fcebe456SJosef Bacik btrfs_abort_transaction(trans, 3388fcebe456SJosef Bacik root, 3389fcebe456SJosef Bacik ret); 3390fcebe456SJosef Bacik btrfs_end_transaction(trans, 3391fcebe456SJosef Bacik root); 3392fcebe456SJosef Bacik ret = no_quota; 3393fcebe456SJosef Bacik goto out; 3394fcebe456SJosef Bacik } 3395fcebe456SJosef Bacik } 3396fcebe456SJosef Bacik 3397c5c9cd4dSSage Weil if (disko) { 3398c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3399ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 34005d4f98a2SYan Zheng disko, diskl, 0, 3401f46b5a66SChristoph Hellwig root->root_key.objectid, 340233345d01SLi Zefan btrfs_ino(inode), 340366d7e7f0SArne Jansen new_key.offset - datao, 3404fcebe456SJosef Bacik no_quota); 340579787eaaSJeff Mahoney if (ret) { 340679787eaaSJeff Mahoney btrfs_abort_transaction(trans, 340779787eaaSJeff Mahoney root, 340879787eaaSJeff Mahoney ret); 340979787eaaSJeff Mahoney btrfs_end_transaction(trans, 341079787eaaSJeff Mahoney root); 341179787eaaSJeff Mahoney goto out; 341279787eaaSJeff Mahoney 341379787eaaSJeff Mahoney } 3414f46b5a66SChristoph Hellwig } 3415c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3416c5c9cd4dSSage Weil u64 skip = 0; 3417c5c9cd4dSSage Weil u64 trim = 0; 3418d3ecfcdfSLiu Bo u64 aligned_end = 0; 3419d3ecfcdfSLiu Bo 3420c5c9cd4dSSage Weil if (off > key.offset) { 3421c5c9cd4dSSage Weil skip = off - key.offset; 3422c5c9cd4dSSage Weil new_key.offset += skip; 342331840ae1SZheng Yan } 3424d397712bSChris Mason 3425c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3426c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3427d397712bSChris Mason 3428c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3429c5c9cd4dSSage Weil ret = -EINVAL; 3430a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 3431c5c9cd4dSSage Weil goto out; 343231840ae1SZheng Yan } 3433c5c9cd4dSSage Weil size -= skip + trim; 3434c5c9cd4dSSage Weil datal -= skip + trim; 3435a22285a6SYan, Zheng 3436d3ecfcdfSLiu Bo aligned_end = ALIGN(new_key.offset + datal, 3437d3ecfcdfSLiu Bo root->sectorsize); 34385dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3439f82a9901SFilipe Manana drop_start, 3440d3ecfcdfSLiu Bo aligned_end, 34412671485dSJosef Bacik 1); 344279787eaaSJeff Mahoney if (ret) { 34433f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 34443a29bc09SChris Mason btrfs_abort_transaction(trans, 34453a29bc09SChris Mason root, ret); 344679787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 344779787eaaSJeff Mahoney goto out; 344879787eaaSJeff Mahoney } 3449a22285a6SYan, Zheng 3450c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 3451c5c9cd4dSSage Weil &new_key, size); 345279787eaaSJeff Mahoney if (ret) { 345379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 345479787eaaSJeff Mahoney ret); 345579787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 345679787eaaSJeff Mahoney goto out; 345779787eaaSJeff Mahoney } 3458c5c9cd4dSSage Weil 3459c5c9cd4dSSage Weil if (skip) { 3460d397712bSChris Mason u32 start = 3461d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 3462c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 3463c5c9cd4dSSage Weil datal); 3464c5c9cd4dSSage Weil } 3465c5c9cd4dSSage Weil 3466c5c9cd4dSSage Weil leaf = path->nodes[0]; 3467c5c9cd4dSSage Weil slot = path->slots[0]; 3468c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 3469c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 3470c5c9cd4dSSage Weil size); 3471c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 34727ffbb598SFilipe Manana extent = btrfs_item_ptr(leaf, slot, 34737ffbb598SFilipe Manana struct btrfs_file_extent_item); 3474c5c9cd4dSSage Weil } 3475c5c9cd4dSSage Weil 34767ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 34777ffbb598SFilipe Manana if (drop_start < new_key.offset) 34787ffbb598SFilipe Manana clone_update_extent_map(inode, trans, 34797ffbb598SFilipe Manana path, NULL, drop_start, 34807ffbb598SFilipe Manana new_key.offset - drop_start); 34817ffbb598SFilipe Manana 34827ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, 34837ffbb598SFilipe Manana extent, 0, 0); 34847ffbb598SFilipe Manana 3485c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3486b3b4aa74SDavid Sterba btrfs_release_path(path); 3487c5c9cd4dSSage Weil 3488f82a9901SFilipe Manana last_dest_end = new_key.offset + datal; 3489f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3490f82a9901SFilipe Manana last_dest_end, 3491f82a9901SFilipe Manana destoff, olen); 3492f82a9901SFilipe Manana if (ret) 349379787eaaSJeff Mahoney goto out; 34942c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 34952c463823SFilipe Manana break; 3496a22285a6SYan, Zheng } 3497b3b4aa74SDavid Sterba btrfs_release_path(path); 3498ae01a0abSYan Zheng key.offset++; 3499ae01a0abSYan Zheng } 3500f46b5a66SChristoph Hellwig ret = 0; 350132b7c687SMark Fasheh 3502f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3503f82a9901SFilipe Manana /* 3504f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3505f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3506f82a9901SFilipe Manana */ 3507f82a9901SFilipe Manana btrfs_release_path(path); 3508f82a9901SFilipe Manana 3509f82a9901SFilipe Manana /* 3510f82a9901SFilipe Manana * 1 - remove extent(s) 3511f82a9901SFilipe Manana * 1 - inode update 3512f82a9901SFilipe Manana */ 3513f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3514f82a9901SFilipe Manana if (IS_ERR(trans)) { 3515f82a9901SFilipe Manana ret = PTR_ERR(trans); 3516f82a9901SFilipe Manana goto out; 3517f82a9901SFilipe Manana } 3518f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3519f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3520f82a9901SFilipe Manana if (ret) { 3521f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 3522f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3523f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3524f82a9901SFilipe Manana goto out; 3525f82a9901SFilipe Manana } 3526f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 3527f82a9901SFilipe Manana destoff, olen); 35287ffbb598SFilipe Manana if (ret) 35297ffbb598SFilipe Manana goto out; 35307ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, NULL, last_dest_end, 35317ffbb598SFilipe Manana destoff + len - last_dest_end); 3532f82a9901SFilipe Manana } 3533f82a9901SFilipe Manana 3534f46b5a66SChristoph Hellwig out: 353532b7c687SMark Fasheh btrfs_free_path(path); 353632b7c687SMark Fasheh vfree(buf); 353732b7c687SMark Fasheh return ret; 353832b7c687SMark Fasheh } 353932b7c687SMark Fasheh 354032b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 354132b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 354232b7c687SMark Fasheh { 354354563d41SAl Viro struct inode *inode = file_inode(file); 354432b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 354532b7c687SMark Fasheh struct fd src_file; 354632b7c687SMark Fasheh struct inode *src; 354732b7c687SMark Fasheh int ret; 354832b7c687SMark Fasheh u64 len = olen; 354932b7c687SMark Fasheh u64 bs = root->fs_info->sb->s_blocksize; 355032b7c687SMark Fasheh int same_inode = 0; 355132b7c687SMark Fasheh 355232b7c687SMark Fasheh /* 355332b7c687SMark Fasheh * TODO: 355432b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 355532b7c687SMark Fasheh * decompress into destination's address_space (the file offset 355632b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 355732b7c687SMark Fasheh * otherwise reinsert) a subrange. 355800fdf13aSLiu Bo * 355900fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 356000fdf13aSLiu Bo * be either compressed or non-compressed. 356132b7c687SMark Fasheh */ 356232b7c687SMark Fasheh 356332b7c687SMark Fasheh /* the destination must be opened for writing */ 356432b7c687SMark Fasheh if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 356532b7c687SMark Fasheh return -EINVAL; 356632b7c687SMark Fasheh 356732b7c687SMark Fasheh if (btrfs_root_readonly(root)) 356832b7c687SMark Fasheh return -EROFS; 356932b7c687SMark Fasheh 357032b7c687SMark Fasheh ret = mnt_want_write_file(file); 357132b7c687SMark Fasheh if (ret) 357232b7c687SMark Fasheh return ret; 357332b7c687SMark Fasheh 357432b7c687SMark Fasheh src_file = fdget(srcfd); 357532b7c687SMark Fasheh if (!src_file.file) { 357632b7c687SMark Fasheh ret = -EBADF; 357732b7c687SMark Fasheh goto out_drop_write; 357832b7c687SMark Fasheh } 357932b7c687SMark Fasheh 358032b7c687SMark Fasheh ret = -EXDEV; 358132b7c687SMark Fasheh if (src_file.file->f_path.mnt != file->f_path.mnt) 358232b7c687SMark Fasheh goto out_fput; 358332b7c687SMark Fasheh 358432b7c687SMark Fasheh src = file_inode(src_file.file); 358532b7c687SMark Fasheh 358632b7c687SMark Fasheh ret = -EINVAL; 358732b7c687SMark Fasheh if (src == inode) 358832b7c687SMark Fasheh same_inode = 1; 358932b7c687SMark Fasheh 359032b7c687SMark Fasheh /* the src must be open for reading */ 359132b7c687SMark Fasheh if (!(src_file.file->f_mode & FMODE_READ)) 359232b7c687SMark Fasheh goto out_fput; 359332b7c687SMark Fasheh 359432b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 359532b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 359632b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 359732b7c687SMark Fasheh goto out_fput; 359832b7c687SMark Fasheh 359932b7c687SMark Fasheh ret = -EISDIR; 360032b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 360132b7c687SMark Fasheh goto out_fput; 360232b7c687SMark Fasheh 360332b7c687SMark Fasheh ret = -EXDEV; 360432b7c687SMark Fasheh if (src->i_sb != inode->i_sb) 360532b7c687SMark Fasheh goto out_fput; 360632b7c687SMark Fasheh 360732b7c687SMark Fasheh if (!same_inode) { 360832b7c687SMark Fasheh if (inode < src) { 360932b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 361032b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 361132b7c687SMark Fasheh } else { 361232b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 361332b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 361432b7c687SMark Fasheh } 361532b7c687SMark Fasheh } else { 361632b7c687SMark Fasheh mutex_lock(&src->i_mutex); 361732b7c687SMark Fasheh } 361832b7c687SMark Fasheh 361932b7c687SMark Fasheh /* determine range to clone */ 362032b7c687SMark Fasheh ret = -EINVAL; 362132b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 362232b7c687SMark Fasheh goto out_unlock; 362332b7c687SMark Fasheh if (len == 0) 362432b7c687SMark Fasheh olen = len = src->i_size - off; 362532b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 362632b7c687SMark Fasheh if (off + len == src->i_size) 362732b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 362832b7c687SMark Fasheh 362932b7c687SMark Fasheh /* verify the end result is block aligned */ 363032b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 363132b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 363232b7c687SMark Fasheh goto out_unlock; 363332b7c687SMark Fasheh 363432b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 363532b7c687SMark Fasheh if (same_inode) { 363632b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 363732b7c687SMark Fasheh goto out_unlock; 363832b7c687SMark Fasheh } 363932b7c687SMark Fasheh 364032b7c687SMark Fasheh if (destoff > inode->i_size) { 364132b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 364232b7c687SMark Fasheh if (ret) 364332b7c687SMark Fasheh goto out_unlock; 364432b7c687SMark Fasheh } 364532b7c687SMark Fasheh 3646c125b8bfSFilipe Manana /* 3647c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3648c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3649c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3650c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3651c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3652c125b8bfSFilipe Manana */ 3653c125b8bfSFilipe Manana if (same_inode) { 3654c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3655c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 365632b7c687SMark Fasheh 3657c125b8bfSFilipe Manana lock_extent_range(src, lock_start, lock_len); 3658c125b8bfSFilipe Manana } else { 365932b7c687SMark Fasheh lock_extent_range(src, off, len); 3660c125b8bfSFilipe Manana lock_extent_range(inode, destoff, len); 3661c125b8bfSFilipe Manana } 366232b7c687SMark Fasheh 366332b7c687SMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff); 366432b7c687SMark Fasheh 3665c125b8bfSFilipe Manana if (same_inode) { 3666c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3667c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3668c125b8bfSFilipe Manana 3669c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3670c125b8bfSFilipe Manana } else { 3671aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 3672c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(inode)->io_tree, destoff, 3673c125b8bfSFilipe Manana destoff + len - 1); 3674c125b8bfSFilipe Manana } 3675c125b8bfSFilipe Manana /* 3676c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3677c125b8bfSFilipe Manana * data immediately and not the previous data. 3678c125b8bfSFilipe Manana */ 3679c125b8bfSFilipe Manana truncate_inode_pages_range(&inode->i_data, destoff, 3680c125b8bfSFilipe Manana PAGE_CACHE_ALIGN(destoff + len) - 1); 3681f46b5a66SChristoph Hellwig out_unlock: 3682c57c2b3eSFilipe David Borba Manana if (!same_inode) { 3683c57c2b3eSFilipe David Borba Manana if (inode < src) { 3684f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 3685f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 3686c57c2b3eSFilipe David Borba Manana } else { 3687c57c2b3eSFilipe David Borba Manana mutex_unlock(&inode->i_mutex); 3688c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3689c57c2b3eSFilipe David Borba Manana } 3690c57c2b3eSFilipe David Borba Manana } else { 3691c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3692c57c2b3eSFilipe David Borba Manana } 3693f46b5a66SChristoph Hellwig out_fput: 36942903ff01SAl Viro fdput(src_file); 3695ab67b7c1SYan Zheng out_drop_write: 36962a79f17eSAl Viro mnt_drop_write_file(file); 3697f46b5a66SChristoph Hellwig return ret; 3698f46b5a66SChristoph Hellwig } 3699f46b5a66SChristoph Hellwig 37007a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 3701c5c9cd4dSSage Weil { 3702c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 3703c5c9cd4dSSage Weil 37047a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 3705c5c9cd4dSSage Weil return -EFAULT; 3706c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 3707c5c9cd4dSSage Weil args.src_length, args.dest_offset); 3708c5c9cd4dSSage Weil } 3709c5c9cd4dSSage Weil 3710f46b5a66SChristoph Hellwig /* 3711f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3712f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3713f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3714f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3715f46b5a66SChristoph Hellwig */ 3716b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3717f46b5a66SChristoph Hellwig { 3718496ad9aaSAl Viro struct inode *inode = file_inode(file); 3719f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3720f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 37211ab86aedSSage Weil int ret; 3722f46b5a66SChristoph Hellwig 37231ab86aedSSage Weil ret = -EPERM; 3724df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3725f46b5a66SChristoph Hellwig goto out; 37261ab86aedSSage Weil 37271ab86aedSSage Weil ret = -EINPROGRESS; 37281ab86aedSSage Weil if (file->private_data) 37291ab86aedSSage Weil goto out; 37309ca9ee09SSage Weil 3731b83cc969SLi Zefan ret = -EROFS; 3732b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3733b83cc969SLi Zefan goto out; 3734b83cc969SLi Zefan 3735a561be71SAl Viro ret = mnt_want_write_file(file); 3736c146afadSYan Zheng if (ret) 3737c146afadSYan Zheng goto out; 3738c146afadSYan Zheng 3739a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 37409ca9ee09SSage Weil 3741f46b5a66SChristoph Hellwig ret = -ENOMEM; 37427a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3743abd30bb0STsutomu Itoh if (IS_ERR(trans)) 37441ab86aedSSage Weil goto out_drop; 37451ab86aedSSage Weil 37461ab86aedSSage Weil file->private_data = trans; 37471ab86aedSSage Weil return 0; 37481ab86aedSSage Weil 37491ab86aedSSage Weil out_drop: 3750a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 37512a79f17eSAl Viro mnt_drop_write_file(file); 3752f46b5a66SChristoph Hellwig out: 3753f46b5a66SChristoph Hellwig return ret; 3754f46b5a66SChristoph Hellwig } 3755f46b5a66SChristoph Hellwig 37566ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 37576ef5ed0dSJosef Bacik { 3758496ad9aaSAl Viro struct inode *inode = file_inode(file); 37596ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 37606ef5ed0dSJosef Bacik struct btrfs_root *new_root; 37616ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 37626ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 37636ef5ed0dSJosef Bacik struct btrfs_path *path; 37646ef5ed0dSJosef Bacik struct btrfs_key location; 37656ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 37666ef5ed0dSJosef Bacik u64 objectid = 0; 37676ef5ed0dSJosef Bacik u64 dir_id; 37683c04ce01SMiao Xie int ret; 37696ef5ed0dSJosef Bacik 37706ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 37716ef5ed0dSJosef Bacik return -EPERM; 37726ef5ed0dSJosef Bacik 37733c04ce01SMiao Xie ret = mnt_want_write_file(file); 37743c04ce01SMiao Xie if (ret) 37753c04ce01SMiao Xie return ret; 37763c04ce01SMiao Xie 37773c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 37783c04ce01SMiao Xie ret = -EFAULT; 37793c04ce01SMiao Xie goto out; 37803c04ce01SMiao Xie } 37816ef5ed0dSJosef Bacik 37826ef5ed0dSJosef Bacik if (!objectid) 37831cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 37846ef5ed0dSJosef Bacik 37856ef5ed0dSJosef Bacik location.objectid = objectid; 37866ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 37876ef5ed0dSJosef Bacik location.offset = (u64)-1; 37886ef5ed0dSJosef Bacik 37896ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 37903c04ce01SMiao Xie if (IS_ERR(new_root)) { 37913c04ce01SMiao Xie ret = PTR_ERR(new_root); 37923c04ce01SMiao Xie goto out; 37933c04ce01SMiao Xie } 37946ef5ed0dSJosef Bacik 37956ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 37963c04ce01SMiao Xie if (!path) { 37973c04ce01SMiao Xie ret = -ENOMEM; 37983c04ce01SMiao Xie goto out; 37993c04ce01SMiao Xie } 38006ef5ed0dSJosef Bacik path->leave_spinning = 1; 38016ef5ed0dSJosef Bacik 38026ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 380398d5dc13STsutomu Itoh if (IS_ERR(trans)) { 38046ef5ed0dSJosef Bacik btrfs_free_path(path); 38053c04ce01SMiao Xie ret = PTR_ERR(trans); 38063c04ce01SMiao Xie goto out; 38076ef5ed0dSJosef Bacik } 38086ef5ed0dSJosef Bacik 38096c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 38106ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 38116ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3812cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 38136ef5ed0dSJosef Bacik btrfs_free_path(path); 38146ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 3815efe120a0SFrank Holton btrfs_err(new_root->fs_info, "Umm, you don't have the default dir" 3816efe120a0SFrank Holton "item, this isn't going to work"); 38173c04ce01SMiao Xie ret = -ENOENT; 38183c04ce01SMiao Xie goto out; 38196ef5ed0dSJosef Bacik } 38206ef5ed0dSJosef Bacik 38216ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 38226ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 38236ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 38246ef5ed0dSJosef Bacik btrfs_free_path(path); 38256ef5ed0dSJosef Bacik 38262b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 38276ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 38283c04ce01SMiao Xie out: 38293c04ce01SMiao Xie mnt_drop_write_file(file); 38303c04ce01SMiao Xie return ret; 38316ef5ed0dSJosef Bacik } 38326ef5ed0dSJosef Bacik 38335af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 3834bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3835bf5fc093SJosef Bacik { 3836bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 3837bf5fc093SJosef Bacik 3838bf5fc093SJosef Bacik space->total_bytes = 0; 3839bf5fc093SJosef Bacik space->used_bytes = 0; 3840bf5fc093SJosef Bacik space->flags = 0; 3841bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3842bf5fc093SJosef Bacik space->flags = block_group->flags; 3843bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 3844bf5fc093SJosef Bacik space->used_bytes += 3845bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 3846bf5fc093SJosef Bacik } 3847bf5fc093SJosef Bacik } 3848bf5fc093SJosef Bacik 384948a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 38501406e432SJosef Bacik { 38511406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 38521406e432SJosef Bacik struct btrfs_ioctl_space_info space; 38531406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 38547fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 385513f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 38561406e432SJosef Bacik struct btrfs_space_info *info; 3857bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 3858bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3859bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3860bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 3861bf5fc093SJosef Bacik int num_types = 4; 38627fde62bfSChris Mason int alloc_size; 38631406e432SJosef Bacik int ret = 0; 386451788b1bSDan Rosenberg u64 slot_count = 0; 3865bf5fc093SJosef Bacik int i, c; 38661406e432SJosef Bacik 38671406e432SJosef Bacik if (copy_from_user(&space_args, 38681406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 38691406e432SJosef Bacik sizeof(space_args))) 38701406e432SJosef Bacik return -EFAULT; 38711406e432SJosef Bacik 3872bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3873bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3874bf5fc093SJosef Bacik 3875bf5fc093SJosef Bacik info = NULL; 38767fde62bfSChris Mason rcu_read_lock(); 3877bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3878bf5fc093SJosef Bacik list) { 3879bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3880bf5fc093SJosef Bacik info = tmp; 3881bf5fc093SJosef Bacik break; 3882bf5fc093SJosef Bacik } 3883bf5fc093SJosef Bacik } 38847fde62bfSChris Mason rcu_read_unlock(); 38851406e432SJosef Bacik 3886bf5fc093SJosef Bacik if (!info) 3887bf5fc093SJosef Bacik continue; 3888bf5fc093SJosef Bacik 3889bf5fc093SJosef Bacik down_read(&info->groups_sem); 3890bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3891bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3892bf5fc093SJosef Bacik slot_count++; 3893bf5fc093SJosef Bacik } 3894bf5fc093SJosef Bacik up_read(&info->groups_sem); 3895bf5fc093SJosef Bacik } 3896bf5fc093SJosef Bacik 389736523e95SDavid Sterba /* 389836523e95SDavid Sterba * Global block reserve, exported as a space_info 389936523e95SDavid Sterba */ 390036523e95SDavid Sterba slot_count++; 390136523e95SDavid Sterba 39027fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 39037fde62bfSChris Mason if (space_args.space_slots == 0) { 39047fde62bfSChris Mason space_args.total_spaces = slot_count; 39057fde62bfSChris Mason goto out; 39067fde62bfSChris Mason } 3907bf5fc093SJosef Bacik 390851788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3909bf5fc093SJosef Bacik 39107fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3911bf5fc093SJosef Bacik 39127fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 39137fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 39147fde62bfSChris Mason */ 39157fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 39167fde62bfSChris Mason return -ENOMEM; 39177fde62bfSChris Mason 39187fde62bfSChris Mason space_args.total_spaces = 0; 39197fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 39207fde62bfSChris Mason if (!dest) 39217fde62bfSChris Mason return -ENOMEM; 39227fde62bfSChris Mason dest_orig = dest; 39237fde62bfSChris Mason 39247fde62bfSChris Mason /* now we have a buffer to copy into */ 3925bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3926bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3927bf5fc093SJosef Bacik 392851788b1bSDan Rosenberg if (!slot_count) 392951788b1bSDan Rosenberg break; 393051788b1bSDan Rosenberg 3931bf5fc093SJosef Bacik info = NULL; 39321406e432SJosef Bacik rcu_read_lock(); 3933bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3934bf5fc093SJosef Bacik list) { 3935bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3936bf5fc093SJosef Bacik info = tmp; 39377fde62bfSChris Mason break; 3938bf5fc093SJosef Bacik } 3939bf5fc093SJosef Bacik } 3940bf5fc093SJosef Bacik rcu_read_unlock(); 39417fde62bfSChris Mason 3942bf5fc093SJosef Bacik if (!info) 3943bf5fc093SJosef Bacik continue; 3944bf5fc093SJosef Bacik down_read(&info->groups_sem); 3945bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3946bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 39475af3e8ccSStefan Behrens btrfs_get_block_group_info( 39485af3e8ccSStefan Behrens &info->block_groups[c], &space); 39497fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 39501406e432SJosef Bacik dest++; 39511406e432SJosef Bacik space_args.total_spaces++; 395251788b1bSDan Rosenberg slot_count--; 39531406e432SJosef Bacik } 395451788b1bSDan Rosenberg if (!slot_count) 395551788b1bSDan Rosenberg break; 3956bf5fc093SJosef Bacik } 3957bf5fc093SJosef Bacik up_read(&info->groups_sem); 3958bf5fc093SJosef Bacik } 39591406e432SJosef Bacik 396036523e95SDavid Sterba /* 396136523e95SDavid Sterba * Add global block reserve 396236523e95SDavid Sterba */ 396336523e95SDavid Sterba if (slot_count) { 396436523e95SDavid Sterba struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 396536523e95SDavid Sterba 396636523e95SDavid Sterba spin_lock(&block_rsv->lock); 396736523e95SDavid Sterba space.total_bytes = block_rsv->size; 396836523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 396936523e95SDavid Sterba spin_unlock(&block_rsv->lock); 397036523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 397136523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 397236523e95SDavid Sterba space_args.total_spaces++; 397336523e95SDavid Sterba } 397436523e95SDavid Sterba 39752eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 39767fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 39777fde62bfSChris Mason 39787fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 39797fde62bfSChris Mason ret = -EFAULT; 39807fde62bfSChris Mason 39817fde62bfSChris Mason kfree(dest_orig); 39827fde62bfSChris Mason out: 39837fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 39841406e432SJosef Bacik ret = -EFAULT; 39851406e432SJosef Bacik 39861406e432SJosef Bacik return ret; 39871406e432SJosef Bacik } 39881406e432SJosef Bacik 3989f46b5a66SChristoph Hellwig /* 3990f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3991f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3992f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3993f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3994f46b5a66SChristoph Hellwig */ 3995f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3996f46b5a66SChristoph Hellwig { 3997496ad9aaSAl Viro struct inode *inode = file_inode(file); 3998f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3999f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 4000f46b5a66SChristoph Hellwig 4001f46b5a66SChristoph Hellwig trans = file->private_data; 40021ab86aedSSage Weil if (!trans) 40031ab86aedSSage Weil return -EINVAL; 4004b214107eSChristoph Hellwig file->private_data = NULL; 40059ca9ee09SSage Weil 40061ab86aedSSage Weil btrfs_end_transaction(trans, root); 40071ab86aedSSage Weil 4008a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 40099ca9ee09SSage Weil 40102a79f17eSAl Viro mnt_drop_write_file(file); 40111ab86aedSSage Weil return 0; 4012f46b5a66SChristoph Hellwig } 4013f46b5a66SChristoph Hellwig 40149a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 40159a8c28beSMiao Xie void __user *argp) 401646204592SSage Weil { 401746204592SSage Weil struct btrfs_trans_handle *trans; 401846204592SSage Weil u64 transid; 4019db5b493aSTsutomu Itoh int ret; 402046204592SSage Weil 4021d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4022ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4023ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 402498d5dc13STsutomu Itoh return PTR_ERR(trans); 4025ff7c1d33SMiao Xie 4026ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4027ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4028ff7c1d33SMiao Xie goto out; 4029ff7c1d33SMiao Xie } 403046204592SSage Weil transid = trans->transid; 4031db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 40328b2b2d3cSTsutomu Itoh if (ret) { 40338b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 4034db5b493aSTsutomu Itoh return ret; 40358b2b2d3cSTsutomu Itoh } 4036ff7c1d33SMiao Xie out: 403746204592SSage Weil if (argp) 403846204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 403946204592SSage Weil return -EFAULT; 404046204592SSage Weil return 0; 404146204592SSage Weil } 404246204592SSage Weil 40439a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 40449a8c28beSMiao Xie void __user *argp) 404546204592SSage Weil { 404646204592SSage Weil u64 transid; 404746204592SSage Weil 404846204592SSage Weil if (argp) { 404946204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 405046204592SSage Weil return -EFAULT; 405146204592SSage Weil } else { 405246204592SSage Weil transid = 0; /* current trans */ 405346204592SSage Weil } 405446204592SSage Weil return btrfs_wait_for_commit(root, transid); 405546204592SSage Weil } 405646204592SSage Weil 4057b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4058475f6387SJan Schmidt { 4059496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4060475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4061b8e95489SMiao Xie int ret; 4062475f6387SJan Schmidt 4063475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4064475f6387SJan Schmidt return -EPERM; 4065475f6387SJan Schmidt 4066475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4067475f6387SJan Schmidt if (IS_ERR(sa)) 4068475f6387SJan Schmidt return PTR_ERR(sa); 4069475f6387SJan Schmidt 4070b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4071b8e95489SMiao Xie ret = mnt_want_write_file(file); 4072b8e95489SMiao Xie if (ret) 4073b8e95489SMiao Xie goto out; 4074b8e95489SMiao Xie } 4075b8e95489SMiao Xie 4076aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 407763a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 407863a212abSStefan Behrens 0); 4079475f6387SJan Schmidt 4080475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4081475f6387SJan Schmidt ret = -EFAULT; 4082475f6387SJan Schmidt 4083b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4084b8e95489SMiao Xie mnt_drop_write_file(file); 4085b8e95489SMiao Xie out: 4086475f6387SJan Schmidt kfree(sa); 4087475f6387SJan Schmidt return ret; 4088475f6387SJan Schmidt } 4089475f6387SJan Schmidt 4090475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 4091475f6387SJan Schmidt { 4092475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4093475f6387SJan Schmidt return -EPERM; 4094475f6387SJan Schmidt 4095aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 4096475f6387SJan Schmidt } 4097475f6387SJan Schmidt 4098475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 4099475f6387SJan Schmidt void __user *arg) 4100475f6387SJan Schmidt { 4101475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4102475f6387SJan Schmidt int ret; 4103475f6387SJan Schmidt 4104475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4105475f6387SJan Schmidt return -EPERM; 4106475f6387SJan Schmidt 4107475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4108475f6387SJan Schmidt if (IS_ERR(sa)) 4109475f6387SJan Schmidt return PTR_ERR(sa); 4110475f6387SJan Schmidt 4111475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 4112475f6387SJan Schmidt 4113475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4114475f6387SJan Schmidt ret = -EFAULT; 4115475f6387SJan Schmidt 4116475f6387SJan Schmidt kfree(sa); 4117475f6387SJan Schmidt return ret; 4118475f6387SJan Schmidt } 4119475f6387SJan Schmidt 4120c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 4121b27f7c0cSDavid Sterba void __user *arg) 4122c11d2c23SStefan Behrens { 4123c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4124c11d2c23SStefan Behrens int ret; 4125c11d2c23SStefan Behrens 4126c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4127c11d2c23SStefan Behrens if (IS_ERR(sa)) 4128c11d2c23SStefan Behrens return PTR_ERR(sa); 4129c11d2c23SStefan Behrens 4130b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4131b27f7c0cSDavid Sterba kfree(sa); 4132b27f7c0cSDavid Sterba return -EPERM; 4133b27f7c0cSDavid Sterba } 4134b27f7c0cSDavid Sterba 4135b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 4136c11d2c23SStefan Behrens 4137c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4138c11d2c23SStefan Behrens ret = -EFAULT; 4139c11d2c23SStefan Behrens 4140c11d2c23SStefan Behrens kfree(sa); 4141c11d2c23SStefan Behrens return ret; 4142c11d2c23SStefan Behrens } 4143c11d2c23SStefan Behrens 41443f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 41453f6bcfbdSStefan Behrens { 41463f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 41473f6bcfbdSStefan Behrens int ret; 41483f6bcfbdSStefan Behrens 41493f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 41503f6bcfbdSStefan Behrens return -EPERM; 41513f6bcfbdSStefan Behrens 41523f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 41533f6bcfbdSStefan Behrens if (IS_ERR(p)) 41543f6bcfbdSStefan Behrens return PTR_ERR(p); 41553f6bcfbdSStefan Behrens 41563f6bcfbdSStefan Behrens switch (p->cmd) { 41573f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4158adfa97cbSIlya Dryomov if (root->fs_info->sb->s_flags & MS_RDONLY) { 4159adfa97cbSIlya Dryomov ret = -EROFS; 4160adfa97cbSIlya Dryomov goto out; 4161adfa97cbSIlya Dryomov } 41623f6bcfbdSStefan Behrens if (atomic_xchg( 41633f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41643f6bcfbdSStefan Behrens 1)) { 4165e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 41663f6bcfbdSStefan Behrens } else { 41673f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 41683f6bcfbdSStefan Behrens atomic_set( 41693f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41703f6bcfbdSStefan Behrens 0); 41713f6bcfbdSStefan Behrens } 41723f6bcfbdSStefan Behrens break; 41733f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 41743f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 41753f6bcfbdSStefan Behrens ret = 0; 41763f6bcfbdSStefan Behrens break; 41773f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 41783f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 41793f6bcfbdSStefan Behrens break; 41803f6bcfbdSStefan Behrens default: 41813f6bcfbdSStefan Behrens ret = -EINVAL; 41823f6bcfbdSStefan Behrens break; 41833f6bcfbdSStefan Behrens } 41843f6bcfbdSStefan Behrens 41853f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 41863f6bcfbdSStefan Behrens ret = -EFAULT; 4187adfa97cbSIlya Dryomov out: 41883f6bcfbdSStefan Behrens kfree(p); 41893f6bcfbdSStefan Behrens return ret; 41903f6bcfbdSStefan Behrens } 41913f6bcfbdSStefan Behrens 4192d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4193d7728c96SJan Schmidt { 4194d7728c96SJan Schmidt int ret = 0; 4195d7728c96SJan Schmidt int i; 4196740c3d22SChris Mason u64 rel_ptr; 4197d7728c96SJan Schmidt int size; 4198806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4199d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4200d7728c96SJan Schmidt struct btrfs_path *path; 4201d7728c96SJan Schmidt 420282b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4203d7728c96SJan Schmidt return -EPERM; 4204d7728c96SJan Schmidt 4205d7728c96SJan Schmidt path = btrfs_alloc_path(); 4206d7728c96SJan Schmidt if (!path) { 4207d7728c96SJan Schmidt ret = -ENOMEM; 4208d7728c96SJan Schmidt goto out; 4209d7728c96SJan Schmidt } 4210d7728c96SJan Schmidt 4211d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4212d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4213d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4214d7728c96SJan Schmidt ipa = NULL; 4215d7728c96SJan Schmidt goto out; 4216d7728c96SJan Schmidt } 4217d7728c96SJan Schmidt 4218d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4219d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4220d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4221d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4222d7728c96SJan Schmidt ipath = NULL; 4223d7728c96SJan Schmidt goto out; 4224d7728c96SJan Schmidt } 4225d7728c96SJan Schmidt 4226d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4227d7728c96SJan Schmidt if (ret < 0) 4228d7728c96SJan Schmidt goto out; 4229d7728c96SJan Schmidt 4230d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4231745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4232745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4233740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4234d7728c96SJan Schmidt } 4235d7728c96SJan Schmidt 4236745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 4237745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 4238d7728c96SJan Schmidt if (ret) { 4239d7728c96SJan Schmidt ret = -EFAULT; 4240d7728c96SJan Schmidt goto out; 4241d7728c96SJan Schmidt } 4242d7728c96SJan Schmidt 4243d7728c96SJan Schmidt out: 4244d7728c96SJan Schmidt btrfs_free_path(path); 4245d7728c96SJan Schmidt free_ipath(ipath); 4246d7728c96SJan Schmidt kfree(ipa); 4247d7728c96SJan Schmidt 4248d7728c96SJan Schmidt return ret; 4249d7728c96SJan Schmidt } 4250d7728c96SJan Schmidt 4251d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4252d7728c96SJan Schmidt { 4253d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4254d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4255d7728c96SJan Schmidt 4256d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4257d7728c96SJan Schmidt inodes->bytes_left -= c; 4258d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4259d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4260d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4261d7728c96SJan Schmidt inodes->elem_cnt += 3; 4262d7728c96SJan Schmidt } else { 4263d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4264d7728c96SJan Schmidt inodes->bytes_left = 0; 4265d7728c96SJan Schmidt inodes->elem_missed += 3; 4266d7728c96SJan Schmidt } 4267d7728c96SJan Schmidt 4268d7728c96SJan Schmidt return 0; 4269d7728c96SJan Schmidt } 4270d7728c96SJan Schmidt 4271d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 4272d7728c96SJan Schmidt void __user *arg) 4273d7728c96SJan Schmidt { 4274d7728c96SJan Schmidt int ret = 0; 4275d7728c96SJan Schmidt int size; 4276d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4277d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4278d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4279d7728c96SJan Schmidt 4280d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4281d7728c96SJan Schmidt return -EPERM; 4282d7728c96SJan Schmidt 4283d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 4284d7728c96SJan Schmidt if (IS_ERR(loi)) { 4285d7728c96SJan Schmidt ret = PTR_ERR(loi); 4286d7728c96SJan Schmidt loi = NULL; 4287d7728c96SJan Schmidt goto out; 4288d7728c96SJan Schmidt } 4289d7728c96SJan Schmidt 4290d7728c96SJan Schmidt path = btrfs_alloc_path(); 4291d7728c96SJan Schmidt if (!path) { 4292d7728c96SJan Schmidt ret = -ENOMEM; 4293d7728c96SJan Schmidt goto out; 4294d7728c96SJan Schmidt } 4295d7728c96SJan Schmidt 4296425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 4297d7728c96SJan Schmidt inodes = init_data_container(size); 4298d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4299d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4300d7728c96SJan Schmidt inodes = NULL; 4301d7728c96SJan Schmidt goto out; 4302d7728c96SJan Schmidt } 4303d7728c96SJan Schmidt 4304df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 4305df031f07SLiu Bo build_ino_list, inodes); 4306df031f07SLiu Bo if (ret == -EINVAL) 4307d7728c96SJan Schmidt ret = -ENOENT; 4308d7728c96SJan Schmidt if (ret < 0) 4309d7728c96SJan Schmidt goto out; 4310d7728c96SJan Schmidt 4311745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 4312745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 4313d7728c96SJan Schmidt if (ret) 4314d7728c96SJan Schmidt ret = -EFAULT; 4315d7728c96SJan Schmidt 4316d7728c96SJan Schmidt out: 4317d7728c96SJan Schmidt btrfs_free_path(path); 4318425d17a2SLiu Bo vfree(inodes); 4319d7728c96SJan Schmidt kfree(loi); 4320d7728c96SJan Schmidt 4321d7728c96SJan Schmidt return ret; 4322d7728c96SJan Schmidt } 4323d7728c96SJan Schmidt 432419a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4325c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4326c9e9f97bSIlya Dryomov { 4327c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4328c9e9f97bSIlya Dryomov 4329c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4330c9e9f97bSIlya Dryomov 4331837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4332837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4333837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4334837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4335a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4336a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4337837d5b6eSIlya Dryomov 4338c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4339c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4340c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 434119a39dceSIlya Dryomov 434219a39dceSIlya Dryomov if (lock) { 434319a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 434419a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 434519a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 434619a39dceSIlya Dryomov } else { 434719a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 434819a39dceSIlya Dryomov } 4349c9e9f97bSIlya Dryomov } 4350c9e9f97bSIlya Dryomov 43519ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4352c9e9f97bSIlya Dryomov { 4353496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4354c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4355c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4356c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4357ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4358c9e9f97bSIlya Dryomov int ret; 4359c9e9f97bSIlya Dryomov 4360c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4361c9e9f97bSIlya Dryomov return -EPERM; 4362c9e9f97bSIlya Dryomov 4363e54bfa31SLiu Bo ret = mnt_want_write_file(file); 43649ba1f6e4SLiu Bo if (ret) 43659ba1f6e4SLiu Bo return ret; 43669ba1f6e4SLiu Bo 4367ed0fb78fSIlya Dryomov again: 4368ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 4369c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4370c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4371ed0fb78fSIlya Dryomov need_unlock = true; 4372ed0fb78fSIlya Dryomov goto locked; 4373ed0fb78fSIlya Dryomov } 4374ed0fb78fSIlya Dryomov 4375ed0fb78fSIlya Dryomov /* 4376ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 4377ed0fb78fSIlya Dryomov * (1) some other op is running 4378ed0fb78fSIlya Dryomov * (2) balance is running 4379ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4380ed0fb78fSIlya Dryomov */ 4381ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4382ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4383ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4384ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4385ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4386ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4387ed0fb78fSIlya Dryomov goto again; 4388ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4389ed0fb78fSIlya Dryomov 4390ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4391ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4392ed0fb78fSIlya Dryomov /* this is (3) */ 4393ed0fb78fSIlya Dryomov need_unlock = false; 4394ed0fb78fSIlya Dryomov goto locked; 4395ed0fb78fSIlya Dryomov } 4396ed0fb78fSIlya Dryomov 4397ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4398ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4399ed0fb78fSIlya Dryomov goto again; 4400ed0fb78fSIlya Dryomov } else { 4401ed0fb78fSIlya Dryomov /* this is (2) */ 4402ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4403ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4404ed0fb78fSIlya Dryomov goto out; 4405ed0fb78fSIlya Dryomov } 4406ed0fb78fSIlya Dryomov } else { 4407ed0fb78fSIlya Dryomov /* this is (1) */ 4408ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4409e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4410ed0fb78fSIlya Dryomov goto out; 4411ed0fb78fSIlya Dryomov } 4412ed0fb78fSIlya Dryomov 4413ed0fb78fSIlya Dryomov locked: 4414ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 4415c9e9f97bSIlya Dryomov 4416c9e9f97bSIlya Dryomov if (arg) { 4417c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4418c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4419c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4420ed0fb78fSIlya Dryomov goto out_unlock; 4421c9e9f97bSIlya Dryomov } 4422de322263SIlya Dryomov 4423de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4424de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4425de322263SIlya Dryomov ret = -ENOTCONN; 4426de322263SIlya Dryomov goto out_bargs; 4427de322263SIlya Dryomov } 4428de322263SIlya Dryomov 4429de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4430de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4431de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4432de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4433de322263SIlya Dryomov 4434de322263SIlya Dryomov goto do_balance; 4435de322263SIlya Dryomov } 4436c9e9f97bSIlya Dryomov } else { 4437c9e9f97bSIlya Dryomov bargs = NULL; 4438c9e9f97bSIlya Dryomov } 4439c9e9f97bSIlya Dryomov 4440ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4441837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4442837d5b6eSIlya Dryomov goto out_bargs; 4443837d5b6eSIlya Dryomov } 4444837d5b6eSIlya Dryomov 4445c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 4446c9e9f97bSIlya Dryomov if (!bctl) { 4447c9e9f97bSIlya Dryomov ret = -ENOMEM; 4448c9e9f97bSIlya Dryomov goto out_bargs; 4449c9e9f97bSIlya Dryomov } 4450c9e9f97bSIlya Dryomov 4451c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4452c9e9f97bSIlya Dryomov if (arg) { 4453c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4454c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4455c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4456c9e9f97bSIlya Dryomov 4457c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4458f43ffb60SIlya Dryomov } else { 4459f43ffb60SIlya Dryomov /* balance everything - no filters */ 4460f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4461c9e9f97bSIlya Dryomov } 4462c9e9f97bSIlya Dryomov 4463de322263SIlya Dryomov do_balance: 4464c9e9f97bSIlya Dryomov /* 4465ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 4466ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4467ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4468ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 4469ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 4470c9e9f97bSIlya Dryomov */ 4471ed0fb78fSIlya Dryomov need_unlock = false; 4472ed0fb78fSIlya Dryomov 4473ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 4474ed0fb78fSIlya Dryomov 4475c9e9f97bSIlya Dryomov if (arg) { 4476c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4477c9e9f97bSIlya Dryomov ret = -EFAULT; 4478c9e9f97bSIlya Dryomov } 4479c9e9f97bSIlya Dryomov 4480c9e9f97bSIlya Dryomov out_bargs: 4481c9e9f97bSIlya Dryomov kfree(bargs); 4482ed0fb78fSIlya Dryomov out_unlock: 4483c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4484c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4485ed0fb78fSIlya Dryomov if (need_unlock) 4486ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 4487ed0fb78fSIlya Dryomov out: 4488e54bfa31SLiu Bo mnt_drop_write_file(file); 4489c9e9f97bSIlya Dryomov return ret; 4490c9e9f97bSIlya Dryomov } 4491c9e9f97bSIlya Dryomov 4492837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4493837d5b6eSIlya Dryomov { 4494837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4495837d5b6eSIlya Dryomov return -EPERM; 4496837d5b6eSIlya Dryomov 4497837d5b6eSIlya Dryomov switch (cmd) { 4498837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 4499837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 4500a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 4501a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 4502837d5b6eSIlya Dryomov } 4503837d5b6eSIlya Dryomov 4504837d5b6eSIlya Dryomov return -EINVAL; 4505837d5b6eSIlya Dryomov } 4506837d5b6eSIlya Dryomov 450719a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 450819a39dceSIlya Dryomov void __user *arg) 450919a39dceSIlya Dryomov { 451019a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 451119a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 451219a39dceSIlya Dryomov int ret = 0; 451319a39dceSIlya Dryomov 451419a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 451519a39dceSIlya Dryomov return -EPERM; 451619a39dceSIlya Dryomov 451719a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 451819a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 451919a39dceSIlya Dryomov ret = -ENOTCONN; 452019a39dceSIlya Dryomov goto out; 452119a39dceSIlya Dryomov } 452219a39dceSIlya Dryomov 452319a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 452419a39dceSIlya Dryomov if (!bargs) { 452519a39dceSIlya Dryomov ret = -ENOMEM; 452619a39dceSIlya Dryomov goto out; 452719a39dceSIlya Dryomov } 452819a39dceSIlya Dryomov 452919a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 453019a39dceSIlya Dryomov 453119a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 453219a39dceSIlya Dryomov ret = -EFAULT; 453319a39dceSIlya Dryomov 453419a39dceSIlya Dryomov kfree(bargs); 453519a39dceSIlya Dryomov out: 453619a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 453719a39dceSIlya Dryomov return ret; 453819a39dceSIlya Dryomov } 453919a39dceSIlya Dryomov 4540905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 45415d13a37bSArne Jansen { 4542496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45435d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 45445d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 45455d13a37bSArne Jansen int ret; 45465d13a37bSArne Jansen int err; 45475d13a37bSArne Jansen 45485d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45495d13a37bSArne Jansen return -EPERM; 45505d13a37bSArne Jansen 4551905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4552905b0ddaSMiao Xie if (ret) 4553905b0ddaSMiao Xie return ret; 45545d13a37bSArne Jansen 45555d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4556905b0ddaSMiao Xie if (IS_ERR(sa)) { 4557905b0ddaSMiao Xie ret = PTR_ERR(sa); 4558905b0ddaSMiao Xie goto drop_write; 4559905b0ddaSMiao Xie } 45605d13a37bSArne Jansen 45617708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 456292f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 45635d13a37bSArne Jansen if (IS_ERR(trans)) { 45645d13a37bSArne Jansen ret = PTR_ERR(trans); 45655d13a37bSArne Jansen goto out; 45665d13a37bSArne Jansen } 45675d13a37bSArne Jansen 45685d13a37bSArne Jansen switch (sa->cmd) { 45695d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 45705d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 45715d13a37bSArne Jansen break; 45725d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 45735d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 45745d13a37bSArne Jansen break; 45755d13a37bSArne Jansen default: 45765d13a37bSArne Jansen ret = -EINVAL; 45775d13a37bSArne Jansen break; 45785d13a37bSArne Jansen } 45795d13a37bSArne Jansen 458092f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 45815d13a37bSArne Jansen if (err && !ret) 45825d13a37bSArne Jansen ret = err; 45835d13a37bSArne Jansen out: 45845d13a37bSArne Jansen kfree(sa); 45857708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 4586905b0ddaSMiao Xie drop_write: 4587905b0ddaSMiao Xie mnt_drop_write_file(file); 45885d13a37bSArne Jansen return ret; 45895d13a37bSArne Jansen } 45905d13a37bSArne Jansen 4591905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 45925d13a37bSArne Jansen { 4593496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45945d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 45955d13a37bSArne Jansen struct btrfs_trans_handle *trans; 45965d13a37bSArne Jansen int ret; 45975d13a37bSArne Jansen int err; 45985d13a37bSArne Jansen 45995d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46005d13a37bSArne Jansen return -EPERM; 46015d13a37bSArne Jansen 4602905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4603905b0ddaSMiao Xie if (ret) 4604905b0ddaSMiao Xie return ret; 46055d13a37bSArne Jansen 46065d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4607905b0ddaSMiao Xie if (IS_ERR(sa)) { 4608905b0ddaSMiao Xie ret = PTR_ERR(sa); 4609905b0ddaSMiao Xie goto drop_write; 4610905b0ddaSMiao Xie } 46115d13a37bSArne Jansen 46125d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46135d13a37bSArne Jansen if (IS_ERR(trans)) { 46145d13a37bSArne Jansen ret = PTR_ERR(trans); 46155d13a37bSArne Jansen goto out; 46165d13a37bSArne Jansen } 46175d13a37bSArne Jansen 46185d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 46195d13a37bSArne Jansen if (sa->assign) { 46205d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 46215d13a37bSArne Jansen sa->src, sa->dst); 46225d13a37bSArne Jansen } else { 46235d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 46245d13a37bSArne Jansen sa->src, sa->dst); 46255d13a37bSArne Jansen } 46265d13a37bSArne Jansen 46275d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46285d13a37bSArne Jansen if (err && !ret) 46295d13a37bSArne Jansen ret = err; 46305d13a37bSArne Jansen 46315d13a37bSArne Jansen out: 46325d13a37bSArne Jansen kfree(sa); 4633905b0ddaSMiao Xie drop_write: 4634905b0ddaSMiao Xie mnt_drop_write_file(file); 46355d13a37bSArne Jansen return ret; 46365d13a37bSArne Jansen } 46375d13a37bSArne Jansen 4638905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 46395d13a37bSArne Jansen { 4640496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46415d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 46425d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46435d13a37bSArne Jansen int ret; 46445d13a37bSArne Jansen int err; 46455d13a37bSArne Jansen 46465d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46475d13a37bSArne Jansen return -EPERM; 46485d13a37bSArne Jansen 4649905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4650905b0ddaSMiao Xie if (ret) 4651905b0ddaSMiao Xie return ret; 46525d13a37bSArne Jansen 46535d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4654905b0ddaSMiao Xie if (IS_ERR(sa)) { 4655905b0ddaSMiao Xie ret = PTR_ERR(sa); 4656905b0ddaSMiao Xie goto drop_write; 4657905b0ddaSMiao Xie } 46585d13a37bSArne Jansen 4659d86e56cfSMiao Xie if (!sa->qgroupid) { 4660d86e56cfSMiao Xie ret = -EINVAL; 4661d86e56cfSMiao Xie goto out; 4662d86e56cfSMiao Xie } 4663d86e56cfSMiao Xie 46645d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46655d13a37bSArne Jansen if (IS_ERR(trans)) { 46665d13a37bSArne Jansen ret = PTR_ERR(trans); 46675d13a37bSArne Jansen goto out; 46685d13a37bSArne Jansen } 46695d13a37bSArne Jansen 46705d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 46715d13a37bSArne Jansen if (sa->create) { 46725d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 46735d13a37bSArne Jansen NULL); 46745d13a37bSArne Jansen } else { 46755d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 46765d13a37bSArne Jansen } 46775d13a37bSArne Jansen 46785d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46795d13a37bSArne Jansen if (err && !ret) 46805d13a37bSArne Jansen ret = err; 46815d13a37bSArne Jansen 46825d13a37bSArne Jansen out: 46835d13a37bSArne Jansen kfree(sa); 4684905b0ddaSMiao Xie drop_write: 4685905b0ddaSMiao Xie mnt_drop_write_file(file); 46865d13a37bSArne Jansen return ret; 46875d13a37bSArne Jansen } 46885d13a37bSArne Jansen 4689905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 46905d13a37bSArne Jansen { 4691496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46925d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 46935d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46945d13a37bSArne Jansen int ret; 46955d13a37bSArne Jansen int err; 46965d13a37bSArne Jansen u64 qgroupid; 46975d13a37bSArne Jansen 46985d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46995d13a37bSArne Jansen return -EPERM; 47005d13a37bSArne Jansen 4701905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4702905b0ddaSMiao Xie if (ret) 4703905b0ddaSMiao Xie return ret; 47045d13a37bSArne Jansen 47055d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4706905b0ddaSMiao Xie if (IS_ERR(sa)) { 4707905b0ddaSMiao Xie ret = PTR_ERR(sa); 4708905b0ddaSMiao Xie goto drop_write; 4709905b0ddaSMiao Xie } 47105d13a37bSArne Jansen 47115d13a37bSArne Jansen trans = btrfs_join_transaction(root); 47125d13a37bSArne Jansen if (IS_ERR(trans)) { 47135d13a37bSArne Jansen ret = PTR_ERR(trans); 47145d13a37bSArne Jansen goto out; 47155d13a37bSArne Jansen } 47165d13a37bSArne Jansen 47175d13a37bSArne Jansen qgroupid = sa->qgroupid; 47185d13a37bSArne Jansen if (!qgroupid) { 47195d13a37bSArne Jansen /* take the current subvol as qgroup */ 47205d13a37bSArne Jansen qgroupid = root->root_key.objectid; 47215d13a37bSArne Jansen } 47225d13a37bSArne Jansen 47235d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 47245d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 47255d13a37bSArne Jansen 47265d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 47275d13a37bSArne Jansen if (err && !ret) 47285d13a37bSArne Jansen ret = err; 47295d13a37bSArne Jansen 47305d13a37bSArne Jansen out: 47315d13a37bSArne Jansen kfree(sa); 4732905b0ddaSMiao Xie drop_write: 4733905b0ddaSMiao Xie mnt_drop_write_file(file); 47345d13a37bSArne Jansen return ret; 47355d13a37bSArne Jansen } 47365d13a37bSArne Jansen 47372f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 47382f232036SJan Schmidt { 47396d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47402f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47412f232036SJan Schmidt int ret; 47422f232036SJan Schmidt 47432f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47442f232036SJan Schmidt return -EPERM; 47452f232036SJan Schmidt 47462f232036SJan Schmidt ret = mnt_want_write_file(file); 47472f232036SJan Schmidt if (ret) 47482f232036SJan Schmidt return ret; 47492f232036SJan Schmidt 47502f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 47512f232036SJan Schmidt if (IS_ERR(qsa)) { 47522f232036SJan Schmidt ret = PTR_ERR(qsa); 47532f232036SJan Schmidt goto drop_write; 47542f232036SJan Schmidt } 47552f232036SJan Schmidt 47562f232036SJan Schmidt if (qsa->flags) { 47572f232036SJan Schmidt ret = -EINVAL; 47582f232036SJan Schmidt goto out; 47592f232036SJan Schmidt } 47602f232036SJan Schmidt 47612f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 47622f232036SJan Schmidt 47632f232036SJan Schmidt out: 47642f232036SJan Schmidt kfree(qsa); 47652f232036SJan Schmidt drop_write: 47662f232036SJan Schmidt mnt_drop_write_file(file); 47672f232036SJan Schmidt return ret; 47682f232036SJan Schmidt } 47692f232036SJan Schmidt 47702f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 47712f232036SJan Schmidt { 47726d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47732f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47742f232036SJan Schmidt int ret = 0; 47752f232036SJan Schmidt 47762f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47772f232036SJan Schmidt return -EPERM; 47782f232036SJan Schmidt 47792f232036SJan Schmidt qsa = kzalloc(sizeof(*qsa), GFP_NOFS); 47802f232036SJan Schmidt if (!qsa) 47812f232036SJan Schmidt return -ENOMEM; 47822f232036SJan Schmidt 47832f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 47842f232036SJan Schmidt qsa->flags = 1; 47852f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 47862f232036SJan Schmidt } 47872f232036SJan Schmidt 47882f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 47892f232036SJan Schmidt ret = -EFAULT; 47902f232036SJan Schmidt 47912f232036SJan Schmidt kfree(qsa); 47922f232036SJan Schmidt return ret; 47932f232036SJan Schmidt } 47942f232036SJan Schmidt 479557254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 479657254b6eSJan Schmidt { 479754563d41SAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 479857254b6eSJan Schmidt 479957254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 480057254b6eSJan Schmidt return -EPERM; 480157254b6eSJan Schmidt 480257254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 480357254b6eSJan Schmidt } 480457254b6eSJan Schmidt 4805abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4806abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 48078ea05e3aSAlexander Block { 4808496ad9aaSAl Viro struct inode *inode = file_inode(file); 48098ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 48108ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 48118ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 48128ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 48138ea05e3aSAlexander Block int ret = 0; 4814dd5f9615SStefan Behrens int received_uuid_changed; 48158ea05e3aSAlexander Block 4816bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 4817bd60ea0fSDavid Sterba return -EPERM; 4818bd60ea0fSDavid Sterba 48198ea05e3aSAlexander Block ret = mnt_want_write_file(file); 48208ea05e3aSAlexander Block if (ret < 0) 48218ea05e3aSAlexander Block return ret; 48228ea05e3aSAlexander Block 48238ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 48248ea05e3aSAlexander Block 48258ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 48268ea05e3aSAlexander Block ret = -EINVAL; 48278ea05e3aSAlexander Block goto out; 48288ea05e3aSAlexander Block } 48298ea05e3aSAlexander Block 48308ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 48318ea05e3aSAlexander Block ret = -EROFS; 48328ea05e3aSAlexander Block goto out; 48338ea05e3aSAlexander Block } 48348ea05e3aSAlexander Block 4835dd5f9615SStefan Behrens /* 4836dd5f9615SStefan Behrens * 1 - root item 4837dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4838dd5f9615SStefan Behrens */ 4839dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 48408ea05e3aSAlexander Block if (IS_ERR(trans)) { 48418ea05e3aSAlexander Block ret = PTR_ERR(trans); 48428ea05e3aSAlexander Block trans = NULL; 48438ea05e3aSAlexander Block goto out; 48448ea05e3aSAlexander Block } 48458ea05e3aSAlexander Block 48468ea05e3aSAlexander Block sa->rtransid = trans->transid; 48478ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 48488ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 48498ea05e3aSAlexander Block 4850dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4851dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4852dd5f9615SStefan Behrens if (received_uuid_changed && 4853dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 4854dd5f9615SStefan Behrens btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 4855dd5f9615SStefan Behrens root_item->received_uuid, 4856dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4857dd5f9615SStefan Behrens root->root_key.objectid); 48588ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 48598ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 48608ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 48613cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 48623cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 48633cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 48643cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 48658ea05e3aSAlexander Block 48668ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 48678ea05e3aSAlexander Block &root->root_key, &root->root_item); 48688ea05e3aSAlexander Block if (ret < 0) { 48698ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 48708ea05e3aSAlexander Block goto out; 4871dd5f9615SStefan Behrens } 4872dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4873dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 4874dd5f9615SStefan Behrens sa->uuid, 4875dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4876dd5f9615SStefan Behrens root->root_key.objectid); 4877dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 4878dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 4879dd5f9615SStefan Behrens goto out; 4880dd5f9615SStefan Behrens } 4881dd5f9615SStefan Behrens } 48828ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 4883dd5f9615SStefan Behrens if (ret < 0) { 4884dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 48858ea05e3aSAlexander Block goto out; 48868ea05e3aSAlexander Block } 48878ea05e3aSAlexander Block 4888abccd00fSHugo Mills out: 4889abccd00fSHugo Mills up_write(&root->fs_info->subvol_sem); 4890abccd00fSHugo Mills mnt_drop_write_file(file); 4891abccd00fSHugo Mills return ret; 4892abccd00fSHugo Mills } 4893abccd00fSHugo Mills 4894abccd00fSHugo Mills #ifdef CONFIG_64BIT 4895abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 4896abccd00fSHugo Mills void __user *arg) 4897abccd00fSHugo Mills { 4898abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 4899abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 4900abccd00fSHugo Mills int ret = 0; 4901abccd00fSHugo Mills 4902abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 4903abccd00fSHugo Mills if (IS_ERR(args32)) { 4904abccd00fSHugo Mills ret = PTR_ERR(args32); 4905abccd00fSHugo Mills args32 = NULL; 4906abccd00fSHugo Mills goto out; 4907abccd00fSHugo Mills } 4908abccd00fSHugo Mills 4909abccd00fSHugo Mills args64 = kmalloc(sizeof(*args64), GFP_NOFS); 491084dbeb87SDan Carpenter if (!args64) { 491184dbeb87SDan Carpenter ret = -ENOMEM; 4912abccd00fSHugo Mills goto out; 4913abccd00fSHugo Mills } 4914abccd00fSHugo Mills 4915abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 4916abccd00fSHugo Mills args64->stransid = args32->stransid; 4917abccd00fSHugo Mills args64->rtransid = args32->rtransid; 4918abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 4919abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 4920abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 4921abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 4922abccd00fSHugo Mills args64->flags = args32->flags; 4923abccd00fSHugo Mills 4924abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 4925abccd00fSHugo Mills if (ret) 4926abccd00fSHugo Mills goto out; 4927abccd00fSHugo Mills 4928abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 4929abccd00fSHugo Mills args32->stransid = args64->stransid; 4930abccd00fSHugo Mills args32->rtransid = args64->rtransid; 4931abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 4932abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 4933abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 4934abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 4935abccd00fSHugo Mills args32->flags = args64->flags; 4936abccd00fSHugo Mills 4937abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 4938abccd00fSHugo Mills if (ret) 4939abccd00fSHugo Mills ret = -EFAULT; 4940abccd00fSHugo Mills 4941abccd00fSHugo Mills out: 4942abccd00fSHugo Mills kfree(args32); 4943abccd00fSHugo Mills kfree(args64); 4944abccd00fSHugo Mills return ret; 4945abccd00fSHugo Mills } 4946abccd00fSHugo Mills #endif 4947abccd00fSHugo Mills 4948abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 4949abccd00fSHugo Mills void __user *arg) 4950abccd00fSHugo Mills { 4951abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 4952abccd00fSHugo Mills int ret = 0; 4953abccd00fSHugo Mills 4954abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 4955abccd00fSHugo Mills if (IS_ERR(sa)) { 4956abccd00fSHugo Mills ret = PTR_ERR(sa); 4957abccd00fSHugo Mills sa = NULL; 4958abccd00fSHugo Mills goto out; 4959abccd00fSHugo Mills } 4960abccd00fSHugo Mills 4961abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 4962abccd00fSHugo Mills 4963abccd00fSHugo Mills if (ret) 4964abccd00fSHugo Mills goto out; 4965abccd00fSHugo Mills 49668ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 49678ea05e3aSAlexander Block if (ret) 49688ea05e3aSAlexander Block ret = -EFAULT; 49698ea05e3aSAlexander Block 49708ea05e3aSAlexander Block out: 49718ea05e3aSAlexander Block kfree(sa); 49728ea05e3aSAlexander Block return ret; 49738ea05e3aSAlexander Block } 49748ea05e3aSAlexander Block 4975867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 4976867ab667Sjeff.liu { 49776d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4978a1b83ac5SAnand Jain size_t len; 4979867ab667Sjeff.liu int ret; 4980a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4981a1b83ac5SAnand Jain 4982a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4983a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 4984a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4985a1b83ac5SAnand Jain 4986a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4987867ab667Sjeff.liu 4988867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 4989efe120a0SFrank Holton btrfs_warn(root->fs_info, 4990efe120a0SFrank Holton "label is too long, return the first %zu bytes", --len); 4991867ab667Sjeff.liu } 4992867ab667Sjeff.liu 4993867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4994867ab667Sjeff.liu 4995867ab667Sjeff.liu return ret ? -EFAULT : 0; 4996867ab667Sjeff.liu } 4997867ab667Sjeff.liu 4998a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4999a8bfd4abSjeff.liu { 50006d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5001a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 5002a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5003a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5004a8bfd4abSjeff.liu int ret; 5005a8bfd4abSjeff.liu 5006a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5007a8bfd4abSjeff.liu return -EPERM; 5008a8bfd4abSjeff.liu 5009a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5010a8bfd4abSjeff.liu return -EFAULT; 5011a8bfd4abSjeff.liu 5012a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 5013efe120a0SFrank Holton btrfs_err(root->fs_info, "unable to set label with more than %d bytes", 5014a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5015a8bfd4abSjeff.liu return -EINVAL; 5016a8bfd4abSjeff.liu } 5017a8bfd4abSjeff.liu 5018a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5019a8bfd4abSjeff.liu if (ret) 5020a8bfd4abSjeff.liu return ret; 5021a8bfd4abSjeff.liu 5022a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5023a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5024a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5025a8bfd4abSjeff.liu goto out_unlock; 5026a8bfd4abSjeff.liu } 5027a8bfd4abSjeff.liu 5028a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 5029a8bfd4abSjeff.liu strcpy(super_block->label, label); 5030a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 5031d0270acaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 5032a8bfd4abSjeff.liu 5033a8bfd4abSjeff.liu out_unlock: 5034a8bfd4abSjeff.liu mnt_drop_write_file(file); 5035a8bfd4abSjeff.liu return ret; 5036a8bfd4abSjeff.liu } 5037a8bfd4abSjeff.liu 50382eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 50392eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 50402eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 50412eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 50422eaa055fSJeff Mahoney 50432eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file, 50442eaa055fSJeff Mahoney void __user *arg) 50452eaa055fSJeff Mahoney { 50462eaa055fSJeff Mahoney static struct btrfs_ioctl_feature_flags features[3] = { 50472eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 50482eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 50492eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 50502eaa055fSJeff Mahoney }; 50512eaa055fSJeff Mahoney 50522eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50532eaa055fSJeff Mahoney return -EFAULT; 50542eaa055fSJeff Mahoney 50552eaa055fSJeff Mahoney return 0; 50562eaa055fSJeff Mahoney } 50572eaa055fSJeff Mahoney 50582eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 50592eaa055fSJeff Mahoney { 50602eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 50612eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 50622eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 50632eaa055fSJeff Mahoney 50642eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 50652eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 50662eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 50672eaa055fSJeff Mahoney 50682eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50692eaa055fSJeff Mahoney return -EFAULT; 50702eaa055fSJeff Mahoney 50712eaa055fSJeff Mahoney return 0; 50722eaa055fSJeff Mahoney } 50732eaa055fSJeff Mahoney 50743b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root, 50753b02a68aSJeff Mahoney enum btrfs_feature_set set, 50762eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 50772eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 50782eaa055fSJeff Mahoney { 50793b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 50803b02a68aSJeff Mahoney char *names; 50812eaa055fSJeff Mahoney u64 disallowed, unsupported; 50822eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 50832eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 50842eaa055fSJeff Mahoney 50852eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 50862eaa055fSJeff Mahoney if (unsupported) { 50873b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 50883b02a68aSJeff Mahoney if (names) { 50893b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50903b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 50913b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50923b02a68aSJeff Mahoney kfree(names); 50933b02a68aSJeff Mahoney } else 50942eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50952eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 50962eaa055fSJeff Mahoney type, unsupported); 50972eaa055fSJeff Mahoney return -EOPNOTSUPP; 50982eaa055fSJeff Mahoney } 50992eaa055fSJeff Mahoney 51002eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 51012eaa055fSJeff Mahoney if (disallowed) { 51023b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 51033b02a68aSJeff Mahoney if (names) { 51043b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 51053b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 51063b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 51073b02a68aSJeff Mahoney kfree(names); 51083b02a68aSJeff Mahoney } else 51092eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 51102eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 51112eaa055fSJeff Mahoney type, disallowed); 51122eaa055fSJeff Mahoney return -EPERM; 51132eaa055fSJeff Mahoney } 51142eaa055fSJeff Mahoney 51152eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 51162eaa055fSJeff Mahoney if (disallowed) { 51173b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 51183b02a68aSJeff Mahoney if (names) { 51193b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 51203b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 51213b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 51223b02a68aSJeff Mahoney kfree(names); 51233b02a68aSJeff Mahoney } else 51242eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 51252eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 51262eaa055fSJeff Mahoney type, disallowed); 51272eaa055fSJeff Mahoney return -EPERM; 51282eaa055fSJeff Mahoney } 51292eaa055fSJeff Mahoney 51302eaa055fSJeff Mahoney return 0; 51312eaa055fSJeff Mahoney } 51322eaa055fSJeff Mahoney 51332eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base) \ 51343b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 51352eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 51362eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 51372eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 51382eaa055fSJeff Mahoney 51392eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 51402eaa055fSJeff Mahoney { 51412eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 51422eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 51432eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 51442eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 51452eaa055fSJeff Mahoney u64 newflags; 51462eaa055fSJeff Mahoney int ret; 51472eaa055fSJeff Mahoney 51482eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 51492eaa055fSJeff Mahoney return -EPERM; 51502eaa055fSJeff Mahoney 51512eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 51522eaa055fSJeff Mahoney return -EFAULT; 51532eaa055fSJeff Mahoney 51542eaa055fSJeff Mahoney /* Nothing to do */ 51552eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 51562eaa055fSJeff Mahoney !flags[0].incompat_flags) 51572eaa055fSJeff Mahoney return 0; 51582eaa055fSJeff Mahoney 51592eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_flags, 51602eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 51612eaa055fSJeff Mahoney if (ret) 51622eaa055fSJeff Mahoney return ret; 51632eaa055fSJeff Mahoney 51642eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_ro_flags, 51652eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 51662eaa055fSJeff Mahoney if (ret) 51672eaa055fSJeff Mahoney return ret; 51682eaa055fSJeff Mahoney 51692eaa055fSJeff Mahoney ret = check_feature(root, flags[0].incompat_flags, 51702eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 51712eaa055fSJeff Mahoney if (ret) 51722eaa055fSJeff Mahoney return ret; 51732eaa055fSJeff Mahoney 51748051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 51752eaa055fSJeff Mahoney if (IS_ERR(trans)) 51762eaa055fSJeff Mahoney return PTR_ERR(trans); 51772eaa055fSJeff Mahoney 51782eaa055fSJeff Mahoney spin_lock(&root->fs_info->super_lock); 51792eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 51802eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 51812eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 51822eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 51832eaa055fSJeff Mahoney 51842eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 51852eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 51862eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 51872eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 51882eaa055fSJeff Mahoney 51892eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 51902eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 51912eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 51922eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 51932eaa055fSJeff Mahoney spin_unlock(&root->fs_info->super_lock); 51942eaa055fSJeff Mahoney 5195d0270acaSJeff Mahoney return btrfs_commit_transaction(trans, root); 51962eaa055fSJeff Mahoney } 51972eaa055fSJeff Mahoney 5198f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5199f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5200f46b5a66SChristoph Hellwig { 5201496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 52024bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5203f46b5a66SChristoph Hellwig 5204f46b5a66SChristoph Hellwig switch (cmd) { 52056cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 52066cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 52076cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 52086cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 52096cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 52106cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5211f7039b1dSLi Dongyang case FITRIM: 5212f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5213f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5214fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5215fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5216fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 52173de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5218fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 52196f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 52206f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 522176dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 522276dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 52230caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 52240caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 52250caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 52260caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 52276ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 52286ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5229f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 52301e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 52311e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 52321e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5233f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5234198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5235f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 52364bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 5237f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5238da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 5239475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 5240475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 5241475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 5242475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 5243f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 52449ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5245f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 5246c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 5247c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 52487a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 5249f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 5250f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 5251f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 5252f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 5253ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5254ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5255ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5256ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5257d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5258d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5259d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5260d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 52611406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 52621406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 52639b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 52649b199859SFilipe David Borba Manana int ret; 52659b199859SFilipe David Borba Manana 52666c255e67SMiao Xie ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 52679b199859SFilipe David Borba Manana if (ret) 52689b199859SFilipe David Borba Manana return ret; 52699b199859SFilipe David Borba Manana ret = btrfs_sync_fs(file->f_dentry->d_sb, 1); 52709b199859SFilipe David Borba Manana return ret; 52719b199859SFilipe David Borba Manana } 527246204592SSage Weil case BTRFS_IOC_START_SYNC: 52739a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 527446204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 52759a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 5276475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5277b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5278475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 5279475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 5280475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 5281475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 5282c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 52839ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5284837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 5285837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 528619a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 528719a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 52888ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 52898ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5290abccd00fSHugo Mills #ifdef CONFIG_64BIT 5291abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5292abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5293abccd00fSHugo Mills #endif 529431db9f7cSAlexander Block case BTRFS_IOC_SEND: 529531db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 5296c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 5297b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 52985d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5299905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 53005d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5301905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 53025d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5303905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 53045d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5305905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 53062f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 53072f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 53082f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 53092f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 531057254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 531157254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 53123f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 53133f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 5314867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5315867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5316a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5317a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 5318416161dbSMark Fasheh case BTRFS_IOC_FILE_EXTENT_SAME: 5319416161dbSMark Fasheh return btrfs_ioctl_file_extent_same(file, argp); 53202eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 53212eaa055fSJeff Mahoney return btrfs_ioctl_get_supported_features(file, argp); 53222eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 53232eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 53242eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 53252eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5326f46b5a66SChristoph Hellwig } 5327f46b5a66SChristoph Hellwig 5328f46b5a66SChristoph Hellwig return -ENOTTY; 5329f46b5a66SChristoph Hellwig } 5330