LCOV - code coverage report
Current view: top level - fs/btrfs - ioctl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1935 0.0 %
Date: 2014-04-16 Functions: 0 81 0.0 %
Branches: 0 1439 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2007 Oracle.  All rights reserved.
       3                 :            :  *
       4                 :            :  * This program is free software; you can redistribute it and/or
       5                 :            :  * modify it under the terms of the GNU General Public
       6                 :            :  * License v2 as published by the Free Software Foundation.
       7                 :            :  *
       8                 :            :  * This program is distributed in the hope that it will be useful,
       9                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      10                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      11                 :            :  * General Public License for more details.
      12                 :            :  *
      13                 :            :  * You should have received a copy of the GNU General Public
      14                 :            :  * License along with this program; if not, write to the
      15                 :            :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      16                 :            :  * Boston, MA 021110-1307, USA.
      17                 :            :  */
      18                 :            : 
      19                 :            : #include <linux/kernel.h>
      20                 :            : #include <linux/bio.h>
      21                 :            : #include <linux/buffer_head.h>
      22                 :            : #include <linux/file.h>
      23                 :            : #include <linux/fs.h>
      24                 :            : #include <linux/fsnotify.h>
      25                 :            : #include <linux/pagemap.h>
      26                 :            : #include <linux/highmem.h>
      27                 :            : #include <linux/time.h>
      28                 :            : #include <linux/init.h>
      29                 :            : #include <linux/string.h>
      30                 :            : #include <linux/backing-dev.h>
      31                 :            : #include <linux/mount.h>
      32                 :            : #include <linux/mpage.h>
      33                 :            : #include <linux/namei.h>
      34                 :            : #include <linux/swap.h>
      35                 :            : #include <linux/writeback.h>
      36                 :            : #include <linux/statfs.h>
      37                 :            : #include <linux/compat.h>
      38                 :            : #include <linux/bit_spinlock.h>
      39                 :            : #include <linux/security.h>
      40                 :            : #include <linux/xattr.h>
      41                 :            : #include <linux/vmalloc.h>
      42                 :            : #include <linux/slab.h>
      43                 :            : #include <linux/blkdev.h>
      44                 :            : #include <linux/uuid.h>
      45                 :            : #include <linux/btrfs.h>
      46                 :            : #include <linux/uaccess.h>
      47                 :            : #include "ctree.h"
      48                 :            : #include "disk-io.h"
      49                 :            : #include "transaction.h"
      50                 :            : #include "btrfs_inode.h"
      51                 :            : #include "print-tree.h"
      52                 :            : #include "volumes.h"
      53                 :            : #include "locking.h"
      54                 :            : #include "inode-map.h"
      55                 :            : #include "backref.h"
      56                 :            : #include "rcu-string.h"
      57                 :            : #include "send.h"
      58                 :            : #include "dev-replace.h"
      59                 :            : #include "props.h"
      60                 :            : #include "sysfs.h"
      61                 :            : 
      62                 :            : static int btrfs_clone(struct inode *src, struct inode *inode,
      63                 :            :                        u64 off, u64 olen, u64 olen_aligned, u64 destoff);
      64                 :            : 
      65                 :            : /* Mask out flags that are inappropriate for the given type of inode. */
      66                 :            : static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
      67                 :            : {
      68         [ #  # ]:          0 :         if (S_ISDIR(mode))
      69                 :            :                 return flags;
      70         [ #  # ]:          0 :         else if (S_ISREG(mode))
      71                 :          0 :                 return flags & ~FS_DIRSYNC_FL;
      72                 :            :         else
      73                 :          0 :                 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
      74                 :            : }
      75                 :            : 
      76                 :            : /*
      77                 :            :  * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
      78                 :            :  */
      79                 :          0 : static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
      80                 :            : {
      81                 :            :         unsigned int iflags = 0;
      82                 :            : 
      83         [ #  # ]:          0 :         if (flags & BTRFS_INODE_SYNC)
      84                 :            :                 iflags |= FS_SYNC_FL;
      85         [ #  # ]:          0 :         if (flags & BTRFS_INODE_IMMUTABLE)
      86                 :          0 :                 iflags |= FS_IMMUTABLE_FL;
      87         [ #  # ]:          0 :         if (flags & BTRFS_INODE_APPEND)
      88                 :          0 :                 iflags |= FS_APPEND_FL;
      89         [ #  # ]:          0 :         if (flags & BTRFS_INODE_NODUMP)
      90                 :          0 :                 iflags |= FS_NODUMP_FL;
      91         [ #  # ]:          0 :         if (flags & BTRFS_INODE_NOATIME)
      92                 :          0 :                 iflags |= FS_NOATIME_FL;
      93         [ #  # ]:          0 :         if (flags & BTRFS_INODE_DIRSYNC)
      94                 :          0 :                 iflags |= FS_DIRSYNC_FL;
      95         [ #  # ]:          0 :         if (flags & BTRFS_INODE_NODATACOW)
      96                 :          0 :                 iflags |= FS_NOCOW_FL;
      97                 :            : 
      98         [ #  # ]:          0 :         if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
      99                 :          0 :                 iflags |= FS_COMPR_FL;
     100         [ #  # ]:          0 :         else if (flags & BTRFS_INODE_NOCOMPRESS)
     101                 :          0 :                 iflags |= FS_NOCOMP_FL;
     102                 :            : 
     103                 :          0 :         return iflags;
     104                 :            : }
     105                 :            : 
     106                 :            : /*
     107                 :            :  * Update inode->i_flags based on the btrfs internal flags.
     108                 :            :  */
     109                 :          0 : void btrfs_update_iflags(struct inode *inode)
     110                 :            : {
     111                 :            :         struct btrfs_inode *ip = BTRFS_I(inode);
     112                 :            : 
     113                 :          0 :         inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
     114                 :            : 
     115         [ #  # ]:          0 :         if (ip->flags & BTRFS_INODE_SYNC)
     116                 :          0 :                 inode->i_flags |= S_SYNC;
     117         [ #  # ]:          0 :         if (ip->flags & BTRFS_INODE_IMMUTABLE)
     118                 :          0 :                 inode->i_flags |= S_IMMUTABLE;
     119         [ #  # ]:          0 :         if (ip->flags & BTRFS_INODE_APPEND)
     120                 :          0 :                 inode->i_flags |= S_APPEND;
     121         [ #  # ]:          0 :         if (ip->flags & BTRFS_INODE_NOATIME)
     122                 :          0 :                 inode->i_flags |= S_NOATIME;
     123         [ #  # ]:          0 :         if (ip->flags & BTRFS_INODE_DIRSYNC)
     124                 :          0 :                 inode->i_flags |= S_DIRSYNC;
     125                 :          0 : }
     126                 :            : 
     127                 :            : /*
     128                 :            :  * Inherit flags from the parent inode.
     129                 :            :  *
     130                 :            :  * Currently only the compression flags and the cow flags are inherited.
     131                 :            :  */
     132                 :          0 : void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
     133                 :            : {
     134                 :            :         unsigned int flags;
     135                 :            : 
     136         [ #  # ]:          0 :         if (!dir)
     137                 :          0 :                 return;
     138                 :            : 
     139                 :          0 :         flags = BTRFS_I(dir)->flags;
     140                 :            : 
     141         [ #  # ]:          0 :         if (flags & BTRFS_INODE_NOCOMPRESS) {
     142                 :          0 :                 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
     143                 :          0 :                 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
     144         [ #  # ]:          0 :         } else if (flags & BTRFS_INODE_COMPRESS) {
     145                 :          0 :                 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
     146                 :          0 :                 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
     147                 :            :         }
     148                 :            : 
     149         [ #  # ]:          0 :         if (flags & BTRFS_INODE_NODATACOW) {
     150                 :          0 :                 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
     151         [ #  # ]:          0 :                 if (S_ISREG(inode->i_mode))
     152                 :          0 :                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
     153                 :            :         }
     154                 :            : 
     155                 :          0 :         btrfs_update_iflags(inode);
     156                 :            : }
     157                 :            : 
     158                 :          0 : static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
     159                 :            : {
     160                 :            :         struct btrfs_inode *ip = BTRFS_I(file_inode(file));
     161                 :          0 :         unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
     162                 :            : 
     163         [ #  # ]:          0 :         if (copy_to_user(arg, &flags, sizeof(flags)))
     164                 :            :                 return -EFAULT;
     165                 :          0 :         return 0;
     166                 :            : }
     167                 :            : 
     168                 :            : static int check_flags(unsigned int flags)
     169                 :            : {
     170         [ #  # ]:          0 :         if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
     171                 :            :                       FS_NOATIME_FL | FS_NODUMP_FL | \
     172                 :            :                       FS_SYNC_FL | FS_DIRSYNC_FL | \
     173                 :            :                       FS_NOCOMP_FL | FS_COMPR_FL |
     174                 :            :                       FS_NOCOW_FL))
     175                 :            :                 return -EOPNOTSUPP;
     176                 :            : 
     177         [ #  # ]:          0 :         if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
     178                 :            :                 return -EINVAL;
     179                 :            : 
     180                 :            :         return 0;
     181                 :            : }
     182                 :            : 
     183                 :          0 : static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
     184                 :            : {
     185                 :            :         struct inode *inode = file_inode(file);
     186                 :            :         struct btrfs_inode *ip = BTRFS_I(inode);
     187                 :          0 :         struct btrfs_root *root = ip->root;
     188                 :            :         struct btrfs_trans_handle *trans;
     189                 :            :         unsigned int flags, oldflags;
     190                 :            :         int ret;
     191                 :            :         u64 ip_oldflags;
     192                 :            :         unsigned int i_oldflags;
     193                 :            :         umode_t mode;
     194                 :            : 
     195         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
     196                 :            :                 return -EPERM;
     197                 :            : 
     198         [ #  # ]:          0 :         if (btrfs_root_readonly(root))
     199                 :            :                 return -EROFS;
     200                 :            : 
     201         [ #  # ]:          0 :         if (copy_from_user(&flags, arg, sizeof(flags)))
     202                 :            :                 return -EFAULT;
     203                 :            : 
     204                 :          0 :         ret = check_flags(flags);
     205         [ #  # ]:          0 :         if (ret)
     206                 :            :                 return ret;
     207                 :            : 
     208                 :          0 :         ret = mnt_want_write_file(file);
     209         [ #  # ]:          0 :         if (ret)
     210                 :            :                 return ret;
     211                 :            : 
     212                 :          0 :         mutex_lock(&inode->i_mutex);
     213                 :            : 
     214                 :          0 :         ip_oldflags = ip->flags;
     215                 :          0 :         i_oldflags = inode->i_flags;
     216                 :          0 :         mode = inode->i_mode;
     217                 :            : 
     218                 :          0 :         flags = btrfs_mask_flags(inode->i_mode, flags);
     219                 :          0 :         oldflags = btrfs_flags_to_ioctl(ip->flags);
     220         [ #  # ]:          0 :         if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
     221         [ #  # ]:          0 :                 if (!capable(CAP_LINUX_IMMUTABLE)) {
     222                 :            :                         ret = -EPERM;
     223                 :            :                         goto out_unlock;
     224                 :            :                 }
     225                 :            :         }
     226                 :            : 
     227         [ #  # ]:          0 :         if (flags & FS_SYNC_FL)
     228                 :          0 :                 ip->flags |= BTRFS_INODE_SYNC;
     229                 :            :         else
     230                 :          0 :                 ip->flags &= ~BTRFS_INODE_SYNC;
     231         [ #  # ]:          0 :         if (flags & FS_IMMUTABLE_FL)
     232                 :          0 :                 ip->flags |= BTRFS_INODE_IMMUTABLE;
     233                 :            :         else
     234                 :          0 :                 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
     235         [ #  # ]:          0 :         if (flags & FS_APPEND_FL)
     236                 :          0 :                 ip->flags |= BTRFS_INODE_APPEND;
     237                 :            :         else
     238                 :          0 :                 ip->flags &= ~BTRFS_INODE_APPEND;
     239         [ #  # ]:          0 :         if (flags & FS_NODUMP_FL)
     240                 :          0 :                 ip->flags |= BTRFS_INODE_NODUMP;
     241                 :            :         else
     242                 :          0 :                 ip->flags &= ~BTRFS_INODE_NODUMP;
     243         [ #  # ]:          0 :         if (flags & FS_NOATIME_FL)
     244                 :          0 :                 ip->flags |= BTRFS_INODE_NOATIME;
     245                 :            :         else
     246                 :          0 :                 ip->flags &= ~BTRFS_INODE_NOATIME;
     247         [ #  # ]:          0 :         if (flags & FS_DIRSYNC_FL)
     248                 :          0 :                 ip->flags |= BTRFS_INODE_DIRSYNC;
     249                 :            :         else
     250                 :          0 :                 ip->flags &= ~BTRFS_INODE_DIRSYNC;
     251         [ #  # ]:          0 :         if (flags & FS_NOCOW_FL) {
     252         [ #  # ]:          0 :                 if (S_ISREG(mode)) {
     253                 :            :                         /*
     254                 :            :                          * It's safe to turn csums off here, no extents exist.
     255                 :            :                          * Otherwise we want the flag to reflect the real COW
     256                 :            :                          * status of the file and will not set it.
     257                 :            :                          */
     258         [ #  # ]:          0 :                         if (inode->i_size == 0)
     259                 :          0 :                                 ip->flags |= BTRFS_INODE_NODATACOW
     260                 :            :                                            | BTRFS_INODE_NODATASUM;
     261                 :            :                 } else {
     262                 :          0 :                         ip->flags |= BTRFS_INODE_NODATACOW;
     263                 :            :                 }
     264                 :            :         } else {
     265                 :            :                 /*
     266                 :            :                  * Revert back under same assuptions as above
     267                 :            :                  */
     268         [ #  # ]:          0 :                 if (S_ISREG(mode)) {
     269         [ #  # ]:          0 :                         if (inode->i_size == 0)
     270                 :          0 :                                 ip->flags &= ~(BTRFS_INODE_NODATACOW
     271                 :            :                                              | BTRFS_INODE_NODATASUM);
     272                 :            :                 } else {
     273                 :          0 :                         ip->flags &= ~BTRFS_INODE_NODATACOW;
     274                 :            :                 }
     275                 :            :         }
     276                 :            : 
     277                 :            :         /*
     278                 :            :          * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
     279                 :            :          * flag may be changed automatically if compression code won't make
     280                 :            :          * things smaller.
     281                 :            :          */
     282         [ #  # ]:          0 :         if (flags & FS_NOCOMP_FL) {
     283                 :          0 :                 ip->flags &= ~BTRFS_INODE_COMPRESS;
     284                 :          0 :                 ip->flags |= BTRFS_INODE_NOCOMPRESS;
     285                 :            : 
     286                 :          0 :                 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
     287         [ #  # ]:          0 :                 if (ret && ret != -ENODATA)
     288                 :            :                         goto out_drop;
     289         [ #  # ]:          0 :         } else if (flags & FS_COMPR_FL) {
     290                 :            :                 const char *comp;
     291                 :            : 
     292                 :          0 :                 ip->flags |= BTRFS_INODE_COMPRESS;
     293                 :          0 :                 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
     294                 :            : 
     295         [ #  # ]:          0 :                 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
     296                 :            :                         comp = "lzo";
     297                 :            :                 else
     298                 :            :                         comp = "zlib";
     299                 :          0 :                 ret = btrfs_set_prop(inode, "btrfs.compression",
     300                 :            :                                      comp, strlen(comp), 0);
     301         [ #  # ]:          0 :                 if (ret)
     302                 :            :                         goto out_drop;
     303                 :            : 
     304                 :            :         } else {
     305                 :          0 :                 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
     306                 :            :         }
     307                 :            : 
     308                 :          0 :         trans = btrfs_start_transaction(root, 1);
     309         [ #  # ]:          0 :         if (IS_ERR(trans)) {
     310                 :            :                 ret = PTR_ERR(trans);
     311                 :          0 :                 goto out_drop;
     312                 :            :         }
     313                 :            : 
     314                 :          0 :         btrfs_update_iflags(inode);
     315                 :            :         inode_inc_iversion(inode);
     316                 :          0 :         inode->i_ctime = CURRENT_TIME;
     317                 :          0 :         ret = btrfs_update_inode(trans, root, inode);
     318                 :            : 
     319                 :          0 :         btrfs_end_transaction(trans, root);
     320                 :            :  out_drop:
     321         [ #  # ]:          0 :         if (ret) {
     322                 :          0 :                 ip->flags = ip_oldflags;
     323                 :          0 :                 inode->i_flags = i_oldflags;
     324                 :            :         }
     325                 :            : 
     326                 :            :  out_unlock:
     327                 :          0 :         mutex_unlock(&inode->i_mutex);
     328                 :          0 :         mnt_drop_write_file(file);
     329                 :          0 :         return ret;
     330                 :            : }
     331                 :            : 
     332                 :          0 : static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
     333                 :            : {
     334                 :            :         struct inode *inode = file_inode(file);
     335                 :            : 
     336                 :          0 :         return put_user(inode->i_generation, arg);
     337                 :            : }
     338                 :            : 
     339                 :          0 : static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
     340                 :            : {
     341                 :          0 :         struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
     342                 :            :         struct btrfs_device *device;
     343                 :            :         struct request_queue *q;
     344                 :            :         struct fstrim_range range;
     345                 :            :         u64 minlen = ULLONG_MAX;
     346                 :            :         u64 num_devices = 0;
     347                 :          0 :         u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
     348                 :            :         int ret;
     349                 :            : 
     350         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
     351                 :            :                 return -EPERM;
     352                 :            : 
     353                 :            :         rcu_read_lock();
     354         [ #  # ]:          0 :         list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
     355                 :            :                                 dev_list) {
     356         [ #  # ]:          0 :                 if (!device->bdev)
     357                 :          0 :                         continue;
     358                 :            :                 q = bdev_get_queue(device->bdev);
     359         [ #  # ]:          0 :                 if (blk_queue_discard(q)) {
     360                 :          0 :                         num_devices++;
     361                 :          0 :                         minlen = min((u64)q->limits.discard_granularity,
     362                 :            :                                      minlen);
     363                 :            :                 }
     364                 :            :         }
     365                 :            :         rcu_read_unlock();
     366                 :            : 
     367         [ #  # ]:          0 :         if (!num_devices)
     368                 :            :                 return -EOPNOTSUPP;
     369         [ #  # ]:          0 :         if (copy_from_user(&range, arg, sizeof(range)))
     370                 :            :                 return -EFAULT;
     371 [ #  # ][ #  # ]:          0 :         if (range.start > total_bytes ||
     372                 :          0 :             range.len < fs_info->sb->s_blocksize)
     373                 :            :                 return -EINVAL;
     374                 :            : 
     375                 :          0 :         range.len = min(range.len, total_bytes - range.start);
     376                 :          0 :         range.minlen = max(range.minlen, minlen);
     377                 :          0 :         ret = btrfs_trim_fs(fs_info->tree_root, &range);
     378         [ #  # ]:          0 :         if (ret < 0)
     379                 :            :                 return ret;
     380                 :            : 
     381         [ #  # ]:          0 :         if (copy_to_user(arg, &range, sizeof(range)))
     382                 :            :                 return -EFAULT;
     383                 :            : 
     384                 :          0 :         return 0;
     385                 :            : }
     386                 :            : 
     387                 :          0 : int btrfs_is_empty_uuid(u8 *uuid)
     388                 :            : {
     389                 :            :         int i;
     390                 :            : 
     391 [ #  # ][ #  # ]:          0 :         for (i = 0; i < BTRFS_UUID_SIZE; i++) {
         [ #  # ][ #  # ]
                 [ #  # ]
     392 [ #  # ][ #  # ]:          0 :                 if (uuid[i])
         [ #  # ][ #  # ]
                 [ #  # ]
     393                 :            :                         return 0;
     394                 :            :         }
     395                 :            :         return 1;
     396                 :            : }
     397                 :            : 
     398                 :          0 : static noinline int create_subvol(struct inode *dir,
     399                 :            :                                   struct dentry *dentry,
     400                 :            :                                   char *name, int namelen,
     401                 :            :                                   u64 *async_transid,
     402                 :            :                                   struct btrfs_qgroup_inherit *inherit)
     403                 :            : {
     404                 :            :         struct btrfs_trans_handle *trans;
     405                 :            :         struct btrfs_key key;
     406                 :            :         struct btrfs_root_item root_item;
     407                 :            :         struct btrfs_inode_item *inode_item;
     408                 :          0 :         struct extent_buffer *leaf;
     409                 :          0 :         struct btrfs_root *root = BTRFS_I(dir)->root;
     410                 :            :         struct btrfs_root *new_root;
     411                 :            :         struct btrfs_block_rsv block_rsv;
     412                 :          0 :         struct timespec cur_time = CURRENT_TIME;
     413                 :            :         struct inode *inode;
     414                 :            :         int ret;
     415                 :            :         int err;
     416                 :            :         u64 objectid;
     417                 :            :         u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
     418                 :          0 :         u64 index = 0;
     419                 :            :         u64 qgroup_reserved;
     420                 :            :         uuid_le new_uuid;
     421                 :            : 
     422                 :          0 :         ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
     423         [ #  # ]:          0 :         if (ret)
     424                 :            :                 return ret;
     425                 :            : 
     426                 :          0 :         btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
     427                 :            :         /*
     428                 :            :          * The same as the snapshot creation, please see the comment
     429                 :            :          * of create_snapshot().
     430                 :            :          */
     431                 :          0 :         ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
     432                 :            :                                                8, &qgroup_reserved, false);
     433         [ #  # ]:          0 :         if (ret)
     434                 :            :                 return ret;
     435                 :            : 
     436                 :          0 :         trans = btrfs_start_transaction(root, 0);
     437         [ #  # ]:          0 :         if (IS_ERR(trans)) {
     438                 :            :                 ret = PTR_ERR(trans);
     439                 :          0 :                 btrfs_subvolume_release_metadata(root, &block_rsv,
     440                 :            :                                                  qgroup_reserved);
     441                 :          0 :                 return ret;
     442                 :            :         }
     443                 :          0 :         trans->block_rsv = &block_rsv;
     444                 :          0 :         trans->bytes_reserved = block_rsv.size;
     445                 :            : 
     446                 :          0 :         ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
     447         [ #  # ]:          0 :         if (ret)
     448                 :            :                 goto fail;
     449                 :            : 
     450                 :          0 :         leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
     451                 :            :                                       0, objectid, NULL, 0, 0, 0);
     452         [ #  # ]:          0 :         if (IS_ERR(leaf)) {
     453                 :            :                 ret = PTR_ERR(leaf);
     454                 :          0 :                 goto fail;
     455                 :            :         }
     456                 :            : 
     457                 :          0 :         memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
     458                 :          0 :         btrfs_set_header_bytenr(leaf, leaf->start);
     459                 :          0 :         btrfs_set_header_generation(leaf, trans->transid);
     460                 :            :         btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
     461                 :          0 :         btrfs_set_header_owner(leaf, objectid);
     462                 :            : 
     463                 :          0 :         write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
     464                 :            :                             BTRFS_FSID_SIZE);
     465                 :          0 :         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
     466                 :            :                             btrfs_header_chunk_tree_uuid(leaf),
     467                 :            :                             BTRFS_UUID_SIZE);
     468                 :          0 :         btrfs_mark_buffer_dirty(leaf);
     469                 :            : 
     470                 :          0 :         memset(&root_item, 0, sizeof(root_item));
     471                 :            : 
     472                 :            :         inode_item = &root_item.inode;
     473                 :            :         btrfs_set_stack_inode_generation(inode_item, 1);
     474                 :            :         btrfs_set_stack_inode_size(inode_item, 3);
     475                 :            :         btrfs_set_stack_inode_nlink(inode_item, 1);
     476                 :          0 :         btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
     477                 :            :         btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
     478                 :            : 
     479                 :            :         btrfs_set_root_flags(&root_item, 0);
     480                 :            :         btrfs_set_root_limit(&root_item, 0);
     481                 :            :         btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
     482                 :            : 
     483                 :          0 :         btrfs_set_root_bytenr(&root_item, leaf->start);
     484                 :          0 :         btrfs_set_root_generation(&root_item, trans->transid);
     485                 :            :         btrfs_set_root_level(&root_item, 0);
     486                 :            :         btrfs_set_root_refs(&root_item, 1);
     487                 :          0 :         btrfs_set_root_used(&root_item, leaf->len);
     488                 :            :         btrfs_set_root_last_snapshot(&root_item, 0);
     489                 :            : 
     490                 :            :         btrfs_set_root_generation_v2(&root_item,
     491                 :            :                         btrfs_root_generation(&root_item));
     492                 :          0 :         uuid_le_gen(&new_uuid);
     493                 :          0 :         memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
     494                 :          0 :         btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
     495                 :          0 :         btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
     496                 :          0 :         root_item.ctime = root_item.otime;
     497                 :          0 :         btrfs_set_root_ctransid(&root_item, trans->transid);
     498                 :          0 :         btrfs_set_root_otransid(&root_item, trans->transid);
     499                 :            : 
     500                 :          0 :         btrfs_tree_unlock(leaf);
     501                 :          0 :         free_extent_buffer(leaf);
     502                 :            :         leaf = NULL;
     503                 :            : 
     504                 :            :         btrfs_set_root_dirid(&root_item, new_dirid);
     505                 :            : 
     506                 :          0 :         key.objectid = objectid;
     507                 :          0 :         key.offset = 0;
     508                 :            :         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
     509                 :          0 :         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
     510                 :            :                                 &root_item);
     511         [ #  # ]:          0 :         if (ret)
     512                 :            :                 goto fail;
     513                 :            : 
     514                 :          0 :         key.offset = (u64)-1;
     515                 :          0 :         new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
     516         [ #  # ]:          0 :         if (IS_ERR(new_root)) {
     517                 :          0 :                 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
     518                 :            :                 ret = PTR_ERR(new_root);
     519                 :          0 :                 goto fail;
     520                 :            :         }
     521                 :            : 
     522                 :          0 :         btrfs_record_root_in_trans(trans, new_root);
     523                 :            : 
     524                 :          0 :         ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
     525         [ #  # ]:          0 :         if (ret) {
     526                 :            :                 /* We potentially lose an unused inode item here */
     527                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
     528                 :          0 :                 goto fail;
     529                 :            :         }
     530                 :            : 
     531                 :            :         /*
     532                 :            :          * insert the directory item
     533                 :            :          */
     534                 :          0 :         ret = btrfs_set_inode_index(dir, &index);
     535         [ #  # ]:          0 :         if (ret) {
     536                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
     537                 :          0 :                 goto fail;
     538                 :            :         }
     539                 :            : 
     540                 :          0 :         ret = btrfs_insert_dir_item(trans, root,
     541                 :            :                                     name, namelen, dir, &key,
     542                 :            :                                     BTRFS_FT_DIR, index);
     543         [ #  # ]:          0 :         if (ret) {
     544                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
     545                 :          0 :                 goto fail;
     546                 :            :         }
     547                 :            : 
     548                 :          0 :         btrfs_i_size_write(dir, dir->i_size + namelen * 2);
     549                 :          0 :         ret = btrfs_update_inode(trans, root, dir);
     550         [ #  # ]:          0 :         BUG_ON(ret);
     551                 :            : 
     552                 :          0 :         ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
     553                 :            :                                  objectid, root->root_key.objectid,
     554                 :            :                                  btrfs_ino(dir), index, name, namelen);
     555         [ #  # ]:          0 :         BUG_ON(ret);
     556                 :            : 
     557                 :          0 :         ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
     558                 :            :                                   root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
     559                 :            :                                   objectid);
     560         [ #  # ]:          0 :         if (ret)
     561                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
     562                 :            : 
     563                 :            : fail:
     564                 :          0 :         trans->block_rsv = NULL;
     565                 :          0 :         trans->bytes_reserved = 0;
     566                 :          0 :         btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
     567                 :            : 
     568         [ #  # ]:          0 :         if (async_transid) {
     569                 :          0 :                 *async_transid = trans->transid;
     570                 :          0 :                 err = btrfs_commit_transaction_async(trans, root, 1);
     571         [ #  # ]:          0 :                 if (err)
     572                 :          0 :                         err = btrfs_commit_transaction(trans, root);
     573                 :            :         } else {
     574                 :          0 :                 err = btrfs_commit_transaction(trans, root);
     575                 :            :         }
     576         [ #  # ]:          0 :         if (err && !ret)
     577                 :            :                 ret = err;
     578                 :            : 
     579         [ #  # ]:          0 :         if (!ret) {
     580                 :          0 :                 inode = btrfs_lookup_dentry(dir, dentry);
     581         [ #  # ]:          0 :                 if (IS_ERR(inode))
     582                 :          0 :                         return PTR_ERR(inode);
     583                 :          0 :                 d_instantiate(dentry, inode);
     584                 :            :         }
     585                 :          0 :         return ret;
     586                 :            : }
     587                 :            : 
     588                 :          0 : static int create_snapshot(struct btrfs_root *root, struct inode *dir,
     589                 :            :                            struct dentry *dentry, char *name, int namelen,
     590                 :            :                            u64 *async_transid, bool readonly,
     591                 :            :                            struct btrfs_qgroup_inherit *inherit)
     592                 :            : {
     593                 :            :         struct inode *inode;
     594                 :            :         struct btrfs_pending_snapshot *pending_snapshot;
     595                 :            :         struct btrfs_trans_handle *trans;
     596                 :            :         int ret;
     597                 :            : 
     598         [ #  # ]:          0 :         if (!root->ref_cows)
     599                 :            :                 return -EINVAL;
     600                 :            : 
     601                 :          0 :         ret = btrfs_start_delalloc_inodes(root, 0);
     602         [ #  # ]:          0 :         if (ret)
     603                 :            :                 return ret;
     604                 :            : 
     605                 :          0 :         btrfs_wait_ordered_extents(root, -1);
     606                 :            : 
     607                 :            :         pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
     608         [ #  # ]:          0 :         if (!pending_snapshot)
     609                 :            :                 return -ENOMEM;
     610                 :            : 
     611                 :          0 :         btrfs_init_block_rsv(&pending_snapshot->block_rsv,
     612                 :            :                              BTRFS_BLOCK_RSV_TEMP);
     613                 :            :         /*
     614                 :            :          * 1 - parent dir inode
     615                 :            :          * 2 - dir entries
     616                 :            :          * 1 - root item
     617                 :            :          * 2 - root ref/backref
     618                 :            :          * 1 - root of snapshot
     619                 :            :          * 1 - UUID item
     620                 :            :          */
     621                 :          0 :         ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
     622                 :            :                                         &pending_snapshot->block_rsv, 8,
     623                 :            :                                         &pending_snapshot->qgroup_reserved,
     624                 :            :                                         false);
     625         [ #  # ]:          0 :         if (ret)
     626                 :            :                 goto out;
     627                 :            : 
     628                 :          0 :         pending_snapshot->dentry = dentry;
     629                 :          0 :         pending_snapshot->root = root;
     630                 :          0 :         pending_snapshot->readonly = readonly;
     631                 :          0 :         pending_snapshot->dir = dir;
     632                 :          0 :         pending_snapshot->inherit = inherit;
     633                 :            : 
     634                 :          0 :         trans = btrfs_start_transaction(root, 0);
     635         [ #  # ]:          0 :         if (IS_ERR(trans)) {
     636                 :            :                 ret = PTR_ERR(trans);
     637                 :            :                 goto fail;
     638                 :            :         }
     639                 :            : 
     640                 :          0 :         spin_lock(&root->fs_info->trans_lock);
     641                 :          0 :         list_add(&pending_snapshot->list,
     642                 :          0 :                  &trans->transaction->pending_snapshots);
     643                 :          0 :         spin_unlock(&root->fs_info->trans_lock);
     644         [ #  # ]:          0 :         if (async_transid) {
     645                 :          0 :                 *async_transid = trans->transid;
     646                 :          0 :                 ret = btrfs_commit_transaction_async(trans,
     647                 :          0 :                                      root->fs_info->extent_root, 1);
     648         [ #  # ]:          0 :                 if (ret)
     649                 :          0 :                         ret = btrfs_commit_transaction(trans, root);
     650                 :            :         } else {
     651                 :          0 :                 ret = btrfs_commit_transaction(trans,
     652                 :          0 :                                                root->fs_info->extent_root);
     653                 :            :         }
     654         [ #  # ]:          0 :         if (ret)
     655                 :            :                 goto fail;
     656                 :            : 
     657                 :          0 :         ret = pending_snapshot->error;
     658         [ #  # ]:          0 :         if (ret)
     659                 :            :                 goto fail;
     660                 :            : 
     661                 :          0 :         ret = btrfs_orphan_cleanup(pending_snapshot->snap);
     662         [ #  # ]:          0 :         if (ret)
     663                 :            :                 goto fail;
     664                 :            : 
     665                 :          0 :         inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
     666         [ #  # ]:          0 :         if (IS_ERR(inode)) {
     667                 :            :                 ret = PTR_ERR(inode);
     668                 :            :                 goto fail;
     669                 :            :         }
     670                 :            : 
     671                 :          0 :         d_instantiate(dentry, inode);
     672                 :            :         ret = 0;
     673                 :            : fail:
     674                 :          0 :         btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
     675                 :            :                                          &pending_snapshot->block_rsv,
     676                 :            :                                          pending_snapshot->qgroup_reserved);
     677                 :            : out:
     678                 :          0 :         kfree(pending_snapshot);
     679                 :            :         return ret;
     680                 :            : }
     681                 :            : 
     682                 :            : /*  copy of check_sticky in fs/namei.c()
     683                 :            : * It's inline, so penalty for filesystems that don't use sticky bit is
     684                 :            : * minimal.
     685                 :            : */
     686                 :            : static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
     687                 :            : {
     688                 :          0 :         kuid_t fsuid = current_fsuid();
     689                 :            : 
     690         [ #  # ]:          0 :         if (!(dir->i_mode & S_ISVTX))
     691                 :            :                 return 0;
     692         [ #  # ]:          0 :         if (uid_eq(inode->i_uid, fsuid))
     693                 :            :                 return 0;
     694         [ #  # ]:          0 :         if (uid_eq(dir->i_uid, fsuid))
     695                 :            :                 return 0;
     696                 :          0 :         return !capable(CAP_FOWNER);
     697                 :            : }
     698                 :            : 
     699                 :            : /*  copy of may_delete in fs/namei.c()
     700                 :            :  *      Check whether we can remove a link victim from directory dir, check
     701                 :            :  *  whether the type of victim is right.
     702                 :            :  *  1. We can't do it if dir is read-only (done in permission())
     703                 :            :  *  2. We should have write and exec permissions on dir
     704                 :            :  *  3. We can't remove anything from append-only dir
     705                 :            :  *  4. We can't do anything with immutable dir (done in permission())
     706                 :            :  *  5. If the sticky bit on dir is set we should either
     707                 :            :  *      a. be owner of dir, or
     708                 :            :  *      b. be owner of victim, or
     709                 :            :  *      c. have CAP_FOWNER capability
     710                 :            :  *  6. If the victim is append-only or immutable we can't do antyhing with
     711                 :            :  *     links pointing to it.
     712                 :            :  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
     713                 :            :  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
     714                 :            :  *  9. We can't remove a root or mountpoint.
     715                 :            :  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
     716                 :            :  *     nfs_async_unlink().
     717                 :            :  */
     718                 :            : 
     719                 :          0 : static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
     720                 :            : {
     721                 :            :         int error;
     722                 :            : 
     723         [ #  # ]:          0 :         if (!victim->d_inode)
     724                 :            :                 return -ENOENT;
     725                 :            : 
     726         [ #  # ]:          0 :         BUG_ON(victim->d_parent->d_inode != dir);
     727                 :            :         audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
     728                 :            : 
     729                 :          0 :         error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
     730         [ #  # ]:          0 :         if (error)
     731                 :            :                 return error;
     732         [ #  # ]:          0 :         if (IS_APPEND(dir))
     733                 :            :                 return -EPERM;
     734         [ #  # ]:          0 :         if (btrfs_check_sticky(dir, victim->d_inode)||
     735                 :          0 :                 IS_APPEND(victim->d_inode)||
     736         [ #  # ]:          0 :             IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
     737                 :            :                 return -EPERM;
     738         [ #  # ]:          0 :         if (isdir) {
     739         [ #  # ]:          0 :                 if (!S_ISDIR(victim->d_inode->i_mode))
     740                 :            :                         return -ENOTDIR;
     741         [ #  # ]:          0 :                 if (IS_ROOT(victim))
     742                 :            :                         return -EBUSY;
     743         [ #  # ]:          0 :         } else if (S_ISDIR(victim->d_inode->i_mode))
     744                 :            :                 return -EISDIR;
     745         [ #  # ]:          0 :         if (IS_DEADDIR(dir))
     746                 :            :                 return -ENOENT;
     747         [ #  # ]:          0 :         if (victim->d_flags & DCACHE_NFSFS_RENAMED)
     748                 :            :                 return -EBUSY;
     749                 :          0 :         return 0;
     750                 :            : }
     751                 :            : 
     752                 :            : /* copy of may_create in fs/namei.c() */
     753                 :            : static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
     754                 :            : {
     755         [ #  # ]:          0 :         if (child->d_inode)
     756                 :            :                 return -EEXIST;
     757         [ #  # ]:          0 :         if (IS_DEADDIR(dir))
     758                 :            :                 return -ENOENT;
     759                 :          0 :         return inode_permission(dir, MAY_WRITE | MAY_EXEC);
     760                 :            : }
     761                 :            : 
     762                 :            : /*
     763                 :            :  * Create a new subvolume below @parent.  This is largely modeled after
     764                 :            :  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
     765                 :            :  * inside this filesystem so it's quite a bit simpler.
     766                 :            :  */
     767                 :          0 : static noinline int btrfs_mksubvol(struct path *parent,
     768                 :            :                                    char *name, int namelen,
     769                 :            :                                    struct btrfs_root *snap_src,
     770                 :            :                                    u64 *async_transid, bool readonly,
     771                 :            :                                    struct btrfs_qgroup_inherit *inherit)
     772                 :            : {
     773                 :          0 :         struct inode *dir  = parent->dentry->d_inode;
     774                 :            :         struct dentry *dentry;
     775                 :            :         int error;
     776                 :            : 
     777                 :          0 :         error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
     778         [ #  # ]:          0 :         if (error == -EINTR)
     779                 :            :                 return error;
     780                 :            : 
     781                 :          0 :         dentry = lookup_one_len(name, parent->dentry, namelen);
     782                 :            :         error = PTR_ERR(dentry);
     783         [ #  # ]:          0 :         if (IS_ERR(dentry))
     784                 :            :                 goto out_unlock;
     785                 :            : 
     786                 :            :         error = -EEXIST;
     787         [ #  # ]:          0 :         if (dentry->d_inode)
     788                 :            :                 goto out_dput;
     789                 :            : 
     790                 :            :         error = btrfs_may_create(dir, dentry);
     791         [ #  # ]:          0 :         if (error)
     792                 :            :                 goto out_dput;
     793                 :            : 
     794                 :            :         /*
     795                 :            :          * even if this name doesn't exist, we may get hash collisions.
     796                 :            :          * check for them now when we can safely fail
     797                 :            :          */
     798                 :          0 :         error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
     799                 :          0 :                                                dir->i_ino, name,
     800                 :            :                                                namelen);
     801         [ #  # ]:          0 :         if (error)
     802                 :            :                 goto out_dput;
     803                 :            : 
     804                 :          0 :         down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
     805                 :            : 
     806         [ #  # ]:          0 :         if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
     807                 :            :                 goto out_up_read;
     808                 :            : 
     809         [ #  # ]:          0 :         if (snap_src) {
     810                 :          0 :                 error = create_snapshot(snap_src, dir, dentry, name, namelen,
     811                 :            :                                         async_transid, readonly, inherit);
     812                 :            :         } else {
     813                 :          0 :                 error = create_subvol(dir, dentry, name, namelen,
     814                 :            :                                       async_transid, inherit);
     815                 :            :         }
     816         [ #  # ]:          0 :         if (!error)
     817                 :            :                 fsnotify_mkdir(dir, dentry);
     818                 :            : out_up_read:
     819                 :          0 :         up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
     820                 :            : out_dput:
     821                 :          0 :         dput(dentry);
     822                 :            : out_unlock:
     823                 :          0 :         mutex_unlock(&dir->i_mutex);
     824                 :            :         return error;
     825                 :            : }
     826                 :            : 
     827                 :            : /*
     828                 :            :  * When we're defragging a range, we don't want to kick it off again
     829                 :            :  * if it is really just waiting for delalloc to send it down.
     830                 :            :  * If we find a nice big extent or delalloc range for the bytes in the
     831                 :            :  * file you want to defrag, we return 0 to let you know to skip this
     832                 :            :  * part of the file
     833                 :            :  */
     834                 :          0 : static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
     835                 :            : {
     836                 :          0 :         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
     837                 :            :         struct extent_map *em = NULL;
     838                 :          0 :         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
     839                 :            :         u64 end;
     840                 :            : 
     841                 :          0 :         read_lock(&em_tree->lock);
     842                 :          0 :         em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
     843                 :            :         read_unlock(&em_tree->lock);
     844                 :            : 
     845         [ #  # ]:          0 :         if (em) {
     846                 :            :                 end = extent_map_end(em);
     847                 :          0 :                 free_extent_map(em);
     848         [ #  # ]:          0 :                 if (end - offset > thresh)
     849                 :            :                         return 0;
     850                 :            :         }
     851                 :            :         /* if we already have a nice delalloc here, just stop */
     852                 :          0 :         thresh /= 2;
     853                 :          0 :         end = count_range_bits(io_tree, &offset, offset + thresh,
     854                 :            :                                thresh, EXTENT_DELALLOC, 1);
     855         [ #  # ]:          0 :         if (end >= thresh)
     856                 :            :                 return 0;
     857                 :          0 :         return 1;
     858                 :            : }
     859                 :            : 
     860                 :            : /*
     861                 :            :  * helper function to walk through a file and find extents
     862                 :            :  * newer than a specific transid, and smaller than thresh.
     863                 :            :  *
     864                 :            :  * This is used by the defragging code to find new and small
     865                 :            :  * extents
     866                 :            :  */
     867                 :          0 : static int find_new_extents(struct btrfs_root *root,
     868                 :            :                             struct inode *inode, u64 newer_than,
     869                 :            :                             u64 *off, int thresh)
     870                 :            : {
     871                 :            :         struct btrfs_path *path;
     872                 :            :         struct btrfs_key min_key;
     873                 :            :         struct extent_buffer *leaf;
     874                 :            :         struct btrfs_file_extent_item *extent;
     875                 :            :         int type;
     876                 :            :         int ret;
     877                 :            :         u64 ino = btrfs_ino(inode);
     878                 :            : 
     879                 :          0 :         path = btrfs_alloc_path();
     880         [ #  # ]:          0 :         if (!path)
     881                 :            :                 return -ENOMEM;
     882                 :            : 
     883                 :          0 :         min_key.objectid = ino;
     884                 :          0 :         min_key.type = BTRFS_EXTENT_DATA_KEY;
     885                 :          0 :         min_key.offset = *off;
     886                 :            : 
     887                 :          0 :         path->keep_locks = 1;
     888                 :            : 
     889                 :            :         while (1) {
     890                 :          0 :                 ret = btrfs_search_forward(root, &min_key, path, newer_than);
     891         [ #  # ]:          0 :                 if (ret != 0)
     892                 :            :                         goto none;
     893         [ #  # ]:          0 :                 if (min_key.objectid != ino)
     894                 :            :                         goto none;
     895         [ #  # ]:          0 :                 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
     896                 :            :                         goto none;
     897                 :            : 
     898                 :          0 :                 leaf = path->nodes[0];
     899                 :          0 :                 extent = btrfs_item_ptr(leaf, path->slots[0],
     900                 :            :                                         struct btrfs_file_extent_item);
     901                 :            : 
     902                 :            :                 type = btrfs_file_extent_type(leaf, extent);
     903   [ #  #  #  # ]:          0 :                 if (type == BTRFS_FILE_EXTENT_REG &&
     904         [ #  # ]:          0 :                     btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
     905                 :          0 :                     check_defrag_in_cache(inode, min_key.offset, thresh)) {
     906                 :          0 :                         *off = min_key.offset;
     907                 :          0 :                         btrfs_free_path(path);
     908                 :          0 :                         return 0;
     909                 :            :                 }
     910                 :            : 
     911         [ #  # ]:          0 :                 if (min_key.offset == (u64)-1)
     912                 :            :                         goto none;
     913                 :            : 
     914                 :          0 :                 min_key.offset++;
     915                 :          0 :                 btrfs_release_path(path);
     916                 :          0 :         }
     917                 :            : none:
     918                 :          0 :         btrfs_free_path(path);
     919                 :          0 :         return -ENOENT;
     920                 :            : }
     921                 :            : 
     922                 :          0 : static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
     923                 :            : {
     924                 :          0 :         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
     925                 :          0 :         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
     926                 :            :         struct extent_map *em;
     927                 :            :         u64 len = PAGE_CACHE_SIZE;
     928                 :            : 
     929                 :            :         /*
     930                 :            :          * hopefully we have this extent in the tree already, try without
     931                 :            :          * the full extent lock
     932                 :            :          */
     933                 :          0 :         read_lock(&em_tree->lock);
     934                 :          0 :         em = lookup_extent_mapping(em_tree, start, len);
     935                 :            :         read_unlock(&em_tree->lock);
     936                 :            : 
     937         [ #  # ]:          0 :         if (!em) {
     938                 :            :                 /* get the big lock and read metadata off disk */
     939                 :          0 :                 lock_extent(io_tree, start, start + len - 1);
     940                 :          0 :                 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
     941                 :          0 :                 unlock_extent(io_tree, start, start + len - 1);
     942                 :            : 
     943         [ #  # ]:          0 :                 if (IS_ERR(em))
     944                 :            :                         return NULL;
     945                 :            :         }
     946                 :            : 
     947                 :          0 :         return em;
     948                 :            : }
     949                 :            : 
     950                 :          0 : static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
     951                 :            : {
     952                 :            :         struct extent_map *next;
     953                 :            :         bool ret = true;
     954                 :            : 
     955                 :            :         /* this is the last extent */
     956         [ #  # ]:          0 :         if (em->start + em->len >= i_size_read(inode))
     957                 :            :                 return false;
     958                 :            : 
     959                 :          0 :         next = defrag_lookup_extent(inode, em->start + em->len);
     960 [ #  # ][ #  # ]:          0 :         if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
     961                 :            :                 ret = false;
     962                 :            : 
     963                 :          0 :         free_extent_map(next);
     964                 :            :         return ret;
     965                 :            : }
     966                 :            : 
     967                 :          0 : static int should_defrag_range(struct inode *inode, u64 start, int thresh,
     968                 :            :                                u64 *last_len, u64 *skip, u64 *defrag_end,
     969                 :            :                                int compress)
     970                 :            : {
     971                 :            :         struct extent_map *em;
     972                 :            :         int ret = 1;
     973                 :            :         bool next_mergeable = true;
     974                 :            : 
     975                 :            :         /*
     976                 :            :          * make sure that once we start defragging an extent, we keep on
     977                 :            :          * defragging it
     978                 :            :          */
     979         [ #  # ]:          0 :         if (start < *defrag_end)
     980                 :            :                 return 1;
     981                 :            : 
     982                 :          0 :         *skip = 0;
     983                 :            : 
     984                 :          0 :         em = defrag_lookup_extent(inode, start);
     985         [ #  # ]:          0 :         if (!em)
     986                 :            :                 return 0;
     987                 :            : 
     988                 :            :         /* this will cover holes, and inline extents */
     989         [ #  # ]:          0 :         if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
     990                 :            :                 ret = 0;
     991                 :            :                 goto out;
     992                 :            :         }
     993                 :            : 
     994                 :          0 :         next_mergeable = defrag_check_next_extent(inode, em);
     995                 :            : 
     996                 :            :         /*
     997                 :            :          * we hit a real extent, if it is big or the next extent is not a
     998                 :            :          * real extent, don't bother defragging it
     999                 :            :          */
    1000 [ #  # ][ #  # ]:          0 :         if (!compress && (*last_len == 0 || *last_len >= thresh) &&
         [ #  # ][ #  # ]
    1001         [ #  # ]:          0 :             (em->len >= thresh || !next_mergeable))
    1002                 :            :                 ret = 0;
    1003                 :            : out:
    1004                 :            :         /*
    1005                 :            :          * last_len ends up being a counter of how many bytes we've defragged.
    1006                 :            :          * every time we choose not to defrag an extent, we reset *last_len
    1007                 :            :          * so that the next tiny extent will force a defrag.
    1008                 :            :          *
    1009                 :            :          * The end result of this is that tiny extents before a single big
    1010                 :            :          * extent will force at least part of that big extent to be defragged.
    1011                 :            :          */
    1012         [ #  # ]:          0 :         if (ret) {
    1013                 :          0 :                 *defrag_end = extent_map_end(em);
    1014                 :            :         } else {
    1015                 :          0 :                 *last_len = 0;
    1016                 :          0 :                 *skip = extent_map_end(em);
    1017                 :          0 :                 *defrag_end = 0;
    1018                 :            :         }
    1019                 :            : 
    1020                 :          0 :         free_extent_map(em);
    1021                 :          0 :         return ret;
    1022                 :            : }
    1023                 :            : 
    1024                 :            : /*
    1025                 :            :  * it doesn't do much good to defrag one or two pages
    1026                 :            :  * at a time.  This pulls in a nice chunk of pages
    1027                 :            :  * to COW and defrag.
    1028                 :            :  *
    1029                 :            :  * It also makes sure the delalloc code has enough
    1030                 :            :  * dirty data to avoid making new small extents as part
    1031                 :            :  * of the defrag
    1032                 :            :  *
    1033                 :            :  * It's a good idea to start RA on this range
    1034                 :            :  * before calling this.
    1035                 :            :  */
    1036                 :          0 : static int cluster_pages_for_defrag(struct inode *inode,
    1037                 :            :                                     struct page **pages,
    1038                 :            :                                     unsigned long start_index,
    1039                 :            :                                     unsigned long num_pages)
    1040                 :            : {
    1041                 :            :         unsigned long file_end;
    1042                 :          0 :         u64 isize = i_size_read(inode);
    1043                 :            :         u64 page_start;
    1044                 :            :         u64 page_end;
    1045                 :            :         u64 page_cnt;
    1046                 :            :         int ret;
    1047                 :            :         int i;
    1048                 :            :         int i_done;
    1049                 :            :         struct btrfs_ordered_extent *ordered;
    1050                 :          0 :         struct extent_state *cached_state = NULL;
    1051                 :            :         struct extent_io_tree *tree;
    1052                 :          0 :         gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
    1053                 :            : 
    1054                 :          0 :         file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
    1055         [ #  # ]:          0 :         if (!isize || start_index > file_end)
    1056                 :            :                 return 0;
    1057                 :            : 
    1058                 :          0 :         page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
    1059                 :            : 
    1060                 :          0 :         ret = btrfs_delalloc_reserve_space(inode,
    1061                 :            :                                            page_cnt << PAGE_CACHE_SHIFT);
    1062         [ #  # ]:          0 :         if (ret)
    1063                 :            :                 return ret;
    1064                 :            :         i_done = 0;
    1065                 :          0 :         tree = &BTRFS_I(inode)->io_tree;
    1066                 :            : 
    1067                 :            :         /* step one, lock all the pages */
    1068         [ #  # ]:          0 :         for (i = 0; i < page_cnt; i++) {
    1069                 :          0 :                 struct page *page;
    1070                 :            : again:
    1071                 :          0 :                 page = find_or_create_page(inode->i_mapping,
    1072                 :            :                                            start_index + i, mask);
    1073         [ #  # ]:          0 :                 if (!page)
    1074                 :            :                         break;
    1075                 :            : 
    1076                 :          0 :                 page_start = page_offset(page);
    1077                 :          0 :                 page_end = page_start + PAGE_CACHE_SIZE - 1;
    1078                 :            :                 while (1) {
    1079                 :          0 :                         lock_extent(tree, page_start, page_end);
    1080                 :          0 :                         ordered = btrfs_lookup_ordered_extent(inode,
    1081                 :            :                                                               page_start);
    1082                 :          0 :                         unlock_extent(tree, page_start, page_end);
    1083         [ #  # ]:          0 :                         if (!ordered)
    1084                 :            :                                 break;
    1085                 :            : 
    1086                 :          0 :                         unlock_page(page);
    1087                 :          0 :                         btrfs_start_ordered_extent(inode, ordered, 1);
    1088                 :          0 :                         btrfs_put_ordered_extent(ordered);
    1089                 :            :                         lock_page(page);
    1090                 :            :                         /*
    1091                 :            :                          * we unlocked the page above, so we need check if
    1092                 :            :                          * it was released or not.
    1093                 :            :                          */
    1094         [ #  # ]:          0 :                         if (page->mapping != inode->i_mapping) {
    1095                 :          0 :                                 unlock_page(page);
    1096                 :          0 :                                 page_cache_release(page);
    1097                 :          0 :                                 goto again;
    1098                 :            :                         }
    1099                 :            :                 }
    1100                 :            : 
    1101         [ #  # ]:          0 :                 if (!PageUptodate(page)) {
    1102                 :          0 :                         btrfs_readpage(NULL, page);
    1103                 :            :                         lock_page(page);
    1104         [ #  # ]:          0 :                         if (!PageUptodate(page)) {
    1105                 :          0 :                                 unlock_page(page);
    1106                 :          0 :                                 page_cache_release(page);
    1107                 :            :                                 ret = -EIO;
    1108                 :          0 :                                 break;
    1109                 :            :                         }
    1110                 :            :                 }
    1111                 :            : 
    1112         [ #  # ]:          0 :                 if (page->mapping != inode->i_mapping) {
    1113                 :          0 :                         unlock_page(page);
    1114                 :          0 :                         page_cache_release(page);
    1115                 :          0 :                         goto again;
    1116                 :            :                 }
    1117                 :            : 
    1118                 :          0 :                 pages[i] = page;
    1119                 :          0 :                 i_done++;
    1120                 :            :         }
    1121         [ #  # ]:          0 :         if (!i_done || ret)
    1122                 :            :                 goto out;
    1123                 :            : 
    1124         [ #  # ]:          0 :         if (!(inode->i_sb->s_flags & MS_ACTIVE))
    1125                 :            :                 goto out;
    1126                 :            : 
    1127                 :            :         /*
    1128                 :            :          * so now we have a nice long stream of locked
    1129                 :            :          * and up to date pages, lets wait on them
    1130                 :            :          */
    1131         [ #  # ]:          0 :         for (i = 0; i < i_done; i++)
    1132                 :          0 :                 wait_on_page_writeback(pages[i]);
    1133                 :            : 
    1134                 :          0 :         page_start = page_offset(pages[0]);
    1135                 :          0 :         page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
    1136                 :            : 
    1137                 :          0 :         lock_extent_bits(&BTRFS_I(inode)->io_tree,
    1138                 :            :                          page_start, page_end - 1, 0, &cached_state);
    1139                 :          0 :         clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
    1140                 :            :                           page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
    1141                 :            :                           EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
    1142                 :            :                           &cached_state, GFP_NOFS);
    1143                 :            : 
    1144         [ #  # ]:          0 :         if (i_done != page_cnt) {
    1145                 :            :                 spin_lock(&BTRFS_I(inode)->lock);
    1146                 :          0 :                 BTRFS_I(inode)->outstanding_extents++;
    1147                 :            :                 spin_unlock(&BTRFS_I(inode)->lock);
    1148                 :          0 :                 btrfs_delalloc_release_space(inode,
    1149                 :          0 :                                      (page_cnt - i_done) << PAGE_CACHE_SHIFT);
    1150                 :            :         }
    1151                 :            : 
    1152                 :            : 
    1153                 :          0 :         set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
    1154                 :            :                           &cached_state, GFP_NOFS);
    1155                 :            : 
    1156                 :          0 :         unlock_extent_cached(&BTRFS_I(inode)->io_tree,
    1157                 :            :                              page_start, page_end - 1, &cached_state,
    1158                 :            :                              GFP_NOFS);
    1159                 :            : 
    1160         [ #  # ]:          0 :         for (i = 0; i < i_done; i++) {
    1161                 :          0 :                 clear_page_dirty_for_io(pages[i]);
    1162                 :          0 :                 ClearPageChecked(pages[i]);
    1163                 :          0 :                 set_page_extent_mapped(pages[i]);
    1164                 :          0 :                 set_page_dirty(pages[i]);
    1165                 :          0 :                 unlock_page(pages[i]);
    1166                 :          0 :                 page_cache_release(pages[i]);
    1167                 :            :         }
    1168                 :            :         return i_done;
    1169                 :            : out:
    1170         [ #  # ]:          0 :         for (i = 0; i < i_done; i++) {
    1171                 :          0 :                 unlock_page(pages[i]);
    1172                 :          0 :                 page_cache_release(pages[i]);
    1173                 :            :         }
    1174                 :          0 :         btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
    1175                 :          0 :         return ret;
    1176                 :            : 
    1177                 :            : }
    1178                 :            : 
    1179                 :          0 : int btrfs_defrag_file(struct inode *inode, struct file *file,
    1180                 :            :                       struct btrfs_ioctl_defrag_range_args *range,
    1181                 :            :                       u64 newer_than, unsigned long max_to_defrag)
    1182                 :            : {
    1183                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    1184                 :            :         struct file_ra_state *ra = NULL;
    1185                 :            :         unsigned long last_index;
    1186                 :          0 :         u64 isize = i_size_read(inode);
    1187                 :          0 :         u64 last_len = 0;
    1188                 :          0 :         u64 skip = 0;
    1189                 :          0 :         u64 defrag_end = 0;
    1190                 :          0 :         u64 newer_off = range->start;
    1191                 :            :         unsigned long i;
    1192                 :            :         unsigned long ra_index = 0;
    1193                 :            :         int ret;
    1194                 :            :         int defrag_count = 0;
    1195                 :            :         int compress_type = BTRFS_COMPRESS_ZLIB;
    1196                 :          0 :         int extent_thresh = range->extent_thresh;
    1197                 :            :         unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
    1198                 :            :         unsigned long cluster = max_cluster;
    1199                 :            :         u64 new_align = ~((u64)128 * 1024 - 1);
    1200                 :            :         struct page **pages = NULL;
    1201                 :            : 
    1202         [ #  # ]:          0 :         if (isize == 0)
    1203                 :            :                 return 0;
    1204                 :            : 
    1205         [ #  # ]:          0 :         if (range->start >= isize)
    1206                 :            :                 return -EINVAL;
    1207                 :            : 
    1208         [ #  # ]:          0 :         if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
    1209         [ #  # ]:          0 :                 if (range->compress_type > BTRFS_COMPRESS_TYPES)
    1210                 :            :                         return -EINVAL;
    1211         [ #  # ]:          0 :                 if (range->compress_type)
    1212                 :          0 :                         compress_type = range->compress_type;
    1213                 :            :         }
    1214                 :            : 
    1215         [ #  # ]:          0 :         if (extent_thresh == 0)
    1216                 :            :                 extent_thresh = 256 * 1024;
    1217                 :            : 
    1218                 :            :         /*
    1219                 :            :          * if we were not given a file, allocate a readahead
    1220                 :            :          * context
    1221                 :            :          */
    1222         [ #  # ]:          0 :         if (!file) {
    1223                 :            :                 ra = kzalloc(sizeof(*ra), GFP_NOFS);
    1224         [ #  # ]:          0 :                 if (!ra)
    1225                 :            :                         return -ENOMEM;
    1226                 :          0 :                 file_ra_state_init(ra, inode->i_mapping);
    1227                 :            :         } else {
    1228                 :          0 :                 ra = &file->f_ra;
    1229                 :            :         }
    1230                 :            : 
    1231                 :            :         pages = kmalloc_array(max_cluster, sizeof(struct page *),
    1232                 :            :                         GFP_NOFS);
    1233         [ #  # ]:          0 :         if (!pages) {
    1234                 :            :                 ret = -ENOMEM;
    1235                 :            :                 goto out_ra;
    1236                 :            :         }
    1237                 :            : 
    1238                 :            :         /* find the last page to defrag */
    1239         [ #  # ]:          0 :         if (range->start + range->len > range->start) {
    1240                 :          0 :                 last_index = min_t(u64, isize - 1,
    1241                 :          0 :                          range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
    1242                 :            :         } else {
    1243                 :          0 :                 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
    1244                 :            :         }
    1245                 :            : 
    1246         [ #  # ]:          0 :         if (newer_than) {
    1247                 :          0 :                 ret = find_new_extents(root, inode, newer_than,
    1248                 :            :                                        &newer_off, 64 * 1024);
    1249         [ #  # ]:          0 :                 if (!ret) {
    1250                 :          0 :                         range->start = newer_off;
    1251                 :            :                         /*
    1252                 :            :                          * we always align our defrag to help keep
    1253                 :            :                          * the extents in the file evenly spaced
    1254                 :            :                          */
    1255                 :          0 :                         i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
    1256                 :            :                 } else
    1257                 :            :                         goto out_ra;
    1258                 :            :         } else {
    1259                 :          0 :                 i = range->start >> PAGE_CACHE_SHIFT;
    1260                 :            :         }
    1261         [ #  # ]:          0 :         if (!max_to_defrag)
    1262                 :          0 :                 max_to_defrag = last_index + 1;
    1263                 :            : 
    1264                 :            :         /*
    1265                 :            :          * make writeback starts from i, so the defrag range can be
    1266                 :            :          * written sequentially.
    1267                 :            :          */
    1268         [ #  # ]:          0 :         if (i < inode->i_mapping->writeback_index)
    1269                 :          0 :                 inode->i_mapping->writeback_index = i;
    1270                 :            : 
    1271 [ #  # ][ #  # ]:          0 :         while (i <= last_index && defrag_count < max_to_defrag &&
    1272                 :          0 :                (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
    1273                 :            :                 PAGE_CACHE_SHIFT)) {
    1274                 :            :                 /*
    1275                 :            :                  * make sure we stop running if someone unmounts
    1276                 :            :                  * the FS
    1277                 :            :                  */
    1278         [ #  # ]:          0 :                 if (!(inode->i_sb->s_flags & MS_ACTIVE))
    1279                 :            :                         break;
    1280                 :            : 
    1281         [ #  # ]:          0 :                 if (btrfs_defrag_cancelled(root->fs_info)) {
    1282                 :          0 :                         printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
    1283                 :            :                         ret = -EAGAIN;
    1284                 :          0 :                         break;
    1285                 :            :                 }
    1286                 :            : 
    1287         [ #  # ]:          0 :                 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
    1288                 :            :                                          extent_thresh, &last_len, &skip,
    1289                 :          0 :                                          &defrag_end, range->flags &
    1290                 :            :                                          BTRFS_DEFRAG_RANGE_COMPRESS)) {
    1291                 :            :                         unsigned long next;
    1292                 :            :                         /*
    1293                 :            :                          * the should_defrag function tells us how much to skip
    1294                 :            :                          * bump our counter by the suggested amount
    1295                 :            :                          */
    1296                 :          0 :                         next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
    1297                 :          0 :                         i = max(i + 1, next);
    1298                 :          0 :                         continue;
    1299                 :            :                 }
    1300                 :            : 
    1301         [ #  # ]:          0 :                 if (!newer_than) {
    1302                 :          0 :                         cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
    1303                 :            :                                    PAGE_CACHE_SHIFT) - i;
    1304                 :          0 :                         cluster = min(cluster, max_cluster);
    1305                 :            :                 } else {
    1306                 :            :                         cluster = max_cluster;
    1307                 :            :                 }
    1308                 :            : 
    1309         [ #  # ]:          0 :                 if (i + cluster > ra_index) {
    1310                 :          0 :                         ra_index = max(i, ra_index);
    1311                 :          0 :                         btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
    1312                 :            :                                        cluster);
    1313                 :          0 :                         ra_index += max_cluster;
    1314                 :            :                 }
    1315                 :            : 
    1316                 :          0 :                 mutex_lock(&inode->i_mutex);
    1317         [ #  # ]:          0 :                 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
    1318                 :          0 :                         BTRFS_I(inode)->force_compress = compress_type;
    1319                 :          0 :                 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
    1320         [ #  # ]:          0 :                 if (ret < 0) {
    1321                 :          0 :                         mutex_unlock(&inode->i_mutex);
    1322                 :          0 :                         goto out_ra;
    1323                 :            :                 }
    1324                 :            : 
    1325                 :          0 :                 defrag_count += ret;
    1326                 :          0 :                 balance_dirty_pages_ratelimited(inode->i_mapping);
    1327                 :          0 :                 mutex_unlock(&inode->i_mutex);
    1328                 :            : 
    1329         [ #  # ]:          0 :                 if (newer_than) {
    1330         [ #  # ]:          0 :                         if (newer_off == (u64)-1)
    1331                 :            :                                 break;
    1332                 :            : 
    1333         [ #  # ]:          0 :                         if (ret > 0)
    1334                 :          0 :                                 i += ret;
    1335                 :            : 
    1336                 :          0 :                         newer_off = max(newer_off + 1,
    1337                 :            :                                         (u64)i << PAGE_CACHE_SHIFT);
    1338                 :            : 
    1339                 :          0 :                         ret = find_new_extents(root, inode,
    1340                 :            :                                                newer_than, &newer_off,
    1341                 :            :                                                64 * 1024);
    1342         [ #  # ]:          0 :                         if (!ret) {
    1343                 :          0 :                                 range->start = newer_off;
    1344                 :          0 :                                 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
    1345                 :            :                         } else {
    1346                 :            :                                 break;
    1347                 :            :                         }
    1348                 :            :                 } else {
    1349         [ #  # ]:          0 :                         if (ret > 0) {
    1350                 :          0 :                                 i += ret;
    1351                 :          0 :                                 last_len += ret << PAGE_CACHE_SHIFT;
    1352                 :            :                         } else {
    1353                 :          0 :                                 i++;
    1354                 :          0 :                                 last_len = 0;
    1355                 :            :                         }
    1356                 :            :                 }
    1357                 :            :         }
    1358                 :            : 
    1359         [ #  # ]:          0 :         if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
    1360                 :          0 :                 filemap_flush(inode->i_mapping);
    1361                 :            : 
    1362         [ #  # ]:          0 :         if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
    1363                 :            :                 /* the filemap_flush will queue IO into the worker threads, but
    1364                 :            :                  * we have to make sure the IO is actually started and that
    1365                 :            :                  * ordered extents get created before we return
    1366                 :            :                  */
    1367                 :          0 :                 atomic_inc(&root->fs_info->async_submit_draining);
    1368 [ #  # ][ #  # ]:          0 :                 while (atomic_read(&root->fs_info->nr_async_submits) ||
    1369                 :          0 :                       atomic_read(&root->fs_info->async_delalloc_pages)) {
    1370 [ #  # ][ #  # ]:          0 :                         wait_event(root->fs_info->async_submit_wait,
         [ #  # ][ #  # ]
    1371                 :            :                            (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
    1372                 :            :                             atomic_read(&root->fs_info->async_delalloc_pages) == 0));
    1373                 :            :                 }
    1374                 :          0 :                 atomic_dec(&root->fs_info->async_submit_draining);
    1375                 :            :         }
    1376                 :            : 
    1377         [ #  # ]:          0 :         if (range->compress_type == BTRFS_COMPRESS_LZO) {
    1378                 :          0 :                 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
    1379                 :            :         }
    1380                 :            : 
    1381                 :            :         ret = defrag_count;
    1382                 :            : 
    1383                 :            : out_ra:
    1384         [ #  # ]:          0 :         if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
    1385                 :          0 :                 mutex_lock(&inode->i_mutex);
    1386                 :          0 :                 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
    1387                 :          0 :                 mutex_unlock(&inode->i_mutex);
    1388                 :            :         }
    1389         [ #  # ]:          0 :         if (!file)
    1390                 :          0 :                 kfree(ra);
    1391                 :          0 :         kfree(pages);
    1392                 :          0 :         return ret;
    1393                 :            : }
    1394                 :            : 
    1395                 :          0 : static noinline int btrfs_ioctl_resize(struct file *file,
    1396                 :            :                                         void __user *arg)
    1397                 :            : {
    1398                 :            :         u64 new_size;
    1399                 :            :         u64 old_size;
    1400                 :            :         u64 devid = 1;
    1401                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    1402                 :            :         struct btrfs_ioctl_vol_args *vol_args;
    1403                 :            :         struct btrfs_trans_handle *trans;
    1404                 :            :         struct btrfs_device *device = NULL;
    1405                 :            :         char *sizestr;
    1406                 :            :         char *devstr = NULL;
    1407                 :            :         int ret = 0;
    1408                 :            :         int mod = 0;
    1409                 :            : 
    1410         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    1411                 :            :                 return -EPERM;
    1412                 :            : 
    1413                 :          0 :         ret = mnt_want_write_file(file);
    1414         [ #  # ]:          0 :         if (ret)
    1415                 :            :                 return ret;
    1416                 :            : 
    1417         [ #  # ]:          0 :         if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
    1418                 :            :                         1)) {
    1419                 :          0 :                 mnt_drop_write_file(file);
    1420                 :          0 :                 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
    1421                 :            :         }
    1422                 :            : 
    1423                 :          0 :         mutex_lock(&root->fs_info->volume_mutex);
    1424                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    1425         [ #  # ]:          0 :         if (IS_ERR(vol_args)) {
    1426                 :            :                 ret = PTR_ERR(vol_args);
    1427                 :          0 :                 goto out;
    1428                 :            :         }
    1429                 :            : 
    1430                 :          0 :         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
    1431                 :            : 
    1432                 :          0 :         sizestr = vol_args->name;
    1433                 :          0 :         devstr = strchr(sizestr, ':');
    1434         [ #  # ]:          0 :         if (devstr) {
    1435                 :            :                 char *end;
    1436                 :          0 :                 sizestr = devstr + 1;
    1437                 :          0 :                 *devstr = '\0';
    1438                 :            :                 devstr = vol_args->name;
    1439                 :          0 :                 devid = simple_strtoull(devstr, &end, 10);
    1440         [ #  # ]:          0 :                 if (!devid) {
    1441                 :            :                         ret = -EINVAL;
    1442                 :          0 :                         goto out_free;
    1443                 :            :                 }
    1444                 :          0 :                 btrfs_info(root->fs_info, "resizing devid %llu", devid);
    1445                 :            :         }
    1446                 :            : 
    1447                 :          0 :         device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
    1448         [ #  # ]:          0 :         if (!device) {
    1449                 :          0 :                 btrfs_info(root->fs_info, "resizer unable to find device %llu",
    1450                 :            :                        devid);
    1451                 :            :                 ret = -ENODEV;
    1452                 :          0 :                 goto out_free;
    1453                 :            :         }
    1454                 :            : 
    1455         [ #  # ]:          0 :         if (!device->writeable) {
    1456                 :          0 :                 btrfs_info(root->fs_info,
    1457                 :            :                            "resizer unable to apply on readonly device %llu",
    1458                 :            :                        devid);
    1459                 :            :                 ret = -EPERM;
    1460                 :          0 :                 goto out_free;
    1461                 :            :         }
    1462                 :            : 
    1463         [ #  # ]:          0 :         if (!strcmp(sizestr, "max"))
    1464                 :          0 :                 new_size = device->bdev->bd_inode->i_size;
    1465                 :            :         else {
    1466         [ #  # ]:          0 :                 if (sizestr[0] == '-') {
    1467                 :            :                         mod = -1;
    1468                 :          0 :                         sizestr++;
    1469         [ #  # ]:          0 :                 } else if (sizestr[0] == '+') {
    1470                 :            :                         mod = 1;
    1471                 :          0 :                         sizestr++;
    1472                 :            :                 }
    1473                 :          0 :                 new_size = memparse(sizestr, NULL);
    1474         [ #  # ]:          0 :                 if (new_size == 0) {
    1475                 :            :                         ret = -EINVAL;
    1476                 :            :                         goto out_free;
    1477                 :            :                 }
    1478                 :            :         }
    1479                 :            : 
    1480         [ #  # ]:          0 :         if (device->is_tgtdev_for_dev_replace) {
    1481                 :            :                 ret = -EPERM;
    1482                 :            :                 goto out_free;
    1483                 :            :         }
    1484                 :            : 
    1485                 :          0 :         old_size = device->total_bytes;
    1486                 :            : 
    1487         [ #  # ]:          0 :         if (mod < 0) {
    1488         [ #  # ]:          0 :                 if (new_size > old_size) {
    1489                 :            :                         ret = -EINVAL;
    1490                 :            :                         goto out_free;
    1491                 :            :                 }
    1492                 :          0 :                 new_size = old_size - new_size;
    1493         [ #  # ]:          0 :         } else if (mod > 0) {
    1494         [ #  # ]:          0 :                 if (new_size > ULLONG_MAX - old_size) {
    1495                 :            :                         ret = -EINVAL;
    1496                 :            :                         goto out_free;
    1497                 :            :                 }
    1498                 :          0 :                 new_size = old_size + new_size;
    1499                 :            :         }
    1500                 :            : 
    1501         [ #  # ]:          0 :         if (new_size < 256 * 1024 * 1024) {
    1502                 :            :                 ret = -EINVAL;
    1503                 :            :                 goto out_free;
    1504                 :            :         }
    1505         [ #  # ]:          0 :         if (new_size > device->bdev->bd_inode->i_size) {
    1506                 :            :                 ret = -EFBIG;
    1507                 :            :                 goto out_free;
    1508                 :            :         }
    1509                 :            : 
    1510 [ #  # ][ #  # ]:          0 :         do_div(new_size, root->sectorsize);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
    1511                 :          0 :         new_size *= root->sectorsize;
    1512                 :            : 
    1513                 :          0 :         printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
    1514                 :            :                       rcu_str_deref(device->name), new_size);
    1515                 :            : 
    1516         [ #  # ]:          0 :         if (new_size > old_size) {
    1517                 :          0 :                 trans = btrfs_start_transaction(root, 0);
    1518         [ #  # ]:          0 :                 if (IS_ERR(trans)) {
    1519                 :            :                         ret = PTR_ERR(trans);
    1520                 :          0 :                         goto out_free;
    1521                 :            :                 }
    1522                 :          0 :                 ret = btrfs_grow_device(trans, device, new_size);
    1523                 :          0 :                 btrfs_commit_transaction(trans, root);
    1524         [ #  # ]:          0 :         } else if (new_size < old_size) {
    1525                 :          0 :                 ret = btrfs_shrink_device(device, new_size);
    1526                 :            :         } /* equal, nothing need to do */
    1527                 :            : 
    1528                 :            : out_free:
    1529                 :          0 :         kfree(vol_args);
    1530                 :            : out:
    1531                 :          0 :         mutex_unlock(&root->fs_info->volume_mutex);
    1532                 :          0 :         atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
    1533                 :          0 :         mnt_drop_write_file(file);
    1534                 :          0 :         return ret;
    1535                 :            : }
    1536                 :            : 
    1537                 :          0 : static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
    1538                 :            :                                 char *name, unsigned long fd, int subvol,
    1539                 :            :                                 u64 *transid, bool readonly,
    1540                 :            :                                 struct btrfs_qgroup_inherit *inherit)
    1541                 :            : {
    1542                 :            :         int namelen;
    1543                 :            :         int ret = 0;
    1544                 :            : 
    1545                 :          0 :         ret = mnt_want_write_file(file);
    1546         [ #  # ]:          0 :         if (ret)
    1547                 :            :                 goto out;
    1548                 :            : 
    1549                 :          0 :         namelen = strlen(name);
    1550         [ #  # ]:          0 :         if (strchr(name, '/')) {
    1551                 :            :                 ret = -EINVAL;
    1552                 :            :                 goto out_drop_write;
    1553                 :            :         }
    1554                 :            : 
    1555 [ #  # ][ #  # ]:          0 :         if (name[0] == '.' &&
    1556 [ #  # ][ #  # ]:          0 :            (namelen == 1 || (name[1] == '.' && namelen == 2))) {
    1557                 :            :                 ret = -EEXIST;
    1558                 :            :                 goto out_drop_write;
    1559                 :            :         }
    1560                 :            : 
    1561         [ #  # ]:          0 :         if (subvol) {
    1562                 :          0 :                 ret = btrfs_mksubvol(&file->f_path, name, namelen,
    1563                 :            :                                      NULL, transid, readonly, inherit);
    1564                 :            :         } else {
    1565                 :            :                 struct fd src = fdget(fd);
    1566                 :            :                 struct inode *src_inode;
    1567         [ #  # ]:          0 :                 if (!src.file) {
    1568                 :            :                         ret = -EINVAL;
    1569                 :            :                         goto out_drop_write;
    1570                 :            :                 }
    1571                 :            : 
    1572                 :            :                 src_inode = file_inode(src.file);
    1573         [ #  # ]:          0 :                 if (src_inode->i_sb != file_inode(file)->i_sb) {
    1574                 :          0 :                         btrfs_info(BTRFS_I(src_inode)->root->fs_info,
    1575                 :            :                                    "Snapshot src from another FS");
    1576                 :            :                         ret = -EINVAL;
    1577         [ #  # ]:          0 :                 } else if (!inode_owner_or_capable(src_inode)) {
    1578                 :            :                         /*
    1579                 :            :                          * Subvolume creation is not restricted, but snapshots
    1580                 :            :                          * are limited to own subvolumes only
    1581                 :            :                          */
    1582                 :            :                         ret = -EPERM;
    1583                 :            :                 } else {
    1584                 :          0 :                         ret = btrfs_mksubvol(&file->f_path, name, namelen,
    1585                 :            :                                              BTRFS_I(src_inode)->root,
    1586                 :            :                                              transid, readonly, inherit);
    1587                 :            :                 }
    1588                 :            :                 fdput(src);
    1589                 :            :         }
    1590                 :            : out_drop_write:
    1591                 :          0 :         mnt_drop_write_file(file);
    1592                 :            : out:
    1593                 :          0 :         return ret;
    1594                 :            : }
    1595                 :            : 
    1596                 :          0 : static noinline int btrfs_ioctl_snap_create(struct file *file,
    1597                 :            :                                             void __user *arg, int subvol)
    1598                 :            : {
    1599                 :            :         struct btrfs_ioctl_vol_args *vol_args;
    1600                 :            :         int ret;
    1601                 :            : 
    1602                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    1603         [ #  # ]:          0 :         if (IS_ERR(vol_args))
    1604                 :          0 :                 return PTR_ERR(vol_args);
    1605                 :          0 :         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
    1606                 :            : 
    1607                 :          0 :         ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
    1608                 :          0 :                                               vol_args->fd, subvol,
    1609                 :            :                                               NULL, false, NULL);
    1610                 :            : 
    1611                 :          0 :         kfree(vol_args);
    1612                 :          0 :         return ret;
    1613                 :            : }
    1614                 :            : 
    1615                 :          0 : static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
    1616                 :            :                                                void __user *arg, int subvol)
    1617                 :            : {
    1618                 :            :         struct btrfs_ioctl_vol_args_v2 *vol_args;
    1619                 :            :         int ret;
    1620                 :          0 :         u64 transid = 0;
    1621                 :            :         u64 *ptr = NULL;
    1622                 :            :         bool readonly = false;
    1623                 :            :         struct btrfs_qgroup_inherit *inherit = NULL;
    1624                 :            : 
    1625                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    1626         [ #  # ]:          0 :         if (IS_ERR(vol_args))
    1627                 :          0 :                 return PTR_ERR(vol_args);
    1628                 :          0 :         vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
    1629                 :            : 
    1630         [ #  # ]:          0 :         if (vol_args->flags &
    1631                 :            :             ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
    1632                 :            :               BTRFS_SUBVOL_QGROUP_INHERIT)) {
    1633                 :            :                 ret = -EOPNOTSUPP;
    1634                 :            :                 goto out;
    1635                 :            :         }
    1636                 :            : 
    1637         [ #  # ]:          0 :         if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
    1638                 :            :                 ptr = &transid;
    1639         [ #  # ]:          0 :         if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
    1640                 :            :                 readonly = true;
    1641         [ #  # ]:          0 :         if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
    1642         [ #  # ]:          0 :                 if (vol_args->size > PAGE_CACHE_SIZE) {
    1643                 :            :                         ret = -EINVAL;
    1644                 :            :                         goto out;
    1645                 :            :                 }
    1646                 :          0 :                 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
    1647         [ #  # ]:          0 :                 if (IS_ERR(inherit)) {
    1648                 :            :                         ret = PTR_ERR(inherit);
    1649                 :          0 :                         goto out;
    1650                 :            :                 }
    1651                 :            :         }
    1652                 :            : 
    1653                 :          0 :         ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
    1654                 :          0 :                                               vol_args->fd, subvol, ptr,
    1655                 :            :                                               readonly, inherit);
    1656                 :            : 
    1657 [ #  # ][ #  # ]:          0 :         if (ret == 0 && ptr &&
    1658                 :          0 :             copy_to_user(arg +
    1659                 :            :                          offsetof(struct btrfs_ioctl_vol_args_v2,
    1660                 :            :                                   transid), ptr, sizeof(*ptr)))
    1661                 :            :                 ret = -EFAULT;
    1662                 :            : out:
    1663                 :          0 :         kfree(vol_args);
    1664                 :          0 :         kfree(inherit);
    1665                 :          0 :         return ret;
    1666                 :            : }
    1667                 :            : 
    1668                 :          0 : static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
    1669                 :            :                                                 void __user *arg)
    1670                 :            : {
    1671                 :            :         struct inode *inode = file_inode(file);
    1672                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    1673                 :            :         int ret = 0;
    1674                 :          0 :         u64 flags = 0;
    1675                 :            : 
    1676         [ #  # ]:          0 :         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
    1677                 :            :                 return -EINVAL;
    1678                 :            : 
    1679                 :          0 :         down_read(&root->fs_info->subvol_sem);
    1680         [ #  # ]:          0 :         if (btrfs_root_readonly(root))
    1681                 :          0 :                 flags |= BTRFS_SUBVOL_RDONLY;
    1682                 :          0 :         up_read(&root->fs_info->subvol_sem);
    1683                 :            : 
    1684         [ #  # ]:          0 :         if (copy_to_user(arg, &flags, sizeof(flags)))
    1685                 :            :                 ret = -EFAULT;
    1686                 :            : 
    1687                 :          0 :         return ret;
    1688                 :            : }
    1689                 :            : 
    1690                 :          0 : static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
    1691                 :            :                                               void __user *arg)
    1692                 :            : {
    1693                 :            :         struct inode *inode = file_inode(file);
    1694                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    1695                 :            :         struct btrfs_trans_handle *trans;
    1696                 :            :         u64 root_flags;
    1697                 :            :         u64 flags;
    1698                 :            :         int ret = 0;
    1699                 :            : 
    1700         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    1701                 :            :                 return -EPERM;
    1702                 :            : 
    1703                 :          0 :         ret = mnt_want_write_file(file);
    1704         [ #  # ]:          0 :         if (ret)
    1705                 :            :                 goto out;
    1706                 :            : 
    1707         [ #  # ]:          0 :         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
    1708                 :            :                 ret = -EINVAL;
    1709                 :            :                 goto out_drop_write;
    1710                 :            :         }
    1711                 :            : 
    1712         [ #  # ]:          0 :         if (copy_from_user(&flags, arg, sizeof(flags))) {
    1713                 :            :                 ret = -EFAULT;
    1714                 :            :                 goto out_drop_write;
    1715                 :            :         }
    1716                 :            : 
    1717         [ #  # ]:          0 :         if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
    1718                 :            :                 ret = -EINVAL;
    1719                 :            :                 goto out_drop_write;
    1720                 :            :         }
    1721                 :            : 
    1722         [ #  # ]:          0 :         if (flags & ~BTRFS_SUBVOL_RDONLY) {
    1723                 :            :                 ret = -EOPNOTSUPP;
    1724                 :            :                 goto out_drop_write;
    1725                 :            :         }
    1726                 :            : 
    1727                 :          0 :         down_write(&root->fs_info->subvol_sem);
    1728                 :            : 
    1729                 :            :         /* nothing to do */
    1730         [ #  # ]:          0 :         if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
    1731                 :            :                 goto out_drop_sem;
    1732                 :            : 
    1733                 :            :         root_flags = btrfs_root_flags(&root->root_item);
    1734         [ #  # ]:          0 :         if (flags & BTRFS_SUBVOL_RDONLY) {
    1735                 :          0 :                 btrfs_set_root_flags(&root->root_item,
    1736                 :            :                                      root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
    1737                 :            :         } else {
    1738                 :            :                 /*
    1739                 :            :                  * Block RO -> RW transition if this subvolume is involved in
    1740                 :            :                  * send
    1741                 :            :                  */
    1742                 :            :                 spin_lock(&root->root_item_lock);
    1743         [ #  # ]:          0 :                 if (root->send_in_progress == 0) {
    1744                 :          0 :                         btrfs_set_root_flags(&root->root_item,
    1745                 :            :                                      root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
    1746                 :            :                         spin_unlock(&root->root_item_lock);
    1747                 :            :                 } else {
    1748                 :            :                         spin_unlock(&root->root_item_lock);
    1749                 :          0 :                         btrfs_warn(root->fs_info,
    1750                 :            :                         "Attempt to set subvolume %llu read-write during send",
    1751                 :            :                                         root->root_key.objectid);
    1752                 :            :                         ret = -EPERM;
    1753                 :          0 :                         goto out_drop_sem;
    1754                 :            :                 }
    1755                 :            :         }
    1756                 :            : 
    1757                 :          0 :         trans = btrfs_start_transaction(root, 1);
    1758         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    1759                 :            :                 ret = PTR_ERR(trans);
    1760                 :          0 :                 goto out_reset;
    1761                 :            :         }
    1762                 :            : 
    1763                 :          0 :         ret = btrfs_update_root(trans, root->fs_info->tree_root,
    1764                 :            :                                 &root->root_key, &root->root_item);
    1765                 :            : 
    1766                 :          0 :         btrfs_commit_transaction(trans, root);
    1767                 :            : out_reset:
    1768         [ #  # ]:          0 :         if (ret)
    1769                 :            :                 btrfs_set_root_flags(&root->root_item, root_flags);
    1770                 :            : out_drop_sem:
    1771                 :          0 :         up_write(&root->fs_info->subvol_sem);
    1772                 :            : out_drop_write:
    1773                 :          0 :         mnt_drop_write_file(file);
    1774                 :            : out:
    1775                 :          0 :         return ret;
    1776                 :            : }
    1777                 :            : 
    1778                 :            : /*
    1779                 :            :  * helper to check if the subvolume references other subvolumes
    1780                 :            :  */
    1781                 :          0 : static noinline int may_destroy_subvol(struct btrfs_root *root)
    1782                 :            : {
    1783                 :            :         struct btrfs_path *path;
    1784                 :            :         struct btrfs_dir_item *di;
    1785                 :            :         struct btrfs_key key;
    1786                 :            :         u64 dir_id;
    1787                 :            :         int ret;
    1788                 :            : 
    1789                 :          0 :         path = btrfs_alloc_path();
    1790         [ #  # ]:          0 :         if (!path)
    1791                 :            :                 return -ENOMEM;
    1792                 :            : 
    1793                 :            :         /* Make sure this root isn't set as the default subvol */
    1794                 :          0 :         dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
    1795                 :          0 :         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
    1796                 :            :                                    dir_id, "default", 7, 0);
    1797 [ #  # ][ #  # ]:          0 :         if (di && !IS_ERR(di)) {
    1798                 :          0 :                 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
    1799         [ #  # ]:          0 :                 if (key.objectid == root->root_key.objectid) {
    1800                 :            :                         ret = -ENOTEMPTY;
    1801                 :            :                         goto out;
    1802                 :            :                 }
    1803                 :          0 :                 btrfs_release_path(path);
    1804                 :            :         }
    1805                 :            : 
    1806                 :          0 :         key.objectid = root->root_key.objectid;
    1807                 :          0 :         key.type = BTRFS_ROOT_REF_KEY;
    1808                 :          0 :         key.offset = (u64)-1;
    1809                 :            : 
    1810                 :          0 :         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
    1811                 :            :                                 &key, path, 0, 0);
    1812         [ #  # ]:          0 :         if (ret < 0)
    1813                 :            :                 goto out;
    1814         [ #  # ]:          0 :         BUG_ON(ret == 0);
    1815                 :            : 
    1816                 :            :         ret = 0;
    1817         [ #  # ]:          0 :         if (path->slots[0] > 0) {
    1818                 :          0 :                 path->slots[0]--;
    1819                 :          0 :                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
    1820 [ #  # ][ #  # ]:          0 :                 if (key.objectid == root->root_key.objectid &&
    1821                 :            :                     key.type == BTRFS_ROOT_REF_KEY)
    1822                 :            :                         ret = -ENOTEMPTY;
    1823                 :            :         }
    1824                 :            : out:
    1825                 :          0 :         btrfs_free_path(path);
    1826                 :          0 :         return ret;
    1827                 :            : }
    1828                 :            : 
    1829                 :          0 : static noinline int key_in_sk(struct btrfs_key *key,
    1830                 :            :                               struct btrfs_ioctl_search_key *sk)
    1831                 :            : {
    1832                 :            :         struct btrfs_key test;
    1833                 :            :         int ret;
    1834                 :            : 
    1835                 :          0 :         test.objectid = sk->min_objectid;
    1836                 :          0 :         test.type = sk->min_type;
    1837                 :          0 :         test.offset = sk->min_offset;
    1838                 :            : 
    1839                 :          0 :         ret = btrfs_comp_cpu_keys(key, &test);
    1840         [ #  # ]:          0 :         if (ret < 0)
    1841                 :            :                 return 0;
    1842                 :            : 
    1843                 :          0 :         test.objectid = sk->max_objectid;
    1844                 :          0 :         test.type = sk->max_type;
    1845                 :          0 :         test.offset = sk->max_offset;
    1846                 :            : 
    1847                 :          0 :         ret = btrfs_comp_cpu_keys(key, &test);
    1848         [ #  # ]:          0 :         if (ret > 0)
    1849                 :            :                 return 0;
    1850                 :          0 :         return 1;
    1851                 :            : }
    1852                 :            : 
    1853                 :          0 : static noinline int copy_to_sk(struct btrfs_root *root,
    1854                 :            :                                struct btrfs_path *path,
    1855                 :            :                                struct btrfs_key *key,
    1856                 :            :                                struct btrfs_ioctl_search_key *sk,
    1857                 :            :                                char *buf,
    1858                 :            :                                unsigned long *sk_offset,
    1859                 :            :                                int *num_found)
    1860                 :            : {
    1861                 :            :         u64 found_transid;
    1862                 :          0 :         struct extent_buffer *leaf;
    1863                 :            :         struct btrfs_ioctl_search_header sh;
    1864                 :            :         unsigned long item_off;
    1865                 :            :         unsigned long item_len;
    1866                 :            :         int nritems;
    1867                 :            :         int i;
    1868                 :            :         int slot;
    1869                 :            :         int ret = 0;
    1870                 :            : 
    1871                 :          0 :         leaf = path->nodes[0];
    1872                 :          0 :         slot = path->slots[0];
    1873                 :          0 :         nritems = btrfs_header_nritems(leaf);
    1874                 :            : 
    1875         [ #  # ]:          0 :         if (btrfs_header_generation(leaf) > sk->max_transid) {
    1876                 :            :                 i = nritems;
    1877                 :            :                 goto advance_key;
    1878                 :            :         }
    1879                 :            :         found_transid = btrfs_header_generation(leaf);
    1880                 :            : 
    1881         [ #  # ]:          0 :         for (i = slot; i < nritems; i++) {
    1882                 :          0 :                 item_off = btrfs_item_ptr_offset(leaf, i);
    1883                 :            :                 item_len = btrfs_item_size_nr(leaf, i);
    1884                 :            : 
    1885                 :            :                 btrfs_item_key_to_cpu(leaf, key, i);
    1886         [ #  # ]:          0 :                 if (!key_in_sk(key, sk))
    1887                 :          0 :                         continue;
    1888                 :            : 
    1889         [ #  # ]:          0 :                 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
    1890                 :            :                         item_len = 0;
    1891                 :            : 
    1892         [ #  # ]:          0 :                 if (sizeof(sh) + item_len + *sk_offset >
    1893                 :            :                     BTRFS_SEARCH_ARGS_BUFSIZE) {
    1894                 :            :                         ret = 1;
    1895                 :            :                         goto overflow;
    1896                 :            :                 }
    1897                 :            : 
    1898                 :          0 :                 sh.objectid = key->objectid;
    1899                 :          0 :                 sh.offset = key->offset;
    1900                 :          0 :                 sh.type = key->type;
    1901                 :          0 :                 sh.len = item_len;
    1902                 :          0 :                 sh.transid = found_transid;
    1903                 :            : 
    1904                 :            :                 /* copy search result header */
    1905                 :          0 :                 memcpy(buf + *sk_offset, &sh, sizeof(sh));
    1906                 :          0 :                 *sk_offset += sizeof(sh);
    1907                 :            : 
    1908         [ #  # ]:          0 :                 if (item_len) {
    1909                 :          0 :                         char *p = buf + *sk_offset;
    1910                 :            :                         /* copy the item */
    1911                 :          0 :                         read_extent_buffer(leaf, p,
    1912                 :            :                                            item_off, item_len);
    1913                 :          0 :                         *sk_offset += item_len;
    1914                 :            :                 }
    1915                 :          0 :                 (*num_found)++;
    1916                 :            : 
    1917         [ #  # ]:          0 :                 if (*num_found >= sk->nr_items)
    1918                 :            :                         break;
    1919                 :            :         }
    1920                 :            : advance_key:
    1921                 :            :         ret = 0;
    1922 [ #  # ][ #  # ]:          0 :         if (key->offset < (u64)-1 && key->offset < sk->max_offset)
    1923                 :          0 :                 key->offset++;
    1924 [ #  # ][ #  # ]:          0 :         else if (key->type < (u8)-1 && key->type < sk->max_type) {
    1925                 :          0 :                 key->offset = 0;
    1926                 :          0 :                 key->type++;
    1927 [ #  # ][ #  # ]:          0 :         } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
    1928                 :          0 :                 key->offset = 0;
    1929                 :          0 :                 key->type = 0;
    1930                 :          0 :                 key->objectid++;
    1931                 :            :         } else
    1932                 :            :                 ret = 1;
    1933                 :            : overflow:
    1934                 :          0 :         return ret;
    1935                 :            : }
    1936                 :            : 
    1937                 :          0 : static noinline int search_ioctl(struct inode *inode,
    1938                 :            :                                  struct btrfs_ioctl_search_args *args)
    1939                 :            : {
    1940                 :            :         struct btrfs_root *root;
    1941                 :            :         struct btrfs_key key;
    1942                 :          0 :         struct btrfs_path *path;
    1943                 :          0 :         struct btrfs_ioctl_search_key *sk = &args->key;
    1944                 :          0 :         struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
    1945                 :            :         int ret;
    1946                 :          0 :         int num_found = 0;
    1947                 :          0 :         unsigned long sk_offset = 0;
    1948                 :            : 
    1949                 :          0 :         path = btrfs_alloc_path();
    1950         [ #  # ]:          0 :         if (!path)
    1951                 :            :                 return -ENOMEM;
    1952                 :            : 
    1953         [ #  # ]:          0 :         if (sk->tree_id == 0) {
    1954                 :            :                 /* search the root of the inode that was passed */
    1955                 :          0 :                 root = BTRFS_I(inode)->root;
    1956                 :            :         } else {
    1957                 :          0 :                 key.objectid = sk->tree_id;
    1958                 :          0 :                 key.type = BTRFS_ROOT_ITEM_KEY;
    1959                 :          0 :                 key.offset = (u64)-1;
    1960                 :            :                 root = btrfs_read_fs_root_no_name(info, &key);
    1961         [ #  # ]:          0 :                 if (IS_ERR(root)) {
    1962                 :          0 :                         printk(KERN_ERR "BTRFS: could not find root %llu\n",
    1963                 :            :                                sk->tree_id);
    1964                 :          0 :                         btrfs_free_path(path);
    1965                 :          0 :                         return -ENOENT;
    1966                 :            :                 }
    1967                 :            :         }
    1968                 :            : 
    1969                 :          0 :         key.objectid = sk->min_objectid;
    1970                 :          0 :         key.type = sk->min_type;
    1971                 :          0 :         key.offset = sk->min_offset;
    1972                 :            : 
    1973                 :          0 :         path->keep_locks = 1;
    1974                 :            : 
    1975                 :            :         while (1) {
    1976                 :          0 :                 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
    1977         [ #  # ]:          0 :                 if (ret != 0) {
    1978         [ #  # ]:          0 :                         if (ret > 0)
    1979                 :            :                                 ret = 0;
    1980                 :            :                         goto err;
    1981                 :            :                 }
    1982                 :          0 :                 ret = copy_to_sk(root, path, &key, sk, args->buf,
    1983                 :            :                                  &sk_offset, &num_found);
    1984                 :          0 :                 btrfs_release_path(path);
    1985 [ #  # ][ #  # ]:          0 :                 if (ret || num_found >= sk->nr_items)
    1986                 :            :                         break;
    1987                 :            : 
    1988                 :            :         }
    1989                 :            :         ret = 0;
    1990                 :            : err:
    1991                 :          0 :         sk->nr_items = num_found;
    1992                 :          0 :         btrfs_free_path(path);
    1993                 :          0 :         return ret;
    1994                 :            : }
    1995                 :            : 
    1996                 :          0 : static noinline int btrfs_ioctl_tree_search(struct file *file,
    1997                 :            :                                            void __user *argp)
    1998                 :            : {
    1999                 :            :          struct btrfs_ioctl_search_args *args;
    2000                 :            :          struct inode *inode;
    2001                 :            :          int ret;
    2002                 :            : 
    2003         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2004                 :            :                 return -EPERM;
    2005                 :            : 
    2006                 :          0 :         args = memdup_user(argp, sizeof(*args));
    2007         [ #  # ]:          0 :         if (IS_ERR(args))
    2008                 :          0 :                 return PTR_ERR(args);
    2009                 :            : 
    2010                 :            :         inode = file_inode(file);
    2011                 :          0 :         ret = search_ioctl(inode, args);
    2012 [ #  # ][ #  # ]:          0 :         if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
    2013                 :            :                 ret = -EFAULT;
    2014                 :          0 :         kfree(args);
    2015                 :          0 :         return ret;
    2016                 :            : }
    2017                 :            : 
    2018                 :            : /*
    2019                 :            :  * Search INODE_REFs to identify path name of 'dirid' directory
    2020                 :            :  * in a 'tree_id' tree. and sets path name to 'name'.
    2021                 :            :  */
    2022                 :          0 : static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
    2023                 :            :                                 u64 tree_id, u64 dirid, char *name)
    2024                 :            : {
    2025                 :            :         struct btrfs_root *root;
    2026                 :            :         struct btrfs_key key;
    2027                 :            :         char *ptr;
    2028                 :            :         int ret = -1;
    2029                 :            :         int slot;
    2030                 :            :         int len;
    2031                 :            :         int total_len = 0;
    2032                 :            :         struct btrfs_inode_ref *iref;
    2033                 :            :         struct extent_buffer *l;
    2034                 :            :         struct btrfs_path *path;
    2035                 :            : 
    2036         [ #  # ]:          0 :         if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
    2037                 :          0 :                 name[0]='\0';
    2038                 :          0 :                 return 0;
    2039                 :            :         }
    2040                 :            : 
    2041                 :          0 :         path = btrfs_alloc_path();
    2042         [ #  # ]:          0 :         if (!path)
    2043                 :            :                 return -ENOMEM;
    2044                 :            : 
    2045                 :          0 :         ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
    2046                 :            : 
    2047                 :          0 :         key.objectid = tree_id;
    2048                 :          0 :         key.type = BTRFS_ROOT_ITEM_KEY;
    2049                 :          0 :         key.offset = (u64)-1;
    2050                 :            :         root = btrfs_read_fs_root_no_name(info, &key);
    2051         [ #  # ]:          0 :         if (IS_ERR(root)) {
    2052                 :          0 :                 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
    2053                 :            :                 ret = -ENOENT;
    2054                 :          0 :                 goto out;
    2055                 :            :         }
    2056                 :            : 
    2057                 :          0 :         key.objectid = dirid;
    2058                 :          0 :         key.type = BTRFS_INODE_REF_KEY;
    2059                 :          0 :         key.offset = (u64)-1;
    2060                 :            : 
    2061                 :            :         while (1) {
    2062                 :          0 :                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
    2063         [ #  # ]:          0 :                 if (ret < 0)
    2064                 :            :                         goto out;
    2065         [ #  # ]:          0 :                 else if (ret > 0) {
    2066                 :          0 :                         ret = btrfs_previous_item(root, path, dirid,
    2067                 :            :                                                   BTRFS_INODE_REF_KEY);
    2068         [ #  # ]:          0 :                         if (ret < 0)
    2069                 :            :                                 goto out;
    2070         [ #  # ]:          0 :                         else if (ret > 0) {
    2071                 :            :                                 ret = -ENOENT;
    2072                 :            :                                 goto out;
    2073                 :            :                         }
    2074                 :            :                 }
    2075                 :            : 
    2076                 :          0 :                 l = path->nodes[0];
    2077                 :          0 :                 slot = path->slots[0];
    2078                 :            :                 btrfs_item_key_to_cpu(l, &key, slot);
    2079                 :            : 
    2080                 :          0 :                 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
    2081                 :          0 :                 len = btrfs_inode_ref_name_len(l, iref);
    2082                 :          0 :                 ptr -= len + 1;
    2083                 :          0 :                 total_len += len + 1;
    2084         [ #  # ]:          0 :                 if (ptr < name) {
    2085                 :            :                         ret = -ENAMETOOLONG;
    2086                 :            :                         goto out;
    2087                 :            :                 }
    2088                 :            : 
    2089                 :          0 :                 *(ptr + len) = '/';
    2090                 :          0 :                 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
    2091                 :            : 
    2092         [ #  # ]:          0 :                 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
    2093                 :            :                         break;
    2094                 :            : 
    2095                 :          0 :                 btrfs_release_path(path);
    2096                 :          0 :                 key.objectid = key.offset;
    2097                 :          0 :                 key.offset = (u64)-1;
    2098                 :            :                 dirid = key.objectid;
    2099                 :          0 :         }
    2100                 :          0 :         memmove(name, ptr, total_len);
    2101                 :          0 :         name[total_len] = '\0';
    2102                 :            :         ret = 0;
    2103                 :            : out:
    2104                 :          0 :         btrfs_free_path(path);
    2105                 :          0 :         return ret;
    2106                 :            : }
    2107                 :            : 
    2108                 :          0 : static noinline int btrfs_ioctl_ino_lookup(struct file *file,
    2109                 :            :                                            void __user *argp)
    2110                 :            : {
    2111                 :            :          struct btrfs_ioctl_ino_lookup_args *args;
    2112                 :            :          struct inode *inode;
    2113                 :            :          int ret;
    2114                 :            : 
    2115         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2116                 :            :                 return -EPERM;
    2117                 :            : 
    2118                 :          0 :         args = memdup_user(argp, sizeof(*args));
    2119         [ #  # ]:          0 :         if (IS_ERR(args))
    2120                 :          0 :                 return PTR_ERR(args);
    2121                 :            : 
    2122                 :            :         inode = file_inode(file);
    2123                 :            : 
    2124         [ #  # ]:          0 :         if (args->treeid == 0)
    2125                 :          0 :                 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
    2126                 :            : 
    2127                 :          0 :         ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
    2128                 :            :                                         args->treeid, args->objectid,
    2129                 :          0 :                                         args->name);
    2130                 :            : 
    2131 [ #  # ][ #  # ]:          0 :         if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
    2132                 :            :                 ret = -EFAULT;
    2133                 :            : 
    2134                 :          0 :         kfree(args);
    2135                 :          0 :         return ret;
    2136                 :            : }
    2137                 :            : 
    2138                 :          0 : static noinline int btrfs_ioctl_snap_destroy(struct file *file,
    2139                 :            :                                              void __user *arg)
    2140                 :            : {
    2141                 :          0 :         struct dentry *parent = file->f_path.dentry;
    2142                 :            :         struct dentry *dentry;
    2143                 :          0 :         struct inode *dir = parent->d_inode;
    2144                 :            :         struct inode *inode;
    2145                 :          0 :         struct btrfs_root *root = BTRFS_I(dir)->root;
    2146                 :            :         struct btrfs_root *dest = NULL;
    2147                 :            :         struct btrfs_ioctl_vol_args *vol_args;
    2148                 :            :         struct btrfs_trans_handle *trans;
    2149                 :            :         struct btrfs_block_rsv block_rsv;
    2150                 :            :         u64 qgroup_reserved;
    2151                 :            :         int namelen;
    2152                 :            :         int ret;
    2153                 :            :         int err = 0;
    2154                 :            : 
    2155                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    2156         [ #  # ]:          0 :         if (IS_ERR(vol_args))
    2157                 :          0 :                 return PTR_ERR(vol_args);
    2158                 :            : 
    2159                 :          0 :         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
    2160                 :          0 :         namelen = strlen(vol_args->name);
    2161 [ #  # ][ #  # ]:          0 :         if (strchr(vol_args->name, '/') ||
    2162                 :          0 :             strncmp(vol_args->name, "..", namelen) == 0) {
    2163                 :            :                 err = -EINVAL;
    2164                 :            :                 goto out;
    2165                 :            :         }
    2166                 :            : 
    2167                 :          0 :         err = mnt_want_write_file(file);
    2168         [ #  # ]:          0 :         if (err)
    2169                 :            :                 goto out;
    2170                 :            : 
    2171                 :          0 :         err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
    2172         [ #  # ]:          0 :         if (err == -EINTR)
    2173                 :            :                 goto out_drop_write;
    2174                 :          0 :         dentry = lookup_one_len(vol_args->name, parent, namelen);
    2175         [ #  # ]:          0 :         if (IS_ERR(dentry)) {
    2176                 :            :                 err = PTR_ERR(dentry);
    2177                 :          0 :                 goto out_unlock_dir;
    2178                 :            :         }
    2179                 :            : 
    2180         [ #  # ]:          0 :         if (!dentry->d_inode) {
    2181                 :            :                 err = -ENOENT;
    2182                 :            :                 goto out_dput;
    2183                 :            :         }
    2184                 :            : 
    2185                 :            :         inode = dentry->d_inode;
    2186                 :          0 :         dest = BTRFS_I(inode)->root;
    2187         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN)) {
    2188                 :            :                 /*
    2189                 :            :                  * Regular user.  Only allow this with a special mount
    2190                 :            :                  * option, when the user has write+exec access to the
    2191                 :            :                  * subvol root, and when rmdir(2) would have been
    2192                 :            :                  * allowed.
    2193                 :            :                  *
    2194                 :            :                  * Note that this is _not_ check that the subvol is
    2195                 :            :                  * empty or doesn't contain data that we wouldn't
    2196                 :            :                  * otherwise be able to delete.
    2197                 :            :                  *
    2198                 :            :                  * Users who want to delete empty subvols should try
    2199                 :            :                  * rmdir(2).
    2200                 :            :                  */
    2201                 :            :                 err = -EPERM;
    2202         [ #  # ]:          0 :                 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
    2203                 :            :                         goto out_dput;
    2204                 :            : 
    2205                 :            :                 /*
    2206                 :            :                  * Do not allow deletion if the parent dir is the same
    2207                 :            :                  * as the dir to be deleted.  That means the ioctl
    2208                 :            :                  * must be called on the dentry referencing the root
    2209                 :            :                  * of the subvol, not a random directory contained
    2210                 :            :                  * within it.
    2211                 :            :                  */
    2212                 :            :                 err = -EINVAL;
    2213         [ #  # ]:          0 :                 if (root == dest)
    2214                 :            :                         goto out_dput;
    2215                 :            : 
    2216                 :          0 :                 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
    2217         [ #  # ]:          0 :                 if (err)
    2218                 :            :                         goto out_dput;
    2219                 :            :         }
    2220                 :            : 
    2221                 :            :         /* check if subvolume may be deleted by a user */
    2222                 :          0 :         err = btrfs_may_delete(dir, dentry, 1);
    2223         [ #  # ]:          0 :         if (err)
    2224                 :            :                 goto out_dput;
    2225                 :            : 
    2226         [ #  # ]:          0 :         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
    2227                 :            :                 err = -EINVAL;
    2228                 :            :                 goto out_dput;
    2229                 :            :         }
    2230                 :            : 
    2231                 :          0 :         mutex_lock(&inode->i_mutex);
    2232                 :          0 :         err = d_invalidate(dentry);
    2233         [ #  # ]:          0 :         if (err)
    2234                 :            :                 goto out_unlock;
    2235                 :            : 
    2236                 :          0 :         down_write(&root->fs_info->subvol_sem);
    2237                 :            : 
    2238                 :          0 :         err = may_destroy_subvol(dest);
    2239         [ #  # ]:          0 :         if (err)
    2240                 :            :                 goto out_up_write;
    2241                 :            : 
    2242                 :          0 :         btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
    2243                 :            :         /*
    2244                 :            :          * One for dir inode, two for dir entries, two for root
    2245                 :            :          * ref/backref.
    2246                 :            :          */
    2247                 :          0 :         err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
    2248                 :            :                                                5, &qgroup_reserved, true);
    2249         [ #  # ]:          0 :         if (err)
    2250                 :            :                 goto out_up_write;
    2251                 :            : 
    2252                 :          0 :         trans = btrfs_start_transaction(root, 0);
    2253         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    2254                 :            :                 err = PTR_ERR(trans);
    2255                 :          0 :                 goto out_release;
    2256                 :            :         }
    2257                 :          0 :         trans->block_rsv = &block_rsv;
    2258                 :          0 :         trans->bytes_reserved = block_rsv.size;
    2259                 :            : 
    2260                 :          0 :         ret = btrfs_unlink_subvol(trans, root, dir,
    2261                 :            :                                 dest->root_key.objectid,
    2262                 :          0 :                                 dentry->d_name.name,
    2263                 :          0 :                                 dentry->d_name.len);
    2264         [ #  # ]:          0 :         if (ret) {
    2265                 :            :                 err = ret;
    2266                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
    2267                 :          0 :                 goto out_end_trans;
    2268                 :            :         }
    2269                 :            : 
    2270                 :          0 :         btrfs_record_root_in_trans(trans, dest);
    2271                 :            : 
    2272                 :          0 :         memset(&dest->root_item.drop_progress, 0,
    2273                 :            :                 sizeof(dest->root_item.drop_progress));
    2274                 :          0 :         dest->root_item.drop_level = 0;
    2275                 :            :         btrfs_set_root_refs(&dest->root_item, 0);
    2276                 :            : 
    2277         [ #  # ]:          0 :         if (!xchg(&dest->orphan_item_inserted, 1)) {
    2278                 :          0 :                 ret = btrfs_insert_orphan_item(trans,
    2279                 :          0 :                                         root->fs_info->tree_root,
    2280                 :            :                                         dest->root_key.objectid);
    2281         [ #  # ]:          0 :                 if (ret) {
    2282                 :          0 :                         btrfs_abort_transaction(trans, root, ret);
    2283                 :            :                         err = ret;
    2284                 :          0 :                         goto out_end_trans;
    2285                 :            :                 }
    2286                 :            :         }
    2287                 :            : 
    2288                 :          0 :         ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
    2289                 :          0 :                                   dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
    2290                 :            :                                   dest->root_key.objectid);
    2291         [ #  # ]:          0 :         if (ret && ret != -ENOENT) {
    2292                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
    2293                 :            :                 err = ret;
    2294                 :          0 :                 goto out_end_trans;
    2295                 :            :         }
    2296         [ #  # ]:          0 :         if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
    2297                 :          0 :                 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
    2298                 :            :                                           dest->root_item.received_uuid,
    2299                 :            :                                           BTRFS_UUID_KEY_RECEIVED_SUBVOL,
    2300                 :            :                                           dest->root_key.objectid);
    2301         [ #  # ]:          0 :                 if (ret && ret != -ENOENT) {
    2302                 :          0 :                         btrfs_abort_transaction(trans, root, ret);
    2303                 :            :                         err = ret;
    2304                 :          0 :                         goto out_end_trans;
    2305                 :            :                 }
    2306                 :            :         }
    2307                 :            : 
    2308                 :            : out_end_trans:
    2309                 :          0 :         trans->block_rsv = NULL;
    2310                 :          0 :         trans->bytes_reserved = 0;
    2311                 :          0 :         ret = btrfs_end_transaction(trans, root);
    2312         [ #  # ]:          0 :         if (ret && !err)
    2313                 :            :                 err = ret;
    2314                 :          0 :         inode->i_flags |= S_DEAD;
    2315                 :            : out_release:
    2316                 :          0 :         btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
    2317                 :            : out_up_write:
    2318                 :          0 :         up_write(&root->fs_info->subvol_sem);
    2319                 :            : out_unlock:
    2320                 :          0 :         mutex_unlock(&inode->i_mutex);
    2321         [ #  # ]:          0 :         if (!err) {
    2322                 :          0 :                 shrink_dcache_sb(root->fs_info->sb);
    2323                 :          0 :                 btrfs_invalidate_inodes(dest);
    2324                 :          0 :                 d_delete(dentry);
    2325                 :            : 
    2326                 :            :                 /* the last ref */
    2327         [ #  # ]:          0 :                 if (dest->cache_inode) {
    2328                 :          0 :                         iput(dest->cache_inode);
    2329                 :          0 :                         dest->cache_inode = NULL;
    2330                 :            :                 }
    2331                 :            :         }
    2332                 :            : out_dput:
    2333                 :          0 :         dput(dentry);
    2334                 :            : out_unlock_dir:
    2335                 :          0 :         mutex_unlock(&dir->i_mutex);
    2336                 :            : out_drop_write:
    2337                 :          0 :         mnt_drop_write_file(file);
    2338                 :            : out:
    2339                 :          0 :         kfree(vol_args);
    2340                 :          0 :         return err;
    2341                 :            : }
    2342                 :            : 
    2343                 :          0 : static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
    2344                 :            : {
    2345                 :            :         struct inode *inode = file_inode(file);
    2346                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    2347                 :            :         struct btrfs_ioctl_defrag_range_args *range;
    2348                 :            :         int ret;
    2349                 :            : 
    2350                 :          0 :         ret = mnt_want_write_file(file);
    2351         [ #  # ]:          0 :         if (ret)
    2352                 :            :                 return ret;
    2353                 :            : 
    2354         [ #  # ]:          0 :         if (btrfs_root_readonly(root)) {
    2355                 :            :                 ret = -EROFS;
    2356                 :            :                 goto out;
    2357                 :            :         }
    2358                 :            : 
    2359      [ #  #  # ]:          0 :         switch (inode->i_mode & S_IFMT) {
    2360                 :            :         case S_IFDIR:
    2361         [ #  # ]:          0 :                 if (!capable(CAP_SYS_ADMIN)) {
    2362                 :            :                         ret = -EPERM;
    2363                 :            :                         goto out;
    2364                 :            :                 }
    2365                 :          0 :                 ret = btrfs_defrag_root(root);
    2366         [ #  # ]:          0 :                 if (ret)
    2367                 :            :                         goto out;
    2368                 :          0 :                 ret = btrfs_defrag_root(root->fs_info->extent_root);
    2369                 :          0 :                 break;
    2370                 :            :         case S_IFREG:
    2371         [ #  # ]:          0 :                 if (!(file->f_mode & FMODE_WRITE)) {
    2372                 :            :                         ret = -EINVAL;
    2373                 :            :                         goto out;
    2374                 :            :                 }
    2375                 :            : 
    2376                 :            :                 range = kzalloc(sizeof(*range), GFP_KERNEL);
    2377         [ #  # ]:          0 :                 if (!range) {
    2378                 :            :                         ret = -ENOMEM;
    2379                 :            :                         goto out;
    2380                 :            :                 }
    2381                 :            : 
    2382         [ #  # ]:          0 :                 if (argp) {
    2383         [ #  # ]:          0 :                         if (copy_from_user(range, argp,
    2384                 :            :                                            sizeof(*range))) {
    2385                 :            :                                 ret = -EFAULT;
    2386                 :          0 :                                 kfree(range);
    2387                 :          0 :                                 goto out;
    2388                 :            :                         }
    2389                 :            :                         /* compression requires us to start the IO */
    2390         [ #  # ]:          0 :                         if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
    2391                 :          0 :                                 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
    2392                 :          0 :                                 range->extent_thresh = (u32)-1;
    2393                 :            :                         }
    2394                 :            :                 } else {
    2395                 :            :                         /* the rest are all set to zero by kzalloc */
    2396                 :          0 :                         range->len = (u64)-1;
    2397                 :            :                 }
    2398                 :          0 :                 ret = btrfs_defrag_file(file_inode(file), file,
    2399                 :            :                                         range, 0, 0);
    2400         [ #  # ]:          0 :                 if (ret > 0)
    2401                 :            :                         ret = 0;
    2402                 :          0 :                 kfree(range);
    2403                 :          0 :                 break;
    2404                 :            :         default:
    2405                 :            :                 ret = -EINVAL;
    2406                 :            :         }
    2407                 :            : out:
    2408                 :          0 :         mnt_drop_write_file(file);
    2409                 :          0 :         return ret;
    2410                 :            : }
    2411                 :            : 
    2412                 :          0 : static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
    2413                 :            : {
    2414                 :            :         struct btrfs_ioctl_vol_args *vol_args;
    2415                 :            :         int ret;
    2416                 :            : 
    2417         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2418                 :            :                 return -EPERM;
    2419                 :            : 
    2420         [ #  # ]:          0 :         if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
    2421                 :            :                         1)) {
    2422                 :            :                 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
    2423                 :            :         }
    2424                 :            : 
    2425                 :          0 :         mutex_lock(&root->fs_info->volume_mutex);
    2426                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    2427         [ #  # ]:          0 :         if (IS_ERR(vol_args)) {
    2428                 :            :                 ret = PTR_ERR(vol_args);
    2429                 :          0 :                 goto out;
    2430                 :            :         }
    2431                 :            : 
    2432                 :          0 :         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
    2433                 :          0 :         ret = btrfs_init_new_device(root, vol_args->name);
    2434                 :            : 
    2435                 :          0 :         kfree(vol_args);
    2436                 :            : out:
    2437                 :          0 :         mutex_unlock(&root->fs_info->volume_mutex);
    2438                 :          0 :         atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
    2439                 :          0 :         return ret;
    2440                 :            : }
    2441                 :            : 
    2442                 :          0 : static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
    2443                 :            : {
    2444                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    2445                 :            :         struct btrfs_ioctl_vol_args *vol_args;
    2446                 :            :         int ret;
    2447                 :            : 
    2448         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2449                 :            :                 return -EPERM;
    2450                 :            : 
    2451                 :          0 :         ret = mnt_want_write_file(file);
    2452         [ #  # ]:          0 :         if (ret)
    2453                 :            :                 return ret;
    2454                 :            : 
    2455                 :          0 :         vol_args = memdup_user(arg, sizeof(*vol_args));
    2456         [ #  # ]:          0 :         if (IS_ERR(vol_args)) {
    2457                 :            :                 ret = PTR_ERR(vol_args);
    2458                 :          0 :                 goto out;
    2459                 :            :         }
    2460                 :            : 
    2461                 :          0 :         vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
    2462                 :            : 
    2463         [ #  # ]:          0 :         if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
    2464                 :            :                         1)) {
    2465                 :            :                 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
    2466                 :            :                 goto out;
    2467                 :            :         }
    2468                 :            : 
    2469                 :          0 :         mutex_lock(&root->fs_info->volume_mutex);
    2470                 :          0 :         ret = btrfs_rm_device(root, vol_args->name);
    2471                 :          0 :         mutex_unlock(&root->fs_info->volume_mutex);
    2472                 :          0 :         atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
    2473                 :            : 
    2474                 :            : out:
    2475                 :          0 :         kfree(vol_args);
    2476                 :          0 :         mnt_drop_write_file(file);
    2477                 :          0 :         return ret;
    2478                 :            : }
    2479                 :            : 
    2480                 :          0 : static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
    2481                 :            : {
    2482                 :            :         struct btrfs_ioctl_fs_info_args *fi_args;
    2483                 :            :         struct btrfs_device *device;
    2484                 :            :         struct btrfs_device *next;
    2485                 :          0 :         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
    2486                 :            :         int ret = 0;
    2487                 :            : 
    2488         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2489                 :            :                 return -EPERM;
    2490                 :            : 
    2491                 :            :         fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
    2492         [ #  # ]:          0 :         if (!fi_args)
    2493                 :            :                 return -ENOMEM;
    2494                 :            : 
    2495                 :          0 :         mutex_lock(&fs_devices->device_list_mutex);
    2496                 :          0 :         fi_args->num_devices = fs_devices->num_devices;
    2497                 :          0 :         memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
    2498                 :            : 
    2499         [ #  # ]:          0 :         list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
    2500         [ #  # ]:          0 :                 if (device->devid > fi_args->max_id)
    2501                 :          0 :                         fi_args->max_id = device->devid;
    2502                 :            :         }
    2503                 :          0 :         mutex_unlock(&fs_devices->device_list_mutex);
    2504                 :            : 
    2505         [ #  # ]:          0 :         if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
    2506                 :            :                 ret = -EFAULT;
    2507                 :            : 
    2508                 :          0 :         kfree(fi_args);
    2509                 :            :         return ret;
    2510                 :            : }
    2511                 :            : 
    2512                 :          0 : static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
    2513                 :            : {
    2514                 :            :         struct btrfs_ioctl_dev_info_args *di_args;
    2515                 :            :         struct btrfs_device *dev;
    2516                 :          0 :         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
    2517                 :            :         int ret = 0;
    2518                 :            :         char *s_uuid = NULL;
    2519                 :            : 
    2520         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2521                 :            :                 return -EPERM;
    2522                 :            : 
    2523                 :          0 :         di_args = memdup_user(arg, sizeof(*di_args));
    2524         [ #  # ]:          0 :         if (IS_ERR(di_args))
    2525                 :            :                 return PTR_ERR(di_args);
    2526                 :            : 
    2527         [ #  # ]:          0 :         if (!btrfs_is_empty_uuid(di_args->uuid))
    2528                 :            :                 s_uuid = di_args->uuid;
    2529                 :            : 
    2530                 :          0 :         mutex_lock(&fs_devices->device_list_mutex);
    2531                 :          0 :         dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
    2532                 :            : 
    2533         [ #  # ]:          0 :         if (!dev) {
    2534                 :            :                 ret = -ENODEV;
    2535                 :            :                 goto out;
    2536                 :            :         }
    2537                 :            : 
    2538                 :          0 :         di_args->devid = dev->devid;
    2539                 :          0 :         di_args->bytes_used = dev->bytes_used;
    2540                 :          0 :         di_args->total_bytes = dev->total_bytes;
    2541                 :          0 :         memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
    2542         [ #  # ]:          0 :         if (dev->name) {
    2543                 :            :                 struct rcu_string *name;
    2544                 :            : 
    2545                 :            :                 rcu_read_lock();
    2546                 :          0 :                 name = rcu_dereference(dev->name);
    2547                 :          0 :                 strncpy(di_args->path, name->str, sizeof(di_args->path));
    2548                 :            :                 rcu_read_unlock();
    2549                 :          0 :                 di_args->path[sizeof(di_args->path) - 1] = 0;
    2550                 :            :         } else {
    2551                 :          0 :                 di_args->path[0] = '\0';
    2552                 :            :         }
    2553                 :            : 
    2554                 :            : out:
    2555                 :          0 :         mutex_unlock(&fs_devices->device_list_mutex);
    2556 [ #  # ][ #  # ]:          0 :         if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
    2557                 :            :                 ret = -EFAULT;
    2558                 :            : 
    2559                 :          0 :         kfree(di_args);
    2560                 :            :         return ret;
    2561                 :            : }
    2562                 :            : 
    2563                 :          0 : static struct page *extent_same_get_page(struct inode *inode, u64 off)
    2564                 :            : {
    2565                 :            :         struct page *page;
    2566                 :            :         pgoff_t index;
    2567                 :          0 :         struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
    2568                 :            : 
    2569                 :          0 :         index = off >> PAGE_CACHE_SHIFT;
    2570                 :            : 
    2571                 :          0 :         page = grab_cache_page(inode->i_mapping, index);
    2572         [ #  # ]:          0 :         if (!page)
    2573                 :            :                 return NULL;
    2574                 :            : 
    2575         [ #  # ]:          0 :         if (!PageUptodate(page)) {
    2576         [ #  # ]:          0 :                 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
    2577                 :            :                                                  0))
    2578                 :            :                         return NULL;
    2579                 :            :                 lock_page(page);
    2580         [ #  # ]:          0 :                 if (!PageUptodate(page)) {
    2581                 :          0 :                         unlock_page(page);
    2582                 :          0 :                         page_cache_release(page);
    2583                 :          0 :                         return NULL;
    2584                 :            :                 }
    2585                 :            :         }
    2586                 :          0 :         unlock_page(page);
    2587                 :            : 
    2588                 :          0 :         return page;
    2589                 :            : }
    2590                 :            : 
    2591                 :            : static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
    2592                 :            : {
    2593                 :            :         /* do any pending delalloc/csum calc on src, one way or
    2594                 :            :            another, and lock file content */
    2595                 :            :         while (1) {
    2596                 :            :                 struct btrfs_ordered_extent *ordered;
    2597                 :          0 :                 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
    2598                 :          0 :                 ordered = btrfs_lookup_first_ordered_extent(inode,
    2599                 :            :                                                             off + len - 1);
    2600   [ #  #  #  #  :          0 :                 if (!ordered &&
          #  #  #  #  #  
                #  #  # ]
    2601                 :          0 :                     !test_range_bit(&BTRFS_I(inode)->io_tree, off,
    2602                 :            :                                     off + len - 1, EXTENT_DELALLOC, 0, NULL))
    2603                 :            :                         break;
    2604                 :          0 :                 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
    2605   [ #  #  #  #  :          0 :                 if (ordered)
                   #  # ]
    2606                 :          0 :                         btrfs_put_ordered_extent(ordered);
    2607                 :          0 :                 btrfs_wait_ordered_range(inode, off, len);
    2608                 :            :         }
    2609                 :            : }
    2610                 :            : 
    2611                 :          0 : static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
    2612                 :            :                                 struct inode *inode2, u64 loff2, u64 len)
    2613                 :            : {
    2614                 :          0 :         unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
    2615                 :          0 :         unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
    2616                 :            : 
    2617                 :          0 :         mutex_unlock(&inode1->i_mutex);
    2618                 :          0 :         mutex_unlock(&inode2->i_mutex);
    2619                 :          0 : }
    2620                 :            : 
    2621                 :          0 : static void btrfs_double_lock(struct inode *inode1, u64 loff1,
    2622                 :            :                               struct inode *inode2, u64 loff2, u64 len)
    2623                 :            : {
    2624         [ #  # ]:          0 :         if (inode1 < inode2) {
    2625                 :            :                 swap(inode1, inode2);
    2626                 :            :                 swap(loff1, loff2);
    2627                 :            :         }
    2628                 :            : 
    2629                 :          0 :         mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
    2630                 :            :         lock_extent_range(inode1, loff1, len);
    2631         [ #  # ]:          0 :         if (inode1 != inode2) {
    2632                 :          0 :                 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
    2633                 :            :                 lock_extent_range(inode2, loff2, len);
    2634                 :            :         }
    2635                 :          0 : }
    2636                 :            : 
    2637                 :          0 : static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
    2638                 :            :                           u64 dst_loff, u64 len)
    2639                 :            : {
    2640                 :            :         int ret = 0;
    2641                 :            :         struct page *src_page, *dst_page;
    2642                 :            :         unsigned int cmp_len = PAGE_CACHE_SIZE;
    2643                 :            :         void *addr, *dst_addr;
    2644                 :            : 
    2645         [ #  # ]:          0 :         while (len) {
    2646         [ #  # ]:          0 :                 if (len < PAGE_CACHE_SIZE)
    2647                 :          0 :                         cmp_len = len;
    2648                 :            : 
    2649                 :          0 :                 src_page = extent_same_get_page(src, loff);
    2650         [ #  # ]:          0 :                 if (!src_page)
    2651                 :            :                         return -EINVAL;
    2652                 :          0 :                 dst_page = extent_same_get_page(dst, dst_loff);
    2653         [ #  # ]:          0 :                 if (!dst_page) {
    2654                 :          0 :                         page_cache_release(src_page);
    2655                 :          0 :                         return -EINVAL;
    2656                 :            :                 }
    2657                 :          0 :                 addr = kmap_atomic(src_page);
    2658                 :          0 :                 dst_addr = kmap_atomic(dst_page);
    2659                 :            : 
    2660                 :          0 :                 flush_dcache_page(src_page);
    2661                 :          0 :                 flush_dcache_page(dst_page);
    2662                 :            : 
    2663         [ #  # ]:          0 :                 if (memcmp(addr, dst_addr, cmp_len))
    2664                 :            :                         ret = BTRFS_SAME_DATA_DIFFERS;
    2665                 :            : 
    2666                 :          0 :                 kunmap_atomic(addr);
    2667                 :          0 :                 kunmap_atomic(dst_addr);
    2668                 :          0 :                 page_cache_release(src_page);
    2669                 :          0 :                 page_cache_release(dst_page);
    2670                 :            : 
    2671         [ #  # ]:          0 :                 if (ret)
    2672                 :            :                         break;
    2673                 :            : 
    2674                 :          0 :                 loff += cmp_len;
    2675                 :          0 :                 dst_loff += cmp_len;
    2676                 :          0 :                 len -= cmp_len;
    2677                 :            :         }
    2678                 :            : 
    2679                 :          0 :         return ret;
    2680                 :            : }
    2681                 :            : 
    2682                 :            : static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
    2683                 :            : {
    2684                 :          0 :         u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
    2685                 :            : 
    2686 [ #  # ][ #  # ]:          0 :         if (off + len > inode->i_size || off + len < off)
         [ #  # ][ #  # ]
    2687                 :            :                 return -EINVAL;
    2688                 :            :         /* Check that we are block aligned - btrfs_clone() requires this */
    2689 [ #  # ][ #  # ]:          0 :         if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
         [ #  # ][ #  # ]
    2690                 :            :                 return -EINVAL;
    2691                 :            : 
    2692                 :            :         return 0;
    2693                 :            : }
    2694                 :            : 
    2695                 :          0 : static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
    2696                 :            :                              struct inode *dst, u64 dst_loff)
    2697                 :            : {
    2698                 :            :         int ret;
    2699                 :            : 
    2700                 :            :         /*
    2701                 :            :          * btrfs_clone() can't handle extents in the same file
    2702                 :            :          * yet. Once that works, we can drop this check and replace it
    2703                 :            :          * with a check for the same inode, but overlapping extents.
    2704                 :            :          */
    2705         [ #  # ]:          0 :         if (src == dst)
    2706                 :            :                 return -EINVAL;
    2707                 :            : 
    2708                 :          0 :         btrfs_double_lock(src, loff, dst, dst_loff, len);
    2709                 :            : 
    2710                 :            :         ret = extent_same_check_offsets(src, loff, len);
    2711         [ #  # ]:          0 :         if (ret)
    2712                 :            :                 goto out_unlock;
    2713                 :            : 
    2714                 :            :         ret = extent_same_check_offsets(dst, dst_loff, len);
    2715         [ #  # ]:          0 :         if (ret)
    2716                 :            :                 goto out_unlock;
    2717                 :            : 
    2718                 :            :         /* don't make the dst file partly checksummed */
    2719         [ #  # ]:          0 :         if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
    2720                 :          0 :             (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
    2721                 :            :                 ret = -EINVAL;
    2722                 :            :                 goto out_unlock;
    2723                 :            :         }
    2724                 :            : 
    2725                 :          0 :         ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
    2726         [ #  # ]:          0 :         if (ret == 0)
    2727                 :          0 :                 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
    2728                 :            : 
    2729                 :            : out_unlock:
    2730                 :          0 :         btrfs_double_unlock(src, loff, dst, dst_loff, len);
    2731                 :            : 
    2732                 :          0 :         return ret;
    2733                 :            : }
    2734                 :            : 
    2735                 :            : #define BTRFS_MAX_DEDUPE_LEN    (16 * 1024 * 1024)
    2736                 :            : 
    2737                 :          0 : static long btrfs_ioctl_file_extent_same(struct file *file,
    2738                 :            :                         struct btrfs_ioctl_same_args __user *argp)
    2739                 :            : {
    2740                 :            :         struct btrfs_ioctl_same_args *same;
    2741                 :            :         struct btrfs_ioctl_same_extent_info *info;
    2742                 :            :         struct inode *src = file_inode(file);
    2743                 :            :         u64 off;
    2744                 :            :         u64 len;
    2745                 :            :         int i;
    2746                 :            :         int ret;
    2747                 :            :         unsigned long size;
    2748                 :          0 :         u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
    2749                 :          0 :         bool is_admin = capable(CAP_SYS_ADMIN);
    2750                 :            :         u16 count;
    2751                 :            : 
    2752         [ #  # ]:          0 :         if (!(file->f_mode & FMODE_READ))
    2753                 :            :                 return -EINVAL;
    2754                 :            : 
    2755                 :          0 :         ret = mnt_want_write_file(file);
    2756         [ #  # ]:          0 :         if (ret)
    2757                 :            :                 return ret;
    2758                 :            : 
    2759         [ #  # ]:          0 :         if (get_user(count, &argp->dest_count)) {
    2760                 :            :                 ret = -EFAULT;
    2761                 :            :                 goto out;
    2762                 :            :         }
    2763                 :            : 
    2764                 :          0 :         size = offsetof(struct btrfs_ioctl_same_args __user, info[count]);
    2765                 :            : 
    2766                 :          0 :         same = memdup_user(argp, size);
    2767                 :            : 
    2768         [ #  # ]:          0 :         if (IS_ERR(same)) {
    2769                 :            :                 ret = PTR_ERR(same);
    2770                 :          0 :                 goto out;
    2771                 :            :         }
    2772                 :            : 
    2773                 :          0 :         off = same->logical_offset;
    2774                 :          0 :         len = same->length;
    2775                 :            : 
    2776                 :            :         /*
    2777                 :            :          * Limit the total length we will dedupe for each operation.
    2778                 :            :          * This is intended to bound the total time spent in this
    2779                 :            :          * ioctl to something sane.
    2780                 :            :          */
    2781         [ #  # ]:          0 :         if (len > BTRFS_MAX_DEDUPE_LEN)
    2782                 :            :                 len = BTRFS_MAX_DEDUPE_LEN;
    2783                 :            : 
    2784 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
         [ #  # ][ #  # ]
    2785                 :            :                 /*
    2786                 :            :                  * Btrfs does not support blocksize < page_size. As a
    2787                 :            :                  * result, btrfs_cmp_data() won't correctly handle
    2788                 :            :                  * this situation without an update.
    2789                 :            :                  */
    2790                 :            :                 ret = -EINVAL;
    2791                 :            :                 goto out;
    2792                 :            :         }
    2793                 :            : 
    2794                 :            :         ret = -EISDIR;
    2795         [ #  # ]:          0 :         if (S_ISDIR(src->i_mode))
    2796                 :            :                 goto out;
    2797                 :            : 
    2798                 :            :         ret = -EACCES;
    2799         [ #  # ]:          0 :         if (!S_ISREG(src->i_mode))
    2800                 :            :                 goto out;
    2801                 :            : 
    2802                 :            :         /* pre-format output fields to sane values */
    2803         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
    2804                 :          0 :                 same->info[i].bytes_deduped = 0ULL;
    2805                 :          0 :                 same->info[i].status = 0;
    2806                 :            :         }
    2807                 :            : 
    2808         [ #  # ]:          0 :         for (i = 0, info = same->info; i < count; i++, info++) {
    2809                 :            :                 struct inode *dst;
    2810                 :          0 :                 struct fd dst_file = fdget(info->fd);
    2811         [ #  # ]:          0 :                 if (!dst_file.file) {
    2812                 :          0 :                         info->status = -EBADF;
    2813                 :          0 :                         continue;
    2814                 :            :                 }
    2815                 :            :                 dst = file_inode(dst_file.file);
    2816                 :            : 
    2817 [ #  # ][ #  # ]:          0 :                 if (!(is_admin || (dst_file.file->f_mode & FMODE_WRITE))) {
    2818                 :          0 :                         info->status = -EINVAL;
    2819         [ #  # ]:          0 :                 } else if (file->f_path.mnt != dst_file.file->f_path.mnt) {
    2820                 :          0 :                         info->status = -EXDEV;
    2821         [ #  # ]:          0 :                 } else if (S_ISDIR(dst->i_mode)) {
    2822                 :          0 :                         info->status = -EISDIR;
    2823         [ #  # ]:          0 :                 } else if (!S_ISREG(dst->i_mode)) {
    2824                 :          0 :                         info->status = -EACCES;
    2825                 :            :                 } else {
    2826                 :          0 :                         info->status = btrfs_extent_same(src, off, len, dst,
    2827                 :            :                                                         info->logical_offset);
    2828         [ #  # ]:          0 :                         if (info->status == 0)
    2829                 :          0 :                                 info->bytes_deduped += len;
    2830                 :            :                 }
    2831                 :            :                 fdput(dst_file);
    2832                 :            :         }
    2833                 :            : 
    2834                 :          0 :         ret = copy_to_user(argp, same, size);
    2835         [ #  # ]:          0 :         if (ret)
    2836                 :            :                 ret = -EFAULT;
    2837                 :            : 
    2838                 :            : out:
    2839                 :          0 :         mnt_drop_write_file(file);
    2840                 :          0 :         return ret;
    2841                 :            : }
    2842                 :            : 
    2843                 :            : /**
    2844                 :            :  * btrfs_clone() - clone a range from inode file to another
    2845                 :            :  *
    2846                 :            :  * @src: Inode to clone from
    2847                 :            :  * @inode: Inode to clone to
    2848                 :            :  * @off: Offset within source to start clone from
    2849                 :            :  * @olen: Original length, passed by user, of range to clone
    2850                 :            :  * @olen_aligned: Block-aligned value of olen, extent_same uses
    2851                 :            :  *               identical values here
    2852                 :            :  * @destoff: Offset within @inode to start clone
    2853                 :            :  */
    2854                 :          0 : static int btrfs_clone(struct inode *src, struct inode *inode,
    2855                 :            :                        u64 off, u64 olen, u64 olen_aligned, u64 destoff)
    2856                 :            : {
    2857                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    2858                 :            :         struct btrfs_path *path = NULL;
    2859                 :            :         struct extent_buffer *leaf;
    2860                 :            :         struct btrfs_trans_handle *trans;
    2861                 :            :         char *buf = NULL;
    2862                 :            :         struct btrfs_key key;
    2863                 :            :         u32 nritems;
    2864                 :            :         int slot;
    2865                 :            :         int ret;
    2866                 :            :         u64 len = olen_aligned;
    2867                 :            : 
    2868                 :            :         ret = -ENOMEM;
    2869                 :          0 :         buf = vmalloc(btrfs_level_size(root, 0));
    2870         [ #  # ]:          0 :         if (!buf)
    2871                 :            :                 return ret;
    2872                 :            : 
    2873                 :          0 :         path = btrfs_alloc_path();
    2874         [ #  # ]:          0 :         if (!path) {
    2875                 :          0 :                 vfree(buf);
    2876                 :          0 :                 return ret;
    2877                 :            :         }
    2878                 :            : 
    2879                 :          0 :         path->reada = 2;
    2880                 :            :         /* clone data */
    2881                 :          0 :         key.objectid = btrfs_ino(src);
    2882                 :          0 :         key.type = BTRFS_EXTENT_DATA_KEY;
    2883                 :          0 :         key.offset = 0;
    2884                 :            : 
    2885                 :            :         while (1) {
    2886                 :            :                 /*
    2887                 :            :                  * note the key will change type as we walk through the
    2888                 :            :                  * tree.
    2889                 :            :                  */
    2890                 :          0 :                 path->leave_spinning = 1;
    2891                 :          0 :                 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
    2892                 :            :                                 0, 0);
    2893         [ #  # ]:          0 :                 if (ret < 0)
    2894                 :            :                         goto out;
    2895                 :            : 
    2896                 :          0 :                 nritems = btrfs_header_nritems(path->nodes[0]);
    2897                 :            : process_slot:
    2898         [ #  # ]:          0 :                 if (path->slots[0] >= nritems) {
    2899                 :          0 :                         ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
    2900         [ #  # ]:          0 :                         if (ret < 0)
    2901                 :            :                                 goto out;
    2902         [ #  # ]:          0 :                         if (ret > 0)
    2903                 :            :                                 break;
    2904                 :          0 :                         nritems = btrfs_header_nritems(path->nodes[0]);
    2905                 :            :                 }
    2906                 :          0 :                 leaf = path->nodes[0];
    2907                 :          0 :                 slot = path->slots[0];
    2908                 :            : 
    2909                 :            :                 btrfs_item_key_to_cpu(leaf, &key, slot);
    2910 [ #  # ][ #  # ]:          0 :                 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
    2911                 :            :                     key.objectid != btrfs_ino(src))
    2912                 :            :                         break;
    2913                 :            : 
    2914         [ #  # ]:          0 :                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
    2915                 :            :                         struct btrfs_file_extent_item *extent;
    2916                 :            :                         int type;
    2917                 :            :                         u32 size;
    2918                 :            :                         struct btrfs_key new_key;
    2919                 :            :                         u64 disko = 0, diskl = 0;
    2920                 :            :                         u64 datao = 0, datal = 0;
    2921                 :            :                         u8 comp;
    2922                 :            :                         u64 endoff;
    2923                 :            : 
    2924                 :          0 :                         extent = btrfs_item_ptr(leaf, slot,
    2925                 :            :                                                 struct btrfs_file_extent_item);
    2926                 :            :                         comp = btrfs_file_extent_compression(leaf, extent);
    2927                 :          0 :                         type = btrfs_file_extent_type(leaf, extent);
    2928         [ #  # ]:          0 :                         if (type == BTRFS_FILE_EXTENT_REG ||
    2929                 :            :                             type == BTRFS_FILE_EXTENT_PREALLOC) {
    2930                 :            :                                 disko = btrfs_file_extent_disk_bytenr(leaf,
    2931                 :            :                                                                       extent);
    2932                 :            :                                 diskl = btrfs_file_extent_disk_num_bytes(leaf,
    2933                 :            :                                                                  extent);
    2934                 :            :                                 datao = btrfs_file_extent_offset(leaf, extent);
    2935                 :            :                                 datal = btrfs_file_extent_num_bytes(leaf,
    2936                 :            :                                                                     extent);
    2937         [ #  # ]:          0 :                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
    2938                 :            :                                 /* take upper bound, may be compressed */
    2939                 :            :                                 datal = btrfs_file_extent_ram_bytes(leaf,
    2940                 :            :                                                                     extent);
    2941                 :            :                         }
    2942                 :            : 
    2943 [ #  # ][ #  # ]:          0 :                         if (key.offset + datal <= off ||
    2944                 :          0 :                             key.offset >= off + len - 1) {
    2945                 :          0 :                                 path->slots[0]++;
    2946                 :          0 :                                 goto process_slot;
    2947                 :            :                         }
    2948                 :            : 
    2949                 :            :                         size = btrfs_item_size_nr(leaf, slot);
    2950                 :          0 :                         read_extent_buffer(leaf, buf,
    2951                 :            :                                            btrfs_item_ptr_offset(leaf, slot),
    2952                 :            :                                            size);
    2953                 :            : 
    2954                 :          0 :                         btrfs_release_path(path);
    2955                 :          0 :                         path->leave_spinning = 0;
    2956                 :            : 
    2957                 :          0 :                         memcpy(&new_key, &key, sizeof(new_key));
    2958                 :          0 :                         new_key.objectid = btrfs_ino(inode);
    2959         [ #  # ]:          0 :                         if (off <= key.offset)
    2960                 :          0 :                                 new_key.offset = key.offset + destoff - off;
    2961                 :            :                         else
    2962                 :          0 :                                 new_key.offset = destoff;
    2963                 :            : 
    2964                 :            :                         /*
    2965                 :            :                          * 1 - adjusting old extent (we may have to split it)
    2966                 :            :                          * 1 - add new extent
    2967                 :            :                          * 1 - inode update
    2968                 :            :                          */
    2969                 :          0 :                         trans = btrfs_start_transaction(root, 3);
    2970         [ #  # ]:          0 :                         if (IS_ERR(trans)) {
    2971                 :            :                                 ret = PTR_ERR(trans);
    2972                 :          0 :                                 goto out;
    2973                 :            :                         }
    2974                 :            : 
    2975         [ #  # ]:          0 :                         if (type == BTRFS_FILE_EXTENT_REG ||
    2976                 :            :                             type == BTRFS_FILE_EXTENT_PREALLOC) {
    2977                 :            :                                 /*
    2978                 :            :                                  *    a  | --- range to clone ---|  b
    2979                 :            :                                  * | ------------- extent ------------- |
    2980                 :            :                                  */
    2981                 :            : 
    2982                 :            :                                 /* substract range b */
    2983         [ #  # ]:          0 :                                 if (key.offset + datal > off + len)
    2984                 :          0 :                                         datal = off + len - key.offset;
    2985                 :            : 
    2986                 :            :                                 /* substract range a */
    2987         [ #  # ]:          0 :                                 if (off > key.offset) {
    2988                 :          0 :                                         datao += off - key.offset;
    2989                 :          0 :                                         datal -= off - key.offset;
    2990                 :            :                                 }
    2991                 :            : 
    2992                 :          0 :                                 ret = btrfs_drop_extents(trans, root, inode,
    2993                 :            :                                                          new_key.offset,
    2994                 :          0 :                                                          new_key.offset + datal,
    2995                 :            :                                                          1);
    2996         [ #  # ]:          0 :                                 if (ret) {
    2997                 :          0 :                                         btrfs_abort_transaction(trans, root,
    2998                 :            :                                                                 ret);
    2999                 :          0 :                                         btrfs_end_transaction(trans, root);
    3000                 :          0 :                                         goto out;
    3001                 :            :                                 }
    3002                 :            : 
    3003                 :            :                                 ret = btrfs_insert_empty_item(trans, root, path,
    3004                 :            :                                                               &new_key, size);
    3005         [ #  # ]:          0 :                                 if (ret) {
    3006                 :          0 :                                         btrfs_abort_transaction(trans, root,
    3007                 :            :                                                                 ret);
    3008                 :          0 :                                         btrfs_end_transaction(trans, root);
    3009                 :          0 :                                         goto out;
    3010                 :            :                                 }
    3011                 :            : 
    3012                 :          0 :                                 leaf = path->nodes[0];
    3013                 :          0 :                                 slot = path->slots[0];
    3014                 :          0 :                                 write_extent_buffer(leaf, buf,
    3015                 :            :                                             btrfs_item_ptr_offset(leaf, slot),
    3016                 :            :                                             size);
    3017                 :            : 
    3018                 :          0 :                                 extent = btrfs_item_ptr(leaf, slot,
    3019                 :            :                                                 struct btrfs_file_extent_item);
    3020                 :            : 
    3021                 :            :                                 /* disko == 0 means it's a hole */
    3022         [ #  # ]:          0 :                                 if (!disko)
    3023                 :            :                                         datao = 0;
    3024                 :            : 
    3025                 :            :                                 btrfs_set_file_extent_offset(leaf, extent,
    3026                 :            :                                                              datao);
    3027                 :            :                                 btrfs_set_file_extent_num_bytes(leaf, extent,
    3028                 :            :                                                                 datal);
    3029         [ #  # ]:          0 :                                 if (disko) {
    3030                 :          0 :                                         inode_add_bytes(inode, datal);
    3031                 :          0 :                                         ret = btrfs_inc_extent_ref(trans, root,
    3032                 :            :                                                         disko, diskl, 0,
    3033                 :            :                                                         root->root_key.objectid,
    3034                 :            :                                                         btrfs_ino(inode),
    3035                 :          0 :                                                         new_key.offset - datao,
    3036                 :            :                                                         0);
    3037         [ #  # ]:          0 :                                         if (ret) {
    3038                 :          0 :                                                 btrfs_abort_transaction(trans,
    3039                 :            :                                                                         root,
    3040                 :            :                                                                         ret);
    3041                 :          0 :                                                 btrfs_end_transaction(trans,
    3042                 :            :                                                                       root);
    3043                 :          0 :                                                 goto out;
    3044                 :            : 
    3045                 :            :                                         }
    3046                 :            :                                 }
    3047         [ #  # ]:          0 :                         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
    3048                 :            :                                 u64 skip = 0;
    3049                 :            :                                 u64 trim = 0;
    3050         [ #  # ]:          0 :                                 if (off > key.offset) {
    3051                 :          0 :                                         skip = off - key.offset;
    3052                 :          0 :                                         new_key.offset += skip;
    3053                 :            :                                 }
    3054                 :            : 
    3055         [ #  # ]:          0 :                                 if (key.offset + datal > off + len)
    3056                 :          0 :                                         trim = key.offset + datal - (off + len);
    3057                 :            : 
    3058 [ #  # ][ #  # ]:          0 :                                 if (comp && (skip || trim)) {
    3059                 :            :                                         ret = -EINVAL;
    3060                 :          0 :                                         btrfs_end_transaction(trans, root);
    3061                 :          0 :                                         goto out;
    3062                 :            :                                 }
    3063                 :          0 :                                 size -= skip + trim;
    3064                 :          0 :                                 datal -= skip + trim;
    3065                 :            : 
    3066                 :          0 :                                 ret = btrfs_drop_extents(trans, root, inode,
    3067                 :            :                                                          new_key.offset,
    3068                 :          0 :                                                          new_key.offset + datal,
    3069                 :            :                                                          1);
    3070         [ #  # ]:          0 :                                 if (ret) {
    3071                 :          0 :                                         btrfs_abort_transaction(trans, root,
    3072                 :            :                                                                 ret);
    3073                 :          0 :                                         btrfs_end_transaction(trans, root);
    3074                 :          0 :                                         goto out;
    3075                 :            :                                 }
    3076                 :            : 
    3077                 :            :                                 ret = btrfs_insert_empty_item(trans, root, path,
    3078                 :            :                                                               &new_key, size);
    3079         [ #  # ]:          0 :                                 if (ret) {
    3080                 :          0 :                                         btrfs_abort_transaction(trans, root,
    3081                 :            :                                                                 ret);
    3082                 :          0 :                                         btrfs_end_transaction(trans, root);
    3083                 :          0 :                                         goto out;
    3084                 :            :                                 }
    3085                 :            : 
    3086         [ #  # ]:          0 :                                 if (skip) {
    3087                 :            :                                         u32 start =
    3088                 :            :                                           btrfs_file_extent_calc_inline_size(0);
    3089                 :          0 :                                         memmove(buf+start, buf+start+skip,
    3090                 :            :                                                 datal);
    3091                 :            :                                 }
    3092                 :            : 
    3093                 :          0 :                                 leaf = path->nodes[0];
    3094                 :          0 :                                 slot = path->slots[0];
    3095                 :          0 :                                 write_extent_buffer(leaf, buf,
    3096                 :            :                                             btrfs_item_ptr_offset(leaf, slot),
    3097                 :            :                                             size);
    3098                 :          0 :                                 inode_add_bytes(inode, datal);
    3099                 :            :                         }
    3100                 :            : 
    3101                 :          0 :                         btrfs_mark_buffer_dirty(leaf);
    3102                 :          0 :                         btrfs_release_path(path);
    3103                 :            : 
    3104                 :            :                         inode_inc_iversion(inode);
    3105                 :          0 :                         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
    3106                 :            : 
    3107                 :            :                         /*
    3108                 :            :                          * we round up to the block size at eof when
    3109                 :            :                          * determining which extents to clone above,
    3110                 :            :                          * but shouldn't round up the file size
    3111                 :            :                          */
    3112                 :          0 :                         endoff = new_key.offset + datal;
    3113         [ #  # ]:          0 :                         if (endoff > destoff+olen)
    3114                 :            :                                 endoff = destoff+olen;
    3115         [ #  # ]:          0 :                         if (endoff > inode->i_size)
    3116                 :            :                                 btrfs_i_size_write(inode, endoff);
    3117                 :            : 
    3118                 :          0 :                         ret = btrfs_update_inode(trans, root, inode);
    3119         [ #  # ]:          0 :                         if (ret) {
    3120                 :          0 :                                 btrfs_abort_transaction(trans, root, ret);
    3121                 :          0 :                                 btrfs_end_transaction(trans, root);
    3122                 :          0 :                                 goto out;
    3123                 :            :                         }
    3124                 :          0 :                         ret = btrfs_end_transaction(trans, root);
    3125                 :            :                 }
    3126                 :          0 :                 btrfs_release_path(path);
    3127                 :          0 :                 key.offset++;
    3128                 :          0 :         }
    3129                 :            :         ret = 0;
    3130                 :            : 
    3131                 :            : out:
    3132                 :          0 :         btrfs_release_path(path);
    3133                 :          0 :         btrfs_free_path(path);
    3134                 :          0 :         vfree(buf);
    3135                 :          0 :         return ret;
    3136                 :            : }
    3137                 :            : 
    3138                 :          0 : static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
    3139                 :            :                                        u64 off, u64 olen, u64 destoff)
    3140                 :            : {
    3141                 :            :         struct inode *inode = file_inode(file);
    3142                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    3143                 :            :         struct fd src_file;
    3144                 :            :         struct inode *src;
    3145                 :            :         int ret;
    3146                 :            :         u64 len = olen;
    3147                 :          0 :         u64 bs = root->fs_info->sb->s_blocksize;
    3148                 :            :         int same_inode = 0;
    3149                 :            : 
    3150                 :            :         /*
    3151                 :            :          * TODO:
    3152                 :            :          * - split compressed inline extents.  annoying: we need to
    3153                 :            :          *   decompress into destination's address_space (the file offset
    3154                 :            :          *   may change, so source mapping won't do), then recompress (or
    3155                 :            :          *   otherwise reinsert) a subrange.
    3156                 :            :          * - allow ranges within the same file to be cloned (provided
    3157                 :            :          *   they don't overlap)?
    3158                 :            :          */
    3159                 :            : 
    3160                 :            :         /* the destination must be opened for writing */
    3161 [ #  # ][ #  # ]:          0 :         if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
    3162                 :            :                 return -EINVAL;
    3163                 :            : 
    3164         [ #  # ]:          0 :         if (btrfs_root_readonly(root))
    3165                 :            :                 return -EROFS;
    3166                 :            : 
    3167                 :          0 :         ret = mnt_want_write_file(file);
    3168         [ #  # ]:          0 :         if (ret)
    3169                 :            :                 return ret;
    3170                 :            : 
    3171                 :            :         src_file = fdget(srcfd);
    3172         [ #  # ]:          0 :         if (!src_file.file) {
    3173                 :            :                 ret = -EBADF;
    3174                 :            :                 goto out_drop_write;
    3175                 :            :         }
    3176                 :            : 
    3177                 :            :         ret = -EXDEV;
    3178         [ #  # ]:          0 :         if (src_file.file->f_path.mnt != file->f_path.mnt)
    3179                 :            :                 goto out_fput;
    3180                 :            : 
    3181                 :            :         src = file_inode(src_file.file);
    3182                 :            : 
    3183                 :            :         ret = -EINVAL;
    3184         [ #  # ]:          0 :         if (src == inode)
    3185                 :            :                 same_inode = 1;
    3186                 :            : 
    3187                 :            :         /* the src must be open for reading */
    3188         [ #  # ]:          0 :         if (!(src_file.file->f_mode & FMODE_READ))
    3189                 :            :                 goto out_fput;
    3190                 :            : 
    3191                 :            :         /* don't make the dst file partly checksummed */
    3192         [ #  # ]:          0 :         if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
    3193                 :          0 :             (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
    3194                 :            :                 goto out_fput;
    3195                 :            : 
    3196                 :            :         ret = -EISDIR;
    3197 [ #  # ][ #  # ]:          0 :         if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
    3198                 :            :                 goto out_fput;
    3199                 :            : 
    3200                 :            :         ret = -EXDEV;
    3201         [ #  # ]:          0 :         if (src->i_sb != inode->i_sb)
    3202                 :            :                 goto out_fput;
    3203                 :            : 
    3204         [ #  # ]:          0 :         if (!same_inode) {
    3205         [ #  # ]:          0 :                 if (inode < src) {
    3206                 :          0 :                         mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
    3207                 :          0 :                         mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
    3208                 :            :                 } else {
    3209                 :          0 :                         mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
    3210                 :          0 :                         mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
    3211                 :            :                 }
    3212                 :            :         } else {
    3213                 :          0 :                 mutex_lock(&src->i_mutex);
    3214                 :            :         }
    3215                 :            : 
    3216                 :            :         /* determine range to clone */
    3217                 :            :         ret = -EINVAL;
    3218 [ #  # ][ #  # ]:          0 :         if (off + len > src->i_size || off + len < off)
    3219                 :            :                 goto out_unlock;
    3220         [ #  # ]:          0 :         if (len == 0)
    3221                 :          0 :                 olen = len = src->i_size - off;
    3222                 :            :         /* if we extend to eof, continue to block boundary */
    3223         [ #  # ]:          0 :         if (off + len == src->i_size)
    3224                 :          0 :                 len = ALIGN(src->i_size, bs) - off;
    3225                 :            : 
    3226                 :            :         /* verify the end result is block aligned */
    3227 [ #  # ][ #  # ]:          0 :         if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
                 [ #  # ]
    3228                 :          0 :             !IS_ALIGNED(destoff, bs))
    3229                 :            :                 goto out_unlock;
    3230                 :            : 
    3231                 :            :         /* verify if ranges are overlapped within the same file */
    3232         [ #  # ]:          0 :         if (same_inode) {
    3233 [ #  # ][ #  # ]:          0 :                 if (destoff + len > off && destoff < off + len)
    3234                 :            :                         goto out_unlock;
    3235                 :            :         }
    3236                 :            : 
    3237         [ #  # ]:          0 :         if (destoff > inode->i_size) {
    3238                 :          0 :                 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
    3239         [ #  # ]:          0 :                 if (ret)
    3240                 :            :                         goto out_unlock;
    3241                 :            :         }
    3242                 :            : 
    3243                 :            :         /* truncate page cache pages from target inode range */
    3244                 :          0 :         truncate_inode_pages_range(&inode->i_data, destoff,
    3245                 :          0 :                                    PAGE_CACHE_ALIGN(destoff + len) - 1);
    3246                 :            : 
    3247                 :            :         lock_extent_range(src, off, len);
    3248                 :            : 
    3249                 :          0 :         ret = btrfs_clone(src, inode, off, olen, len, destoff);
    3250                 :            : 
    3251                 :          0 :         unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
    3252                 :            : out_unlock:
    3253         [ #  # ]:          0 :         if (!same_inode) {
    3254         [ #  # ]:          0 :                 if (inode < src) {
    3255                 :          0 :                         mutex_unlock(&src->i_mutex);
    3256                 :          0 :                         mutex_unlock(&inode->i_mutex);
    3257                 :            :                 } else {
    3258                 :          0 :                         mutex_unlock(&inode->i_mutex);
    3259                 :          0 :                         mutex_unlock(&src->i_mutex);
    3260                 :            :                 }
    3261                 :            :         } else {
    3262                 :          0 :                 mutex_unlock(&src->i_mutex);
    3263                 :            :         }
    3264                 :            : out_fput:
    3265                 :            :         fdput(src_file);
    3266                 :            : out_drop_write:
    3267                 :          0 :         mnt_drop_write_file(file);
    3268                 :          0 :         return ret;
    3269                 :            : }
    3270                 :            : 
    3271                 :          0 : static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
    3272                 :            : {
    3273                 :            :         struct btrfs_ioctl_clone_range_args args;
    3274                 :            : 
    3275         [ #  # ]:          0 :         if (copy_from_user(&args, argp, sizeof(args)))
    3276                 :            :                 return -EFAULT;
    3277                 :          0 :         return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
    3278                 :            :                                  args.src_length, args.dest_offset);
    3279                 :            : }
    3280                 :            : 
    3281                 :            : /*
    3282                 :            :  * there are many ways the trans_start and trans_end ioctls can lead
    3283                 :            :  * to deadlocks.  They should only be used by applications that
    3284                 :            :  * basically own the machine, and have a very in depth understanding
    3285                 :            :  * of all the possible deadlocks and enospc problems.
    3286                 :            :  */
    3287                 :          0 : static long btrfs_ioctl_trans_start(struct file *file)
    3288                 :            : {
    3289                 :            :         struct inode *inode = file_inode(file);
    3290                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    3291                 :            :         struct btrfs_trans_handle *trans;
    3292                 :            :         int ret;
    3293                 :            : 
    3294                 :            :         ret = -EPERM;
    3295         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3296                 :            :                 goto out;
    3297                 :            : 
    3298                 :            :         ret = -EINPROGRESS;
    3299         [ #  # ]:          0 :         if (file->private_data)
    3300                 :            :                 goto out;
    3301                 :            : 
    3302                 :            :         ret = -EROFS;
    3303         [ #  # ]:          0 :         if (btrfs_root_readonly(root))
    3304                 :            :                 goto out;
    3305                 :            : 
    3306                 :          0 :         ret = mnt_want_write_file(file);
    3307         [ #  # ]:          0 :         if (ret)
    3308                 :            :                 goto out;
    3309                 :            : 
    3310                 :          0 :         atomic_inc(&root->fs_info->open_ioctl_trans);
    3311                 :            : 
    3312                 :            :         ret = -ENOMEM;
    3313                 :          0 :         trans = btrfs_start_ioctl_transaction(root);
    3314         [ #  # ]:          0 :         if (IS_ERR(trans))
    3315                 :            :                 goto out_drop;
    3316                 :            : 
    3317                 :          0 :         file->private_data = trans;
    3318                 :          0 :         return 0;
    3319                 :            : 
    3320                 :            : out_drop:
    3321                 :          0 :         atomic_dec(&root->fs_info->open_ioctl_trans);
    3322                 :          0 :         mnt_drop_write_file(file);
    3323                 :            : out:
    3324                 :          0 :         return ret;
    3325                 :            : }
    3326                 :            : 
    3327                 :          0 : static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
    3328                 :            : {
    3329                 :            :         struct inode *inode = file_inode(file);
    3330                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    3331                 :            :         struct btrfs_root *new_root;
    3332                 :            :         struct btrfs_dir_item *di;
    3333                 :            :         struct btrfs_trans_handle *trans;
    3334                 :            :         struct btrfs_path *path;
    3335                 :            :         struct btrfs_key location;
    3336                 :            :         struct btrfs_disk_key disk_key;
    3337                 :          0 :         u64 objectid = 0;
    3338                 :            :         u64 dir_id;
    3339                 :            :         int ret;
    3340                 :            : 
    3341         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3342                 :            :                 return -EPERM;
    3343                 :            : 
    3344                 :          0 :         ret = mnt_want_write_file(file);
    3345         [ #  # ]:          0 :         if (ret)
    3346                 :            :                 return ret;
    3347                 :            : 
    3348         [ #  # ]:          0 :         if (copy_from_user(&objectid, argp, sizeof(objectid))) {
    3349                 :            :                 ret = -EFAULT;
    3350                 :            :                 goto out;
    3351                 :            :         }
    3352                 :            : 
    3353         [ #  # ]:          0 :         if (!objectid)
    3354                 :          0 :                 objectid = BTRFS_FS_TREE_OBJECTID;
    3355                 :            : 
    3356                 :          0 :         location.objectid = objectid;
    3357                 :          0 :         location.type = BTRFS_ROOT_ITEM_KEY;
    3358                 :          0 :         location.offset = (u64)-1;
    3359                 :            : 
    3360                 :          0 :         new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
    3361         [ #  # ]:          0 :         if (IS_ERR(new_root)) {
    3362                 :            :                 ret = PTR_ERR(new_root);
    3363                 :          0 :                 goto out;
    3364                 :            :         }
    3365                 :            : 
    3366                 :          0 :         path = btrfs_alloc_path();
    3367         [ #  # ]:          0 :         if (!path) {
    3368                 :            :                 ret = -ENOMEM;
    3369                 :            :                 goto out;
    3370                 :            :         }
    3371                 :          0 :         path->leave_spinning = 1;
    3372                 :            : 
    3373                 :          0 :         trans = btrfs_start_transaction(root, 1);
    3374         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    3375                 :          0 :                 btrfs_free_path(path);
    3376                 :            :                 ret = PTR_ERR(trans);
    3377                 :          0 :                 goto out;
    3378                 :            :         }
    3379                 :            : 
    3380                 :          0 :         dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
    3381                 :          0 :         di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
    3382                 :            :                                    dir_id, "default", 7, 1);
    3383         [ #  # ]:          0 :         if (IS_ERR_OR_NULL(di)) {
    3384                 :          0 :                 btrfs_free_path(path);
    3385                 :          0 :                 btrfs_end_transaction(trans, root);
    3386                 :          0 :                 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
    3387                 :            :                            "item, this isn't going to work");
    3388                 :            :                 ret = -ENOENT;
    3389                 :          0 :                 goto out;
    3390                 :            :         }
    3391                 :            : 
    3392                 :            :         btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
    3393                 :          0 :         btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
    3394                 :          0 :         btrfs_mark_buffer_dirty(path->nodes[0]);
    3395                 :          0 :         btrfs_free_path(path);
    3396                 :            : 
    3397                 :          0 :         btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
    3398                 :          0 :         btrfs_end_transaction(trans, root);
    3399                 :            : out:
    3400                 :          0 :         mnt_drop_write_file(file);
    3401                 :          0 :         return ret;
    3402                 :            : }
    3403                 :            : 
    3404                 :          0 : void btrfs_get_block_group_info(struct list_head *groups_list,
    3405                 :            :                                 struct btrfs_ioctl_space_info *space)
    3406                 :            : {
    3407                 :            :         struct btrfs_block_group_cache *block_group;
    3408                 :            : 
    3409                 :          0 :         space->total_bytes = 0;
    3410                 :          0 :         space->used_bytes = 0;
    3411                 :          0 :         space->flags = 0;
    3412         [ #  # ]:          0 :         list_for_each_entry(block_group, groups_list, list) {
    3413                 :          0 :                 space->flags = block_group->flags;
    3414                 :          0 :                 space->total_bytes += block_group->key.offset;
    3415                 :          0 :                 space->used_bytes +=
    3416                 :            :                         btrfs_block_group_used(&block_group->item);
    3417                 :            :         }
    3418                 :          0 : }
    3419                 :            : 
    3420                 :          0 : static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
    3421                 :            : {
    3422                 :            :         struct btrfs_ioctl_space_args space_args;
    3423                 :            :         struct btrfs_ioctl_space_info space;
    3424                 :            :         struct btrfs_ioctl_space_info *dest;
    3425                 :            :         struct btrfs_ioctl_space_info *dest_orig;
    3426                 :            :         struct btrfs_ioctl_space_info __user *user_dest;
    3427                 :            :         struct btrfs_space_info *info;
    3428                 :          0 :         u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
    3429                 :            :                        BTRFS_BLOCK_GROUP_SYSTEM,
    3430                 :            :                        BTRFS_BLOCK_GROUP_METADATA,
    3431                 :            :                        BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
    3432                 :            :         int num_types = 4;
    3433                 :            :         int alloc_size;
    3434                 :            :         int ret = 0;
    3435                 :            :         u64 slot_count = 0;
    3436                 :            :         int i, c;
    3437                 :            : 
    3438         [ #  # ]:          0 :         if (copy_from_user(&space_args,
    3439                 :            :                            (struct btrfs_ioctl_space_args __user *)arg,
    3440                 :            :                            sizeof(space_args)))
    3441                 :            :                 return -EFAULT;
    3442                 :            : 
    3443         [ #  # ]:          0 :         for (i = 0; i < num_types; i++) {
    3444                 :            :                 struct btrfs_space_info *tmp;
    3445                 :            : 
    3446                 :            :                 info = NULL;
    3447                 :            :                 rcu_read_lock();
    3448         [ #  # ]:          0 :                 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
    3449                 :            :                                         list) {
    3450         [ #  # ]:          0 :                         if (tmp->flags == types[i]) {
    3451                 :            :                                 info = tmp;
    3452                 :            :                                 break;
    3453                 :            :                         }
    3454                 :            :                 }
    3455                 :            :                 rcu_read_unlock();
    3456                 :            : 
    3457         [ #  # ]:          0 :                 if (!info)
    3458                 :          0 :                         continue;
    3459                 :            : 
    3460                 :          0 :                 down_read(&info->groups_sem);
    3461         [ #  # ]:          0 :                 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
    3462         [ #  # ]:          0 :                         if (!list_empty(&info->block_groups[c]))
    3463                 :          0 :                                 slot_count++;
    3464                 :            :                 }
    3465                 :          0 :                 up_read(&info->groups_sem);
    3466                 :            :         }
    3467                 :            : 
    3468                 :            :         /* space_slots == 0 means they are asking for a count */
    3469         [ #  # ]:          0 :         if (space_args.space_slots == 0) {
    3470                 :          0 :                 space_args.total_spaces = slot_count;
    3471                 :            :                 goto out;
    3472                 :            :         }
    3473                 :            : 
    3474                 :          0 :         slot_count = min_t(u64, space_args.space_slots, slot_count);
    3475                 :            : 
    3476                 :          0 :         alloc_size = sizeof(*dest) * slot_count;
    3477                 :            : 
    3478                 :            :         /* we generally have at most 6 or so space infos, one for each raid
    3479                 :            :          * level.  So, a whole page should be more than enough for everyone
    3480                 :            :          */
    3481         [ #  # ]:          0 :         if (alloc_size > PAGE_CACHE_SIZE)
    3482                 :            :                 return -ENOMEM;
    3483                 :            : 
    3484                 :          0 :         space_args.total_spaces = 0;
    3485                 :            :         dest = kmalloc(alloc_size, GFP_NOFS);
    3486         [ #  # ]:          0 :         if (!dest)
    3487                 :            :                 return -ENOMEM;
    3488                 :            :         dest_orig = dest;
    3489                 :            : 
    3490                 :            :         /* now we have a buffer to copy into */
    3491         [ #  # ]:          0 :         for (i = 0; i < num_types; i++) {
    3492                 :            :                 struct btrfs_space_info *tmp;
    3493                 :            : 
    3494         [ #  # ]:          0 :                 if (!slot_count)
    3495                 :            :                         break;
    3496                 :            : 
    3497                 :            :                 info = NULL;
    3498                 :            :                 rcu_read_lock();
    3499         [ #  # ]:          0 :                 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
    3500                 :            :                                         list) {
    3501         [ #  # ]:          0 :                         if (tmp->flags == types[i]) {
    3502                 :            :                                 info = tmp;
    3503                 :            :                                 break;
    3504                 :            :                         }
    3505                 :            :                 }
    3506                 :            :                 rcu_read_unlock();
    3507                 :            : 
    3508         [ #  # ]:          0 :                 if (!info)
    3509                 :          0 :                         continue;
    3510                 :          0 :                 down_read(&info->groups_sem);
    3511         [ #  # ]:          0 :                 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
    3512         [ #  # ]:          0 :                         if (!list_empty(&info->block_groups[c])) {
    3513                 :          0 :                                 btrfs_get_block_group_info(
    3514                 :            :                                         &info->block_groups[c], &space);
    3515                 :          0 :                                 memcpy(dest, &space, sizeof(space));
    3516                 :          0 :                                 dest++;
    3517                 :          0 :                                 space_args.total_spaces++;
    3518                 :          0 :                                 slot_count--;
    3519                 :            :                         }
    3520         [ #  # ]:          0 :                         if (!slot_count)
    3521                 :            :                                 break;
    3522                 :            :                 }
    3523                 :          0 :                 up_read(&info->groups_sem);
    3524                 :            :         }
    3525                 :            : 
    3526                 :          0 :         user_dest = (struct btrfs_ioctl_space_info __user *)
    3527                 :            :                 (arg + sizeof(struct btrfs_ioctl_space_args));
    3528                 :            : 
    3529         [ #  # ]:          0 :         if (copy_to_user(user_dest, dest_orig, alloc_size))
    3530                 :            :                 ret = -EFAULT;
    3531                 :            : 
    3532                 :          0 :         kfree(dest_orig);
    3533                 :            : out:
    3534 [ #  # ][ #  # ]:          0 :         if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
    3535                 :            :                 ret = -EFAULT;
    3536                 :            : 
    3537                 :            :         return ret;
    3538                 :            : }
    3539                 :            : 
    3540                 :            : /*
    3541                 :            :  * there are many ways the trans_start and trans_end ioctls can lead
    3542                 :            :  * to deadlocks.  They should only be used by applications that
    3543                 :            :  * basically own the machine, and have a very in depth understanding
    3544                 :            :  * of all the possible deadlocks and enospc problems.
    3545                 :            :  */
    3546                 :          0 : long btrfs_ioctl_trans_end(struct file *file)
    3547                 :            : {
    3548                 :            :         struct inode *inode = file_inode(file);
    3549                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    3550                 :            :         struct btrfs_trans_handle *trans;
    3551                 :            : 
    3552                 :          0 :         trans = file->private_data;
    3553         [ #  # ]:          0 :         if (!trans)
    3554                 :            :                 return -EINVAL;
    3555                 :          0 :         file->private_data = NULL;
    3556                 :            : 
    3557                 :          0 :         btrfs_end_transaction(trans, root);
    3558                 :            : 
    3559                 :          0 :         atomic_dec(&root->fs_info->open_ioctl_trans);
    3560                 :            : 
    3561                 :          0 :         mnt_drop_write_file(file);
    3562                 :          0 :         return 0;
    3563                 :            : }
    3564                 :            : 
    3565                 :          0 : static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
    3566                 :            :                                             void __user *argp)
    3567                 :            : {
    3568                 :            :         struct btrfs_trans_handle *trans;
    3569                 :            :         u64 transid;
    3570                 :            :         int ret;
    3571                 :            : 
    3572                 :          0 :         trans = btrfs_attach_transaction_barrier(root);
    3573         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    3574         [ #  # ]:          0 :                 if (PTR_ERR(trans) != -ENOENT)
    3575                 :            :                         return PTR_ERR(trans);
    3576                 :            : 
    3577                 :            :                 /* No running transaction, don't bother */
    3578                 :          0 :                 transid = root->fs_info->last_trans_committed;
    3579                 :          0 :                 goto out;
    3580                 :            :         }
    3581                 :          0 :         transid = trans->transid;
    3582                 :          0 :         ret = btrfs_commit_transaction_async(trans, root, 0);
    3583         [ #  # ]:          0 :         if (ret) {
    3584                 :          0 :                 btrfs_end_transaction(trans, root);
    3585                 :          0 :                 return ret;
    3586                 :            :         }
    3587                 :            : out:
    3588         [ #  # ]:          0 :         if (argp)
    3589         [ #  # ]:          0 :                 if (copy_to_user(argp, &transid, sizeof(transid)))
    3590                 :            :                         return -EFAULT;
    3591                 :            :         return 0;
    3592                 :            : }
    3593                 :            : 
    3594                 :          0 : static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
    3595                 :            :                                            void __user *argp)
    3596                 :            : {
    3597                 :            :         u64 transid;
    3598                 :            : 
    3599         [ #  # ]:          0 :         if (argp) {
    3600         [ #  # ]:          0 :                 if (copy_from_user(&transid, argp, sizeof(transid)))
    3601                 :            :                         return -EFAULT;
    3602                 :            :         } else {
    3603                 :          0 :                 transid = 0;  /* current trans */
    3604                 :            :         }
    3605                 :          0 :         return btrfs_wait_for_commit(root, transid);
    3606                 :            : }
    3607                 :            : 
    3608                 :          0 : static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
    3609                 :            : {
    3610                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    3611                 :            :         struct btrfs_ioctl_scrub_args *sa;
    3612                 :            :         int ret;
    3613                 :            : 
    3614         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3615                 :            :                 return -EPERM;
    3616                 :            : 
    3617                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    3618         [ #  # ]:          0 :         if (IS_ERR(sa))
    3619                 :          0 :                 return PTR_ERR(sa);
    3620                 :            : 
    3621         [ #  # ]:          0 :         if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
    3622                 :          0 :                 ret = mnt_want_write_file(file);
    3623         [ #  # ]:          0 :                 if (ret)
    3624                 :            :                         goto out;
    3625                 :            :         }
    3626                 :            : 
    3627                 :          0 :         ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
    3628                 :          0 :                               &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
    3629                 :            :                               0);
    3630                 :            : 
    3631         [ #  # ]:          0 :         if (copy_to_user(arg, sa, sizeof(*sa)))
    3632                 :            :                 ret = -EFAULT;
    3633                 :            : 
    3634         [ #  # ]:          0 :         if (!(sa->flags & BTRFS_SCRUB_READONLY))
    3635                 :          0 :                 mnt_drop_write_file(file);
    3636                 :            : out:
    3637                 :          0 :         kfree(sa);
    3638                 :          0 :         return ret;
    3639                 :            : }
    3640                 :            : 
    3641                 :          0 : static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
    3642                 :            : {
    3643         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3644                 :            :                 return -EPERM;
    3645                 :            : 
    3646                 :          0 :         return btrfs_scrub_cancel(root->fs_info);
    3647                 :            : }
    3648                 :            : 
    3649                 :          0 : static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
    3650                 :            :                                        void __user *arg)
    3651                 :            : {
    3652                 :            :         struct btrfs_ioctl_scrub_args *sa;
    3653                 :            :         int ret;
    3654                 :            : 
    3655         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3656                 :            :                 return -EPERM;
    3657                 :            : 
    3658                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    3659         [ #  # ]:          0 :         if (IS_ERR(sa))
    3660                 :          0 :                 return PTR_ERR(sa);
    3661                 :            : 
    3662                 :          0 :         ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
    3663                 :            : 
    3664         [ #  # ]:          0 :         if (copy_to_user(arg, sa, sizeof(*sa)))
    3665                 :            :                 ret = -EFAULT;
    3666                 :            : 
    3667                 :          0 :         kfree(sa);
    3668                 :          0 :         return ret;
    3669                 :            : }
    3670                 :            : 
    3671                 :          0 : static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
    3672                 :            :                                       void __user *arg)
    3673                 :            : {
    3674                 :            :         struct btrfs_ioctl_get_dev_stats *sa;
    3675                 :            :         int ret;
    3676                 :            : 
    3677                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    3678         [ #  # ]:          0 :         if (IS_ERR(sa))
    3679                 :          0 :                 return PTR_ERR(sa);
    3680                 :            : 
    3681 [ #  # ][ #  # ]:          0 :         if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
    3682                 :          0 :                 kfree(sa);
    3683                 :          0 :                 return -EPERM;
    3684                 :            :         }
    3685                 :            : 
    3686                 :          0 :         ret = btrfs_get_dev_stats(root, sa);
    3687                 :            : 
    3688         [ #  # ]:          0 :         if (copy_to_user(arg, sa, sizeof(*sa)))
    3689                 :            :                 ret = -EFAULT;
    3690                 :            : 
    3691                 :          0 :         kfree(sa);
    3692                 :          0 :         return ret;
    3693                 :            : }
    3694                 :            : 
    3695                 :          0 : static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
    3696                 :            : {
    3697                 :            :         struct btrfs_ioctl_dev_replace_args *p;
    3698                 :            :         int ret;
    3699                 :            : 
    3700         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3701                 :            :                 return -EPERM;
    3702                 :            : 
    3703                 :          0 :         p = memdup_user(arg, sizeof(*p));
    3704         [ #  # ]:          0 :         if (IS_ERR(p))
    3705                 :          0 :                 return PTR_ERR(p);
    3706                 :            : 
    3707   [ #  #  #  # ]:          0 :         switch (p->cmd) {
    3708                 :            :         case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
    3709         [ #  # ]:          0 :                 if (root->fs_info->sb->s_flags & MS_RDONLY) {
    3710                 :            :                         ret = -EROFS;
    3711                 :            :                         goto out;
    3712                 :            :                 }
    3713         [ #  # ]:          0 :                 if (atomic_xchg(
    3714                 :            :                         &root->fs_info->mutually_exclusive_operation_running,
    3715                 :            :                         1)) {
    3716                 :            :                         ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
    3717                 :            :                 } else {
    3718                 :          0 :                         ret = btrfs_dev_replace_start(root, p);
    3719                 :          0 :                         atomic_set(
    3720                 :            :                          &root->fs_info->mutually_exclusive_operation_running,
    3721                 :            :                          0);
    3722                 :            :                 }
    3723                 :            :                 break;
    3724                 :            :         case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
    3725                 :          0 :                 btrfs_dev_replace_status(root->fs_info, p);
    3726                 :            :                 ret = 0;
    3727                 :          0 :                 break;
    3728                 :            :         case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
    3729                 :          0 :                 ret = btrfs_dev_replace_cancel(root->fs_info, p);
    3730                 :          0 :                 break;
    3731                 :            :         default:
    3732                 :            :                 ret = -EINVAL;
    3733                 :            :                 break;
    3734                 :            :         }
    3735                 :            : 
    3736         [ #  # ]:          0 :         if (copy_to_user(arg, p, sizeof(*p)))
    3737                 :            :                 ret = -EFAULT;
    3738                 :            : out:
    3739                 :          0 :         kfree(p);
    3740                 :          0 :         return ret;
    3741                 :            : }
    3742                 :            : 
    3743                 :          0 : static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
    3744                 :            : {
    3745                 :            :         int ret = 0;
    3746                 :            :         int i;
    3747                 :            :         u64 rel_ptr;
    3748                 :            :         int size;
    3749                 :            :         struct btrfs_ioctl_ino_path_args *ipa = NULL;
    3750                 :            :         struct inode_fs_paths *ipath = NULL;
    3751                 :            :         struct btrfs_path *path;
    3752                 :            : 
    3753         [ #  # ]:          0 :         if (!capable(CAP_DAC_READ_SEARCH))
    3754                 :            :                 return -EPERM;
    3755                 :            : 
    3756                 :          0 :         path = btrfs_alloc_path();
    3757         [ #  # ]:          0 :         if (!path) {
    3758                 :            :                 ret = -ENOMEM;
    3759                 :            :                 goto out;
    3760                 :            :         }
    3761                 :            : 
    3762                 :          0 :         ipa = memdup_user(arg, sizeof(*ipa));
    3763         [ #  # ]:          0 :         if (IS_ERR(ipa)) {
    3764                 :            :                 ret = PTR_ERR(ipa);
    3765                 :            :                 ipa = NULL;
    3766                 :          0 :                 goto out;
    3767                 :            :         }
    3768                 :            : 
    3769                 :          0 :         size = min_t(u32, ipa->size, 4096);
    3770                 :          0 :         ipath = init_ipath(size, root, path);
    3771         [ #  # ]:          0 :         if (IS_ERR(ipath)) {
    3772                 :            :                 ret = PTR_ERR(ipath);
    3773                 :            :                 ipath = NULL;
    3774                 :          0 :                 goto out;
    3775                 :            :         }
    3776                 :            : 
    3777                 :          0 :         ret = paths_from_inode(ipa->inum, ipath);
    3778         [ #  # ]:          0 :         if (ret < 0)
    3779                 :            :                 goto out;
    3780                 :            : 
    3781         [ #  # ]:          0 :         for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
    3782                 :          0 :                 rel_ptr = ipath->fspath->val[i] -
    3783                 :          0 :                           (u64)(unsigned long)ipath->fspath->val;
    3784                 :          0 :                 ipath->fspath->val[i] = rel_ptr;
    3785                 :            :         }
    3786                 :            : 
    3787                 :          0 :         ret = copy_to_user((void *)(unsigned long)ipa->fspath,
    3788                 :            :                            (void *)(unsigned long)ipath->fspath, size);
    3789         [ #  # ]:          0 :         if (ret) {
    3790                 :            :                 ret = -EFAULT;
    3791                 :          0 :                 goto out;
    3792                 :            :         }
    3793                 :            : 
    3794                 :            : out:
    3795                 :          0 :         btrfs_free_path(path);
    3796                 :          0 :         free_ipath(ipath);
    3797                 :          0 :         kfree(ipa);
    3798                 :            : 
    3799                 :          0 :         return ret;
    3800                 :            : }
    3801                 :            : 
    3802                 :          0 : static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
    3803                 :            : {
    3804                 :            :         struct btrfs_data_container *inodes = ctx;
    3805                 :            :         const size_t c = 3 * sizeof(u64);
    3806                 :            : 
    3807         [ #  # ]:          0 :         if (inodes->bytes_left >= c) {
    3808                 :          0 :                 inodes->bytes_left -= c;
    3809                 :          0 :                 inodes->val[inodes->elem_cnt] = inum;
    3810                 :          0 :                 inodes->val[inodes->elem_cnt + 1] = offset;
    3811                 :          0 :                 inodes->val[inodes->elem_cnt + 2] = root;
    3812                 :          0 :                 inodes->elem_cnt += 3;
    3813                 :            :         } else {
    3814                 :          0 :                 inodes->bytes_missing += c - inodes->bytes_left;
    3815                 :          0 :                 inodes->bytes_left = 0;
    3816                 :          0 :                 inodes->elem_missed += 3;
    3817                 :            :         }
    3818                 :            : 
    3819                 :          0 :         return 0;
    3820                 :            : }
    3821                 :            : 
    3822                 :          0 : static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
    3823                 :            :                                         void __user *arg)
    3824                 :            : {
    3825                 :            :         int ret = 0;
    3826                 :            :         int size;
    3827                 :            :         struct btrfs_ioctl_logical_ino_args *loi;
    3828                 :            :         struct btrfs_data_container *inodes = NULL;
    3829                 :            :         struct btrfs_path *path = NULL;
    3830                 :            : 
    3831         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3832                 :            :                 return -EPERM;
    3833                 :            : 
    3834                 :          0 :         loi = memdup_user(arg, sizeof(*loi));
    3835         [ #  # ]:          0 :         if (IS_ERR(loi)) {
    3836                 :            :                 ret = PTR_ERR(loi);
    3837                 :            :                 loi = NULL;
    3838                 :            :                 goto out;
    3839                 :            :         }
    3840                 :            : 
    3841                 :          0 :         path = btrfs_alloc_path();
    3842         [ #  # ]:          0 :         if (!path) {
    3843                 :            :                 ret = -ENOMEM;
    3844                 :            :                 goto out;
    3845                 :            :         }
    3846                 :            : 
    3847                 :          0 :         size = min_t(u32, loi->size, 64 * 1024);
    3848                 :          0 :         inodes = init_data_container(size);
    3849         [ #  # ]:          0 :         if (IS_ERR(inodes)) {
    3850                 :            :                 ret = PTR_ERR(inodes);
    3851                 :            :                 inodes = NULL;
    3852                 :            :                 goto out;
    3853                 :            :         }
    3854                 :            : 
    3855                 :          0 :         ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
    3856                 :            :                                           build_ino_list, inodes);
    3857         [ #  # ]:          0 :         if (ret == -EINVAL)
    3858                 :            :                 ret = -ENOENT;
    3859         [ #  # ]:          0 :         if (ret < 0)
    3860                 :            :                 goto out;
    3861                 :            : 
    3862                 :          0 :         ret = copy_to_user((void *)(unsigned long)loi->inodes,
    3863                 :            :                            (void *)(unsigned long)inodes, size);
    3864         [ #  # ]:          0 :         if (ret)
    3865                 :            :                 ret = -EFAULT;
    3866                 :            : 
    3867                 :            : out:
    3868                 :          0 :         btrfs_free_path(path);
    3869                 :          0 :         vfree(inodes);
    3870                 :          0 :         kfree(loi);
    3871                 :            : 
    3872                 :            :         return ret;
    3873                 :            : }
    3874                 :            : 
    3875                 :          0 : void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
    3876                 :            :                                struct btrfs_ioctl_balance_args *bargs)
    3877                 :            : {
    3878                 :          0 :         struct btrfs_balance_control *bctl = fs_info->balance_ctl;
    3879                 :            : 
    3880                 :          0 :         bargs->flags = bctl->flags;
    3881                 :            : 
    3882         [ #  # ]:          0 :         if (atomic_read(&fs_info->balance_running))
    3883                 :          0 :                 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
    3884         [ #  # ]:          0 :         if (atomic_read(&fs_info->balance_pause_req))
    3885                 :          0 :                 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
    3886         [ #  # ]:          0 :         if (atomic_read(&fs_info->balance_cancel_req))
    3887                 :          0 :                 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
    3888                 :            : 
    3889                 :          0 :         memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
    3890                 :          0 :         memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
    3891                 :          0 :         memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
    3892                 :            : 
    3893         [ #  # ]:          0 :         if (lock) {
    3894                 :            :                 spin_lock(&fs_info->balance_lock);
    3895                 :          0 :                 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
    3896                 :            :                 spin_unlock(&fs_info->balance_lock);
    3897                 :            :         } else {
    3898                 :          0 :                 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
    3899                 :            :         }
    3900                 :          0 : }
    3901                 :            : 
    3902                 :          0 : static long btrfs_ioctl_balance(struct file *file, void __user *arg)
    3903                 :            : {
    3904                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    3905                 :          0 :         struct btrfs_fs_info *fs_info = root->fs_info;
    3906                 :            :         struct btrfs_ioctl_balance_args *bargs;
    3907                 :            :         struct btrfs_balance_control *bctl;
    3908                 :            :         bool need_unlock; /* for mut. excl. ops lock */
    3909                 :            :         int ret;
    3910                 :            : 
    3911         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    3912                 :            :                 return -EPERM;
    3913                 :            : 
    3914                 :          0 :         ret = mnt_want_write_file(file);
    3915         [ #  # ]:          0 :         if (ret)
    3916                 :            :                 return ret;
    3917                 :            : 
    3918                 :            : again:
    3919         [ #  # ]:          0 :         if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
    3920                 :          0 :                 mutex_lock(&fs_info->volume_mutex);
    3921                 :          0 :                 mutex_lock(&fs_info->balance_mutex);
    3922                 :            :                 need_unlock = true;
    3923                 :          0 :                 goto locked;
    3924                 :            :         }
    3925                 :            : 
    3926                 :            :         /*
    3927                 :            :          * mut. excl. ops lock is locked.  Three possibilites:
    3928                 :            :          *   (1) some other op is running
    3929                 :            :          *   (2) balance is running
    3930                 :            :          *   (3) balance is paused -- special case (think resume)
    3931                 :            :          */
    3932                 :          0 :         mutex_lock(&fs_info->balance_mutex);
    3933         [ #  # ]:          0 :         if (fs_info->balance_ctl) {
    3934                 :            :                 /* this is either (2) or (3) */
    3935         [ #  # ]:          0 :                 if (!atomic_read(&fs_info->balance_running)) {
    3936                 :          0 :                         mutex_unlock(&fs_info->balance_mutex);
    3937         [ #  # ]:          0 :                         if (!mutex_trylock(&fs_info->volume_mutex))
    3938                 :            :                                 goto again;
    3939                 :          0 :                         mutex_lock(&fs_info->balance_mutex);
    3940                 :            : 
    3941 [ #  # ][ #  # ]:          0 :                         if (fs_info->balance_ctl &&
    3942                 :          0 :                             !atomic_read(&fs_info->balance_running)) {
    3943                 :            :                                 /* this is (3) */
    3944                 :            :                                 need_unlock = false;
    3945                 :            :                                 goto locked;
    3946                 :            :                         }
    3947                 :            : 
    3948                 :          0 :                         mutex_unlock(&fs_info->balance_mutex);
    3949                 :          0 :                         mutex_unlock(&fs_info->volume_mutex);
    3950                 :          0 :                         goto again;
    3951                 :            :                 } else {
    3952                 :            :                         /* this is (2) */
    3953                 :          0 :                         mutex_unlock(&fs_info->balance_mutex);
    3954                 :            :                         ret = -EINPROGRESS;
    3955                 :          0 :                         goto out;
    3956                 :            :                 }
    3957                 :            :         } else {
    3958                 :            :                 /* this is (1) */
    3959                 :          0 :                 mutex_unlock(&fs_info->balance_mutex);
    3960                 :            :                 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
    3961                 :          0 :                 goto out;
    3962                 :            :         }
    3963                 :            : 
    3964                 :            : locked:
    3965         [ #  # ]:          0 :         BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
    3966                 :            : 
    3967         [ #  # ]:          0 :         if (arg) {
    3968                 :          0 :                 bargs = memdup_user(arg, sizeof(*bargs));
    3969         [ #  # ]:          0 :                 if (IS_ERR(bargs)) {
    3970                 :            :                         ret = PTR_ERR(bargs);
    3971                 :          0 :                         goto out_unlock;
    3972                 :            :                 }
    3973                 :            : 
    3974         [ #  # ]:          0 :                 if (bargs->flags & BTRFS_BALANCE_RESUME) {
    3975         [ #  # ]:          0 :                         if (!fs_info->balance_ctl) {
    3976                 :            :                                 ret = -ENOTCONN;
    3977                 :            :                                 goto out_bargs;
    3978                 :            :                         }
    3979                 :            : 
    3980                 :            :                         bctl = fs_info->balance_ctl;
    3981                 :            :                         spin_lock(&fs_info->balance_lock);
    3982                 :          0 :                         bctl->flags |= BTRFS_BALANCE_RESUME;
    3983                 :            :                         spin_unlock(&fs_info->balance_lock);
    3984                 :            : 
    3985                 :            :                         goto do_balance;
    3986                 :            :                 }
    3987                 :            :         } else {
    3988                 :            :                 bargs = NULL;
    3989                 :            :         }
    3990                 :            : 
    3991         [ #  # ]:          0 :         if (fs_info->balance_ctl) {
    3992                 :            :                 ret = -EINPROGRESS;
    3993                 :            :                 goto out_bargs;
    3994                 :            :         }
    3995                 :            : 
    3996                 :            :         bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
    3997         [ #  # ]:          0 :         if (!bctl) {
    3998                 :            :                 ret = -ENOMEM;
    3999                 :            :                 goto out_bargs;
    4000                 :            :         }
    4001                 :            : 
    4002                 :          0 :         bctl->fs_info = fs_info;
    4003         [ #  # ]:          0 :         if (arg) {
    4004                 :          0 :                 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
    4005                 :          0 :                 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
    4006                 :          0 :                 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
    4007                 :            : 
    4008                 :          0 :                 bctl->flags = bargs->flags;
    4009                 :            :         } else {
    4010                 :            :                 /* balance everything - no filters */
    4011                 :          0 :                 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
    4012                 :            :         }
    4013                 :            : 
    4014                 :            : do_balance:
    4015                 :            :         /*
    4016                 :            :          * Ownership of bctl and mutually_exclusive_operation_running
    4017                 :            :          * goes to to btrfs_balance.  bctl is freed in __cancel_balance,
    4018                 :            :          * or, if restriper was paused all the way until unmount, in
    4019                 :            :          * free_fs_info.  mutually_exclusive_operation_running is
    4020                 :            :          * cleared in __cancel_balance.
    4021                 :            :          */
    4022                 :            :         need_unlock = false;
    4023                 :            : 
    4024                 :          0 :         ret = btrfs_balance(bctl, bargs);
    4025                 :            : 
    4026         [ #  # ]:          0 :         if (arg) {
    4027         [ #  # ]:          0 :                 if (copy_to_user(arg, bargs, sizeof(*bargs)))
    4028                 :            :                         ret = -EFAULT;
    4029                 :            :         }
    4030                 :            : 
    4031                 :            : out_bargs:
    4032                 :          0 :         kfree(bargs);
    4033                 :            : out_unlock:
    4034                 :          0 :         mutex_unlock(&fs_info->balance_mutex);
    4035                 :          0 :         mutex_unlock(&fs_info->volume_mutex);
    4036         [ #  # ]:          0 :         if (need_unlock)
    4037                 :          0 :                 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
    4038                 :            : out:
    4039                 :          0 :         mnt_drop_write_file(file);
    4040                 :          0 :         return ret;
    4041                 :            : }
    4042                 :            : 
    4043                 :          0 : static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
    4044                 :            : {
    4045         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4046                 :            :                 return -EPERM;
    4047                 :            : 
    4048      [ #  #  # ]:          0 :         switch (cmd) {
    4049                 :            :         case BTRFS_BALANCE_CTL_PAUSE:
    4050                 :          0 :                 return btrfs_pause_balance(root->fs_info);
    4051                 :            :         case BTRFS_BALANCE_CTL_CANCEL:
    4052                 :          0 :                 return btrfs_cancel_balance(root->fs_info);
    4053                 :            :         }
    4054                 :            : 
    4055                 :            :         return -EINVAL;
    4056                 :            : }
    4057                 :            : 
    4058                 :          0 : static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
    4059                 :            :                                          void __user *arg)
    4060                 :            : {
    4061                 :          0 :         struct btrfs_fs_info *fs_info = root->fs_info;
    4062                 :            :         struct btrfs_ioctl_balance_args *bargs;
    4063                 :            :         int ret = 0;
    4064                 :            : 
    4065         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4066                 :            :                 return -EPERM;
    4067                 :            : 
    4068                 :          0 :         mutex_lock(&fs_info->balance_mutex);
    4069         [ #  # ]:          0 :         if (!fs_info->balance_ctl) {
    4070                 :            :                 ret = -ENOTCONN;
    4071                 :            :                 goto out;
    4072                 :            :         }
    4073                 :            : 
    4074                 :            :         bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
    4075         [ #  # ]:          0 :         if (!bargs) {
    4076                 :            :                 ret = -ENOMEM;
    4077                 :            :                 goto out;
    4078                 :            :         }
    4079                 :            : 
    4080                 :          0 :         update_ioctl_balance_args(fs_info, 1, bargs);
    4081                 :            : 
    4082         [ #  # ]:          0 :         if (copy_to_user(arg, bargs, sizeof(*bargs)))
    4083                 :            :                 ret = -EFAULT;
    4084                 :            : 
    4085                 :          0 :         kfree(bargs);
    4086                 :            : out:
    4087                 :          0 :         mutex_unlock(&fs_info->balance_mutex);
    4088                 :            :         return ret;
    4089                 :            : }
    4090                 :            : 
    4091                 :          0 : static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
    4092                 :            : {
    4093                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4094                 :            :         struct btrfs_ioctl_quota_ctl_args *sa;
    4095                 :            :         struct btrfs_trans_handle *trans = NULL;
    4096                 :            :         int ret;
    4097                 :            :         int err;
    4098                 :            : 
    4099         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4100                 :            :                 return -EPERM;
    4101                 :            : 
    4102                 :          0 :         ret = mnt_want_write_file(file);
    4103         [ #  # ]:          0 :         if (ret)
    4104                 :            :                 return ret;
    4105                 :            : 
    4106                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    4107         [ #  # ]:          0 :         if (IS_ERR(sa)) {
    4108                 :            :                 ret = PTR_ERR(sa);
    4109                 :          0 :                 goto drop_write;
    4110                 :            :         }
    4111                 :            : 
    4112                 :          0 :         down_write(&root->fs_info->subvol_sem);
    4113                 :          0 :         trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
    4114         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4115                 :            :                 ret = PTR_ERR(trans);
    4116                 :          0 :                 goto out;
    4117                 :            :         }
    4118                 :            : 
    4119      [ #  #  # ]:          0 :         switch (sa->cmd) {
    4120                 :            :         case BTRFS_QUOTA_CTL_ENABLE:
    4121                 :          0 :                 ret = btrfs_quota_enable(trans, root->fs_info);
    4122                 :          0 :                 break;
    4123                 :            :         case BTRFS_QUOTA_CTL_DISABLE:
    4124                 :          0 :                 ret = btrfs_quota_disable(trans, root->fs_info);
    4125                 :          0 :                 break;
    4126                 :            :         default:
    4127                 :            :                 ret = -EINVAL;
    4128                 :            :                 break;
    4129                 :            :         }
    4130                 :            : 
    4131                 :          0 :         err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
    4132         [ #  # ]:          0 :         if (err && !ret)
    4133                 :            :                 ret = err;
    4134                 :            : out:
    4135                 :          0 :         kfree(sa);
    4136                 :          0 :         up_write(&root->fs_info->subvol_sem);
    4137                 :            : drop_write:
    4138                 :          0 :         mnt_drop_write_file(file);
    4139                 :          0 :         return ret;
    4140                 :            : }
    4141                 :            : 
    4142                 :          0 : static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
    4143                 :            : {
    4144                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4145                 :            :         struct btrfs_ioctl_qgroup_assign_args *sa;
    4146                 :            :         struct btrfs_trans_handle *trans;
    4147                 :            :         int ret;
    4148                 :            :         int err;
    4149                 :            : 
    4150         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4151                 :            :                 return -EPERM;
    4152                 :            : 
    4153                 :          0 :         ret = mnt_want_write_file(file);
    4154         [ #  # ]:          0 :         if (ret)
    4155                 :            :                 return ret;
    4156                 :            : 
    4157                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    4158         [ #  # ]:          0 :         if (IS_ERR(sa)) {
    4159                 :            :                 ret = PTR_ERR(sa);
    4160                 :          0 :                 goto drop_write;
    4161                 :            :         }
    4162                 :            : 
    4163                 :          0 :         trans = btrfs_join_transaction(root);
    4164         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4165                 :            :                 ret = PTR_ERR(trans);
    4166                 :          0 :                 goto out;
    4167                 :            :         }
    4168                 :            : 
    4169                 :            :         /* FIXME: check if the IDs really exist */
    4170         [ #  # ]:          0 :         if (sa->assign) {
    4171                 :          0 :                 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
    4172                 :            :                                                 sa->src, sa->dst);
    4173                 :            :         } else {
    4174                 :          0 :                 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
    4175                 :            :                                                 sa->src, sa->dst);
    4176                 :            :         }
    4177                 :            : 
    4178                 :          0 :         err = btrfs_end_transaction(trans, root);
    4179         [ #  # ]:          0 :         if (err && !ret)
    4180                 :            :                 ret = err;
    4181                 :            : 
    4182                 :            : out:
    4183                 :          0 :         kfree(sa);
    4184                 :            : drop_write:
    4185                 :          0 :         mnt_drop_write_file(file);
    4186                 :          0 :         return ret;
    4187                 :            : }
    4188                 :            : 
    4189                 :          0 : static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
    4190                 :            : {
    4191                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4192                 :            :         struct btrfs_ioctl_qgroup_create_args *sa;
    4193                 :            :         struct btrfs_trans_handle *trans;
    4194                 :            :         int ret;
    4195                 :            :         int err;
    4196                 :            : 
    4197         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4198                 :            :                 return -EPERM;
    4199                 :            : 
    4200                 :          0 :         ret = mnt_want_write_file(file);
    4201         [ #  # ]:          0 :         if (ret)
    4202                 :            :                 return ret;
    4203                 :            : 
    4204                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    4205         [ #  # ]:          0 :         if (IS_ERR(sa)) {
    4206                 :            :                 ret = PTR_ERR(sa);
    4207                 :          0 :                 goto drop_write;
    4208                 :            :         }
    4209                 :            : 
    4210         [ #  # ]:          0 :         if (!sa->qgroupid) {
    4211                 :            :                 ret = -EINVAL;
    4212                 :            :                 goto out;
    4213                 :            :         }
    4214                 :            : 
    4215                 :          0 :         trans = btrfs_join_transaction(root);
    4216         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4217                 :            :                 ret = PTR_ERR(trans);
    4218                 :          0 :                 goto out;
    4219                 :            :         }
    4220                 :            : 
    4221                 :            :         /* FIXME: check if the IDs really exist */
    4222         [ #  # ]:          0 :         if (sa->create) {
    4223                 :          0 :                 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
    4224                 :            :                                           NULL);
    4225                 :            :         } else {
    4226                 :          0 :                 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
    4227                 :            :         }
    4228                 :            : 
    4229                 :          0 :         err = btrfs_end_transaction(trans, root);
    4230         [ #  # ]:          0 :         if (err && !ret)
    4231                 :            :                 ret = err;
    4232                 :            : 
    4233                 :            : out:
    4234                 :          0 :         kfree(sa);
    4235                 :            : drop_write:
    4236                 :          0 :         mnt_drop_write_file(file);
    4237                 :          0 :         return ret;
    4238                 :            : }
    4239                 :            : 
    4240                 :          0 : static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
    4241                 :            : {
    4242                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4243                 :            :         struct btrfs_ioctl_qgroup_limit_args *sa;
    4244                 :            :         struct btrfs_trans_handle *trans;
    4245                 :            :         int ret;
    4246                 :            :         int err;
    4247                 :            :         u64 qgroupid;
    4248                 :            : 
    4249         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4250                 :            :                 return -EPERM;
    4251                 :            : 
    4252                 :          0 :         ret = mnt_want_write_file(file);
    4253         [ #  # ]:          0 :         if (ret)
    4254                 :            :                 return ret;
    4255                 :            : 
    4256                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    4257         [ #  # ]:          0 :         if (IS_ERR(sa)) {
    4258                 :            :                 ret = PTR_ERR(sa);
    4259                 :          0 :                 goto drop_write;
    4260                 :            :         }
    4261                 :            : 
    4262                 :          0 :         trans = btrfs_join_transaction(root);
    4263         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4264                 :            :                 ret = PTR_ERR(trans);
    4265                 :          0 :                 goto out;
    4266                 :            :         }
    4267                 :            : 
    4268                 :          0 :         qgroupid = sa->qgroupid;
    4269         [ #  # ]:          0 :         if (!qgroupid) {
    4270                 :            :                 /* take the current subvol as qgroup */
    4271                 :          0 :                 qgroupid = root->root_key.objectid;
    4272                 :            :         }
    4273                 :            : 
    4274                 :            :         /* FIXME: check if the IDs really exist */
    4275                 :          0 :         ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
    4276                 :            : 
    4277                 :          0 :         err = btrfs_end_transaction(trans, root);
    4278         [ #  # ]:          0 :         if (err && !ret)
    4279                 :            :                 ret = err;
    4280                 :            : 
    4281                 :            : out:
    4282                 :          0 :         kfree(sa);
    4283                 :            : drop_write:
    4284                 :          0 :         mnt_drop_write_file(file);
    4285                 :          0 :         return ret;
    4286                 :            : }
    4287                 :            : 
    4288                 :          0 : static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
    4289                 :            : {
    4290                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4291                 :            :         struct btrfs_ioctl_quota_rescan_args *qsa;
    4292                 :            :         int ret;
    4293                 :            : 
    4294         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4295                 :            :                 return -EPERM;
    4296                 :            : 
    4297                 :          0 :         ret = mnt_want_write_file(file);
    4298         [ #  # ]:          0 :         if (ret)
    4299                 :            :                 return ret;
    4300                 :            : 
    4301                 :          0 :         qsa = memdup_user(arg, sizeof(*qsa));
    4302         [ #  # ]:          0 :         if (IS_ERR(qsa)) {
    4303                 :            :                 ret = PTR_ERR(qsa);
    4304                 :          0 :                 goto drop_write;
    4305                 :            :         }
    4306                 :            : 
    4307         [ #  # ]:          0 :         if (qsa->flags) {
    4308                 :            :                 ret = -EINVAL;
    4309                 :            :                 goto out;
    4310                 :            :         }
    4311                 :            : 
    4312                 :          0 :         ret = btrfs_qgroup_rescan(root->fs_info);
    4313                 :            : 
    4314                 :            : out:
    4315                 :          0 :         kfree(qsa);
    4316                 :            : drop_write:
    4317                 :          0 :         mnt_drop_write_file(file);
    4318                 :          0 :         return ret;
    4319                 :            : }
    4320                 :            : 
    4321                 :          0 : static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
    4322                 :            : {
    4323                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4324                 :            :         struct btrfs_ioctl_quota_rescan_args *qsa;
    4325                 :            :         int ret = 0;
    4326                 :            : 
    4327         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4328                 :            :                 return -EPERM;
    4329                 :            : 
    4330                 :            :         qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
    4331         [ #  # ]:          0 :         if (!qsa)
    4332                 :            :                 return -ENOMEM;
    4333                 :            : 
    4334         [ #  # ]:          0 :         if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
    4335                 :          0 :                 qsa->flags = 1;
    4336                 :          0 :                 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
    4337                 :            :         }
    4338                 :            : 
    4339         [ #  # ]:          0 :         if (copy_to_user(arg, qsa, sizeof(*qsa)))
    4340                 :            :                 ret = -EFAULT;
    4341                 :            : 
    4342                 :          0 :         kfree(qsa);
    4343                 :          0 :         return ret;
    4344                 :            : }
    4345                 :            : 
    4346                 :          0 : static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
    4347                 :            : {
    4348                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4349                 :            : 
    4350         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4351                 :            :                 return -EPERM;
    4352                 :            : 
    4353                 :          0 :         return btrfs_qgroup_wait_for_completion(root->fs_info);
    4354                 :            : }
    4355                 :            : 
    4356                 :          0 : static long btrfs_ioctl_set_received_subvol(struct file *file,
    4357                 :            :                                             void __user *arg)
    4358                 :            : {
    4359                 :            :         struct btrfs_ioctl_received_subvol_args *sa = NULL;
    4360                 :            :         struct inode *inode = file_inode(file);
    4361                 :          0 :         struct btrfs_root *root = BTRFS_I(inode)->root;
    4362                 :            :         struct btrfs_root_item *root_item = &root->root_item;
    4363                 :            :         struct btrfs_trans_handle *trans;
    4364                 :          0 :         struct timespec ct = CURRENT_TIME;
    4365                 :            :         int ret = 0;
    4366                 :            :         int received_uuid_changed;
    4367                 :            : 
    4368         [ #  # ]:          0 :         if (!inode_owner_or_capable(inode))
    4369                 :            :                 return -EPERM;
    4370                 :            : 
    4371                 :          0 :         ret = mnt_want_write_file(file);
    4372         [ #  # ]:          0 :         if (ret < 0)
    4373                 :            :                 return ret;
    4374                 :            : 
    4375                 :          0 :         down_write(&root->fs_info->subvol_sem);
    4376                 :            : 
    4377         [ #  # ]:          0 :         if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
    4378                 :            :                 ret = -EINVAL;
    4379                 :            :                 goto out;
    4380                 :            :         }
    4381                 :            : 
    4382         [ #  # ]:          0 :         if (btrfs_root_readonly(root)) {
    4383                 :            :                 ret = -EROFS;
    4384                 :            :                 goto out;
    4385                 :            :         }
    4386                 :            : 
    4387                 :          0 :         sa = memdup_user(arg, sizeof(*sa));
    4388         [ #  # ]:          0 :         if (IS_ERR(sa)) {
    4389                 :            :                 ret = PTR_ERR(sa);
    4390                 :            :                 sa = NULL;
    4391                 :          0 :                 goto out;
    4392                 :            :         }
    4393                 :            : 
    4394                 :            :         /*
    4395                 :            :          * 1 - root item
    4396                 :            :          * 2 - uuid items (received uuid + subvol uuid)
    4397                 :            :          */
    4398                 :          0 :         trans = btrfs_start_transaction(root, 3);
    4399         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4400                 :            :                 ret = PTR_ERR(trans);
    4401                 :            :                 trans = NULL;
    4402                 :          0 :                 goto out;
    4403                 :            :         }
    4404                 :            : 
    4405                 :          0 :         sa->rtransid = trans->transid;
    4406                 :          0 :         sa->rtime.sec = ct.tv_sec;
    4407                 :          0 :         sa->rtime.nsec = ct.tv_nsec;
    4408                 :            : 
    4409                 :          0 :         received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
    4410                 :            :                                        BTRFS_UUID_SIZE);
    4411 [ #  # ][ #  # ]:          0 :         if (received_uuid_changed &&
    4412                 :            :             !btrfs_is_empty_uuid(root_item->received_uuid))
    4413                 :          0 :                 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
    4414                 :            :                                     root_item->received_uuid,
    4415                 :            :                                     BTRFS_UUID_KEY_RECEIVED_SUBVOL,
    4416                 :            :                                     root->root_key.objectid);
    4417                 :          0 :         memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
    4418                 :          0 :         btrfs_set_root_stransid(root_item, sa->stransid);
    4419                 :          0 :         btrfs_set_root_rtransid(root_item, sa->rtransid);
    4420                 :          0 :         btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
    4421                 :          0 :         btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
    4422                 :          0 :         btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
    4423                 :          0 :         btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
    4424                 :            : 
    4425                 :          0 :         ret = btrfs_update_root(trans, root->fs_info->tree_root,
    4426                 :            :                                 &root->root_key, &root->root_item);
    4427         [ #  # ]:          0 :         if (ret < 0) {
    4428                 :          0 :                 btrfs_end_transaction(trans, root);
    4429                 :          0 :                 goto out;
    4430                 :            :         }
    4431 [ #  # ][ #  # ]:          0 :         if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
    4432                 :          0 :                 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
    4433                 :            :                                           sa->uuid,
    4434                 :            :                                           BTRFS_UUID_KEY_RECEIVED_SUBVOL,
    4435                 :            :                                           root->root_key.objectid);
    4436         [ #  # ]:          0 :                 if (ret < 0 && ret != -EEXIST) {
    4437                 :          0 :                         btrfs_abort_transaction(trans, root, ret);
    4438                 :          0 :                         goto out;
    4439                 :            :                 }
    4440                 :            :         }
    4441                 :          0 :         ret = btrfs_commit_transaction(trans, root);
    4442         [ #  # ]:          0 :         if (ret < 0) {
    4443                 :          0 :                 btrfs_abort_transaction(trans, root, ret);
    4444                 :          0 :                 goto out;
    4445                 :            :         }
    4446                 :            : 
    4447                 :          0 :         ret = copy_to_user(arg, sa, sizeof(*sa));
    4448         [ #  # ]:          0 :         if (ret)
    4449                 :            :                 ret = -EFAULT;
    4450                 :            : 
    4451                 :            : out:
    4452                 :          0 :         kfree(sa);
    4453                 :          0 :         up_write(&root->fs_info->subvol_sem);
    4454                 :          0 :         mnt_drop_write_file(file);
    4455                 :          0 :         return ret;
    4456                 :            : }
    4457                 :            : 
    4458                 :          0 : static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
    4459                 :            : {
    4460                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4461                 :            :         size_t len;
    4462                 :            :         int ret;
    4463                 :            :         char label[BTRFS_LABEL_SIZE];
    4464                 :            : 
    4465                 :          0 :         spin_lock(&root->fs_info->super_lock);
    4466                 :          0 :         memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
    4467                 :            :         spin_unlock(&root->fs_info->super_lock);
    4468                 :            : 
    4469                 :          0 :         len = strnlen(label, BTRFS_LABEL_SIZE);
    4470                 :            : 
    4471         [ #  # ]:          0 :         if (len == BTRFS_LABEL_SIZE) {
    4472                 :          0 :                 btrfs_warn(root->fs_info,
    4473                 :            :                         "label is too long, return the first %zu bytes", --len);
    4474                 :            :         }
    4475                 :            : 
    4476                 :            :         ret = copy_to_user(arg, label, len);
    4477                 :            : 
    4478         [ #  # ]:          0 :         return ret ? -EFAULT : 0;
    4479                 :            : }
    4480                 :            : 
    4481                 :          0 : static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
    4482                 :            : {
    4483                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4484                 :          0 :         struct btrfs_super_block *super_block = root->fs_info->super_copy;
    4485                 :            :         struct btrfs_trans_handle *trans;
    4486                 :            :         char label[BTRFS_LABEL_SIZE];
    4487                 :            :         int ret;
    4488                 :            : 
    4489         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4490                 :            :                 return -EPERM;
    4491                 :            : 
    4492         [ #  # ]:          0 :         if (copy_from_user(label, arg, sizeof(label)))
    4493                 :            :                 return -EFAULT;
    4494                 :            : 
    4495         [ #  # ]:          0 :         if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
    4496                 :          0 :                 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
    4497                 :            :                        BTRFS_LABEL_SIZE - 1);
    4498                 :          0 :                 return -EINVAL;
    4499                 :            :         }
    4500                 :            : 
    4501                 :          0 :         ret = mnt_want_write_file(file);
    4502         [ #  # ]:          0 :         if (ret)
    4503                 :            :                 return ret;
    4504                 :            : 
    4505                 :          0 :         trans = btrfs_start_transaction(root, 0);
    4506         [ #  # ]:          0 :         if (IS_ERR(trans)) {
    4507                 :            :                 ret = PTR_ERR(trans);
    4508                 :          0 :                 goto out_unlock;
    4509                 :            :         }
    4510                 :            : 
    4511                 :          0 :         spin_lock(&root->fs_info->super_lock);
    4512                 :          0 :         strcpy(super_block->label, label);
    4513                 :          0 :         spin_unlock(&root->fs_info->super_lock);
    4514                 :          0 :         ret = btrfs_commit_transaction(trans, root);
    4515                 :            : 
    4516                 :            : out_unlock:
    4517                 :          0 :         mnt_drop_write_file(file);
    4518                 :          0 :         return ret;
    4519                 :            : }
    4520                 :            : 
    4521                 :            : #define INIT_FEATURE_FLAGS(suffix) \
    4522                 :            :         { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
    4523                 :            :           .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
    4524                 :            :           .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
    4525                 :            : 
    4526                 :          0 : static int btrfs_ioctl_get_supported_features(struct file *file,
    4527                 :            :                                               void __user *arg)
    4528                 :            : {
    4529                 :            :         static struct btrfs_ioctl_feature_flags features[3] = {
    4530                 :            :                 INIT_FEATURE_FLAGS(SUPP),
    4531                 :            :                 INIT_FEATURE_FLAGS(SAFE_SET),
    4532                 :            :                 INIT_FEATURE_FLAGS(SAFE_CLEAR)
    4533                 :            :         };
    4534                 :            : 
    4535         [ #  # ]:          0 :         if (copy_to_user(arg, &features, sizeof(features)))
    4536                 :            :                 return -EFAULT;
    4537                 :            : 
    4538                 :            :         return 0;
    4539                 :            : }
    4540                 :            : 
    4541                 :          0 : static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
    4542                 :            : {
    4543                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4544                 :          0 :         struct btrfs_super_block *super_block = root->fs_info->super_copy;
    4545                 :            :         struct btrfs_ioctl_feature_flags features;
    4546                 :            : 
    4547                 :          0 :         features.compat_flags = btrfs_super_compat_flags(super_block);
    4548                 :          0 :         features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
    4549                 :          0 :         features.incompat_flags = btrfs_super_incompat_flags(super_block);
    4550                 :            : 
    4551         [ #  # ]:          0 :         if (copy_to_user(arg, &features, sizeof(features)))
    4552                 :            :                 return -EFAULT;
    4553                 :            : 
    4554                 :          0 :         return 0;
    4555                 :            : }
    4556                 :            : 
    4557                 :          0 : static int check_feature_bits(struct btrfs_root *root,
    4558                 :            :                               enum btrfs_feature_set set,
    4559                 :            :                               u64 change_mask, u64 flags, u64 supported_flags,
    4560                 :            :                               u64 safe_set, u64 safe_clear)
    4561                 :            : {
    4562                 :          0 :         const char *type = btrfs_feature_set_names[set];
    4563                 :            :         char *names;
    4564                 :            :         u64 disallowed, unsupported;
    4565                 :          0 :         u64 set_mask = flags & change_mask;
    4566                 :          0 :         u64 clear_mask = ~flags & change_mask;
    4567                 :            : 
    4568                 :          0 :         unsupported = set_mask & ~supported_flags;
    4569         [ #  # ]:          0 :         if (unsupported) {
    4570                 :          0 :                 names = btrfs_printable_features(set, unsupported);
    4571         [ #  # ]:          0 :                 if (names) {
    4572         [ #  # ]:          0 :                         btrfs_warn(root->fs_info,
    4573                 :            :                            "this kernel does not support the %s feature bit%s",
    4574                 :            :                            names, strchr(names, ',') ? "s" : "");
    4575                 :          0 :                         kfree(names);
    4576                 :            :                 } else
    4577                 :          0 :                         btrfs_warn(root->fs_info,
    4578                 :            :                            "this kernel does not support %s bits 0x%llx",
    4579                 :            :                            type, unsupported);
    4580                 :            :                 return -EOPNOTSUPP;
    4581                 :            :         }
    4582                 :            : 
    4583                 :          0 :         disallowed = set_mask & ~safe_set;
    4584         [ #  # ]:          0 :         if (disallowed) {
    4585                 :          0 :                 names = btrfs_printable_features(set, disallowed);
    4586         [ #  # ]:          0 :                 if (names) {
    4587         [ #  # ]:          0 :                         btrfs_warn(root->fs_info,
    4588                 :            :                            "can't set the %s feature bit%s while mounted",
    4589                 :            :                            names, strchr(names, ',') ? "s" : "");
    4590                 :          0 :                         kfree(names);
    4591                 :            :                 } else
    4592                 :          0 :                         btrfs_warn(root->fs_info,
    4593                 :            :                            "can't set %s bits 0x%llx while mounted",
    4594                 :            :                            type, disallowed);
    4595                 :            :                 return -EPERM;
    4596                 :            :         }
    4597                 :            : 
    4598                 :          0 :         disallowed = clear_mask & ~safe_clear;
    4599         [ #  # ]:          0 :         if (disallowed) {
    4600                 :          0 :                 names = btrfs_printable_features(set, disallowed);
    4601         [ #  # ]:          0 :                 if (names) {
    4602         [ #  # ]:          0 :                         btrfs_warn(root->fs_info,
    4603                 :            :                            "can't clear the %s feature bit%s while mounted",
    4604                 :            :                            names, strchr(names, ',') ? "s" : "");
    4605                 :          0 :                         kfree(names);
    4606                 :            :                 } else
    4607                 :          0 :                         btrfs_warn(root->fs_info,
    4608                 :            :                            "can't clear %s bits 0x%llx while mounted",
    4609                 :            :                            type, disallowed);
    4610                 :            :                 return -EPERM;
    4611                 :            :         }
    4612                 :            : 
    4613                 :            :         return 0;
    4614                 :            : }
    4615                 :            : 
    4616                 :            : #define check_feature(root, change_mask, flags, mask_base)      \
    4617                 :            : check_feature_bits(root, FEAT_##mask_base, change_mask, flags,  \
    4618                 :            :                    BTRFS_FEATURE_ ## mask_base ## _SUPP,        \
    4619                 :            :                    BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,    \
    4620                 :            :                    BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
    4621                 :            : 
    4622                 :          0 : static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
    4623                 :            : {
    4624                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4625                 :          0 :         struct btrfs_super_block *super_block = root->fs_info->super_copy;
    4626                 :            :         struct btrfs_ioctl_feature_flags flags[2];
    4627                 :            :         struct btrfs_trans_handle *trans;
    4628                 :            :         u64 newflags;
    4629                 :            :         int ret;
    4630                 :            : 
    4631         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    4632                 :            :                 return -EPERM;
    4633                 :            : 
    4634         [ #  # ]:          0 :         if (copy_from_user(flags, arg, sizeof(flags)))
    4635                 :            :                 return -EFAULT;
    4636                 :            : 
    4637                 :            :         /* Nothing to do */
    4638 [ #  # ][ #  # ]:          0 :         if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
                 [ #  # ]
    4639                 :          0 :             !flags[0].incompat_flags)
    4640                 :            :                 return 0;
    4641                 :            : 
    4642                 :          0 :         ret = check_feature(root, flags[0].compat_flags,
    4643                 :            :                             flags[1].compat_flags, COMPAT);
    4644         [ #  # ]:          0 :         if (ret)
    4645                 :            :                 return ret;
    4646                 :            : 
    4647                 :          0 :         ret = check_feature(root, flags[0].compat_ro_flags,
    4648                 :            :                             flags[1].compat_ro_flags, COMPAT_RO);
    4649         [ #  # ]:          0 :         if (ret)
    4650                 :            :                 return ret;
    4651                 :            : 
    4652                 :          0 :         ret = check_feature(root, flags[0].incompat_flags,
    4653                 :            :                             flags[1].incompat_flags, INCOMPAT);
    4654         [ #  # ]:          0 :         if (ret)
    4655                 :            :                 return ret;
    4656                 :            : 
    4657                 :          0 :         trans = btrfs_start_transaction(root, 0);
    4658         [ #  # ]:          0 :         if (IS_ERR(trans))
    4659                 :          0 :                 return PTR_ERR(trans);
    4660                 :            : 
    4661                 :          0 :         spin_lock(&root->fs_info->super_lock);
    4662                 :            :         newflags = btrfs_super_compat_flags(super_block);
    4663                 :          0 :         newflags |= flags[0].compat_flags & flags[1].compat_flags;
    4664                 :          0 :         newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
    4665                 :            :         btrfs_set_super_compat_flags(super_block, newflags);
    4666                 :            : 
    4667                 :            :         newflags = btrfs_super_compat_ro_flags(super_block);
    4668                 :          0 :         newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
    4669                 :          0 :         newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
    4670                 :            :         btrfs_set_super_compat_ro_flags(super_block, newflags);
    4671                 :            : 
    4672                 :            :         newflags = btrfs_super_incompat_flags(super_block);
    4673                 :          0 :         newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
    4674                 :          0 :         newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
    4675                 :            :         btrfs_set_super_incompat_flags(super_block, newflags);
    4676                 :          0 :         spin_unlock(&root->fs_info->super_lock);
    4677                 :            : 
    4678                 :          0 :         return btrfs_commit_transaction(trans, root);
    4679                 :            : }
    4680                 :            : 
    4681                 :          0 : long btrfs_ioctl(struct file *file, unsigned int
    4682                 :            :                 cmd, unsigned long arg)
    4683                 :            : {
    4684                 :          0 :         struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
    4685                 :          0 :         void __user *argp = (void __user *)arg;
    4686                 :            : 
    4687   [ #  #  #  #  :          0 :         switch (cmd) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    4688                 :            :         case FS_IOC_GETFLAGS:
    4689                 :          0 :                 return btrfs_ioctl_getflags(file, argp);
    4690                 :            :         case FS_IOC_SETFLAGS:
    4691                 :          0 :                 return btrfs_ioctl_setflags(file, argp);
    4692                 :            :         case FS_IOC_GETVERSION:
    4693                 :          0 :                 return btrfs_ioctl_getversion(file, argp);
    4694                 :            :         case FITRIM:
    4695                 :          0 :                 return btrfs_ioctl_fitrim(file, argp);
    4696                 :            :         case BTRFS_IOC_SNAP_CREATE:
    4697                 :          0 :                 return btrfs_ioctl_snap_create(file, argp, 0);
    4698                 :            :         case BTRFS_IOC_SNAP_CREATE_V2:
    4699                 :          0 :                 return btrfs_ioctl_snap_create_v2(file, argp, 0);
    4700                 :            :         case BTRFS_IOC_SUBVOL_CREATE:
    4701                 :          0 :                 return btrfs_ioctl_snap_create(file, argp, 1);
    4702                 :            :         case BTRFS_IOC_SUBVOL_CREATE_V2:
    4703                 :          0 :                 return btrfs_ioctl_snap_create_v2(file, argp, 1);
    4704                 :            :         case BTRFS_IOC_SNAP_DESTROY:
    4705                 :          0 :                 return btrfs_ioctl_snap_destroy(file, argp);
    4706                 :            :         case BTRFS_IOC_SUBVOL_GETFLAGS:
    4707                 :          0 :                 return btrfs_ioctl_subvol_getflags(file, argp);
    4708                 :            :         case BTRFS_IOC_SUBVOL_SETFLAGS:
    4709                 :          0 :                 return btrfs_ioctl_subvol_setflags(file, argp);
    4710                 :            :         case BTRFS_IOC_DEFAULT_SUBVOL:
    4711                 :          0 :                 return btrfs_ioctl_default_subvol(file, argp);
    4712                 :            :         case BTRFS_IOC_DEFRAG:
    4713                 :          0 :                 return btrfs_ioctl_defrag(file, NULL);
    4714                 :            :         case BTRFS_IOC_DEFRAG_RANGE:
    4715                 :          0 :                 return btrfs_ioctl_defrag(file, argp);
    4716                 :            :         case BTRFS_IOC_RESIZE:
    4717                 :          0 :                 return btrfs_ioctl_resize(file, argp);
    4718                 :            :         case BTRFS_IOC_ADD_DEV:
    4719                 :          0 :                 return btrfs_ioctl_add_dev(root, argp);
    4720                 :            :         case BTRFS_IOC_RM_DEV:
    4721                 :          0 :                 return btrfs_ioctl_rm_dev(file, argp);
    4722                 :            :         case BTRFS_IOC_FS_INFO:
    4723                 :          0 :                 return btrfs_ioctl_fs_info(root, argp);
    4724                 :            :         case BTRFS_IOC_DEV_INFO:
    4725                 :          0 :                 return btrfs_ioctl_dev_info(root, argp);
    4726                 :            :         case BTRFS_IOC_BALANCE:
    4727                 :          0 :                 return btrfs_ioctl_balance(file, NULL);
    4728                 :            :         case BTRFS_IOC_CLONE:
    4729                 :          0 :                 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
    4730                 :            :         case BTRFS_IOC_CLONE_RANGE:
    4731                 :          0 :                 return btrfs_ioctl_clone_range(file, argp);
    4732                 :            :         case BTRFS_IOC_TRANS_START:
    4733                 :          0 :                 return btrfs_ioctl_trans_start(file);
    4734                 :            :         case BTRFS_IOC_TRANS_END:
    4735                 :          0 :                 return btrfs_ioctl_trans_end(file);
    4736                 :            :         case BTRFS_IOC_TREE_SEARCH:
    4737                 :          0 :                 return btrfs_ioctl_tree_search(file, argp);
    4738                 :            :         case BTRFS_IOC_INO_LOOKUP:
    4739                 :          0 :                 return btrfs_ioctl_ino_lookup(file, argp);
    4740                 :            :         case BTRFS_IOC_INO_PATHS:
    4741                 :          0 :                 return btrfs_ioctl_ino_to_path(root, argp);
    4742                 :            :         case BTRFS_IOC_LOGICAL_INO:
    4743                 :          0 :                 return btrfs_ioctl_logical_to_ino(root, argp);
    4744                 :            :         case BTRFS_IOC_SPACE_INFO:
    4745                 :          0 :                 return btrfs_ioctl_space_info(root, argp);
    4746                 :            :         case BTRFS_IOC_SYNC: {
    4747                 :            :                 int ret;
    4748                 :            : 
    4749                 :          0 :                 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
    4750         [ #  # ]:          0 :                 if (ret)
    4751                 :            :                         return ret;
    4752                 :          0 :                 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
    4753                 :          0 :                 return ret;
    4754                 :            :         }
    4755                 :            :         case BTRFS_IOC_START_SYNC:
    4756                 :          0 :                 return btrfs_ioctl_start_sync(root, argp);
    4757                 :            :         case BTRFS_IOC_WAIT_SYNC:
    4758                 :          0 :                 return btrfs_ioctl_wait_sync(root, argp);
    4759                 :            :         case BTRFS_IOC_SCRUB:
    4760                 :          0 :                 return btrfs_ioctl_scrub(file, argp);
    4761                 :            :         case BTRFS_IOC_SCRUB_CANCEL:
    4762                 :          0 :                 return btrfs_ioctl_scrub_cancel(root, argp);
    4763                 :            :         case BTRFS_IOC_SCRUB_PROGRESS:
    4764                 :          0 :                 return btrfs_ioctl_scrub_progress(root, argp);
    4765                 :            :         case BTRFS_IOC_BALANCE_V2:
    4766                 :          0 :                 return btrfs_ioctl_balance(file, argp);
    4767                 :            :         case BTRFS_IOC_BALANCE_CTL:
    4768                 :          0 :                 return btrfs_ioctl_balance_ctl(root, arg);
    4769                 :            :         case BTRFS_IOC_BALANCE_PROGRESS:
    4770                 :          0 :                 return btrfs_ioctl_balance_progress(root, argp);
    4771                 :            :         case BTRFS_IOC_SET_RECEIVED_SUBVOL:
    4772                 :          0 :                 return btrfs_ioctl_set_received_subvol(file, argp);
    4773                 :            :         case BTRFS_IOC_SEND:
    4774                 :          0 :                 return btrfs_ioctl_send(file, argp);
    4775                 :            :         case BTRFS_IOC_GET_DEV_STATS:
    4776                 :          0 :                 return btrfs_ioctl_get_dev_stats(root, argp);
    4777                 :            :         case BTRFS_IOC_QUOTA_CTL:
    4778                 :          0 :                 return btrfs_ioctl_quota_ctl(file, argp);
    4779                 :            :         case BTRFS_IOC_QGROUP_ASSIGN:
    4780                 :          0 :                 return btrfs_ioctl_qgroup_assign(file, argp);
    4781                 :            :         case BTRFS_IOC_QGROUP_CREATE:
    4782                 :          0 :                 return btrfs_ioctl_qgroup_create(file, argp);
    4783                 :            :         case BTRFS_IOC_QGROUP_LIMIT:
    4784                 :          0 :                 return btrfs_ioctl_qgroup_limit(file, argp);
    4785                 :            :         case BTRFS_IOC_QUOTA_RESCAN:
    4786                 :          0 :                 return btrfs_ioctl_quota_rescan(file, argp);
    4787                 :            :         case BTRFS_IOC_QUOTA_RESCAN_STATUS:
    4788                 :          0 :                 return btrfs_ioctl_quota_rescan_status(file, argp);
    4789                 :            :         case BTRFS_IOC_QUOTA_RESCAN_WAIT:
    4790                 :          0 :                 return btrfs_ioctl_quota_rescan_wait(file, argp);
    4791                 :            :         case BTRFS_IOC_DEV_REPLACE:
    4792                 :          0 :                 return btrfs_ioctl_dev_replace(root, argp);
    4793                 :            :         case BTRFS_IOC_GET_FSLABEL:
    4794                 :          0 :                 return btrfs_ioctl_get_fslabel(file, argp);
    4795                 :            :         case BTRFS_IOC_SET_FSLABEL:
    4796                 :          0 :                 return btrfs_ioctl_set_fslabel(file, argp);
    4797                 :            :         case BTRFS_IOC_FILE_EXTENT_SAME:
    4798                 :          0 :                 return btrfs_ioctl_file_extent_same(file, argp);
    4799                 :            :         case BTRFS_IOC_GET_SUPPORTED_FEATURES:
    4800                 :          0 :                 return btrfs_ioctl_get_supported_features(file, argp);
    4801                 :            :         case BTRFS_IOC_GET_FEATURES:
    4802                 :          0 :                 return btrfs_ioctl_get_features(file, argp);
    4803                 :            :         case BTRFS_IOC_SET_FEATURES:
    4804                 :          0 :                 return btrfs_ioctl_set_features(file, argp);
    4805                 :            :         }
    4806                 :            : 
    4807                 :            :         return -ENOTTY;
    4808                 :            : }

Generated by: LCOV version 1.9