1f46b5a66SChristoph Hellwig /* 2f46b5a66SChristoph Hellwig * Copyright (C) 2007 Oracle. All rights reserved. 3f46b5a66SChristoph Hellwig * 4f46b5a66SChristoph Hellwig * This program is free software; you can redistribute it and/or 5f46b5a66SChristoph Hellwig * modify it under the terms of the GNU General Public 6f46b5a66SChristoph Hellwig * License v2 as published by the Free Software Foundation. 7f46b5a66SChristoph Hellwig * 8f46b5a66SChristoph Hellwig * This program is distributed in the hope that it will be useful, 9f46b5a66SChristoph Hellwig * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f46b5a66SChristoph Hellwig * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11f46b5a66SChristoph Hellwig * General Public License for more details. 12f46b5a66SChristoph Hellwig * 13f46b5a66SChristoph Hellwig * You should have received a copy of the GNU General Public 14f46b5a66SChristoph Hellwig * License along with this program; if not, write to the 15f46b5a66SChristoph Hellwig * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16f46b5a66SChristoph Hellwig * Boston, MA 021110-1307, USA. 17f46b5a66SChristoph Hellwig */ 18f46b5a66SChristoph Hellwig 19f46b5a66SChristoph Hellwig #include <linux/kernel.h> 20f46b5a66SChristoph Hellwig #include <linux/bio.h> 21f46b5a66SChristoph Hellwig #include <linux/buffer_head.h> 22f46b5a66SChristoph Hellwig #include <linux/file.h> 23f46b5a66SChristoph Hellwig #include <linux/fs.h> 24cb8e7090SChristoph Hellwig #include <linux/fsnotify.h> 25f46b5a66SChristoph Hellwig #include <linux/pagemap.h> 26f46b5a66SChristoph Hellwig #include <linux/highmem.h> 27f46b5a66SChristoph Hellwig #include <linux/time.h> 28f46b5a66SChristoph Hellwig #include <linux/init.h> 29f46b5a66SChristoph Hellwig #include <linux/string.h> 30f46b5a66SChristoph Hellwig #include <linux/backing-dev.h> 31cb8e7090SChristoph Hellwig #include <linux/mount.h> 32f46b5a66SChristoph Hellwig #include <linux/mpage.h> 33cb8e7090SChristoph Hellwig #include <linux/namei.h> 34f46b5a66SChristoph Hellwig #include <linux/swap.h> 35f46b5a66SChristoph Hellwig #include <linux/writeback.h> 36f46b5a66SChristoph Hellwig #include <linux/statfs.h> 37f46b5a66SChristoph Hellwig #include <linux/compat.h> 38f46b5a66SChristoph Hellwig #include <linux/bit_spinlock.h> 39cb8e7090SChristoph Hellwig #include <linux/security.h> 40f46b5a66SChristoph Hellwig #include <linux/xattr.h> 417ea394f1SYan Zheng #include <linux/vmalloc.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 43f7039b1dSLi Dongyang #include <linux/blkdev.h> 448ea05e3aSAlexander Block #include <linux/uuid.h> 4555e301fdSFilipe Brandenburger #include <linux/btrfs.h> 46416161dbSMark Fasheh #include <linux/uaccess.h> 47f46b5a66SChristoph Hellwig #include "ctree.h" 48f46b5a66SChristoph Hellwig #include "disk-io.h" 49f46b5a66SChristoph Hellwig #include "transaction.h" 50f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 51f46b5a66SChristoph Hellwig #include "print-tree.h" 52f46b5a66SChristoph Hellwig #include "volumes.h" 53925baeddSChris Mason #include "locking.h" 54581bb050SLi Zefan #include "inode-map.h" 55d7728c96SJan Schmidt #include "backref.h" 56606686eeSJosef Bacik #include "rcu-string.h" 5731db9f7cSAlexander Block #include "send.h" 583f6bcfbdSStefan Behrens #include "dev-replace.h" 593b02a68aSJeff Mahoney #include "sysfs.h" 60f46b5a66SChristoph Hellwig 61416161dbSMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 62416161dbSMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff); 63416161dbSMark Fasheh 646cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 656cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 666cbff00fSChristoph Hellwig { 676cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 686cbff00fSChristoph Hellwig return flags; 696cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 706cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 716cbff00fSChristoph Hellwig else 726cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 736cbff00fSChristoph Hellwig } 74f46b5a66SChristoph Hellwig 756cbff00fSChristoph Hellwig /* 766cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 776cbff00fSChristoph Hellwig */ 786cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 796cbff00fSChristoph Hellwig { 806cbff00fSChristoph Hellwig unsigned int iflags = 0; 816cbff00fSChristoph Hellwig 826cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 836cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 846cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 856cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 866cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 876cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 886cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 896cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 906cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 916cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 926cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 936cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 94d0092bddSLi Zefan if (flags & BTRFS_INODE_NODATACOW) 95d0092bddSLi Zefan iflags |= FS_NOCOW_FL; 96d0092bddSLi Zefan 97d0092bddSLi Zefan if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) 98d0092bddSLi Zefan iflags |= FS_COMPR_FL; 99d0092bddSLi Zefan else if (flags & BTRFS_INODE_NOCOMPRESS) 100d0092bddSLi Zefan iflags |= FS_NOCOMP_FL; 1016cbff00fSChristoph Hellwig 1026cbff00fSChristoph Hellwig return iflags; 1036cbff00fSChristoph Hellwig } 1046cbff00fSChristoph Hellwig 1056cbff00fSChristoph Hellwig /* 1066cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 1076cbff00fSChristoph Hellwig */ 1086cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 1096cbff00fSChristoph Hellwig { 1106cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1116cbff00fSChristoph Hellwig 1126cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 1136cbff00fSChristoph Hellwig 1146cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 1156cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 1166cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 1176cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1186cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1196cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1206cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1216cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1226cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1236cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1246cbff00fSChristoph Hellwig } 1256cbff00fSChristoph Hellwig 1266cbff00fSChristoph Hellwig /* 1276cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1286cbff00fSChristoph Hellwig * 129e27425d6SJosef Bacik * Currently only the compression flags and the cow flags are inherited. 1306cbff00fSChristoph Hellwig */ 1316cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1326cbff00fSChristoph Hellwig { 1330b4dcea5SChris Mason unsigned int flags; 1340b4dcea5SChris Mason 1350b4dcea5SChris Mason if (!dir) 1360b4dcea5SChris Mason return; 1370b4dcea5SChris Mason 1380b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1396cbff00fSChristoph Hellwig 140e27425d6SJosef Bacik if (flags & BTRFS_INODE_NOCOMPRESS) { 141e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; 142e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 143e27425d6SJosef Bacik } else if (flags & BTRFS_INODE_COMPRESS) { 144e27425d6SJosef Bacik BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; 145e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; 146e27425d6SJosef Bacik } 1476cbff00fSChristoph Hellwig 148213490b3SLiu Bo if (flags & BTRFS_INODE_NODATACOW) { 149e27425d6SJosef Bacik BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 150213490b3SLiu Bo if (S_ISREG(inode->i_mode)) 151213490b3SLiu Bo BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 152213490b3SLiu Bo } 153e27425d6SJosef Bacik 1546cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1556cbff00fSChristoph Hellwig } 1566cbff00fSChristoph Hellwig 1576cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1586cbff00fSChristoph Hellwig { 159496ad9aaSAl Viro struct btrfs_inode *ip = BTRFS_I(file_inode(file)); 1606cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1616cbff00fSChristoph Hellwig 1626cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1636cbff00fSChristoph Hellwig return -EFAULT; 1646cbff00fSChristoph Hellwig return 0; 1656cbff00fSChristoph Hellwig } 1666cbff00fSChristoph Hellwig 16775e7cb7fSLiu Bo static int check_flags(unsigned int flags) 16875e7cb7fSLiu Bo { 16975e7cb7fSLiu Bo if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 17075e7cb7fSLiu Bo FS_NOATIME_FL | FS_NODUMP_FL | \ 17175e7cb7fSLiu Bo FS_SYNC_FL | FS_DIRSYNC_FL | \ 172e1e8fb6aSLi Zefan FS_NOCOMP_FL | FS_COMPR_FL | 173e1e8fb6aSLi Zefan FS_NOCOW_FL)) 17475e7cb7fSLiu Bo return -EOPNOTSUPP; 17575e7cb7fSLiu Bo 17675e7cb7fSLiu Bo if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 17775e7cb7fSLiu Bo return -EINVAL; 17875e7cb7fSLiu Bo 17975e7cb7fSLiu Bo return 0; 18075e7cb7fSLiu Bo } 18175e7cb7fSLiu Bo 1826cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1836cbff00fSChristoph Hellwig { 184496ad9aaSAl Viro struct inode *inode = file_inode(file); 1856cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1866cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1876cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1886cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1896cbff00fSChristoph Hellwig int ret; 190f062abf0SLi Zefan u64 ip_oldflags; 191f062abf0SLi Zefan unsigned int i_oldflags; 1927e97b8daSDavid Sterba umode_t mode; 1936cbff00fSChristoph Hellwig 194b83cc969SLi Zefan if (btrfs_root_readonly(root)) 195b83cc969SLi Zefan return -EROFS; 196b83cc969SLi Zefan 1976cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1986cbff00fSChristoph Hellwig return -EFAULT; 1996cbff00fSChristoph Hellwig 20075e7cb7fSLiu Bo ret = check_flags(flags); 20175e7cb7fSLiu Bo if (ret) 20275e7cb7fSLiu Bo return ret; 2036cbff00fSChristoph Hellwig 2042e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 2056cbff00fSChristoph Hellwig return -EACCES; 2066cbff00fSChristoph Hellwig 207e7848683SJan Kara ret = mnt_want_write_file(file); 208e7848683SJan Kara if (ret) 209e7848683SJan Kara return ret; 210e7848683SJan Kara 2116cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 2126cbff00fSChristoph Hellwig 213f062abf0SLi Zefan ip_oldflags = ip->flags; 214f062abf0SLi Zefan i_oldflags = inode->i_flags; 2157e97b8daSDavid Sterba mode = inode->i_mode; 216f062abf0SLi Zefan 2176cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 2186cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 2196cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 2206cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 2216cbff00fSChristoph Hellwig ret = -EPERM; 2226cbff00fSChristoph Hellwig goto out_unlock; 2236cbff00fSChristoph Hellwig } 2246cbff00fSChristoph Hellwig } 2256cbff00fSChristoph Hellwig 2266cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 2276cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 2286cbff00fSChristoph Hellwig else 2296cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 2306cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 2316cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 2326cbff00fSChristoph Hellwig else 2336cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 2346cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 2356cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 2366cbff00fSChristoph Hellwig else 2376cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 2386cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 2396cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 2406cbff00fSChristoph Hellwig else 2416cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 2426cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 2436cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 2446cbff00fSChristoph Hellwig else 2456cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 2466cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2476cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2486cbff00fSChristoph Hellwig else 2496cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2507e97b8daSDavid Sterba if (flags & FS_NOCOW_FL) { 2517e97b8daSDavid Sterba if (S_ISREG(mode)) { 2527e97b8daSDavid Sterba /* 2537e97b8daSDavid Sterba * It's safe to turn csums off here, no extents exist. 2547e97b8daSDavid Sterba * Otherwise we want the flag to reflect the real COW 2557e97b8daSDavid Sterba * status of the file and will not set it. 2567e97b8daSDavid Sterba */ 2577e97b8daSDavid Sterba if (inode->i_size == 0) 2587e97b8daSDavid Sterba ip->flags |= BTRFS_INODE_NODATACOW 2597e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM; 2607e97b8daSDavid Sterba } else { 261e1e8fb6aSLi Zefan ip->flags |= BTRFS_INODE_NODATACOW; 2627e97b8daSDavid Sterba } 2637e97b8daSDavid Sterba } else { 2647e97b8daSDavid Sterba /* 2657e97b8daSDavid Sterba * Revert back under same assuptions as above 2667e97b8daSDavid Sterba */ 2677e97b8daSDavid Sterba if (S_ISREG(mode)) { 2687e97b8daSDavid Sterba if (inode->i_size == 0) 2697e97b8daSDavid Sterba ip->flags &= ~(BTRFS_INODE_NODATACOW 2707e97b8daSDavid Sterba | BTRFS_INODE_NODATASUM); 2717e97b8daSDavid Sterba } else { 272e1e8fb6aSLi Zefan ip->flags &= ~BTRFS_INODE_NODATACOW; 2737e97b8daSDavid Sterba } 2747e97b8daSDavid Sterba } 2756cbff00fSChristoph Hellwig 27675e7cb7fSLiu Bo /* 27775e7cb7fSLiu Bo * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 27875e7cb7fSLiu Bo * flag may be changed automatically if compression code won't make 27975e7cb7fSLiu Bo * things smaller. 28075e7cb7fSLiu Bo */ 28175e7cb7fSLiu Bo if (flags & FS_NOCOMP_FL) { 28275e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_COMPRESS; 28375e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_NOCOMPRESS; 28475e7cb7fSLiu Bo } else if (flags & FS_COMPR_FL) { 28575e7cb7fSLiu Bo ip->flags |= BTRFS_INODE_COMPRESS; 28675e7cb7fSLiu Bo ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 287ebcb904dSLi Zefan } else { 288ebcb904dSLi Zefan ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 28975e7cb7fSLiu Bo } 2906cbff00fSChristoph Hellwig 2914da6f1a3SLi Zefan trans = btrfs_start_transaction(root, 1); 292f062abf0SLi Zefan if (IS_ERR(trans)) { 293f062abf0SLi Zefan ret = PTR_ERR(trans); 294f062abf0SLi Zefan goto out_drop; 295f062abf0SLi Zefan } 2966cbff00fSChristoph Hellwig 297306424ccSLi Zefan btrfs_update_iflags(inode); 2980c4d2d95SJosef Bacik inode_inc_iversion(inode); 299306424ccSLi Zefan inode->i_ctime = CURRENT_TIME; 3006cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 3016cbff00fSChristoph Hellwig 3026cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 303f062abf0SLi Zefan out_drop: 304f062abf0SLi Zefan if (ret) { 305f062abf0SLi Zefan ip->flags = ip_oldflags; 306f062abf0SLi Zefan inode->i_flags = i_oldflags; 307f062abf0SLi Zefan } 3086cbff00fSChristoph Hellwig 3096cbff00fSChristoph Hellwig out_unlock: 3106cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 311e7848683SJan Kara mnt_drop_write_file(file); 3122d4e6f6aSliubo return ret; 3136cbff00fSChristoph Hellwig } 3146cbff00fSChristoph Hellwig 3156cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 3166cbff00fSChristoph Hellwig { 317496ad9aaSAl Viro struct inode *inode = file_inode(file); 3186cbff00fSChristoph Hellwig 3196cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 3206cbff00fSChristoph Hellwig } 321f46b5a66SChristoph Hellwig 322f7039b1dSLi Dongyang static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 323f7039b1dSLi Dongyang { 32454563d41SAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 325f7039b1dSLi Dongyang struct btrfs_device *device; 326f7039b1dSLi Dongyang struct request_queue *q; 327f7039b1dSLi Dongyang struct fstrim_range range; 328f7039b1dSLi Dongyang u64 minlen = ULLONG_MAX; 329f7039b1dSLi Dongyang u64 num_devices = 0; 330815745cfSAl Viro u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); 331f7039b1dSLi Dongyang int ret; 332f7039b1dSLi Dongyang 333f7039b1dSLi Dongyang if (!capable(CAP_SYS_ADMIN)) 334f7039b1dSLi Dongyang return -EPERM; 335f7039b1dSLi Dongyang 3361f78160cSXiao Guangrong rcu_read_lock(); 3371f78160cSXiao Guangrong list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, 3381f78160cSXiao Guangrong dev_list) { 339f7039b1dSLi Dongyang if (!device->bdev) 340f7039b1dSLi Dongyang continue; 341f7039b1dSLi Dongyang q = bdev_get_queue(device->bdev); 342f7039b1dSLi Dongyang if (blk_queue_discard(q)) { 343f7039b1dSLi Dongyang num_devices++; 344f7039b1dSLi Dongyang minlen = min((u64)q->limits.discard_granularity, 345f7039b1dSLi Dongyang minlen); 346f7039b1dSLi Dongyang } 347f7039b1dSLi Dongyang } 3481f78160cSXiao Guangrong rcu_read_unlock(); 349f4c697e6SLukas Czerner 350f7039b1dSLi Dongyang if (!num_devices) 351f7039b1dSLi Dongyang return -EOPNOTSUPP; 352f7039b1dSLi Dongyang if (copy_from_user(&range, arg, sizeof(range))) 353f7039b1dSLi Dongyang return -EFAULT; 354e515c18bSLukas Czerner if (range.start > total_bytes || 355e515c18bSLukas Czerner range.len < fs_info->sb->s_blocksize) 356f4c697e6SLukas Czerner return -EINVAL; 357f7039b1dSLi Dongyang 358f4c697e6SLukas Czerner range.len = min(range.len, total_bytes - range.start); 359f7039b1dSLi Dongyang range.minlen = max(range.minlen, minlen); 360815745cfSAl Viro ret = btrfs_trim_fs(fs_info->tree_root, &range); 361f7039b1dSLi Dongyang if (ret < 0) 362f7039b1dSLi Dongyang return ret; 363f7039b1dSLi Dongyang 364f7039b1dSLi Dongyang if (copy_to_user(arg, &range, sizeof(range))) 365f7039b1dSLi Dongyang return -EFAULT; 366f7039b1dSLi Dongyang 367f7039b1dSLi Dongyang return 0; 368f7039b1dSLi Dongyang } 369f7039b1dSLi Dongyang 370dd5f9615SStefan Behrens int btrfs_is_empty_uuid(u8 *uuid) 371dd5f9615SStefan Behrens { 37246e0f66aSChris Mason int i; 37346e0f66aSChris Mason 37446e0f66aSChris Mason for (i = 0; i < BTRFS_UUID_SIZE; i++) { 37546e0f66aSChris Mason if (uuid[i]) 37646e0f66aSChris Mason return 0; 37746e0f66aSChris Mason } 37846e0f66aSChris Mason return 1; 379dd5f9615SStefan Behrens } 380dd5f9615SStefan Behrens 381d5c12070SMiao Xie static noinline int create_subvol(struct inode *dir, 382cb8e7090SChristoph Hellwig struct dentry *dentry, 38372fd032eSSage Weil char *name, int namelen, 3846f72c7e2SArne Jansen u64 *async_transid, 3858696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 386f46b5a66SChristoph Hellwig { 387f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 388f46b5a66SChristoph Hellwig struct btrfs_key key; 389f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 390f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 391f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 392d5c12070SMiao Xie struct btrfs_root *root = BTRFS_I(dir)->root; 39376dda93cSYan, Zheng struct btrfs_root *new_root; 394d5c12070SMiao Xie struct btrfs_block_rsv block_rsv; 3958ea05e3aSAlexander Block struct timespec cur_time = CURRENT_TIME; 3965662344bSTsutomu Itoh struct inode *inode; 397f46b5a66SChristoph Hellwig int ret; 398f46b5a66SChristoph Hellwig int err; 399f46b5a66SChristoph Hellwig u64 objectid; 400f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 4013de4586cSChris Mason u64 index = 0; 402d5c12070SMiao Xie u64 qgroup_reserved; 4038ea05e3aSAlexander Block uuid_le new_uuid; 404f46b5a66SChristoph Hellwig 405581bb050SLi Zefan ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 4062fbe8c8aSAl Viro if (ret) 407a22285a6SYan, Zheng return ret; 4086a912213SJosef Bacik 409d5c12070SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 4109ed74f2dSJosef Bacik /* 411d5c12070SMiao Xie * The same as the snapshot creation, please see the comment 412d5c12070SMiao Xie * of create_snapshot(). 4139ed74f2dSJosef Bacik */ 414d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 415dd5f9615SStefan Behrens 8, &qgroup_reserved, false); 416d5c12070SMiao Xie if (ret) 417d5c12070SMiao Xie return ret; 418f46b5a66SChristoph Hellwig 419d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 420d5c12070SMiao Xie if (IS_ERR(trans)) { 421d5c12070SMiao Xie ret = PTR_ERR(trans); 422d5c12070SMiao Xie goto out; 423d5c12070SMiao Xie } 424d5c12070SMiao Xie trans->block_rsv = &block_rsv; 425d5c12070SMiao Xie trans->bytes_reserved = block_rsv.size; 426f46b5a66SChristoph Hellwig 4278696c533SMiao Xie ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 4286f72c7e2SArne Jansen if (ret) 4296f72c7e2SArne Jansen goto fail; 4306f72c7e2SArne Jansen 4315d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 4325581a51aSJan Schmidt 0, objectid, NULL, 0, 0, 0); 4338e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 4348e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 4358e8a1e31SJosef Bacik goto fail; 4368e8a1e31SJosef Bacik } 437f46b5a66SChristoph Hellwig 4385d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 439f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 440f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 4415d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 442f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 443f46b5a66SChristoph Hellwig 4440a4e5586SRoss Kirk write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(), 445f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 4465d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 447b308bc2fSGeert Uytterhoeven btrfs_header_chunk_tree_uuid(leaf), 4485d4f98a2SYan Zheng BTRFS_UUID_SIZE); 449f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 450f46b5a66SChristoph Hellwig 4518ea05e3aSAlexander Block memset(&root_item, 0, sizeof(root_item)); 4528ea05e3aSAlexander Block 453f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 4543cae210fSQu Wenruo btrfs_set_stack_inode_generation(inode_item, 1); 4553cae210fSQu Wenruo btrfs_set_stack_inode_size(inode_item, 3); 4563cae210fSQu Wenruo btrfs_set_stack_inode_nlink(inode_item, 1); 4573cae210fSQu Wenruo btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); 4583cae210fSQu Wenruo btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 459f46b5a66SChristoph Hellwig 4603cae210fSQu Wenruo btrfs_set_root_flags(&root_item, 0); 4613cae210fSQu Wenruo btrfs_set_root_limit(&root_item, 0); 4623cae210fSQu Wenruo btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); 46308fe4db1SLi Zefan 464f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 46584234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 466f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 467f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 46886b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 46980ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 470f46b5a66SChristoph Hellwig 4718ea05e3aSAlexander Block btrfs_set_root_generation_v2(&root_item, 4728ea05e3aSAlexander Block btrfs_root_generation(&root_item)); 4738ea05e3aSAlexander Block uuid_le_gen(&new_uuid); 4748ea05e3aSAlexander Block memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); 4753cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); 4763cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); 4778ea05e3aSAlexander Block root_item.ctime = root_item.otime; 4788ea05e3aSAlexander Block btrfs_set_root_ctransid(&root_item, trans->transid); 4798ea05e3aSAlexander Block btrfs_set_root_otransid(&root_item, trans->transid); 480f46b5a66SChristoph Hellwig 481925baeddSChris Mason btrfs_tree_unlock(leaf); 482f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 483f46b5a66SChristoph Hellwig leaf = NULL; 484f46b5a66SChristoph Hellwig 485f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 486f46b5a66SChristoph Hellwig 487f46b5a66SChristoph Hellwig key.objectid = objectid; 4885d4f98a2SYan Zheng key.offset = 0; 489f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 490f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 491f46b5a66SChristoph Hellwig &root_item); 492f46b5a66SChristoph Hellwig if (ret) 493f46b5a66SChristoph Hellwig goto fail; 494f46b5a66SChristoph Hellwig 49576dda93cSYan, Zheng key.offset = (u64)-1; 49676dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 49779787eaaSJeff Mahoney if (IS_ERR(new_root)) { 49879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); 49979787eaaSJeff Mahoney ret = PTR_ERR(new_root); 50079787eaaSJeff Mahoney goto fail; 50179787eaaSJeff Mahoney } 50276dda93cSYan, Zheng 50376dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 50476dda93cSYan, Zheng 505d82a6f1dSJosef Bacik ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 506ce598979SMark Fasheh if (ret) { 507ce598979SMark Fasheh /* We potentially lose an unused inode item here */ 50879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 509ce598979SMark Fasheh goto fail; 510ce598979SMark Fasheh } 511ce598979SMark Fasheh 512f46b5a66SChristoph Hellwig /* 513f46b5a66SChristoph Hellwig * insert the directory item 514f46b5a66SChristoph Hellwig */ 5153de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 51679787eaaSJeff Mahoney if (ret) { 51779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 51879787eaaSJeff Mahoney goto fail; 51979787eaaSJeff Mahoney } 5203de4586cSChris Mason 5213de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 52216cdcec7SMiao Xie name, namelen, dir, &key, 5233de4586cSChris Mason BTRFS_FT_DIR, index); 52479787eaaSJeff Mahoney if (ret) { 52579787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 526f46b5a66SChristoph Hellwig goto fail; 52779787eaaSJeff Mahoney } 5280660b5afSChris Mason 52952c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 53052c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 53152c26179SYan Zheng BUG_ON(ret); 53252c26179SYan Zheng 5330660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 5344df27c4dSYan, Zheng objectid, root->root_key.objectid, 53533345d01SLi Zefan btrfs_ino(dir), index, name, namelen); 5360660b5afSChris Mason BUG_ON(ret); 5370660b5afSChris Mason 538dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 539dd5f9615SStefan Behrens root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 540dd5f9615SStefan Behrens objectid); 541dd5f9615SStefan Behrens if (ret) 542dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 543dd5f9615SStefan Behrens 544f46b5a66SChristoph Hellwig fail: 545d5c12070SMiao Xie trans->block_rsv = NULL; 546d5c12070SMiao Xie trans->bytes_reserved = 0; 54772fd032eSSage Weil if (async_transid) { 54872fd032eSSage Weil *async_transid = trans->transid; 54972fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 55000d71c9cSMiao Xie if (err) 55100d71c9cSMiao Xie err = btrfs_commit_transaction(trans, root); 55272fd032eSSage Weil } else { 55376dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 55472fd032eSSage Weil } 555f46b5a66SChristoph Hellwig if (err && !ret) 556f46b5a66SChristoph Hellwig ret = err; 5571a65e24bSChris Mason 5585662344bSTsutomu Itoh if (!ret) { 5595662344bSTsutomu Itoh inode = btrfs_lookup_dentry(dir, dentry); 5605662344bSTsutomu Itoh if (IS_ERR(inode)) { 5615662344bSTsutomu Itoh ret = PTR_ERR(inode); 5625662344bSTsutomu Itoh goto out; 5635662344bSTsutomu Itoh } 5645662344bSTsutomu Itoh d_instantiate(dentry, inode); 5655662344bSTsutomu Itoh } 566d5c12070SMiao Xie out: 567d5c12070SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 568f46b5a66SChristoph Hellwig return ret; 569f46b5a66SChristoph Hellwig } 570f46b5a66SChristoph Hellwig 571e9662f70SMiao Xie static int create_snapshot(struct btrfs_root *root, struct inode *dir, 572e9662f70SMiao Xie struct dentry *dentry, char *name, int namelen, 573e9662f70SMiao Xie u64 *async_transid, bool readonly, 574e9662f70SMiao Xie struct btrfs_qgroup_inherit *inherit) 575f46b5a66SChristoph Hellwig { 5762e4bfab9SYan, Zheng struct inode *inode; 577f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 578f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 5792e4bfab9SYan, Zheng int ret; 580f46b5a66SChristoph Hellwig 581f46b5a66SChristoph Hellwig if (!root->ref_cows) 582f46b5a66SChristoph Hellwig return -EINVAL; 583f46b5a66SChristoph Hellwig 5846a03843dSMiao Xie ret = btrfs_start_delalloc_inodes(root, 0); 5856a03843dSMiao Xie if (ret) 5866a03843dSMiao Xie return ret; 5876a03843dSMiao Xie 588b0244199SMiao Xie btrfs_wait_ordered_extents(root, -1); 5896a03843dSMiao Xie 590a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 591a22285a6SYan, Zheng if (!pending_snapshot) 592a22285a6SYan, Zheng return -ENOMEM; 593a22285a6SYan, Zheng 59466d8f3ddSMiao Xie btrfs_init_block_rsv(&pending_snapshot->block_rsv, 59566d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 596d5c12070SMiao Xie /* 597d5c12070SMiao Xie * 1 - parent dir inode 598d5c12070SMiao Xie * 2 - dir entries 599d5c12070SMiao Xie * 1 - root item 600d5c12070SMiao Xie * 2 - root ref/backref 601d5c12070SMiao Xie * 1 - root of snapshot 602dd5f9615SStefan Behrens * 1 - UUID item 603d5c12070SMiao Xie */ 604d5c12070SMiao Xie ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, 605dd5f9615SStefan Behrens &pending_snapshot->block_rsv, 8, 606ee3441b4SJeff Mahoney &pending_snapshot->qgroup_reserved, 607ee3441b4SJeff Mahoney false); 608d5c12070SMiao Xie if (ret) 609d5c12070SMiao Xie goto out; 610d5c12070SMiao Xie 611a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 612a22285a6SYan, Zheng pending_snapshot->root = root; 613b83cc969SLi Zefan pending_snapshot->readonly = readonly; 614e9662f70SMiao Xie pending_snapshot->dir = dir; 6158696c533SMiao Xie pending_snapshot->inherit = inherit; 616a22285a6SYan, Zheng 617d5c12070SMiao Xie trans = btrfs_start_transaction(root, 0); 618a22285a6SYan, Zheng if (IS_ERR(trans)) { 619a22285a6SYan, Zheng ret = PTR_ERR(trans); 620a22285a6SYan, Zheng goto fail; 621a22285a6SYan, Zheng } 622a22285a6SYan, Zheng 6238351583eSJosef Bacik spin_lock(&root->fs_info->trans_lock); 624a22285a6SYan, Zheng list_add(&pending_snapshot->list, 625a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 6268351583eSJosef Bacik spin_unlock(&root->fs_info->trans_lock); 62772fd032eSSage Weil if (async_transid) { 62872fd032eSSage Weil *async_transid = trans->transid; 62972fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 63072fd032eSSage Weil root->fs_info->extent_root, 1); 63100d71c9cSMiao Xie if (ret) 63200d71c9cSMiao Xie ret = btrfs_commit_transaction(trans, root); 63372fd032eSSage Weil } else { 63472fd032eSSage Weil ret = btrfs_commit_transaction(trans, 63572fd032eSSage Weil root->fs_info->extent_root); 63672fd032eSSage Weil } 637aec8030aSMiao Xie if (ret) 638c37b2b62SJosef Bacik goto fail; 639a22285a6SYan, Zheng 640a22285a6SYan, Zheng ret = pending_snapshot->error; 641f46b5a66SChristoph Hellwig if (ret) 6422e4bfab9SYan, Zheng goto fail; 643f46b5a66SChristoph Hellwig 64466b4ffd1SJosef Bacik ret = btrfs_orphan_cleanup(pending_snapshot->snap); 64566b4ffd1SJosef Bacik if (ret) 64666b4ffd1SJosef Bacik goto fail; 647f46b5a66SChristoph Hellwig 6482fbe8c8aSAl Viro inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); 6492e4bfab9SYan, Zheng if (IS_ERR(inode)) { 6502e4bfab9SYan, Zheng ret = PTR_ERR(inode); 6512e4bfab9SYan, Zheng goto fail; 6522e4bfab9SYan, Zheng } 6535662344bSTsutomu Itoh 6542e4bfab9SYan, Zheng d_instantiate(dentry, inode); 6552e4bfab9SYan, Zheng ret = 0; 6562e4bfab9SYan, Zheng fail: 657d5c12070SMiao Xie btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, 658d5c12070SMiao Xie &pending_snapshot->block_rsv, 659d5c12070SMiao Xie pending_snapshot->qgroup_reserved); 660d5c12070SMiao Xie out: 661a22285a6SYan, Zheng kfree(pending_snapshot); 662f46b5a66SChristoph Hellwig return ret; 663f46b5a66SChristoph Hellwig } 664f46b5a66SChristoph Hellwig 6654260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 6664260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 6674260f7c7SSage Weil * minimal. 6684260f7c7SSage Weil */ 6694260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 6704260f7c7SSage Weil { 6712f2f43d3SEric W. Biederman kuid_t fsuid = current_fsuid(); 6724260f7c7SSage Weil 6734260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 6744260f7c7SSage Weil return 0; 6752f2f43d3SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 6764260f7c7SSage Weil return 0; 6772f2f43d3SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 6784260f7c7SSage Weil return 0; 6794260f7c7SSage Weil return !capable(CAP_FOWNER); 6804260f7c7SSage Weil } 6814260f7c7SSage Weil 6824260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 6834260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 6844260f7c7SSage Weil * whether the type of victim is right. 6854260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 6864260f7c7SSage Weil * 2. We should have write and exec permissions on dir 6874260f7c7SSage Weil * 3. We can't remove anything from append-only dir 6884260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 6894260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 6904260f7c7SSage Weil * a. be owner of dir, or 6914260f7c7SSage Weil * b. be owner of victim, or 6924260f7c7SSage Weil * c. have CAP_FOWNER capability 6934260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 6944260f7c7SSage Weil * links pointing to it. 6954260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 6964260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 6974260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 6984260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 6994260f7c7SSage Weil * nfs_async_unlink(). 7004260f7c7SSage Weil */ 7014260f7c7SSage Weil 7024260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) 7034260f7c7SSage Weil { 7044260f7c7SSage Weil int error; 7054260f7c7SSage Weil 7064260f7c7SSage Weil if (!victim->d_inode) 7074260f7c7SSage Weil return -ENOENT; 7084260f7c7SSage Weil 7094260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 7104fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 7114260f7c7SSage Weil 7124260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 7134260f7c7SSage Weil if (error) 7144260f7c7SSage Weil return error; 7154260f7c7SSage Weil if (IS_APPEND(dir)) 7164260f7c7SSage Weil return -EPERM; 7174260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 7184260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 7194260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 7204260f7c7SSage Weil return -EPERM; 7214260f7c7SSage Weil if (isdir) { 7224260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 7234260f7c7SSage Weil return -ENOTDIR; 7244260f7c7SSage Weil if (IS_ROOT(victim)) 7254260f7c7SSage Weil return -EBUSY; 7264260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 7274260f7c7SSage Weil return -EISDIR; 7284260f7c7SSage Weil if (IS_DEADDIR(dir)) 7294260f7c7SSage Weil return -ENOENT; 7304260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 7314260f7c7SSage Weil return -EBUSY; 7324260f7c7SSage Weil return 0; 7334260f7c7SSage Weil } 7344260f7c7SSage Weil 735cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 736cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 737cb8e7090SChristoph Hellwig { 738cb8e7090SChristoph Hellwig if (child->d_inode) 739cb8e7090SChristoph Hellwig return -EEXIST; 740cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 741cb8e7090SChristoph Hellwig return -ENOENT; 742cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 743cb8e7090SChristoph Hellwig } 744cb8e7090SChristoph Hellwig 745cb8e7090SChristoph Hellwig /* 746cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 747cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 748cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 749cb8e7090SChristoph Hellwig */ 75076dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 75176dda93cSYan, Zheng char *name, int namelen, 75272fd032eSSage Weil struct btrfs_root *snap_src, 7536f72c7e2SArne Jansen u64 *async_transid, bool readonly, 7548696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 755cb8e7090SChristoph Hellwig { 75676dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 757cb8e7090SChristoph Hellwig struct dentry *dentry; 758cb8e7090SChristoph Hellwig int error; 759cb8e7090SChristoph Hellwig 7605c50c9b8SDavid Sterba error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 7615c50c9b8SDavid Sterba if (error == -EINTR) 7625c50c9b8SDavid Sterba return error; 763cb8e7090SChristoph Hellwig 764cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 765cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 766cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 767cb8e7090SChristoph Hellwig goto out_unlock; 768cb8e7090SChristoph Hellwig 769cb8e7090SChristoph Hellwig error = -EEXIST; 770cb8e7090SChristoph Hellwig if (dentry->d_inode) 771cb8e7090SChristoph Hellwig goto out_dput; 772cb8e7090SChristoph Hellwig 77376dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 774cb8e7090SChristoph Hellwig if (error) 775a874a63eSLiu Bo goto out_dput; 776cb8e7090SChristoph Hellwig 7779c52057cSChris Mason /* 7789c52057cSChris Mason * even if this name doesn't exist, we may get hash collisions. 7799c52057cSChris Mason * check for them now when we can safely fail 7809c52057cSChris Mason */ 7819c52057cSChris Mason error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, 7829c52057cSChris Mason dir->i_ino, name, 7839c52057cSChris Mason namelen); 7849c52057cSChris Mason if (error) 7859c52057cSChris Mason goto out_dput; 7869c52057cSChris Mason 78776dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 78876dda93cSYan, Zheng 78976dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 79076dda93cSYan, Zheng goto out_up_read; 79176dda93cSYan, Zheng 7923de4586cSChris Mason if (snap_src) { 793e9662f70SMiao Xie error = create_snapshot(snap_src, dir, dentry, name, namelen, 7946f72c7e2SArne Jansen async_transid, readonly, inherit); 7953de4586cSChris Mason } else { 796d5c12070SMiao Xie error = create_subvol(dir, dentry, name, namelen, 797d5c12070SMiao Xie async_transid, inherit); 7983de4586cSChris Mason } 79976dda93cSYan, Zheng if (!error) 80076dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 80176dda93cSYan, Zheng out_up_read: 80276dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 803cb8e7090SChristoph Hellwig out_dput: 804cb8e7090SChristoph Hellwig dput(dentry); 805cb8e7090SChristoph Hellwig out_unlock: 80676dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 807cb8e7090SChristoph Hellwig return error; 808cb8e7090SChristoph Hellwig } 809cb8e7090SChristoph Hellwig 8104cb5300bSChris Mason /* 8114cb5300bSChris Mason * When we're defragging a range, we don't want to kick it off again 8124cb5300bSChris Mason * if it is really just waiting for delalloc to send it down. 8134cb5300bSChris Mason * If we find a nice big extent or delalloc range for the bytes in the 8144cb5300bSChris Mason * file you want to defrag, we return 0 to let you know to skip this 8154cb5300bSChris Mason * part of the file 8164cb5300bSChris Mason */ 8174cb5300bSChris Mason static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) 8184cb5300bSChris Mason { 8194cb5300bSChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8204cb5300bSChris Mason struct extent_map *em = NULL; 8214cb5300bSChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 8224cb5300bSChris Mason u64 end; 8234cb5300bSChris Mason 8244cb5300bSChris Mason read_lock(&em_tree->lock); 8254cb5300bSChris Mason em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 8264cb5300bSChris Mason read_unlock(&em_tree->lock); 8274cb5300bSChris Mason 8284cb5300bSChris Mason if (em) { 8294cb5300bSChris Mason end = extent_map_end(em); 8304cb5300bSChris Mason free_extent_map(em); 8314cb5300bSChris Mason if (end - offset > thresh) 8324cb5300bSChris Mason return 0; 8334cb5300bSChris Mason } 8344cb5300bSChris Mason /* if we already have a nice delalloc here, just stop */ 8354cb5300bSChris Mason thresh /= 2; 8364cb5300bSChris Mason end = count_range_bits(io_tree, &offset, offset + thresh, 8374cb5300bSChris Mason thresh, EXTENT_DELALLOC, 1); 8384cb5300bSChris Mason if (end >= thresh) 8394cb5300bSChris Mason return 0; 8404cb5300bSChris Mason return 1; 8414cb5300bSChris Mason } 8424cb5300bSChris Mason 8434cb5300bSChris Mason /* 8444cb5300bSChris Mason * helper function to walk through a file and find extents 8454cb5300bSChris Mason * newer than a specific transid, and smaller than thresh. 8464cb5300bSChris Mason * 8474cb5300bSChris Mason * This is used by the defragging code to find new and small 8484cb5300bSChris Mason * extents 8494cb5300bSChris Mason */ 8504cb5300bSChris Mason static int find_new_extents(struct btrfs_root *root, 8514cb5300bSChris Mason struct inode *inode, u64 newer_than, 8524cb5300bSChris Mason u64 *off, int thresh) 8534cb5300bSChris Mason { 8544cb5300bSChris Mason struct btrfs_path *path; 8554cb5300bSChris Mason struct btrfs_key min_key; 8564cb5300bSChris Mason struct extent_buffer *leaf; 8574cb5300bSChris Mason struct btrfs_file_extent_item *extent; 8584cb5300bSChris Mason int type; 8594cb5300bSChris Mason int ret; 860a4689d2bSDavid Sterba u64 ino = btrfs_ino(inode); 8614cb5300bSChris Mason 8624cb5300bSChris Mason path = btrfs_alloc_path(); 8634cb5300bSChris Mason if (!path) 8644cb5300bSChris Mason return -ENOMEM; 8654cb5300bSChris Mason 866a4689d2bSDavid Sterba min_key.objectid = ino; 8674cb5300bSChris Mason min_key.type = BTRFS_EXTENT_DATA_KEY; 8684cb5300bSChris Mason min_key.offset = *off; 8694cb5300bSChris Mason 8704cb5300bSChris Mason path->keep_locks = 1; 8714cb5300bSChris Mason 8724cb5300bSChris Mason while (1) { 8736174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &min_key, path, newer_than); 8744cb5300bSChris Mason if (ret != 0) 8754cb5300bSChris Mason goto none; 876a4689d2bSDavid Sterba if (min_key.objectid != ino) 8774cb5300bSChris Mason goto none; 8784cb5300bSChris Mason if (min_key.type != BTRFS_EXTENT_DATA_KEY) 8794cb5300bSChris Mason goto none; 8804cb5300bSChris Mason 8814cb5300bSChris Mason leaf = path->nodes[0]; 8824cb5300bSChris Mason extent = btrfs_item_ptr(leaf, path->slots[0], 8834cb5300bSChris Mason struct btrfs_file_extent_item); 8844cb5300bSChris Mason 8854cb5300bSChris Mason type = btrfs_file_extent_type(leaf, extent); 8864cb5300bSChris Mason if (type == BTRFS_FILE_EXTENT_REG && 8874cb5300bSChris Mason btrfs_file_extent_num_bytes(leaf, extent) < thresh && 8884cb5300bSChris Mason check_defrag_in_cache(inode, min_key.offset, thresh)) { 8894cb5300bSChris Mason *off = min_key.offset; 8904cb5300bSChris Mason btrfs_free_path(path); 8914cb5300bSChris Mason return 0; 8924cb5300bSChris Mason } 8934cb5300bSChris Mason 8944cb5300bSChris Mason if (min_key.offset == (u64)-1) 8954cb5300bSChris Mason goto none; 8964cb5300bSChris Mason 8974cb5300bSChris Mason min_key.offset++; 8984cb5300bSChris Mason btrfs_release_path(path); 8994cb5300bSChris Mason } 9004cb5300bSChris Mason none: 9014cb5300bSChris Mason btrfs_free_path(path); 9024cb5300bSChris Mason return -ENOENT; 9034cb5300bSChris Mason } 9044cb5300bSChris Mason 9056c282eb4SLi Zefan static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) 90617ce6ef8SLiu Bo { 90717ce6ef8SLiu Bo struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 908940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 9096c282eb4SLi Zefan struct extent_map *em; 9106c282eb4SLi Zefan u64 len = PAGE_CACHE_SIZE; 911940100a4SChris Mason 912940100a4SChris Mason /* 913940100a4SChris Mason * hopefully we have this extent in the tree already, try without 914940100a4SChris Mason * the full extent lock 915940100a4SChris Mason */ 916940100a4SChris Mason read_lock(&em_tree->lock); 917940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 918940100a4SChris Mason read_unlock(&em_tree->lock); 919940100a4SChris Mason 920940100a4SChris Mason if (!em) { 921940100a4SChris Mason /* get the big lock and read metadata off disk */ 922d0082371SJeff Mahoney lock_extent(io_tree, start, start + len - 1); 923940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 924d0082371SJeff Mahoney unlock_extent(io_tree, start, start + len - 1); 925940100a4SChris Mason 9266cf8bfbfSDan Carpenter if (IS_ERR(em)) 9276c282eb4SLi Zefan return NULL; 928940100a4SChris Mason } 929940100a4SChris Mason 9306c282eb4SLi Zefan return em; 9316c282eb4SLi Zefan } 9326c282eb4SLi Zefan 9336c282eb4SLi Zefan static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) 9346c282eb4SLi Zefan { 9356c282eb4SLi Zefan struct extent_map *next; 9366c282eb4SLi Zefan bool ret = true; 9376c282eb4SLi Zefan 9386c282eb4SLi Zefan /* this is the last extent */ 9396c282eb4SLi Zefan if (em->start + em->len >= i_size_read(inode)) 9406c282eb4SLi Zefan return false; 9416c282eb4SLi Zefan 9426c282eb4SLi Zefan next = defrag_lookup_extent(inode, em->start + em->len); 9436c282eb4SLi Zefan if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) 9446c282eb4SLi Zefan ret = false; 9456c282eb4SLi Zefan 9466c282eb4SLi Zefan free_extent_map(next); 9476c282eb4SLi Zefan return ret; 9486c282eb4SLi Zefan } 9496c282eb4SLi Zefan 9506c282eb4SLi Zefan static int should_defrag_range(struct inode *inode, u64 start, int thresh, 951a43a2111SAndrew Mahone u64 *last_len, u64 *skip, u64 *defrag_end, 952a43a2111SAndrew Mahone int compress) 9536c282eb4SLi Zefan { 9546c282eb4SLi Zefan struct extent_map *em; 9556c282eb4SLi Zefan int ret = 1; 9566c282eb4SLi Zefan bool next_mergeable = true; 9576c282eb4SLi Zefan 9586c282eb4SLi Zefan /* 9596c282eb4SLi Zefan * make sure that once we start defragging an extent, we keep on 9606c282eb4SLi Zefan * defragging it 9616c282eb4SLi Zefan */ 9626c282eb4SLi Zefan if (start < *defrag_end) 9636c282eb4SLi Zefan return 1; 9646c282eb4SLi Zefan 9656c282eb4SLi Zefan *skip = 0; 9666c282eb4SLi Zefan 9676c282eb4SLi Zefan em = defrag_lookup_extent(inode, start); 9686c282eb4SLi Zefan if (!em) 9696c282eb4SLi Zefan return 0; 9706c282eb4SLi Zefan 971940100a4SChris Mason /* this will cover holes, and inline extents */ 97217ce6ef8SLiu Bo if (em->block_start >= EXTENT_MAP_LAST_BYTE) { 973940100a4SChris Mason ret = 0; 97417ce6ef8SLiu Bo goto out; 97517ce6ef8SLiu Bo } 97617ce6ef8SLiu Bo 9776c282eb4SLi Zefan next_mergeable = defrag_check_next_extent(inode, em); 978940100a4SChris Mason 979940100a4SChris Mason /* 9806c282eb4SLi Zefan * we hit a real extent, if it is big or the next extent is not a 9816c282eb4SLi Zefan * real extent, don't bother defragging it 982940100a4SChris Mason */ 983a43a2111SAndrew Mahone if (!compress && (*last_len == 0 || *last_len >= thresh) && 9846c282eb4SLi Zefan (em->len >= thresh || !next_mergeable)) 985940100a4SChris Mason ret = 0; 98617ce6ef8SLiu Bo out: 987940100a4SChris Mason /* 988940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 989940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 990940100a4SChris Mason * so that the next tiny extent will force a defrag. 991940100a4SChris Mason * 992940100a4SChris Mason * The end result of this is that tiny extents before a single big 993940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 994940100a4SChris Mason */ 995940100a4SChris Mason if (ret) { 996940100a4SChris Mason *defrag_end = extent_map_end(em); 997940100a4SChris Mason } else { 998940100a4SChris Mason *last_len = 0; 999940100a4SChris Mason *skip = extent_map_end(em); 1000940100a4SChris Mason *defrag_end = 0; 1001940100a4SChris Mason } 1002940100a4SChris Mason 1003940100a4SChris Mason free_extent_map(em); 1004940100a4SChris Mason return ret; 1005940100a4SChris Mason } 1006940100a4SChris Mason 10074cb5300bSChris Mason /* 10084cb5300bSChris Mason * it doesn't do much good to defrag one or two pages 10094cb5300bSChris Mason * at a time. This pulls in a nice chunk of pages 10104cb5300bSChris Mason * to COW and defrag. 10114cb5300bSChris Mason * 10124cb5300bSChris Mason * It also makes sure the delalloc code has enough 10134cb5300bSChris Mason * dirty data to avoid making new small extents as part 10144cb5300bSChris Mason * of the defrag 10154cb5300bSChris Mason * 10164cb5300bSChris Mason * It's a good idea to start RA on this range 10174cb5300bSChris Mason * before calling this. 10184cb5300bSChris Mason */ 10194cb5300bSChris Mason static int cluster_pages_for_defrag(struct inode *inode, 10204cb5300bSChris Mason struct page **pages, 10214cb5300bSChris Mason unsigned long start_index, 10224cb5300bSChris Mason int num_pages) 1023f46b5a66SChristoph Hellwig { 10244cb5300bSChris Mason unsigned long file_end; 10254cb5300bSChris Mason u64 isize = i_size_read(inode); 1026f46b5a66SChristoph Hellwig u64 page_start; 1027f46b5a66SChristoph Hellwig u64 page_end; 10281f12bd06SLiu Bo u64 page_cnt; 10294cb5300bSChris Mason int ret; 10304cb5300bSChris Mason int i; 10314cb5300bSChris Mason int i_done; 10324cb5300bSChris Mason struct btrfs_ordered_extent *ordered; 10334cb5300bSChris Mason struct extent_state *cached_state = NULL; 1034600a45e1SMiao Xie struct extent_io_tree *tree; 10353b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 10364cb5300bSChris Mason 10374cb5300bSChris Mason file_end = (isize - 1) >> PAGE_CACHE_SHIFT; 10381f12bd06SLiu Bo if (!isize || start_index > file_end) 10391f12bd06SLiu Bo return 0; 10401f12bd06SLiu Bo 10411f12bd06SLiu Bo page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); 10424cb5300bSChris Mason 10434cb5300bSChris Mason ret = btrfs_delalloc_reserve_space(inode, 10441f12bd06SLiu Bo page_cnt << PAGE_CACHE_SHIFT); 10454cb5300bSChris Mason if (ret) 10464cb5300bSChris Mason return ret; 10474cb5300bSChris Mason i_done = 0; 1048600a45e1SMiao Xie tree = &BTRFS_I(inode)->io_tree; 10494cb5300bSChris Mason 10504cb5300bSChris Mason /* step one, lock all the pages */ 10511f12bd06SLiu Bo for (i = 0; i < page_cnt; i++) { 10524cb5300bSChris Mason struct page *page; 1053600a45e1SMiao Xie again: 1054a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, 10553b16a4e3SJosef Bacik start_index + i, mask); 10564cb5300bSChris Mason if (!page) 10574cb5300bSChris Mason break; 10584cb5300bSChris Mason 1059600a45e1SMiao Xie page_start = page_offset(page); 1060600a45e1SMiao Xie page_end = page_start + PAGE_CACHE_SIZE - 1; 1061600a45e1SMiao Xie while (1) { 1062d0082371SJeff Mahoney lock_extent(tree, page_start, page_end); 1063600a45e1SMiao Xie ordered = btrfs_lookup_ordered_extent(inode, 1064600a45e1SMiao Xie page_start); 1065d0082371SJeff Mahoney unlock_extent(tree, page_start, page_end); 1066600a45e1SMiao Xie if (!ordered) 1067600a45e1SMiao Xie break; 1068600a45e1SMiao Xie 1069600a45e1SMiao Xie unlock_page(page); 1070600a45e1SMiao Xie btrfs_start_ordered_extent(inode, ordered, 1); 1071600a45e1SMiao Xie btrfs_put_ordered_extent(ordered); 1072600a45e1SMiao Xie lock_page(page); 10731f12bd06SLiu Bo /* 10741f12bd06SLiu Bo * we unlocked the page above, so we need check if 10751f12bd06SLiu Bo * it was released or not. 10761f12bd06SLiu Bo */ 10771f12bd06SLiu Bo if (page->mapping != inode->i_mapping) { 10781f12bd06SLiu Bo unlock_page(page); 10791f12bd06SLiu Bo page_cache_release(page); 10801f12bd06SLiu Bo goto again; 10811f12bd06SLiu Bo } 1082600a45e1SMiao Xie } 1083600a45e1SMiao Xie 10844cb5300bSChris Mason if (!PageUptodate(page)) { 10854cb5300bSChris Mason btrfs_readpage(NULL, page); 10864cb5300bSChris Mason lock_page(page); 10874cb5300bSChris Mason if (!PageUptodate(page)) { 10884cb5300bSChris Mason unlock_page(page); 10894cb5300bSChris Mason page_cache_release(page); 10904cb5300bSChris Mason ret = -EIO; 10914cb5300bSChris Mason break; 10924cb5300bSChris Mason } 10934cb5300bSChris Mason } 1094600a45e1SMiao Xie 1095600a45e1SMiao Xie if (page->mapping != inode->i_mapping) { 1096600a45e1SMiao Xie unlock_page(page); 1097600a45e1SMiao Xie page_cache_release(page); 1098600a45e1SMiao Xie goto again; 1099600a45e1SMiao Xie } 1100600a45e1SMiao Xie 11014cb5300bSChris Mason pages[i] = page; 11024cb5300bSChris Mason i_done++; 11034cb5300bSChris Mason } 11044cb5300bSChris Mason if (!i_done || ret) 11054cb5300bSChris Mason goto out; 11064cb5300bSChris Mason 11074cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 11084cb5300bSChris Mason goto out; 11094cb5300bSChris Mason 11104cb5300bSChris Mason /* 11114cb5300bSChris Mason * so now we have a nice long stream of locked 11124cb5300bSChris Mason * and up to date pages, lets wait on them 11134cb5300bSChris Mason */ 11144cb5300bSChris Mason for (i = 0; i < i_done; i++) 11154cb5300bSChris Mason wait_on_page_writeback(pages[i]); 11164cb5300bSChris Mason 11174cb5300bSChris Mason page_start = page_offset(pages[0]); 11184cb5300bSChris Mason page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; 11194cb5300bSChris Mason 11204cb5300bSChris Mason lock_extent_bits(&BTRFS_I(inode)->io_tree, 1121d0082371SJeff Mahoney page_start, page_end - 1, 0, &cached_state); 11224cb5300bSChris Mason clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, 11234cb5300bSChris Mason page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 11249e8a4a8bSLiu Bo EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, 11259e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 11264cb5300bSChris Mason 11271f12bd06SLiu Bo if (i_done != page_cnt) { 11289e0baf60SJosef Bacik spin_lock(&BTRFS_I(inode)->lock); 11299e0baf60SJosef Bacik BTRFS_I(inode)->outstanding_extents++; 11309e0baf60SJosef Bacik spin_unlock(&BTRFS_I(inode)->lock); 11314cb5300bSChris Mason btrfs_delalloc_release_space(inode, 11321f12bd06SLiu Bo (page_cnt - i_done) << PAGE_CACHE_SHIFT); 11334cb5300bSChris Mason } 11344cb5300bSChris Mason 11354cb5300bSChris Mason 11369e8a4a8bSLiu Bo set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, 11379e8a4a8bSLiu Bo &cached_state, GFP_NOFS); 11384cb5300bSChris Mason 11394cb5300bSChris Mason unlock_extent_cached(&BTRFS_I(inode)->io_tree, 11404cb5300bSChris Mason page_start, page_end - 1, &cached_state, 11414cb5300bSChris Mason GFP_NOFS); 11424cb5300bSChris Mason 11434cb5300bSChris Mason for (i = 0; i < i_done; i++) { 11444cb5300bSChris Mason clear_page_dirty_for_io(pages[i]); 11454cb5300bSChris Mason ClearPageChecked(pages[i]); 11464cb5300bSChris Mason set_page_extent_mapped(pages[i]); 11474cb5300bSChris Mason set_page_dirty(pages[i]); 11484cb5300bSChris Mason unlock_page(pages[i]); 11494cb5300bSChris Mason page_cache_release(pages[i]); 11504cb5300bSChris Mason } 11514cb5300bSChris Mason return i_done; 11524cb5300bSChris Mason out: 11534cb5300bSChris Mason for (i = 0; i < i_done; i++) { 11544cb5300bSChris Mason unlock_page(pages[i]); 11554cb5300bSChris Mason page_cache_release(pages[i]); 11564cb5300bSChris Mason } 11571f12bd06SLiu Bo btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); 11584cb5300bSChris Mason return ret; 11594cb5300bSChris Mason 11604cb5300bSChris Mason } 11614cb5300bSChris Mason 11624cb5300bSChris Mason int btrfs_defrag_file(struct inode *inode, struct file *file, 11634cb5300bSChris Mason struct btrfs_ioctl_defrag_range_args *range, 11644cb5300bSChris Mason u64 newer_than, unsigned long max_to_defrag) 11654cb5300bSChris Mason { 11664cb5300bSChris Mason struct btrfs_root *root = BTRFS_I(inode)->root; 11674cb5300bSChris Mason struct file_ra_state *ra = NULL; 11684cb5300bSChris Mason unsigned long last_index; 1169151a31b2SLi Zefan u64 isize = i_size_read(inode); 1170940100a4SChris Mason u64 last_len = 0; 1171940100a4SChris Mason u64 skip = 0; 1172940100a4SChris Mason u64 defrag_end = 0; 11734cb5300bSChris Mason u64 newer_off = range->start; 1174f46b5a66SChristoph Hellwig unsigned long i; 1175008873eaSLi Zefan unsigned long ra_index = 0; 1176f46b5a66SChristoph Hellwig int ret; 11774cb5300bSChris Mason int defrag_count = 0; 11781a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 11794cb5300bSChris Mason int extent_thresh = range->extent_thresh; 1180008873eaSLi Zefan int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; 1181008873eaSLi Zefan int cluster = max_cluster; 11824cb5300bSChris Mason u64 new_align = ~((u64)128 * 1024 - 1); 11834cb5300bSChris Mason struct page **pages = NULL; 11844cb5300bSChris Mason 11850abd5b17SLiu Bo if (isize == 0) 11860abd5b17SLiu Bo return 0; 11870abd5b17SLiu Bo 11880abd5b17SLiu Bo if (range->start >= isize) 11890abd5b17SLiu Bo return -EINVAL; 11901a419d85SLi Zefan 11911a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 11921a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 11931a419d85SLi Zefan return -EINVAL; 11941a419d85SLi Zefan if (range->compress_type) 11951a419d85SLi Zefan compress_type = range->compress_type; 11961a419d85SLi Zefan } 1197f46b5a66SChristoph Hellwig 11980abd5b17SLiu Bo if (extent_thresh == 0) 11990abd5b17SLiu Bo extent_thresh = 256 * 1024; 1200f46b5a66SChristoph Hellwig 12014cb5300bSChris Mason /* 12024cb5300bSChris Mason * if we were not given a file, allocate a readahead 12034cb5300bSChris Mason * context 12044cb5300bSChris Mason */ 12054cb5300bSChris Mason if (!file) { 12064cb5300bSChris Mason ra = kzalloc(sizeof(*ra), GFP_NOFS); 12074cb5300bSChris Mason if (!ra) 12084cb5300bSChris Mason return -ENOMEM; 12094cb5300bSChris Mason file_ra_state_init(ra, inode->i_mapping); 12104cb5300bSChris Mason } else { 12114cb5300bSChris Mason ra = &file->f_ra; 12124cb5300bSChris Mason } 12134cb5300bSChris Mason 1214d9b0d9baSDulshani Gunawardhana pages = kmalloc_array(max_cluster, sizeof(struct page *), 12154cb5300bSChris Mason GFP_NOFS); 12164cb5300bSChris Mason if (!pages) { 12174cb5300bSChris Mason ret = -ENOMEM; 12184cb5300bSChris Mason goto out_ra; 12194cb5300bSChris Mason } 12204cb5300bSChris Mason 12214cb5300bSChris Mason /* find the last page to defrag */ 12221e701a32SChris Mason if (range->start + range->len > range->start) { 1223151a31b2SLi Zefan last_index = min_t(u64, isize - 1, 12241e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 12251e701a32SChris Mason } else { 1226151a31b2SLi Zefan last_index = (isize - 1) >> PAGE_CACHE_SHIFT; 12271e701a32SChris Mason } 12281e701a32SChris Mason 12294cb5300bSChris Mason if (newer_than) { 12304cb5300bSChris Mason ret = find_new_extents(root, inode, newer_than, 12314cb5300bSChris Mason &newer_off, 64 * 1024); 12324cb5300bSChris Mason if (!ret) { 12334cb5300bSChris Mason range->start = newer_off; 12344cb5300bSChris Mason /* 12354cb5300bSChris Mason * we always align our defrag to help keep 12364cb5300bSChris Mason * the extents in the file evenly spaced 12374cb5300bSChris Mason */ 12384cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 12394cb5300bSChris Mason } else 12404cb5300bSChris Mason goto out_ra; 12414cb5300bSChris Mason } else { 12421e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 12434cb5300bSChris Mason } 12444cb5300bSChris Mason if (!max_to_defrag) 12457ec31b54SLiu Bo max_to_defrag = last_index + 1; 12464cb5300bSChris Mason 12472a0f7f57SLi Zefan /* 12482a0f7f57SLi Zefan * make writeback starts from i, so the defrag range can be 12492a0f7f57SLi Zefan * written sequentially. 12502a0f7f57SLi Zefan */ 12512a0f7f57SLi Zefan if (i < inode->i_mapping->writeback_index) 12522a0f7f57SLi Zefan inode->i_mapping->writeback_index = i; 12532a0f7f57SLi Zefan 1254f7f43cc8SChris Mason while (i <= last_index && defrag_count < max_to_defrag && 1255f7f43cc8SChris Mason (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 1256f7f43cc8SChris Mason PAGE_CACHE_SHIFT)) { 12574cb5300bSChris Mason /* 12584cb5300bSChris Mason * make sure we stop running if someone unmounts 12594cb5300bSChris Mason * the FS 12604cb5300bSChris Mason */ 12614cb5300bSChris Mason if (!(inode->i_sb->s_flags & MS_ACTIVE)) 12624cb5300bSChris Mason break; 12634cb5300bSChris Mason 1264210549ebSDavid Sterba if (btrfs_defrag_cancelled(root->fs_info)) { 1265210549ebSDavid Sterba printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); 1266210549ebSDavid Sterba ret = -EAGAIN; 1267210549ebSDavid Sterba break; 1268210549ebSDavid Sterba } 1269210549ebSDavid Sterba 127066c26892SLiu Bo if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 12716c282eb4SLi Zefan extent_thresh, &last_len, &skip, 1272a43a2111SAndrew Mahone &defrag_end, range->flags & 1273a43a2111SAndrew Mahone BTRFS_DEFRAG_RANGE_COMPRESS)) { 1274940100a4SChris Mason unsigned long next; 1275940100a4SChris Mason /* 1276940100a4SChris Mason * the should_defrag function tells us how much to skip 1277940100a4SChris Mason * bump our counter by the suggested amount 1278940100a4SChris Mason */ 1279940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1280940100a4SChris Mason i = max(i + 1, next); 1281940100a4SChris Mason continue; 1282940100a4SChris Mason } 1283008873eaSLi Zefan 1284008873eaSLi Zefan if (!newer_than) { 1285008873eaSLi Zefan cluster = (PAGE_CACHE_ALIGN(defrag_end) >> 1286008873eaSLi Zefan PAGE_CACHE_SHIFT) - i; 1287008873eaSLi Zefan cluster = min(cluster, max_cluster); 1288008873eaSLi Zefan } else { 1289008873eaSLi Zefan cluster = max_cluster; 1290008873eaSLi Zefan } 1291008873eaSLi Zefan 1292008873eaSLi Zefan if (i + cluster > ra_index) { 1293008873eaSLi Zefan ra_index = max(i, ra_index); 1294008873eaSLi Zefan btrfs_force_ra(inode->i_mapping, ra, file, ra_index, 1295008873eaSLi Zefan cluster); 1296008873eaSLi Zefan ra_index += max_cluster; 1297008873eaSLi Zefan } 12984cb5300bSChris Mason 1299ecb8bea8SLiu Bo mutex_lock(&inode->i_mutex); 1300633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 1301633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = compress_type; 1302008873eaSLi Zefan ret = cluster_pages_for_defrag(inode, pages, i, cluster); 1303ecb8bea8SLiu Bo if (ret < 0) { 1304ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 13054cb5300bSChris Mason goto out_ra; 1306ecb8bea8SLiu Bo } 13074cb5300bSChris Mason 13084cb5300bSChris Mason defrag_count += ret; 1309d0e1d66bSNamjae Jeon balance_dirty_pages_ratelimited(inode->i_mapping); 1310ecb8bea8SLiu Bo mutex_unlock(&inode->i_mutex); 13114cb5300bSChris Mason 13124cb5300bSChris Mason if (newer_than) { 13134cb5300bSChris Mason if (newer_off == (u64)-1) 13144cb5300bSChris Mason break; 13154cb5300bSChris Mason 1316e1f041e1SLiu Bo if (ret > 0) 1317e1f041e1SLiu Bo i += ret; 1318e1f041e1SLiu Bo 13194cb5300bSChris Mason newer_off = max(newer_off + 1, 13204cb5300bSChris Mason (u64)i << PAGE_CACHE_SHIFT); 13214cb5300bSChris Mason 13224cb5300bSChris Mason ret = find_new_extents(root, inode, 13234cb5300bSChris Mason newer_than, &newer_off, 13244cb5300bSChris Mason 64 * 1024); 13254cb5300bSChris Mason if (!ret) { 13264cb5300bSChris Mason range->start = newer_off; 13274cb5300bSChris Mason i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; 13284cb5300bSChris Mason } else { 13294cb5300bSChris Mason break; 1330940100a4SChris Mason } 13314cb5300bSChris Mason } else { 1332008873eaSLi Zefan if (ret > 0) { 1333cbcc8326SLi Zefan i += ret; 1334008873eaSLi Zefan last_len += ret << PAGE_CACHE_SHIFT; 1335008873eaSLi Zefan } else { 1336940100a4SChris Mason i++; 1337008873eaSLi Zefan last_len = 0; 1338008873eaSLi Zefan } 1339f46b5a66SChristoph Hellwig } 13404cb5300bSChris Mason } 1341f46b5a66SChristoph Hellwig 13421e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 13431e701a32SChris Mason filemap_flush(inode->i_mapping); 13441e701a32SChris Mason 13451e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 13461e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 13471e701a32SChris Mason * we have to make sure the IO is actually started and that 13481e701a32SChris Mason * ordered extents get created before we return 13491e701a32SChris Mason */ 13501e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 13511e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 13521e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 13531e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 13541e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 13551e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 13561e701a32SChris Mason } 13571e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 13581e701a32SChris Mason } 13591e701a32SChris Mason 13601a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 13612b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 13621a419d85SLi Zefan } 13631a419d85SLi Zefan 136460ccf82fSDiego Calleja ret = defrag_count; 1365940100a4SChris Mason 13664cb5300bSChris Mason out_ra: 1367633085c7SFilipe David Borba Manana if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 1368633085c7SFilipe David Borba Manana mutex_lock(&inode->i_mutex); 1369633085c7SFilipe David Borba Manana BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 1370633085c7SFilipe David Borba Manana mutex_unlock(&inode->i_mutex); 1371633085c7SFilipe David Borba Manana } 13724cb5300bSChris Mason if (!file) 13734cb5300bSChris Mason kfree(ra); 13744cb5300bSChris Mason kfree(pages); 1375940100a4SChris Mason return ret; 1376f46b5a66SChristoph Hellwig } 1377f46b5a66SChristoph Hellwig 1378198605a8SMiao Xie static noinline int btrfs_ioctl_resize(struct file *file, 137976dda93cSYan, Zheng void __user *arg) 1380f46b5a66SChristoph Hellwig { 1381f46b5a66SChristoph Hellwig u64 new_size; 1382f46b5a66SChristoph Hellwig u64 old_size; 1383f46b5a66SChristoph Hellwig u64 devid = 1; 1384496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 1385f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1386f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1387f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 1388f46b5a66SChristoph Hellwig char *sizestr; 1389f46b5a66SChristoph Hellwig char *devstr = NULL; 1390f46b5a66SChristoph Hellwig int ret = 0; 1391f46b5a66SChristoph Hellwig int mod = 0; 1392f46b5a66SChristoph Hellwig 1393e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1394e441d54dSChris Mason return -EPERM; 1395e441d54dSChris Mason 1396198605a8SMiao Xie ret = mnt_want_write_file(file); 1397198605a8SMiao Xie if (ret) 1398198605a8SMiao Xie return ret; 1399198605a8SMiao Xie 14005ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 14015ac00addSStefan Behrens 1)) { 140297547676SMiao Xie mnt_drop_write_file(file); 1403e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1404c9e9f97bSIlya Dryomov } 1405c9e9f97bSIlya Dryomov 14065ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 1407dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1408c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 1409c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 1410c9e9f97bSIlya Dryomov goto out; 1411c9e9f97bSIlya Dryomov } 14125516e595SMark Fasheh 14135516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1414f46b5a66SChristoph Hellwig 1415f46b5a66SChristoph Hellwig sizestr = vol_args->name; 1416f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 1417f46b5a66SChristoph Hellwig if (devstr) { 1418f46b5a66SChristoph Hellwig char *end; 1419f46b5a66SChristoph Hellwig sizestr = devstr + 1; 1420f46b5a66SChristoph Hellwig *devstr = '\0'; 1421f46b5a66SChristoph Hellwig devstr = vol_args->name; 1422f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 1423dfd79829SMiao Xie if (!devid) { 1424dfd79829SMiao Xie ret = -EINVAL; 1425dfd79829SMiao Xie goto out_free; 1426dfd79829SMiao Xie } 1427c1c9ff7cSGeert Uytterhoeven printk(KERN_INFO "btrfs: resizing devid %llu\n", devid); 1428f46b5a66SChristoph Hellwig } 1429dba60f3fSMiao Xie 1430aa1b8cd4SStefan Behrens device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1431f46b5a66SChristoph Hellwig if (!device) { 14325bb14682SArnd Hannemann printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", 1433c1c9ff7cSGeert Uytterhoeven devid); 1434dfd79829SMiao Xie ret = -ENODEV; 1435c9e9f97bSIlya Dryomov goto out_free; 1436f46b5a66SChristoph Hellwig } 1437dba60f3fSMiao Xie 1438dba60f3fSMiao Xie if (!device->writeable) { 14394e42ae1bSLiu Bo printk(KERN_INFO "btrfs: resizer unable to apply on " 1440dba60f3fSMiao Xie "readonly device %llu\n", 1441c1c9ff7cSGeert Uytterhoeven devid); 1442dfd79829SMiao Xie ret = -EPERM; 14434e42ae1bSLiu Bo goto out_free; 14444e42ae1bSLiu Bo } 14454e42ae1bSLiu Bo 1446f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 1447f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 1448f46b5a66SChristoph Hellwig else { 1449f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 1450f46b5a66SChristoph Hellwig mod = -1; 1451f46b5a66SChristoph Hellwig sizestr++; 1452f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 1453f46b5a66SChristoph Hellwig mod = 1; 1454f46b5a66SChristoph Hellwig sizestr++; 1455f46b5a66SChristoph Hellwig } 145691748467SAkinobu Mita new_size = memparse(sizestr, NULL); 1457f46b5a66SChristoph Hellwig if (new_size == 0) { 1458f46b5a66SChristoph Hellwig ret = -EINVAL; 1459c9e9f97bSIlya Dryomov goto out_free; 1460f46b5a66SChristoph Hellwig } 1461f46b5a66SChristoph Hellwig } 1462f46b5a66SChristoph Hellwig 146363a212abSStefan Behrens if (device->is_tgtdev_for_dev_replace) { 1464dfd79829SMiao Xie ret = -EPERM; 146563a212abSStefan Behrens goto out_free; 146663a212abSStefan Behrens } 146763a212abSStefan Behrens 1468f46b5a66SChristoph Hellwig old_size = device->total_bytes; 1469f46b5a66SChristoph Hellwig 1470f46b5a66SChristoph Hellwig if (mod < 0) { 1471f46b5a66SChristoph Hellwig if (new_size > old_size) { 1472f46b5a66SChristoph Hellwig ret = -EINVAL; 1473c9e9f97bSIlya Dryomov goto out_free; 1474f46b5a66SChristoph Hellwig } 1475f46b5a66SChristoph Hellwig new_size = old_size - new_size; 1476f46b5a66SChristoph Hellwig } else if (mod > 0) { 1477f46b5a66SChristoph Hellwig new_size = old_size + new_size; 1478f46b5a66SChristoph Hellwig } 1479f46b5a66SChristoph Hellwig 1480f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 1481f46b5a66SChristoph Hellwig ret = -EINVAL; 1482c9e9f97bSIlya Dryomov goto out_free; 1483f46b5a66SChristoph Hellwig } 1484f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 1485f46b5a66SChristoph Hellwig ret = -EFBIG; 1486c9e9f97bSIlya Dryomov goto out_free; 1487f46b5a66SChristoph Hellwig } 1488f46b5a66SChristoph Hellwig 1489f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 1490f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 1491f46b5a66SChristoph Hellwig 1492606686eeSJosef Bacik printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", 1493c1c9ff7cSGeert Uytterhoeven rcu_str_deref(device->name), new_size); 1494f46b5a66SChristoph Hellwig 1495f46b5a66SChristoph Hellwig if (new_size > old_size) { 1496a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 149798d5dc13STsutomu Itoh if (IS_ERR(trans)) { 149898d5dc13STsutomu Itoh ret = PTR_ERR(trans); 1499c9e9f97bSIlya Dryomov goto out_free; 150098d5dc13STsutomu Itoh } 1501f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 1502f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 1503ece7d20eSMike Fleetwood } else if (new_size < old_size) { 1504f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 15050253f40eSjeff.liu } /* equal, nothing need to do */ 1506f46b5a66SChristoph Hellwig 1507c9e9f97bSIlya Dryomov out_free: 1508f46b5a66SChristoph Hellwig kfree(vol_args); 1509c9e9f97bSIlya Dryomov out: 1510c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 15115ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 151218f39c41SIlya Dryomov mnt_drop_write_file(file); 1513f46b5a66SChristoph Hellwig return ret; 1514f46b5a66SChristoph Hellwig } 1515f46b5a66SChristoph Hellwig 151672fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 15176f72c7e2SArne Jansen char *name, unsigned long fd, int subvol, 15186f72c7e2SArne Jansen u64 *transid, bool readonly, 15198696c533SMiao Xie struct btrfs_qgroup_inherit *inherit) 1520f46b5a66SChristoph Hellwig { 1521f46b5a66SChristoph Hellwig int namelen; 15223de4586cSChris Mason int ret = 0; 1523f46b5a66SChristoph Hellwig 1524a874a63eSLiu Bo ret = mnt_want_write_file(file); 1525a874a63eSLiu Bo if (ret) 1526a874a63eSLiu Bo goto out; 1527a874a63eSLiu Bo 152872fd032eSSage Weil namelen = strlen(name); 152972fd032eSSage Weil if (strchr(name, '/')) { 1530f46b5a66SChristoph Hellwig ret = -EINVAL; 1531a874a63eSLiu Bo goto out_drop_write; 1532f46b5a66SChristoph Hellwig } 1533f46b5a66SChristoph Hellwig 153416780cabSChris Mason if (name[0] == '.' && 153516780cabSChris Mason (namelen == 1 || (name[1] == '.' && namelen == 2))) { 153616780cabSChris Mason ret = -EEXIST; 1537a874a63eSLiu Bo goto out_drop_write; 153816780cabSChris Mason } 153916780cabSChris Mason 15403de4586cSChris Mason if (subvol) { 154172fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 15426f72c7e2SArne Jansen NULL, transid, readonly, inherit); 1543cb8e7090SChristoph Hellwig } else { 15442903ff01SAl Viro struct fd src = fdget(fd); 15453de4586cSChris Mason struct inode *src_inode; 15462903ff01SAl Viro if (!src.file) { 15473de4586cSChris Mason ret = -EINVAL; 1548a874a63eSLiu Bo goto out_drop_write; 15493de4586cSChris Mason } 15503de4586cSChris Mason 1551496ad9aaSAl Viro src_inode = file_inode(src.file); 1552496ad9aaSAl Viro if (src_inode->i_sb != file_inode(file)->i_sb) { 1553d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 1554d397712bSChris Mason "another FS\n"); 15553de4586cSChris Mason ret = -EINVAL; 1556ecd18815SAl Viro } else { 155772fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 155872fd032eSSage Weil BTRFS_I(src_inode)->root, 15596f72c7e2SArne Jansen transid, readonly, inherit); 1560ecd18815SAl Viro } 15612903ff01SAl Viro fdput(src); 1562cb8e7090SChristoph Hellwig } 1563a874a63eSLiu Bo out_drop_write: 1564a874a63eSLiu Bo mnt_drop_write_file(file); 1565f46b5a66SChristoph Hellwig out: 156672fd032eSSage Weil return ret; 156772fd032eSSage Weil } 156872fd032eSSage Weil 156972fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 1570fa0d2b9bSLi Zefan void __user *arg, int subvol) 157172fd032eSSage Weil { 1572fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 157372fd032eSSage Weil int ret; 157472fd032eSSage Weil 1575fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1576fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1577fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1578fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1579fa0d2b9bSLi Zefan 1580fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1581b83cc969SLi Zefan vol_args->fd, subvol, 15826f72c7e2SArne Jansen NULL, false, NULL); 1583fa0d2b9bSLi Zefan 1584fa0d2b9bSLi Zefan kfree(vol_args); 1585fa0d2b9bSLi Zefan return ret; 1586fa0d2b9bSLi Zefan } 1587fa0d2b9bSLi Zefan 1588fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 1589fa0d2b9bSLi Zefan void __user *arg, int subvol) 1590fa0d2b9bSLi Zefan { 1591fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 1592fa0d2b9bSLi Zefan int ret; 1593fdfb1e4fSLi Zefan u64 transid = 0; 1594fdfb1e4fSLi Zefan u64 *ptr = NULL; 1595b83cc969SLi Zefan bool readonly = false; 15966f72c7e2SArne Jansen struct btrfs_qgroup_inherit *inherit = NULL; 159772fd032eSSage Weil 1598fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1599fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1600fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1601fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1602fdfb1e4fSLi Zefan 1603b83cc969SLi Zefan if (vol_args->flags & 16046f72c7e2SArne Jansen ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | 16056f72c7e2SArne Jansen BTRFS_SUBVOL_QGROUP_INHERIT)) { 1606b83cc969SLi Zefan ret = -EOPNOTSUPP; 1607fdfb1e4fSLi Zefan goto out; 1608fdfb1e4fSLi Zefan } 1609fdfb1e4fSLi Zefan 1610fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1611fdfb1e4fSLi Zefan ptr = &transid; 1612b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1613b83cc969SLi Zefan readonly = true; 16146f72c7e2SArne Jansen if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { 16156f72c7e2SArne Jansen if (vol_args->size > PAGE_CACHE_SIZE) { 16166f72c7e2SArne Jansen ret = -EINVAL; 16176f72c7e2SArne Jansen goto out; 16186f72c7e2SArne Jansen } 16196f72c7e2SArne Jansen inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); 16206f72c7e2SArne Jansen if (IS_ERR(inherit)) { 16216f72c7e2SArne Jansen ret = PTR_ERR(inherit); 16226f72c7e2SArne Jansen goto out; 16236f72c7e2SArne Jansen } 16246f72c7e2SArne Jansen } 162575eaa0e2SSage Weil 1626fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 16276f72c7e2SArne Jansen vol_args->fd, subvol, ptr, 16288696c533SMiao Xie readonly, inherit); 162975eaa0e2SSage Weil 1630fdfb1e4fSLi Zefan if (ret == 0 && ptr && 163175eaa0e2SSage Weil copy_to_user(arg + 1632fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1633fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 163475eaa0e2SSage Weil ret = -EFAULT; 1635fdfb1e4fSLi Zefan out: 1636f46b5a66SChristoph Hellwig kfree(vol_args); 16376f72c7e2SArne Jansen kfree(inherit); 1638f46b5a66SChristoph Hellwig return ret; 1639f46b5a66SChristoph Hellwig } 1640f46b5a66SChristoph Hellwig 16410caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 16420caa102dSLi Zefan void __user *arg) 16430caa102dSLi Zefan { 1644496ad9aaSAl Viro struct inode *inode = file_inode(file); 16450caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 16460caa102dSLi Zefan int ret = 0; 16470caa102dSLi Zefan u64 flags = 0; 16480caa102dSLi Zefan 164933345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 16500caa102dSLi Zefan return -EINVAL; 16510caa102dSLi Zefan 16520caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 16530caa102dSLi Zefan if (btrfs_root_readonly(root)) 16540caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 16550caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 16560caa102dSLi Zefan 16570caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 16580caa102dSLi Zefan ret = -EFAULT; 16590caa102dSLi Zefan 16600caa102dSLi Zefan return ret; 16610caa102dSLi Zefan } 16620caa102dSLi Zefan 16630caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 16640caa102dSLi Zefan void __user *arg) 16650caa102dSLi Zefan { 1666496ad9aaSAl Viro struct inode *inode = file_inode(file); 16670caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 16680caa102dSLi Zefan struct btrfs_trans_handle *trans; 16690caa102dSLi Zefan u64 root_flags; 16700caa102dSLi Zefan u64 flags; 16710caa102dSLi Zefan int ret = 0; 16720caa102dSLi Zefan 1673b9ca0664SLiu Bo ret = mnt_want_write_file(file); 1674b9ca0664SLiu Bo if (ret) 1675b9ca0664SLiu Bo goto out; 16760caa102dSLi Zefan 1677b9ca0664SLiu Bo if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 1678b9ca0664SLiu Bo ret = -EINVAL; 1679b9ca0664SLiu Bo goto out_drop_write; 1680b9ca0664SLiu Bo } 16810caa102dSLi Zefan 1682b9ca0664SLiu Bo if (copy_from_user(&flags, arg, sizeof(flags))) { 1683b9ca0664SLiu Bo ret = -EFAULT; 1684b9ca0664SLiu Bo goto out_drop_write; 1685b9ca0664SLiu Bo } 16860caa102dSLi Zefan 1687b9ca0664SLiu Bo if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { 1688b9ca0664SLiu Bo ret = -EINVAL; 1689b9ca0664SLiu Bo goto out_drop_write; 1690b9ca0664SLiu Bo } 16910caa102dSLi Zefan 1692b9ca0664SLiu Bo if (flags & ~BTRFS_SUBVOL_RDONLY) { 1693b9ca0664SLiu Bo ret = -EOPNOTSUPP; 1694b9ca0664SLiu Bo goto out_drop_write; 1695b9ca0664SLiu Bo } 16960caa102dSLi Zefan 1697b9ca0664SLiu Bo if (!inode_owner_or_capable(inode)) { 1698b9ca0664SLiu Bo ret = -EACCES; 1699b9ca0664SLiu Bo goto out_drop_write; 1700b9ca0664SLiu Bo } 1701b4dc2b8cSLi Zefan 17020caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 17030caa102dSLi Zefan 17040caa102dSLi Zefan /* nothing to do */ 17050caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1706b9ca0664SLiu Bo goto out_drop_sem; 17070caa102dSLi Zefan 17080caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 1709*2c686537SDavid Sterba if (flags & BTRFS_SUBVOL_RDONLY) { 17100caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 17110caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 1712*2c686537SDavid Sterba } else { 1713*2c686537SDavid Sterba /* 1714*2c686537SDavid Sterba * Block RO -> RW transition if this subvolume is involved in 1715*2c686537SDavid Sterba * send 1716*2c686537SDavid Sterba */ 1717*2c686537SDavid Sterba spin_lock(&root->root_item_lock); 1718*2c686537SDavid Sterba if (root->send_in_progress == 0) { 17190caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 17200caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 1721*2c686537SDavid Sterba spin_unlock(&root->root_item_lock); 1722*2c686537SDavid Sterba } else { 1723*2c686537SDavid Sterba spin_unlock(&root->root_item_lock); 1724*2c686537SDavid Sterba btrfs_warn(root->fs_info, 1725*2c686537SDavid Sterba "Attempt to set subvolume %llu read-write during send", 1726*2c686537SDavid Sterba root->root_key.objectid); 1727*2c686537SDavid Sterba ret = -EPERM; 1728*2c686537SDavid Sterba goto out_drop_sem; 1729*2c686537SDavid Sterba } 1730*2c686537SDavid Sterba } 17310caa102dSLi Zefan 17320caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 17330caa102dSLi Zefan if (IS_ERR(trans)) { 17340caa102dSLi Zefan ret = PTR_ERR(trans); 17350caa102dSLi Zefan goto out_reset; 17360caa102dSLi Zefan } 17370caa102dSLi Zefan 1738b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 17390caa102dSLi Zefan &root->root_key, &root->root_item); 17400caa102dSLi Zefan 17410caa102dSLi Zefan btrfs_commit_transaction(trans, root); 17420caa102dSLi Zefan out_reset: 17430caa102dSLi Zefan if (ret) 17440caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 1745b9ca0664SLiu Bo out_drop_sem: 17460caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 1747b9ca0664SLiu Bo out_drop_write: 1748b9ca0664SLiu Bo mnt_drop_write_file(file); 1749b9ca0664SLiu Bo out: 17500caa102dSLi Zefan return ret; 17510caa102dSLi Zefan } 17520caa102dSLi Zefan 175376dda93cSYan, Zheng /* 175476dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 175576dda93cSYan, Zheng */ 175676dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 175776dda93cSYan, Zheng { 175876dda93cSYan, Zheng struct btrfs_path *path; 1759175a2b87SJosef Bacik struct btrfs_dir_item *di; 176076dda93cSYan, Zheng struct btrfs_key key; 1761175a2b87SJosef Bacik u64 dir_id; 176276dda93cSYan, Zheng int ret; 176376dda93cSYan, Zheng 176476dda93cSYan, Zheng path = btrfs_alloc_path(); 176576dda93cSYan, Zheng if (!path) 176676dda93cSYan, Zheng return -ENOMEM; 176776dda93cSYan, Zheng 1768175a2b87SJosef Bacik /* Make sure this root isn't set as the default subvol */ 1769175a2b87SJosef Bacik dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 1770175a2b87SJosef Bacik di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, 1771175a2b87SJosef Bacik dir_id, "default", 7, 0); 1772175a2b87SJosef Bacik if (di && !IS_ERR(di)) { 1773175a2b87SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1774175a2b87SJosef Bacik if (key.objectid == root->root_key.objectid) { 1775175a2b87SJosef Bacik ret = -ENOTEMPTY; 1776175a2b87SJosef Bacik goto out; 1777175a2b87SJosef Bacik } 1778175a2b87SJosef Bacik btrfs_release_path(path); 1779175a2b87SJosef Bacik } 1780175a2b87SJosef Bacik 178176dda93cSYan, Zheng key.objectid = root->root_key.objectid; 178276dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 178376dda93cSYan, Zheng key.offset = (u64)-1; 178476dda93cSYan, Zheng 178576dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 178676dda93cSYan, Zheng &key, path, 0, 0); 178776dda93cSYan, Zheng if (ret < 0) 178876dda93cSYan, Zheng goto out; 178976dda93cSYan, Zheng BUG_ON(ret == 0); 179076dda93cSYan, Zheng 179176dda93cSYan, Zheng ret = 0; 179276dda93cSYan, Zheng if (path->slots[0] > 0) { 179376dda93cSYan, Zheng path->slots[0]--; 179476dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 179576dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 179676dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 179776dda93cSYan, Zheng ret = -ENOTEMPTY; 179876dda93cSYan, Zheng } 179976dda93cSYan, Zheng out: 180076dda93cSYan, Zheng btrfs_free_path(path); 180176dda93cSYan, Zheng return ret; 180276dda93cSYan, Zheng } 180376dda93cSYan, Zheng 1804ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1805ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1806ac8e9819SChris Mason { 1807abc6e134SChris Mason struct btrfs_key test; 1808abc6e134SChris Mason int ret; 1809abc6e134SChris Mason 1810abc6e134SChris Mason test.objectid = sk->min_objectid; 1811abc6e134SChris Mason test.type = sk->min_type; 1812abc6e134SChris Mason test.offset = sk->min_offset; 1813abc6e134SChris Mason 1814abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1815abc6e134SChris Mason if (ret < 0) 1816ac8e9819SChris Mason return 0; 1817abc6e134SChris Mason 1818abc6e134SChris Mason test.objectid = sk->max_objectid; 1819abc6e134SChris Mason test.type = sk->max_type; 1820abc6e134SChris Mason test.offset = sk->max_offset; 1821abc6e134SChris Mason 1822abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1823abc6e134SChris Mason if (ret > 0) 1824ac8e9819SChris Mason return 0; 1825ac8e9819SChris Mason return 1; 1826ac8e9819SChris Mason } 1827ac8e9819SChris Mason 1828ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1829ac8e9819SChris Mason struct btrfs_path *path, 1830ac8e9819SChris Mason struct btrfs_key *key, 1831ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1832ac8e9819SChris Mason char *buf, 1833ac8e9819SChris Mason unsigned long *sk_offset, 1834ac8e9819SChris Mason int *num_found) 1835ac8e9819SChris Mason { 1836ac8e9819SChris Mason u64 found_transid; 1837ac8e9819SChris Mason struct extent_buffer *leaf; 1838ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1839ac8e9819SChris Mason unsigned long item_off; 1840ac8e9819SChris Mason unsigned long item_len; 1841ac8e9819SChris Mason int nritems; 1842ac8e9819SChris Mason int i; 1843ac8e9819SChris Mason int slot; 1844ac8e9819SChris Mason int ret = 0; 1845ac8e9819SChris Mason 1846ac8e9819SChris Mason leaf = path->nodes[0]; 1847ac8e9819SChris Mason slot = path->slots[0]; 1848ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1849ac8e9819SChris Mason 1850ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1851ac8e9819SChris Mason i = nritems; 1852ac8e9819SChris Mason goto advance_key; 1853ac8e9819SChris Mason } 1854ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1855ac8e9819SChris Mason 1856ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1857ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1858ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1859ac8e9819SChris Mason 186003b71c6cSGabriel de Perthuis btrfs_item_key_to_cpu(leaf, key, i); 186103b71c6cSGabriel de Perthuis if (!key_in_sk(key, sk)) 186203b71c6cSGabriel de Perthuis continue; 186303b71c6cSGabriel de Perthuis 186403b71c6cSGabriel de Perthuis if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1865ac8e9819SChris Mason item_len = 0; 1866ac8e9819SChris Mason 1867ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1868ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1869ac8e9819SChris Mason ret = 1; 1870ac8e9819SChris Mason goto overflow; 1871ac8e9819SChris Mason } 1872ac8e9819SChris Mason 1873ac8e9819SChris Mason sh.objectid = key->objectid; 1874ac8e9819SChris Mason sh.offset = key->offset; 1875ac8e9819SChris Mason sh.type = key->type; 1876ac8e9819SChris Mason sh.len = item_len; 1877ac8e9819SChris Mason sh.transid = found_transid; 1878ac8e9819SChris Mason 1879ac8e9819SChris Mason /* copy search result header */ 1880ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1881ac8e9819SChris Mason *sk_offset += sizeof(sh); 1882ac8e9819SChris Mason 1883ac8e9819SChris Mason if (item_len) { 1884ac8e9819SChris Mason char *p = buf + *sk_offset; 1885ac8e9819SChris Mason /* copy the item */ 1886ac8e9819SChris Mason read_extent_buffer(leaf, p, 1887ac8e9819SChris Mason item_off, item_len); 1888ac8e9819SChris Mason *sk_offset += item_len; 1889ac8e9819SChris Mason } 1890e2156867SHugo Mills (*num_found)++; 1891ac8e9819SChris Mason 1892ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1893ac8e9819SChris Mason break; 1894ac8e9819SChris Mason } 1895ac8e9819SChris Mason advance_key: 1896ac8e9819SChris Mason ret = 0; 1897abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1898abc6e134SChris Mason key->offset++; 1899abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1900abc6e134SChris Mason key->offset = 0; 1901abc6e134SChris Mason key->type++; 1902abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1903abc6e134SChris Mason key->offset = 0; 1904abc6e134SChris Mason key->type = 0; 1905abc6e134SChris Mason key->objectid++; 1906abc6e134SChris Mason } else 1907abc6e134SChris Mason ret = 1; 1908ac8e9819SChris Mason overflow: 1909ac8e9819SChris Mason return ret; 1910ac8e9819SChris Mason } 1911ac8e9819SChris Mason 1912ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1913ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1914ac8e9819SChris Mason { 1915ac8e9819SChris Mason struct btrfs_root *root; 1916ac8e9819SChris Mason struct btrfs_key key; 1917ac8e9819SChris Mason struct btrfs_path *path; 1918ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1919ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1920ac8e9819SChris Mason int ret; 1921ac8e9819SChris Mason int num_found = 0; 1922ac8e9819SChris Mason unsigned long sk_offset = 0; 1923ac8e9819SChris Mason 1924ac8e9819SChris Mason path = btrfs_alloc_path(); 1925ac8e9819SChris Mason if (!path) 1926ac8e9819SChris Mason return -ENOMEM; 1927ac8e9819SChris Mason 1928ac8e9819SChris Mason if (sk->tree_id == 0) { 1929ac8e9819SChris Mason /* search the root of the inode that was passed */ 1930ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1931ac8e9819SChris Mason } else { 1932ac8e9819SChris Mason key.objectid = sk->tree_id; 1933ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1934ac8e9819SChris Mason key.offset = (u64)-1; 1935ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1936ac8e9819SChris Mason if (IS_ERR(root)) { 1937ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1938ac8e9819SChris Mason sk->tree_id); 1939ac8e9819SChris Mason btrfs_free_path(path); 1940ac8e9819SChris Mason return -ENOENT; 1941ac8e9819SChris Mason } 1942ac8e9819SChris Mason } 1943ac8e9819SChris Mason 1944ac8e9819SChris Mason key.objectid = sk->min_objectid; 1945ac8e9819SChris Mason key.type = sk->min_type; 1946ac8e9819SChris Mason key.offset = sk->min_offset; 1947ac8e9819SChris Mason 1948ac8e9819SChris Mason path->keep_locks = 1; 1949ac8e9819SChris Mason 1950ac8e9819SChris Mason while (1) { 19516174d3cbSFilipe David Borba Manana ret = btrfs_search_forward(root, &key, path, sk->min_transid); 1952ac8e9819SChris Mason if (ret != 0) { 1953ac8e9819SChris Mason if (ret > 0) 1954ac8e9819SChris Mason ret = 0; 1955ac8e9819SChris Mason goto err; 1956ac8e9819SChris Mason } 1957ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1958ac8e9819SChris Mason &sk_offset, &num_found); 1959b3b4aa74SDavid Sterba btrfs_release_path(path); 1960ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1961ac8e9819SChris Mason break; 1962ac8e9819SChris Mason 1963ac8e9819SChris Mason } 1964ac8e9819SChris Mason ret = 0; 1965ac8e9819SChris Mason err: 1966ac8e9819SChris Mason sk->nr_items = num_found; 1967ac8e9819SChris Mason btrfs_free_path(path); 1968ac8e9819SChris Mason return ret; 1969ac8e9819SChris Mason } 1970ac8e9819SChris Mason 1971ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1972ac8e9819SChris Mason void __user *argp) 1973ac8e9819SChris Mason { 1974ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1975ac8e9819SChris Mason struct inode *inode; 1976ac8e9819SChris Mason int ret; 1977ac8e9819SChris Mason 1978ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1979ac8e9819SChris Mason return -EPERM; 1980ac8e9819SChris Mason 19812354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 19822354d08fSJulia Lawall if (IS_ERR(args)) 19832354d08fSJulia Lawall return PTR_ERR(args); 1984ac8e9819SChris Mason 1985496ad9aaSAl Viro inode = file_inode(file); 1986ac8e9819SChris Mason ret = search_ioctl(inode, args); 1987ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1988ac8e9819SChris Mason ret = -EFAULT; 1989ac8e9819SChris Mason kfree(args); 1990ac8e9819SChris Mason return ret; 1991ac8e9819SChris Mason } 1992ac8e9819SChris Mason 199398d377a0STARUISI Hiroaki /* 1994ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1995ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 199698d377a0STARUISI Hiroaki */ 199798d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 199898d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 199998d377a0STARUISI Hiroaki { 200098d377a0STARUISI Hiroaki struct btrfs_root *root; 200198d377a0STARUISI Hiroaki struct btrfs_key key; 2002ac8e9819SChris Mason char *ptr; 200398d377a0STARUISI Hiroaki int ret = -1; 200498d377a0STARUISI Hiroaki int slot; 200598d377a0STARUISI Hiroaki int len; 200698d377a0STARUISI Hiroaki int total_len = 0; 200798d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 200898d377a0STARUISI Hiroaki struct extent_buffer *l; 200998d377a0STARUISI Hiroaki struct btrfs_path *path; 201098d377a0STARUISI Hiroaki 201198d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 201298d377a0STARUISI Hiroaki name[0]='\0'; 201398d377a0STARUISI Hiroaki return 0; 201498d377a0STARUISI Hiroaki } 201598d377a0STARUISI Hiroaki 201698d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 201798d377a0STARUISI Hiroaki if (!path) 201898d377a0STARUISI Hiroaki return -ENOMEM; 201998d377a0STARUISI Hiroaki 2020ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 202198d377a0STARUISI Hiroaki 202298d377a0STARUISI Hiroaki key.objectid = tree_id; 202398d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 202498d377a0STARUISI Hiroaki key.offset = (u64)-1; 202598d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 202698d377a0STARUISI Hiroaki if (IS_ERR(root)) { 202798d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 20288ad6fcabSChris Mason ret = -ENOENT; 20298ad6fcabSChris Mason goto out; 203098d377a0STARUISI Hiroaki } 203198d377a0STARUISI Hiroaki 203298d377a0STARUISI Hiroaki key.objectid = dirid; 203398d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 20348ad6fcabSChris Mason key.offset = (u64)-1; 203598d377a0STARUISI Hiroaki 203698d377a0STARUISI Hiroaki while (1) { 203798d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 203898d377a0STARUISI Hiroaki if (ret < 0) 203998d377a0STARUISI Hiroaki goto out; 204018674c6cSFilipe David Borba Manana else if (ret > 0) { 204118674c6cSFilipe David Borba Manana ret = btrfs_previous_item(root, path, dirid, 204218674c6cSFilipe David Borba Manana BTRFS_INODE_REF_KEY); 204318674c6cSFilipe David Borba Manana if (ret < 0) 204418674c6cSFilipe David Borba Manana goto out; 204518674c6cSFilipe David Borba Manana else if (ret > 0) { 2046ac8e9819SChris Mason ret = -ENOENT; 204798d377a0STARUISI Hiroaki goto out; 2048ac8e9819SChris Mason } 204918674c6cSFilipe David Borba Manana } 205018674c6cSFilipe David Borba Manana 205118674c6cSFilipe David Borba Manana l = path->nodes[0]; 205218674c6cSFilipe David Borba Manana slot = path->slots[0]; 205318674c6cSFilipe David Borba Manana btrfs_item_key_to_cpu(l, &key, slot); 205498d377a0STARUISI Hiroaki 205598d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 205698d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 205798d377a0STARUISI Hiroaki ptr -= len + 1; 205898d377a0STARUISI Hiroaki total_len += len + 1; 2059a696cf35SFilipe David Borba Manana if (ptr < name) { 2060a696cf35SFilipe David Borba Manana ret = -ENAMETOOLONG; 206198d377a0STARUISI Hiroaki goto out; 2062a696cf35SFilipe David Borba Manana } 206398d377a0STARUISI Hiroaki 206498d377a0STARUISI Hiroaki *(ptr + len) = '/'; 206598d377a0STARUISI Hiroaki read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); 206698d377a0STARUISI Hiroaki 206798d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 206898d377a0STARUISI Hiroaki break; 206998d377a0STARUISI Hiroaki 2070b3b4aa74SDavid Sterba btrfs_release_path(path); 207198d377a0STARUISI Hiroaki key.objectid = key.offset; 20728ad6fcabSChris Mason key.offset = (u64)-1; 207398d377a0STARUISI Hiroaki dirid = key.objectid; 207498d377a0STARUISI Hiroaki } 207577906a50SLi Zefan memmove(name, ptr, total_len); 207698d377a0STARUISI Hiroaki name[total_len] = '\0'; 207798d377a0STARUISI Hiroaki ret = 0; 207898d377a0STARUISI Hiroaki out: 207998d377a0STARUISI Hiroaki btrfs_free_path(path); 2080ac8e9819SChris Mason return ret; 2081ac8e9819SChris Mason } 2082ac8e9819SChris Mason 2083ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 2084ac8e9819SChris Mason void __user *argp) 2085ac8e9819SChris Mason { 2086ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 2087ac8e9819SChris Mason struct inode *inode; 2088ac8e9819SChris Mason int ret; 2089ac8e9819SChris Mason 2090ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 2091ac8e9819SChris Mason return -EPERM; 2092ac8e9819SChris Mason 20932354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 20942354d08fSJulia Lawall if (IS_ERR(args)) 20952354d08fSJulia Lawall return PTR_ERR(args); 2096c2b96929SDan Carpenter 2097496ad9aaSAl Viro inode = file_inode(file); 2098ac8e9819SChris Mason 20991b53ac4dSChris Mason if (args->treeid == 0) 21001b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 21011b53ac4dSChris Mason 2102ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 2103ac8e9819SChris Mason args->treeid, args->objectid, 2104ac8e9819SChris Mason args->name); 2105ac8e9819SChris Mason 2106ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 2107ac8e9819SChris Mason ret = -EFAULT; 2108ac8e9819SChris Mason 2109ac8e9819SChris Mason kfree(args); 211098d377a0STARUISI Hiroaki return ret; 211198d377a0STARUISI Hiroaki } 211298d377a0STARUISI Hiroaki 211376dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 211476dda93cSYan, Zheng void __user *arg) 211576dda93cSYan, Zheng { 211654563d41SAl Viro struct dentry *parent = file->f_path.dentry; 211776dda93cSYan, Zheng struct dentry *dentry; 211876dda93cSYan, Zheng struct inode *dir = parent->d_inode; 211976dda93cSYan, Zheng struct inode *inode; 212076dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 212176dda93cSYan, Zheng struct btrfs_root *dest = NULL; 212276dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 212376dda93cSYan, Zheng struct btrfs_trans_handle *trans; 2124c58aaad2SMiao Xie struct btrfs_block_rsv block_rsv; 2125c58aaad2SMiao Xie u64 qgroup_reserved; 212676dda93cSYan, Zheng int namelen; 212776dda93cSYan, Zheng int ret; 212876dda93cSYan, Zheng int err = 0; 212976dda93cSYan, Zheng 213076dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 213176dda93cSYan, Zheng if (IS_ERR(vol_args)) 213276dda93cSYan, Zheng return PTR_ERR(vol_args); 213376dda93cSYan, Zheng 213476dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 213576dda93cSYan, Zheng namelen = strlen(vol_args->name); 213676dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 213776dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 213876dda93cSYan, Zheng err = -EINVAL; 213976dda93cSYan, Zheng goto out; 214076dda93cSYan, Zheng } 214176dda93cSYan, Zheng 2142a561be71SAl Viro err = mnt_want_write_file(file); 214376dda93cSYan, Zheng if (err) 214476dda93cSYan, Zheng goto out; 214576dda93cSYan, Zheng 21465c50c9b8SDavid Sterba err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); 21475c50c9b8SDavid Sterba if (err == -EINTR) 2148e43f998eSDavid Sterba goto out_drop_write; 214976dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 215076dda93cSYan, Zheng if (IS_ERR(dentry)) { 215176dda93cSYan, Zheng err = PTR_ERR(dentry); 215276dda93cSYan, Zheng goto out_unlock_dir; 215376dda93cSYan, Zheng } 215476dda93cSYan, Zheng 215576dda93cSYan, Zheng if (!dentry->d_inode) { 215676dda93cSYan, Zheng err = -ENOENT; 215776dda93cSYan, Zheng goto out_dput; 215876dda93cSYan, Zheng } 215976dda93cSYan, Zheng 216076dda93cSYan, Zheng inode = dentry->d_inode; 21614260f7c7SSage Weil dest = BTRFS_I(inode)->root; 21624260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)) { 21634260f7c7SSage Weil /* 21644260f7c7SSage Weil * Regular user. Only allow this with a special mount 21654260f7c7SSage Weil * option, when the user has write+exec access to the 21664260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 21674260f7c7SSage Weil * allowed. 21684260f7c7SSage Weil * 21694260f7c7SSage Weil * Note that this is _not_ check that the subvol is 21704260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 21714260f7c7SSage Weil * otherwise be able to delete. 21724260f7c7SSage Weil * 21734260f7c7SSage Weil * Users who want to delete empty subvols should try 21744260f7c7SSage Weil * rmdir(2). 21754260f7c7SSage Weil */ 21764260f7c7SSage Weil err = -EPERM; 21774260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 21784260f7c7SSage Weil goto out_dput; 21794260f7c7SSage Weil 21804260f7c7SSage Weil /* 21814260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 21824260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 21834260f7c7SSage Weil * must be called on the dentry referencing the root 21844260f7c7SSage Weil * of the subvol, not a random directory contained 21854260f7c7SSage Weil * within it. 21864260f7c7SSage Weil */ 21874260f7c7SSage Weil err = -EINVAL; 21884260f7c7SSage Weil if (root == dest) 21894260f7c7SSage Weil goto out_dput; 21904260f7c7SSage Weil 21914260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 21924260f7c7SSage Weil if (err) 21934260f7c7SSage Weil goto out_dput; 21945c39da5bSMiao Xie } 21954260f7c7SSage Weil 21965c39da5bSMiao Xie /* check if subvolume may be deleted by a user */ 21974260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 21984260f7c7SSage Weil if (err) 21994260f7c7SSage Weil goto out_dput; 22004260f7c7SSage Weil 220133345d01SLi Zefan if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 220276dda93cSYan, Zheng err = -EINVAL; 220376dda93cSYan, Zheng goto out_dput; 220476dda93cSYan, Zheng } 220576dda93cSYan, Zheng 220676dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 220776dda93cSYan, Zheng err = d_invalidate(dentry); 220876dda93cSYan, Zheng if (err) 220976dda93cSYan, Zheng goto out_unlock; 221076dda93cSYan, Zheng 221176dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 221276dda93cSYan, Zheng 221376dda93cSYan, Zheng err = may_destroy_subvol(dest); 221476dda93cSYan, Zheng if (err) 221576dda93cSYan, Zheng goto out_up_write; 221676dda93cSYan, Zheng 2217c58aaad2SMiao Xie btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); 2218c58aaad2SMiao Xie /* 2219c58aaad2SMiao Xie * One for dir inode, two for dir entries, two for root 2220c58aaad2SMiao Xie * ref/backref. 2221c58aaad2SMiao Xie */ 2222c58aaad2SMiao Xie err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 2223ee3441b4SJeff Mahoney 5, &qgroup_reserved, true); 2224c58aaad2SMiao Xie if (err) 2225c58aaad2SMiao Xie goto out_up_write; 2226c58aaad2SMiao Xie 2227a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 2228a22285a6SYan, Zheng if (IS_ERR(trans)) { 2229a22285a6SYan, Zheng err = PTR_ERR(trans); 2230c58aaad2SMiao Xie goto out_release; 2231a22285a6SYan, Zheng } 2232c58aaad2SMiao Xie trans->block_rsv = &block_rsv; 2233c58aaad2SMiao Xie trans->bytes_reserved = block_rsv.size; 2234a22285a6SYan, Zheng 223576dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 223676dda93cSYan, Zheng dest->root_key.objectid, 223776dda93cSYan, Zheng dentry->d_name.name, 223876dda93cSYan, Zheng dentry->d_name.len); 223979787eaaSJeff Mahoney if (ret) { 224079787eaaSJeff Mahoney err = ret; 224179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 224279787eaaSJeff Mahoney goto out_end_trans; 224379787eaaSJeff Mahoney } 224476dda93cSYan, Zheng 224576dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 224676dda93cSYan, Zheng 224776dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 224876dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 224976dda93cSYan, Zheng dest->root_item.drop_level = 0; 225076dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 225176dda93cSYan, Zheng 2252d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 225376dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 225476dda93cSYan, Zheng root->fs_info->tree_root, 225576dda93cSYan, Zheng dest->root_key.objectid); 225679787eaaSJeff Mahoney if (ret) { 225779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 225879787eaaSJeff Mahoney err = ret; 225979787eaaSJeff Mahoney goto out_end_trans; 2260d68fc57bSYan, Zheng } 226179787eaaSJeff Mahoney } 2262dd5f9615SStefan Behrens 2263dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2264dd5f9615SStefan Behrens dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL, 2265dd5f9615SStefan Behrens dest->root_key.objectid); 2266dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2267dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2268dd5f9615SStefan Behrens err = ret; 2269dd5f9615SStefan Behrens goto out_end_trans; 2270dd5f9615SStefan Behrens } 2271dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2272dd5f9615SStefan Behrens ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 2273dd5f9615SStefan Behrens dest->root_item.received_uuid, 2274dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2275dd5f9615SStefan Behrens dest->root_key.objectid); 2276dd5f9615SStefan Behrens if (ret && ret != -ENOENT) { 2277dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 2278dd5f9615SStefan Behrens err = ret; 2279dd5f9615SStefan Behrens goto out_end_trans; 2280dd5f9615SStefan Behrens } 2281dd5f9615SStefan Behrens } 2282dd5f9615SStefan Behrens 228379787eaaSJeff Mahoney out_end_trans: 2284c58aaad2SMiao Xie trans->block_rsv = NULL; 2285c58aaad2SMiao Xie trans->bytes_reserved = 0; 2286531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 228779787eaaSJeff Mahoney if (ret && !err) 228879787eaaSJeff Mahoney err = ret; 228976dda93cSYan, Zheng inode->i_flags |= S_DEAD; 2290c58aaad2SMiao Xie out_release: 2291c58aaad2SMiao Xie btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 229276dda93cSYan, Zheng out_up_write: 229376dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 229476dda93cSYan, Zheng out_unlock: 229576dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 229676dda93cSYan, Zheng if (!err) { 2297efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 229876dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 229976dda93cSYan, Zheng d_delete(dentry); 2300fa6ac876SLiu Bo 2301fa6ac876SLiu Bo /* the last ref */ 2302fa6ac876SLiu Bo if (dest->cache_inode) { 2303fa6ac876SLiu Bo iput(dest->cache_inode); 2304fa6ac876SLiu Bo dest->cache_inode = NULL; 2305fa6ac876SLiu Bo } 230676dda93cSYan, Zheng } 230776dda93cSYan, Zheng out_dput: 230876dda93cSYan, Zheng dput(dentry); 230976dda93cSYan, Zheng out_unlock_dir: 231076dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 2311e43f998eSDavid Sterba out_drop_write: 23122a79f17eSAl Viro mnt_drop_write_file(file); 231376dda93cSYan, Zheng out: 231476dda93cSYan, Zheng kfree(vol_args); 231576dda93cSYan, Zheng return err; 231676dda93cSYan, Zheng } 231776dda93cSYan, Zheng 23181e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 2319f46b5a66SChristoph Hellwig { 2320496ad9aaSAl Viro struct inode *inode = file_inode(file); 2321f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 23221e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 2323c146afadSYan Zheng int ret; 2324c146afadSYan Zheng 232525122d15SIlya Dryomov ret = mnt_want_write_file(file); 232625122d15SIlya Dryomov if (ret) 232725122d15SIlya Dryomov return ret; 2328b83cc969SLi Zefan 232925122d15SIlya Dryomov if (btrfs_root_readonly(root)) { 233025122d15SIlya Dryomov ret = -EROFS; 233125122d15SIlya Dryomov goto out; 23325ac00addSStefan Behrens } 2333f46b5a66SChristoph Hellwig 2334f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2335f46b5a66SChristoph Hellwig case S_IFDIR: 2336e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 2337e441d54dSChris Mason ret = -EPERM; 2338e441d54dSChris Mason goto out; 2339e441d54dSChris Mason } 2340de78b51aSEric Sandeen ret = btrfs_defrag_root(root); 23418929ecfaSYan, Zheng if (ret) 23428929ecfaSYan, Zheng goto out; 2343de78b51aSEric Sandeen ret = btrfs_defrag_root(root->fs_info->extent_root); 2344f46b5a66SChristoph Hellwig break; 2345f46b5a66SChristoph Hellwig case S_IFREG: 2346e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 2347e441d54dSChris Mason ret = -EINVAL; 2348e441d54dSChris Mason goto out; 2349e441d54dSChris Mason } 23501e701a32SChris Mason 23511e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 23521e701a32SChris Mason if (!range) { 23531e701a32SChris Mason ret = -ENOMEM; 23541e701a32SChris Mason goto out; 23551e701a32SChris Mason } 23561e701a32SChris Mason 23571e701a32SChris Mason if (argp) { 23581e701a32SChris Mason if (copy_from_user(range, argp, 23591e701a32SChris Mason sizeof(*range))) { 23601e701a32SChris Mason ret = -EFAULT; 23611e701a32SChris Mason kfree(range); 2362683be16eSDan Carpenter goto out; 23631e701a32SChris Mason } 23641e701a32SChris Mason /* compression requires us to start the IO */ 23651e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 23661e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 23671e701a32SChris Mason range->extent_thresh = (u32)-1; 23681e701a32SChris Mason } 23691e701a32SChris Mason } else { 23701e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 23711e701a32SChris Mason range->len = (u64)-1; 23721e701a32SChris Mason } 2373496ad9aaSAl Viro ret = btrfs_defrag_file(file_inode(file), file, 23744cb5300bSChris Mason range, 0, 0); 23754cb5300bSChris Mason if (ret > 0) 23764cb5300bSChris Mason ret = 0; 23771e701a32SChris Mason kfree(range); 2378f46b5a66SChristoph Hellwig break; 23798929ecfaSYan, Zheng default: 23808929ecfaSYan, Zheng ret = -EINVAL; 2381f46b5a66SChristoph Hellwig } 2382e441d54dSChris Mason out: 238325122d15SIlya Dryomov mnt_drop_write_file(file); 2384e441d54dSChris Mason return ret; 2385f46b5a66SChristoph Hellwig } 2386f46b5a66SChristoph Hellwig 2387b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2388f46b5a66SChristoph Hellwig { 2389f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2390f46b5a66SChristoph Hellwig int ret; 2391f46b5a66SChristoph Hellwig 2392e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2393e441d54dSChris Mason return -EPERM; 2394e441d54dSChris Mason 23955ac00addSStefan Behrens if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 23965ac00addSStefan Behrens 1)) { 2397e57138b3SAnand Jain return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2398c9e9f97bSIlya Dryomov } 2399c9e9f97bSIlya Dryomov 24005ac00addSStefan Behrens mutex_lock(&root->fs_info->volume_mutex); 2401dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2402c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2403c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2404c9e9f97bSIlya Dryomov goto out; 2405c9e9f97bSIlya Dryomov } 2406f46b5a66SChristoph Hellwig 24075516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2408f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 2409f46b5a66SChristoph Hellwig 2410f46b5a66SChristoph Hellwig kfree(vol_args); 2411c9e9f97bSIlya Dryomov out: 2412c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 24135ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2414f46b5a66SChristoph Hellwig return ret; 2415f46b5a66SChristoph Hellwig } 2416f46b5a66SChristoph Hellwig 2417da24927bSMiao Xie static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2418f46b5a66SChristoph Hellwig { 2419496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2420f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 2421f46b5a66SChristoph Hellwig int ret; 2422f46b5a66SChristoph Hellwig 2423e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2424e441d54dSChris Mason return -EPERM; 2425e441d54dSChris Mason 2426da24927bSMiao Xie ret = mnt_want_write_file(file); 2427da24927bSMiao Xie if (ret) 2428da24927bSMiao Xie return ret; 2429c146afadSYan Zheng 2430dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 2431c9e9f97bSIlya Dryomov if (IS_ERR(vol_args)) { 2432c9e9f97bSIlya Dryomov ret = PTR_ERR(vol_args); 2433c9e9f97bSIlya Dryomov goto out; 2434c9e9f97bSIlya Dryomov } 2435f46b5a66SChristoph Hellwig 24365516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2437f46b5a66SChristoph Hellwig 2438183860f6SAnand Jain if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2439183860f6SAnand Jain 1)) { 2440183860f6SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2441183860f6SAnand Jain goto out; 2442183860f6SAnand Jain } 2443183860f6SAnand Jain 2444183860f6SAnand Jain mutex_lock(&root->fs_info->volume_mutex); 2445183860f6SAnand Jain ret = btrfs_rm_device(root, vol_args->name); 2446c9e9f97bSIlya Dryomov mutex_unlock(&root->fs_info->volume_mutex); 24475ac00addSStefan Behrens atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2448183860f6SAnand Jain 2449183860f6SAnand Jain out: 2450183860f6SAnand Jain kfree(vol_args); 24514ac20c70SIlya Dryomov mnt_drop_write_file(file); 2452f46b5a66SChristoph Hellwig return ret; 2453f46b5a66SChristoph Hellwig } 2454f46b5a66SChristoph Hellwig 2455475f6387SJan Schmidt static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2456475f6387SJan Schmidt { 2457027ed2f0SLi Zefan struct btrfs_ioctl_fs_info_args *fi_args; 2458475f6387SJan Schmidt struct btrfs_device *device; 2459475f6387SJan Schmidt struct btrfs_device *next; 2460475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2461027ed2f0SLi Zefan int ret = 0; 2462475f6387SJan Schmidt 2463475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2464475f6387SJan Schmidt return -EPERM; 2465475f6387SJan Schmidt 2466027ed2f0SLi Zefan fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2467027ed2f0SLi Zefan if (!fi_args) 2468027ed2f0SLi Zefan return -ENOMEM; 2469027ed2f0SLi Zefan 2470f7171750SFilipe David Borba Manana mutex_lock(&fs_devices->device_list_mutex); 2471027ed2f0SLi Zefan fi_args->num_devices = fs_devices->num_devices; 2472027ed2f0SLi Zefan memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2473475f6387SJan Schmidt 2474475f6387SJan Schmidt list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { 2475027ed2f0SLi Zefan if (device->devid > fi_args->max_id) 2476027ed2f0SLi Zefan fi_args->max_id = device->devid; 2477475f6387SJan Schmidt } 2478475f6387SJan Schmidt mutex_unlock(&fs_devices->device_list_mutex); 2479475f6387SJan Schmidt 2480027ed2f0SLi Zefan if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2481027ed2f0SLi Zefan ret = -EFAULT; 2482475f6387SJan Schmidt 2483027ed2f0SLi Zefan kfree(fi_args); 2484027ed2f0SLi Zefan return ret; 2485475f6387SJan Schmidt } 2486475f6387SJan Schmidt 2487475f6387SJan Schmidt static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2488475f6387SJan Schmidt { 2489475f6387SJan Schmidt struct btrfs_ioctl_dev_info_args *di_args; 2490475f6387SJan Schmidt struct btrfs_device *dev; 2491475f6387SJan Schmidt struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2492475f6387SJan Schmidt int ret = 0; 2493475f6387SJan Schmidt char *s_uuid = NULL; 2494475f6387SJan Schmidt 2495475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 2496475f6387SJan Schmidt return -EPERM; 2497475f6387SJan Schmidt 2498475f6387SJan Schmidt di_args = memdup_user(arg, sizeof(*di_args)); 2499475f6387SJan Schmidt if (IS_ERR(di_args)) 2500475f6387SJan Schmidt return PTR_ERR(di_args); 2501475f6387SJan Schmidt 2502dd5f9615SStefan Behrens if (!btrfs_is_empty_uuid(di_args->uuid)) 2503475f6387SJan Schmidt s_uuid = di_args->uuid; 2504475f6387SJan Schmidt 2505475f6387SJan Schmidt mutex_lock(&fs_devices->device_list_mutex); 2506aa1b8cd4SStefan Behrens dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2507475f6387SJan Schmidt 2508475f6387SJan Schmidt if (!dev) { 2509475f6387SJan Schmidt ret = -ENODEV; 2510475f6387SJan Schmidt goto out; 2511475f6387SJan Schmidt } 2512475f6387SJan Schmidt 2513475f6387SJan Schmidt di_args->devid = dev->devid; 2514475f6387SJan Schmidt di_args->bytes_used = dev->bytes_used; 2515475f6387SJan Schmidt di_args->total_bytes = dev->total_bytes; 2516475f6387SJan Schmidt memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); 2517a27202fbSJim Meyering if (dev->name) { 2518606686eeSJosef Bacik struct rcu_string *name; 2519606686eeSJosef Bacik 2520606686eeSJosef Bacik rcu_read_lock(); 2521606686eeSJosef Bacik name = rcu_dereference(dev->name); 2522606686eeSJosef Bacik strncpy(di_args->path, name->str, sizeof(di_args->path)); 2523606686eeSJosef Bacik rcu_read_unlock(); 2524a27202fbSJim Meyering di_args->path[sizeof(di_args->path) - 1] = 0; 2525a27202fbSJim Meyering } else { 252699ba55adSStefan Behrens di_args->path[0] = '\0'; 2527a27202fbSJim Meyering } 2528475f6387SJan Schmidt 2529475f6387SJan Schmidt out: 253055793c0dSDavid Sterba mutex_unlock(&fs_devices->device_list_mutex); 2531475f6387SJan Schmidt if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) 2532475f6387SJan Schmidt ret = -EFAULT; 2533475f6387SJan Schmidt 2534475f6387SJan Schmidt kfree(di_args); 2535475f6387SJan Schmidt return ret; 2536475f6387SJan Schmidt } 2537475f6387SJan Schmidt 2538416161dbSMark Fasheh static struct page *extent_same_get_page(struct inode *inode, u64 off) 2539416161dbSMark Fasheh { 2540416161dbSMark Fasheh struct page *page; 2541416161dbSMark Fasheh pgoff_t index; 2542416161dbSMark Fasheh struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; 2543416161dbSMark Fasheh 2544416161dbSMark Fasheh index = off >> PAGE_CACHE_SHIFT; 2545416161dbSMark Fasheh 2546416161dbSMark Fasheh page = grab_cache_page(inode->i_mapping, index); 2547416161dbSMark Fasheh if (!page) 2548416161dbSMark Fasheh return NULL; 2549416161dbSMark Fasheh 2550416161dbSMark Fasheh if (!PageUptodate(page)) { 2551416161dbSMark Fasheh if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, 2552416161dbSMark Fasheh 0)) 2553416161dbSMark Fasheh return NULL; 2554416161dbSMark Fasheh lock_page(page); 2555416161dbSMark Fasheh if (!PageUptodate(page)) { 2556416161dbSMark Fasheh unlock_page(page); 2557416161dbSMark Fasheh page_cache_release(page); 2558416161dbSMark Fasheh return NULL; 2559416161dbSMark Fasheh } 2560416161dbSMark Fasheh } 2561416161dbSMark Fasheh unlock_page(page); 2562416161dbSMark Fasheh 2563416161dbSMark Fasheh return page; 2564416161dbSMark Fasheh } 2565416161dbSMark Fasheh 256677fe20dcSMark Fasheh static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) 256777fe20dcSMark Fasheh { 256877fe20dcSMark Fasheh /* do any pending delalloc/csum calc on src, one way or 256977fe20dcSMark Fasheh another, and lock file content */ 257077fe20dcSMark Fasheh while (1) { 257177fe20dcSMark Fasheh struct btrfs_ordered_extent *ordered; 257277fe20dcSMark Fasheh lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 257377fe20dcSMark Fasheh ordered = btrfs_lookup_first_ordered_extent(inode, 257477fe20dcSMark Fasheh off + len - 1); 257577fe20dcSMark Fasheh if (!ordered && 257677fe20dcSMark Fasheh !test_range_bit(&BTRFS_I(inode)->io_tree, off, 257777fe20dcSMark Fasheh off + len - 1, EXTENT_DELALLOC, 0, NULL)) 257877fe20dcSMark Fasheh break; 257977fe20dcSMark Fasheh unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); 258077fe20dcSMark Fasheh if (ordered) 258177fe20dcSMark Fasheh btrfs_put_ordered_extent(ordered); 258277fe20dcSMark Fasheh btrfs_wait_ordered_range(inode, off, len); 258377fe20dcSMark Fasheh } 258477fe20dcSMark Fasheh } 258577fe20dcSMark Fasheh 2586416161dbSMark Fasheh static void btrfs_double_unlock(struct inode *inode1, u64 loff1, 2587416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2588416161dbSMark Fasheh { 2589416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); 2590416161dbSMark Fasheh unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); 2591416161dbSMark Fasheh 2592416161dbSMark Fasheh mutex_unlock(&inode1->i_mutex); 2593416161dbSMark Fasheh mutex_unlock(&inode2->i_mutex); 2594416161dbSMark Fasheh } 2595416161dbSMark Fasheh 2596416161dbSMark Fasheh static void btrfs_double_lock(struct inode *inode1, u64 loff1, 2597416161dbSMark Fasheh struct inode *inode2, u64 loff2, u64 len) 2598416161dbSMark Fasheh { 2599416161dbSMark Fasheh if (inode1 < inode2) { 2600416161dbSMark Fasheh swap(inode1, inode2); 2601416161dbSMark Fasheh swap(loff1, loff2); 2602416161dbSMark Fasheh } 2603416161dbSMark Fasheh 2604416161dbSMark Fasheh mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); 2605416161dbSMark Fasheh lock_extent_range(inode1, loff1, len); 2606416161dbSMark Fasheh if (inode1 != inode2) { 2607416161dbSMark Fasheh mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); 2608416161dbSMark Fasheh lock_extent_range(inode2, loff2, len); 2609416161dbSMark Fasheh } 2610416161dbSMark Fasheh } 2611416161dbSMark Fasheh 2612416161dbSMark Fasheh static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, 2613416161dbSMark Fasheh u64 dst_loff, u64 len) 2614416161dbSMark Fasheh { 2615416161dbSMark Fasheh int ret = 0; 2616416161dbSMark Fasheh struct page *src_page, *dst_page; 2617416161dbSMark Fasheh unsigned int cmp_len = PAGE_CACHE_SIZE; 2618416161dbSMark Fasheh void *addr, *dst_addr; 2619416161dbSMark Fasheh 2620416161dbSMark Fasheh while (len) { 2621416161dbSMark Fasheh if (len < PAGE_CACHE_SIZE) 2622416161dbSMark Fasheh cmp_len = len; 2623416161dbSMark Fasheh 2624416161dbSMark Fasheh src_page = extent_same_get_page(src, loff); 2625416161dbSMark Fasheh if (!src_page) 2626416161dbSMark Fasheh return -EINVAL; 2627416161dbSMark Fasheh dst_page = extent_same_get_page(dst, dst_loff); 2628416161dbSMark Fasheh if (!dst_page) { 2629416161dbSMark Fasheh page_cache_release(src_page); 2630416161dbSMark Fasheh return -EINVAL; 2631416161dbSMark Fasheh } 2632416161dbSMark Fasheh addr = kmap_atomic(src_page); 2633416161dbSMark Fasheh dst_addr = kmap_atomic(dst_page); 2634416161dbSMark Fasheh 2635416161dbSMark Fasheh flush_dcache_page(src_page); 2636416161dbSMark Fasheh flush_dcache_page(dst_page); 2637416161dbSMark Fasheh 2638416161dbSMark Fasheh if (memcmp(addr, dst_addr, cmp_len)) 2639416161dbSMark Fasheh ret = BTRFS_SAME_DATA_DIFFERS; 2640416161dbSMark Fasheh 2641416161dbSMark Fasheh kunmap_atomic(addr); 2642416161dbSMark Fasheh kunmap_atomic(dst_addr); 2643416161dbSMark Fasheh page_cache_release(src_page); 2644416161dbSMark Fasheh page_cache_release(dst_page); 2645416161dbSMark Fasheh 2646416161dbSMark Fasheh if (ret) 2647416161dbSMark Fasheh break; 2648416161dbSMark Fasheh 2649416161dbSMark Fasheh loff += cmp_len; 2650416161dbSMark Fasheh dst_loff += cmp_len; 2651416161dbSMark Fasheh len -= cmp_len; 2652416161dbSMark Fasheh } 2653416161dbSMark Fasheh 2654416161dbSMark Fasheh return ret; 2655416161dbSMark Fasheh } 2656416161dbSMark Fasheh 2657416161dbSMark Fasheh static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) 2658416161dbSMark Fasheh { 2659416161dbSMark Fasheh u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; 2660416161dbSMark Fasheh 2661416161dbSMark Fasheh if (off + len > inode->i_size || off + len < off) 2662416161dbSMark Fasheh return -EINVAL; 2663416161dbSMark Fasheh /* Check that we are block aligned - btrfs_clone() requires this */ 2664416161dbSMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) 2665416161dbSMark Fasheh return -EINVAL; 2666416161dbSMark Fasheh 2667416161dbSMark Fasheh return 0; 2668416161dbSMark Fasheh } 2669416161dbSMark Fasheh 2670416161dbSMark Fasheh static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, 2671416161dbSMark Fasheh struct inode *dst, u64 dst_loff) 2672416161dbSMark Fasheh { 2673416161dbSMark Fasheh int ret; 2674416161dbSMark Fasheh 2675416161dbSMark Fasheh /* 2676416161dbSMark Fasheh * btrfs_clone() can't handle extents in the same file 2677416161dbSMark Fasheh * yet. Once that works, we can drop this check and replace it 2678416161dbSMark Fasheh * with a check for the same inode, but overlapping extents. 2679416161dbSMark Fasheh */ 2680416161dbSMark Fasheh if (src == dst) 2681416161dbSMark Fasheh return -EINVAL; 2682416161dbSMark Fasheh 2683416161dbSMark Fasheh btrfs_double_lock(src, loff, dst, dst_loff, len); 2684416161dbSMark Fasheh 2685416161dbSMark Fasheh ret = extent_same_check_offsets(src, loff, len); 2686416161dbSMark Fasheh if (ret) 2687416161dbSMark Fasheh goto out_unlock; 2688416161dbSMark Fasheh 2689416161dbSMark Fasheh ret = extent_same_check_offsets(dst, dst_loff, len); 2690416161dbSMark Fasheh if (ret) 2691416161dbSMark Fasheh goto out_unlock; 2692416161dbSMark Fasheh 2693416161dbSMark Fasheh /* don't make the dst file partly checksummed */ 2694416161dbSMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 2695416161dbSMark Fasheh (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { 2696416161dbSMark Fasheh ret = -EINVAL; 2697416161dbSMark Fasheh goto out_unlock; 2698416161dbSMark Fasheh } 2699416161dbSMark Fasheh 2700416161dbSMark Fasheh ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); 2701416161dbSMark Fasheh if (ret == 0) 2702416161dbSMark Fasheh ret = btrfs_clone(src, dst, loff, len, len, dst_loff); 2703416161dbSMark Fasheh 2704416161dbSMark Fasheh out_unlock: 2705416161dbSMark Fasheh btrfs_double_unlock(src, loff, dst, dst_loff, len); 2706416161dbSMark Fasheh 2707416161dbSMark Fasheh return ret; 2708416161dbSMark Fasheh } 2709416161dbSMark Fasheh 2710416161dbSMark Fasheh #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) 2711416161dbSMark Fasheh 2712416161dbSMark Fasheh static long btrfs_ioctl_file_extent_same(struct file *file, 2713416161dbSMark Fasheh void __user *argp) 2714416161dbSMark Fasheh { 2715cbf8b8caSMark Fasheh struct btrfs_ioctl_same_args tmp; 2716cbf8b8caSMark Fasheh struct btrfs_ioctl_same_args *same; 2717cbf8b8caSMark Fasheh struct btrfs_ioctl_same_extent_info *info; 2718416161dbSMark Fasheh struct inode *src = file->f_dentry->d_inode; 2719416161dbSMark Fasheh struct file *dst_file = NULL; 2720416161dbSMark Fasheh struct inode *dst; 2721416161dbSMark Fasheh u64 off; 2722416161dbSMark Fasheh u64 len; 2723416161dbSMark Fasheh int i; 2724416161dbSMark Fasheh int ret; 2725cbf8b8caSMark Fasheh unsigned long size; 2726416161dbSMark Fasheh u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; 2727416161dbSMark Fasheh bool is_admin = capable(CAP_SYS_ADMIN); 2728416161dbSMark Fasheh 2729416161dbSMark Fasheh if (!(file->f_mode & FMODE_READ)) 2730416161dbSMark Fasheh return -EINVAL; 2731416161dbSMark Fasheh 2732416161dbSMark Fasheh ret = mnt_want_write_file(file); 2733416161dbSMark Fasheh if (ret) 2734416161dbSMark Fasheh return ret; 2735416161dbSMark Fasheh 2736cbf8b8caSMark Fasheh if (copy_from_user(&tmp, 2737416161dbSMark Fasheh (struct btrfs_ioctl_same_args __user *)argp, 2738cbf8b8caSMark Fasheh sizeof(tmp))) { 2739416161dbSMark Fasheh ret = -EFAULT; 2740416161dbSMark Fasheh goto out; 2741416161dbSMark Fasheh } 2742416161dbSMark Fasheh 2743cbf8b8caSMark Fasheh size = sizeof(tmp) + 2744cbf8b8caSMark Fasheh tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info); 2745cbf8b8caSMark Fasheh 2746229eed43SGeyslan G. Bem same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size); 2747cbf8b8caSMark Fasheh 2748229eed43SGeyslan G. Bem if (IS_ERR(same)) { 2749229eed43SGeyslan G. Bem ret = PTR_ERR(same); 2750cbf8b8caSMark Fasheh goto out; 2751cbf8b8caSMark Fasheh } 2752cbf8b8caSMark Fasheh 2753cbf8b8caSMark Fasheh off = same->logical_offset; 2754cbf8b8caSMark Fasheh len = same->length; 2755416161dbSMark Fasheh 2756416161dbSMark Fasheh /* 2757416161dbSMark Fasheh * Limit the total length we will dedupe for each operation. 2758416161dbSMark Fasheh * This is intended to bound the total time spent in this 2759416161dbSMark Fasheh * ioctl to something sane. 2760416161dbSMark Fasheh */ 2761416161dbSMark Fasheh if (len > BTRFS_MAX_DEDUPE_LEN) 2762416161dbSMark Fasheh len = BTRFS_MAX_DEDUPE_LEN; 2763416161dbSMark Fasheh 2764416161dbSMark Fasheh if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { 2765416161dbSMark Fasheh /* 2766416161dbSMark Fasheh * Btrfs does not support blocksize < page_size. As a 2767416161dbSMark Fasheh * result, btrfs_cmp_data() won't correctly handle 2768416161dbSMark Fasheh * this situation without an update. 2769416161dbSMark Fasheh */ 2770416161dbSMark Fasheh ret = -EINVAL; 2771416161dbSMark Fasheh goto out; 2772416161dbSMark Fasheh } 2773416161dbSMark Fasheh 2774416161dbSMark Fasheh ret = -EISDIR; 2775416161dbSMark Fasheh if (S_ISDIR(src->i_mode)) 2776416161dbSMark Fasheh goto out; 2777416161dbSMark Fasheh 2778416161dbSMark Fasheh ret = -EACCES; 2779416161dbSMark Fasheh if (!S_ISREG(src->i_mode)) 2780416161dbSMark Fasheh goto out; 2781416161dbSMark Fasheh 2782cbf8b8caSMark Fasheh /* pre-format output fields to sane values */ 2783cbf8b8caSMark Fasheh for (i = 0; i < same->dest_count; i++) { 2784cbf8b8caSMark Fasheh same->info[i].bytes_deduped = 0ULL; 2785cbf8b8caSMark Fasheh same->info[i].status = 0; 2786416161dbSMark Fasheh } 2787416161dbSMark Fasheh 2788cbf8b8caSMark Fasheh ret = 0; 2789cbf8b8caSMark Fasheh for (i = 0; i < same->dest_count; i++) { 2790cbf8b8caSMark Fasheh info = &same->info[i]; 2791416161dbSMark Fasheh 2792cbf8b8caSMark Fasheh dst_file = fget(info->fd); 2793416161dbSMark Fasheh if (!dst_file) { 2794cbf8b8caSMark Fasheh info->status = -EBADF; 2795416161dbSMark Fasheh goto next; 2796416161dbSMark Fasheh } 2797416161dbSMark Fasheh 2798416161dbSMark Fasheh if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { 2799cbf8b8caSMark Fasheh info->status = -EINVAL; 2800416161dbSMark Fasheh goto next; 2801416161dbSMark Fasheh } 2802416161dbSMark Fasheh 2803cbf8b8caSMark Fasheh info->status = -EXDEV; 2804416161dbSMark Fasheh if (file->f_path.mnt != dst_file->f_path.mnt) 2805416161dbSMark Fasheh goto next; 2806416161dbSMark Fasheh 2807416161dbSMark Fasheh dst = dst_file->f_dentry->d_inode; 2808416161dbSMark Fasheh if (src->i_sb != dst->i_sb) 2809416161dbSMark Fasheh goto next; 2810416161dbSMark Fasheh 2811416161dbSMark Fasheh if (S_ISDIR(dst->i_mode)) { 2812cbf8b8caSMark Fasheh info->status = -EISDIR; 2813416161dbSMark Fasheh goto next; 2814416161dbSMark Fasheh } 2815416161dbSMark Fasheh 2816416161dbSMark Fasheh if (!S_ISREG(dst->i_mode)) { 2817cbf8b8caSMark Fasheh info->status = -EACCES; 2818416161dbSMark Fasheh goto next; 2819416161dbSMark Fasheh } 2820416161dbSMark Fasheh 2821cbf8b8caSMark Fasheh info->status = btrfs_extent_same(src, off, len, dst, 2822cbf8b8caSMark Fasheh info->logical_offset); 2823cbf8b8caSMark Fasheh if (info->status == 0) 2824cbf8b8caSMark Fasheh info->bytes_deduped += len; 2825416161dbSMark Fasheh 2826416161dbSMark Fasheh next: 2827416161dbSMark Fasheh if (dst_file) 2828416161dbSMark Fasheh fput(dst_file); 2829cbf8b8caSMark Fasheh } 2830416161dbSMark Fasheh 2831cbf8b8caSMark Fasheh ret = copy_to_user(argp, same, size); 2832cbf8b8caSMark Fasheh if (ret) 2833416161dbSMark Fasheh ret = -EFAULT; 2834416161dbSMark Fasheh 2835416161dbSMark Fasheh out: 2836416161dbSMark Fasheh mnt_drop_write_file(file); 2837416161dbSMark Fasheh return ret; 2838416161dbSMark Fasheh } 2839416161dbSMark Fasheh 284032b7c687SMark Fasheh /** 284132b7c687SMark Fasheh * btrfs_clone() - clone a range from inode file to another 284232b7c687SMark Fasheh * 284332b7c687SMark Fasheh * @src: Inode to clone from 284432b7c687SMark Fasheh * @inode: Inode to clone to 284532b7c687SMark Fasheh * @off: Offset within source to start clone from 284632b7c687SMark Fasheh * @olen: Original length, passed by user, of range to clone 284732b7c687SMark Fasheh * @olen_aligned: Block-aligned value of olen, extent_same uses 284832b7c687SMark Fasheh * identical values here 284932b7c687SMark Fasheh * @destoff: Offset within @inode to start clone 285032b7c687SMark Fasheh */ 285132b7c687SMark Fasheh static int btrfs_clone(struct inode *src, struct inode *inode, 285232b7c687SMark Fasheh u64 off, u64 olen, u64 olen_aligned, u64 destoff) 2853f46b5a66SChristoph Hellwig { 2854f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 285532b7c687SMark Fasheh struct btrfs_path *path = NULL; 2856f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 285732b7c687SMark Fasheh struct btrfs_trans_handle *trans; 285832b7c687SMark Fasheh char *buf = NULL; 2859ae01a0abSYan Zheng struct btrfs_key key; 2860f46b5a66SChristoph Hellwig u32 nritems; 2861f46b5a66SChristoph Hellwig int slot; 2862ae01a0abSYan Zheng int ret; 286332b7c687SMark Fasheh u64 len = olen_aligned; 2864ae01a0abSYan Zheng 2865ae01a0abSYan Zheng ret = -ENOMEM; 2866ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 2867ae01a0abSYan Zheng if (!buf) 286832b7c687SMark Fasheh return ret; 2869ae01a0abSYan Zheng 2870ae01a0abSYan Zheng path = btrfs_alloc_path(); 2871ae01a0abSYan Zheng if (!path) { 2872ae01a0abSYan Zheng vfree(buf); 287332b7c687SMark Fasheh return ret; 2874ae01a0abSYan Zheng } 287532b7c687SMark Fasheh 2876ae01a0abSYan Zheng path->reada = 2; 2877c5c9cd4dSSage Weil /* clone data */ 287833345d01SLi Zefan key.objectid = btrfs_ino(src); 2879ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 2880ae01a0abSYan Zheng key.offset = 0; 2881f46b5a66SChristoph Hellwig 2882f46b5a66SChristoph Hellwig while (1) { 2883f46b5a66SChristoph Hellwig /* 2884f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 2885f46b5a66SChristoph Hellwig * tree. 2886f46b5a66SChristoph Hellwig */ 2887362a20c5SDavid Sterba ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, 2888362a20c5SDavid Sterba 0, 0); 2889f46b5a66SChristoph Hellwig if (ret < 0) 2890f46b5a66SChristoph Hellwig goto out; 2891f46b5a66SChristoph Hellwig 2892ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2893ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 2894362a20c5SDavid Sterba ret = btrfs_next_leaf(BTRFS_I(src)->root, path); 2895f46b5a66SChristoph Hellwig if (ret < 0) 2896f46b5a66SChristoph Hellwig goto out; 2897f46b5a66SChristoph Hellwig if (ret > 0) 2898f46b5a66SChristoph Hellwig break; 2899ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 2900f46b5a66SChristoph Hellwig } 2901f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 2902f46b5a66SChristoph Hellwig slot = path->slots[0]; 2903f46b5a66SChristoph Hellwig 2904ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 2905d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 290633345d01SLi Zefan key.objectid != btrfs_ino(src)) 2907f46b5a66SChristoph Hellwig break; 2908f46b5a66SChristoph Hellwig 2909c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 2910c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 2911c5c9cd4dSSage Weil int type; 291231840ae1SZheng Yan u32 size; 291331840ae1SZheng Yan struct btrfs_key new_key; 2914c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 2915c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 2916c5c9cd4dSSage Weil u8 comp; 2917b5384d48SSage Weil u64 endoff; 291831840ae1SZheng Yan 291931840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 292031840ae1SZheng Yan read_extent_buffer(leaf, buf, 292131840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 292231840ae1SZheng Yan size); 2923c5c9cd4dSSage Weil 2924c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 2925c5c9cd4dSSage Weil struct btrfs_file_extent_item); 2926c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 2927c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 2928c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2929c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2930d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 2931d397712bSChris Mason extent); 2932d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 2933d397712bSChris Mason extent); 2934c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 2935d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 2936d397712bSChris Mason extent); 2937c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 2938c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 2939c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 2940c5c9cd4dSSage Weil extent); 2941c5c9cd4dSSage Weil } 2942b3b4aa74SDavid Sterba btrfs_release_path(path); 294331840ae1SZheng Yan 2944050006a7SSage Weil if (key.offset + datal <= off || 2945aa42ffd9SLiu Bo key.offset >= off + len - 1) 2946c5c9cd4dSSage Weil goto next; 2947c5c9cd4dSSage Weil 294831840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 294933345d01SLi Zefan new_key.objectid = btrfs_ino(inode); 29504d728ec7SLi Zefan if (off <= key.offset) 2951c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 29524d728ec7SLi Zefan else 29534d728ec7SLi Zefan new_key.offset = destoff; 2954c5c9cd4dSSage Weil 2955b6f3409bSSage Weil /* 2956b6f3409bSSage Weil * 1 - adjusting old extent (we may have to split it) 2957b6f3409bSSage Weil * 1 - add new extent 2958b6f3409bSSage Weil * 1 - inode update 2959b6f3409bSSage Weil */ 2960b6f3409bSSage Weil trans = btrfs_start_transaction(root, 3); 2961a22285a6SYan, Zheng if (IS_ERR(trans)) { 2962a22285a6SYan, Zheng ret = PTR_ERR(trans); 2963a22285a6SYan, Zheng goto out; 2964a22285a6SYan, Zheng } 2965a22285a6SYan, Zheng 2966c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 2967c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 2968d72c0842SLi Zefan /* 2969d72c0842SLi Zefan * a | --- range to clone ---| b 2970d72c0842SLi Zefan * | ------------- extent ------------- | 2971d72c0842SLi Zefan */ 2972d72c0842SLi Zefan 2973d72c0842SLi Zefan /* substract range b */ 2974d72c0842SLi Zefan if (key.offset + datal > off + len) 2975d72c0842SLi Zefan datal = off + len - key.offset; 2976d72c0842SLi Zefan 2977d72c0842SLi Zefan /* substract range a */ 2978a22285a6SYan, Zheng if (off > key.offset) { 2979a22285a6SYan, Zheng datao += off - key.offset; 2980a22285a6SYan, Zheng datal -= off - key.offset; 2981a22285a6SYan, Zheng } 2982a22285a6SYan, Zheng 29835dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 2984a22285a6SYan, Zheng new_key.offset, 2985a22285a6SYan, Zheng new_key.offset + datal, 29862671485dSJosef Bacik 1); 298779787eaaSJeff Mahoney if (ret) { 298879787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 298979787eaaSJeff Mahoney ret); 299079787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 299179787eaaSJeff Mahoney goto out; 299279787eaaSJeff Mahoney } 2993a22285a6SYan, Zheng 299431840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 299531840ae1SZheng Yan &new_key, size); 299679787eaaSJeff Mahoney if (ret) { 299779787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 299879787eaaSJeff Mahoney ret); 299979787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 300079787eaaSJeff Mahoney goto out; 300179787eaaSJeff Mahoney } 300231840ae1SZheng Yan 300331840ae1SZheng Yan leaf = path->nodes[0]; 300431840ae1SZheng Yan slot = path->slots[0]; 300531840ae1SZheng Yan write_extent_buffer(leaf, buf, 300631840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 300731840ae1SZheng Yan size); 3008ae01a0abSYan Zheng 3009f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 3010f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 3011c5c9cd4dSSage Weil 3012c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 3013c5c9cd4dSSage Weil if (!disko) 3014c5c9cd4dSSage Weil datao = 0; 3015c5c9cd4dSSage Weil 3016c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 3017c5c9cd4dSSage Weil datao); 3018c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 3019c5c9cd4dSSage Weil datal); 3020c5c9cd4dSSage Weil if (disko) { 3021c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3022ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 30235d4f98a2SYan Zheng disko, diskl, 0, 3024f46b5a66SChristoph Hellwig root->root_key.objectid, 302533345d01SLi Zefan btrfs_ino(inode), 302666d7e7f0SArne Jansen new_key.offset - datao, 302766d7e7f0SArne Jansen 0); 302879787eaaSJeff Mahoney if (ret) { 302979787eaaSJeff Mahoney btrfs_abort_transaction(trans, 303079787eaaSJeff Mahoney root, 303179787eaaSJeff Mahoney ret); 303279787eaaSJeff Mahoney btrfs_end_transaction(trans, 303379787eaaSJeff Mahoney root); 303479787eaaSJeff Mahoney goto out; 303579787eaaSJeff Mahoney 303679787eaaSJeff Mahoney } 3037f46b5a66SChristoph Hellwig } 3038c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 3039c5c9cd4dSSage Weil u64 skip = 0; 3040c5c9cd4dSSage Weil u64 trim = 0; 3041c5c9cd4dSSage Weil if (off > key.offset) { 3042c5c9cd4dSSage Weil skip = off - key.offset; 3043c5c9cd4dSSage Weil new_key.offset += skip; 304431840ae1SZheng Yan } 3045d397712bSChris Mason 3046c5c9cd4dSSage Weil if (key.offset + datal > off + len) 3047c5c9cd4dSSage Weil trim = key.offset + datal - (off + len); 3048d397712bSChris Mason 3049c5c9cd4dSSage Weil if (comp && (skip || trim)) { 3050c5c9cd4dSSage Weil ret = -EINVAL; 3051a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 3052c5c9cd4dSSage Weil goto out; 305331840ae1SZheng Yan } 3054c5c9cd4dSSage Weil size -= skip + trim; 3055c5c9cd4dSSage Weil datal -= skip + trim; 3056a22285a6SYan, Zheng 30575dc562c5SJosef Bacik ret = btrfs_drop_extents(trans, root, inode, 3058a22285a6SYan, Zheng new_key.offset, 3059a22285a6SYan, Zheng new_key.offset + datal, 30602671485dSJosef Bacik 1); 306179787eaaSJeff Mahoney if (ret) { 306279787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 306379787eaaSJeff Mahoney ret); 306479787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 306579787eaaSJeff Mahoney goto out; 306679787eaaSJeff Mahoney } 3067a22285a6SYan, Zheng 3068c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 3069c5c9cd4dSSage Weil &new_key, size); 307079787eaaSJeff Mahoney if (ret) { 307179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, 307279787eaaSJeff Mahoney ret); 307379787eaaSJeff Mahoney btrfs_end_transaction(trans, root); 307479787eaaSJeff Mahoney goto out; 307579787eaaSJeff Mahoney } 3076c5c9cd4dSSage Weil 3077c5c9cd4dSSage Weil if (skip) { 3078d397712bSChris Mason u32 start = 3079d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 3080c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 3081c5c9cd4dSSage Weil datal); 3082c5c9cd4dSSage Weil } 3083c5c9cd4dSSage Weil 3084c5c9cd4dSSage Weil leaf = path->nodes[0]; 3085c5c9cd4dSSage Weil slot = path->slots[0]; 3086c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 3087c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 3088c5c9cd4dSSage Weil size); 3089c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 3090c5c9cd4dSSage Weil } 3091c5c9cd4dSSage Weil 3092c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 3093b3b4aa74SDavid Sterba btrfs_release_path(path); 3094c5c9cd4dSSage Weil 30950c4d2d95SJosef Bacik inode_inc_iversion(inode); 3096a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 3097b5384d48SSage Weil 3098b5384d48SSage Weil /* 3099b5384d48SSage Weil * we round up to the block size at eof when 3100b5384d48SSage Weil * determining which extents to clone above, 3101b5384d48SSage Weil * but shouldn't round up the file size 3102b5384d48SSage Weil */ 3103b5384d48SSage Weil endoff = new_key.offset + datal; 31045f3888ffSLi Zefan if (endoff > destoff+olen) 31055f3888ffSLi Zefan endoff = destoff+olen; 3106b5384d48SSage Weil if (endoff > inode->i_size) 3107b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 3108b5384d48SSage Weil 3109a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 311079787eaaSJeff Mahoney if (ret) { 311179787eaaSJeff Mahoney btrfs_abort_transaction(trans, root, ret); 3112a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 311379787eaaSJeff Mahoney goto out; 311479787eaaSJeff Mahoney } 311579787eaaSJeff Mahoney ret = btrfs_end_transaction(trans, root); 3116a22285a6SYan, Zheng } 3117c5c9cd4dSSage Weil next: 3118b3b4aa74SDavid Sterba btrfs_release_path(path); 3119ae01a0abSYan Zheng key.offset++; 3120ae01a0abSYan Zheng } 3121f46b5a66SChristoph Hellwig ret = 0; 312232b7c687SMark Fasheh 3123f46b5a66SChristoph Hellwig out: 3124b3b4aa74SDavid Sterba btrfs_release_path(path); 312532b7c687SMark Fasheh btrfs_free_path(path); 312632b7c687SMark Fasheh vfree(buf); 312732b7c687SMark Fasheh return ret; 312832b7c687SMark Fasheh } 312932b7c687SMark Fasheh 313032b7c687SMark Fasheh static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 313132b7c687SMark Fasheh u64 off, u64 olen, u64 destoff) 313232b7c687SMark Fasheh { 313354563d41SAl Viro struct inode *inode = file_inode(file); 313432b7c687SMark Fasheh struct btrfs_root *root = BTRFS_I(inode)->root; 313532b7c687SMark Fasheh struct fd src_file; 313632b7c687SMark Fasheh struct inode *src; 313732b7c687SMark Fasheh int ret; 313832b7c687SMark Fasheh u64 len = olen; 313932b7c687SMark Fasheh u64 bs = root->fs_info->sb->s_blocksize; 314032b7c687SMark Fasheh int same_inode = 0; 314132b7c687SMark Fasheh 314232b7c687SMark Fasheh /* 314332b7c687SMark Fasheh * TODO: 314432b7c687SMark Fasheh * - split compressed inline extents. annoying: we need to 314532b7c687SMark Fasheh * decompress into destination's address_space (the file offset 314632b7c687SMark Fasheh * may change, so source mapping won't do), then recompress (or 314732b7c687SMark Fasheh * otherwise reinsert) a subrange. 314832b7c687SMark Fasheh * - allow ranges within the same file to be cloned (provided 314932b7c687SMark Fasheh * they don't overlap)? 315032b7c687SMark Fasheh */ 315132b7c687SMark Fasheh 315232b7c687SMark Fasheh /* the destination must be opened for writing */ 315332b7c687SMark Fasheh if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 315432b7c687SMark Fasheh return -EINVAL; 315532b7c687SMark Fasheh 315632b7c687SMark Fasheh if (btrfs_root_readonly(root)) 315732b7c687SMark Fasheh return -EROFS; 315832b7c687SMark Fasheh 315932b7c687SMark Fasheh ret = mnt_want_write_file(file); 316032b7c687SMark Fasheh if (ret) 316132b7c687SMark Fasheh return ret; 316232b7c687SMark Fasheh 316332b7c687SMark Fasheh src_file = fdget(srcfd); 316432b7c687SMark Fasheh if (!src_file.file) { 316532b7c687SMark Fasheh ret = -EBADF; 316632b7c687SMark Fasheh goto out_drop_write; 316732b7c687SMark Fasheh } 316832b7c687SMark Fasheh 316932b7c687SMark Fasheh ret = -EXDEV; 317032b7c687SMark Fasheh if (src_file.file->f_path.mnt != file->f_path.mnt) 317132b7c687SMark Fasheh goto out_fput; 317232b7c687SMark Fasheh 317332b7c687SMark Fasheh src = file_inode(src_file.file); 317432b7c687SMark Fasheh 317532b7c687SMark Fasheh ret = -EINVAL; 317632b7c687SMark Fasheh if (src == inode) 317732b7c687SMark Fasheh same_inode = 1; 317832b7c687SMark Fasheh 317932b7c687SMark Fasheh /* the src must be open for reading */ 318032b7c687SMark Fasheh if (!(src_file.file->f_mode & FMODE_READ)) 318132b7c687SMark Fasheh goto out_fput; 318232b7c687SMark Fasheh 318332b7c687SMark Fasheh /* don't make the dst file partly checksummed */ 318432b7c687SMark Fasheh if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != 318532b7c687SMark Fasheh (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 318632b7c687SMark Fasheh goto out_fput; 318732b7c687SMark Fasheh 318832b7c687SMark Fasheh ret = -EISDIR; 318932b7c687SMark Fasheh if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 319032b7c687SMark Fasheh goto out_fput; 319132b7c687SMark Fasheh 319232b7c687SMark Fasheh ret = -EXDEV; 319332b7c687SMark Fasheh if (src->i_sb != inode->i_sb) 319432b7c687SMark Fasheh goto out_fput; 319532b7c687SMark Fasheh 319632b7c687SMark Fasheh if (!same_inode) { 319732b7c687SMark Fasheh if (inode < src) { 319832b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 319932b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 320032b7c687SMark Fasheh } else { 320132b7c687SMark Fasheh mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 320232b7c687SMark Fasheh mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 320332b7c687SMark Fasheh } 320432b7c687SMark Fasheh } else { 320532b7c687SMark Fasheh mutex_lock(&src->i_mutex); 320632b7c687SMark Fasheh } 320732b7c687SMark Fasheh 320832b7c687SMark Fasheh /* determine range to clone */ 320932b7c687SMark Fasheh ret = -EINVAL; 321032b7c687SMark Fasheh if (off + len > src->i_size || off + len < off) 321132b7c687SMark Fasheh goto out_unlock; 321232b7c687SMark Fasheh if (len == 0) 321332b7c687SMark Fasheh olen = len = src->i_size - off; 321432b7c687SMark Fasheh /* if we extend to eof, continue to block boundary */ 321532b7c687SMark Fasheh if (off + len == src->i_size) 321632b7c687SMark Fasheh len = ALIGN(src->i_size, bs) - off; 321732b7c687SMark Fasheh 321832b7c687SMark Fasheh /* verify the end result is block aligned */ 321932b7c687SMark Fasheh if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 322032b7c687SMark Fasheh !IS_ALIGNED(destoff, bs)) 322132b7c687SMark Fasheh goto out_unlock; 322232b7c687SMark Fasheh 322332b7c687SMark Fasheh /* verify if ranges are overlapped within the same file */ 322432b7c687SMark Fasheh if (same_inode) { 322532b7c687SMark Fasheh if (destoff + len > off && destoff < off + len) 322632b7c687SMark Fasheh goto out_unlock; 322732b7c687SMark Fasheh } 322832b7c687SMark Fasheh 322932b7c687SMark Fasheh if (destoff > inode->i_size) { 323032b7c687SMark Fasheh ret = btrfs_cont_expand(inode, inode->i_size, destoff); 323132b7c687SMark Fasheh if (ret) 323232b7c687SMark Fasheh goto out_unlock; 323332b7c687SMark Fasheh } 323432b7c687SMark Fasheh 323532b7c687SMark Fasheh /* truncate page cache pages from target inode range */ 323632b7c687SMark Fasheh truncate_inode_pages_range(&inode->i_data, destoff, 323732b7c687SMark Fasheh PAGE_CACHE_ALIGN(destoff + len) - 1); 323832b7c687SMark Fasheh 323932b7c687SMark Fasheh lock_extent_range(src, off, len); 324032b7c687SMark Fasheh 324132b7c687SMark Fasheh ret = btrfs_clone(src, inode, off, olen, len, destoff); 324232b7c687SMark Fasheh 3243aa42ffd9SLiu Bo unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); 3244f46b5a66SChristoph Hellwig out_unlock: 3245f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 3246a96fbc72SLiu Bo if (!same_inode) 3247f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 3248f46b5a66SChristoph Hellwig out_fput: 32492903ff01SAl Viro fdput(src_file); 3250ab67b7c1SYan Zheng out_drop_write: 32512a79f17eSAl Viro mnt_drop_write_file(file); 3252f46b5a66SChristoph Hellwig return ret; 3253f46b5a66SChristoph Hellwig } 3254f46b5a66SChristoph Hellwig 32557a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 3256c5c9cd4dSSage Weil { 3257c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 3258c5c9cd4dSSage Weil 32597a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 3260c5c9cd4dSSage Weil return -EFAULT; 3261c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 3262c5c9cd4dSSage Weil args.src_length, args.dest_offset); 3263c5c9cd4dSSage Weil } 3264c5c9cd4dSSage Weil 3265f46b5a66SChristoph Hellwig /* 3266f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3267f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3268f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3269f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3270f46b5a66SChristoph Hellwig */ 3271b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 3272f46b5a66SChristoph Hellwig { 3273496ad9aaSAl Viro struct inode *inode = file_inode(file); 3274f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3275f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 32761ab86aedSSage Weil int ret; 3277f46b5a66SChristoph Hellwig 32781ab86aedSSage Weil ret = -EPERM; 3279df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 3280f46b5a66SChristoph Hellwig goto out; 32811ab86aedSSage Weil 32821ab86aedSSage Weil ret = -EINPROGRESS; 32831ab86aedSSage Weil if (file->private_data) 32841ab86aedSSage Weil goto out; 32859ca9ee09SSage Weil 3286b83cc969SLi Zefan ret = -EROFS; 3287b83cc969SLi Zefan if (btrfs_root_readonly(root)) 3288b83cc969SLi Zefan goto out; 3289b83cc969SLi Zefan 3290a561be71SAl Viro ret = mnt_want_write_file(file); 3291c146afadSYan Zheng if (ret) 3292c146afadSYan Zheng goto out; 3293c146afadSYan Zheng 3294a4abeea4SJosef Bacik atomic_inc(&root->fs_info->open_ioctl_trans); 32959ca9ee09SSage Weil 3296f46b5a66SChristoph Hellwig ret = -ENOMEM; 32977a7eaa40SJosef Bacik trans = btrfs_start_ioctl_transaction(root); 3298abd30bb0STsutomu Itoh if (IS_ERR(trans)) 32991ab86aedSSage Weil goto out_drop; 33001ab86aedSSage Weil 33011ab86aedSSage Weil file->private_data = trans; 33021ab86aedSSage Weil return 0; 33031ab86aedSSage Weil 33041ab86aedSSage Weil out_drop: 3305a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 33062a79f17eSAl Viro mnt_drop_write_file(file); 3307f46b5a66SChristoph Hellwig out: 3308f46b5a66SChristoph Hellwig return ret; 3309f46b5a66SChristoph Hellwig } 3310f46b5a66SChristoph Hellwig 33116ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 33126ef5ed0dSJosef Bacik { 3313496ad9aaSAl Viro struct inode *inode = file_inode(file); 33146ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 33156ef5ed0dSJosef Bacik struct btrfs_root *new_root; 33166ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 33176ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 33186ef5ed0dSJosef Bacik struct btrfs_path *path; 33196ef5ed0dSJosef Bacik struct btrfs_key location; 33206ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 33216ef5ed0dSJosef Bacik u64 objectid = 0; 33226ef5ed0dSJosef Bacik u64 dir_id; 33233c04ce01SMiao Xie int ret; 33246ef5ed0dSJosef Bacik 33256ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 33266ef5ed0dSJosef Bacik return -EPERM; 33276ef5ed0dSJosef Bacik 33283c04ce01SMiao Xie ret = mnt_want_write_file(file); 33293c04ce01SMiao Xie if (ret) 33303c04ce01SMiao Xie return ret; 33313c04ce01SMiao Xie 33323c04ce01SMiao Xie if (copy_from_user(&objectid, argp, sizeof(objectid))) { 33333c04ce01SMiao Xie ret = -EFAULT; 33343c04ce01SMiao Xie goto out; 33353c04ce01SMiao Xie } 33366ef5ed0dSJosef Bacik 33376ef5ed0dSJosef Bacik if (!objectid) 33381cecf579Schandan objectid = BTRFS_FS_TREE_OBJECTID; 33396ef5ed0dSJosef Bacik 33406ef5ed0dSJosef Bacik location.objectid = objectid; 33416ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 33426ef5ed0dSJosef Bacik location.offset = (u64)-1; 33436ef5ed0dSJosef Bacik 33446ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 33453c04ce01SMiao Xie if (IS_ERR(new_root)) { 33463c04ce01SMiao Xie ret = PTR_ERR(new_root); 33473c04ce01SMiao Xie goto out; 33483c04ce01SMiao Xie } 33496ef5ed0dSJosef Bacik 33506ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 33513c04ce01SMiao Xie if (!path) { 33523c04ce01SMiao Xie ret = -ENOMEM; 33533c04ce01SMiao Xie goto out; 33543c04ce01SMiao Xie } 33556ef5ed0dSJosef Bacik path->leave_spinning = 1; 33566ef5ed0dSJosef Bacik 33576ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 335898d5dc13STsutomu Itoh if (IS_ERR(trans)) { 33596ef5ed0dSJosef Bacik btrfs_free_path(path); 33603c04ce01SMiao Xie ret = PTR_ERR(trans); 33613c04ce01SMiao Xie goto out; 33626ef5ed0dSJosef Bacik } 33636ef5ed0dSJosef Bacik 33646c41761fSDavid Sterba dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 33656ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 33666ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 3367cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 33686ef5ed0dSJosef Bacik btrfs_free_path(path); 33696ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 33706ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 33716ef5ed0dSJosef Bacik "this isn't going to work\n"); 33723c04ce01SMiao Xie ret = -ENOENT; 33733c04ce01SMiao Xie goto out; 33746ef5ed0dSJosef Bacik } 33756ef5ed0dSJosef Bacik 33766ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 33776ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 33786ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 33796ef5ed0dSJosef Bacik btrfs_free_path(path); 33806ef5ed0dSJosef Bacik 33812b0ce2c2SMitch Harder btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 33826ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 33833c04ce01SMiao Xie out: 33843c04ce01SMiao Xie mnt_drop_write_file(file); 33853c04ce01SMiao Xie return ret; 33866ef5ed0dSJosef Bacik } 33876ef5ed0dSJosef Bacik 33885af3e8ccSStefan Behrens void btrfs_get_block_group_info(struct list_head *groups_list, 3389bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 3390bf5fc093SJosef Bacik { 3391bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 3392bf5fc093SJosef Bacik 3393bf5fc093SJosef Bacik space->total_bytes = 0; 3394bf5fc093SJosef Bacik space->used_bytes = 0; 3395bf5fc093SJosef Bacik space->flags = 0; 3396bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 3397bf5fc093SJosef Bacik space->flags = block_group->flags; 3398bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 3399bf5fc093SJosef Bacik space->used_bytes += 3400bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 3401bf5fc093SJosef Bacik } 3402bf5fc093SJosef Bacik } 3403bf5fc093SJosef Bacik 340448a3b636SEric Sandeen static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 34051406e432SJosef Bacik { 34061406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 34071406e432SJosef Bacik struct btrfs_ioctl_space_info space; 34081406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 34097fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 341013f2696fSDaniel J Blueman struct btrfs_ioctl_space_info __user *user_dest; 34111406e432SJosef Bacik struct btrfs_space_info *info; 3412bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 3413bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 3414bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 3415bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 3416bf5fc093SJosef Bacik int num_types = 4; 34177fde62bfSChris Mason int alloc_size; 34181406e432SJosef Bacik int ret = 0; 341951788b1bSDan Rosenberg u64 slot_count = 0; 3420bf5fc093SJosef Bacik int i, c; 34211406e432SJosef Bacik 34221406e432SJosef Bacik if (copy_from_user(&space_args, 34231406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 34241406e432SJosef Bacik sizeof(space_args))) 34251406e432SJosef Bacik return -EFAULT; 34261406e432SJosef Bacik 3427bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3428bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3429bf5fc093SJosef Bacik 3430bf5fc093SJosef Bacik info = NULL; 34317fde62bfSChris Mason rcu_read_lock(); 3432bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3433bf5fc093SJosef Bacik list) { 3434bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3435bf5fc093SJosef Bacik info = tmp; 3436bf5fc093SJosef Bacik break; 3437bf5fc093SJosef Bacik } 3438bf5fc093SJosef Bacik } 34397fde62bfSChris Mason rcu_read_unlock(); 34401406e432SJosef Bacik 3441bf5fc093SJosef Bacik if (!info) 3442bf5fc093SJosef Bacik continue; 3443bf5fc093SJosef Bacik 3444bf5fc093SJosef Bacik down_read(&info->groups_sem); 3445bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3446bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 3447bf5fc093SJosef Bacik slot_count++; 3448bf5fc093SJosef Bacik } 3449bf5fc093SJosef Bacik up_read(&info->groups_sem); 3450bf5fc093SJosef Bacik } 3451bf5fc093SJosef Bacik 34527fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 34537fde62bfSChris Mason if (space_args.space_slots == 0) { 34547fde62bfSChris Mason space_args.total_spaces = slot_count; 34557fde62bfSChris Mason goto out; 34567fde62bfSChris Mason } 3457bf5fc093SJosef Bacik 345851788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 3459bf5fc093SJosef Bacik 34607fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 3461bf5fc093SJosef Bacik 34627fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 34637fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 34647fde62bfSChris Mason */ 34657fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 34667fde62bfSChris Mason return -ENOMEM; 34677fde62bfSChris Mason 34687fde62bfSChris Mason space_args.total_spaces = 0; 34697fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 34707fde62bfSChris Mason if (!dest) 34717fde62bfSChris Mason return -ENOMEM; 34727fde62bfSChris Mason dest_orig = dest; 34737fde62bfSChris Mason 34747fde62bfSChris Mason /* now we have a buffer to copy into */ 3475bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 3476bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 3477bf5fc093SJosef Bacik 347851788b1bSDan Rosenberg if (!slot_count) 347951788b1bSDan Rosenberg break; 348051788b1bSDan Rosenberg 3481bf5fc093SJosef Bacik info = NULL; 34821406e432SJosef Bacik rcu_read_lock(); 3483bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 3484bf5fc093SJosef Bacik list) { 3485bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 3486bf5fc093SJosef Bacik info = tmp; 34877fde62bfSChris Mason break; 3488bf5fc093SJosef Bacik } 3489bf5fc093SJosef Bacik } 3490bf5fc093SJosef Bacik rcu_read_unlock(); 34917fde62bfSChris Mason 3492bf5fc093SJosef Bacik if (!info) 3493bf5fc093SJosef Bacik continue; 3494bf5fc093SJosef Bacik down_read(&info->groups_sem); 3495bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 3496bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 34975af3e8ccSStefan Behrens btrfs_get_block_group_info( 34985af3e8ccSStefan Behrens &info->block_groups[c], &space); 34997fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 35001406e432SJosef Bacik dest++; 35011406e432SJosef Bacik space_args.total_spaces++; 350251788b1bSDan Rosenberg slot_count--; 35031406e432SJosef Bacik } 350451788b1bSDan Rosenberg if (!slot_count) 350551788b1bSDan Rosenberg break; 3506bf5fc093SJosef Bacik } 3507bf5fc093SJosef Bacik up_read(&info->groups_sem); 3508bf5fc093SJosef Bacik } 35091406e432SJosef Bacik 35102eec6c81SDaniel J Blueman user_dest = (struct btrfs_ioctl_space_info __user *) 35117fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 35127fde62bfSChris Mason 35137fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 35147fde62bfSChris Mason ret = -EFAULT; 35157fde62bfSChris Mason 35167fde62bfSChris Mason kfree(dest_orig); 35177fde62bfSChris Mason out: 35187fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 35191406e432SJosef Bacik ret = -EFAULT; 35201406e432SJosef Bacik 35211406e432SJosef Bacik return ret; 35221406e432SJosef Bacik } 35231406e432SJosef Bacik 352401e219e8SJeff Mahoney static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg) 352501e219e8SJeff Mahoney { 352601e219e8SJeff Mahoney struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 352701e219e8SJeff Mahoney u64 reserved; 352801e219e8SJeff Mahoney 352901e219e8SJeff Mahoney spin_lock(&block_rsv->lock); 353001e219e8SJeff Mahoney reserved = block_rsv->reserved; 353101e219e8SJeff Mahoney spin_unlock(&block_rsv->lock); 353201e219e8SJeff Mahoney 353301e219e8SJeff Mahoney if (arg && copy_to_user(arg, &reserved, sizeof(reserved))) 353401e219e8SJeff Mahoney return -EFAULT; 353501e219e8SJeff Mahoney return 0; 353601e219e8SJeff Mahoney } 353701e219e8SJeff Mahoney 3538f46b5a66SChristoph Hellwig /* 3539f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 3540f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 3541f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 3542f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 3543f46b5a66SChristoph Hellwig */ 3544f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 3545f46b5a66SChristoph Hellwig { 3546496ad9aaSAl Viro struct inode *inode = file_inode(file); 3547f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 3548f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3549f46b5a66SChristoph Hellwig 3550f46b5a66SChristoph Hellwig trans = file->private_data; 35511ab86aedSSage Weil if (!trans) 35521ab86aedSSage Weil return -EINVAL; 3553b214107eSChristoph Hellwig file->private_data = NULL; 35549ca9ee09SSage Weil 35551ab86aedSSage Weil btrfs_end_transaction(trans, root); 35561ab86aedSSage Weil 3557a4abeea4SJosef Bacik atomic_dec(&root->fs_info->open_ioctl_trans); 35589ca9ee09SSage Weil 35592a79f17eSAl Viro mnt_drop_write_file(file); 35601ab86aedSSage Weil return 0; 3561f46b5a66SChristoph Hellwig } 3562f46b5a66SChristoph Hellwig 35639a8c28beSMiao Xie static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, 35649a8c28beSMiao Xie void __user *argp) 356546204592SSage Weil { 356646204592SSage Weil struct btrfs_trans_handle *trans; 356746204592SSage Weil u64 transid; 3568db5b493aSTsutomu Itoh int ret; 356946204592SSage Weil 3570d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 3571ff7c1d33SMiao Xie if (IS_ERR(trans)) { 3572ff7c1d33SMiao Xie if (PTR_ERR(trans) != -ENOENT) 357398d5dc13STsutomu Itoh return PTR_ERR(trans); 3574ff7c1d33SMiao Xie 3575ff7c1d33SMiao Xie /* No running transaction, don't bother */ 3576ff7c1d33SMiao Xie transid = root->fs_info->last_trans_committed; 3577ff7c1d33SMiao Xie goto out; 3578ff7c1d33SMiao Xie } 357946204592SSage Weil transid = trans->transid; 3580db5b493aSTsutomu Itoh ret = btrfs_commit_transaction_async(trans, root, 0); 35818b2b2d3cSTsutomu Itoh if (ret) { 35828b2b2d3cSTsutomu Itoh btrfs_end_transaction(trans, root); 3583db5b493aSTsutomu Itoh return ret; 35848b2b2d3cSTsutomu Itoh } 3585ff7c1d33SMiao Xie out: 358646204592SSage Weil if (argp) 358746204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 358846204592SSage Weil return -EFAULT; 358946204592SSage Weil return 0; 359046204592SSage Weil } 359146204592SSage Weil 35929a8c28beSMiao Xie static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 35939a8c28beSMiao Xie void __user *argp) 359446204592SSage Weil { 359546204592SSage Weil u64 transid; 359646204592SSage Weil 359746204592SSage Weil if (argp) { 359846204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 359946204592SSage Weil return -EFAULT; 360046204592SSage Weil } else { 360146204592SSage Weil transid = 0; /* current trans */ 360246204592SSage Weil } 360346204592SSage Weil return btrfs_wait_for_commit(root, transid); 360446204592SSage Weil } 360546204592SSage Weil 3606b8e95489SMiao Xie static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 3607475f6387SJan Schmidt { 3608496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 3609475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3610b8e95489SMiao Xie int ret; 3611475f6387SJan Schmidt 3612475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3613475f6387SJan Schmidt return -EPERM; 3614475f6387SJan Schmidt 3615475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3616475f6387SJan Schmidt if (IS_ERR(sa)) 3617475f6387SJan Schmidt return PTR_ERR(sa); 3618475f6387SJan Schmidt 3619b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) { 3620b8e95489SMiao Xie ret = mnt_want_write_file(file); 3621b8e95489SMiao Xie if (ret) 3622b8e95489SMiao Xie goto out; 3623b8e95489SMiao Xie } 3624b8e95489SMiao Xie 3625aa1b8cd4SStefan Behrens ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 362663a212abSStefan Behrens &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 362763a212abSStefan Behrens 0); 3628475f6387SJan Schmidt 3629475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3630475f6387SJan Schmidt ret = -EFAULT; 3631475f6387SJan Schmidt 3632b8e95489SMiao Xie if (!(sa->flags & BTRFS_SCRUB_READONLY)) 3633b8e95489SMiao Xie mnt_drop_write_file(file); 3634b8e95489SMiao Xie out: 3635475f6387SJan Schmidt kfree(sa); 3636475f6387SJan Schmidt return ret; 3637475f6387SJan Schmidt } 3638475f6387SJan Schmidt 3639475f6387SJan Schmidt static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 3640475f6387SJan Schmidt { 3641475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3642475f6387SJan Schmidt return -EPERM; 3643475f6387SJan Schmidt 3644aa1b8cd4SStefan Behrens return btrfs_scrub_cancel(root->fs_info); 3645475f6387SJan Schmidt } 3646475f6387SJan Schmidt 3647475f6387SJan Schmidt static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 3648475f6387SJan Schmidt void __user *arg) 3649475f6387SJan Schmidt { 3650475f6387SJan Schmidt struct btrfs_ioctl_scrub_args *sa; 3651475f6387SJan Schmidt int ret; 3652475f6387SJan Schmidt 3653475f6387SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3654475f6387SJan Schmidt return -EPERM; 3655475f6387SJan Schmidt 3656475f6387SJan Schmidt sa = memdup_user(arg, sizeof(*sa)); 3657475f6387SJan Schmidt if (IS_ERR(sa)) 3658475f6387SJan Schmidt return PTR_ERR(sa); 3659475f6387SJan Schmidt 3660475f6387SJan Schmidt ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 3661475f6387SJan Schmidt 3662475f6387SJan Schmidt if (copy_to_user(arg, sa, sizeof(*sa))) 3663475f6387SJan Schmidt ret = -EFAULT; 3664475f6387SJan Schmidt 3665475f6387SJan Schmidt kfree(sa); 3666475f6387SJan Schmidt return ret; 3667475f6387SJan Schmidt } 3668475f6387SJan Schmidt 3669c11d2c23SStefan Behrens static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 3670b27f7c0cSDavid Sterba void __user *arg) 3671c11d2c23SStefan Behrens { 3672c11d2c23SStefan Behrens struct btrfs_ioctl_get_dev_stats *sa; 3673c11d2c23SStefan Behrens int ret; 3674c11d2c23SStefan Behrens 3675c11d2c23SStefan Behrens sa = memdup_user(arg, sizeof(*sa)); 3676c11d2c23SStefan Behrens if (IS_ERR(sa)) 3677c11d2c23SStefan Behrens return PTR_ERR(sa); 3678c11d2c23SStefan Behrens 3679b27f7c0cSDavid Sterba if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { 3680b27f7c0cSDavid Sterba kfree(sa); 3681b27f7c0cSDavid Sterba return -EPERM; 3682b27f7c0cSDavid Sterba } 3683b27f7c0cSDavid Sterba 3684b27f7c0cSDavid Sterba ret = btrfs_get_dev_stats(root, sa); 3685c11d2c23SStefan Behrens 3686c11d2c23SStefan Behrens if (copy_to_user(arg, sa, sizeof(*sa))) 3687c11d2c23SStefan Behrens ret = -EFAULT; 3688c11d2c23SStefan Behrens 3689c11d2c23SStefan Behrens kfree(sa); 3690c11d2c23SStefan Behrens return ret; 3691c11d2c23SStefan Behrens } 3692c11d2c23SStefan Behrens 36933f6bcfbdSStefan Behrens static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 36943f6bcfbdSStefan Behrens { 36953f6bcfbdSStefan Behrens struct btrfs_ioctl_dev_replace_args *p; 36963f6bcfbdSStefan Behrens int ret; 36973f6bcfbdSStefan Behrens 36983f6bcfbdSStefan Behrens if (!capable(CAP_SYS_ADMIN)) 36993f6bcfbdSStefan Behrens return -EPERM; 37003f6bcfbdSStefan Behrens 37013f6bcfbdSStefan Behrens p = memdup_user(arg, sizeof(*p)); 37023f6bcfbdSStefan Behrens if (IS_ERR(p)) 37033f6bcfbdSStefan Behrens return PTR_ERR(p); 37043f6bcfbdSStefan Behrens 37053f6bcfbdSStefan Behrens switch (p->cmd) { 37063f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 3707adfa97cbSIlya Dryomov if (root->fs_info->sb->s_flags & MS_RDONLY) { 3708adfa97cbSIlya Dryomov ret = -EROFS; 3709adfa97cbSIlya Dryomov goto out; 3710adfa97cbSIlya Dryomov } 37113f6bcfbdSStefan Behrens if (atomic_xchg( 37123f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 37133f6bcfbdSStefan Behrens 1)) { 3714e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 37153f6bcfbdSStefan Behrens } else { 37163f6bcfbdSStefan Behrens ret = btrfs_dev_replace_start(root, p); 37173f6bcfbdSStefan Behrens atomic_set( 37183f6bcfbdSStefan Behrens &root->fs_info->mutually_exclusive_operation_running, 37193f6bcfbdSStefan Behrens 0); 37203f6bcfbdSStefan Behrens } 37213f6bcfbdSStefan Behrens break; 37223f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 37233f6bcfbdSStefan Behrens btrfs_dev_replace_status(root->fs_info, p); 37243f6bcfbdSStefan Behrens ret = 0; 37253f6bcfbdSStefan Behrens break; 37263f6bcfbdSStefan Behrens case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 37273f6bcfbdSStefan Behrens ret = btrfs_dev_replace_cancel(root->fs_info, p); 37283f6bcfbdSStefan Behrens break; 37293f6bcfbdSStefan Behrens default: 37303f6bcfbdSStefan Behrens ret = -EINVAL; 37313f6bcfbdSStefan Behrens break; 37323f6bcfbdSStefan Behrens } 37333f6bcfbdSStefan Behrens 37343f6bcfbdSStefan Behrens if (copy_to_user(arg, p, sizeof(*p))) 37353f6bcfbdSStefan Behrens ret = -EFAULT; 3736adfa97cbSIlya Dryomov out: 37373f6bcfbdSStefan Behrens kfree(p); 37383f6bcfbdSStefan Behrens return ret; 37393f6bcfbdSStefan Behrens } 37403f6bcfbdSStefan Behrens 3741d7728c96SJan Schmidt static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) 3742d7728c96SJan Schmidt { 3743d7728c96SJan Schmidt int ret = 0; 3744d7728c96SJan Schmidt int i; 3745740c3d22SChris Mason u64 rel_ptr; 3746d7728c96SJan Schmidt int size; 3747806468f8SChris Mason struct btrfs_ioctl_ino_path_args *ipa = NULL; 3748d7728c96SJan Schmidt struct inode_fs_paths *ipath = NULL; 3749d7728c96SJan Schmidt struct btrfs_path *path; 3750d7728c96SJan Schmidt 375182b22ac8SKusanagi Kouichi if (!capable(CAP_DAC_READ_SEARCH)) 3752d7728c96SJan Schmidt return -EPERM; 3753d7728c96SJan Schmidt 3754d7728c96SJan Schmidt path = btrfs_alloc_path(); 3755d7728c96SJan Schmidt if (!path) { 3756d7728c96SJan Schmidt ret = -ENOMEM; 3757d7728c96SJan Schmidt goto out; 3758d7728c96SJan Schmidt } 3759d7728c96SJan Schmidt 3760d7728c96SJan Schmidt ipa = memdup_user(arg, sizeof(*ipa)); 3761d7728c96SJan Schmidt if (IS_ERR(ipa)) { 3762d7728c96SJan Schmidt ret = PTR_ERR(ipa); 3763d7728c96SJan Schmidt ipa = NULL; 3764d7728c96SJan Schmidt goto out; 3765d7728c96SJan Schmidt } 3766d7728c96SJan Schmidt 3767d7728c96SJan Schmidt size = min_t(u32, ipa->size, 4096); 3768d7728c96SJan Schmidt ipath = init_ipath(size, root, path); 3769d7728c96SJan Schmidt if (IS_ERR(ipath)) { 3770d7728c96SJan Schmidt ret = PTR_ERR(ipath); 3771d7728c96SJan Schmidt ipath = NULL; 3772d7728c96SJan Schmidt goto out; 3773d7728c96SJan Schmidt } 3774d7728c96SJan Schmidt 3775d7728c96SJan Schmidt ret = paths_from_inode(ipa->inum, ipath); 3776d7728c96SJan Schmidt if (ret < 0) 3777d7728c96SJan Schmidt goto out; 3778d7728c96SJan Schmidt 3779d7728c96SJan Schmidt for (i = 0; i < ipath->fspath->elem_cnt; ++i) { 3780745c4d8eSJeff Mahoney rel_ptr = ipath->fspath->val[i] - 3781745c4d8eSJeff Mahoney (u64)(unsigned long)ipath->fspath->val; 3782740c3d22SChris Mason ipath->fspath->val[i] = rel_ptr; 3783d7728c96SJan Schmidt } 3784d7728c96SJan Schmidt 3785745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)ipa->fspath, 3786745c4d8eSJeff Mahoney (void *)(unsigned long)ipath->fspath, size); 3787d7728c96SJan Schmidt if (ret) { 3788d7728c96SJan Schmidt ret = -EFAULT; 3789d7728c96SJan Schmidt goto out; 3790d7728c96SJan Schmidt } 3791d7728c96SJan Schmidt 3792d7728c96SJan Schmidt out: 3793d7728c96SJan Schmidt btrfs_free_path(path); 3794d7728c96SJan Schmidt free_ipath(ipath); 3795d7728c96SJan Schmidt kfree(ipa); 3796d7728c96SJan Schmidt 3797d7728c96SJan Schmidt return ret; 3798d7728c96SJan Schmidt } 3799d7728c96SJan Schmidt 3800d7728c96SJan Schmidt static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) 3801d7728c96SJan Schmidt { 3802d7728c96SJan Schmidt struct btrfs_data_container *inodes = ctx; 3803d7728c96SJan Schmidt const size_t c = 3 * sizeof(u64); 3804d7728c96SJan Schmidt 3805d7728c96SJan Schmidt if (inodes->bytes_left >= c) { 3806d7728c96SJan Schmidt inodes->bytes_left -= c; 3807d7728c96SJan Schmidt inodes->val[inodes->elem_cnt] = inum; 3808d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 1] = offset; 3809d7728c96SJan Schmidt inodes->val[inodes->elem_cnt + 2] = root; 3810d7728c96SJan Schmidt inodes->elem_cnt += 3; 3811d7728c96SJan Schmidt } else { 3812d7728c96SJan Schmidt inodes->bytes_missing += c - inodes->bytes_left; 3813d7728c96SJan Schmidt inodes->bytes_left = 0; 3814d7728c96SJan Schmidt inodes->elem_missed += 3; 3815d7728c96SJan Schmidt } 3816d7728c96SJan Schmidt 3817d7728c96SJan Schmidt return 0; 3818d7728c96SJan Schmidt } 3819d7728c96SJan Schmidt 3820d7728c96SJan Schmidt static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 3821d7728c96SJan Schmidt void __user *arg) 3822d7728c96SJan Schmidt { 3823d7728c96SJan Schmidt int ret = 0; 3824d7728c96SJan Schmidt int size; 3825d7728c96SJan Schmidt struct btrfs_ioctl_logical_ino_args *loi; 3826d7728c96SJan Schmidt struct btrfs_data_container *inodes = NULL; 3827d7728c96SJan Schmidt struct btrfs_path *path = NULL; 3828d7728c96SJan Schmidt 3829d7728c96SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 3830d7728c96SJan Schmidt return -EPERM; 3831d7728c96SJan Schmidt 3832d7728c96SJan Schmidt loi = memdup_user(arg, sizeof(*loi)); 3833d7728c96SJan Schmidt if (IS_ERR(loi)) { 3834d7728c96SJan Schmidt ret = PTR_ERR(loi); 3835d7728c96SJan Schmidt loi = NULL; 3836d7728c96SJan Schmidt goto out; 3837d7728c96SJan Schmidt } 3838d7728c96SJan Schmidt 3839d7728c96SJan Schmidt path = btrfs_alloc_path(); 3840d7728c96SJan Schmidt if (!path) { 3841d7728c96SJan Schmidt ret = -ENOMEM; 3842d7728c96SJan Schmidt goto out; 3843d7728c96SJan Schmidt } 3844d7728c96SJan Schmidt 3845425d17a2SLiu Bo size = min_t(u32, loi->size, 64 * 1024); 3846d7728c96SJan Schmidt inodes = init_data_container(size); 3847d7728c96SJan Schmidt if (IS_ERR(inodes)) { 3848d7728c96SJan Schmidt ret = PTR_ERR(inodes); 3849d7728c96SJan Schmidt inodes = NULL; 3850d7728c96SJan Schmidt goto out; 3851d7728c96SJan Schmidt } 3852d7728c96SJan Schmidt 3853df031f07SLiu Bo ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 3854df031f07SLiu Bo build_ino_list, inodes); 3855df031f07SLiu Bo if (ret == -EINVAL) 3856d7728c96SJan Schmidt ret = -ENOENT; 3857d7728c96SJan Schmidt if (ret < 0) 3858d7728c96SJan Schmidt goto out; 3859d7728c96SJan Schmidt 3860745c4d8eSJeff Mahoney ret = copy_to_user((void *)(unsigned long)loi->inodes, 3861745c4d8eSJeff Mahoney (void *)(unsigned long)inodes, size); 3862d7728c96SJan Schmidt if (ret) 3863d7728c96SJan Schmidt ret = -EFAULT; 3864d7728c96SJan Schmidt 3865d7728c96SJan Schmidt out: 3866d7728c96SJan Schmidt btrfs_free_path(path); 3867425d17a2SLiu Bo vfree(inodes); 3868d7728c96SJan Schmidt kfree(loi); 3869d7728c96SJan Schmidt 3870d7728c96SJan Schmidt return ret; 3871d7728c96SJan Schmidt } 3872d7728c96SJan Schmidt 387319a39dceSIlya Dryomov void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, 3874c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs) 3875c9e9f97bSIlya Dryomov { 3876c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3877c9e9f97bSIlya Dryomov 3878c9e9f97bSIlya Dryomov bargs->flags = bctl->flags; 3879c9e9f97bSIlya Dryomov 3880837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_running)) 3881837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_RUNNING; 3882837d5b6eSIlya Dryomov if (atomic_read(&fs_info->balance_pause_req)) 3883837d5b6eSIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; 3884a7e99c69SIlya Dryomov if (atomic_read(&fs_info->balance_cancel_req)) 3885a7e99c69SIlya Dryomov bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; 3886837d5b6eSIlya Dryomov 3887c9e9f97bSIlya Dryomov memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); 3888c9e9f97bSIlya Dryomov memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); 3889c9e9f97bSIlya Dryomov memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); 389019a39dceSIlya Dryomov 389119a39dceSIlya Dryomov if (lock) { 389219a39dceSIlya Dryomov spin_lock(&fs_info->balance_lock); 389319a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 389419a39dceSIlya Dryomov spin_unlock(&fs_info->balance_lock); 389519a39dceSIlya Dryomov } else { 389619a39dceSIlya Dryomov memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); 389719a39dceSIlya Dryomov } 3898c9e9f97bSIlya Dryomov } 3899c9e9f97bSIlya Dryomov 39009ba1f6e4SLiu Bo static long btrfs_ioctl_balance(struct file *file, void __user *arg) 3901c9e9f97bSIlya Dryomov { 3902496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 3903c9e9f97bSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 3904c9e9f97bSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 3905c9e9f97bSIlya Dryomov struct btrfs_balance_control *bctl; 3906ed0fb78fSIlya Dryomov bool need_unlock; /* for mut. excl. ops lock */ 3907c9e9f97bSIlya Dryomov int ret; 3908c9e9f97bSIlya Dryomov 3909c9e9f97bSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 3910c9e9f97bSIlya Dryomov return -EPERM; 3911c9e9f97bSIlya Dryomov 3912e54bfa31SLiu Bo ret = mnt_want_write_file(file); 39139ba1f6e4SLiu Bo if (ret) 39149ba1f6e4SLiu Bo return ret; 39159ba1f6e4SLiu Bo 3916ed0fb78fSIlya Dryomov again: 3917ed0fb78fSIlya Dryomov if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { 3918c9e9f97bSIlya Dryomov mutex_lock(&fs_info->volume_mutex); 3919c9e9f97bSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3920ed0fb78fSIlya Dryomov need_unlock = true; 3921ed0fb78fSIlya Dryomov goto locked; 3922ed0fb78fSIlya Dryomov } 3923ed0fb78fSIlya Dryomov 3924ed0fb78fSIlya Dryomov /* 3925ed0fb78fSIlya Dryomov * mut. excl. ops lock is locked. Three possibilites: 3926ed0fb78fSIlya Dryomov * (1) some other op is running 3927ed0fb78fSIlya Dryomov * (2) balance is running 3928ed0fb78fSIlya Dryomov * (3) balance is paused -- special case (think resume) 3929ed0fb78fSIlya Dryomov */ 3930ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3931ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3932ed0fb78fSIlya Dryomov /* this is either (2) or (3) */ 3933ed0fb78fSIlya Dryomov if (!atomic_read(&fs_info->balance_running)) { 3934ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3935ed0fb78fSIlya Dryomov if (!mutex_trylock(&fs_info->volume_mutex)) 3936ed0fb78fSIlya Dryomov goto again; 3937ed0fb78fSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 3938ed0fb78fSIlya Dryomov 3939ed0fb78fSIlya Dryomov if (fs_info->balance_ctl && 3940ed0fb78fSIlya Dryomov !atomic_read(&fs_info->balance_running)) { 3941ed0fb78fSIlya Dryomov /* this is (3) */ 3942ed0fb78fSIlya Dryomov need_unlock = false; 3943ed0fb78fSIlya Dryomov goto locked; 3944ed0fb78fSIlya Dryomov } 3945ed0fb78fSIlya Dryomov 3946ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3947ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 3948ed0fb78fSIlya Dryomov goto again; 3949ed0fb78fSIlya Dryomov } else { 3950ed0fb78fSIlya Dryomov /* this is (2) */ 3951ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3952ed0fb78fSIlya Dryomov ret = -EINPROGRESS; 3953ed0fb78fSIlya Dryomov goto out; 3954ed0fb78fSIlya Dryomov } 3955ed0fb78fSIlya Dryomov } else { 3956ed0fb78fSIlya Dryomov /* this is (1) */ 3957ed0fb78fSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 3958e57138b3SAnand Jain ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 3959ed0fb78fSIlya Dryomov goto out; 3960ed0fb78fSIlya Dryomov } 3961ed0fb78fSIlya Dryomov 3962ed0fb78fSIlya Dryomov locked: 3963ed0fb78fSIlya Dryomov BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); 3964c9e9f97bSIlya Dryomov 3965c9e9f97bSIlya Dryomov if (arg) { 3966c9e9f97bSIlya Dryomov bargs = memdup_user(arg, sizeof(*bargs)); 3967c9e9f97bSIlya Dryomov if (IS_ERR(bargs)) { 3968c9e9f97bSIlya Dryomov ret = PTR_ERR(bargs); 3969ed0fb78fSIlya Dryomov goto out_unlock; 3970c9e9f97bSIlya Dryomov } 3971de322263SIlya Dryomov 3972de322263SIlya Dryomov if (bargs->flags & BTRFS_BALANCE_RESUME) { 3973de322263SIlya Dryomov if (!fs_info->balance_ctl) { 3974de322263SIlya Dryomov ret = -ENOTCONN; 3975de322263SIlya Dryomov goto out_bargs; 3976de322263SIlya Dryomov } 3977de322263SIlya Dryomov 3978de322263SIlya Dryomov bctl = fs_info->balance_ctl; 3979de322263SIlya Dryomov spin_lock(&fs_info->balance_lock); 3980de322263SIlya Dryomov bctl->flags |= BTRFS_BALANCE_RESUME; 3981de322263SIlya Dryomov spin_unlock(&fs_info->balance_lock); 3982de322263SIlya Dryomov 3983de322263SIlya Dryomov goto do_balance; 3984de322263SIlya Dryomov } 3985c9e9f97bSIlya Dryomov } else { 3986c9e9f97bSIlya Dryomov bargs = NULL; 3987c9e9f97bSIlya Dryomov } 3988c9e9f97bSIlya Dryomov 3989ed0fb78fSIlya Dryomov if (fs_info->balance_ctl) { 3990837d5b6eSIlya Dryomov ret = -EINPROGRESS; 3991837d5b6eSIlya Dryomov goto out_bargs; 3992837d5b6eSIlya Dryomov } 3993837d5b6eSIlya Dryomov 3994c9e9f97bSIlya Dryomov bctl = kzalloc(sizeof(*bctl), GFP_NOFS); 3995c9e9f97bSIlya Dryomov if (!bctl) { 3996c9e9f97bSIlya Dryomov ret = -ENOMEM; 3997c9e9f97bSIlya Dryomov goto out_bargs; 3998c9e9f97bSIlya Dryomov } 3999c9e9f97bSIlya Dryomov 4000c9e9f97bSIlya Dryomov bctl->fs_info = fs_info; 4001c9e9f97bSIlya Dryomov if (arg) { 4002c9e9f97bSIlya Dryomov memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); 4003c9e9f97bSIlya Dryomov memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); 4004c9e9f97bSIlya Dryomov memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); 4005c9e9f97bSIlya Dryomov 4006c9e9f97bSIlya Dryomov bctl->flags = bargs->flags; 4007f43ffb60SIlya Dryomov } else { 4008f43ffb60SIlya Dryomov /* balance everything - no filters */ 4009f43ffb60SIlya Dryomov bctl->flags |= BTRFS_BALANCE_TYPE_MASK; 4010c9e9f97bSIlya Dryomov } 4011c9e9f97bSIlya Dryomov 4012de322263SIlya Dryomov do_balance: 4013c9e9f97bSIlya Dryomov /* 4014ed0fb78fSIlya Dryomov * Ownership of bctl and mutually_exclusive_operation_running 4015ed0fb78fSIlya Dryomov * goes to to btrfs_balance. bctl is freed in __cancel_balance, 4016ed0fb78fSIlya Dryomov * or, if restriper was paused all the way until unmount, in 4017ed0fb78fSIlya Dryomov * free_fs_info. mutually_exclusive_operation_running is 4018ed0fb78fSIlya Dryomov * cleared in __cancel_balance. 4019c9e9f97bSIlya Dryomov */ 4020ed0fb78fSIlya Dryomov need_unlock = false; 4021ed0fb78fSIlya Dryomov 4022ed0fb78fSIlya Dryomov ret = btrfs_balance(bctl, bargs); 4023ed0fb78fSIlya Dryomov 4024c9e9f97bSIlya Dryomov if (arg) { 4025c9e9f97bSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 4026c9e9f97bSIlya Dryomov ret = -EFAULT; 4027c9e9f97bSIlya Dryomov } 4028c9e9f97bSIlya Dryomov 4029c9e9f97bSIlya Dryomov out_bargs: 4030c9e9f97bSIlya Dryomov kfree(bargs); 4031ed0fb78fSIlya Dryomov out_unlock: 4032c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 4033c9e9f97bSIlya Dryomov mutex_unlock(&fs_info->volume_mutex); 4034ed0fb78fSIlya Dryomov if (need_unlock) 4035ed0fb78fSIlya Dryomov atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 4036ed0fb78fSIlya Dryomov out: 4037e54bfa31SLiu Bo mnt_drop_write_file(file); 4038c9e9f97bSIlya Dryomov return ret; 4039c9e9f97bSIlya Dryomov } 4040c9e9f97bSIlya Dryomov 4041837d5b6eSIlya Dryomov static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4042837d5b6eSIlya Dryomov { 4043837d5b6eSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 4044837d5b6eSIlya Dryomov return -EPERM; 4045837d5b6eSIlya Dryomov 4046837d5b6eSIlya Dryomov switch (cmd) { 4047837d5b6eSIlya Dryomov case BTRFS_BALANCE_CTL_PAUSE: 4048837d5b6eSIlya Dryomov return btrfs_pause_balance(root->fs_info); 4049a7e99c69SIlya Dryomov case BTRFS_BALANCE_CTL_CANCEL: 4050a7e99c69SIlya Dryomov return btrfs_cancel_balance(root->fs_info); 4051837d5b6eSIlya Dryomov } 4052837d5b6eSIlya Dryomov 4053837d5b6eSIlya Dryomov return -EINVAL; 4054837d5b6eSIlya Dryomov } 4055837d5b6eSIlya Dryomov 405619a39dceSIlya Dryomov static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 405719a39dceSIlya Dryomov void __user *arg) 405819a39dceSIlya Dryomov { 405919a39dceSIlya Dryomov struct btrfs_fs_info *fs_info = root->fs_info; 406019a39dceSIlya Dryomov struct btrfs_ioctl_balance_args *bargs; 406119a39dceSIlya Dryomov int ret = 0; 406219a39dceSIlya Dryomov 406319a39dceSIlya Dryomov if (!capable(CAP_SYS_ADMIN)) 406419a39dceSIlya Dryomov return -EPERM; 406519a39dceSIlya Dryomov 406619a39dceSIlya Dryomov mutex_lock(&fs_info->balance_mutex); 406719a39dceSIlya Dryomov if (!fs_info->balance_ctl) { 406819a39dceSIlya Dryomov ret = -ENOTCONN; 406919a39dceSIlya Dryomov goto out; 407019a39dceSIlya Dryomov } 407119a39dceSIlya Dryomov 407219a39dceSIlya Dryomov bargs = kzalloc(sizeof(*bargs), GFP_NOFS); 407319a39dceSIlya Dryomov if (!bargs) { 407419a39dceSIlya Dryomov ret = -ENOMEM; 407519a39dceSIlya Dryomov goto out; 407619a39dceSIlya Dryomov } 407719a39dceSIlya Dryomov 407819a39dceSIlya Dryomov update_ioctl_balance_args(fs_info, 1, bargs); 407919a39dceSIlya Dryomov 408019a39dceSIlya Dryomov if (copy_to_user(arg, bargs, sizeof(*bargs))) 408119a39dceSIlya Dryomov ret = -EFAULT; 408219a39dceSIlya Dryomov 408319a39dceSIlya Dryomov kfree(bargs); 408419a39dceSIlya Dryomov out: 408519a39dceSIlya Dryomov mutex_unlock(&fs_info->balance_mutex); 408619a39dceSIlya Dryomov return ret; 408719a39dceSIlya Dryomov } 408819a39dceSIlya Dryomov 4089905b0ddaSMiao Xie static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 40905d13a37bSArne Jansen { 4091496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 40925d13a37bSArne Jansen struct btrfs_ioctl_quota_ctl_args *sa; 40935d13a37bSArne Jansen struct btrfs_trans_handle *trans = NULL; 40945d13a37bSArne Jansen int ret; 40955d13a37bSArne Jansen int err; 40965d13a37bSArne Jansen 40975d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 40985d13a37bSArne Jansen return -EPERM; 40995d13a37bSArne Jansen 4100905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4101905b0ddaSMiao Xie if (ret) 4102905b0ddaSMiao Xie return ret; 41035d13a37bSArne Jansen 41045d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4105905b0ddaSMiao Xie if (IS_ERR(sa)) { 4106905b0ddaSMiao Xie ret = PTR_ERR(sa); 4107905b0ddaSMiao Xie goto drop_write; 4108905b0ddaSMiao Xie } 41095d13a37bSArne Jansen 41107708f029SWang Shilong down_write(&root->fs_info->subvol_sem); 411192f183aaSWang Shilong trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 41125d13a37bSArne Jansen if (IS_ERR(trans)) { 41135d13a37bSArne Jansen ret = PTR_ERR(trans); 41145d13a37bSArne Jansen goto out; 41155d13a37bSArne Jansen } 41165d13a37bSArne Jansen 41175d13a37bSArne Jansen switch (sa->cmd) { 41185d13a37bSArne Jansen case BTRFS_QUOTA_CTL_ENABLE: 41195d13a37bSArne Jansen ret = btrfs_quota_enable(trans, root->fs_info); 41205d13a37bSArne Jansen break; 41215d13a37bSArne Jansen case BTRFS_QUOTA_CTL_DISABLE: 41225d13a37bSArne Jansen ret = btrfs_quota_disable(trans, root->fs_info); 41235d13a37bSArne Jansen break; 41245d13a37bSArne Jansen default: 41255d13a37bSArne Jansen ret = -EINVAL; 41265d13a37bSArne Jansen break; 41275d13a37bSArne Jansen } 41285d13a37bSArne Jansen 412992f183aaSWang Shilong err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 41305d13a37bSArne Jansen if (err && !ret) 41315d13a37bSArne Jansen ret = err; 41325d13a37bSArne Jansen out: 41335d13a37bSArne Jansen kfree(sa); 41347708f029SWang Shilong up_write(&root->fs_info->subvol_sem); 4135905b0ddaSMiao Xie drop_write: 4136905b0ddaSMiao Xie mnt_drop_write_file(file); 41375d13a37bSArne Jansen return ret; 41385d13a37bSArne Jansen } 41395d13a37bSArne Jansen 4140905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 41415d13a37bSArne Jansen { 4142496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 41435d13a37bSArne Jansen struct btrfs_ioctl_qgroup_assign_args *sa; 41445d13a37bSArne Jansen struct btrfs_trans_handle *trans; 41455d13a37bSArne Jansen int ret; 41465d13a37bSArne Jansen int err; 41475d13a37bSArne Jansen 41485d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 41495d13a37bSArne Jansen return -EPERM; 41505d13a37bSArne Jansen 4151905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4152905b0ddaSMiao Xie if (ret) 4153905b0ddaSMiao Xie return ret; 41545d13a37bSArne Jansen 41555d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4156905b0ddaSMiao Xie if (IS_ERR(sa)) { 4157905b0ddaSMiao Xie ret = PTR_ERR(sa); 4158905b0ddaSMiao Xie goto drop_write; 4159905b0ddaSMiao Xie } 41605d13a37bSArne Jansen 41615d13a37bSArne Jansen trans = btrfs_join_transaction(root); 41625d13a37bSArne Jansen if (IS_ERR(trans)) { 41635d13a37bSArne Jansen ret = PTR_ERR(trans); 41645d13a37bSArne Jansen goto out; 41655d13a37bSArne Jansen } 41665d13a37bSArne Jansen 41675d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 41685d13a37bSArne Jansen if (sa->assign) { 41695d13a37bSArne Jansen ret = btrfs_add_qgroup_relation(trans, root->fs_info, 41705d13a37bSArne Jansen sa->src, sa->dst); 41715d13a37bSArne Jansen } else { 41725d13a37bSArne Jansen ret = btrfs_del_qgroup_relation(trans, root->fs_info, 41735d13a37bSArne Jansen sa->src, sa->dst); 41745d13a37bSArne Jansen } 41755d13a37bSArne Jansen 41765d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 41775d13a37bSArne Jansen if (err && !ret) 41785d13a37bSArne Jansen ret = err; 41795d13a37bSArne Jansen 41805d13a37bSArne Jansen out: 41815d13a37bSArne Jansen kfree(sa); 4182905b0ddaSMiao Xie drop_write: 4183905b0ddaSMiao Xie mnt_drop_write_file(file); 41845d13a37bSArne Jansen return ret; 41855d13a37bSArne Jansen } 41865d13a37bSArne Jansen 4187905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 41885d13a37bSArne Jansen { 4189496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 41905d13a37bSArne Jansen struct btrfs_ioctl_qgroup_create_args *sa; 41915d13a37bSArne Jansen struct btrfs_trans_handle *trans; 41925d13a37bSArne Jansen int ret; 41935d13a37bSArne Jansen int err; 41945d13a37bSArne Jansen 41955d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 41965d13a37bSArne Jansen return -EPERM; 41975d13a37bSArne Jansen 4198905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4199905b0ddaSMiao Xie if (ret) 4200905b0ddaSMiao Xie return ret; 42015d13a37bSArne Jansen 42025d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4203905b0ddaSMiao Xie if (IS_ERR(sa)) { 4204905b0ddaSMiao Xie ret = PTR_ERR(sa); 4205905b0ddaSMiao Xie goto drop_write; 4206905b0ddaSMiao Xie } 42075d13a37bSArne Jansen 4208d86e56cfSMiao Xie if (!sa->qgroupid) { 4209d86e56cfSMiao Xie ret = -EINVAL; 4210d86e56cfSMiao Xie goto out; 4211d86e56cfSMiao Xie } 4212d86e56cfSMiao Xie 42135d13a37bSArne Jansen trans = btrfs_join_transaction(root); 42145d13a37bSArne Jansen if (IS_ERR(trans)) { 42155d13a37bSArne Jansen ret = PTR_ERR(trans); 42165d13a37bSArne Jansen goto out; 42175d13a37bSArne Jansen } 42185d13a37bSArne Jansen 42195d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 42205d13a37bSArne Jansen if (sa->create) { 42215d13a37bSArne Jansen ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, 42225d13a37bSArne Jansen NULL); 42235d13a37bSArne Jansen } else { 42245d13a37bSArne Jansen ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 42255d13a37bSArne Jansen } 42265d13a37bSArne Jansen 42275d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 42285d13a37bSArne Jansen if (err && !ret) 42295d13a37bSArne Jansen ret = err; 42305d13a37bSArne Jansen 42315d13a37bSArne Jansen out: 42325d13a37bSArne Jansen kfree(sa); 4233905b0ddaSMiao Xie drop_write: 4234905b0ddaSMiao Xie mnt_drop_write_file(file); 42355d13a37bSArne Jansen return ret; 42365d13a37bSArne Jansen } 42375d13a37bSArne Jansen 4238905b0ddaSMiao Xie static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 42395d13a37bSArne Jansen { 4240496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 42415d13a37bSArne Jansen struct btrfs_ioctl_qgroup_limit_args *sa; 42425d13a37bSArne Jansen struct btrfs_trans_handle *trans; 42435d13a37bSArne Jansen int ret; 42445d13a37bSArne Jansen int err; 42455d13a37bSArne Jansen u64 qgroupid; 42465d13a37bSArne Jansen 42475d13a37bSArne Jansen if (!capable(CAP_SYS_ADMIN)) 42485d13a37bSArne Jansen return -EPERM; 42495d13a37bSArne Jansen 4250905b0ddaSMiao Xie ret = mnt_want_write_file(file); 4251905b0ddaSMiao Xie if (ret) 4252905b0ddaSMiao Xie return ret; 42535d13a37bSArne Jansen 42545d13a37bSArne Jansen sa = memdup_user(arg, sizeof(*sa)); 4255905b0ddaSMiao Xie if (IS_ERR(sa)) { 4256905b0ddaSMiao Xie ret = PTR_ERR(sa); 4257905b0ddaSMiao Xie goto drop_write; 4258905b0ddaSMiao Xie } 42595d13a37bSArne Jansen 42605d13a37bSArne Jansen trans = btrfs_join_transaction(root); 42615d13a37bSArne Jansen if (IS_ERR(trans)) { 42625d13a37bSArne Jansen ret = PTR_ERR(trans); 42635d13a37bSArne Jansen goto out; 42645d13a37bSArne Jansen } 42655d13a37bSArne Jansen 42665d13a37bSArne Jansen qgroupid = sa->qgroupid; 42675d13a37bSArne Jansen if (!qgroupid) { 42685d13a37bSArne Jansen /* take the current subvol as qgroup */ 42695d13a37bSArne Jansen qgroupid = root->root_key.objectid; 42705d13a37bSArne Jansen } 42715d13a37bSArne Jansen 42725d13a37bSArne Jansen /* FIXME: check if the IDs really exist */ 42735d13a37bSArne Jansen ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 42745d13a37bSArne Jansen 42755d13a37bSArne Jansen err = btrfs_end_transaction(trans, root); 42765d13a37bSArne Jansen if (err && !ret) 42775d13a37bSArne Jansen ret = err; 42785d13a37bSArne Jansen 42795d13a37bSArne Jansen out: 42805d13a37bSArne Jansen kfree(sa); 4281905b0ddaSMiao Xie drop_write: 4282905b0ddaSMiao Xie mnt_drop_write_file(file); 42835d13a37bSArne Jansen return ret; 42845d13a37bSArne Jansen } 42855d13a37bSArne Jansen 42862f232036SJan Schmidt static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 42872f232036SJan Schmidt { 42886d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 42892f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 42902f232036SJan Schmidt int ret; 42912f232036SJan Schmidt 42922f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 42932f232036SJan Schmidt return -EPERM; 42942f232036SJan Schmidt 42952f232036SJan Schmidt ret = mnt_want_write_file(file); 42962f232036SJan Schmidt if (ret) 42972f232036SJan Schmidt return ret; 42982f232036SJan Schmidt 42992f232036SJan Schmidt qsa = memdup_user(arg, sizeof(*qsa)); 43002f232036SJan Schmidt if (IS_ERR(qsa)) { 43012f232036SJan Schmidt ret = PTR_ERR(qsa); 43022f232036SJan Schmidt goto drop_write; 43032f232036SJan Schmidt } 43042f232036SJan Schmidt 43052f232036SJan Schmidt if (qsa->flags) { 43062f232036SJan Schmidt ret = -EINVAL; 43072f232036SJan Schmidt goto out; 43082f232036SJan Schmidt } 43092f232036SJan Schmidt 43102f232036SJan Schmidt ret = btrfs_qgroup_rescan(root->fs_info); 43112f232036SJan Schmidt 43122f232036SJan Schmidt out: 43132f232036SJan Schmidt kfree(qsa); 43142f232036SJan Schmidt drop_write: 43152f232036SJan Schmidt mnt_drop_write_file(file); 43162f232036SJan Schmidt return ret; 43172f232036SJan Schmidt } 43182f232036SJan Schmidt 43192f232036SJan Schmidt static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 43202f232036SJan Schmidt { 43216d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 43222f232036SJan Schmidt struct btrfs_ioctl_quota_rescan_args *qsa; 43232f232036SJan Schmidt int ret = 0; 43242f232036SJan Schmidt 43252f232036SJan Schmidt if (!capable(CAP_SYS_ADMIN)) 43262f232036SJan Schmidt return -EPERM; 43272f232036SJan Schmidt 43282f232036SJan Schmidt qsa = kzalloc(sizeof(*qsa), GFP_NOFS); 43292f232036SJan Schmidt if (!qsa) 43302f232036SJan Schmidt return -ENOMEM; 43312f232036SJan Schmidt 43322f232036SJan Schmidt if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 43332f232036SJan Schmidt qsa->flags = 1; 43342f232036SJan Schmidt qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 43352f232036SJan Schmidt } 43362f232036SJan Schmidt 43372f232036SJan Schmidt if (copy_to_user(arg, qsa, sizeof(*qsa))) 43382f232036SJan Schmidt ret = -EFAULT; 43392f232036SJan Schmidt 43402f232036SJan Schmidt kfree(qsa); 43412f232036SJan Schmidt return ret; 43422f232036SJan Schmidt } 43432f232036SJan Schmidt 434457254b6eSJan Schmidt static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 434557254b6eSJan Schmidt { 434654563d41SAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 434757254b6eSJan Schmidt 434857254b6eSJan Schmidt if (!capable(CAP_SYS_ADMIN)) 434957254b6eSJan Schmidt return -EPERM; 435057254b6eSJan Schmidt 435157254b6eSJan Schmidt return btrfs_qgroup_wait_for_completion(root->fs_info); 435257254b6eSJan Schmidt } 435357254b6eSJan Schmidt 43548ea05e3aSAlexander Block static long btrfs_ioctl_set_received_subvol(struct file *file, 43558ea05e3aSAlexander Block void __user *arg) 43568ea05e3aSAlexander Block { 43578ea05e3aSAlexander Block struct btrfs_ioctl_received_subvol_args *sa = NULL; 4358496ad9aaSAl Viro struct inode *inode = file_inode(file); 43598ea05e3aSAlexander Block struct btrfs_root *root = BTRFS_I(inode)->root; 43608ea05e3aSAlexander Block struct btrfs_root_item *root_item = &root->root_item; 43618ea05e3aSAlexander Block struct btrfs_trans_handle *trans; 43628ea05e3aSAlexander Block struct timespec ct = CURRENT_TIME; 43638ea05e3aSAlexander Block int ret = 0; 4364dd5f9615SStefan Behrens int received_uuid_changed; 43658ea05e3aSAlexander Block 43668ea05e3aSAlexander Block ret = mnt_want_write_file(file); 43678ea05e3aSAlexander Block if (ret < 0) 43688ea05e3aSAlexander Block return ret; 43698ea05e3aSAlexander Block 43708ea05e3aSAlexander Block down_write(&root->fs_info->subvol_sem); 43718ea05e3aSAlexander Block 43728ea05e3aSAlexander Block if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 43738ea05e3aSAlexander Block ret = -EINVAL; 43748ea05e3aSAlexander Block goto out; 43758ea05e3aSAlexander Block } 43768ea05e3aSAlexander Block 43778ea05e3aSAlexander Block if (btrfs_root_readonly(root)) { 43788ea05e3aSAlexander Block ret = -EROFS; 43798ea05e3aSAlexander Block goto out; 43808ea05e3aSAlexander Block } 43818ea05e3aSAlexander Block 43828ea05e3aSAlexander Block if (!inode_owner_or_capable(inode)) { 43838ea05e3aSAlexander Block ret = -EACCES; 43848ea05e3aSAlexander Block goto out; 43858ea05e3aSAlexander Block } 43868ea05e3aSAlexander Block 43878ea05e3aSAlexander Block sa = memdup_user(arg, sizeof(*sa)); 43888ea05e3aSAlexander Block if (IS_ERR(sa)) { 43898ea05e3aSAlexander Block ret = PTR_ERR(sa); 43908ea05e3aSAlexander Block sa = NULL; 43918ea05e3aSAlexander Block goto out; 43928ea05e3aSAlexander Block } 43938ea05e3aSAlexander Block 4394dd5f9615SStefan Behrens /* 4395dd5f9615SStefan Behrens * 1 - root item 4396dd5f9615SStefan Behrens * 2 - uuid items (received uuid + subvol uuid) 4397dd5f9615SStefan Behrens */ 4398dd5f9615SStefan Behrens trans = btrfs_start_transaction(root, 3); 43998ea05e3aSAlexander Block if (IS_ERR(trans)) { 44008ea05e3aSAlexander Block ret = PTR_ERR(trans); 44018ea05e3aSAlexander Block trans = NULL; 44028ea05e3aSAlexander Block goto out; 44038ea05e3aSAlexander Block } 44048ea05e3aSAlexander Block 44058ea05e3aSAlexander Block sa->rtransid = trans->transid; 44068ea05e3aSAlexander Block sa->rtime.sec = ct.tv_sec; 44078ea05e3aSAlexander Block sa->rtime.nsec = ct.tv_nsec; 44088ea05e3aSAlexander Block 4409dd5f9615SStefan Behrens received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, 4410dd5f9615SStefan Behrens BTRFS_UUID_SIZE); 4411dd5f9615SStefan Behrens if (received_uuid_changed && 4412dd5f9615SStefan Behrens !btrfs_is_empty_uuid(root_item->received_uuid)) 4413dd5f9615SStefan Behrens btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root, 4414dd5f9615SStefan Behrens root_item->received_uuid, 4415dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4416dd5f9615SStefan Behrens root->root_key.objectid); 44178ea05e3aSAlexander Block memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 44188ea05e3aSAlexander Block btrfs_set_root_stransid(root_item, sa->stransid); 44198ea05e3aSAlexander Block btrfs_set_root_rtransid(root_item, sa->rtransid); 44203cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); 44213cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); 44223cae210fSQu Wenruo btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 44233cae210fSQu Wenruo btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 44248ea05e3aSAlexander Block 44258ea05e3aSAlexander Block ret = btrfs_update_root(trans, root->fs_info->tree_root, 44268ea05e3aSAlexander Block &root->root_key, &root->root_item); 44278ea05e3aSAlexander Block if (ret < 0) { 44288ea05e3aSAlexander Block btrfs_end_transaction(trans, root); 44298ea05e3aSAlexander Block goto out; 4430dd5f9615SStefan Behrens } 4431dd5f9615SStefan Behrens if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 4432dd5f9615SStefan Behrens ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root, 4433dd5f9615SStefan Behrens sa->uuid, 4434dd5f9615SStefan Behrens BTRFS_UUID_KEY_RECEIVED_SUBVOL, 4435dd5f9615SStefan Behrens root->root_key.objectid); 4436dd5f9615SStefan Behrens if (ret < 0 && ret != -EEXIST) { 4437dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 4438dd5f9615SStefan Behrens goto out; 4439dd5f9615SStefan Behrens } 4440dd5f9615SStefan Behrens } 44418ea05e3aSAlexander Block ret = btrfs_commit_transaction(trans, root); 4442dd5f9615SStefan Behrens if (ret < 0) { 4443dd5f9615SStefan Behrens btrfs_abort_transaction(trans, root, ret); 44448ea05e3aSAlexander Block goto out; 44458ea05e3aSAlexander Block } 44468ea05e3aSAlexander Block 44478ea05e3aSAlexander Block ret = copy_to_user(arg, sa, sizeof(*sa)); 44488ea05e3aSAlexander Block if (ret) 44498ea05e3aSAlexander Block ret = -EFAULT; 44508ea05e3aSAlexander Block 44518ea05e3aSAlexander Block out: 44528ea05e3aSAlexander Block kfree(sa); 44538ea05e3aSAlexander Block up_write(&root->fs_info->subvol_sem); 44548ea05e3aSAlexander Block mnt_drop_write_file(file); 44558ea05e3aSAlexander Block return ret; 44568ea05e3aSAlexander Block } 44578ea05e3aSAlexander Block 4458867ab667Sjeff.liu static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 4459867ab667Sjeff.liu { 44606d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4461a1b83ac5SAnand Jain size_t len; 4462867ab667Sjeff.liu int ret; 4463a1b83ac5SAnand Jain char label[BTRFS_LABEL_SIZE]; 4464a1b83ac5SAnand Jain 4465a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4466a1b83ac5SAnand Jain memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 4467a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4468a1b83ac5SAnand Jain 4469a1b83ac5SAnand Jain len = strnlen(label, BTRFS_LABEL_SIZE); 4470867ab667Sjeff.liu 4471867ab667Sjeff.liu if (len == BTRFS_LABEL_SIZE) { 4472867ab667Sjeff.liu pr_warn("btrfs: label is too long, return the first %zu bytes\n", 4473867ab667Sjeff.liu --len); 4474867ab667Sjeff.liu } 4475867ab667Sjeff.liu 4476867ab667Sjeff.liu ret = copy_to_user(arg, label, len); 4477867ab667Sjeff.liu 4478867ab667Sjeff.liu return ret ? -EFAULT : 0; 4479867ab667Sjeff.liu } 4480867ab667Sjeff.liu 4481a8bfd4abSjeff.liu static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 4482a8bfd4abSjeff.liu { 44836d0379ecSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4484a8bfd4abSjeff.liu struct btrfs_super_block *super_block = root->fs_info->super_copy; 4485a8bfd4abSjeff.liu struct btrfs_trans_handle *trans; 4486a8bfd4abSjeff.liu char label[BTRFS_LABEL_SIZE]; 4487a8bfd4abSjeff.liu int ret; 4488a8bfd4abSjeff.liu 4489a8bfd4abSjeff.liu if (!capable(CAP_SYS_ADMIN)) 4490a8bfd4abSjeff.liu return -EPERM; 4491a8bfd4abSjeff.liu 4492a8bfd4abSjeff.liu if (copy_from_user(label, arg, sizeof(label))) 4493a8bfd4abSjeff.liu return -EFAULT; 4494a8bfd4abSjeff.liu 4495a8bfd4abSjeff.liu if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 4496a8bfd4abSjeff.liu pr_err("btrfs: unable to set label with more than %d bytes\n", 4497a8bfd4abSjeff.liu BTRFS_LABEL_SIZE - 1); 4498a8bfd4abSjeff.liu return -EINVAL; 4499a8bfd4abSjeff.liu } 4500a8bfd4abSjeff.liu 4501a8bfd4abSjeff.liu ret = mnt_want_write_file(file); 4502a8bfd4abSjeff.liu if (ret) 4503a8bfd4abSjeff.liu return ret; 4504a8bfd4abSjeff.liu 4505a8bfd4abSjeff.liu trans = btrfs_start_transaction(root, 0); 4506a8bfd4abSjeff.liu if (IS_ERR(trans)) { 4507a8bfd4abSjeff.liu ret = PTR_ERR(trans); 4508a8bfd4abSjeff.liu goto out_unlock; 4509a8bfd4abSjeff.liu } 4510a8bfd4abSjeff.liu 4511a1b83ac5SAnand Jain spin_lock(&root->fs_info->super_lock); 4512a8bfd4abSjeff.liu strcpy(super_block->label, label); 4513a1b83ac5SAnand Jain spin_unlock(&root->fs_info->super_lock); 4514a8bfd4abSjeff.liu ret = btrfs_end_transaction(trans, root); 4515a8bfd4abSjeff.liu 4516a8bfd4abSjeff.liu out_unlock: 4517a8bfd4abSjeff.liu mnt_drop_write_file(file); 4518a8bfd4abSjeff.liu return ret; 4519a8bfd4abSjeff.liu } 4520a8bfd4abSjeff.liu 45212eaa055fSJeff Mahoney #define INIT_FEATURE_FLAGS(suffix) \ 45222eaa055fSJeff Mahoney { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ 45232eaa055fSJeff Mahoney .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ 45242eaa055fSJeff Mahoney .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } 45252eaa055fSJeff Mahoney 45262eaa055fSJeff Mahoney static int btrfs_ioctl_get_supported_features(struct file *file, 45272eaa055fSJeff Mahoney void __user *arg) 45282eaa055fSJeff Mahoney { 45292eaa055fSJeff Mahoney static struct btrfs_ioctl_feature_flags features[3] = { 45302eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SUPP), 45312eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_SET), 45322eaa055fSJeff Mahoney INIT_FEATURE_FLAGS(SAFE_CLEAR) 45332eaa055fSJeff Mahoney }; 45342eaa055fSJeff Mahoney 45352eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 45362eaa055fSJeff Mahoney return -EFAULT; 45372eaa055fSJeff Mahoney 45382eaa055fSJeff Mahoney return 0; 45392eaa055fSJeff Mahoney } 45402eaa055fSJeff Mahoney 45412eaa055fSJeff Mahoney static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 45422eaa055fSJeff Mahoney { 45432eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 45442eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 45452eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags features; 45462eaa055fSJeff Mahoney 45472eaa055fSJeff Mahoney features.compat_flags = btrfs_super_compat_flags(super_block); 45482eaa055fSJeff Mahoney features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); 45492eaa055fSJeff Mahoney features.incompat_flags = btrfs_super_incompat_flags(super_block); 45502eaa055fSJeff Mahoney 45512eaa055fSJeff Mahoney if (copy_to_user(arg, &features, sizeof(features))) 45522eaa055fSJeff Mahoney return -EFAULT; 45532eaa055fSJeff Mahoney 45542eaa055fSJeff Mahoney return 0; 45552eaa055fSJeff Mahoney } 45562eaa055fSJeff Mahoney 45573b02a68aSJeff Mahoney static int check_feature_bits(struct btrfs_root *root, 45583b02a68aSJeff Mahoney enum btrfs_feature_set set, 45592eaa055fSJeff Mahoney u64 change_mask, u64 flags, u64 supported_flags, 45602eaa055fSJeff Mahoney u64 safe_set, u64 safe_clear) 45612eaa055fSJeff Mahoney { 45623b02a68aSJeff Mahoney const char *type = btrfs_feature_set_names[set]; 45633b02a68aSJeff Mahoney char *names; 45642eaa055fSJeff Mahoney u64 disallowed, unsupported; 45652eaa055fSJeff Mahoney u64 set_mask = flags & change_mask; 45662eaa055fSJeff Mahoney u64 clear_mask = ~flags & change_mask; 45672eaa055fSJeff Mahoney 45682eaa055fSJeff Mahoney unsupported = set_mask & ~supported_flags; 45692eaa055fSJeff Mahoney if (unsupported) { 45703b02a68aSJeff Mahoney names = btrfs_printable_features(set, unsupported); 45713b02a68aSJeff Mahoney if (names) { 45723b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 45733b02a68aSJeff Mahoney "this kernel does not support the %s feature bit%s", 45743b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 45753b02a68aSJeff Mahoney kfree(names); 45763b02a68aSJeff Mahoney } else 45772eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 45782eaa055fSJeff Mahoney "this kernel does not support %s bits 0x%llx", 45792eaa055fSJeff Mahoney type, unsupported); 45802eaa055fSJeff Mahoney return -EOPNOTSUPP; 45812eaa055fSJeff Mahoney } 45822eaa055fSJeff Mahoney 45832eaa055fSJeff Mahoney disallowed = set_mask & ~safe_set; 45842eaa055fSJeff Mahoney if (disallowed) { 45853b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 45863b02a68aSJeff Mahoney if (names) { 45873b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 45883b02a68aSJeff Mahoney "can't set the %s feature bit%s while mounted", 45893b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 45903b02a68aSJeff Mahoney kfree(names); 45913b02a68aSJeff Mahoney } else 45922eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 45932eaa055fSJeff Mahoney "can't set %s bits 0x%llx while mounted", 45942eaa055fSJeff Mahoney type, disallowed); 45952eaa055fSJeff Mahoney return -EPERM; 45962eaa055fSJeff Mahoney } 45972eaa055fSJeff Mahoney 45982eaa055fSJeff Mahoney disallowed = clear_mask & ~safe_clear; 45992eaa055fSJeff Mahoney if (disallowed) { 46003b02a68aSJeff Mahoney names = btrfs_printable_features(set, disallowed); 46013b02a68aSJeff Mahoney if (names) { 46023b02a68aSJeff Mahoney btrfs_warn(root->fs_info, 46033b02a68aSJeff Mahoney "can't clear the %s feature bit%s while mounted", 46043b02a68aSJeff Mahoney names, strchr(names, ',') ? "s" : ""); 46053b02a68aSJeff Mahoney kfree(names); 46063b02a68aSJeff Mahoney } else 46072eaa055fSJeff Mahoney btrfs_warn(root->fs_info, 46082eaa055fSJeff Mahoney "can't clear %s bits 0x%llx while mounted", 46092eaa055fSJeff Mahoney type, disallowed); 46102eaa055fSJeff Mahoney return -EPERM; 46112eaa055fSJeff Mahoney } 46122eaa055fSJeff Mahoney 46132eaa055fSJeff Mahoney return 0; 46142eaa055fSJeff Mahoney } 46152eaa055fSJeff Mahoney 46162eaa055fSJeff Mahoney #define check_feature(root, change_mask, flags, mask_base) \ 46173b02a68aSJeff Mahoney check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 46182eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 46192eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 46202eaa055fSJeff Mahoney BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 46212eaa055fSJeff Mahoney 46222eaa055fSJeff Mahoney static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 46232eaa055fSJeff Mahoney { 46242eaa055fSJeff Mahoney struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46252eaa055fSJeff Mahoney struct btrfs_super_block *super_block = root->fs_info->super_copy; 46262eaa055fSJeff Mahoney struct btrfs_ioctl_feature_flags flags[2]; 46272eaa055fSJeff Mahoney struct btrfs_trans_handle *trans; 46282eaa055fSJeff Mahoney u64 newflags; 46292eaa055fSJeff Mahoney int ret; 46302eaa055fSJeff Mahoney 46312eaa055fSJeff Mahoney if (!capable(CAP_SYS_ADMIN)) 46322eaa055fSJeff Mahoney return -EPERM; 46332eaa055fSJeff Mahoney 46342eaa055fSJeff Mahoney if (copy_from_user(flags, arg, sizeof(flags))) 46352eaa055fSJeff Mahoney return -EFAULT; 46362eaa055fSJeff Mahoney 46372eaa055fSJeff Mahoney /* Nothing to do */ 46382eaa055fSJeff Mahoney if (!flags[0].compat_flags && !flags[0].compat_ro_flags && 46392eaa055fSJeff Mahoney !flags[0].incompat_flags) 46402eaa055fSJeff Mahoney return 0; 46412eaa055fSJeff Mahoney 46422eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_flags, 46432eaa055fSJeff Mahoney flags[1].compat_flags, COMPAT); 46442eaa055fSJeff Mahoney if (ret) 46452eaa055fSJeff Mahoney return ret; 46462eaa055fSJeff Mahoney 46472eaa055fSJeff Mahoney ret = check_feature(root, flags[0].compat_ro_flags, 46482eaa055fSJeff Mahoney flags[1].compat_ro_flags, COMPAT_RO); 46492eaa055fSJeff Mahoney if (ret) 46502eaa055fSJeff Mahoney return ret; 46512eaa055fSJeff Mahoney 46522eaa055fSJeff Mahoney ret = check_feature(root, flags[0].incompat_flags, 46532eaa055fSJeff Mahoney flags[1].incompat_flags, INCOMPAT); 46542eaa055fSJeff Mahoney if (ret) 46552eaa055fSJeff Mahoney return ret; 46562eaa055fSJeff Mahoney 46572eaa055fSJeff Mahoney trans = btrfs_start_transaction(root, 1); 46582eaa055fSJeff Mahoney if (IS_ERR(trans)) 46592eaa055fSJeff Mahoney return PTR_ERR(trans); 46602eaa055fSJeff Mahoney 46612eaa055fSJeff Mahoney spin_lock(&root->fs_info->super_lock); 46622eaa055fSJeff Mahoney newflags = btrfs_super_compat_flags(super_block); 46632eaa055fSJeff Mahoney newflags |= flags[0].compat_flags & flags[1].compat_flags; 46642eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 46652eaa055fSJeff Mahoney btrfs_set_super_compat_flags(super_block, newflags); 46662eaa055fSJeff Mahoney 46672eaa055fSJeff Mahoney newflags = btrfs_super_compat_ro_flags(super_block); 46682eaa055fSJeff Mahoney newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; 46692eaa055fSJeff Mahoney newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); 46702eaa055fSJeff Mahoney btrfs_set_super_compat_ro_flags(super_block, newflags); 46712eaa055fSJeff Mahoney 46722eaa055fSJeff Mahoney newflags = btrfs_super_incompat_flags(super_block); 46732eaa055fSJeff Mahoney newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 46742eaa055fSJeff Mahoney newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 46752eaa055fSJeff Mahoney btrfs_set_super_incompat_flags(super_block, newflags); 46762eaa055fSJeff Mahoney spin_unlock(&root->fs_info->super_lock); 46772eaa055fSJeff Mahoney 46782eaa055fSJeff Mahoney return btrfs_end_transaction(trans, root); 46792eaa055fSJeff Mahoney } 46802eaa055fSJeff Mahoney 4681f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 4682f46b5a66SChristoph Hellwig cmd, unsigned long arg) 4683f46b5a66SChristoph Hellwig { 4684496ad9aaSAl Viro struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 46854bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 4686f46b5a66SChristoph Hellwig 4687f46b5a66SChristoph Hellwig switch (cmd) { 46886cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 46896cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 46906cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 46916cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 46926cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 46936cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 4694f7039b1dSLi Dongyang case FITRIM: 4695f7039b1dSLi Dongyang return btrfs_ioctl_fitrim(file, argp); 4696f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 4697fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 4698fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 4699fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 47003de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 4701fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 47026f72c7e2SArne Jansen case BTRFS_IOC_SUBVOL_CREATE_V2: 47036f72c7e2SArne Jansen return btrfs_ioctl_snap_create_v2(file, argp, 1); 470476dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 470576dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 47060caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 47070caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 47080caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 47090caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 47106ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 47116ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 4712f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 47131e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 47141e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 47151e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 4716f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 4717198605a8SMiao Xie return btrfs_ioctl_resize(file, argp); 4718f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 47194bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 4720f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 4721da24927bSMiao Xie return btrfs_ioctl_rm_dev(file, argp); 4722475f6387SJan Schmidt case BTRFS_IOC_FS_INFO: 4723475f6387SJan Schmidt return btrfs_ioctl_fs_info(root, argp); 4724475f6387SJan Schmidt case BTRFS_IOC_DEV_INFO: 4725475f6387SJan Schmidt return btrfs_ioctl_dev_info(root, argp); 4726f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 47279ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, NULL); 4728f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 4729c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 4730c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 47317a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 4732f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 4733f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 4734f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 4735f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 4736ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 4737ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 4738ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 4739ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 4740d7728c96SJan Schmidt case BTRFS_IOC_INO_PATHS: 4741d7728c96SJan Schmidt return btrfs_ioctl_ino_to_path(root, argp); 4742d7728c96SJan Schmidt case BTRFS_IOC_LOGICAL_INO: 4743d7728c96SJan Schmidt return btrfs_ioctl_logical_to_ino(root, argp); 47441406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 47451406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 474601e219e8SJeff Mahoney case BTRFS_IOC_GLOBAL_RSV: 474701e219e8SJeff Mahoney return btrfs_ioctl_global_rsv(root, argp); 47489b199859SFilipe David Borba Manana case BTRFS_IOC_SYNC: { 47499b199859SFilipe David Borba Manana int ret; 47509b199859SFilipe David Borba Manana 475191aef86fSMiao Xie ret = btrfs_start_delalloc_roots(root->fs_info, 0); 47529b199859SFilipe David Borba Manana if (ret) 47539b199859SFilipe David Borba Manana return ret; 47549b199859SFilipe David Borba Manana ret = btrfs_sync_fs(file->f_dentry->d_sb, 1); 47559b199859SFilipe David Borba Manana return ret; 47569b199859SFilipe David Borba Manana } 475746204592SSage Weil case BTRFS_IOC_START_SYNC: 47589a8c28beSMiao Xie return btrfs_ioctl_start_sync(root, argp); 475946204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 47609a8c28beSMiao Xie return btrfs_ioctl_wait_sync(root, argp); 4761475f6387SJan Schmidt case BTRFS_IOC_SCRUB: 4762b8e95489SMiao Xie return btrfs_ioctl_scrub(file, argp); 4763475f6387SJan Schmidt case BTRFS_IOC_SCRUB_CANCEL: 4764475f6387SJan Schmidt return btrfs_ioctl_scrub_cancel(root, argp); 4765475f6387SJan Schmidt case BTRFS_IOC_SCRUB_PROGRESS: 4766475f6387SJan Schmidt return btrfs_ioctl_scrub_progress(root, argp); 4767c9e9f97bSIlya Dryomov case BTRFS_IOC_BALANCE_V2: 47689ba1f6e4SLiu Bo return btrfs_ioctl_balance(file, argp); 4769837d5b6eSIlya Dryomov case BTRFS_IOC_BALANCE_CTL: 4770837d5b6eSIlya Dryomov return btrfs_ioctl_balance_ctl(root, arg); 477119a39dceSIlya Dryomov case BTRFS_IOC_BALANCE_PROGRESS: 477219a39dceSIlya Dryomov return btrfs_ioctl_balance_progress(root, argp); 47738ea05e3aSAlexander Block case BTRFS_IOC_SET_RECEIVED_SUBVOL: 47748ea05e3aSAlexander Block return btrfs_ioctl_set_received_subvol(file, argp); 477531db9f7cSAlexander Block case BTRFS_IOC_SEND: 477631db9f7cSAlexander Block return btrfs_ioctl_send(file, argp); 4777c11d2c23SStefan Behrens case BTRFS_IOC_GET_DEV_STATS: 4778b27f7c0cSDavid Sterba return btrfs_ioctl_get_dev_stats(root, argp); 47795d13a37bSArne Jansen case BTRFS_IOC_QUOTA_CTL: 4780905b0ddaSMiao Xie return btrfs_ioctl_quota_ctl(file, argp); 47815d13a37bSArne Jansen case BTRFS_IOC_QGROUP_ASSIGN: 4782905b0ddaSMiao Xie return btrfs_ioctl_qgroup_assign(file, argp); 47835d13a37bSArne Jansen case BTRFS_IOC_QGROUP_CREATE: 4784905b0ddaSMiao Xie return btrfs_ioctl_qgroup_create(file, argp); 47855d13a37bSArne Jansen case BTRFS_IOC_QGROUP_LIMIT: 4786905b0ddaSMiao Xie return btrfs_ioctl_qgroup_limit(file, argp); 47872f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN: 47882f232036SJan Schmidt return btrfs_ioctl_quota_rescan(file, argp); 47892f232036SJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_STATUS: 47902f232036SJan Schmidt return btrfs_ioctl_quota_rescan_status(file, argp); 479157254b6eSJan Schmidt case BTRFS_IOC_QUOTA_RESCAN_WAIT: 479257254b6eSJan Schmidt return btrfs_ioctl_quota_rescan_wait(file, argp); 47933f6bcfbdSStefan Behrens case BTRFS_IOC_DEV_REPLACE: 47943f6bcfbdSStefan Behrens return btrfs_ioctl_dev_replace(root, argp); 4795867ab667Sjeff.liu case BTRFS_IOC_GET_FSLABEL: 4796867ab667Sjeff.liu return btrfs_ioctl_get_fslabel(file, argp); 4797a8bfd4abSjeff.liu case BTRFS_IOC_SET_FSLABEL: 4798a8bfd4abSjeff.liu return btrfs_ioctl_set_fslabel(file, argp); 4799416161dbSMark Fasheh case BTRFS_IOC_FILE_EXTENT_SAME: 4800416161dbSMark Fasheh return btrfs_ioctl_file_extent_same(file, argp); 48012eaa055fSJeff Mahoney case BTRFS_IOC_GET_SUPPORTED_FEATURES: 48022eaa055fSJeff Mahoney return btrfs_ioctl_get_supported_features(file, argp); 48032eaa055fSJeff Mahoney case BTRFS_IOC_GET_FEATURES: 48042eaa055fSJeff Mahoney return btrfs_ioctl_get_features(file, argp); 48052eaa055fSJeff Mahoney case BTRFS_IOC_SET_FEATURES: 48062eaa055fSJeff Mahoney return btrfs_ioctl_set_features(file, argp); 4807f46b5a66SChristoph Hellwig } 4808f46b5a66SChristoph Hellwig 4809f46b5a66SChristoph Hellwig return -ENOTTY; 4810f46b5a66SChristoph Hellwig } 4811