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> 444b4e25f2SChris Mason #include "compat.h" 45f46b5a66SChristoph Hellwig #include "ctree.h" 46f46b5a66SChristoph Hellwig #include "disk-io.h" 47f46b5a66SChristoph Hellwig #include "transaction.h" 48f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 49f46b5a66SChristoph Hellwig #include "ioctl.h" 50f46b5a66SChristoph Hellwig #include "print-tree.h" 51f46b5a66SChristoph Hellwig #include "volumes.h" 52925baeddSChris Mason #include "locking.h" 53581bb050SLi Zefan #include "inode-map.h" 54d7728c96SJan Schmidt #include "backref.h" 55f46b5a66SChristoph Hellwig 566cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 576cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 586cbff00fSChristoph Hellwig { 596cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 606cbff00fSChristoph Hellwig return flags; 616cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 626cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 636cbff00fSChristoph Hellwig else 646cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 656cbff00fSChristoph Hellwig } 66f46b5a66SChristoph Hellwig 676cbff00fSChristoph Hellwig /* 686cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 696cbff00fSChristoph Hellwig */ 706cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 716cbff00fSChristoph Hellwig { 726cbff00fSChristoph Hellwig unsigned int iflags = 0; 736cbff00fSChristoph Hellwig 746cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 756cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 766cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 776cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 786cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 796cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 806cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 816cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 826cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 836cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 846cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 856cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 86d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 87d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 88d0092bddSLi Zefan 89d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 90d0092bddSLi Zefan iflags |= FS_COMPR_FL; 91d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 92d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 936cbff00fSChristoph Hellwig 946cbff00fSChristoph Hellwig return iflags; 956cbff00fSChristoph Hellwig } 966cbff00fSChristoph Hellwig 976cbff00fSChristoph Hellwig /* 986cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 996cbff00fSChristoph Hellwig */ 1006cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1016cbff00fSChristoph Hellwig { 1026cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1036cbff00fSChristoph Hellwig 1046cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 1056cbff00fSChristoph Hellwig 1066cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1076cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 1086cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1096cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1106cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1116cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1126cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1136cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1146cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1156cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1166cbff00fSChristoph Hellwig } 1176cbff00fSChristoph Hellwig 1186cbff00fSChristoph Hellwig /* 1196cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1206cbff00fSChristoph Hellwig * 121e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1226cbff00fSChristoph Hellwig */ 1236cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1246cbff00fSChristoph Hellwig { 1250b4dcea5SChris Mason unsigned int flags; 1260b4dcea5SChris Mason 1270b4dcea5SChris Mason if (!dir) 1280b4dcea5SChris Mason return; 1290b4dcea5SChris Mason 1300b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1316cbff00fSChristoph Hellwig 132e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 133e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 134e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 135e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 136e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 137e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 138e27425d6SJosef Bacik } 1396cbff00fSChristoph Hellwig 140e27425d6SJosef Bacik if (flags & BTRFS_INODE_NODATACOW) 141e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 142e27425d6SJosef Bacik 1436cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1446cbff00fSChristoph Hellwig } 1456cbff00fSChristoph Hellwig 1466cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1476cbff00fSChristoph Hellwig { 1486cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); 1496cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1506cbff00fSChristoph Hellwig 1516cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1526cbff00fSChristoph Hellwig return -EFAULT; 1536cbff00fSChristoph Hellwig return 0; 1546cbff00fSChristoph Hellwig } 1556cbff00fSChristoph Hellwig 15675e7cb7fSLiu Bo static int check_flags(unsigned int flags) 15775e7cb7fSLiu Bo { 15875e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 15975e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 16075e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 161e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 162e1e8fb6aSLi Zefan FS_NOCOW_FL)) 16375e7cb7fSLiu Bo return -EOPNOTSUPP; 16475e7cb7fSLiu Bo 16575e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 16675e7cb7fSLiu Bo return -EINVAL; 16775e7cb7fSLiu Bo 16875e7cb7fSLiu Bo return 0; 16975e7cb7fSLiu Bo } 17075e7cb7fSLiu Bo 1716cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1726cbff00fSChristoph Hellwig { 1736cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 1746cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1756cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1766cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1776cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1786cbff00fSChristoph Hellwig int ret; 1796cbff00fSChristoph Hellwig 180b83cc969SLi Zefan if (btrfs_root_readonly(root)) 181b83cc969SLi Zefan return -EROFS; 182b83cc969SLi Zefan 1836cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1846cbff00fSChristoph Hellwig return -EFAULT; 1856cbff00fSChristoph Hellwig 18675e7cb7fSLiu Bo ret = check_flags(flags); 18775e7cb7fSLiu Bo if (ret) 18875e7cb7fSLiu Bo return ret; 1896cbff00fSChristoph Hellwig 1902e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1916cbff00fSChristoph Hellwig return -EACCES; 1926cbff00fSChristoph Hellwig 1936cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 1946cbff00fSChristoph Hellwig 1956cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 1966cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 1976cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 1986cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 1996cbff00fSChristoph Hellwig ret = -EPERM; 2006cbff00fSChristoph Hellwig goto out_unlock; 2016cbff00fSChristoph Hellwig } 2026cbff00fSChristoph Hellwig } 2036cbff00fSChristoph Hellwig 2046cbff00fSChristoph Hellwig ret = mnt_want_write(file->f_path.mnt); 2056cbff00fSChristoph Hellwig if (ret) 2066cbff00fSChristoph Hellwig goto out_unlock; 2076cbff00fSChristoph Hellwig 2086cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2096cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2106cbff00fSChristoph Hellwig else 2116cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2126cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2136cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2146cbff00fSChristoph Hellwig else 2156cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2166cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2176cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2186cbff00fSChristoph Hellwig else 2196cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2206cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2216cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2226cbff00fSChristoph Hellwig else 2236cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2246cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2256cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2266cbff00fSChristoph Hellwig else 2276cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2286cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2296cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2306cbff00fSChristoph Hellwig else 2316cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 232e1e8fb6aSLi Zefan if (flags & FS_NOCOW_FL) 233e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 234e1e8fb6aSLi Zefan else 235e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2366cbff00fSChristoph Hellwig 23775e7cb7fSLiu Bo /* 23875e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 23975e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 24075e7cb7fSLiu Bo * things smaller. 24175e7cb7fSLiu Bo */ 24275e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 24375e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 24475e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 24575e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 24675e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 24775e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 248ebcb904dSLi Zefan } else { 249ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 25075e7cb7fSLiu Bo } 2516cbff00fSChristoph Hellwig 2527a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 2533612b495STsutomu Itoh BUG_ON(IS_ERR(trans)); 2546cbff00fSChristoph Hellwig 255306424ccSLi Zefan btrfs_update_iflags(inode); 256306424ccSLi Zefan inode->i_ctime = CURRENT_TIME; 2576cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 2586cbff00fSChristoph Hellwig BUG_ON(ret); 2596cbff00fSChristoph Hellwig 2606cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 2616cbff00fSChristoph Hellwig 2626cbff00fSChristoph Hellwig mnt_drop_write(file->f_path.mnt); 2632d4e6f6aSliubo 2642d4e6f6aSliubo ret = 0; 2656cbff00fSChristoph Hellwig out_unlock: 2666cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 2672d4e6f6aSliubo return ret; 2686cbff00fSChristoph Hellwig } 2696cbff00fSChristoph Hellwig 2706cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 2716cbff00fSChristoph Hellwig { 2726cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 2736cbff00fSChristoph Hellwig 2746cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 2756cbff00fSChristoph Hellwig } 276f46b5a66SChristoph Hellwig 277f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 278f7039b1dSLi Dongyang { 279f7039b1dSLi Dongyang struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info; 280f7039b1dSLi Dongyang struct btrfs_fs_info *fs_info = root->fs_info; 281f7039b1dSLi Dongyang struct btrfs_device *device; 282f7039b1dSLi Dongyang struct request_queue *q; 283f7039b1dSLi Dongyang struct fstrim_range range; 284f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 285f7039b1dSLi Dongyang u64 num_devices = 0; 2866c41761fSDavid Sterba u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); 287f7039b1dSLi Dongyang int ret; 288f7039b1dSLi Dongyang 289f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 290f7039b1dSLi Dongyang return -EPERM; 291f7039b1dSLi Dongyang 2921f78160cSXiao Guangrong rcu_read_lock(); 2931f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 2941f78160cSXiao Guangrong dev_list) { 295f7039b1dSLi Dongyang if (!device->bdev) 296f7039b1dSLi Dongyang continue; 297f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 298f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 299f7039b1dSLi Dongyang num_devices++; 300f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 301f7039b1dSLi Dongyang minlen); 302f7039b1dSLi Dongyang } 303f7039b1dSLi Dongyang } 3041f78160cSXiao Guangrong rcu_read_unlock(); 305f4c697e6SLukas Czerner 306f7039b1dSLi Dongyang if (!num_devices) 307f7039b1dSLi Dongyang return -EOPNOTSUPP; 308f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 309f7039b1dSLi Dongyang return -EFAULT; 310f4c697e6SLukas Czerner if (range.start > total_bytes) 311f4c697e6SLukas Czerner return -EINVAL; 312f7039b1dSLi Dongyang 313f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 314f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 315f7039b1dSLi Dongyang ret = btrfs_trim_fs(root, &range); 316f7039b1dSLi Dongyang if (ret < 0) 317f7039b1dSLi Dongyang return ret; 318f7039b1dSLi Dongyang 319f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 320f7039b1dSLi Dongyang return -EFAULT; 321f7039b1dSLi Dongyang 322f7039b1dSLi Dongyang return 0; 323f7039b1dSLi Dongyang } 324f7039b1dSLi Dongyang 325cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root, 326cb8e7090SChristoph Hellwig struct dentry *dentry, 32772fd032eSSage Weil char *name, int namelen, 32872fd032eSSage Weil u64 *async_transid) 329f46b5a66SChristoph Hellwig { 330f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 331f46b5a66SChristoph Hellwig struct btrfs_key key; 332f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 333f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 334f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 33576dda93cSYan, Zheng struct btrfs_root *new_root; 3362fbe8c8aSAl Viro struct dentry *parent = dentry->d_parent; 3376a912213SJosef Bacik struct inode *dir; 338f46b5a66SChristoph Hellwig int ret; 339f46b5a66SChristoph Hellwig int err; 340f46b5a66SChristoph Hellwig u64 objectid; 341f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 3423de4586cSChris Mason u64 index = 0; 343f46b5a66SChristoph Hellwig 344581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 3452fbe8c8aSAl Viro if (ret) 346a22285a6SYan, Zheng return ret; 3476a912213SJosef Bacik 3486a912213SJosef Bacik dir = parent->d_inode; 3496a912213SJosef Bacik 3509ed74f2dSJosef Bacik /* 3519ed74f2dSJosef Bacik * 1 - inode item 3529ed74f2dSJosef Bacik * 2 - refs 3539ed74f2dSJosef Bacik * 1 - root item 3549ed74f2dSJosef Bacik * 2 - dir items 3559ed74f2dSJosef Bacik */ 356a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 3572fbe8c8aSAl Viro if (IS_ERR(trans)) 358a22285a6SYan, Zheng return PTR_ERR(trans); 359f46b5a66SChristoph Hellwig 3605d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 3615d4f98a2SYan Zheng 0, objectid, NULL, 0, 0, 0); 3628e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 3638e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 3648e8a1e31SJosef Bacik goto fail; 3658e8a1e31SJosef Bacik } 366f46b5a66SChristoph Hellwig 3675d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 368f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 369f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 3705d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 371f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 372f46b5a66SChristoph Hellwig 373f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 374f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 375f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 3765d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 3775d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 3785d4f98a2SYan Zheng BTRFS_UUID_SIZE); 379f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 380f46b5a66SChristoph Hellwig 381f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 382f46b5a66SChristoph Hellwig memset(inode_item, 0, sizeof(*inode_item)); 383f46b5a66SChristoph Hellwig inode_item->generation = cpu_to_le64(1); 384f46b5a66SChristoph Hellwig inode_item->size = cpu_to_le64(3); 385f46b5a66SChristoph Hellwig inode_item->nlink = cpu_to_le32(1); 386a76a3cd4SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 387f46b5a66SChristoph Hellwig inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 388f46b5a66SChristoph Hellwig 38908fe4db1SLi Zefan root_item.flags = 0; 39008fe4db1SLi Zefan root_item.byte_limit = 0; 39108fe4db1SLi Zefan inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); 39208fe4db1SLi Zefan 393f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 39484234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 395f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 396f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 39786b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 39880ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 399f46b5a66SChristoph Hellwig 400f46b5a66SChristoph Hellwig memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 401f46b5a66SChristoph Hellwig root_item.drop_level = 0; 402f46b5a66SChristoph Hellwig 403925baeddSChris Mason btrfs_tree_unlock(leaf); 404f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 405f46b5a66SChristoph Hellwig leaf = NULL; 406f46b5a66SChristoph Hellwig 407f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 408f46b5a66SChristoph Hellwig 409f46b5a66SChristoph Hellwig key.objectid = objectid; 4105d4f98a2SYan Zheng key.offset = 0; 411f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 412f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 413f46b5a66SChristoph Hellwig &root_item); 414f46b5a66SChristoph Hellwig if (ret) 415f46b5a66SChristoph Hellwig goto fail; 416f46b5a66SChristoph Hellwig 41776dda93cSYan, Zheng key.offset = (u64)-1; 41876dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 41976dda93cSYan, Zheng BUG_ON(IS_ERR(new_root)); 42076dda93cSYan, Zheng 42176dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 42276dda93cSYan, Zheng 423d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 424f46b5a66SChristoph Hellwig /* 425f46b5a66SChristoph Hellwig * insert the directory item 426f46b5a66SChristoph Hellwig */ 4273de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 4283de4586cSChris Mason BUG_ON(ret); 4293de4586cSChris Mason 4303de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 43116cdcec7SMiao Xie name, namelen, dir, &key, 4323de4586cSChris Mason BTRFS_FT_DIR, index); 433f46b5a66SChristoph Hellwig if (ret) 434f46b5a66SChristoph Hellwig goto fail; 4350660b5afSChris Mason 43652c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 43752c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 43852c26179SYan Zheng BUG_ON(ret); 43952c26179SYan Zheng 4400660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4414df27c4dSYan, Zheng objectid, root->root_key.objectid, 44233345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 44376dda93cSYan, Zheng 4440660b5afSChris Mason BUG_ON(ret); 4450660b5afSChris Mason 44676dda93cSYan, Zheng d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 447f46b5a66SChristoph Hellwig fail: 44872fd032eSSage Weil if (async_transid) { 44972fd032eSSage Weil *async_transid = trans->transid; 45072fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 45172fd032eSSage Weil } else { 45276dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 45372fd032eSSage Weil } 454f46b5a66SChristoph Hellwig if (err && !ret) 455f46b5a66SChristoph Hellwig ret = err; 456f46b5a66SChristoph Hellwig return ret; 457f46b5a66SChristoph Hellwig } 458f46b5a66SChristoph Hellwig 45972fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 460b83cc969SLi Zefan char *name, int namelen, u64 *async_transid, 461b83cc969SLi Zefan bool readonly) 462f46b5a66SChristoph Hellwig { 4632e4bfab9SYan, Zheng struct inode *inode; 464f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 465f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 4662e4bfab9SYan, Zheng int ret; 467f46b5a66SChristoph Hellwig 468f46b5a66SChristoph Hellwig if (!root->ref_cows) 469f46b5a66SChristoph Hellwig return -EINVAL; 470f46b5a66SChristoph Hellwig 471a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 472a22285a6SYan, Zheng if (!pending_snapshot) 473a22285a6SYan, Zheng return -ENOMEM; 474a22285a6SYan, Zheng 475a22285a6SYan, Zheng btrfs_init_block_rsv(&pending_snapshot->block_rsv); 476a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 477a22285a6SYan, Zheng pending_snapshot->root = root; 478b83cc969SLi Zefan pending_snapshot->readonly = readonly; 479a22285a6SYan, Zheng 480a22285a6SYan, Zheng trans = btrfs_start_transaction(root->fs_info->extent_root, 5); 481a22285a6SYan, Zheng if (IS_ERR(trans)) { 482a22285a6SYan, Zheng ret = PTR_ERR(trans); 483a22285a6SYan, Zheng goto fail; 484a22285a6SYan, Zheng } 485a22285a6SYan, Zheng 486a22285a6SYan, Zheng ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); 487a22285a6SYan, Zheng BUG_ON(ret); 488a22285a6SYan, Zheng 4898351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 490a22285a6SYan, Zheng list_add(&pending_snapshot->list, 491a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 4928351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 49372fd032eSSage Weil if (async_transid) { 49472fd032eSSage Weil *async_transid = trans->transid; 49572fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 49672fd032eSSage Weil root->fs_info->extent_root, 1); 49772fd032eSSage Weil } else { 49872fd032eSSage Weil ret = btrfs_commit_transaction(trans, 49972fd032eSSage Weil root->fs_info->extent_root); 50072fd032eSSage Weil } 501a22285a6SYan, Zheng BUG_ON(ret); 502a22285a6SYan, Zheng 503a22285a6SYan, Zheng ret = pending_snapshot->error; 504f46b5a66SChristoph Hellwig if (ret) 5052e4bfab9SYan, Zheng goto fail; 506f46b5a66SChristoph Hellwig 50766b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 50866b4ffd1SJosef Bacik if (ret) 50966b4ffd1SJosef Bacik goto fail; 510f46b5a66SChristoph Hellwig 5112fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 5122e4bfab9SYan, Zheng if (IS_ERR(inode)) { 5132e4bfab9SYan, Zheng ret = PTR_ERR(inode); 5142e4bfab9SYan, Zheng goto fail; 5152e4bfab9SYan, Zheng } 5162e4bfab9SYan, Zheng BUG_ON(!inode); 5172e4bfab9SYan, Zheng d_instantiate(dentry, inode); 5182e4bfab9SYan, Zheng ret = 0; 5192e4bfab9SYan, Zheng fail: 520a22285a6SYan, Zheng kfree(pending_snapshot); 521f46b5a66SChristoph Hellwig return ret; 522f46b5a66SChristoph Hellwig } 523f46b5a66SChristoph Hellwig 5244260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 5254260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 5264260f7c7SSage Weil * minimal. 5274260f7c7SSage Weil */ 5284260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 5294260f7c7SSage Weil { 5304260f7c7SSage Weil uid_t fsuid = current_fsuid(); 5314260f7c7SSage Weil 5324260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 5334260f7c7SSage Weil return 0; 5344260f7c7SSage Weil if (inode->i_uid == fsuid) 5354260f7c7SSage Weil return 0; 5364260f7c7SSage Weil if (dir->i_uid == fsuid) 5374260f7c7SSage Weil return 0; 5384260f7c7SSage Weil return !capable(CAP_FOWNER); 5394260f7c7SSage Weil } 5404260f7c7SSage Weil 5414260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 5424260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 5434260f7c7SSage Weil * whether the type of victim is right. 5444260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 5454260f7c7SSage Weil * 2. We should have write and exec permissions on dir 5464260f7c7SSage Weil * 3. We can't remove anything from append-only dir 5474260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 5484260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 5494260f7c7SSage Weil * a. be owner of dir, or 5504260f7c7SSage Weil * b. be owner of victim, or 5514260f7c7SSage Weil * c. have CAP_FOWNER capability 5524260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 5534260f7c7SSage Weil * links pointing to it. 5544260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 5554260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 5564260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 5574260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 5584260f7c7SSage Weil * nfs_async_unlink(). 5594260f7c7SSage Weil */ 5604260f7c7SSage Weil 5614260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 5624260f7c7SSage Weil { 5634260f7c7SSage Weil int error; 5644260f7c7SSage Weil 5654260f7c7SSage Weil if (!victim->d_inode) 5664260f7c7SSage Weil return -ENOENT; 5674260f7c7SSage Weil 5684260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 5694260f7c7SSage Weil audit_inode_child(victim, dir); 5704260f7c7SSage Weil 5714260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 5724260f7c7SSage Weil if (error) 5734260f7c7SSage Weil return error; 5744260f7c7SSage Weil if (IS_APPEND(dir)) 5754260f7c7SSage Weil return -EPERM; 5764260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 5774260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 5784260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 5794260f7c7SSage Weil return -EPERM; 5804260f7c7SSage Weil if (isdir) { 5814260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 5824260f7c7SSage Weil return -ENOTDIR; 5834260f7c7SSage Weil if (IS_ROOT(victim)) 5844260f7c7SSage Weil return -EBUSY; 5854260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 5864260f7c7SSage Weil return -EISDIR; 5874260f7c7SSage Weil if (IS_DEADDIR(dir)) 5884260f7c7SSage Weil return -ENOENT; 5894260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 5904260f7c7SSage Weil return -EBUSY; 5914260f7c7SSage Weil return 0; 5924260f7c7SSage Weil } 5934260f7c7SSage Weil 594cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 595cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 596cb8e7090SChristoph Hellwig { 597cb8e7090SChristoph Hellwig if (child->d_inode) 598cb8e7090SChristoph Hellwig return -EEXIST; 599cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 600cb8e7090SChristoph Hellwig return -ENOENT; 601cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 602cb8e7090SChristoph Hellwig } 603cb8e7090SChristoph Hellwig 604cb8e7090SChristoph Hellwig /* 605cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 606cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 607cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 608cb8e7090SChristoph Hellwig */ 60976dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 61076dda93cSYan, Zheng char *name, int namelen, 61172fd032eSSage Weil struct btrfs_root *snap_src, 612b83cc969SLi Zefan u64 *async_transid, bool readonly) 613cb8e7090SChristoph Hellwig { 61476dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 615cb8e7090SChristoph Hellwig struct dentry *dentry; 616cb8e7090SChristoph Hellwig int error; 617cb8e7090SChristoph Hellwig 61876dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 619cb8e7090SChristoph Hellwig 620cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 621cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 622cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 623cb8e7090SChristoph Hellwig goto out_unlock; 624cb8e7090SChristoph Hellwig 625cb8e7090SChristoph Hellwig error = -EEXIST; 626cb8e7090SChristoph Hellwig if (dentry->d_inode) 627cb8e7090SChristoph Hellwig goto out_dput; 628cb8e7090SChristoph Hellwig 629cb8e7090SChristoph Hellwig error = mnt_want_write(parent->mnt); 630cb8e7090SChristoph Hellwig if (error) 631cb8e7090SChristoph Hellwig goto out_dput; 632cb8e7090SChristoph Hellwig 63376dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 634cb8e7090SChristoph Hellwig if (error) 635cb8e7090SChristoph Hellwig goto out_drop_write; 636cb8e7090SChristoph Hellwig 63776dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 63876dda93cSYan, Zheng 63976dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 64076dda93cSYan, Zheng goto out_up_read; 64176dda93cSYan, Zheng 6423de4586cSChris Mason if (snap_src) { 64372fd032eSSage Weil error = create_snapshot(snap_src, dentry, 644b83cc969SLi Zefan name, namelen, async_transid, readonly); 6453de4586cSChris Mason } else { 64676dda93cSYan, Zheng error = create_subvol(BTRFS_I(dir)->root, dentry, 64772fd032eSSage Weil name, namelen, async_transid); 6483de4586cSChris Mason } 64976dda93cSYan, Zheng if (!error) 65076dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 65176dda93cSYan, Zheng out_up_read: 65276dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 653cb8e7090SChristoph Hellwig out_drop_write: 654cb8e7090SChristoph Hellwig mnt_drop_write(parent->mnt); 655cb8e7090SChristoph Hellwig out_dput: 656cb8e7090SChristoph Hellwig dput(dentry); 657cb8e7090SChristoph Hellwig out_unlock: 65876dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 659cb8e7090SChristoph Hellwig return error; 660cb8e7090SChristoph Hellwig } 661cb8e7090SChristoph Hellwig 6624cb5300bSChris Mason /* 6634cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 6644cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 6654cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 6664cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 6674cb5300bSChris Mason * part of the file 6684cb5300bSChris Mason */ 6694cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 6704cb5300bSChris Mason { 6714cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 6724cb5300bSChris Mason struct extent_map *em = NULL; 6734cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 6744cb5300bSChris Mason u64 end; 6754cb5300bSChris Mason 6764cb5300bSChris Mason read_lock(&em_tree->lock); 6774cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 6784cb5300bSChris Mason read_unlock(&em_tree->lock); 6794cb5300bSChris Mason 6804cb5300bSChris Mason if (em) { 6814cb5300bSChris Mason end = extent_map_end(em); 6824cb5300bSChris Mason free_extent_map(em); 6834cb5300bSChris Mason if (end - offset > thresh) 6844cb5300bSChris Mason return 0; 6854cb5300bSChris Mason } 6864cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 6874cb5300bSChris Mason thresh /= 2; 6884cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 6894cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 6904cb5300bSChris Mason if (end >= thresh) 6914cb5300bSChris Mason return 0; 6924cb5300bSChris Mason return 1; 6934cb5300bSChris Mason } 6944cb5300bSChris Mason 6954cb5300bSChris Mason /* 6964cb5300bSChris Mason * helper function to walk through a file and find extents 6974cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 6984cb5300bSChris Mason * 6994cb5300bSChris Mason * This is used by the defragging code to find new and small 7004cb5300bSChris Mason * extents 7014cb5300bSChris Mason */ 7024cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 7034cb5300bSChris Mason struct inode *inode, u64 newer_than, 7044cb5300bSChris Mason u64 *off, int thresh) 7054cb5300bSChris Mason { 7064cb5300bSChris Mason struct btrfs_path *path; 7074cb5300bSChris Mason struct btrfs_key min_key; 7084cb5300bSChris Mason struct btrfs_key max_key; 7094cb5300bSChris Mason struct extent_buffer *leaf; 7104cb5300bSChris Mason struct btrfs_file_extent_item *extent; 7114cb5300bSChris Mason int type; 7124cb5300bSChris Mason int ret; 713a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 7144cb5300bSChris Mason 7154cb5300bSChris Mason path = btrfs_alloc_path(); 7164cb5300bSChris Mason if (!path) 7174cb5300bSChris Mason return -ENOMEM; 7184cb5300bSChris Mason 719a4689d2bSDavid Sterba min_key.objectid = ino; 7204cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 7214cb5300bSChris Mason min_key.offset = *off; 7224cb5300bSChris Mason 723a4689d2bSDavid Sterba max_key.objectid = ino; 7244cb5300bSChris Mason max_key.type = (u8)-1; 7254cb5300bSChris Mason max_key.offset = (u64)-1; 7264cb5300bSChris Mason 7274cb5300bSChris Mason path->keep_locks = 1; 7284cb5300bSChris Mason 7294cb5300bSChris Mason while(1) { 7304cb5300bSChris Mason ret = btrfs_search_forward(root, &min_key, &max_key, 7314cb5300bSChris Mason path, 0, newer_than); 7324cb5300bSChris Mason if (ret != 0) 7334cb5300bSChris Mason goto none; 734a4689d2bSDavid Sterba if (min_key.objectid != ino) 7354cb5300bSChris Mason goto none; 7364cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 7374cb5300bSChris Mason goto none; 7384cb5300bSChris Mason 7394cb5300bSChris Mason leaf = path->nodes[0]; 7404cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 7414cb5300bSChris Mason struct btrfs_file_extent_item); 7424cb5300bSChris Mason 7434cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 7444cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 7454cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 7464cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 7474cb5300bSChris Mason *off = min_key.offset; 7484cb5300bSChris Mason btrfs_free_path(path); 7494cb5300bSChris Mason return 0; 7504cb5300bSChris Mason } 7514cb5300bSChris Mason 7524cb5300bSChris Mason if (min_key.offset == (u64)-1) 7534cb5300bSChris Mason goto none; 7544cb5300bSChris Mason 7554cb5300bSChris Mason min_key.offset++; 7564cb5300bSChris Mason btrfs_release_path(path); 7574cb5300bSChris Mason } 7584cb5300bSChris Mason none: 7594cb5300bSChris Mason btrfs_free_path(path); 7604cb5300bSChris Mason return -ENOENT; 7614cb5300bSChris Mason } 7624cb5300bSChris Mason 763940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len, 7641e701a32SChris Mason int thresh, u64 *last_len, u64 *skip, 7651e701a32SChris Mason u64 *defrag_end) 766940100a4SChris Mason { 767940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 768940100a4SChris Mason struct extent_map *em = NULL; 769940100a4SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 770940100a4SChris Mason int ret = 1; 771940100a4SChris Mason 772940100a4SChris Mason /* 773008873eaSLi Zefan * make sure that once we start defragging an extent, we keep on 774940100a4SChris Mason * defragging it 775940100a4SChris Mason */ 776940100a4SChris Mason if (start < *defrag_end) 777940100a4SChris Mason return 1; 778940100a4SChris Mason 779940100a4SChris Mason *skip = 0; 780940100a4SChris Mason 781940100a4SChris Mason /* 782940100a4SChris Mason * hopefully we have this extent in the tree already, try without 783940100a4SChris Mason * the full extent lock 784940100a4SChris Mason */ 785940100a4SChris Mason read_lock(&em_tree->lock); 786940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 787940100a4SChris Mason read_unlock(&em_tree->lock); 788940100a4SChris Mason 789940100a4SChris Mason if (!em) { 790940100a4SChris Mason /* get the big lock and read metadata off disk */ 791940100a4SChris Mason lock_extent(io_tree, start, start + len - 1, GFP_NOFS); 792940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 793940100a4SChris Mason unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); 794940100a4SChris Mason 7956cf8bfbfSDan Carpenter if (IS_ERR(em)) 796940100a4SChris Mason return 0; 797940100a4SChris Mason } 798940100a4SChris Mason 799940100a4SChris Mason /* this will cover holes, and inline extents */ 800940100a4SChris Mason if (em->block_start >= EXTENT_MAP_LAST_BYTE) 801940100a4SChris Mason ret = 0; 802940100a4SChris Mason 803940100a4SChris Mason /* 804940100a4SChris Mason * we hit a real extent, if it is big don't bother defragging it again 805940100a4SChris Mason */ 8061e701a32SChris Mason if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) 807940100a4SChris Mason ret = 0; 808940100a4SChris Mason 809940100a4SChris Mason /* 810940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 811940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 812940100a4SChris Mason * so that the next tiny extent will force a defrag. 813940100a4SChris Mason * 814940100a4SChris Mason * The end result of this is that tiny extents before a single big 815940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 816940100a4SChris Mason */ 817940100a4SChris Mason if (ret) { 818940100a4SChris Mason *defrag_end = extent_map_end(em); 819940100a4SChris Mason } else { 820940100a4SChris Mason *last_len = 0; 821940100a4SChris Mason *skip = extent_map_end(em); 822940100a4SChris Mason *defrag_end = 0; 823940100a4SChris Mason } 824940100a4SChris Mason 825940100a4SChris Mason free_extent_map(em); 826940100a4SChris Mason return ret; 827940100a4SChris Mason } 828940100a4SChris Mason 8294cb5300bSChris Mason /* 8304cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 8314cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 8324cb5300bSChris Mason * to COW and defrag. 8334cb5300bSChris Mason * 8344cb5300bSChris Mason * It also makes sure the delalloc code has enough 8354cb5300bSChris Mason * dirty data to avoid making new small extents as part 8364cb5300bSChris Mason * of the defrag 8374cb5300bSChris Mason * 8384cb5300bSChris Mason * It's a good idea to start RA on this range 8394cb5300bSChris Mason * before calling this. 8404cb5300bSChris Mason */ 8414cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 8424cb5300bSChris Mason struct page **pages, 8434cb5300bSChris Mason unsigned long start_index, 8444cb5300bSChris Mason int num_pages) 845f46b5a66SChristoph Hellwig { 8464cb5300bSChris Mason unsigned long file_end; 8474cb5300bSChris Mason u64 isize = i_size_read(inode); 848f46b5a66SChristoph Hellwig u64 page_start; 849f46b5a66SChristoph Hellwig u64 page_end; 8504cb5300bSChris Mason int ret; 8514cb5300bSChris Mason int i; 8524cb5300bSChris Mason int i_done; 8534cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 8544cb5300bSChris Mason struct extent_state *cached_state = NULL; 8553b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 8564cb5300bSChris Mason 8574cb5300bSChris Mason if (isize == 0) 8584cb5300bSChris Mason return 0; 8594cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 8604cb5300bSChris Mason 861660d3f6cSJosef Bacik mutex_lock(&inode->i_mutex); 8624cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 8634cb5300bSChris Mason num_pages << PAGE_CACHE_SHIFT); 864660d3f6cSJosef Bacik mutex_unlock(&inode->i_mutex); 8654cb5300bSChris Mason if (ret) 8664cb5300bSChris Mason return ret; 8674cb5300bSChris Mason again: 8684cb5300bSChris Mason ret = 0; 8694cb5300bSChris Mason i_done = 0; 8704cb5300bSChris Mason 8714cb5300bSChris Mason /* step one, lock all the pages */ 8724cb5300bSChris Mason for (i = 0; i < num_pages; i++) { 8734cb5300bSChris Mason struct page *page; 874a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 8753b16a4e3SJosef Bacik start_index + i, mask); 8764cb5300bSChris Mason if (!page) 8774cb5300bSChris Mason break; 8784cb5300bSChris Mason 8794cb5300bSChris Mason if (!PageUptodate(page)) { 8804cb5300bSChris Mason btrfs_readpage(NULL, page); 8814cb5300bSChris Mason lock_page(page); 8824cb5300bSChris Mason if (!PageUptodate(page)) { 8834cb5300bSChris Mason unlock_page(page); 8844cb5300bSChris Mason page_cache_release(page); 8854cb5300bSChris Mason ret = -EIO; 8864cb5300bSChris Mason break; 8874cb5300bSChris Mason } 8884cb5300bSChris Mason } 8894cb5300bSChris Mason isize = i_size_read(inode); 8904cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 8914cb5300bSChris Mason if (!isize || page->index > file_end || 8924cb5300bSChris Mason page->mapping != inode->i_mapping) { 8934cb5300bSChris Mason /* whoops, we blew past eof, skip this page */ 8944cb5300bSChris Mason unlock_page(page); 8954cb5300bSChris Mason page_cache_release(page); 8964cb5300bSChris Mason break; 8974cb5300bSChris Mason } 8984cb5300bSChris Mason pages[i] = page; 8994cb5300bSChris Mason i_done++; 9004cb5300bSChris Mason } 9014cb5300bSChris Mason if (!i_done || ret) 9024cb5300bSChris Mason goto out; 9034cb5300bSChris Mason 9044cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 9054cb5300bSChris Mason goto out; 9064cb5300bSChris Mason 9074cb5300bSChris Mason /* 9084cb5300bSChris Mason * so now we have a nice long stream of locked 9094cb5300bSChris Mason * and up to date pages, lets wait on them 9104cb5300bSChris Mason */ 9114cb5300bSChris Mason for (i = 0; i < i_done; i++) 9124cb5300bSChris Mason wait_on_page_writeback(pages[i]); 9134cb5300bSChris Mason 9144cb5300bSChris Mason page_start = page_offset(pages[0]); 9154cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 9164cb5300bSChris Mason 9174cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 9184cb5300bSChris Mason page_start, page_end - 1, 0, &cached_state, 9194cb5300bSChris Mason GFP_NOFS); 9204cb5300bSChris Mason ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1); 9214cb5300bSChris Mason if (ordered && 9224cb5300bSChris Mason ordered->file_offset + ordered->len > page_start && 9234cb5300bSChris Mason ordered->file_offset < page_end) { 9244cb5300bSChris Mason btrfs_put_ordered_extent(ordered); 9254cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 9264cb5300bSChris Mason page_start, page_end - 1, 9274cb5300bSChris Mason &cached_state, GFP_NOFS); 9284cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9294cb5300bSChris Mason unlock_page(pages[i]); 9304cb5300bSChris Mason page_cache_release(pages[i]); 9314cb5300bSChris Mason } 9324cb5300bSChris Mason btrfs_wait_ordered_range(inode, page_start, 9334cb5300bSChris Mason page_end - page_start); 9344cb5300bSChris Mason goto again; 9354cb5300bSChris Mason } 9364cb5300bSChris Mason if (ordered) 9374cb5300bSChris Mason btrfs_put_ordered_extent(ordered); 9384cb5300bSChris Mason 9394cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 9404cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 9414cb5300bSChris Mason EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, 9424cb5300bSChris Mason GFP_NOFS); 9434cb5300bSChris Mason 9444cb5300bSChris Mason if (i_done != num_pages) { 9459e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 9469e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 9479e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 9484cb5300bSChris Mason btrfs_delalloc_release_space(inode, 9494cb5300bSChris Mason (num_pages - i_done) << PAGE_CACHE_SHIFT); 9504cb5300bSChris Mason } 9514cb5300bSChris Mason 9524cb5300bSChris Mason 9534cb5300bSChris Mason btrfs_set_extent_delalloc(inode, page_start, page_end - 1, 9544cb5300bSChris Mason &cached_state); 9554cb5300bSChris Mason 9564cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 9574cb5300bSChris Mason page_start, page_end - 1, &cached_state, 9584cb5300bSChris Mason GFP_NOFS); 9594cb5300bSChris Mason 9604cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9614cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 9624cb5300bSChris Mason ClearPageChecked(pages[i]); 9634cb5300bSChris Mason set_page_extent_mapped(pages[i]); 9644cb5300bSChris Mason set_page_dirty(pages[i]); 9654cb5300bSChris Mason unlock_page(pages[i]); 9664cb5300bSChris Mason page_cache_release(pages[i]); 9674cb5300bSChris Mason } 9684cb5300bSChris Mason return i_done; 9694cb5300bSChris Mason out: 9704cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9714cb5300bSChris Mason unlock_page(pages[i]); 9724cb5300bSChris Mason page_cache_release(pages[i]); 9734cb5300bSChris Mason } 9744cb5300bSChris Mason btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT); 9754cb5300bSChris Mason return ret; 9764cb5300bSChris Mason 9774cb5300bSChris Mason } 9784cb5300bSChris Mason 9794cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 9804cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 9814cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 9824cb5300bSChris Mason { 9834cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 9844cb5300bSChris Mason struct btrfs_super_block *disk_super; 9854cb5300bSChris Mason struct file_ra_state *ra = NULL; 9864cb5300bSChris Mason unsigned long last_index; 987151a31b2SLi Zefan u64 isize = i_size_read(inode); 9884cb5300bSChris Mason u64 features; 989940100a4SChris Mason u64 last_len = 0; 990940100a4SChris Mason u64 skip = 0; 991940100a4SChris Mason u64 defrag_end = 0; 9924cb5300bSChris Mason u64 newer_off = range->start; 993f46b5a66SChristoph Hellwig unsigned long i; 994008873eaSLi Zefan unsigned long ra_index = 0; 995f46b5a66SChristoph Hellwig int ret; 9964cb5300bSChris Mason int defrag_count = 0; 9971a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 9984cb5300bSChris Mason int extent_thresh = range->extent_thresh; 999008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1000008873eaSLi Zefan int cluster = max_cluster; 10014cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 10024cb5300bSChris Mason struct page **pages = NULL; 10034cb5300bSChris Mason 10044cb5300bSChris Mason if (extent_thresh == 0) 10054cb5300bSChris Mason extent_thresh = 256 * 1024; 10061a419d85SLi Zefan 10071a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 10081a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 10091a419d85SLi Zefan return -EINVAL; 10101a419d85SLi Zefan if (range->compress_type) 10111a419d85SLi Zefan compress_type = range->compress_type; 10121a419d85SLi Zefan } 1013f46b5a66SChristoph Hellwig 1014151a31b2SLi Zefan if (isize == 0) 1015940100a4SChris Mason return 0; 1016f46b5a66SChristoph Hellwig 10174cb5300bSChris Mason /* 10184cb5300bSChris Mason * if we were not given a file, allocate a readahead 10194cb5300bSChris Mason * context 10204cb5300bSChris Mason */ 10214cb5300bSChris Mason if (!file) { 10224cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 10234cb5300bSChris Mason if (!ra) 10244cb5300bSChris Mason return -ENOMEM; 10254cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 10264cb5300bSChris Mason } else { 10274cb5300bSChris Mason ra = &file->f_ra; 10284cb5300bSChris Mason } 10294cb5300bSChris Mason 1030008873eaSLi Zefan pages = kmalloc(sizeof(struct page *) * max_cluster, 10314cb5300bSChris Mason GFP_NOFS); 10324cb5300bSChris Mason if (!pages) { 10334cb5300bSChris Mason ret = -ENOMEM; 10344cb5300bSChris Mason goto out_ra; 10354cb5300bSChris Mason } 10364cb5300bSChris Mason 10374cb5300bSChris Mason /* find the last page to defrag */ 10381e701a32SChris Mason if (range->start + range->len > range->start) { 1039151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 10401e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 10411e701a32SChris Mason } else { 1042151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 10431e701a32SChris Mason } 10441e701a32SChris Mason 10454cb5300bSChris Mason if (newer_than) { 10464cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 10474cb5300bSChris Mason &newer_off, 64 * 1024); 10484cb5300bSChris Mason if (!ret) { 10494cb5300bSChris Mason range->start = newer_off; 10504cb5300bSChris Mason /* 10514cb5300bSChris Mason * we always align our defrag to help keep 10524cb5300bSChris Mason * the extents in the file evenly spaced 10534cb5300bSChris Mason */ 10544cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 10554cb5300bSChris Mason } else 10564cb5300bSChris Mason goto out_ra; 10574cb5300bSChris Mason } else { 10581e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 10594cb5300bSChris Mason } 10604cb5300bSChris Mason if (!max_to_defrag) 10615ca49660SLi Zefan max_to_defrag = last_index; 10624cb5300bSChris Mason 10632a0f7f57SLi Zefan /* 10642a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 10652a0f7f57SLi Zefan * written sequentially. 10662a0f7f57SLi Zefan */ 10672a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 10682a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 10692a0f7f57SLi Zefan 1070f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1071f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1072f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 10734cb5300bSChris Mason /* 10744cb5300bSChris Mason * make sure we stop running if someone unmounts 10754cb5300bSChris Mason * the FS 10764cb5300bSChris Mason */ 10774cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 10784cb5300bSChris Mason break; 10794cb5300bSChris Mason 10804cb5300bSChris Mason if (!newer_than && 10814cb5300bSChris Mason !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 10821e701a32SChris Mason PAGE_CACHE_SIZE, 10834cb5300bSChris Mason extent_thresh, 10841e701a32SChris Mason &last_len, &skip, 1085940100a4SChris Mason &defrag_end)) { 1086940100a4SChris Mason unsigned long next; 1087940100a4SChris Mason /* 1088940100a4SChris Mason * the should_defrag function tells us how much to skip 1089940100a4SChris Mason * bump our counter by the suggested amount 1090940100a4SChris Mason */ 1091940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1092940100a4SChris Mason i = max(i + 1, next); 1093940100a4SChris Mason continue; 1094940100a4SChris Mason } 1095008873eaSLi Zefan 1096008873eaSLi Zefan if (!newer_than) { 1097008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1098008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1099008873eaSLi Zefan cluster = min(cluster, max_cluster); 1100008873eaSLi Zefan } else { 1101008873eaSLi Zefan cluster = max_cluster; 1102008873eaSLi Zefan } 1103008873eaSLi Zefan 11041e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 11051a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 1106940100a4SChris Mason 1107008873eaSLi Zefan if (i + cluster > ra_index) { 1108008873eaSLi Zefan ra_index = max(i, ra_index); 1109008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1110008873eaSLi Zefan cluster); 1111008873eaSLi Zefan ra_index += max_cluster; 1112008873eaSLi Zefan } 11134cb5300bSChris Mason 1114008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 11154cb5300bSChris Mason if (ret < 0) 11164cb5300bSChris Mason goto out_ra; 11174cb5300bSChris Mason 11184cb5300bSChris Mason defrag_count += ret; 11194cb5300bSChris Mason balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); 11204cb5300bSChris Mason 11214cb5300bSChris Mason if (newer_than) { 11224cb5300bSChris Mason if (newer_off == (u64)-1) 11234cb5300bSChris Mason break; 11244cb5300bSChris Mason 11254cb5300bSChris Mason newer_off = max(newer_off + 1, 11264cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 11274cb5300bSChris Mason 11284cb5300bSChris Mason ret = find_new_extents(root, inode, 11294cb5300bSChris Mason newer_than, &newer_off, 11304cb5300bSChris Mason 64 * 1024); 11314cb5300bSChris Mason if (!ret) { 11324cb5300bSChris Mason range->start = newer_off; 11334cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 11344cb5300bSChris Mason } else { 11354cb5300bSChris Mason break; 1136940100a4SChris Mason } 11374cb5300bSChris Mason } else { 1138008873eaSLi Zefan if (ret > 0) { 1139cbcc8326SLi Zefan i += ret; 1140008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1141008873eaSLi Zefan } else { 1142940100a4SChris Mason i++; 1143008873eaSLi Zefan last_len = 0; 1144008873eaSLi Zefan } 1145f46b5a66SChristoph Hellwig } 11464cb5300bSChris Mason } 1147f46b5a66SChristoph Hellwig 11481e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 11491e701a32SChris Mason filemap_flush(inode->i_mapping); 11501e701a32SChris Mason 11511e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 11521e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 11531e701a32SChris Mason * we have to make sure the IO is actually started and that 11541e701a32SChris Mason * ordered extents get created before we return 11551e701a32SChris Mason */ 11561e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 11571e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 11581e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 11591e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 11601e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 11611e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 11621e701a32SChris Mason } 11631e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 11641e701a32SChris Mason 11651e701a32SChris Mason mutex_lock(&inode->i_mutex); 1166261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 11671e701a32SChris Mason mutex_unlock(&inode->i_mutex); 11681e701a32SChris Mason } 11691e701a32SChris Mason 11706c41761fSDavid Sterba disk_super = root->fs_info->super_copy; 11711a419d85SLi Zefan features = btrfs_super_incompat_flags(disk_super); 11721a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 11731a419d85SLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 11741a419d85SLi Zefan btrfs_set_super_incompat_flags(disk_super, features); 11751a419d85SLi Zefan } 11761a419d85SLi Zefan 117760ccf82fSDiego Calleja ret = defrag_count; 1178940100a4SChris Mason 11794cb5300bSChris Mason out_ra: 11804cb5300bSChris Mason if (!file) 11814cb5300bSChris Mason kfree(ra); 11824cb5300bSChris Mason kfree(pages); 1183940100a4SChris Mason return ret; 1184f46b5a66SChristoph Hellwig } 1185f46b5a66SChristoph Hellwig 118676dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root, 118776dda93cSYan, Zheng void __user *arg) 1188f46b5a66SChristoph Hellwig { 1189f46b5a66SChristoph Hellwig u64 new_size; 1190f46b5a66SChristoph Hellwig u64 old_size; 1191f46b5a66SChristoph Hellwig u64 devid = 1; 1192f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1193f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1194f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1195f46b5a66SChristoph Hellwig char *sizestr; 1196f46b5a66SChristoph Hellwig char *devstr = NULL; 1197f46b5a66SChristoph Hellwig int ret = 0; 1198f46b5a66SChristoph Hellwig int mod = 0; 1199f46b5a66SChristoph Hellwig 1200c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1201c146afadSYan Zheng return -EROFS; 1202c146afadSYan Zheng 1203e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1204e441d54dSChris Mason return -EPERM; 1205e441d54dSChris Mason 1206c9e9f97bSIlya Dryomov mutex_lock(&root->fs_info->volume_mutex); 1207c9e9f97bSIlya Dryomov if (root->fs_info->balance_ctl) { 1208c9e9f97bSIlya Dryomov printk(KERN_INFO "btrfs: balance in progress\n"); 1209c9e9f97bSIlya Dryomov ret = -EINVAL; 1210c9e9f97bSIlya Dryomov goto out; 1211c9e9f97bSIlya Dryomov } 1212c9e9f97bSIlya Dryomov 1213dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1214c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1215c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1216c9e9f97bSIlya Dryomov goto out; 1217c9e9f97bSIlya Dryomov } 12185516e595SMark Fasheh 12195516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1220f46b5a66SChristoph Hellwig 1221f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1222f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1223f46b5a66SChristoph Hellwig if (devstr) { 1224f46b5a66SChristoph Hellwig char *end; 1225f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1226f46b5a66SChristoph Hellwig *devstr = '\0'; 1227f46b5a66SChristoph Hellwig devstr = vol_args->name; 1228f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 12295bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizing devid %llu\n", 123021380931SJoel Becker (unsigned long long)devid); 1231f46b5a66SChristoph Hellwig } 12322b82032cSYan Zheng device = btrfs_find_device(root, devid, NULL, NULL); 1233f46b5a66SChristoph Hellwig if (!device) { 12345bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 123521380931SJoel Becker (unsigned long long)devid); 1236f46b5a66SChristoph Hellwig ret = -EINVAL; 1237c9e9f97bSIlya Dryomov goto out_free; 1238f46b5a66SChristoph Hellwig } 1239f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1240f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1241f46b5a66SChristoph Hellwig else { 1242f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1243f46b5a66SChristoph Hellwig mod = -1; 1244f46b5a66SChristoph Hellwig sizestr++; 1245f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1246f46b5a66SChristoph Hellwig mod = 1; 1247f46b5a66SChristoph Hellwig sizestr++; 1248f46b5a66SChristoph Hellwig } 124991748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1250f46b5a66SChristoph Hellwig if (new_size == 0) { 1251f46b5a66SChristoph Hellwig ret = -EINVAL; 1252c9e9f97bSIlya Dryomov goto out_free; 1253f46b5a66SChristoph Hellwig } 1254f46b5a66SChristoph Hellwig } 1255f46b5a66SChristoph Hellwig 1256f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1257f46b5a66SChristoph Hellwig 1258f46b5a66SChristoph Hellwig if (mod < 0) { 1259f46b5a66SChristoph Hellwig if (new_size > old_size) { 1260f46b5a66SChristoph Hellwig ret = -EINVAL; 1261c9e9f97bSIlya Dryomov goto out_free; 1262f46b5a66SChristoph Hellwig } 1263f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1264f46b5a66SChristoph Hellwig } else if (mod > 0) { 1265f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1266f46b5a66SChristoph Hellwig } 1267f46b5a66SChristoph Hellwig 1268f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1269f46b5a66SChristoph Hellwig ret = -EINVAL; 1270c9e9f97bSIlya Dryomov goto out_free; 1271f46b5a66SChristoph Hellwig } 1272f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1273f46b5a66SChristoph Hellwig ret = -EFBIG; 1274c9e9f97bSIlya Dryomov goto out_free; 1275f46b5a66SChristoph Hellwig } 1276f46b5a66SChristoph Hellwig 1277f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1278f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1279f46b5a66SChristoph Hellwig 12805bb14682SArnd Hannemann printk(KERN_INFO "btrfs: new size for %s is %llu\n", 1281f46b5a66SChristoph Hellwig device->name, (unsigned long long)new_size); 1282f46b5a66SChristoph Hellwig 1283f46b5a66SChristoph Hellwig if (new_size > old_size) { 1284a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 128598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 128698d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1287c9e9f97bSIlya Dryomov goto out_free; 128898d5dc13STsutomu Itoh } 1289f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1290f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1291ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1292f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 1293f46b5a66SChristoph Hellwig } 1294f46b5a66SChristoph Hellwig 1295c9e9f97bSIlya Dryomov out_free: 1296f46b5a66SChristoph Hellwig kfree(vol_args); 1297c9e9f97bSIlya Dryomov out: 1298c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 1299f46b5a66SChristoph Hellwig return ret; 1300f46b5a66SChristoph Hellwig } 1301f46b5a66SChristoph Hellwig 130272fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 130372fd032eSSage Weil char *name, 130472fd032eSSage Weil unsigned long fd, 130572fd032eSSage Weil int subvol, 1306b83cc969SLi Zefan u64 *transid, 1307b83cc969SLi Zefan bool readonly) 1308f46b5a66SChristoph Hellwig { 1309cb8e7090SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 13103de4586cSChris Mason struct file *src_file; 1311f46b5a66SChristoph Hellwig int namelen; 13123de4586cSChris Mason int ret = 0; 1313f46b5a66SChristoph Hellwig 1314c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1315c146afadSYan Zheng return -EROFS; 1316c146afadSYan Zheng 131772fd032eSSage Weil namelen = strlen(name); 131872fd032eSSage Weil if (strchr(name, '/')) { 1319f46b5a66SChristoph Hellwig ret = -EINVAL; 1320f46b5a66SChristoph Hellwig goto out; 1321f46b5a66SChristoph Hellwig } 1322f46b5a66SChristoph Hellwig 13233de4586cSChris Mason if (subvol) { 132472fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 1325b83cc969SLi Zefan NULL, transid, readonly); 1326cb8e7090SChristoph Hellwig } else { 13273de4586cSChris Mason struct inode *src_inode; 132872fd032eSSage Weil src_file = fget(fd); 13293de4586cSChris Mason if (!src_file) { 13303de4586cSChris Mason ret = -EINVAL; 13313de4586cSChris Mason goto out; 13323de4586cSChris Mason } 13333de4586cSChris Mason 13343de4586cSChris Mason src_inode = src_file->f_path.dentry->d_inode; 13353de4586cSChris Mason if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { 1336d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1337d397712bSChris Mason "another FS\n"); 13383de4586cSChris Mason ret = -EINVAL; 13393de4586cSChris Mason fput(src_file); 13403de4586cSChris Mason goto out; 13413de4586cSChris Mason } 134272fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 134372fd032eSSage Weil BTRFS_I(src_inode)->root, 1344b83cc969SLi Zefan transid, readonly); 13453de4586cSChris Mason fput(src_file); 1346cb8e7090SChristoph Hellwig } 1347f46b5a66SChristoph Hellwig out: 134872fd032eSSage Weil return ret; 134972fd032eSSage Weil } 135072fd032eSSage Weil 135172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1352fa0d2b9bSLi Zefan void __user *arg, int subvol) 135372fd032eSSage Weil { 1354fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 135572fd032eSSage Weil int ret; 135672fd032eSSage Weil 1357fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1358fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1359fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1360fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1361fa0d2b9bSLi Zefan 1362fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1363b83cc969SLi Zefan vol_args->fd, subvol, 1364b83cc969SLi Zefan NULL, false); 1365fa0d2b9bSLi Zefan 1366fa0d2b9bSLi Zefan kfree(vol_args); 1367fa0d2b9bSLi Zefan return ret; 1368fa0d2b9bSLi Zefan } 1369fa0d2b9bSLi Zefan 1370fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1371fa0d2b9bSLi Zefan void __user *arg, int subvol) 1372fa0d2b9bSLi Zefan { 1373fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1374fa0d2b9bSLi Zefan int ret; 1375fdfb1e4fSLi Zefan u64 transid = 0; 1376fdfb1e4fSLi Zefan u64 *ptr = NULL; 1377b83cc969SLi Zefan bool readonly = false; 137872fd032eSSage Weil 1379fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1380fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1381fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1382fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1383fdfb1e4fSLi Zefan 1384b83cc969SLi Zefan if (vol_args->flags & 1385b83cc969SLi Zefan ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { 1386b83cc969SLi Zefan ret = -EOPNOTSUPP; 1387fdfb1e4fSLi Zefan goto out; 1388fdfb1e4fSLi Zefan } 1389fdfb1e4fSLi Zefan 1390fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1391fdfb1e4fSLi Zefan ptr = &transid; 1392b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1393b83cc969SLi Zefan readonly = true; 139475eaa0e2SSage Weil 1395fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1396b83cc969SLi Zefan vol_args->fd, subvol, 1397b83cc969SLi Zefan ptr, readonly); 139875eaa0e2SSage Weil 1399fdfb1e4fSLi Zefan if (ret == 0 && ptr && 140075eaa0e2SSage Weil copy_to_user(arg + 1401fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1402fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 140375eaa0e2SSage Weil ret = -EFAULT; 1404fdfb1e4fSLi Zefan out: 1405f46b5a66SChristoph Hellwig kfree(vol_args); 1406f46b5a66SChristoph Hellwig return ret; 1407f46b5a66SChristoph Hellwig } 1408f46b5a66SChristoph Hellwig 14090caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 14100caa102dSLi Zefan void __user *arg) 14110caa102dSLi Zefan { 14120caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 14130caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 14140caa102dSLi Zefan int ret = 0; 14150caa102dSLi Zefan u64 flags = 0; 14160caa102dSLi Zefan 141733345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 14180caa102dSLi Zefan return -EINVAL; 14190caa102dSLi Zefan 14200caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 14210caa102dSLi Zefan if (btrfs_root_readonly(root)) 14220caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 14230caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 14240caa102dSLi Zefan 14250caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 14260caa102dSLi Zefan ret = -EFAULT; 14270caa102dSLi Zefan 14280caa102dSLi Zefan return ret; 14290caa102dSLi Zefan } 14300caa102dSLi Zefan 14310caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 14320caa102dSLi Zefan void __user *arg) 14330caa102dSLi Zefan { 14340caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 14350caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 14360caa102dSLi Zefan struct btrfs_trans_handle *trans; 14370caa102dSLi Zefan u64 root_flags; 14380caa102dSLi Zefan u64 flags; 14390caa102dSLi Zefan int ret = 0; 14400caa102dSLi Zefan 14410caa102dSLi Zefan if (root->fs_info->sb->s_flags & MS_RDONLY) 14420caa102dSLi Zefan return -EROFS; 14430caa102dSLi Zefan 144433345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 14450caa102dSLi Zefan return -EINVAL; 14460caa102dSLi Zefan 14470caa102dSLi Zefan if (copy_from_user(&flags, arg, sizeof(flags))) 14480caa102dSLi Zefan return -EFAULT; 14490caa102dSLi Zefan 1450b4dc2b8cSLi Zefan if (flags & BTRFS_SUBVOL_CREATE_ASYNC) 14510caa102dSLi Zefan return -EINVAL; 14520caa102dSLi Zefan 14530caa102dSLi Zefan if (flags & ~BTRFS_SUBVOL_RDONLY) 14540caa102dSLi Zefan return -EOPNOTSUPP; 14550caa102dSLi Zefan 14562e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1457b4dc2b8cSLi Zefan return -EACCES; 1458b4dc2b8cSLi Zefan 14590caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 14600caa102dSLi Zefan 14610caa102dSLi Zefan /* nothing to do */ 14620caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 14630caa102dSLi Zefan goto out; 14640caa102dSLi Zefan 14650caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 14660caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 14670caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 14680caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 14690caa102dSLi Zefan else 14700caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 14710caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 14720caa102dSLi Zefan 14730caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 14740caa102dSLi Zefan if (IS_ERR(trans)) { 14750caa102dSLi Zefan ret = PTR_ERR(trans); 14760caa102dSLi Zefan goto out_reset; 14770caa102dSLi Zefan } 14780caa102dSLi Zefan 1479b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 14800caa102dSLi Zefan &root->root_key, &root->root_item); 14810caa102dSLi Zefan 14820caa102dSLi Zefan btrfs_commit_transaction(trans, root); 14830caa102dSLi Zefan out_reset: 14840caa102dSLi Zefan if (ret) 14850caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 14860caa102dSLi Zefan out: 14870caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 14880caa102dSLi Zefan return ret; 14890caa102dSLi Zefan } 14900caa102dSLi Zefan 149176dda93cSYan, Zheng /* 149276dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 149376dda93cSYan, Zheng */ 149476dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 149576dda93cSYan, Zheng { 149676dda93cSYan, Zheng struct btrfs_path *path; 149776dda93cSYan, Zheng struct btrfs_key key; 149876dda93cSYan, Zheng int ret; 149976dda93cSYan, Zheng 150076dda93cSYan, Zheng path = btrfs_alloc_path(); 150176dda93cSYan, Zheng if (!path) 150276dda93cSYan, Zheng return -ENOMEM; 150376dda93cSYan, Zheng 150476dda93cSYan, Zheng key.objectid = root->root_key.objectid; 150576dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 150676dda93cSYan, Zheng key.offset = (u64)-1; 150776dda93cSYan, Zheng 150876dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 150976dda93cSYan, Zheng &key, path, 0, 0); 151076dda93cSYan, Zheng if (ret < 0) 151176dda93cSYan, Zheng goto out; 151276dda93cSYan, Zheng BUG_ON(ret == 0); 151376dda93cSYan, Zheng 151476dda93cSYan, Zheng ret = 0; 151576dda93cSYan, Zheng if (path->slots[0] > 0) { 151676dda93cSYan, Zheng path->slots[0]--; 151776dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 151876dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 151976dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 152076dda93cSYan, Zheng ret = -ENOTEMPTY; 152176dda93cSYan, Zheng } 152276dda93cSYan, Zheng out: 152376dda93cSYan, Zheng btrfs_free_path(path); 152476dda93cSYan, Zheng return ret; 152576dda93cSYan, Zheng } 152676dda93cSYan, Zheng 1527ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1528ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1529ac8e9819SChris Mason { 1530abc6e134SChris Mason struct btrfs_key test; 1531abc6e134SChris Mason int ret; 1532abc6e134SChris Mason 1533abc6e134SChris Mason test.objectid = sk->min_objectid; 1534abc6e134SChris Mason test.type = sk->min_type; 1535abc6e134SChris Mason test.offset = sk->min_offset; 1536abc6e134SChris Mason 1537abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1538abc6e134SChris Mason if (ret < 0) 1539ac8e9819SChris Mason return 0; 1540abc6e134SChris Mason 1541abc6e134SChris Mason test.objectid = sk->max_objectid; 1542abc6e134SChris Mason test.type = sk->max_type; 1543abc6e134SChris Mason test.offset = sk->max_offset; 1544abc6e134SChris Mason 1545abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1546abc6e134SChris Mason if (ret > 0) 1547ac8e9819SChris Mason return 0; 1548ac8e9819SChris Mason return 1; 1549ac8e9819SChris Mason } 1550ac8e9819SChris Mason 1551ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1552ac8e9819SChris Mason struct btrfs_path *path, 1553ac8e9819SChris Mason struct btrfs_key *key, 1554ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1555ac8e9819SChris Mason char *buf, 1556ac8e9819SChris Mason unsigned long *sk_offset, 1557ac8e9819SChris Mason int *num_found) 1558ac8e9819SChris Mason { 1559ac8e9819SChris Mason u64 found_transid; 1560ac8e9819SChris Mason struct extent_buffer *leaf; 1561ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1562ac8e9819SChris Mason unsigned long item_off; 1563ac8e9819SChris Mason unsigned long item_len; 1564ac8e9819SChris Mason int nritems; 1565ac8e9819SChris Mason int i; 1566ac8e9819SChris Mason int slot; 1567ac8e9819SChris Mason int ret = 0; 1568ac8e9819SChris Mason 1569ac8e9819SChris Mason leaf = path->nodes[0]; 1570ac8e9819SChris Mason slot = path->slots[0]; 1571ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1572ac8e9819SChris Mason 1573ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1574ac8e9819SChris Mason i = nritems; 1575ac8e9819SChris Mason goto advance_key; 1576ac8e9819SChris Mason } 1577ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1578ac8e9819SChris Mason 1579ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1580ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1581ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1582ac8e9819SChris Mason 1583ac8e9819SChris Mason if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1584ac8e9819SChris Mason item_len = 0; 1585ac8e9819SChris Mason 1586ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1587ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1588ac8e9819SChris Mason ret = 1; 1589ac8e9819SChris Mason goto overflow; 1590ac8e9819SChris Mason } 1591ac8e9819SChris Mason 1592ac8e9819SChris Mason btrfs_item_key_to_cpu(leaf, key, i); 1593ac8e9819SChris Mason if (!key_in_sk(key, sk)) 1594ac8e9819SChris Mason continue; 1595ac8e9819SChris Mason 1596ac8e9819SChris Mason sh.objectid = key->objectid; 1597ac8e9819SChris Mason sh.offset = key->offset; 1598ac8e9819SChris Mason sh.type = key->type; 1599ac8e9819SChris Mason sh.len = item_len; 1600ac8e9819SChris Mason sh.transid = found_transid; 1601ac8e9819SChris Mason 1602ac8e9819SChris Mason /* copy search result header */ 1603ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1604ac8e9819SChris Mason *sk_offset += sizeof(sh); 1605ac8e9819SChris Mason 1606ac8e9819SChris Mason if (item_len) { 1607ac8e9819SChris Mason char *p = buf + *sk_offset; 1608ac8e9819SChris Mason /* copy the item */ 1609ac8e9819SChris Mason read_extent_buffer(leaf, p, 1610ac8e9819SChris Mason item_off, item_len); 1611ac8e9819SChris Mason *sk_offset += item_len; 1612ac8e9819SChris Mason } 1613e2156867SHugo Mills (*num_found)++; 1614ac8e9819SChris Mason 1615ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1616ac8e9819SChris Mason break; 1617ac8e9819SChris Mason } 1618ac8e9819SChris Mason advance_key: 1619ac8e9819SChris Mason ret = 0; 1620abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1621abc6e134SChris Mason key->offset++; 1622abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1623abc6e134SChris Mason key->offset = 0; 1624abc6e134SChris Mason key->type++; 1625abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1626abc6e134SChris Mason key->offset = 0; 1627abc6e134SChris Mason key->type = 0; 1628abc6e134SChris Mason key->objectid++; 1629abc6e134SChris Mason } else 1630abc6e134SChris Mason ret = 1; 1631ac8e9819SChris Mason overflow: 1632ac8e9819SChris Mason return ret; 1633ac8e9819SChris Mason } 1634ac8e9819SChris Mason 1635ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1636ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1637ac8e9819SChris Mason { 1638ac8e9819SChris Mason struct btrfs_root *root; 1639ac8e9819SChris Mason struct btrfs_key key; 1640ac8e9819SChris Mason struct btrfs_key max_key; 1641ac8e9819SChris Mason struct btrfs_path *path; 1642ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1643ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1644ac8e9819SChris Mason int ret; 1645ac8e9819SChris Mason int num_found = 0; 1646ac8e9819SChris Mason unsigned long sk_offset = 0; 1647ac8e9819SChris Mason 1648ac8e9819SChris Mason path = btrfs_alloc_path(); 1649ac8e9819SChris Mason if (!path) 1650ac8e9819SChris Mason return -ENOMEM; 1651ac8e9819SChris Mason 1652ac8e9819SChris Mason if (sk->tree_id == 0) { 1653ac8e9819SChris Mason /* search the root of the inode that was passed */ 1654ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1655ac8e9819SChris Mason } else { 1656ac8e9819SChris Mason key.objectid = sk->tree_id; 1657ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1658ac8e9819SChris Mason key.offset = (u64)-1; 1659ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1660ac8e9819SChris Mason if (IS_ERR(root)) { 1661ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1662ac8e9819SChris Mason sk->tree_id); 1663ac8e9819SChris Mason btrfs_free_path(path); 1664ac8e9819SChris Mason return -ENOENT; 1665ac8e9819SChris Mason } 1666ac8e9819SChris Mason } 1667ac8e9819SChris Mason 1668ac8e9819SChris Mason key.objectid = sk->min_objectid; 1669ac8e9819SChris Mason key.type = sk->min_type; 1670ac8e9819SChris Mason key.offset = sk->min_offset; 1671ac8e9819SChris Mason 1672ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1673ac8e9819SChris Mason max_key.type = sk->max_type; 1674ac8e9819SChris Mason max_key.offset = sk->max_offset; 1675ac8e9819SChris Mason 1676ac8e9819SChris Mason path->keep_locks = 1; 1677ac8e9819SChris Mason 1678ac8e9819SChris Mason while(1) { 1679ac8e9819SChris Mason ret = btrfs_search_forward(root, &key, &max_key, path, 0, 1680ac8e9819SChris Mason sk->min_transid); 1681ac8e9819SChris Mason if (ret != 0) { 1682ac8e9819SChris Mason if (ret > 0) 1683ac8e9819SChris Mason ret = 0; 1684ac8e9819SChris Mason goto err; 1685ac8e9819SChris Mason } 1686ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1687ac8e9819SChris Mason &sk_offset, &num_found); 1688b3b4aa74SDavid Sterba btrfs_release_path(path); 1689ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1690ac8e9819SChris Mason break; 1691ac8e9819SChris Mason 1692ac8e9819SChris Mason } 1693ac8e9819SChris Mason ret = 0; 1694ac8e9819SChris Mason err: 1695ac8e9819SChris Mason sk->nr_items = num_found; 1696ac8e9819SChris Mason btrfs_free_path(path); 1697ac8e9819SChris Mason return ret; 1698ac8e9819SChris Mason } 1699ac8e9819SChris Mason 1700ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1701ac8e9819SChris Mason void __user *argp) 1702ac8e9819SChris Mason { 1703ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1704ac8e9819SChris Mason struct inode *inode; 1705ac8e9819SChris Mason int ret; 1706ac8e9819SChris Mason 1707ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1708ac8e9819SChris Mason return -EPERM; 1709ac8e9819SChris Mason 17102354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 17112354d08fSJulia Lawall if (IS_ERR(args)) 17122354d08fSJulia Lawall return PTR_ERR(args); 1713ac8e9819SChris Mason 1714ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1715ac8e9819SChris Mason ret = search_ioctl(inode, args); 1716ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1717ac8e9819SChris Mason ret = -EFAULT; 1718ac8e9819SChris Mason kfree(args); 1719ac8e9819SChris Mason return ret; 1720ac8e9819SChris Mason } 1721ac8e9819SChris Mason 172298d377a0STARUISI Hiroaki /* 1723ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1724ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 172598d377a0STARUISI Hiroaki */ 172698d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 172798d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 172898d377a0STARUISI Hiroaki { 172998d377a0STARUISI Hiroaki struct btrfs_root *root; 173098d377a0STARUISI Hiroaki struct btrfs_key key; 1731ac8e9819SChris Mason char *ptr; 173298d377a0STARUISI Hiroaki int ret = -1; 173398d377a0STARUISI Hiroaki int slot; 173498d377a0STARUISI Hiroaki int len; 173598d377a0STARUISI Hiroaki int total_len = 0; 173698d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 173798d377a0STARUISI Hiroaki struct extent_buffer *l; 173898d377a0STARUISI Hiroaki struct btrfs_path *path; 173998d377a0STARUISI Hiroaki 174098d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 174198d377a0STARUISI Hiroaki name[0]='\0'; 174298d377a0STARUISI Hiroaki return 0; 174398d377a0STARUISI Hiroaki } 174498d377a0STARUISI Hiroaki 174598d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 174698d377a0STARUISI Hiroaki if (!path) 174798d377a0STARUISI Hiroaki return -ENOMEM; 174898d377a0STARUISI Hiroaki 1749ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 175098d377a0STARUISI Hiroaki 175198d377a0STARUISI Hiroaki key.objectid = tree_id; 175298d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 175398d377a0STARUISI Hiroaki key.offset = (u64)-1; 175498d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 175598d377a0STARUISI Hiroaki if (IS_ERR(root)) { 175698d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 17578ad6fcabSChris Mason ret = -ENOENT; 17588ad6fcabSChris Mason goto out; 175998d377a0STARUISI Hiroaki } 176098d377a0STARUISI Hiroaki 176198d377a0STARUISI Hiroaki key.objectid = dirid; 176298d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 17638ad6fcabSChris Mason key.offset = (u64)-1; 176498d377a0STARUISI Hiroaki 176598d377a0STARUISI Hiroaki while(1) { 176698d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 176798d377a0STARUISI Hiroaki if (ret < 0) 176898d377a0STARUISI Hiroaki goto out; 176998d377a0STARUISI Hiroaki 177098d377a0STARUISI Hiroaki l = path->nodes[0]; 177198d377a0STARUISI Hiroaki slot = path->slots[0]; 17728ad6fcabSChris Mason if (ret > 0 && slot > 0) 17738ad6fcabSChris Mason slot--; 177498d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 177598d377a0STARUISI Hiroaki 177698d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 1777ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 1778ac8e9819SChris Mason ret = -ENOENT; 177998d377a0STARUISI Hiroaki goto out; 1780ac8e9819SChris Mason } 178198d377a0STARUISI Hiroaki 178298d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 178398d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 178498d377a0STARUISI Hiroaki ptr -= len + 1; 178598d377a0STARUISI Hiroaki total_len += len + 1; 1786ac8e9819SChris Mason if (ptr < name) 178798d377a0STARUISI Hiroaki goto out; 178898d377a0STARUISI Hiroaki 178998d377a0STARUISI Hiroaki *(ptr + len) = '/'; 179098d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 179198d377a0STARUISI Hiroaki 179298d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 179398d377a0STARUISI Hiroaki break; 179498d377a0STARUISI Hiroaki 1795b3b4aa74SDavid Sterba btrfs_release_path(path); 179698d377a0STARUISI Hiroaki key.objectid = key.offset; 17978ad6fcabSChris Mason key.offset = (u64)-1; 179898d377a0STARUISI Hiroaki dirid = key.objectid; 179998d377a0STARUISI Hiroaki } 1800ac8e9819SChris Mason if (ptr < name) 180198d377a0STARUISI Hiroaki goto out; 180277906a50SLi Zefan memmove(name, ptr, total_len); 180398d377a0STARUISI Hiroaki name[total_len]='\0'; 180498d377a0STARUISI Hiroaki ret = 0; 180598d377a0STARUISI Hiroaki out: 180698d377a0STARUISI Hiroaki btrfs_free_path(path); 1807ac8e9819SChris Mason return ret; 1808ac8e9819SChris Mason } 1809ac8e9819SChris Mason 1810ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 1811ac8e9819SChris Mason void __user *argp) 1812ac8e9819SChris Mason { 1813ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 1814ac8e9819SChris Mason struct inode *inode; 1815ac8e9819SChris Mason int ret; 1816ac8e9819SChris Mason 1817ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1818ac8e9819SChris Mason return -EPERM; 1819ac8e9819SChris Mason 18202354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 18212354d08fSJulia Lawall if (IS_ERR(args)) 18222354d08fSJulia Lawall return PTR_ERR(args); 1823c2b96929SDan Carpenter 1824ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1825ac8e9819SChris Mason 18261b53ac4dSChris Mason if (args->treeid == 0) 18271b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 18281b53ac4dSChris Mason 1829ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 1830ac8e9819SChris Mason args->treeid, args->objectid, 1831ac8e9819SChris Mason args->name); 1832ac8e9819SChris Mason 1833ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1834ac8e9819SChris Mason ret = -EFAULT; 1835ac8e9819SChris Mason 1836ac8e9819SChris Mason kfree(args); 183798d377a0STARUISI Hiroaki return ret; 183898d377a0STARUISI Hiroaki } 183998d377a0STARUISI Hiroaki 184076dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 184176dda93cSYan, Zheng void __user *arg) 184276dda93cSYan, Zheng { 184376dda93cSYan, Zheng struct dentry *parent = fdentry(file); 184476dda93cSYan, Zheng struct dentry *dentry; 184576dda93cSYan, Zheng struct inode *dir = parent->d_inode; 184676dda93cSYan, Zheng struct inode *inode; 184776dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 184876dda93cSYan, Zheng struct btrfs_root *dest = NULL; 184976dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 185076dda93cSYan, Zheng struct btrfs_trans_handle *trans; 185176dda93cSYan, Zheng int namelen; 185276dda93cSYan, Zheng int ret; 185376dda93cSYan, Zheng int err = 0; 185476dda93cSYan, Zheng 185576dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 185676dda93cSYan, Zheng if (IS_ERR(vol_args)) 185776dda93cSYan, Zheng return PTR_ERR(vol_args); 185876dda93cSYan, Zheng 185976dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 186076dda93cSYan, Zheng namelen = strlen(vol_args->name); 186176dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 186276dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 186376dda93cSYan, Zheng err = -EINVAL; 186476dda93cSYan, Zheng goto out; 186576dda93cSYan, Zheng } 186676dda93cSYan, Zheng 186776dda93cSYan, Zheng err = mnt_want_write(file->f_path.mnt); 186876dda93cSYan, Zheng if (err) 186976dda93cSYan, Zheng goto out; 187076dda93cSYan, Zheng 187176dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 187276dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 187376dda93cSYan, Zheng if (IS_ERR(dentry)) { 187476dda93cSYan, Zheng err = PTR_ERR(dentry); 187576dda93cSYan, Zheng goto out_unlock_dir; 187676dda93cSYan, Zheng } 187776dda93cSYan, Zheng 187876dda93cSYan, Zheng if (!dentry->d_inode) { 187976dda93cSYan, Zheng err = -ENOENT; 188076dda93cSYan, Zheng goto out_dput; 188176dda93cSYan, Zheng } 188276dda93cSYan, Zheng 188376dda93cSYan, Zheng inode = dentry->d_inode; 18844260f7c7SSage Weil dest = BTRFS_I(inode)->root; 18854260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 18864260f7c7SSage Weil /* 18874260f7c7SSage Weil * Regular user. Only allow this with a special mount 18884260f7c7SSage Weil * option, when the user has write+exec access to the 18894260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 18904260f7c7SSage Weil * allowed. 18914260f7c7SSage Weil * 18924260f7c7SSage Weil * Note that this is _not_ check that the subvol is 18934260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 18944260f7c7SSage Weil * otherwise be able to delete. 18954260f7c7SSage Weil * 18964260f7c7SSage Weil * Users who want to delete empty subvols should try 18974260f7c7SSage Weil * rmdir(2). 18984260f7c7SSage Weil */ 18994260f7c7SSage Weil err = -EPERM; 19004260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 19014260f7c7SSage Weil goto out_dput; 19024260f7c7SSage Weil 19034260f7c7SSage Weil /* 19044260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 19054260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 19064260f7c7SSage Weil * must be called on the dentry referencing the root 19074260f7c7SSage Weil * of the subvol, not a random directory contained 19084260f7c7SSage Weil * within it. 19094260f7c7SSage Weil */ 19104260f7c7SSage Weil err = -EINVAL; 19114260f7c7SSage Weil if (root == dest) 19124260f7c7SSage Weil goto out_dput; 19134260f7c7SSage Weil 19144260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 19154260f7c7SSage Weil if (err) 19164260f7c7SSage Weil goto out_dput; 19174260f7c7SSage Weil 19184260f7c7SSage Weil /* check if subvolume may be deleted by a non-root user */ 19194260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 19204260f7c7SSage Weil if (err) 19214260f7c7SSage Weil goto out_dput; 19224260f7c7SSage Weil } 19234260f7c7SSage Weil 192433345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 192576dda93cSYan, Zheng err = -EINVAL; 192676dda93cSYan, Zheng goto out_dput; 192776dda93cSYan, Zheng } 192876dda93cSYan, Zheng 192976dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 193076dda93cSYan, Zheng err = d_invalidate(dentry); 193176dda93cSYan, Zheng if (err) 193276dda93cSYan, Zheng goto out_unlock; 193376dda93cSYan, Zheng 193476dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 193576dda93cSYan, Zheng 193676dda93cSYan, Zheng err = may_destroy_subvol(dest); 193776dda93cSYan, Zheng if (err) 193876dda93cSYan, Zheng goto out_up_write; 193976dda93cSYan, Zheng 1940a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 1941a22285a6SYan, Zheng if (IS_ERR(trans)) { 1942a22285a6SYan, Zheng err = PTR_ERR(trans); 1943d327099aSDan Carpenter goto out_up_write; 1944a22285a6SYan, Zheng } 1945a22285a6SYan, Zheng trans->block_rsv = &root->fs_info->global_block_rsv; 1946a22285a6SYan, Zheng 194776dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 194876dda93cSYan, Zheng dest->root_key.objectid, 194976dda93cSYan, Zheng dentry->d_name.name, 195076dda93cSYan, Zheng dentry->d_name.len); 195176dda93cSYan, Zheng BUG_ON(ret); 195276dda93cSYan, Zheng 195376dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 195476dda93cSYan, Zheng 195576dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 195676dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 195776dda93cSYan, Zheng dest->root_item.drop_level = 0; 195876dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 195976dda93cSYan, Zheng 1960d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 196176dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 196276dda93cSYan, Zheng root->fs_info->tree_root, 196376dda93cSYan, Zheng dest->root_key.objectid); 196476dda93cSYan, Zheng BUG_ON(ret); 1965d68fc57bSYan, Zheng } 196676dda93cSYan, Zheng 1967531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 196876dda93cSYan, Zheng BUG_ON(ret); 196976dda93cSYan, Zheng inode->i_flags |= S_DEAD; 197076dda93cSYan, Zheng out_up_write: 197176dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 197276dda93cSYan, Zheng out_unlock: 197376dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 197476dda93cSYan, Zheng if (!err) { 1975efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 197676dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 197776dda93cSYan, Zheng d_delete(dentry); 197876dda93cSYan, Zheng } 197976dda93cSYan, Zheng out_dput: 198076dda93cSYan, Zheng dput(dentry); 198176dda93cSYan, Zheng out_unlock_dir: 198276dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 198376dda93cSYan, Zheng mnt_drop_write(file->f_path.mnt); 198476dda93cSYan, Zheng out: 198576dda93cSYan, Zheng kfree(vol_args); 198676dda93cSYan, Zheng return err; 198776dda93cSYan, Zheng } 198876dda93cSYan, Zheng 19891e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 1990f46b5a66SChristoph Hellwig { 1991f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 1992f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 19931e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 1994c146afadSYan Zheng int ret; 1995c146afadSYan Zheng 1996b83cc969SLi Zefan if (btrfs_root_readonly(root)) 1997b83cc969SLi Zefan return -EROFS; 1998b83cc969SLi Zefan 1999c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2000c146afadSYan Zheng if (ret) 2001c146afadSYan Zheng return ret; 2002f46b5a66SChristoph Hellwig 2003f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2004f46b5a66SChristoph Hellwig case S_IFDIR: 2005e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2006e441d54dSChris Mason ret = -EPERM; 2007e441d54dSChris Mason goto out; 2008e441d54dSChris Mason } 20098929ecfaSYan, Zheng ret = btrfs_defrag_root(root, 0); 20108929ecfaSYan, Zheng if (ret) 20118929ecfaSYan, Zheng goto out; 20128929ecfaSYan, Zheng ret = btrfs_defrag_root(root->fs_info->extent_root, 0); 2013f46b5a66SChristoph Hellwig break; 2014f46b5a66SChristoph Hellwig case S_IFREG: 2015e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2016e441d54dSChris Mason ret = -EINVAL; 2017e441d54dSChris Mason goto out; 2018e441d54dSChris Mason } 20191e701a32SChris Mason 20201e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 20211e701a32SChris Mason if (!range) { 20221e701a32SChris Mason ret = -ENOMEM; 20231e701a32SChris Mason goto out; 20241e701a32SChris Mason } 20251e701a32SChris Mason 20261e701a32SChris Mason if (argp) { 20271e701a32SChris Mason if (copy_from_user(range, argp, 20281e701a32SChris Mason sizeof(*range))) { 20291e701a32SChris Mason ret = -EFAULT; 20301e701a32SChris Mason kfree(range); 2031683be16eSDan Carpenter goto out; 20321e701a32SChris Mason } 20331e701a32SChris Mason /* compression requires us to start the IO */ 20341e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 20351e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 20361e701a32SChris Mason range->extent_thresh = (u32)-1; 20371e701a32SChris Mason } 20381e701a32SChris Mason } else { 20391e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 20401e701a32SChris Mason range->len = (u64)-1; 20411e701a32SChris Mason } 20424cb5300bSChris Mason ret = btrfs_defrag_file(fdentry(file)->d_inode, file, 20434cb5300bSChris Mason range, 0, 0); 20444cb5300bSChris Mason if (ret > 0) 20454cb5300bSChris Mason ret = 0; 20461e701a32SChris Mason kfree(range); 2047f46b5a66SChristoph Hellwig break; 20488929ecfaSYan, Zheng default: 20498929ecfaSYan, Zheng ret = -EINVAL; 2050f46b5a66SChristoph Hellwig } 2051e441d54dSChris Mason out: 2052ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 2053e441d54dSChris Mason return ret; 2054f46b5a66SChristoph Hellwig } 2055f46b5a66SChristoph Hellwig 2056b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2057f46b5a66SChristoph Hellwig { 2058f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2059f46b5a66SChristoph Hellwig int ret; 2060f46b5a66SChristoph Hellwig 2061e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2062e441d54dSChris Mason return -EPERM; 2063e441d54dSChris Mason 2064c9e9f97bSIlya Dryomov mutex_lock(&root->fs_info->volume_mutex); 2065c9e9f97bSIlya Dryomov if (root->fs_info->balance_ctl) { 2066c9e9f97bSIlya Dryomov printk(KERN_INFO "btrfs: balance in progress\n"); 2067c9e9f97bSIlya Dryomov ret = -EINVAL; 2068c9e9f97bSIlya Dryomov goto out; 2069c9e9f97bSIlya Dryomov } 2070c9e9f97bSIlya Dryomov 2071dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2072c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2073c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2074c9e9f97bSIlya Dryomov goto out; 2075c9e9f97bSIlya Dryomov } 2076f46b5a66SChristoph Hellwig 20775516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2078f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2079f46b5a66SChristoph Hellwig 2080f46b5a66SChristoph Hellwig kfree(vol_args); 2081c9e9f97bSIlya Dryomov out: 2082c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 2083f46b5a66SChristoph Hellwig return ret; 2084f46b5a66SChristoph Hellwig } 2085f46b5a66SChristoph Hellwig 2086b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) 2087f46b5a66SChristoph Hellwig { 2088f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2089f46b5a66SChristoph Hellwig int ret; 2090f46b5a66SChristoph Hellwig 2091e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2092e441d54dSChris Mason return -EPERM; 2093e441d54dSChris Mason 2094c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 2095c146afadSYan Zheng return -EROFS; 2096c146afadSYan Zheng 2097c9e9f97bSIlya Dryomov mutex_lock(&root->fs_info->volume_mutex); 2098c9e9f97bSIlya Dryomov if (root->fs_info->balance_ctl) { 2099c9e9f97bSIlya Dryomov printk(KERN_INFO "btrfs: balance in progress\n"); 2100c9e9f97bSIlya Dryomov ret = -EINVAL; 2101c9e9f97bSIlya Dryomov goto out; 2102c9e9f97bSIlya Dryomov } 2103c9e9f97bSIlya Dryomov 2104dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2105c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2106c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2107c9e9f97bSIlya Dryomov goto out; 2108c9e9f97bSIlya Dryomov } 2109f46b5a66SChristoph Hellwig 21105516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2111f46b5a66SChristoph Hellwig ret = btrfs_rm_device(root, vol_args->name); 2112f46b5a66SChristoph Hellwig 2113f46b5a66SChristoph Hellwig kfree(vol_args); 2114c9e9f97bSIlya Dryomov out: 2115c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 2116f46b5a66SChristoph Hellwig return ret; 2117f46b5a66SChristoph Hellwig } 2118f46b5a66SChristoph Hellwig 2119475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2120475f6387SJan Schmidt { 2121027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2122475f6387SJan Schmidt struct btrfs_device *device; 2123475f6387SJan Schmidt struct btrfs_device *next; 2124475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2125027ed2f0SLi Zefan int ret = 0; 2126475f6387SJan Schmidt 2127475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2128475f6387SJan Schmidt return -EPERM; 2129475f6387SJan Schmidt 2130027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2131027ed2f0SLi Zefan if (!fi_args) 2132027ed2f0SLi Zefan return -ENOMEM; 2133027ed2f0SLi Zefan 2134027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2135027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2136475f6387SJan Schmidt 2137475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2138475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2139027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2140027ed2f0SLi Zefan fi_args->max_id = device->devid; 2141475f6387SJan Schmidt } 2142475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2143475f6387SJan Schmidt 2144027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2145027ed2f0SLi Zefan ret = -EFAULT; 2146475f6387SJan Schmidt 2147027ed2f0SLi Zefan kfree(fi_args); 2148027ed2f0SLi Zefan return ret; 2149475f6387SJan Schmidt } 2150475f6387SJan Schmidt 2151475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2152475f6387SJan Schmidt { 2153475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2154475f6387SJan Schmidt struct btrfs_device *dev; 2155475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2156475f6387SJan Schmidt int ret = 0; 2157475f6387SJan Schmidt char *s_uuid = NULL; 2158475f6387SJan Schmidt char empty_uuid[BTRFS_UUID_SIZE] = {0}; 2159475f6387SJan Schmidt 2160475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2161475f6387SJan Schmidt return -EPERM; 2162475f6387SJan Schmidt 2163475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2164475f6387SJan Schmidt if (IS_ERR(di_args)) 2165475f6387SJan Schmidt return PTR_ERR(di_args); 2166475f6387SJan Schmidt 2167475f6387SJan Schmidt if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) 2168475f6387SJan Schmidt s_uuid = di_args->uuid; 2169475f6387SJan Schmidt 2170475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2171475f6387SJan Schmidt dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL); 2172475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2173475f6387SJan Schmidt 2174475f6387SJan Schmidt if (!dev) { 2175475f6387SJan Schmidt ret = -ENODEV; 2176475f6387SJan Schmidt goto out; 2177475f6387SJan Schmidt } 2178475f6387SJan Schmidt 2179475f6387SJan Schmidt di_args->devid = dev->devid; 2180475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2181475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2182475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2183475f6387SJan Schmidt strncpy(di_args->path, dev->name, sizeof(di_args->path)); 2184475f6387SJan Schmidt 2185475f6387SJan Schmidt out: 2186475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2187475f6387SJan Schmidt ret = -EFAULT; 2188475f6387SJan Schmidt 2189475f6387SJan Schmidt kfree(di_args); 2190475f6387SJan Schmidt return ret; 2191475f6387SJan Schmidt } 2192475f6387SJan Schmidt 219376dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 2194b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 2195f46b5a66SChristoph Hellwig { 2196f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2197f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2198f46b5a66SChristoph Hellwig struct file *src_file; 2199f46b5a66SChristoph Hellwig struct inode *src; 2200f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2201f46b5a66SChristoph Hellwig struct btrfs_path *path; 2202f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 2203ae01a0abSYan Zheng char *buf; 2204ae01a0abSYan Zheng struct btrfs_key key; 2205f46b5a66SChristoph Hellwig u32 nritems; 2206f46b5a66SChristoph Hellwig int slot; 2207ae01a0abSYan Zheng int ret; 2208c5c9cd4dSSage Weil u64 len = olen; 2209c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 2210c5c9cd4dSSage Weil u64 hint_byte; 2211d20f7043SChris Mason 2212c5c9cd4dSSage Weil /* 2213c5c9cd4dSSage Weil * TODO: 2214c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 2215c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 2216c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 2217c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 2218c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 2219c5c9cd4dSSage Weil * they don't overlap)? 2220c5c9cd4dSSage Weil */ 2221c5c9cd4dSSage Weil 2222e441d54dSChris Mason /* the destination must be opened for writing */ 22232ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 2224e441d54dSChris Mason return -EINVAL; 2225e441d54dSChris Mason 2226b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2227b83cc969SLi Zefan return -EROFS; 2228b83cc969SLi Zefan 2229c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2230c146afadSYan Zheng if (ret) 2231c146afadSYan Zheng return ret; 2232c146afadSYan Zheng 2233c5c9cd4dSSage Weil src_file = fget(srcfd); 2234ab67b7c1SYan Zheng if (!src_file) { 2235ab67b7c1SYan Zheng ret = -EBADF; 2236ab67b7c1SYan Zheng goto out_drop_write; 2237ab67b7c1SYan Zheng } 22385dc64164SDan Rosenberg 2239f46b5a66SChristoph Hellwig src = src_file->f_dentry->d_inode; 2240f46b5a66SChristoph Hellwig 2241c5c9cd4dSSage Weil ret = -EINVAL; 2242c5c9cd4dSSage Weil if (src == inode) 2243c5c9cd4dSSage Weil goto out_fput; 2244c5c9cd4dSSage Weil 22455dc64164SDan Rosenberg /* the src must be open for reading */ 22465dc64164SDan Rosenberg if (!(src_file->f_mode & FMODE_READ)) 22475dc64164SDan Rosenberg goto out_fput; 22485dc64164SDan Rosenberg 22490e7b824cSLi Zefan /* don't make the dst file partly checksummed */ 22500e7b824cSLi Zefan if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 22510e7b824cSLi Zefan (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 22520e7b824cSLi Zefan goto out_fput; 22530e7b824cSLi Zefan 2254ae01a0abSYan Zheng ret = -EISDIR; 2255ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2256f46b5a66SChristoph Hellwig goto out_fput; 2257f46b5a66SChristoph Hellwig 2258ae01a0abSYan Zheng ret = -EXDEV; 2259ae01a0abSYan Zheng if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) 2260ae01a0abSYan Zheng goto out_fput; 2261ae01a0abSYan Zheng 2262ae01a0abSYan Zheng ret = -ENOMEM; 2263ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2264ae01a0abSYan Zheng if (!buf) 2265ae01a0abSYan Zheng goto out_fput; 2266ae01a0abSYan Zheng 2267ae01a0abSYan Zheng path = btrfs_alloc_path(); 2268ae01a0abSYan Zheng if (!path) { 2269ae01a0abSYan Zheng vfree(buf); 2270ae01a0abSYan Zheng goto out_fput; 2271ae01a0abSYan Zheng } 2272ae01a0abSYan Zheng path->reada = 2; 2273ae01a0abSYan Zheng 2274f46b5a66SChristoph Hellwig if (inode < src) { 2275fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 2276fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 2277f46b5a66SChristoph Hellwig } else { 2278fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 2279fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 2280f46b5a66SChristoph Hellwig } 2281f46b5a66SChristoph Hellwig 2282c5c9cd4dSSage Weil /* determine range to clone */ 2283c5c9cd4dSSage Weil ret = -EINVAL; 22842ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 2285f46b5a66SChristoph Hellwig goto out_unlock; 2286c5c9cd4dSSage Weil if (len == 0) 2287c5c9cd4dSSage Weil olen = len = src->i_size - off; 2288c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 2289c5c9cd4dSSage Weil if (off + len == src->i_size) 22902a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 2291c5c9cd4dSSage Weil 2292c5c9cd4dSSage Weil /* verify the end result is block aligned */ 22932a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 22942a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 2295c5c9cd4dSSage Weil goto out_unlock; 2296c5c9cd4dSSage Weil 2297d525e8abSLi Zefan if (destoff > inode->i_size) { 2298d525e8abSLi Zefan ret = btrfs_cont_expand(inode, inode->i_size, destoff); 2299d525e8abSLi Zefan if (ret) 2300d525e8abSLi Zefan goto out_unlock; 2301d525e8abSLi Zefan } 2302d525e8abSLi Zefan 230371ef0786SLi Zefan /* truncate page cache pages from target inode range */ 230471ef0786SLi Zefan truncate_inode_pages_range(&inode->i_data, destoff, 230571ef0786SLi Zefan PAGE_CACHE_ALIGN(destoff + len) - 1); 230671ef0786SLi Zefan 2307f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 2308f46b5a66SChristoph Hellwig another, and lock file content */ 2309f46b5a66SChristoph Hellwig while (1) { 231031840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 2311c5c9cd4dSSage Weil lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 23129a019196SSage Weil ordered = btrfs_lookup_first_ordered_extent(src, off+len); 23139a019196SSage Weil if (!ordered && 23149a019196SSage Weil !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, 23159a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 2316f46b5a66SChristoph Hellwig break; 2317c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 2318ae01a0abSYan Zheng if (ordered) 2319ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 23209a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 2321f46b5a66SChristoph Hellwig } 2322f46b5a66SChristoph Hellwig 2323c5c9cd4dSSage Weil /* clone data */ 232433345d01SLi Zefan key.objectid = btrfs_ino(src); 2325ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2326ae01a0abSYan Zheng key.offset = 0; 2327f46b5a66SChristoph Hellwig 2328f46b5a66SChristoph Hellwig while (1) { 2329f46b5a66SChristoph Hellwig /* 2330f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2331f46b5a66SChristoph Hellwig * tree. 2332f46b5a66SChristoph Hellwig */ 2333a22285a6SYan, Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2334f46b5a66SChristoph Hellwig if (ret < 0) 2335f46b5a66SChristoph Hellwig goto out; 2336f46b5a66SChristoph Hellwig 2337ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2338ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2339f46b5a66SChristoph Hellwig ret = btrfs_next_leaf(root, path); 2340f46b5a66SChristoph Hellwig if (ret < 0) 2341f46b5a66SChristoph Hellwig goto out; 2342f46b5a66SChristoph Hellwig if (ret > 0) 2343f46b5a66SChristoph Hellwig break; 2344ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2345f46b5a66SChristoph Hellwig } 2346f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2347f46b5a66SChristoph Hellwig slot = path->slots[0]; 2348f46b5a66SChristoph Hellwig 2349ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2350d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 235133345d01SLi Zefan key.objectid != btrfs_ino(src)) 2352f46b5a66SChristoph Hellwig break; 2353f46b5a66SChristoph Hellwig 2354c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2355c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2356c5c9cd4dSSage Weil int type; 235731840ae1SZheng Yan u32 size; 235831840ae1SZheng Yan struct btrfs_key new_key; 2359c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2360c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2361c5c9cd4dSSage Weil u8 comp; 2362b5384d48SSage Weil u64 endoff; 236331840ae1SZheng Yan 236431840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 236531840ae1SZheng Yan read_extent_buffer(leaf, buf, 236631840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 236731840ae1SZheng Yan size); 2368c5c9cd4dSSage Weil 2369c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2370c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2371c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2372c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2373c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2374c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2375d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2376d397712bSChris Mason extent); 2377d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2378d397712bSChris Mason extent); 2379c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2380d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2381d397712bSChris Mason extent); 2382c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2383c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2384c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2385c5c9cd4dSSage Weil extent); 2386c5c9cd4dSSage Weil } 2387b3b4aa74SDavid Sterba btrfs_release_path(path); 238831840ae1SZheng Yan 2389050006a7SSage Weil if (key.offset + datal <= off || 2390c5c9cd4dSSage Weil key.offset >= off+len) 2391c5c9cd4dSSage Weil goto next; 2392c5c9cd4dSSage Weil 239331840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 239433345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 23954d728ec7SLi Zefan if (off <= key.offset) 2396c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 23974d728ec7SLi Zefan else 23984d728ec7SLi Zefan new_key.offset = destoff; 2399c5c9cd4dSSage Weil 2400b6f3409bSSage Weil /* 2401b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2402b6f3409bSSage Weil * 1 - add new extent 2403b6f3409bSSage Weil * 1 - inode update 2404b6f3409bSSage Weil */ 2405b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2406a22285a6SYan, Zheng if (IS_ERR(trans)) { 2407a22285a6SYan, Zheng ret = PTR_ERR(trans); 2408a22285a6SYan, Zheng goto out; 2409a22285a6SYan, Zheng } 2410a22285a6SYan, Zheng 2411c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2412c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2413d72c0842SLi Zefan /* 2414d72c0842SLi Zefan * a | --- range to clone ---| b 2415d72c0842SLi Zefan * | ------------- extent ------------- | 2416d72c0842SLi Zefan */ 2417d72c0842SLi Zefan 2418d72c0842SLi Zefan /* substract range b */ 2419d72c0842SLi Zefan if (key.offset + datal > off + len) 2420d72c0842SLi Zefan datal = off + len - key.offset; 2421d72c0842SLi Zefan 2422d72c0842SLi Zefan /* substract range a */ 2423a22285a6SYan, Zheng if (off > key.offset) { 2424a22285a6SYan, Zheng datao += off - key.offset; 2425a22285a6SYan, Zheng datal -= off - key.offset; 2426a22285a6SYan, Zheng } 2427a22285a6SYan, Zheng 2428a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 2429a22285a6SYan, Zheng new_key.offset, 2430a22285a6SYan, Zheng new_key.offset + datal, 2431a22285a6SYan, Zheng &hint_byte, 1); 2432a22285a6SYan, Zheng BUG_ON(ret); 2433a22285a6SYan, Zheng 243431840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 243531840ae1SZheng Yan &new_key, size); 2436a22285a6SYan, Zheng BUG_ON(ret); 243731840ae1SZheng Yan 243831840ae1SZheng Yan leaf = path->nodes[0]; 243931840ae1SZheng Yan slot = path->slots[0]; 244031840ae1SZheng Yan write_extent_buffer(leaf, buf, 244131840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 244231840ae1SZheng Yan size); 2443ae01a0abSYan Zheng 2444f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 2445f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 2446c5c9cd4dSSage Weil 2447c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 2448c5c9cd4dSSage Weil if (!disko) 2449c5c9cd4dSSage Weil datao = 0; 2450c5c9cd4dSSage Weil 2451c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 2452c5c9cd4dSSage Weil datao); 2453c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 2454c5c9cd4dSSage Weil datal); 2455c5c9cd4dSSage Weil if (disko) { 2456c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2457ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 24585d4f98a2SYan Zheng disko, diskl, 0, 2459f46b5a66SChristoph Hellwig root->root_key.objectid, 246033345d01SLi Zefan btrfs_ino(inode), 24615d4f98a2SYan Zheng new_key.offset - datao); 2462ae01a0abSYan Zheng BUG_ON(ret); 2463f46b5a66SChristoph Hellwig } 2464c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2465c5c9cd4dSSage Weil u64 skip = 0; 2466c5c9cd4dSSage Weil u64 trim = 0; 2467c5c9cd4dSSage Weil if (off > key.offset) { 2468c5c9cd4dSSage Weil skip = off - key.offset; 2469c5c9cd4dSSage Weil new_key.offset += skip; 247031840ae1SZheng Yan } 2471d397712bSChris Mason 2472c5c9cd4dSSage Weil if (key.offset + datal > off+len) 2473c5c9cd4dSSage Weil trim = key.offset + datal - (off+len); 2474d397712bSChris Mason 2475c5c9cd4dSSage Weil if (comp && (skip || trim)) { 2476c5c9cd4dSSage Weil ret = -EINVAL; 2477a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2478c5c9cd4dSSage Weil goto out; 247931840ae1SZheng Yan } 2480c5c9cd4dSSage Weil size -= skip + trim; 2481c5c9cd4dSSage Weil datal -= skip + trim; 2482a22285a6SYan, Zheng 2483a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 2484a22285a6SYan, Zheng new_key.offset, 2485a22285a6SYan, Zheng new_key.offset + datal, 2486a22285a6SYan, Zheng &hint_byte, 1); 2487a22285a6SYan, Zheng BUG_ON(ret); 2488a22285a6SYan, Zheng 2489c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 2490c5c9cd4dSSage Weil &new_key, size); 2491a22285a6SYan, Zheng BUG_ON(ret); 2492c5c9cd4dSSage Weil 2493c5c9cd4dSSage Weil if (skip) { 2494d397712bSChris Mason u32 start = 2495d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 2496c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 2497c5c9cd4dSSage Weil datal); 2498c5c9cd4dSSage Weil } 2499c5c9cd4dSSage Weil 2500c5c9cd4dSSage Weil leaf = path->nodes[0]; 2501c5c9cd4dSSage Weil slot = path->slots[0]; 2502c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2503c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2504c5c9cd4dSSage Weil size); 2505c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2506c5c9cd4dSSage Weil } 2507c5c9cd4dSSage Weil 2508c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2509b3b4aa74SDavid Sterba btrfs_release_path(path); 2510c5c9cd4dSSage Weil 2511a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2512b5384d48SSage Weil 2513b5384d48SSage Weil /* 2514b5384d48SSage Weil * we round up to the block size at eof when 2515b5384d48SSage Weil * determining which extents to clone above, 2516b5384d48SSage Weil * but shouldn't round up the file size 2517b5384d48SSage Weil */ 2518b5384d48SSage Weil endoff = new_key.offset + datal; 25195f3888ffSLi Zefan if (endoff > destoff+olen) 25205f3888ffSLi Zefan endoff = destoff+olen; 2521b5384d48SSage Weil if (endoff > inode->i_size) 2522b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2523b5384d48SSage Weil 2524a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 2525a22285a6SYan, Zheng BUG_ON(ret); 2526a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2527a22285a6SYan, Zheng } 2528c5c9cd4dSSage Weil next: 2529b3b4aa74SDavid Sterba btrfs_release_path(path); 2530ae01a0abSYan Zheng key.offset++; 2531ae01a0abSYan Zheng } 2532f46b5a66SChristoph Hellwig ret = 0; 2533f46b5a66SChristoph Hellwig out: 2534b3b4aa74SDavid Sterba btrfs_release_path(path); 2535c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 2536f46b5a66SChristoph Hellwig out_unlock: 2537f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2538f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2539ae01a0abSYan Zheng vfree(buf); 2540ae01a0abSYan Zheng btrfs_free_path(path); 2541f46b5a66SChristoph Hellwig out_fput: 2542f46b5a66SChristoph Hellwig fput(src_file); 2543ab67b7c1SYan Zheng out_drop_write: 2544ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 2545f46b5a66SChristoph Hellwig return ret; 2546f46b5a66SChristoph Hellwig } 2547f46b5a66SChristoph Hellwig 25487a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2549c5c9cd4dSSage Weil { 2550c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2551c5c9cd4dSSage Weil 25527a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2553c5c9cd4dSSage Weil return -EFAULT; 2554c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2555c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2556c5c9cd4dSSage Weil } 2557c5c9cd4dSSage Weil 2558f46b5a66SChristoph Hellwig /* 2559f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2560f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2561f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2562f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2563f46b5a66SChristoph Hellwig */ 2564b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2565f46b5a66SChristoph Hellwig { 2566f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2567f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2568f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 25691ab86aedSSage Weil int ret; 2570f46b5a66SChristoph Hellwig 25711ab86aedSSage Weil ret = -EPERM; 2572df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2573f46b5a66SChristoph Hellwig goto out; 25741ab86aedSSage Weil 25751ab86aedSSage Weil ret = -EINPROGRESS; 25761ab86aedSSage Weil if (file->private_data) 25771ab86aedSSage Weil goto out; 25789ca9ee09SSage Weil 2579b83cc969SLi Zefan ret = -EROFS; 2580b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2581b83cc969SLi Zefan goto out; 2582b83cc969SLi Zefan 2583c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2584c146afadSYan Zheng if (ret) 2585c146afadSYan Zheng goto out; 2586c146afadSYan Zheng 2587a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 25889ca9ee09SSage Weil 2589f46b5a66SChristoph Hellwig ret = -ENOMEM; 25907a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 2591abd30bb0STsutomu Itoh if (IS_ERR(trans)) 25921ab86aedSSage Weil goto out_drop; 25931ab86aedSSage Weil 25941ab86aedSSage Weil file->private_data = trans; 25951ab86aedSSage Weil return 0; 25961ab86aedSSage Weil 25971ab86aedSSage Weil out_drop: 2598a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 25991ab86aedSSage Weil mnt_drop_write(file->f_path.mnt); 2600f46b5a66SChristoph Hellwig out: 2601f46b5a66SChristoph Hellwig return ret; 2602f46b5a66SChristoph Hellwig } 2603f46b5a66SChristoph Hellwig 26046ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 26056ef5ed0dSJosef Bacik { 26066ef5ed0dSJosef Bacik struct inode *inode = fdentry(file)->d_inode; 26076ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 26086ef5ed0dSJosef Bacik struct btrfs_root *new_root; 26096ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 26106ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 26116ef5ed0dSJosef Bacik struct btrfs_path *path; 26126ef5ed0dSJosef Bacik struct btrfs_key location; 26136ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 26146ef5ed0dSJosef Bacik struct btrfs_super_block *disk_super; 26156ef5ed0dSJosef Bacik u64 features; 26166ef5ed0dSJosef Bacik u64 objectid = 0; 26176ef5ed0dSJosef Bacik u64 dir_id; 26186ef5ed0dSJosef Bacik 26196ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 26206ef5ed0dSJosef Bacik return -EPERM; 26216ef5ed0dSJosef Bacik 26226ef5ed0dSJosef Bacik if (copy_from_user(&objectid, argp, sizeof(objectid))) 26236ef5ed0dSJosef Bacik return -EFAULT; 26246ef5ed0dSJosef Bacik 26256ef5ed0dSJosef Bacik if (!objectid) 26266ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 26276ef5ed0dSJosef Bacik 26286ef5ed0dSJosef Bacik location.objectid = objectid; 26296ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 26306ef5ed0dSJosef Bacik location.offset = (u64)-1; 26316ef5ed0dSJosef Bacik 26326ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 26336ef5ed0dSJosef Bacik if (IS_ERR(new_root)) 26346ef5ed0dSJosef Bacik return PTR_ERR(new_root); 26356ef5ed0dSJosef Bacik 26366ef5ed0dSJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 26376ef5ed0dSJosef Bacik return -ENOENT; 26386ef5ed0dSJosef Bacik 26396ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 26406ef5ed0dSJosef Bacik if (!path) 26416ef5ed0dSJosef Bacik return -ENOMEM; 26426ef5ed0dSJosef Bacik path->leave_spinning = 1; 26436ef5ed0dSJosef Bacik 26446ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 264598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 26466ef5ed0dSJosef Bacik btrfs_free_path(path); 264798d5dc13STsutomu Itoh return PTR_ERR(trans); 26486ef5ed0dSJosef Bacik } 26496ef5ed0dSJosef Bacik 26506c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 26516ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 26526ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2653cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 26546ef5ed0dSJosef Bacik btrfs_free_path(path); 26556ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 26566ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 26576ef5ed0dSJosef Bacik "this isn't going to work\n"); 26586ef5ed0dSJosef Bacik return -ENOENT; 26596ef5ed0dSJosef Bacik } 26606ef5ed0dSJosef Bacik 26616ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 26626ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 26636ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 26646ef5ed0dSJosef Bacik btrfs_free_path(path); 26656ef5ed0dSJosef Bacik 26666c41761fSDavid Sterba disk_super = root->fs_info->super_copy; 26676ef5ed0dSJosef Bacik features = btrfs_super_incompat_flags(disk_super); 26686ef5ed0dSJosef Bacik if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { 26696ef5ed0dSJosef Bacik features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; 26706ef5ed0dSJosef Bacik btrfs_set_super_incompat_flags(disk_super, features); 26716ef5ed0dSJosef Bacik } 26726ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 26736ef5ed0dSJosef Bacik 26746ef5ed0dSJosef Bacik return 0; 26756ef5ed0dSJosef Bacik } 26766ef5ed0dSJosef Bacik 2677bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list, 2678bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 2679bf5fc093SJosef Bacik { 2680bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 2681bf5fc093SJosef Bacik 2682bf5fc093SJosef Bacik space->total_bytes = 0; 2683bf5fc093SJosef Bacik space->used_bytes = 0; 2684bf5fc093SJosef Bacik space->flags = 0; 2685bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 2686bf5fc093SJosef Bacik space->flags = block_group->flags; 2687bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 2688bf5fc093SJosef Bacik space->used_bytes += 2689bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 2690bf5fc093SJosef Bacik } 2691bf5fc093SJosef Bacik } 2692bf5fc093SJosef Bacik 26931406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 26941406e432SJosef Bacik { 26951406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 26961406e432SJosef Bacik struct btrfs_ioctl_space_info space; 26971406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 26987fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 269913f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 27001406e432SJosef Bacik struct btrfs_space_info *info; 2701bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 2702bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 2703bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 2704bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 2705bf5fc093SJosef Bacik int num_types = 4; 27067fde62bfSChris Mason int alloc_size; 27071406e432SJosef Bacik int ret = 0; 270851788b1bSDan Rosenberg u64 slot_count = 0; 2709bf5fc093SJosef Bacik int i, c; 27101406e432SJosef Bacik 27111406e432SJosef Bacik if (copy_from_user(&space_args, 27121406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 27131406e432SJosef Bacik sizeof(space_args))) 27141406e432SJosef Bacik return -EFAULT; 27151406e432SJosef Bacik 2716bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2717bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2718bf5fc093SJosef Bacik 2719bf5fc093SJosef Bacik info = NULL; 27207fde62bfSChris Mason rcu_read_lock(); 2721bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2722bf5fc093SJosef Bacik list) { 2723bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2724bf5fc093SJosef Bacik info = tmp; 2725bf5fc093SJosef Bacik break; 2726bf5fc093SJosef Bacik } 2727bf5fc093SJosef Bacik } 27287fde62bfSChris Mason rcu_read_unlock(); 27291406e432SJosef Bacik 2730bf5fc093SJosef Bacik if (!info) 2731bf5fc093SJosef Bacik continue; 2732bf5fc093SJosef Bacik 2733bf5fc093SJosef Bacik down_read(&info->groups_sem); 2734bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2735bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 2736bf5fc093SJosef Bacik slot_count++; 2737bf5fc093SJosef Bacik } 2738bf5fc093SJosef Bacik up_read(&info->groups_sem); 2739bf5fc093SJosef Bacik } 2740bf5fc093SJosef Bacik 27417fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 27427fde62bfSChris Mason if (space_args.space_slots == 0) { 27437fde62bfSChris Mason space_args.total_spaces = slot_count; 27447fde62bfSChris Mason goto out; 27457fde62bfSChris Mason } 2746bf5fc093SJosef Bacik 274751788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 2748bf5fc093SJosef Bacik 27497fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 2750bf5fc093SJosef Bacik 27517fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 27527fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 27537fde62bfSChris Mason */ 27547fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 27557fde62bfSChris Mason return -ENOMEM; 27567fde62bfSChris Mason 27577fde62bfSChris Mason space_args.total_spaces = 0; 27587fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 27597fde62bfSChris Mason if (!dest) 27607fde62bfSChris Mason return -ENOMEM; 27617fde62bfSChris Mason dest_orig = dest; 27627fde62bfSChris Mason 27637fde62bfSChris Mason /* now we have a buffer to copy into */ 2764bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2765bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2766bf5fc093SJosef Bacik 276751788b1bSDan Rosenberg if (!slot_count) 276851788b1bSDan Rosenberg break; 276951788b1bSDan Rosenberg 2770bf5fc093SJosef Bacik info = NULL; 27711406e432SJosef Bacik rcu_read_lock(); 2772bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2773bf5fc093SJosef Bacik list) { 2774bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2775bf5fc093SJosef Bacik info = tmp; 27767fde62bfSChris Mason break; 2777bf5fc093SJosef Bacik } 2778bf5fc093SJosef Bacik } 2779bf5fc093SJosef Bacik rcu_read_unlock(); 27807fde62bfSChris Mason 2781bf5fc093SJosef Bacik if (!info) 2782bf5fc093SJosef Bacik continue; 2783bf5fc093SJosef Bacik down_read(&info->groups_sem); 2784bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2785bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 2786bf5fc093SJosef Bacik get_block_group_info(&info->block_groups[c], 2787bf5fc093SJosef Bacik &space); 27887fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 27891406e432SJosef Bacik dest++; 27901406e432SJosef Bacik space_args.total_spaces++; 279151788b1bSDan Rosenberg slot_count--; 27921406e432SJosef Bacik } 279351788b1bSDan Rosenberg if (!slot_count) 279451788b1bSDan Rosenberg break; 2795bf5fc093SJosef Bacik } 2796bf5fc093SJosef Bacik up_read(&info->groups_sem); 2797bf5fc093SJosef Bacik } 27981406e432SJosef Bacik 27997fde62bfSChris Mason user_dest = (struct btrfs_ioctl_space_info *) 28007fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 28017fde62bfSChris Mason 28027fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 28037fde62bfSChris Mason ret = -EFAULT; 28047fde62bfSChris Mason 28057fde62bfSChris Mason kfree(dest_orig); 28067fde62bfSChris Mason out: 28077fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 28081406e432SJosef Bacik ret = -EFAULT; 28091406e432SJosef Bacik 28101406e432SJosef Bacik return ret; 28111406e432SJosef Bacik } 28121406e432SJosef Bacik 2813f46b5a66SChristoph Hellwig /* 2814f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2815f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2816f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2817f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2818f46b5a66SChristoph Hellwig */ 2819f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 2820f46b5a66SChristoph Hellwig { 2821f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2822f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2823f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2824f46b5a66SChristoph Hellwig 2825f46b5a66SChristoph Hellwig trans = file->private_data; 28261ab86aedSSage Weil if (!trans) 28271ab86aedSSage Weil return -EINVAL; 2828b214107eSChristoph Hellwig file->private_data = NULL; 28299ca9ee09SSage Weil 28301ab86aedSSage Weil btrfs_end_transaction(trans, root); 28311ab86aedSSage Weil 2832a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 28339ca9ee09SSage Weil 2834cfc8ea87SSage Weil mnt_drop_write(file->f_path.mnt); 28351ab86aedSSage Weil return 0; 2836f46b5a66SChristoph Hellwig } 2837f46b5a66SChristoph Hellwig 283846204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) 283946204592SSage Weil { 284046204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 284146204592SSage Weil struct btrfs_trans_handle *trans; 284246204592SSage Weil u64 transid; 2843db5b493aSTsutomu Itoh int ret; 284446204592SSage Weil 284546204592SSage Weil trans = btrfs_start_transaction(root, 0); 284698d5dc13STsutomu Itoh if (IS_ERR(trans)) 284798d5dc13STsutomu Itoh return PTR_ERR(trans); 284846204592SSage Weil transid = trans->transid; 2849db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 28508b2b2d3cSTsutomu Itoh if (ret) { 28518b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 2852db5b493aSTsutomu Itoh return ret; 28538b2b2d3cSTsutomu Itoh } 285446204592SSage Weil 285546204592SSage Weil if (argp) 285646204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 285746204592SSage Weil return -EFAULT; 285846204592SSage Weil return 0; 285946204592SSage Weil } 286046204592SSage Weil 286146204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) 286246204592SSage Weil { 286346204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 286446204592SSage Weil u64 transid; 286546204592SSage Weil 286646204592SSage Weil if (argp) { 286746204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 286846204592SSage Weil return -EFAULT; 286946204592SSage Weil } else { 287046204592SSage Weil transid = 0; /* current trans */ 287146204592SSage Weil } 287246204592SSage Weil return btrfs_wait_for_commit(root, transid); 287346204592SSage Weil } 287446204592SSage Weil 2875475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg) 2876475f6387SJan Schmidt { 2877475f6387SJan Schmidt int ret; 2878475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 2879475f6387SJan Schmidt 2880475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2881475f6387SJan Schmidt return -EPERM; 2882475f6387SJan Schmidt 2883475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 2884475f6387SJan Schmidt if (IS_ERR(sa)) 2885475f6387SJan Schmidt return PTR_ERR(sa); 2886475f6387SJan Schmidt 2887475f6387SJan Schmidt ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end, 28888628764eSArne Jansen &sa->progress, sa->flags & BTRFS_SCRUB_READONLY); 2889475f6387SJan Schmidt 2890475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 2891475f6387SJan Schmidt ret = -EFAULT; 2892475f6387SJan Schmidt 2893475f6387SJan Schmidt kfree(sa); 2894475f6387SJan Schmidt return ret; 2895475f6387SJan Schmidt } 2896475f6387SJan Schmidt 2897475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 2898475f6387SJan Schmidt { 2899475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2900475f6387SJan Schmidt return -EPERM; 2901475f6387SJan Schmidt 2902475f6387SJan Schmidt return btrfs_scrub_cancel(root); 2903475f6387SJan Schmidt } 2904475f6387SJan Schmidt 2905475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 2906475f6387SJan Schmidt void __user *arg) 2907475f6387SJan Schmidt { 2908475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 2909475f6387SJan Schmidt int ret; 2910475f6387SJan Schmidt 2911475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2912475f6387SJan Schmidt return -EPERM; 2913475f6387SJan Schmidt 2914475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 2915475f6387SJan Schmidt if (IS_ERR(sa)) 2916475f6387SJan Schmidt return PTR_ERR(sa); 2917475f6387SJan Schmidt 2918475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 2919475f6387SJan Schmidt 2920475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 2921475f6387SJan Schmidt ret = -EFAULT; 2922475f6387SJan Schmidt 2923475f6387SJan Schmidt kfree(sa); 2924475f6387SJan Schmidt return ret; 2925475f6387SJan Schmidt } 2926475f6387SJan Schmidt 2927d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 2928d7728c96SJan Schmidt { 2929d7728c96SJan Schmidt int ret = 0; 2930d7728c96SJan Schmidt int i; 2931740c3d22SChris Mason u64 rel_ptr; 2932d7728c96SJan Schmidt int size; 2933806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 2934d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 2935d7728c96SJan Schmidt struct btrfs_path *path; 2936d7728c96SJan Schmidt 2937d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2938d7728c96SJan Schmidt return -EPERM; 2939d7728c96SJan Schmidt 2940d7728c96SJan Schmidt path = btrfs_alloc_path(); 2941d7728c96SJan Schmidt if (!path) { 2942d7728c96SJan Schmidt ret = -ENOMEM; 2943d7728c96SJan Schmidt goto out; 2944d7728c96SJan Schmidt } 2945d7728c96SJan Schmidt 2946d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 2947d7728c96SJan Schmidt if (IS_ERR(ipa)) { 2948d7728c96SJan Schmidt ret = PTR_ERR(ipa); 2949d7728c96SJan Schmidt ipa = NULL; 2950d7728c96SJan Schmidt goto out; 2951d7728c96SJan Schmidt } 2952d7728c96SJan Schmidt 2953d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 2954d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 2955d7728c96SJan Schmidt if (IS_ERR(ipath)) { 2956d7728c96SJan Schmidt ret = PTR_ERR(ipath); 2957d7728c96SJan Schmidt ipath = NULL; 2958d7728c96SJan Schmidt goto out; 2959d7728c96SJan Schmidt } 2960d7728c96SJan Schmidt 2961d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 2962d7728c96SJan Schmidt if (ret < 0) 2963d7728c96SJan Schmidt goto out; 2964d7728c96SJan Schmidt 2965d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 2966745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 2967745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 2968740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 2969d7728c96SJan Schmidt } 2970d7728c96SJan Schmidt 2971745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 2972745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 2973d7728c96SJan Schmidt if (ret) { 2974d7728c96SJan Schmidt ret = -EFAULT; 2975d7728c96SJan Schmidt goto out; 2976d7728c96SJan Schmidt } 2977d7728c96SJan Schmidt 2978d7728c96SJan Schmidt out: 2979d7728c96SJan Schmidt btrfs_free_path(path); 2980d7728c96SJan Schmidt free_ipath(ipath); 2981d7728c96SJan Schmidt kfree(ipa); 2982d7728c96SJan Schmidt 2983d7728c96SJan Schmidt return ret; 2984d7728c96SJan Schmidt } 2985d7728c96SJan Schmidt 2986d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 2987d7728c96SJan Schmidt { 2988d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 2989d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 2990d7728c96SJan Schmidt 2991d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 2992d7728c96SJan Schmidt inodes->bytes_left -= c; 2993d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 2994d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 2995d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 2996d7728c96SJan Schmidt inodes->elem_cnt += 3; 2997d7728c96SJan Schmidt } else { 2998d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 2999d7728c96SJan Schmidt inodes->bytes_left = 0; 3000d7728c96SJan Schmidt inodes->elem_missed += 3; 3001d7728c96SJan Schmidt } 3002d7728c96SJan Schmidt 3003d7728c96SJan Schmidt return 0; 3004d7728c96SJan Schmidt } 3005d7728c96SJan Schmidt 3006d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 3007d7728c96SJan Schmidt void __user *arg) 3008d7728c96SJan Schmidt { 3009d7728c96SJan Schmidt int ret = 0; 3010d7728c96SJan Schmidt int size; 3011d7728c96SJan Schmidt u64 extent_offset; 3012d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3013d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3014d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3015d7728c96SJan Schmidt struct btrfs_key key; 3016d7728c96SJan Schmidt 3017d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3018d7728c96SJan Schmidt return -EPERM; 3019d7728c96SJan Schmidt 3020d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 3021d7728c96SJan Schmidt if (IS_ERR(loi)) { 3022d7728c96SJan Schmidt ret = PTR_ERR(loi); 3023d7728c96SJan Schmidt loi = NULL; 3024d7728c96SJan Schmidt goto out; 3025d7728c96SJan Schmidt } 3026d7728c96SJan Schmidt 3027d7728c96SJan Schmidt path = btrfs_alloc_path(); 3028d7728c96SJan Schmidt if (!path) { 3029d7728c96SJan Schmidt ret = -ENOMEM; 3030d7728c96SJan Schmidt goto out; 3031d7728c96SJan Schmidt } 3032d7728c96SJan Schmidt 3033d7728c96SJan Schmidt size = min_t(u32, loi->size, 4096); 3034d7728c96SJan Schmidt inodes = init_data_container(size); 3035d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3036d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3037d7728c96SJan Schmidt inodes = NULL; 3038d7728c96SJan Schmidt goto out; 3039d7728c96SJan Schmidt } 3040d7728c96SJan Schmidt 3041d7728c96SJan Schmidt ret = extent_from_logical(root->fs_info, loi->logical, path, &key); 3042d7728c96SJan Schmidt 3043d7728c96SJan Schmidt if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) 3044d7728c96SJan Schmidt ret = -ENOENT; 3045d7728c96SJan Schmidt if (ret < 0) 3046d7728c96SJan Schmidt goto out; 3047d7728c96SJan Schmidt 3048d7728c96SJan Schmidt extent_offset = loi->logical - key.objectid; 3049d7728c96SJan Schmidt ret = iterate_extent_inodes(root->fs_info, path, key.objectid, 3050d7728c96SJan Schmidt extent_offset, build_ino_list, inodes); 3051d7728c96SJan Schmidt 3052d7728c96SJan Schmidt if (ret < 0) 3053d7728c96SJan Schmidt goto out; 3054d7728c96SJan Schmidt 3055745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 3056745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 3057d7728c96SJan Schmidt if (ret) 3058d7728c96SJan Schmidt ret = -EFAULT; 3059d7728c96SJan Schmidt 3060d7728c96SJan Schmidt out: 3061d7728c96SJan Schmidt btrfs_free_path(path); 3062d7728c96SJan Schmidt kfree(inodes); 3063d7728c96SJan Schmidt kfree(loi); 3064d7728c96SJan Schmidt 3065d7728c96SJan Schmidt return ret; 3066d7728c96SJan Schmidt } 3067d7728c96SJan Schmidt 3068c9e9f97bSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, 3069c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3070c9e9f97bSIlya Dryomov { 3071c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3072c9e9f97bSIlya Dryomov 3073c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3074c9e9f97bSIlya Dryomov 3075837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 3076837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 3077837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 3078837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 3079*a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 3080*a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 3081837d5b6eSIlya Dryomov 3082c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 3083c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 3084c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 3085c9e9f97bSIlya Dryomov } 3086c9e9f97bSIlya Dryomov 3087c9e9f97bSIlya Dryomov static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg) 3088c9e9f97bSIlya Dryomov { 3089c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 3090c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 3091c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 3092c9e9f97bSIlya Dryomov int ret; 3093c9e9f97bSIlya Dryomov 3094c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3095c9e9f97bSIlya Dryomov return -EPERM; 3096c9e9f97bSIlya Dryomov 3097c9e9f97bSIlya Dryomov if (fs_info->sb->s_flags & MS_RDONLY) 3098c9e9f97bSIlya Dryomov return -EROFS; 3099c9e9f97bSIlya Dryomov 3100c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 3101c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3102c9e9f97bSIlya Dryomov 3103c9e9f97bSIlya Dryomov if (arg) { 3104c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 3105c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 3106c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 3107c9e9f97bSIlya Dryomov goto out; 3108c9e9f97bSIlya Dryomov } 3109c9e9f97bSIlya Dryomov } else { 3110c9e9f97bSIlya Dryomov bargs = NULL; 3111c9e9f97bSIlya Dryomov } 3112c9e9f97bSIlya Dryomov 3113837d5b6eSIlya Dryomov if (fs_info->balance_ctl) { 3114837d5b6eSIlya Dryomov ret = -EINPROGRESS; 3115837d5b6eSIlya Dryomov goto out_bargs; 3116837d5b6eSIlya Dryomov } 3117837d5b6eSIlya Dryomov 3118c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3119c9e9f97bSIlya Dryomov if (!bctl) { 3120c9e9f97bSIlya Dryomov ret = -ENOMEM; 3121c9e9f97bSIlya Dryomov goto out_bargs; 3122c9e9f97bSIlya Dryomov } 3123c9e9f97bSIlya Dryomov 3124c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 3125c9e9f97bSIlya Dryomov if (arg) { 3126c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 3127c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 3128c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 3129c9e9f97bSIlya Dryomov 3130c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 3131f43ffb60SIlya Dryomov } else { 3132f43ffb60SIlya Dryomov /* balance everything - no filters */ 3133f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 3134c9e9f97bSIlya Dryomov } 3135c9e9f97bSIlya Dryomov 3136c9e9f97bSIlya Dryomov ret = btrfs_balance(bctl, bargs); 3137c9e9f97bSIlya Dryomov /* 3138837d5b6eSIlya Dryomov * bctl is freed in __cancel_balance or in free_fs_info if 3139837d5b6eSIlya Dryomov * restriper was paused all the way until unmount 3140c9e9f97bSIlya Dryomov */ 3141c9e9f97bSIlya Dryomov if (arg) { 3142c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 3143c9e9f97bSIlya Dryomov ret = -EFAULT; 3144c9e9f97bSIlya Dryomov } 3145c9e9f97bSIlya Dryomov 3146c9e9f97bSIlya Dryomov out_bargs: 3147c9e9f97bSIlya Dryomov kfree(bargs); 3148c9e9f97bSIlya Dryomov out: 3149c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3150c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3151c9e9f97bSIlya Dryomov return ret; 3152c9e9f97bSIlya Dryomov } 3153c9e9f97bSIlya Dryomov 3154837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 3155837d5b6eSIlya Dryomov { 3156837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3157837d5b6eSIlya Dryomov return -EPERM; 3158837d5b6eSIlya Dryomov 3159837d5b6eSIlya Dryomov switch (cmd) { 3160837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 3161837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 3162*a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 3163*a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 3164837d5b6eSIlya Dryomov } 3165837d5b6eSIlya Dryomov 3166837d5b6eSIlya Dryomov return -EINVAL; 3167837d5b6eSIlya Dryomov } 3168837d5b6eSIlya Dryomov 3169f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 3170f46b5a66SChristoph Hellwig cmd, unsigned long arg) 3171f46b5a66SChristoph Hellwig { 3172f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 31734bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 3174f46b5a66SChristoph Hellwig 3175f46b5a66SChristoph Hellwig switch (cmd) { 31766cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 31776cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 31786cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 31796cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 31806cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 31816cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 3182f7039b1dSLi Dongyang case FITRIM: 3183f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 3184f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 3185fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 3186fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 3187fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 31883de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 3189fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 319076dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 319176dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 31920caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 31930caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 31940caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 31950caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 31966ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 31976ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 3198f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 31991e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 32001e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 32011e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 3202f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 32034bcabaa3SChristoph Hellwig return btrfs_ioctl_resize(root, argp); 3204f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 32054bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 3206f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 32074bcabaa3SChristoph Hellwig return btrfs_ioctl_rm_dev(root, argp); 3208475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 3209475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 3210475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 3211475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 3212f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 3213c9e9f97bSIlya Dryomov return btrfs_ioctl_balance(root, NULL); 3214f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 3215c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 3216c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 32177a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 3218f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 3219f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 3220f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 3221f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 3222ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 3223ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 3224ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 3225ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 3226d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 3227d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 3228d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 3229d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 32301406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 32311406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 3232f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 3233f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 3234f46b5a66SChristoph Hellwig return 0; 323546204592SSage Weil case BTRFS_IOC_START_SYNC: 323646204592SSage Weil return btrfs_ioctl_start_sync(file, argp); 323746204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 323846204592SSage Weil return btrfs_ioctl_wait_sync(file, argp); 3239475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 3240475f6387SJan Schmidt return btrfs_ioctl_scrub(root, argp); 3241475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 3242475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 3243475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 3244475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 3245c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 3246c9e9f97bSIlya Dryomov return btrfs_ioctl_balance(root, argp); 3247837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 3248837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 3249f46b5a66SChristoph Hellwig } 3250f46b5a66SChristoph Hellwig 3251f46b5a66SChristoph Hellwig return -ENOTTY; 3252f46b5a66SChristoph Hellwig } 3253