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> 46ae5e165dSJeff Layton #include <linux/iversion.h> 47f46b5a66SChristoph Hellwig #include "ctree.h" 48f46b5a66SChristoph Hellwig #include "disk-io.h" 49f46b5a66SChristoph Hellwig #include "transaction.h" 50f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 51f46b5a66SChristoph Hellwig #include "print-tree.h" 52f46b5a66SChristoph Hellwig #include "volumes.h" 53925baeddSChris Mason #include "locking.h" 54581bb050SLi Zefan #include "inode-map.h" 55d7728c96SJan Schmidt #include "backref.h" 56606686eeSJosef Bacik #include "rcu-string.h" 5731db9f7cSAlexander Block #include "send.h" 583f6bcfbdSStefan Behrens #include "dev-replace.h" 5963541927SFilipe David Borba Manana #include "props.h" 603b02a68aSJeff Mahoney #include "sysfs.h" 61fcebe456SJosef Bacik #include "qgroup.h" 621ec9a1aeSFilipe Manana #include "tree-log.h" 63ebb8765bSAnand Jain #include "compression.h" 64f46b5a66SChristoph Hellwig 65abccd00fSHugo Mills #ifdef CONFIG_64BIT 66abccd00fSHugo Mills /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI 67abccd00fSHugo Mills * structures are incorrect, as the timespec structure from userspace 68abccd00fSHugo Mills * is 4 bytes too small. We define these alternatives here to teach 69abccd00fSHugo Mills * the kernel about the 32-bit struct packing. 70abccd00fSHugo Mills */ 71abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 { 72abccd00fSHugo Mills __u64 sec; 73abccd00fSHugo Mills __u32 nsec; 74abccd00fSHugo Mills } __attribute__ ((__packed__)); 75abccd00fSHugo Mills 76abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 { 77abccd00fSHugo Mills char uuid[BTRFS_UUID_SIZE]; /* in */ 78abccd00fSHugo Mills __u64 stransid; /* in */ 79abccd00fSHugo Mills __u64 rtransid; /* out */ 80abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 stime; /* in */ 81abccd00fSHugo Mills struct btrfs_ioctl_timespec_32 rtime; /* out */ 82abccd00fSHugo Mills __u64 flags; /* in */ 83abccd00fSHugo Mills __u64 reserved[16]; /* in */ 84abccd00fSHugo Mills } __attribute__ ((__packed__)); 85abccd00fSHugo Mills 86abccd00fSHugo Mills #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ 87abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32) 88abccd00fSHugo Mills #endif 89abccd00fSHugo Mills 902351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 912351f431SJosef Bacik struct btrfs_ioctl_send_args_32 { 922351f431SJosef Bacik __s64 send_fd; /* in */ 932351f431SJosef Bacik __u64 clone_sources_count; /* in */ 942351f431SJosef Bacik compat_uptr_t clone_sources; /* in */ 952351f431SJosef Bacik __u64 parent_root; /* in */ 962351f431SJosef Bacik __u64 flags; /* in */ 972351f431SJosef Bacik __u64 reserved[4]; /* in */ 982351f431SJosef Bacik } __attribute__ ((__packed__)); 992351f431SJosef Bacik 1002351f431SJosef Bacik #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ 1012351f431SJosef Bacik struct btrfs_ioctl_send_args_32) 1022351f431SJosef Bacik #endif 103abccd00fSHugo Mills 104416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 1051c919a5eSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff, 1061c919a5eSMark Fasheh int no_time_update); 107416161dbSMark Fasheh 1086cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 1096cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 1106cbff00fSChristoph Hellwig { 1116cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 1126cbff00fSChristoph Hellwig return flags; 1136cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 1146cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 1156cbff00fSChristoph Hellwig else 1166cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 1176cbff00fSChristoph Hellwig } 118f46b5a66SChristoph Hellwig 1196cbff00fSChristoph Hellwig /* 1206cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 1216cbff00fSChristoph Hellwig */ 1226cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 1236cbff00fSChristoph Hellwig { 1246cbff00fSChristoph Hellwig unsigned int iflags = 0; 1256cbff00fSChristoph Hellwig 1266cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 1276cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 1286cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 1296cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 1306cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 1316cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 1326cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 1336cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 1346cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 1356cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 1366cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 1376cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 138d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 139d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 140d0092bddSLi Zefan 14113f48dc9SSatoru Takeuchi if (flags & BTRFS_INODE_NOCOMPRESS) 142d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 14313f48dc9SSatoru Takeuchi else if (flags & BTRFS_INODE_COMPRESS) 14413f48dc9SSatoru Takeuchi iflags |= FS_COMPR_FL; 1456cbff00fSChristoph Hellwig 1466cbff00fSChristoph Hellwig return iflags; 1476cbff00fSChristoph Hellwig } 1486cbff00fSChristoph Hellwig 1496cbff00fSChristoph Hellwig /* 1506cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1516cbff00fSChristoph Hellwig */ 1526cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1536cbff00fSChristoph Hellwig { 1546cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1553cc79392SFilipe Manana unsigned int new_fl = 0; 1566cbff00fSChristoph Hellwig 1576cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1583cc79392SFilipe Manana new_fl |= S_SYNC; 1596cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1603cc79392SFilipe Manana new_fl |= S_IMMUTABLE; 1616cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1623cc79392SFilipe Manana new_fl |= S_APPEND; 1636cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1643cc79392SFilipe Manana new_fl |= S_NOATIME; 1656cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1663cc79392SFilipe Manana new_fl |= S_DIRSYNC; 1673cc79392SFilipe Manana 1683cc79392SFilipe Manana set_mask_bits(&inode->i_flags, 1693cc79392SFilipe Manana S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, 1703cc79392SFilipe Manana new_fl); 1716cbff00fSChristoph Hellwig } 1726cbff00fSChristoph Hellwig 1736cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1746cbff00fSChristoph Hellwig { 175496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1766cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1776cbff00fSChristoph Hellwig 1786cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1796cbff00fSChristoph Hellwig return -EFAULT; 1806cbff00fSChristoph Hellwig return 0; 1816cbff00fSChristoph Hellwig } 1826cbff00fSChristoph Hellwig 18375e7cb7fSLiu Bo static int check_flags(unsigned int flags) 18475e7cb7fSLiu Bo { 18575e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 18675e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 18775e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 188e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 189e1e8fb6aSLi Zefan FS_NOCOW_FL)) 19075e7cb7fSLiu Bo return -EOPNOTSUPP; 19175e7cb7fSLiu Bo 19275e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 19375e7cb7fSLiu Bo return -EINVAL; 19475e7cb7fSLiu Bo 19575e7cb7fSLiu Bo return 0; 19675e7cb7fSLiu Bo } 19775e7cb7fSLiu Bo 1986cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1996cbff00fSChristoph Hellwig { 200496ad9aaSAl Viro struct inode *inode = file_inode(file); 2010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2026cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 2036cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 2046cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 2056cbff00fSChristoph Hellwig unsigned int flags, oldflags; 2066cbff00fSChristoph Hellwig int ret; 207f062abf0SLi Zefan u64 ip_oldflags; 208f062abf0SLi Zefan unsigned int i_oldflags; 2097e97b8daSDavid Sterba umode_t mode; 2106cbff00fSChristoph Hellwig 211bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 212bd60ea0fSDavid Sterba return -EPERM; 213bd60ea0fSDavid Sterba 214b83cc969SLi Zefan if (btrfs_root_readonly(root)) 215b83cc969SLi Zefan return -EROFS; 216b83cc969SLi Zefan 2176cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 2186cbff00fSChristoph Hellwig return -EFAULT; 2196cbff00fSChristoph Hellwig 22075e7cb7fSLiu Bo ret = check_flags(flags); 22175e7cb7fSLiu Bo if (ret) 22275e7cb7fSLiu Bo return ret; 2236cbff00fSChristoph Hellwig 224e7848683SJan Kara ret = mnt_want_write_file(file); 225e7848683SJan Kara if (ret) 226e7848683SJan Kara return ret; 227e7848683SJan Kara 2285955102cSAl Viro inode_lock(inode); 2296cbff00fSChristoph Hellwig 230f062abf0SLi Zefan ip_oldflags = ip->flags; 231f062abf0SLi Zefan i_oldflags = inode->i_flags; 2327e97b8daSDavid Sterba mode = inode->i_mode; 233f062abf0SLi Zefan 2346cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2356cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2366cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2376cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2386cbff00fSChristoph Hellwig ret = -EPERM; 2396cbff00fSChristoph Hellwig goto out_unlock; 2406cbff00fSChristoph Hellwig } 2416cbff00fSChristoph Hellwig } 2426cbff00fSChristoph Hellwig 2436cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2446cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2456cbff00fSChristoph Hellwig else 2466cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2476cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2486cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2496cbff00fSChristoph Hellwig else 2506cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2516cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2526cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2536cbff00fSChristoph Hellwig else 2546cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2556cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2566cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2576cbff00fSChristoph Hellwig else 2586cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2596cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2606cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2616cbff00fSChristoph Hellwig else 2626cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2636cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2646cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2656cbff00fSChristoph Hellwig else 2666cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2677e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2687e97b8daSDavid Sterba if (S_ISREG(mode)) { 2697e97b8daSDavid Sterba /* 2707e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2717e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2727e97b8daSDavid Sterba * status of the file and will not set it. 2737e97b8daSDavid Sterba */ 2747e97b8daSDavid Sterba if (inode->i_size == 0) 2757e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2767e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2777e97b8daSDavid Sterba } else { 278e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2797e97b8daSDavid Sterba } 2807e97b8daSDavid Sterba } else { 2817e97b8daSDavid Sterba /* 28201327610SNicholas D Steeves * Revert back under same assumptions as above 2837e97b8daSDavid Sterba */ 2847e97b8daSDavid Sterba if (S_ISREG(mode)) { 2857e97b8daSDavid Sterba if (inode->i_size == 0) 2867e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2877e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2887e97b8daSDavid Sterba } else { 289e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2907e97b8daSDavid Sterba } 2917e97b8daSDavid Sterba } 2926cbff00fSChristoph Hellwig 29375e7cb7fSLiu Bo /* 29475e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 29575e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 29675e7cb7fSLiu Bo * things smaller. 29775e7cb7fSLiu Bo */ 29875e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 29975e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 30075e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 30163541927SFilipe David Borba Manana 30263541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 30363541927SFilipe David Borba Manana if (ret && ret != -ENODATA) 30463541927SFilipe David Borba Manana goto out_drop; 30575e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 30663541927SFilipe David Borba Manana const char *comp; 30763541927SFilipe David Borba Manana 30875e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 30975e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 31063541927SFilipe David Borba Manana 31193370509SDavid Sterba comp = btrfs_compress_type2str(fs_info->compress_type); 31293370509SDavid Sterba if (!comp || comp[0] == 0) 31393370509SDavid Sterba comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); 31493370509SDavid Sterba 31563541927SFilipe David Borba Manana ret = btrfs_set_prop(inode, "btrfs.compression", 31663541927SFilipe David Borba Manana comp, strlen(comp), 0); 31763541927SFilipe David Borba Manana if (ret) 31863541927SFilipe David Borba Manana goto out_drop; 31963541927SFilipe David Borba Manana 320ebcb904dSLi Zefan } else { 32178a017a2SFilipe Manana ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); 32278a017a2SFilipe Manana if (ret && ret != -ENODATA) 32378a017a2SFilipe Manana goto out_drop; 324ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 32575e7cb7fSLiu Bo } 3266cbff00fSChristoph Hellwig 3274da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 328f062abf0SLi Zefan if (IS_ERR(trans)) { 329f062abf0SLi Zefan ret = PTR_ERR(trans); 330f062abf0SLi Zefan goto out_drop; 331f062abf0SLi Zefan } 3326cbff00fSChristoph Hellwig 333306424ccSLi Zefan btrfs_update_iflags(inode); 3340c4d2d95SJosef Bacik inode_inc_iversion(inode); 335c2050a45SDeepa Dinamani inode->i_ctime = current_time(inode); 3366cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3376cbff00fSChristoph Hellwig 3383a45bb20SJeff Mahoney btrfs_end_transaction(trans); 339f062abf0SLi Zefan out_drop: 340f062abf0SLi Zefan if (ret) { 341f062abf0SLi Zefan ip->flags = ip_oldflags; 342f062abf0SLi Zefan inode->i_flags = i_oldflags; 343f062abf0SLi Zefan } 3446cbff00fSChristoph Hellwig 3456cbff00fSChristoph Hellwig out_unlock: 3465955102cSAl Viro inode_unlock(inode); 347e7848683SJan Kara mnt_drop_write_file(file); 3482d4e6f6aSliubo return ret; 3496cbff00fSChristoph Hellwig } 3506cbff00fSChristoph Hellwig 3516cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3526cbff00fSChristoph Hellwig { 353496ad9aaSAl Viro struct inode *inode = file_inode(file); 3546cbff00fSChristoph Hellwig 3556cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3566cbff00fSChristoph Hellwig } 357f46b5a66SChristoph Hellwig 358f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 359f7039b1dSLi Dongyang { 3600b246afaSJeff Mahoney struct inode *inode = file_inode(file); 3610b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 362f7039b1dSLi Dongyang struct btrfs_device *device; 363f7039b1dSLi Dongyang struct request_queue *q; 364f7039b1dSLi Dongyang struct fstrim_range range; 365f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 366f7039b1dSLi Dongyang u64 num_devices = 0; 367815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 368f7039b1dSLi Dongyang int ret; 369f7039b1dSLi Dongyang 370f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 371f7039b1dSLi Dongyang return -EPERM; 372f7039b1dSLi Dongyang 3731f78160cSXiao Guangrong rcu_read_lock(); 3741f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3751f78160cSXiao Guangrong dev_list) { 376f7039b1dSLi Dongyang if (!device->bdev) 377f7039b1dSLi Dongyang continue; 378f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 379f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 380f7039b1dSLi Dongyang num_devices++; 38150d0446eSSeraphime Kirkovski minlen = min_t(u64, q->limits.discard_granularity, 382f7039b1dSLi Dongyang minlen); 383f7039b1dSLi Dongyang } 384f7039b1dSLi Dongyang } 3851f78160cSXiao Guangrong rcu_read_unlock(); 386f4c697e6SLukas Czerner 387f7039b1dSLi Dongyang if (!num_devices) 388f7039b1dSLi Dongyang return -EOPNOTSUPP; 389f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 390f7039b1dSLi Dongyang return -EFAULT; 391e515c18bSLukas Czerner if (range.start > total_bytes || 392e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 393f4c697e6SLukas Czerner return -EINVAL; 394f7039b1dSLi Dongyang 395f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 396f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 3972ff7e61eSJeff Mahoney ret = btrfs_trim_fs(fs_info, &range); 398f7039b1dSLi Dongyang if (ret < 0) 399f7039b1dSLi Dongyang return ret; 400f7039b1dSLi Dongyang 401f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 402f7039b1dSLi Dongyang return -EFAULT; 403f7039b1dSLi Dongyang 404f7039b1dSLi Dongyang return 0; 405f7039b1dSLi Dongyang } 406f7039b1dSLi Dongyang 407dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 408dd5f9615SStefan Behrens { 40946e0f66aSChris Mason int i; 41046e0f66aSChris Mason 41146e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 41246e0f66aSChris Mason if (uuid[i]) 41346e0f66aSChris Mason return 0; 41446e0f66aSChris Mason } 41546e0f66aSChris Mason return 1; 416dd5f9615SStefan Behrens } 417dd5f9615SStefan Behrens 418d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 419cb8e7090SChristoph Hellwig struct dentry *dentry, 42052f75f4fSDavid Sterba const char *name, int namelen, 4216f72c7e2SArne Jansen u64 *async_transid, 4228696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 423f46b5a66SChristoph Hellwig { 4240b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 425f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 426f46b5a66SChristoph Hellwig struct btrfs_key key; 42749a3c4d9SDavid Sterba struct btrfs_root_item *root_item; 428f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 429f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 430d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 43176dda93cSYan, Zheng struct btrfs_root *new_root; 432d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 433c2050a45SDeepa Dinamani struct timespec cur_time = current_time(dir); 4345662344bSTsutomu Itoh struct inode *inode; 435f46b5a66SChristoph Hellwig int ret; 436f46b5a66SChristoph Hellwig int err; 437f46b5a66SChristoph Hellwig u64 objectid; 438f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 4393de4586cSChris Mason u64 index = 0; 440d5c12070SMiao Xie u64 qgroup_reserved; 4418ea05e3aSAlexander Block uuid_le new_uuid; 442f46b5a66SChristoph Hellwig 44349a3c4d9SDavid Sterba root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); 44449a3c4d9SDavid Sterba if (!root_item) 44549a3c4d9SDavid Sterba return -ENOMEM; 44649a3c4d9SDavid Sterba 4470b246afaSJeff Mahoney ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); 4482fbe8c8aSAl Viro if (ret) 44949a3c4d9SDavid Sterba goto fail_free; 4506a912213SJosef Bacik 451e09fe2d2SQu Wenruo /* 452e09fe2d2SQu Wenruo * Don't create subvolume whose level is not zero. Or qgroup will be 45301327610SNicholas D Steeves * screwed up since it assumes subvolume qgroup's level to be 0. 454e09fe2d2SQu Wenruo */ 45549a3c4d9SDavid Sterba if (btrfs_qgroup_level(objectid)) { 45649a3c4d9SDavid Sterba ret = -ENOSPC; 45749a3c4d9SDavid Sterba goto fail_free; 45849a3c4d9SDavid Sterba } 459e09fe2d2SQu Wenruo 460d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 4619ed74f2dSJosef Bacik /* 462d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 463d5c12070SMiao Xie * of create_snapshot(). 4649ed74f2dSJosef Bacik */ 465d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 466dd5f9615SStefan Behrens 8, &qgroup_reserved, false); 467d5c12070SMiao Xie if (ret) 46849a3c4d9SDavid Sterba goto fail_free; 469f46b5a66SChristoph Hellwig 470d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 471d5c12070SMiao Xie if (IS_ERR(trans)) { 472d5c12070SMiao Xie ret = PTR_ERR(trans); 4737775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 47449a3c4d9SDavid Sterba goto fail_free; 475d5c12070SMiao Xie } 476d5c12070SMiao Xie trans->block_rsv = &block_rsv; 477d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 478f46b5a66SChristoph Hellwig 4790b246afaSJeff Mahoney ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit); 4806f72c7e2SArne Jansen if (ret) 4816f72c7e2SArne Jansen goto fail; 4826f72c7e2SArne Jansen 4834d75f8a9SDavid Sterba leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); 4848e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4858e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4868e8a1e31SJosef Bacik goto fail; 4878e8a1e31SJosef Bacik } 488f46b5a66SChristoph Hellwig 489b159fa28SDavid Sterba memzero_extent_buffer(leaf, 0, sizeof(struct btrfs_header)); 490f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 491f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4925d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 493f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 494f46b5a66SChristoph Hellwig 4950b246afaSJeff Mahoney write_extent_buffer_fsid(leaf, fs_info->fsid); 4960b246afaSJeff Mahoney write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid); 497f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 498f46b5a66SChristoph Hellwig 49949a3c4d9SDavid Sterba inode_item = &root_item->inode; 5003cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 5013cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 5023cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 503da17066cSJeff Mahoney btrfs_set_stack_inode_nbytes(inode_item, 5040b246afaSJeff Mahoney fs_info->nodesize); 5053cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 506f46b5a66SChristoph Hellwig 50749a3c4d9SDavid Sterba btrfs_set_root_flags(root_item, 0); 50849a3c4d9SDavid Sterba btrfs_set_root_limit(root_item, 0); 5093cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 51008fe4db1SLi Zefan 51149a3c4d9SDavid Sterba btrfs_set_root_bytenr(root_item, leaf->start); 51249a3c4d9SDavid Sterba btrfs_set_root_generation(root_item, trans->transid); 51349a3c4d9SDavid Sterba btrfs_set_root_level(root_item, 0); 51449a3c4d9SDavid Sterba btrfs_set_root_refs(root_item, 1); 51549a3c4d9SDavid Sterba btrfs_set_root_used(root_item, leaf->len); 51649a3c4d9SDavid Sterba btrfs_set_root_last_snapshot(root_item, 0); 517f46b5a66SChristoph Hellwig 51849a3c4d9SDavid Sterba btrfs_set_root_generation_v2(root_item, 51949a3c4d9SDavid Sterba btrfs_root_generation(root_item)); 5208ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 52149a3c4d9SDavid Sterba memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); 52249a3c4d9SDavid Sterba btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); 52349a3c4d9SDavid Sterba btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); 52449a3c4d9SDavid Sterba root_item->ctime = root_item->otime; 52549a3c4d9SDavid Sterba btrfs_set_root_ctransid(root_item, trans->transid); 52649a3c4d9SDavid Sterba btrfs_set_root_otransid(root_item, trans->transid); 527f46b5a66SChristoph Hellwig 528925baeddSChris Mason btrfs_tree_unlock(leaf); 529f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 530f46b5a66SChristoph Hellwig leaf = NULL; 531f46b5a66SChristoph Hellwig 53249a3c4d9SDavid Sterba btrfs_set_root_dirid(root_item, new_dirid); 533f46b5a66SChristoph Hellwig 534f46b5a66SChristoph Hellwig key.objectid = objectid; 5355d4f98a2SYan Zheng key.offset = 0; 536962a298fSDavid Sterba key.type = BTRFS_ROOT_ITEM_KEY; 5370b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, &key, 53849a3c4d9SDavid Sterba root_item); 539f46b5a66SChristoph Hellwig if (ret) 540f46b5a66SChristoph Hellwig goto fail; 541f46b5a66SChristoph Hellwig 54276dda93cSYan, Zheng key.offset = (u64)-1; 5430b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &key); 54479787eaaSJeff Mahoney if (IS_ERR(new_root)) { 54579787eaaSJeff Mahoney ret = PTR_ERR(new_root); 54666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 54779787eaaSJeff Mahoney goto fail; 54879787eaaSJeff Mahoney } 54976dda93cSYan, Zheng 55076dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 55176dda93cSYan, Zheng 55263541927SFilipe David Borba Manana ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); 553ce598979SMark Fasheh if (ret) { 554ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 55566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 556ce598979SMark Fasheh goto fail; 557ce598979SMark Fasheh } 558ce598979SMark Fasheh 559f32e48e9SChandan Rajendra mutex_lock(&new_root->objectid_mutex); 560f32e48e9SChandan Rajendra new_root->highest_objectid = new_dirid; 561f32e48e9SChandan Rajendra mutex_unlock(&new_root->objectid_mutex); 562f32e48e9SChandan Rajendra 563f46b5a66SChristoph Hellwig /* 564f46b5a66SChristoph Hellwig * insert the directory item 565f46b5a66SChristoph Hellwig */ 566877574e2SNikolay Borisov ret = btrfs_set_inode_index(BTRFS_I(dir), &index); 56779787eaaSJeff Mahoney if (ret) { 56866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 56979787eaaSJeff Mahoney goto fail; 57079787eaaSJeff Mahoney } 5713de4586cSChris Mason 5723de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 5738e7611cfSNikolay Borisov name, namelen, BTRFS_I(dir), &key, 5743de4586cSChris Mason BTRFS_FT_DIR, index); 57579787eaaSJeff Mahoney if (ret) { 57666642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 577f46b5a66SChristoph Hellwig goto fail; 57879787eaaSJeff Mahoney } 5790660b5afSChris Mason 5806ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); 58152c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 58252c26179SYan Zheng BUG_ON(ret); 58352c26179SYan Zheng 5840b246afaSJeff Mahoney ret = btrfs_add_root_ref(trans, fs_info, 5854df27c4dSYan, Zheng objectid, root->root_key.objectid, 5864a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(dir)), index, name, namelen); 5870660b5afSChris Mason BUG_ON(ret); 5880660b5afSChris Mason 5890b246afaSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid, 5906bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, objectid); 591dd5f9615SStefan Behrens if (ret) 59266642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 593dd5f9615SStefan Behrens 594f46b5a66SChristoph Hellwig fail: 59549a3c4d9SDavid Sterba kfree(root_item); 596d5c12070SMiao Xie trans->block_rsv = NULL; 597d5c12070SMiao Xie trans->bytes_reserved = 0; 5987775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 599de6e8200SLiu Bo 60072fd032eSSage Weil if (async_transid) { 60172fd032eSSage Weil *async_transid = trans->transid; 6023a45bb20SJeff Mahoney err = btrfs_commit_transaction_async(trans, 1); 60300d71c9cSMiao Xie if (err) 6043a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 60572fd032eSSage Weil } else { 6063a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 60772fd032eSSage Weil } 608f46b5a66SChristoph Hellwig if (err && !ret) 609f46b5a66SChristoph Hellwig ret = err; 6101a65e24bSChris Mason 6115662344bSTsutomu Itoh if (!ret) { 6125662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 613de6e8200SLiu Bo if (IS_ERR(inode)) 614de6e8200SLiu Bo return PTR_ERR(inode); 6155662344bSTsutomu Itoh d_instantiate(dentry, inode); 6165662344bSTsutomu Itoh } 617f46b5a66SChristoph Hellwig return ret; 61849a3c4d9SDavid Sterba 61949a3c4d9SDavid Sterba fail_free: 62049a3c4d9SDavid Sterba kfree(root_item); 62149a3c4d9SDavid Sterba return ret; 622f46b5a66SChristoph Hellwig } 623f46b5a66SChristoph Hellwig 624e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 62561d7e4cbSDavid Sterba struct dentry *dentry, 626e9662f70SMiao Xie u64 *async_transid, bool readonly, 627e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 628f46b5a66SChristoph Hellwig { 6290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 6302e4bfab9SYan, Zheng struct inode *inode; 631f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 632f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 6332e4bfab9SYan, Zheng int ret; 634f46b5a66SChristoph Hellwig 63527cdeb70SMiao Xie if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 636f46b5a66SChristoph Hellwig return -EINVAL; 637f46b5a66SChristoph Hellwig 63823269bf5SDavid Sterba pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); 639a1ee7362SDavid Sterba if (!pending_snapshot) 640a1ee7362SDavid Sterba return -ENOMEM; 641a1ee7362SDavid Sterba 642b0c0ea63SDavid Sterba pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), 64323269bf5SDavid Sterba GFP_KERNEL); 6448546b570SDavid Sterba pending_snapshot->path = btrfs_alloc_path(); 6458546b570SDavid Sterba if (!pending_snapshot->root_item || !pending_snapshot->path) { 646b0c0ea63SDavid Sterba ret = -ENOMEM; 647b0c0ea63SDavid Sterba goto free_pending; 648b0c0ea63SDavid Sterba } 649b0c0ea63SDavid Sterba 650ea14b57fSDavid Sterba atomic_inc(&root->will_be_snapshotted); 6514e857c58SPeter Zijlstra smp_mb__after_atomic(); 65245bac0f3SLiu Bo /* wait for no snapshot writes */ 65345bac0f3SLiu Bo wait_event(root->subv_writers->wait, 65445bac0f3SLiu Bo percpu_counter_sum(&root->subv_writers->counter) == 0); 6558257b2dcSMiao Xie 6566a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 6576a03843dSMiao Xie if (ret) 658a1ee7362SDavid Sterba goto dec_and_free; 6596a03843dSMiao Xie 6606374e57aSChris Mason btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); 6616a03843dSMiao Xie 66266d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 66366d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 664d5c12070SMiao Xie /* 665d5c12070SMiao Xie * 1 - parent dir inode 666d5c12070SMiao Xie * 2 - dir entries 667d5c12070SMiao Xie * 1 - root item 668d5c12070SMiao Xie * 2 - root ref/backref 669d5c12070SMiao Xie * 1 - root of snapshot 670dd5f9615SStefan Behrens * 1 - UUID item 671d5c12070SMiao Xie */ 672d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 673dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 674ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 675ee3441b4SJeff Mahoney false); 676d5c12070SMiao Xie if (ret) 677a1ee7362SDavid Sterba goto dec_and_free; 678d5c12070SMiao Xie 679a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 680a22285a6SYan, Zheng pending_snapshot->root = root; 681b83cc969SLi Zefan pending_snapshot->readonly = readonly; 682e9662f70SMiao Xie pending_snapshot->dir = dir; 6838696c533SMiao Xie pending_snapshot->inherit = inherit; 684a22285a6SYan, Zheng 685d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 686a22285a6SYan, Zheng if (IS_ERR(trans)) { 687a22285a6SYan, Zheng ret = PTR_ERR(trans); 688a22285a6SYan, Zheng goto fail; 689a22285a6SYan, Zheng } 690a22285a6SYan, Zheng 6910b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 692a22285a6SYan, Zheng list_add(&pending_snapshot->list, 693a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 6940b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 69572fd032eSSage Weil if (async_transid) { 69672fd032eSSage Weil *async_transid = trans->transid; 6973a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 1); 69800d71c9cSMiao Xie if (ret) 6993a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 70072fd032eSSage Weil } else { 7013a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 70272fd032eSSage Weil } 703aec8030aSMiao Xie if (ret) 704c37b2b62SJosef Bacik goto fail; 705a22285a6SYan, Zheng 706a22285a6SYan, Zheng ret = pending_snapshot->error; 707f46b5a66SChristoph Hellwig if (ret) 7082e4bfab9SYan, Zheng goto fail; 709f46b5a66SChristoph Hellwig 710d3797308SChris Mason ret = btrfs_orphan_cleanup(pending_snapshot->snap); 711d3797308SChris Mason if (ret) 712d3797308SChris Mason goto fail; 713d3797308SChris Mason 7142b0143b5SDavid Howells inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); 7152e4bfab9SYan, Zheng if (IS_ERR(inode)) { 7162e4bfab9SYan, Zheng ret = PTR_ERR(inode); 7172e4bfab9SYan, Zheng goto fail; 7182e4bfab9SYan, Zheng } 7195662344bSTsutomu Itoh 7202e4bfab9SYan, Zheng d_instantiate(dentry, inode); 7212e4bfab9SYan, Zheng ret = 0; 7222e4bfab9SYan, Zheng fail: 7237775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); 724a1ee7362SDavid Sterba dec_and_free: 725ea14b57fSDavid Sterba if (atomic_dec_and_test(&root->will_be_snapshotted)) 726ea14b57fSDavid Sterba wake_up_atomic_t(&root->will_be_snapshotted); 727b0c0ea63SDavid Sterba free_pending: 728b0c0ea63SDavid Sterba kfree(pending_snapshot->root_item); 7298546b570SDavid Sterba btrfs_free_path(pending_snapshot->path); 730a1ee7362SDavid Sterba kfree(pending_snapshot); 731a1ee7362SDavid Sterba 732f46b5a66SChristoph Hellwig return ret; 733f46b5a66SChristoph Hellwig } 734f46b5a66SChristoph Hellwig 7354260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 7364260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 7374260f7c7SSage Weil * whether the type of victim is right. 7384260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 7394260f7c7SSage Weil * 2. We should have write and exec permissions on dir 7404260f7c7SSage Weil * 3. We can't remove anything from append-only dir 7414260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 7424260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 7434260f7c7SSage Weil * a. be owner of dir, or 7444260f7c7SSage Weil * b. be owner of victim, or 7454260f7c7SSage Weil * c. have CAP_FOWNER capability 74601327610SNicholas D Steeves * 6. If the victim is append-only or immutable we can't do anything with 7474260f7c7SSage Weil * links pointing to it. 7484260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 7494260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 7504260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 7514260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 7524260f7c7SSage Weil * nfs_async_unlink(). 7534260f7c7SSage Weil */ 7544260f7c7SSage Weil 7554260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 7564260f7c7SSage Weil { 7574260f7c7SSage Weil int error; 7584260f7c7SSage Weil 7592b0143b5SDavid Howells if (d_really_is_negative(victim)) 7604260f7c7SSage Weil return -ENOENT; 7614260f7c7SSage Weil 7622b0143b5SDavid Howells BUG_ON(d_inode(victim->d_parent) != dir); 7634fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 7644260f7c7SSage Weil 7654260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 7664260f7c7SSage Weil if (error) 7674260f7c7SSage Weil return error; 7684260f7c7SSage Weil if (IS_APPEND(dir)) 7694260f7c7SSage Weil return -EPERM; 7702b0143b5SDavid Howells if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || 7712b0143b5SDavid Howells IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) 7724260f7c7SSage Weil return -EPERM; 7734260f7c7SSage Weil if (isdir) { 774e36cb0b8SDavid Howells if (!d_is_dir(victim)) 7754260f7c7SSage Weil return -ENOTDIR; 7764260f7c7SSage Weil if (IS_ROOT(victim)) 7774260f7c7SSage Weil return -EBUSY; 778e36cb0b8SDavid Howells } else if (d_is_dir(victim)) 7794260f7c7SSage Weil return -EISDIR; 7804260f7c7SSage Weil if (IS_DEADDIR(dir)) 7814260f7c7SSage Weil return -ENOENT; 7824260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 7834260f7c7SSage Weil return -EBUSY; 7844260f7c7SSage Weil return 0; 7854260f7c7SSage Weil } 7864260f7c7SSage Weil 787cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 788cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 789cb8e7090SChristoph Hellwig { 7902b0143b5SDavid Howells if (d_really_is_positive(child)) 791cb8e7090SChristoph Hellwig return -EEXIST; 792cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 793cb8e7090SChristoph Hellwig return -ENOENT; 794cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 795cb8e7090SChristoph Hellwig } 796cb8e7090SChristoph Hellwig 797cb8e7090SChristoph Hellwig /* 798cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 799cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 800cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 801cb8e7090SChristoph Hellwig */ 80292872094SAl Viro static noinline int btrfs_mksubvol(const struct path *parent, 80352f75f4fSDavid Sterba const char *name, int namelen, 80472fd032eSSage Weil struct btrfs_root *snap_src, 8056f72c7e2SArne Jansen u64 *async_transid, bool readonly, 8068696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 807cb8e7090SChristoph Hellwig { 8082b0143b5SDavid Howells struct inode *dir = d_inode(parent->dentry); 8090b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); 810cb8e7090SChristoph Hellwig struct dentry *dentry; 811cb8e7090SChristoph Hellwig int error; 812cb8e7090SChristoph Hellwig 81300235411SAl Viro error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 81400235411SAl Viro if (error == -EINTR) 81500235411SAl Viro return error; 816cb8e7090SChristoph Hellwig 817cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 818cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 819cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 820cb8e7090SChristoph Hellwig goto out_unlock; 821cb8e7090SChristoph Hellwig 82276dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 823cb8e7090SChristoph Hellwig if (error) 824a874a63eSLiu Bo goto out_dput; 825cb8e7090SChristoph Hellwig 8269c52057cSChris Mason /* 8279c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 8289c52057cSChris Mason * check for them now when we can safely fail 8299c52057cSChris Mason */ 8309c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 8319c52057cSChris Mason dir->i_ino, name, 8329c52057cSChris Mason namelen); 8339c52057cSChris Mason if (error) 8349c52057cSChris Mason goto out_dput; 8359c52057cSChris Mason 8360b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 83776dda93cSYan, Zheng 83876dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 83976dda93cSYan, Zheng goto out_up_read; 84076dda93cSYan, Zheng 8413de4586cSChris Mason if (snap_src) { 84261d7e4cbSDavid Sterba error = create_snapshot(snap_src, dir, dentry, 8436f72c7e2SArne Jansen async_transid, readonly, inherit); 8443de4586cSChris Mason } else { 845d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 846d5c12070SMiao Xie async_transid, inherit); 8473de4586cSChris Mason } 84876dda93cSYan, Zheng if (!error) 84976dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 85076dda93cSYan, Zheng out_up_read: 8510b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 852cb8e7090SChristoph Hellwig out_dput: 853cb8e7090SChristoph Hellwig dput(dentry); 854cb8e7090SChristoph Hellwig out_unlock: 8555955102cSAl Viro inode_unlock(dir); 856cb8e7090SChristoph Hellwig return error; 857cb8e7090SChristoph Hellwig } 858cb8e7090SChristoph Hellwig 8594cb5300bSChris Mason /* 8604cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 8614cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 8624cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 8634cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 8644cb5300bSChris Mason * part of the file 8654cb5300bSChris Mason */ 866aab110abSDavid Sterba static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) 8674cb5300bSChris Mason { 8684cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8694cb5300bSChris Mason struct extent_map *em = NULL; 8704cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 8714cb5300bSChris Mason u64 end; 8724cb5300bSChris Mason 8734cb5300bSChris Mason read_lock(&em_tree->lock); 87409cbfeafSKirill A. Shutemov em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); 8754cb5300bSChris Mason read_unlock(&em_tree->lock); 8764cb5300bSChris Mason 8774cb5300bSChris Mason if (em) { 8784cb5300bSChris Mason end = extent_map_end(em); 8794cb5300bSChris Mason free_extent_map(em); 8804cb5300bSChris Mason if (end - offset > thresh) 8814cb5300bSChris Mason return 0; 8824cb5300bSChris Mason } 8834cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 8844cb5300bSChris Mason thresh /= 2; 8854cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 8864cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 8874cb5300bSChris Mason if (end >= thresh) 8884cb5300bSChris Mason return 0; 8894cb5300bSChris Mason return 1; 8904cb5300bSChris Mason } 8914cb5300bSChris Mason 8924cb5300bSChris Mason /* 8934cb5300bSChris Mason * helper function to walk through a file and find extents 8944cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 8954cb5300bSChris Mason * 8964cb5300bSChris Mason * This is used by the defragging code to find new and small 8974cb5300bSChris Mason * extents 8984cb5300bSChris Mason */ 8994cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 9004cb5300bSChris Mason struct inode *inode, u64 newer_than, 901aab110abSDavid Sterba u64 *off, u32 thresh) 9024cb5300bSChris Mason { 9034cb5300bSChris Mason struct btrfs_path *path; 9044cb5300bSChris Mason struct btrfs_key min_key; 9054cb5300bSChris Mason struct extent_buffer *leaf; 9064cb5300bSChris Mason struct btrfs_file_extent_item *extent; 9074cb5300bSChris Mason int type; 9084cb5300bSChris Mason int ret; 9094a0cc7caSNikolay Borisov u64 ino = btrfs_ino(BTRFS_I(inode)); 9104cb5300bSChris Mason 9114cb5300bSChris Mason path = btrfs_alloc_path(); 9124cb5300bSChris Mason if (!path) 9134cb5300bSChris Mason return -ENOMEM; 9144cb5300bSChris Mason 915a4689d2bSDavid Sterba min_key.objectid = ino; 9164cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 9174cb5300bSChris Mason min_key.offset = *off; 9184cb5300bSChris Mason 9194cb5300bSChris Mason while (1) { 9206174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 9214cb5300bSChris Mason if (ret != 0) 9224cb5300bSChris Mason goto none; 923f094c9bdSFilipe Manana process_slot: 924a4689d2bSDavid Sterba if (min_key.objectid != ino) 9254cb5300bSChris Mason goto none; 9264cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 9274cb5300bSChris Mason goto none; 9284cb5300bSChris Mason 9294cb5300bSChris Mason leaf = path->nodes[0]; 9304cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 9314cb5300bSChris Mason struct btrfs_file_extent_item); 9324cb5300bSChris Mason 9334cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 9344cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 9354cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 9364cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 9374cb5300bSChris Mason *off = min_key.offset; 9384cb5300bSChris Mason btrfs_free_path(path); 9394cb5300bSChris Mason return 0; 9404cb5300bSChris Mason } 9414cb5300bSChris Mason 942f094c9bdSFilipe Manana path->slots[0]++; 943f094c9bdSFilipe Manana if (path->slots[0] < btrfs_header_nritems(leaf)) { 944f094c9bdSFilipe Manana btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); 945f094c9bdSFilipe Manana goto process_slot; 946f094c9bdSFilipe Manana } 947f094c9bdSFilipe Manana 9484cb5300bSChris Mason if (min_key.offset == (u64)-1) 9494cb5300bSChris Mason goto none; 9504cb5300bSChris Mason 9514cb5300bSChris Mason min_key.offset++; 9524cb5300bSChris Mason btrfs_release_path(path); 9534cb5300bSChris Mason } 9544cb5300bSChris Mason none: 9554cb5300bSChris Mason btrfs_free_path(path); 9564cb5300bSChris Mason return -ENOENT; 9574cb5300bSChris Mason } 9584cb5300bSChris Mason 9596c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 96017ce6ef8SLiu Bo { 96117ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 962940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 9636c282eb4SLi Zefan struct extent_map *em; 96409cbfeafSKirill A. Shutemov u64 len = PAGE_SIZE; 965940100a4SChris Mason 966940100a4SChris Mason /* 967940100a4SChris Mason * hopefully we have this extent in the tree already, try without 968940100a4SChris Mason * the full extent lock 969940100a4SChris Mason */ 970940100a4SChris Mason read_lock(&em_tree->lock); 971940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 972940100a4SChris Mason read_unlock(&em_tree->lock); 973940100a4SChris Mason 974940100a4SChris Mason if (!em) { 975308d9800SFilipe Manana struct extent_state *cached = NULL; 976308d9800SFilipe Manana u64 end = start + len - 1; 977308d9800SFilipe Manana 978940100a4SChris Mason /* get the big lock and read metadata off disk */ 979ff13db41SDavid Sterba lock_extent_bits(io_tree, start, end, &cached); 980fc4f21b1SNikolay Borisov em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); 981e43bbe5eSDavid Sterba unlock_extent_cached(io_tree, start, end, &cached); 982940100a4SChris Mason 9836cf8bfbfSDan Carpenter if (IS_ERR(em)) 9846c282eb4SLi Zefan return NULL; 985940100a4SChris Mason } 986940100a4SChris Mason 9876c282eb4SLi Zefan return em; 9886c282eb4SLi Zefan } 9896c282eb4SLi Zefan 9906c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 9916c282eb4SLi Zefan { 9926c282eb4SLi Zefan struct extent_map *next; 9936c282eb4SLi Zefan bool ret = true; 9946c282eb4SLi Zefan 9956c282eb4SLi Zefan /* this is the last extent */ 9966c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 9976c282eb4SLi Zefan return false; 9986c282eb4SLi Zefan 9996c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 1000e9512d72SChris Mason if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 1001e9512d72SChris Mason ret = false; 1002e9512d72SChris Mason else if ((em->block_start + em->block_len == next->block_start) && 1003ee22184bSByongho Lee (em->block_len > SZ_128K && next->block_len > SZ_128K)) 10046c282eb4SLi Zefan ret = false; 10056c282eb4SLi Zefan 10066c282eb4SLi Zefan free_extent_map(next); 10076c282eb4SLi Zefan return ret; 10086c282eb4SLi Zefan } 10096c282eb4SLi Zefan 1010aab110abSDavid Sterba static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, 1011a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 1012a43a2111SAndrew Mahone int compress) 10136c282eb4SLi Zefan { 10146c282eb4SLi Zefan struct extent_map *em; 10156c282eb4SLi Zefan int ret = 1; 10166c282eb4SLi Zefan bool next_mergeable = true; 10174a3560c4SLiu Bo bool prev_mergeable = true; 10186c282eb4SLi Zefan 10196c282eb4SLi Zefan /* 10206c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 10216c282eb4SLi Zefan * defragging it 10226c282eb4SLi Zefan */ 10236c282eb4SLi Zefan if (start < *defrag_end) 10246c282eb4SLi Zefan return 1; 10256c282eb4SLi Zefan 10266c282eb4SLi Zefan *skip = 0; 10276c282eb4SLi Zefan 10286c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 10296c282eb4SLi Zefan if (!em) 10306c282eb4SLi Zefan return 0; 10316c282eb4SLi Zefan 1032940100a4SChris Mason /* this will cover holes, and inline extents */ 103317ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 1034940100a4SChris Mason ret = 0; 103517ce6ef8SLiu Bo goto out; 103617ce6ef8SLiu Bo } 103717ce6ef8SLiu Bo 10384a3560c4SLiu Bo if (!*defrag_end) 10394a3560c4SLiu Bo prev_mergeable = false; 10404a3560c4SLiu Bo 10416c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 1042940100a4SChris Mason /* 10436c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 10446c282eb4SLi Zefan * real extent, don't bother defragging it 1045940100a4SChris Mason */ 1046a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 10474a3560c4SLiu Bo (em->len >= thresh || (!next_mergeable && !prev_mergeable))) 1048940100a4SChris Mason ret = 0; 104917ce6ef8SLiu Bo out: 1050940100a4SChris Mason /* 1051940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 1052940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 1053940100a4SChris Mason * so that the next tiny extent will force a defrag. 1054940100a4SChris Mason * 1055940100a4SChris Mason * The end result of this is that tiny extents before a single big 1056940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 1057940100a4SChris Mason */ 1058940100a4SChris Mason if (ret) { 1059940100a4SChris Mason *defrag_end = extent_map_end(em); 1060940100a4SChris Mason } else { 1061940100a4SChris Mason *last_len = 0; 1062940100a4SChris Mason *skip = extent_map_end(em); 1063940100a4SChris Mason *defrag_end = 0; 1064940100a4SChris Mason } 1065940100a4SChris Mason 1066940100a4SChris Mason free_extent_map(em); 1067940100a4SChris Mason return ret; 1068940100a4SChris Mason } 1069940100a4SChris Mason 10704cb5300bSChris Mason /* 10714cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 10724cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 10734cb5300bSChris Mason * to COW and defrag. 10744cb5300bSChris Mason * 10754cb5300bSChris Mason * It also makes sure the delalloc code has enough 10764cb5300bSChris Mason * dirty data to avoid making new small extents as part 10774cb5300bSChris Mason * of the defrag 10784cb5300bSChris Mason * 10794cb5300bSChris Mason * It's a good idea to start RA on this range 10804cb5300bSChris Mason * before calling this. 10814cb5300bSChris Mason */ 10824cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 10834cb5300bSChris Mason struct page **pages, 10844cb5300bSChris Mason unsigned long start_index, 1085c41570c9SJustin Maggard unsigned long num_pages) 1086f46b5a66SChristoph Hellwig { 10874cb5300bSChris Mason unsigned long file_end; 10884cb5300bSChris Mason u64 isize = i_size_read(inode); 1089f46b5a66SChristoph Hellwig u64 page_start; 1090f46b5a66SChristoph Hellwig u64 page_end; 10911f12bd06SLiu Bo u64 page_cnt; 10924cb5300bSChris Mason int ret; 10934cb5300bSChris Mason int i; 10944cb5300bSChris Mason int i_done; 10954cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 10964cb5300bSChris Mason struct extent_state *cached_state = NULL; 1097600a45e1SMiao Xie struct extent_io_tree *tree; 1098364ecf36SQu Wenruo struct extent_changeset *data_reserved = NULL; 10993b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 11004cb5300bSChris Mason 110109cbfeafSKirill A. Shutemov file_end = (isize - 1) >> PAGE_SHIFT; 11021f12bd06SLiu Bo if (!isize || start_index > file_end) 11031f12bd06SLiu Bo return 0; 11041f12bd06SLiu Bo 11051f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 11064cb5300bSChris Mason 1107364ecf36SQu Wenruo ret = btrfs_delalloc_reserve_space(inode, &data_reserved, 110809cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 110909cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 11104cb5300bSChris Mason if (ret) 11114cb5300bSChris Mason return ret; 11124cb5300bSChris Mason i_done = 0; 1113600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 11144cb5300bSChris Mason 11154cb5300bSChris Mason /* step one, lock all the pages */ 11161f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 11174cb5300bSChris Mason struct page *page; 1118600a45e1SMiao Xie again: 1119a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 11203b16a4e3SJosef Bacik start_index + i, mask); 11214cb5300bSChris Mason if (!page) 11224cb5300bSChris Mason break; 11234cb5300bSChris Mason 1124600a45e1SMiao Xie page_start = page_offset(page); 112509cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 1126600a45e1SMiao Xie while (1) { 1127308d9800SFilipe Manana lock_extent_bits(tree, page_start, page_end, 1128ff13db41SDavid Sterba &cached_state); 1129600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1130600a45e1SMiao Xie page_start); 1131308d9800SFilipe Manana unlock_extent_cached(tree, page_start, page_end, 1132e43bbe5eSDavid Sterba &cached_state); 1133600a45e1SMiao Xie if (!ordered) 1134600a45e1SMiao Xie break; 1135600a45e1SMiao Xie 1136600a45e1SMiao Xie unlock_page(page); 1137600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1138600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1139600a45e1SMiao Xie lock_page(page); 11401f12bd06SLiu Bo /* 11411f12bd06SLiu Bo * we unlocked the page above, so we need check if 11421f12bd06SLiu Bo * it was released or not. 11431f12bd06SLiu Bo */ 11441f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 11451f12bd06SLiu Bo unlock_page(page); 114609cbfeafSKirill A. Shutemov put_page(page); 11471f12bd06SLiu Bo goto again; 11481f12bd06SLiu Bo } 1149600a45e1SMiao Xie } 1150600a45e1SMiao Xie 11514cb5300bSChris Mason if (!PageUptodate(page)) { 11524cb5300bSChris Mason btrfs_readpage(NULL, page); 11534cb5300bSChris Mason lock_page(page); 11544cb5300bSChris Mason if (!PageUptodate(page)) { 11554cb5300bSChris Mason unlock_page(page); 115609cbfeafSKirill A. Shutemov put_page(page); 11574cb5300bSChris Mason ret = -EIO; 11584cb5300bSChris Mason break; 11594cb5300bSChris Mason } 11604cb5300bSChris Mason } 1161600a45e1SMiao Xie 1162600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1163600a45e1SMiao Xie unlock_page(page); 116409cbfeafSKirill A. Shutemov put_page(page); 1165600a45e1SMiao Xie goto again; 1166600a45e1SMiao Xie } 1167600a45e1SMiao Xie 11684cb5300bSChris Mason pages[i] = page; 11694cb5300bSChris Mason i_done++; 11704cb5300bSChris Mason } 11714cb5300bSChris Mason if (!i_done || ret) 11724cb5300bSChris Mason goto out; 11734cb5300bSChris Mason 11741751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 11754cb5300bSChris Mason goto out; 11764cb5300bSChris Mason 11774cb5300bSChris Mason /* 11784cb5300bSChris Mason * so now we have a nice long stream of locked 11794cb5300bSChris Mason * and up to date pages, lets wait on them 11804cb5300bSChris Mason */ 11814cb5300bSChris Mason for (i = 0; i < i_done; i++) 11824cb5300bSChris Mason wait_on_page_writeback(pages[i]); 11834cb5300bSChris Mason 11844cb5300bSChris Mason page_start = page_offset(pages[0]); 118509cbfeafSKirill A. Shutemov page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; 11864cb5300bSChris Mason 11874cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1188ff13db41SDavid Sterba page_start, page_end - 1, &cached_state); 11894cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 11904cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 11919e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 1192ae0f1625SDavid Sterba &cached_state); 11934cb5300bSChris Mason 11941f12bd06SLiu Bo if (i_done != page_cnt) { 11959e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 11969e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 11979e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 1198bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 119909cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 120009cbfeafSKirill A. Shutemov (page_cnt - i_done) << PAGE_SHIFT); 12014cb5300bSChris Mason } 12024cb5300bSChris Mason 12034cb5300bSChris Mason 12049e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 1205018ed4f7SDavid Sterba &cached_state); 12064cb5300bSChris Mason 12074cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 1208e43bbe5eSDavid Sterba page_start, page_end - 1, &cached_state); 12094cb5300bSChris Mason 12104cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12114cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 12124cb5300bSChris Mason ClearPageChecked(pages[i]); 12134cb5300bSChris Mason set_page_extent_mapped(pages[i]); 12144cb5300bSChris Mason set_page_dirty(pages[i]); 12154cb5300bSChris Mason unlock_page(pages[i]); 121609cbfeafSKirill A. Shutemov put_page(pages[i]); 12174cb5300bSChris Mason } 12188b62f87bSJosef Bacik btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1219364ecf36SQu Wenruo extent_changeset_free(data_reserved); 12204cb5300bSChris Mason return i_done; 12214cb5300bSChris Mason out: 12224cb5300bSChris Mason for (i = 0; i < i_done; i++) { 12234cb5300bSChris Mason unlock_page(pages[i]); 122409cbfeafSKirill A. Shutemov put_page(pages[i]); 12254cb5300bSChris Mason } 1226bc42bda2SQu Wenruo btrfs_delalloc_release_space(inode, data_reserved, 122709cbfeafSKirill A. Shutemov start_index << PAGE_SHIFT, 122809cbfeafSKirill A. Shutemov page_cnt << PAGE_SHIFT); 12298b62f87bSJosef Bacik btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT); 1230364ecf36SQu Wenruo extent_changeset_free(data_reserved); 12314cb5300bSChris Mason return ret; 12324cb5300bSChris Mason 12334cb5300bSChris Mason } 12344cb5300bSChris Mason 12354cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 12364cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 12374cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 12384cb5300bSChris Mason { 12390b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 12404cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 12414cb5300bSChris Mason struct file_ra_state *ra = NULL; 12424cb5300bSChris Mason unsigned long last_index; 1243151a31b2SLi Zefan u64 isize = i_size_read(inode); 1244940100a4SChris Mason u64 last_len = 0; 1245940100a4SChris Mason u64 skip = 0; 1246940100a4SChris Mason u64 defrag_end = 0; 12474cb5300bSChris Mason u64 newer_off = range->start; 1248f46b5a66SChristoph Hellwig unsigned long i; 1249008873eaSLi Zefan unsigned long ra_index = 0; 1250f46b5a66SChristoph Hellwig int ret; 12514cb5300bSChris Mason int defrag_count = 0; 12521a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 1253aab110abSDavid Sterba u32 extent_thresh = range->extent_thresh; 125409cbfeafSKirill A. Shutemov unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; 1255c41570c9SJustin Maggard unsigned long cluster = max_cluster; 1256ee22184bSByongho Lee u64 new_align = ~((u64)SZ_128K - 1); 12574cb5300bSChris Mason struct page **pages = NULL; 12581e2ef46dSDavid Sterba bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; 12594cb5300bSChris Mason 12600abd5b17SLiu Bo if (isize == 0) 12610abd5b17SLiu Bo return 0; 12620abd5b17SLiu Bo 12630abd5b17SLiu Bo if (range->start >= isize) 12640abd5b17SLiu Bo return -EINVAL; 12651a419d85SLi Zefan 12661e2ef46dSDavid Sterba if (do_compress) { 12671a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 12681a419d85SLi Zefan return -EINVAL; 12691a419d85SLi Zefan if (range->compress_type) 12701a419d85SLi Zefan compress_type = range->compress_type; 12711a419d85SLi Zefan } 1272f46b5a66SChristoph Hellwig 12730abd5b17SLiu Bo if (extent_thresh == 0) 1274ee22184bSByongho Lee extent_thresh = SZ_256K; 1275f46b5a66SChristoph Hellwig 12764cb5300bSChris Mason /* 12770a52d108SDavid Sterba * If we were not given a file, allocate a readahead context. As 12780a52d108SDavid Sterba * readahead is just an optimization, defrag will work without it so 12790a52d108SDavid Sterba * we don't error out. 12804cb5300bSChris Mason */ 12814cb5300bSChris Mason if (!file) { 128263e727ecSDavid Sterba ra = kzalloc(sizeof(*ra), GFP_KERNEL); 12830a52d108SDavid Sterba if (ra) 12844cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 12854cb5300bSChris Mason } else { 12864cb5300bSChris Mason ra = &file->f_ra; 12874cb5300bSChris Mason } 12884cb5300bSChris Mason 128963e727ecSDavid Sterba pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); 12904cb5300bSChris Mason if (!pages) { 12914cb5300bSChris Mason ret = -ENOMEM; 12924cb5300bSChris Mason goto out_ra; 12934cb5300bSChris Mason } 12944cb5300bSChris Mason 12954cb5300bSChris Mason /* find the last page to defrag */ 12961e701a32SChris Mason if (range->start + range->len > range->start) { 1297151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 129809cbfeafSKirill A. Shutemov range->start + range->len - 1) >> PAGE_SHIFT; 12991e701a32SChris Mason } else { 130009cbfeafSKirill A. Shutemov last_index = (isize - 1) >> PAGE_SHIFT; 13011e701a32SChris Mason } 13021e701a32SChris Mason 13034cb5300bSChris Mason if (newer_than) { 13044cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 1305ee22184bSByongho Lee &newer_off, SZ_64K); 13064cb5300bSChris Mason if (!ret) { 13074cb5300bSChris Mason range->start = newer_off; 13084cb5300bSChris Mason /* 13094cb5300bSChris Mason * we always align our defrag to help keep 13104cb5300bSChris Mason * the extents in the file evenly spaced 13114cb5300bSChris Mason */ 131209cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 13134cb5300bSChris Mason } else 13144cb5300bSChris Mason goto out_ra; 13154cb5300bSChris Mason } else { 131609cbfeafSKirill A. Shutemov i = range->start >> PAGE_SHIFT; 13174cb5300bSChris Mason } 13184cb5300bSChris Mason if (!max_to_defrag) 1319070034bdSchandan max_to_defrag = last_index - i + 1; 13204cb5300bSChris Mason 13212a0f7f57SLi Zefan /* 13222a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 13232a0f7f57SLi Zefan * written sequentially. 13242a0f7f57SLi Zefan */ 13252a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 13262a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 13272a0f7f57SLi Zefan 1328f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 132909cbfeafSKirill A. Shutemov (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { 13304cb5300bSChris Mason /* 13314cb5300bSChris Mason * make sure we stop running if someone unmounts 13324cb5300bSChris Mason * the FS 13334cb5300bSChris Mason */ 13341751e8a6SLinus Torvalds if (!(inode->i_sb->s_flags & SB_ACTIVE)) 13354cb5300bSChris Mason break; 13364cb5300bSChris Mason 13370b246afaSJeff Mahoney if (btrfs_defrag_cancelled(fs_info)) { 13380b246afaSJeff Mahoney btrfs_debug(fs_info, "defrag_file cancelled"); 1339210549ebSDavid Sterba ret = -EAGAIN; 1340210549ebSDavid Sterba break; 1341210549ebSDavid Sterba } 1342210549ebSDavid Sterba 134309cbfeafSKirill A. Shutemov if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, 13446c282eb4SLi Zefan extent_thresh, &last_len, &skip, 13451e2ef46dSDavid Sterba &defrag_end, do_compress)){ 1346940100a4SChris Mason unsigned long next; 1347940100a4SChris Mason /* 1348940100a4SChris Mason * the should_defrag function tells us how much to skip 1349940100a4SChris Mason * bump our counter by the suggested amount 1350940100a4SChris Mason */ 135109cbfeafSKirill A. Shutemov next = DIV_ROUND_UP(skip, PAGE_SIZE); 1352940100a4SChris Mason i = max(i + 1, next); 1353940100a4SChris Mason continue; 1354940100a4SChris Mason } 1355008873eaSLi Zefan 1356008873eaSLi Zefan if (!newer_than) { 135709cbfeafSKirill A. Shutemov cluster = (PAGE_ALIGN(defrag_end) >> 135809cbfeafSKirill A. Shutemov PAGE_SHIFT) - i; 1359008873eaSLi Zefan cluster = min(cluster, max_cluster); 1360008873eaSLi Zefan } else { 1361008873eaSLi Zefan cluster = max_cluster; 1362008873eaSLi Zefan } 1363008873eaSLi Zefan 1364008873eaSLi Zefan if (i + cluster > ra_index) { 1365008873eaSLi Zefan ra_index = max(i, ra_index); 13660a52d108SDavid Sterba if (ra) 1367d3c0bab5SDavid Sterba page_cache_sync_readahead(inode->i_mapping, ra, 1368d3c0bab5SDavid Sterba file, ra_index, cluster); 1369e4826a5bSchandan ra_index += cluster; 1370008873eaSLi Zefan } 13714cb5300bSChris Mason 13725955102cSAl Viro inode_lock(inode); 13731e2ef46dSDavid Sterba if (do_compress) 1374eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = compress_type; 1375008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1376ecb8bea8SLiu Bo if (ret < 0) { 13775955102cSAl Viro inode_unlock(inode); 13784cb5300bSChris Mason goto out_ra; 1379ecb8bea8SLiu Bo } 13804cb5300bSChris Mason 13814cb5300bSChris Mason defrag_count += ret; 1382d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 13835955102cSAl Viro inode_unlock(inode); 13844cb5300bSChris Mason 13854cb5300bSChris Mason if (newer_than) { 13864cb5300bSChris Mason if (newer_off == (u64)-1) 13874cb5300bSChris Mason break; 13884cb5300bSChris Mason 1389e1f041e1SLiu Bo if (ret > 0) 1390e1f041e1SLiu Bo i += ret; 1391e1f041e1SLiu Bo 13924cb5300bSChris Mason newer_off = max(newer_off + 1, 139309cbfeafSKirill A. Shutemov (u64)i << PAGE_SHIFT); 13944cb5300bSChris Mason 1395ee22184bSByongho Lee ret = find_new_extents(root, inode, newer_than, 1396ee22184bSByongho Lee &newer_off, SZ_64K); 13974cb5300bSChris Mason if (!ret) { 13984cb5300bSChris Mason range->start = newer_off; 139909cbfeafSKirill A. Shutemov i = (newer_off & new_align) >> PAGE_SHIFT; 14004cb5300bSChris Mason } else { 14014cb5300bSChris Mason break; 1402940100a4SChris Mason } 14034cb5300bSChris Mason } else { 1404008873eaSLi Zefan if (ret > 0) { 1405cbcc8326SLi Zefan i += ret; 140609cbfeafSKirill A. Shutemov last_len += ret << PAGE_SHIFT; 1407008873eaSLi Zefan } else { 1408940100a4SChris Mason i++; 1409008873eaSLi Zefan last_len = 0; 1410008873eaSLi Zefan } 1411f46b5a66SChristoph Hellwig } 14124cb5300bSChris Mason } 1413f46b5a66SChristoph Hellwig 1414dec8ef90SFilipe Manana if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { 14151e701a32SChris Mason filemap_flush(inode->i_mapping); 1416dec8ef90SFilipe Manana if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, 1417dec8ef90SFilipe Manana &BTRFS_I(inode)->runtime_flags)) 1418dec8ef90SFilipe Manana filemap_flush(inode->i_mapping); 1419dec8ef90SFilipe Manana } 14201e701a32SChris Mason 14211a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 14220b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); 14235c1aab1dSNick Terrell } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { 14245c1aab1dSNick Terrell btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); 14251a419d85SLi Zefan } 14261a419d85SLi Zefan 142760ccf82fSDiego Calleja ret = defrag_count; 1428940100a4SChris Mason 14294cb5300bSChris Mason out_ra: 14301e2ef46dSDavid Sterba if (do_compress) { 14315955102cSAl Viro inode_lock(inode); 1432eec63c65SDavid Sterba BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; 14335955102cSAl Viro inode_unlock(inode); 1434633085c7SFilipe David Borba Manana } 14354cb5300bSChris Mason if (!file) 14364cb5300bSChris Mason kfree(ra); 14374cb5300bSChris Mason kfree(pages); 1438940100a4SChris Mason return ret; 1439f46b5a66SChristoph Hellwig } 1440f46b5a66SChristoph Hellwig 1441198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 144276dda93cSYan, Zheng void __user *arg) 1443f46b5a66SChristoph Hellwig { 14440b246afaSJeff Mahoney struct inode *inode = file_inode(file); 14450b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 1446f46b5a66SChristoph Hellwig u64 new_size; 1447f46b5a66SChristoph Hellwig u64 old_size; 1448f46b5a66SChristoph Hellwig u64 devid = 1; 14490b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 1450f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1451f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1452f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1453f46b5a66SChristoph Hellwig char *sizestr; 14549a40f122SGui Hecheng char *retptr; 1455f46b5a66SChristoph Hellwig char *devstr = NULL; 1456f46b5a66SChristoph Hellwig int ret = 0; 1457f46b5a66SChristoph Hellwig int mod = 0; 1458f46b5a66SChristoph Hellwig 1459e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1460e441d54dSChris Mason return -EPERM; 1461e441d54dSChris Mason 1462198605a8SMiao Xie ret = mnt_want_write_file(file); 1463198605a8SMiao Xie if (ret) 1464198605a8SMiao Xie return ret; 1465198605a8SMiao Xie 1466171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 146797547676SMiao Xie mnt_drop_write_file(file); 1468e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1469c9e9f97bSIlya Dryomov } 1470c9e9f97bSIlya Dryomov 14710b246afaSJeff Mahoney mutex_lock(&fs_info->volume_mutex); 1472dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1473c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1474c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1475c9e9f97bSIlya Dryomov goto out; 1476c9e9f97bSIlya Dryomov } 14775516e595SMark Fasheh 14785516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1479f46b5a66SChristoph Hellwig 1480f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1481f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1482f46b5a66SChristoph Hellwig if (devstr) { 1483f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1484f46b5a66SChristoph Hellwig *devstr = '\0'; 1485f46b5a66SChristoph Hellwig devstr = vol_args->name; 148658dfae63SZhangZhen ret = kstrtoull(devstr, 10, &devid); 148758dfae63SZhangZhen if (ret) 148858dfae63SZhangZhen goto out_free; 1489dfd79829SMiao Xie if (!devid) { 1490dfd79829SMiao Xie ret = -EINVAL; 1491dfd79829SMiao Xie goto out_free; 1492dfd79829SMiao Xie } 14930b246afaSJeff Mahoney btrfs_info(fs_info, "resizing devid %llu", devid); 1494f46b5a66SChristoph Hellwig } 1495dba60f3fSMiao Xie 14960b246afaSJeff Mahoney device = btrfs_find_device(fs_info, devid, NULL, NULL); 1497f46b5a66SChristoph Hellwig if (!device) { 14980b246afaSJeff Mahoney btrfs_info(fs_info, "resizer unable to find device %llu", 1499c1c9ff7cSGeert Uytterhoeven devid); 1500dfd79829SMiao Xie ret = -ENODEV; 1501c9e9f97bSIlya Dryomov goto out_free; 1502f46b5a66SChristoph Hellwig } 1503dba60f3fSMiao Xie 1504ebbede42SAnand Jain if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { 15050b246afaSJeff Mahoney btrfs_info(fs_info, 1506efe120a0SFrank Holton "resizer unable to apply on readonly device %llu", 1507c1c9ff7cSGeert Uytterhoeven devid); 1508dfd79829SMiao Xie ret = -EPERM; 15094e42ae1bSLiu Bo goto out_free; 15104e42ae1bSLiu Bo } 15114e42ae1bSLiu Bo 1512f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1513f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1514f46b5a66SChristoph Hellwig else { 1515f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1516f46b5a66SChristoph Hellwig mod = -1; 1517f46b5a66SChristoph Hellwig sizestr++; 1518f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1519f46b5a66SChristoph Hellwig mod = 1; 1520f46b5a66SChristoph Hellwig sizestr++; 1521f46b5a66SChristoph Hellwig } 15229a40f122SGui Hecheng new_size = memparse(sizestr, &retptr); 15239a40f122SGui Hecheng if (*retptr != '\0' || new_size == 0) { 1524f46b5a66SChristoph Hellwig ret = -EINVAL; 1525c9e9f97bSIlya Dryomov goto out_free; 1526f46b5a66SChristoph Hellwig } 1527f46b5a66SChristoph Hellwig } 1528f46b5a66SChristoph Hellwig 1529401e29c1SAnand Jain if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { 1530dfd79829SMiao Xie ret = -EPERM; 153163a212abSStefan Behrens goto out_free; 153263a212abSStefan Behrens } 153363a212abSStefan Behrens 15347cc8e58dSMiao Xie old_size = btrfs_device_get_total_bytes(device); 1535f46b5a66SChristoph Hellwig 1536f46b5a66SChristoph Hellwig if (mod < 0) { 1537f46b5a66SChristoph Hellwig if (new_size > old_size) { 1538f46b5a66SChristoph Hellwig ret = -EINVAL; 1539c9e9f97bSIlya Dryomov goto out_free; 1540f46b5a66SChristoph Hellwig } 1541f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1542f46b5a66SChristoph Hellwig } else if (mod > 0) { 1543eb8052e0SWenliang Fan if (new_size > ULLONG_MAX - old_size) { 1544902c68a4SGui Hecheng ret = -ERANGE; 1545eb8052e0SWenliang Fan goto out_free; 1546eb8052e0SWenliang Fan } 1547f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1548f46b5a66SChristoph Hellwig } 1549f46b5a66SChristoph Hellwig 1550ee22184bSByongho Lee if (new_size < SZ_256M) { 1551f46b5a66SChristoph Hellwig ret = -EINVAL; 1552c9e9f97bSIlya Dryomov goto out_free; 1553f46b5a66SChristoph Hellwig } 1554f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1555f46b5a66SChristoph Hellwig ret = -EFBIG; 1556c9e9f97bSIlya Dryomov goto out_free; 1557f46b5a66SChristoph Hellwig } 1558f46b5a66SChristoph Hellwig 155947f08b96SNikolay Borisov new_size = round_down(new_size, fs_info->sectorsize); 1560f46b5a66SChristoph Hellwig 15610b246afaSJeff Mahoney btrfs_info_in_rcu(fs_info, "new size for %s is %llu", 1562c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1563f46b5a66SChristoph Hellwig 1564f46b5a66SChristoph Hellwig if (new_size > old_size) { 1565a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 156698d5dc13STsutomu Itoh if (IS_ERR(trans)) { 156798d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1568c9e9f97bSIlya Dryomov goto out_free; 156998d5dc13STsutomu Itoh } 1570f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 15713a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 1572ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1573f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 15740253f40eSjeff.liu } /* equal, nothing need to do */ 1575f46b5a66SChristoph Hellwig 1576c9e9f97bSIlya Dryomov out_free: 1577f46b5a66SChristoph Hellwig kfree(vol_args); 1578c9e9f97bSIlya Dryomov out: 15790b246afaSJeff Mahoney mutex_unlock(&fs_info->volume_mutex); 1580171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 158118f39c41SIlya Dryomov mnt_drop_write_file(file); 1582f46b5a66SChristoph Hellwig return ret; 1583f46b5a66SChristoph Hellwig } 1584f46b5a66SChristoph Hellwig 158572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 158652f75f4fSDavid Sterba const char *name, unsigned long fd, int subvol, 15876f72c7e2SArne Jansen u64 *transid, bool readonly, 15888696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1589f46b5a66SChristoph Hellwig { 1590f46b5a66SChristoph Hellwig int namelen; 15913de4586cSChris Mason int ret = 0; 1592f46b5a66SChristoph Hellwig 1593325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1594325c50e3SJeff Mahoney return -ENOTDIR; 1595325c50e3SJeff Mahoney 1596a874a63eSLiu Bo ret = mnt_want_write_file(file); 1597a874a63eSLiu Bo if (ret) 1598a874a63eSLiu Bo goto out; 1599a874a63eSLiu Bo 160072fd032eSSage Weil namelen = strlen(name); 160172fd032eSSage Weil if (strchr(name, '/')) { 1602f46b5a66SChristoph Hellwig ret = -EINVAL; 1603a874a63eSLiu Bo goto out_drop_write; 1604f46b5a66SChristoph Hellwig } 1605f46b5a66SChristoph Hellwig 160616780cabSChris Mason if (name[0] == '.' && 160716780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 160816780cabSChris Mason ret = -EEXIST; 1609a874a63eSLiu Bo goto out_drop_write; 161016780cabSChris Mason } 161116780cabSChris Mason 16123de4586cSChris Mason if (subvol) { 161372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 16146f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1615cb8e7090SChristoph Hellwig } else { 16162903ff01SAl Viro struct fd src = fdget(fd); 16173de4586cSChris Mason struct inode *src_inode; 16182903ff01SAl Viro if (!src.file) { 16193de4586cSChris Mason ret = -EINVAL; 1620a874a63eSLiu Bo goto out_drop_write; 16213de4586cSChris Mason } 16223de4586cSChris Mason 1623496ad9aaSAl Viro src_inode = file_inode(src.file); 1624496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1625c79b4713SJosef Bacik btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, 1626efe120a0SFrank Holton "Snapshot src from another FS"); 162723ad5b17SKusanagi Kouichi ret = -EXDEV; 1628d0242061SDavid Sterba } else if (!inode_owner_or_capable(src_inode)) { 1629d0242061SDavid Sterba /* 1630d0242061SDavid Sterba * Subvolume creation is not restricted, but snapshots 1631d0242061SDavid Sterba * are limited to own subvolumes only 1632d0242061SDavid Sterba */ 1633d0242061SDavid Sterba ret = -EPERM; 1634ecd18815SAl Viro } else { 163572fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 163672fd032eSSage Weil BTRFS_I(src_inode)->root, 16376f72c7e2SArne Jansen transid, readonly, inherit); 1638ecd18815SAl Viro } 16392903ff01SAl Viro fdput(src); 1640cb8e7090SChristoph Hellwig } 1641a874a63eSLiu Bo out_drop_write: 1642a874a63eSLiu Bo mnt_drop_write_file(file); 1643f46b5a66SChristoph Hellwig out: 164472fd032eSSage Weil return ret; 164572fd032eSSage Weil } 164672fd032eSSage Weil 164772fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1648fa0d2b9bSLi Zefan void __user *arg, int subvol) 164972fd032eSSage Weil { 1650fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 165172fd032eSSage Weil int ret; 165272fd032eSSage Weil 1653325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1654325c50e3SJeff Mahoney return -ENOTDIR; 1655325c50e3SJeff Mahoney 1656fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1657fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1658fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1659fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1660fa0d2b9bSLi Zefan 1661fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1662b83cc969SLi Zefan vol_args->fd, subvol, 16636f72c7e2SArne Jansen NULL, false, NULL); 1664fa0d2b9bSLi Zefan 1665fa0d2b9bSLi Zefan kfree(vol_args); 1666fa0d2b9bSLi Zefan return ret; 1667fa0d2b9bSLi Zefan } 1668fa0d2b9bSLi Zefan 1669fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1670fa0d2b9bSLi Zefan void __user *arg, int subvol) 1671fa0d2b9bSLi Zefan { 1672fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1673fa0d2b9bSLi Zefan int ret; 1674fdfb1e4fSLi Zefan u64 transid = 0; 1675fdfb1e4fSLi Zefan u64 *ptr = NULL; 1676b83cc969SLi Zefan bool readonly = false; 16776f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 167872fd032eSSage Weil 1679325c50e3SJeff Mahoney if (!S_ISDIR(file_inode(file)->i_mode)) 1680325c50e3SJeff Mahoney return -ENOTDIR; 1681325c50e3SJeff Mahoney 1682fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1683fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1684fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1685fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1686fdfb1e4fSLi Zefan 1687b83cc969SLi Zefan if (vol_args->flags & 16886f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 16896f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1690b83cc969SLi Zefan ret = -EOPNOTSUPP; 1691c47ca32dSDan Carpenter goto free_args; 1692fdfb1e4fSLi Zefan } 1693fdfb1e4fSLi Zefan 1694fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1695fdfb1e4fSLi Zefan ptr = &transid; 1696b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1697b83cc969SLi Zefan readonly = true; 16986f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 169909cbfeafSKirill A. Shutemov if (vol_args->size > PAGE_SIZE) { 17006f72c7e2SArne Jansen ret = -EINVAL; 1701c47ca32dSDan Carpenter goto free_args; 17026f72c7e2SArne Jansen } 17036f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 17046f72c7e2SArne Jansen if (IS_ERR(inherit)) { 17056f72c7e2SArne Jansen ret = PTR_ERR(inherit); 1706c47ca32dSDan Carpenter goto free_args; 17076f72c7e2SArne Jansen } 17086f72c7e2SArne Jansen } 170975eaa0e2SSage Weil 1710fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 17116f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 17128696c533SMiao Xie readonly, inherit); 1713c47ca32dSDan Carpenter if (ret) 1714c47ca32dSDan Carpenter goto free_inherit; 171575eaa0e2SSage Weil 1716c47ca32dSDan Carpenter if (ptr && copy_to_user(arg + 1717fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1718c47ca32dSDan Carpenter transid), 1719c47ca32dSDan Carpenter ptr, sizeof(*ptr))) 172075eaa0e2SSage Weil ret = -EFAULT; 1721c47ca32dSDan Carpenter 1722c47ca32dSDan Carpenter free_inherit: 17236f72c7e2SArne Jansen kfree(inherit); 1724c47ca32dSDan Carpenter free_args: 1725c47ca32dSDan Carpenter kfree(vol_args); 1726f46b5a66SChristoph Hellwig return ret; 1727f46b5a66SChristoph Hellwig } 1728f46b5a66SChristoph Hellwig 17290caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 17300caa102dSLi Zefan void __user *arg) 17310caa102dSLi Zefan { 1732496ad9aaSAl Viro struct inode *inode = file_inode(file); 17330b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 17340caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17350caa102dSLi Zefan int ret = 0; 17360caa102dSLi Zefan u64 flags = 0; 17370caa102dSLi Zefan 17384a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) 17390caa102dSLi Zefan return -EINVAL; 17400caa102dSLi Zefan 17410b246afaSJeff Mahoney down_read(&fs_info->subvol_sem); 17420caa102dSLi Zefan if (btrfs_root_readonly(root)) 17430caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 17440b246afaSJeff Mahoney up_read(&fs_info->subvol_sem); 17450caa102dSLi Zefan 17460caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 17470caa102dSLi Zefan ret = -EFAULT; 17480caa102dSLi Zefan 17490caa102dSLi Zefan return ret; 17500caa102dSLi Zefan } 17510caa102dSLi Zefan 17520caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 17530caa102dSLi Zefan void __user *arg) 17540caa102dSLi Zefan { 1755496ad9aaSAl Viro struct inode *inode = file_inode(file); 17560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 17570caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 17580caa102dSLi Zefan struct btrfs_trans_handle *trans; 17590caa102dSLi Zefan u64 root_flags; 17600caa102dSLi Zefan u64 flags; 17610caa102dSLi Zefan int ret = 0; 17620caa102dSLi Zefan 1763bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 1764bd60ea0fSDavid Sterba return -EPERM; 1765bd60ea0fSDavid Sterba 1766b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1767b9ca0664SLiu Bo if (ret) 1768b9ca0664SLiu Bo goto out; 17690caa102dSLi Zefan 17704a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 1771b9ca0664SLiu Bo ret = -EINVAL; 1772b9ca0664SLiu Bo goto out_drop_write; 1773b9ca0664SLiu Bo } 17740caa102dSLi Zefan 1775b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1776b9ca0664SLiu Bo ret = -EFAULT; 1777b9ca0664SLiu Bo goto out_drop_write; 1778b9ca0664SLiu Bo } 17790caa102dSLi Zefan 1780b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1781b9ca0664SLiu Bo ret = -EINVAL; 1782b9ca0664SLiu Bo goto out_drop_write; 1783b9ca0664SLiu Bo } 17840caa102dSLi Zefan 1785b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1786b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1787b9ca0664SLiu Bo goto out_drop_write; 1788b9ca0664SLiu Bo } 17890caa102dSLi Zefan 17900b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 17910caa102dSLi Zefan 17920caa102dSLi Zefan /* nothing to do */ 17930caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1794b9ca0664SLiu Bo goto out_drop_sem; 17950caa102dSLi Zefan 17960caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 17972c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 17980caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 17990caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 18002c686537SDavid Sterba } else { 18012c686537SDavid Sterba /* 18022c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 18032c686537SDavid Sterba * send 18042c686537SDavid Sterba */ 18052c686537SDavid Sterba spin_lock(&root->root_item_lock); 18062c686537SDavid Sterba if (root->send_in_progress == 0) { 18070caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 18080caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 18092c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18102c686537SDavid Sterba } else { 18112c686537SDavid Sterba spin_unlock(&root->root_item_lock); 18120b246afaSJeff Mahoney btrfs_warn(fs_info, 18132c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 18142c686537SDavid Sterba root->root_key.objectid); 18152c686537SDavid Sterba ret = -EPERM; 18162c686537SDavid Sterba goto out_drop_sem; 18172c686537SDavid Sterba } 18182c686537SDavid Sterba } 18190caa102dSLi Zefan 18200caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 18210caa102dSLi Zefan if (IS_ERR(trans)) { 18220caa102dSLi Zefan ret = PTR_ERR(trans); 18230caa102dSLi Zefan goto out_reset; 18240caa102dSLi Zefan } 18250caa102dSLi Zefan 18260b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 18270caa102dSLi Zefan &root->root_key, &root->root_item); 18289417ebc8SNikolay Borisov if (ret < 0) { 18299417ebc8SNikolay Borisov btrfs_end_transaction(trans); 18309417ebc8SNikolay Borisov goto out_reset; 18319417ebc8SNikolay Borisov } 18320caa102dSLi Zefan 18339417ebc8SNikolay Borisov ret = btrfs_commit_transaction(trans); 18349417ebc8SNikolay Borisov 18350caa102dSLi Zefan out_reset: 18360caa102dSLi Zefan if (ret) 18370caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1838b9ca0664SLiu Bo out_drop_sem: 18390b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 1840b9ca0664SLiu Bo out_drop_write: 1841b9ca0664SLiu Bo mnt_drop_write_file(file); 1842b9ca0664SLiu Bo out: 18430caa102dSLi Zefan return ret; 18440caa102dSLi Zefan } 18450caa102dSLi Zefan 184676dda93cSYan, Zheng /* 184776dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 184876dda93cSYan, Zheng */ 184976dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 185076dda93cSYan, Zheng { 18510b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 185276dda93cSYan, Zheng struct btrfs_path *path; 1853175a2b87SJosef Bacik struct btrfs_dir_item *di; 185476dda93cSYan, Zheng struct btrfs_key key; 1855175a2b87SJosef Bacik u64 dir_id; 185676dda93cSYan, Zheng int ret; 185776dda93cSYan, Zheng 185876dda93cSYan, Zheng path = btrfs_alloc_path(); 185976dda93cSYan, Zheng if (!path) 186076dda93cSYan, Zheng return -ENOMEM; 186176dda93cSYan, Zheng 1862175a2b87SJosef Bacik /* Make sure this root isn't set as the default subvol */ 18630b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 18640b246afaSJeff Mahoney di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, 1865175a2b87SJosef Bacik dir_id, "default", 7, 0); 1866175a2b87SJosef Bacik if (di && !IS_ERR(di)) { 1867175a2b87SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1868175a2b87SJosef Bacik if (key.objectid == root->root_key.objectid) { 186972de6b53SGuangyu Sun ret = -EPERM; 18700b246afaSJeff Mahoney btrfs_err(fs_info, 18715d163e0eSJeff Mahoney "deleting default subvolume %llu is not allowed", 18725d163e0eSJeff Mahoney key.objectid); 1873175a2b87SJosef Bacik goto out; 1874175a2b87SJosef Bacik } 1875175a2b87SJosef Bacik btrfs_release_path(path); 1876175a2b87SJosef Bacik } 1877175a2b87SJosef Bacik 187876dda93cSYan, Zheng key.objectid = root->root_key.objectid; 187976dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 188076dda93cSYan, Zheng key.offset = (u64)-1; 188176dda93cSYan, Zheng 18820b246afaSJeff Mahoney ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); 188376dda93cSYan, Zheng if (ret < 0) 188476dda93cSYan, Zheng goto out; 188576dda93cSYan, Zheng BUG_ON(ret == 0); 188676dda93cSYan, Zheng 188776dda93cSYan, Zheng ret = 0; 188876dda93cSYan, Zheng if (path->slots[0] > 0) { 188976dda93cSYan, Zheng path->slots[0]--; 189076dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 189176dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 189276dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 189376dda93cSYan, Zheng ret = -ENOTEMPTY; 189476dda93cSYan, Zheng } 189576dda93cSYan, Zheng out: 189676dda93cSYan, Zheng btrfs_free_path(path); 189776dda93cSYan, Zheng return ret; 189876dda93cSYan, Zheng } 189976dda93cSYan, Zheng 1900ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1901ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1902ac8e9819SChris Mason { 1903abc6e134SChris Mason struct btrfs_key test; 1904abc6e134SChris Mason int ret; 1905abc6e134SChris Mason 1906abc6e134SChris Mason test.objectid = sk->min_objectid; 1907abc6e134SChris Mason test.type = sk->min_type; 1908abc6e134SChris Mason test.offset = sk->min_offset; 1909abc6e134SChris Mason 1910abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1911abc6e134SChris Mason if (ret < 0) 1912ac8e9819SChris Mason return 0; 1913abc6e134SChris Mason 1914abc6e134SChris Mason test.objectid = sk->max_objectid; 1915abc6e134SChris Mason test.type = sk->max_type; 1916abc6e134SChris Mason test.offset = sk->max_offset; 1917abc6e134SChris Mason 1918abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1919abc6e134SChris Mason if (ret > 0) 1920ac8e9819SChris Mason return 0; 1921ac8e9819SChris Mason return 1; 1922ac8e9819SChris Mason } 1923ac8e9819SChris Mason 1924df397565SJeff Mahoney static noinline int copy_to_sk(struct btrfs_path *path, 1925ac8e9819SChris Mason struct btrfs_key *key, 1926ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 19279b6e817dSGerhard Heift size_t *buf_size, 1928ba346b35SGerhard Heift char __user *ubuf, 1929ac8e9819SChris Mason unsigned long *sk_offset, 1930ac8e9819SChris Mason int *num_found) 1931ac8e9819SChris Mason { 1932ac8e9819SChris Mason u64 found_transid; 1933ac8e9819SChris Mason struct extent_buffer *leaf; 1934ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1935dd81d459SNaohiro Aota struct btrfs_key test; 1936ac8e9819SChris Mason unsigned long item_off; 1937ac8e9819SChris Mason unsigned long item_len; 1938ac8e9819SChris Mason int nritems; 1939ac8e9819SChris Mason int i; 1940ac8e9819SChris Mason int slot; 1941ac8e9819SChris Mason int ret = 0; 1942ac8e9819SChris Mason 1943ac8e9819SChris Mason leaf = path->nodes[0]; 1944ac8e9819SChris Mason slot = path->slots[0]; 1945ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1946ac8e9819SChris Mason 1947ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1948ac8e9819SChris Mason i = nritems; 1949ac8e9819SChris Mason goto advance_key; 1950ac8e9819SChris Mason } 1951ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1952ac8e9819SChris Mason 1953ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1954ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1955ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1956ac8e9819SChris Mason 195703b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 195803b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 195903b71c6cSGabriel de Perthuis continue; 196003b71c6cSGabriel de Perthuis 19619b6e817dSGerhard Heift if (sizeof(sh) + item_len > *buf_size) { 19628f5f6178SGerhard Heift if (*num_found) { 1963ac8e9819SChris Mason ret = 1; 19648f5f6178SGerhard Heift goto out; 19658f5f6178SGerhard Heift } 19668f5f6178SGerhard Heift 19678f5f6178SGerhard Heift /* 19688f5f6178SGerhard Heift * return one empty item back for v1, which does not 19698f5f6178SGerhard Heift * handle -EOVERFLOW 19708f5f6178SGerhard Heift */ 19718f5f6178SGerhard Heift 19729b6e817dSGerhard Heift *buf_size = sizeof(sh) + item_len; 1973ac8e9819SChris Mason item_len = 0; 19748f5f6178SGerhard Heift ret = -EOVERFLOW; 19758f5f6178SGerhard Heift } 1976ac8e9819SChris Mason 19779b6e817dSGerhard Heift if (sizeof(sh) + item_len + *sk_offset > *buf_size) { 1978ac8e9819SChris Mason ret = 1; 197925c9bc2eSGerhard Heift goto out; 1980ac8e9819SChris Mason } 1981ac8e9819SChris Mason 1982ac8e9819SChris Mason sh.objectid = key->objectid; 1983ac8e9819SChris Mason sh.offset = key->offset; 1984ac8e9819SChris Mason sh.type = key->type; 1985ac8e9819SChris Mason sh.len = item_len; 1986ac8e9819SChris Mason sh.transid = found_transid; 1987ac8e9819SChris Mason 1988ac8e9819SChris Mason /* copy search result header */ 1989ba346b35SGerhard Heift if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { 1990ba346b35SGerhard Heift ret = -EFAULT; 1991ba346b35SGerhard Heift goto out; 1992ba346b35SGerhard Heift } 1993ba346b35SGerhard Heift 1994ac8e9819SChris Mason *sk_offset += sizeof(sh); 1995ac8e9819SChris Mason 1996ac8e9819SChris Mason if (item_len) { 1997ba346b35SGerhard Heift char __user *up = ubuf + *sk_offset; 1998ac8e9819SChris Mason /* copy the item */ 1999ba346b35SGerhard Heift if (read_extent_buffer_to_user(leaf, up, 2000ba346b35SGerhard Heift item_off, item_len)) { 2001ba346b35SGerhard Heift ret = -EFAULT; 2002ba346b35SGerhard Heift goto out; 2003ba346b35SGerhard Heift } 2004ba346b35SGerhard Heift 2005ac8e9819SChris Mason *sk_offset += item_len; 2006ac8e9819SChris Mason } 2007e2156867SHugo Mills (*num_found)++; 2008ac8e9819SChris Mason 20098f5f6178SGerhard Heift if (ret) /* -EOVERFLOW from above */ 20108f5f6178SGerhard Heift goto out; 20118f5f6178SGerhard Heift 201225c9bc2eSGerhard Heift if (*num_found >= sk->nr_items) { 201325c9bc2eSGerhard Heift ret = 1; 201425c9bc2eSGerhard Heift goto out; 201525c9bc2eSGerhard Heift } 2016ac8e9819SChris Mason } 2017ac8e9819SChris Mason advance_key: 2018ac8e9819SChris Mason ret = 0; 2019dd81d459SNaohiro Aota test.objectid = sk->max_objectid; 2020dd81d459SNaohiro Aota test.type = sk->max_type; 2021dd81d459SNaohiro Aota test.offset = sk->max_offset; 2022dd81d459SNaohiro Aota if (btrfs_comp_cpu_keys(key, &test) >= 0) 2023dd81d459SNaohiro Aota ret = 1; 2024dd81d459SNaohiro Aota else if (key->offset < (u64)-1) 2025abc6e134SChris Mason key->offset++; 2026dd81d459SNaohiro Aota else if (key->type < (u8)-1) { 2027abc6e134SChris Mason key->offset = 0; 2028abc6e134SChris Mason key->type++; 2029dd81d459SNaohiro Aota } else if (key->objectid < (u64)-1) { 2030abc6e134SChris Mason key->offset = 0; 2031abc6e134SChris Mason key->type = 0; 2032abc6e134SChris Mason key->objectid++; 2033abc6e134SChris Mason } else 2034abc6e134SChris Mason ret = 1; 203525c9bc2eSGerhard Heift out: 2036ba346b35SGerhard Heift /* 2037ba346b35SGerhard Heift * 0: all items from this leaf copied, continue with next 2038ba346b35SGerhard Heift * 1: * more items can be copied, but unused buffer is too small 2039ba346b35SGerhard Heift * * all items were found 2040ba346b35SGerhard Heift * Either way, it will stops the loop which iterates to the next 2041ba346b35SGerhard Heift * leaf 2042ba346b35SGerhard Heift * -EOVERFLOW: item was to large for buffer 2043ba346b35SGerhard Heift * -EFAULT: could not copy extent buffer back to userspace 2044ba346b35SGerhard Heift */ 2045ac8e9819SChris Mason return ret; 2046ac8e9819SChris Mason } 2047ac8e9819SChris Mason 2048ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 204912544442SGerhard Heift struct btrfs_ioctl_search_key *sk, 20509b6e817dSGerhard Heift size_t *buf_size, 2051ba346b35SGerhard Heift char __user *ubuf) 2052ac8e9819SChris Mason { 20530b246afaSJeff Mahoney struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); 2054ac8e9819SChris Mason struct btrfs_root *root; 2055ac8e9819SChris Mason struct btrfs_key key; 2056ac8e9819SChris Mason struct btrfs_path *path; 2057ac8e9819SChris Mason int ret; 2058ac8e9819SChris Mason int num_found = 0; 2059ac8e9819SChris Mason unsigned long sk_offset = 0; 2060ac8e9819SChris Mason 20619b6e817dSGerhard Heift if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { 20629b6e817dSGerhard Heift *buf_size = sizeof(struct btrfs_ioctl_search_header); 206312544442SGerhard Heift return -EOVERFLOW; 20649b6e817dSGerhard Heift } 206512544442SGerhard Heift 2066ac8e9819SChris Mason path = btrfs_alloc_path(); 2067ac8e9819SChris Mason if (!path) 2068ac8e9819SChris Mason return -ENOMEM; 2069ac8e9819SChris Mason 2070ac8e9819SChris Mason if (sk->tree_id == 0) { 2071ac8e9819SChris Mason /* search the root of the inode that was passed */ 2072ac8e9819SChris Mason root = BTRFS_I(inode)->root; 2073ac8e9819SChris Mason } else { 2074ac8e9819SChris Mason key.objectid = sk->tree_id; 2075ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 2076ac8e9819SChris Mason key.offset = (u64)-1; 2077ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 2078ac8e9819SChris Mason if (IS_ERR(root)) { 2079ac8e9819SChris Mason btrfs_free_path(path); 2080ac8e9819SChris Mason return -ENOENT; 2081ac8e9819SChris Mason } 2082ac8e9819SChris Mason } 2083ac8e9819SChris Mason 2084ac8e9819SChris Mason key.objectid = sk->min_objectid; 2085ac8e9819SChris Mason key.type = sk->min_type; 2086ac8e9819SChris Mason key.offset = sk->min_offset; 2087ac8e9819SChris Mason 2088ac8e9819SChris Mason while (1) { 20896174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 2090ac8e9819SChris Mason if (ret != 0) { 2091ac8e9819SChris Mason if (ret > 0) 2092ac8e9819SChris Mason ret = 0; 2093ac8e9819SChris Mason goto err; 2094ac8e9819SChris Mason } 2095df397565SJeff Mahoney ret = copy_to_sk(path, &key, sk, buf_size, ubuf, 2096ac8e9819SChris Mason &sk_offset, &num_found); 2097b3b4aa74SDavid Sterba btrfs_release_path(path); 209825c9bc2eSGerhard Heift if (ret) 2099ac8e9819SChris Mason break; 2100ac8e9819SChris Mason 2101ac8e9819SChris Mason } 21028f5f6178SGerhard Heift if (ret > 0) 2103ac8e9819SChris Mason ret = 0; 2104ac8e9819SChris Mason err: 2105ac8e9819SChris Mason sk->nr_items = num_found; 2106ac8e9819SChris Mason btrfs_free_path(path); 2107ac8e9819SChris Mason return ret; 2108ac8e9819SChris Mason } 2109ac8e9819SChris Mason 2110ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 2111ac8e9819SChris Mason void __user *argp) 2112ac8e9819SChris Mason { 2113ba346b35SGerhard Heift struct btrfs_ioctl_search_args __user *uargs; 2114ba346b35SGerhard Heift struct btrfs_ioctl_search_key sk; 2115ac8e9819SChris Mason struct inode *inode; 2116ac8e9819SChris Mason int ret; 21179b6e817dSGerhard Heift size_t buf_size; 2118ac8e9819SChris Mason 2119ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2120ac8e9819SChris Mason return -EPERM; 2121ac8e9819SChris Mason 2122ba346b35SGerhard Heift uargs = (struct btrfs_ioctl_search_args __user *)argp; 2123ac8e9819SChris Mason 2124ba346b35SGerhard Heift if (copy_from_user(&sk, &uargs->key, sizeof(sk))) 2125ba346b35SGerhard Heift return -EFAULT; 2126ba346b35SGerhard Heift 2127ba346b35SGerhard Heift buf_size = sizeof(uargs->buf); 2128ac8e9819SChris Mason 2129496ad9aaSAl Viro inode = file_inode(file); 2130ba346b35SGerhard Heift ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); 21318f5f6178SGerhard Heift 21328f5f6178SGerhard Heift /* 21338f5f6178SGerhard Heift * In the origin implementation an overflow is handled by returning a 21348f5f6178SGerhard Heift * search header with a len of zero, so reset ret. 21358f5f6178SGerhard Heift */ 21368f5f6178SGerhard Heift if (ret == -EOVERFLOW) 21378f5f6178SGerhard Heift ret = 0; 21388f5f6178SGerhard Heift 2139ba346b35SGerhard Heift if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) 2140ac8e9819SChris Mason ret = -EFAULT; 2141ac8e9819SChris Mason return ret; 2142ac8e9819SChris Mason } 2143ac8e9819SChris Mason 2144cc68a8a5SGerhard Heift static noinline int btrfs_ioctl_tree_search_v2(struct file *file, 2145cc68a8a5SGerhard Heift void __user *argp) 2146cc68a8a5SGerhard Heift { 2147cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 __user *uarg; 2148cc68a8a5SGerhard Heift struct btrfs_ioctl_search_args_v2 args; 2149cc68a8a5SGerhard Heift struct inode *inode; 2150cc68a8a5SGerhard Heift int ret; 2151cc68a8a5SGerhard Heift size_t buf_size; 2152ee22184bSByongho Lee const size_t buf_limit = SZ_16M; 2153cc68a8a5SGerhard Heift 2154cc68a8a5SGerhard Heift if (!capable(CAP_SYS_ADMIN)) 2155cc68a8a5SGerhard Heift return -EPERM; 2156cc68a8a5SGerhard Heift 2157cc68a8a5SGerhard Heift /* copy search header and buffer size */ 2158cc68a8a5SGerhard Heift uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; 2159cc68a8a5SGerhard Heift if (copy_from_user(&args, uarg, sizeof(args))) 2160cc68a8a5SGerhard Heift return -EFAULT; 2161cc68a8a5SGerhard Heift 2162cc68a8a5SGerhard Heift buf_size = args.buf_size; 2163cc68a8a5SGerhard Heift 2164cc68a8a5SGerhard Heift /* limit result size to 16MB */ 2165cc68a8a5SGerhard Heift if (buf_size > buf_limit) 2166cc68a8a5SGerhard Heift buf_size = buf_limit; 2167cc68a8a5SGerhard Heift 2168cc68a8a5SGerhard Heift inode = file_inode(file); 2169cc68a8a5SGerhard Heift ret = search_ioctl(inode, &args.key, &buf_size, 2170718dc5faSOmar Sandoval (char __user *)(&uarg->buf[0])); 2171cc68a8a5SGerhard Heift if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) 2172cc68a8a5SGerhard Heift ret = -EFAULT; 2173cc68a8a5SGerhard Heift else if (ret == -EOVERFLOW && 2174cc68a8a5SGerhard Heift copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) 2175cc68a8a5SGerhard Heift ret = -EFAULT; 2176cc68a8a5SGerhard Heift 217776dda93cSYan, Zheng return ret; 217876dda93cSYan, Zheng } 217976dda93cSYan, Zheng 218098d377a0STARUISI Hiroaki /* 2181ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 2182ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 218398d377a0STARUISI Hiroaki */ 218498d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 218598d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 218698d377a0STARUISI Hiroaki { 218798d377a0STARUISI Hiroaki struct btrfs_root *root; 218898d377a0STARUISI Hiroaki struct btrfs_key key; 2189ac8e9819SChris Mason char *ptr; 219098d377a0STARUISI Hiroaki int ret = -1; 219198d377a0STARUISI Hiroaki int slot; 219298d377a0STARUISI Hiroaki int len; 219398d377a0STARUISI Hiroaki int total_len = 0; 219498d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 219598d377a0STARUISI Hiroaki struct extent_buffer *l; 219698d377a0STARUISI Hiroaki struct btrfs_path *path; 219798d377a0STARUISI Hiroaki 219898d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 219998d377a0STARUISI Hiroaki name[0]='\0'; 220098d377a0STARUISI Hiroaki return 0; 220198d377a0STARUISI Hiroaki } 220298d377a0STARUISI Hiroaki 220398d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 220498d377a0STARUISI Hiroaki if (!path) 220598d377a0STARUISI Hiroaki return -ENOMEM; 220698d377a0STARUISI Hiroaki 2207c8bcbfbdSNikolay Borisov ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; 220898d377a0STARUISI Hiroaki 220998d377a0STARUISI Hiroaki key.objectid = tree_id; 221098d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 221198d377a0STARUISI Hiroaki key.offset = (u64)-1; 221298d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 221398d377a0STARUISI Hiroaki if (IS_ERR(root)) { 2214f14d104dSDavid Sterba btrfs_err(info, "could not find root %llu", tree_id); 22158ad6fcabSChris Mason ret = -ENOENT; 22168ad6fcabSChris Mason goto out; 221798d377a0STARUISI Hiroaki } 221898d377a0STARUISI Hiroaki 221998d377a0STARUISI Hiroaki key.objectid = dirid; 222098d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 22218ad6fcabSChris Mason key.offset = (u64)-1; 222298d377a0STARUISI Hiroaki 222398d377a0STARUISI Hiroaki while (1) { 222498d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 222598d377a0STARUISI Hiroaki if (ret < 0) 222698d377a0STARUISI Hiroaki goto out; 222718674c6cSFilipe David Borba Manana else if (ret > 0) { 222818674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 222918674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 223018674c6cSFilipe David Borba Manana if (ret < 0) 223118674c6cSFilipe David Borba Manana goto out; 223218674c6cSFilipe David Borba Manana else if (ret > 0) { 2233ac8e9819SChris Mason ret = -ENOENT; 223498d377a0STARUISI Hiroaki goto out; 2235ac8e9819SChris Mason } 223618674c6cSFilipe David Borba Manana } 223718674c6cSFilipe David Borba Manana 223818674c6cSFilipe David Borba Manana l = path->nodes[0]; 223918674c6cSFilipe David Borba Manana slot = path->slots[0]; 224018674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 224198d377a0STARUISI Hiroaki 224298d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 224398d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 224498d377a0STARUISI Hiroaki ptr -= len + 1; 224598d377a0STARUISI Hiroaki total_len += len + 1; 2246a696cf35SFilipe David Borba Manana if (ptr < name) { 2247a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 224898d377a0STARUISI Hiroaki goto out; 2249a696cf35SFilipe David Borba Manana } 225098d377a0STARUISI Hiroaki 225198d377a0STARUISI Hiroaki *(ptr + len) = '/'; 225298d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 225398d377a0STARUISI Hiroaki 225498d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 225598d377a0STARUISI Hiroaki break; 225698d377a0STARUISI Hiroaki 2257b3b4aa74SDavid Sterba btrfs_release_path(path); 225898d377a0STARUISI Hiroaki key.objectid = key.offset; 22598ad6fcabSChris Mason key.offset = (u64)-1; 226098d377a0STARUISI Hiroaki dirid = key.objectid; 226198d377a0STARUISI Hiroaki } 226277906a50SLi Zefan memmove(name, ptr, total_len); 226398d377a0STARUISI Hiroaki name[total_len] = '\0'; 226498d377a0STARUISI Hiroaki ret = 0; 226598d377a0STARUISI Hiroaki out: 226698d377a0STARUISI Hiroaki btrfs_free_path(path); 2267ac8e9819SChris Mason return ret; 2268ac8e9819SChris Mason } 2269ac8e9819SChris Mason 2270ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2271ac8e9819SChris Mason void __user *argp) 2272ac8e9819SChris Mason { 2273ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2274ac8e9819SChris Mason struct inode *inode; 227501b810b8SDavid Sterba int ret = 0; 2276ac8e9819SChris Mason 22772354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 22782354d08fSJulia Lawall if (IS_ERR(args)) 22792354d08fSJulia Lawall return PTR_ERR(args); 2280c2b96929SDan Carpenter 2281496ad9aaSAl Viro inode = file_inode(file); 2282ac8e9819SChris Mason 228301b810b8SDavid Sterba /* 228401b810b8SDavid Sterba * Unprivileged query to obtain the containing subvolume root id. The 228501b810b8SDavid Sterba * path is reset so it's consistent with btrfs_search_path_in_tree. 228601b810b8SDavid Sterba */ 22871b53ac4dSChris Mason if (args->treeid == 0) 22881b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 22891b53ac4dSChris Mason 229001b810b8SDavid Sterba if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { 229101b810b8SDavid Sterba args->name[0] = 0; 229201b810b8SDavid Sterba goto out; 229301b810b8SDavid Sterba } 229401b810b8SDavid Sterba 229501b810b8SDavid Sterba if (!capable(CAP_SYS_ADMIN)) { 229601b810b8SDavid Sterba ret = -EPERM; 229701b810b8SDavid Sterba goto out; 229801b810b8SDavid Sterba } 229901b810b8SDavid Sterba 2300ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2301ac8e9819SChris Mason args->treeid, args->objectid, 2302ac8e9819SChris Mason args->name); 2303ac8e9819SChris Mason 230401b810b8SDavid Sterba out: 2305ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2306ac8e9819SChris Mason ret = -EFAULT; 2307ac8e9819SChris Mason 2308ac8e9819SChris Mason kfree(args); 230998d377a0STARUISI Hiroaki return ret; 231098d377a0STARUISI Hiroaki } 231198d377a0STARUISI Hiroaki 231276dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 231376dda93cSYan, Zheng void __user *arg) 231476dda93cSYan, Zheng { 231554563d41SAl Viro struct dentry *parent = file->f_path.dentry; 23160b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); 231776dda93cSYan, Zheng struct dentry *dentry; 23182b0143b5SDavid Howells struct inode *dir = d_inode(parent); 231976dda93cSYan, Zheng struct inode *inode; 232076dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 232176dda93cSYan, Zheng struct btrfs_root *dest = NULL; 232276dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 232376dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2324c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2325521e0546SDavid Sterba u64 root_flags; 2326c58aaad2SMiao Xie u64 qgroup_reserved; 232776dda93cSYan, Zheng int namelen; 232876dda93cSYan, Zheng int ret; 232976dda93cSYan, Zheng int err = 0; 233076dda93cSYan, Zheng 2331325c50e3SJeff Mahoney if (!S_ISDIR(dir->i_mode)) 2332325c50e3SJeff Mahoney return -ENOTDIR; 2333325c50e3SJeff Mahoney 233476dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 233576dda93cSYan, Zheng if (IS_ERR(vol_args)) 233676dda93cSYan, Zheng return PTR_ERR(vol_args); 233776dda93cSYan, Zheng 233876dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 233976dda93cSYan, Zheng namelen = strlen(vol_args->name); 234076dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 234176dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 234276dda93cSYan, Zheng err = -EINVAL; 234376dda93cSYan, Zheng goto out; 234476dda93cSYan, Zheng } 234576dda93cSYan, Zheng 2346a561be71SAl Viro err = mnt_want_write_file(file); 234776dda93cSYan, Zheng if (err) 234876dda93cSYan, Zheng goto out; 234976dda93cSYan, Zheng 2350521e0546SDavid Sterba 235100235411SAl Viro err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); 235200235411SAl Viro if (err == -EINTR) 235300235411SAl Viro goto out_drop_write; 235476dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 235576dda93cSYan, Zheng if (IS_ERR(dentry)) { 235676dda93cSYan, Zheng err = PTR_ERR(dentry); 235776dda93cSYan, Zheng goto out_unlock_dir; 235876dda93cSYan, Zheng } 235976dda93cSYan, Zheng 23602b0143b5SDavid Howells if (d_really_is_negative(dentry)) { 236176dda93cSYan, Zheng err = -ENOENT; 236276dda93cSYan, Zheng goto out_dput; 236376dda93cSYan, Zheng } 236476dda93cSYan, Zheng 23652b0143b5SDavid Howells inode = d_inode(dentry); 23664260f7c7SSage Weil dest = BTRFS_I(inode)->root; 23674260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 23684260f7c7SSage Weil /* 23694260f7c7SSage Weil * Regular user. Only allow this with a special mount 23704260f7c7SSage Weil * option, when the user has write+exec access to the 23714260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 23724260f7c7SSage Weil * allowed. 23734260f7c7SSage Weil * 23744260f7c7SSage Weil * Note that this is _not_ check that the subvol is 23754260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 23764260f7c7SSage Weil * otherwise be able to delete. 23774260f7c7SSage Weil * 23784260f7c7SSage Weil * Users who want to delete empty subvols should try 23794260f7c7SSage Weil * rmdir(2). 23804260f7c7SSage Weil */ 23814260f7c7SSage Weil err = -EPERM; 23820b246afaSJeff Mahoney if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) 23834260f7c7SSage Weil goto out_dput; 23844260f7c7SSage Weil 23854260f7c7SSage Weil /* 23864260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 23874260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 23884260f7c7SSage Weil * must be called on the dentry referencing the root 23894260f7c7SSage Weil * of the subvol, not a random directory contained 23904260f7c7SSage Weil * within it. 23914260f7c7SSage Weil */ 23924260f7c7SSage Weil err = -EINVAL; 23934260f7c7SSage Weil if (root == dest) 23944260f7c7SSage Weil goto out_dput; 23954260f7c7SSage Weil 23964260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 23974260f7c7SSage Weil if (err) 23984260f7c7SSage Weil goto out_dput; 23995c39da5bSMiao Xie } 24004260f7c7SSage Weil 24015c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 24024260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 24034260f7c7SSage Weil if (err) 24044260f7c7SSage Weil goto out_dput; 24054260f7c7SSage Weil 24064a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 240776dda93cSYan, Zheng err = -EINVAL; 240876dda93cSYan, Zheng goto out_dput; 240976dda93cSYan, Zheng } 241076dda93cSYan, Zheng 24115955102cSAl Viro inode_lock(inode); 2412521e0546SDavid Sterba 2413521e0546SDavid Sterba /* 2414521e0546SDavid Sterba * Don't allow to delete a subvolume with send in progress. This is 2415521e0546SDavid Sterba * inside the i_mutex so the error handling that has to drop the bit 2416521e0546SDavid Sterba * again is not run concurrently. 2417521e0546SDavid Sterba */ 2418521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2419c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2420c55bfa67SFilipe Manana if (dest->send_in_progress == 0) { 2421c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2422521e0546SDavid Sterba root_flags | BTRFS_ROOT_SUBVOL_DEAD); 2423521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2424521e0546SDavid Sterba } else { 2425521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 24260b246afaSJeff Mahoney btrfs_warn(fs_info, 2427521e0546SDavid Sterba "Attempt to delete subvolume %llu during send", 2428c55bfa67SFilipe Manana dest->root_key.objectid); 2429521e0546SDavid Sterba err = -EPERM; 2430909e26dcSOmar Sandoval goto out_unlock_inode; 2431521e0546SDavid Sterba } 2432521e0546SDavid Sterba 24330b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 243476dda93cSYan, Zheng 243576dda93cSYan, Zheng err = may_destroy_subvol(dest); 243676dda93cSYan, Zheng if (err) 243776dda93cSYan, Zheng goto out_up_write; 243876dda93cSYan, Zheng 2439c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2440c58aaad2SMiao Xie /* 2441c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2442c58aaad2SMiao Xie * ref/backref. 2443c58aaad2SMiao Xie */ 2444c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2445ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2446c58aaad2SMiao Xie if (err) 2447c58aaad2SMiao Xie goto out_up_write; 2448c58aaad2SMiao Xie 2449a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2450a22285a6SYan, Zheng if (IS_ERR(trans)) { 2451a22285a6SYan, Zheng err = PTR_ERR(trans); 2452c58aaad2SMiao Xie goto out_release; 2453a22285a6SYan, Zheng } 2454c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2455c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2456a22285a6SYan, Zheng 245743663557SNikolay Borisov btrfs_record_snapshot_destroy(trans, BTRFS_I(dir)); 24582be63d5cSFilipe Manana 245976dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 246076dda93cSYan, Zheng dest->root_key.objectid, 246176dda93cSYan, Zheng dentry->d_name.name, 246276dda93cSYan, Zheng dentry->d_name.len); 246379787eaaSJeff Mahoney if (ret) { 246479787eaaSJeff Mahoney err = ret; 246566642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 246679787eaaSJeff Mahoney goto out_end_trans; 246779787eaaSJeff Mahoney } 246876dda93cSYan, Zheng 246976dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 247076dda93cSYan, Zheng 247176dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 247276dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 247376dda93cSYan, Zheng dest->root_item.drop_level = 0; 247476dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 247576dda93cSYan, Zheng 247627cdeb70SMiao Xie if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 247776dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 24780b246afaSJeff Mahoney fs_info->tree_root, 247976dda93cSYan, Zheng dest->root_key.objectid); 248079787eaaSJeff Mahoney if (ret) { 248166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 248279787eaaSJeff Mahoney err = ret; 248379787eaaSJeff Mahoney goto out_end_trans; 2484d68fc57bSYan, Zheng } 248579787eaaSJeff Mahoney } 2486dd5f9615SStefan Behrens 24870b246afaSJeff Mahoney ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid, 24886bccf3abSJeff Mahoney BTRFS_UUID_KEY_SUBVOL, 2489dd5f9615SStefan Behrens dest->root_key.objectid); 2490dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 249166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 2492dd5f9615SStefan Behrens err = ret; 2493dd5f9615SStefan Behrens goto out_end_trans; 2494dd5f9615SStefan Behrens } 2495dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 24960b246afaSJeff Mahoney ret = btrfs_uuid_tree_rem(trans, fs_info, 2497dd5f9615SStefan Behrens dest->root_item.received_uuid, 2498dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2499dd5f9615SStefan Behrens dest->root_key.objectid); 2500dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 250166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 2502dd5f9615SStefan Behrens err = ret; 2503dd5f9615SStefan Behrens goto out_end_trans; 2504dd5f9615SStefan Behrens } 2505dd5f9615SStefan Behrens } 2506dd5f9615SStefan Behrens 250779787eaaSJeff Mahoney out_end_trans: 2508c58aaad2SMiao Xie trans->block_rsv = NULL; 2509c58aaad2SMiao Xie trans->bytes_reserved = 0; 25103a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 251179787eaaSJeff Mahoney if (ret && !err) 251279787eaaSJeff Mahoney err = ret; 251376dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2514c58aaad2SMiao Xie out_release: 25157775c818SDavid Sterba btrfs_subvolume_release_metadata(fs_info, &block_rsv); 251676dda93cSYan, Zheng out_up_write: 25170b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 2518521e0546SDavid Sterba if (err) { 2519521e0546SDavid Sterba spin_lock(&dest->root_item_lock); 2520c55bfa67SFilipe Manana root_flags = btrfs_root_flags(&dest->root_item); 2521c55bfa67SFilipe Manana btrfs_set_root_flags(&dest->root_item, 2522521e0546SDavid Sterba root_flags & ~BTRFS_ROOT_SUBVOL_DEAD); 2523521e0546SDavid Sterba spin_unlock(&dest->root_item_lock); 2524521e0546SDavid Sterba } 2525909e26dcSOmar Sandoval out_unlock_inode: 25265955102cSAl Viro inode_unlock(inode); 252776dda93cSYan, Zheng if (!err) { 252864ad6c48SOmar Sandoval d_invalidate(dentry); 252976dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 253076dda93cSYan, Zheng d_delete(dentry); 253161155aa0SDavid Sterba ASSERT(dest->send_in_progress == 0); 2532fa6ac876SLiu Bo 2533fa6ac876SLiu Bo /* the last ref */ 253457cdc8dbSDavid Sterba if (dest->ino_cache_inode) { 253557cdc8dbSDavid Sterba iput(dest->ino_cache_inode); 253657cdc8dbSDavid Sterba dest->ino_cache_inode = NULL; 2537fa6ac876SLiu Bo } 253876dda93cSYan, Zheng } 253976dda93cSYan, Zheng out_dput: 254076dda93cSYan, Zheng dput(dentry); 254176dda93cSYan, Zheng out_unlock_dir: 25425955102cSAl Viro inode_unlock(dir); 254300235411SAl Viro out_drop_write: 25442a79f17eSAl Viro mnt_drop_write_file(file); 254576dda93cSYan, Zheng out: 254676dda93cSYan, Zheng kfree(vol_args); 254776dda93cSYan, Zheng return err; 254876dda93cSYan, Zheng } 254976dda93cSYan, Zheng 25501e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2551f46b5a66SChristoph Hellwig { 2552496ad9aaSAl Viro struct inode *inode = file_inode(file); 2553f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 25541e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2555c146afadSYan Zheng int ret; 2556c146afadSYan Zheng 255725122d15SIlya Dryomov ret = mnt_want_write_file(file); 255825122d15SIlya Dryomov if (ret) 255925122d15SIlya Dryomov return ret; 2560b83cc969SLi Zefan 256125122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 256225122d15SIlya Dryomov ret = -EROFS; 256325122d15SIlya Dryomov goto out; 25645ac00addSStefan Behrens } 2565f46b5a66SChristoph Hellwig 2566f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2567f46b5a66SChristoph Hellwig case S_IFDIR: 2568e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2569e441d54dSChris Mason ret = -EPERM; 2570e441d54dSChris Mason goto out; 2571e441d54dSChris Mason } 2572de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 2573f46b5a66SChristoph Hellwig break; 2574f46b5a66SChristoph Hellwig case S_IFREG: 2575e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2576e441d54dSChris Mason ret = -EINVAL; 2577e441d54dSChris Mason goto out; 2578e441d54dSChris Mason } 25791e701a32SChris Mason 25801e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 25811e701a32SChris Mason if (!range) { 25821e701a32SChris Mason ret = -ENOMEM; 25831e701a32SChris Mason goto out; 25841e701a32SChris Mason } 25851e701a32SChris Mason 25861e701a32SChris Mason if (argp) { 25871e701a32SChris Mason if (copy_from_user(range, argp, 25881e701a32SChris Mason sizeof(*range))) { 25891e701a32SChris Mason ret = -EFAULT; 25901e701a32SChris Mason kfree(range); 2591683be16eSDan Carpenter goto out; 25921e701a32SChris Mason } 25931e701a32SChris Mason /* compression requires us to start the IO */ 25941e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 25951e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 25961e701a32SChris Mason range->extent_thresh = (u32)-1; 25971e701a32SChris Mason } 25981e701a32SChris Mason } else { 25991e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 26001e701a32SChris Mason range->len = (u64)-1; 26011e701a32SChris Mason } 2602496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 26037c829b72SAnand Jain range, BTRFS_OLDEST_GENERATION, 0); 26044cb5300bSChris Mason if (ret > 0) 26054cb5300bSChris Mason ret = 0; 26061e701a32SChris Mason kfree(range); 2607f46b5a66SChristoph Hellwig break; 26088929ecfaSYan, Zheng default: 26098929ecfaSYan, Zheng ret = -EINVAL; 2610f46b5a66SChristoph Hellwig } 2611e441d54dSChris Mason out: 261225122d15SIlya Dryomov mnt_drop_write_file(file); 2613e441d54dSChris Mason return ret; 2614f46b5a66SChristoph Hellwig } 2615f46b5a66SChristoph Hellwig 26162ff7e61eSJeff Mahoney static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) 2617f46b5a66SChristoph Hellwig { 2618f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2619f46b5a66SChristoph Hellwig int ret; 2620f46b5a66SChristoph Hellwig 2621e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2622e441d54dSChris Mason return -EPERM; 2623e441d54dSChris Mason 2624171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) 2625e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2626c9e9f97bSIlya Dryomov 26270b246afaSJeff Mahoney mutex_lock(&fs_info->volume_mutex); 2628dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2629c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2630c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2631c9e9f97bSIlya Dryomov goto out; 2632c9e9f97bSIlya Dryomov } 2633f46b5a66SChristoph Hellwig 26345516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 26352ff7e61eSJeff Mahoney ret = btrfs_init_new_device(fs_info, vol_args->name); 2636f46b5a66SChristoph Hellwig 263743d20761SAnand Jain if (!ret) 26380b246afaSJeff Mahoney btrfs_info(fs_info, "disk added %s", vol_args->name); 263943d20761SAnand Jain 2640f46b5a66SChristoph Hellwig kfree(vol_args); 2641c9e9f97bSIlya Dryomov out: 26420b246afaSJeff Mahoney mutex_unlock(&fs_info->volume_mutex); 2643171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 2644f46b5a66SChristoph Hellwig return ret; 2645f46b5a66SChristoph Hellwig } 2646f46b5a66SChristoph Hellwig 26476b526ed7SAnand Jain static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 2648f46b5a66SChristoph Hellwig { 26490b246afaSJeff Mahoney struct inode *inode = file_inode(file); 26500b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 26516b526ed7SAnand Jain struct btrfs_ioctl_vol_args_v2 *vol_args; 2652f46b5a66SChristoph Hellwig int ret; 2653f46b5a66SChristoph Hellwig 2654e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2655e441d54dSChris Mason return -EPERM; 2656e441d54dSChris Mason 2657da24927bSMiao Xie ret = mnt_want_write_file(file); 2658da24927bSMiao Xie if (ret) 2659da24927bSMiao Xie return ret; 2660c146afadSYan Zheng 2661dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2662c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2663c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2664c47ca32dSDan Carpenter goto err_drop; 2665c9e9f97bSIlya Dryomov } 2666f46b5a66SChristoph Hellwig 26676b526ed7SAnand Jain /* Check for compatibility reject unknown flags */ 2668735654eaSDavid Sterba if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) 2669735654eaSDavid Sterba return -EOPNOTSUPP; 2670f46b5a66SChristoph Hellwig 2671171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 2672183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2673183860f6SAnand Jain goto out; 2674183860f6SAnand Jain } 2675183860f6SAnand Jain 2676735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 26772ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); 26786b526ed7SAnand Jain } else { 26796b526ed7SAnand Jain vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 26802ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 26816b526ed7SAnand Jain } 2682171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 2683183860f6SAnand Jain 26846b526ed7SAnand Jain if (!ret) { 2685735654eaSDavid Sterba if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 26860b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: id %llu", 26876b526ed7SAnand Jain vol_args->devid); 26886b526ed7SAnand Jain else 26890b246afaSJeff Mahoney btrfs_info(fs_info, "device deleted: %s", 26906b526ed7SAnand Jain vol_args->name); 26916b526ed7SAnand Jain } 2692183860f6SAnand Jain out: 2693183860f6SAnand Jain kfree(vol_args); 2694c47ca32dSDan Carpenter err_drop: 26954ac20c70SIlya Dryomov mnt_drop_write_file(file); 2696f46b5a66SChristoph Hellwig return ret; 2697f46b5a66SChristoph Hellwig } 2698f46b5a66SChristoph Hellwig 2699f46b5a66SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2700f46b5a66SChristoph Hellwig { 27010b246afaSJeff Mahoney struct inode *inode = file_inode(file); 27020b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2703f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2704f46b5a66SChristoph Hellwig int ret; 2705f46b5a66SChristoph Hellwig 2706f46b5a66SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2707f46b5a66SChristoph Hellwig return -EPERM; 2708f46b5a66SChristoph Hellwig 2709f46b5a66SChristoph Hellwig ret = mnt_want_write_file(file); 2710f46b5a66SChristoph Hellwig if (ret) 2711f46b5a66SChristoph Hellwig return ret; 2712f46b5a66SChristoph Hellwig 2713171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 2714f46b5a66SChristoph Hellwig ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 271558d7bbf8SDavid Sterba goto out_drop_write; 271658d7bbf8SDavid Sterba } 271758d7bbf8SDavid Sterba 271858d7bbf8SDavid Sterba vol_args = memdup_user(arg, sizeof(*vol_args)); 271958d7bbf8SDavid Sterba if (IS_ERR(vol_args)) { 272058d7bbf8SDavid Sterba ret = PTR_ERR(vol_args); 2721f46b5a66SChristoph Hellwig goto out; 2722f46b5a66SChristoph Hellwig } 2723f46b5a66SChristoph Hellwig 272458d7bbf8SDavid Sterba vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 27252ff7e61eSJeff Mahoney ret = btrfs_rm_device(fs_info, vol_args->name, 0); 2726f46b5a66SChristoph Hellwig 2727f46b5a66SChristoph Hellwig if (!ret) 27280b246afaSJeff Mahoney btrfs_info(fs_info, "disk deleted %s", vol_args->name); 2729f46b5a66SChristoph Hellwig kfree(vol_args); 273058d7bbf8SDavid Sterba out: 2731171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 273258d7bbf8SDavid Sterba out_drop_write: 2733f46b5a66SChristoph Hellwig mnt_drop_write_file(file); 273458d7bbf8SDavid Sterba 2735f46b5a66SChristoph Hellwig return ret; 2736f46b5a66SChristoph Hellwig } 2737f46b5a66SChristoph Hellwig 27382ff7e61eSJeff Mahoney static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, 27392ff7e61eSJeff Mahoney void __user *arg) 2740475f6387SJan Schmidt { 2741027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2742475f6387SJan Schmidt struct btrfs_device *device; 27430b246afaSJeff Mahoney struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2744027ed2f0SLi Zefan int ret = 0; 2745475f6387SJan Schmidt 2746027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2747027ed2f0SLi Zefan if (!fi_args) 2748027ed2f0SLi Zefan return -ENOMEM; 2749027ed2f0SLi Zefan 2750d03262c7SDavid Sterba rcu_read_lock(); 2751027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2752475f6387SJan Schmidt 2753d03262c7SDavid Sterba list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2754027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2755027ed2f0SLi Zefan fi_args->max_id = device->devid; 2756475f6387SJan Schmidt } 2757d03262c7SDavid Sterba rcu_read_unlock(); 2758475f6387SJan Schmidt 2759d03262c7SDavid Sterba memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid)); 2760bea7eafdSOmar Sandoval fi_args->nodesize = fs_info->nodesize; 2761bea7eafdSOmar Sandoval fi_args->sectorsize = fs_info->sectorsize; 2762bea7eafdSOmar Sandoval fi_args->clone_alignment = fs_info->sectorsize; 276380a773fbSDavid Sterba 2764027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2765027ed2f0SLi Zefan ret = -EFAULT; 2766475f6387SJan Schmidt 2767027ed2f0SLi Zefan kfree(fi_args); 2768027ed2f0SLi Zefan return ret; 2769475f6387SJan Schmidt } 2770475f6387SJan Schmidt 27712ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, 27722ff7e61eSJeff Mahoney void __user *arg) 2773475f6387SJan Schmidt { 2774475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2775475f6387SJan Schmidt struct btrfs_device *dev; 2776475f6387SJan Schmidt int ret = 0; 2777475f6387SJan Schmidt char *s_uuid = NULL; 2778475f6387SJan Schmidt 2779475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2780475f6387SJan Schmidt if (IS_ERR(di_args)) 2781475f6387SJan Schmidt return PTR_ERR(di_args); 2782475f6387SJan Schmidt 2783dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2784475f6387SJan Schmidt s_uuid = di_args->uuid; 2785475f6387SJan Schmidt 2786c5593ca3SDavid Sterba rcu_read_lock(); 27870b246afaSJeff Mahoney dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL); 2788475f6387SJan Schmidt 2789475f6387SJan Schmidt if (!dev) { 2790475f6387SJan Schmidt ret = -ENODEV; 2791475f6387SJan Schmidt goto out; 2792475f6387SJan Schmidt } 2793475f6387SJan Schmidt 2794475f6387SJan Schmidt di_args->devid = dev->devid; 27957cc8e58dSMiao Xie di_args->bytes_used = btrfs_device_get_bytes_used(dev); 27967cc8e58dSMiao Xie di_args->total_bytes = btrfs_device_get_total_bytes(dev); 2797475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2798a27202fbSJim Meyering if (dev->name) { 2799606686eeSJosef Bacik struct rcu_string *name; 2800606686eeSJosef Bacik 2801606686eeSJosef Bacik name = rcu_dereference(dev->name); 28026670d4c2SXiongfeng Wang strncpy(di_args->path, name->str, sizeof(di_args->path) - 1); 2803a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2804a27202fbSJim Meyering } else { 280599ba55adSStefan Behrens di_args->path[0] = '\0'; 2806a27202fbSJim Meyering } 2807475f6387SJan Schmidt 2808475f6387SJan Schmidt out: 2809c5593ca3SDavid Sterba rcu_read_unlock(); 2810475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2811475f6387SJan Schmidt ret = -EFAULT; 2812475f6387SJan Schmidt 2813475f6387SJan Schmidt kfree(di_args); 2814475f6387SJan Schmidt return ret; 2815475f6387SJan Schmidt } 2816475f6387SJan Schmidt 2817f4414602SMark Fasheh static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) 2818416161dbSMark Fasheh { 2819416161dbSMark Fasheh struct page *page; 2820416161dbSMark Fasheh 2821416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2822416161dbSMark Fasheh if (!page) 282331314002SFilipe Manana return ERR_PTR(-ENOMEM); 2824416161dbSMark Fasheh 2825416161dbSMark Fasheh if (!PageUptodate(page)) { 282631314002SFilipe Manana int ret; 282731314002SFilipe Manana 282831314002SFilipe Manana ret = btrfs_readpage(NULL, page); 282931314002SFilipe Manana if (ret) 283031314002SFilipe Manana return ERR_PTR(ret); 2831416161dbSMark Fasheh lock_page(page); 2832416161dbSMark Fasheh if (!PageUptodate(page)) { 2833416161dbSMark Fasheh unlock_page(page); 283409cbfeafSKirill A. Shutemov put_page(page); 283531314002SFilipe Manana return ERR_PTR(-EIO); 2836416161dbSMark Fasheh } 283731314002SFilipe Manana if (page->mapping != inode->i_mapping) { 2838416161dbSMark Fasheh unlock_page(page); 283909cbfeafSKirill A. Shutemov put_page(page); 284031314002SFilipe Manana return ERR_PTR(-EAGAIN); 2841416161dbSMark Fasheh } 2842416161dbSMark Fasheh } 2843416161dbSMark Fasheh 2844416161dbSMark Fasheh return page; 2845416161dbSMark Fasheh } 2846416161dbSMark Fasheh 2847f4414602SMark Fasheh static int gather_extent_pages(struct inode *inode, struct page **pages, 2848f4414602SMark Fasheh int num_pages, u64 off) 2849f4414602SMark Fasheh { 2850f4414602SMark Fasheh int i; 285109cbfeafSKirill A. Shutemov pgoff_t index = off >> PAGE_SHIFT; 2852f4414602SMark Fasheh 2853f4414602SMark Fasheh for (i = 0; i < num_pages; i++) { 285431314002SFilipe Manana again: 2855f4414602SMark Fasheh pages[i] = extent_same_get_page(inode, index + i); 285631314002SFilipe Manana if (IS_ERR(pages[i])) { 285731314002SFilipe Manana int err = PTR_ERR(pages[i]); 285831314002SFilipe Manana 285931314002SFilipe Manana if (err == -EAGAIN) 286031314002SFilipe Manana goto again; 286131314002SFilipe Manana pages[i] = NULL; 286231314002SFilipe Manana return err; 286331314002SFilipe Manana } 2864f4414602SMark Fasheh } 2865f4414602SMark Fasheh return 0; 2866f4414602SMark Fasheh } 2867f4414602SMark Fasheh 2868e0bd70c6SFilipe Manana static int lock_extent_range(struct inode *inode, u64 off, u64 len, 2869e0bd70c6SFilipe Manana bool retry_range_locking) 287077fe20dcSMark Fasheh { 2871e0bd70c6SFilipe Manana /* 2872e0bd70c6SFilipe Manana * Do any pending delalloc/csum calculations on inode, one way or 2873e0bd70c6SFilipe Manana * another, and lock file content. 2874e0bd70c6SFilipe Manana * The locking order is: 2875e0bd70c6SFilipe Manana * 2876e0bd70c6SFilipe Manana * 1) pages 2877e0bd70c6SFilipe Manana * 2) range in the inode's io tree 2878e0bd70c6SFilipe Manana */ 287977fe20dcSMark Fasheh while (1) { 288077fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 288177fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 288277fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 288377fe20dcSMark Fasheh off + len - 1); 2884ff5df9b8SFilipe Manana if ((!ordered || 2885ff5df9b8SFilipe Manana ordered->file_offset + ordered->len <= off || 2886ff5df9b8SFilipe Manana ordered->file_offset >= off + len) && 288777fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 2888ff5df9b8SFilipe Manana off + len - 1, EXTENT_DELALLOC, 0, NULL)) { 2889ff5df9b8SFilipe Manana if (ordered) 2890ff5df9b8SFilipe Manana btrfs_put_ordered_extent(ordered); 289177fe20dcSMark Fasheh break; 2892ff5df9b8SFilipe Manana } 289377fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 289477fe20dcSMark Fasheh if (ordered) 289577fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 2896e0bd70c6SFilipe Manana if (!retry_range_locking) 2897e0bd70c6SFilipe Manana return -EAGAIN; 289877fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 289977fe20dcSMark Fasheh } 2900e0bd70c6SFilipe Manana return 0; 290177fe20dcSMark Fasheh } 290277fe20dcSMark Fasheh 2903f4414602SMark Fasheh static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) 2904416161dbSMark Fasheh { 29055955102cSAl Viro inode_unlock(inode1); 29065955102cSAl Viro inode_unlock(inode2); 2907416161dbSMark Fasheh } 2908416161dbSMark Fasheh 2909f4414602SMark Fasheh static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) 2910f4414602SMark Fasheh { 2911f4414602SMark Fasheh if (inode1 < inode2) 2912f4414602SMark Fasheh swap(inode1, inode2); 2913f4414602SMark Fasheh 29145955102cSAl Viro inode_lock_nested(inode1, I_MUTEX_PARENT); 29155955102cSAl Viro inode_lock_nested(inode2, I_MUTEX_CHILD); 2916f4414602SMark Fasheh } 2917f4414602SMark Fasheh 2918f4414602SMark Fasheh static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, 2919f4414602SMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2920f4414602SMark Fasheh { 2921f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2922f4414602SMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2923f4414602SMark Fasheh } 2924f4414602SMark Fasheh 2925e0bd70c6SFilipe Manana static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, 2926e0bd70c6SFilipe Manana struct inode *inode2, u64 loff2, u64 len, 2927e0bd70c6SFilipe Manana bool retry_range_locking) 2928416161dbSMark Fasheh { 2929e0bd70c6SFilipe Manana int ret; 2930e0bd70c6SFilipe Manana 2931416161dbSMark Fasheh if (inode1 < inode2) { 2932416161dbSMark Fasheh swap(inode1, inode2); 2933416161dbSMark Fasheh swap(loff1, loff2); 2934416161dbSMark Fasheh } 2935e0bd70c6SFilipe Manana ret = lock_extent_range(inode1, loff1, len, retry_range_locking); 2936e0bd70c6SFilipe Manana if (ret) 2937e0bd70c6SFilipe Manana return ret; 2938e0bd70c6SFilipe Manana ret = lock_extent_range(inode2, loff2, len, retry_range_locking); 2939e0bd70c6SFilipe Manana if (ret) 2940e0bd70c6SFilipe Manana unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, 2941e0bd70c6SFilipe Manana loff1 + len - 1); 2942e0bd70c6SFilipe Manana return ret; 2943416161dbSMark Fasheh } 2944f4414602SMark Fasheh 2945f4414602SMark Fasheh struct cmp_pages { 2946f4414602SMark Fasheh int num_pages; 2947f4414602SMark Fasheh struct page **src_pages; 2948f4414602SMark Fasheh struct page **dst_pages; 2949f4414602SMark Fasheh }; 2950f4414602SMark Fasheh 2951f4414602SMark Fasheh static void btrfs_cmp_data_free(struct cmp_pages *cmp) 2952f4414602SMark Fasheh { 2953f4414602SMark Fasheh int i; 2954f4414602SMark Fasheh struct page *pg; 2955f4414602SMark Fasheh 2956f4414602SMark Fasheh for (i = 0; i < cmp->num_pages; i++) { 2957f4414602SMark Fasheh pg = cmp->src_pages[i]; 2958e0bd70c6SFilipe Manana if (pg) { 2959e0bd70c6SFilipe Manana unlock_page(pg); 296009cbfeafSKirill A. Shutemov put_page(pg); 2961e0bd70c6SFilipe Manana } 2962f4414602SMark Fasheh pg = cmp->dst_pages[i]; 2963e0bd70c6SFilipe Manana if (pg) { 2964e0bd70c6SFilipe Manana unlock_page(pg); 296509cbfeafSKirill A. Shutemov put_page(pg); 2966f4414602SMark Fasheh } 2967e0bd70c6SFilipe Manana } 2968f4414602SMark Fasheh kfree(cmp->src_pages); 2969f4414602SMark Fasheh kfree(cmp->dst_pages); 2970f4414602SMark Fasheh } 2971f4414602SMark Fasheh 2972f4414602SMark Fasheh static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, 2973f4414602SMark Fasheh struct inode *dst, u64 dst_loff, 2974f4414602SMark Fasheh u64 len, struct cmp_pages *cmp) 2975f4414602SMark Fasheh { 2976f4414602SMark Fasheh int ret; 297709cbfeafSKirill A. Shutemov int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; 2978f4414602SMark Fasheh struct page **src_pgarr, **dst_pgarr; 2979f4414602SMark Fasheh 2980f4414602SMark Fasheh /* 2981f4414602SMark Fasheh * We must gather up all the pages before we initiate our 2982f4414602SMark Fasheh * extent locking. We use an array for the page pointers. Size 2983f4414602SMark Fasheh * of the array is bounded by len, which is in turn bounded by 2984f4414602SMark Fasheh * BTRFS_MAX_DEDUPE_LEN. 2985f4414602SMark Fasheh */ 298666722f7cSDavid Sterba src_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 298766722f7cSDavid Sterba dst_pgarr = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 2988f4414602SMark Fasheh if (!src_pgarr || !dst_pgarr) { 2989f4414602SMark Fasheh kfree(src_pgarr); 2990f4414602SMark Fasheh kfree(dst_pgarr); 2991f4414602SMark Fasheh return -ENOMEM; 2992f4414602SMark Fasheh } 2993f4414602SMark Fasheh cmp->num_pages = num_pages; 2994f4414602SMark Fasheh cmp->src_pages = src_pgarr; 2995f4414602SMark Fasheh cmp->dst_pages = dst_pgarr; 2996f4414602SMark Fasheh 2997b1517622SFilipe Manana /* 2998b1517622SFilipe Manana * If deduping ranges in the same inode, locking rules make it mandatory 2999b1517622SFilipe Manana * to always lock pages in ascending order to avoid deadlocks with 3000b1517622SFilipe Manana * concurrent tasks (such as starting writeback/delalloc). 3001b1517622SFilipe Manana */ 3002b1517622SFilipe Manana if (src == dst && dst_loff < loff) { 3003b1517622SFilipe Manana swap(src_pgarr, dst_pgarr); 3004b1517622SFilipe Manana swap(loff, dst_loff); 3005b1517622SFilipe Manana } 3006b1517622SFilipe Manana 3007b1517622SFilipe Manana ret = gather_extent_pages(src, src_pgarr, cmp->num_pages, loff); 3008f4414602SMark Fasheh if (ret) 3009f4414602SMark Fasheh goto out; 3010f4414602SMark Fasheh 3011b1517622SFilipe Manana ret = gather_extent_pages(dst, dst_pgarr, cmp->num_pages, dst_loff); 3012f4414602SMark Fasheh 3013f4414602SMark Fasheh out: 3014f4414602SMark Fasheh if (ret) 3015f4414602SMark Fasheh btrfs_cmp_data_free(cmp); 301678ad4ce0SNaohiro Aota return ret; 3017416161dbSMark Fasheh } 3018416161dbSMark Fasheh 30191a287cfeSDavid Sterba static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) 3020416161dbSMark Fasheh { 3021416161dbSMark Fasheh int ret = 0; 3022f4414602SMark Fasheh int i; 3023416161dbSMark Fasheh struct page *src_page, *dst_page; 302409cbfeafSKirill A. Shutemov unsigned int cmp_len = PAGE_SIZE; 3025416161dbSMark Fasheh void *addr, *dst_addr; 3026416161dbSMark Fasheh 3027f4414602SMark Fasheh i = 0; 3028416161dbSMark Fasheh while (len) { 302909cbfeafSKirill A. Shutemov if (len < PAGE_SIZE) 3030416161dbSMark Fasheh cmp_len = len; 3031416161dbSMark Fasheh 3032f4414602SMark Fasheh BUG_ON(i >= cmp->num_pages); 3033f4414602SMark Fasheh 3034f4414602SMark Fasheh src_page = cmp->src_pages[i]; 3035f4414602SMark Fasheh dst_page = cmp->dst_pages[i]; 3036e0bd70c6SFilipe Manana ASSERT(PageLocked(src_page)); 3037e0bd70c6SFilipe Manana ASSERT(PageLocked(dst_page)); 3038f4414602SMark Fasheh 3039416161dbSMark Fasheh addr = kmap_atomic(src_page); 3040416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 3041416161dbSMark Fasheh 3042416161dbSMark Fasheh flush_dcache_page(src_page); 3043416161dbSMark Fasheh flush_dcache_page(dst_page); 3044416161dbSMark Fasheh 3045416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 30462b3909f8SDarrick J. Wong ret = -EBADE; 3047416161dbSMark Fasheh 3048416161dbSMark Fasheh kunmap_atomic(addr); 3049416161dbSMark Fasheh kunmap_atomic(dst_addr); 3050416161dbSMark Fasheh 3051416161dbSMark Fasheh if (ret) 3052416161dbSMark Fasheh break; 3053416161dbSMark Fasheh 3054416161dbSMark Fasheh len -= cmp_len; 3055f4414602SMark Fasheh i++; 3056416161dbSMark Fasheh } 3057416161dbSMark Fasheh 3058416161dbSMark Fasheh return ret; 3059416161dbSMark Fasheh } 3060416161dbSMark Fasheh 3061e1d227a4SMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, 3062e1d227a4SMark Fasheh u64 olen) 3063416161dbSMark Fasheh { 3064e1d227a4SMark Fasheh u64 len = *plen; 3065416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 3066416161dbSMark Fasheh 3067e1d227a4SMark Fasheh if (off + olen > inode->i_size || off + olen < off) 3068416161dbSMark Fasheh return -EINVAL; 3069e1d227a4SMark Fasheh 3070e1d227a4SMark Fasheh /* if we extend to eof, continue to block boundary */ 3071e1d227a4SMark Fasheh if (off + len == inode->i_size) 3072e1d227a4SMark Fasheh *plen = len = ALIGN(inode->i_size, bs) - off; 3073e1d227a4SMark Fasheh 3074416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 3075416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 3076416161dbSMark Fasheh return -EINVAL; 3077416161dbSMark Fasheh 3078416161dbSMark Fasheh return 0; 3079416161dbSMark Fasheh } 3080416161dbSMark Fasheh 3081e1d227a4SMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, 3082416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 3083416161dbSMark Fasheh { 3084416161dbSMark Fasheh int ret; 3085e1d227a4SMark Fasheh u64 len = olen; 3086f4414602SMark Fasheh struct cmp_pages cmp; 3087fc4badd9SOmar Sandoval bool same_inode = (src == dst); 30880efa9f48SMark Fasheh u64 same_lock_start = 0; 30890efa9f48SMark Fasheh u64 same_lock_len = 0; 3090416161dbSMark Fasheh 3091113e8283SFilipe Manana if (len == 0) 3092113e8283SFilipe Manana return 0; 3093113e8283SFilipe Manana 3094fc4badd9SOmar Sandoval if (same_inode) 30955955102cSAl Viro inode_lock(src); 3096fc4badd9SOmar Sandoval else 3097fc4badd9SOmar Sandoval btrfs_double_inode_lock(src, dst); 30980efa9f48SMark Fasheh 30990efa9f48SMark Fasheh ret = extent_same_check_offsets(src, loff, &len, olen); 31000efa9f48SMark Fasheh if (ret) 31010efa9f48SMark Fasheh goto out_unlock; 3102fc4badd9SOmar Sandoval 3103fc4badd9SOmar Sandoval ret = extent_same_check_offsets(dst, dst_loff, &len, olen); 3104f4dfe687SFilipe Manana if (ret) 3105f4dfe687SFilipe Manana goto out_unlock; 31060efa9f48SMark Fasheh 3107fc4badd9SOmar Sandoval if (same_inode) { 31080efa9f48SMark Fasheh /* 31090efa9f48SMark Fasheh * Single inode case wants the same checks, except we 31100efa9f48SMark Fasheh * don't want our length pushed out past i_size as 31110efa9f48SMark Fasheh * comparing that data range makes no sense. 31120efa9f48SMark Fasheh * 31130efa9f48SMark Fasheh * extent_same_check_offsets() will do this for an 31140efa9f48SMark Fasheh * unaligned length at i_size, so catch it here and 31150efa9f48SMark Fasheh * reject the request. 31160efa9f48SMark Fasheh * 31170efa9f48SMark Fasheh * This effectively means we require aligned extents 31180efa9f48SMark Fasheh * for the single-inode case, whereas the other cases 31190efa9f48SMark Fasheh * allow an unaligned length so long as it ends at 31200efa9f48SMark Fasheh * i_size. 31210efa9f48SMark Fasheh */ 31220efa9f48SMark Fasheh if (len != olen) { 31230efa9f48SMark Fasheh ret = -EINVAL; 31240efa9f48SMark Fasheh goto out_unlock; 31250efa9f48SMark Fasheh } 31260efa9f48SMark Fasheh 31270efa9f48SMark Fasheh /* Check for overlapping ranges */ 31280efa9f48SMark Fasheh if (dst_loff + len > loff && dst_loff < loff + len) { 31290efa9f48SMark Fasheh ret = -EINVAL; 31300efa9f48SMark Fasheh goto out_unlock; 31310efa9f48SMark Fasheh } 31320efa9f48SMark Fasheh 31330efa9f48SMark Fasheh same_lock_start = min_t(u64, loff, dst_loff); 31340efa9f48SMark Fasheh same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; 31350efa9f48SMark Fasheh } 3136416161dbSMark Fasheh 3137416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 3138416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 3139416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 3140416161dbSMark Fasheh ret = -EINVAL; 3141416161dbSMark Fasheh goto out_unlock; 3142416161dbSMark Fasheh } 3143416161dbSMark Fasheh 3144e0bd70c6SFilipe Manana again: 3145f4414602SMark Fasheh ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); 3146f4414602SMark Fasheh if (ret) 3147f4414602SMark Fasheh goto out_unlock; 3148f4414602SMark Fasheh 31490efa9f48SMark Fasheh if (same_inode) 3150e0bd70c6SFilipe Manana ret = lock_extent_range(src, same_lock_start, same_lock_len, 3151e0bd70c6SFilipe Manana false); 31520efa9f48SMark Fasheh else 3153e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, 3154e0bd70c6SFilipe Manana false); 3155e0bd70c6SFilipe Manana /* 3156e0bd70c6SFilipe Manana * If one of the inodes has dirty pages in the respective range or 3157e0bd70c6SFilipe Manana * ordered extents, we need to flush dellaloc and wait for all ordered 3158e0bd70c6SFilipe Manana * extents in the range. We must unlock the pages and the ranges in the 3159e0bd70c6SFilipe Manana * io trees to avoid deadlocks when flushing delalloc (requires locking 3160e0bd70c6SFilipe Manana * pages) and when waiting for ordered extents to complete (they require 3161e0bd70c6SFilipe Manana * range locking). 3162e0bd70c6SFilipe Manana */ 3163e0bd70c6SFilipe Manana if (ret == -EAGAIN) { 3164e0bd70c6SFilipe Manana /* 3165e0bd70c6SFilipe Manana * Ranges in the io trees already unlocked. Now unlock all 3166e0bd70c6SFilipe Manana * pages before waiting for all IO to complete. 3167e0bd70c6SFilipe Manana */ 3168e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3169e0bd70c6SFilipe Manana if (same_inode) { 3170e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, same_lock_start, 3171e0bd70c6SFilipe Manana same_lock_len); 3172e0bd70c6SFilipe Manana } else { 3173e0bd70c6SFilipe Manana btrfs_wait_ordered_range(src, loff, len); 3174e0bd70c6SFilipe Manana btrfs_wait_ordered_range(dst, dst_loff, len); 3175e0bd70c6SFilipe Manana } 3176e0bd70c6SFilipe Manana goto again; 3177e0bd70c6SFilipe Manana } 3178e0bd70c6SFilipe Manana ASSERT(ret == 0); 3179e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3180e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3181e0bd70c6SFilipe Manana btrfs_cmp_data_free(&cmp); 3182e0bd70c6SFilipe Manana return ret; 3183e0bd70c6SFilipe Manana } 3184f4414602SMark Fasheh 3185207910ddSMark Fasheh /* pass original length for comparison so we stay within i_size */ 31861a287cfeSDavid Sterba ret = btrfs_cmp_data(olen, &cmp); 3187416161dbSMark Fasheh if (ret == 0) 31881c919a5eSMark Fasheh ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); 3189416161dbSMark Fasheh 31900efa9f48SMark Fasheh if (same_inode) 31910efa9f48SMark Fasheh unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, 31920efa9f48SMark Fasheh same_lock_start + same_lock_len - 1); 31930efa9f48SMark Fasheh else 3194f4414602SMark Fasheh btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); 3195f4414602SMark Fasheh 3196f4414602SMark Fasheh btrfs_cmp_data_free(&cmp); 3197416161dbSMark Fasheh out_unlock: 31980efa9f48SMark Fasheh if (same_inode) 31995955102cSAl Viro inode_unlock(src); 32000efa9f48SMark Fasheh else 3201f4414602SMark Fasheh btrfs_double_inode_unlock(src, dst); 3202416161dbSMark Fasheh 3203416161dbSMark Fasheh return ret; 3204416161dbSMark Fasheh } 3205416161dbSMark Fasheh 3206ee22184bSByongho Lee #define BTRFS_MAX_DEDUPE_LEN SZ_16M 3207416161dbSMark Fasheh 32082b3909f8SDarrick J. Wong ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen, 32092b3909f8SDarrick J. Wong struct file *dst_file, u64 dst_loff) 3210416161dbSMark Fasheh { 32112b3909f8SDarrick J. Wong struct inode *src = file_inode(src_file); 32122b3909f8SDarrick J. Wong struct inode *dst = file_inode(dst_file); 3213416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 32142b3909f8SDarrick J. Wong ssize_t res; 3215416161dbSMark Fasheh 32162b3909f8SDarrick J. Wong if (olen > BTRFS_MAX_DEDUPE_LEN) 32172b3909f8SDarrick J. Wong olen = BTRFS_MAX_DEDUPE_LEN; 3218416161dbSMark Fasheh 321909cbfeafSKirill A. Shutemov if (WARN_ON_ONCE(bs < PAGE_SIZE)) { 3220416161dbSMark Fasheh /* 3221416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 3222416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 3223416161dbSMark Fasheh * this situation without an update. 3224416161dbSMark Fasheh */ 32252b3909f8SDarrick J. Wong return -EINVAL; 3226416161dbSMark Fasheh } 3227416161dbSMark Fasheh 32282b3909f8SDarrick J. Wong res = btrfs_extent_same(src, loff, olen, dst, dst_loff); 32292b3909f8SDarrick J. Wong if (res) 32302b3909f8SDarrick J. Wong return res; 32312b3909f8SDarrick J. Wong return olen; 3232416161dbSMark Fasheh } 3233416161dbSMark Fasheh 3234f82a9901SFilipe Manana static int clone_finish_inode_update(struct btrfs_trans_handle *trans, 3235f82a9901SFilipe Manana struct inode *inode, 3236f82a9901SFilipe Manana u64 endoff, 3237f82a9901SFilipe Manana const u64 destoff, 32381c919a5eSMark Fasheh const u64 olen, 32391c919a5eSMark Fasheh int no_time_update) 3240f82a9901SFilipe Manana { 3241f82a9901SFilipe Manana struct btrfs_root *root = BTRFS_I(inode)->root; 3242f82a9901SFilipe Manana int ret; 3243f82a9901SFilipe Manana 3244f82a9901SFilipe Manana inode_inc_iversion(inode); 32451c919a5eSMark Fasheh if (!no_time_update) 3246c2050a45SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 3247f82a9901SFilipe Manana /* 3248f82a9901SFilipe Manana * We round up to the block size at eof when determining which 3249f82a9901SFilipe Manana * extents to clone above, but shouldn't round up the file size. 3250f82a9901SFilipe Manana */ 3251f82a9901SFilipe Manana if (endoff > destoff + olen) 3252f82a9901SFilipe Manana endoff = destoff + olen; 3253f82a9901SFilipe Manana if (endoff > inode->i_size) 32546ef06d27SNikolay Borisov btrfs_i_size_write(BTRFS_I(inode), endoff); 3255f82a9901SFilipe Manana 3256f82a9901SFilipe Manana ret = btrfs_update_inode(trans, root, inode); 3257f82a9901SFilipe Manana if (ret) { 325866642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 32593a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3260f82a9901SFilipe Manana goto out; 3261f82a9901SFilipe Manana } 32623a45bb20SJeff Mahoney ret = btrfs_end_transaction(trans); 3263f82a9901SFilipe Manana out: 3264f82a9901SFilipe Manana return ret; 3265f82a9901SFilipe Manana } 3266f82a9901SFilipe Manana 3267a2f392e4SNikolay Borisov static void clone_update_extent_map(struct btrfs_inode *inode, 32687ffbb598SFilipe Manana const struct btrfs_trans_handle *trans, 32697ffbb598SFilipe Manana const struct btrfs_path *path, 32707ffbb598SFilipe Manana const u64 hole_offset, 32717ffbb598SFilipe Manana const u64 hole_len) 32727ffbb598SFilipe Manana { 3273a2f392e4SNikolay Borisov struct extent_map_tree *em_tree = &inode->extent_tree; 32747ffbb598SFilipe Manana struct extent_map *em; 32757ffbb598SFilipe Manana int ret; 32767ffbb598SFilipe Manana 32777ffbb598SFilipe Manana em = alloc_extent_map(); 32787ffbb598SFilipe Manana if (!em) { 3279a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 32807ffbb598SFilipe Manana return; 32817ffbb598SFilipe Manana } 32827ffbb598SFilipe Manana 328314f59796SFilipe Manana if (path) { 328414f59796SFilipe Manana struct btrfs_file_extent_item *fi; 328514f59796SFilipe Manana 328614f59796SFilipe Manana fi = btrfs_item_ptr(path->nodes[0], path->slots[0], 328714f59796SFilipe Manana struct btrfs_file_extent_item); 3288a2f392e4SNikolay Borisov btrfs_extent_item_to_extent_map(inode, path, fi, false, em); 32897ffbb598SFilipe Manana em->generation = -1; 32907ffbb598SFilipe Manana if (btrfs_file_extent_type(path->nodes[0], fi) == 32917ffbb598SFilipe Manana BTRFS_FILE_EXTENT_INLINE) 32927ffbb598SFilipe Manana set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 3293a2f392e4SNikolay Borisov &inode->runtime_flags); 32947ffbb598SFilipe Manana } else { 32957ffbb598SFilipe Manana em->start = hole_offset; 32967ffbb598SFilipe Manana em->len = hole_len; 32977ffbb598SFilipe Manana em->ram_bytes = em->len; 32987ffbb598SFilipe Manana em->orig_start = hole_offset; 32997ffbb598SFilipe Manana em->block_start = EXTENT_MAP_HOLE; 33007ffbb598SFilipe Manana em->block_len = 0; 33017ffbb598SFilipe Manana em->orig_block_len = 0; 33027ffbb598SFilipe Manana em->compress_type = BTRFS_COMPRESS_NONE; 33037ffbb598SFilipe Manana em->generation = trans->transid; 33047ffbb598SFilipe Manana } 33057ffbb598SFilipe Manana 33067ffbb598SFilipe Manana while (1) { 33077ffbb598SFilipe Manana write_lock(&em_tree->lock); 33087ffbb598SFilipe Manana ret = add_extent_mapping(em_tree, em, 1); 33097ffbb598SFilipe Manana write_unlock(&em_tree->lock); 33107ffbb598SFilipe Manana if (ret != -EEXIST) { 33117ffbb598SFilipe Manana free_extent_map(em); 33127ffbb598SFilipe Manana break; 33137ffbb598SFilipe Manana } 3314a2f392e4SNikolay Borisov btrfs_drop_extent_cache(inode, em->start, 33157ffbb598SFilipe Manana em->start + em->len - 1, 0); 33167ffbb598SFilipe Manana } 33177ffbb598SFilipe Manana 3318ee39b432SDavid Sterba if (ret) 3319a2f392e4SNikolay Borisov set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); 33207ffbb598SFilipe Manana } 33217ffbb598SFilipe Manana 33228039d87dSFilipe Manana /* 33238039d87dSFilipe Manana * Make sure we do not end up inserting an inline extent into a file that has 33248039d87dSFilipe Manana * already other (non-inline) extents. If a file has an inline extent it can 33258039d87dSFilipe Manana * not have any other extents and the (single) inline extent must start at the 33268039d87dSFilipe Manana * file offset 0. Failing to respect these rules will lead to file corruption, 33278039d87dSFilipe Manana * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc 33288039d87dSFilipe Manana * 33298039d87dSFilipe Manana * We can have extents that have been already written to disk or we can have 33308039d87dSFilipe Manana * dirty ranges still in delalloc, in which case the extent maps and items are 33318039d87dSFilipe Manana * created only when we run delalloc, and the delalloc ranges might fall outside 33328039d87dSFilipe Manana * the range we are currently locking in the inode's io tree. So we check the 33338039d87dSFilipe Manana * inode's i_size because of that (i_size updates are done while holding the 33348039d87dSFilipe Manana * i_mutex, which we are holding here). 33358039d87dSFilipe Manana * We also check to see if the inode has a size not greater than "datal" but has 33368039d87dSFilipe Manana * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are 33378039d87dSFilipe Manana * protected against such concurrent fallocate calls by the i_mutex). 33388039d87dSFilipe Manana * 33398039d87dSFilipe Manana * If the file has no extents but a size greater than datal, do not allow the 33408039d87dSFilipe Manana * copy because we would need turn the inline extent into a non-inline one (even 33418039d87dSFilipe Manana * with NO_HOLES enabled). If we find our destination inode only has one inline 33428039d87dSFilipe Manana * extent, just overwrite it with the source inline extent if its size is less 33438039d87dSFilipe Manana * than the source extent's size, or we could copy the source inline extent's 33448039d87dSFilipe Manana * data into the destination inode's inline extent if the later is greater then 33458039d87dSFilipe Manana * the former. 33468039d87dSFilipe Manana */ 33474a0ab9d7SDavid Sterba static int clone_copy_inline_extent(struct inode *dst, 33488039d87dSFilipe Manana struct btrfs_trans_handle *trans, 33498039d87dSFilipe Manana struct btrfs_path *path, 33508039d87dSFilipe Manana struct btrfs_key *new_key, 33518039d87dSFilipe Manana const u64 drop_start, 33528039d87dSFilipe Manana const u64 datal, 33538039d87dSFilipe Manana const u64 skip, 33548039d87dSFilipe Manana const u64 size, 33558039d87dSFilipe Manana char *inline_data) 33568039d87dSFilipe Manana { 33570b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); 33588039d87dSFilipe Manana struct btrfs_root *root = BTRFS_I(dst)->root; 33598039d87dSFilipe Manana const u64 aligned_end = ALIGN(new_key->offset + datal, 33600b246afaSJeff Mahoney fs_info->sectorsize); 33618039d87dSFilipe Manana int ret; 33628039d87dSFilipe Manana struct btrfs_key key; 33638039d87dSFilipe Manana 33648039d87dSFilipe Manana if (new_key->offset > 0) 33658039d87dSFilipe Manana return -EOPNOTSUPP; 33668039d87dSFilipe Manana 33674a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(dst)); 33688039d87dSFilipe Manana key.type = BTRFS_EXTENT_DATA_KEY; 33698039d87dSFilipe Manana key.offset = 0; 33708039d87dSFilipe Manana ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 33718039d87dSFilipe Manana if (ret < 0) { 33728039d87dSFilipe Manana return ret; 33738039d87dSFilipe Manana } else if (ret > 0) { 33748039d87dSFilipe Manana if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { 33758039d87dSFilipe Manana ret = btrfs_next_leaf(root, path); 33768039d87dSFilipe Manana if (ret < 0) 33778039d87dSFilipe Manana return ret; 33788039d87dSFilipe Manana else if (ret > 0) 33798039d87dSFilipe Manana goto copy_inline_extent; 33808039d87dSFilipe Manana } 33818039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 33824a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 33838039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) { 33848039d87dSFilipe Manana ASSERT(key.offset > 0); 33858039d87dSFilipe Manana return -EOPNOTSUPP; 33868039d87dSFilipe Manana } 33878039d87dSFilipe Manana } else if (i_size_read(dst) <= datal) { 33888039d87dSFilipe Manana struct btrfs_file_extent_item *ei; 33898039d87dSFilipe Manana u64 ext_len; 33908039d87dSFilipe Manana 33918039d87dSFilipe Manana /* 33928039d87dSFilipe Manana * If the file size is <= datal, make sure there are no other 33938039d87dSFilipe Manana * extents following (can happen do to an fallocate call with 33948039d87dSFilipe Manana * the flag FALLOC_FL_KEEP_SIZE). 33958039d87dSFilipe Manana */ 33968039d87dSFilipe Manana ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 33978039d87dSFilipe Manana struct btrfs_file_extent_item); 33988039d87dSFilipe Manana /* 33998039d87dSFilipe Manana * If it's an inline extent, it can not have other extents 34008039d87dSFilipe Manana * following it. 34018039d87dSFilipe Manana */ 34028039d87dSFilipe Manana if (btrfs_file_extent_type(path->nodes[0], ei) == 34038039d87dSFilipe Manana BTRFS_FILE_EXTENT_INLINE) 34048039d87dSFilipe Manana goto copy_inline_extent; 34058039d87dSFilipe Manana 34068039d87dSFilipe Manana ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); 34078039d87dSFilipe Manana if (ext_len > aligned_end) 34088039d87dSFilipe Manana return -EOPNOTSUPP; 34098039d87dSFilipe Manana 34108039d87dSFilipe Manana ret = btrfs_next_item(root, path); 34118039d87dSFilipe Manana if (ret < 0) { 34128039d87dSFilipe Manana return ret; 34138039d87dSFilipe Manana } else if (ret == 0) { 34148039d87dSFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 34158039d87dSFilipe Manana path->slots[0]); 34164a0cc7caSNikolay Borisov if (key.objectid == btrfs_ino(BTRFS_I(dst)) && 34178039d87dSFilipe Manana key.type == BTRFS_EXTENT_DATA_KEY) 34188039d87dSFilipe Manana return -EOPNOTSUPP; 34198039d87dSFilipe Manana } 34208039d87dSFilipe Manana } 34218039d87dSFilipe Manana 34228039d87dSFilipe Manana copy_inline_extent: 34238039d87dSFilipe Manana /* 34248039d87dSFilipe Manana * We have no extent items, or we have an extent at offset 0 which may 34258039d87dSFilipe Manana * or may not be inlined. All these cases are dealt the same way. 34268039d87dSFilipe Manana */ 34278039d87dSFilipe Manana if (i_size_read(dst) > datal) { 34288039d87dSFilipe Manana /* 34298039d87dSFilipe Manana * If the destination inode has an inline extent... 34308039d87dSFilipe Manana * This would require copying the data from the source inline 34318039d87dSFilipe Manana * extent into the beginning of the destination's inline extent. 34328039d87dSFilipe Manana * But this is really complex, both extents can be compressed 34338039d87dSFilipe Manana * or just one of them, which would require decompressing and 34348039d87dSFilipe Manana * re-compressing data (which could increase the new compressed 34358039d87dSFilipe Manana * size, not allowing the compressed data to fit anymore in an 34368039d87dSFilipe Manana * inline extent). 34378039d87dSFilipe Manana * So just don't support this case for now (it should be rare, 34388039d87dSFilipe Manana * we are not really saving space when cloning inline extents). 34398039d87dSFilipe Manana */ 34408039d87dSFilipe Manana return -EOPNOTSUPP; 34418039d87dSFilipe Manana } 34428039d87dSFilipe Manana 34438039d87dSFilipe Manana btrfs_release_path(path); 34448039d87dSFilipe Manana ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); 34458039d87dSFilipe Manana if (ret) 34468039d87dSFilipe Manana return ret; 34478039d87dSFilipe Manana ret = btrfs_insert_empty_item(trans, root, path, new_key, size); 34488039d87dSFilipe Manana if (ret) 34498039d87dSFilipe Manana return ret; 34508039d87dSFilipe Manana 34518039d87dSFilipe Manana if (skip) { 34528039d87dSFilipe Manana const u32 start = btrfs_file_extent_calc_inline_size(0); 34538039d87dSFilipe Manana 34548039d87dSFilipe Manana memmove(inline_data + start, inline_data + start + skip, datal); 34558039d87dSFilipe Manana } 34568039d87dSFilipe Manana 34578039d87dSFilipe Manana write_extent_buffer(path->nodes[0], inline_data, 34588039d87dSFilipe Manana btrfs_item_ptr_offset(path->nodes[0], 34598039d87dSFilipe Manana path->slots[0]), 34608039d87dSFilipe Manana size); 34618039d87dSFilipe Manana inode_add_bytes(dst, datal); 34628039d87dSFilipe Manana 34638039d87dSFilipe Manana return 0; 34648039d87dSFilipe Manana } 34658039d87dSFilipe Manana 346632b7c687SMark Fasheh /** 346732b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 346832b7c687SMark Fasheh * 346932b7c687SMark Fasheh * @src: Inode to clone from 347032b7c687SMark Fasheh * @inode: Inode to clone to 347132b7c687SMark Fasheh * @off: Offset within source to start clone from 347232b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 34731c919a5eSMark Fasheh * @olen_aligned: Block-aligned value of olen 347432b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 34751c919a5eSMark Fasheh * @no_time_update: Whether to update mtime/ctime on the target inode 347632b7c687SMark Fasheh */ 347732b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 3478f82a9901SFilipe Manana const u64 off, const u64 olen, const u64 olen_aligned, 34791c919a5eSMark Fasheh const u64 destoff, int no_time_update) 3480f46b5a66SChristoph Hellwig { 34810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 3482f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 348332b7c687SMark Fasheh struct btrfs_path *path = NULL; 3484f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 348532b7c687SMark Fasheh struct btrfs_trans_handle *trans; 348632b7c687SMark Fasheh char *buf = NULL; 3487ae01a0abSYan Zheng struct btrfs_key key; 3488f46b5a66SChristoph Hellwig u32 nritems; 3489f46b5a66SChristoph Hellwig int slot; 3490ae01a0abSYan Zheng int ret; 3491f82a9901SFilipe Manana const u64 len = olen_aligned; 3492f82a9901SFilipe Manana u64 last_dest_end = destoff; 3493ae01a0abSYan Zheng 3494ae01a0abSYan Zheng ret = -ENOMEM; 3495752ade68SMichal Hocko buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); 3496ae01a0abSYan Zheng if (!buf) 349732b7c687SMark Fasheh return ret; 3498ae01a0abSYan Zheng 3499ae01a0abSYan Zheng path = btrfs_alloc_path(); 3500ae01a0abSYan Zheng if (!path) { 350115351955SDavid Sterba kvfree(buf); 350232b7c687SMark Fasheh return ret; 3503ae01a0abSYan Zheng } 350432b7c687SMark Fasheh 3505e4058b54SDavid Sterba path->reada = READA_FORWARD; 3506c5c9cd4dSSage Weil /* clone data */ 35074a0cc7caSNikolay Borisov key.objectid = btrfs_ino(BTRFS_I(src)); 3508ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 35092c463823SFilipe Manana key.offset = off; 3510f46b5a66SChristoph Hellwig 3511f46b5a66SChristoph Hellwig while (1) { 3512de249e66SChris Mason u64 next_key_min_offset = key.offset + 1; 3513df858e76SFilipe Manana 3514f46b5a66SChristoph Hellwig /* 3515f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 3516f46b5a66SChristoph Hellwig * tree. 3517f46b5a66SChristoph Hellwig */ 3518e4355f34SFilipe David Borba Manana path->leave_spinning = 1; 3519362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 3520362a20c5SDavid Sterba 0, 0); 3521f46b5a66SChristoph Hellwig if (ret < 0) 3522f46b5a66SChristoph Hellwig goto out; 35232c463823SFilipe Manana /* 35242c463823SFilipe Manana * First search, if no extent item that starts at offset off was 35252c463823SFilipe Manana * found but the previous item is an extent item, it's possible 35262c463823SFilipe Manana * it might overlap our target range, therefore process it. 35272c463823SFilipe Manana */ 35282c463823SFilipe Manana if (key.offset == off && ret > 0 && path->slots[0] > 0) { 35292c463823SFilipe Manana btrfs_item_key_to_cpu(path->nodes[0], &key, 35302c463823SFilipe Manana path->slots[0] - 1); 35312c463823SFilipe Manana if (key.type == BTRFS_EXTENT_DATA_KEY) 35322c463823SFilipe Manana path->slots[0]--; 35332c463823SFilipe Manana } 3534f46b5a66SChristoph Hellwig 3535ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3536e4355f34SFilipe David Borba Manana process_slot: 3537ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 3538362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 3539f46b5a66SChristoph Hellwig if (ret < 0) 3540f46b5a66SChristoph Hellwig goto out; 3541f46b5a66SChristoph Hellwig if (ret > 0) 3542f46b5a66SChristoph Hellwig break; 3543ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 3544f46b5a66SChristoph Hellwig } 3545f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 3546f46b5a66SChristoph Hellwig slot = path->slots[0]; 3547f46b5a66SChristoph Hellwig 3548ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 3549962a298fSDavid Sterba if (key.type > BTRFS_EXTENT_DATA_KEY || 35504a0cc7caSNikolay Borisov key.objectid != btrfs_ino(BTRFS_I(src))) 3551f46b5a66SChristoph Hellwig break; 3552f46b5a66SChristoph Hellwig 3553962a298fSDavid Sterba if (key.type == BTRFS_EXTENT_DATA_KEY) { 3554c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 3555c5c9cd4dSSage Weil int type; 355631840ae1SZheng Yan u32 size; 355731840ae1SZheng Yan struct btrfs_key new_key; 3558c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 3559c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 3560c5c9cd4dSSage Weil u8 comp; 3561f82a9901SFilipe Manana u64 drop_start; 356231840ae1SZheng Yan 3563c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 3564c5c9cd4dSSage Weil struct btrfs_file_extent_item); 3565c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 3566c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 3567c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3568c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3569d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 3570d397712bSChris Mason extent); 3571d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 3572d397712bSChris Mason extent); 3573c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 3574d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 3575d397712bSChris Mason extent); 3576c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3577c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 3578c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 3579c5c9cd4dSSage Weil extent); 3580c5c9cd4dSSage Weil } 358131840ae1SZheng Yan 35822c463823SFilipe Manana /* 35832c463823SFilipe Manana * The first search might have left us at an extent 35842c463823SFilipe Manana * item that ends before our target range's start, can 35852c463823SFilipe Manana * happen if we have holes and NO_HOLES feature enabled. 35862c463823SFilipe Manana */ 35872c463823SFilipe Manana if (key.offset + datal <= off) { 3588e4355f34SFilipe David Borba Manana path->slots[0]++; 3589e4355f34SFilipe David Borba Manana goto process_slot; 35902c463823SFilipe Manana } else if (key.offset >= off + len) { 35912c463823SFilipe Manana break; 3592e4355f34SFilipe David Borba Manana } 3593df858e76SFilipe Manana next_key_min_offset = key.offset + datal; 3594e4355f34SFilipe David Borba Manana size = btrfs_item_size_nr(leaf, slot); 3595e4355f34SFilipe David Borba Manana read_extent_buffer(leaf, buf, 3596e4355f34SFilipe David Borba Manana btrfs_item_ptr_offset(leaf, slot), 3597e4355f34SFilipe David Borba Manana size); 3598e4355f34SFilipe David Borba Manana 3599e4355f34SFilipe David Borba Manana btrfs_release_path(path); 3600e4355f34SFilipe David Borba Manana path->leave_spinning = 0; 3601c5c9cd4dSSage Weil 360231840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 36034a0cc7caSNikolay Borisov new_key.objectid = btrfs_ino(BTRFS_I(inode)); 36044d728ec7SLi Zefan if (off <= key.offset) 3605c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 36064d728ec7SLi Zefan else 36074d728ec7SLi Zefan new_key.offset = destoff; 3608c5c9cd4dSSage Weil 3609b6f3409bSSage Weil /* 3610f82a9901SFilipe Manana * Deal with a hole that doesn't have an extent item 3611f82a9901SFilipe Manana * that represents it (NO_HOLES feature enabled). 3612f82a9901SFilipe Manana * This hole is either in the middle of the cloning 3613f82a9901SFilipe Manana * range or at the beginning (fully overlaps it or 3614f82a9901SFilipe Manana * partially overlaps it). 3615f82a9901SFilipe Manana */ 3616f82a9901SFilipe Manana if (new_key.offset != last_dest_end) 3617f82a9901SFilipe Manana drop_start = last_dest_end; 3618f82a9901SFilipe Manana else 3619f82a9901SFilipe Manana drop_start = new_key.offset; 3620f82a9901SFilipe Manana 3621f82a9901SFilipe Manana /* 3622b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 3623b6f3409bSSage Weil * 1 - add new extent 3624b6f3409bSSage Weil * 1 - inode update 3625b6f3409bSSage Weil */ 3626b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 3627a22285a6SYan, Zheng if (IS_ERR(trans)) { 3628a22285a6SYan, Zheng ret = PTR_ERR(trans); 3629a22285a6SYan, Zheng goto out; 3630a22285a6SYan, Zheng } 3631a22285a6SYan, Zheng 3632c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 3633c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 3634d72c0842SLi Zefan /* 3635d72c0842SLi Zefan * a | --- range to clone ---| b 3636d72c0842SLi Zefan * | ------------- extent ------------- | 3637d72c0842SLi Zefan */ 3638d72c0842SLi Zefan 363993915584SAntonio Ospite /* subtract range b */ 3640d72c0842SLi Zefan if (key.offset + datal > off + len) 3641d72c0842SLi Zefan datal = off + len - key.offset; 3642d72c0842SLi Zefan 364393915584SAntonio Ospite /* subtract range a */ 3644a22285a6SYan, Zheng if (off > key.offset) { 3645a22285a6SYan, Zheng datao += off - key.offset; 3646a22285a6SYan, Zheng datal -= off - key.offset; 3647a22285a6SYan, Zheng } 3648a22285a6SYan, Zheng 36495dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3650f82a9901SFilipe Manana drop_start, 3651a22285a6SYan, Zheng new_key.offset + datal, 36522671485dSJosef Bacik 1); 365379787eaaSJeff Mahoney if (ret) { 36543f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 365500fdf13aSLiu Bo btrfs_abort_transaction(trans, 365666642832SJeff Mahoney ret); 36573a45bb20SJeff Mahoney btrfs_end_transaction(trans); 365879787eaaSJeff Mahoney goto out; 365979787eaaSJeff Mahoney } 3660a22285a6SYan, Zheng 366131840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 366231840ae1SZheng Yan &new_key, size); 366379787eaaSJeff Mahoney if (ret) { 366466642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 36653a45bb20SJeff Mahoney btrfs_end_transaction(trans); 366679787eaaSJeff Mahoney goto out; 366779787eaaSJeff Mahoney } 366831840ae1SZheng Yan 366931840ae1SZheng Yan leaf = path->nodes[0]; 367031840ae1SZheng Yan slot = path->slots[0]; 367131840ae1SZheng Yan write_extent_buffer(leaf, buf, 367231840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 367331840ae1SZheng Yan size); 3674ae01a0abSYan Zheng 3675f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3676f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3677c5c9cd4dSSage Weil 3678c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3679c5c9cd4dSSage Weil if (!disko) 3680c5c9cd4dSSage Weil datao = 0; 3681c5c9cd4dSSage Weil 3682c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3683c5c9cd4dSSage Weil datao); 3684c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3685c5c9cd4dSSage Weil datal); 3686fcebe456SJosef Bacik 3687c5c9cd4dSSage Weil if (disko) { 3688c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 36892ff7e61eSJeff Mahoney ret = btrfs_inc_extent_ref(trans, 369084f7d8e6SJosef Bacik root, 36915d4f98a2SYan Zheng disko, diskl, 0, 3692f46b5a66SChristoph Hellwig root->root_key.objectid, 36934a0cc7caSNikolay Borisov btrfs_ino(BTRFS_I(inode)), 3694b06c4bf5SFilipe Manana new_key.offset - datao); 369579787eaaSJeff Mahoney if (ret) { 369679787eaaSJeff Mahoney btrfs_abort_transaction(trans, 369779787eaaSJeff Mahoney ret); 36983a45bb20SJeff Mahoney btrfs_end_transaction(trans); 369979787eaaSJeff Mahoney goto out; 370079787eaaSJeff Mahoney 370179787eaaSJeff Mahoney } 3702f46b5a66SChristoph Hellwig } 3703c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3704c5c9cd4dSSage Weil u64 skip = 0; 3705c5c9cd4dSSage Weil u64 trim = 0; 3706ed958762SFilipe Manana 3707c5c9cd4dSSage Weil if (off > key.offset) { 3708c5c9cd4dSSage Weil skip = off - key.offset; 3709c5c9cd4dSSage Weil new_key.offset += skip; 371031840ae1SZheng Yan } 3711d397712bSChris Mason 3712c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3713c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3714d397712bSChris Mason 3715c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3716c5c9cd4dSSage Weil ret = -EINVAL; 37173a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3718c5c9cd4dSSage Weil goto out; 371931840ae1SZheng Yan } 3720c5c9cd4dSSage Weil size -= skip + trim; 3721c5c9cd4dSSage Weil datal -= skip + trim; 3722a22285a6SYan, Zheng 37234a0ab9d7SDavid Sterba ret = clone_copy_inline_extent(inode, 37248039d87dSFilipe Manana trans, path, 37258039d87dSFilipe Manana &new_key, 3726f82a9901SFilipe Manana drop_start, 37278039d87dSFilipe Manana datal, 37288039d87dSFilipe Manana skip, size, buf); 372979787eaaSJeff Mahoney if (ret) { 37303f9e3df8SDavid Sterba if (ret != -EOPNOTSUPP) 37313a29bc09SChris Mason btrfs_abort_transaction(trans, 373279787eaaSJeff Mahoney ret); 37333a45bb20SJeff Mahoney btrfs_end_transaction(trans); 373479787eaaSJeff Mahoney goto out; 373579787eaaSJeff Mahoney } 3736c5c9cd4dSSage Weil leaf = path->nodes[0]; 3737c5c9cd4dSSage Weil slot = path->slots[0]; 3738c5c9cd4dSSage Weil } 3739c5c9cd4dSSage Weil 37407ffbb598SFilipe Manana /* If we have an implicit hole (NO_HOLES feature). */ 37417ffbb598SFilipe Manana if (drop_start < new_key.offset) 3742a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 374314f59796SFilipe Manana NULL, drop_start, 37447ffbb598SFilipe Manana new_key.offset - drop_start); 37457ffbb598SFilipe Manana 3746a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, 3747a2f392e4SNikolay Borisov path, 0, 0); 37487ffbb598SFilipe Manana 3749c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3750b3b4aa74SDavid Sterba btrfs_release_path(path); 3751c5c9cd4dSSage Weil 375262e2390eSFilipe Manana last_dest_end = ALIGN(new_key.offset + datal, 37530b246afaSJeff Mahoney fs_info->sectorsize); 3754f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, 3755f82a9901SFilipe Manana last_dest_end, 37561c919a5eSMark Fasheh destoff, olen, 37571c919a5eSMark Fasheh no_time_update); 3758f82a9901SFilipe Manana if (ret) 375979787eaaSJeff Mahoney goto out; 37602c463823SFilipe Manana if (new_key.offset + datal >= destoff + len) 37612c463823SFilipe Manana break; 3762a22285a6SYan, Zheng } 3763b3b4aa74SDavid Sterba btrfs_release_path(path); 3764df858e76SFilipe Manana key.offset = next_key_min_offset; 376569ae5e44SWang Xiaoguang 376669ae5e44SWang Xiaoguang if (fatal_signal_pending(current)) { 376769ae5e44SWang Xiaoguang ret = -EINTR; 376869ae5e44SWang Xiaoguang goto out; 376969ae5e44SWang Xiaoguang } 3770ae01a0abSYan Zheng } 3771f46b5a66SChristoph Hellwig ret = 0; 377232b7c687SMark Fasheh 3773f82a9901SFilipe Manana if (last_dest_end < destoff + len) { 3774f82a9901SFilipe Manana /* 3775f82a9901SFilipe Manana * We have an implicit hole (NO_HOLES feature is enabled) that 3776f82a9901SFilipe Manana * fully or partially overlaps our cloning range at its end. 3777f82a9901SFilipe Manana */ 3778b3b4aa74SDavid Sterba btrfs_release_path(path); 3779f82a9901SFilipe Manana 3780f82a9901SFilipe Manana /* 3781f82a9901SFilipe Manana * 1 - remove extent(s) 3782f82a9901SFilipe Manana * 1 - inode update 3783f82a9901SFilipe Manana */ 3784f82a9901SFilipe Manana trans = btrfs_start_transaction(root, 2); 3785f82a9901SFilipe Manana if (IS_ERR(trans)) { 3786f82a9901SFilipe Manana ret = PTR_ERR(trans); 3787f82a9901SFilipe Manana goto out; 3788f82a9901SFilipe Manana } 3789f82a9901SFilipe Manana ret = btrfs_drop_extents(trans, root, inode, 3790f82a9901SFilipe Manana last_dest_end, destoff + len, 1); 3791f82a9901SFilipe Manana if (ret) { 3792f82a9901SFilipe Manana if (ret != -EOPNOTSUPP) 379366642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 37943a45bb20SJeff Mahoney btrfs_end_transaction(trans); 3795f82a9901SFilipe Manana goto out; 3796f82a9901SFilipe Manana } 3797a2f392e4SNikolay Borisov clone_update_extent_map(BTRFS_I(inode), trans, NULL, 3798a2f392e4SNikolay Borisov last_dest_end, 379914f59796SFilipe Manana destoff + len - last_dest_end); 3800f82a9901SFilipe Manana ret = clone_finish_inode_update(trans, inode, destoff + len, 38011c919a5eSMark Fasheh destoff, olen, no_time_update); 3802f82a9901SFilipe Manana } 3803f82a9901SFilipe Manana 3804f46b5a66SChristoph Hellwig out: 380532b7c687SMark Fasheh btrfs_free_path(path); 380615351955SDavid Sterba kvfree(buf); 380732b7c687SMark Fasheh return ret; 380832b7c687SMark Fasheh } 380932b7c687SMark Fasheh 38103db11b2eSZach Brown static noinline int btrfs_clone_files(struct file *file, struct file *file_src, 381132b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 381232b7c687SMark Fasheh { 381354563d41SAl Viro struct inode *inode = file_inode(file); 38143db11b2eSZach Brown struct inode *src = file_inode(file_src); 38150b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 381632b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 381732b7c687SMark Fasheh int ret; 381832b7c687SMark Fasheh u64 len = olen; 38190b246afaSJeff Mahoney u64 bs = fs_info->sb->s_blocksize; 38203db11b2eSZach Brown int same_inode = src == inode; 382132b7c687SMark Fasheh 382232b7c687SMark Fasheh /* 382332b7c687SMark Fasheh * TODO: 382432b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 382532b7c687SMark Fasheh * decompress into destination's address_space (the file offset 382632b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 382732b7c687SMark Fasheh * otherwise reinsert) a subrange. 382800fdf13aSLiu Bo * 382900fdf13aSLiu Bo * - split destination inode's inline extents. The inline extents can 383000fdf13aSLiu Bo * be either compressed or non-compressed. 383132b7c687SMark Fasheh */ 383232b7c687SMark Fasheh 383332b7c687SMark Fasheh if (btrfs_root_readonly(root)) 383432b7c687SMark Fasheh return -EROFS; 383532b7c687SMark Fasheh 38363db11b2eSZach Brown if (file_src->f_path.mnt != file->f_path.mnt || 38373db11b2eSZach Brown src->i_sb != inode->i_sb) 38383db11b2eSZach Brown return -EXDEV; 383932b7c687SMark Fasheh 384032b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 384132b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 384232b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 38433db11b2eSZach Brown return -EINVAL; 384432b7c687SMark Fasheh 384532b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 38463db11b2eSZach Brown return -EISDIR; 384732b7c687SMark Fasheh 384832b7c687SMark Fasheh if (!same_inode) { 3849293a8489SMark Fasheh btrfs_double_inode_lock(src, inode); 385032b7c687SMark Fasheh } else { 38515955102cSAl Viro inode_lock(src); 385232b7c687SMark Fasheh } 385332b7c687SMark Fasheh 385432b7c687SMark Fasheh /* determine range to clone */ 385532b7c687SMark Fasheh ret = -EINVAL; 385632b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 385732b7c687SMark Fasheh goto out_unlock; 385832b7c687SMark Fasheh if (len == 0) 385932b7c687SMark Fasheh olen = len = src->i_size - off; 386032b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 386132b7c687SMark Fasheh if (off + len == src->i_size) 386232b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 386332b7c687SMark Fasheh 3864ccccf3d6SFilipe Manana if (len == 0) { 3865ccccf3d6SFilipe Manana ret = 0; 3866ccccf3d6SFilipe Manana goto out_unlock; 3867ccccf3d6SFilipe Manana } 3868ccccf3d6SFilipe Manana 386932b7c687SMark Fasheh /* verify the end result is block aligned */ 387032b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 387132b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 387232b7c687SMark Fasheh goto out_unlock; 387332b7c687SMark Fasheh 387432b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 387532b7c687SMark Fasheh if (same_inode) { 387632b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 387732b7c687SMark Fasheh goto out_unlock; 387832b7c687SMark Fasheh } 387932b7c687SMark Fasheh 388032b7c687SMark Fasheh if (destoff > inode->i_size) { 388132b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 388232b7c687SMark Fasheh if (ret) 388332b7c687SMark Fasheh goto out_unlock; 388432b7c687SMark Fasheh } 388532b7c687SMark Fasheh 3886c125b8bfSFilipe Manana /* 3887c125b8bfSFilipe Manana * Lock the target range too. Right after we replace the file extent 3888c125b8bfSFilipe Manana * items in the fs tree (which now point to the cloned data), we might 3889c125b8bfSFilipe Manana * have a worker replace them with extent items relative to a write 3890c125b8bfSFilipe Manana * operation that was issued before this clone operation (i.e. confront 3891c125b8bfSFilipe Manana * with inode.c:btrfs_finish_ordered_io). 3892c125b8bfSFilipe Manana */ 3893c125b8bfSFilipe Manana if (same_inode) { 3894c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3895c125b8bfSFilipe Manana u64 lock_len = max_t(u64, off, destoff) + len - lock_start; 389632b7c687SMark Fasheh 3897e0bd70c6SFilipe Manana ret = lock_extent_range(src, lock_start, lock_len, true); 3898c125b8bfSFilipe Manana } else { 3899e0bd70c6SFilipe Manana ret = btrfs_double_extent_lock(src, off, inode, destoff, len, 3900e0bd70c6SFilipe Manana true); 3901e0bd70c6SFilipe Manana } 3902e0bd70c6SFilipe Manana ASSERT(ret == 0); 3903e0bd70c6SFilipe Manana if (WARN_ON(ret)) { 3904e0bd70c6SFilipe Manana /* ranges in the io trees already unlocked */ 3905e0bd70c6SFilipe Manana goto out_unlock; 3906c125b8bfSFilipe Manana } 390732b7c687SMark Fasheh 39081c919a5eSMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); 390932b7c687SMark Fasheh 3910c125b8bfSFilipe Manana if (same_inode) { 3911c125b8bfSFilipe Manana u64 lock_start = min_t(u64, off, destoff); 3912c125b8bfSFilipe Manana u64 lock_end = max_t(u64, off, destoff) + len - 1; 3913c125b8bfSFilipe Manana 3914c125b8bfSFilipe Manana unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); 3915c125b8bfSFilipe Manana } else { 3916293a8489SMark Fasheh btrfs_double_extent_unlock(src, off, inode, destoff, len); 3917c125b8bfSFilipe Manana } 3918c125b8bfSFilipe Manana /* 3919c125b8bfSFilipe Manana * Truncate page cache pages so that future reads will see the cloned 3920c125b8bfSFilipe Manana * data immediately and not the previous data. 3921c125b8bfSFilipe Manana */ 392265bfa658SChandan Rajendra truncate_inode_pages_range(&inode->i_data, 392309cbfeafSKirill A. Shutemov round_down(destoff, PAGE_SIZE), 392409cbfeafSKirill A. Shutemov round_up(destoff + len, PAGE_SIZE) - 1); 3925f46b5a66SChristoph Hellwig out_unlock: 3926293a8489SMark Fasheh if (!same_inode) 3927293a8489SMark Fasheh btrfs_double_inode_unlock(src, inode); 3928293a8489SMark Fasheh else 39295955102cSAl Viro inode_unlock(src); 39303db11b2eSZach Brown return ret; 39313db11b2eSZach Brown } 39323db11b2eSZach Brown 393304b38d60SChristoph Hellwig int btrfs_clone_file_range(struct file *src_file, loff_t off, 393404b38d60SChristoph Hellwig struct file *dst_file, loff_t destoff, u64 len) 39353db11b2eSZach Brown { 393604b38d60SChristoph Hellwig return btrfs_clone_files(dst_file, src_file, off, len, destoff); 3937c5c9cd4dSSage Weil } 3938c5c9cd4dSSage Weil 39396ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 39406ef5ed0dSJosef Bacik { 3941496ad9aaSAl Viro struct inode *inode = file_inode(file); 39420b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 39436ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 39446ef5ed0dSJosef Bacik struct btrfs_root *new_root; 39456ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 39466ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 39476ef5ed0dSJosef Bacik struct btrfs_path *path; 39486ef5ed0dSJosef Bacik struct btrfs_key location; 39496ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 39506ef5ed0dSJosef Bacik u64 objectid = 0; 39516ef5ed0dSJosef Bacik u64 dir_id; 39523c04ce01SMiao Xie int ret; 39536ef5ed0dSJosef Bacik 39546ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 39556ef5ed0dSJosef Bacik return -EPERM; 39566ef5ed0dSJosef Bacik 39573c04ce01SMiao Xie ret = mnt_want_write_file(file); 39583c04ce01SMiao Xie if (ret) 39593c04ce01SMiao Xie return ret; 39603c04ce01SMiao Xie 39613c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 39623c04ce01SMiao Xie ret = -EFAULT; 39633c04ce01SMiao Xie goto out; 39643c04ce01SMiao Xie } 39656ef5ed0dSJosef Bacik 39666ef5ed0dSJosef Bacik if (!objectid) 39671cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 39686ef5ed0dSJosef Bacik 39696ef5ed0dSJosef Bacik location.objectid = objectid; 39706ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 39716ef5ed0dSJosef Bacik location.offset = (u64)-1; 39726ef5ed0dSJosef Bacik 39730b246afaSJeff Mahoney new_root = btrfs_read_fs_root_no_name(fs_info, &location); 39743c04ce01SMiao Xie if (IS_ERR(new_root)) { 39753c04ce01SMiao Xie ret = PTR_ERR(new_root); 39763c04ce01SMiao Xie goto out; 39773c04ce01SMiao Xie } 39786d6d2829Ssatoru takeuchi if (!is_fstree(new_root->objectid)) { 39796d6d2829Ssatoru takeuchi ret = -ENOENT; 39806d6d2829Ssatoru takeuchi goto out; 39816d6d2829Ssatoru takeuchi } 39826ef5ed0dSJosef Bacik 39836ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 39843c04ce01SMiao Xie if (!path) { 39853c04ce01SMiao Xie ret = -ENOMEM; 39863c04ce01SMiao Xie goto out; 39873c04ce01SMiao Xie } 39886ef5ed0dSJosef Bacik path->leave_spinning = 1; 39896ef5ed0dSJosef Bacik 39906ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 399198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 39926ef5ed0dSJosef Bacik btrfs_free_path(path); 39933c04ce01SMiao Xie ret = PTR_ERR(trans); 39943c04ce01SMiao Xie goto out; 39956ef5ed0dSJosef Bacik } 39966ef5ed0dSJosef Bacik 39970b246afaSJeff Mahoney dir_id = btrfs_super_root_dir(fs_info->super_copy); 39980b246afaSJeff Mahoney di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, 39996ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 4000cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 40016ef5ed0dSJosef Bacik btrfs_free_path(path); 40023a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40030b246afaSJeff Mahoney btrfs_err(fs_info, 40045d163e0eSJeff Mahoney "Umm, you don't have the default diritem, this isn't going to work"); 40053c04ce01SMiao Xie ret = -ENOENT; 40063c04ce01SMiao Xie goto out; 40076ef5ed0dSJosef Bacik } 40086ef5ed0dSJosef Bacik 40096ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 40106ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 40116ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 40126ef5ed0dSJosef Bacik btrfs_free_path(path); 40136ef5ed0dSJosef Bacik 40140b246afaSJeff Mahoney btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); 40153a45bb20SJeff Mahoney btrfs_end_transaction(trans); 40163c04ce01SMiao Xie out: 40173c04ce01SMiao Xie mnt_drop_write_file(file); 40183c04ce01SMiao Xie return ret; 40196ef5ed0dSJosef Bacik } 40206ef5ed0dSJosef Bacik 40215af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 4022bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 4023bf5fc093SJosef Bacik { 4024bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 4025bf5fc093SJosef Bacik 4026bf5fc093SJosef Bacik space->total_bytes = 0; 4027bf5fc093SJosef Bacik space->used_bytes = 0; 4028bf5fc093SJosef Bacik space->flags = 0; 4029bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 4030bf5fc093SJosef Bacik space->flags = block_group->flags; 4031bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 4032bf5fc093SJosef Bacik space->used_bytes += 4033bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 4034bf5fc093SJosef Bacik } 4035bf5fc093SJosef Bacik } 4036bf5fc093SJosef Bacik 40372ff7e61eSJeff Mahoney static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, 40382ff7e61eSJeff Mahoney void __user *arg) 40391406e432SJosef Bacik { 40401406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 40411406e432SJosef Bacik struct btrfs_ioctl_space_info space; 40421406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 40437fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 404413f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 40451406e432SJosef Bacik struct btrfs_space_info *info; 4046315d8e98SColin Ian King static const u64 types[] = { 4047315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA, 4048bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 4049bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 4050315d8e98SColin Ian King BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA 4051315d8e98SColin Ian King }; 4052bf5fc093SJosef Bacik int num_types = 4; 40537fde62bfSChris Mason int alloc_size; 40541406e432SJosef Bacik int ret = 0; 405551788b1bSDan Rosenberg u64 slot_count = 0; 4056bf5fc093SJosef Bacik int i, c; 40571406e432SJosef Bacik 40581406e432SJosef Bacik if (copy_from_user(&space_args, 40591406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 40601406e432SJosef Bacik sizeof(space_args))) 40611406e432SJosef Bacik return -EFAULT; 40621406e432SJosef Bacik 4063bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4064bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4065bf5fc093SJosef Bacik 4066bf5fc093SJosef Bacik info = NULL; 40677fde62bfSChris Mason rcu_read_lock(); 40680b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4069bf5fc093SJosef Bacik list) { 4070bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4071bf5fc093SJosef Bacik info = tmp; 4072bf5fc093SJosef Bacik break; 4073bf5fc093SJosef Bacik } 4074bf5fc093SJosef Bacik } 40757fde62bfSChris Mason rcu_read_unlock(); 40761406e432SJosef Bacik 4077bf5fc093SJosef Bacik if (!info) 4078bf5fc093SJosef Bacik continue; 4079bf5fc093SJosef Bacik 4080bf5fc093SJosef Bacik down_read(&info->groups_sem); 4081bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4082bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 4083bf5fc093SJosef Bacik slot_count++; 4084bf5fc093SJosef Bacik } 4085bf5fc093SJosef Bacik up_read(&info->groups_sem); 4086bf5fc093SJosef Bacik } 4087bf5fc093SJosef Bacik 408836523e95SDavid Sterba /* 408936523e95SDavid Sterba * Global block reserve, exported as a space_info 409036523e95SDavid Sterba */ 409136523e95SDavid Sterba slot_count++; 409236523e95SDavid Sterba 40937fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 40947fde62bfSChris Mason if (space_args.space_slots == 0) { 40957fde62bfSChris Mason space_args.total_spaces = slot_count; 40967fde62bfSChris Mason goto out; 40977fde62bfSChris Mason } 4098bf5fc093SJosef Bacik 409951788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 4100bf5fc093SJosef Bacik 41017fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 4102bf5fc093SJosef Bacik 41037fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 41047fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 41057fde62bfSChris Mason */ 410609cbfeafSKirill A. Shutemov if (alloc_size > PAGE_SIZE) 41077fde62bfSChris Mason return -ENOMEM; 41087fde62bfSChris Mason 41097fde62bfSChris Mason space_args.total_spaces = 0; 41108d2db785SDavid Sterba dest = kmalloc(alloc_size, GFP_KERNEL); 41117fde62bfSChris Mason if (!dest) 41127fde62bfSChris Mason return -ENOMEM; 41137fde62bfSChris Mason dest_orig = dest; 41147fde62bfSChris Mason 41157fde62bfSChris Mason /* now we have a buffer to copy into */ 4116bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 4117bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 4118bf5fc093SJosef Bacik 411951788b1bSDan Rosenberg if (!slot_count) 412051788b1bSDan Rosenberg break; 412151788b1bSDan Rosenberg 4122bf5fc093SJosef Bacik info = NULL; 41231406e432SJosef Bacik rcu_read_lock(); 41240b246afaSJeff Mahoney list_for_each_entry_rcu(tmp, &fs_info->space_info, 4125bf5fc093SJosef Bacik list) { 4126bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 4127bf5fc093SJosef Bacik info = tmp; 41287fde62bfSChris Mason break; 4129bf5fc093SJosef Bacik } 4130bf5fc093SJosef Bacik } 4131bf5fc093SJosef Bacik rcu_read_unlock(); 41327fde62bfSChris Mason 4133bf5fc093SJosef Bacik if (!info) 4134bf5fc093SJosef Bacik continue; 4135bf5fc093SJosef Bacik down_read(&info->groups_sem); 4136bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 4137bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 41385af3e8ccSStefan Behrens btrfs_get_block_group_info( 41395af3e8ccSStefan Behrens &info->block_groups[c], &space); 41407fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 41411406e432SJosef Bacik dest++; 41421406e432SJosef Bacik space_args.total_spaces++; 414351788b1bSDan Rosenberg slot_count--; 41441406e432SJosef Bacik } 414551788b1bSDan Rosenberg if (!slot_count) 414651788b1bSDan Rosenberg break; 4147bf5fc093SJosef Bacik } 4148bf5fc093SJosef Bacik up_read(&info->groups_sem); 4149bf5fc093SJosef Bacik } 41501406e432SJosef Bacik 415136523e95SDavid Sterba /* 415236523e95SDavid Sterba * Add global block reserve 415336523e95SDavid Sterba */ 415436523e95SDavid Sterba if (slot_count) { 41550b246afaSJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 415636523e95SDavid Sterba 415736523e95SDavid Sterba spin_lock(&block_rsv->lock); 415836523e95SDavid Sterba space.total_bytes = block_rsv->size; 415936523e95SDavid Sterba space.used_bytes = block_rsv->size - block_rsv->reserved; 416036523e95SDavid Sterba spin_unlock(&block_rsv->lock); 416136523e95SDavid Sterba space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; 416236523e95SDavid Sterba memcpy(dest, &space, sizeof(space)); 416336523e95SDavid Sterba space_args.total_spaces++; 416436523e95SDavid Sterba } 416536523e95SDavid Sterba 41662eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 41677fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 41687fde62bfSChris Mason 41697fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 41707fde62bfSChris Mason ret = -EFAULT; 41717fde62bfSChris Mason 41727fde62bfSChris Mason kfree(dest_orig); 41737fde62bfSChris Mason out: 41747fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 41751406e432SJosef Bacik ret = -EFAULT; 41761406e432SJosef Bacik 41771406e432SJosef Bacik return ret; 41781406e432SJosef Bacik } 41791406e432SJosef Bacik 41809a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 41819a8c28beSMiao Xie void __user *argp) 418246204592SSage Weil { 418346204592SSage Weil struct btrfs_trans_handle *trans; 418446204592SSage Weil u64 transid; 4185db5b493aSTsutomu Itoh int ret; 418646204592SSage Weil 4187d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 4188ff7c1d33SMiao Xie if (IS_ERR(trans)) { 4189ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 419098d5dc13STsutomu Itoh return PTR_ERR(trans); 4191ff7c1d33SMiao Xie 4192ff7c1d33SMiao Xie /* No running transaction, don't bother */ 4193ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 4194ff7c1d33SMiao Xie goto out; 4195ff7c1d33SMiao Xie } 419646204592SSage Weil transid = trans->transid; 41973a45bb20SJeff Mahoney ret = btrfs_commit_transaction_async(trans, 0); 41988b2b2d3cSTsutomu Itoh if (ret) { 41993a45bb20SJeff Mahoney btrfs_end_transaction(trans); 4200db5b493aSTsutomu Itoh return ret; 42018b2b2d3cSTsutomu Itoh } 4202ff7c1d33SMiao Xie out: 420346204592SSage Weil if (argp) 420446204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 420546204592SSage Weil return -EFAULT; 420646204592SSage Weil return 0; 420746204592SSage Weil } 420846204592SSage Weil 42092ff7e61eSJeff Mahoney static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, 42109a8c28beSMiao Xie void __user *argp) 421146204592SSage Weil { 421246204592SSage Weil u64 transid; 421346204592SSage Weil 421446204592SSage Weil if (argp) { 421546204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 421646204592SSage Weil return -EFAULT; 421746204592SSage Weil } else { 421846204592SSage Weil transid = 0; /* current trans */ 421946204592SSage Weil } 42202ff7e61eSJeff Mahoney return btrfs_wait_for_commit(fs_info, transid); 422146204592SSage Weil } 422246204592SSage Weil 4223b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4224475f6387SJan Schmidt { 42250b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 4226475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4227b8e95489SMiao Xie int ret; 4228475f6387SJan Schmidt 4229475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4230475f6387SJan Schmidt return -EPERM; 4231475f6387SJan Schmidt 4232475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4233475f6387SJan Schmidt if (IS_ERR(sa)) 4234475f6387SJan Schmidt return PTR_ERR(sa); 4235475f6387SJan Schmidt 4236b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 4237b8e95489SMiao Xie ret = mnt_want_write_file(file); 4238b8e95489SMiao Xie if (ret) 4239b8e95489SMiao Xie goto out; 4240b8e95489SMiao Xie } 4241b8e95489SMiao Xie 42420b246afaSJeff Mahoney ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, 424363a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 424463a212abSStefan Behrens 0); 4245475f6387SJan Schmidt 4246475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4247475f6387SJan Schmidt ret = -EFAULT; 4248475f6387SJan Schmidt 4249b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 4250b8e95489SMiao Xie mnt_drop_write_file(file); 4251b8e95489SMiao Xie out: 4252475f6387SJan Schmidt kfree(sa); 4253475f6387SJan Schmidt return ret; 4254475f6387SJan Schmidt } 4255475f6387SJan Schmidt 42562ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) 4257475f6387SJan Schmidt { 4258475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4259475f6387SJan Schmidt return -EPERM; 4260475f6387SJan Schmidt 42612ff7e61eSJeff Mahoney return btrfs_scrub_cancel(fs_info); 4262475f6387SJan Schmidt } 4263475f6387SJan Schmidt 42642ff7e61eSJeff Mahoney static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, 4265475f6387SJan Schmidt void __user *arg) 4266475f6387SJan Schmidt { 4267475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 4268475f6387SJan Schmidt int ret; 4269475f6387SJan Schmidt 4270475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4271475f6387SJan Schmidt return -EPERM; 4272475f6387SJan Schmidt 4273475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 4274475f6387SJan Schmidt if (IS_ERR(sa)) 4275475f6387SJan Schmidt return PTR_ERR(sa); 4276475f6387SJan Schmidt 42772ff7e61eSJeff Mahoney ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); 4278475f6387SJan Schmidt 4279475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 4280475f6387SJan Schmidt ret = -EFAULT; 4281475f6387SJan Schmidt 4282475f6387SJan Schmidt kfree(sa); 4283475f6387SJan Schmidt return ret; 4284475f6387SJan Schmidt } 4285475f6387SJan Schmidt 42862ff7e61eSJeff Mahoney static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, 4287b27f7c0cSDavid Sterba void __user *arg) 4288c11d2c23SStefan Behrens { 4289c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 4290c11d2c23SStefan Behrens int ret; 4291c11d2c23SStefan Behrens 4292c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 4293c11d2c23SStefan Behrens if (IS_ERR(sa)) 4294c11d2c23SStefan Behrens return PTR_ERR(sa); 4295c11d2c23SStefan Behrens 4296b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 4297b27f7c0cSDavid Sterba kfree(sa); 4298b27f7c0cSDavid Sterba return -EPERM; 4299b27f7c0cSDavid Sterba } 4300b27f7c0cSDavid Sterba 43012ff7e61eSJeff Mahoney ret = btrfs_get_dev_stats(fs_info, sa); 4302c11d2c23SStefan Behrens 4303c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 4304c11d2c23SStefan Behrens ret = -EFAULT; 4305c11d2c23SStefan Behrens 4306c11d2c23SStefan Behrens kfree(sa); 4307c11d2c23SStefan Behrens return ret; 4308c11d2c23SStefan Behrens } 4309c11d2c23SStefan Behrens 43102ff7e61eSJeff Mahoney static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, 43112ff7e61eSJeff Mahoney void __user *arg) 43123f6bcfbdSStefan Behrens { 43133f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 43143f6bcfbdSStefan Behrens int ret; 43153f6bcfbdSStefan Behrens 43163f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 43173f6bcfbdSStefan Behrens return -EPERM; 43183f6bcfbdSStefan Behrens 43193f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 43203f6bcfbdSStefan Behrens if (IS_ERR(p)) 43213f6bcfbdSStefan Behrens return PTR_ERR(p); 43223f6bcfbdSStefan Behrens 43233f6bcfbdSStefan Behrens switch (p->cmd) { 43243f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4325bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) { 4326adfa97cbSIlya Dryomov ret = -EROFS; 4327adfa97cbSIlya Dryomov goto out; 4328adfa97cbSIlya Dryomov } 4329171938e5SDavid Sterba if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4330e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 43313f6bcfbdSStefan Behrens } else { 43322ff7e61eSJeff Mahoney ret = btrfs_dev_replace_by_ioctl(fs_info, p); 4333171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 43343f6bcfbdSStefan Behrens } 43353f6bcfbdSStefan Behrens break; 43363f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 43370b246afaSJeff Mahoney btrfs_dev_replace_status(fs_info, p); 43383f6bcfbdSStefan Behrens ret = 0; 43393f6bcfbdSStefan Behrens break; 43403f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 434117d202b9SAnand Jain p->result = btrfs_dev_replace_cancel(fs_info); 434297282031SAnand Jain ret = 0; 43433f6bcfbdSStefan Behrens break; 43443f6bcfbdSStefan Behrens default: 43453f6bcfbdSStefan Behrens ret = -EINVAL; 43463f6bcfbdSStefan Behrens break; 43473f6bcfbdSStefan Behrens } 43483f6bcfbdSStefan Behrens 43493f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 43503f6bcfbdSStefan Behrens ret = -EFAULT; 4351adfa97cbSIlya Dryomov out: 43523f6bcfbdSStefan Behrens kfree(p); 43533f6bcfbdSStefan Behrens return ret; 43543f6bcfbdSStefan Behrens } 43553f6bcfbdSStefan Behrens 4356d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 4357d7728c96SJan Schmidt { 4358d7728c96SJan Schmidt int ret = 0; 4359d7728c96SJan Schmidt int i; 4360740c3d22SChris Mason u64 rel_ptr; 4361d7728c96SJan Schmidt int size; 4362806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 4363d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 4364d7728c96SJan Schmidt struct btrfs_path *path; 4365d7728c96SJan Schmidt 436682b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 4367d7728c96SJan Schmidt return -EPERM; 4368d7728c96SJan Schmidt 4369d7728c96SJan Schmidt path = btrfs_alloc_path(); 4370d7728c96SJan Schmidt if (!path) { 4371d7728c96SJan Schmidt ret = -ENOMEM; 4372d7728c96SJan Schmidt goto out; 4373d7728c96SJan Schmidt } 4374d7728c96SJan Schmidt 4375d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 4376d7728c96SJan Schmidt if (IS_ERR(ipa)) { 4377d7728c96SJan Schmidt ret = PTR_ERR(ipa); 4378d7728c96SJan Schmidt ipa = NULL; 4379d7728c96SJan Schmidt goto out; 4380d7728c96SJan Schmidt } 4381d7728c96SJan Schmidt 4382d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 4383d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 4384d7728c96SJan Schmidt if (IS_ERR(ipath)) { 4385d7728c96SJan Schmidt ret = PTR_ERR(ipath); 4386d7728c96SJan Schmidt ipath = NULL; 4387d7728c96SJan Schmidt goto out; 4388d7728c96SJan Schmidt } 4389d7728c96SJan Schmidt 4390d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 4391d7728c96SJan Schmidt if (ret < 0) 4392d7728c96SJan Schmidt goto out; 4393d7728c96SJan Schmidt 4394d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 4395745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 4396745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 4397740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 4398d7728c96SJan Schmidt } 4399d7728c96SJan Schmidt 4400718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, 4401718dc5faSOmar Sandoval ipath->fspath, size); 4402d7728c96SJan Schmidt if (ret) { 4403d7728c96SJan Schmidt ret = -EFAULT; 4404d7728c96SJan Schmidt goto out; 4405d7728c96SJan Schmidt } 4406d7728c96SJan Schmidt 4407d7728c96SJan Schmidt out: 4408d7728c96SJan Schmidt btrfs_free_path(path); 4409d7728c96SJan Schmidt free_ipath(ipath); 4410d7728c96SJan Schmidt kfree(ipa); 4411d7728c96SJan Schmidt 4412d7728c96SJan Schmidt return ret; 4413d7728c96SJan Schmidt } 4414d7728c96SJan Schmidt 4415d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 4416d7728c96SJan Schmidt { 4417d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 4418d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 4419d7728c96SJan Schmidt 4420d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 4421d7728c96SJan Schmidt inodes->bytes_left -= c; 4422d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 4423d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 4424d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 4425d7728c96SJan Schmidt inodes->elem_cnt += 3; 4426d7728c96SJan Schmidt } else { 4427d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 4428d7728c96SJan Schmidt inodes->bytes_left = 0; 4429d7728c96SJan Schmidt inodes->elem_missed += 3; 4430d7728c96SJan Schmidt } 4431d7728c96SJan Schmidt 4432d7728c96SJan Schmidt return 0; 4433d7728c96SJan Schmidt } 4434d7728c96SJan Schmidt 44352ff7e61eSJeff Mahoney static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, 4436d24a67b2SZygo Blaxell void __user *arg, int version) 4437d7728c96SJan Schmidt { 4438d7728c96SJan Schmidt int ret = 0; 4439d7728c96SJan Schmidt int size; 4440d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 4441d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 4442d7728c96SJan Schmidt struct btrfs_path *path = NULL; 4443d24a67b2SZygo Blaxell bool ignore_offset; 4444d7728c96SJan Schmidt 4445d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 4446d7728c96SJan Schmidt return -EPERM; 4447d7728c96SJan Schmidt 4448d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 44497b9ea627SShailendra Verma if (IS_ERR(loi)) 44507b9ea627SShailendra Verma return PTR_ERR(loi); 4451d7728c96SJan Schmidt 4452d24a67b2SZygo Blaxell if (version == 1) { 4453d24a67b2SZygo Blaxell ignore_offset = false; 4454b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_64K); 4455d24a67b2SZygo Blaxell } else { 4456d24a67b2SZygo Blaxell /* All reserved bits must be 0 for now */ 4457d24a67b2SZygo Blaxell if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { 4458d24a67b2SZygo Blaxell ret = -EINVAL; 4459d24a67b2SZygo Blaxell goto out_loi; 4460d24a67b2SZygo Blaxell } 4461d24a67b2SZygo Blaxell /* Only accept flags we have defined so far */ 4462d24a67b2SZygo Blaxell if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { 4463d24a67b2SZygo Blaxell ret = -EINVAL; 4464d24a67b2SZygo Blaxell goto out_loi; 4465d24a67b2SZygo Blaxell } 4466d24a67b2SZygo Blaxell ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; 4467b115e3bcSZygo Blaxell size = min_t(u32, loi->size, SZ_16M); 4468d24a67b2SZygo Blaxell } 4469d24a67b2SZygo Blaxell 4470d7728c96SJan Schmidt path = btrfs_alloc_path(); 4471d7728c96SJan Schmidt if (!path) { 4472d7728c96SJan Schmidt ret = -ENOMEM; 4473d7728c96SJan Schmidt goto out; 4474d7728c96SJan Schmidt } 4475d7728c96SJan Schmidt 4476d7728c96SJan Schmidt inodes = init_data_container(size); 4477d7728c96SJan Schmidt if (IS_ERR(inodes)) { 4478d7728c96SJan Schmidt ret = PTR_ERR(inodes); 4479d7728c96SJan Schmidt inodes = NULL; 4480d7728c96SJan Schmidt goto out; 4481d7728c96SJan Schmidt } 4482d7728c96SJan Schmidt 44832ff7e61eSJeff Mahoney ret = iterate_inodes_from_logical(loi->logical, fs_info, path, 4484d24a67b2SZygo Blaxell build_ino_list, inodes, ignore_offset); 4485df031f07SLiu Bo if (ret == -EINVAL) 4486d7728c96SJan Schmidt ret = -ENOENT; 4487d7728c96SJan Schmidt if (ret < 0) 4488d7728c96SJan Schmidt goto out; 4489d7728c96SJan Schmidt 4490718dc5faSOmar Sandoval ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, 4491718dc5faSOmar Sandoval size); 4492d7728c96SJan Schmidt if (ret) 4493d7728c96SJan Schmidt ret = -EFAULT; 4494d7728c96SJan Schmidt 4495d7728c96SJan Schmidt out: 4496d7728c96SJan Schmidt btrfs_free_path(path); 4497f54de068SDavid Sterba kvfree(inodes); 4498d24a67b2SZygo Blaxell out_loi: 4499d7728c96SJan Schmidt kfree(loi); 4500d7728c96SJan Schmidt 4501d7728c96SJan Schmidt return ret; 4502d7728c96SJan Schmidt } 4503d7728c96SJan Schmidt 450419a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 4505c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 4506c9e9f97bSIlya Dryomov { 4507c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 4508c9e9f97bSIlya Dryomov 4509c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 4510c9e9f97bSIlya Dryomov 4511837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 4512837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 4513837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 4514837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 4515a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 4516a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 4517837d5b6eSIlya Dryomov 4518c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 4519c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 4520c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 452119a39dceSIlya Dryomov 452219a39dceSIlya Dryomov if (lock) { 452319a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 452419a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 452519a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 452619a39dceSIlya Dryomov } else { 452719a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 452819a39dceSIlya Dryomov } 4529c9e9f97bSIlya Dryomov } 4530c9e9f97bSIlya Dryomov 45319ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 4532c9e9f97bSIlya Dryomov { 4533496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4534c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 4535c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 4536c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 4537ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 4538c9e9f97bSIlya Dryomov int ret; 4539c9e9f97bSIlya Dryomov 4540c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4541c9e9f97bSIlya Dryomov return -EPERM; 4542c9e9f97bSIlya Dryomov 4543e54bfa31SLiu Bo ret = mnt_want_write_file(file); 45449ba1f6e4SLiu Bo if (ret) 45459ba1f6e4SLiu Bo return ret; 45469ba1f6e4SLiu Bo 4547ed0fb78fSIlya Dryomov again: 4548171938e5SDavid Sterba if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { 4549c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 4550c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4551ed0fb78fSIlya Dryomov need_unlock = true; 4552ed0fb78fSIlya Dryomov goto locked; 4553ed0fb78fSIlya Dryomov } 4554ed0fb78fSIlya Dryomov 4555ed0fb78fSIlya Dryomov /* 455601327610SNicholas D Steeves * mut. excl. ops lock is locked. Three possibilities: 4557ed0fb78fSIlya Dryomov * (1) some other op is running 4558ed0fb78fSIlya Dryomov * (2) balance is running 4559ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 4560ed0fb78fSIlya Dryomov */ 4561ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4562ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4563ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 4564ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 4565ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4566ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 4567ed0fb78fSIlya Dryomov goto again; 4568ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 4569ed0fb78fSIlya Dryomov 4570ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 4571ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 4572ed0fb78fSIlya Dryomov /* this is (3) */ 4573ed0fb78fSIlya Dryomov need_unlock = false; 4574ed0fb78fSIlya Dryomov goto locked; 4575ed0fb78fSIlya Dryomov } 4576ed0fb78fSIlya Dryomov 4577ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4578ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4579ed0fb78fSIlya Dryomov goto again; 4580ed0fb78fSIlya Dryomov } else { 4581ed0fb78fSIlya Dryomov /* this is (2) */ 4582ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4583ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 4584ed0fb78fSIlya Dryomov goto out; 4585ed0fb78fSIlya Dryomov } 4586ed0fb78fSIlya Dryomov } else { 4587ed0fb78fSIlya Dryomov /* this is (1) */ 4588ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4589e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4590ed0fb78fSIlya Dryomov goto out; 4591ed0fb78fSIlya Dryomov } 4592ed0fb78fSIlya Dryomov 4593ed0fb78fSIlya Dryomov locked: 4594171938e5SDavid Sterba BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); 4595c9e9f97bSIlya Dryomov 4596c9e9f97bSIlya Dryomov if (arg) { 4597c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 4598c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 4599c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 4600ed0fb78fSIlya Dryomov goto out_unlock; 4601c9e9f97bSIlya Dryomov } 4602de322263SIlya Dryomov 4603de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 4604de322263SIlya Dryomov if (!fs_info->balance_ctl) { 4605de322263SIlya Dryomov ret = -ENOTCONN; 4606de322263SIlya Dryomov goto out_bargs; 4607de322263SIlya Dryomov } 4608de322263SIlya Dryomov 4609de322263SIlya Dryomov bctl = fs_info->balance_ctl; 4610de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 4611de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 4612de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 4613de322263SIlya Dryomov 4614de322263SIlya Dryomov goto do_balance; 4615de322263SIlya Dryomov } 4616c9e9f97bSIlya Dryomov } else { 4617c9e9f97bSIlya Dryomov bargs = NULL; 4618c9e9f97bSIlya Dryomov } 4619c9e9f97bSIlya Dryomov 4620ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 4621837d5b6eSIlya Dryomov ret = -EINPROGRESS; 4622837d5b6eSIlya Dryomov goto out_bargs; 4623837d5b6eSIlya Dryomov } 4624837d5b6eSIlya Dryomov 46258d2db785SDavid Sterba bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); 4626c9e9f97bSIlya Dryomov if (!bctl) { 4627c9e9f97bSIlya Dryomov ret = -ENOMEM; 4628c9e9f97bSIlya Dryomov goto out_bargs; 4629c9e9f97bSIlya Dryomov } 4630c9e9f97bSIlya Dryomov 4631c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4632c9e9f97bSIlya Dryomov if (arg) { 4633c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4634c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4635c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4636c9e9f97bSIlya Dryomov 4637c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4638f43ffb60SIlya Dryomov } else { 4639f43ffb60SIlya Dryomov /* balance everything - no filters */ 4640f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4641c9e9f97bSIlya Dryomov } 4642c9e9f97bSIlya Dryomov 46438eb93459SDavid Sterba if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { 46448eb93459SDavid Sterba ret = -EINVAL; 46450f89abf5SChristian Engelmayer goto out_bctl; 46468eb93459SDavid Sterba } 46478eb93459SDavid Sterba 4648de322263SIlya Dryomov do_balance: 4649c9e9f97bSIlya Dryomov /* 4650171938e5SDavid Sterba * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP 4651ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4652ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4653171938e5SDavid Sterba * free_fs_info. The flag is cleared in __cancel_balance. 4654c9e9f97bSIlya Dryomov */ 4655ed0fb78fSIlya Dryomov need_unlock = false; 4656ed0fb78fSIlya Dryomov 4657ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 46580f89abf5SChristian Engelmayer bctl = NULL; 4659ed0fb78fSIlya Dryomov 4660c9e9f97bSIlya Dryomov if (arg) { 4661c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4662c9e9f97bSIlya Dryomov ret = -EFAULT; 4663c9e9f97bSIlya Dryomov } 4664c9e9f97bSIlya Dryomov 46650f89abf5SChristian Engelmayer out_bctl: 46660f89abf5SChristian Engelmayer kfree(bctl); 4667c9e9f97bSIlya Dryomov out_bargs: 4668c9e9f97bSIlya Dryomov kfree(bargs); 4669ed0fb78fSIlya Dryomov out_unlock: 4670c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4671c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4672ed0fb78fSIlya Dryomov if (need_unlock) 4673171938e5SDavid Sterba clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); 4674ed0fb78fSIlya Dryomov out: 4675e54bfa31SLiu Bo mnt_drop_write_file(file); 4676c9e9f97bSIlya Dryomov return ret; 4677c9e9f97bSIlya Dryomov } 4678c9e9f97bSIlya Dryomov 46792ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) 4680837d5b6eSIlya Dryomov { 4681837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4682837d5b6eSIlya Dryomov return -EPERM; 4683837d5b6eSIlya Dryomov 4684837d5b6eSIlya Dryomov switch (cmd) { 4685837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 46860b246afaSJeff Mahoney return btrfs_pause_balance(fs_info); 4687a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 46880b246afaSJeff Mahoney return btrfs_cancel_balance(fs_info); 4689837d5b6eSIlya Dryomov } 4690837d5b6eSIlya Dryomov 4691837d5b6eSIlya Dryomov return -EINVAL; 4692837d5b6eSIlya Dryomov } 4693837d5b6eSIlya Dryomov 46942ff7e61eSJeff Mahoney static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, 469519a39dceSIlya Dryomov void __user *arg) 469619a39dceSIlya Dryomov { 469719a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 469819a39dceSIlya Dryomov int ret = 0; 469919a39dceSIlya Dryomov 470019a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 470119a39dceSIlya Dryomov return -EPERM; 470219a39dceSIlya Dryomov 470319a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 470419a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 470519a39dceSIlya Dryomov ret = -ENOTCONN; 470619a39dceSIlya Dryomov goto out; 470719a39dceSIlya Dryomov } 470819a39dceSIlya Dryomov 47098d2db785SDavid Sterba bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); 471019a39dceSIlya Dryomov if (!bargs) { 471119a39dceSIlya Dryomov ret = -ENOMEM; 471219a39dceSIlya Dryomov goto out; 471319a39dceSIlya Dryomov } 471419a39dceSIlya Dryomov 471519a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 471619a39dceSIlya Dryomov 471719a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 471819a39dceSIlya Dryomov ret = -EFAULT; 471919a39dceSIlya Dryomov 472019a39dceSIlya Dryomov kfree(bargs); 472119a39dceSIlya Dryomov out: 472219a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 472319a39dceSIlya Dryomov return ret; 472419a39dceSIlya Dryomov } 472519a39dceSIlya Dryomov 4726905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 47275d13a37bSArne Jansen { 47280b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47290b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47305d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 47315d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 47325d13a37bSArne Jansen int ret; 47335d13a37bSArne Jansen int err; 47345d13a37bSArne Jansen 47355d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47365d13a37bSArne Jansen return -EPERM; 47375d13a37bSArne Jansen 4738905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4739905b0ddaSMiao Xie if (ret) 4740905b0ddaSMiao Xie return ret; 47415d13a37bSArne Jansen 47425d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4743905b0ddaSMiao Xie if (IS_ERR(sa)) { 4744905b0ddaSMiao Xie ret = PTR_ERR(sa); 4745905b0ddaSMiao Xie goto drop_write; 4746905b0ddaSMiao Xie } 47475d13a37bSArne Jansen 47480b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 47490b246afaSJeff Mahoney trans = btrfs_start_transaction(fs_info->tree_root, 2); 47505d13a37bSArne Jansen if (IS_ERR(trans)) { 47515d13a37bSArne Jansen ret = PTR_ERR(trans); 47525d13a37bSArne Jansen goto out; 47535d13a37bSArne Jansen } 47545d13a37bSArne Jansen 47555d13a37bSArne Jansen switch (sa->cmd) { 47565d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 47570b246afaSJeff Mahoney ret = btrfs_quota_enable(trans, fs_info); 47585d13a37bSArne Jansen break; 47595d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 47600b246afaSJeff Mahoney ret = btrfs_quota_disable(trans, fs_info); 47615d13a37bSArne Jansen break; 47625d13a37bSArne Jansen default: 47635d13a37bSArne Jansen ret = -EINVAL; 47645d13a37bSArne Jansen break; 47655d13a37bSArne Jansen } 47665d13a37bSArne Jansen 47673a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 47685d13a37bSArne Jansen if (err && !ret) 47695d13a37bSArne Jansen ret = err; 47705d13a37bSArne Jansen out: 47715d13a37bSArne Jansen kfree(sa); 47720b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 4773905b0ddaSMiao Xie drop_write: 4774905b0ddaSMiao Xie mnt_drop_write_file(file); 47755d13a37bSArne Jansen return ret; 47765d13a37bSArne Jansen } 47775d13a37bSArne Jansen 4778905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 47795d13a37bSArne Jansen { 47800b246afaSJeff Mahoney struct inode *inode = file_inode(file); 47810b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 47820b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 47835d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 47845d13a37bSArne Jansen struct btrfs_trans_handle *trans; 47855d13a37bSArne Jansen int ret; 47865d13a37bSArne Jansen int err; 47875d13a37bSArne Jansen 47885d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 47895d13a37bSArne Jansen return -EPERM; 47905d13a37bSArne Jansen 4791905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4792905b0ddaSMiao Xie if (ret) 4793905b0ddaSMiao Xie return ret; 47945d13a37bSArne Jansen 47955d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4796905b0ddaSMiao Xie if (IS_ERR(sa)) { 4797905b0ddaSMiao Xie ret = PTR_ERR(sa); 4798905b0ddaSMiao Xie goto drop_write; 4799905b0ddaSMiao Xie } 48005d13a37bSArne Jansen 48015d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48025d13a37bSArne Jansen if (IS_ERR(trans)) { 48035d13a37bSArne Jansen ret = PTR_ERR(trans); 48045d13a37bSArne Jansen goto out; 48055d13a37bSArne Jansen } 48065d13a37bSArne Jansen 48075d13a37bSArne Jansen if (sa->assign) { 48080b246afaSJeff Mahoney ret = btrfs_add_qgroup_relation(trans, fs_info, 48095d13a37bSArne Jansen sa->src, sa->dst); 48105d13a37bSArne Jansen } else { 48110b246afaSJeff Mahoney ret = btrfs_del_qgroup_relation(trans, fs_info, 48125d13a37bSArne Jansen sa->src, sa->dst); 48135d13a37bSArne Jansen } 48145d13a37bSArne Jansen 4815e082f563SQu Wenruo /* update qgroup status and info */ 48160b246afaSJeff Mahoney err = btrfs_run_qgroups(trans, fs_info); 4817e082f563SQu Wenruo if (err < 0) 48180b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, err, 4819ad8403dfSAnand Jain "failed to update qgroup status and info"); 48203a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 48215d13a37bSArne Jansen if (err && !ret) 48225d13a37bSArne Jansen ret = err; 48235d13a37bSArne Jansen 48245d13a37bSArne Jansen out: 48255d13a37bSArne Jansen kfree(sa); 4826905b0ddaSMiao Xie drop_write: 4827905b0ddaSMiao Xie mnt_drop_write_file(file); 48285d13a37bSArne Jansen return ret; 48295d13a37bSArne Jansen } 48305d13a37bSArne Jansen 4831905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 48325d13a37bSArne Jansen { 48330b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48350b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48365d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 48375d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48385d13a37bSArne Jansen int ret; 48395d13a37bSArne Jansen int err; 48405d13a37bSArne Jansen 48415d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48425d13a37bSArne Jansen return -EPERM; 48435d13a37bSArne Jansen 4844905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4845905b0ddaSMiao Xie if (ret) 4846905b0ddaSMiao Xie return ret; 48475d13a37bSArne Jansen 48485d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4849905b0ddaSMiao Xie if (IS_ERR(sa)) { 4850905b0ddaSMiao Xie ret = PTR_ERR(sa); 4851905b0ddaSMiao Xie goto drop_write; 4852905b0ddaSMiao Xie } 48535d13a37bSArne Jansen 4854d86e56cfSMiao Xie if (!sa->qgroupid) { 4855d86e56cfSMiao Xie ret = -EINVAL; 4856d86e56cfSMiao Xie goto out; 4857d86e56cfSMiao Xie } 4858d86e56cfSMiao Xie 48595d13a37bSArne Jansen trans = btrfs_join_transaction(root); 48605d13a37bSArne Jansen if (IS_ERR(trans)) { 48615d13a37bSArne Jansen ret = PTR_ERR(trans); 48625d13a37bSArne Jansen goto out; 48635d13a37bSArne Jansen } 48645d13a37bSArne Jansen 48655d13a37bSArne Jansen if (sa->create) { 48660b246afaSJeff Mahoney ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid); 48675d13a37bSArne Jansen } else { 48680b246afaSJeff Mahoney ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid); 48695d13a37bSArne Jansen } 48705d13a37bSArne Jansen 48713a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 48725d13a37bSArne Jansen if (err && !ret) 48735d13a37bSArne Jansen ret = err; 48745d13a37bSArne Jansen 48755d13a37bSArne Jansen out: 48765d13a37bSArne Jansen kfree(sa); 4877905b0ddaSMiao Xie drop_write: 4878905b0ddaSMiao Xie mnt_drop_write_file(file); 48795d13a37bSArne Jansen return ret; 48805d13a37bSArne Jansen } 48815d13a37bSArne Jansen 4882905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 48835d13a37bSArne Jansen { 48840b246afaSJeff Mahoney struct inode *inode = file_inode(file); 48850b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 48860b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 48875d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 48885d13a37bSArne Jansen struct btrfs_trans_handle *trans; 48895d13a37bSArne Jansen int ret; 48905d13a37bSArne Jansen int err; 48915d13a37bSArne Jansen u64 qgroupid; 48925d13a37bSArne Jansen 48935d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 48945d13a37bSArne Jansen return -EPERM; 48955d13a37bSArne Jansen 4896905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4897905b0ddaSMiao Xie if (ret) 4898905b0ddaSMiao Xie return ret; 48995d13a37bSArne Jansen 49005d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4901905b0ddaSMiao Xie if (IS_ERR(sa)) { 4902905b0ddaSMiao Xie ret = PTR_ERR(sa); 4903905b0ddaSMiao Xie goto drop_write; 4904905b0ddaSMiao Xie } 49055d13a37bSArne Jansen 49065d13a37bSArne Jansen trans = btrfs_join_transaction(root); 49075d13a37bSArne Jansen if (IS_ERR(trans)) { 49085d13a37bSArne Jansen ret = PTR_ERR(trans); 49095d13a37bSArne Jansen goto out; 49105d13a37bSArne Jansen } 49115d13a37bSArne Jansen 49125d13a37bSArne Jansen qgroupid = sa->qgroupid; 49135d13a37bSArne Jansen if (!qgroupid) { 49145d13a37bSArne Jansen /* take the current subvol as qgroup */ 49155d13a37bSArne Jansen qgroupid = root->root_key.objectid; 49165d13a37bSArne Jansen } 49175d13a37bSArne Jansen 49180b246afaSJeff Mahoney ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim); 49195d13a37bSArne Jansen 49203a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 49215d13a37bSArne Jansen if (err && !ret) 49225d13a37bSArne Jansen ret = err; 49235d13a37bSArne Jansen 49245d13a37bSArne Jansen out: 49255d13a37bSArne Jansen kfree(sa); 4926905b0ddaSMiao Xie drop_write: 4927905b0ddaSMiao Xie mnt_drop_write_file(file); 49285d13a37bSArne Jansen return ret; 49295d13a37bSArne Jansen } 49305d13a37bSArne Jansen 49312f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 49322f232036SJan Schmidt { 49330b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49352f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49362f232036SJan Schmidt int ret; 49372f232036SJan Schmidt 49382f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49392f232036SJan Schmidt return -EPERM; 49402f232036SJan Schmidt 49412f232036SJan Schmidt ret = mnt_want_write_file(file); 49422f232036SJan Schmidt if (ret) 49432f232036SJan Schmidt return ret; 49442f232036SJan Schmidt 49452f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 49462f232036SJan Schmidt if (IS_ERR(qsa)) { 49472f232036SJan Schmidt ret = PTR_ERR(qsa); 49482f232036SJan Schmidt goto drop_write; 49492f232036SJan Schmidt } 49502f232036SJan Schmidt 49512f232036SJan Schmidt if (qsa->flags) { 49522f232036SJan Schmidt ret = -EINVAL; 49532f232036SJan Schmidt goto out; 49542f232036SJan Schmidt } 49552f232036SJan Schmidt 49560b246afaSJeff Mahoney ret = btrfs_qgroup_rescan(fs_info); 49572f232036SJan Schmidt 49582f232036SJan Schmidt out: 49592f232036SJan Schmidt kfree(qsa); 49602f232036SJan Schmidt drop_write: 49612f232036SJan Schmidt mnt_drop_write_file(file); 49622f232036SJan Schmidt return ret; 49632f232036SJan Schmidt } 49642f232036SJan Schmidt 49652f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 49662f232036SJan Schmidt { 49670b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49680b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 49692f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 49702f232036SJan Schmidt int ret = 0; 49712f232036SJan Schmidt 49722f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 49732f232036SJan Schmidt return -EPERM; 49742f232036SJan Schmidt 49758d2db785SDavid Sterba qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); 49762f232036SJan Schmidt if (!qsa) 49772f232036SJan Schmidt return -ENOMEM; 49782f232036SJan Schmidt 49790b246afaSJeff Mahoney if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 49802f232036SJan Schmidt qsa->flags = 1; 49810b246afaSJeff Mahoney qsa->progress = fs_info->qgroup_rescan_progress.objectid; 49822f232036SJan Schmidt } 49832f232036SJan Schmidt 49842f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 49852f232036SJan Schmidt ret = -EFAULT; 49862f232036SJan Schmidt 49872f232036SJan Schmidt kfree(qsa); 49882f232036SJan Schmidt return ret; 49892f232036SJan Schmidt } 49902f232036SJan Schmidt 499157254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 499257254b6eSJan Schmidt { 49930b246afaSJeff Mahoney struct inode *inode = file_inode(file); 49940b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 499557254b6eSJan Schmidt 499657254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 499757254b6eSJan Schmidt return -EPERM; 499857254b6eSJan Schmidt 49990b246afaSJeff Mahoney return btrfs_qgroup_wait_for_completion(fs_info, true); 500057254b6eSJan Schmidt } 500157254b6eSJan Schmidt 5002abccd00fSHugo Mills static long _btrfs_ioctl_set_received_subvol(struct file *file, 5003abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa) 50048ea05e3aSAlexander Block { 5005496ad9aaSAl Viro struct inode *inode = file_inode(file); 50060b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 50078ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 50088ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 50098ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 5010c2050a45SDeepa Dinamani struct timespec ct = current_time(inode); 50118ea05e3aSAlexander Block int ret = 0; 5012dd5f9615SStefan Behrens int received_uuid_changed; 50138ea05e3aSAlexander Block 5014bd60ea0fSDavid Sterba if (!inode_owner_or_capable(inode)) 5015bd60ea0fSDavid Sterba return -EPERM; 5016bd60ea0fSDavid Sterba 50178ea05e3aSAlexander Block ret = mnt_want_write_file(file); 50188ea05e3aSAlexander Block if (ret < 0) 50198ea05e3aSAlexander Block return ret; 50208ea05e3aSAlexander Block 50210b246afaSJeff Mahoney down_write(&fs_info->subvol_sem); 50228ea05e3aSAlexander Block 50234a0cc7caSNikolay Borisov if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { 50248ea05e3aSAlexander Block ret = -EINVAL; 50258ea05e3aSAlexander Block goto out; 50268ea05e3aSAlexander Block } 50278ea05e3aSAlexander Block 50288ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 50298ea05e3aSAlexander Block ret = -EROFS; 50308ea05e3aSAlexander Block goto out; 50318ea05e3aSAlexander Block } 50328ea05e3aSAlexander Block 5033dd5f9615SStefan Behrens /* 5034dd5f9615SStefan Behrens * 1 - root item 5035dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 5036dd5f9615SStefan Behrens */ 5037dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 50388ea05e3aSAlexander Block if (IS_ERR(trans)) { 50398ea05e3aSAlexander Block ret = PTR_ERR(trans); 50408ea05e3aSAlexander Block trans = NULL; 50418ea05e3aSAlexander Block goto out; 50428ea05e3aSAlexander Block } 50438ea05e3aSAlexander Block 50448ea05e3aSAlexander Block sa->rtransid = trans->transid; 50458ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 50468ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 50478ea05e3aSAlexander Block 5048dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 5049dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 5050dd5f9615SStefan Behrens if (received_uuid_changed && 5051d87ff758SNikolay Borisov !btrfs_is_empty_uuid(root_item->received_uuid)) { 5052d87ff758SNikolay Borisov ret = btrfs_uuid_tree_rem(trans, fs_info, 5053d87ff758SNikolay Borisov root_item->received_uuid, 5054dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5055dd5f9615SStefan Behrens root->root_key.objectid); 5056d87ff758SNikolay Borisov if (ret && ret != -ENOENT) { 5057d87ff758SNikolay Borisov btrfs_abort_transaction(trans, ret); 5058d87ff758SNikolay Borisov btrfs_end_transaction(trans); 5059d87ff758SNikolay Borisov goto out; 5060d87ff758SNikolay Borisov } 5061d87ff758SNikolay Borisov } 50628ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 50638ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 50648ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 50653cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 50663cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 50673cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 50683cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 50698ea05e3aSAlexander Block 50700b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 50718ea05e3aSAlexander Block &root->root_key, &root->root_item); 50728ea05e3aSAlexander Block if (ret < 0) { 50733a45bb20SJeff Mahoney btrfs_end_transaction(trans); 50748ea05e3aSAlexander Block goto out; 5075dd5f9615SStefan Behrens } 5076dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 50770b246afaSJeff Mahoney ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid, 5078dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5079dd5f9615SStefan Behrens root->root_key.objectid); 5080dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 508166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 5082efd38150SNikolay Borisov btrfs_end_transaction(trans); 5083dd5f9615SStefan Behrens goto out; 5084dd5f9615SStefan Behrens } 5085dd5f9615SStefan Behrens } 50863a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5087abccd00fSHugo Mills out: 50880b246afaSJeff Mahoney up_write(&fs_info->subvol_sem); 5089abccd00fSHugo Mills mnt_drop_write_file(file); 5090abccd00fSHugo Mills return ret; 5091abccd00fSHugo Mills } 5092abccd00fSHugo Mills 5093abccd00fSHugo Mills #ifdef CONFIG_64BIT 5094abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol_32(struct file *file, 5095abccd00fSHugo Mills void __user *arg) 5096abccd00fSHugo Mills { 5097abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; 5098abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *args64 = NULL; 5099abccd00fSHugo Mills int ret = 0; 5100abccd00fSHugo Mills 5101abccd00fSHugo Mills args32 = memdup_user(arg, sizeof(*args32)); 51027b9ea627SShailendra Verma if (IS_ERR(args32)) 51037b9ea627SShailendra Verma return PTR_ERR(args32); 5104abccd00fSHugo Mills 51058d2db785SDavid Sterba args64 = kmalloc(sizeof(*args64), GFP_KERNEL); 510684dbeb87SDan Carpenter if (!args64) { 510784dbeb87SDan Carpenter ret = -ENOMEM; 5108abccd00fSHugo Mills goto out; 5109abccd00fSHugo Mills } 5110abccd00fSHugo Mills 5111abccd00fSHugo Mills memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); 5112abccd00fSHugo Mills args64->stransid = args32->stransid; 5113abccd00fSHugo Mills args64->rtransid = args32->rtransid; 5114abccd00fSHugo Mills args64->stime.sec = args32->stime.sec; 5115abccd00fSHugo Mills args64->stime.nsec = args32->stime.nsec; 5116abccd00fSHugo Mills args64->rtime.sec = args32->rtime.sec; 5117abccd00fSHugo Mills args64->rtime.nsec = args32->rtime.nsec; 5118abccd00fSHugo Mills args64->flags = args32->flags; 5119abccd00fSHugo Mills 5120abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, args64); 5121abccd00fSHugo Mills if (ret) 5122abccd00fSHugo Mills goto out; 5123abccd00fSHugo Mills 5124abccd00fSHugo Mills memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); 5125abccd00fSHugo Mills args32->stransid = args64->stransid; 5126abccd00fSHugo Mills args32->rtransid = args64->rtransid; 5127abccd00fSHugo Mills args32->stime.sec = args64->stime.sec; 5128abccd00fSHugo Mills args32->stime.nsec = args64->stime.nsec; 5129abccd00fSHugo Mills args32->rtime.sec = args64->rtime.sec; 5130abccd00fSHugo Mills args32->rtime.nsec = args64->rtime.nsec; 5131abccd00fSHugo Mills args32->flags = args64->flags; 5132abccd00fSHugo Mills 5133abccd00fSHugo Mills ret = copy_to_user(arg, args32, sizeof(*args32)); 5134abccd00fSHugo Mills if (ret) 5135abccd00fSHugo Mills ret = -EFAULT; 5136abccd00fSHugo Mills 5137abccd00fSHugo Mills out: 5138abccd00fSHugo Mills kfree(args32); 5139abccd00fSHugo Mills kfree(args64); 5140abccd00fSHugo Mills return ret; 5141abccd00fSHugo Mills } 5142abccd00fSHugo Mills #endif 5143abccd00fSHugo Mills 5144abccd00fSHugo Mills static long btrfs_ioctl_set_received_subvol(struct file *file, 5145abccd00fSHugo Mills void __user *arg) 5146abccd00fSHugo Mills { 5147abccd00fSHugo Mills struct btrfs_ioctl_received_subvol_args *sa = NULL; 5148abccd00fSHugo Mills int ret = 0; 5149abccd00fSHugo Mills 5150abccd00fSHugo Mills sa = memdup_user(arg, sizeof(*sa)); 51517b9ea627SShailendra Verma if (IS_ERR(sa)) 51527b9ea627SShailendra Verma return PTR_ERR(sa); 5153abccd00fSHugo Mills 5154abccd00fSHugo Mills ret = _btrfs_ioctl_set_received_subvol(file, sa); 5155abccd00fSHugo Mills 5156abccd00fSHugo Mills if (ret) 5157abccd00fSHugo Mills goto out; 5158abccd00fSHugo Mills 51598ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 51608ea05e3aSAlexander Block if (ret) 51618ea05e3aSAlexander Block ret = -EFAULT; 51628ea05e3aSAlexander Block 51638ea05e3aSAlexander Block out: 51648ea05e3aSAlexander Block kfree(sa); 51658ea05e3aSAlexander Block return ret; 51668ea05e3aSAlexander Block } 51678ea05e3aSAlexander Block 5168867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 5169867ab667Sjeff.liu { 51700b246afaSJeff Mahoney struct inode *inode = file_inode(file); 51710b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 5172a1b83ac5SAnand Jain size_t len; 5173867ab667Sjeff.liu int ret; 5174a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 5175a1b83ac5SAnand Jain 51760b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 51770b246afaSJeff Mahoney memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); 51780b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 5179a1b83ac5SAnand Jain 5180a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 5181867ab667Sjeff.liu 5182867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 51830b246afaSJeff Mahoney btrfs_warn(fs_info, 51840b246afaSJeff Mahoney "label is too long, return the first %zu bytes", 51850b246afaSJeff Mahoney --len); 5186867ab667Sjeff.liu } 5187867ab667Sjeff.liu 5188867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 5189867ab667Sjeff.liu 5190867ab667Sjeff.liu return ret ? -EFAULT : 0; 5191867ab667Sjeff.liu } 5192867ab667Sjeff.liu 5193a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5194a8bfd4abSjeff.liu { 51950b246afaSJeff Mahoney struct inode *inode = file_inode(file); 51960b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 51970b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 51980b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 5199a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 5200a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 5201a8bfd4abSjeff.liu int ret; 5202a8bfd4abSjeff.liu 5203a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 5204a8bfd4abSjeff.liu return -EPERM; 5205a8bfd4abSjeff.liu 5206a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 5207a8bfd4abSjeff.liu return -EFAULT; 5208a8bfd4abSjeff.liu 5209a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 52100b246afaSJeff Mahoney btrfs_err(fs_info, 52115d163e0eSJeff Mahoney "unable to set label with more than %d bytes", 5212a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 5213a8bfd4abSjeff.liu return -EINVAL; 5214a8bfd4abSjeff.liu } 5215a8bfd4abSjeff.liu 5216a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 5217a8bfd4abSjeff.liu if (ret) 5218a8bfd4abSjeff.liu return ret; 5219a8bfd4abSjeff.liu 5220a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 5221a8bfd4abSjeff.liu if (IS_ERR(trans)) { 5222a8bfd4abSjeff.liu ret = PTR_ERR(trans); 5223a8bfd4abSjeff.liu goto out_unlock; 5224a8bfd4abSjeff.liu } 5225a8bfd4abSjeff.liu 52260b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 5227a8bfd4abSjeff.liu strcpy(super_block->label, label); 52280b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 52293a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 5230a8bfd4abSjeff.liu 5231a8bfd4abSjeff.liu out_unlock: 5232a8bfd4abSjeff.liu mnt_drop_write_file(file); 5233a8bfd4abSjeff.liu return ret; 5234a8bfd4abSjeff.liu } 5235a8bfd4abSjeff.liu 52362eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 52372eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 52382eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 52392eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 52402eaa055fSJeff Mahoney 5241d5131b65SDavid Sterba int btrfs_ioctl_get_supported_features(void __user *arg) 52422eaa055fSJeff Mahoney { 52434d4ab6d6SDavid Sterba static const struct btrfs_ioctl_feature_flags features[3] = { 52442eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 52452eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 52462eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 52472eaa055fSJeff Mahoney }; 52482eaa055fSJeff Mahoney 52492eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52502eaa055fSJeff Mahoney return -EFAULT; 52512eaa055fSJeff Mahoney 52522eaa055fSJeff Mahoney return 0; 52532eaa055fSJeff Mahoney } 52542eaa055fSJeff Mahoney 52552eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 52562eaa055fSJeff Mahoney { 52570b246afaSJeff Mahoney struct inode *inode = file_inode(file); 52580b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 52590b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 52602eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 52612eaa055fSJeff Mahoney 52622eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 52632eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 52642eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 52652eaa055fSJeff Mahoney 52662eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 52672eaa055fSJeff Mahoney return -EFAULT; 52682eaa055fSJeff Mahoney 52692eaa055fSJeff Mahoney return 0; 52702eaa055fSJeff Mahoney } 52712eaa055fSJeff Mahoney 52722ff7e61eSJeff Mahoney static int check_feature_bits(struct btrfs_fs_info *fs_info, 52733b02a68aSJeff Mahoney enum btrfs_feature_set set, 52742eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 52752eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 52762eaa055fSJeff Mahoney { 52773b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 52783b02a68aSJeff Mahoney char *names; 52792eaa055fSJeff Mahoney u64 disallowed, unsupported; 52802eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 52812eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 52822eaa055fSJeff Mahoney 52832eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 52842eaa055fSJeff Mahoney if (unsupported) { 52853b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 52863b02a68aSJeff Mahoney if (names) { 52870b246afaSJeff Mahoney btrfs_warn(fs_info, 52883b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 52893b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 52903b02a68aSJeff Mahoney kfree(names); 52913b02a68aSJeff Mahoney } else 52920b246afaSJeff Mahoney btrfs_warn(fs_info, 52932eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 52942eaa055fSJeff Mahoney type, unsupported); 52952eaa055fSJeff Mahoney return -EOPNOTSUPP; 52962eaa055fSJeff Mahoney } 52972eaa055fSJeff Mahoney 52982eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 52992eaa055fSJeff Mahoney if (disallowed) { 53003b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53013b02a68aSJeff Mahoney if (names) { 53020b246afaSJeff Mahoney btrfs_warn(fs_info, 53033b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 53043b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53053b02a68aSJeff Mahoney kfree(names); 53063b02a68aSJeff Mahoney } else 53070b246afaSJeff Mahoney btrfs_warn(fs_info, 53082eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 53092eaa055fSJeff Mahoney type, disallowed); 53102eaa055fSJeff Mahoney return -EPERM; 53112eaa055fSJeff Mahoney } 53122eaa055fSJeff Mahoney 53132eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 53142eaa055fSJeff Mahoney if (disallowed) { 53153b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 53163b02a68aSJeff Mahoney if (names) { 53170b246afaSJeff Mahoney btrfs_warn(fs_info, 53183b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 53193b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 53203b02a68aSJeff Mahoney kfree(names); 53213b02a68aSJeff Mahoney } else 53220b246afaSJeff Mahoney btrfs_warn(fs_info, 53232eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 53242eaa055fSJeff Mahoney type, disallowed); 53252eaa055fSJeff Mahoney return -EPERM; 53262eaa055fSJeff Mahoney } 53272eaa055fSJeff Mahoney 53282eaa055fSJeff Mahoney return 0; 53292eaa055fSJeff Mahoney } 53302eaa055fSJeff Mahoney 53312ff7e61eSJeff Mahoney #define check_feature(fs_info, change_mask, flags, mask_base) \ 53322ff7e61eSJeff Mahoney check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ 53332eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 53342eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 53352eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 53362eaa055fSJeff Mahoney 53372eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 53382eaa055fSJeff Mahoney { 53390b246afaSJeff Mahoney struct inode *inode = file_inode(file); 53400b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 53410b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 53420b246afaSJeff Mahoney struct btrfs_super_block *super_block = fs_info->super_copy; 53432eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 53442eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 53452eaa055fSJeff Mahoney u64 newflags; 53462eaa055fSJeff Mahoney int ret; 53472eaa055fSJeff Mahoney 53482eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 53492eaa055fSJeff Mahoney return -EPERM; 53502eaa055fSJeff Mahoney 53512eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 53522eaa055fSJeff Mahoney return -EFAULT; 53532eaa055fSJeff Mahoney 53542eaa055fSJeff Mahoney /* Nothing to do */ 53552eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 53562eaa055fSJeff Mahoney !flags[0].incompat_flags) 53572eaa055fSJeff Mahoney return 0; 53582eaa055fSJeff Mahoney 53592ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_flags, 53602eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 53612eaa055fSJeff Mahoney if (ret) 53622eaa055fSJeff Mahoney return ret; 53632eaa055fSJeff Mahoney 53642ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].compat_ro_flags, 53652eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 53662eaa055fSJeff Mahoney if (ret) 53672eaa055fSJeff Mahoney return ret; 53682eaa055fSJeff Mahoney 53692ff7e61eSJeff Mahoney ret = check_feature(fs_info, flags[0].incompat_flags, 53702eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 53712eaa055fSJeff Mahoney if (ret) 53722eaa055fSJeff Mahoney return ret; 53732eaa055fSJeff Mahoney 53747ab19625SDavid Sterba ret = mnt_want_write_file(file); 53757ab19625SDavid Sterba if (ret) 53767ab19625SDavid Sterba return ret; 53777ab19625SDavid Sterba 53788051aa1aSDavid Sterba trans = btrfs_start_transaction(root, 0); 53797ab19625SDavid Sterba if (IS_ERR(trans)) { 53807ab19625SDavid Sterba ret = PTR_ERR(trans); 53817ab19625SDavid Sterba goto out_drop_write; 53827ab19625SDavid Sterba } 53832eaa055fSJeff Mahoney 53840b246afaSJeff Mahoney spin_lock(&fs_info->super_lock); 53852eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 53862eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 53872eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 53882eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 53892eaa055fSJeff Mahoney 53902eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 53912eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 53922eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 53932eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 53942eaa055fSJeff Mahoney 53952eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 53962eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 53972eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 53982eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 53990b246afaSJeff Mahoney spin_unlock(&fs_info->super_lock); 54002eaa055fSJeff Mahoney 54013a45bb20SJeff Mahoney ret = btrfs_commit_transaction(trans); 54027ab19625SDavid Sterba out_drop_write: 54037ab19625SDavid Sterba mnt_drop_write_file(file); 54047ab19625SDavid Sterba 54057ab19625SDavid Sterba return ret; 54062eaa055fSJeff Mahoney } 54072eaa055fSJeff Mahoney 54082351f431SJosef Bacik static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) 54092351f431SJosef Bacik { 54102351f431SJosef Bacik struct btrfs_ioctl_send_args *arg; 54112351f431SJosef Bacik int ret; 54122351f431SJosef Bacik 54132351f431SJosef Bacik if (compat) { 54142351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 54152351f431SJosef Bacik struct btrfs_ioctl_send_args_32 args32; 54162351f431SJosef Bacik 54172351f431SJosef Bacik ret = copy_from_user(&args32, argp, sizeof(args32)); 54182351f431SJosef Bacik if (ret) 54192351f431SJosef Bacik return -EFAULT; 54202351f431SJosef Bacik arg = kzalloc(sizeof(*arg), GFP_KERNEL); 54212351f431SJosef Bacik if (!arg) 54222351f431SJosef Bacik return -ENOMEM; 54232351f431SJosef Bacik arg->send_fd = args32.send_fd; 54242351f431SJosef Bacik arg->clone_sources_count = args32.clone_sources_count; 54252351f431SJosef Bacik arg->clone_sources = compat_ptr(args32.clone_sources); 54262351f431SJosef Bacik arg->parent_root = args32.parent_root; 54272351f431SJosef Bacik arg->flags = args32.flags; 54282351f431SJosef Bacik memcpy(arg->reserved, args32.reserved, 54292351f431SJosef Bacik sizeof(args32.reserved)); 54302351f431SJosef Bacik #else 54312351f431SJosef Bacik return -ENOTTY; 54322351f431SJosef Bacik #endif 54332351f431SJosef Bacik } else { 54342351f431SJosef Bacik arg = memdup_user(argp, sizeof(*arg)); 54352351f431SJosef Bacik if (IS_ERR(arg)) 54362351f431SJosef Bacik return PTR_ERR(arg); 54372351f431SJosef Bacik } 54382351f431SJosef Bacik ret = btrfs_ioctl_send(file, arg); 54392351f431SJosef Bacik kfree(arg); 54402351f431SJosef Bacik return ret; 54412351f431SJosef Bacik } 54422351f431SJosef Bacik 5443f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 5444f46b5a66SChristoph Hellwig cmd, unsigned long arg) 5445f46b5a66SChristoph Hellwig { 54460b246afaSJeff Mahoney struct inode *inode = file_inode(file); 54470b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 54480b246afaSJeff Mahoney struct btrfs_root *root = BTRFS_I(inode)->root; 54494bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 5450f46b5a66SChristoph Hellwig 5451f46b5a66SChristoph Hellwig switch (cmd) { 54526cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 54536cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 54546cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 54556cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 54566cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 54576cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 5458f7039b1dSLi Dongyang case FITRIM: 5459f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 5460f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 5461fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 5462fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 5463fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 54643de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 5465fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 54666f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 54676f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 546876dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 546976dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 54700caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 54710caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 54720caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 54730caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 54746ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 54756ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 5476f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 54771e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 54781e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 54791e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 5480f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 5481198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 5482f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 54832ff7e61eSJeff Mahoney return btrfs_ioctl_add_dev(fs_info, argp); 5484f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 5485da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 54866b526ed7SAnand Jain case BTRFS_IOC_RM_DEV_V2: 54876b526ed7SAnand Jain return btrfs_ioctl_rm_dev_v2(file, argp); 5488475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 54892ff7e61eSJeff Mahoney return btrfs_ioctl_fs_info(fs_info, argp); 5490475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 54912ff7e61eSJeff Mahoney return btrfs_ioctl_dev_info(fs_info, argp); 5492f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 54939ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 5494ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 5495ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 5496cc68a8a5SGerhard Heift case BTRFS_IOC_TREE_SEARCH_V2: 5497cc68a8a5SGerhard Heift return btrfs_ioctl_tree_search_v2(file, argp); 5498ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 5499ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 5500d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 5501d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 5502d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 5503d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); 5504d24a67b2SZygo Blaxell case BTRFS_IOC_LOGICAL_INO_V2: 5505d24a67b2SZygo Blaxell return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); 55061406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 55072ff7e61eSJeff Mahoney return btrfs_ioctl_space_info(fs_info, argp); 55089b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 55099b199859SFilipe David Borba Manana int ret; 55109b199859SFilipe David Borba Manana 55110b246afaSJeff Mahoney ret = btrfs_start_delalloc_roots(fs_info, 0, -1); 55129b199859SFilipe David Borba Manana if (ret) 55139b199859SFilipe David Borba Manana return ret; 55140b246afaSJeff Mahoney ret = btrfs_sync_fs(inode->i_sb, 1); 55152fad4e83SDavid Sterba /* 55162fad4e83SDavid Sterba * The transaction thread may want to do more work, 551701327610SNicholas D Steeves * namely it pokes the cleaner kthread that will start 55182fad4e83SDavid Sterba * processing uncleaned subvols. 55192fad4e83SDavid Sterba */ 55200b246afaSJeff Mahoney wake_up_process(fs_info->transaction_kthread); 55219b199859SFilipe David Borba Manana return ret; 55229b199859SFilipe David Borba Manana } 552346204592SSage Weil case BTRFS_IOC_START_SYNC: 55249a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 552546204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 55262ff7e61eSJeff Mahoney return btrfs_ioctl_wait_sync(fs_info, argp); 5527475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 5528b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 5529475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 55302ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_cancel(fs_info); 5531475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 55322ff7e61eSJeff Mahoney return btrfs_ioctl_scrub_progress(fs_info, argp); 5533c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 55349ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 5535837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 55362ff7e61eSJeff Mahoney return btrfs_ioctl_balance_ctl(fs_info, arg); 553719a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 55382ff7e61eSJeff Mahoney return btrfs_ioctl_balance_progress(fs_info, argp); 55398ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 55408ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 5541abccd00fSHugo Mills #ifdef CONFIG_64BIT 5542abccd00fSHugo Mills case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: 5543abccd00fSHugo Mills return btrfs_ioctl_set_received_subvol_32(file, argp); 5544abccd00fSHugo Mills #endif 554531db9f7cSAlexander Block case BTRFS_IOC_SEND: 55462351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, false); 55472351f431SJosef Bacik #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) 55482351f431SJosef Bacik case BTRFS_IOC_SEND_32: 55492351f431SJosef Bacik return _btrfs_ioctl_send(file, argp, true); 55502351f431SJosef Bacik #endif 5551c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 55522ff7e61eSJeff Mahoney return btrfs_ioctl_get_dev_stats(fs_info, argp); 55535d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 5554905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 55555d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 5556905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 55575d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 5558905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 55595d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 5560905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 55612f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 55622f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 55632f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 55642f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 556557254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 556657254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 55673f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 55682ff7e61eSJeff Mahoney return btrfs_ioctl_dev_replace(fs_info, argp); 5569867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 5570867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 5571a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 5572a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 55732eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 5574d5131b65SDavid Sterba return btrfs_ioctl_get_supported_features(argp); 55752eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 55762eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 55772eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 55782eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 5579f46b5a66SChristoph Hellwig } 5580f46b5a66SChristoph Hellwig 5581f46b5a66SChristoph Hellwig return -ENOTTY; 5582f46b5a66SChristoph Hellwig } 55834c63c245SLuke Dashjr 55844c63c245SLuke Dashjr #ifdef CONFIG_COMPAT 55854c63c245SLuke Dashjr long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 55864c63c245SLuke Dashjr { 55872a362249SJeff Mahoney /* 55882a362249SJeff Mahoney * These all access 32-bit values anyway so no further 55892a362249SJeff Mahoney * handling is necessary. 55902a362249SJeff Mahoney */ 55914c63c245SLuke Dashjr switch (cmd) { 55924c63c245SLuke Dashjr case FS_IOC32_GETFLAGS: 55934c63c245SLuke Dashjr cmd = FS_IOC_GETFLAGS; 55944c63c245SLuke Dashjr break; 55954c63c245SLuke Dashjr case FS_IOC32_SETFLAGS: 55964c63c245SLuke Dashjr cmd = FS_IOC_SETFLAGS; 55974c63c245SLuke Dashjr break; 55984c63c245SLuke Dashjr case FS_IOC32_GETVERSION: 55994c63c245SLuke Dashjr cmd = FS_IOC_GETVERSION; 56004c63c245SLuke Dashjr break; 56014c63c245SLuke Dashjr } 56024c63c245SLuke Dashjr 56034c63c245SLuke Dashjr return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); 56044c63c245SLuke Dashjr } 56054c63c245SLuke Dashjr #endif 5606