blob: 465ee00e29545008f21f150e45789ca5da0d9c1e [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008#include <linux/file.h>
9#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040010#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040011#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040014#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040015#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040016#include <linux/mount.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040017#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040018#include <linux/writeback.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040019#include <linux/compat.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040020#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040021#include <linux/xattr.h>
David Sterbaf54de062017-05-31 19:32:09 +020022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000024#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020025#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000026#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070027#include <linux/uaccess.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050028#include <linux/iversion.h>
Miklos Szeredi97fc2972021-04-07 14:36:43 +020029#include <linux/fileattr.h>
Boris Burkov14605402021-06-30 13:01:49 -070030#include <linux/fsverity.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040031#include "ctree.h"
32#include "disk-io.h"
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -030033#include "export.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include "transaction.h"
35#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040036#include "print-tree.h"
37#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040038#include "locking.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020039#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020041#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010042#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000043#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040044#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070045#include "qgroup.h"
Filipe Manana1ec9a1a2016-02-10 10:42:25 +000046#include "tree-log.h"
Anand Jainebb87652016-03-10 17:26:59 +080047#include "compression.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040048#include "space-info.h"
Josef Bacik86736342019-06-19 15:12:00 -040049#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040050#include "block-group.h"
Qu Wenruo22b398e2021-08-06 16:12:37 +080051#include "subpage.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Hugo Millsabccd002014-01-30 20:17:00 +000053#ifdef CONFIG_64BIT
54/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
55 * structures are incorrect, as the timespec structure from userspace
56 * is 4 bytes too small. We define these alternatives here to teach
57 * the kernel about the 32-bit struct packing.
58 */
59struct btrfs_ioctl_timespec_32 {
60 __u64 sec;
61 __u32 nsec;
62} __attribute__ ((__packed__));
63
64struct btrfs_ioctl_received_subvol_args_32 {
65 char uuid[BTRFS_UUID_SIZE]; /* in */
66 __u64 stransid; /* in */
67 __u64 rtransid; /* out */
68 struct btrfs_ioctl_timespec_32 stime; /* in */
69 struct btrfs_ioctl_timespec_32 rtime; /* out */
70 __u64 flags; /* in */
71 __u64 reserved[16]; /* in */
72} __attribute__ ((__packed__));
73
74#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
75 struct btrfs_ioctl_received_subvol_args_32)
76#endif
77
Josef Bacik2351f432017-09-27 10:43:13 -040078#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
79struct btrfs_ioctl_send_args_32 {
80 __s64 send_fd; /* in */
81 __u64 clone_sources_count; /* in */
82 compat_uptr_t clone_sources; /* in */
83 __u64 parent_root; /* in */
84 __u64 flags; /* in */
85 __u64 reserved[4]; /* in */
86} __attribute__ ((__packed__));
87
88#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
89 struct btrfs_ioctl_send_args_32)
90#endif
Hugo Millsabccd002014-01-30 20:17:00 +000091
Christoph Hellwig6cbff002009-04-17 10:37:41 +020092/* Mask out flags that are inappropriate for the given type of inode. */
David Sterba1905a0f2018-03-26 18:52:15 +020093static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
94 unsigned int flags)
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095{
David Sterba1905a0f2018-03-26 18:52:15 +020096 if (S_ISDIR(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097 return flags;
David Sterba1905a0f2018-03-26 18:52:15 +020098 else if (S_ISREG(inode->i_mode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +020099 return flags & ~FS_DIRSYNC_FL;
100 else
101 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
102}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400103
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200104/*
David Sterbaa157d4f2018-03-26 19:12:25 +0200105 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
106 * ioctl.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200107 */
Boris Burkov77eea052021-06-30 13:01:48 -0700108static unsigned int btrfs_inode_flags_to_fsflags(struct btrfs_inode *binode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200109{
110 unsigned int iflags = 0;
Boris Burkov77eea052021-06-30 13:01:48 -0700111 u32 flags = binode->flags;
Boris Burkov14605402021-06-30 13:01:49 -0700112 u32 ro_flags = binode->ro_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200113
114 if (flags & BTRFS_INODE_SYNC)
115 iflags |= FS_SYNC_FL;
116 if (flags & BTRFS_INODE_IMMUTABLE)
117 iflags |= FS_IMMUTABLE_FL;
118 if (flags & BTRFS_INODE_APPEND)
119 iflags |= FS_APPEND_FL;
120 if (flags & BTRFS_INODE_NODUMP)
121 iflags |= FS_NODUMP_FL;
122 if (flags & BTRFS_INODE_NOATIME)
123 iflags |= FS_NOATIME_FL;
124 if (flags & BTRFS_INODE_DIRSYNC)
125 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000126 if (flags & BTRFS_INODE_NODATACOW)
127 iflags |= FS_NOCOW_FL;
Boris Burkov14605402021-06-30 13:01:49 -0700128 if (ro_flags & BTRFS_INODE_RO_VERITY)
129 iflags |= FS_VERITY_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000130
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900131 if (flags & BTRFS_INODE_NOCOMPRESS)
Li Zefand0092bd2011-04-15 03:03:06 +0000132 iflags |= FS_NOCOMP_FL;
Satoru Takeuchi13f48dc2016-03-15 09:09:59 +0900133 else if (flags & BTRFS_INODE_COMPRESS)
134 iflags |= FS_COMPR_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200135
136 return iflags;
137}
138
139/*
140 * Update inode->i_flags based on the btrfs internal flags.
141 */
David Sterba7b6a2212018-03-26 18:40:21 +0200142void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143{
David Sterba5c57b8b2018-04-23 15:45:18 +0200144 struct btrfs_inode *binode = BTRFS_I(inode);
Filipe Manana3cc79392014-06-25 22:36:02 +0100145 unsigned int new_fl = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146
David Sterba5c57b8b2018-04-23 15:45:18 +0200147 if (binode->flags & BTRFS_INODE_SYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100148 new_fl |= S_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200149 if (binode->flags & BTRFS_INODE_IMMUTABLE)
Filipe Manana3cc79392014-06-25 22:36:02 +0100150 new_fl |= S_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200151 if (binode->flags & BTRFS_INODE_APPEND)
Filipe Manana3cc79392014-06-25 22:36:02 +0100152 new_fl |= S_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200153 if (binode->flags & BTRFS_INODE_NOATIME)
Filipe Manana3cc79392014-06-25 22:36:02 +0100154 new_fl |= S_NOATIME;
David Sterba5c57b8b2018-04-23 15:45:18 +0200155 if (binode->flags & BTRFS_INODE_DIRSYNC)
Filipe Manana3cc79392014-06-25 22:36:02 +0100156 new_fl |= S_DIRSYNC;
Boris Burkov14605402021-06-30 13:01:49 -0700157 if (binode->ro_flags & BTRFS_INODE_RO_VERITY)
158 new_fl |= S_VERITY;
Filipe Manana3cc79392014-06-25 22:36:02 +0100159
160 set_mask_bits(&inode->i_flags,
Boris Burkov14605402021-06-30 13:01:49 -0700161 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
162 S_VERITY, new_fl);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200163}
164
David Sterbaf37c5632020-07-10 09:49:56 +0200165/*
166 * Check if @flags are a supported and valid set of FS_*_FL flags and that
167 * the old and new flags are not conflicting
168 */
169static int check_fsflags(unsigned int old_flags, unsigned int flags)
Liu Bo75e7cb72011-03-22 10:12:20 +0000170{
171 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
172 FS_NOATIME_FL | FS_NODUMP_FL | \
173 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000174 FS_NOCOMP_FL | FS_COMPR_FL |
175 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000176 return -EOPNOTSUPP;
177
David Sterbaf37c5632020-07-10 09:49:56 +0200178 /* COMPR and NOCOMP on new/old are valid */
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
180 return -EINVAL;
181
David Sterbaf37c5632020-07-10 09:49:56 +0200182 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
183 return -EINVAL;
184
185 /* NOCOW and compression options are mutually exclusive */
186 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
187 return -EINVAL;
188 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
189 return -EINVAL;
190
Liu Bo75e7cb72011-03-22 10:12:20 +0000191 return 0;
192}
193
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900194static int check_fsflags_compatible(struct btrfs_fs_info *fs_info,
195 unsigned int flags)
196{
197 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
198 return -EPERM;
199
200 return 0;
201}
202
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200203/*
204 * Set flags/xflags from the internal inode flags. The remaining items of
205 * fsxattr are zeroed.
206 */
207int btrfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200208{
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200209 struct btrfs_inode *binode = BTRFS_I(d_inode(dentry));
210
Boris Burkov77eea052021-06-30 13:01:48 -0700211 fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(binode));
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200212 return 0;
213}
214
215int btrfs_fileattr_set(struct user_namespace *mnt_userns,
216 struct dentry *dentry, struct fileattr *fa)
217{
218 struct inode *inode = d_inode(dentry);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400219 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba5c57b8b2018-04-23 15:45:18 +0200220 struct btrfs_inode *binode = BTRFS_I(inode);
221 struct btrfs_root *root = binode->root;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 struct btrfs_trans_handle *trans;
Darrick J. Wong5aca2842019-07-01 08:25:34 -0700223 unsigned int fsflags, old_fsflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200224 int ret;
Anand Jainff9fef552019-04-20 19:48:53 +0800225 const char *comp = NULL;
David Sterbaf37c5632020-07-10 09:49:56 +0200226 u32 binode_flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227
Li Zefanb83cc962010-12-20 16:04:08 +0800228 if (btrfs_root_readonly(root))
229 return -EROFS;
230
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200231 if (fileattr_has_fsx(fa))
232 return -EOPNOTSUPP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200234 fsflags = btrfs_mask_fsflags_for_type(inode, fa->flags);
Boris Burkov77eea052021-06-30 13:01:48 -0700235 old_fsflags = btrfs_inode_flags_to_fsflags(binode);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200236 ret = check_fsflags(old_fsflags, fsflags);
Jan Karae7848682012-06-12 16:20:32 +0200237 if (ret)
238 return ret;
239
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900240 ret = check_fsflags_compatible(fs_info, fsflags);
241 if (ret)
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200242 return ret;
Naohiro Aotad206e9c2020-11-10 20:26:11 +0900243
David Sterbaf37c5632020-07-10 09:49:56 +0200244 binode_flags = binode->flags;
David Sterba5c57b8b2018-04-23 15:45:18 +0200245 if (fsflags & FS_SYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800246 binode_flags |= BTRFS_INODE_SYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200247 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800248 binode_flags &= ~BTRFS_INODE_SYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200249 if (fsflags & FS_IMMUTABLE_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800250 binode_flags |= BTRFS_INODE_IMMUTABLE;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800252 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
David Sterba5c57b8b2018-04-23 15:45:18 +0200253 if (fsflags & FS_APPEND_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800254 binode_flags |= BTRFS_INODE_APPEND;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200255 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800256 binode_flags &= ~BTRFS_INODE_APPEND;
David Sterba5c57b8b2018-04-23 15:45:18 +0200257 if (fsflags & FS_NODUMP_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800258 binode_flags |= BTRFS_INODE_NODUMP;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200259 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800260 binode_flags &= ~BTRFS_INODE_NODUMP;
David Sterba5c57b8b2018-04-23 15:45:18 +0200261 if (fsflags & FS_NOATIME_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800262 binode_flags |= BTRFS_INODE_NOATIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200263 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800264 binode_flags &= ~BTRFS_INODE_NOATIME;
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200265
266 /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
267 if (!fa->flags_valid) {
268 /* 1 item for the inode */
269 trans = btrfs_start_transaction(root, 1);
Ritesh Harjani9b8a2332021-04-30 21:30:55 +0530270 if (IS_ERR(trans))
271 return PTR_ERR(trans);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200272 goto update_flags;
273 }
274
David Sterba5c57b8b2018-04-23 15:45:18 +0200275 if (fsflags & FS_DIRSYNC_FL)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800276 binode_flags |= BTRFS_INODE_DIRSYNC;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200277 else
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800278 binode_flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba5c57b8b2018-04-23 15:45:18 +0200279 if (fsflags & FS_NOCOW_FL) {
Anand Jain44e51942019-04-20 19:48:57 +0800280 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600281 /*
282 * It's safe to turn csums off here, no extents exist.
283 * Otherwise we want the flag to reflect the real COW
284 * status of the file and will not set it.
285 */
286 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800287 binode_flags |= BTRFS_INODE_NODATACOW |
288 BTRFS_INODE_NODATASUM;
David Sterba7e97b8d2012-09-07 05:56:55 -0600289 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800290 binode_flags |= BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600291 }
292 } else {
293 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400294 * Revert back under same assumptions as above
David Sterba7e97b8d2012-09-07 05:56:55 -0600295 */
Anand Jain44e51942019-04-20 19:48:57 +0800296 if (S_ISREG(inode->i_mode)) {
David Sterba7e97b8d2012-09-07 05:56:55 -0600297 if (inode->i_size == 0)
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800298 binode_flags &= ~(BTRFS_INODE_NODATACOW |
299 BTRFS_INODE_NODATASUM);
David Sterba7e97b8d2012-09-07 05:56:55 -0600300 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800301 binode_flags &= ~BTRFS_INODE_NODATACOW;
David Sterba7e97b8d2012-09-07 05:56:55 -0600302 }
303 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200304
Liu Bo75e7cb72011-03-22 10:12:20 +0000305 /*
306 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
307 * flag may be changed automatically if compression code won't make
308 * things smaller.
309 */
David Sterba5c57b8b2018-04-23 15:45:18 +0200310 if (fsflags & FS_NOCOMP_FL) {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800311 binode_flags &= ~BTRFS_INODE_COMPRESS;
312 binode_flags |= BTRFS_INODE_NOCOMPRESS;
David Sterba5c57b8b2018-04-23 15:45:18 +0200313 } else if (fsflags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000314
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200315 if (IS_SWAPFILE(inode))
316 return -ETXTBSY;
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700317
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800318 binode_flags |= BTRFS_INODE_COMPRESS;
319 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000320
David Sterba93370502017-10-31 17:32:41 +0100321 comp = btrfs_compress_type2str(fs_info->compress_type);
322 if (!comp || comp[0] == 0)
323 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Li Zefanebcb9042011-04-15 03:03:17 +0000324 } else {
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800325 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000326 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200327
Anand Jainff9fef552019-04-20 19:48:53 +0800328 /*
329 * 1 for inode item
330 * 2 for properties
331 */
332 trans = btrfs_start_transaction(root, 3);
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200333 if (IS_ERR(trans))
334 return PTR_ERR(trans);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335
Anand Jainff9fef552019-04-20 19:48:53 +0800336 if (comp) {
337 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
338 strlen(comp), 0);
339 if (ret) {
340 btrfs_abort_transaction(trans, ret);
341 goto out_end_trans;
342 }
Anand Jainff9fef552019-04-20 19:48:53 +0800343 } else {
344 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
345 0, 0);
346 if (ret && ret != -ENODATA) {
347 btrfs_abort_transaction(trans, ret);
348 goto out_end_trans;
349 }
350 }
351
Miklos Szeredi97fc2972021-04-07 14:36:43 +0200352update_flags:
Anand Jaind2b8fcf2019-04-20 19:48:55 +0800353 binode->flags = binode_flags;
David Sterba7b6a2212018-03-26 18:40:21 +0200354 btrfs_sync_inode_flags_to_i_flags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400355 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700356 inode->i_ctime = current_time(inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200357 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200358
Anand Jainff9fef552019-04-20 19:48:53 +0800359 out_end_trans:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400360 btrfs_end_transaction(trans);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000361 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200362}
363
David Sterba0d7ed322021-05-14 17:42:30 +0200364/*
365 * Start exclusive operation @type, return true on success
366 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500367bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
368 enum btrfs_exclusive_operation type)
369{
David Sterba0d7ed322021-05-14 17:42:30 +0200370 bool ret = false;
371
372 spin_lock(&fs_info->super_lock);
373 if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
374 fs_info->exclusive_operation = type;
375 ret = true;
376 }
377 spin_unlock(&fs_info->super_lock);
378
379 return ret;
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500380}
381
David Sterba578bda92021-05-18 21:05:52 +0200382/*
383 * Conditionally allow to enter the exclusive operation in case it's compatible
384 * with the running one. This must be paired with btrfs_exclop_start_unlock and
385 * btrfs_exclop_finish.
386 *
387 * Compatibility:
388 * - the same type is already running
389 * - not BTRFS_EXCLOP_NONE - this is intentionally incompatible and the caller
390 * must check the condition first that would allow none -> @type
391 */
392bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,
393 enum btrfs_exclusive_operation type)
394{
395 spin_lock(&fs_info->super_lock);
396 if (fs_info->exclusive_operation == type)
397 return true;
398
399 spin_unlock(&fs_info->super_lock);
400 return false;
401}
402
403void btrfs_exclop_start_unlock(struct btrfs_fs_info *fs_info)
404{
405 spin_unlock(&fs_info->super_lock);
406}
407
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500408void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
409{
David Sterba0d7ed322021-05-14 17:42:30 +0200410 spin_lock(&fs_info->super_lock);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500411 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
David Sterba0d7ed322021-05-14 17:42:30 +0200412 spin_unlock(&fs_info->super_lock);
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500413 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500414}
415
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200416static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
417{
Al Viro496ad9a2013-01-23 17:07:38 -0500418 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200419
420 return put_user(inode->i_generation, arg);
421}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400422
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -0300423static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
424 void __user *arg)
Li Dongyangf7039b12011-03-24 10:24:28 +0000425{
Li Dongyangf7039b12011-03-24 10:24:28 +0000426 struct btrfs_device *device;
427 struct request_queue *q;
428 struct fstrim_range range;
429 u64 minlen = ULLONG_MAX;
430 u64 num_devices = 0;
431 int ret;
432
433 if (!capable(CAP_SYS_ADMIN))
434 return -EPERM;
435
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000436 /*
Naohiro Aota1cb3dc32021-02-04 19:21:46 +0900437 * btrfs_trim_block_group() depends on space cache, which is not
438 * available in zoned filesystem. So, disallow fitrim on a zoned
439 * filesystem for now.
440 */
441 if (btrfs_is_zoned(fs_info))
442 return -EOPNOTSUPP;
443
444 /*
Filipe Mananaf35f06c2019-03-26 10:49:56 +0000445 * If the fs is mounted with nologreplay, which requires it to be
446 * mounted in RO mode as well, we can not allow discard on free space
447 * inside block groups, because log trees refer to extents that are not
448 * pinned in a block group's free space cache (pinning the extents is
449 * precisely the first phase of replaying a log tree).
450 */
451 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
452 return -EROFS;
453
Xiao Guangrong1f781602011-04-20 10:09:16 +0000454 rcu_read_lock();
455 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
456 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000457 if (!device->bdev)
458 continue;
459 q = bdev_get_queue(device->bdev);
460 if (blk_queue_discard(q)) {
461 num_devices++;
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100462 minlen = min_t(u64, q->limits.discard_granularity,
Li Dongyangf7039b12011-03-24 10:24:28 +0000463 minlen);
464 }
465 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000466 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200467
Li Dongyangf7039b12011-03-24 10:24:28 +0000468 if (!num_devices)
469 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000470 if (copy_from_user(&range, arg, sizeof(range)))
471 return -EFAULT;
Qu Wenruo6ba9fc82018-09-07 14:16:24 +0800472
473 /*
474 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
475 * block group is in the logical address space, which can be any
476 * sectorsize aligned bytenr in the range [0, U64_MAX].
477 */
478 if (range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200479 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000480
481 range.minlen = max(range.minlen, minlen);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400482 ret = btrfs_trim_fs(fs_info, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000483 if (ret < 0)
484 return ret;
485
486 if (copy_to_user(arg, &range, sizeof(range)))
487 return -EFAULT;
488
489 return 0;
490}
491
David Sterbae1f60a62019-10-01 19:57:39 +0200492int __pure btrfs_is_empty_uuid(u8 *uuid)
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200493{
Chris Mason46e0f662013-11-15 12:14:55 +0100494 int i;
495
496 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
497 if (uuid[i])
498 return 0;
499 }
500 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200501}
502
Christian Brauner4d4340c2021-07-27 12:48:52 +0200503static noinline int create_subvol(struct user_namespace *mnt_userns,
504 struct inode *dir, struct dentry *dentry,
David Sterba52f75f42017-02-14 18:33:53 +0100505 const char *name, int namelen,
Miao Xie8696c532013-02-07 06:02:44 +0000506 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400508 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 struct btrfs_trans_handle *trans;
510 struct btrfs_key key;
David Sterba49a3c4d2016-03-24 17:49:22 +0100511 struct btrfs_root_item *root_item;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 struct btrfs_inode_item *inode_item;
513 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000514 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400515 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000516 struct btrfs_block_rsv block_rsv;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700517 struct timespec64 cur_time = current_time(dir);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900518 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 int ret;
520 int err;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800521 dev_t anon_dev = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 u64 objectid;
Chris Mason3de45862008-11-17 21:02:50 -0500523 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524
David Sterba49a3c4d2016-03-24 17:49:22 +0100525 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
526 if (!root_item)
527 return -ENOMEM;
528
Nikolay Borisov543068a2020-12-07 17:32:33 +0200529 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400530 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100531 goto fail_free;
Josef Bacik6a912212010-11-20 09:48:00 +0000532
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800533 ret = get_anon_bdev(&anon_dev);
534 if (ret < 0)
535 goto fail_free;
536
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800537 /*
538 * Don't create subvolume whose level is not zero. Or qgroup will be
Nicholas D Steeves01327612016-05-19 21:18:45 -0400539 * screwed up since it assumes subvolume qgroup's level to be 0.
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800540 */
David Sterba49a3c4d2016-03-24 17:49:22 +0100541 if (btrfs_qgroup_level(objectid)) {
542 ret = -ENOSPC;
543 goto fail_free;
544 }
Qu Wenruoe09fe2d2015-02-27 16:24:23 +0800545
Miao Xied5c12072013-02-28 10:04:33 +0000546 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400547 /*
Miao Xied5c12072013-02-28 10:04:33 +0000548 * The same as the snapshot creation, please see the comment
549 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400550 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +0800551 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
Miao Xied5c12072013-02-28 10:04:33 +0000552 if (ret)
David Sterba49a3c4d2016-03-24 17:49:22 +0100553 goto fail_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554
Miao Xied5c12072013-02-28 10:04:33 +0000555 trans = btrfs_start_transaction(root, 0);
556 if (IS_ERR(trans)) {
557 ret = PTR_ERR(trans);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800558 btrfs_subvolume_release_metadata(root, &block_rsv);
David Sterba49a3c4d2016-03-24 17:49:22 +0100559 goto fail_free;
Miao Xied5c12072013-02-28 10:04:33 +0000560 }
561 trans->block_rsv = &block_rsv;
562 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563
Lu Fengqia93774222018-07-18 14:45:41 +0800564 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200565 if (ret)
566 goto fail;
567
Josef Bacik9631e4c2020-08-20 11:46:03 -0400568 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
569 BTRFS_NESTING_NORMAL);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400570 if (IS_ERR(leaf)) {
571 ret = PTR_ERR(leaf);
572 goto fail;
573 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400574
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 btrfs_mark_buffer_dirty(leaf);
576
David Sterba49a3c4d2016-03-24 17:49:22 +0100577 inode_item = &root_item->inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800578 btrfs_set_stack_inode_generation(inode_item, 1);
579 btrfs_set_stack_inode_size(inode_item, 3);
580 btrfs_set_stack_inode_nlink(inode_item, 1);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400581 btrfs_set_stack_inode_nbytes(inode_item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400582 fs_info->nodesize);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800583 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400584
David Sterba49a3c4d2016-03-24 17:49:22 +0100585 btrfs_set_root_flags(root_item, 0);
586 btrfs_set_root_limit(root_item, 0);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800587 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000588
David Sterba49a3c4d2016-03-24 17:49:22 +0100589 btrfs_set_root_bytenr(root_item, leaf->start);
590 btrfs_set_root_generation(root_item, trans->transid);
591 btrfs_set_root_level(root_item, 0);
592 btrfs_set_root_refs(root_item, 1);
593 btrfs_set_root_used(root_item, leaf->len);
594 btrfs_set_root_last_snapshot(root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595
David Sterba49a3c4d2016-03-24 17:49:22 +0100596 btrfs_set_root_generation_v2(root_item,
597 btrfs_root_generation(root_item));
Andy Shevchenko807fc792020-02-24 17:37:51 +0200598 generate_random_guid(root_item->uuid);
David Sterba49a3c4d2016-03-24 17:49:22 +0100599 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
600 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
601 root_item->ctime = root_item->otime;
602 btrfs_set_root_ctransid(root_item, trans->transid);
603 btrfs_set_root_otransid(root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400604
Chris Mason925baed2008-06-25 16:01:30 -0400605 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400606
Nikolay Borisov69948022020-12-07 17:32:37 +0200607 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608
609 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +0200611 key.type = BTRFS_ROOT_ITEM_KEY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400612 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
David Sterba49a3c4d2016-03-24 17:49:22 +0100613 root_item);
Filipe Manana67addf22021-04-20 10:55:12 +0100614 if (ret) {
615 /*
616 * Since we don't abort the transaction in this case, free the
617 * tree block so that we don't leak space and leave the
618 * filesystem in an inconsistent state (an extent item in the
619 * extent tree without backreferences). Also no need to have
620 * the tree block locked since it is not in any tree at this
621 * point, so no other task can find it and use it.
622 */
623 btrfs_free_tree_block(trans, root, leaf, 0, 1);
624 free_extent_buffer(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 goto fail;
Filipe Manana67addf22021-04-20 10:55:12 +0100626 }
627
628 free_extent_buffer(leaf);
629 leaf = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630
Yan, Zheng76dda932009-09-21 16:00:26 -0400631 key.offset = (u64)-1;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800632 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100633 if (IS_ERR(new_root)) {
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800634 free_anon_bdev(anon_dev);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 ret = PTR_ERR(new_root);
Jeff Mahoney66642832016-06-10 18:19:25 -0400636 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 goto fail;
638 }
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800639 /* Freeing will be done in btrfs_put_root() of new_root */
640 anon_dev = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -0400641
Josef Bacik221581e2021-03-12 15:25:06 -0500642 ret = btrfs_record_root_in_trans(trans, new_root);
643 if (ret) {
644 btrfs_put_root(new_root);
645 btrfs_abort_transaction(trans, ret);
646 goto fail;
647 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400648
Christian Brauner4d4340c2021-07-27 12:48:52 +0200649 ret = btrfs_create_subvol_root(trans, new_root, root, mnt_userns);
Josef Bacik00246522020-01-24 09:33:01 -0500650 btrfs_put_root(new_root);
Mark Fashehce598972011-07-26 11:32:23 -0700651 if (ret) {
652 /* We potentially lose an unused inode item here */
Jeff Mahoney66642832016-06-10 18:19:25 -0400653 btrfs_abort_transaction(trans, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700654 goto fail;
655 }
656
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400657 /*
658 * insert the directory item
659 */
Nikolay Borisov877574e2017-02-20 13:50:33 +0200660 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400662 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100663 goto fail;
664 }
Chris Mason3de45862008-11-17 21:02:50 -0500665
Lu Fengqi684572d2018-08-04 21:10:57 +0800666 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Chris Mason3de45862008-11-17 21:02:50 -0500667 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100668 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400669 btrfs_abort_transaction(trans, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400670 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100671 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500672
Nikolay Borisov6ef06d22017-02-20 13:50:34 +0200673 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200674 ret = btrfs_update_inode(trans, root, BTRFS_I(dir));
Josef Bacikc7e54b52019-12-06 09:37:15 -0500675 if (ret) {
676 btrfs_abort_transaction(trans, ret);
677 goto fail;
678 }
Yan Zheng52c26172009-01-05 15:43:43 -0500679
Lu Fengqi6025c192018-08-01 11:32:29 +0800680 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200681 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Josef Bacikc7e54b52019-12-06 09:37:15 -0500682 if (ret) {
683 btrfs_abort_transaction(trans, ret);
684 goto fail;
685 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500686
Lu Fengqicdb345a2018-05-29 15:01:53 +0800687 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400688 BTRFS_UUID_KEY_SUBVOL, objectid);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200689 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400690 btrfs_abort_transaction(trans, ret);
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400692fail:
David Sterba49a3c4d2016-03-24 17:49:22 +0100693 kfree(root_item);
Miao Xied5c12072013-02-28 10:04:33 +0000694 trans->block_rsv = NULL;
695 trans->bytes_reserved = 0;
Qu Wenruoe85fde52020-07-24 14:46:10 +0800696 btrfs_subvolume_release_metadata(root, &block_rsv);
Liu Bode6e8202014-01-09 14:57:06 +0800697
Nikolay Borisov9babda92020-03-13 17:23:20 +0200698 err = btrfs_commit_transaction(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400699 if (err && !ret)
700 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500701
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900702 if (!ret) {
703 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800704 if (IS_ERR(inode))
705 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900706 d_instantiate(dentry, inode);
707 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 return ret;
David Sterba49a3c4d2016-03-24 17:49:22 +0100709
710fail_free:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800711 if (anon_dev)
712 free_anon_bdev(anon_dev);
David Sterba49a3c4d2016-03-24 17:49:22 +0100713 kfree(root_item);
714 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400715}
716
Miao Xiee9662f72013-02-28 10:01:15 +0000717static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200718 struct dentry *dentry, bool readonly,
Miao Xiee9662f72013-02-28 10:01:15 +0000719 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000722 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723 struct btrfs_pending_snapshot *pending_snapshot;
724 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000725 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800727 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400728 return -EINVAL;
729
Omar Sandovaleede2bf2016-11-03 10:28:12 -0700730 if (atomic_read(&root->nr_swapfiles)) {
731 btrfs_warn(fs_info,
732 "cannot snapshot subvolume with active swapfile");
733 return -ETXTBSY;
734 }
735
David Sterba23269bf2017-02-13 11:03:44 +0100736 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
David Sterbaa1ee7362015-11-10 18:53:56 +0100737 if (!pending_snapshot)
738 return -ENOMEM;
739
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800740 ret = get_anon_bdev(&pending_snapshot->anon_dev);
741 if (ret < 0)
742 goto free_pending;
David Sterbab0c0ea62015-11-10 18:54:00 +0100743 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
David Sterba23269bf2017-02-13 11:03:44 +0100744 GFP_KERNEL);
David Sterba8546b572015-11-10 18:54:03 +0100745 pending_snapshot->path = btrfs_alloc_path();
746 if (!pending_snapshot->root_item || !pending_snapshot->path) {
David Sterbab0c0ea62015-11-10 18:54:00 +0100747 ret = -ENOMEM;
748 goto free_pending;
749 }
750
Miao Xie66d8f3d2012-09-06 04:02:28 -0600751 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
752 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000753 /*
754 * 1 - parent dir inode
755 * 2 - dir entries
756 * 1 - root item
757 * 2 - root ref/backref
758 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200759 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000760 */
761 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200762 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400763 false);
Miao Xied5c12072013-02-28 10:04:33 +0000764 if (ret)
Robbie Koc11fbb62020-05-14 17:19:18 +0800765 goto free_pending;
Miao Xied5c12072013-02-28 10:04:33 +0000766
Yan, Zhenga22285a2010-05-16 10:48:46 -0400767 pending_snapshot->dentry = dentry;
768 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800769 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000770 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000771 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400772
Miao Xied5c12072013-02-28 10:04:33 +0000773 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774 if (IS_ERR(trans)) {
775 ret = PTR_ERR(trans);
776 goto fail;
777 }
778
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400779 spin_lock(&fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400780 list_add(&pending_snapshot->list,
781 &trans->transaction->pending_snapshots);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400782 spin_unlock(&fs_info->trans_lock);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200783
784 ret = btrfs_commit_transaction(trans);
Miao Xieaec80302013-03-04 09:44:29 +0000785 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400786 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400787
788 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400789 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000790 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400791
Chris Masond3797302014-10-15 13:50:56 -0700792 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
793 if (ret)
794 goto fail;
795
David Howells2b0143b2015-03-17 22:25:59 +0000796 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000797 if (IS_ERR(inode)) {
798 ret = PTR_ERR(inode);
799 goto fail;
800 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900801
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000802 d_instantiate(dentry, inode);
803 ret = 0;
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800804 pending_snapshot->anon_dev = 0;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000805fail:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800806 /* Prevent double freeing of anon_dev */
807 if (ret && pending_snapshot->snap)
808 pending_snapshot->snap->anon_dev = 0;
Josef Bacik00246522020-01-24 09:33:01 -0500809 btrfs_put_root(pending_snapshot->snap);
Qu Wenruoe85fde52020-07-24 14:46:10 +0800810 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
David Sterbab0c0ea62015-11-10 18:54:00 +0100811free_pending:
Qu Wenruo2dfb1e42020-06-16 10:17:36 +0800812 if (pending_snapshot->anon_dev)
813 free_anon_bdev(pending_snapshot->anon_dev);
David Sterbab0c0ea62015-11-10 18:54:00 +0100814 kfree(pending_snapshot->root_item);
David Sterba8546b572015-11-10 18:54:03 +0100815 btrfs_free_path(pending_snapshot->path);
David Sterbaa1ee7362015-11-10 18:53:56 +0100816 kfree(pending_snapshot);
817
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400818 return ret;
819}
820
Sage Weil4260f7c2010-10-29 15:46:43 -0400821/* copy of may_delete in fs/namei.c()
822 * Check whether we can remove a link victim from directory dir, check
823 * whether the type of victim is right.
824 * 1. We can't do it if dir is read-only (done in permission())
825 * 2. We should have write and exec permissions on dir
826 * 3. We can't remove anything from append-only dir
827 * 4. We can't do anything with immutable dir (done in permission())
828 * 5. If the sticky bit on dir is set we should either
829 * a. be owner of dir, or
830 * b. be owner of victim, or
831 * c. have CAP_FOWNER capability
Nicholas D Steeves01327612016-05-19 21:18:45 -0400832 * 6. If the victim is append-only or immutable we can't do anything with
Sage Weil4260f7c2010-10-29 15:46:43 -0400833 * links pointing to it.
834 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
835 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
836 * 9. We can't remove a root or mountpoint.
837 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
838 * nfs_async_unlink().
839 */
840
Christian Braunerc4ed5332021-07-27 12:48:53 +0200841static int btrfs_may_delete(struct user_namespace *mnt_userns,
842 struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400843{
844 int error;
845
David Howells2b0143b2015-03-17 22:25:59 +0000846 if (d_really_is_negative(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400847 return -ENOENT;
848
David Howells2b0143b2015-03-17 22:25:59 +0000849 BUG_ON(d_inode(victim->d_parent) != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400850 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400851
Christian Braunerc4ed5332021-07-27 12:48:53 +0200852 error = inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -0400853 if (error)
854 return error;
855 if (IS_APPEND(dir))
856 return -EPERM;
Christian Braunerc4ed5332021-07-27 12:48:53 +0200857 if (check_sticky(mnt_userns, dir, d_inode(victim)) ||
Christian Braunerba73d982021-01-21 14:19:31 +0100858 IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
859 IS_SWAPFILE(d_inode(victim)))
Sage Weil4260f7c2010-10-29 15:46:43 -0400860 return -EPERM;
861 if (isdir) {
David Howellse36cb0b2015-01-29 12:02:35 +0000862 if (!d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400863 return -ENOTDIR;
864 if (IS_ROOT(victim))
865 return -EBUSY;
David Howellse36cb0b2015-01-29 12:02:35 +0000866 } else if (d_is_dir(victim))
Sage Weil4260f7c2010-10-29 15:46:43 -0400867 return -EISDIR;
868 if (IS_DEADDIR(dir))
869 return -ENOENT;
870 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
871 return -EBUSY;
872 return 0;
873}
874
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400875/* copy of may_create in fs/namei.c() */
Christian Brauner4d4340c2021-07-27 12:48:52 +0200876static inline int btrfs_may_create(struct user_namespace *mnt_userns,
877 struct inode *dir, struct dentry *child)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400878{
David Howells2b0143b2015-03-17 22:25:59 +0000879 if (d_really_is_positive(child))
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400880 return -EEXIST;
881 if (IS_DEADDIR(dir))
882 return -ENOENT;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200883 if (!fsuidgid_has_mapping(dir->i_sb, mnt_userns))
Christian Brauner5474bf42021-07-27 12:48:51 +0200884 return -EOVERFLOW;
Christian Brauner4d4340c2021-07-27 12:48:52 +0200885 return inode_permission(mnt_userns, dir, MAY_WRITE | MAY_EXEC);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400886}
887
888/*
889 * Create a new subvolume below @parent. This is largely modeled after
890 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
891 * inside this filesystem so it's quite a bit simpler.
892 */
Al Viro92872092016-11-20 19:34:31 -0500893static noinline int btrfs_mksubvol(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200894 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +0100895 const char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400896 struct btrfs_root *snap_src,
Nikolay Borisov9babda92020-03-13 17:23:20 +0200897 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000898 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400899{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400900 struct inode *dir = d_inode(parent->dentry);
901 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400902 struct dentry *dentry;
903 int error;
904
Al Viro00235412016-05-26 00:05:12 -0400905 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
906 if (error == -EINTR)
907 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400908
Christian Brauner4d4340c2021-07-27 12:48:52 +0200909 dentry = lookup_one(mnt_userns, name, parent->dentry, namelen);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400910 error = PTR_ERR(dentry);
911 if (IS_ERR(dentry))
912 goto out_unlock;
913
Christian Brauner4d4340c2021-07-27 12:48:52 +0200914 error = btrfs_may_create(mnt_userns, dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400915 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600916 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400917
Chris Mason9c520572012-12-17 14:26:57 -0500918 /*
919 * even if this name doesn't exist, we may get hash collisions.
920 * check for them now when we can safely fail
921 */
922 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
923 dir->i_ino, name,
924 namelen);
925 if (error)
926 goto out_dput;
927
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400928 down_read(&fs_info->subvol_sem);
Yan, Zheng76dda932009-09-21 16:00:26 -0400929
930 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
931 goto out_up_read;
932
Nikolay Borisov9babda92020-03-13 17:23:20 +0200933 if (snap_src)
934 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
935 else
Christian Brauner4d4340c2021-07-27 12:48:52 +0200936 error = create_subvol(mnt_userns, dir, dentry, name, namelen, inherit);
Nikolay Borisov9babda92020-03-13 17:23:20 +0200937
Yan, Zheng76dda932009-09-21 16:00:26 -0400938 if (!error)
939 fsnotify_mkdir(dir, dentry);
940out_up_read:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 up_read(&fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400942out_dput:
943 dput(dentry);
944out_unlock:
Josef Bacik64708532021-02-10 17:14:34 -0500945 btrfs_inode_unlock(dir, 0);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400946 return error;
947}
948
Robbie Koc11fbb62020-05-14 17:19:18 +0800949static noinline int btrfs_mksnapshot(const struct path *parent,
Christian Brauner4d4340c2021-07-27 12:48:52 +0200950 struct user_namespace *mnt_userns,
Robbie Koc11fbb62020-05-14 17:19:18 +0800951 const char *name, int namelen,
952 struct btrfs_root *root,
953 bool readonly,
954 struct btrfs_qgroup_inherit *inherit)
955{
956 int ret;
957 bool snapshot_force_cow = false;
958
959 /*
960 * Force new buffered writes to reserve space even when NOCOW is
961 * possible. This is to avoid later writeback (running dealloc) to
962 * fallback to COW mode and unexpectedly fail with ENOSPC.
963 */
964 btrfs_drew_read_lock(&root->snapshot_lock);
965
Filipe Mananaf9baa502021-04-22 12:08:05 +0100966 ret = btrfs_start_delalloc_snapshot(root, false);
Robbie Koc11fbb62020-05-14 17:19:18 +0800967 if (ret)
968 goto out;
969
970 /*
971 * All previous writes have started writeback in NOCOW mode, so now
972 * we force future writes to fallback to COW mode during snapshot
973 * creation.
974 */
975 atomic_inc(&root->snapshot_force_cow);
976 snapshot_force_cow = true;
977
978 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
979
Christian Brauner4d4340c2021-07-27 12:48:52 +0200980 ret = btrfs_mksubvol(parent, mnt_userns, name, namelen,
Robbie Koc11fbb62020-05-14 17:19:18 +0800981 root, readonly, inherit);
982out:
983 if (snapshot_force_cow)
984 atomic_dec(&root->snapshot_force_cow);
985 btrfs_drew_read_unlock(&root->snapshot_lock);
986 return ret;
987}
988
Chris Mason4cb53002011-05-24 15:35:30 -0400989/*
990 * When we're defragging a range, we don't want to kick it off again
991 * if it is really just waiting for delalloc to send it down.
992 * If we find a nice big extent or delalloc range for the bytes in the
993 * file you want to defrag, we return 0 to let you know to skip this
994 * part of the file
995 */
David Sterbaaab110a2014-07-29 17:32:10 +0200996static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -0400997{
998 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
999 struct extent_map *em = NULL;
1000 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001001 const u32 sectorsize = btrfs_sb(inode->i_sb)->sectorsize;
Chris Mason4cb53002011-05-24 15:35:30 -04001002 u64 end;
1003
1004 read_lock(&em_tree->lock);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001005 em = lookup_extent_mapping(em_tree, offset, sectorsize);
Chris Mason4cb53002011-05-24 15:35:30 -04001006 read_unlock(&em_tree->lock);
1007
1008 if (em) {
1009 end = extent_map_end(em);
1010 free_extent_map(em);
1011 if (end - offset > thresh)
1012 return 0;
1013 }
1014 /* if we already have a nice delalloc here, just stop */
1015 thresh /= 2;
1016 end = count_range_bits(io_tree, &offset, offset + thresh,
1017 thresh, EXTENT_DELALLOC, 1);
1018 if (end >= thresh)
1019 return 0;
1020 return 1;
1021}
1022
1023/*
1024 * helper function to walk through a file and find extents
1025 * newer than a specific transid, and smaller than thresh.
1026 *
1027 * This is used by the defragging code to find new and small
1028 * extents
1029 */
1030static int find_new_extents(struct btrfs_root *root,
1031 struct inode *inode, u64 newer_than,
David Sterbaaab110a2014-07-29 17:32:10 +02001032 u64 *off, u32 thresh)
Chris Mason4cb53002011-05-24 15:35:30 -04001033{
1034 struct btrfs_path *path;
1035 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -04001036 struct extent_buffer *leaf;
1037 struct btrfs_file_extent_item *extent;
1038 int type;
1039 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001040 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason4cb53002011-05-24 15:35:30 -04001041
1042 path = btrfs_alloc_path();
1043 if (!path)
1044 return -ENOMEM;
1045
David Sterbaa4689d22011-05-31 17:08:14 +00001046 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -04001047 min_key.type = BTRFS_EXTENT_DATA_KEY;
1048 min_key.offset = *off;
1049
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301050 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001051 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -04001052 if (ret != 0)
1053 goto none;
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001054process_slot:
David Sterbaa4689d22011-05-31 17:08:14 +00001055 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -04001056 goto none;
1057 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1058 goto none;
1059
1060 leaf = path->nodes[0];
1061 extent = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_file_extent_item);
1063
1064 type = btrfs_file_extent_type(leaf, extent);
1065 if (type == BTRFS_FILE_EXTENT_REG &&
1066 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1067 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1068 *off = min_key.offset;
1069 btrfs_free_path(path);
1070 return 0;
1071 }
1072
Filipe Mananaf094c9bd2014-03-12 01:28:24 +00001073 path->slots[0]++;
1074 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1075 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1076 goto process_slot;
1077 }
1078
Chris Mason4cb53002011-05-24 15:35:30 -04001079 if (min_key.offset == (u64)-1)
1080 goto none;
1081
1082 min_key.offset++;
1083 btrfs_release_path(path);
1084 }
1085none:
1086 btrfs_free_path(path);
1087 return -ENOENT;
1088}
1089
Li Zefan6c282eb2012-06-11 16:03:35 +08001090static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -04001091{
1092 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -05001093 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +08001094 struct extent_map *em;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001095 const u32 sectorsize = BTRFS_I(inode)->root->fs_info->sectorsize;
Chris Mason940100a2010-03-10 10:52:59 -05001096
1097 /*
1098 * hopefully we have this extent in the tree already, try without
1099 * the full extent lock
1100 */
1101 read_lock(&em_tree->lock);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001102 em = lookup_extent_mapping(em_tree, start, sectorsize);
Chris Mason940100a2010-03-10 10:52:59 -05001103 read_unlock(&em_tree->lock);
1104
1105 if (!em) {
Filipe Manana308d9802014-03-11 13:56:15 +00001106 struct extent_state *cached = NULL;
Qu Wenruo76068ca2021-08-06 16:12:34 +08001107 u64 end = start + sectorsize - 1;
Filipe Manana308d9802014-03-11 13:56:15 +00001108
Chris Mason940100a2010-03-10 10:52:59 -05001109 /* get the big lock and read metadata off disk */
David Sterbaff13db42015-12-03 14:30:40 +01001110 lock_extent_bits(io_tree, start, end, &cached);
Qu Wenruo76068ca2021-08-06 16:12:34 +08001111 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, sectorsize);
David Sterbae43bbe52017-12-12 21:43:52 +01001112 unlock_extent_cached(io_tree, start, end, &cached);
Chris Mason940100a2010-03-10 10:52:59 -05001113
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +00001114 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +08001115 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -05001116 }
1117
Li Zefan6c282eb2012-06-11 16:03:35 +08001118 return em;
1119}
1120
1121static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1122{
1123 struct extent_map *next;
1124 bool ret = true;
1125
1126 /* this is the last extent */
1127 if (em->start + em->len >= i_size_read(inode))
1128 return false;
1129
1130 next = defrag_lookup_extent(inode, em->start + em->len);
Chris Masone9512d72014-08-26 13:55:54 -07001131 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1132 ret = false;
1133 else if ((em->block_start + em->block_len == next->block_start) &&
Byongho Leeee221842015-12-15 01:42:10 +09001134 (em->block_len > SZ_128K && next->block_len > SZ_128K))
Li Zefan6c282eb2012-06-11 16:03:35 +08001135 ret = false;
1136
1137 free_extent_map(next);
1138 return ret;
1139}
1140
David Sterbaaab110a2014-07-29 17:32:10 +02001141static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001142 u64 *last_len, u64 *skip, u64 *defrag_end,
1143 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +08001144{
1145 struct extent_map *em;
1146 int ret = 1;
1147 bool next_mergeable = true;
Liu Bo4a3560c2015-08-07 16:48:41 +08001148 bool prev_mergeable = true;
Li Zefan6c282eb2012-06-11 16:03:35 +08001149
1150 /*
1151 * make sure that once we start defragging an extent, we keep on
1152 * defragging it
1153 */
1154 if (start < *defrag_end)
1155 return 1;
1156
1157 *skip = 0;
1158
1159 em = defrag_lookup_extent(inode, start);
1160 if (!em)
1161 return 0;
1162
Chris Mason940100a2010-03-10 10:52:59 -05001163 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001164 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001165 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001166 goto out;
1167 }
1168
Liu Bo4a3560c2015-08-07 16:48:41 +08001169 if (!*defrag_end)
1170 prev_mergeable = false;
1171
Li Zefan6c282eb2012-06-11 16:03:35 +08001172 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001173 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001174 * we hit a real extent, if it is big or the next extent is not a
1175 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001176 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001177 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Liu Bo4a3560c2015-08-07 16:48:41 +08001178 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
Chris Mason940100a2010-03-10 10:52:59 -05001179 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001180out:
Chris Mason940100a2010-03-10 10:52:59 -05001181 /*
1182 * last_len ends up being a counter of how many bytes we've defragged.
1183 * every time we choose not to defrag an extent, we reset *last_len
1184 * so that the next tiny extent will force a defrag.
1185 *
1186 * The end result of this is that tiny extents before a single big
1187 * extent will force at least part of that big extent to be defragged.
1188 */
1189 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001190 *defrag_end = extent_map_end(em);
1191 } else {
1192 *last_len = 0;
1193 *skip = extent_map_end(em);
1194 *defrag_end = 0;
1195 }
1196
1197 free_extent_map(em);
1198 return ret;
1199}
1200
Chris Mason4cb53002011-05-24 15:35:30 -04001201/*
Qu Wenruo5767b502021-08-06 16:12:35 +08001202 * Prepare one page to be defragged.
1203 *
1204 * This will ensure:
1205 *
1206 * - Returned page is locked and has been set up properly.
1207 * - No ordered extent exists in the page.
1208 * - The page is uptodate.
1209 *
1210 * NOTE: Caller should also wait for page writeback after the cluster is
1211 * prepared, here we don't do writeback wait for each page.
1212 */
1213static struct page *defrag_prepare_one_page(struct btrfs_inode *inode,
1214 pgoff_t index)
1215{
1216 struct address_space *mapping = inode->vfs_inode.i_mapping;
1217 gfp_t mask = btrfs_alloc_write_mask(mapping);
1218 u64 page_start = (u64)index << PAGE_SHIFT;
1219 u64 page_end = page_start + PAGE_SIZE - 1;
1220 struct extent_state *cached_state = NULL;
1221 struct page *page;
1222 int ret;
1223
1224again:
1225 page = find_or_create_page(mapping, index, mask);
1226 if (!page)
1227 return ERR_PTR(-ENOMEM);
1228
1229 ret = set_page_extent_mapped(page);
1230 if (ret < 0) {
1231 unlock_page(page);
1232 put_page(page);
1233 return ERR_PTR(ret);
1234 }
1235
1236 /* Wait for any existing ordered extent in the range */
1237 while (1) {
1238 struct btrfs_ordered_extent *ordered;
1239
1240 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
1241 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
1242 unlock_extent_cached(&inode->io_tree, page_start, page_end,
1243 &cached_state);
1244 if (!ordered)
1245 break;
1246
1247 unlock_page(page);
1248 btrfs_start_ordered_extent(ordered, 1);
1249 btrfs_put_ordered_extent(ordered);
1250 lock_page(page);
1251 /*
1252 * We unlocked the page above, so we need check if it was
1253 * released or not.
1254 */
1255 if (page->mapping != mapping || !PagePrivate(page)) {
1256 unlock_page(page);
1257 put_page(page);
1258 goto again;
1259 }
1260 }
1261
1262 /*
1263 * Now the page range has no ordered extent any more. Read the page to
1264 * make it uptodate.
1265 */
1266 if (!PageUptodate(page)) {
1267 btrfs_readpage(NULL, page);
1268 lock_page(page);
1269 if (page->mapping != mapping || !PagePrivate(page)) {
1270 unlock_page(page);
1271 put_page(page);
1272 goto again;
1273 }
1274 if (!PageUptodate(page)) {
1275 unlock_page(page);
1276 put_page(page);
1277 return ERR_PTR(-EIO);
1278 }
1279 }
1280 return page;
1281}
1282
1283/*
Chris Mason4cb53002011-05-24 15:35:30 -04001284 * it doesn't do much good to defrag one or two pages
1285 * at a time. This pulls in a nice chunk of pages
1286 * to COW and defrag.
1287 *
1288 * It also makes sure the delalloc code has enough
1289 * dirty data to avoid making new small extents as part
1290 * of the defrag
1291 *
1292 * It's a good idea to start RA on this range
1293 * before calling this.
1294 */
1295static int cluster_pages_for_defrag(struct inode *inode,
1296 struct page **pages,
1297 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001298 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299{
Chris Mason4cb53002011-05-24 15:35:30 -04001300 unsigned long file_end;
1301 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 u64 page_start;
1303 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001304 u64 page_cnt;
Matthew Wilcox (Oracle)a1fbc672020-10-04 19:04:26 +01001305 u64 start = (u64)start_index << PAGE_SHIFT;
Filipe Manana7f458a32020-11-04 11:07:33 +00001306 u64 search_start;
Chris Mason4cb53002011-05-24 15:35:30 -04001307 int ret;
1308 int i;
1309 int i_done;
Chris Mason4cb53002011-05-24 15:35:30 -04001310 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08001311 struct extent_changeset *data_reserved = NULL;
Chris Mason4cb53002011-05-24 15:35:30 -04001312
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001313 file_end = (isize - 1) >> PAGE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001314 if (!isize || start_index > file_end)
1315 return 0;
1316
1317 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001318
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03001319 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
Matthew Wilcox (Oracle)a1fbc672020-10-04 19:04:26 +01001320 start, page_cnt << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001321 if (ret)
1322 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001323 i_done = 0;
1324
1325 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001326 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001327 struct page *page;
Chris Mason4cb53002011-05-24 15:35:30 -04001328
Qu Wenruo5767b502021-08-06 16:12:35 +08001329 page = defrag_prepare_one_page(BTRFS_I(inode), start_index + i);
1330 if (IS_ERR(page)) {
1331 ret = PTR_ERR(page);
Qu Wenruo32443de2021-01-26 16:34:00 +08001332 break;
1333 }
Chris Mason4cb53002011-05-24 15:35:30 -04001334 pages[i] = page;
1335 i_done++;
1336 }
1337 if (!i_done || ret)
1338 goto out;
1339
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001340 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001341 goto out;
1342
1343 /*
Qu Wenruo5767b502021-08-06 16:12:35 +08001344 * Now we have a nice long stream of locked and up to date pages, let's
1345 * wait on them.
Chris Mason4cb53002011-05-24 15:35:30 -04001346 */
1347 for (i = 0; i < i_done; i++)
1348 wait_on_page_writeback(pages[i]);
1349
1350 page_start = page_offset(pages[0]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001351 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
Chris Mason4cb53002011-05-24 15:35:30 -04001352
1353 lock_extent_bits(&BTRFS_I(inode)->io_tree,
David Sterbaff13db42015-12-03 14:30:40 +01001354 page_start, page_end - 1, &cached_state);
Filipe Manana7f458a32020-11-04 11:07:33 +00001355
1356 /*
1357 * When defragmenting we skip ranges that have holes or inline extents,
1358 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1359 * space. At btrfs_defrag_file(), we check if a range should be defragged
1360 * before locking the inode and then, if it should, we trigger a sync
1361 * page cache readahead - we lock the inode only after that to avoid
1362 * blocking for too long other tasks that possibly want to operate on
1363 * other file ranges. But before we were able to get the inode lock,
1364 * some other task may have punched a hole in the range, or we may have
1365 * now an inline extent, in which case we should not defrag. So check
1366 * for that here, where we have the inode and the range locked, and bail
1367 * out if that happened.
1368 */
1369 search_start = page_start;
1370 while (search_start < page_end) {
1371 struct extent_map *em;
1372
1373 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
1374 page_end - search_start);
1375 if (IS_ERR(em)) {
1376 ret = PTR_ERR(em);
1377 goto out_unlock_range;
1378 }
1379 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1380 free_extent_map(em);
1381 /* Ok, 0 means we did not defrag anything */
1382 ret = 0;
1383 goto out_unlock_range;
1384 }
1385 search_start = extent_map_end(em);
1386 free_extent_map(em);
1387 }
1388
Chris Mason4cb53002011-05-24 15:35:30 -04001389 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
Omar Sandovale1821632019-08-15 14:04:04 -07001390 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1391 EXTENT_DEFRAG, 0, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001392
Liu Bo1f12bd02012-03-29 09:57:44 -04001393 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001394 spin_lock(&BTRFS_I(inode)->lock);
Su Yue28c4a3e2018-09-05 11:07:33 +08001395 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001396 spin_unlock(&BTRFS_I(inode)->lock);
Nikolay Borisov86d52922020-06-03 08:55:40 +03001397 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Matthew Wilcox (Oracle)a1fbc672020-10-04 19:04:26 +01001398 start, (page_cnt - i_done) << PAGE_SHIFT, true);
Chris Mason4cb53002011-05-24 15:35:30 -04001399 }
1400
1401
Liu Bo9e8a4a82012-09-05 19:10:51 -06001402 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
David Sterba018ed4f2016-04-26 23:54:39 +02001403 &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001404
1405 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
David Sterbae43bbe52017-12-12 21:43:52 +01001406 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001407
1408 for (i = 0; i < i_done; i++) {
1409 clear_page_dirty_for_io(pages[i]);
1410 ClearPageChecked(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001411 set_page_dirty(pages[i]);
1412 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001413 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001414 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08001415 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001416 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001417 return i_done;
Filipe Manana7f458a32020-11-04 11:07:33 +00001418
1419out_unlock_range:
1420 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1421 page_start, page_end - 1, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001422out:
1423 for (i = 0; i < i_done; i++) {
1424 unlock_page(pages[i]);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001425 put_page(pages[i]);
Chris Mason4cb53002011-05-24 15:35:30 -04001426 }
Nikolay Borisov86d52922020-06-03 08:55:40 +03001427 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Matthew Wilcox (Oracle)a1fbc672020-10-04 19:04:26 +01001428 start, page_cnt << PAGE_SHIFT, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08001429 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Qu Wenruo364ecf32017-02-27 15:10:38 +08001430 extent_changeset_free(data_reserved);
Chris Mason4cb53002011-05-24 15:35:30 -04001431 return ret;
1432
1433}
1434
Qu Wenruoeb793cf2021-08-06 16:12:36 +08001435struct defrag_target_range {
1436 struct list_head list;
1437 u64 start;
1438 u64 len;
1439};
1440
1441/*
1442 * Collect all valid target extents.
1443 *
1444 * @start: file offset to lookup
1445 * @len: length to lookup
1446 * @extent_thresh: file extent size threshold, any extent size >= this value
1447 * will be ignored
1448 * @newer_than: only defrag extents newer than this value
1449 * @do_compress: whether the defrag is doing compression
1450 * if true, @extent_thresh will be ignored and all regular
1451 * file extents meeting @newer_than will be targets.
1452 * @target_list: list of targets file extents
1453 */
1454static int defrag_collect_targets(struct btrfs_inode *inode,
1455 u64 start, u64 len, u32 extent_thresh,
1456 u64 newer_than, bool do_compress,
1457 struct list_head *target_list)
1458{
1459 u64 cur = start;
1460 int ret = 0;
1461
1462 while (cur < start + len) {
1463 struct extent_map *em;
1464 struct defrag_target_range *new;
1465 bool next_mergeable = true;
1466 u64 range_len;
1467
1468 em = defrag_lookup_extent(&inode->vfs_inode, cur);
1469 if (!em)
1470 break;
1471
1472 /* Skip hole/inline/preallocated extents */
1473 if (em->block_start >= EXTENT_MAP_LAST_BYTE ||
1474 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
1475 goto next;
1476
1477 /* Skip older extent */
1478 if (em->generation < newer_than)
1479 goto next;
1480
1481 /*
1482 * For do_compress case, we want to compress all valid file
1483 * extents, thus no @extent_thresh or mergeable check.
1484 */
1485 if (do_compress)
1486 goto add;
1487
1488 /* Skip too large extent */
1489 if (em->len >= extent_thresh)
1490 goto next;
1491
1492 next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em);
1493 if (!next_mergeable) {
1494 struct defrag_target_range *last;
1495
1496 /* Empty target list, no way to merge with last entry */
1497 if (list_empty(target_list))
1498 goto next;
1499 last = list_entry(target_list->prev,
1500 struct defrag_target_range, list);
1501 /* Not mergeable with last entry */
1502 if (last->start + last->len != cur)
1503 goto next;
1504
1505 /* Mergeable, fall through to add it to @target_list. */
1506 }
1507
1508add:
1509 range_len = min(extent_map_end(em), start + len) - cur;
1510 /*
1511 * This one is a good target, check if it can be merged into
1512 * last range of the target list.
1513 */
1514 if (!list_empty(target_list)) {
1515 struct defrag_target_range *last;
1516
1517 last = list_entry(target_list->prev,
1518 struct defrag_target_range, list);
1519 ASSERT(last->start + last->len <= cur);
1520 if (last->start + last->len == cur) {
1521 /* Mergeable, enlarge the last entry */
1522 last->len += range_len;
1523 goto next;
1524 }
1525 /* Fall through to allocate a new entry */
1526 }
1527
1528 /* Allocate new defrag_target_range */
1529 new = kmalloc(sizeof(*new), GFP_NOFS);
1530 if (!new) {
1531 free_extent_map(em);
1532 ret = -ENOMEM;
1533 break;
1534 }
1535 new->start = cur;
1536 new->len = range_len;
1537 list_add_tail(&new->list, target_list);
1538
1539next:
1540 cur = extent_map_end(em);
1541 free_extent_map(em);
1542 }
1543 if (ret < 0) {
1544 struct defrag_target_range *entry;
1545 struct defrag_target_range *tmp;
1546
1547 list_for_each_entry_safe(entry, tmp, target_list, list) {
1548 list_del_init(&entry->list);
1549 kfree(entry);
1550 }
1551 }
1552 return ret;
1553}
1554
Qu Wenruo22b398e2021-08-06 16:12:37 +08001555#define CLUSTER_SIZE (SZ_256K)
1556
1557/*
1558 * Defrag one contiguous target range.
1559 *
1560 * @inode: target inode
1561 * @target: target range to defrag
1562 * @pages: locked pages covering the defrag range
1563 * @nr_pages: number of locked pages
1564 *
1565 * Caller should ensure:
1566 *
1567 * - Pages are prepared
1568 * Pages should be locked, no ordered extent in the pages range,
1569 * no writeback.
1570 *
1571 * - Extent bits are locked
1572 */
1573static int defrag_one_locked_target(struct btrfs_inode *inode,
1574 struct defrag_target_range *target,
1575 struct page **pages, int nr_pages,
1576 struct extent_state **cached_state)
1577{
1578 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1579 struct extent_changeset *data_reserved = NULL;
1580 const u64 start = target->start;
1581 const u64 len = target->len;
1582 unsigned long last_index = (start + len - 1) >> PAGE_SHIFT;
1583 unsigned long start_index = start >> PAGE_SHIFT;
1584 unsigned long first_index = page_index(pages[0]);
1585 int ret = 0;
1586 int i;
1587
1588 ASSERT(last_index - first_index + 1 <= nr_pages);
1589
1590 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len);
1591 if (ret < 0)
1592 return ret;
1593 clear_extent_bit(&inode->io_tree, start, start + len - 1,
1594 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1595 EXTENT_DEFRAG, 0, 0, cached_state);
1596 set_extent_defrag(&inode->io_tree, start, start + len - 1, cached_state);
1597
1598 /* Update the page status */
1599 for (i = start_index - first_index; i <= last_index - first_index; i++) {
1600 ClearPageChecked(pages[i]);
1601 btrfs_page_clamp_set_dirty(fs_info, pages[i], start, len);
1602 }
1603 btrfs_delalloc_release_extents(inode, len);
1604 extent_changeset_free(data_reserved);
1605
1606 return ret;
1607}
1608
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001609/*
1610 * Entry point to file defragmentation.
1611 *
1612 * @inode: inode to be defragged
1613 * @ra: readahead state (can be NUL)
1614 * @range: defrag options including range and flags
1615 * @newer_than: minimum transid to defrag
1616 * @max_to_defrag: max number of sectors to be defragged, if 0, the whole inode
1617 * will be defragged.
1618 */
1619int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
Chris Mason4cb53002011-05-24 15:35:30 -04001620 struct btrfs_ioctl_defrag_range_args *range,
1621 u64 newer_than, unsigned long max_to_defrag)
1622{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001623 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason4cb53002011-05-24 15:35:30 -04001624 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001625 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001626 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001627 u64 last_len = 0;
1628 u64 skip = 0;
1629 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001630 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001632 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001634 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001635 int compress_type = BTRFS_COMPRESS_ZLIB;
David Sterbaaab110a2014-07-29 17:32:10 +02001636 u32 extent_thresh = range->extent_thresh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001637 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
Justin Maggardc41570c2014-01-21 11:18:29 -08001638 unsigned long cluster = max_cluster;
Byongho Leeee221842015-12-15 01:42:10 +09001639 u64 new_align = ~((u64)SZ_128K - 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001640 struct page **pages = NULL;
David Sterba1e2ef462017-07-17 20:01:59 +02001641 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001642 bool ra_allocated = false;
Chris Mason4cb53002011-05-24 15:35:30 -04001643
Liu Bo0abd5b12013-04-16 09:20:28 +00001644 if (isize == 0)
1645 return 0;
1646
1647 if (range->start >= isize)
1648 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001649
David Sterba1e2ef462017-07-17 20:01:59 +02001650 if (do_compress) {
Chengguang Xuce96b7f2019-10-10 15:59:57 +08001651 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Li Zefan1a419d82010-10-25 15:12:50 +08001652 return -EINVAL;
1653 if (range->compress_type)
1654 compress_type = range->compress_type;
1655 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656
Liu Bo0abd5b12013-04-16 09:20:28 +00001657 if (extent_thresh == 0)
Byongho Leeee221842015-12-15 01:42:10 +09001658 extent_thresh = SZ_256K;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659
Chris Mason4cb53002011-05-24 15:35:30 -04001660 /*
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001661 * If we were not given a ra, allocate a readahead context. As
David Sterba0a52d102017-06-22 03:22:58 +02001662 * readahead is just an optimization, defrag will work without it so
1663 * we don't error out.
Chris Mason4cb53002011-05-24 15:35:30 -04001664 */
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001665 if (!ra) {
1666 ra_allocated = true;
David Sterba63e727e2017-06-22 03:13:02 +02001667 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
David Sterba0a52d102017-06-22 03:22:58 +02001668 if (ra)
1669 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001670 }
1671
David Sterba63e727e2017-06-22 03:13:02 +02001672 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
Chris Mason4cb53002011-05-24 15:35:30 -04001673 if (!pages) {
1674 ret = -ENOMEM;
1675 goto out_ra;
1676 }
1677
1678 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001679 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001680 last_index = min_t(u64, isize - 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001681 range->start + range->len - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001682 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001683 last_index = (isize - 1) >> PAGE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001684 }
1685
Chris Mason4cb53002011-05-24 15:35:30 -04001686 if (newer_than) {
1687 ret = find_new_extents(root, inode, newer_than,
Byongho Leeee221842015-12-15 01:42:10 +09001688 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001689 if (!ret) {
1690 range->start = newer_off;
1691 /*
1692 * we always align our defrag to help keep
1693 * the extents in the file evenly spaced
1694 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001695 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001696 } else
1697 goto out_ra;
1698 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001699 i = range->start >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001700 }
1701 if (!max_to_defrag)
chandan070034b2015-06-09 10:35:11 +05301702 max_to_defrag = last_index - i + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001703
Li Zefan2a0f7f52011-10-10 15:43:34 -04001704 /*
1705 * make writeback starts from i, so the defrag range can be
1706 * written sequentially.
1707 */
1708 if (i < inode->i_mapping->writeback_index)
1709 inode->i_mapping->writeback_index = i;
1710
Chris Masonf7f43cc2011-10-11 11:41:40 -04001711 while (i <= last_index && defrag_count < max_to_defrag &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001712 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
Chris Mason4cb53002011-05-24 15:35:30 -04001713 /*
1714 * make sure we stop running if someone unmounts
1715 * the FS
1716 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001717 if (!(inode->i_sb->s_flags & SB_ACTIVE))
Chris Mason4cb53002011-05-24 15:35:30 -04001718 break;
1719
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001720 if (btrfs_defrag_cancelled(fs_info)) {
1721 btrfs_debug(fs_info, "defrag_file cancelled");
David Sterba210549e2013-02-09 23:38:06 +00001722 ret = -EAGAIN;
Tian Tao50535db2021-05-05 09:26:28 +08001723 goto error;
David Sterba210549e2013-02-09 23:38:06 +00001724 }
1725
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001726 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001727 extent_thresh, &last_len, &skip,
David Sterba1e2ef462017-07-17 20:01:59 +02001728 &defrag_end, do_compress)){
Chris Mason940100a2010-03-10 10:52:59 -05001729 unsigned long next;
1730 /*
1731 * the should_defrag function tells us how much to skip
1732 * bump our counter by the suggested amount
1733 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001734 next = DIV_ROUND_UP(skip, PAGE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -05001735 i = max(i + 1, next);
1736 continue;
1737 }
Li Zefan008873e2011-09-02 15:57:07 +08001738
1739 if (!newer_than) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001740 cluster = (PAGE_ALIGN(defrag_end) >>
1741 PAGE_SHIFT) - i;
Li Zefan008873e2011-09-02 15:57:07 +08001742 cluster = min(cluster, max_cluster);
1743 } else {
1744 cluster = max_cluster;
1745 }
1746
Li Zefan008873e2011-09-02 15:57:07 +08001747 if (i + cluster > ra_index) {
1748 ra_index = max(i, ra_index);
David Sterba0a52d102017-06-22 03:22:58 +02001749 if (ra)
David Sterbad3c0bab2017-06-22 03:35:28 +02001750 page_cache_sync_readahead(inode->i_mapping, ra,
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001751 NULL, ra_index, cluster);
chandane4826a52015-06-09 17:38:32 +05301752 ra_index += cluster;
Li Zefan008873e2011-09-02 15:57:07 +08001753 }
Chris Mason940100a2010-03-10 10:52:59 -05001754
Josef Bacik64708532021-02-10 17:14:34 -05001755 btrfs_inode_lock(inode, 0);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001756 if (IS_SWAPFILE(inode)) {
1757 ret = -ETXTBSY;
1758 } else {
1759 if (do_compress)
1760 BTRFS_I(inode)->defrag_compress = compress_type;
1761 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1762 }
Liu Boecb8bea2012-03-29 09:57:44 -04001763 if (ret < 0) {
Josef Bacik64708532021-02-10 17:14:34 -05001764 btrfs_inode_unlock(inode, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04001765 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001766 }
Chris Mason940100a2010-03-10 10:52:59 -05001767
Chris Mason4cb53002011-05-24 15:35:30 -04001768 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001769 balance_dirty_pages_ratelimited(inode->i_mapping);
Josef Bacik64708532021-02-10 17:14:34 -05001770 btrfs_inode_unlock(inode, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04001771
1772 if (newer_than) {
1773 if (newer_off == (u64)-1)
1774 break;
1775
Liu Boe1f041e2012-03-29 09:57:45 -04001776 if (ret > 0)
1777 i += ret;
1778
Chris Mason4cb53002011-05-24 15:35:30 -04001779 newer_off = max(newer_off + 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001780 (u64)i << PAGE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001781
Byongho Leeee221842015-12-15 01:42:10 +09001782 ret = find_new_extents(root, inode, newer_than,
1783 &newer_off, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001784 if (!ret) {
1785 range->start = newer_off;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001786 i = (newer_off & new_align) >> PAGE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001787 } else {
1788 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001789 }
Chris Mason4cb53002011-05-24 15:35:30 -04001790 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001791 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001792 i += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001793 last_len += ret << PAGE_SHIFT;
Li Zefan008873e2011-09-02 15:57:07 +08001794 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001795 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001796 last_len = 0;
1797 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001798 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001799 }
1800
Tian Tao50535db2021-05-05 09:26:28 +08001801 ret = defrag_count;
1802error:
Filipe Mananadec8ef92014-03-01 10:55:54 +00001803 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001804 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001805 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1806 &BTRFS_I(inode)->runtime_flags))
1807 filemap_flush(inode->i_mapping);
1808 }
Chris Mason1e701a32010-03-11 09:42:04 -05001809
Li Zefan1a419d82010-10-25 15:12:50 +08001810 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001811 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -07001812 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1813 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
Li Zefan1a419d82010-10-25 15:12:50 +08001814 }
1815
Chris Mason4cb53002011-05-24 15:35:30 -04001816out_ra:
David Sterba1e2ef462017-07-17 20:01:59 +02001817 if (do_compress) {
Josef Bacik64708532021-02-10 17:14:34 -05001818 btrfs_inode_lock(inode, 0);
David Sterbaeec63c62017-07-17 19:41:31 +02001819 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
Josef Bacik64708532021-02-10 17:14:34 -05001820 btrfs_inode_unlock(inode, 0);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001821 }
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08001822 if (ra_allocated)
Chris Mason4cb53002011-05-24 15:35:30 -04001823 kfree(ra);
1824 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001825 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001826}
1827
David Sterba17aaa432021-05-14 21:32:44 +02001828/*
1829 * Try to start exclusive operation @type or cancel it if it's running.
1830 *
1831 * Return:
1832 * 0 - normal mode, newly claimed op started
1833 * >0 - normal mode, something else is running,
1834 * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
1835 * ECANCELED - cancel mode, successful cancel
1836 * ENOTCONN - cancel mode, operation not running anymore
1837 */
1838static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
1839 enum btrfs_exclusive_operation type, bool cancel)
1840{
1841 if (!cancel) {
1842 /* Start normal op */
1843 if (!btrfs_exclop_start(fs_info, type))
1844 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1845 /* Exclusive operation is now claimed */
1846 return 0;
1847 }
1848
1849 /* Cancel running op */
1850 if (btrfs_exclop_start_try_lock(fs_info, type)) {
1851 /*
1852 * This blocks any exclop finish from setting it to NONE, so we
1853 * request cancellation. Either it runs and we will wait for it,
1854 * or it has finished and no waiting will happen.
1855 */
1856 atomic_inc(&fs_info->reloc_cancel_req);
1857 btrfs_exclop_start_unlock(fs_info);
1858
1859 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
1860 wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
1861 TASK_INTERRUPTIBLE);
1862
1863 return -ECANCELED;
1864 }
1865
1866 /* Something else is running or none */
1867 return -ENOTCONN;
1868}
1869
Miao Xie198605a2012-11-26 08:43:45 +00001870static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001871 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 struct inode *inode = file_inode(file);
1874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001875 u64 new_size;
1876 u64 old_size;
1877 u64 devid = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001879 struct btrfs_ioctl_vol_args *vol_args;
1880 struct btrfs_trans_handle *trans;
1881 struct btrfs_device *device = NULL;
1882 char *sizestr;
Gui Hecheng9a40f122014-03-31 18:03:25 +08001883 char *retptr;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001884 char *devstr = NULL;
1885 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001886 int mod = 0;
David Sterbabb059a32021-05-18 21:12:33 +02001887 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001888
Chris Masone441d542009-01-05 16:57:23 -05001889 if (!capable(CAP_SYS_ADMIN))
1890 return -EPERM;
1891
Miao Xie198605a2012-11-26 08:43:45 +00001892 ret = mnt_want_write_file(file);
1893 if (ret)
1894 return ret;
1895
David Sterbabb059a32021-05-18 21:12:33 +02001896 /*
1897 * Read the arguments before checking exclusivity to be able to
1898 * distinguish regular resize and cancel
1899 */
Li Zefandae7b662009-04-08 15:06:54 +08001900 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001901 if (IS_ERR(vol_args)) {
1902 ret = PTR_ERR(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02001903 goto out_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001904 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001905 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001906 sizestr = vol_args->name;
David Sterbabb059a32021-05-18 21:12:33 +02001907 cancel = (strcmp("cancel", sizestr) == 0);
1908 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
1909 if (ret)
1910 goto out_free;
1911 /* Exclusive operation is now claimed */
1912
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001913 devstr = strchr(sizestr, ':');
1914 if (devstr) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001915 sizestr = devstr + 1;
1916 *devstr = '\0';
1917 devstr = vol_args->name;
ZhangZhen58dfae62014-05-13 16:36:08 +08001918 ret = kstrtoull(devstr, 10, &devid);
1919 if (ret)
David Sterbabb059a32021-05-18 21:12:33 +02001920 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001921 if (!devid) {
1922 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001923 goto out_finish;
Miao Xiedfd79822012-12-21 09:21:30 +00001924 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001925 btrfs_info(fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001926 }
Miao Xiedba60f32012-12-21 09:19:51 +00001927
Anand Jainb2598ed2020-11-03 13:49:43 +08001928 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001929 if (!device) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001930 btrfs_info(fs_info, "resizer unable to find device %llu",
1931 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001932 ret = -ENODEV;
David Sterbabb059a32021-05-18 21:12:33 +02001933 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001934 }
Miao Xiedba60f32012-12-21 09:19:51 +00001935
Anand Jainebbede42017-12-04 12:54:52 +08001936 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 btrfs_info(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001938 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001939 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001940 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001941 goto out_finish;
Liu Bo4e42ae12012-06-14 02:23:19 -06001942 }
1943
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001944 if (!strcmp(sizestr, "max"))
1945 new_size = device->bdev->bd_inode->i_size;
1946 else {
1947 if (sizestr[0] == '-') {
1948 mod = -1;
1949 sizestr++;
1950 } else if (sizestr[0] == '+') {
1951 mod = 1;
1952 sizestr++;
1953 }
Gui Hecheng9a40f122014-03-31 18:03:25 +08001954 new_size = memparse(sizestr, &retptr);
1955 if (*retptr != '\0' || new_size == 0) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001956 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001957 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001958 }
1959 }
1960
Anand Jain401e29c2017-12-04 12:54:55 +08001961 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xiedfd79822012-12-21 09:21:30 +00001962 ret = -EPERM;
David Sterbabb059a32021-05-18 21:12:33 +02001963 goto out_finish;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001964 }
1965
Miao Xie7cc8e582014-09-03 21:35:38 +08001966 old_size = btrfs_device_get_total_bytes(device);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001967
1968 if (mod < 0) {
1969 if (new_size > old_size) {
1970 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001971 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001972 }
1973 new_size = old_size - new_size;
1974 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001975 if (new_size > ULLONG_MAX - old_size) {
Gui Hecheng902c68a2014-05-29 09:19:58 +08001976 ret = -ERANGE;
David Sterbabb059a32021-05-18 21:12:33 +02001977 goto out_finish;
Wenliang Faneb8052e2013-12-20 15:28:56 +08001978 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001979 new_size = old_size + new_size;
1980 }
1981
Byongho Leeee221842015-12-15 01:42:10 +09001982 if (new_size < SZ_256M) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001983 ret = -EINVAL;
David Sterbabb059a32021-05-18 21:12:33 +02001984 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001985 }
1986 if (new_size > device->bdev->bd_inode->i_size) {
1987 ret = -EFBIG;
David Sterbabb059a32021-05-18 21:12:33 +02001988 goto out_finish;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001989 }
1990
Nikolay Borisov47f08b92017-07-18 15:39:08 +03001991 new_size = round_down(new_size, fs_info->sectorsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001992
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001993 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001994 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001995 if (IS_ERR(trans)) {
1996 ret = PTR_ERR(trans);
David Sterbabb059a32021-05-18 21:12:33 +02001997 goto out_finish;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001998 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001999 ret = btrfs_grow_device(trans, device, new_size);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002000 btrfs_commit_transaction(trans);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00002001 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002002 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00002003 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002004
Marcos Paulo de Souzafaf8f7b2020-02-11 10:55:26 -03002005 if (ret == 0 && new_size != old_size)
2006 btrfs_info_in_rcu(fs_info,
2007 "resize device %s (devid %llu) from %llu to %llu",
2008 rcu_str_deref(device->name), device->devid,
2009 old_size, new_size);
David Sterbabb059a32021-05-18 21:12:33 +02002010out_finish:
2011 btrfs_exclop_finish(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002012out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002013 kfree(vol_args);
David Sterbabb059a32021-05-18 21:12:33 +02002014out_drop:
Ilya Dryomov18f39c42013-01-20 15:57:57 +02002015 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002016 return ret;
2017}
2018
Nikolay Borisov5d54c672020-03-13 17:23:19 +02002019static noinline int __btrfs_ioctl_snap_create(struct file *file,
Christian Brauner4d4340c2021-07-27 12:48:52 +02002020 struct user_namespace *mnt_userns,
David Sterba52f75f42017-02-14 18:33:53 +01002021 const char *name, unsigned long fd, int subvol,
Nikolay Borisov5d54c672020-03-13 17:23:19 +02002022 bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00002023 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002024{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002025 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05002026 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002027
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002028 if (!S_ISDIR(file_inode(file)->i_mode))
2029 return -ENOTDIR;
2030
Liu Boa874a632012-06-29 03:58:46 -06002031 ret = mnt_want_write_file(file);
2032 if (ret)
2033 goto out;
2034
Sage Weil72fd0322010-10-29 15:41:32 -04002035 namelen = strlen(name);
2036 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002037 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06002038 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002039 }
2040
Chris Mason16780ca2012-02-20 22:14:55 -05002041 if (name[0] == '.' &&
2042 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
2043 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06002044 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05002045 }
2046
Chris Mason3de45862008-11-17 21:02:50 -05002047 if (subvol) {
Christian Brauner4d4340c2021-07-27 12:48:52 +02002048 ret = btrfs_mksubvol(&file->f_path, mnt_userns, name,
2049 namelen, NULL, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04002050 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002051 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05002052 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04002053 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05002054 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06002055 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05002056 }
2057
Al Viro496ad9a2013-01-23 17:07:38 -05002058 src_inode = file_inode(src.file);
2059 if (src_inode->i_sb != file_inode(file)->i_sb) {
Josef Bacikc79b4712016-03-25 10:02:41 -04002060 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05002061 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09002062 ret = -EXDEV;
Christian Brauner4d4340c2021-07-27 12:48:52 +02002063 } else if (!inode_owner_or_capable(mnt_userns, src_inode)) {
David Sterbad0242062014-01-15 18:15:52 +01002064 /*
2065 * Subvolume creation is not restricted, but snapshots
2066 * are limited to own subvolumes only
2067 */
2068 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04002069 } else {
Christian Brauner4d4340c2021-07-27 12:48:52 +02002070 ret = btrfs_mksnapshot(&file->f_path, mnt_userns,
2071 name, namelen,
2072 BTRFS_I(src_inode)->root,
2073 readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05002074 }
Al Viro2903ff02012-08-28 12:52:22 -04002075 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04002076 }
Liu Boa874a632012-06-29 03:58:46 -06002077out_drop_write:
2078 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002079out:
Sage Weil72fd0322010-10-29 15:41:32 -04002080 return ret;
2081}
2082
2083static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08002084 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04002085{
Li Zefanfa0d2b92010-12-20 15:53:28 +08002086 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04002087 int ret;
2088
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002089 if (!S_ISDIR(file_inode(file)->i_mode))
2090 return -ENOTDIR;
2091
Li Zefanfa0d2b92010-12-20 15:53:28 +08002092 vol_args = memdup_user(arg, sizeof(*vol_args));
2093 if (IS_ERR(vol_args))
2094 return PTR_ERR(vol_args);
2095 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04002096
Christian Brauner4d4340c2021-07-27 12:48:52 +02002097 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
2098 vol_args->name, vol_args->fd, subvol,
2099 false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002100
Li Zefanfa0d2b92010-12-20 15:53:28 +08002101 kfree(vol_args);
2102 return ret;
2103}
Li Zefanfdfb1e42010-12-10 06:41:56 +00002104
Li Zefanfa0d2b92010-12-20 15:53:28 +08002105static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
2106 void __user *arg, int subvol)
2107{
2108 struct btrfs_ioctl_vol_args_v2 *vol_args;
2109 int ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002110 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002111 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00002112
Jeff Mahoney325c50e2016-09-21 08:31:29 -04002113 if (!S_ISDIR(file_inode(file)->i_mode))
2114 return -ENOTDIR;
2115
Li Zefanfa0d2b92010-12-20 15:53:28 +08002116 vol_args = memdup_user(arg, sizeof(*vol_args));
2117 if (IS_ERR(vol_args))
2118 return PTR_ERR(vol_args);
2119 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00002120
David Sterba673990d2020-02-21 13:24:37 +01002121 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Li Zefanb83cc962010-12-20 16:04:08 +08002122 ret = -EOPNOTSUPP;
Dan Carpenterc47ca322014-09-04 14:09:15 +03002123 goto free_args;
Sage Weil72fd0322010-10-29 15:41:32 -04002124 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08002125
Li Zefanb83cc962010-12-20 16:04:08 +08002126 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
2127 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002128 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Dan Carpenter5011c5a2021-02-17 09:04:34 +03002129 u64 nums;
2130
2131 if (vol_args->size < sizeof(*inherit) ||
2132 vol_args->size > PAGE_SIZE) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002133 ret = -EINVAL;
Dan Carpenterc47ca322014-09-04 14:09:15 +03002134 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002135 }
2136 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
2137 if (IS_ERR(inherit)) {
2138 ret = PTR_ERR(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002139 goto free_args;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002140 }
Dan Carpenter5011c5a2021-02-17 09:04:34 +03002141
2142 if (inherit->num_qgroups > PAGE_SIZE ||
2143 inherit->num_ref_copies > PAGE_SIZE ||
2144 inherit->num_excl_copies > PAGE_SIZE) {
2145 ret = -EINVAL;
2146 goto free_inherit;
2147 }
2148
2149 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
2150 2 * inherit->num_excl_copies;
2151 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
2152 ret = -EINVAL;
2153 goto free_inherit;
2154 }
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002155 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08002156
Christian Brauner4d4340c2021-07-27 12:48:52 +02002157 ret = __btrfs_ioctl_snap_create(file, file_mnt_user_ns(file),
2158 vol_args->name, vol_args->fd, subvol,
2159 readonly, inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002160 if (ret)
2161 goto free_inherit;
Dan Carpenterc47ca322014-09-04 14:09:15 +03002162free_inherit:
Arne Jansen6f72c7e2011-09-14 15:58:21 +02002163 kfree(inherit);
Dan Carpenterc47ca322014-09-04 14:09:15 +03002164free_args:
2165 kfree(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002166 return ret;
2167}
2168
Li Zefan0caa1022010-12-20 16:30:25 +08002169static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
2170 void __user *arg)
2171{
Al Viro496ad9a2013-01-23 17:07:38 -05002172 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08002174 struct btrfs_root *root = BTRFS_I(inode)->root;
2175 int ret = 0;
2176 u64 flags = 0;
2177
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002178 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08002179 return -EINVAL;
2180
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002181 down_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002182 if (btrfs_root_readonly(root))
2183 flags |= BTRFS_SUBVOL_RDONLY;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002184 up_read(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002185
2186 if (copy_to_user(arg, &flags, sizeof(flags)))
2187 ret = -EFAULT;
2188
2189 return ret;
2190}
2191
2192static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
2193 void __user *arg)
2194{
Al Viro496ad9a2013-01-23 17:07:38 -05002195 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Li Zefan0caa1022010-12-20 16:30:25 +08002197 struct btrfs_root *root = BTRFS_I(inode)->root;
2198 struct btrfs_trans_handle *trans;
2199 u64 root_flags;
2200 u64 flags;
2201 int ret = 0;
2202
Christian Brauner39e16742021-07-27 12:48:56 +02002203 if (!inode_owner_or_capable(file_mnt_user_ns(file), inode))
David Sterbabd60ea02014-01-16 15:50:22 +01002204 return -EPERM;
2205
Liu Bob9ca0662012-06-29 03:58:49 -06002206 ret = mnt_want_write_file(file);
2207 if (ret)
2208 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08002209
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002210 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Liu Bob9ca0662012-06-29 03:58:49 -06002211 ret = -EINVAL;
2212 goto out_drop_write;
2213 }
Li Zefan0caa1022010-12-20 16:30:25 +08002214
Liu Bob9ca0662012-06-29 03:58:49 -06002215 if (copy_from_user(&flags, arg, sizeof(flags))) {
2216 ret = -EFAULT;
2217 goto out_drop_write;
2218 }
Li Zefan0caa1022010-12-20 16:30:25 +08002219
Liu Bob9ca0662012-06-29 03:58:49 -06002220 if (flags & ~BTRFS_SUBVOL_RDONLY) {
2221 ret = -EOPNOTSUPP;
2222 goto out_drop_write;
2223 }
Li Zefan0caa1022010-12-20 16:30:25 +08002224
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002225 down_write(&fs_info->subvol_sem);
Li Zefan0caa1022010-12-20 16:30:25 +08002226
2227 /* nothing to do */
2228 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06002229 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08002230
2231 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01002232 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08002233 btrfs_set_root_flags(&root->root_item,
2234 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01002235 } else {
2236 /*
2237 * Block RO -> RW transition if this subvolume is involved in
2238 * send
2239 */
2240 spin_lock(&root->root_item_lock);
2241 if (root->send_in_progress == 0) {
2242 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08002243 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01002244 spin_unlock(&root->root_item_lock);
2245 } else {
2246 spin_unlock(&root->root_item_lock);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002247 btrfs_warn(fs_info,
2248 "Attempt to set subvolume %llu read-write during send",
2249 root->root_key.objectid);
David Sterba2c686532013-12-16 17:34:17 +01002250 ret = -EPERM;
2251 goto out_drop_sem;
2252 }
2253 }
Li Zefan0caa1022010-12-20 16:30:25 +08002254
2255 trans = btrfs_start_transaction(root, 1);
2256 if (IS_ERR(trans)) {
2257 ret = PTR_ERR(trans);
2258 goto out_reset;
2259 }
2260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002261 ret = btrfs_update_root(trans, fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08002262 &root->root_key, &root->root_item);
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002263 if (ret < 0) {
2264 btrfs_end_transaction(trans);
2265 goto out_reset;
2266 }
Li Zefan0caa1022010-12-20 16:30:25 +08002267
Nikolay Borisov9417ebc2017-09-28 10:53:17 +03002268 ret = btrfs_commit_transaction(trans);
2269
Li Zefan0caa1022010-12-20 16:30:25 +08002270out_reset:
2271 if (ret)
2272 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06002273out_drop_sem:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002274 up_write(&fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06002275out_drop_write:
2276 mnt_drop_write_file(file);
2277out:
Li Zefan0caa1022010-12-20 16:30:25 +08002278 return ret;
2279}
2280
Chris Masonac8e9812010-02-28 15:39:26 -05002281static noinline int key_in_sk(struct btrfs_key *key,
2282 struct btrfs_ioctl_search_key *sk)
2283{
Chris Masonabc6e132010-03-18 12:10:08 -04002284 struct btrfs_key test;
2285 int ret;
2286
2287 test.objectid = sk->min_objectid;
2288 test.type = sk->min_type;
2289 test.offset = sk->min_offset;
2290
2291 ret = btrfs_comp_cpu_keys(key, &test);
2292 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002293 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04002294
2295 test.objectid = sk->max_objectid;
2296 test.type = sk->max_type;
2297 test.offset = sk->max_offset;
2298
2299 ret = btrfs_comp_cpu_keys(key, &test);
2300 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05002301 return 0;
2302 return 1;
2303}
2304
Jeff Mahoneydf397562016-06-21 20:18:21 -04002305static noinline int copy_to_sk(struct btrfs_path *path,
Chris Masonac8e9812010-02-28 15:39:26 -05002306 struct btrfs_key *key,
2307 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002308 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002309 char __user *ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002310 unsigned long *sk_offset,
2311 int *num_found)
2312{
2313 u64 found_transid;
2314 struct extent_buffer *leaf;
2315 struct btrfs_ioctl_search_header sh;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002316 struct btrfs_key test;
Chris Masonac8e9812010-02-28 15:39:26 -05002317 unsigned long item_off;
2318 unsigned long item_len;
2319 int nritems;
2320 int i;
2321 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05002322 int ret = 0;
2323
2324 leaf = path->nodes[0];
2325 slot = path->slots[0];
2326 nritems = btrfs_header_nritems(leaf);
2327
2328 if (btrfs_header_generation(leaf) > sk->max_transid) {
2329 i = nritems;
2330 goto advance_key;
2331 }
2332 found_transid = btrfs_header_generation(leaf);
2333
2334 for (i = slot; i < nritems; i++) {
2335 item_off = btrfs_item_ptr_offset(leaf, i);
2336 item_len = btrfs_item_size_nr(leaf, i);
2337
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00002338 btrfs_item_key_to_cpu(leaf, key, i);
2339 if (!key_in_sk(key, sk))
2340 continue;
2341
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002342 if (sizeof(sh) + item_len > *buf_size) {
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002343 if (*num_found) {
2344 ret = 1;
2345 goto out;
2346 }
Chris Masonac8e9812010-02-28 15:39:26 -05002347
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002348 /*
2349 * return one empty item back for v1, which does not
2350 * handle -EOVERFLOW
2351 */
2352
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002353 *buf_size = sizeof(sh) + item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002354 item_len = 0;
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002355 ret = -EOVERFLOW;
2356 }
Chris Masonac8e9812010-02-28 15:39:26 -05002357
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002358 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
Chris Masonac8e9812010-02-28 15:39:26 -05002359 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002360 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002361 }
2362
Chris Masonac8e9812010-02-28 15:39:26 -05002363 sh.objectid = key->objectid;
2364 sh.offset = key->offset;
2365 sh.type = key->type;
2366 sh.len = item_len;
2367 sh.transid = found_transid;
2368
Josef Bacika48b73e2020-08-10 11:42:27 -04002369 /*
2370 * Copy search result header. If we fault then loop again so we
2371 * can fault in the pages and -EFAULT there if there's a
2372 * problem. Otherwise we'll fault and then copy the buffer in
2373 * properly this next time through
2374 */
2375 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
2376 ret = 0;
Gerhard Heiftba346b32014-01-30 16:24:02 +01002377 goto out;
2378 }
2379
Chris Masonac8e9812010-02-28 15:39:26 -05002380 *sk_offset += sizeof(sh);
2381
2382 if (item_len) {
Gerhard Heiftba346b32014-01-30 16:24:02 +01002383 char __user *up = ubuf + *sk_offset;
Josef Bacika48b73e2020-08-10 11:42:27 -04002384 /*
2385 * Copy the item, same behavior as above, but reset the
2386 * * sk_offset so we copy the full thing again.
2387 */
2388 if (read_extent_buffer_to_user_nofault(leaf, up,
2389 item_off, item_len)) {
2390 ret = 0;
2391 *sk_offset -= sizeof(sh);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002392 goto out;
2393 }
2394
Chris Masonac8e9812010-02-28 15:39:26 -05002395 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05002396 }
Hugo Millse2156862011-05-14 17:43:41 +00002397 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05002398
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002399 if (ret) /* -EOVERFLOW from above */
2400 goto out;
2401
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002402 if (*num_found >= sk->nr_items) {
2403 ret = 1;
2404 goto out;
2405 }
Chris Masonac8e9812010-02-28 15:39:26 -05002406 }
2407advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05002408 ret = 0;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002409 test.objectid = sk->max_objectid;
2410 test.type = sk->max_type;
2411 test.offset = sk->max_offset;
2412 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2413 ret = 1;
2414 else if (key->offset < (u64)-1)
Chris Masonabc6e132010-03-18 12:10:08 -04002415 key->offset++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002416 else if (key->type < (u8)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002417 key->offset = 0;
2418 key->type++;
Naohiro Aotadd81d452015-06-30 11:25:43 +09002419 } else if (key->objectid < (u64)-1) {
Chris Masonabc6e132010-03-18 12:10:08 -04002420 key->offset = 0;
2421 key->type = 0;
2422 key->objectid++;
2423 } else
2424 ret = 1;
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002425out:
Gerhard Heiftba346b32014-01-30 16:24:02 +01002426 /*
2427 * 0: all items from this leaf copied, continue with next
2428 * 1: * more items can be copied, but unused buffer is too small
2429 * * all items were found
2430 * Either way, it will stops the loop which iterates to the next
2431 * leaf
2432 * -EOVERFLOW: item was to large for buffer
2433 * -EFAULT: could not copy extent buffer back to userspace
2434 */
Chris Masonac8e9812010-02-28 15:39:26 -05002435 return ret;
2436}
2437
2438static noinline int search_ioctl(struct inode *inode,
Gerhard Heift12544442014-01-30 16:23:58 +01002439 struct btrfs_ioctl_search_key *sk,
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002440 size_t *buf_size,
Gerhard Heiftba346b32014-01-30 16:24:02 +01002441 char __user *ubuf)
Chris Masonac8e9812010-02-28 15:39:26 -05002442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
Chris Masonac8e9812010-02-28 15:39:26 -05002444 struct btrfs_root *root;
2445 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002446 struct btrfs_path *path;
Chris Masonac8e9812010-02-28 15:39:26 -05002447 int ret;
2448 int num_found = 0;
2449 unsigned long sk_offset = 0;
2450
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002451 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2452 *buf_size = sizeof(struct btrfs_ioctl_search_header);
Gerhard Heift12544442014-01-30 16:23:58 +01002453 return -EOVERFLOW;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002454 }
Gerhard Heift12544442014-01-30 16:23:58 +01002455
Chris Masonac8e9812010-02-28 15:39:26 -05002456 path = btrfs_alloc_path();
2457 if (!path)
2458 return -ENOMEM;
2459
2460 if (sk->tree_id == 0) {
2461 /* search the root of the inode that was passed */
Josef Bacik00246522020-01-24 09:33:01 -05002462 root = btrfs_grab_root(BTRFS_I(inode)->root);
Chris Masonac8e9812010-02-28 15:39:26 -05002463 } else {
David Sterba56e93572020-05-15 19:35:55 +02002464 root = btrfs_get_fs_root(info, sk->tree_id, true);
Chris Masonac8e9812010-02-28 15:39:26 -05002465 if (IS_ERR(root)) {
Chris Masonac8e9812010-02-28 15:39:26 -05002466 btrfs_free_path(path);
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002467 return PTR_ERR(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002468 }
2469 }
2470
2471 key.objectid = sk->min_objectid;
2472 key.type = sk->min_type;
2473 key.offset = sk->min_offset;
2474
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302475 while (1) {
Filipe Manana1c785442020-09-14 09:01:04 +01002476 ret = fault_in_pages_writeable(ubuf + sk_offset,
2477 *buf_size - sk_offset);
Josef Bacika48b73e2020-08-10 11:42:27 -04002478 if (ret)
2479 break;
2480
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002481 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002482 if (ret != 0) {
2483 if (ret > 0)
2484 ret = 0;
2485 goto err;
2486 }
Jeff Mahoneydf397562016-06-21 20:18:21 -04002487 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
Chris Masonac8e9812010-02-28 15:39:26 -05002488 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002489 btrfs_release_path(path);
Gerhard Heift25c9bc22014-01-30 16:23:57 +01002490 if (ret)
Chris Masonac8e9812010-02-28 15:39:26 -05002491 break;
2492
2493 }
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002494 if (ret > 0)
2495 ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002496err:
2497 sk->nr_items = num_found;
Josef Bacik00246522020-01-24 09:33:01 -05002498 btrfs_put_root(root);
Chris Masonac8e9812010-02-28 15:39:26 -05002499 btrfs_free_path(path);
2500 return ret;
2501}
2502
2503static noinline int btrfs_ioctl_tree_search(struct file *file,
2504 void __user *argp)
2505{
Gerhard Heiftba346b32014-01-30 16:24:02 +01002506 struct btrfs_ioctl_search_args __user *uargs;
2507 struct btrfs_ioctl_search_key sk;
Gerhard Heift9b6e8172014-01-30 16:24:00 +01002508 struct inode *inode;
2509 int ret;
2510 size_t buf_size;
Chris Masonac8e9812010-02-28 15:39:26 -05002511
2512 if (!capable(CAP_SYS_ADMIN))
2513 return -EPERM;
2514
Gerhard Heiftba346b32014-01-30 16:24:02 +01002515 uargs = (struct btrfs_ioctl_search_args __user *)argp;
Chris Masonac8e9812010-02-28 15:39:26 -05002516
Gerhard Heiftba346b32014-01-30 16:24:02 +01002517 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2518 return -EFAULT;
2519
2520 buf_size = sizeof(uargs->buf);
Chris Masonac8e9812010-02-28 15:39:26 -05002521
Al Viro496ad9a2013-01-23 17:07:38 -05002522 inode = file_inode(file);
Gerhard Heiftba346b32014-01-30 16:24:02 +01002523 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
Gerhard Heift8f5f6172014-01-30 16:23:59 +01002524
2525 /*
2526 * In the origin implementation an overflow is handled by returning a
2527 * search header with a len of zero, so reset ret.
2528 */
2529 if (ret == -EOVERFLOW)
2530 ret = 0;
2531
Gerhard Heiftba346b32014-01-30 16:24:02 +01002532 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
Chris Masonac8e9812010-02-28 15:39:26 -05002533 ret = -EFAULT;
Chris Masonac8e9812010-02-28 15:39:26 -05002534 return ret;
2535}
2536
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002537static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2538 void __user *argp)
2539{
2540 struct btrfs_ioctl_search_args_v2 __user *uarg;
2541 struct btrfs_ioctl_search_args_v2 args;
2542 struct inode *inode;
2543 int ret;
2544 size_t buf_size;
Byongho Leeee221842015-12-15 01:42:10 +09002545 const size_t buf_limit = SZ_16M;
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002546
2547 if (!capable(CAP_SYS_ADMIN))
2548 return -EPERM;
2549
2550 /* copy search header and buffer size */
2551 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2552 if (copy_from_user(&args, uarg, sizeof(args)))
2553 return -EFAULT;
2554
2555 buf_size = args.buf_size;
2556
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002557 /* limit result size to 16MB */
2558 if (buf_size > buf_limit)
2559 buf_size = buf_limit;
2560
2561 inode = file_inode(file);
2562 ret = search_ioctl(inode, &args.key, &buf_size,
Omar Sandoval718dc5f2017-08-22 23:46:05 -07002563 (char __user *)(&uarg->buf[0]));
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01002564 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2565 ret = -EFAULT;
2566 else if (ret == -EOVERFLOW &&
2567 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2568 ret = -EFAULT;
2569
Yan, Zheng76dda932009-09-21 16:00:26 -04002570 return ret;
2571}
2572
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002573/*
Chris Masonac8e9812010-02-28 15:39:26 -05002574 * Search INODE_REFs to identify path name of 'dirid' directory
2575 * in a 'tree_id' tree. and sets path name to 'name'.
2576 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002577static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2578 u64 tree_id, u64 dirid, char *name)
2579{
2580 struct btrfs_root *root;
2581 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002582 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002583 int ret = -1;
2584 int slot;
2585 int len;
2586 int total_len = 0;
2587 struct btrfs_inode_ref *iref;
2588 struct extent_buffer *l;
2589 struct btrfs_path *path;
2590
2591 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2592 name[0]='\0';
2593 return 0;
2594 }
2595
2596 path = btrfs_alloc_path();
2597 if (!path)
2598 return -ENOMEM;
2599
Nikolay Borisovc8bcbfbd2017-12-01 11:19:42 +02002600 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002601
David Sterba56e93572020-05-15 19:35:55 +02002602 root = btrfs_get_fs_root(info, tree_id, true);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002603 if (IS_ERR(root)) {
Misono Tomohiroad1e3d52018-05-21 13:57:27 +09002604 ret = PTR_ERR(root);
Josef Bacik88234012020-01-24 09:32:34 -05002605 root = NULL;
2606 goto out;
2607 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002608
2609 key.objectid = dirid;
2610 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002611 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002612
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302613 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002614 ret = btrfs_search_backwards(root, &key, path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002615 if (ret < 0)
2616 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002617 else if (ret > 0) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002618 ret = -ENOENT;
2619 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002620 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002621
2622 l = path->nodes[0];
2623 slot = path->slots[0];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002624
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002625 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2626 len = btrfs_inode_ref_name_len(l, iref);
2627 ptr -= len + 1;
2628 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002629 if (ptr < name) {
2630 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002631 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002632 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002633
2634 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302635 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002636
2637 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2638 break;
2639
David Sterbab3b4aa72011-04-21 01:20:15 +02002640 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002641 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002642 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002643 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002644 }
Li Zefan77906a502011-07-14 03:16:00 +00002645 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302646 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002647 ret = 0;
2648out:
Josef Bacik00246522020-01-24 09:33:01 -05002649 btrfs_put_root(root);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002650 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002651 return ret;
2652}
2653
Christian Brauner6623d9a2021-07-27 12:48:57 +02002654static int btrfs_search_path_in_tree_user(struct user_namespace *mnt_userns,
2655 struct inode *inode,
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002656 struct btrfs_ioctl_ino_lookup_user_args *args)
2657{
2658 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2659 struct super_block *sb = inode->i_sb;
2660 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2661 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2662 u64 dirid = args->dirid;
2663 unsigned long item_off;
2664 unsigned long item_len;
2665 struct btrfs_inode_ref *iref;
2666 struct btrfs_root_ref *rref;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002667 struct btrfs_root *root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002668 struct btrfs_path *path;
2669 struct btrfs_key key, key2;
2670 struct extent_buffer *leaf;
2671 struct inode *temp_inode;
2672 char *ptr;
2673 int slot;
2674 int len;
2675 int total_len = 0;
2676 int ret;
2677
2678 path = btrfs_alloc_path();
2679 if (!path)
2680 return -ENOMEM;
2681
2682 /*
2683 * If the bottom subvolume does not exist directly under upper_limit,
2684 * construct the path in from the bottom up.
2685 */
2686 if (dirid != upper_limit.objectid) {
2687 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2688
David Sterba56e93572020-05-15 19:35:55 +02002689 root = btrfs_get_fs_root(fs_info, treeid, true);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002690 if (IS_ERR(root)) {
2691 ret = PTR_ERR(root);
2692 goto out;
2693 }
2694
2695 key.objectid = dirid;
2696 key.type = BTRFS_INODE_REF_KEY;
2697 key.offset = (u64)-1;
2698 while (1) {
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002699 ret = btrfs_search_backwards(root, &key, path);
2700 if (ret < 0)
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002701 goto out_put;
Marcos Paulo de Souza0ff40a92021-07-29 05:22:16 -03002702 else if (ret > 0) {
2703 ret = -ENOENT;
2704 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002705 }
2706
2707 leaf = path->nodes[0];
2708 slot = path->slots[0];
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002709
2710 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2711 len = btrfs_inode_ref_name_len(leaf, iref);
2712 ptr -= len + 1;
2713 total_len += len + 1;
2714 if (ptr < args->path) {
2715 ret = -ENAMETOOLONG;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002716 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002717 }
2718
2719 *(ptr + len) = '/';
2720 read_extent_buffer(leaf, ptr,
2721 (unsigned long)(iref + 1), len);
2722
2723 /* Check the read+exec permission of this directory */
2724 ret = btrfs_previous_item(root, path, dirid,
2725 BTRFS_INODE_ITEM_KEY);
2726 if (ret < 0) {
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002727 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002728 } else if (ret > 0) {
2729 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002730 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002731 }
2732
2733 leaf = path->nodes[0];
2734 slot = path->slots[0];
2735 btrfs_item_key_to_cpu(leaf, &key2, slot);
2736 if (key2.objectid != dirid) {
2737 ret = -ENOENT;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002738 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002739 }
2740
David Sterba0202e832020-05-15 19:35:59 +02002741 temp_inode = btrfs_iget(sb, key2.objectid, root);
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002742 if (IS_ERR(temp_inode)) {
2743 ret = PTR_ERR(temp_inode);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002744 goto out_put;
Misono Tomohiro3ca57bd2018-06-04 16:41:07 +09002745 }
Christian Brauner6623d9a2021-07-27 12:48:57 +02002746 ret = inode_permission(mnt_userns, temp_inode,
Christian Brauner47291ba2021-01-21 14:19:24 +01002747 MAY_READ | MAY_EXEC);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002748 iput(temp_inode);
2749 if (ret) {
2750 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002751 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002752 }
2753
2754 if (key.offset == upper_limit.objectid)
2755 break;
2756 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2757 ret = -EACCES;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002758 goto out_put;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002759 }
2760
2761 btrfs_release_path(path);
2762 key.objectid = key.offset;
2763 key.offset = (u64)-1;
2764 dirid = key.objectid;
2765 }
2766
2767 memmove(args->path, ptr, total_len);
2768 args->path[total_len] = '\0';
Josef Bacik00246522020-01-24 09:33:01 -05002769 btrfs_put_root(root);
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002770 root = NULL;
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002771 btrfs_release_path(path);
2772 }
2773
2774 /* Get the bottom subvolume's name from ROOT_REF */
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002775 key.objectid = treeid;
2776 key.type = BTRFS_ROOT_REF_KEY;
2777 key.offset = args->treeid;
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002778 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002779 if (ret < 0) {
2780 goto out;
2781 } else if (ret > 0) {
2782 ret = -ENOENT;
2783 goto out;
2784 }
2785
2786 leaf = path->nodes[0];
2787 slot = path->slots[0];
2788 btrfs_item_key_to_cpu(leaf, &key, slot);
2789
2790 item_off = btrfs_item_ptr_offset(leaf, slot);
2791 item_len = btrfs_item_size_nr(leaf, slot);
2792 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2793 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2794 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2795 ret = -EINVAL;
2796 goto out;
2797 }
2798
2799 /* Copy subvolume's name */
2800 item_off += sizeof(struct btrfs_root_ref);
2801 item_len -= sizeof(struct btrfs_root_ref);
2802 read_extent_buffer(leaf, args->name, item_off, item_len);
2803 args->name[item_len] = 0;
2804
Josef Bacikb8a49ae2020-01-24 09:32:35 -05002805out_put:
Josef Bacik00246522020-01-24 09:33:01 -05002806 btrfs_put_root(root);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002807out:
2808 btrfs_free_path(path);
2809 return ret;
2810}
2811
Chris Masonac8e9812010-02-28 15:39:26 -05002812static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2813 void __user *argp)
2814{
Bart Van Asschebece2e82018-06-20 10:03:31 -07002815 struct btrfs_ioctl_ino_lookup_args *args;
2816 struct inode *inode;
David Sterba01b810b2015-05-12 19:14:49 +02002817 int ret = 0;
Chris Masonac8e9812010-02-28 15:39:26 -05002818
Julia Lawall2354d08f2010-10-29 15:14:18 -04002819 args = memdup_user(argp, sizeof(*args));
2820 if (IS_ERR(args))
2821 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002822
Al Viro496ad9a2013-01-23 17:07:38 -05002823 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002824
David Sterba01b810b2015-05-12 19:14:49 +02002825 /*
2826 * Unprivileged query to obtain the containing subvolume root id. The
2827 * path is reset so it's consistent with btrfs_search_path_in_tree.
2828 */
Chris Mason1b53ac42010-03-18 12:17:05 -04002829 if (args->treeid == 0)
2830 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2831
David Sterba01b810b2015-05-12 19:14:49 +02002832 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2833 args->name[0] = 0;
2834 goto out;
2835 }
2836
2837 if (!capable(CAP_SYS_ADMIN)) {
2838 ret = -EPERM;
2839 goto out;
2840 }
2841
Chris Masonac8e9812010-02-28 15:39:26 -05002842 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2843 args->treeid, args->objectid,
2844 args->name);
2845
David Sterba01b810b2015-05-12 19:14:49 +02002846out:
Chris Masonac8e9812010-02-28 15:39:26 -05002847 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2848 ret = -EFAULT;
2849
2850 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002851 return ret;
2852}
2853
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002854/*
2855 * Version of ino_lookup ioctl (unprivileged)
2856 *
2857 * The main differences from ino_lookup ioctl are:
2858 *
2859 * 1. Read + Exec permission will be checked using inode_permission() during
2860 * path construction. -EACCES will be returned in case of failure.
2861 * 2. Path construction will be stopped at the inode number which corresponds
2862 * to the fd with which this ioctl is called. If constructed path does not
2863 * exist under fd's inode, -EACCES will be returned.
2864 * 3. The name of bottom subvolume is also searched and filled.
2865 */
2866static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2867{
2868 struct btrfs_ioctl_ino_lookup_user_args *args;
2869 struct inode *inode;
2870 int ret;
2871
2872 args = memdup_user(argp, sizeof(*args));
2873 if (IS_ERR(args))
2874 return PTR_ERR(args);
2875
2876 inode = file_inode(file);
2877
2878 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2879 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2880 /*
2881 * The subvolume does not exist under fd with which this is
2882 * called
2883 */
2884 kfree(args);
2885 return -EACCES;
2886 }
2887
Christian Brauner6623d9a2021-07-27 12:48:57 +02002888 ret = btrfs_search_path_in_tree_user(file_mnt_user_ns(file), inode, args);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09002889
2890 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2891 ret = -EFAULT;
2892
2893 kfree(args);
2894 return ret;
2895}
2896
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002897/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2898static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2899{
2900 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2901 struct btrfs_fs_info *fs_info;
2902 struct btrfs_root *root;
2903 struct btrfs_path *path;
2904 struct btrfs_key key;
2905 struct btrfs_root_item *root_item;
2906 struct btrfs_root_ref *rref;
2907 struct extent_buffer *leaf;
2908 unsigned long item_off;
2909 unsigned long item_len;
2910 struct inode *inode;
2911 int slot;
2912 int ret = 0;
2913
2914 path = btrfs_alloc_path();
2915 if (!path)
2916 return -ENOMEM;
2917
2918 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2919 if (!subvol_info) {
2920 btrfs_free_path(path);
2921 return -ENOMEM;
2922 }
2923
2924 inode = file_inode(file);
2925 fs_info = BTRFS_I(inode)->root->fs_info;
2926
2927 /* Get root_item of inode's subvolume */
2928 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
David Sterba56e93572020-05-15 19:35:55 +02002929 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002930 if (IS_ERR(root)) {
2931 ret = PTR_ERR(root);
Josef Bacik04734e82020-01-24 09:32:36 -05002932 goto out_free;
2933 }
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002934 root_item = &root->root_item;
2935
2936 subvol_info->treeid = key.objectid;
2937
2938 subvol_info->generation = btrfs_root_generation(root_item);
2939 subvol_info->flags = btrfs_root_flags(root_item);
2940
2941 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2942 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2943 BTRFS_UUID_SIZE);
2944 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2945 BTRFS_UUID_SIZE);
2946
2947 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2948 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2949 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2950
2951 subvol_info->otransid = btrfs_root_otransid(root_item);
2952 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2953 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2954
2955 subvol_info->stransid = btrfs_root_stransid(root_item);
2956 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2957 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2958
2959 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2960 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2961 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2962
2963 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2964 /* Search root tree for ROOT_BACKREF of this subvolume */
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002965 key.type = BTRFS_ROOT_BACKREF_KEY;
2966 key.offset = 0;
Josef Bacik04734e82020-01-24 09:32:36 -05002967 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002968 if (ret < 0) {
2969 goto out;
2970 } else if (path->slots[0] >=
2971 btrfs_header_nritems(path->nodes[0])) {
Josef Bacik04734e82020-01-24 09:32:36 -05002972 ret = btrfs_next_leaf(fs_info->tree_root, path);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09002973 if (ret < 0) {
2974 goto out;
2975 } else if (ret > 0) {
2976 ret = -EUCLEAN;
2977 goto out;
2978 }
2979 }
2980
2981 leaf = path->nodes[0];
2982 slot = path->slots[0];
2983 btrfs_item_key_to_cpu(leaf, &key, slot);
2984 if (key.objectid == subvol_info->treeid &&
2985 key.type == BTRFS_ROOT_BACKREF_KEY) {
2986 subvol_info->parent_id = key.offset;
2987
2988 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2989 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2990
2991 item_off = btrfs_item_ptr_offset(leaf, slot)
2992 + sizeof(struct btrfs_root_ref);
2993 item_len = btrfs_item_size_nr(leaf, slot)
2994 - sizeof(struct btrfs_root_ref);
2995 read_extent_buffer(leaf, subvol_info->name,
2996 item_off, item_len);
2997 } else {
2998 ret = -ENOENT;
2999 goto out;
3000 }
3001 }
3002
3003 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
3004 ret = -EFAULT;
3005
3006out:
Josef Bacik00246522020-01-24 09:33:01 -05003007 btrfs_put_root(root);
Josef Bacik04734e82020-01-24 09:32:36 -05003008out_free:
Tomohiro Misonob64ec072018-05-21 10:09:42 +09003009 btrfs_free_path(path);
Waiman Longb091f7f2020-06-16 11:31:59 -04003010 kfree(subvol_info);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09003011 return ret;
3012}
3013
Tomohiro Misono42e4b522018-05-21 10:09:43 +09003014/*
3015 * Return ROOT_REF information of the subvolume containing this inode
3016 * except the subvolume name.
3017 */
3018static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
3019{
3020 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
3021 struct btrfs_root_ref *rref;
3022 struct btrfs_root *root;
3023 struct btrfs_path *path;
3024 struct btrfs_key key;
3025 struct extent_buffer *leaf;
3026 struct inode *inode;
3027 u64 objectid;
3028 int slot;
3029 int ret;
3030 u8 found;
3031
3032 path = btrfs_alloc_path();
3033 if (!path)
3034 return -ENOMEM;
3035
3036 rootrefs = memdup_user(argp, sizeof(*rootrefs));
3037 if (IS_ERR(rootrefs)) {
3038 btrfs_free_path(path);
3039 return PTR_ERR(rootrefs);
3040 }
3041
3042 inode = file_inode(file);
3043 root = BTRFS_I(inode)->root->fs_info->tree_root;
3044 objectid = BTRFS_I(inode)->root->root_key.objectid;
3045
3046 key.objectid = objectid;
3047 key.type = BTRFS_ROOT_REF_KEY;
3048 key.offset = rootrefs->min_treeid;
3049 found = 0;
3050
3051 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3052 if (ret < 0) {
3053 goto out;
3054 } else if (path->slots[0] >=
3055 btrfs_header_nritems(path->nodes[0])) {
3056 ret = btrfs_next_leaf(root, path);
3057 if (ret < 0) {
3058 goto out;
3059 } else if (ret > 0) {
3060 ret = -EUCLEAN;
3061 goto out;
3062 }
3063 }
3064 while (1) {
3065 leaf = path->nodes[0];
3066 slot = path->slots[0];
3067
3068 btrfs_item_key_to_cpu(leaf, &key, slot);
3069 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
3070 ret = 0;
3071 goto out;
3072 }
3073
3074 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
3075 ret = -EOVERFLOW;
3076 goto out;
3077 }
3078
3079 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
3080 rootrefs->rootref[found].treeid = key.offset;
3081 rootrefs->rootref[found].dirid =
3082 btrfs_root_ref_dirid(leaf, rref);
3083 found++;
3084
3085 ret = btrfs_next_item(root, path);
3086 if (ret < 0) {
3087 goto out;
3088 } else if (ret > 0) {
3089 ret = -EUCLEAN;
3090 goto out;
3091 }
3092 }
3093
3094out:
3095 if (!ret || ret == -EOVERFLOW) {
3096 rootrefs->num_items = found;
3097 /* update min_treeid for next search */
3098 if (found)
3099 rootrefs->min_treeid =
3100 rootrefs->rootref[found - 1].treeid + 1;
3101 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
3102 ret = -EFAULT;
3103 }
3104
3105 kfree(rootrefs);
3106 btrfs_free_path(path);
3107
3108 return ret;
3109}
3110
Yan, Zheng76dda932009-09-21 16:00:26 -04003111static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003112 void __user *arg,
3113 bool destroy_v2)
Yan, Zheng76dda932009-09-21 16:00:26 -04003114{
Al Viro54563d42013-09-01 15:57:51 -04003115 struct dentry *parent = file->f_path.dentry;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003116 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04003117 struct dentry *dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003118 struct inode *dir = d_inode(parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04003119 struct inode *inode;
3120 struct btrfs_root *root = BTRFS_I(dir)->root;
3121 struct btrfs_root *dest = NULL;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003122 struct btrfs_ioctl_vol_args *vol_args = NULL;
3123 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003124 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003125 char *subvol_name, *subvol_name_ptr = NULL;
3126 int subvol_namelen;
Yan, Zheng76dda932009-09-21 16:00:26 -04003127 int err = 0;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003128 bool destroy_parent = false;
Yan, Zheng76dda932009-09-21 16:00:26 -04003129
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003130 if (destroy_v2) {
3131 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
3132 if (IS_ERR(vol_args2))
3133 return PTR_ERR(vol_args2);
Jeff Mahoney325c50e2016-09-21 08:31:29 -04003134
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003135 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
3136 err = -EOPNOTSUPP;
3137 goto out;
3138 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003139
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003140 /*
3141 * If SPEC_BY_ID is not set, we are looking for the subvolume by
3142 * name, same as v1 currently does.
3143 */
3144 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
3145 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
3146 subvol_name = vol_args2->name;
3147
3148 err = mnt_want_write_file(file);
3149 if (err)
3150 goto out;
3151 } else {
Christian Brauneraabb34e2021-07-27 12:48:54 +02003152 struct inode *old_dir;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003153
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003154 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
3155 err = -EINVAL;
3156 goto out;
3157 }
3158
3159 err = mnt_want_write_file(file);
3160 if (err)
3161 goto out;
3162
3163 dentry = btrfs_get_dentry(fs_info->sb,
3164 BTRFS_FIRST_FREE_OBJECTID,
3165 vol_args2->subvolid, 0, 0);
3166 if (IS_ERR(dentry)) {
3167 err = PTR_ERR(dentry);
3168 goto out_drop_write;
3169 }
3170
3171 /*
3172 * Change the default parent since the subvolume being
3173 * deleted can be outside of the current mount point.
3174 */
3175 parent = btrfs_get_parent(dentry);
3176
3177 /*
3178 * At this point dentry->d_name can point to '/' if the
3179 * subvolume we want to destroy is outsite of the
3180 * current mount point, so we need to release the
3181 * current dentry and execute the lookup to return a new
3182 * one with ->d_name pointing to the
3183 * <mount point>/subvol_name.
3184 */
3185 dput(dentry);
3186 if (IS_ERR(parent)) {
3187 err = PTR_ERR(parent);
3188 goto out_drop_write;
3189 }
Christian Brauneraabb34e2021-07-27 12:48:54 +02003190 old_dir = dir;
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003191 dir = d_inode(parent);
3192
3193 /*
3194 * If v2 was used with SPEC_BY_ID, a new parent was
3195 * allocated since the subvolume can be outside of the
3196 * current mount point. Later on we need to release this
3197 * new parent dentry.
3198 */
3199 destroy_parent = true;
3200
Christian Brauneraabb34e2021-07-27 12:48:54 +02003201 /*
3202 * On idmapped mounts, deletion via subvolid is
3203 * restricted to subvolumes that are immediate
3204 * ancestors of the inode referenced by the file
3205 * descriptor in the ioctl. Otherwise the idmapping
3206 * could potentially be abused to delete subvolumes
3207 * anywhere in the filesystem the user wouldn't be able
3208 * to delete without an idmapped mount.
3209 */
3210 if (old_dir != dir && mnt_userns != &init_user_ns) {
3211 err = -EOPNOTSUPP;
3212 goto free_parent;
3213 }
3214
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003215 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
3216 fs_info, vol_args2->subvolid);
3217 if (IS_ERR(subvol_name_ptr)) {
3218 err = PTR_ERR(subvol_name_ptr);
3219 goto free_parent;
3220 }
David Sterba1a9fd412021-05-21 17:42:23 +02003221 /* subvol_name_ptr is already nul terminated */
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003222 subvol_name = (char *)kbasename(subvol_name_ptr);
3223 }
3224 } else {
3225 vol_args = memdup_user(arg, sizeof(*vol_args));
3226 if (IS_ERR(vol_args))
3227 return PTR_ERR(vol_args);
3228
3229 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3230 subvol_name = vol_args->name;
3231
3232 err = mnt_want_write_file(file);
3233 if (err)
3234 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04003235 }
3236
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003237 subvol_namelen = strlen(subvol_name);
Yan, Zheng76dda932009-09-21 16:00:26 -04003238
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003239 if (strchr(subvol_name, '/') ||
3240 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3241 err = -EINVAL;
3242 goto free_subvol_name;
3243 }
3244
3245 if (!S_ISDIR(dir->i_mode)) {
3246 err = -ENOTDIR;
3247 goto free_subvol_name;
3248 }
David Sterba521e0542014-04-15 16:41:44 +02003249
Al Viro00235412016-05-26 00:05:12 -04003250 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3251 if (err == -EINTR)
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003252 goto free_subvol_name;
Christian Braunerc4ed5332021-07-27 12:48:53 +02003253 dentry = lookup_one(mnt_userns, subvol_name, parent, subvol_namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -04003254 if (IS_ERR(dentry)) {
3255 err = PTR_ERR(dentry);
3256 goto out_unlock_dir;
3257 }
3258
David Howells2b0143b2015-03-17 22:25:59 +00003259 if (d_really_is_negative(dentry)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003260 err = -ENOENT;
3261 goto out_dput;
3262 }
3263
David Howells2b0143b2015-03-17 22:25:59 +00003264 inode = d_inode(dentry);
Sage Weil4260f7c2010-10-29 15:46:43 -04003265 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303266 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04003267 /*
3268 * Regular user. Only allow this with a special mount
3269 * option, when the user has write+exec access to the
3270 * subvol root, and when rmdir(2) would have been
3271 * allowed.
3272 *
3273 * Note that this is _not_ check that the subvol is
3274 * empty or doesn't contain data that we wouldn't
3275 * otherwise be able to delete.
3276 *
3277 * Users who want to delete empty subvols should try
3278 * rmdir(2).
3279 */
3280 err = -EPERM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003281 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
Sage Weil4260f7c2010-10-29 15:46:43 -04003282 goto out_dput;
3283
3284 /*
3285 * Do not allow deletion if the parent dir is the same
3286 * as the dir to be deleted. That means the ioctl
3287 * must be called on the dentry referencing the root
3288 * of the subvol, not a random directory contained
3289 * within it.
3290 */
3291 err = -EINVAL;
3292 if (root == dest)
3293 goto out_dput;
3294
Christian Braunerc4ed5332021-07-27 12:48:53 +02003295 err = inode_permission(mnt_userns, inode, MAY_WRITE | MAY_EXEC);
Sage Weil4260f7c2010-10-29 15:46:43 -04003296 if (err)
3297 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04003298 }
3299
Miao Xie5c39da52012-10-22 11:39:53 +00003300 /* check if subvolume may be deleted by a user */
Christian Braunerc4ed5332021-07-27 12:48:53 +02003301 err = btrfs_may_delete(mnt_userns, dir, dentry, 1);
Miao Xie5c39da52012-10-22 11:39:53 +00003302 if (err)
3303 goto out_dput;
3304
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003305 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003306 err = -EINVAL;
3307 goto out_dput;
3308 }
3309
Josef Bacik64708532021-02-10 17:14:34 -05003310 btrfs_inode_lock(inode, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003311 err = btrfs_delete_subvolume(dir, dentry);
Josef Bacik64708532021-02-10 17:14:34 -05003312 btrfs_inode_unlock(inode, 0);
Amir Goldstein46008d92019-05-26 17:34:03 +03003313 if (!err) {
3314 fsnotify_rmdir(dir, dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04003315 d_delete(dentry);
Amir Goldstein46008d92019-05-26 17:34:03 +03003316 }
Liu Bofa6ac872013-02-20 14:10:23 +00003317
Yan, Zheng76dda932009-09-21 16:00:26 -04003318out_dput:
3319 dput(dentry);
3320out_unlock_dir:
Josef Bacik64708532021-02-10 17:14:34 -05003321 btrfs_inode_unlock(dir, 0);
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003322free_subvol_name:
3323 kfree(subvol_name_ptr);
3324free_parent:
3325 if (destroy_parent)
3326 dput(parent);
Al Viro00235412016-05-26 00:05:12 -04003327out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003328 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04003329out:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03003330 kfree(vol_args2);
Yan, Zheng76dda932009-09-21 16:00:26 -04003331 kfree(vol_args);
3332 return err;
3333}
3334
Chris Mason1e701a32010-03-11 09:42:04 -05003335static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003336{
Al Viro496ad9a2013-01-23 17:07:38 -05003337 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003338 struct btrfs_root *root = BTRFS_I(inode)->root;
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003339 struct btrfs_ioctl_defrag_range_args range = {0};
Yan Zhengc146afa2008-11-12 14:34:12 -05003340 int ret;
3341
Ilya Dryomov25122d12013-01-20 15:57:57 +02003342 ret = mnt_want_write_file(file);
3343 if (ret)
3344 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08003345
Ilya Dryomov25122d12013-01-20 15:57:57 +02003346 if (btrfs_root_readonly(root)) {
3347 ret = -EROFS;
3348 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003349 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003350
Qu Wenruo95ea0482021-07-26 14:35:06 +08003351 /* Subpage defrag will be supported in later commits */
3352 if (root->fs_info->sectorsize < PAGE_SIZE) {
3353 ret = -ENOTTY;
3354 goto out;
3355 }
3356
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003357 switch (inode->i_mode & S_IFMT) {
3358 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05003359 if (!capable(CAP_SYS_ADMIN)) {
3360 ret = -EPERM;
3361 goto out;
3362 }
Eric Sandeende78b512013-01-31 18:21:12 +00003363 ret = btrfs_defrag_root(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003364 break;
3365 case S_IFREG:
Adam Borowski616d3742018-07-18 00:08:59 +02003366 /*
3367 * Note that this does not check the file descriptor for write
3368 * access. This prevents defragmenting executables that are
3369 * running and allows defrag on files open in read-only mode.
3370 */
3371 if (!capable(CAP_SYS_ADMIN) &&
Christian Brauner47291ba2021-01-21 14:19:24 +01003372 inode_permission(&init_user_ns, inode, MAY_WRITE)) {
Adam Borowski616d3742018-07-18 00:08:59 +02003373 ret = -EPERM;
Chris Masone441d542009-01-05 16:57:23 -05003374 goto out;
3375 }
Chris Mason1e701a32010-03-11 09:42:04 -05003376
Chris Mason1e701a32010-03-11 09:42:04 -05003377 if (argp) {
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003378 if (copy_from_user(&range, argp, sizeof(range))) {
Chris Mason1e701a32010-03-11 09:42:04 -05003379 ret = -EFAULT;
Dan Carpenter683be162010-03-20 11:24:48 +00003380 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05003381 }
3382 /* compression requires us to start the IO */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003383 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3384 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
3385 range.extent_thresh = (u32)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003386 }
3387 } else {
3388 /* the rest are all set to zero by kzalloc */
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003389 range.len = (u64)-1;
Chris Mason1e701a32010-03-11 09:42:04 -05003390 }
Qu Wenruo1ccc2e82021-08-06 16:12:32 +08003391 ret = btrfs_defrag_file(file_inode(file), &file->f_ra,
Goldwyn Rodriguesc853a572021-07-27 16:17:30 -05003392 &range, BTRFS_OLDEST_GENERATION, 0);
Chris Mason4cb53002011-05-24 15:35:30 -04003393 if (ret > 0)
3394 ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003395 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003396 default:
3397 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003398 }
Chris Masone441d542009-01-05 16:57:23 -05003399out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02003400 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05003401 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003402}
3403
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003404static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003405{
3406 struct btrfs_ioctl_vol_args *vol_args;
3407 int ret;
3408
Chris Masone441d542009-01-05 16:57:23 -05003409 if (!capable(CAP_SYS_ADMIN))
3410 return -EPERM;
3411
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003412 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
Anand Jaine57138b2013-08-21 11:44:48 +08003413 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003414
Li Zefandae7b662009-04-08 15:06:54 +08003415 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416 if (IS_ERR(vol_args)) {
3417 ret = PTR_ERR(vol_args);
3418 goto out;
3419 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003420
Mark Fasheh5516e592008-07-24 12:20:14 -04003421 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003422 ret = btrfs_init_new_device(fs_info, vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003423
Anand Jain43d20762014-07-01 00:58:56 +08003424 if (!ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003425 btrfs_info(fs_info, "disk added %s", vol_args->name);
Anand Jain43d20762014-07-01 00:58:56 +08003426
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003427 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003428out:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003429 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003430 return ret;
3431}
3432
Anand Jain6b526ed2016-02-13 10:01:39 +08003433static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003434{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 struct inode *inode = file_inode(file);
3436 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Anand Jain6b526ed2016-02-13 10:01:39 +08003437 struct btrfs_ioctl_vol_args_v2 *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003438 struct block_device *bdev = NULL;
3439 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003440 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003441 bool cancel = false;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003442
Chris Masone441d542009-01-05 16:57:23 -05003443 if (!capable(CAP_SYS_ADMIN))
3444 return -EPERM;
3445
Miao Xieda249272012-11-26 08:44:50 +00003446 ret = mnt_want_write_file(file);
3447 if (ret)
3448 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05003449
Li Zefandae7b662009-04-08 15:06:54 +08003450 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003451 if (IS_ERR(vol_args)) {
3452 ret = PTR_ERR(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003453 goto err_drop;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003454 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003455
David Sterba748449cdb2020-02-21 13:30:14 +01003456 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Omar Sandovalfd4e9942018-05-22 15:44:01 -07003457 ret = -EOPNOTSUPP;
3458 goto out;
3459 }
David Sterba67ae34b2021-05-14 21:21:27 +02003460 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3461 if (!(vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) &&
3462 strcmp("cancel", vol_args->name) == 0)
3463 cancel = true;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003464
David Sterba67ae34b2021-05-14 21:21:27 +02003465 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3466 cancel);
3467 if (ret)
Anand Jain183860f2013-05-17 10:52:45 +00003468 goto out;
David Sterba67ae34b2021-05-14 21:21:27 +02003469 /* Exclusive operation is now claimed */
Anand Jain183860f2013-05-17 10:52:45 +00003470
David Sterba67ae34b2021-05-14 21:21:27 +02003471 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Josef Bacik3fa421d2021-07-27 17:01:17 -04003472 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003473 else
Josef Bacik3fa421d2021-07-27 17:01:17 -04003474 ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003475
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05003476 btrfs_exclop_finish(fs_info);
Anand Jain183860f2013-05-17 10:52:45 +00003477
Anand Jain6b526ed2016-02-13 10:01:39 +08003478 if (!ret) {
David Sterba735654e2016-02-15 18:15:21 +01003479 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003480 btrfs_info(fs_info, "device deleted: id %llu",
Anand Jain6b526ed2016-02-13 10:01:39 +08003481 vol_args->devid);
3482 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003483 btrfs_info(fs_info, "device deleted: %s",
Anand Jain6b526ed2016-02-13 10:01:39 +08003484 vol_args->name);
3485 }
Anand Jain183860f2013-05-17 10:52:45 +00003486out:
3487 kfree(vol_args);
Dan Carpenterc47ca322014-09-04 14:09:15 +03003488err_drop:
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02003489 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003490 if (bdev)
3491 blkdev_put(bdev, mode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003492 return ret;
3493}
3494
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003495static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3496{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003497 struct inode *inode = file_inode(file);
3498 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003499 struct btrfs_ioctl_vol_args *vol_args;
Josef Bacik3fa421d2021-07-27 17:01:17 -04003500 struct block_device *bdev = NULL;
3501 fmode_t mode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003502 int ret;
David Sterba67ae34b2021-05-14 21:21:27 +02003503 bool cancel;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003504
3505 if (!capable(CAP_SYS_ADMIN))
3506 return -EPERM;
3507
3508 ret = mnt_want_write_file(file);
3509 if (ret)
3510 return ret;
3511
David Sterba58d7bbf2016-05-04 14:10:47 +02003512 vol_args = memdup_user(arg, sizeof(*vol_args));
3513 if (IS_ERR(vol_args)) {
3514 ret = PTR_ERR(vol_args);
David Sterba67ae34b2021-05-14 21:21:27 +02003515 goto out_drop_write;
3516 }
3517 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3518 cancel = (strcmp("cancel", vol_args->name) == 0);
3519
3520 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
3521 cancel);
3522 if (ret == 0) {
Josef Bacik3fa421d2021-07-27 17:01:17 -04003523 ret = btrfs_rm_device(fs_info, vol_args->name, 0, &bdev, &mode);
David Sterba67ae34b2021-05-14 21:21:27 +02003524 if (!ret)
3525 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3526 btrfs_exclop_finish(fs_info);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003527 }
3528
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003529 kfree(vol_args);
David Sterba58d7bbf2016-05-04 14:10:47 +02003530out_drop_write:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003531 mnt_drop_write_file(file);
Josef Bacik3fa421d2021-07-27 17:01:17 -04003532 if (bdev)
3533 blkdev_put(bdev, mode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003534 return ret;
3535}
3536
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003537static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3538 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003539{
Li Zefan027ed2f2011-06-08 08:27:56 +00003540 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01003541 struct btrfs_device *device;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003542 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003543 u64 flags_in;
Li Zefan027ed2f2011-06-08 08:27:56 +00003544 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01003545
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003546 fi_args = memdup_user(arg, sizeof(*fi_args));
3547 if (IS_ERR(fi_args))
3548 return PTR_ERR(fi_args);
3549
3550 flags_in = fi_args->flags;
3551 memset(fi_args, 0, sizeof(*fi_args));
Li Zefan027ed2f2011-06-08 08:27:56 +00003552
David Sterbad03262c2017-06-16 00:09:21 +02003553 rcu_read_lock();
Li Zefan027ed2f2011-06-08 08:27:56 +00003554 fi_args->num_devices = fs_devices->num_devices;
Jan Schmidt475f6382011-03-11 15:41:01 +01003555
David Sterbad03262c2017-06-16 00:09:21 +02003556 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00003557 if (device->devid > fi_args->max_id)
3558 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01003559 }
David Sterbad03262c2017-06-16 00:09:21 +02003560 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003561
Nikolay Borisovde37aa52018-10-30 16:43:24 +02003562 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Omar Sandovalbea7eaf2017-08-22 23:46:00 -07003563 fi_args->nodesize = fs_info->nodesize;
3564 fi_args->sectorsize = fs_info->sectorsize;
3565 fi_args->clone_alignment = fs_info->sectorsize;
David Sterba80a773f2014-05-07 18:17:06 +02003566
Johannes Thumshirn137c5412020-07-13 21:28:58 +09003567 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3568 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3569 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3570 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3571 }
3572
Johannes Thumshirn0fb408a2020-07-13 21:28:59 +09003573 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3574 fi_args->generation = fs_info->generation;
3575 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3576 }
3577
Johannes Thumshirn49bac892020-07-13 21:29:00 +09003578 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3579 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3580 sizeof(fi_args->metadata_uuid));
3581 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3582 }
3583
Li Zefan027ed2f2011-06-08 08:27:56 +00003584 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3585 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01003586
Li Zefan027ed2f2011-06-08 08:27:56 +00003587 kfree(fi_args);
3588 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003589}
3590
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003591static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3592 void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003593{
3594 struct btrfs_ioctl_dev_info_args *di_args;
3595 struct btrfs_device *dev;
Jan Schmidt475f6382011-03-11 15:41:01 +01003596 int ret = 0;
3597 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01003598
Jan Schmidt475f6382011-03-11 15:41:01 +01003599 di_args = memdup_user(arg, sizeof(*di_args));
3600 if (IS_ERR(di_args))
3601 return PTR_ERR(di_args);
3602
Stefan Behrensdd5f9612013-08-15 17:11:20 +02003603 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01003604 s_uuid = di_args->uuid;
3605
David Sterbac5593ca2017-06-16 00:09:21 +02003606 rcu_read_lock();
Anand Jaine4319cd2019-01-17 23:32:31 +08003607 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
Anand Jainb2598ed2020-11-03 13:49:43 +08003608 NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01003609
3610 if (!dev) {
3611 ret = -ENODEV;
3612 goto out;
3613 }
3614
3615 di_args->devid = dev->devid;
Miao Xie7cc8e582014-09-03 21:35:38 +08003616 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3617 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
Jan Schmidt475f6382011-03-11 15:41:01 +01003618 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02003619 if (dev->name) {
Misono Tomohiro672d5992018-08-02 16:19:07 +09003620 strncpy(di_args->path, rcu_str_deref(dev->name),
3621 sizeof(di_args->path) - 1);
Jim Meyeringa27202f2012-04-26 18:36:56 +02003622 di_args->path[sizeof(di_args->path) - 1] = 0;
3623 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01003624 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02003625 }
Jan Schmidt475f6382011-03-11 15:41:01 +01003626
3627out:
David Sterbac5593ca2017-06-16 00:09:21 +02003628 rcu_read_unlock();
Jan Schmidt475f6382011-03-11 15:41:01 +01003629 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3630 ret = -EFAULT;
3631
3632 kfree(di_args);
3633 return ret;
3634}
3635
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003636static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3637{
Al Viro496ad9a2013-01-23 17:07:38 -05003638 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003639 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003640 struct btrfs_root *root = BTRFS_I(inode)->root;
3641 struct btrfs_root *new_root;
3642 struct btrfs_dir_item *di;
3643 struct btrfs_trans_handle *trans;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003644 struct btrfs_path *path = NULL;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003645 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003646 u64 objectid = 0;
3647 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003648 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003649
3650 if (!capable(CAP_SYS_ADMIN))
3651 return -EPERM;
3652
Miao Xie3c04ce02012-11-26 08:43:07 +00003653 ret = mnt_want_write_file(file);
3654 if (ret)
3655 return ret;
3656
3657 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3658 ret = -EFAULT;
3659 goto out;
3660 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003661
3662 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303663 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003664
David Sterba56e93572020-05-15 19:35:55 +02003665 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Miao Xie3c04ce02012-11-26 08:43:07 +00003666 if (IS_ERR(new_root)) {
3667 ret = PTR_ERR(new_root);
3668 goto out;
3669 }
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003670 if (!is_fstree(new_root->root_key.objectid)) {
3671 ret = -ENOENT;
3672 goto out_free;
3673 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003674
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003675 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003676 if (!path) {
3677 ret = -ENOMEM;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003678 goto out_free;
Miao Xie3c04ce02012-11-26 08:43:07 +00003679 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003680
3681 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003682 if (IS_ERR(trans)) {
Miao Xie3c04ce02012-11-26 08:43:07 +00003683 ret = PTR_ERR(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003684 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003685 }
3686
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003687 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3688 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003689 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003690 if (IS_ERR_OR_NULL(di)) {
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003691 btrfs_release_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003692 btrfs_end_transaction(trans);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003693 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003694 "Umm, you don't have the default diritem, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003695 ret = -ENOENT;
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003696 goto out_free;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003697 }
3698
3699 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3700 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3701 btrfs_mark_buffer_dirty(path->nodes[0]);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003702 btrfs_release_path(path);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003703
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003704 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003705 btrfs_end_transaction(trans);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003706out_free:
Josef Bacik00246522020-01-24 09:33:01 -05003707 btrfs_put_root(new_root);
Josef Bacik2a2b5d62020-01-24 09:32:37 -05003708 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003709out:
3710 mnt_drop_write_file(file);
3711 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003712}
3713
Su Yuec065f5b12018-04-02 17:24:11 +08003714static void get_block_group_info(struct list_head *groups_list,
3715 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003716{
David Sterba32da53862019-10-29 19:20:18 +01003717 struct btrfs_block_group *block_group;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003718
3719 space->total_bytes = 0;
3720 space->used_bytes = 0;
3721 space->flags = 0;
3722 list_for_each_entry(block_group, groups_list, list) {
3723 space->flags = block_group->flags;
David Sterbab3470b52019-10-23 18:48:22 +02003724 space->total_bytes += block_group->length;
David Sterbabf38be62019-10-23 18:48:11 +02003725 space->used_bytes += block_group->used;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003726 }
3727}
3728
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003729static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3730 void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003731{
3732 struct btrfs_ioctl_space_args space_args;
3733 struct btrfs_ioctl_space_info space;
3734 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003735 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003736 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003737 struct btrfs_space_info *info;
Colin Ian King315d8e92017-09-19 16:01:23 +01003738 static const u64 types[] = {
3739 BTRFS_BLOCK_GROUP_DATA,
3740 BTRFS_BLOCK_GROUP_SYSTEM,
3741 BTRFS_BLOCK_GROUP_METADATA,
3742 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3743 };
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003744 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003745 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003746 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003747 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003748 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003749
3750 if (copy_from_user(&space_args,
3751 (struct btrfs_ioctl_space_args __user *)arg,
3752 sizeof(space_args)))
3753 return -EFAULT;
3754
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003755 for (i = 0; i < num_types; i++) {
3756 struct btrfs_space_info *tmp;
3757
3758 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003759 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003760 if (tmp->flags == types[i]) {
3761 info = tmp;
3762 break;
3763 }
3764 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003765
3766 if (!info)
3767 continue;
3768
3769 down_read(&info->groups_sem);
3770 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3771 if (!list_empty(&info->block_groups[c]))
3772 slot_count++;
3773 }
3774 up_read(&info->groups_sem);
3775 }
Josef Bacik1406e432010-01-13 18:19:06 +00003776
David Sterba36523e952014-02-07 14:34:12 +01003777 /*
3778 * Global block reserve, exported as a space_info
3779 */
3780 slot_count++;
3781
Chris Mason7fde62b2010-03-16 15:40:10 -04003782 /* space_slots == 0 means they are asking for a count */
3783 if (space_args.space_slots == 0) {
3784 space_args.total_spaces = slot_count;
3785 goto out;
3786 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003787
Dan Rosenberg51788b12011-02-14 16:04:23 -05003788 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003789
Chris Mason7fde62b2010-03-16 15:40:10 -04003790 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003791
Chris Mason7fde62b2010-03-16 15:40:10 -04003792 /* we generally have at most 6 or so space infos, one for each raid
3793 * level. So, a whole page should be more than enough for everyone
3794 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003795 if (alloc_size > PAGE_SIZE)
Chris Mason7fde62b2010-03-16 15:40:10 -04003796 return -ENOMEM;
3797
3798 space_args.total_spaces = 0;
David Sterba8d2db782015-11-04 15:38:29 +01003799 dest = kmalloc(alloc_size, GFP_KERNEL);
Chris Mason7fde62b2010-03-16 15:40:10 -04003800 if (!dest)
3801 return -ENOMEM;
3802 dest_orig = dest;
3803
3804 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003805 for (i = 0; i < num_types; i++) {
3806 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003807
Dan Rosenberg51788b12011-02-14 16:04:23 -05003808 if (!slot_count)
3809 break;
3810
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003811 info = NULL;
Josef Bacik72804902020-09-01 17:40:37 -04003812 list_for_each_entry(tmp, &fs_info->space_info, list) {
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003813 if (tmp->flags == types[i]) {
3814 info = tmp;
3815 break;
3816 }
3817 }
Chris Mason7fde62b2010-03-16 15:40:10 -04003818
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003819 if (!info)
3820 continue;
3821 down_read(&info->groups_sem);
3822 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3823 if (!list_empty(&info->block_groups[c])) {
Su Yuec065f5b12018-04-02 17:24:11 +08003824 get_block_group_info(&info->block_groups[c],
3825 &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003826 memcpy(dest, &space, sizeof(space));
3827 dest++;
3828 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003829 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003830 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003831 if (!slot_count)
3832 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003833 }
3834 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003835 }
Josef Bacik1406e432010-01-13 18:19:06 +00003836
David Sterba36523e952014-02-07 14:34:12 +01003837 /*
3838 * Add global block reserve
3839 */
3840 if (slot_count) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003841 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
David Sterba36523e952014-02-07 14:34:12 +01003842
3843 spin_lock(&block_rsv->lock);
3844 space.total_bytes = block_rsv->size;
3845 space.used_bytes = block_rsv->size - block_rsv->reserved;
3846 spin_unlock(&block_rsv->lock);
3847 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3848 memcpy(dest, &space, sizeof(space));
3849 space_args.total_spaces++;
3850 }
3851
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003852 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003853 (arg + sizeof(struct btrfs_ioctl_space_args));
3854
3855 if (copy_to_user(user_dest, dest_orig, alloc_size))
3856 ret = -EFAULT;
3857
3858 kfree(dest_orig);
3859out:
3860 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003861 ret = -EFAULT;
3862
3863 return ret;
3864}
3865
Miao Xie9a8c28b2012-11-26 08:40:43 +00003866static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3867 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003868{
Sage Weil46204592010-10-29 15:41:32 -04003869 struct btrfs_trans_handle *trans;
3870 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003871 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003872
Miao Xied4edf392013-02-20 09:17:06 +00003873 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003874 if (IS_ERR(trans)) {
3875 if (PTR_ERR(trans) != -ENOENT)
3876 return PTR_ERR(trans);
3877
3878 /* No running transaction, don't bother */
3879 transid = root->fs_info->last_trans_committed;
3880 goto out;
3881 }
Sage Weil46204592010-10-29 15:41:32 -04003882 transid = trans->transid;
David Sterba32cc4f82021-06-03 17:20:21 +02003883 ret = btrfs_commit_transaction_async(trans);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003884 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003885 btrfs_end_transaction(trans);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003886 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003887 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003888out:
Sage Weil46204592010-10-29 15:41:32 -04003889 if (argp)
3890 if (copy_to_user(argp, &transid, sizeof(transid)))
3891 return -EFAULT;
3892 return 0;
3893}
3894
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003895static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
Miao Xie9a8c28b2012-11-26 08:40:43 +00003896 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003897{
Sage Weil46204592010-10-29 15:41:32 -04003898 u64 transid;
3899
3900 if (argp) {
3901 if (copy_from_user(&transid, argp, sizeof(transid)))
3902 return -EFAULT;
3903 } else {
3904 transid = 0; /* current trans */
3905 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003906 return btrfs_wait_for_commit(fs_info, transid);
Sage Weil46204592010-10-29 15:41:32 -04003907}
3908
Miao Xieb8e95482012-11-26 08:48:01 +00003909static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003910{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Jan Schmidt475f6382011-03-11 15:41:01 +01003912 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003913 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003914
3915 if (!capable(CAP_SYS_ADMIN))
3916 return -EPERM;
3917
3918 sa = memdup_user(arg, sizeof(*sa));
3919 if (IS_ERR(sa))
3920 return PTR_ERR(sa);
3921
Miao Xieb8e95482012-11-26 08:48:01 +00003922 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3923 ret = mnt_want_write_file(file);
3924 if (ret)
3925 goto out;
3926 }
3927
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003928 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003929 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3930 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003931
Filipe Manana5afe6ce2020-01-16 11:29:20 +00003932 /*
3933 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3934 * error. This is important as it allows user space to know how much
3935 * progress scrub has done. For example, if scrub is canceled we get
3936 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3937 * space. Later user space can inspect the progress from the structure
3938 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3939 * previously (btrfs-progs does this).
3940 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3941 * then return -EFAULT to signal the structure was not copied or it may
3942 * be corrupt and unreliable due to a partial copy.
3943 */
3944 if (copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003945 ret = -EFAULT;
3946
Miao Xieb8e95482012-11-26 08:48:01 +00003947 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3948 mnt_drop_write_file(file);
3949out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003950 kfree(sa);
3951 return ret;
3952}
3953
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003954static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
Jan Schmidt475f6382011-03-11 15:41:01 +01003955{
3956 if (!capable(CAP_SYS_ADMIN))
3957 return -EPERM;
3958
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003959 return btrfs_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003960}
3961
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003962static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
Jan Schmidt475f6382011-03-11 15:41:01 +01003963 void __user *arg)
3964{
3965 struct btrfs_ioctl_scrub_args *sa;
3966 int ret;
3967
3968 if (!capable(CAP_SYS_ADMIN))
3969 return -EPERM;
3970
3971 sa = memdup_user(arg, sizeof(*sa));
3972 if (IS_ERR(sa))
3973 return PTR_ERR(sa);
3974
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003975 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
Jan Schmidt475f6382011-03-11 15:41:01 +01003976
Filipe Manana4fa99b02018-12-14 19:45:13 +00003977 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Jan Schmidt475f6382011-03-11 15:41:01 +01003978 ret = -EFAULT;
3979
3980 kfree(sa);
3981 return ret;
3982}
3983
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003984static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06003985 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003986{
3987 struct btrfs_ioctl_get_dev_stats *sa;
3988 int ret;
3989
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003990 sa = memdup_user(arg, sizeof(*sa));
3991 if (IS_ERR(sa))
3992 return PTR_ERR(sa);
3993
David Sterbab27f7c02012-06-22 06:30:39 -06003994 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3995 kfree(sa);
3996 return -EPERM;
3997 }
3998
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003999 ret = btrfs_get_dev_stats(fs_info, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004000
Filipe Mananaeee99572018-12-14 19:45:22 +00004001 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004002 ret = -EFAULT;
4003
4004 kfree(sa);
4005 return ret;
4006}
4007
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004008static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4009 void __user *arg)
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004010{
4011 struct btrfs_ioctl_dev_replace_args *p;
4012 int ret;
4013
4014 if (!capable(CAP_SYS_ADMIN))
4015 return -EPERM;
4016
4017 p = memdup_user(arg, sizeof(*p));
4018 if (IS_ERR(p))
4019 return PTR_ERR(p);
4020
4021 switch (p->cmd) {
4022 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
David Howellsbc98a422017-07-17 08:45:34 +01004023 if (sb_rdonly(fs_info->sb)) {
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004024 ret = -EROFS;
4025 goto out;
4026 }
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004027 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
Anand Jaine57138b2013-08-21 11:44:48 +08004028 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004029 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004030 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004031 btrfs_exclop_finish(fs_info);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004032 }
4033 break;
4034 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 btrfs_dev_replace_status(fs_info, p);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004036 ret = 0;
4037 break;
4038 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
Anand Jain17d202b2018-02-12 23:33:30 +08004039 p->result = btrfs_dev_replace_cancel(fs_info);
Anand Jain97282032018-02-12 23:33:29 +08004040 ret = 0;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004041 break;
4042 default:
4043 ret = -EINVAL;
4044 break;
4045 }
4046
Filipe Mananad3a53282019-01-08 11:42:09 +00004047 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004048 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03004049out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004050 kfree(p);
4051 return ret;
4052}
4053
Jan Schmidtd7728c92011-07-07 16:48:38 +02004054static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4055{
4056 int ret = 0;
4057 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04004058 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004059 int size;
Chris Mason806468f2011-11-06 03:07:10 -05004060 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004061 struct inode_fs_paths *ipath = NULL;
4062 struct btrfs_path *path;
4063
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00004064 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02004065 return -EPERM;
4066
4067 path = btrfs_alloc_path();
4068 if (!path) {
4069 ret = -ENOMEM;
4070 goto out;
4071 }
4072
4073 ipa = memdup_user(arg, sizeof(*ipa));
4074 if (IS_ERR(ipa)) {
4075 ret = PTR_ERR(ipa);
4076 ipa = NULL;
4077 goto out;
4078 }
4079
4080 size = min_t(u32, ipa->size, 4096);
4081 ipath = init_ipath(size, root, path);
4082 if (IS_ERR(ipath)) {
4083 ret = PTR_ERR(ipath);
4084 ipath = NULL;
4085 goto out;
4086 }
4087
4088 ret = paths_from_inode(ipa->inum, ipath);
4089 if (ret < 0)
4090 goto out;
4091
4092 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05004093 rel_ptr = ipath->fspath->val[i] -
4094 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04004095 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004096 }
4097
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004098 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4099 ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004100 if (ret) {
4101 ret = -EFAULT;
4102 goto out;
4103 }
4104
4105out:
4106 btrfs_free_path(path);
4107 free_ipath(ipath);
4108 kfree(ipa);
4109
4110 return ret;
4111}
4112
4113static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4114{
4115 struct btrfs_data_container *inodes = ctx;
4116 const size_t c = 3 * sizeof(u64);
4117
4118 if (inodes->bytes_left >= c) {
4119 inodes->bytes_left -= c;
4120 inodes->val[inodes->elem_cnt] = inum;
4121 inodes->val[inodes->elem_cnt + 1] = offset;
4122 inodes->val[inodes->elem_cnt + 2] = root;
4123 inodes->elem_cnt += 3;
4124 } else {
4125 inodes->bytes_missing += c - inodes->bytes_left;
4126 inodes->bytes_left = 0;
4127 inodes->elem_missed += 3;
4128 }
4129
4130 return 0;
4131}
4132
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004133static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004134 void __user *arg, int version)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004135{
4136 int ret = 0;
4137 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004138 struct btrfs_ioctl_logical_ino_args *loi;
4139 struct btrfs_data_container *inodes = NULL;
4140 struct btrfs_path *path = NULL;
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004141 bool ignore_offset;
Jan Schmidtd7728c92011-07-07 16:48:38 +02004142
4143 if (!capable(CAP_SYS_ADMIN))
4144 return -EPERM;
4145
4146 loi = memdup_user(arg, sizeof(*loi));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304147 if (IS_ERR(loi))
4148 return PTR_ERR(loi);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004149
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004150 if (version == 1) {
4151 ignore_offset = false;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004152 size = min_t(u32, loi->size, SZ_64K);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004153 } else {
4154 /* All reserved bits must be 0 for now */
4155 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4156 ret = -EINVAL;
4157 goto out_loi;
4158 }
4159 /* Only accept flags we have defined so far */
4160 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4161 ret = -EINVAL;
4162 goto out_loi;
4163 }
4164 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
Zygo Blaxellb115e3b2017-09-22 13:58:47 -04004165 size = min_t(u32, loi->size, SZ_16M);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004166 }
4167
Jan Schmidtd7728c92011-07-07 16:48:38 +02004168 path = btrfs_alloc_path();
4169 if (!path) {
4170 ret = -ENOMEM;
4171 goto out;
4172 }
4173
Jan Schmidtd7728c92011-07-07 16:48:38 +02004174 inodes = init_data_container(size);
4175 if (IS_ERR(inodes)) {
4176 ret = PTR_ERR(inodes);
4177 inodes = NULL;
4178 goto out;
4179 }
4180
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004181 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004182 build_ino_list, inodes, ignore_offset);
Liu Bodf031f02012-09-07 20:01:29 -06004183 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02004184 ret = -ENOENT;
4185 if (ret < 0)
4186 goto out;
4187
Omar Sandoval718dc5f2017-08-22 23:46:05 -07004188 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4189 size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004190 if (ret)
4191 ret = -EFAULT;
4192
4193out:
4194 btrfs_free_path(path);
David Sterbaf54de062017-05-31 19:32:09 +02004195 kvfree(inodes);
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04004196out_loi:
Jan Schmidtd7728c92011-07-07 16:48:38 +02004197 kfree(loi);
4198
4199 return ret;
4200}
4201
David Sterba008ef092018-03-21 02:05:27 +01004202void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004203 struct btrfs_ioctl_balance_args *bargs)
4204{
4205 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4206
4207 bargs->flags = bctl->flags;
4208
David Sterba3009a622018-03-21 01:31:04 +01004209 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004210 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4211 if (atomic_read(&fs_info->balance_pause_req))
4212 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004213 if (atomic_read(&fs_info->balance_cancel_req))
4214 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004215
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004216 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4217 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4218 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004219
David Sterba008ef092018-03-21 02:05:27 +01004220 spin_lock(&fs_info->balance_lock);
4221 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4222 spin_unlock(&fs_info->balance_lock);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004223}
4224
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004225static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004226{
Al Viro496ad9a2013-01-23 17:07:38 -05004227 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004228 struct btrfs_fs_info *fs_info = root->fs_info;
4229 struct btrfs_ioctl_balance_args *bargs;
4230 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004231 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004232 int ret;
4233
4234 if (!capable(CAP_SYS_ADMIN))
4235 return -EPERM;
4236
Liu Boe54bfa312012-06-29 03:58:48 -06004237 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004238 if (ret)
4239 return ret;
4240
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004241again:
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004242 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004243 mutex_lock(&fs_info->balance_mutex);
4244 need_unlock = true;
4245 goto locked;
4246 }
4247
4248 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04004249 * mut. excl. ops lock is locked. Three possibilities:
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004250 * (1) some other op is running
4251 * (2) balance is running
4252 * (3) balance is paused -- special case (think resume)
4253 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004254 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004255 if (fs_info->balance_ctl) {
4256 /* this is either (2) or (3) */
David Sterba3009a622018-03-21 01:31:04 +01004257 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004258 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004259 /*
4260 * Lock released to allow other waiters to continue,
4261 * we'll reexamine the status again.
4262 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004263 mutex_lock(&fs_info->balance_mutex);
4264
4265 if (fs_info->balance_ctl &&
David Sterba3009a622018-03-21 01:31:04 +01004266 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004267 /* this is (3) */
4268 need_unlock = false;
4269 goto locked;
4270 }
4271
4272 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004273 goto again;
4274 } else {
4275 /* this is (2) */
4276 mutex_unlock(&fs_info->balance_mutex);
4277 ret = -EINPROGRESS;
4278 goto out;
4279 }
4280 } else {
4281 /* this is (1) */
4282 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004283 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004284 goto out;
4285 }
4286
4287locked:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004288
4289 if (arg) {
4290 bargs = memdup_user(arg, sizeof(*bargs));
4291 if (IS_ERR(bargs)) {
4292 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004293 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004294 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004295
4296 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4297 if (!fs_info->balance_ctl) {
4298 ret = -ENOTCONN;
4299 goto out_bargs;
4300 }
4301
4302 bctl = fs_info->balance_ctl;
4303 spin_lock(&fs_info->balance_lock);
4304 bctl->flags |= BTRFS_BALANCE_RESUME;
4305 spin_unlock(&fs_info->balance_lock);
4306
4307 goto do_balance;
4308 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004309 } else {
4310 bargs = NULL;
4311 }
4312
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004313 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004314 ret = -EINPROGRESS;
4315 goto out_bargs;
4316 }
4317
David Sterba8d2db782015-11-04 15:38:29 +01004318 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004319 if (!bctl) {
4320 ret = -ENOMEM;
4321 goto out_bargs;
4322 }
4323
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004324 if (arg) {
4325 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4326 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4327 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4328
4329 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004330 } else {
4331 /* balance everything - no filters */
4332 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004333 }
4334
David Sterba8eb93452015-10-12 16:55:54 +02004335 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4336 ret = -EINVAL;
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004337 goto out_bctl;
David Sterba8eb93452015-10-12 16:55:54 +02004338 }
4339
Ilya Dryomovde322262012-01-16 22:04:49 +02004340do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004341 /*
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004342 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4343 * bctl is freed in reset_balance_state, or, if restriper was paused
4344 * all the way until unmount, in free_fs_info. The flag should be
4345 * cleared after reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004346 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004347 need_unlock = false;
4348
David Sterba6fcf6e22018-05-07 17:44:03 +02004349 ret = btrfs_balance(fs_info, bctl, bargs);
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004350 bctl = NULL;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004351
Filipe Mananad00c2d92019-01-08 11:42:01 +00004352 if ((ret == 0 || ret == -ECANCELED) && arg) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004353 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4354 ret = -EFAULT;
4355 }
4356
Christian Engelmayer0f89abf2015-10-21 00:50:06 +02004357out_bctl:
4358 kfree(bctl);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004359out_bargs:
4360 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004361out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004362 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004363 if (need_unlock)
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004364 btrfs_exclop_finish(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004365out:
Liu Boe54bfa312012-06-29 03:58:48 -06004366 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004367 return ret;
4368}
4369
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004370static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004371{
4372 if (!capable(CAP_SYS_ADMIN))
4373 return -EPERM;
4374
4375 switch (cmd) {
4376 case BTRFS_BALANCE_CTL_PAUSE:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 return btrfs_pause_balance(fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004378 case BTRFS_BALANCE_CTL_CANCEL:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004379 return btrfs_cancel_balance(fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004380 }
4381
4382 return -EINVAL;
4383}
4384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004385static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004386 void __user *arg)
4387{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004388 struct btrfs_ioctl_balance_args *bargs;
4389 int ret = 0;
4390
4391 if (!capable(CAP_SYS_ADMIN))
4392 return -EPERM;
4393
4394 mutex_lock(&fs_info->balance_mutex);
4395 if (!fs_info->balance_ctl) {
4396 ret = -ENOTCONN;
4397 goto out;
4398 }
4399
David Sterba8d2db782015-11-04 15:38:29 +01004400 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004401 if (!bargs) {
4402 ret = -ENOMEM;
4403 goto out;
4404 }
4405
David Sterba008ef092018-03-21 02:05:27 +01004406 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004407
4408 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4409 ret = -EFAULT;
4410
4411 kfree(bargs);
4412out:
4413 mutex_unlock(&fs_info->balance_mutex);
4414 return ret;
4415}
4416
Miao Xie905b0dd2012-11-26 08:50:11 +00004417static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004418{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004419 struct inode *inode = file_inode(file);
4420 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Arne Jansen5d13a372011-09-14 15:53:51 +02004421 struct btrfs_ioctl_quota_ctl_args *sa;
Arne Jansen5d13a372011-09-14 15:53:51 +02004422 int ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004423
4424 if (!capable(CAP_SYS_ADMIN))
4425 return -EPERM;
4426
Miao Xie905b0dd2012-11-26 08:50:11 +00004427 ret = mnt_want_write_file(file);
4428 if (ret)
4429 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004430
4431 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004432 if (IS_ERR(sa)) {
4433 ret = PTR_ERR(sa);
4434 goto drop_write;
4435 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004436
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 down_write(&fs_info->subvol_sem);
Arne Jansen5d13a372011-09-14 15:53:51 +02004438
4439 switch (sa->cmd) {
4440 case BTRFS_QUOTA_CTL_ENABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004441 ret = btrfs_quota_enable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004442 break;
4443 case BTRFS_QUOTA_CTL_DISABLE:
Nikolay Borisov340f1aa2018-07-05 14:50:48 +03004444 ret = btrfs_quota_disable(fs_info);
Arne Jansen5d13a372011-09-14 15:53:51 +02004445 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004446 default:
4447 ret = -EINVAL;
4448 break;
4449 }
4450
Arne Jansen5d13a372011-09-14 15:53:51 +02004451 kfree(sa);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004452 up_write(&fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004453drop_write:
4454 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004455 return ret;
4456}
4457
Miao Xie905b0dd2012-11-26 08:50:11 +00004458static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004460 struct inode *inode = file_inode(file);
4461 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4462 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004463 struct btrfs_ioctl_qgroup_assign_args *sa;
4464 struct btrfs_trans_handle *trans;
4465 int ret;
4466 int err;
4467
4468 if (!capable(CAP_SYS_ADMIN))
4469 return -EPERM;
4470
Miao Xie905b0dd2012-11-26 08:50:11 +00004471 ret = mnt_want_write_file(file);
4472 if (ret)
4473 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004474
4475 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004476 if (IS_ERR(sa)) {
4477 ret = PTR_ERR(sa);
4478 goto drop_write;
4479 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004480
4481 trans = btrfs_join_transaction(root);
4482 if (IS_ERR(trans)) {
4483 ret = PTR_ERR(trans);
4484 goto out;
4485 }
4486
Arne Jansen5d13a372011-09-14 15:53:51 +02004487 if (sa->assign) {
Lu Fengqi9f8a6ce2018-07-18 14:45:30 +08004488 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004489 } else {
Lu Fengqi39616c22018-07-18 14:45:32 +08004490 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
Arne Jansen5d13a372011-09-14 15:53:51 +02004491 }
4492
Qu Wenruoe082f562015-02-27 16:24:28 +08004493 /* update qgroup status and info */
Lu Fengqi280f8bd2018-07-18 14:45:40 +08004494 err = btrfs_run_qgroups(trans);
Qu Wenruoe082f562015-02-27 16:24:28 +08004495 if (err < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 btrfs_handle_fs_error(fs_info, err,
4497 "failed to update qgroup status and info");
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004498 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004499 if (err && !ret)
4500 ret = err;
4501
4502out:
4503 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004504drop_write:
4505 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004506 return ret;
4507}
4508
Miao Xie905b0dd2012-11-26 08:50:11 +00004509static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004511 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004512 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004513 struct btrfs_ioctl_qgroup_create_args *sa;
4514 struct btrfs_trans_handle *trans;
4515 int ret;
4516 int err;
4517
4518 if (!capable(CAP_SYS_ADMIN))
4519 return -EPERM;
4520
Miao Xie905b0dd2012-11-26 08:50:11 +00004521 ret = mnt_want_write_file(file);
4522 if (ret)
4523 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004524
4525 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004526 if (IS_ERR(sa)) {
4527 ret = PTR_ERR(sa);
4528 goto drop_write;
4529 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004530
Miao Xied86e56c2012-11-15 11:35:41 +00004531 if (!sa->qgroupid) {
4532 ret = -EINVAL;
4533 goto out;
4534 }
4535
Arne Jansen5d13a372011-09-14 15:53:51 +02004536 trans = btrfs_join_transaction(root);
4537 if (IS_ERR(trans)) {
4538 ret = PTR_ERR(trans);
4539 goto out;
4540 }
4541
Arne Jansen5d13a372011-09-14 15:53:51 +02004542 if (sa->create) {
Lu Fengqi49a05ec2018-07-18 14:45:33 +08004543 ret = btrfs_create_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004544 } else {
Lu Fengqi3efbee12018-07-18 14:45:34 +08004545 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
Arne Jansen5d13a372011-09-14 15:53:51 +02004546 }
4547
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004548 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004549 if (err && !ret)
4550 ret = err;
4551
4552out:
4553 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004554drop_write:
4555 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004556 return ret;
4557}
4558
Miao Xie905b0dd2012-11-26 08:50:11 +00004559static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004560{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004562 struct btrfs_root *root = BTRFS_I(inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004563 struct btrfs_ioctl_qgroup_limit_args *sa;
4564 struct btrfs_trans_handle *trans;
4565 int ret;
4566 int err;
4567 u64 qgroupid;
4568
4569 if (!capable(CAP_SYS_ADMIN))
4570 return -EPERM;
4571
Miao Xie905b0dd2012-11-26 08:50:11 +00004572 ret = mnt_want_write_file(file);
4573 if (ret)
4574 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004575
4576 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004577 if (IS_ERR(sa)) {
4578 ret = PTR_ERR(sa);
4579 goto drop_write;
4580 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004581
4582 trans = btrfs_join_transaction(root);
4583 if (IS_ERR(trans)) {
4584 ret = PTR_ERR(trans);
4585 goto out;
4586 }
4587
4588 qgroupid = sa->qgroupid;
4589 if (!qgroupid) {
4590 /* take the current subvol as qgroup */
4591 qgroupid = root->root_key.objectid;
4592 }
4593
Lu Fengqif0042d52018-07-18 14:45:35 +08004594 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
Arne Jansen5d13a372011-09-14 15:53:51 +02004595
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004596 err = btrfs_end_transaction(trans);
Arne Jansen5d13a372011-09-14 15:53:51 +02004597 if (err && !ret)
4598 ret = err;
4599
4600out:
4601 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004602drop_write:
4603 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004604 return ret;
4605}
4606
Jan Schmidt2f232032013-04-25 16:04:51 +00004607static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004609 struct inode *inode = file_inode(file);
4610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jan Schmidt2f232032013-04-25 16:04:51 +00004611 struct btrfs_ioctl_quota_rescan_args *qsa;
4612 int ret;
4613
4614 if (!capable(CAP_SYS_ADMIN))
4615 return -EPERM;
4616
4617 ret = mnt_want_write_file(file);
4618 if (ret)
4619 return ret;
4620
4621 qsa = memdup_user(arg, sizeof(*qsa));
4622 if (IS_ERR(qsa)) {
4623 ret = PTR_ERR(qsa);
4624 goto drop_write;
4625 }
4626
4627 if (qsa->flags) {
4628 ret = -EINVAL;
4629 goto out;
4630 }
4631
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004632 ret = btrfs_qgroup_rescan(fs_info);
Jan Schmidt2f232032013-04-25 16:04:51 +00004633
4634out:
4635 kfree(qsa);
4636drop_write:
4637 mnt_drop_write_file(file);
4638 return ret;
4639}
4640
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004641static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4642 void __user *arg)
Jan Schmidt2f232032013-04-25 16:04:51 +00004643{
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004644 struct btrfs_ioctl_quota_rescan_args qsa = {0};
Jan Schmidt2f232032013-04-25 16:04:51 +00004645
4646 if (!capable(CAP_SYS_ADMIN))
4647 return -EPERM;
4648
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004650 qsa.flags = 1;
4651 qsa.progress = fs_info->qgroup_rescan_progress.objectid;
Jan Schmidt2f232032013-04-25 16:04:51 +00004652 }
4653
Goldwyn Rodrigues0afb6032021-07-27 16:17:29 -05004654 if (copy_to_user(arg, &qsa, sizeof(qsa)))
Anand Jain991a3da2021-07-28 14:20:41 +08004655 return -EFAULT;
Jan Schmidt2f232032013-04-25 16:04:51 +00004656
Anand Jain991a3da2021-07-28 14:20:41 +08004657 return 0;
Jan Schmidt2f232032013-04-25 16:04:51 +00004658}
4659
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004660static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4661 void __user *arg)
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004662{
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004663 if (!capable(CAP_SYS_ADMIN))
4664 return -EPERM;
4665
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004666 return btrfs_qgroup_wait_for_completion(fs_info, true);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004667}
4668
Hugo Millsabccd002014-01-30 20:17:00 +00004669static long _btrfs_ioctl_set_received_subvol(struct file *file,
Christian Braunere4fed172021-07-27 12:48:55 +02004670 struct user_namespace *mnt_userns,
Hugo Millsabccd002014-01-30 20:17:00 +00004671 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004672{
Al Viro496ad9a2013-01-23 17:07:38 -05004673 struct inode *inode = file_inode(file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004674 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Alexander Block8ea05e32012-07-25 17:35:53 +02004675 struct btrfs_root *root = BTRFS_I(inode)->root;
4676 struct btrfs_root_item *root_item = &root->root_item;
4677 struct btrfs_trans_handle *trans;
Deepa Dinamani95582b02018-05-08 19:36:02 -07004678 struct timespec64 ct = current_time(inode);
Alexander Block8ea05e32012-07-25 17:35:53 +02004679 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004680 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004681
Christian Braunere4fed172021-07-27 12:48:55 +02004682 if (!inode_owner_or_capable(mnt_userns, inode))
David Sterbabd60ea02014-01-16 15:50:22 +01004683 return -EPERM;
4684
Alexander Block8ea05e32012-07-25 17:35:53 +02004685 ret = mnt_want_write_file(file);
4686 if (ret < 0)
4687 return ret;
4688
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004689 down_write(&fs_info->subvol_sem);
Alexander Block8ea05e32012-07-25 17:35:53 +02004690
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004691 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004692 ret = -EINVAL;
4693 goto out;
4694 }
4695
4696 if (btrfs_root_readonly(root)) {
4697 ret = -EROFS;
4698 goto out;
4699 }
4700
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004701 /*
4702 * 1 - root item
4703 * 2 - uuid items (received uuid + subvol uuid)
4704 */
4705 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004706 if (IS_ERR(trans)) {
4707 ret = PTR_ERR(trans);
4708 trans = NULL;
4709 goto out;
4710 }
4711
4712 sa->rtransid = trans->transid;
4713 sa->rtime.sec = ct.tv_sec;
4714 sa->rtime.nsec = ct.tv_nsec;
4715
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004716 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4717 BTRFS_UUID_SIZE);
4718 if (received_uuid_changed &&
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004719 !btrfs_is_empty_uuid(root_item->received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004720 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
Nikolay Borisovd87ff752018-03-12 14:48:09 +02004721 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4722 root->root_key.objectid);
4723 if (ret && ret != -ENOENT) {
4724 btrfs_abort_transaction(trans, ret);
4725 btrfs_end_transaction(trans);
4726 goto out;
4727 }
4728 }
Alexander Block8ea05e32012-07-25 17:35:53 +02004729 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4730 btrfs_set_root_stransid(root_item, sa->stransid);
4731 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004732 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4733 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4734 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4735 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004736
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004737 ret = btrfs_update_root(trans, fs_info->tree_root,
Alexander Block8ea05e32012-07-25 17:35:53 +02004738 &root->root_key, &root->root_item);
4739 if (ret < 0) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004740 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004741 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004742 }
4743 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004744 ret = btrfs_uuid_tree_add(trans, sa->uuid,
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004745 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4746 root->root_key.objectid);
4747 if (ret < 0 && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004748 btrfs_abort_transaction(trans, ret);
Nikolay Borisovefd38152017-09-28 11:45:26 +03004749 btrfs_end_transaction(trans);
Alexander Block8ea05e32012-07-25 17:35:53 +02004750 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004751 }
4752 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004753 ret = btrfs_commit_transaction(trans);
Hugo Millsabccd002014-01-30 20:17:00 +00004754out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004755 up_write(&fs_info->subvol_sem);
Hugo Millsabccd002014-01-30 20:17:00 +00004756 mnt_drop_write_file(file);
4757 return ret;
4758}
4759
4760#ifdef CONFIG_64BIT
4761static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4762 void __user *arg)
4763{
4764 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4765 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4766 int ret = 0;
4767
4768 args32 = memdup_user(arg, sizeof(*args32));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304769 if (IS_ERR(args32))
4770 return PTR_ERR(args32);
Hugo Millsabccd002014-01-30 20:17:00 +00004771
David Sterba8d2db782015-11-04 15:38:29 +01004772 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
Dan Carpenter84dbeb82014-03-28 11:06:00 +03004773 if (!args64) {
4774 ret = -ENOMEM;
Hugo Millsabccd002014-01-30 20:17:00 +00004775 goto out;
4776 }
4777
4778 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4779 args64->stransid = args32->stransid;
4780 args64->rtransid = args32->rtransid;
4781 args64->stime.sec = args32->stime.sec;
4782 args64->stime.nsec = args32->stime.nsec;
4783 args64->rtime.sec = args32->rtime.sec;
4784 args64->rtime.nsec = args32->rtime.nsec;
4785 args64->flags = args32->flags;
4786
Christian Braunere4fed172021-07-27 12:48:55 +02004787 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), args64);
Hugo Millsabccd002014-01-30 20:17:00 +00004788 if (ret)
4789 goto out;
4790
4791 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4792 args32->stransid = args64->stransid;
4793 args32->rtransid = args64->rtransid;
4794 args32->stime.sec = args64->stime.sec;
4795 args32->stime.nsec = args64->stime.nsec;
4796 args32->rtime.sec = args64->rtime.sec;
4797 args32->rtime.nsec = args64->rtime.nsec;
4798 args32->flags = args64->flags;
4799
4800 ret = copy_to_user(arg, args32, sizeof(*args32));
4801 if (ret)
4802 ret = -EFAULT;
4803
4804out:
4805 kfree(args32);
4806 kfree(args64);
4807 return ret;
4808}
4809#endif
4810
4811static long btrfs_ioctl_set_received_subvol(struct file *file,
4812 void __user *arg)
4813{
4814 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4815 int ret = 0;
4816
4817 sa = memdup_user(arg, sizeof(*sa));
Shailendra Verma7b9ea622016-11-10 15:17:41 +05304818 if (IS_ERR(sa))
4819 return PTR_ERR(sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004820
Christian Braunere4fed172021-07-27 12:48:55 +02004821 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_user_ns(file), sa);
Hugo Millsabccd002014-01-30 20:17:00 +00004822
4823 if (ret)
4824 goto out;
4825
Alexander Block8ea05e32012-07-25 17:35:53 +02004826 ret = copy_to_user(arg, sa, sizeof(*sa));
4827 if (ret)
4828 ret = -EFAULT;
4829
4830out:
4831 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004832 return ret;
4833}
4834
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004835static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4836 void __user *arg)
jeff.liu867ab662013-01-05 02:48:01 +00004837{
Anand Jaina1b83ac2013-07-19 17:39:32 +08004838 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004839 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004840 char label[BTRFS_LABEL_SIZE];
4841
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 spin_lock(&fs_info->super_lock);
4843 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4844 spin_unlock(&fs_info->super_lock);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004845
4846 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004847
4848 if (len == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004849 btrfs_warn(fs_info,
4850 "label is too long, return the first %zu bytes",
4851 --len);
jeff.liu867ab662013-01-05 02:48:01 +00004852 }
4853
jeff.liu867ab662013-01-05 02:48:01 +00004854 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004855
4856 return ret ? -EFAULT : 0;
4857}
4858
jeff.liua8bfd4a2013-01-05 02:48:08 +00004859static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4860{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004861 struct inode *inode = file_inode(file);
4862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4863 struct btrfs_root *root = BTRFS_I(inode)->root;
4864 struct btrfs_super_block *super_block = fs_info->super_copy;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004865 struct btrfs_trans_handle *trans;
4866 char label[BTRFS_LABEL_SIZE];
4867 int ret;
4868
4869 if (!capable(CAP_SYS_ADMIN))
4870 return -EPERM;
4871
4872 if (copy_from_user(label, arg, sizeof(label)))
4873 return -EFAULT;
4874
4875 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004877 "unable to set label with more than %d bytes",
4878 BTRFS_LABEL_SIZE - 1);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004879 return -EINVAL;
4880 }
4881
4882 ret = mnt_want_write_file(file);
4883 if (ret)
4884 return ret;
4885
jeff.liua8bfd4a2013-01-05 02:48:08 +00004886 trans = btrfs_start_transaction(root, 0);
4887 if (IS_ERR(trans)) {
4888 ret = PTR_ERR(trans);
4889 goto out_unlock;
4890 }
4891
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004892 spin_lock(&fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004893 strcpy(super_block->label, label);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004894 spin_unlock(&fs_info->super_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004895 ret = btrfs_commit_transaction(trans);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004896
4897out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004898 mnt_drop_write_file(file);
4899 return ret;
4900}
4901
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004902#define INIT_FEATURE_FLAGS(suffix) \
4903 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4904 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4905 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4906
David Sterbad5131b62016-02-17 15:26:27 +01004907int btrfs_ioctl_get_supported_features(void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004908{
David Sterba4d4ab6d2015-11-19 11:42:31 +01004909 static const struct btrfs_ioctl_feature_flags features[3] = {
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004910 INIT_FEATURE_FLAGS(SUPP),
4911 INIT_FEATURE_FLAGS(SAFE_SET),
4912 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4913 };
4914
4915 if (copy_to_user(arg, &features, sizeof(features)))
4916 return -EFAULT;
4917
4918 return 0;
4919}
4920
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03004921static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4922 void __user *arg)
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004923{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004924 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004925 struct btrfs_ioctl_feature_flags features;
4926
4927 features.compat_flags = btrfs_super_compat_flags(super_block);
4928 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4929 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4930
4931 if (copy_to_user(arg, &features, sizeof(features)))
4932 return -EFAULT;
4933
4934 return 0;
4935}
4936
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004937static int check_feature_bits(struct btrfs_fs_info *fs_info,
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004938 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004939 u64 change_mask, u64 flags, u64 supported_flags,
4940 u64 safe_set, u64 safe_clear)
4941{
David Sterbaf10152b2019-08-01 19:07:55 +02004942 const char *type = btrfs_feature_set_name(set);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004943 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004944 u64 disallowed, unsupported;
4945 u64 set_mask = flags & change_mask;
4946 u64 clear_mask = ~flags & change_mask;
4947
4948 unsupported = set_mask & ~supported_flags;
4949 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004950 names = btrfs_printable_features(set, unsupported);
4951 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004952 btrfs_warn(fs_info,
4953 "this kernel does not support the %s feature bit%s",
4954 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004955 kfree(names);
4956 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004957 btrfs_warn(fs_info,
4958 "this kernel does not support %s bits 0x%llx",
4959 type, unsupported);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004960 return -EOPNOTSUPP;
4961 }
4962
4963 disallowed = set_mask & ~safe_set;
4964 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004965 names = btrfs_printable_features(set, disallowed);
4966 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 btrfs_warn(fs_info,
4968 "can't set the %s feature bit%s while mounted",
4969 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004970 kfree(names);
4971 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004972 btrfs_warn(fs_info,
4973 "can't set %s bits 0x%llx while mounted",
4974 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004975 return -EPERM;
4976 }
4977
4978 disallowed = clear_mask & ~safe_clear;
4979 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004980 names = btrfs_printable_features(set, disallowed);
4981 if (names) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004982 btrfs_warn(fs_info,
4983 "can't clear the %s feature bit%s while mounted",
4984 names, strchr(names, ',') ? "s" : "");
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004985 kfree(names);
4986 } else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004987 btrfs_warn(fs_info,
4988 "can't clear %s bits 0x%llx while mounted",
4989 type, disallowed);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004990 return -EPERM;
4991 }
4992
4993 return 0;
4994}
4995
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004996#define check_feature(fs_info, change_mask, flags, mask_base) \
4997check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004998 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4999 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5000 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5001
5002static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5003{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005004 struct inode *inode = file_inode(file);
5005 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5006 struct btrfs_root *root = BTRFS_I(inode)->root;
5007 struct btrfs_super_block *super_block = fs_info->super_copy;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005008 struct btrfs_ioctl_feature_flags flags[2];
5009 struct btrfs_trans_handle *trans;
5010 u64 newflags;
5011 int ret;
5012
5013 if (!capable(CAP_SYS_ADMIN))
5014 return -EPERM;
5015
5016 if (copy_from_user(flags, arg, sizeof(flags)))
5017 return -EFAULT;
5018
5019 /* Nothing to do */
5020 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5021 !flags[0].incompat_flags)
5022 return 0;
5023
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005024 ret = check_feature(fs_info, flags[0].compat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005025 flags[1].compat_flags, COMPAT);
5026 if (ret)
5027 return ret;
5028
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005029 ret = check_feature(fs_info, flags[0].compat_ro_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005030 flags[1].compat_ro_flags, COMPAT_RO);
5031 if (ret)
5032 return ret;
5033
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005034 ret = check_feature(fs_info, flags[0].incompat_flags,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005035 flags[1].incompat_flags, INCOMPAT);
5036 if (ret)
5037 return ret;
5038
David Sterba7ab19622016-05-04 11:32:00 +02005039 ret = mnt_want_write_file(file);
5040 if (ret)
5041 return ret;
5042
David Sterba8051aa12014-02-07 14:34:04 +01005043 trans = btrfs_start_transaction(root, 0);
David Sterba7ab19622016-05-04 11:32:00 +02005044 if (IS_ERR(trans)) {
5045 ret = PTR_ERR(trans);
5046 goto out_drop_write;
5047 }
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005048
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005049 spin_lock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005050 newflags = btrfs_super_compat_flags(super_block);
5051 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5052 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5053 btrfs_set_super_compat_flags(super_block, newflags);
5054
5055 newflags = btrfs_super_compat_ro_flags(super_block);
5056 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5057 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5058 btrfs_set_super_compat_ro_flags(super_block, newflags);
5059
5060 newflags = btrfs_super_incompat_flags(super_block);
5061 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5062 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5063 btrfs_set_super_incompat_flags(super_block, newflags);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005064 spin_unlock(&fs_info->super_lock);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005065
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005066 ret = btrfs_commit_transaction(trans);
David Sterba7ab19622016-05-04 11:32:00 +02005067out_drop_write:
5068 mnt_drop_write_file(file);
5069
5070 return ret;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005071}
5072
Josef Bacik2351f432017-09-27 10:43:13 -04005073static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5074{
5075 struct btrfs_ioctl_send_args *arg;
5076 int ret;
5077
5078 if (compat) {
5079#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5080 struct btrfs_ioctl_send_args_32 args32;
5081
5082 ret = copy_from_user(&args32, argp, sizeof(args32));
5083 if (ret)
5084 return -EFAULT;
5085 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5086 if (!arg)
5087 return -ENOMEM;
5088 arg->send_fd = args32.send_fd;
5089 arg->clone_sources_count = args32.clone_sources_count;
5090 arg->clone_sources = compat_ptr(args32.clone_sources);
5091 arg->parent_root = args32.parent_root;
5092 arg->flags = args32.flags;
5093 memcpy(arg->reserved, args32.reserved,
5094 sizeof(args32.reserved));
5095#else
5096 return -ENOTTY;
5097#endif
5098 } else {
5099 arg = memdup_user(argp, sizeof(*arg));
5100 if (IS_ERR(arg))
5101 return PTR_ERR(arg);
5102 }
5103 ret = btrfs_ioctl_send(file, arg);
5104 kfree(arg);
5105 return ret;
5106}
5107
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005108long btrfs_ioctl(struct file *file, unsigned int
5109 cmd, unsigned long arg)
5110{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005111 struct inode *inode = file_inode(file);
5112 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5113 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05005114 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005115
5116 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005117 case FS_IOC_GETVERSION:
5118 return btrfs_ioctl_getversion(file, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005119 case FS_IOC_GETFSLABEL:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005120 return btrfs_ioctl_get_fslabel(fs_info, argp);
Eric Sandeen40cf9312019-07-17 12:39:20 -05005121 case FS_IOC_SETFSLABEL:
5122 return btrfs_ioctl_set_fslabel(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00005123 case FITRIM:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005124 return btrfs_ioctl_fitrim(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005125 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005126 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00005127 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005128 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05005129 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08005130 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02005131 case BTRFS_IOC_SUBVOL_CREATE_V2:
5132 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04005133 case BTRFS_IOC_SNAP_DESTROY:
Marcos Paulo de Souza949964c2020-02-07 10:05:46 -03005134 return btrfs_ioctl_snap_destroy(file, argp, false);
5135 case BTRFS_IOC_SNAP_DESTROY_V2:
5136 return btrfs_ioctl_snap_destroy(file, argp, true);
Li Zefan0caa1022010-12-20 16:30:25 +08005137 case BTRFS_IOC_SUBVOL_GETFLAGS:
5138 return btrfs_ioctl_subvol_getflags(file, argp);
5139 case BTRFS_IOC_SUBVOL_SETFLAGS:
5140 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00005141 case BTRFS_IOC_DEFAULT_SUBVOL:
5142 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005143 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05005144 return btrfs_ioctl_defrag(file, NULL);
5145 case BTRFS_IOC_DEFRAG_RANGE:
5146 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005147 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00005148 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005149 case BTRFS_IOC_ADD_DEV:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005150 return btrfs_ioctl_add_dev(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005151 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00005152 return btrfs_ioctl_rm_dev(file, argp);
Anand Jain6b526ed2016-02-13 10:01:39 +08005153 case BTRFS_IOC_RM_DEV_V2:
5154 return btrfs_ioctl_rm_dev_v2(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005155 case BTRFS_IOC_FS_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005156 return btrfs_ioctl_fs_info(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005157 case BTRFS_IOC_DEV_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005158 return btrfs_ioctl_dev_info(fs_info, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005159 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005160 return btrfs_ioctl_balance(file, NULL);
Chris Masonac8e9812010-02-28 15:39:26 -05005161 case BTRFS_IOC_TREE_SEARCH:
5162 return btrfs_ioctl_tree_search(file, argp);
Gerhard Heiftcc68a8a2014-01-30 16:24:03 +01005163 case BTRFS_IOC_TREE_SEARCH_V2:
5164 return btrfs_ioctl_tree_search_v2(file, argp);
Chris Masonac8e9812010-02-28 15:39:26 -05005165 case BTRFS_IOC_INO_LOOKUP:
5166 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02005167 case BTRFS_IOC_INO_PATHS:
5168 return btrfs_ioctl_ino_to_path(root, argp);
5169 case BTRFS_IOC_LOGICAL_INO:
Zygo Blaxelld24a67b2017-09-22 13:58:46 -04005170 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5171 case BTRFS_IOC_LOGICAL_INO_V2:
5172 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
Josef Bacik1406e432010-01-13 18:19:06 +00005173 case BTRFS_IOC_SPACE_INFO:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005174 return btrfs_ioctl_space_info(fs_info, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005175 case BTRFS_IOC_SYNC: {
5176 int ret;
5177
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02005178 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005179 if (ret)
5180 return ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005181 ret = btrfs_sync_fs(inode->i_sb, 1);
David Sterba2fad4e82014-07-23 14:39:35 +02005182 /*
5183 * The transaction thread may want to do more work,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005184 * namely it pokes the cleaner kthread that will start
David Sterba2fad4e82014-07-23 14:39:35 +02005185 * processing uncleaned subvols.
5186 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005187 wake_up_process(fs_info->transaction_kthread);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01005188 return ret;
5189 }
Sage Weil46204592010-10-29 15:41:32 -04005190 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00005191 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04005192 case BTRFS_IOC_WAIT_SYNC:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005193 return btrfs_ioctl_wait_sync(fs_info, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005194 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00005195 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01005196 case BTRFS_IOC_SCRUB_CANCEL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005197 return btrfs_ioctl_scrub_cancel(fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01005198 case BTRFS_IOC_SCRUB_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005199 return btrfs_ioctl_scrub_progress(fs_info, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02005200 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08005201 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02005202 case BTRFS_IOC_BALANCE_CTL:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005203 return btrfs_ioctl_balance_ctl(fs_info, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02005204 case BTRFS_IOC_BALANCE_PROGRESS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005205 return btrfs_ioctl_balance_progress(fs_info, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02005206 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5207 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00005208#ifdef CONFIG_64BIT
5209 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5210 return btrfs_ioctl_set_received_subvol_32(file, argp);
5211#endif
Alexander Block31db9f72012-07-25 23:19:24 +02005212 case BTRFS_IOC_SEND:
Josef Bacik2351f432017-09-27 10:43:13 -04005213 return _btrfs_ioctl_send(file, argp, false);
5214#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5215 case BTRFS_IOC_SEND_32:
5216 return _btrfs_ioctl_send(file, argp, true);
5217#endif
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005218 case BTRFS_IOC_GET_DEV_STATS:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005219 return btrfs_ioctl_get_dev_stats(fs_info, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005220 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00005221 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005222 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00005223 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005224 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00005225 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02005226 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00005227 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00005228 case BTRFS_IOC_QUOTA_RESCAN:
5229 return btrfs_ioctl_quota_rescan(file, argp);
5230 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005231 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00005232 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005233 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01005234 case BTRFS_IOC_DEV_REPLACE:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005235 return btrfs_ioctl_dev_replace(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005236 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01005237 return btrfs_ioctl_get_supported_features(argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005238 case BTRFS_IOC_GET_FEATURES:
Marcos Paulo de Souzab929c1d2019-10-10 21:23:11 -03005239 return btrfs_ioctl_get_features(fs_info, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05005240 case BTRFS_IOC_SET_FEATURES:
5241 return btrfs_ioctl_set_features(file, argp);
Tomohiro Misonob64ec072018-05-21 10:09:42 +09005242 case BTRFS_IOC_GET_SUBVOL_INFO:
5243 return btrfs_ioctl_get_subvol_info(file, argp);
Tomohiro Misono42e4b522018-05-21 10:09:43 +09005244 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5245 return btrfs_ioctl_get_subvol_rootref(file, argp);
Tomohiro Misono23d0b792018-05-21 10:09:44 +09005246 case BTRFS_IOC_INO_LOOKUP_USER:
5247 return btrfs_ioctl_ino_lookup_user(file, argp);
Boris Burkov14605402021-06-30 13:01:49 -07005248 case FS_IOC_ENABLE_VERITY:
5249 return fsverity_ioctl_enable(file, (const void __user *)argp);
5250 case FS_IOC_MEASURE_VERITY:
5251 return fsverity_ioctl_measure(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005252 }
5253
5254 return -ENOTTY;
5255}
Luke Dashjr4c63c242015-10-29 08:22:21 +00005256
5257#ifdef CONFIG_COMPAT
5258long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5259{
Jeff Mahoney2a362242017-02-06 19:39:09 -05005260 /*
5261 * These all access 32-bit values anyway so no further
5262 * handling is necessary.
5263 */
Luke Dashjr4c63c242015-10-29 08:22:21 +00005264 switch (cmd) {
Luke Dashjr4c63c242015-10-29 08:22:21 +00005265 case FS_IOC32_GETVERSION:
5266 cmd = FS_IOC_GETVERSION;
5267 break;
Luke Dashjr4c63c242015-10-29 08:22:21 +00005268 }
5269
5270 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5271}
5272#endif