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 199312544442SGerhard Heift if (sizeof(sh) + item_len > buf_size) 1994ac8e9819SChris Mason item_len = 0; 1995ac8e9819SChris Mason 199612544442SGerhard Heift if (sizeof(sh) + item_len + *sk_offset > buf_size) { 1997ac8e9819SChris Mason ret = 1; 199825c9bc2eSGerhard Heift goto out; 1999ac8e9819SChris Mason } 2000ac8e9819SChris Mason 2001ac8e9819SChris Mason sh.objectid = key->objectid; 2002ac8e9819SChris Mason sh.offset = key->offset; 2003ac8e9819SChris Mason sh.type = key->type; 2004ac8e9819SChris Mason sh.len = item_len; 2005ac8e9819SChris Mason sh.transid = found_transid; 2006ac8e9819SChris Mason 2007ac8e9819SChris Mason /* copy search result header */ 2008ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 2009ac8e9819SChris Mason *sk_offset += sizeof(sh); 2010ac8e9819SChris Mason 2011ac8e9819SChris Mason if (item_len) { 2012ac8e9819SChris Mason char *p = buf + *sk_offset; 2013ac8e9819SChris Mason /* copy the item */ 2014ac8e9819SChris Mason read_extent_buffer(leaf, p, 2015ac8e9819SChris Mason item_off, item_len); 2016ac8e9819SChris Mason *sk_offset += item_len; 2017ac8e9819SChris Mason } 2018e2156867SHugo Mills (*num_found)++; 2019ac8e9819SChris Mason 202025c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 202125c9bc2eSGerhard Heift ret = 1; 202225c9bc2eSGerhard Heift goto out; 202325c9bc2eSGerhard Heift } 2024ac8e9819SChris Mason } 2025ac8e9819SChris Mason advance_key: 2026ac8e9819SChris Mason ret = 0; 2027abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 2028abc6e134SChris Mason key->offset++; 2029abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 2030abc6e134SChris Mason key->offset = 0; 2031abc6e134SChris Mason key->type++; 2032abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 2033abc6e134SChris Mason key->offset = 0; 2034abc6e134SChris Mason key->type = 0; 2035abc6e134SChris Mason key->objectid++; 2036abc6e134SChris Mason } else 2037abc6e134SChris Mason ret = 1; 203825c9bc2eSGerhard Heift out: 2039ac8e9819SChris Mason return ret; 2040ac8e9819SChris Mason } 2041ac8e9819SChris Mason 2042ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 204312544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 204412544442SGerhard Heift size_t buf_size, 204512544442SGerhard Heift char *buf) 2046ac8e9819SChris Mason { 2047ac8e9819SChris Mason struct btrfs_root *root; 2048ac8e9819SChris Mason struct btrfs_key key; 2049ac8e9819SChris Mason struct btrfs_path *path; 2050ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 2051ac8e9819SChris Mason int ret; 2052ac8e9819SChris Mason int num_found = 0; 2053ac8e9819SChris Mason unsigned long sk_offset = 0; 2054ac8e9819SChris Mason 205512544442SGerhard Heift if (buf_size < sizeof(struct btrfs_ioctl_search_header)) 205612544442SGerhard Heift return -EOVERFLOW; 205712544442SGerhard Heift 2058ac8e9819SChris Mason path = btrfs_alloc_path(); 2059ac8e9819SChris Mason if (!path) 2060ac8e9819SChris Mason return -ENOMEM; 2061ac8e9819SChris Mason 2062ac8e9819SChris Mason if (sk->tree_id == 0) { 2063ac8e9819SChris Mason /* search the root of the inode that was passed */ 2064ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2065ac8e9819SChris Mason } else { 2066ac8e9819SChris Mason key.objectid = sk->tree_id; 2067ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2068ac8e9819SChris Mason key.offset = (u64)-1; 2069ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2070ac8e9819SChris Mason if (IS_ERR(root)) { 2071efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", 2072ac8e9819SChris Mason sk->tree_id); 2073ac8e9819SChris Mason btrfs_free_path(path); 2074ac8e9819SChris Mason return -ENOENT; 2075ac8e9819SChris Mason } 2076ac8e9819SChris Mason } 2077ac8e9819SChris Mason 2078ac8e9819SChris Mason key.objectid = sk->min_objectid; 2079ac8e9819SChris Mason key.type = sk->min_type; 2080ac8e9819SChris Mason key.offset = sk->min_offset; 2081ac8e9819SChris Mason 2082ac8e9819SChris Mason path->keep_locks = 1; 2083ac8e9819SChris Mason 2084ac8e9819SChris Mason while (1) { 20856174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2086ac8e9819SChris Mason if (ret != 0) { 2087ac8e9819SChris Mason if (ret > 0) 2088ac8e9819SChris Mason ret = 0; 2089ac8e9819SChris Mason goto err; 2090ac8e9819SChris Mason } 209112544442SGerhard Heift ret = copy_to_sk(root, path, &key, sk, buf_size, buf, 2092ac8e9819SChris Mason &sk_offset, &num_found); 2093b3b4aa74SDavid Sterba btrfs_release_path(path); 209425c9bc2eSGerhard Heift if (ret) 2095ac8e9819SChris Mason break; 2096ac8e9819SChris Mason 2097ac8e9819SChris Mason } 2098ac8e9819SChris Mason ret = 0; 2099ac8e9819SChris Mason err: 2100ac8e9819SChris Mason sk->nr_items = num_found; 2101ac8e9819SChris Mason btrfs_free_path(path); 2102ac8e9819SChris Mason return ret; 2103ac8e9819SChris Mason } 2104ac8e9819SChris Mason 2105ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2106ac8e9819SChris Mason void __user *argp) 2107ac8e9819SChris Mason { 2108ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 2109ac8e9819SChris Mason struct inode *inode; 2110ac8e9819SChris Mason int ret; 2111ac8e9819SChris Mason 2112ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2113ac8e9819SChris Mason return -EPERM; 2114ac8e9819SChris Mason 21152354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 21162354d08fSJulia Lawall if (IS_ERR(args)) 21172354d08fSJulia Lawall return PTR_ERR(args); 2118ac8e9819SChris Mason 2119496ad9aaSAl Viro inode = file_inode(file); 212012544442SGerhard Heift ret = search_ioctl(inode, &args->key, sizeof(args->buf), args->buf); 2121ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2122ac8e9819SChris Mason ret = -EFAULT; 2123ac8e9819SChris Mason kfree(args); 2124ac8e9819SChris Mason return ret; 2125ac8e9819SChris Mason } 2126ac8e9819SChris Mason 212798d377a0STARUISI Hiroaki /* 2128ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2129ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 213098d377a0STARUISI Hiroaki */ 213198d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 213298d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 213398d377a0STARUISI Hiroaki { 213498d377a0STARUISI Hiroaki struct btrfs_root *root; 213598d377a0STARUISI Hiroaki struct btrfs_key key; 2136ac8e9819SChris Mason char *ptr; 213798d377a0STARUISI Hiroaki int ret = -1; 213898d377a0STARUISI Hiroaki int slot; 213998d377a0STARUISI Hiroaki int len; 214098d377a0STARUISI Hiroaki int total_len = 0; 214198d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 214298d377a0STARUISI Hiroaki struct extent_buffer *l; 214398d377a0STARUISI Hiroaki struct btrfs_path *path; 214498d377a0STARUISI Hiroaki 214598d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 214698d377a0STARUISI Hiroaki name[0]='\0'; 214798d377a0STARUISI Hiroaki return 0; 214898d377a0STARUISI Hiroaki } 214998d377a0STARUISI Hiroaki 215098d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 215198d377a0STARUISI Hiroaki if (!path) 215298d377a0STARUISI Hiroaki return -ENOMEM; 215398d377a0STARUISI Hiroaki 2154ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 215598d377a0STARUISI Hiroaki 215698d377a0STARUISI Hiroaki key.objectid = tree_id; 215798d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 215898d377a0STARUISI Hiroaki key.offset = (u64)-1; 215998d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 216098d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2161efe120a0SFrank Holton printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id); 21628ad6fcabSChris Mason ret = -ENOENT; 21638ad6fcabSChris Mason goto out; 216498d377a0STARUISI Hiroaki } 216598d377a0STARUISI Hiroaki 216698d377a0STARUISI Hiroaki key.objectid = dirid; 216798d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 21688ad6fcabSChris Mason key.offset = (u64)-1; 216998d377a0STARUISI Hiroaki 217098d377a0STARUISI Hiroaki while (1) { 217198d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 217298d377a0STARUISI Hiroaki if (ret < 0) 217398d377a0STARUISI Hiroaki goto out; 217418674c6cSFilipe David Borba Manana else if (ret > 0) { 217518674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 217618674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 217718674c6cSFilipe David Borba Manana if (ret < 0) 217818674c6cSFilipe David Borba Manana goto out; 217918674c6cSFilipe David Borba Manana else if (ret > 0) { 2180ac8e9819SChris Mason ret = -ENOENT; 218198d377a0STARUISI Hiroaki goto out; 2182ac8e9819SChris Mason } 218318674c6cSFilipe David Borba Manana } 218418674c6cSFilipe David Borba Manana 218518674c6cSFilipe David Borba Manana l = path->nodes[0]; 218618674c6cSFilipe David Borba Manana slot = path->slots[0]; 218718674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 218898d377a0STARUISI Hiroaki 218998d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 219098d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 219198d377a0STARUISI Hiroaki ptr -= len + 1; 219298d377a0STARUISI Hiroaki total_len += len + 1; 2193a696cf35SFilipe David Borba Manana if (ptr < name) { 2194a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 219598d377a0STARUISI Hiroaki goto out; 2196a696cf35SFilipe David Borba Manana } 219798d377a0STARUISI Hiroaki 219898d377a0STARUISI Hiroaki *(ptr + len) = '/'; 219998d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 220098d377a0STARUISI Hiroaki 220198d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 220298d377a0STARUISI Hiroaki break; 220398d377a0STARUISI Hiroaki 2204b3b4aa74SDavid Sterba btrfs_release_path(path); 220598d377a0STARUISI Hiroaki key.objectid = key.offset; 22068ad6fcabSChris Mason key.offset = (u64)-1; 220798d377a0STARUISI Hiroaki dirid = key.objectid; 220898d377a0STARUISI Hiroaki } 220977906a50SLi Zefan memmove(name, ptr, total_len); 221098d377a0STARUISI Hiroaki name[total_len] = '\0'; 221198d377a0STARUISI Hiroaki ret = 0; 221298d377a0STARUISI Hiroaki out: 221398d377a0STARUISI Hiroaki btrfs_free_path(path); 2214ac8e9819SChris Mason return ret; 2215ac8e9819SChris Mason } 2216ac8e9819SChris Mason 2217ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2218ac8e9819SChris Mason void __user *argp) 2219ac8e9819SChris Mason { 2220ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2221ac8e9819SChris Mason struct inode *inode; 2222ac8e9819SChris Mason int ret; 2223ac8e9819SChris Mason 2224ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2225ac8e9819SChris Mason return -EPERM; 2226ac8e9819SChris Mason 22272354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 22282354d08fSJulia Lawall if (IS_ERR(args)) 22292354d08fSJulia Lawall return PTR_ERR(args); 2230c2b96929SDan Carpenter 2231496ad9aaSAl Viro inode = file_inode(file); 2232ac8e9819SChris Mason 22331b53ac4dSChris Mason if (args->treeid == 0) 22341b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 22351b53ac4dSChris Mason 2236ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2237ac8e9819SChris Mason args->treeid, args->objectid, 2238ac8e9819SChris Mason args->name); 2239ac8e9819SChris Mason 2240ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2241ac8e9819SChris Mason ret = -EFAULT; 2242ac8e9819SChris Mason 2243ac8e9819SChris Mason kfree(args); 224498d377a0STARUISI Hiroaki return ret; 224598d377a0STARUISI Hiroaki } 224698d377a0STARUISI Hiroaki 224776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 224876dda93cSYan, Zheng void __user *arg) 224976dda93cSYan, Zheng { 225054563d41SAl Viro struct dentry *parent = file->f_path.dentry; 225176dda93cSYan, Zheng struct dentry *dentry; 225276dda93cSYan, Zheng struct inode *dir = parent->d_inode; 225376dda93cSYan, Zheng struct inode *inode; 225476dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 225576dda93cSYan, Zheng struct btrfs_root *dest = NULL; 225676dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 225776dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2258c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2259521e0546SDavid Sterba u64 root_flags; 2260c58aaad2SMiao Xie u64 qgroup_reserved; 226176dda93cSYan, Zheng int namelen; 226276dda93cSYan, Zheng int ret; 226376dda93cSYan, Zheng int err = 0; 226476dda93cSYan, Zheng 226576dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 226676dda93cSYan, Zheng if (IS_ERR(vol_args)) 226776dda93cSYan, Zheng return PTR_ERR(vol_args); 226876dda93cSYan, Zheng 226976dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 227076dda93cSYan, Zheng namelen = strlen(vol_args->name); 227176dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 227276dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 227376dda93cSYan, Zheng err = -EINVAL; 227476dda93cSYan, Zheng goto out; 227576dda93cSYan, Zheng } 227676dda93cSYan, Zheng 2277a561be71SAl Viro err = mnt_want_write_file(file); 227876dda93cSYan, Zheng if (err) 227976dda93cSYan, Zheng goto out; 228076dda93cSYan, Zheng 2281521e0546SDavid Sterba 22825c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 22835c50c9b8SDavid Sterba if (err == -EINTR) 2284e43f998eSDavid Sterba goto out_drop_write; 228576dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 228676dda93cSYan, Zheng if (IS_ERR(dentry)) { 228776dda93cSYan, Zheng err = PTR_ERR(dentry); 228876dda93cSYan, Zheng goto out_unlock_dir; 228976dda93cSYan, Zheng } 229076dda93cSYan, Zheng 229176dda93cSYan, Zheng if (!dentry->d_inode) { 229276dda93cSYan, Zheng err = -ENOENT; 229376dda93cSYan, Zheng goto out_dput; 229476dda93cSYan, Zheng } 229576dda93cSYan, Zheng 229676dda93cSYan, Zheng inode = dentry->d_inode; 22974260f7c7SSage Weil dest = BTRFS_I(inode)->root; 22984260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 22994260f7c7SSage Weil /* 23004260f7c7SSage Weil * Regular user. Only allow this with a special mount 23014260f7c7SSage Weil * option, when the user has write+exec access to the 23024260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23034260f7c7SSage Weil * allowed. 23044260f7c7SSage Weil * 23054260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23064260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23074260f7c7SSage Weil * otherwise be able to delete. 23084260f7c7SSage Weil * 23094260f7c7SSage Weil * Users who want to delete empty subvols should try 23104260f7c7SSage Weil * rmdir(2). 23114260f7c7SSage Weil */ 23124260f7c7SSage Weil err = -EPERM; 23134260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 23144260f7c7SSage Weil goto out_dput; 23154260f7c7SSage Weil 23164260f7c7SSage Weil /* 23174260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 23184260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 23194260f7c7SSage Weil * must be called on the dentry referencing the root 23204260f7c7SSage Weil * of the subvol, not a random directory contained 23214260f7c7SSage Weil * within it. 23224260f7c7SSage Weil */ 23234260f7c7SSage Weil err = -EINVAL; 23244260f7c7SSage Weil if (root == dest) 23254260f7c7SSage Weil goto out_dput; 23264260f7c7SSage Weil 23274260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 23284260f7c7SSage Weil if (err) 23294260f7c7SSage Weil goto out_dput; 23305c39da5bSMiao Xie } 23314260f7c7SSage Weil 23325c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 23334260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 23344260f7c7SSage Weil if (err) 23354260f7c7SSage Weil goto out_dput; 23364260f7c7SSage Weil 233733345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 233876dda93cSYan, Zheng err = -EINVAL; 233976dda93cSYan, Zheng goto out_dput; 234076dda93cSYan, Zheng } 234176dda93cSYan, Zheng 234276dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 2343521e0546SDavid Sterba 2344521e0546SDavid Sterba /* 2345521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2346521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2347521e0546SDavid Sterba * again is not run concurrently. 2348521e0546SDavid Sterba */ 2349521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2350c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2351c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2352c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2353521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2354521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2355521e0546SDavid Sterba } else { 2356521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2357521e0546SDavid Sterba btrfs_warn(root->fs_info, 2358521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2359c55bfa67SFilipe Manana dest->root_key.objectid); 2360521e0546SDavid Sterba err = -EPERM; 2361521e0546SDavid Sterba goto out_dput; 2362521e0546SDavid Sterba } 2363521e0546SDavid Sterba 236476dda93cSYan, Zheng err = d_invalidate(dentry); 236576dda93cSYan, Zheng if (err) 236676dda93cSYan, Zheng goto out_unlock; 236776dda93cSYan, Zheng 236876dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 236976dda93cSYan, Zheng 237076dda93cSYan, Zheng err = may_destroy_subvol(dest); 237176dda93cSYan, Zheng if (err) 237276dda93cSYan, Zheng goto out_up_write; 237376dda93cSYan, Zheng 2374c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2375c58aaad2SMiao Xie /* 2376c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2377c58aaad2SMiao Xie * ref/backref. 2378c58aaad2SMiao Xie */ 2379c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2380ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2381c58aaad2SMiao Xie if (err) 2382c58aaad2SMiao Xie goto out_up_write; 2383c58aaad2SMiao Xie 2384a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2385a22285a6SYan, Zheng if (IS_ERR(trans)) { 2386a22285a6SYan, Zheng err = PTR_ERR(trans); 2387c58aaad2SMiao Xie goto out_release; 2388a22285a6SYan, Zheng } 2389c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2390c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2391a22285a6SYan, Zheng 239276dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 239376dda93cSYan, Zheng dest->root_key.objectid, 239476dda93cSYan, Zheng dentry->d_name.name, 239576dda93cSYan, Zheng dentry->d_name.len); 239679787eaaSJeff Mahoney if (ret) { 239779787eaaSJeff Mahoney err = ret; 239879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 239979787eaaSJeff Mahoney goto out_end_trans; 240079787eaaSJeff Mahoney } 240176dda93cSYan, Zheng 240276dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 240376dda93cSYan, Zheng 240476dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 240576dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 240676dda93cSYan, Zheng dest->root_item.drop_level = 0; 240776dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 240876dda93cSYan, Zheng 240927cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 241076dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 241176dda93cSYan, Zheng root->fs_info->tree_root, 241276dda93cSYan, Zheng dest->root_key.objectid); 241379787eaaSJeff Mahoney if (ret) { 241479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 241579787eaaSJeff Mahoney err = ret; 241679787eaaSJeff Mahoney goto out_end_trans; 2417d68fc57bSYan, Zheng } 241879787eaaSJeff Mahoney } 2419dd5f9615SStefan Behrens 2420dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2421dd5f9615SStefan Behrens dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 2422dd5f9615SStefan Behrens dest->root_key.objectid); 2423dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2424dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2425dd5f9615SStefan Behrens err = ret; 2426dd5f9615SStefan Behrens goto out_end_trans; 2427dd5f9615SStefan Behrens } 2428dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2429dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2430dd5f9615SStefan Behrens dest->root_item.received_uuid, 2431dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2432dd5f9615SStefan Behrens dest->root_key.objectid); 2433dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2434dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2435dd5f9615SStefan Behrens err = ret; 2436dd5f9615SStefan Behrens goto out_end_trans; 2437dd5f9615SStefan Behrens } 2438dd5f9615SStefan Behrens } 2439dd5f9615SStefan Behrens 244079787eaaSJeff Mahoney out_end_trans: 2441c58aaad2SMiao Xie trans->block_rsv = NULL; 2442c58aaad2SMiao Xie trans->bytes_reserved = 0; 2443531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 244479787eaaSJeff Mahoney if (ret && !err) 244579787eaaSJeff Mahoney err = ret; 244676dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2447c58aaad2SMiao Xie out_release: 2448c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 244976dda93cSYan, Zheng out_up_write: 245076dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 245176dda93cSYan, Zheng out_unlock: 2452521e0546SDavid Sterba if (err) { 2453521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2454c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2455c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2456521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2457521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2458521e0546SDavid Sterba } 245976dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 246076dda93cSYan, Zheng if (!err) { 2461efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 246276dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 246376dda93cSYan, Zheng d_delete(dentry); 246461155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2465fa6ac876SLiu Bo 2466fa6ac876SLiu Bo /* the last ref */ 2467fa6ac876SLiu Bo if (dest->cache_inode) { 2468fa6ac876SLiu Bo iput(dest->cache_inode); 2469fa6ac876SLiu Bo dest->cache_inode = NULL; 2470fa6ac876SLiu Bo } 247176dda93cSYan, Zheng } 247276dda93cSYan, Zheng out_dput: 247376dda93cSYan, Zheng dput(dentry); 247476dda93cSYan, Zheng out_unlock_dir: 247576dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 2476e43f998eSDavid Sterba out_drop_write: 24772a79f17eSAl Viro mnt_drop_write_file(file); 247876dda93cSYan, Zheng out: 247976dda93cSYan, Zheng kfree(vol_args); 248076dda93cSYan, Zheng return err; 248176dda93cSYan, Zheng } 248276dda93cSYan, Zheng 24831e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2484f46b5a66SChristoph Hellwig { 2485496ad9aaSAl Viro struct inode *inode = file_inode(file); 2486f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 24871e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2488c146afadSYan Zheng int ret; 2489c146afadSYan Zheng 249025122d15SIlya Dryomov ret = mnt_want_write_file(file); 249125122d15SIlya Dryomov if (ret) 249225122d15SIlya Dryomov return ret; 2493b83cc969SLi Zefan 249425122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 249525122d15SIlya Dryomov ret = -EROFS; 249625122d15SIlya Dryomov goto out; 24975ac00addSStefan Behrens } 2498f46b5a66SChristoph Hellwig 2499f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2500f46b5a66SChristoph Hellwig case S_IFDIR: 2501e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2502e441d54dSChris Mason ret = -EPERM; 2503e441d54dSChris Mason goto out; 2504e441d54dSChris Mason } 2505de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 25068929ecfaSYan, Zheng if (ret) 25078929ecfaSYan, Zheng goto out; 2508de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2509f46b5a66SChristoph Hellwig break; 2510f46b5a66SChristoph Hellwig case S_IFREG: 2511e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2512e441d54dSChris Mason ret = -EINVAL; 2513e441d54dSChris Mason goto out; 2514e441d54dSChris Mason } 25151e701a32SChris Mason 25161e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 25171e701a32SChris Mason if (!range) { 25181e701a32SChris Mason ret = -ENOMEM; 25191e701a32SChris Mason goto out; 25201e701a32SChris Mason } 25211e701a32SChris Mason 25221e701a32SChris Mason if (argp) { 25231e701a32SChris Mason if (copy_from_user(range, argp, 25241e701a32SChris Mason sizeof(*range))) { 25251e701a32SChris Mason ret = -EFAULT; 25261e701a32SChris Mason kfree(range); 2527683be16eSDan Carpenter goto out; 25281e701a32SChris Mason } 25291e701a32SChris Mason /* compression requires us to start the IO */ 25301e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 25311e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 25321e701a32SChris Mason range->extent_thresh = (u32)-1; 25331e701a32SChris Mason } 25341e701a32SChris Mason } else { 25351e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 25361e701a32SChris Mason range->len = (u64)-1; 25371e701a32SChris Mason } 2538496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 25394cb5300bSChris Mason range, 0, 0); 25404cb5300bSChris Mason if (ret > 0) 25414cb5300bSChris Mason ret = 0; 25421e701a32SChris Mason kfree(range); 2543f46b5a66SChristoph Hellwig break; 25448929ecfaSYan, Zheng default: 25458929ecfaSYan, Zheng ret = -EINVAL; 2546f46b5a66SChristoph Hellwig } 2547e441d54dSChris Mason out: 254825122d15SIlya Dryomov mnt_drop_write_file(file); 2549e441d54dSChris Mason return ret; 2550f46b5a66SChristoph Hellwig } 2551f46b5a66SChristoph Hellwig 2552b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2553f46b5a66SChristoph Hellwig { 2554f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2555f46b5a66SChristoph Hellwig int ret; 2556f46b5a66SChristoph Hellwig 2557e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2558e441d54dSChris Mason return -EPERM; 2559e441d54dSChris Mason 25605ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 25615ac00addSStefan Behrens 1)) { 2562e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2563c9e9f97bSIlya Dryomov } 2564c9e9f97bSIlya Dryomov 25655ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2566dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2567c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2568c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2569c9e9f97bSIlya Dryomov goto out; 2570c9e9f97bSIlya Dryomov } 2571f46b5a66SChristoph Hellwig 25725516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2573f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2574f46b5a66SChristoph Hellwig 2575f46b5a66SChristoph Hellwig kfree(vol_args); 2576c9e9f97bSIlya Dryomov out: 2577c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 25785ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2579f46b5a66SChristoph Hellwig return ret; 2580f46b5a66SChristoph Hellwig } 2581f46b5a66SChristoph Hellwig 2582da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2583f46b5a66SChristoph Hellwig { 2584496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2585f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2586f46b5a66SChristoph Hellwig int ret; 2587f46b5a66SChristoph Hellwig 2588e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2589e441d54dSChris Mason return -EPERM; 2590e441d54dSChris Mason 2591da24927bSMiao Xie ret = mnt_want_write_file(file); 2592da24927bSMiao Xie if (ret) 2593da24927bSMiao Xie return ret; 2594c146afadSYan Zheng 2595dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2596c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2597c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2598c9e9f97bSIlya Dryomov goto out; 2599c9e9f97bSIlya Dryomov } 2600f46b5a66SChristoph Hellwig 26015516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2602f46b5a66SChristoph Hellwig 2603183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2604183860f6SAnand Jain 1)) { 2605183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2606183860f6SAnand Jain goto out; 2607183860f6SAnand Jain } 2608183860f6SAnand Jain 2609183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2610183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2611c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 26125ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2613183860f6SAnand Jain 2614183860f6SAnand Jain out: 2615183860f6SAnand Jain kfree(vol_args); 26164ac20c70SIlya Dryomov mnt_drop_write_file(file); 2617f46b5a66SChristoph Hellwig return ret; 2618f46b5a66SChristoph Hellwig } 2619f46b5a66SChristoph Hellwig 2620475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2621475f6387SJan Schmidt { 2622027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2623475f6387SJan Schmidt struct btrfs_device *device; 2624475f6387SJan Schmidt struct btrfs_device *next; 2625475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2626027ed2f0SLi Zefan int ret = 0; 2627475f6387SJan Schmidt 2628027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2629027ed2f0SLi Zefan if (!fi_args) 2630027ed2f0SLi Zefan return -ENOMEM; 2631027ed2f0SLi Zefan 2632f7171750SFilipe David Borba Manana mutex_lock(&fs_devices->device_list_mutex); 2633027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2634027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2635475f6387SJan Schmidt 2636475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2637027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2638027ed2f0SLi Zefan fi_args->max_id = device->devid; 2639475f6387SJan Schmidt } 2640475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2641475f6387SJan Schmidt 264280a773fbSDavid Sterba fi_args->nodesize = root->fs_info->super_copy->nodesize; 264380a773fbSDavid Sterba fi_args->sectorsize = root->fs_info->super_copy->sectorsize; 264480a773fbSDavid Sterba fi_args->clone_alignment = root->fs_info->super_copy->sectorsize; 264580a773fbSDavid Sterba 2646027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2647027ed2f0SLi Zefan ret = -EFAULT; 2648475f6387SJan Schmidt 2649027ed2f0SLi Zefan kfree(fi_args); 2650027ed2f0SLi Zefan return ret; 2651475f6387SJan Schmidt } 2652475f6387SJan Schmidt 2653475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2654475f6387SJan Schmidt { 2655475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2656475f6387SJan Schmidt struct btrfs_device *dev; 2657475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2658475f6387SJan Schmidt int ret = 0; 2659475f6387SJan Schmidt char *s_uuid = NULL; 2660475f6387SJan Schmidt 2661475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2662475f6387SJan Schmidt if (IS_ERR(di_args)) 2663475f6387SJan Schmidt return PTR_ERR(di_args); 2664475f6387SJan Schmidt 2665dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2666475f6387SJan Schmidt s_uuid = di_args->uuid; 2667475f6387SJan Schmidt 2668475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2669aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2670475f6387SJan Schmidt 2671475f6387SJan Schmidt if (!dev) { 2672475f6387SJan Schmidt ret = -ENODEV; 2673475f6387SJan Schmidt goto out; 2674475f6387SJan Schmidt } 2675475f6387SJan Schmidt 2676475f6387SJan Schmidt di_args->devid = dev->devid; 2677475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2678475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2679475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2680a27202fbSJim Meyering if (dev->name) { 2681606686eeSJosef Bacik struct rcu_string *name; 2682606686eeSJosef Bacik 2683606686eeSJosef Bacik rcu_read_lock(); 2684606686eeSJosef Bacik name = rcu_dereference(dev->name); 2685606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2686606686eeSJosef Bacik rcu_read_unlock(); 2687a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2688a27202fbSJim Meyering } else { 268999ba55adSStefan Behrens di_args->path[0] = '\0'; 2690a27202fbSJim Meyering } 2691475f6387SJan Schmidt 2692475f6387SJan Schmidt out: 269355793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2694475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2695475f6387SJan Schmidt ret = -EFAULT; 2696475f6387SJan Schmidt 2697475f6387SJan Schmidt kfree(di_args); 2698475f6387SJan Schmidt return ret; 2699475f6387SJan Schmidt } 2700475f6387SJan Schmidt 2701416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off) 2702416161dbSMark Fasheh { 2703416161dbSMark Fasheh struct page *page; 2704416161dbSMark Fasheh pgoff_t index; 2705416161dbSMark Fasheh struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 2706416161dbSMark Fasheh 2707416161dbSMark Fasheh index = off >> PAGE_CACHE_SHIFT; 2708416161dbSMark Fasheh 2709416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2710416161dbSMark Fasheh if (!page) 2711416161dbSMark Fasheh return NULL; 2712416161dbSMark Fasheh 2713416161dbSMark Fasheh if (!PageUptodate(page)) { 2714416161dbSMark Fasheh if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, 2715416161dbSMark Fasheh 0)) 2716416161dbSMark Fasheh return NULL; 2717416161dbSMark Fasheh lock_page(page); 2718416161dbSMark Fasheh if (!PageUptodate(page)) { 2719416161dbSMark Fasheh unlock_page(page); 2720416161dbSMark Fasheh page_cache_release(page); 2721416161dbSMark Fasheh return NULL; 2722416161dbSMark Fasheh } 2723416161dbSMark Fasheh } 2724416161dbSMark Fasheh unlock_page(page); 2725416161dbSMark Fasheh 2726416161dbSMark Fasheh return page; 2727416161dbSMark Fasheh } 2728416161dbSMark Fasheh 272977fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) 273077fe20dcSMark Fasheh { 273177fe20dcSMark Fasheh /* do any pending delalloc/csum calc on src, one way or 273277fe20dcSMark Fasheh another, and lock file content */ 273377fe20dcSMark Fasheh while (1) { 273477fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 273577fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 273677fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 273777fe20dcSMark Fasheh off + len - 1); 2738ff5df9b8SFilipe Manana if ((!ordered || 2739ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2740ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 274177fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2742ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2743ff5df9b8SFilipe Manana if (ordered) 2744ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 274577fe20dcSMark Fasheh break; 2746ff5df9b8SFilipe Manana } 274777fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 274877fe20dcSMark Fasheh if (ordered) 274977fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 275077fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 275177fe20dcSMark Fasheh } 275277fe20dcSMark Fasheh } 275377fe20dcSMark Fasheh 2754416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1, 2755416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2756416161dbSMark Fasheh { 2757416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2758416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2759416161dbSMark Fasheh 2760416161dbSMark Fasheh mutex_unlock(&inode1->i_mutex); 2761416161dbSMark Fasheh mutex_unlock(&inode2->i_mutex); 2762416161dbSMark Fasheh } 2763416161dbSMark Fasheh 2764416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1, 2765416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2766416161dbSMark Fasheh { 2767416161dbSMark Fasheh if (inode1 < inode2) { 2768416161dbSMark Fasheh swap(inode1, inode2); 2769416161dbSMark Fasheh swap(loff1, loff2); 2770416161dbSMark Fasheh } 2771416161dbSMark Fasheh 2772416161dbSMark Fasheh mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); 2773416161dbSMark Fasheh lock_extent_range(inode1, loff1, len); 2774416161dbSMark Fasheh if (inode1 != inode2) { 2775416161dbSMark Fasheh mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); 2776416161dbSMark Fasheh lock_extent_range(inode2, loff2, len); 2777416161dbSMark Fasheh } 2778416161dbSMark Fasheh } 2779416161dbSMark Fasheh 2780416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, 2781416161dbSMark Fasheh u64 dst_loff, u64 len) 2782416161dbSMark Fasheh { 2783416161dbSMark Fasheh int ret = 0; 2784416161dbSMark Fasheh struct page *src_page, *dst_page; 2785416161dbSMark Fasheh unsigned int cmp_len = PAGE_CACHE_SIZE; 2786416161dbSMark Fasheh void *addr, *dst_addr; 2787416161dbSMark Fasheh 2788416161dbSMark Fasheh while (len) { 2789416161dbSMark Fasheh if (len < PAGE_CACHE_SIZE) 2790416161dbSMark Fasheh cmp_len = len; 2791416161dbSMark Fasheh 2792416161dbSMark Fasheh src_page = extent_same_get_page(src, loff); 2793416161dbSMark Fasheh if (!src_page) 2794416161dbSMark Fasheh return -EINVAL; 2795416161dbSMark Fasheh dst_page = extent_same_get_page(dst, dst_loff); 2796416161dbSMark Fasheh if (!dst_page) { 2797416161dbSMark Fasheh page_cache_release(src_page); 2798416161dbSMark Fasheh return -EINVAL; 2799416161dbSMark Fasheh } 2800416161dbSMark Fasheh addr = kmap_atomic(src_page); 2801416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 2802416161dbSMark Fasheh 2803416161dbSMark Fasheh flush_dcache_page(src_page); 2804416161dbSMark Fasheh flush_dcache_page(dst_page); 2805416161dbSMark Fasheh 2806416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 2807416161dbSMark Fasheh ret = BTRFS_SAME_DATA_DIFFERS; 2808416161dbSMark Fasheh 2809416161dbSMark Fasheh kunmap_atomic(addr); 2810416161dbSMark Fasheh kunmap_atomic(dst_addr); 2811416161dbSMark Fasheh page_cache_release(src_page); 2812416161dbSMark Fasheh page_cache_release(dst_page); 2813416161dbSMark Fasheh 2814416161dbSMark Fasheh if (ret) 2815416161dbSMark Fasheh break; 2816416161dbSMark Fasheh 2817416161dbSMark Fasheh loff += cmp_len; 2818416161dbSMark Fasheh dst_loff += cmp_len; 2819416161dbSMark Fasheh len -= cmp_len; 2820416161dbSMark Fasheh } 2821416161dbSMark Fasheh 2822416161dbSMark Fasheh return ret; 2823416161dbSMark Fasheh } 2824416161dbSMark Fasheh 2825416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) 2826416161dbSMark Fasheh { 2827416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 2828416161dbSMark Fasheh 2829416161dbSMark Fasheh if (off + len > inode->i_size || off + len < off) 2830416161dbSMark Fasheh return -EINVAL; 2831416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 2832416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 2833416161dbSMark Fasheh return -EINVAL; 2834416161dbSMark Fasheh 2835416161dbSMark Fasheh return 0; 2836416161dbSMark Fasheh } 2837416161dbSMark Fasheh 2838416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, 2839416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 2840416161dbSMark Fasheh { 2841416161dbSMark Fasheh int ret; 2842416161dbSMark Fasheh 2843416161dbSMark Fasheh /* 2844416161dbSMark Fasheh * btrfs_clone() can't handle extents in the same file 2845416161dbSMark Fasheh * yet. Once that works, we can drop this check and replace it 2846416161dbSMark Fasheh * with a check for the same inode, but overlapping extents. 2847416161dbSMark Fasheh */ 2848416161dbSMark Fasheh if (src == dst) 2849416161dbSMark Fasheh return -EINVAL; 2850416161dbSMark Fasheh 2851416161dbSMark Fasheh btrfs_double_lock(src, loff, dst, dst_loff, len); 2852416161dbSMark Fasheh 2853416161dbSMark Fasheh ret = extent_same_check_offsets(src, loff, len); 2854416161dbSMark Fasheh if (ret) 2855416161dbSMark Fasheh goto out_unlock; 2856416161dbSMark Fasheh 2857416161dbSMark Fasheh ret = extent_same_check_offsets(dst, dst_loff, len); 2858416161dbSMark Fasheh if (ret) 2859416161dbSMark Fasheh goto out_unlock; 2860416161dbSMark Fasheh 2861416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 2862416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 2863416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 2864416161dbSMark Fasheh ret = -EINVAL; 2865416161dbSMark Fasheh goto out_unlock; 2866416161dbSMark Fasheh } 2867416161dbSMark Fasheh 2868416161dbSMark Fasheh ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); 2869416161dbSMark Fasheh if (ret == 0) 2870416161dbSMark Fasheh ret = btrfs_clone(src, dst, loff, len, len, dst_loff); 2871416161dbSMark Fasheh 2872416161dbSMark Fasheh out_unlock: 2873416161dbSMark Fasheh btrfs_double_unlock(src, loff, dst, dst_loff, len); 2874416161dbSMark Fasheh 2875416161dbSMark Fasheh return ret; 2876416161dbSMark Fasheh } 2877416161dbSMark Fasheh 2878416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) 2879416161dbSMark Fasheh 2880416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file, 28811c1c8747SAl Viro struct btrfs_ioctl_same_args __user *argp) 2882416161dbSMark Fasheh { 2883cbf8b8caSMark Fasheh struct btrfs_ioctl_same_args *same; 2884cbf8b8caSMark Fasheh struct btrfs_ioctl_same_extent_info *info; 28851c1c8747SAl Viro struct inode *src = file_inode(file); 2886416161dbSMark Fasheh u64 off; 2887416161dbSMark Fasheh u64 len; 2888416161dbSMark Fasheh int i; 2889416161dbSMark Fasheh int ret; 2890cbf8b8caSMark Fasheh unsigned long size; 2891416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 2892416161dbSMark Fasheh bool is_admin = capable(CAP_SYS_ADMIN); 28931c1c8747SAl Viro u16 count; 2894416161dbSMark Fasheh 2895416161dbSMark Fasheh if (!(file->f_mode & FMODE_READ)) 2896416161dbSMark Fasheh return -EINVAL; 2897416161dbSMark Fasheh 2898416161dbSMark Fasheh ret = mnt_want_write_file(file); 2899416161dbSMark Fasheh if (ret) 2900416161dbSMark Fasheh return ret; 2901416161dbSMark Fasheh 29021c1c8747SAl Viro if (get_user(count, &argp->dest_count)) { 2903416161dbSMark Fasheh ret = -EFAULT; 2904416161dbSMark Fasheh goto out; 2905416161dbSMark Fasheh } 2906416161dbSMark Fasheh 29071c1c8747SAl Viro size = offsetof(struct btrfs_ioctl_same_args __user, info[count]); 2908cbf8b8caSMark Fasheh 29091c1c8747SAl Viro same = memdup_user(argp, size); 2910cbf8b8caSMark Fasheh 2911229eed43SGeyslan G. Bem if (IS_ERR(same)) { 2912229eed43SGeyslan G. Bem ret = PTR_ERR(same); 2913cbf8b8caSMark Fasheh goto out; 2914cbf8b8caSMark Fasheh } 2915cbf8b8caSMark Fasheh 2916cbf8b8caSMark Fasheh off = same->logical_offset; 2917cbf8b8caSMark Fasheh len = same->length; 2918416161dbSMark Fasheh 2919416161dbSMark Fasheh /* 2920416161dbSMark Fasheh * Limit the total length we will dedupe for each operation. 2921416161dbSMark Fasheh * This is intended to bound the total time spent in this 2922416161dbSMark Fasheh * ioctl to something sane. 2923416161dbSMark Fasheh */ 2924416161dbSMark Fasheh if (len > BTRFS_MAX_DEDUPE_LEN) 2925416161dbSMark Fasheh len = BTRFS_MAX_DEDUPE_LEN; 2926416161dbSMark Fasheh 2927416161dbSMark Fasheh if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { 2928416161dbSMark Fasheh /* 2929416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 2930416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 2931416161dbSMark Fasheh * this situation without an update. 2932416161dbSMark Fasheh */ 2933416161dbSMark Fasheh ret = -EINVAL; 2934416161dbSMark Fasheh goto out; 2935416161dbSMark Fasheh } 2936416161dbSMark Fasheh 2937416161dbSMark Fasheh ret = -EISDIR; 2938416161dbSMark Fasheh if (S_ISDIR(src->i_mode)) 2939416161dbSMark Fasheh goto out; 2940416161dbSMark Fasheh 2941416161dbSMark Fasheh ret = -EACCES; 2942416161dbSMark Fasheh if (!S_ISREG(src->i_mode)) 2943416161dbSMark Fasheh goto out; 2944416161dbSMark Fasheh 2945cbf8b8caSMark Fasheh /* pre-format output fields to sane values */ 29461c1c8747SAl Viro for (i = 0; i < count; i++) { 2947cbf8b8caSMark Fasheh same->info[i].bytes_deduped = 0ULL; 2948cbf8b8caSMark Fasheh same->info[i].status = 0; 2949416161dbSMark Fasheh } 2950416161dbSMark Fasheh 29511c1c8747SAl Viro for (i = 0, info = same->info; i < count; i++, info++) { 29521c1c8747SAl Viro struct inode *dst; 29531c1c8747SAl Viro struct fd dst_file = fdget(info->fd); 29541c1c8747SAl Viro if (!dst_file.file) { 2955cbf8b8caSMark Fasheh info->status = -EBADF; 29561c1c8747SAl Viro continue; 2957416161dbSMark Fasheh } 29581c1c8747SAl Viro dst = file_inode(dst_file.file); 2959416161dbSMark Fasheh 29601c1c8747SAl Viro if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) { 2961cbf8b8caSMark Fasheh info->status = -EINVAL; 29621c1c8747SAl Viro } else if (file->f_path.mnt != dst_file.file->f_path.mnt) { 2963cbf8b8caSMark Fasheh info->status = -EXDEV; 29641c1c8747SAl Viro } else if (S_ISDIR(dst->i_mode)) { 2965cbf8b8caSMark Fasheh info->status = -EISDIR; 29661c1c8747SAl Viro } else if (!S_ISREG(dst->i_mode)) { 2967cbf8b8caSMark Fasheh info->status = -EACCES; 29681c1c8747SAl Viro } else { 2969cbf8b8caSMark Fasheh info->status = btrfs_extent_same(src, off, len, dst, 2970cbf8b8caSMark Fasheh info->logical_offset); 2971cbf8b8caSMark Fasheh if (info->status == 0) 2972cbf8b8caSMark Fasheh info->bytes_deduped += len; 29731c1c8747SAl Viro } 29741c1c8747SAl Viro fdput(dst_file); 2975cbf8b8caSMark Fasheh } 2976416161dbSMark Fasheh 2977cbf8b8caSMark Fasheh ret = copy_to_user(argp, same, size); 2978cbf8b8caSMark Fasheh if (ret) 2979416161dbSMark Fasheh ret = -EFAULT; 2980416161dbSMark Fasheh 2981416161dbSMark Fasheh out: 2982416161dbSMark Fasheh mnt_drop_write_file(file); 2983416161dbSMark Fasheh return ret; 2984416161dbSMark Fasheh } 2985416161dbSMark Fasheh 2986fcebe456SJosef Bacik /* Helper to check and see if this root currently has a ref on the given disk 2987fcebe456SJosef Bacik * bytenr. If it does then we need to update the quota for this root. This 2988fcebe456SJosef Bacik * doesn't do anything if quotas aren't enabled. 2989fcebe456SJosef Bacik */ 2990fcebe456SJosef Bacik static int check_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2991fcebe456SJosef Bacik u64 disko) 2992fcebe456SJosef Bacik { 2993fcebe456SJosef Bacik struct seq_list tree_mod_seq_elem = {}; 2994fcebe456SJosef Bacik struct ulist *roots; 2995fcebe456SJosef Bacik struct ulist_iterator uiter; 2996fcebe456SJosef Bacik struct ulist_node *root_node = NULL; 2997fcebe456SJosef Bacik int ret; 2998fcebe456SJosef Bacik 2999fcebe456SJosef Bacik if (!root->fs_info->quota_enabled) 3000fcebe456SJosef Bacik return 1; 3001fcebe456SJosef Bacik 3002fcebe456SJosef Bacik btrfs_get_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3003fcebe456SJosef Bacik ret = btrfs_find_all_roots(trans, root->fs_info, disko, 3004fcebe456SJosef Bacik tree_mod_seq_elem.seq, &roots); 3005fcebe456SJosef Bacik if (ret < 0) 3006fcebe456SJosef Bacik goto out; 3007fcebe456SJosef Bacik ret = 0; 3008fcebe456SJosef Bacik ULIST_ITER_INIT(&uiter); 3009fcebe456SJosef Bacik while ((root_node = ulist_next(roots, &uiter))) { 3010fcebe456SJosef Bacik if (root_node->val == root->objectid) { 3011fcebe456SJosef Bacik ret = 1; 3012fcebe456SJosef Bacik break; 3013fcebe456SJosef Bacik } 3014fcebe456SJosef Bacik } 3015fcebe456SJosef Bacik ulist_free(roots); 3016fcebe456SJosef Bacik out: 3017fcebe456SJosef Bacik btrfs_put_tree_mod_seq(root->fs_info, &tree_mod_seq_elem); 3018fcebe456SJosef Bacik return ret; 3019fcebe456SJosef Bacik } 3020fcebe456SJosef Bacik 3021f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3022f82a9901SFilipe Manana struct inode *inode, 3023f82a9901SFilipe Manana u64 endoff, 3024f82a9901SFilipe Manana const u64 destoff, 3025f82a9901SFilipe Manana const u64 olen) 3026f82a9901SFilipe Manana { 3027f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3028f82a9901SFilipe Manana int ret; 3029f82a9901SFilipe Manana 3030f82a9901SFilipe Manana inode_inc_iversion(inode); 3031f82a9901SFilipe Manana inode->i_mtime = inode->i_ctime = CURRENT_TIME; 3032f82a9901SFilipe Manana /* 3033f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3034f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3035f82a9901SFilipe Manana */ 3036f82a9901SFilipe Manana if (endoff > destoff + olen) 3037f82a9901SFilipe Manana endoff = destoff + olen; 3038f82a9901SFilipe Manana if (endoff > inode->i_size) 3039f82a9901SFilipe Manana btrfs_i_size_write(inode, endoff); 3040f82a9901SFilipe Manana 3041f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3042f82a9901SFilipe Manana if (ret) { 3043f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3044f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3045f82a9901SFilipe Manana goto out; 3046f82a9901SFilipe Manana } 3047f82a9901SFilipe Manana ret = btrfs_end_transaction(trans, root); 3048f82a9901SFilipe Manana out: 3049f82a9901SFilipe Manana return ret; 3050f82a9901SFilipe Manana } 3051f82a9901SFilipe Manana 30527ffbb598SFilipe Manana static void clone_update_extent_map(struct inode *inode, 30537ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 30547ffbb598SFilipe Manana const struct btrfs_path *path, 30557ffbb598SFilipe Manana struct btrfs_file_extent_item *fi, 30567ffbb598SFilipe Manana const u64 hole_offset, 30577ffbb598SFilipe Manana const u64 hole_len) 30587ffbb598SFilipe Manana { 30597ffbb598SFilipe Manana struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 30607ffbb598SFilipe Manana struct extent_map *em; 30617ffbb598SFilipe Manana int ret; 30627ffbb598SFilipe Manana 30637ffbb598SFilipe Manana em = alloc_extent_map(); 30647ffbb598SFilipe Manana if (!em) { 30657ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 30667ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 30677ffbb598SFilipe Manana return; 30687ffbb598SFilipe Manana } 30697ffbb598SFilipe Manana 30707ffbb598SFilipe Manana if (fi) { 30717ffbb598SFilipe Manana btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 30727ffbb598SFilipe Manana em->generation = -1; 30737ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 30747ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 30757ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 30767ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 30777ffbb598SFilipe Manana } else { 30787ffbb598SFilipe Manana em->start = hole_offset; 30797ffbb598SFilipe Manana em->len = hole_len; 30807ffbb598SFilipe Manana em->ram_bytes = em->len; 30817ffbb598SFilipe Manana em->orig_start = hole_offset; 30827ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 30837ffbb598SFilipe Manana em->block_len = 0; 30847ffbb598SFilipe Manana em->orig_block_len = 0; 30857ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 30867ffbb598SFilipe Manana em->generation = trans->transid; 30877ffbb598SFilipe Manana } 30887ffbb598SFilipe Manana 30897ffbb598SFilipe Manana while (1) { 30907ffbb598SFilipe Manana write_lock(&em_tree->lock); 30917ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 30927ffbb598SFilipe Manana write_unlock(&em_tree->lock); 30937ffbb598SFilipe Manana if (ret != -EEXIST) { 30947ffbb598SFilipe Manana free_extent_map(em); 30957ffbb598SFilipe Manana break; 30967ffbb598SFilipe Manana } 30977ffbb598SFilipe Manana btrfs_drop_extent_cache(inode, em->start, 30987ffbb598SFilipe Manana em->start + em->len - 1, 0); 30997ffbb598SFilipe Manana } 31007ffbb598SFilipe Manana 31017ffbb598SFilipe Manana if (unlikely(ret)) 31027ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 31037ffbb598SFilipe Manana &BTRFS_I(inode)->runtime_flags); 31047ffbb598SFilipe Manana } 31057ffbb598SFilipe Manana 310632b7c687SMark Fasheh /** 310732b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 310832b7c687SMark Fasheh * 310932b7c687SMark Fasheh * @src: Inode to clone from 311032b7c687SMark Fasheh * @inode: Inode to clone to 311132b7c687SMark Fasheh * @off: Offset within source to start clone from 311232b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 311332b7c687SMark Fasheh * @olen_aligned: Block-aligned value of olen, extent_same uses 311432b7c687SMark Fasheh * identical values here 311532b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 311632b7c687SMark Fasheh */ 311732b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3118f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 3119f82a9901SFilipe Manana const u64 destoff) 3120f46b5a66SChristoph Hellwig { 3121f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 312232b7c687SMark Fasheh struct btrfs_path *path = NULL; 3123f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 312432b7c687SMark Fasheh struct btrfs_trans_handle *trans; 312532b7c687SMark Fasheh char *buf = NULL; 3126ae01a0abSYan Zheng struct btrfs_key key; 3127f46b5a66SChristoph Hellwig u32 nritems; 3128f46b5a66SChristoph Hellwig int slot; 3129ae01a0abSYan Zheng int ret; 3130fcebe456SJosef Bacik int no_quota; 3131f82a9901SFilipe Manana const u64 len = olen_aligned; 3132fcebe456SJosef Bacik u64 last_disko = 0; 3133f82a9901SFilipe Manana u64 last_dest_end = destoff; 3134ae01a0abSYan Zheng 3135ae01a0abSYan Zheng ret = -ENOMEM; 3136ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 3137ae01a0abSYan Zheng if (!buf) 313832b7c687SMark Fasheh return ret; 3139ae01a0abSYan Zheng 3140ae01a0abSYan Zheng path = btrfs_alloc_path(); 3141ae01a0abSYan Zheng if (!path) { 3142ae01a0abSYan Zheng vfree(buf); 314332b7c687SMark Fasheh return ret; 3144ae01a0abSYan Zheng } 314532b7c687SMark Fasheh 3146ae01a0abSYan Zheng path->reada = 2; 3147c5c9cd4dSSage Weil /* clone data */ 314833345d01SLi Zefan key.objectid = btrfs_ino(src); 3149ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 31502c463823SFilipe Manana key.offset = off; 3151f46b5a66SChristoph Hellwig 3152f46b5a66SChristoph Hellwig while (1) { 3153f46b5a66SChristoph Hellwig /* 3154f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3155f46b5a66SChristoph Hellwig * tree. 3156f46b5a66SChristoph Hellwig */ 3157e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3158362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3159362a20c5SDavid Sterba 0, 0); 3160f46b5a66SChristoph Hellwig if (ret < 0) 3161f46b5a66SChristoph Hellwig goto out; 31622c463823SFilipe Manana /* 31632c463823SFilipe Manana * First search, if no extent item that starts at offset off was 31642c463823SFilipe Manana * found but the previous item is an extent item, it's possible 31652c463823SFilipe Manana * it might overlap our target range, therefore process it. 31662c463823SFilipe Manana */ 31672c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 31682c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 31692c463823SFilipe Manana path->slots[0] - 1); 31702c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 31712c463823SFilipe Manana path->slots[0]--; 31722c463823SFilipe Manana } 3173f46b5a66SChristoph Hellwig 3174ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3175e4355f34SFilipe David Borba Manana process_slot: 3176fcebe456SJosef Bacik no_quota = 1; 3177ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3178362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3179f46b5a66SChristoph Hellwig if (ret < 0) 3180f46b5a66SChristoph Hellwig goto out; 3181f46b5a66SChristoph Hellwig if (ret > 0) 3182f46b5a66SChristoph Hellwig break; 3183ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3184f46b5a66SChristoph Hellwig } 3185f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3186f46b5a66SChristoph Hellwig slot = path->slots[0]; 3187f46b5a66SChristoph Hellwig 3188ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3189d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 319033345d01SLi Zefan key.objectid != btrfs_ino(src)) 3191f46b5a66SChristoph Hellwig break; 3192f46b5a66SChristoph Hellwig 3193c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 3194c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3195c5c9cd4dSSage Weil int type; 319631840ae1SZheng Yan u32 size; 319731840ae1SZheng Yan struct btrfs_key new_key; 3198c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3199c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3200c5c9cd4dSSage Weil u8 comp; 3201f82a9901SFilipe Manana u64 drop_start; 320231840ae1SZheng Yan 3203c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3204c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3205c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3206c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3207c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3208c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3209d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3210d397712bSChris Mason extent); 3211d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3212d397712bSChris Mason extent); 3213c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3214d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3215d397712bSChris Mason extent); 3216c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3217c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3218c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3219c5c9cd4dSSage Weil extent); 3220c5c9cd4dSSage Weil } 322131840ae1SZheng Yan 32222c463823SFilipe Manana /* 32232c463823SFilipe Manana * The first search might have left us at an extent 32242c463823SFilipe Manana * item that ends before our target range's start, can 32252c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 32262c463823SFilipe Manana */ 32272c463823SFilipe Manana if (key.offset + datal <= off) { 3228e4355f34SFilipe David Borba Manana path->slots[0]++; 3229e4355f34SFilipe David Borba Manana goto process_slot; 32302c463823SFilipe Manana } else if (key.offset >= off + len) { 32312c463823SFilipe Manana break; 3232e4355f34SFilipe David Borba Manana } 3233e4355f34SFilipe David Borba Manana 3234e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3235e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3236e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3237e4355f34SFilipe David Borba Manana size); 3238e4355f34SFilipe David Borba Manana 3239e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3240e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3241c5c9cd4dSSage Weil 324231840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 324333345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 32444d728ec7SLi Zefan if (off <= key.offset) 3245c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 32464d728ec7SLi Zefan else 32474d728ec7SLi Zefan new_key.offset = destoff; 3248c5c9cd4dSSage Weil 3249b6f3409bSSage Weil /* 3250f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3251f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3252f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3253f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3254f82a9901SFilipe Manana * partially overlaps it). 3255f82a9901SFilipe Manana */ 3256f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3257f82a9901SFilipe Manana drop_start = last_dest_end; 3258f82a9901SFilipe Manana else 3259f82a9901SFilipe Manana drop_start = new_key.offset; 3260f82a9901SFilipe Manana 3261f82a9901SFilipe Manana /* 3262b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3263b6f3409bSSage Weil * 1 - add new extent 3264b6f3409bSSage Weil * 1 - inode update 3265b6f3409bSSage Weil */ 3266b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3267a22285a6SYan, Zheng if (IS_ERR(trans)) { 3268a22285a6SYan, Zheng ret = PTR_ERR(trans); 3269a22285a6SYan, Zheng goto out; 3270a22285a6SYan, Zheng } 3271a22285a6SYan, Zheng 3272c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3273c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3274d72c0842SLi Zefan /* 3275d72c0842SLi Zefan * a | --- range to clone ---| b 3276d72c0842SLi Zefan * | ------------- extent ------------- | 3277d72c0842SLi Zefan */ 3278d72c0842SLi Zefan 327993915584SAntonio Ospite /* subtract range b */ 3280d72c0842SLi Zefan if (key.offset + datal > off + len) 3281d72c0842SLi Zefan datal = off + len - key.offset; 3282d72c0842SLi Zefan 328393915584SAntonio Ospite /* subtract range a */ 3284a22285a6SYan, Zheng if (off > key.offset) { 3285a22285a6SYan, Zheng datao += off - key.offset; 3286a22285a6SYan, Zheng datal -= off - key.offset; 3287a22285a6SYan, Zheng } 3288a22285a6SYan, Zheng 32895dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3290f82a9901SFilipe Manana drop_start, 3291a22285a6SYan, Zheng new_key.offset + datal, 32922671485dSJosef Bacik 1); 329379787eaaSJeff Mahoney if (ret) { 32943f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 329500fdf13aSLiu Bo btrfs_abort_transaction(trans, 329600fdf13aSLiu Bo root, ret); 329779787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 329879787eaaSJeff Mahoney goto out; 329979787eaaSJeff Mahoney } 3300a22285a6SYan, Zheng 330131840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 330231840ae1SZheng Yan &new_key, size); 330379787eaaSJeff Mahoney if (ret) { 330479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 330579787eaaSJeff Mahoney ret); 330679787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 330779787eaaSJeff Mahoney goto out; 330879787eaaSJeff Mahoney } 330931840ae1SZheng Yan 331031840ae1SZheng Yan leaf = path->nodes[0]; 331131840ae1SZheng Yan slot = path->slots[0]; 331231840ae1SZheng Yan write_extent_buffer(leaf, buf, 331331840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 331431840ae1SZheng Yan size); 3315ae01a0abSYan Zheng 3316f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3317f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3318c5c9cd4dSSage Weil 3319c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3320c5c9cd4dSSage Weil if (!disko) 3321c5c9cd4dSSage Weil datao = 0; 3322c5c9cd4dSSage Weil 3323c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3324c5c9cd4dSSage Weil datao); 3325c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3326c5c9cd4dSSage Weil datal); 3327fcebe456SJosef Bacik 3328fcebe456SJosef Bacik /* 3329fcebe456SJosef Bacik * We need to look up the roots that point at 3330fcebe456SJosef Bacik * this bytenr and see if the new root does. If 3331fcebe456SJosef Bacik * it does not we need to make sure we update 3332fcebe456SJosef Bacik * quotas appropriately. 3333fcebe456SJosef Bacik */ 3334fcebe456SJosef Bacik if (disko && root != BTRFS_I(src)->root && 3335fcebe456SJosef Bacik disko != last_disko) { 3336fcebe456SJosef Bacik no_quota = check_ref(trans, root, 3337fcebe456SJosef Bacik disko); 3338fcebe456SJosef Bacik if (no_quota < 0) { 3339fcebe456SJosef Bacik btrfs_abort_transaction(trans, 3340fcebe456SJosef Bacik root, 3341fcebe456SJosef Bacik ret); 3342fcebe456SJosef Bacik btrfs_end_transaction(trans, 3343fcebe456SJosef Bacik root); 3344fcebe456SJosef Bacik ret = no_quota; 3345fcebe456SJosef Bacik goto out; 3346fcebe456SJosef Bacik } 3347fcebe456SJosef Bacik } 3348fcebe456SJosef Bacik 3349c5c9cd4dSSage Weil if (disko) { 3350c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3351ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 33525d4f98a2SYan Zheng disko, diskl, 0, 3353f46b5a66SChristoph Hellwig root->root_key.objectid, 335433345d01SLi Zefan btrfs_ino(inode), 335566d7e7f0SArne Jansen new_key.offset - datao, 3356fcebe456SJosef Bacik no_quota); 335779787eaaSJeff Mahoney if (ret) { 335879787eaaSJeff Mahoney btrfs_abort_transaction(trans, 335979787eaaSJeff Mahoney root, 336079787eaaSJeff Mahoney ret); 336179787eaaSJeff Mahoney btrfs_end_transaction(trans, 336279787eaaSJeff Mahoney root); 336379787eaaSJeff Mahoney goto out; 336479787eaaSJeff Mahoney 336579787eaaSJeff Mahoney } 3366f46b5a66SChristoph Hellwig } 3367c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3368c5c9cd4dSSage Weil u64 skip = 0; 3369c5c9cd4dSSage Weil u64 trim = 0; 3370d3ecfcdfSLiu Bo u64 aligned_end = 0; 3371d3ecfcdfSLiu Bo 3372c5c9cd4dSSage Weil if (off > key.offset) { 3373c5c9cd4dSSage Weil skip = off - key.offset; 3374c5c9cd4dSSage Weil new_key.offset += skip; 337531840ae1SZheng Yan } 3376d397712bSChris Mason 3377c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3378c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3379d397712bSChris Mason 3380c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3381c5c9cd4dSSage Weil ret = -EINVAL; 3382a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 3383c5c9cd4dSSage Weil goto out; 338431840ae1SZheng Yan } 3385c5c9cd4dSSage Weil size -= skip + trim; 3386c5c9cd4dSSage Weil datal -= skip + trim; 3387a22285a6SYan, Zheng 3388d3ecfcdfSLiu Bo aligned_end = ALIGN(new_key.offset + datal, 3389d3ecfcdfSLiu Bo root->sectorsize); 33905dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3391f82a9901SFilipe Manana drop_start, 3392d3ecfcdfSLiu Bo aligned_end, 33932671485dSJosef Bacik 1); 339479787eaaSJeff Mahoney if (ret) { 33953f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 33963a29bc09SChris Mason btrfs_abort_transaction(trans, 33973a29bc09SChris Mason root, ret); 339879787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 339979787eaaSJeff Mahoney goto out; 340079787eaaSJeff Mahoney } 3401a22285a6SYan, Zheng 3402c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 3403c5c9cd4dSSage Weil &new_key, size); 340479787eaaSJeff Mahoney if (ret) { 340579787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 340679787eaaSJeff Mahoney ret); 340779787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 340879787eaaSJeff Mahoney goto out; 340979787eaaSJeff Mahoney } 3410c5c9cd4dSSage Weil 3411c5c9cd4dSSage Weil if (skip) { 3412d397712bSChris Mason u32 start = 3413d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 3414c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 3415c5c9cd4dSSage Weil datal); 3416c5c9cd4dSSage Weil } 3417c5c9cd4dSSage Weil 3418c5c9cd4dSSage Weil leaf = path->nodes[0]; 3419c5c9cd4dSSage Weil slot = path->slots[0]; 3420c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 3421c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 3422c5c9cd4dSSage Weil size); 3423c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 34247ffbb598SFilipe Manana extent = btrfs_item_ptr(leaf, slot, 34257ffbb598SFilipe Manana struct btrfs_file_extent_item); 3426c5c9cd4dSSage Weil } 3427c5c9cd4dSSage Weil 34287ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 34297ffbb598SFilipe Manana if (drop_start < new_key.offset) 34307ffbb598SFilipe Manana clone_update_extent_map(inode, trans, 34317ffbb598SFilipe Manana path, NULL, drop_start, 34327ffbb598SFilipe Manana new_key.offset - drop_start); 34337ffbb598SFilipe Manana 34347ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, 34357ffbb598SFilipe Manana extent, 0, 0); 34367ffbb598SFilipe Manana 3437c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3438b3b4aa74SDavid Sterba btrfs_release_path(path); 3439c5c9cd4dSSage Weil 3440f82a9901SFilipe Manana last_dest_end = new_key.offset + datal; 3441f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3442f82a9901SFilipe Manana last_dest_end, 3443f82a9901SFilipe Manana destoff, olen); 3444f82a9901SFilipe Manana if (ret) 344579787eaaSJeff Mahoney goto out; 34462c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 34472c463823SFilipe Manana break; 3448a22285a6SYan, Zheng } 3449b3b4aa74SDavid Sterba btrfs_release_path(path); 3450ae01a0abSYan Zheng key.offset++; 3451ae01a0abSYan Zheng } 3452f46b5a66SChristoph Hellwig ret = 0; 345332b7c687SMark Fasheh 3454f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3455f82a9901SFilipe Manana /* 3456f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3457f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3458f82a9901SFilipe Manana */ 3459f82a9901SFilipe Manana btrfs_release_path(path); 3460f82a9901SFilipe Manana 3461f82a9901SFilipe Manana /* 3462f82a9901SFilipe Manana * 1 - remove extent(s) 3463f82a9901SFilipe Manana * 1 - inode update 3464f82a9901SFilipe Manana */ 3465f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3466f82a9901SFilipe Manana if (IS_ERR(trans)) { 3467f82a9901SFilipe Manana ret = PTR_ERR(trans); 3468f82a9901SFilipe Manana goto out; 3469f82a9901SFilipe Manana } 3470f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3471f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3472f82a9901SFilipe Manana if (ret) { 3473f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 3474f82a9901SFilipe Manana btrfs_abort_transaction(trans, root, ret); 3475f82a9901SFilipe Manana btrfs_end_transaction(trans, root); 3476f82a9901SFilipe Manana goto out; 3477f82a9901SFilipe Manana } 3478f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 3479f82a9901SFilipe Manana destoff, olen); 34807ffbb598SFilipe Manana if (ret) 34817ffbb598SFilipe Manana goto out; 34827ffbb598SFilipe Manana clone_update_extent_map(inode, trans, path, NULL, last_dest_end, 34837ffbb598SFilipe Manana destoff + len - last_dest_end); 3484f82a9901SFilipe Manana } 3485f82a9901SFilipe Manana 3486f46b5a66SChristoph Hellwig out: 348732b7c687SMark Fasheh btrfs_free_path(path); 348832b7c687SMark Fasheh vfree(buf); 348932b7c687SMark Fasheh return ret; 349032b7c687SMark Fasheh } 349132b7c687SMark Fasheh 349232b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 349332b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 349432b7c687SMark Fasheh { 349554563d41SAl Viro struct inode *inode = file_inode(file); 349632b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 349732b7c687SMark Fasheh struct fd src_file; 349832b7c687SMark Fasheh struct inode *src; 349932b7c687SMark Fasheh int ret; 350032b7c687SMark Fasheh u64 len = olen; 350132b7c687SMark Fasheh u64 bs = root->fs_info->sb->s_blocksize; 350232b7c687SMark Fasheh int same_inode = 0; 350332b7c687SMark Fasheh 350432b7c687SMark Fasheh /* 350532b7c687SMark Fasheh * TODO: 350632b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 350732b7c687SMark Fasheh * decompress into destination's address_space (the file offset 350832b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 350932b7c687SMark Fasheh * otherwise reinsert) a subrange. 351000fdf13aSLiu Bo * 351100fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 351200fdf13aSLiu Bo * be either compressed or non-compressed. 351332b7c687SMark Fasheh */ 351432b7c687SMark Fasheh 351532b7c687SMark Fasheh /* the destination must be opened for writing */ 351632b7c687SMark Fasheh if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 351732b7c687SMark Fasheh return -EINVAL; 351832b7c687SMark Fasheh 351932b7c687SMark Fasheh if (btrfs_root_readonly(root)) 352032b7c687SMark Fasheh return -EROFS; 352132b7c687SMark Fasheh 352232b7c687SMark Fasheh ret = mnt_want_write_file(file); 352332b7c687SMark Fasheh if (ret) 352432b7c687SMark Fasheh return ret; 352532b7c687SMark Fasheh 352632b7c687SMark Fasheh src_file = fdget(srcfd); 352732b7c687SMark Fasheh if (!src_file.file) { 352832b7c687SMark Fasheh ret = -EBADF; 352932b7c687SMark Fasheh goto out_drop_write; 353032b7c687SMark Fasheh } 353132b7c687SMark Fasheh 353232b7c687SMark Fasheh ret = -EXDEV; 353332b7c687SMark Fasheh if (src_file.file->f_path.mnt != file->f_path.mnt) 353432b7c687SMark Fasheh goto out_fput; 353532b7c687SMark Fasheh 353632b7c687SMark Fasheh src = file_inode(src_file.file); 353732b7c687SMark Fasheh 353832b7c687SMark Fasheh ret = -EINVAL; 353932b7c687SMark Fasheh if (src == inode) 354032b7c687SMark Fasheh same_inode = 1; 354132b7c687SMark Fasheh 354232b7c687SMark Fasheh /* the src must be open for reading */ 354332b7c687SMark Fasheh if (!(src_file.file->f_mode & FMODE_READ)) 354432b7c687SMark Fasheh goto out_fput; 354532b7c687SMark Fasheh 354632b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 354732b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 354832b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 354932b7c687SMark Fasheh goto out_fput; 355032b7c687SMark Fasheh 355132b7c687SMark Fasheh ret = -EISDIR; 355232b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 355332b7c687SMark Fasheh goto out_fput; 355432b7c687SMark Fasheh 355532b7c687SMark Fasheh ret = -EXDEV; 355632b7c687SMark Fasheh if (src->i_sb != inode->i_sb) 355732b7c687SMark Fasheh goto out_fput; 355832b7c687SMark Fasheh 355932b7c687SMark Fasheh if (!same_inode) { 356032b7c687SMark Fasheh if (inode < src) { 356132b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 356232b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 356332b7c687SMark Fasheh } else { 356432b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 356532b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 356632b7c687SMark Fasheh } 356732b7c687SMark Fasheh } else { 356832b7c687SMark Fasheh mutex_lock(&src->i_mutex); 356932b7c687SMark Fasheh } 357032b7c687SMark Fasheh 357132b7c687SMark Fasheh /* determine range to clone */ 357232b7c687SMark Fasheh ret = -EINVAL; 357332b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 357432b7c687SMark Fasheh goto out_unlock; 357532b7c687SMark Fasheh if (len == 0) 357632b7c687SMark Fasheh olen = len = src->i_size - off; 357732b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 357832b7c687SMark Fasheh if (off + len == src->i_size) 357932b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 358032b7c687SMark Fasheh 358132b7c687SMark Fasheh /* verify the end result is block aligned */ 358232b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 358332b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 358432b7c687SMark Fasheh goto out_unlock; 358532b7c687SMark Fasheh 358632b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 358732b7c687SMark Fasheh if (same_inode) { 358832b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 358932b7c687SMark Fasheh goto out_unlock; 359032b7c687SMark Fasheh } 359132b7c687SMark Fasheh 359232b7c687SMark Fasheh if (destoff > inode->i_size) { 359332b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 359432b7c687SMark Fasheh if (ret) 359532b7c687SMark Fasheh goto out_unlock; 359632b7c687SMark Fasheh } 359732b7c687SMark Fasheh 3598c125b8bfSFilipe Manana /* 3599c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3600c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3601c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3602c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3603c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3604c125b8bfSFilipe Manana */ 3605c125b8bfSFilipe Manana if (same_inode) { 3606c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3607c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 360832b7c687SMark Fasheh 3609c125b8bfSFilipe Manana lock_extent_range(src, lock_start, lock_len); 3610c125b8bfSFilipe Manana } else { 361132b7c687SMark Fasheh lock_extent_range(src, off, len); 3612c125b8bfSFilipe Manana lock_extent_range(inode, destoff, len); 3613c125b8bfSFilipe Manana } 361432b7c687SMark Fasheh 361532b7c687SMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff); 361632b7c687SMark Fasheh 3617c125b8bfSFilipe Manana if (same_inode) { 3618c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3619c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3620c125b8bfSFilipe Manana 3621c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3622c125b8bfSFilipe Manana } else { 3623aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 3624c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(inode)->io_tree, destoff, 3625c125b8bfSFilipe Manana destoff + len - 1); 3626c125b8bfSFilipe Manana } 3627c125b8bfSFilipe Manana /* 3628c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3629c125b8bfSFilipe Manana * data immediately and not the previous data. 3630c125b8bfSFilipe Manana */ 3631c125b8bfSFilipe Manana truncate_inode_pages_range(&inode->i_data, destoff, 3632c125b8bfSFilipe Manana PAGE_CACHE_ALIGN(destoff + len) - 1); 3633f46b5a66SChristoph Hellwig out_unlock: 3634c57c2b3eSFilipe David Borba Manana if (!same_inode) { 3635c57c2b3eSFilipe David Borba Manana if (inode < src) { 3636f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 3637f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 3638c57c2b3eSFilipe David Borba Manana } else { 3639c57c2b3eSFilipe David Borba Manana mutex_unlock(&inode->i_mutex); 3640c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3641c57c2b3eSFilipe David Borba Manana } 3642c57c2b3eSFilipe David Borba Manana } else { 3643c57c2b3eSFilipe David Borba Manana mutex_unlock(&src->i_mutex); 3644c57c2b3eSFilipe David Borba Manana } 3645f46b5a66SChristoph Hellwig out_fput: 36462903ff01SAl Viro fdput(src_file); 3647ab67b7c1SYan Zheng out_drop_write: 36482a79f17eSAl Viro mnt_drop_write_file(file); 3649f46b5a66SChristoph Hellwig return ret; 3650f46b5a66SChristoph Hellwig } 3651f46b5a66SChristoph Hellwig 36527a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 3653c5c9cd4dSSage Weil { 3654c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 3655c5c9cd4dSSage Weil 36567a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 3657c5c9cd4dSSage Weil return -EFAULT; 3658c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 3659c5c9cd4dSSage Weil args.src_length, args.dest_offset); 3660c5c9cd4dSSage Weil } 3661c5c9cd4dSSage Weil 3662f46b5a66SChristoph Hellwig /* 3663f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3664f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3665f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3666f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3667f46b5a66SChristoph Hellwig */ 3668b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3669f46b5a66SChristoph Hellwig { 3670496ad9aaSAl Viro struct inode *inode = file_inode(file); 3671f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3672f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 36731ab86aedSSage Weil int ret; 3674f46b5a66SChristoph Hellwig 36751ab86aedSSage Weil ret = -EPERM; 3676df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3677f46b5a66SChristoph Hellwig goto out; 36781ab86aedSSage Weil 36791ab86aedSSage Weil ret = -EINPROGRESS; 36801ab86aedSSage Weil if (file->private_data) 36811ab86aedSSage Weil goto out; 36829ca9ee09SSage Weil 3683b83cc969SLi Zefan ret = -EROFS; 3684b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3685b83cc969SLi Zefan goto out; 3686b83cc969SLi Zefan 3687a561be71SAl Viro ret = mnt_want_write_file(file); 3688c146afadSYan Zheng if (ret) 3689c146afadSYan Zheng goto out; 3690c146afadSYan Zheng 3691a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 36929ca9ee09SSage Weil 3693f46b5a66SChristoph Hellwig ret = -ENOMEM; 36947a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3695abd30bb0STsutomu Itoh if (IS_ERR(trans)) 36961ab86aedSSage Weil goto out_drop; 36971ab86aedSSage Weil 36981ab86aedSSage Weil file->private_data = trans; 36991ab86aedSSage Weil return 0; 37001ab86aedSSage Weil 37011ab86aedSSage Weil out_drop: 3702a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 37032a79f17eSAl Viro mnt_drop_write_file(file); 3704f46b5a66SChristoph Hellwig out: 3705f46b5a66SChristoph Hellwig return ret; 3706f46b5a66SChristoph Hellwig } 3707f46b5a66SChristoph Hellwig 37086ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 37096ef5ed0dSJosef Bacik { 3710496ad9aaSAl Viro struct inode *inode = file_inode(file); 37116ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 37126ef5ed0dSJosef Bacik struct btrfs_root *new_root; 37136ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 37146ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 37156ef5ed0dSJosef Bacik struct btrfs_path *path; 37166ef5ed0dSJosef Bacik struct btrfs_key location; 37176ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 37186ef5ed0dSJosef Bacik u64 objectid = 0; 37196ef5ed0dSJosef Bacik u64 dir_id; 37203c04ce01SMiao Xie int ret; 37216ef5ed0dSJosef Bacik 37226ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 37236ef5ed0dSJosef Bacik return -EPERM; 37246ef5ed0dSJosef Bacik 37253c04ce01SMiao Xie ret = mnt_want_write_file(file); 37263c04ce01SMiao Xie if (ret) 37273c04ce01SMiao Xie return ret; 37283c04ce01SMiao Xie 37293c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 37303c04ce01SMiao Xie ret = -EFAULT; 37313c04ce01SMiao Xie goto out; 37323c04ce01SMiao Xie } 37336ef5ed0dSJosef Bacik 37346ef5ed0dSJosef Bacik if (!objectid) 37351cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 37366ef5ed0dSJosef Bacik 37376ef5ed0dSJosef Bacik location.objectid = objectid; 37386ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 37396ef5ed0dSJosef Bacik location.offset = (u64)-1; 37406ef5ed0dSJosef Bacik 37416ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 37423c04ce01SMiao Xie if (IS_ERR(new_root)) { 37433c04ce01SMiao Xie ret = PTR_ERR(new_root); 37443c04ce01SMiao Xie goto out; 37453c04ce01SMiao Xie } 37466ef5ed0dSJosef Bacik 37476ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 37483c04ce01SMiao Xie if (!path) { 37493c04ce01SMiao Xie ret = -ENOMEM; 37503c04ce01SMiao Xie goto out; 37513c04ce01SMiao Xie } 37526ef5ed0dSJosef Bacik path->leave_spinning = 1; 37536ef5ed0dSJosef Bacik 37546ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 375598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 37566ef5ed0dSJosef Bacik btrfs_free_path(path); 37573c04ce01SMiao Xie ret = PTR_ERR(trans); 37583c04ce01SMiao Xie goto out; 37596ef5ed0dSJosef Bacik } 37606ef5ed0dSJosef Bacik 37616c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 37626ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 37636ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3764cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 37656ef5ed0dSJosef Bacik btrfs_free_path(path); 37666ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 3767efe120a0SFrank Holton btrfs_err(new_root->fs_info, "Umm, you don't have the default dir" 3768efe120a0SFrank Holton "item, this isn't going to work"); 37693c04ce01SMiao Xie ret = -ENOENT; 37703c04ce01SMiao Xie goto out; 37716ef5ed0dSJosef Bacik } 37726ef5ed0dSJosef Bacik 37736ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 37746ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 37756ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 37766ef5ed0dSJosef Bacik btrfs_free_path(path); 37776ef5ed0dSJosef Bacik 37782b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 37796ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 37803c04ce01SMiao Xie out: 37813c04ce01SMiao Xie mnt_drop_write_file(file); 37823c04ce01SMiao Xie return ret; 37836ef5ed0dSJosef Bacik } 37846ef5ed0dSJosef Bacik 37855af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 3786bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3787bf5fc093SJosef Bacik { 3788bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 3789bf5fc093SJosef Bacik 3790bf5fc093SJosef Bacik space->total_bytes = 0; 3791bf5fc093SJosef Bacik space->used_bytes = 0; 3792bf5fc093SJosef Bacik space->flags = 0; 3793bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3794bf5fc093SJosef Bacik space->flags = block_group->flags; 3795bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 3796bf5fc093SJosef Bacik space->used_bytes += 3797bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 3798bf5fc093SJosef Bacik } 3799bf5fc093SJosef Bacik } 3800bf5fc093SJosef Bacik 380148a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 38021406e432SJosef Bacik { 38031406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 38041406e432SJosef Bacik struct btrfs_ioctl_space_info space; 38051406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 38067fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 380713f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 38081406e432SJosef Bacik struct btrfs_space_info *info; 3809bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 3810bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3811bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3812bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 3813bf5fc093SJosef Bacik int num_types = 4; 38147fde62bfSChris Mason int alloc_size; 38151406e432SJosef Bacik int ret = 0; 381651788b1bSDan Rosenberg u64 slot_count = 0; 3817bf5fc093SJosef Bacik int i, c; 38181406e432SJosef Bacik 38191406e432SJosef Bacik if (copy_from_user(&space_args, 38201406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 38211406e432SJosef Bacik sizeof(space_args))) 38221406e432SJosef Bacik return -EFAULT; 38231406e432SJosef Bacik 3824bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3825bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3826bf5fc093SJosef Bacik 3827bf5fc093SJosef Bacik info = NULL; 38287fde62bfSChris Mason rcu_read_lock(); 3829bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3830bf5fc093SJosef Bacik list) { 3831bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3832bf5fc093SJosef Bacik info = tmp; 3833bf5fc093SJosef Bacik break; 3834bf5fc093SJosef Bacik } 3835bf5fc093SJosef Bacik } 38367fde62bfSChris Mason rcu_read_unlock(); 38371406e432SJosef Bacik 3838bf5fc093SJosef Bacik if (!info) 3839bf5fc093SJosef Bacik continue; 3840bf5fc093SJosef Bacik 3841bf5fc093SJosef Bacik down_read(&info->groups_sem); 3842bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3843bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3844bf5fc093SJosef Bacik slot_count++; 3845bf5fc093SJosef Bacik } 3846bf5fc093SJosef Bacik up_read(&info->groups_sem); 3847bf5fc093SJosef Bacik } 3848bf5fc093SJosef Bacik 384936523e95SDavid Sterba /* 385036523e95SDavid Sterba * Global block reserve, exported as a space_info 385136523e95SDavid Sterba */ 385236523e95SDavid Sterba slot_count++; 385336523e95SDavid Sterba 38547fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 38557fde62bfSChris Mason if (space_args.space_slots == 0) { 38567fde62bfSChris Mason space_args.total_spaces = slot_count; 38577fde62bfSChris Mason goto out; 38587fde62bfSChris Mason } 3859bf5fc093SJosef Bacik 386051788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3861bf5fc093SJosef Bacik 38627fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3863bf5fc093SJosef Bacik 38647fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 38657fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 38667fde62bfSChris Mason */ 38677fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 38687fde62bfSChris Mason return -ENOMEM; 38697fde62bfSChris Mason 38707fde62bfSChris Mason space_args.total_spaces = 0; 38717fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 38727fde62bfSChris Mason if (!dest) 38737fde62bfSChris Mason return -ENOMEM; 38747fde62bfSChris Mason dest_orig = dest; 38757fde62bfSChris Mason 38767fde62bfSChris Mason /* now we have a buffer to copy into */ 3877bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3878bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3879bf5fc093SJosef Bacik 388051788b1bSDan Rosenberg if (!slot_count) 388151788b1bSDan Rosenberg break; 388251788b1bSDan Rosenberg 3883bf5fc093SJosef Bacik info = NULL; 38841406e432SJosef Bacik rcu_read_lock(); 3885bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3886bf5fc093SJosef Bacik list) { 3887bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3888bf5fc093SJosef Bacik info = tmp; 38897fde62bfSChris Mason break; 3890bf5fc093SJosef Bacik } 3891bf5fc093SJosef Bacik } 3892bf5fc093SJosef Bacik rcu_read_unlock(); 38937fde62bfSChris Mason 3894bf5fc093SJosef Bacik if (!info) 3895bf5fc093SJosef Bacik continue; 3896bf5fc093SJosef Bacik down_read(&info->groups_sem); 3897bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3898bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 38995af3e8ccSStefan Behrens btrfs_get_block_group_info( 39005af3e8ccSStefan Behrens &info->block_groups[c], &space); 39017fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 39021406e432SJosef Bacik dest++; 39031406e432SJosef Bacik space_args.total_spaces++; 390451788b1bSDan Rosenberg slot_count--; 39051406e432SJosef Bacik } 390651788b1bSDan Rosenberg if (!slot_count) 390751788b1bSDan Rosenberg break; 3908bf5fc093SJosef Bacik } 3909bf5fc093SJosef Bacik up_read(&info->groups_sem); 3910bf5fc093SJosef Bacik } 39111406e432SJosef Bacik 391236523e95SDavid Sterba /* 391336523e95SDavid Sterba * Add global block reserve 391436523e95SDavid Sterba */ 391536523e95SDavid Sterba if (slot_count) { 391636523e95SDavid Sterba struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 391736523e95SDavid Sterba 391836523e95SDavid Sterba spin_lock(&block_rsv->lock); 391936523e95SDavid Sterba space.total_bytes = block_rsv->size; 392036523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 392136523e95SDavid Sterba spin_unlock(&block_rsv->lock); 392236523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 392336523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 392436523e95SDavid Sterba space_args.total_spaces++; 392536523e95SDavid Sterba } 392636523e95SDavid Sterba 39272eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 39287fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 39297fde62bfSChris Mason 39307fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 39317fde62bfSChris Mason ret = -EFAULT; 39327fde62bfSChris Mason 39337fde62bfSChris Mason kfree(dest_orig); 39347fde62bfSChris Mason out: 39357fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 39361406e432SJosef Bacik ret = -EFAULT; 39371406e432SJosef Bacik 39381406e432SJosef Bacik return ret; 39391406e432SJosef Bacik } 39401406e432SJosef Bacik 3941f46b5a66SChristoph Hellwig /* 3942f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3943f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3944f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3945f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3946f46b5a66SChristoph Hellwig */ 3947f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3948f46b5a66SChristoph Hellwig { 3949496ad9aaSAl Viro struct inode *inode = file_inode(file); 3950f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3951f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3952f46b5a66SChristoph Hellwig 3953f46b5a66SChristoph Hellwig trans = file->private_data; 39541ab86aedSSage Weil if (!trans) 39551ab86aedSSage Weil return -EINVAL; 3956b214107eSChristoph Hellwig file->private_data = NULL; 39579ca9ee09SSage Weil 39581ab86aedSSage Weil btrfs_end_transaction(trans, root); 39591ab86aedSSage Weil 3960a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 39619ca9ee09SSage Weil 39622a79f17eSAl Viro mnt_drop_write_file(file); 39631ab86aedSSage Weil return 0; 3964f46b5a66SChristoph Hellwig } 3965f46b5a66SChristoph Hellwig 39669a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 39679a8c28beSMiao Xie void __user *argp) 396846204592SSage Weil { 396946204592SSage Weil struct btrfs_trans_handle *trans; 397046204592SSage Weil u64 transid; 3971db5b493aSTsutomu Itoh int ret; 397246204592SSage Weil 3973d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3974ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3975ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 397698d5dc13STsutomu Itoh return PTR_ERR(trans); 3977ff7c1d33SMiao Xie 3978ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3979ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3980ff7c1d33SMiao Xie goto out; 3981ff7c1d33SMiao Xie } 398246204592SSage Weil transid = trans->transid; 3983db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 39848b2b2d3cSTsutomu Itoh if (ret) { 39858b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 3986db5b493aSTsutomu Itoh return ret; 39878b2b2d3cSTsutomu Itoh } 3988ff7c1d33SMiao Xie out: 398946204592SSage Weil if (argp) 399046204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 399146204592SSage Weil return -EFAULT; 399246204592SSage Weil return 0; 399346204592SSage Weil } 399446204592SSage Weil 39959a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 39969a8c28beSMiao Xie void __user *argp) 399746204592SSage Weil { 399846204592SSage Weil u64 transid; 399946204592SSage Weil 400046204592SSage Weil if (argp) { 400146204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 400246204592SSage Weil return -EFAULT; 400346204592SSage Weil } else { 400446204592SSage Weil transid = 0; /* current trans */ 400546204592SSage Weil } 400646204592SSage Weil return btrfs_wait_for_commit(root, transid); 400746204592SSage Weil } 400846204592SSage Weil 4009b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4010475f6387SJan Schmidt { 4011496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4012475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4013b8e95489SMiao Xie int ret; 4014475f6387SJan Schmidt 4015475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4016475f6387SJan Schmidt return -EPERM; 4017475f6387SJan Schmidt 4018475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4019475f6387SJan Schmidt if (IS_ERR(sa)) 4020475f6387SJan Schmidt return PTR_ERR(sa); 4021475f6387SJan Schmidt 4022b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4023b8e95489SMiao Xie ret = mnt_want_write_file(file); 4024b8e95489SMiao Xie if (ret) 4025b8e95489SMiao Xie goto out; 4026b8e95489SMiao Xie } 4027b8e95489SMiao Xie 4028aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 402963a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 403063a212abSStefan Behrens 0); 4031475f6387SJan Schmidt 4032475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4033475f6387SJan Schmidt ret = -EFAULT; 4034475f6387SJan Schmidt 4035b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4036b8e95489SMiao Xie mnt_drop_write_file(file); 4037b8e95489SMiao Xie out: 4038475f6387SJan Schmidt kfree(sa); 4039475f6387SJan Schmidt return ret; 4040475f6387SJan Schmidt } 4041475f6387SJan Schmidt 4042475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 4043475f6387SJan Schmidt { 4044475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4045475f6387SJan Schmidt return -EPERM; 4046475f6387SJan Schmidt 4047aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 4048475f6387SJan Schmidt } 4049475f6387SJan Schmidt 4050475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 4051475f6387SJan Schmidt void __user *arg) 4052475f6387SJan Schmidt { 4053475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4054475f6387SJan Schmidt int ret; 4055475f6387SJan Schmidt 4056475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4057475f6387SJan Schmidt return -EPERM; 4058475f6387SJan Schmidt 4059475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4060475f6387SJan Schmidt if (IS_ERR(sa)) 4061475f6387SJan Schmidt return PTR_ERR(sa); 4062475f6387SJan Schmidt 4063475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 4064475f6387SJan Schmidt 4065475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4066475f6387SJan Schmidt ret = -EFAULT; 4067475f6387SJan Schmidt 4068475f6387SJan Schmidt kfree(sa); 4069475f6387SJan Schmidt return ret; 4070475f6387SJan Schmidt } 4071475f6387SJan Schmidt 4072c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 4073b27f7c0cSDavid Sterba void __user *arg) 4074c11d2c23SStefan Behrens { 4075c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4076c11d2c23SStefan Behrens int ret; 4077c11d2c23SStefan Behrens 4078c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4079c11d2c23SStefan Behrens if (IS_ERR(sa)) 4080c11d2c23SStefan Behrens return PTR_ERR(sa); 4081c11d2c23SStefan Behrens 4082b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4083b27f7c0cSDavid Sterba kfree(sa); 4084b27f7c0cSDavid Sterba return -EPERM; 4085b27f7c0cSDavid Sterba } 4086b27f7c0cSDavid Sterba 4087b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 4088c11d2c23SStefan Behrens 4089c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4090c11d2c23SStefan Behrens ret = -EFAULT; 4091c11d2c23SStefan Behrens 4092c11d2c23SStefan Behrens kfree(sa); 4093c11d2c23SStefan Behrens return ret; 4094c11d2c23SStefan Behrens } 4095c11d2c23SStefan Behrens 40963f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 40973f6bcfbdSStefan Behrens { 40983f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 40993f6bcfbdSStefan Behrens int ret; 41003f6bcfbdSStefan Behrens 41013f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 41023f6bcfbdSStefan Behrens return -EPERM; 41033f6bcfbdSStefan Behrens 41043f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 41053f6bcfbdSStefan Behrens if (IS_ERR(p)) 41063f6bcfbdSStefan Behrens return PTR_ERR(p); 41073f6bcfbdSStefan Behrens 41083f6bcfbdSStefan Behrens switch (p->cmd) { 41093f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4110adfa97cbSIlya Dryomov if (root->fs_info->sb->s_flags & MS_RDONLY) { 4111adfa97cbSIlya Dryomov ret = -EROFS; 4112adfa97cbSIlya Dryomov goto out; 4113adfa97cbSIlya Dryomov } 41143f6bcfbdSStefan Behrens if (atomic_xchg( 41153f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41163f6bcfbdSStefan Behrens 1)) { 4117e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 41183f6bcfbdSStefan Behrens } else { 41193f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 41203f6bcfbdSStefan Behrens atomic_set( 41213f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 41223f6bcfbdSStefan Behrens 0); 41233f6bcfbdSStefan Behrens } 41243f6bcfbdSStefan Behrens break; 41253f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 41263f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 41273f6bcfbdSStefan Behrens ret = 0; 41283f6bcfbdSStefan Behrens break; 41293f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 41303f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 41313f6bcfbdSStefan Behrens break; 41323f6bcfbdSStefan Behrens default: 41333f6bcfbdSStefan Behrens ret = -EINVAL; 41343f6bcfbdSStefan Behrens break; 41353f6bcfbdSStefan Behrens } 41363f6bcfbdSStefan Behrens 41373f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 41383f6bcfbdSStefan Behrens ret = -EFAULT; 4139adfa97cbSIlya Dryomov out: 41403f6bcfbdSStefan Behrens kfree(p); 41413f6bcfbdSStefan Behrens return ret; 41423f6bcfbdSStefan Behrens } 41433f6bcfbdSStefan Behrens 4144d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4145d7728c96SJan Schmidt { 4146d7728c96SJan Schmidt int ret = 0; 4147d7728c96SJan Schmidt int i; 4148740c3d22SChris Mason u64 rel_ptr; 4149d7728c96SJan Schmidt int size; 4150806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4151d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4152d7728c96SJan Schmidt struct btrfs_path *path; 4153d7728c96SJan Schmidt 415482b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4155d7728c96SJan Schmidt return -EPERM; 4156d7728c96SJan Schmidt 4157d7728c96SJan Schmidt path = btrfs_alloc_path(); 4158d7728c96SJan Schmidt if (!path) { 4159d7728c96SJan Schmidt ret = -ENOMEM; 4160d7728c96SJan Schmidt goto out; 4161d7728c96SJan Schmidt } 4162d7728c96SJan Schmidt 4163d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4164d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4165d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4166d7728c96SJan Schmidt ipa = NULL; 4167d7728c96SJan Schmidt goto out; 4168d7728c96SJan Schmidt } 4169d7728c96SJan Schmidt 4170d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4171d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4172d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4173d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4174d7728c96SJan Schmidt ipath = NULL; 4175d7728c96SJan Schmidt goto out; 4176d7728c96SJan Schmidt } 4177d7728c96SJan Schmidt 4178d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4179d7728c96SJan Schmidt if (ret < 0) 4180d7728c96SJan Schmidt goto out; 4181d7728c96SJan Schmidt 4182d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4183745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4184745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4185740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4186d7728c96SJan Schmidt } 4187d7728c96SJan Schmidt 4188745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 4189745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 4190d7728c96SJan Schmidt if (ret) { 4191d7728c96SJan Schmidt ret = -EFAULT; 4192d7728c96SJan Schmidt goto out; 4193d7728c96SJan Schmidt } 4194d7728c96SJan Schmidt 4195d7728c96SJan Schmidt out: 4196d7728c96SJan Schmidt btrfs_free_path(path); 4197d7728c96SJan Schmidt free_ipath(ipath); 4198d7728c96SJan Schmidt kfree(ipa); 4199d7728c96SJan Schmidt 4200d7728c96SJan Schmidt return ret; 4201d7728c96SJan Schmidt } 4202d7728c96SJan Schmidt 4203d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4204d7728c96SJan Schmidt { 4205d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4206d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4207d7728c96SJan Schmidt 4208d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4209d7728c96SJan Schmidt inodes->bytes_left -= c; 4210d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4211d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4212d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4213d7728c96SJan Schmidt inodes->elem_cnt += 3; 4214d7728c96SJan Schmidt } else { 4215d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4216d7728c96SJan Schmidt inodes->bytes_left = 0; 4217d7728c96SJan Schmidt inodes->elem_missed += 3; 4218d7728c96SJan Schmidt } 4219d7728c96SJan Schmidt 4220d7728c96SJan Schmidt return 0; 4221d7728c96SJan Schmidt } 4222d7728c96SJan Schmidt 4223d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 4224d7728c96SJan Schmidt void __user *arg) 4225d7728c96SJan Schmidt { 4226d7728c96SJan Schmidt int ret = 0; 4227d7728c96SJan Schmidt int size; 4228d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4229d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4230d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4231d7728c96SJan Schmidt 4232d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4233d7728c96SJan Schmidt return -EPERM; 4234d7728c96SJan Schmidt 4235d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 4236d7728c96SJan Schmidt if (IS_ERR(loi)) { 4237d7728c96SJan Schmidt ret = PTR_ERR(loi); 4238d7728c96SJan Schmidt loi = NULL; 4239d7728c96SJan Schmidt goto out; 4240d7728c96SJan Schmidt } 4241d7728c96SJan Schmidt 4242d7728c96SJan Schmidt path = btrfs_alloc_path(); 4243d7728c96SJan Schmidt if (!path) { 4244d7728c96SJan Schmidt ret = -ENOMEM; 4245d7728c96SJan Schmidt goto out; 4246d7728c96SJan Schmidt } 4247d7728c96SJan Schmidt 4248425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 4249d7728c96SJan Schmidt inodes = init_data_container(size); 4250d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4251d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4252d7728c96SJan Schmidt inodes = NULL; 4253d7728c96SJan Schmidt goto out; 4254d7728c96SJan Schmidt } 4255d7728c96SJan Schmidt 4256df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 4257df031f07SLiu Bo build_ino_list, inodes); 4258df031f07SLiu Bo if (ret == -EINVAL) 4259d7728c96SJan Schmidt ret = -ENOENT; 4260d7728c96SJan Schmidt if (ret < 0) 4261d7728c96SJan Schmidt goto out; 4262d7728c96SJan Schmidt 4263745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 4264745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 4265d7728c96SJan Schmidt if (ret) 4266d7728c96SJan Schmidt ret = -EFAULT; 4267d7728c96SJan Schmidt 4268d7728c96SJan Schmidt out: 4269d7728c96SJan Schmidt btrfs_free_path(path); 4270425d17a2SLiu Bo vfree(inodes); 4271d7728c96SJan Schmidt kfree(loi); 4272d7728c96SJan Schmidt 4273d7728c96SJan Schmidt return ret; 4274d7728c96SJan Schmidt } 4275d7728c96SJan Schmidt 427619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4277c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4278c9e9f97bSIlya Dryomov { 4279c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4280c9e9f97bSIlya Dryomov 4281c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4282c9e9f97bSIlya Dryomov 4283837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4284837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4285837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4286837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4287a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4288a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4289837d5b6eSIlya Dryomov 4290c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4291c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4292c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 429319a39dceSIlya Dryomov 429419a39dceSIlya Dryomov if (lock) { 429519a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 429619a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 429719a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 429819a39dceSIlya Dryomov } else { 429919a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 430019a39dceSIlya Dryomov } 4301c9e9f97bSIlya Dryomov } 4302c9e9f97bSIlya Dryomov 43039ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4304c9e9f97bSIlya Dryomov { 4305496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4306c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4307c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4308c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4309ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4310c9e9f97bSIlya Dryomov int ret; 4311c9e9f97bSIlya Dryomov 4312c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4313c9e9f97bSIlya Dryomov return -EPERM; 4314c9e9f97bSIlya Dryomov 4315e54bfa31SLiu Bo ret = mnt_want_write_file(file); 43169ba1f6e4SLiu Bo if (ret) 43179ba1f6e4SLiu Bo return ret; 43189ba1f6e4SLiu Bo 4319ed0fb78fSIlya Dryomov again: 4320ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 4321c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4322c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4323ed0fb78fSIlya Dryomov need_unlock = true; 4324ed0fb78fSIlya Dryomov goto locked; 4325ed0fb78fSIlya Dryomov } 4326ed0fb78fSIlya Dryomov 4327ed0fb78fSIlya Dryomov /* 4328ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 4329ed0fb78fSIlya Dryomov * (1) some other op is running 4330ed0fb78fSIlya Dryomov * (2) balance is running 4331ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4332ed0fb78fSIlya Dryomov */ 4333ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4334ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4335ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4336ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4337ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4338ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4339ed0fb78fSIlya Dryomov goto again; 4340ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4341ed0fb78fSIlya Dryomov 4342ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4343ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4344ed0fb78fSIlya Dryomov /* this is (3) */ 4345ed0fb78fSIlya Dryomov need_unlock = false; 4346ed0fb78fSIlya Dryomov goto locked; 4347ed0fb78fSIlya Dryomov } 4348ed0fb78fSIlya Dryomov 4349ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4350ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4351ed0fb78fSIlya Dryomov goto again; 4352ed0fb78fSIlya Dryomov } else { 4353ed0fb78fSIlya Dryomov /* this is (2) */ 4354ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4355ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4356ed0fb78fSIlya Dryomov goto out; 4357ed0fb78fSIlya Dryomov } 4358ed0fb78fSIlya Dryomov } else { 4359ed0fb78fSIlya Dryomov /* this is (1) */ 4360ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4361e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4362ed0fb78fSIlya Dryomov goto out; 4363ed0fb78fSIlya Dryomov } 4364ed0fb78fSIlya Dryomov 4365ed0fb78fSIlya Dryomov locked: 4366ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 4367c9e9f97bSIlya Dryomov 4368c9e9f97bSIlya Dryomov if (arg) { 4369c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4370c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4371c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4372ed0fb78fSIlya Dryomov goto out_unlock; 4373c9e9f97bSIlya Dryomov } 4374de322263SIlya Dryomov 4375de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4376de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4377de322263SIlya Dryomov ret = -ENOTCONN; 4378de322263SIlya Dryomov goto out_bargs; 4379de322263SIlya Dryomov } 4380de322263SIlya Dryomov 4381de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4382de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4383de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4384de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4385de322263SIlya Dryomov 4386de322263SIlya Dryomov goto do_balance; 4387de322263SIlya Dryomov } 4388c9e9f97bSIlya Dryomov } else { 4389c9e9f97bSIlya Dryomov bargs = NULL; 4390c9e9f97bSIlya Dryomov } 4391c9e9f97bSIlya Dryomov 4392ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4393837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4394837d5b6eSIlya Dryomov goto out_bargs; 4395837d5b6eSIlya Dryomov } 4396837d5b6eSIlya Dryomov 4397c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 4398c9e9f97bSIlya Dryomov if (!bctl) { 4399c9e9f97bSIlya Dryomov ret = -ENOMEM; 4400c9e9f97bSIlya Dryomov goto out_bargs; 4401c9e9f97bSIlya Dryomov } 4402c9e9f97bSIlya Dryomov 4403c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4404c9e9f97bSIlya Dryomov if (arg) { 4405c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4406c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4407c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4408c9e9f97bSIlya Dryomov 4409c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4410f43ffb60SIlya Dryomov } else { 4411f43ffb60SIlya Dryomov /* balance everything - no filters */ 4412f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4413c9e9f97bSIlya Dryomov } 4414c9e9f97bSIlya Dryomov 4415de322263SIlya Dryomov do_balance: 4416c9e9f97bSIlya Dryomov /* 4417ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 4418ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4419ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4420ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 4421ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 4422c9e9f97bSIlya Dryomov */ 4423ed0fb78fSIlya Dryomov need_unlock = false; 4424ed0fb78fSIlya Dryomov 4425ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 4426ed0fb78fSIlya Dryomov 4427c9e9f97bSIlya Dryomov if (arg) { 4428c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4429c9e9f97bSIlya Dryomov ret = -EFAULT; 4430c9e9f97bSIlya Dryomov } 4431c9e9f97bSIlya Dryomov 4432c9e9f97bSIlya Dryomov out_bargs: 4433c9e9f97bSIlya Dryomov kfree(bargs); 4434ed0fb78fSIlya Dryomov out_unlock: 4435c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4436c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4437ed0fb78fSIlya Dryomov if (need_unlock) 4438ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 4439ed0fb78fSIlya Dryomov out: 4440e54bfa31SLiu Bo mnt_drop_write_file(file); 4441c9e9f97bSIlya Dryomov return ret; 4442c9e9f97bSIlya Dryomov } 4443c9e9f97bSIlya Dryomov 4444837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4445837d5b6eSIlya Dryomov { 4446837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4447837d5b6eSIlya Dryomov return -EPERM; 4448837d5b6eSIlya Dryomov 4449837d5b6eSIlya Dryomov switch (cmd) { 4450837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 4451837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 4452a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 4453a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 4454837d5b6eSIlya Dryomov } 4455837d5b6eSIlya Dryomov 4456837d5b6eSIlya Dryomov return -EINVAL; 4457837d5b6eSIlya Dryomov } 4458837d5b6eSIlya Dryomov 445919a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 446019a39dceSIlya Dryomov void __user *arg) 446119a39dceSIlya Dryomov { 446219a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 446319a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 446419a39dceSIlya Dryomov int ret = 0; 446519a39dceSIlya Dryomov 446619a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 446719a39dceSIlya Dryomov return -EPERM; 446819a39dceSIlya Dryomov 446919a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 447019a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 447119a39dceSIlya Dryomov ret = -ENOTCONN; 447219a39dceSIlya Dryomov goto out; 447319a39dceSIlya Dryomov } 447419a39dceSIlya Dryomov 447519a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 447619a39dceSIlya Dryomov if (!bargs) { 447719a39dceSIlya Dryomov ret = -ENOMEM; 447819a39dceSIlya Dryomov goto out; 447919a39dceSIlya Dryomov } 448019a39dceSIlya Dryomov 448119a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 448219a39dceSIlya Dryomov 448319a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 448419a39dceSIlya Dryomov ret = -EFAULT; 448519a39dceSIlya Dryomov 448619a39dceSIlya Dryomov kfree(bargs); 448719a39dceSIlya Dryomov out: 448819a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 448919a39dceSIlya Dryomov return ret; 449019a39dceSIlya Dryomov } 449119a39dceSIlya Dryomov 4492905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 44935d13a37bSArne Jansen { 4494496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 44955d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 44965d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 44975d13a37bSArne Jansen int ret; 44985d13a37bSArne Jansen int err; 44995d13a37bSArne Jansen 45005d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45015d13a37bSArne Jansen return -EPERM; 45025d13a37bSArne Jansen 4503905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4504905b0ddaSMiao Xie if (ret) 4505905b0ddaSMiao Xie return ret; 45065d13a37bSArne Jansen 45075d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4508905b0ddaSMiao Xie if (IS_ERR(sa)) { 4509905b0ddaSMiao Xie ret = PTR_ERR(sa); 4510905b0ddaSMiao Xie goto drop_write; 4511905b0ddaSMiao Xie } 45125d13a37bSArne Jansen 45137708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 451492f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 45155d13a37bSArne Jansen if (IS_ERR(trans)) { 45165d13a37bSArne Jansen ret = PTR_ERR(trans); 45175d13a37bSArne Jansen goto out; 45185d13a37bSArne Jansen } 45195d13a37bSArne Jansen 45205d13a37bSArne Jansen switch (sa->cmd) { 45215d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 45225d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 45235d13a37bSArne Jansen break; 45245d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 45255d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 45265d13a37bSArne Jansen break; 45275d13a37bSArne Jansen default: 45285d13a37bSArne Jansen ret = -EINVAL; 45295d13a37bSArne Jansen break; 45305d13a37bSArne Jansen } 45315d13a37bSArne Jansen 453292f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 45335d13a37bSArne Jansen if (err && !ret) 45345d13a37bSArne Jansen ret = err; 45355d13a37bSArne Jansen out: 45365d13a37bSArne Jansen kfree(sa); 45377708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 4538905b0ddaSMiao Xie drop_write: 4539905b0ddaSMiao Xie mnt_drop_write_file(file); 45405d13a37bSArne Jansen return ret; 45415d13a37bSArne Jansen } 45425d13a37bSArne Jansen 4543905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 45445d13a37bSArne Jansen { 4545496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45465d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 45475d13a37bSArne Jansen struct btrfs_trans_handle *trans; 45485d13a37bSArne Jansen int ret; 45495d13a37bSArne Jansen int err; 45505d13a37bSArne Jansen 45515d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45525d13a37bSArne Jansen return -EPERM; 45535d13a37bSArne Jansen 4554905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4555905b0ddaSMiao Xie if (ret) 4556905b0ddaSMiao Xie return ret; 45575d13a37bSArne Jansen 45585d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4559905b0ddaSMiao Xie if (IS_ERR(sa)) { 4560905b0ddaSMiao Xie ret = PTR_ERR(sa); 4561905b0ddaSMiao Xie goto drop_write; 4562905b0ddaSMiao Xie } 45635d13a37bSArne Jansen 45645d13a37bSArne Jansen trans = btrfs_join_transaction(root); 45655d13a37bSArne Jansen if (IS_ERR(trans)) { 45665d13a37bSArne Jansen ret = PTR_ERR(trans); 45675d13a37bSArne Jansen goto out; 45685d13a37bSArne Jansen } 45695d13a37bSArne Jansen 45705d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 45715d13a37bSArne Jansen if (sa->assign) { 45725d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 45735d13a37bSArne Jansen sa->src, sa->dst); 45745d13a37bSArne Jansen } else { 45755d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 45765d13a37bSArne Jansen sa->src, sa->dst); 45775d13a37bSArne Jansen } 45785d13a37bSArne Jansen 45795d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 45805d13a37bSArne Jansen if (err && !ret) 45815d13a37bSArne Jansen ret = err; 45825d13a37bSArne Jansen 45835d13a37bSArne Jansen out: 45845d13a37bSArne Jansen kfree(sa); 4585905b0ddaSMiao Xie drop_write: 4586905b0ddaSMiao Xie mnt_drop_write_file(file); 45875d13a37bSArne Jansen return ret; 45885d13a37bSArne Jansen } 45895d13a37bSArne Jansen 4590905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 45915d13a37bSArne Jansen { 4592496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45935d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 45945d13a37bSArne Jansen struct btrfs_trans_handle *trans; 45955d13a37bSArne Jansen int ret; 45965d13a37bSArne Jansen int err; 45975d13a37bSArne Jansen 45985d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 45995d13a37bSArne Jansen return -EPERM; 46005d13a37bSArne Jansen 4601905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4602905b0ddaSMiao Xie if (ret) 4603905b0ddaSMiao Xie return ret; 46045d13a37bSArne Jansen 46055d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4606905b0ddaSMiao Xie if (IS_ERR(sa)) { 4607905b0ddaSMiao Xie ret = PTR_ERR(sa); 4608905b0ddaSMiao Xie goto drop_write; 4609905b0ddaSMiao Xie } 46105d13a37bSArne Jansen 4611d86e56cfSMiao Xie if (!sa->qgroupid) { 4612d86e56cfSMiao Xie ret = -EINVAL; 4613d86e56cfSMiao Xie goto out; 4614d86e56cfSMiao Xie } 4615d86e56cfSMiao Xie 46165d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46175d13a37bSArne Jansen if (IS_ERR(trans)) { 46185d13a37bSArne Jansen ret = PTR_ERR(trans); 46195d13a37bSArne Jansen goto out; 46205d13a37bSArne Jansen } 46215d13a37bSArne Jansen 46225d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 46235d13a37bSArne Jansen if (sa->create) { 46245d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 46255d13a37bSArne Jansen NULL); 46265d13a37bSArne Jansen } else { 46275d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 46285d13a37bSArne Jansen } 46295d13a37bSArne Jansen 46305d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46315d13a37bSArne Jansen if (err && !ret) 46325d13a37bSArne Jansen ret = err; 46335d13a37bSArne Jansen 46345d13a37bSArne Jansen out: 46355d13a37bSArne Jansen kfree(sa); 4636905b0ddaSMiao Xie drop_write: 4637905b0ddaSMiao Xie mnt_drop_write_file(file); 46385d13a37bSArne Jansen return ret; 46395d13a37bSArne Jansen } 46405d13a37bSArne Jansen 4641905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 46425d13a37bSArne Jansen { 4643496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46445d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 46455d13a37bSArne Jansen struct btrfs_trans_handle *trans; 46465d13a37bSArne Jansen int ret; 46475d13a37bSArne Jansen int err; 46485d13a37bSArne Jansen u64 qgroupid; 46495d13a37bSArne Jansen 46505d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 46515d13a37bSArne Jansen return -EPERM; 46525d13a37bSArne Jansen 4653905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4654905b0ddaSMiao Xie if (ret) 4655905b0ddaSMiao Xie return ret; 46565d13a37bSArne Jansen 46575d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4658905b0ddaSMiao Xie if (IS_ERR(sa)) { 4659905b0ddaSMiao Xie ret = PTR_ERR(sa); 4660905b0ddaSMiao Xie goto drop_write; 4661905b0ddaSMiao Xie } 46625d13a37bSArne Jansen 46635d13a37bSArne Jansen trans = btrfs_join_transaction(root); 46645d13a37bSArne Jansen if (IS_ERR(trans)) { 46655d13a37bSArne Jansen ret = PTR_ERR(trans); 46665d13a37bSArne Jansen goto out; 46675d13a37bSArne Jansen } 46685d13a37bSArne Jansen 46695d13a37bSArne Jansen qgroupid = sa->qgroupid; 46705d13a37bSArne Jansen if (!qgroupid) { 46715d13a37bSArne Jansen /* take the current subvol as qgroup */ 46725d13a37bSArne Jansen qgroupid = root->root_key.objectid; 46735d13a37bSArne Jansen } 46745d13a37bSArne Jansen 46755d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 46765d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 46775d13a37bSArne Jansen 46785d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 46795d13a37bSArne Jansen if (err && !ret) 46805d13a37bSArne Jansen ret = err; 46815d13a37bSArne Jansen 46825d13a37bSArne Jansen out: 46835d13a37bSArne Jansen kfree(sa); 4684905b0ddaSMiao Xie drop_write: 4685905b0ddaSMiao Xie mnt_drop_write_file(file); 46865d13a37bSArne Jansen return ret; 46875d13a37bSArne Jansen } 46885d13a37bSArne Jansen 46892f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 46902f232036SJan Schmidt { 46916d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46922f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 46932f232036SJan Schmidt int ret; 46942f232036SJan Schmidt 46952f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 46962f232036SJan Schmidt return -EPERM; 46972f232036SJan Schmidt 46982f232036SJan Schmidt ret = mnt_want_write_file(file); 46992f232036SJan Schmidt if (ret) 47002f232036SJan Schmidt return ret; 47012f232036SJan Schmidt 47022f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 47032f232036SJan Schmidt if (IS_ERR(qsa)) { 47042f232036SJan Schmidt ret = PTR_ERR(qsa); 47052f232036SJan Schmidt goto drop_write; 47062f232036SJan Schmidt } 47072f232036SJan Schmidt 47082f232036SJan Schmidt if (qsa->flags) { 47092f232036SJan Schmidt ret = -EINVAL; 47102f232036SJan Schmidt goto out; 47112f232036SJan Schmidt } 47122f232036SJan Schmidt 47132f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 47142f232036SJan Schmidt 47152f232036SJan Schmidt out: 47162f232036SJan Schmidt kfree(qsa); 47172f232036SJan Schmidt drop_write: 47182f232036SJan Schmidt mnt_drop_write_file(file); 47192f232036SJan Schmidt return ret; 47202f232036SJan Schmidt } 47212f232036SJan Schmidt 47222f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 47232f232036SJan Schmidt { 47246d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 47252f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 47262f232036SJan Schmidt int ret = 0; 47272f232036SJan Schmidt 47282f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 47292f232036SJan Schmidt return -EPERM; 47302f232036SJan Schmidt 47312f232036SJan Schmidt qsa = kzalloc(sizeof(*qsa), GFP_NOFS); 47322f232036SJan Schmidt if (!qsa) 47332f232036SJan Schmidt return -ENOMEM; 47342f232036SJan Schmidt 47352f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 47362f232036SJan Schmidt qsa->flags = 1; 47372f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 47382f232036SJan Schmidt } 47392f232036SJan Schmidt 47402f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 47412f232036SJan Schmidt ret = -EFAULT; 47422f232036SJan Schmidt 47432f232036SJan Schmidt kfree(qsa); 47442f232036SJan Schmidt return ret; 47452f232036SJan Schmidt } 47462f232036SJan Schmidt 474757254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 474857254b6eSJan Schmidt { 474954563d41SAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 475057254b6eSJan Schmidt 475157254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 475257254b6eSJan Schmidt return -EPERM; 475357254b6eSJan Schmidt 475457254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 475557254b6eSJan Schmidt } 475657254b6eSJan Schmidt 4757abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 4758abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 47598ea05e3aSAlexander Block { 4760496ad9aaSAl Viro struct inode *inode = file_inode(file); 47618ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 47628ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 47638ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 47648ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 47658ea05e3aSAlexander Block int ret = 0; 4766dd5f9615SStefan Behrens int received_uuid_changed; 47678ea05e3aSAlexander Block 4768bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 4769bd60ea0fSDavid Sterba return -EPERM; 4770bd60ea0fSDavid Sterba 47718ea05e3aSAlexander Block ret = mnt_want_write_file(file); 47728ea05e3aSAlexander Block if (ret < 0) 47738ea05e3aSAlexander Block return ret; 47748ea05e3aSAlexander Block 47758ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 47768ea05e3aSAlexander Block 47778ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 47788ea05e3aSAlexander Block ret = -EINVAL; 47798ea05e3aSAlexander Block goto out; 47808ea05e3aSAlexander Block } 47818ea05e3aSAlexander Block 47828ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 47838ea05e3aSAlexander Block ret = -EROFS; 47848ea05e3aSAlexander Block goto out; 47858ea05e3aSAlexander Block } 47868ea05e3aSAlexander Block 4787dd5f9615SStefan Behrens /* 4788dd5f9615SStefan Behrens * 1 - root item 4789dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4790dd5f9615SStefan Behrens */ 4791dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 47928ea05e3aSAlexander Block if (IS_ERR(trans)) { 47938ea05e3aSAlexander Block ret = PTR_ERR(trans); 47948ea05e3aSAlexander Block trans = NULL; 47958ea05e3aSAlexander Block goto out; 47968ea05e3aSAlexander Block } 47978ea05e3aSAlexander Block 47988ea05e3aSAlexander Block sa->rtransid = trans->transid; 47998ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 48008ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 48018ea05e3aSAlexander Block 4802dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4803dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4804dd5f9615SStefan Behrens if (received_uuid_changed && 4805dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 4806dd5f9615SStefan Behrens btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 4807dd5f9615SStefan Behrens root_item->received_uuid, 4808dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4809dd5f9615SStefan Behrens root->root_key.objectid); 48108ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 48118ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 48128ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 48133cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 48143cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 48153cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 48163cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 48178ea05e3aSAlexander Block 48188ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 48198ea05e3aSAlexander Block &root->root_key, &root->root_item); 48208ea05e3aSAlexander Block if (ret < 0) { 48218ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 48228ea05e3aSAlexander Block goto out; 4823dd5f9615SStefan Behrens } 4824dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4825dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 4826dd5f9615SStefan Behrens sa->uuid, 4827dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4828dd5f9615SStefan Behrens root->root_key.objectid); 4829dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 4830dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 4831dd5f9615SStefan Behrens goto out; 4832dd5f9615SStefan Behrens } 4833dd5f9615SStefan Behrens } 48348ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 4835dd5f9615SStefan Behrens if (ret < 0) { 4836dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 48378ea05e3aSAlexander Block goto out; 48388ea05e3aSAlexander Block } 48398ea05e3aSAlexander Block 4840abccd00fSHugo Mills out: 4841abccd00fSHugo Mills up_write(&root->fs_info->subvol_sem); 4842abccd00fSHugo Mills mnt_drop_write_file(file); 4843abccd00fSHugo Mills return ret; 4844abccd00fSHugo Mills } 4845abccd00fSHugo Mills 4846abccd00fSHugo Mills #ifdef CONFIG_64BIT 4847abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 4848abccd00fSHugo Mills void __user *arg) 4849abccd00fSHugo Mills { 4850abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 4851abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 4852abccd00fSHugo Mills int ret = 0; 4853abccd00fSHugo Mills 4854abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 4855abccd00fSHugo Mills if (IS_ERR(args32)) { 4856abccd00fSHugo Mills ret = PTR_ERR(args32); 4857abccd00fSHugo Mills args32 = NULL; 4858abccd00fSHugo Mills goto out; 4859abccd00fSHugo Mills } 4860abccd00fSHugo Mills 4861abccd00fSHugo Mills args64 = kmalloc(sizeof(*args64), GFP_NOFS); 486284dbeb87SDan Carpenter if (!args64) { 486384dbeb87SDan Carpenter ret = -ENOMEM; 4864abccd00fSHugo Mills goto out; 4865abccd00fSHugo Mills } 4866abccd00fSHugo Mills 4867abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 4868abccd00fSHugo Mills args64->stransid = args32->stransid; 4869abccd00fSHugo Mills args64->rtransid = args32->rtransid; 4870abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 4871abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 4872abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 4873abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 4874abccd00fSHugo Mills args64->flags = args32->flags; 4875abccd00fSHugo Mills 4876abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 4877abccd00fSHugo Mills if (ret) 4878abccd00fSHugo Mills goto out; 4879abccd00fSHugo Mills 4880abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 4881abccd00fSHugo Mills args32->stransid = args64->stransid; 4882abccd00fSHugo Mills args32->rtransid = args64->rtransid; 4883abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 4884abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 4885abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 4886abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 4887abccd00fSHugo Mills args32->flags = args64->flags; 4888abccd00fSHugo Mills 4889abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 4890abccd00fSHugo Mills if (ret) 4891abccd00fSHugo Mills ret = -EFAULT; 4892abccd00fSHugo Mills 4893abccd00fSHugo Mills out: 4894abccd00fSHugo Mills kfree(args32); 4895abccd00fSHugo Mills kfree(args64); 4896abccd00fSHugo Mills return ret; 4897abccd00fSHugo Mills } 4898abccd00fSHugo Mills #endif 4899abccd00fSHugo Mills 4900abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 4901abccd00fSHugo Mills void __user *arg) 4902abccd00fSHugo Mills { 4903abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 4904abccd00fSHugo Mills int ret = 0; 4905abccd00fSHugo Mills 4906abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 4907abccd00fSHugo Mills if (IS_ERR(sa)) { 4908abccd00fSHugo Mills ret = PTR_ERR(sa); 4909abccd00fSHugo Mills sa = NULL; 4910abccd00fSHugo Mills goto out; 4911abccd00fSHugo Mills } 4912abccd00fSHugo Mills 4913abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 4914abccd00fSHugo Mills 4915abccd00fSHugo Mills if (ret) 4916abccd00fSHugo Mills goto out; 4917abccd00fSHugo Mills 49188ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 49198ea05e3aSAlexander Block if (ret) 49208ea05e3aSAlexander Block ret = -EFAULT; 49218ea05e3aSAlexander Block 49228ea05e3aSAlexander Block out: 49238ea05e3aSAlexander Block kfree(sa); 49248ea05e3aSAlexander Block return ret; 49258ea05e3aSAlexander Block } 49268ea05e3aSAlexander Block 4927867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 4928867ab667Sjeff.liu { 49296d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4930a1b83ac5SAnand Jain size_t len; 4931867ab667Sjeff.liu int ret; 4932a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4933a1b83ac5SAnand Jain 4934a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4935a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 4936a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4937a1b83ac5SAnand Jain 4938a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4939867ab667Sjeff.liu 4940867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 4941efe120a0SFrank Holton btrfs_warn(root->fs_info, 4942efe120a0SFrank Holton "label is too long, return the first %zu bytes", --len); 4943867ab667Sjeff.liu } 4944867ab667Sjeff.liu 4945867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4946867ab667Sjeff.liu 4947867ab667Sjeff.liu return ret ? -EFAULT : 0; 4948867ab667Sjeff.liu } 4949867ab667Sjeff.liu 4950a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4951a8bfd4abSjeff.liu { 49526d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4953a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 4954a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4955a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4956a8bfd4abSjeff.liu int ret; 4957a8bfd4abSjeff.liu 4958a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4959a8bfd4abSjeff.liu return -EPERM; 4960a8bfd4abSjeff.liu 4961a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4962a8bfd4abSjeff.liu return -EFAULT; 4963a8bfd4abSjeff.liu 4964a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 4965efe120a0SFrank Holton btrfs_err(root->fs_info, "unable to set label with more than %d bytes", 4966a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4967a8bfd4abSjeff.liu return -EINVAL; 4968a8bfd4abSjeff.liu } 4969a8bfd4abSjeff.liu 4970a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4971a8bfd4abSjeff.liu if (ret) 4972a8bfd4abSjeff.liu return ret; 4973a8bfd4abSjeff.liu 4974a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4975a8bfd4abSjeff.liu if (IS_ERR(trans)) { 4976a8bfd4abSjeff.liu ret = PTR_ERR(trans); 4977a8bfd4abSjeff.liu goto out_unlock; 4978a8bfd4abSjeff.liu } 4979a8bfd4abSjeff.liu 4980a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4981a8bfd4abSjeff.liu strcpy(super_block->label, label); 4982a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4983d0270acaSJeff Mahoney ret = btrfs_commit_transaction(trans, root); 4984a8bfd4abSjeff.liu 4985a8bfd4abSjeff.liu out_unlock: 4986a8bfd4abSjeff.liu mnt_drop_write_file(file); 4987a8bfd4abSjeff.liu return ret; 4988a8bfd4abSjeff.liu } 4989a8bfd4abSjeff.liu 49902eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 49912eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 49922eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 49932eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 49942eaa055fSJeff Mahoney 49952eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file, 49962eaa055fSJeff Mahoney void __user *arg) 49972eaa055fSJeff Mahoney { 49982eaa055fSJeff Mahoney static struct btrfs_ioctl_feature_flags features[3] = { 49992eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 50002eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 50012eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 50022eaa055fSJeff Mahoney }; 50032eaa055fSJeff Mahoney 50042eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50052eaa055fSJeff Mahoney return -EFAULT; 50062eaa055fSJeff Mahoney 50072eaa055fSJeff Mahoney return 0; 50082eaa055fSJeff Mahoney } 50092eaa055fSJeff Mahoney 50102eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 50112eaa055fSJeff Mahoney { 50122eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 50132eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 50142eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 50152eaa055fSJeff Mahoney 50162eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 50172eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 50182eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 50192eaa055fSJeff Mahoney 50202eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 50212eaa055fSJeff Mahoney return -EFAULT; 50222eaa055fSJeff Mahoney 50232eaa055fSJeff Mahoney return 0; 50242eaa055fSJeff Mahoney } 50252eaa055fSJeff Mahoney 50263b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root, 50273b02a68aSJeff Mahoney enum btrfs_feature_set set, 50282eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 50292eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 50302eaa055fSJeff Mahoney { 50313b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 50323b02a68aSJeff Mahoney char *names; 50332eaa055fSJeff Mahoney u64 disallowed, unsupported; 50342eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 50352eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 50362eaa055fSJeff Mahoney 50372eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 50382eaa055fSJeff Mahoney if (unsupported) { 50393b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 50403b02a68aSJeff Mahoney if (names) { 50413b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50423b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 50433b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50443b02a68aSJeff Mahoney kfree(names); 50453b02a68aSJeff Mahoney } else 50462eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50472eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 50482eaa055fSJeff Mahoney type, unsupported); 50492eaa055fSJeff Mahoney return -EOPNOTSUPP; 50502eaa055fSJeff Mahoney } 50512eaa055fSJeff Mahoney 50522eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 50532eaa055fSJeff Mahoney if (disallowed) { 50543b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50553b02a68aSJeff Mahoney if (names) { 50563b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50573b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 50583b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50593b02a68aSJeff Mahoney kfree(names); 50603b02a68aSJeff Mahoney } else 50612eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50622eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 50632eaa055fSJeff Mahoney type, disallowed); 50642eaa055fSJeff Mahoney return -EPERM; 50652eaa055fSJeff Mahoney } 50662eaa055fSJeff Mahoney 50672eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 50682eaa055fSJeff Mahoney if (disallowed) { 50693b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 50703b02a68aSJeff Mahoney if (names) { 50713b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 50723b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 50733b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 50743b02a68aSJeff Mahoney kfree(names); 50753b02a68aSJeff Mahoney } else 50762eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 50772eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 50782eaa055fSJeff Mahoney type, disallowed); 50792eaa055fSJeff Mahoney return -EPERM; 50802eaa055fSJeff Mahoney } 50812eaa055fSJeff Mahoney 50822eaa055fSJeff Mahoney return 0; 50832eaa055fSJeff Mahoney } 50842eaa055fSJeff Mahoney 50852eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base) \ 50863b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 50872eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 50882eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 50892eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 50902eaa055fSJeff Mahoney 50912eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 50922eaa055fSJeff Mahoney { 50932eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 50942eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 50952eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 50962eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 50972eaa055fSJeff Mahoney u64 newflags; 50982eaa055fSJeff Mahoney int ret; 50992eaa055fSJeff Mahoney 51002eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 51012eaa055fSJeff Mahoney return -EPERM; 51022eaa055fSJeff Mahoney 51032eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 51042eaa055fSJeff Mahoney return -EFAULT; 51052eaa055fSJeff Mahoney 51062eaa055fSJeff Mahoney /* Nothing to do */ 51072eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 51082eaa055fSJeff Mahoney !flags[0].incompat_flags) 51092eaa055fSJeff Mahoney return 0; 51102eaa055fSJeff Mahoney 51112eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_flags, 51122eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 51132eaa055fSJeff Mahoney if (ret) 51142eaa055fSJeff Mahoney return ret; 51152eaa055fSJeff Mahoney 51162eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_ro_flags, 51172eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 51182eaa055fSJeff Mahoney if (ret) 51192eaa055fSJeff Mahoney return ret; 51202eaa055fSJeff Mahoney 51212eaa055fSJeff Mahoney ret = check_feature(root, flags[0].incompat_flags, 51222eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 51232eaa055fSJeff Mahoney if (ret) 51242eaa055fSJeff Mahoney return ret; 51252eaa055fSJeff Mahoney 51268051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 51272eaa055fSJeff Mahoney if (IS_ERR(trans)) 51282eaa055fSJeff Mahoney return PTR_ERR(trans); 51292eaa055fSJeff Mahoney 51302eaa055fSJeff Mahoney spin_lock(&root->fs_info->super_lock); 51312eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 51322eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 51332eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 51342eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 51352eaa055fSJeff Mahoney 51362eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 51372eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 51382eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 51392eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 51402eaa055fSJeff Mahoney 51412eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 51422eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 51432eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 51442eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 51452eaa055fSJeff Mahoney spin_unlock(&root->fs_info->super_lock); 51462eaa055fSJeff Mahoney 5147d0270acaSJeff Mahoney return btrfs_commit_transaction(trans, root); 51482eaa055fSJeff Mahoney } 51492eaa055fSJeff Mahoney 5150f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5151f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5152f46b5a66SChristoph Hellwig { 5153496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 51544bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5155f46b5a66SChristoph Hellwig 5156f46b5a66SChristoph Hellwig switch (cmd) { 51576cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 51586cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 51596cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 51606cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 51616cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 51626cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5163f7039b1dSLi Dongyang case FITRIM: 5164f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5165f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5166fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5167fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5168fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 51693de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5170fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 51716f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 51726f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 517376dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 517476dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 51750caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 51760caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 51770caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 51780caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 51796ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 51806ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5181f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 51821e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 51831e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 51841e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5185f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5186198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5187f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 51884bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 5189f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5190da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 5191475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 5192475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 5193475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 5194475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 5195f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 51969ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5197f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 5198c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 5199c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 52007a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 5201f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 5202f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 5203f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 5204f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 5205ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5206ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5207ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5208ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5209d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5210d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5211d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5212d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 52131406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 52141406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 52159b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 52169b199859SFilipe David Borba Manana int ret; 52179b199859SFilipe David Borba Manana 52186c255e67SMiao Xie ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 52199b199859SFilipe David Borba Manana if (ret) 52209b199859SFilipe David Borba Manana return ret; 52219b199859SFilipe David Borba Manana ret = btrfs_sync_fs(file->f_dentry->d_sb, 1); 52229b199859SFilipe David Borba Manana return ret; 52239b199859SFilipe David Borba Manana } 522446204592SSage Weil case BTRFS_IOC_START_SYNC: 52259a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 522646204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 52279a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 5228475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5229b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5230475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 5231475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 5232475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 5233475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 5234c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 52359ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5236837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 5237837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 523819a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 523919a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 52408ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 52418ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5242abccd00fSHugo Mills #ifdef CONFIG_64BIT 5243abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5244abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5245abccd00fSHugo Mills #endif 524631db9f7cSAlexander Block case BTRFS_IOC_SEND: 524731db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 5248c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 5249b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 52505d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5251905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 52525d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5253905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 52545d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5255905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 52565d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5257905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 52582f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 52592f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 52602f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 52612f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 526257254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 526357254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 52643f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 52653f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 5266867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5267867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5268a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5269a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 5270416161dbSMark Fasheh case BTRFS_IOC_FILE_EXTENT_SAME: 5271416161dbSMark Fasheh return btrfs_ioctl_file_extent_same(file, argp); 52722eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 52732eaa055fSJeff Mahoney return btrfs_ioctl_get_supported_features(file, argp); 52742eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 52752eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 52762eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 52772eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5278f46b5a66SChristoph Hellwig } 5279f46b5a66SChristoph Hellwig 5280f46b5a66SChristoph Hellwig return -ENOTTY; 5281f46b5a66SChristoph Hellwig } 5282