David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/bio.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 8 | #include <linux/file.h> |
| 9 | #include <linux/fs.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 10 | #include <linux/fsnotify.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/highmem.h> |
| 13 | #include <linux/time.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 14 | #include <linux/string.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 15 | #include <linux/backing-dev.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 16 | #include <linux/mount.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 17 | #include <linux/namei.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 18 | #include <linux/writeback.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 19 | #include <linux/compat.h> |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 20 | #include <linux/security.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 21 | #include <linux/xattr.h> |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 22 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 24 | #include <linux/blkdev.h> |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 25 | #include <linux/uuid.h> |
Filipe Brandenburger | 55e301f | 2013-01-29 06:04:50 +0000 | [diff] [blame] | 26 | #include <linux/btrfs.h> |
Mark Fasheh | 416161d | 2013-08-06 11:42:51 -0700 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Jeff Layton | ae5e165 | 2018-01-29 06:41:30 -0500 | [diff] [blame] | 28 | #include <linux/iversion.h> |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 29 | #include <linux/fileattr.h> |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 30 | #include <linux/fsverity.h> |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 31 | #include "ctree.h" |
| 32 | #include "disk-io.h" |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 33 | #include "export.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 34 | #include "transaction.h" |
| 35 | #include "btrfs_inode.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 36 | #include "print-tree.h" |
| 37 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 38 | #include "locking.h" |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 39 | #include "backref.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 40 | #include "rcu-string.h" |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 41 | #include "send.h" |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 42 | #include "dev-replace.h" |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 43 | #include "props.h" |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 44 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 45 | #include "qgroup.h" |
Filipe Manana | 1ec9a1a | 2016-02-10 10:42:25 +0000 | [diff] [blame] | 46 | #include "tree-log.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 47 | #include "compression.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 48 | #include "space-info.h" |
Josef Bacik | 8673634 | 2019-06-19 15:12:00 -0400 | [diff] [blame] | 49 | #include "delalloc-space.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 50 | #include "block-group.h" |
Qu Wenruo | 22b398e | 2021-08-06 16:12:37 +0800 | [diff] [blame] | 51 | #include "subpage.h" |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 52 | |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 53 | #ifdef CONFIG_64BIT |
| 54 | /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI |
| 55 | * structures are incorrect, as the timespec structure from userspace |
| 56 | * is 4 bytes too small. We define these alternatives here to teach |
| 57 | * the kernel about the 32-bit struct packing. |
| 58 | */ |
| 59 | struct btrfs_ioctl_timespec_32 { |
| 60 | __u64 sec; |
| 61 | __u32 nsec; |
| 62 | } __attribute__ ((__packed__)); |
| 63 | |
| 64 | struct btrfs_ioctl_received_subvol_args_32 { |
| 65 | char uuid[BTRFS_UUID_SIZE]; /* in */ |
| 66 | __u64 stransid; /* in */ |
| 67 | __u64 rtransid; /* out */ |
| 68 | struct btrfs_ioctl_timespec_32 stime; /* in */ |
| 69 | struct btrfs_ioctl_timespec_32 rtime; /* out */ |
| 70 | __u64 flags; /* in */ |
| 71 | __u64 reserved[16]; /* in */ |
| 72 | } __attribute__ ((__packed__)); |
| 73 | |
| 74 | #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ |
| 75 | struct btrfs_ioctl_received_subvol_args_32) |
| 76 | #endif |
| 77 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 78 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 79 | struct btrfs_ioctl_send_args_32 { |
| 80 | __s64 send_fd; /* in */ |
| 81 | __u64 clone_sources_count; /* in */ |
| 82 | compat_uptr_t clone_sources; /* in */ |
| 83 | __u64 parent_root; /* in */ |
| 84 | __u64 flags; /* in */ |
David Sterba | e77fbf9 | 2021-10-22 16:53:36 +0200 | [diff] [blame] | 85 | __u32 version; /* in */ |
| 86 | __u8 reserved[28]; /* in */ |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 87 | } __attribute__ ((__packed__)); |
| 88 | |
| 89 | #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ |
| 90 | struct btrfs_ioctl_send_args_32) |
| 91 | #endif |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 92 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 93 | /* Mask out flags that are inappropriate for the given type of inode. */ |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 94 | static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, |
| 95 | unsigned int flags) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 96 | { |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 97 | if (S_ISDIR(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 98 | return flags; |
David Sterba | 1905a0f | 2018-03-26 18:52:15 +0200 | [diff] [blame] | 99 | else if (S_ISREG(inode->i_mode)) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 100 | return flags & ~FS_DIRSYNC_FL; |
| 101 | else |
| 102 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
| 103 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 104 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 105 | /* |
David Sterba | a157d4f | 2018-03-26 19:12:25 +0200 | [diff] [blame] | 106 | * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS |
| 107 | * ioctl. |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 108 | */ |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 109 | static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 110 | { |
| 111 | unsigned int iflags = 0; |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 112 | u32 flags = binode->flags; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 113 | u32 ro_flags = binode->ro_flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 114 | |
| 115 | if (flags & BTRFS_INODE_SYNC) |
| 116 | iflags |= FS_SYNC_FL; |
| 117 | if (flags & BTRFS_INODE_IMMUTABLE) |
| 118 | iflags |= FS_IMMUTABLE_FL; |
| 119 | if (flags & BTRFS_INODE_APPEND) |
| 120 | iflags |= FS_APPEND_FL; |
| 121 | if (flags & BTRFS_INODE_NODUMP) |
| 122 | iflags |= FS_NODUMP_FL; |
| 123 | if (flags & BTRFS_INODE_NOATIME) |
| 124 | iflags |= FS_NOATIME_FL; |
| 125 | if (flags & BTRFS_INODE_DIRSYNC) |
| 126 | iflags |= FS_DIRSYNC_FL; |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 127 | if (flags & BTRFS_INODE_NODATACOW) |
| 128 | iflags |= FS_NOCOW_FL; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 129 | if (ro_flags & BTRFS_INODE_RO_VERITY) |
| 130 | iflags |= FS_VERITY_FL; |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 131 | |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 132 | if (flags & BTRFS_INODE_NOCOMPRESS) |
Li Zefan | d0092bd | 2011-04-15 03:03:06 +0000 | [diff] [blame] | 133 | iflags |= FS_NOCOMP_FL; |
Satoru Takeuchi | 13f48dc | 2016-03-15 09:09:59 +0900 | [diff] [blame] | 134 | else if (flags & BTRFS_INODE_COMPRESS) |
| 135 | iflags |= FS_COMPR_FL; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 136 | |
| 137 | return iflags; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Update inode->i_flags based on the btrfs internal flags. |
| 142 | */ |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 143 | void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 144 | { |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 145 | struct btrfs_inode *binode = BTRFS_I(inode); |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 146 | unsigned int new_fl = 0; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 147 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 148 | if (binode->flags & BTRFS_INODE_SYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 149 | new_fl |= S_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 150 | if (binode->flags & BTRFS_INODE_IMMUTABLE) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 151 | new_fl |= S_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 152 | if (binode->flags & BTRFS_INODE_APPEND) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 153 | new_fl |= S_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 154 | if (binode->flags & BTRFS_INODE_NOATIME) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 155 | new_fl |= S_NOATIME; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 156 | if (binode->flags & BTRFS_INODE_DIRSYNC) |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 157 | new_fl |= S_DIRSYNC; |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 158 | if (binode->ro_flags & BTRFS_INODE_RO_VERITY) |
| 159 | new_fl |= S_VERITY; |
Filipe Manana | 3cc7939 | 2014-06-25 22:36:02 +0100 | [diff] [blame] | 160 | |
| 161 | set_mask_bits(&inode->i_flags, |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 162 | S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC | |
| 163 | S_VERITY, new_fl); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 164 | } |
| 165 | |
David Sterba | f37c563 | 2020-07-10 09:49:56 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Check if @flags are a supported and valid set of FS_*_FL flags and that |
| 168 | * the old and new flags are not conflicting |
| 169 | */ |
| 170 | static int check_fsflags(unsigned int old_flags, unsigned int flags) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 171 | { |
| 172 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
| 173 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
| 174 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
Li Zefan | e1e8fb6 | 2011-04-15 03:02:49 +0000 | [diff] [blame] | 175 | FS_NOCOMP_FL | FS_COMPR_FL | |
| 176 | FS_NOCOW_FL)) |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 177 | return -EOPNOTSUPP; |
| 178 | |
David Sterba | f37c563 | 2020-07-10 09:49:56 +0200 | [diff] [blame] | 179 | /* COMPR and NOCOMP on new/old are valid */ |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 180 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
| 181 | return -EINVAL; |
| 182 | |
David Sterba | f37c563 | 2020-07-10 09:49:56 +0200 | [diff] [blame] | 183 | if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL)) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | /* NOCOW and compression options are mutually exclusive */ |
| 187 | if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL))) |
| 188 | return -EINVAL; |
| 189 | if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL))) |
| 190 | return -EINVAL; |
| 191 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Naohiro Aota | d206e9c | 2020-11-10 20:26:11 +0900 | [diff] [blame] | 195 | static int check_fsflags_compatible(struct btrfs_fs_info *fs_info, |
| 196 | unsigned int flags) |
| 197 | { |
| 198 | if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL)) |
| 199 | return -EPERM; |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 204 | /* |
| 205 | * Set flags/xflags from the internal inode flags. The remaining items of |
| 206 | * fsxattr are zeroed. |
| 207 | */ |
| 208 | int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 209 | { |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 210 | struct btrfs_inode *binode = BTRFS_I(d_inode(dentry)); |
| 211 | |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 212 | fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode)); |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int btrfs_fileattr_set(struct user_namespace *mnt_userns, |
| 217 | struct dentry *dentry, struct fileattr *fa) |
| 218 | { |
| 219 | struct inode *inode = d_inode(dentry); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 220 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 221 | struct btrfs_inode *binode = BTRFS_I(inode); |
| 222 | struct btrfs_root *root = binode->root; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 223 | struct btrfs_trans_handle *trans; |
Darrick J. Wong | 5aca284 | 2019-07-01 08:25:34 -0700 | [diff] [blame] | 224 | unsigned int fsflags, old_fsflags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 225 | int ret; |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 226 | const char *comp = NULL; |
David Sterba | f37c563 | 2020-07-10 09:49:56 +0200 | [diff] [blame] | 227 | u32 binode_flags; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 228 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 229 | if (btrfs_root_readonly(root)) |
| 230 | return -EROFS; |
| 231 | |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 232 | if (fileattr_has_fsx(fa)) |
| 233 | return -EOPNOTSUPP; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 234 | |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 235 | fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags); |
Boris Burkov | 77eea05 | 2021-06-30 13:01:48 -0700 | [diff] [blame] | 236 | old_fsflags = btrfs_inode_flags_to_fsflags(binode); |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 237 | ret = check_fsflags(old_fsflags, fsflags); |
Jan Kara | e784868 | 2012-06-12 16:20:32 +0200 | [diff] [blame] | 238 | if (ret) |
| 239 | return ret; |
| 240 | |
Naohiro Aota | d206e9c | 2020-11-10 20:26:11 +0900 | [diff] [blame] | 241 | ret = check_fsflags_compatible(fs_info, fsflags); |
| 242 | if (ret) |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 243 | return ret; |
Naohiro Aota | d206e9c | 2020-11-10 20:26:11 +0900 | [diff] [blame] | 244 | |
David Sterba | f37c563 | 2020-07-10 09:49:56 +0200 | [diff] [blame] | 245 | binode_flags = binode->flags; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 246 | if (fsflags & FS_SYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 247 | binode_flags |= BTRFS_INODE_SYNC; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 248 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 249 | binode_flags &= ~BTRFS_INODE_SYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 250 | if (fsflags & FS_IMMUTABLE_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 251 | binode_flags |= BTRFS_INODE_IMMUTABLE; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 252 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 253 | binode_flags &= ~BTRFS_INODE_IMMUTABLE; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 254 | if (fsflags & FS_APPEND_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 255 | binode_flags |= BTRFS_INODE_APPEND; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 256 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 257 | binode_flags &= ~BTRFS_INODE_APPEND; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 258 | if (fsflags & FS_NODUMP_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 259 | binode_flags |= BTRFS_INODE_NODUMP; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 260 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 261 | binode_flags &= ~BTRFS_INODE_NODUMP; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 262 | if (fsflags & FS_NOATIME_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 263 | binode_flags |= BTRFS_INODE_NOATIME; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 264 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 265 | binode_flags &= ~BTRFS_INODE_NOATIME; |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 266 | |
| 267 | /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */ |
| 268 | if (!fa->flags_valid) { |
| 269 | /* 1 item for the inode */ |
| 270 | trans = btrfs_start_transaction(root, 1); |
Ritesh Harjani | 9b8a233 | 2021-04-30 21:30:55 +0530 | [diff] [blame] | 271 | if (IS_ERR(trans)) |
| 272 | return PTR_ERR(trans); |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 273 | goto update_flags; |
| 274 | } |
| 275 | |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 276 | if (fsflags & FS_DIRSYNC_FL) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 277 | binode_flags |= BTRFS_INODE_DIRSYNC; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 278 | else |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 279 | binode_flags &= ~BTRFS_INODE_DIRSYNC; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 280 | if (fsflags & FS_NOCOW_FL) { |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 281 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 282 | /* |
| 283 | * It's safe to turn csums off here, no extents exist. |
| 284 | * Otherwise we want the flag to reflect the real COW |
| 285 | * status of the file and will not set it. |
| 286 | */ |
| 287 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 288 | binode_flags |= BTRFS_INODE_NODATACOW | |
| 289 | BTRFS_INODE_NODATASUM; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 290 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 291 | binode_flags |= BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 292 | } |
| 293 | } else { |
| 294 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 295 | * Revert back under same assumptions as above |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 296 | */ |
Anand Jain | 44e5194 | 2019-04-20 19:48:57 +0800 | [diff] [blame] | 297 | if (S_ISREG(inode->i_mode)) { |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 298 | if (inode->i_size == 0) |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 299 | binode_flags &= ~(BTRFS_INODE_NODATACOW | |
| 300 | BTRFS_INODE_NODATASUM); |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 301 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 302 | binode_flags &= ~BTRFS_INODE_NODATACOW; |
David Sterba | 7e97b8d | 2012-09-07 05:56:55 -0600 | [diff] [blame] | 303 | } |
| 304 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 305 | |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 306 | /* |
| 307 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
| 308 | * flag may be changed automatically if compression code won't make |
| 309 | * things smaller. |
| 310 | */ |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 311 | if (fsflags & FS_NOCOMP_FL) { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 312 | binode_flags &= ~BTRFS_INODE_COMPRESS; |
| 313 | binode_flags |= BTRFS_INODE_NOCOMPRESS; |
David Sterba | 5c57b8b | 2018-04-23 15:45:18 +0200 | [diff] [blame] | 314 | } else if (fsflags & FS_COMPR_FL) { |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 315 | |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 316 | if (IS_SWAPFILE(inode)) |
| 317 | return -ETXTBSY; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 318 | |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 319 | binode_flags |= BTRFS_INODE_COMPRESS; |
| 320 | binode_flags &= ~BTRFS_INODE_NOCOMPRESS; |
Filipe David Borba Manana | 6354192 | 2014-01-07 11:47:46 +0000 | [diff] [blame] | 321 | |
David Sterba | 9337050 | 2017-10-31 17:32:41 +0100 | [diff] [blame] | 322 | comp = btrfs_compress_type2str(fs_info->compress_type); |
| 323 | if (!comp || comp[0] == 0) |
| 324 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); |
Li Zefan | ebcb904 | 2011-04-15 03:03:17 +0000 | [diff] [blame] | 325 | } else { |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 326 | binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
Liu Bo | 75e7cb7 | 2011-03-22 10:12:20 +0000 | [diff] [blame] | 327 | } |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 328 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 329 | /* |
| 330 | * 1 for inode item |
| 331 | * 2 for properties |
| 332 | */ |
| 333 | trans = btrfs_start_transaction(root, 3); |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 334 | if (IS_ERR(trans)) |
| 335 | return PTR_ERR(trans); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 336 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 337 | if (comp) { |
| 338 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp, |
| 339 | strlen(comp), 0); |
| 340 | if (ret) { |
| 341 | btrfs_abort_transaction(trans, ret); |
| 342 | goto out_end_trans; |
| 343 | } |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 344 | } else { |
| 345 | ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, |
| 346 | 0, 0); |
| 347 | if (ret && ret != -ENODATA) { |
| 348 | btrfs_abort_transaction(trans, ret); |
| 349 | goto out_end_trans; |
| 350 | } |
| 351 | } |
| 352 | |
Miklos Szeredi | 97fc297 | 2021-04-07 14:36:43 +0200 | [diff] [blame] | 353 | update_flags: |
Anand Jain | d2b8fcf | 2019-04-20 19:48:55 +0800 | [diff] [blame] | 354 | binode->flags = binode_flags; |
David Sterba | 7b6a221 | 2018-03-26 18:40:21 +0200 | [diff] [blame] | 355 | btrfs_sync_inode_flags_to_i_flags(inode); |
Josef Bacik | 0c4d2d9 | 2012-04-05 15:03:02 -0400 | [diff] [blame] | 356 | inode_inc_iversion(inode); |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 357 | inode->i_ctime = current_time(inode); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 358 | ret = btrfs_update_inode(trans, root, BTRFS_I(inode)); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 359 | |
Anand Jain | ff9fef55 | 2019-04-20 19:48:53 +0800 | [diff] [blame] | 360 | out_end_trans: |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 361 | btrfs_end_transaction(trans); |
liubo | 2d4e6f6ad | 2011-02-24 09:38:16 +0000 | [diff] [blame] | 362 | return ret; |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 363 | } |
| 364 | |
David Sterba | 0d7ed32 | 2021-05-14 17:42:30 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Start exclusive operation @type, return true on success |
| 367 | */ |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 368 | bool btrfs_exclop_start(struct btrfs_fs_info *fs_info, |
| 369 | enum btrfs_exclusive_operation type) |
| 370 | { |
David Sterba | 0d7ed32 | 2021-05-14 17:42:30 +0200 | [diff] [blame] | 371 | bool ret = false; |
| 372 | |
| 373 | spin_lock(&fs_info->super_lock); |
| 374 | if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) { |
| 375 | fs_info->exclusive_operation = type; |
| 376 | ret = true; |
| 377 | } |
| 378 | spin_unlock(&fs_info->super_lock); |
| 379 | |
| 380 | return ret; |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 381 | } |
| 382 | |
David Sterba | 578bda9 | 2021-05-18 21:05:52 +0200 | [diff] [blame] | 383 | /* |
| 384 | * Conditionally allow to enter the exclusive operation in case it's compatible |
| 385 | * with the running one. This must be paired with btrfs_exclop_start_unlock and |
| 386 | * btrfs_exclop_finish. |
| 387 | * |
| 388 | * Compatibility: |
| 389 | * - the same type is already running |
Nikolay Borisov | 621a1ee | 2021-11-25 11:14:42 +0200 | [diff] [blame] | 390 | * - when trying to add a device and balance has been paused |
David Sterba | 578bda9 | 2021-05-18 21:05:52 +0200 | [diff] [blame] | 391 | * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller |
| 392 | * must check the condition first that would allow none -> @type |
| 393 | */ |
| 394 | bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info, |
| 395 | enum btrfs_exclusive_operation type) |
| 396 | { |
| 397 | spin_lock(&fs_info->super_lock); |
Nikolay Borisov | 621a1ee | 2021-11-25 11:14:42 +0200 | [diff] [blame] | 398 | if (fs_info->exclusive_operation == type || |
| 399 | (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED && |
| 400 | type == BTRFS_EXCLOP_DEV_ADD)) |
David Sterba | 578bda9 | 2021-05-18 21:05:52 +0200 | [diff] [blame] | 401 | return true; |
| 402 | |
| 403 | spin_unlock(&fs_info->super_lock); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info) |
| 408 | { |
| 409 | spin_unlock(&fs_info->super_lock); |
| 410 | } |
| 411 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 412 | void btrfs_exclop_finish(struct btrfs_fs_info *fs_info) |
| 413 | { |
David Sterba | 0d7ed32 | 2021-05-14 17:42:30 +0200 | [diff] [blame] | 414 | spin_lock(&fs_info->super_lock); |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 415 | WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE); |
David Sterba | 0d7ed32 | 2021-05-14 17:42:30 +0200 | [diff] [blame] | 416 | spin_unlock(&fs_info->super_lock); |
Goldwyn Rodrigues | 66a2823 | 2020-08-25 10:02:33 -0500 | [diff] [blame] | 417 | sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation"); |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 418 | } |
| 419 | |
Nikolay Borisov | efc0e69 | 2021-11-25 11:14:41 +0200 | [diff] [blame] | 420 | void btrfs_exclop_balance(struct btrfs_fs_info *fs_info, |
| 421 | enum btrfs_exclusive_operation op) |
| 422 | { |
| 423 | switch (op) { |
| 424 | case BTRFS_EXCLOP_BALANCE_PAUSED: |
| 425 | spin_lock(&fs_info->super_lock); |
| 426 | ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE || |
| 427 | fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD); |
| 428 | fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED; |
| 429 | spin_unlock(&fs_info->super_lock); |
| 430 | break; |
| 431 | case BTRFS_EXCLOP_BALANCE: |
| 432 | spin_lock(&fs_info->super_lock); |
| 433 | ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED); |
| 434 | fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE; |
| 435 | spin_unlock(&fs_info->super_lock); |
| 436 | break; |
| 437 | default: |
| 438 | btrfs_warn(fs_info, |
| 439 | "invalid exclop balance operation %d requested", op); |
| 440 | } |
| 441 | } |
| 442 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 443 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
| 444 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 445 | struct inode *inode = file_inode(file); |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 446 | |
| 447 | return put_user(inode->i_generation, arg); |
| 448 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 449 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 450 | static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info, |
| 451 | void __user *arg) |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 452 | { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 453 | struct btrfs_device *device; |
| 454 | struct request_queue *q; |
| 455 | struct fstrim_range range; |
| 456 | u64 minlen = ULLONG_MAX; |
| 457 | u64 num_devices = 0; |
| 458 | int ret; |
| 459 | |
| 460 | if (!capable(CAP_SYS_ADMIN)) |
| 461 | return -EPERM; |
| 462 | |
Filipe Manana | f35f06c | 2019-03-26 10:49:56 +0000 | [diff] [blame] | 463 | /* |
Naohiro Aota | 1cb3dc3 | 2021-02-04 19:21:46 +0900 | [diff] [blame] | 464 | * btrfs_trim_block_group() depends on space cache, which is not |
| 465 | * available in zoned filesystem. So, disallow fitrim on a zoned |
| 466 | * filesystem for now. |
| 467 | */ |
| 468 | if (btrfs_is_zoned(fs_info)) |
| 469 | return -EOPNOTSUPP; |
| 470 | |
| 471 | /* |
Filipe Manana | f35f06c | 2019-03-26 10:49:56 +0000 | [diff] [blame] | 472 | * If the fs is mounted with nologreplay, which requires it to be |
| 473 | * mounted in RO mode as well, we can not allow discard on free space |
| 474 | * inside block groups, because log trees refer to extents that are not |
| 475 | * pinned in a block group's free space cache (pinning the extents is |
| 476 | * precisely the first phase of replaying a log tree). |
| 477 | */ |
| 478 | if (btrfs_test_opt(fs_info, NOLOGREPLAY)) |
| 479 | return -EROFS; |
| 480 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 481 | rcu_read_lock(); |
| 482 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
| 483 | dev_list) { |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 484 | if (!device->bdev) |
| 485 | continue; |
| 486 | q = bdev_get_queue(device->bdev); |
| 487 | if (blk_queue_discard(q)) { |
| 488 | num_devices++; |
Seraphime Kirkovski | 50d0446 | 2016-12-15 14:38:28 +0100 | [diff] [blame] | 489 | minlen = min_t(u64, q->limits.discard_granularity, |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 490 | minlen); |
| 491 | } |
| 492 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 493 | rcu_read_unlock(); |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 494 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 495 | if (!num_devices) |
| 496 | return -EOPNOTSUPP; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 497 | if (copy_from_user(&range, arg, sizeof(range))) |
| 498 | return -EFAULT; |
Qu Wenruo | 6ba9fc8 | 2018-09-07 14:16:24 +0800 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | * NOTE: Don't truncate the range using super->total_bytes. Bytenr of |
| 502 | * block group is in the logical address space, which can be any |
| 503 | * sectorsize aligned bytenr in the range [0, U64_MAX]. |
| 504 | */ |
| 505 | if (range.len < fs_info->sb->s_blocksize) |
Lukas Czerner | f4c697e | 2011-09-05 16:34:54 +0200 | [diff] [blame] | 506 | return -EINVAL; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 507 | |
| 508 | range.minlen = max(range.minlen, minlen); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 509 | ret = btrfs_trim_fs(fs_info, &range); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 510 | if (ret < 0) |
| 511 | return ret; |
| 512 | |
| 513 | if (copy_to_user(arg, &range, sizeof(range))) |
| 514 | return -EFAULT; |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 519 | int __pure btrfs_is_empty_uuid(u8 *uuid) |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 520 | { |
Chris Mason | 46e0f66 | 2013-11-15 12:14:55 +0100 | [diff] [blame] | 521 | int i; |
| 522 | |
| 523 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { |
| 524 | if (uuid[i]) |
| 525 | return 0; |
| 526 | } |
| 527 | return 1; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 530 | static noinline int create_subvol(struct user_namespace *mnt_userns, |
| 531 | struct inode *dir, struct dentry *dentry, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 532 | const char *name, int namelen, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 533 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 534 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 535 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 536 | struct btrfs_trans_handle *trans; |
| 537 | struct btrfs_key key; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 538 | struct btrfs_root_item *root_item; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 539 | struct btrfs_inode_item *inode_item; |
| 540 | struct extent_buffer *leaf; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 541 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 542 | struct btrfs_root *new_root; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 543 | struct btrfs_block_rsv block_rsv; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 544 | struct timespec64 cur_time = current_time(dir); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 545 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 546 | int ret; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 547 | dev_t anon_dev = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 548 | u64 objectid; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 549 | u64 index = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 550 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 551 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
| 552 | if (!root_item) |
| 553 | return -ENOMEM; |
| 554 | |
Nikolay Borisov | 543068a | 2020-12-07 17:32:33 +0200 | [diff] [blame] | 555 | ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid); |
Al Viro | 2fbe8c8 | 2011-07-16 21:38:06 -0400 | [diff] [blame] | 556 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 557 | goto fail_free; |
Josef Bacik | 6a91221 | 2010-11-20 09:48:00 +0000 | [diff] [blame] | 558 | |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 559 | ret = get_anon_bdev(&anon_dev); |
| 560 | if (ret < 0) |
| 561 | goto fail_free; |
| 562 | |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 563 | /* |
| 564 | * Don't create subvolume whose level is not zero. Or qgroup will be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 565 | * screwed up since it assumes subvolume qgroup's level to be 0. |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 566 | */ |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 567 | if (btrfs_qgroup_level(objectid)) { |
| 568 | ret = -ENOSPC; |
| 569 | goto fail_free; |
| 570 | } |
Qu Wenruo | e09fe2d | 2015-02-27 16:24:23 +0800 | [diff] [blame] | 571 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 572 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 573 | /* |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 574 | * The same as the snapshot creation, please see the comment |
| 575 | * of create_snapshot(). |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 576 | */ |
Gu JinXiang | c4c129d | 2018-05-30 11:00:38 +0800 | [diff] [blame] | 577 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 578 | if (ret) |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 579 | goto fail_free; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 580 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 581 | trans = btrfs_start_transaction(root, 0); |
| 582 | if (IS_ERR(trans)) { |
| 583 | ret = PTR_ERR(trans); |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 584 | btrfs_subvolume_release_metadata(root, &block_rsv); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 585 | goto fail_free; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 586 | } |
| 587 | trans->block_rsv = &block_rsv; |
| 588 | trans->bytes_reserved = block_rsv.size; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 589 | |
Lu Fengqi | a9377422 | 2018-07-18 14:45:41 +0800 | [diff] [blame] | 590 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 591 | if (ret) |
| 592 | goto fail; |
| 593 | |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 594 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0, |
| 595 | BTRFS_NESTING_NORMAL); |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 596 | if (IS_ERR(leaf)) { |
| 597 | ret = PTR_ERR(leaf); |
| 598 | goto fail; |
| 599 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 600 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 601 | btrfs_mark_buffer_dirty(leaf); |
| 602 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 603 | inode_item = &root_item->inode; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 604 | btrfs_set_stack_inode_generation(inode_item, 1); |
| 605 | btrfs_set_stack_inode_size(inode_item, 3); |
| 606 | btrfs_set_stack_inode_nlink(inode_item, 1); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 607 | btrfs_set_stack_inode_nbytes(inode_item, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 608 | fs_info->nodesize); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 609 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 610 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 611 | btrfs_set_root_flags(root_item, 0); |
| 612 | btrfs_set_root_limit(root_item, 0); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 613 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 614 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 615 | btrfs_set_root_bytenr(root_item, leaf->start); |
| 616 | btrfs_set_root_generation(root_item, trans->transid); |
| 617 | btrfs_set_root_level(root_item, 0); |
| 618 | btrfs_set_root_refs(root_item, 1); |
| 619 | btrfs_set_root_used(root_item, leaf->len); |
| 620 | btrfs_set_root_last_snapshot(root_item, 0); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 621 | |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 622 | btrfs_set_root_generation_v2(root_item, |
| 623 | btrfs_root_generation(root_item)); |
Andy Shevchenko | 807fc79 | 2020-02-24 17:37:51 +0200 | [diff] [blame] | 624 | generate_random_guid(root_item->uuid); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 625 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); |
| 626 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); |
| 627 | root_item->ctime = root_item->otime; |
| 628 | btrfs_set_root_ctransid(root_item, trans->transid); |
| 629 | btrfs_set_root_otransid(root_item, trans->transid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 630 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 631 | btrfs_tree_unlock(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 632 | |
Nikolay Borisov | 6994802 | 2020-12-07 17:32:37 +0200 | [diff] [blame] | 633 | btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 634 | |
| 635 | key.objectid = objectid; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 636 | key.offset = 0; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 637 | key.type = BTRFS_ROOT_ITEM_KEY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 638 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 639 | root_item); |
Filipe Manana | 67addf2 | 2021-04-20 10:55:12 +0100 | [diff] [blame] | 640 | if (ret) { |
| 641 | /* |
| 642 | * Since we don't abort the transaction in this case, free the |
| 643 | * tree block so that we don't leak space and leave the |
| 644 | * filesystem in an inconsistent state (an extent item in the |
Filipe Manana | 7a163608 | 2021-12-13 08:45:12 +0000 | [diff] [blame] | 645 | * extent tree with a backreference for a root that does not |
Filipe Manana | 212a58f | 2021-12-13 08:45:13 +0000 | [diff] [blame] | 646 | * exists). |
Filipe Manana | 67addf2 | 2021-04-20 10:55:12 +0100 | [diff] [blame] | 647 | */ |
Filipe Manana | 212a58f | 2021-12-13 08:45:13 +0000 | [diff] [blame] | 648 | btrfs_tree_lock(leaf); |
| 649 | btrfs_clean_tree_block(leaf); |
| 650 | btrfs_tree_unlock(leaf); |
Filipe Manana | 7a163608 | 2021-12-13 08:45:12 +0000 | [diff] [blame] | 651 | btrfs_free_tree_block(trans, objectid, leaf, 0, 1); |
Filipe Manana | 67addf2 | 2021-04-20 10:55:12 +0100 | [diff] [blame] | 652 | free_extent_buffer(leaf); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 653 | goto fail; |
Filipe Manana | 67addf2 | 2021-04-20 10:55:12 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | free_extent_buffer(leaf); |
| 657 | leaf = NULL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 658 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 659 | key.offset = (u64)-1; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 660 | new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 661 | if (IS_ERR(new_root)) { |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 662 | free_anon_bdev(anon_dev); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 663 | ret = PTR_ERR(new_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 664 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 665 | goto fail; |
| 666 | } |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 667 | /* Freeing will be done in btrfs_put_root() of new_root */ |
| 668 | anon_dev = 0; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 669 | |
Josef Bacik | 221581e | 2021-03-12 15:25:06 -0500 | [diff] [blame] | 670 | ret = btrfs_record_root_in_trans(trans, new_root); |
| 671 | if (ret) { |
| 672 | btrfs_put_root(new_root); |
| 673 | btrfs_abort_transaction(trans, ret); |
| 674 | goto fail; |
| 675 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 676 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 677 | ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns); |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 678 | btrfs_put_root(new_root); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 679 | if (ret) { |
| 680 | /* We potentially lose an unused inode item here */ |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 681 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | ce59897 | 2011-07-26 11:32:23 -0700 | [diff] [blame] | 682 | goto fail; |
| 683 | } |
| 684 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 685 | /* |
| 686 | * insert the directory item |
| 687 | */ |
Nikolay Borisov | 877574e | 2017-02-20 13:50:33 +0200 | [diff] [blame] | 688 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 689 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 690 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 691 | goto fail; |
| 692 | } |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 693 | |
Lu Fengqi | 684572d | 2018-08-04 21:10:57 +0800 | [diff] [blame] | 694 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 695 | BTRFS_FT_DIR, index); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 696 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 697 | btrfs_abort_transaction(trans, ret); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 698 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 699 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 700 | |
Nikolay Borisov | 6ef06d2 | 2017-02-20 13:50:34 +0200 | [diff] [blame] | 701 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
Nikolay Borisov | 9a56fcd | 2020-11-02 16:48:59 +0200 | [diff] [blame] | 702 | ret = btrfs_update_inode(trans, root, BTRFS_I(dir)); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 703 | if (ret) { |
| 704 | btrfs_abort_transaction(trans, ret); |
| 705 | goto fail; |
| 706 | } |
Yan Zheng | 52c2617 | 2009-01-05 15:43:43 -0500 | [diff] [blame] | 707 | |
Lu Fengqi | 6025c19 | 2018-08-01 11:32:29 +0800 | [diff] [blame] | 708 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 709 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
Josef Bacik | c7e54b5 | 2019-12-06 09:37:15 -0500 | [diff] [blame] | 710 | if (ret) { |
| 711 | btrfs_abort_transaction(trans, ret); |
| 712 | goto fail; |
| 713 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 714 | |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 715 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 716 | BTRFS_UUID_KEY_SUBVOL, objectid); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 717 | if (ret) |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 718 | btrfs_abort_transaction(trans, ret); |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 719 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 720 | fail: |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 721 | kfree(root_item); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 722 | trans->block_rsv = NULL; |
| 723 | trans->bytes_reserved = 0; |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 724 | btrfs_subvolume_release_metadata(root, &block_rsv); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 725 | |
Filipe Manana | 1b58ae0 | 2021-12-13 08:45:14 +0000 | [diff] [blame] | 726 | if (ret) |
| 727 | btrfs_end_transaction(trans); |
| 728 | else |
| 729 | ret = btrfs_commit_transaction(trans); |
Chris Mason | 1a65e24 | 2013-02-06 12:06:02 -0500 | [diff] [blame] | 730 | |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 731 | if (!ret) { |
| 732 | inode = btrfs_lookup_dentry(dir, dentry); |
Liu Bo | de6e820 | 2014-01-09 14:57:06 +0800 | [diff] [blame] | 733 | if (IS_ERR(inode)) |
| 734 | return PTR_ERR(inode); |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 735 | d_instantiate(dentry, inode); |
| 736 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 737 | return ret; |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 738 | |
| 739 | fail_free: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 740 | if (anon_dev) |
| 741 | free_anon_bdev(anon_dev); |
David Sterba | 49a3c4d | 2016-03-24 17:49:22 +0100 | [diff] [blame] | 742 | kfree(root_item); |
| 743 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 744 | } |
| 745 | |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 746 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 747 | struct dentry *dentry, bool readonly, |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 748 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 749 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 750 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 751 | struct inode *inode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 752 | struct btrfs_pending_snapshot *pending_snapshot; |
| 753 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 754 | int ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 755 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 756 | if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 757 | return -EINVAL; |
| 758 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 759 | if (atomic_read(&root->nr_swapfiles)) { |
| 760 | btrfs_warn(fs_info, |
| 761 | "cannot snapshot subvolume with active swapfile"); |
| 762 | return -ETXTBSY; |
| 763 | } |
| 764 | |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 765 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 766 | if (!pending_snapshot) |
| 767 | return -ENOMEM; |
| 768 | |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 769 | ret = get_anon_bdev(&pending_snapshot->anon_dev); |
| 770 | if (ret < 0) |
| 771 | goto free_pending; |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 772 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
David Sterba | 23269bf | 2017-02-13 11:03:44 +0100 | [diff] [blame] | 773 | GFP_KERNEL); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 774 | pending_snapshot->path = btrfs_alloc_path(); |
| 775 | if (!pending_snapshot->root_item || !pending_snapshot->path) { |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 776 | ret = -ENOMEM; |
| 777 | goto free_pending; |
| 778 | } |
| 779 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 780 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
| 781 | BTRFS_BLOCK_RSV_TEMP); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 782 | /* |
| 783 | * 1 - parent dir inode |
| 784 | * 2 - dir entries |
| 785 | * 1 - root item |
| 786 | * 2 - root ref/backref |
| 787 | * 1 - root of snapshot |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 788 | * 1 - UUID item |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 789 | */ |
| 790 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 791 | &pending_snapshot->block_rsv, 8, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 792 | false); |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 793 | if (ret) |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 794 | goto free_pending; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 795 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 796 | pending_snapshot->dentry = dentry; |
| 797 | pending_snapshot->root = root; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 798 | pending_snapshot->readonly = readonly; |
Miao Xie | e9662f7 | 2013-02-28 10:01:15 +0000 | [diff] [blame] | 799 | pending_snapshot->dir = dir; |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 800 | pending_snapshot->inherit = inherit; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 801 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 802 | trans = btrfs_start_transaction(root, 0); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 803 | if (IS_ERR(trans)) { |
| 804 | ret = PTR_ERR(trans); |
| 805 | goto fail; |
| 806 | } |
| 807 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 808 | spin_lock(&fs_info->trans_lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 809 | list_add(&pending_snapshot->list, |
| 810 | &trans->transaction->pending_snapshots); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 811 | spin_unlock(&fs_info->trans_lock); |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 812 | |
| 813 | ret = btrfs_commit_transaction(trans); |
Miao Xie | aec8030 | 2013-03-04 09:44:29 +0000 | [diff] [blame] | 814 | if (ret) |
Josef Bacik | c37b2b6 | 2012-10-22 15:51:44 -0400 | [diff] [blame] | 815 | goto fail; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 816 | |
| 817 | ret = pending_snapshot->error; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 818 | if (ret) |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 819 | goto fail; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 820 | |
Chris Mason | d379730 | 2014-10-15 13:50:56 -0700 | [diff] [blame] | 821 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
| 822 | if (ret) |
| 823 | goto fail; |
| 824 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 825 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 826 | if (IS_ERR(inode)) { |
| 827 | ret = PTR_ERR(inode); |
| 828 | goto fail; |
| 829 | } |
Tsutomu Itoh | 5662344b3 | 2013-12-13 09:51:42 +0900 | [diff] [blame] | 830 | |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 831 | d_instantiate(dentry, inode); |
| 832 | ret = 0; |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 833 | pending_snapshot->anon_dev = 0; |
Yan, Zheng | 2e4bfab | 2009-11-12 09:37:02 +0000 | [diff] [blame] | 834 | fail: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 835 | /* Prevent double freeing of anon_dev */ |
| 836 | if (ret && pending_snapshot->snap) |
| 837 | pending_snapshot->snap->anon_dev = 0; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 838 | btrfs_put_root(pending_snapshot->snap); |
Qu Wenruo | e85fde5 | 2020-07-24 14:46:10 +0800 | [diff] [blame] | 839 | btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv); |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 840 | free_pending: |
Qu Wenruo | 2dfb1e4 | 2020-06-16 10:17:36 +0800 | [diff] [blame] | 841 | if (pending_snapshot->anon_dev) |
| 842 | free_anon_bdev(pending_snapshot->anon_dev); |
David Sterba | b0c0ea6 | 2015-11-10 18:54:00 +0100 | [diff] [blame] | 843 | kfree(pending_snapshot->root_item); |
David Sterba | 8546b57 | 2015-11-10 18:54:03 +0100 | [diff] [blame] | 844 | btrfs_free_path(pending_snapshot->path); |
David Sterba | a1ee736 | 2015-11-10 18:53:56 +0100 | [diff] [blame] | 845 | kfree(pending_snapshot); |
| 846 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 847 | return ret; |
| 848 | } |
| 849 | |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 850 | /* copy of may_delete in fs/namei.c() |
| 851 | * Check whether we can remove a link victim from directory dir, check |
| 852 | * whether the type of victim is right. |
| 853 | * 1. We can't do it if dir is read-only (done in permission()) |
| 854 | * 2. We should have write and exec permissions on dir |
| 855 | * 3. We can't remove anything from append-only dir |
| 856 | * 4. We can't do anything with immutable dir (done in permission()) |
| 857 | * 5. If the sticky bit on dir is set we should either |
| 858 | * a. be owner of dir, or |
| 859 | * b. be owner of victim, or |
| 860 | * c. have CAP_FOWNER capability |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 861 | * 6. If the victim is append-only or immutable we can't do anything with |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 862 | * links pointing to it. |
| 863 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 864 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 865 | * 9. We can't remove a root or mountpoint. |
| 866 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 867 | * nfs_async_unlink(). |
| 868 | */ |
| 869 | |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 870 | static int btrfs_may_delete(struct user_namespace *mnt_userns, |
| 871 | struct inode *dir, struct dentry *victim, int isdir) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 872 | { |
| 873 | int error; |
| 874 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 875 | if (d_really_is_negative(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 876 | return -ENOENT; |
| 877 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 878 | BUG_ON(d_inode(victim->d_parent) != dir); |
Jeff Layton | 4fa6b5e | 2012-10-10 15:25:25 -0400 | [diff] [blame] | 879 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 880 | |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 881 | error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 882 | if (error) |
| 883 | return error; |
| 884 | if (IS_APPEND(dir)) |
| 885 | return -EPERM; |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 886 | if (check_sticky(mnt_userns, dir, d_inode(victim)) || |
Christian Brauner | ba73d98 | 2021-01-21 14:19:31 +0100 | [diff] [blame] | 887 | IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) || |
| 888 | IS_SWAPFILE(d_inode(victim))) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 889 | return -EPERM; |
| 890 | if (isdir) { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 891 | if (!d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 892 | return -ENOTDIR; |
| 893 | if (IS_ROOT(victim)) |
| 894 | return -EBUSY; |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 895 | } else if (d_is_dir(victim)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 896 | return -EISDIR; |
| 897 | if (IS_DEADDIR(dir)) |
| 898 | return -ENOENT; |
| 899 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 900 | return -EBUSY; |
| 901 | return 0; |
| 902 | } |
| 903 | |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 904 | /* copy of may_create in fs/namei.c() */ |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 905 | static inline int btrfs_may_create(struct user_namespace *mnt_userns, |
| 906 | struct inode *dir, struct dentry *child) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 907 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 908 | if (d_really_is_positive(child)) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 909 | return -EEXIST; |
| 910 | if (IS_DEADDIR(dir)) |
| 911 | return -ENOENT; |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 912 | if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns)) |
Christian Brauner | 5474bf4 | 2021-07-27 12:48:51 +0200 | [diff] [blame] | 913 | return -EOVERFLOW; |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 914 | return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Create a new subvolume below @parent. This is largely modeled after |
| 919 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
| 920 | * inside this filesystem so it's quite a bit simpler. |
| 921 | */ |
Al Viro | 9287209 | 2016-11-20 19:34:31 -0500 | [diff] [blame] | 922 | static noinline int btrfs_mksubvol(const struct path *parent, |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 923 | struct user_namespace *mnt_userns, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 924 | const char *name, int namelen, |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 925 | struct btrfs_root *snap_src, |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 926 | bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 927 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 928 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 929 | struct inode *dir = d_inode(parent->dentry); |
| 930 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 931 | struct dentry *dentry; |
| 932 | int error; |
| 933 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 934 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 935 | if (error == -EINTR) |
| 936 | return error; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 937 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 938 | dentry = lookup_one(mnt_userns, name, parent->dentry, namelen); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 939 | error = PTR_ERR(dentry); |
| 940 | if (IS_ERR(dentry)) |
| 941 | goto out_unlock; |
| 942 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 943 | error = btrfs_may_create(mnt_userns, dir, dentry); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 944 | if (error) |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 945 | goto out_dput; |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 946 | |
Chris Mason | 9c52057 | 2012-12-17 14:26:57 -0500 | [diff] [blame] | 947 | /* |
| 948 | * even if this name doesn't exist, we may get hash collisions. |
| 949 | * check for them now when we can safely fail |
| 950 | */ |
| 951 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, |
| 952 | dir->i_ino, name, |
| 953 | namelen); |
| 954 | if (error) |
| 955 | goto out_dput; |
| 956 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 957 | down_read(&fs_info->subvol_sem); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 958 | |
| 959 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
| 960 | goto out_up_read; |
| 961 | |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 962 | if (snap_src) |
| 963 | error = create_snapshot(snap_src, dir, dentry, readonly, inherit); |
| 964 | else |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 965 | error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit); |
Nikolay Borisov | 9babda9 | 2020-03-13 17:23:20 +0200 | [diff] [blame] | 966 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 967 | if (!error) |
| 968 | fsnotify_mkdir(dir, dentry); |
| 969 | out_up_read: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 970 | up_read(&fs_info->subvol_sem); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 971 | out_dput: |
| 972 | dput(dentry); |
| 973 | out_unlock: |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 974 | btrfs_inode_unlock(dir, 0); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 975 | return error; |
| 976 | } |
| 977 | |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 978 | static noinline int btrfs_mksnapshot(const struct path *parent, |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 979 | struct user_namespace *mnt_userns, |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 980 | const char *name, int namelen, |
| 981 | struct btrfs_root *root, |
| 982 | bool readonly, |
| 983 | struct btrfs_qgroup_inherit *inherit) |
| 984 | { |
| 985 | int ret; |
| 986 | bool snapshot_force_cow = false; |
| 987 | |
| 988 | /* |
| 989 | * Force new buffered writes to reserve space even when NOCOW is |
| 990 | * possible. This is to avoid later writeback (running dealloc) to |
| 991 | * fallback to COW mode and unexpectedly fail with ENOSPC. |
| 992 | */ |
| 993 | btrfs_drew_read_lock(&root->snapshot_lock); |
| 994 | |
Filipe Manana | f9baa50 | 2021-04-22 12:08:05 +0100 | [diff] [blame] | 995 | ret = btrfs_start_delalloc_snapshot(root, false); |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 996 | if (ret) |
| 997 | goto out; |
| 998 | |
| 999 | /* |
| 1000 | * All previous writes have started writeback in NOCOW mode, so now |
| 1001 | * we force future writes to fallback to COW mode during snapshot |
| 1002 | * creation. |
| 1003 | */ |
| 1004 | atomic_inc(&root->snapshot_force_cow); |
| 1005 | snapshot_force_cow = true; |
| 1006 | |
| 1007 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
| 1008 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1009 | ret = btrfs_mksubvol(parent, mnt_userns, name, namelen, |
Robbie Ko | c11fbb6 | 2020-05-14 17:19:18 +0800 | [diff] [blame] | 1010 | root, readonly, inherit); |
| 1011 | out: |
| 1012 | if (snapshot_force_cow) |
| 1013 | atomic_dec(&root->snapshot_force_cow); |
| 1014 | btrfs_drew_read_unlock(&root->snapshot_lock); |
| 1015 | return ret; |
| 1016 | } |
| 1017 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1018 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start, |
| 1019 | bool locked) |
Liu Bo | 17ce6ef | 2012-03-29 09:57:45 -0400 | [diff] [blame] | 1020 | { |
| 1021 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1022 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1023 | struct extent_map *em; |
Qu Wenruo | 76068ca | 2021-08-06 16:12:34 +0800 | [diff] [blame] | 1024 | const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | * hopefully we have this extent in the tree already, try without |
| 1028 | * the full extent lock |
| 1029 | */ |
| 1030 | read_lock(&em_tree->lock); |
Qu Wenruo | 76068ca | 2021-08-06 16:12:34 +0800 | [diff] [blame] | 1031 | em = lookup_extent_mapping(em_tree, start, sectorsize); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1032 | read_unlock(&em_tree->lock); |
| 1033 | |
| 1034 | if (!em) { |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1035 | struct extent_state *cached = NULL; |
Qu Wenruo | 76068ca | 2021-08-06 16:12:34 +0800 | [diff] [blame] | 1036 | u64 end = start + sectorsize - 1; |
Filipe Manana | 308d980 | 2014-03-11 13:56:15 +0000 | [diff] [blame] | 1037 | |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1038 | /* get the big lock and read metadata off disk */ |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1039 | if (!locked) |
| 1040 | lock_extent_bits(io_tree, start, end, &cached); |
Qu Wenruo | 76068ca | 2021-08-06 16:12:34 +0800 | [diff] [blame] | 1041 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize); |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1042 | if (!locked) |
| 1043 | unlock_extent_cached(io_tree, start, end, &cached); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1044 | |
Dan Carpenter | 6cf8bfb | 2010-03-20 11:22:10 +0000 | [diff] [blame] | 1045 | if (IS_ERR(em)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1046 | return NULL; |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1049 | return em; |
| 1050 | } |
| 1051 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1052 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em, |
| 1053 | bool locked) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1054 | { |
| 1055 | struct extent_map *next; |
| 1056 | bool ret = true; |
| 1057 | |
| 1058 | /* this is the last extent */ |
| 1059 | if (em->start + em->len >= i_size_read(inode)) |
| 1060 | return false; |
| 1061 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1062 | next = defrag_lookup_extent(inode, em->start + em->len, locked); |
Chris Mason | e9512d7 | 2014-08-26 13:55:54 -0700 | [diff] [blame] | 1063 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
| 1064 | ret = false; |
| 1065 | else if ((em->block_start + em->block_len == next->block_start) && |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1066 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
Li Zefan | 6c282eb | 2012-06-11 16:03:35 +0800 | [diff] [blame] | 1067 | ret = false; |
| 1068 | |
| 1069 | free_extent_map(next); |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1073 | /* |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1074 | * Prepare one page to be defragged. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1075 | * |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1076 | * This will ensure: |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1077 | * |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1078 | * - Returned page is locked and has been set up properly. |
| 1079 | * - No ordered extent exists in the page. |
| 1080 | * - The page is uptodate. |
| 1081 | * |
| 1082 | * NOTE: Caller should also wait for page writeback after the cluster is |
| 1083 | * prepared, here we don't do writeback wait for each page. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1084 | */ |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1085 | static struct page *defrag_prepare_one_page(struct btrfs_inode *inode, |
| 1086 | pgoff_t index) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1087 | { |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1088 | struct address_space *mapping = inode->vfs_inode.i_mapping; |
| 1089 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
| 1090 | u64 page_start = (u64)index << PAGE_SHIFT; |
| 1091 | u64 page_end = page_start + PAGE_SIZE - 1; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1092 | struct extent_state *cached_state = NULL; |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1093 | struct page *page; |
| 1094 | int ret; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1095 | |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1096 | again: |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1097 | page = find_or_create_page(mapping, index, mask); |
| 1098 | if (!page) |
| 1099 | return ERR_PTR(-ENOMEM); |
| 1100 | |
Omar Sandoval | 24bcb45 | 2021-10-19 20:35:01 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Since we can defragment files opened read-only, we can encounter |
| 1103 | * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We |
| 1104 | * can't do I/O using huge pages yet, so return an error for now. |
| 1105 | * Filesystem transparent huge pages are typically only used for |
| 1106 | * executables that explicitly enable them, so this isn't very |
| 1107 | * restrictive. |
| 1108 | */ |
| 1109 | if (PageCompound(page)) { |
| 1110 | unlock_page(page); |
| 1111 | put_page(page); |
| 1112 | return ERR_PTR(-ETXTBSY); |
| 1113 | } |
| 1114 | |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1115 | ret = set_page_extent_mapped(page); |
| 1116 | if (ret < 0) { |
| 1117 | unlock_page(page); |
| 1118 | put_page(page); |
| 1119 | return ERR_PTR(ret); |
| 1120 | } |
| 1121 | |
| 1122 | /* Wait for any existing ordered extent in the range */ |
| 1123 | while (1) { |
| 1124 | struct btrfs_ordered_extent *ordered; |
| 1125 | |
| 1126 | lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state); |
| 1127 | ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE); |
| 1128 | unlock_extent_cached(&inode->io_tree, page_start, page_end, |
| 1129 | &cached_state); |
| 1130 | if (!ordered) |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1131 | break; |
| 1132 | |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1133 | unlock_page(page); |
| 1134 | btrfs_start_ordered_extent(ordered, 1); |
| 1135 | btrfs_put_ordered_extent(ordered); |
| 1136 | lock_page(page); |
| 1137 | /* |
| 1138 | * We unlocked the page above, so we need check if it was |
| 1139 | * released or not. |
| 1140 | */ |
| 1141 | if (page->mapping != mapping || !PagePrivate(page)) { |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1142 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1143 | put_page(page); |
Miao Xie | 600a45e | 2012-02-16 15:01:24 +0800 | [diff] [blame] | 1144 | goto again; |
| 1145 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1146 | } |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1147 | |
| 1148 | /* |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1149 | * Now the page range has no ordered extent any more. Read the page to |
| 1150 | * make it uptodate. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1151 | */ |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1152 | if (!PageUptodate(page)) { |
| 1153 | btrfs_readpage(NULL, page); |
| 1154 | lock_page(page); |
| 1155 | if (page->mapping != mapping || !PagePrivate(page)) { |
| 1156 | unlock_page(page); |
| 1157 | put_page(page); |
| 1158 | goto again; |
Filipe Manana | 7f458a3 | 2020-11-04 11:07:33 +0000 | [diff] [blame] | 1159 | } |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1160 | if (!PageUptodate(page)) { |
| 1161 | unlock_page(page); |
| 1162 | put_page(page); |
| 1163 | return ERR_PTR(-EIO); |
Filipe Manana | 7f458a3 | 2020-11-04 11:07:33 +0000 | [diff] [blame] | 1164 | } |
Filipe Manana | 7f458a3 | 2020-11-04 11:07:33 +0000 | [diff] [blame] | 1165 | } |
Qu Wenruo | 5767b50 | 2021-08-06 16:12:35 +0800 | [diff] [blame] | 1166 | return page; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
Qu Wenruo | eb793cf | 2021-08-06 16:12:36 +0800 | [diff] [blame] | 1169 | struct defrag_target_range { |
| 1170 | struct list_head list; |
| 1171 | u64 start; |
| 1172 | u64 len; |
| 1173 | }; |
| 1174 | |
| 1175 | /* |
| 1176 | * Collect all valid target extents. |
| 1177 | * |
| 1178 | * @start: file offset to lookup |
| 1179 | * @len: length to lookup |
| 1180 | * @extent_thresh: file extent size threshold, any extent size >= this value |
| 1181 | * will be ignored |
| 1182 | * @newer_than: only defrag extents newer than this value |
| 1183 | * @do_compress: whether the defrag is doing compression |
| 1184 | * if true, @extent_thresh will be ignored and all regular |
| 1185 | * file extents meeting @newer_than will be targets. |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1186 | * @locked: if the range has already held extent lock |
Qu Wenruo | eb793cf | 2021-08-06 16:12:36 +0800 | [diff] [blame] | 1187 | * @target_list: list of targets file extents |
| 1188 | */ |
| 1189 | static int defrag_collect_targets(struct btrfs_inode *inode, |
| 1190 | u64 start, u64 len, u32 extent_thresh, |
| 1191 | u64 newer_than, bool do_compress, |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1192 | bool locked, struct list_head *target_list) |
Qu Wenruo | eb793cf | 2021-08-06 16:12:36 +0800 | [diff] [blame] | 1193 | { |
| 1194 | u64 cur = start; |
| 1195 | int ret = 0; |
| 1196 | |
| 1197 | while (cur < start + len) { |
| 1198 | struct extent_map *em; |
| 1199 | struct defrag_target_range *new; |
| 1200 | bool next_mergeable = true; |
| 1201 | u64 range_len; |
| 1202 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1203 | em = defrag_lookup_extent(&inode->vfs_inode, cur, locked); |
Qu Wenruo | eb793cf | 2021-08-06 16:12:36 +0800 | [diff] [blame] | 1204 | if (!em) |
| 1205 | break; |
| 1206 | |
| 1207 | /* Skip hole/inline/preallocated extents */ |
| 1208 | if (em->block_start >= EXTENT_MAP_LAST_BYTE || |
| 1209 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
| 1210 | goto next; |
| 1211 | |
| 1212 | /* Skip older extent */ |
| 1213 | if (em->generation < newer_than) |
| 1214 | goto next; |
| 1215 | |
| 1216 | /* |
| 1217 | * For do_compress case, we want to compress all valid file |
| 1218 | * extents, thus no @extent_thresh or mergeable check. |
| 1219 | */ |
| 1220 | if (do_compress) |
| 1221 | goto add; |
| 1222 | |
| 1223 | /* Skip too large extent */ |
| 1224 | if (em->len >= extent_thresh) |
| 1225 | goto next; |
| 1226 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1227 | next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em, |
| 1228 | locked); |
Qu Wenruo | eb793cf | 2021-08-06 16:12:36 +0800 | [diff] [blame] | 1229 | if (!next_mergeable) { |
| 1230 | struct defrag_target_range *last; |
| 1231 | |
| 1232 | /* Empty target list, no way to merge with last entry */ |
| 1233 | if (list_empty(target_list)) |
| 1234 | goto next; |
| 1235 | last = list_entry(target_list->prev, |
| 1236 | struct defrag_target_range, list); |
| 1237 | /* Not mergeable with last entry */ |
| 1238 | if (last->start + last->len != cur) |
| 1239 | goto next; |
| 1240 | |
| 1241 | /* Mergeable, fall through to add it to @target_list. */ |
| 1242 | } |
| 1243 | |
| 1244 | add: |
| 1245 | range_len = min(extent_map_end(em), start + len) - cur; |
| 1246 | /* |
| 1247 | * This one is a good target, check if it can be merged into |
| 1248 | * last range of the target list. |
| 1249 | */ |
| 1250 | if (!list_empty(target_list)) { |
| 1251 | struct defrag_target_range *last; |
| 1252 | |
| 1253 | last = list_entry(target_list->prev, |
| 1254 | struct defrag_target_range, list); |
| 1255 | ASSERT(last->start + last->len <= cur); |
| 1256 | if (last->start + last->len == cur) { |
| 1257 | /* Mergeable, enlarge the last entry */ |
| 1258 | last->len += range_len; |
| 1259 | goto next; |
| 1260 | } |
| 1261 | /* Fall through to allocate a new entry */ |
| 1262 | } |
| 1263 | |
| 1264 | /* Allocate new defrag_target_range */ |
| 1265 | new = kmalloc(sizeof(*new), GFP_NOFS); |
| 1266 | if (!new) { |
| 1267 | free_extent_map(em); |
| 1268 | ret = -ENOMEM; |
| 1269 | break; |
| 1270 | } |
| 1271 | new->start = cur; |
| 1272 | new->len = range_len; |
| 1273 | list_add_tail(&new->list, target_list); |
| 1274 | |
| 1275 | next: |
| 1276 | cur = extent_map_end(em); |
| 1277 | free_extent_map(em); |
| 1278 | } |
| 1279 | if (ret < 0) { |
| 1280 | struct defrag_target_range *entry; |
| 1281 | struct defrag_target_range *tmp; |
| 1282 | |
| 1283 | list_for_each_entry_safe(entry, tmp, target_list, list) { |
| 1284 | list_del_init(&entry->list); |
| 1285 | kfree(entry); |
| 1286 | } |
| 1287 | } |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Qu Wenruo | 22b398e | 2021-08-06 16:12:37 +0800 | [diff] [blame] | 1291 | #define CLUSTER_SIZE (SZ_256K) |
| 1292 | |
| 1293 | /* |
| 1294 | * Defrag one contiguous target range. |
| 1295 | * |
| 1296 | * @inode: target inode |
| 1297 | * @target: target range to defrag |
| 1298 | * @pages: locked pages covering the defrag range |
| 1299 | * @nr_pages: number of locked pages |
| 1300 | * |
| 1301 | * Caller should ensure: |
| 1302 | * |
| 1303 | * - Pages are prepared |
| 1304 | * Pages should be locked, no ordered extent in the pages range, |
| 1305 | * no writeback. |
| 1306 | * |
| 1307 | * - Extent bits are locked |
| 1308 | */ |
| 1309 | static int defrag_one_locked_target(struct btrfs_inode *inode, |
| 1310 | struct defrag_target_range *target, |
| 1311 | struct page **pages, int nr_pages, |
| 1312 | struct extent_state **cached_state) |
| 1313 | { |
| 1314 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
| 1315 | struct extent_changeset *data_reserved = NULL; |
| 1316 | const u64 start = target->start; |
| 1317 | const u64 len = target->len; |
| 1318 | unsigned long last_index = (start + len - 1) >> PAGE_SHIFT; |
| 1319 | unsigned long start_index = start >> PAGE_SHIFT; |
| 1320 | unsigned long first_index = page_index(pages[0]); |
| 1321 | int ret = 0; |
| 1322 | int i; |
| 1323 | |
| 1324 | ASSERT(last_index - first_index + 1 <= nr_pages); |
| 1325 | |
| 1326 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len); |
| 1327 | if (ret < 0) |
| 1328 | return ret; |
| 1329 | clear_extent_bit(&inode->io_tree, start, start + len - 1, |
| 1330 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | |
| 1331 | EXTENT_DEFRAG, 0, 0, cached_state); |
| 1332 | set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state); |
| 1333 | |
| 1334 | /* Update the page status */ |
| 1335 | for (i = start_index - first_index; i <= last_index - first_index; i++) { |
| 1336 | ClearPageChecked(pages[i]); |
| 1337 | btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len); |
| 1338 | } |
| 1339 | btrfs_delalloc_release_extents(inode, len); |
| 1340 | extent_changeset_free(data_reserved); |
| 1341 | |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
Qu Wenruo | e9eec72 | 2021-08-06 16:12:38 +0800 | [diff] [blame] | 1345 | static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len, |
| 1346 | u32 extent_thresh, u64 newer_than, bool do_compress) |
| 1347 | { |
| 1348 | struct extent_state *cached_state = NULL; |
| 1349 | struct defrag_target_range *entry; |
| 1350 | struct defrag_target_range *tmp; |
| 1351 | LIST_HEAD(target_list); |
| 1352 | struct page **pages; |
| 1353 | const u32 sectorsize = inode->root->fs_info->sectorsize; |
| 1354 | u64 last_index = (start + len - 1) >> PAGE_SHIFT; |
| 1355 | u64 start_index = start >> PAGE_SHIFT; |
| 1356 | unsigned int nr_pages = last_index - start_index + 1; |
| 1357 | int ret = 0; |
| 1358 | int i; |
| 1359 | |
| 1360 | ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE); |
| 1361 | ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize)); |
| 1362 | |
| 1363 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
| 1364 | if (!pages) |
| 1365 | return -ENOMEM; |
| 1366 | |
| 1367 | /* Prepare all pages */ |
| 1368 | for (i = 0; i < nr_pages; i++) { |
| 1369 | pages[i] = defrag_prepare_one_page(inode, start_index + i); |
| 1370 | if (IS_ERR(pages[i])) { |
| 1371 | ret = PTR_ERR(pages[i]); |
| 1372 | pages[i] = NULL; |
| 1373 | goto free_pages; |
| 1374 | } |
| 1375 | } |
| 1376 | for (i = 0; i < nr_pages; i++) |
| 1377 | wait_on_page_writeback(pages[i]); |
| 1378 | |
| 1379 | /* Lock the pages range */ |
| 1380 | lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT, |
| 1381 | (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, |
| 1382 | &cached_state); |
| 1383 | /* |
| 1384 | * Now we have a consistent view about the extent map, re-check |
| 1385 | * which range really needs to be defragged. |
| 1386 | * |
| 1387 | * And this time we have extent locked already, pass @locked = true |
| 1388 | * so that we won't relock the extent range and cause deadlock. |
| 1389 | */ |
| 1390 | ret = defrag_collect_targets(inode, start, len, extent_thresh, |
| 1391 | newer_than, do_compress, true, |
| 1392 | &target_list); |
| 1393 | if (ret < 0) |
| 1394 | goto unlock_extent; |
| 1395 | |
| 1396 | list_for_each_entry(entry, &target_list, list) { |
| 1397 | ret = defrag_one_locked_target(inode, entry, pages, nr_pages, |
| 1398 | &cached_state); |
| 1399 | if (ret < 0) |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | list_for_each_entry_safe(entry, tmp, &target_list, list) { |
| 1404 | list_del_init(&entry->list); |
| 1405 | kfree(entry); |
| 1406 | } |
| 1407 | unlock_extent: |
| 1408 | unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT, |
| 1409 | (last_index << PAGE_SHIFT) + PAGE_SIZE - 1, |
| 1410 | &cached_state); |
| 1411 | free_pages: |
| 1412 | for (i = 0; i < nr_pages; i++) { |
| 1413 | if (pages[i]) { |
| 1414 | unlock_page(pages[i]); |
| 1415 | put_page(pages[i]); |
| 1416 | } |
| 1417 | } |
| 1418 | kfree(pages); |
| 1419 | return ret; |
| 1420 | } |
| 1421 | |
Qu Wenruo | b18c3ab | 2021-08-06 16:12:39 +0800 | [diff] [blame] | 1422 | static int defrag_one_cluster(struct btrfs_inode *inode, |
| 1423 | struct file_ra_state *ra, |
| 1424 | u64 start, u32 len, u32 extent_thresh, |
| 1425 | u64 newer_than, bool do_compress, |
| 1426 | unsigned long *sectors_defragged, |
| 1427 | unsigned long max_sectors) |
| 1428 | { |
| 1429 | const u32 sectorsize = inode->root->fs_info->sectorsize; |
| 1430 | struct defrag_target_range *entry; |
| 1431 | struct defrag_target_range *tmp; |
| 1432 | LIST_HEAD(target_list); |
| 1433 | int ret; |
| 1434 | |
| 1435 | BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE)); |
| 1436 | ret = defrag_collect_targets(inode, start, len, extent_thresh, |
| 1437 | newer_than, do_compress, false, |
| 1438 | &target_list); |
| 1439 | if (ret < 0) |
| 1440 | goto out; |
| 1441 | |
| 1442 | list_for_each_entry(entry, &target_list, list) { |
| 1443 | u32 range_len = entry->len; |
| 1444 | |
| 1445 | /* Reached the limit */ |
| 1446 | if (max_sectors && max_sectors == *sectors_defragged) |
| 1447 | break; |
| 1448 | |
| 1449 | if (max_sectors) |
| 1450 | range_len = min_t(u32, range_len, |
| 1451 | (max_sectors - *sectors_defragged) * sectorsize); |
| 1452 | |
| 1453 | if (ra) |
| 1454 | page_cache_sync_readahead(inode->vfs_inode.i_mapping, |
| 1455 | ra, NULL, entry->start >> PAGE_SHIFT, |
| 1456 | ((entry->start + range_len - 1) >> PAGE_SHIFT) - |
| 1457 | (entry->start >> PAGE_SHIFT) + 1); |
| 1458 | /* |
| 1459 | * Here we may not defrag any range if holes are punched before |
| 1460 | * we locked the pages. |
| 1461 | * But that's fine, it only affects the @sectors_defragged |
| 1462 | * accounting. |
| 1463 | */ |
| 1464 | ret = defrag_one_range(inode, entry->start, range_len, |
| 1465 | extent_thresh, newer_than, do_compress); |
| 1466 | if (ret < 0) |
| 1467 | break; |
| 1468 | *sectors_defragged += range_len; |
| 1469 | } |
| 1470 | out: |
| 1471 | list_for_each_entry_safe(entry, tmp, &target_list, list) { |
| 1472 | list_del_init(&entry->list); |
| 1473 | kfree(entry); |
| 1474 | } |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
Qu Wenruo | 1ccc2e8 | 2021-08-06 16:12:32 +0800 | [diff] [blame] | 1478 | /* |
| 1479 | * Entry point to file defragmentation. |
| 1480 | * |
| 1481 | * @inode: inode to be defragged |
| 1482 | * @ra: readahead state (can be NUL) |
| 1483 | * @range: defrag options including range and flags |
| 1484 | * @newer_than: minimum transid to defrag |
| 1485 | * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode |
| 1486 | * will be defragged. |
| 1487 | */ |
| 1488 | int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra, |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1489 | struct btrfs_ioctl_defrag_range_args *range, |
| 1490 | u64 newer_than, unsigned long max_to_defrag) |
| 1491 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1492 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1493 | unsigned long sectors_defragged = 0; |
Li Zefan | 151a31b | 2011-09-02 15:56:39 +0800 | [diff] [blame] | 1494 | u64 isize = i_size_read(inode); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1495 | u64 cur; |
| 1496 | u64 last_byte; |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1497 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
Qu Wenruo | 1ccc2e8 | 2021-08-06 16:12:32 +0800 | [diff] [blame] | 1498 | bool ra_allocated = false; |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1499 | int compress_type = BTRFS_COMPRESS_ZLIB; |
| 1500 | int ret = 0; |
| 1501 | u32 extent_thresh = range->extent_thresh; |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1502 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1503 | if (isize == 0) |
| 1504 | return 0; |
| 1505 | |
| 1506 | if (range->start >= isize) |
| 1507 | return -EINVAL; |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1508 | |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1509 | if (do_compress) { |
Chengguang Xu | ce96b7f | 2019-10-10 15:59:57 +0800 | [diff] [blame] | 1510 | if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES) |
Li Zefan | 1a419d8 | 2010-10-25 15:12:50 +0800 | [diff] [blame] | 1511 | return -EINVAL; |
| 1512 | if (range->compress_type) |
| 1513 | compress_type = range->compress_type; |
| 1514 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1515 | |
Liu Bo | 0abd5b1 | 2013-04-16 09:20:28 +0000 | [diff] [blame] | 1516 | if (extent_thresh == 0) |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1517 | extent_thresh = SZ_256K; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1518 | |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1519 | if (range->start + range->len > range->start) { |
| 1520 | /* Got a specific range */ |
Filipe Manana | 6b34cd8 | 2022-01-17 16:28:29 +0000 | [diff] [blame^] | 1521 | last_byte = min(isize, range->start + range->len); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1522 | } else { |
| 1523 | /* Defrag until file end */ |
Filipe Manana | 6b34cd8 | 2022-01-17 16:28:29 +0000 | [diff] [blame^] | 1524 | last_byte = isize; |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Filipe Manana | 6b34cd8 | 2022-01-17 16:28:29 +0000 | [diff] [blame^] | 1527 | /* Align the range */ |
| 1528 | cur = round_down(range->start, fs_info->sectorsize); |
| 1529 | last_byte = round_up(last_byte, fs_info->sectorsize) - 1; |
| 1530 | |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1531 | /* |
Qu Wenruo | 1ccc2e8 | 2021-08-06 16:12:32 +0800 | [diff] [blame] | 1532 | * If we were not given a ra, allocate a readahead context. As |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1533 | * readahead is just an optimization, defrag will work without it so |
| 1534 | * we don't error out. |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1535 | */ |
Qu Wenruo | 1ccc2e8 | 2021-08-06 16:12:32 +0800 | [diff] [blame] | 1536 | if (!ra) { |
| 1537 | ra_allocated = true; |
David Sterba | 63e727e | 2017-06-22 03:13:02 +0200 | [diff] [blame] | 1538 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
David Sterba | 0a52d10 | 2017-06-22 03:22:58 +0200 | [diff] [blame] | 1539 | if (ra) |
| 1540 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1543 | while (cur < last_byte) { |
| 1544 | u64 cluster_end; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 1545 | |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1546 | /* The cluster size 256K should always be page aligned */ |
| 1547 | BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE)); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 1548 | |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1549 | /* We want the cluster end at page boundary when possible */ |
| 1550 | cluster_end = (((cur >> PAGE_SHIFT) + |
| 1551 | (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1; |
| 1552 | cluster_end = min(cluster_end, last_byte); |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1553 | |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1554 | btrfs_inode_lock(inode, 0); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 1555 | if (IS_SWAPFILE(inode)) { |
| 1556 | ret = -ETXTBSY; |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1557 | btrfs_inode_unlock(inode, 0); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1558 | break; |
Liu Bo | ecb8bea | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 1559 | } |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1560 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) { |
| 1561 | btrfs_inode_unlock(inode, 0); |
| 1562 | break; |
| 1563 | } |
| 1564 | if (do_compress) |
| 1565 | BTRFS_I(inode)->defrag_compress = compress_type; |
| 1566 | ret = defrag_one_cluster(BTRFS_I(inode), ra, cur, |
| 1567 | cluster_end + 1 - cur, extent_thresh, |
| 1568 | newer_than, do_compress, |
| 1569 | §ors_defragged, max_to_defrag); |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1570 | btrfs_inode_unlock(inode, 0); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1571 | if (ret < 0) |
| 1572 | break; |
| 1573 | cur = cluster_end + 1; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1574 | } |
| 1575 | |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1576 | if (ra_allocated) |
| 1577 | kfree(ra); |
| 1578 | if (sectors_defragged) { |
| 1579 | /* |
| 1580 | * We have defragged some sectors, for compression case they |
| 1581 | * need to be written back immediately. |
| 1582 | */ |
| 1583 | if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) { |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1584 | filemap_flush(inode->i_mapping); |
Qu Wenruo | 7b50803 | 2021-05-27 20:33:22 +0800 | [diff] [blame] | 1585 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
| 1586 | &BTRFS_I(inode)->runtime_flags)) |
| 1587 | filemap_flush(inode->i_mapping); |
| 1588 | } |
| 1589 | if (range->compress_type == BTRFS_COMPRESS_LZO) |
| 1590 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
| 1591 | else if (range->compress_type == BTRFS_COMPRESS_ZSTD) |
| 1592 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); |
| 1593 | ret = sectors_defragged; |
Filipe Manana | dec8ef9 | 2014-03-01 10:55:54 +0000 | [diff] [blame] | 1594 | } |
David Sterba | 1e2ef46 | 2017-07-17 20:01:59 +0200 | [diff] [blame] | 1595 | if (do_compress) { |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1596 | btrfs_inode_lock(inode, 0); |
David Sterba | eec63c6 | 2017-07-17 19:41:31 +0200 | [diff] [blame] | 1597 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 1598 | btrfs_inode_unlock(inode, 0); |
Filipe David Borba Manana | 633085c | 2013-08-16 15:23:33 +0100 | [diff] [blame] | 1599 | } |
Chris Mason | 940100a | 2010-03-10 10:52:59 -0500 | [diff] [blame] | 1600 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1601 | } |
| 1602 | |
David Sterba | 17aaa43 | 2021-05-14 21:32:44 +0200 | [diff] [blame] | 1603 | /* |
| 1604 | * Try to start exclusive operation @type or cancel it if it's running. |
| 1605 | * |
| 1606 | * Return: |
| 1607 | * 0 - normal mode, newly claimed op started |
| 1608 | * >0 - normal mode, something else is running, |
| 1609 | * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space |
| 1610 | * ECANCELED - cancel mode, successful cancel |
| 1611 | * ENOTCONN - cancel mode, operation not running anymore |
| 1612 | */ |
| 1613 | static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info, |
| 1614 | enum btrfs_exclusive_operation type, bool cancel) |
| 1615 | { |
| 1616 | if (!cancel) { |
| 1617 | /* Start normal op */ |
| 1618 | if (!btrfs_exclop_start(fs_info, type)) |
| 1619 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 1620 | /* Exclusive operation is now claimed */ |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | /* Cancel running op */ |
| 1625 | if (btrfs_exclop_start_try_lock(fs_info, type)) { |
| 1626 | /* |
| 1627 | * This blocks any exclop finish from setting it to NONE, so we |
| 1628 | * request cancellation. Either it runs and we will wait for it, |
| 1629 | * or it has finished and no waiting will happen. |
| 1630 | */ |
| 1631 | atomic_inc(&fs_info->reloc_cancel_req); |
| 1632 | btrfs_exclop_start_unlock(fs_info); |
| 1633 | |
| 1634 | if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) |
| 1635 | wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING, |
| 1636 | TASK_INTERRUPTIBLE); |
| 1637 | |
| 1638 | return -ECANCELED; |
| 1639 | } |
| 1640 | |
| 1641 | /* Something else is running or none */ |
| 1642 | return -ENOTCONN; |
| 1643 | } |
| 1644 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1645 | static noinline int btrfs_ioctl_resize(struct file *file, |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 1646 | void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1647 | { |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 1648 | BTRFS_DEV_LOOKUP_ARGS(args); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1649 | struct inode *inode = file_inode(file); |
| 1650 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1651 | u64 new_size; |
| 1652 | u64 old_size; |
| 1653 | u64 devid = 1; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1654 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1655 | struct btrfs_ioctl_vol_args *vol_args; |
| 1656 | struct btrfs_trans_handle *trans; |
| 1657 | struct btrfs_device *device = NULL; |
| 1658 | char *sizestr; |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1659 | char *retptr; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1660 | char *devstr = NULL; |
| 1661 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1662 | int mod = 0; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1663 | bool cancel; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1664 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 1665 | if (!capable(CAP_SYS_ADMIN)) |
| 1666 | return -EPERM; |
| 1667 | |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 1668 | ret = mnt_want_write_file(file); |
| 1669 | if (ret) |
| 1670 | return ret; |
| 1671 | |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1672 | /* |
| 1673 | * Read the arguments before checking exclusivity to be able to |
| 1674 | * distinguish regular resize and cancel |
| 1675 | */ |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 1676 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1677 | if (IS_ERR(vol_args)) { |
| 1678 | ret = PTR_ERR(vol_args); |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1679 | goto out_drop; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1680 | } |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 1681 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1682 | sizestr = vol_args->name; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1683 | cancel = (strcmp("cancel", sizestr) == 0); |
| 1684 | ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel); |
| 1685 | if (ret) |
| 1686 | goto out_free; |
| 1687 | /* Exclusive operation is now claimed */ |
| 1688 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1689 | devstr = strchr(sizestr, ':'); |
| 1690 | if (devstr) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1691 | sizestr = devstr + 1; |
| 1692 | *devstr = '\0'; |
| 1693 | devstr = vol_args->name; |
ZhangZhen | 58dfae6 | 2014-05-13 16:36:08 +0800 | [diff] [blame] | 1694 | ret = kstrtoull(devstr, 10, &devid); |
| 1695 | if (ret) |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1696 | goto out_finish; |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1697 | if (!devid) { |
| 1698 | ret = -EINVAL; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1699 | goto out_finish; |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1700 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1701 | btrfs_info(fs_info, "resizing devid %llu", devid); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1702 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1703 | |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 1704 | args.devid = devid; |
| 1705 | device = btrfs_find_device(fs_info->fs_devices, &args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1706 | if (!device) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1707 | btrfs_info(fs_info, "resizer unable to find device %llu", |
| 1708 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1709 | ret = -ENODEV; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1710 | goto out_finish; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1711 | } |
Miao Xie | dba60f3 | 2012-12-21 09:19:51 +0000 | [diff] [blame] | 1712 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1713 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1714 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1715 | "resizer unable to apply on readonly device %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1716 | devid); |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1717 | ret = -EPERM; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1718 | goto out_finish; |
Liu Bo | 4e42ae1 | 2012-06-14 02:23:19 -0600 | [diff] [blame] | 1719 | } |
| 1720 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1721 | if (!strcmp(sizestr, "max")) |
Christoph Hellwig | cda00eb | 2021-10-18 12:11:12 +0200 | [diff] [blame] | 1722 | new_size = bdev_nr_bytes(device->bdev); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1723 | else { |
| 1724 | if (sizestr[0] == '-') { |
| 1725 | mod = -1; |
| 1726 | sizestr++; |
| 1727 | } else if (sizestr[0] == '+') { |
| 1728 | mod = 1; |
| 1729 | sizestr++; |
| 1730 | } |
Gui Hecheng | 9a40f12 | 2014-03-31 18:03:25 +0800 | [diff] [blame] | 1731 | new_size = memparse(sizestr, &retptr); |
| 1732 | if (*retptr != '\0' || new_size == 0) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1733 | ret = -EINVAL; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1734 | goto out_finish; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1738 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | dfd7982 | 2012-12-21 09:21:30 +0000 | [diff] [blame] | 1739 | ret = -EPERM; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1740 | goto out_finish; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1743 | old_size = btrfs_device_get_total_bytes(device); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1744 | |
| 1745 | if (mod < 0) { |
| 1746 | if (new_size > old_size) { |
| 1747 | ret = -EINVAL; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1748 | goto out_finish; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1749 | } |
| 1750 | new_size = old_size - new_size; |
| 1751 | } else if (mod > 0) { |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1752 | if (new_size > ULLONG_MAX - old_size) { |
Gui Hecheng | 902c68a | 2014-05-29 09:19:58 +0800 | [diff] [blame] | 1753 | ret = -ERANGE; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1754 | goto out_finish; |
Wenliang Fan | eb8052e | 2013-12-20 15:28:56 +0800 | [diff] [blame] | 1755 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1756 | new_size = old_size + new_size; |
| 1757 | } |
| 1758 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1759 | if (new_size < SZ_256M) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1760 | ret = -EINVAL; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1761 | goto out_finish; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1762 | } |
Christoph Hellwig | cda00eb | 2021-10-18 12:11:12 +0200 | [diff] [blame] | 1763 | if (new_size > bdev_nr_bytes(device->bdev)) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1764 | ret = -EFBIG; |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1765 | goto out_finish; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1766 | } |
| 1767 | |
Nikolay Borisov | 47f08b9 | 2017-07-18 15:39:08 +0300 | [diff] [blame] | 1768 | new_size = round_down(new_size, fs_info->sectorsize); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1769 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1770 | if (new_size > old_size) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1771 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1772 | if (IS_ERR(trans)) { |
| 1773 | ret = PTR_ERR(trans); |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1774 | goto out_finish; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1775 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1776 | ret = btrfs_grow_device(trans, device, new_size); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1777 | btrfs_commit_transaction(trans); |
Mike Fleetwood | ece7d20 | 2011-11-18 18:55:01 +0000 | [diff] [blame] | 1778 | } else if (new_size < old_size) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1779 | ret = btrfs_shrink_device(device, new_size); |
jeff.liu | 0253f40 | 2012-10-27 12:06:39 +0000 | [diff] [blame] | 1780 | } /* equal, nothing need to do */ |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1781 | |
Marcos Paulo de Souza | faf8f7b | 2020-02-11 10:55:26 -0300 | [diff] [blame] | 1782 | if (ret == 0 && new_size != old_size) |
| 1783 | btrfs_info_in_rcu(fs_info, |
| 1784 | "resize device %s (devid %llu) from %llu to %llu", |
| 1785 | rcu_str_deref(device->name), device->devid, |
| 1786 | old_size, new_size); |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1787 | out_finish: |
| 1788 | btrfs_exclop_finish(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 1789 | out_free: |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1790 | kfree(vol_args); |
David Sterba | bb059a3 | 2021-05-18 21:12:33 +0200 | [diff] [blame] | 1791 | out_drop: |
Ilya Dryomov | 18f39c4 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 1792 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1793 | return ret; |
| 1794 | } |
| 1795 | |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1796 | static noinline int __btrfs_ioctl_snap_create(struct file *file, |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1797 | struct user_namespace *mnt_userns, |
David Sterba | 52f75f4 | 2017-02-14 18:33:53 +0100 | [diff] [blame] | 1798 | const char *name, unsigned long fd, int subvol, |
Nikolay Borisov | 5d54c67 | 2020-03-13 17:23:19 +0200 | [diff] [blame] | 1799 | bool readonly, |
Miao Xie | 8696c53 | 2013-02-07 06:02:44 +0000 | [diff] [blame] | 1800 | struct btrfs_qgroup_inherit *inherit) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1801 | { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1802 | int namelen; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1803 | int ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1804 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1805 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1806 | return -ENOTDIR; |
| 1807 | |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1808 | ret = mnt_want_write_file(file); |
| 1809 | if (ret) |
| 1810 | goto out; |
| 1811 | |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1812 | namelen = strlen(name); |
| 1813 | if (strchr(name, '/')) { |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1814 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1815 | goto out_drop_write; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1818 | if (name[0] == '.' && |
| 1819 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
| 1820 | ret = -EEXIST; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1821 | goto out_drop_write; |
Chris Mason | 16780ca | 2012-02-20 22:14:55 -0500 | [diff] [blame] | 1822 | } |
| 1823 | |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1824 | if (subvol) { |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1825 | ret = btrfs_mksubvol(&file->f_path, mnt_userns, name, |
| 1826 | namelen, NULL, readonly, inherit); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1827 | } else { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1828 | struct fd src = fdget(fd); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1829 | struct inode *src_inode; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1830 | if (!src.file) { |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1831 | ret = -EINVAL; |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1832 | goto out_drop_write; |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1833 | } |
| 1834 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1835 | src_inode = file_inode(src.file); |
| 1836 | if (src_inode->i_sb != file_inode(file)->i_sb) { |
Josef Bacik | c79b471 | 2016-03-25 10:02:41 -0400 | [diff] [blame] | 1837 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1838 | "Snapshot src from another FS"); |
Kusanagi Kouichi | 23ad5b1 | 2014-01-30 16:32:02 +0900 | [diff] [blame] | 1839 | ret = -EXDEV; |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1840 | } else if (!inode_owner_or_capable(mnt_userns, src_inode)) { |
David Sterba | d024206 | 2014-01-15 18:15:52 +0100 | [diff] [blame] | 1841 | /* |
| 1842 | * Subvolume creation is not restricted, but snapshots |
| 1843 | * are limited to own subvolumes only |
| 1844 | */ |
| 1845 | ret = -EPERM; |
Al Viro | ecd1881 | 2012-08-26 21:20:24 -0400 | [diff] [blame] | 1846 | } else { |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1847 | ret = btrfs_mksnapshot(&file->f_path, mnt_userns, |
| 1848 | name, namelen, |
| 1849 | BTRFS_I(src_inode)->root, |
| 1850 | readonly, inherit); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 1851 | } |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1852 | fdput(src); |
Christoph Hellwig | cb8e709 | 2008-10-09 13:39:39 -0400 | [diff] [blame] | 1853 | } |
Liu Bo | a874a63 | 2012-06-29 03:58:46 -0600 | [diff] [blame] | 1854 | out_drop_write: |
| 1855 | mnt_drop_write_file(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1856 | out: |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1857 | return ret; |
| 1858 | } |
| 1859 | |
| 1860 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1861 | void __user *arg, int subvol) |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1862 | { |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1863 | struct btrfs_ioctl_vol_args *vol_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1864 | int ret; |
| 1865 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1866 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1867 | return -ENOTDIR; |
| 1868 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1869 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1870 | if (IS_ERR(vol_args)) |
| 1871 | return PTR_ERR(vol_args); |
| 1872 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1873 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1874 | ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), |
| 1875 | vol_args->name, vol_args->fd, subvol, |
| 1876 | false, NULL); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1877 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1878 | kfree(vol_args); |
| 1879 | return ret; |
| 1880 | } |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1881 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1882 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
| 1883 | void __user *arg, int subvol) |
| 1884 | { |
| 1885 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
| 1886 | int ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1887 | bool readonly = false; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1888 | struct btrfs_qgroup_inherit *inherit = NULL; |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 1889 | |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 1890 | if (!S_ISDIR(file_inode(file)->i_mode)) |
| 1891 | return -ENOTDIR; |
| 1892 | |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1893 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 1894 | if (IS_ERR(vol_args)) |
| 1895 | return PTR_ERR(vol_args); |
| 1896 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Sage Weil | 75eaa0e | 2010-12-10 00:36:28 +0000 | [diff] [blame] | 1897 | |
David Sterba | 673990d | 2020-02-21 13:24:37 +0100 | [diff] [blame] | 1898 | if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) { |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1899 | ret = -EOPNOTSUPP; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1900 | goto free_args; |
Sage Weil | 72fd032 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 1901 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1902 | |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 1903 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
| 1904 | readonly = true; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1905 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
Dan Carpenter | 5011c5a | 2021-02-17 09:04:34 +0300 | [diff] [blame] | 1906 | u64 nums; |
| 1907 | |
| 1908 | if (vol_args->size < sizeof(*inherit) || |
| 1909 | vol_args->size > PAGE_SIZE) { |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1910 | ret = -EINVAL; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1911 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1912 | } |
| 1913 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); |
| 1914 | if (IS_ERR(inherit)) { |
| 1915 | ret = PTR_ERR(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1916 | goto free_args; |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1917 | } |
Dan Carpenter | 5011c5a | 2021-02-17 09:04:34 +0300 | [diff] [blame] | 1918 | |
| 1919 | if (inherit->num_qgroups > PAGE_SIZE || |
| 1920 | inherit->num_ref_copies > PAGE_SIZE || |
| 1921 | inherit->num_excl_copies > PAGE_SIZE) { |
| 1922 | ret = -EINVAL; |
| 1923 | goto free_inherit; |
| 1924 | } |
| 1925 | |
| 1926 | nums = inherit->num_qgroups + 2 * inherit->num_ref_copies + |
| 1927 | 2 * inherit->num_excl_copies; |
| 1928 | if (vol_args->size != struct_size(inherit, qgroups, nums)) { |
| 1929 | ret = -EINVAL; |
| 1930 | goto free_inherit; |
| 1931 | } |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1932 | } |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 1933 | |
Christian Brauner | 4d4340c | 2021-07-27 12:48:52 +0200 | [diff] [blame] | 1934 | ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file), |
| 1935 | vol_args->name, vol_args->fd, subvol, |
| 1936 | readonly, inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1937 | if (ret) |
| 1938 | goto free_inherit; |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1939 | free_inherit: |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 1940 | kfree(inherit); |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 1941 | free_args: |
| 1942 | kfree(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 1943 | return ret; |
| 1944 | } |
| 1945 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1946 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
| 1947 | void __user *arg) |
| 1948 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1949 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1950 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1951 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1952 | int ret = 0; |
| 1953 | u64 flags = 0; |
| 1954 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1955 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1956 | return -EINVAL; |
| 1957 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1958 | down_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1959 | if (btrfs_root_readonly(root)) |
| 1960 | flags |= BTRFS_SUBVOL_RDONLY; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1961 | up_read(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1962 | |
| 1963 | if (copy_to_user(arg, &flags, sizeof(flags))) |
| 1964 | ret = -EFAULT; |
| 1965 | |
| 1966 | return ret; |
| 1967 | } |
| 1968 | |
| 1969 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
| 1970 | void __user *arg) |
| 1971 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1972 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1973 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1974 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1975 | struct btrfs_trans_handle *trans; |
| 1976 | u64 root_flags; |
| 1977 | u64 flags; |
| 1978 | int ret = 0; |
| 1979 | |
Christian Brauner | 39e1674 | 2021-07-27 12:48:56 +0200 | [diff] [blame] | 1980 | if (!inode_owner_or_capable(file_mnt_user_ns(file), inode)) |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 1981 | return -EPERM; |
| 1982 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1983 | ret = mnt_want_write_file(file); |
| 1984 | if (ret) |
| 1985 | goto out; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1986 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 1987 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1988 | ret = -EINVAL; |
| 1989 | goto out_drop_write; |
| 1990 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1991 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1992 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
| 1993 | ret = -EFAULT; |
| 1994 | goto out_drop_write; |
| 1995 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 1996 | |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 1997 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
| 1998 | ret = -EOPNOTSUPP; |
| 1999 | goto out_drop_write; |
| 2000 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2001 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2002 | down_write(&fs_info->subvol_sem); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2003 | |
| 2004 | /* nothing to do */ |
| 2005 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 2006 | goto out_drop_sem; |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2007 | |
| 2008 | root_flags = btrfs_root_flags(&root->root_item); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 2009 | if (flags & BTRFS_SUBVOL_RDONLY) { |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2010 | btrfs_set_root_flags(&root->root_item, |
| 2011 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 2012 | } else { |
| 2013 | /* |
| 2014 | * Block RO -> RW transition if this subvolume is involved in |
| 2015 | * send |
| 2016 | */ |
| 2017 | spin_lock(&root->root_item_lock); |
| 2018 | if (root->send_in_progress == 0) { |
| 2019 | btrfs_set_root_flags(&root->root_item, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2020 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 2021 | spin_unlock(&root->root_item_lock); |
| 2022 | } else { |
| 2023 | spin_unlock(&root->root_item_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2024 | btrfs_warn(fs_info, |
| 2025 | "Attempt to set subvolume %llu read-write during send", |
| 2026 | root->root_key.objectid); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 2027 | ret = -EPERM; |
| 2028 | goto out_drop_sem; |
| 2029 | } |
| 2030 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2031 | |
| 2032 | trans = btrfs_start_transaction(root, 1); |
| 2033 | if (IS_ERR(trans)) { |
| 2034 | ret = PTR_ERR(trans); |
| 2035 | goto out_reset; |
| 2036 | } |
| 2037 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2038 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2039 | &root->root_key, &root->root_item); |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 2040 | if (ret < 0) { |
| 2041 | btrfs_end_transaction(trans); |
| 2042 | goto out_reset; |
| 2043 | } |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2044 | |
Nikolay Borisov | 9417ebc | 2017-09-28 10:53:17 +0300 | [diff] [blame] | 2045 | ret = btrfs_commit_transaction(trans); |
| 2046 | |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2047 | out_reset: |
| 2048 | if (ret) |
| 2049 | btrfs_set_root_flags(&root->root_item, root_flags); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 2050 | out_drop_sem: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2051 | up_write(&fs_info->subvol_sem); |
Liu Bo | b9ca066 | 2012-06-29 03:58:49 -0600 | [diff] [blame] | 2052 | out_drop_write: |
| 2053 | mnt_drop_write_file(file); |
| 2054 | out: |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 2055 | return ret; |
| 2056 | } |
| 2057 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2058 | static noinline int key_in_sk(struct btrfs_key *key, |
| 2059 | struct btrfs_ioctl_search_key *sk) |
| 2060 | { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2061 | struct btrfs_key test; |
| 2062 | int ret; |
| 2063 | |
| 2064 | test.objectid = sk->min_objectid; |
| 2065 | test.type = sk->min_type; |
| 2066 | test.offset = sk->min_offset; |
| 2067 | |
| 2068 | ret = btrfs_comp_cpu_keys(key, &test); |
| 2069 | if (ret < 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2070 | return 0; |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2071 | |
| 2072 | test.objectid = sk->max_objectid; |
| 2073 | test.type = sk->max_type; |
| 2074 | test.offset = sk->max_offset; |
| 2075 | |
| 2076 | ret = btrfs_comp_cpu_keys(key, &test); |
| 2077 | if (ret > 0) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2078 | return 0; |
| 2079 | return 1; |
| 2080 | } |
| 2081 | |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2082 | static noinline int copy_to_sk(struct btrfs_path *path, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2083 | struct btrfs_key *key, |
| 2084 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2085 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2086 | char __user *ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2087 | unsigned long *sk_offset, |
| 2088 | int *num_found) |
| 2089 | { |
| 2090 | u64 found_transid; |
| 2091 | struct extent_buffer *leaf; |
| 2092 | struct btrfs_ioctl_search_header sh; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2093 | struct btrfs_key test; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2094 | unsigned long item_off; |
| 2095 | unsigned long item_len; |
| 2096 | int nritems; |
| 2097 | int i; |
| 2098 | int slot; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2099 | int ret = 0; |
| 2100 | |
| 2101 | leaf = path->nodes[0]; |
| 2102 | slot = path->slots[0]; |
| 2103 | nritems = btrfs_header_nritems(leaf); |
| 2104 | |
| 2105 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
| 2106 | i = nritems; |
| 2107 | goto advance_key; |
| 2108 | } |
| 2109 | found_transid = btrfs_header_generation(leaf); |
| 2110 | |
| 2111 | for (i = slot; i < nritems; i++) { |
| 2112 | item_off = btrfs_item_ptr_offset(leaf, i); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 2113 | item_len = btrfs_item_size(leaf, i); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2114 | |
Gabriel de Perthuis | 03b71c6 | 2013-05-06 17:40:18 +0000 | [diff] [blame] | 2115 | btrfs_item_key_to_cpu(leaf, key, i); |
| 2116 | if (!key_in_sk(key, sk)) |
| 2117 | continue; |
| 2118 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2119 | if (sizeof(sh) + item_len > *buf_size) { |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2120 | if (*num_found) { |
| 2121 | ret = 1; |
| 2122 | goto out; |
| 2123 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2124 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2125 | /* |
| 2126 | * return one empty item back for v1, which does not |
| 2127 | * handle -EOVERFLOW |
| 2128 | */ |
| 2129 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2130 | *buf_size = sizeof(sh) + item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2131 | item_len = 0; |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2132 | ret = -EOVERFLOW; |
| 2133 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2134 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2135 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2136 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2137 | goto out; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2138 | } |
| 2139 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2140 | sh.objectid = key->objectid; |
| 2141 | sh.offset = key->offset; |
| 2142 | sh.type = key->type; |
| 2143 | sh.len = item_len; |
| 2144 | sh.transid = found_transid; |
| 2145 | |
Josef Bacik | a48b73e | 2020-08-10 11:42:27 -0400 | [diff] [blame] | 2146 | /* |
| 2147 | * Copy search result header. If we fault then loop again so we |
| 2148 | * can fault in the pages and -EFAULT there if there's a |
| 2149 | * problem. Otherwise we'll fault and then copy the buffer in |
| 2150 | * properly this next time through |
| 2151 | */ |
| 2152 | if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) { |
| 2153 | ret = 0; |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2154 | goto out; |
| 2155 | } |
| 2156 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2157 | *sk_offset += sizeof(sh); |
| 2158 | |
| 2159 | if (item_len) { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2160 | char __user *up = ubuf + *sk_offset; |
Josef Bacik | a48b73e | 2020-08-10 11:42:27 -0400 | [diff] [blame] | 2161 | /* |
| 2162 | * Copy the item, same behavior as above, but reset the |
| 2163 | * * sk_offset so we copy the full thing again. |
| 2164 | */ |
| 2165 | if (read_extent_buffer_to_user_nofault(leaf, up, |
| 2166 | item_off, item_len)) { |
| 2167 | ret = 0; |
| 2168 | *sk_offset -= sizeof(sh); |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2169 | goto out; |
| 2170 | } |
| 2171 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2172 | *sk_offset += item_len; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2173 | } |
Hugo Mills | e215686 | 2011-05-14 17:43:41 +0000 | [diff] [blame] | 2174 | (*num_found)++; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2175 | |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2176 | if (ret) /* -EOVERFLOW from above */ |
| 2177 | goto out; |
| 2178 | |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2179 | if (*num_found >= sk->nr_items) { |
| 2180 | ret = 1; |
| 2181 | goto out; |
| 2182 | } |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2183 | } |
| 2184 | advance_key: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2185 | ret = 0; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2186 | test.objectid = sk->max_objectid; |
| 2187 | test.type = sk->max_type; |
| 2188 | test.offset = sk->max_offset; |
| 2189 | if (btrfs_comp_cpu_keys(key, &test) >= 0) |
| 2190 | ret = 1; |
| 2191 | else if (key->offset < (u64)-1) |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2192 | key->offset++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2193 | else if (key->type < (u8)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2194 | key->offset = 0; |
| 2195 | key->type++; |
Naohiro Aota | dd81d45 | 2015-06-30 11:25:43 +0900 | [diff] [blame] | 2196 | } else if (key->objectid < (u64)-1) { |
Chris Mason | abc6e13 | 2010-03-18 12:10:08 -0400 | [diff] [blame] | 2197 | key->offset = 0; |
| 2198 | key->type = 0; |
| 2199 | key->objectid++; |
| 2200 | } else |
| 2201 | ret = 1; |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2202 | out: |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2203 | /* |
| 2204 | * 0: all items from this leaf copied, continue with next |
| 2205 | * 1: * more items can be copied, but unused buffer is too small |
| 2206 | * * all items were found |
| 2207 | * Either way, it will stops the loop which iterates to the next |
| 2208 | * leaf |
| 2209 | * -EOVERFLOW: item was to large for buffer |
| 2210 | * -EFAULT: could not copy extent buffer back to userspace |
| 2211 | */ |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2212 | return ret; |
| 2213 | } |
| 2214 | |
| 2215 | static noinline int search_ioctl(struct inode *inode, |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2216 | struct btrfs_ioctl_search_key *sk, |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2217 | size_t *buf_size, |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2218 | char __user *ubuf) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2219 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2220 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2221 | struct btrfs_root *root; |
| 2222 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2223 | struct btrfs_path *path; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2224 | int ret; |
| 2225 | int num_found = 0; |
| 2226 | unsigned long sk_offset = 0; |
| 2227 | |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2228 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
| 2229 | *buf_size = sizeof(struct btrfs_ioctl_search_header); |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2230 | return -EOVERFLOW; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2231 | } |
Gerhard Heift | 1254444 | 2014-01-30 16:23:58 +0100 | [diff] [blame] | 2232 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2233 | path = btrfs_alloc_path(); |
| 2234 | if (!path) |
| 2235 | return -ENOMEM; |
| 2236 | |
| 2237 | if (sk->tree_id == 0) { |
| 2238 | /* search the root of the inode that was passed */ |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2239 | root = btrfs_grab_root(BTRFS_I(inode)->root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2240 | } else { |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2241 | root = btrfs_get_fs_root(info, sk->tree_id, true); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2242 | if (IS_ERR(root)) { |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2243 | btrfs_free_path(path); |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2244 | return PTR_ERR(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | key.objectid = sk->min_objectid; |
| 2249 | key.type = sk->min_type; |
| 2250 | key.offset = sk->min_offset; |
| 2251 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2252 | while (1) { |
Andreas Gruenbacher | bb523b4 | 2021-08-02 13:44:20 +0200 | [diff] [blame] | 2253 | ret = -EFAULT; |
| 2254 | if (fault_in_writeable(ubuf + sk_offset, *buf_size - sk_offset)) |
Josef Bacik | a48b73e | 2020-08-10 11:42:27 -0400 | [diff] [blame] | 2255 | break; |
| 2256 | |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 2257 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2258 | if (ret != 0) { |
| 2259 | if (ret > 0) |
| 2260 | ret = 0; |
| 2261 | goto err; |
| 2262 | } |
Jeff Mahoney | df39756 | 2016-06-21 20:18:21 -0400 | [diff] [blame] | 2263 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2264 | &sk_offset, &num_found); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2265 | btrfs_release_path(path); |
Gerhard Heift | 25c9bc2 | 2014-01-30 16:23:57 +0100 | [diff] [blame] | 2266 | if (ret) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2267 | break; |
| 2268 | |
| 2269 | } |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2270 | if (ret > 0) |
| 2271 | ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2272 | err: |
| 2273 | sk->nr_items = num_found; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2274 | btrfs_put_root(root); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2275 | btrfs_free_path(path); |
| 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
| 2280 | void __user *argp) |
| 2281 | { |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2282 | struct btrfs_ioctl_search_args __user *uargs; |
| 2283 | struct btrfs_ioctl_search_key sk; |
Gerhard Heift | 9b6e817 | 2014-01-30 16:24:00 +0100 | [diff] [blame] | 2284 | struct inode *inode; |
| 2285 | int ret; |
| 2286 | size_t buf_size; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2287 | |
| 2288 | if (!capable(CAP_SYS_ADMIN)) |
| 2289 | return -EPERM; |
| 2290 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2291 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2292 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2293 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) |
| 2294 | return -EFAULT; |
| 2295 | |
| 2296 | buf_size = sizeof(uargs->buf); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2297 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2298 | inode = file_inode(file); |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2299 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
Gerhard Heift | 8f5f617 | 2014-01-30 16:23:59 +0100 | [diff] [blame] | 2300 | |
| 2301 | /* |
| 2302 | * In the origin implementation an overflow is handled by returning a |
| 2303 | * search header with a len of zero, so reset ret. |
| 2304 | */ |
| 2305 | if (ret == -EOVERFLOW) |
| 2306 | ret = 0; |
| 2307 | |
Gerhard Heift | ba346b3 | 2014-01-30 16:24:02 +0100 | [diff] [blame] | 2308 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2309 | ret = -EFAULT; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2310 | return ret; |
| 2311 | } |
| 2312 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2313 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
| 2314 | void __user *argp) |
| 2315 | { |
| 2316 | struct btrfs_ioctl_search_args_v2 __user *uarg; |
| 2317 | struct btrfs_ioctl_search_args_v2 args; |
| 2318 | struct inode *inode; |
| 2319 | int ret; |
| 2320 | size_t buf_size; |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 2321 | const size_t buf_limit = SZ_16M; |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2322 | |
| 2323 | if (!capable(CAP_SYS_ADMIN)) |
| 2324 | return -EPERM; |
| 2325 | |
| 2326 | /* copy search header and buffer size */ |
| 2327 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; |
| 2328 | if (copy_from_user(&args, uarg, sizeof(args))) |
| 2329 | return -EFAULT; |
| 2330 | |
| 2331 | buf_size = args.buf_size; |
| 2332 | |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2333 | /* limit result size to 16MB */ |
| 2334 | if (buf_size > buf_limit) |
| 2335 | buf_size = buf_limit; |
| 2336 | |
| 2337 | inode = file_inode(file); |
| 2338 | ret = search_ioctl(inode, &args.key, &buf_size, |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 2339 | (char __user *)(&uarg->buf[0])); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 2340 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
| 2341 | ret = -EFAULT; |
| 2342 | else if (ret == -EOVERFLOW && |
| 2343 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) |
| 2344 | ret = -EFAULT; |
| 2345 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2346 | return ret; |
| 2347 | } |
| 2348 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2349 | /* |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2350 | * Search INODE_REFs to identify path name of 'dirid' directory |
| 2351 | * in a 'tree_id' tree. and sets path name to 'name'. |
| 2352 | */ |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2353 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
| 2354 | u64 tree_id, u64 dirid, char *name) |
| 2355 | { |
| 2356 | struct btrfs_root *root; |
| 2357 | struct btrfs_key key; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2358 | char *ptr; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2359 | int ret = -1; |
| 2360 | int slot; |
| 2361 | int len; |
| 2362 | int total_len = 0; |
| 2363 | struct btrfs_inode_ref *iref; |
| 2364 | struct extent_buffer *l; |
| 2365 | struct btrfs_path *path; |
| 2366 | |
| 2367 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2368 | name[0]='\0'; |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | path = btrfs_alloc_path(); |
| 2373 | if (!path) |
| 2374 | return -ENOMEM; |
| 2375 | |
Nikolay Borisov | c8bcbfbd | 2017-12-01 11:19:42 +0200 | [diff] [blame] | 2376 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2377 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2378 | root = btrfs_get_fs_root(info, tree_id, true); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2379 | if (IS_ERR(root)) { |
Misono Tomohiro | ad1e3d5 | 2018-05-21 13:57:27 +0900 | [diff] [blame] | 2380 | ret = PTR_ERR(root); |
Josef Bacik | 8823401 | 2020-01-24 09:32:34 -0500 | [diff] [blame] | 2381 | root = NULL; |
| 2382 | goto out; |
| 2383 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2384 | |
| 2385 | key.objectid = dirid; |
| 2386 | key.type = BTRFS_INODE_REF_KEY; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2387 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2388 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2389 | while (1) { |
Marcos Paulo de Souza | 0ff40a9 | 2021-07-29 05:22:16 -0300 | [diff] [blame] | 2390 | ret = btrfs_search_backwards(root, &key, path); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2391 | if (ret < 0) |
| 2392 | goto out; |
Filipe David Borba Manana | 18674c6 | 2013-08-14 03:00:21 +0100 | [diff] [blame] | 2393 | else if (ret > 0) { |
Marcos Paulo de Souza | 0ff40a9 | 2021-07-29 05:22:16 -0300 | [diff] [blame] | 2394 | ret = -ENOENT; |
| 2395 | goto out; |
Filipe David Borba Manana | 18674c6 | 2013-08-14 03:00:21 +0100 | [diff] [blame] | 2396 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2397 | |
| 2398 | l = path->nodes[0]; |
| 2399 | slot = path->slots[0]; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2400 | |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2401 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
| 2402 | len = btrfs_inode_ref_name_len(l, iref); |
| 2403 | ptr -= len + 1; |
| 2404 | total_len += len + 1; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2405 | if (ptr < name) { |
| 2406 | ret = -ENAMETOOLONG; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2407 | goto out; |
Filipe David Borba Manana | a696cf3 | 2013-08-14 03:00:20 +0100 | [diff] [blame] | 2408 | } |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2409 | |
| 2410 | *(ptr + len) = '/'; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2411 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2412 | |
| 2413 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
| 2414 | break; |
| 2415 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2416 | btrfs_release_path(path); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2417 | key.objectid = key.offset; |
Chris Mason | 8ad6fca | 2010-03-18 12:23:10 -0400 | [diff] [blame] | 2418 | key.offset = (u64)-1; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2419 | dirid = key.objectid; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2420 | } |
Li Zefan | 77906a50 | 2011-07-14 03:16:00 +0000 | [diff] [blame] | 2421 | memmove(name, ptr, total_len); |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 2422 | name[total_len] = '\0'; |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2423 | ret = 0; |
| 2424 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2425 | btrfs_put_root(root); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2426 | btrfs_free_path(path); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2427 | return ret; |
| 2428 | } |
| 2429 | |
Christian Brauner | 6623d9a | 2021-07-27 12:48:57 +0200 | [diff] [blame] | 2430 | static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns, |
| 2431 | struct inode *inode, |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2432 | struct btrfs_ioctl_ino_lookup_user_args *args) |
| 2433 | { |
| 2434 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; |
| 2435 | struct super_block *sb = inode->i_sb; |
| 2436 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; |
| 2437 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2438 | u64 dirid = args->dirid; |
| 2439 | unsigned long item_off; |
| 2440 | unsigned long item_len; |
| 2441 | struct btrfs_inode_ref *iref; |
| 2442 | struct btrfs_root_ref *rref; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2443 | struct btrfs_root *root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2444 | struct btrfs_path *path; |
| 2445 | struct btrfs_key key, key2; |
| 2446 | struct extent_buffer *leaf; |
| 2447 | struct inode *temp_inode; |
| 2448 | char *ptr; |
| 2449 | int slot; |
| 2450 | int len; |
| 2451 | int total_len = 0; |
| 2452 | int ret; |
| 2453 | |
| 2454 | path = btrfs_alloc_path(); |
| 2455 | if (!path) |
| 2456 | return -ENOMEM; |
| 2457 | |
| 2458 | /* |
| 2459 | * If the bottom subvolume does not exist directly under upper_limit, |
| 2460 | * construct the path in from the bottom up. |
| 2461 | */ |
| 2462 | if (dirid != upper_limit.objectid) { |
| 2463 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; |
| 2464 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2465 | root = btrfs_get_fs_root(fs_info, treeid, true); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2466 | if (IS_ERR(root)) { |
| 2467 | ret = PTR_ERR(root); |
| 2468 | goto out; |
| 2469 | } |
| 2470 | |
| 2471 | key.objectid = dirid; |
| 2472 | key.type = BTRFS_INODE_REF_KEY; |
| 2473 | key.offset = (u64)-1; |
| 2474 | while (1) { |
Marcos Paulo de Souza | 0ff40a9 | 2021-07-29 05:22:16 -0300 | [diff] [blame] | 2475 | ret = btrfs_search_backwards(root, &key, path); |
| 2476 | if (ret < 0) |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2477 | goto out_put; |
Marcos Paulo de Souza | 0ff40a9 | 2021-07-29 05:22:16 -0300 | [diff] [blame] | 2478 | else if (ret > 0) { |
| 2479 | ret = -ENOENT; |
| 2480 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | leaf = path->nodes[0]; |
| 2484 | slot = path->slots[0]; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2485 | |
| 2486 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); |
| 2487 | len = btrfs_inode_ref_name_len(leaf, iref); |
| 2488 | ptr -= len + 1; |
| 2489 | total_len += len + 1; |
| 2490 | if (ptr < args->path) { |
| 2491 | ret = -ENAMETOOLONG; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2492 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | *(ptr + len) = '/'; |
| 2496 | read_extent_buffer(leaf, ptr, |
| 2497 | (unsigned long)(iref + 1), len); |
| 2498 | |
| 2499 | /* Check the read+exec permission of this directory */ |
| 2500 | ret = btrfs_previous_item(root, path, dirid, |
| 2501 | BTRFS_INODE_ITEM_KEY); |
| 2502 | if (ret < 0) { |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2503 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2504 | } else if (ret > 0) { |
| 2505 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2506 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | leaf = path->nodes[0]; |
| 2510 | slot = path->slots[0]; |
| 2511 | btrfs_item_key_to_cpu(leaf, &key2, slot); |
| 2512 | if (key2.objectid != dirid) { |
| 2513 | ret = -ENOENT; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2514 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2515 | } |
| 2516 | |
David Sterba | 0202e83 | 2020-05-15 19:35:59 +0200 | [diff] [blame] | 2517 | temp_inode = btrfs_iget(sb, key2.objectid, root); |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2518 | if (IS_ERR(temp_inode)) { |
| 2519 | ret = PTR_ERR(temp_inode); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2520 | goto out_put; |
Misono Tomohiro | 3ca57bd | 2018-06-04 16:41:07 +0900 | [diff] [blame] | 2521 | } |
Christian Brauner | 6623d9a | 2021-07-27 12:48:57 +0200 | [diff] [blame] | 2522 | ret = inode_permission(mnt_userns, temp_inode, |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 2523 | MAY_READ | MAY_EXEC); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2524 | iput(temp_inode); |
| 2525 | if (ret) { |
| 2526 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2527 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | if (key.offset == upper_limit.objectid) |
| 2531 | break; |
| 2532 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2533 | ret = -EACCES; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2534 | goto out_put; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | btrfs_release_path(path); |
| 2538 | key.objectid = key.offset; |
| 2539 | key.offset = (u64)-1; |
| 2540 | dirid = key.objectid; |
| 2541 | } |
| 2542 | |
| 2543 | memmove(args->path, ptr, total_len); |
| 2544 | args->path[total_len] = '\0'; |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2545 | btrfs_put_root(root); |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2546 | root = NULL; |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2547 | btrfs_release_path(path); |
| 2548 | } |
| 2549 | |
| 2550 | /* Get the bottom subvolume's name from ROOT_REF */ |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2551 | key.objectid = treeid; |
| 2552 | key.type = BTRFS_ROOT_REF_KEY; |
| 2553 | key.offset = args->treeid; |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2554 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2555 | if (ret < 0) { |
| 2556 | goto out; |
| 2557 | } else if (ret > 0) { |
| 2558 | ret = -ENOENT; |
| 2559 | goto out; |
| 2560 | } |
| 2561 | |
| 2562 | leaf = path->nodes[0]; |
| 2563 | slot = path->slots[0]; |
| 2564 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2565 | |
| 2566 | item_off = btrfs_item_ptr_offset(leaf, slot); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 2567 | item_len = btrfs_item_size(leaf, slot); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2568 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ |
| 2569 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2570 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { |
| 2571 | ret = -EINVAL; |
| 2572 | goto out; |
| 2573 | } |
| 2574 | |
| 2575 | /* Copy subvolume's name */ |
| 2576 | item_off += sizeof(struct btrfs_root_ref); |
| 2577 | item_len -= sizeof(struct btrfs_root_ref); |
| 2578 | read_extent_buffer(leaf, args->name, item_off, item_len); |
| 2579 | args->name[item_len] = 0; |
| 2580 | |
Josef Bacik | b8a49ae | 2020-01-24 09:32:35 -0500 | [diff] [blame] | 2581 | out_put: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2582 | btrfs_put_root(root); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2583 | out: |
| 2584 | btrfs_free_path(path); |
| 2585 | return ret; |
| 2586 | } |
| 2587 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2588 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
| 2589 | void __user *argp) |
| 2590 | { |
Bart Van Assche | bece2e8 | 2018-06-20 10:03:31 -0700 | [diff] [blame] | 2591 | struct btrfs_ioctl_ino_lookup_args *args; |
| 2592 | struct inode *inode; |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2593 | int ret = 0; |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2594 | |
Julia Lawall | 2354d08f | 2010-10-29 15:14:18 -0400 | [diff] [blame] | 2595 | args = memdup_user(argp, sizeof(*args)); |
| 2596 | if (IS_ERR(args)) |
| 2597 | return PTR_ERR(args); |
Dan Carpenter | c2b9692 | 2010-03-20 11:24:15 +0000 | [diff] [blame] | 2598 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2599 | inode = file_inode(file); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2600 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2601 | /* |
| 2602 | * Unprivileged query to obtain the containing subvolume root id. The |
| 2603 | * path is reset so it's consistent with btrfs_search_path_in_tree. |
| 2604 | */ |
Chris Mason | 1b53ac4 | 2010-03-18 12:17:05 -0400 | [diff] [blame] | 2605 | if (args->treeid == 0) |
| 2606 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
| 2607 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2608 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
| 2609 | args->name[0] = 0; |
| 2610 | goto out; |
| 2611 | } |
| 2612 | |
| 2613 | if (!capable(CAP_SYS_ADMIN)) { |
| 2614 | ret = -EPERM; |
| 2615 | goto out; |
| 2616 | } |
| 2617 | |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2618 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
| 2619 | args->treeid, args->objectid, |
| 2620 | args->name); |
| 2621 | |
David Sterba | 01b810b | 2015-05-12 19:14:49 +0200 | [diff] [blame] | 2622 | out: |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 2623 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2624 | ret = -EFAULT; |
| 2625 | |
| 2626 | kfree(args); |
TARUISI Hiroaki | 98d377a | 2009-11-18 05:42:14 +0000 | [diff] [blame] | 2627 | return ret; |
| 2628 | } |
| 2629 | |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2630 | /* |
| 2631 | * Version of ino_lookup ioctl (unprivileged) |
| 2632 | * |
| 2633 | * The main differences from ino_lookup ioctl are: |
| 2634 | * |
| 2635 | * 1. Read + Exec permission will be checked using inode_permission() during |
| 2636 | * path construction. -EACCES will be returned in case of failure. |
| 2637 | * 2. Path construction will be stopped at the inode number which corresponds |
| 2638 | * to the fd with which this ioctl is called. If constructed path does not |
| 2639 | * exist under fd's inode, -EACCES will be returned. |
| 2640 | * 3. The name of bottom subvolume is also searched and filled. |
| 2641 | */ |
| 2642 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) |
| 2643 | { |
| 2644 | struct btrfs_ioctl_ino_lookup_user_args *args; |
| 2645 | struct inode *inode; |
| 2646 | int ret; |
| 2647 | |
| 2648 | args = memdup_user(argp, sizeof(*args)); |
| 2649 | if (IS_ERR(args)) |
| 2650 | return PTR_ERR(args); |
| 2651 | |
| 2652 | inode = file_inode(file); |
| 2653 | |
| 2654 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && |
| 2655 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { |
| 2656 | /* |
| 2657 | * The subvolume does not exist under fd with which this is |
| 2658 | * called |
| 2659 | */ |
| 2660 | kfree(args); |
| 2661 | return -EACCES; |
| 2662 | } |
| 2663 | |
Christian Brauner | 6623d9a | 2021-07-27 12:48:57 +0200 | [diff] [blame] | 2664 | ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 2665 | |
| 2666 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
| 2667 | ret = -EFAULT; |
| 2668 | |
| 2669 | kfree(args); |
| 2670 | return ret; |
| 2671 | } |
| 2672 | |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2673 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
| 2674 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) |
| 2675 | { |
| 2676 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; |
| 2677 | struct btrfs_fs_info *fs_info; |
| 2678 | struct btrfs_root *root; |
| 2679 | struct btrfs_path *path; |
| 2680 | struct btrfs_key key; |
| 2681 | struct btrfs_root_item *root_item; |
| 2682 | struct btrfs_root_ref *rref; |
| 2683 | struct extent_buffer *leaf; |
| 2684 | unsigned long item_off; |
| 2685 | unsigned long item_len; |
| 2686 | struct inode *inode; |
| 2687 | int slot; |
| 2688 | int ret = 0; |
| 2689 | |
| 2690 | path = btrfs_alloc_path(); |
| 2691 | if (!path) |
| 2692 | return -ENOMEM; |
| 2693 | |
| 2694 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); |
| 2695 | if (!subvol_info) { |
| 2696 | btrfs_free_path(path); |
| 2697 | return -ENOMEM; |
| 2698 | } |
| 2699 | |
| 2700 | inode = file_inode(file); |
| 2701 | fs_info = BTRFS_I(inode)->root->fs_info; |
| 2702 | |
| 2703 | /* Get root_item of inode's subvolume */ |
| 2704 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 2705 | root = btrfs_get_fs_root(fs_info, key.objectid, true); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2706 | if (IS_ERR(root)) { |
| 2707 | ret = PTR_ERR(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2708 | goto out_free; |
| 2709 | } |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2710 | root_item = &root->root_item; |
| 2711 | |
| 2712 | subvol_info->treeid = key.objectid; |
| 2713 | |
| 2714 | subvol_info->generation = btrfs_root_generation(root_item); |
| 2715 | subvol_info->flags = btrfs_root_flags(root_item); |
| 2716 | |
| 2717 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); |
| 2718 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, |
| 2719 | BTRFS_UUID_SIZE); |
| 2720 | memcpy(subvol_info->received_uuid, root_item->received_uuid, |
| 2721 | BTRFS_UUID_SIZE); |
| 2722 | |
| 2723 | subvol_info->ctransid = btrfs_root_ctransid(root_item); |
| 2724 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); |
| 2725 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); |
| 2726 | |
| 2727 | subvol_info->otransid = btrfs_root_otransid(root_item); |
| 2728 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); |
| 2729 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); |
| 2730 | |
| 2731 | subvol_info->stransid = btrfs_root_stransid(root_item); |
| 2732 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); |
| 2733 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); |
| 2734 | |
| 2735 | subvol_info->rtransid = btrfs_root_rtransid(root_item); |
| 2736 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); |
| 2737 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); |
| 2738 | |
| 2739 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { |
| 2740 | /* Search root tree for ROOT_BACKREF of this subvolume */ |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2741 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2742 | key.offset = 0; |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2743 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2744 | if (ret < 0) { |
| 2745 | goto out; |
| 2746 | } else if (path->slots[0] >= |
| 2747 | btrfs_header_nritems(path->nodes[0])) { |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2748 | ret = btrfs_next_leaf(fs_info->tree_root, path); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2749 | if (ret < 0) { |
| 2750 | goto out; |
| 2751 | } else if (ret > 0) { |
| 2752 | ret = -EUCLEAN; |
| 2753 | goto out; |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | leaf = path->nodes[0]; |
| 2758 | slot = path->slots[0]; |
| 2759 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2760 | if (key.objectid == subvol_info->treeid && |
| 2761 | key.type == BTRFS_ROOT_BACKREF_KEY) { |
| 2762 | subvol_info->parent_id = key.offset; |
| 2763 | |
| 2764 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2765 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); |
| 2766 | |
| 2767 | item_off = btrfs_item_ptr_offset(leaf, slot) |
| 2768 | + sizeof(struct btrfs_root_ref); |
Josef Bacik | 3212fa1 | 2021-10-21 14:58:35 -0400 | [diff] [blame] | 2769 | item_len = btrfs_item_size(leaf, slot) |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2770 | - sizeof(struct btrfs_root_ref); |
| 2771 | read_extent_buffer(leaf, subvol_info->name, |
| 2772 | item_off, item_len); |
| 2773 | } else { |
| 2774 | ret = -ENOENT; |
| 2775 | goto out; |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) |
| 2780 | ret = -EFAULT; |
| 2781 | |
| 2782 | out: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 2783 | btrfs_put_root(root); |
Josef Bacik | 04734e8 | 2020-01-24 09:32:36 -0500 | [diff] [blame] | 2784 | out_free: |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2785 | btrfs_free_path(path); |
Waiman Long | b091f7f | 2020-06-16 11:31:59 -0400 | [diff] [blame] | 2786 | kfree(subvol_info); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 2787 | return ret; |
| 2788 | } |
| 2789 | |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 2790 | /* |
| 2791 | * Return ROOT_REF information of the subvolume containing this inode |
| 2792 | * except the subvolume name. |
| 2793 | */ |
| 2794 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) |
| 2795 | { |
| 2796 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; |
| 2797 | struct btrfs_root_ref *rref; |
| 2798 | struct btrfs_root *root; |
| 2799 | struct btrfs_path *path; |
| 2800 | struct btrfs_key key; |
| 2801 | struct extent_buffer *leaf; |
| 2802 | struct inode *inode; |
| 2803 | u64 objectid; |
| 2804 | int slot; |
| 2805 | int ret; |
| 2806 | u8 found; |
| 2807 | |
| 2808 | path = btrfs_alloc_path(); |
| 2809 | if (!path) |
| 2810 | return -ENOMEM; |
| 2811 | |
| 2812 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); |
| 2813 | if (IS_ERR(rootrefs)) { |
| 2814 | btrfs_free_path(path); |
| 2815 | return PTR_ERR(rootrefs); |
| 2816 | } |
| 2817 | |
| 2818 | inode = file_inode(file); |
| 2819 | root = BTRFS_I(inode)->root->fs_info->tree_root; |
| 2820 | objectid = BTRFS_I(inode)->root->root_key.objectid; |
| 2821 | |
| 2822 | key.objectid = objectid; |
| 2823 | key.type = BTRFS_ROOT_REF_KEY; |
| 2824 | key.offset = rootrefs->min_treeid; |
| 2825 | found = 0; |
| 2826 | |
| 2827 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2828 | if (ret < 0) { |
| 2829 | goto out; |
| 2830 | } else if (path->slots[0] >= |
| 2831 | btrfs_header_nritems(path->nodes[0])) { |
| 2832 | ret = btrfs_next_leaf(root, path); |
| 2833 | if (ret < 0) { |
| 2834 | goto out; |
| 2835 | } else if (ret > 0) { |
| 2836 | ret = -EUCLEAN; |
| 2837 | goto out; |
| 2838 | } |
| 2839 | } |
| 2840 | while (1) { |
| 2841 | leaf = path->nodes[0]; |
| 2842 | slot = path->slots[0]; |
| 2843 | |
| 2844 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 2845 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { |
| 2846 | ret = 0; |
| 2847 | goto out; |
| 2848 | } |
| 2849 | |
| 2850 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { |
| 2851 | ret = -EOVERFLOW; |
| 2852 | goto out; |
| 2853 | } |
| 2854 | |
| 2855 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); |
| 2856 | rootrefs->rootref[found].treeid = key.offset; |
| 2857 | rootrefs->rootref[found].dirid = |
| 2858 | btrfs_root_ref_dirid(leaf, rref); |
| 2859 | found++; |
| 2860 | |
| 2861 | ret = btrfs_next_item(root, path); |
| 2862 | if (ret < 0) { |
| 2863 | goto out; |
| 2864 | } else if (ret > 0) { |
| 2865 | ret = -EUCLEAN; |
| 2866 | goto out; |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | out: |
| 2871 | if (!ret || ret == -EOVERFLOW) { |
| 2872 | rootrefs->num_items = found; |
| 2873 | /* update min_treeid for next search */ |
| 2874 | if (found) |
| 2875 | rootrefs->min_treeid = |
| 2876 | rootrefs->rootref[found - 1].treeid + 1; |
| 2877 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) |
| 2878 | ret = -EFAULT; |
| 2879 | } |
| 2880 | |
| 2881 | kfree(rootrefs); |
| 2882 | btrfs_free_path(path); |
| 2883 | |
| 2884 | return ret; |
| 2885 | } |
| 2886 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2887 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2888 | void __user *arg, |
| 2889 | bool destroy_v2) |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2890 | { |
Al Viro | 54563d4 | 2013-09-01 15:57:51 -0400 | [diff] [blame] | 2891 | struct dentry *parent = file->f_path.dentry; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2892 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2893 | struct dentry *dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2894 | struct inode *dir = d_inode(parent); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2895 | struct inode *inode; |
| 2896 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2897 | struct btrfs_root *dest = NULL; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2898 | struct btrfs_ioctl_vol_args *vol_args = NULL; |
| 2899 | struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL; |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 2900 | struct user_namespace *mnt_userns = file_mnt_user_ns(file); |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2901 | char *subvol_name, *subvol_name_ptr = NULL; |
| 2902 | int subvol_namelen; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2903 | int err = 0; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2904 | bool destroy_parent = false; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2905 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2906 | if (destroy_v2) { |
| 2907 | vol_args2 = memdup_user(arg, sizeof(*vol_args2)); |
| 2908 | if (IS_ERR(vol_args2)) |
| 2909 | return PTR_ERR(vol_args2); |
Jeff Mahoney | 325c50e | 2016-09-21 08:31:29 -0400 | [diff] [blame] | 2910 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2911 | if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) { |
| 2912 | err = -EOPNOTSUPP; |
| 2913 | goto out; |
| 2914 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 2915 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2916 | /* |
| 2917 | * If SPEC_BY_ID is not set, we are looking for the subvolume by |
| 2918 | * name, same as v1 currently does. |
| 2919 | */ |
| 2920 | if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) { |
| 2921 | vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0; |
| 2922 | subvol_name = vol_args2->name; |
| 2923 | |
| 2924 | err = mnt_want_write_file(file); |
| 2925 | if (err) |
| 2926 | goto out; |
| 2927 | } else { |
Christian Brauner | aabb34e | 2021-07-27 12:48:54 +0200 | [diff] [blame] | 2928 | struct inode *old_dir; |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 2929 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2930 | if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2931 | err = -EINVAL; |
| 2932 | goto out; |
| 2933 | } |
| 2934 | |
| 2935 | err = mnt_want_write_file(file); |
| 2936 | if (err) |
| 2937 | goto out; |
| 2938 | |
| 2939 | dentry = btrfs_get_dentry(fs_info->sb, |
| 2940 | BTRFS_FIRST_FREE_OBJECTID, |
| 2941 | vol_args2->subvolid, 0, 0); |
| 2942 | if (IS_ERR(dentry)) { |
| 2943 | err = PTR_ERR(dentry); |
| 2944 | goto out_drop_write; |
| 2945 | } |
| 2946 | |
| 2947 | /* |
| 2948 | * Change the default parent since the subvolume being |
| 2949 | * deleted can be outside of the current mount point. |
| 2950 | */ |
| 2951 | parent = btrfs_get_parent(dentry); |
| 2952 | |
| 2953 | /* |
| 2954 | * At this point dentry->d_name can point to '/' if the |
| 2955 | * subvolume we want to destroy is outsite of the |
| 2956 | * current mount point, so we need to release the |
| 2957 | * current dentry and execute the lookup to return a new |
| 2958 | * one with ->d_name pointing to the |
| 2959 | * <mount point>/subvol_name. |
| 2960 | */ |
| 2961 | dput(dentry); |
| 2962 | if (IS_ERR(parent)) { |
| 2963 | err = PTR_ERR(parent); |
| 2964 | goto out_drop_write; |
| 2965 | } |
Christian Brauner | aabb34e | 2021-07-27 12:48:54 +0200 | [diff] [blame] | 2966 | old_dir = dir; |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2967 | dir = d_inode(parent); |
| 2968 | |
| 2969 | /* |
| 2970 | * If v2 was used with SPEC_BY_ID, a new parent was |
| 2971 | * allocated since the subvolume can be outside of the |
| 2972 | * current mount point. Later on we need to release this |
| 2973 | * new parent dentry. |
| 2974 | */ |
| 2975 | destroy_parent = true; |
| 2976 | |
Christian Brauner | aabb34e | 2021-07-27 12:48:54 +0200 | [diff] [blame] | 2977 | /* |
| 2978 | * On idmapped mounts, deletion via subvolid is |
| 2979 | * restricted to subvolumes that are immediate |
| 2980 | * ancestors of the inode referenced by the file |
| 2981 | * descriptor in the ioctl. Otherwise the idmapping |
| 2982 | * could potentially be abused to delete subvolumes |
| 2983 | * anywhere in the filesystem the user wouldn't be able |
| 2984 | * to delete without an idmapped mount. |
| 2985 | */ |
| 2986 | if (old_dir != dir && mnt_userns != &init_user_ns) { |
| 2987 | err = -EOPNOTSUPP; |
| 2988 | goto free_parent; |
| 2989 | } |
| 2990 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2991 | subvol_name_ptr = btrfs_get_subvol_name_from_objectid( |
| 2992 | fs_info, vol_args2->subvolid); |
| 2993 | if (IS_ERR(subvol_name_ptr)) { |
| 2994 | err = PTR_ERR(subvol_name_ptr); |
| 2995 | goto free_parent; |
| 2996 | } |
David Sterba | 1a9fd41 | 2021-05-21 17:42:23 +0200 | [diff] [blame] | 2997 | /* subvol_name_ptr is already nul terminated */ |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 2998 | subvol_name = (char *)kbasename(subvol_name_ptr); |
| 2999 | } |
| 3000 | } else { |
| 3001 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 3002 | if (IS_ERR(vol_args)) |
| 3003 | return PTR_ERR(vol_args); |
| 3004 | |
| 3005 | vol_args->name[BTRFS_PATH_NAME_MAX] = 0; |
| 3006 | subvol_name = vol_args->name; |
| 3007 | |
| 3008 | err = mnt_want_write_file(file); |
| 3009 | if (err) |
| 3010 | goto out; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3013 | subvol_namelen = strlen(subvol_name); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3014 | |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3015 | if (strchr(subvol_name, '/') || |
| 3016 | strncmp(subvol_name, "..", subvol_namelen) == 0) { |
| 3017 | err = -EINVAL; |
| 3018 | goto free_subvol_name; |
| 3019 | } |
| 3020 | |
| 3021 | if (!S_ISDIR(dir->i_mode)) { |
| 3022 | err = -ENOTDIR; |
| 3023 | goto free_subvol_name; |
| 3024 | } |
David Sterba | 521e054 | 2014-04-15 16:41:44 +0200 | [diff] [blame] | 3025 | |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 3026 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
| 3027 | if (err == -EINTR) |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3028 | goto free_subvol_name; |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 3029 | dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3030 | if (IS_ERR(dentry)) { |
| 3031 | err = PTR_ERR(dentry); |
| 3032 | goto out_unlock_dir; |
| 3033 | } |
| 3034 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3035 | if (d_really_is_negative(dentry)) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3036 | err = -ENOENT; |
| 3037 | goto out_dput; |
| 3038 | } |
| 3039 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3040 | inode = d_inode(dentry); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3041 | dest = BTRFS_I(inode)->root; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 3042 | if (!capable(CAP_SYS_ADMIN)) { |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3043 | /* |
| 3044 | * Regular user. Only allow this with a special mount |
| 3045 | * option, when the user has write+exec access to the |
| 3046 | * subvol root, and when rmdir(2) would have been |
| 3047 | * allowed. |
| 3048 | * |
| 3049 | * Note that this is _not_ check that the subvol is |
| 3050 | * empty or doesn't contain data that we wouldn't |
| 3051 | * otherwise be able to delete. |
| 3052 | * |
| 3053 | * Users who want to delete empty subvols should try |
| 3054 | * rmdir(2). |
| 3055 | */ |
| 3056 | err = -EPERM; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3057 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3058 | goto out_dput; |
| 3059 | |
| 3060 | /* |
| 3061 | * Do not allow deletion if the parent dir is the same |
| 3062 | * as the dir to be deleted. That means the ioctl |
| 3063 | * must be called on the dentry referencing the root |
| 3064 | * of the subvol, not a random directory contained |
| 3065 | * within it. |
| 3066 | */ |
| 3067 | err = -EINVAL; |
| 3068 | if (root == dest) |
| 3069 | goto out_dput; |
| 3070 | |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 3071 | err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC); |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3072 | if (err) |
| 3073 | goto out_dput; |
Sage Weil | 4260f7c | 2010-10-29 15:46:43 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
Miao Xie | 5c39da5 | 2012-10-22 11:39:53 +0000 | [diff] [blame] | 3076 | /* check if subvolume may be deleted by a user */ |
Christian Brauner | c4ed533 | 2021-07-27 12:48:53 +0200 | [diff] [blame] | 3077 | err = btrfs_may_delete(mnt_userns, dir, dentry, 1); |
Miao Xie | 5c39da5 | 2012-10-22 11:39:53 +0000 | [diff] [blame] | 3078 | if (err) |
| 3079 | goto out_dput; |
| 3080 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 3081 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3082 | err = -EINVAL; |
| 3083 | goto out_dput; |
| 3084 | } |
| 3085 | |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 3086 | btrfs_inode_lock(inode, 0); |
Misono Tomohiro | f60a236 | 2018-04-18 11:34:52 +0900 | [diff] [blame] | 3087 | err = btrfs_delete_subvolume(dir, dentry); |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 3088 | btrfs_inode_unlock(inode, 0); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 3089 | if (!err) { |
| 3090 | fsnotify_rmdir(dir, dentry); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3091 | d_delete(dentry); |
Amir Goldstein | 46008d9 | 2019-05-26 17:34:03 +0300 | [diff] [blame] | 3092 | } |
Liu Bo | fa6ac87 | 2013-02-20 14:10:23 +0000 | [diff] [blame] | 3093 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3094 | out_dput: |
| 3095 | dput(dentry); |
| 3096 | out_unlock_dir: |
Josef Bacik | 6470853 | 2021-02-10 17:14:34 -0500 | [diff] [blame] | 3097 | btrfs_inode_unlock(dir, 0); |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3098 | free_subvol_name: |
| 3099 | kfree(subvol_name_ptr); |
| 3100 | free_parent: |
| 3101 | if (destroy_parent) |
| 3102 | dput(parent); |
Al Viro | 0023541 | 2016-05-26 00:05:12 -0400 | [diff] [blame] | 3103 | out_drop_write: |
Al Viro | 2a79f17 | 2011-12-09 08:06:57 -0500 | [diff] [blame] | 3104 | mnt_drop_write_file(file); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3105 | out: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 3106 | kfree(vol_args2); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 3107 | kfree(vol_args); |
| 3108 | return err; |
| 3109 | } |
| 3110 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3111 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3112 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3113 | struct inode *inode = file_inode(file); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3114 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Goldwyn Rodrigues | c853a57 | 2021-07-27 16:17:30 -0500 | [diff] [blame] | 3115 | struct btrfs_ioctl_defrag_range_args range = {0}; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3116 | int ret; |
| 3117 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3118 | ret = mnt_want_write_file(file); |
| 3119 | if (ret) |
| 3120 | return ret; |
Li Zefan | b83cc96 | 2010-12-20 16:04:08 +0800 | [diff] [blame] | 3121 | |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3122 | if (btrfs_root_readonly(root)) { |
| 3123 | ret = -EROFS; |
| 3124 | goto out; |
Stefan Behrens | 5ac00ad | 2012-11-05 17:54:08 +0100 | [diff] [blame] | 3125 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3126 | |
| 3127 | switch (inode->i_mode & S_IFMT) { |
| 3128 | case S_IFDIR: |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3129 | if (!capable(CAP_SYS_ADMIN)) { |
| 3130 | ret = -EPERM; |
| 3131 | goto out; |
| 3132 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 3133 | ret = btrfs_defrag_root(root); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3134 | break; |
| 3135 | case S_IFREG: |
Adam Borowski | 616d374 | 2018-07-18 00:08:59 +0200 | [diff] [blame] | 3136 | /* |
| 3137 | * Note that this does not check the file descriptor for write |
| 3138 | * access. This prevents defragmenting executables that are |
| 3139 | * running and allows defrag on files open in read-only mode. |
| 3140 | */ |
| 3141 | if (!capable(CAP_SYS_ADMIN) && |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 3142 | inode_permission(&init_user_ns, inode, MAY_WRITE)) { |
Adam Borowski | 616d374 | 2018-07-18 00:08:59 +0200 | [diff] [blame] | 3143 | ret = -EPERM; |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3144 | goto out; |
| 3145 | } |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3146 | |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3147 | if (argp) { |
Goldwyn Rodrigues | c853a57 | 2021-07-27 16:17:30 -0500 | [diff] [blame] | 3148 | if (copy_from_user(&range, argp, sizeof(range))) { |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3149 | ret = -EFAULT; |
Dan Carpenter | 683be16 | 2010-03-20 11:24:48 +0000 | [diff] [blame] | 3150 | goto out; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3151 | } |
| 3152 | /* compression requires us to start the IO */ |
Goldwyn Rodrigues | c853a57 | 2021-07-27 16:17:30 -0500 | [diff] [blame] | 3153 | if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
| 3154 | range.flags |= BTRFS_DEFRAG_RANGE_START_IO; |
| 3155 | range.extent_thresh = (u32)-1; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3156 | } |
| 3157 | } else { |
| 3158 | /* the rest are all set to zero by kzalloc */ |
Goldwyn Rodrigues | c853a57 | 2021-07-27 16:17:30 -0500 | [diff] [blame] | 3159 | range.len = (u64)-1; |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 3160 | } |
Qu Wenruo | 1ccc2e8 | 2021-08-06 16:12:32 +0800 | [diff] [blame] | 3161 | ret = btrfs_defrag_file(file_inode(file), &file->f_ra, |
Goldwyn Rodrigues | c853a57 | 2021-07-27 16:17:30 -0500 | [diff] [blame] | 3162 | &range, BTRFS_OLDEST_GENERATION, 0); |
Chris Mason | 4cb5300 | 2011-05-24 15:35:30 -0400 | [diff] [blame] | 3163 | if (ret > 0) |
| 3164 | ret = 0; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3165 | break; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3166 | default: |
| 3167 | ret = -EINVAL; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3168 | } |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3169 | out: |
Ilya Dryomov | 25122d1 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3170 | mnt_drop_write_file(file); |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3171 | return ret; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3172 | } |
| 3173 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3174 | static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3175 | { |
| 3176 | struct btrfs_ioctl_vol_args *vol_args; |
Nikolay Borisov | a174c0a | 2021-11-25 11:14:43 +0200 | [diff] [blame] | 3177 | bool restore_op = false; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3178 | int ret; |
| 3179 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3180 | if (!capable(CAP_SYS_ADMIN)) |
| 3181 | return -EPERM; |
| 3182 | |
Nikolay Borisov | a174c0a | 2021-11-25 11:14:43 +0200 | [diff] [blame] | 3183 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) { |
| 3184 | if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD)) |
| 3185 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
| 3186 | |
| 3187 | /* |
| 3188 | * We can do the device add because we have a paused balanced, |
| 3189 | * change the exclusive op type and remember we should bring |
| 3190 | * back the paused balance |
| 3191 | */ |
| 3192 | fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD; |
| 3193 | btrfs_exclop_start_unlock(fs_info); |
| 3194 | restore_op = true; |
| 3195 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3196 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3197 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3198 | if (IS_ERR(vol_args)) { |
| 3199 | ret = PTR_ERR(vol_args); |
| 3200 | goto out; |
| 3201 | } |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3202 | |
Mark Fasheh | 5516e59 | 2008-07-24 12:20:14 -0400 | [diff] [blame] | 3203 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3204 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3205 | |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3206 | if (!ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3207 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
Anand Jain | 43d2076 | 2014-07-01 00:58:56 +0800 | [diff] [blame] | 3208 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3209 | kfree(vol_args); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3210 | out: |
Nikolay Borisov | a174c0a | 2021-11-25 11:14:43 +0200 | [diff] [blame] | 3211 | if (restore_op) |
| 3212 | btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED); |
| 3213 | else |
| 3214 | btrfs_exclop_finish(fs_info); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3215 | return ret; |
| 3216 | } |
| 3217 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3218 | static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3219 | { |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3220 | BTRFS_DEV_LOOKUP_ARGS(args); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3221 | struct inode *inode = file_inode(file); |
| 3222 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3223 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
Josef Bacik | 3fa421d | 2021-07-27 17:01:17 -0400 | [diff] [blame] | 3224 | struct block_device *bdev = NULL; |
| 3225 | fmode_t mode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3226 | int ret; |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3227 | bool cancel = false; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3228 | |
Chris Mason | e441d54 | 2009-01-05 16:57:23 -0500 | [diff] [blame] | 3229 | if (!capable(CAP_SYS_ADMIN)) |
| 3230 | return -EPERM; |
| 3231 | |
Li Zefan | dae7b66 | 2009-04-08 15:06:54 +0800 | [diff] [blame] | 3232 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
Dan Carpenter | d815b3f | 2021-11-16 14:50:25 +0300 | [diff] [blame] | 3233 | if (IS_ERR(vol_args)) |
| 3234 | return PTR_ERR(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3235 | |
David Sterba | 748449cdb | 2020-02-21 13:30:14 +0100 | [diff] [blame] | 3236 | if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) { |
Omar Sandoval | fd4e994 | 2018-05-22 15:44:01 -0700 | [diff] [blame] | 3237 | ret = -EOPNOTSUPP; |
| 3238 | goto out; |
| 3239 | } |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3240 | |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3241 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3242 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
| 3243 | args.devid = vol_args->devid; |
| 3244 | } else if (!strcmp("cancel", vol_args->name)) { |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3245 | cancel = true; |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3246 | } else { |
| 3247 | ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); |
| 3248 | if (ret) |
| 3249 | goto out; |
| 3250 | } |
| 3251 | |
| 3252 | ret = mnt_want_write_file(file); |
| 3253 | if (ret) |
| 3254 | goto out; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3255 | |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3256 | ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, |
| 3257 | cancel); |
| 3258 | if (ret) |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3259 | goto err_drop; |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3260 | |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3261 | /* Exclusive operation is now claimed */ |
| 3262 | ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3263 | |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 3264 | btrfs_exclop_finish(fs_info); |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 3265 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3266 | if (!ret) { |
David Sterba | 735654e | 2016-02-15 18:15:21 +0100 | [diff] [blame] | 3267 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3268 | btrfs_info(fs_info, "device deleted: id %llu", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3269 | vol_args->devid); |
| 3270 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3271 | btrfs_info(fs_info, "device deleted: %s", |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 3272 | vol_args->name); |
| 3273 | } |
Dan Carpenter | c47ca32 | 2014-09-04 14:09:15 +0300 | [diff] [blame] | 3274 | err_drop: |
Ilya Dryomov | 4ac20c7 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3275 | mnt_drop_write_file(file); |
Josef Bacik | 3fa421d | 2021-07-27 17:01:17 -0400 | [diff] [blame] | 3276 | if (bdev) |
| 3277 | blkdev_put(bdev, mode); |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3278 | out: |
| 3279 | btrfs_put_dev_args_from_path(&args); |
| 3280 | kfree(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3281 | return ret; |
| 3282 | } |
| 3283 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3284 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
| 3285 | { |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3286 | BTRFS_DEV_LOOKUP_ARGS(args); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3287 | struct inode *inode = file_inode(file); |
| 3288 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3289 | struct btrfs_ioctl_vol_args *vol_args; |
Josef Bacik | 3fa421d | 2021-07-27 17:01:17 -0400 | [diff] [blame] | 3290 | struct block_device *bdev = NULL; |
| 3291 | fmode_t mode; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3292 | int ret; |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3293 | bool cancel; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3294 | |
| 3295 | if (!capable(CAP_SYS_ADMIN)) |
| 3296 | return -EPERM; |
| 3297 | |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3298 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
| 3299 | if (IS_ERR(vol_args)) |
| 3300 | return PTR_ERR(vol_args); |
| 3301 | |
| 3302 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
| 3303 | if (!strcmp("cancel", vol_args->name)) { |
| 3304 | cancel = true; |
| 3305 | } else { |
| 3306 | ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name); |
| 3307 | if (ret) |
| 3308 | goto out; |
| 3309 | } |
| 3310 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3311 | ret = mnt_want_write_file(file); |
| 3312 | if (ret) |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3313 | goto out; |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3314 | |
| 3315 | ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE, |
| 3316 | cancel); |
| 3317 | if (ret == 0) { |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3318 | ret = btrfs_rm_device(fs_info, &args, &bdev, &mode); |
David Sterba | 67ae34b | 2021-05-14 21:21:27 +0200 | [diff] [blame] | 3319 | if (!ret) |
| 3320 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
| 3321 | btrfs_exclop_finish(fs_info); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3322 | } |
| 3323 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3324 | mnt_drop_write_file(file); |
Josef Bacik | 3fa421d | 2021-07-27 17:01:17 -0400 | [diff] [blame] | 3325 | if (bdev) |
| 3326 | blkdev_put(bdev, mode); |
Josef Bacik | 1a15eb7 | 2021-10-05 16:12:44 -0400 | [diff] [blame] | 3327 | out: |
| 3328 | btrfs_put_dev_args_from_path(&args); |
| 3329 | kfree(vol_args); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 3330 | return ret; |
| 3331 | } |
| 3332 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3333 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
| 3334 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3335 | { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3336 | struct btrfs_ioctl_fs_info_args *fi_args; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3337 | struct btrfs_device *device; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3338 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Johannes Thumshirn | 137c541 | 2020-07-13 21:28:58 +0900 | [diff] [blame] | 3339 | u64 flags_in; |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3340 | int ret = 0; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3341 | |
Johannes Thumshirn | 137c541 | 2020-07-13 21:28:58 +0900 | [diff] [blame] | 3342 | fi_args = memdup_user(arg, sizeof(*fi_args)); |
| 3343 | if (IS_ERR(fi_args)) |
| 3344 | return PTR_ERR(fi_args); |
| 3345 | |
| 3346 | flags_in = fi_args->flags; |
| 3347 | memset(fi_args, 0, sizeof(*fi_args)); |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3348 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3349 | rcu_read_lock(); |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3350 | fi_args->num_devices = fs_devices->num_devices; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3351 | |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3352 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3353 | if (device->devid > fi_args->max_id) |
| 3354 | fi_args->max_id = device->devid; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3355 | } |
David Sterba | d03262c | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3356 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3357 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 3358 | memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid)); |
Omar Sandoval | bea7eaf | 2017-08-22 23:46:00 -0700 | [diff] [blame] | 3359 | fi_args->nodesize = fs_info->nodesize; |
| 3360 | fi_args->sectorsize = fs_info->sectorsize; |
| 3361 | fi_args->clone_alignment = fs_info->sectorsize; |
David Sterba | 80a773f | 2014-05-07 18:17:06 +0200 | [diff] [blame] | 3362 | |
Johannes Thumshirn | 137c541 | 2020-07-13 21:28:58 +0900 | [diff] [blame] | 3363 | if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) { |
| 3364 | fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy); |
| 3365 | fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 3366 | fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO; |
| 3367 | } |
| 3368 | |
Johannes Thumshirn | 0fb408a | 2020-07-13 21:28:59 +0900 | [diff] [blame] | 3369 | if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) { |
| 3370 | fi_args->generation = fs_info->generation; |
| 3371 | fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION; |
| 3372 | } |
| 3373 | |
Johannes Thumshirn | 49bac89 | 2020-07-13 21:29:00 +0900 | [diff] [blame] | 3374 | if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) { |
| 3375 | memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid, |
| 3376 | sizeof(fi_args->metadata_uuid)); |
| 3377 | fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID; |
| 3378 | } |
| 3379 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3380 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
| 3381 | ret = -EFAULT; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3382 | |
Li Zefan | 027ed2f | 2011-06-08 08:27:56 +0000 | [diff] [blame] | 3383 | kfree(fi_args); |
| 3384 | return ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3385 | } |
| 3386 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3387 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
| 3388 | void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3389 | { |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 3390 | BTRFS_DEV_LOOKUP_ARGS(args); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3391 | struct btrfs_ioctl_dev_info_args *di_args; |
| 3392 | struct btrfs_device *dev; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3393 | int ret = 0; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3394 | |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3395 | di_args = memdup_user(arg, sizeof(*di_args)); |
| 3396 | if (IS_ERR(di_args)) |
| 3397 | return PTR_ERR(di_args); |
| 3398 | |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 3399 | args.devid = di_args->devid; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 3400 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 3401 | args.uuid = di_args->uuid; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3402 | |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3403 | rcu_read_lock(); |
Josef Bacik | 562d7b1 | 2021-10-05 16:12:42 -0400 | [diff] [blame] | 3404 | dev = btrfs_find_device(fs_info->fs_devices, &args); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3405 | if (!dev) { |
| 3406 | ret = -ENODEV; |
| 3407 | goto out; |
| 3408 | } |
| 3409 | |
| 3410 | di_args->devid = dev->devid; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3411 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
| 3412 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3413 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3414 | if (dev->name) { |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 3415 | strncpy(di_args->path, rcu_str_deref(dev->name), |
| 3416 | sizeof(di_args->path) - 1); |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3417 | di_args->path[sizeof(di_args->path) - 1] = 0; |
| 3418 | } else { |
Stefan Behrens | 99ba55a | 2012-03-19 16:17:22 +0100 | [diff] [blame] | 3419 | di_args->path[0] = '\0'; |
Jim Meyering | a27202f | 2012-04-26 18:36:56 +0200 | [diff] [blame] | 3420 | } |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3421 | |
| 3422 | out: |
David Sterba | c5593ca | 2017-06-16 00:09:21 +0200 | [diff] [blame] | 3423 | rcu_read_unlock(); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3424 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
| 3425 | ret = -EFAULT; |
| 3426 | |
| 3427 | kfree(di_args); |
| 3428 | return ret; |
| 3429 | } |
| 3430 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3431 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
| 3432 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3433 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3434 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3435 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3436 | struct btrfs_root *new_root; |
| 3437 | struct btrfs_dir_item *di; |
| 3438 | struct btrfs_trans_handle *trans; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3439 | struct btrfs_path *path = NULL; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3440 | struct btrfs_disk_key disk_key; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3441 | u64 objectid = 0; |
| 3442 | u64 dir_id; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3443 | int ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3444 | |
| 3445 | if (!capable(CAP_SYS_ADMIN)) |
| 3446 | return -EPERM; |
| 3447 | |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3448 | ret = mnt_want_write_file(file); |
| 3449 | if (ret) |
| 3450 | return ret; |
| 3451 | |
| 3452 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { |
| 3453 | ret = -EFAULT; |
| 3454 | goto out; |
| 3455 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3456 | |
| 3457 | if (!objectid) |
chandan | 1cecf57 | 2013-09-13 19:34:10 +0530 | [diff] [blame] | 3458 | objectid = BTRFS_FS_TREE_OBJECTID; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3459 | |
David Sterba | 56e9357 | 2020-05-15 19:35:55 +0200 | [diff] [blame] | 3460 | new_root = btrfs_get_fs_root(fs_info, objectid, true); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3461 | if (IS_ERR(new_root)) { |
| 3462 | ret = PTR_ERR(new_root); |
| 3463 | goto out; |
| 3464 | } |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3465 | if (!is_fstree(new_root->root_key.objectid)) { |
| 3466 | ret = -ENOENT; |
| 3467 | goto out_free; |
| 3468 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3469 | |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3470 | path = btrfs_alloc_path(); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3471 | if (!path) { |
| 3472 | ret = -ENOMEM; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3473 | goto out_free; |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3474 | } |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3475 | |
| 3476 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3477 | if (IS_ERR(trans)) { |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3478 | ret = PTR_ERR(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3479 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3482 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
| 3483 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3484 | dir_id, "default", 7, 1); |
Dan Carpenter | cf1e99a | 2010-05-29 09:47:24 +0000 | [diff] [blame] | 3485 | if (IS_ERR_OR_NULL(di)) { |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3486 | btrfs_release_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3487 | btrfs_end_transaction(trans); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3488 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3489 | "Umm, you don't have the default diritem, this isn't going to work"); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3490 | ret = -ENOENT; |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3491 | goto out_free; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
| 3495 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
| 3496 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3497 | btrfs_release_path(path); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3498 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3499 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3500 | btrfs_end_transaction(trans); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3501 | out_free: |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 3502 | btrfs_put_root(new_root); |
Josef Bacik | 2a2b5d6 | 2020-01-24 09:32:37 -0500 | [diff] [blame] | 3503 | btrfs_free_path(path); |
Miao Xie | 3c04ce0 | 2012-11-26 08:43:07 +0000 | [diff] [blame] | 3504 | out: |
| 3505 | mnt_drop_write_file(file); |
| 3506 | return ret; |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 3509 | static void get_block_group_info(struct list_head *groups_list, |
| 3510 | struct btrfs_ioctl_space_info *space) |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3511 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3512 | struct btrfs_block_group *block_group; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3513 | |
| 3514 | space->total_bytes = 0; |
| 3515 | space->used_bytes = 0; |
| 3516 | space->flags = 0; |
| 3517 | list_for_each_entry(block_group, groups_list, list) { |
| 3518 | space->flags = block_group->flags; |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3519 | space->total_bytes += block_group->length; |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3520 | space->used_bytes += block_group->used; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3524 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
| 3525 | void __user *arg) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3526 | { |
| 3527 | struct btrfs_ioctl_space_args space_args; |
| 3528 | struct btrfs_ioctl_space_info space; |
| 3529 | struct btrfs_ioctl_space_info *dest; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3530 | struct btrfs_ioctl_space_info *dest_orig; |
Daniel J Blueman | 13f2696 | 2011-04-11 15:56:31 +0000 | [diff] [blame] | 3531 | struct btrfs_ioctl_space_info __user *user_dest; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3532 | struct btrfs_space_info *info; |
Colin Ian King | 315d8e9 | 2017-09-19 16:01:23 +0100 | [diff] [blame] | 3533 | static const u64 types[] = { |
| 3534 | BTRFS_BLOCK_GROUP_DATA, |
| 3535 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 3536 | BTRFS_BLOCK_GROUP_METADATA, |
| 3537 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA |
| 3538 | }; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3539 | int num_types = 4; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3540 | int alloc_size; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3541 | int ret = 0; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3542 | u64 slot_count = 0; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3543 | int i, c; |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3544 | |
| 3545 | if (copy_from_user(&space_args, |
| 3546 | (struct btrfs_ioctl_space_args __user *)arg, |
| 3547 | sizeof(space_args))) |
| 3548 | return -EFAULT; |
| 3549 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3550 | for (i = 0; i < num_types; i++) { |
| 3551 | struct btrfs_space_info *tmp; |
| 3552 | |
| 3553 | info = NULL; |
Josef Bacik | 7280490 | 2020-09-01 17:40:37 -0400 | [diff] [blame] | 3554 | list_for_each_entry(tmp, &fs_info->space_info, list) { |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3555 | if (tmp->flags == types[i]) { |
| 3556 | info = tmp; |
| 3557 | break; |
| 3558 | } |
| 3559 | } |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3560 | |
| 3561 | if (!info) |
| 3562 | continue; |
| 3563 | |
| 3564 | down_read(&info->groups_sem); |
| 3565 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3566 | if (!list_empty(&info->block_groups[c])) |
| 3567 | slot_count++; |
| 3568 | } |
| 3569 | up_read(&info->groups_sem); |
| 3570 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3571 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3572 | /* |
| 3573 | * Global block reserve, exported as a space_info |
| 3574 | */ |
| 3575 | slot_count++; |
| 3576 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3577 | /* space_slots == 0 means they are asking for a count */ |
| 3578 | if (space_args.space_slots == 0) { |
| 3579 | space_args.total_spaces = slot_count; |
| 3580 | goto out; |
| 3581 | } |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3582 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3583 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3584 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3585 | alloc_size = sizeof(*dest) * slot_count; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3586 | |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3587 | /* we generally have at most 6 or so space infos, one for each raid |
| 3588 | * level. So, a whole page should be more than enough for everyone |
| 3589 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3590 | if (alloc_size > PAGE_SIZE) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3591 | return -ENOMEM; |
| 3592 | |
| 3593 | space_args.total_spaces = 0; |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 3594 | dest = kmalloc(alloc_size, GFP_KERNEL); |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3595 | if (!dest) |
| 3596 | return -ENOMEM; |
| 3597 | dest_orig = dest; |
| 3598 | |
| 3599 | /* now we have a buffer to copy into */ |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3600 | for (i = 0; i < num_types; i++) { |
| 3601 | struct btrfs_space_info *tmp; |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3602 | |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3603 | if (!slot_count) |
| 3604 | break; |
| 3605 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3606 | info = NULL; |
Josef Bacik | 7280490 | 2020-09-01 17:40:37 -0400 | [diff] [blame] | 3607 | list_for_each_entry(tmp, &fs_info->space_info, list) { |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3608 | if (tmp->flags == types[i]) { |
| 3609 | info = tmp; |
| 3610 | break; |
| 3611 | } |
| 3612 | } |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3613 | |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3614 | if (!info) |
| 3615 | continue; |
| 3616 | down_read(&info->groups_sem); |
| 3617 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
| 3618 | if (!list_empty(&info->block_groups[c])) { |
Su Yue | c065f5b1 | 2018-04-02 17:24:11 +0800 | [diff] [blame] | 3619 | get_block_group_info(&info->block_groups[c], |
| 3620 | &space); |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3621 | memcpy(dest, &space, sizeof(space)); |
| 3622 | dest++; |
| 3623 | space_args.total_spaces++; |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3624 | slot_count--; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3625 | } |
Dan Rosenberg | 51788b1 | 2011-02-14 16:04:23 -0500 | [diff] [blame] | 3626 | if (!slot_count) |
| 3627 | break; |
Josef Bacik | bf5fc093 | 2010-09-29 11:22:36 -0400 | [diff] [blame] | 3628 | } |
| 3629 | up_read(&info->groups_sem); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3630 | } |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3631 | |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3632 | /* |
| 3633 | * Add global block reserve |
| 3634 | */ |
| 3635 | if (slot_count) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3636 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
David Sterba | 36523e95 | 2014-02-07 14:34:12 +0100 | [diff] [blame] | 3637 | |
| 3638 | spin_lock(&block_rsv->lock); |
| 3639 | space.total_bytes = block_rsv->size; |
| 3640 | space.used_bytes = block_rsv->size - block_rsv->reserved; |
| 3641 | spin_unlock(&block_rsv->lock); |
| 3642 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; |
| 3643 | memcpy(dest, &space, sizeof(space)); |
| 3644 | space_args.total_spaces++; |
| 3645 | } |
| 3646 | |
Daniel J Blueman | 2eec6c8 | 2012-04-26 00:37:14 +0800 | [diff] [blame] | 3647 | user_dest = (struct btrfs_ioctl_space_info __user *) |
Chris Mason | 7fde62b | 2010-03-16 15:40:10 -0400 | [diff] [blame] | 3648 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
| 3649 | |
| 3650 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
| 3651 | ret = -EFAULT; |
| 3652 | |
| 3653 | kfree(dest_orig); |
| 3654 | out: |
| 3655 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 3656 | ret = -EFAULT; |
| 3657 | |
| 3658 | return ret; |
| 3659 | } |
| 3660 | |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3661 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
| 3662 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3663 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3664 | struct btrfs_trans_handle *trans; |
| 3665 | u64 transid; |
| 3666 | |
Miao Xie | d4edf39 | 2013-02-20 09:17:06 +0000 | [diff] [blame] | 3667 | trans = btrfs_attach_transaction_barrier(root); |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3668 | if (IS_ERR(trans)) { |
| 3669 | if (PTR_ERR(trans) != -ENOENT) |
| 3670 | return PTR_ERR(trans); |
| 3671 | |
| 3672 | /* No running transaction, don't bother */ |
| 3673 | transid = root->fs_info->last_trans_committed; |
| 3674 | goto out; |
| 3675 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3676 | transid = trans->transid; |
Josef Bacik | fdfbf02 | 2021-11-05 16:45:28 -0400 | [diff] [blame] | 3677 | btrfs_commit_transaction_async(trans); |
Miao Xie | ff7c1d3 | 2012-11-26 08:41:29 +0000 | [diff] [blame] | 3678 | out: |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3679 | if (argp) |
| 3680 | if (copy_to_user(argp, &transid, sizeof(transid))) |
| 3681 | return -EFAULT; |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3685 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 3686 | void __user *argp) |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3687 | { |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3688 | u64 transid; |
| 3689 | |
| 3690 | if (argp) { |
| 3691 | if (copy_from_user(&transid, argp, sizeof(transid))) |
| 3692 | return -EFAULT; |
| 3693 | } else { |
| 3694 | transid = 0; /* current trans */ |
| 3695 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3696 | return btrfs_wait_for_commit(fs_info, transid); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 3697 | } |
| 3698 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3699 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3700 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3701 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3702 | struct btrfs_ioctl_scrub_args *sa; |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3703 | int ret; |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3704 | |
| 3705 | if (!capable(CAP_SYS_ADMIN)) |
| 3706 | return -EPERM; |
| 3707 | |
| 3708 | sa = memdup_user(arg, sizeof(*sa)); |
| 3709 | if (IS_ERR(sa)) |
| 3710 | return PTR_ERR(sa); |
| 3711 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3712 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
| 3713 | ret = mnt_want_write_file(file); |
| 3714 | if (ret) |
| 3715 | goto out; |
| 3716 | } |
| 3717 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3718 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3719 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
| 3720 | 0); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3721 | |
Filipe Manana | 5afe6ce | 2020-01-16 11:29:20 +0000 | [diff] [blame] | 3722 | /* |
| 3723 | * Copy scrub args to user space even if btrfs_scrub_dev() returned an |
| 3724 | * error. This is important as it allows user space to know how much |
| 3725 | * progress scrub has done. For example, if scrub is canceled we get |
| 3726 | * -ECANCELED from btrfs_scrub_dev() and return that error back to user |
| 3727 | * space. Later user space can inspect the progress from the structure |
| 3728 | * btrfs_ioctl_scrub_args and resume scrub from where it left off |
| 3729 | * previously (btrfs-progs does this). |
| 3730 | * If we fail to copy the btrfs_ioctl_scrub_args structure to user space |
| 3731 | * then return -EFAULT to signal the structure was not copied or it may |
| 3732 | * be corrupt and unreliable due to a partial copy. |
| 3733 | */ |
| 3734 | if (copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3735 | ret = -EFAULT; |
| 3736 | |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 3737 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
| 3738 | mnt_drop_write_file(file); |
| 3739 | out: |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3740 | kfree(sa); |
| 3741 | return ret; |
| 3742 | } |
| 3743 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3744 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3745 | { |
| 3746 | if (!capable(CAP_SYS_ADMIN)) |
| 3747 | return -EPERM; |
| 3748 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3749 | return btrfs_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3752 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3753 | void __user *arg) |
| 3754 | { |
| 3755 | struct btrfs_ioctl_scrub_args *sa; |
| 3756 | int ret; |
| 3757 | |
| 3758 | if (!capable(CAP_SYS_ADMIN)) |
| 3759 | return -EPERM; |
| 3760 | |
| 3761 | sa = memdup_user(arg, sizeof(*sa)); |
| 3762 | if (IS_ERR(sa)) |
| 3763 | return PTR_ERR(sa); |
| 3764 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3765 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3766 | |
Filipe Manana | 4fa99b0 | 2018-12-14 19:45:13 +0000 | [diff] [blame] | 3767 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 3768 | ret = -EFAULT; |
| 3769 | |
| 3770 | kfree(sa); |
| 3771 | return ret; |
| 3772 | } |
| 3773 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3774 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3775 | void __user *arg) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3776 | { |
| 3777 | struct btrfs_ioctl_get_dev_stats *sa; |
| 3778 | int ret; |
| 3779 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3780 | sa = memdup_user(arg, sizeof(*sa)); |
| 3781 | if (IS_ERR(sa)) |
| 3782 | return PTR_ERR(sa); |
| 3783 | |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 3784 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
| 3785 | kfree(sa); |
| 3786 | return -EPERM; |
| 3787 | } |
| 3788 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3789 | ret = btrfs_get_dev_stats(fs_info, sa); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3790 | |
Filipe Manana | eee9957 | 2018-12-14 19:45:22 +0000 | [diff] [blame] | 3791 | if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa))) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 3792 | ret = -EFAULT; |
| 3793 | |
| 3794 | kfree(sa); |
| 3795 | return ret; |
| 3796 | } |
| 3797 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3798 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
| 3799 | void __user *arg) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3800 | { |
| 3801 | struct btrfs_ioctl_dev_replace_args *p; |
| 3802 | int ret; |
| 3803 | |
| 3804 | if (!capable(CAP_SYS_ADMIN)) |
| 3805 | return -EPERM; |
| 3806 | |
| 3807 | p = memdup_user(arg, sizeof(*p)); |
| 3808 | if (IS_ERR(p)) |
| 3809 | return PTR_ERR(p); |
| 3810 | |
| 3811 | switch (p->cmd) { |
| 3812 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 3813 | if (sb_rdonly(fs_info->sb)) { |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 3814 | ret = -EROFS; |
| 3815 | goto out; |
| 3816 | } |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 3817 | if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) { |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 3818 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3819 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3820 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 3821 | btrfs_exclop_finish(fs_info); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3822 | } |
| 3823 | break; |
| 3824 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3825 | btrfs_dev_replace_status(fs_info, p); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3826 | ret = 0; |
| 3827 | break; |
| 3828 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: |
Anand Jain | 17d202b | 2018-02-12 23:33:30 +0800 | [diff] [blame] | 3829 | p->result = btrfs_dev_replace_cancel(fs_info); |
Anand Jain | 9728203 | 2018-02-12 23:33:29 +0800 | [diff] [blame] | 3830 | ret = 0; |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3831 | break; |
| 3832 | default: |
| 3833 | ret = -EINVAL; |
| 3834 | break; |
| 3835 | } |
| 3836 | |
Filipe Manana | d3a5328 | 2019-01-08 11:42:09 +0000 | [diff] [blame] | 3837 | if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p))) |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3838 | ret = -EFAULT; |
Ilya Dryomov | adfa97c | 2013-10-10 20:39:28 +0300 | [diff] [blame] | 3839 | out: |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 3840 | kfree(p); |
| 3841 | return ret; |
| 3842 | } |
| 3843 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3844 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
| 3845 | { |
| 3846 | int ret = 0; |
| 3847 | int i; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3848 | u64 rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3849 | int size; |
Chris Mason | 806468f | 2011-11-06 03:07:10 -0500 | [diff] [blame] | 3850 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3851 | struct inode_fs_paths *ipath = NULL; |
| 3852 | struct btrfs_path *path; |
| 3853 | |
Kusanagi Kouichi | 82b22ac | 2013-01-28 11:33:31 +0000 | [diff] [blame] | 3854 | if (!capable(CAP_DAC_READ_SEARCH)) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3855 | return -EPERM; |
| 3856 | |
| 3857 | path = btrfs_alloc_path(); |
| 3858 | if (!path) { |
| 3859 | ret = -ENOMEM; |
| 3860 | goto out; |
| 3861 | } |
| 3862 | |
| 3863 | ipa = memdup_user(arg, sizeof(*ipa)); |
| 3864 | if (IS_ERR(ipa)) { |
| 3865 | ret = PTR_ERR(ipa); |
| 3866 | ipa = NULL; |
| 3867 | goto out; |
| 3868 | } |
| 3869 | |
| 3870 | size = min_t(u32, ipa->size, 4096); |
| 3871 | ipath = init_ipath(size, root, path); |
| 3872 | if (IS_ERR(ipath)) { |
| 3873 | ret = PTR_ERR(ipath); |
| 3874 | ipath = NULL; |
| 3875 | goto out; |
| 3876 | } |
| 3877 | |
| 3878 | ret = paths_from_inode(ipa->inum, ipath); |
| 3879 | if (ret < 0) |
| 3880 | goto out; |
| 3881 | |
| 3882 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
Jeff Mahoney | 745c4d8 | 2011-11-20 07:31:57 -0500 | [diff] [blame] | 3883 | rel_ptr = ipath->fspath->val[i] - |
| 3884 | (u64)(unsigned long)ipath->fspath->val; |
Chris Mason | 740c3d2 | 2011-11-02 15:48:34 -0400 | [diff] [blame] | 3885 | ipath->fspath->val[i] = rel_ptr; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 3888 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
| 3889 | ipath->fspath, size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3890 | if (ret) { |
| 3891 | ret = -EFAULT; |
| 3892 | goto out; |
| 3893 | } |
| 3894 | |
| 3895 | out: |
| 3896 | btrfs_free_path(path); |
| 3897 | free_ipath(ipath); |
| 3898 | kfree(ipa); |
| 3899 | |
| 3900 | return ret; |
| 3901 | } |
| 3902 | |
| 3903 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
| 3904 | { |
| 3905 | struct btrfs_data_container *inodes = ctx; |
| 3906 | const size_t c = 3 * sizeof(u64); |
| 3907 | |
| 3908 | if (inodes->bytes_left >= c) { |
| 3909 | inodes->bytes_left -= c; |
| 3910 | inodes->val[inodes->elem_cnt] = inum; |
| 3911 | inodes->val[inodes->elem_cnt + 1] = offset; |
| 3912 | inodes->val[inodes->elem_cnt + 2] = root; |
| 3913 | inodes->elem_cnt += 3; |
| 3914 | } else { |
| 3915 | inodes->bytes_missing += c - inodes->bytes_left; |
| 3916 | inodes->bytes_left = 0; |
| 3917 | inodes->elem_missed += 3; |
| 3918 | } |
| 3919 | |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3923 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3924 | void __user *arg, int version) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3925 | { |
| 3926 | int ret = 0; |
| 3927 | int size; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3928 | struct btrfs_ioctl_logical_ino_args *loi; |
| 3929 | struct btrfs_data_container *inodes = NULL; |
| 3930 | struct btrfs_path *path = NULL; |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3931 | bool ignore_offset; |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3932 | |
| 3933 | if (!capable(CAP_SYS_ADMIN)) |
| 3934 | return -EPERM; |
| 3935 | |
| 3936 | loi = memdup_user(arg, sizeof(*loi)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 3937 | if (IS_ERR(loi)) |
| 3938 | return PTR_ERR(loi); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3939 | |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3940 | if (version == 1) { |
| 3941 | ignore_offset = false; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 3942 | size = min_t(u32, loi->size, SZ_64K); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3943 | } else { |
| 3944 | /* All reserved bits must be 0 for now */ |
| 3945 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { |
| 3946 | ret = -EINVAL; |
| 3947 | goto out_loi; |
| 3948 | } |
| 3949 | /* Only accept flags we have defined so far */ |
| 3950 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { |
| 3951 | ret = -EINVAL; |
| 3952 | goto out_loi; |
| 3953 | } |
| 3954 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; |
Zygo Blaxell | b115e3b | 2017-09-22 13:58:47 -0400 | [diff] [blame] | 3955 | size = min_t(u32, loi->size, SZ_16M); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3956 | } |
| 3957 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3958 | path = btrfs_alloc_path(); |
| 3959 | if (!path) { |
| 3960 | ret = -ENOMEM; |
| 3961 | goto out; |
| 3962 | } |
| 3963 | |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3964 | inodes = init_data_container(size); |
| 3965 | if (IS_ERR(inodes)) { |
| 3966 | ret = PTR_ERR(inodes); |
| 3967 | inodes = NULL; |
| 3968 | goto out; |
| 3969 | } |
| 3970 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3971 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3972 | build_ino_list, inodes, ignore_offset); |
Liu Bo | df031f0 | 2012-09-07 20:01:29 -0600 | [diff] [blame] | 3973 | if (ret == -EINVAL) |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3974 | ret = -ENOENT; |
| 3975 | if (ret < 0) |
| 3976 | goto out; |
| 3977 | |
Omar Sandoval | 718dc5f | 2017-08-22 23:46:05 -0700 | [diff] [blame] | 3978 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
| 3979 | size); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3980 | if (ret) |
| 3981 | ret = -EFAULT; |
| 3982 | |
| 3983 | out: |
| 3984 | btrfs_free_path(path); |
David Sterba | f54de06 | 2017-05-31 19:32:09 +0200 | [diff] [blame] | 3985 | kvfree(inodes); |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 3986 | out_loi: |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 3987 | kfree(loi); |
| 3988 | |
| 3989 | return ret; |
| 3990 | } |
| 3991 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 3992 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3993 | struct btrfs_ioctl_balance_args *bargs) |
| 3994 | { |
| 3995 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3996 | |
| 3997 | bargs->flags = bctl->flags; |
| 3998 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3999 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4000 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
| 4001 | if (atomic_read(&fs_info->balance_pause_req)) |
| 4002 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4003 | if (atomic_read(&fs_info->balance_cancel_req)) |
| 4004 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4005 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4006 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
| 4007 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
| 4008 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4009 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4010 | spin_lock(&fs_info->balance_lock); |
| 4011 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
| 4012 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4013 | } |
| 4014 | |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4015 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4016 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4017 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4018 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4019 | struct btrfs_ioctl_balance_args *bargs; |
| 4020 | struct btrfs_balance_control *bctl; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4021 | bool need_unlock; /* for mut. excl. ops lock */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4022 | int ret; |
| 4023 | |
Nikolay Borisov | 6c405b2 | 2021-11-10 13:41:04 +0200 | [diff] [blame] | 4024 | if (!arg) |
| 4025 | btrfs_warn(fs_info, |
| 4026 | "IOC_BALANCE ioctl (v1) is deprecated and will be removed in kernel 5.18"); |
| 4027 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4028 | if (!capable(CAP_SYS_ADMIN)) |
| 4029 | return -EPERM; |
| 4030 | |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4031 | ret = mnt_want_write_file(file); |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4032 | if (ret) |
| 4033 | return ret; |
| 4034 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4035 | again: |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 4036 | if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4037 | mutex_lock(&fs_info->balance_mutex); |
| 4038 | need_unlock = true; |
| 4039 | goto locked; |
| 4040 | } |
| 4041 | |
| 4042 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4043 | * mut. excl. ops lock is locked. Three possibilities: |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4044 | * (1) some other op is running |
| 4045 | * (2) balance is running |
| 4046 | * (3) balance is paused -- special case (think resume) |
| 4047 | */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4048 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4049 | if (fs_info->balance_ctl) { |
| 4050 | /* this is either (2) or (3) */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4051 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4052 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4053 | /* |
| 4054 | * Lock released to allow other waiters to continue, |
| 4055 | * we'll reexamine the status again. |
| 4056 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4057 | mutex_lock(&fs_info->balance_mutex); |
| 4058 | |
| 4059 | if (fs_info->balance_ctl && |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4060 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4061 | /* this is (3) */ |
| 4062 | need_unlock = false; |
| 4063 | goto locked; |
| 4064 | } |
| 4065 | |
| 4066 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4067 | goto again; |
| 4068 | } else { |
| 4069 | /* this is (2) */ |
| 4070 | mutex_unlock(&fs_info->balance_mutex); |
| 4071 | ret = -EINPROGRESS; |
| 4072 | goto out; |
| 4073 | } |
| 4074 | } else { |
| 4075 | /* this is (1) */ |
| 4076 | mutex_unlock(&fs_info->balance_mutex); |
Anand Jain | e57138b | 2013-08-21 11:44:48 +0800 | [diff] [blame] | 4077 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4078 | goto out; |
| 4079 | } |
| 4080 | |
| 4081 | locked: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4082 | |
| 4083 | if (arg) { |
| 4084 | bargs = memdup_user(arg, sizeof(*bargs)); |
| 4085 | if (IS_ERR(bargs)) { |
| 4086 | ret = PTR_ERR(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4087 | goto out_unlock; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4088 | } |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4089 | |
| 4090 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
| 4091 | if (!fs_info->balance_ctl) { |
| 4092 | ret = -ENOTCONN; |
| 4093 | goto out_bargs; |
| 4094 | } |
| 4095 | |
| 4096 | bctl = fs_info->balance_ctl; |
| 4097 | spin_lock(&fs_info->balance_lock); |
| 4098 | bctl->flags |= BTRFS_BALANCE_RESUME; |
| 4099 | spin_unlock(&fs_info->balance_lock); |
Nikolay Borisov | efc0e69 | 2021-11-25 11:14:41 +0200 | [diff] [blame] | 4100 | btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE); |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4101 | |
| 4102 | goto do_balance; |
| 4103 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4104 | } else { |
| 4105 | bargs = NULL; |
| 4106 | } |
| 4107 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4108 | if (fs_info->balance_ctl) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4109 | ret = -EINPROGRESS; |
| 4110 | goto out_bargs; |
| 4111 | } |
| 4112 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4113 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4114 | if (!bctl) { |
| 4115 | ret = -ENOMEM; |
| 4116 | goto out_bargs; |
| 4117 | } |
| 4118 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4119 | if (arg) { |
| 4120 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
| 4121 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
| 4122 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
| 4123 | |
| 4124 | bctl->flags = bargs->flags; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4125 | } else { |
| 4126 | /* balance everything - no filters */ |
| 4127 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4128 | } |
| 4129 | |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 4130 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
| 4131 | ret = -EINVAL; |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4132 | goto out_bctl; |
David Sterba | 8eb9345 | 2015-10-12 16:55:54 +0200 | [diff] [blame] | 4133 | } |
| 4134 | |
Ilya Dryomov | de32226 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4135 | do_balance: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4136 | /* |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 4137 | * Ownership of bctl and exclusive operation goes to btrfs_balance. |
| 4138 | * bctl is freed in reset_balance_state, or, if restriper was paused |
| 4139 | * all the way until unmount, in free_fs_info. The flag should be |
| 4140 | * cleared after reset_balance_state. |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4141 | */ |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4142 | need_unlock = false; |
| 4143 | |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4144 | ret = btrfs_balance(fs_info, bctl, bargs); |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4145 | bctl = NULL; |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4146 | |
Filipe Manana | d00c2d9 | 2019-01-08 11:42:01 +0000 | [diff] [blame] | 4147 | if ((ret == 0 || ret == -ECANCELED) && arg) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4148 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4149 | ret = -EFAULT; |
| 4150 | } |
| 4151 | |
Christian Engelmayer | 0f89abf | 2015-10-21 00:50:06 +0200 | [diff] [blame] | 4152 | out_bctl: |
| 4153 | kfree(bctl); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4154 | out_bargs: |
| 4155 | kfree(bargs); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4156 | out_unlock: |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4157 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4158 | if (need_unlock) |
Goldwyn Rodrigues | c3e1f96 | 2020-08-25 10:02:32 -0500 | [diff] [blame] | 4159 | btrfs_exclop_finish(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4160 | out: |
Liu Bo | e54bfa31 | 2012-06-29 03:58:48 -0600 | [diff] [blame] | 4161 | mnt_drop_write_file(file); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4162 | return ret; |
| 4163 | } |
| 4164 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4165 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4166 | { |
| 4167 | if (!capable(CAP_SYS_ADMIN)) |
| 4168 | return -EPERM; |
| 4169 | |
| 4170 | switch (cmd) { |
| 4171 | case BTRFS_BALANCE_CTL_PAUSE: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4172 | return btrfs_pause_balance(fs_info); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4173 | case BTRFS_BALANCE_CTL_CANCEL: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4174 | return btrfs_cancel_balance(fs_info); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | return -EINVAL; |
| 4178 | } |
| 4179 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4180 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4181 | void __user *arg) |
| 4182 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4183 | struct btrfs_ioctl_balance_args *bargs; |
| 4184 | int ret = 0; |
| 4185 | |
| 4186 | if (!capable(CAP_SYS_ADMIN)) |
| 4187 | return -EPERM; |
| 4188 | |
| 4189 | mutex_lock(&fs_info->balance_mutex); |
| 4190 | if (!fs_info->balance_ctl) { |
| 4191 | ret = -ENOTCONN; |
| 4192 | goto out; |
| 4193 | } |
| 4194 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4195 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4196 | if (!bargs) { |
| 4197 | ret = -ENOMEM; |
| 4198 | goto out; |
| 4199 | } |
| 4200 | |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4201 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4202 | |
| 4203 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
| 4204 | ret = -EFAULT; |
| 4205 | |
| 4206 | kfree(bargs); |
| 4207 | out: |
| 4208 | mutex_unlock(&fs_info->balance_mutex); |
| 4209 | return ret; |
| 4210 | } |
| 4211 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4212 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4213 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4214 | struct inode *inode = file_inode(file); |
| 4215 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4216 | struct btrfs_ioctl_quota_ctl_args *sa; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4217 | int ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4218 | |
| 4219 | if (!capable(CAP_SYS_ADMIN)) |
| 4220 | return -EPERM; |
| 4221 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4222 | ret = mnt_want_write_file(file); |
| 4223 | if (ret) |
| 4224 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4225 | |
| 4226 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4227 | if (IS_ERR(sa)) { |
| 4228 | ret = PTR_ERR(sa); |
| 4229 | goto drop_write; |
| 4230 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4231 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4232 | down_write(&fs_info->subvol_sem); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4233 | |
| 4234 | switch (sa->cmd) { |
| 4235 | case BTRFS_QUOTA_CTL_ENABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4236 | ret = btrfs_quota_enable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4237 | break; |
| 4238 | case BTRFS_QUOTA_CTL_DISABLE: |
Nikolay Borisov | 340f1aa | 2018-07-05 14:50:48 +0300 | [diff] [blame] | 4239 | ret = btrfs_quota_disable(fs_info); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4240 | break; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4241 | default: |
| 4242 | ret = -EINVAL; |
| 4243 | break; |
| 4244 | } |
| 4245 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4246 | kfree(sa); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4247 | up_write(&fs_info->subvol_sem); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4248 | drop_write: |
| 4249 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4250 | return ret; |
| 4251 | } |
| 4252 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4253 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4254 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4255 | struct inode *inode = file_inode(file); |
| 4256 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4257 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4258 | struct btrfs_ioctl_qgroup_assign_args *sa; |
| 4259 | struct btrfs_trans_handle *trans; |
| 4260 | int ret; |
| 4261 | int err; |
| 4262 | |
| 4263 | if (!capable(CAP_SYS_ADMIN)) |
| 4264 | return -EPERM; |
| 4265 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4266 | ret = mnt_want_write_file(file); |
| 4267 | if (ret) |
| 4268 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4269 | |
| 4270 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4271 | if (IS_ERR(sa)) { |
| 4272 | ret = PTR_ERR(sa); |
| 4273 | goto drop_write; |
| 4274 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4275 | |
| 4276 | trans = btrfs_join_transaction(root); |
| 4277 | if (IS_ERR(trans)) { |
| 4278 | ret = PTR_ERR(trans); |
| 4279 | goto out; |
| 4280 | } |
| 4281 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4282 | if (sa->assign) { |
Lu Fengqi | 9f8a6ce | 2018-07-18 14:45:30 +0800 | [diff] [blame] | 4283 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4284 | } else { |
Lu Fengqi | 39616c2 | 2018-07-18 14:45:32 +0800 | [diff] [blame] | 4285 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4286 | } |
| 4287 | |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4288 | /* update qgroup status and info */ |
Lu Fengqi | 280f8bd | 2018-07-18 14:45:40 +0800 | [diff] [blame] | 4289 | err = btrfs_run_qgroups(trans); |
Qu Wenruo | e082f56 | 2015-02-27 16:24:28 +0800 | [diff] [blame] | 4290 | if (err < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4291 | btrfs_handle_fs_error(fs_info, err, |
| 4292 | "failed to update qgroup status and info"); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4293 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4294 | if (err && !ret) |
| 4295 | ret = err; |
| 4296 | |
| 4297 | out: |
| 4298 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4299 | drop_write: |
| 4300 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4301 | return ret; |
| 4302 | } |
| 4303 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4304 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4305 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4306 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4307 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4308 | struct btrfs_ioctl_qgroup_create_args *sa; |
| 4309 | struct btrfs_trans_handle *trans; |
| 4310 | int ret; |
| 4311 | int err; |
| 4312 | |
| 4313 | if (!capable(CAP_SYS_ADMIN)) |
| 4314 | return -EPERM; |
| 4315 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4316 | ret = mnt_want_write_file(file); |
| 4317 | if (ret) |
| 4318 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4319 | |
| 4320 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4321 | if (IS_ERR(sa)) { |
| 4322 | ret = PTR_ERR(sa); |
| 4323 | goto drop_write; |
| 4324 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4325 | |
Miao Xie | d86e56c | 2012-11-15 11:35:41 +0000 | [diff] [blame] | 4326 | if (!sa->qgroupid) { |
| 4327 | ret = -EINVAL; |
| 4328 | goto out; |
| 4329 | } |
| 4330 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4331 | trans = btrfs_join_transaction(root); |
| 4332 | if (IS_ERR(trans)) { |
| 4333 | ret = PTR_ERR(trans); |
| 4334 | goto out; |
| 4335 | } |
| 4336 | |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4337 | if (sa->create) { |
Lu Fengqi | 49a05ec | 2018-07-18 14:45:33 +0800 | [diff] [blame] | 4338 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4339 | } else { |
Lu Fengqi | 3efbee1 | 2018-07-18 14:45:34 +0800 | [diff] [blame] | 4340 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4341 | } |
| 4342 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4343 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4344 | if (err && !ret) |
| 4345 | ret = err; |
| 4346 | |
| 4347 | out: |
| 4348 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4349 | drop_write: |
| 4350 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4351 | return ret; |
| 4352 | } |
| 4353 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4354 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4355 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4356 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4357 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4358 | struct btrfs_ioctl_qgroup_limit_args *sa; |
| 4359 | struct btrfs_trans_handle *trans; |
| 4360 | int ret; |
| 4361 | int err; |
| 4362 | u64 qgroupid; |
| 4363 | |
| 4364 | if (!capable(CAP_SYS_ADMIN)) |
| 4365 | return -EPERM; |
| 4366 | |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4367 | ret = mnt_want_write_file(file); |
| 4368 | if (ret) |
| 4369 | return ret; |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4370 | |
| 4371 | sa = memdup_user(arg, sizeof(*sa)); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4372 | if (IS_ERR(sa)) { |
| 4373 | ret = PTR_ERR(sa); |
| 4374 | goto drop_write; |
| 4375 | } |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4376 | |
| 4377 | trans = btrfs_join_transaction(root); |
| 4378 | if (IS_ERR(trans)) { |
| 4379 | ret = PTR_ERR(trans); |
| 4380 | goto out; |
| 4381 | } |
| 4382 | |
| 4383 | qgroupid = sa->qgroupid; |
| 4384 | if (!qgroupid) { |
| 4385 | /* take the current subvol as qgroup */ |
| 4386 | qgroupid = root->root_key.objectid; |
| 4387 | } |
| 4388 | |
Lu Fengqi | f0042d5 | 2018-07-18 14:45:35 +0800 | [diff] [blame] | 4389 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4390 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4391 | err = btrfs_end_transaction(trans); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4392 | if (err && !ret) |
| 4393 | ret = err; |
| 4394 | |
| 4395 | out: |
| 4396 | kfree(sa); |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 4397 | drop_write: |
| 4398 | mnt_drop_write_file(file); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 4399 | return ret; |
| 4400 | } |
| 4401 | |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4402 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
| 4403 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4404 | struct inode *inode = file_inode(file); |
| 4405 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4406 | struct btrfs_ioctl_quota_rescan_args *qsa; |
| 4407 | int ret; |
| 4408 | |
| 4409 | if (!capable(CAP_SYS_ADMIN)) |
| 4410 | return -EPERM; |
| 4411 | |
| 4412 | ret = mnt_want_write_file(file); |
| 4413 | if (ret) |
| 4414 | return ret; |
| 4415 | |
| 4416 | qsa = memdup_user(arg, sizeof(*qsa)); |
| 4417 | if (IS_ERR(qsa)) { |
| 4418 | ret = PTR_ERR(qsa); |
| 4419 | goto drop_write; |
| 4420 | } |
| 4421 | |
| 4422 | if (qsa->flags) { |
| 4423 | ret = -EINVAL; |
| 4424 | goto out; |
| 4425 | } |
| 4426 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4427 | ret = btrfs_qgroup_rescan(fs_info); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4428 | |
| 4429 | out: |
| 4430 | kfree(qsa); |
| 4431 | drop_write: |
| 4432 | mnt_drop_write_file(file); |
| 4433 | return ret; |
| 4434 | } |
| 4435 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4436 | static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info, |
| 4437 | void __user *arg) |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4438 | { |
Goldwyn Rodrigues | 0afb603 | 2021-07-27 16:17:29 -0500 | [diff] [blame] | 4439 | struct btrfs_ioctl_quota_rescan_args qsa = {0}; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (!capable(CAP_SYS_ADMIN)) |
| 4442 | return -EPERM; |
| 4443 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4444 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
Goldwyn Rodrigues | 0afb603 | 2021-07-27 16:17:29 -0500 | [diff] [blame] | 4445 | qsa.flags = 1; |
| 4446 | qsa.progress = fs_info->qgroup_rescan_progress.objectid; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Goldwyn Rodrigues | 0afb603 | 2021-07-27 16:17:29 -0500 | [diff] [blame] | 4449 | if (copy_to_user(arg, &qsa, sizeof(qsa))) |
Anand Jain | 991a3da | 2021-07-28 14:20:41 +0800 | [diff] [blame] | 4450 | return -EFAULT; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4451 | |
Anand Jain | 991a3da | 2021-07-28 14:20:41 +0800 | [diff] [blame] | 4452 | return 0; |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4455 | static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info, |
| 4456 | void __user *arg) |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4457 | { |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4458 | if (!capable(CAP_SYS_ADMIN)) |
| 4459 | return -EPERM; |
| 4460 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4461 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4464 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
Christian Brauner | e4fed17 | 2021-07-27 12:48:55 +0200 | [diff] [blame] | 4465 | struct user_namespace *mnt_userns, |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4466 | struct btrfs_ioctl_received_subvol_args *sa) |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4467 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4468 | struct inode *inode = file_inode(file); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4469 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4470 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4471 | struct btrfs_root_item *root_item = &root->root_item; |
| 4472 | struct btrfs_trans_handle *trans; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 4473 | struct timespec64 ct = current_time(inode); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4474 | int ret = 0; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4475 | int received_uuid_changed; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4476 | |
Christian Brauner | e4fed17 | 2021-07-27 12:48:55 +0200 | [diff] [blame] | 4477 | if (!inode_owner_or_capable(mnt_userns, inode)) |
David Sterba | bd60ea0 | 2014-01-16 15:50:22 +0100 | [diff] [blame] | 4478 | return -EPERM; |
| 4479 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4480 | ret = mnt_want_write_file(file); |
| 4481 | if (ret < 0) |
| 4482 | return ret; |
| 4483 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4484 | down_write(&fs_info->subvol_sem); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4485 | |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 4486 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4487 | ret = -EINVAL; |
| 4488 | goto out; |
| 4489 | } |
| 4490 | |
| 4491 | if (btrfs_root_readonly(root)) { |
| 4492 | ret = -EROFS; |
| 4493 | goto out; |
| 4494 | } |
| 4495 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4496 | /* |
| 4497 | * 1 - root item |
| 4498 | * 2 - uuid items (received uuid + subvol uuid) |
| 4499 | */ |
| 4500 | trans = btrfs_start_transaction(root, 3); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4501 | if (IS_ERR(trans)) { |
| 4502 | ret = PTR_ERR(trans); |
| 4503 | trans = NULL; |
| 4504 | goto out; |
| 4505 | } |
| 4506 | |
| 4507 | sa->rtransid = trans->transid; |
| 4508 | sa->rtime.sec = ct.tv_sec; |
| 4509 | sa->rtime.nsec = ct.tv_nsec; |
| 4510 | |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4511 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
| 4512 | BTRFS_UUID_SIZE); |
| 4513 | if (received_uuid_changed && |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 4514 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
Lu Fengqi | d195779 | 2018-05-29 15:01:54 +0800 | [diff] [blame] | 4515 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
Nikolay Borisov | d87ff75 | 2018-03-12 14:48:09 +0200 | [diff] [blame] | 4516 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4517 | root->root_key.objectid); |
| 4518 | if (ret && ret != -ENOENT) { |
| 4519 | btrfs_abort_transaction(trans, ret); |
| 4520 | btrfs_end_transaction(trans); |
| 4521 | goto out; |
| 4522 | } |
| 4523 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4524 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
| 4525 | btrfs_set_root_stransid(root_item, sa->stransid); |
| 4526 | btrfs_set_root_rtransid(root_item, sa->rtransid); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 4527 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
| 4528 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); |
| 4529 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); |
| 4530 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4531 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4532 | ret = btrfs_update_root(trans, fs_info->tree_root, |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4533 | &root->root_key, &root->root_item); |
| 4534 | if (ret < 0) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4535 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4536 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4537 | } |
| 4538 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4539 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4540 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4541 | root->root_key.objectid); |
| 4542 | if (ret < 0 && ret != -EEXIST) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4543 | btrfs_abort_transaction(trans, ret); |
Nikolay Borisov | efd3815 | 2017-09-28 11:45:26 +0300 | [diff] [blame] | 4544 | btrfs_end_transaction(trans); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4545 | goto out; |
Stefan Behrens | dd5f961 | 2013-08-15 17:11:20 +0200 | [diff] [blame] | 4546 | } |
| 4547 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4548 | ret = btrfs_commit_transaction(trans); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4549 | out: |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4550 | up_write(&fs_info->subvol_sem); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4551 | mnt_drop_write_file(file); |
| 4552 | return ret; |
| 4553 | } |
| 4554 | |
| 4555 | #ifdef CONFIG_64BIT |
| 4556 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, |
| 4557 | void __user *arg) |
| 4558 | { |
| 4559 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; |
| 4560 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; |
| 4561 | int ret = 0; |
| 4562 | |
| 4563 | args32 = memdup_user(arg, sizeof(*args32)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 4564 | if (IS_ERR(args32)) |
| 4565 | return PTR_ERR(args32); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4566 | |
David Sterba | 8d2db78 | 2015-11-04 15:38:29 +0100 | [diff] [blame] | 4567 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
Dan Carpenter | 84dbeb8 | 2014-03-28 11:06:00 +0300 | [diff] [blame] | 4568 | if (!args64) { |
| 4569 | ret = -ENOMEM; |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4570 | goto out; |
| 4571 | } |
| 4572 | |
| 4573 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); |
| 4574 | args64->stransid = args32->stransid; |
| 4575 | args64->rtransid = args32->rtransid; |
| 4576 | args64->stime.sec = args32->stime.sec; |
| 4577 | args64->stime.nsec = args32->stime.nsec; |
| 4578 | args64->rtime.sec = args32->rtime.sec; |
| 4579 | args64->rtime.nsec = args32->rtime.nsec; |
| 4580 | args64->flags = args32->flags; |
| 4581 | |
Christian Brauner | e4fed17 | 2021-07-27 12:48:55 +0200 | [diff] [blame] | 4582 | ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4583 | if (ret) |
| 4584 | goto out; |
| 4585 | |
| 4586 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); |
| 4587 | args32->stransid = args64->stransid; |
| 4588 | args32->rtransid = args64->rtransid; |
| 4589 | args32->stime.sec = args64->stime.sec; |
| 4590 | args32->stime.nsec = args64->stime.nsec; |
| 4591 | args32->rtime.sec = args64->rtime.sec; |
| 4592 | args32->rtime.nsec = args64->rtime.nsec; |
| 4593 | args32->flags = args64->flags; |
| 4594 | |
| 4595 | ret = copy_to_user(arg, args32, sizeof(*args32)); |
| 4596 | if (ret) |
| 4597 | ret = -EFAULT; |
| 4598 | |
| 4599 | out: |
| 4600 | kfree(args32); |
| 4601 | kfree(args64); |
| 4602 | return ret; |
| 4603 | } |
| 4604 | #endif |
| 4605 | |
| 4606 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
| 4607 | void __user *arg) |
| 4608 | { |
| 4609 | struct btrfs_ioctl_received_subvol_args *sa = NULL; |
| 4610 | int ret = 0; |
| 4611 | |
| 4612 | sa = memdup_user(arg, sizeof(*sa)); |
Shailendra Verma | 7b9ea62 | 2016-11-10 15:17:41 +0530 | [diff] [blame] | 4613 | if (IS_ERR(sa)) |
| 4614 | return PTR_ERR(sa); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4615 | |
Christian Brauner | e4fed17 | 2021-07-27 12:48:55 +0200 | [diff] [blame] | 4616 | ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 4617 | |
| 4618 | if (ret) |
| 4619 | goto out; |
| 4620 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4621 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
| 4622 | if (ret) |
| 4623 | ret = -EFAULT; |
| 4624 | |
| 4625 | out: |
| 4626 | kfree(sa); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 4627 | return ret; |
| 4628 | } |
| 4629 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4630 | static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info, |
| 4631 | void __user *arg) |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4632 | { |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4633 | size_t len; |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4634 | int ret; |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4635 | char label[BTRFS_LABEL_SIZE]; |
| 4636 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4637 | spin_lock(&fs_info->super_lock); |
| 4638 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); |
| 4639 | spin_unlock(&fs_info->super_lock); |
Anand Jain | a1b83ac | 2013-07-19 17:39:32 +0800 | [diff] [blame] | 4640 | |
| 4641 | len = strnlen(label, BTRFS_LABEL_SIZE); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4642 | |
| 4643 | if (len == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4644 | btrfs_warn(fs_info, |
| 4645 | "label is too long, return the first %zu bytes", |
| 4646 | --len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4649 | ret = copy_to_user(arg, label, len); |
jeff.liu | 867ab66 | 2013-01-05 02:48:01 +0000 | [diff] [blame] | 4650 | |
| 4651 | return ret ? -EFAULT : 0; |
| 4652 | } |
| 4653 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4654 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
| 4655 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4656 | struct inode *inode = file_inode(file); |
| 4657 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4658 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4659 | struct btrfs_super_block *super_block = fs_info->super_copy; |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4660 | struct btrfs_trans_handle *trans; |
| 4661 | char label[BTRFS_LABEL_SIZE]; |
| 4662 | int ret; |
| 4663 | |
| 4664 | if (!capable(CAP_SYS_ADMIN)) |
| 4665 | return -EPERM; |
| 4666 | |
| 4667 | if (copy_from_user(label, arg, sizeof(label))) |
| 4668 | return -EFAULT; |
| 4669 | |
| 4670 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4671 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4672 | "unable to set label with more than %d bytes", |
| 4673 | BTRFS_LABEL_SIZE - 1); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4674 | return -EINVAL; |
| 4675 | } |
| 4676 | |
| 4677 | ret = mnt_want_write_file(file); |
| 4678 | if (ret) |
| 4679 | return ret; |
| 4680 | |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4681 | trans = btrfs_start_transaction(root, 0); |
| 4682 | if (IS_ERR(trans)) { |
| 4683 | ret = PTR_ERR(trans); |
| 4684 | goto out_unlock; |
| 4685 | } |
| 4686 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4687 | spin_lock(&fs_info->super_lock); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4688 | strcpy(super_block->label, label); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4689 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4690 | ret = btrfs_commit_transaction(trans); |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4691 | |
| 4692 | out_unlock: |
jeff.liu | a8bfd4a | 2013-01-05 02:48:08 +0000 | [diff] [blame] | 4693 | mnt_drop_write_file(file); |
| 4694 | return ret; |
| 4695 | } |
| 4696 | |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4697 | #define INIT_FEATURE_FLAGS(suffix) \ |
| 4698 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ |
| 4699 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ |
| 4700 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } |
| 4701 | |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 4702 | int btrfs_ioctl_get_supported_features(void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4703 | { |
David Sterba | 4d4ab6d | 2015-11-19 11:42:31 +0100 | [diff] [blame] | 4704 | static const struct btrfs_ioctl_feature_flags features[3] = { |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4705 | INIT_FEATURE_FLAGS(SUPP), |
| 4706 | INIT_FEATURE_FLAGS(SAFE_SET), |
| 4707 | INIT_FEATURE_FLAGS(SAFE_CLEAR) |
| 4708 | }; |
| 4709 | |
| 4710 | if (copy_to_user(arg, &features, sizeof(features))) |
| 4711 | return -EFAULT; |
| 4712 | |
| 4713 | return 0; |
| 4714 | } |
| 4715 | |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4716 | static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info, |
| 4717 | void __user *arg) |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4718 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4719 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4720 | struct btrfs_ioctl_feature_flags features; |
| 4721 | |
| 4722 | features.compat_flags = btrfs_super_compat_flags(super_block); |
| 4723 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); |
| 4724 | features.incompat_flags = btrfs_super_incompat_flags(super_block); |
| 4725 | |
| 4726 | if (copy_to_user(arg, &features, sizeof(features))) |
| 4727 | return -EFAULT; |
| 4728 | |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4732 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4733 | enum btrfs_feature_set set, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4734 | u64 change_mask, u64 flags, u64 supported_flags, |
| 4735 | u64 safe_set, u64 safe_clear) |
| 4736 | { |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 4737 | const char *type = btrfs_feature_set_name(set); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4738 | char *names; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4739 | u64 disallowed, unsupported; |
| 4740 | u64 set_mask = flags & change_mask; |
| 4741 | u64 clear_mask = ~flags & change_mask; |
| 4742 | |
| 4743 | unsupported = set_mask & ~supported_flags; |
| 4744 | if (unsupported) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4745 | names = btrfs_printable_features(set, unsupported); |
| 4746 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4747 | btrfs_warn(fs_info, |
| 4748 | "this kernel does not support the %s feature bit%s", |
| 4749 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4750 | kfree(names); |
| 4751 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4752 | btrfs_warn(fs_info, |
| 4753 | "this kernel does not support %s bits 0x%llx", |
| 4754 | type, unsupported); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4755 | return -EOPNOTSUPP; |
| 4756 | } |
| 4757 | |
| 4758 | disallowed = set_mask & ~safe_set; |
| 4759 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4760 | names = btrfs_printable_features(set, disallowed); |
| 4761 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4762 | btrfs_warn(fs_info, |
| 4763 | "can't set the %s feature bit%s while mounted", |
| 4764 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4765 | kfree(names); |
| 4766 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4767 | btrfs_warn(fs_info, |
| 4768 | "can't set %s bits 0x%llx while mounted", |
| 4769 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4770 | return -EPERM; |
| 4771 | } |
| 4772 | |
| 4773 | disallowed = clear_mask & ~safe_clear; |
| 4774 | if (disallowed) { |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4775 | names = btrfs_printable_features(set, disallowed); |
| 4776 | if (names) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4777 | btrfs_warn(fs_info, |
| 4778 | "can't clear the %s feature bit%s while mounted", |
| 4779 | names, strchr(names, ',') ? "s" : ""); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 4780 | kfree(names); |
| 4781 | } else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4782 | btrfs_warn(fs_info, |
| 4783 | "can't clear %s bits 0x%llx while mounted", |
| 4784 | type, disallowed); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4785 | return -EPERM; |
| 4786 | } |
| 4787 | |
| 4788 | return 0; |
| 4789 | } |
| 4790 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4791 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
| 4792 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4793 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
| 4794 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ |
| 4795 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) |
| 4796 | |
| 4797 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) |
| 4798 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4799 | struct inode *inode = file_inode(file); |
| 4800 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4801 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4802 | struct btrfs_super_block *super_block = fs_info->super_copy; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4803 | struct btrfs_ioctl_feature_flags flags[2]; |
| 4804 | struct btrfs_trans_handle *trans; |
| 4805 | u64 newflags; |
| 4806 | int ret; |
| 4807 | |
| 4808 | if (!capable(CAP_SYS_ADMIN)) |
| 4809 | return -EPERM; |
| 4810 | |
| 4811 | if (copy_from_user(flags, arg, sizeof(flags))) |
| 4812 | return -EFAULT; |
| 4813 | |
| 4814 | /* Nothing to do */ |
| 4815 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && |
| 4816 | !flags[0].incompat_flags) |
| 4817 | return 0; |
| 4818 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4819 | ret = check_feature(fs_info, flags[0].compat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4820 | flags[1].compat_flags, COMPAT); |
| 4821 | if (ret) |
| 4822 | return ret; |
| 4823 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4824 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4825 | flags[1].compat_ro_flags, COMPAT_RO); |
| 4826 | if (ret) |
| 4827 | return ret; |
| 4828 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4829 | ret = check_feature(fs_info, flags[0].incompat_flags, |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4830 | flags[1].incompat_flags, INCOMPAT); |
| 4831 | if (ret) |
| 4832 | return ret; |
| 4833 | |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4834 | ret = mnt_want_write_file(file); |
| 4835 | if (ret) |
| 4836 | return ret; |
| 4837 | |
David Sterba | 8051aa1 | 2014-02-07 14:34:04 +0100 | [diff] [blame] | 4838 | trans = btrfs_start_transaction(root, 0); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4839 | if (IS_ERR(trans)) { |
| 4840 | ret = PTR_ERR(trans); |
| 4841 | goto out_drop_write; |
| 4842 | } |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4843 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4844 | spin_lock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4845 | newflags = btrfs_super_compat_flags(super_block); |
| 4846 | newflags |= flags[0].compat_flags & flags[1].compat_flags; |
| 4847 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); |
| 4848 | btrfs_set_super_compat_flags(super_block, newflags); |
| 4849 | |
| 4850 | newflags = btrfs_super_compat_ro_flags(super_block); |
| 4851 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; |
| 4852 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); |
| 4853 | btrfs_set_super_compat_ro_flags(super_block, newflags); |
| 4854 | |
| 4855 | newflags = btrfs_super_incompat_flags(super_block); |
| 4856 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; |
| 4857 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); |
| 4858 | btrfs_set_super_incompat_flags(super_block, newflags); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4859 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4860 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4861 | ret = btrfs_commit_transaction(trans); |
David Sterba | 7ab1962 | 2016-05-04 11:32:00 +0200 | [diff] [blame] | 4862 | out_drop_write: |
| 4863 | mnt_drop_write_file(file); |
| 4864 | |
| 4865 | return ret; |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 4866 | } |
| 4867 | |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 4868 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
| 4869 | { |
| 4870 | struct btrfs_ioctl_send_args *arg; |
| 4871 | int ret; |
| 4872 | |
| 4873 | if (compat) { |
| 4874 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 4875 | struct btrfs_ioctl_send_args_32 args32; |
| 4876 | |
| 4877 | ret = copy_from_user(&args32, argp, sizeof(args32)); |
| 4878 | if (ret) |
| 4879 | return -EFAULT; |
| 4880 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); |
| 4881 | if (!arg) |
| 4882 | return -ENOMEM; |
| 4883 | arg->send_fd = args32.send_fd; |
| 4884 | arg->clone_sources_count = args32.clone_sources_count; |
| 4885 | arg->clone_sources = compat_ptr(args32.clone_sources); |
| 4886 | arg->parent_root = args32.parent_root; |
| 4887 | arg->flags = args32.flags; |
| 4888 | memcpy(arg->reserved, args32.reserved, |
| 4889 | sizeof(args32.reserved)); |
| 4890 | #else |
| 4891 | return -ENOTTY; |
| 4892 | #endif |
| 4893 | } else { |
| 4894 | arg = memdup_user(argp, sizeof(*arg)); |
| 4895 | if (IS_ERR(arg)) |
| 4896 | return PTR_ERR(arg); |
| 4897 | } |
| 4898 | ret = btrfs_ioctl_send(file, arg); |
| 4899 | kfree(arg); |
| 4900 | return ret; |
| 4901 | } |
| 4902 | |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4903 | long btrfs_ioctl(struct file *file, unsigned int |
| 4904 | cmd, unsigned long arg) |
| 4905 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4906 | struct inode *inode = file_inode(file); |
| 4907 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
| 4908 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Christoph Hellwig | 4bcabaa | 2008-12-02 06:36:08 -0500 | [diff] [blame] | 4909 | void __user *argp = (void __user *)arg; |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4910 | |
| 4911 | switch (cmd) { |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 4912 | case FS_IOC_GETVERSION: |
| 4913 | return btrfs_ioctl_getversion(file, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 4914 | case FS_IOC_GETFSLABEL: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4915 | return btrfs_ioctl_get_fslabel(fs_info, argp); |
Eric Sandeen | 40cf931 | 2019-07-17 12:39:20 -0500 | [diff] [blame] | 4916 | case FS_IOC_SETFSLABEL: |
| 4917 | return btrfs_ioctl_set_fslabel(file, argp); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 4918 | case FITRIM: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 4919 | return btrfs_ioctl_fitrim(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4920 | case BTRFS_IOC_SNAP_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4921 | return btrfs_ioctl_snap_create(file, argp, 0); |
Li Zefan | fdfb1e4 | 2010-12-10 06:41:56 +0000 | [diff] [blame] | 4922 | case BTRFS_IOC_SNAP_CREATE_V2: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4923 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
Chris Mason | 3de4586 | 2008-11-17 21:02:50 -0500 | [diff] [blame] | 4924 | case BTRFS_IOC_SUBVOL_CREATE: |
Li Zefan | fa0d2b9 | 2010-12-20 15:53:28 +0800 | [diff] [blame] | 4925 | return btrfs_ioctl_snap_create(file, argp, 1); |
Arne Jansen | 6f72c7e | 2011-09-14 15:58:21 +0200 | [diff] [blame] | 4926 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
| 4927 | return btrfs_ioctl_snap_create_v2(file, argp, 1); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 4928 | case BTRFS_IOC_SNAP_DESTROY: |
Marcos Paulo de Souza | 949964c | 2020-02-07 10:05:46 -0300 | [diff] [blame] | 4929 | return btrfs_ioctl_snap_destroy(file, argp, false); |
| 4930 | case BTRFS_IOC_SNAP_DESTROY_V2: |
| 4931 | return btrfs_ioctl_snap_destroy(file, argp, true); |
Li Zefan | 0caa102 | 2010-12-20 16:30:25 +0800 | [diff] [blame] | 4932 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
| 4933 | return btrfs_ioctl_subvol_getflags(file, argp); |
| 4934 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
| 4935 | return btrfs_ioctl_subvol_setflags(file, argp); |
Josef Bacik | 6ef5ed0 | 2009-12-11 21:11:29 +0000 | [diff] [blame] | 4936 | case BTRFS_IOC_DEFAULT_SUBVOL: |
| 4937 | return btrfs_ioctl_default_subvol(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4938 | case BTRFS_IOC_DEFRAG: |
Chris Mason | 1e701a3 | 2010-03-11 09:42:04 -0500 | [diff] [blame] | 4939 | return btrfs_ioctl_defrag(file, NULL); |
| 4940 | case BTRFS_IOC_DEFRAG_RANGE: |
| 4941 | return btrfs_ioctl_defrag(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4942 | case BTRFS_IOC_RESIZE: |
Miao Xie | 198605a | 2012-11-26 08:43:45 +0000 | [diff] [blame] | 4943 | return btrfs_ioctl_resize(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4944 | case BTRFS_IOC_ADD_DEV: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4945 | return btrfs_ioctl_add_dev(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4946 | case BTRFS_IOC_RM_DEV: |
Miao Xie | da24927 | 2012-11-26 08:44:50 +0000 | [diff] [blame] | 4947 | return btrfs_ioctl_rm_dev(file, argp); |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame] | 4948 | case BTRFS_IOC_RM_DEV_V2: |
| 4949 | return btrfs_ioctl_rm_dev_v2(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4950 | case BTRFS_IOC_FS_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4951 | return btrfs_ioctl_fs_info(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4952 | case BTRFS_IOC_DEV_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4953 | return btrfs_ioctl_dev_info(fs_info, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 4954 | case BTRFS_IOC_BALANCE: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4955 | return btrfs_ioctl_balance(file, NULL); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 4956 | case BTRFS_IOC_TREE_SEARCH: |
| 4957 | return btrfs_ioctl_tree_search(file, argp); |
Gerhard Heift | cc68a8a | 2014-01-30 16:24:03 +0100 | [diff] [blame] | 4958 | case BTRFS_IOC_TREE_SEARCH_V2: |
| 4959 | return btrfs_ioctl_tree_search_v2(file, argp); |
Chris Mason | ac8e981 | 2010-02-28 15:39:26 -0500 | [diff] [blame] | 4960 | case BTRFS_IOC_INO_LOOKUP: |
| 4961 | return btrfs_ioctl_ino_lookup(file, argp); |
Jan Schmidt | d7728c9 | 2011-07-07 16:48:38 +0200 | [diff] [blame] | 4962 | case BTRFS_IOC_INO_PATHS: |
| 4963 | return btrfs_ioctl_ino_to_path(root, argp); |
| 4964 | case BTRFS_IOC_LOGICAL_INO: |
Zygo Blaxell | d24a67b | 2017-09-22 13:58:46 -0400 | [diff] [blame] | 4965 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
| 4966 | case BTRFS_IOC_LOGICAL_INO_V2: |
| 4967 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); |
Josef Bacik | 1406e43 | 2010-01-13 18:19:06 +0000 | [diff] [blame] | 4968 | case BTRFS_IOC_SPACE_INFO: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4969 | return btrfs_ioctl_space_info(fs_info, argp); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4970 | case BTRFS_IOC_SYNC: { |
| 4971 | int ret; |
| 4972 | |
Nikolay Borisov | 9db4dc2 | 2021-01-11 12:58:11 +0200 | [diff] [blame] | 4973 | ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4974 | if (ret) |
| 4975 | return ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4976 | ret = btrfs_sync_fs(inode->i_sb, 1); |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 4977 | /* |
| 4978 | * The transaction thread may want to do more work, |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 4979 | * namely it pokes the cleaner kthread that will start |
David Sterba | 2fad4e8 | 2014-07-23 14:39:35 +0200 | [diff] [blame] | 4980 | * processing uncleaned subvols. |
| 4981 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4982 | wake_up_process(fs_info->transaction_kthread); |
Filipe David Borba Manana | 9b19985 | 2013-09-23 11:35:11 +0100 | [diff] [blame] | 4983 | return ret; |
| 4984 | } |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4985 | case BTRFS_IOC_START_SYNC: |
Miao Xie | 9a8c28b | 2012-11-26 08:40:43 +0000 | [diff] [blame] | 4986 | return btrfs_ioctl_start_sync(root, argp); |
Sage Weil | 4620459 | 2010-10-29 15:41:32 -0400 | [diff] [blame] | 4987 | case BTRFS_IOC_WAIT_SYNC: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4988 | return btrfs_ioctl_wait_sync(fs_info, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4989 | case BTRFS_IOC_SCRUB: |
Miao Xie | b8e9548 | 2012-11-26 08:48:01 +0000 | [diff] [blame] | 4990 | return btrfs_ioctl_scrub(file, argp); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4991 | case BTRFS_IOC_SCRUB_CANCEL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4992 | return btrfs_ioctl_scrub_cancel(fs_info); |
Jan Schmidt | 475f638 | 2011-03-11 15:41:01 +0100 | [diff] [blame] | 4993 | case BTRFS_IOC_SCRUB_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4994 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4995 | case BTRFS_IOC_BALANCE_V2: |
Liu Bo | 9ba1f6e | 2012-05-11 18:11:26 +0800 | [diff] [blame] | 4996 | return btrfs_ioctl_balance(file, argp); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4997 | case BTRFS_IOC_BALANCE_CTL: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4998 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4999 | case BTRFS_IOC_BALANCE_PROGRESS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5000 | return btrfs_ioctl_balance_progress(fs_info, argp); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 5001 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
| 5002 | return btrfs_ioctl_set_received_subvol(file, argp); |
Hugo Mills | abccd00 | 2014-01-30 20:17:00 +0000 | [diff] [blame] | 5003 | #ifdef CONFIG_64BIT |
| 5004 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: |
| 5005 | return btrfs_ioctl_set_received_subvol_32(file, argp); |
| 5006 | #endif |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5007 | case BTRFS_IOC_SEND: |
Josef Bacik | 2351f43 | 2017-09-27 10:43:13 -0400 | [diff] [blame] | 5008 | return _btrfs_ioctl_send(file, argp, false); |
| 5009 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
| 5010 | case BTRFS_IOC_SEND_32: |
| 5011 | return _btrfs_ioctl_send(file, argp, true); |
| 5012 | #endif |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 5013 | case BTRFS_IOC_GET_DEV_STATS: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5014 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5015 | case BTRFS_IOC_QUOTA_CTL: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5016 | return btrfs_ioctl_quota_ctl(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5017 | case BTRFS_IOC_QGROUP_ASSIGN: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5018 | return btrfs_ioctl_qgroup_assign(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5019 | case BTRFS_IOC_QGROUP_CREATE: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5020 | return btrfs_ioctl_qgroup_create(file, argp); |
Arne Jansen | 5d13a37 | 2011-09-14 15:53:51 +0200 | [diff] [blame] | 5021 | case BTRFS_IOC_QGROUP_LIMIT: |
Miao Xie | 905b0dd | 2012-11-26 08:50:11 +0000 | [diff] [blame] | 5022 | return btrfs_ioctl_qgroup_limit(file, argp); |
Jan Schmidt | 2f23203 | 2013-04-25 16:04:51 +0000 | [diff] [blame] | 5023 | case BTRFS_IOC_QUOTA_RESCAN: |
| 5024 | return btrfs_ioctl_quota_rescan(file, argp); |
| 5025 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5026 | return btrfs_ioctl_quota_rescan_status(fs_info, argp); |
Jan Schmidt | 57254b6e | 2013-05-06 19:14:17 +0000 | [diff] [blame] | 5027 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5028 | return btrfs_ioctl_quota_rescan_wait(fs_info, argp); |
Stefan Behrens | 3f6bcfb | 2012-11-06 15:08:53 +0100 | [diff] [blame] | 5029 | case BTRFS_IOC_DEV_REPLACE: |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5030 | return btrfs_ioctl_dev_replace(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5031 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
David Sterba | d5131b6 | 2016-02-17 15:26:27 +0100 | [diff] [blame] | 5032 | return btrfs_ioctl_get_supported_features(argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5033 | case BTRFS_IOC_GET_FEATURES: |
Marcos Paulo de Souza | b929c1d | 2019-10-10 21:23:11 -0300 | [diff] [blame] | 5034 | return btrfs_ioctl_get_features(fs_info, argp); |
Jeff Mahoney | 2eaa055 | 2013-11-15 15:33:55 -0500 | [diff] [blame] | 5035 | case BTRFS_IOC_SET_FEATURES: |
| 5036 | return btrfs_ioctl_set_features(file, argp); |
Tomohiro Misono | b64ec07 | 2018-05-21 10:09:42 +0900 | [diff] [blame] | 5037 | case BTRFS_IOC_GET_SUBVOL_INFO: |
| 5038 | return btrfs_ioctl_get_subvol_info(file, argp); |
Tomohiro Misono | 42e4b52 | 2018-05-21 10:09:43 +0900 | [diff] [blame] | 5039 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
| 5040 | return btrfs_ioctl_get_subvol_rootref(file, argp); |
Tomohiro Misono | 23d0b79 | 2018-05-21 10:09:44 +0900 | [diff] [blame] | 5041 | case BTRFS_IOC_INO_LOOKUP_USER: |
| 5042 | return btrfs_ioctl_ino_lookup_user(file, argp); |
Boris Burkov | 1460540 | 2021-06-30 13:01:49 -0700 | [diff] [blame] | 5043 | case FS_IOC_ENABLE_VERITY: |
| 5044 | return fsverity_ioctl_enable(file, (const void __user *)argp); |
| 5045 | case FS_IOC_MEASURE_VERITY: |
| 5046 | return fsverity_ioctl_measure(file, argp); |
Christoph Hellwig | f46b5a6 | 2008-06-11 21:53:53 -0400 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | return -ENOTTY; |
| 5050 | } |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5051 | |
| 5052 | #ifdef CONFIG_COMPAT |
| 5053 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 5054 | { |
Jeff Mahoney | 2a36224 | 2017-02-06 19:39:09 -0500 | [diff] [blame] | 5055 | /* |
| 5056 | * These all access 32-bit values anyway so no further |
| 5057 | * handling is necessary. |
| 5058 | */ |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5059 | switch (cmd) { |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5060 | case FS_IOC32_GETVERSION: |
| 5061 | cmd = FS_IOC_GETVERSION; |
| 5062 | break; |
Luke Dashjr | 4c63c24 | 2015-10-29 08:22:21 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 5066 | } |
| 5067 | #endif |