blob: 33eda39df685f7607e57a886a7a53b59f1fe6347 [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 */
David Sterbae77fbf92021-10-22 16:53:36 +020085 __u32 version; /* in */
86 __u8 reserved[28]; /* in */
Josef Bacik2351f432017-09-27 10:43:13 -040087} __attribute__ ((__packed__));
88
89#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
90 struct btrfs_ioctl_send_args_32)
91#endif
Hugo Millsabccd002014-01-30 20:17:00 +000092
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020094static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
95 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020096{
David Sterba1905a0f2018-03-26 18:52:15 +020097 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020098 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020099 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100 return flags & ~FS_DIRSYNC_FL;
101 else
102 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
103}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400104
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200105/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200106 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
107 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200108 */
Boris Burkov77eea052021-06-30 13:01:48 -0700109static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200110{
111 unsigned int iflags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -0700112 u32 flags = binode->flags;
Boris Burkov14605402021-06-30 13:01:49 -0700113 u32 ro_flags = binode->ro_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200114
115 if (flags & BTRFS_INODE_SYNC)
116 iflags |= FS_SYNC_FL;
117 if (flags & BTRFS_INODE_IMMUTABLE)
118 iflags |= FS_IMMUTABLE_FL;
119 if (flags & BTRFS_INODE_APPEND)
120 iflags |= FS_APPEND_FL;
121 if (flags & BTRFS_INODE_NODUMP)
122 iflags |= FS_NODUMP_FL;
123 if (flags & BTRFS_INODE_NOATIME)
124 iflags |= FS_NOATIME_FL;
125 if (flags & BTRFS_INODE_DIRSYNC)
126 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000127 if (flags & BTRFS_INODE_NODATACOW)
128 iflags |= FS_NOCOW_FL;
Boris Burkov14605402021-06-30 13:01:49 -0700129 if (ro_flags & BTRFS_INODE_RO_VERITY)
130 iflags |= FS_VERITY_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000131
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900132 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000133 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900134 else if (flags & BTRFS_INODE_COMPRESS)
135 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200136
137 return iflags;
138}
139
140/*
141 * Update inode->i_flags based on the btrfs internal flags.
142 */
David Sterba7b6a2212018-03-26 18:40:21 +0200143void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200144{
David Sterba5c57b8b2018-04-23 15:45:18 +0200145 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100146 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147
David Sterba5c57b8b2018-04-23 15:45:18 +0200148 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100149 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200150 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100151 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200152 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100153 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200154 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100155 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200156 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100157 new_fl |= S_DIRSYNC;
Boris Burkov14605402021-06-30 13:01:49 -0700158 if (binode->ro_flags & BTRFS_INODE_RO_VERITY)
159 new_fl |= S_VERITY;
Filipe Manana3cc79392014-06-25 22:36:02 +0100160
161 set_mask_bits(&inode->i_flags,
Boris Burkov14605402021-06-30 13:01:49 -0700162 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
163 S_VERITY, new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200164}
165
David Sterbaf37c5632020-07-10 09:49:56 +0200166/*
167 * Check if @flags are a supported and valid set of FS_*_FL flags and that
168 * the old and new flags are not conflicting
169 */
170static int check_fsflags(unsigned int old_flags, unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000171{
172 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
173 FS_NOATIME_FL | FS_NODUMP_FL | \
174 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000175 FS_NOCOMP_FL | FS_COMPR_FL |
176 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000177 return -EOPNOTSUPP;
178
David Sterbaf37c5632020-07-10 09:49:56 +0200179 /* COMPR and NOCOMP on new/old are valid */
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
181 return -EINVAL;
182
David Sterbaf37c5632020-07-10 09:49:56 +0200183 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
184 return -EINVAL;
185
186 /* NOCOW and compression options are mutually exclusive */
187 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
188 return -EINVAL;
189 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
190 return -EINVAL;
191
Liu Bo75e7cb72011-03-22 10:12:20 +0000192 return 0;
193}
194
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900195static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
196 unsigned int flags)
197{
198 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
199 return -EPERM;
200
201 return 0;
202}
203
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200204/*
205 * Set flags/xflags from the internal inode flags. The remaining items of
206 * fsxattr are zeroed.
207 */
208int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200209{
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200210 struct btrfs_inode *binode = BTRFS_I(d_inode(dentry));
211
Boris Burkov77eea052021-06-30 13:01:48 -0700212 fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode));
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200213 return 0;
214}
215
216int btrfs_fileattr_set(struct user_namespace *mnt_userns,
217 struct dentry *dentry, struct fileattr *fa)
218{
219 struct inode *inode = d_inode(dentry);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200221 struct btrfs_inode *binode = BTRFS_I(inode);
222 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200223 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700224 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800226 const char *comp = NULL;
David Sterbaf37c5632020-07-10 09:49:56 +0200227 u32 binode_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200228
Li Zefanb83cc962010-12-20 16:04:08 +0800229 if (btrfs_root_readonly(root))
230 return -EROFS;
231
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200232 if (fileattr_has_fsx(fa))
233 return -EOPNOTSUPP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200234
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200235 fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags);
Boris Burkov77eea052021-06-30 13:01:48 -0700236 old_fsflags = btrfs_inode_flags_to_fsflags(binode);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200237 ret = check_fsflags(old_fsflags, fsflags);
Jan Karae7848682012-06-12 16:20:32 +0200238 if (ret)
239 return ret;
240
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900241 ret = check_fsflags_compatible(fs_info, fsflags);
242 if (ret)
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200243 return ret;
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900244
David Sterbaf37c5632020-07-10 09:49:56 +0200245 binode_flags = binode->flags;
David Sterba5c57b8b2018-04-23 15:45:18 +0200246 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800247 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200248 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800249 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200250 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800251 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200252 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800253 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200254 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800255 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200256 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800257 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200258 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800259 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200260 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800261 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200262 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800263 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200264 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800265 binode_flags &= ~BTRFS_INODE_NOATIME;
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200266
267 /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
268 if (!fa->flags_valid) {
269 /* 1 item for the inode */
270 trans = btrfs_start_transaction(root, 1);
Ritesh Harjani9b8a2332021-04-30 21:30:55 +0530271 if (IS_ERR(trans))
272 return PTR_ERR(trans);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200273 goto update_flags;
274 }
275
David Sterba5c57b8b2018-04-23 15:45:18 +0200276 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800277 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800279 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200280 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800281 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600282 /*
283 * It's safe to turn csums off here, no extents exist.
284 * Otherwise we want the flag to reflect the real COW
285 * status of the file and will not set it.
286 */
287 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800288 binode_flags |= BTRFS_INODE_NODATACOW |
289 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600290 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800291 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600292 }
293 } else {
294 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400295 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600296 */
Anand Jain44e51942019-04-20 19:48:57 +0800297 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600298 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800299 binode_flags &= ~(BTRFS_INODE_NODATACOW |
300 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600301 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800302 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600303 }
304 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200305
Liu Bo75e7cb72011-03-22 10:12:20 +0000306 /*
307 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
308 * flag may be changed automatically if compression code won't make
309 * things smaller.
310 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200311 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800312 binode_flags &= ~BTRFS_INODE_COMPRESS;
313 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200314 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000315
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200316 if (IS_SWAPFILE(inode))
317 return -ETXTBSY;
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700318
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800319 binode_flags |= BTRFS_INODE_COMPRESS;
320 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000321
David Sterba93370502017-10-31 17:32:41 +0100322 comp = btrfs_compress_type2str(fs_info->compress_type);
323 if (!comp || comp[0] == 0)
324 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000325 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800326 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000327 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200328
Anand Jainff9fef552019-04-20 19:48:53 +0800329 /*
330 * 1 for inode item
331 * 2 for properties
332 */
333 trans = btrfs_start_transaction(root, 3);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200334 if (IS_ERR(trans))
335 return PTR_ERR(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200336
Anand Jainff9fef552019-04-20 19:48:53 +0800337 if (comp) {
338 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
339 strlen(comp), 0);
340 if (ret) {
341 btrfs_abort_transaction(trans, ret);
342 goto out_end_trans;
343 }
Anand Jainff9fef552019-04-20 19:48:53 +0800344 } else {
345 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
346 0, 0);
347 if (ret && ret != -ENODATA) {
348 btrfs_abort_transaction(trans, ret);
349 goto out_end_trans;
350 }
351 }
352
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200353update_flags:
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800354 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200355 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400356 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700357 inode->i_ctime = current_time(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200358 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200359
Anand Jainff9fef552019-04-20 19:48:53 +0800360 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000362 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200363}
364
David Sterba0d7ed322021-05-14 17:42:30 +0200365/*
366 * Start exclusive operation @type, return true on success
367 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500368bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
369 enum btrfs_exclusive_operation type)
370{
David Sterba0d7ed322021-05-14 17:42:30 +0200371 bool ret = false;
372
373 spin_lock(&fs_info->super_lock);
374 if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
375 fs_info->exclusive_operation = type;
376 ret = true;
377 }
378 spin_unlock(&fs_info->super_lock);
379
380 return ret;
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500381}
382
David Sterba578bda92021-05-18 21:05:52 +0200383/*
384 * Conditionally allow to enter the exclusive operation in case it's compatible
385 * with the running one. This must be paired with btrfs_exclop_start_unlock and
386 * btrfs_exclop_finish.
387 *
388 * Compatibility:
389 * - the same type is already running
Nikolay Borisov621a1ee2021-11-25 11:14:42 +0200390 * - when trying to add a device and balance has been paused
David Sterba578bda92021-05-18 21:05:52 +0200391 * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller
392 * must check the condition first that would allow none -> @type
393 */
394bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
395 enum btrfs_exclusive_operation type)
396{
397 spin_lock(&fs_info->super_lock);
Nikolay Borisov621a1ee2021-11-25 11:14:42 +0200398 if (fs_info->exclusive_operation == type ||
399 (fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED &&
400 type == BTRFS_EXCLOP_DEV_ADD))
David Sterba578bda92021-05-18 21:05:52 +0200401 return true;
402
403 spin_unlock(&fs_info->super_lock);
404 return false;
405}
406
407void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info)
408{
409 spin_unlock(&fs_info->super_lock);
410}
411
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500412void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
413{
David Sterba0d7ed322021-05-14 17:42:30 +0200414 spin_lock(&fs_info->super_lock);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500415 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
David Sterba0d7ed322021-05-14 17:42:30 +0200416 spin_unlock(&fs_info->super_lock);
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500417 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500418}
419
Nikolay Borisovefc0e692021-11-25 11:14:41 +0200420void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
421 enum btrfs_exclusive_operation op)
422{
423 switch (op) {
424 case BTRFS_EXCLOP_BALANCE_PAUSED:
425 spin_lock(&fs_info->super_lock);
426 ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE ||
427 fs_info->exclusive_operation == BTRFS_EXCLOP_DEV_ADD);
428 fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE_PAUSED;
429 spin_unlock(&fs_info->super_lock);
430 break;
431 case BTRFS_EXCLOP_BALANCE:
432 spin_lock(&fs_info->super_lock);
433 ASSERT(fs_info->exclusive_operation == BTRFS_EXCLOP_BALANCE_PAUSED);
434 fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE;
435 spin_unlock(&fs_info->super_lock);
436 break;
437 default:
438 btrfs_warn(fs_info,
439 "invalid exclop balance operation %d requested", op);
440 }
441}
442
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200443static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
444{
Al Viro496ad9a2013-01-23 17:07:38 -0500445 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200446
447 return put_user(inode->i_generation, arg);
448}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300450static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
451 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000452{
Li Dongyangf7039b12011-03-24 10:24:28 +0000453 struct btrfs_device *device;
454 struct request_queue *q;
455 struct fstrim_range range;
456 u64 minlen = ULLONG_MAX;
457 u64 num_devices = 0;
458 int ret;
459
460 if (!capable(CAP_SYS_ADMIN))
461 return -EPERM;
462
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000463 /*
Naohiro Aota1cb3dc32021-02-04 19:21:46 +0900464 * btrfs_trim_block_group() depends on space cache, which is not
465 * available in zoned filesystem. So, disallow fitrim on a zoned
466 * filesystem for now.
467 */
468 if (btrfs_is_zoned(fs_info))
469 return -EOPNOTSUPP;
470
471 /*
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000472 * If the fs is mounted with nologreplay, which requires it to be
473 * mounted in RO mode as well, we can not allow discard on free space
474 * inside block groups, because log trees refer to extents that are not
475 * pinned in a block group's free space cache (pinning the extents is
476 * precisely the first phase of replaying a log tree).
477 */
478 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
479 return -EROFS;
480
Xiao Guangrong1f781602011-04-20 10:09:16 +0000481 rcu_read_lock();
482 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
483 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000484 if (!device->bdev)
485 continue;
486 q = bdev_get_queue(device->bdev);
487 if (blk_queue_discard(q)) {
488 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100489 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000490 minlen);
491 }
492 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000493 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200494
Li Dongyangf7039b12011-03-24 10:24:28 +0000495 if (!num_devices)
496 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000497 if (copy_from_user(&range, arg, sizeof(range)))
498 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800499
500 /*
501 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
502 * block group is in the logical address space, which can be any
503 * sectorsize aligned bytenr in the range [0, U64_MAX].
504 */
505 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200506 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000507
508 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400509 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000510 if (ret < 0)
511 return ret;
512
513 if (copy_to_user(arg, &range, sizeof(range)))
514 return -EFAULT;
515
516 return 0;
517}
518
David Sterbae1f60a62019-10-01 19:57:39 +0200519int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200520{
Chris Mason46e0f662013-11-15 12:14:55 +0100521 int i;
522
523 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
524 if (uuid[i])
525 return 0;
526 }
527 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200528}
529
Christian Brauner4d4340c2021-07-27 12:48:52 +0200530static noinline int create_subvol(struct user_namespace *mnt_userns,
531 struct inode *dir, struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100532 const char *name, int namelen,
Miao Xie8696c532013-02-07 06:02:44 +0000533 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400534{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400535 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536 struct btrfs_trans_handle *trans;
537 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100538 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539 struct btrfs_inode_item *inode_item;
540 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000541 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400542 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000543 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700544 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900545 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546 int ret;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800547 dev_t anon_dev = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400548 u64 objectid;
Chris Mason3de45862008-11-17 21:02:50 -0500549 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550
David Sterba49a3c4d2016-03-24 17:49:22 +0100551 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
552 if (!root_item)
553 return -ENOMEM;
554
Nikolay Borisov543068a2020-12-07 17:32:33 +0200555 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400556 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100557 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000558
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800559 ret = get_anon_bdev(&anon_dev);
560 if (ret < 0)
561 goto fail_free;
562
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800563 /*
564 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400565 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800566 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100567 if (btrfs_qgroup_level(objectid)) {
568 ret = -ENOSPC;
569 goto fail_free;
570 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800571
Miao Xied5c12072013-02-28 10:04:33 +0000572 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400573 /*
Miao Xied5c12072013-02-28 10:04:33 +0000574 * The same as the snapshot creation, please see the comment
575 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400576 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800577 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000578 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100579 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580
Miao Xied5c12072013-02-28 10:04:33 +0000581 trans = btrfs_start_transaction(root, 0);
582 if (IS_ERR(trans)) {
583 ret = PTR_ERR(trans);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800584 btrfs_subvolume_release_metadata(root, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100585 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000586 }
587 trans->block_rsv = &block_rsv;
588 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400589
Lu Fengqia93774222018-07-18 14:45:41 +0800590 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200591 if (ret)
592 goto fail;
593
Josef Bacik9631e4c2020-08-20 11:46:03 -0400594 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
595 BTRFS_NESTING_NORMAL);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400596 if (IS_ERR(leaf)) {
597 ret = PTR_ERR(leaf);
598 goto fail;
599 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400600
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400601 btrfs_mark_buffer_dirty(leaf);
602
David Sterba49a3c4d2016-03-24 17:49:22 +0100603 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800604 btrfs_set_stack_inode_generation(inode_item, 1);
605 btrfs_set_stack_inode_size(inode_item, 3);
606 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400607 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400608 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800609 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400610
David Sterba49a3c4d2016-03-24 17:49:22 +0100611 btrfs_set_root_flags(root_item, 0);
612 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800613 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000614
David Sterba49a3c4d2016-03-24 17:49:22 +0100615 btrfs_set_root_bytenr(root_item, leaf->start);
616 btrfs_set_root_generation(root_item, trans->transid);
617 btrfs_set_root_level(root_item, 0);
618 btrfs_set_root_refs(root_item, 1);
619 btrfs_set_root_used(root_item, leaf->len);
620 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400621
David Sterba49a3c4d2016-03-24 17:49:22 +0100622 btrfs_set_root_generation_v2(root_item,
623 btrfs_root_generation(root_item));
Andy Shevchenko807fc792020-02-24 17:37:51 +0200624 generate_random_guid(root_item->uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100625 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
626 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
627 root_item->ctime = root_item->otime;
628 btrfs_set_root_ctransid(root_item, trans->transid);
629 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630
Chris Mason925baed2008-06-25 16:01:30 -0400631 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632
Nikolay Borisov69948022020-12-07 17:32:37 +0200633 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634
635 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400636 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200637 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400638 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100639 root_item);
Filipe Manana67addf22021-04-20 10:55:12 +0100640 if (ret) {
641 /*
642 * Since we don't abort the transaction in this case, free the
643 * tree block so that we don't leak space and leave the
644 * filesystem in an inconsistent state (an extent item in the
Filipe Manana7a1636082021-12-13 08:45:12 +0000645 * extent tree with a backreference for a root that does not
Filipe Manana212a58f2021-12-13 08:45:13 +0000646 * exists).
Filipe Manana67addf22021-04-20 10:55:12 +0100647 */
Filipe Manana212a58f2021-12-13 08:45:13 +0000648 btrfs_tree_lock(leaf);
649 btrfs_clean_tree_block(leaf);
650 btrfs_tree_unlock(leaf);
Filipe Manana7a1636082021-12-13 08:45:12 +0000651 btrfs_free_tree_block(trans, objectid, leaf, 0, 1);
Filipe Manana67addf22021-04-20 10:55:12 +0100652 free_extent_buffer(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653 goto fail;
Filipe Manana67addf22021-04-20 10:55:12 +0100654 }
655
656 free_extent_buffer(leaf);
657 leaf = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 key.offset = (u64)-1;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800660 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (IS_ERR(new_root)) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800662 free_anon_bdev(anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400664 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100665 goto fail;
666 }
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800667 /* Freeing will be done in btrfs_put_root() of new_root */
668 anon_dev = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -0400669
Josef Bacik221581e2021-03-12 15:25:06 -0500670 ret = btrfs_record_root_in_trans(trans, new_root);
671 if (ret) {
672 btrfs_put_root(new_root);
673 btrfs_abort_transaction(trans, ret);
674 goto fail;
675 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400676
Christian Brauner4d4340c2021-07-27 12:48:52 +0200677 ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns);
Josef Bacik00246522020-01-24 09:33:01 -0500678 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700679 if (ret) {
680 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400681 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700682 goto fail;
683 }
684
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400685 /*
686 * insert the directory item
687 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200688 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400690 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100691 goto fail;
692 }
Chris Mason3de45862008-11-17 21:02:50 -0500693
Lu Fengqi684572d2018-08-04 21:10:57 +0800694 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500695 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400697 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400698 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100699 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500700
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200701 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200702 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikc7e54b52019-12-06 09:37:15 -0500703 if (ret) {
704 btrfs_abort_transaction(trans, ret);
705 goto fail;
706 }
Yan Zheng52c26172009-01-05 15:43:43 -0500707
Lu Fengqi6025c192018-08-01 11:32:29 +0800708 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200709 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500710 if (ret) {
711 btrfs_abort_transaction(trans, ret);
712 goto fail;
713 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500714
Lu Fengqicdb345a2018-05-29 15:01:53 +0800715 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400716 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200717 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400718 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200719
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100721 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000722 trans->block_rsv = NULL;
723 trans->bytes_reserved = 0;
Qu Wenruoe85fde52020-07-24 14:46:10 +0800724 btrfs_subvolume_release_metadata(root, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800725
Filipe Manana1b58ae02021-12-13 08:45:14 +0000726 if (ret)
727 btrfs_end_transaction(trans);
728 else
729 ret = btrfs_commit_transaction(trans);
Chris Mason1a65e242013-02-06 12:06:02 -0500730
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900731 if (!ret) {
732 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800733 if (IS_ERR(inode))
734 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900735 d_instantiate(dentry, inode);
736 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400737 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100738
739fail_free:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800740 if (anon_dev)
741 free_anon_bdev(anon_dev);
David Sterba49a3c4d2016-03-24 17:49:22 +0100742 kfree(root_item);
743 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400744}
745
Miao Xiee9662f72013-02-28 10:01:15 +0000746static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200747 struct dentry *dentry, bool readonly,
Miao Xiee9662f72013-02-28 10:01:15 +0000748 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400750 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000751 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 struct btrfs_pending_snapshot *pending_snapshot;
753 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000754 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400755
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800756 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400757 return -EINVAL;
758
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700759 if (atomic_read(&root->nr_swapfiles)) {
760 btrfs_warn(fs_info,
761 "cannot snapshot subvolume with active swapfile");
762 return -ETXTBSY;
763 }
764
David Sterba23269bf2017-02-13 11:03:44 +0100765 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100766 if (!pending_snapshot)
767 return -ENOMEM;
768
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800769 ret = get_anon_bdev(&pending_snapshot->anon_dev);
770 if (ret < 0)
771 goto free_pending;
David Sterbab0c0ea62015-11-10 18:54:00 +0100772 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100773 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100774 pending_snapshot->path = btrfs_alloc_path();
775 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100776 ret = -ENOMEM;
777 goto free_pending;
778 }
779
Miao Xie66d8f3d2012-09-06 04:02:28 -0600780 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
781 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000782 /*
783 * 1 - parent dir inode
784 * 2 - dir entries
785 * 1 - root item
786 * 2 - root ref/backref
787 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200788 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000789 */
790 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200791 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400792 false);
Miao Xied5c12072013-02-28 10:04:33 +0000793 if (ret)
Robbie Koc11fbb62020-05-14 17:19:18 +0800794 goto free_pending;
Miao Xied5c12072013-02-28 10:04:33 +0000795
Yan, Zhenga22285a2010-05-16 10:48:46 -0400796 pending_snapshot->dentry = dentry;
797 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800798 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000799 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000800 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400801
Miao Xied5c12072013-02-28 10:04:33 +0000802 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400803 if (IS_ERR(trans)) {
804 ret = PTR_ERR(trans);
805 goto fail;
806 }
807
Filipe Manana28b21c52022-01-21 15:44:39 +0000808 trans->pending_snapshot = pending_snapshot;
Nikolay Borisov9babda92020-03-13 17:23:20 +0200809
810 ret = btrfs_commit_transaction(trans);
Miao Xieaec80302013-03-04 09:44:29 +0000811 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400812 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400813
814 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400815 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000816 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400817
Chris Masond3797302014-10-15 13:50:56 -0700818 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
819 if (ret)
820 goto fail;
821
David Howells2b0143b2015-03-17 22:25:59 +0000822 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000823 if (IS_ERR(inode)) {
824 ret = PTR_ERR(inode);
825 goto fail;
826 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900827
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000828 d_instantiate(dentry, inode);
829 ret = 0;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800830 pending_snapshot->anon_dev = 0;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000831fail:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800832 /* Prevent double freeing of anon_dev */
833 if (ret && pending_snapshot->snap)
834 pending_snapshot->snap->anon_dev = 0;
Josef Bacik00246522020-01-24 09:33:01 -0500835 btrfs_put_root(pending_snapshot->snap);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800836 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
David Sterbab0c0ea62015-11-10 18:54:00 +0100837free_pending:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800838 if (pending_snapshot->anon_dev)
839 free_anon_bdev(pending_snapshot->anon_dev);
David Sterbab0c0ea62015-11-10 18:54:00 +0100840 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100841 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100842 kfree(pending_snapshot);
843
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400844 return ret;
845}
846
Sage Weil4260f7c2010-10-29 15:46:43 -0400847/* copy of may_delete in fs/namei.c()
848 * Check whether we can remove a link victim from directory dir, check
849 * whether the type of victim is right.
850 * 1. We can't do it if dir is read-only (done in permission())
851 * 2. We should have write and exec permissions on dir
852 * 3. We can't remove anything from append-only dir
853 * 4. We can't do anything with immutable dir (done in permission())
854 * 5. If the sticky bit on dir is set we should either
855 * a. be owner of dir, or
856 * b. be owner of victim, or
857 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400858 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400859 * links pointing to it.
860 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
861 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
862 * 9. We can't remove a root or mountpoint.
863 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
864 * nfs_async_unlink().
865 */
866
Christian Braunerc4ed5332021-07-27 12:48:53 +0200867static int btrfs_may_delete(struct user_namespace *mnt_userns,
868 struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400869{
870 int error;
871
David Howells2b0143b2015-03-17 22:25:59 +0000872 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400873 return -ENOENT;
874
David Howells2b0143b2015-03-17 22:25:59 +0000875 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400876 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400877
Christian Braunerc4ed5332021-07-27 12:48:53 +0200878 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -0400879 if (error)
880 return error;
881 if (IS_APPEND(dir))
882 return -EPERM;
Christian Braunerc4ed5332021-07-27 12:48:53 +0200883 if (check_sticky(mnt_userns, dir, d_inode(victim)) ||
Christian Braunerba73d982021-01-21 14:19:31 +0100884 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
885 IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400886 return -EPERM;
887 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000888 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400889 return -ENOTDIR;
890 if (IS_ROOT(victim))
891 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000892 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400893 return -EISDIR;
894 if (IS_DEADDIR(dir))
895 return -ENOENT;
896 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
897 return -EBUSY;
898 return 0;
899}
900
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400901/* copy of may_create in fs/namei.c() */
Christian Brauner4d4340c2021-07-27 12:48:52 +0200902static inline int btrfs_may_create(struct user_namespace *mnt_userns,
903 struct inode *dir, struct dentry *child)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400904{
David Howells2b0143b2015-03-17 22:25:59 +0000905 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400906 return -EEXIST;
907 if (IS_DEADDIR(dir))
908 return -ENOENT;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200909 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Christian Brauner5474bf42021-07-27 12:48:51 +0200910 return -EOVERFLOW;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200911 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400912}
913
914/*
915 * Create a new subvolume below @parent. This is largely modeled after
916 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
917 * inside this filesystem so it's quite a bit simpler.
918 */
Al Viro92872092016-11-20 19:34:31 -0500919static noinline int btrfs_mksubvol(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200920 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +0100921 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400922 struct btrfs_root *snap_src,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200923 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000924 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400926 struct inode *dir = d_inode(parent->dentry);
927 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400928 struct dentry *dentry;
929 int error;
930
Al Viro00235412016-05-26 00:05:12 -0400931 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
932 if (error == -EINTR)
933 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400934
Christian Brauner4d4340c2021-07-27 12:48:52 +0200935 dentry = lookup_one(mnt_userns, name, parent->dentry, namelen);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400936 error = PTR_ERR(dentry);
937 if (IS_ERR(dentry))
938 goto out_unlock;
939
Christian Brauner4d4340c2021-07-27 12:48:52 +0200940 error = btrfs_may_create(mnt_userns, dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400941 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600942 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400943
Chris Mason9c520572012-12-17 14:26:57 -0500944 /*
945 * even if this name doesn't exist, we may get hash collisions.
946 * check for them now when we can safely fail
947 */
948 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
949 dir->i_ino, name,
950 namelen);
951 if (error)
952 goto out_dput;
953
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400954 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400955
956 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
957 goto out_up_read;
958
Nikolay Borisov9babda92020-03-13 17:23:20 +0200959 if (snap_src)
960 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
961 else
Christian Brauner4d4340c2021-07-27 12:48:52 +0200962 error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200963
Yan, Zheng76dda932009-09-21 16:00:26 -0400964 if (!error)
965 fsnotify_mkdir(dir, dentry);
966out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400967 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400968out_dput:
969 dput(dentry);
970out_unlock:
Josef Bacik64708532021-02-10 17:14:34 -0500971 btrfs_inode_unlock(dir, 0);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400972 return error;
973}
974
Robbie Koc11fbb62020-05-14 17:19:18 +0800975static noinline int btrfs_mksnapshot(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200976 struct user_namespace *mnt_userns,
Robbie Koc11fbb62020-05-14 17:19:18 +0800977 const char *name, int namelen,
978 struct btrfs_root *root,
979 bool readonly,
980 struct btrfs_qgroup_inherit *inherit)
981{
982 int ret;
983 bool snapshot_force_cow = false;
984
985 /*
986 * Force new buffered writes to reserve space even when NOCOW is
987 * possible. This is to avoid later writeback (running dealloc) to
988 * fallback to COW mode and unexpectedly fail with ENOSPC.
989 */
990 btrfs_drew_read_lock(&root->snapshot_lock);
991
Filipe Mananaf9baa502021-04-22 12:08:05 +0100992 ret = btrfs_start_delalloc_snapshot(root, false);
Robbie Koc11fbb62020-05-14 17:19:18 +0800993 if (ret)
994 goto out;
995
996 /*
997 * All previous writes have started writeback in NOCOW mode, so now
998 * we force future writes to fallback to COW mode during snapshot
999 * creation.
1000 */
1001 atomic_inc(&root->snapshot_force_cow);
1002 snapshot_force_cow = true;
1003
1004 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1005
Christian Brauner4d4340c2021-07-27 12:48:52 +02001006 ret = btrfs_mksubvol(parent, mnt_userns, name, namelen,
Robbie Koc11fbb62020-05-14 17:19:18 +08001007 root, readonly, inherit);
1008out:
1009 if (snapshot_force_cow)
1010 atomic_dec(&root->snapshot_force_cow);
1011 btrfs_drew_read_unlock(&root->snapshot_lock);
1012 return ret;
1013}
1014
Qu Wenruoe9eec722021-08-06 16:12:38 +08001015static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,
1016 bool locked)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001017{
1018 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001020 struct extent_map *em;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001021 const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize;
Chris Mason940100a2010-03-10 10:52:59 -05001022
1023 /*
1024 * hopefully we have this extent in the tree already, try without
1025 * the full extent lock
1026 */
1027 read_lock(&em_tree->lock);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001028 em = lookup_extent_mapping(em_tree, start, sectorsize);
Chris Mason940100a2010-03-10 10:52:59 -05001029 read_unlock(&em_tree->lock);
1030
1031 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001032 struct extent_state *cached = NULL;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001033 u64 end = start + sectorsize - 1;
Filipe Manana308d9802014-03-11 13:56:15 +00001034
Chris Mason940100a2010-03-10 10:52:59 -05001035 /* get the big lock and read metadata off disk */
Qu Wenruoe9eec722021-08-06 16:12:38 +08001036 if (!locked)
1037 lock_extent_bits(io_tree, start, end, &cached);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001038 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize);
Qu Wenruoe9eec722021-08-06 16:12:38 +08001039 if (!locked)
1040 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001041
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001042 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001043 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001044 }
1045
Li Zefan6c282eb2012-06-11 16:03:35 +08001046 return em;
1047}
1048
Qu Wenruoe9eec722021-08-06 16:12:38 +08001049static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
1050 bool locked)
Li Zefan6c282eb2012-06-11 16:03:35 +08001051{
1052 struct extent_map *next;
1053 bool ret = true;
1054
1055 /* this is the last extent */
1056 if (em->start + em->len >= i_size_read(inode))
1057 return false;
1058
Qu Wenruoe9eec722021-08-06 16:12:38 +08001059 next = defrag_lookup_extent(inode, em->start + em->len, locked);
Chris Masone9512d72014-08-26 13:55:54 -07001060 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1061 ret = false;
1062 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001063 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001064 ret = false;
1065
1066 free_extent_map(next);
1067 return ret;
1068}
1069
Chris Mason4cb53002011-05-24 15:35:30 -04001070/*
Qu Wenruo5767b502021-08-06 16:12:35 +08001071 * Prepare one page to be defragged.
Chris Mason4cb53002011-05-24 15:35:30 -04001072 *
Qu Wenruo5767b502021-08-06 16:12:35 +08001073 * This will ensure:
Chris Mason4cb53002011-05-24 15:35:30 -04001074 *
Qu Wenruo5767b502021-08-06 16:12:35 +08001075 * - Returned page is locked and has been set up properly.
1076 * - No ordered extent exists in the page.
1077 * - The page is uptodate.
1078 *
1079 * NOTE: Caller should also wait for page writeback after the cluster is
1080 * prepared, here we don't do writeback wait for each page.
Chris Mason4cb53002011-05-24 15:35:30 -04001081 */
Qu Wenruo5767b502021-08-06 16:12:35 +08001082static struct page *defrag_prepare_one_page(struct btrfs_inode *inode,
1083 pgoff_t index)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001084{
Qu Wenruo5767b502021-08-06 16:12:35 +08001085 struct address_space *mapping = inode->vfs_inode.i_mapping;
1086 gfp_t mask = btrfs_alloc_write_mask(mapping);
1087 u64 page_start = (u64)index << PAGE_SHIFT;
1088 u64 page_end = page_start + PAGE_SIZE - 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001089 struct extent_state *cached_state = NULL;
Qu Wenruo5767b502021-08-06 16:12:35 +08001090 struct page *page;
1091 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001092
Miao Xie600a45e2012-02-16 15:01:24 +08001093again:
Qu Wenruo5767b502021-08-06 16:12:35 +08001094 page = find_or_create_page(mapping, index, mask);
1095 if (!page)
1096 return ERR_PTR(-ENOMEM);
1097
Omar Sandoval24bcb452021-10-19 20:35:01 -07001098 /*
1099 * Since we can defragment files opened read-only, we can encounter
1100 * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We
1101 * can't do I/O using huge pages yet, so return an error for now.
1102 * Filesystem transparent huge pages are typically only used for
1103 * executables that explicitly enable them, so this isn't very
1104 * restrictive.
1105 */
1106 if (PageCompound(page)) {
1107 unlock_page(page);
1108 put_page(page);
1109 return ERR_PTR(-ETXTBSY);
1110 }
1111
Qu Wenruo5767b502021-08-06 16:12:35 +08001112 ret = set_page_extent_mapped(page);
1113 if (ret < 0) {
1114 unlock_page(page);
1115 put_page(page);
1116 return ERR_PTR(ret);
1117 }
1118
1119 /* Wait for any existing ordered extent in the range */
1120 while (1) {
1121 struct btrfs_ordered_extent *ordered;
1122
1123 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
1124 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
1125 unlock_extent_cached(&inode->io_tree, page_start, page_end,
1126 &cached_state);
1127 if (!ordered)
Chris Mason4cb53002011-05-24 15:35:30 -04001128 break;
1129
Qu Wenruo5767b502021-08-06 16:12:35 +08001130 unlock_page(page);
1131 btrfs_start_ordered_extent(ordered, 1);
1132 btrfs_put_ordered_extent(ordered);
1133 lock_page(page);
1134 /*
1135 * We unlocked the page above, so we need check if it was
1136 * released or not.
1137 */
1138 if (page->mapping != mapping || !PagePrivate(page)) {
Miao Xie600a45e2012-02-16 15:01:24 +08001139 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001140 put_page(page);
Miao Xie600a45e2012-02-16 15:01:24 +08001141 goto again;
1142 }
Chris Mason4cb53002011-05-24 15:35:30 -04001143 }
Chris Mason4cb53002011-05-24 15:35:30 -04001144
1145 /*
Qu Wenruo5767b502021-08-06 16:12:35 +08001146 * Now the page range has no ordered extent any more. Read the page to
1147 * make it uptodate.
Chris Mason4cb53002011-05-24 15:35:30 -04001148 */
Qu Wenruo5767b502021-08-06 16:12:35 +08001149 if (!PageUptodate(page)) {
1150 btrfs_readpage(NULL, page);
1151 lock_page(page);
1152 if (page->mapping != mapping || !PagePrivate(page)) {
1153 unlock_page(page);
1154 put_page(page);
1155 goto again;
Filipe Manana7f458a32020-11-04 11:07:33 +00001156 }
Qu Wenruo5767b502021-08-06 16:12:35 +08001157 if (!PageUptodate(page)) {
1158 unlock_page(page);
1159 put_page(page);
1160 return ERR_PTR(-EIO);
Filipe Manana7f458a32020-11-04 11:07:33 +00001161 }
Filipe Manana7f458a32020-11-04 11:07:33 +00001162 }
Qu Wenruo5767b502021-08-06 16:12:35 +08001163 return page;
Chris Mason4cb53002011-05-24 15:35:30 -04001164}
1165
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001166struct defrag_target_range {
1167 struct list_head list;
1168 u64 start;
1169 u64 len;
1170};
1171
1172/*
1173 * Collect all valid target extents.
1174 *
1175 * @start: file offset to lookup
1176 * @len: length to lookup
1177 * @extent_thresh: file extent size threshold, any extent size >= this value
1178 * will be ignored
1179 * @newer_than: only defrag extents newer than this value
1180 * @do_compress: whether the defrag is doing compression
1181 * if true, @extent_thresh will be ignored and all regular
1182 * file extents meeting @newer_than will be targets.
Qu Wenruoe9eec722021-08-06 16:12:38 +08001183 * @locked: if the range has already held extent lock
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001184 * @target_list: list of targets file extents
1185 */
1186static int defrag_collect_targets(struct btrfs_inode *inode,
1187 u64 start, u64 len, u32 extent_thresh,
1188 u64 newer_than, bool do_compress,
Qu Wenruoe9eec722021-08-06 16:12:38 +08001189 bool locked, struct list_head *target_list)
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001190{
1191 u64 cur = start;
1192 int ret = 0;
1193
1194 while (cur < start + len) {
1195 struct extent_map *em;
1196 struct defrag_target_range *new;
1197 bool next_mergeable = true;
1198 u64 range_len;
1199
Qu Wenruoe9eec722021-08-06 16:12:38 +08001200 em = defrag_lookup_extent(&inode->vfs_inode, cur, locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001201 if (!em)
1202 break;
1203
1204 /* Skip hole/inline/preallocated extents */
1205 if (em->block_start >= EXTENT_MAP_LAST_BYTE ||
1206 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1207 goto next;
1208
1209 /* Skip older extent */
1210 if (em->generation < newer_than)
1211 goto next;
1212
1213 /*
Filipe Manana0cb59502022-01-20 14:27:56 +00001214 * Our start offset might be in the middle of an existing extent
1215 * map, so take that into account.
1216 */
1217 range_len = em->len - (cur - em->start);
1218 /*
1219 * If this range of the extent map is already flagged for delalloc,
1220 * skip it, because:
1221 *
1222 * 1) We could deadlock later, when trying to reserve space for
1223 * delalloc, because in case we can't immediately reserve space
1224 * the flusher can start delalloc and wait for the respective
1225 * ordered extents to complete. The deadlock would happen
1226 * because we do the space reservation while holding the range
1227 * locked, and starting writeback, or finishing an ordered
1228 * extent, requires locking the range;
1229 *
1230 * 2) If there's delalloc there, it means there's dirty pages for
1231 * which writeback has not started yet (we clean the delalloc
1232 * flag when starting writeback and after creating an ordered
1233 * extent). If we mark pages in an adjacent range for defrag,
1234 * then we will have a larger contiguous range for delalloc,
1235 * very likely resulting in a larger extent after writeback is
1236 * triggered (except in a case of free space fragmentation).
1237 */
1238 if (test_range_bit(&inode->io_tree, cur, cur + range_len - 1,
1239 EXTENT_DELALLOC, 0, NULL))
1240 goto next;
1241
1242 /*
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001243 * For do_compress case, we want to compress all valid file
1244 * extents, thus no @extent_thresh or mergeable check.
1245 */
1246 if (do_compress)
1247 goto add;
1248
1249 /* Skip too large extent */
Filipe Manana0cb59502022-01-20 14:27:56 +00001250 if (range_len >= extent_thresh)
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001251 goto next;
1252
Qu Wenruoe9eec722021-08-06 16:12:38 +08001253 next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
1254 locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001255 if (!next_mergeable) {
1256 struct defrag_target_range *last;
1257
1258 /* Empty target list, no way to merge with last entry */
1259 if (list_empty(target_list))
1260 goto next;
1261 last = list_entry(target_list->prev,
1262 struct defrag_target_range, list);
1263 /* Not mergeable with last entry */
1264 if (last->start + last->len != cur)
1265 goto next;
1266
1267 /* Mergeable, fall through to add it to @target_list. */
1268 }
1269
1270add:
1271 range_len = min(extent_map_end(em), start + len) - cur;
1272 /*
1273 * This one is a good target, check if it can be merged into
1274 * last range of the target list.
1275 */
1276 if (!list_empty(target_list)) {
1277 struct defrag_target_range *last;
1278
1279 last = list_entry(target_list->prev,
1280 struct defrag_target_range, list);
1281 ASSERT(last->start + last->len <= cur);
1282 if (last->start + last->len == cur) {
1283 /* Mergeable, enlarge the last entry */
1284 last->len += range_len;
1285 goto next;
1286 }
1287 /* Fall through to allocate a new entry */
1288 }
1289
1290 /* Allocate new defrag_target_range */
1291 new = kmalloc(sizeof(*new), GFP_NOFS);
1292 if (!new) {
1293 free_extent_map(em);
1294 ret = -ENOMEM;
1295 break;
1296 }
1297 new->start = cur;
1298 new->len = range_len;
1299 list_add_tail(&new->list, target_list);
1300
1301next:
1302 cur = extent_map_end(em);
1303 free_extent_map(em);
1304 }
1305 if (ret < 0) {
1306 struct defrag_target_range *entry;
1307 struct defrag_target_range *tmp;
1308
1309 list_for_each_entry_safe(entry, tmp, target_list, list) {
1310 list_del_init(&entry->list);
1311 kfree(entry);
1312 }
1313 }
1314 return ret;
1315}
1316
Qu Wenruo22b398e2021-08-06 16:12:37 +08001317#define CLUSTER_SIZE (SZ_256K)
1318
1319/*
1320 * Defrag one contiguous target range.
1321 *
1322 * @inode: target inode
1323 * @target: target range to defrag
1324 * @pages: locked pages covering the defrag range
1325 * @nr_pages: number of locked pages
1326 *
1327 * Caller should ensure:
1328 *
1329 * - Pages are prepared
1330 * Pages should be locked, no ordered extent in the pages range,
1331 * no writeback.
1332 *
1333 * - Extent bits are locked
1334 */
1335static int defrag_one_locked_target(struct btrfs_inode *inode,
1336 struct defrag_target_range *target,
1337 struct page **pages, int nr_pages,
1338 struct extent_state **cached_state)
1339{
1340 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1341 struct extent_changeset *data_reserved = NULL;
1342 const u64 start = target->start;
1343 const u64 len = target->len;
1344 unsigned long last_index = (start + len - 1) >> PAGE_SHIFT;
1345 unsigned long start_index = start >> PAGE_SHIFT;
1346 unsigned long first_index = page_index(pages[0]);
1347 int ret = 0;
1348 int i;
1349
1350 ASSERT(last_index - first_index + 1 <= nr_pages);
1351
1352 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len);
1353 if (ret < 0)
1354 return ret;
1355 clear_extent_bit(&inode->io_tree, start, start + len - 1,
1356 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1357 EXTENT_DEFRAG, 0, 0, cached_state);
1358 set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state);
1359
1360 /* Update the page status */
1361 for (i = start_index - first_index; i <= last_index - first_index; i++) {
1362 ClearPageChecked(pages[i]);
1363 btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len);
1364 }
1365 btrfs_delalloc_release_extents(inode, len);
1366 extent_changeset_free(data_reserved);
1367
1368 return ret;
1369}
1370
Qu Wenruoe9eec722021-08-06 16:12:38 +08001371static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,
1372 u32 extent_thresh, u64 newer_than, bool do_compress)
1373{
1374 struct extent_state *cached_state = NULL;
1375 struct defrag_target_range *entry;
1376 struct defrag_target_range *tmp;
1377 LIST_HEAD(target_list);
1378 struct page **pages;
1379 const u32 sectorsize = inode->root->fs_info->sectorsize;
1380 u64 last_index = (start + len - 1) >> PAGE_SHIFT;
1381 u64 start_index = start >> PAGE_SHIFT;
1382 unsigned int nr_pages = last_index - start_index + 1;
1383 int ret = 0;
1384 int i;
1385
1386 ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE);
1387 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize));
1388
1389 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
1390 if (!pages)
1391 return -ENOMEM;
1392
1393 /* Prepare all pages */
1394 for (i = 0; i < nr_pages; i++) {
1395 pages[i] = defrag_prepare_one_page(inode, start_index + i);
1396 if (IS_ERR(pages[i])) {
1397 ret = PTR_ERR(pages[i]);
1398 pages[i] = NULL;
1399 goto free_pages;
1400 }
1401 }
1402 for (i = 0; i < nr_pages; i++)
1403 wait_on_page_writeback(pages[i]);
1404
1405 /* Lock the pages range */
1406 lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT,
1407 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1408 &cached_state);
1409 /*
1410 * Now we have a consistent view about the extent map, re-check
1411 * which range really needs to be defragged.
1412 *
1413 * And this time we have extent locked already, pass @locked = true
1414 * so that we won't relock the extent range and cause deadlock.
1415 */
1416 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1417 newer_than, do_compress, true,
1418 &target_list);
1419 if (ret < 0)
1420 goto unlock_extent;
1421
1422 list_for_each_entry(entry, &target_list, list) {
1423 ret = defrag_one_locked_target(inode, entry, pages, nr_pages,
1424 &cached_state);
1425 if (ret < 0)
1426 break;
1427 }
1428
1429 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1430 list_del_init(&entry->list);
1431 kfree(entry);
1432 }
1433unlock_extent:
1434 unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT,
1435 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1436 &cached_state);
1437free_pages:
1438 for (i = 0; i < nr_pages; i++) {
1439 if (pages[i]) {
1440 unlock_page(pages[i]);
1441 put_page(pages[i]);
1442 }
1443 }
1444 kfree(pages);
1445 return ret;
1446}
1447
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001448static int defrag_one_cluster(struct btrfs_inode *inode,
1449 struct file_ra_state *ra,
1450 u64 start, u32 len, u32 extent_thresh,
1451 u64 newer_than, bool do_compress,
1452 unsigned long *sectors_defragged,
1453 unsigned long max_sectors)
1454{
1455 const u32 sectorsize = inode->root->fs_info->sectorsize;
1456 struct defrag_target_range *entry;
1457 struct defrag_target_range *tmp;
1458 LIST_HEAD(target_list);
1459 int ret;
1460
1461 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
1462 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1463 newer_than, do_compress, false,
1464 &target_list);
1465 if (ret < 0)
1466 goto out;
1467
1468 list_for_each_entry(entry, &target_list, list) {
1469 u32 range_len = entry->len;
1470
Qu Wenruo484167d2022-01-18 15:19:04 +08001471 /* Reached or beyond the limit */
Qu Wenruoc080b412022-01-18 19:53:52 +08001472 if (max_sectors && *sectors_defragged >= max_sectors) {
1473 ret = 1;
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001474 break;
Qu Wenruoc080b412022-01-18 19:53:52 +08001475 }
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001476
1477 if (max_sectors)
1478 range_len = min_t(u32, range_len,
1479 (max_sectors - *sectors_defragged) * sectorsize);
1480
1481 if (ra)
1482 page_cache_sync_readahead(inode->vfs_inode.i_mapping,
1483 ra, NULL, entry->start >> PAGE_SHIFT,
1484 ((entry->start + range_len - 1) >> PAGE_SHIFT) -
1485 (entry->start >> PAGE_SHIFT) + 1);
1486 /*
1487 * Here we may not defrag any range if holes are punched before
1488 * we locked the pages.
1489 * But that's fine, it only affects the @sectors_defragged
1490 * accounting.
1491 */
1492 ret = defrag_one_range(inode, entry->start, range_len,
1493 extent_thresh, newer_than, do_compress);
1494 if (ret < 0)
1495 break;
Qu Wenruo484167d2022-01-18 15:19:04 +08001496 *sectors_defragged += range_len >>
1497 inode->root->fs_info->sectorsize_bits;
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001498 }
1499out:
1500 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1501 list_del_init(&entry->list);
1502 kfree(entry);
1503 }
1504 return ret;
1505}
1506
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001507/*
1508 * Entry point to file defragmentation.
1509 *
1510 * @inode: inode to be defragged
1511 * @ra: readahead state (can be NUL)
1512 * @range: defrag options including range and flags
1513 * @newer_than: minimum transid to defrag
1514 * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode
1515 * will be defragged.
Qu Wenruo484167d2022-01-18 15:19:04 +08001516 *
1517 * Return <0 for error.
Qu Wenruoc080b412022-01-18 19:53:52 +08001518 * Return >=0 for the number of sectors defragged, and range->start will be updated
1519 * to indicate the file offset where next defrag should be started at.
1520 * (Mostly for autodefrag, which sets @max_to_defrag thus we may exit early without
1521 * defragging all the range).
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001522 */
1523int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
Chris Mason4cb53002011-05-24 15:35:30 -04001524 struct btrfs_ioctl_defrag_range_args *range,
1525 u64 newer_than, unsigned long max_to_defrag)
1526{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001527 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo7b508032021-05-27 20:33:22 +08001528 unsigned long sectors_defragged = 0;
Li Zefan151a31b2011-09-02 15:56:39 +08001529 u64 isize = i_size_read(inode);
Qu Wenruo7b508032021-05-27 20:33:22 +08001530 u64 cur;
1531 u64 last_byte;
David Sterba1e2ef462017-07-17 20:01:59 +02001532 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001533 bool ra_allocated = false;
Qu Wenruo7b508032021-05-27 20:33:22 +08001534 int compress_type = BTRFS_COMPRESS_ZLIB;
1535 int ret = 0;
1536 u32 extent_thresh = range->extent_thresh;
Filipe Manana27cdfde2022-01-20 17:41:17 +00001537 pgoff_t start_index;
Chris Mason4cb53002011-05-24 15:35:30 -04001538
Liu Bo0abd5b12013-04-16 09:20:28 +00001539 if (isize == 0)
1540 return 0;
1541
1542 if (range->start >= isize)
1543 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001544
David Sterba1e2ef462017-07-17 20:01:59 +02001545 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001546 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001547 return -EINVAL;
1548 if (range->compress_type)
1549 compress_type = range->compress_type;
1550 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001551
Liu Bo0abd5b12013-04-16 09:20:28 +00001552 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001553 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001554
Qu Wenruo7b508032021-05-27 20:33:22 +08001555 if (range->start + range->len > range->start) {
1556 /* Got a specific range */
Filipe Manana6b34cd82022-01-17 16:28:29 +00001557 last_byte = min(isize, range->start + range->len);
Qu Wenruo7b508032021-05-27 20:33:22 +08001558 } else {
1559 /* Defrag until file end */
Filipe Manana6b34cd82022-01-17 16:28:29 +00001560 last_byte = isize;
Qu Wenruo7b508032021-05-27 20:33:22 +08001561 }
1562
Filipe Manana6b34cd82022-01-17 16:28:29 +00001563 /* Align the range */
1564 cur = round_down(range->start, fs_info->sectorsize);
1565 last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
1566
Chris Mason4cb53002011-05-24 15:35:30 -04001567 /*
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001568 * If we were not given a ra, allocate a readahead context. As
David Sterba0a52d102017-06-22 03:22:58 +02001569 * readahead is just an optimization, defrag will work without it so
1570 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001571 */
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001572 if (!ra) {
1573 ra_allocated = true;
David Sterba63e727e2017-06-22 03:13:02 +02001574 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001575 if (ra)
1576 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001577 }
1578
Filipe Manana27cdfde2022-01-20 17:41:17 +00001579 /*
1580 * Make writeback start from the beginning of the range, so that the
1581 * defrag range can be written sequentially.
1582 */
1583 start_index = cur >> PAGE_SHIFT;
1584 if (start_index < inode->i_mapping->writeback_index)
1585 inode->i_mapping->writeback_index = start_index;
1586
Qu Wenruo7b508032021-05-27 20:33:22 +08001587 while (cur < last_byte) {
Filipe Manana3c9d31c72022-01-20 17:11:52 +00001588 const unsigned long prev_sectors_defragged = sectors_defragged;
Qu Wenruo7b508032021-05-27 20:33:22 +08001589 u64 cluster_end;
Chris Mason1e701a32010-03-11 09:42:04 -05001590
Qu Wenruo7b508032021-05-27 20:33:22 +08001591 /* The cluster size 256K should always be page aligned */
1592 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
Chris Mason4cb53002011-05-24 15:35:30 -04001593
Filipe Mananab767c2f2022-01-18 13:43:31 +00001594 if (btrfs_defrag_cancelled(fs_info)) {
1595 ret = -EAGAIN;
1596 break;
1597 }
1598
Qu Wenruo7b508032021-05-27 20:33:22 +08001599 /* We want the cluster end at page boundary when possible */
1600 cluster_end = (((cur >> PAGE_SHIFT) +
1601 (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1;
1602 cluster_end = min(cluster_end, last_byte);
Chris Mason940100a2010-03-10 10:52:59 -05001603
Josef Bacik64708532021-02-10 17:14:34 -05001604 btrfs_inode_lock(inode, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001605 if (IS_SWAPFILE(inode)) {
1606 ret = -ETXTBSY;
Josef Bacik64708532021-02-10 17:14:34 -05001607 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001608 break;
Liu Boecb8bea2012-03-29 09:57:44 -04001609 }
Qu Wenruo7b508032021-05-27 20:33:22 +08001610 if (!(inode->i_sb->s_flags & SB_ACTIVE)) {
1611 btrfs_inode_unlock(inode, 0);
1612 break;
1613 }
1614 if (do_compress)
1615 BTRFS_I(inode)->defrag_compress = compress_type;
1616 ret = defrag_one_cluster(BTRFS_I(inode), ra, cur,
1617 cluster_end + 1 - cur, extent_thresh,
1618 newer_than, do_compress,
1619 &sectors_defragged, max_to_defrag);
Filipe Manana3c9d31c72022-01-20 17:11:52 +00001620
1621 if (sectors_defragged > prev_sectors_defragged)
1622 balance_dirty_pages_ratelimited(inode->i_mapping);
1623
Josef Bacik64708532021-02-10 17:14:34 -05001624 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001625 if (ret < 0)
1626 break;
1627 cur = cluster_end + 1;
Qu Wenruoc080b412022-01-18 19:53:52 +08001628 if (ret > 0) {
1629 ret = 0;
1630 break;
1631 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632 }
1633
Qu Wenruo7b508032021-05-27 20:33:22 +08001634 if (ra_allocated)
1635 kfree(ra);
Qu Wenruoc080b412022-01-18 19:53:52 +08001636 /*
1637 * Update range.start for autodefrag, this will indicate where to start
1638 * in next run.
1639 */
1640 range->start = cur;
Qu Wenruo7b508032021-05-27 20:33:22 +08001641 if (sectors_defragged) {
1642 /*
1643 * We have defragged some sectors, for compression case they
1644 * need to be written back immediately.
1645 */
1646 if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) {
Filipe Mananadec8ef92014-03-01 10:55:54 +00001647 filemap_flush(inode->i_mapping);
Qu Wenruo7b508032021-05-27 20:33:22 +08001648 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1649 &BTRFS_I(inode)->runtime_flags))
1650 filemap_flush(inode->i_mapping);
1651 }
1652 if (range->compress_type == BTRFS_COMPRESS_LZO)
1653 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1654 else if (range->compress_type == BTRFS_COMPRESS_ZSTD)
1655 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1656 ret = sectors_defragged;
Filipe Mananadec8ef92014-03-01 10:55:54 +00001657 }
David Sterba1e2ef462017-07-17 20:01:59 +02001658 if (do_compress) {
Josef Bacik64708532021-02-10 17:14:34 -05001659 btrfs_inode_lock(inode, 0);
David Sterbaeec63c62017-07-17 19:41:31 +02001660 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Josef Bacik64708532021-02-10 17:14:34 -05001661 btrfs_inode_unlock(inode, 0);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001662 }
Chris Mason940100a2010-03-10 10:52:59 -05001663 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001664}
1665
David Sterba17aaa432021-05-14 21:32:44 +02001666/*
1667 * Try to start exclusive operation @type or cancel it if it's running.
1668 *
1669 * Return:
1670 * 0 - normal mode, newly claimed op started
1671 * >0 - normal mode, something else is running,
1672 * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
1673 * ECANCELED - cancel mode, successful cancel
1674 * ENOTCONN - cancel mode, operation not running anymore
1675 */
1676static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
1677 enum btrfs_exclusive_operation type, bool cancel)
1678{
1679 if (!cancel) {
1680 /* Start normal op */
1681 if (!btrfs_exclop_start(fs_info, type))
1682 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1683 /* Exclusive operation is now claimed */
1684 return 0;
1685 }
1686
1687 /* Cancel running op */
1688 if (btrfs_exclop_start_try_lock(fs_info, type)) {
1689 /*
1690 * This blocks any exclop finish from setting it to NONE, so we
1691 * request cancellation. Either it runs and we will wait for it,
1692 * or it has finished and no waiting will happen.
1693 */
1694 atomic_inc(&fs_info->reloc_cancel_req);
1695 btrfs_exclop_start_unlock(fs_info);
1696
1697 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
1698 wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
1699 TASK_INTERRUPTIBLE);
1700
1701 return -ECANCELED;
1702 }
1703
1704 /* Something else is running or none */
1705 return -ENOTCONN;
1706}
1707
Miao Xie198605a2012-11-26 08:43:45 +00001708static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001709 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710{
Josef Bacik562d7b12021-10-05 16:12:42 -04001711 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 struct inode *inode = file_inode(file);
1713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714 u64 new_size;
1715 u64 old_size;
1716 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718 struct btrfs_ioctl_vol_args *vol_args;
1719 struct btrfs_trans_handle *trans;
1720 struct btrfs_device *device = NULL;
1721 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001722 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723 char *devstr = NULL;
1724 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001725 int mod = 0;
David Sterbabb059a32021-05-18 21:12:33 +02001726 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727
Chris Masone441d542009-01-05 16:57:23 -05001728 if (!capable(CAP_SYS_ADMIN))
1729 return -EPERM;
1730
Miao Xie198605a2012-11-26 08:43:45 +00001731 ret = mnt_want_write_file(file);
1732 if (ret)
1733 return ret;
1734
David Sterbabb059a32021-05-18 21:12:33 +02001735 /*
1736 * Read the arguments before checking exclusivity to be able to
1737 * distinguish regular resize and cancel
1738 */
Li Zefandae7b662009-04-08 15:06:54 +08001739 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001740 if (IS_ERR(vol_args)) {
1741 ret = PTR_ERR(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001742 goto out_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001743 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001744 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001745 sizestr = vol_args->name;
David Sterbabb059a32021-05-18 21:12:33 +02001746 cancel = (strcmp("cancel", sizestr) == 0);
1747 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
1748 if (ret)
1749 goto out_free;
1750 /* Exclusive operation is now claimed */
1751
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752 devstr = strchr(sizestr, ':');
1753 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001754 sizestr = devstr + 1;
1755 *devstr = '\0';
1756 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001757 ret = kstrtoull(devstr, 10, &devid);
1758 if (ret)
David Sterbabb059a32021-05-18 21:12:33 +02001759 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001760 if (!devid) {
1761 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001762 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001763 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001765 }
Miao Xiedba60f32012-12-21 09:19:51 +00001766
Josef Bacik562d7b12021-10-05 16:12:42 -04001767 args.devid = devid;
1768 device = btrfs_find_device(fs_info->fs_devices, &args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001769 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001770 btrfs_info(fs_info, "resizer unable to find device %llu",
1771 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001772 ret = -ENODEV;
David Sterbabb059a32021-05-18 21:12:33 +02001773 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001774 }
Miao Xiedba60f32012-12-21 09:19:51 +00001775
Anand Jainebbede42017-12-04 12:54:52 +08001776 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001777 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001778 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001779 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001780 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001781 goto out_finish;
Liu Bo4e42ae12012-06-14 02:23:19 -06001782 }
1783
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001784 if (!strcmp(sizestr, "max"))
Christoph Hellwigcda00eb2021-10-18 12:11:12 +02001785 new_size = bdev_nr_bytes(device->bdev);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001786 else {
1787 if (sizestr[0] == '-') {
1788 mod = -1;
1789 sizestr++;
1790 } else if (sizestr[0] == '+') {
1791 mod = 1;
1792 sizestr++;
1793 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001794 new_size = memparse(sizestr, &retptr);
1795 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001796 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001797 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001798 }
1799 }
1800
Anand Jain401e29c2017-12-04 12:54:55 +08001801 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001802 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001803 goto out_finish;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001804 }
1805
Miao Xie7cc8e582014-09-03 21:35:38 +08001806 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001807
1808 if (mod < 0) {
1809 if (new_size > old_size) {
1810 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001811 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001812 }
1813 new_size = old_size - new_size;
1814 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001815 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001816 ret = -ERANGE;
David Sterbabb059a32021-05-18 21:12:33 +02001817 goto out_finish;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001818 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001819 new_size = old_size + new_size;
1820 }
1821
Byongho Leeee221842015-12-15 01:42:10 +09001822 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001823 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001824 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001825 }
Christoph Hellwigcda00eb2021-10-18 12:11:12 +02001826 if (new_size > bdev_nr_bytes(device->bdev)) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001827 ret = -EFBIG;
David Sterbabb059a32021-05-18 21:12:33 +02001828 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001829 }
1830
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001831 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001832
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001833 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001834 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001835 if (IS_ERR(trans)) {
1836 ret = PTR_ERR(trans);
David Sterbabb059a32021-05-18 21:12:33 +02001837 goto out_finish;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001838 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001839 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001840 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001841 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001842 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001843 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001844
Marcos Paulo de Souzafaf8f7b2020-02-11 10:55:26 -03001845 if (ret == 0 && new_size != old_size)
1846 btrfs_info_in_rcu(fs_info,
1847 "resize device %s (devid %llu) from %llu to %llu",
1848 rcu_str_deref(device->name), device->devid,
1849 old_size, new_size);
David Sterbabb059a32021-05-18 21:12:33 +02001850out_finish:
1851 btrfs_exclop_finish(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001852out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001853 kfree(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001854out_drop:
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001855 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001856 return ret;
1857}
1858
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001859static noinline int __btrfs_ioctl_snap_create(struct file *file,
Christian Brauner4d4340c2021-07-27 12:48:52 +02001860 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +01001861 const char *name, unsigned long fd, int subvol,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001862 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001863 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001864{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001865 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001866 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001867
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001868 if (!S_ISDIR(file_inode(file)->i_mode))
1869 return -ENOTDIR;
1870
Liu Boa874a632012-06-29 03:58:46 -06001871 ret = mnt_want_write_file(file);
1872 if (ret)
1873 goto out;
1874
Sage Weil72fd0322010-10-29 15:41:32 -04001875 namelen = strlen(name);
1876 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001877 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001878 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001879 }
1880
Chris Mason16780ca2012-02-20 22:14:55 -05001881 if (name[0] == '.' &&
1882 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1883 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001884 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001885 }
1886
Chris Mason3de45862008-11-17 21:02:50 -05001887 if (subvol) {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001888 ret = btrfs_mksubvol(&file->f_path, mnt_userns, name,
1889 namelen, NULL, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001890 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001891 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001892 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001893 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001894 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001895 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001896 }
1897
Al Viro496ad9a2013-01-23 17:07:38 -05001898 src_inode = file_inode(src.file);
1899 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001900 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001901 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001902 ret = -EXDEV;
Christian Brauner4d4340c2021-07-27 12:48:52 +02001903 } else if (!inode_owner_or_capable(mnt_userns, src_inode)) {
David Sterbad0242062014-01-15 18:15:52 +01001904 /*
1905 * Subvolume creation is not restricted, but snapshots
1906 * are limited to own subvolumes only
1907 */
1908 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001909 } else {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001910 ret = btrfs_mksnapshot(&file->f_path, mnt_userns,
1911 name, namelen,
1912 BTRFS_I(src_inode)->root,
1913 readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001914 }
Al Viro2903ff02012-08-28 12:52:22 -04001915 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001916 }
Liu Boa874a632012-06-29 03:58:46 -06001917out_drop_write:
1918 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001919out:
Sage Weil72fd0322010-10-29 15:41:32 -04001920 return ret;
1921}
1922
1923static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001924 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001925{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001926 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001927 int ret;
1928
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001929 if (!S_ISDIR(file_inode(file)->i_mode))
1930 return -ENOTDIR;
1931
Li Zefanfa0d2b92010-12-20 15:53:28 +08001932 vol_args = memdup_user(arg, sizeof(*vol_args));
1933 if (IS_ERR(vol_args))
1934 return PTR_ERR(vol_args);
1935 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001936
Christian Brauner4d4340c2021-07-27 12:48:52 +02001937 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1938 vol_args->name, vol_args->fd, subvol,
1939 false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001940
Li Zefanfa0d2b92010-12-20 15:53:28 +08001941 kfree(vol_args);
1942 return ret;
1943}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001944
Li Zefanfa0d2b92010-12-20 15:53:28 +08001945static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1946 void __user *arg, int subvol)
1947{
1948 struct btrfs_ioctl_vol_args_v2 *vol_args;
1949 int ret;
Li Zefanb83cc962010-12-20 16:04:08 +08001950 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001951 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001952
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001953 if (!S_ISDIR(file_inode(file)->i_mode))
1954 return -ENOTDIR;
1955
Li Zefanfa0d2b92010-12-20 15:53:28 +08001956 vol_args = memdup_user(arg, sizeof(*vol_args));
1957 if (IS_ERR(vol_args))
1958 return PTR_ERR(vol_args);
1959 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001960
David Sterba673990d2020-02-21 13:24:37 +01001961 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08001962 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001963 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001964 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001965
Li Zefanb83cc962010-12-20 16:04:08 +08001966 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1967 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001968 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001969 u64 nums;
1970
1971 if (vol_args->size < sizeof(*inherit) ||
1972 vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001973 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001974 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001975 }
1976 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1977 if (IS_ERR(inherit)) {
1978 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001979 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001980 }
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001981
1982 if (inherit->num_qgroups > PAGE_SIZE ||
1983 inherit->num_ref_copies > PAGE_SIZE ||
1984 inherit->num_excl_copies > PAGE_SIZE) {
1985 ret = -EINVAL;
1986 goto free_inherit;
1987 }
1988
1989 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1990 2 * inherit->num_excl_copies;
1991 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1992 ret = -EINVAL;
1993 goto free_inherit;
1994 }
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001995 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001996
Christian Brauner4d4340c2021-07-27 12:48:52 +02001997 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1998 vol_args->name, vol_args->fd, subvol,
1999 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002000 if (ret)
2001 goto free_inherit;
Dan Carpenterc47ca322014-09-04 14:09:15 +03002002free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002003 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002004free_args:
2005 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002006 return ret;
2007}
2008
Li Zefan0caa1022010-12-20 16:30:25 +08002009static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
2010 void __user *arg)
2011{
Al Viro496ad9a2013-01-23 17:07:38 -05002012 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08002014 struct btrfs_root *root = BTRFS_I(inode)->root;
2015 int ret = 0;
2016 u64 flags = 0;
2017
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002018 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08002019 return -EINVAL;
2020
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002021 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002022 if (btrfs_root_readonly(root))
2023 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002024 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002025
2026 if (copy_to_user(arg, &flags, sizeof(flags)))
2027 ret = -EFAULT;
2028
2029 return ret;
2030}
2031
2032static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
2033 void __user *arg)
2034{
Al Viro496ad9a2013-01-23 17:07:38 -05002035 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002036 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08002037 struct btrfs_root *root = BTRFS_I(inode)->root;
2038 struct btrfs_trans_handle *trans;
2039 u64 root_flags;
2040 u64 flags;
2041 int ret = 0;
2042
Christian Brauner39e16742021-07-27 12:48:56 +02002043 if (!inode_owner_or_capable(file_mnt_user_ns(file), inode))
David Sterbabd60ea02014-01-16 15:50:22 +01002044 return -EPERM;
2045
Liu Bob9ca0662012-06-29 03:58:49 -06002046 ret = mnt_want_write_file(file);
2047 if (ret)
2048 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08002049
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002050 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06002051 ret = -EINVAL;
2052 goto out_drop_write;
2053 }
Li Zefan0caa1022010-12-20 16:30:25 +08002054
Liu Bob9ca0662012-06-29 03:58:49 -06002055 if (copy_from_user(&flags, arg, sizeof(flags))) {
2056 ret = -EFAULT;
2057 goto out_drop_write;
2058 }
Li Zefan0caa1022010-12-20 16:30:25 +08002059
Liu Bob9ca0662012-06-29 03:58:49 -06002060 if (flags & ~BTRFS_SUBVOL_RDONLY) {
2061 ret = -EOPNOTSUPP;
2062 goto out_drop_write;
2063 }
Li Zefan0caa1022010-12-20 16:30:25 +08002064
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002065 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002066
2067 /* nothing to do */
2068 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06002069 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08002070
2071 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01002072 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08002073 btrfs_set_root_flags(&root->root_item,
2074 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01002075 } else {
2076 /*
2077 * Block RO -> RW transition if this subvolume is involved in
2078 * send
2079 */
2080 spin_lock(&root->root_item_lock);
2081 if (root->send_in_progress == 0) {
2082 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08002083 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01002084 spin_unlock(&root->root_item_lock);
2085 } else {
2086 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002087 btrfs_warn(fs_info,
2088 "Attempt to set subvolume %llu read-write during send",
2089 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01002090 ret = -EPERM;
2091 goto out_drop_sem;
2092 }
2093 }
Li Zefan0caa1022010-12-20 16:30:25 +08002094
2095 trans = btrfs_start_transaction(root, 1);
2096 if (IS_ERR(trans)) {
2097 ret = PTR_ERR(trans);
2098 goto out_reset;
2099 }
2100
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002101 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08002102 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002103 if (ret < 0) {
2104 btrfs_end_transaction(trans);
2105 goto out_reset;
2106 }
Li Zefan0caa1022010-12-20 16:30:25 +08002107
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002108 ret = btrfs_commit_transaction(trans);
2109
Li Zefan0caa1022010-12-20 16:30:25 +08002110out_reset:
2111 if (ret)
2112 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06002113out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06002115out_drop_write:
2116 mnt_drop_write_file(file);
2117out:
Li Zefan0caa1022010-12-20 16:30:25 +08002118 return ret;
2119}
2120
Chris Masonac8e9812010-02-28 15:39:26 -05002121static noinline int key_in_sk(struct btrfs_key *key,
2122 struct btrfs_ioctl_search_key *sk)
2123{
Chris Masonabc6e132010-03-18 12:10:08 -04002124 struct btrfs_key test;
2125 int ret;
2126
2127 test.objectid = sk->min_objectid;
2128 test.type = sk->min_type;
2129 test.offset = sk->min_offset;
2130
2131 ret = btrfs_comp_cpu_keys(key, &test);
2132 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002133 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002134
2135 test.objectid = sk->max_objectid;
2136 test.type = sk->max_type;
2137 test.offset = sk->max_offset;
2138
2139 ret = btrfs_comp_cpu_keys(key, &test);
2140 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002141 return 0;
2142 return 1;
2143}
2144
Jeff Mahoneydf397562016-06-21 20:18:21 -04002145static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002146 struct btrfs_key *key,
2147 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002148 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002149 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002150 unsigned long *sk_offset,
2151 int *num_found)
2152{
2153 u64 found_transid;
2154 struct extent_buffer *leaf;
2155 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002156 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002157 unsigned long item_off;
2158 unsigned long item_len;
2159 int nritems;
2160 int i;
2161 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002162 int ret = 0;
2163
2164 leaf = path->nodes[0];
2165 slot = path->slots[0];
2166 nritems = btrfs_header_nritems(leaf);
2167
2168 if (btrfs_header_generation(leaf) > sk->max_transid) {
2169 i = nritems;
2170 goto advance_key;
2171 }
2172 found_transid = btrfs_header_generation(leaf);
2173
2174 for (i = slot; i < nritems; i++) {
2175 item_off = btrfs_item_ptr_offset(leaf, i);
Josef Bacik3212fa12021-10-21 14:58:35 -04002176 item_len = btrfs_item_size(leaf, i);
Chris Masonac8e9812010-02-28 15:39:26 -05002177
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002178 btrfs_item_key_to_cpu(leaf, key, i);
2179 if (!key_in_sk(key, sk))
2180 continue;
2181
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002182 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002183 if (*num_found) {
2184 ret = 1;
2185 goto out;
2186 }
Chris Masonac8e9812010-02-28 15:39:26 -05002187
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002188 /*
2189 * return one empty item back for v1, which does not
2190 * handle -EOVERFLOW
2191 */
2192
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002193 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002194 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002195 ret = -EOVERFLOW;
2196 }
Chris Masonac8e9812010-02-28 15:39:26 -05002197
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002198 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002199 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002200 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002201 }
2202
Chris Masonac8e9812010-02-28 15:39:26 -05002203 sh.objectid = key->objectid;
2204 sh.offset = key->offset;
2205 sh.type = key->type;
2206 sh.len = item_len;
2207 sh.transid = found_transid;
2208
Josef Bacika48b73e2020-08-10 11:42:27 -04002209 /*
2210 * Copy search result header. If we fault then loop again so we
2211 * can fault in the pages and -EFAULT there if there's a
2212 * problem. Otherwise we'll fault and then copy the buffer in
2213 * properly this next time through
2214 */
2215 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2216 ret = 0;
Gerhard Heiftba346b32014-01-30 16:24:02 +01002217 goto out;
2218 }
2219
Chris Masonac8e9812010-02-28 15:39:26 -05002220 *sk_offset += sizeof(sh);
2221
2222 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002223 char __user *up = ubuf + *sk_offset;
Josef Bacika48b73e2020-08-10 11:42:27 -04002224 /*
2225 * Copy the item, same behavior as above, but reset the
2226 * * sk_offset so we copy the full thing again.
2227 */
2228 if (read_extent_buffer_to_user_nofault(leaf, up,
2229 item_off, item_len)) {
2230 ret = 0;
2231 *sk_offset -= sizeof(sh);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002232 goto out;
2233 }
2234
Chris Masonac8e9812010-02-28 15:39:26 -05002235 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002236 }
Hugo Millse2156862011-05-14 17:43:41 +00002237 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002238
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002239 if (ret) /* -EOVERFLOW from above */
2240 goto out;
2241
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002242 if (*num_found >= sk->nr_items) {
2243 ret = 1;
2244 goto out;
2245 }
Chris Masonac8e9812010-02-28 15:39:26 -05002246 }
2247advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002248 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002249 test.objectid = sk->max_objectid;
2250 test.type = sk->max_type;
2251 test.offset = sk->max_offset;
2252 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2253 ret = 1;
2254 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002255 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002256 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002257 key->offset = 0;
2258 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002259 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002260 key->offset = 0;
2261 key->type = 0;
2262 key->objectid++;
2263 } else
2264 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002265out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002266 /*
2267 * 0: all items from this leaf copied, continue with next
2268 * 1: * more items can be copied, but unused buffer is too small
2269 * * all items were found
2270 * Either way, it will stops the loop which iterates to the next
2271 * leaf
2272 * -EOVERFLOW: item was to large for buffer
2273 * -EFAULT: could not copy extent buffer back to userspace
2274 */
Chris Masonac8e9812010-02-28 15:39:26 -05002275 return ret;
2276}
2277
2278static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002279 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002280 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002281 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002282{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002283 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002284 struct btrfs_root *root;
2285 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002286 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002287 int ret;
2288 int num_found = 0;
2289 unsigned long sk_offset = 0;
2290
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002291 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2292 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002293 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002294 }
Gerhard Heift12544442014-01-30 16:23:58 +01002295
Chris Masonac8e9812010-02-28 15:39:26 -05002296 path = btrfs_alloc_path();
2297 if (!path)
2298 return -ENOMEM;
2299
2300 if (sk->tree_id == 0) {
2301 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002302 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002303 } else {
David Sterba56e93572020-05-15 19:35:55 +02002304 root = btrfs_get_fs_root(info, sk->tree_id, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002305 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002306 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002307 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002308 }
2309 }
2310
2311 key.objectid = sk->min_objectid;
2312 key.type = sk->min_type;
2313 key.offset = sk->min_offset;
2314
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302315 while (1) {
Andreas Gruenbacherbb523b42021-08-02 13:44:20 +02002316 ret = -EFAULT;
2317 if (fault_in_writeable(ubuf + sk_offset, *buf_size - sk_offset))
Josef Bacika48b73e2020-08-10 11:42:27 -04002318 break;
2319
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002320 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002321 if (ret != 0) {
2322 if (ret > 0)
2323 ret = 0;
2324 goto err;
2325 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002326 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002327 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002328 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002329 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002330 break;
2331
2332 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002333 if (ret > 0)
2334 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002335err:
2336 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002337 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002338 btrfs_free_path(path);
2339 return ret;
2340}
2341
2342static noinline int btrfs_ioctl_tree_search(struct file *file,
2343 void __user *argp)
2344{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002345 struct btrfs_ioctl_search_args __user *uargs;
2346 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002347 struct inode *inode;
2348 int ret;
2349 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002350
2351 if (!capable(CAP_SYS_ADMIN))
2352 return -EPERM;
2353
Gerhard Heiftba346b32014-01-30 16:24:02 +01002354 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002355
Gerhard Heiftba346b32014-01-30 16:24:02 +01002356 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2357 return -EFAULT;
2358
2359 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002360
Al Viro496ad9a2013-01-23 17:07:38 -05002361 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002362 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002363
2364 /*
2365 * In the origin implementation an overflow is handled by returning a
2366 * search header with a len of zero, so reset ret.
2367 */
2368 if (ret == -EOVERFLOW)
2369 ret = 0;
2370
Gerhard Heiftba346b32014-01-30 16:24:02 +01002371 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002372 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002373 return ret;
2374}
2375
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002376static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2377 void __user *argp)
2378{
2379 struct btrfs_ioctl_search_args_v2 __user *uarg;
2380 struct btrfs_ioctl_search_args_v2 args;
2381 struct inode *inode;
2382 int ret;
2383 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002384 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002385
2386 if (!capable(CAP_SYS_ADMIN))
2387 return -EPERM;
2388
2389 /* copy search header and buffer size */
2390 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2391 if (copy_from_user(&args, uarg, sizeof(args)))
2392 return -EFAULT;
2393
2394 buf_size = args.buf_size;
2395
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002396 /* limit result size to 16MB */
2397 if (buf_size > buf_limit)
2398 buf_size = buf_limit;
2399
2400 inode = file_inode(file);
2401 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002402 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002403 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2404 ret = -EFAULT;
2405 else if (ret == -EOVERFLOW &&
2406 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2407 ret = -EFAULT;
2408
Yan, Zheng76dda932009-09-21 16:00:26 -04002409 return ret;
2410}
2411
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002412/*
Chris Masonac8e9812010-02-28 15:39:26 -05002413 * Search INODE_REFs to identify path name of 'dirid' directory
2414 * in a 'tree_id' tree. and sets path name to 'name'.
2415 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002416static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2417 u64 tree_id, u64 dirid, char *name)
2418{
2419 struct btrfs_root *root;
2420 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002421 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002422 int ret = -1;
2423 int slot;
2424 int len;
2425 int total_len = 0;
2426 struct btrfs_inode_ref *iref;
2427 struct extent_buffer *l;
2428 struct btrfs_path *path;
2429
2430 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2431 name[0]='\0';
2432 return 0;
2433 }
2434
2435 path = btrfs_alloc_path();
2436 if (!path)
2437 return -ENOMEM;
2438
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002439 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002440
David Sterba56e93572020-05-15 19:35:55 +02002441 root = btrfs_get_fs_root(info, tree_id, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002442 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002443 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002444 root = NULL;
2445 goto out;
2446 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002447
2448 key.objectid = dirid;
2449 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002450 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002451
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302452 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002453 ret = btrfs_search_backwards(root, &key, path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002454 if (ret < 0)
2455 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002456 else if (ret > 0) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002457 ret = -ENOENT;
2458 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002459 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002460
2461 l = path->nodes[0];
2462 slot = path->slots[0];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002463
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002464 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2465 len = btrfs_inode_ref_name_len(l, iref);
2466 ptr -= len + 1;
2467 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002468 if (ptr < name) {
2469 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002470 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002471 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002472
2473 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302474 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002475
2476 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2477 break;
2478
David Sterbab3b4aa72011-04-21 01:20:15 +02002479 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002480 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002481 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002482 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002483 }
Li Zefan77906a502011-07-14 03:16:00 +00002484 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302485 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002486 ret = 0;
2487out:
Josef Bacik00246522020-01-24 09:33:01 -05002488 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002489 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002490 return ret;
2491}
2492
Christian Brauner6623d9a2021-07-27 12:48:57 +02002493static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns,
2494 struct inode *inode,
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002495 struct btrfs_ioctl_ino_lookup_user_args *args)
2496{
2497 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2498 struct super_block *sb = inode->i_sb;
2499 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2500 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2501 u64 dirid = args->dirid;
2502 unsigned long item_off;
2503 unsigned long item_len;
2504 struct btrfs_inode_ref *iref;
2505 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002506 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002507 struct btrfs_path *path;
2508 struct btrfs_key key, key2;
2509 struct extent_buffer *leaf;
2510 struct inode *temp_inode;
2511 char *ptr;
2512 int slot;
2513 int len;
2514 int total_len = 0;
2515 int ret;
2516
2517 path = btrfs_alloc_path();
2518 if (!path)
2519 return -ENOMEM;
2520
2521 /*
2522 * If the bottom subvolume does not exist directly under upper_limit,
2523 * construct the path in from the bottom up.
2524 */
2525 if (dirid != upper_limit.objectid) {
2526 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2527
David Sterba56e93572020-05-15 19:35:55 +02002528 root = btrfs_get_fs_root(fs_info, treeid, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002529 if (IS_ERR(root)) {
2530 ret = PTR_ERR(root);
2531 goto out;
2532 }
2533
2534 key.objectid = dirid;
2535 key.type = BTRFS_INODE_REF_KEY;
2536 key.offset = (u64)-1;
2537 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002538 ret = btrfs_search_backwards(root, &key, path);
2539 if (ret < 0)
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002540 goto out_put;
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002541 else if (ret > 0) {
2542 ret = -ENOENT;
2543 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002544 }
2545
2546 leaf = path->nodes[0];
2547 slot = path->slots[0];
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002548
2549 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2550 len = btrfs_inode_ref_name_len(leaf, iref);
2551 ptr -= len + 1;
2552 total_len += len + 1;
2553 if (ptr < args->path) {
2554 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002555 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002556 }
2557
2558 *(ptr + len) = '/';
2559 read_extent_buffer(leaf, ptr,
2560 (unsigned long)(iref + 1), len);
2561
2562 /* Check the read+exec permission of this directory */
2563 ret = btrfs_previous_item(root, path, dirid,
2564 BTRFS_INODE_ITEM_KEY);
2565 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002566 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002567 } else if (ret > 0) {
2568 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002569 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002570 }
2571
2572 leaf = path->nodes[0];
2573 slot = path->slots[0];
2574 btrfs_item_key_to_cpu(leaf, &key2, slot);
2575 if (key2.objectid != dirid) {
2576 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002577 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002578 }
2579
David Sterba0202e832020-05-15 19:35:59 +02002580 temp_inode = btrfs_iget(sb, key2.objectid, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002581 if (IS_ERR(temp_inode)) {
2582 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002583 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002584 }
Christian Brauner6623d9a2021-07-27 12:48:57 +02002585 ret = inode_permission(mnt_userns, temp_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01002586 MAY_READ | MAY_EXEC);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002587 iput(temp_inode);
2588 if (ret) {
2589 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002590 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002591 }
2592
2593 if (key.offset == upper_limit.objectid)
2594 break;
2595 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2596 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002597 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002598 }
2599
2600 btrfs_release_path(path);
2601 key.objectid = key.offset;
2602 key.offset = (u64)-1;
2603 dirid = key.objectid;
2604 }
2605
2606 memmove(args->path, ptr, total_len);
2607 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002608 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002609 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002610 btrfs_release_path(path);
2611 }
2612
2613 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002614 key.objectid = treeid;
2615 key.type = BTRFS_ROOT_REF_KEY;
2616 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002617 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002618 if (ret < 0) {
2619 goto out;
2620 } else if (ret > 0) {
2621 ret = -ENOENT;
2622 goto out;
2623 }
2624
2625 leaf = path->nodes[0];
2626 slot = path->slots[0];
2627 btrfs_item_key_to_cpu(leaf, &key, slot);
2628
2629 item_off = btrfs_item_ptr_offset(leaf, slot);
Josef Bacik3212fa12021-10-21 14:58:35 -04002630 item_len = btrfs_item_size(leaf, slot);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002631 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2632 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2633 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2634 ret = -EINVAL;
2635 goto out;
2636 }
2637
2638 /* Copy subvolume's name */
2639 item_off += sizeof(struct btrfs_root_ref);
2640 item_len -= sizeof(struct btrfs_root_ref);
2641 read_extent_buffer(leaf, args->name, item_off, item_len);
2642 args->name[item_len] = 0;
2643
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002644out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002645 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002646out:
2647 btrfs_free_path(path);
2648 return ret;
2649}
2650
Chris Masonac8e9812010-02-28 15:39:26 -05002651static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2652 void __user *argp)
2653{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002654 struct btrfs_ioctl_ino_lookup_args *args;
2655 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002656 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002657
Julia Lawall2354d08f2010-10-29 15:14:18 -04002658 args = memdup_user(argp, sizeof(*args));
2659 if (IS_ERR(args))
2660 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002661
Al Viro496ad9a2013-01-23 17:07:38 -05002662 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002663
David Sterba01b810b2015-05-12 19:14:49 +02002664 /*
2665 * Unprivileged query to obtain the containing subvolume root id. The
2666 * path is reset so it's consistent with btrfs_search_path_in_tree.
2667 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002668 if (args->treeid == 0)
2669 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2670
David Sterba01b810b2015-05-12 19:14:49 +02002671 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2672 args->name[0] = 0;
2673 goto out;
2674 }
2675
2676 if (!capable(CAP_SYS_ADMIN)) {
2677 ret = -EPERM;
2678 goto out;
2679 }
2680
Chris Masonac8e9812010-02-28 15:39:26 -05002681 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2682 args->treeid, args->objectid,
2683 args->name);
2684
David Sterba01b810b2015-05-12 19:14:49 +02002685out:
Chris Masonac8e9812010-02-28 15:39:26 -05002686 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2687 ret = -EFAULT;
2688
2689 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002690 return ret;
2691}
2692
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002693/*
2694 * Version of ino_lookup ioctl (unprivileged)
2695 *
2696 * The main differences from ino_lookup ioctl are:
2697 *
2698 * 1. Read + Exec permission will be checked using inode_permission() during
2699 * path construction. -EACCES will be returned in case of failure.
2700 * 2. Path construction will be stopped at the inode number which corresponds
2701 * to the fd with which this ioctl is called. If constructed path does not
2702 * exist under fd's inode, -EACCES will be returned.
2703 * 3. The name of bottom subvolume is also searched and filled.
2704 */
2705static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2706{
2707 struct btrfs_ioctl_ino_lookup_user_args *args;
2708 struct inode *inode;
2709 int ret;
2710
2711 args = memdup_user(argp, sizeof(*args));
2712 if (IS_ERR(args))
2713 return PTR_ERR(args);
2714
2715 inode = file_inode(file);
2716
2717 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2718 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2719 /*
2720 * The subvolume does not exist under fd with which this is
2721 * called
2722 */
2723 kfree(args);
2724 return -EACCES;
2725 }
2726
Christian Brauner6623d9a2021-07-27 12:48:57 +02002727 ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002728
2729 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2730 ret = -EFAULT;
2731
2732 kfree(args);
2733 return ret;
2734}
2735
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002736/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2737static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2738{
2739 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2740 struct btrfs_fs_info *fs_info;
2741 struct btrfs_root *root;
2742 struct btrfs_path *path;
2743 struct btrfs_key key;
2744 struct btrfs_root_item *root_item;
2745 struct btrfs_root_ref *rref;
2746 struct extent_buffer *leaf;
2747 unsigned long item_off;
2748 unsigned long item_len;
2749 struct inode *inode;
2750 int slot;
2751 int ret = 0;
2752
2753 path = btrfs_alloc_path();
2754 if (!path)
2755 return -ENOMEM;
2756
2757 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2758 if (!subvol_info) {
2759 btrfs_free_path(path);
2760 return -ENOMEM;
2761 }
2762
2763 inode = file_inode(file);
2764 fs_info = BTRFS_I(inode)->root->fs_info;
2765
2766 /* Get root_item of inode's subvolume */
2767 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
David Sterba56e93572020-05-15 19:35:55 +02002768 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002769 if (IS_ERR(root)) {
2770 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002771 goto out_free;
2772 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002773 root_item = &root->root_item;
2774
2775 subvol_info->treeid = key.objectid;
2776
2777 subvol_info->generation = btrfs_root_generation(root_item);
2778 subvol_info->flags = btrfs_root_flags(root_item);
2779
2780 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2781 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2782 BTRFS_UUID_SIZE);
2783 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2784 BTRFS_UUID_SIZE);
2785
2786 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2787 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2788 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2789
2790 subvol_info->otransid = btrfs_root_otransid(root_item);
2791 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2792 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2793
2794 subvol_info->stransid = btrfs_root_stransid(root_item);
2795 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2796 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2797
2798 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2799 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2800 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2801
2802 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2803 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002804 key.type = BTRFS_ROOT_BACKREF_KEY;
2805 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002806 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002807 if (ret < 0) {
2808 goto out;
2809 } else if (path->slots[0] >=
2810 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002811 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002812 if (ret < 0) {
2813 goto out;
2814 } else if (ret > 0) {
2815 ret = -EUCLEAN;
2816 goto out;
2817 }
2818 }
2819
2820 leaf = path->nodes[0];
2821 slot = path->slots[0];
2822 btrfs_item_key_to_cpu(leaf, &key, slot);
2823 if (key.objectid == subvol_info->treeid &&
2824 key.type == BTRFS_ROOT_BACKREF_KEY) {
2825 subvol_info->parent_id = key.offset;
2826
2827 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2828 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2829
2830 item_off = btrfs_item_ptr_offset(leaf, slot)
2831 + sizeof(struct btrfs_root_ref);
Josef Bacik3212fa12021-10-21 14:58:35 -04002832 item_len = btrfs_item_size(leaf, slot)
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002833 - sizeof(struct btrfs_root_ref);
2834 read_extent_buffer(leaf, subvol_info->name,
2835 item_off, item_len);
2836 } else {
2837 ret = -ENOENT;
2838 goto out;
2839 }
2840 }
2841
2842 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2843 ret = -EFAULT;
2844
2845out:
Josef Bacik00246522020-01-24 09:33:01 -05002846 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002847out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002848 btrfs_free_path(path);
Waiman Longb091f7f2020-06-16 11:31:59 -04002849 kfree(subvol_info);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002850 return ret;
2851}
2852
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002853/*
2854 * Return ROOT_REF information of the subvolume containing this inode
2855 * except the subvolume name.
2856 */
2857static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2858{
2859 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2860 struct btrfs_root_ref *rref;
2861 struct btrfs_root *root;
2862 struct btrfs_path *path;
2863 struct btrfs_key key;
2864 struct extent_buffer *leaf;
2865 struct inode *inode;
2866 u64 objectid;
2867 int slot;
2868 int ret;
2869 u8 found;
2870
2871 path = btrfs_alloc_path();
2872 if (!path)
2873 return -ENOMEM;
2874
2875 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2876 if (IS_ERR(rootrefs)) {
2877 btrfs_free_path(path);
2878 return PTR_ERR(rootrefs);
2879 }
2880
2881 inode = file_inode(file);
2882 root = BTRFS_I(inode)->root->fs_info->tree_root;
2883 objectid = BTRFS_I(inode)->root->root_key.objectid;
2884
2885 key.objectid = objectid;
2886 key.type = BTRFS_ROOT_REF_KEY;
2887 key.offset = rootrefs->min_treeid;
2888 found = 0;
2889
2890 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2891 if (ret < 0) {
2892 goto out;
2893 } else if (path->slots[0] >=
2894 btrfs_header_nritems(path->nodes[0])) {
2895 ret = btrfs_next_leaf(root, path);
2896 if (ret < 0) {
2897 goto out;
2898 } else if (ret > 0) {
2899 ret = -EUCLEAN;
2900 goto out;
2901 }
2902 }
2903 while (1) {
2904 leaf = path->nodes[0];
2905 slot = path->slots[0];
2906
2907 btrfs_item_key_to_cpu(leaf, &key, slot);
2908 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2909 ret = 0;
2910 goto out;
2911 }
2912
2913 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2914 ret = -EOVERFLOW;
2915 goto out;
2916 }
2917
2918 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2919 rootrefs->rootref[found].treeid = key.offset;
2920 rootrefs->rootref[found].dirid =
2921 btrfs_root_ref_dirid(leaf, rref);
2922 found++;
2923
2924 ret = btrfs_next_item(root, path);
2925 if (ret < 0) {
2926 goto out;
2927 } else if (ret > 0) {
2928 ret = -EUCLEAN;
2929 goto out;
2930 }
2931 }
2932
2933out:
2934 if (!ret || ret == -EOVERFLOW) {
2935 rootrefs->num_items = found;
2936 /* update min_treeid for next search */
2937 if (found)
2938 rootrefs->min_treeid =
2939 rootrefs->rootref[found - 1].treeid + 1;
2940 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2941 ret = -EFAULT;
2942 }
2943
2944 kfree(rootrefs);
2945 btrfs_free_path(path);
2946
2947 return ret;
2948}
2949
Yan, Zheng76dda932009-09-21 16:00:26 -04002950static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002951 void __user *arg,
2952 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04002953{
Al Viro54563d42013-09-01 15:57:51 -04002954 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002956 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002957 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002958 struct inode *inode;
2959 struct btrfs_root *root = BTRFS_I(dir)->root;
2960 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002961 struct btrfs_ioctl_vol_args *vol_args = NULL;
2962 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002963 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002964 char *subvol_name, *subvol_name_ptr = NULL;
2965 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002966 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002967 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04002968
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002969 if (destroy_v2) {
2970 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2971 if (IS_ERR(vol_args2))
2972 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002973
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002974 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2975 err = -EOPNOTSUPP;
2976 goto out;
2977 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002978
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002979 /*
2980 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2981 * name, same as v1 currently does.
2982 */
2983 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2984 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2985 subvol_name = vol_args2->name;
2986
2987 err = mnt_want_write_file(file);
2988 if (err)
2989 goto out;
2990 } else {
Christian Brauneraabb34e2021-07-27 12:48:54 +02002991 struct inode *old_dir;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002992
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002993 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2994 err = -EINVAL;
2995 goto out;
2996 }
2997
2998 err = mnt_want_write_file(file);
2999 if (err)
3000 goto out;
3001
3002 dentry = btrfs_get_dentry(fs_info->sb,
3003 BTRFS_FIRST_FREE_OBJECTID,
3004 vol_args2->subvolid, 0, 0);
3005 if (IS_ERR(dentry)) {
3006 err = PTR_ERR(dentry);
3007 goto out_drop_write;
3008 }
3009
3010 /*
3011 * Change the default parent since the subvolume being
3012 * deleted can be outside of the current mount point.
3013 */
3014 parent = btrfs_get_parent(dentry);
3015
3016 /*
3017 * At this point dentry->d_name can point to '/' if the
3018 * subvolume we want to destroy is outsite of the
3019 * current mount point, so we need to release the
3020 * current dentry and execute the lookup to return a new
3021 * one with ->d_name pointing to the
3022 * <mount point>/subvol_name.
3023 */
3024 dput(dentry);
3025 if (IS_ERR(parent)) {
3026 err = PTR_ERR(parent);
3027 goto out_drop_write;
3028 }
Christian Brauneraabb34e2021-07-27 12:48:54 +02003029 old_dir = dir;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003030 dir = d_inode(parent);
3031
3032 /*
3033 * If v2 was used with SPEC_BY_ID, a new parent was
3034 * allocated since the subvolume can be outside of the
3035 * current mount point. Later on we need to release this
3036 * new parent dentry.
3037 */
3038 destroy_parent = true;
3039
Christian Brauneraabb34e2021-07-27 12:48:54 +02003040 /*
3041 * On idmapped mounts, deletion via subvolid is
3042 * restricted to subvolumes that are immediate
3043 * ancestors of the inode referenced by the file
3044 * descriptor in the ioctl. Otherwise the idmapping
3045 * could potentially be abused to delete subvolumes
3046 * anywhere in the filesystem the user wouldn't be able
3047 * to delete without an idmapped mount.
3048 */
3049 if (old_dir != dir && mnt_userns != &init_user_ns) {
3050 err = -EOPNOTSUPP;
3051 goto free_parent;
3052 }
3053
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003054 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
3055 fs_info, vol_args2->subvolid);
3056 if (IS_ERR(subvol_name_ptr)) {
3057 err = PTR_ERR(subvol_name_ptr);
3058 goto free_parent;
3059 }
David Sterba1a9fd412021-05-21 17:42:23 +02003060 /* subvol_name_ptr is already nul terminated */
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003061 subvol_name = (char *)kbasename(subvol_name_ptr);
3062 }
3063 } else {
3064 vol_args = memdup_user(arg, sizeof(*vol_args));
3065 if (IS_ERR(vol_args))
3066 return PTR_ERR(vol_args);
3067
3068 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3069 subvol_name = vol_args->name;
3070
3071 err = mnt_want_write_file(file);
3072 if (err)
3073 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04003074 }
3075
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003076 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04003077
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003078 if (strchr(subvol_name, '/') ||
3079 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3080 err = -EINVAL;
3081 goto free_subvol_name;
3082 }
3083
3084 if (!S_ISDIR(dir->i_mode)) {
3085 err = -ENOTDIR;
3086 goto free_subvol_name;
3087 }
David Sterba521e0542014-04-15 16:41:44 +02003088
Al Viro00235412016-05-26 00:05:12 -04003089 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3090 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003091 goto free_subvol_name;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003092 dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04003093 if (IS_ERR(dentry)) {
3094 err = PTR_ERR(dentry);
3095 goto out_unlock_dir;
3096 }
3097
David Howells2b0143b2015-03-17 22:25:59 +00003098 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003099 err = -ENOENT;
3100 goto out_dput;
3101 }
3102
David Howells2b0143b2015-03-17 22:25:59 +00003103 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04003104 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303105 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04003106 /*
3107 * Regular user. Only allow this with a special mount
3108 * option, when the user has write+exec access to the
3109 * subvol root, and when rmdir(2) would have been
3110 * allowed.
3111 *
3112 * Note that this is _not_ check that the subvol is
3113 * empty or doesn't contain data that we wouldn't
3114 * otherwise be able to delete.
3115 *
3116 * Users who want to delete empty subvols should try
3117 * rmdir(2).
3118 */
3119 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04003121 goto out_dput;
3122
3123 /*
3124 * Do not allow deletion if the parent dir is the same
3125 * as the dir to be deleted. That means the ioctl
3126 * must be called on the dentry referencing the root
3127 * of the subvol, not a random directory contained
3128 * within it.
3129 */
3130 err = -EINVAL;
3131 if (root == dest)
3132 goto out_dput;
3133
Christian Braunerc4ed5332021-07-27 12:48:53 +02003134 err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -04003135 if (err)
3136 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04003137 }
3138
Miao Xie5c39da52012-10-22 11:39:53 +00003139 /* check if subvolume may be deleted by a user */
Christian Braunerc4ed5332021-07-27 12:48:53 +02003140 err = btrfs_may_delete(mnt_userns, dir, dentry, 1);
Miao Xie5c39da52012-10-22 11:39:53 +00003141 if (err)
3142 goto out_dput;
3143
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003144 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003145 err = -EINVAL;
3146 goto out_dput;
3147 }
3148
Josef Bacik64708532021-02-10 17:14:34 -05003149 btrfs_inode_lock(inode, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003150 err = btrfs_delete_subvolume(dir, dentry);
Josef Bacik64708532021-02-10 17:14:34 -05003151 btrfs_inode_unlock(inode, 0);
Amir Goldsteina37d9a12022-01-20 23:53:04 +02003152 if (!err)
3153 d_delete_notify(dir, dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00003154
Yan, Zheng76dda932009-09-21 16:00:26 -04003155out_dput:
3156 dput(dentry);
3157out_unlock_dir:
Josef Bacik64708532021-02-10 17:14:34 -05003158 btrfs_inode_unlock(dir, 0);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003159free_subvol_name:
3160 kfree(subvol_name_ptr);
3161free_parent:
3162 if (destroy_parent)
3163 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003164out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003165 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003166out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003167 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003168 kfree(vol_args);
3169 return err;
3170}
3171
Chris Mason1e701a32010-03-11 09:42:04 -05003172static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003173{
Al Viro496ad9a2013-01-23 17:07:38 -05003174 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003175 struct btrfs_root *root = BTRFS_I(inode)->root;
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003176 struct btrfs_ioctl_defrag_range_args range = {0};
Yan Zhengc146afa2008-11-12 14:34:12 -05003177 int ret;
3178
Ilya Dryomov25122d12013-01-20 15:57:57 +02003179 ret = mnt_want_write_file(file);
3180 if (ret)
3181 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003182
Ilya Dryomov25122d12013-01-20 15:57:57 +02003183 if (btrfs_root_readonly(root)) {
3184 ret = -EROFS;
3185 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003186 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003187
3188 switch (inode->i_mode & S_IFMT) {
3189 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003190 if (!capable(CAP_SYS_ADMIN)) {
3191 ret = -EPERM;
3192 goto out;
3193 }
Eric Sandeende78b512013-01-31 18:21:12 +00003194 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003195 break;
3196 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003197 /*
3198 * Note that this does not check the file descriptor for write
3199 * access. This prevents defragmenting executables that are
3200 * running and allows defrag on files open in read-only mode.
3201 */
3202 if (!capable(CAP_SYS_ADMIN) &&
Christian Brauner47291ba2021-01-21 14:19:24 +01003203 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
Adam Borowski616d3742018-07-18 00:08:59 +02003204 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003205 goto out;
3206 }
Chris Mason1e701a32010-03-11 09:42:04 -05003207
Chris Mason1e701a32010-03-11 09:42:04 -05003208 if (argp) {
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003209 if (copy_from_user(&range, argp, sizeof(range))) {
Chris Mason1e701a32010-03-11 09:42:04 -05003210 ret = -EFAULT;
Dan Carpenter683be162010-03-20 11:24:48 +00003211 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003212 }
3213 /* compression requires us to start the IO */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003214 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3215 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
3216 range.extent_thresh = (u32)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003217 }
3218 } else {
3219 /* the rest are all set to zero by kzalloc */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003220 range.len = (u64)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003221 }
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08003222 ret = btrfs_defrag_file(file_inode(file), &file->f_ra,
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003223 &range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003224 if (ret > 0)
3225 ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003226 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003227 default:
3228 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003229 }
Chris Masone441d542009-01-05 16:57:23 -05003230out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003231 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003232 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003233}
3234
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003235static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003236{
3237 struct btrfs_ioctl_vol_args *vol_args;
Nikolay Borisova174c0a2021-11-25 11:14:43 +02003238 bool restore_op = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003239 int ret;
3240
Chris Masone441d542009-01-05 16:57:23 -05003241 if (!capable(CAP_SYS_ADMIN))
3242 return -EPERM;
3243
Nikolay Borisova174c0a2021-11-25 11:14:43 +02003244 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
3245 if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
3246 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3247
3248 /*
3249 * We can do the device add because we have a paused balanced,
3250 * change the exclusive op type and remember we should bring
3251 * back the paused balance
3252 */
3253 fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD;
3254 btrfs_exclop_start_unlock(fs_info);
3255 restore_op = true;
3256 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003257
Li Zefandae7b662009-04-08 15:06:54 +08003258 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003259 if (IS_ERR(vol_args)) {
3260 ret = PTR_ERR(vol_args);
3261 goto out;
3262 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003263
Mark Fasheh5516e592008-07-24 12:20:14 -04003264 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003265 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003266
Anand Jain43d20762014-07-01 00:58:56 +08003267 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003269
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003270 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003271out:
Nikolay Borisova174c0a2021-11-25 11:14:43 +02003272 if (restore_op)
3273 btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED);
3274 else
3275 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003276 return ret;
3277}
3278
Anand Jain6b526ed2016-02-13 10:01:39 +08003279static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003280{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003281 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003282 struct inode *inode = file_inode(file);
3283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003284 struct btrfs_ioctl_vol_args_v2 *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003285 struct block_device *bdev = NULL;
3286 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003287 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003288 bool cancel = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003289
Chris Masone441d542009-01-05 16:57:23 -05003290 if (!capable(CAP_SYS_ADMIN))
3291 return -EPERM;
3292
Li Zefandae7b662009-04-08 15:06:54 +08003293 vol_args = memdup_user(arg, sizeof(*vol_args));
Dan Carpenterd815b3f2021-11-16 14:50:25 +03003294 if (IS_ERR(vol_args))
3295 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003296
David Sterba748449cdb2020-02-21 13:30:14 +01003297 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003298 ret = -EOPNOTSUPP;
3299 goto out;
3300 }
Josef Bacik1a15eb72021-10-05 16:12:44 -04003301
David Sterba67ae34b2021-05-14 21:21:27 +02003302 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Josef Bacik1a15eb72021-10-05 16:12:44 -04003303 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3304 args.devid = vol_args->devid;
3305 } else if (!strcmp("cancel", vol_args->name)) {
David Sterba67ae34b2021-05-14 21:21:27 +02003306 cancel = true;
Josef Bacik1a15eb72021-10-05 16:12:44 -04003307 } else {
3308 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3309 if (ret)
3310 goto out;
3311 }
3312
3313 ret = mnt_want_write_file(file);
3314 if (ret)
3315 goto out;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003316
David Sterba67ae34b2021-05-14 21:21:27 +02003317 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3318 cancel);
3319 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003320 goto err_drop;
Anand Jain183860f2013-05-17 10:52:45 +00003321
Josef Bacik1a15eb72021-10-05 16:12:44 -04003322 /* Exclusive operation is now claimed */
3323 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003324
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003325 btrfs_exclop_finish(fs_info);
Anand Jain183860f2013-05-17 10:52:45 +00003326
Anand Jain6b526ed2016-02-13 10:01:39 +08003327 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003328 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003329 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003330 vol_args->devid);
3331 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003332 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003333 vol_args->name);
3334 }
Dan Carpenterc47ca322014-09-04 14:09:15 +03003335err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003336 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003337 if (bdev)
3338 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003339out:
3340 btrfs_put_dev_args_from_path(&args);
3341 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003342 return ret;
3343}
3344
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003345static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3346{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003347 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003348 struct inode *inode = file_inode(file);
3349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003350 struct btrfs_ioctl_vol_args *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003351 struct block_device *bdev = NULL;
3352 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003353 int ret;
Tom Rix37b45992022-01-21 05:45:22 -08003354 bool cancel = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003355
3356 if (!capable(CAP_SYS_ADMIN))
3357 return -EPERM;
3358
Josef Bacik1a15eb72021-10-05 16:12:44 -04003359 vol_args = memdup_user(arg, sizeof(*vol_args));
3360 if (IS_ERR(vol_args))
3361 return PTR_ERR(vol_args);
3362
3363 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3364 if (!strcmp("cancel", vol_args->name)) {
3365 cancel = true;
3366 } else {
3367 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3368 if (ret)
3369 goto out;
3370 }
3371
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003372 ret = mnt_want_write_file(file);
3373 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003374 goto out;
David Sterba67ae34b2021-05-14 21:21:27 +02003375
3376 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3377 cancel);
3378 if (ret == 0) {
Josef Bacik1a15eb72021-10-05 16:12:44 -04003379 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003380 if (!ret)
3381 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3382 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003383 }
3384
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003385 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003386 if (bdev)
3387 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003388out:
3389 btrfs_put_dev_args_from_path(&args);
3390 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003391 return ret;
3392}
3393
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003394static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3395 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003396{
Li Zefan027ed2f2011-06-08 08:27:56 +00003397 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003398 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003399 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003400 u64 flags_in;
Li Zefan027ed2f2011-06-08 08:27:56 +00003401 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003402
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003403 fi_args = memdup_user(arg, sizeof(*fi_args));
3404 if (IS_ERR(fi_args))
3405 return PTR_ERR(fi_args);
3406
3407 flags_in = fi_args->flags;
3408 memset(fi_args, 0, sizeof(*fi_args));
Li Zefan027ed2f2011-06-08 08:27:56 +00003409
David Sterbad03262c2017-06-16 00:09:21 +02003410 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003411 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003412
David Sterbad03262c2017-06-16 00:09:21 +02003413 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003414 if (device->devid > fi_args->max_id)
3415 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003416 }
David Sterbad03262c2017-06-16 00:09:21 +02003417 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003418
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003419 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003420 fi_args->nodesize = fs_info->nodesize;
3421 fi_args->sectorsize = fs_info->sectorsize;
3422 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003423
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003424 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3425 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3426 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3427 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3428 }
3429
Johannes Thumshirn0fb408a2020-07-13 21:28:59 +09003430 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3431 fi_args->generation = fs_info->generation;
3432 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3433 }
3434
Johannes Thumshirn49bac892020-07-13 21:29:00 +09003435 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3436 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3437 sizeof(fi_args->metadata_uuid));
3438 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3439 }
3440
Li Zefan027ed2f2011-06-08 08:27:56 +00003441 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3442 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003443
Li Zefan027ed2f2011-06-08 08:27:56 +00003444 kfree(fi_args);
3445 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003446}
3447
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003448static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3449 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003450{
Josef Bacik562d7b12021-10-05 16:12:42 -04003451 BTRFS_DEV_LOOKUP_ARGS(args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003452 struct btrfs_ioctl_dev_info_args *di_args;
3453 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003454 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003455
Jan Schmidt475f6382011-03-11 15:41:01 +01003456 di_args = memdup_user(arg, sizeof(*di_args));
3457 if (IS_ERR(di_args))
3458 return PTR_ERR(di_args);
3459
Josef Bacik562d7b12021-10-05 16:12:42 -04003460 args.devid = di_args->devid;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003461 if (!btrfs_is_empty_uuid(di_args->uuid))
Josef Bacik562d7b12021-10-05 16:12:42 -04003462 args.uuid = di_args->uuid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003463
David Sterbac5593ca2017-06-16 00:09:21 +02003464 rcu_read_lock();
Josef Bacik562d7b12021-10-05 16:12:42 -04003465 dev = btrfs_find_device(fs_info->fs_devices, &args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003466 if (!dev) {
3467 ret = -ENODEV;
3468 goto out;
3469 }
3470
3471 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003472 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3473 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003474 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003475 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003476 strncpy(di_args->path, rcu_str_deref(dev->name),
3477 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003478 di_args->path[sizeof(di_args->path) - 1] = 0;
3479 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003480 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003481 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003482
3483out:
David Sterbac5593ca2017-06-16 00:09:21 +02003484 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003485 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3486 ret = -EFAULT;
3487
3488 kfree(di_args);
3489 return ret;
3490}
3491
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003492static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3493{
Al Viro496ad9a2013-01-23 17:07:38 -05003494 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003495 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003496 struct btrfs_root *root = BTRFS_I(inode)->root;
3497 struct btrfs_root *new_root;
3498 struct btrfs_dir_item *di;
3499 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003500 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003501 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003502 u64 objectid = 0;
3503 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003504 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003505
3506 if (!capable(CAP_SYS_ADMIN))
3507 return -EPERM;
3508
Miao Xie3c04ce02012-11-26 08:43:07 +00003509 ret = mnt_want_write_file(file);
3510 if (ret)
3511 return ret;
3512
3513 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3514 ret = -EFAULT;
3515 goto out;
3516 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003517
3518 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303519 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003520
David Sterba56e93572020-05-15 19:35:55 +02003521 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00003522 if (IS_ERR(new_root)) {
3523 ret = PTR_ERR(new_root);
3524 goto out;
3525 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003526 if (!is_fstree(new_root->root_key.objectid)) {
3527 ret = -ENOENT;
3528 goto out_free;
3529 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003530
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003531 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003532 if (!path) {
3533 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003534 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00003535 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003536
3537 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003538 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00003539 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003540 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003541 }
3542
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3544 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003545 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003546 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003547 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003548 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003550 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003551 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003552 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003553 }
3554
3555 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3556 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3557 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003558 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003559
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003561 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003562out_free:
Josef Bacik00246522020-01-24 09:33:01 -05003563 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003564 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003565out:
3566 mnt_drop_write_file(file);
3567 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003568}
3569
Su Yuec065f5b12018-04-02 17:24:11 +08003570static void get_block_group_info(struct list_head *groups_list,
3571 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003572{
David Sterba32da53862019-10-29 19:20:18 +01003573 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003574
3575 space->total_bytes = 0;
3576 space->used_bytes = 0;
3577 space->flags = 0;
3578 list_for_each_entry(block_group, groups_list, list) {
3579 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02003580 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02003581 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003582 }
3583}
3584
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003585static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3586 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003587{
3588 struct btrfs_ioctl_space_args space_args;
3589 struct btrfs_ioctl_space_info space;
3590 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003591 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003592 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003593 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003594 static const u64 types[] = {
3595 BTRFS_BLOCK_GROUP_DATA,
3596 BTRFS_BLOCK_GROUP_SYSTEM,
3597 BTRFS_BLOCK_GROUP_METADATA,
3598 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3599 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003600 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003601 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003602 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003603 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003604 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003605
3606 if (copy_from_user(&space_args,
3607 (struct btrfs_ioctl_space_args __user *)arg,
3608 sizeof(space_args)))
3609 return -EFAULT;
3610
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003611 for (i = 0; i < num_types; i++) {
3612 struct btrfs_space_info *tmp;
3613
3614 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003615 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003616 if (tmp->flags == types[i]) {
3617 info = tmp;
3618 break;
3619 }
3620 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003621
3622 if (!info)
3623 continue;
3624
3625 down_read(&info->groups_sem);
3626 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3627 if (!list_empty(&info->block_groups[c]))
3628 slot_count++;
3629 }
3630 up_read(&info->groups_sem);
3631 }
Josef Bacik1406e432010-01-13 18:19:06 +00003632
David Sterba36523e952014-02-07 14:34:12 +01003633 /*
3634 * Global block reserve, exported as a space_info
3635 */
3636 slot_count++;
3637
Chris Mason7fde62b2010-03-16 15:40:10 -04003638 /* space_slots == 0 means they are asking for a count */
3639 if (space_args.space_slots == 0) {
3640 space_args.total_spaces = slot_count;
3641 goto out;
3642 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003643
Dan Rosenberg51788b12011-02-14 16:04:23 -05003644 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003645
Chris Mason7fde62b2010-03-16 15:40:10 -04003646 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003647
Chris Mason7fde62b2010-03-16 15:40:10 -04003648 /* we generally have at most 6 or so space infos, one for each raid
3649 * level. So, a whole page should be more than enough for everyone
3650 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003651 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003652 return -ENOMEM;
3653
3654 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003655 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003656 if (!dest)
3657 return -ENOMEM;
3658 dest_orig = dest;
3659
3660 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003661 for (i = 0; i < num_types; i++) {
3662 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003663
Dan Rosenberg51788b12011-02-14 16:04:23 -05003664 if (!slot_count)
3665 break;
3666
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003667 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003668 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003669 if (tmp->flags == types[i]) {
3670 info = tmp;
3671 break;
3672 }
3673 }
Chris Mason7fde62b2010-03-16 15:40:10 -04003674
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003675 if (!info)
3676 continue;
3677 down_read(&info->groups_sem);
3678 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3679 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003680 get_block_group_info(&info->block_groups[c],
3681 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003682 memcpy(dest, &space, sizeof(space));
3683 dest++;
3684 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003685 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003686 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003687 if (!slot_count)
3688 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003689 }
3690 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003691 }
Josef Bacik1406e432010-01-13 18:19:06 +00003692
David Sterba36523e952014-02-07 14:34:12 +01003693 /*
3694 * Add global block reserve
3695 */
3696 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003697 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003698
3699 spin_lock(&block_rsv->lock);
3700 space.total_bytes = block_rsv->size;
3701 space.used_bytes = block_rsv->size - block_rsv->reserved;
3702 spin_unlock(&block_rsv->lock);
3703 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3704 memcpy(dest, &space, sizeof(space));
3705 space_args.total_spaces++;
3706 }
3707
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003708 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003709 (arg + sizeof(struct btrfs_ioctl_space_args));
3710
3711 if (copy_to_user(user_dest, dest_orig, alloc_size))
3712 ret = -EFAULT;
3713
3714 kfree(dest_orig);
3715out:
3716 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003717 ret = -EFAULT;
3718
3719 return ret;
3720}
3721
Miao Xie9a8c28b2012-11-26 08:40:43 +00003722static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3723 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003724{
Sage Weil46204592010-10-29 15:41:32 -04003725 struct btrfs_trans_handle *trans;
3726 u64 transid;
3727
Miao Xied4edf392013-02-20 09:17:06 +00003728 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003729 if (IS_ERR(trans)) {
3730 if (PTR_ERR(trans) != -ENOENT)
3731 return PTR_ERR(trans);
3732
3733 /* No running transaction, don't bother */
3734 transid = root->fs_info->last_trans_committed;
3735 goto out;
3736 }
Sage Weil46204592010-10-29 15:41:32 -04003737 transid = trans->transid;
Josef Bacikfdfbf022021-11-05 16:45:28 -04003738 btrfs_commit_transaction_async(trans);
Miao Xieff7c1d32012-11-26 08:41:29 +00003739out:
Sage Weil46204592010-10-29 15:41:32 -04003740 if (argp)
3741 if (copy_to_user(argp, &transid, sizeof(transid)))
3742 return -EFAULT;
3743 return 0;
3744}
3745
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003746static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00003747 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003748{
Sage Weil46204592010-10-29 15:41:32 -04003749 u64 transid;
3750
3751 if (argp) {
3752 if (copy_from_user(&transid, argp, sizeof(transid)))
3753 return -EFAULT;
3754 } else {
3755 transid = 0; /* current trans */
3756 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003757 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04003758}
3759
Miao Xieb8e95482012-11-26 08:48:01 +00003760static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003762 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01003763 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003764 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003765
3766 if (!capable(CAP_SYS_ADMIN))
3767 return -EPERM;
3768
3769 sa = memdup_user(arg, sizeof(*sa));
3770 if (IS_ERR(sa))
3771 return PTR_ERR(sa);
3772
Miao Xieb8e95482012-11-26 08:48:01 +00003773 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3774 ret = mnt_want_write_file(file);
3775 if (ret)
3776 goto out;
3777 }
3778
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003779 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003780 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3781 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003782
Filipe Manana5afe6ce2020-01-16 11:29:20 +00003783 /*
3784 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3785 * error. This is important as it allows user space to know how much
3786 * progress scrub has done. For example, if scrub is canceled we get
3787 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3788 * space. Later user space can inspect the progress from the structure
3789 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3790 * previously (btrfs-progs does this).
3791 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3792 * then return -EFAULT to signal the structure was not copied or it may
3793 * be corrupt and unreliable due to a partial copy.
3794 */
3795 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003796 ret = -EFAULT;
3797
Miao Xieb8e95482012-11-26 08:48:01 +00003798 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3799 mnt_drop_write_file(file);
3800out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003801 kfree(sa);
3802 return ret;
3803}
3804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003805static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01003806{
3807 if (!capable(CAP_SYS_ADMIN))
3808 return -EPERM;
3809
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003810 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003811}
3812
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003813static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01003814 void __user *arg)
3815{
3816 struct btrfs_ioctl_scrub_args *sa;
3817 int ret;
3818
3819 if (!capable(CAP_SYS_ADMIN))
3820 return -EPERM;
3821
3822 sa = memdup_user(arg, sizeof(*sa));
3823 if (IS_ERR(sa))
3824 return PTR_ERR(sa);
3825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003826 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01003827
Filipe Manana4fa99b02018-12-14 19:45:13 +00003828 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003829 ret = -EFAULT;
3830
3831 kfree(sa);
3832 return ret;
3833}
3834
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003835static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06003836 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003837{
3838 struct btrfs_ioctl_get_dev_stats *sa;
3839 int ret;
3840
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003841 sa = memdup_user(arg, sizeof(*sa));
3842 if (IS_ERR(sa))
3843 return PTR_ERR(sa);
3844
David Sterbab27f7c02012-06-22 06:30:39 -06003845 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3846 kfree(sa);
3847 return -EPERM;
3848 }
3849
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003850 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003851
Filipe Mananaeee99572018-12-14 19:45:22 +00003852 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003853 ret = -EFAULT;
3854
3855 kfree(sa);
3856 return ret;
3857}
3858
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003859static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3860 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003861{
3862 struct btrfs_ioctl_dev_replace_args *p;
3863 int ret;
3864
3865 if (!capable(CAP_SYS_ADMIN))
3866 return -EPERM;
3867
3868 p = memdup_user(arg, sizeof(*p));
3869 if (IS_ERR(p))
3870 return PTR_ERR(p);
3871
3872 switch (p->cmd) {
3873 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01003874 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003875 ret = -EROFS;
3876 goto out;
3877 }
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003878 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003879 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003880 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003881 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003882 btrfs_exclop_finish(fs_info);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003883 }
3884 break;
3885 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003887 ret = 0;
3888 break;
3889 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08003890 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08003891 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003892 break;
3893 default:
3894 ret = -EINVAL;
3895 break;
3896 }
3897
Filipe Mananad3a53282019-01-08 11:42:09 +00003898 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003899 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003900out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003901 kfree(p);
3902 return ret;
3903}
3904
Jan Schmidtd7728c92011-07-07 16:48:38 +02003905static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3906{
3907 int ret = 0;
3908 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003909 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003910 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003911 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003912 struct inode_fs_paths *ipath = NULL;
3913 struct btrfs_path *path;
3914
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003915 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003916 return -EPERM;
3917
3918 path = btrfs_alloc_path();
3919 if (!path) {
3920 ret = -ENOMEM;
3921 goto out;
3922 }
3923
3924 ipa = memdup_user(arg, sizeof(*ipa));
3925 if (IS_ERR(ipa)) {
3926 ret = PTR_ERR(ipa);
3927 ipa = NULL;
3928 goto out;
3929 }
3930
3931 size = min_t(u32, ipa->size, 4096);
3932 ipath = init_ipath(size, root, path);
3933 if (IS_ERR(ipath)) {
3934 ret = PTR_ERR(ipath);
3935 ipath = NULL;
3936 goto out;
3937 }
3938
3939 ret = paths_from_inode(ipa->inum, ipath);
3940 if (ret < 0)
3941 goto out;
3942
3943 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003944 rel_ptr = ipath->fspath->val[i] -
3945 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003946 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003947 }
3948
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003949 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3950 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003951 if (ret) {
3952 ret = -EFAULT;
3953 goto out;
3954 }
3955
3956out:
3957 btrfs_free_path(path);
3958 free_ipath(ipath);
3959 kfree(ipa);
3960
3961 return ret;
3962}
3963
3964static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3965{
3966 struct btrfs_data_container *inodes = ctx;
3967 const size_t c = 3 * sizeof(u64);
3968
3969 if (inodes->bytes_left >= c) {
3970 inodes->bytes_left -= c;
3971 inodes->val[inodes->elem_cnt] = inum;
3972 inodes->val[inodes->elem_cnt + 1] = offset;
3973 inodes->val[inodes->elem_cnt + 2] = root;
3974 inodes->elem_cnt += 3;
3975 } else {
3976 inodes->bytes_missing += c - inodes->bytes_left;
3977 inodes->bytes_left = 0;
3978 inodes->elem_missed += 3;
3979 }
3980
3981 return 0;
3982}
3983
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003984static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003985 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003986{
3987 int ret = 0;
3988 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003989 struct btrfs_ioctl_logical_ino_args *loi;
3990 struct btrfs_data_container *inodes = NULL;
3991 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003992 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003993
3994 if (!capable(CAP_SYS_ADMIN))
3995 return -EPERM;
3996
3997 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05303998 if (IS_ERR(loi))
3999 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004000
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004001 if (version == 1) {
4002 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004003 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004004 } else {
4005 /* All reserved bits must be 0 for now */
4006 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4007 ret = -EINVAL;
4008 goto out_loi;
4009 }
4010 /* Only accept flags we have defined so far */
4011 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4012 ret = -EINVAL;
4013 goto out_loi;
4014 }
4015 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004016 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004017 }
4018
Jan Schmidtd7728c92011-07-07 16:48:38 +02004019 path = btrfs_alloc_path();
4020 if (!path) {
4021 ret = -ENOMEM;
4022 goto out;
4023 }
4024
Jan Schmidtd7728c92011-07-07 16:48:38 +02004025 inodes = init_data_container(size);
4026 if (IS_ERR(inodes)) {
4027 ret = PTR_ERR(inodes);
4028 inodes = NULL;
4029 goto out;
4030 }
4031
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004032 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004033 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004034 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004035 ret = -ENOENT;
4036 if (ret < 0)
4037 goto out;
4038
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004039 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4040 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004041 if (ret)
4042 ret = -EFAULT;
4043
4044out:
4045 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004046 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004047out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004048 kfree(loi);
4049
4050 return ret;
4051}
4052
David Sterba008ef092018-03-21 02:05:27 +01004053void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004054 struct btrfs_ioctl_balance_args *bargs)
4055{
4056 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4057
4058 bargs->flags = bctl->flags;
4059
David Sterba3009a622018-03-21 01:31:04 +01004060 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004061 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4062 if (atomic_read(&fs_info->balance_pause_req))
4063 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004064 if (atomic_read(&fs_info->balance_cancel_req))
4065 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004066
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004067 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4068 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4069 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004070
David Sterba008ef092018-03-21 02:05:27 +01004071 spin_lock(&fs_info->balance_lock);
4072 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4073 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004074}
4075
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004076static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004077{
Al Viro496ad9a2013-01-23 17:07:38 -05004078 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004079 struct btrfs_fs_info *fs_info = root->fs_info;
4080 struct btrfs_ioctl_balance_args *bargs;
4081 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004082 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004083 int ret;
4084
Nikolay Borisov6c405b22021-11-10 13:41:04 +02004085 if (!arg)
4086 btrfs_warn(fs_info,
4087 "IOC_BALANCE ioctl (v1) is deprecated and will be removed in kernel 5.18");
4088
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004089 if (!capable(CAP_SYS_ADMIN))
4090 return -EPERM;
4091
Liu Boe54bfa312012-06-29 03:58:48 -06004092 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004093 if (ret)
4094 return ret;
4095
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004096again:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004097 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004098 mutex_lock(&fs_info->balance_mutex);
4099 need_unlock = true;
4100 goto locked;
4101 }
4102
4103 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004104 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004105 * (1) some other op is running
4106 * (2) balance is running
4107 * (3) balance is paused -- special case (think resume)
4108 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004109 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004110 if (fs_info->balance_ctl) {
4111 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004112 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004113 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004114 /*
4115 * Lock released to allow other waiters to continue,
4116 * we'll reexamine the status again.
4117 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004118 mutex_lock(&fs_info->balance_mutex);
4119
4120 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004121 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004122 /* this is (3) */
4123 need_unlock = false;
4124 goto locked;
4125 }
4126
4127 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004128 goto again;
4129 } else {
4130 /* this is (2) */
4131 mutex_unlock(&fs_info->balance_mutex);
4132 ret = -EINPROGRESS;
4133 goto out;
4134 }
4135 } else {
4136 /* this is (1) */
4137 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004138 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004139 goto out;
4140 }
4141
4142locked:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004143
4144 if (arg) {
4145 bargs = memdup_user(arg, sizeof(*bargs));
4146 if (IS_ERR(bargs)) {
4147 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004148 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004149 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004150
4151 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4152 if (!fs_info->balance_ctl) {
4153 ret = -ENOTCONN;
4154 goto out_bargs;
4155 }
4156
4157 bctl = fs_info->balance_ctl;
4158 spin_lock(&fs_info->balance_lock);
4159 bctl->flags |= BTRFS_BALANCE_RESUME;
4160 spin_unlock(&fs_info->balance_lock);
Nikolay Borisovefc0e692021-11-25 11:14:41 +02004161 btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE);
Ilya Dryomovde322262012-01-16 22:04:49 +02004162
4163 goto do_balance;
4164 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004165 } else {
4166 bargs = NULL;
4167 }
4168
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004169 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004170 ret = -EINPROGRESS;
4171 goto out_bargs;
4172 }
4173
David Sterba8d2db782015-11-04 15:38:29 +01004174 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004175 if (!bctl) {
4176 ret = -ENOMEM;
4177 goto out_bargs;
4178 }
4179
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004180 if (arg) {
4181 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4182 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4183 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4184
4185 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004186 } else {
4187 /* balance everything - no filters */
4188 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004189 }
4190
David Sterba8eb93452015-10-12 16:55:54 +02004191 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4192 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004193 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004194 }
4195
Ilya Dryomovde322262012-01-16 22:04:49 +02004196do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004197 /*
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004198 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4199 * bctl is freed in reset_balance_state, or, if restriper was paused
4200 * all the way until unmount, in free_fs_info. The flag should be
4201 * cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004202 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004203 need_unlock = false;
4204
David Sterba6fcf6e22018-05-07 17:44:03 +02004205 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004206 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004207
Filipe Mananad00c2d92019-01-08 11:42:01 +00004208 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004209 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4210 ret = -EFAULT;
4211 }
4212
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004213out_bctl:
4214 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004215out_bargs:
4216 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004217out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004218 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004219 if (need_unlock)
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004220 btrfs_exclop_finish(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004221out:
Liu Boe54bfa312012-06-29 03:58:48 -06004222 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004223 return ret;
4224}
4225
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004226static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004227{
4228 if (!capable(CAP_SYS_ADMIN))
4229 return -EPERM;
4230
4231 switch (cmd) {
4232 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004233 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004234 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004235 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004236 }
4237
4238 return -EINVAL;
4239}
4240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004241static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004242 void __user *arg)
4243{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004244 struct btrfs_ioctl_balance_args *bargs;
4245 int ret = 0;
4246
4247 if (!capable(CAP_SYS_ADMIN))
4248 return -EPERM;
4249
4250 mutex_lock(&fs_info->balance_mutex);
4251 if (!fs_info->balance_ctl) {
4252 ret = -ENOTCONN;
4253 goto out;
4254 }
4255
David Sterba8d2db782015-11-04 15:38:29 +01004256 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004257 if (!bargs) {
4258 ret = -ENOMEM;
4259 goto out;
4260 }
4261
David Sterba008ef092018-03-21 02:05:27 +01004262 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004263
4264 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4265 ret = -EFAULT;
4266
4267 kfree(bargs);
4268out:
4269 mutex_unlock(&fs_info->balance_mutex);
4270 return ret;
4271}
4272
Miao Xie905b0dd2012-11-26 08:50:11 +00004273static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004275 struct inode *inode = file_inode(file);
4276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004277 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004278 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004279
4280 if (!capable(CAP_SYS_ADMIN))
4281 return -EPERM;
4282
Miao Xie905b0dd2012-11-26 08:50:11 +00004283 ret = mnt_want_write_file(file);
4284 if (ret)
4285 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004286
4287 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004288 if (IS_ERR(sa)) {
4289 ret = PTR_ERR(sa);
4290 goto drop_write;
4291 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004292
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004293 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004294
4295 switch (sa->cmd) {
4296 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004297 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004298 break;
4299 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004300 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004301 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004302 default:
4303 ret = -EINVAL;
4304 break;
4305 }
4306
Arne Jansen5d13a372011-09-14 15:53:51 +02004307 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004308 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004309drop_write:
4310 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004311 return ret;
4312}
4313
Miao Xie905b0dd2012-11-26 08:50:11 +00004314static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004316 struct inode *inode = file_inode(file);
4317 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4318 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004319 struct btrfs_ioctl_qgroup_assign_args *sa;
4320 struct btrfs_trans_handle *trans;
4321 int ret;
4322 int err;
4323
4324 if (!capable(CAP_SYS_ADMIN))
4325 return -EPERM;
4326
Miao Xie905b0dd2012-11-26 08:50:11 +00004327 ret = mnt_want_write_file(file);
4328 if (ret)
4329 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004330
4331 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004332 if (IS_ERR(sa)) {
4333 ret = PTR_ERR(sa);
4334 goto drop_write;
4335 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004336
4337 trans = btrfs_join_transaction(root);
4338 if (IS_ERR(trans)) {
4339 ret = PTR_ERR(trans);
4340 goto out;
4341 }
4342
Arne Jansen5d13a372011-09-14 15:53:51 +02004343 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004344 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004345 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004346 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004347 }
4348
Qu Wenruoe082f562015-02-27 16:24:28 +08004349 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004350 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004351 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004352 btrfs_handle_fs_error(fs_info, err,
4353 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004354 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004355 if (err && !ret)
4356 ret = err;
4357
4358out:
4359 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004360drop_write:
4361 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004362 return ret;
4363}
4364
Miao Xie905b0dd2012-11-26 08:50:11 +00004365static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004367 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004368 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004369 struct btrfs_ioctl_qgroup_create_args *sa;
4370 struct btrfs_trans_handle *trans;
4371 int ret;
4372 int err;
4373
4374 if (!capable(CAP_SYS_ADMIN))
4375 return -EPERM;
4376
Miao Xie905b0dd2012-11-26 08:50:11 +00004377 ret = mnt_want_write_file(file);
4378 if (ret)
4379 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004380
4381 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004382 if (IS_ERR(sa)) {
4383 ret = PTR_ERR(sa);
4384 goto drop_write;
4385 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004386
Miao Xied86e56c2012-11-15 11:35:41 +00004387 if (!sa->qgroupid) {
4388 ret = -EINVAL;
4389 goto out;
4390 }
4391
Arne Jansen5d13a372011-09-14 15:53:51 +02004392 trans = btrfs_join_transaction(root);
4393 if (IS_ERR(trans)) {
4394 ret = PTR_ERR(trans);
4395 goto out;
4396 }
4397
Arne Jansen5d13a372011-09-14 15:53:51 +02004398 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004399 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004400 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004401 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004402 }
4403
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004404 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004405 if (err && !ret)
4406 ret = err;
4407
4408out:
4409 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004410drop_write:
4411 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004412 return ret;
4413}
4414
Miao Xie905b0dd2012-11-26 08:50:11 +00004415static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004418 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004419 struct btrfs_ioctl_qgroup_limit_args *sa;
4420 struct btrfs_trans_handle *trans;
4421 int ret;
4422 int err;
4423 u64 qgroupid;
4424
4425 if (!capable(CAP_SYS_ADMIN))
4426 return -EPERM;
4427
Miao Xie905b0dd2012-11-26 08:50:11 +00004428 ret = mnt_want_write_file(file);
4429 if (ret)
4430 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004431
4432 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004433 if (IS_ERR(sa)) {
4434 ret = PTR_ERR(sa);
4435 goto drop_write;
4436 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004437
4438 trans = btrfs_join_transaction(root);
4439 if (IS_ERR(trans)) {
4440 ret = PTR_ERR(trans);
4441 goto out;
4442 }
4443
4444 qgroupid = sa->qgroupid;
4445 if (!qgroupid) {
4446 /* take the current subvol as qgroup */
4447 qgroupid = root->root_key.objectid;
4448 }
4449
Lu Fengqif0042d52018-07-18 14:45:35 +08004450 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004451
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004452 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004453 if (err && !ret)
4454 ret = err;
4455
4456out:
4457 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004458drop_write:
4459 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004460 return ret;
4461}
4462
Jan Schmidt2f232032013-04-25 16:04:51 +00004463static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4464{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004465 struct inode *inode = file_inode(file);
4466 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004467 struct btrfs_ioctl_quota_rescan_args *qsa;
4468 int ret;
4469
4470 if (!capable(CAP_SYS_ADMIN))
4471 return -EPERM;
4472
4473 ret = mnt_want_write_file(file);
4474 if (ret)
4475 return ret;
4476
4477 qsa = memdup_user(arg, sizeof(*qsa));
4478 if (IS_ERR(qsa)) {
4479 ret = PTR_ERR(qsa);
4480 goto drop_write;
4481 }
4482
4483 if (qsa->flags) {
4484 ret = -EINVAL;
4485 goto out;
4486 }
4487
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004488 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004489
4490out:
4491 kfree(qsa);
4492drop_write:
4493 mnt_drop_write_file(file);
4494 return ret;
4495}
4496
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004497static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4498 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004499{
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004500 struct btrfs_ioctl_quota_rescan_args qsa = {0};
Jan Schmidt2f232032013-04-25 16:04:51 +00004501
4502 if (!capable(CAP_SYS_ADMIN))
4503 return -EPERM;
4504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004505 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004506 qsa.flags = 1;
4507 qsa.progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004508 }
4509
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004510 if (copy_to_user(arg, &qsa, sizeof(qsa)))
Anand Jain991a3da2021-07-28 14:20:41 +08004511 return -EFAULT;
Jan Schmidt2f232032013-04-25 16:04:51 +00004512
Anand Jain991a3da2021-07-28 14:20:41 +08004513 return 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00004514}
4515
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004516static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4517 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004518{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004519 if (!capable(CAP_SYS_ADMIN))
4520 return -EPERM;
4521
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004522 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004523}
4524
Hugo Millsabccd002014-01-30 20:17:00 +00004525static long _btrfs_ioctl_set_received_subvol(struct file *file,
Christian Braunere4fed172021-07-27 12:48:55 +02004526 struct user_namespace *mnt_userns,
Hugo Millsabccd002014-01-30 20:17:00 +00004527 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004528{
Al Viro496ad9a2013-01-23 17:07:38 -05004529 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004530 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004531 struct btrfs_root *root = BTRFS_I(inode)->root;
4532 struct btrfs_root_item *root_item = &root->root_item;
4533 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004534 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004535 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004536 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004537
Christian Braunere4fed172021-07-27 12:48:55 +02004538 if (!inode_owner_or_capable(mnt_userns, inode))
David Sterbabd60ea02014-01-16 15:50:22 +01004539 return -EPERM;
4540
Alexander Block8ea05e32012-07-25 17:35:53 +02004541 ret = mnt_want_write_file(file);
4542 if (ret < 0)
4543 return ret;
4544
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004545 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004546
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004547 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004548 ret = -EINVAL;
4549 goto out;
4550 }
4551
4552 if (btrfs_root_readonly(root)) {
4553 ret = -EROFS;
4554 goto out;
4555 }
4556
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004557 /*
4558 * 1 - root item
4559 * 2 - uuid items (received uuid + subvol uuid)
4560 */
4561 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004562 if (IS_ERR(trans)) {
4563 ret = PTR_ERR(trans);
4564 trans = NULL;
4565 goto out;
4566 }
4567
4568 sa->rtransid = trans->transid;
4569 sa->rtime.sec = ct.tv_sec;
4570 sa->rtime.nsec = ct.tv_nsec;
4571
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004572 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4573 BTRFS_UUID_SIZE);
4574 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004575 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004576 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004577 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4578 root->root_key.objectid);
4579 if (ret && ret != -ENOENT) {
4580 btrfs_abort_transaction(trans, ret);
4581 btrfs_end_transaction(trans);
4582 goto out;
4583 }
4584 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004585 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4586 btrfs_set_root_stransid(root_item, sa->stransid);
4587 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004588 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4589 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4590 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4591 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004592
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004593 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004594 &root->root_key, &root->root_item);
4595 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004596 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004597 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004598 }
4599 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004600 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004601 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4602 root->root_key.objectid);
4603 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004604 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004605 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004606 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004607 }
4608 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004609 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004610out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004611 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004612 mnt_drop_write_file(file);
4613 return ret;
4614}
4615
4616#ifdef CONFIG_64BIT
4617static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4618 void __user *arg)
4619{
4620 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4621 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4622 int ret = 0;
4623
4624 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304625 if (IS_ERR(args32))
4626 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004627
David Sterba8d2db782015-11-04 15:38:29 +01004628 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004629 if (!args64) {
4630 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004631 goto out;
4632 }
4633
4634 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4635 args64->stransid = args32->stransid;
4636 args64->rtransid = args32->rtransid;
4637 args64->stime.sec = args32->stime.sec;
4638 args64->stime.nsec = args32->stime.nsec;
4639 args64->rtime.sec = args32->rtime.sec;
4640 args64->rtime.nsec = args32->rtime.nsec;
4641 args64->flags = args32->flags;
4642
Christian Braunere4fed172021-07-27 12:48:55 +02004643 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64);
Hugo Millsabccd002014-01-30 20:17:00 +00004644 if (ret)
4645 goto out;
4646
4647 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4648 args32->stransid = args64->stransid;
4649 args32->rtransid = args64->rtransid;
4650 args32->stime.sec = args64->stime.sec;
4651 args32->stime.nsec = args64->stime.nsec;
4652 args32->rtime.sec = args64->rtime.sec;
4653 args32->rtime.nsec = args64->rtime.nsec;
4654 args32->flags = args64->flags;
4655
4656 ret = copy_to_user(arg, args32, sizeof(*args32));
4657 if (ret)
4658 ret = -EFAULT;
4659
4660out:
4661 kfree(args32);
4662 kfree(args64);
4663 return ret;
4664}
4665#endif
4666
4667static long btrfs_ioctl_set_received_subvol(struct file *file,
4668 void __user *arg)
4669{
4670 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4671 int ret = 0;
4672
4673 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304674 if (IS_ERR(sa))
4675 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004676
Christian Braunere4fed172021-07-27 12:48:55 +02004677 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004678
4679 if (ret)
4680 goto out;
4681
Alexander Block8ea05e32012-07-25 17:35:53 +02004682 ret = copy_to_user(arg, sa, sizeof(*sa));
4683 if (ret)
4684 ret = -EFAULT;
4685
4686out:
4687 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004688 return ret;
4689}
4690
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004691static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4692 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00004693{
Anand Jaina1b83ac2013-07-19 17:39:32 +08004694 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004695 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004696 char label[BTRFS_LABEL_SIZE];
4697
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004698 spin_lock(&fs_info->super_lock);
4699 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4700 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004701
4702 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004703
4704 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004705 btrfs_warn(fs_info,
4706 "label is too long, return the first %zu bytes",
4707 --len);
jeff.liu867ab662013-01-05 02:48:01 +00004708 }
4709
jeff.liu867ab662013-01-05 02:48:01 +00004710 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004711
4712 return ret ? -EFAULT : 0;
4713}
4714
jeff.liua8bfd4a2013-01-05 02:48:08 +00004715static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004717 struct inode *inode = file_inode(file);
4718 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4719 struct btrfs_root *root = BTRFS_I(inode)->root;
4720 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004721 struct btrfs_trans_handle *trans;
4722 char label[BTRFS_LABEL_SIZE];
4723 int ret;
4724
4725 if (!capable(CAP_SYS_ADMIN))
4726 return -EPERM;
4727
4728 if (copy_from_user(label, arg, sizeof(label)))
4729 return -EFAULT;
4730
4731 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004732 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004733 "unable to set label with more than %d bytes",
4734 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004735 return -EINVAL;
4736 }
4737
4738 ret = mnt_want_write_file(file);
4739 if (ret)
4740 return ret;
4741
jeff.liua8bfd4a2013-01-05 02:48:08 +00004742 trans = btrfs_start_transaction(root, 0);
4743 if (IS_ERR(trans)) {
4744 ret = PTR_ERR(trans);
4745 goto out_unlock;
4746 }
4747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004748 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004749 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004751 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004752
4753out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004754 mnt_drop_write_file(file);
4755 return ret;
4756}
4757
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004758#define INIT_FEATURE_FLAGS(suffix) \
4759 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4760 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4761 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4762
David Sterbad5131b62016-02-17 15:26:27 +01004763int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004764{
David Sterba4d4ab6d2015-11-19 11:42:31 +01004765 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004766 INIT_FEATURE_FLAGS(SUPP),
4767 INIT_FEATURE_FLAGS(SAFE_SET),
4768 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4769 };
4770
4771 if (copy_to_user(arg, &features, sizeof(features)))
4772 return -EFAULT;
4773
4774 return 0;
4775}
4776
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004777static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4778 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004779{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004781 struct btrfs_ioctl_feature_flags features;
4782
4783 features.compat_flags = btrfs_super_compat_flags(super_block);
4784 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4785 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4786
4787 if (copy_to_user(arg, &features, sizeof(features)))
4788 return -EFAULT;
4789
4790 return 0;
4791}
4792
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004793static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004794 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004795 u64 change_mask, u64 flags, u64 supported_flags,
4796 u64 safe_set, u64 safe_clear)
4797{
David Sterbaf10152b2019-08-01 19:07:55 +02004798 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004799 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004800 u64 disallowed, unsupported;
4801 u64 set_mask = flags & change_mask;
4802 u64 clear_mask = ~flags & change_mask;
4803
4804 unsupported = set_mask & ~supported_flags;
4805 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004806 names = btrfs_printable_features(set, unsupported);
4807 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 btrfs_warn(fs_info,
4809 "this kernel does not support the %s feature bit%s",
4810 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004811 kfree(names);
4812 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 btrfs_warn(fs_info,
4814 "this kernel does not support %s bits 0x%llx",
4815 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004816 return -EOPNOTSUPP;
4817 }
4818
4819 disallowed = set_mask & ~safe_set;
4820 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004821 names = btrfs_printable_features(set, disallowed);
4822 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 btrfs_warn(fs_info,
4824 "can't set the %s feature bit%s while mounted",
4825 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004826 kfree(names);
4827 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004828 btrfs_warn(fs_info,
4829 "can't set %s bits 0x%llx while mounted",
4830 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004831 return -EPERM;
4832 }
4833
4834 disallowed = clear_mask & ~safe_clear;
4835 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004836 names = btrfs_printable_features(set, disallowed);
4837 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 btrfs_warn(fs_info,
4839 "can't clear the %s feature bit%s while mounted",
4840 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004841 kfree(names);
4842 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 btrfs_warn(fs_info,
4844 "can't clear %s bits 0x%llx while mounted",
4845 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004846 return -EPERM;
4847 }
4848
4849 return 0;
4850}
4851
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004852#define check_feature(fs_info, change_mask, flags, mask_base) \
4853check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004854 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4855 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4856 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4857
4858static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4859{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004860 struct inode *inode = file_inode(file);
4861 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4862 struct btrfs_root *root = BTRFS_I(inode)->root;
4863 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004864 struct btrfs_ioctl_feature_flags flags[2];
4865 struct btrfs_trans_handle *trans;
4866 u64 newflags;
4867 int ret;
4868
4869 if (!capable(CAP_SYS_ADMIN))
4870 return -EPERM;
4871
4872 if (copy_from_user(flags, arg, sizeof(flags)))
4873 return -EFAULT;
4874
4875 /* Nothing to do */
4876 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4877 !flags[0].incompat_flags)
4878 return 0;
4879
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004880 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004881 flags[1].compat_flags, COMPAT);
4882 if (ret)
4883 return ret;
4884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004885 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004886 flags[1].compat_ro_flags, COMPAT_RO);
4887 if (ret)
4888 return ret;
4889
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004890 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004891 flags[1].incompat_flags, INCOMPAT);
4892 if (ret)
4893 return ret;
4894
David Sterba7ab19622016-05-04 11:32:00 +02004895 ret = mnt_want_write_file(file);
4896 if (ret)
4897 return ret;
4898
David Sterba8051aa12014-02-07 14:34:04 +01004899 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02004900 if (IS_ERR(trans)) {
4901 ret = PTR_ERR(trans);
4902 goto out_drop_write;
4903 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004904
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004905 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004906 newflags = btrfs_super_compat_flags(super_block);
4907 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4908 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4909 btrfs_set_super_compat_flags(super_block, newflags);
4910
4911 newflags = btrfs_super_compat_ro_flags(super_block);
4912 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4913 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4914 btrfs_set_super_compat_ro_flags(super_block, newflags);
4915
4916 newflags = btrfs_super_incompat_flags(super_block);
4917 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4918 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4919 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004920 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004921
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004922 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02004923out_drop_write:
4924 mnt_drop_write_file(file);
4925
4926 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004927}
4928
Josef Bacik2351f432017-09-27 10:43:13 -04004929static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4930{
4931 struct btrfs_ioctl_send_args *arg;
4932 int ret;
4933
4934 if (compat) {
4935#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4936 struct btrfs_ioctl_send_args_32 args32;
4937
4938 ret = copy_from_user(&args32, argp, sizeof(args32));
4939 if (ret)
4940 return -EFAULT;
4941 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4942 if (!arg)
4943 return -ENOMEM;
4944 arg->send_fd = args32.send_fd;
4945 arg->clone_sources_count = args32.clone_sources_count;
4946 arg->clone_sources = compat_ptr(args32.clone_sources);
4947 arg->parent_root = args32.parent_root;
4948 arg->flags = args32.flags;
4949 memcpy(arg->reserved, args32.reserved,
4950 sizeof(args32.reserved));
4951#else
4952 return -ENOTTY;
4953#endif
4954 } else {
4955 arg = memdup_user(argp, sizeof(*arg));
4956 if (IS_ERR(arg))
4957 return PTR_ERR(arg);
4958 }
4959 ret = btrfs_ioctl_send(file, arg);
4960 kfree(arg);
4961 return ret;
4962}
4963
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004964long btrfs_ioctl(struct file *file, unsigned int
4965 cmd, unsigned long arg)
4966{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 struct inode *inode = file_inode(file);
4968 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4969 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004970 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004971
4972 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004973 case FS_IOC_GETVERSION:
4974 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004975 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004976 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004977 case FS_IOC_SETFSLABEL:
4978 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004979 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004980 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004981 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004982 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004983 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004984 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004985 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004986 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004987 case BTRFS_IOC_SUBVOL_CREATE_V2:
4988 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004989 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03004990 return btrfs_ioctl_snap_destroy(file, argp, false);
4991 case BTRFS_IOC_SNAP_DESTROY_V2:
4992 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08004993 case BTRFS_IOC_SUBVOL_GETFLAGS:
4994 return btrfs_ioctl_subvol_getflags(file, argp);
4995 case BTRFS_IOC_SUBVOL_SETFLAGS:
4996 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004997 case BTRFS_IOC_DEFAULT_SUBVOL:
4998 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004999 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005000 return btrfs_ioctl_defrag(file, NULL);
5001 case BTRFS_IOC_DEFRAG_RANGE:
5002 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005003 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005004 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005005 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005006 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005007 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005008 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005009 case BTRFS_IOC_RM_DEV_V2:
5010 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005011 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005012 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005013 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005014 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005015 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005016 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005017 case BTRFS_IOC_TREE_SEARCH:
5018 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005019 case BTRFS_IOC_TREE_SEARCH_V2:
5020 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005021 case BTRFS_IOC_INO_LOOKUP:
5022 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005023 case BTRFS_IOC_INO_PATHS:
5024 return btrfs_ioctl_ino_to_path(root, argp);
5025 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005026 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5027 case BTRFS_IOC_LOGICAL_INO_V2:
5028 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005029 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005030 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005031 case BTRFS_IOC_SYNC: {
5032 int ret;
5033
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02005034 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005035 if (ret)
5036 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005037 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005038 /*
5039 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005040 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005041 * processing uncleaned subvols.
5042 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005043 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005044 return ret;
5045 }
Sage Weil46204592010-10-29 15:41:32 -04005046 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005047 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005048 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005049 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005050 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005051 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005052 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005053 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005054 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005055 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005056 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005057 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005058 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005059 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005060 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005061 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005062 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5063 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005064#ifdef CONFIG_64BIT
5065 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5066 return btrfs_ioctl_set_received_subvol_32(file, argp);
5067#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005068 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005069 return _btrfs_ioctl_send(file, argp, false);
5070#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5071 case BTRFS_IOC_SEND_32:
5072 return _btrfs_ioctl_send(file, argp, true);
5073#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005074 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005075 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005076 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005077 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005078 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005079 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005080 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005081 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005082 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005083 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005084 case BTRFS_IOC_QUOTA_RESCAN:
5085 return btrfs_ioctl_quota_rescan(file, argp);
5086 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005087 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005088 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005089 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005090 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005091 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005092 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005093 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005094 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005095 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005096 case BTRFS_IOC_SET_FEATURES:
5097 return btrfs_ioctl_set_features(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005098 case BTRFS_IOC_GET_SUBVOL_INFO:
5099 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005100 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5101 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005102 case BTRFS_IOC_INO_LOOKUP_USER:
5103 return btrfs_ioctl_ino_lookup_user(file, argp);
Boris Burkov14605402021-06-30 13:01:49 -07005104 case FS_IOC_ENABLE_VERITY:
5105 return fsverity_ioctl_enable(file, (const void __user *)argp);
5106 case FS_IOC_MEASURE_VERITY:
5107 return fsverity_ioctl_measure(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005108 }
5109
5110 return -ENOTTY;
5111}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005112
5113#ifdef CONFIG_COMPAT
5114long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5115{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005116 /*
5117 * These all access 32-bit values anyway so no further
5118 * handling is necessary.
5119 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005120 switch (cmd) {
Luke Dashjr4c63c242015-10-29 08:22:21 +00005121 case FS_IOC32_GETVERSION:
5122 cmd = FS_IOC_GETVERSION;
5123 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005124 }
5125
5126 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5127}
5128#endif