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" 54f46b5a66SChristoph Hellwig 556cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 566cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 576cbff00fSChristoph Hellwig { 586cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 596cbff00fSChristoph Hellwig return flags; 606cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 616cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 626cbff00fSChristoph Hellwig else 636cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 646cbff00fSChristoph Hellwig } 65f46b5a66SChristoph Hellwig 666cbff00fSChristoph Hellwig /* 676cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 686cbff00fSChristoph Hellwig */ 696cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 706cbff00fSChristoph Hellwig { 716cbff00fSChristoph Hellwig unsigned int iflags = 0; 726cbff00fSChristoph Hellwig 736cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 746cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 756cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 766cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 776cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 786cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 796cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 806cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 816cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 826cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 836cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 846cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 85d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 86d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 87d0092bddSLi Zefan 88d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 89d0092bddSLi Zefan iflags |= FS_COMPR_FL; 90d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 91d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 926cbff00fSChristoph Hellwig 936cbff00fSChristoph Hellwig return iflags; 946cbff00fSChristoph Hellwig } 956cbff00fSChristoph Hellwig 966cbff00fSChristoph Hellwig /* 976cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 986cbff00fSChristoph Hellwig */ 996cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1006cbff00fSChristoph Hellwig { 1016cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1026cbff00fSChristoph Hellwig 1036cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 1046cbff00fSChristoph Hellwig 1056cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1066cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 1076cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1086cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1096cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1106cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1116cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1126cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1136cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1146cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1156cbff00fSChristoph Hellwig } 1166cbff00fSChristoph Hellwig 1176cbff00fSChristoph Hellwig /* 1186cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1196cbff00fSChristoph Hellwig * 120e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1216cbff00fSChristoph Hellwig */ 1226cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1236cbff00fSChristoph Hellwig { 1240b4dcea5SChris Mason unsigned int flags; 1250b4dcea5SChris Mason 1260b4dcea5SChris Mason if (!dir) 1270b4dcea5SChris Mason return; 1280b4dcea5SChris Mason 1290b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1306cbff00fSChristoph Hellwig 131e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 132e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 133e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 134e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 135e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 136e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 137e27425d6SJosef Bacik } 1386cbff00fSChristoph Hellwig 139e27425d6SJosef Bacik if (flags & BTRFS_INODE_NODATACOW) 140e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 141e27425d6SJosef Bacik 1426cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1436cbff00fSChristoph Hellwig } 1446cbff00fSChristoph Hellwig 1456cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1466cbff00fSChristoph Hellwig { 1476cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); 1486cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1496cbff00fSChristoph Hellwig 1506cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1516cbff00fSChristoph Hellwig return -EFAULT; 1526cbff00fSChristoph Hellwig return 0; 1536cbff00fSChristoph Hellwig } 1546cbff00fSChristoph Hellwig 15575e7cb7fSLiu Bo static int check_flags(unsigned int flags) 15675e7cb7fSLiu Bo { 15775e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 15875e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 15975e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 160e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 161e1e8fb6aSLi Zefan FS_NOCOW_FL)) 16275e7cb7fSLiu Bo return -EOPNOTSUPP; 16375e7cb7fSLiu Bo 16475e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 16575e7cb7fSLiu Bo return -EINVAL; 16675e7cb7fSLiu Bo 16775e7cb7fSLiu Bo return 0; 16875e7cb7fSLiu Bo } 16975e7cb7fSLiu Bo 1706cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1716cbff00fSChristoph Hellwig { 1726cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 1736cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1746cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1756cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1766cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1776cbff00fSChristoph Hellwig int ret; 1786cbff00fSChristoph Hellwig 179b83cc969SLi Zefan if (btrfs_root_readonly(root)) 180b83cc969SLi Zefan return -EROFS; 181b83cc969SLi Zefan 1826cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1836cbff00fSChristoph Hellwig return -EFAULT; 1846cbff00fSChristoph Hellwig 18575e7cb7fSLiu Bo ret = check_flags(flags); 18675e7cb7fSLiu Bo if (ret) 18775e7cb7fSLiu Bo return ret; 1886cbff00fSChristoph Hellwig 1892e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1906cbff00fSChristoph Hellwig return -EACCES; 1916cbff00fSChristoph Hellwig 1926cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 1936cbff00fSChristoph Hellwig 1946cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 1956cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 1966cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 1976cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 1986cbff00fSChristoph Hellwig ret = -EPERM; 1996cbff00fSChristoph Hellwig goto out_unlock; 2006cbff00fSChristoph Hellwig } 2016cbff00fSChristoph Hellwig } 2026cbff00fSChristoph Hellwig 2036cbff00fSChristoph Hellwig ret = mnt_want_write(file->f_path.mnt); 2046cbff00fSChristoph Hellwig if (ret) 2056cbff00fSChristoph Hellwig goto out_unlock; 2066cbff00fSChristoph Hellwig 2076cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2086cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2096cbff00fSChristoph Hellwig else 2106cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2116cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2126cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2136cbff00fSChristoph Hellwig else 2146cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2156cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2166cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2176cbff00fSChristoph Hellwig else 2186cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2196cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2206cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2216cbff00fSChristoph Hellwig else 2226cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2236cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2246cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2256cbff00fSChristoph Hellwig else 2266cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2276cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2286cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2296cbff00fSChristoph Hellwig else 2306cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 231e1e8fb6aSLi Zefan if (flags & FS_NOCOW_FL) 232e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 233e1e8fb6aSLi Zefan else 234e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2356cbff00fSChristoph Hellwig 23675e7cb7fSLiu Bo /* 23775e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 23875e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 23975e7cb7fSLiu Bo * things smaller. 24075e7cb7fSLiu Bo */ 24175e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 24275e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 24375e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 24475e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 24575e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 24675e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 247ebcb904dSLi Zefan } else { 248ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 24975e7cb7fSLiu Bo } 2506cbff00fSChristoph Hellwig 2517a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 2523612b495STsutomu Itoh BUG_ON(IS_ERR(trans)); 2536cbff00fSChristoph Hellwig 2546cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 2556cbff00fSChristoph Hellwig BUG_ON(ret); 2566cbff00fSChristoph Hellwig 2576cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 2586cbff00fSChristoph Hellwig inode->i_ctime = CURRENT_TIME; 2596cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 2606cbff00fSChristoph Hellwig 2616cbff00fSChristoph Hellwig mnt_drop_write(file->f_path.mnt); 2622d4e6f6aSliubo 2632d4e6f6aSliubo ret = 0; 2646cbff00fSChristoph Hellwig out_unlock: 2656cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 2662d4e6f6aSliubo return ret; 2676cbff00fSChristoph Hellwig } 2686cbff00fSChristoph Hellwig 2696cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 2706cbff00fSChristoph Hellwig { 2716cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 2726cbff00fSChristoph Hellwig 2736cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 2746cbff00fSChristoph Hellwig } 275f46b5a66SChristoph Hellwig 276f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 277f7039b1dSLi Dongyang { 278f7039b1dSLi Dongyang struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info; 279f7039b1dSLi Dongyang struct btrfs_fs_info *fs_info = root->fs_info; 280f7039b1dSLi Dongyang struct btrfs_device *device; 281f7039b1dSLi Dongyang struct request_queue *q; 282f7039b1dSLi Dongyang struct fstrim_range range; 283f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 284f7039b1dSLi Dongyang u64 num_devices = 0; 285f4c697e6SLukas Czerner u64 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); 286f7039b1dSLi Dongyang int ret; 287f7039b1dSLi Dongyang 288f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 289f7039b1dSLi Dongyang return -EPERM; 290f7039b1dSLi Dongyang 2911f78160cSXiao Guangrong rcu_read_lock(); 2921f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 2931f78160cSXiao Guangrong dev_list) { 294f7039b1dSLi Dongyang if (!device->bdev) 295f7039b1dSLi Dongyang continue; 296f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 297f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 298f7039b1dSLi Dongyang num_devices++; 299f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 300f7039b1dSLi Dongyang minlen); 301f7039b1dSLi Dongyang } 302f7039b1dSLi Dongyang } 3031f78160cSXiao Guangrong rcu_read_unlock(); 304f4c697e6SLukas Czerner 305f7039b1dSLi Dongyang if (!num_devices) 306f7039b1dSLi Dongyang return -EOPNOTSUPP; 307f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 308f7039b1dSLi Dongyang return -EFAULT; 309f4c697e6SLukas Czerner if (range.start > total_bytes) 310f4c697e6SLukas Czerner return -EINVAL; 311f7039b1dSLi Dongyang 312f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 313f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 314f7039b1dSLi Dongyang ret = btrfs_trim_fs(root, &range); 315f7039b1dSLi Dongyang if (ret < 0) 316f7039b1dSLi Dongyang return ret; 317f7039b1dSLi Dongyang 318f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 319f7039b1dSLi Dongyang return -EFAULT; 320f7039b1dSLi Dongyang 321f7039b1dSLi Dongyang return 0; 322f7039b1dSLi Dongyang } 323f7039b1dSLi Dongyang 324cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root, 325cb8e7090SChristoph Hellwig struct dentry *dentry, 32672fd032eSSage Weil char *name, int namelen, 32772fd032eSSage Weil u64 *async_transid) 328f46b5a66SChristoph Hellwig { 329f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 330f46b5a66SChristoph Hellwig struct btrfs_key key; 331f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 332f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 333f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 33476dda93cSYan, Zheng struct btrfs_root *new_root; 3352fbe8c8aSAl Viro struct dentry *parent = dentry->d_parent; 3366a912213SJosef Bacik struct inode *dir; 337f46b5a66SChristoph Hellwig int ret; 338f46b5a66SChristoph Hellwig int err; 339f46b5a66SChristoph Hellwig u64 objectid; 340f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 3413de4586cSChris Mason u64 index = 0; 342f46b5a66SChristoph Hellwig 343581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 3442fbe8c8aSAl Viro if (ret) 345a22285a6SYan, Zheng return ret; 3466a912213SJosef Bacik 3476a912213SJosef Bacik dir = parent->d_inode; 3486a912213SJosef Bacik 3499ed74f2dSJosef Bacik /* 3509ed74f2dSJosef Bacik * 1 - inode item 3519ed74f2dSJosef Bacik * 2 - refs 3529ed74f2dSJosef Bacik * 1 - root item 3539ed74f2dSJosef Bacik * 2 - dir items 3549ed74f2dSJosef Bacik */ 355a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 3562fbe8c8aSAl Viro if (IS_ERR(trans)) 357a22285a6SYan, Zheng return PTR_ERR(trans); 358f46b5a66SChristoph Hellwig 3595d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 3605d4f98a2SYan Zheng 0, objectid, NULL, 0, 0, 0); 3618e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 3628e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 3638e8a1e31SJosef Bacik goto fail; 3648e8a1e31SJosef Bacik } 365f46b5a66SChristoph Hellwig 3665d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 367f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 368f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 3695d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 370f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 371f46b5a66SChristoph Hellwig 372f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 373f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 374f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 3755d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 3765d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 3775d4f98a2SYan Zheng BTRFS_UUID_SIZE); 378f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 379f46b5a66SChristoph Hellwig 380f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 381f46b5a66SChristoph Hellwig memset(inode_item, 0, sizeof(*inode_item)); 382f46b5a66SChristoph Hellwig inode_item->generation = cpu_to_le64(1); 383f46b5a66SChristoph Hellwig inode_item->size = cpu_to_le64(3); 384f46b5a66SChristoph Hellwig inode_item->nlink = cpu_to_le32(1); 385a76a3cd4SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 386f46b5a66SChristoph Hellwig inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 387f46b5a66SChristoph Hellwig 38808fe4db1SLi Zefan root_item.flags = 0; 38908fe4db1SLi Zefan root_item.byte_limit = 0; 39008fe4db1SLi Zefan inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); 39108fe4db1SLi Zefan 392f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 39384234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 394f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 395f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 39686b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 39780ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 398f46b5a66SChristoph Hellwig 399f46b5a66SChristoph Hellwig memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 400f46b5a66SChristoph Hellwig root_item.drop_level = 0; 401f46b5a66SChristoph Hellwig 402925baeddSChris Mason btrfs_tree_unlock(leaf); 403f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 404f46b5a66SChristoph Hellwig leaf = NULL; 405f46b5a66SChristoph Hellwig 406f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 407f46b5a66SChristoph Hellwig 408f46b5a66SChristoph Hellwig key.objectid = objectid; 4095d4f98a2SYan Zheng key.offset = 0; 410f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 411f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 412f46b5a66SChristoph Hellwig &root_item); 413f46b5a66SChristoph Hellwig if (ret) 414f46b5a66SChristoph Hellwig goto fail; 415f46b5a66SChristoph Hellwig 41676dda93cSYan, Zheng key.offset = (u64)-1; 41776dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 41876dda93cSYan, Zheng BUG_ON(IS_ERR(new_root)); 41976dda93cSYan, Zheng 42076dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 42176dda93cSYan, Zheng 422d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 423f46b5a66SChristoph Hellwig /* 424f46b5a66SChristoph Hellwig * insert the directory item 425f46b5a66SChristoph Hellwig */ 4263de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 4273de4586cSChris Mason BUG_ON(ret); 4283de4586cSChris Mason 4293de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 43016cdcec7SMiao Xie name, namelen, dir, &key, 4313de4586cSChris Mason BTRFS_FT_DIR, index); 432f46b5a66SChristoph Hellwig if (ret) 433f46b5a66SChristoph Hellwig goto fail; 4340660b5afSChris Mason 43552c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 43652c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 43752c26179SYan Zheng BUG_ON(ret); 43852c26179SYan Zheng 4390660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4404df27c4dSYan, Zheng objectid, root->root_key.objectid, 44133345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 44276dda93cSYan, Zheng 4430660b5afSChris Mason BUG_ON(ret); 4440660b5afSChris Mason 44576dda93cSYan, Zheng d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 446f46b5a66SChristoph Hellwig fail: 44772fd032eSSage Weil if (async_transid) { 44872fd032eSSage Weil *async_transid = trans->transid; 44972fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 45072fd032eSSage Weil } else { 45176dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 45272fd032eSSage Weil } 453f46b5a66SChristoph Hellwig if (err && !ret) 454f46b5a66SChristoph Hellwig ret = err; 455f46b5a66SChristoph Hellwig return ret; 456f46b5a66SChristoph Hellwig } 457f46b5a66SChristoph Hellwig 45872fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 459b83cc969SLi Zefan char *name, int namelen, u64 *async_transid, 460b83cc969SLi Zefan bool readonly) 461f46b5a66SChristoph Hellwig { 4622e4bfab9SYan, Zheng struct inode *inode; 463f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 464f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 4652e4bfab9SYan, Zheng int ret; 466f46b5a66SChristoph Hellwig 467f46b5a66SChristoph Hellwig if (!root->ref_cows) 468f46b5a66SChristoph Hellwig return -EINVAL; 469f46b5a66SChristoph Hellwig 470a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 471a22285a6SYan, Zheng if (!pending_snapshot) 472a22285a6SYan, Zheng return -ENOMEM; 473a22285a6SYan, Zheng 474a22285a6SYan, Zheng btrfs_init_block_rsv(&pending_snapshot->block_rsv); 475a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 476a22285a6SYan, Zheng pending_snapshot->root = root; 477b83cc969SLi Zefan pending_snapshot->readonly = readonly; 478a22285a6SYan, Zheng 479a22285a6SYan, Zheng trans = btrfs_start_transaction(root->fs_info->extent_root, 5); 480a22285a6SYan, Zheng if (IS_ERR(trans)) { 481a22285a6SYan, Zheng ret = PTR_ERR(trans); 482a22285a6SYan, Zheng goto fail; 483a22285a6SYan, Zheng } 484a22285a6SYan, Zheng 485a22285a6SYan, Zheng ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); 486a22285a6SYan, Zheng BUG_ON(ret); 487a22285a6SYan, Zheng 4888351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 489a22285a6SYan, Zheng list_add(&pending_snapshot->list, 490a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 4918351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 49272fd032eSSage Weil if (async_transid) { 49372fd032eSSage Weil *async_transid = trans->transid; 49472fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 49572fd032eSSage Weil root->fs_info->extent_root, 1); 49672fd032eSSage Weil } else { 49772fd032eSSage Weil ret = btrfs_commit_transaction(trans, 49872fd032eSSage Weil root->fs_info->extent_root); 49972fd032eSSage Weil } 500a22285a6SYan, Zheng BUG_ON(ret); 501a22285a6SYan, Zheng 502a22285a6SYan, Zheng ret = pending_snapshot->error; 503f46b5a66SChristoph Hellwig if (ret) 5042e4bfab9SYan, Zheng goto fail; 505f46b5a66SChristoph Hellwig 50666b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 50766b4ffd1SJosef Bacik if (ret) 50866b4ffd1SJosef Bacik goto fail; 509f46b5a66SChristoph Hellwig 5102fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 5112e4bfab9SYan, Zheng if (IS_ERR(inode)) { 5122e4bfab9SYan, Zheng ret = PTR_ERR(inode); 5132e4bfab9SYan, Zheng goto fail; 5142e4bfab9SYan, Zheng } 5152e4bfab9SYan, Zheng BUG_ON(!inode); 5162e4bfab9SYan, Zheng d_instantiate(dentry, inode); 5172e4bfab9SYan, Zheng ret = 0; 5182e4bfab9SYan, Zheng fail: 519a22285a6SYan, Zheng kfree(pending_snapshot); 520f46b5a66SChristoph Hellwig return ret; 521f46b5a66SChristoph Hellwig } 522f46b5a66SChristoph Hellwig 5234260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 5244260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 5254260f7c7SSage Weil * minimal. 5264260f7c7SSage Weil */ 5274260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 5284260f7c7SSage Weil { 5294260f7c7SSage Weil uid_t fsuid = current_fsuid(); 5304260f7c7SSage Weil 5314260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 5324260f7c7SSage Weil return 0; 5334260f7c7SSage Weil if (inode->i_uid == fsuid) 5344260f7c7SSage Weil return 0; 5354260f7c7SSage Weil if (dir->i_uid == fsuid) 5364260f7c7SSage Weil return 0; 5374260f7c7SSage Weil return !capable(CAP_FOWNER); 5384260f7c7SSage Weil } 5394260f7c7SSage Weil 5404260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 5414260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 5424260f7c7SSage Weil * whether the type of victim is right. 5434260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 5444260f7c7SSage Weil * 2. We should have write and exec permissions on dir 5454260f7c7SSage Weil * 3. We can't remove anything from append-only dir 5464260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 5474260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 5484260f7c7SSage Weil * a. be owner of dir, or 5494260f7c7SSage Weil * b. be owner of victim, or 5504260f7c7SSage Weil * c. have CAP_FOWNER capability 5514260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 5524260f7c7SSage Weil * links pointing to it. 5534260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 5544260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 5554260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 5564260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 5574260f7c7SSage Weil * nfs_async_unlink(). 5584260f7c7SSage Weil */ 5594260f7c7SSage Weil 5604260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 5614260f7c7SSage Weil { 5624260f7c7SSage Weil int error; 5634260f7c7SSage Weil 5644260f7c7SSage Weil if (!victim->d_inode) 5654260f7c7SSage Weil return -ENOENT; 5664260f7c7SSage Weil 5674260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 5684260f7c7SSage Weil audit_inode_child(victim, dir); 5694260f7c7SSage Weil 5704260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 5714260f7c7SSage Weil if (error) 5724260f7c7SSage Weil return error; 5734260f7c7SSage Weil if (IS_APPEND(dir)) 5744260f7c7SSage Weil return -EPERM; 5754260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 5764260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 5774260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 5784260f7c7SSage Weil return -EPERM; 5794260f7c7SSage Weil if (isdir) { 5804260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 5814260f7c7SSage Weil return -ENOTDIR; 5824260f7c7SSage Weil if (IS_ROOT(victim)) 5834260f7c7SSage Weil return -EBUSY; 5844260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 5854260f7c7SSage Weil return -EISDIR; 5864260f7c7SSage Weil if (IS_DEADDIR(dir)) 5874260f7c7SSage Weil return -ENOENT; 5884260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 5894260f7c7SSage Weil return -EBUSY; 5904260f7c7SSage Weil return 0; 5914260f7c7SSage Weil } 5924260f7c7SSage Weil 593cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 594cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 595cb8e7090SChristoph Hellwig { 596cb8e7090SChristoph Hellwig if (child->d_inode) 597cb8e7090SChristoph Hellwig return -EEXIST; 598cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 599cb8e7090SChristoph Hellwig return -ENOENT; 600cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 601cb8e7090SChristoph Hellwig } 602cb8e7090SChristoph Hellwig 603cb8e7090SChristoph Hellwig /* 604cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 605cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 606cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 607cb8e7090SChristoph Hellwig */ 60876dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 60976dda93cSYan, Zheng char *name, int namelen, 61072fd032eSSage Weil struct btrfs_root *snap_src, 611b83cc969SLi Zefan u64 *async_transid, bool readonly) 612cb8e7090SChristoph Hellwig { 61376dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 614cb8e7090SChristoph Hellwig struct dentry *dentry; 615cb8e7090SChristoph Hellwig int error; 616cb8e7090SChristoph Hellwig 61776dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 618cb8e7090SChristoph Hellwig 619cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 620cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 621cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 622cb8e7090SChristoph Hellwig goto out_unlock; 623cb8e7090SChristoph Hellwig 624cb8e7090SChristoph Hellwig error = -EEXIST; 625cb8e7090SChristoph Hellwig if (dentry->d_inode) 626cb8e7090SChristoph Hellwig goto out_dput; 627cb8e7090SChristoph Hellwig 628cb8e7090SChristoph Hellwig error = mnt_want_write(parent->mnt); 629cb8e7090SChristoph Hellwig if (error) 630cb8e7090SChristoph Hellwig goto out_dput; 631cb8e7090SChristoph Hellwig 63276dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 633cb8e7090SChristoph Hellwig if (error) 634cb8e7090SChristoph Hellwig goto out_drop_write; 635cb8e7090SChristoph Hellwig 63676dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 63776dda93cSYan, Zheng 63876dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 63976dda93cSYan, Zheng goto out_up_read; 64076dda93cSYan, Zheng 6413de4586cSChris Mason if (snap_src) { 64272fd032eSSage Weil error = create_snapshot(snap_src, dentry, 643b83cc969SLi Zefan name, namelen, async_transid, readonly); 6443de4586cSChris Mason } else { 64576dda93cSYan, Zheng error = create_subvol(BTRFS_I(dir)->root, dentry, 64672fd032eSSage Weil name, namelen, async_transid); 6473de4586cSChris Mason } 64876dda93cSYan, Zheng if (!error) 64976dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 65076dda93cSYan, Zheng out_up_read: 65176dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 652cb8e7090SChristoph Hellwig out_drop_write: 653cb8e7090SChristoph Hellwig mnt_drop_write(parent->mnt); 654cb8e7090SChristoph Hellwig out_dput: 655cb8e7090SChristoph Hellwig dput(dentry); 656cb8e7090SChristoph Hellwig out_unlock: 65776dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 658cb8e7090SChristoph Hellwig return error; 659cb8e7090SChristoph Hellwig } 660cb8e7090SChristoph Hellwig 6614cb5300bSChris Mason /* 6624cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 6634cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 6644cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 6654cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 6664cb5300bSChris Mason * part of the file 6674cb5300bSChris Mason */ 6684cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 6694cb5300bSChris Mason { 6704cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 6714cb5300bSChris Mason struct extent_map *em = NULL; 6724cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 6734cb5300bSChris Mason u64 end; 6744cb5300bSChris Mason 6754cb5300bSChris Mason read_lock(&em_tree->lock); 6764cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 6774cb5300bSChris Mason read_unlock(&em_tree->lock); 6784cb5300bSChris Mason 6794cb5300bSChris Mason if (em) { 6804cb5300bSChris Mason end = extent_map_end(em); 6814cb5300bSChris Mason free_extent_map(em); 6824cb5300bSChris Mason if (end - offset > thresh) 6834cb5300bSChris Mason return 0; 6844cb5300bSChris Mason } 6854cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 6864cb5300bSChris Mason thresh /= 2; 6874cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 6884cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 6894cb5300bSChris Mason if (end >= thresh) 6904cb5300bSChris Mason return 0; 6914cb5300bSChris Mason return 1; 6924cb5300bSChris Mason } 6934cb5300bSChris Mason 6944cb5300bSChris Mason /* 6954cb5300bSChris Mason * helper function to walk through a file and find extents 6964cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 6974cb5300bSChris Mason * 6984cb5300bSChris Mason * This is used by the defragging code to find new and small 6994cb5300bSChris Mason * extents 7004cb5300bSChris Mason */ 7014cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 7024cb5300bSChris Mason struct inode *inode, u64 newer_than, 7034cb5300bSChris Mason u64 *off, int thresh) 7044cb5300bSChris Mason { 7054cb5300bSChris Mason struct btrfs_path *path; 7064cb5300bSChris Mason struct btrfs_key min_key; 7074cb5300bSChris Mason struct btrfs_key max_key; 7084cb5300bSChris Mason struct extent_buffer *leaf; 7094cb5300bSChris Mason struct btrfs_file_extent_item *extent; 7104cb5300bSChris Mason int type; 7114cb5300bSChris Mason int ret; 712a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 7134cb5300bSChris Mason 7144cb5300bSChris Mason path = btrfs_alloc_path(); 7154cb5300bSChris Mason if (!path) 7164cb5300bSChris Mason return -ENOMEM; 7174cb5300bSChris Mason 718a4689d2bSDavid Sterba min_key.objectid = ino; 7194cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 7204cb5300bSChris Mason min_key.offset = *off; 7214cb5300bSChris Mason 722a4689d2bSDavid Sterba max_key.objectid = ino; 7234cb5300bSChris Mason max_key.type = (u8)-1; 7244cb5300bSChris Mason max_key.offset = (u64)-1; 7254cb5300bSChris Mason 7264cb5300bSChris Mason path->keep_locks = 1; 7274cb5300bSChris Mason 7284cb5300bSChris Mason while(1) { 7294cb5300bSChris Mason ret = btrfs_search_forward(root, &min_key, &max_key, 7304cb5300bSChris Mason path, 0, newer_than); 7314cb5300bSChris Mason if (ret != 0) 7324cb5300bSChris Mason goto none; 733a4689d2bSDavid Sterba if (min_key.objectid != ino) 7344cb5300bSChris Mason goto none; 7354cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 7364cb5300bSChris Mason goto none; 7374cb5300bSChris Mason 7384cb5300bSChris Mason leaf = path->nodes[0]; 7394cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 7404cb5300bSChris Mason struct btrfs_file_extent_item); 7414cb5300bSChris Mason 7424cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 7434cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 7444cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 7454cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 7464cb5300bSChris Mason *off = min_key.offset; 7474cb5300bSChris Mason btrfs_free_path(path); 7484cb5300bSChris Mason return 0; 7494cb5300bSChris Mason } 7504cb5300bSChris Mason 7514cb5300bSChris Mason if (min_key.offset == (u64)-1) 7524cb5300bSChris Mason goto none; 7534cb5300bSChris Mason 7544cb5300bSChris Mason min_key.offset++; 7554cb5300bSChris Mason btrfs_release_path(path); 7564cb5300bSChris Mason } 7574cb5300bSChris Mason none: 7584cb5300bSChris Mason btrfs_free_path(path); 7594cb5300bSChris Mason return -ENOENT; 7604cb5300bSChris Mason } 7614cb5300bSChris Mason 762940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len, 7631e701a32SChris Mason int thresh, u64 *last_len, u64 *skip, 7641e701a32SChris Mason u64 *defrag_end) 765940100a4SChris Mason { 766940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 767940100a4SChris Mason struct extent_map *em = NULL; 768940100a4SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 769940100a4SChris Mason int ret = 1; 770940100a4SChris Mason 771940100a4SChris Mason /* 772008873eaSLi Zefan * make sure that once we start defragging an extent, we keep on 773940100a4SChris Mason * defragging it 774940100a4SChris Mason */ 775940100a4SChris Mason if (start < *defrag_end) 776940100a4SChris Mason return 1; 777940100a4SChris Mason 778940100a4SChris Mason *skip = 0; 779940100a4SChris Mason 780940100a4SChris Mason /* 781940100a4SChris Mason * hopefully we have this extent in the tree already, try without 782940100a4SChris Mason * the full extent lock 783940100a4SChris Mason */ 784940100a4SChris Mason read_lock(&em_tree->lock); 785940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 786940100a4SChris Mason read_unlock(&em_tree->lock); 787940100a4SChris Mason 788940100a4SChris Mason if (!em) { 789940100a4SChris Mason /* get the big lock and read metadata off disk */ 790940100a4SChris Mason lock_extent(io_tree, start, start + len - 1, GFP_NOFS); 791940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 792940100a4SChris Mason unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); 793940100a4SChris Mason 7946cf8bfbfSDan Carpenter if (IS_ERR(em)) 795940100a4SChris Mason return 0; 796940100a4SChris Mason } 797940100a4SChris Mason 798940100a4SChris Mason /* this will cover holes, and inline extents */ 799940100a4SChris Mason if (em->block_start >= EXTENT_MAP_LAST_BYTE) 800940100a4SChris Mason ret = 0; 801940100a4SChris Mason 802940100a4SChris Mason /* 803940100a4SChris Mason * we hit a real extent, if it is big don't bother defragging it again 804940100a4SChris Mason */ 8051e701a32SChris Mason if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) 806940100a4SChris Mason ret = 0; 807940100a4SChris Mason 808940100a4SChris Mason /* 809940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 810940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 811940100a4SChris Mason * so that the next tiny extent will force a defrag. 812940100a4SChris Mason * 813940100a4SChris Mason * The end result of this is that tiny extents before a single big 814940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 815940100a4SChris Mason */ 816940100a4SChris Mason if (ret) { 817940100a4SChris Mason *defrag_end = extent_map_end(em); 818940100a4SChris Mason } else { 819940100a4SChris Mason *last_len = 0; 820940100a4SChris Mason *skip = extent_map_end(em); 821940100a4SChris Mason *defrag_end = 0; 822940100a4SChris Mason } 823940100a4SChris Mason 824940100a4SChris Mason free_extent_map(em); 825940100a4SChris Mason return ret; 826940100a4SChris Mason } 827940100a4SChris Mason 8284cb5300bSChris Mason /* 8294cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 8304cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 8314cb5300bSChris Mason * to COW and defrag. 8324cb5300bSChris Mason * 8334cb5300bSChris Mason * It also makes sure the delalloc code has enough 8344cb5300bSChris Mason * dirty data to avoid making new small extents as part 8354cb5300bSChris Mason * of the defrag 8364cb5300bSChris Mason * 8374cb5300bSChris Mason * It's a good idea to start RA on this range 8384cb5300bSChris Mason * before calling this. 8394cb5300bSChris Mason */ 8404cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 8414cb5300bSChris Mason struct page **pages, 8424cb5300bSChris Mason unsigned long start_index, 8434cb5300bSChris Mason int num_pages) 844f46b5a66SChristoph Hellwig { 8454cb5300bSChris Mason unsigned long file_end; 8464cb5300bSChris Mason u64 isize = i_size_read(inode); 847f46b5a66SChristoph Hellwig u64 page_start; 848f46b5a66SChristoph Hellwig u64 page_end; 8494cb5300bSChris Mason int ret; 8504cb5300bSChris Mason int i; 8514cb5300bSChris Mason int i_done; 8524cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 8534cb5300bSChris Mason struct extent_state *cached_state = NULL; 8543b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 8554cb5300bSChris Mason 8564cb5300bSChris Mason if (isize == 0) 8574cb5300bSChris Mason return 0; 8584cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 8594cb5300bSChris Mason 8604cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 8614cb5300bSChris Mason num_pages << PAGE_CACHE_SHIFT); 8624cb5300bSChris Mason if (ret) 8634cb5300bSChris Mason return ret; 8644cb5300bSChris Mason again: 8654cb5300bSChris Mason ret = 0; 8664cb5300bSChris Mason i_done = 0; 8674cb5300bSChris Mason 8684cb5300bSChris Mason /* step one, lock all the pages */ 8694cb5300bSChris Mason for (i = 0; i < num_pages; i++) { 8704cb5300bSChris Mason struct page *page; 871a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 8723b16a4e3SJosef Bacik start_index + i, mask); 8734cb5300bSChris Mason if (!page) 8744cb5300bSChris Mason break; 8754cb5300bSChris Mason 8764cb5300bSChris Mason if (!PageUptodate(page)) { 8774cb5300bSChris Mason btrfs_readpage(NULL, page); 8784cb5300bSChris Mason lock_page(page); 8794cb5300bSChris Mason if (!PageUptodate(page)) { 8804cb5300bSChris Mason unlock_page(page); 8814cb5300bSChris Mason page_cache_release(page); 8824cb5300bSChris Mason ret = -EIO; 8834cb5300bSChris Mason break; 8844cb5300bSChris Mason } 8854cb5300bSChris Mason } 8864cb5300bSChris Mason isize = i_size_read(inode); 8874cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 8884cb5300bSChris Mason if (!isize || page->index > file_end || 8894cb5300bSChris Mason page->mapping != inode->i_mapping) { 8904cb5300bSChris Mason /* whoops, we blew past eof, skip this page */ 8914cb5300bSChris Mason unlock_page(page); 8924cb5300bSChris Mason page_cache_release(page); 8934cb5300bSChris Mason break; 8944cb5300bSChris Mason } 8954cb5300bSChris Mason pages[i] = page; 8964cb5300bSChris Mason i_done++; 8974cb5300bSChris Mason } 8984cb5300bSChris Mason if (!i_done || ret) 8994cb5300bSChris Mason goto out; 9004cb5300bSChris Mason 9014cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 9024cb5300bSChris Mason goto out; 9034cb5300bSChris Mason 9044cb5300bSChris Mason /* 9054cb5300bSChris Mason * so now we have a nice long stream of locked 9064cb5300bSChris Mason * and up to date pages, lets wait on them 9074cb5300bSChris Mason */ 9084cb5300bSChris Mason for (i = 0; i < i_done; i++) 9094cb5300bSChris Mason wait_on_page_writeback(pages[i]); 9104cb5300bSChris Mason 9114cb5300bSChris Mason page_start = page_offset(pages[0]); 9124cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 9134cb5300bSChris Mason 9144cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 9154cb5300bSChris Mason page_start, page_end - 1, 0, &cached_state, 9164cb5300bSChris Mason GFP_NOFS); 9174cb5300bSChris Mason ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1); 9184cb5300bSChris Mason if (ordered && 9194cb5300bSChris Mason ordered->file_offset + ordered->len > page_start && 9204cb5300bSChris Mason ordered->file_offset < page_end) { 9214cb5300bSChris Mason btrfs_put_ordered_extent(ordered); 9224cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 9234cb5300bSChris Mason page_start, page_end - 1, 9244cb5300bSChris Mason &cached_state, GFP_NOFS); 9254cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9264cb5300bSChris Mason unlock_page(pages[i]); 9274cb5300bSChris Mason page_cache_release(pages[i]); 9284cb5300bSChris Mason } 9294cb5300bSChris Mason btrfs_wait_ordered_range(inode, page_start, 9304cb5300bSChris Mason page_end - page_start); 9314cb5300bSChris Mason goto again; 9324cb5300bSChris Mason } 9334cb5300bSChris Mason if (ordered) 9344cb5300bSChris Mason btrfs_put_ordered_extent(ordered); 9354cb5300bSChris Mason 9364cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 9374cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 9384cb5300bSChris Mason EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, 9394cb5300bSChris Mason GFP_NOFS); 9404cb5300bSChris Mason 9414cb5300bSChris Mason if (i_done != num_pages) { 9429e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 9439e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 9449e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 9454cb5300bSChris Mason btrfs_delalloc_release_space(inode, 9464cb5300bSChris Mason (num_pages - i_done) << PAGE_CACHE_SHIFT); 9474cb5300bSChris Mason } 9484cb5300bSChris Mason 9494cb5300bSChris Mason 9504cb5300bSChris Mason btrfs_set_extent_delalloc(inode, page_start, page_end - 1, 9514cb5300bSChris Mason &cached_state); 9524cb5300bSChris Mason 9534cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 9544cb5300bSChris Mason page_start, page_end - 1, &cached_state, 9554cb5300bSChris Mason GFP_NOFS); 9564cb5300bSChris Mason 9574cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9584cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 9594cb5300bSChris Mason ClearPageChecked(pages[i]); 9604cb5300bSChris Mason set_page_extent_mapped(pages[i]); 9614cb5300bSChris Mason set_page_dirty(pages[i]); 9624cb5300bSChris Mason unlock_page(pages[i]); 9634cb5300bSChris Mason page_cache_release(pages[i]); 9644cb5300bSChris Mason } 9654cb5300bSChris Mason return i_done; 9664cb5300bSChris Mason out: 9674cb5300bSChris Mason for (i = 0; i < i_done; i++) { 9684cb5300bSChris Mason unlock_page(pages[i]); 9694cb5300bSChris Mason page_cache_release(pages[i]); 9704cb5300bSChris Mason } 9714cb5300bSChris Mason btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT); 9724cb5300bSChris Mason return ret; 9734cb5300bSChris Mason 9744cb5300bSChris Mason } 9754cb5300bSChris Mason 9764cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 9774cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 9784cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 9794cb5300bSChris Mason { 9804cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 9814cb5300bSChris Mason struct btrfs_super_block *disk_super; 9824cb5300bSChris Mason struct file_ra_state *ra = NULL; 9834cb5300bSChris Mason unsigned long last_index; 984151a31b2SLi Zefan u64 isize = i_size_read(inode); 9854cb5300bSChris Mason u64 features; 986940100a4SChris Mason u64 last_len = 0; 987940100a4SChris Mason u64 skip = 0; 988940100a4SChris Mason u64 defrag_end = 0; 9894cb5300bSChris Mason u64 newer_off = range->start; 990f46b5a66SChristoph Hellwig unsigned long i; 991008873eaSLi Zefan unsigned long ra_index = 0; 992f46b5a66SChristoph Hellwig int ret; 9934cb5300bSChris Mason int defrag_count = 0; 9941a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 9954cb5300bSChris Mason int extent_thresh = range->extent_thresh; 996008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 997008873eaSLi Zefan int cluster = max_cluster; 9984cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 9994cb5300bSChris Mason struct page **pages = NULL; 10004cb5300bSChris Mason 10014cb5300bSChris Mason if (extent_thresh == 0) 10024cb5300bSChris Mason extent_thresh = 256 * 1024; 10031a419d85SLi Zefan 10041a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 10051a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 10061a419d85SLi Zefan return -EINVAL; 10071a419d85SLi Zefan if (range->compress_type) 10081a419d85SLi Zefan compress_type = range->compress_type; 10091a419d85SLi Zefan } 1010f46b5a66SChristoph Hellwig 1011151a31b2SLi Zefan if (isize == 0) 1012940100a4SChris Mason return 0; 1013f46b5a66SChristoph Hellwig 10144cb5300bSChris Mason /* 10154cb5300bSChris Mason * if we were not given a file, allocate a readahead 10164cb5300bSChris Mason * context 10174cb5300bSChris Mason */ 10184cb5300bSChris Mason if (!file) { 10194cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 10204cb5300bSChris Mason if (!ra) 10214cb5300bSChris Mason return -ENOMEM; 10224cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 10234cb5300bSChris Mason } else { 10244cb5300bSChris Mason ra = &file->f_ra; 10254cb5300bSChris Mason } 10264cb5300bSChris Mason 1027008873eaSLi Zefan pages = kmalloc(sizeof(struct page *) * max_cluster, 10284cb5300bSChris Mason GFP_NOFS); 10294cb5300bSChris Mason if (!pages) { 10304cb5300bSChris Mason ret = -ENOMEM; 10314cb5300bSChris Mason goto out_ra; 10324cb5300bSChris Mason } 10334cb5300bSChris Mason 10344cb5300bSChris Mason /* find the last page to defrag */ 10351e701a32SChris Mason if (range->start + range->len > range->start) { 1036151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 10371e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 10381e701a32SChris Mason } else { 1039151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 10401e701a32SChris Mason } 10411e701a32SChris Mason 10424cb5300bSChris Mason if (newer_than) { 10434cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 10444cb5300bSChris Mason &newer_off, 64 * 1024); 10454cb5300bSChris Mason if (!ret) { 10464cb5300bSChris Mason range->start = newer_off; 10474cb5300bSChris Mason /* 10484cb5300bSChris Mason * we always align our defrag to help keep 10494cb5300bSChris Mason * the extents in the file evenly spaced 10504cb5300bSChris Mason */ 10514cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 10524cb5300bSChris Mason } else 10534cb5300bSChris Mason goto out_ra; 10544cb5300bSChris Mason } else { 10551e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 10564cb5300bSChris Mason } 10574cb5300bSChris Mason if (!max_to_defrag) 10585ca49660SLi Zefan max_to_defrag = last_index; 10594cb5300bSChris Mason 10604cb5300bSChris Mason while (i <= last_index && defrag_count < max_to_defrag) { 10614cb5300bSChris Mason /* 10624cb5300bSChris Mason * make sure we stop running if someone unmounts 10634cb5300bSChris Mason * the FS 10644cb5300bSChris Mason */ 10654cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 10664cb5300bSChris Mason break; 10674cb5300bSChris Mason 10684cb5300bSChris Mason if (!newer_than && 10694cb5300bSChris Mason !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 10701e701a32SChris Mason PAGE_CACHE_SIZE, 10714cb5300bSChris Mason extent_thresh, 10721e701a32SChris Mason &last_len, &skip, 1073940100a4SChris Mason &defrag_end)) { 1074940100a4SChris Mason unsigned long next; 1075940100a4SChris Mason /* 1076940100a4SChris Mason * the should_defrag function tells us how much to skip 1077940100a4SChris Mason * bump our counter by the suggested amount 1078940100a4SChris Mason */ 1079940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1080940100a4SChris Mason i = max(i + 1, next); 1081940100a4SChris Mason continue; 1082940100a4SChris Mason } 1083008873eaSLi Zefan 1084008873eaSLi Zefan if (!newer_than) { 1085008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1086008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1087008873eaSLi Zefan cluster = min(cluster, max_cluster); 1088008873eaSLi Zefan } else { 1089008873eaSLi Zefan cluster = max_cluster; 1090008873eaSLi Zefan } 1091008873eaSLi Zefan 10921e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 10931a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 1094940100a4SChris Mason 1095008873eaSLi Zefan if (i + cluster > ra_index) { 1096008873eaSLi Zefan ra_index = max(i, ra_index); 1097008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1098008873eaSLi Zefan cluster); 1099008873eaSLi Zefan ra_index += max_cluster; 1100008873eaSLi Zefan } 11014cb5300bSChris Mason 1102008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 11034cb5300bSChris Mason if (ret < 0) 11044cb5300bSChris Mason goto out_ra; 11054cb5300bSChris Mason 11064cb5300bSChris Mason defrag_count += ret; 11074cb5300bSChris Mason balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); 11084cb5300bSChris Mason 11094cb5300bSChris Mason if (newer_than) { 11104cb5300bSChris Mason if (newer_off == (u64)-1) 11114cb5300bSChris Mason break; 11124cb5300bSChris Mason 11134cb5300bSChris Mason newer_off = max(newer_off + 1, 11144cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 11154cb5300bSChris Mason 11164cb5300bSChris Mason ret = find_new_extents(root, inode, 11174cb5300bSChris Mason newer_than, &newer_off, 11184cb5300bSChris Mason 64 * 1024); 11194cb5300bSChris Mason if (!ret) { 11204cb5300bSChris Mason range->start = newer_off; 11214cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 11224cb5300bSChris Mason } else { 11234cb5300bSChris Mason break; 1124940100a4SChris Mason } 11254cb5300bSChris Mason } else { 1126008873eaSLi Zefan if (ret > 0) { 1127cbcc8326SLi Zefan i += ret; 1128008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1129008873eaSLi Zefan } else { 1130940100a4SChris Mason i++; 1131008873eaSLi Zefan last_len = 0; 1132008873eaSLi Zefan } 1133f46b5a66SChristoph Hellwig } 11344cb5300bSChris Mason } 1135f46b5a66SChristoph Hellwig 11361e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 11371e701a32SChris Mason filemap_flush(inode->i_mapping); 11381e701a32SChris Mason 11391e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 11401e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 11411e701a32SChris Mason * we have to make sure the IO is actually started and that 11421e701a32SChris Mason * ordered extents get created before we return 11431e701a32SChris Mason */ 11441e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 11451e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 11461e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 11471e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 11481e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 11491e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 11501e701a32SChris Mason } 11511e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 11521e701a32SChris Mason 11531e701a32SChris Mason mutex_lock(&inode->i_mutex); 1154261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 11551e701a32SChris Mason mutex_unlock(&inode->i_mutex); 11561e701a32SChris Mason } 11571e701a32SChris Mason 11581a419d85SLi Zefan disk_super = &root->fs_info->super_copy; 11591a419d85SLi Zefan features = btrfs_super_incompat_flags(disk_super); 11601a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 11611a419d85SLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 11621a419d85SLi Zefan btrfs_set_super_incompat_flags(disk_super, features); 11631a419d85SLi Zefan } 11641a419d85SLi Zefan 116560ccf82fSDiego Calleja ret = defrag_count; 1166940100a4SChris Mason 11674cb5300bSChris Mason out_ra: 11684cb5300bSChris Mason if (!file) 11694cb5300bSChris Mason kfree(ra); 11704cb5300bSChris Mason kfree(pages); 1171940100a4SChris Mason return ret; 1172f46b5a66SChristoph Hellwig } 1173f46b5a66SChristoph Hellwig 117476dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root, 117576dda93cSYan, Zheng void __user *arg) 1176f46b5a66SChristoph Hellwig { 1177f46b5a66SChristoph Hellwig u64 new_size; 1178f46b5a66SChristoph Hellwig u64 old_size; 1179f46b5a66SChristoph Hellwig u64 devid = 1; 1180f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1181f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1182f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1183f46b5a66SChristoph Hellwig char *sizestr; 1184f46b5a66SChristoph Hellwig char *devstr = NULL; 1185f46b5a66SChristoph Hellwig int ret = 0; 1186f46b5a66SChristoph Hellwig int mod = 0; 1187f46b5a66SChristoph Hellwig 1188c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1189c146afadSYan Zheng return -EROFS; 1190c146afadSYan Zheng 1191e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1192e441d54dSChris Mason return -EPERM; 1193e441d54dSChris Mason 1194dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1195dae7b665SLi Zefan if (IS_ERR(vol_args)) 1196dae7b665SLi Zefan return PTR_ERR(vol_args); 11975516e595SMark Fasheh 11985516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1199f46b5a66SChristoph Hellwig 12007d9eb12cSChris Mason mutex_lock(&root->fs_info->volume_mutex); 1201f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1202f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1203f46b5a66SChristoph Hellwig if (devstr) { 1204f46b5a66SChristoph Hellwig char *end; 1205f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1206f46b5a66SChristoph Hellwig *devstr = '\0'; 1207f46b5a66SChristoph Hellwig devstr = vol_args->name; 1208f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 120921380931SJoel Becker printk(KERN_INFO "resizing devid %llu\n", 121021380931SJoel Becker (unsigned long long)devid); 1211f46b5a66SChristoph Hellwig } 12122b82032cSYan Zheng device = btrfs_find_device(root, devid, NULL, NULL); 1213f46b5a66SChristoph Hellwig if (!device) { 121421380931SJoel Becker printk(KERN_INFO "resizer unable to find device %llu\n", 121521380931SJoel Becker (unsigned long long)devid); 1216f46b5a66SChristoph Hellwig ret = -EINVAL; 1217f46b5a66SChristoph Hellwig goto out_unlock; 1218f46b5a66SChristoph Hellwig } 1219f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1220f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1221f46b5a66SChristoph Hellwig else { 1222f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1223f46b5a66SChristoph Hellwig mod = -1; 1224f46b5a66SChristoph Hellwig sizestr++; 1225f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1226f46b5a66SChristoph Hellwig mod = 1; 1227f46b5a66SChristoph Hellwig sizestr++; 1228f46b5a66SChristoph Hellwig } 122991748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1230f46b5a66SChristoph Hellwig if (new_size == 0) { 1231f46b5a66SChristoph Hellwig ret = -EINVAL; 1232f46b5a66SChristoph Hellwig goto out_unlock; 1233f46b5a66SChristoph Hellwig } 1234f46b5a66SChristoph Hellwig } 1235f46b5a66SChristoph Hellwig 1236f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1237f46b5a66SChristoph Hellwig 1238f46b5a66SChristoph Hellwig if (mod < 0) { 1239f46b5a66SChristoph Hellwig if (new_size > old_size) { 1240f46b5a66SChristoph Hellwig ret = -EINVAL; 1241f46b5a66SChristoph Hellwig goto out_unlock; 1242f46b5a66SChristoph Hellwig } 1243f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1244f46b5a66SChristoph Hellwig } else if (mod > 0) { 1245f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1246f46b5a66SChristoph Hellwig } 1247f46b5a66SChristoph Hellwig 1248f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1249f46b5a66SChristoph Hellwig ret = -EINVAL; 1250f46b5a66SChristoph Hellwig goto out_unlock; 1251f46b5a66SChristoph Hellwig } 1252f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1253f46b5a66SChristoph Hellwig ret = -EFBIG; 1254f46b5a66SChristoph Hellwig goto out_unlock; 1255f46b5a66SChristoph Hellwig } 1256f46b5a66SChristoph Hellwig 1257f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1258f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1259f46b5a66SChristoph Hellwig 1260f46b5a66SChristoph Hellwig printk(KERN_INFO "new size for %s is %llu\n", 1261f46b5a66SChristoph Hellwig device->name, (unsigned long long)new_size); 1262f46b5a66SChristoph Hellwig 1263f46b5a66SChristoph Hellwig if (new_size > old_size) { 1264a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 126598d5dc13STsutomu Itoh if (IS_ERR(trans)) { 126698d5dc13STsutomu Itoh ret = PTR_ERR(trans); 126798d5dc13STsutomu Itoh goto out_unlock; 126898d5dc13STsutomu Itoh } 1269f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1270f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1271f46b5a66SChristoph Hellwig } else { 1272f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 1273f46b5a66SChristoph Hellwig } 1274f46b5a66SChristoph Hellwig 1275f46b5a66SChristoph Hellwig out_unlock: 12767d9eb12cSChris Mason mutex_unlock(&root->fs_info->volume_mutex); 1277f46b5a66SChristoph Hellwig kfree(vol_args); 1278f46b5a66SChristoph Hellwig return ret; 1279f46b5a66SChristoph Hellwig } 1280f46b5a66SChristoph Hellwig 128172fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 128272fd032eSSage Weil char *name, 128372fd032eSSage Weil unsigned long fd, 128472fd032eSSage Weil int subvol, 1285b83cc969SLi Zefan u64 *transid, 1286b83cc969SLi Zefan bool readonly) 1287f46b5a66SChristoph Hellwig { 1288cb8e7090SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 12893de4586cSChris Mason struct file *src_file; 1290f46b5a66SChristoph Hellwig int namelen; 12913de4586cSChris Mason int ret = 0; 1292f46b5a66SChristoph Hellwig 1293c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1294c146afadSYan Zheng return -EROFS; 1295c146afadSYan Zheng 129672fd032eSSage Weil namelen = strlen(name); 129772fd032eSSage Weil if (strchr(name, '/')) { 1298f46b5a66SChristoph Hellwig ret = -EINVAL; 1299f46b5a66SChristoph Hellwig goto out; 1300f46b5a66SChristoph Hellwig } 1301f46b5a66SChristoph Hellwig 13023de4586cSChris Mason if (subvol) { 130372fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 1304b83cc969SLi Zefan NULL, transid, readonly); 1305cb8e7090SChristoph Hellwig } else { 13063de4586cSChris Mason struct inode *src_inode; 130772fd032eSSage Weil src_file = fget(fd); 13083de4586cSChris Mason if (!src_file) { 13093de4586cSChris Mason ret = -EINVAL; 13103de4586cSChris Mason goto out; 13113de4586cSChris Mason } 13123de4586cSChris Mason 13133de4586cSChris Mason src_inode = src_file->f_path.dentry->d_inode; 13143de4586cSChris Mason if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { 1315d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1316d397712bSChris Mason "another FS\n"); 13173de4586cSChris Mason ret = -EINVAL; 13183de4586cSChris Mason fput(src_file); 13193de4586cSChris Mason goto out; 13203de4586cSChris Mason } 132172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 132272fd032eSSage Weil BTRFS_I(src_inode)->root, 1323b83cc969SLi Zefan transid, readonly); 13243de4586cSChris Mason fput(src_file); 1325cb8e7090SChristoph Hellwig } 1326f46b5a66SChristoph Hellwig out: 132772fd032eSSage Weil return ret; 132872fd032eSSage Weil } 132972fd032eSSage Weil 133072fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1331fa0d2b9bSLi Zefan void __user *arg, int subvol) 133272fd032eSSage Weil { 1333fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 133472fd032eSSage Weil int ret; 133572fd032eSSage Weil 1336fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1337fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1338fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1339fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1340fa0d2b9bSLi Zefan 1341fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1342b83cc969SLi Zefan vol_args->fd, subvol, 1343b83cc969SLi Zefan NULL, false); 1344fa0d2b9bSLi Zefan 1345fa0d2b9bSLi Zefan kfree(vol_args); 1346fa0d2b9bSLi Zefan return ret; 1347fa0d2b9bSLi Zefan } 1348fa0d2b9bSLi Zefan 1349fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1350fa0d2b9bSLi Zefan void __user *arg, int subvol) 1351fa0d2b9bSLi Zefan { 1352fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1353fa0d2b9bSLi Zefan int ret; 1354fdfb1e4fSLi Zefan u64 transid = 0; 1355fdfb1e4fSLi Zefan u64 *ptr = NULL; 1356b83cc969SLi Zefan bool readonly = false; 135772fd032eSSage Weil 1358fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1359fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1360fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1361fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1362fdfb1e4fSLi Zefan 1363b83cc969SLi Zefan if (vol_args->flags & 1364b83cc969SLi Zefan ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { 1365b83cc969SLi Zefan ret = -EOPNOTSUPP; 1366fdfb1e4fSLi Zefan goto out; 1367fdfb1e4fSLi Zefan } 1368fdfb1e4fSLi Zefan 1369fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1370fdfb1e4fSLi Zefan ptr = &transid; 1371b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1372b83cc969SLi Zefan readonly = true; 137375eaa0e2SSage Weil 1374fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1375b83cc969SLi Zefan vol_args->fd, subvol, 1376b83cc969SLi Zefan ptr, readonly); 137775eaa0e2SSage Weil 1378fdfb1e4fSLi Zefan if (ret == 0 && ptr && 137975eaa0e2SSage Weil copy_to_user(arg + 1380fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1381fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 138275eaa0e2SSage Weil ret = -EFAULT; 1383fdfb1e4fSLi Zefan out: 1384f46b5a66SChristoph Hellwig kfree(vol_args); 1385f46b5a66SChristoph Hellwig return ret; 1386f46b5a66SChristoph Hellwig } 1387f46b5a66SChristoph Hellwig 13880caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 13890caa102dSLi Zefan void __user *arg) 13900caa102dSLi Zefan { 13910caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 13920caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 13930caa102dSLi Zefan int ret = 0; 13940caa102dSLi Zefan u64 flags = 0; 13950caa102dSLi Zefan 139633345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 13970caa102dSLi Zefan return -EINVAL; 13980caa102dSLi Zefan 13990caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 14000caa102dSLi Zefan if (btrfs_root_readonly(root)) 14010caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 14020caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 14030caa102dSLi Zefan 14040caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 14050caa102dSLi Zefan ret = -EFAULT; 14060caa102dSLi Zefan 14070caa102dSLi Zefan return ret; 14080caa102dSLi Zefan } 14090caa102dSLi Zefan 14100caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 14110caa102dSLi Zefan void __user *arg) 14120caa102dSLi Zefan { 14130caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 14140caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 14150caa102dSLi Zefan struct btrfs_trans_handle *trans; 14160caa102dSLi Zefan u64 root_flags; 14170caa102dSLi Zefan u64 flags; 14180caa102dSLi Zefan int ret = 0; 14190caa102dSLi Zefan 14200caa102dSLi Zefan if (root->fs_info->sb->s_flags & MS_RDONLY) 14210caa102dSLi Zefan return -EROFS; 14220caa102dSLi Zefan 142333345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 14240caa102dSLi Zefan return -EINVAL; 14250caa102dSLi Zefan 14260caa102dSLi Zefan if (copy_from_user(&flags, arg, sizeof(flags))) 14270caa102dSLi Zefan return -EFAULT; 14280caa102dSLi Zefan 1429b4dc2b8cSLi Zefan if (flags & BTRFS_SUBVOL_CREATE_ASYNC) 14300caa102dSLi Zefan return -EINVAL; 14310caa102dSLi Zefan 14320caa102dSLi Zefan if (flags & ~BTRFS_SUBVOL_RDONLY) 14330caa102dSLi Zefan return -EOPNOTSUPP; 14340caa102dSLi Zefan 14352e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1436b4dc2b8cSLi Zefan return -EACCES; 1437b4dc2b8cSLi Zefan 14380caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 14390caa102dSLi Zefan 14400caa102dSLi Zefan /* nothing to do */ 14410caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 14420caa102dSLi Zefan goto out; 14430caa102dSLi Zefan 14440caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 14450caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 14460caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 14470caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 14480caa102dSLi Zefan else 14490caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 14500caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 14510caa102dSLi Zefan 14520caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 14530caa102dSLi Zefan if (IS_ERR(trans)) { 14540caa102dSLi Zefan ret = PTR_ERR(trans); 14550caa102dSLi Zefan goto out_reset; 14560caa102dSLi Zefan } 14570caa102dSLi Zefan 1458b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 14590caa102dSLi Zefan &root->root_key, &root->root_item); 14600caa102dSLi Zefan 14610caa102dSLi Zefan btrfs_commit_transaction(trans, root); 14620caa102dSLi Zefan out_reset: 14630caa102dSLi Zefan if (ret) 14640caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 14650caa102dSLi Zefan out: 14660caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 14670caa102dSLi Zefan return ret; 14680caa102dSLi Zefan } 14690caa102dSLi Zefan 147076dda93cSYan, Zheng /* 147176dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 147276dda93cSYan, Zheng */ 147376dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 147476dda93cSYan, Zheng { 147576dda93cSYan, Zheng struct btrfs_path *path; 147676dda93cSYan, Zheng struct btrfs_key key; 147776dda93cSYan, Zheng int ret; 147876dda93cSYan, Zheng 147976dda93cSYan, Zheng path = btrfs_alloc_path(); 148076dda93cSYan, Zheng if (!path) 148176dda93cSYan, Zheng return -ENOMEM; 148276dda93cSYan, Zheng 148376dda93cSYan, Zheng key.objectid = root->root_key.objectid; 148476dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 148576dda93cSYan, Zheng key.offset = (u64)-1; 148676dda93cSYan, Zheng 148776dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 148876dda93cSYan, Zheng &key, path, 0, 0); 148976dda93cSYan, Zheng if (ret < 0) 149076dda93cSYan, Zheng goto out; 149176dda93cSYan, Zheng BUG_ON(ret == 0); 149276dda93cSYan, Zheng 149376dda93cSYan, Zheng ret = 0; 149476dda93cSYan, Zheng if (path->slots[0] > 0) { 149576dda93cSYan, Zheng path->slots[0]--; 149676dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 149776dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 149876dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 149976dda93cSYan, Zheng ret = -ENOTEMPTY; 150076dda93cSYan, Zheng } 150176dda93cSYan, Zheng out: 150276dda93cSYan, Zheng btrfs_free_path(path); 150376dda93cSYan, Zheng return ret; 150476dda93cSYan, Zheng } 150576dda93cSYan, Zheng 1506ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1507ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1508ac8e9819SChris Mason { 1509abc6e134SChris Mason struct btrfs_key test; 1510abc6e134SChris Mason int ret; 1511abc6e134SChris Mason 1512abc6e134SChris Mason test.objectid = sk->min_objectid; 1513abc6e134SChris Mason test.type = sk->min_type; 1514abc6e134SChris Mason test.offset = sk->min_offset; 1515abc6e134SChris Mason 1516abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1517abc6e134SChris Mason if (ret < 0) 1518ac8e9819SChris Mason return 0; 1519abc6e134SChris Mason 1520abc6e134SChris Mason test.objectid = sk->max_objectid; 1521abc6e134SChris Mason test.type = sk->max_type; 1522abc6e134SChris Mason test.offset = sk->max_offset; 1523abc6e134SChris Mason 1524abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1525abc6e134SChris Mason if (ret > 0) 1526ac8e9819SChris Mason return 0; 1527ac8e9819SChris Mason return 1; 1528ac8e9819SChris Mason } 1529ac8e9819SChris Mason 1530ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1531ac8e9819SChris Mason struct btrfs_path *path, 1532ac8e9819SChris Mason struct btrfs_key *key, 1533ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1534ac8e9819SChris Mason char *buf, 1535ac8e9819SChris Mason unsigned long *sk_offset, 1536ac8e9819SChris Mason int *num_found) 1537ac8e9819SChris Mason { 1538ac8e9819SChris Mason u64 found_transid; 1539ac8e9819SChris Mason struct extent_buffer *leaf; 1540ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1541ac8e9819SChris Mason unsigned long item_off; 1542ac8e9819SChris Mason unsigned long item_len; 1543ac8e9819SChris Mason int nritems; 1544ac8e9819SChris Mason int i; 1545ac8e9819SChris Mason int slot; 1546ac8e9819SChris Mason int ret = 0; 1547ac8e9819SChris Mason 1548ac8e9819SChris Mason leaf = path->nodes[0]; 1549ac8e9819SChris Mason slot = path->slots[0]; 1550ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1551ac8e9819SChris Mason 1552ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1553ac8e9819SChris Mason i = nritems; 1554ac8e9819SChris Mason goto advance_key; 1555ac8e9819SChris Mason } 1556ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1557ac8e9819SChris Mason 1558ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1559ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1560ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1561ac8e9819SChris Mason 1562ac8e9819SChris Mason if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1563ac8e9819SChris Mason item_len = 0; 1564ac8e9819SChris Mason 1565ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1566ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1567ac8e9819SChris Mason ret = 1; 1568ac8e9819SChris Mason goto overflow; 1569ac8e9819SChris Mason } 1570ac8e9819SChris Mason 1571ac8e9819SChris Mason btrfs_item_key_to_cpu(leaf, key, i); 1572ac8e9819SChris Mason if (!key_in_sk(key, sk)) 1573ac8e9819SChris Mason continue; 1574ac8e9819SChris Mason 1575ac8e9819SChris Mason sh.objectid = key->objectid; 1576ac8e9819SChris Mason sh.offset = key->offset; 1577ac8e9819SChris Mason sh.type = key->type; 1578ac8e9819SChris Mason sh.len = item_len; 1579ac8e9819SChris Mason sh.transid = found_transid; 1580ac8e9819SChris Mason 1581ac8e9819SChris Mason /* copy search result header */ 1582ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1583ac8e9819SChris Mason *sk_offset += sizeof(sh); 1584ac8e9819SChris Mason 1585ac8e9819SChris Mason if (item_len) { 1586ac8e9819SChris Mason char *p = buf + *sk_offset; 1587ac8e9819SChris Mason /* copy the item */ 1588ac8e9819SChris Mason read_extent_buffer(leaf, p, 1589ac8e9819SChris Mason item_off, item_len); 1590ac8e9819SChris Mason *sk_offset += item_len; 1591ac8e9819SChris Mason } 1592e2156867SHugo Mills (*num_found)++; 1593ac8e9819SChris Mason 1594ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1595ac8e9819SChris Mason break; 1596ac8e9819SChris Mason } 1597ac8e9819SChris Mason advance_key: 1598ac8e9819SChris Mason ret = 0; 1599abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1600abc6e134SChris Mason key->offset++; 1601abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1602abc6e134SChris Mason key->offset = 0; 1603abc6e134SChris Mason key->type++; 1604abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1605abc6e134SChris Mason key->offset = 0; 1606abc6e134SChris Mason key->type = 0; 1607abc6e134SChris Mason key->objectid++; 1608abc6e134SChris Mason } else 1609abc6e134SChris Mason ret = 1; 1610ac8e9819SChris Mason overflow: 1611ac8e9819SChris Mason return ret; 1612ac8e9819SChris Mason } 1613ac8e9819SChris Mason 1614ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1615ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1616ac8e9819SChris Mason { 1617ac8e9819SChris Mason struct btrfs_root *root; 1618ac8e9819SChris Mason struct btrfs_key key; 1619ac8e9819SChris Mason struct btrfs_key max_key; 1620ac8e9819SChris Mason struct btrfs_path *path; 1621ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1622ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1623ac8e9819SChris Mason int ret; 1624ac8e9819SChris Mason int num_found = 0; 1625ac8e9819SChris Mason unsigned long sk_offset = 0; 1626ac8e9819SChris Mason 1627ac8e9819SChris Mason path = btrfs_alloc_path(); 1628ac8e9819SChris Mason if (!path) 1629ac8e9819SChris Mason return -ENOMEM; 1630ac8e9819SChris Mason 1631ac8e9819SChris Mason if (sk->tree_id == 0) { 1632ac8e9819SChris Mason /* search the root of the inode that was passed */ 1633ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1634ac8e9819SChris Mason } else { 1635ac8e9819SChris Mason key.objectid = sk->tree_id; 1636ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1637ac8e9819SChris Mason key.offset = (u64)-1; 1638ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1639ac8e9819SChris Mason if (IS_ERR(root)) { 1640ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1641ac8e9819SChris Mason sk->tree_id); 1642ac8e9819SChris Mason btrfs_free_path(path); 1643ac8e9819SChris Mason return -ENOENT; 1644ac8e9819SChris Mason } 1645ac8e9819SChris Mason } 1646ac8e9819SChris Mason 1647ac8e9819SChris Mason key.objectid = sk->min_objectid; 1648ac8e9819SChris Mason key.type = sk->min_type; 1649ac8e9819SChris Mason key.offset = sk->min_offset; 1650ac8e9819SChris Mason 1651ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1652ac8e9819SChris Mason max_key.type = sk->max_type; 1653ac8e9819SChris Mason max_key.offset = sk->max_offset; 1654ac8e9819SChris Mason 1655ac8e9819SChris Mason path->keep_locks = 1; 1656ac8e9819SChris Mason 1657ac8e9819SChris Mason while(1) { 1658ac8e9819SChris Mason ret = btrfs_search_forward(root, &key, &max_key, path, 0, 1659ac8e9819SChris Mason sk->min_transid); 1660ac8e9819SChris Mason if (ret != 0) { 1661ac8e9819SChris Mason if (ret > 0) 1662ac8e9819SChris Mason ret = 0; 1663ac8e9819SChris Mason goto err; 1664ac8e9819SChris Mason } 1665ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1666ac8e9819SChris Mason &sk_offset, &num_found); 1667b3b4aa74SDavid Sterba btrfs_release_path(path); 1668ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1669ac8e9819SChris Mason break; 1670ac8e9819SChris Mason 1671ac8e9819SChris Mason } 1672ac8e9819SChris Mason ret = 0; 1673ac8e9819SChris Mason err: 1674ac8e9819SChris Mason sk->nr_items = num_found; 1675ac8e9819SChris Mason btrfs_free_path(path); 1676ac8e9819SChris Mason return ret; 1677ac8e9819SChris Mason } 1678ac8e9819SChris Mason 1679ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1680ac8e9819SChris Mason void __user *argp) 1681ac8e9819SChris Mason { 1682ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1683ac8e9819SChris Mason struct inode *inode; 1684ac8e9819SChris Mason int ret; 1685ac8e9819SChris Mason 1686ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1687ac8e9819SChris Mason return -EPERM; 1688ac8e9819SChris Mason 16892354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 16902354d08fSJulia Lawall if (IS_ERR(args)) 16912354d08fSJulia Lawall return PTR_ERR(args); 1692ac8e9819SChris Mason 1693ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1694ac8e9819SChris Mason ret = search_ioctl(inode, args); 1695ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1696ac8e9819SChris Mason ret = -EFAULT; 1697ac8e9819SChris Mason kfree(args); 1698ac8e9819SChris Mason return ret; 1699ac8e9819SChris Mason } 1700ac8e9819SChris Mason 170198d377a0STARUISI Hiroaki /* 1702ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1703ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 170498d377a0STARUISI Hiroaki */ 170598d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 170698d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 170798d377a0STARUISI Hiroaki { 170898d377a0STARUISI Hiroaki struct btrfs_root *root; 170998d377a0STARUISI Hiroaki struct btrfs_key key; 1710ac8e9819SChris Mason char *ptr; 171198d377a0STARUISI Hiroaki int ret = -1; 171298d377a0STARUISI Hiroaki int slot; 171398d377a0STARUISI Hiroaki int len; 171498d377a0STARUISI Hiroaki int total_len = 0; 171598d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 171698d377a0STARUISI Hiroaki struct extent_buffer *l; 171798d377a0STARUISI Hiroaki struct btrfs_path *path; 171898d377a0STARUISI Hiroaki 171998d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 172098d377a0STARUISI Hiroaki name[0]='\0'; 172198d377a0STARUISI Hiroaki return 0; 172298d377a0STARUISI Hiroaki } 172398d377a0STARUISI Hiroaki 172498d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 172598d377a0STARUISI Hiroaki if (!path) 172698d377a0STARUISI Hiroaki return -ENOMEM; 172798d377a0STARUISI Hiroaki 1728ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 172998d377a0STARUISI Hiroaki 173098d377a0STARUISI Hiroaki key.objectid = tree_id; 173198d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 173298d377a0STARUISI Hiroaki key.offset = (u64)-1; 173398d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 173498d377a0STARUISI Hiroaki if (IS_ERR(root)) { 173598d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 17368ad6fcabSChris Mason ret = -ENOENT; 17378ad6fcabSChris Mason goto out; 173898d377a0STARUISI Hiroaki } 173998d377a0STARUISI Hiroaki 174098d377a0STARUISI Hiroaki key.objectid = dirid; 174198d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 17428ad6fcabSChris Mason key.offset = (u64)-1; 174398d377a0STARUISI Hiroaki 174498d377a0STARUISI Hiroaki while(1) { 174598d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 174698d377a0STARUISI Hiroaki if (ret < 0) 174798d377a0STARUISI Hiroaki goto out; 174898d377a0STARUISI Hiroaki 174998d377a0STARUISI Hiroaki l = path->nodes[0]; 175098d377a0STARUISI Hiroaki slot = path->slots[0]; 17518ad6fcabSChris Mason if (ret > 0 && slot > 0) 17528ad6fcabSChris Mason slot--; 175398d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 175498d377a0STARUISI Hiroaki 175598d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 1756ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 1757ac8e9819SChris Mason ret = -ENOENT; 175898d377a0STARUISI Hiroaki goto out; 1759ac8e9819SChris Mason } 176098d377a0STARUISI Hiroaki 176198d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 176298d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 176398d377a0STARUISI Hiroaki ptr -= len + 1; 176498d377a0STARUISI Hiroaki total_len += len + 1; 1765ac8e9819SChris Mason if (ptr < name) 176698d377a0STARUISI Hiroaki goto out; 176798d377a0STARUISI Hiroaki 176898d377a0STARUISI Hiroaki *(ptr + len) = '/'; 176998d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 177098d377a0STARUISI Hiroaki 177198d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 177298d377a0STARUISI Hiroaki break; 177398d377a0STARUISI Hiroaki 1774b3b4aa74SDavid Sterba btrfs_release_path(path); 177598d377a0STARUISI Hiroaki key.objectid = key.offset; 17768ad6fcabSChris Mason key.offset = (u64)-1; 177798d377a0STARUISI Hiroaki dirid = key.objectid; 177898d377a0STARUISI Hiroaki } 1779ac8e9819SChris Mason if (ptr < name) 178098d377a0STARUISI Hiroaki goto out; 178177906a50SLi Zefan memmove(name, ptr, total_len); 178298d377a0STARUISI Hiroaki name[total_len]='\0'; 178398d377a0STARUISI Hiroaki ret = 0; 178498d377a0STARUISI Hiroaki out: 178598d377a0STARUISI Hiroaki btrfs_free_path(path); 1786ac8e9819SChris Mason return ret; 1787ac8e9819SChris Mason } 1788ac8e9819SChris Mason 1789ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 1790ac8e9819SChris Mason void __user *argp) 1791ac8e9819SChris Mason { 1792ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 1793ac8e9819SChris Mason struct inode *inode; 1794ac8e9819SChris Mason int ret; 1795ac8e9819SChris Mason 1796ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1797ac8e9819SChris Mason return -EPERM; 1798ac8e9819SChris Mason 17992354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 18002354d08fSJulia Lawall if (IS_ERR(args)) 18012354d08fSJulia Lawall return PTR_ERR(args); 1802c2b96929SDan Carpenter 1803ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1804ac8e9819SChris Mason 18051b53ac4dSChris Mason if (args->treeid == 0) 18061b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 18071b53ac4dSChris Mason 1808ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 1809ac8e9819SChris Mason args->treeid, args->objectid, 1810ac8e9819SChris Mason args->name); 1811ac8e9819SChris Mason 1812ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1813ac8e9819SChris Mason ret = -EFAULT; 1814ac8e9819SChris Mason 1815ac8e9819SChris Mason kfree(args); 181698d377a0STARUISI Hiroaki return ret; 181798d377a0STARUISI Hiroaki } 181898d377a0STARUISI Hiroaki 181976dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 182076dda93cSYan, Zheng void __user *arg) 182176dda93cSYan, Zheng { 182276dda93cSYan, Zheng struct dentry *parent = fdentry(file); 182376dda93cSYan, Zheng struct dentry *dentry; 182476dda93cSYan, Zheng struct inode *dir = parent->d_inode; 182576dda93cSYan, Zheng struct inode *inode; 182676dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 182776dda93cSYan, Zheng struct btrfs_root *dest = NULL; 182876dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 182976dda93cSYan, Zheng struct btrfs_trans_handle *trans; 183076dda93cSYan, Zheng int namelen; 183176dda93cSYan, Zheng int ret; 183276dda93cSYan, Zheng int err = 0; 183376dda93cSYan, Zheng 183476dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 183576dda93cSYan, Zheng if (IS_ERR(vol_args)) 183676dda93cSYan, Zheng return PTR_ERR(vol_args); 183776dda93cSYan, Zheng 183876dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 183976dda93cSYan, Zheng namelen = strlen(vol_args->name); 184076dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 184176dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 184276dda93cSYan, Zheng err = -EINVAL; 184376dda93cSYan, Zheng goto out; 184476dda93cSYan, Zheng } 184576dda93cSYan, Zheng 184676dda93cSYan, Zheng err = mnt_want_write(file->f_path.mnt); 184776dda93cSYan, Zheng if (err) 184876dda93cSYan, Zheng goto out; 184976dda93cSYan, Zheng 185076dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 185176dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 185276dda93cSYan, Zheng if (IS_ERR(dentry)) { 185376dda93cSYan, Zheng err = PTR_ERR(dentry); 185476dda93cSYan, Zheng goto out_unlock_dir; 185576dda93cSYan, Zheng } 185676dda93cSYan, Zheng 185776dda93cSYan, Zheng if (!dentry->d_inode) { 185876dda93cSYan, Zheng err = -ENOENT; 185976dda93cSYan, Zheng goto out_dput; 186076dda93cSYan, Zheng } 186176dda93cSYan, Zheng 186276dda93cSYan, Zheng inode = dentry->d_inode; 18634260f7c7SSage Weil dest = BTRFS_I(inode)->root; 18644260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 18654260f7c7SSage Weil /* 18664260f7c7SSage Weil * Regular user. Only allow this with a special mount 18674260f7c7SSage Weil * option, when the user has write+exec access to the 18684260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 18694260f7c7SSage Weil * allowed. 18704260f7c7SSage Weil * 18714260f7c7SSage Weil * Note that this is _not_ check that the subvol is 18724260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 18734260f7c7SSage Weil * otherwise be able to delete. 18744260f7c7SSage Weil * 18754260f7c7SSage Weil * Users who want to delete empty subvols should try 18764260f7c7SSage Weil * rmdir(2). 18774260f7c7SSage Weil */ 18784260f7c7SSage Weil err = -EPERM; 18794260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 18804260f7c7SSage Weil goto out_dput; 18814260f7c7SSage Weil 18824260f7c7SSage Weil /* 18834260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 18844260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 18854260f7c7SSage Weil * must be called on the dentry referencing the root 18864260f7c7SSage Weil * of the subvol, not a random directory contained 18874260f7c7SSage Weil * within it. 18884260f7c7SSage Weil */ 18894260f7c7SSage Weil err = -EINVAL; 18904260f7c7SSage Weil if (root == dest) 18914260f7c7SSage Weil goto out_dput; 18924260f7c7SSage Weil 18934260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 18944260f7c7SSage Weil if (err) 18954260f7c7SSage Weil goto out_dput; 18964260f7c7SSage Weil 18974260f7c7SSage Weil /* check if subvolume may be deleted by a non-root user */ 18984260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 18994260f7c7SSage Weil if (err) 19004260f7c7SSage Weil goto out_dput; 19014260f7c7SSage Weil } 19024260f7c7SSage Weil 190333345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 190476dda93cSYan, Zheng err = -EINVAL; 190576dda93cSYan, Zheng goto out_dput; 190676dda93cSYan, Zheng } 190776dda93cSYan, Zheng 190876dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 190976dda93cSYan, Zheng err = d_invalidate(dentry); 191076dda93cSYan, Zheng if (err) 191176dda93cSYan, Zheng goto out_unlock; 191276dda93cSYan, Zheng 191376dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 191476dda93cSYan, Zheng 191576dda93cSYan, Zheng err = may_destroy_subvol(dest); 191676dda93cSYan, Zheng if (err) 191776dda93cSYan, Zheng goto out_up_write; 191876dda93cSYan, Zheng 1919a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 1920a22285a6SYan, Zheng if (IS_ERR(trans)) { 1921a22285a6SYan, Zheng err = PTR_ERR(trans); 1922d327099aSDan Carpenter goto out_up_write; 1923a22285a6SYan, Zheng } 1924a22285a6SYan, Zheng trans->block_rsv = &root->fs_info->global_block_rsv; 1925a22285a6SYan, Zheng 192676dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 192776dda93cSYan, Zheng dest->root_key.objectid, 192876dda93cSYan, Zheng dentry->d_name.name, 192976dda93cSYan, Zheng dentry->d_name.len); 193076dda93cSYan, Zheng BUG_ON(ret); 193176dda93cSYan, Zheng 193276dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 193376dda93cSYan, Zheng 193476dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 193576dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 193676dda93cSYan, Zheng dest->root_item.drop_level = 0; 193776dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 193876dda93cSYan, Zheng 1939d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 194076dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 194176dda93cSYan, Zheng root->fs_info->tree_root, 194276dda93cSYan, Zheng dest->root_key.objectid); 194376dda93cSYan, Zheng BUG_ON(ret); 1944d68fc57bSYan, Zheng } 194576dda93cSYan, Zheng 1946531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 194776dda93cSYan, Zheng BUG_ON(ret); 194876dda93cSYan, Zheng inode->i_flags |= S_DEAD; 194976dda93cSYan, Zheng out_up_write: 195076dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 195176dda93cSYan, Zheng out_unlock: 195276dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 195376dda93cSYan, Zheng if (!err) { 1954efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 195576dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 195676dda93cSYan, Zheng d_delete(dentry); 195776dda93cSYan, Zheng } 195876dda93cSYan, Zheng out_dput: 195976dda93cSYan, Zheng dput(dentry); 196076dda93cSYan, Zheng out_unlock_dir: 196176dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 196276dda93cSYan, Zheng mnt_drop_write(file->f_path.mnt); 196376dda93cSYan, Zheng out: 196476dda93cSYan, Zheng kfree(vol_args); 196576dda93cSYan, Zheng return err; 196676dda93cSYan, Zheng } 196776dda93cSYan, Zheng 19681e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 1969f46b5a66SChristoph Hellwig { 1970f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 1971f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 19721e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 1973c146afadSYan Zheng int ret; 1974c146afadSYan Zheng 1975b83cc969SLi Zefan if (btrfs_root_readonly(root)) 1976b83cc969SLi Zefan return -EROFS; 1977b83cc969SLi Zefan 1978c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 1979c146afadSYan Zheng if (ret) 1980c146afadSYan Zheng return ret; 1981f46b5a66SChristoph Hellwig 1982f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1983f46b5a66SChristoph Hellwig case S_IFDIR: 1984e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 1985e441d54dSChris Mason ret = -EPERM; 1986e441d54dSChris Mason goto out; 1987e441d54dSChris Mason } 19888929ecfaSYan, Zheng ret = btrfs_defrag_root(root, 0); 19898929ecfaSYan, Zheng if (ret) 19908929ecfaSYan, Zheng goto out; 19918929ecfaSYan, Zheng ret = btrfs_defrag_root(root->fs_info->extent_root, 0); 1992f46b5a66SChristoph Hellwig break; 1993f46b5a66SChristoph Hellwig case S_IFREG: 1994e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 1995e441d54dSChris Mason ret = -EINVAL; 1996e441d54dSChris Mason goto out; 1997e441d54dSChris Mason } 19981e701a32SChris Mason 19991e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 20001e701a32SChris Mason if (!range) { 20011e701a32SChris Mason ret = -ENOMEM; 20021e701a32SChris Mason goto out; 20031e701a32SChris Mason } 20041e701a32SChris Mason 20051e701a32SChris Mason if (argp) { 20061e701a32SChris Mason if (copy_from_user(range, argp, 20071e701a32SChris Mason sizeof(*range))) { 20081e701a32SChris Mason ret = -EFAULT; 20091e701a32SChris Mason kfree(range); 2010683be16eSDan Carpenter goto out; 20111e701a32SChris Mason } 20121e701a32SChris Mason /* compression requires us to start the IO */ 20131e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 20141e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 20151e701a32SChris Mason range->extent_thresh = (u32)-1; 20161e701a32SChris Mason } 20171e701a32SChris Mason } else { 20181e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 20191e701a32SChris Mason range->len = (u64)-1; 20201e701a32SChris Mason } 20214cb5300bSChris Mason ret = btrfs_defrag_file(fdentry(file)->d_inode, file, 20224cb5300bSChris Mason range, 0, 0); 20234cb5300bSChris Mason if (ret > 0) 20244cb5300bSChris Mason ret = 0; 20251e701a32SChris Mason kfree(range); 2026f46b5a66SChristoph Hellwig break; 20278929ecfaSYan, Zheng default: 20288929ecfaSYan, Zheng ret = -EINVAL; 2029f46b5a66SChristoph Hellwig } 2030e441d54dSChris Mason out: 2031ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 2032e441d54dSChris Mason return ret; 2033f46b5a66SChristoph Hellwig } 2034f46b5a66SChristoph Hellwig 2035b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2036f46b5a66SChristoph Hellwig { 2037f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2038f46b5a66SChristoph Hellwig int ret; 2039f46b5a66SChristoph Hellwig 2040e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2041e441d54dSChris Mason return -EPERM; 2042e441d54dSChris Mason 2043dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2044dae7b665SLi Zefan if (IS_ERR(vol_args)) 2045dae7b665SLi Zefan return PTR_ERR(vol_args); 2046f46b5a66SChristoph Hellwig 20475516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2048f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2049f46b5a66SChristoph Hellwig 2050f46b5a66SChristoph Hellwig kfree(vol_args); 2051f46b5a66SChristoph Hellwig return ret; 2052f46b5a66SChristoph Hellwig } 2053f46b5a66SChristoph Hellwig 2054b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) 2055f46b5a66SChristoph Hellwig { 2056f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2057f46b5a66SChristoph Hellwig int ret; 2058f46b5a66SChristoph Hellwig 2059e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2060e441d54dSChris Mason return -EPERM; 2061e441d54dSChris Mason 2062c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 2063c146afadSYan Zheng return -EROFS; 2064c146afadSYan Zheng 2065dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2066dae7b665SLi Zefan if (IS_ERR(vol_args)) 2067dae7b665SLi Zefan return PTR_ERR(vol_args); 2068f46b5a66SChristoph Hellwig 20695516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2070f46b5a66SChristoph Hellwig ret = btrfs_rm_device(root, vol_args->name); 2071f46b5a66SChristoph Hellwig 2072f46b5a66SChristoph Hellwig kfree(vol_args); 2073f46b5a66SChristoph Hellwig return ret; 2074f46b5a66SChristoph Hellwig } 2075f46b5a66SChristoph Hellwig 2076475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2077475f6387SJan Schmidt { 2078027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2079475f6387SJan Schmidt struct btrfs_device *device; 2080475f6387SJan Schmidt struct btrfs_device *next; 2081475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2082027ed2f0SLi Zefan int ret = 0; 2083475f6387SJan Schmidt 2084475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2085475f6387SJan Schmidt return -EPERM; 2086475f6387SJan Schmidt 2087027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2088027ed2f0SLi Zefan if (!fi_args) 2089027ed2f0SLi Zefan return -ENOMEM; 2090027ed2f0SLi Zefan 2091027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2092027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2093475f6387SJan Schmidt 2094475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2095475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2096027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2097027ed2f0SLi Zefan fi_args->max_id = device->devid; 2098475f6387SJan Schmidt } 2099475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2100475f6387SJan Schmidt 2101027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2102027ed2f0SLi Zefan ret = -EFAULT; 2103475f6387SJan Schmidt 2104027ed2f0SLi Zefan kfree(fi_args); 2105027ed2f0SLi Zefan return ret; 2106475f6387SJan Schmidt } 2107475f6387SJan Schmidt 2108475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2109475f6387SJan Schmidt { 2110475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2111475f6387SJan Schmidt struct btrfs_device *dev; 2112475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2113475f6387SJan Schmidt int ret = 0; 2114475f6387SJan Schmidt char *s_uuid = NULL; 2115475f6387SJan Schmidt char empty_uuid[BTRFS_UUID_SIZE] = {0}; 2116475f6387SJan Schmidt 2117475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2118475f6387SJan Schmidt return -EPERM; 2119475f6387SJan Schmidt 2120475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2121475f6387SJan Schmidt if (IS_ERR(di_args)) 2122475f6387SJan Schmidt return PTR_ERR(di_args); 2123475f6387SJan Schmidt 2124475f6387SJan Schmidt if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) 2125475f6387SJan Schmidt s_uuid = di_args->uuid; 2126475f6387SJan Schmidt 2127475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2128475f6387SJan Schmidt dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL); 2129475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2130475f6387SJan Schmidt 2131475f6387SJan Schmidt if (!dev) { 2132475f6387SJan Schmidt ret = -ENODEV; 2133475f6387SJan Schmidt goto out; 2134475f6387SJan Schmidt } 2135475f6387SJan Schmidt 2136475f6387SJan Schmidt di_args->devid = dev->devid; 2137475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2138475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2139475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2140475f6387SJan Schmidt strncpy(di_args->path, dev->name, sizeof(di_args->path)); 2141475f6387SJan Schmidt 2142475f6387SJan Schmidt out: 2143475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2144475f6387SJan Schmidt ret = -EFAULT; 2145475f6387SJan Schmidt 2146475f6387SJan Schmidt kfree(di_args); 2147475f6387SJan Schmidt return ret; 2148475f6387SJan Schmidt } 2149475f6387SJan Schmidt 215076dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 2151b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 2152f46b5a66SChristoph Hellwig { 2153f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2154f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2155f46b5a66SChristoph Hellwig struct file *src_file; 2156f46b5a66SChristoph Hellwig struct inode *src; 2157f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2158f46b5a66SChristoph Hellwig struct btrfs_path *path; 2159f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 2160ae01a0abSYan Zheng char *buf; 2161ae01a0abSYan Zheng struct btrfs_key key; 2162f46b5a66SChristoph Hellwig u32 nritems; 2163f46b5a66SChristoph Hellwig int slot; 2164ae01a0abSYan Zheng int ret; 2165c5c9cd4dSSage Weil u64 len = olen; 2166c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 2167c5c9cd4dSSage Weil u64 hint_byte; 2168d20f7043SChris Mason 2169c5c9cd4dSSage Weil /* 2170c5c9cd4dSSage Weil * TODO: 2171c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 2172c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 2173c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 2174c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 2175c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 2176c5c9cd4dSSage Weil * they don't overlap)? 2177c5c9cd4dSSage Weil */ 2178c5c9cd4dSSage Weil 2179e441d54dSChris Mason /* the destination must be opened for writing */ 21802ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 2181e441d54dSChris Mason return -EINVAL; 2182e441d54dSChris Mason 2183b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2184b83cc969SLi Zefan return -EROFS; 2185b83cc969SLi Zefan 2186c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2187c146afadSYan Zheng if (ret) 2188c146afadSYan Zheng return ret; 2189c146afadSYan Zheng 2190c5c9cd4dSSage Weil src_file = fget(srcfd); 2191ab67b7c1SYan Zheng if (!src_file) { 2192ab67b7c1SYan Zheng ret = -EBADF; 2193ab67b7c1SYan Zheng goto out_drop_write; 2194ab67b7c1SYan Zheng } 21955dc64164SDan Rosenberg 2196f46b5a66SChristoph Hellwig src = src_file->f_dentry->d_inode; 2197f46b5a66SChristoph Hellwig 2198c5c9cd4dSSage Weil ret = -EINVAL; 2199c5c9cd4dSSage Weil if (src == inode) 2200c5c9cd4dSSage Weil goto out_fput; 2201c5c9cd4dSSage Weil 22025dc64164SDan Rosenberg /* the src must be open for reading */ 22035dc64164SDan Rosenberg if (!(src_file->f_mode & FMODE_READ)) 22045dc64164SDan Rosenberg goto out_fput; 22055dc64164SDan Rosenberg 22060e7b824cSLi Zefan /* don't make the dst file partly checksummed */ 22070e7b824cSLi Zefan if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 22080e7b824cSLi Zefan (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 22090e7b824cSLi Zefan goto out_fput; 22100e7b824cSLi Zefan 2211ae01a0abSYan Zheng ret = -EISDIR; 2212ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 2213f46b5a66SChristoph Hellwig goto out_fput; 2214f46b5a66SChristoph Hellwig 2215ae01a0abSYan Zheng ret = -EXDEV; 2216ae01a0abSYan Zheng if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) 2217ae01a0abSYan Zheng goto out_fput; 2218ae01a0abSYan Zheng 2219ae01a0abSYan Zheng ret = -ENOMEM; 2220ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2221ae01a0abSYan Zheng if (!buf) 2222ae01a0abSYan Zheng goto out_fput; 2223ae01a0abSYan Zheng 2224ae01a0abSYan Zheng path = btrfs_alloc_path(); 2225ae01a0abSYan Zheng if (!path) { 2226ae01a0abSYan Zheng vfree(buf); 2227ae01a0abSYan Zheng goto out_fput; 2228ae01a0abSYan Zheng } 2229ae01a0abSYan Zheng path->reada = 2; 2230ae01a0abSYan Zheng 2231f46b5a66SChristoph Hellwig if (inode < src) { 2232fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 2233fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 2234f46b5a66SChristoph Hellwig } else { 2235fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 2236fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 2237f46b5a66SChristoph Hellwig } 2238f46b5a66SChristoph Hellwig 2239c5c9cd4dSSage Weil /* determine range to clone */ 2240c5c9cd4dSSage Weil ret = -EINVAL; 22412ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 2242f46b5a66SChristoph Hellwig goto out_unlock; 2243c5c9cd4dSSage Weil if (len == 0) 2244c5c9cd4dSSage Weil olen = len = src->i_size - off; 2245c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 2246c5c9cd4dSSage Weil if (off + len == src->i_size) 22472a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 2248c5c9cd4dSSage Weil 2249c5c9cd4dSSage Weil /* verify the end result is block aligned */ 22502a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 22512a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 2252c5c9cd4dSSage Weil goto out_unlock; 2253c5c9cd4dSSage Weil 2254d525e8abSLi Zefan if (destoff > inode->i_size) { 2255d525e8abSLi Zefan ret = btrfs_cont_expand(inode, inode->i_size, destoff); 2256d525e8abSLi Zefan if (ret) 2257d525e8abSLi Zefan goto out_unlock; 2258d525e8abSLi Zefan } 2259d525e8abSLi Zefan 226071ef0786SLi Zefan /* truncate page cache pages from target inode range */ 226171ef0786SLi Zefan truncate_inode_pages_range(&inode->i_data, destoff, 226271ef0786SLi Zefan PAGE_CACHE_ALIGN(destoff + len) - 1); 226371ef0786SLi Zefan 2264f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 2265f46b5a66SChristoph Hellwig another, and lock file content */ 2266f46b5a66SChristoph Hellwig while (1) { 226731840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 2268c5c9cd4dSSage Weil lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 22699a019196SSage Weil ordered = btrfs_lookup_first_ordered_extent(src, off+len); 22709a019196SSage Weil if (!ordered && 22719a019196SSage Weil !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, 22729a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 2273f46b5a66SChristoph Hellwig break; 2274c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 2275ae01a0abSYan Zheng if (ordered) 2276ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 22779a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 2278f46b5a66SChristoph Hellwig } 2279f46b5a66SChristoph Hellwig 2280c5c9cd4dSSage Weil /* clone data */ 228133345d01SLi Zefan key.objectid = btrfs_ino(src); 2282ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2283ae01a0abSYan Zheng key.offset = 0; 2284f46b5a66SChristoph Hellwig 2285f46b5a66SChristoph Hellwig while (1) { 2286f46b5a66SChristoph Hellwig /* 2287f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2288f46b5a66SChristoph Hellwig * tree. 2289f46b5a66SChristoph Hellwig */ 2290a22285a6SYan, Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2291f46b5a66SChristoph Hellwig if (ret < 0) 2292f46b5a66SChristoph Hellwig goto out; 2293f46b5a66SChristoph Hellwig 2294ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2295ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2296f46b5a66SChristoph Hellwig ret = btrfs_next_leaf(root, path); 2297f46b5a66SChristoph Hellwig if (ret < 0) 2298f46b5a66SChristoph Hellwig goto out; 2299f46b5a66SChristoph Hellwig if (ret > 0) 2300f46b5a66SChristoph Hellwig break; 2301ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2302f46b5a66SChristoph Hellwig } 2303f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2304f46b5a66SChristoph Hellwig slot = path->slots[0]; 2305f46b5a66SChristoph Hellwig 2306ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2307d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 230833345d01SLi Zefan key.objectid != btrfs_ino(src)) 2309f46b5a66SChristoph Hellwig break; 2310f46b5a66SChristoph Hellwig 2311c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2312c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2313c5c9cd4dSSage Weil int type; 231431840ae1SZheng Yan u32 size; 231531840ae1SZheng Yan struct btrfs_key new_key; 2316c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2317c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2318c5c9cd4dSSage Weil u8 comp; 2319b5384d48SSage Weil u64 endoff; 232031840ae1SZheng Yan 232131840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 232231840ae1SZheng Yan read_extent_buffer(leaf, buf, 232331840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 232431840ae1SZheng Yan size); 2325c5c9cd4dSSage Weil 2326c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2327c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2328c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2329c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2330c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2331c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2332d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2333d397712bSChris Mason extent); 2334d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2335d397712bSChris Mason extent); 2336c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2337d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2338d397712bSChris Mason extent); 2339c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2340c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2341c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2342c5c9cd4dSSage Weil extent); 2343c5c9cd4dSSage Weil } 2344b3b4aa74SDavid Sterba btrfs_release_path(path); 234531840ae1SZheng Yan 2346050006a7SSage Weil if (key.offset + datal <= off || 2347c5c9cd4dSSage Weil key.offset >= off+len) 2348c5c9cd4dSSage Weil goto next; 2349c5c9cd4dSSage Weil 235031840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 235133345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 23524d728ec7SLi Zefan if (off <= key.offset) 2353c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 23544d728ec7SLi Zefan else 23554d728ec7SLi Zefan new_key.offset = destoff; 2356c5c9cd4dSSage Weil 2357b6f3409bSSage Weil /* 2358b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2359b6f3409bSSage Weil * 1 - add new extent 2360b6f3409bSSage Weil * 1 - inode update 2361b6f3409bSSage Weil */ 2362b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2363a22285a6SYan, Zheng if (IS_ERR(trans)) { 2364a22285a6SYan, Zheng ret = PTR_ERR(trans); 2365a22285a6SYan, Zheng goto out; 2366a22285a6SYan, Zheng } 2367a22285a6SYan, Zheng 2368c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2369c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2370d72c0842SLi Zefan /* 2371d72c0842SLi Zefan * a | --- range to clone ---| b 2372d72c0842SLi Zefan * | ------------- extent ------------- | 2373d72c0842SLi Zefan */ 2374d72c0842SLi Zefan 2375d72c0842SLi Zefan /* substract range b */ 2376d72c0842SLi Zefan if (key.offset + datal > off + len) 2377d72c0842SLi Zefan datal = off + len - key.offset; 2378d72c0842SLi Zefan 2379d72c0842SLi Zefan /* substract range a */ 2380a22285a6SYan, Zheng if (off > key.offset) { 2381a22285a6SYan, Zheng datao += off - key.offset; 2382a22285a6SYan, Zheng datal -= off - key.offset; 2383a22285a6SYan, Zheng } 2384a22285a6SYan, Zheng 2385a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 2386a22285a6SYan, Zheng new_key.offset, 2387a22285a6SYan, Zheng new_key.offset + datal, 2388a22285a6SYan, Zheng &hint_byte, 1); 2389a22285a6SYan, Zheng BUG_ON(ret); 2390a22285a6SYan, Zheng 239131840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 239231840ae1SZheng Yan &new_key, size); 2393a22285a6SYan, Zheng BUG_ON(ret); 239431840ae1SZheng Yan 239531840ae1SZheng Yan leaf = path->nodes[0]; 239631840ae1SZheng Yan slot = path->slots[0]; 239731840ae1SZheng Yan write_extent_buffer(leaf, buf, 239831840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 239931840ae1SZheng Yan size); 2400ae01a0abSYan Zheng 2401f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 2402f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 2403c5c9cd4dSSage Weil 2404c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 2405c5c9cd4dSSage Weil if (!disko) 2406c5c9cd4dSSage Weil datao = 0; 2407c5c9cd4dSSage Weil 2408c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 2409c5c9cd4dSSage Weil datao); 2410c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 2411c5c9cd4dSSage Weil datal); 2412c5c9cd4dSSage Weil if (disko) { 2413c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2414ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 24155d4f98a2SYan Zheng disko, diskl, 0, 2416f46b5a66SChristoph Hellwig root->root_key.objectid, 241733345d01SLi Zefan btrfs_ino(inode), 24185d4f98a2SYan Zheng new_key.offset - datao); 2419ae01a0abSYan Zheng BUG_ON(ret); 2420f46b5a66SChristoph Hellwig } 2421c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2422c5c9cd4dSSage Weil u64 skip = 0; 2423c5c9cd4dSSage Weil u64 trim = 0; 2424c5c9cd4dSSage Weil if (off > key.offset) { 2425c5c9cd4dSSage Weil skip = off - key.offset; 2426c5c9cd4dSSage Weil new_key.offset += skip; 242731840ae1SZheng Yan } 2428d397712bSChris Mason 2429c5c9cd4dSSage Weil if (key.offset + datal > off+len) 2430c5c9cd4dSSage Weil trim = key.offset + datal - (off+len); 2431d397712bSChris Mason 2432c5c9cd4dSSage Weil if (comp && (skip || trim)) { 2433c5c9cd4dSSage Weil ret = -EINVAL; 2434a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2435c5c9cd4dSSage Weil goto out; 243631840ae1SZheng Yan } 2437c5c9cd4dSSage Weil size -= skip + trim; 2438c5c9cd4dSSage Weil datal -= skip + trim; 2439a22285a6SYan, Zheng 2440a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 2441a22285a6SYan, Zheng new_key.offset, 2442a22285a6SYan, Zheng new_key.offset + datal, 2443a22285a6SYan, Zheng &hint_byte, 1); 2444a22285a6SYan, Zheng BUG_ON(ret); 2445a22285a6SYan, Zheng 2446c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 2447c5c9cd4dSSage Weil &new_key, size); 2448a22285a6SYan, Zheng BUG_ON(ret); 2449c5c9cd4dSSage Weil 2450c5c9cd4dSSage Weil if (skip) { 2451d397712bSChris Mason u32 start = 2452d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 2453c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 2454c5c9cd4dSSage Weil datal); 2455c5c9cd4dSSage Weil } 2456c5c9cd4dSSage Weil 2457c5c9cd4dSSage Weil leaf = path->nodes[0]; 2458c5c9cd4dSSage Weil slot = path->slots[0]; 2459c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2460c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2461c5c9cd4dSSage Weil size); 2462c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2463c5c9cd4dSSage Weil } 2464c5c9cd4dSSage Weil 2465c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2466b3b4aa74SDavid Sterba btrfs_release_path(path); 2467c5c9cd4dSSage Weil 2468a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2469b5384d48SSage Weil 2470b5384d48SSage Weil /* 2471b5384d48SSage Weil * we round up to the block size at eof when 2472b5384d48SSage Weil * determining which extents to clone above, 2473b5384d48SSage Weil * but shouldn't round up the file size 2474b5384d48SSage Weil */ 2475b5384d48SSage Weil endoff = new_key.offset + datal; 24765f3888ffSLi Zefan if (endoff > destoff+olen) 24775f3888ffSLi Zefan endoff = destoff+olen; 2478b5384d48SSage Weil if (endoff > inode->i_size) 2479b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2480b5384d48SSage Weil 2481a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 2482a22285a6SYan, Zheng BUG_ON(ret); 2483a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2484a22285a6SYan, Zheng } 2485c5c9cd4dSSage Weil next: 2486b3b4aa74SDavid Sterba btrfs_release_path(path); 2487ae01a0abSYan Zheng key.offset++; 2488ae01a0abSYan Zheng } 2489f46b5a66SChristoph Hellwig ret = 0; 2490f46b5a66SChristoph Hellwig out: 2491b3b4aa74SDavid Sterba btrfs_release_path(path); 2492c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 2493f46b5a66SChristoph Hellwig out_unlock: 2494f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2495f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2496ae01a0abSYan Zheng vfree(buf); 2497ae01a0abSYan Zheng btrfs_free_path(path); 2498f46b5a66SChristoph Hellwig out_fput: 2499f46b5a66SChristoph Hellwig fput(src_file); 2500ab67b7c1SYan Zheng out_drop_write: 2501ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 2502f46b5a66SChristoph Hellwig return ret; 2503f46b5a66SChristoph Hellwig } 2504f46b5a66SChristoph Hellwig 25057a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2506c5c9cd4dSSage Weil { 2507c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2508c5c9cd4dSSage Weil 25097a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2510c5c9cd4dSSage Weil return -EFAULT; 2511c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2512c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2513c5c9cd4dSSage Weil } 2514c5c9cd4dSSage Weil 2515f46b5a66SChristoph Hellwig /* 2516f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2517f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2518f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2519f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2520f46b5a66SChristoph Hellwig */ 2521b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2522f46b5a66SChristoph Hellwig { 2523f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2524f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2525f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 25261ab86aedSSage Weil int ret; 2527f46b5a66SChristoph Hellwig 25281ab86aedSSage Weil ret = -EPERM; 2529df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2530f46b5a66SChristoph Hellwig goto out; 25311ab86aedSSage Weil 25321ab86aedSSage Weil ret = -EINPROGRESS; 25331ab86aedSSage Weil if (file->private_data) 25341ab86aedSSage Weil goto out; 25359ca9ee09SSage Weil 2536b83cc969SLi Zefan ret = -EROFS; 2537b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2538b83cc969SLi Zefan goto out; 2539b83cc969SLi Zefan 2540c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2541c146afadSYan Zheng if (ret) 2542c146afadSYan Zheng goto out; 2543c146afadSYan Zheng 2544a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 25459ca9ee09SSage Weil 2546f46b5a66SChristoph Hellwig ret = -ENOMEM; 25477a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 2548abd30bb0STsutomu Itoh if (IS_ERR(trans)) 25491ab86aedSSage Weil goto out_drop; 25501ab86aedSSage Weil 25511ab86aedSSage Weil file->private_data = trans; 25521ab86aedSSage Weil return 0; 25531ab86aedSSage Weil 25541ab86aedSSage Weil out_drop: 2555a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 25561ab86aedSSage Weil mnt_drop_write(file->f_path.mnt); 2557f46b5a66SChristoph Hellwig out: 2558f46b5a66SChristoph Hellwig return ret; 2559f46b5a66SChristoph Hellwig } 2560f46b5a66SChristoph Hellwig 25616ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 25626ef5ed0dSJosef Bacik { 25636ef5ed0dSJosef Bacik struct inode *inode = fdentry(file)->d_inode; 25646ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 25656ef5ed0dSJosef Bacik struct btrfs_root *new_root; 25666ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 25676ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 25686ef5ed0dSJosef Bacik struct btrfs_path *path; 25696ef5ed0dSJosef Bacik struct btrfs_key location; 25706ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 25716ef5ed0dSJosef Bacik struct btrfs_super_block *disk_super; 25726ef5ed0dSJosef Bacik u64 features; 25736ef5ed0dSJosef Bacik u64 objectid = 0; 25746ef5ed0dSJosef Bacik u64 dir_id; 25756ef5ed0dSJosef Bacik 25766ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 25776ef5ed0dSJosef Bacik return -EPERM; 25786ef5ed0dSJosef Bacik 25796ef5ed0dSJosef Bacik if (copy_from_user(&objectid, argp, sizeof(objectid))) 25806ef5ed0dSJosef Bacik return -EFAULT; 25816ef5ed0dSJosef Bacik 25826ef5ed0dSJosef Bacik if (!objectid) 25836ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 25846ef5ed0dSJosef Bacik 25856ef5ed0dSJosef Bacik location.objectid = objectid; 25866ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 25876ef5ed0dSJosef Bacik location.offset = (u64)-1; 25886ef5ed0dSJosef Bacik 25896ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 25906ef5ed0dSJosef Bacik if (IS_ERR(new_root)) 25916ef5ed0dSJosef Bacik return PTR_ERR(new_root); 25926ef5ed0dSJosef Bacik 25936ef5ed0dSJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 25946ef5ed0dSJosef Bacik return -ENOENT; 25956ef5ed0dSJosef Bacik 25966ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 25976ef5ed0dSJosef Bacik if (!path) 25986ef5ed0dSJosef Bacik return -ENOMEM; 25996ef5ed0dSJosef Bacik path->leave_spinning = 1; 26006ef5ed0dSJosef Bacik 26016ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 260298d5dc13STsutomu Itoh if (IS_ERR(trans)) { 26036ef5ed0dSJosef Bacik btrfs_free_path(path); 260498d5dc13STsutomu Itoh return PTR_ERR(trans); 26056ef5ed0dSJosef Bacik } 26066ef5ed0dSJosef Bacik 26076ef5ed0dSJosef Bacik dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); 26086ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 26096ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2610cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 26116ef5ed0dSJosef Bacik btrfs_free_path(path); 26126ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 26136ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 26146ef5ed0dSJosef Bacik "this isn't going to work\n"); 26156ef5ed0dSJosef Bacik return -ENOENT; 26166ef5ed0dSJosef Bacik } 26176ef5ed0dSJosef Bacik 26186ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 26196ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 26206ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 26216ef5ed0dSJosef Bacik btrfs_free_path(path); 26226ef5ed0dSJosef Bacik 26236ef5ed0dSJosef Bacik disk_super = &root->fs_info->super_copy; 26246ef5ed0dSJosef Bacik features = btrfs_super_incompat_flags(disk_super); 26256ef5ed0dSJosef Bacik if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { 26266ef5ed0dSJosef Bacik features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; 26276ef5ed0dSJosef Bacik btrfs_set_super_incompat_flags(disk_super, features); 26286ef5ed0dSJosef Bacik } 26296ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 26306ef5ed0dSJosef Bacik 26316ef5ed0dSJosef Bacik return 0; 26326ef5ed0dSJosef Bacik } 26336ef5ed0dSJosef Bacik 2634bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list, 2635bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 2636bf5fc093SJosef Bacik { 2637bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 2638bf5fc093SJosef Bacik 2639bf5fc093SJosef Bacik space->total_bytes = 0; 2640bf5fc093SJosef Bacik space->used_bytes = 0; 2641bf5fc093SJosef Bacik space->flags = 0; 2642bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 2643bf5fc093SJosef Bacik space->flags = block_group->flags; 2644bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 2645bf5fc093SJosef Bacik space->used_bytes += 2646bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 2647bf5fc093SJosef Bacik } 2648bf5fc093SJosef Bacik } 2649bf5fc093SJosef Bacik 26501406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 26511406e432SJosef Bacik { 26521406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 26531406e432SJosef Bacik struct btrfs_ioctl_space_info space; 26541406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 26557fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 265613f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 26571406e432SJosef Bacik struct btrfs_space_info *info; 2658bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 2659bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 2660bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 2661bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 2662bf5fc093SJosef Bacik int num_types = 4; 26637fde62bfSChris Mason int alloc_size; 26641406e432SJosef Bacik int ret = 0; 266551788b1bSDan Rosenberg u64 slot_count = 0; 2666bf5fc093SJosef Bacik int i, c; 26671406e432SJosef Bacik 26681406e432SJosef Bacik if (copy_from_user(&space_args, 26691406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 26701406e432SJosef Bacik sizeof(space_args))) 26711406e432SJosef Bacik return -EFAULT; 26721406e432SJosef Bacik 2673bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2674bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2675bf5fc093SJosef Bacik 2676bf5fc093SJosef Bacik info = NULL; 26777fde62bfSChris Mason rcu_read_lock(); 2678bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2679bf5fc093SJosef Bacik list) { 2680bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2681bf5fc093SJosef Bacik info = tmp; 2682bf5fc093SJosef Bacik break; 2683bf5fc093SJosef Bacik } 2684bf5fc093SJosef Bacik } 26857fde62bfSChris Mason rcu_read_unlock(); 26861406e432SJosef Bacik 2687bf5fc093SJosef Bacik if (!info) 2688bf5fc093SJosef Bacik continue; 2689bf5fc093SJosef Bacik 2690bf5fc093SJosef Bacik down_read(&info->groups_sem); 2691bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2692bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 2693bf5fc093SJosef Bacik slot_count++; 2694bf5fc093SJosef Bacik } 2695bf5fc093SJosef Bacik up_read(&info->groups_sem); 2696bf5fc093SJosef Bacik } 2697bf5fc093SJosef Bacik 26987fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 26997fde62bfSChris Mason if (space_args.space_slots == 0) { 27007fde62bfSChris Mason space_args.total_spaces = slot_count; 27017fde62bfSChris Mason goto out; 27027fde62bfSChris Mason } 2703bf5fc093SJosef Bacik 270451788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 2705bf5fc093SJosef Bacik 27067fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 2707bf5fc093SJosef Bacik 27087fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 27097fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 27107fde62bfSChris Mason */ 27117fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 27127fde62bfSChris Mason return -ENOMEM; 27137fde62bfSChris Mason 27147fde62bfSChris Mason space_args.total_spaces = 0; 27157fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 27167fde62bfSChris Mason if (!dest) 27177fde62bfSChris Mason return -ENOMEM; 27187fde62bfSChris Mason dest_orig = dest; 27197fde62bfSChris Mason 27207fde62bfSChris Mason /* now we have a buffer to copy into */ 2721bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2722bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2723bf5fc093SJosef Bacik 272451788b1bSDan Rosenberg if (!slot_count) 272551788b1bSDan Rosenberg break; 272651788b1bSDan Rosenberg 2727bf5fc093SJosef Bacik info = NULL; 27281406e432SJosef Bacik rcu_read_lock(); 2729bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2730bf5fc093SJosef Bacik list) { 2731bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2732bf5fc093SJosef Bacik info = tmp; 27337fde62bfSChris Mason break; 2734bf5fc093SJosef Bacik } 2735bf5fc093SJosef Bacik } 2736bf5fc093SJosef Bacik rcu_read_unlock(); 27377fde62bfSChris Mason 2738bf5fc093SJosef Bacik if (!info) 2739bf5fc093SJosef Bacik continue; 2740bf5fc093SJosef Bacik down_read(&info->groups_sem); 2741bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2742bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 2743bf5fc093SJosef Bacik get_block_group_info(&info->block_groups[c], 2744bf5fc093SJosef Bacik &space); 27457fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 27461406e432SJosef Bacik dest++; 27471406e432SJosef Bacik space_args.total_spaces++; 274851788b1bSDan Rosenberg slot_count--; 27491406e432SJosef Bacik } 275051788b1bSDan Rosenberg if (!slot_count) 275151788b1bSDan Rosenberg break; 2752bf5fc093SJosef Bacik } 2753bf5fc093SJosef Bacik up_read(&info->groups_sem); 2754bf5fc093SJosef Bacik } 27551406e432SJosef Bacik 27567fde62bfSChris Mason user_dest = (struct btrfs_ioctl_space_info *) 27577fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 27587fde62bfSChris Mason 27597fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 27607fde62bfSChris Mason ret = -EFAULT; 27617fde62bfSChris Mason 27627fde62bfSChris Mason kfree(dest_orig); 27637fde62bfSChris Mason out: 27647fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 27651406e432SJosef Bacik ret = -EFAULT; 27661406e432SJosef Bacik 27671406e432SJosef Bacik return ret; 27681406e432SJosef Bacik } 27691406e432SJosef Bacik 2770f46b5a66SChristoph Hellwig /* 2771f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2772f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2773f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2774f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2775f46b5a66SChristoph Hellwig */ 2776f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 2777f46b5a66SChristoph Hellwig { 2778f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2779f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2780f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2781f46b5a66SChristoph Hellwig 2782f46b5a66SChristoph Hellwig trans = file->private_data; 27831ab86aedSSage Weil if (!trans) 27841ab86aedSSage Weil return -EINVAL; 2785b214107eSChristoph Hellwig file->private_data = NULL; 27869ca9ee09SSage Weil 27871ab86aedSSage Weil btrfs_end_transaction(trans, root); 27881ab86aedSSage Weil 2789a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 27909ca9ee09SSage Weil 2791cfc8ea87SSage Weil mnt_drop_write(file->f_path.mnt); 27921ab86aedSSage Weil return 0; 2793f46b5a66SChristoph Hellwig } 2794f46b5a66SChristoph Hellwig 279546204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) 279646204592SSage Weil { 279746204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 279846204592SSage Weil struct btrfs_trans_handle *trans; 279946204592SSage Weil u64 transid; 2800db5b493aSTsutomu Itoh int ret; 280146204592SSage Weil 280246204592SSage Weil trans = btrfs_start_transaction(root, 0); 280398d5dc13STsutomu Itoh if (IS_ERR(trans)) 280498d5dc13STsutomu Itoh return PTR_ERR(trans); 280546204592SSage Weil transid = trans->transid; 2806db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 28078b2b2d3cSTsutomu Itoh if (ret) { 28088b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 2809db5b493aSTsutomu Itoh return ret; 28108b2b2d3cSTsutomu Itoh } 281146204592SSage Weil 281246204592SSage Weil if (argp) 281346204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 281446204592SSage Weil return -EFAULT; 281546204592SSage Weil return 0; 281646204592SSage Weil } 281746204592SSage Weil 281846204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) 281946204592SSage Weil { 282046204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 282146204592SSage Weil u64 transid; 282246204592SSage Weil 282346204592SSage Weil if (argp) { 282446204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 282546204592SSage Weil return -EFAULT; 282646204592SSage Weil } else { 282746204592SSage Weil transid = 0; /* current trans */ 282846204592SSage Weil } 282946204592SSage Weil return btrfs_wait_for_commit(root, transid); 283046204592SSage Weil } 283146204592SSage Weil 2832475f6387SJan Schmidt static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg) 2833475f6387SJan Schmidt { 2834475f6387SJan Schmidt int ret; 2835475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 2836475f6387SJan Schmidt 2837475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2838475f6387SJan Schmidt return -EPERM; 2839475f6387SJan Schmidt 2840475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 2841475f6387SJan Schmidt if (IS_ERR(sa)) 2842475f6387SJan Schmidt return PTR_ERR(sa); 2843475f6387SJan Schmidt 2844475f6387SJan Schmidt ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end, 28458628764eSArne Jansen &sa->progress, sa->flags & BTRFS_SCRUB_READONLY); 2846475f6387SJan Schmidt 2847475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 2848475f6387SJan Schmidt ret = -EFAULT; 2849475f6387SJan Schmidt 2850475f6387SJan Schmidt kfree(sa); 2851475f6387SJan Schmidt return ret; 2852475f6387SJan Schmidt } 2853475f6387SJan Schmidt 2854475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 2855475f6387SJan Schmidt { 2856475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2857475f6387SJan Schmidt return -EPERM; 2858475f6387SJan Schmidt 2859475f6387SJan Schmidt return btrfs_scrub_cancel(root); 2860475f6387SJan Schmidt } 2861475f6387SJan Schmidt 2862475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 2863475f6387SJan Schmidt void __user *arg) 2864475f6387SJan Schmidt { 2865475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 2866475f6387SJan Schmidt int ret; 2867475f6387SJan Schmidt 2868475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2869475f6387SJan Schmidt return -EPERM; 2870475f6387SJan Schmidt 2871475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 2872475f6387SJan Schmidt if (IS_ERR(sa)) 2873475f6387SJan Schmidt return PTR_ERR(sa); 2874475f6387SJan Schmidt 2875475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 2876475f6387SJan Schmidt 2877475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 2878475f6387SJan Schmidt ret = -EFAULT; 2879475f6387SJan Schmidt 2880475f6387SJan Schmidt kfree(sa); 2881475f6387SJan Schmidt return ret; 2882475f6387SJan Schmidt } 2883475f6387SJan Schmidt 2884f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 2885f46b5a66SChristoph Hellwig cmd, unsigned long arg) 2886f46b5a66SChristoph Hellwig { 2887f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 28884bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 2889f46b5a66SChristoph Hellwig 2890f46b5a66SChristoph Hellwig switch (cmd) { 28916cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 28926cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 28936cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 28946cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 28956cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 28966cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 2897f7039b1dSLi Dongyang case FITRIM: 2898f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 2899f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 2900fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 2901fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 2902fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 29033de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 2904fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 290576dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 290676dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 29070caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 29080caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 29090caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 29100caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 29116ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 29126ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 2913f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 29141e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 29151e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 29161e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 2917f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 29184bcabaa3SChristoph Hellwig return btrfs_ioctl_resize(root, argp); 2919f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 29204bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 2921f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 29224bcabaa3SChristoph Hellwig return btrfs_ioctl_rm_dev(root, argp); 2923475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 2924475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 2925475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 2926475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 2927f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 2928f46b5a66SChristoph Hellwig return btrfs_balance(root->fs_info->dev_root); 2929f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 2930c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 2931c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 29327a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 2933f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 2934f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 2935f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 2936f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 2937ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 2938ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 2939ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 2940ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 29411406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 29421406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 2943f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 2944f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 2945f46b5a66SChristoph Hellwig return 0; 294646204592SSage Weil case BTRFS_IOC_START_SYNC: 294746204592SSage Weil return btrfs_ioctl_start_sync(file, argp); 294846204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 294946204592SSage Weil return btrfs_ioctl_wait_sync(file, argp); 2950475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 2951475f6387SJan Schmidt return btrfs_ioctl_scrub(root, argp); 2952475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 2953475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 2954475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 2955475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 2956f46b5a66SChristoph Hellwig } 2957f46b5a66SChristoph Hellwig 2958f46b5a66SChristoph Hellwig return -ENOTTY; 2959f46b5a66SChristoph Hellwig } 2960