1f46b5a66SChristoph Hellwig /* 2f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 3f46b5a66SChristoph Hellwig * 4f46b5a66SChristoph Hellwig * This program is free software; you can redistribute it and/or 5f46b5a66SChristoph Hellwig * modify it under the terms of the GNU General Public 6f46b5a66SChristoph Hellwig * License v2 as published by the Free Software Foundation. 7f46b5a66SChristoph Hellwig * 8f46b5a66SChristoph Hellwig * This program is distributed in the hope that it will be useful, 9f46b5a66SChristoph Hellwig * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f46b5a66SChristoph Hellwig * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11f46b5a66SChristoph Hellwig * General Public License for more details. 12f46b5a66SChristoph Hellwig * 13f46b5a66SChristoph Hellwig * You should have received a copy of the GNU General Public 14f46b5a66SChristoph Hellwig * License along with this program; if not, write to the 15f46b5a66SChristoph Hellwig * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16f46b5a66SChristoph Hellwig * Boston, MA 021110-1307, USA. 17f46b5a66SChristoph Hellwig */ 18f46b5a66SChristoph Hellwig 19f46b5a66SChristoph Hellwig #include <linux/kernel.h> 20f46b5a66SChristoph Hellwig #include <linux/bio.h> 21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h> 22f46b5a66SChristoph Hellwig #include <linux/file.h> 23f46b5a66SChristoph Hellwig #include <linux/fs.h> 24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 25f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 26f46b5a66SChristoph Hellwig #include <linux/highmem.h> 27f46b5a66SChristoph Hellwig #include <linux/time.h> 28f46b5a66SChristoph Hellwig #include <linux/init.h> 29f46b5a66SChristoph Hellwig #include <linux/string.h> 30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 31cb8e7090SChristoph Hellwig #include <linux/mount.h> 32f46b5a66SChristoph Hellwig #include <linux/mpage.h> 33cb8e7090SChristoph Hellwig #include <linux/namei.h> 34f46b5a66SChristoph Hellwig #include <linux/swap.h> 35f46b5a66SChristoph Hellwig #include <linux/writeback.h> 36f46b5a66SChristoph Hellwig #include <linux/statfs.h> 37f46b5a66SChristoph Hellwig #include <linux/compat.h> 38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h> 39cb8e7090SChristoph Hellwig #include <linux/security.h> 40f46b5a66SChristoph Hellwig #include <linux/xattr.h> 417ea394f1SYan Zheng #include <linux/vmalloc.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 43f7039b1dSLi Dongyang #include <linux/blkdev.h> 448ea05e3aSAlexander Block #include <linux/uuid.h> 454b4e25f2SChris Mason #include "compat.h" 46f46b5a66SChristoph Hellwig #include "ctree.h" 47f46b5a66SChristoph Hellwig #include "disk-io.h" 48f46b5a66SChristoph Hellwig #include "transaction.h" 49f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 50f46b5a66SChristoph Hellwig #include "ioctl.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" 59f46b5a66SChristoph Hellwig 606cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 616cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 626cbff00fSChristoph Hellwig { 636cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 646cbff00fSChristoph Hellwig return flags; 656cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 666cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 676cbff00fSChristoph Hellwig else 686cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 696cbff00fSChristoph Hellwig } 70f46b5a66SChristoph Hellwig 716cbff00fSChristoph Hellwig /* 726cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 736cbff00fSChristoph Hellwig */ 746cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 756cbff00fSChristoph Hellwig { 766cbff00fSChristoph Hellwig unsigned int iflags = 0; 776cbff00fSChristoph Hellwig 786cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 796cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 806cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 816cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 826cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 836cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 846cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 856cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 866cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 876cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 886cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 896cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 90d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 91d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 92d0092bddSLi Zefan 93d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 94d0092bddSLi Zefan iflags |= FS_COMPR_FL; 95d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 96d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 976cbff00fSChristoph Hellwig 986cbff00fSChristoph Hellwig return iflags; 996cbff00fSChristoph Hellwig } 1006cbff00fSChristoph Hellwig 1016cbff00fSChristoph Hellwig /* 1026cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1036cbff00fSChristoph Hellwig */ 1046cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1056cbff00fSChristoph Hellwig { 1066cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1076cbff00fSChristoph Hellwig 1086cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 1096cbff00fSChristoph Hellwig 1106cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1116cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 1126cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1136cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1146cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1156cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1166cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1176cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1186cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1196cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1206cbff00fSChristoph Hellwig } 1216cbff00fSChristoph Hellwig 1226cbff00fSChristoph Hellwig /* 1236cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1246cbff00fSChristoph Hellwig * 125e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1266cbff00fSChristoph Hellwig */ 1276cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1286cbff00fSChristoph Hellwig { 1290b4dcea5SChris Mason unsigned int flags; 1300b4dcea5SChris Mason 1310b4dcea5SChris Mason if (!dir) 1320b4dcea5SChris Mason return; 1330b4dcea5SChris Mason 1340b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1356cbff00fSChristoph Hellwig 136e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 137e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 138e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 139e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 140e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 141e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 142e27425d6SJosef Bacik } 1436cbff00fSChristoph Hellwig 144e27425d6SJosef Bacik if (flags & BTRFS_INODE_NODATACOW) 145e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 146e27425d6SJosef Bacik 1476cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1486cbff00fSChristoph Hellwig } 1496cbff00fSChristoph Hellwig 1506cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1516cbff00fSChristoph Hellwig { 1526cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); 1536cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1546cbff00fSChristoph Hellwig 1556cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1566cbff00fSChristoph Hellwig return -EFAULT; 1576cbff00fSChristoph Hellwig return 0; 1586cbff00fSChristoph Hellwig } 1596cbff00fSChristoph Hellwig 16075e7cb7fSLiu Bo static int check_flags(unsigned int flags) 16175e7cb7fSLiu Bo { 16275e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 16375e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 16475e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 165e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 166e1e8fb6aSLi Zefan FS_NOCOW_FL)) 16775e7cb7fSLiu Bo return -EOPNOTSUPP; 16875e7cb7fSLiu Bo 16975e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 17075e7cb7fSLiu Bo return -EINVAL; 17175e7cb7fSLiu Bo 17275e7cb7fSLiu Bo return 0; 17375e7cb7fSLiu Bo } 17475e7cb7fSLiu Bo 1756cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1766cbff00fSChristoph Hellwig { 1776cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 1786cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1796cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1806cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1816cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1826cbff00fSChristoph Hellwig int ret; 183f062abf0SLi Zefan u64 ip_oldflags; 184f062abf0SLi Zefan unsigned int i_oldflags; 1857e97b8daSDavid Sterba umode_t mode; 1866cbff00fSChristoph Hellwig 187b83cc969SLi Zefan if (btrfs_root_readonly(root)) 188b83cc969SLi Zefan return -EROFS; 189b83cc969SLi Zefan 1906cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1916cbff00fSChristoph Hellwig return -EFAULT; 1926cbff00fSChristoph Hellwig 19375e7cb7fSLiu Bo ret = check_flags(flags); 19475e7cb7fSLiu Bo if (ret) 19575e7cb7fSLiu Bo return ret; 1966cbff00fSChristoph Hellwig 1972e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1986cbff00fSChristoph Hellwig return -EACCES; 1996cbff00fSChristoph Hellwig 200e7848683SJan Kara ret = mnt_want_write_file(file); 201e7848683SJan Kara if (ret) 202e7848683SJan Kara return ret; 203e7848683SJan Kara 2046cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 2056cbff00fSChristoph Hellwig 206f062abf0SLi Zefan ip_oldflags = ip->flags; 207f062abf0SLi Zefan i_oldflags = inode->i_flags; 2087e97b8daSDavid Sterba mode = inode->i_mode; 209f062abf0SLi Zefan 2106cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2116cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2126cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2136cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2146cbff00fSChristoph Hellwig ret = -EPERM; 2156cbff00fSChristoph Hellwig goto out_unlock; 2166cbff00fSChristoph Hellwig } 2176cbff00fSChristoph Hellwig } 2186cbff00fSChristoph Hellwig 2196cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2206cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2216cbff00fSChristoph Hellwig else 2226cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2236cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2246cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2256cbff00fSChristoph Hellwig else 2266cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2276cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2286cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2296cbff00fSChristoph Hellwig else 2306cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2316cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2326cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2336cbff00fSChristoph Hellwig else 2346cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2356cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2366cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2376cbff00fSChristoph Hellwig else 2386cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2396cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2406cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2416cbff00fSChristoph Hellwig else 2426cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2437e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2447e97b8daSDavid Sterba if (S_ISREG(mode)) { 2457e97b8daSDavid Sterba /* 2467e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2477e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2487e97b8daSDavid Sterba * status of the file and will not set it. 2497e97b8daSDavid Sterba */ 2507e97b8daSDavid Sterba if (inode->i_size == 0) 2517e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2527e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2537e97b8daSDavid Sterba } else { 254e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2557e97b8daSDavid Sterba } 2567e97b8daSDavid Sterba } else { 2577e97b8daSDavid Sterba /* 2587e97b8daSDavid Sterba * Revert back under same assuptions as above 2597e97b8daSDavid Sterba */ 2607e97b8daSDavid Sterba if (S_ISREG(mode)) { 2617e97b8daSDavid Sterba if (inode->i_size == 0) 2627e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2637e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2647e97b8daSDavid Sterba } else { 265e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2667e97b8daSDavid Sterba } 2677e97b8daSDavid Sterba } 2686cbff00fSChristoph Hellwig 26975e7cb7fSLiu Bo /* 27075e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 27175e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 27275e7cb7fSLiu Bo * things smaller. 27375e7cb7fSLiu Bo */ 27475e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 27575e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 27675e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 27775e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 27875e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 27975e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 280ebcb904dSLi Zefan } else { 281ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 28275e7cb7fSLiu Bo } 2836cbff00fSChristoph Hellwig 2844da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 285f062abf0SLi Zefan if (IS_ERR(trans)) { 286f062abf0SLi Zefan ret = PTR_ERR(trans); 287f062abf0SLi Zefan goto out_drop; 288f062abf0SLi Zefan } 2896cbff00fSChristoph Hellwig 290306424ccSLi Zefan btrfs_update_iflags(inode); 2910c4d2d95SJosef Bacik inode_inc_iversion(inode); 292306424ccSLi Zefan inode->i_ctime = CURRENT_TIME; 2936cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 2946cbff00fSChristoph Hellwig 2956cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 296f062abf0SLi Zefan out_drop: 297f062abf0SLi Zefan if (ret) { 298f062abf0SLi Zefan ip->flags = ip_oldflags; 299f062abf0SLi Zefan inode->i_flags = i_oldflags; 300f062abf0SLi Zefan } 3016cbff00fSChristoph Hellwig 3026cbff00fSChristoph Hellwig out_unlock: 3036cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 304e7848683SJan Kara mnt_drop_write_file(file); 3052d4e6f6aSliubo return ret; 3066cbff00fSChristoph Hellwig } 3076cbff00fSChristoph Hellwig 3086cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3096cbff00fSChristoph Hellwig { 3106cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 3116cbff00fSChristoph Hellwig 3126cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3136cbff00fSChristoph Hellwig } 314f46b5a66SChristoph Hellwig 315f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 316f7039b1dSLi Dongyang { 317815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); 318f7039b1dSLi Dongyang struct btrfs_device *device; 319f7039b1dSLi Dongyang struct request_queue *q; 320f7039b1dSLi Dongyang struct fstrim_range range; 321f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 322f7039b1dSLi Dongyang u64 num_devices = 0; 323815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 324f7039b1dSLi Dongyang int ret; 325f7039b1dSLi Dongyang 326f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 327f7039b1dSLi Dongyang return -EPERM; 328f7039b1dSLi Dongyang 3291f78160cSXiao Guangrong rcu_read_lock(); 3301f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3311f78160cSXiao Guangrong dev_list) { 332f7039b1dSLi Dongyang if (!device->bdev) 333f7039b1dSLi Dongyang continue; 334f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 335f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 336f7039b1dSLi Dongyang num_devices++; 337f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 338f7039b1dSLi Dongyang minlen); 339f7039b1dSLi Dongyang } 340f7039b1dSLi Dongyang } 3411f78160cSXiao Guangrong rcu_read_unlock(); 342f4c697e6SLukas Czerner 343f7039b1dSLi Dongyang if (!num_devices) 344f7039b1dSLi Dongyang return -EOPNOTSUPP; 345f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 346f7039b1dSLi Dongyang return -EFAULT; 347e515c18bSLukas Czerner if (range.start > total_bytes || 348e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 349f4c697e6SLukas Czerner return -EINVAL; 350f7039b1dSLi Dongyang 351f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 352f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 353815745cfSAl Viro ret = btrfs_trim_fs(fs_info->tree_root, &range); 354f7039b1dSLi Dongyang if (ret < 0) 355f7039b1dSLi Dongyang return ret; 356f7039b1dSLi Dongyang 357f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 358f7039b1dSLi Dongyang return -EFAULT; 359f7039b1dSLi Dongyang 360f7039b1dSLi Dongyang return 0; 361f7039b1dSLi Dongyang } 362f7039b1dSLi Dongyang 363cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root, 364cb8e7090SChristoph Hellwig struct dentry *dentry, 36572fd032eSSage Weil char *name, int namelen, 3666f72c7e2SArne Jansen u64 *async_transid, 3676f72c7e2SArne Jansen struct btrfs_qgroup_inherit **inherit) 368f46b5a66SChristoph Hellwig { 369f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 370f46b5a66SChristoph Hellwig struct btrfs_key key; 371f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 372f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 373f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 37476dda93cSYan, Zheng struct btrfs_root *new_root; 3752fbe8c8aSAl Viro struct dentry *parent = dentry->d_parent; 3766a912213SJosef Bacik struct inode *dir; 3778ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 378f46b5a66SChristoph Hellwig int ret; 379f46b5a66SChristoph Hellwig int err; 380f46b5a66SChristoph Hellwig u64 objectid; 381f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 3823de4586cSChris Mason u64 index = 0; 3838ea05e3aSAlexander Block uuid_le new_uuid; 384f46b5a66SChristoph Hellwig 385581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 3862fbe8c8aSAl Viro if (ret) 387a22285a6SYan, Zheng return ret; 3886a912213SJosef Bacik 3896a912213SJosef Bacik dir = parent->d_inode; 3906a912213SJosef Bacik 3919ed74f2dSJosef Bacik /* 3929ed74f2dSJosef Bacik * 1 - inode item 3939ed74f2dSJosef Bacik * 2 - refs 3949ed74f2dSJosef Bacik * 1 - root item 3959ed74f2dSJosef Bacik * 2 - dir items 3969ed74f2dSJosef Bacik */ 397a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 3982fbe8c8aSAl Viro if (IS_ERR(trans)) 399a22285a6SYan, Zheng return PTR_ERR(trans); 400f46b5a66SChristoph Hellwig 4016f72c7e2SArne Jansen ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, 4026f72c7e2SArne Jansen inherit ? *inherit : NULL); 4036f72c7e2SArne Jansen if (ret) 4046f72c7e2SArne Jansen goto fail; 4056f72c7e2SArne Jansen 4065d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 4075581a51aSJan Schmidt 0, objectid, NULL, 0, 0, 0); 4088e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4098e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4108e8a1e31SJosef Bacik goto fail; 4118e8a1e31SJosef Bacik } 412f46b5a66SChristoph Hellwig 4135d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 414f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 415f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4165d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 417f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 418f46b5a66SChristoph Hellwig 419f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 420f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 421f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 4225d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 4235d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 4245d4f98a2SYan Zheng BTRFS_UUID_SIZE); 425f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 426f46b5a66SChristoph Hellwig 4278ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 4288ea05e3aSAlexander Block 429f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 430f46b5a66SChristoph Hellwig inode_item->generation = cpu_to_le64(1); 431f46b5a66SChristoph Hellwig inode_item->size = cpu_to_le64(3); 432f46b5a66SChristoph Hellwig inode_item->nlink = cpu_to_le32(1); 433a76a3cd4SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 434f46b5a66SChristoph Hellwig inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 435f46b5a66SChristoph Hellwig 43608fe4db1SLi Zefan root_item.flags = 0; 43708fe4db1SLi Zefan root_item.byte_limit = 0; 43808fe4db1SLi Zefan inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); 43908fe4db1SLi Zefan 440f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 44184234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 442f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 443f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 44486b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 44580ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 446f46b5a66SChristoph Hellwig 4478ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 4488ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 4498ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 4508ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 4518ea05e3aSAlexander Block root_item.otime.sec = cpu_to_le64(cur_time.tv_sec); 452dadd1105SDan Carpenter root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec); 4538ea05e3aSAlexander Block root_item.ctime = root_item.otime; 4548ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 4558ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 456f46b5a66SChristoph Hellwig 457925baeddSChris Mason btrfs_tree_unlock(leaf); 458f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 459f46b5a66SChristoph Hellwig leaf = NULL; 460f46b5a66SChristoph Hellwig 461f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 462f46b5a66SChristoph Hellwig 463f46b5a66SChristoph Hellwig key.objectid = objectid; 4645d4f98a2SYan Zheng key.offset = 0; 465f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 466f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 467f46b5a66SChristoph Hellwig &root_item); 468f46b5a66SChristoph Hellwig if (ret) 469f46b5a66SChristoph Hellwig goto fail; 470f46b5a66SChristoph Hellwig 47176dda93cSYan, Zheng key.offset = (u64)-1; 47276dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 47379787eaaSJeff Mahoney if (IS_ERR(new_root)) { 47479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); 47579787eaaSJeff Mahoney ret = PTR_ERR(new_root); 47679787eaaSJeff Mahoney goto fail; 47779787eaaSJeff Mahoney } 47876dda93cSYan, Zheng 47976dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 48076dda93cSYan, Zheng 481d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 482ce598979SMark Fasheh if (ret) { 483ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 48479787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 485ce598979SMark Fasheh goto fail; 486ce598979SMark Fasheh } 487ce598979SMark Fasheh 488f46b5a66SChristoph Hellwig /* 489f46b5a66SChristoph Hellwig * insert the directory item 490f46b5a66SChristoph Hellwig */ 4913de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 49279787eaaSJeff Mahoney if (ret) { 49379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 49479787eaaSJeff Mahoney goto fail; 49579787eaaSJeff Mahoney } 4963de4586cSChris Mason 4973de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 49816cdcec7SMiao Xie name, namelen, dir, &key, 4993de4586cSChris Mason BTRFS_FT_DIR, index); 50079787eaaSJeff Mahoney if (ret) { 50179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 502f46b5a66SChristoph Hellwig goto fail; 50379787eaaSJeff Mahoney } 5040660b5afSChris Mason 50552c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 50652c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 50752c26179SYan Zheng BUG_ON(ret); 50852c26179SYan Zheng 5090660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5104df27c4dSYan, Zheng objectid, root->root_key.objectid, 51133345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 51276dda93cSYan, Zheng 5130660b5afSChris Mason BUG_ON(ret); 5140660b5afSChris Mason 51576dda93cSYan, Zheng d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 516f46b5a66SChristoph Hellwig fail: 51772fd032eSSage Weil if (async_transid) { 51872fd032eSSage Weil *async_transid = trans->transid; 51972fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 52072fd032eSSage Weil } else { 52176dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 52272fd032eSSage Weil } 523f46b5a66SChristoph Hellwig if (err && !ret) 524f46b5a66SChristoph Hellwig ret = err; 525f46b5a66SChristoph Hellwig return ret; 526f46b5a66SChristoph Hellwig } 527f46b5a66SChristoph Hellwig 52872fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 529b83cc969SLi Zefan char *name, int namelen, u64 *async_transid, 5306f72c7e2SArne Jansen bool readonly, struct btrfs_qgroup_inherit **inherit) 531f46b5a66SChristoph Hellwig { 5322e4bfab9SYan, Zheng struct inode *inode; 533f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 534f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 5352e4bfab9SYan, Zheng int ret; 536f46b5a66SChristoph Hellwig 537f46b5a66SChristoph Hellwig if (!root->ref_cows) 538f46b5a66SChristoph Hellwig return -EINVAL; 539f46b5a66SChristoph Hellwig 540a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 541a22285a6SYan, Zheng if (!pending_snapshot) 542a22285a6SYan, Zheng return -ENOMEM; 543a22285a6SYan, Zheng 54466d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 54566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 546a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 547a22285a6SYan, Zheng pending_snapshot->root = root; 548b83cc969SLi Zefan pending_snapshot->readonly = readonly; 5496f72c7e2SArne Jansen if (inherit) { 5506f72c7e2SArne Jansen pending_snapshot->inherit = *inherit; 5516f72c7e2SArne Jansen *inherit = NULL; /* take responsibility to free it */ 5526f72c7e2SArne Jansen } 553a22285a6SYan, Zheng 55448c03c4bSMiao Xie trans = btrfs_start_transaction(root->fs_info->extent_root, 6); 555a22285a6SYan, Zheng if (IS_ERR(trans)) { 556a22285a6SYan, Zheng ret = PTR_ERR(trans); 557a22285a6SYan, Zheng goto fail; 558a22285a6SYan, Zheng } 559a22285a6SYan, Zheng 560a22285a6SYan, Zheng ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); 561a22285a6SYan, Zheng BUG_ON(ret); 562a22285a6SYan, Zheng 5638351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 564a22285a6SYan, Zheng list_add(&pending_snapshot->list, 565a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 5668351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 56772fd032eSSage Weil if (async_transid) { 56872fd032eSSage Weil *async_transid = trans->transid; 56972fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 57072fd032eSSage Weil root->fs_info->extent_root, 1); 57172fd032eSSage Weil } else { 57272fd032eSSage Weil ret = btrfs_commit_transaction(trans, 57372fd032eSSage Weil root->fs_info->extent_root); 57472fd032eSSage Weil } 575109f2365SLiu Bo if (ret) { 576109f2365SLiu Bo /* cleanup_transaction has freed this for us */ 577109f2365SLiu Bo if (trans->aborted) 578109f2365SLiu Bo pending_snapshot = NULL; 579c37b2b62SJosef Bacik goto fail; 580109f2365SLiu Bo } 581a22285a6SYan, Zheng 582a22285a6SYan, Zheng ret = pending_snapshot->error; 583f46b5a66SChristoph Hellwig if (ret) 5842e4bfab9SYan, Zheng goto fail; 585f46b5a66SChristoph Hellwig 58666b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 58766b4ffd1SJosef Bacik if (ret) 58866b4ffd1SJosef Bacik goto fail; 589f46b5a66SChristoph Hellwig 5902fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 5912e4bfab9SYan, Zheng if (IS_ERR(inode)) { 5922e4bfab9SYan, Zheng ret = PTR_ERR(inode); 5932e4bfab9SYan, Zheng goto fail; 5942e4bfab9SYan, Zheng } 5952e4bfab9SYan, Zheng BUG_ON(!inode); 5962e4bfab9SYan, Zheng d_instantiate(dentry, inode); 5972e4bfab9SYan, Zheng ret = 0; 5982e4bfab9SYan, Zheng fail: 599a22285a6SYan, Zheng kfree(pending_snapshot); 600f46b5a66SChristoph Hellwig return ret; 601f46b5a66SChristoph Hellwig } 602f46b5a66SChristoph Hellwig 6034260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 6044260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 6054260f7c7SSage Weil * minimal. 6064260f7c7SSage Weil */ 6074260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 6084260f7c7SSage Weil { 6092f2f43d3SEric W. Biederman kuid_t fsuid = current_fsuid(); 6104260f7c7SSage Weil 6114260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 6124260f7c7SSage Weil return 0; 6132f2f43d3SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 6144260f7c7SSage Weil return 0; 6152f2f43d3SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 6164260f7c7SSage Weil return 0; 6174260f7c7SSage Weil return !capable(CAP_FOWNER); 6184260f7c7SSage Weil } 6194260f7c7SSage Weil 6204260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 6214260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 6224260f7c7SSage Weil * whether the type of victim is right. 6234260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 6244260f7c7SSage Weil * 2. We should have write and exec permissions on dir 6254260f7c7SSage Weil * 3. We can't remove anything from append-only dir 6264260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 6274260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 6284260f7c7SSage Weil * a. be owner of dir, or 6294260f7c7SSage Weil * b. be owner of victim, or 6304260f7c7SSage Weil * c. have CAP_FOWNER capability 6314260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 6324260f7c7SSage Weil * links pointing to it. 6334260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 6344260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 6354260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 6364260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 6374260f7c7SSage Weil * nfs_async_unlink(). 6384260f7c7SSage Weil */ 6394260f7c7SSage Weil 6404260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 6414260f7c7SSage Weil { 6424260f7c7SSage Weil int error; 6434260f7c7SSage Weil 6444260f7c7SSage Weil if (!victim->d_inode) 6454260f7c7SSage Weil return -ENOENT; 6464260f7c7SSage Weil 6474260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 6484fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 6494260f7c7SSage Weil 6504260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 6514260f7c7SSage Weil if (error) 6524260f7c7SSage Weil return error; 6534260f7c7SSage Weil if (IS_APPEND(dir)) 6544260f7c7SSage Weil return -EPERM; 6554260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 6564260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 6574260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 6584260f7c7SSage Weil return -EPERM; 6594260f7c7SSage Weil if (isdir) { 6604260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 6614260f7c7SSage Weil return -ENOTDIR; 6624260f7c7SSage Weil if (IS_ROOT(victim)) 6634260f7c7SSage Weil return -EBUSY; 6644260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 6654260f7c7SSage Weil return -EISDIR; 6664260f7c7SSage Weil if (IS_DEADDIR(dir)) 6674260f7c7SSage Weil return -ENOENT; 6684260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 6694260f7c7SSage Weil return -EBUSY; 6704260f7c7SSage Weil return 0; 6714260f7c7SSage Weil } 6724260f7c7SSage Weil 673cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 674cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 675cb8e7090SChristoph Hellwig { 676cb8e7090SChristoph Hellwig if (child->d_inode) 677cb8e7090SChristoph Hellwig return -EEXIST; 678cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 679cb8e7090SChristoph Hellwig return -ENOENT; 680cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 681cb8e7090SChristoph Hellwig } 682cb8e7090SChristoph Hellwig 683cb8e7090SChristoph Hellwig /* 684cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 685cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 686cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 687cb8e7090SChristoph Hellwig */ 68876dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 68976dda93cSYan, Zheng char *name, int namelen, 69072fd032eSSage Weil struct btrfs_root *snap_src, 6916f72c7e2SArne Jansen u64 *async_transid, bool readonly, 6926f72c7e2SArne Jansen struct btrfs_qgroup_inherit **inherit) 693cb8e7090SChristoph Hellwig { 69476dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 695cb8e7090SChristoph Hellwig struct dentry *dentry; 696cb8e7090SChristoph Hellwig int error; 697cb8e7090SChristoph Hellwig 69876dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 699cb8e7090SChristoph Hellwig 700cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 701cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 702cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 703cb8e7090SChristoph Hellwig goto out_unlock; 704cb8e7090SChristoph Hellwig 705cb8e7090SChristoph Hellwig error = -EEXIST; 706cb8e7090SChristoph Hellwig if (dentry->d_inode) 707cb8e7090SChristoph Hellwig goto out_dput; 708cb8e7090SChristoph Hellwig 70976dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 710cb8e7090SChristoph Hellwig if (error) 711a874a63eSLiu Bo goto out_dput; 712cb8e7090SChristoph Hellwig 71376dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 71476dda93cSYan, Zheng 71576dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 71676dda93cSYan, Zheng goto out_up_read; 71776dda93cSYan, Zheng 7183de4586cSChris Mason if (snap_src) { 7196f72c7e2SArne Jansen error = create_snapshot(snap_src, dentry, name, namelen, 7206f72c7e2SArne Jansen async_transid, readonly, inherit); 7213de4586cSChris Mason } else { 72276dda93cSYan, Zheng error = create_subvol(BTRFS_I(dir)->root, dentry, 7236f72c7e2SArne Jansen name, namelen, async_transid, inherit); 7243de4586cSChris Mason } 72576dda93cSYan, Zheng if (!error) 72676dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 72776dda93cSYan, Zheng out_up_read: 72876dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 729cb8e7090SChristoph Hellwig out_dput: 730cb8e7090SChristoph Hellwig dput(dentry); 731cb8e7090SChristoph Hellwig out_unlock: 73276dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 733cb8e7090SChristoph Hellwig return error; 734cb8e7090SChristoph Hellwig } 735cb8e7090SChristoph Hellwig 7364cb5300bSChris Mason /* 7374cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 7384cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 7394cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 7404cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 7414cb5300bSChris Mason * part of the file 7424cb5300bSChris Mason */ 7434cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 7444cb5300bSChris Mason { 7454cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 7464cb5300bSChris Mason struct extent_map *em = NULL; 7474cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 7484cb5300bSChris Mason u64 end; 7494cb5300bSChris Mason 7504cb5300bSChris Mason read_lock(&em_tree->lock); 7514cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 7524cb5300bSChris Mason read_unlock(&em_tree->lock); 7534cb5300bSChris Mason 7544cb5300bSChris Mason if (em) { 7554cb5300bSChris Mason end = extent_map_end(em); 7564cb5300bSChris Mason free_extent_map(em); 7574cb5300bSChris Mason if (end - offset > thresh) 7584cb5300bSChris Mason return 0; 7594cb5300bSChris Mason } 7604cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 7614cb5300bSChris Mason thresh /= 2; 7624cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 7634cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 7644cb5300bSChris Mason if (end >= thresh) 7654cb5300bSChris Mason return 0; 7664cb5300bSChris Mason return 1; 7674cb5300bSChris Mason } 7684cb5300bSChris Mason 7694cb5300bSChris Mason /* 7704cb5300bSChris Mason * helper function to walk through a file and find extents 7714cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 7724cb5300bSChris Mason * 7734cb5300bSChris Mason * This is used by the defragging code to find new and small 7744cb5300bSChris Mason * extents 7754cb5300bSChris Mason */ 7764cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 7774cb5300bSChris Mason struct inode *inode, u64 newer_than, 7784cb5300bSChris Mason u64 *off, int thresh) 7794cb5300bSChris Mason { 7804cb5300bSChris Mason struct btrfs_path *path; 7814cb5300bSChris Mason struct btrfs_key min_key; 7824cb5300bSChris Mason struct btrfs_key max_key; 7834cb5300bSChris Mason struct extent_buffer *leaf; 7844cb5300bSChris Mason struct btrfs_file_extent_item *extent; 7854cb5300bSChris Mason int type; 7864cb5300bSChris Mason int ret; 787a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 7884cb5300bSChris Mason 7894cb5300bSChris Mason path = btrfs_alloc_path(); 7904cb5300bSChris Mason if (!path) 7914cb5300bSChris Mason return -ENOMEM; 7924cb5300bSChris Mason 793a4689d2bSDavid Sterba min_key.objectid = ino; 7944cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 7954cb5300bSChris Mason min_key.offset = *off; 7964cb5300bSChris Mason 797a4689d2bSDavid Sterba max_key.objectid = ino; 7984cb5300bSChris Mason max_key.type = (u8)-1; 7994cb5300bSChris Mason max_key.offset = (u64)-1; 8004cb5300bSChris Mason 8014cb5300bSChris Mason path->keep_locks = 1; 8024cb5300bSChris Mason 8034cb5300bSChris Mason while(1) { 8044cb5300bSChris Mason ret = btrfs_search_forward(root, &min_key, &max_key, 8054cb5300bSChris Mason path, 0, newer_than); 8064cb5300bSChris Mason if (ret != 0) 8074cb5300bSChris Mason goto none; 808a4689d2bSDavid Sterba if (min_key.objectid != ino) 8094cb5300bSChris Mason goto none; 8104cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 8114cb5300bSChris Mason goto none; 8124cb5300bSChris Mason 8134cb5300bSChris Mason leaf = path->nodes[0]; 8144cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 8154cb5300bSChris Mason struct btrfs_file_extent_item); 8164cb5300bSChris Mason 8174cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 8184cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 8194cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 8204cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 8214cb5300bSChris Mason *off = min_key.offset; 8224cb5300bSChris Mason btrfs_free_path(path); 8234cb5300bSChris Mason return 0; 8244cb5300bSChris Mason } 8254cb5300bSChris Mason 8264cb5300bSChris Mason if (min_key.offset == (u64)-1) 8274cb5300bSChris Mason goto none; 8284cb5300bSChris Mason 8294cb5300bSChris Mason min_key.offset++; 8304cb5300bSChris Mason btrfs_release_path(path); 8314cb5300bSChris Mason } 8324cb5300bSChris Mason none: 8334cb5300bSChris Mason btrfs_free_path(path); 8344cb5300bSChris Mason return -ENOENT; 8354cb5300bSChris Mason } 8364cb5300bSChris Mason 8376c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 83817ce6ef8SLiu Bo { 83917ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 840940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8416c282eb4SLi Zefan struct extent_map *em; 8426c282eb4SLi Zefan u64 len = PAGE_CACHE_SIZE; 843940100a4SChris Mason 844940100a4SChris Mason /* 845940100a4SChris Mason * hopefully we have this extent in the tree already, try without 846940100a4SChris Mason * the full extent lock 847940100a4SChris Mason */ 848940100a4SChris Mason read_lock(&em_tree->lock); 849940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 850940100a4SChris Mason read_unlock(&em_tree->lock); 851940100a4SChris Mason 852940100a4SChris Mason if (!em) { 853940100a4SChris Mason /* get the big lock and read metadata off disk */ 854d0082371SJeff Mahoney lock_extent(io_tree, start, start + len - 1); 855940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 856d0082371SJeff Mahoney unlock_extent(io_tree, start, start + len - 1); 857940100a4SChris Mason 8586cf8bfbfSDan Carpenter if (IS_ERR(em)) 8596c282eb4SLi Zefan return NULL; 860940100a4SChris Mason } 861940100a4SChris Mason 8626c282eb4SLi Zefan return em; 8636c282eb4SLi Zefan } 8646c282eb4SLi Zefan 8656c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 8666c282eb4SLi Zefan { 8676c282eb4SLi Zefan struct extent_map *next; 8686c282eb4SLi Zefan bool ret = true; 8696c282eb4SLi Zefan 8706c282eb4SLi Zefan /* this is the last extent */ 8716c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 8726c282eb4SLi Zefan return false; 8736c282eb4SLi Zefan 8746c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 8756c282eb4SLi Zefan if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 8766c282eb4SLi Zefan ret = false; 8776c282eb4SLi Zefan 8786c282eb4SLi Zefan free_extent_map(next); 8796c282eb4SLi Zefan return ret; 8806c282eb4SLi Zefan } 8816c282eb4SLi Zefan 8826c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh, 883a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 884a43a2111SAndrew Mahone int compress) 8856c282eb4SLi Zefan { 8866c282eb4SLi Zefan struct extent_map *em; 8876c282eb4SLi Zefan int ret = 1; 8886c282eb4SLi Zefan bool next_mergeable = true; 8896c282eb4SLi Zefan 8906c282eb4SLi Zefan /* 8916c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 8926c282eb4SLi Zefan * defragging it 8936c282eb4SLi Zefan */ 8946c282eb4SLi Zefan if (start < *defrag_end) 8956c282eb4SLi Zefan return 1; 8966c282eb4SLi Zefan 8976c282eb4SLi Zefan *skip = 0; 8986c282eb4SLi Zefan 8996c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 9006c282eb4SLi Zefan if (!em) 9016c282eb4SLi Zefan return 0; 9026c282eb4SLi Zefan 903940100a4SChris Mason /* this will cover holes, and inline extents */ 90417ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 905940100a4SChris Mason ret = 0; 90617ce6ef8SLiu Bo goto out; 90717ce6ef8SLiu Bo } 90817ce6ef8SLiu Bo 9096c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 910940100a4SChris Mason 911940100a4SChris Mason /* 9126c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 9136c282eb4SLi Zefan * real extent, don't bother defragging it 914940100a4SChris Mason */ 915a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 9166c282eb4SLi Zefan (em->len >= thresh || !next_mergeable)) 917940100a4SChris Mason ret = 0; 91817ce6ef8SLiu Bo out: 919940100a4SChris Mason /* 920940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 921940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 922940100a4SChris Mason * so that the next tiny extent will force a defrag. 923940100a4SChris Mason * 924940100a4SChris Mason * The end result of this is that tiny extents before a single big 925940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 926940100a4SChris Mason */ 927940100a4SChris Mason if (ret) { 928940100a4SChris Mason *defrag_end = extent_map_end(em); 929940100a4SChris Mason } else { 930940100a4SChris Mason *last_len = 0; 931940100a4SChris Mason *skip = extent_map_end(em); 932940100a4SChris Mason *defrag_end = 0; 933940100a4SChris Mason } 934940100a4SChris Mason 935940100a4SChris Mason free_extent_map(em); 936940100a4SChris Mason return ret; 937940100a4SChris Mason } 938940100a4SChris Mason 9394cb5300bSChris Mason /* 9404cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 9414cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 9424cb5300bSChris Mason * to COW and defrag. 9434cb5300bSChris Mason * 9444cb5300bSChris Mason * It also makes sure the delalloc code has enough 9454cb5300bSChris Mason * dirty data to avoid making new small extents as part 9464cb5300bSChris Mason * of the defrag 9474cb5300bSChris Mason * 9484cb5300bSChris Mason * It's a good idea to start RA on this range 9494cb5300bSChris Mason * before calling this. 9504cb5300bSChris Mason */ 9514cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 9524cb5300bSChris Mason struct page **pages, 9534cb5300bSChris Mason unsigned long start_index, 9544cb5300bSChris Mason int num_pages) 955f46b5a66SChristoph Hellwig { 9564cb5300bSChris Mason unsigned long file_end; 9574cb5300bSChris Mason u64 isize = i_size_read(inode); 958f46b5a66SChristoph Hellwig u64 page_start; 959f46b5a66SChristoph Hellwig u64 page_end; 9601f12bd06SLiu Bo u64 page_cnt; 9614cb5300bSChris Mason int ret; 9624cb5300bSChris Mason int i; 9634cb5300bSChris Mason int i_done; 9644cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 9654cb5300bSChris Mason struct extent_state *cached_state = NULL; 966600a45e1SMiao Xie struct extent_io_tree *tree; 9673b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 9684cb5300bSChris Mason 9694cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 9701f12bd06SLiu Bo if (!isize || start_index > file_end) 9711f12bd06SLiu Bo return 0; 9721f12bd06SLiu Bo 9731f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 9744cb5300bSChris Mason 9754cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 9761f12bd06SLiu Bo page_cnt << PAGE_CACHE_SHIFT); 9774cb5300bSChris Mason if (ret) 9784cb5300bSChris Mason return ret; 9794cb5300bSChris Mason i_done = 0; 980600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 9814cb5300bSChris Mason 9824cb5300bSChris Mason /* step one, lock all the pages */ 9831f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 9844cb5300bSChris Mason struct page *page; 985600a45e1SMiao Xie again: 986a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 9873b16a4e3SJosef Bacik start_index + i, mask); 9884cb5300bSChris Mason if (!page) 9894cb5300bSChris Mason break; 9904cb5300bSChris Mason 991600a45e1SMiao Xie page_start = page_offset(page); 992600a45e1SMiao Xie page_end = page_start + PAGE_CACHE_SIZE - 1; 993600a45e1SMiao Xie while (1) { 994d0082371SJeff Mahoney lock_extent(tree, page_start, page_end); 995600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 996600a45e1SMiao Xie page_start); 997d0082371SJeff Mahoney unlock_extent(tree, page_start, page_end); 998600a45e1SMiao Xie if (!ordered) 999600a45e1SMiao Xie break; 1000600a45e1SMiao Xie 1001600a45e1SMiao Xie unlock_page(page); 1002600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1003600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1004600a45e1SMiao Xie lock_page(page); 10051f12bd06SLiu Bo /* 10061f12bd06SLiu Bo * we unlocked the page above, so we need check if 10071f12bd06SLiu Bo * it was released or not. 10081f12bd06SLiu Bo */ 10091f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 10101f12bd06SLiu Bo unlock_page(page); 10111f12bd06SLiu Bo page_cache_release(page); 10121f12bd06SLiu Bo goto again; 10131f12bd06SLiu Bo } 1014600a45e1SMiao Xie } 1015600a45e1SMiao Xie 10164cb5300bSChris Mason if (!PageUptodate(page)) { 10174cb5300bSChris Mason btrfs_readpage(NULL, page); 10184cb5300bSChris Mason lock_page(page); 10194cb5300bSChris Mason if (!PageUptodate(page)) { 10204cb5300bSChris Mason unlock_page(page); 10214cb5300bSChris Mason page_cache_release(page); 10224cb5300bSChris Mason ret = -EIO; 10234cb5300bSChris Mason break; 10244cb5300bSChris Mason } 10254cb5300bSChris Mason } 1026600a45e1SMiao Xie 1027600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1028600a45e1SMiao Xie unlock_page(page); 1029600a45e1SMiao Xie page_cache_release(page); 1030600a45e1SMiao Xie goto again; 1031600a45e1SMiao Xie } 1032600a45e1SMiao Xie 10334cb5300bSChris Mason pages[i] = page; 10344cb5300bSChris Mason i_done++; 10354cb5300bSChris Mason } 10364cb5300bSChris Mason if (!i_done || ret) 10374cb5300bSChris Mason goto out; 10384cb5300bSChris Mason 10394cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 10404cb5300bSChris Mason goto out; 10414cb5300bSChris Mason 10424cb5300bSChris Mason /* 10434cb5300bSChris Mason * so now we have a nice long stream of locked 10444cb5300bSChris Mason * and up to date pages, lets wait on them 10454cb5300bSChris Mason */ 10464cb5300bSChris Mason for (i = 0; i < i_done; i++) 10474cb5300bSChris Mason wait_on_page_writeback(pages[i]); 10484cb5300bSChris Mason 10494cb5300bSChris Mason page_start = page_offset(pages[0]); 10504cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 10514cb5300bSChris Mason 10524cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1053d0082371SJeff Mahoney page_start, page_end - 1, 0, &cached_state); 10544cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 10554cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 10569e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 10579e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 10584cb5300bSChris Mason 10591f12bd06SLiu Bo if (i_done != page_cnt) { 10609e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 10619e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 10629e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 10634cb5300bSChris Mason btrfs_delalloc_release_space(inode, 10641f12bd06SLiu Bo (page_cnt - i_done) << PAGE_CACHE_SHIFT); 10654cb5300bSChris Mason } 10664cb5300bSChris Mason 10674cb5300bSChris Mason 10689e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 10699e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 10704cb5300bSChris Mason 10714cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 10724cb5300bSChris Mason page_start, page_end - 1, &cached_state, 10734cb5300bSChris Mason GFP_NOFS); 10744cb5300bSChris Mason 10754cb5300bSChris Mason for (i = 0; i < i_done; i++) { 10764cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 10774cb5300bSChris Mason ClearPageChecked(pages[i]); 10784cb5300bSChris Mason set_page_extent_mapped(pages[i]); 10794cb5300bSChris Mason set_page_dirty(pages[i]); 10804cb5300bSChris Mason unlock_page(pages[i]); 10814cb5300bSChris Mason page_cache_release(pages[i]); 10824cb5300bSChris Mason } 10834cb5300bSChris Mason return i_done; 10844cb5300bSChris Mason out: 10854cb5300bSChris Mason for (i = 0; i < i_done; i++) { 10864cb5300bSChris Mason unlock_page(pages[i]); 10874cb5300bSChris Mason page_cache_release(pages[i]); 10884cb5300bSChris Mason } 10891f12bd06SLiu Bo btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); 10904cb5300bSChris Mason return ret; 10914cb5300bSChris Mason 10924cb5300bSChris Mason } 10934cb5300bSChris Mason 10944cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 10954cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 10964cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 10974cb5300bSChris Mason { 10984cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 10994cb5300bSChris Mason struct file_ra_state *ra = NULL; 11004cb5300bSChris Mason unsigned long last_index; 1101151a31b2SLi Zefan u64 isize = i_size_read(inode); 1102940100a4SChris Mason u64 last_len = 0; 1103940100a4SChris Mason u64 skip = 0; 1104940100a4SChris Mason u64 defrag_end = 0; 11054cb5300bSChris Mason u64 newer_off = range->start; 1106f46b5a66SChristoph Hellwig unsigned long i; 1107008873eaSLi Zefan unsigned long ra_index = 0; 1108f46b5a66SChristoph Hellwig int ret; 11094cb5300bSChris Mason int defrag_count = 0; 11101a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 11114cb5300bSChris Mason int extent_thresh = range->extent_thresh; 1112008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1113008873eaSLi Zefan int cluster = max_cluster; 11144cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 11154cb5300bSChris Mason struct page **pages = NULL; 11164cb5300bSChris Mason 11174cb5300bSChris Mason if (extent_thresh == 0) 11184cb5300bSChris Mason extent_thresh = 256 * 1024; 11191a419d85SLi Zefan 11201a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 11211a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 11221a419d85SLi Zefan return -EINVAL; 11231a419d85SLi Zefan if (range->compress_type) 11241a419d85SLi Zefan compress_type = range->compress_type; 11251a419d85SLi Zefan } 1126f46b5a66SChristoph Hellwig 1127151a31b2SLi Zefan if (isize == 0) 1128940100a4SChris Mason return 0; 1129f46b5a66SChristoph Hellwig 11304cb5300bSChris Mason /* 11314cb5300bSChris Mason * if we were not given a file, allocate a readahead 11324cb5300bSChris Mason * context 11334cb5300bSChris Mason */ 11344cb5300bSChris Mason if (!file) { 11354cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 11364cb5300bSChris Mason if (!ra) 11374cb5300bSChris Mason return -ENOMEM; 11384cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 11394cb5300bSChris Mason } else { 11404cb5300bSChris Mason ra = &file->f_ra; 11414cb5300bSChris Mason } 11424cb5300bSChris Mason 1143008873eaSLi Zefan pages = kmalloc(sizeof(struct page *) * max_cluster, 11444cb5300bSChris Mason GFP_NOFS); 11454cb5300bSChris Mason if (!pages) { 11464cb5300bSChris Mason ret = -ENOMEM; 11474cb5300bSChris Mason goto out_ra; 11484cb5300bSChris Mason } 11494cb5300bSChris Mason 11504cb5300bSChris Mason /* find the last page to defrag */ 11511e701a32SChris Mason if (range->start + range->len > range->start) { 1152151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 11531e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 11541e701a32SChris Mason } else { 1155151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 11561e701a32SChris Mason } 11571e701a32SChris Mason 11584cb5300bSChris Mason if (newer_than) { 11594cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 11604cb5300bSChris Mason &newer_off, 64 * 1024); 11614cb5300bSChris Mason if (!ret) { 11624cb5300bSChris Mason range->start = newer_off; 11634cb5300bSChris Mason /* 11644cb5300bSChris Mason * we always align our defrag to help keep 11654cb5300bSChris Mason * the extents in the file evenly spaced 11664cb5300bSChris Mason */ 11674cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 11684cb5300bSChris Mason } else 11694cb5300bSChris Mason goto out_ra; 11704cb5300bSChris Mason } else { 11711e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 11724cb5300bSChris Mason } 11734cb5300bSChris Mason if (!max_to_defrag) 11747ec31b54SLiu Bo max_to_defrag = last_index + 1; 11754cb5300bSChris Mason 11762a0f7f57SLi Zefan /* 11772a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 11782a0f7f57SLi Zefan * written sequentially. 11792a0f7f57SLi Zefan */ 11802a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 11812a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 11822a0f7f57SLi Zefan 1183f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1184f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1185f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 11864cb5300bSChris Mason /* 11874cb5300bSChris Mason * make sure we stop running if someone unmounts 11884cb5300bSChris Mason * the FS 11894cb5300bSChris Mason */ 11904cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 11914cb5300bSChris Mason break; 11924cb5300bSChris Mason 119366c26892SLiu Bo if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 11946c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1195a43a2111SAndrew Mahone &defrag_end, range->flags & 1196a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1197940100a4SChris Mason unsigned long next; 1198940100a4SChris Mason /* 1199940100a4SChris Mason * the should_defrag function tells us how much to skip 1200940100a4SChris Mason * bump our counter by the suggested amount 1201940100a4SChris Mason */ 1202940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1203940100a4SChris Mason i = max(i + 1, next); 1204940100a4SChris Mason continue; 1205940100a4SChris Mason } 1206008873eaSLi Zefan 1207008873eaSLi Zefan if (!newer_than) { 1208008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1209008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1210008873eaSLi Zefan cluster = min(cluster, max_cluster); 1211008873eaSLi Zefan } else { 1212008873eaSLi Zefan cluster = max_cluster; 1213008873eaSLi Zefan } 1214008873eaSLi Zefan 12151e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 12161a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 1217940100a4SChris Mason 1218008873eaSLi Zefan if (i + cluster > ra_index) { 1219008873eaSLi Zefan ra_index = max(i, ra_index); 1220008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1221008873eaSLi Zefan cluster); 1222008873eaSLi Zefan ra_index += max_cluster; 1223008873eaSLi Zefan } 12244cb5300bSChris Mason 1225ecb8bea8SLiu Bo mutex_lock(&inode->i_mutex); 1226008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1227ecb8bea8SLiu Bo if (ret < 0) { 1228ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12294cb5300bSChris Mason goto out_ra; 1230ecb8bea8SLiu Bo } 12314cb5300bSChris Mason 12324cb5300bSChris Mason defrag_count += ret; 12334cb5300bSChris Mason balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); 1234ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12354cb5300bSChris Mason 12364cb5300bSChris Mason if (newer_than) { 12374cb5300bSChris Mason if (newer_off == (u64)-1) 12384cb5300bSChris Mason break; 12394cb5300bSChris Mason 1240e1f041e1SLiu Bo if (ret > 0) 1241e1f041e1SLiu Bo i += ret; 1242e1f041e1SLiu Bo 12434cb5300bSChris Mason newer_off = max(newer_off + 1, 12444cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 12454cb5300bSChris Mason 12464cb5300bSChris Mason ret = find_new_extents(root, inode, 12474cb5300bSChris Mason newer_than, &newer_off, 12484cb5300bSChris Mason 64 * 1024); 12494cb5300bSChris Mason if (!ret) { 12504cb5300bSChris Mason range->start = newer_off; 12514cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 12524cb5300bSChris Mason } else { 12534cb5300bSChris Mason break; 1254940100a4SChris Mason } 12554cb5300bSChris Mason } else { 1256008873eaSLi Zefan if (ret > 0) { 1257cbcc8326SLi Zefan i += ret; 1258008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1259008873eaSLi Zefan } else { 1260940100a4SChris Mason i++; 1261008873eaSLi Zefan last_len = 0; 1262008873eaSLi Zefan } 1263f46b5a66SChristoph Hellwig } 12644cb5300bSChris Mason } 1265f46b5a66SChristoph Hellwig 12661e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 12671e701a32SChris Mason filemap_flush(inode->i_mapping); 12681e701a32SChris Mason 12691e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 12701e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 12711e701a32SChris Mason * we have to make sure the IO is actually started and that 12721e701a32SChris Mason * ordered extents get created before we return 12731e701a32SChris Mason */ 12741e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 12751e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 12761e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 12771e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 12781e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 12791e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 12801e701a32SChris Mason } 12811e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 12821e701a32SChris Mason 12831e701a32SChris Mason mutex_lock(&inode->i_mutex); 1284261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 12851e701a32SChris Mason mutex_unlock(&inode->i_mutex); 12861e701a32SChris Mason } 12871e701a32SChris Mason 12881a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 12892b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 12901a419d85SLi Zefan } 12911a419d85SLi Zefan 129260ccf82fSDiego Calleja ret = defrag_count; 1293940100a4SChris Mason 12944cb5300bSChris Mason out_ra: 12954cb5300bSChris Mason if (!file) 12964cb5300bSChris Mason kfree(ra); 12974cb5300bSChris Mason kfree(pages); 1298940100a4SChris Mason return ret; 1299f46b5a66SChristoph Hellwig } 1300f46b5a66SChristoph Hellwig 1301198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 130276dda93cSYan, Zheng void __user *arg) 1303f46b5a66SChristoph Hellwig { 1304f46b5a66SChristoph Hellwig u64 new_size; 1305f46b5a66SChristoph Hellwig u64 old_size; 1306f46b5a66SChristoph Hellwig u64 devid = 1; 1307198605a8SMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 1308f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1309f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1310f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1311f46b5a66SChristoph Hellwig char *sizestr; 1312f46b5a66SChristoph Hellwig char *devstr = NULL; 1313f46b5a66SChristoph Hellwig int ret = 0; 1314f46b5a66SChristoph Hellwig int mod = 0; 1315f46b5a66SChristoph Hellwig 1316c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1317c146afadSYan Zheng return -EROFS; 1318c146afadSYan Zheng 1319e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1320e441d54dSChris Mason return -EPERM; 1321e441d54dSChris Mason 1322198605a8SMiao Xie ret = mnt_want_write_file(file); 1323198605a8SMiao Xie if (ret) 1324198605a8SMiao Xie return ret; 1325198605a8SMiao Xie 13265ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 13275ac00addSStefan Behrens 1)) { 13285ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 13295ac00addSStefan Behrens return -EINPROGRESS; 1330c9e9f97bSIlya Dryomov } 1331c9e9f97bSIlya Dryomov 13325ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1333dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1334c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1335c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1336c9e9f97bSIlya Dryomov goto out; 1337c9e9f97bSIlya Dryomov } 13385516e595SMark Fasheh 13395516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1340f46b5a66SChristoph Hellwig 1341f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1342f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1343f46b5a66SChristoph Hellwig if (devstr) { 1344f46b5a66SChristoph Hellwig char *end; 1345f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1346f46b5a66SChristoph Hellwig *devstr = '\0'; 1347f46b5a66SChristoph Hellwig devstr = vol_args->name; 1348f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 13495bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizing devid %llu\n", 135021380931SJoel Becker (unsigned long long)devid); 1351f46b5a66SChristoph Hellwig } 1352aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1353f46b5a66SChristoph Hellwig if (!device) { 13545bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 135521380931SJoel Becker (unsigned long long)devid); 1356f46b5a66SChristoph Hellwig ret = -EINVAL; 1357c9e9f97bSIlya Dryomov goto out_free; 1358f46b5a66SChristoph Hellwig } 13594e42ae1bSLiu Bo if (device->fs_devices && device->fs_devices->seeding) { 13604e42ae1bSLiu Bo printk(KERN_INFO "btrfs: resizer unable to apply on " 1361a8c4a33bSChris Mason "seeding device %llu\n", 1362a8c4a33bSChris Mason (unsigned long long)devid); 13634e42ae1bSLiu Bo ret = -EINVAL; 13644e42ae1bSLiu Bo goto out_free; 13654e42ae1bSLiu Bo } 13664e42ae1bSLiu Bo 1367f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1368f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1369f46b5a66SChristoph Hellwig else { 1370f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1371f46b5a66SChristoph Hellwig mod = -1; 1372f46b5a66SChristoph Hellwig sizestr++; 1373f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1374f46b5a66SChristoph Hellwig mod = 1; 1375f46b5a66SChristoph Hellwig sizestr++; 1376f46b5a66SChristoph Hellwig } 137791748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1378f46b5a66SChristoph Hellwig if (new_size == 0) { 1379f46b5a66SChristoph Hellwig ret = -EINVAL; 1380c9e9f97bSIlya Dryomov goto out_free; 1381f46b5a66SChristoph Hellwig } 1382f46b5a66SChristoph Hellwig } 1383f46b5a66SChristoph Hellwig 138463a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 138563a212abSStefan Behrens ret = -EINVAL; 138663a212abSStefan Behrens goto out_free; 138763a212abSStefan Behrens } 138863a212abSStefan Behrens 1389f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1390f46b5a66SChristoph Hellwig 1391f46b5a66SChristoph Hellwig if (mod < 0) { 1392f46b5a66SChristoph Hellwig if (new_size > old_size) { 1393f46b5a66SChristoph Hellwig ret = -EINVAL; 1394c9e9f97bSIlya Dryomov goto out_free; 1395f46b5a66SChristoph Hellwig } 1396f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1397f46b5a66SChristoph Hellwig } else if (mod > 0) { 1398f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1399f46b5a66SChristoph Hellwig } 1400f46b5a66SChristoph Hellwig 1401f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1402f46b5a66SChristoph Hellwig ret = -EINVAL; 1403c9e9f97bSIlya Dryomov goto out_free; 1404f46b5a66SChristoph Hellwig } 1405f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1406f46b5a66SChristoph Hellwig ret = -EFBIG; 1407c9e9f97bSIlya Dryomov goto out_free; 1408f46b5a66SChristoph Hellwig } 1409f46b5a66SChristoph Hellwig 1410f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1411f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1412f46b5a66SChristoph Hellwig 1413606686eeSJosef Bacik printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", 1414606686eeSJosef Bacik rcu_str_deref(device->name), 1415606686eeSJosef Bacik (unsigned long long)new_size); 1416f46b5a66SChristoph Hellwig 1417f46b5a66SChristoph Hellwig if (new_size > old_size) { 1418a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 141998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 142098d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1421c9e9f97bSIlya Dryomov goto out_free; 142298d5dc13STsutomu Itoh } 1423f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1424f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1425ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1426f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 14270253f40eSjeff.liu } /* equal, nothing need to do */ 1428f46b5a66SChristoph Hellwig 1429c9e9f97bSIlya Dryomov out_free: 1430f46b5a66SChristoph Hellwig kfree(vol_args); 1431c9e9f97bSIlya Dryomov out: 1432c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 1433198605a8SMiao Xie mnt_drop_write_file(file); 14345ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 1435f46b5a66SChristoph Hellwig return ret; 1436f46b5a66SChristoph Hellwig } 1437f46b5a66SChristoph Hellwig 143872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 14396f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 14406f72c7e2SArne Jansen u64 *transid, bool readonly, 14416f72c7e2SArne Jansen struct btrfs_qgroup_inherit **inherit) 1442f46b5a66SChristoph Hellwig { 1443f46b5a66SChristoph Hellwig int namelen; 14443de4586cSChris Mason int ret = 0; 1445f46b5a66SChristoph Hellwig 1446a874a63eSLiu Bo ret = mnt_want_write_file(file); 1447a874a63eSLiu Bo if (ret) 1448a874a63eSLiu Bo goto out; 1449a874a63eSLiu Bo 145072fd032eSSage Weil namelen = strlen(name); 145172fd032eSSage Weil if (strchr(name, '/')) { 1452f46b5a66SChristoph Hellwig ret = -EINVAL; 1453a874a63eSLiu Bo goto out_drop_write; 1454f46b5a66SChristoph Hellwig } 1455f46b5a66SChristoph Hellwig 145616780cabSChris Mason if (name[0] == '.' && 145716780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 145816780cabSChris Mason ret = -EEXIST; 1459a874a63eSLiu Bo goto out_drop_write; 146016780cabSChris Mason } 146116780cabSChris Mason 14623de4586cSChris Mason if (subvol) { 146372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 14646f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1465cb8e7090SChristoph Hellwig } else { 14662903ff01SAl Viro struct fd src = fdget(fd); 14673de4586cSChris Mason struct inode *src_inode; 14682903ff01SAl Viro if (!src.file) { 14693de4586cSChris Mason ret = -EINVAL; 1470a874a63eSLiu Bo goto out_drop_write; 14713de4586cSChris Mason } 14723de4586cSChris Mason 14732903ff01SAl Viro src_inode = src.file->f_path.dentry->d_inode; 14743de4586cSChris Mason if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { 1475d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1476d397712bSChris Mason "another FS\n"); 14773de4586cSChris Mason ret = -EINVAL; 1478ecd18815SAl Viro } else { 147972fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 148072fd032eSSage Weil BTRFS_I(src_inode)->root, 14816f72c7e2SArne Jansen transid, readonly, inherit); 1482ecd18815SAl Viro } 14832903ff01SAl Viro fdput(src); 1484cb8e7090SChristoph Hellwig } 1485a874a63eSLiu Bo out_drop_write: 1486a874a63eSLiu Bo mnt_drop_write_file(file); 1487f46b5a66SChristoph Hellwig out: 148872fd032eSSage Weil return ret; 148972fd032eSSage Weil } 149072fd032eSSage Weil 149172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1492fa0d2b9bSLi Zefan void __user *arg, int subvol) 149372fd032eSSage Weil { 1494fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 149572fd032eSSage Weil int ret; 149672fd032eSSage Weil 1497fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1498fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1499fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1500fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1501fa0d2b9bSLi Zefan 1502fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1503b83cc969SLi Zefan vol_args->fd, subvol, 15046f72c7e2SArne Jansen NULL, false, NULL); 1505fa0d2b9bSLi Zefan 1506fa0d2b9bSLi Zefan kfree(vol_args); 1507fa0d2b9bSLi Zefan return ret; 1508fa0d2b9bSLi Zefan } 1509fa0d2b9bSLi Zefan 1510fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1511fa0d2b9bSLi Zefan void __user *arg, int subvol) 1512fa0d2b9bSLi Zefan { 1513fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1514fa0d2b9bSLi Zefan int ret; 1515fdfb1e4fSLi Zefan u64 transid = 0; 1516fdfb1e4fSLi Zefan u64 *ptr = NULL; 1517b83cc969SLi Zefan bool readonly = false; 15186f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 151972fd032eSSage Weil 1520fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1521fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1522fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1523fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1524fdfb1e4fSLi Zefan 1525b83cc969SLi Zefan if (vol_args->flags & 15266f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 15276f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1528b83cc969SLi Zefan ret = -EOPNOTSUPP; 1529fdfb1e4fSLi Zefan goto out; 1530fdfb1e4fSLi Zefan } 1531fdfb1e4fSLi Zefan 1532fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1533fdfb1e4fSLi Zefan ptr = &transid; 1534b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1535b83cc969SLi Zefan readonly = true; 15366f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 15376f72c7e2SArne Jansen if (vol_args->size > PAGE_CACHE_SIZE) { 15386f72c7e2SArne Jansen ret = -EINVAL; 15396f72c7e2SArne Jansen goto out; 15406f72c7e2SArne Jansen } 15416f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 15426f72c7e2SArne Jansen if (IS_ERR(inherit)) { 15436f72c7e2SArne Jansen ret = PTR_ERR(inherit); 15446f72c7e2SArne Jansen goto out; 15456f72c7e2SArne Jansen } 15466f72c7e2SArne Jansen } 154775eaa0e2SSage Weil 1548fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 15496f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 15506f72c7e2SArne Jansen readonly, &inherit); 155175eaa0e2SSage Weil 1552fdfb1e4fSLi Zefan if (ret == 0 && ptr && 155375eaa0e2SSage Weil copy_to_user(arg + 1554fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1555fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 155675eaa0e2SSage Weil ret = -EFAULT; 1557fdfb1e4fSLi Zefan out: 1558f46b5a66SChristoph Hellwig kfree(vol_args); 15596f72c7e2SArne Jansen kfree(inherit); 1560f46b5a66SChristoph Hellwig return ret; 1561f46b5a66SChristoph Hellwig } 1562f46b5a66SChristoph Hellwig 15630caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 15640caa102dSLi Zefan void __user *arg) 15650caa102dSLi Zefan { 15660caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 15670caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 15680caa102dSLi Zefan int ret = 0; 15690caa102dSLi Zefan u64 flags = 0; 15700caa102dSLi Zefan 157133345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 15720caa102dSLi Zefan return -EINVAL; 15730caa102dSLi Zefan 15740caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 15750caa102dSLi Zefan if (btrfs_root_readonly(root)) 15760caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 15770caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 15780caa102dSLi Zefan 15790caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 15800caa102dSLi Zefan ret = -EFAULT; 15810caa102dSLi Zefan 15820caa102dSLi Zefan return ret; 15830caa102dSLi Zefan } 15840caa102dSLi Zefan 15850caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 15860caa102dSLi Zefan void __user *arg) 15870caa102dSLi Zefan { 15880caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 15890caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 15900caa102dSLi Zefan struct btrfs_trans_handle *trans; 15910caa102dSLi Zefan u64 root_flags; 15920caa102dSLi Zefan u64 flags; 15930caa102dSLi Zefan int ret = 0; 15940caa102dSLi Zefan 1595b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1596b9ca0664SLiu Bo if (ret) 1597b9ca0664SLiu Bo goto out; 15980caa102dSLi Zefan 1599b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1600b9ca0664SLiu Bo ret = -EINVAL; 1601b9ca0664SLiu Bo goto out_drop_write; 1602b9ca0664SLiu Bo } 16030caa102dSLi Zefan 1604b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1605b9ca0664SLiu Bo ret = -EFAULT; 1606b9ca0664SLiu Bo goto out_drop_write; 1607b9ca0664SLiu Bo } 16080caa102dSLi Zefan 1609b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1610b9ca0664SLiu Bo ret = -EINVAL; 1611b9ca0664SLiu Bo goto out_drop_write; 1612b9ca0664SLiu Bo } 16130caa102dSLi Zefan 1614b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1615b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1616b9ca0664SLiu Bo goto out_drop_write; 1617b9ca0664SLiu Bo } 16180caa102dSLi Zefan 1619b9ca0664SLiu Bo if (!inode_owner_or_capable(inode)) { 1620b9ca0664SLiu Bo ret = -EACCES; 1621b9ca0664SLiu Bo goto out_drop_write; 1622b9ca0664SLiu Bo } 1623b4dc2b8cSLi Zefan 16240caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 16250caa102dSLi Zefan 16260caa102dSLi Zefan /* nothing to do */ 16270caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1628b9ca0664SLiu Bo goto out_drop_sem; 16290caa102dSLi Zefan 16300caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 16310caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 16320caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16330caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 16340caa102dSLi Zefan else 16350caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16360caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 16370caa102dSLi Zefan 16380caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 16390caa102dSLi Zefan if (IS_ERR(trans)) { 16400caa102dSLi Zefan ret = PTR_ERR(trans); 16410caa102dSLi Zefan goto out_reset; 16420caa102dSLi Zefan } 16430caa102dSLi Zefan 1644b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 16450caa102dSLi Zefan &root->root_key, &root->root_item); 16460caa102dSLi Zefan 16470caa102dSLi Zefan btrfs_commit_transaction(trans, root); 16480caa102dSLi Zefan out_reset: 16490caa102dSLi Zefan if (ret) 16500caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1651b9ca0664SLiu Bo out_drop_sem: 16520caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1653b9ca0664SLiu Bo out_drop_write: 1654b9ca0664SLiu Bo mnt_drop_write_file(file); 1655b9ca0664SLiu Bo out: 16560caa102dSLi Zefan return ret; 16570caa102dSLi Zefan } 16580caa102dSLi Zefan 165976dda93cSYan, Zheng /* 166076dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 166176dda93cSYan, Zheng */ 166276dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 166376dda93cSYan, Zheng { 166476dda93cSYan, Zheng struct btrfs_path *path; 166576dda93cSYan, Zheng struct btrfs_key key; 166676dda93cSYan, Zheng int ret; 166776dda93cSYan, Zheng 166876dda93cSYan, Zheng path = btrfs_alloc_path(); 166976dda93cSYan, Zheng if (!path) 167076dda93cSYan, Zheng return -ENOMEM; 167176dda93cSYan, Zheng 167276dda93cSYan, Zheng key.objectid = root->root_key.objectid; 167376dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 167476dda93cSYan, Zheng key.offset = (u64)-1; 167576dda93cSYan, Zheng 167676dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 167776dda93cSYan, Zheng &key, path, 0, 0); 167876dda93cSYan, Zheng if (ret < 0) 167976dda93cSYan, Zheng goto out; 168076dda93cSYan, Zheng BUG_ON(ret == 0); 168176dda93cSYan, Zheng 168276dda93cSYan, Zheng ret = 0; 168376dda93cSYan, Zheng if (path->slots[0] > 0) { 168476dda93cSYan, Zheng path->slots[0]--; 168576dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 168676dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 168776dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 168876dda93cSYan, Zheng ret = -ENOTEMPTY; 168976dda93cSYan, Zheng } 169076dda93cSYan, Zheng out: 169176dda93cSYan, Zheng btrfs_free_path(path); 169276dda93cSYan, Zheng return ret; 169376dda93cSYan, Zheng } 169476dda93cSYan, Zheng 1695ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1696ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1697ac8e9819SChris Mason { 1698abc6e134SChris Mason struct btrfs_key test; 1699abc6e134SChris Mason int ret; 1700abc6e134SChris Mason 1701abc6e134SChris Mason test.objectid = sk->min_objectid; 1702abc6e134SChris Mason test.type = sk->min_type; 1703abc6e134SChris Mason test.offset = sk->min_offset; 1704abc6e134SChris Mason 1705abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1706abc6e134SChris Mason if (ret < 0) 1707ac8e9819SChris Mason return 0; 1708abc6e134SChris Mason 1709abc6e134SChris Mason test.objectid = sk->max_objectid; 1710abc6e134SChris Mason test.type = sk->max_type; 1711abc6e134SChris Mason test.offset = sk->max_offset; 1712abc6e134SChris Mason 1713abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1714abc6e134SChris Mason if (ret > 0) 1715ac8e9819SChris Mason return 0; 1716ac8e9819SChris Mason return 1; 1717ac8e9819SChris Mason } 1718ac8e9819SChris Mason 1719ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1720ac8e9819SChris Mason struct btrfs_path *path, 1721ac8e9819SChris Mason struct btrfs_key *key, 1722ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1723ac8e9819SChris Mason char *buf, 1724ac8e9819SChris Mason unsigned long *sk_offset, 1725ac8e9819SChris Mason int *num_found) 1726ac8e9819SChris Mason { 1727ac8e9819SChris Mason u64 found_transid; 1728ac8e9819SChris Mason struct extent_buffer *leaf; 1729ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1730ac8e9819SChris Mason unsigned long item_off; 1731ac8e9819SChris Mason unsigned long item_len; 1732ac8e9819SChris Mason int nritems; 1733ac8e9819SChris Mason int i; 1734ac8e9819SChris Mason int slot; 1735ac8e9819SChris Mason int ret = 0; 1736ac8e9819SChris Mason 1737ac8e9819SChris Mason leaf = path->nodes[0]; 1738ac8e9819SChris Mason slot = path->slots[0]; 1739ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1740ac8e9819SChris Mason 1741ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1742ac8e9819SChris Mason i = nritems; 1743ac8e9819SChris Mason goto advance_key; 1744ac8e9819SChris Mason } 1745ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1746ac8e9819SChris Mason 1747ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1748ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1749ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1750ac8e9819SChris Mason 1751ac8e9819SChris Mason if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1752ac8e9819SChris Mason item_len = 0; 1753ac8e9819SChris Mason 1754ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1755ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1756ac8e9819SChris Mason ret = 1; 1757ac8e9819SChris Mason goto overflow; 1758ac8e9819SChris Mason } 1759ac8e9819SChris Mason 1760ac8e9819SChris Mason btrfs_item_key_to_cpu(leaf, key, i); 1761ac8e9819SChris Mason if (!key_in_sk(key, sk)) 1762ac8e9819SChris Mason continue; 1763ac8e9819SChris Mason 1764ac8e9819SChris Mason sh.objectid = key->objectid; 1765ac8e9819SChris Mason sh.offset = key->offset; 1766ac8e9819SChris Mason sh.type = key->type; 1767ac8e9819SChris Mason sh.len = item_len; 1768ac8e9819SChris Mason sh.transid = found_transid; 1769ac8e9819SChris Mason 1770ac8e9819SChris Mason /* copy search result header */ 1771ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1772ac8e9819SChris Mason *sk_offset += sizeof(sh); 1773ac8e9819SChris Mason 1774ac8e9819SChris Mason if (item_len) { 1775ac8e9819SChris Mason char *p = buf + *sk_offset; 1776ac8e9819SChris Mason /* copy the item */ 1777ac8e9819SChris Mason read_extent_buffer(leaf, p, 1778ac8e9819SChris Mason item_off, item_len); 1779ac8e9819SChris Mason *sk_offset += item_len; 1780ac8e9819SChris Mason } 1781e2156867SHugo Mills (*num_found)++; 1782ac8e9819SChris Mason 1783ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1784ac8e9819SChris Mason break; 1785ac8e9819SChris Mason } 1786ac8e9819SChris Mason advance_key: 1787ac8e9819SChris Mason ret = 0; 1788abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1789abc6e134SChris Mason key->offset++; 1790abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1791abc6e134SChris Mason key->offset = 0; 1792abc6e134SChris Mason key->type++; 1793abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1794abc6e134SChris Mason key->offset = 0; 1795abc6e134SChris Mason key->type = 0; 1796abc6e134SChris Mason key->objectid++; 1797abc6e134SChris Mason } else 1798abc6e134SChris Mason ret = 1; 1799ac8e9819SChris Mason overflow: 1800ac8e9819SChris Mason return ret; 1801ac8e9819SChris Mason } 1802ac8e9819SChris Mason 1803ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1804ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1805ac8e9819SChris Mason { 1806ac8e9819SChris Mason struct btrfs_root *root; 1807ac8e9819SChris Mason struct btrfs_key key; 1808ac8e9819SChris Mason struct btrfs_key max_key; 1809ac8e9819SChris Mason struct btrfs_path *path; 1810ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1811ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1812ac8e9819SChris Mason int ret; 1813ac8e9819SChris Mason int num_found = 0; 1814ac8e9819SChris Mason unsigned long sk_offset = 0; 1815ac8e9819SChris Mason 1816ac8e9819SChris Mason path = btrfs_alloc_path(); 1817ac8e9819SChris Mason if (!path) 1818ac8e9819SChris Mason return -ENOMEM; 1819ac8e9819SChris Mason 1820ac8e9819SChris Mason if (sk->tree_id == 0) { 1821ac8e9819SChris Mason /* search the root of the inode that was passed */ 1822ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1823ac8e9819SChris Mason } else { 1824ac8e9819SChris Mason key.objectid = sk->tree_id; 1825ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1826ac8e9819SChris Mason key.offset = (u64)-1; 1827ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1828ac8e9819SChris Mason if (IS_ERR(root)) { 1829ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1830ac8e9819SChris Mason sk->tree_id); 1831ac8e9819SChris Mason btrfs_free_path(path); 1832ac8e9819SChris Mason return -ENOENT; 1833ac8e9819SChris Mason } 1834ac8e9819SChris Mason } 1835ac8e9819SChris Mason 1836ac8e9819SChris Mason key.objectid = sk->min_objectid; 1837ac8e9819SChris Mason key.type = sk->min_type; 1838ac8e9819SChris Mason key.offset = sk->min_offset; 1839ac8e9819SChris Mason 1840ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1841ac8e9819SChris Mason max_key.type = sk->max_type; 1842ac8e9819SChris Mason max_key.offset = sk->max_offset; 1843ac8e9819SChris Mason 1844ac8e9819SChris Mason path->keep_locks = 1; 1845ac8e9819SChris Mason 1846ac8e9819SChris Mason while(1) { 1847ac8e9819SChris Mason ret = btrfs_search_forward(root, &key, &max_key, path, 0, 1848ac8e9819SChris Mason sk->min_transid); 1849ac8e9819SChris Mason if (ret != 0) { 1850ac8e9819SChris Mason if (ret > 0) 1851ac8e9819SChris Mason ret = 0; 1852ac8e9819SChris Mason goto err; 1853ac8e9819SChris Mason } 1854ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1855ac8e9819SChris Mason &sk_offset, &num_found); 1856b3b4aa74SDavid Sterba btrfs_release_path(path); 1857ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1858ac8e9819SChris Mason break; 1859ac8e9819SChris Mason 1860ac8e9819SChris Mason } 1861ac8e9819SChris Mason ret = 0; 1862ac8e9819SChris Mason err: 1863ac8e9819SChris Mason sk->nr_items = num_found; 1864ac8e9819SChris Mason btrfs_free_path(path); 1865ac8e9819SChris Mason return ret; 1866ac8e9819SChris Mason } 1867ac8e9819SChris Mason 1868ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1869ac8e9819SChris Mason void __user *argp) 1870ac8e9819SChris Mason { 1871ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1872ac8e9819SChris Mason struct inode *inode; 1873ac8e9819SChris Mason int ret; 1874ac8e9819SChris Mason 1875ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1876ac8e9819SChris Mason return -EPERM; 1877ac8e9819SChris Mason 18782354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 18792354d08fSJulia Lawall if (IS_ERR(args)) 18802354d08fSJulia Lawall return PTR_ERR(args); 1881ac8e9819SChris Mason 1882ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1883ac8e9819SChris Mason ret = search_ioctl(inode, args); 1884ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1885ac8e9819SChris Mason ret = -EFAULT; 1886ac8e9819SChris Mason kfree(args); 1887ac8e9819SChris Mason return ret; 1888ac8e9819SChris Mason } 1889ac8e9819SChris Mason 189098d377a0STARUISI Hiroaki /* 1891ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1892ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 189398d377a0STARUISI Hiroaki */ 189498d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 189598d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 189698d377a0STARUISI Hiroaki { 189798d377a0STARUISI Hiroaki struct btrfs_root *root; 189898d377a0STARUISI Hiroaki struct btrfs_key key; 1899ac8e9819SChris Mason char *ptr; 190098d377a0STARUISI Hiroaki int ret = -1; 190198d377a0STARUISI Hiroaki int slot; 190298d377a0STARUISI Hiroaki int len; 190398d377a0STARUISI Hiroaki int total_len = 0; 190498d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 190598d377a0STARUISI Hiroaki struct extent_buffer *l; 190698d377a0STARUISI Hiroaki struct btrfs_path *path; 190798d377a0STARUISI Hiroaki 190898d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 190998d377a0STARUISI Hiroaki name[0]='\0'; 191098d377a0STARUISI Hiroaki return 0; 191198d377a0STARUISI Hiroaki } 191298d377a0STARUISI Hiroaki 191398d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 191498d377a0STARUISI Hiroaki if (!path) 191598d377a0STARUISI Hiroaki return -ENOMEM; 191698d377a0STARUISI Hiroaki 1917ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 191898d377a0STARUISI Hiroaki 191998d377a0STARUISI Hiroaki key.objectid = tree_id; 192098d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 192198d377a0STARUISI Hiroaki key.offset = (u64)-1; 192298d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 192398d377a0STARUISI Hiroaki if (IS_ERR(root)) { 192498d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 19258ad6fcabSChris Mason ret = -ENOENT; 19268ad6fcabSChris Mason goto out; 192798d377a0STARUISI Hiroaki } 192898d377a0STARUISI Hiroaki 192998d377a0STARUISI Hiroaki key.objectid = dirid; 193098d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 19318ad6fcabSChris Mason key.offset = (u64)-1; 193298d377a0STARUISI Hiroaki 193398d377a0STARUISI Hiroaki while(1) { 193498d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 193598d377a0STARUISI Hiroaki if (ret < 0) 193698d377a0STARUISI Hiroaki goto out; 193798d377a0STARUISI Hiroaki 193898d377a0STARUISI Hiroaki l = path->nodes[0]; 193998d377a0STARUISI Hiroaki slot = path->slots[0]; 19408ad6fcabSChris Mason if (ret > 0 && slot > 0) 19418ad6fcabSChris Mason slot--; 194298d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 194398d377a0STARUISI Hiroaki 194498d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 1945ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 1946ac8e9819SChris Mason ret = -ENOENT; 194798d377a0STARUISI Hiroaki goto out; 1948ac8e9819SChris Mason } 194998d377a0STARUISI Hiroaki 195098d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 195198d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 195298d377a0STARUISI Hiroaki ptr -= len + 1; 195398d377a0STARUISI Hiroaki total_len += len + 1; 1954ac8e9819SChris Mason if (ptr < name) 195598d377a0STARUISI Hiroaki goto out; 195698d377a0STARUISI Hiroaki 195798d377a0STARUISI Hiroaki *(ptr + len) = '/'; 195898d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 195998d377a0STARUISI Hiroaki 196098d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 196198d377a0STARUISI Hiroaki break; 196298d377a0STARUISI Hiroaki 1963b3b4aa74SDavid Sterba btrfs_release_path(path); 196498d377a0STARUISI Hiroaki key.objectid = key.offset; 19658ad6fcabSChris Mason key.offset = (u64)-1; 196698d377a0STARUISI Hiroaki dirid = key.objectid; 196798d377a0STARUISI Hiroaki } 1968ac8e9819SChris Mason if (ptr < name) 196998d377a0STARUISI Hiroaki goto out; 197077906a50SLi Zefan memmove(name, ptr, total_len); 197198d377a0STARUISI Hiroaki name[total_len]='\0'; 197298d377a0STARUISI Hiroaki ret = 0; 197398d377a0STARUISI Hiroaki out: 197498d377a0STARUISI Hiroaki btrfs_free_path(path); 1975ac8e9819SChris Mason return ret; 1976ac8e9819SChris Mason } 1977ac8e9819SChris Mason 1978ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 1979ac8e9819SChris Mason void __user *argp) 1980ac8e9819SChris Mason { 1981ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 1982ac8e9819SChris Mason struct inode *inode; 1983ac8e9819SChris Mason int ret; 1984ac8e9819SChris Mason 1985ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1986ac8e9819SChris Mason return -EPERM; 1987ac8e9819SChris Mason 19882354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 19892354d08fSJulia Lawall if (IS_ERR(args)) 19902354d08fSJulia Lawall return PTR_ERR(args); 1991c2b96929SDan Carpenter 1992ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1993ac8e9819SChris Mason 19941b53ac4dSChris Mason if (args->treeid == 0) 19951b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 19961b53ac4dSChris Mason 1997ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 1998ac8e9819SChris Mason args->treeid, args->objectid, 1999ac8e9819SChris Mason args->name); 2000ac8e9819SChris Mason 2001ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2002ac8e9819SChris Mason ret = -EFAULT; 2003ac8e9819SChris Mason 2004ac8e9819SChris Mason kfree(args); 200598d377a0STARUISI Hiroaki return ret; 200698d377a0STARUISI Hiroaki } 200798d377a0STARUISI Hiroaki 200876dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 200976dda93cSYan, Zheng void __user *arg) 201076dda93cSYan, Zheng { 201176dda93cSYan, Zheng struct dentry *parent = fdentry(file); 201276dda93cSYan, Zheng struct dentry *dentry; 201376dda93cSYan, Zheng struct inode *dir = parent->d_inode; 201476dda93cSYan, Zheng struct inode *inode; 201576dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 201676dda93cSYan, Zheng struct btrfs_root *dest = NULL; 201776dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 201876dda93cSYan, Zheng struct btrfs_trans_handle *trans; 201976dda93cSYan, Zheng int namelen; 202076dda93cSYan, Zheng int ret; 202176dda93cSYan, Zheng int err = 0; 202276dda93cSYan, Zheng 202376dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 202476dda93cSYan, Zheng if (IS_ERR(vol_args)) 202576dda93cSYan, Zheng return PTR_ERR(vol_args); 202676dda93cSYan, Zheng 202776dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 202876dda93cSYan, Zheng namelen = strlen(vol_args->name); 202976dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 203076dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 203176dda93cSYan, Zheng err = -EINVAL; 203276dda93cSYan, Zheng goto out; 203376dda93cSYan, Zheng } 203476dda93cSYan, Zheng 2035a561be71SAl Viro err = mnt_want_write_file(file); 203676dda93cSYan, Zheng if (err) 203776dda93cSYan, Zheng goto out; 203876dda93cSYan, Zheng 203976dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 204076dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 204176dda93cSYan, Zheng if (IS_ERR(dentry)) { 204276dda93cSYan, Zheng err = PTR_ERR(dentry); 204376dda93cSYan, Zheng goto out_unlock_dir; 204476dda93cSYan, Zheng } 204576dda93cSYan, Zheng 204676dda93cSYan, Zheng if (!dentry->d_inode) { 204776dda93cSYan, Zheng err = -ENOENT; 204876dda93cSYan, Zheng goto out_dput; 204976dda93cSYan, Zheng } 205076dda93cSYan, Zheng 205176dda93cSYan, Zheng inode = dentry->d_inode; 20524260f7c7SSage Weil dest = BTRFS_I(inode)->root; 20534260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 20544260f7c7SSage Weil /* 20554260f7c7SSage Weil * Regular user. Only allow this with a special mount 20564260f7c7SSage Weil * option, when the user has write+exec access to the 20574260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 20584260f7c7SSage Weil * allowed. 20594260f7c7SSage Weil * 20604260f7c7SSage Weil * Note that this is _not_ check that the subvol is 20614260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 20624260f7c7SSage Weil * otherwise be able to delete. 20634260f7c7SSage Weil * 20644260f7c7SSage Weil * Users who want to delete empty subvols should try 20654260f7c7SSage Weil * rmdir(2). 20664260f7c7SSage Weil */ 20674260f7c7SSage Weil err = -EPERM; 20684260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 20694260f7c7SSage Weil goto out_dput; 20704260f7c7SSage Weil 20714260f7c7SSage Weil /* 20724260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 20734260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 20744260f7c7SSage Weil * must be called on the dentry referencing the root 20754260f7c7SSage Weil * of the subvol, not a random directory contained 20764260f7c7SSage Weil * within it. 20774260f7c7SSage Weil */ 20784260f7c7SSage Weil err = -EINVAL; 20794260f7c7SSage Weil if (root == dest) 20804260f7c7SSage Weil goto out_dput; 20814260f7c7SSage Weil 20824260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 20834260f7c7SSage Weil if (err) 20844260f7c7SSage Weil goto out_dput; 20854260f7c7SSage Weil 20864260f7c7SSage Weil /* check if subvolume may be deleted by a non-root user */ 20874260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 20884260f7c7SSage Weil if (err) 20894260f7c7SSage Weil goto out_dput; 20904260f7c7SSage Weil } 20914260f7c7SSage Weil 209233345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 209376dda93cSYan, Zheng err = -EINVAL; 209476dda93cSYan, Zheng goto out_dput; 209576dda93cSYan, Zheng } 209676dda93cSYan, Zheng 209776dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 209876dda93cSYan, Zheng err = d_invalidate(dentry); 209976dda93cSYan, Zheng if (err) 210076dda93cSYan, Zheng goto out_unlock; 210176dda93cSYan, Zheng 210276dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 210376dda93cSYan, Zheng 210476dda93cSYan, Zheng err = may_destroy_subvol(dest); 210576dda93cSYan, Zheng if (err) 210676dda93cSYan, Zheng goto out_up_write; 210776dda93cSYan, Zheng 2108a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2109a22285a6SYan, Zheng if (IS_ERR(trans)) { 2110a22285a6SYan, Zheng err = PTR_ERR(trans); 2111d327099aSDan Carpenter goto out_up_write; 2112a22285a6SYan, Zheng } 2113a22285a6SYan, Zheng trans->block_rsv = &root->fs_info->global_block_rsv; 2114a22285a6SYan, Zheng 211576dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 211676dda93cSYan, Zheng dest->root_key.objectid, 211776dda93cSYan, Zheng dentry->d_name.name, 211876dda93cSYan, Zheng dentry->d_name.len); 211979787eaaSJeff Mahoney if (ret) { 212079787eaaSJeff Mahoney err = ret; 212179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 212279787eaaSJeff Mahoney goto out_end_trans; 212379787eaaSJeff Mahoney } 212476dda93cSYan, Zheng 212576dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 212676dda93cSYan, Zheng 212776dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 212876dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 212976dda93cSYan, Zheng dest->root_item.drop_level = 0; 213076dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 213176dda93cSYan, Zheng 2132d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 213376dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 213476dda93cSYan, Zheng root->fs_info->tree_root, 213576dda93cSYan, Zheng dest->root_key.objectid); 213679787eaaSJeff Mahoney if (ret) { 213779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 213879787eaaSJeff Mahoney err = ret; 213979787eaaSJeff Mahoney goto out_end_trans; 2140d68fc57bSYan, Zheng } 214179787eaaSJeff Mahoney } 214279787eaaSJeff Mahoney out_end_trans: 2143531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 214479787eaaSJeff Mahoney if (ret && !err) 214579787eaaSJeff Mahoney err = ret; 214676dda93cSYan, Zheng inode->i_flags |= S_DEAD; 214776dda93cSYan, Zheng out_up_write: 214876dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 214976dda93cSYan, Zheng out_unlock: 215076dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 215176dda93cSYan, Zheng if (!err) { 2152efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 215376dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 215476dda93cSYan, Zheng d_delete(dentry); 215576dda93cSYan, Zheng } 215676dda93cSYan, Zheng out_dput: 215776dda93cSYan, Zheng dput(dentry); 215876dda93cSYan, Zheng out_unlock_dir: 215976dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 21602a79f17eSAl Viro mnt_drop_write_file(file); 216176dda93cSYan, Zheng out: 216276dda93cSYan, Zheng kfree(vol_args); 216376dda93cSYan, Zheng return err; 216476dda93cSYan, Zheng } 216576dda93cSYan, Zheng 21661e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2167f46b5a66SChristoph Hellwig { 2168f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2169f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 21701e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2171c146afadSYan Zheng int ret; 2172c146afadSYan Zheng 2173b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2174b83cc969SLi Zefan return -EROFS; 2175b83cc969SLi Zefan 21765ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 21775ac00addSStefan Behrens 1)) { 21785ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 21795ac00addSStefan Behrens return -EINPROGRESS; 21805ac00addSStefan Behrens } 2181a561be71SAl Viro ret = mnt_want_write_file(file); 21825ac00addSStefan Behrens if (ret) { 21835ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 21845ac00addSStefan Behrens 0); 2185c146afadSYan Zheng return ret; 21865ac00addSStefan Behrens } 2187f46b5a66SChristoph Hellwig 2188f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2189f46b5a66SChristoph Hellwig case S_IFDIR: 2190e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2191e441d54dSChris Mason ret = -EPERM; 2192e441d54dSChris Mason goto out; 2193e441d54dSChris Mason } 21948929ecfaSYan, Zheng ret = btrfs_defrag_root(root, 0); 21958929ecfaSYan, Zheng if (ret) 21968929ecfaSYan, Zheng goto out; 21978929ecfaSYan, Zheng ret = btrfs_defrag_root(root->fs_info->extent_root, 0); 2198f46b5a66SChristoph Hellwig break; 2199f46b5a66SChristoph Hellwig case S_IFREG: 2200e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2201e441d54dSChris Mason ret = -EINVAL; 2202e441d54dSChris Mason goto out; 2203e441d54dSChris Mason } 22041e701a32SChris Mason 22051e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 22061e701a32SChris Mason if (!range) { 22071e701a32SChris Mason ret = -ENOMEM; 22081e701a32SChris Mason goto out; 22091e701a32SChris Mason } 22101e701a32SChris Mason 22111e701a32SChris Mason if (argp) { 22121e701a32SChris Mason if (copy_from_user(range, argp, 22131e701a32SChris Mason sizeof(*range))) { 22141e701a32SChris Mason ret = -EFAULT; 22151e701a32SChris Mason kfree(range); 2216683be16eSDan Carpenter goto out; 22171e701a32SChris Mason } 22181e701a32SChris Mason /* compression requires us to start the IO */ 22191e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 22201e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 22211e701a32SChris Mason range->extent_thresh = (u32)-1; 22221e701a32SChris Mason } 22231e701a32SChris Mason } else { 22241e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 22251e701a32SChris Mason range->len = (u64)-1; 22261e701a32SChris Mason } 22274cb5300bSChris Mason ret = btrfs_defrag_file(fdentry(file)->d_inode, file, 22284cb5300bSChris Mason range, 0, 0); 22294cb5300bSChris Mason if (ret > 0) 22304cb5300bSChris Mason ret = 0; 22311e701a32SChris Mason kfree(range); 2232f46b5a66SChristoph Hellwig break; 22338929ecfaSYan, Zheng default: 22348929ecfaSYan, Zheng ret = -EINVAL; 2235f46b5a66SChristoph Hellwig } 2236e441d54dSChris Mason out: 22372a79f17eSAl Viro mnt_drop_write_file(file); 22385ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2239e441d54dSChris Mason return ret; 2240f46b5a66SChristoph Hellwig } 2241f46b5a66SChristoph Hellwig 2242b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2243f46b5a66SChristoph Hellwig { 2244f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2245f46b5a66SChristoph Hellwig int ret; 2246f46b5a66SChristoph Hellwig 2247e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2248e441d54dSChris Mason return -EPERM; 2249e441d54dSChris Mason 22505ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 22515ac00addSStefan Behrens 1)) { 22525ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 22535ac00addSStefan Behrens return -EINPROGRESS; 2254c9e9f97bSIlya Dryomov } 2255c9e9f97bSIlya Dryomov 22565ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2257dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2258c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2259c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2260c9e9f97bSIlya Dryomov goto out; 2261c9e9f97bSIlya Dryomov } 2262f46b5a66SChristoph Hellwig 22635516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2264f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2265f46b5a66SChristoph Hellwig 2266f46b5a66SChristoph Hellwig kfree(vol_args); 2267c9e9f97bSIlya Dryomov out: 2268c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 22695ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2270f46b5a66SChristoph Hellwig return ret; 2271f46b5a66SChristoph Hellwig } 2272f46b5a66SChristoph Hellwig 2273da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2274f46b5a66SChristoph Hellwig { 2275da24927bSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 2276f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2277f46b5a66SChristoph Hellwig int ret; 2278f46b5a66SChristoph Hellwig 2279e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2280e441d54dSChris Mason return -EPERM; 2281e441d54dSChris Mason 2282da24927bSMiao Xie ret = mnt_want_write_file(file); 2283da24927bSMiao Xie if (ret) 2284da24927bSMiao Xie return ret; 2285c146afadSYan Zheng 22865ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 22875ac00addSStefan Behrens 1)) { 22885ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2289da24927bSMiao Xie mnt_drop_write_file(file); 22905ac00addSStefan Behrens return -EINPROGRESS; 2291c9e9f97bSIlya Dryomov } 2292c9e9f97bSIlya Dryomov 22935ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2294dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2295c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2296c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2297c9e9f97bSIlya Dryomov goto out; 2298c9e9f97bSIlya Dryomov } 2299f46b5a66SChristoph Hellwig 23005516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2301f46b5a66SChristoph Hellwig ret = btrfs_rm_device(root, vol_args->name); 2302f46b5a66SChristoph Hellwig 2303f46b5a66SChristoph Hellwig kfree(vol_args); 2304c9e9f97bSIlya Dryomov out: 2305c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 2306da24927bSMiao Xie mnt_drop_write_file(file); 23075ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2308f46b5a66SChristoph Hellwig return ret; 2309f46b5a66SChristoph Hellwig } 2310f46b5a66SChristoph Hellwig 2311475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2312475f6387SJan Schmidt { 2313027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2314475f6387SJan Schmidt struct btrfs_device *device; 2315475f6387SJan Schmidt struct btrfs_device *next; 2316475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2317027ed2f0SLi Zefan int ret = 0; 2318475f6387SJan Schmidt 2319475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2320475f6387SJan Schmidt return -EPERM; 2321475f6387SJan Schmidt 2322027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2323027ed2f0SLi Zefan if (!fi_args) 2324027ed2f0SLi Zefan return -ENOMEM; 2325027ed2f0SLi Zefan 2326027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2327027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2328475f6387SJan Schmidt 2329475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2330475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2331027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2332027ed2f0SLi Zefan fi_args->max_id = device->devid; 2333475f6387SJan Schmidt } 2334475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2335475f6387SJan Schmidt 2336027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2337027ed2f0SLi Zefan ret = -EFAULT; 2338475f6387SJan Schmidt 2339027ed2f0SLi Zefan kfree(fi_args); 2340027ed2f0SLi Zefan return ret; 2341475f6387SJan Schmidt } 2342475f6387SJan Schmidt 2343475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2344475f6387SJan Schmidt { 2345475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2346475f6387SJan Schmidt struct btrfs_device *dev; 2347475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2348475f6387SJan Schmidt int ret = 0; 2349475f6387SJan Schmidt char *s_uuid = NULL; 2350475f6387SJan Schmidt char empty_uuid[BTRFS_UUID_SIZE] = {0}; 2351475f6387SJan Schmidt 2352475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2353475f6387SJan Schmidt return -EPERM; 2354475f6387SJan Schmidt 2355475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2356475f6387SJan Schmidt if (IS_ERR(di_args)) 2357475f6387SJan Schmidt return PTR_ERR(di_args); 2358475f6387SJan Schmidt 2359475f6387SJan Schmidt if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) 2360475f6387SJan Schmidt s_uuid = di_args->uuid; 2361475f6387SJan Schmidt 2362475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2363aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2364475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2365475f6387SJan Schmidt 2366475f6387SJan Schmidt if (!dev) { 2367475f6387SJan Schmidt ret = -ENODEV; 2368475f6387SJan Schmidt goto out; 2369475f6387SJan Schmidt } 2370475f6387SJan Schmidt 2371475f6387SJan Schmidt di_args->devid = dev->devid; 2372475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2373475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2374475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2375a27202fbSJim Meyering if (dev->name) { 2376606686eeSJosef Bacik struct rcu_string *name; 2377606686eeSJosef Bacik 2378606686eeSJosef Bacik rcu_read_lock(); 2379606686eeSJosef Bacik name = rcu_dereference(dev->name); 2380606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2381606686eeSJosef Bacik rcu_read_unlock(); 2382a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2383a27202fbSJim Meyering } else { 238499ba55adSStefan Behrens di_args->path[0] = '\0'; 2385a27202fbSJim Meyering } 2386475f6387SJan Schmidt 2387475f6387SJan Schmidt out: 2388475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2389475f6387SJan Schmidt ret = -EFAULT; 2390475f6387SJan Schmidt 2391475f6387SJan Schmidt kfree(di_args); 2392475f6387SJan Schmidt return ret; 2393475f6387SJan Schmidt } 2394475f6387SJan Schmidt 239576dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 2396b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 2397f46b5a66SChristoph Hellwig { 2398f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2399f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 24002903ff01SAl Viro struct fd src_file; 2401f46b5a66SChristoph Hellwig struct inode *src; 2402f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2403f46b5a66SChristoph Hellwig struct btrfs_path *path; 2404f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 2405ae01a0abSYan Zheng char *buf; 2406ae01a0abSYan Zheng struct btrfs_key key; 2407f46b5a66SChristoph Hellwig u32 nritems; 2408f46b5a66SChristoph Hellwig int slot; 2409ae01a0abSYan Zheng int ret; 2410c5c9cd4dSSage Weil u64 len = olen; 2411c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 2412d20f7043SChris Mason 2413c5c9cd4dSSage Weil /* 2414c5c9cd4dSSage Weil * TODO: 2415c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 2416c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 2417c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 2418c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 2419c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 2420c5c9cd4dSSage Weil * they don't overlap)? 2421c5c9cd4dSSage Weil */ 2422c5c9cd4dSSage Weil 2423e441d54dSChris Mason /* the destination must be opened for writing */ 24242ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 2425e441d54dSChris Mason return -EINVAL; 2426e441d54dSChris Mason 2427b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2428b83cc969SLi Zefan return -EROFS; 2429b83cc969SLi Zefan 2430a561be71SAl Viro ret = mnt_want_write_file(file); 2431c146afadSYan Zheng if (ret) 2432c146afadSYan Zheng return ret; 2433c146afadSYan Zheng 24342903ff01SAl Viro src_file = fdget(srcfd); 24352903ff01SAl Viro if (!src_file.file) { 2436ab67b7c1SYan Zheng ret = -EBADF; 2437ab67b7c1SYan Zheng goto out_drop_write; 2438ab67b7c1SYan Zheng } 24395dc64164SDan Rosenberg 2440362a20c5SDavid Sterba ret = -EXDEV; 24412903ff01SAl Viro if (src_file.file->f_path.mnt != file->f_path.mnt) 2442362a20c5SDavid Sterba goto out_fput; 2443362a20c5SDavid Sterba 24442903ff01SAl Viro src = src_file.file->f_dentry->d_inode; 2445f46b5a66SChristoph Hellwig 2446c5c9cd4dSSage Weil ret = -EINVAL; 2447c5c9cd4dSSage Weil if (src == inode) 2448c5c9cd4dSSage Weil goto out_fput; 2449c5c9cd4dSSage Weil 24505dc64164SDan Rosenberg /* the src must be open for reading */ 24512903ff01SAl Viro if (!(src_file.file->f_mode & FMODE_READ)) 24525dc64164SDan Rosenberg goto out_fput; 24535dc64164SDan Rosenberg 24540e7b824cSLi Zefan /* don't make the dst file partly checksummed */ 24550e7b824cSLi Zefan if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 24560e7b824cSLi Zefan (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 24570e7b824cSLi Zefan goto out_fput; 24580e7b824cSLi Zefan 2459ae01a0abSYan Zheng ret = -EISDIR; 2460ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2461f46b5a66SChristoph Hellwig goto out_fput; 2462f46b5a66SChristoph Hellwig 2463ae01a0abSYan Zheng ret = -EXDEV; 2464362a20c5SDavid Sterba if (src->i_sb != inode->i_sb) 2465ae01a0abSYan Zheng goto out_fput; 2466ae01a0abSYan Zheng 2467ae01a0abSYan Zheng ret = -ENOMEM; 2468ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2469ae01a0abSYan Zheng if (!buf) 2470ae01a0abSYan Zheng goto out_fput; 2471ae01a0abSYan Zheng 2472ae01a0abSYan Zheng path = btrfs_alloc_path(); 2473ae01a0abSYan Zheng if (!path) { 2474ae01a0abSYan Zheng vfree(buf); 2475ae01a0abSYan Zheng goto out_fput; 2476ae01a0abSYan Zheng } 2477ae01a0abSYan Zheng path->reada = 2; 2478ae01a0abSYan Zheng 2479f46b5a66SChristoph Hellwig if (inode < src) { 2480fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 2481fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 2482f46b5a66SChristoph Hellwig } else { 2483fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 2484fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 2485f46b5a66SChristoph Hellwig } 2486f46b5a66SChristoph Hellwig 2487c5c9cd4dSSage Weil /* determine range to clone */ 2488c5c9cd4dSSage Weil ret = -EINVAL; 24892ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 2490f46b5a66SChristoph Hellwig goto out_unlock; 2491c5c9cd4dSSage Weil if (len == 0) 2492c5c9cd4dSSage Weil olen = len = src->i_size - off; 2493c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 2494c5c9cd4dSSage Weil if (off + len == src->i_size) 24952a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 2496c5c9cd4dSSage Weil 2497c5c9cd4dSSage Weil /* verify the end result is block aligned */ 24982a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 24992a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 2500c5c9cd4dSSage Weil goto out_unlock; 2501c5c9cd4dSSage Weil 2502d525e8abSLi Zefan if (destoff > inode->i_size) { 2503d525e8abSLi Zefan ret = btrfs_cont_expand(inode, inode->i_size, destoff); 2504d525e8abSLi Zefan if (ret) 2505d525e8abSLi Zefan goto out_unlock; 2506d525e8abSLi Zefan } 2507d525e8abSLi Zefan 250871ef0786SLi Zefan /* truncate page cache pages from target inode range */ 250971ef0786SLi Zefan truncate_inode_pages_range(&inode->i_data, destoff, 251071ef0786SLi Zefan PAGE_CACHE_ALIGN(destoff + len) - 1); 251171ef0786SLi Zefan 2512f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 2513f46b5a66SChristoph Hellwig another, and lock file content */ 2514f46b5a66SChristoph Hellwig while (1) { 251531840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 2516aa42ffd9SLiu Bo lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2517aa42ffd9SLiu Bo ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1); 25189a019196SSage Weil if (!ordered && 2519aa42ffd9SLiu Bo !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1, 25209a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 2521f46b5a66SChristoph Hellwig break; 2522aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2523ae01a0abSYan Zheng if (ordered) 2524ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 25259a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 2526f46b5a66SChristoph Hellwig } 2527f46b5a66SChristoph Hellwig 2528c5c9cd4dSSage Weil /* clone data */ 252933345d01SLi Zefan key.objectid = btrfs_ino(src); 2530ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2531ae01a0abSYan Zheng key.offset = 0; 2532f46b5a66SChristoph Hellwig 2533f46b5a66SChristoph Hellwig while (1) { 2534f46b5a66SChristoph Hellwig /* 2535f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2536f46b5a66SChristoph Hellwig * tree. 2537f46b5a66SChristoph Hellwig */ 2538362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 2539362a20c5SDavid Sterba 0, 0); 2540f46b5a66SChristoph Hellwig if (ret < 0) 2541f46b5a66SChristoph Hellwig goto out; 2542f46b5a66SChristoph Hellwig 2543ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2544ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2545362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 2546f46b5a66SChristoph Hellwig if (ret < 0) 2547f46b5a66SChristoph Hellwig goto out; 2548f46b5a66SChristoph Hellwig if (ret > 0) 2549f46b5a66SChristoph Hellwig break; 2550ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2551f46b5a66SChristoph Hellwig } 2552f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2553f46b5a66SChristoph Hellwig slot = path->slots[0]; 2554f46b5a66SChristoph Hellwig 2555ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2556d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 255733345d01SLi Zefan key.objectid != btrfs_ino(src)) 2558f46b5a66SChristoph Hellwig break; 2559f46b5a66SChristoph Hellwig 2560c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2561c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2562c5c9cd4dSSage Weil int type; 256331840ae1SZheng Yan u32 size; 256431840ae1SZheng Yan struct btrfs_key new_key; 2565c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2566c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2567c5c9cd4dSSage Weil u8 comp; 2568b5384d48SSage Weil u64 endoff; 256931840ae1SZheng Yan 257031840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 257131840ae1SZheng Yan read_extent_buffer(leaf, buf, 257231840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 257331840ae1SZheng Yan size); 2574c5c9cd4dSSage Weil 2575c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2576c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2577c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2578c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2579c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2580c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2581d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2582d397712bSChris Mason extent); 2583d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2584d397712bSChris Mason extent); 2585c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2586d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2587d397712bSChris Mason extent); 2588c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2589c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2590c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2591c5c9cd4dSSage Weil extent); 2592c5c9cd4dSSage Weil } 2593b3b4aa74SDavid Sterba btrfs_release_path(path); 259431840ae1SZheng Yan 2595050006a7SSage Weil if (key.offset + datal <= off || 2596aa42ffd9SLiu Bo key.offset >= off + len - 1) 2597c5c9cd4dSSage Weil goto next; 2598c5c9cd4dSSage Weil 259931840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 260033345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 26014d728ec7SLi Zefan if (off <= key.offset) 2602c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 26034d728ec7SLi Zefan else 26044d728ec7SLi Zefan new_key.offset = destoff; 2605c5c9cd4dSSage Weil 2606b6f3409bSSage Weil /* 2607b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2608b6f3409bSSage Weil * 1 - add new extent 2609b6f3409bSSage Weil * 1 - inode update 2610b6f3409bSSage Weil */ 2611b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2612a22285a6SYan, Zheng if (IS_ERR(trans)) { 2613a22285a6SYan, Zheng ret = PTR_ERR(trans); 2614a22285a6SYan, Zheng goto out; 2615a22285a6SYan, Zheng } 2616a22285a6SYan, Zheng 2617c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2618c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2619d72c0842SLi Zefan /* 2620d72c0842SLi Zefan * a | --- range to clone ---| b 2621d72c0842SLi Zefan * | ------------- extent ------------- | 2622d72c0842SLi Zefan */ 2623d72c0842SLi Zefan 2624d72c0842SLi Zefan /* substract range b */ 2625d72c0842SLi Zefan if (key.offset + datal > off + len) 2626d72c0842SLi Zefan datal = off + len - key.offset; 2627d72c0842SLi Zefan 2628d72c0842SLi Zefan /* substract range a */ 2629a22285a6SYan, Zheng if (off > key.offset) { 2630a22285a6SYan, Zheng datao += off - key.offset; 2631a22285a6SYan, Zheng datal -= off - key.offset; 2632a22285a6SYan, Zheng } 2633a22285a6SYan, Zheng 26345dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2635a22285a6SYan, Zheng new_key.offset, 2636a22285a6SYan, Zheng new_key.offset + datal, 26372671485dSJosef Bacik 1); 263879787eaaSJeff Mahoney if (ret) { 263979787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 264079787eaaSJeff Mahoney ret); 264179787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 264279787eaaSJeff Mahoney goto out; 264379787eaaSJeff Mahoney } 2644a22285a6SYan, Zheng 264531840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 264631840ae1SZheng Yan &new_key, size); 264779787eaaSJeff Mahoney if (ret) { 264879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 264979787eaaSJeff Mahoney ret); 265079787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 265179787eaaSJeff Mahoney goto out; 265279787eaaSJeff Mahoney } 265331840ae1SZheng Yan 265431840ae1SZheng Yan leaf = path->nodes[0]; 265531840ae1SZheng Yan slot = path->slots[0]; 265631840ae1SZheng Yan write_extent_buffer(leaf, buf, 265731840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 265831840ae1SZheng Yan size); 2659ae01a0abSYan Zheng 2660f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 2661f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 2662c5c9cd4dSSage Weil 2663c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 2664c5c9cd4dSSage Weil if (!disko) 2665c5c9cd4dSSage Weil datao = 0; 2666c5c9cd4dSSage Weil 2667c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 2668c5c9cd4dSSage Weil datao); 2669c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 2670c5c9cd4dSSage Weil datal); 2671c5c9cd4dSSage Weil if (disko) { 2672c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2673ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 26745d4f98a2SYan Zheng disko, diskl, 0, 2675f46b5a66SChristoph Hellwig root->root_key.objectid, 267633345d01SLi Zefan btrfs_ino(inode), 267766d7e7f0SArne Jansen new_key.offset - datao, 267866d7e7f0SArne Jansen 0); 267979787eaaSJeff Mahoney if (ret) { 268079787eaaSJeff Mahoney btrfs_abort_transaction(trans, 268179787eaaSJeff Mahoney root, 268279787eaaSJeff Mahoney ret); 268379787eaaSJeff Mahoney btrfs_end_transaction(trans, 268479787eaaSJeff Mahoney root); 268579787eaaSJeff Mahoney goto out; 268679787eaaSJeff Mahoney 268779787eaaSJeff Mahoney } 2688f46b5a66SChristoph Hellwig } 2689c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2690c5c9cd4dSSage Weil u64 skip = 0; 2691c5c9cd4dSSage Weil u64 trim = 0; 2692c5c9cd4dSSage Weil if (off > key.offset) { 2693c5c9cd4dSSage Weil skip = off - key.offset; 2694c5c9cd4dSSage Weil new_key.offset += skip; 269531840ae1SZheng Yan } 2696d397712bSChris Mason 2697c5c9cd4dSSage Weil if (key.offset + datal > off + len) 2698c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 2699d397712bSChris Mason 2700c5c9cd4dSSage Weil if (comp && (skip || trim)) { 2701c5c9cd4dSSage Weil ret = -EINVAL; 2702a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2703c5c9cd4dSSage Weil goto out; 270431840ae1SZheng Yan } 2705c5c9cd4dSSage Weil size -= skip + trim; 2706c5c9cd4dSSage Weil datal -= skip + trim; 2707a22285a6SYan, Zheng 27085dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2709a22285a6SYan, Zheng new_key.offset, 2710a22285a6SYan, Zheng new_key.offset + datal, 27112671485dSJosef Bacik 1); 271279787eaaSJeff Mahoney if (ret) { 271379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 271479787eaaSJeff Mahoney ret); 271579787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 271679787eaaSJeff Mahoney goto out; 271779787eaaSJeff Mahoney } 2718a22285a6SYan, Zheng 2719c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 2720c5c9cd4dSSage Weil &new_key, size); 272179787eaaSJeff Mahoney if (ret) { 272279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 272379787eaaSJeff Mahoney ret); 272479787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 272579787eaaSJeff Mahoney goto out; 272679787eaaSJeff Mahoney } 2727c5c9cd4dSSage Weil 2728c5c9cd4dSSage Weil if (skip) { 2729d397712bSChris Mason u32 start = 2730d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 2731c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 2732c5c9cd4dSSage Weil datal); 2733c5c9cd4dSSage Weil } 2734c5c9cd4dSSage Weil 2735c5c9cd4dSSage Weil leaf = path->nodes[0]; 2736c5c9cd4dSSage Weil slot = path->slots[0]; 2737c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2738c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2739c5c9cd4dSSage Weil size); 2740c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2741c5c9cd4dSSage Weil } 2742c5c9cd4dSSage Weil 2743c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2744b3b4aa74SDavid Sterba btrfs_release_path(path); 2745c5c9cd4dSSage Weil 27460c4d2d95SJosef Bacik inode_inc_iversion(inode); 2747a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2748b5384d48SSage Weil 2749b5384d48SSage Weil /* 2750b5384d48SSage Weil * we round up to the block size at eof when 2751b5384d48SSage Weil * determining which extents to clone above, 2752b5384d48SSage Weil * but shouldn't round up the file size 2753b5384d48SSage Weil */ 2754b5384d48SSage Weil endoff = new_key.offset + datal; 27555f3888ffSLi Zefan if (endoff > destoff+olen) 27565f3888ffSLi Zefan endoff = destoff+olen; 2757b5384d48SSage Weil if (endoff > inode->i_size) 2758b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2759b5384d48SSage Weil 2760a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 276179787eaaSJeff Mahoney if (ret) { 276279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 2763a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 276479787eaaSJeff Mahoney goto out; 276579787eaaSJeff Mahoney } 276679787eaaSJeff Mahoney ret = btrfs_end_transaction(trans, root); 2767a22285a6SYan, Zheng } 2768c5c9cd4dSSage Weil next: 2769b3b4aa74SDavid Sterba btrfs_release_path(path); 2770ae01a0abSYan Zheng key.offset++; 2771ae01a0abSYan Zheng } 2772f46b5a66SChristoph Hellwig ret = 0; 2773f46b5a66SChristoph Hellwig out: 2774b3b4aa74SDavid Sterba btrfs_release_path(path); 2775aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2776f46b5a66SChristoph Hellwig out_unlock: 2777f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2778f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2779ae01a0abSYan Zheng vfree(buf); 2780ae01a0abSYan Zheng btrfs_free_path(path); 2781f46b5a66SChristoph Hellwig out_fput: 27822903ff01SAl Viro fdput(src_file); 2783ab67b7c1SYan Zheng out_drop_write: 27842a79f17eSAl Viro mnt_drop_write_file(file); 2785f46b5a66SChristoph Hellwig return ret; 2786f46b5a66SChristoph Hellwig } 2787f46b5a66SChristoph Hellwig 27887a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2789c5c9cd4dSSage Weil { 2790c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2791c5c9cd4dSSage Weil 27927a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2793c5c9cd4dSSage Weil return -EFAULT; 2794c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2795c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2796c5c9cd4dSSage Weil } 2797c5c9cd4dSSage Weil 2798f46b5a66SChristoph Hellwig /* 2799f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2800f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2801f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2802f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2803f46b5a66SChristoph Hellwig */ 2804b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2805f46b5a66SChristoph Hellwig { 2806f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2807f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2808f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 28091ab86aedSSage Weil int ret; 2810f46b5a66SChristoph Hellwig 28111ab86aedSSage Weil ret = -EPERM; 2812df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2813f46b5a66SChristoph Hellwig goto out; 28141ab86aedSSage Weil 28151ab86aedSSage Weil ret = -EINPROGRESS; 28161ab86aedSSage Weil if (file->private_data) 28171ab86aedSSage Weil goto out; 28189ca9ee09SSage Weil 2819b83cc969SLi Zefan ret = -EROFS; 2820b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2821b83cc969SLi Zefan goto out; 2822b83cc969SLi Zefan 2823a561be71SAl Viro ret = mnt_want_write_file(file); 2824c146afadSYan Zheng if (ret) 2825c146afadSYan Zheng goto out; 2826c146afadSYan Zheng 2827a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 28289ca9ee09SSage Weil 2829f46b5a66SChristoph Hellwig ret = -ENOMEM; 28307a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 2831abd30bb0STsutomu Itoh if (IS_ERR(trans)) 28321ab86aedSSage Weil goto out_drop; 28331ab86aedSSage Weil 28341ab86aedSSage Weil file->private_data = trans; 28351ab86aedSSage Weil return 0; 28361ab86aedSSage Weil 28371ab86aedSSage Weil out_drop: 2838a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 28392a79f17eSAl Viro mnt_drop_write_file(file); 2840f46b5a66SChristoph Hellwig out: 2841f46b5a66SChristoph Hellwig return ret; 2842f46b5a66SChristoph Hellwig } 2843f46b5a66SChristoph Hellwig 28446ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 28456ef5ed0dSJosef Bacik { 28466ef5ed0dSJosef Bacik struct inode *inode = fdentry(file)->d_inode; 28476ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 28486ef5ed0dSJosef Bacik struct btrfs_root *new_root; 28496ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 28506ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 28516ef5ed0dSJosef Bacik struct btrfs_path *path; 28526ef5ed0dSJosef Bacik struct btrfs_key location; 28536ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 28546ef5ed0dSJosef Bacik u64 objectid = 0; 28556ef5ed0dSJosef Bacik u64 dir_id; 28563c04ce01SMiao Xie int ret; 28576ef5ed0dSJosef Bacik 28586ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 28596ef5ed0dSJosef Bacik return -EPERM; 28606ef5ed0dSJosef Bacik 28613c04ce01SMiao Xie ret = mnt_want_write_file(file); 28623c04ce01SMiao Xie if (ret) 28633c04ce01SMiao Xie return ret; 28643c04ce01SMiao Xie 28653c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 28663c04ce01SMiao Xie ret = -EFAULT; 28673c04ce01SMiao Xie goto out; 28683c04ce01SMiao Xie } 28696ef5ed0dSJosef Bacik 28706ef5ed0dSJosef Bacik if (!objectid) 28716ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 28726ef5ed0dSJosef Bacik 28736ef5ed0dSJosef Bacik location.objectid = objectid; 28746ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 28756ef5ed0dSJosef Bacik location.offset = (u64)-1; 28766ef5ed0dSJosef Bacik 28776ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 28783c04ce01SMiao Xie if (IS_ERR(new_root)) { 28793c04ce01SMiao Xie ret = PTR_ERR(new_root); 28803c04ce01SMiao Xie goto out; 28813c04ce01SMiao Xie } 28826ef5ed0dSJosef Bacik 28833c04ce01SMiao Xie if (btrfs_root_refs(&new_root->root_item) == 0) { 28843c04ce01SMiao Xie ret = -ENOENT; 28853c04ce01SMiao Xie goto out; 28863c04ce01SMiao Xie } 28876ef5ed0dSJosef Bacik 28886ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 28893c04ce01SMiao Xie if (!path) { 28903c04ce01SMiao Xie ret = -ENOMEM; 28913c04ce01SMiao Xie goto out; 28923c04ce01SMiao Xie } 28936ef5ed0dSJosef Bacik path->leave_spinning = 1; 28946ef5ed0dSJosef Bacik 28956ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 289698d5dc13STsutomu Itoh if (IS_ERR(trans)) { 28976ef5ed0dSJosef Bacik btrfs_free_path(path); 28983c04ce01SMiao Xie ret = PTR_ERR(trans); 28993c04ce01SMiao Xie goto out; 29006ef5ed0dSJosef Bacik } 29016ef5ed0dSJosef Bacik 29026c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 29036ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 29046ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2905cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 29066ef5ed0dSJosef Bacik btrfs_free_path(path); 29076ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 29086ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 29096ef5ed0dSJosef Bacik "this isn't going to work\n"); 29103c04ce01SMiao Xie ret = -ENOENT; 29113c04ce01SMiao Xie goto out; 29126ef5ed0dSJosef Bacik } 29136ef5ed0dSJosef Bacik 29146ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 29156ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 29166ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 29176ef5ed0dSJosef Bacik btrfs_free_path(path); 29186ef5ed0dSJosef Bacik 29192b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 29206ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 29213c04ce01SMiao Xie out: 29223c04ce01SMiao Xie mnt_drop_write_file(file); 29233c04ce01SMiao Xie return ret; 29246ef5ed0dSJosef Bacik } 29256ef5ed0dSJosef Bacik 29265af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 2927bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 2928bf5fc093SJosef Bacik { 2929bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 2930bf5fc093SJosef Bacik 2931bf5fc093SJosef Bacik space->total_bytes = 0; 2932bf5fc093SJosef Bacik space->used_bytes = 0; 2933bf5fc093SJosef Bacik space->flags = 0; 2934bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 2935bf5fc093SJosef Bacik space->flags = block_group->flags; 2936bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 2937bf5fc093SJosef Bacik space->used_bytes += 2938bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 2939bf5fc093SJosef Bacik } 2940bf5fc093SJosef Bacik } 2941bf5fc093SJosef Bacik 29421406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 29431406e432SJosef Bacik { 29441406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 29451406e432SJosef Bacik struct btrfs_ioctl_space_info space; 29461406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 29477fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 294813f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 29491406e432SJosef Bacik struct btrfs_space_info *info; 2950bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 2951bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 2952bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 2953bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 2954bf5fc093SJosef Bacik int num_types = 4; 29557fde62bfSChris Mason int alloc_size; 29561406e432SJosef Bacik int ret = 0; 295751788b1bSDan Rosenberg u64 slot_count = 0; 2958bf5fc093SJosef Bacik int i, c; 29591406e432SJosef Bacik 29601406e432SJosef Bacik if (copy_from_user(&space_args, 29611406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 29621406e432SJosef Bacik sizeof(space_args))) 29631406e432SJosef Bacik return -EFAULT; 29641406e432SJosef Bacik 2965bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2966bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2967bf5fc093SJosef Bacik 2968bf5fc093SJosef Bacik info = NULL; 29697fde62bfSChris Mason rcu_read_lock(); 2970bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2971bf5fc093SJosef Bacik list) { 2972bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2973bf5fc093SJosef Bacik info = tmp; 2974bf5fc093SJosef Bacik break; 2975bf5fc093SJosef Bacik } 2976bf5fc093SJosef Bacik } 29777fde62bfSChris Mason rcu_read_unlock(); 29781406e432SJosef Bacik 2979bf5fc093SJosef Bacik if (!info) 2980bf5fc093SJosef Bacik continue; 2981bf5fc093SJosef Bacik 2982bf5fc093SJosef Bacik down_read(&info->groups_sem); 2983bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2984bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 2985bf5fc093SJosef Bacik slot_count++; 2986bf5fc093SJosef Bacik } 2987bf5fc093SJosef Bacik up_read(&info->groups_sem); 2988bf5fc093SJosef Bacik } 2989bf5fc093SJosef Bacik 29907fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 29917fde62bfSChris Mason if (space_args.space_slots == 0) { 29927fde62bfSChris Mason space_args.total_spaces = slot_count; 29937fde62bfSChris Mason goto out; 29947fde62bfSChris Mason } 2995bf5fc093SJosef Bacik 299651788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 2997bf5fc093SJosef Bacik 29987fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 2999bf5fc093SJosef Bacik 30007fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 30017fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 30027fde62bfSChris Mason */ 30037fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 30047fde62bfSChris Mason return -ENOMEM; 30057fde62bfSChris Mason 30067fde62bfSChris Mason space_args.total_spaces = 0; 30077fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 30087fde62bfSChris Mason if (!dest) 30097fde62bfSChris Mason return -ENOMEM; 30107fde62bfSChris Mason dest_orig = dest; 30117fde62bfSChris Mason 30127fde62bfSChris Mason /* now we have a buffer to copy into */ 3013bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3014bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3015bf5fc093SJosef Bacik 301651788b1bSDan Rosenberg if (!slot_count) 301751788b1bSDan Rosenberg break; 301851788b1bSDan Rosenberg 3019bf5fc093SJosef Bacik info = NULL; 30201406e432SJosef Bacik rcu_read_lock(); 3021bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3022bf5fc093SJosef Bacik list) { 3023bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3024bf5fc093SJosef Bacik info = tmp; 30257fde62bfSChris Mason break; 3026bf5fc093SJosef Bacik } 3027bf5fc093SJosef Bacik } 3028bf5fc093SJosef Bacik rcu_read_unlock(); 30297fde62bfSChris Mason 3030bf5fc093SJosef Bacik if (!info) 3031bf5fc093SJosef Bacik continue; 3032bf5fc093SJosef Bacik down_read(&info->groups_sem); 3033bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3034bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 30355af3e8ccSStefan Behrens btrfs_get_block_group_info( 30365af3e8ccSStefan Behrens &info->block_groups[c], &space); 30377fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 30381406e432SJosef Bacik dest++; 30391406e432SJosef Bacik space_args.total_spaces++; 304051788b1bSDan Rosenberg slot_count--; 30411406e432SJosef Bacik } 304251788b1bSDan Rosenberg if (!slot_count) 304351788b1bSDan Rosenberg break; 3044bf5fc093SJosef Bacik } 3045bf5fc093SJosef Bacik up_read(&info->groups_sem); 3046bf5fc093SJosef Bacik } 30471406e432SJosef Bacik 30482eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 30497fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 30507fde62bfSChris Mason 30517fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 30527fde62bfSChris Mason ret = -EFAULT; 30537fde62bfSChris Mason 30547fde62bfSChris Mason kfree(dest_orig); 30557fde62bfSChris Mason out: 30567fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 30571406e432SJosef Bacik ret = -EFAULT; 30581406e432SJosef Bacik 30591406e432SJosef Bacik return ret; 30601406e432SJosef Bacik } 30611406e432SJosef Bacik 3062f46b5a66SChristoph Hellwig /* 3063f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3064f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3065f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3066f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3067f46b5a66SChristoph Hellwig */ 3068f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3069f46b5a66SChristoph Hellwig { 3070f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 3071f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3072f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3073f46b5a66SChristoph Hellwig 3074f46b5a66SChristoph Hellwig trans = file->private_data; 30751ab86aedSSage Weil if (!trans) 30761ab86aedSSage Weil return -EINVAL; 3077b214107eSChristoph Hellwig file->private_data = NULL; 30789ca9ee09SSage Weil 30791ab86aedSSage Weil btrfs_end_transaction(trans, root); 30801ab86aedSSage Weil 3081a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 30829ca9ee09SSage Weil 30832a79f17eSAl Viro mnt_drop_write_file(file); 30841ab86aedSSage Weil return 0; 3085f46b5a66SChristoph Hellwig } 3086f46b5a66SChristoph Hellwig 30879a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 30889a8c28beSMiao Xie void __user *argp) 308946204592SSage Weil { 309046204592SSage Weil struct btrfs_trans_handle *trans; 309146204592SSage Weil u64 transid; 3092db5b493aSTsutomu Itoh int ret; 309346204592SSage Weil 3094ff7c1d33SMiao Xie trans = btrfs_attach_transaction(root); 3095ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3096ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 309798d5dc13STsutomu Itoh return PTR_ERR(trans); 3098ff7c1d33SMiao Xie 3099ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3100ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3101ff7c1d33SMiao Xie goto out; 3102ff7c1d33SMiao Xie } 310346204592SSage Weil transid = trans->transid; 3104db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 31058b2b2d3cSTsutomu Itoh if (ret) { 31068b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 3107db5b493aSTsutomu Itoh return ret; 31088b2b2d3cSTsutomu Itoh } 3109ff7c1d33SMiao Xie out: 311046204592SSage Weil if (argp) 311146204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 311246204592SSage Weil return -EFAULT; 311346204592SSage Weil return 0; 311446204592SSage Weil } 311546204592SSage Weil 31169a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 31179a8c28beSMiao Xie void __user *argp) 311846204592SSage Weil { 311946204592SSage Weil u64 transid; 312046204592SSage Weil 312146204592SSage Weil if (argp) { 312246204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 312346204592SSage Weil return -EFAULT; 312446204592SSage Weil } else { 312546204592SSage Weil transid = 0; /* current trans */ 312646204592SSage Weil } 312746204592SSage Weil return btrfs_wait_for_commit(root, transid); 312846204592SSage Weil } 312946204592SSage Weil 3130b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 3131475f6387SJan Schmidt { 3132b8e95489SMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3133475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3134b8e95489SMiao Xie int ret; 3135475f6387SJan Schmidt 3136475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3137475f6387SJan Schmidt return -EPERM; 3138475f6387SJan Schmidt 3139475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3140475f6387SJan Schmidt if (IS_ERR(sa)) 3141475f6387SJan Schmidt return PTR_ERR(sa); 3142475f6387SJan Schmidt 3143b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 3144b8e95489SMiao Xie ret = mnt_want_write_file(file); 3145b8e95489SMiao Xie if (ret) 3146b8e95489SMiao Xie goto out; 3147b8e95489SMiao Xie } 3148b8e95489SMiao Xie 3149aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 315063a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 315163a212abSStefan Behrens 0); 3152475f6387SJan Schmidt 3153475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3154475f6387SJan Schmidt ret = -EFAULT; 3155475f6387SJan Schmidt 3156b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 3157b8e95489SMiao Xie mnt_drop_write_file(file); 3158b8e95489SMiao Xie out: 3159475f6387SJan Schmidt kfree(sa); 3160475f6387SJan Schmidt return ret; 3161475f6387SJan Schmidt } 3162475f6387SJan Schmidt 3163475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 3164475f6387SJan Schmidt { 3165475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3166475f6387SJan Schmidt return -EPERM; 3167475f6387SJan Schmidt 3168aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 3169475f6387SJan Schmidt } 3170475f6387SJan Schmidt 3171475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 3172475f6387SJan Schmidt void __user *arg) 3173475f6387SJan Schmidt { 3174475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3175475f6387SJan Schmidt int ret; 3176475f6387SJan Schmidt 3177475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3178475f6387SJan Schmidt return -EPERM; 3179475f6387SJan Schmidt 3180475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3181475f6387SJan Schmidt if (IS_ERR(sa)) 3182475f6387SJan Schmidt return PTR_ERR(sa); 3183475f6387SJan Schmidt 3184475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 3185475f6387SJan Schmidt 3186475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3187475f6387SJan Schmidt ret = -EFAULT; 3188475f6387SJan Schmidt 3189475f6387SJan Schmidt kfree(sa); 3190475f6387SJan Schmidt return ret; 3191475f6387SJan Schmidt } 3192475f6387SJan Schmidt 3193c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 3194b27f7c0cSDavid Sterba void __user *arg) 3195c11d2c23SStefan Behrens { 3196c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 3197c11d2c23SStefan Behrens int ret; 3198c11d2c23SStefan Behrens 3199c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 3200c11d2c23SStefan Behrens if (IS_ERR(sa)) 3201c11d2c23SStefan Behrens return PTR_ERR(sa); 3202c11d2c23SStefan Behrens 3203b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 3204b27f7c0cSDavid Sterba kfree(sa); 3205b27f7c0cSDavid Sterba return -EPERM; 3206b27f7c0cSDavid Sterba } 3207b27f7c0cSDavid Sterba 3208b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 3209c11d2c23SStefan Behrens 3210c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 3211c11d2c23SStefan Behrens ret = -EFAULT; 3212c11d2c23SStefan Behrens 3213c11d2c23SStefan Behrens kfree(sa); 3214c11d2c23SStefan Behrens return ret; 3215c11d2c23SStefan Behrens } 3216c11d2c23SStefan Behrens 32173f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 32183f6bcfbdSStefan Behrens { 32193f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 32203f6bcfbdSStefan Behrens int ret; 32213f6bcfbdSStefan Behrens 32223f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 32233f6bcfbdSStefan Behrens return -EPERM; 32243f6bcfbdSStefan Behrens 32253f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 32263f6bcfbdSStefan Behrens if (IS_ERR(p)) 32273f6bcfbdSStefan Behrens return PTR_ERR(p); 32283f6bcfbdSStefan Behrens 32293f6bcfbdSStefan Behrens switch (p->cmd) { 32303f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 32313f6bcfbdSStefan Behrens if (atomic_xchg( 32323f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 32333f6bcfbdSStefan Behrens 1)) { 32343f6bcfbdSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 32353f6bcfbdSStefan Behrens ret = -EINPROGRESS; 32363f6bcfbdSStefan Behrens } else { 32373f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 32383f6bcfbdSStefan Behrens atomic_set( 32393f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 32403f6bcfbdSStefan Behrens 0); 32413f6bcfbdSStefan Behrens } 32423f6bcfbdSStefan Behrens break; 32433f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 32443f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 32453f6bcfbdSStefan Behrens ret = 0; 32463f6bcfbdSStefan Behrens break; 32473f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 32483f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 32493f6bcfbdSStefan Behrens break; 32503f6bcfbdSStefan Behrens default: 32513f6bcfbdSStefan Behrens ret = -EINVAL; 32523f6bcfbdSStefan Behrens break; 32533f6bcfbdSStefan Behrens } 32543f6bcfbdSStefan Behrens 32553f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 32563f6bcfbdSStefan Behrens ret = -EFAULT; 32573f6bcfbdSStefan Behrens 32583f6bcfbdSStefan Behrens kfree(p); 32593f6bcfbdSStefan Behrens return ret; 32603f6bcfbdSStefan Behrens } 32613f6bcfbdSStefan Behrens 3262d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 3263d7728c96SJan Schmidt { 3264d7728c96SJan Schmidt int ret = 0; 3265d7728c96SJan Schmidt int i; 3266740c3d22SChris Mason u64 rel_ptr; 3267d7728c96SJan Schmidt int size; 3268806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 3269d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 3270d7728c96SJan Schmidt struct btrfs_path *path; 3271d7728c96SJan Schmidt 3272d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3273d7728c96SJan Schmidt return -EPERM; 3274d7728c96SJan Schmidt 3275d7728c96SJan Schmidt path = btrfs_alloc_path(); 3276d7728c96SJan Schmidt if (!path) { 3277d7728c96SJan Schmidt ret = -ENOMEM; 3278d7728c96SJan Schmidt goto out; 3279d7728c96SJan Schmidt } 3280d7728c96SJan Schmidt 3281d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 3282d7728c96SJan Schmidt if (IS_ERR(ipa)) { 3283d7728c96SJan Schmidt ret = PTR_ERR(ipa); 3284d7728c96SJan Schmidt ipa = NULL; 3285d7728c96SJan Schmidt goto out; 3286d7728c96SJan Schmidt } 3287d7728c96SJan Schmidt 3288d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 3289d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 3290d7728c96SJan Schmidt if (IS_ERR(ipath)) { 3291d7728c96SJan Schmidt ret = PTR_ERR(ipath); 3292d7728c96SJan Schmidt ipath = NULL; 3293d7728c96SJan Schmidt goto out; 3294d7728c96SJan Schmidt } 3295d7728c96SJan Schmidt 3296d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 3297d7728c96SJan Schmidt if (ret < 0) 3298d7728c96SJan Schmidt goto out; 3299d7728c96SJan Schmidt 3300d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 3301745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 3302745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 3303740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 3304d7728c96SJan Schmidt } 3305d7728c96SJan Schmidt 3306745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 3307745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 3308d7728c96SJan Schmidt if (ret) { 3309d7728c96SJan Schmidt ret = -EFAULT; 3310d7728c96SJan Schmidt goto out; 3311d7728c96SJan Schmidt } 3312d7728c96SJan Schmidt 3313d7728c96SJan Schmidt out: 3314d7728c96SJan Schmidt btrfs_free_path(path); 3315d7728c96SJan Schmidt free_ipath(ipath); 3316d7728c96SJan Schmidt kfree(ipa); 3317d7728c96SJan Schmidt 3318d7728c96SJan Schmidt return ret; 3319d7728c96SJan Schmidt } 3320d7728c96SJan Schmidt 3321d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 3322d7728c96SJan Schmidt { 3323d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 3324d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 3325d7728c96SJan Schmidt 3326d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 3327d7728c96SJan Schmidt inodes->bytes_left -= c; 3328d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 3329d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 3330d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 3331d7728c96SJan Schmidt inodes->elem_cnt += 3; 3332d7728c96SJan Schmidt } else { 3333d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 3334d7728c96SJan Schmidt inodes->bytes_left = 0; 3335d7728c96SJan Schmidt inodes->elem_missed += 3; 3336d7728c96SJan Schmidt } 3337d7728c96SJan Schmidt 3338d7728c96SJan Schmidt return 0; 3339d7728c96SJan Schmidt } 3340d7728c96SJan Schmidt 3341d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 3342d7728c96SJan Schmidt void __user *arg) 3343d7728c96SJan Schmidt { 3344d7728c96SJan Schmidt int ret = 0; 3345d7728c96SJan Schmidt int size; 3346d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3347d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3348d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3349d7728c96SJan Schmidt 3350d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3351d7728c96SJan Schmidt return -EPERM; 3352d7728c96SJan Schmidt 3353d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 3354d7728c96SJan Schmidt if (IS_ERR(loi)) { 3355d7728c96SJan Schmidt ret = PTR_ERR(loi); 3356d7728c96SJan Schmidt loi = NULL; 3357d7728c96SJan Schmidt goto out; 3358d7728c96SJan Schmidt } 3359d7728c96SJan Schmidt 3360d7728c96SJan Schmidt path = btrfs_alloc_path(); 3361d7728c96SJan Schmidt if (!path) { 3362d7728c96SJan Schmidt ret = -ENOMEM; 3363d7728c96SJan Schmidt goto out; 3364d7728c96SJan Schmidt } 3365d7728c96SJan Schmidt 3366425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 3367d7728c96SJan Schmidt inodes = init_data_container(size); 3368d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3369d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3370d7728c96SJan Schmidt inodes = NULL; 3371d7728c96SJan Schmidt goto out; 3372d7728c96SJan Schmidt } 3373d7728c96SJan Schmidt 3374df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 3375df031f07SLiu Bo build_ino_list, inodes); 3376df031f07SLiu Bo if (ret == -EINVAL) 3377d7728c96SJan Schmidt ret = -ENOENT; 3378d7728c96SJan Schmidt if (ret < 0) 3379d7728c96SJan Schmidt goto out; 3380d7728c96SJan Schmidt 3381745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 3382745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 3383d7728c96SJan Schmidt if (ret) 3384d7728c96SJan Schmidt ret = -EFAULT; 3385d7728c96SJan Schmidt 3386d7728c96SJan Schmidt out: 3387d7728c96SJan Schmidt btrfs_free_path(path); 3388425d17a2SLiu Bo vfree(inodes); 3389d7728c96SJan Schmidt kfree(loi); 3390d7728c96SJan Schmidt 3391d7728c96SJan Schmidt return ret; 3392d7728c96SJan Schmidt } 3393d7728c96SJan Schmidt 339419a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 3395c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3396c9e9f97bSIlya Dryomov { 3397c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3398c9e9f97bSIlya Dryomov 3399c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3400c9e9f97bSIlya Dryomov 3401837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 3402837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 3403837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 3404837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 3405a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 3406a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 3407837d5b6eSIlya Dryomov 3408c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 3409c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 3410c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 341119a39dceSIlya Dryomov 341219a39dceSIlya Dryomov if (lock) { 341319a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 341419a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 341519a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 341619a39dceSIlya Dryomov } else { 341719a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 341819a39dceSIlya Dryomov } 3419c9e9f97bSIlya Dryomov } 3420c9e9f97bSIlya Dryomov 34219ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 3422c9e9f97bSIlya Dryomov { 34239ba1f6e4SLiu Bo struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3424c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 3425c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 3426c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 3427c9e9f97bSIlya Dryomov int ret; 34285ac00addSStefan Behrens int need_to_clear_lock = 0; 3429c9e9f97bSIlya Dryomov 3430c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3431c9e9f97bSIlya Dryomov return -EPERM; 3432c9e9f97bSIlya Dryomov 3433e54bfa31SLiu Bo ret = mnt_want_write_file(file); 34349ba1f6e4SLiu Bo if (ret) 34359ba1f6e4SLiu Bo return ret; 34369ba1f6e4SLiu Bo 3437c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 3438c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3439c9e9f97bSIlya Dryomov 3440c9e9f97bSIlya Dryomov if (arg) { 3441c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 3442c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 3443c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 3444c9e9f97bSIlya Dryomov goto out; 3445c9e9f97bSIlya Dryomov } 3446de322263SIlya Dryomov 3447de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 3448de322263SIlya Dryomov if (!fs_info->balance_ctl) { 3449de322263SIlya Dryomov ret = -ENOTCONN; 3450de322263SIlya Dryomov goto out_bargs; 3451de322263SIlya Dryomov } 3452de322263SIlya Dryomov 3453de322263SIlya Dryomov bctl = fs_info->balance_ctl; 3454de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 3455de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 3456de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 3457de322263SIlya Dryomov 3458de322263SIlya Dryomov goto do_balance; 3459de322263SIlya Dryomov } 3460c9e9f97bSIlya Dryomov } else { 3461c9e9f97bSIlya Dryomov bargs = NULL; 3462c9e9f97bSIlya Dryomov } 3463c9e9f97bSIlya Dryomov 34645ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 34655ac00addSStefan Behrens 1)) { 34665ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 3467837d5b6eSIlya Dryomov ret = -EINPROGRESS; 3468837d5b6eSIlya Dryomov goto out_bargs; 3469837d5b6eSIlya Dryomov } 34705ac00addSStefan Behrens need_to_clear_lock = 1; 3471837d5b6eSIlya Dryomov 3472c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3473c9e9f97bSIlya Dryomov if (!bctl) { 3474c9e9f97bSIlya Dryomov ret = -ENOMEM; 3475c9e9f97bSIlya Dryomov goto out_bargs; 3476c9e9f97bSIlya Dryomov } 3477c9e9f97bSIlya Dryomov 3478c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 3479c9e9f97bSIlya Dryomov if (arg) { 3480c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 3481c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 3482c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 3483c9e9f97bSIlya Dryomov 3484c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 3485f43ffb60SIlya Dryomov } else { 3486f43ffb60SIlya Dryomov /* balance everything - no filters */ 3487f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 3488c9e9f97bSIlya Dryomov } 3489c9e9f97bSIlya Dryomov 3490de322263SIlya Dryomov do_balance: 3491c9e9f97bSIlya Dryomov ret = btrfs_balance(bctl, bargs); 3492c9e9f97bSIlya Dryomov /* 3493837d5b6eSIlya Dryomov * bctl is freed in __cancel_balance or in free_fs_info if 3494837d5b6eSIlya Dryomov * restriper was paused all the way until unmount 3495c9e9f97bSIlya Dryomov */ 3496c9e9f97bSIlya Dryomov if (arg) { 3497c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 3498c9e9f97bSIlya Dryomov ret = -EFAULT; 3499c9e9f97bSIlya Dryomov } 3500c9e9f97bSIlya Dryomov 3501c9e9f97bSIlya Dryomov out_bargs: 3502c9e9f97bSIlya Dryomov kfree(bargs); 3503c9e9f97bSIlya Dryomov out: 35045ac00addSStefan Behrens if (need_to_clear_lock) 35055ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 35065ac00addSStefan Behrens 0); 3507c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3508c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3509e54bfa31SLiu Bo mnt_drop_write_file(file); 3510c9e9f97bSIlya Dryomov return ret; 3511c9e9f97bSIlya Dryomov } 3512c9e9f97bSIlya Dryomov 3513837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 3514837d5b6eSIlya Dryomov { 3515837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3516837d5b6eSIlya Dryomov return -EPERM; 3517837d5b6eSIlya Dryomov 3518837d5b6eSIlya Dryomov switch (cmd) { 3519837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 3520837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 3521a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 3522a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 3523837d5b6eSIlya Dryomov } 3524837d5b6eSIlya Dryomov 3525837d5b6eSIlya Dryomov return -EINVAL; 3526837d5b6eSIlya Dryomov } 3527837d5b6eSIlya Dryomov 352819a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 352919a39dceSIlya Dryomov void __user *arg) 353019a39dceSIlya Dryomov { 353119a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 353219a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 353319a39dceSIlya Dryomov int ret = 0; 353419a39dceSIlya Dryomov 353519a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 353619a39dceSIlya Dryomov return -EPERM; 353719a39dceSIlya Dryomov 353819a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 353919a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 354019a39dceSIlya Dryomov ret = -ENOTCONN; 354119a39dceSIlya Dryomov goto out; 354219a39dceSIlya Dryomov } 354319a39dceSIlya Dryomov 354419a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 354519a39dceSIlya Dryomov if (!bargs) { 354619a39dceSIlya Dryomov ret = -ENOMEM; 354719a39dceSIlya Dryomov goto out; 354819a39dceSIlya Dryomov } 354919a39dceSIlya Dryomov 355019a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 355119a39dceSIlya Dryomov 355219a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 355319a39dceSIlya Dryomov ret = -EFAULT; 355419a39dceSIlya Dryomov 355519a39dceSIlya Dryomov kfree(bargs); 355619a39dceSIlya Dryomov out: 355719a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 355819a39dceSIlya Dryomov return ret; 355919a39dceSIlya Dryomov } 356019a39dceSIlya Dryomov 3561*905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 35625d13a37bSArne Jansen { 3563*905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 35645d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 35655d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 35665d13a37bSArne Jansen int ret; 35675d13a37bSArne Jansen int err; 35685d13a37bSArne Jansen 35695d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 35705d13a37bSArne Jansen return -EPERM; 35715d13a37bSArne Jansen 3572*905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3573*905b0ddaSMiao Xie if (ret) 3574*905b0ddaSMiao Xie return ret; 35755d13a37bSArne Jansen 35765d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3577*905b0ddaSMiao Xie if (IS_ERR(sa)) { 3578*905b0ddaSMiao Xie ret = PTR_ERR(sa); 3579*905b0ddaSMiao Xie goto drop_write; 3580*905b0ddaSMiao Xie } 35815d13a37bSArne Jansen 35825d13a37bSArne Jansen if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) { 35835d13a37bSArne Jansen trans = btrfs_start_transaction(root, 2); 35845d13a37bSArne Jansen if (IS_ERR(trans)) { 35855d13a37bSArne Jansen ret = PTR_ERR(trans); 35865d13a37bSArne Jansen goto out; 35875d13a37bSArne Jansen } 35885d13a37bSArne Jansen } 35895d13a37bSArne Jansen 35905d13a37bSArne Jansen switch (sa->cmd) { 35915d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 35925d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 35935d13a37bSArne Jansen break; 35945d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 35955d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 35965d13a37bSArne Jansen break; 35975d13a37bSArne Jansen case BTRFS_QUOTA_CTL_RESCAN: 35985d13a37bSArne Jansen ret = btrfs_quota_rescan(root->fs_info); 35995d13a37bSArne Jansen break; 36005d13a37bSArne Jansen default: 36015d13a37bSArne Jansen ret = -EINVAL; 36025d13a37bSArne Jansen break; 36035d13a37bSArne Jansen } 36045d13a37bSArne Jansen 36055d13a37bSArne Jansen if (copy_to_user(arg, sa, sizeof(*sa))) 36065d13a37bSArne Jansen ret = -EFAULT; 36075d13a37bSArne Jansen 36085d13a37bSArne Jansen if (trans) { 36095d13a37bSArne Jansen err = btrfs_commit_transaction(trans, root); 36105d13a37bSArne Jansen if (err && !ret) 36115d13a37bSArne Jansen ret = err; 36125d13a37bSArne Jansen } 36135d13a37bSArne Jansen out: 36145d13a37bSArne Jansen kfree(sa); 3615*905b0ddaSMiao Xie drop_write: 3616*905b0ddaSMiao Xie mnt_drop_write_file(file); 36175d13a37bSArne Jansen return ret; 36185d13a37bSArne Jansen } 36195d13a37bSArne Jansen 3620*905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 36215d13a37bSArne Jansen { 3622*905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 36235d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 36245d13a37bSArne Jansen struct btrfs_trans_handle *trans; 36255d13a37bSArne Jansen int ret; 36265d13a37bSArne Jansen int err; 36275d13a37bSArne Jansen 36285d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 36295d13a37bSArne Jansen return -EPERM; 36305d13a37bSArne Jansen 3631*905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3632*905b0ddaSMiao Xie if (ret) 3633*905b0ddaSMiao Xie return ret; 36345d13a37bSArne Jansen 36355d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3636*905b0ddaSMiao Xie if (IS_ERR(sa)) { 3637*905b0ddaSMiao Xie ret = PTR_ERR(sa); 3638*905b0ddaSMiao Xie goto drop_write; 3639*905b0ddaSMiao Xie } 36405d13a37bSArne Jansen 36415d13a37bSArne Jansen trans = btrfs_join_transaction(root); 36425d13a37bSArne Jansen if (IS_ERR(trans)) { 36435d13a37bSArne Jansen ret = PTR_ERR(trans); 36445d13a37bSArne Jansen goto out; 36455d13a37bSArne Jansen } 36465d13a37bSArne Jansen 36475d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 36485d13a37bSArne Jansen if (sa->assign) { 36495d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 36505d13a37bSArne Jansen sa->src, sa->dst); 36515d13a37bSArne Jansen } else { 36525d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 36535d13a37bSArne Jansen sa->src, sa->dst); 36545d13a37bSArne Jansen } 36555d13a37bSArne Jansen 36565d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 36575d13a37bSArne Jansen if (err && !ret) 36585d13a37bSArne Jansen ret = err; 36595d13a37bSArne Jansen 36605d13a37bSArne Jansen out: 36615d13a37bSArne Jansen kfree(sa); 3662*905b0ddaSMiao Xie drop_write: 3663*905b0ddaSMiao Xie mnt_drop_write_file(file); 36645d13a37bSArne Jansen return ret; 36655d13a37bSArne Jansen } 36665d13a37bSArne Jansen 3667*905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 36685d13a37bSArne Jansen { 3669*905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 36705d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 36715d13a37bSArne Jansen struct btrfs_trans_handle *trans; 36725d13a37bSArne Jansen int ret; 36735d13a37bSArne Jansen int err; 36745d13a37bSArne Jansen 36755d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 36765d13a37bSArne Jansen return -EPERM; 36775d13a37bSArne Jansen 3678*905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3679*905b0ddaSMiao Xie if (ret) 3680*905b0ddaSMiao Xie return ret; 36815d13a37bSArne Jansen 36825d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3683*905b0ddaSMiao Xie if (IS_ERR(sa)) { 3684*905b0ddaSMiao Xie ret = PTR_ERR(sa); 3685*905b0ddaSMiao Xie goto drop_write; 3686*905b0ddaSMiao Xie } 36875d13a37bSArne Jansen 36885d13a37bSArne Jansen trans = btrfs_join_transaction(root); 36895d13a37bSArne Jansen if (IS_ERR(trans)) { 36905d13a37bSArne Jansen ret = PTR_ERR(trans); 36915d13a37bSArne Jansen goto out; 36925d13a37bSArne Jansen } 36935d13a37bSArne Jansen 36945d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 36955d13a37bSArne Jansen if (sa->create) { 36965d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 36975d13a37bSArne Jansen NULL); 36985d13a37bSArne Jansen } else { 36995d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 37005d13a37bSArne Jansen } 37015d13a37bSArne Jansen 37025d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 37035d13a37bSArne Jansen if (err && !ret) 37045d13a37bSArne Jansen ret = err; 37055d13a37bSArne Jansen 37065d13a37bSArne Jansen out: 37075d13a37bSArne Jansen kfree(sa); 3708*905b0ddaSMiao Xie drop_write: 3709*905b0ddaSMiao Xie mnt_drop_write_file(file); 37105d13a37bSArne Jansen return ret; 37115d13a37bSArne Jansen } 37125d13a37bSArne Jansen 3713*905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 37145d13a37bSArne Jansen { 3715*905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 37165d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 37175d13a37bSArne Jansen struct btrfs_trans_handle *trans; 37185d13a37bSArne Jansen int ret; 37195d13a37bSArne Jansen int err; 37205d13a37bSArne Jansen u64 qgroupid; 37215d13a37bSArne Jansen 37225d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 37235d13a37bSArne Jansen return -EPERM; 37245d13a37bSArne Jansen 3725*905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3726*905b0ddaSMiao Xie if (ret) 3727*905b0ddaSMiao Xie return ret; 37285d13a37bSArne Jansen 37295d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3730*905b0ddaSMiao Xie if (IS_ERR(sa)) { 3731*905b0ddaSMiao Xie ret = PTR_ERR(sa); 3732*905b0ddaSMiao Xie goto drop_write; 3733*905b0ddaSMiao Xie } 37345d13a37bSArne Jansen 37355d13a37bSArne Jansen trans = btrfs_join_transaction(root); 37365d13a37bSArne Jansen if (IS_ERR(trans)) { 37375d13a37bSArne Jansen ret = PTR_ERR(trans); 37385d13a37bSArne Jansen goto out; 37395d13a37bSArne Jansen } 37405d13a37bSArne Jansen 37415d13a37bSArne Jansen qgroupid = sa->qgroupid; 37425d13a37bSArne Jansen if (!qgroupid) { 37435d13a37bSArne Jansen /* take the current subvol as qgroup */ 37445d13a37bSArne Jansen qgroupid = root->root_key.objectid; 37455d13a37bSArne Jansen } 37465d13a37bSArne Jansen 37475d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 37485d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 37495d13a37bSArne Jansen 37505d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 37515d13a37bSArne Jansen if (err && !ret) 37525d13a37bSArne Jansen ret = err; 37535d13a37bSArne Jansen 37545d13a37bSArne Jansen out: 37555d13a37bSArne Jansen kfree(sa); 3756*905b0ddaSMiao Xie drop_write: 3757*905b0ddaSMiao Xie mnt_drop_write_file(file); 37585d13a37bSArne Jansen return ret; 37595d13a37bSArne Jansen } 37605d13a37bSArne Jansen 37618ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file, 37628ea05e3aSAlexander Block void __user *arg) 37638ea05e3aSAlexander Block { 37648ea05e3aSAlexander Block struct btrfs_ioctl_received_subvol_args *sa = NULL; 37658ea05e3aSAlexander Block struct inode *inode = fdentry(file)->d_inode; 37668ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 37678ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 37688ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 37698ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 37708ea05e3aSAlexander Block int ret = 0; 37718ea05e3aSAlexander Block 37728ea05e3aSAlexander Block ret = mnt_want_write_file(file); 37738ea05e3aSAlexander Block if (ret < 0) 37748ea05e3aSAlexander Block return ret; 37758ea05e3aSAlexander Block 37768ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 37778ea05e3aSAlexander Block 37788ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 37798ea05e3aSAlexander Block ret = -EINVAL; 37808ea05e3aSAlexander Block goto out; 37818ea05e3aSAlexander Block } 37828ea05e3aSAlexander Block 37838ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 37848ea05e3aSAlexander Block ret = -EROFS; 37858ea05e3aSAlexander Block goto out; 37868ea05e3aSAlexander Block } 37878ea05e3aSAlexander Block 37888ea05e3aSAlexander Block if (!inode_owner_or_capable(inode)) { 37898ea05e3aSAlexander Block ret = -EACCES; 37908ea05e3aSAlexander Block goto out; 37918ea05e3aSAlexander Block } 37928ea05e3aSAlexander Block 37938ea05e3aSAlexander Block sa = memdup_user(arg, sizeof(*sa)); 37948ea05e3aSAlexander Block if (IS_ERR(sa)) { 37958ea05e3aSAlexander Block ret = PTR_ERR(sa); 37968ea05e3aSAlexander Block sa = NULL; 37978ea05e3aSAlexander Block goto out; 37988ea05e3aSAlexander Block } 37998ea05e3aSAlexander Block 38008ea05e3aSAlexander Block trans = btrfs_start_transaction(root, 1); 38018ea05e3aSAlexander Block if (IS_ERR(trans)) { 38028ea05e3aSAlexander Block ret = PTR_ERR(trans); 38038ea05e3aSAlexander Block trans = NULL; 38048ea05e3aSAlexander Block goto out; 38058ea05e3aSAlexander Block } 38068ea05e3aSAlexander Block 38078ea05e3aSAlexander Block sa->rtransid = trans->transid; 38088ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 38098ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 38108ea05e3aSAlexander Block 38118ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 38128ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 38138ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 38148ea05e3aSAlexander Block root_item->stime.sec = cpu_to_le64(sa->stime.sec); 38158ea05e3aSAlexander Block root_item->stime.nsec = cpu_to_le32(sa->stime.nsec); 38168ea05e3aSAlexander Block root_item->rtime.sec = cpu_to_le64(sa->rtime.sec); 38178ea05e3aSAlexander Block root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec); 38188ea05e3aSAlexander Block 38198ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 38208ea05e3aSAlexander Block &root->root_key, &root->root_item); 38218ea05e3aSAlexander Block if (ret < 0) { 38228ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 38238ea05e3aSAlexander Block trans = NULL; 38248ea05e3aSAlexander Block goto out; 38258ea05e3aSAlexander Block } else { 38268ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 38278ea05e3aSAlexander Block if (ret < 0) 38288ea05e3aSAlexander Block goto out; 38298ea05e3aSAlexander Block } 38308ea05e3aSAlexander Block 38318ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 38328ea05e3aSAlexander Block if (ret) 38338ea05e3aSAlexander Block ret = -EFAULT; 38348ea05e3aSAlexander Block 38358ea05e3aSAlexander Block out: 38368ea05e3aSAlexander Block kfree(sa); 38378ea05e3aSAlexander Block up_write(&root->fs_info->subvol_sem); 38388ea05e3aSAlexander Block mnt_drop_write_file(file); 38398ea05e3aSAlexander Block return ret; 38408ea05e3aSAlexander Block } 38418ea05e3aSAlexander Block 3842f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 3843f46b5a66SChristoph Hellwig cmd, unsigned long arg) 3844f46b5a66SChristoph Hellwig { 3845f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 38464bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 3847f46b5a66SChristoph Hellwig 3848f46b5a66SChristoph Hellwig switch (cmd) { 38496cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 38506cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 38516cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 38526cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 38536cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 38546cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 3855f7039b1dSLi Dongyang case FITRIM: 3856f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 3857f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 3858fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 3859fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 3860fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 38613de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 3862fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 38636f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 38646f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 386576dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 386676dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 38670caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 38680caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 38690caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 38700caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 38716ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 38726ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 3873f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 38741e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 38751e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 38761e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 3877f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 3878198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 3879f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 38804bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 3881f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 3882da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 3883475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 3884475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 3885475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 3886475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 3887f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 38889ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 3889f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 3890c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 3891c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 38927a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 3893f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 3894f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 3895f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 3896f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 3897ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 3898ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 3899ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 3900ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 3901d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 3902d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 3903d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 3904d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 39051406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 39061406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 3907f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 3908f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 3909f46b5a66SChristoph Hellwig return 0; 391046204592SSage Weil case BTRFS_IOC_START_SYNC: 39119a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 391246204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 39139a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 3914475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 3915b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 3916475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 3917475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 3918475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 3919475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 3920c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 39219ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 3922837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 3923837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 392419a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 392519a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 39268ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 39278ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 392831db9f7cSAlexander Block case BTRFS_IOC_SEND: 392931db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 3930c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 3931b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 39325d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 3933*905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 39345d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 3935*905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 39365d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 3937*905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 39385d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 3939*905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 39403f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 39413f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 3942f46b5a66SChristoph Hellwig } 3943f46b5a66SChristoph Hellwig 3944f46b5a66SChristoph Hellwig return -ENOTTY; 3945f46b5a66SChristoph Hellwig } 3946