1f46b5a66SChristoph Hellwig /* 2f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 3f46b5a66SChristoph Hellwig * 4f46b5a66SChristoph Hellwig * This program is free software; you can redistribute it and/or 5f46b5a66SChristoph Hellwig * modify it under the terms of the GNU General Public 6f46b5a66SChristoph Hellwig * License v2 as published by the Free Software Foundation. 7f46b5a66SChristoph Hellwig * 8f46b5a66SChristoph Hellwig * This program is distributed in the hope that it will be useful, 9f46b5a66SChristoph Hellwig * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f46b5a66SChristoph Hellwig * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11f46b5a66SChristoph Hellwig * General Public License for more details. 12f46b5a66SChristoph Hellwig * 13f46b5a66SChristoph Hellwig * You should have received a copy of the GNU General Public 14f46b5a66SChristoph Hellwig * License along with this program; if not, write to the 15f46b5a66SChristoph Hellwig * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16f46b5a66SChristoph Hellwig * Boston, MA 021110-1307, USA. 17f46b5a66SChristoph Hellwig */ 18f46b5a66SChristoph Hellwig 19f46b5a66SChristoph Hellwig #include <linux/kernel.h> 20f46b5a66SChristoph Hellwig #include <linux/bio.h> 21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h> 22f46b5a66SChristoph Hellwig #include <linux/file.h> 23f46b5a66SChristoph Hellwig #include <linux/fs.h> 24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 25f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 26f46b5a66SChristoph Hellwig #include <linux/highmem.h> 27f46b5a66SChristoph Hellwig #include <linux/time.h> 28f46b5a66SChristoph Hellwig #include <linux/init.h> 29f46b5a66SChristoph Hellwig #include <linux/string.h> 30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 31cb8e7090SChristoph Hellwig #include <linux/mount.h> 32f46b5a66SChristoph Hellwig #include <linux/mpage.h> 33cb8e7090SChristoph Hellwig #include <linux/namei.h> 34f46b5a66SChristoph Hellwig #include <linux/swap.h> 35f46b5a66SChristoph Hellwig #include <linux/writeback.h> 36f46b5a66SChristoph Hellwig #include <linux/statfs.h> 37f46b5a66SChristoph Hellwig #include <linux/compat.h> 38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h> 39cb8e7090SChristoph Hellwig #include <linux/security.h> 40f46b5a66SChristoph Hellwig #include <linux/xattr.h> 417ea394f1SYan Zheng #include <linux/vmalloc.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 43f7039b1dSLi Dongyang #include <linux/blkdev.h> 448ea05e3aSAlexander Block #include <linux/uuid.h> 4555e301fdSFilipe Brandenburger #include <linux/btrfs.h> 464b4e25f2SChris Mason #include "compat.h" 47f46b5a66SChristoph Hellwig #include "ctree.h" 48f46b5a66SChristoph Hellwig #include "disk-io.h" 49f46b5a66SChristoph Hellwig #include "transaction.h" 50f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 51f46b5a66SChristoph Hellwig #include "print-tree.h" 52f46b5a66SChristoph Hellwig #include "volumes.h" 53925baeddSChris Mason #include "locking.h" 54581bb050SLi Zefan #include "inode-map.h" 55d7728c96SJan Schmidt #include "backref.h" 56606686eeSJosef Bacik #include "rcu-string.h" 5731db9f7cSAlexander Block #include "send.h" 583f6bcfbdSStefan Behrens #include "dev-replace.h" 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 144213490b3SLiu Bo if (flags & BTRFS_INODE_NODATACOW) { 145e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 146213490b3SLiu Bo if (S_ISREG(inode->i_mode)) 147213490b3SLiu Bo BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 148213490b3SLiu Bo } 149e27425d6SJosef Bacik 1506cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1516cbff00fSChristoph Hellwig } 1526cbff00fSChristoph Hellwig 1536cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1546cbff00fSChristoph Hellwig { 155496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1566cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1576cbff00fSChristoph Hellwig 1586cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1596cbff00fSChristoph Hellwig return -EFAULT; 1606cbff00fSChristoph Hellwig return 0; 1616cbff00fSChristoph Hellwig } 1626cbff00fSChristoph Hellwig 16375e7cb7fSLiu Bo static int check_flags(unsigned int flags) 16475e7cb7fSLiu Bo { 16575e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 16675e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 16775e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 168e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 169e1e8fb6aSLi Zefan FS_NOCOW_FL)) 17075e7cb7fSLiu Bo return -EOPNOTSUPP; 17175e7cb7fSLiu Bo 17275e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 17375e7cb7fSLiu Bo return -EINVAL; 17475e7cb7fSLiu Bo 17575e7cb7fSLiu Bo return 0; 17675e7cb7fSLiu Bo } 17775e7cb7fSLiu Bo 1786cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1796cbff00fSChristoph Hellwig { 180496ad9aaSAl Viro struct inode *inode = file_inode(file); 1816cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1826cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1836cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1846cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1856cbff00fSChristoph Hellwig int ret; 186f062abf0SLi Zefan u64 ip_oldflags; 187f062abf0SLi Zefan unsigned int i_oldflags; 1887e97b8daSDavid Sterba umode_t mode; 1896cbff00fSChristoph Hellwig 190b83cc969SLi Zefan if (btrfs_root_readonly(root)) 191b83cc969SLi Zefan return -EROFS; 192b83cc969SLi Zefan 1936cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1946cbff00fSChristoph Hellwig return -EFAULT; 1956cbff00fSChristoph Hellwig 19675e7cb7fSLiu Bo ret = check_flags(flags); 19775e7cb7fSLiu Bo if (ret) 19875e7cb7fSLiu Bo return ret; 1996cbff00fSChristoph Hellwig 2002e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 2016cbff00fSChristoph Hellwig return -EACCES; 2026cbff00fSChristoph Hellwig 203e7848683SJan Kara ret = mnt_want_write_file(file); 204e7848683SJan Kara if (ret) 205e7848683SJan Kara return ret; 206e7848683SJan Kara 2076cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 2086cbff00fSChristoph Hellwig 209f062abf0SLi Zefan ip_oldflags = ip->flags; 210f062abf0SLi Zefan i_oldflags = inode->i_flags; 2117e97b8daSDavid Sterba mode = inode->i_mode; 212f062abf0SLi Zefan 2136cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2146cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2156cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2166cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2176cbff00fSChristoph Hellwig ret = -EPERM; 2186cbff00fSChristoph Hellwig goto out_unlock; 2196cbff00fSChristoph Hellwig } 2206cbff00fSChristoph Hellwig } 2216cbff00fSChristoph Hellwig 2226cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2236cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2246cbff00fSChristoph Hellwig else 2256cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2266cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2276cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2286cbff00fSChristoph Hellwig else 2296cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2306cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2316cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2326cbff00fSChristoph Hellwig else 2336cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2346cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2356cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2366cbff00fSChristoph Hellwig else 2376cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2386cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2396cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2406cbff00fSChristoph Hellwig else 2416cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2426cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2436cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2446cbff00fSChristoph Hellwig else 2456cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2467e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2477e97b8daSDavid Sterba if (S_ISREG(mode)) { 2487e97b8daSDavid Sterba /* 2497e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2507e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2517e97b8daSDavid Sterba * status of the file and will not set it. 2527e97b8daSDavid Sterba */ 2537e97b8daSDavid Sterba if (inode->i_size == 0) 2547e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2557e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2567e97b8daSDavid Sterba } else { 257e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2587e97b8daSDavid Sterba } 2597e97b8daSDavid Sterba } else { 2607e97b8daSDavid Sterba /* 2617e97b8daSDavid Sterba * Revert back under same assuptions as above 2627e97b8daSDavid Sterba */ 2637e97b8daSDavid Sterba if (S_ISREG(mode)) { 2647e97b8daSDavid Sterba if (inode->i_size == 0) 2657e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2667e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2677e97b8daSDavid Sterba } else { 268e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2697e97b8daSDavid Sterba } 2707e97b8daSDavid Sterba } 2716cbff00fSChristoph Hellwig 27275e7cb7fSLiu Bo /* 27375e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 27475e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 27575e7cb7fSLiu Bo * things smaller. 27675e7cb7fSLiu Bo */ 27775e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 27875e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 27975e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 28075e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 28175e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 28275e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 283ebcb904dSLi Zefan } else { 284ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 28575e7cb7fSLiu Bo } 2866cbff00fSChristoph Hellwig 2874da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 288f062abf0SLi Zefan if (IS_ERR(trans)) { 289f062abf0SLi Zefan ret = PTR_ERR(trans); 290f062abf0SLi Zefan goto out_drop; 291f062abf0SLi Zefan } 2926cbff00fSChristoph Hellwig 293306424ccSLi Zefan btrfs_update_iflags(inode); 2940c4d2d95SJosef Bacik inode_inc_iversion(inode); 295306424ccSLi Zefan inode->i_ctime = CURRENT_TIME; 2966cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 2976cbff00fSChristoph Hellwig 2986cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 299f062abf0SLi Zefan out_drop: 300f062abf0SLi Zefan if (ret) { 301f062abf0SLi Zefan ip->flags = ip_oldflags; 302f062abf0SLi Zefan inode->i_flags = i_oldflags; 303f062abf0SLi Zefan } 3046cbff00fSChristoph Hellwig 3056cbff00fSChristoph Hellwig out_unlock: 3066cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 307e7848683SJan Kara mnt_drop_write_file(file); 3082d4e6f6aSliubo return ret; 3096cbff00fSChristoph Hellwig } 3106cbff00fSChristoph Hellwig 3116cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3126cbff00fSChristoph Hellwig { 313496ad9aaSAl Viro struct inode *inode = file_inode(file); 3146cbff00fSChristoph Hellwig 3156cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3166cbff00fSChristoph Hellwig } 317f46b5a66SChristoph Hellwig 318f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 319f7039b1dSLi Dongyang { 320815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); 321f7039b1dSLi Dongyang struct btrfs_device *device; 322f7039b1dSLi Dongyang struct request_queue *q; 323f7039b1dSLi Dongyang struct fstrim_range range; 324f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 325f7039b1dSLi Dongyang u64 num_devices = 0; 326815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 327f7039b1dSLi Dongyang int ret; 328f7039b1dSLi Dongyang 329f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 330f7039b1dSLi Dongyang return -EPERM; 331f7039b1dSLi Dongyang 3321f78160cSXiao Guangrong rcu_read_lock(); 3331f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3341f78160cSXiao Guangrong dev_list) { 335f7039b1dSLi Dongyang if (!device->bdev) 336f7039b1dSLi Dongyang continue; 337f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 338f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 339f7039b1dSLi Dongyang num_devices++; 340f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 341f7039b1dSLi Dongyang minlen); 342f7039b1dSLi Dongyang } 343f7039b1dSLi Dongyang } 3441f78160cSXiao Guangrong rcu_read_unlock(); 345f4c697e6SLukas Czerner 346f7039b1dSLi Dongyang if (!num_devices) 347f7039b1dSLi Dongyang return -EOPNOTSUPP; 348f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 349f7039b1dSLi Dongyang return -EFAULT; 350e515c18bSLukas Czerner if (range.start > total_bytes || 351e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 352f4c697e6SLukas Czerner return -EINVAL; 353f7039b1dSLi Dongyang 354f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 355f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 356815745cfSAl Viro ret = btrfs_trim_fs(fs_info->tree_root, &range); 357f7039b1dSLi Dongyang if (ret < 0) 358f7039b1dSLi Dongyang return ret; 359f7039b1dSLi Dongyang 360f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 361f7039b1dSLi Dongyang return -EFAULT; 362f7039b1dSLi Dongyang 363f7039b1dSLi Dongyang return 0; 364f7039b1dSLi Dongyang } 365f7039b1dSLi Dongyang 366d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 367cb8e7090SChristoph Hellwig struct dentry *dentry, 36872fd032eSSage Weil char *name, int namelen, 3696f72c7e2SArne Jansen u64 *async_transid, 3708696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 371f46b5a66SChristoph Hellwig { 372f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 373f46b5a66SChristoph Hellwig struct btrfs_key key; 374f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 375f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 376f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 377d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 37876dda93cSYan, Zheng struct btrfs_root *new_root; 379d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 3808ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 381f46b5a66SChristoph Hellwig int ret; 382f46b5a66SChristoph Hellwig int err; 383f46b5a66SChristoph Hellwig u64 objectid; 384f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 3853de4586cSChris Mason u64 index = 0; 386d5c12070SMiao Xie u64 qgroup_reserved; 3878ea05e3aSAlexander Block uuid_le new_uuid; 388f46b5a66SChristoph Hellwig 389581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 3902fbe8c8aSAl Viro if (ret) 391a22285a6SYan, Zheng return ret; 3926a912213SJosef Bacik 393d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 3949ed74f2dSJosef Bacik /* 395d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 396d5c12070SMiao Xie * of create_snapshot(). 3979ed74f2dSJosef Bacik */ 398d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 399ee3441b4SJeff Mahoney 7, &qgroup_reserved, false); 400d5c12070SMiao Xie if (ret) 401d5c12070SMiao Xie return ret; 402f46b5a66SChristoph Hellwig 403d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 404d5c12070SMiao Xie if (IS_ERR(trans)) { 405d5c12070SMiao Xie ret = PTR_ERR(trans); 406d5c12070SMiao Xie goto out; 407d5c12070SMiao Xie } 408d5c12070SMiao Xie trans->block_rsv = &block_rsv; 409d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 410f46b5a66SChristoph Hellwig 4118696c533SMiao Xie ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 4126f72c7e2SArne Jansen if (ret) 4136f72c7e2SArne Jansen goto fail; 4146f72c7e2SArne Jansen 4155d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 4165581a51aSJan Schmidt 0, objectid, NULL, 0, 0, 0); 4178e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4188e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4198e8a1e31SJosef Bacik goto fail; 4208e8a1e31SJosef Bacik } 421f46b5a66SChristoph Hellwig 4225d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 423f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 424f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4255d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 426f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 427f46b5a66SChristoph Hellwig 428f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 429f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 430f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 4315d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 4325d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 4335d4f98a2SYan Zheng BTRFS_UUID_SIZE); 434f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 435f46b5a66SChristoph Hellwig 4368ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 4378ea05e3aSAlexander Block 438f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 4393cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 4403cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 4413cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 4423cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 4433cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 444f46b5a66SChristoph Hellwig 4453cae210fSQu Wenruo btrfs_set_root_flags(&root_item, 0); 4463cae210fSQu Wenruo btrfs_set_root_limit(&root_item, 0); 4473cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 44808fe4db1SLi Zefan 449f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 45084234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 451f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 452f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 45386b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 45480ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 455f46b5a66SChristoph Hellwig 4568ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 4578ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 4588ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 4598ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 4603cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); 4613cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); 4628ea05e3aSAlexander Block root_item.ctime = root_item.otime; 4638ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 4648ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 465f46b5a66SChristoph Hellwig 466925baeddSChris Mason btrfs_tree_unlock(leaf); 467f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 468f46b5a66SChristoph Hellwig leaf = NULL; 469f46b5a66SChristoph Hellwig 470f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 471f46b5a66SChristoph Hellwig 472f46b5a66SChristoph Hellwig key.objectid = objectid; 4735d4f98a2SYan Zheng key.offset = 0; 474f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 475f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 476f46b5a66SChristoph Hellwig &root_item); 477f46b5a66SChristoph Hellwig if (ret) 478f46b5a66SChristoph Hellwig goto fail; 479f46b5a66SChristoph Hellwig 48076dda93cSYan, Zheng key.offset = (u64)-1; 48176dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 48279787eaaSJeff Mahoney if (IS_ERR(new_root)) { 48379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); 48479787eaaSJeff Mahoney ret = PTR_ERR(new_root); 48579787eaaSJeff Mahoney goto fail; 48679787eaaSJeff Mahoney } 48776dda93cSYan, Zheng 48876dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 48976dda93cSYan, Zheng 490d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 491ce598979SMark Fasheh if (ret) { 492ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 49379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 494ce598979SMark Fasheh goto fail; 495ce598979SMark Fasheh } 496ce598979SMark Fasheh 497f46b5a66SChristoph Hellwig /* 498f46b5a66SChristoph Hellwig * insert the directory item 499f46b5a66SChristoph Hellwig */ 5003de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 50179787eaaSJeff Mahoney if (ret) { 50279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 50379787eaaSJeff Mahoney goto fail; 50479787eaaSJeff Mahoney } 5053de4586cSChris Mason 5063de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 50716cdcec7SMiao Xie name, namelen, dir, &key, 5083de4586cSChris Mason BTRFS_FT_DIR, index); 50979787eaaSJeff Mahoney if (ret) { 51079787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 511f46b5a66SChristoph Hellwig goto fail; 51279787eaaSJeff Mahoney } 5130660b5afSChris Mason 51452c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 51552c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 51652c26179SYan Zheng BUG_ON(ret); 51752c26179SYan Zheng 5180660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5194df27c4dSYan, Zheng objectid, root->root_key.objectid, 52033345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 52176dda93cSYan, Zheng 5220660b5afSChris Mason BUG_ON(ret); 5230660b5afSChris Mason 524f46b5a66SChristoph Hellwig fail: 525d5c12070SMiao Xie trans->block_rsv = NULL; 526d5c12070SMiao Xie trans->bytes_reserved = 0; 52772fd032eSSage Weil if (async_transid) { 52872fd032eSSage Weil *async_transid = trans->transid; 52972fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 53000d71c9cSMiao Xie if (err) 53100d71c9cSMiao Xie err = btrfs_commit_transaction(trans, root); 53272fd032eSSage Weil } else { 53376dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 53472fd032eSSage Weil } 535f46b5a66SChristoph Hellwig if (err && !ret) 536f46b5a66SChristoph Hellwig ret = err; 5371a65e24bSChris Mason 5381a65e24bSChris Mason if (!ret) 5391a65e24bSChris Mason d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 540d5c12070SMiao Xie out: 541d5c12070SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 542f46b5a66SChristoph Hellwig return ret; 543f46b5a66SChristoph Hellwig } 544f46b5a66SChristoph Hellwig 545e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 546e9662f70SMiao Xie struct dentry *dentry, char *name, int namelen, 547e9662f70SMiao Xie u64 *async_transid, bool readonly, 548e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 549f46b5a66SChristoph Hellwig { 5502e4bfab9SYan, Zheng struct inode *inode; 551f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 552f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 5532e4bfab9SYan, Zheng int ret; 554f46b5a66SChristoph Hellwig 555f46b5a66SChristoph Hellwig if (!root->ref_cows) 556f46b5a66SChristoph Hellwig return -EINVAL; 557f46b5a66SChristoph Hellwig 5586a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 5596a03843dSMiao Xie if (ret) 5606a03843dSMiao Xie return ret; 5616a03843dSMiao Xie 5626a03843dSMiao Xie btrfs_wait_ordered_extents(root, 0); 5636a03843dSMiao Xie 564a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 565a22285a6SYan, Zheng if (!pending_snapshot) 566a22285a6SYan, Zheng return -ENOMEM; 567a22285a6SYan, Zheng 56866d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 56966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 570d5c12070SMiao Xie /* 571d5c12070SMiao Xie * 1 - parent dir inode 572d5c12070SMiao Xie * 2 - dir entries 573d5c12070SMiao Xie * 1 - root item 574d5c12070SMiao Xie * 2 - root ref/backref 575d5c12070SMiao Xie * 1 - root of snapshot 576d5c12070SMiao Xie */ 577d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 578d5c12070SMiao Xie &pending_snapshot->block_rsv, 7, 579ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 580ee3441b4SJeff Mahoney false); 581d5c12070SMiao Xie if (ret) 582d5c12070SMiao Xie goto out; 583d5c12070SMiao Xie 584a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 585a22285a6SYan, Zheng pending_snapshot->root = root; 586b83cc969SLi Zefan pending_snapshot->readonly = readonly; 587e9662f70SMiao Xie pending_snapshot->dir = dir; 5888696c533SMiao Xie pending_snapshot->inherit = inherit; 589a22285a6SYan, Zheng 590d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 591a22285a6SYan, Zheng if (IS_ERR(trans)) { 592a22285a6SYan, Zheng ret = PTR_ERR(trans); 593a22285a6SYan, Zheng goto fail; 594a22285a6SYan, Zheng } 595a22285a6SYan, Zheng 5968351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 597a22285a6SYan, Zheng list_add(&pending_snapshot->list, 598a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 5998351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 60072fd032eSSage Weil if (async_transid) { 60172fd032eSSage Weil *async_transid = trans->transid; 60272fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 60372fd032eSSage Weil root->fs_info->extent_root, 1); 60400d71c9cSMiao Xie if (ret) 60500d71c9cSMiao Xie ret = btrfs_commit_transaction(trans, root); 60672fd032eSSage Weil } else { 60772fd032eSSage Weil ret = btrfs_commit_transaction(trans, 60872fd032eSSage Weil root->fs_info->extent_root); 60972fd032eSSage Weil } 610aec8030aSMiao Xie if (ret) 611c37b2b62SJosef Bacik goto fail; 612a22285a6SYan, Zheng 613a22285a6SYan, Zheng ret = pending_snapshot->error; 614f46b5a66SChristoph Hellwig if (ret) 6152e4bfab9SYan, Zheng goto fail; 616f46b5a66SChristoph Hellwig 61766b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 61866b4ffd1SJosef Bacik if (ret) 61966b4ffd1SJosef Bacik goto fail; 620f46b5a66SChristoph Hellwig 6212fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 6222e4bfab9SYan, Zheng if (IS_ERR(inode)) { 6232e4bfab9SYan, Zheng ret = PTR_ERR(inode); 6242e4bfab9SYan, Zheng goto fail; 6252e4bfab9SYan, Zheng } 6262e4bfab9SYan, Zheng BUG_ON(!inode); 6272e4bfab9SYan, Zheng d_instantiate(dentry, inode); 6282e4bfab9SYan, Zheng ret = 0; 6292e4bfab9SYan, Zheng fail: 630d5c12070SMiao Xie btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, 631d5c12070SMiao Xie &pending_snapshot->block_rsv, 632d5c12070SMiao Xie pending_snapshot->qgroup_reserved); 633d5c12070SMiao Xie out: 634a22285a6SYan, Zheng kfree(pending_snapshot); 635f46b5a66SChristoph Hellwig return ret; 636f46b5a66SChristoph Hellwig } 637f46b5a66SChristoph Hellwig 6384260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 6394260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 6404260f7c7SSage Weil * minimal. 6414260f7c7SSage Weil */ 6424260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 6434260f7c7SSage Weil { 6442f2f43d3SEric W. Biederman kuid_t fsuid = current_fsuid(); 6454260f7c7SSage Weil 6464260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 6474260f7c7SSage Weil return 0; 6482f2f43d3SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 6494260f7c7SSage Weil return 0; 6502f2f43d3SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 6514260f7c7SSage Weil return 0; 6524260f7c7SSage Weil return !capable(CAP_FOWNER); 6534260f7c7SSage Weil } 6544260f7c7SSage Weil 6554260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 6564260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 6574260f7c7SSage Weil * whether the type of victim is right. 6584260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 6594260f7c7SSage Weil * 2. We should have write and exec permissions on dir 6604260f7c7SSage Weil * 3. We can't remove anything from append-only dir 6614260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 6624260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 6634260f7c7SSage Weil * a. be owner of dir, or 6644260f7c7SSage Weil * b. be owner of victim, or 6654260f7c7SSage Weil * c. have CAP_FOWNER capability 6664260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 6674260f7c7SSage Weil * links pointing to it. 6684260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 6694260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 6704260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 6714260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 6724260f7c7SSage Weil * nfs_async_unlink(). 6734260f7c7SSage Weil */ 6744260f7c7SSage Weil 6754260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 6764260f7c7SSage Weil { 6774260f7c7SSage Weil int error; 6784260f7c7SSage Weil 6794260f7c7SSage Weil if (!victim->d_inode) 6804260f7c7SSage Weil return -ENOENT; 6814260f7c7SSage Weil 6824260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 6834fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 6844260f7c7SSage Weil 6854260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 6864260f7c7SSage Weil if (error) 6874260f7c7SSage Weil return error; 6884260f7c7SSage Weil if (IS_APPEND(dir)) 6894260f7c7SSage Weil return -EPERM; 6904260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 6914260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 6924260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 6934260f7c7SSage Weil return -EPERM; 6944260f7c7SSage Weil if (isdir) { 6954260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 6964260f7c7SSage Weil return -ENOTDIR; 6974260f7c7SSage Weil if (IS_ROOT(victim)) 6984260f7c7SSage Weil return -EBUSY; 6994260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 7004260f7c7SSage Weil return -EISDIR; 7014260f7c7SSage Weil if (IS_DEADDIR(dir)) 7024260f7c7SSage Weil return -ENOENT; 7034260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 7044260f7c7SSage Weil return -EBUSY; 7054260f7c7SSage Weil return 0; 7064260f7c7SSage Weil } 7074260f7c7SSage Weil 708cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 709cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 710cb8e7090SChristoph Hellwig { 711cb8e7090SChristoph Hellwig if (child->d_inode) 712cb8e7090SChristoph Hellwig return -EEXIST; 713cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 714cb8e7090SChristoph Hellwig return -ENOENT; 715cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 716cb8e7090SChristoph Hellwig } 717cb8e7090SChristoph Hellwig 718cb8e7090SChristoph Hellwig /* 719cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 720cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 721cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 722cb8e7090SChristoph Hellwig */ 72376dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 72476dda93cSYan, Zheng char *name, int namelen, 72572fd032eSSage Weil struct btrfs_root *snap_src, 7266f72c7e2SArne Jansen u64 *async_transid, bool readonly, 7278696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 728cb8e7090SChristoph Hellwig { 72976dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 730cb8e7090SChristoph Hellwig struct dentry *dentry; 731cb8e7090SChristoph Hellwig int error; 732cb8e7090SChristoph Hellwig 7335c50c9b8SDavid Sterba error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 7345c50c9b8SDavid Sterba if (error == -EINTR) 7355c50c9b8SDavid Sterba return error; 736cb8e7090SChristoph Hellwig 737cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 738cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 739cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 740cb8e7090SChristoph Hellwig goto out_unlock; 741cb8e7090SChristoph Hellwig 742cb8e7090SChristoph Hellwig error = -EEXIST; 743cb8e7090SChristoph Hellwig if (dentry->d_inode) 744cb8e7090SChristoph Hellwig goto out_dput; 745cb8e7090SChristoph Hellwig 74676dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 747cb8e7090SChristoph Hellwig if (error) 748a874a63eSLiu Bo goto out_dput; 749cb8e7090SChristoph Hellwig 7509c52057cSChris Mason /* 7519c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 7529c52057cSChris Mason * check for them now when we can safely fail 7539c52057cSChris Mason */ 7549c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 7559c52057cSChris Mason dir->i_ino, name, 7569c52057cSChris Mason namelen); 7579c52057cSChris Mason if (error) 7589c52057cSChris Mason goto out_dput; 7599c52057cSChris Mason 76076dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 76176dda93cSYan, Zheng 76276dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 76376dda93cSYan, Zheng goto out_up_read; 76476dda93cSYan, Zheng 7653de4586cSChris Mason if (snap_src) { 766e9662f70SMiao Xie error = create_snapshot(snap_src, dir, dentry, name, namelen, 7676f72c7e2SArne Jansen async_transid, readonly, inherit); 7683de4586cSChris Mason } else { 769d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 770d5c12070SMiao Xie async_transid, inherit); 7713de4586cSChris Mason } 77276dda93cSYan, Zheng if (!error) 77376dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 77476dda93cSYan, Zheng out_up_read: 77576dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 776cb8e7090SChristoph Hellwig out_dput: 777cb8e7090SChristoph Hellwig dput(dentry); 778cb8e7090SChristoph Hellwig out_unlock: 77976dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 780cb8e7090SChristoph Hellwig return error; 781cb8e7090SChristoph Hellwig } 782cb8e7090SChristoph Hellwig 7834cb5300bSChris Mason /* 7844cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 7854cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 7864cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 7874cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 7884cb5300bSChris Mason * part of the file 7894cb5300bSChris Mason */ 7904cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 7914cb5300bSChris Mason { 7924cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 7934cb5300bSChris Mason struct extent_map *em = NULL; 7944cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 7954cb5300bSChris Mason u64 end; 7964cb5300bSChris Mason 7974cb5300bSChris Mason read_lock(&em_tree->lock); 7984cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 7994cb5300bSChris Mason read_unlock(&em_tree->lock); 8004cb5300bSChris Mason 8014cb5300bSChris Mason if (em) { 8024cb5300bSChris Mason end = extent_map_end(em); 8034cb5300bSChris Mason free_extent_map(em); 8044cb5300bSChris Mason if (end - offset > thresh) 8054cb5300bSChris Mason return 0; 8064cb5300bSChris Mason } 8074cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 8084cb5300bSChris Mason thresh /= 2; 8094cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 8104cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 8114cb5300bSChris Mason if (end >= thresh) 8124cb5300bSChris Mason return 0; 8134cb5300bSChris Mason return 1; 8144cb5300bSChris Mason } 8154cb5300bSChris Mason 8164cb5300bSChris Mason /* 8174cb5300bSChris Mason * helper function to walk through a file and find extents 8184cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 8194cb5300bSChris Mason * 8204cb5300bSChris Mason * This is used by the defragging code to find new and small 8214cb5300bSChris Mason * extents 8224cb5300bSChris Mason */ 8234cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 8244cb5300bSChris Mason struct inode *inode, u64 newer_than, 8254cb5300bSChris Mason u64 *off, int thresh) 8264cb5300bSChris Mason { 8274cb5300bSChris Mason struct btrfs_path *path; 8284cb5300bSChris Mason struct btrfs_key min_key; 8294cb5300bSChris Mason struct btrfs_key max_key; 8304cb5300bSChris Mason struct extent_buffer *leaf; 8314cb5300bSChris Mason struct btrfs_file_extent_item *extent; 8324cb5300bSChris Mason int type; 8334cb5300bSChris Mason int ret; 834a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 8354cb5300bSChris Mason 8364cb5300bSChris Mason path = btrfs_alloc_path(); 8374cb5300bSChris Mason if (!path) 8384cb5300bSChris Mason return -ENOMEM; 8394cb5300bSChris Mason 840a4689d2bSDavid Sterba min_key.objectid = ino; 8414cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 8424cb5300bSChris Mason min_key.offset = *off; 8434cb5300bSChris Mason 844a4689d2bSDavid Sterba max_key.objectid = ino; 8454cb5300bSChris Mason max_key.type = (u8)-1; 8464cb5300bSChris Mason max_key.offset = (u64)-1; 8474cb5300bSChris Mason 8484cb5300bSChris Mason path->keep_locks = 1; 8494cb5300bSChris Mason 8504cb5300bSChris Mason while(1) { 8514cb5300bSChris Mason ret = btrfs_search_forward(root, &min_key, &max_key, 852de78b51aSEric Sandeen path, newer_than); 8534cb5300bSChris Mason if (ret != 0) 8544cb5300bSChris Mason goto none; 855a4689d2bSDavid Sterba if (min_key.objectid != ino) 8564cb5300bSChris Mason goto none; 8574cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 8584cb5300bSChris Mason goto none; 8594cb5300bSChris Mason 8604cb5300bSChris Mason leaf = path->nodes[0]; 8614cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 8624cb5300bSChris Mason struct btrfs_file_extent_item); 8634cb5300bSChris Mason 8644cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 8654cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 8664cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 8674cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 8684cb5300bSChris Mason *off = min_key.offset; 8694cb5300bSChris Mason btrfs_free_path(path); 8704cb5300bSChris Mason return 0; 8714cb5300bSChris Mason } 8724cb5300bSChris Mason 8734cb5300bSChris Mason if (min_key.offset == (u64)-1) 8744cb5300bSChris Mason goto none; 8754cb5300bSChris Mason 8764cb5300bSChris Mason min_key.offset++; 8774cb5300bSChris Mason btrfs_release_path(path); 8784cb5300bSChris Mason } 8794cb5300bSChris Mason none: 8804cb5300bSChris Mason btrfs_free_path(path); 8814cb5300bSChris Mason return -ENOENT; 8824cb5300bSChris Mason } 8834cb5300bSChris Mason 8846c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 88517ce6ef8SLiu Bo { 88617ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 887940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8886c282eb4SLi Zefan struct extent_map *em; 8896c282eb4SLi Zefan u64 len = PAGE_CACHE_SIZE; 890940100a4SChris Mason 891940100a4SChris Mason /* 892940100a4SChris Mason * hopefully we have this extent in the tree already, try without 893940100a4SChris Mason * the full extent lock 894940100a4SChris Mason */ 895940100a4SChris Mason read_lock(&em_tree->lock); 896940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 897940100a4SChris Mason read_unlock(&em_tree->lock); 898940100a4SChris Mason 899940100a4SChris Mason if (!em) { 900940100a4SChris Mason /* get the big lock and read metadata off disk */ 901d0082371SJeff Mahoney lock_extent(io_tree, start, start + len - 1); 902940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 903d0082371SJeff Mahoney unlock_extent(io_tree, start, start + len - 1); 904940100a4SChris Mason 9056cf8bfbfSDan Carpenter if (IS_ERR(em)) 9066c282eb4SLi Zefan return NULL; 907940100a4SChris Mason } 908940100a4SChris Mason 9096c282eb4SLi Zefan return em; 9106c282eb4SLi Zefan } 9116c282eb4SLi Zefan 9126c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 9136c282eb4SLi Zefan { 9146c282eb4SLi Zefan struct extent_map *next; 9156c282eb4SLi Zefan bool ret = true; 9166c282eb4SLi Zefan 9176c282eb4SLi Zefan /* this is the last extent */ 9186c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 9196c282eb4SLi Zefan return false; 9206c282eb4SLi Zefan 9216c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 9226c282eb4SLi Zefan if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 9236c282eb4SLi Zefan ret = false; 9246c282eb4SLi Zefan 9256c282eb4SLi Zefan free_extent_map(next); 9266c282eb4SLi Zefan return ret; 9276c282eb4SLi Zefan } 9286c282eb4SLi Zefan 9296c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh, 930a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 931a43a2111SAndrew Mahone int compress) 9326c282eb4SLi Zefan { 9336c282eb4SLi Zefan struct extent_map *em; 9346c282eb4SLi Zefan int ret = 1; 9356c282eb4SLi Zefan bool next_mergeable = true; 9366c282eb4SLi Zefan 9376c282eb4SLi Zefan /* 9386c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 9396c282eb4SLi Zefan * defragging it 9406c282eb4SLi Zefan */ 9416c282eb4SLi Zefan if (start < *defrag_end) 9426c282eb4SLi Zefan return 1; 9436c282eb4SLi Zefan 9446c282eb4SLi Zefan *skip = 0; 9456c282eb4SLi Zefan 9466c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 9476c282eb4SLi Zefan if (!em) 9486c282eb4SLi Zefan return 0; 9496c282eb4SLi Zefan 950940100a4SChris Mason /* this will cover holes, and inline extents */ 95117ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 952940100a4SChris Mason ret = 0; 95317ce6ef8SLiu Bo goto out; 95417ce6ef8SLiu Bo } 95517ce6ef8SLiu Bo 9566c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 957940100a4SChris Mason 958940100a4SChris Mason /* 9596c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 9606c282eb4SLi Zefan * real extent, don't bother defragging it 961940100a4SChris Mason */ 962a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 9636c282eb4SLi Zefan (em->len >= thresh || !next_mergeable)) 964940100a4SChris Mason ret = 0; 96517ce6ef8SLiu Bo out: 966940100a4SChris Mason /* 967940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 968940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 969940100a4SChris Mason * so that the next tiny extent will force a defrag. 970940100a4SChris Mason * 971940100a4SChris Mason * The end result of this is that tiny extents before a single big 972940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 973940100a4SChris Mason */ 974940100a4SChris Mason if (ret) { 975940100a4SChris Mason *defrag_end = extent_map_end(em); 976940100a4SChris Mason } else { 977940100a4SChris Mason *last_len = 0; 978940100a4SChris Mason *skip = extent_map_end(em); 979940100a4SChris Mason *defrag_end = 0; 980940100a4SChris Mason } 981940100a4SChris Mason 982940100a4SChris Mason free_extent_map(em); 983940100a4SChris Mason return ret; 984940100a4SChris Mason } 985940100a4SChris Mason 9864cb5300bSChris Mason /* 9874cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 9884cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 9894cb5300bSChris Mason * to COW and defrag. 9904cb5300bSChris Mason * 9914cb5300bSChris Mason * It also makes sure the delalloc code has enough 9924cb5300bSChris Mason * dirty data to avoid making new small extents as part 9934cb5300bSChris Mason * of the defrag 9944cb5300bSChris Mason * 9954cb5300bSChris Mason * It's a good idea to start RA on this range 9964cb5300bSChris Mason * before calling this. 9974cb5300bSChris Mason */ 9984cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 9994cb5300bSChris Mason struct page **pages, 10004cb5300bSChris Mason unsigned long start_index, 10014cb5300bSChris Mason int num_pages) 1002f46b5a66SChristoph Hellwig { 10034cb5300bSChris Mason unsigned long file_end; 10044cb5300bSChris Mason u64 isize = i_size_read(inode); 1005f46b5a66SChristoph Hellwig u64 page_start; 1006f46b5a66SChristoph Hellwig u64 page_end; 10071f12bd06SLiu Bo u64 page_cnt; 10084cb5300bSChris Mason int ret; 10094cb5300bSChris Mason int i; 10104cb5300bSChris Mason int i_done; 10114cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 10124cb5300bSChris Mason struct extent_state *cached_state = NULL; 1013600a45e1SMiao Xie struct extent_io_tree *tree; 10143b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 10154cb5300bSChris Mason 10164cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 10171f12bd06SLiu Bo if (!isize || start_index > file_end) 10181f12bd06SLiu Bo return 0; 10191f12bd06SLiu Bo 10201f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 10214cb5300bSChris Mason 10224cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 10231f12bd06SLiu Bo page_cnt << PAGE_CACHE_SHIFT); 10244cb5300bSChris Mason if (ret) 10254cb5300bSChris Mason return ret; 10264cb5300bSChris Mason i_done = 0; 1027600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 10284cb5300bSChris Mason 10294cb5300bSChris Mason /* step one, lock all the pages */ 10301f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 10314cb5300bSChris Mason struct page *page; 1032600a45e1SMiao Xie again: 1033a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 10343b16a4e3SJosef Bacik start_index + i, mask); 10354cb5300bSChris Mason if (!page) 10364cb5300bSChris Mason break; 10374cb5300bSChris Mason 1038600a45e1SMiao Xie page_start = page_offset(page); 1039600a45e1SMiao Xie page_end = page_start + PAGE_CACHE_SIZE - 1; 1040600a45e1SMiao Xie while (1) { 1041d0082371SJeff Mahoney lock_extent(tree, page_start, page_end); 1042600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1043600a45e1SMiao Xie page_start); 1044d0082371SJeff Mahoney unlock_extent(tree, page_start, page_end); 1045600a45e1SMiao Xie if (!ordered) 1046600a45e1SMiao Xie break; 1047600a45e1SMiao Xie 1048600a45e1SMiao Xie unlock_page(page); 1049600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1050600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1051600a45e1SMiao Xie lock_page(page); 10521f12bd06SLiu Bo /* 10531f12bd06SLiu Bo * we unlocked the page above, so we need check if 10541f12bd06SLiu Bo * it was released or not. 10551f12bd06SLiu Bo */ 10561f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 10571f12bd06SLiu Bo unlock_page(page); 10581f12bd06SLiu Bo page_cache_release(page); 10591f12bd06SLiu Bo goto again; 10601f12bd06SLiu Bo } 1061600a45e1SMiao Xie } 1062600a45e1SMiao Xie 10634cb5300bSChris Mason if (!PageUptodate(page)) { 10644cb5300bSChris Mason btrfs_readpage(NULL, page); 10654cb5300bSChris Mason lock_page(page); 10664cb5300bSChris Mason if (!PageUptodate(page)) { 10674cb5300bSChris Mason unlock_page(page); 10684cb5300bSChris Mason page_cache_release(page); 10694cb5300bSChris Mason ret = -EIO; 10704cb5300bSChris Mason break; 10714cb5300bSChris Mason } 10724cb5300bSChris Mason } 1073600a45e1SMiao Xie 1074600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1075600a45e1SMiao Xie unlock_page(page); 1076600a45e1SMiao Xie page_cache_release(page); 1077600a45e1SMiao Xie goto again; 1078600a45e1SMiao Xie } 1079600a45e1SMiao Xie 10804cb5300bSChris Mason pages[i] = page; 10814cb5300bSChris Mason i_done++; 10824cb5300bSChris Mason } 10834cb5300bSChris Mason if (!i_done || ret) 10844cb5300bSChris Mason goto out; 10854cb5300bSChris Mason 10864cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 10874cb5300bSChris Mason goto out; 10884cb5300bSChris Mason 10894cb5300bSChris Mason /* 10904cb5300bSChris Mason * so now we have a nice long stream of locked 10914cb5300bSChris Mason * and up to date pages, lets wait on them 10924cb5300bSChris Mason */ 10934cb5300bSChris Mason for (i = 0; i < i_done; i++) 10944cb5300bSChris Mason wait_on_page_writeback(pages[i]); 10954cb5300bSChris Mason 10964cb5300bSChris Mason page_start = page_offset(pages[0]); 10974cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 10984cb5300bSChris Mason 10994cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1100d0082371SJeff Mahoney page_start, page_end - 1, 0, &cached_state); 11014cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 11024cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 11039e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 11049e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 11054cb5300bSChris Mason 11061f12bd06SLiu Bo if (i_done != page_cnt) { 11079e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 11089e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 11099e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 11104cb5300bSChris Mason btrfs_delalloc_release_space(inode, 11111f12bd06SLiu Bo (page_cnt - i_done) << PAGE_CACHE_SHIFT); 11124cb5300bSChris Mason } 11134cb5300bSChris Mason 11144cb5300bSChris Mason 11159e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 11169e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 11174cb5300bSChris Mason 11184cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 11194cb5300bSChris Mason page_start, page_end - 1, &cached_state, 11204cb5300bSChris Mason GFP_NOFS); 11214cb5300bSChris Mason 11224cb5300bSChris Mason for (i = 0; i < i_done; i++) { 11234cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 11244cb5300bSChris Mason ClearPageChecked(pages[i]); 11254cb5300bSChris Mason set_page_extent_mapped(pages[i]); 11264cb5300bSChris Mason set_page_dirty(pages[i]); 11274cb5300bSChris Mason unlock_page(pages[i]); 11284cb5300bSChris Mason page_cache_release(pages[i]); 11294cb5300bSChris Mason } 11304cb5300bSChris Mason return i_done; 11314cb5300bSChris Mason out: 11324cb5300bSChris Mason for (i = 0; i < i_done; i++) { 11334cb5300bSChris Mason unlock_page(pages[i]); 11344cb5300bSChris Mason page_cache_release(pages[i]); 11354cb5300bSChris Mason } 11361f12bd06SLiu Bo btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); 11374cb5300bSChris Mason return ret; 11384cb5300bSChris Mason 11394cb5300bSChris Mason } 11404cb5300bSChris Mason 11414cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 11424cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 11434cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 11444cb5300bSChris Mason { 11454cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 11464cb5300bSChris Mason struct file_ra_state *ra = NULL; 11474cb5300bSChris Mason unsigned long last_index; 1148151a31b2SLi Zefan u64 isize = i_size_read(inode); 1149940100a4SChris Mason u64 last_len = 0; 1150940100a4SChris Mason u64 skip = 0; 1151940100a4SChris Mason u64 defrag_end = 0; 11524cb5300bSChris Mason u64 newer_off = range->start; 1153f46b5a66SChristoph Hellwig unsigned long i; 1154008873eaSLi Zefan unsigned long ra_index = 0; 1155f46b5a66SChristoph Hellwig int ret; 11564cb5300bSChris Mason int defrag_count = 0; 11571a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 11584cb5300bSChris Mason int extent_thresh = range->extent_thresh; 1159008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1160008873eaSLi Zefan int cluster = max_cluster; 11614cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 11624cb5300bSChris Mason struct page **pages = NULL; 11634cb5300bSChris Mason 11640abd5b17SLiu Bo if (isize == 0) 11650abd5b17SLiu Bo return 0; 11660abd5b17SLiu Bo 11670abd5b17SLiu Bo if (range->start >= isize) 11680abd5b17SLiu Bo return -EINVAL; 11691a419d85SLi Zefan 11701a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 11711a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 11721a419d85SLi Zefan return -EINVAL; 11731a419d85SLi Zefan if (range->compress_type) 11741a419d85SLi Zefan compress_type = range->compress_type; 11751a419d85SLi Zefan } 1176f46b5a66SChristoph Hellwig 11770abd5b17SLiu Bo if (extent_thresh == 0) 11780abd5b17SLiu Bo extent_thresh = 256 * 1024; 1179f46b5a66SChristoph Hellwig 11804cb5300bSChris Mason /* 11814cb5300bSChris Mason * if we were not given a file, allocate a readahead 11824cb5300bSChris Mason * context 11834cb5300bSChris Mason */ 11844cb5300bSChris Mason if (!file) { 11854cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 11864cb5300bSChris Mason if (!ra) 11874cb5300bSChris Mason return -ENOMEM; 11884cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 11894cb5300bSChris Mason } else { 11904cb5300bSChris Mason ra = &file->f_ra; 11914cb5300bSChris Mason } 11924cb5300bSChris Mason 1193008873eaSLi Zefan pages = kmalloc(sizeof(struct page *) * max_cluster, 11944cb5300bSChris Mason GFP_NOFS); 11954cb5300bSChris Mason if (!pages) { 11964cb5300bSChris Mason ret = -ENOMEM; 11974cb5300bSChris Mason goto out_ra; 11984cb5300bSChris Mason } 11994cb5300bSChris Mason 12004cb5300bSChris Mason /* find the last page to defrag */ 12011e701a32SChris Mason if (range->start + range->len > range->start) { 1202151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 12031e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 12041e701a32SChris Mason } else { 1205151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 12061e701a32SChris Mason } 12071e701a32SChris Mason 12084cb5300bSChris Mason if (newer_than) { 12094cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 12104cb5300bSChris Mason &newer_off, 64 * 1024); 12114cb5300bSChris Mason if (!ret) { 12124cb5300bSChris Mason range->start = newer_off; 12134cb5300bSChris Mason /* 12144cb5300bSChris Mason * we always align our defrag to help keep 12154cb5300bSChris Mason * the extents in the file evenly spaced 12164cb5300bSChris Mason */ 12174cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 12184cb5300bSChris Mason } else 12194cb5300bSChris Mason goto out_ra; 12204cb5300bSChris Mason } else { 12211e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 12224cb5300bSChris Mason } 12234cb5300bSChris Mason if (!max_to_defrag) 12247ec31b54SLiu Bo max_to_defrag = last_index + 1; 12254cb5300bSChris Mason 12262a0f7f57SLi Zefan /* 12272a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 12282a0f7f57SLi Zefan * written sequentially. 12292a0f7f57SLi Zefan */ 12302a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 12312a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 12322a0f7f57SLi Zefan 1233f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1234f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1235f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 12364cb5300bSChris Mason /* 12374cb5300bSChris Mason * make sure we stop running if someone unmounts 12384cb5300bSChris Mason * the FS 12394cb5300bSChris Mason */ 12404cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 12414cb5300bSChris Mason break; 12424cb5300bSChris Mason 1243210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1244210549ebSDavid Sterba printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); 1245210549ebSDavid Sterba ret = -EAGAIN; 1246210549ebSDavid Sterba break; 1247210549ebSDavid Sterba } 1248210549ebSDavid Sterba 124966c26892SLiu Bo if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 12506c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1251a43a2111SAndrew Mahone &defrag_end, range->flags & 1252a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1253940100a4SChris Mason unsigned long next; 1254940100a4SChris Mason /* 1255940100a4SChris Mason * the should_defrag function tells us how much to skip 1256940100a4SChris Mason * bump our counter by the suggested amount 1257940100a4SChris Mason */ 1258940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1259940100a4SChris Mason i = max(i + 1, next); 1260940100a4SChris Mason continue; 1261940100a4SChris Mason } 1262008873eaSLi Zefan 1263008873eaSLi Zefan if (!newer_than) { 1264008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1265008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1266008873eaSLi Zefan cluster = min(cluster, max_cluster); 1267008873eaSLi Zefan } else { 1268008873eaSLi Zefan cluster = max_cluster; 1269008873eaSLi Zefan } 1270008873eaSLi Zefan 12711e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 12721a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 1273940100a4SChris Mason 1274008873eaSLi Zefan if (i + cluster > ra_index) { 1275008873eaSLi Zefan ra_index = max(i, ra_index); 1276008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1277008873eaSLi Zefan cluster); 1278008873eaSLi Zefan ra_index += max_cluster; 1279008873eaSLi Zefan } 12804cb5300bSChris Mason 1281ecb8bea8SLiu Bo mutex_lock(&inode->i_mutex); 1282008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1283ecb8bea8SLiu Bo if (ret < 0) { 1284ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12854cb5300bSChris Mason goto out_ra; 1286ecb8bea8SLiu Bo } 12874cb5300bSChris Mason 12884cb5300bSChris Mason defrag_count += ret; 1289d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 1290ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12914cb5300bSChris Mason 12924cb5300bSChris Mason if (newer_than) { 12934cb5300bSChris Mason if (newer_off == (u64)-1) 12944cb5300bSChris Mason break; 12954cb5300bSChris Mason 1296e1f041e1SLiu Bo if (ret > 0) 1297e1f041e1SLiu Bo i += ret; 1298e1f041e1SLiu Bo 12994cb5300bSChris Mason newer_off = max(newer_off + 1, 13004cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 13014cb5300bSChris Mason 13024cb5300bSChris Mason ret = find_new_extents(root, inode, 13034cb5300bSChris Mason newer_than, &newer_off, 13044cb5300bSChris Mason 64 * 1024); 13054cb5300bSChris Mason if (!ret) { 13064cb5300bSChris Mason range->start = newer_off; 13074cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 13084cb5300bSChris Mason } else { 13094cb5300bSChris Mason break; 1310940100a4SChris Mason } 13114cb5300bSChris Mason } else { 1312008873eaSLi Zefan if (ret > 0) { 1313cbcc8326SLi Zefan i += ret; 1314008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1315008873eaSLi Zefan } else { 1316940100a4SChris Mason i++; 1317008873eaSLi Zefan last_len = 0; 1318008873eaSLi Zefan } 1319f46b5a66SChristoph Hellwig } 13204cb5300bSChris Mason } 1321f46b5a66SChristoph Hellwig 13221e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 13231e701a32SChris Mason filemap_flush(inode->i_mapping); 13241e701a32SChris Mason 13251e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 13261e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 13271e701a32SChris Mason * we have to make sure the IO is actually started and that 13281e701a32SChris Mason * ordered extents get created before we return 13291e701a32SChris Mason */ 13301e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 13311e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 13321e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 13331e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 13341e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 13351e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 13361e701a32SChris Mason } 13371e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 13381e701a32SChris Mason 13391e701a32SChris Mason mutex_lock(&inode->i_mutex); 1340261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 13411e701a32SChris Mason mutex_unlock(&inode->i_mutex); 13421e701a32SChris Mason } 13431e701a32SChris Mason 13441a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 13452b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 13461a419d85SLi Zefan } 13471a419d85SLi Zefan 134860ccf82fSDiego Calleja ret = defrag_count; 1349940100a4SChris Mason 13504cb5300bSChris Mason out_ra: 13514cb5300bSChris Mason if (!file) 13524cb5300bSChris Mason kfree(ra); 13534cb5300bSChris Mason kfree(pages); 1354940100a4SChris Mason return ret; 1355f46b5a66SChristoph Hellwig } 1356f46b5a66SChristoph Hellwig 1357198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 135876dda93cSYan, Zheng void __user *arg) 1359f46b5a66SChristoph Hellwig { 1360f46b5a66SChristoph Hellwig u64 new_size; 1361f46b5a66SChristoph Hellwig u64 old_size; 1362f46b5a66SChristoph Hellwig u64 devid = 1; 1363496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 1364f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1365f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1366f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1367f46b5a66SChristoph Hellwig char *sizestr; 1368f46b5a66SChristoph Hellwig char *devstr = NULL; 1369f46b5a66SChristoph Hellwig int ret = 0; 1370f46b5a66SChristoph Hellwig int mod = 0; 1371f46b5a66SChristoph Hellwig 1372e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1373e441d54dSChris Mason return -EPERM; 1374e441d54dSChris Mason 1375198605a8SMiao Xie ret = mnt_want_write_file(file); 1376198605a8SMiao Xie if (ret) 1377198605a8SMiao Xie return ret; 1378198605a8SMiao Xie 13795ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 13805ac00addSStefan Behrens 1)) { 13815ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 138297547676SMiao Xie mnt_drop_write_file(file); 13832c0c9da0SIlya Dryomov return -EINVAL; 1384c9e9f97bSIlya Dryomov } 1385c9e9f97bSIlya Dryomov 13865ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1387dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1388c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1389c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1390c9e9f97bSIlya Dryomov goto out; 1391c9e9f97bSIlya Dryomov } 13925516e595SMark Fasheh 13935516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1394f46b5a66SChristoph Hellwig 1395f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1396f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1397f46b5a66SChristoph Hellwig if (devstr) { 1398f46b5a66SChristoph Hellwig char *end; 1399f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1400f46b5a66SChristoph Hellwig *devstr = '\0'; 1401f46b5a66SChristoph Hellwig devstr = vol_args->name; 1402f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 1403dfd79829SMiao Xie if (!devid) { 1404dfd79829SMiao Xie ret = -EINVAL; 1405dfd79829SMiao Xie goto out_free; 1406dfd79829SMiao Xie } 14075bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizing devid %llu\n", 140821380931SJoel Becker (unsigned long long)devid); 1409f46b5a66SChristoph Hellwig } 1410dba60f3fSMiao Xie 1411aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1412f46b5a66SChristoph Hellwig if (!device) { 14135bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 141421380931SJoel Becker (unsigned long long)devid); 1415dfd79829SMiao Xie ret = -ENODEV; 1416c9e9f97bSIlya Dryomov goto out_free; 1417f46b5a66SChristoph Hellwig } 1418dba60f3fSMiao Xie 1419dba60f3fSMiao Xie if (!device->writeable) { 14204e42ae1bSLiu Bo printk(KERN_INFO "btrfs: resizer unable to apply on " 1421dba60f3fSMiao Xie "readonly device %llu\n", 1422a8c4a33bSChris Mason (unsigned long long)devid); 1423dfd79829SMiao Xie ret = -EPERM; 14244e42ae1bSLiu Bo goto out_free; 14254e42ae1bSLiu Bo } 14264e42ae1bSLiu Bo 1427f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1428f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1429f46b5a66SChristoph Hellwig else { 1430f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1431f46b5a66SChristoph Hellwig mod = -1; 1432f46b5a66SChristoph Hellwig sizestr++; 1433f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1434f46b5a66SChristoph Hellwig mod = 1; 1435f46b5a66SChristoph Hellwig sizestr++; 1436f46b5a66SChristoph Hellwig } 143791748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1438f46b5a66SChristoph Hellwig if (new_size == 0) { 1439f46b5a66SChristoph Hellwig ret = -EINVAL; 1440c9e9f97bSIlya Dryomov goto out_free; 1441f46b5a66SChristoph Hellwig } 1442f46b5a66SChristoph Hellwig } 1443f46b5a66SChristoph Hellwig 144463a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1445dfd79829SMiao Xie ret = -EPERM; 144663a212abSStefan Behrens goto out_free; 144763a212abSStefan Behrens } 144863a212abSStefan Behrens 1449f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1450f46b5a66SChristoph Hellwig 1451f46b5a66SChristoph Hellwig if (mod < 0) { 1452f46b5a66SChristoph Hellwig if (new_size > old_size) { 1453f46b5a66SChristoph Hellwig ret = -EINVAL; 1454c9e9f97bSIlya Dryomov goto out_free; 1455f46b5a66SChristoph Hellwig } 1456f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1457f46b5a66SChristoph Hellwig } else if (mod > 0) { 1458f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1459f46b5a66SChristoph Hellwig } 1460f46b5a66SChristoph Hellwig 1461f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1462f46b5a66SChristoph Hellwig ret = -EINVAL; 1463c9e9f97bSIlya Dryomov goto out_free; 1464f46b5a66SChristoph Hellwig } 1465f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1466f46b5a66SChristoph Hellwig ret = -EFBIG; 1467c9e9f97bSIlya Dryomov goto out_free; 1468f46b5a66SChristoph Hellwig } 1469f46b5a66SChristoph Hellwig 1470f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1471f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1472f46b5a66SChristoph Hellwig 1473606686eeSJosef Bacik printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", 1474606686eeSJosef Bacik rcu_str_deref(device->name), 1475606686eeSJosef Bacik (unsigned long long)new_size); 1476f46b5a66SChristoph Hellwig 1477f46b5a66SChristoph Hellwig if (new_size > old_size) { 1478a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 147998d5dc13STsutomu Itoh if (IS_ERR(trans)) { 148098d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1481c9e9f97bSIlya Dryomov goto out_free; 148298d5dc13STsutomu Itoh } 1483f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1484f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1485ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1486f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 14870253f40eSjeff.liu } /* equal, nothing need to do */ 1488f46b5a66SChristoph Hellwig 1489c9e9f97bSIlya Dryomov out_free: 1490f46b5a66SChristoph Hellwig kfree(vol_args); 1491c9e9f97bSIlya Dryomov out: 1492c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 14935ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 149418f39c41SIlya Dryomov mnt_drop_write_file(file); 1495f46b5a66SChristoph Hellwig return ret; 1496f46b5a66SChristoph Hellwig } 1497f46b5a66SChristoph Hellwig 149872fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 14996f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 15006f72c7e2SArne Jansen u64 *transid, bool readonly, 15018696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1502f46b5a66SChristoph Hellwig { 1503f46b5a66SChristoph Hellwig int namelen; 15043de4586cSChris Mason int ret = 0; 1505f46b5a66SChristoph Hellwig 1506a874a63eSLiu Bo ret = mnt_want_write_file(file); 1507a874a63eSLiu Bo if (ret) 1508a874a63eSLiu Bo goto out; 1509a874a63eSLiu Bo 151072fd032eSSage Weil namelen = strlen(name); 151172fd032eSSage Weil if (strchr(name, '/')) { 1512f46b5a66SChristoph Hellwig ret = -EINVAL; 1513a874a63eSLiu Bo goto out_drop_write; 1514f46b5a66SChristoph Hellwig } 1515f46b5a66SChristoph Hellwig 151616780cabSChris Mason if (name[0] == '.' && 151716780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 151816780cabSChris Mason ret = -EEXIST; 1519a874a63eSLiu Bo goto out_drop_write; 152016780cabSChris Mason } 152116780cabSChris Mason 15223de4586cSChris Mason if (subvol) { 152372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 15246f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1525cb8e7090SChristoph Hellwig } else { 15262903ff01SAl Viro struct fd src = fdget(fd); 15273de4586cSChris Mason struct inode *src_inode; 15282903ff01SAl Viro if (!src.file) { 15293de4586cSChris Mason ret = -EINVAL; 1530a874a63eSLiu Bo goto out_drop_write; 15313de4586cSChris Mason } 15323de4586cSChris Mason 1533496ad9aaSAl Viro src_inode = file_inode(src.file); 1534496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1535d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1536d397712bSChris Mason "another FS\n"); 15373de4586cSChris Mason ret = -EINVAL; 1538ecd18815SAl Viro } else { 153972fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 154072fd032eSSage Weil BTRFS_I(src_inode)->root, 15416f72c7e2SArne Jansen transid, readonly, inherit); 1542ecd18815SAl Viro } 15432903ff01SAl Viro fdput(src); 1544cb8e7090SChristoph Hellwig } 1545a874a63eSLiu Bo out_drop_write: 1546a874a63eSLiu Bo mnt_drop_write_file(file); 1547f46b5a66SChristoph Hellwig out: 154872fd032eSSage Weil return ret; 154972fd032eSSage Weil } 155072fd032eSSage Weil 155172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1552fa0d2b9bSLi Zefan void __user *arg, int subvol) 155372fd032eSSage Weil { 1554fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 155572fd032eSSage Weil int ret; 155672fd032eSSage Weil 1557fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1558fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1559fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1560fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1561fa0d2b9bSLi Zefan 1562fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1563b83cc969SLi Zefan vol_args->fd, subvol, 15646f72c7e2SArne Jansen NULL, false, NULL); 1565fa0d2b9bSLi Zefan 1566fa0d2b9bSLi Zefan kfree(vol_args); 1567fa0d2b9bSLi Zefan return ret; 1568fa0d2b9bSLi Zefan } 1569fa0d2b9bSLi Zefan 1570fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1571fa0d2b9bSLi Zefan void __user *arg, int subvol) 1572fa0d2b9bSLi Zefan { 1573fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1574fa0d2b9bSLi Zefan int ret; 1575fdfb1e4fSLi Zefan u64 transid = 0; 1576fdfb1e4fSLi Zefan u64 *ptr = NULL; 1577b83cc969SLi Zefan bool readonly = false; 15786f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 157972fd032eSSage Weil 1580fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1581fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1582fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1583fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1584fdfb1e4fSLi Zefan 1585b83cc969SLi Zefan if (vol_args->flags & 15866f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 15876f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1588b83cc969SLi Zefan ret = -EOPNOTSUPP; 1589fdfb1e4fSLi Zefan goto out; 1590fdfb1e4fSLi Zefan } 1591fdfb1e4fSLi Zefan 1592fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1593fdfb1e4fSLi Zefan ptr = &transid; 1594b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1595b83cc969SLi Zefan readonly = true; 15966f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 15976f72c7e2SArne Jansen if (vol_args->size > PAGE_CACHE_SIZE) { 15986f72c7e2SArne Jansen ret = -EINVAL; 15996f72c7e2SArne Jansen goto out; 16006f72c7e2SArne Jansen } 16016f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 16026f72c7e2SArne Jansen if (IS_ERR(inherit)) { 16036f72c7e2SArne Jansen ret = PTR_ERR(inherit); 16046f72c7e2SArne Jansen goto out; 16056f72c7e2SArne Jansen } 16066f72c7e2SArne Jansen } 160775eaa0e2SSage Weil 1608fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 16096f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 16108696c533SMiao Xie readonly, inherit); 161175eaa0e2SSage Weil 1612fdfb1e4fSLi Zefan if (ret == 0 && ptr && 161375eaa0e2SSage Weil copy_to_user(arg + 1614fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1615fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 161675eaa0e2SSage Weil ret = -EFAULT; 1617fdfb1e4fSLi Zefan out: 1618f46b5a66SChristoph Hellwig kfree(vol_args); 16196f72c7e2SArne Jansen kfree(inherit); 1620f46b5a66SChristoph Hellwig return ret; 1621f46b5a66SChristoph Hellwig } 1622f46b5a66SChristoph Hellwig 16230caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 16240caa102dSLi Zefan void __user *arg) 16250caa102dSLi Zefan { 1626496ad9aaSAl Viro struct inode *inode = file_inode(file); 16270caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 16280caa102dSLi Zefan int ret = 0; 16290caa102dSLi Zefan u64 flags = 0; 16300caa102dSLi Zefan 163133345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 16320caa102dSLi Zefan return -EINVAL; 16330caa102dSLi Zefan 16340caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 16350caa102dSLi Zefan if (btrfs_root_readonly(root)) 16360caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 16370caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 16380caa102dSLi Zefan 16390caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 16400caa102dSLi Zefan ret = -EFAULT; 16410caa102dSLi Zefan 16420caa102dSLi Zefan return ret; 16430caa102dSLi Zefan } 16440caa102dSLi Zefan 16450caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 16460caa102dSLi Zefan void __user *arg) 16470caa102dSLi Zefan { 1648496ad9aaSAl Viro struct inode *inode = file_inode(file); 16490caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 16500caa102dSLi Zefan struct btrfs_trans_handle *trans; 16510caa102dSLi Zefan u64 root_flags; 16520caa102dSLi Zefan u64 flags; 16530caa102dSLi Zefan int ret = 0; 16540caa102dSLi Zefan 1655b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1656b9ca0664SLiu Bo if (ret) 1657b9ca0664SLiu Bo goto out; 16580caa102dSLi Zefan 1659b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1660b9ca0664SLiu Bo ret = -EINVAL; 1661b9ca0664SLiu Bo goto out_drop_write; 1662b9ca0664SLiu Bo } 16630caa102dSLi Zefan 1664b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1665b9ca0664SLiu Bo ret = -EFAULT; 1666b9ca0664SLiu Bo goto out_drop_write; 1667b9ca0664SLiu Bo } 16680caa102dSLi Zefan 1669b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1670b9ca0664SLiu Bo ret = -EINVAL; 1671b9ca0664SLiu Bo goto out_drop_write; 1672b9ca0664SLiu Bo } 16730caa102dSLi Zefan 1674b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1675b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1676b9ca0664SLiu Bo goto out_drop_write; 1677b9ca0664SLiu Bo } 16780caa102dSLi Zefan 1679b9ca0664SLiu Bo if (!inode_owner_or_capable(inode)) { 1680b9ca0664SLiu Bo ret = -EACCES; 1681b9ca0664SLiu Bo goto out_drop_write; 1682b9ca0664SLiu Bo } 1683b4dc2b8cSLi Zefan 16840caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 16850caa102dSLi Zefan 16860caa102dSLi Zefan /* nothing to do */ 16870caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1688b9ca0664SLiu Bo goto out_drop_sem; 16890caa102dSLi Zefan 16900caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 16910caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 16920caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16930caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 16940caa102dSLi Zefan else 16950caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16960caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 16970caa102dSLi Zefan 16980caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 16990caa102dSLi Zefan if (IS_ERR(trans)) { 17000caa102dSLi Zefan ret = PTR_ERR(trans); 17010caa102dSLi Zefan goto out_reset; 17020caa102dSLi Zefan } 17030caa102dSLi Zefan 1704b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 17050caa102dSLi Zefan &root->root_key, &root->root_item); 17060caa102dSLi Zefan 17070caa102dSLi Zefan btrfs_commit_transaction(trans, root); 17080caa102dSLi Zefan out_reset: 17090caa102dSLi Zefan if (ret) 17100caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1711b9ca0664SLiu Bo out_drop_sem: 17120caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1713b9ca0664SLiu Bo out_drop_write: 1714b9ca0664SLiu Bo mnt_drop_write_file(file); 1715b9ca0664SLiu Bo out: 17160caa102dSLi Zefan return ret; 17170caa102dSLi Zefan } 17180caa102dSLi Zefan 171976dda93cSYan, Zheng /* 172076dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 172176dda93cSYan, Zheng */ 172276dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 172376dda93cSYan, Zheng { 172476dda93cSYan, Zheng struct btrfs_path *path; 172576dda93cSYan, Zheng struct btrfs_key key; 172676dda93cSYan, Zheng int ret; 172776dda93cSYan, Zheng 172876dda93cSYan, Zheng path = btrfs_alloc_path(); 172976dda93cSYan, Zheng if (!path) 173076dda93cSYan, Zheng return -ENOMEM; 173176dda93cSYan, Zheng 173276dda93cSYan, Zheng key.objectid = root->root_key.objectid; 173376dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 173476dda93cSYan, Zheng key.offset = (u64)-1; 173576dda93cSYan, Zheng 173676dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 173776dda93cSYan, Zheng &key, path, 0, 0); 173876dda93cSYan, Zheng if (ret < 0) 173976dda93cSYan, Zheng goto out; 174076dda93cSYan, Zheng BUG_ON(ret == 0); 174176dda93cSYan, Zheng 174276dda93cSYan, Zheng ret = 0; 174376dda93cSYan, Zheng if (path->slots[0] > 0) { 174476dda93cSYan, Zheng path->slots[0]--; 174576dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 174676dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 174776dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 174876dda93cSYan, Zheng ret = -ENOTEMPTY; 174976dda93cSYan, Zheng } 175076dda93cSYan, Zheng out: 175176dda93cSYan, Zheng btrfs_free_path(path); 175276dda93cSYan, Zheng return ret; 175376dda93cSYan, Zheng } 175476dda93cSYan, Zheng 1755ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1756ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1757ac8e9819SChris Mason { 1758abc6e134SChris Mason struct btrfs_key test; 1759abc6e134SChris Mason int ret; 1760abc6e134SChris Mason 1761abc6e134SChris Mason test.objectid = sk->min_objectid; 1762abc6e134SChris Mason test.type = sk->min_type; 1763abc6e134SChris Mason test.offset = sk->min_offset; 1764abc6e134SChris Mason 1765abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1766abc6e134SChris Mason if (ret < 0) 1767ac8e9819SChris Mason return 0; 1768abc6e134SChris Mason 1769abc6e134SChris Mason test.objectid = sk->max_objectid; 1770abc6e134SChris Mason test.type = sk->max_type; 1771abc6e134SChris Mason test.offset = sk->max_offset; 1772abc6e134SChris Mason 1773abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1774abc6e134SChris Mason if (ret > 0) 1775ac8e9819SChris Mason return 0; 1776ac8e9819SChris Mason return 1; 1777ac8e9819SChris Mason } 1778ac8e9819SChris Mason 1779ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1780ac8e9819SChris Mason struct btrfs_path *path, 1781ac8e9819SChris Mason struct btrfs_key *key, 1782ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1783ac8e9819SChris Mason char *buf, 1784ac8e9819SChris Mason unsigned long *sk_offset, 1785ac8e9819SChris Mason int *num_found) 1786ac8e9819SChris Mason { 1787ac8e9819SChris Mason u64 found_transid; 1788ac8e9819SChris Mason struct extent_buffer *leaf; 1789ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1790ac8e9819SChris Mason unsigned long item_off; 1791ac8e9819SChris Mason unsigned long item_len; 1792ac8e9819SChris Mason int nritems; 1793ac8e9819SChris Mason int i; 1794ac8e9819SChris Mason int slot; 1795ac8e9819SChris Mason int ret = 0; 1796ac8e9819SChris Mason 1797ac8e9819SChris Mason leaf = path->nodes[0]; 1798ac8e9819SChris Mason slot = path->slots[0]; 1799ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1800ac8e9819SChris Mason 1801ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1802ac8e9819SChris Mason i = nritems; 1803ac8e9819SChris Mason goto advance_key; 1804ac8e9819SChris Mason } 1805ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1806ac8e9819SChris Mason 1807ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1808ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1809ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1810ac8e9819SChris Mason 181103b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 181203b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 181303b71c6cSGabriel de Perthuis continue; 181403b71c6cSGabriel de Perthuis 181503b71c6cSGabriel de Perthuis if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1816ac8e9819SChris Mason item_len = 0; 1817ac8e9819SChris Mason 1818ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1819ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1820ac8e9819SChris Mason ret = 1; 1821ac8e9819SChris Mason goto overflow; 1822ac8e9819SChris Mason } 1823ac8e9819SChris Mason 1824ac8e9819SChris Mason sh.objectid = key->objectid; 1825ac8e9819SChris Mason sh.offset = key->offset; 1826ac8e9819SChris Mason sh.type = key->type; 1827ac8e9819SChris Mason sh.len = item_len; 1828ac8e9819SChris Mason sh.transid = found_transid; 1829ac8e9819SChris Mason 1830ac8e9819SChris Mason /* copy search result header */ 1831ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1832ac8e9819SChris Mason *sk_offset += sizeof(sh); 1833ac8e9819SChris Mason 1834ac8e9819SChris Mason if (item_len) { 1835ac8e9819SChris Mason char *p = buf + *sk_offset; 1836ac8e9819SChris Mason /* copy the item */ 1837ac8e9819SChris Mason read_extent_buffer(leaf, p, 1838ac8e9819SChris Mason item_off, item_len); 1839ac8e9819SChris Mason *sk_offset += item_len; 1840ac8e9819SChris Mason } 1841e2156867SHugo Mills (*num_found)++; 1842ac8e9819SChris Mason 1843ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1844ac8e9819SChris Mason break; 1845ac8e9819SChris Mason } 1846ac8e9819SChris Mason advance_key: 1847ac8e9819SChris Mason ret = 0; 1848abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1849abc6e134SChris Mason key->offset++; 1850abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1851abc6e134SChris Mason key->offset = 0; 1852abc6e134SChris Mason key->type++; 1853abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1854abc6e134SChris Mason key->offset = 0; 1855abc6e134SChris Mason key->type = 0; 1856abc6e134SChris Mason key->objectid++; 1857abc6e134SChris Mason } else 1858abc6e134SChris Mason ret = 1; 1859ac8e9819SChris Mason overflow: 1860ac8e9819SChris Mason return ret; 1861ac8e9819SChris Mason } 1862ac8e9819SChris Mason 1863ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1864ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1865ac8e9819SChris Mason { 1866ac8e9819SChris Mason struct btrfs_root *root; 1867ac8e9819SChris Mason struct btrfs_key key; 1868ac8e9819SChris Mason struct btrfs_key max_key; 1869ac8e9819SChris Mason struct btrfs_path *path; 1870ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1871ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1872ac8e9819SChris Mason int ret; 1873ac8e9819SChris Mason int num_found = 0; 1874ac8e9819SChris Mason unsigned long sk_offset = 0; 1875ac8e9819SChris Mason 1876ac8e9819SChris Mason path = btrfs_alloc_path(); 1877ac8e9819SChris Mason if (!path) 1878ac8e9819SChris Mason return -ENOMEM; 1879ac8e9819SChris Mason 1880ac8e9819SChris Mason if (sk->tree_id == 0) { 1881ac8e9819SChris Mason /* search the root of the inode that was passed */ 1882ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1883ac8e9819SChris Mason } else { 1884ac8e9819SChris Mason key.objectid = sk->tree_id; 1885ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1886ac8e9819SChris Mason key.offset = (u64)-1; 1887ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1888ac8e9819SChris Mason if (IS_ERR(root)) { 1889ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1890ac8e9819SChris Mason sk->tree_id); 1891ac8e9819SChris Mason btrfs_free_path(path); 1892ac8e9819SChris Mason return -ENOENT; 1893ac8e9819SChris Mason } 1894ac8e9819SChris Mason } 1895ac8e9819SChris Mason 1896ac8e9819SChris Mason key.objectid = sk->min_objectid; 1897ac8e9819SChris Mason key.type = sk->min_type; 1898ac8e9819SChris Mason key.offset = sk->min_offset; 1899ac8e9819SChris Mason 1900ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1901ac8e9819SChris Mason max_key.type = sk->max_type; 1902ac8e9819SChris Mason max_key.offset = sk->max_offset; 1903ac8e9819SChris Mason 1904ac8e9819SChris Mason path->keep_locks = 1; 1905ac8e9819SChris Mason 1906ac8e9819SChris Mason while(1) { 1907de78b51aSEric Sandeen ret = btrfs_search_forward(root, &key, &max_key, path, 1908ac8e9819SChris Mason sk->min_transid); 1909ac8e9819SChris Mason if (ret != 0) { 1910ac8e9819SChris Mason if (ret > 0) 1911ac8e9819SChris Mason ret = 0; 1912ac8e9819SChris Mason goto err; 1913ac8e9819SChris Mason } 1914ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1915ac8e9819SChris Mason &sk_offset, &num_found); 1916b3b4aa74SDavid Sterba btrfs_release_path(path); 1917ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1918ac8e9819SChris Mason break; 1919ac8e9819SChris Mason 1920ac8e9819SChris Mason } 1921ac8e9819SChris Mason ret = 0; 1922ac8e9819SChris Mason err: 1923ac8e9819SChris Mason sk->nr_items = num_found; 1924ac8e9819SChris Mason btrfs_free_path(path); 1925ac8e9819SChris Mason return ret; 1926ac8e9819SChris Mason } 1927ac8e9819SChris Mason 1928ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1929ac8e9819SChris Mason void __user *argp) 1930ac8e9819SChris Mason { 1931ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1932ac8e9819SChris Mason struct inode *inode; 1933ac8e9819SChris Mason int ret; 1934ac8e9819SChris Mason 1935ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1936ac8e9819SChris Mason return -EPERM; 1937ac8e9819SChris Mason 19382354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 19392354d08fSJulia Lawall if (IS_ERR(args)) 19402354d08fSJulia Lawall return PTR_ERR(args); 1941ac8e9819SChris Mason 1942496ad9aaSAl Viro inode = file_inode(file); 1943ac8e9819SChris Mason ret = search_ioctl(inode, args); 1944ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1945ac8e9819SChris Mason ret = -EFAULT; 1946ac8e9819SChris Mason kfree(args); 1947ac8e9819SChris Mason return ret; 1948ac8e9819SChris Mason } 1949ac8e9819SChris Mason 195098d377a0STARUISI Hiroaki /* 1951ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1952ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 195398d377a0STARUISI Hiroaki */ 195498d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 195598d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 195698d377a0STARUISI Hiroaki { 195798d377a0STARUISI Hiroaki struct btrfs_root *root; 195898d377a0STARUISI Hiroaki struct btrfs_key key; 1959ac8e9819SChris Mason char *ptr; 196098d377a0STARUISI Hiroaki int ret = -1; 196198d377a0STARUISI Hiroaki int slot; 196298d377a0STARUISI Hiroaki int len; 196398d377a0STARUISI Hiroaki int total_len = 0; 196498d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 196598d377a0STARUISI Hiroaki struct extent_buffer *l; 196698d377a0STARUISI Hiroaki struct btrfs_path *path; 196798d377a0STARUISI Hiroaki 196898d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 196998d377a0STARUISI Hiroaki name[0]='\0'; 197098d377a0STARUISI Hiroaki return 0; 197198d377a0STARUISI Hiroaki } 197298d377a0STARUISI Hiroaki 197398d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 197498d377a0STARUISI Hiroaki if (!path) 197598d377a0STARUISI Hiroaki return -ENOMEM; 197698d377a0STARUISI Hiroaki 1977ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 197898d377a0STARUISI Hiroaki 197998d377a0STARUISI Hiroaki key.objectid = tree_id; 198098d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 198198d377a0STARUISI Hiroaki key.offset = (u64)-1; 198298d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 198398d377a0STARUISI Hiroaki if (IS_ERR(root)) { 198498d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 19858ad6fcabSChris Mason ret = -ENOENT; 19868ad6fcabSChris Mason goto out; 198798d377a0STARUISI Hiroaki } 198898d377a0STARUISI Hiroaki 198998d377a0STARUISI Hiroaki key.objectid = dirid; 199098d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 19918ad6fcabSChris Mason key.offset = (u64)-1; 199298d377a0STARUISI Hiroaki 199398d377a0STARUISI Hiroaki while(1) { 199498d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 199598d377a0STARUISI Hiroaki if (ret < 0) 199698d377a0STARUISI Hiroaki goto out; 199798d377a0STARUISI Hiroaki 199898d377a0STARUISI Hiroaki l = path->nodes[0]; 199998d377a0STARUISI Hiroaki slot = path->slots[0]; 20008ad6fcabSChris Mason if (ret > 0 && slot > 0) 20018ad6fcabSChris Mason slot--; 200298d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 200398d377a0STARUISI Hiroaki 200498d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 2005ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 2006ac8e9819SChris Mason ret = -ENOENT; 200798d377a0STARUISI Hiroaki goto out; 2008ac8e9819SChris Mason } 200998d377a0STARUISI Hiroaki 201098d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 201198d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 201298d377a0STARUISI Hiroaki ptr -= len + 1; 201398d377a0STARUISI Hiroaki total_len += len + 1; 2014ac8e9819SChris Mason if (ptr < name) 201598d377a0STARUISI Hiroaki goto out; 201698d377a0STARUISI Hiroaki 201798d377a0STARUISI Hiroaki *(ptr + len) = '/'; 201898d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 201998d377a0STARUISI Hiroaki 202098d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 202198d377a0STARUISI Hiroaki break; 202298d377a0STARUISI Hiroaki 2023b3b4aa74SDavid Sterba btrfs_release_path(path); 202498d377a0STARUISI Hiroaki key.objectid = key.offset; 20258ad6fcabSChris Mason key.offset = (u64)-1; 202698d377a0STARUISI Hiroaki dirid = key.objectid; 202798d377a0STARUISI Hiroaki } 2028ac8e9819SChris Mason if (ptr < name) 202998d377a0STARUISI Hiroaki goto out; 203077906a50SLi Zefan memmove(name, ptr, total_len); 203198d377a0STARUISI Hiroaki name[total_len]='\0'; 203298d377a0STARUISI Hiroaki ret = 0; 203398d377a0STARUISI Hiroaki out: 203498d377a0STARUISI Hiroaki btrfs_free_path(path); 2035ac8e9819SChris Mason return ret; 2036ac8e9819SChris Mason } 2037ac8e9819SChris Mason 2038ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2039ac8e9819SChris Mason void __user *argp) 2040ac8e9819SChris Mason { 2041ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2042ac8e9819SChris Mason struct inode *inode; 2043ac8e9819SChris Mason int ret; 2044ac8e9819SChris Mason 2045ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2046ac8e9819SChris Mason return -EPERM; 2047ac8e9819SChris Mason 20482354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 20492354d08fSJulia Lawall if (IS_ERR(args)) 20502354d08fSJulia Lawall return PTR_ERR(args); 2051c2b96929SDan Carpenter 2052496ad9aaSAl Viro inode = file_inode(file); 2053ac8e9819SChris Mason 20541b53ac4dSChris Mason if (args->treeid == 0) 20551b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 20561b53ac4dSChris Mason 2057ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2058ac8e9819SChris Mason args->treeid, args->objectid, 2059ac8e9819SChris Mason args->name); 2060ac8e9819SChris Mason 2061ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2062ac8e9819SChris Mason ret = -EFAULT; 2063ac8e9819SChris Mason 2064ac8e9819SChris Mason kfree(args); 206598d377a0STARUISI Hiroaki return ret; 206698d377a0STARUISI Hiroaki } 206798d377a0STARUISI Hiroaki 206876dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 206976dda93cSYan, Zheng void __user *arg) 207076dda93cSYan, Zheng { 207176dda93cSYan, Zheng struct dentry *parent = fdentry(file); 207276dda93cSYan, Zheng struct dentry *dentry; 207376dda93cSYan, Zheng struct inode *dir = parent->d_inode; 207476dda93cSYan, Zheng struct inode *inode; 207576dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 207676dda93cSYan, Zheng struct btrfs_root *dest = NULL; 207776dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 207876dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2079c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2080c58aaad2SMiao Xie u64 qgroup_reserved; 208176dda93cSYan, Zheng int namelen; 208276dda93cSYan, Zheng int ret; 208376dda93cSYan, Zheng int err = 0; 208476dda93cSYan, Zheng 208576dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 208676dda93cSYan, Zheng if (IS_ERR(vol_args)) 208776dda93cSYan, Zheng return PTR_ERR(vol_args); 208876dda93cSYan, Zheng 208976dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 209076dda93cSYan, Zheng namelen = strlen(vol_args->name); 209176dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 209276dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 209376dda93cSYan, Zheng err = -EINVAL; 209476dda93cSYan, Zheng goto out; 209576dda93cSYan, Zheng } 209676dda93cSYan, Zheng 2097a561be71SAl Viro err = mnt_want_write_file(file); 209876dda93cSYan, Zheng if (err) 209976dda93cSYan, Zheng goto out; 210076dda93cSYan, Zheng 21015c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 21025c50c9b8SDavid Sterba if (err == -EINTR) 21035c50c9b8SDavid Sterba goto out; 210476dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 210576dda93cSYan, Zheng if (IS_ERR(dentry)) { 210676dda93cSYan, Zheng err = PTR_ERR(dentry); 210776dda93cSYan, Zheng goto out_unlock_dir; 210876dda93cSYan, Zheng } 210976dda93cSYan, Zheng 211076dda93cSYan, Zheng if (!dentry->d_inode) { 211176dda93cSYan, Zheng err = -ENOENT; 211276dda93cSYan, Zheng goto out_dput; 211376dda93cSYan, Zheng } 211476dda93cSYan, Zheng 211576dda93cSYan, Zheng inode = dentry->d_inode; 21164260f7c7SSage Weil dest = BTRFS_I(inode)->root; 21174260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 21184260f7c7SSage Weil /* 21194260f7c7SSage Weil * Regular user. Only allow this with a special mount 21204260f7c7SSage Weil * option, when the user has write+exec access to the 21214260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 21224260f7c7SSage Weil * allowed. 21234260f7c7SSage Weil * 21244260f7c7SSage Weil * Note that this is _not_ check that the subvol is 21254260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 21264260f7c7SSage Weil * otherwise be able to delete. 21274260f7c7SSage Weil * 21284260f7c7SSage Weil * Users who want to delete empty subvols should try 21294260f7c7SSage Weil * rmdir(2). 21304260f7c7SSage Weil */ 21314260f7c7SSage Weil err = -EPERM; 21324260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 21334260f7c7SSage Weil goto out_dput; 21344260f7c7SSage Weil 21354260f7c7SSage Weil /* 21364260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 21374260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 21384260f7c7SSage Weil * must be called on the dentry referencing the root 21394260f7c7SSage Weil * of the subvol, not a random directory contained 21404260f7c7SSage Weil * within it. 21414260f7c7SSage Weil */ 21424260f7c7SSage Weil err = -EINVAL; 21434260f7c7SSage Weil if (root == dest) 21444260f7c7SSage Weil goto out_dput; 21454260f7c7SSage Weil 21464260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 21474260f7c7SSage Weil if (err) 21484260f7c7SSage Weil goto out_dput; 21495c39da5bSMiao Xie } 21504260f7c7SSage Weil 21515c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 21524260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 21534260f7c7SSage Weil if (err) 21544260f7c7SSage Weil goto out_dput; 21554260f7c7SSage Weil 215633345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 215776dda93cSYan, Zheng err = -EINVAL; 215876dda93cSYan, Zheng goto out_dput; 215976dda93cSYan, Zheng } 216076dda93cSYan, Zheng 216176dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 216276dda93cSYan, Zheng err = d_invalidate(dentry); 216376dda93cSYan, Zheng if (err) 216476dda93cSYan, Zheng goto out_unlock; 216576dda93cSYan, Zheng 216676dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 216776dda93cSYan, Zheng 216876dda93cSYan, Zheng err = may_destroy_subvol(dest); 216976dda93cSYan, Zheng if (err) 217076dda93cSYan, Zheng goto out_up_write; 217176dda93cSYan, Zheng 2172c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2173c58aaad2SMiao Xie /* 2174c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2175c58aaad2SMiao Xie * ref/backref. 2176c58aaad2SMiao Xie */ 2177c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2178ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2179c58aaad2SMiao Xie if (err) 2180c58aaad2SMiao Xie goto out_up_write; 2181c58aaad2SMiao Xie 2182a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2183a22285a6SYan, Zheng if (IS_ERR(trans)) { 2184a22285a6SYan, Zheng err = PTR_ERR(trans); 2185c58aaad2SMiao Xie goto out_release; 2186a22285a6SYan, Zheng } 2187c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2188c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2189a22285a6SYan, Zheng 219076dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 219176dda93cSYan, Zheng dest->root_key.objectid, 219276dda93cSYan, Zheng dentry->d_name.name, 219376dda93cSYan, Zheng dentry->d_name.len); 219479787eaaSJeff Mahoney if (ret) { 219579787eaaSJeff Mahoney err = ret; 219679787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 219779787eaaSJeff Mahoney goto out_end_trans; 219879787eaaSJeff Mahoney } 219976dda93cSYan, Zheng 220076dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 220176dda93cSYan, Zheng 220276dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 220376dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 220476dda93cSYan, Zheng dest->root_item.drop_level = 0; 220576dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 220676dda93cSYan, Zheng 2207d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 220876dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 220976dda93cSYan, Zheng root->fs_info->tree_root, 221076dda93cSYan, Zheng dest->root_key.objectid); 221179787eaaSJeff Mahoney if (ret) { 221279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 221379787eaaSJeff Mahoney err = ret; 221479787eaaSJeff Mahoney goto out_end_trans; 2215d68fc57bSYan, Zheng } 221679787eaaSJeff Mahoney } 221779787eaaSJeff Mahoney out_end_trans: 2218c58aaad2SMiao Xie trans->block_rsv = NULL; 2219c58aaad2SMiao Xie trans->bytes_reserved = 0; 2220531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 222179787eaaSJeff Mahoney if (ret && !err) 222279787eaaSJeff Mahoney err = ret; 222376dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2224c58aaad2SMiao Xie out_release: 2225c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 222676dda93cSYan, Zheng out_up_write: 222776dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 222876dda93cSYan, Zheng out_unlock: 222976dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 223076dda93cSYan, Zheng if (!err) { 2231efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 223276dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 223376dda93cSYan, Zheng d_delete(dentry); 2234fa6ac876SLiu Bo 2235fa6ac876SLiu Bo /* the last ref */ 2236fa6ac876SLiu Bo if (dest->cache_inode) { 2237fa6ac876SLiu Bo iput(dest->cache_inode); 2238fa6ac876SLiu Bo dest->cache_inode = NULL; 2239fa6ac876SLiu Bo } 224076dda93cSYan, Zheng } 224176dda93cSYan, Zheng out_dput: 224276dda93cSYan, Zheng dput(dentry); 224376dda93cSYan, Zheng out_unlock_dir: 224476dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 22452a79f17eSAl Viro mnt_drop_write_file(file); 224676dda93cSYan, Zheng out: 224776dda93cSYan, Zheng kfree(vol_args); 224876dda93cSYan, Zheng return err; 224976dda93cSYan, Zheng } 225076dda93cSYan, Zheng 22511e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2252f46b5a66SChristoph Hellwig { 2253496ad9aaSAl Viro struct inode *inode = file_inode(file); 2254f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 22551e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2256c146afadSYan Zheng int ret; 2257c146afadSYan Zheng 225825122d15SIlya Dryomov ret = mnt_want_write_file(file); 225925122d15SIlya Dryomov if (ret) 226025122d15SIlya Dryomov return ret; 2261b83cc969SLi Zefan 226225122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 226325122d15SIlya Dryomov ret = -EROFS; 226425122d15SIlya Dryomov goto out; 22655ac00addSStefan Behrens } 2266f46b5a66SChristoph Hellwig 2267f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2268f46b5a66SChristoph Hellwig case S_IFDIR: 2269e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2270e441d54dSChris Mason ret = -EPERM; 2271e441d54dSChris Mason goto out; 2272e441d54dSChris Mason } 2273de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 22748929ecfaSYan, Zheng if (ret) 22758929ecfaSYan, Zheng goto out; 2276de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2277f46b5a66SChristoph Hellwig break; 2278f46b5a66SChristoph Hellwig case S_IFREG: 2279e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2280e441d54dSChris Mason ret = -EINVAL; 2281e441d54dSChris Mason goto out; 2282e441d54dSChris Mason } 22831e701a32SChris Mason 22841e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 22851e701a32SChris Mason if (!range) { 22861e701a32SChris Mason ret = -ENOMEM; 22871e701a32SChris Mason goto out; 22881e701a32SChris Mason } 22891e701a32SChris Mason 22901e701a32SChris Mason if (argp) { 22911e701a32SChris Mason if (copy_from_user(range, argp, 22921e701a32SChris Mason sizeof(*range))) { 22931e701a32SChris Mason ret = -EFAULT; 22941e701a32SChris Mason kfree(range); 2295683be16eSDan Carpenter goto out; 22961e701a32SChris Mason } 22971e701a32SChris Mason /* compression requires us to start the IO */ 22981e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 22991e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 23001e701a32SChris Mason range->extent_thresh = (u32)-1; 23011e701a32SChris Mason } 23021e701a32SChris Mason } else { 23031e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 23041e701a32SChris Mason range->len = (u64)-1; 23051e701a32SChris Mason } 2306496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 23074cb5300bSChris Mason range, 0, 0); 23084cb5300bSChris Mason if (ret > 0) 23094cb5300bSChris Mason ret = 0; 23101e701a32SChris Mason kfree(range); 2311f46b5a66SChristoph Hellwig break; 23128929ecfaSYan, Zheng default: 23138929ecfaSYan, Zheng ret = -EINVAL; 2314f46b5a66SChristoph Hellwig } 2315e441d54dSChris Mason out: 231625122d15SIlya Dryomov mnt_drop_write_file(file); 2317e441d54dSChris Mason return ret; 2318f46b5a66SChristoph Hellwig } 2319f46b5a66SChristoph Hellwig 2320b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2321f46b5a66SChristoph Hellwig { 2322f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2323f46b5a66SChristoph Hellwig int ret; 2324f46b5a66SChristoph Hellwig 2325e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2326e441d54dSChris Mason return -EPERM; 2327e441d54dSChris Mason 23285ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 23295ac00addSStefan Behrens 1)) { 23305ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 23312c0c9da0SIlya Dryomov return -EINVAL; 2332c9e9f97bSIlya Dryomov } 2333c9e9f97bSIlya Dryomov 23345ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2335dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2336c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2337c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2338c9e9f97bSIlya Dryomov goto out; 2339c9e9f97bSIlya Dryomov } 2340f46b5a66SChristoph Hellwig 23415516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2342f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2343f46b5a66SChristoph Hellwig 2344f46b5a66SChristoph Hellwig kfree(vol_args); 2345c9e9f97bSIlya Dryomov out: 2346c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 23475ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2348f46b5a66SChristoph Hellwig return ret; 2349f46b5a66SChristoph Hellwig } 2350f46b5a66SChristoph Hellwig 2351da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2352f46b5a66SChristoph Hellwig { 2353496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2354f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2355f46b5a66SChristoph Hellwig int ret; 2356f46b5a66SChristoph Hellwig 2357e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2358e441d54dSChris Mason return -EPERM; 2359e441d54dSChris Mason 2360da24927bSMiao Xie ret = mnt_want_write_file(file); 2361da24927bSMiao Xie if (ret) 2362da24927bSMiao Xie return ret; 2363c146afadSYan Zheng 2364dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2365c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2366c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2367c9e9f97bSIlya Dryomov goto out; 2368c9e9f97bSIlya Dryomov } 2369f46b5a66SChristoph Hellwig 23705516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2371f46b5a66SChristoph Hellwig 2372183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2373183860f6SAnand Jain 1)) { 2374183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2375183860f6SAnand Jain goto out; 2376183860f6SAnand Jain } 2377183860f6SAnand Jain 2378183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2379183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2380c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 23815ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2382183860f6SAnand Jain 2383183860f6SAnand Jain out: 2384183860f6SAnand Jain kfree(vol_args); 23854ac20c70SIlya Dryomov mnt_drop_write_file(file); 2386f46b5a66SChristoph Hellwig return ret; 2387f46b5a66SChristoph Hellwig } 2388f46b5a66SChristoph Hellwig 2389475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2390475f6387SJan Schmidt { 2391027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2392475f6387SJan Schmidt struct btrfs_device *device; 2393475f6387SJan Schmidt struct btrfs_device *next; 2394475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2395027ed2f0SLi Zefan int ret = 0; 2396475f6387SJan Schmidt 2397475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2398475f6387SJan Schmidt return -EPERM; 2399475f6387SJan Schmidt 2400027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2401027ed2f0SLi Zefan if (!fi_args) 2402027ed2f0SLi Zefan return -ENOMEM; 2403027ed2f0SLi Zefan 2404027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2405027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2406475f6387SJan Schmidt 2407475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2408475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2409027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2410027ed2f0SLi Zefan fi_args->max_id = device->devid; 2411475f6387SJan Schmidt } 2412475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2413475f6387SJan Schmidt 2414027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2415027ed2f0SLi Zefan ret = -EFAULT; 2416475f6387SJan Schmidt 2417027ed2f0SLi Zefan kfree(fi_args); 2418027ed2f0SLi Zefan return ret; 2419475f6387SJan Schmidt } 2420475f6387SJan Schmidt 2421475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2422475f6387SJan Schmidt { 2423475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2424475f6387SJan Schmidt struct btrfs_device *dev; 2425475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2426475f6387SJan Schmidt int ret = 0; 2427475f6387SJan Schmidt char *s_uuid = NULL; 2428475f6387SJan Schmidt char empty_uuid[BTRFS_UUID_SIZE] = {0}; 2429475f6387SJan Schmidt 2430475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2431475f6387SJan Schmidt return -EPERM; 2432475f6387SJan Schmidt 2433475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2434475f6387SJan Schmidt if (IS_ERR(di_args)) 2435475f6387SJan Schmidt return PTR_ERR(di_args); 2436475f6387SJan Schmidt 2437475f6387SJan Schmidt if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) 2438475f6387SJan Schmidt s_uuid = di_args->uuid; 2439475f6387SJan Schmidt 2440475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2441aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2442475f6387SJan Schmidt 2443475f6387SJan Schmidt if (!dev) { 2444475f6387SJan Schmidt ret = -ENODEV; 2445475f6387SJan Schmidt goto out; 2446475f6387SJan Schmidt } 2447475f6387SJan Schmidt 2448475f6387SJan Schmidt di_args->devid = dev->devid; 2449475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2450475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2451475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2452a27202fbSJim Meyering if (dev->name) { 2453606686eeSJosef Bacik struct rcu_string *name; 2454606686eeSJosef Bacik 2455606686eeSJosef Bacik rcu_read_lock(); 2456606686eeSJosef Bacik name = rcu_dereference(dev->name); 2457606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2458606686eeSJosef Bacik rcu_read_unlock(); 2459a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2460a27202fbSJim Meyering } else { 246199ba55adSStefan Behrens di_args->path[0] = '\0'; 2462a27202fbSJim Meyering } 2463475f6387SJan Schmidt 2464475f6387SJan Schmidt out: 246555793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2466475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2467475f6387SJan Schmidt ret = -EFAULT; 2468475f6387SJan Schmidt 2469475f6387SJan Schmidt kfree(di_args); 2470475f6387SJan Schmidt return ret; 2471475f6387SJan Schmidt } 2472475f6387SJan Schmidt 247376dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 2474b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 2475f46b5a66SChristoph Hellwig { 2476496ad9aaSAl Viro struct inode *inode = file_inode(file); 2477f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 24782903ff01SAl Viro struct fd src_file; 2479f46b5a66SChristoph Hellwig struct inode *src; 2480f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2481f46b5a66SChristoph Hellwig struct btrfs_path *path; 2482f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 2483ae01a0abSYan Zheng char *buf; 2484ae01a0abSYan Zheng struct btrfs_key key; 2485f46b5a66SChristoph Hellwig u32 nritems; 2486f46b5a66SChristoph Hellwig int slot; 2487ae01a0abSYan Zheng int ret; 2488c5c9cd4dSSage Weil u64 len = olen; 2489c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 2490a96fbc72SLiu Bo int same_inode = 0; 2491d20f7043SChris Mason 2492c5c9cd4dSSage Weil /* 2493c5c9cd4dSSage Weil * TODO: 2494c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 2495c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 2496c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 2497c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 2498c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 2499c5c9cd4dSSage Weil * they don't overlap)? 2500c5c9cd4dSSage Weil */ 2501c5c9cd4dSSage Weil 2502e441d54dSChris Mason /* the destination must be opened for writing */ 25032ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 2504e441d54dSChris Mason return -EINVAL; 2505e441d54dSChris Mason 2506b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2507b83cc969SLi Zefan return -EROFS; 2508b83cc969SLi Zefan 2509a561be71SAl Viro ret = mnt_want_write_file(file); 2510c146afadSYan Zheng if (ret) 2511c146afadSYan Zheng return ret; 2512c146afadSYan Zheng 25132903ff01SAl Viro src_file = fdget(srcfd); 25142903ff01SAl Viro if (!src_file.file) { 2515ab67b7c1SYan Zheng ret = -EBADF; 2516ab67b7c1SYan Zheng goto out_drop_write; 2517ab67b7c1SYan Zheng } 25185dc64164SDan Rosenberg 2519362a20c5SDavid Sterba ret = -EXDEV; 25202903ff01SAl Viro if (src_file.file->f_path.mnt != file->f_path.mnt) 2521362a20c5SDavid Sterba goto out_fput; 2522362a20c5SDavid Sterba 2523496ad9aaSAl Viro src = file_inode(src_file.file); 2524f46b5a66SChristoph Hellwig 2525c5c9cd4dSSage Weil ret = -EINVAL; 2526c5c9cd4dSSage Weil if (src == inode) 2527a96fbc72SLiu Bo same_inode = 1; 2528c5c9cd4dSSage Weil 25295dc64164SDan Rosenberg /* the src must be open for reading */ 25302903ff01SAl Viro if (!(src_file.file->f_mode & FMODE_READ)) 25315dc64164SDan Rosenberg goto out_fput; 25325dc64164SDan Rosenberg 25330e7b824cSLi Zefan /* don't make the dst file partly checksummed */ 25340e7b824cSLi Zefan if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 25350e7b824cSLi Zefan (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 25360e7b824cSLi Zefan goto out_fput; 25370e7b824cSLi Zefan 2538ae01a0abSYan Zheng ret = -EISDIR; 2539ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2540f46b5a66SChristoph Hellwig goto out_fput; 2541f46b5a66SChristoph Hellwig 2542ae01a0abSYan Zheng ret = -EXDEV; 2543362a20c5SDavid Sterba if (src->i_sb != inode->i_sb) 2544ae01a0abSYan Zheng goto out_fput; 2545ae01a0abSYan Zheng 2546ae01a0abSYan Zheng ret = -ENOMEM; 2547ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2548ae01a0abSYan Zheng if (!buf) 2549ae01a0abSYan Zheng goto out_fput; 2550ae01a0abSYan Zheng 2551ae01a0abSYan Zheng path = btrfs_alloc_path(); 2552ae01a0abSYan Zheng if (!path) { 2553ae01a0abSYan Zheng vfree(buf); 2554ae01a0abSYan Zheng goto out_fput; 2555ae01a0abSYan Zheng } 2556ae01a0abSYan Zheng path->reada = 2; 2557ae01a0abSYan Zheng 2558a96fbc72SLiu Bo if (!same_inode) { 2559f46b5a66SChristoph Hellwig if (inode < src) { 2560fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 2561fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 2562f46b5a66SChristoph Hellwig } else { 2563fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 2564fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 2565f46b5a66SChristoph Hellwig } 2566a96fbc72SLiu Bo } else { 2567a96fbc72SLiu Bo mutex_lock(&src->i_mutex); 2568a96fbc72SLiu Bo } 2569f46b5a66SChristoph Hellwig 2570c5c9cd4dSSage Weil /* determine range to clone */ 2571c5c9cd4dSSage Weil ret = -EINVAL; 25722ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 2573f46b5a66SChristoph Hellwig goto out_unlock; 2574c5c9cd4dSSage Weil if (len == 0) 2575c5c9cd4dSSage Weil olen = len = src->i_size - off; 2576c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 2577c5c9cd4dSSage Weil if (off + len == src->i_size) 25782a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 2579c5c9cd4dSSage Weil 2580c5c9cd4dSSage Weil /* verify the end result is block aligned */ 25812a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 25822a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 2583c5c9cd4dSSage Weil goto out_unlock; 2584c5c9cd4dSSage Weil 2585a96fbc72SLiu Bo /* verify if ranges are overlapped within the same file */ 2586a96fbc72SLiu Bo if (same_inode) { 2587a96fbc72SLiu Bo if (destoff + len > off && destoff < off + len) 2588a96fbc72SLiu Bo goto out_unlock; 2589a96fbc72SLiu Bo } 2590a96fbc72SLiu Bo 2591d525e8abSLi Zefan if (destoff > inode->i_size) { 2592d525e8abSLi Zefan ret = btrfs_cont_expand(inode, inode->i_size, destoff); 2593d525e8abSLi Zefan if (ret) 2594d525e8abSLi Zefan goto out_unlock; 2595d525e8abSLi Zefan } 2596d525e8abSLi Zefan 259771ef0786SLi Zefan /* truncate page cache pages from target inode range */ 259871ef0786SLi Zefan truncate_inode_pages_range(&inode->i_data, destoff, 259971ef0786SLi Zefan PAGE_CACHE_ALIGN(destoff + len) - 1); 260071ef0786SLi Zefan 2601f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 2602f46b5a66SChristoph Hellwig another, and lock file content */ 2603f46b5a66SChristoph Hellwig while (1) { 260431840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 2605aa42ffd9SLiu Bo lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2606aa42ffd9SLiu Bo ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1); 26079a019196SSage Weil if (!ordered && 2608aa42ffd9SLiu Bo !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1, 26099a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 2610f46b5a66SChristoph Hellwig break; 2611aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2612ae01a0abSYan Zheng if (ordered) 2613ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 26149a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 2615f46b5a66SChristoph Hellwig } 2616f46b5a66SChristoph Hellwig 2617c5c9cd4dSSage Weil /* clone data */ 261833345d01SLi Zefan key.objectid = btrfs_ino(src); 2619ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2620ae01a0abSYan Zheng key.offset = 0; 2621f46b5a66SChristoph Hellwig 2622f46b5a66SChristoph Hellwig while (1) { 2623f46b5a66SChristoph Hellwig /* 2624f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2625f46b5a66SChristoph Hellwig * tree. 2626f46b5a66SChristoph Hellwig */ 2627362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 2628362a20c5SDavid Sterba 0, 0); 2629f46b5a66SChristoph Hellwig if (ret < 0) 2630f46b5a66SChristoph Hellwig goto out; 2631f46b5a66SChristoph Hellwig 2632ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2633ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2634362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 2635f46b5a66SChristoph Hellwig if (ret < 0) 2636f46b5a66SChristoph Hellwig goto out; 2637f46b5a66SChristoph Hellwig if (ret > 0) 2638f46b5a66SChristoph Hellwig break; 2639ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2640f46b5a66SChristoph Hellwig } 2641f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2642f46b5a66SChristoph Hellwig slot = path->slots[0]; 2643f46b5a66SChristoph Hellwig 2644ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2645d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 264633345d01SLi Zefan key.objectid != btrfs_ino(src)) 2647f46b5a66SChristoph Hellwig break; 2648f46b5a66SChristoph Hellwig 2649c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2650c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2651c5c9cd4dSSage Weil int type; 265231840ae1SZheng Yan u32 size; 265331840ae1SZheng Yan struct btrfs_key new_key; 2654c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2655c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2656c5c9cd4dSSage Weil u8 comp; 2657b5384d48SSage Weil u64 endoff; 265831840ae1SZheng Yan 265931840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 266031840ae1SZheng Yan read_extent_buffer(leaf, buf, 266131840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 266231840ae1SZheng Yan size); 2663c5c9cd4dSSage Weil 2664c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2665c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2666c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2667c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2668c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2669c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2670d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2671d397712bSChris Mason extent); 2672d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2673d397712bSChris Mason extent); 2674c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2675d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2676d397712bSChris Mason extent); 2677c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2678c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2679c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2680c5c9cd4dSSage Weil extent); 2681c5c9cd4dSSage Weil } 2682b3b4aa74SDavid Sterba btrfs_release_path(path); 268331840ae1SZheng Yan 2684050006a7SSage Weil if (key.offset + datal <= off || 2685aa42ffd9SLiu Bo key.offset >= off + len - 1) 2686c5c9cd4dSSage Weil goto next; 2687c5c9cd4dSSage Weil 268831840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 268933345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 26904d728ec7SLi Zefan if (off <= key.offset) 2691c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 26924d728ec7SLi Zefan else 26934d728ec7SLi Zefan new_key.offset = destoff; 2694c5c9cd4dSSage Weil 2695b6f3409bSSage Weil /* 2696b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2697b6f3409bSSage Weil * 1 - add new extent 2698b6f3409bSSage Weil * 1 - inode update 2699b6f3409bSSage Weil */ 2700b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2701a22285a6SYan, Zheng if (IS_ERR(trans)) { 2702a22285a6SYan, Zheng ret = PTR_ERR(trans); 2703a22285a6SYan, Zheng goto out; 2704a22285a6SYan, Zheng } 2705a22285a6SYan, Zheng 2706c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2707c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2708d72c0842SLi Zefan /* 2709d72c0842SLi Zefan * a | --- range to clone ---| b 2710d72c0842SLi Zefan * | ------------- extent ------------- | 2711d72c0842SLi Zefan */ 2712d72c0842SLi Zefan 2713d72c0842SLi Zefan /* substract range b */ 2714d72c0842SLi Zefan if (key.offset + datal > off + len) 2715d72c0842SLi Zefan datal = off + len - key.offset; 2716d72c0842SLi Zefan 2717d72c0842SLi Zefan /* substract range a */ 2718a22285a6SYan, Zheng if (off > key.offset) { 2719a22285a6SYan, Zheng datao += off - key.offset; 2720a22285a6SYan, Zheng datal -= off - key.offset; 2721a22285a6SYan, Zheng } 2722a22285a6SYan, Zheng 27235dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2724a22285a6SYan, Zheng new_key.offset, 2725a22285a6SYan, Zheng new_key.offset + datal, 27262671485dSJosef Bacik 1); 272779787eaaSJeff Mahoney if (ret) { 272879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 272979787eaaSJeff Mahoney ret); 273079787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 273179787eaaSJeff Mahoney goto out; 273279787eaaSJeff Mahoney } 2733a22285a6SYan, Zheng 273431840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 273531840ae1SZheng Yan &new_key, size); 273679787eaaSJeff Mahoney if (ret) { 273779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 273879787eaaSJeff Mahoney ret); 273979787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 274079787eaaSJeff Mahoney goto out; 274179787eaaSJeff Mahoney } 274231840ae1SZheng Yan 274331840ae1SZheng Yan leaf = path->nodes[0]; 274431840ae1SZheng Yan slot = path->slots[0]; 274531840ae1SZheng Yan write_extent_buffer(leaf, buf, 274631840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 274731840ae1SZheng Yan size); 2748ae01a0abSYan Zheng 2749f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 2750f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 2751c5c9cd4dSSage Weil 2752c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 2753c5c9cd4dSSage Weil if (!disko) 2754c5c9cd4dSSage Weil datao = 0; 2755c5c9cd4dSSage Weil 2756c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 2757c5c9cd4dSSage Weil datao); 2758c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 2759c5c9cd4dSSage Weil datal); 2760c5c9cd4dSSage Weil if (disko) { 2761c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2762ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 27635d4f98a2SYan Zheng disko, diskl, 0, 2764f46b5a66SChristoph Hellwig root->root_key.objectid, 276533345d01SLi Zefan btrfs_ino(inode), 276666d7e7f0SArne Jansen new_key.offset - datao, 276766d7e7f0SArne Jansen 0); 276879787eaaSJeff Mahoney if (ret) { 276979787eaaSJeff Mahoney btrfs_abort_transaction(trans, 277079787eaaSJeff Mahoney root, 277179787eaaSJeff Mahoney ret); 277279787eaaSJeff Mahoney btrfs_end_transaction(trans, 277379787eaaSJeff Mahoney root); 277479787eaaSJeff Mahoney goto out; 277579787eaaSJeff Mahoney 277679787eaaSJeff Mahoney } 2777f46b5a66SChristoph Hellwig } 2778c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2779c5c9cd4dSSage Weil u64 skip = 0; 2780c5c9cd4dSSage Weil u64 trim = 0; 2781c5c9cd4dSSage Weil if (off > key.offset) { 2782c5c9cd4dSSage Weil skip = off - key.offset; 2783c5c9cd4dSSage Weil new_key.offset += skip; 278431840ae1SZheng Yan } 2785d397712bSChris Mason 2786c5c9cd4dSSage Weil if (key.offset + datal > off + len) 2787c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 2788d397712bSChris Mason 2789c5c9cd4dSSage Weil if (comp && (skip || trim)) { 2790c5c9cd4dSSage Weil ret = -EINVAL; 2791a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2792c5c9cd4dSSage Weil goto out; 279331840ae1SZheng Yan } 2794c5c9cd4dSSage Weil size -= skip + trim; 2795c5c9cd4dSSage Weil datal -= skip + trim; 2796a22285a6SYan, Zheng 27975dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2798a22285a6SYan, Zheng new_key.offset, 2799a22285a6SYan, Zheng new_key.offset + datal, 28002671485dSJosef Bacik 1); 280179787eaaSJeff Mahoney if (ret) { 280279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 280379787eaaSJeff Mahoney ret); 280479787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 280579787eaaSJeff Mahoney goto out; 280679787eaaSJeff Mahoney } 2807a22285a6SYan, Zheng 2808c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 2809c5c9cd4dSSage Weil &new_key, size); 281079787eaaSJeff Mahoney if (ret) { 281179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 281279787eaaSJeff Mahoney ret); 281379787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 281479787eaaSJeff Mahoney goto out; 281579787eaaSJeff Mahoney } 2816c5c9cd4dSSage Weil 2817c5c9cd4dSSage Weil if (skip) { 2818d397712bSChris Mason u32 start = 2819d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 2820c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 2821c5c9cd4dSSage Weil datal); 2822c5c9cd4dSSage Weil } 2823c5c9cd4dSSage Weil 2824c5c9cd4dSSage Weil leaf = path->nodes[0]; 2825c5c9cd4dSSage Weil slot = path->slots[0]; 2826c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2827c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2828c5c9cd4dSSage Weil size); 2829c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2830c5c9cd4dSSage Weil } 2831c5c9cd4dSSage Weil 2832c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2833b3b4aa74SDavid Sterba btrfs_release_path(path); 2834c5c9cd4dSSage Weil 28350c4d2d95SJosef Bacik inode_inc_iversion(inode); 2836a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2837b5384d48SSage Weil 2838b5384d48SSage Weil /* 2839b5384d48SSage Weil * we round up to the block size at eof when 2840b5384d48SSage Weil * determining which extents to clone above, 2841b5384d48SSage Weil * but shouldn't round up the file size 2842b5384d48SSage Weil */ 2843b5384d48SSage Weil endoff = new_key.offset + datal; 28445f3888ffSLi Zefan if (endoff > destoff+olen) 28455f3888ffSLi Zefan endoff = destoff+olen; 2846b5384d48SSage Weil if (endoff > inode->i_size) 2847b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2848b5384d48SSage Weil 2849a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 285079787eaaSJeff Mahoney if (ret) { 285179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 2852a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 285379787eaaSJeff Mahoney goto out; 285479787eaaSJeff Mahoney } 285579787eaaSJeff Mahoney ret = btrfs_end_transaction(trans, root); 2856a22285a6SYan, Zheng } 2857c5c9cd4dSSage Weil next: 2858b3b4aa74SDavid Sterba btrfs_release_path(path); 2859ae01a0abSYan Zheng key.offset++; 2860ae01a0abSYan Zheng } 2861f46b5a66SChristoph Hellwig ret = 0; 2862f46b5a66SChristoph Hellwig out: 2863b3b4aa74SDavid Sterba btrfs_release_path(path); 2864aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2865f46b5a66SChristoph Hellwig out_unlock: 2866f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2867a96fbc72SLiu Bo if (!same_inode) 2868f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2869ae01a0abSYan Zheng vfree(buf); 2870ae01a0abSYan Zheng btrfs_free_path(path); 2871f46b5a66SChristoph Hellwig out_fput: 28722903ff01SAl Viro fdput(src_file); 2873ab67b7c1SYan Zheng out_drop_write: 28742a79f17eSAl Viro mnt_drop_write_file(file); 2875f46b5a66SChristoph Hellwig return ret; 2876f46b5a66SChristoph Hellwig } 2877f46b5a66SChristoph Hellwig 28787a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2879c5c9cd4dSSage Weil { 2880c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2881c5c9cd4dSSage Weil 28827a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2883c5c9cd4dSSage Weil return -EFAULT; 2884c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2885c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2886c5c9cd4dSSage Weil } 2887c5c9cd4dSSage Weil 2888f46b5a66SChristoph Hellwig /* 2889f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2890f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2891f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2892f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2893f46b5a66SChristoph Hellwig */ 2894b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2895f46b5a66SChristoph Hellwig { 2896496ad9aaSAl Viro struct inode *inode = file_inode(file); 2897f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2898f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 28991ab86aedSSage Weil int ret; 2900f46b5a66SChristoph Hellwig 29011ab86aedSSage Weil ret = -EPERM; 2902df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2903f46b5a66SChristoph Hellwig goto out; 29041ab86aedSSage Weil 29051ab86aedSSage Weil ret = -EINPROGRESS; 29061ab86aedSSage Weil if (file->private_data) 29071ab86aedSSage Weil goto out; 29089ca9ee09SSage Weil 2909b83cc969SLi Zefan ret = -EROFS; 2910b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2911b83cc969SLi Zefan goto out; 2912b83cc969SLi Zefan 2913a561be71SAl Viro ret = mnt_want_write_file(file); 2914c146afadSYan Zheng if (ret) 2915c146afadSYan Zheng goto out; 2916c146afadSYan Zheng 2917a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 29189ca9ee09SSage Weil 2919f46b5a66SChristoph Hellwig ret = -ENOMEM; 29207a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 2921abd30bb0STsutomu Itoh if (IS_ERR(trans)) 29221ab86aedSSage Weil goto out_drop; 29231ab86aedSSage Weil 29241ab86aedSSage Weil file->private_data = trans; 29251ab86aedSSage Weil return 0; 29261ab86aedSSage Weil 29271ab86aedSSage Weil out_drop: 2928a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 29292a79f17eSAl Viro mnt_drop_write_file(file); 2930f46b5a66SChristoph Hellwig out: 2931f46b5a66SChristoph Hellwig return ret; 2932f46b5a66SChristoph Hellwig } 2933f46b5a66SChristoph Hellwig 29346ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 29356ef5ed0dSJosef Bacik { 2936496ad9aaSAl Viro struct inode *inode = file_inode(file); 29376ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 29386ef5ed0dSJosef Bacik struct btrfs_root *new_root; 29396ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 29406ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 29416ef5ed0dSJosef Bacik struct btrfs_path *path; 29426ef5ed0dSJosef Bacik struct btrfs_key location; 29436ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 29446ef5ed0dSJosef Bacik u64 objectid = 0; 29456ef5ed0dSJosef Bacik u64 dir_id; 29463c04ce01SMiao Xie int ret; 29476ef5ed0dSJosef Bacik 29486ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 29496ef5ed0dSJosef Bacik return -EPERM; 29506ef5ed0dSJosef Bacik 29513c04ce01SMiao Xie ret = mnt_want_write_file(file); 29523c04ce01SMiao Xie if (ret) 29533c04ce01SMiao Xie return ret; 29543c04ce01SMiao Xie 29553c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 29563c04ce01SMiao Xie ret = -EFAULT; 29573c04ce01SMiao Xie goto out; 29583c04ce01SMiao Xie } 29596ef5ed0dSJosef Bacik 29606ef5ed0dSJosef Bacik if (!objectid) 29616ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 29626ef5ed0dSJosef Bacik 29636ef5ed0dSJosef Bacik location.objectid = objectid; 29646ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 29656ef5ed0dSJosef Bacik location.offset = (u64)-1; 29666ef5ed0dSJosef Bacik 29676ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 29683c04ce01SMiao Xie if (IS_ERR(new_root)) { 29693c04ce01SMiao Xie ret = PTR_ERR(new_root); 29703c04ce01SMiao Xie goto out; 29713c04ce01SMiao Xie } 29726ef5ed0dSJosef Bacik 29736ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 29743c04ce01SMiao Xie if (!path) { 29753c04ce01SMiao Xie ret = -ENOMEM; 29763c04ce01SMiao Xie goto out; 29773c04ce01SMiao Xie } 29786ef5ed0dSJosef Bacik path->leave_spinning = 1; 29796ef5ed0dSJosef Bacik 29806ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 298198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 29826ef5ed0dSJosef Bacik btrfs_free_path(path); 29833c04ce01SMiao Xie ret = PTR_ERR(trans); 29843c04ce01SMiao Xie goto out; 29856ef5ed0dSJosef Bacik } 29866ef5ed0dSJosef Bacik 29876c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 29886ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 29896ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2990cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 29916ef5ed0dSJosef Bacik btrfs_free_path(path); 29926ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 29936ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 29946ef5ed0dSJosef Bacik "this isn't going to work\n"); 29953c04ce01SMiao Xie ret = -ENOENT; 29963c04ce01SMiao Xie goto out; 29976ef5ed0dSJosef Bacik } 29986ef5ed0dSJosef Bacik 29996ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 30006ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 30016ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 30026ef5ed0dSJosef Bacik btrfs_free_path(path); 30036ef5ed0dSJosef Bacik 30042b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 30056ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 30063c04ce01SMiao Xie out: 30073c04ce01SMiao Xie mnt_drop_write_file(file); 30083c04ce01SMiao Xie return ret; 30096ef5ed0dSJosef Bacik } 30106ef5ed0dSJosef Bacik 30115af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 3012bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3013bf5fc093SJosef Bacik { 3014bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 3015bf5fc093SJosef Bacik 3016bf5fc093SJosef Bacik space->total_bytes = 0; 3017bf5fc093SJosef Bacik space->used_bytes = 0; 3018bf5fc093SJosef Bacik space->flags = 0; 3019bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3020bf5fc093SJosef Bacik space->flags = block_group->flags; 3021bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 3022bf5fc093SJosef Bacik space->used_bytes += 3023bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 3024bf5fc093SJosef Bacik } 3025bf5fc093SJosef Bacik } 3026bf5fc093SJosef Bacik 302748a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 30281406e432SJosef Bacik { 30291406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 30301406e432SJosef Bacik struct btrfs_ioctl_space_info space; 30311406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 30327fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 303313f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 30341406e432SJosef Bacik struct btrfs_space_info *info; 3035bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 3036bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3037bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3038bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 3039bf5fc093SJosef Bacik int num_types = 4; 30407fde62bfSChris Mason int alloc_size; 30411406e432SJosef Bacik int ret = 0; 304251788b1bSDan Rosenberg u64 slot_count = 0; 3043bf5fc093SJosef Bacik int i, c; 30441406e432SJosef Bacik 30451406e432SJosef Bacik if (copy_from_user(&space_args, 30461406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 30471406e432SJosef Bacik sizeof(space_args))) 30481406e432SJosef Bacik return -EFAULT; 30491406e432SJosef Bacik 3050bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3051bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3052bf5fc093SJosef Bacik 3053bf5fc093SJosef Bacik info = NULL; 30547fde62bfSChris Mason rcu_read_lock(); 3055bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3056bf5fc093SJosef Bacik list) { 3057bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3058bf5fc093SJosef Bacik info = tmp; 3059bf5fc093SJosef Bacik break; 3060bf5fc093SJosef Bacik } 3061bf5fc093SJosef Bacik } 30627fde62bfSChris Mason rcu_read_unlock(); 30631406e432SJosef Bacik 3064bf5fc093SJosef Bacik if (!info) 3065bf5fc093SJosef Bacik continue; 3066bf5fc093SJosef Bacik 3067bf5fc093SJosef Bacik down_read(&info->groups_sem); 3068bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3069bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3070bf5fc093SJosef Bacik slot_count++; 3071bf5fc093SJosef Bacik } 3072bf5fc093SJosef Bacik up_read(&info->groups_sem); 3073bf5fc093SJosef Bacik } 3074bf5fc093SJosef Bacik 30757fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 30767fde62bfSChris Mason if (space_args.space_slots == 0) { 30777fde62bfSChris Mason space_args.total_spaces = slot_count; 30787fde62bfSChris Mason goto out; 30797fde62bfSChris Mason } 3080bf5fc093SJosef Bacik 308151788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3082bf5fc093SJosef Bacik 30837fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3084bf5fc093SJosef Bacik 30857fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 30867fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 30877fde62bfSChris Mason */ 30887fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 30897fde62bfSChris Mason return -ENOMEM; 30907fde62bfSChris Mason 30917fde62bfSChris Mason space_args.total_spaces = 0; 30927fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 30937fde62bfSChris Mason if (!dest) 30947fde62bfSChris Mason return -ENOMEM; 30957fde62bfSChris Mason dest_orig = dest; 30967fde62bfSChris Mason 30977fde62bfSChris Mason /* now we have a buffer to copy into */ 3098bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3099bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3100bf5fc093SJosef Bacik 310151788b1bSDan Rosenberg if (!slot_count) 310251788b1bSDan Rosenberg break; 310351788b1bSDan Rosenberg 3104bf5fc093SJosef Bacik info = NULL; 31051406e432SJosef Bacik rcu_read_lock(); 3106bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3107bf5fc093SJosef Bacik list) { 3108bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3109bf5fc093SJosef Bacik info = tmp; 31107fde62bfSChris Mason break; 3111bf5fc093SJosef Bacik } 3112bf5fc093SJosef Bacik } 3113bf5fc093SJosef Bacik rcu_read_unlock(); 31147fde62bfSChris Mason 3115bf5fc093SJosef Bacik if (!info) 3116bf5fc093SJosef Bacik continue; 3117bf5fc093SJosef Bacik down_read(&info->groups_sem); 3118bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3119bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 31205af3e8ccSStefan Behrens btrfs_get_block_group_info( 31215af3e8ccSStefan Behrens &info->block_groups[c], &space); 31227fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 31231406e432SJosef Bacik dest++; 31241406e432SJosef Bacik space_args.total_spaces++; 312551788b1bSDan Rosenberg slot_count--; 31261406e432SJosef Bacik } 312751788b1bSDan Rosenberg if (!slot_count) 312851788b1bSDan Rosenberg break; 3129bf5fc093SJosef Bacik } 3130bf5fc093SJosef Bacik up_read(&info->groups_sem); 3131bf5fc093SJosef Bacik } 31321406e432SJosef Bacik 31332eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 31347fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 31357fde62bfSChris Mason 31367fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 31377fde62bfSChris Mason ret = -EFAULT; 31387fde62bfSChris Mason 31397fde62bfSChris Mason kfree(dest_orig); 31407fde62bfSChris Mason out: 31417fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 31421406e432SJosef Bacik ret = -EFAULT; 31431406e432SJosef Bacik 31441406e432SJosef Bacik return ret; 31451406e432SJosef Bacik } 31461406e432SJosef Bacik 3147f46b5a66SChristoph Hellwig /* 3148f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3149f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3150f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3151f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3152f46b5a66SChristoph Hellwig */ 3153f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3154f46b5a66SChristoph Hellwig { 3155496ad9aaSAl Viro struct inode *inode = file_inode(file); 3156f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3157f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3158f46b5a66SChristoph Hellwig 3159f46b5a66SChristoph Hellwig trans = file->private_data; 31601ab86aedSSage Weil if (!trans) 31611ab86aedSSage Weil return -EINVAL; 3162b214107eSChristoph Hellwig file->private_data = NULL; 31639ca9ee09SSage Weil 31641ab86aedSSage Weil btrfs_end_transaction(trans, root); 31651ab86aedSSage Weil 3166a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 31679ca9ee09SSage Weil 31682a79f17eSAl Viro mnt_drop_write_file(file); 31691ab86aedSSage Weil return 0; 3170f46b5a66SChristoph Hellwig } 3171f46b5a66SChristoph Hellwig 31729a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 31739a8c28beSMiao Xie void __user *argp) 317446204592SSage Weil { 317546204592SSage Weil struct btrfs_trans_handle *trans; 317646204592SSage Weil u64 transid; 3177db5b493aSTsutomu Itoh int ret; 317846204592SSage Weil 3179d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3180ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3181ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 318298d5dc13STsutomu Itoh return PTR_ERR(trans); 3183ff7c1d33SMiao Xie 3184ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3185ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3186ff7c1d33SMiao Xie goto out; 3187ff7c1d33SMiao Xie } 318846204592SSage Weil transid = trans->transid; 3189db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 31908b2b2d3cSTsutomu Itoh if (ret) { 31918b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 3192db5b493aSTsutomu Itoh return ret; 31938b2b2d3cSTsutomu Itoh } 3194ff7c1d33SMiao Xie out: 319546204592SSage Weil if (argp) 319646204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 319746204592SSage Weil return -EFAULT; 319846204592SSage Weil return 0; 319946204592SSage Weil } 320046204592SSage Weil 32019a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 32029a8c28beSMiao Xie void __user *argp) 320346204592SSage Weil { 320446204592SSage Weil u64 transid; 320546204592SSage Weil 320646204592SSage Weil if (argp) { 320746204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 320846204592SSage Weil return -EFAULT; 320946204592SSage Weil } else { 321046204592SSage Weil transid = 0; /* current trans */ 321146204592SSage Weil } 321246204592SSage Weil return btrfs_wait_for_commit(root, transid); 321346204592SSage Weil } 321446204592SSage Weil 3215b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 3216475f6387SJan Schmidt { 3217496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 3218475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3219b8e95489SMiao Xie int ret; 3220475f6387SJan Schmidt 3221475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3222475f6387SJan Schmidt return -EPERM; 3223475f6387SJan Schmidt 3224475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3225475f6387SJan Schmidt if (IS_ERR(sa)) 3226475f6387SJan Schmidt return PTR_ERR(sa); 3227475f6387SJan Schmidt 3228b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 3229b8e95489SMiao Xie ret = mnt_want_write_file(file); 3230b8e95489SMiao Xie if (ret) 3231b8e95489SMiao Xie goto out; 3232b8e95489SMiao Xie } 3233b8e95489SMiao Xie 3234aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 323563a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 323663a212abSStefan Behrens 0); 3237475f6387SJan Schmidt 3238475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3239475f6387SJan Schmidt ret = -EFAULT; 3240475f6387SJan Schmidt 3241b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 3242b8e95489SMiao Xie mnt_drop_write_file(file); 3243b8e95489SMiao Xie out: 3244475f6387SJan Schmidt kfree(sa); 3245475f6387SJan Schmidt return ret; 3246475f6387SJan Schmidt } 3247475f6387SJan Schmidt 3248475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 3249475f6387SJan Schmidt { 3250475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3251475f6387SJan Schmidt return -EPERM; 3252475f6387SJan Schmidt 3253aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 3254475f6387SJan Schmidt } 3255475f6387SJan Schmidt 3256475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 3257475f6387SJan Schmidt void __user *arg) 3258475f6387SJan Schmidt { 3259475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3260475f6387SJan Schmidt int ret; 3261475f6387SJan Schmidt 3262475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3263475f6387SJan Schmidt return -EPERM; 3264475f6387SJan Schmidt 3265475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3266475f6387SJan Schmidt if (IS_ERR(sa)) 3267475f6387SJan Schmidt return PTR_ERR(sa); 3268475f6387SJan Schmidt 3269475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 3270475f6387SJan Schmidt 3271475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3272475f6387SJan Schmidt ret = -EFAULT; 3273475f6387SJan Schmidt 3274475f6387SJan Schmidt kfree(sa); 3275475f6387SJan Schmidt return ret; 3276475f6387SJan Schmidt } 3277475f6387SJan Schmidt 3278c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 3279b27f7c0cSDavid Sterba void __user *arg) 3280c11d2c23SStefan Behrens { 3281c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 3282c11d2c23SStefan Behrens int ret; 3283c11d2c23SStefan Behrens 3284c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 3285c11d2c23SStefan Behrens if (IS_ERR(sa)) 3286c11d2c23SStefan Behrens return PTR_ERR(sa); 3287c11d2c23SStefan Behrens 3288b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 3289b27f7c0cSDavid Sterba kfree(sa); 3290b27f7c0cSDavid Sterba return -EPERM; 3291b27f7c0cSDavid Sterba } 3292b27f7c0cSDavid Sterba 3293b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 3294c11d2c23SStefan Behrens 3295c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 3296c11d2c23SStefan Behrens ret = -EFAULT; 3297c11d2c23SStefan Behrens 3298c11d2c23SStefan Behrens kfree(sa); 3299c11d2c23SStefan Behrens return ret; 3300c11d2c23SStefan Behrens } 3301c11d2c23SStefan Behrens 33023f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 33033f6bcfbdSStefan Behrens { 33043f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 33053f6bcfbdSStefan Behrens int ret; 33063f6bcfbdSStefan Behrens 33073f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 33083f6bcfbdSStefan Behrens return -EPERM; 33093f6bcfbdSStefan Behrens 33103f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 33113f6bcfbdSStefan Behrens if (IS_ERR(p)) 33123f6bcfbdSStefan Behrens return PTR_ERR(p); 33133f6bcfbdSStefan Behrens 33143f6bcfbdSStefan Behrens switch (p->cmd) { 33153f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 33163f6bcfbdSStefan Behrens if (atomic_xchg( 33173f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 33183f6bcfbdSStefan Behrens 1)) { 33193f6bcfbdSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 33203f6bcfbdSStefan Behrens ret = -EINPROGRESS; 33213f6bcfbdSStefan Behrens } else { 33223f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 33233f6bcfbdSStefan Behrens atomic_set( 33243f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 33253f6bcfbdSStefan Behrens 0); 33263f6bcfbdSStefan Behrens } 33273f6bcfbdSStefan Behrens break; 33283f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 33293f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 33303f6bcfbdSStefan Behrens ret = 0; 33313f6bcfbdSStefan Behrens break; 33323f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 33333f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 33343f6bcfbdSStefan Behrens break; 33353f6bcfbdSStefan Behrens default: 33363f6bcfbdSStefan Behrens ret = -EINVAL; 33373f6bcfbdSStefan Behrens break; 33383f6bcfbdSStefan Behrens } 33393f6bcfbdSStefan Behrens 33403f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 33413f6bcfbdSStefan Behrens ret = -EFAULT; 33423f6bcfbdSStefan Behrens 33433f6bcfbdSStefan Behrens kfree(p); 33443f6bcfbdSStefan Behrens return ret; 33453f6bcfbdSStefan Behrens } 33463f6bcfbdSStefan Behrens 3347d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 3348d7728c96SJan Schmidt { 3349d7728c96SJan Schmidt int ret = 0; 3350d7728c96SJan Schmidt int i; 3351740c3d22SChris Mason u64 rel_ptr; 3352d7728c96SJan Schmidt int size; 3353806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 3354d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 3355d7728c96SJan Schmidt struct btrfs_path *path; 3356d7728c96SJan Schmidt 335782b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 3358d7728c96SJan Schmidt return -EPERM; 3359d7728c96SJan Schmidt 3360d7728c96SJan Schmidt path = btrfs_alloc_path(); 3361d7728c96SJan Schmidt if (!path) { 3362d7728c96SJan Schmidt ret = -ENOMEM; 3363d7728c96SJan Schmidt goto out; 3364d7728c96SJan Schmidt } 3365d7728c96SJan Schmidt 3366d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 3367d7728c96SJan Schmidt if (IS_ERR(ipa)) { 3368d7728c96SJan Schmidt ret = PTR_ERR(ipa); 3369d7728c96SJan Schmidt ipa = NULL; 3370d7728c96SJan Schmidt goto out; 3371d7728c96SJan Schmidt } 3372d7728c96SJan Schmidt 3373d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 3374d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 3375d7728c96SJan Schmidt if (IS_ERR(ipath)) { 3376d7728c96SJan Schmidt ret = PTR_ERR(ipath); 3377d7728c96SJan Schmidt ipath = NULL; 3378d7728c96SJan Schmidt goto out; 3379d7728c96SJan Schmidt } 3380d7728c96SJan Schmidt 3381d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 3382d7728c96SJan Schmidt if (ret < 0) 3383d7728c96SJan Schmidt goto out; 3384d7728c96SJan Schmidt 3385d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 3386745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 3387745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 3388740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 3389d7728c96SJan Schmidt } 3390d7728c96SJan Schmidt 3391745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 3392745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 3393d7728c96SJan Schmidt if (ret) { 3394d7728c96SJan Schmidt ret = -EFAULT; 3395d7728c96SJan Schmidt goto out; 3396d7728c96SJan Schmidt } 3397d7728c96SJan Schmidt 3398d7728c96SJan Schmidt out: 3399d7728c96SJan Schmidt btrfs_free_path(path); 3400d7728c96SJan Schmidt free_ipath(ipath); 3401d7728c96SJan Schmidt kfree(ipa); 3402d7728c96SJan Schmidt 3403d7728c96SJan Schmidt return ret; 3404d7728c96SJan Schmidt } 3405d7728c96SJan Schmidt 3406d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 3407d7728c96SJan Schmidt { 3408d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 3409d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 3410d7728c96SJan Schmidt 3411d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 3412d7728c96SJan Schmidt inodes->bytes_left -= c; 3413d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 3414d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 3415d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 3416d7728c96SJan Schmidt inodes->elem_cnt += 3; 3417d7728c96SJan Schmidt } else { 3418d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 3419d7728c96SJan Schmidt inodes->bytes_left = 0; 3420d7728c96SJan Schmidt inodes->elem_missed += 3; 3421d7728c96SJan Schmidt } 3422d7728c96SJan Schmidt 3423d7728c96SJan Schmidt return 0; 3424d7728c96SJan Schmidt } 3425d7728c96SJan Schmidt 3426d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 3427d7728c96SJan Schmidt void __user *arg) 3428d7728c96SJan Schmidt { 3429d7728c96SJan Schmidt int ret = 0; 3430d7728c96SJan Schmidt int size; 3431d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3432d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3433d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3434d7728c96SJan Schmidt 3435d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3436d7728c96SJan Schmidt return -EPERM; 3437d7728c96SJan Schmidt 3438d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 3439d7728c96SJan Schmidt if (IS_ERR(loi)) { 3440d7728c96SJan Schmidt ret = PTR_ERR(loi); 3441d7728c96SJan Schmidt loi = NULL; 3442d7728c96SJan Schmidt goto out; 3443d7728c96SJan Schmidt } 3444d7728c96SJan Schmidt 3445d7728c96SJan Schmidt path = btrfs_alloc_path(); 3446d7728c96SJan Schmidt if (!path) { 3447d7728c96SJan Schmidt ret = -ENOMEM; 3448d7728c96SJan Schmidt goto out; 3449d7728c96SJan Schmidt } 3450d7728c96SJan Schmidt 3451425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 3452d7728c96SJan Schmidt inodes = init_data_container(size); 3453d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3454d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3455d7728c96SJan Schmidt inodes = NULL; 3456d7728c96SJan Schmidt goto out; 3457d7728c96SJan Schmidt } 3458d7728c96SJan Schmidt 3459df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 3460df031f07SLiu Bo build_ino_list, inodes); 3461df031f07SLiu Bo if (ret == -EINVAL) 3462d7728c96SJan Schmidt ret = -ENOENT; 3463d7728c96SJan Schmidt if (ret < 0) 3464d7728c96SJan Schmidt goto out; 3465d7728c96SJan Schmidt 3466745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 3467745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 3468d7728c96SJan Schmidt if (ret) 3469d7728c96SJan Schmidt ret = -EFAULT; 3470d7728c96SJan Schmidt 3471d7728c96SJan Schmidt out: 3472d7728c96SJan Schmidt btrfs_free_path(path); 3473425d17a2SLiu Bo vfree(inodes); 3474d7728c96SJan Schmidt kfree(loi); 3475d7728c96SJan Schmidt 3476d7728c96SJan Schmidt return ret; 3477d7728c96SJan Schmidt } 3478d7728c96SJan Schmidt 347919a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 3480c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3481c9e9f97bSIlya Dryomov { 3482c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3483c9e9f97bSIlya Dryomov 3484c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3485c9e9f97bSIlya Dryomov 3486837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 3487837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 3488837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 3489837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 3490a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 3491a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 3492837d5b6eSIlya Dryomov 3493c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 3494c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 3495c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 349619a39dceSIlya Dryomov 349719a39dceSIlya Dryomov if (lock) { 349819a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 349919a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 350019a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 350119a39dceSIlya Dryomov } else { 350219a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 350319a39dceSIlya Dryomov } 3504c9e9f97bSIlya Dryomov } 3505c9e9f97bSIlya Dryomov 35069ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 3507c9e9f97bSIlya Dryomov { 3508496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 3509c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 3510c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 3511c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 3512ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 3513c9e9f97bSIlya Dryomov int ret; 3514c9e9f97bSIlya Dryomov 3515c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3516c9e9f97bSIlya Dryomov return -EPERM; 3517c9e9f97bSIlya Dryomov 3518e54bfa31SLiu Bo ret = mnt_want_write_file(file); 35199ba1f6e4SLiu Bo if (ret) 35209ba1f6e4SLiu Bo return ret; 35219ba1f6e4SLiu Bo 3522ed0fb78fSIlya Dryomov again: 3523ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 3524c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 3525c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3526ed0fb78fSIlya Dryomov need_unlock = true; 3527ed0fb78fSIlya Dryomov goto locked; 3528ed0fb78fSIlya Dryomov } 3529ed0fb78fSIlya Dryomov 3530ed0fb78fSIlya Dryomov /* 3531ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 3532ed0fb78fSIlya Dryomov * (1) some other op is running 3533ed0fb78fSIlya Dryomov * (2) balance is running 3534ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 3535ed0fb78fSIlya Dryomov */ 3536ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3537ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3538ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 3539ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 3540ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3541ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 3542ed0fb78fSIlya Dryomov goto again; 3543ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3544ed0fb78fSIlya Dryomov 3545ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 3546ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 3547ed0fb78fSIlya Dryomov /* this is (3) */ 3548ed0fb78fSIlya Dryomov need_unlock = false; 3549ed0fb78fSIlya Dryomov goto locked; 3550ed0fb78fSIlya Dryomov } 3551ed0fb78fSIlya Dryomov 3552ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3553ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3554ed0fb78fSIlya Dryomov goto again; 3555ed0fb78fSIlya Dryomov } else { 3556ed0fb78fSIlya Dryomov /* this is (2) */ 3557ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3558ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 3559ed0fb78fSIlya Dryomov goto out; 3560ed0fb78fSIlya Dryomov } 3561ed0fb78fSIlya Dryomov } else { 3562ed0fb78fSIlya Dryomov /* this is (1) */ 3563ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3564ed0fb78fSIlya Dryomov pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 3565ed0fb78fSIlya Dryomov ret = -EINVAL; 3566ed0fb78fSIlya Dryomov goto out; 3567ed0fb78fSIlya Dryomov } 3568ed0fb78fSIlya Dryomov 3569ed0fb78fSIlya Dryomov locked: 3570ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 3571c9e9f97bSIlya Dryomov 3572c9e9f97bSIlya Dryomov if (arg) { 3573c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 3574c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 3575c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 3576ed0fb78fSIlya Dryomov goto out_unlock; 3577c9e9f97bSIlya Dryomov } 3578de322263SIlya Dryomov 3579de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 3580de322263SIlya Dryomov if (!fs_info->balance_ctl) { 3581de322263SIlya Dryomov ret = -ENOTCONN; 3582de322263SIlya Dryomov goto out_bargs; 3583de322263SIlya Dryomov } 3584de322263SIlya Dryomov 3585de322263SIlya Dryomov bctl = fs_info->balance_ctl; 3586de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 3587de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 3588de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 3589de322263SIlya Dryomov 3590de322263SIlya Dryomov goto do_balance; 3591de322263SIlya Dryomov } 3592c9e9f97bSIlya Dryomov } else { 3593c9e9f97bSIlya Dryomov bargs = NULL; 3594c9e9f97bSIlya Dryomov } 3595c9e9f97bSIlya Dryomov 3596ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3597837d5b6eSIlya Dryomov ret = -EINPROGRESS; 3598837d5b6eSIlya Dryomov goto out_bargs; 3599837d5b6eSIlya Dryomov } 3600837d5b6eSIlya Dryomov 3601c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3602c9e9f97bSIlya Dryomov if (!bctl) { 3603c9e9f97bSIlya Dryomov ret = -ENOMEM; 3604c9e9f97bSIlya Dryomov goto out_bargs; 3605c9e9f97bSIlya Dryomov } 3606c9e9f97bSIlya Dryomov 3607c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 3608c9e9f97bSIlya Dryomov if (arg) { 3609c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 3610c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 3611c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 3612c9e9f97bSIlya Dryomov 3613c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 3614f43ffb60SIlya Dryomov } else { 3615f43ffb60SIlya Dryomov /* balance everything - no filters */ 3616f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 3617c9e9f97bSIlya Dryomov } 3618c9e9f97bSIlya Dryomov 3619de322263SIlya Dryomov do_balance: 3620c9e9f97bSIlya Dryomov /* 3621ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 3622ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 3623ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 3624ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 3625ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 3626c9e9f97bSIlya Dryomov */ 3627ed0fb78fSIlya Dryomov need_unlock = false; 3628ed0fb78fSIlya Dryomov 3629ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 3630ed0fb78fSIlya Dryomov 3631c9e9f97bSIlya Dryomov if (arg) { 3632c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 3633c9e9f97bSIlya Dryomov ret = -EFAULT; 3634c9e9f97bSIlya Dryomov } 3635c9e9f97bSIlya Dryomov 3636c9e9f97bSIlya Dryomov out_bargs: 3637c9e9f97bSIlya Dryomov kfree(bargs); 3638ed0fb78fSIlya Dryomov out_unlock: 3639c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3640c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3641ed0fb78fSIlya Dryomov if (need_unlock) 3642ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3643ed0fb78fSIlya Dryomov out: 3644e54bfa31SLiu Bo mnt_drop_write_file(file); 3645c9e9f97bSIlya Dryomov return ret; 3646c9e9f97bSIlya Dryomov } 3647c9e9f97bSIlya Dryomov 3648837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 3649837d5b6eSIlya Dryomov { 3650837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3651837d5b6eSIlya Dryomov return -EPERM; 3652837d5b6eSIlya Dryomov 3653837d5b6eSIlya Dryomov switch (cmd) { 3654837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 3655837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 3656a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 3657a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 3658837d5b6eSIlya Dryomov } 3659837d5b6eSIlya Dryomov 3660837d5b6eSIlya Dryomov return -EINVAL; 3661837d5b6eSIlya Dryomov } 3662837d5b6eSIlya Dryomov 366319a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 366419a39dceSIlya Dryomov void __user *arg) 366519a39dceSIlya Dryomov { 366619a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 366719a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 366819a39dceSIlya Dryomov int ret = 0; 366919a39dceSIlya Dryomov 367019a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 367119a39dceSIlya Dryomov return -EPERM; 367219a39dceSIlya Dryomov 367319a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 367419a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 367519a39dceSIlya Dryomov ret = -ENOTCONN; 367619a39dceSIlya Dryomov goto out; 367719a39dceSIlya Dryomov } 367819a39dceSIlya Dryomov 367919a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 368019a39dceSIlya Dryomov if (!bargs) { 368119a39dceSIlya Dryomov ret = -ENOMEM; 368219a39dceSIlya Dryomov goto out; 368319a39dceSIlya Dryomov } 368419a39dceSIlya Dryomov 368519a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 368619a39dceSIlya Dryomov 368719a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 368819a39dceSIlya Dryomov ret = -EFAULT; 368919a39dceSIlya Dryomov 369019a39dceSIlya Dryomov kfree(bargs); 369119a39dceSIlya Dryomov out: 369219a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 369319a39dceSIlya Dryomov return ret; 369419a39dceSIlya Dryomov } 369519a39dceSIlya Dryomov 3696905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 36975d13a37bSArne Jansen { 3698496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 36995d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 37005d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 37015d13a37bSArne Jansen int ret; 37025d13a37bSArne Jansen int err; 37035d13a37bSArne Jansen 37045d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 37055d13a37bSArne Jansen return -EPERM; 37065d13a37bSArne Jansen 3707905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3708905b0ddaSMiao Xie if (ret) 3709905b0ddaSMiao Xie return ret; 37105d13a37bSArne Jansen 37115d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3712905b0ddaSMiao Xie if (IS_ERR(sa)) { 3713905b0ddaSMiao Xie ret = PTR_ERR(sa); 3714905b0ddaSMiao Xie goto drop_write; 3715905b0ddaSMiao Xie } 37165d13a37bSArne Jansen 37177708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 371892f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 37195d13a37bSArne Jansen if (IS_ERR(trans)) { 37205d13a37bSArne Jansen ret = PTR_ERR(trans); 37215d13a37bSArne Jansen goto out; 37225d13a37bSArne Jansen } 37235d13a37bSArne Jansen 37245d13a37bSArne Jansen switch (sa->cmd) { 37255d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 37265d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 37275d13a37bSArne Jansen break; 37285d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 37295d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 37305d13a37bSArne Jansen break; 37315d13a37bSArne Jansen default: 37325d13a37bSArne Jansen ret = -EINVAL; 37335d13a37bSArne Jansen break; 37345d13a37bSArne Jansen } 37355d13a37bSArne Jansen 373692f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 37375d13a37bSArne Jansen if (err && !ret) 37385d13a37bSArne Jansen ret = err; 37395d13a37bSArne Jansen out: 37405d13a37bSArne Jansen kfree(sa); 37417708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 3742905b0ddaSMiao Xie drop_write: 3743905b0ddaSMiao Xie mnt_drop_write_file(file); 37445d13a37bSArne Jansen return ret; 37455d13a37bSArne Jansen } 37465d13a37bSArne Jansen 3747905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 37485d13a37bSArne Jansen { 3749496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 37505d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 37515d13a37bSArne Jansen struct btrfs_trans_handle *trans; 37525d13a37bSArne Jansen int ret; 37535d13a37bSArne Jansen int err; 37545d13a37bSArne Jansen 37555d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 37565d13a37bSArne Jansen return -EPERM; 37575d13a37bSArne Jansen 3758905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3759905b0ddaSMiao Xie if (ret) 3760905b0ddaSMiao Xie return ret; 37615d13a37bSArne Jansen 37625d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3763905b0ddaSMiao Xie if (IS_ERR(sa)) { 3764905b0ddaSMiao Xie ret = PTR_ERR(sa); 3765905b0ddaSMiao Xie goto drop_write; 3766905b0ddaSMiao Xie } 37675d13a37bSArne Jansen 37685d13a37bSArne Jansen trans = btrfs_join_transaction(root); 37695d13a37bSArne Jansen if (IS_ERR(trans)) { 37705d13a37bSArne Jansen ret = PTR_ERR(trans); 37715d13a37bSArne Jansen goto out; 37725d13a37bSArne Jansen } 37735d13a37bSArne Jansen 37745d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 37755d13a37bSArne Jansen if (sa->assign) { 37765d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 37775d13a37bSArne Jansen sa->src, sa->dst); 37785d13a37bSArne Jansen } else { 37795d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 37805d13a37bSArne Jansen sa->src, sa->dst); 37815d13a37bSArne Jansen } 37825d13a37bSArne Jansen 37835d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 37845d13a37bSArne Jansen if (err && !ret) 37855d13a37bSArne Jansen ret = err; 37865d13a37bSArne Jansen 37875d13a37bSArne Jansen out: 37885d13a37bSArne Jansen kfree(sa); 3789905b0ddaSMiao Xie drop_write: 3790905b0ddaSMiao Xie mnt_drop_write_file(file); 37915d13a37bSArne Jansen return ret; 37925d13a37bSArne Jansen } 37935d13a37bSArne Jansen 3794905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 37955d13a37bSArne Jansen { 3796496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 37975d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 37985d13a37bSArne Jansen struct btrfs_trans_handle *trans; 37995d13a37bSArne Jansen int ret; 38005d13a37bSArne Jansen int err; 38015d13a37bSArne Jansen 38025d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 38035d13a37bSArne Jansen return -EPERM; 38045d13a37bSArne Jansen 3805905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3806905b0ddaSMiao Xie if (ret) 3807905b0ddaSMiao Xie return ret; 38085d13a37bSArne Jansen 38095d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3810905b0ddaSMiao Xie if (IS_ERR(sa)) { 3811905b0ddaSMiao Xie ret = PTR_ERR(sa); 3812905b0ddaSMiao Xie goto drop_write; 3813905b0ddaSMiao Xie } 38145d13a37bSArne Jansen 3815d86e56cfSMiao Xie if (!sa->qgroupid) { 3816d86e56cfSMiao Xie ret = -EINVAL; 3817d86e56cfSMiao Xie goto out; 3818d86e56cfSMiao Xie } 3819d86e56cfSMiao Xie 38205d13a37bSArne Jansen trans = btrfs_join_transaction(root); 38215d13a37bSArne Jansen if (IS_ERR(trans)) { 38225d13a37bSArne Jansen ret = PTR_ERR(trans); 38235d13a37bSArne Jansen goto out; 38245d13a37bSArne Jansen } 38255d13a37bSArne Jansen 38265d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 38275d13a37bSArne Jansen if (sa->create) { 38285d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 38295d13a37bSArne Jansen NULL); 38305d13a37bSArne Jansen } else { 38315d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 38325d13a37bSArne Jansen } 38335d13a37bSArne Jansen 38345d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 38355d13a37bSArne Jansen if (err && !ret) 38365d13a37bSArne Jansen ret = err; 38375d13a37bSArne Jansen 38385d13a37bSArne Jansen out: 38395d13a37bSArne Jansen kfree(sa); 3840905b0ddaSMiao Xie drop_write: 3841905b0ddaSMiao Xie mnt_drop_write_file(file); 38425d13a37bSArne Jansen return ret; 38435d13a37bSArne Jansen } 38445d13a37bSArne Jansen 3845905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 38465d13a37bSArne Jansen { 3847496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 38485d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 38495d13a37bSArne Jansen struct btrfs_trans_handle *trans; 38505d13a37bSArne Jansen int ret; 38515d13a37bSArne Jansen int err; 38525d13a37bSArne Jansen u64 qgroupid; 38535d13a37bSArne Jansen 38545d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 38555d13a37bSArne Jansen return -EPERM; 38565d13a37bSArne Jansen 3857905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3858905b0ddaSMiao Xie if (ret) 3859905b0ddaSMiao Xie return ret; 38605d13a37bSArne Jansen 38615d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3862905b0ddaSMiao Xie if (IS_ERR(sa)) { 3863905b0ddaSMiao Xie ret = PTR_ERR(sa); 3864905b0ddaSMiao Xie goto drop_write; 3865905b0ddaSMiao Xie } 38665d13a37bSArne Jansen 38675d13a37bSArne Jansen trans = btrfs_join_transaction(root); 38685d13a37bSArne Jansen if (IS_ERR(trans)) { 38695d13a37bSArne Jansen ret = PTR_ERR(trans); 38705d13a37bSArne Jansen goto out; 38715d13a37bSArne Jansen } 38725d13a37bSArne Jansen 38735d13a37bSArne Jansen qgroupid = sa->qgroupid; 38745d13a37bSArne Jansen if (!qgroupid) { 38755d13a37bSArne Jansen /* take the current subvol as qgroup */ 38765d13a37bSArne Jansen qgroupid = root->root_key.objectid; 38775d13a37bSArne Jansen } 38785d13a37bSArne Jansen 38795d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 38805d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 38815d13a37bSArne Jansen 38825d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 38835d13a37bSArne Jansen if (err && !ret) 38845d13a37bSArne Jansen ret = err; 38855d13a37bSArne Jansen 38865d13a37bSArne Jansen out: 38875d13a37bSArne Jansen kfree(sa); 3888905b0ddaSMiao Xie drop_write: 3889905b0ddaSMiao Xie mnt_drop_write_file(file); 38905d13a37bSArne Jansen return ret; 38915d13a37bSArne Jansen } 38925d13a37bSArne Jansen 38932f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 38942f232036SJan Schmidt { 38956d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 38962f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 38972f232036SJan Schmidt int ret; 38982f232036SJan Schmidt 38992f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 39002f232036SJan Schmidt return -EPERM; 39012f232036SJan Schmidt 39022f232036SJan Schmidt ret = mnt_want_write_file(file); 39032f232036SJan Schmidt if (ret) 39042f232036SJan Schmidt return ret; 39052f232036SJan Schmidt 39062f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 39072f232036SJan Schmidt if (IS_ERR(qsa)) { 39082f232036SJan Schmidt ret = PTR_ERR(qsa); 39092f232036SJan Schmidt goto drop_write; 39102f232036SJan Schmidt } 39112f232036SJan Schmidt 39122f232036SJan Schmidt if (qsa->flags) { 39132f232036SJan Schmidt ret = -EINVAL; 39142f232036SJan Schmidt goto out; 39152f232036SJan Schmidt } 39162f232036SJan Schmidt 39172f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 39182f232036SJan Schmidt 39192f232036SJan Schmidt out: 39202f232036SJan Schmidt kfree(qsa); 39212f232036SJan Schmidt drop_write: 39222f232036SJan Schmidt mnt_drop_write_file(file); 39232f232036SJan Schmidt return ret; 39242f232036SJan Schmidt } 39252f232036SJan Schmidt 39262f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 39272f232036SJan Schmidt { 39286d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 39292f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 39302f232036SJan Schmidt int ret = 0; 39312f232036SJan Schmidt 39322f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 39332f232036SJan Schmidt return -EPERM; 39342f232036SJan Schmidt 39352f232036SJan Schmidt qsa = kzalloc(sizeof(*qsa), GFP_NOFS); 39362f232036SJan Schmidt if (!qsa) 39372f232036SJan Schmidt return -ENOMEM; 39382f232036SJan Schmidt 39392f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 39402f232036SJan Schmidt qsa->flags = 1; 39412f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 39422f232036SJan Schmidt } 39432f232036SJan Schmidt 39442f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 39452f232036SJan Schmidt ret = -EFAULT; 39462f232036SJan Schmidt 39472f232036SJan Schmidt kfree(qsa); 39482f232036SJan Schmidt return ret; 39492f232036SJan Schmidt } 39502f232036SJan Schmidt 395157254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 395257254b6eSJan Schmidt { 395357254b6eSJan Schmidt struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 395457254b6eSJan Schmidt 395557254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 395657254b6eSJan Schmidt return -EPERM; 395757254b6eSJan Schmidt 395857254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 395957254b6eSJan Schmidt } 396057254b6eSJan Schmidt 39618ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file, 39628ea05e3aSAlexander Block void __user *arg) 39638ea05e3aSAlexander Block { 39648ea05e3aSAlexander Block struct btrfs_ioctl_received_subvol_args *sa = NULL; 3965496ad9aaSAl Viro struct inode *inode = file_inode(file); 39668ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 39678ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 39688ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 39698ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 39708ea05e3aSAlexander Block int ret = 0; 39718ea05e3aSAlexander Block 39728ea05e3aSAlexander Block ret = mnt_want_write_file(file); 39738ea05e3aSAlexander Block if (ret < 0) 39748ea05e3aSAlexander Block return ret; 39758ea05e3aSAlexander Block 39768ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 39778ea05e3aSAlexander Block 39788ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 39798ea05e3aSAlexander Block ret = -EINVAL; 39808ea05e3aSAlexander Block goto out; 39818ea05e3aSAlexander Block } 39828ea05e3aSAlexander Block 39838ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 39848ea05e3aSAlexander Block ret = -EROFS; 39858ea05e3aSAlexander Block goto out; 39868ea05e3aSAlexander Block } 39878ea05e3aSAlexander Block 39888ea05e3aSAlexander Block if (!inode_owner_or_capable(inode)) { 39898ea05e3aSAlexander Block ret = -EACCES; 39908ea05e3aSAlexander Block goto out; 39918ea05e3aSAlexander Block } 39928ea05e3aSAlexander Block 39938ea05e3aSAlexander Block sa = memdup_user(arg, sizeof(*sa)); 39948ea05e3aSAlexander Block if (IS_ERR(sa)) { 39958ea05e3aSAlexander Block ret = PTR_ERR(sa); 39968ea05e3aSAlexander Block sa = NULL; 39978ea05e3aSAlexander Block goto out; 39988ea05e3aSAlexander Block } 39998ea05e3aSAlexander Block 40008ea05e3aSAlexander Block trans = btrfs_start_transaction(root, 1); 40018ea05e3aSAlexander Block if (IS_ERR(trans)) { 40028ea05e3aSAlexander Block ret = PTR_ERR(trans); 40038ea05e3aSAlexander Block trans = NULL; 40048ea05e3aSAlexander Block goto out; 40058ea05e3aSAlexander Block } 40068ea05e3aSAlexander Block 40078ea05e3aSAlexander Block sa->rtransid = trans->transid; 40088ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 40098ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 40108ea05e3aSAlexander Block 40118ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 40128ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 40138ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 40143cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 40153cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 40163cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 40173cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 40188ea05e3aSAlexander Block 40198ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 40208ea05e3aSAlexander Block &root->root_key, &root->root_item); 40218ea05e3aSAlexander Block if (ret < 0) { 40228ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 40238ea05e3aSAlexander Block trans = NULL; 40248ea05e3aSAlexander Block goto out; 40258ea05e3aSAlexander Block } else { 40268ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 40278ea05e3aSAlexander Block if (ret < 0) 40288ea05e3aSAlexander Block goto out; 40298ea05e3aSAlexander Block } 40308ea05e3aSAlexander Block 40318ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 40328ea05e3aSAlexander Block if (ret) 40338ea05e3aSAlexander Block ret = -EFAULT; 40348ea05e3aSAlexander Block 40358ea05e3aSAlexander Block out: 40368ea05e3aSAlexander Block kfree(sa); 40378ea05e3aSAlexander Block up_write(&root->fs_info->subvol_sem); 40388ea05e3aSAlexander Block mnt_drop_write_file(file); 40398ea05e3aSAlexander Block return ret; 40408ea05e3aSAlexander Block } 40418ea05e3aSAlexander Block 4042867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 4043867ab667Sjeff.liu { 40446d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4045a1b83ac5SAnand Jain size_t len; 4046867ab667Sjeff.liu int ret; 4047a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4048a1b83ac5SAnand Jain 4049a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4050a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 4051a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4052a1b83ac5SAnand Jain 4053a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4054867ab667Sjeff.liu 4055867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 4056867ab667Sjeff.liu pr_warn("btrfs: label is too long, return the first %zu bytes\n", 4057867ab667Sjeff.liu --len); 4058867ab667Sjeff.liu } 4059867ab667Sjeff.liu 4060867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4061867ab667Sjeff.liu 4062867ab667Sjeff.liu return ret ? -EFAULT : 0; 4063867ab667Sjeff.liu } 4064867ab667Sjeff.liu 4065a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4066a8bfd4abSjeff.liu { 40676d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4068a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 4069a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4070a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4071a8bfd4abSjeff.liu int ret; 4072a8bfd4abSjeff.liu 4073a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4074a8bfd4abSjeff.liu return -EPERM; 4075a8bfd4abSjeff.liu 4076a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4077a8bfd4abSjeff.liu return -EFAULT; 4078a8bfd4abSjeff.liu 4079a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 4080a8bfd4abSjeff.liu pr_err("btrfs: unable to set label with more than %d bytes\n", 4081a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4082a8bfd4abSjeff.liu return -EINVAL; 4083a8bfd4abSjeff.liu } 4084a8bfd4abSjeff.liu 4085a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4086a8bfd4abSjeff.liu if (ret) 4087a8bfd4abSjeff.liu return ret; 4088a8bfd4abSjeff.liu 4089a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4090a8bfd4abSjeff.liu if (IS_ERR(trans)) { 4091a8bfd4abSjeff.liu ret = PTR_ERR(trans); 4092a8bfd4abSjeff.liu goto out_unlock; 4093a8bfd4abSjeff.liu } 4094a8bfd4abSjeff.liu 4095a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4096a8bfd4abSjeff.liu strcpy(super_block->label, label); 4097a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4098a8bfd4abSjeff.liu ret = btrfs_end_transaction(trans, root); 4099a8bfd4abSjeff.liu 4100a8bfd4abSjeff.liu out_unlock: 4101a8bfd4abSjeff.liu mnt_drop_write_file(file); 4102a8bfd4abSjeff.liu return ret; 4103a8bfd4abSjeff.liu } 4104a8bfd4abSjeff.liu 4105f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 4106f46b5a66SChristoph Hellwig cmd, unsigned long arg) 4107f46b5a66SChristoph Hellwig { 4108496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 41094bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 4110f46b5a66SChristoph Hellwig 4111f46b5a66SChristoph Hellwig switch (cmd) { 41126cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 41136cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 41146cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 41156cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 41166cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 41176cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 4118f7039b1dSLi Dongyang case FITRIM: 4119f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 4120f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 4121fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 4122fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 4123fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 41243de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 4125fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 41266f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 41276f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 412876dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 412976dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 41300caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 41310caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 41320caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 41330caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 41346ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 41356ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 4136f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 41371e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 41381e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 41391e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 4140f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 4141198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 4142f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 41434bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 4144f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 4145da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 4146475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 4147475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 4148475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 4149475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 4150f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 41519ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 4152f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 4153c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 4154c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 41557a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 4156f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 4157f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 4158f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 4159f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 4160ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 4161ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 4162ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 4163ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 4164d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 4165d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 4166d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 4167d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 41681406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 41691406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 4170f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 4171f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 4172f46b5a66SChristoph Hellwig return 0; 417346204592SSage Weil case BTRFS_IOC_START_SYNC: 41749a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 417546204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 41769a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 4177475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 4178b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 4179475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 4180475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 4181475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 4182475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 4183c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 41849ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 4185837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 4186837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 418719a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 418819a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 41898ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 41908ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 419131db9f7cSAlexander Block case BTRFS_IOC_SEND: 419231db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 4193c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 4194b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 41955d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 4196905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 41975d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 4198905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 41995d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 4200905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 42015d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 4202905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 42032f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 42042f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 42052f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 42062f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 420757254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 420857254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 42093f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 42103f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 4211867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 4212867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 4213a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 4214a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 4215f46b5a66SChristoph Hellwig } 4216f46b5a66SChristoph Hellwig 4217f46b5a66SChristoph Hellwig return -ENOTTY; 4218f46b5a66SChristoph Hellwig } 4219