Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2007 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/btrfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include "export.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include "inode-map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include "backref.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "rcu-string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "send.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "dev-replace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "props.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include "tree-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "space-info.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include "delalloc-space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include "block-group.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * structures are incorrect, as the timespec structure from userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * is 4 bytes too small. We define these alternatives here to teach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * the kernel about the 32-bit struct packing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) struct btrfs_ioctl_timespec_32 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	__u64 sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	__u32 nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) } __attribute__ ((__packed__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) struct btrfs_ioctl_received_subvol_args_32 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	char	uuid[BTRFS_UUID_SIZE];	/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	__u64	stransid;		/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	__u64	rtransid;		/* out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	struct btrfs_ioctl_timespec_32 stime; /* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct btrfs_ioctl_timespec_32 rtime; /* out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	__u64	flags;			/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	__u64	reserved[16];		/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) } __attribute__ ((__packed__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 				struct btrfs_ioctl_received_subvol_args_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) struct btrfs_ioctl_send_args_32 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	__s64 send_fd;			/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	__u64 clone_sources_count;	/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	compat_uptr_t clone_sources;	/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	__u64 parent_root;		/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	__u64 flags;			/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	__u64 reserved[4];		/* in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) } __attribute__ ((__packed__));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 			       struct btrfs_ioctl_send_args_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) /* Mask out flags that are inappropriate for the given type of inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	else if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		return flags & ~FS_DIRSYNC_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * ioctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	unsigned int iflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	if (flags & BTRFS_INODE_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		iflags |= FS_SYNC_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	if (flags & BTRFS_INODE_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		iflags |= FS_IMMUTABLE_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (flags & BTRFS_INODE_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		iflags |= FS_APPEND_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	if (flags & BTRFS_INODE_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		iflags |= FS_NODUMP_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	if (flags & BTRFS_INODE_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		iflags |= FS_NOATIME_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (flags & BTRFS_INODE_DIRSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		iflags |= FS_DIRSYNC_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (flags & BTRFS_INODE_NODATACOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		iflags |= FS_NOCOW_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	if (flags & BTRFS_INODE_NOCOMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		iflags |= FS_NOCOMP_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	else if (flags & BTRFS_INODE_COMPRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		iflags |= FS_COMPR_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	return iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * Update inode->i_flags based on the btrfs internal flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	struct btrfs_inode *binode = BTRFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	unsigned int new_fl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	if (binode->flags & BTRFS_INODE_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		new_fl |= S_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	if (binode->flags & BTRFS_INODE_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		new_fl |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (binode->flags & BTRFS_INODE_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		new_fl |= S_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	if (binode->flags & BTRFS_INODE_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		new_fl |= S_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	if (binode->flags & BTRFS_INODE_DIRSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		new_fl |= S_DIRSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	set_mask_bits(&inode->i_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		      S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		      new_fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	struct btrfs_inode *binode = BTRFS_I(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	if (copy_to_user(arg, &flags, sizeof(flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * Check if @flags are a supported and valid set of FS_*_FL flags and that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * the old and new flags are not conflicting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static int check_fsflags(unsigned int old_flags, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		      FS_NOATIME_FL | FS_NODUMP_FL | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		      FS_SYNC_FL | FS_DIRSYNC_FL | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		      FS_NOCOMP_FL | FS_COMPR_FL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		      FS_NOCOW_FL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	/* COMPR and NOCOMP on new/old are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	/* NOCOW and compression options are mutually exclusive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	struct btrfs_inode *binode = BTRFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct btrfs_root *root = binode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	unsigned int fsflags, old_fsflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	const char *comp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	u32 binode_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	ret = check_fsflags(old_fsflags, fsflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	binode_flags = binode->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (fsflags & FS_SYNC_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		binode_flags |= BTRFS_INODE_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		binode_flags &= ~BTRFS_INODE_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	if (fsflags & FS_IMMUTABLE_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		binode_flags |= BTRFS_INODE_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		binode_flags &= ~BTRFS_INODE_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	if (fsflags & FS_APPEND_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		binode_flags |= BTRFS_INODE_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		binode_flags &= ~BTRFS_INODE_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (fsflags & FS_NODUMP_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		binode_flags |= BTRFS_INODE_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		binode_flags &= ~BTRFS_INODE_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	if (fsflags & FS_NOATIME_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		binode_flags |= BTRFS_INODE_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		binode_flags &= ~BTRFS_INODE_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	if (fsflags & FS_DIRSYNC_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		binode_flags |= BTRFS_INODE_DIRSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		binode_flags &= ~BTRFS_INODE_DIRSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	if (fsflags & FS_NOCOW_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 			 * It's safe to turn csums off here, no extents exist.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			 * Otherwise we want the flag to reflect the real COW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			 * status of the file and will not set it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			if (inode->i_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 				binode_flags |= BTRFS_INODE_NODATACOW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 						BTRFS_INODE_NODATASUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			binode_flags |= BTRFS_INODE_NODATACOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		 * Revert back under same assumptions as above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			if (inode->i_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 				binode_flags &= ~(BTRFS_INODE_NODATACOW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 						  BTRFS_INODE_NODATASUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			binode_flags &= ~BTRFS_INODE_NODATACOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	 * flag may be changed automatically if compression code won't make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	 * things smaller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (fsflags & FS_NOCOMP_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		binode_flags &= ~BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		binode_flags |= BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	} else if (fsflags & FS_COMPR_FL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		if (IS_SWAPFILE(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 			ret = -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		binode_flags |= BTRFS_INODE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		comp = btrfs_compress_type2str(fs_info->compress_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		if (!comp || comp[0] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	 * 1 for inode item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	 * 2 for properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	trans = btrfs_start_transaction(root, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (comp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 				     strlen(comp), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 				     0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		if (ret && ret != -ENODATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 			goto out_end_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	binode->flags = binode_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	btrfs_sync_inode_flags_to_i_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  out_end_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  * Translate btrfs internal inode flags to xflags as expected by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352)  * silently dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	unsigned int xflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (flags & BTRFS_INODE_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		xflags |= FS_XFLAG_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	if (flags & BTRFS_INODE_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		xflags |= FS_XFLAG_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (flags & BTRFS_INODE_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		xflags |= FS_XFLAG_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	if (flags & BTRFS_INODE_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		xflags |= FS_XFLAG_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	if (flags & BTRFS_INODE_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		xflags |= FS_XFLAG_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	return xflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) static int check_xflags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		      FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			enum btrfs_exclusive_operation type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394)  * Set the xflags from the internal inode flags. The remaining items of fsxattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * are zeroed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	struct btrfs_inode *binode = BTRFS_I(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	struct fsxattr fa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (copy_to_user(arg, &fa, sizeof(fa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	struct btrfs_inode *binode = BTRFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	struct btrfs_root *root = binode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	struct fsxattr fa, old_fa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	unsigned old_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	unsigned old_i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	if (copy_from_user(&fa, arg, sizeof(fa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	ret = check_xflags(fa.fsx_xflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	old_flags = binode->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	old_i_flags = inode->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	simple_fill_fsxattr(&old_fa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			    btrfs_inode_flags_to_xflags(binode->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	if (fa.fsx_xflags & FS_XFLAG_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		binode->flags |= BTRFS_INODE_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		binode->flags &= ~BTRFS_INODE_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		binode->flags |= BTRFS_INODE_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		binode->flags &= ~BTRFS_INODE_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (fa.fsx_xflags & FS_XFLAG_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		binode->flags |= BTRFS_INODE_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		binode->flags &= ~BTRFS_INODE_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (fa.fsx_xflags & FS_XFLAG_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		binode->flags |= BTRFS_INODE_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		binode->flags &= ~BTRFS_INODE_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	if (fa.fsx_xflags & FS_XFLAG_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		binode->flags |= BTRFS_INODE_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		binode->flags &= ~BTRFS_INODE_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	/* 1 item for the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	btrfs_sync_inode_flags_to_i_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 		binode->flags = old_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		inode->i_flags = old_i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	return put_user(inode->i_generation, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 					void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	struct btrfs_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	struct request_queue *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	struct fstrim_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	u64 minlen = ULLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	u64 num_devices = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * If the fs is mounted with nologreplay, which requires it to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 * mounted in RO mode as well, we can not allow discard on free space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 * inside block groups, because log trees refer to extents that are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	 * pinned in a block group's free space cache (pinning the extents is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 * precisely the first phase of replaying a log tree).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (btrfs_test_opt(fs_info, NOLOGREPLAY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 				dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		if (!device->bdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		q = bdev_get_queue(device->bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		if (blk_queue_discard(q)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			num_devices++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			minlen = min_t(u64, q->limits.discard_granularity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 				     minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (!num_devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (copy_from_user(&range, arg, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	 * NOTE: Don't truncate the range using super->total_bytes.  Bytenr of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	 * block group is in the logical address space, which can be any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	 * sectorsize aligned bytenr in  the range [0, U64_MAX].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (range.len < fs_info->sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	range.minlen = max(range.minlen, minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	ret = btrfs_trim_fs(fs_info, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (copy_to_user(arg, &range, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) int __pure btrfs_is_empty_uuid(u8 *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	for (i = 0; i < BTRFS_UUID_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		if (uuid[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) static noinline int create_subvol(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 				  struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				  const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 				  struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	struct btrfs_root_item *root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	struct btrfs_inode_item *inode_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	struct btrfs_root *new_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	struct btrfs_block_rsv block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	struct timespec64 cur_time = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	dev_t anon_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	u64 index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (!root_item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	ret = get_anon_bdev(&anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	 * Don't create subvolume whose level is not zero. Or qgroup will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	 * screwed up since it assumes subvolume qgroup's level to be 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	if (btrfs_qgroup_level(objectid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	 * The same as the snapshot creation, please see the comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	 * of create_snapshot().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		btrfs_subvolume_release_metadata(root, &block_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		goto fail_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	trans->block_rsv = &block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	trans->bytes_reserved = block_rsv.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 				      BTRFS_NESTING_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (IS_ERR(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		ret = PTR_ERR(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	inode_item = &root_item->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	btrfs_set_stack_inode_generation(inode_item, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	btrfs_set_stack_inode_size(inode_item, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	btrfs_set_stack_inode_nlink(inode_item, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	btrfs_set_stack_inode_nbytes(inode_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 				     fs_info->nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	btrfs_set_root_flags(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	btrfs_set_root_limit(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	btrfs_set_root_bytenr(root_item, leaf->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	btrfs_set_root_generation(root_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	btrfs_set_root_level(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	btrfs_set_root_refs(root_item, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	btrfs_set_root_used(root_item, leaf->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	btrfs_set_root_last_snapshot(root_item, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	btrfs_set_root_generation_v2(root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 			btrfs_root_generation(root_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	generate_random_guid(root_item->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	root_item->ctime = root_item->otime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	btrfs_set_root_ctransid(root_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	btrfs_set_root_otransid(root_item, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	btrfs_tree_unlock(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	btrfs_set_root_dirid(root_item, new_dirid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	key.objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	key.type = BTRFS_ROOT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 				root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		 * Since we don't abort the transaction in this case, free the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		 * tree block so that we don't leak space and leave the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		 * filesystem in an inconsistent state (an extent item in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 * extent tree without backreferences). Also no need to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 * the tree block locked since it is not in any tree at this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		 * point, so no other task can find it and use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		btrfs_free_tree_block(trans, root, leaf, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		free_extent_buffer(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	free_extent_buffer(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	leaf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (IS_ERR(new_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		free_anon_bdev(anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		ret = PTR_ERR(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	/* Freeing will be done in btrfs_put_root() of new_root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	anon_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	btrfs_record_root_in_trans(trans, new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	btrfs_put_root(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		/* We potentially lose an unused inode item here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	mutex_lock(&new_root->objectid_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	new_root->highest_objectid = new_dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	mutex_unlock(&new_root->objectid_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 * insert the directory item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 				    BTRFS_FT_DIR, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	ret = btrfs_update_inode(trans, root, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 				 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	ret = btrfs_uuid_tree_add(trans, root_item->uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 				  BTRFS_UUID_KEY_SUBVOL, objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	kfree(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	trans->block_rsv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	trans->bytes_reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	btrfs_subvolume_release_metadata(root, &block_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	err = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (err && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		inode = btrfs_lookup_dentry(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) fail_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	if (anon_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		free_anon_bdev(anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	kfree(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) static int create_snapshot(struct btrfs_root *root, struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			   struct dentry *dentry, bool readonly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 			   struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	struct btrfs_pending_snapshot *pending_snapshot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (atomic_read(&root->nr_swapfiles)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			   "cannot snapshot subvolume with active swapfile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	if (!pending_snapshot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	ret = get_anon_bdev(&pending_snapshot->anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		goto free_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	pending_snapshot->path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (!pending_snapshot->root_item || !pending_snapshot->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		goto free_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	btrfs_init_block_rsv(&pending_snapshot->block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			     BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	 * 1 - parent dir inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	 * 2 - dir entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	 * 1 - root item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	 * 2 - root ref/backref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	 * 1 - root of snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	 * 1 - UUID item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 					&pending_snapshot->block_rsv, 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 					false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		goto free_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	pending_snapshot->dentry = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	pending_snapshot->root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	pending_snapshot->readonly = readonly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	pending_snapshot->dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	pending_snapshot->inherit = inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	spin_lock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	list_add(&pending_snapshot->list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		 &trans->transaction->pending_snapshots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	spin_unlock(&fs_info->trans_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	ret = pending_snapshot->error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	ret = btrfs_orphan_cleanup(pending_snapshot->snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	pending_snapshot->anon_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	/* Prevent double freeing of anon_dev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	if (ret && pending_snapshot->snap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		pending_snapshot->snap->anon_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	btrfs_put_root(pending_snapshot->snap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) free_pending:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	if (pending_snapshot->anon_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		free_anon_bdev(pending_snapshot->anon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	kfree(pending_snapshot->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	btrfs_free_path(pending_snapshot->path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	kfree(pending_snapshot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) /*  copy of may_delete in fs/namei.c()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896)  *	Check whether we can remove a link victim from directory dir, check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897)  *  whether the type of victim is right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  *  1. We can't do it if dir is read-only (done in permission())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  *  2. We should have write and exec permissions on dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  *  3. We can't remove anything from append-only dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  *  4. We can't do anything with immutable dir (done in permission())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  *  5. If the sticky bit on dir is set we should either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  *	a. be owner of dir, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  *	b. be owner of victim, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905)  *	c. have CAP_FOWNER capability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906)  *  6. If the victim is append-only or immutable we can't do anything with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907)  *     links pointing to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908)  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909)  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910)  *  9. We can't remove a root or mountpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911)  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912)  *     nfs_async_unlink().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	if (d_really_is_negative(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	BUG_ON(d_inode(victim->d_parent) != dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (IS_APPEND(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	    IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (isdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		if (!d_is_dir(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 			return -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		if (IS_ROOT(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	} else if (d_is_dir(victim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		return -EISDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	if (IS_DEADDIR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) /* copy of may_create in fs/namei.c() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	if (d_really_is_positive(child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (IS_DEADDIR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  * Create a new subvolume below @parent.  This is largely modeled after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  * inside this filesystem so it's quite a bit simpler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) static noinline int btrfs_mksubvol(const struct path *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 				   const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				   struct btrfs_root *snap_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 				   bool readonly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 				   struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	struct inode *dir = d_inode(parent->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	if (error == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	dentry = lookup_one_len(name, parent->dentry, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	error = PTR_ERR(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (IS_ERR(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	error = btrfs_may_create(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * even if this name doesn't exist, we may get hash collisions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * check for them now when we can safely fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 					       dir->i_ino, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 					       namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	down_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		goto out_up_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	if (snap_src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		error = create_subvol(dir, dentry, name, namelen, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		fsnotify_mkdir(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) out_up_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	up_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) static noinline int btrfs_mksnapshot(const struct path *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 				   const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 				   struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 				   bool readonly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 				   struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	bool snapshot_force_cow = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 * Force new buffered writes to reserve space even when NOCOW is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 * possible. This is to avoid later writeback (running dealloc) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	 * fallback to COW mode and unexpectedly fail with ENOSPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	btrfs_drew_read_lock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	ret = btrfs_start_delalloc_snapshot(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 * All previous writes have started writeback in NOCOW mode, so now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	 * we force future writes to fallback to COW mode during snapshot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	 * creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	atomic_inc(&root->snapshot_force_cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	snapshot_force_cow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	ret = btrfs_mksubvol(parent, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			     root, readonly, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	if (snapshot_force_cow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		atomic_dec(&root->snapshot_force_cow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	btrfs_drew_read_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)  * When we're defragging a range, we don't want to kick it off again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)  * if it is really just waiting for delalloc to send it down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)  * If we find a nice big extent or delalloc range for the bytes in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)  * file you want to defrag, we return 0 to let you know to skip this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)  * part of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	read_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		end = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		if (end - offset > thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	/* if we already have a nice delalloc here, just stop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	thresh /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	end = count_range_bits(io_tree, &offset, offset + thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			       thresh, EXTENT_DELALLOC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (end >= thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)  * helper function to walk through a file and find extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)  * newer than a specific transid, and smaller than thresh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)  * This is used by the defragging code to find new and small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)  * extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) static int find_new_extents(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 			    struct inode *inode, u64 newer_than,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			    u64 *off, u32 thresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	struct btrfs_key min_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	struct btrfs_file_extent_item *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	u64 ino = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	min_key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	min_key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	min_key.offset = *off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		ret = btrfs_search_forward(root, &min_key, path, newer_than);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) process_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		if (min_key.objectid != ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 			goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		if (min_key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		extent = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 					struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		type = btrfs_file_extent_type(leaf, extent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		if (type == BTRFS_FILE_EXTENT_REG &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		    btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		    check_defrag_in_cache(inode, min_key.offset, thresh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 			*off = min_key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 			btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		if (path->slots[0] < btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 			btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 			goto process_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		if (min_key.offset == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			goto none;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		min_key.offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) none:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	u64 len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	 * hopefully we have this extent in the tree already, try without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	 * the full extent lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	read_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	em = lookup_extent_mapping(em_tree, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	read_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		struct extent_state *cached = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		u64 end = start + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		/* get the big lock and read metadata off disk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		lock_extent_bits(io_tree, start, end, &cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		unlock_extent_cached(io_tree, start, end, &cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	struct extent_map *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	bool ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	/* this is the last extent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (em->start + em->len >= i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	next = defrag_lookup_extent(inode, em->start + em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	else if ((em->block_start + em->block_len == next->block_start) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		 (em->block_len > SZ_128K && next->block_len > SZ_128K))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	free_extent_map(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			       u64 *last_len, u64 *skip, u64 *defrag_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			       int compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	bool next_mergeable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	bool prev_mergeable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	 * make sure that once we start defragging an extent, we keep on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	 * defragging it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (start < *defrag_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	*skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	em = defrag_lookup_extent(inode, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	/* this will cover holes, and inline extents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	if (!*defrag_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		prev_mergeable = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	next_mergeable = defrag_check_next_extent(inode, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	 * we hit a real extent, if it is big or the next extent is not a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	 * real extent, don't bother defragging it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	if (!compress && (*last_len == 0 || *last_len >= thresh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	    (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	 * last_len ends up being a counter of how many bytes we've defragged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	 * every time we choose not to defrag an extent, we reset *last_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	 * so that the next tiny extent will force a defrag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	 * The end result of this is that tiny extents before a single big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	 * extent will force at least part of that big extent to be defragged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		*defrag_end = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		*last_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		*skip = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		*defrag_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)  * it doesn't do much good to defrag one or two pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)  * at a time.  This pulls in a nice chunk of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  * to COW and defrag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * It also makes sure the delalloc code has enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * dirty data to avoid making new small extents as part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  * of the defrag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  * It's a good idea to start RA on this range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  * before calling this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) static int cluster_pages_for_defrag(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 				    struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 				    unsigned long start_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 				    unsigned long num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	unsigned long file_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	u64 isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	u64 page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	u64 page_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	u64 page_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	u64 start = (u64)start_index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	u64 search_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	int i_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	struct extent_io_tree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	file_end = (isize - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	if (!isize || start_index > file_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			start, page_cnt << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	i_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	tree = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	/* step one, lock all the pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	for (i = 0; i < page_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		page = find_or_create_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 					   start_index + i, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		page_start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		page_end = page_start + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			lock_extent_bits(tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 					 &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 							      page_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			unlock_extent_cached(tree, page_start, page_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 					     &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			if (!ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 			lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 			 * we unlocked the page above, so we need check if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			 * it was released or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			if (page->mapping != inode->i_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 				goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			btrfs_readpage(NULL, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 			if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 				ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		if (page->mapping != inode->i_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		pages[i] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		i_done++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	if (!i_done || ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	if (!(inode->i_sb->s_flags & SB_ACTIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	 * so now we have a nice long stream of locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	 * and up to date pages, lets wait on them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	for (i = 0; i < i_done; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		wait_on_page_writeback(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	page_start = page_offset(pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	lock_extent_bits(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 			 page_start, page_end - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	 * When defragmenting we skip ranges that have holes or inline extents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	 * (check should_defrag_range()), to avoid unnecessary IO and wasting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	 * space. At btrfs_defrag_file(), we check if a range should be defragged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	 * before locking the inode and then, if it should, we trigger a sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	 * page cache readahead - we lock the inode only after that to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	 * blocking for too long other tasks that possibly want to operate on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	 * other file ranges. But before we were able to get the inode lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	 * some other task may have punched a hole in the range, or we may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	 * now an inline extent, in which case we should not defrag. So check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	 * for that here, where we have the inode and the range locked, and bail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	 * out if that happened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	search_start = page_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	while (search_start < page_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 				      page_end - search_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 			goto out_unlock_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 			free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 			/* Ok, 0 means we did not defrag anything */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			goto out_unlock_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		search_start = extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			  page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			  EXTENT_DEFRAG, 0, 0, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	if (i_done != page_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		spin_lock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		spin_unlock(&BTRFS_I(inode)->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 				start, (page_cnt - i_done) << PAGE_SHIFT, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			  &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			     page_start, page_end - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	for (i = 0; i < i_done; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		clear_page_dirty_for_io(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		ClearPageChecked(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 		set_page_extent_mapped(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		set_page_dirty(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	return i_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) out_unlock_range:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	unlock_extent_cached(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			     page_start, page_end - 1, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	for (i = 0; i < i_done; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 		unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 			start, page_cnt << PAGE_SHIFT, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) int btrfs_defrag_file(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		      struct btrfs_ioctl_defrag_range_args *range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		      u64 newer_than, unsigned long max_to_defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	struct file_ra_state *ra = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	unsigned long last_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	u64 isize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	u64 last_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	u64 skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	u64 defrag_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	u64 newer_off = range->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	unsigned long ra_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	int defrag_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	int compress_type = BTRFS_COMPRESS_ZLIB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	u32 extent_thresh = range->extent_thresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	unsigned long cluster = max_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	u64 new_align = ~((u64)SZ_128K - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	struct page **pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	if (isize == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	if (range->start >= isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	if (do_compress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		if (range->compress_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 			compress_type = range->compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	if (extent_thresh == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		extent_thresh = SZ_256K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	 * If we were not given a file, allocate a readahead context. As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	 * readahead is just an optimization, defrag will work without it so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	 * we don't error out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	if (!file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		ra = kzalloc(sizeof(*ra), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		if (ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 			file_ra_state_init(ra, inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 		ra = &file->f_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	if (!pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		goto out_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	/* find the last page to defrag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (range->start + range->len > range->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		last_index = min_t(u64, isize - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			 range->start + range->len - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		last_index = (isize - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	if (newer_than) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 		ret = find_new_extents(root, inode, newer_than,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 				       &newer_off, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			range->start = newer_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			 * we always align our defrag to help keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			 * the extents in the file evenly spaced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 			i = (newer_off & new_align) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 			goto out_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		i = range->start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	if (!max_to_defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		max_to_defrag = last_index - i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	 * make writeback starts from i, so the defrag range can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	 * written sequentially.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	if (i < inode->i_mapping->writeback_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 		inode->i_mapping->writeback_index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	while (i <= last_index && defrag_count < max_to_defrag &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	       (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		 * make sure we stop running if someone unmounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 		 * the FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 		if (!(inode->i_sb->s_flags & SB_ACTIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		if (btrfs_defrag_cancelled(fs_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			btrfs_debug(fs_info, "defrag_file cancelled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 					 extent_thresh, &last_len, &skip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 					 &defrag_end, do_compress)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			unsigned long next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			 * the should_defrag function tells us how much to skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 			 * bump our counter by the suggested amount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			next = DIV_ROUND_UP(skip, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			i = max(i + 1, next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 		if (!newer_than) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			cluster = (PAGE_ALIGN(defrag_end) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 				   PAGE_SHIFT) - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			cluster = min(cluster, max_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 			cluster = max_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		if (i + cluster > ra_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 			ra_index = max(i, ra_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 			if (ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 				page_cache_sync_readahead(inode->i_mapping, ra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 						file, ra_index, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			ra_index += cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 		if (IS_SWAPFILE(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 			ret = -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			if (do_compress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 				BTRFS_I(inode)->defrag_compress = compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			ret = cluster_pages_for_defrag(inode, pages, i, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 			inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 			goto out_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		defrag_count += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		balance_dirty_pages_ratelimited(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		if (newer_than) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			if (newer_off == (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 				i += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 			newer_off = max(newer_off + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 					(u64)i << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 			ret = find_new_extents(root, inode, newer_than,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 					       &newer_off, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 				range->start = newer_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 				i = (newer_off & new_align) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 				i += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 				last_len += ret << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 				i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 				last_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 			     &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	if (range->compress_type == BTRFS_COMPRESS_LZO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	} else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	ret = defrag_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) out_ra:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	if (do_compress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (!file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		kfree(ra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) static noinline int btrfs_ioctl_resize(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 					void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	u64 new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	u64 old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	u64 devid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	struct btrfs_ioctl_vol_args *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	struct btrfs_device *device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	char *sizestr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	char *retptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	char *devstr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	int mod = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	if (IS_ERR(vol_args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		ret = PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	sizestr = vol_args->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	devstr = strchr(sizestr, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	if (devstr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		sizestr = devstr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		*devstr = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		devstr = vol_args->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		ret = kstrtoull(devstr, 10, &devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 		if (!devid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		btrfs_info(fs_info, "resizing devid %llu", devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	if (!device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 		btrfs_info(fs_info, "resizer unable to find device %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 			   devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		btrfs_info(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 			   "resizer unable to apply on readonly device %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		       devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	if (!strcmp(sizestr, "max"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 		new_size = device->bdev->bd_inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 		if (sizestr[0] == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 			mod = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 			sizestr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		} else if (sizestr[0] == '+') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 			mod = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 			sizestr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		new_size = memparse(sizestr, &retptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		if (*retptr != '\0' || new_size == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	old_size = btrfs_device_get_total_bytes(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	if (mod < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		if (new_size > old_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		new_size = old_size - new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	} else if (mod > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		if (new_size > ULLONG_MAX - old_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 			ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		new_size = old_size + new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	if (new_size < SZ_256M) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	if (new_size > device->bdev->bd_inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		ret = -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	new_size = round_down(new_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	if (new_size > old_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		ret = btrfs_grow_device(trans, device, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	} else if (new_size < old_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		ret = btrfs_shrink_device(device, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	} /* equal, nothing need to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	if (ret == 0 && new_size != old_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 		btrfs_info_in_rcu(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			"resize device %s (devid %llu) from %llu to %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 			rcu_str_deref(device->name), device->devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			old_size, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) static noinline int __btrfs_ioctl_snap_create(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 				const char *name, unsigned long fd, int subvol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 				bool readonly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 				struct btrfs_qgroup_inherit *inherit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	if (!S_ISDIR(file_inode(file)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 		return -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	namelen = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	if (strchr(name, '/')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	if (name[0] == '.' &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	   (namelen == 1 || (name[1] == '.' && namelen == 2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	if (subvol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		ret = btrfs_mksubvol(&file->f_path, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 				     NULL, readonly, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		struct fd src = fdget(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		struct inode *src_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		if (!src.file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 			goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		src_inode = file_inode(src.file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		if (src_inode->i_sb != file_inode(file)->i_sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 				   "Snapshot src from another FS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 			ret = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		} else if (!inode_owner_or_capable(src_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 			 * Subvolume creation is not restricted, but snapshots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 			 * are limited to own subvolumes only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 			ret = btrfs_mksnapshot(&file->f_path, name, namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 					     BTRFS_I(src_inode)->root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 					     readonly, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		fdput(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) static noinline int btrfs_ioctl_snap_create(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 					    void __user *arg, int subvol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	struct btrfs_ioctl_vol_args *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	if (!S_ISDIR(file_inode(file)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		return -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	if (IS_ERR(vol_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		return PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 					subvol, false, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 					       void __user *arg, int subvol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	struct btrfs_ioctl_vol_args_v2 *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	bool readonly = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	struct btrfs_qgroup_inherit *inherit = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	if (!S_ISDIR(file_inode(file)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		return -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	if (IS_ERR(vol_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		return PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		goto free_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		readonly = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		u64 nums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		if (vol_args->size < sizeof(*inherit) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 		    vol_args->size > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 			goto free_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		if (IS_ERR(inherit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 			ret = PTR_ERR(inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 			goto free_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		if (inherit->num_qgroups > PAGE_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		    inherit->num_ref_copies > PAGE_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		    inherit->num_excl_copies > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 			goto free_inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		       2 * inherit->num_excl_copies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		if (vol_args->size != struct_size(inherit, qgroups, nums)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 			goto free_inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 					subvol, readonly, inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		goto free_inherit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) free_inherit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	kfree(inherit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) free_args:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 						void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	u64 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	down_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	if (btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		flags |= BTRFS_SUBVOL_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	up_read(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	if (copy_to_user(arg, &flags, sizeof(flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 					      void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	u64 root_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	u64 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	if (copy_from_user(&flags, arg, sizeof(flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	if (flags & ~BTRFS_SUBVOL_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	down_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	/* nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		goto out_drop_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	root_flags = btrfs_root_flags(&root->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	if (flags & BTRFS_SUBVOL_RDONLY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		btrfs_set_root_flags(&root->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 				     root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		 * Block RO -> RW transition if this subvolume is involved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		 * send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		spin_lock(&root->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		if (root->send_in_progress == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			btrfs_set_root_flags(&root->root_item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 				     root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 			spin_unlock(&root->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			spin_unlock(&root->root_item_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 				   "Attempt to set subvolume %llu read-write during send",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 				   root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 			goto out_drop_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 		goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	ret = btrfs_update_root(trans, fs_info->tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 				&root->root_key, &root->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		goto out_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) out_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		btrfs_set_root_flags(&root->root_item, root_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) out_drop_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	up_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) static noinline int key_in_sk(struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			      struct btrfs_ioctl_search_key *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 	struct btrfs_key test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	test.objectid = sk->min_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	test.type = sk->min_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	test.offset = sk->min_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	ret = btrfs_comp_cpu_keys(key, &test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	test.objectid = sk->max_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	test.type = sk->max_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	test.offset = sk->max_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	ret = btrfs_comp_cpu_keys(key, &test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) static noinline int copy_to_sk(struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 			       struct btrfs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 			       struct btrfs_ioctl_search_key *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			       size_t *buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			       char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 			       unsigned long *sk_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 			       int *num_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	u64 found_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	struct btrfs_ioctl_search_header sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	struct btrfs_key test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	unsigned long item_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	unsigned long item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	int nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	nritems = btrfs_header_nritems(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	if (btrfs_header_generation(leaf) > sk->max_transid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 		i = nritems;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		goto advance_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	found_transid = btrfs_header_generation(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	for (i = slot; i < nritems; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		item_off = btrfs_item_ptr_offset(leaf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		item_len = btrfs_item_size_nr(leaf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 		btrfs_item_key_to_cpu(leaf, key, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		if (!key_in_sk(key, sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 		if (sizeof(sh) + item_len > *buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 			if (*num_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 				ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 			 * return one empty item back for v1, which does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 			 * handle -EOVERFLOW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 			*buf_size = sizeof(sh) + item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 			item_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 			ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 		if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		sh.objectid = key->objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		sh.offset = key->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		sh.type = key->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		sh.len = item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 		sh.transid = found_transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 		 * Copy search result header. If we fault then loop again so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		 * can fault in the pages and -EFAULT there if there's a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		 * problem. Otherwise we'll fault and then copy the buffer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		 * properly this next time through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		*sk_offset += sizeof(sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 		if (item_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 			char __user *up = ubuf + *sk_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 			 * Copy the item, same behavior as above, but reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 			 * * sk_offset so we copy the full thing again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			if (read_extent_buffer_to_user_nofault(leaf, up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 						item_off, item_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 				*sk_offset -= sizeof(sh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 			*sk_offset += item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		(*num_found)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		if (ret) /* -EOVERFLOW from above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		if (*num_found >= sk->nr_items) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) advance_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	test.objectid = sk->max_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	test.type = sk->max_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	test.offset = sk->max_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 	if (btrfs_comp_cpu_keys(key, &test) >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	else if (key->offset < (u64)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		key->offset++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	else if (key->type < (u8)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		key->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		key->type++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	} else if (key->objectid < (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		key->offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		key->type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		key->objectid++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	 *  0: all items from this leaf copied, continue with next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	 *  1: * more items can be copied, but unused buffer is too small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	 *     * all items were found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	 *     Either way, it will stops the loop which iterates to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	 *     leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	 *  -EOVERFLOW: item was to large for buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	 *  -EFAULT: could not copy extent buffer back to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) static noinline int search_ioctl(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 				 struct btrfs_ioctl_search_key *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 				 size_t *buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 				 char __user *ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 	int num_found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	unsigned long sk_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		*buf_size = sizeof(struct btrfs_ioctl_search_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 	if (sk->tree_id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		/* search the root of the inode that was passed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		root = btrfs_grab_root(BTRFS_I(inode)->root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		root = btrfs_get_fs_root(info, sk->tree_id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 		if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 			btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 			return PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	key.objectid = sk->min_objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	key.type = sk->min_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	key.offset = sk->min_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		ret = fault_in_pages_writeable(ubuf + sk_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 					       *buf_size - sk_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		ret = btrfs_search_forward(root, &key, path, sk->min_transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 			if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 				ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 				 &sk_offset, &num_found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	sk->nr_items = num_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) static noinline int btrfs_ioctl_tree_search(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 					   void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	struct btrfs_ioctl_search_args __user *uargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	struct btrfs_ioctl_search_key sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	uargs = (struct btrfs_ioctl_search_args __user *)argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 	if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	buf_size = sizeof(uargs->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	 * In the origin implementation an overflow is handled by returning a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	 * search header with a len of zero, so reset ret.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	if (ret == -EOVERFLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 					       void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	struct btrfs_ioctl_search_args_v2 __user *uarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	struct btrfs_ioctl_search_args_v2 args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	size_t buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	const size_t buf_limit = SZ_16M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	/* copy search header and buffer size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	if (copy_from_user(&args, uarg, sizeof(args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	buf_size = args.buf_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	/* limit result size to 16MB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	if (buf_size > buf_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		buf_size = buf_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	ret = search_ioctl(inode, &args.key, &buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			   (char __user *)(&uarg->buf[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	else if (ret == -EOVERFLOW &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373)  * Search INODE_REFs to identify path name of 'dirid' directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374)  * in a 'tree_id' tree. and sets path name to 'name'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 				u64 tree_id, u64 dirid, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	int total_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	struct btrfs_inode_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	struct extent_buffer *l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		name[0]='\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	root = btrfs_get_fs_root(info, tree_id, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		ret = PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 		root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 			ret = btrfs_previous_item(root, path, dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 						  BTRFS_INODE_REF_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 			else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 		l = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		btrfs_item_key_to_cpu(l, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		len = btrfs_inode_ref_name_len(l, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		ptr -= len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		total_len += len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		if (ptr < name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 			ret = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 		*(ptr + len) = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 		if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 		key.objectid = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 		key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		dirid = key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	memmove(name, ptr, total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	name[total_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) static int btrfs_search_path_in_tree_user(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 				struct btrfs_ioctl_ino_lookup_user_args *args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	struct btrfs_key upper_limit = BTRFS_I(inode)->location;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	u64 dirid = args->dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	unsigned long item_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	unsigned long item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	struct btrfs_inode_ref *iref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	struct btrfs_root_ref *rref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	struct btrfs_root *root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 	struct btrfs_key key, key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	struct inode *temp_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	char *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	int total_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	 * If the bottom subvolume does not exist directly under upper_limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	 * construct the path in from the bottom up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	if (dirid != upper_limit.objectid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 		ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		root = btrfs_get_fs_root(fs_info, treeid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 		if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			ret = PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 		key.objectid = dirid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		key.type = BTRFS_INODE_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 			ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 			} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 				ret = btrfs_previous_item(root, path, dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 							  BTRFS_INODE_REF_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 				if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 					goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 				} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 					ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 					goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 			leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 			slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 			btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 			iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 			len = btrfs_inode_ref_name_len(leaf, iref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 			ptr -= len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 			total_len += len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 			if (ptr < args->path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 				ret = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 			*(ptr + len) = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 			read_extent_buffer(leaf, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 					(unsigned long)(iref + 1), len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 			/* Check the read+exec permission of this directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 			ret = btrfs_previous_item(root, path, dirid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 						  BTRFS_INODE_ITEM_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 			leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 			slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 			btrfs_item_key_to_cpu(leaf, &key2, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			if (key2.objectid != dirid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 			temp_inode = btrfs_iget(sb, key2.objectid, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 			if (IS_ERR(temp_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 				ret = PTR_ERR(temp_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 			ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 			iput(temp_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 				ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 			if (key.offset == upper_limit.objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 			if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 				ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 			btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 			key.objectid = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 			key.offset = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 			dirid = key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 		memmove(args->path, ptr, total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 		args->path[total_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 		btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 		root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	/* Get the bottom subvolume's name from ROOT_REF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) 	key.objectid = treeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	key.offset = args->treeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	item_off = btrfs_item_ptr_offset(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	item_len = btrfs_item_size_nr(leaf, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	/* Check if dirid in ROOT_REF corresponds to passed dirid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	/* Copy subvolume's name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	item_off += sizeof(struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	item_len -= sizeof(struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	read_extent_buffer(leaf, args->name, item_off, item_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	args->name[item_len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) static noinline int btrfs_ioctl_ino_lookup(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 					   void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	struct btrfs_ioctl_ino_lookup_args *args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	args = memdup_user(argp, sizeof(*args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	if (IS_ERR(args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 		return PTR_ERR(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	 * Unprivileged query to obtain the containing subvolume root id. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	 * path is reset so it's consistent with btrfs_search_path_in_tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	if (args->treeid == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 		args->treeid = BTRFS_I(inode)->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 		args->name[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	if (!capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 		ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 					args->treeid, args->objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 					args->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	kfree(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666)  * Version of ino_lookup ioctl (unprivileged)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668)  * The main differences from ino_lookup ioctl are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670)  *   1. Read + Exec permission will be checked using inode_permission() during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671)  *      path construction. -EACCES will be returned in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672)  *   2. Path construction will be stopped at the inode number which corresponds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673)  *      to the fd with which this ioctl is called. If constructed path does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674)  *      exist under fd's inode, -EACCES will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675)  *   3. The name of bottom subvolume is also searched and filled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	struct btrfs_ioctl_ino_lookup_user_args *args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	args = memdup_user(argp, sizeof(*args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	if (IS_ERR(args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 		return PTR_ERR(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 	if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) 	    BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 		 * The subvolume does not exist under fd with which this is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 		 * called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		kfree(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 		return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	ret = btrfs_search_path_in_tree_user(inode, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	kfree(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	struct btrfs_ioctl_get_subvol_info_args *subvol_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	struct btrfs_root_item *root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	struct btrfs_root_ref *rref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	unsigned long item_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 	unsigned long item_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	if (!subvol_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	/* Get root_item of inode's subvolume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	key.objectid = BTRFS_I(inode)->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	root = btrfs_get_fs_root(fs_info, key.objectid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	if (IS_ERR(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 		ret = PTR_ERR(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	root_item = &root->root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	subvol_info->treeid = key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	subvol_info->generation = btrfs_root_generation(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	subvol_info->flags = btrfs_root_flags(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 	memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 						    BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	memcpy(subvol_info->received_uuid, root_item->received_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 						    BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	subvol_info->ctransid = btrfs_root_ctransid(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 	subvol_info->otransid = btrfs_root_otransid(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	subvol_info->stransid = btrfs_root_stransid(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	subvol_info->rtransid = btrfs_root_rtransid(root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		/* Search root tree for ROOT_BACKREF of this subvolume */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 		key.type = BTRFS_ROOT_BACKREF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		key.offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 		} else if (path->slots[0] >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 			   btrfs_header_nritems(path->nodes[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 			ret = btrfs_next_leaf(fs_info->tree_root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 			if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 			} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 				ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 		btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		if (key.objectid == subvol_info->treeid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 		    key.type == BTRFS_ROOT_BACKREF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 			subvol_info->parent_id = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 			rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 			subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 			item_off = btrfs_item_ptr_offset(leaf, slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 					+ sizeof(struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 			item_len = btrfs_item_size_nr(leaf, slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 					- sizeof(struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 			read_extent_buffer(leaf, subvol_info->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 					   item_off, item_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 			ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	btrfs_put_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	kfree(subvol_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826)  * Return ROOT_REF information of the subvolume containing this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827)  * except the subvolume name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 	struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	struct btrfs_root_ref *rref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	struct btrfs_root *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 	struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	u64 objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	u8 found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	rootrefs = memdup_user(argp, sizeof(*rootrefs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	if (IS_ERR(rootrefs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 		btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		return PTR_ERR(rootrefs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	root = BTRFS_I(inode)->root->fs_info->tree_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	objectid = BTRFS_I(inode)->root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 	key.objectid = objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	key.type = BTRFS_ROOT_REF_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 	key.offset = rootrefs->min_treeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	} else if (path->slots[0] >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 		   btrfs_header_nritems(path->nodes[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 		} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 			ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 		slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 		btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 		if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 		if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 			ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 		rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		rootrefs->rootref[found].treeid = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 		rootrefs->rootref[found].dirid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 				  btrfs_root_ref_dirid(leaf, rref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 		found++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		ret = btrfs_next_item(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 		} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 			ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 	if (!ret || ret == -EOVERFLOW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 		rootrefs->num_items = found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 		/* update min_treeid for next search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		if (found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 			rootrefs->min_treeid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 				rootrefs->rootref[found - 1].treeid + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	kfree(rootrefs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) static noinline int btrfs_ioctl_snap_destroy(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 					     void __user *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 					     bool destroy_v2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	struct dentry *parent = file->f_path.dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 	struct inode *dir = d_inode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 	struct btrfs_root *root = BTRFS_I(dir)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 	struct btrfs_root *dest = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	struct btrfs_ioctl_vol_args *vol_args = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 	struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 	char *subvol_name, *subvol_name_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 	int subvol_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 	bool destroy_parent = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 	if (destroy_v2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 		vol_args2 = memdup_user(arg, sizeof(*vol_args2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 		if (IS_ERR(vol_args2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 			return PTR_ERR(vol_args2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 			err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 		 * If SPEC_BY_ID is not set, we are looking for the subvolume by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 		 * name, same as v1 currently does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 		if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 			vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 			subvol_name = vol_args2->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 			err = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 			if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 			err = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 			dentry = btrfs_get_dentry(fs_info->sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 					BTRFS_FIRST_FREE_OBJECTID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 					vol_args2->subvolid, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 			if (IS_ERR(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 				err = PTR_ERR(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 				goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 			 * Change the default parent since the subvolume being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 			 * deleted can be outside of the current mount point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 			parent = btrfs_get_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 			 * At this point dentry->d_name can point to '/' if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 			 * subvolume we want to destroy is outsite of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 			 * current mount point, so we need to release the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 			 * current dentry and execute the lookup to return a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 			 * one with ->d_name pointing to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 			 * <mount point>/subvol_name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 			dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 			if (IS_ERR(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 				err = PTR_ERR(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 				goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 			dir = d_inode(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 			 * If v2 was used with SPEC_BY_ID, a new parent was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 			 * allocated since the subvolume can be outside of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 			 * current mount point. Later on we need to release this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 			 * new parent dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 			destroy_parent = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 			subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 						fs_info, vol_args2->subvolid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 			if (IS_ERR(subvol_name_ptr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 				err = PTR_ERR(subvol_name_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 				goto free_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 			/* subvol_name_ptr is already NULL termined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 			subvol_name = (char *)kbasename(subvol_name_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 		if (IS_ERR(vol_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 			return PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 		vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 		subvol_name = vol_args->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		err = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 	subvol_namelen = strlen(subvol_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	if (strchr(subvol_name, '/') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	    strncmp(subvol_name, "..", subvol_namelen) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 		goto free_subvol_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	if (!S_ISDIR(dir->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 		err = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 		goto free_subvol_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 	if (err == -EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		goto free_subvol_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 	dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	if (IS_ERR(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		err = PTR_ERR(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 		goto out_unlock_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	if (d_really_is_negative(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	dest = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	if (!capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 		 * Regular user.  Only allow this with a special mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 		 * option, when the user has write+exec access to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 		 * subvol root, and when rmdir(2) would have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		 * allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		 * Note that this is _not_ check that the subvol is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 		 * empty or doesn't contain data that we wouldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 		 * otherwise be able to delete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 		 * Users who want to delete empty subvols should try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 		 * rmdir(2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 		if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 			goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 		 * Do not allow deletion if the parent dir is the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 		 * as the dir to be deleted.  That means the ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		 * must be called on the dentry referencing the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 		 * of the subvol, not a random directory contained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 		 * within it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		if (root == dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 			goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 		err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 			goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	/* check if subvolume may be deleted by a user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	err = btrfs_may_delete(dir, dentry, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	err = btrfs_delete_subvolume(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 		d_delete_notify(dir, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) out_unlock_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) free_subvol_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	kfree(subvol_name_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) free_parent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	if (destroy_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	kfree(vol_args2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	struct btrfs_ioctl_defrag_range_args *range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	if (btrfs_root_readonly(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 		ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	switch (inode->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 		if (!capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 		ret = btrfs_defrag_root(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 		 * Note that this does not check the file descriptor for write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 		 * access. This prevents defragmenting executables that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 		 * running and allows defrag on files open in read-only mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 		if (!capable(CAP_SYS_ADMIN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 		    inode_permission(inode, MAY_WRITE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 		range = kzalloc(sizeof(*range), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 		if (!range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 		if (argp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 			if (copy_from_user(range, argp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 					   sizeof(*range))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 				ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 				kfree(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 			/* compression requires us to start the IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 			if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 				range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 				range->extent_thresh = (u32)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 			/* the rest are all set to zero by kzalloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 			range->len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 		ret = btrfs_defrag_file(file_inode(file), file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 					range, BTRFS_OLDEST_GENERATION, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 		kfree(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	struct btrfs_ioctl_vol_args *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 		return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	if (IS_ERR(vol_args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 		ret = PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	ret = btrfs_init_new_device(fs_info, vol_args->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 		btrfs_info(fs_info, "disk added %s", vol_args->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	struct btrfs_ioctl_vol_args_v2 *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	if (IS_ERR(vol_args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 		ret = PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 		goto err_drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 		ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		ret = btrfs_rm_device(fs_info, vol_args->name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 			btrfs_info(fs_info, "device deleted: id %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 					vol_args->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 			btrfs_info(fs_info, "device deleted: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 					vol_args->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) err_drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 	struct btrfs_ioctl_vol_args *vol_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	vol_args = memdup_user(arg, sizeof(*vol_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 	if (IS_ERR(vol_args)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 		ret = PTR_ERR(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 	vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	ret = btrfs_rm_device(fs_info, vol_args->name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 		btrfs_info(fs_info, "disk deleted %s", vol_args->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	kfree(vol_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 	btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 				void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	struct btrfs_ioctl_fs_info_args *fi_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	struct btrfs_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 	u64 flags_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 	fi_args = memdup_user(arg, sizeof(*fi_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	if (IS_ERR(fi_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 		return PTR_ERR(fi_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	flags_in = fi_args->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 	memset(fi_args, 0, sizeof(*fi_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 	fi_args->num_devices = fs_devices->num_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 		if (device->devid > fi_args->max_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 			fi_args->max_id = device->devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	fi_args->nodesize = fs_info->nodesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 	fi_args->sectorsize = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	fi_args->clone_alignment = fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 	if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 		fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 		fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 		fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) 		fi_args->generation = fs_info->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 		fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) 	if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 		memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 		       sizeof(fi_args->metadata_uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 		fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	kfree(fi_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 				 void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) 	struct btrfs_ioctl_dev_info_args *di_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	struct btrfs_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	char *s_uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 	di_args = memdup_user(arg, sizeof(*di_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 	if (IS_ERR(di_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 		return PTR_ERR(di_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 	if (!btrfs_is_empty_uuid(di_args->uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		s_uuid = di_args->uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 				NULL, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 	di_args->devid = dev->devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 	di_args->bytes_used = btrfs_device_get_bytes_used(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 	di_args->total_bytes = btrfs_device_get_total_bytes(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 	memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	if (dev->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 		strncpy(di_args->path, rcu_str_deref(dev->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 				sizeof(di_args->path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 		di_args->path[sizeof(di_args->path) - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 		di_args->path[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 	if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	kfree(di_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 	struct btrfs_root *new_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	struct btrfs_dir_item *di;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	struct btrfs_disk_key disk_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	u64 objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	u64 dir_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 	if (copy_from_user(&objectid, argp, sizeof(objectid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 	if (!objectid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 		objectid = BTRFS_FS_TREE_OBJECTID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 	new_root = btrfs_get_fs_root(fs_info, objectid, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	if (IS_ERR(new_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 		ret = PTR_ERR(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	if (!is_fstree(new_root->root_key.objectid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	path->leave_spinning = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 	trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 	di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 				   dir_id, "default", 7, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 	if (IS_ERR_OR_NULL(di)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 		btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 			  "Umm, you don't have the default diritem, this isn't going to work");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 	btrfs_mark_buffer_dirty(path->nodes[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 	btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	btrfs_put_root(new_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) static void get_block_group_info(struct list_head *groups_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 				 struct btrfs_ioctl_space_info *space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 	struct btrfs_block_group *block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 	space->total_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 	space->used_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 	space->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 	list_for_each_entry(block_group, groups_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 		space->flags = block_group->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 		space->total_bytes += block_group->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 		space->used_bytes += block_group->used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 				   void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	struct btrfs_ioctl_space_args space_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 	struct btrfs_ioctl_space_info space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 	struct btrfs_ioctl_space_info *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	struct btrfs_ioctl_space_info *dest_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	struct btrfs_ioctl_space_info __user *user_dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 	struct btrfs_space_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	static const u64 types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 		BTRFS_BLOCK_GROUP_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 		BTRFS_BLOCK_GROUP_SYSTEM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 		BTRFS_BLOCK_GROUP_METADATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 		BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 	int num_types = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 	int alloc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 	u64 slot_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 	int i, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 	if (copy_from_user(&space_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 			   (struct btrfs_ioctl_space_args __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 			   sizeof(space_args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	for (i = 0; i < num_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 		struct btrfs_space_info *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 		info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 		list_for_each_entry(tmp, &fs_info->space_info, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 			if (tmp->flags == types[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 				info = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 		if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 		down_read(&info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 			if (!list_empty(&info->block_groups[c]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 				slot_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 		up_read(&info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	 * Global block reserve, exported as a space_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	slot_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	/* space_slots == 0 means they are asking for a count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	if (space_args.space_slots == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 		space_args.total_spaces = slot_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 	slot_count = min_t(u64, space_args.space_slots, slot_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 	alloc_size = sizeof(*dest) * slot_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 	/* we generally have at most 6 or so space infos, one for each raid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 	 * level.  So, a whole page should be more than enough for everyone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	if (alloc_size > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	space_args.total_spaces = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 	dest = kmalloc(alloc_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 	if (!dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 	dest_orig = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 	/* now we have a buffer to copy into */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 	for (i = 0; i < num_types; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 		struct btrfs_space_info *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 		if (!slot_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 		info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		list_for_each_entry(tmp, &fs_info->space_info, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 			if (tmp->flags == types[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 				info = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 		if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 		down_read(&info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 		for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 			if (!list_empty(&info->block_groups[c])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 				get_block_group_info(&info->block_groups[c],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 						     &space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 				memcpy(dest, &space, sizeof(space));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 				dest++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 				space_args.total_spaces++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 				slot_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 			if (!slot_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 		up_read(&info->groups_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 	 * Add global block reserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 	if (slot_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 		struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 		spin_lock(&block_rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 		space.total_bytes = block_rsv->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 		space.used_bytes = block_rsv->size - block_rsv->reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		spin_unlock(&block_rsv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 		space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 		memcpy(dest, &space, sizeof(space));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 		space_args.total_spaces++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	user_dest = (struct btrfs_ioctl_space_info __user *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		(arg + sizeof(struct btrfs_ioctl_space_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	if (copy_to_user(user_dest, dest_orig, alloc_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	kfree(dest_orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 	if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 					    void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 	u64 transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	trans = btrfs_attach_transaction_barrier(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		if (PTR_ERR(trans) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 			return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		/* No running transaction, don't bother */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 		transid = root->fs_info->last_trans_committed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 	transid = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 	ret = btrfs_commit_transaction_async(trans, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 	if (argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		if (copy_to_user(argp, &transid, sizeof(transid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 					   void __user *argp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	u64 transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	if (argp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 		if (copy_from_user(&transid, argp, sizeof(transid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 		transid = 0;  /* current trans */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	return btrfs_wait_for_commit(fs_info, transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 	struct btrfs_ioctl_scrub_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 	if (IS_ERR(sa))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 		return PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 	if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 		ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 	ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 			      &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 			      0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 	 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 	 * error. This is important as it allows user space to know how much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 	 * progress scrub has done. For example, if scrub is canceled we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	 * space. Later user space can inspect the progress from the structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	 * btrfs_ioctl_scrub_args and resume scrub from where it left off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	 * previously (btrfs-progs does this).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	 * then return -EFAULT to signal the structure was not copied or it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	 * be corrupt and unreliable due to a partial copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	if (copy_to_user(arg, sa, sizeof(*sa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 	if (!(sa->flags & BTRFS_SCRUB_READONLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 		mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 	return btrfs_scrub_cancel(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 				       void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 	struct btrfs_ioctl_scrub_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	if (IS_ERR(sa))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 		return PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 				      void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 	struct btrfs_ioctl_get_dev_stats *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	if (IS_ERR(sa))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 		return PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 	if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 		kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 	ret = btrfs_get_dev_stats(fs_info, sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 	if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 				    void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 	struct btrfs_ioctl_dev_replace_args *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 	p = memdup_user(arg, sizeof(*p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 	if (IS_ERR(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 		return PTR_ERR(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 	switch (p->cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 	case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 		if (sb_rdonly(fs_info->sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 			ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 		if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 			ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 			ret = btrfs_dev_replace_by_ioctl(fs_info, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 			btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 	case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 		btrfs_dev_replace_status(fs_info, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 	case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 		p->result = btrfs_dev_replace_cancel(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 	if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 	kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 	u64 rel_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 	struct btrfs_ioctl_ino_path_args *ipa = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 	struct inode_fs_paths *ipath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 	struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 	if (!capable(CAP_DAC_READ_SEARCH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 	ipa = memdup_user(arg, sizeof(*ipa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	if (IS_ERR(ipa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		ret = PTR_ERR(ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 		ipa = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 	size = min_t(u32, ipa->size, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 	ipath = init_ipath(size, root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 	if (IS_ERR(ipath)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 		ret = PTR_ERR(ipath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 		ipath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 	ret = paths_from_inode(ipa->inum, ipath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 		rel_ptr = ipath->fspath->val[i] -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 			  (u64)(unsigned long)ipath->fspath->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 		ipath->fspath->val[i] = rel_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 			   ipath->fspath, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 	free_ipath(ipath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 	kfree(ipa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 	struct btrfs_data_container *inodes = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	const size_t c = 3 * sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	if (inodes->bytes_left >= c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 		inodes->bytes_left -= c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 		inodes->val[inodes->elem_cnt] = inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 		inodes->val[inodes->elem_cnt + 1] = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 		inodes->val[inodes->elem_cnt + 2] = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 		inodes->elem_cnt += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 		inodes->bytes_missing += c - inodes->bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 		inodes->bytes_left = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 		inodes->elem_missed += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 					void __user *arg, int version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 	struct btrfs_ioctl_logical_ino_args *loi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	struct btrfs_data_container *inodes = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 	struct btrfs_path *path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 	bool ignore_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 	loi = memdup_user(arg, sizeof(*loi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 	if (IS_ERR(loi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 		return PTR_ERR(loi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	if (version == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 		ignore_offset = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 		size = min_t(u32, loi->size, SZ_64K);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 		/* All reserved bits must be 0 for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 		if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 			goto out_loi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 		/* Only accept flags we have defined so far */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 		if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 			goto out_loi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 		ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 		size = min_t(u32, loi->size, SZ_16M);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 	path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 	inodes = init_data_container(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	if (IS_ERR(inodes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 		ret = PTR_ERR(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 		inodes = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 	ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 					  build_ino_list, inodes, ignore_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	if (ret == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 	ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 			   size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 	btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 	kvfree(inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) out_loi:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 	kfree(loi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 			       struct btrfs_ioctl_balance_args *bargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 	bargs->flags = bctl->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 		bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 	if (atomic_read(&fs_info->balance_pause_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 		bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	if (atomic_read(&fs_info->balance_cancel_req))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 		bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	spin_lock(&fs_info->balance_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 	memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 	spin_unlock(&fs_info->balance_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) static long btrfs_ioctl_balance(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 	struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 	struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	struct btrfs_ioctl_balance_args *bargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 	struct btrfs_balance_control *bctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 	bool need_unlock; /* for mut. excl. ops lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 		mutex_lock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 		need_unlock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 		goto locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 	 * mut. excl. ops lock is locked.  Three possibilities:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 	 *   (1) some other op is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 	 *   (2) balance is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 	 *   (3) balance is paused -- special case (think resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	mutex_lock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 	if (fs_info->balance_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 		/* this is either (2) or (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 		if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 			mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 			 * Lock released to allow other waiters to continue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 			 * we'll reexamine the status again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 			mutex_lock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 			if (fs_info->balance_ctl &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 			    !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 				/* this is (3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 				need_unlock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 				goto locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 			mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 			goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 			/* this is (2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 			mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 			ret = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) 		/* this is (1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 		mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 		ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) locked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 	if (arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 		bargs = memdup_user(arg, sizeof(*bargs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 		if (IS_ERR(bargs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 			ret = PTR_ERR(bargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 		if (bargs->flags & BTRFS_BALANCE_RESUME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 			if (!fs_info->balance_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 				ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 				goto out_bargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 			bctl = fs_info->balance_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 			spin_lock(&fs_info->balance_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 			bctl->flags |= BTRFS_BALANCE_RESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 			spin_unlock(&fs_info->balance_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 			goto do_balance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 		bargs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 	if (fs_info->balance_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 		ret = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 		goto out_bargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 	bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	if (!bctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 		goto out_bargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 	if (arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 		memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 		memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 		memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 		bctl->flags = bargs->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 		/* balance everything - no filters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 		bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 	if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 		goto out_bctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) do_balance:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 	 * Ownership of bctl and exclusive operation goes to btrfs_balance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	 * bctl is freed in reset_balance_state, or, if restriper was paused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 	 * all the way until unmount, in free_fs_info.  The flag should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	 * cleared after reset_balance_state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 	need_unlock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 	ret = btrfs_balance(fs_info, bctl, bargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	bctl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	if ((ret == 0 || ret == -ECANCELED) && arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 		if (copy_to_user(arg, bargs, sizeof(*bargs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) out_bctl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 	kfree(bctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) out_bargs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	kfree(bargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 	if (need_unlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 		btrfs_exclop_finish(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	case BTRFS_BALANCE_CTL_PAUSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 		return btrfs_pause_balance(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	case BTRFS_BALANCE_CTL_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 		return btrfs_cancel_balance(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 					 void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 	struct btrfs_ioctl_balance_args *bargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 	mutex_lock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	if (!fs_info->balance_ctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		ret = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 	if (!bargs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 	btrfs_update_ioctl_balance_args(fs_info, bargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	if (copy_to_user(arg, bargs, sizeof(*bargs)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	kfree(bargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	mutex_unlock(&fs_info->balance_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	struct btrfs_ioctl_quota_ctl_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 	if (IS_ERR(sa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 		ret = PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 		goto drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 	down_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 	switch (sa->cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 	case BTRFS_QUOTA_CTL_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 		ret = btrfs_quota_enable(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	case BTRFS_QUOTA_CTL_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 		ret = btrfs_quota_disable(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 	up_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 	struct btrfs_ioctl_qgroup_assign_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 	if (IS_ERR(sa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 		ret = PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 		goto drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	if (sa->assign) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 		ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 		ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	/* update qgroup status and info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	err = btrfs_run_qgroups(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 		btrfs_handle_fs_error(fs_info, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 				      "failed to update qgroup status and info");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 	err = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	if (err && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	struct btrfs_ioctl_qgroup_create_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 	if (IS_ERR(sa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 		ret = PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 		goto drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 	if (!sa->qgroupid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 	if (sa->create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 		ret = btrfs_create_qgroup(trans, sa->qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 		ret = btrfs_remove_qgroup(trans, sa->qgroupid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 	err = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 	if (err && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 	struct btrfs_ioctl_qgroup_limit_args *sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 	u64 qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 	if (IS_ERR(sa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 		ret = PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 		goto drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 	trans = btrfs_join_transaction(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 	qgroupid = sa->qgroupid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 	if (!qgroupid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 		/* take the current subvol as qgroup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 		qgroupid = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 	ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 	err = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 	if (err && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 	struct btrfs_ioctl_quota_rescan_args *qsa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 	qsa = memdup_user(arg, sizeof(*qsa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 	if (IS_ERR(qsa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 		ret = PTR_ERR(qsa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 		goto drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 	if (qsa->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 	ret = btrfs_qgroup_rescan(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 	kfree(qsa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 						void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 	struct btrfs_ioctl_quota_rescan_args *qsa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 	qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 	if (!qsa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 	if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 		qsa->flags = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 		qsa->progress = fs_info->qgroup_rescan_progress.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 	if (copy_to_user(arg, qsa, sizeof(*qsa)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 	kfree(qsa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 						void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 	return btrfs_qgroup_wait_for_completion(fs_info, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) static long _btrfs_ioctl_set_received_subvol(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 					    struct btrfs_ioctl_received_subvol_args *sa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 	struct btrfs_root_item *root_item = &root->root_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 	struct timespec64 ct = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 	int received_uuid_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 	if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 	down_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 	if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 	if (btrfs_root_readonly(root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 		ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 	 * 1 - root item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 	 * 2 - uuid items (received uuid + subvol uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 	trans = btrfs_start_transaction(root, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 		trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 	sa->rtransid = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 	sa->rtime.sec = ct.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 	sa->rtime.nsec = ct.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 	received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 				       BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 	if (received_uuid_changed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 	    !btrfs_is_empty_uuid(root_item->received_uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 		ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 					  root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 		if (ret && ret != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 		        btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 		        btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 		        goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 	memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 	btrfs_set_root_stransid(root_item, sa->stransid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 	btrfs_set_root_rtransid(root_item, sa->rtransid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 	btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 	btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 	btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 	btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 	ret = btrfs_update_root(trans, fs_info->tree_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 				&root->root_key, &root->root_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 		btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 		ret = btrfs_uuid_tree_add(trans, sa->uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 					  BTRFS_UUID_KEY_RECEIVED_SUBVOL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 					  root->root_key.objectid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 		if (ret < 0 && ret != -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 			btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 			btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 	up_write(&fs_info->subvol_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) static long btrfs_ioctl_set_received_subvol_32(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 						void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 	struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 	struct btrfs_ioctl_received_subvol_args *args64 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 	args32 = memdup_user(arg, sizeof(*args32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 	if (IS_ERR(args32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 		return PTR_ERR(args32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 	args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 	if (!args64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) 	memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 	args64->stransid = args32->stransid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 	args64->rtransid = args32->rtransid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 	args64->stime.sec = args32->stime.sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 	args64->stime.nsec = args32->stime.nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 	args64->rtime.sec = args32->rtime.sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 	args64->rtime.nsec = args32->rtime.nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 	args64->flags = args32->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) 	ret = _btrfs_ioctl_set_received_subvol(file, args64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 	memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) 	args32->stransid = args64->stransid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 	args32->rtransid = args64->rtransid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 	args32->stime.sec = args64->stime.sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 	args32->stime.nsec = args64->stime.nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) 	args32->rtime.sec = args64->rtime.sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 	args32->rtime.nsec = args64->rtime.nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 	args32->flags = args64->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	ret = copy_to_user(arg, args32, sizeof(*args32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 	kfree(args32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	kfree(args64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) static long btrfs_ioctl_set_received_subvol(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 					    void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 	struct btrfs_ioctl_received_subvol_args *sa = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 	sa = memdup_user(arg, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 	if (IS_ERR(sa))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 		return PTR_ERR(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 	ret = _btrfs_ioctl_set_received_subvol(file, sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) 	ret = copy_to_user(arg, sa, sizeof(*sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 	kfree(sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 					void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 	char label[BTRFS_LABEL_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 	spin_lock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 	memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	spin_unlock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 	len = strnlen(label, BTRFS_LABEL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 	if (len == BTRFS_LABEL_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 		btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 			   "label is too long, return the first %zu bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 			   --len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 	ret = copy_to_user(arg, label, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 	return ret ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) 	struct btrfs_super_block *super_block = fs_info->super_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 	char label[BTRFS_LABEL_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) 	if (copy_from_user(label, arg, sizeof(label)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 	if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 		btrfs_err(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 			  "unable to set label with more than %d bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 			  BTRFS_LABEL_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 	spin_lock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 	strcpy(super_block->label, label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 	spin_unlock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) #define INIT_FEATURE_FLAGS(suffix) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 	{ .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 	  .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 	  .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) int btrfs_ioctl_get_supported_features(void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 	static const struct btrfs_ioctl_feature_flags features[3] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 		INIT_FEATURE_FLAGS(SUPP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 		INIT_FEATURE_FLAGS(SAFE_SET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) 		INIT_FEATURE_FLAGS(SAFE_CLEAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 	if (copy_to_user(arg, &features, sizeof(features)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) 					void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) 	struct btrfs_super_block *super_block = fs_info->super_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) 	struct btrfs_ioctl_feature_flags features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) 	features.compat_flags = btrfs_super_compat_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) 	features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) 	features.incompat_flags = btrfs_super_incompat_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) 	if (copy_to_user(arg, &features, sizeof(features)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) static int check_feature_bits(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 			      enum btrfs_feature_set set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) 			      u64 change_mask, u64 flags, u64 supported_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 			      u64 safe_set, u64 safe_clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 	const char *type = btrfs_feature_set_name(set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 	char *names;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 	u64 disallowed, unsupported;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 	u64 set_mask = flags & change_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) 	u64 clear_mask = ~flags & change_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) 	unsupported = set_mask & ~supported_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 	if (unsupported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 		names = btrfs_printable_features(set, unsupported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 		if (names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 				   "this kernel does not support the %s feature bit%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 				   names, strchr(names, ',') ? "s" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 			kfree(names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 				   "this kernel does not support %s bits 0x%llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 				   type, unsupported);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 	disallowed = set_mask & ~safe_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) 	if (disallowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) 		names = btrfs_printable_features(set, disallowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 		if (names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 				   "can't set the %s feature bit%s while mounted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) 				   names, strchr(names, ',') ? "s" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) 			kfree(names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) 				   "can't set %s bits 0x%llx while mounted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 				   type, disallowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) 	disallowed = clear_mask & ~safe_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) 	if (disallowed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 		names = btrfs_printable_features(set, disallowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 		if (names) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 				   "can't clear the %s feature bit%s while mounted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 				   names, strchr(names, ',') ? "s" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 			kfree(names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 			btrfs_warn(fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 				   "can't clear %s bits 0x%llx while mounted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 				   type, disallowed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) #define check_feature(fs_info, change_mask, flags, mask_base)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 		   BTRFS_FEATURE_ ## mask_base ## _SUPP,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_SET,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 		   BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 	struct btrfs_super_block *super_block = fs_info->super_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) 	struct btrfs_ioctl_feature_flags flags[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) 	struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) 	u64 newflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) 	if (copy_from_user(flags, arg, sizeof(flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 	/* Nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) 	if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 	    !flags[0].incompat_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 	ret = check_feature(fs_info, flags[0].compat_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 			    flags[1].compat_flags, COMPAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) 	ret = check_feature(fs_info, flags[0].compat_ro_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) 			    flags[1].compat_ro_flags, COMPAT_RO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 	ret = check_feature(fs_info, flags[0].incompat_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) 			    flags[1].incompat_flags, INCOMPAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) 	ret = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) 	trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) 	if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) 		ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) 		goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 	spin_lock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) 	newflags = btrfs_super_compat_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	newflags |= flags[0].compat_flags & flags[1].compat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 	btrfs_set_super_compat_flags(super_block, newflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 	newflags = btrfs_super_compat_ro_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 	newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 	newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 	btrfs_set_super_compat_ro_flags(super_block, newflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 	newflags = btrfs_super_incompat_flags(super_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 	newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 	btrfs_set_super_incompat_flags(super_block, newflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 	spin_unlock(&fs_info->super_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 	ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 	struct btrfs_ioctl_send_args *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 	if (compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 		struct btrfs_ioctl_send_args_32 args32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 		ret = copy_from_user(&args32, argp, sizeof(args32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 		arg = kzalloc(sizeof(*arg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 		if (!arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 		arg->send_fd = args32.send_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 		arg->clone_sources_count = args32.clone_sources_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 		arg->clone_sources = compat_ptr(args32.clone_sources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 		arg->parent_root = args32.parent_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 		arg->flags = args32.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 		memcpy(arg->reserved, args32.reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 		       sizeof(args32.reserved));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 		arg = memdup_user(argp, sizeof(*arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 		if (IS_ERR(arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 			return PTR_ERR(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 	ret = btrfs_ioctl_send(file, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 	kfree(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) long btrfs_ioctl(struct file *file, unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 		cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 	struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 	case FS_IOC_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 		return btrfs_ioctl_getflags(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 	case FS_IOC_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 		return btrfs_ioctl_setflags(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 	case FS_IOC_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) 		return btrfs_ioctl_getversion(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 	case FS_IOC_GETFSLABEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) 		return btrfs_ioctl_get_fslabel(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 	case FS_IOC_SETFSLABEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) 		return btrfs_ioctl_set_fslabel(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) 	case FITRIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) 		return btrfs_ioctl_fitrim(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) 	case BTRFS_IOC_SNAP_CREATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 		return btrfs_ioctl_snap_create(file, argp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	case BTRFS_IOC_SNAP_CREATE_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 		return btrfs_ioctl_snap_create_v2(file, argp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 	case BTRFS_IOC_SUBVOL_CREATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 		return btrfs_ioctl_snap_create(file, argp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 	case BTRFS_IOC_SUBVOL_CREATE_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 		return btrfs_ioctl_snap_create_v2(file, argp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 	case BTRFS_IOC_SNAP_DESTROY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 		return btrfs_ioctl_snap_destroy(file, argp, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) 	case BTRFS_IOC_SNAP_DESTROY_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 		return btrfs_ioctl_snap_destroy(file, argp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) 	case BTRFS_IOC_SUBVOL_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) 		return btrfs_ioctl_subvol_getflags(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) 	case BTRFS_IOC_SUBVOL_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) 		return btrfs_ioctl_subvol_setflags(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) 	case BTRFS_IOC_DEFAULT_SUBVOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) 		return btrfs_ioctl_default_subvol(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) 	case BTRFS_IOC_DEFRAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) 		return btrfs_ioctl_defrag(file, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) 	case BTRFS_IOC_DEFRAG_RANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 		return btrfs_ioctl_defrag(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 	case BTRFS_IOC_RESIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 		return btrfs_ioctl_resize(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 	case BTRFS_IOC_ADD_DEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 		return btrfs_ioctl_add_dev(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 	case BTRFS_IOC_RM_DEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 		return btrfs_ioctl_rm_dev(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 	case BTRFS_IOC_RM_DEV_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 		return btrfs_ioctl_rm_dev_v2(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 	case BTRFS_IOC_FS_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) 		return btrfs_ioctl_fs_info(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 	case BTRFS_IOC_DEV_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) 		return btrfs_ioctl_dev_info(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) 	case BTRFS_IOC_BALANCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 		return btrfs_ioctl_balance(file, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 	case BTRFS_IOC_TREE_SEARCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 		return btrfs_ioctl_tree_search(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 	case BTRFS_IOC_TREE_SEARCH_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) 		return btrfs_ioctl_tree_search_v2(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) 	case BTRFS_IOC_INO_LOOKUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 		return btrfs_ioctl_ino_lookup(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) 	case BTRFS_IOC_INO_PATHS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) 		return btrfs_ioctl_ino_to_path(root, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) 	case BTRFS_IOC_LOGICAL_INO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) 		return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) 	case BTRFS_IOC_LOGICAL_INO_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) 		return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) 	case BTRFS_IOC_SPACE_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 		return btrfs_ioctl_space_info(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) 	case BTRFS_IOC_SYNC: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) 		ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 		ret = btrfs_sync_fs(inode->i_sb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) 		 * The transaction thread may want to do more work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) 		 * namely it pokes the cleaner kthread that will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) 		 * processing uncleaned subvols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) 		wake_up_process(fs_info->transaction_kthread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) 	case BTRFS_IOC_START_SYNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) 		return btrfs_ioctl_start_sync(root, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 	case BTRFS_IOC_WAIT_SYNC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) 		return btrfs_ioctl_wait_sync(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) 	case BTRFS_IOC_SCRUB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 		return btrfs_ioctl_scrub(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) 	case BTRFS_IOC_SCRUB_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 		return btrfs_ioctl_scrub_cancel(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) 	case BTRFS_IOC_SCRUB_PROGRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) 		return btrfs_ioctl_scrub_progress(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) 	case BTRFS_IOC_BALANCE_V2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 		return btrfs_ioctl_balance(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) 	case BTRFS_IOC_BALANCE_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) 		return btrfs_ioctl_balance_ctl(fs_info, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) 	case BTRFS_IOC_BALANCE_PROGRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) 		return btrfs_ioctl_balance_progress(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) 	case BTRFS_IOC_SET_RECEIVED_SUBVOL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) 		return btrfs_ioctl_set_received_subvol(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 	case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 		return btrfs_ioctl_set_received_subvol_32(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 	case BTRFS_IOC_SEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 		return _btrfs_ioctl_send(file, argp, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 	case BTRFS_IOC_SEND_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) 		return _btrfs_ioctl_send(file, argp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) 	case BTRFS_IOC_GET_DEV_STATS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 		return btrfs_ioctl_get_dev_stats(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) 	case BTRFS_IOC_QUOTA_CTL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 		return btrfs_ioctl_quota_ctl(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 	case BTRFS_IOC_QGROUP_ASSIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 		return btrfs_ioctl_qgroup_assign(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 	case BTRFS_IOC_QGROUP_CREATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 		return btrfs_ioctl_qgroup_create(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) 	case BTRFS_IOC_QGROUP_LIMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) 		return btrfs_ioctl_qgroup_limit(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) 	case BTRFS_IOC_QUOTA_RESCAN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) 		return btrfs_ioctl_quota_rescan(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 	case BTRFS_IOC_QUOTA_RESCAN_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 		return btrfs_ioctl_quota_rescan_status(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 	case BTRFS_IOC_QUOTA_RESCAN_WAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) 		return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) 	case BTRFS_IOC_DEV_REPLACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) 		return btrfs_ioctl_dev_replace(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) 		return btrfs_ioctl_get_supported_features(argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) 	case BTRFS_IOC_GET_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) 		return btrfs_ioctl_get_features(fs_info, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) 	case BTRFS_IOC_SET_FEATURES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) 		return btrfs_ioctl_set_features(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) 	case FS_IOC_FSGETXATTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) 		return btrfs_ioctl_fsgetxattr(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 	case FS_IOC_FSSETXATTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) 		return btrfs_ioctl_fssetxattr(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 	case BTRFS_IOC_GET_SUBVOL_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 		return btrfs_ioctl_get_subvol_info(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 	case BTRFS_IOC_GET_SUBVOL_ROOTREF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 		return btrfs_ioctl_get_subvol_rootref(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 	case BTRFS_IOC_INO_LOOKUP_USER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 		return btrfs_ioctl_ino_lookup_user(file, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) 	return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 	 * These all access 32-bit values anyway so no further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 	 * handling is necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 	case FS_IOC32_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) 		cmd = FS_IOC_GETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) 	case FS_IOC32_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) 		cmd = FS_IOC_SETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) 	case FS_IOC32_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) 		cmd = FS_IOC_GETVERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 	return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) #endif