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> 434b4e25f2SChris Mason #include "compat.h" 44f46b5a66SChristoph Hellwig #include "ctree.h" 45f46b5a66SChristoph Hellwig #include "disk-io.h" 46f46b5a66SChristoph Hellwig #include "transaction.h" 47f46b5a66SChristoph Hellwig #include "btrfs_inode.h" 48f46b5a66SChristoph Hellwig #include "ioctl.h" 49f46b5a66SChristoph Hellwig #include "print-tree.h" 50f46b5a66SChristoph Hellwig #include "volumes.h" 51925baeddSChris Mason #include "locking.h" 52f46b5a66SChristoph Hellwig 536cbff00fSChristoph Hellwig /* Mask out flags that are inappropriate for the given type of inode. */ 546cbff00fSChristoph Hellwig static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) 556cbff00fSChristoph Hellwig { 566cbff00fSChristoph Hellwig if (S_ISDIR(mode)) 576cbff00fSChristoph Hellwig return flags; 586cbff00fSChristoph Hellwig else if (S_ISREG(mode)) 596cbff00fSChristoph Hellwig return flags & ~FS_DIRSYNC_FL; 606cbff00fSChristoph Hellwig else 616cbff00fSChristoph Hellwig return flags & (FS_NODUMP_FL | FS_NOATIME_FL); 626cbff00fSChristoph Hellwig } 63f46b5a66SChristoph Hellwig 646cbff00fSChristoph Hellwig /* 656cbff00fSChristoph Hellwig * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. 666cbff00fSChristoph Hellwig */ 676cbff00fSChristoph Hellwig static unsigned int btrfs_flags_to_ioctl(unsigned int flags) 686cbff00fSChristoph Hellwig { 696cbff00fSChristoph Hellwig unsigned int iflags = 0; 706cbff00fSChristoph Hellwig 716cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_SYNC) 726cbff00fSChristoph Hellwig iflags |= FS_SYNC_FL; 736cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_IMMUTABLE) 746cbff00fSChristoph Hellwig iflags |= FS_IMMUTABLE_FL; 756cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_APPEND) 766cbff00fSChristoph Hellwig iflags |= FS_APPEND_FL; 776cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NODUMP) 786cbff00fSChristoph Hellwig iflags |= FS_NODUMP_FL; 796cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_NOATIME) 806cbff00fSChristoph Hellwig iflags |= FS_NOATIME_FL; 816cbff00fSChristoph Hellwig if (flags & BTRFS_INODE_DIRSYNC) 826cbff00fSChristoph Hellwig iflags |= FS_DIRSYNC_FL; 836cbff00fSChristoph Hellwig 846cbff00fSChristoph Hellwig return iflags; 856cbff00fSChristoph Hellwig } 866cbff00fSChristoph Hellwig 876cbff00fSChristoph Hellwig /* 886cbff00fSChristoph Hellwig * Update inode->i_flags based on the btrfs internal flags. 896cbff00fSChristoph Hellwig */ 906cbff00fSChristoph Hellwig void btrfs_update_iflags(struct inode *inode) 916cbff00fSChristoph Hellwig { 926cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 936cbff00fSChristoph Hellwig 946cbff00fSChristoph Hellwig inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 956cbff00fSChristoph Hellwig 966cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_SYNC) 976cbff00fSChristoph Hellwig inode->i_flags |= S_SYNC; 986cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_IMMUTABLE) 996cbff00fSChristoph Hellwig inode->i_flags |= S_IMMUTABLE; 1006cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_APPEND) 1016cbff00fSChristoph Hellwig inode->i_flags |= S_APPEND; 1026cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_NOATIME) 1036cbff00fSChristoph Hellwig inode->i_flags |= S_NOATIME; 1046cbff00fSChristoph Hellwig if (ip->flags & BTRFS_INODE_DIRSYNC) 1056cbff00fSChristoph Hellwig inode->i_flags |= S_DIRSYNC; 1066cbff00fSChristoph Hellwig } 1076cbff00fSChristoph Hellwig 1086cbff00fSChristoph Hellwig /* 1096cbff00fSChristoph Hellwig * Inherit flags from the parent inode. 1106cbff00fSChristoph Hellwig * 1116cbff00fSChristoph Hellwig * Unlike extN we don't have any flags we don't want to inherit currently. 1126cbff00fSChristoph Hellwig */ 1136cbff00fSChristoph Hellwig void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) 1146cbff00fSChristoph Hellwig { 1150b4dcea5SChris Mason unsigned int flags; 1160b4dcea5SChris Mason 1170b4dcea5SChris Mason if (!dir) 1180b4dcea5SChris Mason return; 1190b4dcea5SChris Mason 1200b4dcea5SChris Mason flags = BTRFS_I(dir)->flags; 1216cbff00fSChristoph Hellwig 1226cbff00fSChristoph Hellwig if (S_ISREG(inode->i_mode)) 1236cbff00fSChristoph Hellwig flags &= ~BTRFS_INODE_DIRSYNC; 1246cbff00fSChristoph Hellwig else if (!S_ISDIR(inode->i_mode)) 1256cbff00fSChristoph Hellwig flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME); 1266cbff00fSChristoph Hellwig 1276cbff00fSChristoph Hellwig BTRFS_I(inode)->flags = flags; 1286cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 1296cbff00fSChristoph Hellwig } 1306cbff00fSChristoph Hellwig 1316cbff00fSChristoph Hellwig static int btrfs_ioctl_getflags(struct file *file, void __user *arg) 1326cbff00fSChristoph Hellwig { 1336cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); 1346cbff00fSChristoph Hellwig unsigned int flags = btrfs_flags_to_ioctl(ip->flags); 1356cbff00fSChristoph Hellwig 1366cbff00fSChristoph Hellwig if (copy_to_user(arg, &flags, sizeof(flags))) 1376cbff00fSChristoph Hellwig return -EFAULT; 1386cbff00fSChristoph Hellwig return 0; 1396cbff00fSChristoph Hellwig } 1406cbff00fSChristoph Hellwig 1416cbff00fSChristoph Hellwig static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 1426cbff00fSChristoph Hellwig { 1436cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 1446cbff00fSChristoph Hellwig struct btrfs_inode *ip = BTRFS_I(inode); 1456cbff00fSChristoph Hellwig struct btrfs_root *root = ip->root; 1466cbff00fSChristoph Hellwig struct btrfs_trans_handle *trans; 1476cbff00fSChristoph Hellwig unsigned int flags, oldflags; 1486cbff00fSChristoph Hellwig int ret; 1496cbff00fSChristoph Hellwig 150b83cc969SLi Zefan if (btrfs_root_readonly(root)) 151b83cc969SLi Zefan return -EROFS; 152b83cc969SLi Zefan 1536cbff00fSChristoph Hellwig if (copy_from_user(&flags, arg, sizeof(flags))) 1546cbff00fSChristoph Hellwig return -EFAULT; 1556cbff00fSChristoph Hellwig 1566cbff00fSChristoph Hellwig if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 1576cbff00fSChristoph Hellwig FS_NOATIME_FL | FS_NODUMP_FL | \ 1586cbff00fSChristoph Hellwig FS_SYNC_FL | FS_DIRSYNC_FL)) 1596cbff00fSChristoph Hellwig return -EOPNOTSUPP; 1606cbff00fSChristoph Hellwig 161*2e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1626cbff00fSChristoph Hellwig return -EACCES; 1636cbff00fSChristoph Hellwig 1646cbff00fSChristoph Hellwig mutex_lock(&inode->i_mutex); 1656cbff00fSChristoph Hellwig 1666cbff00fSChristoph Hellwig flags = btrfs_mask_flags(inode->i_mode, flags); 1676cbff00fSChristoph Hellwig oldflags = btrfs_flags_to_ioctl(ip->flags); 1686cbff00fSChristoph Hellwig if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { 1696cbff00fSChristoph Hellwig if (!capable(CAP_LINUX_IMMUTABLE)) { 1706cbff00fSChristoph Hellwig ret = -EPERM; 1716cbff00fSChristoph Hellwig goto out_unlock; 1726cbff00fSChristoph Hellwig } 1736cbff00fSChristoph Hellwig } 1746cbff00fSChristoph Hellwig 1756cbff00fSChristoph Hellwig ret = mnt_want_write(file->f_path.mnt); 1766cbff00fSChristoph Hellwig if (ret) 1776cbff00fSChristoph Hellwig goto out_unlock; 1786cbff00fSChristoph Hellwig 1796cbff00fSChristoph Hellwig if (flags & FS_SYNC_FL) 1806cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_SYNC; 1816cbff00fSChristoph Hellwig else 1826cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_SYNC; 1836cbff00fSChristoph Hellwig if (flags & FS_IMMUTABLE_FL) 1846cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_IMMUTABLE; 1856cbff00fSChristoph Hellwig else 1866cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_IMMUTABLE; 1876cbff00fSChristoph Hellwig if (flags & FS_APPEND_FL) 1886cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_APPEND; 1896cbff00fSChristoph Hellwig else 1906cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_APPEND; 1916cbff00fSChristoph Hellwig if (flags & FS_NODUMP_FL) 1926cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NODUMP; 1936cbff00fSChristoph Hellwig else 1946cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NODUMP; 1956cbff00fSChristoph Hellwig if (flags & FS_NOATIME_FL) 1966cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_NOATIME; 1976cbff00fSChristoph Hellwig else 1986cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_NOATIME; 1996cbff00fSChristoph Hellwig if (flags & FS_DIRSYNC_FL) 2006cbff00fSChristoph Hellwig ip->flags |= BTRFS_INODE_DIRSYNC; 2016cbff00fSChristoph Hellwig else 2026cbff00fSChristoph Hellwig ip->flags &= ~BTRFS_INODE_DIRSYNC; 2036cbff00fSChristoph Hellwig 2046cbff00fSChristoph Hellwig 2056cbff00fSChristoph Hellwig trans = btrfs_join_transaction(root, 1); 2063612b495STsutomu Itoh BUG_ON(IS_ERR(trans)); 2076cbff00fSChristoph Hellwig 2086cbff00fSChristoph Hellwig ret = btrfs_update_inode(trans, root, inode); 2096cbff00fSChristoph Hellwig BUG_ON(ret); 2106cbff00fSChristoph Hellwig 2116cbff00fSChristoph Hellwig btrfs_update_iflags(inode); 2126cbff00fSChristoph Hellwig inode->i_ctime = CURRENT_TIME; 2136cbff00fSChristoph Hellwig btrfs_end_transaction(trans, root); 2146cbff00fSChristoph Hellwig 2156cbff00fSChristoph Hellwig mnt_drop_write(file->f_path.mnt); 2166cbff00fSChristoph Hellwig out_unlock: 2176cbff00fSChristoph Hellwig mutex_unlock(&inode->i_mutex); 2186cbff00fSChristoph Hellwig return 0; 2196cbff00fSChristoph Hellwig } 2206cbff00fSChristoph Hellwig 2216cbff00fSChristoph Hellwig static int btrfs_ioctl_getversion(struct file *file, int __user *arg) 2226cbff00fSChristoph Hellwig { 2236cbff00fSChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 2246cbff00fSChristoph Hellwig 2256cbff00fSChristoph Hellwig return put_user(inode->i_generation, arg); 2266cbff00fSChristoph Hellwig } 227f46b5a66SChristoph Hellwig 228cb8e7090SChristoph Hellwig static noinline int create_subvol(struct btrfs_root *root, 229cb8e7090SChristoph Hellwig struct dentry *dentry, 23072fd032eSSage Weil char *name, int namelen, 23172fd032eSSage Weil u64 *async_transid) 232f46b5a66SChristoph Hellwig { 233f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 234f46b5a66SChristoph Hellwig struct btrfs_key key; 235f46b5a66SChristoph Hellwig struct btrfs_root_item root_item; 236f46b5a66SChristoph Hellwig struct btrfs_inode_item *inode_item; 237f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 23876dda93cSYan, Zheng struct btrfs_root *new_root; 2396a912213SJosef Bacik struct dentry *parent = dget_parent(dentry); 2406a912213SJosef Bacik struct inode *dir; 241f46b5a66SChristoph Hellwig int ret; 242f46b5a66SChristoph Hellwig int err; 243f46b5a66SChristoph Hellwig u64 objectid; 244f46b5a66SChristoph Hellwig u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 2453de4586cSChris Mason u64 index = 0; 246f46b5a66SChristoph Hellwig 247a22285a6SYan, Zheng ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root, 248a22285a6SYan, Zheng 0, &objectid); 2496a912213SJosef Bacik if (ret) { 2506a912213SJosef Bacik dput(parent); 251a22285a6SYan, Zheng return ret; 2526a912213SJosef Bacik } 2536a912213SJosef Bacik 2546a912213SJosef Bacik dir = parent->d_inode; 2556a912213SJosef Bacik 2569ed74f2dSJosef Bacik /* 2579ed74f2dSJosef Bacik * 1 - inode item 2589ed74f2dSJosef Bacik * 2 - refs 2599ed74f2dSJosef Bacik * 1 - root item 2609ed74f2dSJosef Bacik * 2 - dir items 2619ed74f2dSJosef Bacik */ 262a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 2636a912213SJosef Bacik if (IS_ERR(trans)) { 2646a912213SJosef Bacik dput(parent); 265a22285a6SYan, Zheng return PTR_ERR(trans); 2666a912213SJosef Bacik } 267f46b5a66SChristoph Hellwig 2685d4f98a2SYan Zheng leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 2695d4f98a2SYan Zheng 0, objectid, NULL, 0, 0, 0); 2708e8a1e31SJosef Bacik if (IS_ERR(leaf)) { 2718e8a1e31SJosef Bacik ret = PTR_ERR(leaf); 2728e8a1e31SJosef Bacik goto fail; 2738e8a1e31SJosef Bacik } 274f46b5a66SChristoph Hellwig 2755d4f98a2SYan Zheng memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); 276f46b5a66SChristoph Hellwig btrfs_set_header_bytenr(leaf, leaf->start); 277f46b5a66SChristoph Hellwig btrfs_set_header_generation(leaf, trans->transid); 2785d4f98a2SYan Zheng btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 279f46b5a66SChristoph Hellwig btrfs_set_header_owner(leaf, objectid); 280f46b5a66SChristoph Hellwig 281f46b5a66SChristoph Hellwig write_extent_buffer(leaf, root->fs_info->fsid, 282f46b5a66SChristoph Hellwig (unsigned long)btrfs_header_fsid(leaf), 283f46b5a66SChristoph Hellwig BTRFS_FSID_SIZE); 2845d4f98a2SYan Zheng write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, 2855d4f98a2SYan Zheng (unsigned long)btrfs_header_chunk_tree_uuid(leaf), 2865d4f98a2SYan Zheng BTRFS_UUID_SIZE); 287f46b5a66SChristoph Hellwig btrfs_mark_buffer_dirty(leaf); 288f46b5a66SChristoph Hellwig 289f46b5a66SChristoph Hellwig inode_item = &root_item.inode; 290f46b5a66SChristoph Hellwig memset(inode_item, 0, sizeof(*inode_item)); 291f46b5a66SChristoph Hellwig inode_item->generation = cpu_to_le64(1); 292f46b5a66SChristoph Hellwig inode_item->size = cpu_to_le64(3); 293f46b5a66SChristoph Hellwig inode_item->nlink = cpu_to_le32(1); 294a76a3cd4SYan Zheng inode_item->nbytes = cpu_to_le64(root->leafsize); 295f46b5a66SChristoph Hellwig inode_item->mode = cpu_to_le32(S_IFDIR | 0755); 296f46b5a66SChristoph Hellwig 297f46b5a66SChristoph Hellwig btrfs_set_root_bytenr(&root_item, leaf->start); 29884234f3aSYan Zheng btrfs_set_root_generation(&root_item, trans->transid); 299f46b5a66SChristoph Hellwig btrfs_set_root_level(&root_item, 0); 300f46b5a66SChristoph Hellwig btrfs_set_root_refs(&root_item, 1); 30186b9f2ecSYan, Zheng btrfs_set_root_used(&root_item, leaf->len); 30280ff3856SYan Zheng btrfs_set_root_last_snapshot(&root_item, 0); 303f46b5a66SChristoph Hellwig 304f46b5a66SChristoph Hellwig memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); 305f46b5a66SChristoph Hellwig root_item.drop_level = 0; 306f46b5a66SChristoph Hellwig 307925baeddSChris Mason btrfs_tree_unlock(leaf); 308f46b5a66SChristoph Hellwig free_extent_buffer(leaf); 309f46b5a66SChristoph Hellwig leaf = NULL; 310f46b5a66SChristoph Hellwig 311f46b5a66SChristoph Hellwig btrfs_set_root_dirid(&root_item, new_dirid); 312f46b5a66SChristoph Hellwig 313f46b5a66SChristoph Hellwig key.objectid = objectid; 3145d4f98a2SYan Zheng key.offset = 0; 315f46b5a66SChristoph Hellwig btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); 316f46b5a66SChristoph Hellwig ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 317f46b5a66SChristoph Hellwig &root_item); 318f46b5a66SChristoph Hellwig if (ret) 319f46b5a66SChristoph Hellwig goto fail; 320f46b5a66SChristoph Hellwig 32176dda93cSYan, Zheng key.offset = (u64)-1; 32276dda93cSYan, Zheng new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 32376dda93cSYan, Zheng BUG_ON(IS_ERR(new_root)); 32476dda93cSYan, Zheng 32576dda93cSYan, Zheng btrfs_record_root_in_trans(trans, new_root); 32676dda93cSYan, Zheng 32776dda93cSYan, Zheng ret = btrfs_create_subvol_root(trans, new_root, new_dirid, 32876dda93cSYan, Zheng BTRFS_I(dir)->block_group); 329f46b5a66SChristoph Hellwig /* 330f46b5a66SChristoph Hellwig * insert the directory item 331f46b5a66SChristoph Hellwig */ 3323de4586cSChris Mason ret = btrfs_set_inode_index(dir, &index); 3333de4586cSChris Mason BUG_ON(ret); 3343de4586cSChris Mason 3353de4586cSChris Mason ret = btrfs_insert_dir_item(trans, root, 336f46b5a66SChristoph Hellwig name, namelen, dir->i_ino, &key, 3373de4586cSChris Mason BTRFS_FT_DIR, index); 338f46b5a66SChristoph Hellwig if (ret) 339f46b5a66SChristoph Hellwig goto fail; 3400660b5afSChris Mason 34152c26179SYan Zheng btrfs_i_size_write(dir, dir->i_size + namelen * 2); 34252c26179SYan Zheng ret = btrfs_update_inode(trans, root, dir); 34352c26179SYan Zheng BUG_ON(ret); 34452c26179SYan Zheng 3450660b5afSChris Mason ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 3464df27c4dSYan, Zheng objectid, root->root_key.objectid, 3470660b5afSChris Mason dir->i_ino, index, name, namelen); 34876dda93cSYan, Zheng 3490660b5afSChris Mason BUG_ON(ret); 3500660b5afSChris Mason 35176dda93cSYan, Zheng d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); 352f46b5a66SChristoph Hellwig fail: 3536a912213SJosef Bacik dput(parent); 35472fd032eSSage Weil if (async_transid) { 35572fd032eSSage Weil *async_transid = trans->transid; 35672fd032eSSage Weil err = btrfs_commit_transaction_async(trans, root, 1); 35772fd032eSSage Weil } else { 35876dda93cSYan, Zheng err = btrfs_commit_transaction(trans, root); 35972fd032eSSage Weil } 360f46b5a66SChristoph Hellwig if (err && !ret) 361f46b5a66SChristoph Hellwig ret = err; 362f46b5a66SChristoph Hellwig return ret; 363f46b5a66SChristoph Hellwig } 364f46b5a66SChristoph Hellwig 36572fd032eSSage Weil static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, 366b83cc969SLi Zefan char *name, int namelen, u64 *async_transid, 367b83cc969SLi Zefan bool readonly) 368f46b5a66SChristoph Hellwig { 3692e4bfab9SYan, Zheng struct inode *inode; 3706a912213SJosef Bacik struct dentry *parent; 371f46b5a66SChristoph Hellwig struct btrfs_pending_snapshot *pending_snapshot; 372f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 3732e4bfab9SYan, Zheng int ret; 374f46b5a66SChristoph Hellwig 375f46b5a66SChristoph Hellwig if (!root->ref_cows) 376f46b5a66SChristoph Hellwig return -EINVAL; 377f46b5a66SChristoph Hellwig 378a22285a6SYan, Zheng pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); 379a22285a6SYan, Zheng if (!pending_snapshot) 380a22285a6SYan, Zheng return -ENOMEM; 381a22285a6SYan, Zheng 382a22285a6SYan, Zheng btrfs_init_block_rsv(&pending_snapshot->block_rsv); 383a22285a6SYan, Zheng pending_snapshot->dentry = dentry; 384a22285a6SYan, Zheng pending_snapshot->root = root; 385b83cc969SLi Zefan pending_snapshot->readonly = readonly; 386a22285a6SYan, Zheng 387a22285a6SYan, Zheng trans = btrfs_start_transaction(root->fs_info->extent_root, 5); 388a22285a6SYan, Zheng if (IS_ERR(trans)) { 389a22285a6SYan, Zheng ret = PTR_ERR(trans); 390a22285a6SYan, Zheng goto fail; 391a22285a6SYan, Zheng } 392a22285a6SYan, Zheng 393a22285a6SYan, Zheng ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); 394a22285a6SYan, Zheng BUG_ON(ret); 395a22285a6SYan, Zheng 396a22285a6SYan, Zheng list_add(&pending_snapshot->list, 397a22285a6SYan, Zheng &trans->transaction->pending_snapshots); 39872fd032eSSage Weil if (async_transid) { 39972fd032eSSage Weil *async_transid = trans->transid; 40072fd032eSSage Weil ret = btrfs_commit_transaction_async(trans, 40172fd032eSSage Weil root->fs_info->extent_root, 1); 40272fd032eSSage Weil } else { 40372fd032eSSage Weil ret = btrfs_commit_transaction(trans, 40472fd032eSSage Weil root->fs_info->extent_root); 40572fd032eSSage Weil } 406a22285a6SYan, Zheng BUG_ON(ret); 407a22285a6SYan, Zheng 408a22285a6SYan, Zheng ret = pending_snapshot->error; 409f46b5a66SChristoph Hellwig if (ret) 4102e4bfab9SYan, Zheng goto fail; 411f46b5a66SChristoph Hellwig 412a22285a6SYan, Zheng btrfs_orphan_cleanup(pending_snapshot->snap); 413f46b5a66SChristoph Hellwig 4146a912213SJosef Bacik parent = dget_parent(dentry); 4156a912213SJosef Bacik inode = btrfs_lookup_dentry(parent->d_inode, dentry); 4166a912213SJosef Bacik dput(parent); 4172e4bfab9SYan, Zheng if (IS_ERR(inode)) { 4182e4bfab9SYan, Zheng ret = PTR_ERR(inode); 4192e4bfab9SYan, Zheng goto fail; 4202e4bfab9SYan, Zheng } 4212e4bfab9SYan, Zheng BUG_ON(!inode); 4222e4bfab9SYan, Zheng d_instantiate(dentry, inode); 4232e4bfab9SYan, Zheng ret = 0; 4242e4bfab9SYan, Zheng fail: 425a22285a6SYan, Zheng kfree(pending_snapshot); 426f46b5a66SChristoph Hellwig return ret; 427f46b5a66SChristoph Hellwig } 428f46b5a66SChristoph Hellwig 4294260f7c7SSage Weil /* copy of check_sticky in fs/namei.c() 4304260f7c7SSage Weil * It's inline, so penalty for filesystems that don't use sticky bit is 4314260f7c7SSage Weil * minimal. 4324260f7c7SSage Weil */ 4334260f7c7SSage Weil static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) 4344260f7c7SSage Weil { 4354260f7c7SSage Weil uid_t fsuid = current_fsuid(); 4364260f7c7SSage Weil 4374260f7c7SSage Weil if (!(dir->i_mode & S_ISVTX)) 4384260f7c7SSage Weil return 0; 4394260f7c7SSage Weil if (inode->i_uid == fsuid) 4404260f7c7SSage Weil return 0; 4414260f7c7SSage Weil if (dir->i_uid == fsuid) 4424260f7c7SSage Weil return 0; 4434260f7c7SSage Weil return !capable(CAP_FOWNER); 4444260f7c7SSage Weil } 4454260f7c7SSage Weil 4464260f7c7SSage Weil /* copy of may_delete in fs/namei.c() 4474260f7c7SSage Weil * Check whether we can remove a link victim from directory dir, check 4484260f7c7SSage Weil * whether the type of victim is right. 4494260f7c7SSage Weil * 1. We can't do it if dir is read-only (done in permission()) 4504260f7c7SSage Weil * 2. We should have write and exec permissions on dir 4514260f7c7SSage Weil * 3. We can't remove anything from append-only dir 4524260f7c7SSage Weil * 4. We can't do anything with immutable dir (done in permission()) 4534260f7c7SSage Weil * 5. If the sticky bit on dir is set we should either 4544260f7c7SSage Weil * a. be owner of dir, or 4554260f7c7SSage Weil * b. be owner of victim, or 4564260f7c7SSage Weil * c. have CAP_FOWNER capability 4574260f7c7SSage Weil * 6. If the victim is append-only or immutable we can't do antyhing with 4584260f7c7SSage Weil * links pointing to it. 4594260f7c7SSage Weil * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 4604260f7c7SSage Weil * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 4614260f7c7SSage Weil * 9. We can't remove a root or mountpoint. 4624260f7c7SSage Weil * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 4634260f7c7SSage Weil * nfs_async_unlink(). 4644260f7c7SSage Weil */ 4654260f7c7SSage Weil 4664260f7c7SSage Weil static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) 4674260f7c7SSage Weil { 4684260f7c7SSage Weil int error; 4694260f7c7SSage Weil 4704260f7c7SSage Weil if (!victim->d_inode) 4714260f7c7SSage Weil return -ENOENT; 4724260f7c7SSage Weil 4734260f7c7SSage Weil BUG_ON(victim->d_parent->d_inode != dir); 4744260f7c7SSage Weil audit_inode_child(victim, dir); 4754260f7c7SSage Weil 4764260f7c7SSage Weil error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 4774260f7c7SSage Weil if (error) 4784260f7c7SSage Weil return error; 4794260f7c7SSage Weil if (IS_APPEND(dir)) 4804260f7c7SSage Weil return -EPERM; 4814260f7c7SSage Weil if (btrfs_check_sticky(dir, victim->d_inode)|| 4824260f7c7SSage Weil IS_APPEND(victim->d_inode)|| 4834260f7c7SSage Weil IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 4844260f7c7SSage Weil return -EPERM; 4854260f7c7SSage Weil if (isdir) { 4864260f7c7SSage Weil if (!S_ISDIR(victim->d_inode->i_mode)) 4874260f7c7SSage Weil return -ENOTDIR; 4884260f7c7SSage Weil if (IS_ROOT(victim)) 4894260f7c7SSage Weil return -EBUSY; 4904260f7c7SSage Weil } else if (S_ISDIR(victim->d_inode->i_mode)) 4914260f7c7SSage Weil return -EISDIR; 4924260f7c7SSage Weil if (IS_DEADDIR(dir)) 4934260f7c7SSage Weil return -ENOENT; 4944260f7c7SSage Weil if (victim->d_flags & DCACHE_NFSFS_RENAMED) 4954260f7c7SSage Weil return -EBUSY; 4964260f7c7SSage Weil return 0; 4974260f7c7SSage Weil } 4984260f7c7SSage Weil 499cb8e7090SChristoph Hellwig /* copy of may_create in fs/namei.c() */ 500cb8e7090SChristoph Hellwig static inline int btrfs_may_create(struct inode *dir, struct dentry *child) 501cb8e7090SChristoph Hellwig { 502cb8e7090SChristoph Hellwig if (child->d_inode) 503cb8e7090SChristoph Hellwig return -EEXIST; 504cb8e7090SChristoph Hellwig if (IS_DEADDIR(dir)) 505cb8e7090SChristoph Hellwig return -ENOENT; 506cb8e7090SChristoph Hellwig return inode_permission(dir, MAY_WRITE | MAY_EXEC); 507cb8e7090SChristoph Hellwig } 508cb8e7090SChristoph Hellwig 509cb8e7090SChristoph Hellwig /* 510cb8e7090SChristoph Hellwig * Create a new subvolume below @parent. This is largely modeled after 511cb8e7090SChristoph Hellwig * sys_mkdirat and vfs_mkdir, but we only do a single component lookup 512cb8e7090SChristoph Hellwig * inside this filesystem so it's quite a bit simpler. 513cb8e7090SChristoph Hellwig */ 51476dda93cSYan, Zheng static noinline int btrfs_mksubvol(struct path *parent, 51576dda93cSYan, Zheng char *name, int namelen, 51672fd032eSSage Weil struct btrfs_root *snap_src, 517b83cc969SLi Zefan u64 *async_transid, bool readonly) 518cb8e7090SChristoph Hellwig { 51976dda93cSYan, Zheng struct inode *dir = parent->dentry->d_inode; 520cb8e7090SChristoph Hellwig struct dentry *dentry; 521cb8e7090SChristoph Hellwig int error; 522cb8e7090SChristoph Hellwig 52376dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 524cb8e7090SChristoph Hellwig 525cb8e7090SChristoph Hellwig dentry = lookup_one_len(name, parent->dentry, namelen); 526cb8e7090SChristoph Hellwig error = PTR_ERR(dentry); 527cb8e7090SChristoph Hellwig if (IS_ERR(dentry)) 528cb8e7090SChristoph Hellwig goto out_unlock; 529cb8e7090SChristoph Hellwig 530cb8e7090SChristoph Hellwig error = -EEXIST; 531cb8e7090SChristoph Hellwig if (dentry->d_inode) 532cb8e7090SChristoph Hellwig goto out_dput; 533cb8e7090SChristoph Hellwig 534cb8e7090SChristoph Hellwig error = mnt_want_write(parent->mnt); 535cb8e7090SChristoph Hellwig if (error) 536cb8e7090SChristoph Hellwig goto out_dput; 537cb8e7090SChristoph Hellwig 53876dda93cSYan, Zheng error = btrfs_may_create(dir, dentry); 539cb8e7090SChristoph Hellwig if (error) 540cb8e7090SChristoph Hellwig goto out_drop_write; 541cb8e7090SChristoph Hellwig 54276dda93cSYan, Zheng down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 54376dda93cSYan, Zheng 54476dda93cSYan, Zheng if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 54576dda93cSYan, Zheng goto out_up_read; 54676dda93cSYan, Zheng 5473de4586cSChris Mason if (snap_src) { 54872fd032eSSage Weil error = create_snapshot(snap_src, dentry, 549b83cc969SLi Zefan name, namelen, async_transid, readonly); 5503de4586cSChris Mason } else { 55176dda93cSYan, Zheng error = create_subvol(BTRFS_I(dir)->root, dentry, 55272fd032eSSage Weil name, namelen, async_transid); 5533de4586cSChris Mason } 55476dda93cSYan, Zheng if (!error) 55576dda93cSYan, Zheng fsnotify_mkdir(dir, dentry); 55676dda93cSYan, Zheng out_up_read: 55776dda93cSYan, Zheng up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 558cb8e7090SChristoph Hellwig out_drop_write: 559cb8e7090SChristoph Hellwig mnt_drop_write(parent->mnt); 560cb8e7090SChristoph Hellwig out_dput: 561cb8e7090SChristoph Hellwig dput(dentry); 562cb8e7090SChristoph Hellwig out_unlock: 56376dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 564cb8e7090SChristoph Hellwig return error; 565cb8e7090SChristoph Hellwig } 566cb8e7090SChristoph Hellwig 567940100a4SChris Mason static int should_defrag_range(struct inode *inode, u64 start, u64 len, 5681e701a32SChris Mason int thresh, u64 *last_len, u64 *skip, 5691e701a32SChris Mason u64 *defrag_end) 570940100a4SChris Mason { 571940100a4SChris Mason struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 572940100a4SChris Mason struct extent_map *em = NULL; 573940100a4SChris Mason struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 574940100a4SChris Mason int ret = 1; 575940100a4SChris Mason 5761e701a32SChris Mason 5771e701a32SChris Mason if (thresh == 0) 5781e701a32SChris Mason thresh = 256 * 1024; 5791e701a32SChris Mason 580940100a4SChris Mason /* 581940100a4SChris Mason * make sure that once we start defragging and extent, we keep on 582940100a4SChris Mason * defragging it 583940100a4SChris Mason */ 584940100a4SChris Mason if (start < *defrag_end) 585940100a4SChris Mason return 1; 586940100a4SChris Mason 587940100a4SChris Mason *skip = 0; 588940100a4SChris Mason 589940100a4SChris Mason /* 590940100a4SChris Mason * hopefully we have this extent in the tree already, try without 591940100a4SChris Mason * the full extent lock 592940100a4SChris Mason */ 593940100a4SChris Mason read_lock(&em_tree->lock); 594940100a4SChris Mason em = lookup_extent_mapping(em_tree, start, len); 595940100a4SChris Mason read_unlock(&em_tree->lock); 596940100a4SChris Mason 597940100a4SChris Mason if (!em) { 598940100a4SChris Mason /* get the big lock and read metadata off disk */ 599940100a4SChris Mason lock_extent(io_tree, start, start + len - 1, GFP_NOFS); 600940100a4SChris Mason em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 601940100a4SChris Mason unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); 602940100a4SChris Mason 6036cf8bfbfSDan Carpenter if (IS_ERR(em)) 604940100a4SChris Mason return 0; 605940100a4SChris Mason } 606940100a4SChris Mason 607940100a4SChris Mason /* this will cover holes, and inline extents */ 608940100a4SChris Mason if (em->block_start >= EXTENT_MAP_LAST_BYTE) 609940100a4SChris Mason ret = 0; 610940100a4SChris Mason 611940100a4SChris Mason /* 612940100a4SChris Mason * we hit a real extent, if it is big don't bother defragging it again 613940100a4SChris Mason */ 6141e701a32SChris Mason if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) 615940100a4SChris Mason ret = 0; 616940100a4SChris Mason 617940100a4SChris Mason /* 618940100a4SChris Mason * last_len ends up being a counter of how many bytes we've defragged. 619940100a4SChris Mason * every time we choose not to defrag an extent, we reset *last_len 620940100a4SChris Mason * so that the next tiny extent will force a defrag. 621940100a4SChris Mason * 622940100a4SChris Mason * The end result of this is that tiny extents before a single big 623940100a4SChris Mason * extent will force at least part of that big extent to be defragged. 624940100a4SChris Mason */ 625940100a4SChris Mason if (ret) { 626940100a4SChris Mason *last_len += len; 627940100a4SChris Mason *defrag_end = extent_map_end(em); 628940100a4SChris Mason } else { 629940100a4SChris Mason *last_len = 0; 630940100a4SChris Mason *skip = extent_map_end(em); 631940100a4SChris Mason *defrag_end = 0; 632940100a4SChris Mason } 633940100a4SChris Mason 634940100a4SChris Mason free_extent_map(em); 635940100a4SChris Mason return ret; 636940100a4SChris Mason } 637940100a4SChris Mason 6381e701a32SChris Mason static int btrfs_defrag_file(struct file *file, 6391e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range) 640f46b5a66SChristoph Hellwig { 641f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 642f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 643f46b5a66SChristoph Hellwig struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 6443eaa2885SChris Mason struct btrfs_ordered_extent *ordered; 645f46b5a66SChristoph Hellwig struct page *page; 6461a419d85SLi Zefan struct btrfs_super_block *disk_super; 647f46b5a66SChristoph Hellwig unsigned long last_index; 648f46b5a66SChristoph Hellwig unsigned long ra_pages = root->fs_info->bdi.ra_pages; 649f46b5a66SChristoph Hellwig unsigned long total_read = 0; 6501a419d85SLi Zefan u64 features; 651f46b5a66SChristoph Hellwig u64 page_start; 652f46b5a66SChristoph Hellwig u64 page_end; 653940100a4SChris Mason u64 last_len = 0; 654940100a4SChris Mason u64 skip = 0; 655940100a4SChris Mason u64 defrag_end = 0; 656f46b5a66SChristoph Hellwig unsigned long i; 657f46b5a66SChristoph Hellwig int ret; 6581a419d85SLi Zefan int compress_type = BTRFS_COMPRESS_ZLIB; 6591a419d85SLi Zefan 6601a419d85SLi Zefan if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { 6611a419d85SLi Zefan if (range->compress_type > BTRFS_COMPRESS_TYPES) 6621a419d85SLi Zefan return -EINVAL; 6631a419d85SLi Zefan if (range->compress_type) 6641a419d85SLi Zefan compress_type = range->compress_type; 6651a419d85SLi Zefan } 666f46b5a66SChristoph Hellwig 667940100a4SChris Mason if (inode->i_size == 0) 668940100a4SChris Mason return 0; 669f46b5a66SChristoph Hellwig 6701e701a32SChris Mason if (range->start + range->len > range->start) { 6711e701a32SChris Mason last_index = min_t(u64, inode->i_size - 1, 6721e701a32SChris Mason range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 6731e701a32SChris Mason } else { 674940100a4SChris Mason last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT; 6751e701a32SChris Mason } 6761e701a32SChris Mason 6771e701a32SChris Mason i = range->start >> PAGE_CACHE_SHIFT; 678940100a4SChris Mason while (i <= last_index) { 679940100a4SChris Mason if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 6801e701a32SChris Mason PAGE_CACHE_SIZE, 6811e701a32SChris Mason range->extent_thresh, 6821e701a32SChris Mason &last_len, &skip, 683940100a4SChris Mason &defrag_end)) { 684940100a4SChris Mason unsigned long next; 685940100a4SChris Mason /* 686940100a4SChris Mason * the should_defrag function tells us how much to skip 687940100a4SChris Mason * bump our counter by the suggested amount 688940100a4SChris Mason */ 689940100a4SChris Mason next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 690940100a4SChris Mason i = max(i + 1, next); 691940100a4SChris Mason continue; 692940100a4SChris Mason } 693940100a4SChris Mason 694f46b5a66SChristoph Hellwig if (total_read % ra_pages == 0) { 695f46b5a66SChristoph Hellwig btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, 696f46b5a66SChristoph Hellwig min(last_index, i + ra_pages - 1)); 697f46b5a66SChristoph Hellwig } 698f46b5a66SChristoph Hellwig total_read++; 699940100a4SChris Mason mutex_lock(&inode->i_mutex); 7001e701a32SChris Mason if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 7011a419d85SLi Zefan BTRFS_I(inode)->force_compress = compress_type; 702940100a4SChris Mason 7030ca1f7ceSYan, Zheng ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); 7040ca1f7ceSYan, Zheng if (ret) 7050ca1f7ceSYan, Zheng goto err_unlock; 7063eaa2885SChris Mason again: 707940100a4SChris Mason if (inode->i_size == 0 || 708940100a4SChris Mason i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) { 709940100a4SChris Mason ret = 0; 710940100a4SChris Mason goto err_reservations; 711940100a4SChris Mason } 712940100a4SChris Mason 713f46b5a66SChristoph Hellwig page = grab_cache_page(inode->i_mapping, i); 7140ca1f7ceSYan, Zheng if (!page) { 7150ca1f7ceSYan, Zheng ret = -ENOMEM; 716940100a4SChris Mason goto err_reservations; 7170ca1f7ceSYan, Zheng } 718940100a4SChris Mason 719f46b5a66SChristoph Hellwig if (!PageUptodate(page)) { 720f46b5a66SChristoph Hellwig btrfs_readpage(NULL, page); 721f46b5a66SChristoph Hellwig lock_page(page); 722f46b5a66SChristoph Hellwig if (!PageUptodate(page)) { 723f46b5a66SChristoph Hellwig unlock_page(page); 724f46b5a66SChristoph Hellwig page_cache_release(page); 7250ca1f7ceSYan, Zheng ret = -EIO; 726940100a4SChris Mason goto err_reservations; 727f46b5a66SChristoph Hellwig } 728f46b5a66SChristoph Hellwig } 729f46b5a66SChristoph Hellwig 730940100a4SChris Mason if (page->mapping != inode->i_mapping) { 731940100a4SChris Mason unlock_page(page); 732940100a4SChris Mason page_cache_release(page); 733940100a4SChris Mason goto again; 734940100a4SChris Mason } 735940100a4SChris Mason 736f46b5a66SChristoph Hellwig wait_on_page_writeback(page); 737f46b5a66SChristoph Hellwig 738940100a4SChris Mason if (PageDirty(page)) { 7390ca1f7ceSYan, Zheng btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); 740940100a4SChris Mason goto loop_unlock; 741940100a4SChris Mason } 742940100a4SChris Mason 743f46b5a66SChristoph Hellwig page_start = (u64)page->index << PAGE_CACHE_SHIFT; 744f46b5a66SChristoph Hellwig page_end = page_start + PAGE_CACHE_SIZE - 1; 745f46b5a66SChristoph Hellwig lock_extent(io_tree, page_start, page_end, GFP_NOFS); 7463eaa2885SChris Mason 7473eaa2885SChris Mason ordered = btrfs_lookup_ordered_extent(inode, page_start); 7483eaa2885SChris Mason if (ordered) { 7493eaa2885SChris Mason unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 7503eaa2885SChris Mason unlock_page(page); 7513eaa2885SChris Mason page_cache_release(page); 7523eaa2885SChris Mason btrfs_start_ordered_extent(inode, ordered, 1); 7533eaa2885SChris Mason btrfs_put_ordered_extent(ordered); 7543eaa2885SChris Mason goto again; 7553eaa2885SChris Mason } 7563eaa2885SChris Mason set_page_extent_mapped(page); 7573eaa2885SChris Mason 758f87f057bSChris Mason /* 759f87f057bSChris Mason * this makes sure page_mkwrite is called on the 760f87f057bSChris Mason * page if it is dirtied again later 761f87f057bSChris Mason */ 762f87f057bSChris Mason clear_page_dirty_for_io(page); 763940100a4SChris Mason clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, 764940100a4SChris Mason page_end, EXTENT_DIRTY | EXTENT_DELALLOC | 765940100a4SChris Mason EXTENT_DO_ACCOUNTING, GFP_NOFS); 766f87f057bSChris Mason 7672ac55d41SJosef Bacik btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); 768940100a4SChris Mason ClearPageChecked(page); 769f46b5a66SChristoph Hellwig set_page_dirty(page); 770a1ed835eSChris Mason unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 771940100a4SChris Mason 772940100a4SChris Mason loop_unlock: 773f46b5a66SChristoph Hellwig unlock_page(page); 774f46b5a66SChristoph Hellwig page_cache_release(page); 775940100a4SChris Mason mutex_unlock(&inode->i_mutex); 776940100a4SChris Mason 777f46b5a66SChristoph Hellwig balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); 778940100a4SChris Mason i++; 779f46b5a66SChristoph Hellwig } 780f46b5a66SChristoph Hellwig 7811e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 7821e701a32SChris Mason filemap_flush(inode->i_mapping); 7831e701a32SChris Mason 7841e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 7851e701a32SChris Mason /* the filemap_flush will queue IO into the worker threads, but 7861e701a32SChris Mason * we have to make sure the IO is actually started and that 7871e701a32SChris Mason * ordered extents get created before we return 7881e701a32SChris Mason */ 7891e701a32SChris Mason atomic_inc(&root->fs_info->async_submit_draining); 7901e701a32SChris Mason while (atomic_read(&root->fs_info->nr_async_submits) || 7911e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages)) { 7921e701a32SChris Mason wait_event(root->fs_info->async_submit_wait, 7931e701a32SChris Mason (atomic_read(&root->fs_info->nr_async_submits) == 0 && 7941e701a32SChris Mason atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 7951e701a32SChris Mason } 7961e701a32SChris Mason atomic_dec(&root->fs_info->async_submit_draining); 7971e701a32SChris Mason 7981e701a32SChris Mason mutex_lock(&inode->i_mutex); 799261507a0SLi Zefan BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; 8001e701a32SChris Mason mutex_unlock(&inode->i_mutex); 8011e701a32SChris Mason } 8021e701a32SChris Mason 8031a419d85SLi Zefan disk_super = &root->fs_info->super_copy; 8041a419d85SLi Zefan features = btrfs_super_incompat_flags(disk_super); 8051a419d85SLi Zefan if (range->compress_type == BTRFS_COMPRESS_LZO) { 8061a419d85SLi Zefan features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 8071a419d85SLi Zefan btrfs_set_super_incompat_flags(disk_super, features); 8081a419d85SLi Zefan } 8091a419d85SLi Zefan 810f46b5a66SChristoph Hellwig return 0; 811940100a4SChris Mason 812940100a4SChris Mason err_reservations: 8130ca1f7ceSYan, Zheng btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); 8140ca1f7ceSYan, Zheng err_unlock: 815940100a4SChris Mason mutex_unlock(&inode->i_mutex); 816940100a4SChris Mason return ret; 817f46b5a66SChristoph Hellwig } 818f46b5a66SChristoph Hellwig 81976dda93cSYan, Zheng static noinline int btrfs_ioctl_resize(struct btrfs_root *root, 82076dda93cSYan, Zheng void __user *arg) 821f46b5a66SChristoph Hellwig { 822f46b5a66SChristoph Hellwig u64 new_size; 823f46b5a66SChristoph Hellwig u64 old_size; 824f46b5a66SChristoph Hellwig u64 devid = 1; 825f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 826f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 827f46b5a66SChristoph Hellwig struct btrfs_device *device = NULL; 828f46b5a66SChristoph Hellwig char *sizestr; 829f46b5a66SChristoph Hellwig char *devstr = NULL; 830f46b5a66SChristoph Hellwig int ret = 0; 831f46b5a66SChristoph Hellwig int mod = 0; 832f46b5a66SChristoph Hellwig 833c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 834c146afadSYan Zheng return -EROFS; 835c146afadSYan Zheng 836e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 837e441d54dSChris Mason return -EPERM; 838e441d54dSChris Mason 839dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 840dae7b665SLi Zefan if (IS_ERR(vol_args)) 841dae7b665SLi Zefan return PTR_ERR(vol_args); 8425516e595SMark Fasheh 8435516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 844f46b5a66SChristoph Hellwig 8457d9eb12cSChris Mason mutex_lock(&root->fs_info->volume_mutex); 846f46b5a66SChristoph Hellwig sizestr = vol_args->name; 847f46b5a66SChristoph Hellwig devstr = strchr(sizestr, ':'); 848f46b5a66SChristoph Hellwig if (devstr) { 849f46b5a66SChristoph Hellwig char *end; 850f46b5a66SChristoph Hellwig sizestr = devstr + 1; 851f46b5a66SChristoph Hellwig *devstr = '\0'; 852f46b5a66SChristoph Hellwig devstr = vol_args->name; 853f46b5a66SChristoph Hellwig devid = simple_strtoull(devstr, &end, 10); 85421380931SJoel Becker printk(KERN_INFO "resizing devid %llu\n", 85521380931SJoel Becker (unsigned long long)devid); 856f46b5a66SChristoph Hellwig } 8572b82032cSYan Zheng device = btrfs_find_device(root, devid, NULL, NULL); 858f46b5a66SChristoph Hellwig if (!device) { 85921380931SJoel Becker printk(KERN_INFO "resizer unable to find device %llu\n", 86021380931SJoel Becker (unsigned long long)devid); 861f46b5a66SChristoph Hellwig ret = -EINVAL; 862f46b5a66SChristoph Hellwig goto out_unlock; 863f46b5a66SChristoph Hellwig } 864f46b5a66SChristoph Hellwig if (!strcmp(sizestr, "max")) 865f46b5a66SChristoph Hellwig new_size = device->bdev->bd_inode->i_size; 866f46b5a66SChristoph Hellwig else { 867f46b5a66SChristoph Hellwig if (sizestr[0] == '-') { 868f46b5a66SChristoph Hellwig mod = -1; 869f46b5a66SChristoph Hellwig sizestr++; 870f46b5a66SChristoph Hellwig } else if (sizestr[0] == '+') { 871f46b5a66SChristoph Hellwig mod = 1; 872f46b5a66SChristoph Hellwig sizestr++; 873f46b5a66SChristoph Hellwig } 87491748467SAkinobu Mita new_size = memparse(sizestr, NULL); 875f46b5a66SChristoph Hellwig if (new_size == 0) { 876f46b5a66SChristoph Hellwig ret = -EINVAL; 877f46b5a66SChristoph Hellwig goto out_unlock; 878f46b5a66SChristoph Hellwig } 879f46b5a66SChristoph Hellwig } 880f46b5a66SChristoph Hellwig 881f46b5a66SChristoph Hellwig old_size = device->total_bytes; 882f46b5a66SChristoph Hellwig 883f46b5a66SChristoph Hellwig if (mod < 0) { 884f46b5a66SChristoph Hellwig if (new_size > old_size) { 885f46b5a66SChristoph Hellwig ret = -EINVAL; 886f46b5a66SChristoph Hellwig goto out_unlock; 887f46b5a66SChristoph Hellwig } 888f46b5a66SChristoph Hellwig new_size = old_size - new_size; 889f46b5a66SChristoph Hellwig } else if (mod > 0) { 890f46b5a66SChristoph Hellwig new_size = old_size + new_size; 891f46b5a66SChristoph Hellwig } 892f46b5a66SChristoph Hellwig 893f46b5a66SChristoph Hellwig if (new_size < 256 * 1024 * 1024) { 894f46b5a66SChristoph Hellwig ret = -EINVAL; 895f46b5a66SChristoph Hellwig goto out_unlock; 896f46b5a66SChristoph Hellwig } 897f46b5a66SChristoph Hellwig if (new_size > device->bdev->bd_inode->i_size) { 898f46b5a66SChristoph Hellwig ret = -EFBIG; 899f46b5a66SChristoph Hellwig goto out_unlock; 900f46b5a66SChristoph Hellwig } 901f46b5a66SChristoph Hellwig 902f46b5a66SChristoph Hellwig do_div(new_size, root->sectorsize); 903f46b5a66SChristoph Hellwig new_size *= root->sectorsize; 904f46b5a66SChristoph Hellwig 905f46b5a66SChristoph Hellwig printk(KERN_INFO "new size for %s is %llu\n", 906f46b5a66SChristoph Hellwig device->name, (unsigned long long)new_size); 907f46b5a66SChristoph Hellwig 908f46b5a66SChristoph Hellwig if (new_size > old_size) { 909a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 91098d5dc13STsutomu Itoh if (IS_ERR(trans)) { 91198d5dc13STsutomu Itoh ret = PTR_ERR(trans); 91298d5dc13STsutomu Itoh goto out_unlock; 91398d5dc13STsutomu Itoh } 914f46b5a66SChristoph Hellwig ret = btrfs_grow_device(trans, device, new_size); 915f46b5a66SChristoph Hellwig btrfs_commit_transaction(trans, root); 916f46b5a66SChristoph Hellwig } else { 917f46b5a66SChristoph Hellwig ret = btrfs_shrink_device(device, new_size); 918f46b5a66SChristoph Hellwig } 919f46b5a66SChristoph Hellwig 920f46b5a66SChristoph Hellwig out_unlock: 9217d9eb12cSChris Mason mutex_unlock(&root->fs_info->volume_mutex); 922f46b5a66SChristoph Hellwig kfree(vol_args); 923f46b5a66SChristoph Hellwig return ret; 924f46b5a66SChristoph Hellwig } 925f46b5a66SChristoph Hellwig 92672fd032eSSage Weil static noinline int btrfs_ioctl_snap_create_transid(struct file *file, 92772fd032eSSage Weil char *name, 92872fd032eSSage Weil unsigned long fd, 92972fd032eSSage Weil int subvol, 930b83cc969SLi Zefan u64 *transid, 931b83cc969SLi Zefan bool readonly) 932f46b5a66SChristoph Hellwig { 933cb8e7090SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 9343de4586cSChris Mason struct file *src_file; 935f46b5a66SChristoph Hellwig int namelen; 9363de4586cSChris Mason int ret = 0; 937f46b5a66SChristoph Hellwig 938c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 939c146afadSYan Zheng return -EROFS; 940c146afadSYan Zheng 94172fd032eSSage Weil namelen = strlen(name); 94272fd032eSSage Weil if (strchr(name, '/')) { 943f46b5a66SChristoph Hellwig ret = -EINVAL; 944f46b5a66SChristoph Hellwig goto out; 945f46b5a66SChristoph Hellwig } 946f46b5a66SChristoph Hellwig 9473de4586cSChris Mason if (subvol) { 94872fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 949b83cc969SLi Zefan NULL, transid, readonly); 950cb8e7090SChristoph Hellwig } else { 9513de4586cSChris Mason struct inode *src_inode; 95272fd032eSSage Weil src_file = fget(fd); 9533de4586cSChris Mason if (!src_file) { 9543de4586cSChris Mason ret = -EINVAL; 9553de4586cSChris Mason goto out; 9563de4586cSChris Mason } 9573de4586cSChris Mason 9583de4586cSChris Mason src_inode = src_file->f_path.dentry->d_inode; 9593de4586cSChris Mason if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { 960d397712bSChris Mason printk(KERN_INFO "btrfs: Snapshot src from " 961d397712bSChris Mason "another FS\n"); 9623de4586cSChris Mason ret = -EINVAL; 9633de4586cSChris Mason fput(src_file); 9643de4586cSChris Mason goto out; 9653de4586cSChris Mason } 96672fd032eSSage Weil ret = btrfs_mksubvol(&file->f_path, name, namelen, 96772fd032eSSage Weil BTRFS_I(src_inode)->root, 968b83cc969SLi Zefan transid, readonly); 9693de4586cSChris Mason fput(src_file); 970cb8e7090SChristoph Hellwig } 971f46b5a66SChristoph Hellwig out: 97272fd032eSSage Weil return ret; 97372fd032eSSage Weil } 97472fd032eSSage Weil 97572fd032eSSage Weil static noinline int btrfs_ioctl_snap_create(struct file *file, 976fa0d2b9bSLi Zefan void __user *arg, int subvol) 97772fd032eSSage Weil { 978fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args *vol_args; 97972fd032eSSage Weil int ret; 98072fd032eSSage Weil 981fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 982fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 983fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 984fa0d2b9bSLi Zefan vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 985fa0d2b9bSLi Zefan 986fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 987b83cc969SLi Zefan vol_args->fd, subvol, 988b83cc969SLi Zefan NULL, false); 989fa0d2b9bSLi Zefan 990fa0d2b9bSLi Zefan kfree(vol_args); 991fa0d2b9bSLi Zefan return ret; 992fa0d2b9bSLi Zefan } 993fa0d2b9bSLi Zefan 994fa0d2b9bSLi Zefan static noinline int btrfs_ioctl_snap_create_v2(struct file *file, 995fa0d2b9bSLi Zefan void __user *arg, int subvol) 996fa0d2b9bSLi Zefan { 997fa0d2b9bSLi Zefan struct btrfs_ioctl_vol_args_v2 *vol_args; 998fa0d2b9bSLi Zefan int ret; 999fdfb1e4fSLi Zefan u64 transid = 0; 1000fdfb1e4fSLi Zefan u64 *ptr = NULL; 1001b83cc969SLi Zefan bool readonly = false; 100272fd032eSSage Weil 1003fa0d2b9bSLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1004fa0d2b9bSLi Zefan if (IS_ERR(vol_args)) 1005fa0d2b9bSLi Zefan return PTR_ERR(vol_args); 1006fa0d2b9bSLi Zefan vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 1007fdfb1e4fSLi Zefan 1008b83cc969SLi Zefan if (vol_args->flags & 1009b83cc969SLi Zefan ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { 1010b83cc969SLi Zefan ret = -EOPNOTSUPP; 1011fdfb1e4fSLi Zefan goto out; 1012fdfb1e4fSLi Zefan } 1013fdfb1e4fSLi Zefan 1014fa0d2b9bSLi Zefan if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) 1015fdfb1e4fSLi Zefan ptr = &transid; 1016b83cc969SLi Zefan if (vol_args->flags & BTRFS_SUBVOL_RDONLY) 1017b83cc969SLi Zefan readonly = true; 101875eaa0e2SSage Weil 1019fa0d2b9bSLi Zefan ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, 1020b83cc969SLi Zefan vol_args->fd, subvol, 1021b83cc969SLi Zefan ptr, readonly); 102275eaa0e2SSage Weil 1023fdfb1e4fSLi Zefan if (ret == 0 && ptr && 102475eaa0e2SSage Weil copy_to_user(arg + 1025fdfb1e4fSLi Zefan offsetof(struct btrfs_ioctl_vol_args_v2, 1026fdfb1e4fSLi Zefan transid), ptr, sizeof(*ptr))) 102775eaa0e2SSage Weil ret = -EFAULT; 1028fdfb1e4fSLi Zefan out: 1029f46b5a66SChristoph Hellwig kfree(vol_args); 1030f46b5a66SChristoph Hellwig return ret; 1031f46b5a66SChristoph Hellwig } 1032f46b5a66SChristoph Hellwig 10330caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_getflags(struct file *file, 10340caa102dSLi Zefan void __user *arg) 10350caa102dSLi Zefan { 10360caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 10370caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 10380caa102dSLi Zefan int ret = 0; 10390caa102dSLi Zefan u64 flags = 0; 10400caa102dSLi Zefan 10410caa102dSLi Zefan if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) 10420caa102dSLi Zefan return -EINVAL; 10430caa102dSLi Zefan 10440caa102dSLi Zefan down_read(&root->fs_info->subvol_sem); 10450caa102dSLi Zefan if (btrfs_root_readonly(root)) 10460caa102dSLi Zefan flags |= BTRFS_SUBVOL_RDONLY; 10470caa102dSLi Zefan up_read(&root->fs_info->subvol_sem); 10480caa102dSLi Zefan 10490caa102dSLi Zefan if (copy_to_user(arg, &flags, sizeof(flags))) 10500caa102dSLi Zefan ret = -EFAULT; 10510caa102dSLi Zefan 10520caa102dSLi Zefan return ret; 10530caa102dSLi Zefan } 10540caa102dSLi Zefan 10550caa102dSLi Zefan static noinline int btrfs_ioctl_subvol_setflags(struct file *file, 10560caa102dSLi Zefan void __user *arg) 10570caa102dSLi Zefan { 10580caa102dSLi Zefan struct inode *inode = fdentry(file)->d_inode; 10590caa102dSLi Zefan struct btrfs_root *root = BTRFS_I(inode)->root; 10600caa102dSLi Zefan struct btrfs_trans_handle *trans; 10610caa102dSLi Zefan u64 root_flags; 10620caa102dSLi Zefan u64 flags; 10630caa102dSLi Zefan int ret = 0; 10640caa102dSLi Zefan 10650caa102dSLi Zefan if (root->fs_info->sb->s_flags & MS_RDONLY) 10660caa102dSLi Zefan return -EROFS; 10670caa102dSLi Zefan 10680caa102dSLi Zefan if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) 10690caa102dSLi Zefan return -EINVAL; 10700caa102dSLi Zefan 10710caa102dSLi Zefan if (copy_from_user(&flags, arg, sizeof(flags))) 10720caa102dSLi Zefan return -EFAULT; 10730caa102dSLi Zefan 1074b4dc2b8cSLi Zefan if (flags & BTRFS_SUBVOL_CREATE_ASYNC) 10750caa102dSLi Zefan return -EINVAL; 10760caa102dSLi Zefan 10770caa102dSLi Zefan if (flags & ~BTRFS_SUBVOL_RDONLY) 10780caa102dSLi Zefan return -EOPNOTSUPP; 10790caa102dSLi Zefan 1080*2e149670SSerge E. Hallyn if (!inode_owner_or_capable(inode)) 1081b4dc2b8cSLi Zefan return -EACCES; 1082b4dc2b8cSLi Zefan 10830caa102dSLi Zefan down_write(&root->fs_info->subvol_sem); 10840caa102dSLi Zefan 10850caa102dSLi Zefan /* nothing to do */ 10860caa102dSLi Zefan if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 10870caa102dSLi Zefan goto out; 10880caa102dSLi Zefan 10890caa102dSLi Zefan root_flags = btrfs_root_flags(&root->root_item); 10900caa102dSLi Zefan if (flags & BTRFS_SUBVOL_RDONLY) 10910caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 10920caa102dSLi Zefan root_flags | BTRFS_ROOT_SUBVOL_RDONLY); 10930caa102dSLi Zefan else 10940caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, 10950caa102dSLi Zefan root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); 10960caa102dSLi Zefan 10970caa102dSLi Zefan trans = btrfs_start_transaction(root, 1); 10980caa102dSLi Zefan if (IS_ERR(trans)) { 10990caa102dSLi Zefan ret = PTR_ERR(trans); 11000caa102dSLi Zefan goto out_reset; 11010caa102dSLi Zefan } 11020caa102dSLi Zefan 1103b4dc2b8cSLi Zefan ret = btrfs_update_root(trans, root->fs_info->tree_root, 11040caa102dSLi Zefan &root->root_key, &root->root_item); 11050caa102dSLi Zefan 11060caa102dSLi Zefan btrfs_commit_transaction(trans, root); 11070caa102dSLi Zefan out_reset: 11080caa102dSLi Zefan if (ret) 11090caa102dSLi Zefan btrfs_set_root_flags(&root->root_item, root_flags); 11100caa102dSLi Zefan out: 11110caa102dSLi Zefan up_write(&root->fs_info->subvol_sem); 11120caa102dSLi Zefan return ret; 11130caa102dSLi Zefan } 11140caa102dSLi Zefan 111576dda93cSYan, Zheng /* 111676dda93cSYan, Zheng * helper to check if the subvolume references other subvolumes 111776dda93cSYan, Zheng */ 111876dda93cSYan, Zheng static noinline int may_destroy_subvol(struct btrfs_root *root) 111976dda93cSYan, Zheng { 112076dda93cSYan, Zheng struct btrfs_path *path; 112176dda93cSYan, Zheng struct btrfs_key key; 112276dda93cSYan, Zheng int ret; 112376dda93cSYan, Zheng 112476dda93cSYan, Zheng path = btrfs_alloc_path(); 112576dda93cSYan, Zheng if (!path) 112676dda93cSYan, Zheng return -ENOMEM; 112776dda93cSYan, Zheng 112876dda93cSYan, Zheng key.objectid = root->root_key.objectid; 112976dda93cSYan, Zheng key.type = BTRFS_ROOT_REF_KEY; 113076dda93cSYan, Zheng key.offset = (u64)-1; 113176dda93cSYan, Zheng 113276dda93cSYan, Zheng ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 113376dda93cSYan, Zheng &key, path, 0, 0); 113476dda93cSYan, Zheng if (ret < 0) 113576dda93cSYan, Zheng goto out; 113676dda93cSYan, Zheng BUG_ON(ret == 0); 113776dda93cSYan, Zheng 113876dda93cSYan, Zheng ret = 0; 113976dda93cSYan, Zheng if (path->slots[0] > 0) { 114076dda93cSYan, Zheng path->slots[0]--; 114176dda93cSYan, Zheng btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 114276dda93cSYan, Zheng if (key.objectid == root->root_key.objectid && 114376dda93cSYan, Zheng key.type == BTRFS_ROOT_REF_KEY) 114476dda93cSYan, Zheng ret = -ENOTEMPTY; 114576dda93cSYan, Zheng } 114676dda93cSYan, Zheng out: 114776dda93cSYan, Zheng btrfs_free_path(path); 114876dda93cSYan, Zheng return ret; 114976dda93cSYan, Zheng } 115076dda93cSYan, Zheng 1151ac8e9819SChris Mason static noinline int key_in_sk(struct btrfs_key *key, 1152ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk) 1153ac8e9819SChris Mason { 1154abc6e134SChris Mason struct btrfs_key test; 1155abc6e134SChris Mason int ret; 1156abc6e134SChris Mason 1157abc6e134SChris Mason test.objectid = sk->min_objectid; 1158abc6e134SChris Mason test.type = sk->min_type; 1159abc6e134SChris Mason test.offset = sk->min_offset; 1160abc6e134SChris Mason 1161abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1162abc6e134SChris Mason if (ret < 0) 1163ac8e9819SChris Mason return 0; 1164abc6e134SChris Mason 1165abc6e134SChris Mason test.objectid = sk->max_objectid; 1166abc6e134SChris Mason test.type = sk->max_type; 1167abc6e134SChris Mason test.offset = sk->max_offset; 1168abc6e134SChris Mason 1169abc6e134SChris Mason ret = btrfs_comp_cpu_keys(key, &test); 1170abc6e134SChris Mason if (ret > 0) 1171ac8e9819SChris Mason return 0; 1172ac8e9819SChris Mason return 1; 1173ac8e9819SChris Mason } 1174ac8e9819SChris Mason 1175ac8e9819SChris Mason static noinline int copy_to_sk(struct btrfs_root *root, 1176ac8e9819SChris Mason struct btrfs_path *path, 1177ac8e9819SChris Mason struct btrfs_key *key, 1178ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk, 1179ac8e9819SChris Mason char *buf, 1180ac8e9819SChris Mason unsigned long *sk_offset, 1181ac8e9819SChris Mason int *num_found) 1182ac8e9819SChris Mason { 1183ac8e9819SChris Mason u64 found_transid; 1184ac8e9819SChris Mason struct extent_buffer *leaf; 1185ac8e9819SChris Mason struct btrfs_ioctl_search_header sh; 1186ac8e9819SChris Mason unsigned long item_off; 1187ac8e9819SChris Mason unsigned long item_len; 1188ac8e9819SChris Mason int nritems; 1189ac8e9819SChris Mason int i; 1190ac8e9819SChris Mason int slot; 1191ac8e9819SChris Mason int found = 0; 1192ac8e9819SChris Mason int ret = 0; 1193ac8e9819SChris Mason 1194ac8e9819SChris Mason leaf = path->nodes[0]; 1195ac8e9819SChris Mason slot = path->slots[0]; 1196ac8e9819SChris Mason nritems = btrfs_header_nritems(leaf); 1197ac8e9819SChris Mason 1198ac8e9819SChris Mason if (btrfs_header_generation(leaf) > sk->max_transid) { 1199ac8e9819SChris Mason i = nritems; 1200ac8e9819SChris Mason goto advance_key; 1201ac8e9819SChris Mason } 1202ac8e9819SChris Mason found_transid = btrfs_header_generation(leaf); 1203ac8e9819SChris Mason 1204ac8e9819SChris Mason for (i = slot; i < nritems; i++) { 1205ac8e9819SChris Mason item_off = btrfs_item_ptr_offset(leaf, i); 1206ac8e9819SChris Mason item_len = btrfs_item_size_nr(leaf, i); 1207ac8e9819SChris Mason 1208ac8e9819SChris Mason if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 1209ac8e9819SChris Mason item_len = 0; 1210ac8e9819SChris Mason 1211ac8e9819SChris Mason if (sizeof(sh) + item_len + *sk_offset > 1212ac8e9819SChris Mason BTRFS_SEARCH_ARGS_BUFSIZE) { 1213ac8e9819SChris Mason ret = 1; 1214ac8e9819SChris Mason goto overflow; 1215ac8e9819SChris Mason } 1216ac8e9819SChris Mason 1217ac8e9819SChris Mason btrfs_item_key_to_cpu(leaf, key, i); 1218ac8e9819SChris Mason if (!key_in_sk(key, sk)) 1219ac8e9819SChris Mason continue; 1220ac8e9819SChris Mason 1221ac8e9819SChris Mason sh.objectid = key->objectid; 1222ac8e9819SChris Mason sh.offset = key->offset; 1223ac8e9819SChris Mason sh.type = key->type; 1224ac8e9819SChris Mason sh.len = item_len; 1225ac8e9819SChris Mason sh.transid = found_transid; 1226ac8e9819SChris Mason 1227ac8e9819SChris Mason /* copy search result header */ 1228ac8e9819SChris Mason memcpy(buf + *sk_offset, &sh, sizeof(sh)); 1229ac8e9819SChris Mason *sk_offset += sizeof(sh); 1230ac8e9819SChris Mason 1231ac8e9819SChris Mason if (item_len) { 1232ac8e9819SChris Mason char *p = buf + *sk_offset; 1233ac8e9819SChris Mason /* copy the item */ 1234ac8e9819SChris Mason read_extent_buffer(leaf, p, 1235ac8e9819SChris Mason item_off, item_len); 1236ac8e9819SChris Mason *sk_offset += item_len; 1237ac8e9819SChris Mason } 123890fdde14SChris Mason found++; 1239ac8e9819SChris Mason 1240ac8e9819SChris Mason if (*num_found >= sk->nr_items) 1241ac8e9819SChris Mason break; 1242ac8e9819SChris Mason } 1243ac8e9819SChris Mason advance_key: 1244ac8e9819SChris Mason ret = 0; 1245abc6e134SChris Mason if (key->offset < (u64)-1 && key->offset < sk->max_offset) 1246abc6e134SChris Mason key->offset++; 1247abc6e134SChris Mason else if (key->type < (u8)-1 && key->type < sk->max_type) { 1248abc6e134SChris Mason key->offset = 0; 1249abc6e134SChris Mason key->type++; 1250abc6e134SChris Mason } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 1251abc6e134SChris Mason key->offset = 0; 1252abc6e134SChris Mason key->type = 0; 1253abc6e134SChris Mason key->objectid++; 1254abc6e134SChris Mason } else 1255abc6e134SChris Mason ret = 1; 1256ac8e9819SChris Mason overflow: 1257ac8e9819SChris Mason *num_found += found; 1258ac8e9819SChris Mason return ret; 1259ac8e9819SChris Mason } 1260ac8e9819SChris Mason 1261ac8e9819SChris Mason static noinline int search_ioctl(struct inode *inode, 1262ac8e9819SChris Mason struct btrfs_ioctl_search_args *args) 1263ac8e9819SChris Mason { 1264ac8e9819SChris Mason struct btrfs_root *root; 1265ac8e9819SChris Mason struct btrfs_key key; 1266ac8e9819SChris Mason struct btrfs_key max_key; 1267ac8e9819SChris Mason struct btrfs_path *path; 1268ac8e9819SChris Mason struct btrfs_ioctl_search_key *sk = &args->key; 1269ac8e9819SChris Mason struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 1270ac8e9819SChris Mason int ret; 1271ac8e9819SChris Mason int num_found = 0; 1272ac8e9819SChris Mason unsigned long sk_offset = 0; 1273ac8e9819SChris Mason 1274ac8e9819SChris Mason path = btrfs_alloc_path(); 1275ac8e9819SChris Mason if (!path) 1276ac8e9819SChris Mason return -ENOMEM; 1277ac8e9819SChris Mason 1278ac8e9819SChris Mason if (sk->tree_id == 0) { 1279ac8e9819SChris Mason /* search the root of the inode that was passed */ 1280ac8e9819SChris Mason root = BTRFS_I(inode)->root; 1281ac8e9819SChris Mason } else { 1282ac8e9819SChris Mason key.objectid = sk->tree_id; 1283ac8e9819SChris Mason key.type = BTRFS_ROOT_ITEM_KEY; 1284ac8e9819SChris Mason key.offset = (u64)-1; 1285ac8e9819SChris Mason root = btrfs_read_fs_root_no_name(info, &key); 1286ac8e9819SChris Mason if (IS_ERR(root)) { 1287ac8e9819SChris Mason printk(KERN_ERR "could not find root %llu\n", 1288ac8e9819SChris Mason sk->tree_id); 1289ac8e9819SChris Mason btrfs_free_path(path); 1290ac8e9819SChris Mason return -ENOENT; 1291ac8e9819SChris Mason } 1292ac8e9819SChris Mason } 1293ac8e9819SChris Mason 1294ac8e9819SChris Mason key.objectid = sk->min_objectid; 1295ac8e9819SChris Mason key.type = sk->min_type; 1296ac8e9819SChris Mason key.offset = sk->min_offset; 1297ac8e9819SChris Mason 1298ac8e9819SChris Mason max_key.objectid = sk->max_objectid; 1299ac8e9819SChris Mason max_key.type = sk->max_type; 1300ac8e9819SChris Mason max_key.offset = sk->max_offset; 1301ac8e9819SChris Mason 1302ac8e9819SChris Mason path->keep_locks = 1; 1303ac8e9819SChris Mason 1304ac8e9819SChris Mason while(1) { 1305ac8e9819SChris Mason ret = btrfs_search_forward(root, &key, &max_key, path, 0, 1306ac8e9819SChris Mason sk->min_transid); 1307ac8e9819SChris Mason if (ret != 0) { 1308ac8e9819SChris Mason if (ret > 0) 1309ac8e9819SChris Mason ret = 0; 1310ac8e9819SChris Mason goto err; 1311ac8e9819SChris Mason } 1312ac8e9819SChris Mason ret = copy_to_sk(root, path, &key, sk, args->buf, 1313ac8e9819SChris Mason &sk_offset, &num_found); 1314ac8e9819SChris Mason btrfs_release_path(root, path); 1315ac8e9819SChris Mason if (ret || num_found >= sk->nr_items) 1316ac8e9819SChris Mason break; 1317ac8e9819SChris Mason 1318ac8e9819SChris Mason } 1319ac8e9819SChris Mason ret = 0; 1320ac8e9819SChris Mason err: 1321ac8e9819SChris Mason sk->nr_items = num_found; 1322ac8e9819SChris Mason btrfs_free_path(path); 1323ac8e9819SChris Mason return ret; 1324ac8e9819SChris Mason } 1325ac8e9819SChris Mason 1326ac8e9819SChris Mason static noinline int btrfs_ioctl_tree_search(struct file *file, 1327ac8e9819SChris Mason void __user *argp) 1328ac8e9819SChris Mason { 1329ac8e9819SChris Mason struct btrfs_ioctl_search_args *args; 1330ac8e9819SChris Mason struct inode *inode; 1331ac8e9819SChris Mason int ret; 1332ac8e9819SChris Mason 1333ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1334ac8e9819SChris Mason return -EPERM; 1335ac8e9819SChris Mason 13362354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 13372354d08fSJulia Lawall if (IS_ERR(args)) 13382354d08fSJulia Lawall return PTR_ERR(args); 1339ac8e9819SChris Mason 1340ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1341ac8e9819SChris Mason ret = search_ioctl(inode, args); 1342ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1343ac8e9819SChris Mason ret = -EFAULT; 1344ac8e9819SChris Mason kfree(args); 1345ac8e9819SChris Mason return ret; 1346ac8e9819SChris Mason } 1347ac8e9819SChris Mason 134898d377a0STARUISI Hiroaki /* 1349ac8e9819SChris Mason * Search INODE_REFs to identify path name of 'dirid' directory 1350ac8e9819SChris Mason * in a 'tree_id' tree. and sets path name to 'name'. 135198d377a0STARUISI Hiroaki */ 135298d377a0STARUISI Hiroaki static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 135398d377a0STARUISI Hiroaki u64 tree_id, u64 dirid, char *name) 135498d377a0STARUISI Hiroaki { 135598d377a0STARUISI Hiroaki struct btrfs_root *root; 135698d377a0STARUISI Hiroaki struct btrfs_key key; 1357ac8e9819SChris Mason char *ptr; 135898d377a0STARUISI Hiroaki int ret = -1; 135998d377a0STARUISI Hiroaki int slot; 136098d377a0STARUISI Hiroaki int len; 136198d377a0STARUISI Hiroaki int total_len = 0; 136298d377a0STARUISI Hiroaki struct btrfs_inode_ref *iref; 136398d377a0STARUISI Hiroaki struct extent_buffer *l; 136498d377a0STARUISI Hiroaki struct btrfs_path *path; 136598d377a0STARUISI Hiroaki 136698d377a0STARUISI Hiroaki if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 136798d377a0STARUISI Hiroaki name[0]='\0'; 136898d377a0STARUISI Hiroaki return 0; 136998d377a0STARUISI Hiroaki } 137098d377a0STARUISI Hiroaki 137198d377a0STARUISI Hiroaki path = btrfs_alloc_path(); 137298d377a0STARUISI Hiroaki if (!path) 137398d377a0STARUISI Hiroaki return -ENOMEM; 137498d377a0STARUISI Hiroaki 1375ac8e9819SChris Mason ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 137698d377a0STARUISI Hiroaki 137798d377a0STARUISI Hiroaki key.objectid = tree_id; 137898d377a0STARUISI Hiroaki key.type = BTRFS_ROOT_ITEM_KEY; 137998d377a0STARUISI Hiroaki key.offset = (u64)-1; 138098d377a0STARUISI Hiroaki root = btrfs_read_fs_root_no_name(info, &key); 138198d377a0STARUISI Hiroaki if (IS_ERR(root)) { 138298d377a0STARUISI Hiroaki printk(KERN_ERR "could not find root %llu\n", tree_id); 13838ad6fcabSChris Mason ret = -ENOENT; 13848ad6fcabSChris Mason goto out; 138598d377a0STARUISI Hiroaki } 138698d377a0STARUISI Hiroaki 138798d377a0STARUISI Hiroaki key.objectid = dirid; 138898d377a0STARUISI Hiroaki key.type = BTRFS_INODE_REF_KEY; 13898ad6fcabSChris Mason key.offset = (u64)-1; 139098d377a0STARUISI Hiroaki 139198d377a0STARUISI Hiroaki while(1) { 139298d377a0STARUISI Hiroaki ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 139398d377a0STARUISI Hiroaki if (ret < 0) 139498d377a0STARUISI Hiroaki goto out; 139598d377a0STARUISI Hiroaki 139698d377a0STARUISI Hiroaki l = path->nodes[0]; 139798d377a0STARUISI Hiroaki slot = path->slots[0]; 13988ad6fcabSChris Mason if (ret > 0 && slot > 0) 13998ad6fcabSChris Mason slot--; 140098d377a0STARUISI Hiroaki btrfs_item_key_to_cpu(l, &key, slot); 140198d377a0STARUISI Hiroaki 140298d377a0STARUISI Hiroaki if (ret > 0 && (key.objectid != dirid || 1403ac8e9819SChris Mason key.type != BTRFS_INODE_REF_KEY)) { 1404ac8e9819SChris Mason ret = -ENOENT; 140598d377a0STARUISI Hiroaki goto out; 1406ac8e9819SChris Mason } 140798d377a0STARUISI Hiroaki 140898d377a0STARUISI Hiroaki iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 140998d377a0STARUISI Hiroaki len = btrfs_inode_ref_name_len(l, iref); 141098d377a0STARUISI Hiroaki ptr -= len + 1; 141198d377a0STARUISI Hiroaki total_len += len + 1; 1412ac8e9819SChris Mason if (ptr < name) 141398d377a0STARUISI Hiroaki goto out; 141498d377a0STARUISI Hiroaki 141598d377a0STARUISI Hiroaki *(ptr + len) = '/'; 141698d377a0STARUISI Hiroaki read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 141798d377a0STARUISI Hiroaki 141898d377a0STARUISI Hiroaki if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 141998d377a0STARUISI Hiroaki break; 142098d377a0STARUISI Hiroaki 142198d377a0STARUISI Hiroaki btrfs_release_path(root, path); 142298d377a0STARUISI Hiroaki key.objectid = key.offset; 14238ad6fcabSChris Mason key.offset = (u64)-1; 142498d377a0STARUISI Hiroaki dirid = key.objectid; 142598d377a0STARUISI Hiroaki 142698d377a0STARUISI Hiroaki } 1427ac8e9819SChris Mason if (ptr < name) 142898d377a0STARUISI Hiroaki goto out; 1429ac8e9819SChris Mason memcpy(name, ptr, total_len); 143098d377a0STARUISI Hiroaki name[total_len]='\0'; 143198d377a0STARUISI Hiroaki ret = 0; 143298d377a0STARUISI Hiroaki out: 143398d377a0STARUISI Hiroaki btrfs_free_path(path); 1434ac8e9819SChris Mason return ret; 1435ac8e9819SChris Mason } 1436ac8e9819SChris Mason 1437ac8e9819SChris Mason static noinline int btrfs_ioctl_ino_lookup(struct file *file, 1438ac8e9819SChris Mason void __user *argp) 1439ac8e9819SChris Mason { 1440ac8e9819SChris Mason struct btrfs_ioctl_ino_lookup_args *args; 1441ac8e9819SChris Mason struct inode *inode; 1442ac8e9819SChris Mason int ret; 1443ac8e9819SChris Mason 1444ac8e9819SChris Mason if (!capable(CAP_SYS_ADMIN)) 1445ac8e9819SChris Mason return -EPERM; 1446ac8e9819SChris Mason 14472354d08fSJulia Lawall args = memdup_user(argp, sizeof(*args)); 14482354d08fSJulia Lawall if (IS_ERR(args)) 14492354d08fSJulia Lawall return PTR_ERR(args); 1450c2b96929SDan Carpenter 1451ac8e9819SChris Mason inode = fdentry(file)->d_inode; 1452ac8e9819SChris Mason 14531b53ac4dSChris Mason if (args->treeid == 0) 14541b53ac4dSChris Mason args->treeid = BTRFS_I(inode)->root->root_key.objectid; 14551b53ac4dSChris Mason 1456ac8e9819SChris Mason ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 1457ac8e9819SChris Mason args->treeid, args->objectid, 1458ac8e9819SChris Mason args->name); 1459ac8e9819SChris Mason 1460ac8e9819SChris Mason if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1461ac8e9819SChris Mason ret = -EFAULT; 1462ac8e9819SChris Mason 1463ac8e9819SChris Mason kfree(args); 146498d377a0STARUISI Hiroaki return ret; 146598d377a0STARUISI Hiroaki } 146698d377a0STARUISI Hiroaki 146776dda93cSYan, Zheng static noinline int btrfs_ioctl_snap_destroy(struct file *file, 146876dda93cSYan, Zheng void __user *arg) 146976dda93cSYan, Zheng { 147076dda93cSYan, Zheng struct dentry *parent = fdentry(file); 147176dda93cSYan, Zheng struct dentry *dentry; 147276dda93cSYan, Zheng struct inode *dir = parent->d_inode; 147376dda93cSYan, Zheng struct inode *inode; 147476dda93cSYan, Zheng struct btrfs_root *root = BTRFS_I(dir)->root; 147576dda93cSYan, Zheng struct btrfs_root *dest = NULL; 147676dda93cSYan, Zheng struct btrfs_ioctl_vol_args *vol_args; 147776dda93cSYan, Zheng struct btrfs_trans_handle *trans; 147876dda93cSYan, Zheng int namelen; 147976dda93cSYan, Zheng int ret; 148076dda93cSYan, Zheng int err = 0; 148176dda93cSYan, Zheng 148276dda93cSYan, Zheng vol_args = memdup_user(arg, sizeof(*vol_args)); 148376dda93cSYan, Zheng if (IS_ERR(vol_args)) 148476dda93cSYan, Zheng return PTR_ERR(vol_args); 148576dda93cSYan, Zheng 148676dda93cSYan, Zheng vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 148776dda93cSYan, Zheng namelen = strlen(vol_args->name); 148876dda93cSYan, Zheng if (strchr(vol_args->name, '/') || 148976dda93cSYan, Zheng strncmp(vol_args->name, "..", namelen) == 0) { 149076dda93cSYan, Zheng err = -EINVAL; 149176dda93cSYan, Zheng goto out; 149276dda93cSYan, Zheng } 149376dda93cSYan, Zheng 149476dda93cSYan, Zheng err = mnt_want_write(file->f_path.mnt); 149576dda93cSYan, Zheng if (err) 149676dda93cSYan, Zheng goto out; 149776dda93cSYan, Zheng 149876dda93cSYan, Zheng mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); 149976dda93cSYan, Zheng dentry = lookup_one_len(vol_args->name, parent, namelen); 150076dda93cSYan, Zheng if (IS_ERR(dentry)) { 150176dda93cSYan, Zheng err = PTR_ERR(dentry); 150276dda93cSYan, Zheng goto out_unlock_dir; 150376dda93cSYan, Zheng } 150476dda93cSYan, Zheng 150576dda93cSYan, Zheng if (!dentry->d_inode) { 150676dda93cSYan, Zheng err = -ENOENT; 150776dda93cSYan, Zheng goto out_dput; 150876dda93cSYan, Zheng } 150976dda93cSYan, Zheng 151076dda93cSYan, Zheng inode = dentry->d_inode; 15114260f7c7SSage Weil dest = BTRFS_I(inode)->root; 15124260f7c7SSage Weil if (!capable(CAP_SYS_ADMIN)){ 15134260f7c7SSage Weil /* 15144260f7c7SSage Weil * Regular user. Only allow this with a special mount 15154260f7c7SSage Weil * option, when the user has write+exec access to the 15164260f7c7SSage Weil * subvol root, and when rmdir(2) would have been 15174260f7c7SSage Weil * allowed. 15184260f7c7SSage Weil * 15194260f7c7SSage Weil * Note that this is _not_ check that the subvol is 15204260f7c7SSage Weil * empty or doesn't contain data that we wouldn't 15214260f7c7SSage Weil * otherwise be able to delete. 15224260f7c7SSage Weil * 15234260f7c7SSage Weil * Users who want to delete empty subvols should try 15244260f7c7SSage Weil * rmdir(2). 15254260f7c7SSage Weil */ 15264260f7c7SSage Weil err = -EPERM; 15274260f7c7SSage Weil if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 15284260f7c7SSage Weil goto out_dput; 15294260f7c7SSage Weil 15304260f7c7SSage Weil /* 15314260f7c7SSage Weil * Do not allow deletion if the parent dir is the same 15324260f7c7SSage Weil * as the dir to be deleted. That means the ioctl 15334260f7c7SSage Weil * must be called on the dentry referencing the root 15344260f7c7SSage Weil * of the subvol, not a random directory contained 15354260f7c7SSage Weil * within it. 15364260f7c7SSage Weil */ 15374260f7c7SSage Weil err = -EINVAL; 15384260f7c7SSage Weil if (root == dest) 15394260f7c7SSage Weil goto out_dput; 15404260f7c7SSage Weil 15414260f7c7SSage Weil err = inode_permission(inode, MAY_WRITE | MAY_EXEC); 15424260f7c7SSage Weil if (err) 15434260f7c7SSage Weil goto out_dput; 15444260f7c7SSage Weil 15454260f7c7SSage Weil /* check if subvolume may be deleted by a non-root user */ 15464260f7c7SSage Weil err = btrfs_may_delete(dir, dentry, 1); 15474260f7c7SSage Weil if (err) 15484260f7c7SSage Weil goto out_dput; 15494260f7c7SSage Weil } 15504260f7c7SSage Weil 155176dda93cSYan, Zheng if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { 155276dda93cSYan, Zheng err = -EINVAL; 155376dda93cSYan, Zheng goto out_dput; 155476dda93cSYan, Zheng } 155576dda93cSYan, Zheng 155676dda93cSYan, Zheng mutex_lock(&inode->i_mutex); 155776dda93cSYan, Zheng err = d_invalidate(dentry); 155876dda93cSYan, Zheng if (err) 155976dda93cSYan, Zheng goto out_unlock; 156076dda93cSYan, Zheng 156176dda93cSYan, Zheng down_write(&root->fs_info->subvol_sem); 156276dda93cSYan, Zheng 156376dda93cSYan, Zheng err = may_destroy_subvol(dest); 156476dda93cSYan, Zheng if (err) 156576dda93cSYan, Zheng goto out_up_write; 156676dda93cSYan, Zheng 1567a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 0); 1568a22285a6SYan, Zheng if (IS_ERR(trans)) { 1569a22285a6SYan, Zheng err = PTR_ERR(trans); 1570d327099aSDan Carpenter goto out_up_write; 1571a22285a6SYan, Zheng } 1572a22285a6SYan, Zheng trans->block_rsv = &root->fs_info->global_block_rsv; 1573a22285a6SYan, Zheng 157476dda93cSYan, Zheng ret = btrfs_unlink_subvol(trans, root, dir, 157576dda93cSYan, Zheng dest->root_key.objectid, 157676dda93cSYan, Zheng dentry->d_name.name, 157776dda93cSYan, Zheng dentry->d_name.len); 157876dda93cSYan, Zheng BUG_ON(ret); 157976dda93cSYan, Zheng 158076dda93cSYan, Zheng btrfs_record_root_in_trans(trans, dest); 158176dda93cSYan, Zheng 158276dda93cSYan, Zheng memset(&dest->root_item.drop_progress, 0, 158376dda93cSYan, Zheng sizeof(dest->root_item.drop_progress)); 158476dda93cSYan, Zheng dest->root_item.drop_level = 0; 158576dda93cSYan, Zheng btrfs_set_root_refs(&dest->root_item, 0); 158676dda93cSYan, Zheng 1587d68fc57bSYan, Zheng if (!xchg(&dest->orphan_item_inserted, 1)) { 158876dda93cSYan, Zheng ret = btrfs_insert_orphan_item(trans, 158976dda93cSYan, Zheng root->fs_info->tree_root, 159076dda93cSYan, Zheng dest->root_key.objectid); 159176dda93cSYan, Zheng BUG_ON(ret); 1592d68fc57bSYan, Zheng } 159376dda93cSYan, Zheng 1594531cb13fSSage Weil ret = btrfs_end_transaction(trans, root); 159576dda93cSYan, Zheng BUG_ON(ret); 159676dda93cSYan, Zheng inode->i_flags |= S_DEAD; 159776dda93cSYan, Zheng out_up_write: 159876dda93cSYan, Zheng up_write(&root->fs_info->subvol_sem); 159976dda93cSYan, Zheng out_unlock: 160076dda93cSYan, Zheng mutex_unlock(&inode->i_mutex); 160176dda93cSYan, Zheng if (!err) { 1602efefb143SYan, Zheng shrink_dcache_sb(root->fs_info->sb); 160376dda93cSYan, Zheng btrfs_invalidate_inodes(dest); 160476dda93cSYan, Zheng d_delete(dentry); 160576dda93cSYan, Zheng } 160676dda93cSYan, Zheng out_dput: 160776dda93cSYan, Zheng dput(dentry); 160876dda93cSYan, Zheng out_unlock_dir: 160976dda93cSYan, Zheng mutex_unlock(&dir->i_mutex); 161076dda93cSYan, Zheng mnt_drop_write(file->f_path.mnt); 161176dda93cSYan, Zheng out: 161276dda93cSYan, Zheng kfree(vol_args); 161376dda93cSYan, Zheng return err; 161476dda93cSYan, Zheng } 161576dda93cSYan, Zheng 16161e701a32SChris Mason static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 1617f46b5a66SChristoph Hellwig { 1618f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 1619f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 16201e701a32SChris Mason struct btrfs_ioctl_defrag_range_args *range; 1621c146afadSYan Zheng int ret; 1622c146afadSYan Zheng 1623b83cc969SLi Zefan if (btrfs_root_readonly(root)) 1624b83cc969SLi Zefan return -EROFS; 1625b83cc969SLi Zefan 1626c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 1627c146afadSYan Zheng if (ret) 1628c146afadSYan Zheng return ret; 1629f46b5a66SChristoph Hellwig 1630f46b5a66SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1631f46b5a66SChristoph Hellwig case S_IFDIR: 1632e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) { 1633e441d54dSChris Mason ret = -EPERM; 1634e441d54dSChris Mason goto out; 1635e441d54dSChris Mason } 16368929ecfaSYan, Zheng ret = btrfs_defrag_root(root, 0); 16378929ecfaSYan, Zheng if (ret) 16388929ecfaSYan, Zheng goto out; 16398929ecfaSYan, Zheng ret = btrfs_defrag_root(root->fs_info->extent_root, 0); 1640f46b5a66SChristoph Hellwig break; 1641f46b5a66SChristoph Hellwig case S_IFREG: 1642e441d54dSChris Mason if (!(file->f_mode & FMODE_WRITE)) { 1643e441d54dSChris Mason ret = -EINVAL; 1644e441d54dSChris Mason goto out; 1645e441d54dSChris Mason } 16461e701a32SChris Mason 16471e701a32SChris Mason range = kzalloc(sizeof(*range), GFP_KERNEL); 16481e701a32SChris Mason if (!range) { 16491e701a32SChris Mason ret = -ENOMEM; 16501e701a32SChris Mason goto out; 16511e701a32SChris Mason } 16521e701a32SChris Mason 16531e701a32SChris Mason if (argp) { 16541e701a32SChris Mason if (copy_from_user(range, argp, 16551e701a32SChris Mason sizeof(*range))) { 16561e701a32SChris Mason ret = -EFAULT; 16571e701a32SChris Mason kfree(range); 1658683be16eSDan Carpenter goto out; 16591e701a32SChris Mason } 16601e701a32SChris Mason /* compression requires us to start the IO */ 16611e701a32SChris Mason if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 16621e701a32SChris Mason range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 16631e701a32SChris Mason range->extent_thresh = (u32)-1; 16641e701a32SChris Mason } 16651e701a32SChris Mason } else { 16661e701a32SChris Mason /* the rest are all set to zero by kzalloc */ 16671e701a32SChris Mason range->len = (u64)-1; 16681e701a32SChris Mason } 16698929ecfaSYan, Zheng ret = btrfs_defrag_file(file, range); 16701e701a32SChris Mason kfree(range); 1671f46b5a66SChristoph Hellwig break; 16728929ecfaSYan, Zheng default: 16738929ecfaSYan, Zheng ret = -EINVAL; 1674f46b5a66SChristoph Hellwig } 1675e441d54dSChris Mason out: 1676ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 1677e441d54dSChris Mason return ret; 1678f46b5a66SChristoph Hellwig } 1679f46b5a66SChristoph Hellwig 1680b2950863SChristoph Hellwig static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 1681f46b5a66SChristoph Hellwig { 1682f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1683f46b5a66SChristoph Hellwig int ret; 1684f46b5a66SChristoph Hellwig 1685e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1686e441d54dSChris Mason return -EPERM; 1687e441d54dSChris Mason 1688dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1689dae7b665SLi Zefan if (IS_ERR(vol_args)) 1690dae7b665SLi Zefan return PTR_ERR(vol_args); 1691f46b5a66SChristoph Hellwig 16925516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1693f46b5a66SChristoph Hellwig ret = btrfs_init_new_device(root, vol_args->name); 1694f46b5a66SChristoph Hellwig 1695f46b5a66SChristoph Hellwig kfree(vol_args); 1696f46b5a66SChristoph Hellwig return ret; 1697f46b5a66SChristoph Hellwig } 1698f46b5a66SChristoph Hellwig 1699b2950863SChristoph Hellwig static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) 1700f46b5a66SChristoph Hellwig { 1701f46b5a66SChristoph Hellwig struct btrfs_ioctl_vol_args *vol_args; 1702f46b5a66SChristoph Hellwig int ret; 1703f46b5a66SChristoph Hellwig 1704e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1705e441d54dSChris Mason return -EPERM; 1706e441d54dSChris Mason 1707c146afadSYan Zheng if (root->fs_info->sb->s_flags & MS_RDONLY) 1708c146afadSYan Zheng return -EROFS; 1709c146afadSYan Zheng 1710dae7b665SLi Zefan vol_args = memdup_user(arg, sizeof(*vol_args)); 1711dae7b665SLi Zefan if (IS_ERR(vol_args)) 1712dae7b665SLi Zefan return PTR_ERR(vol_args); 1713f46b5a66SChristoph Hellwig 17145516e595SMark Fasheh vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 1715f46b5a66SChristoph Hellwig ret = btrfs_rm_device(root, vol_args->name); 1716f46b5a66SChristoph Hellwig 1717f46b5a66SChristoph Hellwig kfree(vol_args); 1718f46b5a66SChristoph Hellwig return ret; 1719f46b5a66SChristoph Hellwig } 1720f46b5a66SChristoph Hellwig 172176dda93cSYan, Zheng static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, 1722b2950863SChristoph Hellwig u64 off, u64 olen, u64 destoff) 1723f46b5a66SChristoph Hellwig { 1724f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 1725f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 1726f46b5a66SChristoph Hellwig struct file *src_file; 1727f46b5a66SChristoph Hellwig struct inode *src; 1728f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 1729f46b5a66SChristoph Hellwig struct btrfs_path *path; 1730f46b5a66SChristoph Hellwig struct extent_buffer *leaf; 1731ae01a0abSYan Zheng char *buf; 1732ae01a0abSYan Zheng struct btrfs_key key; 1733f46b5a66SChristoph Hellwig u32 nritems; 1734f46b5a66SChristoph Hellwig int slot; 1735ae01a0abSYan Zheng int ret; 1736c5c9cd4dSSage Weil u64 len = olen; 1737c5c9cd4dSSage Weil u64 bs = root->fs_info->sb->s_blocksize; 1738c5c9cd4dSSage Weil u64 hint_byte; 1739d20f7043SChris Mason 1740c5c9cd4dSSage Weil /* 1741c5c9cd4dSSage Weil * TODO: 1742c5c9cd4dSSage Weil * - split compressed inline extents. annoying: we need to 1743c5c9cd4dSSage Weil * decompress into destination's address_space (the file offset 1744c5c9cd4dSSage Weil * may change, so source mapping won't do), then recompress (or 1745c5c9cd4dSSage Weil * otherwise reinsert) a subrange. 1746c5c9cd4dSSage Weil * - allow ranges within the same file to be cloned (provided 1747c5c9cd4dSSage Weil * they don't overlap)? 1748c5c9cd4dSSage Weil */ 1749c5c9cd4dSSage Weil 1750e441d54dSChris Mason /* the destination must be opened for writing */ 17512ebc3464SDan Rosenberg if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) 1752e441d54dSChris Mason return -EINVAL; 1753e441d54dSChris Mason 1754b83cc969SLi Zefan if (btrfs_root_readonly(root)) 1755b83cc969SLi Zefan return -EROFS; 1756b83cc969SLi Zefan 1757c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 1758c146afadSYan Zheng if (ret) 1759c146afadSYan Zheng return ret; 1760c146afadSYan Zheng 1761c5c9cd4dSSage Weil src_file = fget(srcfd); 1762ab67b7c1SYan Zheng if (!src_file) { 1763ab67b7c1SYan Zheng ret = -EBADF; 1764ab67b7c1SYan Zheng goto out_drop_write; 1765ab67b7c1SYan Zheng } 17665dc64164SDan Rosenberg 1767f46b5a66SChristoph Hellwig src = src_file->f_dentry->d_inode; 1768f46b5a66SChristoph Hellwig 1769c5c9cd4dSSage Weil ret = -EINVAL; 1770c5c9cd4dSSage Weil if (src == inode) 1771c5c9cd4dSSage Weil goto out_fput; 1772c5c9cd4dSSage Weil 17735dc64164SDan Rosenberg /* the src must be open for reading */ 17745dc64164SDan Rosenberg if (!(src_file->f_mode & FMODE_READ)) 17755dc64164SDan Rosenberg goto out_fput; 17765dc64164SDan Rosenberg 1777ae01a0abSYan Zheng ret = -EISDIR; 1778ae01a0abSYan Zheng if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) 1779f46b5a66SChristoph Hellwig goto out_fput; 1780f46b5a66SChristoph Hellwig 1781ae01a0abSYan Zheng ret = -EXDEV; 1782ae01a0abSYan Zheng if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) 1783ae01a0abSYan Zheng goto out_fput; 1784ae01a0abSYan Zheng 1785ae01a0abSYan Zheng ret = -ENOMEM; 1786ae01a0abSYan Zheng buf = vmalloc(btrfs_level_size(root, 0)); 1787ae01a0abSYan Zheng if (!buf) 1788ae01a0abSYan Zheng goto out_fput; 1789ae01a0abSYan Zheng 1790ae01a0abSYan Zheng path = btrfs_alloc_path(); 1791ae01a0abSYan Zheng if (!path) { 1792ae01a0abSYan Zheng vfree(buf); 1793ae01a0abSYan Zheng goto out_fput; 1794ae01a0abSYan Zheng } 1795ae01a0abSYan Zheng path->reada = 2; 1796ae01a0abSYan Zheng 1797f46b5a66SChristoph Hellwig if (inode < src) { 1798fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); 1799fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); 1800f46b5a66SChristoph Hellwig } else { 1801fccdae43SSage Weil mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); 1802fccdae43SSage Weil mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); 1803f46b5a66SChristoph Hellwig } 1804f46b5a66SChristoph Hellwig 1805c5c9cd4dSSage Weil /* determine range to clone */ 1806c5c9cd4dSSage Weil ret = -EINVAL; 18072ebc3464SDan Rosenberg if (off + len > src->i_size || off + len < off) 1808f46b5a66SChristoph Hellwig goto out_unlock; 1809c5c9cd4dSSage Weil if (len == 0) 1810c5c9cd4dSSage Weil olen = len = src->i_size - off; 1811c5c9cd4dSSage Weil /* if we extend to eof, continue to block boundary */ 1812c5c9cd4dSSage Weil if (off + len == src->i_size) 18132a6b8daeSLi Zefan len = ALIGN(src->i_size, bs) - off; 1814c5c9cd4dSSage Weil 1815c5c9cd4dSSage Weil /* verify the end result is block aligned */ 18162a6b8daeSLi Zefan if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || 18172a6b8daeSLi Zefan !IS_ALIGNED(destoff, bs)) 1818c5c9cd4dSSage Weil goto out_unlock; 1819c5c9cd4dSSage Weil 1820f46b5a66SChristoph Hellwig /* do any pending delalloc/csum calc on src, one way or 1821f46b5a66SChristoph Hellwig another, and lock file content */ 1822f46b5a66SChristoph Hellwig while (1) { 182331840ae1SZheng Yan struct btrfs_ordered_extent *ordered; 1824c5c9cd4dSSage Weil lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 18259a019196SSage Weil ordered = btrfs_lookup_first_ordered_extent(src, off+len); 18269a019196SSage Weil if (!ordered && 18279a019196SSage Weil !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, 18289a019196SSage Weil EXTENT_DELALLOC, 0, NULL)) 1829f46b5a66SChristoph Hellwig break; 1830c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 1831ae01a0abSYan Zheng if (ordered) 1832ae01a0abSYan Zheng btrfs_put_ordered_extent(ordered); 18339a019196SSage Weil btrfs_wait_ordered_range(src, off, len); 1834f46b5a66SChristoph Hellwig } 1835f46b5a66SChristoph Hellwig 1836c5c9cd4dSSage Weil /* clone data */ 1837f46b5a66SChristoph Hellwig key.objectid = src->i_ino; 1838ae01a0abSYan Zheng key.type = BTRFS_EXTENT_DATA_KEY; 1839ae01a0abSYan Zheng key.offset = 0; 1840f46b5a66SChristoph Hellwig 1841f46b5a66SChristoph Hellwig while (1) { 1842f46b5a66SChristoph Hellwig /* 1843f46b5a66SChristoph Hellwig * note the key will change type as we walk through the 1844f46b5a66SChristoph Hellwig * tree. 1845f46b5a66SChristoph Hellwig */ 1846a22285a6SYan, Zheng ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 1847f46b5a66SChristoph Hellwig if (ret < 0) 1848f46b5a66SChristoph Hellwig goto out; 1849f46b5a66SChristoph Hellwig 1850ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 1851ae01a0abSYan Zheng if (path->slots[0] >= nritems) { 1852f46b5a66SChristoph Hellwig ret = btrfs_next_leaf(root, path); 1853f46b5a66SChristoph Hellwig if (ret < 0) 1854f46b5a66SChristoph Hellwig goto out; 1855f46b5a66SChristoph Hellwig if (ret > 0) 1856f46b5a66SChristoph Hellwig break; 1857ae01a0abSYan Zheng nritems = btrfs_header_nritems(path->nodes[0]); 1858f46b5a66SChristoph Hellwig } 1859f46b5a66SChristoph Hellwig leaf = path->nodes[0]; 1860f46b5a66SChristoph Hellwig slot = path->slots[0]; 1861f46b5a66SChristoph Hellwig 1862ae01a0abSYan Zheng btrfs_item_key_to_cpu(leaf, &key, slot); 1863d20f7043SChris Mason if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || 1864f46b5a66SChristoph Hellwig key.objectid != src->i_ino) 1865f46b5a66SChristoph Hellwig break; 1866f46b5a66SChristoph Hellwig 1867c5c9cd4dSSage Weil if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { 1868c5c9cd4dSSage Weil struct btrfs_file_extent_item *extent; 1869c5c9cd4dSSage Weil int type; 187031840ae1SZheng Yan u32 size; 187131840ae1SZheng Yan struct btrfs_key new_key; 1872c5c9cd4dSSage Weil u64 disko = 0, diskl = 0; 1873c5c9cd4dSSage Weil u64 datao = 0, datal = 0; 1874c5c9cd4dSSage Weil u8 comp; 1875b5384d48SSage Weil u64 endoff; 187631840ae1SZheng Yan 187731840ae1SZheng Yan size = btrfs_item_size_nr(leaf, slot); 187831840ae1SZheng Yan read_extent_buffer(leaf, buf, 187931840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 188031840ae1SZheng Yan size); 1881c5c9cd4dSSage Weil 1882c5c9cd4dSSage Weil extent = btrfs_item_ptr(leaf, slot, 1883c5c9cd4dSSage Weil struct btrfs_file_extent_item); 1884c5c9cd4dSSage Weil comp = btrfs_file_extent_compression(leaf, extent); 1885c5c9cd4dSSage Weil type = btrfs_file_extent_type(leaf, extent); 1886c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 1887c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 1888d397712bSChris Mason disko = btrfs_file_extent_disk_bytenr(leaf, 1889d397712bSChris Mason extent); 1890d397712bSChris Mason diskl = btrfs_file_extent_disk_num_bytes(leaf, 1891d397712bSChris Mason extent); 1892c5c9cd4dSSage Weil datao = btrfs_file_extent_offset(leaf, extent); 1893d397712bSChris Mason datal = btrfs_file_extent_num_bytes(leaf, 1894d397712bSChris Mason extent); 1895c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 1896c5c9cd4dSSage Weil /* take upper bound, may be compressed */ 1897c5c9cd4dSSage Weil datal = btrfs_file_extent_ram_bytes(leaf, 1898c5c9cd4dSSage Weil extent); 1899c5c9cd4dSSage Weil } 190031840ae1SZheng Yan btrfs_release_path(root, path); 190131840ae1SZheng Yan 1902050006a7SSage Weil if (key.offset + datal <= off || 1903c5c9cd4dSSage Weil key.offset >= off+len) 1904c5c9cd4dSSage Weil goto next; 1905c5c9cd4dSSage Weil 190631840ae1SZheng Yan memcpy(&new_key, &key, sizeof(new_key)); 190731840ae1SZheng Yan new_key.objectid = inode->i_ino; 19084d728ec7SLi Zefan if (off <= key.offset) 1909c5c9cd4dSSage Weil new_key.offset = key.offset + destoff - off; 19104d728ec7SLi Zefan else 19114d728ec7SLi Zefan new_key.offset = destoff; 1912c5c9cd4dSSage Weil 1913a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 1); 1914a22285a6SYan, Zheng if (IS_ERR(trans)) { 1915a22285a6SYan, Zheng ret = PTR_ERR(trans); 1916a22285a6SYan, Zheng goto out; 1917a22285a6SYan, Zheng } 1918a22285a6SYan, Zheng 1919c8a894d7SChris Mason if (type == BTRFS_FILE_EXTENT_REG || 1920c8a894d7SChris Mason type == BTRFS_FILE_EXTENT_PREALLOC) { 1921a22285a6SYan, Zheng if (off > key.offset) { 1922a22285a6SYan, Zheng datao += off - key.offset; 1923a22285a6SYan, Zheng datal -= off - key.offset; 1924a22285a6SYan, Zheng } 1925a22285a6SYan, Zheng 1926a22285a6SYan, Zheng if (key.offset + datal > off + len) 1927a22285a6SYan, Zheng datal = off + len - key.offset; 1928a22285a6SYan, Zheng 1929a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 1930a22285a6SYan, Zheng new_key.offset, 1931a22285a6SYan, Zheng new_key.offset + datal, 1932a22285a6SYan, Zheng &hint_byte, 1); 1933a22285a6SYan, Zheng BUG_ON(ret); 1934a22285a6SYan, Zheng 193531840ae1SZheng Yan ret = btrfs_insert_empty_item(trans, root, path, 193631840ae1SZheng Yan &new_key, size); 1937a22285a6SYan, Zheng BUG_ON(ret); 193831840ae1SZheng Yan 193931840ae1SZheng Yan leaf = path->nodes[0]; 194031840ae1SZheng Yan slot = path->slots[0]; 194131840ae1SZheng Yan write_extent_buffer(leaf, buf, 194231840ae1SZheng Yan btrfs_item_ptr_offset(leaf, slot), 194331840ae1SZheng Yan size); 1944ae01a0abSYan Zheng 1945f46b5a66SChristoph Hellwig extent = btrfs_item_ptr(leaf, slot, 1946f46b5a66SChristoph Hellwig struct btrfs_file_extent_item); 1947c5c9cd4dSSage Weil 1948c5c9cd4dSSage Weil /* disko == 0 means it's a hole */ 1949c5c9cd4dSSage Weil if (!disko) 1950c5c9cd4dSSage Weil datao = 0; 1951c5c9cd4dSSage Weil 1952c5c9cd4dSSage Weil btrfs_set_file_extent_offset(leaf, extent, 1953c5c9cd4dSSage Weil datao); 1954c5c9cd4dSSage Weil btrfs_set_file_extent_num_bytes(leaf, extent, 1955c5c9cd4dSSage Weil datal); 1956c5c9cd4dSSage Weil if (disko) { 1957c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 1958ae01a0abSYan Zheng ret = btrfs_inc_extent_ref(trans, root, 19595d4f98a2SYan Zheng disko, diskl, 0, 1960f46b5a66SChristoph Hellwig root->root_key.objectid, 19615d4f98a2SYan Zheng inode->i_ino, 19625d4f98a2SYan Zheng new_key.offset - datao); 1963ae01a0abSYan Zheng BUG_ON(ret); 1964f46b5a66SChristoph Hellwig } 1965c5c9cd4dSSage Weil } else if (type == BTRFS_FILE_EXTENT_INLINE) { 1966c5c9cd4dSSage Weil u64 skip = 0; 1967c5c9cd4dSSage Weil u64 trim = 0; 1968c5c9cd4dSSage Weil if (off > key.offset) { 1969c5c9cd4dSSage Weil skip = off - key.offset; 1970c5c9cd4dSSage Weil new_key.offset += skip; 197131840ae1SZheng Yan } 1972d397712bSChris Mason 1973c5c9cd4dSSage Weil if (key.offset + datal > off+len) 1974c5c9cd4dSSage Weil trim = key.offset + datal - (off+len); 1975d397712bSChris Mason 1976c5c9cd4dSSage Weil if (comp && (skip || trim)) { 1977c5c9cd4dSSage Weil ret = -EINVAL; 1978a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 1979c5c9cd4dSSage Weil goto out; 198031840ae1SZheng Yan } 1981c5c9cd4dSSage Weil size -= skip + trim; 1982c5c9cd4dSSage Weil datal -= skip + trim; 1983a22285a6SYan, Zheng 1984a22285a6SYan, Zheng ret = btrfs_drop_extents(trans, inode, 1985a22285a6SYan, Zheng new_key.offset, 1986a22285a6SYan, Zheng new_key.offset + datal, 1987a22285a6SYan, Zheng &hint_byte, 1); 1988a22285a6SYan, Zheng BUG_ON(ret); 1989a22285a6SYan, Zheng 1990c5c9cd4dSSage Weil ret = btrfs_insert_empty_item(trans, root, path, 1991c5c9cd4dSSage Weil &new_key, size); 1992a22285a6SYan, Zheng BUG_ON(ret); 1993c5c9cd4dSSage Weil 1994c5c9cd4dSSage Weil if (skip) { 1995d397712bSChris Mason u32 start = 1996d397712bSChris Mason btrfs_file_extent_calc_inline_size(0); 1997c5c9cd4dSSage Weil memmove(buf+start, buf+start+skip, 1998c5c9cd4dSSage Weil datal); 1999c5c9cd4dSSage Weil } 2000c5c9cd4dSSage Weil 2001c5c9cd4dSSage Weil leaf = path->nodes[0]; 2002c5c9cd4dSSage Weil slot = path->slots[0]; 2003c5c9cd4dSSage Weil write_extent_buffer(leaf, buf, 2004c5c9cd4dSSage Weil btrfs_item_ptr_offset(leaf, slot), 2005c5c9cd4dSSage Weil size); 2006c5c9cd4dSSage Weil inode_add_bytes(inode, datal); 2007c5c9cd4dSSage Weil } 2008c5c9cd4dSSage Weil 2009c5c9cd4dSSage Weil btrfs_mark_buffer_dirty(leaf); 2010a22285a6SYan, Zheng btrfs_release_path(root, path); 2011c5c9cd4dSSage Weil 2012a22285a6SYan, Zheng inode->i_mtime = inode->i_ctime = CURRENT_TIME; 2013b5384d48SSage Weil 2014b5384d48SSage Weil /* 2015b5384d48SSage Weil * we round up to the block size at eof when 2016b5384d48SSage Weil * determining which extents to clone above, 2017b5384d48SSage Weil * but shouldn't round up the file size 2018b5384d48SSage Weil */ 2019b5384d48SSage Weil endoff = new_key.offset + datal; 20205f3888ffSLi Zefan if (endoff > destoff+olen) 20215f3888ffSLi Zefan endoff = destoff+olen; 2022b5384d48SSage Weil if (endoff > inode->i_size) 2023b5384d48SSage Weil btrfs_i_size_write(inode, endoff); 2024b5384d48SSage Weil 2025a22285a6SYan, Zheng BTRFS_I(inode)->flags = BTRFS_I(src)->flags; 2026a22285a6SYan, Zheng ret = btrfs_update_inode(trans, root, inode); 2027a22285a6SYan, Zheng BUG_ON(ret); 2028a22285a6SYan, Zheng btrfs_end_transaction(trans, root); 2029a22285a6SYan, Zheng } 2030c5c9cd4dSSage Weil next: 203131840ae1SZheng Yan btrfs_release_path(root, path); 2032ae01a0abSYan Zheng key.offset++; 2033ae01a0abSYan Zheng } 2034f46b5a66SChristoph Hellwig ret = 0; 2035f46b5a66SChristoph Hellwig out: 2036ae01a0abSYan Zheng btrfs_release_path(root, path); 2037c5c9cd4dSSage Weil unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); 2038f46b5a66SChristoph Hellwig out_unlock: 2039f46b5a66SChristoph Hellwig mutex_unlock(&src->i_mutex); 2040f46b5a66SChristoph Hellwig mutex_unlock(&inode->i_mutex); 2041ae01a0abSYan Zheng vfree(buf); 2042ae01a0abSYan Zheng btrfs_free_path(path); 2043f46b5a66SChristoph Hellwig out_fput: 2044f46b5a66SChristoph Hellwig fput(src_file); 2045ab67b7c1SYan Zheng out_drop_write: 2046ab67b7c1SYan Zheng mnt_drop_write(file->f_path.mnt); 2047f46b5a66SChristoph Hellwig return ret; 2048f46b5a66SChristoph Hellwig } 2049f46b5a66SChristoph Hellwig 20507a865e8aSChristoph Hellwig static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) 2051c5c9cd4dSSage Weil { 2052c5c9cd4dSSage Weil struct btrfs_ioctl_clone_range_args args; 2053c5c9cd4dSSage Weil 20547a865e8aSChristoph Hellwig if (copy_from_user(&args, argp, sizeof(args))) 2055c5c9cd4dSSage Weil return -EFAULT; 2056c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, 2057c5c9cd4dSSage Weil args.src_length, args.dest_offset); 2058c5c9cd4dSSage Weil } 2059c5c9cd4dSSage Weil 2060f46b5a66SChristoph Hellwig /* 2061f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2062f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2063f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2064f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2065f46b5a66SChristoph Hellwig */ 2066b2950863SChristoph Hellwig static long btrfs_ioctl_trans_start(struct file *file) 2067f46b5a66SChristoph Hellwig { 2068f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2069f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2070f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 20711ab86aedSSage Weil int ret; 2072f46b5a66SChristoph Hellwig 20731ab86aedSSage Weil ret = -EPERM; 2074df5b5520SChristoph Hellwig if (!capable(CAP_SYS_ADMIN)) 2075f46b5a66SChristoph Hellwig goto out; 20761ab86aedSSage Weil 20771ab86aedSSage Weil ret = -EINPROGRESS; 20781ab86aedSSage Weil if (file->private_data) 20791ab86aedSSage Weil goto out; 20809ca9ee09SSage Weil 2081b83cc969SLi Zefan ret = -EROFS; 2082b83cc969SLi Zefan if (btrfs_root_readonly(root)) 2083b83cc969SLi Zefan goto out; 2084b83cc969SLi Zefan 2085c146afadSYan Zheng ret = mnt_want_write(file->f_path.mnt); 2086c146afadSYan Zheng if (ret) 2087c146afadSYan Zheng goto out; 2088c146afadSYan Zheng 20899ca9ee09SSage Weil mutex_lock(&root->fs_info->trans_mutex); 20909ca9ee09SSage Weil root->fs_info->open_ioctl_trans++; 20919ca9ee09SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 20929ca9ee09SSage Weil 2093f46b5a66SChristoph Hellwig ret = -ENOMEM; 20941ab86aedSSage Weil trans = btrfs_start_ioctl_transaction(root, 0); 2095abd30bb0STsutomu Itoh if (IS_ERR(trans)) 20961ab86aedSSage Weil goto out_drop; 20971ab86aedSSage Weil 20981ab86aedSSage Weil file->private_data = trans; 20991ab86aedSSage Weil return 0; 21001ab86aedSSage Weil 21011ab86aedSSage Weil out_drop: 21021ab86aedSSage Weil mutex_lock(&root->fs_info->trans_mutex); 21031ab86aedSSage Weil root->fs_info->open_ioctl_trans--; 21041ab86aedSSage Weil mutex_unlock(&root->fs_info->trans_mutex); 21051ab86aedSSage Weil mnt_drop_write(file->f_path.mnt); 2106f46b5a66SChristoph Hellwig out: 2107f46b5a66SChristoph Hellwig return ret; 2108f46b5a66SChristoph Hellwig } 2109f46b5a66SChristoph Hellwig 21106ef5ed0dSJosef Bacik static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 21116ef5ed0dSJosef Bacik { 21126ef5ed0dSJosef Bacik struct inode *inode = fdentry(file)->d_inode; 21136ef5ed0dSJosef Bacik struct btrfs_root *root = BTRFS_I(inode)->root; 21146ef5ed0dSJosef Bacik struct btrfs_root *new_root; 21156ef5ed0dSJosef Bacik struct btrfs_dir_item *di; 21166ef5ed0dSJosef Bacik struct btrfs_trans_handle *trans; 21176ef5ed0dSJosef Bacik struct btrfs_path *path; 21186ef5ed0dSJosef Bacik struct btrfs_key location; 21196ef5ed0dSJosef Bacik struct btrfs_disk_key disk_key; 21206ef5ed0dSJosef Bacik struct btrfs_super_block *disk_super; 21216ef5ed0dSJosef Bacik u64 features; 21226ef5ed0dSJosef Bacik u64 objectid = 0; 21236ef5ed0dSJosef Bacik u64 dir_id; 21246ef5ed0dSJosef Bacik 21256ef5ed0dSJosef Bacik if (!capable(CAP_SYS_ADMIN)) 21266ef5ed0dSJosef Bacik return -EPERM; 21276ef5ed0dSJosef Bacik 21286ef5ed0dSJosef Bacik if (copy_from_user(&objectid, argp, sizeof(objectid))) 21296ef5ed0dSJosef Bacik return -EFAULT; 21306ef5ed0dSJosef Bacik 21316ef5ed0dSJosef Bacik if (!objectid) 21326ef5ed0dSJosef Bacik objectid = root->root_key.objectid; 21336ef5ed0dSJosef Bacik 21346ef5ed0dSJosef Bacik location.objectid = objectid; 21356ef5ed0dSJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 21366ef5ed0dSJosef Bacik location.offset = (u64)-1; 21376ef5ed0dSJosef Bacik 21386ef5ed0dSJosef Bacik new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 21396ef5ed0dSJosef Bacik if (IS_ERR(new_root)) 21406ef5ed0dSJosef Bacik return PTR_ERR(new_root); 21416ef5ed0dSJosef Bacik 21426ef5ed0dSJosef Bacik if (btrfs_root_refs(&new_root->root_item) == 0) 21436ef5ed0dSJosef Bacik return -ENOENT; 21446ef5ed0dSJosef Bacik 21456ef5ed0dSJosef Bacik path = btrfs_alloc_path(); 21466ef5ed0dSJosef Bacik if (!path) 21476ef5ed0dSJosef Bacik return -ENOMEM; 21486ef5ed0dSJosef Bacik path->leave_spinning = 1; 21496ef5ed0dSJosef Bacik 21506ef5ed0dSJosef Bacik trans = btrfs_start_transaction(root, 1); 215198d5dc13STsutomu Itoh if (IS_ERR(trans)) { 21526ef5ed0dSJosef Bacik btrfs_free_path(path); 215398d5dc13STsutomu Itoh return PTR_ERR(trans); 21546ef5ed0dSJosef Bacik } 21556ef5ed0dSJosef Bacik 21566ef5ed0dSJosef Bacik dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); 21576ef5ed0dSJosef Bacik di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 21586ef5ed0dSJosef Bacik dir_id, "default", 7, 1); 2159cf1e99a4SDan Carpenter if (IS_ERR_OR_NULL(di)) { 21606ef5ed0dSJosef Bacik btrfs_free_path(path); 21616ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 21626ef5ed0dSJosef Bacik printk(KERN_ERR "Umm, you don't have the default dir item, " 21636ef5ed0dSJosef Bacik "this isn't going to work\n"); 21646ef5ed0dSJosef Bacik return -ENOENT; 21656ef5ed0dSJosef Bacik } 21666ef5ed0dSJosef Bacik 21676ef5ed0dSJosef Bacik btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 21686ef5ed0dSJosef Bacik btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 21696ef5ed0dSJosef Bacik btrfs_mark_buffer_dirty(path->nodes[0]); 21706ef5ed0dSJosef Bacik btrfs_free_path(path); 21716ef5ed0dSJosef Bacik 21726ef5ed0dSJosef Bacik disk_super = &root->fs_info->super_copy; 21736ef5ed0dSJosef Bacik features = btrfs_super_incompat_flags(disk_super); 21746ef5ed0dSJosef Bacik if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { 21756ef5ed0dSJosef Bacik features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; 21766ef5ed0dSJosef Bacik btrfs_set_super_incompat_flags(disk_super, features); 21776ef5ed0dSJosef Bacik } 21786ef5ed0dSJosef Bacik btrfs_end_transaction(trans, root); 21796ef5ed0dSJosef Bacik 21806ef5ed0dSJosef Bacik return 0; 21816ef5ed0dSJosef Bacik } 21826ef5ed0dSJosef Bacik 2183bf5fc093SJosef Bacik static void get_block_group_info(struct list_head *groups_list, 2184bf5fc093SJosef Bacik struct btrfs_ioctl_space_info *space) 2185bf5fc093SJosef Bacik { 2186bf5fc093SJosef Bacik struct btrfs_block_group_cache *block_group; 2187bf5fc093SJosef Bacik 2188bf5fc093SJosef Bacik space->total_bytes = 0; 2189bf5fc093SJosef Bacik space->used_bytes = 0; 2190bf5fc093SJosef Bacik space->flags = 0; 2191bf5fc093SJosef Bacik list_for_each_entry(block_group, groups_list, list) { 2192bf5fc093SJosef Bacik space->flags = block_group->flags; 2193bf5fc093SJosef Bacik space->total_bytes += block_group->key.offset; 2194bf5fc093SJosef Bacik space->used_bytes += 2195bf5fc093SJosef Bacik btrfs_block_group_used(&block_group->item); 2196bf5fc093SJosef Bacik } 2197bf5fc093SJosef Bacik } 2198bf5fc093SJosef Bacik 21991406e432SJosef Bacik long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 22001406e432SJosef Bacik { 22011406e432SJosef Bacik struct btrfs_ioctl_space_args space_args; 22021406e432SJosef Bacik struct btrfs_ioctl_space_info space; 22031406e432SJosef Bacik struct btrfs_ioctl_space_info *dest; 22047fde62bfSChris Mason struct btrfs_ioctl_space_info *dest_orig; 22057fde62bfSChris Mason struct btrfs_ioctl_space_info *user_dest; 22061406e432SJosef Bacik struct btrfs_space_info *info; 2207bf5fc093SJosef Bacik u64 types[] = {BTRFS_BLOCK_GROUP_DATA, 2208bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_SYSTEM, 2209bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_METADATA, 2210bf5fc093SJosef Bacik BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; 2211bf5fc093SJosef Bacik int num_types = 4; 22127fde62bfSChris Mason int alloc_size; 22131406e432SJosef Bacik int ret = 0; 221451788b1bSDan Rosenberg u64 slot_count = 0; 2215bf5fc093SJosef Bacik int i, c; 22161406e432SJosef Bacik 22171406e432SJosef Bacik if (copy_from_user(&space_args, 22181406e432SJosef Bacik (struct btrfs_ioctl_space_args __user *)arg, 22191406e432SJosef Bacik sizeof(space_args))) 22201406e432SJosef Bacik return -EFAULT; 22211406e432SJosef Bacik 2222bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2223bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2224bf5fc093SJosef Bacik 2225bf5fc093SJosef Bacik info = NULL; 22267fde62bfSChris Mason rcu_read_lock(); 2227bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2228bf5fc093SJosef Bacik list) { 2229bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2230bf5fc093SJosef Bacik info = tmp; 2231bf5fc093SJosef Bacik break; 2232bf5fc093SJosef Bacik } 2233bf5fc093SJosef Bacik } 22347fde62bfSChris Mason rcu_read_unlock(); 22351406e432SJosef Bacik 2236bf5fc093SJosef Bacik if (!info) 2237bf5fc093SJosef Bacik continue; 2238bf5fc093SJosef Bacik 2239bf5fc093SJosef Bacik down_read(&info->groups_sem); 2240bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2241bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) 2242bf5fc093SJosef Bacik slot_count++; 2243bf5fc093SJosef Bacik } 2244bf5fc093SJosef Bacik up_read(&info->groups_sem); 2245bf5fc093SJosef Bacik } 2246bf5fc093SJosef Bacik 22477fde62bfSChris Mason /* space_slots == 0 means they are asking for a count */ 22487fde62bfSChris Mason if (space_args.space_slots == 0) { 22497fde62bfSChris Mason space_args.total_spaces = slot_count; 22507fde62bfSChris Mason goto out; 22517fde62bfSChris Mason } 2252bf5fc093SJosef Bacik 225351788b1bSDan Rosenberg slot_count = min_t(u64, space_args.space_slots, slot_count); 2254bf5fc093SJosef Bacik 22557fde62bfSChris Mason alloc_size = sizeof(*dest) * slot_count; 2256bf5fc093SJosef Bacik 22577fde62bfSChris Mason /* we generally have at most 6 or so space infos, one for each raid 22587fde62bfSChris Mason * level. So, a whole page should be more than enough for everyone 22597fde62bfSChris Mason */ 22607fde62bfSChris Mason if (alloc_size > PAGE_CACHE_SIZE) 22617fde62bfSChris Mason return -ENOMEM; 22627fde62bfSChris Mason 22637fde62bfSChris Mason space_args.total_spaces = 0; 22647fde62bfSChris Mason dest = kmalloc(alloc_size, GFP_NOFS); 22657fde62bfSChris Mason if (!dest) 22667fde62bfSChris Mason return -ENOMEM; 22677fde62bfSChris Mason dest_orig = dest; 22687fde62bfSChris Mason 22697fde62bfSChris Mason /* now we have a buffer to copy into */ 2270bf5fc093SJosef Bacik for (i = 0; i < num_types; i++) { 2271bf5fc093SJosef Bacik struct btrfs_space_info *tmp; 2272bf5fc093SJosef Bacik 227351788b1bSDan Rosenberg if (!slot_count) 227451788b1bSDan Rosenberg break; 227551788b1bSDan Rosenberg 2276bf5fc093SJosef Bacik info = NULL; 22771406e432SJosef Bacik rcu_read_lock(); 2278bf5fc093SJosef Bacik list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 2279bf5fc093SJosef Bacik list) { 2280bf5fc093SJosef Bacik if (tmp->flags == types[i]) { 2281bf5fc093SJosef Bacik info = tmp; 22827fde62bfSChris Mason break; 2283bf5fc093SJosef Bacik } 2284bf5fc093SJosef Bacik } 2285bf5fc093SJosef Bacik rcu_read_unlock(); 22867fde62bfSChris Mason 2287bf5fc093SJosef Bacik if (!info) 2288bf5fc093SJosef Bacik continue; 2289bf5fc093SJosef Bacik down_read(&info->groups_sem); 2290bf5fc093SJosef Bacik for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { 2291bf5fc093SJosef Bacik if (!list_empty(&info->block_groups[c])) { 2292bf5fc093SJosef Bacik get_block_group_info(&info->block_groups[c], 2293bf5fc093SJosef Bacik &space); 22947fde62bfSChris Mason memcpy(dest, &space, sizeof(space)); 22951406e432SJosef Bacik dest++; 22961406e432SJosef Bacik space_args.total_spaces++; 229751788b1bSDan Rosenberg slot_count--; 22981406e432SJosef Bacik } 229951788b1bSDan Rosenberg if (!slot_count) 230051788b1bSDan Rosenberg break; 2301bf5fc093SJosef Bacik } 2302bf5fc093SJosef Bacik up_read(&info->groups_sem); 2303bf5fc093SJosef Bacik } 23041406e432SJosef Bacik 23057fde62bfSChris Mason user_dest = (struct btrfs_ioctl_space_info *) 23067fde62bfSChris Mason (arg + sizeof(struct btrfs_ioctl_space_args)); 23077fde62bfSChris Mason 23087fde62bfSChris Mason if (copy_to_user(user_dest, dest_orig, alloc_size)) 23097fde62bfSChris Mason ret = -EFAULT; 23107fde62bfSChris Mason 23117fde62bfSChris Mason kfree(dest_orig); 23127fde62bfSChris Mason out: 23137fde62bfSChris Mason if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 23141406e432SJosef Bacik ret = -EFAULT; 23151406e432SJosef Bacik 23161406e432SJosef Bacik return ret; 23171406e432SJosef Bacik } 23181406e432SJosef Bacik 2319f46b5a66SChristoph Hellwig /* 2320f46b5a66SChristoph Hellwig * there are many ways the trans_start and trans_end ioctls can lead 2321f46b5a66SChristoph Hellwig * to deadlocks. They should only be used by applications that 2322f46b5a66SChristoph Hellwig * basically own the machine, and have a very in depth understanding 2323f46b5a66SChristoph Hellwig * of all the possible deadlocks and enospc problems. 2324f46b5a66SChristoph Hellwig */ 2325f46b5a66SChristoph Hellwig long btrfs_ioctl_trans_end(struct file *file) 2326f46b5a66SChristoph Hellwig { 2327f46b5a66SChristoph Hellwig struct inode *inode = fdentry(file)->d_inode; 2328f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(inode)->root; 2329f46b5a66SChristoph Hellwig struct btrfs_trans_handle *trans; 2330f46b5a66SChristoph Hellwig 2331f46b5a66SChristoph Hellwig trans = file->private_data; 23321ab86aedSSage Weil if (!trans) 23331ab86aedSSage Weil return -EINVAL; 2334b214107eSChristoph Hellwig file->private_data = NULL; 23359ca9ee09SSage Weil 23361ab86aedSSage Weil btrfs_end_transaction(trans, root); 23371ab86aedSSage Weil 23389ca9ee09SSage Weil mutex_lock(&root->fs_info->trans_mutex); 23399ca9ee09SSage Weil root->fs_info->open_ioctl_trans--; 23409ca9ee09SSage Weil mutex_unlock(&root->fs_info->trans_mutex); 23419ca9ee09SSage Weil 2342cfc8ea87SSage Weil mnt_drop_write(file->f_path.mnt); 23431ab86aedSSage Weil return 0; 2344f46b5a66SChristoph Hellwig } 2345f46b5a66SChristoph Hellwig 234646204592SSage Weil static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) 234746204592SSage Weil { 234846204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 234946204592SSage Weil struct btrfs_trans_handle *trans; 235046204592SSage Weil u64 transid; 235146204592SSage Weil 235246204592SSage Weil trans = btrfs_start_transaction(root, 0); 235398d5dc13STsutomu Itoh if (IS_ERR(trans)) 235498d5dc13STsutomu Itoh return PTR_ERR(trans); 235546204592SSage Weil transid = trans->transid; 235646204592SSage Weil btrfs_commit_transaction_async(trans, root, 0); 235746204592SSage Weil 235846204592SSage Weil if (argp) 235946204592SSage Weil if (copy_to_user(argp, &transid, sizeof(transid))) 236046204592SSage Weil return -EFAULT; 236146204592SSage Weil return 0; 236246204592SSage Weil } 236346204592SSage Weil 236446204592SSage Weil static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) 236546204592SSage Weil { 236646204592SSage Weil struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 236746204592SSage Weil u64 transid; 236846204592SSage Weil 236946204592SSage Weil if (argp) { 237046204592SSage Weil if (copy_from_user(&transid, argp, sizeof(transid))) 237146204592SSage Weil return -EFAULT; 237246204592SSage Weil } else { 237346204592SSage Weil transid = 0; /* current trans */ 237446204592SSage Weil } 237546204592SSage Weil return btrfs_wait_for_commit(root, transid); 237646204592SSage Weil } 237746204592SSage Weil 2378f46b5a66SChristoph Hellwig long btrfs_ioctl(struct file *file, unsigned int 2379f46b5a66SChristoph Hellwig cmd, unsigned long arg) 2380f46b5a66SChristoph Hellwig { 2381f46b5a66SChristoph Hellwig struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; 23824bcabaa3SChristoph Hellwig void __user *argp = (void __user *)arg; 2383f46b5a66SChristoph Hellwig 2384f46b5a66SChristoph Hellwig switch (cmd) { 23856cbff00fSChristoph Hellwig case FS_IOC_GETFLAGS: 23866cbff00fSChristoph Hellwig return btrfs_ioctl_getflags(file, argp); 23876cbff00fSChristoph Hellwig case FS_IOC_SETFLAGS: 23886cbff00fSChristoph Hellwig return btrfs_ioctl_setflags(file, argp); 23896cbff00fSChristoph Hellwig case FS_IOC_GETVERSION: 23906cbff00fSChristoph Hellwig return btrfs_ioctl_getversion(file, argp); 2391f46b5a66SChristoph Hellwig case BTRFS_IOC_SNAP_CREATE: 2392fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 0); 2393fdfb1e4fSLi Zefan case BTRFS_IOC_SNAP_CREATE_V2: 2394fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create_v2(file, argp, 0); 23953de4586cSChris Mason case BTRFS_IOC_SUBVOL_CREATE: 2396fa0d2b9bSLi Zefan return btrfs_ioctl_snap_create(file, argp, 1); 239776dda93cSYan, Zheng case BTRFS_IOC_SNAP_DESTROY: 239876dda93cSYan, Zheng return btrfs_ioctl_snap_destroy(file, argp); 23990caa102dSLi Zefan case BTRFS_IOC_SUBVOL_GETFLAGS: 24000caa102dSLi Zefan return btrfs_ioctl_subvol_getflags(file, argp); 24010caa102dSLi Zefan case BTRFS_IOC_SUBVOL_SETFLAGS: 24020caa102dSLi Zefan return btrfs_ioctl_subvol_setflags(file, argp); 24036ef5ed0dSJosef Bacik case BTRFS_IOC_DEFAULT_SUBVOL: 24046ef5ed0dSJosef Bacik return btrfs_ioctl_default_subvol(file, argp); 2405f46b5a66SChristoph Hellwig case BTRFS_IOC_DEFRAG: 24061e701a32SChris Mason return btrfs_ioctl_defrag(file, NULL); 24071e701a32SChris Mason case BTRFS_IOC_DEFRAG_RANGE: 24081e701a32SChris Mason return btrfs_ioctl_defrag(file, argp); 2409f46b5a66SChristoph Hellwig case BTRFS_IOC_RESIZE: 24104bcabaa3SChristoph Hellwig return btrfs_ioctl_resize(root, argp); 2411f46b5a66SChristoph Hellwig case BTRFS_IOC_ADD_DEV: 24124bcabaa3SChristoph Hellwig return btrfs_ioctl_add_dev(root, argp); 2413f46b5a66SChristoph Hellwig case BTRFS_IOC_RM_DEV: 24144bcabaa3SChristoph Hellwig return btrfs_ioctl_rm_dev(root, argp); 2415f46b5a66SChristoph Hellwig case BTRFS_IOC_BALANCE: 2416f46b5a66SChristoph Hellwig return btrfs_balance(root->fs_info->dev_root); 2417f46b5a66SChristoph Hellwig case BTRFS_IOC_CLONE: 2418c5c9cd4dSSage Weil return btrfs_ioctl_clone(file, arg, 0, 0, 0); 2419c5c9cd4dSSage Weil case BTRFS_IOC_CLONE_RANGE: 24207a865e8aSChristoph Hellwig return btrfs_ioctl_clone_range(file, argp); 2421f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_START: 2422f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_start(file); 2423f46b5a66SChristoph Hellwig case BTRFS_IOC_TRANS_END: 2424f46b5a66SChristoph Hellwig return btrfs_ioctl_trans_end(file); 2425ac8e9819SChris Mason case BTRFS_IOC_TREE_SEARCH: 2426ac8e9819SChris Mason return btrfs_ioctl_tree_search(file, argp); 2427ac8e9819SChris Mason case BTRFS_IOC_INO_LOOKUP: 2428ac8e9819SChris Mason return btrfs_ioctl_ino_lookup(file, argp); 24291406e432SJosef Bacik case BTRFS_IOC_SPACE_INFO: 24301406e432SJosef Bacik return btrfs_ioctl_space_info(root, argp); 2431f46b5a66SChristoph Hellwig case BTRFS_IOC_SYNC: 2432f46b5a66SChristoph Hellwig btrfs_sync_fs(file->f_dentry->d_sb, 1); 2433f46b5a66SChristoph Hellwig return 0; 243446204592SSage Weil case BTRFS_IOC_START_SYNC: 243546204592SSage Weil return btrfs_ioctl_start_sync(file, argp); 243646204592SSage Weil case BTRFS_IOC_WAIT_SYNC: 243746204592SSage Weil return btrfs_ioctl_wait_sync(file, argp); 2438f46b5a66SChristoph Hellwig } 2439f46b5a66SChristoph Hellwig 2440f46b5a66SChristoph Hellwig return -ENOTTY; 2441f46b5a66SChristoph Hellwig } 2442