blob: c474f7e2416328eecb5fda0db1b00f973b2a4885 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Miklos Szeredi97fc2972021-04-07 14:36:43 +020029#include <linux/fileattr.h>
Boris Burkov14605402021-06-30 13:01:49 -070030#include <linux/fsverity.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040031#include "ctree.h"
32#include "disk-io.h"
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -030033#include "export.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "transaction.h"
35#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040036#include "print-tree.h"
37#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040038#include "locking.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020039#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020041#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010042#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000043#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040044#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070045#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000046#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080047#include "compression.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040048#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040049#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040050#include "block-group.h"
Qu Wenruo22b398e2021-08-06 16:12:37 +080051#include "subpage.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Hugo Millsabccd002014-01-30 20:17:00 +000053#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 */
59struct btrfs_ioctl_timespec_32 {
60 __u64 sec;
61 __u32 nsec;
62} __attribute__ ((__packed__));
63
64struct 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 Bacik2351f432017-09-27 10:43:13 -040078#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
79struct 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 */
85 __u64 reserved[4]; /* in */
86} __attribute__ ((__packed__));
87
88#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
89 struct btrfs_ioctl_send_args_32)
90#endif
Hugo Millsabccd002014-01-30 20:17:00 +000091
Christoph Hellwig6cbff002009-04-17 10:37:41 +020092/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020093static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
94 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095{
David Sterba1905a0f2018-03-26 18:52:15 +020096 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020098 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020099 return flags & ~FS_DIRSYNC_FL;
100 else
101 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
102}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400103
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200104/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200105 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
106 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107 */
Boris Burkov77eea052021-06-30 13:01:48 -0700108static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200109{
110 unsigned int iflags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -0700111 u32 flags = binode->flags;
Boris Burkov14605402021-06-30 13:01:49 -0700112 u32 ro_flags = binode->ro_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200113
114 if (flags & BTRFS_INODE_SYNC)
115 iflags |= FS_SYNC_FL;
116 if (flags & BTRFS_INODE_IMMUTABLE)
117 iflags |= FS_IMMUTABLE_FL;
118 if (flags & BTRFS_INODE_APPEND)
119 iflags |= FS_APPEND_FL;
120 if (flags & BTRFS_INODE_NODUMP)
121 iflags |= FS_NODUMP_FL;
122 if (flags & BTRFS_INODE_NOATIME)
123 iflags |= FS_NOATIME_FL;
124 if (flags & BTRFS_INODE_DIRSYNC)
125 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000126 if (flags & BTRFS_INODE_NODATACOW)
127 iflags |= FS_NOCOW_FL;
Boris Burkov14605402021-06-30 13:01:49 -0700128 if (ro_flags & BTRFS_INODE_RO_VERITY)
129 iflags |= FS_VERITY_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000130
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900131 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000132 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900133 else if (flags & BTRFS_INODE_COMPRESS)
134 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200135
136 return iflags;
137}
138
139/*
140 * Update inode->i_flags based on the btrfs internal flags.
141 */
David Sterba7b6a2212018-03-26 18:40:21 +0200142void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143{
David Sterba5c57b8b2018-04-23 15:45:18 +0200144 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100145 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200151 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200153 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100154 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200155 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100156 new_fl |= S_DIRSYNC;
Boris Burkov14605402021-06-30 13:01:49 -0700157 if (binode->ro_flags & BTRFS_INODE_RO_VERITY)
158 new_fl |= S_VERITY;
Filipe Manana3cc79392014-06-25 22:36:02 +0100159
160 set_mask_bits(&inode->i_flags,
Boris Burkov14605402021-06-30 13:01:49 -0700161 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
162 S_VERITY, new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200163}
164
David Sterbaf37c5632020-07-10 09:49:56 +0200165/*
166 * Check if @flags are a supported and valid set of FS_*_FL flags and that
167 * the old and new flags are not conflicting
168 */
169static int check_fsflags(unsigned int old_flags, unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000170{
171 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
172 FS_NOATIME_FL | FS_NODUMP_FL | \
173 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000174 FS_NOCOMP_FL | FS_COMPR_FL |
175 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000176 return -EOPNOTSUPP;
177
David Sterbaf37c5632020-07-10 09:49:56 +0200178 /* COMPR and NOCOMP on new/old are valid */
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
180 return -EINVAL;
181
David Sterbaf37c5632020-07-10 09:49:56 +0200182 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
183 return -EINVAL;
184
185 /* NOCOW and compression options are mutually exclusive */
186 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
187 return -EINVAL;
188 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
189 return -EINVAL;
190
Liu Bo75e7cb72011-03-22 10:12:20 +0000191 return 0;
192}
193
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900194static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
195 unsigned int flags)
196{
197 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
198 return -EPERM;
199
200 return 0;
201}
202
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200203/*
204 * Set flags/xflags from the internal inode flags. The remaining items of
205 * fsxattr are zeroed.
206 */
207int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208{
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200209 struct btrfs_inode *binode = BTRFS_I(d_inode(dentry));
210
Boris Burkov77eea052021-06-30 13:01:48 -0700211 fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode));
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200212 return 0;
213}
214
215int btrfs_fileattr_set(struct user_namespace *mnt_userns,
216 struct dentry *dentry, struct fileattr *fa)
217{
218 struct inode *inode = d_inode(dentry);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400219 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200220 struct btrfs_inode *binode = BTRFS_I(inode);
221 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700223 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200224 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800225 const char *comp = NULL;
David Sterbaf37c5632020-07-10 09:49:56 +0200226 u32 binode_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227
Li Zefanb83cc962010-12-20 16:04:08 +0800228 if (btrfs_root_readonly(root))
229 return -EROFS;
230
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200231 if (fileattr_has_fsx(fa))
232 return -EOPNOTSUPP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200234 fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags);
Boris Burkov77eea052021-06-30 13:01:48 -0700235 old_fsflags = btrfs_inode_flags_to_fsflags(binode);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200236 ret = check_fsflags(old_fsflags, fsflags);
Jan Karae7848682012-06-12 16:20:32 +0200237 if (ret)
238 return ret;
239
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900240 ret = check_fsflags_compatible(fs_info, fsflags);
241 if (ret)
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200242 return ret;
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900243
David Sterbaf37c5632020-07-10 09:49:56 +0200244 binode_flags = binode->flags;
David Sterba5c57b8b2018-04-23 15:45:18 +0200245 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800246 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200247 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800248 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200249 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800250 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800252 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200253 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800254 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200255 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800256 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200257 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800258 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200259 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800260 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200261 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800262 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200263 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800264 binode_flags &= ~BTRFS_INODE_NOATIME;
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200265
266 /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
267 if (!fa->flags_valid) {
268 /* 1 item for the inode */
269 trans = btrfs_start_transaction(root, 1);
Ritesh Harjani9b8a2332021-04-30 21:30:55 +0530270 if (IS_ERR(trans))
271 return PTR_ERR(trans);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200272 goto update_flags;
273 }
274
David Sterba5c57b8b2018-04-23 15:45:18 +0200275 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800276 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800278 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200279 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800280 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600281 /*
282 * It's safe to turn csums off here, no extents exist.
283 * Otherwise we want the flag to reflect the real COW
284 * status of the file and will not set it.
285 */
286 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800287 binode_flags |= BTRFS_INODE_NODATACOW |
288 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600289 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800290 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600291 }
292 } else {
293 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400294 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600295 */
Anand Jain44e51942019-04-20 19:48:57 +0800296 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600297 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800298 binode_flags &= ~(BTRFS_INODE_NODATACOW |
299 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600300 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800301 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600302 }
303 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200304
Liu Bo75e7cb72011-03-22 10:12:20 +0000305 /*
306 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
307 * flag may be changed automatically if compression code won't make
308 * things smaller.
309 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200310 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800311 binode_flags &= ~BTRFS_INODE_COMPRESS;
312 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200313 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000314
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200315 if (IS_SWAPFILE(inode))
316 return -ETXTBSY;
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700317
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800318 binode_flags |= BTRFS_INODE_COMPRESS;
319 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000320
David Sterba93370502017-10-31 17:32:41 +0100321 comp = btrfs_compress_type2str(fs_info->compress_type);
322 if (!comp || comp[0] == 0)
323 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000324 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800325 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000326 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Anand Jainff9fef552019-04-20 19:48:53 +0800328 /*
329 * 1 for inode item
330 * 2 for properties
331 */
332 trans = btrfs_start_transaction(root, 3);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200333 if (IS_ERR(trans))
334 return PTR_ERR(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335
Anand Jainff9fef552019-04-20 19:48:53 +0800336 if (comp) {
337 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
338 strlen(comp), 0);
339 if (ret) {
340 btrfs_abort_transaction(trans, ret);
341 goto out_end_trans;
342 }
Anand Jainff9fef552019-04-20 19:48:53 +0800343 } else {
344 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
345 0, 0);
346 if (ret && ret != -ENODATA) {
347 btrfs_abort_transaction(trans, ret);
348 goto out_end_trans;
349 }
350 }
351
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200352update_flags:
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800353 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200354 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400355 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700356 inode->i_ctime = current_time(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200357 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200358
Anand Jainff9fef552019-04-20 19:48:53 +0800359 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400360 btrfs_end_transaction(trans);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000361 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200362}
363
David Sterba0d7ed322021-05-14 17:42:30 +0200364/*
365 * Start exclusive operation @type, return true on success
366 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500367bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
368 enum btrfs_exclusive_operation type)
369{
David Sterba0d7ed322021-05-14 17:42:30 +0200370 bool ret = false;
371
372 spin_lock(&fs_info->super_lock);
373 if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
374 fs_info->exclusive_operation = type;
375 ret = true;
376 }
377 spin_unlock(&fs_info->super_lock);
378
379 return ret;
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500380}
381
David Sterba578bda92021-05-18 21:05:52 +0200382/*
383 * Conditionally allow to enter the exclusive operation in case it's compatible
384 * with the running one. This must be paired with btrfs_exclop_start_unlock and
385 * btrfs_exclop_finish.
386 *
387 * Compatibility:
388 * - the same type is already running
389 * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller
390 * must check the condition first that would allow none -> @type
391 */
392bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
393 enum btrfs_exclusive_operation type)
394{
395 spin_lock(&fs_info->super_lock);
396 if (fs_info->exclusive_operation == type)
397 return true;
398
399 spin_unlock(&fs_info->super_lock);
400 return false;
401}
402
403void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info)
404{
405 spin_unlock(&fs_info->super_lock);
406}
407
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500408void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
409{
David Sterba0d7ed322021-05-14 17:42:30 +0200410 spin_lock(&fs_info->super_lock);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500411 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
David Sterba0d7ed322021-05-14 17:42:30 +0200412 spin_unlock(&fs_info->super_lock);
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500413 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500414}
415
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200416static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
417{
Al Viro496ad9a2013-01-23 17:07:38 -0500418 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200419
420 return put_user(inode->i_generation, arg);
421}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400422
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300423static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
424 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000425{
Li Dongyangf7039b12011-03-24 10:24:28 +0000426 struct btrfs_device *device;
427 struct request_queue *q;
428 struct fstrim_range range;
429 u64 minlen = ULLONG_MAX;
430 u64 num_devices = 0;
431 int ret;
432
433 if (!capable(CAP_SYS_ADMIN))
434 return -EPERM;
435
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000436 /*
Naohiro Aota1cb3dc32021-02-04 19:21:46 +0900437 * btrfs_trim_block_group() depends on space cache, which is not
438 * available in zoned filesystem. So, disallow fitrim on a zoned
439 * filesystem for now.
440 */
441 if (btrfs_is_zoned(fs_info))
442 return -EOPNOTSUPP;
443
444 /*
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000445 * If the fs is mounted with nologreplay, which requires it to be
446 * mounted in RO mode as well, we can not allow discard on free space
447 * inside block groups, because log trees refer to extents that are not
448 * pinned in a block group's free space cache (pinning the extents is
449 * precisely the first phase of replaying a log tree).
450 */
451 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
452 return -EROFS;
453
Xiao Guangrong1f781602011-04-20 10:09:16 +0000454 rcu_read_lock();
455 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
456 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000457 if (!device->bdev)
458 continue;
459 q = bdev_get_queue(device->bdev);
460 if (blk_queue_discard(q)) {
461 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100462 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000463 minlen);
464 }
465 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000466 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200467
Li Dongyangf7039b12011-03-24 10:24:28 +0000468 if (!num_devices)
469 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000470 if (copy_from_user(&range, arg, sizeof(range)))
471 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800472
473 /*
474 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
475 * block group is in the logical address space, which can be any
476 * sectorsize aligned bytenr in the range [0, U64_MAX].
477 */
478 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200479 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000480
481 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400482 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000483 if (ret < 0)
484 return ret;
485
486 if (copy_to_user(arg, &range, sizeof(range)))
487 return -EFAULT;
488
489 return 0;
490}
491
David Sterbae1f60a62019-10-01 19:57:39 +0200492int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200493{
Chris Mason46e0f662013-11-15 12:14:55 +0100494 int i;
495
496 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
497 if (uuid[i])
498 return 0;
499 }
500 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200501}
502
Christian Brauner4d4340c2021-07-27 12:48:52 +0200503static noinline int create_subvol(struct user_namespace *mnt_userns,
504 struct inode *dir, struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100505 const char *name, int namelen,
Miao Xie8696c532013-02-07 06:02:44 +0000506 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400508 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 struct btrfs_trans_handle *trans;
510 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100511 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 struct btrfs_inode_item *inode_item;
513 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000514 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400515 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000516 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700517 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900518 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 int ret;
520 int err;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800521 dev_t anon_dev = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 u64 objectid;
Chris Mason3de45862008-11-17 21:02:50 -0500523 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524
David Sterba49a3c4d2016-03-24 17:49:22 +0100525 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
526 if (!root_item)
527 return -ENOMEM;
528
Nikolay Borisov543068a2020-12-07 17:32:33 +0200529 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400530 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100531 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000532
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800533 ret = get_anon_bdev(&anon_dev);
534 if (ret < 0)
535 goto fail_free;
536
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800537 /*
538 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400539 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800540 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100541 if (btrfs_qgroup_level(objectid)) {
542 ret = -ENOSPC;
543 goto fail_free;
544 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800545
Miao Xied5c12072013-02-28 10:04:33 +0000546 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400547 /*
Miao Xied5c12072013-02-28 10:04:33 +0000548 * The same as the snapshot creation, please see the comment
549 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400550 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800551 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000552 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100553 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554
Miao Xied5c12072013-02-28 10:04:33 +0000555 trans = btrfs_start_transaction(root, 0);
556 if (IS_ERR(trans)) {
557 ret = PTR_ERR(trans);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800558 btrfs_subvolume_release_metadata(root, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000560 }
561 trans->block_rsv = &block_rsv;
562 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563
Lu Fengqia93774222018-07-18 14:45:41 +0800564 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200565 if (ret)
566 goto fail;
567
Josef Bacik9631e4c2020-08-20 11:46:03 -0400568 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
569 BTRFS_NESTING_NORMAL);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400570 if (IS_ERR(leaf)) {
571 ret = PTR_ERR(leaf);
572 goto fail;
573 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400574
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 btrfs_mark_buffer_dirty(leaf);
576
David Sterba49a3c4d2016-03-24 17:49:22 +0100577 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800578 btrfs_set_stack_inode_generation(inode_item, 1);
579 btrfs_set_stack_inode_size(inode_item, 3);
580 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400581 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400582 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800583 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400584
David Sterba49a3c4d2016-03-24 17:49:22 +0100585 btrfs_set_root_flags(root_item, 0);
586 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800587 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000588
David Sterba49a3c4d2016-03-24 17:49:22 +0100589 btrfs_set_root_bytenr(root_item, leaf->start);
590 btrfs_set_root_generation(root_item, trans->transid);
591 btrfs_set_root_level(root_item, 0);
592 btrfs_set_root_refs(root_item, 1);
593 btrfs_set_root_used(root_item, leaf->len);
594 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595
David Sterba49a3c4d2016-03-24 17:49:22 +0100596 btrfs_set_root_generation_v2(root_item,
597 btrfs_root_generation(root_item));
Andy Shevchenko807fc792020-02-24 17:37:51 +0200598 generate_random_guid(root_item->uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100599 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
600 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
601 root_item->ctime = root_item->otime;
602 btrfs_set_root_ctransid(root_item, trans->transid);
603 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400604
Chris Mason925baed2008-06-25 16:01:30 -0400605 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400606
Nikolay Borisov69948022020-12-07 17:32:37 +0200607 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608
609 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200611 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400612 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100613 root_item);
Filipe Manana67addf22021-04-20 10:55:12 +0100614 if (ret) {
615 /*
616 * Since we don't abort the transaction in this case, free the
617 * tree block so that we don't leak space and leave the
618 * filesystem in an inconsistent state (an extent item in the
619 * extent tree without backreferences). Also no need to have
620 * the tree block locked since it is not in any tree at this
621 * point, so no other task can find it and use it.
622 */
623 btrfs_free_tree_block(trans, root, leaf, 0, 1);
624 free_extent_buffer(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 goto fail;
Filipe Manana67addf22021-04-20 10:55:12 +0100626 }
627
628 free_extent_buffer(leaf);
629 leaf = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630
Yan, Zheng76dda932009-09-21 16:00:26 -0400631 key.offset = (u64)-1;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800632 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100633 if (IS_ERR(new_root)) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800634 free_anon_bdev(anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400636 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 goto fail;
638 }
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800639 /* Freeing will be done in btrfs_put_root() of new_root */
640 anon_dev = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -0400641
Josef Bacik221581e2021-03-12 15:25:06 -0500642 ret = btrfs_record_root_in_trans(trans, new_root);
643 if (ret) {
644 btrfs_put_root(new_root);
645 btrfs_abort_transaction(trans, ret);
646 goto fail;
647 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400648
Christian Brauner4d4340c2021-07-27 12:48:52 +0200649 ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns);
Josef Bacik00246522020-01-24 09:33:01 -0500650 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700651 if (ret) {
652 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400653 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700654 goto fail;
655 }
656
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400657 /*
658 * insert the directory item
659 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200660 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400662 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 goto fail;
664 }
Chris Mason3de45862008-11-17 21:02:50 -0500665
Lu Fengqi684572d2018-08-04 21:10:57 +0800666 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500667 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100668 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400669 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400670 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500672
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200673 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200674 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikc7e54b52019-12-06 09:37:15 -0500675 if (ret) {
676 btrfs_abort_transaction(trans, ret);
677 goto fail;
678 }
Yan Zheng52c26172009-01-05 15:43:43 -0500679
Lu Fengqi6025c192018-08-01 11:32:29 +0800680 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200681 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500682 if (ret) {
683 btrfs_abort_transaction(trans, ret);
684 goto fail;
685 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500686
Lu Fengqicdb345a2018-05-29 15:01:53 +0800687 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400688 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200689 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400690 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400692fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100693 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000694 trans->block_rsv = NULL;
695 trans->bytes_reserved = 0;
Qu Wenruoe85fde52020-07-24 14:46:10 +0800696 btrfs_subvolume_release_metadata(root, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800697
Nikolay Borisov9babda92020-03-13 17:23:20 +0200698 err = btrfs_commit_transaction(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400699 if (err && !ret)
700 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500701
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900702 if (!ret) {
703 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800704 if (IS_ERR(inode))
705 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900706 d_instantiate(dentry, inode);
707 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100709
710fail_free:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800711 if (anon_dev)
712 free_anon_bdev(anon_dev);
David Sterba49a3c4d2016-03-24 17:49:22 +0100713 kfree(root_item);
714 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400715}
716
Miao Xiee9662f72013-02-28 10:01:15 +0000717static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200718 struct dentry *dentry, bool readonly,
Miao Xiee9662f72013-02-28 10:01:15 +0000719 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000722 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723 struct btrfs_pending_snapshot *pending_snapshot;
724 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000725 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800727 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400728 return -EINVAL;
729
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700730 if (atomic_read(&root->nr_swapfiles)) {
731 btrfs_warn(fs_info,
732 "cannot snapshot subvolume with active swapfile");
733 return -ETXTBSY;
734 }
735
David Sterba23269bf2017-02-13 11:03:44 +0100736 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100737 if (!pending_snapshot)
738 return -ENOMEM;
739
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800740 ret = get_anon_bdev(&pending_snapshot->anon_dev);
741 if (ret < 0)
742 goto free_pending;
David Sterbab0c0ea62015-11-10 18:54:00 +0100743 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100744 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100745 pending_snapshot->path = btrfs_alloc_path();
746 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100747 ret = -ENOMEM;
748 goto free_pending;
749 }
750
Miao Xie66d8f3d2012-09-06 04:02:28 -0600751 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
752 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000753 /*
754 * 1 - parent dir inode
755 * 2 - dir entries
756 * 1 - root item
757 * 2 - root ref/backref
758 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200759 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000760 */
761 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200762 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400763 false);
Miao Xied5c12072013-02-28 10:04:33 +0000764 if (ret)
Robbie Koc11fbb62020-05-14 17:19:18 +0800765 goto free_pending;
Miao Xied5c12072013-02-28 10:04:33 +0000766
Yan, Zhenga22285a2010-05-16 10:48:46 -0400767 pending_snapshot->dentry = dentry;
768 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800769 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000770 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000771 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400772
Miao Xied5c12072013-02-28 10:04:33 +0000773 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774 if (IS_ERR(trans)) {
775 ret = PTR_ERR(trans);
776 goto fail;
777 }
778
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400779 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400780 list_add(&pending_snapshot->list,
781 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400782 spin_unlock(&fs_info->trans_lock);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200783
784 ret = btrfs_commit_transaction(trans);
Miao Xieaec80302013-03-04 09:44:29 +0000785 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400786 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400787
788 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400789 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000790 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400791
Chris Masond3797302014-10-15 13:50:56 -0700792 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
793 if (ret)
794 goto fail;
795
David Howells2b0143b2015-03-17 22:25:59 +0000796 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000797 if (IS_ERR(inode)) {
798 ret = PTR_ERR(inode);
799 goto fail;
800 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900801
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000802 d_instantiate(dentry, inode);
803 ret = 0;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800804 pending_snapshot->anon_dev = 0;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000805fail:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800806 /* Prevent double freeing of anon_dev */
807 if (ret && pending_snapshot->snap)
808 pending_snapshot->snap->anon_dev = 0;
Josef Bacik00246522020-01-24 09:33:01 -0500809 btrfs_put_root(pending_snapshot->snap);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800810 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
David Sterbab0c0ea62015-11-10 18:54:00 +0100811free_pending:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800812 if (pending_snapshot->anon_dev)
813 free_anon_bdev(pending_snapshot->anon_dev);
David Sterbab0c0ea62015-11-10 18:54:00 +0100814 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100815 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100816 kfree(pending_snapshot);
817
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400818 return ret;
819}
820
Sage Weil4260f7c2010-10-29 15:46:43 -0400821/* copy of may_delete in fs/namei.c()
822 * Check whether we can remove a link victim from directory dir, check
823 * whether the type of victim is right.
824 * 1. We can't do it if dir is read-only (done in permission())
825 * 2. We should have write and exec permissions on dir
826 * 3. We can't remove anything from append-only dir
827 * 4. We can't do anything with immutable dir (done in permission())
828 * 5. If the sticky bit on dir is set we should either
829 * a. be owner of dir, or
830 * b. be owner of victim, or
831 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400832 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400833 * links pointing to it.
834 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
835 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
836 * 9. We can't remove a root or mountpoint.
837 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
838 * nfs_async_unlink().
839 */
840
Christian Braunerc4ed5332021-07-27 12:48:53 +0200841static int btrfs_may_delete(struct user_namespace *mnt_userns,
842 struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400843{
844 int error;
845
David Howells2b0143b2015-03-17 22:25:59 +0000846 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400847 return -ENOENT;
848
David Howells2b0143b2015-03-17 22:25:59 +0000849 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400850 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400851
Christian Braunerc4ed5332021-07-27 12:48:53 +0200852 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -0400853 if (error)
854 return error;
855 if (IS_APPEND(dir))
856 return -EPERM;
Christian Braunerc4ed5332021-07-27 12:48:53 +0200857 if (check_sticky(mnt_userns, dir, d_inode(victim)) ||
Christian Braunerba73d982021-01-21 14:19:31 +0100858 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
859 IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400860 return -EPERM;
861 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000862 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400863 return -ENOTDIR;
864 if (IS_ROOT(victim))
865 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000866 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400867 return -EISDIR;
868 if (IS_DEADDIR(dir))
869 return -ENOENT;
870 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
871 return -EBUSY;
872 return 0;
873}
874
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400875/* copy of may_create in fs/namei.c() */
Christian Brauner4d4340c2021-07-27 12:48:52 +0200876static inline int btrfs_may_create(struct user_namespace *mnt_userns,
877 struct inode *dir, struct dentry *child)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878{
David Howells2b0143b2015-03-17 22:25:59 +0000879 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400880 return -EEXIST;
881 if (IS_DEADDIR(dir))
882 return -ENOENT;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200883 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Christian Brauner5474bf42021-07-27 12:48:51 +0200884 return -EOVERFLOW;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200885 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400886}
887
888/*
889 * Create a new subvolume below @parent. This is largely modeled after
890 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
891 * inside this filesystem so it's quite a bit simpler.
892 */
Al Viro92872092016-11-20 19:34:31 -0500893static noinline int btrfs_mksubvol(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200894 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +0100895 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400896 struct btrfs_root *snap_src,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200897 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000898 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400899{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400900 struct inode *dir = d_inode(parent->dentry);
901 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400902 struct dentry *dentry;
903 int error;
904
Al Viro00235412016-05-26 00:05:12 -0400905 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
906 if (error == -EINTR)
907 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400908
Christian Brauner4d4340c2021-07-27 12:48:52 +0200909 dentry = lookup_one(mnt_userns, name, parent->dentry, namelen);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400910 error = PTR_ERR(dentry);
911 if (IS_ERR(dentry))
912 goto out_unlock;
913
Christian Brauner4d4340c2021-07-27 12:48:52 +0200914 error = btrfs_may_create(mnt_userns, dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400915 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600916 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400917
Chris Mason9c520572012-12-17 14:26:57 -0500918 /*
919 * even if this name doesn't exist, we may get hash collisions.
920 * check for them now when we can safely fail
921 */
922 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
923 dir->i_ino, name,
924 namelen);
925 if (error)
926 goto out_dput;
927
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400928 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400929
930 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
931 goto out_up_read;
932
Nikolay Borisov9babda92020-03-13 17:23:20 +0200933 if (snap_src)
934 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
935 else
Christian Brauner4d4340c2021-07-27 12:48:52 +0200936 error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200937
Yan, Zheng76dda932009-09-21 16:00:26 -0400938 if (!error)
939 fsnotify_mkdir(dir, dentry);
940out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400942out_dput:
943 dput(dentry);
944out_unlock:
Josef Bacik64708532021-02-10 17:14:34 -0500945 btrfs_inode_unlock(dir, 0);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400946 return error;
947}
948
Robbie Koc11fbb62020-05-14 17:19:18 +0800949static noinline int btrfs_mksnapshot(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200950 struct user_namespace *mnt_userns,
Robbie Koc11fbb62020-05-14 17:19:18 +0800951 const char *name, int namelen,
952 struct btrfs_root *root,
953 bool readonly,
954 struct btrfs_qgroup_inherit *inherit)
955{
956 int ret;
957 bool snapshot_force_cow = false;
958
959 /*
960 * Force new buffered writes to reserve space even when NOCOW is
961 * possible. This is to avoid later writeback (running dealloc) to
962 * fallback to COW mode and unexpectedly fail with ENOSPC.
963 */
964 btrfs_drew_read_lock(&root->snapshot_lock);
965
Filipe Mananaf9baa502021-04-22 12:08:05 +0100966 ret = btrfs_start_delalloc_snapshot(root, false);
Robbie Koc11fbb62020-05-14 17:19:18 +0800967 if (ret)
968 goto out;
969
970 /*
971 * All previous writes have started writeback in NOCOW mode, so now
972 * we force future writes to fallback to COW mode during snapshot
973 * creation.
974 */
975 atomic_inc(&root->snapshot_force_cow);
976 snapshot_force_cow = true;
977
978 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
979
Christian Brauner4d4340c2021-07-27 12:48:52 +0200980 ret = btrfs_mksubvol(parent, mnt_userns, name, namelen,
Robbie Koc11fbb62020-05-14 17:19:18 +0800981 root, readonly, inherit);
982out:
983 if (snapshot_force_cow)
984 atomic_dec(&root->snapshot_force_cow);
985 btrfs_drew_read_unlock(&root->snapshot_lock);
986 return ret;
987}
988
Qu Wenruoe9eec722021-08-06 16:12:38 +0800989static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,
990 bool locked)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400991{
992 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500993 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800994 struct extent_map *em;
Qu Wenruo76068ca2021-08-06 16:12:34 +0800995 const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize;
Chris Mason940100a2010-03-10 10:52:59 -0500996
997 /*
998 * hopefully we have this extent in the tree already, try without
999 * the full extent lock
1000 */
1001 read_lock(&em_tree->lock);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001002 em = lookup_extent_mapping(em_tree, start, sectorsize);
Chris Mason940100a2010-03-10 10:52:59 -05001003 read_unlock(&em_tree->lock);
1004
1005 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001006 struct extent_state *cached = NULL;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001007 u64 end = start + sectorsize - 1;
Filipe Manana308d9802014-03-11 13:56:15 +00001008
Chris Mason940100a2010-03-10 10:52:59 -05001009 /* get the big lock and read metadata off disk */
Qu Wenruoe9eec722021-08-06 16:12:38 +08001010 if (!locked)
1011 lock_extent_bits(io_tree, start, end, &cached);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001012 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize);
Qu Wenruoe9eec722021-08-06 16:12:38 +08001013 if (!locked)
1014 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001015
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001016 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001017 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001018 }
1019
Li Zefan6c282eb2012-06-11 16:03:35 +08001020 return em;
1021}
1022
Qu Wenruoe9eec722021-08-06 16:12:38 +08001023static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
1024 bool locked)
Li Zefan6c282eb2012-06-11 16:03:35 +08001025{
1026 struct extent_map *next;
1027 bool ret = true;
1028
1029 /* this is the last extent */
1030 if (em->start + em->len >= i_size_read(inode))
1031 return false;
1032
Qu Wenruoe9eec722021-08-06 16:12:38 +08001033 next = defrag_lookup_extent(inode, em->start + em->len, locked);
Chris Masone9512d72014-08-26 13:55:54 -07001034 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1035 ret = false;
1036 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001037 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001038 ret = false;
1039
1040 free_extent_map(next);
1041 return ret;
1042}
1043
Chris Mason4cb53002011-05-24 15:35:30 -04001044/*
Qu Wenruo5767b502021-08-06 16:12:35 +08001045 * Prepare one page to be defragged.
1046 *
1047 * This will ensure:
1048 *
1049 * - Returned page is locked and has been set up properly.
1050 * - No ordered extent exists in the page.
1051 * - The page is uptodate.
1052 *
1053 * NOTE: Caller should also wait for page writeback after the cluster is
1054 * prepared, here we don't do writeback wait for each page.
1055 */
1056static struct page *defrag_prepare_one_page(struct btrfs_inode *inode,
1057 pgoff_t index)
1058{
1059 struct address_space *mapping = inode->vfs_inode.i_mapping;
1060 gfp_t mask = btrfs_alloc_write_mask(mapping);
1061 u64 page_start = (u64)index << PAGE_SHIFT;
1062 u64 page_end = page_start + PAGE_SIZE - 1;
1063 struct extent_state *cached_state = NULL;
1064 struct page *page;
1065 int ret;
1066
1067again:
1068 page = find_or_create_page(mapping, index, mask);
1069 if (!page)
1070 return ERR_PTR(-ENOMEM);
1071
Omar Sandoval24bcb452021-10-19 20:35:01 -07001072 /*
1073 * Since we can defragment files opened read-only, we can encounter
1074 * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We
1075 * can't do I/O using huge pages yet, so return an error for now.
1076 * Filesystem transparent huge pages are typically only used for
1077 * executables that explicitly enable them, so this isn't very
1078 * restrictive.
1079 */
1080 if (PageCompound(page)) {
1081 unlock_page(page);
1082 put_page(page);
1083 return ERR_PTR(-ETXTBSY);
1084 }
1085
Qu Wenruo5767b502021-08-06 16:12:35 +08001086 ret = set_page_extent_mapped(page);
1087 if (ret < 0) {
1088 unlock_page(page);
1089 put_page(page);
1090 return ERR_PTR(ret);
1091 }
1092
1093 /* Wait for any existing ordered extent in the range */
1094 while (1) {
1095 struct btrfs_ordered_extent *ordered;
1096
1097 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
1098 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
1099 unlock_extent_cached(&inode->io_tree, page_start, page_end,
1100 &cached_state);
1101 if (!ordered)
1102 break;
1103
1104 unlock_page(page);
1105 btrfs_start_ordered_extent(ordered, 1);
1106 btrfs_put_ordered_extent(ordered);
1107 lock_page(page);
1108 /*
1109 * We unlocked the page above, so we need check if it was
1110 * released or not.
1111 */
1112 if (page->mapping != mapping || !PagePrivate(page)) {
1113 unlock_page(page);
1114 put_page(page);
1115 goto again;
1116 }
1117 }
1118
1119 /*
1120 * Now the page range has no ordered extent any more. Read the page to
1121 * make it uptodate.
1122 */
1123 if (!PageUptodate(page)) {
1124 btrfs_readpage(NULL, page);
1125 lock_page(page);
1126 if (page->mapping != mapping || !PagePrivate(page)) {
1127 unlock_page(page);
1128 put_page(page);
1129 goto again;
1130 }
1131 if (!PageUptodate(page)) {
1132 unlock_page(page);
1133 put_page(page);
1134 return ERR_PTR(-EIO);
1135 }
1136 }
1137 return page;
1138}
1139
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001140struct defrag_target_range {
1141 struct list_head list;
1142 u64 start;
1143 u64 len;
1144};
1145
1146/*
1147 * Collect all valid target extents.
1148 *
1149 * @start: file offset to lookup
1150 * @len: length to lookup
1151 * @extent_thresh: file extent size threshold, any extent size >= this value
1152 * will be ignored
1153 * @newer_than: only defrag extents newer than this value
1154 * @do_compress: whether the defrag is doing compression
1155 * if true, @extent_thresh will be ignored and all regular
1156 * file extents meeting @newer_than will be targets.
Qu Wenruoe9eec722021-08-06 16:12:38 +08001157 * @locked: if the range has already held extent lock
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001158 * @target_list: list of targets file extents
1159 */
1160static int defrag_collect_targets(struct btrfs_inode *inode,
1161 u64 start, u64 len, u32 extent_thresh,
1162 u64 newer_than, bool do_compress,
Qu Wenruoe9eec722021-08-06 16:12:38 +08001163 bool locked, struct list_head *target_list)
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001164{
1165 u64 cur = start;
1166 int ret = 0;
1167
1168 while (cur < start + len) {
1169 struct extent_map *em;
1170 struct defrag_target_range *new;
1171 bool next_mergeable = true;
1172 u64 range_len;
1173
Qu Wenruoe9eec722021-08-06 16:12:38 +08001174 em = defrag_lookup_extent(&inode->vfs_inode, cur, locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001175 if (!em)
1176 break;
1177
1178 /* Skip hole/inline/preallocated extents */
1179 if (em->block_start >= EXTENT_MAP_LAST_BYTE ||
1180 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1181 goto next;
1182
1183 /* Skip older extent */
1184 if (em->generation < newer_than)
1185 goto next;
1186
1187 /*
1188 * For do_compress case, we want to compress all valid file
1189 * extents, thus no @extent_thresh or mergeable check.
1190 */
1191 if (do_compress)
1192 goto add;
1193
1194 /* Skip too large extent */
1195 if (em->len >= extent_thresh)
1196 goto next;
1197
Qu Wenruoe9eec722021-08-06 16:12:38 +08001198 next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
1199 locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001200 if (!next_mergeable) {
1201 struct defrag_target_range *last;
1202
1203 /* Empty target list, no way to merge with last entry */
1204 if (list_empty(target_list))
1205 goto next;
1206 last = list_entry(target_list->prev,
1207 struct defrag_target_range, list);
1208 /* Not mergeable with last entry */
1209 if (last->start + last->len != cur)
1210 goto next;
1211
1212 /* Mergeable, fall through to add it to @target_list. */
1213 }
1214
1215add:
1216 range_len = min(extent_map_end(em), start + len) - cur;
1217 /*
1218 * This one is a good target, check if it can be merged into
1219 * last range of the target list.
1220 */
1221 if (!list_empty(target_list)) {
1222 struct defrag_target_range *last;
1223
1224 last = list_entry(target_list->prev,
1225 struct defrag_target_range, list);
1226 ASSERT(last->start + last->len <= cur);
1227 if (last->start + last->len == cur) {
1228 /* Mergeable, enlarge the last entry */
1229 last->len += range_len;
1230 goto next;
1231 }
1232 /* Fall through to allocate a new entry */
1233 }
1234
1235 /* Allocate new defrag_target_range */
1236 new = kmalloc(sizeof(*new), GFP_NOFS);
1237 if (!new) {
1238 free_extent_map(em);
1239 ret = -ENOMEM;
1240 break;
1241 }
1242 new->start = cur;
1243 new->len = range_len;
1244 list_add_tail(&new->list, target_list);
1245
1246next:
1247 cur = extent_map_end(em);
1248 free_extent_map(em);
1249 }
1250 if (ret < 0) {
1251 struct defrag_target_range *entry;
1252 struct defrag_target_range *tmp;
1253
1254 list_for_each_entry_safe(entry, tmp, target_list, list) {
1255 list_del_init(&entry->list);
1256 kfree(entry);
1257 }
1258 }
1259 return ret;
1260}
1261
Qu Wenruo22b398e2021-08-06 16:12:37 +08001262#define CLUSTER_SIZE (SZ_256K)
1263
1264/*
1265 * Defrag one contiguous target range.
1266 *
1267 * @inode: target inode
1268 * @target: target range to defrag
1269 * @pages: locked pages covering the defrag range
1270 * @nr_pages: number of locked pages
1271 *
1272 * Caller should ensure:
1273 *
1274 * - Pages are prepared
1275 * Pages should be locked, no ordered extent in the pages range,
1276 * no writeback.
1277 *
1278 * - Extent bits are locked
1279 */
1280static int defrag_one_locked_target(struct btrfs_inode *inode,
1281 struct defrag_target_range *target,
1282 struct page **pages, int nr_pages,
1283 struct extent_state **cached_state)
1284{
1285 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1286 struct extent_changeset *data_reserved = NULL;
1287 const u64 start = target->start;
1288 const u64 len = target->len;
1289 unsigned long last_index = (start + len - 1) >> PAGE_SHIFT;
1290 unsigned long start_index = start >> PAGE_SHIFT;
1291 unsigned long first_index = page_index(pages[0]);
1292 int ret = 0;
1293 int i;
1294
1295 ASSERT(last_index - first_index + 1 <= nr_pages);
1296
1297 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len);
1298 if (ret < 0)
1299 return ret;
1300 clear_extent_bit(&inode->io_tree, start, start + len - 1,
1301 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1302 EXTENT_DEFRAG, 0, 0, cached_state);
1303 set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state);
1304
1305 /* Update the page status */
1306 for (i = start_index - first_index; i <= last_index - first_index; i++) {
1307 ClearPageChecked(pages[i]);
1308 btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len);
1309 }
1310 btrfs_delalloc_release_extents(inode, len);
1311 extent_changeset_free(data_reserved);
1312
1313 return ret;
1314}
1315
Qu Wenruoe9eec722021-08-06 16:12:38 +08001316static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,
1317 u32 extent_thresh, u64 newer_than, bool do_compress)
1318{
1319 struct extent_state *cached_state = NULL;
1320 struct defrag_target_range *entry;
1321 struct defrag_target_range *tmp;
1322 LIST_HEAD(target_list);
1323 struct page **pages;
1324 const u32 sectorsize = inode->root->fs_info->sectorsize;
1325 u64 last_index = (start + len - 1) >> PAGE_SHIFT;
1326 u64 start_index = start >> PAGE_SHIFT;
1327 unsigned int nr_pages = last_index - start_index + 1;
1328 int ret = 0;
1329 int i;
1330
1331 ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE);
1332 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize));
1333
1334 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
1335 if (!pages)
1336 return -ENOMEM;
1337
1338 /* Prepare all pages */
1339 for (i = 0; i < nr_pages; i++) {
1340 pages[i] = defrag_prepare_one_page(inode, start_index + i);
1341 if (IS_ERR(pages[i])) {
1342 ret = PTR_ERR(pages[i]);
1343 pages[i] = NULL;
1344 goto free_pages;
1345 }
1346 }
1347 for (i = 0; i < nr_pages; i++)
1348 wait_on_page_writeback(pages[i]);
1349
1350 /* Lock the pages range */
1351 lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT,
1352 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1353 &cached_state);
1354 /*
1355 * Now we have a consistent view about the extent map, re-check
1356 * which range really needs to be defragged.
1357 *
1358 * And this time we have extent locked already, pass @locked = true
1359 * so that we won't relock the extent range and cause deadlock.
1360 */
1361 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1362 newer_than, do_compress, true,
1363 &target_list);
1364 if (ret < 0)
1365 goto unlock_extent;
1366
1367 list_for_each_entry(entry, &target_list, list) {
1368 ret = defrag_one_locked_target(inode, entry, pages, nr_pages,
1369 &cached_state);
1370 if (ret < 0)
1371 break;
1372 }
1373
1374 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1375 list_del_init(&entry->list);
1376 kfree(entry);
1377 }
1378unlock_extent:
1379 unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT,
1380 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1381 &cached_state);
1382free_pages:
1383 for (i = 0; i < nr_pages; i++) {
1384 if (pages[i]) {
1385 unlock_page(pages[i]);
1386 put_page(pages[i]);
1387 }
1388 }
1389 kfree(pages);
1390 return ret;
1391}
1392
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001393static int defrag_one_cluster(struct btrfs_inode *inode,
1394 struct file_ra_state *ra,
1395 u64 start, u32 len, u32 extent_thresh,
1396 u64 newer_than, bool do_compress,
1397 unsigned long *sectors_defragged,
1398 unsigned long max_sectors)
1399{
1400 const u32 sectorsize = inode->root->fs_info->sectorsize;
1401 struct defrag_target_range *entry;
1402 struct defrag_target_range *tmp;
1403 LIST_HEAD(target_list);
1404 int ret;
1405
1406 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
1407 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1408 newer_than, do_compress, false,
1409 &target_list);
1410 if (ret < 0)
1411 goto out;
1412
1413 list_for_each_entry(entry, &target_list, list) {
1414 u32 range_len = entry->len;
1415
1416 /* Reached the limit */
1417 if (max_sectors && max_sectors == *sectors_defragged)
1418 break;
1419
1420 if (max_sectors)
1421 range_len = min_t(u32, range_len,
1422 (max_sectors - *sectors_defragged) * sectorsize);
1423
1424 if (ra)
1425 page_cache_sync_readahead(inode->vfs_inode.i_mapping,
1426 ra, NULL, entry->start >> PAGE_SHIFT,
1427 ((entry->start + range_len - 1) >> PAGE_SHIFT) -
1428 (entry->start >> PAGE_SHIFT) + 1);
1429 /*
1430 * Here we may not defrag any range if holes are punched before
1431 * we locked the pages.
1432 * But that's fine, it only affects the @sectors_defragged
1433 * accounting.
1434 */
1435 ret = defrag_one_range(inode, entry->start, range_len,
1436 extent_thresh, newer_than, do_compress);
1437 if (ret < 0)
1438 break;
1439 *sectors_defragged += range_len;
1440 }
1441out:
1442 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1443 list_del_init(&entry->list);
1444 kfree(entry);
1445 }
1446 return ret;
1447}
1448
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001449/*
1450 * Entry point to file defragmentation.
1451 *
1452 * @inode: inode to be defragged
1453 * @ra: readahead state (can be NUL)
1454 * @range: defrag options including range and flags
1455 * @newer_than: minimum transid to defrag
1456 * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode
1457 * will be defragged.
1458 */
1459int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
Chris Mason4cb53002011-05-24 15:35:30 -04001460 struct btrfs_ioctl_defrag_range_args *range,
1461 u64 newer_than, unsigned long max_to_defrag)
1462{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo7b508032021-05-27 20:33:22 +08001464 unsigned long sectors_defragged = 0;
Li Zefan151a31b2011-09-02 15:56:39 +08001465 u64 isize = i_size_read(inode);
Qu Wenruo7b508032021-05-27 20:33:22 +08001466 u64 cur;
1467 u64 last_byte;
David Sterba1e2ef462017-07-17 20:01:59 +02001468 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001469 bool ra_allocated = false;
Qu Wenruo7b508032021-05-27 20:33:22 +08001470 int compress_type = BTRFS_COMPRESS_ZLIB;
1471 int ret = 0;
1472 u32 extent_thresh = range->extent_thresh;
Chris Mason4cb53002011-05-24 15:35:30 -04001473
Liu Bo0abd5b12013-04-16 09:20:28 +00001474 if (isize == 0)
1475 return 0;
1476
1477 if (range->start >= isize)
1478 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001479
David Sterba1e2ef462017-07-17 20:01:59 +02001480 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001481 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001482 return -EINVAL;
1483 if (range->compress_type)
1484 compress_type = range->compress_type;
1485 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001486
Liu Bo0abd5b12013-04-16 09:20:28 +00001487 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001488 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489
Qu Wenruo7b508032021-05-27 20:33:22 +08001490 if (range->start + range->len > range->start) {
1491 /* Got a specific range */
1492 last_byte = min(isize, range->start + range->len) - 1;
1493 } else {
1494 /* Defrag until file end */
1495 last_byte = isize - 1;
1496 }
1497
Chris Mason4cb53002011-05-24 15:35:30 -04001498 /*
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001499 * If we were not given a ra, allocate a readahead context. As
David Sterba0a52d102017-06-22 03:22:58 +02001500 * readahead is just an optimization, defrag will work without it so
1501 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001502 */
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001503 if (!ra) {
1504 ra_allocated = true;
David Sterba63e727e2017-06-22 03:13:02 +02001505 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001506 if (ra)
1507 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001508 }
1509
Qu Wenruo7b508032021-05-27 20:33:22 +08001510 /* Align the range */
1511 cur = round_down(range->start, fs_info->sectorsize);
1512 last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001513
Qu Wenruo7b508032021-05-27 20:33:22 +08001514 while (cur < last_byte) {
1515 u64 cluster_end;
Chris Mason1e701a32010-03-11 09:42:04 -05001516
Qu Wenruo7b508032021-05-27 20:33:22 +08001517 /* The cluster size 256K should always be page aligned */
1518 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
Chris Mason4cb53002011-05-24 15:35:30 -04001519
Qu Wenruo7b508032021-05-27 20:33:22 +08001520 /* We want the cluster end at page boundary when possible */
1521 cluster_end = (((cur >> PAGE_SHIFT) +
1522 (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1;
1523 cluster_end = min(cluster_end, last_byte);
Chris Mason940100a2010-03-10 10:52:59 -05001524
Josef Bacik64708532021-02-10 17:14:34 -05001525 btrfs_inode_lock(inode, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001526 if (IS_SWAPFILE(inode)) {
1527 ret = -ETXTBSY;
Josef Bacik64708532021-02-10 17:14:34 -05001528 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001529 break;
Liu Boecb8bea2012-03-29 09:57:44 -04001530 }
Qu Wenruo7b508032021-05-27 20:33:22 +08001531 if (!(inode->i_sb->s_flags & SB_ACTIVE)) {
1532 btrfs_inode_unlock(inode, 0);
1533 break;
1534 }
1535 if (do_compress)
1536 BTRFS_I(inode)->defrag_compress = compress_type;
1537 ret = defrag_one_cluster(BTRFS_I(inode), ra, cur,
1538 cluster_end + 1 - cur, extent_thresh,
1539 newer_than, do_compress,
1540 &sectors_defragged, max_to_defrag);
Josef Bacik64708532021-02-10 17:14:34 -05001541 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001542 if (ret < 0)
1543 break;
1544 cur = cluster_end + 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 }
1546
Qu Wenruo7b508032021-05-27 20:33:22 +08001547 if (ra_allocated)
1548 kfree(ra);
1549 if (sectors_defragged) {
1550 /*
1551 * We have defragged some sectors, for compression case they
1552 * need to be written back immediately.
1553 */
1554 if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) {
Filipe Mananadec8ef92014-03-01 10:55:54 +00001555 filemap_flush(inode->i_mapping);
Qu Wenruo7b508032021-05-27 20:33:22 +08001556 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1557 &BTRFS_I(inode)->runtime_flags))
1558 filemap_flush(inode->i_mapping);
1559 }
1560 if (range->compress_type == BTRFS_COMPRESS_LZO)
1561 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1562 else if (range->compress_type == BTRFS_COMPRESS_ZSTD)
1563 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1564 ret = sectors_defragged;
Filipe Mananadec8ef92014-03-01 10:55:54 +00001565 }
David Sterba1e2ef462017-07-17 20:01:59 +02001566 if (do_compress) {
Josef Bacik64708532021-02-10 17:14:34 -05001567 btrfs_inode_lock(inode, 0);
David Sterbaeec63c62017-07-17 19:41:31 +02001568 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Josef Bacik64708532021-02-10 17:14:34 -05001569 btrfs_inode_unlock(inode, 0);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001570 }
Chris Mason940100a2010-03-10 10:52:59 -05001571 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572}
1573
David Sterba17aaa432021-05-14 21:32:44 +02001574/*
1575 * Try to start exclusive operation @type or cancel it if it's running.
1576 *
1577 * Return:
1578 * 0 - normal mode, newly claimed op started
1579 * >0 - normal mode, something else is running,
1580 * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
1581 * ECANCELED - cancel mode, successful cancel
1582 * ENOTCONN - cancel mode, operation not running anymore
1583 */
1584static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
1585 enum btrfs_exclusive_operation type, bool cancel)
1586{
1587 if (!cancel) {
1588 /* Start normal op */
1589 if (!btrfs_exclop_start(fs_info, type))
1590 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1591 /* Exclusive operation is now claimed */
1592 return 0;
1593 }
1594
1595 /* Cancel running op */
1596 if (btrfs_exclop_start_try_lock(fs_info, type)) {
1597 /*
1598 * This blocks any exclop finish from setting it to NONE, so we
1599 * request cancellation. Either it runs and we will wait for it,
1600 * or it has finished and no waiting will happen.
1601 */
1602 atomic_inc(&fs_info->reloc_cancel_req);
1603 btrfs_exclop_start_unlock(fs_info);
1604
1605 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
1606 wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
1607 TASK_INTERRUPTIBLE);
1608
1609 return -ECANCELED;
1610 }
1611
1612 /* Something else is running or none */
1613 return -ENOTCONN;
1614}
1615
Miao Xie198605a2012-11-26 08:43:45 +00001616static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001617 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001618{
Josef Bacik562d7b12021-10-05 16:12:42 -04001619 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001620 struct inode *inode = file_inode(file);
1621 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 u64 new_size;
1623 u64 old_size;
1624 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001625 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626 struct btrfs_ioctl_vol_args *vol_args;
1627 struct btrfs_trans_handle *trans;
1628 struct btrfs_device *device = NULL;
1629 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001630 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631 char *devstr = NULL;
1632 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 int mod = 0;
David Sterbabb059a32021-05-18 21:12:33 +02001634 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635
Chris Masone441d542009-01-05 16:57:23 -05001636 if (!capable(CAP_SYS_ADMIN))
1637 return -EPERM;
1638
Miao Xie198605a2012-11-26 08:43:45 +00001639 ret = mnt_want_write_file(file);
1640 if (ret)
1641 return ret;
1642
David Sterbabb059a32021-05-18 21:12:33 +02001643 /*
1644 * Read the arguments before checking exclusivity to be able to
1645 * distinguish regular resize and cancel
1646 */
Li Zefandae7b662009-04-08 15:06:54 +08001647 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001648 if (IS_ERR(vol_args)) {
1649 ret = PTR_ERR(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001650 goto out_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001651 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001652 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001653 sizestr = vol_args->name;
David Sterbabb059a32021-05-18 21:12:33 +02001654 cancel = (strcmp("cancel", sizestr) == 0);
1655 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
1656 if (ret)
1657 goto out_free;
1658 /* Exclusive operation is now claimed */
1659
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001660 devstr = strchr(sizestr, ':');
1661 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001662 sizestr = devstr + 1;
1663 *devstr = '\0';
1664 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001665 ret = kstrtoull(devstr, 10, &devid);
1666 if (ret)
David Sterbabb059a32021-05-18 21:12:33 +02001667 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001668 if (!devid) {
1669 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001670 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001671 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001672 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 }
Miao Xiedba60f32012-12-21 09:19:51 +00001674
Josef Bacik562d7b12021-10-05 16:12:42 -04001675 args.devid = devid;
1676 device = btrfs_find_device(fs_info->fs_devices, &args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001677 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001678 btrfs_info(fs_info, "resizer unable to find device %llu",
1679 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001680 ret = -ENODEV;
David Sterbabb059a32021-05-18 21:12:33 +02001681 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001682 }
Miao Xiedba60f32012-12-21 09:19:51 +00001683
Anand Jainebbede42017-12-04 12:54:52 +08001684 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001685 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001686 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001687 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001688 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001689 goto out_finish;
Liu Bo4e42ae12012-06-14 02:23:19 -06001690 }
1691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001692 if (!strcmp(sizestr, "max"))
1693 new_size = device->bdev->bd_inode->i_size;
1694 else {
1695 if (sizestr[0] == '-') {
1696 mod = -1;
1697 sizestr++;
1698 } else if (sizestr[0] == '+') {
1699 mod = 1;
1700 sizestr++;
1701 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001702 new_size = memparse(sizestr, &retptr);
1703 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001704 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001705 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 }
1707 }
1708
Anand Jain401e29c2017-12-04 12:54:55 +08001709 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001710 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001711 goto out_finish;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001712 }
1713
Miao Xie7cc8e582014-09-03 21:35:38 +08001714 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001715
1716 if (mod < 0) {
1717 if (new_size > old_size) {
1718 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001719 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720 }
1721 new_size = old_size - new_size;
1722 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001723 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001724 ret = -ERANGE;
David Sterbabb059a32021-05-18 21:12:33 +02001725 goto out_finish;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001726 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727 new_size = old_size + new_size;
1728 }
1729
Byongho Leeee221842015-12-15 01:42:10 +09001730 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001732 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733 }
1734 if (new_size > device->bdev->bd_inode->i_size) {
1735 ret = -EFBIG;
David Sterbabb059a32021-05-18 21:12:33 +02001736 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001737 }
1738
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001739 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001741 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001742 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001743 if (IS_ERR(trans)) {
1744 ret = PTR_ERR(trans);
David Sterbabb059a32021-05-18 21:12:33 +02001745 goto out_finish;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001746 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001747 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001748 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001749 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001751 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752
Marcos Paulo de Souzafaf8f7b2020-02-11 10:55:26 -03001753 if (ret == 0 && new_size != old_size)
1754 btrfs_info_in_rcu(fs_info,
1755 "resize device %s (devid %llu) from %llu to %llu",
1756 rcu_str_deref(device->name), device->devid,
1757 old_size, new_size);
David Sterbabb059a32021-05-18 21:12:33 +02001758out_finish:
1759 btrfs_exclop_finish(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001760out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001761 kfree(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001762out_drop:
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001763 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 return ret;
1765}
1766
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001767static noinline int __btrfs_ioctl_snap_create(struct file *file,
Christian Brauner4d4340c2021-07-27 12:48:52 +02001768 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +01001769 const char *name, unsigned long fd, int subvol,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001770 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001771 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001772{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001773 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001774 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001776 if (!S_ISDIR(file_inode(file)->i_mode))
1777 return -ENOTDIR;
1778
Liu Boa874a632012-06-29 03:58:46 -06001779 ret = mnt_want_write_file(file);
1780 if (ret)
1781 goto out;
1782
Sage Weil72fd0322010-10-29 15:41:32 -04001783 namelen = strlen(name);
1784 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001785 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001786 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001787 }
1788
Chris Mason16780ca2012-02-20 22:14:55 -05001789 if (name[0] == '.' &&
1790 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1791 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001792 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001793 }
1794
Chris Mason3de45862008-11-17 21:02:50 -05001795 if (subvol) {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001796 ret = btrfs_mksubvol(&file->f_path, mnt_userns, name,
1797 namelen, NULL, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001798 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001799 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001800 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001801 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001802 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001803 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001804 }
1805
Al Viro496ad9a2013-01-23 17:07:38 -05001806 src_inode = file_inode(src.file);
1807 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001808 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001809 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001810 ret = -EXDEV;
Christian Brauner4d4340c2021-07-27 12:48:52 +02001811 } else if (!inode_owner_or_capable(mnt_userns, src_inode)) {
David Sterbad0242062014-01-15 18:15:52 +01001812 /*
1813 * Subvolume creation is not restricted, but snapshots
1814 * are limited to own subvolumes only
1815 */
1816 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001817 } else {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001818 ret = btrfs_mksnapshot(&file->f_path, mnt_userns,
1819 name, namelen,
1820 BTRFS_I(src_inode)->root,
1821 readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001822 }
Al Viro2903ff02012-08-28 12:52:22 -04001823 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001824 }
Liu Boa874a632012-06-29 03:58:46 -06001825out_drop_write:
1826 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001827out:
Sage Weil72fd0322010-10-29 15:41:32 -04001828 return ret;
1829}
1830
1831static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001832 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001833{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001834 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001835 int ret;
1836
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001837 if (!S_ISDIR(file_inode(file)->i_mode))
1838 return -ENOTDIR;
1839
Li Zefanfa0d2b92010-12-20 15:53:28 +08001840 vol_args = memdup_user(arg, sizeof(*vol_args));
1841 if (IS_ERR(vol_args))
1842 return PTR_ERR(vol_args);
1843 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001844
Christian Brauner4d4340c2021-07-27 12:48:52 +02001845 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1846 vol_args->name, vol_args->fd, subvol,
1847 false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001848
Li Zefanfa0d2b92010-12-20 15:53:28 +08001849 kfree(vol_args);
1850 return ret;
1851}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001852
Li Zefanfa0d2b92010-12-20 15:53:28 +08001853static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1854 void __user *arg, int subvol)
1855{
1856 struct btrfs_ioctl_vol_args_v2 *vol_args;
1857 int ret;
Li Zefanb83cc962010-12-20 16:04:08 +08001858 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001859 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001860
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001861 if (!S_ISDIR(file_inode(file)->i_mode))
1862 return -ENOTDIR;
1863
Li Zefanfa0d2b92010-12-20 15:53:28 +08001864 vol_args = memdup_user(arg, sizeof(*vol_args));
1865 if (IS_ERR(vol_args))
1866 return PTR_ERR(vol_args);
1867 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001868
David Sterba673990d2020-02-21 13:24:37 +01001869 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08001870 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001871 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001872 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001873
Li Zefanb83cc962010-12-20 16:04:08 +08001874 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1875 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001876 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001877 u64 nums;
1878
1879 if (vol_args->size < sizeof(*inherit) ||
1880 vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001881 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001882 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001883 }
1884 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1885 if (IS_ERR(inherit)) {
1886 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001887 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001888 }
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001889
1890 if (inherit->num_qgroups > PAGE_SIZE ||
1891 inherit->num_ref_copies > PAGE_SIZE ||
1892 inherit->num_excl_copies > PAGE_SIZE) {
1893 ret = -EINVAL;
1894 goto free_inherit;
1895 }
1896
1897 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1898 2 * inherit->num_excl_copies;
1899 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1900 ret = -EINVAL;
1901 goto free_inherit;
1902 }
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001903 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001904
Christian Brauner4d4340c2021-07-27 12:48:52 +02001905 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1906 vol_args->name, vol_args->fd, subvol,
1907 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001908 if (ret)
1909 goto free_inherit;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001910free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001911 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001912free_args:
1913 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001914 return ret;
1915}
1916
Li Zefan0caa1022010-12-20 16:30:25 +08001917static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1918 void __user *arg)
1919{
Al Viro496ad9a2013-01-23 17:07:38 -05001920 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001922 struct btrfs_root *root = BTRFS_I(inode)->root;
1923 int ret = 0;
1924 u64 flags = 0;
1925
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001926 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001927 return -EINVAL;
1928
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001929 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001930 if (btrfs_root_readonly(root))
1931 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001932 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001933
1934 if (copy_to_user(arg, &flags, sizeof(flags)))
1935 ret = -EFAULT;
1936
1937 return ret;
1938}
1939
1940static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1941 void __user *arg)
1942{
Al Viro496ad9a2013-01-23 17:07:38 -05001943 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001945 struct btrfs_root *root = BTRFS_I(inode)->root;
1946 struct btrfs_trans_handle *trans;
1947 u64 root_flags;
1948 u64 flags;
1949 int ret = 0;
1950
Christian Brauner39e16742021-07-27 12:48:56 +02001951 if (!inode_owner_or_capable(file_mnt_user_ns(file), inode))
David Sterbabd60ea02014-01-16 15:50:22 +01001952 return -EPERM;
1953
Liu Bob9ca0662012-06-29 03:58:49 -06001954 ret = mnt_want_write_file(file);
1955 if (ret)
1956 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001957
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001958 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001959 ret = -EINVAL;
1960 goto out_drop_write;
1961 }
Li Zefan0caa1022010-12-20 16:30:25 +08001962
Liu Bob9ca0662012-06-29 03:58:49 -06001963 if (copy_from_user(&flags, arg, sizeof(flags))) {
1964 ret = -EFAULT;
1965 goto out_drop_write;
1966 }
Li Zefan0caa1022010-12-20 16:30:25 +08001967
Liu Bob9ca0662012-06-29 03:58:49 -06001968 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1969 ret = -EOPNOTSUPP;
1970 goto out_drop_write;
1971 }
Li Zefan0caa1022010-12-20 16:30:25 +08001972
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001974
1975 /* nothing to do */
1976 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001977 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001978
1979 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001980 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001981 btrfs_set_root_flags(&root->root_item,
1982 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001983 } else {
1984 /*
1985 * Block RO -> RW transition if this subvolume is involved in
1986 * send
1987 */
1988 spin_lock(&root->root_item_lock);
1989 if (root->send_in_progress == 0) {
1990 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001991 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001992 spin_unlock(&root->root_item_lock);
1993 } else {
1994 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001995 btrfs_warn(fs_info,
1996 "Attempt to set subvolume %llu read-write during send",
1997 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01001998 ret = -EPERM;
1999 goto out_drop_sem;
2000 }
2001 }
Li Zefan0caa1022010-12-20 16:30:25 +08002002
2003 trans = btrfs_start_transaction(root, 1);
2004 if (IS_ERR(trans)) {
2005 ret = PTR_ERR(trans);
2006 goto out_reset;
2007 }
2008
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002009 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08002010 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002011 if (ret < 0) {
2012 btrfs_end_transaction(trans);
2013 goto out_reset;
2014 }
Li Zefan0caa1022010-12-20 16:30:25 +08002015
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002016 ret = btrfs_commit_transaction(trans);
2017
Li Zefan0caa1022010-12-20 16:30:25 +08002018out_reset:
2019 if (ret)
2020 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06002021out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002022 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06002023out_drop_write:
2024 mnt_drop_write_file(file);
2025out:
Li Zefan0caa1022010-12-20 16:30:25 +08002026 return ret;
2027}
2028
Chris Masonac8e9812010-02-28 15:39:26 -05002029static noinline int key_in_sk(struct btrfs_key *key,
2030 struct btrfs_ioctl_search_key *sk)
2031{
Chris Masonabc6e132010-03-18 12:10:08 -04002032 struct btrfs_key test;
2033 int ret;
2034
2035 test.objectid = sk->min_objectid;
2036 test.type = sk->min_type;
2037 test.offset = sk->min_offset;
2038
2039 ret = btrfs_comp_cpu_keys(key, &test);
2040 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002041 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002042
2043 test.objectid = sk->max_objectid;
2044 test.type = sk->max_type;
2045 test.offset = sk->max_offset;
2046
2047 ret = btrfs_comp_cpu_keys(key, &test);
2048 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002049 return 0;
2050 return 1;
2051}
2052
Jeff Mahoneydf397562016-06-21 20:18:21 -04002053static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002054 struct btrfs_key *key,
2055 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002056 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002057 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002058 unsigned long *sk_offset,
2059 int *num_found)
2060{
2061 u64 found_transid;
2062 struct extent_buffer *leaf;
2063 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002064 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002065 unsigned long item_off;
2066 unsigned long item_len;
2067 int nritems;
2068 int i;
2069 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002070 int ret = 0;
2071
2072 leaf = path->nodes[0];
2073 slot = path->slots[0];
2074 nritems = btrfs_header_nritems(leaf);
2075
2076 if (btrfs_header_generation(leaf) > sk->max_transid) {
2077 i = nritems;
2078 goto advance_key;
2079 }
2080 found_transid = btrfs_header_generation(leaf);
2081
2082 for (i = slot; i < nritems; i++) {
2083 item_off = btrfs_item_ptr_offset(leaf, i);
2084 item_len = btrfs_item_size_nr(leaf, i);
2085
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002086 btrfs_item_key_to_cpu(leaf, key, i);
2087 if (!key_in_sk(key, sk))
2088 continue;
2089
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002090 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002091 if (*num_found) {
2092 ret = 1;
2093 goto out;
2094 }
Chris Masonac8e9812010-02-28 15:39:26 -05002095
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002096 /*
2097 * return one empty item back for v1, which does not
2098 * handle -EOVERFLOW
2099 */
2100
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002101 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002102 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002103 ret = -EOVERFLOW;
2104 }
Chris Masonac8e9812010-02-28 15:39:26 -05002105
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002106 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002107 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002108 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002109 }
2110
Chris Masonac8e9812010-02-28 15:39:26 -05002111 sh.objectid = key->objectid;
2112 sh.offset = key->offset;
2113 sh.type = key->type;
2114 sh.len = item_len;
2115 sh.transid = found_transid;
2116
Josef Bacika48b73e2020-08-10 11:42:27 -04002117 /*
2118 * Copy search result header. If we fault then loop again so we
2119 * can fault in the pages and -EFAULT there if there's a
2120 * problem. Otherwise we'll fault and then copy the buffer in
2121 * properly this next time through
2122 */
2123 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2124 ret = 0;
Gerhard Heiftba346b32014-01-30 16:24:02 +01002125 goto out;
2126 }
2127
Chris Masonac8e9812010-02-28 15:39:26 -05002128 *sk_offset += sizeof(sh);
2129
2130 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002131 char __user *up = ubuf + *sk_offset;
Josef Bacika48b73e2020-08-10 11:42:27 -04002132 /*
2133 * Copy the item, same behavior as above, but reset the
2134 * * sk_offset so we copy the full thing again.
2135 */
2136 if (read_extent_buffer_to_user_nofault(leaf, up,
2137 item_off, item_len)) {
2138 ret = 0;
2139 *sk_offset -= sizeof(sh);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002140 goto out;
2141 }
2142
Chris Masonac8e9812010-02-28 15:39:26 -05002143 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002144 }
Hugo Millse2156862011-05-14 17:43:41 +00002145 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002146
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002147 if (ret) /* -EOVERFLOW from above */
2148 goto out;
2149
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002150 if (*num_found >= sk->nr_items) {
2151 ret = 1;
2152 goto out;
2153 }
Chris Masonac8e9812010-02-28 15:39:26 -05002154 }
2155advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002156 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002157 test.objectid = sk->max_objectid;
2158 test.type = sk->max_type;
2159 test.offset = sk->max_offset;
2160 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2161 ret = 1;
2162 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002163 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002164 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002165 key->offset = 0;
2166 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002167 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002168 key->offset = 0;
2169 key->type = 0;
2170 key->objectid++;
2171 } else
2172 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002173out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002174 /*
2175 * 0: all items from this leaf copied, continue with next
2176 * 1: * more items can be copied, but unused buffer is too small
2177 * * all items were found
2178 * Either way, it will stops the loop which iterates to the next
2179 * leaf
2180 * -EOVERFLOW: item was to large for buffer
2181 * -EFAULT: could not copy extent buffer back to userspace
2182 */
Chris Masonac8e9812010-02-28 15:39:26 -05002183 return ret;
2184}
2185
2186static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002187 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002188 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002189 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002190{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002191 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002192 struct btrfs_root *root;
2193 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002194 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002195 int ret;
2196 int num_found = 0;
2197 unsigned long sk_offset = 0;
2198
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002199 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2200 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002201 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002202 }
Gerhard Heift12544442014-01-30 16:23:58 +01002203
Chris Masonac8e9812010-02-28 15:39:26 -05002204 path = btrfs_alloc_path();
2205 if (!path)
2206 return -ENOMEM;
2207
2208 if (sk->tree_id == 0) {
2209 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002210 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002211 } else {
David Sterba56e93572020-05-15 19:35:55 +02002212 root = btrfs_get_fs_root(info, sk->tree_id, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002213 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002214 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002215 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002216 }
2217 }
2218
2219 key.objectid = sk->min_objectid;
2220 key.type = sk->min_type;
2221 key.offset = sk->min_offset;
2222
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302223 while (1) {
Filipe Manana1c785442020-09-14 09:01:04 +01002224 ret = fault_in_pages_writeable(ubuf + sk_offset,
2225 *buf_size - sk_offset);
Josef Bacika48b73e2020-08-10 11:42:27 -04002226 if (ret)
2227 break;
2228
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002229 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002230 if (ret != 0) {
2231 if (ret > 0)
2232 ret = 0;
2233 goto err;
2234 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002235 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002236 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002237 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002238 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002239 break;
2240
2241 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002242 if (ret > 0)
2243 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002244err:
2245 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002246 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002247 btrfs_free_path(path);
2248 return ret;
2249}
2250
2251static noinline int btrfs_ioctl_tree_search(struct file *file,
2252 void __user *argp)
2253{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002254 struct btrfs_ioctl_search_args __user *uargs;
2255 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002256 struct inode *inode;
2257 int ret;
2258 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002259
2260 if (!capable(CAP_SYS_ADMIN))
2261 return -EPERM;
2262
Gerhard Heiftba346b32014-01-30 16:24:02 +01002263 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002264
Gerhard Heiftba346b32014-01-30 16:24:02 +01002265 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2266 return -EFAULT;
2267
2268 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002269
Al Viro496ad9a2013-01-23 17:07:38 -05002270 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002271 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002272
2273 /*
2274 * In the origin implementation an overflow is handled by returning a
2275 * search header with a len of zero, so reset ret.
2276 */
2277 if (ret == -EOVERFLOW)
2278 ret = 0;
2279
Gerhard Heiftba346b32014-01-30 16:24:02 +01002280 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002281 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002282 return ret;
2283}
2284
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002285static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2286 void __user *argp)
2287{
2288 struct btrfs_ioctl_search_args_v2 __user *uarg;
2289 struct btrfs_ioctl_search_args_v2 args;
2290 struct inode *inode;
2291 int ret;
2292 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002293 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002294
2295 if (!capable(CAP_SYS_ADMIN))
2296 return -EPERM;
2297
2298 /* copy search header and buffer size */
2299 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2300 if (copy_from_user(&args, uarg, sizeof(args)))
2301 return -EFAULT;
2302
2303 buf_size = args.buf_size;
2304
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002305 /* limit result size to 16MB */
2306 if (buf_size > buf_limit)
2307 buf_size = buf_limit;
2308
2309 inode = file_inode(file);
2310 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002311 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002312 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2313 ret = -EFAULT;
2314 else if (ret == -EOVERFLOW &&
2315 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2316 ret = -EFAULT;
2317
Yan, Zheng76dda932009-09-21 16:00:26 -04002318 return ret;
2319}
2320
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002321/*
Chris Masonac8e9812010-02-28 15:39:26 -05002322 * Search INODE_REFs to identify path name of 'dirid' directory
2323 * in a 'tree_id' tree. and sets path name to 'name'.
2324 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002325static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2326 u64 tree_id, u64 dirid, char *name)
2327{
2328 struct btrfs_root *root;
2329 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002330 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002331 int ret = -1;
2332 int slot;
2333 int len;
2334 int total_len = 0;
2335 struct btrfs_inode_ref *iref;
2336 struct extent_buffer *l;
2337 struct btrfs_path *path;
2338
2339 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2340 name[0]='\0';
2341 return 0;
2342 }
2343
2344 path = btrfs_alloc_path();
2345 if (!path)
2346 return -ENOMEM;
2347
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002348 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002349
David Sterba56e93572020-05-15 19:35:55 +02002350 root = btrfs_get_fs_root(info, tree_id, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002351 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002352 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002353 root = NULL;
2354 goto out;
2355 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002356
2357 key.objectid = dirid;
2358 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002359 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002360
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302361 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002362 ret = btrfs_search_backwards(root, &key, path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002363 if (ret < 0)
2364 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002365 else if (ret > 0) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002366 ret = -ENOENT;
2367 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002368 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002369
2370 l = path->nodes[0];
2371 slot = path->slots[0];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002372
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002373 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2374 len = btrfs_inode_ref_name_len(l, iref);
2375 ptr -= len + 1;
2376 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002377 if (ptr < name) {
2378 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002379 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002380 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002381
2382 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302383 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002384
2385 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2386 break;
2387
David Sterbab3b4aa72011-04-21 01:20:15 +02002388 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002389 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002390 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002391 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002392 }
Li Zefan77906a502011-07-14 03:16:00 +00002393 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302394 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002395 ret = 0;
2396out:
Josef Bacik00246522020-01-24 09:33:01 -05002397 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002398 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002399 return ret;
2400}
2401
Christian Brauner6623d9a2021-07-27 12:48:57 +02002402static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns,
2403 struct inode *inode,
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002404 struct btrfs_ioctl_ino_lookup_user_args *args)
2405{
2406 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2407 struct super_block *sb = inode->i_sb;
2408 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2409 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2410 u64 dirid = args->dirid;
2411 unsigned long item_off;
2412 unsigned long item_len;
2413 struct btrfs_inode_ref *iref;
2414 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002415 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002416 struct btrfs_path *path;
2417 struct btrfs_key key, key2;
2418 struct extent_buffer *leaf;
2419 struct inode *temp_inode;
2420 char *ptr;
2421 int slot;
2422 int len;
2423 int total_len = 0;
2424 int ret;
2425
2426 path = btrfs_alloc_path();
2427 if (!path)
2428 return -ENOMEM;
2429
2430 /*
2431 * If the bottom subvolume does not exist directly under upper_limit,
2432 * construct the path in from the bottom up.
2433 */
2434 if (dirid != upper_limit.objectid) {
2435 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2436
David Sterba56e93572020-05-15 19:35:55 +02002437 root = btrfs_get_fs_root(fs_info, treeid, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002438 if (IS_ERR(root)) {
2439 ret = PTR_ERR(root);
2440 goto out;
2441 }
2442
2443 key.objectid = dirid;
2444 key.type = BTRFS_INODE_REF_KEY;
2445 key.offset = (u64)-1;
2446 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002447 ret = btrfs_search_backwards(root, &key, path);
2448 if (ret < 0)
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002449 goto out_put;
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002450 else if (ret > 0) {
2451 ret = -ENOENT;
2452 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002453 }
2454
2455 leaf = path->nodes[0];
2456 slot = path->slots[0];
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002457
2458 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2459 len = btrfs_inode_ref_name_len(leaf, iref);
2460 ptr -= len + 1;
2461 total_len += len + 1;
2462 if (ptr < args->path) {
2463 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002464 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002465 }
2466
2467 *(ptr + len) = '/';
2468 read_extent_buffer(leaf, ptr,
2469 (unsigned long)(iref + 1), len);
2470
2471 /* Check the read+exec permission of this directory */
2472 ret = btrfs_previous_item(root, path, dirid,
2473 BTRFS_INODE_ITEM_KEY);
2474 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002475 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002476 } else if (ret > 0) {
2477 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002478 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002479 }
2480
2481 leaf = path->nodes[0];
2482 slot = path->slots[0];
2483 btrfs_item_key_to_cpu(leaf, &key2, slot);
2484 if (key2.objectid != dirid) {
2485 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002486 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002487 }
2488
David Sterba0202e832020-05-15 19:35:59 +02002489 temp_inode = btrfs_iget(sb, key2.objectid, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002490 if (IS_ERR(temp_inode)) {
2491 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002492 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002493 }
Christian Brauner6623d9a2021-07-27 12:48:57 +02002494 ret = inode_permission(mnt_userns, temp_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01002495 MAY_READ | MAY_EXEC);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002496 iput(temp_inode);
2497 if (ret) {
2498 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002499 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002500 }
2501
2502 if (key.offset == upper_limit.objectid)
2503 break;
2504 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2505 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002506 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002507 }
2508
2509 btrfs_release_path(path);
2510 key.objectid = key.offset;
2511 key.offset = (u64)-1;
2512 dirid = key.objectid;
2513 }
2514
2515 memmove(args->path, ptr, total_len);
2516 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002517 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002518 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002519 btrfs_release_path(path);
2520 }
2521
2522 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002523 key.objectid = treeid;
2524 key.type = BTRFS_ROOT_REF_KEY;
2525 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002526 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002527 if (ret < 0) {
2528 goto out;
2529 } else if (ret > 0) {
2530 ret = -ENOENT;
2531 goto out;
2532 }
2533
2534 leaf = path->nodes[0];
2535 slot = path->slots[0];
2536 btrfs_item_key_to_cpu(leaf, &key, slot);
2537
2538 item_off = btrfs_item_ptr_offset(leaf, slot);
2539 item_len = btrfs_item_size_nr(leaf, slot);
2540 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2541 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2542 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2543 ret = -EINVAL;
2544 goto out;
2545 }
2546
2547 /* Copy subvolume's name */
2548 item_off += sizeof(struct btrfs_root_ref);
2549 item_len -= sizeof(struct btrfs_root_ref);
2550 read_extent_buffer(leaf, args->name, item_off, item_len);
2551 args->name[item_len] = 0;
2552
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002553out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002554 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002555out:
2556 btrfs_free_path(path);
2557 return ret;
2558}
2559
Chris Masonac8e9812010-02-28 15:39:26 -05002560static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2561 void __user *argp)
2562{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002563 struct btrfs_ioctl_ino_lookup_args *args;
2564 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002565 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002566
Julia Lawall2354d08f2010-10-29 15:14:18 -04002567 args = memdup_user(argp, sizeof(*args));
2568 if (IS_ERR(args))
2569 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002570
Al Viro496ad9a2013-01-23 17:07:38 -05002571 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002572
David Sterba01b810b2015-05-12 19:14:49 +02002573 /*
2574 * Unprivileged query to obtain the containing subvolume root id. The
2575 * path is reset so it's consistent with btrfs_search_path_in_tree.
2576 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002577 if (args->treeid == 0)
2578 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2579
David Sterba01b810b2015-05-12 19:14:49 +02002580 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2581 args->name[0] = 0;
2582 goto out;
2583 }
2584
2585 if (!capable(CAP_SYS_ADMIN)) {
2586 ret = -EPERM;
2587 goto out;
2588 }
2589
Chris Masonac8e9812010-02-28 15:39:26 -05002590 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2591 args->treeid, args->objectid,
2592 args->name);
2593
David Sterba01b810b2015-05-12 19:14:49 +02002594out:
Chris Masonac8e9812010-02-28 15:39:26 -05002595 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2596 ret = -EFAULT;
2597
2598 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002599 return ret;
2600}
2601
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002602/*
2603 * Version of ino_lookup ioctl (unprivileged)
2604 *
2605 * The main differences from ino_lookup ioctl are:
2606 *
2607 * 1. Read + Exec permission will be checked using inode_permission() during
2608 * path construction. -EACCES will be returned in case of failure.
2609 * 2. Path construction will be stopped at the inode number which corresponds
2610 * to the fd with which this ioctl is called. If constructed path does not
2611 * exist under fd's inode, -EACCES will be returned.
2612 * 3. The name of bottom subvolume is also searched and filled.
2613 */
2614static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2615{
2616 struct btrfs_ioctl_ino_lookup_user_args *args;
2617 struct inode *inode;
2618 int ret;
2619
2620 args = memdup_user(argp, sizeof(*args));
2621 if (IS_ERR(args))
2622 return PTR_ERR(args);
2623
2624 inode = file_inode(file);
2625
2626 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2627 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2628 /*
2629 * The subvolume does not exist under fd with which this is
2630 * called
2631 */
2632 kfree(args);
2633 return -EACCES;
2634 }
2635
Christian Brauner6623d9a2021-07-27 12:48:57 +02002636 ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002637
2638 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2639 ret = -EFAULT;
2640
2641 kfree(args);
2642 return ret;
2643}
2644
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002645/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2646static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2647{
2648 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2649 struct btrfs_fs_info *fs_info;
2650 struct btrfs_root *root;
2651 struct btrfs_path *path;
2652 struct btrfs_key key;
2653 struct btrfs_root_item *root_item;
2654 struct btrfs_root_ref *rref;
2655 struct extent_buffer *leaf;
2656 unsigned long item_off;
2657 unsigned long item_len;
2658 struct inode *inode;
2659 int slot;
2660 int ret = 0;
2661
2662 path = btrfs_alloc_path();
2663 if (!path)
2664 return -ENOMEM;
2665
2666 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2667 if (!subvol_info) {
2668 btrfs_free_path(path);
2669 return -ENOMEM;
2670 }
2671
2672 inode = file_inode(file);
2673 fs_info = BTRFS_I(inode)->root->fs_info;
2674
2675 /* Get root_item of inode's subvolume */
2676 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
David Sterba56e93572020-05-15 19:35:55 +02002677 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002678 if (IS_ERR(root)) {
2679 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002680 goto out_free;
2681 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002682 root_item = &root->root_item;
2683
2684 subvol_info->treeid = key.objectid;
2685
2686 subvol_info->generation = btrfs_root_generation(root_item);
2687 subvol_info->flags = btrfs_root_flags(root_item);
2688
2689 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2690 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2691 BTRFS_UUID_SIZE);
2692 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2693 BTRFS_UUID_SIZE);
2694
2695 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2696 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2697 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2698
2699 subvol_info->otransid = btrfs_root_otransid(root_item);
2700 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2701 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2702
2703 subvol_info->stransid = btrfs_root_stransid(root_item);
2704 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2705 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2706
2707 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2708 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2709 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2710
2711 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2712 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002713 key.type = BTRFS_ROOT_BACKREF_KEY;
2714 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002715 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002716 if (ret < 0) {
2717 goto out;
2718 } else if (path->slots[0] >=
2719 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002720 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002721 if (ret < 0) {
2722 goto out;
2723 } else if (ret > 0) {
2724 ret = -EUCLEAN;
2725 goto out;
2726 }
2727 }
2728
2729 leaf = path->nodes[0];
2730 slot = path->slots[0];
2731 btrfs_item_key_to_cpu(leaf, &key, slot);
2732 if (key.objectid == subvol_info->treeid &&
2733 key.type == BTRFS_ROOT_BACKREF_KEY) {
2734 subvol_info->parent_id = key.offset;
2735
2736 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2737 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2738
2739 item_off = btrfs_item_ptr_offset(leaf, slot)
2740 + sizeof(struct btrfs_root_ref);
2741 item_len = btrfs_item_size_nr(leaf, slot)
2742 - sizeof(struct btrfs_root_ref);
2743 read_extent_buffer(leaf, subvol_info->name,
2744 item_off, item_len);
2745 } else {
2746 ret = -ENOENT;
2747 goto out;
2748 }
2749 }
2750
2751 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2752 ret = -EFAULT;
2753
2754out:
Josef Bacik00246522020-01-24 09:33:01 -05002755 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002756out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002757 btrfs_free_path(path);
Waiman Longb091f7f2020-06-16 11:31:59 -04002758 kfree(subvol_info);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002759 return ret;
2760}
2761
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002762/*
2763 * Return ROOT_REF information of the subvolume containing this inode
2764 * except the subvolume name.
2765 */
2766static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2767{
2768 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2769 struct btrfs_root_ref *rref;
2770 struct btrfs_root *root;
2771 struct btrfs_path *path;
2772 struct btrfs_key key;
2773 struct extent_buffer *leaf;
2774 struct inode *inode;
2775 u64 objectid;
2776 int slot;
2777 int ret;
2778 u8 found;
2779
2780 path = btrfs_alloc_path();
2781 if (!path)
2782 return -ENOMEM;
2783
2784 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2785 if (IS_ERR(rootrefs)) {
2786 btrfs_free_path(path);
2787 return PTR_ERR(rootrefs);
2788 }
2789
2790 inode = file_inode(file);
2791 root = BTRFS_I(inode)->root->fs_info->tree_root;
2792 objectid = BTRFS_I(inode)->root->root_key.objectid;
2793
2794 key.objectid = objectid;
2795 key.type = BTRFS_ROOT_REF_KEY;
2796 key.offset = rootrefs->min_treeid;
2797 found = 0;
2798
2799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2800 if (ret < 0) {
2801 goto out;
2802 } else if (path->slots[0] >=
2803 btrfs_header_nritems(path->nodes[0])) {
2804 ret = btrfs_next_leaf(root, path);
2805 if (ret < 0) {
2806 goto out;
2807 } else if (ret > 0) {
2808 ret = -EUCLEAN;
2809 goto out;
2810 }
2811 }
2812 while (1) {
2813 leaf = path->nodes[0];
2814 slot = path->slots[0];
2815
2816 btrfs_item_key_to_cpu(leaf, &key, slot);
2817 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2818 ret = 0;
2819 goto out;
2820 }
2821
2822 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2823 ret = -EOVERFLOW;
2824 goto out;
2825 }
2826
2827 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2828 rootrefs->rootref[found].treeid = key.offset;
2829 rootrefs->rootref[found].dirid =
2830 btrfs_root_ref_dirid(leaf, rref);
2831 found++;
2832
2833 ret = btrfs_next_item(root, path);
2834 if (ret < 0) {
2835 goto out;
2836 } else if (ret > 0) {
2837 ret = -EUCLEAN;
2838 goto out;
2839 }
2840 }
2841
2842out:
2843 if (!ret || ret == -EOVERFLOW) {
2844 rootrefs->num_items = found;
2845 /* update min_treeid for next search */
2846 if (found)
2847 rootrefs->min_treeid =
2848 rootrefs->rootref[found - 1].treeid + 1;
2849 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2850 ret = -EFAULT;
2851 }
2852
2853 kfree(rootrefs);
2854 btrfs_free_path(path);
2855
2856 return ret;
2857}
2858
Yan, Zheng76dda932009-09-21 16:00:26 -04002859static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002860 void __user *arg,
2861 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04002862{
Al Viro54563d42013-09-01 15:57:51 -04002863 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002864 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002865 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002866 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002867 struct inode *inode;
2868 struct btrfs_root *root = BTRFS_I(dir)->root;
2869 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002870 struct btrfs_ioctl_vol_args *vol_args = NULL;
2871 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002872 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002873 char *subvol_name, *subvol_name_ptr = NULL;
2874 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002875 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002876 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04002877
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002878 if (destroy_v2) {
2879 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2880 if (IS_ERR(vol_args2))
2881 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002882
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002883 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2884 err = -EOPNOTSUPP;
2885 goto out;
2886 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002887
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002888 /*
2889 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2890 * name, same as v1 currently does.
2891 */
2892 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2893 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2894 subvol_name = vol_args2->name;
2895
2896 err = mnt_want_write_file(file);
2897 if (err)
2898 goto out;
2899 } else {
Christian Brauneraabb34e2021-07-27 12:48:54 +02002900 struct inode *old_dir;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002901
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002902 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2903 err = -EINVAL;
2904 goto out;
2905 }
2906
2907 err = mnt_want_write_file(file);
2908 if (err)
2909 goto out;
2910
2911 dentry = btrfs_get_dentry(fs_info->sb,
2912 BTRFS_FIRST_FREE_OBJECTID,
2913 vol_args2->subvolid, 0, 0);
2914 if (IS_ERR(dentry)) {
2915 err = PTR_ERR(dentry);
2916 goto out_drop_write;
2917 }
2918
2919 /*
2920 * Change the default parent since the subvolume being
2921 * deleted can be outside of the current mount point.
2922 */
2923 parent = btrfs_get_parent(dentry);
2924
2925 /*
2926 * At this point dentry->d_name can point to '/' if the
2927 * subvolume we want to destroy is outsite of the
2928 * current mount point, so we need to release the
2929 * current dentry and execute the lookup to return a new
2930 * one with ->d_name pointing to the
2931 * <mount point>/subvol_name.
2932 */
2933 dput(dentry);
2934 if (IS_ERR(parent)) {
2935 err = PTR_ERR(parent);
2936 goto out_drop_write;
2937 }
Christian Brauneraabb34e2021-07-27 12:48:54 +02002938 old_dir = dir;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002939 dir = d_inode(parent);
2940
2941 /*
2942 * If v2 was used with SPEC_BY_ID, a new parent was
2943 * allocated since the subvolume can be outside of the
2944 * current mount point. Later on we need to release this
2945 * new parent dentry.
2946 */
2947 destroy_parent = true;
2948
Christian Brauneraabb34e2021-07-27 12:48:54 +02002949 /*
2950 * On idmapped mounts, deletion via subvolid is
2951 * restricted to subvolumes that are immediate
2952 * ancestors of the inode referenced by the file
2953 * descriptor in the ioctl. Otherwise the idmapping
2954 * could potentially be abused to delete subvolumes
2955 * anywhere in the filesystem the user wouldn't be able
2956 * to delete without an idmapped mount.
2957 */
2958 if (old_dir != dir && mnt_userns != &init_user_ns) {
2959 err = -EOPNOTSUPP;
2960 goto free_parent;
2961 }
2962
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002963 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2964 fs_info, vol_args2->subvolid);
2965 if (IS_ERR(subvol_name_ptr)) {
2966 err = PTR_ERR(subvol_name_ptr);
2967 goto free_parent;
2968 }
David Sterba1a9fd412021-05-21 17:42:23 +02002969 /* subvol_name_ptr is already nul terminated */
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002970 subvol_name = (char *)kbasename(subvol_name_ptr);
2971 }
2972 } else {
2973 vol_args = memdup_user(arg, sizeof(*vol_args));
2974 if (IS_ERR(vol_args))
2975 return PTR_ERR(vol_args);
2976
2977 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2978 subvol_name = vol_args->name;
2979
2980 err = mnt_want_write_file(file);
2981 if (err)
2982 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002983 }
2984
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002985 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04002986
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002987 if (strchr(subvol_name, '/') ||
2988 strncmp(subvol_name, "..", subvol_namelen) == 0) {
2989 err = -EINVAL;
2990 goto free_subvol_name;
2991 }
2992
2993 if (!S_ISDIR(dir->i_mode)) {
2994 err = -ENOTDIR;
2995 goto free_subvol_name;
2996 }
David Sterba521e0542014-04-15 16:41:44 +02002997
Al Viro00235412016-05-26 00:05:12 -04002998 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
2999 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003000 goto free_subvol_name;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003001 dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04003002 if (IS_ERR(dentry)) {
3003 err = PTR_ERR(dentry);
3004 goto out_unlock_dir;
3005 }
3006
David Howells2b0143b2015-03-17 22:25:59 +00003007 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003008 err = -ENOENT;
3009 goto out_dput;
3010 }
3011
David Howells2b0143b2015-03-17 22:25:59 +00003012 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04003013 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303014 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04003015 /*
3016 * Regular user. Only allow this with a special mount
3017 * option, when the user has write+exec access to the
3018 * subvol root, and when rmdir(2) would have been
3019 * allowed.
3020 *
3021 * Note that this is _not_ check that the subvol is
3022 * empty or doesn't contain data that we wouldn't
3023 * otherwise be able to delete.
3024 *
3025 * Users who want to delete empty subvols should try
3026 * rmdir(2).
3027 */
3028 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04003030 goto out_dput;
3031
3032 /*
3033 * Do not allow deletion if the parent dir is the same
3034 * as the dir to be deleted. That means the ioctl
3035 * must be called on the dentry referencing the root
3036 * of the subvol, not a random directory contained
3037 * within it.
3038 */
3039 err = -EINVAL;
3040 if (root == dest)
3041 goto out_dput;
3042
Christian Braunerc4ed5332021-07-27 12:48:53 +02003043 err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -04003044 if (err)
3045 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04003046 }
3047
Miao Xie5c39da52012-10-22 11:39:53 +00003048 /* check if subvolume may be deleted by a user */
Christian Braunerc4ed5332021-07-27 12:48:53 +02003049 err = btrfs_may_delete(mnt_userns, dir, dentry, 1);
Miao Xie5c39da52012-10-22 11:39:53 +00003050 if (err)
3051 goto out_dput;
3052
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003053 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003054 err = -EINVAL;
3055 goto out_dput;
3056 }
3057
Josef Bacik64708532021-02-10 17:14:34 -05003058 btrfs_inode_lock(inode, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003059 err = btrfs_delete_subvolume(dir, dentry);
Josef Bacik64708532021-02-10 17:14:34 -05003060 btrfs_inode_unlock(inode, 0);
Amir Goldstein46008d92019-05-26 17:34:03 +03003061 if (!err) {
3062 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04003063 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03003064 }
Liu Bofa6ac872013-02-20 14:10:23 +00003065
Yan, Zheng76dda932009-09-21 16:00:26 -04003066out_dput:
3067 dput(dentry);
3068out_unlock_dir:
Josef Bacik64708532021-02-10 17:14:34 -05003069 btrfs_inode_unlock(dir, 0);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003070free_subvol_name:
3071 kfree(subvol_name_ptr);
3072free_parent:
3073 if (destroy_parent)
3074 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003075out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003076 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003077out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003078 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003079 kfree(vol_args);
3080 return err;
3081}
3082
Chris Mason1e701a32010-03-11 09:42:04 -05003083static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003084{
Al Viro496ad9a2013-01-23 17:07:38 -05003085 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003086 struct btrfs_root *root = BTRFS_I(inode)->root;
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003087 struct btrfs_ioctl_defrag_range_args range = {0};
Yan Zhengc146afa2008-11-12 14:34:12 -05003088 int ret;
3089
Ilya Dryomov25122d12013-01-20 15:57:57 +02003090 ret = mnt_want_write_file(file);
3091 if (ret)
3092 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003093
Ilya Dryomov25122d12013-01-20 15:57:57 +02003094 if (btrfs_root_readonly(root)) {
3095 ret = -EROFS;
3096 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003097 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003098
3099 switch (inode->i_mode & S_IFMT) {
3100 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003101 if (!capable(CAP_SYS_ADMIN)) {
3102 ret = -EPERM;
3103 goto out;
3104 }
Eric Sandeende78b512013-01-31 18:21:12 +00003105 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003106 break;
3107 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003108 /*
3109 * Note that this does not check the file descriptor for write
3110 * access. This prevents defragmenting executables that are
3111 * running and allows defrag on files open in read-only mode.
3112 */
3113 if (!capable(CAP_SYS_ADMIN) &&
Christian Brauner47291ba2021-01-21 14:19:24 +01003114 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
Adam Borowski616d3742018-07-18 00:08:59 +02003115 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003116 goto out;
3117 }
Chris Mason1e701a32010-03-11 09:42:04 -05003118
Chris Mason1e701a32010-03-11 09:42:04 -05003119 if (argp) {
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003120 if (copy_from_user(&range, argp, sizeof(range))) {
Chris Mason1e701a32010-03-11 09:42:04 -05003121 ret = -EFAULT;
Dan Carpenter683be162010-03-20 11:24:48 +00003122 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003123 }
3124 /* compression requires us to start the IO */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003125 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3126 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
3127 range.extent_thresh = (u32)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003128 }
3129 } else {
3130 /* the rest are all set to zero by kzalloc */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003131 range.len = (u64)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003132 }
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08003133 ret = btrfs_defrag_file(file_inode(file), &file->f_ra,
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003134 &range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003135 if (ret > 0)
3136 ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003137 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003138 default:
3139 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003140 }
Chris Masone441d542009-01-05 16:57:23 -05003141out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003142 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003143 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003144}
3145
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003146static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003147{
3148 struct btrfs_ioctl_vol_args *vol_args;
3149 int ret;
3150
Chris Masone441d542009-01-05 16:57:23 -05003151 if (!capable(CAP_SYS_ADMIN))
3152 return -EPERM;
3153
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003154 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
Anand Jaine57138b2013-08-21 11:44:48 +08003155 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003156
Li Zefandae7b662009-04-08 15:06:54 +08003157 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003158 if (IS_ERR(vol_args)) {
3159 ret = PTR_ERR(vol_args);
3160 goto out;
3161 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003162
Mark Fasheh5516e592008-07-24 12:20:14 -04003163 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003164 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003165
Anand Jain43d20762014-07-01 00:58:56 +08003166 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003167 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003168
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003169 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003170out:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003171 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003172 return ret;
3173}
3174
Anand Jain6b526ed2016-02-13 10:01:39 +08003175static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003176{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003177 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003178 struct inode *inode = file_inode(file);
3179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003180 struct btrfs_ioctl_vol_args_v2 *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003181 struct block_device *bdev = NULL;
3182 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003183 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003184 bool cancel = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003185
Chris Masone441d542009-01-05 16:57:23 -05003186 if (!capable(CAP_SYS_ADMIN))
3187 return -EPERM;
3188
Li Zefandae7b662009-04-08 15:06:54 +08003189 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003190 if (IS_ERR(vol_args)) {
3191 ret = PTR_ERR(vol_args);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003192 goto out;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003193 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003194
David Sterba748449cdb2020-02-21 13:30:14 +01003195 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003196 ret = -EOPNOTSUPP;
3197 goto out;
3198 }
Josef Bacik1a15eb72021-10-05 16:12:44 -04003199
David Sterba67ae34b2021-05-14 21:21:27 +02003200 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Josef Bacik1a15eb72021-10-05 16:12:44 -04003201 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3202 args.devid = vol_args->devid;
3203 } else if (!strcmp("cancel", vol_args->name)) {
David Sterba67ae34b2021-05-14 21:21:27 +02003204 cancel = true;
Josef Bacik1a15eb72021-10-05 16:12:44 -04003205 } else {
3206 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3207 if (ret)
3208 goto out;
3209 }
3210
3211 ret = mnt_want_write_file(file);
3212 if (ret)
3213 goto out;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003214
David Sterba67ae34b2021-05-14 21:21:27 +02003215 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3216 cancel);
3217 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003218 goto err_drop;
Anand Jain183860f2013-05-17 10:52:45 +00003219
Josef Bacik1a15eb72021-10-05 16:12:44 -04003220 /* Exclusive operation is now claimed */
3221 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003222
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003223 btrfs_exclop_finish(fs_info);
Anand Jain183860f2013-05-17 10:52:45 +00003224
Anand Jain6b526ed2016-02-13 10:01:39 +08003225 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003226 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003227 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003228 vol_args->devid);
3229 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003230 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003231 vol_args->name);
3232 }
Dan Carpenterc47ca322014-09-04 14:09:15 +03003233err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003234 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003235 if (bdev)
3236 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003237out:
3238 btrfs_put_dev_args_from_path(&args);
3239 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003240 return ret;
3241}
3242
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003243static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3244{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003245 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003246 struct inode *inode = file_inode(file);
3247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003248 struct btrfs_ioctl_vol_args *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003249 struct block_device *bdev = NULL;
3250 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003251 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003252 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003253
3254 if (!capable(CAP_SYS_ADMIN))
3255 return -EPERM;
3256
Josef Bacik1a15eb72021-10-05 16:12:44 -04003257 vol_args = memdup_user(arg, sizeof(*vol_args));
3258 if (IS_ERR(vol_args))
3259 return PTR_ERR(vol_args);
3260
3261 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3262 if (!strcmp("cancel", vol_args->name)) {
3263 cancel = true;
3264 } else {
3265 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3266 if (ret)
3267 goto out;
3268 }
3269
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003270 ret = mnt_want_write_file(file);
3271 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003272 goto out;
David Sterba67ae34b2021-05-14 21:21:27 +02003273
3274 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3275 cancel);
3276 if (ret == 0) {
Josef Bacik1a15eb72021-10-05 16:12:44 -04003277 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003278 if (!ret)
3279 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3280 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003281 }
3282
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003283 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003284 if (bdev)
3285 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003286out:
3287 btrfs_put_dev_args_from_path(&args);
3288 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003289 return ret;
3290}
3291
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003292static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3293 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003294{
Li Zefan027ed2f2011-06-08 08:27:56 +00003295 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003296 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003297 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003298 u64 flags_in;
Li Zefan027ed2f2011-06-08 08:27:56 +00003299 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003300
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003301 fi_args = memdup_user(arg, sizeof(*fi_args));
3302 if (IS_ERR(fi_args))
3303 return PTR_ERR(fi_args);
3304
3305 flags_in = fi_args->flags;
3306 memset(fi_args, 0, sizeof(*fi_args));
Li Zefan027ed2f2011-06-08 08:27:56 +00003307
David Sterbad03262c2017-06-16 00:09:21 +02003308 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003309 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003310
David Sterbad03262c2017-06-16 00:09:21 +02003311 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003312 if (device->devid > fi_args->max_id)
3313 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003314 }
David Sterbad03262c2017-06-16 00:09:21 +02003315 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003316
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003317 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003318 fi_args->nodesize = fs_info->nodesize;
3319 fi_args->sectorsize = fs_info->sectorsize;
3320 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003321
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003322 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3323 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3324 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3325 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3326 }
3327
Johannes Thumshirn0fb408a2020-07-13 21:28:59 +09003328 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3329 fi_args->generation = fs_info->generation;
3330 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3331 }
3332
Johannes Thumshirn49bac892020-07-13 21:29:00 +09003333 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3334 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3335 sizeof(fi_args->metadata_uuid));
3336 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3337 }
3338
Li Zefan027ed2f2011-06-08 08:27:56 +00003339 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3340 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003341
Li Zefan027ed2f2011-06-08 08:27:56 +00003342 kfree(fi_args);
3343 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003344}
3345
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003346static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3347 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003348{
Josef Bacik562d7b12021-10-05 16:12:42 -04003349 BTRFS_DEV_LOOKUP_ARGS(args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003350 struct btrfs_ioctl_dev_info_args *di_args;
3351 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003352 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003353
Jan Schmidt475f6382011-03-11 15:41:01 +01003354 di_args = memdup_user(arg, sizeof(*di_args));
3355 if (IS_ERR(di_args))
3356 return PTR_ERR(di_args);
3357
Josef Bacik562d7b12021-10-05 16:12:42 -04003358 args.devid = di_args->devid;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003359 if (!btrfs_is_empty_uuid(di_args->uuid))
Josef Bacik562d7b12021-10-05 16:12:42 -04003360 args.uuid = di_args->uuid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003361
David Sterbac5593ca2017-06-16 00:09:21 +02003362 rcu_read_lock();
Josef Bacik562d7b12021-10-05 16:12:42 -04003363 dev = btrfs_find_device(fs_info->fs_devices, &args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003364 if (!dev) {
3365 ret = -ENODEV;
3366 goto out;
3367 }
3368
3369 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003370 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3371 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003372 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003373 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003374 strncpy(di_args->path, rcu_str_deref(dev->name),
3375 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003376 di_args->path[sizeof(di_args->path) - 1] = 0;
3377 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003378 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003379 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003380
3381out:
David Sterbac5593ca2017-06-16 00:09:21 +02003382 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003383 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3384 ret = -EFAULT;
3385
3386 kfree(di_args);
3387 return ret;
3388}
3389
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003390static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3391{
Al Viro496ad9a2013-01-23 17:07:38 -05003392 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003394 struct btrfs_root *root = BTRFS_I(inode)->root;
3395 struct btrfs_root *new_root;
3396 struct btrfs_dir_item *di;
3397 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003398 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003399 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003400 u64 objectid = 0;
3401 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003402 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003403
3404 if (!capable(CAP_SYS_ADMIN))
3405 return -EPERM;
3406
Miao Xie3c04ce02012-11-26 08:43:07 +00003407 ret = mnt_want_write_file(file);
3408 if (ret)
3409 return ret;
3410
3411 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3412 ret = -EFAULT;
3413 goto out;
3414 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003415
3416 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303417 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003418
David Sterba56e93572020-05-15 19:35:55 +02003419 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00003420 if (IS_ERR(new_root)) {
3421 ret = PTR_ERR(new_root);
3422 goto out;
3423 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003424 if (!is_fstree(new_root->root_key.objectid)) {
3425 ret = -ENOENT;
3426 goto out_free;
3427 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003428
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003429 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003430 if (!path) {
3431 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003432 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00003433 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003434
3435 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003436 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00003437 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003438 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003439 }
3440
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003441 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3442 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003443 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003444 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003445 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003446 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003447 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003448 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003449 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003450 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003451 }
3452
3453 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3454 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3455 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003456 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003457
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003459 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003460out_free:
Josef Bacik00246522020-01-24 09:33:01 -05003461 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003462 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003463out:
3464 mnt_drop_write_file(file);
3465 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003466}
3467
Su Yuec065f5b12018-04-02 17:24:11 +08003468static void get_block_group_info(struct list_head *groups_list,
3469 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003470{
David Sterba32da53862019-10-29 19:20:18 +01003471 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003472
3473 space->total_bytes = 0;
3474 space->used_bytes = 0;
3475 space->flags = 0;
3476 list_for_each_entry(block_group, groups_list, list) {
3477 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02003478 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02003479 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003480 }
3481}
3482
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003483static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3484 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003485{
3486 struct btrfs_ioctl_space_args space_args;
3487 struct btrfs_ioctl_space_info space;
3488 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003489 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003490 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003491 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003492 static const u64 types[] = {
3493 BTRFS_BLOCK_GROUP_DATA,
3494 BTRFS_BLOCK_GROUP_SYSTEM,
3495 BTRFS_BLOCK_GROUP_METADATA,
3496 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3497 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003498 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003499 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003500 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003501 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003502 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003503
3504 if (copy_from_user(&space_args,
3505 (struct btrfs_ioctl_space_args __user *)arg,
3506 sizeof(space_args)))
3507 return -EFAULT;
3508
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003509 for (i = 0; i < num_types; i++) {
3510 struct btrfs_space_info *tmp;
3511
3512 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003513 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003514 if (tmp->flags == types[i]) {
3515 info = tmp;
3516 break;
3517 }
3518 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003519
3520 if (!info)
3521 continue;
3522
3523 down_read(&info->groups_sem);
3524 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3525 if (!list_empty(&info->block_groups[c]))
3526 slot_count++;
3527 }
3528 up_read(&info->groups_sem);
3529 }
Josef Bacik1406e432010-01-13 18:19:06 +00003530
David Sterba36523e952014-02-07 14:34:12 +01003531 /*
3532 * Global block reserve, exported as a space_info
3533 */
3534 slot_count++;
3535
Chris Mason7fde62b2010-03-16 15:40:10 -04003536 /* space_slots == 0 means they are asking for a count */
3537 if (space_args.space_slots == 0) {
3538 space_args.total_spaces = slot_count;
3539 goto out;
3540 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003541
Dan Rosenberg51788b12011-02-14 16:04:23 -05003542 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003543
Chris Mason7fde62b2010-03-16 15:40:10 -04003544 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003545
Chris Mason7fde62b2010-03-16 15:40:10 -04003546 /* we generally have at most 6 or so space infos, one for each raid
3547 * level. So, a whole page should be more than enough for everyone
3548 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003549 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003550 return -ENOMEM;
3551
3552 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003553 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003554 if (!dest)
3555 return -ENOMEM;
3556 dest_orig = dest;
3557
3558 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003559 for (i = 0; i < num_types; i++) {
3560 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003561
Dan Rosenberg51788b12011-02-14 16:04:23 -05003562 if (!slot_count)
3563 break;
3564
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003565 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003566 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003567 if (tmp->flags == types[i]) {
3568 info = tmp;
3569 break;
3570 }
3571 }
Chris Mason7fde62b2010-03-16 15:40:10 -04003572
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003573 if (!info)
3574 continue;
3575 down_read(&info->groups_sem);
3576 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3577 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003578 get_block_group_info(&info->block_groups[c],
3579 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003580 memcpy(dest, &space, sizeof(space));
3581 dest++;
3582 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003583 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003584 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003585 if (!slot_count)
3586 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003587 }
3588 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003589 }
Josef Bacik1406e432010-01-13 18:19:06 +00003590
David Sterba36523e952014-02-07 14:34:12 +01003591 /*
3592 * Add global block reserve
3593 */
3594 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003595 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003596
3597 spin_lock(&block_rsv->lock);
3598 space.total_bytes = block_rsv->size;
3599 space.used_bytes = block_rsv->size - block_rsv->reserved;
3600 spin_unlock(&block_rsv->lock);
3601 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3602 memcpy(dest, &space, sizeof(space));
3603 space_args.total_spaces++;
3604 }
3605
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003606 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003607 (arg + sizeof(struct btrfs_ioctl_space_args));
3608
3609 if (copy_to_user(user_dest, dest_orig, alloc_size))
3610 ret = -EFAULT;
3611
3612 kfree(dest_orig);
3613out:
3614 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003615 ret = -EFAULT;
3616
3617 return ret;
3618}
3619
Miao Xie9a8c28b2012-11-26 08:40:43 +00003620static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3621 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003622{
Sage Weil46204592010-10-29 15:41:32 -04003623 struct btrfs_trans_handle *trans;
3624 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003625 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003626
Miao Xied4edf392013-02-20 09:17:06 +00003627 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003628 if (IS_ERR(trans)) {
3629 if (PTR_ERR(trans) != -ENOENT)
3630 return PTR_ERR(trans);
3631
3632 /* No running transaction, don't bother */
3633 transid = root->fs_info->last_trans_committed;
3634 goto out;
3635 }
Sage Weil46204592010-10-29 15:41:32 -04003636 transid = trans->transid;
David Sterba32cc4f82021-06-03 17:20:21 +02003637 ret = btrfs_commit_transaction_async(trans);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003638 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003639 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003640 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003641 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003642out:
Sage Weil46204592010-10-29 15:41:32 -04003643 if (argp)
3644 if (copy_to_user(argp, &transid, sizeof(transid)))
3645 return -EFAULT;
3646 return 0;
3647}
3648
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003649static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00003650 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003651{
Sage Weil46204592010-10-29 15:41:32 -04003652 u64 transid;
3653
3654 if (argp) {
3655 if (copy_from_user(&transid, argp, sizeof(transid)))
3656 return -EFAULT;
3657 } else {
3658 transid = 0; /* current trans */
3659 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003660 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04003661}
3662
Miao Xieb8e95482012-11-26 08:48:01 +00003663static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003664{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003665 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01003666 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003667 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003668
3669 if (!capable(CAP_SYS_ADMIN))
3670 return -EPERM;
3671
3672 sa = memdup_user(arg, sizeof(*sa));
3673 if (IS_ERR(sa))
3674 return PTR_ERR(sa);
3675
Miao Xieb8e95482012-11-26 08:48:01 +00003676 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3677 ret = mnt_want_write_file(file);
3678 if (ret)
3679 goto out;
3680 }
3681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003682 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003683 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3684 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003685
Filipe Manana5afe6ce2020-01-16 11:29:20 +00003686 /*
3687 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3688 * error. This is important as it allows user space to know how much
3689 * progress scrub has done. For example, if scrub is canceled we get
3690 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3691 * space. Later user space can inspect the progress from the structure
3692 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3693 * previously (btrfs-progs does this).
3694 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3695 * then return -EFAULT to signal the structure was not copied or it may
3696 * be corrupt and unreliable due to a partial copy.
3697 */
3698 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003699 ret = -EFAULT;
3700
Miao Xieb8e95482012-11-26 08:48:01 +00003701 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3702 mnt_drop_write_file(file);
3703out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003704 kfree(sa);
3705 return ret;
3706}
3707
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003708static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01003709{
3710 if (!capable(CAP_SYS_ADMIN))
3711 return -EPERM;
3712
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003713 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003714}
3715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003716static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01003717 void __user *arg)
3718{
3719 struct btrfs_ioctl_scrub_args *sa;
3720 int ret;
3721
3722 if (!capable(CAP_SYS_ADMIN))
3723 return -EPERM;
3724
3725 sa = memdup_user(arg, sizeof(*sa));
3726 if (IS_ERR(sa))
3727 return PTR_ERR(sa);
3728
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003729 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01003730
Filipe Manana4fa99b02018-12-14 19:45:13 +00003731 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003732 ret = -EFAULT;
3733
3734 kfree(sa);
3735 return ret;
3736}
3737
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003738static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06003739 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003740{
3741 struct btrfs_ioctl_get_dev_stats *sa;
3742 int ret;
3743
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003744 sa = memdup_user(arg, sizeof(*sa));
3745 if (IS_ERR(sa))
3746 return PTR_ERR(sa);
3747
David Sterbab27f7c02012-06-22 06:30:39 -06003748 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3749 kfree(sa);
3750 return -EPERM;
3751 }
3752
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003753 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003754
Filipe Mananaeee99572018-12-14 19:45:22 +00003755 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003756 ret = -EFAULT;
3757
3758 kfree(sa);
3759 return ret;
3760}
3761
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003762static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3763 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003764{
3765 struct btrfs_ioctl_dev_replace_args *p;
3766 int ret;
3767
3768 if (!capable(CAP_SYS_ADMIN))
3769 return -EPERM;
3770
3771 p = memdup_user(arg, sizeof(*p));
3772 if (IS_ERR(p))
3773 return PTR_ERR(p);
3774
3775 switch (p->cmd) {
3776 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01003777 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003778 ret = -EROFS;
3779 goto out;
3780 }
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003781 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003782 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003783 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003784 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003785 btrfs_exclop_finish(fs_info);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003786 }
3787 break;
3788 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003789 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003790 ret = 0;
3791 break;
3792 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08003793 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08003794 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003795 break;
3796 default:
3797 ret = -EINVAL;
3798 break;
3799 }
3800
Filipe Mananad3a53282019-01-08 11:42:09 +00003801 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003802 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003803out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003804 kfree(p);
3805 return ret;
3806}
3807
Jan Schmidtd7728c92011-07-07 16:48:38 +02003808static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3809{
3810 int ret = 0;
3811 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003812 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003813 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003814 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003815 struct inode_fs_paths *ipath = NULL;
3816 struct btrfs_path *path;
3817
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003818 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003819 return -EPERM;
3820
3821 path = btrfs_alloc_path();
3822 if (!path) {
3823 ret = -ENOMEM;
3824 goto out;
3825 }
3826
3827 ipa = memdup_user(arg, sizeof(*ipa));
3828 if (IS_ERR(ipa)) {
3829 ret = PTR_ERR(ipa);
3830 ipa = NULL;
3831 goto out;
3832 }
3833
3834 size = min_t(u32, ipa->size, 4096);
3835 ipath = init_ipath(size, root, path);
3836 if (IS_ERR(ipath)) {
3837 ret = PTR_ERR(ipath);
3838 ipath = NULL;
3839 goto out;
3840 }
3841
3842 ret = paths_from_inode(ipa->inum, ipath);
3843 if (ret < 0)
3844 goto out;
3845
3846 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003847 rel_ptr = ipath->fspath->val[i] -
3848 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003849 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003850 }
3851
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003852 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3853 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003854 if (ret) {
3855 ret = -EFAULT;
3856 goto out;
3857 }
3858
3859out:
3860 btrfs_free_path(path);
3861 free_ipath(ipath);
3862 kfree(ipa);
3863
3864 return ret;
3865}
3866
3867static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3868{
3869 struct btrfs_data_container *inodes = ctx;
3870 const size_t c = 3 * sizeof(u64);
3871
3872 if (inodes->bytes_left >= c) {
3873 inodes->bytes_left -= c;
3874 inodes->val[inodes->elem_cnt] = inum;
3875 inodes->val[inodes->elem_cnt + 1] = offset;
3876 inodes->val[inodes->elem_cnt + 2] = root;
3877 inodes->elem_cnt += 3;
3878 } else {
3879 inodes->bytes_missing += c - inodes->bytes_left;
3880 inodes->bytes_left = 0;
3881 inodes->elem_missed += 3;
3882 }
3883
3884 return 0;
3885}
3886
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003887static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003888 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003889{
3890 int ret = 0;
3891 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003892 struct btrfs_ioctl_logical_ino_args *loi;
3893 struct btrfs_data_container *inodes = NULL;
3894 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003895 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003896
3897 if (!capable(CAP_SYS_ADMIN))
3898 return -EPERM;
3899
3900 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05303901 if (IS_ERR(loi))
3902 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003903
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003904 if (version == 1) {
3905 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003906 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003907 } else {
3908 /* All reserved bits must be 0 for now */
3909 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3910 ret = -EINVAL;
3911 goto out_loi;
3912 }
3913 /* Only accept flags we have defined so far */
3914 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3915 ret = -EINVAL;
3916 goto out_loi;
3917 }
3918 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003919 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003920 }
3921
Jan Schmidtd7728c92011-07-07 16:48:38 +02003922 path = btrfs_alloc_path();
3923 if (!path) {
3924 ret = -ENOMEM;
3925 goto out;
3926 }
3927
Jan Schmidtd7728c92011-07-07 16:48:38 +02003928 inodes = init_data_container(size);
3929 if (IS_ERR(inodes)) {
3930 ret = PTR_ERR(inodes);
3931 inodes = NULL;
3932 goto out;
3933 }
3934
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003935 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003936 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06003937 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003938 ret = -ENOENT;
3939 if (ret < 0)
3940 goto out;
3941
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003942 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3943 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003944 if (ret)
3945 ret = -EFAULT;
3946
3947out:
3948 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02003949 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003950out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02003951 kfree(loi);
3952
3953 return ret;
3954}
3955
David Sterba008ef092018-03-21 02:05:27 +01003956void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003957 struct btrfs_ioctl_balance_args *bargs)
3958{
3959 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3960
3961 bargs->flags = bctl->flags;
3962
David Sterba3009a622018-03-21 01:31:04 +01003963 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003964 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3965 if (atomic_read(&fs_info->balance_pause_req))
3966 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003967 if (atomic_read(&fs_info->balance_cancel_req))
3968 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003969
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003970 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3971 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3972 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003973
David Sterba008ef092018-03-21 02:05:27 +01003974 spin_lock(&fs_info->balance_lock);
3975 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3976 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003977}
3978
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003979static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003980{
Al Viro496ad9a2013-01-23 17:07:38 -05003981 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003982 struct btrfs_fs_info *fs_info = root->fs_info;
3983 struct btrfs_ioctl_balance_args *bargs;
3984 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003985 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003986 int ret;
3987
3988 if (!capable(CAP_SYS_ADMIN))
3989 return -EPERM;
3990
Liu Boe54bfa312012-06-29 03:58:48 -06003991 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003992 if (ret)
3993 return ret;
3994
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003995again:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003996 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003997 mutex_lock(&fs_info->balance_mutex);
3998 need_unlock = true;
3999 goto locked;
4000 }
4001
4002 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004003 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004004 * (1) some other op is running
4005 * (2) balance is running
4006 * (3) balance is paused -- special case (think resume)
4007 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004008 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004009 if (fs_info->balance_ctl) {
4010 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004011 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004012 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004013 /*
4014 * Lock released to allow other waiters to continue,
4015 * we'll reexamine the status again.
4016 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004017 mutex_lock(&fs_info->balance_mutex);
4018
4019 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004020 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004021 /* this is (3) */
4022 need_unlock = false;
4023 goto locked;
4024 }
4025
4026 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004027 goto again;
4028 } else {
4029 /* this is (2) */
4030 mutex_unlock(&fs_info->balance_mutex);
4031 ret = -EINPROGRESS;
4032 goto out;
4033 }
4034 } else {
4035 /* this is (1) */
4036 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004037 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004038 goto out;
4039 }
4040
4041locked:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004042
4043 if (arg) {
4044 bargs = memdup_user(arg, sizeof(*bargs));
4045 if (IS_ERR(bargs)) {
4046 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004047 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004048 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004049
4050 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4051 if (!fs_info->balance_ctl) {
4052 ret = -ENOTCONN;
4053 goto out_bargs;
4054 }
4055
4056 bctl = fs_info->balance_ctl;
4057 spin_lock(&fs_info->balance_lock);
4058 bctl->flags |= BTRFS_BALANCE_RESUME;
4059 spin_unlock(&fs_info->balance_lock);
4060
4061 goto do_balance;
4062 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004063 } else {
4064 bargs = NULL;
4065 }
4066
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004067 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004068 ret = -EINPROGRESS;
4069 goto out_bargs;
4070 }
4071
David Sterba8d2db782015-11-04 15:38:29 +01004072 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004073 if (!bctl) {
4074 ret = -ENOMEM;
4075 goto out_bargs;
4076 }
4077
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004078 if (arg) {
4079 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4080 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4081 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4082
4083 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004084 } else {
4085 /* balance everything - no filters */
4086 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004087 }
4088
David Sterba8eb93452015-10-12 16:55:54 +02004089 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4090 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004091 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004092 }
4093
Ilya Dryomovde322262012-01-16 22:04:49 +02004094do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004095 /*
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004096 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4097 * bctl is freed in reset_balance_state, or, if restriper was paused
4098 * all the way until unmount, in free_fs_info. The flag should be
4099 * cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004100 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004101 need_unlock = false;
4102
David Sterba6fcf6e22018-05-07 17:44:03 +02004103 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004104 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004105
Filipe Mananad00c2d92019-01-08 11:42:01 +00004106 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004107 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4108 ret = -EFAULT;
4109 }
4110
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004111out_bctl:
4112 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004113out_bargs:
4114 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004115out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004116 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004117 if (need_unlock)
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004118 btrfs_exclop_finish(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004119out:
Liu Boe54bfa312012-06-29 03:58:48 -06004120 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004121 return ret;
4122}
4123
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004124static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004125{
4126 if (!capable(CAP_SYS_ADMIN))
4127 return -EPERM;
4128
4129 switch (cmd) {
4130 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004131 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004132 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004133 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004134 }
4135
4136 return -EINVAL;
4137}
4138
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004139static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004140 void __user *arg)
4141{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004142 struct btrfs_ioctl_balance_args *bargs;
4143 int ret = 0;
4144
4145 if (!capable(CAP_SYS_ADMIN))
4146 return -EPERM;
4147
4148 mutex_lock(&fs_info->balance_mutex);
4149 if (!fs_info->balance_ctl) {
4150 ret = -ENOTCONN;
4151 goto out;
4152 }
4153
David Sterba8d2db782015-11-04 15:38:29 +01004154 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004155 if (!bargs) {
4156 ret = -ENOMEM;
4157 goto out;
4158 }
4159
David Sterba008ef092018-03-21 02:05:27 +01004160 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004161
4162 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4163 ret = -EFAULT;
4164
4165 kfree(bargs);
4166out:
4167 mutex_unlock(&fs_info->balance_mutex);
4168 return ret;
4169}
4170
Miao Xie905b0dd2012-11-26 08:50:11 +00004171static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004172{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004173 struct inode *inode = file_inode(file);
4174 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004175 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004176 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004177
4178 if (!capable(CAP_SYS_ADMIN))
4179 return -EPERM;
4180
Miao Xie905b0dd2012-11-26 08:50:11 +00004181 ret = mnt_want_write_file(file);
4182 if (ret)
4183 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004184
4185 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004186 if (IS_ERR(sa)) {
4187 ret = PTR_ERR(sa);
4188 goto drop_write;
4189 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004190
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004191 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004192
4193 switch (sa->cmd) {
4194 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004195 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004196 break;
4197 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004198 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004199 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004200 default:
4201 ret = -EINVAL;
4202 break;
4203 }
4204
Arne Jansen5d13a372011-09-14 15:53:51 +02004205 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004206 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004207drop_write:
4208 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004209 return ret;
4210}
4211
Miao Xie905b0dd2012-11-26 08:50:11 +00004212static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004214 struct inode *inode = file_inode(file);
4215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4216 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004217 struct btrfs_ioctl_qgroup_assign_args *sa;
4218 struct btrfs_trans_handle *trans;
4219 int ret;
4220 int err;
4221
4222 if (!capable(CAP_SYS_ADMIN))
4223 return -EPERM;
4224
Miao Xie905b0dd2012-11-26 08:50:11 +00004225 ret = mnt_want_write_file(file);
4226 if (ret)
4227 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004228
4229 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004230 if (IS_ERR(sa)) {
4231 ret = PTR_ERR(sa);
4232 goto drop_write;
4233 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004234
4235 trans = btrfs_join_transaction(root);
4236 if (IS_ERR(trans)) {
4237 ret = PTR_ERR(trans);
4238 goto out;
4239 }
4240
Arne Jansen5d13a372011-09-14 15:53:51 +02004241 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004242 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004243 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004244 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004245 }
4246
Qu Wenruoe082f562015-02-27 16:24:28 +08004247 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004248 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004249 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004250 btrfs_handle_fs_error(fs_info, err,
4251 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004252 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004253 if (err && !ret)
4254 ret = err;
4255
4256out:
4257 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004258drop_write:
4259 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004260 return ret;
4261}
4262
Miao Xie905b0dd2012-11-26 08:50:11 +00004263static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004264{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004265 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004266 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004267 struct btrfs_ioctl_qgroup_create_args *sa;
4268 struct btrfs_trans_handle *trans;
4269 int ret;
4270 int err;
4271
4272 if (!capable(CAP_SYS_ADMIN))
4273 return -EPERM;
4274
Miao Xie905b0dd2012-11-26 08:50:11 +00004275 ret = mnt_want_write_file(file);
4276 if (ret)
4277 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004278
4279 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004280 if (IS_ERR(sa)) {
4281 ret = PTR_ERR(sa);
4282 goto drop_write;
4283 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004284
Miao Xied86e56c2012-11-15 11:35:41 +00004285 if (!sa->qgroupid) {
4286 ret = -EINVAL;
4287 goto out;
4288 }
4289
Arne Jansen5d13a372011-09-14 15:53:51 +02004290 trans = btrfs_join_transaction(root);
4291 if (IS_ERR(trans)) {
4292 ret = PTR_ERR(trans);
4293 goto out;
4294 }
4295
Arne Jansen5d13a372011-09-14 15:53:51 +02004296 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004297 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004298 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004299 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004300 }
4301
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004302 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004303 if (err && !ret)
4304 ret = err;
4305
4306out:
4307 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004308drop_write:
4309 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004310 return ret;
4311}
4312
Miao Xie905b0dd2012-11-26 08:50:11 +00004313static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004315 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004316 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004317 struct btrfs_ioctl_qgroup_limit_args *sa;
4318 struct btrfs_trans_handle *trans;
4319 int ret;
4320 int err;
4321 u64 qgroupid;
4322
4323 if (!capable(CAP_SYS_ADMIN))
4324 return -EPERM;
4325
Miao Xie905b0dd2012-11-26 08:50:11 +00004326 ret = mnt_want_write_file(file);
4327 if (ret)
4328 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004329
4330 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004331 if (IS_ERR(sa)) {
4332 ret = PTR_ERR(sa);
4333 goto drop_write;
4334 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004335
4336 trans = btrfs_join_transaction(root);
4337 if (IS_ERR(trans)) {
4338 ret = PTR_ERR(trans);
4339 goto out;
4340 }
4341
4342 qgroupid = sa->qgroupid;
4343 if (!qgroupid) {
4344 /* take the current subvol as qgroup */
4345 qgroupid = root->root_key.objectid;
4346 }
4347
Lu Fengqif0042d52018-07-18 14:45:35 +08004348 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004349
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004350 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004351 if (err && !ret)
4352 ret = err;
4353
4354out:
4355 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004356drop_write:
4357 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004358 return ret;
4359}
4360
Jan Schmidt2f232032013-04-25 16:04:51 +00004361static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4362{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004363 struct inode *inode = file_inode(file);
4364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004365 struct btrfs_ioctl_quota_rescan_args *qsa;
4366 int ret;
4367
4368 if (!capable(CAP_SYS_ADMIN))
4369 return -EPERM;
4370
4371 ret = mnt_want_write_file(file);
4372 if (ret)
4373 return ret;
4374
4375 qsa = memdup_user(arg, sizeof(*qsa));
4376 if (IS_ERR(qsa)) {
4377 ret = PTR_ERR(qsa);
4378 goto drop_write;
4379 }
4380
4381 if (qsa->flags) {
4382 ret = -EINVAL;
4383 goto out;
4384 }
4385
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004386 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004387
4388out:
4389 kfree(qsa);
4390drop_write:
4391 mnt_drop_write_file(file);
4392 return ret;
4393}
4394
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004395static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4396 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004397{
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004398 struct btrfs_ioctl_quota_rescan_args qsa = {0};
Jan Schmidt2f232032013-04-25 16:04:51 +00004399
4400 if (!capable(CAP_SYS_ADMIN))
4401 return -EPERM;
4402
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004403 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004404 qsa.flags = 1;
4405 qsa.progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004406 }
4407
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004408 if (copy_to_user(arg, &qsa, sizeof(qsa)))
Anand Jain991a3da2021-07-28 14:20:41 +08004409 return -EFAULT;
Jan Schmidt2f232032013-04-25 16:04:51 +00004410
Anand Jain991a3da2021-07-28 14:20:41 +08004411 return 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00004412}
4413
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004414static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4415 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004416{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004417 if (!capable(CAP_SYS_ADMIN))
4418 return -EPERM;
4419
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004421}
4422
Hugo Millsabccd002014-01-30 20:17:00 +00004423static long _btrfs_ioctl_set_received_subvol(struct file *file,
Christian Braunere4fed172021-07-27 12:48:55 +02004424 struct user_namespace *mnt_userns,
Hugo Millsabccd002014-01-30 20:17:00 +00004425 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004426{
Al Viro496ad9a2013-01-23 17:07:38 -05004427 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004429 struct btrfs_root *root = BTRFS_I(inode)->root;
4430 struct btrfs_root_item *root_item = &root->root_item;
4431 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004432 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004433 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004434 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004435
Christian Braunere4fed172021-07-27 12:48:55 +02004436 if (!inode_owner_or_capable(mnt_userns, inode))
David Sterbabd60ea02014-01-16 15:50:22 +01004437 return -EPERM;
4438
Alexander Block8ea05e32012-07-25 17:35:53 +02004439 ret = mnt_want_write_file(file);
4440 if (ret < 0)
4441 return ret;
4442
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004444
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004445 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004446 ret = -EINVAL;
4447 goto out;
4448 }
4449
4450 if (btrfs_root_readonly(root)) {
4451 ret = -EROFS;
4452 goto out;
4453 }
4454
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004455 /*
4456 * 1 - root item
4457 * 2 - uuid items (received uuid + subvol uuid)
4458 */
4459 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004460 if (IS_ERR(trans)) {
4461 ret = PTR_ERR(trans);
4462 trans = NULL;
4463 goto out;
4464 }
4465
4466 sa->rtransid = trans->transid;
4467 sa->rtime.sec = ct.tv_sec;
4468 sa->rtime.nsec = ct.tv_nsec;
4469
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004470 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4471 BTRFS_UUID_SIZE);
4472 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004473 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004474 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004475 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4476 root->root_key.objectid);
4477 if (ret && ret != -ENOENT) {
4478 btrfs_abort_transaction(trans, ret);
4479 btrfs_end_transaction(trans);
4480 goto out;
4481 }
4482 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004483 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4484 btrfs_set_root_stransid(root_item, sa->stransid);
4485 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004486 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4487 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4488 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4489 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004490
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004491 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004492 &root->root_key, &root->root_item);
4493 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004494 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004495 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004496 }
4497 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004498 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004499 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4500 root->root_key.objectid);
4501 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004502 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004503 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004504 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004505 }
4506 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004507 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004508out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004510 mnt_drop_write_file(file);
4511 return ret;
4512}
4513
4514#ifdef CONFIG_64BIT
4515static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4516 void __user *arg)
4517{
4518 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4519 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4520 int ret = 0;
4521
4522 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304523 if (IS_ERR(args32))
4524 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004525
David Sterba8d2db782015-11-04 15:38:29 +01004526 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004527 if (!args64) {
4528 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004529 goto out;
4530 }
4531
4532 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4533 args64->stransid = args32->stransid;
4534 args64->rtransid = args32->rtransid;
4535 args64->stime.sec = args32->stime.sec;
4536 args64->stime.nsec = args32->stime.nsec;
4537 args64->rtime.sec = args32->rtime.sec;
4538 args64->rtime.nsec = args32->rtime.nsec;
4539 args64->flags = args32->flags;
4540
Christian Braunere4fed172021-07-27 12:48:55 +02004541 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64);
Hugo Millsabccd002014-01-30 20:17:00 +00004542 if (ret)
4543 goto out;
4544
4545 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4546 args32->stransid = args64->stransid;
4547 args32->rtransid = args64->rtransid;
4548 args32->stime.sec = args64->stime.sec;
4549 args32->stime.nsec = args64->stime.nsec;
4550 args32->rtime.sec = args64->rtime.sec;
4551 args32->rtime.nsec = args64->rtime.nsec;
4552 args32->flags = args64->flags;
4553
4554 ret = copy_to_user(arg, args32, sizeof(*args32));
4555 if (ret)
4556 ret = -EFAULT;
4557
4558out:
4559 kfree(args32);
4560 kfree(args64);
4561 return ret;
4562}
4563#endif
4564
4565static long btrfs_ioctl_set_received_subvol(struct file *file,
4566 void __user *arg)
4567{
4568 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4569 int ret = 0;
4570
4571 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304572 if (IS_ERR(sa))
4573 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004574
Christian Braunere4fed172021-07-27 12:48:55 +02004575 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004576
4577 if (ret)
4578 goto out;
4579
Alexander Block8ea05e32012-07-25 17:35:53 +02004580 ret = copy_to_user(arg, sa, sizeof(*sa));
4581 if (ret)
4582 ret = -EFAULT;
4583
4584out:
4585 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004586 return ret;
4587}
4588
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004589static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4590 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00004591{
Anand Jaina1b83ac2013-07-19 17:39:32 +08004592 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004593 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004594 char label[BTRFS_LABEL_SIZE];
4595
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004596 spin_lock(&fs_info->super_lock);
4597 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4598 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004599
4600 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004601
4602 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004603 btrfs_warn(fs_info,
4604 "label is too long, return the first %zu bytes",
4605 --len);
jeff.liu867ab662013-01-05 02:48:01 +00004606 }
4607
jeff.liu867ab662013-01-05 02:48:01 +00004608 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004609
4610 return ret ? -EFAULT : 0;
4611}
4612
jeff.liua8bfd4a2013-01-05 02:48:08 +00004613static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4614{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004615 struct inode *inode = file_inode(file);
4616 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4617 struct btrfs_root *root = BTRFS_I(inode)->root;
4618 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004619 struct btrfs_trans_handle *trans;
4620 char label[BTRFS_LABEL_SIZE];
4621 int ret;
4622
4623 if (!capable(CAP_SYS_ADMIN))
4624 return -EPERM;
4625
4626 if (copy_from_user(label, arg, sizeof(label)))
4627 return -EFAULT;
4628
4629 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004630 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004631 "unable to set label with more than %d bytes",
4632 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004633 return -EINVAL;
4634 }
4635
4636 ret = mnt_want_write_file(file);
4637 if (ret)
4638 return ret;
4639
jeff.liua8bfd4a2013-01-05 02:48:08 +00004640 trans = btrfs_start_transaction(root, 0);
4641 if (IS_ERR(trans)) {
4642 ret = PTR_ERR(trans);
4643 goto out_unlock;
4644 }
4645
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004646 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004647 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004649 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004650
4651out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004652 mnt_drop_write_file(file);
4653 return ret;
4654}
4655
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004656#define INIT_FEATURE_FLAGS(suffix) \
4657 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4658 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4659 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4660
David Sterbad5131b62016-02-17 15:26:27 +01004661int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004662{
David Sterba4d4ab6d2015-11-19 11:42:31 +01004663 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004664 INIT_FEATURE_FLAGS(SUPP),
4665 INIT_FEATURE_FLAGS(SAFE_SET),
4666 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4667 };
4668
4669 if (copy_to_user(arg, &features, sizeof(features)))
4670 return -EFAULT;
4671
4672 return 0;
4673}
4674
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004675static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4676 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004677{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004678 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004679 struct btrfs_ioctl_feature_flags features;
4680
4681 features.compat_flags = btrfs_super_compat_flags(super_block);
4682 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4683 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4684
4685 if (copy_to_user(arg, &features, sizeof(features)))
4686 return -EFAULT;
4687
4688 return 0;
4689}
4690
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004691static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004692 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004693 u64 change_mask, u64 flags, u64 supported_flags,
4694 u64 safe_set, u64 safe_clear)
4695{
David Sterbaf10152b2019-08-01 19:07:55 +02004696 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004697 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004698 u64 disallowed, unsupported;
4699 u64 set_mask = flags & change_mask;
4700 u64 clear_mask = ~flags & change_mask;
4701
4702 unsupported = set_mask & ~supported_flags;
4703 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004704 names = btrfs_printable_features(set, unsupported);
4705 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004706 btrfs_warn(fs_info,
4707 "this kernel does not support the %s feature bit%s",
4708 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004709 kfree(names);
4710 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004711 btrfs_warn(fs_info,
4712 "this kernel does not support %s bits 0x%llx",
4713 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004714 return -EOPNOTSUPP;
4715 }
4716
4717 disallowed = set_mask & ~safe_set;
4718 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004719 names = btrfs_printable_features(set, disallowed);
4720 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004721 btrfs_warn(fs_info,
4722 "can't set the %s feature bit%s while mounted",
4723 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004724 kfree(names);
4725 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004726 btrfs_warn(fs_info,
4727 "can't set %s bits 0x%llx while mounted",
4728 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004729 return -EPERM;
4730 }
4731
4732 disallowed = clear_mask & ~safe_clear;
4733 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004734 names = btrfs_printable_features(set, disallowed);
4735 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004736 btrfs_warn(fs_info,
4737 "can't clear the %s feature bit%s while mounted",
4738 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004739 kfree(names);
4740 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004741 btrfs_warn(fs_info,
4742 "can't clear %s bits 0x%llx while mounted",
4743 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004744 return -EPERM;
4745 }
4746
4747 return 0;
4748}
4749
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004750#define check_feature(fs_info, change_mask, flags, mask_base) \
4751check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004752 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4753 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4754 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4755
4756static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4757{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004758 struct inode *inode = file_inode(file);
4759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4760 struct btrfs_root *root = BTRFS_I(inode)->root;
4761 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004762 struct btrfs_ioctl_feature_flags flags[2];
4763 struct btrfs_trans_handle *trans;
4764 u64 newflags;
4765 int ret;
4766
4767 if (!capable(CAP_SYS_ADMIN))
4768 return -EPERM;
4769
4770 if (copy_from_user(flags, arg, sizeof(flags)))
4771 return -EFAULT;
4772
4773 /* Nothing to do */
4774 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4775 !flags[0].incompat_flags)
4776 return 0;
4777
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004778 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004779 flags[1].compat_flags, COMPAT);
4780 if (ret)
4781 return ret;
4782
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004783 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004784 flags[1].compat_ro_flags, COMPAT_RO);
4785 if (ret)
4786 return ret;
4787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004788 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004789 flags[1].incompat_flags, INCOMPAT);
4790 if (ret)
4791 return ret;
4792
David Sterba7ab19622016-05-04 11:32:00 +02004793 ret = mnt_want_write_file(file);
4794 if (ret)
4795 return ret;
4796
David Sterba8051aa12014-02-07 14:34:04 +01004797 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02004798 if (IS_ERR(trans)) {
4799 ret = PTR_ERR(trans);
4800 goto out_drop_write;
4801 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004802
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004803 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004804 newflags = btrfs_super_compat_flags(super_block);
4805 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4806 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4807 btrfs_set_super_compat_flags(super_block, newflags);
4808
4809 newflags = btrfs_super_compat_ro_flags(super_block);
4810 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4811 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4812 btrfs_set_super_compat_ro_flags(super_block, newflags);
4813
4814 newflags = btrfs_super_incompat_flags(super_block);
4815 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4816 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4817 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004819
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004820 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02004821out_drop_write:
4822 mnt_drop_write_file(file);
4823
4824 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004825}
4826
Josef Bacik2351f432017-09-27 10:43:13 -04004827static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4828{
4829 struct btrfs_ioctl_send_args *arg;
4830 int ret;
4831
4832 if (compat) {
4833#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4834 struct btrfs_ioctl_send_args_32 args32;
4835
4836 ret = copy_from_user(&args32, argp, sizeof(args32));
4837 if (ret)
4838 return -EFAULT;
4839 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4840 if (!arg)
4841 return -ENOMEM;
4842 arg->send_fd = args32.send_fd;
4843 arg->clone_sources_count = args32.clone_sources_count;
4844 arg->clone_sources = compat_ptr(args32.clone_sources);
4845 arg->parent_root = args32.parent_root;
4846 arg->flags = args32.flags;
4847 memcpy(arg->reserved, args32.reserved,
4848 sizeof(args32.reserved));
4849#else
4850 return -ENOTTY;
4851#endif
4852 } else {
4853 arg = memdup_user(argp, sizeof(*arg));
4854 if (IS_ERR(arg))
4855 return PTR_ERR(arg);
4856 }
4857 ret = btrfs_ioctl_send(file, arg);
4858 kfree(arg);
4859 return ret;
4860}
4861
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004862long btrfs_ioctl(struct file *file, unsigned int
4863 cmd, unsigned long arg)
4864{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 struct inode *inode = file_inode(file);
4866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4867 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004868 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004869
4870 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004871 case FS_IOC_GETVERSION:
4872 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004873 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004874 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004875 case FS_IOC_SETFSLABEL:
4876 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004877 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004878 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004879 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004880 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004881 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004882 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004883 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004884 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004885 case BTRFS_IOC_SUBVOL_CREATE_V2:
4886 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004887 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03004888 return btrfs_ioctl_snap_destroy(file, argp, false);
4889 case BTRFS_IOC_SNAP_DESTROY_V2:
4890 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08004891 case BTRFS_IOC_SUBVOL_GETFLAGS:
4892 return btrfs_ioctl_subvol_getflags(file, argp);
4893 case BTRFS_IOC_SUBVOL_SETFLAGS:
4894 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004895 case BTRFS_IOC_DEFAULT_SUBVOL:
4896 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004897 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004898 return btrfs_ioctl_defrag(file, NULL);
4899 case BTRFS_IOC_DEFRAG_RANGE:
4900 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004901 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004902 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004903 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004904 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004905 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004906 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08004907 case BTRFS_IOC_RM_DEV_V2:
4908 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004909 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004910 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004911 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004912 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004913 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004914 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05004915 case BTRFS_IOC_TREE_SEARCH:
4916 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01004917 case BTRFS_IOC_TREE_SEARCH_V2:
4918 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05004919 case BTRFS_IOC_INO_LOOKUP:
4920 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004921 case BTRFS_IOC_INO_PATHS:
4922 return btrfs_ioctl_ino_to_path(root, argp);
4923 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004924 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4925 case BTRFS_IOC_LOGICAL_INO_V2:
4926 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00004927 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004928 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004929 case BTRFS_IOC_SYNC: {
4930 int ret;
4931
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02004932 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004933 if (ret)
4934 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02004936 /*
4937 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004938 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02004939 * processing uncleaned subvols.
4940 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004942 return ret;
4943 }
Sage Weil46204592010-10-29 15:41:32 -04004944 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004945 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004946 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004947 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004948 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004949 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004950 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004951 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004952 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004953 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004954 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004955 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004956 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004957 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004958 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004959 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004960 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4961 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00004962#ifdef CONFIG_64BIT
4963 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4964 return btrfs_ioctl_set_received_subvol_32(file, argp);
4965#endif
Alexander Block31db9f72012-07-25 23:19:24 +02004966 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04004967 return _btrfs_ioctl_send(file, argp, false);
4968#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4969 case BTRFS_IOC_SEND_32:
4970 return _btrfs_ioctl_send(file, argp, true);
4971#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004972 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004973 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004974 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004975 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004976 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004977 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004978 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004979 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004980 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004981 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004982 case BTRFS_IOC_QUOTA_RESCAN:
4983 return btrfs_ioctl_quota_rescan(file, argp);
4984 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004985 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004986 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004987 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004988 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004989 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004990 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01004991 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004992 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004993 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004994 case BTRFS_IOC_SET_FEATURES:
4995 return btrfs_ioctl_set_features(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09004996 case BTRFS_IOC_GET_SUBVOL_INFO:
4997 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09004998 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
4999 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005000 case BTRFS_IOC_INO_LOOKUP_USER:
5001 return btrfs_ioctl_ino_lookup_user(file, argp);
Boris Burkov14605402021-06-30 13:01:49 -07005002 case FS_IOC_ENABLE_VERITY:
5003 return fsverity_ioctl_enable(file, (const void __user *)argp);
5004 case FS_IOC_MEASURE_VERITY:
5005 return fsverity_ioctl_measure(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005006 }
5007
5008 return -ENOTTY;
5009}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005010
5011#ifdef CONFIG_COMPAT
5012long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5013{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005014 /*
5015 * These all access 32-bit values anyway so no further
5016 * handling is necessary.
5017 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005018 switch (cmd) {
Luke Dashjr4c63c242015-10-29 08:22:21 +00005019 case FS_IOC32_GETVERSION:
5020 cmd = FS_IOC_GETVERSION;
5021 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005022 }
5023
5024 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5025}
5026#endif