blob: 8a442b59eee05592aa551614c0489d3e9d6cf774 [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
390 * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller
391 * must check the condition first that would allow none -> @type
392 */
393bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
394 enum btrfs_exclusive_operation type)
395{
396 spin_lock(&fs_info->super_lock);
397 if (fs_info->exclusive_operation == type)
398 return true;
399
400 spin_unlock(&fs_info->super_lock);
401 return false;
402}
403
404void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info)
405{
406 spin_unlock(&fs_info->super_lock);
407}
408
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500409void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
410{
David Sterba0d7ed322021-05-14 17:42:30 +0200411 spin_lock(&fs_info->super_lock);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500412 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
David Sterba0d7ed322021-05-14 17:42:30 +0200413 spin_unlock(&fs_info->super_lock);
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500414 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500415}
416
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200417static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
418{
Al Viro496ad9a2013-01-23 17:07:38 -0500419 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200420
421 return put_user(inode->i_generation, arg);
422}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400423
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300424static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
425 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000426{
Li Dongyangf7039b12011-03-24 10:24:28 +0000427 struct btrfs_device *device;
428 struct request_queue *q;
429 struct fstrim_range range;
430 u64 minlen = ULLONG_MAX;
431 u64 num_devices = 0;
432 int ret;
433
434 if (!capable(CAP_SYS_ADMIN))
435 return -EPERM;
436
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000437 /*
Naohiro Aota1cb3dc32021-02-04 19:21:46 +0900438 * btrfs_trim_block_group() depends on space cache, which is not
439 * available in zoned filesystem. So, disallow fitrim on a zoned
440 * filesystem for now.
441 */
442 if (btrfs_is_zoned(fs_info))
443 return -EOPNOTSUPP;
444
445 /*
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000446 * If the fs is mounted with nologreplay, which requires it to be
447 * mounted in RO mode as well, we can not allow discard on free space
448 * inside block groups, because log trees refer to extents that are not
449 * pinned in a block group's free space cache (pinning the extents is
450 * precisely the first phase of replaying a log tree).
451 */
452 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
453 return -EROFS;
454
Xiao Guangrong1f781602011-04-20 10:09:16 +0000455 rcu_read_lock();
456 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
457 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000458 if (!device->bdev)
459 continue;
460 q = bdev_get_queue(device->bdev);
461 if (blk_queue_discard(q)) {
462 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100463 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000464 minlen);
465 }
466 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000467 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200468
Li Dongyangf7039b12011-03-24 10:24:28 +0000469 if (!num_devices)
470 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000471 if (copy_from_user(&range, arg, sizeof(range)))
472 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800473
474 /*
475 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
476 * block group is in the logical address space, which can be any
477 * sectorsize aligned bytenr in the range [0, U64_MAX].
478 */
479 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200480 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000481
482 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400483 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000484 if (ret < 0)
485 return ret;
486
487 if (copy_to_user(arg, &range, sizeof(range)))
488 return -EFAULT;
489
490 return 0;
491}
492
David Sterbae1f60a62019-10-01 19:57:39 +0200493int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200494{
Chris Mason46e0f662013-11-15 12:14:55 +0100495 int i;
496
497 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
498 if (uuid[i])
499 return 0;
500 }
501 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200502}
503
Christian Brauner4d4340c2021-07-27 12:48:52 +0200504static noinline int create_subvol(struct user_namespace *mnt_userns,
505 struct inode *dir, struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100506 const char *name, int namelen,
Miao Xie8696c532013-02-07 06:02:44 +0000507 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400509 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400510 struct btrfs_trans_handle *trans;
511 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100512 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400513 struct btrfs_inode_item *inode_item;
514 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000515 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400516 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000517 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700518 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900519 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400520 int ret;
521 int err;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800522 dev_t anon_dev = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400523 u64 objectid;
Chris Mason3de45862008-11-17 21:02:50 -0500524 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525
David Sterba49a3c4d2016-03-24 17:49:22 +0100526 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
527 if (!root_item)
528 return -ENOMEM;
529
Nikolay Borisov543068a2020-12-07 17:32:33 +0200530 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400531 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100532 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000533
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800534 ret = get_anon_bdev(&anon_dev);
535 if (ret < 0)
536 goto fail_free;
537
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800538 /*
539 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400540 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800541 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100542 if (btrfs_qgroup_level(objectid)) {
543 ret = -ENOSPC;
544 goto fail_free;
545 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800546
Miao Xied5c12072013-02-28 10:04:33 +0000547 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400548 /*
Miao Xied5c12072013-02-28 10:04:33 +0000549 * The same as the snapshot creation, please see the comment
550 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400551 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800552 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000553 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100554 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555
Miao Xied5c12072013-02-28 10:04:33 +0000556 trans = btrfs_start_transaction(root, 0);
557 if (IS_ERR(trans)) {
558 ret = PTR_ERR(trans);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800559 btrfs_subvolume_release_metadata(root, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100560 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000561 }
562 trans->block_rsv = &block_rsv;
563 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564
Lu Fengqia93774222018-07-18 14:45:41 +0800565 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200566 if (ret)
567 goto fail;
568
Josef Bacik9631e4c2020-08-20 11:46:03 -0400569 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
570 BTRFS_NESTING_NORMAL);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400571 if (IS_ERR(leaf)) {
572 ret = PTR_ERR(leaf);
573 goto fail;
574 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400576 btrfs_mark_buffer_dirty(leaf);
577
David Sterba49a3c4d2016-03-24 17:49:22 +0100578 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800579 btrfs_set_stack_inode_generation(inode_item, 1);
580 btrfs_set_stack_inode_size(inode_item, 3);
581 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400582 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400583 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800584 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585
David Sterba49a3c4d2016-03-24 17:49:22 +0100586 btrfs_set_root_flags(root_item, 0);
587 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800588 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000589
David Sterba49a3c4d2016-03-24 17:49:22 +0100590 btrfs_set_root_bytenr(root_item, leaf->start);
591 btrfs_set_root_generation(root_item, trans->transid);
592 btrfs_set_root_level(root_item, 0);
593 btrfs_set_root_refs(root_item, 1);
594 btrfs_set_root_used(root_item, leaf->len);
595 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400596
David Sterba49a3c4d2016-03-24 17:49:22 +0100597 btrfs_set_root_generation_v2(root_item,
598 btrfs_root_generation(root_item));
Andy Shevchenko807fc792020-02-24 17:37:51 +0200599 generate_random_guid(root_item->uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100600 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
601 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
602 root_item->ctime = root_item->otime;
603 btrfs_set_root_ctransid(root_item, trans->transid);
604 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400605
Chris Mason925baed2008-06-25 16:01:30 -0400606 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400607
Nikolay Borisov69948022020-12-07 17:32:37 +0200608 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400609
610 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400611 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200612 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400613 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100614 root_item);
Filipe Manana67addf22021-04-20 10:55:12 +0100615 if (ret) {
616 /*
617 * Since we don't abort the transaction in this case, free the
618 * tree block so that we don't leak space and leave the
619 * filesystem in an inconsistent state (an extent item in the
Filipe Manana7a1636082021-12-13 08:45:12 +0000620 * extent tree with a backreference for a root that does not
Filipe Manana212a58f2021-12-13 08:45:13 +0000621 * exists).
Filipe Manana67addf22021-04-20 10:55:12 +0100622 */
Filipe Manana212a58f2021-12-13 08:45:13 +0000623 btrfs_tree_lock(leaf);
624 btrfs_clean_tree_block(leaf);
625 btrfs_tree_unlock(leaf);
Filipe Manana7a1636082021-12-13 08:45:12 +0000626 btrfs_free_tree_block(trans, objectid, leaf, 0, 1);
Filipe Manana67addf22021-04-20 10:55:12 +0100627 free_extent_buffer(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400628 goto fail;
Filipe Manana67addf22021-04-20 10:55:12 +0100629 }
630
631 free_extent_buffer(leaf);
632 leaf = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633
Yan, Zheng76dda932009-09-21 16:00:26 -0400634 key.offset = (u64)-1;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800635 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100636 if (IS_ERR(new_root)) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800637 free_anon_bdev(anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100638 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400639 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100640 goto fail;
641 }
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800642 /* Freeing will be done in btrfs_put_root() of new_root */
643 anon_dev = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -0400644
Josef Bacik221581e2021-03-12 15:25:06 -0500645 ret = btrfs_record_root_in_trans(trans, new_root);
646 if (ret) {
647 btrfs_put_root(new_root);
648 btrfs_abort_transaction(trans, ret);
649 goto fail;
650 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400651
Christian Brauner4d4340c2021-07-27 12:48:52 +0200652 ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns);
Josef Bacik00246522020-01-24 09:33:01 -0500653 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700654 if (ret) {
655 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400656 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700657 goto fail;
658 }
659
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660 /*
661 * insert the directory item
662 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200663 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100664 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400665 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100666 goto fail;
667 }
Chris Mason3de45862008-11-17 21:02:50 -0500668
Lu Fengqi684572d2018-08-04 21:10:57 +0800669 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500670 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400672 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400673 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100674 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500675
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200676 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200677 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikc7e54b52019-12-06 09:37:15 -0500678 if (ret) {
679 btrfs_abort_transaction(trans, ret);
680 goto fail;
681 }
Yan Zheng52c26172009-01-05 15:43:43 -0500682
Lu Fengqi6025c192018-08-01 11:32:29 +0800683 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200684 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500685 if (ret) {
686 btrfs_abort_transaction(trans, ret);
687 goto fail;
688 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500689
Lu Fengqicdb345a2018-05-29 15:01:53 +0800690 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400691 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200692 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400693 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200694
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400695fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100696 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000697 trans->block_rsv = NULL;
698 trans->bytes_reserved = 0;
Qu Wenruoe85fde52020-07-24 14:46:10 +0800699 btrfs_subvolume_release_metadata(root, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800700
Nikolay Borisov9babda92020-03-13 17:23:20 +0200701 err = btrfs_commit_transaction(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400702 if (err && !ret)
703 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500704
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900705 if (!ret) {
706 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800707 if (IS_ERR(inode))
708 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900709 d_instantiate(dentry, inode);
710 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400711 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100712
713fail_free:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800714 if (anon_dev)
715 free_anon_bdev(anon_dev);
David Sterba49a3c4d2016-03-24 17:49:22 +0100716 kfree(root_item);
717 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400718}
719
Miao Xiee9662f72013-02-28 10:01:15 +0000720static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200721 struct dentry *dentry, bool readonly,
Miao Xiee9662f72013-02-28 10:01:15 +0000722 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400724 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000725 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726 struct btrfs_pending_snapshot *pending_snapshot;
727 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000728 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800730 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731 return -EINVAL;
732
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700733 if (atomic_read(&root->nr_swapfiles)) {
734 btrfs_warn(fs_info,
735 "cannot snapshot subvolume with active swapfile");
736 return -ETXTBSY;
737 }
738
David Sterba23269bf2017-02-13 11:03:44 +0100739 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100740 if (!pending_snapshot)
741 return -ENOMEM;
742
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800743 ret = get_anon_bdev(&pending_snapshot->anon_dev);
744 if (ret < 0)
745 goto free_pending;
David Sterbab0c0ea62015-11-10 18:54:00 +0100746 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100747 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100748 pending_snapshot->path = btrfs_alloc_path();
749 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100750 ret = -ENOMEM;
751 goto free_pending;
752 }
753
Miao Xie66d8f3d2012-09-06 04:02:28 -0600754 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
755 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000756 /*
757 * 1 - parent dir inode
758 * 2 - dir entries
759 * 1 - root item
760 * 2 - root ref/backref
761 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200762 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000763 */
764 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200765 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400766 false);
Miao Xied5c12072013-02-28 10:04:33 +0000767 if (ret)
Robbie Koc11fbb62020-05-14 17:19:18 +0800768 goto free_pending;
Miao Xied5c12072013-02-28 10:04:33 +0000769
Yan, Zhenga22285a2010-05-16 10:48:46 -0400770 pending_snapshot->dentry = dentry;
771 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800772 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000773 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000774 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400775
Miao Xied5c12072013-02-28 10:04:33 +0000776 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400777 if (IS_ERR(trans)) {
778 ret = PTR_ERR(trans);
779 goto fail;
780 }
781
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400782 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400783 list_add(&pending_snapshot->list,
784 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400785 spin_unlock(&fs_info->trans_lock);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200786
787 ret = btrfs_commit_transaction(trans);
Miao Xieaec80302013-03-04 09:44:29 +0000788 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400789 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400790
791 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400792 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000793 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400794
Chris Masond3797302014-10-15 13:50:56 -0700795 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
796 if (ret)
797 goto fail;
798
David Howells2b0143b2015-03-17 22:25:59 +0000799 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000800 if (IS_ERR(inode)) {
801 ret = PTR_ERR(inode);
802 goto fail;
803 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900804
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000805 d_instantiate(dentry, inode);
806 ret = 0;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800807 pending_snapshot->anon_dev = 0;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000808fail:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800809 /* Prevent double freeing of anon_dev */
810 if (ret && pending_snapshot->snap)
811 pending_snapshot->snap->anon_dev = 0;
Josef Bacik00246522020-01-24 09:33:01 -0500812 btrfs_put_root(pending_snapshot->snap);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800813 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
David Sterbab0c0ea62015-11-10 18:54:00 +0100814free_pending:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800815 if (pending_snapshot->anon_dev)
816 free_anon_bdev(pending_snapshot->anon_dev);
David Sterbab0c0ea62015-11-10 18:54:00 +0100817 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100818 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100819 kfree(pending_snapshot);
820
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400821 return ret;
822}
823
Sage Weil4260f7c2010-10-29 15:46:43 -0400824/* copy of may_delete in fs/namei.c()
825 * Check whether we can remove a link victim from directory dir, check
826 * whether the type of victim is right.
827 * 1. We can't do it if dir is read-only (done in permission())
828 * 2. We should have write and exec permissions on dir
829 * 3. We can't remove anything from append-only dir
830 * 4. We can't do anything with immutable dir (done in permission())
831 * 5. If the sticky bit on dir is set we should either
832 * a. be owner of dir, or
833 * b. be owner of victim, or
834 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400835 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400836 * links pointing to it.
837 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
838 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
839 * 9. We can't remove a root or mountpoint.
840 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
841 * nfs_async_unlink().
842 */
843
Christian Braunerc4ed5332021-07-27 12:48:53 +0200844static int btrfs_may_delete(struct user_namespace *mnt_userns,
845 struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400846{
847 int error;
848
David Howells2b0143b2015-03-17 22:25:59 +0000849 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400850 return -ENOENT;
851
David Howells2b0143b2015-03-17 22:25:59 +0000852 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400853 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400854
Christian Braunerc4ed5332021-07-27 12:48:53 +0200855 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -0400856 if (error)
857 return error;
858 if (IS_APPEND(dir))
859 return -EPERM;
Christian Braunerc4ed5332021-07-27 12:48:53 +0200860 if (check_sticky(mnt_userns, dir, d_inode(victim)) ||
Christian Braunerba73d982021-01-21 14:19:31 +0100861 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
862 IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400863 return -EPERM;
864 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000865 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400866 return -ENOTDIR;
867 if (IS_ROOT(victim))
868 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000869 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400870 return -EISDIR;
871 if (IS_DEADDIR(dir))
872 return -ENOENT;
873 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
874 return -EBUSY;
875 return 0;
876}
877
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878/* copy of may_create in fs/namei.c() */
Christian Brauner4d4340c2021-07-27 12:48:52 +0200879static inline int btrfs_may_create(struct user_namespace *mnt_userns,
880 struct inode *dir, struct dentry *child)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400881{
David Howells2b0143b2015-03-17 22:25:59 +0000882 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400883 return -EEXIST;
884 if (IS_DEADDIR(dir))
885 return -ENOENT;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200886 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Christian Brauner5474bf42021-07-27 12:48:51 +0200887 return -EOVERFLOW;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200888 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400889}
890
891/*
892 * Create a new subvolume below @parent. This is largely modeled after
893 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
894 * inside this filesystem so it's quite a bit simpler.
895 */
Al Viro92872092016-11-20 19:34:31 -0500896static noinline int btrfs_mksubvol(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200897 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +0100898 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400899 struct btrfs_root *snap_src,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200900 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000901 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400902{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400903 struct inode *dir = d_inode(parent->dentry);
904 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400905 struct dentry *dentry;
906 int error;
907
Al Viro00235412016-05-26 00:05:12 -0400908 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
909 if (error == -EINTR)
910 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400911
Christian Brauner4d4340c2021-07-27 12:48:52 +0200912 dentry = lookup_one(mnt_userns, name, parent->dentry, namelen);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400913 error = PTR_ERR(dentry);
914 if (IS_ERR(dentry))
915 goto out_unlock;
916
Christian Brauner4d4340c2021-07-27 12:48:52 +0200917 error = btrfs_may_create(mnt_userns, dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400918 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600919 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400920
Chris Mason9c520572012-12-17 14:26:57 -0500921 /*
922 * even if this name doesn't exist, we may get hash collisions.
923 * check for them now when we can safely fail
924 */
925 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
926 dir->i_ino, name,
927 namelen);
928 if (error)
929 goto out_dput;
930
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400931 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400932
933 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
934 goto out_up_read;
935
Nikolay Borisov9babda92020-03-13 17:23:20 +0200936 if (snap_src)
937 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
938 else
Christian Brauner4d4340c2021-07-27 12:48:52 +0200939 error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200940
Yan, Zheng76dda932009-09-21 16:00:26 -0400941 if (!error)
942 fsnotify_mkdir(dir, dentry);
943out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400944 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400945out_dput:
946 dput(dentry);
947out_unlock:
Josef Bacik64708532021-02-10 17:14:34 -0500948 btrfs_inode_unlock(dir, 0);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400949 return error;
950}
951
Robbie Koc11fbb62020-05-14 17:19:18 +0800952static noinline int btrfs_mksnapshot(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200953 struct user_namespace *mnt_userns,
Robbie Koc11fbb62020-05-14 17:19:18 +0800954 const char *name, int namelen,
955 struct btrfs_root *root,
956 bool readonly,
957 struct btrfs_qgroup_inherit *inherit)
958{
959 int ret;
960 bool snapshot_force_cow = false;
961
962 /*
963 * Force new buffered writes to reserve space even when NOCOW is
964 * possible. This is to avoid later writeback (running dealloc) to
965 * fallback to COW mode and unexpectedly fail with ENOSPC.
966 */
967 btrfs_drew_read_lock(&root->snapshot_lock);
968
Filipe Mananaf9baa502021-04-22 12:08:05 +0100969 ret = btrfs_start_delalloc_snapshot(root, false);
Robbie Koc11fbb62020-05-14 17:19:18 +0800970 if (ret)
971 goto out;
972
973 /*
974 * All previous writes have started writeback in NOCOW mode, so now
975 * we force future writes to fallback to COW mode during snapshot
976 * creation.
977 */
978 atomic_inc(&root->snapshot_force_cow);
979 snapshot_force_cow = true;
980
981 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
982
Christian Brauner4d4340c2021-07-27 12:48:52 +0200983 ret = btrfs_mksubvol(parent, mnt_userns, name, namelen,
Robbie Koc11fbb62020-05-14 17:19:18 +0800984 root, readonly, inherit);
985out:
986 if (snapshot_force_cow)
987 atomic_dec(&root->snapshot_force_cow);
988 btrfs_drew_read_unlock(&root->snapshot_lock);
989 return ret;
990}
991
Qu Wenruoe9eec722021-08-06 16:12:38 +0800992static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,
993 bool locked)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400994{
995 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500996 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800997 struct extent_map *em;
Qu Wenruo76068ca2021-08-06 16:12:34 +0800998 const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize;
Chris Mason940100a2010-03-10 10:52:59 -0500999
1000 /*
1001 * hopefully we have this extent in the tree already, try without
1002 * the full extent lock
1003 */
1004 read_lock(&em_tree->lock);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001005 em = lookup_extent_mapping(em_tree, start, sectorsize);
Chris Mason940100a2010-03-10 10:52:59 -05001006 read_unlock(&em_tree->lock);
1007
1008 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001009 struct extent_state *cached = NULL;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001010 u64 end = start + sectorsize - 1;
Filipe Manana308d9802014-03-11 13:56:15 +00001011
Chris Mason940100a2010-03-10 10:52:59 -05001012 /* get the big lock and read metadata off disk */
Qu Wenruoe9eec722021-08-06 16:12:38 +08001013 if (!locked)
1014 lock_extent_bits(io_tree, start, end, &cached);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001015 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize);
Qu Wenruoe9eec722021-08-06 16:12:38 +08001016 if (!locked)
1017 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001018
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001019 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001020 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001021 }
1022
Li Zefan6c282eb2012-06-11 16:03:35 +08001023 return em;
1024}
1025
Qu Wenruoe9eec722021-08-06 16:12:38 +08001026static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
1027 bool locked)
Li Zefan6c282eb2012-06-11 16:03:35 +08001028{
1029 struct extent_map *next;
1030 bool ret = true;
1031
1032 /* this is the last extent */
1033 if (em->start + em->len >= i_size_read(inode))
1034 return false;
1035
Qu Wenruoe9eec722021-08-06 16:12:38 +08001036 next = defrag_lookup_extent(inode, em->start + em->len, locked);
Chris Masone9512d72014-08-26 13:55:54 -07001037 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1038 ret = false;
1039 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001040 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001041 ret = false;
1042
1043 free_extent_map(next);
1044 return ret;
1045}
1046
Chris Mason4cb53002011-05-24 15:35:30 -04001047/*
Qu Wenruo5767b502021-08-06 16:12:35 +08001048 * Prepare one page to be defragged.
1049 *
1050 * This will ensure:
1051 *
1052 * - Returned page is locked and has been set up properly.
1053 * - No ordered extent exists in the page.
1054 * - The page is uptodate.
1055 *
1056 * NOTE: Caller should also wait for page writeback after the cluster is
1057 * prepared, here we don't do writeback wait for each page.
1058 */
1059static struct page *defrag_prepare_one_page(struct btrfs_inode *inode,
1060 pgoff_t index)
1061{
1062 struct address_space *mapping = inode->vfs_inode.i_mapping;
1063 gfp_t mask = btrfs_alloc_write_mask(mapping);
1064 u64 page_start = (u64)index << PAGE_SHIFT;
1065 u64 page_end = page_start + PAGE_SIZE - 1;
1066 struct extent_state *cached_state = NULL;
1067 struct page *page;
1068 int ret;
1069
1070again:
1071 page = find_or_create_page(mapping, index, mask);
1072 if (!page)
1073 return ERR_PTR(-ENOMEM);
1074
Omar Sandoval24bcb452021-10-19 20:35:01 -07001075 /*
1076 * Since we can defragment files opened read-only, we can encounter
1077 * transparent huge pages here (see CONFIG_READ_ONLY_THP_FOR_FS). We
1078 * can't do I/O using huge pages yet, so return an error for now.
1079 * Filesystem transparent huge pages are typically only used for
1080 * executables that explicitly enable them, so this isn't very
1081 * restrictive.
1082 */
1083 if (PageCompound(page)) {
1084 unlock_page(page);
1085 put_page(page);
1086 return ERR_PTR(-ETXTBSY);
1087 }
1088
Qu Wenruo5767b502021-08-06 16:12:35 +08001089 ret = set_page_extent_mapped(page);
1090 if (ret < 0) {
1091 unlock_page(page);
1092 put_page(page);
1093 return ERR_PTR(ret);
1094 }
1095
1096 /* Wait for any existing ordered extent in the range */
1097 while (1) {
1098 struct btrfs_ordered_extent *ordered;
1099
1100 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
1101 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
1102 unlock_extent_cached(&inode->io_tree, page_start, page_end,
1103 &cached_state);
1104 if (!ordered)
1105 break;
1106
1107 unlock_page(page);
1108 btrfs_start_ordered_extent(ordered, 1);
1109 btrfs_put_ordered_extent(ordered);
1110 lock_page(page);
1111 /*
1112 * We unlocked the page above, so we need check if it was
1113 * released or not.
1114 */
1115 if (page->mapping != mapping || !PagePrivate(page)) {
1116 unlock_page(page);
1117 put_page(page);
1118 goto again;
1119 }
1120 }
1121
1122 /*
1123 * Now the page range has no ordered extent any more. Read the page to
1124 * make it uptodate.
1125 */
1126 if (!PageUptodate(page)) {
1127 btrfs_readpage(NULL, page);
1128 lock_page(page);
1129 if (page->mapping != mapping || !PagePrivate(page)) {
1130 unlock_page(page);
1131 put_page(page);
1132 goto again;
1133 }
1134 if (!PageUptodate(page)) {
1135 unlock_page(page);
1136 put_page(page);
1137 return ERR_PTR(-EIO);
1138 }
1139 }
1140 return page;
1141}
1142
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001143struct defrag_target_range {
1144 struct list_head list;
1145 u64 start;
1146 u64 len;
1147};
1148
1149/*
1150 * Collect all valid target extents.
1151 *
1152 * @start: file offset to lookup
1153 * @len: length to lookup
1154 * @extent_thresh: file extent size threshold, any extent size >= this value
1155 * will be ignored
1156 * @newer_than: only defrag extents newer than this value
1157 * @do_compress: whether the defrag is doing compression
1158 * if true, @extent_thresh will be ignored and all regular
1159 * file extents meeting @newer_than will be targets.
Qu Wenruoe9eec722021-08-06 16:12:38 +08001160 * @locked: if the range has already held extent lock
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001161 * @target_list: list of targets file extents
1162 */
1163static int defrag_collect_targets(struct btrfs_inode *inode,
1164 u64 start, u64 len, u32 extent_thresh,
1165 u64 newer_than, bool do_compress,
Qu Wenruoe9eec722021-08-06 16:12:38 +08001166 bool locked, struct list_head *target_list)
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001167{
1168 u64 cur = start;
1169 int ret = 0;
1170
1171 while (cur < start + len) {
1172 struct extent_map *em;
1173 struct defrag_target_range *new;
1174 bool next_mergeable = true;
1175 u64 range_len;
1176
Qu Wenruoe9eec722021-08-06 16:12:38 +08001177 em = defrag_lookup_extent(&inode->vfs_inode, cur, locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001178 if (!em)
1179 break;
1180
1181 /* Skip hole/inline/preallocated extents */
1182 if (em->block_start >= EXTENT_MAP_LAST_BYTE ||
1183 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1184 goto next;
1185
1186 /* Skip older extent */
1187 if (em->generation < newer_than)
1188 goto next;
1189
1190 /*
1191 * For do_compress case, we want to compress all valid file
1192 * extents, thus no @extent_thresh or mergeable check.
1193 */
1194 if (do_compress)
1195 goto add;
1196
1197 /* Skip too large extent */
1198 if (em->len >= extent_thresh)
1199 goto next;
1200
Qu Wenruoe9eec722021-08-06 16:12:38 +08001201 next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
1202 locked);
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001203 if (!next_mergeable) {
1204 struct defrag_target_range *last;
1205
1206 /* Empty target list, no way to merge with last entry */
1207 if (list_empty(target_list))
1208 goto next;
1209 last = list_entry(target_list->prev,
1210 struct defrag_target_range, list);
1211 /* Not mergeable with last entry */
1212 if (last->start + last->len != cur)
1213 goto next;
1214
1215 /* Mergeable, fall through to add it to @target_list. */
1216 }
1217
1218add:
1219 range_len = min(extent_map_end(em), start + len) - cur;
1220 /*
1221 * This one is a good target, check if it can be merged into
1222 * last range of the target list.
1223 */
1224 if (!list_empty(target_list)) {
1225 struct defrag_target_range *last;
1226
1227 last = list_entry(target_list->prev,
1228 struct defrag_target_range, list);
1229 ASSERT(last->start + last->len <= cur);
1230 if (last->start + last->len == cur) {
1231 /* Mergeable, enlarge the last entry */
1232 last->len += range_len;
1233 goto next;
1234 }
1235 /* Fall through to allocate a new entry */
1236 }
1237
1238 /* Allocate new defrag_target_range */
1239 new = kmalloc(sizeof(*new), GFP_NOFS);
1240 if (!new) {
1241 free_extent_map(em);
1242 ret = -ENOMEM;
1243 break;
1244 }
1245 new->start = cur;
1246 new->len = range_len;
1247 list_add_tail(&new->list, target_list);
1248
1249next:
1250 cur = extent_map_end(em);
1251 free_extent_map(em);
1252 }
1253 if (ret < 0) {
1254 struct defrag_target_range *entry;
1255 struct defrag_target_range *tmp;
1256
1257 list_for_each_entry_safe(entry, tmp, target_list, list) {
1258 list_del_init(&entry->list);
1259 kfree(entry);
1260 }
1261 }
1262 return ret;
1263}
1264
Qu Wenruo22b398e2021-08-06 16:12:37 +08001265#define CLUSTER_SIZE (SZ_256K)
1266
1267/*
1268 * Defrag one contiguous target range.
1269 *
1270 * @inode: target inode
1271 * @target: target range to defrag
1272 * @pages: locked pages covering the defrag range
1273 * @nr_pages: number of locked pages
1274 *
1275 * Caller should ensure:
1276 *
1277 * - Pages are prepared
1278 * Pages should be locked, no ordered extent in the pages range,
1279 * no writeback.
1280 *
1281 * - Extent bits are locked
1282 */
1283static int defrag_one_locked_target(struct btrfs_inode *inode,
1284 struct defrag_target_range *target,
1285 struct page **pages, int nr_pages,
1286 struct extent_state **cached_state)
1287{
1288 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1289 struct extent_changeset *data_reserved = NULL;
1290 const u64 start = target->start;
1291 const u64 len = target->len;
1292 unsigned long last_index = (start + len - 1) >> PAGE_SHIFT;
1293 unsigned long start_index = start >> PAGE_SHIFT;
1294 unsigned long first_index = page_index(pages[0]);
1295 int ret = 0;
1296 int i;
1297
1298 ASSERT(last_index - first_index + 1 <= nr_pages);
1299
1300 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len);
1301 if (ret < 0)
1302 return ret;
1303 clear_extent_bit(&inode->io_tree, start, start + len - 1,
1304 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1305 EXTENT_DEFRAG, 0, 0, cached_state);
1306 set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state);
1307
1308 /* Update the page status */
1309 for (i = start_index - first_index; i <= last_index - first_index; i++) {
1310 ClearPageChecked(pages[i]);
1311 btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len);
1312 }
1313 btrfs_delalloc_release_extents(inode, len);
1314 extent_changeset_free(data_reserved);
1315
1316 return ret;
1317}
1318
Qu Wenruoe9eec722021-08-06 16:12:38 +08001319static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,
1320 u32 extent_thresh, u64 newer_than, bool do_compress)
1321{
1322 struct extent_state *cached_state = NULL;
1323 struct defrag_target_range *entry;
1324 struct defrag_target_range *tmp;
1325 LIST_HEAD(target_list);
1326 struct page **pages;
1327 const u32 sectorsize = inode->root->fs_info->sectorsize;
1328 u64 last_index = (start + len - 1) >> PAGE_SHIFT;
1329 u64 start_index = start >> PAGE_SHIFT;
1330 unsigned int nr_pages = last_index - start_index + 1;
1331 int ret = 0;
1332 int i;
1333
1334 ASSERT(nr_pages <= CLUSTER_SIZE / PAGE_SIZE);
1335 ASSERT(IS_ALIGNED(start, sectorsize) && IS_ALIGNED(len, sectorsize));
1336
1337 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
1338 if (!pages)
1339 return -ENOMEM;
1340
1341 /* Prepare all pages */
1342 for (i = 0; i < nr_pages; i++) {
1343 pages[i] = defrag_prepare_one_page(inode, start_index + i);
1344 if (IS_ERR(pages[i])) {
1345 ret = PTR_ERR(pages[i]);
1346 pages[i] = NULL;
1347 goto free_pages;
1348 }
1349 }
1350 for (i = 0; i < nr_pages; i++)
1351 wait_on_page_writeback(pages[i]);
1352
1353 /* Lock the pages range */
1354 lock_extent_bits(&inode->io_tree, start_index << PAGE_SHIFT,
1355 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1356 &cached_state);
1357 /*
1358 * Now we have a consistent view about the extent map, re-check
1359 * which range really needs to be defragged.
1360 *
1361 * And this time we have extent locked already, pass @locked = true
1362 * so that we won't relock the extent range and cause deadlock.
1363 */
1364 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1365 newer_than, do_compress, true,
1366 &target_list);
1367 if (ret < 0)
1368 goto unlock_extent;
1369
1370 list_for_each_entry(entry, &target_list, list) {
1371 ret = defrag_one_locked_target(inode, entry, pages, nr_pages,
1372 &cached_state);
1373 if (ret < 0)
1374 break;
1375 }
1376
1377 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1378 list_del_init(&entry->list);
1379 kfree(entry);
1380 }
1381unlock_extent:
1382 unlock_extent_cached(&inode->io_tree, start_index << PAGE_SHIFT,
1383 (last_index << PAGE_SHIFT) + PAGE_SIZE - 1,
1384 &cached_state);
1385free_pages:
1386 for (i = 0; i < nr_pages; i++) {
1387 if (pages[i]) {
1388 unlock_page(pages[i]);
1389 put_page(pages[i]);
1390 }
1391 }
1392 kfree(pages);
1393 return ret;
1394}
1395
Qu Wenruob18c3ab2021-08-06 16:12:39 +08001396static int defrag_one_cluster(struct btrfs_inode *inode,
1397 struct file_ra_state *ra,
1398 u64 start, u32 len, u32 extent_thresh,
1399 u64 newer_than, bool do_compress,
1400 unsigned long *sectors_defragged,
1401 unsigned long max_sectors)
1402{
1403 const u32 sectorsize = inode->root->fs_info->sectorsize;
1404 struct defrag_target_range *entry;
1405 struct defrag_target_range *tmp;
1406 LIST_HEAD(target_list);
1407 int ret;
1408
1409 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
1410 ret = defrag_collect_targets(inode, start, len, extent_thresh,
1411 newer_than, do_compress, false,
1412 &target_list);
1413 if (ret < 0)
1414 goto out;
1415
1416 list_for_each_entry(entry, &target_list, list) {
1417 u32 range_len = entry->len;
1418
1419 /* Reached the limit */
1420 if (max_sectors && max_sectors == *sectors_defragged)
1421 break;
1422
1423 if (max_sectors)
1424 range_len = min_t(u32, range_len,
1425 (max_sectors - *sectors_defragged) * sectorsize);
1426
1427 if (ra)
1428 page_cache_sync_readahead(inode->vfs_inode.i_mapping,
1429 ra, NULL, entry->start >> PAGE_SHIFT,
1430 ((entry->start + range_len - 1) >> PAGE_SHIFT) -
1431 (entry->start >> PAGE_SHIFT) + 1);
1432 /*
1433 * Here we may not defrag any range if holes are punched before
1434 * we locked the pages.
1435 * But that's fine, it only affects the @sectors_defragged
1436 * accounting.
1437 */
1438 ret = defrag_one_range(inode, entry->start, range_len,
1439 extent_thresh, newer_than, do_compress);
1440 if (ret < 0)
1441 break;
1442 *sectors_defragged += range_len;
1443 }
1444out:
1445 list_for_each_entry_safe(entry, tmp, &target_list, list) {
1446 list_del_init(&entry->list);
1447 kfree(entry);
1448 }
1449 return ret;
1450}
1451
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001452/*
1453 * Entry point to file defragmentation.
1454 *
1455 * @inode: inode to be defragged
1456 * @ra: readahead state (can be NUL)
1457 * @range: defrag options including range and flags
1458 * @newer_than: minimum transid to defrag
1459 * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode
1460 * will be defragged.
1461 */
1462int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
Chris Mason4cb53002011-05-24 15:35:30 -04001463 struct btrfs_ioctl_defrag_range_args *range,
1464 u64 newer_than, unsigned long max_to_defrag)
1465{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001466 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Qu Wenruo7b508032021-05-27 20:33:22 +08001467 unsigned long sectors_defragged = 0;
Li Zefan151a31b2011-09-02 15:56:39 +08001468 u64 isize = i_size_read(inode);
Qu Wenruo7b508032021-05-27 20:33:22 +08001469 u64 cur;
1470 u64 last_byte;
David Sterba1e2ef462017-07-17 20:01:59 +02001471 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001472 bool ra_allocated = false;
Qu Wenruo7b508032021-05-27 20:33:22 +08001473 int compress_type = BTRFS_COMPRESS_ZLIB;
1474 int ret = 0;
1475 u32 extent_thresh = range->extent_thresh;
Chris Mason4cb53002011-05-24 15:35:30 -04001476
Liu Bo0abd5b12013-04-16 09:20:28 +00001477 if (isize == 0)
1478 return 0;
1479
1480 if (range->start >= isize)
1481 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001482
David Sterba1e2ef462017-07-17 20:01:59 +02001483 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001484 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001485 return -EINVAL;
1486 if (range->compress_type)
1487 compress_type = range->compress_type;
1488 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489
Liu Bo0abd5b12013-04-16 09:20:28 +00001490 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001491 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001492
Qu Wenruo7b508032021-05-27 20:33:22 +08001493 if (range->start + range->len > range->start) {
1494 /* Got a specific range */
1495 last_byte = min(isize, range->start + range->len) - 1;
1496 } else {
1497 /* Defrag until file end */
1498 last_byte = isize - 1;
1499 }
1500
Chris Mason4cb53002011-05-24 15:35:30 -04001501 /*
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001502 * If we were not given a ra, allocate a readahead context. As
David Sterba0a52d102017-06-22 03:22:58 +02001503 * readahead is just an optimization, defrag will work without it so
1504 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001505 */
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001506 if (!ra) {
1507 ra_allocated = true;
David Sterba63e727e2017-06-22 03:13:02 +02001508 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001509 if (ra)
1510 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001511 }
1512
Qu Wenruo7b508032021-05-27 20:33:22 +08001513 /* Align the range */
1514 cur = round_down(range->start, fs_info->sectorsize);
1515 last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001516
Qu Wenruo7b508032021-05-27 20:33:22 +08001517 while (cur < last_byte) {
1518 u64 cluster_end;
Chris Mason1e701a32010-03-11 09:42:04 -05001519
Qu Wenruo7b508032021-05-27 20:33:22 +08001520 /* The cluster size 256K should always be page aligned */
1521 BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
Chris Mason4cb53002011-05-24 15:35:30 -04001522
Qu Wenruo7b508032021-05-27 20:33:22 +08001523 /* We want the cluster end at page boundary when possible */
1524 cluster_end = (((cur >> PAGE_SHIFT) +
1525 (SZ_256K >> PAGE_SHIFT)) << PAGE_SHIFT) - 1;
1526 cluster_end = min(cluster_end, last_byte);
Chris Mason940100a2010-03-10 10:52:59 -05001527
Josef Bacik64708532021-02-10 17:14:34 -05001528 btrfs_inode_lock(inode, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001529 if (IS_SWAPFILE(inode)) {
1530 ret = -ETXTBSY;
Josef Bacik64708532021-02-10 17:14:34 -05001531 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001532 break;
Liu Boecb8bea2012-03-29 09:57:44 -04001533 }
Qu Wenruo7b508032021-05-27 20:33:22 +08001534 if (!(inode->i_sb->s_flags & SB_ACTIVE)) {
1535 btrfs_inode_unlock(inode, 0);
1536 break;
1537 }
1538 if (do_compress)
1539 BTRFS_I(inode)->defrag_compress = compress_type;
1540 ret = defrag_one_cluster(BTRFS_I(inode), ra, cur,
1541 cluster_end + 1 - cur, extent_thresh,
1542 newer_than, do_compress,
1543 &sectors_defragged, max_to_defrag);
Josef Bacik64708532021-02-10 17:14:34 -05001544 btrfs_inode_unlock(inode, 0);
Qu Wenruo7b508032021-05-27 20:33:22 +08001545 if (ret < 0)
1546 break;
1547 cur = cluster_end + 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001548 }
1549
Qu Wenruo7b508032021-05-27 20:33:22 +08001550 if (ra_allocated)
1551 kfree(ra);
1552 if (sectors_defragged) {
1553 /*
1554 * We have defragged some sectors, for compression case they
1555 * need to be written back immediately.
1556 */
1557 if (range->flags & BTRFS_DEFRAG_RANGE_START_IO) {
Filipe Mananadec8ef92014-03-01 10:55:54 +00001558 filemap_flush(inode->i_mapping);
Qu Wenruo7b508032021-05-27 20:33:22 +08001559 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1560 &BTRFS_I(inode)->runtime_flags))
1561 filemap_flush(inode->i_mapping);
1562 }
1563 if (range->compress_type == BTRFS_COMPRESS_LZO)
1564 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1565 else if (range->compress_type == BTRFS_COMPRESS_ZSTD)
1566 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1567 ret = sectors_defragged;
Filipe Mananadec8ef92014-03-01 10:55:54 +00001568 }
David Sterba1e2ef462017-07-17 20:01:59 +02001569 if (do_compress) {
Josef Bacik64708532021-02-10 17:14:34 -05001570 btrfs_inode_lock(inode, 0);
David Sterbaeec63c62017-07-17 19:41:31 +02001571 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Josef Bacik64708532021-02-10 17:14:34 -05001572 btrfs_inode_unlock(inode, 0);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001573 }
Chris Mason940100a2010-03-10 10:52:59 -05001574 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575}
1576
David Sterba17aaa432021-05-14 21:32:44 +02001577/*
1578 * Try to start exclusive operation @type or cancel it if it's running.
1579 *
1580 * Return:
1581 * 0 - normal mode, newly claimed op started
1582 * >0 - normal mode, something else is running,
1583 * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
1584 * ECANCELED - cancel mode, successful cancel
1585 * ENOTCONN - cancel mode, operation not running anymore
1586 */
1587static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
1588 enum btrfs_exclusive_operation type, bool cancel)
1589{
1590 if (!cancel) {
1591 /* Start normal op */
1592 if (!btrfs_exclop_start(fs_info, type))
1593 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1594 /* Exclusive operation is now claimed */
1595 return 0;
1596 }
1597
1598 /* Cancel running op */
1599 if (btrfs_exclop_start_try_lock(fs_info, type)) {
1600 /*
1601 * This blocks any exclop finish from setting it to NONE, so we
1602 * request cancellation. Either it runs and we will wait for it,
1603 * or it has finished and no waiting will happen.
1604 */
1605 atomic_inc(&fs_info->reloc_cancel_req);
1606 btrfs_exclop_start_unlock(fs_info);
1607
1608 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
1609 wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
1610 TASK_INTERRUPTIBLE);
1611
1612 return -ECANCELED;
1613 }
1614
1615 /* Something else is running or none */
1616 return -ENOTCONN;
1617}
1618
Miao Xie198605a2012-11-26 08:43:45 +00001619static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001620 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001621{
Josef Bacik562d7b12021-10-05 16:12:42 -04001622 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001623 struct inode *inode = file_inode(file);
1624 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001625 u64 new_size;
1626 u64 old_size;
1627 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001628 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001629 struct btrfs_ioctl_vol_args *vol_args;
1630 struct btrfs_trans_handle *trans;
1631 struct btrfs_device *device = NULL;
1632 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001633 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001634 char *devstr = NULL;
1635 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 int mod = 0;
David Sterbabb059a32021-05-18 21:12:33 +02001637 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638
Chris Masone441d542009-01-05 16:57:23 -05001639 if (!capable(CAP_SYS_ADMIN))
1640 return -EPERM;
1641
Miao Xie198605a2012-11-26 08:43:45 +00001642 ret = mnt_want_write_file(file);
1643 if (ret)
1644 return ret;
1645
David Sterbabb059a32021-05-18 21:12:33 +02001646 /*
1647 * Read the arguments before checking exclusivity to be able to
1648 * distinguish regular resize and cancel
1649 */
Li Zefandae7b662009-04-08 15:06:54 +08001650 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001651 if (IS_ERR(vol_args)) {
1652 ret = PTR_ERR(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001653 goto out_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001654 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001655 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656 sizestr = vol_args->name;
David Sterbabb059a32021-05-18 21:12:33 +02001657 cancel = (strcmp("cancel", sizestr) == 0);
1658 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
1659 if (ret)
1660 goto out_free;
1661 /* Exclusive operation is now claimed */
1662
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001663 devstr = strchr(sizestr, ':');
1664 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665 sizestr = devstr + 1;
1666 *devstr = '\0';
1667 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001668 ret = kstrtoull(devstr, 10, &devid);
1669 if (ret)
David Sterbabb059a32021-05-18 21:12:33 +02001670 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001671 if (!devid) {
1672 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001673 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001674 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001675 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001676 }
Miao Xiedba60f32012-12-21 09:19:51 +00001677
Josef Bacik562d7b12021-10-05 16:12:42 -04001678 args.devid = devid;
1679 device = btrfs_find_device(fs_info->fs_devices, &args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001681 btrfs_info(fs_info, "resizer unable to find device %llu",
1682 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001683 ret = -ENODEV;
David Sterbabb059a32021-05-18 21:12:33 +02001684 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001685 }
Miao Xiedba60f32012-12-21 09:19:51 +00001686
Anand Jainebbede42017-12-04 12:54:52 +08001687 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001688 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001689 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001690 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001691 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001692 goto out_finish;
Liu Bo4e42ae12012-06-14 02:23:19 -06001693 }
1694
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001695 if (!strcmp(sizestr, "max"))
1696 new_size = device->bdev->bd_inode->i_size;
1697 else {
1698 if (sizestr[0] == '-') {
1699 mod = -1;
1700 sizestr++;
1701 } else if (sizestr[0] == '+') {
1702 mod = 1;
1703 sizestr++;
1704 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001705 new_size = memparse(sizestr, &retptr);
1706 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001708 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 }
1710 }
1711
Anand Jain401e29c2017-12-04 12:54:55 +08001712 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001713 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001714 goto out_finish;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001715 }
1716
Miao Xie7cc8e582014-09-03 21:35:38 +08001717 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718
1719 if (mod < 0) {
1720 if (new_size > old_size) {
1721 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001722 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723 }
1724 new_size = old_size - new_size;
1725 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001726 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001727 ret = -ERANGE;
David Sterbabb059a32021-05-18 21:12:33 +02001728 goto out_finish;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001729 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 new_size = old_size + new_size;
1731 }
1732
Byongho Leeee221842015-12-15 01:42:10 +09001733 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001735 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736 }
1737 if (new_size > device->bdev->bd_inode->i_size) {
1738 ret = -EFBIG;
David Sterbabb059a32021-05-18 21:12:33 +02001739 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740 }
1741
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001742 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001743
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001744 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001745 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001746 if (IS_ERR(trans)) {
1747 ret = PTR_ERR(trans);
David Sterbabb059a32021-05-18 21:12:33 +02001748 goto out_finish;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001749 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001751 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001752 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001753 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001754 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001755
Marcos Paulo de Souzafaf8f7b2020-02-11 10:55:26 -03001756 if (ret == 0 && new_size != old_size)
1757 btrfs_info_in_rcu(fs_info,
1758 "resize device %s (devid %llu) from %llu to %llu",
1759 rcu_str_deref(device->name), device->devid,
1760 old_size, new_size);
David Sterbabb059a32021-05-18 21:12:33 +02001761out_finish:
1762 btrfs_exclop_finish(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001763out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 kfree(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001765out_drop:
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001766 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001767 return ret;
1768}
1769
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001770static noinline int __btrfs_ioctl_snap_create(struct file *file,
Christian Brauner4d4340c2021-07-27 12:48:52 +02001771 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +01001772 const char *name, unsigned long fd, int subvol,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02001773 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001774 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001776 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001777 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001778
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001779 if (!S_ISDIR(file_inode(file)->i_mode))
1780 return -ENOTDIR;
1781
Liu Boa874a632012-06-29 03:58:46 -06001782 ret = mnt_want_write_file(file);
1783 if (ret)
1784 goto out;
1785
Sage Weil72fd0322010-10-29 15:41:32 -04001786 namelen = strlen(name);
1787 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001788 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001789 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001790 }
1791
Chris Mason16780ca2012-02-20 22:14:55 -05001792 if (name[0] == '.' &&
1793 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1794 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001795 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001796 }
1797
Chris Mason3de45862008-11-17 21:02:50 -05001798 if (subvol) {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001799 ret = btrfs_mksubvol(&file->f_path, mnt_userns, name,
1800 namelen, NULL, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001801 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001802 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001803 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001804 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001805 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001806 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001807 }
1808
Al Viro496ad9a2013-01-23 17:07:38 -05001809 src_inode = file_inode(src.file);
1810 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04001811 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001812 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001813 ret = -EXDEV;
Christian Brauner4d4340c2021-07-27 12:48:52 +02001814 } else if (!inode_owner_or_capable(mnt_userns, src_inode)) {
David Sterbad0242062014-01-15 18:15:52 +01001815 /*
1816 * Subvolume creation is not restricted, but snapshots
1817 * are limited to own subvolumes only
1818 */
1819 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001820 } else {
Christian Brauner4d4340c2021-07-27 12:48:52 +02001821 ret = btrfs_mksnapshot(&file->f_path, mnt_userns,
1822 name, namelen,
1823 BTRFS_I(src_inode)->root,
1824 readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001825 }
Al Viro2903ff02012-08-28 12:52:22 -04001826 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001827 }
Liu Boa874a632012-06-29 03:58:46 -06001828out_drop_write:
1829 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001830out:
Sage Weil72fd0322010-10-29 15:41:32 -04001831 return ret;
1832}
1833
1834static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001835 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001836{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001837 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001838 int ret;
1839
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001840 if (!S_ISDIR(file_inode(file)->i_mode))
1841 return -ENOTDIR;
1842
Li Zefanfa0d2b92010-12-20 15:53:28 +08001843 vol_args = memdup_user(arg, sizeof(*vol_args));
1844 if (IS_ERR(vol_args))
1845 return PTR_ERR(vol_args);
1846 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001847
Christian Brauner4d4340c2021-07-27 12:48:52 +02001848 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1849 vol_args->name, vol_args->fd, subvol,
1850 false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001851
Li Zefanfa0d2b92010-12-20 15:53:28 +08001852 kfree(vol_args);
1853 return ret;
1854}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001855
Li Zefanfa0d2b92010-12-20 15:53:28 +08001856static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1857 void __user *arg, int subvol)
1858{
1859 struct btrfs_ioctl_vol_args_v2 *vol_args;
1860 int ret;
Li Zefanb83cc962010-12-20 16:04:08 +08001861 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001862 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001863
Jeff Mahoney325c50e2016-09-21 08:31:29 -04001864 if (!S_ISDIR(file_inode(file)->i_mode))
1865 return -ENOTDIR;
1866
Li Zefanfa0d2b92010-12-20 15:53:28 +08001867 vol_args = memdup_user(arg, sizeof(*vol_args));
1868 if (IS_ERR(vol_args))
1869 return PTR_ERR(vol_args);
1870 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001871
David Sterba673990d2020-02-21 13:24:37 +01001872 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08001873 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001874 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001875 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001876
Li Zefanb83cc962010-12-20 16:04:08 +08001877 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1878 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001879 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001880 u64 nums;
1881
1882 if (vol_args->size < sizeof(*inherit) ||
1883 vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001884 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001885 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001886 }
1887 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1888 if (IS_ERR(inherit)) {
1889 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001890 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001891 }
Dan Carpenter5011c5a2021-02-17 09:04:34 +03001892
1893 if (inherit->num_qgroups > PAGE_SIZE ||
1894 inherit->num_ref_copies > PAGE_SIZE ||
1895 inherit->num_excl_copies > PAGE_SIZE) {
1896 ret = -EINVAL;
1897 goto free_inherit;
1898 }
1899
1900 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1901 2 * inherit->num_excl_copies;
1902 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1903 ret = -EINVAL;
1904 goto free_inherit;
1905 }
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001906 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001907
Christian Brauner4d4340c2021-07-27 12:48:52 +02001908 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
1909 vol_args->name, vol_args->fd, subvol,
1910 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001911 if (ret)
1912 goto free_inherit;
Dan Carpenterc47ca322014-09-04 14:09:15 +03001913free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001914 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03001915free_args:
1916 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001917 return ret;
1918}
1919
Li Zefan0caa1022010-12-20 16:30:25 +08001920static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1921 void __user *arg)
1922{
Al Viro496ad9a2013-01-23 17:07:38 -05001923 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001924 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001925 struct btrfs_root *root = BTRFS_I(inode)->root;
1926 int ret = 0;
1927 u64 flags = 0;
1928
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001929 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001930 return -EINVAL;
1931
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001932 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001933 if (btrfs_root_readonly(root))
1934 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001935 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001936
1937 if (copy_to_user(arg, &flags, sizeof(flags)))
1938 ret = -EFAULT;
1939
1940 return ret;
1941}
1942
1943static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1944 void __user *arg)
1945{
Al Viro496ad9a2013-01-23 17:07:38 -05001946 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08001948 struct btrfs_root *root = BTRFS_I(inode)->root;
1949 struct btrfs_trans_handle *trans;
1950 u64 root_flags;
1951 u64 flags;
1952 int ret = 0;
1953
Christian Brauner39e16742021-07-27 12:48:56 +02001954 if (!inode_owner_or_capable(file_mnt_user_ns(file), inode))
David Sterbabd60ea02014-01-16 15:50:22 +01001955 return -EPERM;
1956
Liu Bob9ca0662012-06-29 03:58:49 -06001957 ret = mnt_want_write_file(file);
1958 if (ret)
1959 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001960
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001961 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06001962 ret = -EINVAL;
1963 goto out_drop_write;
1964 }
Li Zefan0caa1022010-12-20 16:30:25 +08001965
Liu Bob9ca0662012-06-29 03:58:49 -06001966 if (copy_from_user(&flags, arg, sizeof(flags))) {
1967 ret = -EFAULT;
1968 goto out_drop_write;
1969 }
Li Zefan0caa1022010-12-20 16:30:25 +08001970
Liu Bob9ca0662012-06-29 03:58:49 -06001971 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1972 ret = -EOPNOTSUPP;
1973 goto out_drop_write;
1974 }
Li Zefan0caa1022010-12-20 16:30:25 +08001975
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001976 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08001977
1978 /* nothing to do */
1979 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001980 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001981
1982 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001983 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001984 btrfs_set_root_flags(&root->root_item,
1985 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001986 } else {
1987 /*
1988 * Block RO -> RW transition if this subvolume is involved in
1989 * send
1990 */
1991 spin_lock(&root->root_item_lock);
1992 if (root->send_in_progress == 0) {
1993 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001994 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001995 spin_unlock(&root->root_item_lock);
1996 } else {
1997 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 btrfs_warn(fs_info,
1999 "Attempt to set subvolume %llu read-write during send",
2000 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01002001 ret = -EPERM;
2002 goto out_drop_sem;
2003 }
2004 }
Li Zefan0caa1022010-12-20 16:30:25 +08002005
2006 trans = btrfs_start_transaction(root, 1);
2007 if (IS_ERR(trans)) {
2008 ret = PTR_ERR(trans);
2009 goto out_reset;
2010 }
2011
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002012 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08002013 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002014 if (ret < 0) {
2015 btrfs_end_transaction(trans);
2016 goto out_reset;
2017 }
Li Zefan0caa1022010-12-20 16:30:25 +08002018
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002019 ret = btrfs_commit_transaction(trans);
2020
Li Zefan0caa1022010-12-20 16:30:25 +08002021out_reset:
2022 if (ret)
2023 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06002024out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002025 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06002026out_drop_write:
2027 mnt_drop_write_file(file);
2028out:
Li Zefan0caa1022010-12-20 16:30:25 +08002029 return ret;
2030}
2031
Chris Masonac8e9812010-02-28 15:39:26 -05002032static noinline int key_in_sk(struct btrfs_key *key,
2033 struct btrfs_ioctl_search_key *sk)
2034{
Chris Masonabc6e132010-03-18 12:10:08 -04002035 struct btrfs_key test;
2036 int ret;
2037
2038 test.objectid = sk->min_objectid;
2039 test.type = sk->min_type;
2040 test.offset = sk->min_offset;
2041
2042 ret = btrfs_comp_cpu_keys(key, &test);
2043 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002044 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002045
2046 test.objectid = sk->max_objectid;
2047 test.type = sk->max_type;
2048 test.offset = sk->max_offset;
2049
2050 ret = btrfs_comp_cpu_keys(key, &test);
2051 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002052 return 0;
2053 return 1;
2054}
2055
Jeff Mahoneydf397562016-06-21 20:18:21 -04002056static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002057 struct btrfs_key *key,
2058 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002059 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002060 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002061 unsigned long *sk_offset,
2062 int *num_found)
2063{
2064 u64 found_transid;
2065 struct extent_buffer *leaf;
2066 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002067 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002068 unsigned long item_off;
2069 unsigned long item_len;
2070 int nritems;
2071 int i;
2072 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002073 int ret = 0;
2074
2075 leaf = path->nodes[0];
2076 slot = path->slots[0];
2077 nritems = btrfs_header_nritems(leaf);
2078
2079 if (btrfs_header_generation(leaf) > sk->max_transid) {
2080 i = nritems;
2081 goto advance_key;
2082 }
2083 found_transid = btrfs_header_generation(leaf);
2084
2085 for (i = slot; i < nritems; i++) {
2086 item_off = btrfs_item_ptr_offset(leaf, i);
2087 item_len = btrfs_item_size_nr(leaf, i);
2088
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002089 btrfs_item_key_to_cpu(leaf, key, i);
2090 if (!key_in_sk(key, sk))
2091 continue;
2092
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002093 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002094 if (*num_found) {
2095 ret = 1;
2096 goto out;
2097 }
Chris Masonac8e9812010-02-28 15:39:26 -05002098
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002099 /*
2100 * return one empty item back for v1, which does not
2101 * handle -EOVERFLOW
2102 */
2103
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002104 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002105 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002106 ret = -EOVERFLOW;
2107 }
Chris Masonac8e9812010-02-28 15:39:26 -05002108
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002109 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002110 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002111 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002112 }
2113
Chris Masonac8e9812010-02-28 15:39:26 -05002114 sh.objectid = key->objectid;
2115 sh.offset = key->offset;
2116 sh.type = key->type;
2117 sh.len = item_len;
2118 sh.transid = found_transid;
2119
Josef Bacika48b73e2020-08-10 11:42:27 -04002120 /*
2121 * Copy search result header. If we fault then loop again so we
2122 * can fault in the pages and -EFAULT there if there's a
2123 * problem. Otherwise we'll fault and then copy the buffer in
2124 * properly this next time through
2125 */
2126 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2127 ret = 0;
Gerhard Heiftba346b32014-01-30 16:24:02 +01002128 goto out;
2129 }
2130
Chris Masonac8e9812010-02-28 15:39:26 -05002131 *sk_offset += sizeof(sh);
2132
2133 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002134 char __user *up = ubuf + *sk_offset;
Josef Bacika48b73e2020-08-10 11:42:27 -04002135 /*
2136 * Copy the item, same behavior as above, but reset the
2137 * * sk_offset so we copy the full thing again.
2138 */
2139 if (read_extent_buffer_to_user_nofault(leaf, up,
2140 item_off, item_len)) {
2141 ret = 0;
2142 *sk_offset -= sizeof(sh);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002143 goto out;
2144 }
2145
Chris Masonac8e9812010-02-28 15:39:26 -05002146 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002147 }
Hugo Millse2156862011-05-14 17:43:41 +00002148 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002149
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002150 if (ret) /* -EOVERFLOW from above */
2151 goto out;
2152
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002153 if (*num_found >= sk->nr_items) {
2154 ret = 1;
2155 goto out;
2156 }
Chris Masonac8e9812010-02-28 15:39:26 -05002157 }
2158advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002159 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002160 test.objectid = sk->max_objectid;
2161 test.type = sk->max_type;
2162 test.offset = sk->max_offset;
2163 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2164 ret = 1;
2165 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002166 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002167 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002168 key->offset = 0;
2169 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002170 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002171 key->offset = 0;
2172 key->type = 0;
2173 key->objectid++;
2174 } else
2175 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002176out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002177 /*
2178 * 0: all items from this leaf copied, continue with next
2179 * 1: * more items can be copied, but unused buffer is too small
2180 * * all items were found
2181 * Either way, it will stops the loop which iterates to the next
2182 * leaf
2183 * -EOVERFLOW: item was to large for buffer
2184 * -EFAULT: could not copy extent buffer back to userspace
2185 */
Chris Masonac8e9812010-02-28 15:39:26 -05002186 return ret;
2187}
2188
2189static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002190 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002191 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002192 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002194 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002195 struct btrfs_root *root;
2196 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002197 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002198 int ret;
2199 int num_found = 0;
2200 unsigned long sk_offset = 0;
2201
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002202 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2203 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002204 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002205 }
Gerhard Heift12544442014-01-30 16:23:58 +01002206
Chris Masonac8e9812010-02-28 15:39:26 -05002207 path = btrfs_alloc_path();
2208 if (!path)
2209 return -ENOMEM;
2210
2211 if (sk->tree_id == 0) {
2212 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002213 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002214 } else {
David Sterba56e93572020-05-15 19:35:55 +02002215 root = btrfs_get_fs_root(info, sk->tree_id, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002216 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002217 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002218 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002219 }
2220 }
2221
2222 key.objectid = sk->min_objectid;
2223 key.type = sk->min_type;
2224 key.offset = sk->min_offset;
2225
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302226 while (1) {
Filipe Manana1c785442020-09-14 09:01:04 +01002227 ret = fault_in_pages_writeable(ubuf + sk_offset,
2228 *buf_size - sk_offset);
Josef Bacika48b73e2020-08-10 11:42:27 -04002229 if (ret)
2230 break;
2231
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002232 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002233 if (ret != 0) {
2234 if (ret > 0)
2235 ret = 0;
2236 goto err;
2237 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002238 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002239 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002240 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002241 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002242 break;
2243
2244 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002245 if (ret > 0)
2246 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002247err:
2248 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002249 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002250 btrfs_free_path(path);
2251 return ret;
2252}
2253
2254static noinline int btrfs_ioctl_tree_search(struct file *file,
2255 void __user *argp)
2256{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002257 struct btrfs_ioctl_search_args __user *uargs;
2258 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002259 struct inode *inode;
2260 int ret;
2261 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002262
2263 if (!capable(CAP_SYS_ADMIN))
2264 return -EPERM;
2265
Gerhard Heiftba346b32014-01-30 16:24:02 +01002266 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002267
Gerhard Heiftba346b32014-01-30 16:24:02 +01002268 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2269 return -EFAULT;
2270
2271 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002272
Al Viro496ad9a2013-01-23 17:07:38 -05002273 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002274 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002275
2276 /*
2277 * In the origin implementation an overflow is handled by returning a
2278 * search header with a len of zero, so reset ret.
2279 */
2280 if (ret == -EOVERFLOW)
2281 ret = 0;
2282
Gerhard Heiftba346b32014-01-30 16:24:02 +01002283 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002284 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002285 return ret;
2286}
2287
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002288static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2289 void __user *argp)
2290{
2291 struct btrfs_ioctl_search_args_v2 __user *uarg;
2292 struct btrfs_ioctl_search_args_v2 args;
2293 struct inode *inode;
2294 int ret;
2295 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002296 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002297
2298 if (!capable(CAP_SYS_ADMIN))
2299 return -EPERM;
2300
2301 /* copy search header and buffer size */
2302 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2303 if (copy_from_user(&args, uarg, sizeof(args)))
2304 return -EFAULT;
2305
2306 buf_size = args.buf_size;
2307
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002308 /* limit result size to 16MB */
2309 if (buf_size > buf_limit)
2310 buf_size = buf_limit;
2311
2312 inode = file_inode(file);
2313 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002314 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002315 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2316 ret = -EFAULT;
2317 else if (ret == -EOVERFLOW &&
2318 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2319 ret = -EFAULT;
2320
Yan, Zheng76dda932009-09-21 16:00:26 -04002321 return ret;
2322}
2323
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002324/*
Chris Masonac8e9812010-02-28 15:39:26 -05002325 * Search INODE_REFs to identify path name of 'dirid' directory
2326 * in a 'tree_id' tree. and sets path name to 'name'.
2327 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002328static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2329 u64 tree_id, u64 dirid, char *name)
2330{
2331 struct btrfs_root *root;
2332 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002333 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002334 int ret = -1;
2335 int slot;
2336 int len;
2337 int total_len = 0;
2338 struct btrfs_inode_ref *iref;
2339 struct extent_buffer *l;
2340 struct btrfs_path *path;
2341
2342 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2343 name[0]='\0';
2344 return 0;
2345 }
2346
2347 path = btrfs_alloc_path();
2348 if (!path)
2349 return -ENOMEM;
2350
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002351 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002352
David Sterba56e93572020-05-15 19:35:55 +02002353 root = btrfs_get_fs_root(info, tree_id, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002354 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002355 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002356 root = NULL;
2357 goto out;
2358 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002359
2360 key.objectid = dirid;
2361 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002362 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002363
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302364 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002365 ret = btrfs_search_backwards(root, &key, path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002366 if (ret < 0)
2367 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002368 else if (ret > 0) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002369 ret = -ENOENT;
2370 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002371 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002372
2373 l = path->nodes[0];
2374 slot = path->slots[0];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002375
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002376 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2377 len = btrfs_inode_ref_name_len(l, iref);
2378 ptr -= len + 1;
2379 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002380 if (ptr < name) {
2381 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002382 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002383 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002384
2385 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302386 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002387
2388 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2389 break;
2390
David Sterbab3b4aa72011-04-21 01:20:15 +02002391 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002392 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002393 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002394 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002395 }
Li Zefan77906a502011-07-14 03:16:00 +00002396 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302397 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002398 ret = 0;
2399out:
Josef Bacik00246522020-01-24 09:33:01 -05002400 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002401 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002402 return ret;
2403}
2404
Christian Brauner6623d9a2021-07-27 12:48:57 +02002405static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns,
2406 struct inode *inode,
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002407 struct btrfs_ioctl_ino_lookup_user_args *args)
2408{
2409 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2410 struct super_block *sb = inode->i_sb;
2411 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2412 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2413 u64 dirid = args->dirid;
2414 unsigned long item_off;
2415 unsigned long item_len;
2416 struct btrfs_inode_ref *iref;
2417 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002418 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002419 struct btrfs_path *path;
2420 struct btrfs_key key, key2;
2421 struct extent_buffer *leaf;
2422 struct inode *temp_inode;
2423 char *ptr;
2424 int slot;
2425 int len;
2426 int total_len = 0;
2427 int ret;
2428
2429 path = btrfs_alloc_path();
2430 if (!path)
2431 return -ENOMEM;
2432
2433 /*
2434 * If the bottom subvolume does not exist directly under upper_limit,
2435 * construct the path in from the bottom up.
2436 */
2437 if (dirid != upper_limit.objectid) {
2438 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2439
David Sterba56e93572020-05-15 19:35:55 +02002440 root = btrfs_get_fs_root(fs_info, treeid, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002441 if (IS_ERR(root)) {
2442 ret = PTR_ERR(root);
2443 goto out;
2444 }
2445
2446 key.objectid = dirid;
2447 key.type = BTRFS_INODE_REF_KEY;
2448 key.offset = (u64)-1;
2449 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002450 ret = btrfs_search_backwards(root, &key, path);
2451 if (ret < 0)
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002452 goto out_put;
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002453 else if (ret > 0) {
2454 ret = -ENOENT;
2455 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002456 }
2457
2458 leaf = path->nodes[0];
2459 slot = path->slots[0];
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002460
2461 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2462 len = btrfs_inode_ref_name_len(leaf, iref);
2463 ptr -= len + 1;
2464 total_len += len + 1;
2465 if (ptr < args->path) {
2466 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002467 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002468 }
2469
2470 *(ptr + len) = '/';
2471 read_extent_buffer(leaf, ptr,
2472 (unsigned long)(iref + 1), len);
2473
2474 /* Check the read+exec permission of this directory */
2475 ret = btrfs_previous_item(root, path, dirid,
2476 BTRFS_INODE_ITEM_KEY);
2477 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002478 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002479 } else if (ret > 0) {
2480 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002481 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002482 }
2483
2484 leaf = path->nodes[0];
2485 slot = path->slots[0];
2486 btrfs_item_key_to_cpu(leaf, &key2, slot);
2487 if (key2.objectid != dirid) {
2488 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002489 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002490 }
2491
David Sterba0202e832020-05-15 19:35:59 +02002492 temp_inode = btrfs_iget(sb, key2.objectid, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002493 if (IS_ERR(temp_inode)) {
2494 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002495 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002496 }
Christian Brauner6623d9a2021-07-27 12:48:57 +02002497 ret = inode_permission(mnt_userns, temp_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01002498 MAY_READ | MAY_EXEC);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002499 iput(temp_inode);
2500 if (ret) {
2501 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002502 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002503 }
2504
2505 if (key.offset == upper_limit.objectid)
2506 break;
2507 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2508 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002509 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002510 }
2511
2512 btrfs_release_path(path);
2513 key.objectid = key.offset;
2514 key.offset = (u64)-1;
2515 dirid = key.objectid;
2516 }
2517
2518 memmove(args->path, ptr, total_len);
2519 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002520 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002521 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002522 btrfs_release_path(path);
2523 }
2524
2525 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002526 key.objectid = treeid;
2527 key.type = BTRFS_ROOT_REF_KEY;
2528 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002529 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002530 if (ret < 0) {
2531 goto out;
2532 } else if (ret > 0) {
2533 ret = -ENOENT;
2534 goto out;
2535 }
2536
2537 leaf = path->nodes[0];
2538 slot = path->slots[0];
2539 btrfs_item_key_to_cpu(leaf, &key, slot);
2540
2541 item_off = btrfs_item_ptr_offset(leaf, slot);
2542 item_len = btrfs_item_size_nr(leaf, slot);
2543 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2544 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2545 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2546 ret = -EINVAL;
2547 goto out;
2548 }
2549
2550 /* Copy subvolume's name */
2551 item_off += sizeof(struct btrfs_root_ref);
2552 item_len -= sizeof(struct btrfs_root_ref);
2553 read_extent_buffer(leaf, args->name, item_off, item_len);
2554 args->name[item_len] = 0;
2555
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002556out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002557 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002558out:
2559 btrfs_free_path(path);
2560 return ret;
2561}
2562
Chris Masonac8e9812010-02-28 15:39:26 -05002563static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2564 void __user *argp)
2565{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002566 struct btrfs_ioctl_ino_lookup_args *args;
2567 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002568 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002569
Julia Lawall2354d08f2010-10-29 15:14:18 -04002570 args = memdup_user(argp, sizeof(*args));
2571 if (IS_ERR(args))
2572 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002573
Al Viro496ad9a2013-01-23 17:07:38 -05002574 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002575
David Sterba01b810b2015-05-12 19:14:49 +02002576 /*
2577 * Unprivileged query to obtain the containing subvolume root id. The
2578 * path is reset so it's consistent with btrfs_search_path_in_tree.
2579 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002580 if (args->treeid == 0)
2581 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2582
David Sterba01b810b2015-05-12 19:14:49 +02002583 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2584 args->name[0] = 0;
2585 goto out;
2586 }
2587
2588 if (!capable(CAP_SYS_ADMIN)) {
2589 ret = -EPERM;
2590 goto out;
2591 }
2592
Chris Masonac8e9812010-02-28 15:39:26 -05002593 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2594 args->treeid, args->objectid,
2595 args->name);
2596
David Sterba01b810b2015-05-12 19:14:49 +02002597out:
Chris Masonac8e9812010-02-28 15:39:26 -05002598 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2599 ret = -EFAULT;
2600
2601 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002602 return ret;
2603}
2604
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002605/*
2606 * Version of ino_lookup ioctl (unprivileged)
2607 *
2608 * The main differences from ino_lookup ioctl are:
2609 *
2610 * 1. Read + Exec permission will be checked using inode_permission() during
2611 * path construction. -EACCES will be returned in case of failure.
2612 * 2. Path construction will be stopped at the inode number which corresponds
2613 * to the fd with which this ioctl is called. If constructed path does not
2614 * exist under fd's inode, -EACCES will be returned.
2615 * 3. The name of bottom subvolume is also searched and filled.
2616 */
2617static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2618{
2619 struct btrfs_ioctl_ino_lookup_user_args *args;
2620 struct inode *inode;
2621 int ret;
2622
2623 args = memdup_user(argp, sizeof(*args));
2624 if (IS_ERR(args))
2625 return PTR_ERR(args);
2626
2627 inode = file_inode(file);
2628
2629 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2630 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2631 /*
2632 * The subvolume does not exist under fd with which this is
2633 * called
2634 */
2635 kfree(args);
2636 return -EACCES;
2637 }
2638
Christian Brauner6623d9a2021-07-27 12:48:57 +02002639 ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002640
2641 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2642 ret = -EFAULT;
2643
2644 kfree(args);
2645 return ret;
2646}
2647
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002648/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2649static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2650{
2651 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2652 struct btrfs_fs_info *fs_info;
2653 struct btrfs_root *root;
2654 struct btrfs_path *path;
2655 struct btrfs_key key;
2656 struct btrfs_root_item *root_item;
2657 struct btrfs_root_ref *rref;
2658 struct extent_buffer *leaf;
2659 unsigned long item_off;
2660 unsigned long item_len;
2661 struct inode *inode;
2662 int slot;
2663 int ret = 0;
2664
2665 path = btrfs_alloc_path();
2666 if (!path)
2667 return -ENOMEM;
2668
2669 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2670 if (!subvol_info) {
2671 btrfs_free_path(path);
2672 return -ENOMEM;
2673 }
2674
2675 inode = file_inode(file);
2676 fs_info = BTRFS_I(inode)->root->fs_info;
2677
2678 /* Get root_item of inode's subvolume */
2679 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
David Sterba56e93572020-05-15 19:35:55 +02002680 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002681 if (IS_ERR(root)) {
2682 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002683 goto out_free;
2684 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002685 root_item = &root->root_item;
2686
2687 subvol_info->treeid = key.objectid;
2688
2689 subvol_info->generation = btrfs_root_generation(root_item);
2690 subvol_info->flags = btrfs_root_flags(root_item);
2691
2692 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2693 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2694 BTRFS_UUID_SIZE);
2695 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2696 BTRFS_UUID_SIZE);
2697
2698 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2699 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2700 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2701
2702 subvol_info->otransid = btrfs_root_otransid(root_item);
2703 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2704 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2705
2706 subvol_info->stransid = btrfs_root_stransid(root_item);
2707 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2708 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2709
2710 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2711 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2712 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2713
2714 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2715 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002716 key.type = BTRFS_ROOT_BACKREF_KEY;
2717 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002718 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002719 if (ret < 0) {
2720 goto out;
2721 } else if (path->slots[0] >=
2722 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002723 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002724 if (ret < 0) {
2725 goto out;
2726 } else if (ret > 0) {
2727 ret = -EUCLEAN;
2728 goto out;
2729 }
2730 }
2731
2732 leaf = path->nodes[0];
2733 slot = path->slots[0];
2734 btrfs_item_key_to_cpu(leaf, &key, slot);
2735 if (key.objectid == subvol_info->treeid &&
2736 key.type == BTRFS_ROOT_BACKREF_KEY) {
2737 subvol_info->parent_id = key.offset;
2738
2739 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2740 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2741
2742 item_off = btrfs_item_ptr_offset(leaf, slot)
2743 + sizeof(struct btrfs_root_ref);
2744 item_len = btrfs_item_size_nr(leaf, slot)
2745 - sizeof(struct btrfs_root_ref);
2746 read_extent_buffer(leaf, subvol_info->name,
2747 item_off, item_len);
2748 } else {
2749 ret = -ENOENT;
2750 goto out;
2751 }
2752 }
2753
2754 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2755 ret = -EFAULT;
2756
2757out:
Josef Bacik00246522020-01-24 09:33:01 -05002758 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002759out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002760 btrfs_free_path(path);
Waiman Longb091f7f2020-06-16 11:31:59 -04002761 kfree(subvol_info);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002762 return ret;
2763}
2764
Tomohiro Misono42e4b522018-05-21 10:09:43 +09002765/*
2766 * Return ROOT_REF information of the subvolume containing this inode
2767 * except the subvolume name.
2768 */
2769static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2770{
2771 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2772 struct btrfs_root_ref *rref;
2773 struct btrfs_root *root;
2774 struct btrfs_path *path;
2775 struct btrfs_key key;
2776 struct extent_buffer *leaf;
2777 struct inode *inode;
2778 u64 objectid;
2779 int slot;
2780 int ret;
2781 u8 found;
2782
2783 path = btrfs_alloc_path();
2784 if (!path)
2785 return -ENOMEM;
2786
2787 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2788 if (IS_ERR(rootrefs)) {
2789 btrfs_free_path(path);
2790 return PTR_ERR(rootrefs);
2791 }
2792
2793 inode = file_inode(file);
2794 root = BTRFS_I(inode)->root->fs_info->tree_root;
2795 objectid = BTRFS_I(inode)->root->root_key.objectid;
2796
2797 key.objectid = objectid;
2798 key.type = BTRFS_ROOT_REF_KEY;
2799 key.offset = rootrefs->min_treeid;
2800 found = 0;
2801
2802 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2803 if (ret < 0) {
2804 goto out;
2805 } else if (path->slots[0] >=
2806 btrfs_header_nritems(path->nodes[0])) {
2807 ret = btrfs_next_leaf(root, path);
2808 if (ret < 0) {
2809 goto out;
2810 } else if (ret > 0) {
2811 ret = -EUCLEAN;
2812 goto out;
2813 }
2814 }
2815 while (1) {
2816 leaf = path->nodes[0];
2817 slot = path->slots[0];
2818
2819 btrfs_item_key_to_cpu(leaf, &key, slot);
2820 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2821 ret = 0;
2822 goto out;
2823 }
2824
2825 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2826 ret = -EOVERFLOW;
2827 goto out;
2828 }
2829
2830 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2831 rootrefs->rootref[found].treeid = key.offset;
2832 rootrefs->rootref[found].dirid =
2833 btrfs_root_ref_dirid(leaf, rref);
2834 found++;
2835
2836 ret = btrfs_next_item(root, path);
2837 if (ret < 0) {
2838 goto out;
2839 } else if (ret > 0) {
2840 ret = -EUCLEAN;
2841 goto out;
2842 }
2843 }
2844
2845out:
2846 if (!ret || ret == -EOVERFLOW) {
2847 rootrefs->num_items = found;
2848 /* update min_treeid for next search */
2849 if (found)
2850 rootrefs->min_treeid =
2851 rootrefs->rootref[found - 1].treeid + 1;
2852 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2853 ret = -EFAULT;
2854 }
2855
2856 kfree(rootrefs);
2857 btrfs_free_path(path);
2858
2859 return ret;
2860}
2861
Yan, Zheng76dda932009-09-21 16:00:26 -04002862static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002863 void __user *arg,
2864 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04002865{
Al Viro54563d42013-09-01 15:57:51 -04002866 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002867 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002868 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002869 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04002870 struct inode *inode;
2871 struct btrfs_root *root = BTRFS_I(dir)->root;
2872 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002873 struct btrfs_ioctl_vol_args *vol_args = NULL;
2874 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002875 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002876 char *subvol_name, *subvol_name_ptr = NULL;
2877 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04002878 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002879 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04002880
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002881 if (destroy_v2) {
2882 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2883 if (IS_ERR(vol_args2))
2884 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002885
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002886 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2887 err = -EOPNOTSUPP;
2888 goto out;
2889 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002890
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002891 /*
2892 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2893 * name, same as v1 currently does.
2894 */
2895 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2896 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2897 subvol_name = vol_args2->name;
2898
2899 err = mnt_want_write_file(file);
2900 if (err)
2901 goto out;
2902 } else {
Christian Brauneraabb34e2021-07-27 12:48:54 +02002903 struct inode *old_dir;
Christian Braunerc4ed5332021-07-27 12:48:53 +02002904
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002905 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2906 err = -EINVAL;
2907 goto out;
2908 }
2909
2910 err = mnt_want_write_file(file);
2911 if (err)
2912 goto out;
2913
2914 dentry = btrfs_get_dentry(fs_info->sb,
2915 BTRFS_FIRST_FREE_OBJECTID,
2916 vol_args2->subvolid, 0, 0);
2917 if (IS_ERR(dentry)) {
2918 err = PTR_ERR(dentry);
2919 goto out_drop_write;
2920 }
2921
2922 /*
2923 * Change the default parent since the subvolume being
2924 * deleted can be outside of the current mount point.
2925 */
2926 parent = btrfs_get_parent(dentry);
2927
2928 /*
2929 * At this point dentry->d_name can point to '/' if the
2930 * subvolume we want to destroy is outsite of the
2931 * current mount point, so we need to release the
2932 * current dentry and execute the lookup to return a new
2933 * one with ->d_name pointing to the
2934 * <mount point>/subvol_name.
2935 */
2936 dput(dentry);
2937 if (IS_ERR(parent)) {
2938 err = PTR_ERR(parent);
2939 goto out_drop_write;
2940 }
Christian Brauneraabb34e2021-07-27 12:48:54 +02002941 old_dir = dir;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002942 dir = d_inode(parent);
2943
2944 /*
2945 * If v2 was used with SPEC_BY_ID, a new parent was
2946 * allocated since the subvolume can be outside of the
2947 * current mount point. Later on we need to release this
2948 * new parent dentry.
2949 */
2950 destroy_parent = true;
2951
Christian Brauneraabb34e2021-07-27 12:48:54 +02002952 /*
2953 * On idmapped mounts, deletion via subvolid is
2954 * restricted to subvolumes that are immediate
2955 * ancestors of the inode referenced by the file
2956 * descriptor in the ioctl. Otherwise the idmapping
2957 * could potentially be abused to delete subvolumes
2958 * anywhere in the filesystem the user wouldn't be able
2959 * to delete without an idmapped mount.
2960 */
2961 if (old_dir != dir && mnt_userns != &init_user_ns) {
2962 err = -EOPNOTSUPP;
2963 goto free_parent;
2964 }
2965
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002966 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2967 fs_info, vol_args2->subvolid);
2968 if (IS_ERR(subvol_name_ptr)) {
2969 err = PTR_ERR(subvol_name_ptr);
2970 goto free_parent;
2971 }
David Sterba1a9fd412021-05-21 17:42:23 +02002972 /* subvol_name_ptr is already nul terminated */
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002973 subvol_name = (char *)kbasename(subvol_name_ptr);
2974 }
2975 } else {
2976 vol_args = memdup_user(arg, sizeof(*vol_args));
2977 if (IS_ERR(vol_args))
2978 return PTR_ERR(vol_args);
2979
2980 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
2981 subvol_name = vol_args->name;
2982
2983 err = mnt_want_write_file(file);
2984 if (err)
2985 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002986 }
2987
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002988 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04002989
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03002990 if (strchr(subvol_name, '/') ||
2991 strncmp(subvol_name, "..", subvol_namelen) == 0) {
2992 err = -EINVAL;
2993 goto free_subvol_name;
2994 }
2995
2996 if (!S_ISDIR(dir->i_mode)) {
2997 err = -ENOTDIR;
2998 goto free_subvol_name;
2999 }
David Sterba521e0542014-04-15 16:41:44 +02003000
Al Viro00235412016-05-26 00:05:12 -04003001 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3002 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003003 goto free_subvol_name;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003004 dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04003005 if (IS_ERR(dentry)) {
3006 err = PTR_ERR(dentry);
3007 goto out_unlock_dir;
3008 }
3009
David Howells2b0143b2015-03-17 22:25:59 +00003010 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003011 err = -ENOENT;
3012 goto out_dput;
3013 }
3014
David Howells2b0143b2015-03-17 22:25:59 +00003015 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04003016 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303017 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04003018 /*
3019 * Regular user. Only allow this with a special mount
3020 * option, when the user has write+exec access to the
3021 * subvol root, and when rmdir(2) would have been
3022 * allowed.
3023 *
3024 * Note that this is _not_ check that the subvol is
3025 * empty or doesn't contain data that we wouldn't
3026 * otherwise be able to delete.
3027 *
3028 * Users who want to delete empty subvols should try
3029 * rmdir(2).
3030 */
3031 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04003033 goto out_dput;
3034
3035 /*
3036 * Do not allow deletion if the parent dir is the same
3037 * as the dir to be deleted. That means the ioctl
3038 * must be called on the dentry referencing the root
3039 * of the subvol, not a random directory contained
3040 * within it.
3041 */
3042 err = -EINVAL;
3043 if (root == dest)
3044 goto out_dput;
3045
Christian Braunerc4ed5332021-07-27 12:48:53 +02003046 err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -04003047 if (err)
3048 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04003049 }
3050
Miao Xie5c39da52012-10-22 11:39:53 +00003051 /* check if subvolume may be deleted by a user */
Christian Braunerc4ed5332021-07-27 12:48:53 +02003052 err = btrfs_may_delete(mnt_userns, dir, dentry, 1);
Miao Xie5c39da52012-10-22 11:39:53 +00003053 if (err)
3054 goto out_dput;
3055
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003056 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003057 err = -EINVAL;
3058 goto out_dput;
3059 }
3060
Josef Bacik64708532021-02-10 17:14:34 -05003061 btrfs_inode_lock(inode, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003062 err = btrfs_delete_subvolume(dir, dentry);
Josef Bacik64708532021-02-10 17:14:34 -05003063 btrfs_inode_unlock(inode, 0);
Amir Goldstein46008d92019-05-26 17:34:03 +03003064 if (!err) {
3065 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04003066 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03003067 }
Liu Bofa6ac872013-02-20 14:10:23 +00003068
Yan, Zheng76dda932009-09-21 16:00:26 -04003069out_dput:
3070 dput(dentry);
3071out_unlock_dir:
Josef Bacik64708532021-02-10 17:14:34 -05003072 btrfs_inode_unlock(dir, 0);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003073free_subvol_name:
3074 kfree(subvol_name_ptr);
3075free_parent:
3076 if (destroy_parent)
3077 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003078out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003079 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003080out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003081 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003082 kfree(vol_args);
3083 return err;
3084}
3085
Chris Mason1e701a32010-03-11 09:42:04 -05003086static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003087{
Al Viro496ad9a2013-01-23 17:07:38 -05003088 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003089 struct btrfs_root *root = BTRFS_I(inode)->root;
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003090 struct btrfs_ioctl_defrag_range_args range = {0};
Yan Zhengc146afa2008-11-12 14:34:12 -05003091 int ret;
3092
Ilya Dryomov25122d12013-01-20 15:57:57 +02003093 ret = mnt_want_write_file(file);
3094 if (ret)
3095 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003096
Ilya Dryomov25122d12013-01-20 15:57:57 +02003097 if (btrfs_root_readonly(root)) {
3098 ret = -EROFS;
3099 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003100 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003101
3102 switch (inode->i_mode & S_IFMT) {
3103 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003104 if (!capable(CAP_SYS_ADMIN)) {
3105 ret = -EPERM;
3106 goto out;
3107 }
Eric Sandeende78b512013-01-31 18:21:12 +00003108 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003109 break;
3110 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003111 /*
3112 * Note that this does not check the file descriptor for write
3113 * access. This prevents defragmenting executables that are
3114 * running and allows defrag on files open in read-only mode.
3115 */
3116 if (!capable(CAP_SYS_ADMIN) &&
Christian Brauner47291ba2021-01-21 14:19:24 +01003117 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
Adam Borowski616d3742018-07-18 00:08:59 +02003118 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003119 goto out;
3120 }
Chris Mason1e701a32010-03-11 09:42:04 -05003121
Chris Mason1e701a32010-03-11 09:42:04 -05003122 if (argp) {
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003123 if (copy_from_user(&range, argp, sizeof(range))) {
Chris Mason1e701a32010-03-11 09:42:04 -05003124 ret = -EFAULT;
Dan Carpenter683be162010-03-20 11:24:48 +00003125 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003126 }
3127 /* compression requires us to start the IO */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003128 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3129 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
3130 range.extent_thresh = (u32)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003131 }
3132 } else {
3133 /* the rest are all set to zero by kzalloc */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003134 range.len = (u64)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003135 }
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08003136 ret = btrfs_defrag_file(file_inode(file), &file->f_ra,
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003137 &range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003138 if (ret > 0)
3139 ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003140 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003141 default:
3142 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003143 }
Chris Masone441d542009-01-05 16:57:23 -05003144out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003145 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003146 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003147}
3148
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003149static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003150{
3151 struct btrfs_ioctl_vol_args *vol_args;
3152 int ret;
3153
Chris Masone441d542009-01-05 16:57:23 -05003154 if (!capable(CAP_SYS_ADMIN))
3155 return -EPERM;
3156
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003157 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
Anand Jaine57138b2013-08-21 11:44:48 +08003158 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003159
Li Zefandae7b662009-04-08 15:06:54 +08003160 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003161 if (IS_ERR(vol_args)) {
3162 ret = PTR_ERR(vol_args);
3163 goto out;
3164 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003165
Mark Fasheh5516e592008-07-24 12:20:14 -04003166 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003167 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003168
Anand Jain43d20762014-07-01 00:58:56 +08003169 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003170 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003171
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003172 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003173out:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003174 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003175 return ret;
3176}
3177
Anand Jain6b526ed2016-02-13 10:01:39 +08003178static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003179{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003180 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003181 struct inode *inode = file_inode(file);
3182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003183 struct btrfs_ioctl_vol_args_v2 *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003184 struct block_device *bdev = NULL;
3185 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003186 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003187 bool cancel = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003188
Chris Masone441d542009-01-05 16:57:23 -05003189 if (!capable(CAP_SYS_ADMIN))
3190 return -EPERM;
3191
Li Zefandae7b662009-04-08 15:06:54 +08003192 vol_args = memdup_user(arg, sizeof(*vol_args));
Dan Carpenterd815b3f2021-11-16 14:50:25 +03003193 if (IS_ERR(vol_args))
3194 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003195
David Sterba748449cdb2020-02-21 13:30:14 +01003196 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003197 ret = -EOPNOTSUPP;
3198 goto out;
3199 }
Josef Bacik1a15eb72021-10-05 16:12:44 -04003200
David Sterba67ae34b2021-05-14 21:21:27 +02003201 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Josef Bacik1a15eb72021-10-05 16:12:44 -04003202 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3203 args.devid = vol_args->devid;
3204 } else if (!strcmp("cancel", vol_args->name)) {
David Sterba67ae34b2021-05-14 21:21:27 +02003205 cancel = true;
Josef Bacik1a15eb72021-10-05 16:12:44 -04003206 } else {
3207 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3208 if (ret)
3209 goto out;
3210 }
3211
3212 ret = mnt_want_write_file(file);
3213 if (ret)
3214 goto out;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003215
David Sterba67ae34b2021-05-14 21:21:27 +02003216 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3217 cancel);
3218 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003219 goto err_drop;
Anand Jain183860f2013-05-17 10:52:45 +00003220
Josef Bacik1a15eb72021-10-05 16:12:44 -04003221 /* Exclusive operation is now claimed */
3222 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003223
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003224 btrfs_exclop_finish(fs_info);
Anand Jain183860f2013-05-17 10:52:45 +00003225
Anand Jain6b526ed2016-02-13 10:01:39 +08003226 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003227 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003228 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003229 vol_args->devid);
3230 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003231 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003232 vol_args->name);
3233 }
Dan Carpenterc47ca322014-09-04 14:09:15 +03003234err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003235 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003236 if (bdev)
3237 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003238out:
3239 btrfs_put_dev_args_from_path(&args);
3240 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003241 return ret;
3242}
3243
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003244static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3245{
Josef Bacik1a15eb72021-10-05 16:12:44 -04003246 BTRFS_DEV_LOOKUP_ARGS(args);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003247 struct inode *inode = file_inode(file);
3248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003249 struct btrfs_ioctl_vol_args *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003250 struct block_device *bdev = NULL;
3251 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003253 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003254
3255 if (!capable(CAP_SYS_ADMIN))
3256 return -EPERM;
3257
Josef Bacik1a15eb72021-10-05 16:12:44 -04003258 vol_args = memdup_user(arg, sizeof(*vol_args));
3259 if (IS_ERR(vol_args))
3260 return PTR_ERR(vol_args);
3261
3262 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3263 if (!strcmp("cancel", vol_args->name)) {
3264 cancel = true;
3265 } else {
3266 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
3267 if (ret)
3268 goto out;
3269 }
3270
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003271 ret = mnt_want_write_file(file);
3272 if (ret)
Josef Bacik1a15eb72021-10-05 16:12:44 -04003273 goto out;
David Sterba67ae34b2021-05-14 21:21:27 +02003274
3275 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3276 cancel);
3277 if (ret == 0) {
Josef Bacik1a15eb72021-10-05 16:12:44 -04003278 ret = btrfs_rm_device(fs_info, &args, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003279 if (!ret)
3280 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3281 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003282 }
3283
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003284 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003285 if (bdev)
3286 blkdev_put(bdev, mode);
Josef Bacik1a15eb72021-10-05 16:12:44 -04003287out:
3288 btrfs_put_dev_args_from_path(&args);
3289 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003290 return ret;
3291}
3292
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003293static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3294 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003295{
Li Zefan027ed2f2011-06-08 08:27:56 +00003296 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003297 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003298 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003299 u64 flags_in;
Li Zefan027ed2f2011-06-08 08:27:56 +00003300 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003301
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003302 fi_args = memdup_user(arg, sizeof(*fi_args));
3303 if (IS_ERR(fi_args))
3304 return PTR_ERR(fi_args);
3305
3306 flags_in = fi_args->flags;
3307 memset(fi_args, 0, sizeof(*fi_args));
Li Zefan027ed2f2011-06-08 08:27:56 +00003308
David Sterbad03262c2017-06-16 00:09:21 +02003309 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003310 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003311
David Sterbad03262c2017-06-16 00:09:21 +02003312 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003313 if (device->devid > fi_args->max_id)
3314 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003315 }
David Sterbad03262c2017-06-16 00:09:21 +02003316 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003317
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003318 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003319 fi_args->nodesize = fs_info->nodesize;
3320 fi_args->sectorsize = fs_info->sectorsize;
3321 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003322
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003323 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3324 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3325 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3326 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3327 }
3328
Johannes Thumshirn0fb408a2020-07-13 21:28:59 +09003329 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3330 fi_args->generation = fs_info->generation;
3331 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3332 }
3333
Johannes Thumshirn49bac892020-07-13 21:29:00 +09003334 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3335 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3336 sizeof(fi_args->metadata_uuid));
3337 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3338 }
3339
Li Zefan027ed2f2011-06-08 08:27:56 +00003340 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3341 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003342
Li Zefan027ed2f2011-06-08 08:27:56 +00003343 kfree(fi_args);
3344 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003345}
3346
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003347static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3348 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003349{
Josef Bacik562d7b12021-10-05 16:12:42 -04003350 BTRFS_DEV_LOOKUP_ARGS(args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003351 struct btrfs_ioctl_dev_info_args *di_args;
3352 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003353 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003354
Jan Schmidt475f6382011-03-11 15:41:01 +01003355 di_args = memdup_user(arg, sizeof(*di_args));
3356 if (IS_ERR(di_args))
3357 return PTR_ERR(di_args);
3358
Josef Bacik562d7b12021-10-05 16:12:42 -04003359 args.devid = di_args->devid;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003360 if (!btrfs_is_empty_uuid(di_args->uuid))
Josef Bacik562d7b12021-10-05 16:12:42 -04003361 args.uuid = di_args->uuid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003362
David Sterbac5593ca2017-06-16 00:09:21 +02003363 rcu_read_lock();
Josef Bacik562d7b12021-10-05 16:12:42 -04003364 dev = btrfs_find_device(fs_info->fs_devices, &args);
Jan Schmidt475f6382011-03-11 15:41:01 +01003365 if (!dev) {
3366 ret = -ENODEV;
3367 goto out;
3368 }
3369
3370 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003371 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3372 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003373 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003374 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003375 strncpy(di_args->path, rcu_str_deref(dev->name),
3376 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003377 di_args->path[sizeof(di_args->path) - 1] = 0;
3378 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003379 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003380 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003381
3382out:
David Sterbac5593ca2017-06-16 00:09:21 +02003383 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003384 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3385 ret = -EFAULT;
3386
3387 kfree(di_args);
3388 return ret;
3389}
3390
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003391static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3392{
Al Viro496ad9a2013-01-23 17:07:38 -05003393 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003395 struct btrfs_root *root = BTRFS_I(inode)->root;
3396 struct btrfs_root *new_root;
3397 struct btrfs_dir_item *di;
3398 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003399 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003400 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003401 u64 objectid = 0;
3402 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003403 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003404
3405 if (!capable(CAP_SYS_ADMIN))
3406 return -EPERM;
3407
Miao Xie3c04ce02012-11-26 08:43:07 +00003408 ret = mnt_want_write_file(file);
3409 if (ret)
3410 return ret;
3411
3412 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3413 ret = -EFAULT;
3414 goto out;
3415 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003416
3417 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303418 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003419
David Sterba56e93572020-05-15 19:35:55 +02003420 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00003421 if (IS_ERR(new_root)) {
3422 ret = PTR_ERR(new_root);
3423 goto out;
3424 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003425 if (!is_fstree(new_root->root_key.objectid)) {
3426 ret = -ENOENT;
3427 goto out_free;
3428 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003429
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003430 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003431 if (!path) {
3432 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003433 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00003434 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003435
3436 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003437 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00003438 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003439 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003440 }
3441
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3443 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003444 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003445 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003446 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003447 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003448 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003449 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003450 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003451 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003452 }
3453
3454 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3455 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3456 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003457 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003458
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003460 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003461out_free:
Josef Bacik00246522020-01-24 09:33:01 -05003462 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003463 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003464out:
3465 mnt_drop_write_file(file);
3466 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003467}
3468
Su Yuec065f5b12018-04-02 17:24:11 +08003469static void get_block_group_info(struct list_head *groups_list,
3470 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003471{
David Sterba32da53862019-10-29 19:20:18 +01003472 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003473
3474 space->total_bytes = 0;
3475 space->used_bytes = 0;
3476 space->flags = 0;
3477 list_for_each_entry(block_group, groups_list, list) {
3478 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02003479 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02003480 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003481 }
3482}
3483
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003484static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3485 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003486{
3487 struct btrfs_ioctl_space_args space_args;
3488 struct btrfs_ioctl_space_info space;
3489 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003490 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003491 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003492 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003493 static const u64 types[] = {
3494 BTRFS_BLOCK_GROUP_DATA,
3495 BTRFS_BLOCK_GROUP_SYSTEM,
3496 BTRFS_BLOCK_GROUP_METADATA,
3497 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3498 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003499 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003500 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003501 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003502 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003503 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003504
3505 if (copy_from_user(&space_args,
3506 (struct btrfs_ioctl_space_args __user *)arg,
3507 sizeof(space_args)))
3508 return -EFAULT;
3509
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003510 for (i = 0; i < num_types; i++) {
3511 struct btrfs_space_info *tmp;
3512
3513 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003514 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003515 if (tmp->flags == types[i]) {
3516 info = tmp;
3517 break;
3518 }
3519 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003520
3521 if (!info)
3522 continue;
3523
3524 down_read(&info->groups_sem);
3525 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3526 if (!list_empty(&info->block_groups[c]))
3527 slot_count++;
3528 }
3529 up_read(&info->groups_sem);
3530 }
Josef Bacik1406e432010-01-13 18:19:06 +00003531
David Sterba36523e952014-02-07 14:34:12 +01003532 /*
3533 * Global block reserve, exported as a space_info
3534 */
3535 slot_count++;
3536
Chris Mason7fde62b2010-03-16 15:40:10 -04003537 /* space_slots == 0 means they are asking for a count */
3538 if (space_args.space_slots == 0) {
3539 space_args.total_spaces = slot_count;
3540 goto out;
3541 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003542
Dan Rosenberg51788b12011-02-14 16:04:23 -05003543 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003544
Chris Mason7fde62b2010-03-16 15:40:10 -04003545 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003546
Chris Mason7fde62b2010-03-16 15:40:10 -04003547 /* we generally have at most 6 or so space infos, one for each raid
3548 * level. So, a whole page should be more than enough for everyone
3549 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003550 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003551 return -ENOMEM;
3552
3553 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003554 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003555 if (!dest)
3556 return -ENOMEM;
3557 dest_orig = dest;
3558
3559 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003560 for (i = 0; i < num_types; i++) {
3561 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003562
Dan Rosenberg51788b12011-02-14 16:04:23 -05003563 if (!slot_count)
3564 break;
3565
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003566 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003567 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003568 if (tmp->flags == types[i]) {
3569 info = tmp;
3570 break;
3571 }
3572 }
Chris Mason7fde62b2010-03-16 15:40:10 -04003573
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003574 if (!info)
3575 continue;
3576 down_read(&info->groups_sem);
3577 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3578 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003579 get_block_group_info(&info->block_groups[c],
3580 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003581 memcpy(dest, &space, sizeof(space));
3582 dest++;
3583 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003584 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003585 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003586 if (!slot_count)
3587 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003588 }
3589 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003590 }
Josef Bacik1406e432010-01-13 18:19:06 +00003591
David Sterba36523e952014-02-07 14:34:12 +01003592 /*
3593 * Add global block reserve
3594 */
3595 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003596 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003597
3598 spin_lock(&block_rsv->lock);
3599 space.total_bytes = block_rsv->size;
3600 space.used_bytes = block_rsv->size - block_rsv->reserved;
3601 spin_unlock(&block_rsv->lock);
3602 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3603 memcpy(dest, &space, sizeof(space));
3604 space_args.total_spaces++;
3605 }
3606
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003607 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003608 (arg + sizeof(struct btrfs_ioctl_space_args));
3609
3610 if (copy_to_user(user_dest, dest_orig, alloc_size))
3611 ret = -EFAULT;
3612
3613 kfree(dest_orig);
3614out:
3615 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003616 ret = -EFAULT;
3617
3618 return ret;
3619}
3620
Miao Xie9a8c28b2012-11-26 08:40:43 +00003621static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3622 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003623{
Sage Weil46204592010-10-29 15:41:32 -04003624 struct btrfs_trans_handle *trans;
3625 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003626 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003627
Miao Xied4edf392013-02-20 09:17:06 +00003628 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003629 if (IS_ERR(trans)) {
3630 if (PTR_ERR(trans) != -ENOENT)
3631 return PTR_ERR(trans);
3632
3633 /* No running transaction, don't bother */
3634 transid = root->fs_info->last_trans_committed;
3635 goto out;
3636 }
Sage Weil46204592010-10-29 15:41:32 -04003637 transid = trans->transid;
David Sterba32cc4f82021-06-03 17:20:21 +02003638 ret = btrfs_commit_transaction_async(trans);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003639 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003640 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003641 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003642 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003643out:
Sage Weil46204592010-10-29 15:41:32 -04003644 if (argp)
3645 if (copy_to_user(argp, &transid, sizeof(transid)))
3646 return -EFAULT;
3647 return 0;
3648}
3649
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003650static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00003651 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003652{
Sage Weil46204592010-10-29 15:41:32 -04003653 u64 transid;
3654
3655 if (argp) {
3656 if (copy_from_user(&transid, argp, sizeof(transid)))
3657 return -EFAULT;
3658 } else {
3659 transid = 0; /* current trans */
3660 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003661 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04003662}
3663
Miao Xieb8e95482012-11-26 08:48:01 +00003664static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003665{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003666 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01003667 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003668 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003669
3670 if (!capable(CAP_SYS_ADMIN))
3671 return -EPERM;
3672
3673 sa = memdup_user(arg, sizeof(*sa));
3674 if (IS_ERR(sa))
3675 return PTR_ERR(sa);
3676
Miao Xieb8e95482012-11-26 08:48:01 +00003677 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3678 ret = mnt_want_write_file(file);
3679 if (ret)
3680 goto out;
3681 }
3682
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003683 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003684 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3685 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003686
Filipe Manana5afe6ce2020-01-16 11:29:20 +00003687 /*
3688 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3689 * error. This is important as it allows user space to know how much
3690 * progress scrub has done. For example, if scrub is canceled we get
3691 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3692 * space. Later user space can inspect the progress from the structure
3693 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3694 * previously (btrfs-progs does this).
3695 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3696 * then return -EFAULT to signal the structure was not copied or it may
3697 * be corrupt and unreliable due to a partial copy.
3698 */
3699 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003700 ret = -EFAULT;
3701
Miao Xieb8e95482012-11-26 08:48:01 +00003702 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3703 mnt_drop_write_file(file);
3704out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003705 kfree(sa);
3706 return ret;
3707}
3708
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003709static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01003710{
3711 if (!capable(CAP_SYS_ADMIN))
3712 return -EPERM;
3713
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003714 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003715}
3716
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003717static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01003718 void __user *arg)
3719{
3720 struct btrfs_ioctl_scrub_args *sa;
3721 int ret;
3722
3723 if (!capable(CAP_SYS_ADMIN))
3724 return -EPERM;
3725
3726 sa = memdup_user(arg, sizeof(*sa));
3727 if (IS_ERR(sa))
3728 return PTR_ERR(sa);
3729
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003730 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01003731
Filipe Manana4fa99b02018-12-14 19:45:13 +00003732 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003733 ret = -EFAULT;
3734
3735 kfree(sa);
3736 return ret;
3737}
3738
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003739static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06003740 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003741{
3742 struct btrfs_ioctl_get_dev_stats *sa;
3743 int ret;
3744
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003745 sa = memdup_user(arg, sizeof(*sa));
3746 if (IS_ERR(sa))
3747 return PTR_ERR(sa);
3748
David Sterbab27f7c02012-06-22 06:30:39 -06003749 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3750 kfree(sa);
3751 return -EPERM;
3752 }
3753
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003754 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003755
Filipe Mananaeee99572018-12-14 19:45:22 +00003756 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003757 ret = -EFAULT;
3758
3759 kfree(sa);
3760 return ret;
3761}
3762
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003763static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3764 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003765{
3766 struct btrfs_ioctl_dev_replace_args *p;
3767 int ret;
3768
3769 if (!capable(CAP_SYS_ADMIN))
3770 return -EPERM;
3771
3772 p = memdup_user(arg, sizeof(*p));
3773 if (IS_ERR(p))
3774 return PTR_ERR(p);
3775
3776 switch (p->cmd) {
3777 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01003778 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003779 ret = -EROFS;
3780 goto out;
3781 }
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003782 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003783 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003784 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003785 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003786 btrfs_exclop_finish(fs_info);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003787 }
3788 break;
3789 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003790 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003791 ret = 0;
3792 break;
3793 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08003794 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08003795 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003796 break;
3797 default:
3798 ret = -EINVAL;
3799 break;
3800 }
3801
Filipe Mananad3a53282019-01-08 11:42:09 +00003802 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003803 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003804out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003805 kfree(p);
3806 return ret;
3807}
3808
Jan Schmidtd7728c92011-07-07 16:48:38 +02003809static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3810{
3811 int ret = 0;
3812 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003813 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003814 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003815 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003816 struct inode_fs_paths *ipath = NULL;
3817 struct btrfs_path *path;
3818
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003819 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003820 return -EPERM;
3821
3822 path = btrfs_alloc_path();
3823 if (!path) {
3824 ret = -ENOMEM;
3825 goto out;
3826 }
3827
3828 ipa = memdup_user(arg, sizeof(*ipa));
3829 if (IS_ERR(ipa)) {
3830 ret = PTR_ERR(ipa);
3831 ipa = NULL;
3832 goto out;
3833 }
3834
3835 size = min_t(u32, ipa->size, 4096);
3836 ipath = init_ipath(size, root, path);
3837 if (IS_ERR(ipath)) {
3838 ret = PTR_ERR(ipath);
3839 ipath = NULL;
3840 goto out;
3841 }
3842
3843 ret = paths_from_inode(ipa->inum, ipath);
3844 if (ret < 0)
3845 goto out;
3846
3847 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003848 rel_ptr = ipath->fspath->val[i] -
3849 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003850 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003851 }
3852
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003853 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3854 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003855 if (ret) {
3856 ret = -EFAULT;
3857 goto out;
3858 }
3859
3860out:
3861 btrfs_free_path(path);
3862 free_ipath(ipath);
3863 kfree(ipa);
3864
3865 return ret;
3866}
3867
3868static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3869{
3870 struct btrfs_data_container *inodes = ctx;
3871 const size_t c = 3 * sizeof(u64);
3872
3873 if (inodes->bytes_left >= c) {
3874 inodes->bytes_left -= c;
3875 inodes->val[inodes->elem_cnt] = inum;
3876 inodes->val[inodes->elem_cnt + 1] = offset;
3877 inodes->val[inodes->elem_cnt + 2] = root;
3878 inodes->elem_cnt += 3;
3879 } else {
3880 inodes->bytes_missing += c - inodes->bytes_left;
3881 inodes->bytes_left = 0;
3882 inodes->elem_missed += 3;
3883 }
3884
3885 return 0;
3886}
3887
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003888static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003889 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003890{
3891 int ret = 0;
3892 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003893 struct btrfs_ioctl_logical_ino_args *loi;
3894 struct btrfs_data_container *inodes = NULL;
3895 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003896 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003897
3898 if (!capable(CAP_SYS_ADMIN))
3899 return -EPERM;
3900
3901 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05303902 if (IS_ERR(loi))
3903 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003904
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003905 if (version == 1) {
3906 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003907 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003908 } else {
3909 /* All reserved bits must be 0 for now */
3910 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3911 ret = -EINVAL;
3912 goto out_loi;
3913 }
3914 /* Only accept flags we have defined so far */
3915 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3916 ret = -EINVAL;
3917 goto out_loi;
3918 }
3919 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04003920 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003921 }
3922
Jan Schmidtd7728c92011-07-07 16:48:38 +02003923 path = btrfs_alloc_path();
3924 if (!path) {
3925 ret = -ENOMEM;
3926 goto out;
3927 }
3928
Jan Schmidtd7728c92011-07-07 16:48:38 +02003929 inodes = init_data_container(size);
3930 if (IS_ERR(inodes)) {
3931 ret = PTR_ERR(inodes);
3932 inodes = NULL;
3933 goto out;
3934 }
3935
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003936 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003937 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06003938 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003939 ret = -ENOENT;
3940 if (ret < 0)
3941 goto out;
3942
Omar Sandoval718dc5f2017-08-22 23:46:05 -07003943 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3944 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003945 if (ret)
3946 ret = -EFAULT;
3947
3948out:
3949 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02003950 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04003951out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02003952 kfree(loi);
3953
3954 return ret;
3955}
3956
David Sterba008ef092018-03-21 02:05:27 +01003957void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003958 struct btrfs_ioctl_balance_args *bargs)
3959{
3960 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3961
3962 bargs->flags = bctl->flags;
3963
David Sterba3009a622018-03-21 01:31:04 +01003964 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003965 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3966 if (atomic_read(&fs_info->balance_pause_req))
3967 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003968 if (atomic_read(&fs_info->balance_cancel_req))
3969 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003970
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003971 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3972 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3973 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003974
David Sterba008ef092018-03-21 02:05:27 +01003975 spin_lock(&fs_info->balance_lock);
3976 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3977 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003978}
3979
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003980static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003981{
Al Viro496ad9a2013-01-23 17:07:38 -05003982 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003983 struct btrfs_fs_info *fs_info = root->fs_info;
3984 struct btrfs_ioctl_balance_args *bargs;
3985 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003986 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003987 int ret;
3988
Nikolay Borisov6c405b22021-11-10 13:41:04 +02003989 if (!arg)
3990 btrfs_warn(fs_info,
3991 "IOC_BALANCE ioctl (v1) is deprecated and will be removed in kernel 5.18");
3992
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003993 if (!capable(CAP_SYS_ADMIN))
3994 return -EPERM;
3995
Liu Boe54bfa312012-06-29 03:58:48 -06003996 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003997 if (ret)
3998 return ret;
3999
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004000again:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004001 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004002 mutex_lock(&fs_info->balance_mutex);
4003 need_unlock = true;
4004 goto locked;
4005 }
4006
4007 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004008 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004009 * (1) some other op is running
4010 * (2) balance is running
4011 * (3) balance is paused -- special case (think resume)
4012 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004013 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004014 if (fs_info->balance_ctl) {
4015 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004016 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004017 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004018 /*
4019 * Lock released to allow other waiters to continue,
4020 * we'll reexamine the status again.
4021 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004022 mutex_lock(&fs_info->balance_mutex);
4023
4024 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004025 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004026 /* this is (3) */
4027 need_unlock = false;
4028 goto locked;
4029 }
4030
4031 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004032 goto again;
4033 } else {
4034 /* this is (2) */
4035 mutex_unlock(&fs_info->balance_mutex);
4036 ret = -EINPROGRESS;
4037 goto out;
4038 }
4039 } else {
4040 /* this is (1) */
4041 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004042 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004043 goto out;
4044 }
4045
4046locked:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004047
4048 if (arg) {
4049 bargs = memdup_user(arg, sizeof(*bargs));
4050 if (IS_ERR(bargs)) {
4051 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004052 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004053 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004054
4055 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4056 if (!fs_info->balance_ctl) {
4057 ret = -ENOTCONN;
4058 goto out_bargs;
4059 }
4060
4061 bctl = fs_info->balance_ctl;
4062 spin_lock(&fs_info->balance_lock);
4063 bctl->flags |= BTRFS_BALANCE_RESUME;
4064 spin_unlock(&fs_info->balance_lock);
4065
4066 goto do_balance;
4067 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004068 } else {
4069 bargs = NULL;
4070 }
4071
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004072 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004073 ret = -EINPROGRESS;
4074 goto out_bargs;
4075 }
4076
David Sterba8d2db782015-11-04 15:38:29 +01004077 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004078 if (!bctl) {
4079 ret = -ENOMEM;
4080 goto out_bargs;
4081 }
4082
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004083 if (arg) {
4084 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4085 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4086 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4087
4088 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004089 } else {
4090 /* balance everything - no filters */
4091 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004092 }
4093
David Sterba8eb93452015-10-12 16:55:54 +02004094 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4095 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004096 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004097 }
4098
Ilya Dryomovde322262012-01-16 22:04:49 +02004099do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004100 /*
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004101 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4102 * bctl is freed in reset_balance_state, or, if restriper was paused
4103 * all the way until unmount, in free_fs_info. The flag should be
4104 * cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004105 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004106 need_unlock = false;
4107
David Sterba6fcf6e22018-05-07 17:44:03 +02004108 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004109 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004110
Filipe Mananad00c2d92019-01-08 11:42:01 +00004111 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004112 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4113 ret = -EFAULT;
4114 }
4115
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004116out_bctl:
4117 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004118out_bargs:
4119 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004120out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004121 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004122 if (need_unlock)
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004123 btrfs_exclop_finish(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004124out:
Liu Boe54bfa312012-06-29 03:58:48 -06004125 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004126 return ret;
4127}
4128
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004129static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004130{
4131 if (!capable(CAP_SYS_ADMIN))
4132 return -EPERM;
4133
4134 switch (cmd) {
4135 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004136 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004137 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004138 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004139 }
4140
4141 return -EINVAL;
4142}
4143
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004144static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004145 void __user *arg)
4146{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004147 struct btrfs_ioctl_balance_args *bargs;
4148 int ret = 0;
4149
4150 if (!capable(CAP_SYS_ADMIN))
4151 return -EPERM;
4152
4153 mutex_lock(&fs_info->balance_mutex);
4154 if (!fs_info->balance_ctl) {
4155 ret = -ENOTCONN;
4156 goto out;
4157 }
4158
David Sterba8d2db782015-11-04 15:38:29 +01004159 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004160 if (!bargs) {
4161 ret = -ENOMEM;
4162 goto out;
4163 }
4164
David Sterba008ef092018-03-21 02:05:27 +01004165 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004166
4167 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4168 ret = -EFAULT;
4169
4170 kfree(bargs);
4171out:
4172 mutex_unlock(&fs_info->balance_mutex);
4173 return ret;
4174}
4175
Miao Xie905b0dd2012-11-26 08:50:11 +00004176static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004178 struct inode *inode = file_inode(file);
4179 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004180 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004181 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004182
4183 if (!capable(CAP_SYS_ADMIN))
4184 return -EPERM;
4185
Miao Xie905b0dd2012-11-26 08:50:11 +00004186 ret = mnt_want_write_file(file);
4187 if (ret)
4188 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004189
4190 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004191 if (IS_ERR(sa)) {
4192 ret = PTR_ERR(sa);
4193 goto drop_write;
4194 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004195
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004196 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004197
4198 switch (sa->cmd) {
4199 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004200 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004201 break;
4202 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004203 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004204 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004205 default:
4206 ret = -EINVAL;
4207 break;
4208 }
4209
Arne Jansen5d13a372011-09-14 15:53:51 +02004210 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004211 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004212drop_write:
4213 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004214 return ret;
4215}
4216
Miao Xie905b0dd2012-11-26 08:50:11 +00004217static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 struct inode *inode = file_inode(file);
4220 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4221 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004222 struct btrfs_ioctl_qgroup_assign_args *sa;
4223 struct btrfs_trans_handle *trans;
4224 int ret;
4225 int err;
4226
4227 if (!capable(CAP_SYS_ADMIN))
4228 return -EPERM;
4229
Miao Xie905b0dd2012-11-26 08:50:11 +00004230 ret = mnt_want_write_file(file);
4231 if (ret)
4232 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004233
4234 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004235 if (IS_ERR(sa)) {
4236 ret = PTR_ERR(sa);
4237 goto drop_write;
4238 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004239
4240 trans = btrfs_join_transaction(root);
4241 if (IS_ERR(trans)) {
4242 ret = PTR_ERR(trans);
4243 goto out;
4244 }
4245
Arne Jansen5d13a372011-09-14 15:53:51 +02004246 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004247 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004248 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004249 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004250 }
4251
Qu Wenruoe082f562015-02-27 16:24:28 +08004252 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004253 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004254 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004255 btrfs_handle_fs_error(fs_info, err,
4256 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004257 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004258 if (err && !ret)
4259 ret = err;
4260
4261out:
4262 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004263drop_write:
4264 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004265 return ret;
4266}
4267
Miao Xie905b0dd2012-11-26 08:50:11 +00004268static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004269{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004270 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004271 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004272 struct btrfs_ioctl_qgroup_create_args *sa;
4273 struct btrfs_trans_handle *trans;
4274 int ret;
4275 int err;
4276
4277 if (!capable(CAP_SYS_ADMIN))
4278 return -EPERM;
4279
Miao Xie905b0dd2012-11-26 08:50:11 +00004280 ret = mnt_want_write_file(file);
4281 if (ret)
4282 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004283
4284 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004285 if (IS_ERR(sa)) {
4286 ret = PTR_ERR(sa);
4287 goto drop_write;
4288 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004289
Miao Xied86e56c2012-11-15 11:35:41 +00004290 if (!sa->qgroupid) {
4291 ret = -EINVAL;
4292 goto out;
4293 }
4294
Arne Jansen5d13a372011-09-14 15:53:51 +02004295 trans = btrfs_join_transaction(root);
4296 if (IS_ERR(trans)) {
4297 ret = PTR_ERR(trans);
4298 goto out;
4299 }
4300
Arne Jansen5d13a372011-09-14 15:53:51 +02004301 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004302 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004303 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004304 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004305 }
4306
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004307 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004308 if (err && !ret)
4309 ret = err;
4310
4311out:
4312 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004313drop_write:
4314 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004315 return ret;
4316}
4317
Miao Xie905b0dd2012-11-26 08:50:11 +00004318static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004320 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004321 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004322 struct btrfs_ioctl_qgroup_limit_args *sa;
4323 struct btrfs_trans_handle *trans;
4324 int ret;
4325 int err;
4326 u64 qgroupid;
4327
4328 if (!capable(CAP_SYS_ADMIN))
4329 return -EPERM;
4330
Miao Xie905b0dd2012-11-26 08:50:11 +00004331 ret = mnt_want_write_file(file);
4332 if (ret)
4333 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004334
4335 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004336 if (IS_ERR(sa)) {
4337 ret = PTR_ERR(sa);
4338 goto drop_write;
4339 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004340
4341 trans = btrfs_join_transaction(root);
4342 if (IS_ERR(trans)) {
4343 ret = PTR_ERR(trans);
4344 goto out;
4345 }
4346
4347 qgroupid = sa->qgroupid;
4348 if (!qgroupid) {
4349 /* take the current subvol as qgroup */
4350 qgroupid = root->root_key.objectid;
4351 }
4352
Lu Fengqif0042d52018-07-18 14:45:35 +08004353 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004354
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004355 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004356 if (err && !ret)
4357 ret = err;
4358
4359out:
4360 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004361drop_write:
4362 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004363 return ret;
4364}
4365
Jan Schmidt2f232032013-04-25 16:04:51 +00004366static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004368 struct inode *inode = file_inode(file);
4369 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004370 struct btrfs_ioctl_quota_rescan_args *qsa;
4371 int ret;
4372
4373 if (!capable(CAP_SYS_ADMIN))
4374 return -EPERM;
4375
4376 ret = mnt_want_write_file(file);
4377 if (ret)
4378 return ret;
4379
4380 qsa = memdup_user(arg, sizeof(*qsa));
4381 if (IS_ERR(qsa)) {
4382 ret = PTR_ERR(qsa);
4383 goto drop_write;
4384 }
4385
4386 if (qsa->flags) {
4387 ret = -EINVAL;
4388 goto out;
4389 }
4390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004391 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004392
4393out:
4394 kfree(qsa);
4395drop_write:
4396 mnt_drop_write_file(file);
4397 return ret;
4398}
4399
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004400static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4401 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004402{
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004403 struct btrfs_ioctl_quota_rescan_args qsa = {0};
Jan Schmidt2f232032013-04-25 16:04:51 +00004404
4405 if (!capable(CAP_SYS_ADMIN))
4406 return -EPERM;
4407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004409 qsa.flags = 1;
4410 qsa.progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004411 }
4412
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004413 if (copy_to_user(arg, &qsa, sizeof(qsa)))
Anand Jain991a3da2021-07-28 14:20:41 +08004414 return -EFAULT;
Jan Schmidt2f232032013-04-25 16:04:51 +00004415
Anand Jain991a3da2021-07-28 14:20:41 +08004416 return 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00004417}
4418
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004419static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4420 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004421{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004422 if (!capable(CAP_SYS_ADMIN))
4423 return -EPERM;
4424
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004425 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004426}
4427
Hugo Millsabccd002014-01-30 20:17:00 +00004428static long _btrfs_ioctl_set_received_subvol(struct file *file,
Christian Braunere4fed172021-07-27 12:48:55 +02004429 struct user_namespace *mnt_userns,
Hugo Millsabccd002014-01-30 20:17:00 +00004430 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004431{
Al Viro496ad9a2013-01-23 17:07:38 -05004432 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004434 struct btrfs_root *root = BTRFS_I(inode)->root;
4435 struct btrfs_root_item *root_item = &root->root_item;
4436 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004437 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004438 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004439 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004440
Christian Braunere4fed172021-07-27 12:48:55 +02004441 if (!inode_owner_or_capable(mnt_userns, inode))
David Sterbabd60ea02014-01-16 15:50:22 +01004442 return -EPERM;
4443
Alexander Block8ea05e32012-07-25 17:35:53 +02004444 ret = mnt_want_write_file(file);
4445 if (ret < 0)
4446 return ret;
4447
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004448 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004449
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004450 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004451 ret = -EINVAL;
4452 goto out;
4453 }
4454
4455 if (btrfs_root_readonly(root)) {
4456 ret = -EROFS;
4457 goto out;
4458 }
4459
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004460 /*
4461 * 1 - root item
4462 * 2 - uuid items (received uuid + subvol uuid)
4463 */
4464 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004465 if (IS_ERR(trans)) {
4466 ret = PTR_ERR(trans);
4467 trans = NULL;
4468 goto out;
4469 }
4470
4471 sa->rtransid = trans->transid;
4472 sa->rtime.sec = ct.tv_sec;
4473 sa->rtime.nsec = ct.tv_nsec;
4474
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004475 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4476 BTRFS_UUID_SIZE);
4477 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004478 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004479 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004480 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4481 root->root_key.objectid);
4482 if (ret && ret != -ENOENT) {
4483 btrfs_abort_transaction(trans, ret);
4484 btrfs_end_transaction(trans);
4485 goto out;
4486 }
4487 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004488 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4489 btrfs_set_root_stransid(root_item, sa->stransid);
4490 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004491 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4492 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4493 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4494 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004495
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004497 &root->root_key, &root->root_item);
4498 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004499 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004500 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004501 }
4502 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004503 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004504 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4505 root->root_key.objectid);
4506 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004507 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004508 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004509 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004510 }
4511 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004512 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004513out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004514 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004515 mnt_drop_write_file(file);
4516 return ret;
4517}
4518
4519#ifdef CONFIG_64BIT
4520static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4521 void __user *arg)
4522{
4523 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4524 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4525 int ret = 0;
4526
4527 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304528 if (IS_ERR(args32))
4529 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004530
David Sterba8d2db782015-11-04 15:38:29 +01004531 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004532 if (!args64) {
4533 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004534 goto out;
4535 }
4536
4537 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4538 args64->stransid = args32->stransid;
4539 args64->rtransid = args32->rtransid;
4540 args64->stime.sec = args32->stime.sec;
4541 args64->stime.nsec = args32->stime.nsec;
4542 args64->rtime.sec = args32->rtime.sec;
4543 args64->rtime.nsec = args32->rtime.nsec;
4544 args64->flags = args32->flags;
4545
Christian Braunere4fed172021-07-27 12:48:55 +02004546 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64);
Hugo Millsabccd002014-01-30 20:17:00 +00004547 if (ret)
4548 goto out;
4549
4550 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4551 args32->stransid = args64->stransid;
4552 args32->rtransid = args64->rtransid;
4553 args32->stime.sec = args64->stime.sec;
4554 args32->stime.nsec = args64->stime.nsec;
4555 args32->rtime.sec = args64->rtime.sec;
4556 args32->rtime.nsec = args64->rtime.nsec;
4557 args32->flags = args64->flags;
4558
4559 ret = copy_to_user(arg, args32, sizeof(*args32));
4560 if (ret)
4561 ret = -EFAULT;
4562
4563out:
4564 kfree(args32);
4565 kfree(args64);
4566 return ret;
4567}
4568#endif
4569
4570static long btrfs_ioctl_set_received_subvol(struct file *file,
4571 void __user *arg)
4572{
4573 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4574 int ret = 0;
4575
4576 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304577 if (IS_ERR(sa))
4578 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004579
Christian Braunere4fed172021-07-27 12:48:55 +02004580 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004581
4582 if (ret)
4583 goto out;
4584
Alexander Block8ea05e32012-07-25 17:35:53 +02004585 ret = copy_to_user(arg, sa, sizeof(*sa));
4586 if (ret)
4587 ret = -EFAULT;
4588
4589out:
4590 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004591 return ret;
4592}
4593
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004594static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4595 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00004596{
Anand Jaina1b83ac2013-07-19 17:39:32 +08004597 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004598 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004599 char label[BTRFS_LABEL_SIZE];
4600
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004601 spin_lock(&fs_info->super_lock);
4602 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4603 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004604
4605 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004606
4607 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004608 btrfs_warn(fs_info,
4609 "label is too long, return the first %zu bytes",
4610 --len);
jeff.liu867ab662013-01-05 02:48:01 +00004611 }
4612
jeff.liu867ab662013-01-05 02:48:01 +00004613 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004614
4615 return ret ? -EFAULT : 0;
4616}
4617
jeff.liua8bfd4a2013-01-05 02:48:08 +00004618static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4619{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004620 struct inode *inode = file_inode(file);
4621 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4622 struct btrfs_root *root = BTRFS_I(inode)->root;
4623 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004624 struct btrfs_trans_handle *trans;
4625 char label[BTRFS_LABEL_SIZE];
4626 int ret;
4627
4628 if (!capable(CAP_SYS_ADMIN))
4629 return -EPERM;
4630
4631 if (copy_from_user(label, arg, sizeof(label)))
4632 return -EFAULT;
4633
4634 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004635 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004636 "unable to set label with more than %d bytes",
4637 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004638 return -EINVAL;
4639 }
4640
4641 ret = mnt_want_write_file(file);
4642 if (ret)
4643 return ret;
4644
jeff.liua8bfd4a2013-01-05 02:48:08 +00004645 trans = btrfs_start_transaction(root, 0);
4646 if (IS_ERR(trans)) {
4647 ret = PTR_ERR(trans);
4648 goto out_unlock;
4649 }
4650
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004652 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004653 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004654 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004655
4656out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004657 mnt_drop_write_file(file);
4658 return ret;
4659}
4660
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004661#define INIT_FEATURE_FLAGS(suffix) \
4662 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4663 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4664 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4665
David Sterbad5131b62016-02-17 15:26:27 +01004666int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004667{
David Sterba4d4ab6d2015-11-19 11:42:31 +01004668 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004669 INIT_FEATURE_FLAGS(SUPP),
4670 INIT_FEATURE_FLAGS(SAFE_SET),
4671 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4672 };
4673
4674 if (copy_to_user(arg, &features, sizeof(features)))
4675 return -EFAULT;
4676
4677 return 0;
4678}
4679
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004680static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4681 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004682{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004683 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004684 struct btrfs_ioctl_feature_flags features;
4685
4686 features.compat_flags = btrfs_super_compat_flags(super_block);
4687 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4688 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4689
4690 if (copy_to_user(arg, &features, sizeof(features)))
4691 return -EFAULT;
4692
4693 return 0;
4694}
4695
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004696static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004697 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004698 u64 change_mask, u64 flags, u64 supported_flags,
4699 u64 safe_set, u64 safe_clear)
4700{
David Sterbaf10152b2019-08-01 19:07:55 +02004701 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004702 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004703 u64 disallowed, unsupported;
4704 u64 set_mask = flags & change_mask;
4705 u64 clear_mask = ~flags & change_mask;
4706
4707 unsupported = set_mask & ~supported_flags;
4708 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004709 names = btrfs_printable_features(set, unsupported);
4710 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004711 btrfs_warn(fs_info,
4712 "this kernel does not support the %s feature bit%s",
4713 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004714 kfree(names);
4715 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004716 btrfs_warn(fs_info,
4717 "this kernel does not support %s bits 0x%llx",
4718 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004719 return -EOPNOTSUPP;
4720 }
4721
4722 disallowed = set_mask & ~safe_set;
4723 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004724 names = btrfs_printable_features(set, disallowed);
4725 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004726 btrfs_warn(fs_info,
4727 "can't set the %s feature bit%s while mounted",
4728 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004729 kfree(names);
4730 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004731 btrfs_warn(fs_info,
4732 "can't set %s bits 0x%llx while mounted",
4733 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004734 return -EPERM;
4735 }
4736
4737 disallowed = clear_mask & ~safe_clear;
4738 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004739 names = btrfs_printable_features(set, disallowed);
4740 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004741 btrfs_warn(fs_info,
4742 "can't clear the %s feature bit%s while mounted",
4743 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004744 kfree(names);
4745 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004746 btrfs_warn(fs_info,
4747 "can't clear %s bits 0x%llx while mounted",
4748 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004749 return -EPERM;
4750 }
4751
4752 return 0;
4753}
4754
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004755#define check_feature(fs_info, change_mask, flags, mask_base) \
4756check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004757 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4758 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4759 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4760
4761static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 struct inode *inode = file_inode(file);
4764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4765 struct btrfs_root *root = BTRFS_I(inode)->root;
4766 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004767 struct btrfs_ioctl_feature_flags flags[2];
4768 struct btrfs_trans_handle *trans;
4769 u64 newflags;
4770 int ret;
4771
4772 if (!capable(CAP_SYS_ADMIN))
4773 return -EPERM;
4774
4775 if (copy_from_user(flags, arg, sizeof(flags)))
4776 return -EFAULT;
4777
4778 /* Nothing to do */
4779 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4780 !flags[0].incompat_flags)
4781 return 0;
4782
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004783 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004784 flags[1].compat_flags, COMPAT);
4785 if (ret)
4786 return ret;
4787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004788 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004789 flags[1].compat_ro_flags, COMPAT_RO);
4790 if (ret)
4791 return ret;
4792
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004793 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004794 flags[1].incompat_flags, INCOMPAT);
4795 if (ret)
4796 return ret;
4797
David Sterba7ab19622016-05-04 11:32:00 +02004798 ret = mnt_want_write_file(file);
4799 if (ret)
4800 return ret;
4801
David Sterba8051aa12014-02-07 14:34:04 +01004802 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02004803 if (IS_ERR(trans)) {
4804 ret = PTR_ERR(trans);
4805 goto out_drop_write;
4806 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004807
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004808 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004809 newflags = btrfs_super_compat_flags(super_block);
4810 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4811 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4812 btrfs_set_super_compat_flags(super_block, newflags);
4813
4814 newflags = btrfs_super_compat_ro_flags(super_block);
4815 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4816 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4817 btrfs_set_super_compat_ro_flags(super_block, newflags);
4818
4819 newflags = btrfs_super_incompat_flags(super_block);
4820 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4821 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4822 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004823 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004824
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004825 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02004826out_drop_write:
4827 mnt_drop_write_file(file);
4828
4829 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004830}
4831
Josef Bacik2351f432017-09-27 10:43:13 -04004832static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4833{
4834 struct btrfs_ioctl_send_args *arg;
4835 int ret;
4836
4837 if (compat) {
4838#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4839 struct btrfs_ioctl_send_args_32 args32;
4840
4841 ret = copy_from_user(&args32, argp, sizeof(args32));
4842 if (ret)
4843 return -EFAULT;
4844 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4845 if (!arg)
4846 return -ENOMEM;
4847 arg->send_fd = args32.send_fd;
4848 arg->clone_sources_count = args32.clone_sources_count;
4849 arg->clone_sources = compat_ptr(args32.clone_sources);
4850 arg->parent_root = args32.parent_root;
4851 arg->flags = args32.flags;
4852 memcpy(arg->reserved, args32.reserved,
4853 sizeof(args32.reserved));
4854#else
4855 return -ENOTTY;
4856#endif
4857 } else {
4858 arg = memdup_user(argp, sizeof(*arg));
4859 if (IS_ERR(arg))
4860 return PTR_ERR(arg);
4861 }
4862 ret = btrfs_ioctl_send(file, arg);
4863 kfree(arg);
4864 return ret;
4865}
4866
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004867long btrfs_ioctl(struct file *file, unsigned int
4868 cmd, unsigned long arg)
4869{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004870 struct inode *inode = file_inode(file);
4871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4872 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004873 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004874
4875 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004876 case FS_IOC_GETVERSION:
4877 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004878 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004879 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05004880 case FS_IOC_SETFSLABEL:
4881 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004882 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004883 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004884 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004885 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004886 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004887 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004888 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004889 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004890 case BTRFS_IOC_SUBVOL_CREATE_V2:
4891 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004892 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03004893 return btrfs_ioctl_snap_destroy(file, argp, false);
4894 case BTRFS_IOC_SNAP_DESTROY_V2:
4895 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08004896 case BTRFS_IOC_SUBVOL_GETFLAGS:
4897 return btrfs_ioctl_subvol_getflags(file, argp);
4898 case BTRFS_IOC_SUBVOL_SETFLAGS:
4899 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004900 case BTRFS_IOC_DEFAULT_SUBVOL:
4901 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004902 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004903 return btrfs_ioctl_defrag(file, NULL);
4904 case BTRFS_IOC_DEFRAG_RANGE:
4905 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004906 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004907 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004908 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004909 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004910 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004911 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08004912 case BTRFS_IOC_RM_DEV_V2:
4913 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004914 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004915 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004916 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004917 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004918 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004919 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05004920 case BTRFS_IOC_TREE_SEARCH:
4921 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01004922 case BTRFS_IOC_TREE_SEARCH_V2:
4923 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05004924 case BTRFS_IOC_INO_LOOKUP:
4925 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004926 case BTRFS_IOC_INO_PATHS:
4927 return btrfs_ioctl_ino_to_path(root, argp);
4928 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004929 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4930 case BTRFS_IOC_LOGICAL_INO_V2:
4931 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00004932 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004933 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004934 case BTRFS_IOC_SYNC: {
4935 int ret;
4936
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02004937 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004938 if (ret)
4939 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004940 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02004941 /*
4942 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004943 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02004944 * processing uncleaned subvols.
4945 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004946 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004947 return ret;
4948 }
Sage Weil46204592010-10-29 15:41:32 -04004949 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004950 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004951 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004952 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004953 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004954 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004955 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004956 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01004957 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004958 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004959 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004960 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004961 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004962 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004963 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004964 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004965 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4966 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00004967#ifdef CONFIG_64BIT
4968 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4969 return btrfs_ioctl_set_received_subvol_32(file, argp);
4970#endif
Alexander Block31db9f72012-07-25 23:19:24 +02004971 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04004972 return _btrfs_ioctl_send(file, argp, false);
4973#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4974 case BTRFS_IOC_SEND_32:
4975 return _btrfs_ioctl_send(file, argp, true);
4976#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004977 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004978 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004979 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004980 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004981 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004982 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004983 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004984 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004985 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004986 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004987 case BTRFS_IOC_QUOTA_RESCAN:
4988 return btrfs_ioctl_quota_rescan(file, argp);
4989 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004990 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004991 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004992 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004993 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004994 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004995 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01004996 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004997 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004998 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004999 case BTRFS_IOC_SET_FEATURES:
5000 return btrfs_ioctl_set_features(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005001 case BTRFS_IOC_GET_SUBVOL_INFO:
5002 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005003 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5004 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005005 case BTRFS_IOC_INO_LOOKUP_USER:
5006 return btrfs_ioctl_ino_lookup_user(file, argp);
Boris Burkov14605402021-06-30 13:01:49 -07005007 case FS_IOC_ENABLE_VERITY:
5008 return fsverity_ioctl_enable(file, (const void __user *)argp);
5009 case FS_IOC_MEASURE_VERITY:
5010 return fsverity_ioctl_measure(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005011 }
5012
5013 return -ENOTTY;
5014}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005015
5016#ifdef CONFIG_COMPAT
5017long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5018{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005019 /*
5020 * These all access 32-bit values anyway so no further
5021 * handling is necessary.
5022 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005023 switch (cmd) {
Luke Dashjr4c63c242015-10-29 08:22:21 +00005024 case FS_IOC32_GETVERSION:
5025 cmd = FS_IOC_GETVERSION;
5026 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005027 }
5028
5029 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5030}
5031#endif