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, 196012544442SGerhard Heift size_t buf_size, 1961ac8e9819SChris Mason char *buf, 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 1993*8f5f6178SGerhard Heift if (sizeof(sh) + item_len > buf_size) { 1994*8f5f6178SGerhard Heift if (*num_found) { 1995*8f5f6178SGerhard Heift ret = 1; 1996*8f5f6178SGerhard Heift goto out; 1997*8f5f6178SGerhard Heift } 1998*8f5f6178SGerhard Heift 1999*8f5f6178SGerhard Heift /* 2000*8f5f6178SGerhard Heift * return one empty item back for v1, which does not 2001*8f5f6178SGerhard Heift * handle -EOVERFLOW 2002*8f5f6178SGerhard Heift */ 2003*8f5f6178SGerhard Heift 2004ac8e9819SChris Mason item_len = 0; 2005*8f5f6178SGerhard Heift ret = -EOVERFLOW; 2006*8f5f6178SGerhard Heift } 2007ac8e9819SChris Mason 200812544442SGerhard Heift if (sizeof(sh) + item_len + *sk_offset > buf_size) { 2009ac8e9819SChris Mason ret = 1; 201025c9bc2eSGerhard Heift goto out; 2011ac8e9819SChris Mason } 2012ac8e9819SChris Mason 2013ac8e9819SChris Mason sh.objectid = key->objectid; 2014ac8e9819SChris Mason sh.offset = key->offset; 2015ac8e9819SChris Mason sh.type = key->type; 2016ac8e9819SChris Mason sh.len = item_len; 2017ac8e9819SChris Mason sh.transid = found_transid; 2018ac8e9819SChris Mason 2019ac8e9819SChris Mason /* copy search result header */ 2020ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 2021ac8e9819SChris Mason *sk_offset += sizeof(sh); 2022ac8e9819SChris Mason 2023ac8e9819SChris Mason if (item_len) { 2024ac8e9819SChris Mason char *p = buf + *sk_offset; 2025ac8e9819SChris Mason /* copy the item */ 2026ac8e9819SChris Mason read_extent_buffer(leaf, p, 2027ac8e9819SChris Mason item_off, item_len); 2028ac8e9819SChris Mason *sk_offset += item_len; 2029ac8e9819SChris Mason } 2030e2156867SHugo Mills (*num_found)++; 2031ac8e9819SChris Mason 2032*8f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 2033*8f5f6178SGerhard Heift goto out; 2034*8f5f6178SGerhard Heift 203525c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 203625c9bc2eSGerhard Heift ret = 1; 203725c9bc2eSGerhard Heift goto out; 203825c9bc2eSGerhard Heift } 2039ac8e9819SChris Mason } 2040ac8e9819SChris Mason advance_key: 2041ac8e9819SChris Mason ret = 0; 2042abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 2043abc6e134SChris Mason key->offset++; 2044abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 2045abc6e134SChris Mason key->offset = 0; 2046abc6e134SChris Mason key->type++; 2047abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 2048abc6e134SChris Mason key->offset = 0; 2049abc6e134SChris Mason key->type = 0; 2050abc6e134SChris Mason key->objectid++; 2051abc6e134SChris Mason } else 2052abc6e134SChris Mason ret = 1; 205325c9bc2eSGerhard Heift out: 2054ac8e9819SChris Mason return ret; 2055ac8e9819SChris Mason } 2056ac8e9819SChris Mason 2057ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 205812544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 205912544442SGerhard Heift size_t buf_size, 206012544442SGerhard Heift char *buf) 2061ac8e9819SChris Mason { 2062ac8e9819SChris Mason struct btrfs_root *root; 2063ac8e9819SChris Mason struct btrfs_key key; 2064ac8e9819SChris Mason struct btrfs_path *path; 2065ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 2066ac8e9819SChris Mason int ret; 2067ac8e9819SChris Mason int num_found = 0; 2068ac8e9819SChris Mason unsigned long sk_offset = 0; 2069ac8e9819SChris Mason 207012544442SGerhard Heift if (buf_size < sizeof(struct btrfs_ioctl_search_header)) 207112544442SGerhard Heift return -EOVERFLOW; 207212544442SGerhard Heift 2073ac8e9819SChris Mason path = btrfs_alloc_path(); 2074ac8e9819SChris Mason if (!path) 2075ac8e9819SChris Mason return -ENOMEM; 2076ac8e9819SChris Mason 2077ac8e9819SChris Mason if (sk->tree_id == 0) { 2078ac8e9819SChris Mason /* search the root of the inode that was passed */ 2079ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2080ac8e9819SChris Mason } else { 2081ac8e9819SChris Mason key.objectid = sk->tree_id; 2082ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2083ac8e9819SChris Mason key.offset = (u64)-1; 2084ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2085ac8e9819SChris Mason if (IS_ERR(root)) { 2086efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", 2087ac8e9819SChris Mason sk->tree_id); 2088ac8e9819SChris Mason btrfs_free_path(path); 2089ac8e9819SChris Mason return -ENOENT; 2090ac8e9819SChris Mason } 2091ac8e9819SChris Mason } 2092ac8e9819SChris Mason 2093ac8e9819SChris Mason key.objectid = sk->min_objectid; 2094ac8e9819SChris Mason key.type = sk->min_type; 2095ac8e9819SChris Mason key.offset = sk->min_offset; 2096ac8e9819SChris Mason 2097ac8e9819SChris Mason path->keep_locks = 1; 2098ac8e9819SChris Mason 2099ac8e9819SChris Mason while (1) { 21006174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2101ac8e9819SChris Mason if (ret != 0) { 2102ac8e9819SChris Mason if (ret > 0) 2103ac8e9819SChris Mason ret = 0; 2104ac8e9819SChris Mason goto err; 2105ac8e9819SChris Mason } 210612544442SGerhard Heift ret = copy_to_sk(root, path, &key, sk, buf_size, buf, 2107ac8e9819SChris Mason &sk_offset, &num_found); 2108b3b4aa74SDavid Sterba btrfs_release_path(path); 210925c9bc2eSGerhard Heift if (ret) 2110ac8e9819SChris Mason break; 2111ac8e9819SChris Mason 2112ac8e9819SChris Mason } 2113*8f5f6178SGerhard Heift if (ret > 0) 2114ac8e9819SChris Mason ret = 0; 2115ac8e9819SChris Mason err: 2116ac8e9819SChris Mason sk->nr_items = num_found; 2117ac8e9819SChris Mason btrfs_free_path(path); 2118ac8e9819SChris Mason return ret; 2119ac8e9819SChris Mason } 2120ac8e9819SChris Mason 2121ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2122ac8e9819SChris Mason void __user *argp) 2123ac8e9819SChris Mason { 2124ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 2125ac8e9819SChris Mason struct inode *inode; 2126ac8e9819SChris Mason int ret; 2127ac8e9819SChris Mason 2128ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2129ac8e9819SChris Mason return -EPERM; 2130ac8e9819SChris Mason 21312354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 21322354d08fSJulia Lawall if (IS_ERR(args)) 21332354d08fSJulia Lawall return PTR_ERR(args); 2134ac8e9819SChris Mason 2135496ad9aaSAl Viro inode = file_inode(file); 213612544442SGerhard Heift ret = search_ioctl(inode, &args->key, sizeof(args->buf), args->buf); 2137*8f5f6178SGerhard Heift 2138*8f5f6178SGerhard Heift /* 2139*8f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 2140*8f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 2141*8f5f6178SGerhard Heift */ 2142*8f5f6178SGerhard Heift if (ret == -EOVERFLOW) 2143*8f5f6178SGerhard Heift ret = 0; 2144*8f5f6178SGerhard Heift 2145ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2146ac8e9819SChris Mason ret = -EFAULT; 2147ac8e9819SChris Mason kfree(args); 2148ac8e9819SChris Mason return ret; 2149ac8e9819SChris Mason } 2150ac8e9819SChris Mason 215198d377a0STARUISI Hiroaki /* 2152ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2153ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 215498d377a0STARUISI Hiroaki */ 215598d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 215698d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 215798d377a0STARUISI Hiroaki { 215898d377a0STARUISI Hiroaki struct btrfs_root *root; 215998d377a0STARUISI Hiroaki struct btrfs_key key; 2160ac8e9819SChris Mason char *ptr; 216198d377a0STARUISI Hiroaki int ret = -1; 216298d377a0STARUISI Hiroaki int slot; 216398d377a0STARUISI Hiroaki int len; 216498d377a0STARUISI Hiroaki int total_len = 0; 216598d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 216698d377a0STARUISI Hiroaki struct extent_buffer *l; 216798d377a0STARUISI Hiroaki struct btrfs_path *path; 216898d377a0STARUISI Hiroaki 216998d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 217098d377a0STARUISI Hiroaki name[0]='\0'; 217198d377a0STARUISI Hiroaki return 0; 217298d377a0STARUISI Hiroaki } 217398d377a0STARUISI Hiroaki 217498d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 217598d377a0STARUISI Hiroaki if (!path) 217698d377a0STARUISI Hiroaki return -ENOMEM; 217798d377a0STARUISI Hiroaki 2178ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 217998d377a0STARUISI Hiroaki 218098d377a0STARUISI Hiroaki key.objectid = tree_id; 218198d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 218298d377a0STARUISI Hiroaki key.offset = (u64)-1; 218398d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 218498d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2185efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id); 21868ad6fcabSChris Mason ret = -ENOENT; 21878ad6fcabSChris Mason goto out; 218898d377a0STARUISI Hiroaki } 218998d377a0STARUISI Hiroaki 219098d377a0STARUISI Hiroaki key.objectid = dirid; 219198d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 21928ad6fcabSChris Mason key.offset = (u64)-1; 219398d377a0STARUISI Hiroaki 219498d377a0STARUISI Hiroaki while (1) { 219598d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 219698d377a0STARUISI Hiroaki if (ret < 0) 219798d377a0STARUISI Hiroaki goto out; 219818674c6cSFilipe David Borba Manana else if (ret > 0) { 219918674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 220018674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 220118674c6cSFilipe David Borba Manana if (ret < 0) 220218674c6cSFilipe David Borba Manana goto out; 220318674c6cSFilipe David Borba Manana else if (ret > 0) { 2204ac8e9819SChris Mason ret = -ENOENT; 220598d377a0STARUISI Hiroaki goto out; 2206ac8e9819SChris Mason } 220718674c6cSFilipe David Borba Manana } 220818674c6cSFilipe David Borba Manana 220918674c6cSFilipe David Borba Manana l = path->nodes[0]; 221018674c6cSFilipe David Borba Manana slot = path->slots[0]; 221118674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 221298d377a0STARUISI Hiroaki 221398d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 221498d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 221598d377a0STARUISI Hiroaki ptr -= len + 1; 221698d377a0STARUISI Hiroaki total_len += len + 1; 2217a696cf35SFilipe David Borba Manana if (ptr < name) { 2218a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 221998d377a0STARUISI Hiroaki goto out; 2220a696cf35SFilipe David Borba Manana } 222198d377a0STARUISI Hiroaki 222298d377a0STARUISI Hiroaki *(ptr + len) = '/'; 222398d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 222498d377a0STARUISI Hiroaki 222598d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 222698d377a0STARUISI Hiroaki break; 222798d377a0STARUISI Hiroaki 2228b3b4aa74SDavid Sterba btrfs_release_path(path); 222998d377a0STARUISI Hiroaki key.objectid = key.offset; 22308ad6fcabSChris Mason key.offset = (u64)-1; 223198d377a0STARUISI Hiroaki dirid = key.objectid; 223298d377a0STARUISI Hiroaki } 223377906a50SLi Zefan memmove(name, ptr, total_len); 223498d377a0STARUISI Hiroaki name[total_len] = '\0'; 223598d377a0STARUISI Hiroaki ret = 0; 223698d377a0STARUISI Hiroaki out: 223798d377a0STARUISI Hiroaki btrfs_free_path(path); 2238ac8e9819SChris Mason return ret; 2239ac8e9819SChris Mason } 2240ac8e9819SChris Mason 2241ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2242ac8e9819SChris Mason void __user *argp) 2243ac8e9819SChris Mason { 2244ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2245ac8e9819SChris Mason struct inode *inode; 2246ac8e9819SChris Mason int ret; 2247ac8e9819SChris Mason 2248ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2249ac8e9819SChris Mason return -EPERM; 2250ac8e9819SChris Mason 22512354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 22522354d08fSJulia Lawall if (IS_ERR(args)) 22532354d08fSJulia Lawall return PTR_ERR(args); 2254c2b96929SDan Carpenter 2255496ad9aaSAl Viro inode = file_inode(file); 2256ac8e9819SChris Mason 22571b53ac4dSChris Mason if (args->treeid == 0) 22581b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 22591b53ac4dSChris Mason 2260ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2261ac8e9819SChris Mason args->treeid, args->objectid, 2262ac8e9819SChris Mason args->name); 2263ac8e9819SChris Mason 2264ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2265ac8e9819SChris Mason ret = -EFAULT; 2266ac8e9819SChris Mason 2267ac8e9819SChris Mason kfree(args); 226898d377a0STARUISI Hiroaki return ret; 226998d377a0STARUISI Hiroaki } 227098d377a0STARUISI Hiroaki 227176dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 227276dda93cSYan, Zheng void __user *arg) 227376dda93cSYan, Zheng { 227454563d41SAl Viro struct dentry *parent = file->f_path.dentry; 227576dda93cSYan, Zheng struct dentry *dentry; 227676dda93cSYan, Zheng struct inode *dir = parent->d_inode; 227776dda93cSYan, Zheng struct inode *inode; 227876dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 227976dda93cSYan, Zheng struct btrfs_root *dest = NULL; 228076dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 228176dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2282c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2283521e0546SDavid Sterba u64 root_flags; 2284c58aaad2SMiao Xie u64 qgroup_reserved; 228576dda93cSYan, Zheng int namelen; 228676dda93cSYan, Zheng int ret; 228776dda93cSYan, Zheng int err = 0; 228876dda93cSYan, Zheng 228976dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 229076dda93cSYan, Zheng if (IS_ERR(vol_args)) 229176dda93cSYan, Zheng return PTR_ERR(vol_args); 229276dda93cSYan, Zheng 229376dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 229476dda93cSYan, Zheng namelen = strlen(vol_args->name); 229576dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 229676dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 229776dda93cSYan, Zheng err = -EINVAL; 229876dda93cSYan, Zheng goto out; 229976dda93cSYan, Zheng } 230076dda93cSYan, Zheng 2301a561be71SAl Viro err = mnt_want_write_file(file); 230276dda93cSYan, Zheng if (err) 230376dda93cSYan, Zheng goto out; 230476dda93cSYan, Zheng 2305521e0546SDavid Sterba 23065c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 23075c50c9b8SDavid Sterba if (err == -EINTR) 2308e43f998eSDavid Sterba goto out_drop_write; 230976dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 231076dda93cSYan, Zheng if (IS_ERR(dentry)) { 231176dda93cSYan, Zheng err = PTR_ERR(dentry); 231276dda93cSYan, Zheng goto out_unlock_dir; 231376dda93cSYan, Zheng } 231476dda93cSYan, Zheng 231576dda93cSYan, Zheng if (!dentry->d_inode) { 231676dda93cSYan, Zheng err = -ENOENT; 231776dda93cSYan, Zheng goto out_dput; 231876dda93cSYan, Zheng } 231976dda93cSYan, Zheng 232076dda93cSYan, Zheng inode = dentry->d_inode; 23214260f7c7SSage Weil dest = BTRFS_I(inode)->root; 23224260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 23234260f7c7SSage Weil /* 23244260f7c7SSage Weil * Regular user. Only allow this with a special mount 23254260f7c7SSage Weil * option, when the user has write+exec access to the 23264260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23274260f7c7SSage Weil * allowed. 23284260f7c7SSage Weil * 23294260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23304260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23314260f7c7SSage Weil * otherwise be able to delete. 23324260f7c7SSage Weil * 23334260f7c7SSage Weil * Users who want to delete empty subvols should try 23344260f7c7SSage Weil * rmdir(2). 23354260f7c7SSage Weil */ 23364260f7c7SSage Weil err = -EPERM; 23374260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 23384260f7c7SSage Weil goto out_dput; 23394260f7c7SSage Weil 23404260f7c7SSage Weil /* 23414260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 23424260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 23434260f7c7SSage Weil * must be called on the dentry referencing the root 23444260f7c7SSage Weil * of the subvol, not a random directory contained 23454260f7c7SSage Weil * within it. 23464260f7c7SSage Weil */ 23474260f7c7SSage Weil err = -EINVAL; 23484260f7c7SSage Weil if (root == dest) 23494260f7c7SSage Weil goto out_dput; 23504260f7c7SSage Weil 23514260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 23524260f7c7SSage Weil if (err) 23534260f7c7SSage Weil goto out_dput; 23545c39da5bSMiao Xie } 23554260f7c7SSage Weil 23565c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 23574260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 23584260f7c7SSage Weil if (err) 23594260f7c7SSage Weil goto out_dput; 23604260f7c7SSage Weil 236133345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 236276dda93cSYan, Zheng err = -EINVAL; 236376dda93cSYan, Zheng goto out_dput; 236476dda93cSYan, Zheng } 236576dda93cSYan, Zheng 236676dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 2367521e0546SDavid Sterba 2368521e0546SDavid Sterba /* 2369521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2370521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2371521e0546SDavid Sterba * again is not run concurrently. 2372521e0546SDavid Sterba */ 2373521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2374c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2375c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2376c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2377521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2378521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2379521e0546SDavid Sterba } else { 2380521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2381521e0546SDavid Sterba btrfs_warn(root->fs_info, 2382521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2383c55bfa67SFilipe Manana dest->root_key.objectid); 2384521e0546SDavid Sterba err = -EPERM; 2385521e0546SDavid Sterba goto out_dput; 2386521e0546SDavid Sterba } 2387521e0546SDavid Sterba 238876dda93cSYan, Zheng err = d_invalidate(dentry); 238976dda93cSYan, Zheng if (err) 239076dda93cSYan, Zheng goto out_unlock; 239176dda93cSYan, Zheng 239276dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 239376dda93cSYan, Zheng 239476dda93cSYan, Zheng err = may_destroy_subvol(dest); 239576dda93cSYan, Zheng if (err) 239676dda93cSYan, Zheng goto out_up_write; 239776dda93cSYan, Zheng 2398c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2399c58aaad2SMiao Xie /* 2400c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2401c58aaad2SMiao Xie * ref/backref. 2402c58aaad2SMiao Xie */ 2403c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2404ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2405c58aaad2SMiao Xie if (err) 2406c58aaad2SMiao Xie goto out_up_write; 2407c58aaad2SMiao Xie 2408a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2409a22285a6SYan, Zheng if (IS_ERR(trans)) { 2410a22285a6SYan, Zheng err = PTR_ERR(trans); 2411c58aaad2SMiao Xie goto out_release; 2412a22285a6SYan, Zheng } 2413c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2414c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2415a22285a6SYan, Zheng 241676dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 241776dda93cSYan, Zheng dest->root_key.objectid, 241876dda93cSYan, Zheng dentry->d_name.name, 241976dda93cSYan, Zheng dentry->d_name.len); 242079787eaaSJeff Mahoney if (ret) { 242179787eaaSJeff Mahoney err = ret; 242279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 242379787eaaSJeff Mahoney goto out_end_trans; 242479787eaaSJeff Mahoney } 242576dda93cSYan, Zheng 242676dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 242776dda93cSYan, Zheng 242876dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 242976dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 243076dda93cSYan, Zheng dest->root_item.drop_level = 0; 243176dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 243276dda93cSYan, Zheng 243327cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 243476dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 243576dda93cSYan, Zheng root->fs_info->tree_root, 243676dda93cSYan, Zheng dest->root_key.objectid); 243779787eaaSJeff Mahoney if (ret) { 243879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 243979787eaaSJeff Mahoney err = ret; 244079787eaaSJeff Mahoney goto out_end_trans; 2441d68fc57bSYan, Zheng } 244279787eaaSJeff Mahoney } 2443dd5f9615SStefan Behrens 2444dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2445dd5f9615SStefan Behrens dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 2446dd5f9615SStefan Behrens dest->root_key.objectid); 2447dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2448dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2449dd5f9615SStefan Behrens err = ret; 2450dd5f9615SStefan Behrens goto out_end_trans; 2451dd5f9615SStefan Behrens } 2452dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2453dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2454dd5f9615SStefan Behrens dest->root_item.received_uuid, 2455dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2456dd5f9615SStefan Behrens dest->root_key.objectid); 2457dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2458dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2459dd5f9615SStefan Behrens err = ret; 2460dd5f9615SStefan Behrens goto out_end_trans; 2461dd5f9615SStefan Behrens } 2462dd5f9615SStefan Behrens } 2463dd5f9615SStefan Behrens 246479787eaaSJeff Mahoney out_end_trans: 2465c58aaad2SMiao Xie trans->block_rsv = NULL; 2466c58aaad2SMiao Xie trans->bytes_reserved = 0; 2467531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 246879787eaaSJeff Mahoney if (ret && !err) 246979787eaaSJeff Mahoney err = ret; 247076dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2471c58aaad2SMiao Xie out_release: 2472c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 247376dda93cSYan, Zheng out_up_write: 247476dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 247576dda93cSYan, Zheng out_unlock: 2476521e0546SDavid Sterba if (err) { 2477521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2478c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2479c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2480521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2481521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2482521e0546SDavid Sterba } 248376dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 248476dda93cSYan, Zheng if (!err) { 2485efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 248676dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 248776dda93cSYan, Zheng d_delete(dentry); 248861155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2489fa6ac876SLiu Bo 2490fa6ac876SLiu Bo /* the last ref */ 2491fa6ac876SLiu Bo if (dest->cache_inode) { 2492fa6ac876SLiu Bo iput(dest->cache_inode); 2493fa6ac876SLiu Bo dest->cache_inode = NULL; 2494fa6ac876SLiu Bo } 249576dda93cSYan, Zheng } 249676dda93cSYan, Zheng out_dput: 249776dda93cSYan, Zheng dput(dentry); 249876dda93cSYan, Zheng out_unlock_dir: 249976dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 2500e43f998eSDavid Sterba out_drop_write: 25012a79f17eSAl Viro mnt_drop_write_file(file); 250276dda93cSYan, Zheng out: 250376dda93cSYan, Zheng kfree(vol_args); 250476dda93cSYan, Zheng return err; 250576dda93cSYan, Zheng } 250676dda93cSYan, Zheng 25071e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2508f46b5a66SChristoph Hellwig { 2509496ad9aaSAl Viro struct inode *inode = file_inode(file); 2510f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 25111e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2512c146afadSYan Zheng int ret; 2513c146afadSYan Zheng 251425122d15SIlya Dryomov ret = mnt_want_write_file(file); 251525122d15SIlya Dryomov if (ret) 251625122d15SIlya Dryomov return ret; 2517b83cc969SLi Zefan 251825122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 251925122d15SIlya Dryomov ret = -EROFS; 252025122d15SIlya Dryomov goto out; 25215ac00addSStefan Behrens } 2522f46b5a66SChristoph Hellwig 2523f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2524f46b5a66SChristoph Hellwig case S_IFDIR: 2525e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2526e441d54dSChris Mason ret = -EPERM; 2527e441d54dSChris Mason goto out; 2528e441d54dSChris Mason } 2529de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 25308929ecfaSYan, Zheng if (ret) 25318929ecfaSYan, Zheng goto out; 2532de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2533f46b5a66SChristoph Hellwig break; 2534f46b5a66SChristoph Hellwig case S_IFREG: 2535e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2536e441d54dSChris Mason ret = -EINVAL; 2537e441d54dSChris Mason goto out; 2538e441d54dSChris Mason } 25391e701a32SChris Mason 25401e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 25411e701a32SChris Mason if (!range) { 25421e701a32SChris Mason ret = -ENOMEM; 25431e701a32SChris Mason goto out; 25441e701a32SChris Mason } 25451e701a32SChris Mason 25461e701a32SChris Mason if (argp) { 25471e701a32SChris Mason if (copy_from_user(range, argp, 25481e701a32SChris Mason sizeof(*range))) { 25491e701a32SChris Mason ret = -EFAULT; 25501e701a32SChris Mason kfree(range); 2551683be16eSDan Carpenter goto out; 25521e701a32SChris Mason } 25531e701a32SChris Mason /* compression requires us to start the IO */ 25541e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 25551e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 25561e701a32SChris Mason range->extent_thresh = (u32)-1; 25571e701a32SChris Mason } 25581e701a32SChris Mason } else { 25591e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 25601e701a32SChris Mason range->len = (u64)-1; 25611e701a32SChris Mason } 2562496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 25634cb5300bSChris Mason range, 0, 0); 25644cb5300bSChris Mason if (ret > 0) 25654cb5300bSChris Mason ret = 0; 25661e701a32SChris Mason kfree(range); 2567f46b5a66SChristoph Hellwig break; 25688929ecfaSYan, Zheng default: 25698929ecfaSYan, Zheng ret = -EINVAL; 2570f46b5a66SChristoph Hellwig } 2571e441d54dSChris Mason out: 257225122d15SIlya Dryomov mnt_drop_write_file(file); 2573e441d54dSChris Mason return ret; 2574f46b5a66SChristoph Hellwig } 2575f46b5a66SChristoph Hellwig 2576b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2577f46b5a66SChristoph Hellwig { 2578f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2579f46b5a66SChristoph Hellwig int ret; 2580f46b5a66SChristoph Hellwig 2581e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2582e441d54dSChris Mason return -EPERM; 2583e441d54dSChris Mason 25845ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 25855ac00addSStefan Behrens 1)) { 2586e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2587c9e9f97bSIlya Dryomov } 2588c9e9f97bSIlya Dryomov 25895ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2590dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2591c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2592c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2593c9e9f97bSIlya Dryomov goto out; 2594c9e9f97bSIlya Dryomov } 2595f46b5a66SChristoph Hellwig 25965516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2597f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2598f46b5a66SChristoph Hellwig 2599f46b5a66SChristoph Hellwig kfree(vol_args); 2600c9e9f97bSIlya Dryomov out: 2601c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26025ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2603f46b5a66SChristoph Hellwig return ret; 2604f46b5a66SChristoph Hellwig } 2605f46b5a66SChristoph Hellwig 2606da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2607f46b5a66SChristoph Hellwig { 2608496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2609f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2610f46b5a66SChristoph Hellwig int ret; 2611f46b5a66SChristoph Hellwig 2612e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2613e441d54dSChris Mason return -EPERM; 2614e441d54dSChris Mason 2615da24927bSMiao Xie ret = mnt_want_write_file(file); 2616da24927bSMiao Xie if (ret) 2617da24927bSMiao Xie return ret; 2618c146afadSYan Zheng 2619dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2620c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2621c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2622c9e9f97bSIlya Dryomov goto out; 2623c9e9f97bSIlya Dryomov } 2624f46b5a66SChristoph Hellwig 26255516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2626f46b5a66SChristoph Hellwig 2627183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2628183860f6SAnand Jain 1)) { 2629183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2630183860f6SAnand Jain goto out; 2631183860f6SAnand Jain } 2632183860f6SAnand Jain 2633183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2634183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2635c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26365ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2637183860f6SAnand Jain 2638183860f6SAnand Jain out: 2639183860f6SAnand Jain kfree(vol_args); 26404ac20c70SIlya Dryomov mnt_drop_write_file(file); 2641f46b5a66SChristoph Hellwig return ret; 2642f46b5a66SChristoph Hellwig } 2643f46b5a66SChristoph Hellwig 2644475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2645475f6387SJan Schmidt { 2646027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2647475f6387SJan Schmidt struct btrfs_device *device; 2648475f6387SJan Schmidt struct btrfs_device *next; 2649475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2650027ed2f0SLi Zefan int ret = 0; 2651475f6387SJan Schmidt 2652027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2653027ed2f0SLi Zefan if (!fi_args) 2654027ed2f0SLi Zefan return -ENOMEM; 2655027ed2f0SLi Zefan 2656f7171750SFilipe David Borba Manana mutex_lock(&fs_devices->device_list_mutex); 2657027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2658027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2659475f6387SJan Schmidt 2660475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2661027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2662027ed2f0SLi Zefan fi_args->max_id = device->devid; 2663475f6387SJan Schmidt } 2664475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2665475f6387SJan Schmidt 266680a773fbSDavid Sterba fi_args->nodesize = root->fs_info->super_copy->nodesize; 266780a773fbSDavid Sterba fi_args->sectorsize = root->fs_info->super_copy->sectorsize; 266880a773fbSDavid Sterba fi_args->clone_alignment = root->fs_info->super_copy->sectorsize; 266980a773fbSDavid Sterba 2670027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2671027ed2f0SLi Zefan ret = -EFAULT; 2672475f6387SJan Schmidt 2673027ed2f0SLi Zefan kfree(fi_args); 2674027ed2f0SLi Zefan return ret; 2675475f6387SJan Schmidt } 2676475f6387SJan Schmidt 2677475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2678475f6387SJan Schmidt { 2679475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2680475f6387SJan Schmidt struct btrfs_device *dev; 2681475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2682475f6387SJan Schmidt int ret = 0; 2683475f6387SJan Schmidt char *s_uuid = NULL; 2684475f6387SJan Schmidt 2685475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2686475f6387SJan Schmidt if (IS_ERR(di_args)) 2687475f6387SJan Schmidt return PTR_ERR(di_args); 2688475f6387SJan Schmidt 2689dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2690475f6387SJan Schmidt s_uuid = di_args->uuid; 2691475f6387SJan Schmidt 2692475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2693aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2694475f6387SJan Schmidt 2695475f6387SJan Schmidt if (!dev) { 2696475f6387SJan Schmidt ret = -ENODEV; 2697475f6387SJan Schmidt goto out; 2698475f6387SJan Schmidt } 2699475f6387SJan Schmidt 2700475f6387SJan Schmidt di_args->devid = dev->devid; 2701475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2702475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2703475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2704a27202fbSJim Meyering if (dev->name) { 2705606686eeSJosef Bacik struct rcu_string *name; 2706606686eeSJosef Bacik 2707606686eeSJosef Bacik rcu_read_lock(); 2708606686eeSJosef Bacik name = rcu_dereference(dev->name); 2709606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2710606686eeSJosef Bacik rcu_read_unlock(); 2711a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2712a27202fbSJim Meyering } else { 271399ba55adSStefan Behrens di_args->path[0] = '\0'; 2714a27202fbSJim Meyering } 2715475f6387SJan Schmidt 2716475f6387SJan Schmidt out: 271755793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2718475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2719475f6387SJan Schmidt ret = -EFAULT; 2720475f6387SJan Schmidt 2721475f6387SJan Schmidt kfree(di_args); 2722475f6387SJan Schmidt return ret; 2723475f6387SJan Schmidt } 2724475f6387SJan Schmidt 2725416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off) 2726416161dbSMark Fasheh { 2727416161dbSMark Fasheh struct page *page; 2728416161dbSMark Fasheh pgoff_t index; 2729416161dbSMark Fasheh struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 2730416161dbSMark Fasheh 2731416161dbSMark Fasheh index = off >> PAGE_CACHE_SHIFT; 2732416161dbSMark Fasheh 2733416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2734416161dbSMark Fasheh if (!page) 2735416161dbSMark Fasheh return NULL; 2736416161dbSMark Fasheh 2737416161dbSMark Fasheh if (!PageUptodate(page)) { 2738416161dbSMark Fasheh if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, 2739416161dbSMark Fasheh 0)) 2740416161dbSMark Fasheh return NULL; 2741416161dbSMark Fasheh lock_page(page); 2742416161dbSMark Fasheh if (!PageUptodate(page)) { 2743416161dbSMark Fasheh unlock_page(page); 2744416161dbSMark Fasheh page_cache_release(page); 2745416161dbSMark Fasheh return NULL; 2746416161dbSMark Fasheh } 2747416161dbSMark Fasheh } 2748416161dbSMark Fasheh unlock_page(page); 2749416161dbSMark Fasheh 2750416161dbSMark Fasheh return page; 2751416161dbSMark Fasheh } 2752416161dbSMark Fasheh 275377fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) 275477fe20dcSMark Fasheh { 275577fe20dcSMark Fasheh /* do any pending delalloc/csum calc on src, one way or 275677fe20dcSMark Fasheh another, and lock file content */ 275777fe20dcSMark Fasheh while (1) { 275877fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 275977fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 276077fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 276177fe20dcSMark Fasheh off + len - 1); 2762ff5df9b8SFilipe Manana if ((!ordered || 2763ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2764ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 276577fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2766ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2767ff5df9b8SFilipe Manana if (ordered) 2768ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 276977fe20dcSMark Fasheh break; 2770ff5df9b8SFilipe Manana } 277177fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 277277fe20dcSMark Fasheh if (ordered) 277377fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 277477fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 277577fe20dcSMark Fasheh } 277677fe20dcSMark Fasheh } 277777fe20dcSMark Fasheh 2778416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1, 2779416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2780416161dbSMark Fasheh { 2781416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2782416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2783416161dbSMark Fasheh 2784416161dbSMark Fasheh mutex_unlock(&inode1->i_mutex); 2785416161dbSMark Fasheh mutex_unlock(&inode2->i_mutex); 2786416161dbSMark Fasheh } 2787416161dbSMark Fasheh 2788416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1, 2789416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2790416161dbSMark Fasheh { 2791416161dbSMark Fasheh if (inode1 < inode2) { 2792416161dbSMark Fasheh swap(inode1, inode2); 2793416161dbSMark Fasheh swap(loff1, loff2); 2794416161dbSMark Fasheh } 2795416161dbSMark Fasheh 2796416161dbSMark Fasheh mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); 2797416161dbSMark Fasheh lock_extent_range(inode1, loff1, len); 2798416161dbSMark Fasheh if (inode1 != inode2) { 2799416161dbSMark Fasheh mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); 2800416161dbSMark Fasheh lock_extent_range(inode2, loff2, len); 2801416161dbSMark Fasheh } 2802416161dbSMark Fasheh } 2803416161dbSMark Fasheh 2804416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, 2805416161dbSMark Fasheh u64 dst_loff, u64 len) 2806416161dbSMark Fasheh { 2807416161dbSMark Fasheh int ret = 0; 2808416161dbSMark Fasheh struct page *src_page, *dst_page; 2809416161dbSMark Fasheh unsigned int cmp_len = PAGE_CACHE_SIZE; 2810416161dbSMark Fasheh void *addr, *dst_addr; 2811416161dbSMark Fasheh 2812416161dbSMark Fasheh while (len) { 2813416161dbSMark Fasheh if (len < PAGE_CACHE_SIZE) 2814416161dbSMark Fasheh cmp_len = len; 2815416161dbSMark Fasheh 2816416161dbSMark Fasheh src_page = extent_same_get_page(src, loff); 2817416161dbSMark Fasheh if (!src_page) 2818416161dbSMark Fasheh return -EINVAL; 2819416161dbSMark Fasheh dst_page = extent_same_get_page(dst, dst_loff); 2820416161dbSMark Fasheh if (!dst_page) { 2821416161dbSMark Fasheh page_cache_release(src_page); 2822416161dbSMark Fasheh return -EINVAL; 2823416161dbSMark Fasheh } 2824416161dbSMark Fasheh addr = kmap_atomic(src_page); 2825416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 2826416161dbSMark Fasheh 2827416161dbSMark Fasheh flush_dcache_page(src_page); 2828416161dbSMark Fasheh flush_dcache_page(dst_page); 2829416161dbSMark Fasheh 2830416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 2831416161dbSMark Fasheh ret = BTRFS_SAME_DATA_DIFFERS; 2832416161dbSMark Fasheh 2833416161dbSMark Fasheh kunmap_atomic(addr); 2834416161dbSMark Fasheh kunmap_atomic(dst_addr); 2835416161dbSMark Fasheh page_cache_release(src_page); 2836416161dbSMark Fasheh page_cache_release(dst_page); 2837416161dbSMark Fasheh 2838416161dbSMark Fasheh if (ret) 2839416161dbSMark Fasheh break; 2840416161dbSMark Fasheh 2841416161dbSMark Fasheh loff += cmp_len; 2842416161dbSMark Fasheh dst_loff += cmp_len; 2843416161dbSMark Fasheh len -= cmp_len; 2844416161dbSMark Fasheh } 2845416161dbSMark Fasheh 2846416161dbSMark Fasheh return ret; 2847416161dbSMark Fasheh } 2848416161dbSMark Fasheh 2849416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) 2850416161dbSMark Fasheh { 2851416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 2852416161dbSMark Fasheh 2853416161dbSMark Fasheh if (off + len > inode->i_size || off + len < off) 2854416161dbSMark Fasheh return -EINVAL; 2855416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 2856416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 2857416161dbSMark Fasheh return -EINVAL; 2858416161dbSMark Fasheh 2859416161dbSMark Fasheh return 0; 2860416161dbSMark Fasheh } 2861416161dbSMark Fasheh 2862416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, 2863416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 2864416161dbSMark Fasheh { 2865416161dbSMark Fasheh int ret; 2866416161dbSMark Fasheh 2867416161dbSMark Fasheh /* 2868416161dbSMark Fasheh * btrfs_clone() can't handle extents in the same file 2869416161dbSMark Fasheh * yet. Once that works, we can drop this check and replace it 2870416161dbSMark Fasheh * with a check for the same inode, but overlapping extents. 2871416161dbSMark Fasheh */ 2872416161dbSMark Fasheh if (src == dst) 2873416161dbSMark Fasheh return -EINVAL; 2874416161dbSMark Fasheh 2875416161dbSMark Fasheh btrfs_double_lock(src, loff, dst, dst_loff, len); 2876416161dbSMark Fasheh 2877416161dbSMark Fasheh ret = extent_same_check_offsets(src, loff, len); 2878416161dbSMark Fasheh if (ret) 2879416161dbSMark Fasheh goto out_unlock; 2880416161dbSMark Fasheh 2881416161dbSMark Fasheh ret = extent_same_check_offsets(dst, dst_loff, len); 2882416161dbSMark Fasheh if (ret) 2883416161dbSMark Fasheh goto out_unlock; 2884416161dbSMark Fasheh 2885416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 2886416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 2887416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 2888416161dbSMark Fasheh ret = -EINVAL; 2889416161dbSMark Fasheh goto out_unlock; 2890416161dbSMark Fasheh } 2891416161dbSMark Fasheh 2892416161dbSMark Fasheh ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); 2893416161dbSMark Fasheh if (ret == 0) 2894416161dbSMark Fasheh ret = btrfs_clone(src, dst, loff, len, len, dst_loff); 2895416161dbSMark Fasheh 2896416161dbSMark Fasheh out_unlock: 2897416161dbSMark Fasheh btrfs_double_unlock(src, loff, dst, dst_loff, len); 2898416161dbSMark Fasheh 2899416161dbSMark Fasheh return ret; 2900416161dbSMark Fasheh } 2901416161dbSMark Fasheh 2902416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) 2903416161dbSMark Fasheh 2904416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file, 29051c1c8747SAl Viro struct btrfs_ioctl_same_args __user *argp) 2906416161dbSMark Fasheh { 2907cbf8b8caSMark Fasheh struct btrfs_ioctl_same_args *same; 2908cbf8b8caSMark Fasheh struct btrfs_ioctl_same_extent_info *info; 29091c1c8747SAl Viro struct inode *src = file_inode(file); 2910416161dbSMark Fasheh u64 off; 2911416161dbSMark Fasheh u64 len; 2912416161dbSMark Fasheh int i; 2913416161dbSMark Fasheh int ret; 2914cbf8b8caSMark Fasheh unsigned long size; 2915416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 2916416161dbSMark Fasheh bool is_admin = capable(CAP_SYS_ADMIN); 29171c1c8747SAl Viro u16 count; 2918416161dbSMark Fasheh 2919416161dbSMark Fasheh if (!(file->f_mode & FMODE_READ)) 2920416161dbSMark Fasheh return -EINVAL; 2921416161dbSMark Fasheh 2922416161dbSMark Fasheh ret = mnt_want_write_file(file); 2923416161dbSMark Fasheh if (ret) 2924416161dbSMark Fasheh return ret; 2925416161dbSMark Fasheh 29261c1c8747SAl Viro if (get_user(count, &argp->dest_count)) { 2927416161dbSMark Fasheh ret = -EFAULT; 2928416161dbSMark Fasheh goto out; 2929416161dbSMark Fasheh } 2930416161dbSMark Fasheh 29311c1c8747SAl Viro size = offsetof(struct btrfs_ioctl_same_args __user, info[count]); 2932cbf8b8caSMark Fasheh 29331c1c8747SAl Viro same = memdup_user(argp, size); 2934cbf8b8caSMark Fasheh 2935229eed43SGeyslan G. Bem if (IS_ERR(same)) { 2936229eed43SGeyslan G. Bem ret = PTR_ERR(same); 2937cbf8b8caSMark Fasheh goto out; 2938cbf8b8caSMark Fasheh } 2939cbf8b8caSMark Fasheh 2940cbf8b8caSMark Fasheh off = same->logical_offset; 2941cbf8b8caSMark Fasheh len = same->length; 2942416161dbSMark Fasheh 2943416161dbSMark Fasheh /* 2944416161dbSMark Fasheh * Limit the total length we will dedupe for each operation. 2945416161dbSMark Fasheh * This is intended to bound the total time spent in this 2946416161dbSMark Fasheh * ioctl to something sane. 2947416161dbSMark Fasheh */ 2948416161dbSMark Fasheh if (len > BTRFS_MAX_DEDUPE_LEN) 2949416161dbSMark Fasheh len = BTRFS_MAX_DEDUPE_LEN; 2950416161dbSMark Fasheh 2951416161dbSMark Fasheh if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { 2952416161dbSMark Fasheh /* 2953416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 2954416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 2955416161dbSMark Fasheh * this situation without an update. 2956416161dbSMark Fasheh */ 2957416161dbSMark Fasheh ret = -EINVAL; 2958416161dbSMark Fasheh goto out; 2959416161dbSMark Fasheh } 2960416161dbSMark Fasheh 2961416161dbSMark Fasheh ret = -EISDIR; 2962416161dbSMark Fasheh if (S_ISDIR(src->i_mode)) 2963416161dbSMark Fasheh goto out; 2964416161dbSMark Fasheh 2965416161dbSMark Fasheh ret = -EACCES; 2966416161dbSMark Fasheh if (!S_ISREG(src->i_mode)) 2967416161dbSMark Fasheh goto out; 2968416161dbSMark Fasheh 2969cbf8b8caSMark Fasheh /* pre-format output fields to sane values */ 29701c1c8747SAl Viro for (i = 0; i < count; i++) { 2971cbf8b8caSMark Fasheh same->info[i].bytes_deduped = 0ULL; 2972cbf8b8caSMark Fasheh same->info[i].status = 0; 2973416161dbSMark Fasheh } 2974416161dbSMark Fasheh 29751c1c8747SAl Viro for (i = 0, info = same->info; i < count; i++, info++) { 29761c1c8747SAl Viro struct inode *dst; 29771c1c8747SAl Viro struct fd dst_file = fdget(info->fd); 29781c1c8747SAl Viro if (!dst_file.file) { 2979cbf8b8caSMark Fasheh info->status = -EBADF; 29801c1c8747SAl Viro continue; 2981416161dbSMark Fasheh } 29821c1c8747SAl Viro dst = file_inode(dst_file.file); 2983416161dbSMark Fasheh 29841c1c8747SAl Viro if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) { 2985cbf8b8caSMark Fasheh info->status = -EINVAL; 29861c1c8747SAl Viro } else if (file->f_path.mnt != dst_file.file->f_path.mnt) { 2987cbf8b8caSMark Fasheh info->status = -EXDEV; 29881c1c8747SAl Viro } else if (S_ISDIR(dst->i_mode)) { 2989cbf8b8caSMark Fasheh info->status = -EISDIR; 29901c1c8747SAl Viro } else if (!S_ISREG(dst->i_mode)) { 2991cbf8b8caSMark Fasheh info->status = -EACCES; 29921c1c8747SAl Viro } else { 2993cbf8b8caSMark Fasheh info->status = btrfs_extent_same(src, off, len, dst, 2994cbf8b8caSMark Fasheh info->logical_offset); 2995cbf8b8caSMark Fasheh if (info->status == 0) 2996cbf8b8caSMark Fasheh info->bytes_deduped += len; 29971c1c8747SAl Viro } 29981c1c8747SAl Viro fdput(dst_file); 2999cbf8b8caSMark Fasheh } 3000416161dbSMark Fasheh 3001cbf8b8caSMark Fasheh ret = copy_to_user(argp, same, size); 3002cbf8b8caSMark Fasheh if (ret) 3003416161dbSMark Fasheh ret = -EFAULT; 3004416161dbSMark Fasheh 3005416161dbSMark Fasheh out: 3006416161dbSMark Fasheh mnt_drop_write_file(file); 3007416161dbSMark Fasheh return ret; 3008416161dbSMark Fasheh } 3009416161dbSMark Fasheh 3010fcebe456SJosef Bacik /* Helper to check and see if this root currently has a ref on the given disk 3011fcebe456SJosef Bacik * bytenr. If it does then we need to update the quota for this root. This 3012fcebe456SJosef Bacik * doesn't do anything if quotas aren't enabled. 3013fcebe456SJosef Bacik */ 3014fcebe456SJosef Bacik static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3015fcebe456SJosef Bacik u64 disko) 3016fcebe456SJosef Bacik { 3017fcebe456SJosef Bacik struct seq_list tree_mod_seq_elem = {}; 3018fcebe456SJosef Bacik struct ulist *roots; 3019fcebe456SJosef Bacik struct ulist_iterator uiter; 3020fcebe456SJosef Bacik struct ulist_node *root_node = NULL; 3021fcebe456SJosef Bacik int ret; 3022fcebe456SJosef Bacik 3023fcebe456SJosef Bacik if (!root->fs_info->quota_enabled) 3024fcebe456SJosef Bacik return 1; 3025fcebe456SJosef Bacik 3026fcebe456SJosef Bacik btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3027fcebe456SJosef Bacik ret = btrfs_find_all_roots(trans, root->fs_info, disko, 3028fcebe456SJosef Bacik tree_mod_seq_elem.seq, &roots); 3029fcebe456SJosef Bacik if (ret < 0) 3030fcebe456SJosef Bacik goto out; 3031fcebe456SJosef Bacik ret = 0; 3032fcebe456SJosef Bacik ULIST_ITER_INIT(&uiter); 3033fcebe456SJosef Bacik while ((root_node = ulist_next(roots, &uiter))) { 3034fcebe456SJosef Bacik if (root_node->val == root->objectid) { 3035fcebe456SJosef Bacik ret = 1; 3036fcebe456SJosef Bacik break; 3037fcebe456SJosef Bacik } 3038fcebe456SJosef Bacik } 3039fcebe456SJosef Bacik ulist_free(roots); 3040fcebe456SJosef Bacik out: 3041fcebe456SJosef Bacik btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3042fcebe456SJosef Bacik return ret; 3043fcebe456SJosef Bacik } 3044fcebe456SJosef Bacik 3045f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3046f82a9901SFilipe Manana struct inode *inode, 3047f82a9901SFilipe Manana u64 endoff, 3048f82a9901SFilipe Manana const u64 destoff, 3049f82a9901SFilipe Manana const u64 olen) 3050f82a9901SFilipe Manana { 3051f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3052f82a9901SFilipe Manana int ret; 3053f82a9901SFilipe Manana 3054f82a9901SFilipe Manana inode_inc_iversion(inode); 3055f82a9901SFilipe Manana inode->i_mtime = inode->i_ctime = CURRENT_TIME; 3056f82a9901SFilipe Manana /* 3057f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3058f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3059f82a9901SFilipe Manana */ 3060f82a9901SFilipe Manana if (endoff > destoff + olen) 3061f82a9901SFilipe Manana endoff = destoff + olen; 3062f82a9901SFilipe Manana if (endoff > inode->i_size) 3063f82a9901SFilipe Manana btrfs_i_size_write(inode, endoff); 3064f82a9901SFilipe Manana 3065f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3066f82a9901SFilipe Manana if (ret) { 3067f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3068f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3069f82a9901SFilipe Manana goto out; 3070f82a9901SFilipe Manana } 3071f82a9901SFilipe Manana ret = btrfs_end_transaction(trans, root); 3072f82a9901SFilipe Manana out: 3073f82a9901SFilipe Manana return ret; 3074f82a9901SFilipe Manana } 3075f82a9901SFilipe Manana 30767ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode, 30777ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 30787ffbb598SFilipe Manana const struct btrfs_path *path, 30797ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 30807ffbb598SFilipe Manana const u64 hole_offset, 30817ffbb598SFilipe Manana const u64 hole_len) 30827ffbb598SFilipe Manana { 30837ffbb598SFilipe Manana struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 30847ffbb598SFilipe Manana struct extent_map *em; 30857ffbb598SFilipe Manana int ret; 30867ffbb598SFilipe Manana 30877ffbb598SFilipe Manana em = alloc_extent_map(); 30887ffbb598SFilipe Manana if (!em) { 30897ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 30907ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 30917ffbb598SFilipe Manana return; 30927ffbb598SFilipe Manana } 30937ffbb598SFilipe Manana 30947ffbb598SFilipe Manana if (fi) { 30957ffbb598SFilipe Manana btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 30967ffbb598SFilipe Manana em->generation = -1; 30977ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 30987ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 30997ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31007ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31017ffbb598SFilipe Manana } else { 31027ffbb598SFilipe Manana em->start = hole_offset; 31037ffbb598SFilipe Manana em->len = hole_len; 31047ffbb598SFilipe Manana em->ram_bytes = em->len; 31057ffbb598SFilipe Manana em->orig_start = hole_offset; 31067ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 31077ffbb598SFilipe Manana em->block_len = 0; 31087ffbb598SFilipe Manana em->orig_block_len = 0; 31097ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 31107ffbb598SFilipe Manana em->generation = trans->transid; 31117ffbb598SFilipe Manana } 31127ffbb598SFilipe Manana 31137ffbb598SFilipe Manana while (1) { 31147ffbb598SFilipe Manana write_lock(&em_tree->lock); 31157ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 31167ffbb598SFilipe Manana write_unlock(&em_tree->lock); 31177ffbb598SFilipe Manana if (ret != -EEXIST) { 31187ffbb598SFilipe Manana free_extent_map(em); 31197ffbb598SFilipe Manana break; 31207ffbb598SFilipe Manana } 31217ffbb598SFilipe Manana btrfs_drop_extent_cache(inode, em->start, 31227ffbb598SFilipe Manana em->start + em->len - 1, 0); 31237ffbb598SFilipe Manana } 31247ffbb598SFilipe Manana 31257ffbb598SFilipe Manana if (unlikely(ret)) 31267ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31277ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31287ffbb598SFilipe Manana } 31297ffbb598SFilipe Manana 313032b7c687SMark Fasheh /** 313132b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 313232b7c687SMark Fasheh * 313332b7c687SMark Fasheh * @src: Inode to clone from 313432b7c687SMark Fasheh * @inode: Inode to clone to 313532b7c687SMark Fasheh * @off: Offset within source to start clone from 313632b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 313732b7c687SMark Fasheh * @olen_aligned: Block-aligned value of olen, extent_same uses 313832b7c687SMark Fasheh * identical values here 313932b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 314032b7c687SMark Fasheh */ 314132b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3142f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 3143f82a9901SFilipe Manana const u64 destoff) 3144f46b5a66SChristoph Hellwig { 3145f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 314632b7c687SMark Fasheh struct btrfs_path *path = NULL; 3147f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 314832b7c687SMark Fasheh struct btrfs_trans_handle *trans; 314932b7c687SMark Fasheh char *buf = NULL; 3150ae01a0abSYan Zheng struct btrfs_key key; 3151f46b5a66SChristoph Hellwig u32 nritems; 3152f46b5a66SChristoph Hellwig int slot; 3153ae01a0abSYan Zheng int ret; 3154fcebe456SJosef Bacik int no_quota; 3155f82a9901SFilipe Manana const u64 len = olen_aligned; 3156fcebe456SJosef Bacik u64 last_disko = 0; 3157f82a9901SFilipe Manana u64 last_dest_end = destoff; 3158ae01a0abSYan Zheng 3159ae01a0abSYan Zheng ret = -ENOMEM; 3160ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 3161ae01a0abSYan Zheng if (!buf) 316232b7c687SMark Fasheh return ret; 3163ae01a0abSYan Zheng 3164ae01a0abSYan Zheng path = btrfs_alloc_path(); 3165ae01a0abSYan Zheng if (!path) { 3166ae01a0abSYan Zheng vfree(buf); 316732b7c687SMark Fasheh return ret; 3168ae01a0abSYan Zheng } 316932b7c687SMark Fasheh 3170ae01a0abSYan Zheng path->reada = 2; 3171c5c9cd4dSSage Weil /* clone data */ 317233345d01SLi Zefan key.objectid = btrfs_ino(src); 3173ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 31742c463823SFilipe Manana key.offset = off; 3175f46b5a66SChristoph Hellwig 3176f46b5a66SChristoph Hellwig while (1) { 3177f46b5a66SChristoph Hellwig /* 3178f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3179f46b5a66SChristoph Hellwig * tree. 3180f46b5a66SChristoph Hellwig */ 3181e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3182362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3183362a20c5SDavid Sterba 0, 0); 3184f46b5a66SChristoph Hellwig if (ret < 0) 3185f46b5a66SChristoph Hellwig goto out; 31862c463823SFilipe Manana /* 31872c463823SFilipe Manana * First search, if no extent item that starts at offset off was 31882c463823SFilipe Manana * found but the previous item is an extent item, it's possible 31892c463823SFilipe Manana * it might overlap our target range, therefore process it. 31902c463823SFilipe Manana */ 31912c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 31922c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 31932c463823SFilipe Manana path->slots[0] - 1); 31942c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 31952c463823SFilipe Manana path->slots[0]--; 31962c463823SFilipe Manana } 3197f46b5a66SChristoph Hellwig 3198ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3199e4355f34SFilipe David Borba Manana process_slot: 3200fcebe456SJosef Bacik no_quota = 1; 3201ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3202362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3203f46b5a66SChristoph Hellwig if (ret < 0) 3204f46b5a66SChristoph Hellwig goto out; 3205f46b5a66SChristoph Hellwig if (ret > 0) 3206f46b5a66SChristoph Hellwig break; 3207ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3208f46b5a66SChristoph Hellwig } 3209f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3210f46b5a66SChristoph Hellwig slot = path->slots[0]; 3211f46b5a66SChristoph Hellwig 3212ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3213d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 321433345d01SLi Zefan key.objectid != btrfs_ino(src)) 3215f46b5a66SChristoph Hellwig break; 3216f46b5a66SChristoph Hellwig 3217c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 3218c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3219c5c9cd4dSSage Weil int type; 322031840ae1SZheng Yan u32 size; 322131840ae1SZheng Yan struct btrfs_key new_key; 3222c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3223c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3224c5c9cd4dSSage Weil u8 comp; 3225f82a9901SFilipe Manana u64 drop_start; 322631840ae1SZheng Yan 3227c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3228c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3229c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3230c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3231c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3232c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3233d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3234d397712bSChris Mason extent); 3235d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3236d397712bSChris Mason extent); 3237c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3238d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3239d397712bSChris Mason extent); 3240c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3241c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3242c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3243c5c9cd4dSSage Weil extent); 3244c5c9cd4dSSage Weil } 324531840ae1SZheng Yan 32462c463823SFilipe Manana /* 32472c463823SFilipe Manana * The first search might have left us at an extent 32482c463823SFilipe Manana * item that ends before our target range's start, can 32492c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 32502c463823SFilipe Manana */ 32512c463823SFilipe Manana if (key.offset + datal <= off) { 3252e4355f34SFilipe David Borba Manana path->slots[0]++; 3253e4355f34SFilipe David Borba Manana goto process_slot; 32542c463823SFilipe Manana } else if (key.offset >= off + len) { 32552c463823SFilipe Manana break; 3256e4355f34SFilipe David Borba Manana } 3257e4355f34SFilipe David Borba Manana 3258e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3259e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3260e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3261e4355f34SFilipe David Borba Manana size); 3262e4355f34SFilipe David Borba Manana 3263e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3264e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3265c5c9cd4dSSage Weil 326631840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 326733345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 32684d728ec7SLi Zefan if (off <= key.offset) 3269c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 32704d728ec7SLi Zefan else 32714d728ec7SLi Zefan new_key.offset = destoff; 3272c5c9cd4dSSage Weil 3273b6f3409bSSage Weil /* 3274f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3275f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3276f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3277f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3278f82a9901SFilipe Manana * partially overlaps it). 3279f82a9901SFilipe Manana */ 3280f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3281f82a9901SFilipe Manana drop_start = last_dest_end; 3282f82a9901SFilipe Manana else 3283f82a9901SFilipe Manana drop_start = new_key.offset; 3284f82a9901SFilipe Manana 3285f82a9901SFilipe Manana /* 3286b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3287b6f3409bSSage Weil * 1 - add new extent 3288b6f3409bSSage Weil * 1 - inode update 3289b6f3409bSSage Weil */ 3290b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3291a22285a6SYan, Zheng if (IS_ERR(trans)) { 3292a22285a6SYan, Zheng ret = PTR_ERR(trans); 3293a22285a6SYan, Zheng goto out; 3294a22285a6SYan, Zheng } 3295a22285a6SYan, Zheng 3296c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3297c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3298d72c0842SLi Zefan /* 3299d72c0842SLi Zefan * a | --- range to clone ---| b 3300d72c0842SLi Zefan * | ------------- extent ------------- | 3301d72c0842SLi Zefan */ 3302d72c0842SLi Zefan 330393915584SAntonio Ospite /* subtract range b */ 3304d72c0842SLi Zefan if (key.offset + datal > off + len) 3305d72c0842SLi Zefan datal = off + len - key.offset; 3306d72c0842SLi Zefan 330793915584SAntonio Ospite /* subtract range a */ 3308a22285a6SYan, Zheng if (off > key.offset) { 3309a22285a6SYan, Zheng datao += off - key.offset; 3310a22285a6SYan, Zheng datal -= off - key.offset; 3311a22285a6SYan, Zheng } 3312a22285a6SYan, Zheng 33135dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3314f82a9901SFilipe Manana drop_start, 3315a22285a6SYan, Zheng new_key.offset + datal, 33162671485dSJosef Bacik 1); 331779787eaaSJeff Mahoney if (ret) { 33183f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 331900fdf13aSLiu Bo btrfs_abort_transaction(trans, 332000fdf13aSLiu Bo root, ret); 332179787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 332279787eaaSJeff Mahoney goto out; 332379787eaaSJeff Mahoney } 3324a22285a6SYan, Zheng 332531840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 332631840ae1SZheng Yan &new_key, size); 332779787eaaSJeff Mahoney if (ret) { 332879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 332979787eaaSJeff Mahoney ret); 333079787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 333179787eaaSJeff Mahoney goto out; 333279787eaaSJeff Mahoney } 333331840ae1SZheng Yan 333431840ae1SZheng Yan leaf = path->nodes[0]; 333531840ae1SZheng Yan slot = path->slots[0]; 333631840ae1SZheng Yan write_extent_buffer(leaf, buf, 333731840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 333831840ae1SZheng Yan size); 3339ae01a0abSYan Zheng 3340f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3341f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3342c5c9cd4dSSage Weil 3343c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3344c5c9cd4dSSage Weil if (!disko) 3345c5c9cd4dSSage Weil datao = 0; 3346c5c9cd4dSSage Weil 3347c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3348c5c9cd4dSSage Weil datao); 3349c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3350c5c9cd4dSSage Weil datal); 3351fcebe456SJosef Bacik 3352fcebe456SJosef Bacik /* 3353fcebe456SJosef Bacik * We need to look up the roots that point at 3354fcebe456SJosef Bacik * this bytenr and see if the new root does. If 3355fcebe456SJosef Bacik * it does not we need to make sure we update 3356fcebe456SJosef Bacik * quotas appropriately. 3357fcebe456SJosef Bacik */ 3358fcebe456SJosef Bacik if (disko && root != BTRFS_I(src)->root && 3359fcebe456SJosef Bacik disko != last_disko) { 3360fcebe456SJosef Bacik no_quota = check_ref(trans, root, 3361fcebe456SJosef Bacik disko); 3362fcebe456SJosef Bacik if (no_quota < 0) { 3363fcebe456SJosef Bacik btrfs_abort_transaction(trans, 3364fcebe456SJosef Bacik root, 3365fcebe456SJosef Bacik ret); 3366fcebe456SJosef Bacik btrfs_end_transaction(trans, 3367fcebe456SJosef Bacik root); 3368fcebe456SJosef Bacik ret = no_quota; 3369fcebe456SJosef Bacik goto out; 3370fcebe456SJosef Bacik } 3371fcebe456SJosef Bacik } 3372fcebe456SJosef Bacik 3373c5c9cd4dSSage Weil if (disko) { 3374c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3375ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 33765d4f98a2SYan Zheng disko, diskl, 0, 3377f46b5a66SChristoph Hellwig root->root_key.objectid, 337833345d01SLi Zefan btrfs_ino(inode), 337966d7e7f0SArne Jansen new_key.offset - datao, 3380fcebe456SJosef Bacik no_quota); 338179787eaaSJeff Mahoney if (ret) { 338279787eaaSJeff Mahoney btrfs_abort_transaction(trans, 338379787eaaSJeff Mahoney root, 338479787eaaSJeff Mahoney ret); 338579787eaaSJeff Mahoney btrfs_end_transaction(trans, 338679787eaaSJeff Mahoney root); 338779787eaaSJeff Mahoney goto out; 338879787eaaSJeff Mahoney 338979787eaaSJeff Mahoney } 3390f46b5a66SChristoph Hellwig } 3391c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3392c5c9cd4dSSage Weil u64 skip = 0; 3393c5c9cd4dSSage Weil u64 trim = 0; 3394d3ecfcdfSLiu Bo u64 aligned_end = 0; 3395d3ecfcdfSLiu Bo 3396c5c9cd4dSSage Weil if (off > key.offset) { 3397c5c9cd4dSSage Weil skip = off - key.offset; 3398c5c9cd4dSSage Weil new_key.offset += skip; 339931840ae1SZheng Yan } 3400d397712bSChris Mason 3401c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3402c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3403d397712bSChris Mason 3404c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3405c5c9cd4dSSage Weil ret = -EINVAL; 3406a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 3407c5c9cd4dSSage Weil goto out; 340831840ae1SZheng Yan } 3409c5c9cd4dSSage Weil size -= skip + trim; 3410c5c9cd4dSSage Weil datal -= skip + trim; 3411a22285a6SYan, Zheng 3412d3ecfcdfSLiu Bo aligned_end = ALIGN(new_key.offset + datal, 3413d3ecfcdfSLiu Bo root->sectorsize); 34145dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3415f82a9901SFilipe Manana drop_start, 3416d3ecfcdfSLiu Bo aligned_end, 34172671485dSJosef Bacik 1); 341879787eaaSJeff Mahoney if (ret) { 34193f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 34203a29bc09SChris Mason btrfs_abort_transaction(trans, 34213a29bc09SChris Mason root, ret); 342279787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 342379787eaaSJeff Mahoney goto out; 342479787eaaSJeff Mahoney } 3425a22285a6SYan, Zheng 3426c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 3427c5c9cd4dSSage Weil &new_key, size); 342879787eaaSJeff Mahoney if (ret) { 342979787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 343079787eaaSJeff Mahoney ret); 343179787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 343279787eaaSJeff Mahoney goto out; 343379787eaaSJeff Mahoney } 3434c5c9cd4dSSage Weil 3435c5c9cd4dSSage Weil if (skip) { 3436d397712bSChris Mason u32 start = 3437d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 3438c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 3439c5c9cd4dSSage Weil datal); 3440c5c9cd4dSSage Weil } 3441c5c9cd4dSSage Weil 3442c5c9cd4dSSage Weil leaf = path->nodes[0]; 3443c5c9cd4dSSage Weil slot = path->slots[0]; 3444c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 3445c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 3446c5c9cd4dSSage Weil size); 3447c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 34487ffbb598SFilipe Manana extent = btrfs_item_ptr(leaf, slot, 34497ffbb598SFilipe Manana struct btrfs_file_extent_item); 3450c5c9cd4dSSage Weil } 3451c5c9cd4dSSage Weil 34527ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 34537ffbb598SFilipe Manana if (drop_start < new_key.offset) 34547ffbb598SFilipe Manana clone_update_extent_map(inode, trans, 34557ffbb598SFilipe Manana path, NULL, drop_start, 34567ffbb598SFilipe Manana new_key.offset - drop_start); 34577ffbb598SFilipe Manana 34587ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, 34597ffbb598SFilipe Manana extent, 0, 0); 34607ffbb598SFilipe Manana 3461c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3462b3b4aa74SDavid Sterba btrfs_release_path(path); 3463c5c9cd4dSSage Weil 3464f82a9901SFilipe Manana last_dest_end = new_key.offset + datal; 3465f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3466f82a9901SFilipe Manana last_dest_end, 3467f82a9901SFilipe Manana destoff, olen); 3468f82a9901SFilipe Manana if (ret) 346979787eaaSJeff Mahoney goto out; 34702c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 34712c463823SFilipe Manana break; 3472a22285a6SYan, Zheng } 3473b3b4aa74SDavid Sterba btrfs_release_path(path); 3474ae01a0abSYan Zheng key.offset++; 3475ae01a0abSYan Zheng } 3476f46b5a66SChristoph Hellwig ret = 0; 347732b7c687SMark Fasheh 3478f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3479f82a9901SFilipe Manana /* 3480f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3481f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3482f82a9901SFilipe Manana */ 3483f82a9901SFilipe Manana btrfs_release_path(path); 3484f82a9901SFilipe Manana 3485f82a9901SFilipe Manana /* 3486f82a9901SFilipe Manana * 1 - remove extent(s) 3487f82a9901SFilipe Manana * 1 - inode update 3488f82a9901SFilipe Manana */ 3489f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3490f82a9901SFilipe Manana if (IS_ERR(trans)) { 3491f82a9901SFilipe Manana ret = PTR_ERR(trans); 3492f82a9901SFilipe Manana goto out; 3493f82a9901SFilipe Manana } 3494f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3495f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3496f82a9901SFilipe Manana if (ret) { 3497f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 3498f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3499f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3500f82a9901SFilipe Manana goto out; 3501f82a9901SFilipe Manana } 3502f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 3503f82a9901SFilipe Manana destoff, olen); 35047ffbb598SFilipe Manana if (ret) 35057ffbb598SFilipe Manana goto out; 35067ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, NULL, last_dest_end, 35077ffbb598SFilipe Manana destoff + len - last_dest_end); 3508f82a9901SFilipe Manana } 3509f82a9901SFilipe Manana 3510f46b5a66SChristoph Hellwig out: 351132b7c687SMark Fasheh btrfs_free_path(path); 351232b7c687SMark Fasheh vfree(buf); 351332b7c687SMark Fasheh return ret; 351432b7c687SMark Fasheh } 351532b7c687SMark Fasheh 351632b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 351732b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 351832b7c687SMark Fasheh { 351954563d41SAl Viro struct inode *inode = file_inode(file); 352032b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 352132b7c687SMark Fasheh struct fd src_file; 352232b7c687SMark Fasheh struct inode *src; 352332b7c687SMark Fasheh int ret; 352432b7c687SMark Fasheh u64 len = olen; 352532b7c687SMark Fasheh u64 bs = root->fs_info->sb->s_blocksize; 352632b7c687SMark Fasheh int same_inode = 0; 352732b7c687SMark Fasheh 352832b7c687SMark Fasheh /* 352932b7c687SMark Fasheh * TODO: 353032b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 353132b7c687SMark Fasheh * decompress into destination's address_space (the file offset 353232b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 353332b7c687SMark Fasheh * otherwise reinsert) a subrange. 353400fdf13aSLiu Bo * 353500fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 353600fdf13aSLiu Bo * be either compressed or non-compressed. 353732b7c687SMark Fasheh */ 353832b7c687SMark Fasheh 353932b7c687SMark Fasheh /* the destination must be opened for writing */ 354032b7c687SMark Fasheh if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 354132b7c687SMark Fasheh return -EINVAL; 354232b7c687SMark Fasheh 354332b7c687SMark Fasheh if (btrfs_root_readonly(root)) 354432b7c687SMark Fasheh return -EROFS; 354532b7c687SMark Fasheh 354632b7c687SMark Fasheh ret = mnt_want_write_file(file); 354732b7c687SMark Fasheh if (ret) 354832b7c687SMark Fasheh return ret; 354932b7c687SMark Fasheh 355032b7c687SMark Fasheh src_file = fdget(srcfd); 355132b7c687SMark Fasheh if (!src_file.file) { 355232b7c687SMark Fasheh ret = -EBADF; 355332b7c687SMark Fasheh goto out_drop_write; 355432b7c687SMark Fasheh } 355532b7c687SMark Fasheh 355632b7c687SMark Fasheh ret = -EXDEV; 355732b7c687SMark Fasheh if (src_file.file->f_path.mnt != file->f_path.mnt) 355832b7c687SMark Fasheh goto out_fput; 355932b7c687SMark Fasheh 356032b7c687SMark Fasheh src = file_inode(src_file.file); 356132b7c687SMark Fasheh 356232b7c687SMark Fasheh ret = -EINVAL; 356332b7c687SMark Fasheh if (src == inode) 356432b7c687SMark Fasheh same_inode = 1; 356532b7c687SMark Fasheh 356632b7c687SMark Fasheh /* the src must be open for reading */ 356732b7c687SMark Fasheh if (!(src_file.file->f_mode & FMODE_READ)) 356832b7c687SMark Fasheh goto out_fput; 356932b7c687SMark Fasheh 357032b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 357132b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 357232b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 357332b7c687SMark Fasheh goto out_fput; 357432b7c687SMark Fasheh 357532b7c687SMark Fasheh ret = -EISDIR; 357632b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 357732b7c687SMark Fasheh goto out_fput; 357832b7c687SMark Fasheh 357932b7c687SMark Fasheh ret = -EXDEV; 358032b7c687SMark Fasheh if (src->i_sb != inode->i_sb) 358132b7c687SMark Fasheh goto out_fput; 358232b7c687SMark Fasheh 358332b7c687SMark Fasheh if (!same_inode) { 358432b7c687SMark Fasheh if (inode < src) { 358532b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 358632b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 358732b7c687SMark Fasheh } else { 358832b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 358932b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 359032b7c687SMark Fasheh } 359132b7c687SMark Fasheh } else { 359232b7c687SMark Fasheh mutex_lock(&src->i_mutex); 359332b7c687SMark Fasheh } 359432b7c687SMark Fasheh 359532b7c687SMark Fasheh /* determine range to clone */ 359632b7c687SMark Fasheh ret = -EINVAL; 359732b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 359832b7c687SMark Fasheh goto out_unlock; 359932b7c687SMark Fasheh if (len == 0) 360032b7c687SMark Fasheh olen = len = src->i_size - off; 360132b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 360232b7c687SMark Fasheh if (off + len == src->i_size) 360332b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 360432b7c687SMark Fasheh 360532b7c687SMark Fasheh /* verify the end result is block aligned */ 360632b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 360732b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 360832b7c687SMark Fasheh goto out_unlock; 360932b7c687SMark Fasheh 361032b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 361132b7c687SMark Fasheh if (same_inode) { 361232b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 361332b7c687SMark Fasheh goto out_unlock; 361432b7c687SMark Fasheh } 361532b7c687SMark Fasheh 361632b7c687SMark Fasheh if (destoff > inode->i_size) { 361732b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 361832b7c687SMark Fasheh if (ret) 361932b7c687SMark Fasheh goto out_unlock; 362032b7c687SMark Fasheh } 362132b7c687SMark Fasheh 3622c125b8bfSFilipe Manana /* 3623c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3624c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3625c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3626c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3627c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3628c125b8bfSFilipe Manana */ 3629c125b8bfSFilipe Manana if (same_inode) { 3630c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3631c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 363232b7c687SMark Fasheh 3633c125b8bfSFilipe Manana lock_extent_range(src, lock_start, lock_len); 3634c125b8bfSFilipe Manana } else { 363532b7c687SMark Fasheh lock_extent_range(src, off, len); 3636c125b8bfSFilipe Manana lock_extent_range(inode, destoff, len); 3637c125b8bfSFilipe Manana } 363832b7c687SMark Fasheh 363932b7c687SMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff); 364032b7c687SMark Fasheh 3641c125b8bfSFilipe Manana if (same_inode) { 3642c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3643c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3644c125b8bfSFilipe Manana 3645c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3646c125b8bfSFilipe Manana } else { 3647aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 3648c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(inode)->io_tree, destoff, 3649c125b8bfSFilipe Manana destoff + len - 1); 3650c125b8bfSFilipe Manana } 3651c125b8bfSFilipe Manana /* 3652c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3653c125b8bfSFilipe Manana * data immediately and not the previous data. 3654c125b8bfSFilipe Manana */ 3655c125b8bfSFilipe Manana truncate_inode_pages_range(&inode->i_data, destoff, 3656c125b8bfSFilipe Manana PAGE_CACHE_ALIGN(destoff + len) - 1); 3657f46b5a66SChristoph Hellwig out_unlock: 3658c57c2b3eSFilipe David Borba Manana if (!same_inode) { 3659c57c2b3eSFilipe David Borba Manana if (inode < src) { 3660f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 3661f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 3662c57c2b3eSFilipe David Borba Manana } else { 3663c57c2b3eSFilipe David Borba Manana mutex_unlock(&inode->i_mutex); 3664c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3665c57c2b3eSFilipe David Borba Manana } 3666c57c2b3eSFilipe David Borba Manana } else { 3667c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3668c57c2b3eSFilipe David Borba Manana } 3669f46b5a66SChristoph Hellwig out_fput: 36702903ff01SAl Viro fdput(src_file); 3671ab67b7c1SYan Zheng out_drop_write: 36722a79f17eSAl Viro mnt_drop_write_file(file); 3673f46b5a66SChristoph Hellwig return ret; 3674f46b5a66SChristoph Hellwig } 3675f46b5a66SChristoph Hellwig 36767a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 3677c5c9cd4dSSage Weil { 3678c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 3679c5c9cd4dSSage Weil 36807a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 3681c5c9cd4dSSage Weil return -EFAULT; 3682c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 3683c5c9cd4dSSage Weil args.src_length, args.dest_offset); 3684c5c9cd4dSSage Weil } 3685c5c9cd4dSSage Weil 3686f46b5a66SChristoph Hellwig /* 3687f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3688f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3689f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3690f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3691f46b5a66SChristoph Hellwig */ 3692b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3693f46b5a66SChristoph Hellwig { 3694496ad9aaSAl Viro struct inode *inode = file_inode(file); 3695f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3696f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 36971ab86aedSSage Weil int ret; 3698f46b5a66SChristoph Hellwig 36991ab86aedSSage Weil ret = -EPERM; 3700df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3701f46b5a66SChristoph Hellwig goto out; 37021ab86aedSSage Weil 37031ab86aedSSage Weil ret = -EINPROGRESS; 37041ab86aedSSage Weil if (file->private_data) 37051ab86aedSSage Weil goto out; 37069ca9ee09SSage Weil 3707b83cc969SLi Zefan ret = -EROFS; 3708b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3709b83cc969SLi Zefan goto out; 3710b83cc969SLi Zefan 3711a561be71SAl Viro ret = mnt_want_write_file(file); 3712c146afadSYan Zheng if (ret) 3713c146afadSYan Zheng goto out; 3714c146afadSYan Zheng 3715a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 37169ca9ee09SSage Weil 3717f46b5a66SChristoph Hellwig ret = -ENOMEM; 37187a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3719abd30bb0STsutomu Itoh if (IS_ERR(trans)) 37201ab86aedSSage Weil goto out_drop; 37211ab86aedSSage Weil 37221ab86aedSSage Weil file->private_data = trans; 37231ab86aedSSage Weil return 0; 37241ab86aedSSage Weil 37251ab86aedSSage Weil out_drop: 3726a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 37272a79f17eSAl Viro mnt_drop_write_file(file); 3728f46b5a66SChristoph Hellwig out: 3729f46b5a66SChristoph Hellwig return ret; 3730f46b5a66SChristoph Hellwig } 3731f46b5a66SChristoph Hellwig 37326ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 37336ef5ed0dSJosef Bacik { 3734496ad9aaSAl Viro struct inode *inode = file_inode(file); 37356ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 37366ef5ed0dSJosef Bacik struct btrfs_root *new_root; 37376ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 37386ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 37396ef5ed0dSJosef Bacik struct btrfs_path *path; 37406ef5ed0dSJosef Bacik struct btrfs_key location; 37416ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 37426ef5ed0dSJosef Bacik u64 objectid = 0; 37436ef5ed0dSJosef Bacik u64 dir_id; 37443c04ce01SMiao Xie int ret; 37456ef5ed0dSJosef Bacik 37466ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 37476ef5ed0dSJosef Bacik return -EPERM; 37486ef5ed0dSJosef Bacik 37493c04ce01SMiao Xie ret = mnt_want_write_file(file); 37503c04ce01SMiao Xie if (ret) 37513c04ce01SMiao Xie return ret; 37523c04ce01SMiao Xie 37533c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 37543c04ce01SMiao Xie ret = -EFAULT; 37553c04ce01SMiao Xie goto out; 37563c04ce01SMiao Xie } 37576ef5ed0dSJosef Bacik 37586ef5ed0dSJosef Bacik if (!objectid) 37591cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 37606ef5ed0dSJosef Bacik 37616ef5ed0dSJosef Bacik location.objectid = objectid; 37626ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 37636ef5ed0dSJosef Bacik location.offset = (u64)-1; 37646ef5ed0dSJosef Bacik 37656ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 37663c04ce01SMiao Xie if (IS_ERR(new_root)) { 37673c04ce01SMiao Xie ret = PTR_ERR(new_root); 37683c04ce01SMiao Xie goto out; 37693c04ce01SMiao Xie } 37706ef5ed0dSJosef Bacik 37716ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 37723c04ce01SMiao Xie if (!path) { 37733c04ce01SMiao Xie ret = -ENOMEM; 37743c04ce01SMiao Xie goto out; 37753c04ce01SMiao Xie } 37766ef5ed0dSJosef Bacik path->leave_spinning = 1; 37776ef5ed0dSJosef Bacik 37786ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 377998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 37806ef5ed0dSJosef Bacik btrfs_free_path(path); 37813c04ce01SMiao Xie ret = PTR_ERR(trans); 37823c04ce01SMiao Xie goto out; 37836ef5ed0dSJosef Bacik } 37846ef5ed0dSJosef Bacik 37856c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 37866ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 37876ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3788cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 37896ef5ed0dSJosef Bacik btrfs_free_path(path); 37906ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 3791efe120a0SFrank Holton btrfs_err(new_root->fs_info, "Umm, you don't have the default dir" 3792efe120a0SFrank Holton "item, this isn't going to work"); 37933c04ce01SMiao Xie ret = -ENOENT; 37943c04ce01SMiao Xie goto out; 37956ef5ed0dSJosef Bacik } 37966ef5ed0dSJosef Bacik 37976ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 37986ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 37996ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 38006ef5ed0dSJosef Bacik btrfs_free_path(path); 38016ef5ed0dSJosef Bacik 38022b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 38036ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 38043c04ce01SMiao Xie out: 38053c04ce01SMiao Xie mnt_drop_write_file(file); 38063c04ce01SMiao Xie return ret; 38076ef5ed0dSJosef Bacik } 38086ef5ed0dSJosef Bacik 38095af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 3810bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3811bf5fc093SJosef Bacik { 3812bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 3813bf5fc093SJosef Bacik 3814bf5fc093SJosef Bacik space->total_bytes = 0; 3815bf5fc093SJosef Bacik space->used_bytes = 0; 3816bf5fc093SJosef Bacik space->flags = 0; 3817bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3818bf5fc093SJosef Bacik space->flags = block_group->flags; 3819bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 3820bf5fc093SJosef Bacik space->used_bytes += 3821bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 3822bf5fc093SJosef Bacik } 3823bf5fc093SJosef Bacik } 3824bf5fc093SJosef Bacik 382548a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 38261406e432SJosef Bacik { 38271406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 38281406e432SJosef Bacik struct btrfs_ioctl_space_info space; 38291406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 38307fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 383113f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 38321406e432SJosef Bacik struct btrfs_space_info *info; 3833bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 3834bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3835bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3836bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 3837bf5fc093SJosef Bacik int num_types = 4; 38387fde62bfSChris Mason int alloc_size; 38391406e432SJosef Bacik int ret = 0; 384051788b1bSDan Rosenberg u64 slot_count = 0; 3841bf5fc093SJosef Bacik int i, c; 38421406e432SJosef Bacik 38431406e432SJosef Bacik if (copy_from_user(&space_args, 38441406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 38451406e432SJosef Bacik sizeof(space_args))) 38461406e432SJosef Bacik return -EFAULT; 38471406e432SJosef Bacik 3848bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3849bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3850bf5fc093SJosef Bacik 3851bf5fc093SJosef Bacik info = NULL; 38527fde62bfSChris Mason rcu_read_lock(); 3853bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3854bf5fc093SJosef Bacik list) { 3855bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3856bf5fc093SJosef Bacik info = tmp; 3857bf5fc093SJosef Bacik break; 3858bf5fc093SJosef Bacik } 3859bf5fc093SJosef Bacik } 38607fde62bfSChris Mason rcu_read_unlock(); 38611406e432SJosef Bacik 3862bf5fc093SJosef Bacik if (!info) 3863bf5fc093SJosef Bacik continue; 3864bf5fc093SJosef Bacik 3865bf5fc093SJosef Bacik down_read(&info->groups_sem); 3866bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3867bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3868bf5fc093SJosef Bacik slot_count++; 3869bf5fc093SJosef Bacik } 3870bf5fc093SJosef Bacik up_read(&info->groups_sem); 3871bf5fc093SJosef Bacik } 3872bf5fc093SJosef Bacik 387336523e95SDavid Sterba /* 387436523e95SDavid Sterba * Global block reserve, exported as a space_info 387536523e95SDavid Sterba */ 387636523e95SDavid Sterba slot_count++; 387736523e95SDavid Sterba 38787fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 38797fde62bfSChris Mason if (space_args.space_slots == 0) { 38807fde62bfSChris Mason space_args.total_spaces = slot_count; 38817fde62bfSChris Mason goto out; 38827fde62bfSChris Mason } 3883bf5fc093SJosef Bacik 388451788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3885bf5fc093SJosef Bacik 38867fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3887bf5fc093SJosef Bacik 38887fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 38897fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 38907fde62bfSChris Mason */ 38917fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 38927fde62bfSChris Mason return -ENOMEM; 38937fde62bfSChris Mason 38947fde62bfSChris Mason space_args.total_spaces = 0; 38957fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 38967fde62bfSChris Mason if (!dest) 38977fde62bfSChris Mason return -ENOMEM; 38987fde62bfSChris Mason dest_orig = dest; 38997fde62bfSChris Mason 39007fde62bfSChris Mason /* now we have a buffer to copy into */ 3901bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3902bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3903bf5fc093SJosef Bacik 390451788b1bSDan Rosenberg if (!slot_count) 390551788b1bSDan Rosenberg break; 390651788b1bSDan Rosenberg 3907bf5fc093SJosef Bacik info = NULL; 39081406e432SJosef Bacik rcu_read_lock(); 3909bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3910bf5fc093SJosef Bacik list) { 3911bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3912bf5fc093SJosef Bacik info = tmp; 39137fde62bfSChris Mason break; 3914bf5fc093SJosef Bacik } 3915bf5fc093SJosef Bacik } 3916bf5fc093SJosef Bacik rcu_read_unlock(); 39177fde62bfSChris Mason 3918bf5fc093SJosef Bacik if (!info) 3919bf5fc093SJosef Bacik continue; 3920bf5fc093SJosef Bacik down_read(&info->groups_sem); 3921bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3922bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 39235af3e8ccSStefan Behrens btrfs_get_block_group_info( 39245af3e8ccSStefan Behrens &info->block_groups[c], &space); 39257fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 39261406e432SJosef Bacik dest++; 39271406e432SJosef Bacik space_args.total_spaces++; 392851788b1bSDan Rosenberg slot_count--; 39291406e432SJosef Bacik } 393051788b1bSDan Rosenberg if (!slot_count) 393151788b1bSDan Rosenberg break; 3932bf5fc093SJosef Bacik } 3933bf5fc093SJosef Bacik up_read(&info->groups_sem); 3934bf5fc093SJosef Bacik } 39351406e432SJosef Bacik 393636523e95SDavid Sterba /* 393736523e95SDavid Sterba * Add global block reserve 393836523e95SDavid Sterba */ 393936523e95SDavid Sterba if (slot_count) { 394036523e95SDavid Sterba struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 394136523e95SDavid Sterba 394236523e95SDavid Sterba spin_lock(&block_rsv->lock); 394336523e95SDavid Sterba space.total_bytes = block_rsv->size; 394436523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 394536523e95SDavid Sterba spin_unlock(&block_rsv->lock); 394636523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 394736523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 394836523e95SDavid Sterba space_args.total_spaces++; 394936523e95SDavid Sterba } 395036523e95SDavid Sterba 39512eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 39527fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 39537fde62bfSChris Mason 39547fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 39557fde62bfSChris Mason ret = -EFAULT; 39567fde62bfSChris Mason 39577fde62bfSChris Mason kfree(dest_orig); 39587fde62bfSChris Mason out: 39597fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 39601406e432SJosef Bacik ret = -EFAULT; 39611406e432SJosef Bacik 39621406e432SJosef Bacik return ret; 39631406e432SJosef Bacik } 39641406e432SJosef Bacik 3965f46b5a66SChristoph Hellwig /* 3966f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3967f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3968f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3969f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3970f46b5a66SChristoph Hellwig */ 3971f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3972f46b5a66SChristoph Hellwig { 3973496ad9aaSAl Viro struct inode *inode = file_inode(file); 3974f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3975f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3976f46b5a66SChristoph Hellwig 3977f46b5a66SChristoph Hellwig trans = file->private_data; 39781ab86aedSSage Weil if (!trans) 39791ab86aedSSage Weil return -EINVAL; 3980b214107eSChristoph Hellwig file->private_data = NULL; 39819ca9ee09SSage Weil 39821ab86aedSSage Weil btrfs_end_transaction(trans, root); 39831ab86aedSSage Weil 3984a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 39859ca9ee09SSage Weil 39862a79f17eSAl Viro mnt_drop_write_file(file); 39871ab86aedSSage Weil return 0; 3988f46b5a66SChristoph Hellwig } 3989f46b5a66SChristoph Hellwig 39909a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 39919a8c28beSMiao Xie void __user *argp) 399246204592SSage Weil { 399346204592SSage Weil struct btrfs_trans_handle *trans; 399446204592SSage Weil u64 transid; 3995db5b493aSTsutomu Itoh int ret; 399646204592SSage Weil 3997d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3998ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3999ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 400098d5dc13STsutomu Itoh return PTR_ERR(trans); 4001ff7c1d33SMiao Xie 4002ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4003ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4004ff7c1d33SMiao Xie goto out; 4005ff7c1d33SMiao Xie } 400646204592SSage Weil transid = trans->transid; 4007db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 40088b2b2d3cSTsutomu Itoh if (ret) { 40098b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 4010db5b493aSTsutomu Itoh return ret; 40118b2b2d3cSTsutomu Itoh } 4012ff7c1d33SMiao Xie out: 401346204592SSage Weil if (argp) 401446204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 401546204592SSage Weil return -EFAULT; 401646204592SSage Weil return 0; 401746204592SSage Weil } 401846204592SSage Weil 40199a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 40209a8c28beSMiao Xie void __user *argp) 402146204592SSage Weil { 402246204592SSage Weil u64 transid; 402346204592SSage Weil 402446204592SSage Weil if (argp) { 402546204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 402646204592SSage Weil return -EFAULT; 402746204592SSage Weil } else { 402846204592SSage Weil transid = 0; /* current trans */ 402946204592SSage Weil } 403046204592SSage Weil return btrfs_wait_for_commit(root, transid); 403146204592SSage Weil } 403246204592SSage Weil 4033b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4034475f6387SJan Schmidt { 4035496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4036475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4037b8e95489SMiao Xie int ret; 4038475f6387SJan Schmidt 4039475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4040475f6387SJan Schmidt return -EPERM; 4041475f6387SJan Schmidt 4042475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4043475f6387SJan Schmidt if (IS_ERR(sa)) 4044475f6387SJan Schmidt return PTR_ERR(sa); 4045475f6387SJan Schmidt 4046b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4047b8e95489SMiao Xie ret = mnt_want_write_file(file); 4048b8e95489SMiao Xie if (ret) 4049b8e95489SMiao Xie goto out; 4050b8e95489SMiao Xie } 4051b8e95489SMiao Xie 4052aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 405363a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 405463a212abSStefan Behrens 0); 4055475f6387SJan Schmidt 4056475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4057475f6387SJan Schmidt ret = -EFAULT; 4058475f6387SJan Schmidt 4059b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4060b8e95489SMiao Xie mnt_drop_write_file(file); 4061b8e95489SMiao Xie out: 4062475f6387SJan Schmidt kfree(sa); 4063475f6387SJan Schmidt return ret; 4064475f6387SJan Schmidt } 4065475f6387SJan Schmidt 4066475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 4067475f6387SJan Schmidt { 4068475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4069475f6387SJan Schmidt return -EPERM; 4070475f6387SJan Schmidt 4071aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 4072475f6387SJan Schmidt } 4073475f6387SJan Schmidt 4074475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 4075475f6387SJan Schmidt void __user *arg) 4076475f6387SJan Schmidt { 4077475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4078475f6387SJan Schmidt int ret; 4079475f6387SJan Schmidt 4080475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4081475f6387SJan Schmidt return -EPERM; 4082475f6387SJan Schmidt 4083475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4084475f6387SJan Schmidt if (IS_ERR(sa)) 4085475f6387SJan Schmidt return PTR_ERR(sa); 4086475f6387SJan Schmidt 4087475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 4088475f6387SJan Schmidt 4089475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4090475f6387SJan Schmidt ret = -EFAULT; 4091475f6387SJan Schmidt 4092475f6387SJan Schmidt kfree(sa); 4093475f6387SJan Schmidt return ret; 4094475f6387SJan Schmidt } 4095475f6387SJan Schmidt 4096c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 4097b27f7c0cSDavid Sterba void __user *arg) 4098c11d2c23SStefan Behrens { 4099c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4100c11d2c23SStefan Behrens int ret; 4101c11d2c23SStefan Behrens 4102c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4103c11d2c23SStefan Behrens if (IS_ERR(sa)) 4104c11d2c23SStefan Behrens return PTR_ERR(sa); 4105c11d2c23SStefan Behrens 4106b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4107b27f7c0cSDavid Sterba kfree(sa); 4108b27f7c0cSDavid Sterba return -EPERM; 4109b27f7c0cSDavid Sterba } 4110b27f7c0cSDavid Sterba 4111b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 4112c11d2c23SStefan Behrens 4113c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4114c11d2c23SStefan Behrens ret = -EFAULT; 4115c11d2c23SStefan Behrens 4116c11d2c23SStefan Behrens kfree(sa); 4117c11d2c23SStefan Behrens return ret; 4118c11d2c23SStefan Behrens } 4119c11d2c23SStefan Behrens 41203f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 41213f6bcfbdSStefan Behrens { 41223f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 41233f6bcfbdSStefan Behrens int ret; 41243f6bcfbdSStefan Behrens 41253f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 41263f6bcfbdSStefan Behrens return -EPERM; 41273f6bcfbdSStefan Behrens 41283f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 41293f6bcfbdSStefan Behrens if (IS_ERR(p)) 41303f6bcfbdSStefan Behrens return PTR_ERR(p); 41313f6bcfbdSStefan Behrens 41323f6bcfbdSStefan Behrens switch (p->cmd) { 41333f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4134adfa97cbSIlya Dryomov if (root->fs_info->sb->s_flags & MS_RDONLY) { 4135adfa97cbSIlya Dryomov ret = -EROFS; 4136adfa97cbSIlya Dryomov goto out; 4137adfa97cbSIlya Dryomov } 41383f6bcfbdSStefan Behrens if (atomic_xchg( 41393f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41403f6bcfbdSStefan Behrens 1)) { 4141e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 41423f6bcfbdSStefan Behrens } else { 41433f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 41443f6bcfbdSStefan Behrens atomic_set( 41453f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41463f6bcfbdSStefan Behrens 0); 41473f6bcfbdSStefan Behrens } 41483f6bcfbdSStefan Behrens break; 41493f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 41503f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 41513f6bcfbdSStefan Behrens ret = 0; 41523f6bcfbdSStefan Behrens break; 41533f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 41543f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 41553f6bcfbdSStefan Behrens break; 41563f6bcfbdSStefan Behrens default: 41573f6bcfbdSStefan Behrens ret = -EINVAL; 41583f6bcfbdSStefan Behrens break; 41593f6bcfbdSStefan Behrens } 41603f6bcfbdSStefan Behrens 41613f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 41623f6bcfbdSStefan Behrens ret = -EFAULT; 4163adfa97cbSIlya Dryomov out: 41643f6bcfbdSStefan Behrens kfree(p); 41653f6bcfbdSStefan Behrens return ret; 41663f6bcfbdSStefan Behrens } 41673f6bcfbdSStefan Behrens 4168d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4169d7728c96SJan Schmidt { 4170d7728c96SJan Schmidt int ret = 0; 4171d7728c96SJan Schmidt int i; 4172740c3d22SChris Mason u64 rel_ptr; 4173d7728c96SJan Schmidt int size; 4174806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4175d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4176d7728c96SJan Schmidt struct btrfs_path *path; 4177d7728c96SJan Schmidt 417882b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4179d7728c96SJan Schmidt return -EPERM; 4180d7728c96SJan Schmidt 4181d7728c96SJan Schmidt path = btrfs_alloc_path(); 4182d7728c96SJan Schmidt if (!path) { 4183d7728c96SJan Schmidt ret = -ENOMEM; 4184d7728c96SJan Schmidt goto out; 4185d7728c96SJan Schmidt } 4186d7728c96SJan Schmidt 4187d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4188d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4189d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4190d7728c96SJan Schmidt ipa = NULL; 4191d7728c96SJan Schmidt goto out; 4192d7728c96SJan Schmidt } 4193d7728c96SJan Schmidt 4194d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4195d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4196d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4197d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4198d7728c96SJan Schmidt ipath = NULL; 4199d7728c96SJan Schmidt goto out; 4200d7728c96SJan Schmidt } 4201d7728c96SJan Schmidt 4202d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4203d7728c96SJan Schmidt if (ret < 0) 4204d7728c96SJan Schmidt goto out; 4205d7728c96SJan Schmidt 4206d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4207745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4208745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4209740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4210d7728c96SJan Schmidt } 4211d7728c96SJan Schmidt 4212745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 4213745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 4214d7728c96SJan Schmidt if (ret) { 4215d7728c96SJan Schmidt ret = -EFAULT; 4216d7728c96SJan Schmidt goto out; 4217d7728c96SJan Schmidt } 4218d7728c96SJan Schmidt 4219d7728c96SJan Schmidt out: 4220d7728c96SJan Schmidt btrfs_free_path(path); 4221d7728c96SJan Schmidt free_ipath(ipath); 4222d7728c96SJan Schmidt kfree(ipa); 4223d7728c96SJan Schmidt 4224d7728c96SJan Schmidt return ret; 4225d7728c96SJan Schmidt } 4226d7728c96SJan Schmidt 4227d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4228d7728c96SJan Schmidt { 4229d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4230d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4231d7728c96SJan Schmidt 4232d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4233d7728c96SJan Schmidt inodes->bytes_left -= c; 4234d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4235d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4236d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4237d7728c96SJan Schmidt inodes->elem_cnt += 3; 4238d7728c96SJan Schmidt } else { 4239d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4240d7728c96SJan Schmidt inodes->bytes_left = 0; 4241d7728c96SJan Schmidt inodes->elem_missed += 3; 4242d7728c96SJan Schmidt } 4243d7728c96SJan Schmidt 4244d7728c96SJan Schmidt return 0; 4245d7728c96SJan Schmidt } 4246d7728c96SJan Schmidt 4247d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 4248d7728c96SJan Schmidt void __user *arg) 4249d7728c96SJan Schmidt { 4250d7728c96SJan Schmidt int ret = 0; 4251d7728c96SJan Schmidt int size; 4252d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4253d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4254d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4255d7728c96SJan Schmidt 4256d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4257d7728c96SJan Schmidt return -EPERM; 4258d7728c96SJan Schmidt 4259d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 4260d7728c96SJan Schmidt if (IS_ERR(loi)) { 4261d7728c96SJan Schmidt ret = PTR_ERR(loi); 4262d7728c96SJan Schmidt loi = NULL; 4263d7728c96SJan Schmidt goto out; 4264d7728c96SJan Schmidt } 4265d7728c96SJan Schmidt 4266d7728c96SJan Schmidt path = btrfs_alloc_path(); 4267d7728c96SJan Schmidt if (!path) { 4268d7728c96SJan Schmidt ret = -ENOMEM; 4269d7728c96SJan Schmidt goto out; 4270d7728c96SJan Schmidt } 4271d7728c96SJan Schmidt 4272425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 4273d7728c96SJan Schmidt inodes = init_data_container(size); 4274d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4275d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4276d7728c96SJan Schmidt inodes = NULL; 4277d7728c96SJan Schmidt goto out; 4278d7728c96SJan Schmidt } 4279d7728c96SJan Schmidt 4280df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 4281df031f07SLiu Bo build_ino_list, inodes); 4282df031f07SLiu Bo if (ret == -EINVAL) 4283d7728c96SJan Schmidt ret = -ENOENT; 4284d7728c96SJan Schmidt if (ret < 0) 4285d7728c96SJan Schmidt goto out; 4286d7728c96SJan Schmidt 4287745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 4288745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 4289d7728c96SJan Schmidt if (ret) 4290d7728c96SJan Schmidt ret = -EFAULT; 4291d7728c96SJan Schmidt 4292d7728c96SJan Schmidt out: 4293d7728c96SJan Schmidt btrfs_free_path(path); 4294425d17a2SLiu Bo vfree(inodes); 4295d7728c96SJan Schmidt kfree(loi); 4296d7728c96SJan Schmidt 4297d7728c96SJan Schmidt return ret; 4298d7728c96SJan Schmidt } 4299d7728c96SJan Schmidt 430019a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4301c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4302c9e9f97bSIlya Dryomov { 4303c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4304c9e9f97bSIlya Dryomov 4305c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4306c9e9f97bSIlya Dryomov 4307837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4308837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4309837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4310837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4311a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4312a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4313837d5b6eSIlya Dryomov 4314c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4315c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4316c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 431719a39dceSIlya Dryomov 431819a39dceSIlya Dryomov if (lock) { 431919a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 432019a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 432119a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 432219a39dceSIlya Dryomov } else { 432319a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 432419a39dceSIlya Dryomov } 4325c9e9f97bSIlya Dryomov } 4326c9e9f97bSIlya Dryomov 43279ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4328c9e9f97bSIlya Dryomov { 4329496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4330c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4331c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4332c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4333ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4334c9e9f97bSIlya Dryomov int ret; 4335c9e9f97bSIlya Dryomov 4336c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4337c9e9f97bSIlya Dryomov return -EPERM; 4338c9e9f97bSIlya Dryomov 4339e54bfa31SLiu Bo ret = mnt_want_write_file(file); 43409ba1f6e4SLiu Bo if (ret) 43419ba1f6e4SLiu Bo return ret; 43429ba1f6e4SLiu Bo 4343ed0fb78fSIlya Dryomov again: 4344ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 4345c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4346c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4347ed0fb78fSIlya Dryomov need_unlock = true; 4348ed0fb78fSIlya Dryomov goto locked; 4349ed0fb78fSIlya Dryomov } 4350ed0fb78fSIlya Dryomov 4351ed0fb78fSIlya Dryomov /* 4352ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 4353ed0fb78fSIlya Dryomov * (1) some other op is running 4354ed0fb78fSIlya Dryomov * (2) balance is running 4355ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4356ed0fb78fSIlya Dryomov */ 4357ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4358ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4359ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4360ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4361ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4362ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4363ed0fb78fSIlya Dryomov goto again; 4364ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4365ed0fb78fSIlya Dryomov 4366ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4367ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4368ed0fb78fSIlya Dryomov /* this is (3) */ 4369ed0fb78fSIlya Dryomov need_unlock = false; 4370ed0fb78fSIlya Dryomov goto locked; 4371ed0fb78fSIlya Dryomov } 4372ed0fb78fSIlya Dryomov 4373ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4374ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4375ed0fb78fSIlya Dryomov goto again; 4376ed0fb78fSIlya Dryomov } else { 4377ed0fb78fSIlya Dryomov /* this is (2) */ 4378ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4379ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4380ed0fb78fSIlya Dryomov goto out; 4381ed0fb78fSIlya Dryomov } 4382ed0fb78fSIlya Dryomov } else { 4383ed0fb78fSIlya Dryomov /* this is (1) */ 4384ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4385e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4386ed0fb78fSIlya Dryomov goto out; 4387ed0fb78fSIlya Dryomov } 4388ed0fb78fSIlya Dryomov 4389ed0fb78fSIlya Dryomov locked: 4390ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 4391c9e9f97bSIlya Dryomov 4392c9e9f97bSIlya Dryomov if (arg) { 4393c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4394c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4395c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4396ed0fb78fSIlya Dryomov goto out_unlock; 4397c9e9f97bSIlya Dryomov } 4398de322263SIlya Dryomov 4399de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4400de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4401de322263SIlya Dryomov ret = -ENOTCONN; 4402de322263SIlya Dryomov goto out_bargs; 4403de322263SIlya Dryomov } 4404de322263SIlya Dryomov 4405de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4406de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4407de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4408de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4409de322263SIlya Dryomov 4410de322263SIlya Dryomov goto do_balance; 4411de322263SIlya Dryomov } 4412c9e9f97bSIlya Dryomov } else { 4413c9e9f97bSIlya Dryomov bargs = NULL; 4414c9e9f97bSIlya Dryomov } 4415c9e9f97bSIlya Dryomov 4416ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4417837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4418837d5b6eSIlya Dryomov goto out_bargs; 4419837d5b6eSIlya Dryomov } 4420837d5b6eSIlya Dryomov 4421c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 4422c9e9f97bSIlya Dryomov if (!bctl) { 4423c9e9f97bSIlya Dryomov ret = -ENOMEM; 4424c9e9f97bSIlya Dryomov goto out_bargs; 4425c9e9f97bSIlya Dryomov } 4426c9e9f97bSIlya Dryomov 4427c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4428c9e9f97bSIlya Dryomov if (arg) { 4429c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4430c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4431c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4432c9e9f97bSIlya Dryomov 4433c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4434f43ffb60SIlya Dryomov } else { 4435f43ffb60SIlya Dryomov /* balance everything - no filters */ 4436f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4437c9e9f97bSIlya Dryomov } 4438c9e9f97bSIlya Dryomov 4439de322263SIlya Dryomov do_balance: 4440c9e9f97bSIlya Dryomov /* 4441ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 4442ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4443ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4444ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 4445ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 4446c9e9f97bSIlya Dryomov */ 4447ed0fb78fSIlya Dryomov need_unlock = false; 4448ed0fb78fSIlya Dryomov 4449ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 4450ed0fb78fSIlya Dryomov 4451c9e9f97bSIlya Dryomov if (arg) { 4452c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4453c9e9f97bSIlya Dryomov ret = -EFAULT; 4454c9e9f97bSIlya Dryomov } 4455c9e9f97bSIlya Dryomov 4456c9e9f97bSIlya Dryomov out_bargs: 4457c9e9f97bSIlya Dryomov kfree(bargs); 4458ed0fb78fSIlya Dryomov out_unlock: 4459c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4460c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4461ed0fb78fSIlya Dryomov if (need_unlock) 4462ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 4463ed0fb78fSIlya Dryomov out: 4464e54bfa31SLiu Bo mnt_drop_write_file(file); 4465c9e9f97bSIlya Dryomov return ret; 4466c9e9f97bSIlya Dryomov } 4467c9e9f97bSIlya Dryomov 4468837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4469837d5b6eSIlya Dryomov { 4470837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4471837d5b6eSIlya Dryomov return -EPERM; 4472837d5b6eSIlya Dryomov 4473837d5b6eSIlya Dryomov switch (cmd) { 4474837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 4475837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 4476a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 4477a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 4478837d5b6eSIlya Dryomov } 4479837d5b6eSIlya Dryomov 4480837d5b6eSIlya Dryomov return -EINVAL; 4481837d5b6eSIlya Dryomov } 4482837d5b6eSIlya Dryomov 448319a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 448419a39dceSIlya Dryomov void __user *arg) 448519a39dceSIlya Dryomov { 448619a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 448719a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 448819a39dceSIlya Dryomov int ret = 0; 448919a39dceSIlya Dryomov 449019a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 449119a39dceSIlya Dryomov return -EPERM; 449219a39dceSIlya Dryomov 449319a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 449419a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 449519a39dceSIlya Dryomov ret = -ENOTCONN; 449619a39dceSIlya Dryomov goto out; 449719a39dceSIlya Dryomov } 449819a39dceSIlya Dryomov 449919a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 450019a39dceSIlya Dryomov if (!bargs) { 450119a39dceSIlya Dryomov ret = -ENOMEM; 450219a39dceSIlya Dryomov goto out; 450319a39dceSIlya Dryomov } 450419a39dceSIlya Dryomov 450519a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 450619a39dceSIlya Dryomov 450719a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 450819a39dceSIlya Dryomov ret = -EFAULT; 450919a39dceSIlya Dryomov 451019a39dceSIlya Dryomov kfree(bargs); 451119a39dceSIlya Dryomov out: 451219a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 451319a39dceSIlya Dryomov return ret; 451419a39dceSIlya Dryomov } 451519a39dceSIlya Dryomov 4516905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 45175d13a37bSArne Jansen { 4518496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45195d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 45205d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 45215d13a37bSArne Jansen int ret; 45225d13a37bSArne Jansen int err; 45235d13a37bSArne Jansen 45245d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45255d13a37bSArne Jansen return -EPERM; 45265d13a37bSArne Jansen 4527905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4528905b0ddaSMiao Xie if (ret) 4529905b0ddaSMiao Xie return ret; 45305d13a37bSArne Jansen 45315d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4532905b0ddaSMiao Xie if (IS_ERR(sa)) { 4533905b0ddaSMiao Xie ret = PTR_ERR(sa); 4534905b0ddaSMiao Xie goto drop_write; 4535905b0ddaSMiao Xie } 45365d13a37bSArne Jansen 45377708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 453892f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 45395d13a37bSArne Jansen if (IS_ERR(trans)) { 45405d13a37bSArne Jansen ret = PTR_ERR(trans); 45415d13a37bSArne Jansen goto out; 45425d13a37bSArne Jansen } 45435d13a37bSArne Jansen 45445d13a37bSArne Jansen switch (sa->cmd) { 45455d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 45465d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 45475d13a37bSArne Jansen break; 45485d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 45495d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 45505d13a37bSArne Jansen break; 45515d13a37bSArne Jansen default: 45525d13a37bSArne Jansen ret = -EINVAL; 45535d13a37bSArne Jansen break; 45545d13a37bSArne Jansen } 45555d13a37bSArne Jansen 455692f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 45575d13a37bSArne Jansen if (err && !ret) 45585d13a37bSArne Jansen ret = err; 45595d13a37bSArne Jansen out: 45605d13a37bSArne Jansen kfree(sa); 45617708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 4562905b0ddaSMiao Xie drop_write: 4563905b0ddaSMiao Xie mnt_drop_write_file(file); 45645d13a37bSArne Jansen return ret; 45655d13a37bSArne Jansen } 45665d13a37bSArne Jansen 4567905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 45685d13a37bSArne Jansen { 4569496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45705d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 45715d13a37bSArne Jansen struct btrfs_trans_handle *trans; 45725d13a37bSArne Jansen int ret; 45735d13a37bSArne Jansen int err; 45745d13a37bSArne Jansen 45755d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45765d13a37bSArne Jansen return -EPERM; 45775d13a37bSArne Jansen 4578905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4579905b0ddaSMiao Xie if (ret) 4580905b0ddaSMiao Xie return ret; 45815d13a37bSArne Jansen 45825d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4583905b0ddaSMiao Xie if (IS_ERR(sa)) { 4584905b0ddaSMiao Xie ret = PTR_ERR(sa); 4585905b0ddaSMiao Xie goto drop_write; 4586905b0ddaSMiao Xie } 45875d13a37bSArne Jansen 45885d13a37bSArne Jansen trans = btrfs_join_transaction(root); 45895d13a37bSArne Jansen if (IS_ERR(trans)) { 45905d13a37bSArne Jansen ret = PTR_ERR(trans); 45915d13a37bSArne Jansen goto out; 45925d13a37bSArne Jansen } 45935d13a37bSArne Jansen 45945d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 45955d13a37bSArne Jansen if (sa->assign) { 45965d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 45975d13a37bSArne Jansen sa->src, sa->dst); 45985d13a37bSArne Jansen } else { 45995d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 46005d13a37bSArne Jansen sa->src, sa->dst); 46015d13a37bSArne Jansen } 46025d13a37bSArne Jansen 46035d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46045d13a37bSArne Jansen if (err && !ret) 46055d13a37bSArne Jansen ret = err; 46065d13a37bSArne Jansen 46075d13a37bSArne Jansen out: 46085d13a37bSArne Jansen kfree(sa); 4609905b0ddaSMiao Xie drop_write: 4610905b0ddaSMiao Xie mnt_drop_write_file(file); 46115d13a37bSArne Jansen return ret; 46125d13a37bSArne Jansen } 46135d13a37bSArne Jansen 4614905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 46155d13a37bSArne Jansen { 4616496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46175d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 46185d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46195d13a37bSArne Jansen int ret; 46205d13a37bSArne Jansen int err; 46215d13a37bSArne Jansen 46225d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46235d13a37bSArne Jansen return -EPERM; 46245d13a37bSArne Jansen 4625905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4626905b0ddaSMiao Xie if (ret) 4627905b0ddaSMiao Xie return ret; 46285d13a37bSArne Jansen 46295d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4630905b0ddaSMiao Xie if (IS_ERR(sa)) { 4631905b0ddaSMiao Xie ret = PTR_ERR(sa); 4632905b0ddaSMiao Xie goto drop_write; 4633905b0ddaSMiao Xie } 46345d13a37bSArne Jansen 4635d86e56cfSMiao Xie if (!sa->qgroupid) { 4636d86e56cfSMiao Xie ret = -EINVAL; 4637d86e56cfSMiao Xie goto out; 4638d86e56cfSMiao Xie } 4639d86e56cfSMiao Xie 46405d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46415d13a37bSArne Jansen if (IS_ERR(trans)) { 46425d13a37bSArne Jansen ret = PTR_ERR(trans); 46435d13a37bSArne Jansen goto out; 46445d13a37bSArne Jansen } 46455d13a37bSArne Jansen 46465d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 46475d13a37bSArne Jansen if (sa->create) { 46485d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 46495d13a37bSArne Jansen NULL); 46505d13a37bSArne Jansen } else { 46515d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 46525d13a37bSArne Jansen } 46535d13a37bSArne Jansen 46545d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46555d13a37bSArne Jansen if (err && !ret) 46565d13a37bSArne Jansen ret = err; 46575d13a37bSArne Jansen 46585d13a37bSArne Jansen out: 46595d13a37bSArne Jansen kfree(sa); 4660905b0ddaSMiao Xie drop_write: 4661905b0ddaSMiao Xie mnt_drop_write_file(file); 46625d13a37bSArne Jansen return ret; 46635d13a37bSArne Jansen } 46645d13a37bSArne Jansen 4665905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 46665d13a37bSArne Jansen { 4667496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46685d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 46695d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46705d13a37bSArne Jansen int ret; 46715d13a37bSArne Jansen int err; 46725d13a37bSArne Jansen u64 qgroupid; 46735d13a37bSArne Jansen 46745d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46755d13a37bSArne Jansen return -EPERM; 46765d13a37bSArne Jansen 4677905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4678905b0ddaSMiao Xie if (ret) 4679905b0ddaSMiao Xie return ret; 46805d13a37bSArne Jansen 46815d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4682905b0ddaSMiao Xie if (IS_ERR(sa)) { 4683905b0ddaSMiao Xie ret = PTR_ERR(sa); 4684905b0ddaSMiao Xie goto drop_write; 4685905b0ddaSMiao Xie } 46865d13a37bSArne Jansen 46875d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46885d13a37bSArne Jansen if (IS_ERR(trans)) { 46895d13a37bSArne Jansen ret = PTR_ERR(trans); 46905d13a37bSArne Jansen goto out; 46915d13a37bSArne Jansen } 46925d13a37bSArne Jansen 46935d13a37bSArne Jansen qgroupid = sa->qgroupid; 46945d13a37bSArne Jansen if (!qgroupid) { 46955d13a37bSArne Jansen /* take the current subvol as qgroup */ 46965d13a37bSArne Jansen qgroupid = root->root_key.objectid; 46975d13a37bSArne Jansen } 46985d13a37bSArne Jansen 46995d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 47005d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 47015d13a37bSArne Jansen 47025d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 47035d13a37bSArne Jansen if (err && !ret) 47045d13a37bSArne Jansen ret = err; 47055d13a37bSArne Jansen 47065d13a37bSArne Jansen out: 47075d13a37bSArne Jansen kfree(sa); 4708905b0ddaSMiao Xie drop_write: 4709905b0ddaSMiao Xie mnt_drop_write_file(file); 47105d13a37bSArne Jansen return ret; 47115d13a37bSArne Jansen } 47125d13a37bSArne Jansen 47132f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 47142f232036SJan Schmidt { 47156d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47162f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47172f232036SJan Schmidt int ret; 47182f232036SJan Schmidt 47192f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47202f232036SJan Schmidt return -EPERM; 47212f232036SJan Schmidt 47222f232036SJan Schmidt ret = mnt_want_write_file(file); 47232f232036SJan Schmidt if (ret) 47242f232036SJan Schmidt return ret; 47252f232036SJan Schmidt 47262f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 47272f232036SJan Schmidt if (IS_ERR(qsa)) { 47282f232036SJan Schmidt ret = PTR_ERR(qsa); 47292f232036SJan Schmidt goto drop_write; 47302f232036SJan Schmidt } 47312f232036SJan Schmidt 47322f232036SJan Schmidt if (qsa->flags) { 47332f232036SJan Schmidt ret = -EINVAL; 47342f232036SJan Schmidt goto out; 47352f232036SJan Schmidt } 47362f232036SJan Schmidt 47372f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 47382f232036SJan Schmidt 47392f232036SJan Schmidt out: 47402f232036SJan Schmidt kfree(qsa); 47412f232036SJan Schmidt drop_write: 47422f232036SJan Schmidt mnt_drop_write_file(file); 47432f232036SJan Schmidt return ret; 47442f232036SJan Schmidt } 47452f232036SJan Schmidt 47462f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 47472f232036SJan Schmidt { 47486d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47492f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47502f232036SJan Schmidt int ret = 0; 47512f232036SJan Schmidt 47522f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47532f232036SJan Schmidt return -EPERM; 47542f232036SJan Schmidt 47552f232036SJan Schmidt qsa = kzalloc(sizeof(*qsa), GFP_NOFS); 47562f232036SJan Schmidt if (!qsa) 47572f232036SJan Schmidt return -ENOMEM; 47582f232036SJan Schmidt 47592f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 47602f232036SJan Schmidt qsa->flags = 1; 47612f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 47622f232036SJan Schmidt } 47632f232036SJan Schmidt 47642f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 47652f232036SJan Schmidt ret = -EFAULT; 47662f232036SJan Schmidt 47672f232036SJan Schmidt kfree(qsa); 47682f232036SJan Schmidt return ret; 47692f232036SJan Schmidt } 47702f232036SJan Schmidt 477157254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 477257254b6eSJan Schmidt { 477354563d41SAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 477457254b6eSJan Schmidt 477557254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 477657254b6eSJan Schmidt return -EPERM; 477757254b6eSJan Schmidt 477857254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 477957254b6eSJan Schmidt } 478057254b6eSJan Schmidt 4781abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4782abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 47838ea05e3aSAlexander Block { 4784496ad9aaSAl Viro struct inode *inode = file_inode(file); 47858ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 47868ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 47878ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 47888ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 47898ea05e3aSAlexander Block int ret = 0; 4790dd5f9615SStefan Behrens int received_uuid_changed; 47918ea05e3aSAlexander Block 4792bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 4793bd60ea0fSDavid Sterba return -EPERM; 4794bd60ea0fSDavid Sterba 47958ea05e3aSAlexander Block ret = mnt_want_write_file(file); 47968ea05e3aSAlexander Block if (ret < 0) 47978ea05e3aSAlexander Block return ret; 47988ea05e3aSAlexander Block 47998ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 48008ea05e3aSAlexander Block 48018ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 48028ea05e3aSAlexander Block ret = -EINVAL; 48038ea05e3aSAlexander Block goto out; 48048ea05e3aSAlexander Block } 48058ea05e3aSAlexander Block 48068ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 48078ea05e3aSAlexander Block ret = -EROFS; 48088ea05e3aSAlexander Block goto out; 48098ea05e3aSAlexander Block } 48108ea05e3aSAlexander Block 4811dd5f9615SStefan Behrens /* 4812dd5f9615SStefan Behrens * 1 - root item 4813dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4814dd5f9615SStefan Behrens */ 4815dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 48168ea05e3aSAlexander Block if (IS_ERR(trans)) { 48178ea05e3aSAlexander Block ret = PTR_ERR(trans); 48188ea05e3aSAlexander Block trans = NULL; 48198ea05e3aSAlexander Block goto out; 48208ea05e3aSAlexander Block } 48218ea05e3aSAlexander Block 48228ea05e3aSAlexander Block sa->rtransid = trans->transid; 48238ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 48248ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 48258ea05e3aSAlexander Block 4826dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4827dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4828dd5f9615SStefan Behrens if (received_uuid_changed && 4829dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 4830dd5f9615SStefan Behrens btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 4831dd5f9615SStefan Behrens root_item->received_uuid, 4832dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4833dd5f9615SStefan Behrens root->root_key.objectid); 48348ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 48358ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 48368ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 48373cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 48383cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 48393cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 48403cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 48418ea05e3aSAlexander Block 48428ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 48438ea05e3aSAlexander Block &root->root_key, &root->root_item); 48448ea05e3aSAlexander Block if (ret < 0) { 48458ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 48468ea05e3aSAlexander Block goto out; 4847dd5f9615SStefan Behrens } 4848dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4849dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 4850dd5f9615SStefan Behrens sa->uuid, 4851dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4852dd5f9615SStefan Behrens root->root_key.objectid); 4853dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 4854dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 4855dd5f9615SStefan Behrens goto out; 4856dd5f9615SStefan Behrens } 4857dd5f9615SStefan Behrens } 48588ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 4859dd5f9615SStefan Behrens if (ret < 0) { 4860dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 48618ea05e3aSAlexander Block goto out; 48628ea05e3aSAlexander Block } 48638ea05e3aSAlexander Block 4864abccd00fSHugo Mills out: 4865abccd00fSHugo Mills up_write(&root->fs_info->subvol_sem); 4866abccd00fSHugo Mills mnt_drop_write_file(file); 4867abccd00fSHugo Mills return ret; 4868abccd00fSHugo Mills } 4869abccd00fSHugo Mills 4870abccd00fSHugo Mills #ifdef CONFIG_64BIT 4871abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 4872abccd00fSHugo Mills void __user *arg) 4873abccd00fSHugo Mills { 4874abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 4875abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 4876abccd00fSHugo Mills int ret = 0; 4877abccd00fSHugo Mills 4878abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 4879abccd00fSHugo Mills if (IS_ERR(args32)) { 4880abccd00fSHugo Mills ret = PTR_ERR(args32); 4881abccd00fSHugo Mills args32 = NULL; 4882abccd00fSHugo Mills goto out; 4883abccd00fSHugo Mills } 4884abccd00fSHugo Mills 4885abccd00fSHugo Mills args64 = kmalloc(sizeof(*args64), GFP_NOFS); 488684dbeb87SDan Carpenter if (!args64) { 488784dbeb87SDan Carpenter ret = -ENOMEM; 4888abccd00fSHugo Mills goto out; 4889abccd00fSHugo Mills } 4890abccd00fSHugo Mills 4891abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 4892abccd00fSHugo Mills args64->stransid = args32->stransid; 4893abccd00fSHugo Mills args64->rtransid = args32->rtransid; 4894abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 4895abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 4896abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 4897abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 4898abccd00fSHugo Mills args64->flags = args32->flags; 4899abccd00fSHugo Mills 4900abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 4901abccd00fSHugo Mills if (ret) 4902abccd00fSHugo Mills goto out; 4903abccd00fSHugo Mills 4904abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 4905abccd00fSHugo Mills args32->stransid = args64->stransid; 4906abccd00fSHugo Mills args32->rtransid = args64->rtransid; 4907abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 4908abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 4909abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 4910abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 4911abccd00fSHugo Mills args32->flags = args64->flags; 4912abccd00fSHugo Mills 4913abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 4914abccd00fSHugo Mills if (ret) 4915abccd00fSHugo Mills ret = -EFAULT; 4916abccd00fSHugo Mills 4917abccd00fSHugo Mills out: 4918abccd00fSHugo Mills kfree(args32); 4919abccd00fSHugo Mills kfree(args64); 4920abccd00fSHugo Mills return ret; 4921abccd00fSHugo Mills } 4922abccd00fSHugo Mills #endif 4923abccd00fSHugo Mills 4924abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 4925abccd00fSHugo Mills void __user *arg) 4926abccd00fSHugo Mills { 4927abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 4928abccd00fSHugo Mills int ret = 0; 4929abccd00fSHugo Mills 4930abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 4931abccd00fSHugo Mills if (IS_ERR(sa)) { 4932abccd00fSHugo Mills ret = PTR_ERR(sa); 4933abccd00fSHugo Mills sa = NULL; 4934abccd00fSHugo Mills goto out; 4935abccd00fSHugo Mills } 4936abccd00fSHugo Mills 4937abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 4938abccd00fSHugo Mills 4939abccd00fSHugo Mills if (ret) 4940abccd00fSHugo Mills goto out; 4941abccd00fSHugo Mills 49428ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 49438ea05e3aSAlexander Block if (ret) 49448ea05e3aSAlexander Block ret = -EFAULT; 49458ea05e3aSAlexander Block 49468ea05e3aSAlexander Block out: 49478ea05e3aSAlexander Block kfree(sa); 49488ea05e3aSAlexander Block return ret; 49498ea05e3aSAlexander Block } 49508ea05e3aSAlexander Block 4951867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 4952867ab667Sjeff.liu { 49536d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4954a1b83ac5SAnand Jain size_t len; 4955867ab667Sjeff.liu int ret; 4956a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4957a1b83ac5SAnand Jain 4958a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4959a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 4960a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4961a1b83ac5SAnand Jain 4962a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4963867ab667Sjeff.liu 4964867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 4965efe120a0SFrank Holton btrfs_warn(root->fs_info, 4966efe120a0SFrank Holton "label is too long, return the first %zu bytes", --len); 4967867ab667Sjeff.liu } 4968867ab667Sjeff.liu 4969867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4970867ab667Sjeff.liu 4971867ab667Sjeff.liu return ret ? -EFAULT : 0; 4972867ab667Sjeff.liu } 4973867ab667Sjeff.liu 4974a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4975a8bfd4abSjeff.liu { 49766d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4977a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 4978a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4979a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4980a8bfd4abSjeff.liu int ret; 4981a8bfd4abSjeff.liu 4982a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4983a8bfd4abSjeff.liu return -EPERM; 4984a8bfd4abSjeff.liu 4985a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4986a8bfd4abSjeff.liu return -EFAULT; 4987a8bfd4abSjeff.liu 4988a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 4989efe120a0SFrank Holton btrfs_err(root->fs_info, "unable to set label with more than %d bytes", 4990a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4991a8bfd4abSjeff.liu return -EINVAL; 4992a8bfd4abSjeff.liu } 4993a8bfd4abSjeff.liu 4994a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4995a8bfd4abSjeff.liu if (ret) 4996a8bfd4abSjeff.liu return ret; 4997a8bfd4abSjeff.liu 4998a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4999a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5000a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5001a8bfd4abSjeff.liu goto out_unlock; 5002a8bfd4abSjeff.liu } 5003a8bfd4abSjeff.liu 5004a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 5005a8bfd4abSjeff.liu strcpy(super_block->label, label); 5006a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 5007d0270acaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 5008a8bfd4abSjeff.liu 5009a8bfd4abSjeff.liu out_unlock: 5010a8bfd4abSjeff.liu mnt_drop_write_file(file); 5011a8bfd4abSjeff.liu return ret; 5012a8bfd4abSjeff.liu } 5013a8bfd4abSjeff.liu 50142eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 50152eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 50162eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 50172eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 50182eaa055fSJeff Mahoney 50192eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file, 50202eaa055fSJeff Mahoney void __user *arg) 50212eaa055fSJeff Mahoney { 50222eaa055fSJeff Mahoney static struct btrfs_ioctl_feature_flags features[3] = { 50232eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 50242eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 50252eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 50262eaa055fSJeff Mahoney }; 50272eaa055fSJeff Mahoney 50282eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50292eaa055fSJeff Mahoney return -EFAULT; 50302eaa055fSJeff Mahoney 50312eaa055fSJeff Mahoney return 0; 50322eaa055fSJeff Mahoney } 50332eaa055fSJeff Mahoney 50342eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 50352eaa055fSJeff Mahoney { 50362eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 50372eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 50382eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 50392eaa055fSJeff Mahoney 50402eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 50412eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 50422eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 50432eaa055fSJeff Mahoney 50442eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50452eaa055fSJeff Mahoney return -EFAULT; 50462eaa055fSJeff Mahoney 50472eaa055fSJeff Mahoney return 0; 50482eaa055fSJeff Mahoney } 50492eaa055fSJeff Mahoney 50503b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root, 50513b02a68aSJeff Mahoney enum btrfs_feature_set set, 50522eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 50532eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 50542eaa055fSJeff Mahoney { 50553b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 50563b02a68aSJeff Mahoney char *names; 50572eaa055fSJeff Mahoney u64 disallowed, unsupported; 50582eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 50592eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 50602eaa055fSJeff Mahoney 50612eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 50622eaa055fSJeff Mahoney if (unsupported) { 50633b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 50643b02a68aSJeff Mahoney if (names) { 50653b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50663b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 50673b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50683b02a68aSJeff Mahoney kfree(names); 50693b02a68aSJeff Mahoney } else 50702eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50712eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 50722eaa055fSJeff Mahoney type, unsupported); 50732eaa055fSJeff Mahoney return -EOPNOTSUPP; 50742eaa055fSJeff Mahoney } 50752eaa055fSJeff Mahoney 50762eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 50772eaa055fSJeff Mahoney if (disallowed) { 50783b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50793b02a68aSJeff Mahoney if (names) { 50803b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50813b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 50823b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50833b02a68aSJeff Mahoney kfree(names); 50843b02a68aSJeff Mahoney } else 50852eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50862eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 50872eaa055fSJeff Mahoney type, disallowed); 50882eaa055fSJeff Mahoney return -EPERM; 50892eaa055fSJeff Mahoney } 50902eaa055fSJeff Mahoney 50912eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 50922eaa055fSJeff Mahoney if (disallowed) { 50933b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50943b02a68aSJeff Mahoney if (names) { 50953b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50963b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 50973b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50983b02a68aSJeff Mahoney kfree(names); 50993b02a68aSJeff Mahoney } else 51002eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 51012eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 51022eaa055fSJeff Mahoney type, disallowed); 51032eaa055fSJeff Mahoney return -EPERM; 51042eaa055fSJeff Mahoney } 51052eaa055fSJeff Mahoney 51062eaa055fSJeff Mahoney return 0; 51072eaa055fSJeff Mahoney } 51082eaa055fSJeff Mahoney 51092eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base) \ 51103b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 51112eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 51122eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 51132eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 51142eaa055fSJeff Mahoney 51152eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 51162eaa055fSJeff Mahoney { 51172eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 51182eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 51192eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 51202eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 51212eaa055fSJeff Mahoney u64 newflags; 51222eaa055fSJeff Mahoney int ret; 51232eaa055fSJeff Mahoney 51242eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 51252eaa055fSJeff Mahoney return -EPERM; 51262eaa055fSJeff Mahoney 51272eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 51282eaa055fSJeff Mahoney return -EFAULT; 51292eaa055fSJeff Mahoney 51302eaa055fSJeff Mahoney /* Nothing to do */ 51312eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 51322eaa055fSJeff Mahoney !flags[0].incompat_flags) 51332eaa055fSJeff Mahoney return 0; 51342eaa055fSJeff Mahoney 51352eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_flags, 51362eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 51372eaa055fSJeff Mahoney if (ret) 51382eaa055fSJeff Mahoney return ret; 51392eaa055fSJeff Mahoney 51402eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_ro_flags, 51412eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 51422eaa055fSJeff Mahoney if (ret) 51432eaa055fSJeff Mahoney return ret; 51442eaa055fSJeff Mahoney 51452eaa055fSJeff Mahoney ret = check_feature(root, flags[0].incompat_flags, 51462eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 51472eaa055fSJeff Mahoney if (ret) 51482eaa055fSJeff Mahoney return ret; 51492eaa055fSJeff Mahoney 51508051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 51512eaa055fSJeff Mahoney if (IS_ERR(trans)) 51522eaa055fSJeff Mahoney return PTR_ERR(trans); 51532eaa055fSJeff Mahoney 51542eaa055fSJeff Mahoney spin_lock(&root->fs_info->super_lock); 51552eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 51562eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 51572eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 51582eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 51592eaa055fSJeff Mahoney 51602eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 51612eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 51622eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 51632eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 51642eaa055fSJeff Mahoney 51652eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 51662eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 51672eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 51682eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 51692eaa055fSJeff Mahoney spin_unlock(&root->fs_info->super_lock); 51702eaa055fSJeff Mahoney 5171d0270acaSJeff Mahoney return btrfs_commit_transaction(trans, root); 51722eaa055fSJeff Mahoney } 51732eaa055fSJeff Mahoney 5174f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5175f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5176f46b5a66SChristoph Hellwig { 5177496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 51784bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5179f46b5a66SChristoph Hellwig 5180f46b5a66SChristoph Hellwig switch (cmd) { 51816cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 51826cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 51836cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 51846cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 51856cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 51866cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5187f7039b1dSLi Dongyang case FITRIM: 5188f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5189f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5190fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5191fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5192fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 51933de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5194fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 51956f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 51966f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 519776dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 519876dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 51990caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 52000caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 52010caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 52020caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 52036ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 52046ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5205f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 52061e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 52071e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 52081e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5209f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5210198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5211f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 52124bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 5213f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5214da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 5215475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 5216475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 5217475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 5218475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 5219f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 52209ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5221f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 5222c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 5223c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 52247a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 5225f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 5226f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 5227f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 5228f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 5229ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5230ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5231ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5232ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5233d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5234d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5235d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5236d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 52371406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 52381406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 52399b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 52409b199859SFilipe David Borba Manana int ret; 52419b199859SFilipe David Borba Manana 52426c255e67SMiao Xie ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 52439b199859SFilipe David Borba Manana if (ret) 52449b199859SFilipe David Borba Manana return ret; 52459b199859SFilipe David Borba Manana ret = btrfs_sync_fs(file->f_dentry->d_sb, 1); 52469b199859SFilipe David Borba Manana return ret; 52479b199859SFilipe David Borba Manana } 524846204592SSage Weil case BTRFS_IOC_START_SYNC: 52499a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 525046204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 52519a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 5252475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5253b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5254475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 5255475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 5256475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 5257475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 5258c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 52599ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5260837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 5261837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 526219a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 526319a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 52648ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 52658ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5266abccd00fSHugo Mills #ifdef CONFIG_64BIT 5267abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5268abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5269abccd00fSHugo Mills #endif 527031db9f7cSAlexander Block case BTRFS_IOC_SEND: 527131db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 5272c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 5273b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 52745d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5275905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 52765d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5277905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 52785d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5279905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 52805d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5281905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 52822f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 52832f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 52842f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 52852f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 528657254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 528757254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 52883f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 52893f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 5290867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5291867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5292a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5293a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 5294416161dbSMark Fasheh case BTRFS_IOC_FILE_EXTENT_SAME: 5295416161dbSMark Fasheh return btrfs_ioctl_file_extent_same(file, argp); 52962eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 52972eaa055fSJeff Mahoney return btrfs_ioctl_get_supported_features(file, argp); 52982eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 52992eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 53002eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 53012eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5302f46b5a66SChristoph Hellwig } 5303f46b5a66SChristoph Hellwig 5304f46b5a66SChristoph Hellwig return -ENOTTY; 5305f46b5a66SChristoph Hellwig } 5306