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 { 1556cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); 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 { 1806cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 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 { 3136cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 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 366cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root, 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; 37776dda93cSYan, Zheng struct btrfs_root *new_root; 3782fbe8c8aSAl Viro struct dentry *parent = dentry->d_parent; 3796a912213SJosef Bacik struct inode *dir; 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; 3868ea05e3aSAlexander Block uuid_le new_uuid; 387f46b5a66SChristoph Hellwig 388581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 3892fbe8c8aSAl Viro if (ret) 390a22285a6SYan, Zheng return ret; 3916a912213SJosef Bacik 3926a912213SJosef Bacik dir = parent->d_inode; 3936a912213SJosef Bacik 3949ed74f2dSJosef Bacik /* 3959ed74f2dSJosef Bacik * 1 - inode item 3969ed74f2dSJosef Bacik * 2 - refs 3979ed74f2dSJosef Bacik * 1 - root item 3989ed74f2dSJosef Bacik * 2 - dir items 3999ed74f2dSJosef Bacik */ 400a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 4012fbe8c8aSAl Viro if (IS_ERR(trans)) 402a22285a6SYan, Zheng return PTR_ERR(trans); 403f46b5a66SChristoph Hellwig 4048696c533SMiao Xie ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 4056f72c7e2SArne Jansen if (ret) 4066f72c7e2SArne Jansen goto fail; 4076f72c7e2SArne Jansen 4085d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 4095581a51aSJan Schmidt 0, objectid, NULL, 0, 0, 0); 4108e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4118e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4128e8a1e31SJosef Bacik goto fail; 4138e8a1e31SJosef Bacik } 414f46b5a66SChristoph Hellwig 4155d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 416f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 417f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4185d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 419f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 420f46b5a66SChristoph Hellwig 421f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 422f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 423f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 4245d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 4255d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 4265d4f98a2SYan Zheng BTRFS_UUID_SIZE); 427f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 428f46b5a66SChristoph Hellwig 4298ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 4308ea05e3aSAlexander Block 431f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 432f46b5a66SChristoph Hellwig inode_item->generation = cpu_to_le64(1); 433f46b5a66SChristoph Hellwig inode_item->size = cpu_to_le64(3); 434f46b5a66SChristoph Hellwig inode_item->nlink = cpu_to_le32(1); 435a76a3cd4SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 436f46b5a66SChristoph Hellwig inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 437f46b5a66SChristoph Hellwig 43808fe4db1SLi Zefan root_item.flags = 0; 43908fe4db1SLi Zefan root_item.byte_limit = 0; 44008fe4db1SLi Zefan inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); 44108fe4db1SLi Zefan 442f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 44384234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 444f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 445f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 44686b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 44780ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 448f46b5a66SChristoph Hellwig 4498ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 4508ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 4518ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 4528ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 4538ea05e3aSAlexander Block root_item.otime.sec = cpu_to_le64(cur_time.tv_sec); 454dadd1105SDan Carpenter root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec); 4558ea05e3aSAlexander Block root_item.ctime = root_item.otime; 4568ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 4578ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 458f46b5a66SChristoph Hellwig 459925baeddSChris Mason btrfs_tree_unlock(leaf); 460f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 461f46b5a66SChristoph Hellwig leaf = NULL; 462f46b5a66SChristoph Hellwig 463f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 464f46b5a66SChristoph Hellwig 465f46b5a66SChristoph Hellwig key.objectid = objectid; 4665d4f98a2SYan Zheng key.offset = 0; 467f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 468f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 469f46b5a66SChristoph Hellwig &root_item); 470f46b5a66SChristoph Hellwig if (ret) 471f46b5a66SChristoph Hellwig goto fail; 472f46b5a66SChristoph Hellwig 47376dda93cSYan, Zheng key.offset = (u64)-1; 47476dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 47579787eaaSJeff Mahoney if (IS_ERR(new_root)) { 47679787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); 47779787eaaSJeff Mahoney ret = PTR_ERR(new_root); 47879787eaaSJeff Mahoney goto fail; 47979787eaaSJeff Mahoney } 48076dda93cSYan, Zheng 48176dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 48276dda93cSYan, Zheng 483d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 484ce598979SMark Fasheh if (ret) { 485ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 48679787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 487ce598979SMark Fasheh goto fail; 488ce598979SMark Fasheh } 489ce598979SMark Fasheh 490f46b5a66SChristoph Hellwig /* 491f46b5a66SChristoph Hellwig * insert the directory item 492f46b5a66SChristoph Hellwig */ 4933de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 49479787eaaSJeff Mahoney if (ret) { 49579787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 49679787eaaSJeff Mahoney goto fail; 49779787eaaSJeff Mahoney } 4983de4586cSChris Mason 4993de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 50016cdcec7SMiao Xie name, namelen, dir, &key, 5013de4586cSChris Mason BTRFS_FT_DIR, index); 50279787eaaSJeff Mahoney if (ret) { 50379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 504f46b5a66SChristoph Hellwig goto fail; 50579787eaaSJeff Mahoney } 5060660b5afSChris Mason 50752c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 50852c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 50952c26179SYan Zheng BUG_ON(ret); 51052c26179SYan Zheng 5110660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5124df27c4dSYan, Zheng objectid, root->root_key.objectid, 51333345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 51476dda93cSYan, Zheng 5150660b5afSChris Mason BUG_ON(ret); 5160660b5afSChris Mason 517f46b5a66SChristoph Hellwig fail: 51872fd032eSSage Weil if (async_transid) { 51972fd032eSSage Weil *async_transid = trans->transid; 52072fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 52172fd032eSSage Weil } else { 52276dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 52372fd032eSSage Weil } 524f46b5a66SChristoph Hellwig if (err && !ret) 525f46b5a66SChristoph Hellwig ret = err; 5261a65e24bSChris Mason 5271a65e24bSChris Mason if (!ret) 5281a65e24bSChris Mason d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 5291a65e24bSChris Mason 530f46b5a66SChristoph Hellwig return ret; 531f46b5a66SChristoph Hellwig } 532f46b5a66SChristoph Hellwig 53372fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 534b83cc969SLi Zefan char *name, int namelen, u64 *async_transid, 5358696c533SMiao Xie bool readonly, struct btrfs_qgroup_inherit *inherit) 536f46b5a66SChristoph Hellwig { 5372e4bfab9SYan, Zheng struct inode *inode; 538f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 539f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 5402e4bfab9SYan, Zheng int ret; 541f46b5a66SChristoph Hellwig 542f46b5a66SChristoph Hellwig if (!root->ref_cows) 543f46b5a66SChristoph Hellwig return -EINVAL; 544f46b5a66SChristoph Hellwig 545a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 546a22285a6SYan, Zheng if (!pending_snapshot) 547a22285a6SYan, Zheng return -ENOMEM; 548a22285a6SYan, Zheng 54966d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 55066d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 551a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 552a22285a6SYan, Zheng pending_snapshot->root = root; 553b83cc969SLi Zefan pending_snapshot->readonly = readonly; 5548696c533SMiao Xie pending_snapshot->inherit = inherit; 555a22285a6SYan, Zheng 55648c03c4bSMiao Xie trans = btrfs_start_transaction(root->fs_info->extent_root, 6); 557a22285a6SYan, Zheng if (IS_ERR(trans)) { 558a22285a6SYan, Zheng ret = PTR_ERR(trans); 559a22285a6SYan, Zheng goto fail; 560a22285a6SYan, Zheng } 561a22285a6SYan, Zheng 562a22285a6SYan, Zheng ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); 563a22285a6SYan, Zheng BUG_ON(ret); 564a22285a6SYan, Zheng 5658351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 566a22285a6SYan, Zheng list_add(&pending_snapshot->list, 567a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 5688351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 56972fd032eSSage Weil if (async_transid) { 57072fd032eSSage Weil *async_transid = trans->transid; 57172fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 57272fd032eSSage Weil root->fs_info->extent_root, 1); 57372fd032eSSage Weil } else { 57472fd032eSSage Weil ret = btrfs_commit_transaction(trans, 57572fd032eSSage Weil root->fs_info->extent_root); 57672fd032eSSage Weil } 577109f2365SLiu Bo if (ret) { 578109f2365SLiu Bo /* cleanup_transaction has freed this for us */ 579109f2365SLiu Bo if (trans->aborted) 580109f2365SLiu Bo pending_snapshot = NULL; 581c37b2b62SJosef Bacik goto fail; 582109f2365SLiu Bo } 583a22285a6SYan, Zheng 584a22285a6SYan, Zheng ret = pending_snapshot->error; 585f46b5a66SChristoph Hellwig if (ret) 5862e4bfab9SYan, Zheng goto fail; 587f46b5a66SChristoph Hellwig 58866b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 58966b4ffd1SJosef Bacik if (ret) 59066b4ffd1SJosef Bacik goto fail; 591f46b5a66SChristoph Hellwig 5922fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 5932e4bfab9SYan, Zheng if (IS_ERR(inode)) { 5942e4bfab9SYan, Zheng ret = PTR_ERR(inode); 5952e4bfab9SYan, Zheng goto fail; 5962e4bfab9SYan, Zheng } 5972e4bfab9SYan, Zheng BUG_ON(!inode); 5982e4bfab9SYan, Zheng d_instantiate(dentry, inode); 5992e4bfab9SYan, Zheng ret = 0; 6002e4bfab9SYan, Zheng fail: 601a22285a6SYan, Zheng kfree(pending_snapshot); 602f46b5a66SChristoph Hellwig return ret; 603f46b5a66SChristoph Hellwig } 604f46b5a66SChristoph Hellwig 6054260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 6064260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 6074260f7c7SSage Weil * minimal. 6084260f7c7SSage Weil */ 6094260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 6104260f7c7SSage Weil { 6112f2f43d3SEric W. Biederman kuid_t fsuid = current_fsuid(); 6124260f7c7SSage Weil 6134260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 6144260f7c7SSage Weil return 0; 6152f2f43d3SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 6164260f7c7SSage Weil return 0; 6172f2f43d3SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 6184260f7c7SSage Weil return 0; 6194260f7c7SSage Weil return !capable(CAP_FOWNER); 6204260f7c7SSage Weil } 6214260f7c7SSage Weil 6224260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 6234260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 6244260f7c7SSage Weil * whether the type of victim is right. 6254260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 6264260f7c7SSage Weil * 2. We should have write and exec permissions on dir 6274260f7c7SSage Weil * 3. We can't remove anything from append-only dir 6284260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 6294260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 6304260f7c7SSage Weil * a. be owner of dir, or 6314260f7c7SSage Weil * b. be owner of victim, or 6324260f7c7SSage Weil * c. have CAP_FOWNER capability 6334260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 6344260f7c7SSage Weil * links pointing to it. 6354260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 6364260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 6374260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 6384260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 6394260f7c7SSage Weil * nfs_async_unlink(). 6404260f7c7SSage Weil */ 6414260f7c7SSage Weil 6424260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 6434260f7c7SSage Weil { 6444260f7c7SSage Weil int error; 6454260f7c7SSage Weil 6464260f7c7SSage Weil if (!victim->d_inode) 6474260f7c7SSage Weil return -ENOENT; 6484260f7c7SSage Weil 6494260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 6504fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 6514260f7c7SSage Weil 6524260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 6534260f7c7SSage Weil if (error) 6544260f7c7SSage Weil return error; 6554260f7c7SSage Weil if (IS_APPEND(dir)) 6564260f7c7SSage Weil return -EPERM; 6574260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 6584260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 6594260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 6604260f7c7SSage Weil return -EPERM; 6614260f7c7SSage Weil if (isdir) { 6624260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 6634260f7c7SSage Weil return -ENOTDIR; 6644260f7c7SSage Weil if (IS_ROOT(victim)) 6654260f7c7SSage Weil return -EBUSY; 6664260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 6674260f7c7SSage Weil return -EISDIR; 6684260f7c7SSage Weil if (IS_DEADDIR(dir)) 6694260f7c7SSage Weil return -ENOENT; 6704260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 6714260f7c7SSage Weil return -EBUSY; 6724260f7c7SSage Weil return 0; 6734260f7c7SSage Weil } 6744260f7c7SSage Weil 675cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 676cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 677cb8e7090SChristoph Hellwig { 678cb8e7090SChristoph Hellwig if (child->d_inode) 679cb8e7090SChristoph Hellwig return -EEXIST; 680cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 681cb8e7090SChristoph Hellwig return -ENOENT; 682cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 683cb8e7090SChristoph Hellwig } 684cb8e7090SChristoph Hellwig 685cb8e7090SChristoph Hellwig /* 686cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 687cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 688cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 689cb8e7090SChristoph Hellwig */ 69076dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 69176dda93cSYan, Zheng char *name, int namelen, 69272fd032eSSage Weil struct btrfs_root *snap_src, 6936f72c7e2SArne Jansen u64 *async_transid, bool readonly, 6948696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 695cb8e7090SChristoph Hellwig { 69676dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 697cb8e7090SChristoph Hellwig struct dentry *dentry; 698cb8e7090SChristoph Hellwig int error; 699cb8e7090SChristoph Hellwig 70076dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 701cb8e7090SChristoph Hellwig 702cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 703cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 704cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 705cb8e7090SChristoph Hellwig goto out_unlock; 706cb8e7090SChristoph Hellwig 707cb8e7090SChristoph Hellwig error = -EEXIST; 708cb8e7090SChristoph Hellwig if (dentry->d_inode) 709cb8e7090SChristoph Hellwig goto out_dput; 710cb8e7090SChristoph Hellwig 71176dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 712cb8e7090SChristoph Hellwig if (error) 713a874a63eSLiu Bo goto out_dput; 714cb8e7090SChristoph Hellwig 7159c52057cSChris Mason /* 7169c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 7179c52057cSChris Mason * check for them now when we can safely fail 7189c52057cSChris Mason */ 7199c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 7209c52057cSChris Mason dir->i_ino, name, 7219c52057cSChris Mason namelen); 7229c52057cSChris Mason if (error) 7239c52057cSChris Mason goto out_dput; 7249c52057cSChris Mason 72576dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 72676dda93cSYan, Zheng 72776dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 72876dda93cSYan, Zheng goto out_up_read; 72976dda93cSYan, Zheng 7303de4586cSChris Mason if (snap_src) { 7316f72c7e2SArne Jansen error = create_snapshot(snap_src, dentry, name, namelen, 7326f72c7e2SArne Jansen async_transid, readonly, inherit); 7333de4586cSChris Mason } else { 73476dda93cSYan, Zheng error = create_subvol(BTRFS_I(dir)->root, dentry, 7356f72c7e2SArne Jansen name, namelen, async_transid, inherit); 7363de4586cSChris Mason } 73776dda93cSYan, Zheng if (!error) 73876dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 73976dda93cSYan, Zheng out_up_read: 74076dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 741cb8e7090SChristoph Hellwig out_dput: 742cb8e7090SChristoph Hellwig dput(dentry); 743cb8e7090SChristoph Hellwig out_unlock: 74476dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 745cb8e7090SChristoph Hellwig return error; 746cb8e7090SChristoph Hellwig } 747cb8e7090SChristoph Hellwig 7484cb5300bSChris Mason /* 7494cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 7504cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 7514cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 7524cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 7534cb5300bSChris Mason * part of the file 7544cb5300bSChris Mason */ 7554cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 7564cb5300bSChris Mason { 7574cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 7584cb5300bSChris Mason struct extent_map *em = NULL; 7594cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 7604cb5300bSChris Mason u64 end; 7614cb5300bSChris Mason 7624cb5300bSChris Mason read_lock(&em_tree->lock); 7634cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 7644cb5300bSChris Mason read_unlock(&em_tree->lock); 7654cb5300bSChris Mason 7664cb5300bSChris Mason if (em) { 7674cb5300bSChris Mason end = extent_map_end(em); 7684cb5300bSChris Mason free_extent_map(em); 7694cb5300bSChris Mason if (end - offset > thresh) 7704cb5300bSChris Mason return 0; 7714cb5300bSChris Mason } 7724cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 7734cb5300bSChris Mason thresh /= 2; 7744cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 7754cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 7764cb5300bSChris Mason if (end >= thresh) 7774cb5300bSChris Mason return 0; 7784cb5300bSChris Mason return 1; 7794cb5300bSChris Mason } 7804cb5300bSChris Mason 7814cb5300bSChris Mason /* 7824cb5300bSChris Mason * helper function to walk through a file and find extents 7834cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 7844cb5300bSChris Mason * 7854cb5300bSChris Mason * This is used by the defragging code to find new and small 7864cb5300bSChris Mason * extents 7874cb5300bSChris Mason */ 7884cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 7894cb5300bSChris Mason struct inode *inode, u64 newer_than, 7904cb5300bSChris Mason u64 *off, int thresh) 7914cb5300bSChris Mason { 7924cb5300bSChris Mason struct btrfs_path *path; 7934cb5300bSChris Mason struct btrfs_key min_key; 7944cb5300bSChris Mason struct btrfs_key max_key; 7954cb5300bSChris Mason struct extent_buffer *leaf; 7964cb5300bSChris Mason struct btrfs_file_extent_item *extent; 7974cb5300bSChris Mason int type; 7984cb5300bSChris Mason int ret; 799a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 8004cb5300bSChris Mason 8014cb5300bSChris Mason path = btrfs_alloc_path(); 8024cb5300bSChris Mason if (!path) 8034cb5300bSChris Mason return -ENOMEM; 8044cb5300bSChris Mason 805a4689d2bSDavid Sterba min_key.objectid = ino; 8064cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 8074cb5300bSChris Mason min_key.offset = *off; 8084cb5300bSChris Mason 809a4689d2bSDavid Sterba max_key.objectid = ino; 8104cb5300bSChris Mason max_key.type = (u8)-1; 8114cb5300bSChris Mason max_key.offset = (u64)-1; 8124cb5300bSChris Mason 8134cb5300bSChris Mason path->keep_locks = 1; 8144cb5300bSChris Mason 8154cb5300bSChris Mason while(1) { 8164cb5300bSChris Mason ret = btrfs_search_forward(root, &min_key, &max_key, 817de78b51aSEric Sandeen path, newer_than); 8184cb5300bSChris Mason if (ret != 0) 8194cb5300bSChris Mason goto none; 820a4689d2bSDavid Sterba if (min_key.objectid != ino) 8214cb5300bSChris Mason goto none; 8224cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 8234cb5300bSChris Mason goto none; 8244cb5300bSChris Mason 8254cb5300bSChris Mason leaf = path->nodes[0]; 8264cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 8274cb5300bSChris Mason struct btrfs_file_extent_item); 8284cb5300bSChris Mason 8294cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 8304cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 8314cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 8324cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 8334cb5300bSChris Mason *off = min_key.offset; 8344cb5300bSChris Mason btrfs_free_path(path); 8354cb5300bSChris Mason return 0; 8364cb5300bSChris Mason } 8374cb5300bSChris Mason 8384cb5300bSChris Mason if (min_key.offset == (u64)-1) 8394cb5300bSChris Mason goto none; 8404cb5300bSChris Mason 8414cb5300bSChris Mason min_key.offset++; 8424cb5300bSChris Mason btrfs_release_path(path); 8434cb5300bSChris Mason } 8444cb5300bSChris Mason none: 8454cb5300bSChris Mason btrfs_free_path(path); 8464cb5300bSChris Mason return -ENOENT; 8474cb5300bSChris Mason } 8484cb5300bSChris Mason 8496c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 85017ce6ef8SLiu Bo { 85117ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 852940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8536c282eb4SLi Zefan struct extent_map *em; 8546c282eb4SLi Zefan u64 len = PAGE_CACHE_SIZE; 855940100a4SChris Mason 856940100a4SChris Mason /* 857940100a4SChris Mason * hopefully we have this extent in the tree already, try without 858940100a4SChris Mason * the full extent lock 859940100a4SChris Mason */ 860940100a4SChris Mason read_lock(&em_tree->lock); 861940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 862940100a4SChris Mason read_unlock(&em_tree->lock); 863940100a4SChris Mason 864940100a4SChris Mason if (!em) { 865940100a4SChris Mason /* get the big lock and read metadata off disk */ 866d0082371SJeff Mahoney lock_extent(io_tree, start, start + len - 1); 867940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 868d0082371SJeff Mahoney unlock_extent(io_tree, start, start + len - 1); 869940100a4SChris Mason 8706cf8bfbfSDan Carpenter if (IS_ERR(em)) 8716c282eb4SLi Zefan return NULL; 872940100a4SChris Mason } 873940100a4SChris Mason 8746c282eb4SLi Zefan return em; 8756c282eb4SLi Zefan } 8766c282eb4SLi Zefan 8776c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 8786c282eb4SLi Zefan { 8796c282eb4SLi Zefan struct extent_map *next; 8806c282eb4SLi Zefan bool ret = true; 8816c282eb4SLi Zefan 8826c282eb4SLi Zefan /* this is the last extent */ 8836c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 8846c282eb4SLi Zefan return false; 8856c282eb4SLi Zefan 8866c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 8876c282eb4SLi Zefan if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 8886c282eb4SLi Zefan ret = false; 8896c282eb4SLi Zefan 8906c282eb4SLi Zefan free_extent_map(next); 8916c282eb4SLi Zefan return ret; 8926c282eb4SLi Zefan } 8936c282eb4SLi Zefan 8946c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh, 895a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 896a43a2111SAndrew Mahone int compress) 8976c282eb4SLi Zefan { 8986c282eb4SLi Zefan struct extent_map *em; 8996c282eb4SLi Zefan int ret = 1; 9006c282eb4SLi Zefan bool next_mergeable = true; 9016c282eb4SLi Zefan 9026c282eb4SLi Zefan /* 9036c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 9046c282eb4SLi Zefan * defragging it 9056c282eb4SLi Zefan */ 9066c282eb4SLi Zefan if (start < *defrag_end) 9076c282eb4SLi Zefan return 1; 9086c282eb4SLi Zefan 9096c282eb4SLi Zefan *skip = 0; 9106c282eb4SLi Zefan 9116c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 9126c282eb4SLi Zefan if (!em) 9136c282eb4SLi Zefan return 0; 9146c282eb4SLi Zefan 915940100a4SChris Mason /* this will cover holes, and inline extents */ 91617ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 917940100a4SChris Mason ret = 0; 91817ce6ef8SLiu Bo goto out; 91917ce6ef8SLiu Bo } 92017ce6ef8SLiu Bo 9216c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 922940100a4SChris Mason 923940100a4SChris Mason /* 9246c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 9256c282eb4SLi Zefan * real extent, don't bother defragging it 926940100a4SChris Mason */ 927a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 9286c282eb4SLi Zefan (em->len >= thresh || !next_mergeable)) 929940100a4SChris Mason ret = 0; 93017ce6ef8SLiu Bo out: 931940100a4SChris Mason /* 932940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 933940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 934940100a4SChris Mason * so that the next tiny extent will force a defrag. 935940100a4SChris Mason * 936940100a4SChris Mason * The end result of this is that tiny extents before a single big 937940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 938940100a4SChris Mason */ 939940100a4SChris Mason if (ret) { 940940100a4SChris Mason *defrag_end = extent_map_end(em); 941940100a4SChris Mason } else { 942940100a4SChris Mason *last_len = 0; 943940100a4SChris Mason *skip = extent_map_end(em); 944940100a4SChris Mason *defrag_end = 0; 945940100a4SChris Mason } 946940100a4SChris Mason 947940100a4SChris Mason free_extent_map(em); 948940100a4SChris Mason return ret; 949940100a4SChris Mason } 950940100a4SChris Mason 9514cb5300bSChris Mason /* 9524cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 9534cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 9544cb5300bSChris Mason * to COW and defrag. 9554cb5300bSChris Mason * 9564cb5300bSChris Mason * It also makes sure the delalloc code has enough 9574cb5300bSChris Mason * dirty data to avoid making new small extents as part 9584cb5300bSChris Mason * of the defrag 9594cb5300bSChris Mason * 9604cb5300bSChris Mason * It's a good idea to start RA on this range 9614cb5300bSChris Mason * before calling this. 9624cb5300bSChris Mason */ 9634cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 9644cb5300bSChris Mason struct page **pages, 9654cb5300bSChris Mason unsigned long start_index, 9664cb5300bSChris Mason int num_pages) 967f46b5a66SChristoph Hellwig { 9684cb5300bSChris Mason unsigned long file_end; 9694cb5300bSChris Mason u64 isize = i_size_read(inode); 970f46b5a66SChristoph Hellwig u64 page_start; 971f46b5a66SChristoph Hellwig u64 page_end; 9721f12bd06SLiu Bo u64 page_cnt; 9734cb5300bSChris Mason int ret; 9744cb5300bSChris Mason int i; 9754cb5300bSChris Mason int i_done; 9764cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 9774cb5300bSChris Mason struct extent_state *cached_state = NULL; 978600a45e1SMiao Xie struct extent_io_tree *tree; 9793b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 9804cb5300bSChris Mason 9814cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 9821f12bd06SLiu Bo if (!isize || start_index > file_end) 9831f12bd06SLiu Bo return 0; 9841f12bd06SLiu Bo 9851f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 9864cb5300bSChris Mason 9874cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 9881f12bd06SLiu Bo page_cnt << PAGE_CACHE_SHIFT); 9894cb5300bSChris Mason if (ret) 9904cb5300bSChris Mason return ret; 9914cb5300bSChris Mason i_done = 0; 992600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 9934cb5300bSChris Mason 9944cb5300bSChris Mason /* step one, lock all the pages */ 9951f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 9964cb5300bSChris Mason struct page *page; 997600a45e1SMiao Xie again: 998a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 9993b16a4e3SJosef Bacik start_index + i, mask); 10004cb5300bSChris Mason if (!page) 10014cb5300bSChris Mason break; 10024cb5300bSChris Mason 1003600a45e1SMiao Xie page_start = page_offset(page); 1004600a45e1SMiao Xie page_end = page_start + PAGE_CACHE_SIZE - 1; 1005600a45e1SMiao Xie while (1) { 1006d0082371SJeff Mahoney lock_extent(tree, page_start, page_end); 1007600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1008600a45e1SMiao Xie page_start); 1009d0082371SJeff Mahoney unlock_extent(tree, page_start, page_end); 1010600a45e1SMiao Xie if (!ordered) 1011600a45e1SMiao Xie break; 1012600a45e1SMiao Xie 1013600a45e1SMiao Xie unlock_page(page); 1014600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1015600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1016600a45e1SMiao Xie lock_page(page); 10171f12bd06SLiu Bo /* 10181f12bd06SLiu Bo * we unlocked the page above, so we need check if 10191f12bd06SLiu Bo * it was released or not. 10201f12bd06SLiu Bo */ 10211f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 10221f12bd06SLiu Bo unlock_page(page); 10231f12bd06SLiu Bo page_cache_release(page); 10241f12bd06SLiu Bo goto again; 10251f12bd06SLiu Bo } 1026600a45e1SMiao Xie } 1027600a45e1SMiao Xie 10284cb5300bSChris Mason if (!PageUptodate(page)) { 10294cb5300bSChris Mason btrfs_readpage(NULL, page); 10304cb5300bSChris Mason lock_page(page); 10314cb5300bSChris Mason if (!PageUptodate(page)) { 10324cb5300bSChris Mason unlock_page(page); 10334cb5300bSChris Mason page_cache_release(page); 10344cb5300bSChris Mason ret = -EIO; 10354cb5300bSChris Mason break; 10364cb5300bSChris Mason } 10374cb5300bSChris Mason } 1038600a45e1SMiao Xie 1039600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1040600a45e1SMiao Xie unlock_page(page); 1041600a45e1SMiao Xie page_cache_release(page); 1042600a45e1SMiao Xie goto again; 1043600a45e1SMiao Xie } 1044600a45e1SMiao Xie 10454cb5300bSChris Mason pages[i] = page; 10464cb5300bSChris Mason i_done++; 10474cb5300bSChris Mason } 10484cb5300bSChris Mason if (!i_done || ret) 10494cb5300bSChris Mason goto out; 10504cb5300bSChris Mason 10514cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 10524cb5300bSChris Mason goto out; 10534cb5300bSChris Mason 10544cb5300bSChris Mason /* 10554cb5300bSChris Mason * so now we have a nice long stream of locked 10564cb5300bSChris Mason * and up to date pages, lets wait on them 10574cb5300bSChris Mason */ 10584cb5300bSChris Mason for (i = 0; i < i_done; i++) 10594cb5300bSChris Mason wait_on_page_writeback(pages[i]); 10604cb5300bSChris Mason 10614cb5300bSChris Mason page_start = page_offset(pages[0]); 10624cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 10634cb5300bSChris Mason 10644cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1065d0082371SJeff Mahoney page_start, page_end - 1, 0, &cached_state); 10664cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 10674cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 10689e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 10699e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 10704cb5300bSChris Mason 10711f12bd06SLiu Bo if (i_done != page_cnt) { 10729e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 10739e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 10749e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 10754cb5300bSChris Mason btrfs_delalloc_release_space(inode, 10761f12bd06SLiu Bo (page_cnt - i_done) << PAGE_CACHE_SHIFT); 10774cb5300bSChris Mason } 10784cb5300bSChris Mason 10794cb5300bSChris Mason 10809e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 10819e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 10824cb5300bSChris Mason 10834cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 10844cb5300bSChris Mason page_start, page_end - 1, &cached_state, 10854cb5300bSChris Mason GFP_NOFS); 10864cb5300bSChris Mason 10874cb5300bSChris Mason for (i = 0; i < i_done; i++) { 10884cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 10894cb5300bSChris Mason ClearPageChecked(pages[i]); 10904cb5300bSChris Mason set_page_extent_mapped(pages[i]); 10914cb5300bSChris Mason set_page_dirty(pages[i]); 10924cb5300bSChris Mason unlock_page(pages[i]); 10934cb5300bSChris Mason page_cache_release(pages[i]); 10944cb5300bSChris Mason } 10954cb5300bSChris Mason return i_done; 10964cb5300bSChris Mason out: 10974cb5300bSChris Mason for (i = 0; i < i_done; i++) { 10984cb5300bSChris Mason unlock_page(pages[i]); 10994cb5300bSChris Mason page_cache_release(pages[i]); 11004cb5300bSChris Mason } 11011f12bd06SLiu Bo btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); 11024cb5300bSChris Mason return ret; 11034cb5300bSChris Mason 11044cb5300bSChris Mason } 11054cb5300bSChris Mason 11064cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 11074cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 11084cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 11094cb5300bSChris Mason { 11104cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 11114cb5300bSChris Mason struct file_ra_state *ra = NULL; 11124cb5300bSChris Mason unsigned long last_index; 1113151a31b2SLi Zefan u64 isize = i_size_read(inode); 1114940100a4SChris Mason u64 last_len = 0; 1115940100a4SChris Mason u64 skip = 0; 1116940100a4SChris Mason u64 defrag_end = 0; 11174cb5300bSChris Mason u64 newer_off = range->start; 1118f46b5a66SChristoph Hellwig unsigned long i; 1119008873eaSLi Zefan unsigned long ra_index = 0; 1120f46b5a66SChristoph Hellwig int ret; 11214cb5300bSChris Mason int defrag_count = 0; 11221a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 11234cb5300bSChris Mason int extent_thresh = range->extent_thresh; 1124008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1125008873eaSLi Zefan int cluster = max_cluster; 11264cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 11274cb5300bSChris Mason struct page **pages = NULL; 11284cb5300bSChris Mason 11294cb5300bSChris Mason if (extent_thresh == 0) 11304cb5300bSChris Mason extent_thresh = 256 * 1024; 11311a419d85SLi Zefan 11321a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 11331a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 11341a419d85SLi Zefan return -EINVAL; 11351a419d85SLi Zefan if (range->compress_type) 11361a419d85SLi Zefan compress_type = range->compress_type; 11371a419d85SLi Zefan } 1138f46b5a66SChristoph Hellwig 1139151a31b2SLi Zefan if (isize == 0) 1140940100a4SChris Mason return 0; 1141f46b5a66SChristoph Hellwig 11424cb5300bSChris Mason /* 11434cb5300bSChris Mason * if we were not given a file, allocate a readahead 11444cb5300bSChris Mason * context 11454cb5300bSChris Mason */ 11464cb5300bSChris Mason if (!file) { 11474cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 11484cb5300bSChris Mason if (!ra) 11494cb5300bSChris Mason return -ENOMEM; 11504cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 11514cb5300bSChris Mason } else { 11524cb5300bSChris Mason ra = &file->f_ra; 11534cb5300bSChris Mason } 11544cb5300bSChris Mason 1155008873eaSLi Zefan pages = kmalloc(sizeof(struct page *) * max_cluster, 11564cb5300bSChris Mason GFP_NOFS); 11574cb5300bSChris Mason if (!pages) { 11584cb5300bSChris Mason ret = -ENOMEM; 11594cb5300bSChris Mason goto out_ra; 11604cb5300bSChris Mason } 11614cb5300bSChris Mason 11624cb5300bSChris Mason /* find the last page to defrag */ 11631e701a32SChris Mason if (range->start + range->len > range->start) { 1164151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 11651e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 11661e701a32SChris Mason } else { 1167151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 11681e701a32SChris Mason } 11691e701a32SChris Mason 11704cb5300bSChris Mason if (newer_than) { 11714cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 11724cb5300bSChris Mason &newer_off, 64 * 1024); 11734cb5300bSChris Mason if (!ret) { 11744cb5300bSChris Mason range->start = newer_off; 11754cb5300bSChris Mason /* 11764cb5300bSChris Mason * we always align our defrag to help keep 11774cb5300bSChris Mason * the extents in the file evenly spaced 11784cb5300bSChris Mason */ 11794cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 11804cb5300bSChris Mason } else 11814cb5300bSChris Mason goto out_ra; 11824cb5300bSChris Mason } else { 11831e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 11844cb5300bSChris Mason } 11854cb5300bSChris Mason if (!max_to_defrag) 11867ec31b54SLiu Bo max_to_defrag = last_index + 1; 11874cb5300bSChris Mason 11882a0f7f57SLi Zefan /* 11892a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 11902a0f7f57SLi Zefan * written sequentially. 11912a0f7f57SLi Zefan */ 11922a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 11932a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 11942a0f7f57SLi Zefan 1195f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1196f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1197f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 11984cb5300bSChris Mason /* 11994cb5300bSChris Mason * make sure we stop running if someone unmounts 12004cb5300bSChris Mason * the FS 12014cb5300bSChris Mason */ 12024cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 12034cb5300bSChris Mason break; 12044cb5300bSChris Mason 1205210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1206210549ebSDavid Sterba printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); 1207210549ebSDavid Sterba ret = -EAGAIN; 1208210549ebSDavid Sterba break; 1209210549ebSDavid Sterba } 1210210549ebSDavid Sterba 121166c26892SLiu Bo if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 12126c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1213a43a2111SAndrew Mahone &defrag_end, range->flags & 1214a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1215940100a4SChris Mason unsigned long next; 1216940100a4SChris Mason /* 1217940100a4SChris Mason * the should_defrag function tells us how much to skip 1218940100a4SChris Mason * bump our counter by the suggested amount 1219940100a4SChris Mason */ 1220940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1221940100a4SChris Mason i = max(i + 1, next); 1222940100a4SChris Mason continue; 1223940100a4SChris Mason } 1224008873eaSLi Zefan 1225008873eaSLi Zefan if (!newer_than) { 1226008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1227008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1228008873eaSLi Zefan cluster = min(cluster, max_cluster); 1229008873eaSLi Zefan } else { 1230008873eaSLi Zefan cluster = max_cluster; 1231008873eaSLi Zefan } 1232008873eaSLi Zefan 12331e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 12341a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 1235940100a4SChris Mason 1236008873eaSLi Zefan if (i + cluster > ra_index) { 1237008873eaSLi Zefan ra_index = max(i, ra_index); 1238008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1239008873eaSLi Zefan cluster); 1240008873eaSLi Zefan ra_index += max_cluster; 1241008873eaSLi Zefan } 12424cb5300bSChris Mason 1243ecb8bea8SLiu Bo mutex_lock(&inode->i_mutex); 1244008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1245ecb8bea8SLiu Bo if (ret < 0) { 1246ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12474cb5300bSChris Mason goto out_ra; 1248ecb8bea8SLiu Bo } 12494cb5300bSChris Mason 12504cb5300bSChris Mason defrag_count += ret; 12514cb5300bSChris Mason balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); 1252ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 12534cb5300bSChris Mason 12544cb5300bSChris Mason if (newer_than) { 12554cb5300bSChris Mason if (newer_off == (u64)-1) 12564cb5300bSChris Mason break; 12574cb5300bSChris Mason 1258e1f041e1SLiu Bo if (ret > 0) 1259e1f041e1SLiu Bo i += ret; 1260e1f041e1SLiu Bo 12614cb5300bSChris Mason newer_off = max(newer_off + 1, 12624cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 12634cb5300bSChris Mason 12644cb5300bSChris Mason ret = find_new_extents(root, inode, 12654cb5300bSChris Mason newer_than, &newer_off, 12664cb5300bSChris Mason 64 * 1024); 12674cb5300bSChris Mason if (!ret) { 12684cb5300bSChris Mason range->start = newer_off; 12694cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 12704cb5300bSChris Mason } else { 12714cb5300bSChris Mason break; 1272940100a4SChris Mason } 12734cb5300bSChris Mason } else { 1274008873eaSLi Zefan if (ret > 0) { 1275cbcc8326SLi Zefan i += ret; 1276008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1277008873eaSLi Zefan } else { 1278940100a4SChris Mason i++; 1279008873eaSLi Zefan last_len = 0; 1280008873eaSLi Zefan } 1281f46b5a66SChristoph Hellwig } 12824cb5300bSChris Mason } 1283f46b5a66SChristoph Hellwig 12841e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 12851e701a32SChris Mason filemap_flush(inode->i_mapping); 12861e701a32SChris Mason 12871e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 12881e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 12891e701a32SChris Mason * we have to make sure the IO is actually started and that 12901e701a32SChris Mason * ordered extents get created before we return 12911e701a32SChris Mason */ 12921e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 12931e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 12941e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 12951e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 12961e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 12971e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 12981e701a32SChris Mason } 12991e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 13001e701a32SChris Mason 13011e701a32SChris Mason mutex_lock(&inode->i_mutex); 1302261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 13031e701a32SChris Mason mutex_unlock(&inode->i_mutex); 13041e701a32SChris Mason } 13051e701a32SChris Mason 13061a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 13072b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 13081a419d85SLi Zefan } 13091a419d85SLi Zefan 131060ccf82fSDiego Calleja ret = defrag_count; 1311940100a4SChris Mason 13124cb5300bSChris Mason out_ra: 13134cb5300bSChris Mason if (!file) 13144cb5300bSChris Mason kfree(ra); 13154cb5300bSChris Mason kfree(pages); 1316940100a4SChris Mason return ret; 1317f46b5a66SChristoph Hellwig } 1318f46b5a66SChristoph Hellwig 1319198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 132076dda93cSYan, Zheng void __user *arg) 1321f46b5a66SChristoph Hellwig { 1322f46b5a66SChristoph Hellwig u64 new_size; 1323f46b5a66SChristoph Hellwig u64 old_size; 1324f46b5a66SChristoph Hellwig u64 devid = 1; 1325198605a8SMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 1326f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1327f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1328f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1329f46b5a66SChristoph Hellwig char *sizestr; 1330f46b5a66SChristoph Hellwig char *devstr = NULL; 1331f46b5a66SChristoph Hellwig int ret = 0; 1332f46b5a66SChristoph Hellwig int mod = 0; 1333f46b5a66SChristoph Hellwig 1334e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1335e441d54dSChris Mason return -EPERM; 1336e441d54dSChris Mason 1337198605a8SMiao Xie ret = mnt_want_write_file(file); 1338198605a8SMiao Xie if (ret) 1339198605a8SMiao Xie return ret; 1340198605a8SMiao Xie 13415ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 13425ac00addSStefan Behrens 1)) { 13435ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 134497547676SMiao Xie mnt_drop_write_file(file); 13452c0c9da0SIlya Dryomov return -EINVAL; 1346c9e9f97bSIlya Dryomov } 1347c9e9f97bSIlya Dryomov 13485ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1349dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1350c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1351c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1352c9e9f97bSIlya Dryomov goto out; 1353c9e9f97bSIlya Dryomov } 13545516e595SMark Fasheh 13555516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1356f46b5a66SChristoph Hellwig 1357f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1358f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1359f46b5a66SChristoph Hellwig if (devstr) { 1360f46b5a66SChristoph Hellwig char *end; 1361f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1362f46b5a66SChristoph Hellwig *devstr = '\0'; 1363f46b5a66SChristoph Hellwig devstr = vol_args->name; 1364f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 1365dfd79829SMiao Xie if (!devid) { 1366dfd79829SMiao Xie ret = -EINVAL; 1367dfd79829SMiao Xie goto out_free; 1368dfd79829SMiao Xie } 13695bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizing devid %llu\n", 137021380931SJoel Becker (unsigned long long)devid); 1371f46b5a66SChristoph Hellwig } 1372dba60f3fSMiao Xie 1373aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1374f46b5a66SChristoph Hellwig if (!device) { 13755bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 137621380931SJoel Becker (unsigned long long)devid); 1377dfd79829SMiao Xie ret = -ENODEV; 1378c9e9f97bSIlya Dryomov goto out_free; 1379f46b5a66SChristoph Hellwig } 1380dba60f3fSMiao Xie 1381dba60f3fSMiao Xie if (!device->writeable) { 13824e42ae1bSLiu Bo printk(KERN_INFO "btrfs: resizer unable to apply on " 1383dba60f3fSMiao Xie "readonly device %llu\n", 1384a8c4a33bSChris Mason (unsigned long long)devid); 1385dfd79829SMiao Xie ret = -EPERM; 13864e42ae1bSLiu Bo goto out_free; 13874e42ae1bSLiu Bo } 13884e42ae1bSLiu Bo 1389f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1390f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1391f46b5a66SChristoph Hellwig else { 1392f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1393f46b5a66SChristoph Hellwig mod = -1; 1394f46b5a66SChristoph Hellwig sizestr++; 1395f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1396f46b5a66SChristoph Hellwig mod = 1; 1397f46b5a66SChristoph Hellwig sizestr++; 1398f46b5a66SChristoph Hellwig } 139991748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1400f46b5a66SChristoph Hellwig if (new_size == 0) { 1401f46b5a66SChristoph Hellwig ret = -EINVAL; 1402c9e9f97bSIlya Dryomov goto out_free; 1403f46b5a66SChristoph Hellwig } 1404f46b5a66SChristoph Hellwig } 1405f46b5a66SChristoph Hellwig 140663a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1407dfd79829SMiao Xie ret = -EPERM; 140863a212abSStefan Behrens goto out_free; 140963a212abSStefan Behrens } 141063a212abSStefan Behrens 1411f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1412f46b5a66SChristoph Hellwig 1413f46b5a66SChristoph Hellwig if (mod < 0) { 1414f46b5a66SChristoph Hellwig if (new_size > old_size) { 1415f46b5a66SChristoph Hellwig ret = -EINVAL; 1416c9e9f97bSIlya Dryomov goto out_free; 1417f46b5a66SChristoph Hellwig } 1418f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1419f46b5a66SChristoph Hellwig } else if (mod > 0) { 1420f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1421f46b5a66SChristoph Hellwig } 1422f46b5a66SChristoph Hellwig 1423f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1424f46b5a66SChristoph Hellwig ret = -EINVAL; 1425c9e9f97bSIlya Dryomov goto out_free; 1426f46b5a66SChristoph Hellwig } 1427f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1428f46b5a66SChristoph Hellwig ret = -EFBIG; 1429c9e9f97bSIlya Dryomov goto out_free; 1430f46b5a66SChristoph Hellwig } 1431f46b5a66SChristoph Hellwig 1432f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1433f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1434f46b5a66SChristoph Hellwig 1435606686eeSJosef Bacik printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", 1436606686eeSJosef Bacik rcu_str_deref(device->name), 1437606686eeSJosef Bacik (unsigned long long)new_size); 1438f46b5a66SChristoph Hellwig 1439f46b5a66SChristoph Hellwig if (new_size > old_size) { 1440a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 144198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 144298d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1443c9e9f97bSIlya Dryomov goto out_free; 144498d5dc13STsutomu Itoh } 1445f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1446f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1447ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1448f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 14490253f40eSjeff.liu } /* equal, nothing need to do */ 1450f46b5a66SChristoph Hellwig 1451c9e9f97bSIlya Dryomov out_free: 1452f46b5a66SChristoph Hellwig kfree(vol_args); 1453c9e9f97bSIlya Dryomov out: 1454c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 14555ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 145618f39c41SIlya Dryomov mnt_drop_write_file(file); 1457f46b5a66SChristoph Hellwig return ret; 1458f46b5a66SChristoph Hellwig } 1459f46b5a66SChristoph Hellwig 146072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 14616f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 14626f72c7e2SArne Jansen u64 *transid, bool readonly, 14638696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1464f46b5a66SChristoph Hellwig { 1465f46b5a66SChristoph Hellwig int namelen; 14663de4586cSChris Mason int ret = 0; 1467f46b5a66SChristoph Hellwig 1468a874a63eSLiu Bo ret = mnt_want_write_file(file); 1469a874a63eSLiu Bo if (ret) 1470a874a63eSLiu Bo goto out; 1471a874a63eSLiu Bo 147272fd032eSSage Weil namelen = strlen(name); 147372fd032eSSage Weil if (strchr(name, '/')) { 1474f46b5a66SChristoph Hellwig ret = -EINVAL; 1475a874a63eSLiu Bo goto out_drop_write; 1476f46b5a66SChristoph Hellwig } 1477f46b5a66SChristoph Hellwig 147816780cabSChris Mason if (name[0] == '.' && 147916780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 148016780cabSChris Mason ret = -EEXIST; 1481a874a63eSLiu Bo goto out_drop_write; 148216780cabSChris Mason } 148316780cabSChris Mason 14843de4586cSChris Mason if (subvol) { 148572fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 14866f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1487cb8e7090SChristoph Hellwig } else { 14882903ff01SAl Viro struct fd src = fdget(fd); 14893de4586cSChris Mason struct inode *src_inode; 14902903ff01SAl Viro if (!src.file) { 14913de4586cSChris Mason ret = -EINVAL; 1492a874a63eSLiu Bo goto out_drop_write; 14933de4586cSChris Mason } 14943de4586cSChris Mason 14952903ff01SAl Viro src_inode = src.file->f_path.dentry->d_inode; 14963de4586cSChris Mason if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { 1497d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1498d397712bSChris Mason "another FS\n"); 14993de4586cSChris Mason ret = -EINVAL; 1500ecd18815SAl Viro } else { 150172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 150272fd032eSSage Weil BTRFS_I(src_inode)->root, 15036f72c7e2SArne Jansen transid, readonly, inherit); 1504ecd18815SAl Viro } 15052903ff01SAl Viro fdput(src); 1506cb8e7090SChristoph Hellwig } 1507a874a63eSLiu Bo out_drop_write: 1508a874a63eSLiu Bo mnt_drop_write_file(file); 1509f46b5a66SChristoph Hellwig out: 151072fd032eSSage Weil return ret; 151172fd032eSSage Weil } 151272fd032eSSage Weil 151372fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1514fa0d2b9bSLi Zefan void __user *arg, int subvol) 151572fd032eSSage Weil { 1516fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 151772fd032eSSage Weil int ret; 151872fd032eSSage Weil 1519fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1520fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1521fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1522fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1523fa0d2b9bSLi Zefan 1524fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1525b83cc969SLi Zefan vol_args->fd, subvol, 15266f72c7e2SArne Jansen NULL, false, NULL); 1527fa0d2b9bSLi Zefan 1528fa0d2b9bSLi Zefan kfree(vol_args); 1529fa0d2b9bSLi Zefan return ret; 1530fa0d2b9bSLi Zefan } 1531fa0d2b9bSLi Zefan 1532fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1533fa0d2b9bSLi Zefan void __user *arg, int subvol) 1534fa0d2b9bSLi Zefan { 1535fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1536fa0d2b9bSLi Zefan int ret; 1537fdfb1e4fSLi Zefan u64 transid = 0; 1538fdfb1e4fSLi Zefan u64 *ptr = NULL; 1539b83cc969SLi Zefan bool readonly = false; 15406f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 154172fd032eSSage Weil 1542fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1543fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1544fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1545fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1546fdfb1e4fSLi Zefan 1547b83cc969SLi Zefan if (vol_args->flags & 15486f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 15496f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1550b83cc969SLi Zefan ret = -EOPNOTSUPP; 1551fdfb1e4fSLi Zefan goto out; 1552fdfb1e4fSLi Zefan } 1553fdfb1e4fSLi Zefan 1554fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1555fdfb1e4fSLi Zefan ptr = &transid; 1556b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1557b83cc969SLi Zefan readonly = true; 15586f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 15596f72c7e2SArne Jansen if (vol_args->size > PAGE_CACHE_SIZE) { 15606f72c7e2SArne Jansen ret = -EINVAL; 15616f72c7e2SArne Jansen goto out; 15626f72c7e2SArne Jansen } 15636f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 15646f72c7e2SArne Jansen if (IS_ERR(inherit)) { 15656f72c7e2SArne Jansen ret = PTR_ERR(inherit); 15666f72c7e2SArne Jansen goto out; 15676f72c7e2SArne Jansen } 15686f72c7e2SArne Jansen } 156975eaa0e2SSage Weil 1570fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 15716f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 15728696c533SMiao Xie readonly, inherit); 157375eaa0e2SSage Weil 1574fdfb1e4fSLi Zefan if (ret == 0 && ptr && 157575eaa0e2SSage Weil copy_to_user(arg + 1576fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1577fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 157875eaa0e2SSage Weil ret = -EFAULT; 1579fdfb1e4fSLi Zefan out: 1580f46b5a66SChristoph Hellwig kfree(vol_args); 15816f72c7e2SArne Jansen kfree(inherit); 1582f46b5a66SChristoph Hellwig return ret; 1583f46b5a66SChristoph Hellwig } 1584f46b5a66SChristoph Hellwig 15850caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 15860caa102dSLi Zefan void __user *arg) 15870caa102dSLi Zefan { 15880caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 15890caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 15900caa102dSLi Zefan int ret = 0; 15910caa102dSLi Zefan u64 flags = 0; 15920caa102dSLi Zefan 159333345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 15940caa102dSLi Zefan return -EINVAL; 15950caa102dSLi Zefan 15960caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 15970caa102dSLi Zefan if (btrfs_root_readonly(root)) 15980caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 15990caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 16000caa102dSLi Zefan 16010caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 16020caa102dSLi Zefan ret = -EFAULT; 16030caa102dSLi Zefan 16040caa102dSLi Zefan return ret; 16050caa102dSLi Zefan } 16060caa102dSLi Zefan 16070caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 16080caa102dSLi Zefan void __user *arg) 16090caa102dSLi Zefan { 16100caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 16110caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 16120caa102dSLi Zefan struct btrfs_trans_handle *trans; 16130caa102dSLi Zefan u64 root_flags; 16140caa102dSLi Zefan u64 flags; 16150caa102dSLi Zefan int ret = 0; 16160caa102dSLi Zefan 1617b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1618b9ca0664SLiu Bo if (ret) 1619b9ca0664SLiu Bo goto out; 16200caa102dSLi Zefan 1621b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1622b9ca0664SLiu Bo ret = -EINVAL; 1623b9ca0664SLiu Bo goto out_drop_write; 1624b9ca0664SLiu Bo } 16250caa102dSLi Zefan 1626b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1627b9ca0664SLiu Bo ret = -EFAULT; 1628b9ca0664SLiu Bo goto out_drop_write; 1629b9ca0664SLiu Bo } 16300caa102dSLi Zefan 1631b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1632b9ca0664SLiu Bo ret = -EINVAL; 1633b9ca0664SLiu Bo goto out_drop_write; 1634b9ca0664SLiu Bo } 16350caa102dSLi Zefan 1636b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1637b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1638b9ca0664SLiu Bo goto out_drop_write; 1639b9ca0664SLiu Bo } 16400caa102dSLi Zefan 1641b9ca0664SLiu Bo if (!inode_owner_or_capable(inode)) { 1642b9ca0664SLiu Bo ret = -EACCES; 1643b9ca0664SLiu Bo goto out_drop_write; 1644b9ca0664SLiu Bo } 1645b4dc2b8cSLi Zefan 16460caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 16470caa102dSLi Zefan 16480caa102dSLi Zefan /* nothing to do */ 16490caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1650b9ca0664SLiu Bo goto out_drop_sem; 16510caa102dSLi Zefan 16520caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 16530caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 16540caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16550caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 16560caa102dSLi Zefan else 16570caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 16580caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 16590caa102dSLi Zefan 16600caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 16610caa102dSLi Zefan if (IS_ERR(trans)) { 16620caa102dSLi Zefan ret = PTR_ERR(trans); 16630caa102dSLi Zefan goto out_reset; 16640caa102dSLi Zefan } 16650caa102dSLi Zefan 1666b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 16670caa102dSLi Zefan &root->root_key, &root->root_item); 16680caa102dSLi Zefan 16690caa102dSLi Zefan btrfs_commit_transaction(trans, root); 16700caa102dSLi Zefan out_reset: 16710caa102dSLi Zefan if (ret) 16720caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1673b9ca0664SLiu Bo out_drop_sem: 16740caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1675b9ca0664SLiu Bo out_drop_write: 1676b9ca0664SLiu Bo mnt_drop_write_file(file); 1677b9ca0664SLiu Bo out: 16780caa102dSLi Zefan return ret; 16790caa102dSLi Zefan } 16800caa102dSLi Zefan 168176dda93cSYan, Zheng /* 168276dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 168376dda93cSYan, Zheng */ 168476dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 168576dda93cSYan, Zheng { 168676dda93cSYan, Zheng struct btrfs_path *path; 168776dda93cSYan, Zheng struct btrfs_key key; 168876dda93cSYan, Zheng int ret; 168976dda93cSYan, Zheng 169076dda93cSYan, Zheng path = btrfs_alloc_path(); 169176dda93cSYan, Zheng if (!path) 169276dda93cSYan, Zheng return -ENOMEM; 169376dda93cSYan, Zheng 169476dda93cSYan, Zheng key.objectid = root->root_key.objectid; 169576dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 169676dda93cSYan, Zheng key.offset = (u64)-1; 169776dda93cSYan, Zheng 169876dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 169976dda93cSYan, Zheng &key, path, 0, 0); 170076dda93cSYan, Zheng if (ret < 0) 170176dda93cSYan, Zheng goto out; 170276dda93cSYan, Zheng BUG_ON(ret == 0); 170376dda93cSYan, Zheng 170476dda93cSYan, Zheng ret = 0; 170576dda93cSYan, Zheng if (path->slots[0] > 0) { 170676dda93cSYan, Zheng path->slots[0]--; 170776dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 170876dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 170976dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 171076dda93cSYan, Zheng ret = -ENOTEMPTY; 171176dda93cSYan, Zheng } 171276dda93cSYan, Zheng out: 171376dda93cSYan, Zheng btrfs_free_path(path); 171476dda93cSYan, Zheng return ret; 171576dda93cSYan, Zheng } 171676dda93cSYan, Zheng 1717ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1718ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1719ac8e9819SChris Mason { 1720abc6e134SChris Mason struct btrfs_key test; 1721abc6e134SChris Mason int ret; 1722abc6e134SChris Mason 1723abc6e134SChris Mason test.objectid = sk->min_objectid; 1724abc6e134SChris Mason test.type = sk->min_type; 1725abc6e134SChris Mason test.offset = sk->min_offset; 1726abc6e134SChris Mason 1727abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1728abc6e134SChris Mason if (ret < 0) 1729ac8e9819SChris Mason return 0; 1730abc6e134SChris Mason 1731abc6e134SChris Mason test.objectid = sk->max_objectid; 1732abc6e134SChris Mason test.type = sk->max_type; 1733abc6e134SChris Mason test.offset = sk->max_offset; 1734abc6e134SChris Mason 1735abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1736abc6e134SChris Mason if (ret > 0) 1737ac8e9819SChris Mason return 0; 1738ac8e9819SChris Mason return 1; 1739ac8e9819SChris Mason } 1740ac8e9819SChris Mason 1741ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1742ac8e9819SChris Mason struct btrfs_path *path, 1743ac8e9819SChris Mason struct btrfs_key *key, 1744ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1745ac8e9819SChris Mason char *buf, 1746ac8e9819SChris Mason unsigned long *sk_offset, 1747ac8e9819SChris Mason int *num_found) 1748ac8e9819SChris Mason { 1749ac8e9819SChris Mason u64 found_transid; 1750ac8e9819SChris Mason struct extent_buffer *leaf; 1751ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1752ac8e9819SChris Mason unsigned long item_off; 1753ac8e9819SChris Mason unsigned long item_len; 1754ac8e9819SChris Mason int nritems; 1755ac8e9819SChris Mason int i; 1756ac8e9819SChris Mason int slot; 1757ac8e9819SChris Mason int ret = 0; 1758ac8e9819SChris Mason 1759ac8e9819SChris Mason leaf = path->nodes[0]; 1760ac8e9819SChris Mason slot = path->slots[0]; 1761ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1762ac8e9819SChris Mason 1763ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1764ac8e9819SChris Mason i = nritems; 1765ac8e9819SChris Mason goto advance_key; 1766ac8e9819SChris Mason } 1767ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1768ac8e9819SChris Mason 1769ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1770ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1771ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1772ac8e9819SChris Mason 1773ac8e9819SChris Mason if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1774ac8e9819SChris Mason item_len = 0; 1775ac8e9819SChris Mason 1776ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1777ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1778ac8e9819SChris Mason ret = 1; 1779ac8e9819SChris Mason goto overflow; 1780ac8e9819SChris Mason } 1781ac8e9819SChris Mason 1782ac8e9819SChris Mason btrfs_item_key_to_cpu(leaf, key, i); 1783ac8e9819SChris Mason if (!key_in_sk(key, sk)) 1784ac8e9819SChris Mason continue; 1785ac8e9819SChris Mason 1786ac8e9819SChris Mason sh.objectid = key->objectid; 1787ac8e9819SChris Mason sh.offset = key->offset; 1788ac8e9819SChris Mason sh.type = key->type; 1789ac8e9819SChris Mason sh.len = item_len; 1790ac8e9819SChris Mason sh.transid = found_transid; 1791ac8e9819SChris Mason 1792ac8e9819SChris Mason /* copy search result header */ 1793ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1794ac8e9819SChris Mason *sk_offset += sizeof(sh); 1795ac8e9819SChris Mason 1796ac8e9819SChris Mason if (item_len) { 1797ac8e9819SChris Mason char *p = buf + *sk_offset; 1798ac8e9819SChris Mason /* copy the item */ 1799ac8e9819SChris Mason read_extent_buffer(leaf, p, 1800ac8e9819SChris Mason item_off, item_len); 1801ac8e9819SChris Mason *sk_offset += item_len; 1802ac8e9819SChris Mason } 1803e2156867SHugo Mills (*num_found)++; 1804ac8e9819SChris Mason 1805ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1806ac8e9819SChris Mason break; 1807ac8e9819SChris Mason } 1808ac8e9819SChris Mason advance_key: 1809ac8e9819SChris Mason ret = 0; 1810abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1811abc6e134SChris Mason key->offset++; 1812abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1813abc6e134SChris Mason key->offset = 0; 1814abc6e134SChris Mason key->type++; 1815abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1816abc6e134SChris Mason key->offset = 0; 1817abc6e134SChris Mason key->type = 0; 1818abc6e134SChris Mason key->objectid++; 1819abc6e134SChris Mason } else 1820abc6e134SChris Mason ret = 1; 1821ac8e9819SChris Mason overflow: 1822ac8e9819SChris Mason return ret; 1823ac8e9819SChris Mason } 1824ac8e9819SChris Mason 1825ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1826ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1827ac8e9819SChris Mason { 1828ac8e9819SChris Mason struct btrfs_root *root; 1829ac8e9819SChris Mason struct btrfs_key key; 1830ac8e9819SChris Mason struct btrfs_key max_key; 1831ac8e9819SChris Mason struct btrfs_path *path; 1832ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1833ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1834ac8e9819SChris Mason int ret; 1835ac8e9819SChris Mason int num_found = 0; 1836ac8e9819SChris Mason unsigned long sk_offset = 0; 1837ac8e9819SChris Mason 1838ac8e9819SChris Mason path = btrfs_alloc_path(); 1839ac8e9819SChris Mason if (!path) 1840ac8e9819SChris Mason return -ENOMEM; 1841ac8e9819SChris Mason 1842ac8e9819SChris Mason if (sk->tree_id == 0) { 1843ac8e9819SChris Mason /* search the root of the inode that was passed */ 1844ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1845ac8e9819SChris Mason } else { 1846ac8e9819SChris Mason key.objectid = sk->tree_id; 1847ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1848ac8e9819SChris Mason key.offset = (u64)-1; 1849ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1850ac8e9819SChris Mason if (IS_ERR(root)) { 1851ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1852ac8e9819SChris Mason sk->tree_id); 1853ac8e9819SChris Mason btrfs_free_path(path); 1854ac8e9819SChris Mason return -ENOENT; 1855ac8e9819SChris Mason } 1856ac8e9819SChris Mason } 1857ac8e9819SChris Mason 1858ac8e9819SChris Mason key.objectid = sk->min_objectid; 1859ac8e9819SChris Mason key.type = sk->min_type; 1860ac8e9819SChris Mason key.offset = sk->min_offset; 1861ac8e9819SChris Mason 1862ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1863ac8e9819SChris Mason max_key.type = sk->max_type; 1864ac8e9819SChris Mason max_key.offset = sk->max_offset; 1865ac8e9819SChris Mason 1866ac8e9819SChris Mason path->keep_locks = 1; 1867ac8e9819SChris Mason 1868ac8e9819SChris Mason while(1) { 1869de78b51aSEric Sandeen ret = btrfs_search_forward(root, &key, &max_key, path, 1870ac8e9819SChris Mason sk->min_transid); 1871ac8e9819SChris Mason if (ret != 0) { 1872ac8e9819SChris Mason if (ret > 0) 1873ac8e9819SChris Mason ret = 0; 1874ac8e9819SChris Mason goto err; 1875ac8e9819SChris Mason } 1876ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1877ac8e9819SChris Mason &sk_offset, &num_found); 1878b3b4aa74SDavid Sterba btrfs_release_path(path); 1879ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1880ac8e9819SChris Mason break; 1881ac8e9819SChris Mason 1882ac8e9819SChris Mason } 1883ac8e9819SChris Mason ret = 0; 1884ac8e9819SChris Mason err: 1885ac8e9819SChris Mason sk->nr_items = num_found; 1886ac8e9819SChris Mason btrfs_free_path(path); 1887ac8e9819SChris Mason return ret; 1888ac8e9819SChris Mason } 1889ac8e9819SChris Mason 1890ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1891ac8e9819SChris Mason void __user *argp) 1892ac8e9819SChris Mason { 1893ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1894ac8e9819SChris Mason struct inode *inode; 1895ac8e9819SChris Mason int ret; 1896ac8e9819SChris Mason 1897ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1898ac8e9819SChris Mason return -EPERM; 1899ac8e9819SChris Mason 19002354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 19012354d08fSJulia Lawall if (IS_ERR(args)) 19022354d08fSJulia Lawall return PTR_ERR(args); 1903ac8e9819SChris Mason 1904ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1905ac8e9819SChris Mason ret = search_ioctl(inode, args); 1906ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1907ac8e9819SChris Mason ret = -EFAULT; 1908ac8e9819SChris Mason kfree(args); 1909ac8e9819SChris Mason return ret; 1910ac8e9819SChris Mason } 1911ac8e9819SChris Mason 191298d377a0STARUISI Hiroaki /* 1913ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1914ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 191598d377a0STARUISI Hiroaki */ 191698d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 191798d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 191898d377a0STARUISI Hiroaki { 191998d377a0STARUISI Hiroaki struct btrfs_root *root; 192098d377a0STARUISI Hiroaki struct btrfs_key key; 1921ac8e9819SChris Mason char *ptr; 192298d377a0STARUISI Hiroaki int ret = -1; 192398d377a0STARUISI Hiroaki int slot; 192498d377a0STARUISI Hiroaki int len; 192598d377a0STARUISI Hiroaki int total_len = 0; 192698d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 192798d377a0STARUISI Hiroaki struct extent_buffer *l; 192898d377a0STARUISI Hiroaki struct btrfs_path *path; 192998d377a0STARUISI Hiroaki 193098d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 193198d377a0STARUISI Hiroaki name[0]='\0'; 193298d377a0STARUISI Hiroaki return 0; 193398d377a0STARUISI Hiroaki } 193498d377a0STARUISI Hiroaki 193598d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 193698d377a0STARUISI Hiroaki if (!path) 193798d377a0STARUISI Hiroaki return -ENOMEM; 193898d377a0STARUISI Hiroaki 1939ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 194098d377a0STARUISI Hiroaki 194198d377a0STARUISI Hiroaki key.objectid = tree_id; 194298d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 194398d377a0STARUISI Hiroaki key.offset = (u64)-1; 194498d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 194598d377a0STARUISI Hiroaki if (IS_ERR(root)) { 194698d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 19478ad6fcabSChris Mason ret = -ENOENT; 19488ad6fcabSChris Mason goto out; 194998d377a0STARUISI Hiroaki } 195098d377a0STARUISI Hiroaki 195198d377a0STARUISI Hiroaki key.objectid = dirid; 195298d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 19538ad6fcabSChris Mason key.offset = (u64)-1; 195498d377a0STARUISI Hiroaki 195598d377a0STARUISI Hiroaki while(1) { 195698d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 195798d377a0STARUISI Hiroaki if (ret < 0) 195898d377a0STARUISI Hiroaki goto out; 195998d377a0STARUISI Hiroaki 196098d377a0STARUISI Hiroaki l = path->nodes[0]; 196198d377a0STARUISI Hiroaki slot = path->slots[0]; 19628ad6fcabSChris Mason if (ret > 0 && slot > 0) 19638ad6fcabSChris Mason slot--; 196498d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 196598d377a0STARUISI Hiroaki 196698d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 1967ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 1968ac8e9819SChris Mason ret = -ENOENT; 196998d377a0STARUISI Hiroaki goto out; 1970ac8e9819SChris Mason } 197198d377a0STARUISI Hiroaki 197298d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 197398d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 197498d377a0STARUISI Hiroaki ptr -= len + 1; 197598d377a0STARUISI Hiroaki total_len += len + 1; 1976ac8e9819SChris Mason if (ptr < name) 197798d377a0STARUISI Hiroaki goto out; 197898d377a0STARUISI Hiroaki 197998d377a0STARUISI Hiroaki *(ptr + len) = '/'; 198098d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 198198d377a0STARUISI Hiroaki 198298d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 198398d377a0STARUISI Hiroaki break; 198498d377a0STARUISI Hiroaki 1985b3b4aa74SDavid Sterba btrfs_release_path(path); 198698d377a0STARUISI Hiroaki key.objectid = key.offset; 19878ad6fcabSChris Mason key.offset = (u64)-1; 198898d377a0STARUISI Hiroaki dirid = key.objectid; 198998d377a0STARUISI Hiroaki } 1990ac8e9819SChris Mason if (ptr < name) 199198d377a0STARUISI Hiroaki goto out; 199277906a50SLi Zefan memmove(name, ptr, total_len); 199398d377a0STARUISI Hiroaki name[total_len]='\0'; 199498d377a0STARUISI Hiroaki ret = 0; 199598d377a0STARUISI Hiroaki out: 199698d377a0STARUISI Hiroaki btrfs_free_path(path); 1997ac8e9819SChris Mason return ret; 1998ac8e9819SChris Mason } 1999ac8e9819SChris Mason 2000ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2001ac8e9819SChris Mason void __user *argp) 2002ac8e9819SChris Mason { 2003ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2004ac8e9819SChris Mason struct inode *inode; 2005ac8e9819SChris Mason int ret; 2006ac8e9819SChris Mason 2007ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2008ac8e9819SChris Mason return -EPERM; 2009ac8e9819SChris Mason 20102354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 20112354d08fSJulia Lawall if (IS_ERR(args)) 20122354d08fSJulia Lawall return PTR_ERR(args); 2013c2b96929SDan Carpenter 2014ac8e9819SChris Mason inode = fdentry(file)->d_inode; 2015ac8e9819SChris Mason 20161b53ac4dSChris Mason if (args->treeid == 0) 20171b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 20181b53ac4dSChris Mason 2019ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2020ac8e9819SChris Mason args->treeid, args->objectid, 2021ac8e9819SChris Mason args->name); 2022ac8e9819SChris Mason 2023ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2024ac8e9819SChris Mason ret = -EFAULT; 2025ac8e9819SChris Mason 2026ac8e9819SChris Mason kfree(args); 202798d377a0STARUISI Hiroaki return ret; 202898d377a0STARUISI Hiroaki } 202998d377a0STARUISI Hiroaki 203076dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 203176dda93cSYan, Zheng void __user *arg) 203276dda93cSYan, Zheng { 203376dda93cSYan, Zheng struct dentry *parent = fdentry(file); 203476dda93cSYan, Zheng struct dentry *dentry; 203576dda93cSYan, Zheng struct inode *dir = parent->d_inode; 203676dda93cSYan, Zheng struct inode *inode; 203776dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 203876dda93cSYan, Zheng struct btrfs_root *dest = NULL; 203976dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 204076dda93cSYan, Zheng struct btrfs_trans_handle *trans; 204176dda93cSYan, Zheng int namelen; 204276dda93cSYan, Zheng int ret; 204376dda93cSYan, Zheng int err = 0; 204476dda93cSYan, Zheng 204576dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 204676dda93cSYan, Zheng if (IS_ERR(vol_args)) 204776dda93cSYan, Zheng return PTR_ERR(vol_args); 204876dda93cSYan, Zheng 204976dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 205076dda93cSYan, Zheng namelen = strlen(vol_args->name); 205176dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 205276dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 205376dda93cSYan, Zheng err = -EINVAL; 205476dda93cSYan, Zheng goto out; 205576dda93cSYan, Zheng } 205676dda93cSYan, Zheng 2057a561be71SAl Viro err = mnt_want_write_file(file); 205876dda93cSYan, Zheng if (err) 205976dda93cSYan, Zheng goto out; 206076dda93cSYan, Zheng 206176dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 206276dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 206376dda93cSYan, Zheng if (IS_ERR(dentry)) { 206476dda93cSYan, Zheng err = PTR_ERR(dentry); 206576dda93cSYan, Zheng goto out_unlock_dir; 206676dda93cSYan, Zheng } 206776dda93cSYan, Zheng 206876dda93cSYan, Zheng if (!dentry->d_inode) { 206976dda93cSYan, Zheng err = -ENOENT; 207076dda93cSYan, Zheng goto out_dput; 207176dda93cSYan, Zheng } 207276dda93cSYan, Zheng 207376dda93cSYan, Zheng inode = dentry->d_inode; 20744260f7c7SSage Weil dest = BTRFS_I(inode)->root; 20754260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 20764260f7c7SSage Weil /* 20774260f7c7SSage Weil * Regular user. Only allow this with a special mount 20784260f7c7SSage Weil * option, when the user has write+exec access to the 20794260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 20804260f7c7SSage Weil * allowed. 20814260f7c7SSage Weil * 20824260f7c7SSage Weil * Note that this is _not_ check that the subvol is 20834260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 20844260f7c7SSage Weil * otherwise be able to delete. 20854260f7c7SSage Weil * 20864260f7c7SSage Weil * Users who want to delete empty subvols should try 20874260f7c7SSage Weil * rmdir(2). 20884260f7c7SSage Weil */ 20894260f7c7SSage Weil err = -EPERM; 20904260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 20914260f7c7SSage Weil goto out_dput; 20924260f7c7SSage Weil 20934260f7c7SSage Weil /* 20944260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 20954260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 20964260f7c7SSage Weil * must be called on the dentry referencing the root 20974260f7c7SSage Weil * of the subvol, not a random directory contained 20984260f7c7SSage Weil * within it. 20994260f7c7SSage Weil */ 21004260f7c7SSage Weil err = -EINVAL; 21014260f7c7SSage Weil if (root == dest) 21024260f7c7SSage Weil goto out_dput; 21034260f7c7SSage Weil 21044260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 21054260f7c7SSage Weil if (err) 21064260f7c7SSage Weil goto out_dput; 21075c39da5bSMiao Xie } 21084260f7c7SSage Weil 21095c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 21104260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 21114260f7c7SSage Weil if (err) 21124260f7c7SSage Weil goto out_dput; 21134260f7c7SSage Weil 211433345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 211576dda93cSYan, Zheng err = -EINVAL; 211676dda93cSYan, Zheng goto out_dput; 211776dda93cSYan, Zheng } 211876dda93cSYan, Zheng 211976dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 212076dda93cSYan, Zheng err = d_invalidate(dentry); 212176dda93cSYan, Zheng if (err) 212276dda93cSYan, Zheng goto out_unlock; 212376dda93cSYan, Zheng 212476dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 212576dda93cSYan, Zheng 212676dda93cSYan, Zheng err = may_destroy_subvol(dest); 212776dda93cSYan, Zheng if (err) 212876dda93cSYan, Zheng goto out_up_write; 212976dda93cSYan, Zheng 2130a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2131a22285a6SYan, Zheng if (IS_ERR(trans)) { 2132a22285a6SYan, Zheng err = PTR_ERR(trans); 2133d327099aSDan Carpenter goto out_up_write; 2134a22285a6SYan, Zheng } 2135a22285a6SYan, Zheng trans->block_rsv = &root->fs_info->global_block_rsv; 2136a22285a6SYan, Zheng 213776dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 213876dda93cSYan, Zheng dest->root_key.objectid, 213976dda93cSYan, Zheng dentry->d_name.name, 214076dda93cSYan, Zheng dentry->d_name.len); 214179787eaaSJeff Mahoney if (ret) { 214279787eaaSJeff Mahoney err = ret; 214379787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 214479787eaaSJeff Mahoney goto out_end_trans; 214579787eaaSJeff Mahoney } 214676dda93cSYan, Zheng 214776dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 214876dda93cSYan, Zheng 214976dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 215076dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 215176dda93cSYan, Zheng dest->root_item.drop_level = 0; 215276dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 215376dda93cSYan, Zheng 2154d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 215576dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 215676dda93cSYan, Zheng root->fs_info->tree_root, 215776dda93cSYan, Zheng dest->root_key.objectid); 215879787eaaSJeff Mahoney if (ret) { 215979787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 216079787eaaSJeff Mahoney err = ret; 216179787eaaSJeff Mahoney goto out_end_trans; 2162d68fc57bSYan, Zheng } 216379787eaaSJeff Mahoney } 216479787eaaSJeff Mahoney out_end_trans: 2165531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 216679787eaaSJeff Mahoney if (ret && !err) 216779787eaaSJeff Mahoney err = ret; 216876dda93cSYan, Zheng inode->i_flags |= S_DEAD; 216976dda93cSYan, Zheng out_up_write: 217076dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 217176dda93cSYan, Zheng out_unlock: 217276dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 217376dda93cSYan, Zheng if (!err) { 2174efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 217576dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 217676dda93cSYan, Zheng d_delete(dentry); 2177*fa6ac876SLiu Bo 2178*fa6ac876SLiu Bo /* the last ref */ 2179*fa6ac876SLiu Bo if (dest->cache_inode) { 2180*fa6ac876SLiu Bo iput(dest->cache_inode); 2181*fa6ac876SLiu Bo dest->cache_inode = NULL; 2182*fa6ac876SLiu Bo } 218376dda93cSYan, Zheng } 218476dda93cSYan, Zheng out_dput: 218576dda93cSYan, Zheng dput(dentry); 218676dda93cSYan, Zheng out_unlock_dir: 218776dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 21882a79f17eSAl Viro mnt_drop_write_file(file); 218976dda93cSYan, Zheng out: 219076dda93cSYan, Zheng kfree(vol_args); 219176dda93cSYan, Zheng return err; 219276dda93cSYan, Zheng } 219376dda93cSYan, Zheng 21941e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2195f46b5a66SChristoph Hellwig { 2196f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2197f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 21981e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2199c146afadSYan Zheng int ret; 2200c146afadSYan Zheng 220125122d15SIlya Dryomov ret = mnt_want_write_file(file); 220225122d15SIlya Dryomov if (ret) 220325122d15SIlya Dryomov return ret; 2204b83cc969SLi Zefan 22055ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 22065ac00addSStefan Behrens 1)) { 22075ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 220825122d15SIlya Dryomov mnt_drop_write_file(file); 22092c0c9da0SIlya Dryomov return -EINVAL; 22105ac00addSStefan Behrens } 221125122d15SIlya Dryomov 221225122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 221325122d15SIlya Dryomov ret = -EROFS; 221425122d15SIlya Dryomov goto out; 22155ac00addSStefan Behrens } 2216f46b5a66SChristoph Hellwig 2217f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2218f46b5a66SChristoph Hellwig case S_IFDIR: 2219e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2220e441d54dSChris Mason ret = -EPERM; 2221e441d54dSChris Mason goto out; 2222e441d54dSChris Mason } 2223de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 22248929ecfaSYan, Zheng if (ret) 22258929ecfaSYan, Zheng goto out; 2226de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2227f46b5a66SChristoph Hellwig break; 2228f46b5a66SChristoph Hellwig case S_IFREG: 2229e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2230e441d54dSChris Mason ret = -EINVAL; 2231e441d54dSChris Mason goto out; 2232e441d54dSChris Mason } 22331e701a32SChris Mason 22341e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 22351e701a32SChris Mason if (!range) { 22361e701a32SChris Mason ret = -ENOMEM; 22371e701a32SChris Mason goto out; 22381e701a32SChris Mason } 22391e701a32SChris Mason 22401e701a32SChris Mason if (argp) { 22411e701a32SChris Mason if (copy_from_user(range, argp, 22421e701a32SChris Mason sizeof(*range))) { 22431e701a32SChris Mason ret = -EFAULT; 22441e701a32SChris Mason kfree(range); 2245683be16eSDan Carpenter goto out; 22461e701a32SChris Mason } 22471e701a32SChris Mason /* compression requires us to start the IO */ 22481e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 22491e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 22501e701a32SChris Mason range->extent_thresh = (u32)-1; 22511e701a32SChris Mason } 22521e701a32SChris Mason } else { 22531e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 22541e701a32SChris Mason range->len = (u64)-1; 22551e701a32SChris Mason } 22564cb5300bSChris Mason ret = btrfs_defrag_file(fdentry(file)->d_inode, file, 22574cb5300bSChris Mason range, 0, 0); 22584cb5300bSChris Mason if (ret > 0) 22594cb5300bSChris Mason ret = 0; 22601e701a32SChris Mason kfree(range); 2261f46b5a66SChristoph Hellwig break; 22628929ecfaSYan, Zheng default: 22638929ecfaSYan, Zheng ret = -EINVAL; 2264f46b5a66SChristoph Hellwig } 2265e441d54dSChris Mason out: 22665ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 226725122d15SIlya Dryomov mnt_drop_write_file(file); 2268e441d54dSChris Mason return ret; 2269f46b5a66SChristoph Hellwig } 2270f46b5a66SChristoph Hellwig 2271b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2272f46b5a66SChristoph Hellwig { 2273f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2274f46b5a66SChristoph Hellwig int ret; 2275f46b5a66SChristoph Hellwig 2276e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2277e441d54dSChris Mason return -EPERM; 2278e441d54dSChris Mason 22795ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 22805ac00addSStefan Behrens 1)) { 22815ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 22822c0c9da0SIlya Dryomov return -EINVAL; 2283c9e9f97bSIlya Dryomov } 2284c9e9f97bSIlya Dryomov 22855ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2286dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2287c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2288c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2289c9e9f97bSIlya Dryomov goto out; 2290c9e9f97bSIlya Dryomov } 2291f46b5a66SChristoph Hellwig 22925516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2293f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2294f46b5a66SChristoph Hellwig 2295f46b5a66SChristoph Hellwig kfree(vol_args); 2296c9e9f97bSIlya Dryomov out: 2297c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 22985ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2299f46b5a66SChristoph Hellwig return ret; 2300f46b5a66SChristoph Hellwig } 2301f46b5a66SChristoph Hellwig 2302da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2303f46b5a66SChristoph Hellwig { 2304da24927bSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 2305f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2306f46b5a66SChristoph Hellwig int ret; 2307f46b5a66SChristoph Hellwig 2308e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2309e441d54dSChris Mason return -EPERM; 2310e441d54dSChris Mason 2311da24927bSMiao Xie ret = mnt_want_write_file(file); 2312da24927bSMiao Xie if (ret) 2313da24927bSMiao Xie return ret; 2314c146afadSYan Zheng 23155ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 23165ac00addSStefan Behrens 1)) { 23175ac00addSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 2318da24927bSMiao Xie mnt_drop_write_file(file); 23192c0c9da0SIlya Dryomov return -EINVAL; 2320c9e9f97bSIlya Dryomov } 2321c9e9f97bSIlya Dryomov 23225ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2323dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2324c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2325c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2326c9e9f97bSIlya Dryomov goto out; 2327c9e9f97bSIlya Dryomov } 2328f46b5a66SChristoph Hellwig 23295516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2330f46b5a66SChristoph Hellwig ret = btrfs_rm_device(root, vol_args->name); 2331f46b5a66SChristoph Hellwig 2332f46b5a66SChristoph Hellwig kfree(vol_args); 2333c9e9f97bSIlya Dryomov out: 2334c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 23355ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 23364ac20c70SIlya Dryomov mnt_drop_write_file(file); 2337f46b5a66SChristoph Hellwig return ret; 2338f46b5a66SChristoph Hellwig } 2339f46b5a66SChristoph Hellwig 2340475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2341475f6387SJan Schmidt { 2342027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2343475f6387SJan Schmidt struct btrfs_device *device; 2344475f6387SJan Schmidt struct btrfs_device *next; 2345475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2346027ed2f0SLi Zefan int ret = 0; 2347475f6387SJan Schmidt 2348475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2349475f6387SJan Schmidt return -EPERM; 2350475f6387SJan Schmidt 2351027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2352027ed2f0SLi Zefan if (!fi_args) 2353027ed2f0SLi Zefan return -ENOMEM; 2354027ed2f0SLi Zefan 2355027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2356027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2357475f6387SJan Schmidt 2358475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2359475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2360027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2361027ed2f0SLi Zefan fi_args->max_id = device->devid; 2362475f6387SJan Schmidt } 2363475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2364475f6387SJan Schmidt 2365027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2366027ed2f0SLi Zefan ret = -EFAULT; 2367475f6387SJan Schmidt 2368027ed2f0SLi Zefan kfree(fi_args); 2369027ed2f0SLi Zefan return ret; 2370475f6387SJan Schmidt } 2371475f6387SJan Schmidt 2372475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2373475f6387SJan Schmidt { 2374475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2375475f6387SJan Schmidt struct btrfs_device *dev; 2376475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2377475f6387SJan Schmidt int ret = 0; 2378475f6387SJan Schmidt char *s_uuid = NULL; 2379475f6387SJan Schmidt char empty_uuid[BTRFS_UUID_SIZE] = {0}; 2380475f6387SJan Schmidt 2381475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2382475f6387SJan Schmidt return -EPERM; 2383475f6387SJan Schmidt 2384475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2385475f6387SJan Schmidt if (IS_ERR(di_args)) 2386475f6387SJan Schmidt return PTR_ERR(di_args); 2387475f6387SJan Schmidt 2388475f6387SJan Schmidt if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) 2389475f6387SJan Schmidt s_uuid = di_args->uuid; 2390475f6387SJan Schmidt 2391475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2392aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2393475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2394475f6387SJan Schmidt 2395475f6387SJan Schmidt if (!dev) { 2396475f6387SJan Schmidt ret = -ENODEV; 2397475f6387SJan Schmidt goto out; 2398475f6387SJan Schmidt } 2399475f6387SJan Schmidt 2400475f6387SJan Schmidt di_args->devid = dev->devid; 2401475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2402475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2403475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2404a27202fbSJim Meyering if (dev->name) { 2405606686eeSJosef Bacik struct rcu_string *name; 2406606686eeSJosef Bacik 2407606686eeSJosef Bacik rcu_read_lock(); 2408606686eeSJosef Bacik name = rcu_dereference(dev->name); 2409606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2410606686eeSJosef Bacik rcu_read_unlock(); 2411a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2412a27202fbSJim Meyering } else { 241399ba55adSStefan Behrens di_args->path[0] = '\0'; 2414a27202fbSJim Meyering } 2415475f6387SJan Schmidt 2416475f6387SJan Schmidt out: 2417475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2418475f6387SJan Schmidt ret = -EFAULT; 2419475f6387SJan Schmidt 2420475f6387SJan Schmidt kfree(di_args); 2421475f6387SJan Schmidt return ret; 2422475f6387SJan Schmidt } 2423475f6387SJan Schmidt 242476dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 2425b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 2426f46b5a66SChristoph Hellwig { 2427f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2428f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 24292903ff01SAl Viro struct fd src_file; 2430f46b5a66SChristoph Hellwig struct inode *src; 2431f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2432f46b5a66SChristoph Hellwig struct btrfs_path *path; 2433f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 2434ae01a0abSYan Zheng char *buf; 2435ae01a0abSYan Zheng struct btrfs_key key; 2436f46b5a66SChristoph Hellwig u32 nritems; 2437f46b5a66SChristoph Hellwig int slot; 2438ae01a0abSYan Zheng int ret; 2439c5c9cd4dSSage Weil u64 len = olen; 2440c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 2441d20f7043SChris Mason 2442c5c9cd4dSSage Weil /* 2443c5c9cd4dSSage Weil * TODO: 2444c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 2445c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 2446c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 2447c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 2448c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 2449c5c9cd4dSSage Weil * they don't overlap)? 2450c5c9cd4dSSage Weil */ 2451c5c9cd4dSSage Weil 2452e441d54dSChris Mason /* the destination must be opened for writing */ 24532ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 2454e441d54dSChris Mason return -EINVAL; 2455e441d54dSChris Mason 2456b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2457b83cc969SLi Zefan return -EROFS; 2458b83cc969SLi Zefan 2459a561be71SAl Viro ret = mnt_want_write_file(file); 2460c146afadSYan Zheng if (ret) 2461c146afadSYan Zheng return ret; 2462c146afadSYan Zheng 24632903ff01SAl Viro src_file = fdget(srcfd); 24642903ff01SAl Viro if (!src_file.file) { 2465ab67b7c1SYan Zheng ret = -EBADF; 2466ab67b7c1SYan Zheng goto out_drop_write; 2467ab67b7c1SYan Zheng } 24685dc64164SDan Rosenberg 2469362a20c5SDavid Sterba ret = -EXDEV; 24702903ff01SAl Viro if (src_file.file->f_path.mnt != file->f_path.mnt) 2471362a20c5SDavid Sterba goto out_fput; 2472362a20c5SDavid Sterba 24732903ff01SAl Viro src = src_file.file->f_dentry->d_inode; 2474f46b5a66SChristoph Hellwig 2475c5c9cd4dSSage Weil ret = -EINVAL; 2476c5c9cd4dSSage Weil if (src == inode) 2477c5c9cd4dSSage Weil goto out_fput; 2478c5c9cd4dSSage Weil 24795dc64164SDan Rosenberg /* the src must be open for reading */ 24802903ff01SAl Viro if (!(src_file.file->f_mode & FMODE_READ)) 24815dc64164SDan Rosenberg goto out_fput; 24825dc64164SDan Rosenberg 24830e7b824cSLi Zefan /* don't make the dst file partly checksummed */ 24840e7b824cSLi Zefan if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 24850e7b824cSLi Zefan (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 24860e7b824cSLi Zefan goto out_fput; 24870e7b824cSLi Zefan 2488ae01a0abSYan Zheng ret = -EISDIR; 2489ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2490f46b5a66SChristoph Hellwig goto out_fput; 2491f46b5a66SChristoph Hellwig 2492ae01a0abSYan Zheng ret = -EXDEV; 2493362a20c5SDavid Sterba if (src->i_sb != inode->i_sb) 2494ae01a0abSYan Zheng goto out_fput; 2495ae01a0abSYan Zheng 2496ae01a0abSYan Zheng ret = -ENOMEM; 2497ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2498ae01a0abSYan Zheng if (!buf) 2499ae01a0abSYan Zheng goto out_fput; 2500ae01a0abSYan Zheng 2501ae01a0abSYan Zheng path = btrfs_alloc_path(); 2502ae01a0abSYan Zheng if (!path) { 2503ae01a0abSYan Zheng vfree(buf); 2504ae01a0abSYan Zheng goto out_fput; 2505ae01a0abSYan Zheng } 2506ae01a0abSYan Zheng path->reada = 2; 2507ae01a0abSYan Zheng 2508f46b5a66SChristoph Hellwig if (inode < src) { 2509fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 2510fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 2511f46b5a66SChristoph Hellwig } else { 2512fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 2513fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 2514f46b5a66SChristoph Hellwig } 2515f46b5a66SChristoph Hellwig 2516c5c9cd4dSSage Weil /* determine range to clone */ 2517c5c9cd4dSSage Weil ret = -EINVAL; 25182ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 2519f46b5a66SChristoph Hellwig goto out_unlock; 2520c5c9cd4dSSage Weil if (len == 0) 2521c5c9cd4dSSage Weil olen = len = src->i_size - off; 2522c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 2523c5c9cd4dSSage Weil if (off + len == src->i_size) 25242a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 2525c5c9cd4dSSage Weil 2526c5c9cd4dSSage Weil /* verify the end result is block aligned */ 25272a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 25282a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 2529c5c9cd4dSSage Weil goto out_unlock; 2530c5c9cd4dSSage Weil 2531d525e8abSLi Zefan if (destoff > inode->i_size) { 2532d525e8abSLi Zefan ret = btrfs_cont_expand(inode, inode->i_size, destoff); 2533d525e8abSLi Zefan if (ret) 2534d525e8abSLi Zefan goto out_unlock; 2535d525e8abSLi Zefan } 2536d525e8abSLi Zefan 253771ef0786SLi Zefan /* truncate page cache pages from target inode range */ 253871ef0786SLi Zefan truncate_inode_pages_range(&inode->i_data, destoff, 253971ef0786SLi Zefan PAGE_CACHE_ALIGN(destoff + len) - 1); 254071ef0786SLi Zefan 2541f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 2542f46b5a66SChristoph Hellwig another, and lock file content */ 2543f46b5a66SChristoph Hellwig while (1) { 254431840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 2545aa42ffd9SLiu Bo lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2546aa42ffd9SLiu Bo ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1); 25479a019196SSage Weil if (!ordered && 2548aa42ffd9SLiu Bo !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1, 25499a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 2550f46b5a66SChristoph Hellwig break; 2551aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2552ae01a0abSYan Zheng if (ordered) 2553ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 25549a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 2555f46b5a66SChristoph Hellwig } 2556f46b5a66SChristoph Hellwig 2557c5c9cd4dSSage Weil /* clone data */ 255833345d01SLi Zefan key.objectid = btrfs_ino(src); 2559ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2560ae01a0abSYan Zheng key.offset = 0; 2561f46b5a66SChristoph Hellwig 2562f46b5a66SChristoph Hellwig while (1) { 2563f46b5a66SChristoph Hellwig /* 2564f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2565f46b5a66SChristoph Hellwig * tree. 2566f46b5a66SChristoph Hellwig */ 2567362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 2568362a20c5SDavid Sterba 0, 0); 2569f46b5a66SChristoph Hellwig if (ret < 0) 2570f46b5a66SChristoph Hellwig goto out; 2571f46b5a66SChristoph Hellwig 2572ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2573ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2574362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 2575f46b5a66SChristoph Hellwig if (ret < 0) 2576f46b5a66SChristoph Hellwig goto out; 2577f46b5a66SChristoph Hellwig if (ret > 0) 2578f46b5a66SChristoph Hellwig break; 2579ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2580f46b5a66SChristoph Hellwig } 2581f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2582f46b5a66SChristoph Hellwig slot = path->slots[0]; 2583f46b5a66SChristoph Hellwig 2584ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2585d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 258633345d01SLi Zefan key.objectid != btrfs_ino(src)) 2587f46b5a66SChristoph Hellwig break; 2588f46b5a66SChristoph Hellwig 2589c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2590c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2591c5c9cd4dSSage Weil int type; 259231840ae1SZheng Yan u32 size; 259331840ae1SZheng Yan struct btrfs_key new_key; 2594c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2595c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2596c5c9cd4dSSage Weil u8 comp; 2597b5384d48SSage Weil u64 endoff; 259831840ae1SZheng Yan 259931840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 260031840ae1SZheng Yan read_extent_buffer(leaf, buf, 260131840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 260231840ae1SZheng Yan size); 2603c5c9cd4dSSage Weil 2604c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2605c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2606c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2607c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2608c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2609c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2610d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2611d397712bSChris Mason extent); 2612d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2613d397712bSChris Mason extent); 2614c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2615d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2616d397712bSChris Mason extent); 2617c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2618c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2619c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2620c5c9cd4dSSage Weil extent); 2621c5c9cd4dSSage Weil } 2622b3b4aa74SDavid Sterba btrfs_release_path(path); 262331840ae1SZheng Yan 2624050006a7SSage Weil if (key.offset + datal <= off || 2625aa42ffd9SLiu Bo key.offset >= off + len - 1) 2626c5c9cd4dSSage Weil goto next; 2627c5c9cd4dSSage Weil 262831840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 262933345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 26304d728ec7SLi Zefan if (off <= key.offset) 2631c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 26324d728ec7SLi Zefan else 26334d728ec7SLi Zefan new_key.offset = destoff; 2634c5c9cd4dSSage Weil 2635b6f3409bSSage Weil /* 2636b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2637b6f3409bSSage Weil * 1 - add new extent 2638b6f3409bSSage Weil * 1 - inode update 2639b6f3409bSSage Weil */ 2640b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2641a22285a6SYan, Zheng if (IS_ERR(trans)) { 2642a22285a6SYan, Zheng ret = PTR_ERR(trans); 2643a22285a6SYan, Zheng goto out; 2644a22285a6SYan, Zheng } 2645a22285a6SYan, Zheng 2646c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2647c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2648d72c0842SLi Zefan /* 2649d72c0842SLi Zefan * a | --- range to clone ---| b 2650d72c0842SLi Zefan * | ------------- extent ------------- | 2651d72c0842SLi Zefan */ 2652d72c0842SLi Zefan 2653d72c0842SLi Zefan /* substract range b */ 2654d72c0842SLi Zefan if (key.offset + datal > off + len) 2655d72c0842SLi Zefan datal = off + len - key.offset; 2656d72c0842SLi Zefan 2657d72c0842SLi Zefan /* substract range a */ 2658a22285a6SYan, Zheng if (off > key.offset) { 2659a22285a6SYan, Zheng datao += off - key.offset; 2660a22285a6SYan, Zheng datal -= off - key.offset; 2661a22285a6SYan, Zheng } 2662a22285a6SYan, Zheng 26635dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2664a22285a6SYan, Zheng new_key.offset, 2665a22285a6SYan, Zheng new_key.offset + datal, 26662671485dSJosef Bacik 1); 266779787eaaSJeff Mahoney if (ret) { 266879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 266979787eaaSJeff Mahoney ret); 267079787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 267179787eaaSJeff Mahoney goto out; 267279787eaaSJeff Mahoney } 2673a22285a6SYan, Zheng 267431840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 267531840ae1SZheng Yan &new_key, size); 267679787eaaSJeff Mahoney if (ret) { 267779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 267879787eaaSJeff Mahoney ret); 267979787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 268079787eaaSJeff Mahoney goto out; 268179787eaaSJeff Mahoney } 268231840ae1SZheng Yan 268331840ae1SZheng Yan leaf = path->nodes[0]; 268431840ae1SZheng Yan slot = path->slots[0]; 268531840ae1SZheng Yan write_extent_buffer(leaf, buf, 268631840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 268731840ae1SZheng Yan size); 2688ae01a0abSYan Zheng 2689f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 2690f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 2691c5c9cd4dSSage Weil 2692c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 2693c5c9cd4dSSage Weil if (!disko) 2694c5c9cd4dSSage Weil datao = 0; 2695c5c9cd4dSSage Weil 2696c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 2697c5c9cd4dSSage Weil datao); 2698c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 2699c5c9cd4dSSage Weil datal); 2700c5c9cd4dSSage Weil if (disko) { 2701c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2702ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 27035d4f98a2SYan Zheng disko, diskl, 0, 2704f46b5a66SChristoph Hellwig root->root_key.objectid, 270533345d01SLi Zefan btrfs_ino(inode), 270666d7e7f0SArne Jansen new_key.offset - datao, 270766d7e7f0SArne Jansen 0); 270879787eaaSJeff Mahoney if (ret) { 270979787eaaSJeff Mahoney btrfs_abort_transaction(trans, 271079787eaaSJeff Mahoney root, 271179787eaaSJeff Mahoney ret); 271279787eaaSJeff Mahoney btrfs_end_transaction(trans, 271379787eaaSJeff Mahoney root); 271479787eaaSJeff Mahoney goto out; 271579787eaaSJeff Mahoney 271679787eaaSJeff Mahoney } 2717f46b5a66SChristoph Hellwig } 2718c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2719c5c9cd4dSSage Weil u64 skip = 0; 2720c5c9cd4dSSage Weil u64 trim = 0; 2721c5c9cd4dSSage Weil if (off > key.offset) { 2722c5c9cd4dSSage Weil skip = off - key.offset; 2723c5c9cd4dSSage Weil new_key.offset += skip; 272431840ae1SZheng Yan } 2725d397712bSChris Mason 2726c5c9cd4dSSage Weil if (key.offset + datal > off + len) 2727c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 2728d397712bSChris Mason 2729c5c9cd4dSSage Weil if (comp && (skip || trim)) { 2730c5c9cd4dSSage Weil ret = -EINVAL; 2731a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2732c5c9cd4dSSage Weil goto out; 273331840ae1SZheng Yan } 2734c5c9cd4dSSage Weil size -= skip + trim; 2735c5c9cd4dSSage Weil datal -= skip + trim; 2736a22285a6SYan, Zheng 27375dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2738a22285a6SYan, Zheng new_key.offset, 2739a22285a6SYan, Zheng new_key.offset + datal, 27402671485dSJosef Bacik 1); 274179787eaaSJeff Mahoney if (ret) { 274279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 274379787eaaSJeff Mahoney ret); 274479787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 274579787eaaSJeff Mahoney goto out; 274679787eaaSJeff Mahoney } 2747a22285a6SYan, Zheng 2748c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 2749c5c9cd4dSSage Weil &new_key, size); 275079787eaaSJeff Mahoney if (ret) { 275179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 275279787eaaSJeff Mahoney ret); 275379787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 275479787eaaSJeff Mahoney goto out; 275579787eaaSJeff Mahoney } 2756c5c9cd4dSSage Weil 2757c5c9cd4dSSage Weil if (skip) { 2758d397712bSChris Mason u32 start = 2759d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 2760c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 2761c5c9cd4dSSage Weil datal); 2762c5c9cd4dSSage Weil } 2763c5c9cd4dSSage Weil 2764c5c9cd4dSSage Weil leaf = path->nodes[0]; 2765c5c9cd4dSSage Weil slot = path->slots[0]; 2766c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2767c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2768c5c9cd4dSSage Weil size); 2769c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2770c5c9cd4dSSage Weil } 2771c5c9cd4dSSage Weil 2772c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2773b3b4aa74SDavid Sterba btrfs_release_path(path); 2774c5c9cd4dSSage Weil 27750c4d2d95SJosef Bacik inode_inc_iversion(inode); 2776a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2777b5384d48SSage Weil 2778b5384d48SSage Weil /* 2779b5384d48SSage Weil * we round up to the block size at eof when 2780b5384d48SSage Weil * determining which extents to clone above, 2781b5384d48SSage Weil * but shouldn't round up the file size 2782b5384d48SSage Weil */ 2783b5384d48SSage Weil endoff = new_key.offset + datal; 27845f3888ffSLi Zefan if (endoff > destoff+olen) 27855f3888ffSLi Zefan endoff = destoff+olen; 2786b5384d48SSage Weil if (endoff > inode->i_size) 2787b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2788b5384d48SSage Weil 2789a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 279079787eaaSJeff Mahoney if (ret) { 279179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 2792a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 279379787eaaSJeff Mahoney goto out; 279479787eaaSJeff Mahoney } 279579787eaaSJeff Mahoney ret = btrfs_end_transaction(trans, root); 2796a22285a6SYan, Zheng } 2797c5c9cd4dSSage Weil next: 2798b3b4aa74SDavid Sterba btrfs_release_path(path); 2799ae01a0abSYan Zheng key.offset++; 2800ae01a0abSYan Zheng } 2801f46b5a66SChristoph Hellwig ret = 0; 2802f46b5a66SChristoph Hellwig out: 2803b3b4aa74SDavid Sterba btrfs_release_path(path); 2804aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 2805f46b5a66SChristoph Hellwig out_unlock: 2806f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2807f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2808ae01a0abSYan Zheng vfree(buf); 2809ae01a0abSYan Zheng btrfs_free_path(path); 2810f46b5a66SChristoph Hellwig out_fput: 28112903ff01SAl Viro fdput(src_file); 2812ab67b7c1SYan Zheng out_drop_write: 28132a79f17eSAl Viro mnt_drop_write_file(file); 2814f46b5a66SChristoph Hellwig return ret; 2815f46b5a66SChristoph Hellwig } 2816f46b5a66SChristoph Hellwig 28177a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2818c5c9cd4dSSage Weil { 2819c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2820c5c9cd4dSSage Weil 28217a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2822c5c9cd4dSSage Weil return -EFAULT; 2823c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2824c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2825c5c9cd4dSSage Weil } 2826c5c9cd4dSSage Weil 2827f46b5a66SChristoph Hellwig /* 2828f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2829f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2830f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2831f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2832f46b5a66SChristoph Hellwig */ 2833b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2834f46b5a66SChristoph Hellwig { 2835f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2836f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2837f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 28381ab86aedSSage Weil int ret; 2839f46b5a66SChristoph Hellwig 28401ab86aedSSage Weil ret = -EPERM; 2841df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2842f46b5a66SChristoph Hellwig goto out; 28431ab86aedSSage Weil 28441ab86aedSSage Weil ret = -EINPROGRESS; 28451ab86aedSSage Weil if (file->private_data) 28461ab86aedSSage Weil goto out; 28479ca9ee09SSage Weil 2848b83cc969SLi Zefan ret = -EROFS; 2849b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2850b83cc969SLi Zefan goto out; 2851b83cc969SLi Zefan 2852a561be71SAl Viro ret = mnt_want_write_file(file); 2853c146afadSYan Zheng if (ret) 2854c146afadSYan Zheng goto out; 2855c146afadSYan Zheng 2856a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 28579ca9ee09SSage Weil 2858f46b5a66SChristoph Hellwig ret = -ENOMEM; 28597a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 2860abd30bb0STsutomu Itoh if (IS_ERR(trans)) 28611ab86aedSSage Weil goto out_drop; 28621ab86aedSSage Weil 28631ab86aedSSage Weil file->private_data = trans; 28641ab86aedSSage Weil return 0; 28651ab86aedSSage Weil 28661ab86aedSSage Weil out_drop: 2867a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 28682a79f17eSAl Viro mnt_drop_write_file(file); 2869f46b5a66SChristoph Hellwig out: 2870f46b5a66SChristoph Hellwig return ret; 2871f46b5a66SChristoph Hellwig } 2872f46b5a66SChristoph Hellwig 28736ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 28746ef5ed0dSJosef Bacik { 28756ef5ed0dSJosef Bacik struct inode *inode = fdentry(file)->d_inode; 28766ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 28776ef5ed0dSJosef Bacik struct btrfs_root *new_root; 28786ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 28796ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 28806ef5ed0dSJosef Bacik struct btrfs_path *path; 28816ef5ed0dSJosef Bacik struct btrfs_key location; 28826ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 28836ef5ed0dSJosef Bacik u64 objectid = 0; 28846ef5ed0dSJosef Bacik u64 dir_id; 28853c04ce01SMiao Xie int ret; 28866ef5ed0dSJosef Bacik 28876ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 28886ef5ed0dSJosef Bacik return -EPERM; 28896ef5ed0dSJosef Bacik 28903c04ce01SMiao Xie ret = mnt_want_write_file(file); 28913c04ce01SMiao Xie if (ret) 28923c04ce01SMiao Xie return ret; 28933c04ce01SMiao Xie 28943c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 28953c04ce01SMiao Xie ret = -EFAULT; 28963c04ce01SMiao Xie goto out; 28973c04ce01SMiao Xie } 28986ef5ed0dSJosef Bacik 28996ef5ed0dSJosef Bacik if (!objectid) 29006ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 29016ef5ed0dSJosef Bacik 29026ef5ed0dSJosef Bacik location.objectid = objectid; 29036ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 29046ef5ed0dSJosef Bacik location.offset = (u64)-1; 29056ef5ed0dSJosef Bacik 29066ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 29073c04ce01SMiao Xie if (IS_ERR(new_root)) { 29083c04ce01SMiao Xie ret = PTR_ERR(new_root); 29093c04ce01SMiao Xie goto out; 29103c04ce01SMiao Xie } 29116ef5ed0dSJosef Bacik 29123c04ce01SMiao Xie if (btrfs_root_refs(&new_root->root_item) == 0) { 29133c04ce01SMiao Xie ret = -ENOENT; 29143c04ce01SMiao Xie goto out; 29153c04ce01SMiao Xie } 29166ef5ed0dSJosef Bacik 29176ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 29183c04ce01SMiao Xie if (!path) { 29193c04ce01SMiao Xie ret = -ENOMEM; 29203c04ce01SMiao Xie goto out; 29213c04ce01SMiao Xie } 29226ef5ed0dSJosef Bacik path->leave_spinning = 1; 29236ef5ed0dSJosef Bacik 29246ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 292598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 29266ef5ed0dSJosef Bacik btrfs_free_path(path); 29273c04ce01SMiao Xie ret = PTR_ERR(trans); 29283c04ce01SMiao Xie goto out; 29296ef5ed0dSJosef Bacik } 29306ef5ed0dSJosef Bacik 29316c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 29326ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 29336ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2934cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 29356ef5ed0dSJosef Bacik btrfs_free_path(path); 29366ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 29376ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 29386ef5ed0dSJosef Bacik "this isn't going to work\n"); 29393c04ce01SMiao Xie ret = -ENOENT; 29403c04ce01SMiao Xie goto out; 29416ef5ed0dSJosef Bacik } 29426ef5ed0dSJosef Bacik 29436ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 29446ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 29456ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 29466ef5ed0dSJosef Bacik btrfs_free_path(path); 29476ef5ed0dSJosef Bacik 29482b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 29496ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 29503c04ce01SMiao Xie out: 29513c04ce01SMiao Xie mnt_drop_write_file(file); 29523c04ce01SMiao Xie return ret; 29536ef5ed0dSJosef Bacik } 29546ef5ed0dSJosef Bacik 29555af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 2956bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 2957bf5fc093SJosef Bacik { 2958bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 2959bf5fc093SJosef Bacik 2960bf5fc093SJosef Bacik space->total_bytes = 0; 2961bf5fc093SJosef Bacik space->used_bytes = 0; 2962bf5fc093SJosef Bacik space->flags = 0; 2963bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 2964bf5fc093SJosef Bacik space->flags = block_group->flags; 2965bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 2966bf5fc093SJosef Bacik space->used_bytes += 2967bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 2968bf5fc093SJosef Bacik } 2969bf5fc093SJosef Bacik } 2970bf5fc093SJosef Bacik 29711406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 29721406e432SJosef Bacik { 29731406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 29741406e432SJosef Bacik struct btrfs_ioctl_space_info space; 29751406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 29767fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 297713f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 29781406e432SJosef Bacik struct btrfs_space_info *info; 2979bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 2980bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 2981bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 2982bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 2983bf5fc093SJosef Bacik int num_types = 4; 29847fde62bfSChris Mason int alloc_size; 29851406e432SJosef Bacik int ret = 0; 298651788b1bSDan Rosenberg u64 slot_count = 0; 2987bf5fc093SJosef Bacik int i, c; 29881406e432SJosef Bacik 29891406e432SJosef Bacik if (copy_from_user(&space_args, 29901406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 29911406e432SJosef Bacik sizeof(space_args))) 29921406e432SJosef Bacik return -EFAULT; 29931406e432SJosef Bacik 2994bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2995bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2996bf5fc093SJosef Bacik 2997bf5fc093SJosef Bacik info = NULL; 29987fde62bfSChris Mason rcu_read_lock(); 2999bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3000bf5fc093SJosef Bacik list) { 3001bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3002bf5fc093SJosef Bacik info = tmp; 3003bf5fc093SJosef Bacik break; 3004bf5fc093SJosef Bacik } 3005bf5fc093SJosef Bacik } 30067fde62bfSChris Mason rcu_read_unlock(); 30071406e432SJosef Bacik 3008bf5fc093SJosef Bacik if (!info) 3009bf5fc093SJosef Bacik continue; 3010bf5fc093SJosef Bacik 3011bf5fc093SJosef Bacik down_read(&info->groups_sem); 3012bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3013bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3014bf5fc093SJosef Bacik slot_count++; 3015bf5fc093SJosef Bacik } 3016bf5fc093SJosef Bacik up_read(&info->groups_sem); 3017bf5fc093SJosef Bacik } 3018bf5fc093SJosef Bacik 30197fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 30207fde62bfSChris Mason if (space_args.space_slots == 0) { 30217fde62bfSChris Mason space_args.total_spaces = slot_count; 30227fde62bfSChris Mason goto out; 30237fde62bfSChris Mason } 3024bf5fc093SJosef Bacik 302551788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3026bf5fc093SJosef Bacik 30277fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3028bf5fc093SJosef Bacik 30297fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 30307fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 30317fde62bfSChris Mason */ 30327fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 30337fde62bfSChris Mason return -ENOMEM; 30347fde62bfSChris Mason 30357fde62bfSChris Mason space_args.total_spaces = 0; 30367fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 30377fde62bfSChris Mason if (!dest) 30387fde62bfSChris Mason return -ENOMEM; 30397fde62bfSChris Mason dest_orig = dest; 30407fde62bfSChris Mason 30417fde62bfSChris Mason /* now we have a buffer to copy into */ 3042bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3043bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3044bf5fc093SJosef Bacik 304551788b1bSDan Rosenberg if (!slot_count) 304651788b1bSDan Rosenberg break; 304751788b1bSDan Rosenberg 3048bf5fc093SJosef Bacik info = NULL; 30491406e432SJosef Bacik rcu_read_lock(); 3050bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3051bf5fc093SJosef Bacik list) { 3052bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3053bf5fc093SJosef Bacik info = tmp; 30547fde62bfSChris Mason break; 3055bf5fc093SJosef Bacik } 3056bf5fc093SJosef Bacik } 3057bf5fc093SJosef Bacik rcu_read_unlock(); 30587fde62bfSChris Mason 3059bf5fc093SJosef Bacik if (!info) 3060bf5fc093SJosef Bacik continue; 3061bf5fc093SJosef Bacik down_read(&info->groups_sem); 3062bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3063bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 30645af3e8ccSStefan Behrens btrfs_get_block_group_info( 30655af3e8ccSStefan Behrens &info->block_groups[c], &space); 30667fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 30671406e432SJosef Bacik dest++; 30681406e432SJosef Bacik space_args.total_spaces++; 306951788b1bSDan Rosenberg slot_count--; 30701406e432SJosef Bacik } 307151788b1bSDan Rosenberg if (!slot_count) 307251788b1bSDan Rosenberg break; 3073bf5fc093SJosef Bacik } 3074bf5fc093SJosef Bacik up_read(&info->groups_sem); 3075bf5fc093SJosef Bacik } 30761406e432SJosef Bacik 30772eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 30787fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 30797fde62bfSChris Mason 30807fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 30817fde62bfSChris Mason ret = -EFAULT; 30827fde62bfSChris Mason 30837fde62bfSChris Mason kfree(dest_orig); 30847fde62bfSChris Mason out: 30857fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 30861406e432SJosef Bacik ret = -EFAULT; 30871406e432SJosef Bacik 30881406e432SJosef Bacik return ret; 30891406e432SJosef Bacik } 30901406e432SJosef Bacik 3091f46b5a66SChristoph Hellwig /* 3092f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3093f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3094f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3095f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3096f46b5a66SChristoph Hellwig */ 3097f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3098f46b5a66SChristoph Hellwig { 3099f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 3100f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3101f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3102f46b5a66SChristoph Hellwig 3103f46b5a66SChristoph Hellwig trans = file->private_data; 31041ab86aedSSage Weil if (!trans) 31051ab86aedSSage Weil return -EINVAL; 3106b214107eSChristoph Hellwig file->private_data = NULL; 31079ca9ee09SSage Weil 31081ab86aedSSage Weil btrfs_end_transaction(trans, root); 31091ab86aedSSage Weil 3110a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 31119ca9ee09SSage Weil 31122a79f17eSAl Viro mnt_drop_write_file(file); 31131ab86aedSSage Weil return 0; 3114f46b5a66SChristoph Hellwig } 3115f46b5a66SChristoph Hellwig 31169a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 31179a8c28beSMiao Xie void __user *argp) 311846204592SSage Weil { 311946204592SSage Weil struct btrfs_trans_handle *trans; 312046204592SSage Weil u64 transid; 3121db5b493aSTsutomu Itoh int ret; 312246204592SSage Weil 3123d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3124ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3125ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 312698d5dc13STsutomu Itoh return PTR_ERR(trans); 3127ff7c1d33SMiao Xie 3128ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3129ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3130ff7c1d33SMiao Xie goto out; 3131ff7c1d33SMiao Xie } 313246204592SSage Weil transid = trans->transid; 3133db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 31348b2b2d3cSTsutomu Itoh if (ret) { 31358b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 3136db5b493aSTsutomu Itoh return ret; 31378b2b2d3cSTsutomu Itoh } 3138ff7c1d33SMiao Xie out: 313946204592SSage Weil if (argp) 314046204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 314146204592SSage Weil return -EFAULT; 314246204592SSage Weil return 0; 314346204592SSage Weil } 314446204592SSage Weil 31459a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 31469a8c28beSMiao Xie void __user *argp) 314746204592SSage Weil { 314846204592SSage Weil u64 transid; 314946204592SSage Weil 315046204592SSage Weil if (argp) { 315146204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 315246204592SSage Weil return -EFAULT; 315346204592SSage Weil } else { 315446204592SSage Weil transid = 0; /* current trans */ 315546204592SSage Weil } 315646204592SSage Weil return btrfs_wait_for_commit(root, transid); 315746204592SSage Weil } 315846204592SSage Weil 3159b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 3160475f6387SJan Schmidt { 3161b8e95489SMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3162475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3163b8e95489SMiao Xie int ret; 3164475f6387SJan Schmidt 3165475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3166475f6387SJan Schmidt return -EPERM; 3167475f6387SJan Schmidt 3168475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3169475f6387SJan Schmidt if (IS_ERR(sa)) 3170475f6387SJan Schmidt return PTR_ERR(sa); 3171475f6387SJan Schmidt 3172b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 3173b8e95489SMiao Xie ret = mnt_want_write_file(file); 3174b8e95489SMiao Xie if (ret) 3175b8e95489SMiao Xie goto out; 3176b8e95489SMiao Xie } 3177b8e95489SMiao Xie 3178aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 317963a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 318063a212abSStefan Behrens 0); 3181475f6387SJan Schmidt 3182475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3183475f6387SJan Schmidt ret = -EFAULT; 3184475f6387SJan Schmidt 3185b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 3186b8e95489SMiao Xie mnt_drop_write_file(file); 3187b8e95489SMiao Xie out: 3188475f6387SJan Schmidt kfree(sa); 3189475f6387SJan Schmidt return ret; 3190475f6387SJan Schmidt } 3191475f6387SJan Schmidt 3192475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 3193475f6387SJan Schmidt { 3194475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3195475f6387SJan Schmidt return -EPERM; 3196475f6387SJan Schmidt 3197aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 3198475f6387SJan Schmidt } 3199475f6387SJan Schmidt 3200475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 3201475f6387SJan Schmidt void __user *arg) 3202475f6387SJan Schmidt { 3203475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3204475f6387SJan Schmidt int ret; 3205475f6387SJan Schmidt 3206475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3207475f6387SJan Schmidt return -EPERM; 3208475f6387SJan Schmidt 3209475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3210475f6387SJan Schmidt if (IS_ERR(sa)) 3211475f6387SJan Schmidt return PTR_ERR(sa); 3212475f6387SJan Schmidt 3213475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 3214475f6387SJan Schmidt 3215475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3216475f6387SJan Schmidt ret = -EFAULT; 3217475f6387SJan Schmidt 3218475f6387SJan Schmidt kfree(sa); 3219475f6387SJan Schmidt return ret; 3220475f6387SJan Schmidt } 3221475f6387SJan Schmidt 3222c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 3223b27f7c0cSDavid Sterba void __user *arg) 3224c11d2c23SStefan Behrens { 3225c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 3226c11d2c23SStefan Behrens int ret; 3227c11d2c23SStefan Behrens 3228c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 3229c11d2c23SStefan Behrens if (IS_ERR(sa)) 3230c11d2c23SStefan Behrens return PTR_ERR(sa); 3231c11d2c23SStefan Behrens 3232b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 3233b27f7c0cSDavid Sterba kfree(sa); 3234b27f7c0cSDavid Sterba return -EPERM; 3235b27f7c0cSDavid Sterba } 3236b27f7c0cSDavid Sterba 3237b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 3238c11d2c23SStefan Behrens 3239c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 3240c11d2c23SStefan Behrens ret = -EFAULT; 3241c11d2c23SStefan Behrens 3242c11d2c23SStefan Behrens kfree(sa); 3243c11d2c23SStefan Behrens return ret; 3244c11d2c23SStefan Behrens } 3245c11d2c23SStefan Behrens 32463f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 32473f6bcfbdSStefan Behrens { 32483f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 32493f6bcfbdSStefan Behrens int ret; 32503f6bcfbdSStefan Behrens 32513f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 32523f6bcfbdSStefan Behrens return -EPERM; 32533f6bcfbdSStefan Behrens 32543f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 32553f6bcfbdSStefan Behrens if (IS_ERR(p)) 32563f6bcfbdSStefan Behrens return PTR_ERR(p); 32573f6bcfbdSStefan Behrens 32583f6bcfbdSStefan Behrens switch (p->cmd) { 32593f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 32603f6bcfbdSStefan Behrens if (atomic_xchg( 32613f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 32623f6bcfbdSStefan Behrens 1)) { 32633f6bcfbdSStefan Behrens pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 32643f6bcfbdSStefan Behrens ret = -EINPROGRESS; 32653f6bcfbdSStefan Behrens } else { 32663f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 32673f6bcfbdSStefan Behrens atomic_set( 32683f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 32693f6bcfbdSStefan Behrens 0); 32703f6bcfbdSStefan Behrens } 32713f6bcfbdSStefan Behrens break; 32723f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 32733f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 32743f6bcfbdSStefan Behrens ret = 0; 32753f6bcfbdSStefan Behrens break; 32763f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 32773f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 32783f6bcfbdSStefan Behrens break; 32793f6bcfbdSStefan Behrens default: 32803f6bcfbdSStefan Behrens ret = -EINVAL; 32813f6bcfbdSStefan Behrens break; 32823f6bcfbdSStefan Behrens } 32833f6bcfbdSStefan Behrens 32843f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 32853f6bcfbdSStefan Behrens ret = -EFAULT; 32863f6bcfbdSStefan Behrens 32873f6bcfbdSStefan Behrens kfree(p); 32883f6bcfbdSStefan Behrens return ret; 32893f6bcfbdSStefan Behrens } 32903f6bcfbdSStefan Behrens 3291d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 3292d7728c96SJan Schmidt { 3293d7728c96SJan Schmidt int ret = 0; 3294d7728c96SJan Schmidt int i; 3295740c3d22SChris Mason u64 rel_ptr; 3296d7728c96SJan Schmidt int size; 3297806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 3298d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 3299d7728c96SJan Schmidt struct btrfs_path *path; 3300d7728c96SJan Schmidt 330182b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 3302d7728c96SJan Schmidt return -EPERM; 3303d7728c96SJan Schmidt 3304d7728c96SJan Schmidt path = btrfs_alloc_path(); 3305d7728c96SJan Schmidt if (!path) { 3306d7728c96SJan Schmidt ret = -ENOMEM; 3307d7728c96SJan Schmidt goto out; 3308d7728c96SJan Schmidt } 3309d7728c96SJan Schmidt 3310d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 3311d7728c96SJan Schmidt if (IS_ERR(ipa)) { 3312d7728c96SJan Schmidt ret = PTR_ERR(ipa); 3313d7728c96SJan Schmidt ipa = NULL; 3314d7728c96SJan Schmidt goto out; 3315d7728c96SJan Schmidt } 3316d7728c96SJan Schmidt 3317d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 3318d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 3319d7728c96SJan Schmidt if (IS_ERR(ipath)) { 3320d7728c96SJan Schmidt ret = PTR_ERR(ipath); 3321d7728c96SJan Schmidt ipath = NULL; 3322d7728c96SJan Schmidt goto out; 3323d7728c96SJan Schmidt } 3324d7728c96SJan Schmidt 3325d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 3326d7728c96SJan Schmidt if (ret < 0) 3327d7728c96SJan Schmidt goto out; 3328d7728c96SJan Schmidt 3329d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 3330745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 3331745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 3332740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 3333d7728c96SJan Schmidt } 3334d7728c96SJan Schmidt 3335745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 3336745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 3337d7728c96SJan Schmidt if (ret) { 3338d7728c96SJan Schmidt ret = -EFAULT; 3339d7728c96SJan Schmidt goto out; 3340d7728c96SJan Schmidt } 3341d7728c96SJan Schmidt 3342d7728c96SJan Schmidt out: 3343d7728c96SJan Schmidt btrfs_free_path(path); 3344d7728c96SJan Schmidt free_ipath(ipath); 3345d7728c96SJan Schmidt kfree(ipa); 3346d7728c96SJan Schmidt 3347d7728c96SJan Schmidt return ret; 3348d7728c96SJan Schmidt } 3349d7728c96SJan Schmidt 3350d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 3351d7728c96SJan Schmidt { 3352d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 3353d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 3354d7728c96SJan Schmidt 3355d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 3356d7728c96SJan Schmidt inodes->bytes_left -= c; 3357d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 3358d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 3359d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 3360d7728c96SJan Schmidt inodes->elem_cnt += 3; 3361d7728c96SJan Schmidt } else { 3362d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 3363d7728c96SJan Schmidt inodes->bytes_left = 0; 3364d7728c96SJan Schmidt inodes->elem_missed += 3; 3365d7728c96SJan Schmidt } 3366d7728c96SJan Schmidt 3367d7728c96SJan Schmidt return 0; 3368d7728c96SJan Schmidt } 3369d7728c96SJan Schmidt 3370d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 3371d7728c96SJan Schmidt void __user *arg) 3372d7728c96SJan Schmidt { 3373d7728c96SJan Schmidt int ret = 0; 3374d7728c96SJan Schmidt int size; 3375d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3376d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3377d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3378d7728c96SJan Schmidt 3379d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3380d7728c96SJan Schmidt return -EPERM; 3381d7728c96SJan Schmidt 3382d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 3383d7728c96SJan Schmidt if (IS_ERR(loi)) { 3384d7728c96SJan Schmidt ret = PTR_ERR(loi); 3385d7728c96SJan Schmidt loi = NULL; 3386d7728c96SJan Schmidt goto out; 3387d7728c96SJan Schmidt } 3388d7728c96SJan Schmidt 3389d7728c96SJan Schmidt path = btrfs_alloc_path(); 3390d7728c96SJan Schmidt if (!path) { 3391d7728c96SJan Schmidt ret = -ENOMEM; 3392d7728c96SJan Schmidt goto out; 3393d7728c96SJan Schmidt } 3394d7728c96SJan Schmidt 3395425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 3396d7728c96SJan Schmidt inodes = init_data_container(size); 3397d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3398d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3399d7728c96SJan Schmidt inodes = NULL; 3400d7728c96SJan Schmidt goto out; 3401d7728c96SJan Schmidt } 3402d7728c96SJan Schmidt 3403df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 3404df031f07SLiu Bo build_ino_list, inodes); 3405df031f07SLiu Bo if (ret == -EINVAL) 3406d7728c96SJan Schmidt ret = -ENOENT; 3407d7728c96SJan Schmidt if (ret < 0) 3408d7728c96SJan Schmidt goto out; 3409d7728c96SJan Schmidt 3410745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 3411745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 3412d7728c96SJan Schmidt if (ret) 3413d7728c96SJan Schmidt ret = -EFAULT; 3414d7728c96SJan Schmidt 3415d7728c96SJan Schmidt out: 3416d7728c96SJan Schmidt btrfs_free_path(path); 3417425d17a2SLiu Bo vfree(inodes); 3418d7728c96SJan Schmidt kfree(loi); 3419d7728c96SJan Schmidt 3420d7728c96SJan Schmidt return ret; 3421d7728c96SJan Schmidt } 3422d7728c96SJan Schmidt 342319a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 3424c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3425c9e9f97bSIlya Dryomov { 3426c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3427c9e9f97bSIlya Dryomov 3428c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3429c9e9f97bSIlya Dryomov 3430837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 3431837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 3432837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 3433837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 3434a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 3435a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 3436837d5b6eSIlya Dryomov 3437c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 3438c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 3439c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 344019a39dceSIlya Dryomov 344119a39dceSIlya Dryomov if (lock) { 344219a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 344319a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 344419a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 344519a39dceSIlya Dryomov } else { 344619a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 344719a39dceSIlya Dryomov } 3448c9e9f97bSIlya Dryomov } 3449c9e9f97bSIlya Dryomov 34509ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 3451c9e9f97bSIlya Dryomov { 34529ba1f6e4SLiu Bo struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3453c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 3454c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 3455c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 3456ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 3457c9e9f97bSIlya Dryomov int ret; 3458c9e9f97bSIlya Dryomov 3459c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3460c9e9f97bSIlya Dryomov return -EPERM; 3461c9e9f97bSIlya Dryomov 3462e54bfa31SLiu Bo ret = mnt_want_write_file(file); 34639ba1f6e4SLiu Bo if (ret) 34649ba1f6e4SLiu Bo return ret; 34659ba1f6e4SLiu Bo 3466ed0fb78fSIlya Dryomov again: 3467ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 3468c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 3469c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3470ed0fb78fSIlya Dryomov need_unlock = true; 3471ed0fb78fSIlya Dryomov goto locked; 3472ed0fb78fSIlya Dryomov } 3473ed0fb78fSIlya Dryomov 3474ed0fb78fSIlya Dryomov /* 3475ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 3476ed0fb78fSIlya Dryomov * (1) some other op is running 3477ed0fb78fSIlya Dryomov * (2) balance is running 3478ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 3479ed0fb78fSIlya Dryomov */ 3480ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3481ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3482ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 3483ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 3484ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3485ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 3486ed0fb78fSIlya Dryomov goto again; 3487ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3488ed0fb78fSIlya Dryomov 3489ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 3490ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 3491ed0fb78fSIlya Dryomov /* this is (3) */ 3492ed0fb78fSIlya Dryomov need_unlock = false; 3493ed0fb78fSIlya Dryomov goto locked; 3494ed0fb78fSIlya Dryomov } 3495ed0fb78fSIlya Dryomov 3496ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3497ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3498ed0fb78fSIlya Dryomov goto again; 3499ed0fb78fSIlya Dryomov } else { 3500ed0fb78fSIlya Dryomov /* this is (2) */ 3501ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3502ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 3503ed0fb78fSIlya Dryomov goto out; 3504ed0fb78fSIlya Dryomov } 3505ed0fb78fSIlya Dryomov } else { 3506ed0fb78fSIlya Dryomov /* this is (1) */ 3507ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3508ed0fb78fSIlya Dryomov pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); 3509ed0fb78fSIlya Dryomov ret = -EINVAL; 3510ed0fb78fSIlya Dryomov goto out; 3511ed0fb78fSIlya Dryomov } 3512ed0fb78fSIlya Dryomov 3513ed0fb78fSIlya Dryomov locked: 3514ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 3515c9e9f97bSIlya Dryomov 3516c9e9f97bSIlya Dryomov if (arg) { 3517c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 3518c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 3519c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 3520ed0fb78fSIlya Dryomov goto out_unlock; 3521c9e9f97bSIlya Dryomov } 3522de322263SIlya Dryomov 3523de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 3524de322263SIlya Dryomov if (!fs_info->balance_ctl) { 3525de322263SIlya Dryomov ret = -ENOTCONN; 3526de322263SIlya Dryomov goto out_bargs; 3527de322263SIlya Dryomov } 3528de322263SIlya Dryomov 3529de322263SIlya Dryomov bctl = fs_info->balance_ctl; 3530de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 3531de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 3532de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 3533de322263SIlya Dryomov 3534de322263SIlya Dryomov goto do_balance; 3535de322263SIlya Dryomov } 3536c9e9f97bSIlya Dryomov } else { 3537c9e9f97bSIlya Dryomov bargs = NULL; 3538c9e9f97bSIlya Dryomov } 3539c9e9f97bSIlya Dryomov 3540ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3541837d5b6eSIlya Dryomov ret = -EINPROGRESS; 3542837d5b6eSIlya Dryomov goto out_bargs; 3543837d5b6eSIlya Dryomov } 3544837d5b6eSIlya Dryomov 3545c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3546c9e9f97bSIlya Dryomov if (!bctl) { 3547c9e9f97bSIlya Dryomov ret = -ENOMEM; 3548c9e9f97bSIlya Dryomov goto out_bargs; 3549c9e9f97bSIlya Dryomov } 3550c9e9f97bSIlya Dryomov 3551c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 3552c9e9f97bSIlya Dryomov if (arg) { 3553c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 3554c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 3555c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 3556c9e9f97bSIlya Dryomov 3557c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 3558f43ffb60SIlya Dryomov } else { 3559f43ffb60SIlya Dryomov /* balance everything - no filters */ 3560f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 3561c9e9f97bSIlya Dryomov } 3562c9e9f97bSIlya Dryomov 3563de322263SIlya Dryomov do_balance: 3564c9e9f97bSIlya Dryomov /* 3565ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 3566ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 3567ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 3568ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 3569ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 3570c9e9f97bSIlya Dryomov */ 3571ed0fb78fSIlya Dryomov need_unlock = false; 3572ed0fb78fSIlya Dryomov 3573ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 3574ed0fb78fSIlya Dryomov 3575c9e9f97bSIlya Dryomov if (arg) { 3576c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 3577c9e9f97bSIlya Dryomov ret = -EFAULT; 3578c9e9f97bSIlya Dryomov } 3579c9e9f97bSIlya Dryomov 3580c9e9f97bSIlya Dryomov out_bargs: 3581c9e9f97bSIlya Dryomov kfree(bargs); 3582ed0fb78fSIlya Dryomov out_unlock: 3583c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3584c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3585ed0fb78fSIlya Dryomov if (need_unlock) 3586ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3587ed0fb78fSIlya Dryomov out: 3588e54bfa31SLiu Bo mnt_drop_write_file(file); 3589c9e9f97bSIlya Dryomov return ret; 3590c9e9f97bSIlya Dryomov } 3591c9e9f97bSIlya Dryomov 3592837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 3593837d5b6eSIlya Dryomov { 3594837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3595837d5b6eSIlya Dryomov return -EPERM; 3596837d5b6eSIlya Dryomov 3597837d5b6eSIlya Dryomov switch (cmd) { 3598837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 3599837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 3600a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 3601a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 3602837d5b6eSIlya Dryomov } 3603837d5b6eSIlya Dryomov 3604837d5b6eSIlya Dryomov return -EINVAL; 3605837d5b6eSIlya Dryomov } 3606837d5b6eSIlya Dryomov 360719a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 360819a39dceSIlya Dryomov void __user *arg) 360919a39dceSIlya Dryomov { 361019a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 361119a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 361219a39dceSIlya Dryomov int ret = 0; 361319a39dceSIlya Dryomov 361419a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 361519a39dceSIlya Dryomov return -EPERM; 361619a39dceSIlya Dryomov 361719a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 361819a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 361919a39dceSIlya Dryomov ret = -ENOTCONN; 362019a39dceSIlya Dryomov goto out; 362119a39dceSIlya Dryomov } 362219a39dceSIlya Dryomov 362319a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 362419a39dceSIlya Dryomov if (!bargs) { 362519a39dceSIlya Dryomov ret = -ENOMEM; 362619a39dceSIlya Dryomov goto out; 362719a39dceSIlya Dryomov } 362819a39dceSIlya Dryomov 362919a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 363019a39dceSIlya Dryomov 363119a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 363219a39dceSIlya Dryomov ret = -EFAULT; 363319a39dceSIlya Dryomov 363419a39dceSIlya Dryomov kfree(bargs); 363519a39dceSIlya Dryomov out: 363619a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 363719a39dceSIlya Dryomov return ret; 363819a39dceSIlya Dryomov } 363919a39dceSIlya Dryomov 3640905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 36415d13a37bSArne Jansen { 3642905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 36435d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 36445d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 36455d13a37bSArne Jansen int ret; 36465d13a37bSArne Jansen int err; 36475d13a37bSArne Jansen 36485d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 36495d13a37bSArne Jansen return -EPERM; 36505d13a37bSArne Jansen 3651905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3652905b0ddaSMiao Xie if (ret) 3653905b0ddaSMiao Xie return ret; 36545d13a37bSArne Jansen 36555d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3656905b0ddaSMiao Xie if (IS_ERR(sa)) { 3657905b0ddaSMiao Xie ret = PTR_ERR(sa); 3658905b0ddaSMiao Xie goto drop_write; 3659905b0ddaSMiao Xie } 36605d13a37bSArne Jansen 36615d13a37bSArne Jansen if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) { 36625d13a37bSArne Jansen trans = btrfs_start_transaction(root, 2); 36635d13a37bSArne Jansen if (IS_ERR(trans)) { 36645d13a37bSArne Jansen ret = PTR_ERR(trans); 36655d13a37bSArne Jansen goto out; 36665d13a37bSArne Jansen } 36675d13a37bSArne Jansen } 36685d13a37bSArne Jansen 36695d13a37bSArne Jansen switch (sa->cmd) { 36705d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 36715d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 36725d13a37bSArne Jansen break; 36735d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 36745d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 36755d13a37bSArne Jansen break; 36765d13a37bSArne Jansen case BTRFS_QUOTA_CTL_RESCAN: 36775d13a37bSArne Jansen ret = btrfs_quota_rescan(root->fs_info); 36785d13a37bSArne Jansen break; 36795d13a37bSArne Jansen default: 36805d13a37bSArne Jansen ret = -EINVAL; 36815d13a37bSArne Jansen break; 36825d13a37bSArne Jansen } 36835d13a37bSArne Jansen 36845d13a37bSArne Jansen if (copy_to_user(arg, sa, sizeof(*sa))) 36855d13a37bSArne Jansen ret = -EFAULT; 36865d13a37bSArne Jansen 36875d13a37bSArne Jansen if (trans) { 36885d13a37bSArne Jansen err = btrfs_commit_transaction(trans, root); 36895d13a37bSArne Jansen if (err && !ret) 36905d13a37bSArne Jansen ret = err; 36915d13a37bSArne Jansen } 36925d13a37bSArne Jansen out: 36935d13a37bSArne Jansen kfree(sa); 3694905b0ddaSMiao Xie drop_write: 3695905b0ddaSMiao Xie mnt_drop_write_file(file); 36965d13a37bSArne Jansen return ret; 36975d13a37bSArne Jansen } 36985d13a37bSArne Jansen 3699905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 37005d13a37bSArne Jansen { 3701905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 37025d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 37035d13a37bSArne Jansen struct btrfs_trans_handle *trans; 37045d13a37bSArne Jansen int ret; 37055d13a37bSArne Jansen int err; 37065d13a37bSArne Jansen 37075d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 37085d13a37bSArne Jansen return -EPERM; 37095d13a37bSArne Jansen 3710905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3711905b0ddaSMiao Xie if (ret) 3712905b0ddaSMiao Xie return ret; 37135d13a37bSArne Jansen 37145d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3715905b0ddaSMiao Xie if (IS_ERR(sa)) { 3716905b0ddaSMiao Xie ret = PTR_ERR(sa); 3717905b0ddaSMiao Xie goto drop_write; 3718905b0ddaSMiao Xie } 37195d13a37bSArne Jansen 37205d13a37bSArne Jansen trans = btrfs_join_transaction(root); 37215d13a37bSArne Jansen if (IS_ERR(trans)) { 37225d13a37bSArne Jansen ret = PTR_ERR(trans); 37235d13a37bSArne Jansen goto out; 37245d13a37bSArne Jansen } 37255d13a37bSArne Jansen 37265d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 37275d13a37bSArne Jansen if (sa->assign) { 37285d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 37295d13a37bSArne Jansen sa->src, sa->dst); 37305d13a37bSArne Jansen } else { 37315d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 37325d13a37bSArne Jansen sa->src, sa->dst); 37335d13a37bSArne Jansen } 37345d13a37bSArne Jansen 37355d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 37365d13a37bSArne Jansen if (err && !ret) 37375d13a37bSArne Jansen ret = err; 37385d13a37bSArne Jansen 37395d13a37bSArne Jansen out: 37405d13a37bSArne Jansen kfree(sa); 3741905b0ddaSMiao Xie drop_write: 3742905b0ddaSMiao Xie mnt_drop_write_file(file); 37435d13a37bSArne Jansen return ret; 37445d13a37bSArne Jansen } 37455d13a37bSArne Jansen 3746905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 37475d13a37bSArne Jansen { 3748905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 37495d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 37505d13a37bSArne Jansen struct btrfs_trans_handle *trans; 37515d13a37bSArne Jansen int ret; 37525d13a37bSArne Jansen int err; 37535d13a37bSArne Jansen 37545d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 37555d13a37bSArne Jansen return -EPERM; 37565d13a37bSArne Jansen 3757905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3758905b0ddaSMiao Xie if (ret) 3759905b0ddaSMiao Xie return ret; 37605d13a37bSArne Jansen 37615d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3762905b0ddaSMiao Xie if (IS_ERR(sa)) { 3763905b0ddaSMiao Xie ret = PTR_ERR(sa); 3764905b0ddaSMiao Xie goto drop_write; 3765905b0ddaSMiao Xie } 37665d13a37bSArne Jansen 3767d86e56cfSMiao Xie if (!sa->qgroupid) { 3768d86e56cfSMiao Xie ret = -EINVAL; 3769d86e56cfSMiao Xie goto out; 3770d86e56cfSMiao Xie } 3771d86e56cfSMiao Xie 37725d13a37bSArne Jansen trans = btrfs_join_transaction(root); 37735d13a37bSArne Jansen if (IS_ERR(trans)) { 37745d13a37bSArne Jansen ret = PTR_ERR(trans); 37755d13a37bSArne Jansen goto out; 37765d13a37bSArne Jansen } 37775d13a37bSArne Jansen 37785d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 37795d13a37bSArne Jansen if (sa->create) { 37805d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 37815d13a37bSArne Jansen NULL); 37825d13a37bSArne Jansen } else { 37835d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 37845d13a37bSArne Jansen } 37855d13a37bSArne Jansen 37865d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 37875d13a37bSArne Jansen if (err && !ret) 37885d13a37bSArne Jansen ret = err; 37895d13a37bSArne Jansen 37905d13a37bSArne Jansen out: 37915d13a37bSArne Jansen kfree(sa); 3792905b0ddaSMiao Xie drop_write: 3793905b0ddaSMiao Xie mnt_drop_write_file(file); 37945d13a37bSArne Jansen return ret; 37955d13a37bSArne Jansen } 37965d13a37bSArne Jansen 3797905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 37985d13a37bSArne Jansen { 3799905b0ddaSMiao Xie struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 38005d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 38015d13a37bSArne Jansen struct btrfs_trans_handle *trans; 38025d13a37bSArne Jansen int ret; 38035d13a37bSArne Jansen int err; 38045d13a37bSArne Jansen u64 qgroupid; 38055d13a37bSArne Jansen 38065d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 38075d13a37bSArne Jansen return -EPERM; 38085d13a37bSArne Jansen 3809905b0ddaSMiao Xie ret = mnt_want_write_file(file); 3810905b0ddaSMiao Xie if (ret) 3811905b0ddaSMiao Xie return ret; 38125d13a37bSArne Jansen 38135d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 3814905b0ddaSMiao Xie if (IS_ERR(sa)) { 3815905b0ddaSMiao Xie ret = PTR_ERR(sa); 3816905b0ddaSMiao Xie goto drop_write; 3817905b0ddaSMiao Xie } 38185d13a37bSArne Jansen 38195d13a37bSArne Jansen trans = btrfs_join_transaction(root); 38205d13a37bSArne Jansen if (IS_ERR(trans)) { 38215d13a37bSArne Jansen ret = PTR_ERR(trans); 38225d13a37bSArne Jansen goto out; 38235d13a37bSArne Jansen } 38245d13a37bSArne Jansen 38255d13a37bSArne Jansen qgroupid = sa->qgroupid; 38265d13a37bSArne Jansen if (!qgroupid) { 38275d13a37bSArne Jansen /* take the current subvol as qgroup */ 38285d13a37bSArne Jansen qgroupid = root->root_key.objectid; 38295d13a37bSArne Jansen } 38305d13a37bSArne Jansen 38315d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 38325d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 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 38458ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file, 38468ea05e3aSAlexander Block void __user *arg) 38478ea05e3aSAlexander Block { 38488ea05e3aSAlexander Block struct btrfs_ioctl_received_subvol_args *sa = NULL; 38498ea05e3aSAlexander Block struct inode *inode = fdentry(file)->d_inode; 38508ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 38518ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 38528ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 38538ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 38548ea05e3aSAlexander Block int ret = 0; 38558ea05e3aSAlexander Block 38568ea05e3aSAlexander Block ret = mnt_want_write_file(file); 38578ea05e3aSAlexander Block if (ret < 0) 38588ea05e3aSAlexander Block return ret; 38598ea05e3aSAlexander Block 38608ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 38618ea05e3aSAlexander Block 38628ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 38638ea05e3aSAlexander Block ret = -EINVAL; 38648ea05e3aSAlexander Block goto out; 38658ea05e3aSAlexander Block } 38668ea05e3aSAlexander Block 38678ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 38688ea05e3aSAlexander Block ret = -EROFS; 38698ea05e3aSAlexander Block goto out; 38708ea05e3aSAlexander Block } 38718ea05e3aSAlexander Block 38728ea05e3aSAlexander Block if (!inode_owner_or_capable(inode)) { 38738ea05e3aSAlexander Block ret = -EACCES; 38748ea05e3aSAlexander Block goto out; 38758ea05e3aSAlexander Block } 38768ea05e3aSAlexander Block 38778ea05e3aSAlexander Block sa = memdup_user(arg, sizeof(*sa)); 38788ea05e3aSAlexander Block if (IS_ERR(sa)) { 38798ea05e3aSAlexander Block ret = PTR_ERR(sa); 38808ea05e3aSAlexander Block sa = NULL; 38818ea05e3aSAlexander Block goto out; 38828ea05e3aSAlexander Block } 38838ea05e3aSAlexander Block 38848ea05e3aSAlexander Block trans = btrfs_start_transaction(root, 1); 38858ea05e3aSAlexander Block if (IS_ERR(trans)) { 38868ea05e3aSAlexander Block ret = PTR_ERR(trans); 38878ea05e3aSAlexander Block trans = NULL; 38888ea05e3aSAlexander Block goto out; 38898ea05e3aSAlexander Block } 38908ea05e3aSAlexander Block 38918ea05e3aSAlexander Block sa->rtransid = trans->transid; 38928ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 38938ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 38948ea05e3aSAlexander Block 38958ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 38968ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 38978ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 38988ea05e3aSAlexander Block root_item->stime.sec = cpu_to_le64(sa->stime.sec); 38998ea05e3aSAlexander Block root_item->stime.nsec = cpu_to_le32(sa->stime.nsec); 39008ea05e3aSAlexander Block root_item->rtime.sec = cpu_to_le64(sa->rtime.sec); 39018ea05e3aSAlexander Block root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec); 39028ea05e3aSAlexander Block 39038ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 39048ea05e3aSAlexander Block &root->root_key, &root->root_item); 39058ea05e3aSAlexander Block if (ret < 0) { 39068ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 39078ea05e3aSAlexander Block trans = NULL; 39088ea05e3aSAlexander Block goto out; 39098ea05e3aSAlexander Block } else { 39108ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 39118ea05e3aSAlexander Block if (ret < 0) 39128ea05e3aSAlexander Block goto out; 39138ea05e3aSAlexander Block } 39148ea05e3aSAlexander Block 39158ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 39168ea05e3aSAlexander Block if (ret) 39178ea05e3aSAlexander Block ret = -EFAULT; 39188ea05e3aSAlexander Block 39198ea05e3aSAlexander Block out: 39208ea05e3aSAlexander Block kfree(sa); 39218ea05e3aSAlexander Block up_write(&root->fs_info->subvol_sem); 39228ea05e3aSAlexander Block mnt_drop_write_file(file); 39238ea05e3aSAlexander Block return ret; 39248ea05e3aSAlexander Block } 39258ea05e3aSAlexander Block 3926867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 3927867ab667Sjeff.liu { 3928867ab667Sjeff.liu struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3929867ab667Sjeff.liu const char *label = root->fs_info->super_copy->label; 3930867ab667Sjeff.liu size_t len = strnlen(label, BTRFS_LABEL_SIZE); 3931867ab667Sjeff.liu int ret; 3932867ab667Sjeff.liu 3933867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 3934867ab667Sjeff.liu pr_warn("btrfs: label is too long, return the first %zu bytes\n", 3935867ab667Sjeff.liu --len); 3936867ab667Sjeff.liu } 3937867ab667Sjeff.liu 3938867ab667Sjeff.liu mutex_lock(&root->fs_info->volume_mutex); 3939867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 3940867ab667Sjeff.liu mutex_unlock(&root->fs_info->volume_mutex); 3941867ab667Sjeff.liu 3942867ab667Sjeff.liu return ret ? -EFAULT : 0; 3943867ab667Sjeff.liu } 3944867ab667Sjeff.liu 3945a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 3946a8bfd4abSjeff.liu { 3947a8bfd4abSjeff.liu struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 3948a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 3949a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 3950a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 3951a8bfd4abSjeff.liu int ret; 3952a8bfd4abSjeff.liu 3953a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 3954a8bfd4abSjeff.liu return -EPERM; 3955a8bfd4abSjeff.liu 3956a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 3957a8bfd4abSjeff.liu return -EFAULT; 3958a8bfd4abSjeff.liu 3959a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 3960a8bfd4abSjeff.liu pr_err("btrfs: unable to set label with more than %d bytes\n", 3961a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 3962a8bfd4abSjeff.liu return -EINVAL; 3963a8bfd4abSjeff.liu } 3964a8bfd4abSjeff.liu 3965a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 3966a8bfd4abSjeff.liu if (ret) 3967a8bfd4abSjeff.liu return ret; 3968a8bfd4abSjeff.liu 3969a8bfd4abSjeff.liu mutex_lock(&root->fs_info->volume_mutex); 3970a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 3971a8bfd4abSjeff.liu if (IS_ERR(trans)) { 3972a8bfd4abSjeff.liu ret = PTR_ERR(trans); 3973a8bfd4abSjeff.liu goto out_unlock; 3974a8bfd4abSjeff.liu } 3975a8bfd4abSjeff.liu 3976a8bfd4abSjeff.liu strcpy(super_block->label, label); 3977a8bfd4abSjeff.liu ret = btrfs_end_transaction(trans, root); 3978a8bfd4abSjeff.liu 3979a8bfd4abSjeff.liu out_unlock: 3980a8bfd4abSjeff.liu mutex_unlock(&root->fs_info->volume_mutex); 3981a8bfd4abSjeff.liu mnt_drop_write_file(file); 3982a8bfd4abSjeff.liu return ret; 3983a8bfd4abSjeff.liu } 3984a8bfd4abSjeff.liu 3985f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 3986f46b5a66SChristoph Hellwig cmd, unsigned long arg) 3987f46b5a66SChristoph Hellwig { 3988f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 39894bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 3990f46b5a66SChristoph Hellwig 3991f46b5a66SChristoph Hellwig switch (cmd) { 39926cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 39936cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 39946cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 39956cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 39966cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 39976cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 3998f7039b1dSLi Dongyang case FITRIM: 3999f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 4000f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 4001fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 4002fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 4003fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 40043de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 4005fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 40066f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 40076f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 400876dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 400976dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 40100caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 40110caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 40120caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 40130caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 40146ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 40156ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 4016f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 40171e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 40181e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 40191e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 4020f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 4021198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 4022f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 40234bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 4024f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 4025da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 4026475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 4027475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 4028475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 4029475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 4030f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 40319ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 4032f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 4033c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 4034c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 40357a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 4036f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 4037f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 4038f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 4039f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 4040ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 4041ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 4042ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 4043ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 4044d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 4045d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 4046d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 4047d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 40481406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 40491406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 4050f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 4051f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 4052f46b5a66SChristoph Hellwig return 0; 405346204592SSage Weil case BTRFS_IOC_START_SYNC: 40549a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 405546204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 40569a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 4057475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 4058b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 4059475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 4060475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 4061475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 4062475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 4063c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 40649ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 4065837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 4066837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 406719a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 406819a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 40698ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 40708ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 407131db9f7cSAlexander Block case BTRFS_IOC_SEND: 407231db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 4073c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 4074b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 40755d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 4076905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 40775d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 4078905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 40795d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 4080905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 40815d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 4082905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 40833f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 40843f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 4085867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 4086867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 4087a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 4088a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 4089f46b5a66SChristoph Hellwig } 4090f46b5a66SChristoph Hellwig 4091f46b5a66SChristoph Hellwig return -ENOTTY; 4092f46b5a66SChristoph Hellwig } 4093