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