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/compat.h> 37f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h> 38cb8e7090SChristoph Hellwig #include <linux/security.h> 39f46b5a66SChristoph Hellwig #include <linux/xattr.h> 40f54de068SDavid Sterba #include <linux/mm.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 42f7039b1dSLi Dongyang #include <linux/blkdev.h> 438ea05e3aSAlexander Block #include <linux/uuid.h> 4455e301fdSFilipe Brandenburger #include <linux/btrfs.h> 45416161dbSMark Fasheh #include <linux/uaccess.h> 46f46b5a66SChristoph Hellwig #include "ctree.h" 47f46b5a66SChristoph Hellwig #include "disk-io.h" 48f46b5a66SChristoph Hellwig #include "transaction.h" 49f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 50f46b5a66SChristoph Hellwig #include "print-tree.h" 51f46b5a66SChristoph Hellwig #include "volumes.h" 52925baeddSChris Mason #include "locking.h" 53581bb050SLi Zefan #include "inode-map.h" 54d7728c96SJan Schmidt #include "backref.h" 55606686eeSJosef Bacik #include "rcu-string.h" 5631db9f7cSAlexander Block #include "send.h" 573f6bcfbdSStefan Behrens #include "dev-replace.h" 5863541927SFilipe David Borba Manana #include "props.h" 593b02a68aSJeff Mahoney #include "sysfs.h" 60fcebe456SJosef Bacik #include "qgroup.h" 611ec9a1aeSFilipe Manana #include "tree-log.h" 62ebb8765bSAnand Jain #include "compression.h" 63f46b5a66SChristoph Hellwig 64abccd00fSHugo Mills #ifdef CONFIG_64BIT 65abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 66abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 67abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 68abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 69abccd00fSHugo Mills */ 70abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 71abccd00fSHugo Mills __u64 sec; 72abccd00fSHugo Mills __u32 nsec; 73abccd00fSHugo Mills } __attribute__ ((__packed__)); 74abccd00fSHugo Mills 75abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 76abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 77abccd00fSHugo Mills __u64 stransid; /* in */ 78abccd00fSHugo Mills __u64 rtransid; /* out */ 79abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 80abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 81abccd00fSHugo Mills __u64 flags; /* in */ 82abccd00fSHugo Mills __u64 reserved[16]; /* in */ 83abccd00fSHugo Mills } __attribute__ ((__packed__)); 84abccd00fSHugo Mills 85abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 86abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 87abccd00fSHugo Mills #endif 88abccd00fSHugo Mills 892351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 902351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 912351f431SJosef Bacik __s64 send_fd; /* in */ 922351f431SJosef Bacik __u64 clone_sources_count; /* in */ 932351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 942351f431SJosef Bacik __u64 parent_root; /* in */ 952351f431SJosef Bacik __u64 flags; /* in */ 962351f431SJosef Bacik __u64 reserved[4]; /* in */ 972351f431SJosef Bacik } __attribute__ ((__packed__)); 982351f431SJosef Bacik 992351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 1002351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 1012351f431SJosef Bacik #endif 102abccd00fSHugo Mills 103416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 1041c919a5eSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff, 1051c919a5eSMark Fasheh int no_time_update); 106416161dbSMark Fasheh 1076cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 1086cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 1096cbff00fSChristoph Hellwig { 1106cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 1116cbff00fSChristoph Hellwig return flags; 1126cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 1136cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 1146cbff00fSChristoph Hellwig else 1156cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1166cbff00fSChristoph Hellwig } 117f46b5a66SChristoph Hellwig 1186cbff00fSChristoph Hellwig /* 1196cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 1206cbff00fSChristoph Hellwig */ 1216cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 1226cbff00fSChristoph Hellwig { 1236cbff00fSChristoph Hellwig unsigned int iflags = 0; 1246cbff00fSChristoph Hellwig 1256cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1266cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1276cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1286cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1296cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1306cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1316cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1326cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1336cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1346cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1356cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1366cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 137d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 138d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 139d0092bddSLi Zefan 14013f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 141d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 14213f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 14313f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1446cbff00fSChristoph Hellwig 1456cbff00fSChristoph Hellwig return iflags; 1466cbff00fSChristoph Hellwig } 1476cbff00fSChristoph Hellwig 1486cbff00fSChristoph Hellwig /* 1496cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1506cbff00fSChristoph Hellwig */ 1516cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1526cbff00fSChristoph Hellwig { 1536cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1543cc79392SFilipe Manana unsigned int new_fl = 0; 1556cbff00fSChristoph Hellwig 1566cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1573cc79392SFilipe Manana new_fl |= S_SYNC; 1586cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1593cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1606cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1613cc79392SFilipe Manana new_fl |= S_APPEND; 1626cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1633cc79392SFilipe Manana new_fl |= S_NOATIME; 1646cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1653cc79392SFilipe Manana new_fl |= S_DIRSYNC; 1663cc79392SFilipe Manana 1673cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 1683cc79392SFilipe Manana S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, 1693cc79392SFilipe Manana new_fl); 1706cbff00fSChristoph Hellwig } 1716cbff00fSChristoph Hellwig 1726cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1736cbff00fSChristoph Hellwig { 174496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1756cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1766cbff00fSChristoph Hellwig 1776cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1786cbff00fSChristoph Hellwig return -EFAULT; 1796cbff00fSChristoph Hellwig return 0; 1806cbff00fSChristoph Hellwig } 1816cbff00fSChristoph Hellwig 18275e7cb7fSLiu Bo static int check_flags(unsigned int flags) 18375e7cb7fSLiu Bo { 18475e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 18575e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 18675e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 187e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 188e1e8fb6aSLi Zefan FS_NOCOW_FL)) 18975e7cb7fSLiu Bo return -EOPNOTSUPP; 19075e7cb7fSLiu Bo 19175e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 19275e7cb7fSLiu Bo return -EINVAL; 19375e7cb7fSLiu Bo 19475e7cb7fSLiu Bo return 0; 19575e7cb7fSLiu Bo } 19675e7cb7fSLiu Bo 1976cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1986cbff00fSChristoph Hellwig { 199496ad9aaSAl Viro struct inode *inode = file_inode(file); 2000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2016cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 2026cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 2036cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2046cbff00fSChristoph Hellwig unsigned int flags, oldflags; 2056cbff00fSChristoph Hellwig int ret; 206f062abf0SLi Zefan u64 ip_oldflags; 207f062abf0SLi Zefan unsigned int i_oldflags; 2087e97b8daSDavid Sterba umode_t mode; 2096cbff00fSChristoph Hellwig 210bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 211bd60ea0fSDavid Sterba return -EPERM; 212bd60ea0fSDavid Sterba 213b83cc969SLi Zefan if (btrfs_root_readonly(root)) 214b83cc969SLi Zefan return -EROFS; 215b83cc969SLi Zefan 2166cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 2176cbff00fSChristoph Hellwig return -EFAULT; 2186cbff00fSChristoph Hellwig 21975e7cb7fSLiu Bo ret = check_flags(flags); 22075e7cb7fSLiu Bo if (ret) 22175e7cb7fSLiu Bo return ret; 2226cbff00fSChristoph Hellwig 223e7848683SJan Kara ret = mnt_want_write_file(file); 224e7848683SJan Kara if (ret) 225e7848683SJan Kara return ret; 226e7848683SJan Kara 2275955102cSAl Viro inode_lock(inode); 2286cbff00fSChristoph Hellwig 229f062abf0SLi Zefan ip_oldflags = ip->flags; 230f062abf0SLi Zefan i_oldflags = inode->i_flags; 2317e97b8daSDavid Sterba mode = inode->i_mode; 232f062abf0SLi Zefan 2336cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2346cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2356cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2366cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2376cbff00fSChristoph Hellwig ret = -EPERM; 2386cbff00fSChristoph Hellwig goto out_unlock; 2396cbff00fSChristoph Hellwig } 2406cbff00fSChristoph Hellwig } 2416cbff00fSChristoph Hellwig 2426cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2436cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2446cbff00fSChristoph Hellwig else 2456cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2466cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2476cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2486cbff00fSChristoph Hellwig else 2496cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2506cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2516cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2526cbff00fSChristoph Hellwig else 2536cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2546cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2556cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2566cbff00fSChristoph Hellwig else 2576cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2586cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2596cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2606cbff00fSChristoph Hellwig else 2616cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2626cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2636cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2646cbff00fSChristoph Hellwig else 2656cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2667e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2677e97b8daSDavid Sterba if (S_ISREG(mode)) { 2687e97b8daSDavid Sterba /* 2697e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2707e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2717e97b8daSDavid Sterba * status of the file and will not set it. 2727e97b8daSDavid Sterba */ 2737e97b8daSDavid Sterba if (inode->i_size == 0) 2747e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2757e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2767e97b8daSDavid Sterba } else { 277e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2787e97b8daSDavid Sterba } 2797e97b8daSDavid Sterba } else { 2807e97b8daSDavid Sterba /* 28101327610SNicholas D Steeves * Revert back under same assumptions as above 2827e97b8daSDavid Sterba */ 2837e97b8daSDavid Sterba if (S_ISREG(mode)) { 2847e97b8daSDavid Sterba if (inode->i_size == 0) 2857e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2867e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2877e97b8daSDavid Sterba } else { 288e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2897e97b8daSDavid Sterba } 2907e97b8daSDavid Sterba } 2916cbff00fSChristoph Hellwig 29275e7cb7fSLiu Bo /* 29375e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 29475e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 29575e7cb7fSLiu Bo * things smaller. 29675e7cb7fSLiu Bo */ 29775e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 29875e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 29975e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 30063541927SFilipe David Borba Manana 30163541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 30263541927SFilipe David Borba Manana if (ret && ret != -ENODATA) 30363541927SFilipe David Borba Manana goto out_drop; 30475e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 30563541927SFilipe David Borba Manana const char *comp; 30663541927SFilipe David Borba Manana 30775e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 30875e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 30963541927SFilipe David Borba Manana 3100b246afaSJeff Mahoney if (fs_info->compress_type == BTRFS_COMPRESS_LZO) 31163541927SFilipe David Borba Manana comp = "lzo"; 3125c1aab1dSNick Terrell else if (fs_info->compress_type == BTRFS_COMPRESS_ZLIB) 31363541927SFilipe David Borba Manana comp = "zlib"; 3145c1aab1dSNick Terrell else 3155c1aab1dSNick Terrell comp = "zstd"; 31663541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", 31763541927SFilipe David Borba Manana comp, strlen(comp), 0); 31863541927SFilipe David Borba Manana if (ret) 31963541927SFilipe David Borba Manana goto out_drop; 32063541927SFilipe David Borba Manana 321ebcb904dSLi Zefan } else { 32278a017a2SFilipe Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 32378a017a2SFilipe Manana if (ret && ret != -ENODATA) 32478a017a2SFilipe Manana goto out_drop; 325ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 32675e7cb7fSLiu Bo } 3276cbff00fSChristoph Hellwig 3284da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 329f062abf0SLi Zefan if (IS_ERR(trans)) { 330f062abf0SLi Zefan ret = PTR_ERR(trans); 331f062abf0SLi Zefan goto out_drop; 332f062abf0SLi Zefan } 3336cbff00fSChristoph Hellwig 334306424ccSLi Zefan btrfs_update_iflags(inode); 3350c4d2d95SJosef Bacik inode_inc_iversion(inode); 336c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3376cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3386cbff00fSChristoph Hellwig 3393a45bb20SJeff Mahoney btrfs_end_transaction(trans); 340f062abf0SLi Zefan out_drop: 341f062abf0SLi Zefan if (ret) { 342f062abf0SLi Zefan ip->flags = ip_oldflags; 343f062abf0SLi Zefan inode->i_flags = i_oldflags; 344f062abf0SLi Zefan } 3456cbff00fSChristoph Hellwig 3466cbff00fSChristoph Hellwig out_unlock: 3475955102cSAl Viro inode_unlock(inode); 348e7848683SJan Kara mnt_drop_write_file(file); 3492d4e6f6aSliubo return ret; 3506cbff00fSChristoph Hellwig } 3516cbff00fSChristoph Hellwig 3526cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3536cbff00fSChristoph Hellwig { 354496ad9aaSAl Viro struct inode *inode = file_inode(file); 3556cbff00fSChristoph Hellwig 3566cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3576cbff00fSChristoph Hellwig } 358f46b5a66SChristoph Hellwig 359f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 360f7039b1dSLi Dongyang { 3610b246afaSJeff Mahoney struct inode *inode = file_inode(file); 3620b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 363f7039b1dSLi Dongyang struct btrfs_device *device; 364f7039b1dSLi Dongyang struct request_queue *q; 365f7039b1dSLi Dongyang struct fstrim_range range; 366f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 367f7039b1dSLi Dongyang u64 num_devices = 0; 368815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 369f7039b1dSLi Dongyang int ret; 370f7039b1dSLi Dongyang 371f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 372f7039b1dSLi Dongyang return -EPERM; 373f7039b1dSLi Dongyang 3741f78160cSXiao Guangrong rcu_read_lock(); 3751f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3761f78160cSXiao Guangrong dev_list) { 377f7039b1dSLi Dongyang if (!device->bdev) 378f7039b1dSLi Dongyang continue; 379f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 380f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 381f7039b1dSLi Dongyang num_devices++; 38250d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 383f7039b1dSLi Dongyang minlen); 384f7039b1dSLi Dongyang } 385f7039b1dSLi Dongyang } 3861f78160cSXiao Guangrong rcu_read_unlock(); 387f4c697e6SLukas Czerner 388f7039b1dSLi Dongyang if (!num_devices) 389f7039b1dSLi Dongyang return -EOPNOTSUPP; 390f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 391f7039b1dSLi Dongyang return -EFAULT; 392e515c18bSLukas Czerner if (range.start > total_bytes || 393e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 394f4c697e6SLukas Czerner return -EINVAL; 395f7039b1dSLi Dongyang 396f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 397f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 3982ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 399f7039b1dSLi Dongyang if (ret < 0) 400f7039b1dSLi Dongyang return ret; 401f7039b1dSLi Dongyang 402f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 403f7039b1dSLi Dongyang return -EFAULT; 404f7039b1dSLi Dongyang 405f7039b1dSLi Dongyang return 0; 406f7039b1dSLi Dongyang } 407f7039b1dSLi Dongyang 408dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 409dd5f9615SStefan Behrens { 41046e0f66aSChris Mason int i; 41146e0f66aSChris Mason 41246e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 41346e0f66aSChris Mason if (uuid[i]) 41446e0f66aSChris Mason return 0; 41546e0f66aSChris Mason } 41646e0f66aSChris Mason return 1; 417dd5f9615SStefan Behrens } 418dd5f9615SStefan Behrens 419d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 420cb8e7090SChristoph Hellwig struct dentry *dentry, 42152f75f4fSDavid Sterba const char *name, int namelen, 4226f72c7e2SArne Jansen u64 *async_transid, 4238696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 424f46b5a66SChristoph Hellwig { 4250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 426f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 427f46b5a66SChristoph Hellwig struct btrfs_key key; 42849a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 429f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 430f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 431d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 43276dda93cSYan, Zheng struct btrfs_root *new_root; 433d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 434c2050a45SDeepa Dinamani struct timespec cur_time = current_time(dir); 4355662344bSTsutomu Itoh struct inode *inode; 436f46b5a66SChristoph Hellwig int ret; 437f46b5a66SChristoph Hellwig int err; 438f46b5a66SChristoph Hellwig u64 objectid; 439f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 4403de4586cSChris Mason u64 index = 0; 441d5c12070SMiao Xie u64 qgroup_reserved; 4428ea05e3aSAlexander Block uuid_le new_uuid; 443f46b5a66SChristoph Hellwig 44449a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 44549a3c4d9SDavid Sterba if (!root_item) 44649a3c4d9SDavid Sterba return -ENOMEM; 44749a3c4d9SDavid Sterba 4480b246afaSJeff Mahoney ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); 4492fbe8c8aSAl Viro if (ret) 45049a3c4d9SDavid Sterba goto fail_free; 4516a912213SJosef Bacik 452e09fe2d2SQu Wenruo /* 453e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 45401327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 455e09fe2d2SQu Wenruo */ 45649a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 45749a3c4d9SDavid Sterba ret = -ENOSPC; 45849a3c4d9SDavid Sterba goto fail_free; 45949a3c4d9SDavid Sterba } 460e09fe2d2SQu Wenruo 461d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 4629ed74f2dSJosef Bacik /* 463d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 464d5c12070SMiao Xie * of create_snapshot(). 4659ed74f2dSJosef Bacik */ 466d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 467dd5f9615SStefan Behrens 8, &qgroup_reserved, false); 468d5c12070SMiao Xie if (ret) 46949a3c4d9SDavid Sterba goto fail_free; 470f46b5a66SChristoph Hellwig 471d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 472d5c12070SMiao Xie if (IS_ERR(trans)) { 473d5c12070SMiao Xie ret = PTR_ERR(trans); 4747775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 47549a3c4d9SDavid Sterba goto fail_free; 476d5c12070SMiao Xie } 477d5c12070SMiao Xie trans->block_rsv = &block_rsv; 478d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 479f46b5a66SChristoph Hellwig 4800b246afaSJeff Mahoney ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit); 4816f72c7e2SArne Jansen if (ret) 4826f72c7e2SArne Jansen goto fail; 4836f72c7e2SArne Jansen 4844d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 4858e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4868e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4878e8a1e31SJosef Bacik goto fail; 4888e8a1e31SJosef Bacik } 489f46b5a66SChristoph Hellwig 490b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 491f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 492f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4935d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 494f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 495f46b5a66SChristoph Hellwig 4960b246afaSJeff Mahoney write_extent_buffer_fsid(leaf, fs_info->fsid); 4970b246afaSJeff Mahoney write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 498f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 499f46b5a66SChristoph Hellwig 50049a3c4d9SDavid Sterba inode_item = &root_item->inode; 5013cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 5023cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 5033cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 504da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 5050b246afaSJeff Mahoney fs_info->nodesize); 5063cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 507f46b5a66SChristoph Hellwig 50849a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 50949a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 5103cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 51108fe4db1SLi Zefan 51249a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 51349a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 51449a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 51549a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 51649a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 51749a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 518f46b5a66SChristoph Hellwig 51949a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 52049a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 5218ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 52249a3c4d9SDavid Sterba memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 52349a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 52449a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 52549a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 52649a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 52749a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 528f46b5a66SChristoph Hellwig 529925baeddSChris Mason btrfs_tree_unlock(leaf); 530f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 531f46b5a66SChristoph Hellwig leaf = NULL; 532f46b5a66SChristoph Hellwig 53349a3c4d9SDavid Sterba btrfs_set_root_dirid(root_item, new_dirid); 534f46b5a66SChristoph Hellwig 535f46b5a66SChristoph Hellwig key.objectid = objectid; 5365d4f98a2SYan Zheng key.offset = 0; 537962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 5380b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 53949a3c4d9SDavid Sterba root_item); 540f46b5a66SChristoph Hellwig if (ret) 541f46b5a66SChristoph Hellwig goto fail; 542f46b5a66SChristoph Hellwig 54376dda93cSYan, Zheng key.offset = (u64)-1; 5440b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &key); 54579787eaaSJeff Mahoney if (IS_ERR(new_root)) { 54679787eaaSJeff Mahoney ret = PTR_ERR(new_root); 54766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 54879787eaaSJeff Mahoney goto fail; 54979787eaaSJeff Mahoney } 55076dda93cSYan, Zheng 55176dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 55276dda93cSYan, Zheng 55363541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 554ce598979SMark Fasheh if (ret) { 555ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 55666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 557ce598979SMark Fasheh goto fail; 558ce598979SMark Fasheh } 559ce598979SMark Fasheh 560f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 561f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 562f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 563f32e48e9SChandan Rajendra 564f46b5a66SChristoph Hellwig /* 565f46b5a66SChristoph Hellwig * insert the directory item 566f46b5a66SChristoph Hellwig */ 567877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 56879787eaaSJeff Mahoney if (ret) { 56966642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 57079787eaaSJeff Mahoney goto fail; 57179787eaaSJeff Mahoney } 5723de4586cSChris Mason 5733de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 5748e7611cfSNikolay Borisov name, namelen, BTRFS_I(dir), &key, 5753de4586cSChris Mason BTRFS_FT_DIR, index); 57679787eaaSJeff Mahoney if (ret) { 57766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 578f46b5a66SChristoph Hellwig goto fail; 57979787eaaSJeff Mahoney } 5800660b5afSChris Mason 5816ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 58252c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 58352c26179SYan Zheng BUG_ON(ret); 58452c26179SYan Zheng 5850b246afaSJeff Mahoney ret = btrfs_add_root_ref(trans, fs_info, 5864df27c4dSYan, Zheng objectid, root->root_key.objectid, 5874a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 5880660b5afSChris Mason BUG_ON(ret); 5890660b5afSChris Mason 5900b246afaSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid, 5916bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 592dd5f9615SStefan Behrens if (ret) 59366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 594dd5f9615SStefan Behrens 595f46b5a66SChristoph Hellwig fail: 59649a3c4d9SDavid Sterba kfree(root_item); 597d5c12070SMiao Xie trans->block_rsv = NULL; 598d5c12070SMiao Xie trans->bytes_reserved = 0; 5997775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 600de6e8200SLiu Bo 60172fd032eSSage Weil if (async_transid) { 60272fd032eSSage Weil *async_transid = trans->transid; 6033a45bb20SJeff Mahoney err = btrfs_commit_transaction_async(trans, 1); 60400d71c9cSMiao Xie if (err) 6053a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 60672fd032eSSage Weil } else { 6073a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 60872fd032eSSage Weil } 609f46b5a66SChristoph Hellwig if (err && !ret) 610f46b5a66SChristoph Hellwig ret = err; 6111a65e24bSChris Mason 6125662344bSTsutomu Itoh if (!ret) { 6135662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 614de6e8200SLiu Bo if (IS_ERR(inode)) 615de6e8200SLiu Bo return PTR_ERR(inode); 6165662344bSTsutomu Itoh d_instantiate(dentry, inode); 6175662344bSTsutomu Itoh } 618f46b5a66SChristoph Hellwig return ret; 61949a3c4d9SDavid Sterba 62049a3c4d9SDavid Sterba fail_free: 62149a3c4d9SDavid Sterba kfree(root_item); 62249a3c4d9SDavid Sterba return ret; 623f46b5a66SChristoph Hellwig } 624f46b5a66SChristoph Hellwig 625e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 62661d7e4cbSDavid Sterba struct dentry *dentry, 627e9662f70SMiao Xie u64 *async_transid, bool readonly, 628e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 629f46b5a66SChristoph Hellwig { 6300b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 6312e4bfab9SYan, Zheng struct inode *inode; 632f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 633f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 6342e4bfab9SYan, Zheng int ret; 635f46b5a66SChristoph Hellwig 63627cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 637f46b5a66SChristoph Hellwig return -EINVAL; 638f46b5a66SChristoph Hellwig 63923269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 640a1ee7362SDavid Sterba if (!pending_snapshot) 641a1ee7362SDavid Sterba return -ENOMEM; 642a1ee7362SDavid Sterba 643b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 64423269bf5SDavid Sterba GFP_KERNEL); 6458546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 6468546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 647b0c0ea63SDavid Sterba ret = -ENOMEM; 648b0c0ea63SDavid Sterba goto free_pending; 649b0c0ea63SDavid Sterba } 650b0c0ea63SDavid Sterba 651ea14b57fSDavid Sterba atomic_inc(&root->will_be_snapshotted); 6524e857c58SPeter Zijlstra smp_mb__after_atomic(); 65345bac0f3SLiu Bo /* wait for no snapshot writes */ 65445bac0f3SLiu Bo wait_event(root->subv_writers->wait, 65545bac0f3SLiu Bo percpu_counter_sum(&root->subv_writers->counter) == 0); 6568257b2dcSMiao Xie 6576a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 6586a03843dSMiao Xie if (ret) 659a1ee7362SDavid Sterba goto dec_and_free; 6606a03843dSMiao Xie 6616374e57aSChris Mason btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 6626a03843dSMiao Xie 66366d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 66466d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 665d5c12070SMiao Xie /* 666d5c12070SMiao Xie * 1 - parent dir inode 667d5c12070SMiao Xie * 2 - dir entries 668d5c12070SMiao Xie * 1 - root item 669d5c12070SMiao Xie * 2 - root ref/backref 670d5c12070SMiao Xie * 1 - root of snapshot 671dd5f9615SStefan Behrens * 1 - UUID item 672d5c12070SMiao Xie */ 673d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 674dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 675ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 676ee3441b4SJeff Mahoney false); 677d5c12070SMiao Xie if (ret) 678a1ee7362SDavid Sterba goto dec_and_free; 679d5c12070SMiao Xie 680a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 681a22285a6SYan, Zheng pending_snapshot->root = root; 682b83cc969SLi Zefan pending_snapshot->readonly = readonly; 683e9662f70SMiao Xie pending_snapshot->dir = dir; 6848696c533SMiao Xie pending_snapshot->inherit = inherit; 685a22285a6SYan, Zheng 686d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 687a22285a6SYan, Zheng if (IS_ERR(trans)) { 688a22285a6SYan, Zheng ret = PTR_ERR(trans); 689a22285a6SYan, Zheng goto fail; 690a22285a6SYan, Zheng } 691a22285a6SYan, Zheng 6920b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 693a22285a6SYan, Zheng list_add(&pending_snapshot->list, 694a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 6950b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 69672fd032eSSage Weil if (async_transid) { 69772fd032eSSage Weil *async_transid = trans->transid; 6983a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 1); 69900d71c9cSMiao Xie if (ret) 7003a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 70172fd032eSSage Weil } else { 7023a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 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 711d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 712d3797308SChris Mason if (ret) 713d3797308SChris Mason goto fail; 714d3797308SChris Mason 7152b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 7162e4bfab9SYan, Zheng if (IS_ERR(inode)) { 7172e4bfab9SYan, Zheng ret = PTR_ERR(inode); 7182e4bfab9SYan, Zheng goto fail; 7192e4bfab9SYan, Zheng } 7205662344bSTsutomu Itoh 7212e4bfab9SYan, Zheng d_instantiate(dentry, inode); 7222e4bfab9SYan, Zheng ret = 0; 7232e4bfab9SYan, Zheng fail: 7247775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); 725a1ee7362SDavid Sterba dec_and_free: 726ea14b57fSDavid Sterba if (atomic_dec_and_test(&root->will_be_snapshotted)) 727ea14b57fSDavid Sterba wake_up_atomic_t(&root->will_be_snapshotted); 728b0c0ea63SDavid Sterba free_pending: 729b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 7308546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 731a1ee7362SDavid Sterba kfree(pending_snapshot); 732a1ee7362SDavid Sterba 733f46b5a66SChristoph Hellwig return ret; 734f46b5a66SChristoph Hellwig } 735f46b5a66SChristoph Hellwig 7364260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 7374260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 7384260f7c7SSage Weil * whether the type of victim is right. 7394260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 7404260f7c7SSage Weil * 2. We should have write and exec permissions on dir 7414260f7c7SSage Weil * 3. We can't remove anything from append-only dir 7424260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 7434260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 7444260f7c7SSage Weil * a. be owner of dir, or 7454260f7c7SSage Weil * b. be owner of victim, or 7464260f7c7SSage Weil * c. have CAP_FOWNER capability 74701327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 7484260f7c7SSage Weil * links pointing to it. 7494260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 7504260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 7514260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 7524260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 7534260f7c7SSage Weil * nfs_async_unlink(). 7544260f7c7SSage Weil */ 7554260f7c7SSage Weil 7564260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 7574260f7c7SSage Weil { 7584260f7c7SSage Weil int error; 7594260f7c7SSage Weil 7602b0143b5SDavid Howells if (d_really_is_negative(victim)) 7614260f7c7SSage Weil return -ENOENT; 7624260f7c7SSage Weil 7632b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 7644fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 7654260f7c7SSage Weil 7664260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 7674260f7c7SSage Weil if (error) 7684260f7c7SSage Weil return error; 7694260f7c7SSage Weil if (IS_APPEND(dir)) 7704260f7c7SSage Weil return -EPERM; 7712b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 7722b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 7734260f7c7SSage Weil return -EPERM; 7744260f7c7SSage Weil if (isdir) { 775e36cb0b8SDavid Howells if (!d_is_dir(victim)) 7764260f7c7SSage Weil return -ENOTDIR; 7774260f7c7SSage Weil if (IS_ROOT(victim)) 7784260f7c7SSage Weil return -EBUSY; 779e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 7804260f7c7SSage Weil return -EISDIR; 7814260f7c7SSage Weil if (IS_DEADDIR(dir)) 7824260f7c7SSage Weil return -ENOENT; 7834260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 7844260f7c7SSage Weil return -EBUSY; 7854260f7c7SSage Weil return 0; 7864260f7c7SSage Weil } 7874260f7c7SSage Weil 788cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 789cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 790cb8e7090SChristoph Hellwig { 7912b0143b5SDavid Howells if (d_really_is_positive(child)) 792cb8e7090SChristoph Hellwig return -EEXIST; 793cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 794cb8e7090SChristoph Hellwig return -ENOENT; 795cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 796cb8e7090SChristoph Hellwig } 797cb8e7090SChristoph Hellwig 798cb8e7090SChristoph Hellwig /* 799cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 800cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 801cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 802cb8e7090SChristoph Hellwig */ 80392872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 80452f75f4fSDavid Sterba const char *name, int namelen, 80572fd032eSSage Weil struct btrfs_root *snap_src, 8066f72c7e2SArne Jansen u64 *async_transid, bool readonly, 8078696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 808cb8e7090SChristoph Hellwig { 8092b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 8100b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 811cb8e7090SChristoph Hellwig struct dentry *dentry; 812cb8e7090SChristoph Hellwig int error; 813cb8e7090SChristoph Hellwig 81400235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 81500235411SAl Viro if (error == -EINTR) 81600235411SAl Viro return error; 817cb8e7090SChristoph Hellwig 818cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 819cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 820cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 821cb8e7090SChristoph Hellwig goto out_unlock; 822cb8e7090SChristoph Hellwig 82376dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 824cb8e7090SChristoph Hellwig if (error) 825a874a63eSLiu Bo goto out_dput; 826cb8e7090SChristoph Hellwig 8279c52057cSChris Mason /* 8289c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 8299c52057cSChris Mason * check for them now when we can safely fail 8309c52057cSChris Mason */ 8319c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 8329c52057cSChris Mason dir->i_ino, name, 8339c52057cSChris Mason namelen); 8349c52057cSChris Mason if (error) 8359c52057cSChris Mason goto out_dput; 8369c52057cSChris Mason 8370b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 83876dda93cSYan, Zheng 83976dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 84076dda93cSYan, Zheng goto out_up_read; 84176dda93cSYan, Zheng 8423de4586cSChris Mason if (snap_src) { 84361d7e4cbSDavid Sterba error = create_snapshot(snap_src, dir, dentry, 8446f72c7e2SArne Jansen async_transid, readonly, inherit); 8453de4586cSChris Mason } else { 846d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 847d5c12070SMiao Xie async_transid, inherit); 8483de4586cSChris Mason } 84976dda93cSYan, Zheng if (!error) 85076dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 85176dda93cSYan, Zheng out_up_read: 8520b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 853cb8e7090SChristoph Hellwig out_dput: 854cb8e7090SChristoph Hellwig dput(dentry); 855cb8e7090SChristoph Hellwig out_unlock: 8565955102cSAl Viro inode_unlock(dir); 857cb8e7090SChristoph Hellwig return error; 858cb8e7090SChristoph Hellwig } 859cb8e7090SChristoph Hellwig 8604cb5300bSChris Mason /* 8614cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 8624cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 8634cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 8644cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 8654cb5300bSChris Mason * part of the file 8664cb5300bSChris Mason */ 867aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 8684cb5300bSChris Mason { 8694cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8704cb5300bSChris Mason struct extent_map *em = NULL; 8714cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 8724cb5300bSChris Mason u64 end; 8734cb5300bSChris Mason 8744cb5300bSChris Mason read_lock(&em_tree->lock); 87509cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 8764cb5300bSChris Mason read_unlock(&em_tree->lock); 8774cb5300bSChris Mason 8784cb5300bSChris Mason if (em) { 8794cb5300bSChris Mason end = extent_map_end(em); 8804cb5300bSChris Mason free_extent_map(em); 8814cb5300bSChris Mason if (end - offset > thresh) 8824cb5300bSChris Mason return 0; 8834cb5300bSChris Mason } 8844cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 8854cb5300bSChris Mason thresh /= 2; 8864cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 8874cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 8884cb5300bSChris Mason if (end >= thresh) 8894cb5300bSChris Mason return 0; 8904cb5300bSChris Mason return 1; 8914cb5300bSChris Mason } 8924cb5300bSChris Mason 8934cb5300bSChris Mason /* 8944cb5300bSChris Mason * helper function to walk through a file and find extents 8954cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 8964cb5300bSChris Mason * 8974cb5300bSChris Mason * This is used by the defragging code to find new and small 8984cb5300bSChris Mason * extents 8994cb5300bSChris Mason */ 9004cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 9014cb5300bSChris Mason struct inode *inode, u64 newer_than, 902aab110abSDavid Sterba u64 *off, u32 thresh) 9034cb5300bSChris Mason { 9044cb5300bSChris Mason struct btrfs_path *path; 9054cb5300bSChris Mason struct btrfs_key min_key; 9064cb5300bSChris Mason struct extent_buffer *leaf; 9074cb5300bSChris Mason struct btrfs_file_extent_item *extent; 9084cb5300bSChris Mason int type; 9094cb5300bSChris Mason int ret; 9104a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode)); 9114cb5300bSChris Mason 9124cb5300bSChris Mason path = btrfs_alloc_path(); 9134cb5300bSChris Mason if (!path) 9144cb5300bSChris Mason return -ENOMEM; 9154cb5300bSChris Mason 916a4689d2bSDavid Sterba min_key.objectid = ino; 9174cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 9184cb5300bSChris Mason min_key.offset = *off; 9194cb5300bSChris Mason 9204cb5300bSChris Mason while (1) { 9216174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 9224cb5300bSChris Mason if (ret != 0) 9234cb5300bSChris Mason goto none; 924f094c9bdSFilipe Manana process_slot: 925a4689d2bSDavid Sterba if (min_key.objectid != ino) 9264cb5300bSChris Mason goto none; 9274cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 9284cb5300bSChris Mason goto none; 9294cb5300bSChris Mason 9304cb5300bSChris Mason leaf = path->nodes[0]; 9314cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 9324cb5300bSChris Mason struct btrfs_file_extent_item); 9334cb5300bSChris Mason 9344cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 9354cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 9364cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 9374cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 9384cb5300bSChris Mason *off = min_key.offset; 9394cb5300bSChris Mason btrfs_free_path(path); 9404cb5300bSChris Mason return 0; 9414cb5300bSChris Mason } 9424cb5300bSChris Mason 943f094c9bdSFilipe Manana path->slots[0]++; 944f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 945f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 946f094c9bdSFilipe Manana goto process_slot; 947f094c9bdSFilipe Manana } 948f094c9bdSFilipe Manana 9494cb5300bSChris Mason if (min_key.offset == (u64)-1) 9504cb5300bSChris Mason goto none; 9514cb5300bSChris Mason 9524cb5300bSChris Mason min_key.offset++; 9534cb5300bSChris Mason btrfs_release_path(path); 9544cb5300bSChris Mason } 9554cb5300bSChris Mason none: 9564cb5300bSChris Mason btrfs_free_path(path); 9574cb5300bSChris Mason return -ENOENT; 9584cb5300bSChris Mason } 9594cb5300bSChris Mason 9606c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 96117ce6ef8SLiu Bo { 96217ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 963940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 9646c282eb4SLi Zefan struct extent_map *em; 96509cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 966940100a4SChris Mason 967940100a4SChris Mason /* 968940100a4SChris Mason * hopefully we have this extent in the tree already, try without 969940100a4SChris Mason * the full extent lock 970940100a4SChris Mason */ 971940100a4SChris Mason read_lock(&em_tree->lock); 972940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 973940100a4SChris Mason read_unlock(&em_tree->lock); 974940100a4SChris Mason 975940100a4SChris Mason if (!em) { 976308d9800SFilipe Manana struct extent_state *cached = NULL; 977308d9800SFilipe Manana u64 end = start + len - 1; 978308d9800SFilipe Manana 979940100a4SChris Mason /* get the big lock and read metadata off disk */ 980ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 981fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); 982308d9800SFilipe Manana unlock_extent_cached(io_tree, start, end, &cached, GFP_NOFS); 983940100a4SChris Mason 9846cf8bfbfSDan Carpenter if (IS_ERR(em)) 9856c282eb4SLi Zefan return NULL; 986940100a4SChris Mason } 987940100a4SChris Mason 9886c282eb4SLi Zefan return em; 9896c282eb4SLi Zefan } 9906c282eb4SLi Zefan 9916c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 9926c282eb4SLi Zefan { 9936c282eb4SLi Zefan struct extent_map *next; 9946c282eb4SLi Zefan bool ret = true; 9956c282eb4SLi Zefan 9966c282eb4SLi Zefan /* this is the last extent */ 9976c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 9986c282eb4SLi Zefan return false; 9996c282eb4SLi Zefan 10006c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1001e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1002e9512d72SChris Mason ret = false; 1003e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1004ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 10056c282eb4SLi Zefan ret = false; 10066c282eb4SLi Zefan 10076c282eb4SLi Zefan free_extent_map(next); 10086c282eb4SLi Zefan return ret; 10096c282eb4SLi Zefan } 10106c282eb4SLi Zefan 1011aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1012a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1013a43a2111SAndrew Mahone int compress) 10146c282eb4SLi Zefan { 10156c282eb4SLi Zefan struct extent_map *em; 10166c282eb4SLi Zefan int ret = 1; 10176c282eb4SLi Zefan bool next_mergeable = true; 10184a3560c4SLiu Bo bool prev_mergeable = true; 10196c282eb4SLi Zefan 10206c282eb4SLi Zefan /* 10216c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 10226c282eb4SLi Zefan * defragging it 10236c282eb4SLi Zefan */ 10246c282eb4SLi Zefan if (start < *defrag_end) 10256c282eb4SLi Zefan return 1; 10266c282eb4SLi Zefan 10276c282eb4SLi Zefan *skip = 0; 10286c282eb4SLi Zefan 10296c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 10306c282eb4SLi Zefan if (!em) 10316c282eb4SLi Zefan return 0; 10326c282eb4SLi Zefan 1033940100a4SChris Mason /* this will cover holes, and inline extents */ 103417ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1035940100a4SChris Mason ret = 0; 103617ce6ef8SLiu Bo goto out; 103717ce6ef8SLiu Bo } 103817ce6ef8SLiu Bo 10394a3560c4SLiu Bo if (!*defrag_end) 10404a3560c4SLiu Bo prev_mergeable = false; 10414a3560c4SLiu Bo 10426c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1043940100a4SChris Mason /* 10446c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 10456c282eb4SLi Zefan * real extent, don't bother defragging it 1046940100a4SChris Mason */ 1047a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 10484a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1049940100a4SChris Mason ret = 0; 105017ce6ef8SLiu Bo out: 1051940100a4SChris Mason /* 1052940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1053940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1054940100a4SChris Mason * so that the next tiny extent will force a defrag. 1055940100a4SChris Mason * 1056940100a4SChris Mason * The end result of this is that tiny extents before a single big 1057940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1058940100a4SChris Mason */ 1059940100a4SChris Mason if (ret) { 1060940100a4SChris Mason *defrag_end = extent_map_end(em); 1061940100a4SChris Mason } else { 1062940100a4SChris Mason *last_len = 0; 1063940100a4SChris Mason *skip = extent_map_end(em); 1064940100a4SChris Mason *defrag_end = 0; 1065940100a4SChris Mason } 1066940100a4SChris Mason 1067940100a4SChris Mason free_extent_map(em); 1068940100a4SChris Mason return ret; 1069940100a4SChris Mason } 1070940100a4SChris Mason 10714cb5300bSChris Mason /* 10724cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 10734cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 10744cb5300bSChris Mason * to COW and defrag. 10754cb5300bSChris Mason * 10764cb5300bSChris Mason * It also makes sure the delalloc code has enough 10774cb5300bSChris Mason * dirty data to avoid making new small extents as part 10784cb5300bSChris Mason * of the defrag 10794cb5300bSChris Mason * 10804cb5300bSChris Mason * It's a good idea to start RA on this range 10814cb5300bSChris Mason * before calling this. 10824cb5300bSChris Mason */ 10834cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 10844cb5300bSChris Mason struct page **pages, 10854cb5300bSChris Mason unsigned long start_index, 1086c41570c9SJustin Maggard unsigned long num_pages) 1087f46b5a66SChristoph Hellwig { 10884cb5300bSChris Mason unsigned long file_end; 10894cb5300bSChris Mason u64 isize = i_size_read(inode); 1090f46b5a66SChristoph Hellwig u64 page_start; 1091f46b5a66SChristoph Hellwig u64 page_end; 10921f12bd06SLiu Bo u64 page_cnt; 10934cb5300bSChris Mason int ret; 10944cb5300bSChris Mason int i; 10954cb5300bSChris Mason int i_done; 10964cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 10974cb5300bSChris Mason struct extent_state *cached_state = NULL; 1098600a45e1SMiao Xie struct extent_io_tree *tree; 1099364ecf36SQu Wenruo struct extent_changeset *data_reserved = NULL; 11003b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 11014cb5300bSChris Mason 110209cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 11031f12bd06SLiu Bo if (!isize || start_index > file_end) 11041f12bd06SLiu Bo return 0; 11051f12bd06SLiu Bo 11061f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 11074cb5300bSChris Mason 1108364ecf36SQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 110909cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 111009cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 11114cb5300bSChris Mason if (ret) 11124cb5300bSChris Mason return ret; 11134cb5300bSChris Mason i_done = 0; 1114600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 11154cb5300bSChris Mason 11164cb5300bSChris Mason /* step one, lock all the pages */ 11171f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 11184cb5300bSChris Mason struct page *page; 1119600a45e1SMiao Xie again: 1120a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 11213b16a4e3SJosef Bacik start_index + i, mask); 11224cb5300bSChris Mason if (!page) 11234cb5300bSChris Mason break; 11244cb5300bSChris Mason 1125600a45e1SMiao Xie page_start = page_offset(page); 112609cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1127600a45e1SMiao Xie while (1) { 1128308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1129ff13db41SDavid Sterba &cached_state); 1130600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1131600a45e1SMiao Xie page_start); 1132308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1133308d9800SFilipe Manana &cached_state, GFP_NOFS); 1134600a45e1SMiao Xie if (!ordered) 1135600a45e1SMiao Xie break; 1136600a45e1SMiao Xie 1137600a45e1SMiao Xie unlock_page(page); 1138600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1139600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1140600a45e1SMiao Xie lock_page(page); 11411f12bd06SLiu Bo /* 11421f12bd06SLiu Bo * we unlocked the page above, so we need check if 11431f12bd06SLiu Bo * it was released or not. 11441f12bd06SLiu Bo */ 11451f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 11461f12bd06SLiu Bo unlock_page(page); 114709cbfeafSKirill A. Shutemov put_page(page); 11481f12bd06SLiu Bo goto again; 11491f12bd06SLiu Bo } 1150600a45e1SMiao Xie } 1151600a45e1SMiao Xie 11524cb5300bSChris Mason if (!PageUptodate(page)) { 11534cb5300bSChris Mason btrfs_readpage(NULL, page); 11544cb5300bSChris Mason lock_page(page); 11554cb5300bSChris Mason if (!PageUptodate(page)) { 11564cb5300bSChris Mason unlock_page(page); 115709cbfeafSKirill A. Shutemov put_page(page); 11584cb5300bSChris Mason ret = -EIO; 11594cb5300bSChris Mason break; 11604cb5300bSChris Mason } 11614cb5300bSChris Mason } 1162600a45e1SMiao Xie 1163600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1164600a45e1SMiao Xie unlock_page(page); 116509cbfeafSKirill A. Shutemov put_page(page); 1166600a45e1SMiao Xie goto again; 1167600a45e1SMiao Xie } 1168600a45e1SMiao Xie 11694cb5300bSChris Mason pages[i] = page; 11704cb5300bSChris Mason i_done++; 11714cb5300bSChris Mason } 11724cb5300bSChris Mason if (!i_done || ret) 11734cb5300bSChris Mason goto out; 11744cb5300bSChris Mason 11751751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 11764cb5300bSChris Mason goto out; 11774cb5300bSChris Mason 11784cb5300bSChris Mason /* 11794cb5300bSChris Mason * so now we have a nice long stream of locked 11804cb5300bSChris Mason * and up to date pages, lets wait on them 11814cb5300bSChris Mason */ 11824cb5300bSChris Mason for (i = 0; i < i_done; i++) 11834cb5300bSChris Mason wait_on_page_writeback(pages[i]); 11844cb5300bSChris Mason 11854cb5300bSChris Mason page_start = page_offset(pages[0]); 118609cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 11874cb5300bSChris Mason 11884cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1189ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 11904cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 11914cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 11929e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 1193ae0f1625SDavid Sterba &cached_state); 11944cb5300bSChris Mason 11951f12bd06SLiu Bo if (i_done != page_cnt) { 11969e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 11979e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 11989e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 1199bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 120009cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 120109cbfeafSKirill A. Shutemov (page_cnt - i_done) << PAGE_SHIFT); 12024cb5300bSChris Mason } 12034cb5300bSChris Mason 12044cb5300bSChris Mason 12059e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 1206018ed4f7SDavid Sterba &cached_state); 12074cb5300bSChris Mason 12084cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 12094cb5300bSChris Mason page_start, page_end - 1, &cached_state, 12104cb5300bSChris Mason GFP_NOFS); 12114cb5300bSChris Mason 12124cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12134cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 12144cb5300bSChris Mason ClearPageChecked(pages[i]); 12154cb5300bSChris Mason set_page_extent_mapped(pages[i]); 12164cb5300bSChris Mason set_page_dirty(pages[i]); 12174cb5300bSChris Mason unlock_page(pages[i]); 121809cbfeafSKirill A. Shutemov put_page(pages[i]); 12194cb5300bSChris Mason } 12208b62f87bSJosef Bacik btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1221364ecf36SQu Wenruo extent_changeset_free(data_reserved); 12224cb5300bSChris Mason return i_done; 12234cb5300bSChris Mason out: 12244cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12254cb5300bSChris Mason unlock_page(pages[i]); 122609cbfeafSKirill A. Shutemov put_page(pages[i]); 12274cb5300bSChris Mason } 1228bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 122909cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 123009cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12318b62f87bSJosef Bacik btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1232364ecf36SQu Wenruo extent_changeset_free(data_reserved); 12334cb5300bSChris Mason return ret; 12344cb5300bSChris Mason 12354cb5300bSChris Mason } 12364cb5300bSChris Mason 12374cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 12384cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 12394cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 12404cb5300bSChris Mason { 12410b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 12424cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 12434cb5300bSChris Mason struct file_ra_state *ra = NULL; 12444cb5300bSChris Mason unsigned long last_index; 1245151a31b2SLi Zefan u64 isize = i_size_read(inode); 1246940100a4SChris Mason u64 last_len = 0; 1247940100a4SChris Mason u64 skip = 0; 1248940100a4SChris Mason u64 defrag_end = 0; 12494cb5300bSChris Mason u64 newer_off = range->start; 1250f46b5a66SChristoph Hellwig unsigned long i; 1251008873eaSLi Zefan unsigned long ra_index = 0; 1252f46b5a66SChristoph Hellwig int ret; 12534cb5300bSChris Mason int defrag_count = 0; 12541a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1255aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 125609cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1257c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1258ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 12594cb5300bSChris Mason struct page **pages = NULL; 12601e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 12614cb5300bSChris Mason 12620abd5b17SLiu Bo if (isize == 0) 12630abd5b17SLiu Bo return 0; 12640abd5b17SLiu Bo 12650abd5b17SLiu Bo if (range->start >= isize) 12660abd5b17SLiu Bo return -EINVAL; 12671a419d85SLi Zefan 12681e2ef46dSDavid Sterba if (do_compress) { 12691a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 12701a419d85SLi Zefan return -EINVAL; 12711a419d85SLi Zefan if (range->compress_type) 12721a419d85SLi Zefan compress_type = range->compress_type; 12731a419d85SLi Zefan } 1274f46b5a66SChristoph Hellwig 12750abd5b17SLiu Bo if (extent_thresh == 0) 1276ee22184bSByongho Lee extent_thresh = SZ_256K; 1277f46b5a66SChristoph Hellwig 12784cb5300bSChris Mason /* 12790a52d108SDavid Sterba * If we were not given a file, allocate a readahead context. As 12800a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 12810a52d108SDavid Sterba * we don't error out. 12824cb5300bSChris Mason */ 12834cb5300bSChris Mason if (!file) { 128463e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 12850a52d108SDavid Sterba if (ra) 12864cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 12874cb5300bSChris Mason } else { 12884cb5300bSChris Mason ra = &file->f_ra; 12894cb5300bSChris Mason } 12904cb5300bSChris Mason 129163e727ecSDavid Sterba pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); 12924cb5300bSChris Mason if (!pages) { 12934cb5300bSChris Mason ret = -ENOMEM; 12944cb5300bSChris Mason goto out_ra; 12954cb5300bSChris Mason } 12964cb5300bSChris Mason 12974cb5300bSChris Mason /* find the last page to defrag */ 12981e701a32SChris Mason if (range->start + range->len > range->start) { 1299151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 130009cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 13011e701a32SChris Mason } else { 130209cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 13031e701a32SChris Mason } 13041e701a32SChris Mason 13054cb5300bSChris Mason if (newer_than) { 13064cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1307ee22184bSByongho Lee &newer_off, SZ_64K); 13084cb5300bSChris Mason if (!ret) { 13094cb5300bSChris Mason range->start = newer_off; 13104cb5300bSChris Mason /* 13114cb5300bSChris Mason * we always align our defrag to help keep 13124cb5300bSChris Mason * the extents in the file evenly spaced 13134cb5300bSChris Mason */ 131409cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 13154cb5300bSChris Mason } else 13164cb5300bSChris Mason goto out_ra; 13174cb5300bSChris Mason } else { 131809cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 13194cb5300bSChris Mason } 13204cb5300bSChris Mason if (!max_to_defrag) 1321070034bdSchandan max_to_defrag = last_index - i + 1; 13224cb5300bSChris Mason 13232a0f7f57SLi Zefan /* 13242a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 13252a0f7f57SLi Zefan * written sequentially. 13262a0f7f57SLi Zefan */ 13272a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 13282a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 13292a0f7f57SLi Zefan 1330f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 133109cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 13324cb5300bSChris Mason /* 13334cb5300bSChris Mason * make sure we stop running if someone unmounts 13344cb5300bSChris Mason * the FS 13354cb5300bSChris Mason */ 13361751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 13374cb5300bSChris Mason break; 13384cb5300bSChris Mason 13390b246afaSJeff Mahoney if (btrfs_defrag_cancelled(fs_info)) { 13400b246afaSJeff Mahoney btrfs_debug(fs_info, "defrag_file cancelled"); 1341210549ebSDavid Sterba ret = -EAGAIN; 1342210549ebSDavid Sterba break; 1343210549ebSDavid Sterba } 1344210549ebSDavid Sterba 134509cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 13466c282eb4SLi Zefan extent_thresh, &last_len, &skip, 13471e2ef46dSDavid Sterba &defrag_end, do_compress)){ 1348940100a4SChris Mason unsigned long next; 1349940100a4SChris Mason /* 1350940100a4SChris Mason * the should_defrag function tells us how much to skip 1351940100a4SChris Mason * bump our counter by the suggested amount 1352940100a4SChris Mason */ 135309cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1354940100a4SChris Mason i = max(i + 1, next); 1355940100a4SChris Mason continue; 1356940100a4SChris Mason } 1357008873eaSLi Zefan 1358008873eaSLi Zefan if (!newer_than) { 135909cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 136009cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1361008873eaSLi Zefan cluster = min(cluster, max_cluster); 1362008873eaSLi Zefan } else { 1363008873eaSLi Zefan cluster = max_cluster; 1364008873eaSLi Zefan } 1365008873eaSLi Zefan 1366008873eaSLi Zefan if (i + cluster > ra_index) { 1367008873eaSLi Zefan ra_index = max(i, ra_index); 13680a52d108SDavid Sterba if (ra) 1369d3c0bab5SDavid Sterba page_cache_sync_readahead(inode->i_mapping, ra, 1370d3c0bab5SDavid Sterba file, ra_index, cluster); 1371e4826a5bSchandan ra_index += cluster; 1372008873eaSLi Zefan } 13734cb5300bSChris Mason 13745955102cSAl Viro inode_lock(inode); 13751e2ef46dSDavid Sterba if (do_compress) 1376eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 1377008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1378ecb8bea8SLiu Bo if (ret < 0) { 13795955102cSAl Viro inode_unlock(inode); 13804cb5300bSChris Mason goto out_ra; 1381ecb8bea8SLiu Bo } 13824cb5300bSChris Mason 13834cb5300bSChris Mason defrag_count += ret; 1384d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 13855955102cSAl Viro inode_unlock(inode); 13864cb5300bSChris Mason 13874cb5300bSChris Mason if (newer_than) { 13884cb5300bSChris Mason if (newer_off == (u64)-1) 13894cb5300bSChris Mason break; 13904cb5300bSChris Mason 1391e1f041e1SLiu Bo if (ret > 0) 1392e1f041e1SLiu Bo i += ret; 1393e1f041e1SLiu Bo 13944cb5300bSChris Mason newer_off = max(newer_off + 1, 139509cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 13964cb5300bSChris Mason 1397ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1398ee22184bSByongho Lee &newer_off, SZ_64K); 13994cb5300bSChris Mason if (!ret) { 14004cb5300bSChris Mason range->start = newer_off; 140109cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14024cb5300bSChris Mason } else { 14034cb5300bSChris Mason break; 1404940100a4SChris Mason } 14054cb5300bSChris Mason } else { 1406008873eaSLi Zefan if (ret > 0) { 1407cbcc8326SLi Zefan i += ret; 140809cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1409008873eaSLi Zefan } else { 1410940100a4SChris Mason i++; 1411008873eaSLi Zefan last_len = 0; 1412008873eaSLi Zefan } 1413f46b5a66SChristoph Hellwig } 14144cb5300bSChris Mason } 1415f46b5a66SChristoph Hellwig 1416dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 14171e701a32SChris Mason filemap_flush(inode->i_mapping); 1418dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1419dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1420dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1421dec8ef90SFilipe Manana } 14221e701a32SChris Mason 14231a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 14240b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 14255c1aab1dSNick Terrell } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { 14265c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 14271a419d85SLi Zefan } 14281a419d85SLi Zefan 142960ccf82fSDiego Calleja ret = defrag_count; 1430940100a4SChris Mason 14314cb5300bSChris Mason out_ra: 14321e2ef46dSDavid Sterba if (do_compress) { 14335955102cSAl Viro inode_lock(inode); 1434eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 14355955102cSAl Viro inode_unlock(inode); 1436633085c7SFilipe David Borba Manana } 14374cb5300bSChris Mason if (!file) 14384cb5300bSChris Mason kfree(ra); 14394cb5300bSChris Mason kfree(pages); 1440940100a4SChris Mason return ret; 1441f46b5a66SChristoph Hellwig } 1442f46b5a66SChristoph Hellwig 1443198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 144476dda93cSYan, Zheng void __user *arg) 1445f46b5a66SChristoph Hellwig { 14460b246afaSJeff Mahoney struct inode *inode = file_inode(file); 14470b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1448f46b5a66SChristoph Hellwig u64 new_size; 1449f46b5a66SChristoph Hellwig u64 old_size; 1450f46b5a66SChristoph Hellwig u64 devid = 1; 14510b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1452f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1453f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1454f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1455f46b5a66SChristoph Hellwig char *sizestr; 14569a40f122SGui Hecheng char *retptr; 1457f46b5a66SChristoph Hellwig char *devstr = NULL; 1458f46b5a66SChristoph Hellwig int ret = 0; 1459f46b5a66SChristoph Hellwig int mod = 0; 1460f46b5a66SChristoph Hellwig 1461e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1462e441d54dSChris Mason return -EPERM; 1463e441d54dSChris Mason 1464198605a8SMiao Xie ret = mnt_want_write_file(file); 1465198605a8SMiao Xie if (ret) 1466198605a8SMiao Xie return ret; 1467198605a8SMiao Xie 1468171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 146997547676SMiao Xie mnt_drop_write_file(file); 1470e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1471c9e9f97bSIlya Dryomov } 1472c9e9f97bSIlya Dryomov 14730b246afaSJeff Mahoney mutex_lock(&fs_info->volume_mutex); 1474dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1475c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1476c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1477c9e9f97bSIlya Dryomov goto out; 1478c9e9f97bSIlya Dryomov } 14795516e595SMark Fasheh 14805516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1481f46b5a66SChristoph Hellwig 1482f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1483f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1484f46b5a66SChristoph Hellwig if (devstr) { 1485f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1486f46b5a66SChristoph Hellwig *devstr = '\0'; 1487f46b5a66SChristoph Hellwig devstr = vol_args->name; 148858dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 148958dfae63SZhangZhen if (ret) 149058dfae63SZhangZhen goto out_free; 1491dfd79829SMiao Xie if (!devid) { 1492dfd79829SMiao Xie ret = -EINVAL; 1493dfd79829SMiao Xie goto out_free; 1494dfd79829SMiao Xie } 14950b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1496f46b5a66SChristoph Hellwig } 1497dba60f3fSMiao Xie 14980b246afaSJeff Mahoney device = btrfs_find_device(fs_info, devid, NULL, NULL); 1499f46b5a66SChristoph Hellwig if (!device) { 15000b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1501c1c9ff7cSGeert Uytterhoeven devid); 1502dfd79829SMiao Xie ret = -ENODEV; 1503c9e9f97bSIlya Dryomov goto out_free; 1504f46b5a66SChristoph Hellwig } 1505dba60f3fSMiao Xie 1506ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 15070b246afaSJeff Mahoney btrfs_info(fs_info, 1508efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1509c1c9ff7cSGeert Uytterhoeven devid); 1510dfd79829SMiao Xie ret = -EPERM; 15114e42ae1bSLiu Bo goto out_free; 15124e42ae1bSLiu Bo } 15134e42ae1bSLiu Bo 1514f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1515f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1516f46b5a66SChristoph Hellwig else { 1517f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1518f46b5a66SChristoph Hellwig mod = -1; 1519f46b5a66SChristoph Hellwig sizestr++; 1520f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1521f46b5a66SChristoph Hellwig mod = 1; 1522f46b5a66SChristoph Hellwig sizestr++; 1523f46b5a66SChristoph Hellwig } 15249a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 15259a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1526f46b5a66SChristoph Hellwig ret = -EINVAL; 1527c9e9f97bSIlya Dryomov goto out_free; 1528f46b5a66SChristoph Hellwig } 1529f46b5a66SChristoph Hellwig } 1530f46b5a66SChristoph Hellwig 153163a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1532dfd79829SMiao Xie ret = -EPERM; 153363a212abSStefan Behrens goto out_free; 153463a212abSStefan Behrens } 153563a212abSStefan Behrens 15367cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1537f46b5a66SChristoph Hellwig 1538f46b5a66SChristoph Hellwig if (mod < 0) { 1539f46b5a66SChristoph Hellwig if (new_size > old_size) { 1540f46b5a66SChristoph Hellwig ret = -EINVAL; 1541c9e9f97bSIlya Dryomov goto out_free; 1542f46b5a66SChristoph Hellwig } 1543f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1544f46b5a66SChristoph Hellwig } else if (mod > 0) { 1545eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1546902c68a4SGui Hecheng ret = -ERANGE; 1547eb8052e0SWenliang Fan goto out_free; 1548eb8052e0SWenliang Fan } 1549f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1550f46b5a66SChristoph Hellwig } 1551f46b5a66SChristoph Hellwig 1552ee22184bSByongho Lee if (new_size < SZ_256M) { 1553f46b5a66SChristoph Hellwig ret = -EINVAL; 1554c9e9f97bSIlya Dryomov goto out_free; 1555f46b5a66SChristoph Hellwig } 1556f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1557f46b5a66SChristoph Hellwig ret = -EFBIG; 1558c9e9f97bSIlya Dryomov goto out_free; 1559f46b5a66SChristoph Hellwig } 1560f46b5a66SChristoph Hellwig 156147f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1562f46b5a66SChristoph Hellwig 15630b246afaSJeff Mahoney btrfs_info_in_rcu(fs_info, "new size for %s is %llu", 1564c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1565f46b5a66SChristoph Hellwig 1566f46b5a66SChristoph Hellwig if (new_size > old_size) { 1567a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 156898d5dc13STsutomu Itoh if (IS_ERR(trans)) { 156998d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1570c9e9f97bSIlya Dryomov goto out_free; 157198d5dc13STsutomu Itoh } 1572f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 15733a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1574ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1575f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 15760253f40eSjeff.liu } /* equal, nothing need to do */ 1577f46b5a66SChristoph Hellwig 1578c9e9f97bSIlya Dryomov out_free: 1579f46b5a66SChristoph Hellwig kfree(vol_args); 1580c9e9f97bSIlya Dryomov out: 15810b246afaSJeff Mahoney mutex_unlock(&fs_info->volume_mutex); 1582171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 158318f39c41SIlya Dryomov mnt_drop_write_file(file); 1584f46b5a66SChristoph Hellwig return ret; 1585f46b5a66SChristoph Hellwig } 1586f46b5a66SChristoph Hellwig 158772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 158852f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 15896f72c7e2SArne Jansen u64 *transid, bool readonly, 15908696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1591f46b5a66SChristoph Hellwig { 1592f46b5a66SChristoph Hellwig int namelen; 15933de4586cSChris Mason int ret = 0; 1594f46b5a66SChristoph Hellwig 1595325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1596325c50e3SJeff Mahoney return -ENOTDIR; 1597325c50e3SJeff Mahoney 1598a874a63eSLiu Bo ret = mnt_want_write_file(file); 1599a874a63eSLiu Bo if (ret) 1600a874a63eSLiu Bo goto out; 1601a874a63eSLiu Bo 160272fd032eSSage Weil namelen = strlen(name); 160372fd032eSSage Weil if (strchr(name, '/')) { 1604f46b5a66SChristoph Hellwig ret = -EINVAL; 1605a874a63eSLiu Bo goto out_drop_write; 1606f46b5a66SChristoph Hellwig } 1607f46b5a66SChristoph Hellwig 160816780cabSChris Mason if (name[0] == '.' && 160916780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 161016780cabSChris Mason ret = -EEXIST; 1611a874a63eSLiu Bo goto out_drop_write; 161216780cabSChris Mason } 161316780cabSChris Mason 16143de4586cSChris Mason if (subvol) { 161572fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 16166f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1617cb8e7090SChristoph Hellwig } else { 16182903ff01SAl Viro struct fd src = fdget(fd); 16193de4586cSChris Mason struct inode *src_inode; 16202903ff01SAl Viro if (!src.file) { 16213de4586cSChris Mason ret = -EINVAL; 1622a874a63eSLiu Bo goto out_drop_write; 16233de4586cSChris Mason } 16243de4586cSChris Mason 1625496ad9aaSAl Viro src_inode = file_inode(src.file); 1626496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1627c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1628efe120a0SFrank Holton "Snapshot src from another FS"); 162923ad5b17SKusanagi Kouichi ret = -EXDEV; 1630d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1631d0242061SDavid Sterba /* 1632d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1633d0242061SDavid Sterba * are limited to own subvolumes only 1634d0242061SDavid Sterba */ 1635d0242061SDavid Sterba ret = -EPERM; 1636ecd18815SAl Viro } else { 163772fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 163872fd032eSSage Weil BTRFS_I(src_inode)->root, 16396f72c7e2SArne Jansen transid, readonly, inherit); 1640ecd18815SAl Viro } 16412903ff01SAl Viro fdput(src); 1642cb8e7090SChristoph Hellwig } 1643a874a63eSLiu Bo out_drop_write: 1644a874a63eSLiu Bo mnt_drop_write_file(file); 1645f46b5a66SChristoph Hellwig out: 164672fd032eSSage Weil return ret; 164772fd032eSSage Weil } 164872fd032eSSage Weil 164972fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1650fa0d2b9bSLi Zefan void __user *arg, int subvol) 165172fd032eSSage Weil { 1652fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 165372fd032eSSage Weil int ret; 165472fd032eSSage Weil 1655325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1656325c50e3SJeff Mahoney return -ENOTDIR; 1657325c50e3SJeff Mahoney 1658fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1659fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1660fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1661fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1662fa0d2b9bSLi Zefan 1663fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1664b83cc969SLi Zefan vol_args->fd, subvol, 16656f72c7e2SArne Jansen NULL, false, NULL); 1666fa0d2b9bSLi Zefan 1667fa0d2b9bSLi Zefan kfree(vol_args); 1668fa0d2b9bSLi Zefan return ret; 1669fa0d2b9bSLi Zefan } 1670fa0d2b9bSLi Zefan 1671fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1672fa0d2b9bSLi Zefan void __user *arg, int subvol) 1673fa0d2b9bSLi Zefan { 1674fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1675fa0d2b9bSLi Zefan int ret; 1676fdfb1e4fSLi Zefan u64 transid = 0; 1677fdfb1e4fSLi Zefan u64 *ptr = NULL; 1678b83cc969SLi Zefan bool readonly = false; 16796f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 168072fd032eSSage Weil 1681325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1682325c50e3SJeff Mahoney return -ENOTDIR; 1683325c50e3SJeff Mahoney 1684fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1685fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1686fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1687fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1688fdfb1e4fSLi Zefan 1689b83cc969SLi Zefan if (vol_args->flags & 16906f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 16916f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1692b83cc969SLi Zefan ret = -EOPNOTSUPP; 1693c47ca32dSDan Carpenter goto free_args; 1694fdfb1e4fSLi Zefan } 1695fdfb1e4fSLi Zefan 1696fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1697fdfb1e4fSLi Zefan ptr = &transid; 1698b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1699b83cc969SLi Zefan readonly = true; 17006f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 170109cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 17026f72c7e2SArne Jansen ret = -EINVAL; 1703c47ca32dSDan Carpenter goto free_args; 17046f72c7e2SArne Jansen } 17056f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 17066f72c7e2SArne Jansen if (IS_ERR(inherit)) { 17076f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1708c47ca32dSDan Carpenter goto free_args; 17096f72c7e2SArne Jansen } 17106f72c7e2SArne Jansen } 171175eaa0e2SSage Weil 1712fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 17136f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 17148696c533SMiao Xie readonly, inherit); 1715c47ca32dSDan Carpenter if (ret) 1716c47ca32dSDan Carpenter goto free_inherit; 171775eaa0e2SSage Weil 1718c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1719fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1720c47ca32dSDan Carpenter transid), 1721c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 172275eaa0e2SSage Weil ret = -EFAULT; 1723c47ca32dSDan Carpenter 1724c47ca32dSDan Carpenter free_inherit: 17256f72c7e2SArne Jansen kfree(inherit); 1726c47ca32dSDan Carpenter free_args: 1727c47ca32dSDan Carpenter kfree(vol_args); 1728f46b5a66SChristoph Hellwig return ret; 1729f46b5a66SChristoph Hellwig } 1730f46b5a66SChristoph Hellwig 17310caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 17320caa102dSLi Zefan void __user *arg) 17330caa102dSLi Zefan { 1734496ad9aaSAl Viro struct inode *inode = file_inode(file); 17350b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 17360caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17370caa102dSLi Zefan int ret = 0; 17380caa102dSLi Zefan u64 flags = 0; 17390caa102dSLi Zefan 17404a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 17410caa102dSLi Zefan return -EINVAL; 17420caa102dSLi Zefan 17430b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 17440caa102dSLi Zefan if (btrfs_root_readonly(root)) 17450caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 17460b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 17470caa102dSLi Zefan 17480caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 17490caa102dSLi Zefan ret = -EFAULT; 17500caa102dSLi Zefan 17510caa102dSLi Zefan return ret; 17520caa102dSLi Zefan } 17530caa102dSLi Zefan 17540caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 17550caa102dSLi Zefan void __user *arg) 17560caa102dSLi Zefan { 1757496ad9aaSAl Viro struct inode *inode = file_inode(file); 17580b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 17590caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17600caa102dSLi Zefan struct btrfs_trans_handle *trans; 17610caa102dSLi Zefan u64 root_flags; 17620caa102dSLi Zefan u64 flags; 17630caa102dSLi Zefan int ret = 0; 17640caa102dSLi Zefan 1765bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1766bd60ea0fSDavid Sterba return -EPERM; 1767bd60ea0fSDavid Sterba 1768b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1769b9ca0664SLiu Bo if (ret) 1770b9ca0664SLiu Bo goto out; 17710caa102dSLi Zefan 17724a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1773b9ca0664SLiu Bo ret = -EINVAL; 1774b9ca0664SLiu Bo goto out_drop_write; 1775b9ca0664SLiu Bo } 17760caa102dSLi Zefan 1777b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1778b9ca0664SLiu Bo ret = -EFAULT; 1779b9ca0664SLiu Bo goto out_drop_write; 1780b9ca0664SLiu Bo } 17810caa102dSLi Zefan 1782b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1783b9ca0664SLiu Bo ret = -EINVAL; 1784b9ca0664SLiu Bo goto out_drop_write; 1785b9ca0664SLiu Bo } 17860caa102dSLi Zefan 1787b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1788b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1789b9ca0664SLiu Bo goto out_drop_write; 1790b9ca0664SLiu Bo } 17910caa102dSLi Zefan 17920b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 17930caa102dSLi Zefan 17940caa102dSLi Zefan /* nothing to do */ 17950caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1796b9ca0664SLiu Bo goto out_drop_sem; 17970caa102dSLi Zefan 17980caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 17992c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 18000caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18010caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 18022c686537SDavid Sterba } else { 18032c686537SDavid Sterba /* 18042c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 18052c686537SDavid Sterba * send 18062c686537SDavid Sterba */ 18072c686537SDavid Sterba spin_lock(&root->root_item_lock); 18082c686537SDavid Sterba if (root->send_in_progress == 0) { 18090caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18100caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 18112c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18122c686537SDavid Sterba } else { 18132c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18140b246afaSJeff Mahoney btrfs_warn(fs_info, 18152c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 18162c686537SDavid Sterba root->root_key.objectid); 18172c686537SDavid Sterba ret = -EPERM; 18182c686537SDavid Sterba goto out_drop_sem; 18192c686537SDavid Sterba } 18202c686537SDavid Sterba } 18210caa102dSLi Zefan 18220caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 18230caa102dSLi Zefan if (IS_ERR(trans)) { 18240caa102dSLi Zefan ret = PTR_ERR(trans); 18250caa102dSLi Zefan goto out_reset; 18260caa102dSLi Zefan } 18270caa102dSLi Zefan 18280b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 18290caa102dSLi Zefan &root->root_key, &root->root_item); 18309417ebc8SNikolay Borisov if (ret < 0) { 18319417ebc8SNikolay Borisov btrfs_end_transaction(trans); 18329417ebc8SNikolay Borisov goto out_reset; 18339417ebc8SNikolay Borisov } 18340caa102dSLi Zefan 18359417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 18369417ebc8SNikolay Borisov 18370caa102dSLi Zefan out_reset: 18380caa102dSLi Zefan if (ret) 18390caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1840b9ca0664SLiu Bo out_drop_sem: 18410b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 1842b9ca0664SLiu Bo out_drop_write: 1843b9ca0664SLiu Bo mnt_drop_write_file(file); 1844b9ca0664SLiu Bo out: 18450caa102dSLi Zefan return ret; 18460caa102dSLi Zefan } 18470caa102dSLi Zefan 184876dda93cSYan, Zheng /* 184976dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 185076dda93cSYan, Zheng */ 185176dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 185276dda93cSYan, Zheng { 18530b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 185476dda93cSYan, Zheng struct btrfs_path *path; 1855175a2b87SJosef Bacik struct btrfs_dir_item *di; 185676dda93cSYan, Zheng struct btrfs_key key; 1857175a2b87SJosef Bacik u64 dir_id; 185876dda93cSYan, Zheng int ret; 185976dda93cSYan, Zheng 186076dda93cSYan, Zheng path = btrfs_alloc_path(); 186176dda93cSYan, Zheng if (!path) 186276dda93cSYan, Zheng return -ENOMEM; 186376dda93cSYan, Zheng 1864175a2b87SJosef Bacik /* Make sure this root isn't set as the default subvol */ 18650b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 18660b246afaSJeff Mahoney di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, 1867175a2b87SJosef Bacik dir_id, "default", 7, 0); 1868175a2b87SJosef Bacik if (di && !IS_ERR(di)) { 1869175a2b87SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1870175a2b87SJosef Bacik if (key.objectid == root->root_key.objectid) { 187172de6b53SGuangyu Sun ret = -EPERM; 18720b246afaSJeff Mahoney btrfs_err(fs_info, 18735d163e0eSJeff Mahoney "deleting default subvolume %llu is not allowed", 18745d163e0eSJeff Mahoney key.objectid); 1875175a2b87SJosef Bacik goto out; 1876175a2b87SJosef Bacik } 1877175a2b87SJosef Bacik btrfs_release_path(path); 1878175a2b87SJosef Bacik } 1879175a2b87SJosef Bacik 188076dda93cSYan, Zheng key.objectid = root->root_key.objectid; 188176dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 188276dda93cSYan, Zheng key.offset = (u64)-1; 188376dda93cSYan, Zheng 18840b246afaSJeff Mahoney ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 188576dda93cSYan, Zheng if (ret < 0) 188676dda93cSYan, Zheng goto out; 188776dda93cSYan, Zheng BUG_ON(ret == 0); 188876dda93cSYan, Zheng 188976dda93cSYan, Zheng ret = 0; 189076dda93cSYan, Zheng if (path->slots[0] > 0) { 189176dda93cSYan, Zheng path->slots[0]--; 189276dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 189376dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 189476dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 189576dda93cSYan, Zheng ret = -ENOTEMPTY; 189676dda93cSYan, Zheng } 189776dda93cSYan, Zheng out: 189876dda93cSYan, Zheng btrfs_free_path(path); 189976dda93cSYan, Zheng return ret; 190076dda93cSYan, Zheng } 190176dda93cSYan, Zheng 1902ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1903ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1904ac8e9819SChris Mason { 1905abc6e134SChris Mason struct btrfs_key test; 1906abc6e134SChris Mason int ret; 1907abc6e134SChris Mason 1908abc6e134SChris Mason test.objectid = sk->min_objectid; 1909abc6e134SChris Mason test.type = sk->min_type; 1910abc6e134SChris Mason test.offset = sk->min_offset; 1911abc6e134SChris Mason 1912abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1913abc6e134SChris Mason if (ret < 0) 1914ac8e9819SChris Mason return 0; 1915abc6e134SChris Mason 1916abc6e134SChris Mason test.objectid = sk->max_objectid; 1917abc6e134SChris Mason test.type = sk->max_type; 1918abc6e134SChris Mason test.offset = sk->max_offset; 1919abc6e134SChris Mason 1920abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1921abc6e134SChris Mason if (ret > 0) 1922ac8e9819SChris Mason return 0; 1923ac8e9819SChris Mason return 1; 1924ac8e9819SChris Mason } 1925ac8e9819SChris Mason 1926df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 1927ac8e9819SChris Mason struct btrfs_key *key, 1928ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 19299b6e817dSGerhard Heift size_t *buf_size, 1930ba346b35SGerhard Heift char __user *ubuf, 1931ac8e9819SChris Mason unsigned long *sk_offset, 1932ac8e9819SChris Mason int *num_found) 1933ac8e9819SChris Mason { 1934ac8e9819SChris Mason u64 found_transid; 1935ac8e9819SChris Mason struct extent_buffer *leaf; 1936ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1937dd81d459SNaohiro Aota struct btrfs_key test; 1938ac8e9819SChris Mason unsigned long item_off; 1939ac8e9819SChris Mason unsigned long item_len; 1940ac8e9819SChris Mason int nritems; 1941ac8e9819SChris Mason int i; 1942ac8e9819SChris Mason int slot; 1943ac8e9819SChris Mason int ret = 0; 1944ac8e9819SChris Mason 1945ac8e9819SChris Mason leaf = path->nodes[0]; 1946ac8e9819SChris Mason slot = path->slots[0]; 1947ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1948ac8e9819SChris Mason 1949ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1950ac8e9819SChris Mason i = nritems; 1951ac8e9819SChris Mason goto advance_key; 1952ac8e9819SChris Mason } 1953ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1954ac8e9819SChris Mason 1955ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1956ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1957ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1958ac8e9819SChris Mason 195903b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 196003b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 196103b71c6cSGabriel de Perthuis continue; 196203b71c6cSGabriel de Perthuis 19639b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 19648f5f6178SGerhard Heift if (*num_found) { 1965ac8e9819SChris Mason ret = 1; 19668f5f6178SGerhard Heift goto out; 19678f5f6178SGerhard Heift } 19688f5f6178SGerhard Heift 19698f5f6178SGerhard Heift /* 19708f5f6178SGerhard Heift * return one empty item back for v1, which does not 19718f5f6178SGerhard Heift * handle -EOVERFLOW 19728f5f6178SGerhard Heift */ 19738f5f6178SGerhard Heift 19749b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 1975ac8e9819SChris Mason item_len = 0; 19768f5f6178SGerhard Heift ret = -EOVERFLOW; 19778f5f6178SGerhard Heift } 1978ac8e9819SChris Mason 19799b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 1980ac8e9819SChris Mason ret = 1; 198125c9bc2eSGerhard Heift goto out; 1982ac8e9819SChris Mason } 1983ac8e9819SChris Mason 1984ac8e9819SChris Mason sh.objectid = key->objectid; 1985ac8e9819SChris Mason sh.offset = key->offset; 1986ac8e9819SChris Mason sh.type = key->type; 1987ac8e9819SChris Mason sh.len = item_len; 1988ac8e9819SChris Mason sh.transid = found_transid; 1989ac8e9819SChris Mason 1990ac8e9819SChris Mason /* copy search result header */ 1991ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 1992ba346b35SGerhard Heift ret = -EFAULT; 1993ba346b35SGerhard Heift goto out; 1994ba346b35SGerhard Heift } 1995ba346b35SGerhard Heift 1996ac8e9819SChris Mason *sk_offset += sizeof(sh); 1997ac8e9819SChris Mason 1998ac8e9819SChris Mason if (item_len) { 1999ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 2000ac8e9819SChris Mason /* copy the item */ 2001ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2002ba346b35SGerhard Heift item_off, item_len)) { 2003ba346b35SGerhard Heift ret = -EFAULT; 2004ba346b35SGerhard Heift goto out; 2005ba346b35SGerhard Heift } 2006ba346b35SGerhard Heift 2007ac8e9819SChris Mason *sk_offset += item_len; 2008ac8e9819SChris Mason } 2009e2156867SHugo Mills (*num_found)++; 2010ac8e9819SChris Mason 20118f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 20128f5f6178SGerhard Heift goto out; 20138f5f6178SGerhard Heift 201425c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 201525c9bc2eSGerhard Heift ret = 1; 201625c9bc2eSGerhard Heift goto out; 201725c9bc2eSGerhard Heift } 2018ac8e9819SChris Mason } 2019ac8e9819SChris Mason advance_key: 2020ac8e9819SChris Mason ret = 0; 2021dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2022dd81d459SNaohiro Aota test.type = sk->max_type; 2023dd81d459SNaohiro Aota test.offset = sk->max_offset; 2024dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2025dd81d459SNaohiro Aota ret = 1; 2026dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2027abc6e134SChris Mason key->offset++; 2028dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2029abc6e134SChris Mason key->offset = 0; 2030abc6e134SChris Mason key->type++; 2031dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2032abc6e134SChris Mason key->offset = 0; 2033abc6e134SChris Mason key->type = 0; 2034abc6e134SChris Mason key->objectid++; 2035abc6e134SChris Mason } else 2036abc6e134SChris Mason ret = 1; 203725c9bc2eSGerhard Heift out: 2038ba346b35SGerhard Heift /* 2039ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2040ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2041ba346b35SGerhard Heift * * all items were found 2042ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2043ba346b35SGerhard Heift * leaf 2044ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2045ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2046ba346b35SGerhard Heift */ 2047ac8e9819SChris Mason return ret; 2048ac8e9819SChris Mason } 2049ac8e9819SChris Mason 2050ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 205112544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 20529b6e817dSGerhard Heift size_t *buf_size, 2053ba346b35SGerhard Heift char __user *ubuf) 2054ac8e9819SChris Mason { 20550b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2056ac8e9819SChris Mason struct btrfs_root *root; 2057ac8e9819SChris Mason struct btrfs_key key; 2058ac8e9819SChris Mason struct btrfs_path *path; 2059ac8e9819SChris Mason int ret; 2060ac8e9819SChris Mason int num_found = 0; 2061ac8e9819SChris Mason unsigned long sk_offset = 0; 2062ac8e9819SChris Mason 20639b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 20649b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 206512544442SGerhard Heift return -EOVERFLOW; 20669b6e817dSGerhard Heift } 206712544442SGerhard Heift 2068ac8e9819SChris Mason path = btrfs_alloc_path(); 2069ac8e9819SChris Mason if (!path) 2070ac8e9819SChris Mason return -ENOMEM; 2071ac8e9819SChris Mason 2072ac8e9819SChris Mason if (sk->tree_id == 0) { 2073ac8e9819SChris Mason /* search the root of the inode that was passed */ 2074ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2075ac8e9819SChris Mason } else { 2076ac8e9819SChris Mason key.objectid = sk->tree_id; 2077ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2078ac8e9819SChris Mason key.offset = (u64)-1; 2079ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2080ac8e9819SChris Mason if (IS_ERR(root)) { 2081ac8e9819SChris Mason btrfs_free_path(path); 2082ac8e9819SChris Mason return -ENOENT; 2083ac8e9819SChris Mason } 2084ac8e9819SChris Mason } 2085ac8e9819SChris Mason 2086ac8e9819SChris Mason key.objectid = sk->min_objectid; 2087ac8e9819SChris Mason key.type = sk->min_type; 2088ac8e9819SChris Mason key.offset = sk->min_offset; 2089ac8e9819SChris Mason 2090ac8e9819SChris Mason while (1) { 20916174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2092ac8e9819SChris Mason if (ret != 0) { 2093ac8e9819SChris Mason if (ret > 0) 2094ac8e9819SChris Mason ret = 0; 2095ac8e9819SChris Mason goto err; 2096ac8e9819SChris Mason } 2097df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2098ac8e9819SChris Mason &sk_offset, &num_found); 2099b3b4aa74SDavid Sterba btrfs_release_path(path); 210025c9bc2eSGerhard Heift if (ret) 2101ac8e9819SChris Mason break; 2102ac8e9819SChris Mason 2103ac8e9819SChris Mason } 21048f5f6178SGerhard Heift if (ret > 0) 2105ac8e9819SChris Mason ret = 0; 2106ac8e9819SChris Mason err: 2107ac8e9819SChris Mason sk->nr_items = num_found; 2108ac8e9819SChris Mason btrfs_free_path(path); 2109ac8e9819SChris Mason return ret; 2110ac8e9819SChris Mason } 2111ac8e9819SChris Mason 2112ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2113ac8e9819SChris Mason void __user *argp) 2114ac8e9819SChris Mason { 2115ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2116ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2117ac8e9819SChris Mason struct inode *inode; 2118ac8e9819SChris Mason int ret; 21199b6e817dSGerhard Heift size_t buf_size; 2120ac8e9819SChris Mason 2121ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2122ac8e9819SChris Mason return -EPERM; 2123ac8e9819SChris Mason 2124ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2125ac8e9819SChris Mason 2126ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2127ba346b35SGerhard Heift return -EFAULT; 2128ba346b35SGerhard Heift 2129ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2130ac8e9819SChris Mason 2131496ad9aaSAl Viro inode = file_inode(file); 2132ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 21338f5f6178SGerhard Heift 21348f5f6178SGerhard Heift /* 21358f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 21368f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 21378f5f6178SGerhard Heift */ 21388f5f6178SGerhard Heift if (ret == -EOVERFLOW) 21398f5f6178SGerhard Heift ret = 0; 21408f5f6178SGerhard Heift 2141ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2142ac8e9819SChris Mason ret = -EFAULT; 2143ac8e9819SChris Mason return ret; 2144ac8e9819SChris Mason } 2145ac8e9819SChris Mason 2146cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2147cc68a8a5SGerhard Heift void __user *argp) 2148cc68a8a5SGerhard Heift { 2149cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2150cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2151cc68a8a5SGerhard Heift struct inode *inode; 2152cc68a8a5SGerhard Heift int ret; 2153cc68a8a5SGerhard Heift size_t buf_size; 2154ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2155cc68a8a5SGerhard Heift 2156cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2157cc68a8a5SGerhard Heift return -EPERM; 2158cc68a8a5SGerhard Heift 2159cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2160cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2161cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2162cc68a8a5SGerhard Heift return -EFAULT; 2163cc68a8a5SGerhard Heift 2164cc68a8a5SGerhard Heift buf_size = args.buf_size; 2165cc68a8a5SGerhard Heift 2166cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2167cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2168cc68a8a5SGerhard Heift buf_size = buf_limit; 2169cc68a8a5SGerhard Heift 2170cc68a8a5SGerhard Heift inode = file_inode(file); 2171cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2172718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2173cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2174cc68a8a5SGerhard Heift ret = -EFAULT; 2175cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2176cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2177cc68a8a5SGerhard Heift ret = -EFAULT; 2178cc68a8a5SGerhard Heift 217976dda93cSYan, Zheng return ret; 218076dda93cSYan, Zheng } 218176dda93cSYan, Zheng 218298d377a0STARUISI Hiroaki /* 2183ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2184ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 218598d377a0STARUISI Hiroaki */ 218698d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 218798d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 218898d377a0STARUISI Hiroaki { 218998d377a0STARUISI Hiroaki struct btrfs_root *root; 219098d377a0STARUISI Hiroaki struct btrfs_key key; 2191ac8e9819SChris Mason char *ptr; 219298d377a0STARUISI Hiroaki int ret = -1; 219398d377a0STARUISI Hiroaki int slot; 219498d377a0STARUISI Hiroaki int len; 219598d377a0STARUISI Hiroaki int total_len = 0; 219698d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 219798d377a0STARUISI Hiroaki struct extent_buffer *l; 219898d377a0STARUISI Hiroaki struct btrfs_path *path; 219998d377a0STARUISI Hiroaki 220098d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 220198d377a0STARUISI Hiroaki name[0]='\0'; 220298d377a0STARUISI Hiroaki return 0; 220398d377a0STARUISI Hiroaki } 220498d377a0STARUISI Hiroaki 220598d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 220698d377a0STARUISI Hiroaki if (!path) 220798d377a0STARUISI Hiroaki return -ENOMEM; 220898d377a0STARUISI Hiroaki 2209c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 221098d377a0STARUISI Hiroaki 221198d377a0STARUISI Hiroaki key.objectid = tree_id; 221298d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 221398d377a0STARUISI Hiroaki key.offset = (u64)-1; 221498d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 221598d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2216f14d104dSDavid Sterba btrfs_err(info, "could not find root %llu", tree_id); 22178ad6fcabSChris Mason ret = -ENOENT; 22188ad6fcabSChris Mason goto out; 221998d377a0STARUISI Hiroaki } 222098d377a0STARUISI Hiroaki 222198d377a0STARUISI Hiroaki key.objectid = dirid; 222298d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 22238ad6fcabSChris Mason key.offset = (u64)-1; 222498d377a0STARUISI Hiroaki 222598d377a0STARUISI Hiroaki while (1) { 222698d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 222798d377a0STARUISI Hiroaki if (ret < 0) 222898d377a0STARUISI Hiroaki goto out; 222918674c6cSFilipe David Borba Manana else if (ret > 0) { 223018674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 223118674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 223218674c6cSFilipe David Borba Manana if (ret < 0) 223318674c6cSFilipe David Borba Manana goto out; 223418674c6cSFilipe David Borba Manana else if (ret > 0) { 2235ac8e9819SChris Mason ret = -ENOENT; 223698d377a0STARUISI Hiroaki goto out; 2237ac8e9819SChris Mason } 223818674c6cSFilipe David Borba Manana } 223918674c6cSFilipe David Borba Manana 224018674c6cSFilipe David Borba Manana l = path->nodes[0]; 224118674c6cSFilipe David Borba Manana slot = path->slots[0]; 224218674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 224398d377a0STARUISI Hiroaki 224498d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 224598d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 224698d377a0STARUISI Hiroaki ptr -= len + 1; 224798d377a0STARUISI Hiroaki total_len += len + 1; 2248a696cf35SFilipe David Borba Manana if (ptr < name) { 2249a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 225098d377a0STARUISI Hiroaki goto out; 2251a696cf35SFilipe David Borba Manana } 225298d377a0STARUISI Hiroaki 225398d377a0STARUISI Hiroaki *(ptr + len) = '/'; 225498d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 225598d377a0STARUISI Hiroaki 225698d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 225798d377a0STARUISI Hiroaki break; 225898d377a0STARUISI Hiroaki 2259b3b4aa74SDavid Sterba btrfs_release_path(path); 226098d377a0STARUISI Hiroaki key.objectid = key.offset; 22618ad6fcabSChris Mason key.offset = (u64)-1; 226298d377a0STARUISI Hiroaki dirid = key.objectid; 226398d377a0STARUISI Hiroaki } 226477906a50SLi Zefan memmove(name, ptr, total_len); 226598d377a0STARUISI Hiroaki name[total_len] = '\0'; 226698d377a0STARUISI Hiroaki ret = 0; 226798d377a0STARUISI Hiroaki out: 226898d377a0STARUISI Hiroaki btrfs_free_path(path); 2269ac8e9819SChris Mason return ret; 2270ac8e9819SChris Mason } 2271ac8e9819SChris Mason 2272ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2273ac8e9819SChris Mason void __user *argp) 2274ac8e9819SChris Mason { 2275ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2276ac8e9819SChris Mason struct inode *inode; 227701b810b8SDavid Sterba int ret = 0; 2278ac8e9819SChris Mason 22792354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 22802354d08fSJulia Lawall if (IS_ERR(args)) 22812354d08fSJulia Lawall return PTR_ERR(args); 2282c2b96929SDan Carpenter 2283496ad9aaSAl Viro inode = file_inode(file); 2284ac8e9819SChris Mason 228501b810b8SDavid Sterba /* 228601b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 228701b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 228801b810b8SDavid Sterba */ 22891b53ac4dSChris Mason if (args->treeid == 0) 22901b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 22911b53ac4dSChris Mason 229201b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 229301b810b8SDavid Sterba args->name[0] = 0; 229401b810b8SDavid Sterba goto out; 229501b810b8SDavid Sterba } 229601b810b8SDavid Sterba 229701b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 229801b810b8SDavid Sterba ret = -EPERM; 229901b810b8SDavid Sterba goto out; 230001b810b8SDavid Sterba } 230101b810b8SDavid Sterba 2302ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2303ac8e9819SChris Mason args->treeid, args->objectid, 2304ac8e9819SChris Mason args->name); 2305ac8e9819SChris Mason 230601b810b8SDavid Sterba out: 2307ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2308ac8e9819SChris Mason ret = -EFAULT; 2309ac8e9819SChris Mason 2310ac8e9819SChris Mason kfree(args); 231198d377a0STARUISI Hiroaki return ret; 231298d377a0STARUISI Hiroaki } 231398d377a0STARUISI Hiroaki 231476dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 231576dda93cSYan, Zheng void __user *arg) 231676dda93cSYan, Zheng { 231754563d41SAl Viro struct dentry *parent = file->f_path.dentry; 23180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 231976dda93cSYan, Zheng struct dentry *dentry; 23202b0143b5SDavid Howells struct inode *dir = d_inode(parent); 232176dda93cSYan, Zheng struct inode *inode; 232276dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 232376dda93cSYan, Zheng struct btrfs_root *dest = NULL; 232476dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 232576dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2326c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2327521e0546SDavid Sterba u64 root_flags; 2328c58aaad2SMiao Xie u64 qgroup_reserved; 232976dda93cSYan, Zheng int namelen; 233076dda93cSYan, Zheng int ret; 233176dda93cSYan, Zheng int err = 0; 233276dda93cSYan, Zheng 2333325c50e3SJeff Mahoney if (!S_ISDIR(dir->i_mode)) 2334325c50e3SJeff Mahoney return -ENOTDIR; 2335325c50e3SJeff Mahoney 233676dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 233776dda93cSYan, Zheng if (IS_ERR(vol_args)) 233876dda93cSYan, Zheng return PTR_ERR(vol_args); 233976dda93cSYan, Zheng 234076dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 234176dda93cSYan, Zheng namelen = strlen(vol_args->name); 234276dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 234376dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 234476dda93cSYan, Zheng err = -EINVAL; 234576dda93cSYan, Zheng goto out; 234676dda93cSYan, Zheng } 234776dda93cSYan, Zheng 2348a561be71SAl Viro err = mnt_want_write_file(file); 234976dda93cSYan, Zheng if (err) 235076dda93cSYan, Zheng goto out; 235176dda93cSYan, Zheng 2352521e0546SDavid Sterba 235300235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 235400235411SAl Viro if (err == -EINTR) 235500235411SAl Viro goto out_drop_write; 235676dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 235776dda93cSYan, Zheng if (IS_ERR(dentry)) { 235876dda93cSYan, Zheng err = PTR_ERR(dentry); 235976dda93cSYan, Zheng goto out_unlock_dir; 236076dda93cSYan, Zheng } 236176dda93cSYan, Zheng 23622b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 236376dda93cSYan, Zheng err = -ENOENT; 236476dda93cSYan, Zheng goto out_dput; 236576dda93cSYan, Zheng } 236676dda93cSYan, Zheng 23672b0143b5SDavid Howells inode = d_inode(dentry); 23684260f7c7SSage Weil dest = BTRFS_I(inode)->root; 23694260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 23704260f7c7SSage Weil /* 23714260f7c7SSage Weil * Regular user. Only allow this with a special mount 23724260f7c7SSage Weil * option, when the user has write+exec access to the 23734260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23744260f7c7SSage Weil * allowed. 23754260f7c7SSage Weil * 23764260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23774260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23784260f7c7SSage Weil * otherwise be able to delete. 23794260f7c7SSage Weil * 23804260f7c7SSage Weil * Users who want to delete empty subvols should try 23814260f7c7SSage Weil * rmdir(2). 23824260f7c7SSage Weil */ 23834260f7c7SSage Weil err = -EPERM; 23840b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 23854260f7c7SSage Weil goto out_dput; 23864260f7c7SSage Weil 23874260f7c7SSage Weil /* 23884260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 23894260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 23904260f7c7SSage Weil * must be called on the dentry referencing the root 23914260f7c7SSage Weil * of the subvol, not a random directory contained 23924260f7c7SSage Weil * within it. 23934260f7c7SSage Weil */ 23944260f7c7SSage Weil err = -EINVAL; 23954260f7c7SSage Weil if (root == dest) 23964260f7c7SSage Weil goto out_dput; 23974260f7c7SSage Weil 23984260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 23994260f7c7SSage Weil if (err) 24004260f7c7SSage Weil goto out_dput; 24015c39da5bSMiao Xie } 24024260f7c7SSage Weil 24035c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 24044260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 24054260f7c7SSage Weil if (err) 24064260f7c7SSage Weil goto out_dput; 24074260f7c7SSage Weil 24084a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 240976dda93cSYan, Zheng err = -EINVAL; 241076dda93cSYan, Zheng goto out_dput; 241176dda93cSYan, Zheng } 241276dda93cSYan, Zheng 24135955102cSAl Viro inode_lock(inode); 2414521e0546SDavid Sterba 2415521e0546SDavid Sterba /* 2416521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2417521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2418521e0546SDavid Sterba * again is not run concurrently. 2419521e0546SDavid Sterba */ 2420521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2421c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2422c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2423c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2424521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2425521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2426521e0546SDavid Sterba } else { 2427521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 24280b246afaSJeff Mahoney btrfs_warn(fs_info, 2429521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2430c55bfa67SFilipe Manana dest->root_key.objectid); 2431521e0546SDavid Sterba err = -EPERM; 2432909e26dcSOmar Sandoval goto out_unlock_inode; 2433521e0546SDavid Sterba } 2434521e0546SDavid Sterba 24350b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 243676dda93cSYan, Zheng 243776dda93cSYan, Zheng err = may_destroy_subvol(dest); 243876dda93cSYan, Zheng if (err) 243976dda93cSYan, Zheng goto out_up_write; 244076dda93cSYan, Zheng 2441c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2442c58aaad2SMiao Xie /* 2443c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2444c58aaad2SMiao Xie * ref/backref. 2445c58aaad2SMiao Xie */ 2446c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2447ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2448c58aaad2SMiao Xie if (err) 2449c58aaad2SMiao Xie goto out_up_write; 2450c58aaad2SMiao Xie 2451a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2452a22285a6SYan, Zheng if (IS_ERR(trans)) { 2453a22285a6SYan, Zheng err = PTR_ERR(trans); 2454c58aaad2SMiao Xie goto out_release; 2455a22285a6SYan, Zheng } 2456c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2457c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2458a22285a6SYan, Zheng 245943663557SNikolay Borisov btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); 24602be63d5cSFilipe Manana 246176dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 246276dda93cSYan, Zheng dest->root_key.objectid, 246376dda93cSYan, Zheng dentry->d_name.name, 246476dda93cSYan, Zheng dentry->d_name.len); 246579787eaaSJeff Mahoney if (ret) { 246679787eaaSJeff Mahoney err = ret; 246766642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 246879787eaaSJeff Mahoney goto out_end_trans; 246979787eaaSJeff Mahoney } 247076dda93cSYan, Zheng 247176dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 247276dda93cSYan, Zheng 247376dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 247476dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 247576dda93cSYan, Zheng dest->root_item.drop_level = 0; 247676dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 247776dda93cSYan, Zheng 247827cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 247976dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 24800b246afaSJeff Mahoney fs_info->tree_root, 248176dda93cSYan, Zheng dest->root_key.objectid); 248279787eaaSJeff Mahoney if (ret) { 248366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 248479787eaaSJeff Mahoney err = ret; 248579787eaaSJeff Mahoney goto out_end_trans; 2486d68fc57bSYan, Zheng } 248779787eaaSJeff Mahoney } 2488dd5f9615SStefan Behrens 24890b246afaSJeff Mahoney ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid, 24906bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, 2491dd5f9615SStefan Behrens dest->root_key.objectid); 2492dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 249366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 2494dd5f9615SStefan Behrens err = ret; 2495dd5f9615SStefan Behrens goto out_end_trans; 2496dd5f9615SStefan Behrens } 2497dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 24980b246afaSJeff Mahoney ret = btrfs_uuid_tree_rem(trans, fs_info, 2499dd5f9615SStefan Behrens dest->root_item.received_uuid, 2500dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2501dd5f9615SStefan Behrens dest->root_key.objectid); 2502dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 250366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 2504dd5f9615SStefan Behrens err = ret; 2505dd5f9615SStefan Behrens goto out_end_trans; 2506dd5f9615SStefan Behrens } 2507dd5f9615SStefan Behrens } 2508dd5f9615SStefan Behrens 250979787eaaSJeff Mahoney out_end_trans: 2510c58aaad2SMiao Xie trans->block_rsv = NULL; 2511c58aaad2SMiao Xie trans->bytes_reserved = 0; 25123a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 251379787eaaSJeff Mahoney if (ret && !err) 251479787eaaSJeff Mahoney err = ret; 251576dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2516c58aaad2SMiao Xie out_release: 25177775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 251876dda93cSYan, Zheng out_up_write: 25190b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 2520521e0546SDavid Sterba if (err) { 2521521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2522c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2523c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2524521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2525521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2526521e0546SDavid Sterba } 2527909e26dcSOmar Sandoval out_unlock_inode: 25285955102cSAl Viro inode_unlock(inode); 252976dda93cSYan, Zheng if (!err) { 253064ad6c48SOmar Sandoval d_invalidate(dentry); 253176dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 253276dda93cSYan, Zheng d_delete(dentry); 253361155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2534fa6ac876SLiu Bo 2535fa6ac876SLiu Bo /* the last ref */ 253657cdc8dbSDavid Sterba if (dest->ino_cache_inode) { 253757cdc8dbSDavid Sterba iput(dest->ino_cache_inode); 253857cdc8dbSDavid Sterba dest->ino_cache_inode = NULL; 2539fa6ac876SLiu Bo } 254076dda93cSYan, Zheng } 254176dda93cSYan, Zheng out_dput: 254276dda93cSYan, Zheng dput(dentry); 254376dda93cSYan, Zheng out_unlock_dir: 25445955102cSAl Viro inode_unlock(dir); 254500235411SAl Viro out_drop_write: 25462a79f17eSAl Viro mnt_drop_write_file(file); 254776dda93cSYan, Zheng out: 254876dda93cSYan, Zheng kfree(vol_args); 254976dda93cSYan, Zheng return err; 255076dda93cSYan, Zheng } 255176dda93cSYan, Zheng 25521e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2553f46b5a66SChristoph Hellwig { 2554496ad9aaSAl Viro struct inode *inode = file_inode(file); 2555f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 25561e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2557c146afadSYan Zheng int ret; 2558c146afadSYan Zheng 255925122d15SIlya Dryomov ret = mnt_want_write_file(file); 256025122d15SIlya Dryomov if (ret) 256125122d15SIlya Dryomov return ret; 2562b83cc969SLi Zefan 256325122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 256425122d15SIlya Dryomov ret = -EROFS; 256525122d15SIlya Dryomov goto out; 25665ac00addSStefan Behrens } 2567f46b5a66SChristoph Hellwig 2568f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2569f46b5a66SChristoph Hellwig case S_IFDIR: 2570e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2571e441d54dSChris Mason ret = -EPERM; 2572e441d54dSChris Mason goto out; 2573e441d54dSChris Mason } 2574de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 2575f46b5a66SChristoph Hellwig break; 2576f46b5a66SChristoph Hellwig case S_IFREG: 2577e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2578e441d54dSChris Mason ret = -EINVAL; 2579e441d54dSChris Mason goto out; 2580e441d54dSChris Mason } 25811e701a32SChris Mason 25821e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 25831e701a32SChris Mason if (!range) { 25841e701a32SChris Mason ret = -ENOMEM; 25851e701a32SChris Mason goto out; 25861e701a32SChris Mason } 25871e701a32SChris Mason 25881e701a32SChris Mason if (argp) { 25891e701a32SChris Mason if (copy_from_user(range, argp, 25901e701a32SChris Mason sizeof(*range))) { 25911e701a32SChris Mason ret = -EFAULT; 25921e701a32SChris Mason kfree(range); 2593683be16eSDan Carpenter goto out; 25941e701a32SChris Mason } 25951e701a32SChris Mason /* compression requires us to start the IO */ 25961e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 25971e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 25981e701a32SChris Mason range->extent_thresh = (u32)-1; 25991e701a32SChris Mason } 26001e701a32SChris Mason } else { 26011e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 26021e701a32SChris Mason range->len = (u64)-1; 26031e701a32SChris Mason } 2604496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 26054cb5300bSChris Mason range, 0, 0); 26064cb5300bSChris Mason if (ret > 0) 26074cb5300bSChris Mason ret = 0; 26081e701a32SChris Mason kfree(range); 2609f46b5a66SChristoph Hellwig break; 26108929ecfaSYan, Zheng default: 26118929ecfaSYan, Zheng ret = -EINVAL; 2612f46b5a66SChristoph Hellwig } 2613e441d54dSChris Mason out: 261425122d15SIlya Dryomov mnt_drop_write_file(file); 2615e441d54dSChris Mason return ret; 2616f46b5a66SChristoph Hellwig } 2617f46b5a66SChristoph Hellwig 26182ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 2619f46b5a66SChristoph Hellwig { 2620f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2621f46b5a66SChristoph Hellwig int ret; 2622f46b5a66SChristoph Hellwig 2623e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2624e441d54dSChris Mason return -EPERM; 2625e441d54dSChris Mason 2626171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) 2627e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2628c9e9f97bSIlya Dryomov 26290b246afaSJeff Mahoney mutex_lock(&fs_info->volume_mutex); 2630dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2631c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2632c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2633c9e9f97bSIlya Dryomov goto out; 2634c9e9f97bSIlya Dryomov } 2635f46b5a66SChristoph Hellwig 26365516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 26372ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 2638f46b5a66SChristoph Hellwig 263943d20761SAnand Jain if (!ret) 26400b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 264143d20761SAnand Jain 2642f46b5a66SChristoph Hellwig kfree(vol_args); 2643c9e9f97bSIlya Dryomov out: 26440b246afaSJeff Mahoney mutex_unlock(&fs_info->volume_mutex); 2645171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 2646f46b5a66SChristoph Hellwig return ret; 2647f46b5a66SChristoph Hellwig } 2648f46b5a66SChristoph Hellwig 26496b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 2650f46b5a66SChristoph Hellwig { 26510b246afaSJeff Mahoney struct inode *inode = file_inode(file); 26520b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 26536b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 2654f46b5a66SChristoph Hellwig int ret; 2655f46b5a66SChristoph Hellwig 2656e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2657e441d54dSChris Mason return -EPERM; 2658e441d54dSChris Mason 2659da24927bSMiao Xie ret = mnt_want_write_file(file); 2660da24927bSMiao Xie if (ret) 2661da24927bSMiao Xie return ret; 2662c146afadSYan Zheng 2663dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2664c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2665c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2666c47ca32dSDan Carpenter goto err_drop; 2667c9e9f97bSIlya Dryomov } 2668f46b5a66SChristoph Hellwig 26696b526ed7SAnand Jain /* Check for compatibility reject unknown flags */ 2670735654eaSDavid Sterba if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) 2671735654eaSDavid Sterba return -EOPNOTSUPP; 2672f46b5a66SChristoph Hellwig 2673171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 2674183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2675183860f6SAnand Jain goto out; 2676183860f6SAnand Jain } 2677183860f6SAnand Jain 2678735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 26792ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); 26806b526ed7SAnand Jain } else { 26816b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 26822ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 26836b526ed7SAnand Jain } 2684171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 2685183860f6SAnand Jain 26866b526ed7SAnand Jain if (!ret) { 2687735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 26880b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 26896b526ed7SAnand Jain vol_args->devid); 26906b526ed7SAnand Jain else 26910b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 26926b526ed7SAnand Jain vol_args->name); 26936b526ed7SAnand Jain } 2694183860f6SAnand Jain out: 2695183860f6SAnand Jain kfree(vol_args); 2696c47ca32dSDan Carpenter err_drop: 26974ac20c70SIlya Dryomov mnt_drop_write_file(file); 2698f46b5a66SChristoph Hellwig return ret; 2699f46b5a66SChristoph Hellwig } 2700f46b5a66SChristoph Hellwig 2701f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2702f46b5a66SChristoph Hellwig { 27030b246afaSJeff Mahoney struct inode *inode = file_inode(file); 27040b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2705f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2706f46b5a66SChristoph Hellwig int ret; 2707f46b5a66SChristoph Hellwig 2708f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2709f46b5a66SChristoph Hellwig return -EPERM; 2710f46b5a66SChristoph Hellwig 2711f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 2712f46b5a66SChristoph Hellwig if (ret) 2713f46b5a66SChristoph Hellwig return ret; 2714f46b5a66SChristoph Hellwig 2715171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 2716f46b5a66SChristoph Hellwig ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 271758d7bbf8SDavid Sterba goto out_drop_write; 271858d7bbf8SDavid Sterba } 271958d7bbf8SDavid Sterba 272058d7bbf8SDavid Sterba vol_args = memdup_user(arg, sizeof(*vol_args)); 272158d7bbf8SDavid Sterba if (IS_ERR(vol_args)) { 272258d7bbf8SDavid Sterba ret = PTR_ERR(vol_args); 2723f46b5a66SChristoph Hellwig goto out; 2724f46b5a66SChristoph Hellwig } 2725f46b5a66SChristoph Hellwig 272658d7bbf8SDavid Sterba vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 27272ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 2728f46b5a66SChristoph Hellwig 2729f46b5a66SChristoph Hellwig if (!ret) 27300b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 2731f46b5a66SChristoph Hellwig kfree(vol_args); 273258d7bbf8SDavid Sterba out: 2733171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 273458d7bbf8SDavid Sterba out_drop_write: 2735f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 273658d7bbf8SDavid Sterba 2737f46b5a66SChristoph Hellwig return ret; 2738f46b5a66SChristoph Hellwig } 2739f46b5a66SChristoph Hellwig 27402ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 27412ff7e61eSJeff Mahoney void __user *arg) 2742475f6387SJan Schmidt { 2743027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2744475f6387SJan Schmidt struct btrfs_device *device; 27450b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2746027ed2f0SLi Zefan int ret = 0; 2747475f6387SJan Schmidt 2748027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2749027ed2f0SLi Zefan if (!fi_args) 2750027ed2f0SLi Zefan return -ENOMEM; 2751027ed2f0SLi Zefan 2752d03262c7SDavid Sterba rcu_read_lock(); 2753027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2754475f6387SJan Schmidt 2755d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2756027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2757027ed2f0SLi Zefan fi_args->max_id = device->devid; 2758475f6387SJan Schmidt } 2759d03262c7SDavid Sterba rcu_read_unlock(); 2760475f6387SJan Schmidt 2761d03262c7SDavid Sterba memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid)); 2762bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 2763bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 2764bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 276580a773fbSDavid Sterba 2766027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2767027ed2f0SLi Zefan ret = -EFAULT; 2768475f6387SJan Schmidt 2769027ed2f0SLi Zefan kfree(fi_args); 2770027ed2f0SLi Zefan return ret; 2771475f6387SJan Schmidt } 2772475f6387SJan Schmidt 27732ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 27742ff7e61eSJeff Mahoney void __user *arg) 2775475f6387SJan Schmidt { 2776475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2777475f6387SJan Schmidt struct btrfs_device *dev; 2778475f6387SJan Schmidt int ret = 0; 2779475f6387SJan Schmidt char *s_uuid = NULL; 2780475f6387SJan Schmidt 2781475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2782475f6387SJan Schmidt if (IS_ERR(di_args)) 2783475f6387SJan Schmidt return PTR_ERR(di_args); 2784475f6387SJan Schmidt 2785dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2786475f6387SJan Schmidt s_uuid = di_args->uuid; 2787475f6387SJan Schmidt 2788c5593ca3SDavid Sterba rcu_read_lock(); 27890b246afaSJeff Mahoney dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL); 2790475f6387SJan Schmidt 2791475f6387SJan Schmidt if (!dev) { 2792475f6387SJan Schmidt ret = -ENODEV; 2793475f6387SJan Schmidt goto out; 2794475f6387SJan Schmidt } 2795475f6387SJan Schmidt 2796475f6387SJan Schmidt di_args->devid = dev->devid; 27977cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 27987cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 2799475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2800a27202fbSJim Meyering if (dev->name) { 2801606686eeSJosef Bacik struct rcu_string *name; 2802606686eeSJosef Bacik 2803606686eeSJosef Bacik name = rcu_dereference(dev->name); 2804606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2805a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2806a27202fbSJim Meyering } else { 280799ba55adSStefan Behrens di_args->path[0] = '\0'; 2808a27202fbSJim Meyering } 2809475f6387SJan Schmidt 2810475f6387SJan Schmidt out: 2811c5593ca3SDavid Sterba rcu_read_unlock(); 2812475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2813475f6387SJan Schmidt ret = -EFAULT; 2814475f6387SJan Schmidt 2815475f6387SJan Schmidt kfree(di_args); 2816475f6387SJan Schmidt return ret; 2817475f6387SJan Schmidt } 2818475f6387SJan Schmidt 2819f4414602SMark Fasheh static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) 2820416161dbSMark Fasheh { 2821416161dbSMark Fasheh struct page *page; 2822416161dbSMark Fasheh 2823416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2824416161dbSMark Fasheh if (!page) 282531314002SFilipe Manana return ERR_PTR(-ENOMEM); 2826416161dbSMark Fasheh 2827416161dbSMark Fasheh if (!PageUptodate(page)) { 282831314002SFilipe Manana int ret; 282931314002SFilipe Manana 283031314002SFilipe Manana ret = btrfs_readpage(NULL, page); 283131314002SFilipe Manana if (ret) 283231314002SFilipe Manana return ERR_PTR(ret); 2833416161dbSMark Fasheh lock_page(page); 2834416161dbSMark Fasheh if (!PageUptodate(page)) { 2835416161dbSMark Fasheh unlock_page(page); 283609cbfeafSKirill A. Shutemov put_page(page); 283731314002SFilipe Manana return ERR_PTR(-EIO); 2838416161dbSMark Fasheh } 283931314002SFilipe Manana if (page->mapping != inode->i_mapping) { 2840416161dbSMark Fasheh unlock_page(page); 284109cbfeafSKirill A. Shutemov put_page(page); 284231314002SFilipe Manana return ERR_PTR(-EAGAIN); 2843416161dbSMark Fasheh } 2844416161dbSMark Fasheh } 2845416161dbSMark Fasheh 2846416161dbSMark Fasheh return page; 2847416161dbSMark Fasheh } 2848416161dbSMark Fasheh 2849f4414602SMark Fasheh static int gather_extent_pages(struct inode *inode, struct page **pages, 2850f4414602SMark Fasheh int num_pages, u64 off) 2851f4414602SMark Fasheh { 2852f4414602SMark Fasheh int i; 285309cbfeafSKirill A. Shutemov pgoff_t index = off >> PAGE_SHIFT; 2854f4414602SMark Fasheh 2855f4414602SMark Fasheh for (i = 0; i < num_pages; i++) { 285631314002SFilipe Manana again: 2857f4414602SMark Fasheh pages[i] = extent_same_get_page(inode, index + i); 285831314002SFilipe Manana if (IS_ERR(pages[i])) { 285931314002SFilipe Manana int err = PTR_ERR(pages[i]); 286031314002SFilipe Manana 286131314002SFilipe Manana if (err == -EAGAIN) 286231314002SFilipe Manana goto again; 286331314002SFilipe Manana pages[i] = NULL; 286431314002SFilipe Manana return err; 286531314002SFilipe Manana } 2866f4414602SMark Fasheh } 2867f4414602SMark Fasheh return 0; 2868f4414602SMark Fasheh } 2869f4414602SMark Fasheh 2870e0bd70c6SFilipe Manana static int lock_extent_range(struct inode *inode, u64 off, u64 len, 2871e0bd70c6SFilipe Manana bool retry_range_locking) 287277fe20dcSMark Fasheh { 2873e0bd70c6SFilipe Manana /* 2874e0bd70c6SFilipe Manana * Do any pending delalloc/csum calculations on inode, one way or 2875e0bd70c6SFilipe Manana * another, and lock file content. 2876e0bd70c6SFilipe Manana * The locking order is: 2877e0bd70c6SFilipe Manana * 2878e0bd70c6SFilipe Manana * 1) pages 2879e0bd70c6SFilipe Manana * 2) range in the inode's io tree 2880e0bd70c6SFilipe Manana */ 288177fe20dcSMark Fasheh while (1) { 288277fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 288377fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 288477fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 288577fe20dcSMark Fasheh off + len - 1); 2886ff5df9b8SFilipe Manana if ((!ordered || 2887ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2888ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 288977fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2890ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2891ff5df9b8SFilipe Manana if (ordered) 2892ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 289377fe20dcSMark Fasheh break; 2894ff5df9b8SFilipe Manana } 289577fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 289677fe20dcSMark Fasheh if (ordered) 289777fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 2898e0bd70c6SFilipe Manana if (!retry_range_locking) 2899e0bd70c6SFilipe Manana return -EAGAIN; 290077fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 290177fe20dcSMark Fasheh } 2902e0bd70c6SFilipe Manana return 0; 290377fe20dcSMark Fasheh } 290477fe20dcSMark Fasheh 2905f4414602SMark Fasheh static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) 2906416161dbSMark Fasheh { 29075955102cSAl Viro inode_unlock(inode1); 29085955102cSAl Viro inode_unlock(inode2); 2909416161dbSMark Fasheh } 2910416161dbSMark Fasheh 2911f4414602SMark Fasheh static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) 2912f4414602SMark Fasheh { 2913f4414602SMark Fasheh if (inode1 < inode2) 2914f4414602SMark Fasheh swap(inode1, inode2); 2915f4414602SMark Fasheh 29165955102cSAl Viro inode_lock_nested(inode1, I_MUTEX_PARENT); 29175955102cSAl Viro inode_lock_nested(inode2, I_MUTEX_CHILD); 2918f4414602SMark Fasheh } 2919f4414602SMark Fasheh 2920f4414602SMark Fasheh static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 2921f4414602SMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2922f4414602SMark Fasheh { 2923f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2924f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2925f4414602SMark Fasheh } 2926f4414602SMark Fasheh 2927e0bd70c6SFilipe Manana static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 2928e0bd70c6SFilipe Manana struct inode *inode2, u64 loff2, u64 len, 2929e0bd70c6SFilipe Manana bool retry_range_locking) 2930416161dbSMark Fasheh { 2931e0bd70c6SFilipe Manana int ret; 2932e0bd70c6SFilipe Manana 2933416161dbSMark Fasheh if (inode1 < inode2) { 2934416161dbSMark Fasheh swap(inode1, inode2); 2935416161dbSMark Fasheh swap(loff1, loff2); 2936416161dbSMark Fasheh } 2937e0bd70c6SFilipe Manana ret = lock_extent_range(inode1, loff1, len, retry_range_locking); 2938e0bd70c6SFilipe Manana if (ret) 2939e0bd70c6SFilipe Manana return ret; 2940e0bd70c6SFilipe Manana ret = lock_extent_range(inode2, loff2, len, retry_range_locking); 2941e0bd70c6SFilipe Manana if (ret) 2942e0bd70c6SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, 2943e0bd70c6SFilipe Manana loff1 + len - 1); 2944e0bd70c6SFilipe Manana return ret; 2945416161dbSMark Fasheh } 2946f4414602SMark Fasheh 2947f4414602SMark Fasheh struct cmp_pages { 2948f4414602SMark Fasheh int num_pages; 2949f4414602SMark Fasheh struct page **src_pages; 2950f4414602SMark Fasheh struct page **dst_pages; 2951f4414602SMark Fasheh }; 2952f4414602SMark Fasheh 2953f4414602SMark Fasheh static void btrfs_cmp_data_free(struct cmp_pages *cmp) 2954f4414602SMark Fasheh { 2955f4414602SMark Fasheh int i; 2956f4414602SMark Fasheh struct page *pg; 2957f4414602SMark Fasheh 2958f4414602SMark Fasheh for (i = 0; i < cmp->num_pages; i++) { 2959f4414602SMark Fasheh pg = cmp->src_pages[i]; 2960e0bd70c6SFilipe Manana if (pg) { 2961e0bd70c6SFilipe Manana unlock_page(pg); 296209cbfeafSKirill A. Shutemov put_page(pg); 2963e0bd70c6SFilipe Manana } 2964f4414602SMark Fasheh pg = cmp->dst_pages[i]; 2965e0bd70c6SFilipe Manana if (pg) { 2966e0bd70c6SFilipe Manana unlock_page(pg); 296709cbfeafSKirill A. Shutemov put_page(pg); 2968f4414602SMark Fasheh } 2969e0bd70c6SFilipe Manana } 2970f4414602SMark Fasheh kfree(cmp->src_pages); 2971f4414602SMark Fasheh kfree(cmp->dst_pages); 2972f4414602SMark Fasheh } 2973f4414602SMark Fasheh 2974f4414602SMark Fasheh static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, 2975f4414602SMark Fasheh struct inode *dst, u64 dst_loff, 2976f4414602SMark Fasheh u64 len, struct cmp_pages *cmp) 2977f4414602SMark Fasheh { 2978f4414602SMark Fasheh int ret; 297909cbfeafSKirill A. Shutemov int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; 2980f4414602SMark Fasheh struct page **src_pgarr, **dst_pgarr; 2981f4414602SMark Fasheh 2982f4414602SMark Fasheh /* 2983f4414602SMark Fasheh * We must gather up all the pages before we initiate our 2984f4414602SMark Fasheh * extent locking. We use an array for the page pointers. Size 2985f4414602SMark Fasheh * of the array is bounded by len, which is in turn bounded by 2986f4414602SMark Fasheh * BTRFS_MAX_DEDUPE_LEN. 2987f4414602SMark Fasheh */ 298866722f7cSDavid Sterba src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 298966722f7cSDavid Sterba dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 2990f4414602SMark Fasheh if (!src_pgarr || !dst_pgarr) { 2991f4414602SMark Fasheh kfree(src_pgarr); 2992f4414602SMark Fasheh kfree(dst_pgarr); 2993f4414602SMark Fasheh return -ENOMEM; 2994f4414602SMark Fasheh } 2995f4414602SMark Fasheh cmp->num_pages = num_pages; 2996f4414602SMark Fasheh cmp->src_pages = src_pgarr; 2997f4414602SMark Fasheh cmp->dst_pages = dst_pgarr; 2998f4414602SMark Fasheh 2999b1517622SFilipe Manana /* 3000b1517622SFilipe Manana * If deduping ranges in the same inode, locking rules make it mandatory 3001b1517622SFilipe Manana * to always lock pages in ascending order to avoid deadlocks with 3002b1517622SFilipe Manana * concurrent tasks (such as starting writeback/delalloc). 3003b1517622SFilipe Manana */ 3004b1517622SFilipe Manana if (src == dst && dst_loff < loff) { 3005b1517622SFilipe Manana swap(src_pgarr, dst_pgarr); 3006b1517622SFilipe Manana swap(loff, dst_loff); 3007b1517622SFilipe Manana } 3008b1517622SFilipe Manana 3009b1517622SFilipe Manana ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff); 3010f4414602SMark Fasheh if (ret) 3011f4414602SMark Fasheh goto out; 3012f4414602SMark Fasheh 3013b1517622SFilipe Manana ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff); 3014f4414602SMark Fasheh 3015f4414602SMark Fasheh out: 3016f4414602SMark Fasheh if (ret) 3017f4414602SMark Fasheh btrfs_cmp_data_free(cmp); 301878ad4ce0SNaohiro Aota return ret; 3019416161dbSMark Fasheh } 3020416161dbSMark Fasheh 30211a287cfeSDavid Sterba static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) 3022416161dbSMark Fasheh { 3023416161dbSMark Fasheh int ret = 0; 3024f4414602SMark Fasheh int i; 3025416161dbSMark Fasheh struct page *src_page, *dst_page; 302609cbfeafSKirill A. Shutemov unsigned int cmp_len = PAGE_SIZE; 3027416161dbSMark Fasheh void *addr, *dst_addr; 3028416161dbSMark Fasheh 3029f4414602SMark Fasheh i = 0; 3030416161dbSMark Fasheh while (len) { 303109cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 3032416161dbSMark Fasheh cmp_len = len; 3033416161dbSMark Fasheh 3034f4414602SMark Fasheh BUG_ON(i >= cmp->num_pages); 3035f4414602SMark Fasheh 3036f4414602SMark Fasheh src_page = cmp->src_pages[i]; 3037f4414602SMark Fasheh dst_page = cmp->dst_pages[i]; 3038e0bd70c6SFilipe Manana ASSERT(PageLocked(src_page)); 3039e0bd70c6SFilipe Manana ASSERT(PageLocked(dst_page)); 3040f4414602SMark Fasheh 3041416161dbSMark Fasheh addr = kmap_atomic(src_page); 3042416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 3043416161dbSMark Fasheh 3044416161dbSMark Fasheh flush_dcache_page(src_page); 3045416161dbSMark Fasheh flush_dcache_page(dst_page); 3046416161dbSMark Fasheh 3047416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 30482b3909f8SDarrick J. Wong ret = -EBADE; 3049416161dbSMark Fasheh 3050416161dbSMark Fasheh kunmap_atomic(addr); 3051416161dbSMark Fasheh kunmap_atomic(dst_addr); 3052416161dbSMark Fasheh 3053416161dbSMark Fasheh if (ret) 3054416161dbSMark Fasheh break; 3055416161dbSMark Fasheh 3056416161dbSMark Fasheh len -= cmp_len; 3057f4414602SMark Fasheh i++; 3058416161dbSMark Fasheh } 3059416161dbSMark Fasheh 3060416161dbSMark Fasheh return ret; 3061416161dbSMark Fasheh } 3062416161dbSMark Fasheh 3063e1d227a4SMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, 3064e1d227a4SMark Fasheh u64 olen) 3065416161dbSMark Fasheh { 3066e1d227a4SMark Fasheh u64 len = *plen; 3067416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 3068416161dbSMark Fasheh 3069e1d227a4SMark Fasheh if (off + olen > inode->i_size || off + olen < off) 3070416161dbSMark Fasheh return -EINVAL; 3071e1d227a4SMark Fasheh 3072e1d227a4SMark Fasheh /* if we extend to eof, continue to block boundary */ 3073e1d227a4SMark Fasheh if (off + len == inode->i_size) 3074e1d227a4SMark Fasheh *plen = len = ALIGN(inode->i_size, bs) - off; 3075e1d227a4SMark Fasheh 3076416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 3077416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 3078416161dbSMark Fasheh return -EINVAL; 3079416161dbSMark Fasheh 3080416161dbSMark Fasheh return 0; 3081416161dbSMark Fasheh } 3082416161dbSMark Fasheh 3083e1d227a4SMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 3084416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 3085416161dbSMark Fasheh { 3086416161dbSMark Fasheh int ret; 3087e1d227a4SMark Fasheh u64 len = olen; 3088f4414602SMark Fasheh struct cmp_pages cmp; 3089fc4badd9SOmar Sandoval bool same_inode = (src == dst); 30900efa9f48SMark Fasheh u64 same_lock_start = 0; 30910efa9f48SMark Fasheh u64 same_lock_len = 0; 3092416161dbSMark Fasheh 3093113e8283SFilipe Manana if (len == 0) 3094113e8283SFilipe Manana return 0; 3095113e8283SFilipe Manana 3096fc4badd9SOmar Sandoval if (same_inode) 30975955102cSAl Viro inode_lock(src); 3098fc4badd9SOmar Sandoval else 3099fc4badd9SOmar Sandoval btrfs_double_inode_lock(src, dst); 31000efa9f48SMark Fasheh 31010efa9f48SMark Fasheh ret = extent_same_check_offsets(src, loff, &len, olen); 31020efa9f48SMark Fasheh if (ret) 31030efa9f48SMark Fasheh goto out_unlock; 3104fc4badd9SOmar Sandoval 3105fc4badd9SOmar Sandoval ret = extent_same_check_offsets(dst, dst_loff, &len, olen); 3106f4dfe687SFilipe Manana if (ret) 3107f4dfe687SFilipe Manana goto out_unlock; 31080efa9f48SMark Fasheh 3109fc4badd9SOmar Sandoval if (same_inode) { 31100efa9f48SMark Fasheh /* 31110efa9f48SMark Fasheh * Single inode case wants the same checks, except we 31120efa9f48SMark Fasheh * don't want our length pushed out past i_size as 31130efa9f48SMark Fasheh * comparing that data range makes no sense. 31140efa9f48SMark Fasheh * 31150efa9f48SMark Fasheh * extent_same_check_offsets() will do this for an 31160efa9f48SMark Fasheh * unaligned length at i_size, so catch it here and 31170efa9f48SMark Fasheh * reject the request. 31180efa9f48SMark Fasheh * 31190efa9f48SMark Fasheh * This effectively means we require aligned extents 31200efa9f48SMark Fasheh * for the single-inode case, whereas the other cases 31210efa9f48SMark Fasheh * allow an unaligned length so long as it ends at 31220efa9f48SMark Fasheh * i_size. 31230efa9f48SMark Fasheh */ 31240efa9f48SMark Fasheh if (len != olen) { 31250efa9f48SMark Fasheh ret = -EINVAL; 31260efa9f48SMark Fasheh goto out_unlock; 31270efa9f48SMark Fasheh } 31280efa9f48SMark Fasheh 31290efa9f48SMark Fasheh /* Check for overlapping ranges */ 31300efa9f48SMark Fasheh if (dst_loff + len > loff && dst_loff < loff + len) { 31310efa9f48SMark Fasheh ret = -EINVAL; 31320efa9f48SMark Fasheh goto out_unlock; 31330efa9f48SMark Fasheh } 31340efa9f48SMark Fasheh 31350efa9f48SMark Fasheh same_lock_start = min_t(u64, loff, dst_loff); 31360efa9f48SMark Fasheh same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; 31370efa9f48SMark Fasheh } 3138416161dbSMark Fasheh 3139416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 3140416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 3141416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 3142416161dbSMark Fasheh ret = -EINVAL; 3143416161dbSMark Fasheh goto out_unlock; 3144416161dbSMark Fasheh } 3145416161dbSMark Fasheh 3146e0bd70c6SFilipe Manana again: 3147f4414602SMark Fasheh ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); 3148f4414602SMark Fasheh if (ret) 3149f4414602SMark Fasheh goto out_unlock; 3150f4414602SMark Fasheh 31510efa9f48SMark Fasheh if (same_inode) 3152e0bd70c6SFilipe Manana ret = lock_extent_range(src, same_lock_start, same_lock_len, 3153e0bd70c6SFilipe Manana false); 31540efa9f48SMark Fasheh else 3155e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, 3156e0bd70c6SFilipe Manana false); 3157e0bd70c6SFilipe Manana /* 3158e0bd70c6SFilipe Manana * If one of the inodes has dirty pages in the respective range or 3159e0bd70c6SFilipe Manana * ordered extents, we need to flush dellaloc and wait for all ordered 3160e0bd70c6SFilipe Manana * extents in the range. We must unlock the pages and the ranges in the 3161e0bd70c6SFilipe Manana * io trees to avoid deadlocks when flushing delalloc (requires locking 3162e0bd70c6SFilipe Manana * pages) and when waiting for ordered extents to complete (they require 3163e0bd70c6SFilipe Manana * range locking). 3164e0bd70c6SFilipe Manana */ 3165e0bd70c6SFilipe Manana if (ret == -EAGAIN) { 3166e0bd70c6SFilipe Manana /* 3167e0bd70c6SFilipe Manana * Ranges in the io trees already unlocked. Now unlock all 3168e0bd70c6SFilipe Manana * pages before waiting for all IO to complete. 3169e0bd70c6SFilipe Manana */ 3170e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3171e0bd70c6SFilipe Manana if (same_inode) { 3172e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, same_lock_start, 3173e0bd70c6SFilipe Manana same_lock_len); 3174e0bd70c6SFilipe Manana } else { 3175e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, loff, len); 3176e0bd70c6SFilipe Manana btrfs_wait_ordered_range(dst, dst_loff, len); 3177e0bd70c6SFilipe Manana } 3178e0bd70c6SFilipe Manana goto again; 3179e0bd70c6SFilipe Manana } 3180e0bd70c6SFilipe Manana ASSERT(ret == 0); 3181e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3182e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3183e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3184e0bd70c6SFilipe Manana return ret; 3185e0bd70c6SFilipe Manana } 3186f4414602SMark Fasheh 3187207910ddSMark Fasheh /* pass original length for comparison so we stay within i_size */ 31881a287cfeSDavid Sterba ret = btrfs_cmp_data(olen, &cmp); 3189416161dbSMark Fasheh if (ret == 0) 31901c919a5eSMark Fasheh ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); 3191416161dbSMark Fasheh 31920efa9f48SMark Fasheh if (same_inode) 31930efa9f48SMark Fasheh unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, 31940efa9f48SMark Fasheh same_lock_start + same_lock_len - 1); 31950efa9f48SMark Fasheh else 3196f4414602SMark Fasheh btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 3197f4414602SMark Fasheh 3198f4414602SMark Fasheh btrfs_cmp_data_free(&cmp); 3199416161dbSMark Fasheh out_unlock: 32000efa9f48SMark Fasheh if (same_inode) 32015955102cSAl Viro inode_unlock(src); 32020efa9f48SMark Fasheh else 3203f4414602SMark Fasheh btrfs_double_inode_unlock(src, dst); 3204416161dbSMark Fasheh 3205416161dbSMark Fasheh return ret; 3206416161dbSMark Fasheh } 3207416161dbSMark Fasheh 3208ee22184bSByongho Lee #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3209416161dbSMark Fasheh 32102b3909f8SDarrick J. Wong ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen, 32112b3909f8SDarrick J. Wong struct file *dst_file, u64 dst_loff) 3212416161dbSMark Fasheh { 32132b3909f8SDarrick J. Wong struct inode *src = file_inode(src_file); 32142b3909f8SDarrick J. Wong struct inode *dst = file_inode(dst_file); 3215416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 32162b3909f8SDarrick J. Wong ssize_t res; 3217416161dbSMark Fasheh 32182b3909f8SDarrick J. Wong if (olen > BTRFS_MAX_DEDUPE_LEN) 32192b3909f8SDarrick J. Wong olen = BTRFS_MAX_DEDUPE_LEN; 3220416161dbSMark Fasheh 322109cbfeafSKirill A. Shutemov if (WARN_ON_ONCE(bs < PAGE_SIZE)) { 3222416161dbSMark Fasheh /* 3223416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 3224416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 3225416161dbSMark Fasheh * this situation without an update. 3226416161dbSMark Fasheh */ 32272b3909f8SDarrick J. Wong return -EINVAL; 3228416161dbSMark Fasheh } 3229416161dbSMark Fasheh 32302b3909f8SDarrick J. Wong res = btrfs_extent_same(src, loff, olen, dst, dst_loff); 32312b3909f8SDarrick J. Wong if (res) 32322b3909f8SDarrick J. Wong return res; 32332b3909f8SDarrick J. Wong return olen; 3234416161dbSMark Fasheh } 3235416161dbSMark Fasheh 3236f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3237f82a9901SFilipe Manana struct inode *inode, 3238f82a9901SFilipe Manana u64 endoff, 3239f82a9901SFilipe Manana const u64 destoff, 32401c919a5eSMark Fasheh const u64 olen, 32411c919a5eSMark Fasheh int no_time_update) 3242f82a9901SFilipe Manana { 3243f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3244f82a9901SFilipe Manana int ret; 3245f82a9901SFilipe Manana 3246f82a9901SFilipe Manana inode_inc_iversion(inode); 32471c919a5eSMark Fasheh if (!no_time_update) 3248c2050a45SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 3249f82a9901SFilipe Manana /* 3250f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3251f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3252f82a9901SFilipe Manana */ 3253f82a9901SFilipe Manana if (endoff > destoff + olen) 3254f82a9901SFilipe Manana endoff = destoff + olen; 3255f82a9901SFilipe Manana if (endoff > inode->i_size) 32566ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(inode), endoff); 3257f82a9901SFilipe Manana 3258f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3259f82a9901SFilipe Manana if (ret) { 326066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 32613a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3262f82a9901SFilipe Manana goto out; 3263f82a9901SFilipe Manana } 32643a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 3265f82a9901SFilipe Manana out: 3266f82a9901SFilipe Manana return ret; 3267f82a9901SFilipe Manana } 3268f82a9901SFilipe Manana 3269a2f392e4SNikolay Borisov static void clone_update_extent_map(struct btrfs_inode *inode, 32707ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 32717ffbb598SFilipe Manana const struct btrfs_path *path, 32727ffbb598SFilipe Manana const u64 hole_offset, 32737ffbb598SFilipe Manana const u64 hole_len) 32747ffbb598SFilipe Manana { 3275a2f392e4SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 32767ffbb598SFilipe Manana struct extent_map *em; 32777ffbb598SFilipe Manana int ret; 32787ffbb598SFilipe Manana 32797ffbb598SFilipe Manana em = alloc_extent_map(); 32807ffbb598SFilipe Manana if (!em) { 3281a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 32827ffbb598SFilipe Manana return; 32837ffbb598SFilipe Manana } 32847ffbb598SFilipe Manana 328514f59796SFilipe Manana if (path) { 328614f59796SFilipe Manana struct btrfs_file_extent_item *fi; 328714f59796SFilipe Manana 328814f59796SFilipe Manana fi = btrfs_item_ptr(path->nodes[0], path->slots[0], 328914f59796SFilipe Manana struct btrfs_file_extent_item); 3290a2f392e4SNikolay Borisov btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 32917ffbb598SFilipe Manana em->generation = -1; 32927ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 32937ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 32947ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 3295a2f392e4SNikolay Borisov &inode->runtime_flags); 32967ffbb598SFilipe Manana } else { 32977ffbb598SFilipe Manana em->start = hole_offset; 32987ffbb598SFilipe Manana em->len = hole_len; 32997ffbb598SFilipe Manana em->ram_bytes = em->len; 33007ffbb598SFilipe Manana em->orig_start = hole_offset; 33017ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 33027ffbb598SFilipe Manana em->block_len = 0; 33037ffbb598SFilipe Manana em->orig_block_len = 0; 33047ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 33057ffbb598SFilipe Manana em->generation = trans->transid; 33067ffbb598SFilipe Manana } 33077ffbb598SFilipe Manana 33087ffbb598SFilipe Manana while (1) { 33097ffbb598SFilipe Manana write_lock(&em_tree->lock); 33107ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 33117ffbb598SFilipe Manana write_unlock(&em_tree->lock); 33127ffbb598SFilipe Manana if (ret != -EEXIST) { 33137ffbb598SFilipe Manana free_extent_map(em); 33147ffbb598SFilipe Manana break; 33157ffbb598SFilipe Manana } 3316a2f392e4SNikolay Borisov btrfs_drop_extent_cache(inode, em->start, 33177ffbb598SFilipe Manana em->start + em->len - 1, 0); 33187ffbb598SFilipe Manana } 33197ffbb598SFilipe Manana 3320ee39b432SDavid Sterba if (ret) 3321a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 33227ffbb598SFilipe Manana } 33237ffbb598SFilipe Manana 33248039d87dSFilipe Manana /* 33258039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 33268039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 33278039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 33288039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 33298039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 33308039d87dSFilipe Manana * 33318039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 33328039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 33338039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 33348039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 33358039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 33368039d87dSFilipe Manana * i_mutex, which we are holding here). 33378039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 33388039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 33398039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 33408039d87dSFilipe Manana * 33418039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 33428039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 33438039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 33448039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 33458039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 33468039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 33478039d87dSFilipe Manana * the former. 33488039d87dSFilipe Manana */ 33494a0ab9d7SDavid Sterba static int clone_copy_inline_extent(struct inode *dst, 33508039d87dSFilipe Manana struct btrfs_trans_handle *trans, 33518039d87dSFilipe Manana struct btrfs_path *path, 33528039d87dSFilipe Manana struct btrfs_key *new_key, 33538039d87dSFilipe Manana const u64 drop_start, 33548039d87dSFilipe Manana const u64 datal, 33558039d87dSFilipe Manana const u64 skip, 33568039d87dSFilipe Manana const u64 size, 33578039d87dSFilipe Manana char *inline_data) 33588039d87dSFilipe Manana { 33590b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); 33608039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 33618039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 33620b246afaSJeff Mahoney fs_info->sectorsize); 33638039d87dSFilipe Manana int ret; 33648039d87dSFilipe Manana struct btrfs_key key; 33658039d87dSFilipe Manana 33668039d87dSFilipe Manana if (new_key->offset > 0) 33678039d87dSFilipe Manana return -EOPNOTSUPP; 33688039d87dSFilipe Manana 33694a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(dst)); 33708039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 33718039d87dSFilipe Manana key.offset = 0; 33728039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 33738039d87dSFilipe Manana if (ret < 0) { 33748039d87dSFilipe Manana return ret; 33758039d87dSFilipe Manana } else if (ret > 0) { 33768039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 33778039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 33788039d87dSFilipe Manana if (ret < 0) 33798039d87dSFilipe Manana return ret; 33808039d87dSFilipe Manana else if (ret > 0) 33818039d87dSFilipe Manana goto copy_inline_extent; 33828039d87dSFilipe Manana } 33838039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 33844a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 33858039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 33868039d87dSFilipe Manana ASSERT(key.offset > 0); 33878039d87dSFilipe Manana return -EOPNOTSUPP; 33888039d87dSFilipe Manana } 33898039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 33908039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 33918039d87dSFilipe Manana u64 ext_len; 33928039d87dSFilipe Manana 33938039d87dSFilipe Manana /* 33948039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 33958039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 33968039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 33978039d87dSFilipe Manana */ 33988039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 33998039d87dSFilipe Manana struct btrfs_file_extent_item); 34008039d87dSFilipe Manana /* 34018039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 34028039d87dSFilipe Manana * following it. 34038039d87dSFilipe Manana */ 34048039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 34058039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 34068039d87dSFilipe Manana goto copy_inline_extent; 34078039d87dSFilipe Manana 34088039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 34098039d87dSFilipe Manana if (ext_len > aligned_end) 34108039d87dSFilipe Manana return -EOPNOTSUPP; 34118039d87dSFilipe Manana 34128039d87dSFilipe Manana ret = btrfs_next_item(root, path); 34138039d87dSFilipe Manana if (ret < 0) { 34148039d87dSFilipe Manana return ret; 34158039d87dSFilipe Manana } else if (ret == 0) { 34168039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 34178039d87dSFilipe Manana path->slots[0]); 34184a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 34198039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 34208039d87dSFilipe Manana return -EOPNOTSUPP; 34218039d87dSFilipe Manana } 34228039d87dSFilipe Manana } 34238039d87dSFilipe Manana 34248039d87dSFilipe Manana copy_inline_extent: 34258039d87dSFilipe Manana /* 34268039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 34278039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 34288039d87dSFilipe Manana */ 34298039d87dSFilipe Manana if (i_size_read(dst) > datal) { 34308039d87dSFilipe Manana /* 34318039d87dSFilipe Manana * If the destination inode has an inline extent... 34328039d87dSFilipe Manana * This would require copying the data from the source inline 34338039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 34348039d87dSFilipe Manana * But this is really complex, both extents can be compressed 34358039d87dSFilipe Manana * or just one of them, which would require decompressing and 34368039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 34378039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 34388039d87dSFilipe Manana * inline extent). 34398039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 34408039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 34418039d87dSFilipe Manana */ 34428039d87dSFilipe Manana return -EOPNOTSUPP; 34438039d87dSFilipe Manana } 34448039d87dSFilipe Manana 34458039d87dSFilipe Manana btrfs_release_path(path); 34468039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 34478039d87dSFilipe Manana if (ret) 34488039d87dSFilipe Manana return ret; 34498039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 34508039d87dSFilipe Manana if (ret) 34518039d87dSFilipe Manana return ret; 34528039d87dSFilipe Manana 34538039d87dSFilipe Manana if (skip) { 34548039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 34558039d87dSFilipe Manana 34568039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 34578039d87dSFilipe Manana } 34588039d87dSFilipe Manana 34598039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 34608039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 34618039d87dSFilipe Manana path->slots[0]), 34628039d87dSFilipe Manana size); 34638039d87dSFilipe Manana inode_add_bytes(dst, datal); 34648039d87dSFilipe Manana 34658039d87dSFilipe Manana return 0; 34668039d87dSFilipe Manana } 34678039d87dSFilipe Manana 346832b7c687SMark Fasheh /** 346932b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 347032b7c687SMark Fasheh * 347132b7c687SMark Fasheh * @src: Inode to clone from 347232b7c687SMark Fasheh * @inode: Inode to clone to 347332b7c687SMark Fasheh * @off: Offset within source to start clone from 347432b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 34751c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 347632b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 34771c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 347832b7c687SMark Fasheh */ 347932b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3480f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 34811c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3482f46b5a66SChristoph Hellwig { 34830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3484f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 348532b7c687SMark Fasheh struct btrfs_path *path = NULL; 3486f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 348732b7c687SMark Fasheh struct btrfs_trans_handle *trans; 348832b7c687SMark Fasheh char *buf = NULL; 3489ae01a0abSYan Zheng struct btrfs_key key; 3490f46b5a66SChristoph Hellwig u32 nritems; 3491f46b5a66SChristoph Hellwig int slot; 3492ae01a0abSYan Zheng int ret; 3493f82a9901SFilipe Manana const u64 len = olen_aligned; 3494f82a9901SFilipe Manana u64 last_dest_end = destoff; 3495ae01a0abSYan Zheng 3496ae01a0abSYan Zheng ret = -ENOMEM; 3497752ade68SMichal Hocko buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 3498ae01a0abSYan Zheng if (!buf) 349932b7c687SMark Fasheh return ret; 3500ae01a0abSYan Zheng 3501ae01a0abSYan Zheng path = btrfs_alloc_path(); 3502ae01a0abSYan Zheng if (!path) { 350315351955SDavid Sterba kvfree(buf); 350432b7c687SMark Fasheh return ret; 3505ae01a0abSYan Zheng } 350632b7c687SMark Fasheh 3507e4058b54SDavid Sterba path->reada = READA_FORWARD; 3508c5c9cd4dSSage Weil /* clone data */ 35094a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(src)); 3510ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 35112c463823SFilipe Manana key.offset = off; 3512f46b5a66SChristoph Hellwig 3513f46b5a66SChristoph Hellwig while (1) { 3514de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3515df858e76SFilipe Manana 3516f46b5a66SChristoph Hellwig /* 3517f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3518f46b5a66SChristoph Hellwig * tree. 3519f46b5a66SChristoph Hellwig */ 3520e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3521362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3522362a20c5SDavid Sterba 0, 0); 3523f46b5a66SChristoph Hellwig if (ret < 0) 3524f46b5a66SChristoph Hellwig goto out; 35252c463823SFilipe Manana /* 35262c463823SFilipe Manana * First search, if no extent item that starts at offset off was 35272c463823SFilipe Manana * found but the previous item is an extent item, it's possible 35282c463823SFilipe Manana * it might overlap our target range, therefore process it. 35292c463823SFilipe Manana */ 35302c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 35312c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 35322c463823SFilipe Manana path->slots[0] - 1); 35332c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 35342c463823SFilipe Manana path->slots[0]--; 35352c463823SFilipe Manana } 3536f46b5a66SChristoph Hellwig 3537ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3538e4355f34SFilipe David Borba Manana process_slot: 3539ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3540362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3541f46b5a66SChristoph Hellwig if (ret < 0) 3542f46b5a66SChristoph Hellwig goto out; 3543f46b5a66SChristoph Hellwig if (ret > 0) 3544f46b5a66SChristoph Hellwig break; 3545ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3546f46b5a66SChristoph Hellwig } 3547f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3548f46b5a66SChristoph Hellwig slot = path->slots[0]; 3549f46b5a66SChristoph Hellwig 3550ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3551962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 35524a0cc7caSNikolay Borisov key.objectid != btrfs_ino(BTRFS_I(src))) 3553f46b5a66SChristoph Hellwig break; 3554f46b5a66SChristoph Hellwig 3555962a298fSDavid Sterba if (key.type == BTRFS_EXTENT_DATA_KEY) { 3556c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3557c5c9cd4dSSage Weil int type; 355831840ae1SZheng Yan u32 size; 355931840ae1SZheng Yan struct btrfs_key new_key; 3560c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3561c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3562c5c9cd4dSSage Weil u8 comp; 3563f82a9901SFilipe Manana u64 drop_start; 356431840ae1SZheng Yan 3565c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3566c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3567c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3568c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3569c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3570c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3571d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3572d397712bSChris Mason extent); 3573d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3574d397712bSChris Mason extent); 3575c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3576d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3577d397712bSChris Mason extent); 3578c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3579c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3580c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3581c5c9cd4dSSage Weil extent); 3582c5c9cd4dSSage Weil } 358331840ae1SZheng Yan 35842c463823SFilipe Manana /* 35852c463823SFilipe Manana * The first search might have left us at an extent 35862c463823SFilipe Manana * item that ends before our target range's start, can 35872c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 35882c463823SFilipe Manana */ 35892c463823SFilipe Manana if (key.offset + datal <= off) { 3590e4355f34SFilipe David Borba Manana path->slots[0]++; 3591e4355f34SFilipe David Borba Manana goto process_slot; 35922c463823SFilipe Manana } else if (key.offset >= off + len) { 35932c463823SFilipe Manana break; 3594e4355f34SFilipe David Borba Manana } 3595df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 3596e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3597e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3598e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3599e4355f34SFilipe David Borba Manana size); 3600e4355f34SFilipe David Borba Manana 3601e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3602e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3603c5c9cd4dSSage Weil 360431840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 36054a0cc7caSNikolay Borisov new_key.objectid = btrfs_ino(BTRFS_I(inode)); 36064d728ec7SLi Zefan if (off <= key.offset) 3607c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 36084d728ec7SLi Zefan else 36094d728ec7SLi Zefan new_key.offset = destoff; 3610c5c9cd4dSSage Weil 3611b6f3409bSSage Weil /* 3612f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3613f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3614f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3615f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3616f82a9901SFilipe Manana * partially overlaps it). 3617f82a9901SFilipe Manana */ 3618f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3619f82a9901SFilipe Manana drop_start = last_dest_end; 3620f82a9901SFilipe Manana else 3621f82a9901SFilipe Manana drop_start = new_key.offset; 3622f82a9901SFilipe Manana 3623f82a9901SFilipe Manana /* 3624b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3625b6f3409bSSage Weil * 1 - add new extent 3626b6f3409bSSage Weil * 1 - inode update 3627b6f3409bSSage Weil */ 3628b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3629a22285a6SYan, Zheng if (IS_ERR(trans)) { 3630a22285a6SYan, Zheng ret = PTR_ERR(trans); 3631a22285a6SYan, Zheng goto out; 3632a22285a6SYan, Zheng } 3633a22285a6SYan, Zheng 3634c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3635c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3636d72c0842SLi Zefan /* 3637d72c0842SLi Zefan * a | --- range to clone ---| b 3638d72c0842SLi Zefan * | ------------- extent ------------- | 3639d72c0842SLi Zefan */ 3640d72c0842SLi Zefan 364193915584SAntonio Ospite /* subtract range b */ 3642d72c0842SLi Zefan if (key.offset + datal > off + len) 3643d72c0842SLi Zefan datal = off + len - key.offset; 3644d72c0842SLi Zefan 364593915584SAntonio Ospite /* subtract range a */ 3646a22285a6SYan, Zheng if (off > key.offset) { 3647a22285a6SYan, Zheng datao += off - key.offset; 3648a22285a6SYan, Zheng datal -= off - key.offset; 3649a22285a6SYan, Zheng } 3650a22285a6SYan, Zheng 36515dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3652f82a9901SFilipe Manana drop_start, 3653a22285a6SYan, Zheng new_key.offset + datal, 36542671485dSJosef Bacik 1); 365579787eaaSJeff Mahoney if (ret) { 36563f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 365700fdf13aSLiu Bo btrfs_abort_transaction(trans, 365866642832SJeff Mahoney ret); 36593a45bb20SJeff Mahoney btrfs_end_transaction(trans); 366079787eaaSJeff Mahoney goto out; 366179787eaaSJeff Mahoney } 3662a22285a6SYan, Zheng 366331840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 366431840ae1SZheng Yan &new_key, size); 366579787eaaSJeff Mahoney if (ret) { 366666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 36673a45bb20SJeff Mahoney btrfs_end_transaction(trans); 366879787eaaSJeff Mahoney goto out; 366979787eaaSJeff Mahoney } 367031840ae1SZheng Yan 367131840ae1SZheng Yan leaf = path->nodes[0]; 367231840ae1SZheng Yan slot = path->slots[0]; 367331840ae1SZheng Yan write_extent_buffer(leaf, buf, 367431840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 367531840ae1SZheng Yan size); 3676ae01a0abSYan Zheng 3677f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3678f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3679c5c9cd4dSSage Weil 3680c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3681c5c9cd4dSSage Weil if (!disko) 3682c5c9cd4dSSage Weil datao = 0; 3683c5c9cd4dSSage Weil 3684c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3685c5c9cd4dSSage Weil datao); 3686c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3687c5c9cd4dSSage Weil datal); 3688fcebe456SJosef Bacik 3689c5c9cd4dSSage Weil if (disko) { 3690c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 36912ff7e61eSJeff Mahoney ret = btrfs_inc_extent_ref(trans, 369284f7d8e6SJosef Bacik root, 36935d4f98a2SYan Zheng disko, diskl, 0, 3694f46b5a66SChristoph Hellwig root->root_key.objectid, 36954a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), 3696b06c4bf5SFilipe Manana new_key.offset - datao); 369779787eaaSJeff Mahoney if (ret) { 369879787eaaSJeff Mahoney btrfs_abort_transaction(trans, 369979787eaaSJeff Mahoney ret); 37003a45bb20SJeff Mahoney btrfs_end_transaction(trans); 370179787eaaSJeff Mahoney goto out; 370279787eaaSJeff Mahoney 370379787eaaSJeff Mahoney } 3704f46b5a66SChristoph Hellwig } 3705c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3706c5c9cd4dSSage Weil u64 skip = 0; 3707c5c9cd4dSSage Weil u64 trim = 0; 3708ed958762SFilipe Manana 3709c5c9cd4dSSage Weil if (off > key.offset) { 3710c5c9cd4dSSage Weil skip = off - key.offset; 3711c5c9cd4dSSage Weil new_key.offset += skip; 371231840ae1SZheng Yan } 3713d397712bSChris Mason 3714c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3715c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3716d397712bSChris Mason 3717c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3718c5c9cd4dSSage Weil ret = -EINVAL; 37193a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3720c5c9cd4dSSage Weil goto out; 372131840ae1SZheng Yan } 3722c5c9cd4dSSage Weil size -= skip + trim; 3723c5c9cd4dSSage Weil datal -= skip + trim; 3724a22285a6SYan, Zheng 37254a0ab9d7SDavid Sterba ret = clone_copy_inline_extent(inode, 37268039d87dSFilipe Manana trans, path, 37278039d87dSFilipe Manana &new_key, 3728f82a9901SFilipe Manana drop_start, 37298039d87dSFilipe Manana datal, 37308039d87dSFilipe Manana skip, size, buf); 373179787eaaSJeff Mahoney if (ret) { 37323f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 37333a29bc09SChris Mason btrfs_abort_transaction(trans, 373479787eaaSJeff Mahoney ret); 37353a45bb20SJeff Mahoney btrfs_end_transaction(trans); 373679787eaaSJeff Mahoney goto out; 373779787eaaSJeff Mahoney } 3738c5c9cd4dSSage Weil leaf = path->nodes[0]; 3739c5c9cd4dSSage Weil slot = path->slots[0]; 3740c5c9cd4dSSage Weil } 3741c5c9cd4dSSage Weil 37427ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 37437ffbb598SFilipe Manana if (drop_start < new_key.offset) 3744a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 374514f59796SFilipe Manana NULL, drop_start, 37467ffbb598SFilipe Manana new_key.offset - drop_start); 37477ffbb598SFilipe Manana 3748a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 3749a2f392e4SNikolay Borisov path, 0, 0); 37507ffbb598SFilipe Manana 3751c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3752b3b4aa74SDavid Sterba btrfs_release_path(path); 3753c5c9cd4dSSage Weil 375462e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 37550b246afaSJeff Mahoney fs_info->sectorsize); 3756f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3757f82a9901SFilipe Manana last_dest_end, 37581c919a5eSMark Fasheh destoff, olen, 37591c919a5eSMark Fasheh no_time_update); 3760f82a9901SFilipe Manana if (ret) 376179787eaaSJeff Mahoney goto out; 37622c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 37632c463823SFilipe Manana break; 3764a22285a6SYan, Zheng } 3765b3b4aa74SDavid Sterba btrfs_release_path(path); 3766df858e76SFilipe Manana key.offset = next_key_min_offset; 376769ae5e44SWang Xiaoguang 376869ae5e44SWang Xiaoguang if (fatal_signal_pending(current)) { 376969ae5e44SWang Xiaoguang ret = -EINTR; 377069ae5e44SWang Xiaoguang goto out; 377169ae5e44SWang Xiaoguang } 3772ae01a0abSYan Zheng } 3773f46b5a66SChristoph Hellwig ret = 0; 377432b7c687SMark Fasheh 3775f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3776f82a9901SFilipe Manana /* 3777f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3778f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3779f82a9901SFilipe Manana */ 3780b3b4aa74SDavid Sterba btrfs_release_path(path); 3781f82a9901SFilipe Manana 3782f82a9901SFilipe Manana /* 3783f82a9901SFilipe Manana * 1 - remove extent(s) 3784f82a9901SFilipe Manana * 1 - inode update 3785f82a9901SFilipe Manana */ 3786f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3787f82a9901SFilipe Manana if (IS_ERR(trans)) { 3788f82a9901SFilipe Manana ret = PTR_ERR(trans); 3789f82a9901SFilipe Manana goto out; 3790f82a9901SFilipe Manana } 3791f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3792f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3793f82a9901SFilipe Manana if (ret) { 3794f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 379566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 37963a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3797f82a9901SFilipe Manana goto out; 3798f82a9901SFilipe Manana } 3799a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, NULL, 3800a2f392e4SNikolay Borisov last_dest_end, 380114f59796SFilipe Manana destoff + len - last_dest_end); 3802f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 38031c919a5eSMark Fasheh destoff, olen, no_time_update); 3804f82a9901SFilipe Manana } 3805f82a9901SFilipe Manana 3806f46b5a66SChristoph Hellwig out: 380732b7c687SMark Fasheh btrfs_free_path(path); 380815351955SDavid Sterba kvfree(buf); 380932b7c687SMark Fasheh return ret; 381032b7c687SMark Fasheh } 381132b7c687SMark Fasheh 38123db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 381332b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 381432b7c687SMark Fasheh { 381554563d41SAl Viro struct inode *inode = file_inode(file); 38163db11b2eSZach Brown struct inode *src = file_inode(file_src); 38170b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 381832b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 381932b7c687SMark Fasheh int ret; 382032b7c687SMark Fasheh u64 len = olen; 38210b246afaSJeff Mahoney u64 bs = fs_info->sb->s_blocksize; 38223db11b2eSZach Brown int same_inode = src == inode; 382332b7c687SMark Fasheh 382432b7c687SMark Fasheh /* 382532b7c687SMark Fasheh * TODO: 382632b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 382732b7c687SMark Fasheh * decompress into destination's address_space (the file offset 382832b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 382932b7c687SMark Fasheh * otherwise reinsert) a subrange. 383000fdf13aSLiu Bo * 383100fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 383200fdf13aSLiu Bo * be either compressed or non-compressed. 383332b7c687SMark Fasheh */ 383432b7c687SMark Fasheh 383532b7c687SMark Fasheh if (btrfs_root_readonly(root)) 383632b7c687SMark Fasheh return -EROFS; 383732b7c687SMark Fasheh 38383db11b2eSZach Brown if (file_src->f_path.mnt != file->f_path.mnt || 38393db11b2eSZach Brown src->i_sb != inode->i_sb) 38403db11b2eSZach Brown return -EXDEV; 384132b7c687SMark Fasheh 384232b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 384332b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 384432b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 38453db11b2eSZach Brown return -EINVAL; 384632b7c687SMark Fasheh 384732b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 38483db11b2eSZach Brown return -EISDIR; 384932b7c687SMark Fasheh 385032b7c687SMark Fasheh if (!same_inode) { 3851293a8489SMark Fasheh btrfs_double_inode_lock(src, inode); 385232b7c687SMark Fasheh } else { 38535955102cSAl Viro inode_lock(src); 385432b7c687SMark Fasheh } 385532b7c687SMark Fasheh 385632b7c687SMark Fasheh /* determine range to clone */ 385732b7c687SMark Fasheh ret = -EINVAL; 385832b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 385932b7c687SMark Fasheh goto out_unlock; 386032b7c687SMark Fasheh if (len == 0) 386132b7c687SMark Fasheh olen = len = src->i_size - off; 386232b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 386332b7c687SMark Fasheh if (off + len == src->i_size) 386432b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 386532b7c687SMark Fasheh 3866ccccf3d6SFilipe Manana if (len == 0) { 3867ccccf3d6SFilipe Manana ret = 0; 3868ccccf3d6SFilipe Manana goto out_unlock; 3869ccccf3d6SFilipe Manana } 3870ccccf3d6SFilipe Manana 387132b7c687SMark Fasheh /* verify the end result is block aligned */ 387232b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 387332b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 387432b7c687SMark Fasheh goto out_unlock; 387532b7c687SMark Fasheh 387632b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 387732b7c687SMark Fasheh if (same_inode) { 387832b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 387932b7c687SMark Fasheh goto out_unlock; 388032b7c687SMark Fasheh } 388132b7c687SMark Fasheh 388232b7c687SMark Fasheh if (destoff > inode->i_size) { 388332b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 388432b7c687SMark Fasheh if (ret) 388532b7c687SMark Fasheh goto out_unlock; 388632b7c687SMark Fasheh } 388732b7c687SMark Fasheh 3888c125b8bfSFilipe Manana /* 3889c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3890c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3891c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3892c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3893c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3894c125b8bfSFilipe Manana */ 3895c125b8bfSFilipe Manana if (same_inode) { 3896c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3897c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 389832b7c687SMark Fasheh 3899e0bd70c6SFilipe Manana ret = lock_extent_range(src, lock_start, lock_len, true); 3900c125b8bfSFilipe Manana } else { 3901e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, off, inode, destoff, len, 3902e0bd70c6SFilipe Manana true); 3903e0bd70c6SFilipe Manana } 3904e0bd70c6SFilipe Manana ASSERT(ret == 0); 3905e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3906e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3907e0bd70c6SFilipe Manana goto out_unlock; 3908c125b8bfSFilipe Manana } 390932b7c687SMark Fasheh 39101c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 391132b7c687SMark Fasheh 3912c125b8bfSFilipe Manana if (same_inode) { 3913c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3914c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3915c125b8bfSFilipe Manana 3916c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3917c125b8bfSFilipe Manana } else { 3918293a8489SMark Fasheh btrfs_double_extent_unlock(src, off, inode, destoff, len); 3919c125b8bfSFilipe Manana } 3920c125b8bfSFilipe Manana /* 3921c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3922c125b8bfSFilipe Manana * data immediately and not the previous data. 3923c125b8bfSFilipe Manana */ 392465bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 392509cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 392609cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 3927f46b5a66SChristoph Hellwig out_unlock: 3928293a8489SMark Fasheh if (!same_inode) 3929293a8489SMark Fasheh btrfs_double_inode_unlock(src, inode); 3930293a8489SMark Fasheh else 39315955102cSAl Viro inode_unlock(src); 39323db11b2eSZach Brown return ret; 39333db11b2eSZach Brown } 39343db11b2eSZach Brown 393504b38d60SChristoph Hellwig int btrfs_clone_file_range(struct file *src_file, loff_t off, 393604b38d60SChristoph Hellwig struct file *dst_file, loff_t destoff, u64 len) 39373db11b2eSZach Brown { 393804b38d60SChristoph Hellwig return btrfs_clone_files(dst_file, src_file, off, len, destoff); 3939c5c9cd4dSSage Weil } 3940c5c9cd4dSSage Weil 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 */ 3947b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3948f46b5a66SChristoph Hellwig { 3949496ad9aaSAl Viro struct inode *inode = file_inode(file); 39500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3951f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3952f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 395323b5ec74SJosef Bacik struct btrfs_file_private *private; 39541ab86aedSSage Weil int ret; 39553558d4f8SNikolay Borisov static bool warned = false; 3956f46b5a66SChristoph Hellwig 39571ab86aedSSage Weil ret = -EPERM; 3958df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3959f46b5a66SChristoph Hellwig goto out; 39601ab86aedSSage Weil 39613558d4f8SNikolay Borisov if (!warned) { 39623558d4f8SNikolay Borisov btrfs_warn(fs_info, 39633558d4f8SNikolay Borisov "Userspace transaction mechanism is considered " 39643558d4f8SNikolay Borisov "deprecated and slated to be removed in 4.17. " 39653558d4f8SNikolay Borisov "If you have a valid use case please " 39663558d4f8SNikolay Borisov "speak up on the mailing list"); 39673558d4f8SNikolay Borisov WARN_ON(1); 39683558d4f8SNikolay Borisov warned = true; 39693558d4f8SNikolay Borisov } 39703558d4f8SNikolay Borisov 39711ab86aedSSage Weil ret = -EINPROGRESS; 397223b5ec74SJosef Bacik private = file->private_data; 397323b5ec74SJosef Bacik if (private && private->trans) 39741ab86aedSSage Weil goto out; 397523b5ec74SJosef Bacik if (!private) { 397623b5ec74SJosef Bacik private = kzalloc(sizeof(struct btrfs_file_private), 397723b5ec74SJosef Bacik GFP_KERNEL); 397823b5ec74SJosef Bacik if (!private) 397923b5ec74SJosef Bacik return -ENOMEM; 398023b5ec74SJosef Bacik file->private_data = private; 398123b5ec74SJosef Bacik } 39829ca9ee09SSage Weil 3983b83cc969SLi Zefan ret = -EROFS; 3984b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3985b83cc969SLi Zefan goto out; 3986b83cc969SLi Zefan 3987a561be71SAl Viro ret = mnt_want_write_file(file); 3988c146afadSYan Zheng if (ret) 3989c146afadSYan Zheng goto out; 3990c146afadSYan Zheng 39910b246afaSJeff Mahoney atomic_inc(&fs_info->open_ioctl_trans); 39929ca9ee09SSage Weil 3993f46b5a66SChristoph Hellwig ret = -ENOMEM; 39947a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3995abd30bb0STsutomu Itoh if (IS_ERR(trans)) 39961ab86aedSSage Weil goto out_drop; 39971ab86aedSSage Weil 399823b5ec74SJosef Bacik private->trans = trans; 39991ab86aedSSage Weil return 0; 40001ab86aedSSage Weil 40011ab86aedSSage Weil out_drop: 40020b246afaSJeff Mahoney atomic_dec(&fs_info->open_ioctl_trans); 40032a79f17eSAl Viro mnt_drop_write_file(file); 4004f46b5a66SChristoph Hellwig out: 4005f46b5a66SChristoph Hellwig return ret; 4006f46b5a66SChristoph Hellwig } 4007f46b5a66SChristoph Hellwig 40086ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 40096ef5ed0dSJosef Bacik { 4010496ad9aaSAl Viro struct inode *inode = file_inode(file); 40110b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 40126ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 40136ef5ed0dSJosef Bacik struct btrfs_root *new_root; 40146ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 40156ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 40166ef5ed0dSJosef Bacik struct btrfs_path *path; 40176ef5ed0dSJosef Bacik struct btrfs_key location; 40186ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 40196ef5ed0dSJosef Bacik u64 objectid = 0; 40206ef5ed0dSJosef Bacik u64 dir_id; 40213c04ce01SMiao Xie int ret; 40226ef5ed0dSJosef Bacik 40236ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 40246ef5ed0dSJosef Bacik return -EPERM; 40256ef5ed0dSJosef Bacik 40263c04ce01SMiao Xie ret = mnt_want_write_file(file); 40273c04ce01SMiao Xie if (ret) 40283c04ce01SMiao Xie return ret; 40293c04ce01SMiao Xie 40303c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 40313c04ce01SMiao Xie ret = -EFAULT; 40323c04ce01SMiao Xie goto out; 40333c04ce01SMiao Xie } 40346ef5ed0dSJosef Bacik 40356ef5ed0dSJosef Bacik if (!objectid) 40361cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 40376ef5ed0dSJosef Bacik 40386ef5ed0dSJosef Bacik location.objectid = objectid; 40396ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 40406ef5ed0dSJosef Bacik location.offset = (u64)-1; 40416ef5ed0dSJosef Bacik 40420b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &location); 40433c04ce01SMiao Xie if (IS_ERR(new_root)) { 40443c04ce01SMiao Xie ret = PTR_ERR(new_root); 40453c04ce01SMiao Xie goto out; 40463c04ce01SMiao Xie } 40476d6d2829Ssatoru takeuchi if (!is_fstree(new_root->objectid)) { 40486d6d2829Ssatoru takeuchi ret = -ENOENT; 40496d6d2829Ssatoru takeuchi goto out; 40506d6d2829Ssatoru takeuchi } 40516ef5ed0dSJosef Bacik 40526ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 40533c04ce01SMiao Xie if (!path) { 40543c04ce01SMiao Xie ret = -ENOMEM; 40553c04ce01SMiao Xie goto out; 40563c04ce01SMiao Xie } 40576ef5ed0dSJosef Bacik path->leave_spinning = 1; 40586ef5ed0dSJosef Bacik 40596ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 406098d5dc13STsutomu Itoh if (IS_ERR(trans)) { 40616ef5ed0dSJosef Bacik btrfs_free_path(path); 40623c04ce01SMiao Xie ret = PTR_ERR(trans); 40633c04ce01SMiao Xie goto out; 40646ef5ed0dSJosef Bacik } 40656ef5ed0dSJosef Bacik 40660b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 40670b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 40686ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4069cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 40706ef5ed0dSJosef Bacik btrfs_free_path(path); 40713a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40720b246afaSJeff Mahoney btrfs_err(fs_info, 40735d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 40743c04ce01SMiao Xie ret = -ENOENT; 40753c04ce01SMiao Xie goto out; 40766ef5ed0dSJosef Bacik } 40776ef5ed0dSJosef Bacik 40786ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 40796ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 40806ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 40816ef5ed0dSJosef Bacik btrfs_free_path(path); 40826ef5ed0dSJosef Bacik 40830b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 40843a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40853c04ce01SMiao Xie out: 40863c04ce01SMiao Xie mnt_drop_write_file(file); 40873c04ce01SMiao Xie return ret; 40886ef5ed0dSJosef Bacik } 40896ef5ed0dSJosef Bacik 40905af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 4091bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4092bf5fc093SJosef Bacik { 4093bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 4094bf5fc093SJosef Bacik 4095bf5fc093SJosef Bacik space->total_bytes = 0; 4096bf5fc093SJosef Bacik space->used_bytes = 0; 4097bf5fc093SJosef Bacik space->flags = 0; 4098bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4099bf5fc093SJosef Bacik space->flags = block_group->flags; 4100bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 4101bf5fc093SJosef Bacik space->used_bytes += 4102bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 4103bf5fc093SJosef Bacik } 4104bf5fc093SJosef Bacik } 4105bf5fc093SJosef Bacik 41062ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 41072ff7e61eSJeff Mahoney void __user *arg) 41081406e432SJosef Bacik { 41091406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 41101406e432SJosef Bacik struct btrfs_ioctl_space_info space; 41111406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 41127fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 411313f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 41141406e432SJosef Bacik struct btrfs_space_info *info; 4115315d8e98SColin Ian King static const u64 types[] = { 4116315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 4117bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4118bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4119315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 4120315d8e98SColin Ian King }; 4121bf5fc093SJosef Bacik int num_types = 4; 41227fde62bfSChris Mason int alloc_size; 41231406e432SJosef Bacik int ret = 0; 412451788b1bSDan Rosenberg u64 slot_count = 0; 4125bf5fc093SJosef Bacik int i, c; 41261406e432SJosef Bacik 41271406e432SJosef Bacik if (copy_from_user(&space_args, 41281406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 41291406e432SJosef Bacik sizeof(space_args))) 41301406e432SJosef Bacik return -EFAULT; 41311406e432SJosef Bacik 4132bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4133bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4134bf5fc093SJosef Bacik 4135bf5fc093SJosef Bacik info = NULL; 41367fde62bfSChris Mason rcu_read_lock(); 41370b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4138bf5fc093SJosef Bacik list) { 4139bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4140bf5fc093SJosef Bacik info = tmp; 4141bf5fc093SJosef Bacik break; 4142bf5fc093SJosef Bacik } 4143bf5fc093SJosef Bacik } 41447fde62bfSChris Mason rcu_read_unlock(); 41451406e432SJosef Bacik 4146bf5fc093SJosef Bacik if (!info) 4147bf5fc093SJosef Bacik continue; 4148bf5fc093SJosef Bacik 4149bf5fc093SJosef Bacik down_read(&info->groups_sem); 4150bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4151bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4152bf5fc093SJosef Bacik slot_count++; 4153bf5fc093SJosef Bacik } 4154bf5fc093SJosef Bacik up_read(&info->groups_sem); 4155bf5fc093SJosef Bacik } 4156bf5fc093SJosef Bacik 415736523e95SDavid Sterba /* 415836523e95SDavid Sterba * Global block reserve, exported as a space_info 415936523e95SDavid Sterba */ 416036523e95SDavid Sterba slot_count++; 416136523e95SDavid Sterba 41627fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 41637fde62bfSChris Mason if (space_args.space_slots == 0) { 41647fde62bfSChris Mason space_args.total_spaces = slot_count; 41657fde62bfSChris Mason goto out; 41667fde62bfSChris Mason } 4167bf5fc093SJosef Bacik 416851788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4169bf5fc093SJosef Bacik 41707fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4171bf5fc093SJosef Bacik 41727fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 41737fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 41747fde62bfSChris Mason */ 417509cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 41767fde62bfSChris Mason return -ENOMEM; 41777fde62bfSChris Mason 41787fde62bfSChris Mason space_args.total_spaces = 0; 41798d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 41807fde62bfSChris Mason if (!dest) 41817fde62bfSChris Mason return -ENOMEM; 41827fde62bfSChris Mason dest_orig = dest; 41837fde62bfSChris Mason 41847fde62bfSChris Mason /* now we have a buffer to copy into */ 4185bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4186bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4187bf5fc093SJosef Bacik 418851788b1bSDan Rosenberg if (!slot_count) 418951788b1bSDan Rosenberg break; 419051788b1bSDan Rosenberg 4191bf5fc093SJosef Bacik info = NULL; 41921406e432SJosef Bacik rcu_read_lock(); 41930b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4194bf5fc093SJosef Bacik list) { 4195bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4196bf5fc093SJosef Bacik info = tmp; 41977fde62bfSChris Mason break; 4198bf5fc093SJosef Bacik } 4199bf5fc093SJosef Bacik } 4200bf5fc093SJosef Bacik rcu_read_unlock(); 42017fde62bfSChris Mason 4202bf5fc093SJosef Bacik if (!info) 4203bf5fc093SJosef Bacik continue; 4204bf5fc093SJosef Bacik down_read(&info->groups_sem); 4205bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4206bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 42075af3e8ccSStefan Behrens btrfs_get_block_group_info( 42085af3e8ccSStefan Behrens &info->block_groups[c], &space); 42097fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 42101406e432SJosef Bacik dest++; 42111406e432SJosef Bacik space_args.total_spaces++; 421251788b1bSDan Rosenberg slot_count--; 42131406e432SJosef Bacik } 421451788b1bSDan Rosenberg if (!slot_count) 421551788b1bSDan Rosenberg break; 4216bf5fc093SJosef Bacik } 4217bf5fc093SJosef Bacik up_read(&info->groups_sem); 4218bf5fc093SJosef Bacik } 42191406e432SJosef Bacik 422036523e95SDavid Sterba /* 422136523e95SDavid Sterba * Add global block reserve 422236523e95SDavid Sterba */ 422336523e95SDavid Sterba if (slot_count) { 42240b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 422536523e95SDavid Sterba 422636523e95SDavid Sterba spin_lock(&block_rsv->lock); 422736523e95SDavid Sterba space.total_bytes = block_rsv->size; 422836523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 422936523e95SDavid Sterba spin_unlock(&block_rsv->lock); 423036523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 423136523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 423236523e95SDavid Sterba space_args.total_spaces++; 423336523e95SDavid Sterba } 423436523e95SDavid Sterba 42352eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 42367fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 42377fde62bfSChris Mason 42387fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 42397fde62bfSChris Mason ret = -EFAULT; 42407fde62bfSChris Mason 42417fde62bfSChris Mason kfree(dest_orig); 42427fde62bfSChris Mason out: 42437fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 42441406e432SJosef Bacik ret = -EFAULT; 42451406e432SJosef Bacik 42461406e432SJosef Bacik return ret; 42471406e432SJosef Bacik } 42481406e432SJosef Bacik 4249f46b5a66SChristoph Hellwig /* 4250f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 4251f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 4252f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 4253f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 4254f46b5a66SChristoph Hellwig */ 4255f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 4256f46b5a66SChristoph Hellwig { 4257496ad9aaSAl Viro struct inode *inode = file_inode(file); 4258f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 425923b5ec74SJosef Bacik struct btrfs_file_private *private = file->private_data; 4260f46b5a66SChristoph Hellwig 426123b5ec74SJosef Bacik if (!private || !private->trans) 42621ab86aedSSage Weil return -EINVAL; 42639ca9ee09SSage Weil 426423b5ec74SJosef Bacik btrfs_end_transaction(private->trans); 426523b5ec74SJosef Bacik private->trans = NULL; 42661ab86aedSSage Weil 4267a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 42689ca9ee09SSage Weil 42692a79f17eSAl Viro mnt_drop_write_file(file); 42701ab86aedSSage Weil return 0; 4271f46b5a66SChristoph Hellwig } 4272f46b5a66SChristoph Hellwig 42739a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 42749a8c28beSMiao Xie void __user *argp) 427546204592SSage Weil { 427646204592SSage Weil struct btrfs_trans_handle *trans; 427746204592SSage Weil u64 transid; 4278db5b493aSTsutomu Itoh int ret; 427946204592SSage Weil 4280d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4281ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4282ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 428398d5dc13STsutomu Itoh return PTR_ERR(trans); 4284ff7c1d33SMiao Xie 4285ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4286ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4287ff7c1d33SMiao Xie goto out; 4288ff7c1d33SMiao Xie } 428946204592SSage Weil transid = trans->transid; 42903a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 0); 42918b2b2d3cSTsutomu Itoh if (ret) { 42923a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4293db5b493aSTsutomu Itoh return ret; 42948b2b2d3cSTsutomu Itoh } 4295ff7c1d33SMiao Xie out: 429646204592SSage Weil if (argp) 429746204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 429846204592SSage Weil return -EFAULT; 429946204592SSage Weil return 0; 430046204592SSage Weil } 430146204592SSage Weil 43022ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 43039a8c28beSMiao Xie void __user *argp) 430446204592SSage Weil { 430546204592SSage Weil u64 transid; 430646204592SSage Weil 430746204592SSage Weil if (argp) { 430846204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 430946204592SSage Weil return -EFAULT; 431046204592SSage Weil } else { 431146204592SSage Weil transid = 0; /* current trans */ 431246204592SSage Weil } 43132ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 431446204592SSage Weil } 431546204592SSage Weil 4316b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4317475f6387SJan Schmidt { 43180b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4319475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4320b8e95489SMiao Xie int ret; 4321475f6387SJan Schmidt 4322475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4323475f6387SJan Schmidt return -EPERM; 4324475f6387SJan Schmidt 4325475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4326475f6387SJan Schmidt if (IS_ERR(sa)) 4327475f6387SJan Schmidt return PTR_ERR(sa); 4328475f6387SJan Schmidt 4329b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4330b8e95489SMiao Xie ret = mnt_want_write_file(file); 4331b8e95489SMiao Xie if (ret) 4332b8e95489SMiao Xie goto out; 4333b8e95489SMiao Xie } 4334b8e95489SMiao Xie 43350b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 433663a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 433763a212abSStefan Behrens 0); 4338475f6387SJan Schmidt 4339475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4340475f6387SJan Schmidt ret = -EFAULT; 4341475f6387SJan Schmidt 4342b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4343b8e95489SMiao Xie mnt_drop_write_file(file); 4344b8e95489SMiao Xie out: 4345475f6387SJan Schmidt kfree(sa); 4346475f6387SJan Schmidt return ret; 4347475f6387SJan Schmidt } 4348475f6387SJan Schmidt 43492ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4350475f6387SJan Schmidt { 4351475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4352475f6387SJan Schmidt return -EPERM; 4353475f6387SJan Schmidt 43542ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4355475f6387SJan Schmidt } 4356475f6387SJan Schmidt 43572ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4358475f6387SJan Schmidt void __user *arg) 4359475f6387SJan Schmidt { 4360475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4361475f6387SJan Schmidt int ret; 4362475f6387SJan Schmidt 4363475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4364475f6387SJan Schmidt return -EPERM; 4365475f6387SJan Schmidt 4366475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4367475f6387SJan Schmidt if (IS_ERR(sa)) 4368475f6387SJan Schmidt return PTR_ERR(sa); 4369475f6387SJan Schmidt 43702ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4371475f6387SJan Schmidt 4372475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4373475f6387SJan Schmidt ret = -EFAULT; 4374475f6387SJan Schmidt 4375475f6387SJan Schmidt kfree(sa); 4376475f6387SJan Schmidt return ret; 4377475f6387SJan Schmidt } 4378475f6387SJan Schmidt 43792ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4380b27f7c0cSDavid Sterba void __user *arg) 4381c11d2c23SStefan Behrens { 4382c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4383c11d2c23SStefan Behrens int ret; 4384c11d2c23SStefan Behrens 4385c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4386c11d2c23SStefan Behrens if (IS_ERR(sa)) 4387c11d2c23SStefan Behrens return PTR_ERR(sa); 4388c11d2c23SStefan Behrens 4389b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4390b27f7c0cSDavid Sterba kfree(sa); 4391b27f7c0cSDavid Sterba return -EPERM; 4392b27f7c0cSDavid Sterba } 4393b27f7c0cSDavid Sterba 43942ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4395c11d2c23SStefan Behrens 4396c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4397c11d2c23SStefan Behrens ret = -EFAULT; 4398c11d2c23SStefan Behrens 4399c11d2c23SStefan Behrens kfree(sa); 4400c11d2c23SStefan Behrens return ret; 4401c11d2c23SStefan Behrens } 4402c11d2c23SStefan Behrens 44032ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 44042ff7e61eSJeff Mahoney void __user *arg) 44053f6bcfbdSStefan Behrens { 44063f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 44073f6bcfbdSStefan Behrens int ret; 44083f6bcfbdSStefan Behrens 44093f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 44103f6bcfbdSStefan Behrens return -EPERM; 44113f6bcfbdSStefan Behrens 44123f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 44133f6bcfbdSStefan Behrens if (IS_ERR(p)) 44143f6bcfbdSStefan Behrens return PTR_ERR(p); 44153f6bcfbdSStefan Behrens 44163f6bcfbdSStefan Behrens switch (p->cmd) { 44173f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4418bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4419adfa97cbSIlya Dryomov ret = -EROFS; 4420adfa97cbSIlya Dryomov goto out; 4421adfa97cbSIlya Dryomov } 4422171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4423e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 44243f6bcfbdSStefan Behrens } else { 44252ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4426171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 44273f6bcfbdSStefan Behrens } 44283f6bcfbdSStefan Behrens break; 44293f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 44300b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 44313f6bcfbdSStefan Behrens ret = 0; 44323f6bcfbdSStefan Behrens break; 44333f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 44340b246afaSJeff Mahoney ret = btrfs_dev_replace_cancel(fs_info, p); 44353f6bcfbdSStefan Behrens break; 44363f6bcfbdSStefan Behrens default: 44373f6bcfbdSStefan Behrens ret = -EINVAL; 44383f6bcfbdSStefan Behrens break; 44393f6bcfbdSStefan Behrens } 44403f6bcfbdSStefan Behrens 44413f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 44423f6bcfbdSStefan Behrens ret = -EFAULT; 4443adfa97cbSIlya Dryomov out: 44443f6bcfbdSStefan Behrens kfree(p); 44453f6bcfbdSStefan Behrens return ret; 44463f6bcfbdSStefan Behrens } 44473f6bcfbdSStefan Behrens 4448d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4449d7728c96SJan Schmidt { 4450d7728c96SJan Schmidt int ret = 0; 4451d7728c96SJan Schmidt int i; 4452740c3d22SChris Mason u64 rel_ptr; 4453d7728c96SJan Schmidt int size; 4454806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4455d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4456d7728c96SJan Schmidt struct btrfs_path *path; 4457d7728c96SJan Schmidt 445882b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4459d7728c96SJan Schmidt return -EPERM; 4460d7728c96SJan Schmidt 4461d7728c96SJan Schmidt path = btrfs_alloc_path(); 4462d7728c96SJan Schmidt if (!path) { 4463d7728c96SJan Schmidt ret = -ENOMEM; 4464d7728c96SJan Schmidt goto out; 4465d7728c96SJan Schmidt } 4466d7728c96SJan Schmidt 4467d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4468d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4469d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4470d7728c96SJan Schmidt ipa = NULL; 4471d7728c96SJan Schmidt goto out; 4472d7728c96SJan Schmidt } 4473d7728c96SJan Schmidt 4474d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4475d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4476d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4477d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4478d7728c96SJan Schmidt ipath = NULL; 4479d7728c96SJan Schmidt goto out; 4480d7728c96SJan Schmidt } 4481d7728c96SJan Schmidt 4482d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4483d7728c96SJan Schmidt if (ret < 0) 4484d7728c96SJan Schmidt goto out; 4485d7728c96SJan Schmidt 4486d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4487745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4488745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4489740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4490d7728c96SJan Schmidt } 4491d7728c96SJan Schmidt 4492718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4493718dc5faSOmar Sandoval ipath->fspath, size); 4494d7728c96SJan Schmidt if (ret) { 4495d7728c96SJan Schmidt ret = -EFAULT; 4496d7728c96SJan Schmidt goto out; 4497d7728c96SJan Schmidt } 4498d7728c96SJan Schmidt 4499d7728c96SJan Schmidt out: 4500d7728c96SJan Schmidt btrfs_free_path(path); 4501d7728c96SJan Schmidt free_ipath(ipath); 4502d7728c96SJan Schmidt kfree(ipa); 4503d7728c96SJan Schmidt 4504d7728c96SJan Schmidt return ret; 4505d7728c96SJan Schmidt } 4506d7728c96SJan Schmidt 4507d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4508d7728c96SJan Schmidt { 4509d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4510d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4511d7728c96SJan Schmidt 4512d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4513d7728c96SJan Schmidt inodes->bytes_left -= c; 4514d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4515d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4516d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4517d7728c96SJan Schmidt inodes->elem_cnt += 3; 4518d7728c96SJan Schmidt } else { 4519d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4520d7728c96SJan Schmidt inodes->bytes_left = 0; 4521d7728c96SJan Schmidt inodes->elem_missed += 3; 4522d7728c96SJan Schmidt } 4523d7728c96SJan Schmidt 4524d7728c96SJan Schmidt return 0; 4525d7728c96SJan Schmidt } 4526d7728c96SJan Schmidt 45272ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4528d24a67b2SZygo Blaxell void __user *arg, int version) 4529d7728c96SJan Schmidt { 4530d7728c96SJan Schmidt int ret = 0; 4531d7728c96SJan Schmidt int size; 4532d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4533d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4534d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4535d24a67b2SZygo Blaxell bool ignore_offset; 4536d7728c96SJan Schmidt 4537d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4538d7728c96SJan Schmidt return -EPERM; 4539d7728c96SJan Schmidt 4540d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 45417b9ea627SShailendra Verma if (IS_ERR(loi)) 45427b9ea627SShailendra Verma return PTR_ERR(loi); 4543d7728c96SJan Schmidt 4544d24a67b2SZygo Blaxell if (version == 1) { 4545d24a67b2SZygo Blaxell ignore_offset = false; 4546b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4547d24a67b2SZygo Blaxell } else { 4548d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4549d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4550d24a67b2SZygo Blaxell ret = -EINVAL; 4551d24a67b2SZygo Blaxell goto out_loi; 4552d24a67b2SZygo Blaxell } 4553d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4554d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4555d24a67b2SZygo Blaxell ret = -EINVAL; 4556d24a67b2SZygo Blaxell goto out_loi; 4557d24a67b2SZygo Blaxell } 4558d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4559b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4560d24a67b2SZygo Blaxell } 4561d24a67b2SZygo Blaxell 4562d7728c96SJan Schmidt path = btrfs_alloc_path(); 4563d7728c96SJan Schmidt if (!path) { 4564d7728c96SJan Schmidt ret = -ENOMEM; 4565d7728c96SJan Schmidt goto out; 4566d7728c96SJan Schmidt } 4567d7728c96SJan Schmidt 4568d7728c96SJan Schmidt inodes = init_data_container(size); 4569d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4570d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4571d7728c96SJan Schmidt inodes = NULL; 4572d7728c96SJan Schmidt goto out; 4573d7728c96SJan Schmidt } 4574d7728c96SJan Schmidt 45752ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4576d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4577df031f07SLiu Bo if (ret == -EINVAL) 4578d7728c96SJan Schmidt ret = -ENOENT; 4579d7728c96SJan Schmidt if (ret < 0) 4580d7728c96SJan Schmidt goto out; 4581d7728c96SJan Schmidt 4582718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4583718dc5faSOmar Sandoval size); 4584d7728c96SJan Schmidt if (ret) 4585d7728c96SJan Schmidt ret = -EFAULT; 4586d7728c96SJan Schmidt 4587d7728c96SJan Schmidt out: 4588d7728c96SJan Schmidt btrfs_free_path(path); 4589f54de068SDavid Sterba kvfree(inodes); 4590d24a67b2SZygo Blaxell out_loi: 4591d7728c96SJan Schmidt kfree(loi); 4592d7728c96SJan Schmidt 4593d7728c96SJan Schmidt return ret; 4594d7728c96SJan Schmidt } 4595d7728c96SJan Schmidt 459619a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4597c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4598c9e9f97bSIlya Dryomov { 4599c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4600c9e9f97bSIlya Dryomov 4601c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4602c9e9f97bSIlya Dryomov 4603837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4604837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4605837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4606837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4607a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4608a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4609837d5b6eSIlya Dryomov 4610c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4611c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4612c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 461319a39dceSIlya Dryomov 461419a39dceSIlya Dryomov if (lock) { 461519a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 461619a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 461719a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 461819a39dceSIlya Dryomov } else { 461919a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 462019a39dceSIlya Dryomov } 4621c9e9f97bSIlya Dryomov } 4622c9e9f97bSIlya Dryomov 46239ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4624c9e9f97bSIlya Dryomov { 4625496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4626c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4627c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4628c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4629ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4630c9e9f97bSIlya Dryomov int ret; 4631c9e9f97bSIlya Dryomov 4632c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4633c9e9f97bSIlya Dryomov return -EPERM; 4634c9e9f97bSIlya Dryomov 4635e54bfa31SLiu Bo ret = mnt_want_write_file(file); 46369ba1f6e4SLiu Bo if (ret) 46379ba1f6e4SLiu Bo return ret; 46389ba1f6e4SLiu Bo 4639ed0fb78fSIlya Dryomov again: 4640171938e5SDavid Sterba if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4641c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4642c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4643ed0fb78fSIlya Dryomov need_unlock = true; 4644ed0fb78fSIlya Dryomov goto locked; 4645ed0fb78fSIlya Dryomov } 4646ed0fb78fSIlya Dryomov 4647ed0fb78fSIlya Dryomov /* 464801327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4649ed0fb78fSIlya Dryomov * (1) some other op is running 4650ed0fb78fSIlya Dryomov * (2) balance is running 4651ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4652ed0fb78fSIlya Dryomov */ 4653ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4654ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4655ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4656ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4657ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4658ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4659ed0fb78fSIlya Dryomov goto again; 4660ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4661ed0fb78fSIlya Dryomov 4662ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4663ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4664ed0fb78fSIlya Dryomov /* this is (3) */ 4665ed0fb78fSIlya Dryomov need_unlock = false; 4666ed0fb78fSIlya Dryomov goto locked; 4667ed0fb78fSIlya Dryomov } 4668ed0fb78fSIlya Dryomov 4669ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4670ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4671ed0fb78fSIlya Dryomov goto again; 4672ed0fb78fSIlya Dryomov } else { 4673ed0fb78fSIlya Dryomov /* this is (2) */ 4674ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4675ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4676ed0fb78fSIlya Dryomov goto out; 4677ed0fb78fSIlya Dryomov } 4678ed0fb78fSIlya Dryomov } else { 4679ed0fb78fSIlya Dryomov /* this is (1) */ 4680ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4681e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4682ed0fb78fSIlya Dryomov goto out; 4683ed0fb78fSIlya Dryomov } 4684ed0fb78fSIlya Dryomov 4685ed0fb78fSIlya Dryomov locked: 4686171938e5SDavid Sterba BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); 4687c9e9f97bSIlya Dryomov 4688c9e9f97bSIlya Dryomov if (arg) { 4689c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4690c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4691c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4692ed0fb78fSIlya Dryomov goto out_unlock; 4693c9e9f97bSIlya Dryomov } 4694de322263SIlya Dryomov 4695de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4696de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4697de322263SIlya Dryomov ret = -ENOTCONN; 4698de322263SIlya Dryomov goto out_bargs; 4699de322263SIlya Dryomov } 4700de322263SIlya Dryomov 4701de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4702de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4703de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4704de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4705de322263SIlya Dryomov 4706de322263SIlya Dryomov goto do_balance; 4707de322263SIlya Dryomov } 4708c9e9f97bSIlya Dryomov } else { 4709c9e9f97bSIlya Dryomov bargs = NULL; 4710c9e9f97bSIlya Dryomov } 4711c9e9f97bSIlya Dryomov 4712ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4713837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4714837d5b6eSIlya Dryomov goto out_bargs; 4715837d5b6eSIlya Dryomov } 4716837d5b6eSIlya Dryomov 47178d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4718c9e9f97bSIlya Dryomov if (!bctl) { 4719c9e9f97bSIlya Dryomov ret = -ENOMEM; 4720c9e9f97bSIlya Dryomov goto out_bargs; 4721c9e9f97bSIlya Dryomov } 4722c9e9f97bSIlya Dryomov 4723c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4724c9e9f97bSIlya Dryomov if (arg) { 4725c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4726c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4727c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4728c9e9f97bSIlya Dryomov 4729c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4730f43ffb60SIlya Dryomov } else { 4731f43ffb60SIlya Dryomov /* balance everything - no filters */ 4732f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4733c9e9f97bSIlya Dryomov } 4734c9e9f97bSIlya Dryomov 47358eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 47368eb93459SDavid Sterba ret = -EINVAL; 47370f89abf5SChristian Engelmayer goto out_bctl; 47388eb93459SDavid Sterba } 47398eb93459SDavid Sterba 4740de322263SIlya Dryomov do_balance: 4741c9e9f97bSIlya Dryomov /* 4742171938e5SDavid Sterba * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP 4743ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4744ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4745171938e5SDavid Sterba * free_fs_info. The flag is cleared in __cancel_balance. 4746c9e9f97bSIlya Dryomov */ 4747ed0fb78fSIlya Dryomov need_unlock = false; 4748ed0fb78fSIlya Dryomov 4749ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 47500f89abf5SChristian Engelmayer bctl = NULL; 4751ed0fb78fSIlya Dryomov 4752c9e9f97bSIlya Dryomov if (arg) { 4753c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4754c9e9f97bSIlya Dryomov ret = -EFAULT; 4755c9e9f97bSIlya Dryomov } 4756c9e9f97bSIlya Dryomov 47570f89abf5SChristian Engelmayer out_bctl: 47580f89abf5SChristian Engelmayer kfree(bctl); 4759c9e9f97bSIlya Dryomov out_bargs: 4760c9e9f97bSIlya Dryomov kfree(bargs); 4761ed0fb78fSIlya Dryomov out_unlock: 4762c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4763c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4764ed0fb78fSIlya Dryomov if (need_unlock) 4765171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 4766ed0fb78fSIlya Dryomov out: 4767e54bfa31SLiu Bo mnt_drop_write_file(file); 4768c9e9f97bSIlya Dryomov return ret; 4769c9e9f97bSIlya Dryomov } 4770c9e9f97bSIlya Dryomov 47712ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4772837d5b6eSIlya Dryomov { 4773837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4774837d5b6eSIlya Dryomov return -EPERM; 4775837d5b6eSIlya Dryomov 4776837d5b6eSIlya Dryomov switch (cmd) { 4777837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 47780b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4779a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 47800b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4781837d5b6eSIlya Dryomov } 4782837d5b6eSIlya Dryomov 4783837d5b6eSIlya Dryomov return -EINVAL; 4784837d5b6eSIlya Dryomov } 4785837d5b6eSIlya Dryomov 47862ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 478719a39dceSIlya Dryomov void __user *arg) 478819a39dceSIlya Dryomov { 478919a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 479019a39dceSIlya Dryomov int ret = 0; 479119a39dceSIlya Dryomov 479219a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 479319a39dceSIlya Dryomov return -EPERM; 479419a39dceSIlya Dryomov 479519a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 479619a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 479719a39dceSIlya Dryomov ret = -ENOTCONN; 479819a39dceSIlya Dryomov goto out; 479919a39dceSIlya Dryomov } 480019a39dceSIlya Dryomov 48018d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 480219a39dceSIlya Dryomov if (!bargs) { 480319a39dceSIlya Dryomov ret = -ENOMEM; 480419a39dceSIlya Dryomov goto out; 480519a39dceSIlya Dryomov } 480619a39dceSIlya Dryomov 480719a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 480819a39dceSIlya Dryomov 480919a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 481019a39dceSIlya Dryomov ret = -EFAULT; 481119a39dceSIlya Dryomov 481219a39dceSIlya Dryomov kfree(bargs); 481319a39dceSIlya Dryomov out: 481419a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 481519a39dceSIlya Dryomov return ret; 481619a39dceSIlya Dryomov } 481719a39dceSIlya Dryomov 4818905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 48195d13a37bSArne Jansen { 48200b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48210b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48225d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 48235d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 48245d13a37bSArne Jansen int ret; 48255d13a37bSArne Jansen int err; 48265d13a37bSArne Jansen 48275d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48285d13a37bSArne Jansen return -EPERM; 48295d13a37bSArne Jansen 4830905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4831905b0ddaSMiao Xie if (ret) 4832905b0ddaSMiao Xie return ret; 48335d13a37bSArne Jansen 48345d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4835905b0ddaSMiao Xie if (IS_ERR(sa)) { 4836905b0ddaSMiao Xie ret = PTR_ERR(sa); 4837905b0ddaSMiao Xie goto drop_write; 4838905b0ddaSMiao Xie } 48395d13a37bSArne Jansen 48400b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 48410b246afaSJeff Mahoney trans = btrfs_start_transaction(fs_info->tree_root, 2); 48425d13a37bSArne Jansen if (IS_ERR(trans)) { 48435d13a37bSArne Jansen ret = PTR_ERR(trans); 48445d13a37bSArne Jansen goto out; 48455d13a37bSArne Jansen } 48465d13a37bSArne Jansen 48475d13a37bSArne Jansen switch (sa->cmd) { 48485d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 48490b246afaSJeff Mahoney ret = btrfs_quota_enable(trans, fs_info); 48505d13a37bSArne Jansen break; 48515d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 48520b246afaSJeff Mahoney ret = btrfs_quota_disable(trans, fs_info); 48535d13a37bSArne Jansen break; 48545d13a37bSArne Jansen default: 48555d13a37bSArne Jansen ret = -EINVAL; 48565d13a37bSArne Jansen break; 48575d13a37bSArne Jansen } 48585d13a37bSArne Jansen 48593a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 48605d13a37bSArne Jansen if (err && !ret) 48615d13a37bSArne Jansen ret = err; 48625d13a37bSArne Jansen out: 48635d13a37bSArne Jansen kfree(sa); 48640b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4865905b0ddaSMiao Xie drop_write: 4866905b0ddaSMiao Xie mnt_drop_write_file(file); 48675d13a37bSArne Jansen return ret; 48685d13a37bSArne Jansen } 48695d13a37bSArne Jansen 4870905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 48715d13a37bSArne Jansen { 48720b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48730b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48740b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48755d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 48765d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48775d13a37bSArne Jansen int ret; 48785d13a37bSArne Jansen int err; 48795d13a37bSArne Jansen 48805d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48815d13a37bSArne Jansen return -EPERM; 48825d13a37bSArne Jansen 4883905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4884905b0ddaSMiao Xie if (ret) 4885905b0ddaSMiao Xie return ret; 48865d13a37bSArne Jansen 48875d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4888905b0ddaSMiao Xie if (IS_ERR(sa)) { 4889905b0ddaSMiao Xie ret = PTR_ERR(sa); 4890905b0ddaSMiao Xie goto drop_write; 4891905b0ddaSMiao Xie } 48925d13a37bSArne Jansen 48935d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48945d13a37bSArne Jansen if (IS_ERR(trans)) { 48955d13a37bSArne Jansen ret = PTR_ERR(trans); 48965d13a37bSArne Jansen goto out; 48975d13a37bSArne Jansen } 48985d13a37bSArne Jansen 48995d13a37bSArne Jansen if (sa->assign) { 49000b246afaSJeff Mahoney ret = btrfs_add_qgroup_relation(trans, fs_info, 49015d13a37bSArne Jansen sa->src, sa->dst); 49025d13a37bSArne Jansen } else { 49030b246afaSJeff Mahoney ret = btrfs_del_qgroup_relation(trans, fs_info, 49045d13a37bSArne Jansen sa->src, sa->dst); 49055d13a37bSArne Jansen } 49065d13a37bSArne Jansen 4907e082f563SQu Wenruo /* update qgroup status and info */ 49080b246afaSJeff Mahoney err = btrfs_run_qgroups(trans, fs_info); 4909e082f563SQu Wenruo if (err < 0) 49100b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4911ad8403dfSAnand Jain "failed to update qgroup status and info"); 49123a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49135d13a37bSArne Jansen if (err && !ret) 49145d13a37bSArne Jansen ret = err; 49155d13a37bSArne Jansen 49165d13a37bSArne Jansen out: 49175d13a37bSArne Jansen kfree(sa); 4918905b0ddaSMiao Xie drop_write: 4919905b0ddaSMiao Xie mnt_drop_write_file(file); 49205d13a37bSArne Jansen return ret; 49215d13a37bSArne Jansen } 49225d13a37bSArne Jansen 4923905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 49245d13a37bSArne Jansen { 49250b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49270b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 49285d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 49295d13a37bSArne Jansen struct btrfs_trans_handle *trans; 49305d13a37bSArne Jansen int ret; 49315d13a37bSArne Jansen int err; 49325d13a37bSArne Jansen 49335d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 49345d13a37bSArne Jansen return -EPERM; 49355d13a37bSArne Jansen 4936905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4937905b0ddaSMiao Xie if (ret) 4938905b0ddaSMiao Xie return ret; 49395d13a37bSArne Jansen 49405d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4941905b0ddaSMiao Xie if (IS_ERR(sa)) { 4942905b0ddaSMiao Xie ret = PTR_ERR(sa); 4943905b0ddaSMiao Xie goto drop_write; 4944905b0ddaSMiao Xie } 49455d13a37bSArne Jansen 4946d86e56cfSMiao Xie if (!sa->qgroupid) { 4947d86e56cfSMiao Xie ret = -EINVAL; 4948d86e56cfSMiao Xie goto out; 4949d86e56cfSMiao Xie } 4950d86e56cfSMiao Xie 49515d13a37bSArne Jansen trans = btrfs_join_transaction(root); 49525d13a37bSArne Jansen if (IS_ERR(trans)) { 49535d13a37bSArne Jansen ret = PTR_ERR(trans); 49545d13a37bSArne Jansen goto out; 49555d13a37bSArne Jansen } 49565d13a37bSArne Jansen 49575d13a37bSArne Jansen if (sa->create) { 49580b246afaSJeff Mahoney ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid); 49595d13a37bSArne Jansen } else { 49600b246afaSJeff Mahoney ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid); 49615d13a37bSArne Jansen } 49625d13a37bSArne Jansen 49633a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49645d13a37bSArne Jansen if (err && !ret) 49655d13a37bSArne Jansen ret = err; 49665d13a37bSArne Jansen 49675d13a37bSArne Jansen out: 49685d13a37bSArne Jansen kfree(sa); 4969905b0ddaSMiao Xie drop_write: 4970905b0ddaSMiao Xie mnt_drop_write_file(file); 49715d13a37bSArne Jansen return ret; 49725d13a37bSArne Jansen } 49735d13a37bSArne Jansen 4974905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 49755d13a37bSArne Jansen { 49760b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49770b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49780b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 49795d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 49805d13a37bSArne Jansen struct btrfs_trans_handle *trans; 49815d13a37bSArne Jansen int ret; 49825d13a37bSArne Jansen int err; 49835d13a37bSArne Jansen u64 qgroupid; 49845d13a37bSArne Jansen 49855d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 49865d13a37bSArne Jansen return -EPERM; 49875d13a37bSArne Jansen 4988905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4989905b0ddaSMiao Xie if (ret) 4990905b0ddaSMiao Xie return ret; 49915d13a37bSArne Jansen 49925d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4993905b0ddaSMiao Xie if (IS_ERR(sa)) { 4994905b0ddaSMiao Xie ret = PTR_ERR(sa); 4995905b0ddaSMiao Xie goto drop_write; 4996905b0ddaSMiao Xie } 49975d13a37bSArne Jansen 49985d13a37bSArne Jansen trans = btrfs_join_transaction(root); 49995d13a37bSArne Jansen if (IS_ERR(trans)) { 50005d13a37bSArne Jansen ret = PTR_ERR(trans); 50015d13a37bSArne Jansen goto out; 50025d13a37bSArne Jansen } 50035d13a37bSArne Jansen 50045d13a37bSArne Jansen qgroupid = sa->qgroupid; 50055d13a37bSArne Jansen if (!qgroupid) { 50065d13a37bSArne Jansen /* take the current subvol as qgroup */ 50075d13a37bSArne Jansen qgroupid = root->root_key.objectid; 50085d13a37bSArne Jansen } 50095d13a37bSArne Jansen 50100b246afaSJeff Mahoney ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim); 50115d13a37bSArne Jansen 50123a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 50135d13a37bSArne Jansen if (err && !ret) 50145d13a37bSArne Jansen ret = err; 50155d13a37bSArne Jansen 50165d13a37bSArne Jansen out: 50175d13a37bSArne Jansen kfree(sa); 5018905b0ddaSMiao Xie drop_write: 5019905b0ddaSMiao Xie mnt_drop_write_file(file); 50205d13a37bSArne Jansen return ret; 50215d13a37bSArne Jansen } 50225d13a37bSArne Jansen 50232f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 50242f232036SJan Schmidt { 50250b246afaSJeff Mahoney struct inode *inode = file_inode(file); 50260b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 50272f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 50282f232036SJan Schmidt int ret; 50292f232036SJan Schmidt 50302f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 50312f232036SJan Schmidt return -EPERM; 50322f232036SJan Schmidt 50332f232036SJan Schmidt ret = mnt_want_write_file(file); 50342f232036SJan Schmidt if (ret) 50352f232036SJan Schmidt return ret; 50362f232036SJan Schmidt 50372f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 50382f232036SJan Schmidt if (IS_ERR(qsa)) { 50392f232036SJan Schmidt ret = PTR_ERR(qsa); 50402f232036SJan Schmidt goto drop_write; 50412f232036SJan Schmidt } 50422f232036SJan Schmidt 50432f232036SJan Schmidt if (qsa->flags) { 50442f232036SJan Schmidt ret = -EINVAL; 50452f232036SJan Schmidt goto out; 50462f232036SJan Schmidt } 50472f232036SJan Schmidt 50480b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 50492f232036SJan Schmidt 50502f232036SJan Schmidt out: 50512f232036SJan Schmidt kfree(qsa); 50522f232036SJan Schmidt drop_write: 50532f232036SJan Schmidt mnt_drop_write_file(file); 50542f232036SJan Schmidt return ret; 50552f232036SJan Schmidt } 50562f232036SJan Schmidt 50572f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 50582f232036SJan Schmidt { 50590b246afaSJeff Mahoney struct inode *inode = file_inode(file); 50600b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 50612f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 50622f232036SJan Schmidt int ret = 0; 50632f232036SJan Schmidt 50642f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 50652f232036SJan Schmidt return -EPERM; 50662f232036SJan Schmidt 50678d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 50682f232036SJan Schmidt if (!qsa) 50692f232036SJan Schmidt return -ENOMEM; 50702f232036SJan Schmidt 50710b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 50722f232036SJan Schmidt qsa->flags = 1; 50730b246afaSJeff Mahoney qsa->progress = fs_info->qgroup_rescan_progress.objectid; 50742f232036SJan Schmidt } 50752f232036SJan Schmidt 50762f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 50772f232036SJan Schmidt ret = -EFAULT; 50782f232036SJan Schmidt 50792f232036SJan Schmidt kfree(qsa); 50802f232036SJan Schmidt return ret; 50812f232036SJan Schmidt } 50822f232036SJan Schmidt 508357254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 508457254b6eSJan Schmidt { 50850b246afaSJeff Mahoney struct inode *inode = file_inode(file); 50860b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 508757254b6eSJan Schmidt 508857254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 508957254b6eSJan Schmidt return -EPERM; 509057254b6eSJan Schmidt 50910b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 509257254b6eSJan Schmidt } 509357254b6eSJan Schmidt 5094abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 5095abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 50968ea05e3aSAlexander Block { 5097496ad9aaSAl Viro struct inode *inode = file_inode(file); 50980b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 50998ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 51008ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 51018ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 5102c2050a45SDeepa Dinamani struct timespec ct = current_time(inode); 51038ea05e3aSAlexander Block int ret = 0; 5104dd5f9615SStefan Behrens int received_uuid_changed; 51058ea05e3aSAlexander Block 5106bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5107bd60ea0fSDavid Sterba return -EPERM; 5108bd60ea0fSDavid Sterba 51098ea05e3aSAlexander Block ret = mnt_want_write_file(file); 51108ea05e3aSAlexander Block if (ret < 0) 51118ea05e3aSAlexander Block return ret; 51128ea05e3aSAlexander Block 51130b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 51148ea05e3aSAlexander Block 51154a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 51168ea05e3aSAlexander Block ret = -EINVAL; 51178ea05e3aSAlexander Block goto out; 51188ea05e3aSAlexander Block } 51198ea05e3aSAlexander Block 51208ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 51218ea05e3aSAlexander Block ret = -EROFS; 51228ea05e3aSAlexander Block goto out; 51238ea05e3aSAlexander Block } 51248ea05e3aSAlexander Block 5125dd5f9615SStefan Behrens /* 5126dd5f9615SStefan Behrens * 1 - root item 5127dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5128dd5f9615SStefan Behrens */ 5129dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 51308ea05e3aSAlexander Block if (IS_ERR(trans)) { 51318ea05e3aSAlexander Block ret = PTR_ERR(trans); 51328ea05e3aSAlexander Block trans = NULL; 51338ea05e3aSAlexander Block goto out; 51348ea05e3aSAlexander Block } 51358ea05e3aSAlexander Block 51368ea05e3aSAlexander Block sa->rtransid = trans->transid; 51378ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 51388ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 51398ea05e3aSAlexander Block 5140dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5141dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5142dd5f9615SStefan Behrens if (received_uuid_changed && 5143dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 51440b246afaSJeff Mahoney btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid, 5145dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5146dd5f9615SStefan Behrens root->root_key.objectid); 51478ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 51488ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 51498ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 51503cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 51513cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 51523cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 51533cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 51548ea05e3aSAlexander Block 51550b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 51568ea05e3aSAlexander Block &root->root_key, &root->root_item); 51578ea05e3aSAlexander Block if (ret < 0) { 51583a45bb20SJeff Mahoney btrfs_end_transaction(trans); 51598ea05e3aSAlexander Block goto out; 5160dd5f9615SStefan Behrens } 5161dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 51620b246afaSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid, 5163dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5164dd5f9615SStefan Behrens root->root_key.objectid); 5165dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 516666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 5167efd38150SNikolay Borisov btrfs_end_transaction(trans); 5168dd5f9615SStefan Behrens goto out; 5169dd5f9615SStefan Behrens } 5170dd5f9615SStefan Behrens } 51713a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5172abccd00fSHugo Mills out: 51730b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5174abccd00fSHugo Mills mnt_drop_write_file(file); 5175abccd00fSHugo Mills return ret; 5176abccd00fSHugo Mills } 5177abccd00fSHugo Mills 5178abccd00fSHugo Mills #ifdef CONFIG_64BIT 5179abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5180abccd00fSHugo Mills void __user *arg) 5181abccd00fSHugo Mills { 5182abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5183abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5184abccd00fSHugo Mills int ret = 0; 5185abccd00fSHugo Mills 5186abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 51877b9ea627SShailendra Verma if (IS_ERR(args32)) 51887b9ea627SShailendra Verma return PTR_ERR(args32); 5189abccd00fSHugo Mills 51908d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 519184dbeb87SDan Carpenter if (!args64) { 519284dbeb87SDan Carpenter ret = -ENOMEM; 5193abccd00fSHugo Mills goto out; 5194abccd00fSHugo Mills } 5195abccd00fSHugo Mills 5196abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5197abccd00fSHugo Mills args64->stransid = args32->stransid; 5198abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5199abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5200abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5201abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5202abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5203abccd00fSHugo Mills args64->flags = args32->flags; 5204abccd00fSHugo Mills 5205abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5206abccd00fSHugo Mills if (ret) 5207abccd00fSHugo Mills goto out; 5208abccd00fSHugo Mills 5209abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5210abccd00fSHugo Mills args32->stransid = args64->stransid; 5211abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5212abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5213abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5214abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5215abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5216abccd00fSHugo Mills args32->flags = args64->flags; 5217abccd00fSHugo Mills 5218abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5219abccd00fSHugo Mills if (ret) 5220abccd00fSHugo Mills ret = -EFAULT; 5221abccd00fSHugo Mills 5222abccd00fSHugo Mills out: 5223abccd00fSHugo Mills kfree(args32); 5224abccd00fSHugo Mills kfree(args64); 5225abccd00fSHugo Mills return ret; 5226abccd00fSHugo Mills } 5227abccd00fSHugo Mills #endif 5228abccd00fSHugo Mills 5229abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5230abccd00fSHugo Mills void __user *arg) 5231abccd00fSHugo Mills { 5232abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5233abccd00fSHugo Mills int ret = 0; 5234abccd00fSHugo Mills 5235abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 52367b9ea627SShailendra Verma if (IS_ERR(sa)) 52377b9ea627SShailendra Verma return PTR_ERR(sa); 5238abccd00fSHugo Mills 5239abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5240abccd00fSHugo Mills 5241abccd00fSHugo Mills if (ret) 5242abccd00fSHugo Mills goto out; 5243abccd00fSHugo Mills 52448ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 52458ea05e3aSAlexander Block if (ret) 52468ea05e3aSAlexander Block ret = -EFAULT; 52478ea05e3aSAlexander Block 52488ea05e3aSAlexander Block out: 52498ea05e3aSAlexander Block kfree(sa); 52508ea05e3aSAlexander Block return ret; 52518ea05e3aSAlexander Block } 52528ea05e3aSAlexander Block 5253867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 5254867ab667Sjeff.liu { 52550b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 5257a1b83ac5SAnand Jain size_t len; 5258867ab667Sjeff.liu int ret; 5259a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5260a1b83ac5SAnand Jain 52610b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 52620b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 52630b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 5264a1b83ac5SAnand Jain 5265a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5266867ab667Sjeff.liu 5267867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 52680b246afaSJeff Mahoney btrfs_warn(fs_info, 52690b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 52700b246afaSJeff Mahoney --len); 5271867ab667Sjeff.liu } 5272867ab667Sjeff.liu 5273867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5274867ab667Sjeff.liu 5275867ab667Sjeff.liu return ret ? -EFAULT : 0; 5276867ab667Sjeff.liu } 5277867ab667Sjeff.liu 5278a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5279a8bfd4abSjeff.liu { 52800b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 52820b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 52830b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 5284a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5285a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5286a8bfd4abSjeff.liu int ret; 5287a8bfd4abSjeff.liu 5288a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5289a8bfd4abSjeff.liu return -EPERM; 5290a8bfd4abSjeff.liu 5291a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5292a8bfd4abSjeff.liu return -EFAULT; 5293a8bfd4abSjeff.liu 5294a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 52950b246afaSJeff Mahoney btrfs_err(fs_info, 52965d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 5297a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5298a8bfd4abSjeff.liu return -EINVAL; 5299a8bfd4abSjeff.liu } 5300a8bfd4abSjeff.liu 5301a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5302a8bfd4abSjeff.liu if (ret) 5303a8bfd4abSjeff.liu return ret; 5304a8bfd4abSjeff.liu 5305a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5306a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5307a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5308a8bfd4abSjeff.liu goto out_unlock; 5309a8bfd4abSjeff.liu } 5310a8bfd4abSjeff.liu 53110b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5312a8bfd4abSjeff.liu strcpy(super_block->label, label); 53130b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 53143a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5315a8bfd4abSjeff.liu 5316a8bfd4abSjeff.liu out_unlock: 5317a8bfd4abSjeff.liu mnt_drop_write_file(file); 5318a8bfd4abSjeff.liu return ret; 5319a8bfd4abSjeff.liu } 5320a8bfd4abSjeff.liu 53212eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 53222eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 53232eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 53242eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 53252eaa055fSJeff Mahoney 5326d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 53272eaa055fSJeff Mahoney { 53284d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 53292eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 53302eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 53312eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 53322eaa055fSJeff Mahoney }; 53332eaa055fSJeff Mahoney 53342eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 53352eaa055fSJeff Mahoney return -EFAULT; 53362eaa055fSJeff Mahoney 53372eaa055fSJeff Mahoney return 0; 53382eaa055fSJeff Mahoney } 53392eaa055fSJeff Mahoney 53402eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 53412eaa055fSJeff Mahoney { 53420b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53430b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53440b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 53452eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 53462eaa055fSJeff Mahoney 53472eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 53482eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 53492eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 53502eaa055fSJeff Mahoney 53512eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 53522eaa055fSJeff Mahoney return -EFAULT; 53532eaa055fSJeff Mahoney 53542eaa055fSJeff Mahoney return 0; 53552eaa055fSJeff Mahoney } 53562eaa055fSJeff Mahoney 53572ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 53583b02a68aSJeff Mahoney enum btrfs_feature_set set, 53592eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 53602eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 53612eaa055fSJeff Mahoney { 53623b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 53633b02a68aSJeff Mahoney char *names; 53642eaa055fSJeff Mahoney u64 disallowed, unsupported; 53652eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 53662eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 53672eaa055fSJeff Mahoney 53682eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 53692eaa055fSJeff Mahoney if (unsupported) { 53703b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 53713b02a68aSJeff Mahoney if (names) { 53720b246afaSJeff Mahoney btrfs_warn(fs_info, 53733b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 53743b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53753b02a68aSJeff Mahoney kfree(names); 53763b02a68aSJeff Mahoney } else 53770b246afaSJeff Mahoney btrfs_warn(fs_info, 53782eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 53792eaa055fSJeff Mahoney type, unsupported); 53802eaa055fSJeff Mahoney return -EOPNOTSUPP; 53812eaa055fSJeff Mahoney } 53822eaa055fSJeff Mahoney 53832eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 53842eaa055fSJeff Mahoney if (disallowed) { 53853b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53863b02a68aSJeff Mahoney if (names) { 53870b246afaSJeff Mahoney btrfs_warn(fs_info, 53883b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 53893b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53903b02a68aSJeff Mahoney kfree(names); 53913b02a68aSJeff Mahoney } else 53920b246afaSJeff Mahoney btrfs_warn(fs_info, 53932eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 53942eaa055fSJeff Mahoney type, disallowed); 53952eaa055fSJeff Mahoney return -EPERM; 53962eaa055fSJeff Mahoney } 53972eaa055fSJeff Mahoney 53982eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 53992eaa055fSJeff Mahoney if (disallowed) { 54003b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 54013b02a68aSJeff Mahoney if (names) { 54020b246afaSJeff Mahoney btrfs_warn(fs_info, 54033b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 54043b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 54053b02a68aSJeff Mahoney kfree(names); 54063b02a68aSJeff Mahoney } else 54070b246afaSJeff Mahoney btrfs_warn(fs_info, 54082eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 54092eaa055fSJeff Mahoney type, disallowed); 54102eaa055fSJeff Mahoney return -EPERM; 54112eaa055fSJeff Mahoney } 54122eaa055fSJeff Mahoney 54132eaa055fSJeff Mahoney return 0; 54142eaa055fSJeff Mahoney } 54152eaa055fSJeff Mahoney 54162ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 54172ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 54182eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 54192eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 54202eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 54212eaa055fSJeff Mahoney 54222eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 54232eaa055fSJeff Mahoney { 54240b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54260b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 54270b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 54282eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 54292eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 54302eaa055fSJeff Mahoney u64 newflags; 54312eaa055fSJeff Mahoney int ret; 54322eaa055fSJeff Mahoney 54332eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 54342eaa055fSJeff Mahoney return -EPERM; 54352eaa055fSJeff Mahoney 54362eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 54372eaa055fSJeff Mahoney return -EFAULT; 54382eaa055fSJeff Mahoney 54392eaa055fSJeff Mahoney /* Nothing to do */ 54402eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 54412eaa055fSJeff Mahoney !flags[0].incompat_flags) 54422eaa055fSJeff Mahoney return 0; 54432eaa055fSJeff Mahoney 54442ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 54452eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 54462eaa055fSJeff Mahoney if (ret) 54472eaa055fSJeff Mahoney return ret; 54482eaa055fSJeff Mahoney 54492ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 54502eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 54512eaa055fSJeff Mahoney if (ret) 54522eaa055fSJeff Mahoney return ret; 54532eaa055fSJeff Mahoney 54542ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 54552eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 54562eaa055fSJeff Mahoney if (ret) 54572eaa055fSJeff Mahoney return ret; 54582eaa055fSJeff Mahoney 54597ab19625SDavid Sterba ret = mnt_want_write_file(file); 54607ab19625SDavid Sterba if (ret) 54617ab19625SDavid Sterba return ret; 54627ab19625SDavid Sterba 54638051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 54647ab19625SDavid Sterba if (IS_ERR(trans)) { 54657ab19625SDavid Sterba ret = PTR_ERR(trans); 54667ab19625SDavid Sterba goto out_drop_write; 54677ab19625SDavid Sterba } 54682eaa055fSJeff Mahoney 54690b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 54702eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 54712eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 54722eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 54732eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 54742eaa055fSJeff Mahoney 54752eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 54762eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 54772eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 54782eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 54792eaa055fSJeff Mahoney 54802eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 54812eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 54822eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 54832eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 54840b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 54852eaa055fSJeff Mahoney 54863a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 54877ab19625SDavid Sterba out_drop_write: 54887ab19625SDavid Sterba mnt_drop_write_file(file); 54897ab19625SDavid Sterba 54907ab19625SDavid Sterba return ret; 54912eaa055fSJeff Mahoney } 54922eaa055fSJeff Mahoney 54932351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 54942351f431SJosef Bacik { 54952351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 54962351f431SJosef Bacik int ret; 54972351f431SJosef Bacik 54982351f431SJosef Bacik if (compat) { 54992351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 55002351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 55012351f431SJosef Bacik 55022351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 55032351f431SJosef Bacik if (ret) 55042351f431SJosef Bacik return -EFAULT; 55052351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 55062351f431SJosef Bacik if (!arg) 55072351f431SJosef Bacik return -ENOMEM; 55082351f431SJosef Bacik arg->send_fd = args32.send_fd; 55092351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 55102351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 55112351f431SJosef Bacik arg->parent_root = args32.parent_root; 55122351f431SJosef Bacik arg->flags = args32.flags; 55132351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 55142351f431SJosef Bacik sizeof(args32.reserved)); 55152351f431SJosef Bacik #else 55162351f431SJosef Bacik return -ENOTTY; 55172351f431SJosef Bacik #endif 55182351f431SJosef Bacik } else { 55192351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 55202351f431SJosef Bacik if (IS_ERR(arg)) 55212351f431SJosef Bacik return PTR_ERR(arg); 55222351f431SJosef Bacik } 55232351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 55242351f431SJosef Bacik kfree(arg); 55252351f431SJosef Bacik return ret; 55262351f431SJosef Bacik } 55272351f431SJosef Bacik 5528f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5529f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5530f46b5a66SChristoph Hellwig { 55310b246afaSJeff Mahoney struct inode *inode = file_inode(file); 55320b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 55330b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 55344bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5535f46b5a66SChristoph Hellwig 5536f46b5a66SChristoph Hellwig switch (cmd) { 55376cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 55386cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 55396cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 55406cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 55416cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 55426cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5543f7039b1dSLi Dongyang case FITRIM: 5544f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5545f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5546fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5547fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5548fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 55493de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5550fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 55516f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 55526f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 555376dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 555476dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 55550caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 55560caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 55570caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 55580caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 55596ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 55606ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5561f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 55621e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 55631e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 55641e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5565f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5566198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5567f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 55682ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5569f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5570da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 55716b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 55726b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5573475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 55742ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5575475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 55762ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5577f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 55789ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5579f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 5580f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 5581f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 5582f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 5583ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5584ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5585cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5586cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5587ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5588ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5589d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5590d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5591d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5592d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5593d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5594d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 55951406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 55962ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 55979b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 55989b199859SFilipe David Borba Manana int ret; 55999b199859SFilipe David Borba Manana 56000b246afaSJeff Mahoney ret = btrfs_start_delalloc_roots(fs_info, 0, -1); 56019b199859SFilipe David Borba Manana if (ret) 56029b199859SFilipe David Borba Manana return ret; 56030b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 56042fad4e83SDavid Sterba /* 56052fad4e83SDavid Sterba * The transaction thread may want to do more work, 560601327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 56072fad4e83SDavid Sterba * processing uncleaned subvols. 56082fad4e83SDavid Sterba */ 56090b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 56109b199859SFilipe David Borba Manana return ret; 56119b199859SFilipe David Borba Manana } 561246204592SSage Weil case BTRFS_IOC_START_SYNC: 56139a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 561446204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 56152ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5616475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5617b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5618475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 56192ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5620475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 56212ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5622c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 56239ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5624837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 56252ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 562619a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 56272ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 56288ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 56298ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5630abccd00fSHugo Mills #ifdef CONFIG_64BIT 5631abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5632abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5633abccd00fSHugo Mills #endif 563431db9f7cSAlexander Block case BTRFS_IOC_SEND: 56352351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 56362351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 56372351f431SJosef Bacik case BTRFS_IOC_SEND_32: 56382351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 56392351f431SJosef Bacik #endif 5640c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 56412ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 56425d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5643905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 56445d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5645905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 56465d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5647905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 56485d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5649905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 56502f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 56512f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 56522f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 56532f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 565457254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 565557254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 56563f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 56572ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 5658867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5659867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5660a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5661a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 56622eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5663d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 56642eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 56652eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 56662eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 56672eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5668f46b5a66SChristoph Hellwig } 5669f46b5a66SChristoph Hellwig 5670f46b5a66SChristoph Hellwig return -ENOTTY; 5671f46b5a66SChristoph Hellwig } 56724c63c245SLuke Dashjr 56734c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 56744c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 56754c63c245SLuke Dashjr { 56762a362249SJeff Mahoney /* 56772a362249SJeff Mahoney * These all access 32-bit values anyway so no further 56782a362249SJeff Mahoney * handling is necessary. 56792a362249SJeff Mahoney */ 56804c63c245SLuke Dashjr switch (cmd) { 56814c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 56824c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 56834c63c245SLuke Dashjr break; 56844c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 56854c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 56864c63c245SLuke Dashjr break; 56874c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 56884c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 56894c63c245SLuke Dashjr break; 56904c63c245SLuke Dashjr } 56914c63c245SLuke Dashjr 56924c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 56934c63c245SLuke Dashjr } 56944c63c245SLuke Dashjr #endif 5695