blob: d0c31651ec80db695fbd7ca846bc078a5e4f95f3 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6#include <linux/kernel.h>
7#include <linux/bio.h>
8#include <linux/file.h>
9#include <linux/fs.h>
10#include <linux/fsnotify.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/string.h>
15#include <linux/backing-dev.h>
16#include <linux/mount.h>
17#include <linux/namei.h>
18#include <linux/writeback.h>
19#include <linux/compat.h>
20#include <linux/security.h>
21#include <linux/xattr.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
24#include <linux/blkdev.h>
25#include <linux/uuid.h>
26#include <linux/btrfs.h>
27#include <linux/uaccess.h>
28#include <linux/iversion.h>
29#include "ctree.h"
30#include "disk-io.h"
Olivier Deprez157378f2022-04-04 15:47:50 +020031#include "export.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032#include "transaction.h"
33#include "btrfs_inode.h"
34#include "print-tree.h"
35#include "volumes.h"
36#include "locking.h"
37#include "inode-map.h"
38#include "backref.h"
39#include "rcu-string.h"
40#include "send.h"
41#include "dev-replace.h"
42#include "props.h"
43#include "sysfs.h"
44#include "qgroup.h"
45#include "tree-log.h"
46#include "compression.h"
David Brazdil0f672f62019-12-10 10:32:29 +000047#include "space-info.h"
48#include "delalloc-space.h"
49#include "block-group.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050
51#ifdef CONFIG_64BIT
52/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
53 * structures are incorrect, as the timespec structure from userspace
54 * is 4 bytes too small. We define these alternatives here to teach
55 * the kernel about the 32-bit struct packing.
56 */
57struct btrfs_ioctl_timespec_32 {
58 __u64 sec;
59 __u32 nsec;
60} __attribute__ ((__packed__));
61
62struct btrfs_ioctl_received_subvol_args_32 {
63 char uuid[BTRFS_UUID_SIZE]; /* in */
64 __u64 stransid; /* in */
65 __u64 rtransid; /* out */
66 struct btrfs_ioctl_timespec_32 stime; /* in */
67 struct btrfs_ioctl_timespec_32 rtime; /* out */
68 __u64 flags; /* in */
69 __u64 reserved[16]; /* in */
70} __attribute__ ((__packed__));
71
72#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
73 struct btrfs_ioctl_received_subvol_args_32)
74#endif
75
76#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
77struct btrfs_ioctl_send_args_32 {
78 __s64 send_fd; /* in */
79 __u64 clone_sources_count; /* in */
80 compat_uptr_t clone_sources; /* in */
81 __u64 parent_root; /* in */
82 __u64 flags; /* in */
83 __u64 reserved[4]; /* in */
84} __attribute__ ((__packed__));
85
86#define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
87 struct btrfs_ioctl_send_args_32)
88#endif
89
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090/* Mask out flags that are inappropriate for the given type of inode. */
91static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode,
92 unsigned int flags)
93{
94 if (S_ISDIR(inode->i_mode))
95 return flags;
96 else if (S_ISREG(inode->i_mode))
97 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
101
102/*
103 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
104 * ioctl.
105 */
106static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags)
107{
108 unsigned int iflags = 0;
109
110 if (flags & BTRFS_INODE_SYNC)
111 iflags |= FS_SYNC_FL;
112 if (flags & BTRFS_INODE_IMMUTABLE)
113 iflags |= FS_IMMUTABLE_FL;
114 if (flags & BTRFS_INODE_APPEND)
115 iflags |= FS_APPEND_FL;
116 if (flags & BTRFS_INODE_NODUMP)
117 iflags |= FS_NODUMP_FL;
118 if (flags & BTRFS_INODE_NOATIME)
119 iflags |= FS_NOATIME_FL;
120 if (flags & BTRFS_INODE_DIRSYNC)
121 iflags |= FS_DIRSYNC_FL;
122 if (flags & BTRFS_INODE_NODATACOW)
123 iflags |= FS_NOCOW_FL;
124
125 if (flags & BTRFS_INODE_NOCOMPRESS)
126 iflags |= FS_NOCOMP_FL;
127 else if (flags & BTRFS_INODE_COMPRESS)
128 iflags |= FS_COMPR_FL;
129
130 return iflags;
131}
132
133/*
134 * Update inode->i_flags based on the btrfs internal flags.
135 */
136void btrfs_sync_inode_flags_to_i_flags(struct inode *inode)
137{
138 struct btrfs_inode *binode = BTRFS_I(inode);
139 unsigned int new_fl = 0;
140
141 if (binode->flags & BTRFS_INODE_SYNC)
142 new_fl |= S_SYNC;
143 if (binode->flags & BTRFS_INODE_IMMUTABLE)
144 new_fl |= S_IMMUTABLE;
145 if (binode->flags & BTRFS_INODE_APPEND)
146 new_fl |= S_APPEND;
147 if (binode->flags & BTRFS_INODE_NOATIME)
148 new_fl |= S_NOATIME;
149 if (binode->flags & BTRFS_INODE_DIRSYNC)
150 new_fl |= S_DIRSYNC;
151
152 set_mask_bits(&inode->i_flags,
153 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC,
154 new_fl);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
159 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
160 unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
Olivier Deprez0e641232021-09-23 10:07:05 +0200167/*
168 * Check if @flags are a supported and valid set of FS_*_FL flags and that
169 * the old and new flags are not conflicting
170 */
171static int check_fsflags(unsigned int old_flags, unsigned int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172{
173 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
174 FS_NOATIME_FL | FS_NODUMP_FL | \
175 FS_SYNC_FL | FS_DIRSYNC_FL | \
176 FS_NOCOMP_FL | FS_COMPR_FL |
177 FS_NOCOW_FL))
178 return -EOPNOTSUPP;
179
Olivier Deprez0e641232021-09-23 10:07:05 +0200180 /* COMPR and NOCOMP on new/old are valid */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
182 return -EINVAL;
183
Olivier Deprez0e641232021-09-23 10:07:05 +0200184 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
185 return -EINVAL;
186
187 /* NOCOW and compression options are mutually exclusive */
188 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
189 return -EINVAL;
190 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
191 return -EINVAL;
192
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 return 0;
194}
195
196static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
197{
198 struct inode *inode = file_inode(file);
199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
200 struct btrfs_inode *binode = BTRFS_I(inode);
201 struct btrfs_root *root = binode->root;
202 struct btrfs_trans_handle *trans;
203 unsigned int fsflags, old_fsflags;
204 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000205 const char *comp = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200206 u32 binode_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207
208 if (!inode_owner_or_capable(inode))
209 return -EPERM;
210
211 if (btrfs_root_readonly(root))
212 return -EROFS;
213
214 if (copy_from_user(&fsflags, arg, sizeof(fsflags)))
215 return -EFAULT;
216
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217 ret = mnt_want_write_file(file);
218 if (ret)
219 return ret;
220
221 inode_lock(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
223 old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
Olivier Deprez0e641232021-09-23 10:07:05 +0200224
David Brazdil0f672f62019-12-10 10:32:29 +0000225 ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
226 if (ret)
227 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228
Olivier Deprez0e641232021-09-23 10:07:05 +0200229 ret = check_fsflags(old_fsflags, fsflags);
230 if (ret)
231 goto out_unlock;
232
233 binode_flags = binode->flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 if (fsflags & FS_SYNC_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000235 binode_flags |= BTRFS_INODE_SYNC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000236 else
David Brazdil0f672f62019-12-10 10:32:29 +0000237 binode_flags &= ~BTRFS_INODE_SYNC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238 if (fsflags & FS_IMMUTABLE_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000239 binode_flags |= BTRFS_INODE_IMMUTABLE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000240 else
David Brazdil0f672f62019-12-10 10:32:29 +0000241 binode_flags &= ~BTRFS_INODE_IMMUTABLE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000242 if (fsflags & FS_APPEND_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000243 binode_flags |= BTRFS_INODE_APPEND;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244 else
David Brazdil0f672f62019-12-10 10:32:29 +0000245 binode_flags &= ~BTRFS_INODE_APPEND;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246 if (fsflags & FS_NODUMP_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000247 binode_flags |= BTRFS_INODE_NODUMP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000248 else
David Brazdil0f672f62019-12-10 10:32:29 +0000249 binode_flags &= ~BTRFS_INODE_NODUMP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250 if (fsflags & FS_NOATIME_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000251 binode_flags |= BTRFS_INODE_NOATIME;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252 else
David Brazdil0f672f62019-12-10 10:32:29 +0000253 binode_flags &= ~BTRFS_INODE_NOATIME;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 if (fsflags & FS_DIRSYNC_FL)
David Brazdil0f672f62019-12-10 10:32:29 +0000255 binode_flags |= BTRFS_INODE_DIRSYNC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256 else
David Brazdil0f672f62019-12-10 10:32:29 +0000257 binode_flags &= ~BTRFS_INODE_DIRSYNC;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258 if (fsflags & FS_NOCOW_FL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000259 if (S_ISREG(inode->i_mode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000260 /*
261 * It's safe to turn csums off here, no extents exist.
262 * Otherwise we want the flag to reflect the real COW
263 * status of the file and will not set it.
264 */
265 if (inode->i_size == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000266 binode_flags |= BTRFS_INODE_NODATACOW |
267 BTRFS_INODE_NODATASUM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000269 binode_flags |= BTRFS_INODE_NODATACOW;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270 }
271 } else {
272 /*
273 * Revert back under same assumptions as above
274 */
David Brazdil0f672f62019-12-10 10:32:29 +0000275 if (S_ISREG(inode->i_mode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276 if (inode->i_size == 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000277 binode_flags &= ~(BTRFS_INODE_NODATACOW |
278 BTRFS_INODE_NODATASUM);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000280 binode_flags &= ~BTRFS_INODE_NODATACOW;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 }
282 }
283
284 /*
285 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
286 * flag may be changed automatically if compression code won't make
287 * things smaller.
288 */
289 if (fsflags & FS_NOCOMP_FL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000290 binode_flags &= ~BTRFS_INODE_COMPRESS;
291 binode_flags |= BTRFS_INODE_NOCOMPRESS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 } else if (fsflags & FS_COMPR_FL) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293
David Brazdil0f672f62019-12-10 10:32:29 +0000294 if (IS_SWAPFILE(inode)) {
295 ret = -ETXTBSY;
296 goto out_unlock;
297 }
298
299 binode_flags |= BTRFS_INODE_COMPRESS;
300 binode_flags &= ~BTRFS_INODE_NOCOMPRESS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301
302 comp = btrfs_compress_type2str(fs_info->compress_type);
303 if (!comp || comp[0] == 0)
304 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000306 binode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307 }
308
David Brazdil0f672f62019-12-10 10:32:29 +0000309 /*
310 * 1 for inode item
311 * 2 for properties
312 */
313 trans = btrfs_start_transaction(root, 3);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000314 if (IS_ERR(trans)) {
315 ret = PTR_ERR(trans);
David Brazdil0f672f62019-12-10 10:32:29 +0000316 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000317 }
318
David Brazdil0f672f62019-12-10 10:32:29 +0000319 if (comp) {
320 ret = btrfs_set_prop(trans, inode, "btrfs.compression", comp,
321 strlen(comp), 0);
322 if (ret) {
323 btrfs_abort_transaction(trans, ret);
324 goto out_end_trans;
325 }
326 } else {
327 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL,
328 0, 0);
329 if (ret && ret != -ENODATA) {
330 btrfs_abort_transaction(trans, ret);
331 goto out_end_trans;
332 }
333 }
334
335 binode->flags = binode_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336 btrfs_sync_inode_flags_to_i_flags(inode);
337 inode_inc_iversion(inode);
338 inode->i_ctime = current_time(inode);
339 ret = btrfs_update_inode(trans, root, inode);
340
David Brazdil0f672f62019-12-10 10:32:29 +0000341 out_end_trans:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342 btrfs_end_transaction(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343 out_unlock:
344 inode_unlock(inode);
345 mnt_drop_write_file(file);
346 return ret;
347}
348
349/*
350 * Translate btrfs internal inode flags to xflags as expected by the
351 * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are
352 * silently dropped.
353 */
354static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags)
355{
356 unsigned int xflags = 0;
357
358 if (flags & BTRFS_INODE_APPEND)
359 xflags |= FS_XFLAG_APPEND;
360 if (flags & BTRFS_INODE_IMMUTABLE)
361 xflags |= FS_XFLAG_IMMUTABLE;
362 if (flags & BTRFS_INODE_NOATIME)
363 xflags |= FS_XFLAG_NOATIME;
364 if (flags & BTRFS_INODE_NODUMP)
365 xflags |= FS_XFLAG_NODUMP;
366 if (flags & BTRFS_INODE_SYNC)
367 xflags |= FS_XFLAG_SYNC;
368
369 return xflags;
370}
371
372/* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */
373static int check_xflags(unsigned int flags)
374{
375 if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME |
376 FS_XFLAG_NODUMP | FS_XFLAG_SYNC))
377 return -EOPNOTSUPP;
378 return 0;
379}
380
Olivier Deprez157378f2022-04-04 15:47:50 +0200381bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
382 enum btrfs_exclusive_operation type)
383{
384 return !cmpxchg(&fs_info->exclusive_operation, BTRFS_EXCLOP_NONE, type);
385}
386
387void btrfs_exclop_finish(struct btrfs_fs_info *fs_info)
388{
389 WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
390 sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
391}
392
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393/*
394 * Set the xflags from the internal inode flags. The remaining items of fsxattr
395 * are zeroed.
396 */
397static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg)
398{
399 struct btrfs_inode *binode = BTRFS_I(file_inode(file));
400 struct fsxattr fa;
401
David Brazdil0f672f62019-12-10 10:32:29 +0000402 simple_fill_fsxattr(&fa, btrfs_inode_flags_to_xflags(binode->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403 if (copy_to_user(arg, &fa, sizeof(fa)))
404 return -EFAULT;
405
406 return 0;
407}
408
409static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg)
410{
411 struct inode *inode = file_inode(file);
412 struct btrfs_inode *binode = BTRFS_I(inode);
413 struct btrfs_root *root = binode->root;
414 struct btrfs_trans_handle *trans;
David Brazdil0f672f62019-12-10 10:32:29 +0000415 struct fsxattr fa, old_fa;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000416 unsigned old_flags;
417 unsigned old_i_flags;
418 int ret = 0;
419
420 if (!inode_owner_or_capable(inode))
421 return -EPERM;
422
423 if (btrfs_root_readonly(root))
424 return -EROFS;
425
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426 if (copy_from_user(&fa, arg, sizeof(fa)))
427 return -EFAULT;
428
429 ret = check_xflags(fa.fsx_xflags);
430 if (ret)
431 return ret;
432
433 if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0)
434 return -EOPNOTSUPP;
435
436 ret = mnt_want_write_file(file);
437 if (ret)
438 return ret;
439
440 inode_lock(inode);
441
442 old_flags = binode->flags;
443 old_i_flags = inode->i_flags;
444
David Brazdil0f672f62019-12-10 10:32:29 +0000445 simple_fill_fsxattr(&old_fa,
446 btrfs_inode_flags_to_xflags(binode->flags));
447 ret = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa);
448 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450
451 if (fa.fsx_xflags & FS_XFLAG_SYNC)
452 binode->flags |= BTRFS_INODE_SYNC;
453 else
454 binode->flags &= ~BTRFS_INODE_SYNC;
455 if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE)
456 binode->flags |= BTRFS_INODE_IMMUTABLE;
457 else
458 binode->flags &= ~BTRFS_INODE_IMMUTABLE;
459 if (fa.fsx_xflags & FS_XFLAG_APPEND)
460 binode->flags |= BTRFS_INODE_APPEND;
461 else
462 binode->flags &= ~BTRFS_INODE_APPEND;
463 if (fa.fsx_xflags & FS_XFLAG_NODUMP)
464 binode->flags |= BTRFS_INODE_NODUMP;
465 else
466 binode->flags &= ~BTRFS_INODE_NODUMP;
467 if (fa.fsx_xflags & FS_XFLAG_NOATIME)
468 binode->flags |= BTRFS_INODE_NOATIME;
469 else
470 binode->flags &= ~BTRFS_INODE_NOATIME;
471
472 /* 1 item for the inode */
473 trans = btrfs_start_transaction(root, 1);
474 if (IS_ERR(trans)) {
475 ret = PTR_ERR(trans);
476 goto out_unlock;
477 }
478
479 btrfs_sync_inode_flags_to_i_flags(inode);
480 inode_inc_iversion(inode);
481 inode->i_ctime = current_time(inode);
482 ret = btrfs_update_inode(trans, root, inode);
483
484 btrfs_end_transaction(trans);
485
486out_unlock:
487 if (ret) {
488 binode->flags = old_flags;
489 inode->i_flags = old_i_flags;
490 }
491
492 inode_unlock(inode);
493 mnt_drop_write_file(file);
494
495 return ret;
496}
497
498static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
499{
500 struct inode *inode = file_inode(file);
501
502 return put_user(inode->i_generation, arg);
503}
504
Olivier Deprez157378f2022-04-04 15:47:50 +0200505static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
506 void __user *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000507{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508 struct btrfs_device *device;
509 struct request_queue *q;
510 struct fstrim_range range;
511 u64 minlen = ULLONG_MAX;
512 u64 num_devices = 0;
513 int ret;
514
515 if (!capable(CAP_SYS_ADMIN))
516 return -EPERM;
517
David Brazdil0f672f62019-12-10 10:32:29 +0000518 /*
519 * If the fs is mounted with nologreplay, which requires it to be
520 * mounted in RO mode as well, we can not allow discard on free space
521 * inside block groups, because log trees refer to extents that are not
522 * pinned in a block group's free space cache (pinning the extents is
523 * precisely the first phase of replaying a log tree).
524 */
525 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
526 return -EROFS;
527
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528 rcu_read_lock();
529 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
530 dev_list) {
531 if (!device->bdev)
532 continue;
533 q = bdev_get_queue(device->bdev);
534 if (blk_queue_discard(q)) {
535 num_devices++;
536 minlen = min_t(u64, q->limits.discard_granularity,
537 minlen);
538 }
539 }
540 rcu_read_unlock();
541
542 if (!num_devices)
543 return -EOPNOTSUPP;
544 if (copy_from_user(&range, arg, sizeof(range)))
545 return -EFAULT;
546
547 /*
548 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
549 * block group is in the logical address space, which can be any
550 * sectorsize aligned bytenr in the range [0, U64_MAX].
551 */
552 if (range.len < fs_info->sb->s_blocksize)
553 return -EINVAL;
554
555 range.minlen = max(range.minlen, minlen);
556 ret = btrfs_trim_fs(fs_info, &range);
557 if (ret < 0)
558 return ret;
559
560 if (copy_to_user(arg, &range, sizeof(range)))
561 return -EFAULT;
562
563 return 0;
564}
565
Olivier Deprez157378f2022-04-04 15:47:50 +0200566int __pure btrfs_is_empty_uuid(u8 *uuid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567{
568 int i;
569
570 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
571 if (uuid[i])
572 return 0;
573 }
574 return 1;
575}
576
577static noinline int create_subvol(struct inode *dir,
578 struct dentry *dentry,
579 const char *name, int namelen,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580 struct btrfs_qgroup_inherit *inherit)
581{
582 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
583 struct btrfs_trans_handle *trans;
584 struct btrfs_key key;
585 struct btrfs_root_item *root_item;
586 struct btrfs_inode_item *inode_item;
587 struct extent_buffer *leaf;
588 struct btrfs_root *root = BTRFS_I(dir)->root;
589 struct btrfs_root *new_root;
590 struct btrfs_block_rsv block_rsv;
591 struct timespec64 cur_time = current_time(dir);
592 struct inode *inode;
593 int ret;
594 int err;
Olivier Deprez157378f2022-04-04 15:47:50 +0200595 dev_t anon_dev = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000596 u64 objectid;
597 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
598 u64 index = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000599
600 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
601 if (!root_item)
602 return -ENOMEM;
603
604 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
605 if (ret)
606 goto fail_free;
607
Olivier Deprez157378f2022-04-04 15:47:50 +0200608 ret = get_anon_bdev(&anon_dev);
609 if (ret < 0)
610 goto fail_free;
611
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 /*
613 * Don't create subvolume whose level is not zero. Or qgroup will be
614 * screwed up since it assumes subvolume qgroup's level to be 0.
615 */
616 if (btrfs_qgroup_level(objectid)) {
617 ret = -ENOSPC;
618 goto fail_free;
619 }
620
621 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
622 /*
623 * The same as the snapshot creation, please see the comment
624 * of create_snapshot().
625 */
626 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false);
627 if (ret)
628 goto fail_free;
629
630 trans = btrfs_start_transaction(root, 0);
631 if (IS_ERR(trans)) {
632 ret = PTR_ERR(trans);
Olivier Deprez157378f2022-04-04 15:47:50 +0200633 btrfs_subvolume_release_metadata(root, &block_rsv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000634 goto fail_free;
635 }
636 trans->block_rsv = &block_rsv;
637 trans->bytes_reserved = block_rsv.size;
638
639 ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit);
640 if (ret)
641 goto fail;
642
Olivier Deprez157378f2022-04-04 15:47:50 +0200643 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
644 BTRFS_NESTING_NORMAL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 if (IS_ERR(leaf)) {
646 ret = PTR_ERR(leaf);
647 goto fail;
648 }
649
650 btrfs_mark_buffer_dirty(leaf);
651
652 inode_item = &root_item->inode;
653 btrfs_set_stack_inode_generation(inode_item, 1);
654 btrfs_set_stack_inode_size(inode_item, 3);
655 btrfs_set_stack_inode_nlink(inode_item, 1);
656 btrfs_set_stack_inode_nbytes(inode_item,
657 fs_info->nodesize);
658 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
659
660 btrfs_set_root_flags(root_item, 0);
661 btrfs_set_root_limit(root_item, 0);
662 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
663
664 btrfs_set_root_bytenr(root_item, leaf->start);
665 btrfs_set_root_generation(root_item, trans->transid);
666 btrfs_set_root_level(root_item, 0);
667 btrfs_set_root_refs(root_item, 1);
668 btrfs_set_root_used(root_item, leaf->len);
669 btrfs_set_root_last_snapshot(root_item, 0);
670
671 btrfs_set_root_generation_v2(root_item,
672 btrfs_root_generation(root_item));
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 generate_random_guid(root_item->uuid);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000674 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
675 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
676 root_item->ctime = root_item->otime;
677 btrfs_set_root_ctransid(root_item, trans->transid);
678 btrfs_set_root_otransid(root_item, trans->transid);
679
680 btrfs_tree_unlock(leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681
682 btrfs_set_root_dirid(root_item, new_dirid);
683
684 key.objectid = objectid;
685 key.offset = 0;
686 key.type = BTRFS_ROOT_ITEM_KEY;
687 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
688 root_item);
Olivier Deprez0e641232021-09-23 10:07:05 +0200689 if (ret) {
690 /*
691 * Since we don't abort the transaction in this case, free the
692 * tree block so that we don't leak space and leave the
693 * filesystem in an inconsistent state (an extent item in the
694 * extent tree without backreferences). Also no need to have
695 * the tree block locked since it is not in any tree at this
696 * point, so no other task can find it and use it.
697 */
698 btrfs_free_tree_block(trans, root, leaf, 0, 1);
699 free_extent_buffer(leaf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000700 goto fail;
Olivier Deprez0e641232021-09-23 10:07:05 +0200701 }
702
703 free_extent_buffer(leaf);
704 leaf = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705
706 key.offset = (u64)-1;
Olivier Deprez157378f2022-04-04 15:47:50 +0200707 new_root = btrfs_get_new_fs_root(fs_info, objectid, anon_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708 if (IS_ERR(new_root)) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200709 free_anon_bdev(anon_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000710 ret = PTR_ERR(new_root);
711 btrfs_abort_transaction(trans, ret);
712 goto fail;
713 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200714 /* Freeing will be done in btrfs_put_root() of new_root */
715 anon_dev = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716
717 btrfs_record_root_in_trans(trans, new_root);
718
719 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Olivier Deprez157378f2022-04-04 15:47:50 +0200720 btrfs_put_root(new_root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 if (ret) {
722 /* We potentially lose an unused inode item here */
723 btrfs_abort_transaction(trans, ret);
724 goto fail;
725 }
726
727 mutex_lock(&new_root->objectid_mutex);
728 new_root->highest_objectid = new_dirid;
729 mutex_unlock(&new_root->objectid_mutex);
730
731 /*
732 * insert the directory item
733 */
734 ret = btrfs_set_inode_index(BTRFS_I(dir), &index);
735 if (ret) {
736 btrfs_abort_transaction(trans, ret);
737 goto fail;
738 }
739
David Brazdil0f672f62019-12-10 10:32:29 +0000740 ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 BTRFS_FT_DIR, index);
742 if (ret) {
743 btrfs_abort_transaction(trans, ret);
744 goto fail;
745 }
746
747 btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2);
748 ret = btrfs_update_inode(trans, root, dir);
Olivier Deprez0e641232021-09-23 10:07:05 +0200749 if (ret) {
750 btrfs_abort_transaction(trans, ret);
751 goto fail;
752 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000753
754 ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid,
755 btrfs_ino(BTRFS_I(dir)), index, name, namelen);
Olivier Deprez0e641232021-09-23 10:07:05 +0200756 if (ret) {
757 btrfs_abort_transaction(trans, ret);
758 goto fail;
759 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760
761 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
762 BTRFS_UUID_KEY_SUBVOL, objectid);
763 if (ret)
764 btrfs_abort_transaction(trans, ret);
765
766fail:
767 kfree(root_item);
768 trans->block_rsv = NULL;
769 trans->bytes_reserved = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200770 btrfs_subvolume_release_metadata(root, &block_rsv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000771
Olivier Deprez157378f2022-04-04 15:47:50 +0200772 err = btrfs_commit_transaction(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000773 if (err && !ret)
774 ret = err;
775
776 if (!ret) {
777 inode = btrfs_lookup_dentry(dir, dentry);
778 if (IS_ERR(inode))
779 return PTR_ERR(inode);
780 d_instantiate(dentry, inode);
781 }
782 return ret;
783
784fail_free:
Olivier Deprez157378f2022-04-04 15:47:50 +0200785 if (anon_dev)
786 free_anon_bdev(anon_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787 kfree(root_item);
788 return ret;
789}
790
791static int create_snapshot(struct btrfs_root *root, struct inode *dir,
Olivier Deprez157378f2022-04-04 15:47:50 +0200792 struct dentry *dentry, bool readonly,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793 struct btrfs_qgroup_inherit *inherit)
794{
795 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
796 struct inode *inode;
797 struct btrfs_pending_snapshot *pending_snapshot;
798 struct btrfs_trans_handle *trans;
799 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000800
Olivier Deprez157378f2022-04-04 15:47:50 +0200801 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802 return -EINVAL;
803
David Brazdil0f672f62019-12-10 10:32:29 +0000804 if (atomic_read(&root->nr_swapfiles)) {
805 btrfs_warn(fs_info,
806 "cannot snapshot subvolume with active swapfile");
807 return -ETXTBSY;
808 }
809
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL);
811 if (!pending_snapshot)
812 return -ENOMEM;
813
Olivier Deprez157378f2022-04-04 15:47:50 +0200814 ret = get_anon_bdev(&pending_snapshot->anon_dev);
815 if (ret < 0)
816 goto free_pending;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817 pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
818 GFP_KERNEL);
819 pending_snapshot->path = btrfs_alloc_path();
820 if (!pending_snapshot->root_item || !pending_snapshot->path) {
821 ret = -ENOMEM;
822 goto free_pending;
823 }
824
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
826 BTRFS_BLOCK_RSV_TEMP);
827 /*
828 * 1 - parent dir inode
829 * 2 - dir entries
830 * 1 - root item
831 * 2 - root ref/backref
832 * 1 - root of snapshot
833 * 1 - UUID item
834 */
835 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
836 &pending_snapshot->block_rsv, 8,
837 false);
838 if (ret)
Olivier Deprez157378f2022-04-04 15:47:50 +0200839 goto free_pending;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000840
841 pending_snapshot->dentry = dentry;
842 pending_snapshot->root = root;
843 pending_snapshot->readonly = readonly;
844 pending_snapshot->dir = dir;
845 pending_snapshot->inherit = inherit;
846
847 trans = btrfs_start_transaction(root, 0);
848 if (IS_ERR(trans)) {
849 ret = PTR_ERR(trans);
850 goto fail;
851 }
852
853 spin_lock(&fs_info->trans_lock);
854 list_add(&pending_snapshot->list,
855 &trans->transaction->pending_snapshots);
856 spin_unlock(&fs_info->trans_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200857
858 ret = btrfs_commit_transaction(trans);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 if (ret)
860 goto fail;
861
862 ret = pending_snapshot->error;
863 if (ret)
864 goto fail;
865
866 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
867 if (ret)
868 goto fail;
869
870 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
871 if (IS_ERR(inode)) {
872 ret = PTR_ERR(inode);
873 goto fail;
874 }
875
876 d_instantiate(dentry, inode);
877 ret = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200878 pending_snapshot->anon_dev = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000879fail:
Olivier Deprez157378f2022-04-04 15:47:50 +0200880 /* Prevent double freeing of anon_dev */
881 if (ret && pending_snapshot->snap)
882 pending_snapshot->snap->anon_dev = 0;
883 btrfs_put_root(pending_snapshot->snap);
884 btrfs_subvolume_release_metadata(root, &pending_snapshot->block_rsv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885free_pending:
Olivier Deprez157378f2022-04-04 15:47:50 +0200886 if (pending_snapshot->anon_dev)
887 free_anon_bdev(pending_snapshot->anon_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000888 kfree(pending_snapshot->root_item);
889 btrfs_free_path(pending_snapshot->path);
890 kfree(pending_snapshot);
891
892 return ret;
893}
894
895/* copy of may_delete in fs/namei.c()
896 * Check whether we can remove a link victim from directory dir, check
897 * whether the type of victim is right.
898 * 1. We can't do it if dir is read-only (done in permission())
899 * 2. We should have write and exec permissions on dir
900 * 3. We can't remove anything from append-only dir
901 * 4. We can't do anything with immutable dir (done in permission())
902 * 5. If the sticky bit on dir is set we should either
903 * a. be owner of dir, or
904 * b. be owner of victim, or
905 * c. have CAP_FOWNER capability
906 * 6. If the victim is append-only or immutable we can't do anything with
907 * links pointing to it.
908 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
909 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
910 * 9. We can't remove a root or mountpoint.
911 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
912 * nfs_async_unlink().
913 */
914
915static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
916{
917 int error;
918
919 if (d_really_is_negative(victim))
920 return -ENOENT;
921
922 BUG_ON(d_inode(victim->d_parent) != dir);
923 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
924
925 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
926 if (error)
927 return error;
928 if (IS_APPEND(dir))
929 return -EPERM;
930 if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) ||
931 IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim)))
932 return -EPERM;
933 if (isdir) {
934 if (!d_is_dir(victim))
935 return -ENOTDIR;
936 if (IS_ROOT(victim))
937 return -EBUSY;
938 } else if (d_is_dir(victim))
939 return -EISDIR;
940 if (IS_DEADDIR(dir))
941 return -ENOENT;
942 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
943 return -EBUSY;
944 return 0;
945}
946
947/* copy of may_create in fs/namei.c() */
948static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
949{
950 if (d_really_is_positive(child))
951 return -EEXIST;
952 if (IS_DEADDIR(dir))
953 return -ENOENT;
954 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
955}
956
957/*
958 * Create a new subvolume below @parent. This is largely modeled after
959 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
960 * inside this filesystem so it's quite a bit simpler.
961 */
962static noinline int btrfs_mksubvol(const struct path *parent,
963 const char *name, int namelen,
964 struct btrfs_root *snap_src,
Olivier Deprez157378f2022-04-04 15:47:50 +0200965 bool readonly,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966 struct btrfs_qgroup_inherit *inherit)
967{
968 struct inode *dir = d_inode(parent->dentry);
969 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
970 struct dentry *dentry;
971 int error;
972
973 error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
974 if (error == -EINTR)
975 return error;
976
977 dentry = lookup_one_len(name, parent->dentry, namelen);
978 error = PTR_ERR(dentry);
979 if (IS_ERR(dentry))
980 goto out_unlock;
981
982 error = btrfs_may_create(dir, dentry);
983 if (error)
984 goto out_dput;
985
986 /*
987 * even if this name doesn't exist, we may get hash collisions.
988 * check for them now when we can safely fail
989 */
990 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
991 dir->i_ino, name,
992 namelen);
993 if (error)
994 goto out_dput;
995
996 down_read(&fs_info->subvol_sem);
997
998 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
999 goto out_up_read;
1000
Olivier Deprez157378f2022-04-04 15:47:50 +02001001 if (snap_src)
1002 error = create_snapshot(snap_src, dir, dentry, readonly, inherit);
1003 else
1004 error = create_subvol(dir, dentry, name, namelen, inherit);
1005
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001006 if (!error)
1007 fsnotify_mkdir(dir, dentry);
1008out_up_read:
1009 up_read(&fs_info->subvol_sem);
1010out_dput:
1011 dput(dentry);
1012out_unlock:
1013 inode_unlock(dir);
1014 return error;
1015}
1016
Olivier Deprez157378f2022-04-04 15:47:50 +02001017static noinline int btrfs_mksnapshot(const struct path *parent,
1018 const char *name, int namelen,
1019 struct btrfs_root *root,
1020 bool readonly,
1021 struct btrfs_qgroup_inherit *inherit)
1022{
1023 int ret;
1024 bool snapshot_force_cow = false;
1025
1026 /*
1027 * Force new buffered writes to reserve space even when NOCOW is
1028 * possible. This is to avoid later writeback (running dealloc) to
1029 * fallback to COW mode and unexpectedly fail with ENOSPC.
1030 */
1031 btrfs_drew_read_lock(&root->snapshot_lock);
1032
1033 ret = btrfs_start_delalloc_snapshot(root);
1034 if (ret)
1035 goto out;
1036
1037 /*
1038 * All previous writes have started writeback in NOCOW mode, so now
1039 * we force future writes to fallback to COW mode during snapshot
1040 * creation.
1041 */
1042 atomic_inc(&root->snapshot_force_cow);
1043 snapshot_force_cow = true;
1044
1045 btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1);
1046
1047 ret = btrfs_mksubvol(parent, name, namelen,
1048 root, readonly, inherit);
1049out:
1050 if (snapshot_force_cow)
1051 atomic_dec(&root->snapshot_force_cow);
1052 btrfs_drew_read_unlock(&root->snapshot_lock);
1053 return ret;
1054}
1055
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056/*
1057 * When we're defragging a range, we don't want to kick it off again
1058 * if it is really just waiting for delalloc to send it down.
1059 * If we find a nice big extent or delalloc range for the bytes in the
1060 * file you want to defrag, we return 0 to let you know to skip this
1061 * part of the file
1062 */
1063static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh)
1064{
1065 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1066 struct extent_map *em = NULL;
1067 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1068 u64 end;
1069
1070 read_lock(&em_tree->lock);
1071 em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE);
1072 read_unlock(&em_tree->lock);
1073
1074 if (em) {
1075 end = extent_map_end(em);
1076 free_extent_map(em);
1077 if (end - offset > thresh)
1078 return 0;
1079 }
1080 /* if we already have a nice delalloc here, just stop */
1081 thresh /= 2;
1082 end = count_range_bits(io_tree, &offset, offset + thresh,
1083 thresh, EXTENT_DELALLOC, 1);
1084 if (end >= thresh)
1085 return 0;
1086 return 1;
1087}
1088
1089/*
1090 * helper function to walk through a file and find extents
1091 * newer than a specific transid, and smaller than thresh.
1092 *
1093 * This is used by the defragging code to find new and small
1094 * extents
1095 */
1096static int find_new_extents(struct btrfs_root *root,
1097 struct inode *inode, u64 newer_than,
1098 u64 *off, u32 thresh)
1099{
1100 struct btrfs_path *path;
1101 struct btrfs_key min_key;
1102 struct extent_buffer *leaf;
1103 struct btrfs_file_extent_item *extent;
1104 int type;
1105 int ret;
1106 u64 ino = btrfs_ino(BTRFS_I(inode));
1107
1108 path = btrfs_alloc_path();
1109 if (!path)
1110 return -ENOMEM;
1111
1112 min_key.objectid = ino;
1113 min_key.type = BTRFS_EXTENT_DATA_KEY;
1114 min_key.offset = *off;
1115
1116 while (1) {
1117 ret = btrfs_search_forward(root, &min_key, path, newer_than);
1118 if (ret != 0)
1119 goto none;
1120process_slot:
1121 if (min_key.objectid != ino)
1122 goto none;
1123 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
1124 goto none;
1125
1126 leaf = path->nodes[0];
1127 extent = btrfs_item_ptr(leaf, path->slots[0],
1128 struct btrfs_file_extent_item);
1129
1130 type = btrfs_file_extent_type(leaf, extent);
1131 if (type == BTRFS_FILE_EXTENT_REG &&
1132 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
1133 check_defrag_in_cache(inode, min_key.offset, thresh)) {
1134 *off = min_key.offset;
1135 btrfs_free_path(path);
1136 return 0;
1137 }
1138
1139 path->slots[0]++;
1140 if (path->slots[0] < btrfs_header_nritems(leaf)) {
1141 btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]);
1142 goto process_slot;
1143 }
1144
1145 if (min_key.offset == (u64)-1)
1146 goto none;
1147
1148 min_key.offset++;
1149 btrfs_release_path(path);
1150 }
1151none:
1152 btrfs_free_path(path);
1153 return -ENOENT;
1154}
1155
1156static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
1157{
1158 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1159 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1160 struct extent_map *em;
1161 u64 len = PAGE_SIZE;
1162
1163 /*
1164 * hopefully we have this extent in the tree already, try without
1165 * the full extent lock
1166 */
1167 read_lock(&em_tree->lock);
1168 em = lookup_extent_mapping(em_tree, start, len);
1169 read_unlock(&em_tree->lock);
1170
1171 if (!em) {
1172 struct extent_state *cached = NULL;
1173 u64 end = start + len - 1;
1174
1175 /* get the big lock and read metadata off disk */
1176 lock_extent_bits(io_tree, start, end, &cached);
Olivier Deprez157378f2022-04-04 15:47:50 +02001177 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001178 unlock_extent_cached(io_tree, start, end, &cached);
1179
1180 if (IS_ERR(em))
1181 return NULL;
1182 }
1183
1184 return em;
1185}
1186
1187static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
1188{
1189 struct extent_map *next;
1190 bool ret = true;
1191
1192 /* this is the last extent */
1193 if (em->start + em->len >= i_size_read(inode))
1194 return false;
1195
1196 next = defrag_lookup_extent(inode, em->start + em->len);
1197 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
1198 ret = false;
1199 else if ((em->block_start + em->block_len == next->block_start) &&
1200 (em->block_len > SZ_128K && next->block_len > SZ_128K))
1201 ret = false;
1202
1203 free_extent_map(next);
1204 return ret;
1205}
1206
1207static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
1208 u64 *last_len, u64 *skip, u64 *defrag_end,
1209 int compress)
1210{
1211 struct extent_map *em;
1212 int ret = 1;
1213 bool next_mergeable = true;
1214 bool prev_mergeable = true;
1215
1216 /*
1217 * make sure that once we start defragging an extent, we keep on
1218 * defragging it
1219 */
1220 if (start < *defrag_end)
1221 return 1;
1222
1223 *skip = 0;
1224
1225 em = defrag_lookup_extent(inode, start);
1226 if (!em)
1227 return 0;
1228
1229 /* this will cover holes, and inline extents */
1230 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1231 ret = 0;
1232 goto out;
1233 }
1234
1235 if (!*defrag_end)
1236 prev_mergeable = false;
1237
1238 next_mergeable = defrag_check_next_extent(inode, em);
1239 /*
1240 * we hit a real extent, if it is big or the next extent is not a
1241 * real extent, don't bother defragging it
1242 */
1243 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
1244 (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
1245 ret = 0;
1246out:
1247 /*
1248 * last_len ends up being a counter of how many bytes we've defragged.
1249 * every time we choose not to defrag an extent, we reset *last_len
1250 * so that the next tiny extent will force a defrag.
1251 *
1252 * The end result of this is that tiny extents before a single big
1253 * extent will force at least part of that big extent to be defragged.
1254 */
1255 if (ret) {
1256 *defrag_end = extent_map_end(em);
1257 } else {
1258 *last_len = 0;
1259 *skip = extent_map_end(em);
1260 *defrag_end = 0;
1261 }
1262
1263 free_extent_map(em);
1264 return ret;
1265}
1266
1267/*
1268 * it doesn't do much good to defrag one or two pages
1269 * at a time. This pulls in a nice chunk of pages
1270 * to COW and defrag.
1271 *
1272 * It also makes sure the delalloc code has enough
1273 * dirty data to avoid making new small extents as part
1274 * of the defrag
1275 *
1276 * It's a good idea to start RA on this range
1277 * before calling this.
1278 */
1279static int cluster_pages_for_defrag(struct inode *inode,
1280 struct page **pages,
1281 unsigned long start_index,
1282 unsigned long num_pages)
1283{
1284 unsigned long file_end;
1285 u64 isize = i_size_read(inode);
1286 u64 page_start;
1287 u64 page_end;
1288 u64 page_cnt;
Olivier Deprez0e641232021-09-23 10:07:05 +02001289 u64 start = (u64)start_index << PAGE_SHIFT;
1290 u64 search_start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291 int ret;
1292 int i;
1293 int i_done;
1294 struct btrfs_ordered_extent *ordered;
1295 struct extent_state *cached_state = NULL;
1296 struct extent_io_tree *tree;
1297 struct extent_changeset *data_reserved = NULL;
1298 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1299
1300 file_end = (isize - 1) >> PAGE_SHIFT;
1301 if (!isize || start_index > file_end)
1302 return 0;
1303
1304 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
1305
Olivier Deprez157378f2022-04-04 15:47:50 +02001306 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
Olivier Deprez0e641232021-09-23 10:07:05 +02001307 start, page_cnt << PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001308 if (ret)
1309 return ret;
1310 i_done = 0;
1311 tree = &BTRFS_I(inode)->io_tree;
1312
1313 /* step one, lock all the pages */
1314 for (i = 0; i < page_cnt; i++) {
1315 struct page *page;
1316again:
1317 page = find_or_create_page(inode->i_mapping,
1318 start_index + i, mask);
1319 if (!page)
1320 break;
1321
1322 page_start = page_offset(page);
1323 page_end = page_start + PAGE_SIZE - 1;
1324 while (1) {
1325 lock_extent_bits(tree, page_start, page_end,
1326 &cached_state);
Olivier Deprez157378f2022-04-04 15:47:50 +02001327 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001328 page_start);
1329 unlock_extent_cached(tree, page_start, page_end,
1330 &cached_state);
1331 if (!ordered)
1332 break;
1333
1334 unlock_page(page);
Olivier Deprez157378f2022-04-04 15:47:50 +02001335 btrfs_start_ordered_extent(ordered, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001336 btrfs_put_ordered_extent(ordered);
1337 lock_page(page);
1338 /*
1339 * we unlocked the page above, so we need check if
1340 * it was released or not.
1341 */
1342 if (page->mapping != inode->i_mapping) {
1343 unlock_page(page);
1344 put_page(page);
1345 goto again;
1346 }
1347 }
1348
1349 if (!PageUptodate(page)) {
1350 btrfs_readpage(NULL, page);
1351 lock_page(page);
1352 if (!PageUptodate(page)) {
1353 unlock_page(page);
1354 put_page(page);
1355 ret = -EIO;
1356 break;
1357 }
1358 }
1359
1360 if (page->mapping != inode->i_mapping) {
1361 unlock_page(page);
1362 put_page(page);
1363 goto again;
1364 }
1365
1366 pages[i] = page;
1367 i_done++;
1368 }
1369 if (!i_done || ret)
1370 goto out;
1371
1372 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1373 goto out;
1374
1375 /*
1376 * so now we have a nice long stream of locked
1377 * and up to date pages, lets wait on them
1378 */
1379 for (i = 0; i < i_done; i++)
1380 wait_on_page_writeback(pages[i]);
1381
1382 page_start = page_offset(pages[0]);
1383 page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE;
1384
1385 lock_extent_bits(&BTRFS_I(inode)->io_tree,
1386 page_start, page_end - 1, &cached_state);
Olivier Deprez0e641232021-09-23 10:07:05 +02001387
1388 /*
1389 * When defragmenting we skip ranges that have holes or inline extents,
1390 * (check should_defrag_range()), to avoid unnecessary IO and wasting
1391 * space. At btrfs_defrag_file(), we check if a range should be defragged
1392 * before locking the inode and then, if it should, we trigger a sync
1393 * page cache readahead - we lock the inode only after that to avoid
1394 * blocking for too long other tasks that possibly want to operate on
1395 * other file ranges. But before we were able to get the inode lock,
1396 * some other task may have punched a hole in the range, or we may have
1397 * now an inline extent, in which case we should not defrag. So check
1398 * for that here, where we have the inode and the range locked, and bail
1399 * out if that happened.
1400 */
1401 search_start = page_start;
1402 while (search_start < page_end) {
1403 struct extent_map *em;
1404
1405 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, search_start,
Olivier Deprez157378f2022-04-04 15:47:50 +02001406 page_end - search_start);
Olivier Deprez0e641232021-09-23 10:07:05 +02001407 if (IS_ERR(em)) {
1408 ret = PTR_ERR(em);
1409 goto out_unlock_range;
1410 }
1411 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1412 free_extent_map(em);
1413 /* Ok, 0 means we did not defrag anything */
1414 ret = 0;
1415 goto out_unlock_range;
1416 }
1417 search_start = extent_map_end(em);
1418 free_extent_map(em);
1419 }
1420
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
David Brazdil0f672f62019-12-10 10:32:29 +00001422 page_end - 1, EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
1423 EXTENT_DEFRAG, 0, 0, &cached_state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001424
1425 if (i_done != page_cnt) {
1426 spin_lock(&BTRFS_I(inode)->lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001427 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001428 spin_unlock(&BTRFS_I(inode)->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001429 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Olivier Deprez0e641232021-09-23 10:07:05 +02001430 start, (page_cnt - i_done) << PAGE_SHIFT, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001431 }
1432
1433
1434 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1435 &cached_state);
1436
1437 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1438 page_start, page_end - 1, &cached_state);
1439
1440 for (i = 0; i < i_done; i++) {
1441 clear_page_dirty_for_io(pages[i]);
1442 ClearPageChecked(pages[i]);
1443 set_page_extent_mapped(pages[i]);
1444 set_page_dirty(pages[i]);
1445 unlock_page(pages[i]);
1446 put_page(pages[i]);
1447 }
David Brazdil0f672f62019-12-10 10:32:29 +00001448 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 extent_changeset_free(data_reserved);
1450 return i_done;
Olivier Deprez0e641232021-09-23 10:07:05 +02001451
1452out_unlock_range:
1453 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1454 page_start, page_end - 1, &cached_state);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001455out:
1456 for (i = 0; i < i_done; i++) {
1457 unlock_page(pages[i]);
1458 put_page(pages[i]);
1459 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001460 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Olivier Deprez0e641232021-09-23 10:07:05 +02001461 start, page_cnt << PAGE_SHIFT, true);
David Brazdil0f672f62019-12-10 10:32:29 +00001462 btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001463 extent_changeset_free(data_reserved);
1464 return ret;
1465
1466}
1467
1468int btrfs_defrag_file(struct inode *inode, struct file *file,
1469 struct btrfs_ioctl_defrag_range_args *range,
1470 u64 newer_than, unsigned long max_to_defrag)
1471{
1472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1473 struct btrfs_root *root = BTRFS_I(inode)->root;
1474 struct file_ra_state *ra = NULL;
1475 unsigned long last_index;
1476 u64 isize = i_size_read(inode);
1477 u64 last_len = 0;
1478 u64 skip = 0;
1479 u64 defrag_end = 0;
1480 u64 newer_off = range->start;
1481 unsigned long i;
1482 unsigned long ra_index = 0;
1483 int ret;
1484 int defrag_count = 0;
1485 int compress_type = BTRFS_COMPRESS_ZLIB;
1486 u32 extent_thresh = range->extent_thresh;
1487 unsigned long max_cluster = SZ_256K >> PAGE_SHIFT;
1488 unsigned long cluster = max_cluster;
1489 u64 new_align = ~((u64)SZ_128K - 1);
1490 struct page **pages = NULL;
1491 bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS;
1492
1493 if (isize == 0)
1494 return 0;
1495
1496 if (range->start >= isize)
1497 return -EINVAL;
1498
1499 if (do_compress) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001500 if (range->compress_type >= BTRFS_NR_COMPRESS_TYPES)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001501 return -EINVAL;
1502 if (range->compress_type)
1503 compress_type = range->compress_type;
1504 }
1505
1506 if (extent_thresh == 0)
1507 extent_thresh = SZ_256K;
1508
1509 /*
1510 * If we were not given a file, allocate a readahead context. As
1511 * readahead is just an optimization, defrag will work without it so
1512 * we don't error out.
1513 */
1514 if (!file) {
1515 ra = kzalloc(sizeof(*ra), GFP_KERNEL);
1516 if (ra)
1517 file_ra_state_init(ra, inode->i_mapping);
1518 } else {
1519 ra = &file->f_ra;
1520 }
1521
1522 pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL);
1523 if (!pages) {
1524 ret = -ENOMEM;
1525 goto out_ra;
1526 }
1527
1528 /* find the last page to defrag */
1529 if (range->start + range->len > range->start) {
1530 last_index = min_t(u64, isize - 1,
1531 range->start + range->len - 1) >> PAGE_SHIFT;
1532 } else {
1533 last_index = (isize - 1) >> PAGE_SHIFT;
1534 }
1535
1536 if (newer_than) {
1537 ret = find_new_extents(root, inode, newer_than,
1538 &newer_off, SZ_64K);
1539 if (!ret) {
1540 range->start = newer_off;
1541 /*
1542 * we always align our defrag to help keep
1543 * the extents in the file evenly spaced
1544 */
1545 i = (newer_off & new_align) >> PAGE_SHIFT;
1546 } else
1547 goto out_ra;
1548 } else {
1549 i = range->start >> PAGE_SHIFT;
1550 }
1551 if (!max_to_defrag)
1552 max_to_defrag = last_index - i + 1;
1553
1554 /*
1555 * make writeback starts from i, so the defrag range can be
1556 * written sequentially.
1557 */
1558 if (i < inode->i_mapping->writeback_index)
1559 inode->i_mapping->writeback_index = i;
1560
1561 while (i <= last_index && defrag_count < max_to_defrag &&
1562 (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) {
1563 /*
1564 * make sure we stop running if someone unmounts
1565 * the FS
1566 */
1567 if (!(inode->i_sb->s_flags & SB_ACTIVE))
1568 break;
1569
1570 if (btrfs_defrag_cancelled(fs_info)) {
1571 btrfs_debug(fs_info, "defrag_file cancelled");
1572 ret = -EAGAIN;
1573 break;
1574 }
1575
1576 if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT,
1577 extent_thresh, &last_len, &skip,
1578 &defrag_end, do_compress)){
1579 unsigned long next;
1580 /*
1581 * the should_defrag function tells us how much to skip
1582 * bump our counter by the suggested amount
1583 */
1584 next = DIV_ROUND_UP(skip, PAGE_SIZE);
1585 i = max(i + 1, next);
1586 continue;
1587 }
1588
1589 if (!newer_than) {
1590 cluster = (PAGE_ALIGN(defrag_end) >>
1591 PAGE_SHIFT) - i;
1592 cluster = min(cluster, max_cluster);
1593 } else {
1594 cluster = max_cluster;
1595 }
1596
1597 if (i + cluster > ra_index) {
1598 ra_index = max(i, ra_index);
1599 if (ra)
1600 page_cache_sync_readahead(inode->i_mapping, ra,
1601 file, ra_index, cluster);
1602 ra_index += cluster;
1603 }
1604
1605 inode_lock(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001606 if (IS_SWAPFILE(inode)) {
1607 ret = -ETXTBSY;
1608 } else {
1609 if (do_compress)
1610 BTRFS_I(inode)->defrag_compress = compress_type;
1611 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
1612 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001613 if (ret < 0) {
1614 inode_unlock(inode);
1615 goto out_ra;
1616 }
1617
1618 defrag_count += ret;
1619 balance_dirty_pages_ratelimited(inode->i_mapping);
1620 inode_unlock(inode);
1621
1622 if (newer_than) {
1623 if (newer_off == (u64)-1)
1624 break;
1625
1626 if (ret > 0)
1627 i += ret;
1628
1629 newer_off = max(newer_off + 1,
1630 (u64)i << PAGE_SHIFT);
1631
1632 ret = find_new_extents(root, inode, newer_than,
1633 &newer_off, SZ_64K);
1634 if (!ret) {
1635 range->start = newer_off;
1636 i = (newer_off & new_align) >> PAGE_SHIFT;
1637 } else {
1638 break;
1639 }
1640 } else {
1641 if (ret > 0) {
1642 i += ret;
1643 last_len += ret << PAGE_SHIFT;
1644 } else {
1645 i++;
1646 last_len = 0;
1647 }
1648 }
1649 }
1650
1651 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
1652 filemap_flush(inode->i_mapping);
1653 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1654 &BTRFS_I(inode)->runtime_flags))
1655 filemap_flush(inode->i_mapping);
1656 }
1657
1658 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1659 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1660 } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) {
1661 btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD);
1662 }
1663
1664 ret = defrag_count;
1665
1666out_ra:
1667 if (do_compress) {
1668 inode_lock(inode);
1669 BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
1670 inode_unlock(inode);
1671 }
1672 if (!file)
1673 kfree(ra);
1674 kfree(pages);
1675 return ret;
1676}
1677
1678static noinline int btrfs_ioctl_resize(struct file *file,
1679 void __user *arg)
1680{
1681 struct inode *inode = file_inode(file);
1682 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1683 u64 new_size;
1684 u64 old_size;
1685 u64 devid = 1;
1686 struct btrfs_root *root = BTRFS_I(inode)->root;
1687 struct btrfs_ioctl_vol_args *vol_args;
1688 struct btrfs_trans_handle *trans;
1689 struct btrfs_device *device = NULL;
1690 char *sizestr;
1691 char *retptr;
1692 char *devstr = NULL;
1693 int ret = 0;
1694 int mod = 0;
1695
1696 if (!capable(CAP_SYS_ADMIN))
1697 return -EPERM;
1698
1699 ret = mnt_want_write_file(file);
1700 if (ret)
1701 return ret;
1702
Olivier Deprez157378f2022-04-04 15:47:50 +02001703 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_RESIZE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001704 mnt_drop_write_file(file);
1705 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1706 }
1707
1708 vol_args = memdup_user(arg, sizeof(*vol_args));
1709 if (IS_ERR(vol_args)) {
1710 ret = PTR_ERR(vol_args);
1711 goto out;
1712 }
1713
1714 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1715
1716 sizestr = vol_args->name;
1717 devstr = strchr(sizestr, ':');
1718 if (devstr) {
1719 sizestr = devstr + 1;
1720 *devstr = '\0';
1721 devstr = vol_args->name;
1722 ret = kstrtoull(devstr, 10, &devid);
1723 if (ret)
1724 goto out_free;
1725 if (!devid) {
1726 ret = -EINVAL;
1727 goto out_free;
1728 }
1729 btrfs_info(fs_info, "resizing devid %llu", devid);
1730 }
1731
David Brazdil0f672f62019-12-10 10:32:29 +00001732 device = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001733 if (!device) {
1734 btrfs_info(fs_info, "resizer unable to find device %llu",
1735 devid);
1736 ret = -ENODEV;
1737 goto out_free;
1738 }
1739
1740 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1741 btrfs_info(fs_info,
1742 "resizer unable to apply on readonly device %llu",
1743 devid);
1744 ret = -EPERM;
1745 goto out_free;
1746 }
1747
1748 if (!strcmp(sizestr, "max"))
1749 new_size = device->bdev->bd_inode->i_size;
1750 else {
1751 if (sizestr[0] == '-') {
1752 mod = -1;
1753 sizestr++;
1754 } else if (sizestr[0] == '+') {
1755 mod = 1;
1756 sizestr++;
1757 }
1758 new_size = memparse(sizestr, &retptr);
1759 if (*retptr != '\0' || new_size == 0) {
1760 ret = -EINVAL;
1761 goto out_free;
1762 }
1763 }
1764
1765 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1766 ret = -EPERM;
1767 goto out_free;
1768 }
1769
1770 old_size = btrfs_device_get_total_bytes(device);
1771
1772 if (mod < 0) {
1773 if (new_size > old_size) {
1774 ret = -EINVAL;
1775 goto out_free;
1776 }
1777 new_size = old_size - new_size;
1778 } else if (mod > 0) {
1779 if (new_size > ULLONG_MAX - old_size) {
1780 ret = -ERANGE;
1781 goto out_free;
1782 }
1783 new_size = old_size + new_size;
1784 }
1785
1786 if (new_size < SZ_256M) {
1787 ret = -EINVAL;
1788 goto out_free;
1789 }
1790 if (new_size > device->bdev->bd_inode->i_size) {
1791 ret = -EFBIG;
1792 goto out_free;
1793 }
1794
1795 new_size = round_down(new_size, fs_info->sectorsize);
1796
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001797 if (new_size > old_size) {
1798 trans = btrfs_start_transaction(root, 0);
1799 if (IS_ERR(trans)) {
1800 ret = PTR_ERR(trans);
1801 goto out_free;
1802 }
1803 ret = btrfs_grow_device(trans, device, new_size);
1804 btrfs_commit_transaction(trans);
1805 } else if (new_size < old_size) {
1806 ret = btrfs_shrink_device(device, new_size);
1807 } /* equal, nothing need to do */
1808
Olivier Deprez157378f2022-04-04 15:47:50 +02001809 if (ret == 0 && new_size != old_size)
1810 btrfs_info_in_rcu(fs_info,
1811 "resize device %s (devid %llu) from %llu to %llu",
1812 rcu_str_deref(device->name), device->devid,
1813 old_size, new_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001814out_free:
1815 kfree(vol_args);
1816out:
Olivier Deprez157378f2022-04-04 15:47:50 +02001817 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001818 mnt_drop_write_file(file);
1819 return ret;
1820}
1821
Olivier Deprez157378f2022-04-04 15:47:50 +02001822static noinline int __btrfs_ioctl_snap_create(struct file *file,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001823 const char *name, unsigned long fd, int subvol,
Olivier Deprez157378f2022-04-04 15:47:50 +02001824 bool readonly,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001825 struct btrfs_qgroup_inherit *inherit)
1826{
1827 int namelen;
1828 int ret = 0;
1829
1830 if (!S_ISDIR(file_inode(file)->i_mode))
1831 return -ENOTDIR;
1832
1833 ret = mnt_want_write_file(file);
1834 if (ret)
1835 goto out;
1836
1837 namelen = strlen(name);
1838 if (strchr(name, '/')) {
1839 ret = -EINVAL;
1840 goto out_drop_write;
1841 }
1842
1843 if (name[0] == '.' &&
1844 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1845 ret = -EEXIST;
1846 goto out_drop_write;
1847 }
1848
1849 if (subvol) {
1850 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Olivier Deprez157378f2022-04-04 15:47:50 +02001851 NULL, readonly, inherit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001852 } else {
1853 struct fd src = fdget(fd);
1854 struct inode *src_inode;
1855 if (!src.file) {
1856 ret = -EINVAL;
1857 goto out_drop_write;
1858 }
1859
1860 src_inode = file_inode(src.file);
1861 if (src_inode->i_sb != file_inode(file)->i_sb) {
1862 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1863 "Snapshot src from another FS");
1864 ret = -EXDEV;
1865 } else if (!inode_owner_or_capable(src_inode)) {
1866 /*
1867 * Subvolume creation is not restricted, but snapshots
1868 * are limited to own subvolumes only
1869 */
1870 ret = -EPERM;
1871 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02001872 ret = btrfs_mksnapshot(&file->f_path, name, namelen,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001873 BTRFS_I(src_inode)->root,
Olivier Deprez157378f2022-04-04 15:47:50 +02001874 readonly, inherit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001875 }
1876 fdput(src);
1877 }
1878out_drop_write:
1879 mnt_drop_write_file(file);
1880out:
1881 return ret;
1882}
1883
1884static noinline int btrfs_ioctl_snap_create(struct file *file,
1885 void __user *arg, int subvol)
1886{
1887 struct btrfs_ioctl_vol_args *vol_args;
1888 int ret;
1889
1890 if (!S_ISDIR(file_inode(file)->i_mode))
1891 return -ENOTDIR;
1892
1893 vol_args = memdup_user(arg, sizeof(*vol_args));
1894 if (IS_ERR(vol_args))
1895 return PTR_ERR(vol_args);
1896 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1897
Olivier Deprez157378f2022-04-04 15:47:50 +02001898 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1899 subvol, false, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001900
1901 kfree(vol_args);
1902 return ret;
1903}
1904
1905static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1906 void __user *arg, int subvol)
1907{
1908 struct btrfs_ioctl_vol_args_v2 *vol_args;
1909 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001910 bool readonly = false;
1911 struct btrfs_qgroup_inherit *inherit = NULL;
1912
1913 if (!S_ISDIR(file_inode(file)->i_mode))
1914 return -ENOTDIR;
1915
1916 vol_args = memdup_user(arg, sizeof(*vol_args));
1917 if (IS_ERR(vol_args))
1918 return PTR_ERR(vol_args);
1919 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1920
Olivier Deprez157378f2022-04-04 15:47:50 +02001921 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001922 ret = -EOPNOTSUPP;
1923 goto free_args;
1924 }
1925
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001926 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1927 readonly = true;
1928 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001929 u64 nums;
1930
1931 if (vol_args->size < sizeof(*inherit) ||
1932 vol_args->size > PAGE_SIZE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001933 ret = -EINVAL;
1934 goto free_args;
1935 }
1936 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1937 if (IS_ERR(inherit)) {
1938 ret = PTR_ERR(inherit);
1939 goto free_args;
1940 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001941
1942 if (inherit->num_qgroups > PAGE_SIZE ||
1943 inherit->num_ref_copies > PAGE_SIZE ||
1944 inherit->num_excl_copies > PAGE_SIZE) {
1945 ret = -EINVAL;
1946 goto free_inherit;
1947 }
1948
1949 nums = inherit->num_qgroups + 2 * inherit->num_ref_copies +
1950 2 * inherit->num_excl_copies;
1951 if (vol_args->size != struct_size(inherit, qgroups, nums)) {
1952 ret = -EINVAL;
1953 goto free_inherit;
1954 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001955 }
1956
Olivier Deprez157378f2022-04-04 15:47:50 +02001957 ret = __btrfs_ioctl_snap_create(file, vol_args->name, vol_args->fd,
1958 subvol, readonly, inherit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001959 if (ret)
1960 goto free_inherit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001961free_inherit:
1962 kfree(inherit);
1963free_args:
1964 kfree(vol_args);
1965 return ret;
1966}
1967
1968static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1969 void __user *arg)
1970{
1971 struct inode *inode = file_inode(file);
1972 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1973 struct btrfs_root *root = BTRFS_I(inode)->root;
1974 int ret = 0;
1975 u64 flags = 0;
1976
1977 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID)
1978 return -EINVAL;
1979
1980 down_read(&fs_info->subvol_sem);
1981 if (btrfs_root_readonly(root))
1982 flags |= BTRFS_SUBVOL_RDONLY;
1983 up_read(&fs_info->subvol_sem);
1984
1985 if (copy_to_user(arg, &flags, sizeof(flags)))
1986 ret = -EFAULT;
1987
1988 return ret;
1989}
1990
1991static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1992 void __user *arg)
1993{
1994 struct inode *inode = file_inode(file);
1995 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1996 struct btrfs_root *root = BTRFS_I(inode)->root;
1997 struct btrfs_trans_handle *trans;
1998 u64 root_flags;
1999 u64 flags;
2000 int ret = 0;
2001
2002 if (!inode_owner_or_capable(inode))
2003 return -EPERM;
2004
2005 ret = mnt_want_write_file(file);
2006 if (ret)
2007 goto out;
2008
2009 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2010 ret = -EINVAL;
2011 goto out_drop_write;
2012 }
2013
2014 if (copy_from_user(&flags, arg, sizeof(flags))) {
2015 ret = -EFAULT;
2016 goto out_drop_write;
2017 }
2018
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002019 if (flags & ~BTRFS_SUBVOL_RDONLY) {
2020 ret = -EOPNOTSUPP;
2021 goto out_drop_write;
2022 }
2023
2024 down_write(&fs_info->subvol_sem);
2025
2026 /* nothing to do */
2027 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
2028 goto out_drop_sem;
2029
2030 root_flags = btrfs_root_flags(&root->root_item);
2031 if (flags & BTRFS_SUBVOL_RDONLY) {
2032 btrfs_set_root_flags(&root->root_item,
2033 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
2034 } else {
2035 /*
2036 * Block RO -> RW transition if this subvolume is involved in
2037 * send
2038 */
2039 spin_lock(&root->root_item_lock);
2040 if (root->send_in_progress == 0) {
2041 btrfs_set_root_flags(&root->root_item,
2042 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
2043 spin_unlock(&root->root_item_lock);
2044 } else {
2045 spin_unlock(&root->root_item_lock);
2046 btrfs_warn(fs_info,
2047 "Attempt to set subvolume %llu read-write during send",
2048 root->root_key.objectid);
2049 ret = -EPERM;
2050 goto out_drop_sem;
2051 }
2052 }
2053
2054 trans = btrfs_start_transaction(root, 1);
2055 if (IS_ERR(trans)) {
2056 ret = PTR_ERR(trans);
2057 goto out_reset;
2058 }
2059
2060 ret = btrfs_update_root(trans, fs_info->tree_root,
2061 &root->root_key, &root->root_item);
2062 if (ret < 0) {
2063 btrfs_end_transaction(trans);
2064 goto out_reset;
2065 }
2066
2067 ret = btrfs_commit_transaction(trans);
2068
2069out_reset:
2070 if (ret)
2071 btrfs_set_root_flags(&root->root_item, root_flags);
2072out_drop_sem:
2073 up_write(&fs_info->subvol_sem);
2074out_drop_write:
2075 mnt_drop_write_file(file);
2076out:
2077 return ret;
2078}
2079
2080static noinline int key_in_sk(struct btrfs_key *key,
2081 struct btrfs_ioctl_search_key *sk)
2082{
2083 struct btrfs_key test;
2084 int ret;
2085
2086 test.objectid = sk->min_objectid;
2087 test.type = sk->min_type;
2088 test.offset = sk->min_offset;
2089
2090 ret = btrfs_comp_cpu_keys(key, &test);
2091 if (ret < 0)
2092 return 0;
2093
2094 test.objectid = sk->max_objectid;
2095 test.type = sk->max_type;
2096 test.offset = sk->max_offset;
2097
2098 ret = btrfs_comp_cpu_keys(key, &test);
2099 if (ret > 0)
2100 return 0;
2101 return 1;
2102}
2103
2104static noinline int copy_to_sk(struct btrfs_path *path,
2105 struct btrfs_key *key,
2106 struct btrfs_ioctl_search_key *sk,
2107 size_t *buf_size,
2108 char __user *ubuf,
2109 unsigned long *sk_offset,
2110 int *num_found)
2111{
2112 u64 found_transid;
2113 struct extent_buffer *leaf;
2114 struct btrfs_ioctl_search_header sh;
2115 struct btrfs_key test;
2116 unsigned long item_off;
2117 unsigned long item_len;
2118 int nritems;
2119 int i;
2120 int slot;
2121 int ret = 0;
2122
2123 leaf = path->nodes[0];
2124 slot = path->slots[0];
2125 nritems = btrfs_header_nritems(leaf);
2126
2127 if (btrfs_header_generation(leaf) > sk->max_transid) {
2128 i = nritems;
2129 goto advance_key;
2130 }
2131 found_transid = btrfs_header_generation(leaf);
2132
2133 for (i = slot; i < nritems; i++) {
2134 item_off = btrfs_item_ptr_offset(leaf, i);
2135 item_len = btrfs_item_size_nr(leaf, i);
2136
2137 btrfs_item_key_to_cpu(leaf, key, i);
2138 if (!key_in_sk(key, sk))
2139 continue;
2140
2141 if (sizeof(sh) + item_len > *buf_size) {
2142 if (*num_found) {
2143 ret = 1;
2144 goto out;
2145 }
2146
2147 /*
2148 * return one empty item back for v1, which does not
2149 * handle -EOVERFLOW
2150 */
2151
2152 *buf_size = sizeof(sh) + item_len;
2153 item_len = 0;
2154 ret = -EOVERFLOW;
2155 }
2156
2157 if (sizeof(sh) + item_len + *sk_offset > *buf_size) {
2158 ret = 1;
2159 goto out;
2160 }
2161
2162 sh.objectid = key->objectid;
2163 sh.offset = key->offset;
2164 sh.type = key->type;
2165 sh.len = item_len;
2166 sh.transid = found_transid;
2167
Olivier Deprez0e641232021-09-23 10:07:05 +02002168 /*
2169 * Copy search result header. If we fault then loop again so we
2170 * can fault in the pages and -EFAULT there if there's a
2171 * problem. Otherwise we'll fault and then copy the buffer in
2172 * properly this next time through
2173 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002174 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
Olivier Deprez0e641232021-09-23 10:07:05 +02002175 ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176 goto out;
2177 }
2178
2179 *sk_offset += sizeof(sh);
2180
2181 if (item_len) {
2182 char __user *up = ubuf + *sk_offset;
Olivier Deprez0e641232021-09-23 10:07:05 +02002183 /*
2184 * Copy the item, same behavior as above, but reset the
2185 * * sk_offset so we copy the full thing again.
2186 */
2187 if (read_extent_buffer_to_user_nofault(leaf, up,
2188 item_off, item_len)) {
2189 ret = 0;
2190 *sk_offset -= sizeof(sh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002191 goto out;
2192 }
2193
2194 *sk_offset += item_len;
2195 }
2196 (*num_found)++;
2197
2198 if (ret) /* -EOVERFLOW from above */
2199 goto out;
2200
2201 if (*num_found >= sk->nr_items) {
2202 ret = 1;
2203 goto out;
2204 }
2205 }
2206advance_key:
2207 ret = 0;
2208 test.objectid = sk->max_objectid;
2209 test.type = sk->max_type;
2210 test.offset = sk->max_offset;
2211 if (btrfs_comp_cpu_keys(key, &test) >= 0)
2212 ret = 1;
2213 else if (key->offset < (u64)-1)
2214 key->offset++;
2215 else if (key->type < (u8)-1) {
2216 key->offset = 0;
2217 key->type++;
2218 } else if (key->objectid < (u64)-1) {
2219 key->offset = 0;
2220 key->type = 0;
2221 key->objectid++;
2222 } else
2223 ret = 1;
2224out:
2225 /*
2226 * 0: all items from this leaf copied, continue with next
2227 * 1: * more items can be copied, but unused buffer is too small
2228 * * all items were found
2229 * Either way, it will stops the loop which iterates to the next
2230 * leaf
2231 * -EOVERFLOW: item was to large for buffer
2232 * -EFAULT: could not copy extent buffer back to userspace
2233 */
2234 return ret;
2235}
2236
2237static noinline int search_ioctl(struct inode *inode,
2238 struct btrfs_ioctl_search_key *sk,
2239 size_t *buf_size,
2240 char __user *ubuf)
2241{
2242 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2243 struct btrfs_root *root;
2244 struct btrfs_key key;
2245 struct btrfs_path *path;
2246 int ret;
2247 int num_found = 0;
2248 unsigned long sk_offset = 0;
2249
2250 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
2251 *buf_size = sizeof(struct btrfs_ioctl_search_header);
2252 return -EOVERFLOW;
2253 }
2254
2255 path = btrfs_alloc_path();
2256 if (!path)
2257 return -ENOMEM;
2258
2259 if (sk->tree_id == 0) {
2260 /* search the root of the inode that was passed */
Olivier Deprez157378f2022-04-04 15:47:50 +02002261 root = btrfs_grab_root(BTRFS_I(inode)->root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002262 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02002263 root = btrfs_get_fs_root(info, sk->tree_id, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002264 if (IS_ERR(root)) {
2265 btrfs_free_path(path);
2266 return PTR_ERR(root);
2267 }
2268 }
2269
2270 key.objectid = sk->min_objectid;
2271 key.type = sk->min_type;
2272 key.offset = sk->min_offset;
2273
2274 while (1) {
Olivier Deprez0e641232021-09-23 10:07:05 +02002275 ret = fault_in_pages_writeable(ubuf + sk_offset,
2276 *buf_size - sk_offset);
2277 if (ret)
2278 break;
2279
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002280 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
2281 if (ret != 0) {
2282 if (ret > 0)
2283 ret = 0;
2284 goto err;
2285 }
2286 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
2287 &sk_offset, &num_found);
2288 btrfs_release_path(path);
2289 if (ret)
2290 break;
2291
2292 }
2293 if (ret > 0)
2294 ret = 0;
2295err:
2296 sk->nr_items = num_found;
Olivier Deprez157378f2022-04-04 15:47:50 +02002297 btrfs_put_root(root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002298 btrfs_free_path(path);
2299 return ret;
2300}
2301
2302static noinline int btrfs_ioctl_tree_search(struct file *file,
2303 void __user *argp)
2304{
2305 struct btrfs_ioctl_search_args __user *uargs;
2306 struct btrfs_ioctl_search_key sk;
2307 struct inode *inode;
2308 int ret;
2309 size_t buf_size;
2310
2311 if (!capable(CAP_SYS_ADMIN))
2312 return -EPERM;
2313
2314 uargs = (struct btrfs_ioctl_search_args __user *)argp;
2315
2316 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
2317 return -EFAULT;
2318
2319 buf_size = sizeof(uargs->buf);
2320
2321 inode = file_inode(file);
2322 ret = search_ioctl(inode, &sk, &buf_size, uargs->buf);
2323
2324 /*
2325 * In the origin implementation an overflow is handled by returning a
2326 * search header with a len of zero, so reset ret.
2327 */
2328 if (ret == -EOVERFLOW)
2329 ret = 0;
2330
2331 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
2332 ret = -EFAULT;
2333 return ret;
2334}
2335
2336static noinline int btrfs_ioctl_tree_search_v2(struct file *file,
2337 void __user *argp)
2338{
2339 struct btrfs_ioctl_search_args_v2 __user *uarg;
2340 struct btrfs_ioctl_search_args_v2 args;
2341 struct inode *inode;
2342 int ret;
2343 size_t buf_size;
2344 const size_t buf_limit = SZ_16M;
2345
2346 if (!capable(CAP_SYS_ADMIN))
2347 return -EPERM;
2348
2349 /* copy search header and buffer size */
2350 uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp;
2351 if (copy_from_user(&args, uarg, sizeof(args)))
2352 return -EFAULT;
2353
2354 buf_size = args.buf_size;
2355
2356 /* limit result size to 16MB */
2357 if (buf_size > buf_limit)
2358 buf_size = buf_limit;
2359
2360 inode = file_inode(file);
2361 ret = search_ioctl(inode, &args.key, &buf_size,
2362 (char __user *)(&uarg->buf[0]));
2363 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
2364 ret = -EFAULT;
2365 else if (ret == -EOVERFLOW &&
2366 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
2367 ret = -EFAULT;
2368
2369 return ret;
2370}
2371
2372/*
2373 * Search INODE_REFs to identify path name of 'dirid' directory
2374 * in a 'tree_id' tree. and sets path name to 'name'.
2375 */
2376static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2377 u64 tree_id, u64 dirid, char *name)
2378{
2379 struct btrfs_root *root;
2380 struct btrfs_key key;
2381 char *ptr;
2382 int ret = -1;
2383 int slot;
2384 int len;
2385 int total_len = 0;
2386 struct btrfs_inode_ref *iref;
2387 struct extent_buffer *l;
2388 struct btrfs_path *path;
2389
2390 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2391 name[0]='\0';
2392 return 0;
2393 }
2394
2395 path = btrfs_alloc_path();
2396 if (!path)
2397 return -ENOMEM;
2398
2399 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
2400
Olivier Deprez157378f2022-04-04 15:47:50 +02002401 root = btrfs_get_fs_root(info, tree_id, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002402 if (IS_ERR(root)) {
2403 ret = PTR_ERR(root);
Olivier Deprez157378f2022-04-04 15:47:50 +02002404 root = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002405 goto out;
2406 }
2407
2408 key.objectid = dirid;
2409 key.type = BTRFS_INODE_REF_KEY;
2410 key.offset = (u64)-1;
2411
2412 while (1) {
2413 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2414 if (ret < 0)
2415 goto out;
2416 else if (ret > 0) {
2417 ret = btrfs_previous_item(root, path, dirid,
2418 BTRFS_INODE_REF_KEY);
2419 if (ret < 0)
2420 goto out;
2421 else if (ret > 0) {
2422 ret = -ENOENT;
2423 goto out;
2424 }
2425 }
2426
2427 l = path->nodes[0];
2428 slot = path->slots[0];
2429 btrfs_item_key_to_cpu(l, &key, slot);
2430
2431 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2432 len = btrfs_inode_ref_name_len(l, iref);
2433 ptr -= len + 1;
2434 total_len += len + 1;
2435 if (ptr < name) {
2436 ret = -ENAMETOOLONG;
2437 goto out;
2438 }
2439
2440 *(ptr + len) = '/';
2441 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
2442
2443 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2444 break;
2445
2446 btrfs_release_path(path);
2447 key.objectid = key.offset;
2448 key.offset = (u64)-1;
2449 dirid = key.objectid;
2450 }
2451 memmove(name, ptr, total_len);
2452 name[total_len] = '\0';
2453 ret = 0;
2454out:
Olivier Deprez157378f2022-04-04 15:47:50 +02002455 btrfs_put_root(root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002456 btrfs_free_path(path);
2457 return ret;
2458}
2459
2460static int btrfs_search_path_in_tree_user(struct inode *inode,
2461 struct btrfs_ioctl_ino_lookup_user_args *args)
2462{
2463 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2464 struct super_block *sb = inode->i_sb;
2465 struct btrfs_key upper_limit = BTRFS_I(inode)->location;
2466 u64 treeid = BTRFS_I(inode)->root->root_key.objectid;
2467 u64 dirid = args->dirid;
2468 unsigned long item_off;
2469 unsigned long item_len;
2470 struct btrfs_inode_ref *iref;
2471 struct btrfs_root_ref *rref;
Olivier Deprez157378f2022-04-04 15:47:50 +02002472 struct btrfs_root *root = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002473 struct btrfs_path *path;
2474 struct btrfs_key key, key2;
2475 struct extent_buffer *leaf;
2476 struct inode *temp_inode;
2477 char *ptr;
2478 int slot;
2479 int len;
2480 int total_len = 0;
2481 int ret;
2482
2483 path = btrfs_alloc_path();
2484 if (!path)
2485 return -ENOMEM;
2486
2487 /*
2488 * If the bottom subvolume does not exist directly under upper_limit,
2489 * construct the path in from the bottom up.
2490 */
2491 if (dirid != upper_limit.objectid) {
2492 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
2493
Olivier Deprez157378f2022-04-04 15:47:50 +02002494 root = btrfs_get_fs_root(fs_info, treeid, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002495 if (IS_ERR(root)) {
2496 ret = PTR_ERR(root);
2497 goto out;
2498 }
2499
2500 key.objectid = dirid;
2501 key.type = BTRFS_INODE_REF_KEY;
2502 key.offset = (u64)-1;
2503 while (1) {
2504 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2505 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002506 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002507 } else if (ret > 0) {
2508 ret = btrfs_previous_item(root, path, dirid,
2509 BTRFS_INODE_REF_KEY);
2510 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002511 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002512 } else if (ret > 0) {
2513 ret = -ENOENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02002514 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002515 }
2516 }
2517
2518 leaf = path->nodes[0];
2519 slot = path->slots[0];
2520 btrfs_item_key_to_cpu(leaf, &key, slot);
2521
2522 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
2523 len = btrfs_inode_ref_name_len(leaf, iref);
2524 ptr -= len + 1;
2525 total_len += len + 1;
2526 if (ptr < args->path) {
2527 ret = -ENAMETOOLONG;
Olivier Deprez157378f2022-04-04 15:47:50 +02002528 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002529 }
2530
2531 *(ptr + len) = '/';
2532 read_extent_buffer(leaf, ptr,
2533 (unsigned long)(iref + 1), len);
2534
2535 /* Check the read+exec permission of this directory */
2536 ret = btrfs_previous_item(root, path, dirid,
2537 BTRFS_INODE_ITEM_KEY);
2538 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002539 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002540 } else if (ret > 0) {
2541 ret = -ENOENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02002542 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002543 }
2544
2545 leaf = path->nodes[0];
2546 slot = path->slots[0];
2547 btrfs_item_key_to_cpu(leaf, &key2, slot);
2548 if (key2.objectid != dirid) {
2549 ret = -ENOENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02002550 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002551 }
2552
Olivier Deprez157378f2022-04-04 15:47:50 +02002553 temp_inode = btrfs_iget(sb, key2.objectid, root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002554 if (IS_ERR(temp_inode)) {
2555 ret = PTR_ERR(temp_inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02002556 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557 }
2558 ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
2559 iput(temp_inode);
2560 if (ret) {
2561 ret = -EACCES;
Olivier Deprez157378f2022-04-04 15:47:50 +02002562 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002563 }
2564
2565 if (key.offset == upper_limit.objectid)
2566 break;
2567 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
2568 ret = -EACCES;
Olivier Deprez157378f2022-04-04 15:47:50 +02002569 goto out_put;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002570 }
2571
2572 btrfs_release_path(path);
2573 key.objectid = key.offset;
2574 key.offset = (u64)-1;
2575 dirid = key.objectid;
2576 }
2577
2578 memmove(args->path, ptr, total_len);
2579 args->path[total_len] = '\0';
Olivier Deprez157378f2022-04-04 15:47:50 +02002580 btrfs_put_root(root);
2581 root = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002582 btrfs_release_path(path);
2583 }
2584
2585 /* Get the bottom subvolume's name from ROOT_REF */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002586 key.objectid = treeid;
2587 key.type = BTRFS_ROOT_REF_KEY;
2588 key.offset = args->treeid;
Olivier Deprez157378f2022-04-04 15:47:50 +02002589 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002590 if (ret < 0) {
2591 goto out;
2592 } else if (ret > 0) {
2593 ret = -ENOENT;
2594 goto out;
2595 }
2596
2597 leaf = path->nodes[0];
2598 slot = path->slots[0];
2599 btrfs_item_key_to_cpu(leaf, &key, slot);
2600
2601 item_off = btrfs_item_ptr_offset(leaf, slot);
2602 item_len = btrfs_item_size_nr(leaf, slot);
2603 /* Check if dirid in ROOT_REF corresponds to passed dirid */
2604 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2605 if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) {
2606 ret = -EINVAL;
2607 goto out;
2608 }
2609
2610 /* Copy subvolume's name */
2611 item_off += sizeof(struct btrfs_root_ref);
2612 item_len -= sizeof(struct btrfs_root_ref);
2613 read_extent_buffer(leaf, args->name, item_off, item_len);
2614 args->name[item_len] = 0;
2615
Olivier Deprez157378f2022-04-04 15:47:50 +02002616out_put:
2617 btrfs_put_root(root);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002618out:
2619 btrfs_free_path(path);
2620 return ret;
2621}
2622
2623static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2624 void __user *argp)
2625{
2626 struct btrfs_ioctl_ino_lookup_args *args;
2627 struct inode *inode;
2628 int ret = 0;
2629
2630 args = memdup_user(argp, sizeof(*args));
2631 if (IS_ERR(args))
2632 return PTR_ERR(args);
2633
2634 inode = file_inode(file);
2635
2636 /*
2637 * Unprivileged query to obtain the containing subvolume root id. The
2638 * path is reset so it's consistent with btrfs_search_path_in_tree.
2639 */
2640 if (args->treeid == 0)
2641 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2642
2643 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
2644 args->name[0] = 0;
2645 goto out;
2646 }
2647
2648 if (!capable(CAP_SYS_ADMIN)) {
2649 ret = -EPERM;
2650 goto out;
2651 }
2652
2653 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2654 args->treeid, args->objectid,
2655 args->name);
2656
2657out:
2658 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2659 ret = -EFAULT;
2660
2661 kfree(args);
2662 return ret;
2663}
2664
2665/*
2666 * Version of ino_lookup ioctl (unprivileged)
2667 *
2668 * The main differences from ino_lookup ioctl are:
2669 *
2670 * 1. Read + Exec permission will be checked using inode_permission() during
2671 * path construction. -EACCES will be returned in case of failure.
2672 * 2. Path construction will be stopped at the inode number which corresponds
2673 * to the fd with which this ioctl is called. If constructed path does not
2674 * exist under fd's inode, -EACCES will be returned.
2675 * 3. The name of bottom subvolume is also searched and filled.
2676 */
2677static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
2678{
2679 struct btrfs_ioctl_ino_lookup_user_args *args;
2680 struct inode *inode;
2681 int ret;
2682
2683 args = memdup_user(argp, sizeof(*args));
2684 if (IS_ERR(args))
2685 return PTR_ERR(args);
2686
2687 inode = file_inode(file);
2688
2689 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
2690 BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) {
2691 /*
2692 * The subvolume does not exist under fd with which this is
2693 * called
2694 */
2695 kfree(args);
2696 return -EACCES;
2697 }
2698
2699 ret = btrfs_search_path_in_tree_user(inode, args);
2700
2701 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2702 ret = -EFAULT;
2703
2704 kfree(args);
2705 return ret;
2706}
2707
2708/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
2709static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
2710{
2711 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
2712 struct btrfs_fs_info *fs_info;
2713 struct btrfs_root *root;
2714 struct btrfs_path *path;
2715 struct btrfs_key key;
2716 struct btrfs_root_item *root_item;
2717 struct btrfs_root_ref *rref;
2718 struct extent_buffer *leaf;
2719 unsigned long item_off;
2720 unsigned long item_len;
2721 struct inode *inode;
2722 int slot;
2723 int ret = 0;
2724
2725 path = btrfs_alloc_path();
2726 if (!path)
2727 return -ENOMEM;
2728
2729 subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL);
2730 if (!subvol_info) {
2731 btrfs_free_path(path);
2732 return -ENOMEM;
2733 }
2734
2735 inode = file_inode(file);
2736 fs_info = BTRFS_I(inode)->root->fs_info;
2737
2738 /* Get root_item of inode's subvolume */
2739 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
Olivier Deprez157378f2022-04-04 15:47:50 +02002740 root = btrfs_get_fs_root(fs_info, key.objectid, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002741 if (IS_ERR(root)) {
2742 ret = PTR_ERR(root);
Olivier Deprez157378f2022-04-04 15:47:50 +02002743 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002744 }
2745 root_item = &root->root_item;
2746
2747 subvol_info->treeid = key.objectid;
2748
2749 subvol_info->generation = btrfs_root_generation(root_item);
2750 subvol_info->flags = btrfs_root_flags(root_item);
2751
2752 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
2753 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
2754 BTRFS_UUID_SIZE);
2755 memcpy(subvol_info->received_uuid, root_item->received_uuid,
2756 BTRFS_UUID_SIZE);
2757
2758 subvol_info->ctransid = btrfs_root_ctransid(root_item);
2759 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
2760 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
2761
2762 subvol_info->otransid = btrfs_root_otransid(root_item);
2763 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
2764 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
2765
2766 subvol_info->stransid = btrfs_root_stransid(root_item);
2767 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
2768 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
2769
2770 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2771 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2772 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2773
2774 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2775 /* Search root tree for ROOT_BACKREF of this subvolume */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002776 key.type = BTRFS_ROOT_BACKREF_KEY;
2777 key.offset = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002778 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002779 if (ret < 0) {
2780 goto out;
2781 } else if (path->slots[0] >=
2782 btrfs_header_nritems(path->nodes[0])) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002783 ret = btrfs_next_leaf(fs_info->tree_root, path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002784 if (ret < 0) {
2785 goto out;
2786 } else if (ret > 0) {
2787 ret = -EUCLEAN;
2788 goto out;
2789 }
2790 }
2791
2792 leaf = path->nodes[0];
2793 slot = path->slots[0];
2794 btrfs_item_key_to_cpu(leaf, &key, slot);
2795 if (key.objectid == subvol_info->treeid &&
2796 key.type == BTRFS_ROOT_BACKREF_KEY) {
2797 subvol_info->parent_id = key.offset;
2798
2799 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2800 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2801
2802 item_off = btrfs_item_ptr_offset(leaf, slot)
2803 + sizeof(struct btrfs_root_ref);
2804 item_len = btrfs_item_size_nr(leaf, slot)
2805 - sizeof(struct btrfs_root_ref);
2806 read_extent_buffer(leaf, subvol_info->name,
2807 item_off, item_len);
2808 } else {
2809 ret = -ENOENT;
2810 goto out;
2811 }
2812 }
2813
Olivier Deprez92d4c212022-12-06 15:05:30 +01002814 btrfs_free_path(path);
2815 path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002816 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2817 ret = -EFAULT;
2818
2819out:
Olivier Deprez157378f2022-04-04 15:47:50 +02002820 btrfs_put_root(root);
2821out_free:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002822 btrfs_free_path(path);
Olivier Deprez157378f2022-04-04 15:47:50 +02002823 kfree(subvol_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002824 return ret;
2825}
2826
2827/*
2828 * Return ROOT_REF information of the subvolume containing this inode
2829 * except the subvolume name.
2830 */
2831static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp)
2832{
2833 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2834 struct btrfs_root_ref *rref;
2835 struct btrfs_root *root;
2836 struct btrfs_path *path;
2837 struct btrfs_key key;
2838 struct extent_buffer *leaf;
2839 struct inode *inode;
2840 u64 objectid;
2841 int slot;
2842 int ret;
2843 u8 found;
2844
2845 path = btrfs_alloc_path();
2846 if (!path)
2847 return -ENOMEM;
2848
2849 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2850 if (IS_ERR(rootrefs)) {
2851 btrfs_free_path(path);
2852 return PTR_ERR(rootrefs);
2853 }
2854
2855 inode = file_inode(file);
2856 root = BTRFS_I(inode)->root->fs_info->tree_root;
2857 objectid = BTRFS_I(inode)->root->root_key.objectid;
2858
2859 key.objectid = objectid;
2860 key.type = BTRFS_ROOT_REF_KEY;
2861 key.offset = rootrefs->min_treeid;
2862 found = 0;
2863
2864 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2865 if (ret < 0) {
2866 goto out;
2867 } else if (path->slots[0] >=
2868 btrfs_header_nritems(path->nodes[0])) {
2869 ret = btrfs_next_leaf(root, path);
2870 if (ret < 0) {
2871 goto out;
2872 } else if (ret > 0) {
2873 ret = -EUCLEAN;
2874 goto out;
2875 }
2876 }
2877 while (1) {
2878 leaf = path->nodes[0];
2879 slot = path->slots[0];
2880
2881 btrfs_item_key_to_cpu(leaf, &key, slot);
2882 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2883 ret = 0;
2884 goto out;
2885 }
2886
2887 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2888 ret = -EOVERFLOW;
2889 goto out;
2890 }
2891
2892 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2893 rootrefs->rootref[found].treeid = key.offset;
2894 rootrefs->rootref[found].dirid =
2895 btrfs_root_ref_dirid(leaf, rref);
2896 found++;
2897
2898 ret = btrfs_next_item(root, path);
2899 if (ret < 0) {
2900 goto out;
2901 } else if (ret > 0) {
2902 ret = -EUCLEAN;
2903 goto out;
2904 }
2905 }
2906
2907out:
Olivier Deprez92d4c212022-12-06 15:05:30 +01002908 btrfs_free_path(path);
2909
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002910 if (!ret || ret == -EOVERFLOW) {
2911 rootrefs->num_items = found;
2912 /* update min_treeid for next search */
2913 if (found)
2914 rootrefs->min_treeid =
2915 rootrefs->rootref[found - 1].treeid + 1;
2916 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2917 ret = -EFAULT;
2918 }
2919
2920 kfree(rootrefs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921
2922 return ret;
2923}
2924
2925static noinline int btrfs_ioctl_snap_destroy(struct file *file,
Olivier Deprez157378f2022-04-04 15:47:50 +02002926 void __user *arg,
2927 bool destroy_v2)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002928{
2929 struct dentry *parent = file->f_path.dentry;
2930 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2931 struct dentry *dentry;
2932 struct inode *dir = d_inode(parent);
2933 struct inode *inode;
2934 struct btrfs_root *root = BTRFS_I(dir)->root;
2935 struct btrfs_root *dest = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02002936 struct btrfs_ioctl_vol_args *vol_args = NULL;
2937 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2938 char *subvol_name, *subvol_name_ptr = NULL;
2939 int subvol_namelen;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002940 int err = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002941 bool destroy_parent = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002942
Olivier Deprez157378f2022-04-04 15:47:50 +02002943 if (destroy_v2) {
2944 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2945 if (IS_ERR(vol_args2))
2946 return PTR_ERR(vol_args2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002947
Olivier Deprez157378f2022-04-04 15:47:50 +02002948 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2949 err = -EOPNOTSUPP;
2950 goto out;
2951 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002952
Olivier Deprez157378f2022-04-04 15:47:50 +02002953 /*
2954 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2955 * name, same as v1 currently does.
2956 */
2957 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2958 vol_args2->name[BTRFS_SUBVOL_NAME_MAX] = 0;
2959 subvol_name = vol_args2->name;
2960
2961 err = mnt_want_write_file(file);
2962 if (err)
2963 goto out;
2964 } else {
2965 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2966 err = -EINVAL;
2967 goto out;
2968 }
2969
2970 err = mnt_want_write_file(file);
2971 if (err)
2972 goto out;
2973
2974 dentry = btrfs_get_dentry(fs_info->sb,
2975 BTRFS_FIRST_FREE_OBJECTID,
2976 vol_args2->subvolid, 0, 0);
2977 if (IS_ERR(dentry)) {
2978 err = PTR_ERR(dentry);
2979 goto out_drop_write;
2980 }
2981
2982 /*
2983 * Change the default parent since the subvolume being
2984 * deleted can be outside of the current mount point.
2985 */
2986 parent = btrfs_get_parent(dentry);
2987
2988 /*
2989 * At this point dentry->d_name can point to '/' if the
2990 * subvolume we want to destroy is outsite of the
2991 * current mount point, so we need to release the
2992 * current dentry and execute the lookup to return a new
2993 * one with ->d_name pointing to the
2994 * <mount point>/subvol_name.
2995 */
2996 dput(dentry);
2997 if (IS_ERR(parent)) {
2998 err = PTR_ERR(parent);
2999 goto out_drop_write;
3000 }
3001 dir = d_inode(parent);
3002
3003 /*
3004 * If v2 was used with SPEC_BY_ID, a new parent was
3005 * allocated since the subvolume can be outside of the
3006 * current mount point. Later on we need to release this
3007 * new parent dentry.
3008 */
3009 destroy_parent = true;
3010
3011 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
3012 fs_info, vol_args2->subvolid);
3013 if (IS_ERR(subvol_name_ptr)) {
3014 err = PTR_ERR(subvol_name_ptr);
3015 goto free_parent;
3016 }
3017 /* subvol_name_ptr is already NULL termined */
3018 subvol_name = (char *)kbasename(subvol_name_ptr);
3019 }
3020 } else {
3021 vol_args = memdup_user(arg, sizeof(*vol_args));
3022 if (IS_ERR(vol_args))
3023 return PTR_ERR(vol_args);
3024
3025 vol_args->name[BTRFS_PATH_NAME_MAX] = 0;
3026 subvol_name = vol_args->name;
3027
3028 err = mnt_want_write_file(file);
3029 if (err)
3030 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003031 }
3032
Olivier Deprez157378f2022-04-04 15:47:50 +02003033 subvol_namelen = strlen(subvol_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003034
Olivier Deprez157378f2022-04-04 15:47:50 +02003035 if (strchr(subvol_name, '/') ||
3036 strncmp(subvol_name, "..", subvol_namelen) == 0) {
3037 err = -EINVAL;
3038 goto free_subvol_name;
3039 }
3040
3041 if (!S_ISDIR(dir->i_mode)) {
3042 err = -ENOTDIR;
3043 goto free_subvol_name;
3044 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003045
3046 err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT);
3047 if (err == -EINTR)
Olivier Deprez157378f2022-04-04 15:47:50 +02003048 goto free_subvol_name;
3049 dentry = lookup_one_len(subvol_name, parent, subvol_namelen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003050 if (IS_ERR(dentry)) {
3051 err = PTR_ERR(dentry);
3052 goto out_unlock_dir;
3053 }
3054
3055 if (d_really_is_negative(dentry)) {
3056 err = -ENOENT;
3057 goto out_dput;
3058 }
3059
3060 inode = d_inode(dentry);
3061 dest = BTRFS_I(inode)->root;
3062 if (!capable(CAP_SYS_ADMIN)) {
3063 /*
3064 * Regular user. Only allow this with a special mount
3065 * option, when the user has write+exec access to the
3066 * subvol root, and when rmdir(2) would have been
3067 * allowed.
3068 *
3069 * Note that this is _not_ check that the subvol is
3070 * empty or doesn't contain data that we wouldn't
3071 * otherwise be able to delete.
3072 *
3073 * Users who want to delete empty subvols should try
3074 * rmdir(2).
3075 */
3076 err = -EPERM;
3077 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
3078 goto out_dput;
3079
3080 /*
3081 * Do not allow deletion if the parent dir is the same
3082 * as the dir to be deleted. That means the ioctl
3083 * must be called on the dentry referencing the root
3084 * of the subvol, not a random directory contained
3085 * within it.
3086 */
3087 err = -EINVAL;
3088 if (root == dest)
3089 goto out_dput;
3090
3091 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
3092 if (err)
3093 goto out_dput;
3094 }
3095
3096 /* check if subvolume may be deleted by a user */
3097 err = btrfs_may_delete(dir, dentry, 1);
3098 if (err)
3099 goto out_dput;
3100
3101 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3102 err = -EINVAL;
3103 goto out_dput;
3104 }
3105
3106 inode_lock(inode);
3107 err = btrfs_delete_subvolume(dir, dentry);
3108 inode_unlock(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02003109 if (!err)
3110 d_delete_notify(dir, dentry);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003111
3112out_dput:
3113 dput(dentry);
3114out_unlock_dir:
3115 inode_unlock(dir);
Olivier Deprez157378f2022-04-04 15:47:50 +02003116free_subvol_name:
3117 kfree(subvol_name_ptr);
3118free_parent:
3119 if (destroy_parent)
3120 dput(parent);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003121out_drop_write:
3122 mnt_drop_write_file(file);
3123out:
Olivier Deprez157378f2022-04-04 15:47:50 +02003124 kfree(vol_args2);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003125 kfree(vol_args);
3126 return err;
3127}
3128
3129static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
3130{
3131 struct inode *inode = file_inode(file);
3132 struct btrfs_root *root = BTRFS_I(inode)->root;
3133 struct btrfs_ioctl_defrag_range_args *range;
3134 int ret;
3135
3136 ret = mnt_want_write_file(file);
3137 if (ret)
3138 return ret;
3139
3140 if (btrfs_root_readonly(root)) {
3141 ret = -EROFS;
3142 goto out;
3143 }
3144
3145 switch (inode->i_mode & S_IFMT) {
3146 case S_IFDIR:
3147 if (!capable(CAP_SYS_ADMIN)) {
3148 ret = -EPERM;
3149 goto out;
3150 }
3151 ret = btrfs_defrag_root(root);
3152 break;
3153 case S_IFREG:
3154 /*
3155 * Note that this does not check the file descriptor for write
3156 * access. This prevents defragmenting executables that are
3157 * running and allows defrag on files open in read-only mode.
3158 */
3159 if (!capable(CAP_SYS_ADMIN) &&
3160 inode_permission(inode, MAY_WRITE)) {
3161 ret = -EPERM;
3162 goto out;
3163 }
3164
3165 range = kzalloc(sizeof(*range), GFP_KERNEL);
3166 if (!range) {
3167 ret = -ENOMEM;
3168 goto out;
3169 }
3170
3171 if (argp) {
3172 if (copy_from_user(range, argp,
3173 sizeof(*range))) {
3174 ret = -EFAULT;
3175 kfree(range);
3176 goto out;
3177 }
3178 /* compression requires us to start the IO */
3179 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
3180 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
3181 range->extent_thresh = (u32)-1;
3182 }
3183 } else {
3184 /* the rest are all set to zero by kzalloc */
3185 range->len = (u64)-1;
3186 }
3187 ret = btrfs_defrag_file(file_inode(file), file,
3188 range, BTRFS_OLDEST_GENERATION, 0);
3189 if (ret > 0)
3190 ret = 0;
3191 kfree(range);
3192 break;
3193 default:
3194 ret = -EINVAL;
3195 }
3196out:
3197 mnt_drop_write_file(file);
3198 return ret;
3199}
3200
3201static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
3202{
3203 struct btrfs_ioctl_vol_args *vol_args;
3204 int ret;
3205
3206 if (!capable(CAP_SYS_ADMIN))
3207 return -EPERM;
3208
Olivier Deprez157378f2022-04-04 15:47:50 +02003209 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003210 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3211
3212 vol_args = memdup_user(arg, sizeof(*vol_args));
3213 if (IS_ERR(vol_args)) {
3214 ret = PTR_ERR(vol_args);
3215 goto out;
3216 }
3217
3218 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3219 ret = btrfs_init_new_device(fs_info, vol_args->name);
3220
3221 if (!ret)
3222 btrfs_info(fs_info, "disk added %s", vol_args->name);
3223
3224 kfree(vol_args);
3225out:
Olivier Deprez157378f2022-04-04 15:47:50 +02003226 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003227 return ret;
3228}
3229
3230static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
3231{
3232 struct inode *inode = file_inode(file);
3233 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3234 struct btrfs_ioctl_vol_args_v2 *vol_args;
3235 int ret;
3236
3237 if (!capable(CAP_SYS_ADMIN))
3238 return -EPERM;
3239
3240 ret = mnt_want_write_file(file);
3241 if (ret)
3242 return ret;
3243
3244 vol_args = memdup_user(arg, sizeof(*vol_args));
3245 if (IS_ERR(vol_args)) {
3246 ret = PTR_ERR(vol_args);
3247 goto err_drop;
3248 }
3249
Olivier Deprez157378f2022-04-04 15:47:50 +02003250 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003251 ret = -EOPNOTSUPP;
3252 goto out;
3253 }
3254
Olivier Deprez157378f2022-04-04 15:47:50 +02003255 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003256 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3257 goto out;
3258 }
3259
3260 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
3261 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
3262 } else {
3263 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
3264 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3265 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003266 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003267
3268 if (!ret) {
3269 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
3270 btrfs_info(fs_info, "device deleted: id %llu",
3271 vol_args->devid);
3272 else
3273 btrfs_info(fs_info, "device deleted: %s",
3274 vol_args->name);
3275 }
3276out:
3277 kfree(vol_args);
3278err_drop:
3279 mnt_drop_write_file(file);
3280 return ret;
3281}
3282
3283static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
3284{
3285 struct inode *inode = file_inode(file);
3286 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3287 struct btrfs_ioctl_vol_args *vol_args;
3288 int ret;
3289
3290 if (!capable(CAP_SYS_ADMIN))
3291 return -EPERM;
3292
3293 ret = mnt_want_write_file(file);
3294 if (ret)
3295 return ret;
3296
Olivier Deprez157378f2022-04-04 15:47:50 +02003297 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REMOVE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003298 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3299 goto out_drop_write;
3300 }
3301
3302 vol_args = memdup_user(arg, sizeof(*vol_args));
3303 if (IS_ERR(vol_args)) {
3304 ret = PTR_ERR(vol_args);
3305 goto out;
3306 }
3307
3308 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
3309 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
3310
3311 if (!ret)
3312 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
3313 kfree(vol_args);
3314out:
Olivier Deprez157378f2022-04-04 15:47:50 +02003315 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003316out_drop_write:
3317 mnt_drop_write_file(file);
3318
3319 return ret;
3320}
3321
3322static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3323 void __user *arg)
3324{
3325 struct btrfs_ioctl_fs_info_args *fi_args;
3326 struct btrfs_device *device;
3327 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Olivier Deprez157378f2022-04-04 15:47:50 +02003328 u64 flags_in;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003329 int ret = 0;
3330
Olivier Deprez157378f2022-04-04 15:47:50 +02003331 fi_args = memdup_user(arg, sizeof(*fi_args));
3332 if (IS_ERR(fi_args))
3333 return PTR_ERR(fi_args);
3334
3335 flags_in = fi_args->flags;
3336 memset(fi_args, 0, sizeof(*fi_args));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003337
3338 rcu_read_lock();
3339 fi_args->num_devices = fs_devices->num_devices;
3340
3341 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
3342 if (device->devid > fi_args->max_id)
3343 fi_args->max_id = device->devid;
3344 }
3345 rcu_read_unlock();
3346
David Brazdil0f672f62019-12-10 10:32:29 +00003347 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003348 fi_args->nodesize = fs_info->nodesize;
3349 fi_args->sectorsize = fs_info->sectorsize;
3350 fi_args->clone_alignment = fs_info->sectorsize;
3351
Olivier Deprez157378f2022-04-04 15:47:50 +02003352 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
3353 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
3354 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
3355 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
3356 }
3357
3358 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
3359 fi_args->generation = fs_info->generation;
3360 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
3361 }
3362
3363 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
3364 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
3365 sizeof(fi_args->metadata_uuid));
3366 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
3367 }
3368
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003369 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3370 ret = -EFAULT;
3371
3372 kfree(fi_args);
3373 return ret;
3374}
3375
3376static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3377 void __user *arg)
3378{
3379 struct btrfs_ioctl_dev_info_args *di_args;
3380 struct btrfs_device *dev;
3381 int ret = 0;
3382 char *s_uuid = NULL;
3383
3384 di_args = memdup_user(arg, sizeof(*di_args));
3385 if (IS_ERR(di_args))
3386 return PTR_ERR(di_args);
3387
3388 if (!btrfs_is_empty_uuid(di_args->uuid))
3389 s_uuid = di_args->uuid;
3390
3391 rcu_read_lock();
David Brazdil0f672f62019-12-10 10:32:29 +00003392 dev = btrfs_find_device(fs_info->fs_devices, di_args->devid, s_uuid,
3393 NULL, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003394
3395 if (!dev) {
3396 ret = -ENODEV;
3397 goto out;
3398 }
3399
3400 di_args->devid = dev->devid;
3401 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3402 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
3403 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
3404 if (dev->name) {
3405 strncpy(di_args->path, rcu_str_deref(dev->name),
3406 sizeof(di_args->path) - 1);
3407 di_args->path[sizeof(di_args->path) - 1] = 0;
3408 } else {
3409 di_args->path[0] = '\0';
3410 }
3411
3412out:
3413 rcu_read_unlock();
3414 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3415 ret = -EFAULT;
3416
3417 kfree(di_args);
3418 return ret;
3419}
3420
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003421static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3422{
3423 struct inode *inode = file_inode(file);
3424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3425 struct btrfs_root *root = BTRFS_I(inode)->root;
3426 struct btrfs_root *new_root;
3427 struct btrfs_dir_item *di;
3428 struct btrfs_trans_handle *trans;
Olivier Deprez157378f2022-04-04 15:47:50 +02003429 struct btrfs_path *path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003430 struct btrfs_disk_key disk_key;
3431 u64 objectid = 0;
3432 u64 dir_id;
3433 int ret;
3434
3435 if (!capable(CAP_SYS_ADMIN))
3436 return -EPERM;
3437
3438 ret = mnt_want_write_file(file);
3439 if (ret)
3440 return ret;
3441
3442 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3443 ret = -EFAULT;
3444 goto out;
3445 }
3446
3447 if (!objectid)
3448 objectid = BTRFS_FS_TREE_OBJECTID;
3449
Olivier Deprez157378f2022-04-04 15:47:50 +02003450 new_root = btrfs_get_fs_root(fs_info, objectid, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003451 if (IS_ERR(new_root)) {
3452 ret = PTR_ERR(new_root);
3453 goto out;
3454 }
David Brazdil0f672f62019-12-10 10:32:29 +00003455 if (!is_fstree(new_root->root_key.objectid)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003456 ret = -ENOENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02003457 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003458 }
3459
3460 path = btrfs_alloc_path();
3461 if (!path) {
3462 ret = -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +02003463 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003464 }
3465 path->leave_spinning = 1;
3466
3467 trans = btrfs_start_transaction(root, 1);
3468 if (IS_ERR(trans)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003469 ret = PTR_ERR(trans);
Olivier Deprez157378f2022-04-04 15:47:50 +02003470 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003471 }
3472
3473 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3474 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
3475 dir_id, "default", 7, 1);
3476 if (IS_ERR_OR_NULL(di)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003477 btrfs_release_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003478 btrfs_end_transaction(trans);
3479 btrfs_err(fs_info,
3480 "Umm, you don't have the default diritem, this isn't going to work");
3481 ret = -ENOENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02003482 goto out_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003483 }
3484
3485 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3486 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3487 btrfs_mark_buffer_dirty(path->nodes[0]);
Olivier Deprez157378f2022-04-04 15:47:50 +02003488 btrfs_release_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003489
3490 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3491 btrfs_end_transaction(trans);
Olivier Deprez157378f2022-04-04 15:47:50 +02003492out_free:
3493 btrfs_put_root(new_root);
3494 btrfs_free_path(path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003495out:
3496 mnt_drop_write_file(file);
3497 return ret;
3498}
3499
3500static void get_block_group_info(struct list_head *groups_list,
3501 struct btrfs_ioctl_space_info *space)
3502{
Olivier Deprez157378f2022-04-04 15:47:50 +02003503 struct btrfs_block_group *block_group;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003504
3505 space->total_bytes = 0;
3506 space->used_bytes = 0;
3507 space->flags = 0;
3508 list_for_each_entry(block_group, groups_list, list) {
3509 space->flags = block_group->flags;
Olivier Deprez157378f2022-04-04 15:47:50 +02003510 space->total_bytes += block_group->length;
3511 space->used_bytes += block_group->used;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003512 }
3513}
3514
3515static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
3516 void __user *arg)
3517{
3518 struct btrfs_ioctl_space_args space_args;
3519 struct btrfs_ioctl_space_info space;
3520 struct btrfs_ioctl_space_info *dest;
3521 struct btrfs_ioctl_space_info *dest_orig;
3522 struct btrfs_ioctl_space_info __user *user_dest;
3523 struct btrfs_space_info *info;
3524 static const u64 types[] = {
3525 BTRFS_BLOCK_GROUP_DATA,
3526 BTRFS_BLOCK_GROUP_SYSTEM,
3527 BTRFS_BLOCK_GROUP_METADATA,
3528 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
3529 };
3530 int num_types = 4;
3531 int alloc_size;
3532 int ret = 0;
3533 u64 slot_count = 0;
3534 int i, c;
3535
3536 if (copy_from_user(&space_args,
3537 (struct btrfs_ioctl_space_args __user *)arg,
3538 sizeof(space_args)))
3539 return -EFAULT;
3540
3541 for (i = 0; i < num_types; i++) {
3542 struct btrfs_space_info *tmp;
3543
3544 info = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02003545 list_for_each_entry(tmp, &fs_info->space_info, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003546 if (tmp->flags == types[i]) {
3547 info = tmp;
3548 break;
3549 }
3550 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003551
3552 if (!info)
3553 continue;
3554
3555 down_read(&info->groups_sem);
3556 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3557 if (!list_empty(&info->block_groups[c]))
3558 slot_count++;
3559 }
3560 up_read(&info->groups_sem);
3561 }
3562
3563 /*
3564 * Global block reserve, exported as a space_info
3565 */
3566 slot_count++;
3567
3568 /* space_slots == 0 means they are asking for a count */
3569 if (space_args.space_slots == 0) {
3570 space_args.total_spaces = slot_count;
3571 goto out;
3572 }
3573
3574 slot_count = min_t(u64, space_args.space_slots, slot_count);
3575
3576 alloc_size = sizeof(*dest) * slot_count;
3577
3578 /* we generally have at most 6 or so space infos, one for each raid
3579 * level. So, a whole page should be more than enough for everyone
3580 */
3581 if (alloc_size > PAGE_SIZE)
3582 return -ENOMEM;
3583
3584 space_args.total_spaces = 0;
3585 dest = kmalloc(alloc_size, GFP_KERNEL);
3586 if (!dest)
3587 return -ENOMEM;
3588 dest_orig = dest;
3589
3590 /* now we have a buffer to copy into */
3591 for (i = 0; i < num_types; i++) {
3592 struct btrfs_space_info *tmp;
3593
3594 if (!slot_count)
3595 break;
3596
3597 info = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02003598 list_for_each_entry(tmp, &fs_info->space_info, list) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003599 if (tmp->flags == types[i]) {
3600 info = tmp;
3601 break;
3602 }
3603 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003604
3605 if (!info)
3606 continue;
3607 down_read(&info->groups_sem);
3608 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3609 if (!list_empty(&info->block_groups[c])) {
3610 get_block_group_info(&info->block_groups[c],
3611 &space);
3612 memcpy(dest, &space, sizeof(space));
3613 dest++;
3614 space_args.total_spaces++;
3615 slot_count--;
3616 }
3617 if (!slot_count)
3618 break;
3619 }
3620 up_read(&info->groups_sem);
3621 }
3622
3623 /*
3624 * Add global block reserve
3625 */
3626 if (slot_count) {
3627 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3628
3629 spin_lock(&block_rsv->lock);
3630 space.total_bytes = block_rsv->size;
3631 space.used_bytes = block_rsv->size - block_rsv->reserved;
3632 spin_unlock(&block_rsv->lock);
3633 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
3634 memcpy(dest, &space, sizeof(space));
3635 space_args.total_spaces++;
3636 }
3637
3638 user_dest = (struct btrfs_ioctl_space_info __user *)
3639 (arg + sizeof(struct btrfs_ioctl_space_args));
3640
3641 if (copy_to_user(user_dest, dest_orig, alloc_size))
3642 ret = -EFAULT;
3643
3644 kfree(dest_orig);
3645out:
3646 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
3647 ret = -EFAULT;
3648
3649 return ret;
3650}
3651
3652static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3653 void __user *argp)
3654{
3655 struct btrfs_trans_handle *trans;
3656 u64 transid;
3657 int ret;
3658
3659 trans = btrfs_attach_transaction_barrier(root);
3660 if (IS_ERR(trans)) {
3661 if (PTR_ERR(trans) != -ENOENT)
3662 return PTR_ERR(trans);
3663
3664 /* No running transaction, don't bother */
3665 transid = root->fs_info->last_trans_committed;
3666 goto out;
3667 }
3668 transid = trans->transid;
3669 ret = btrfs_commit_transaction_async(trans, 0);
3670 if (ret) {
3671 btrfs_end_transaction(trans);
3672 return ret;
3673 }
3674out:
3675 if (argp)
3676 if (copy_to_user(argp, &transid, sizeof(transid)))
3677 return -EFAULT;
3678 return 0;
3679}
3680
3681static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
3682 void __user *argp)
3683{
3684 u64 transid;
3685
3686 if (argp) {
3687 if (copy_from_user(&transid, argp, sizeof(transid)))
3688 return -EFAULT;
3689 } else {
3690 transid = 0; /* current trans */
3691 }
3692 return btrfs_wait_for_commit(fs_info, transid);
3693}
3694
3695static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3696{
3697 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
3698 struct btrfs_ioctl_scrub_args *sa;
3699 int ret;
3700
3701 if (!capable(CAP_SYS_ADMIN))
3702 return -EPERM;
3703
3704 sa = memdup_user(arg, sizeof(*sa));
3705 if (IS_ERR(sa))
3706 return PTR_ERR(sa);
3707
3708 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3709 ret = mnt_want_write_file(file);
3710 if (ret)
3711 goto out;
3712 }
3713
3714 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3715 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3716 0);
3717
Olivier Deprez0e641232021-09-23 10:07:05 +02003718 /*
3719 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3720 * error. This is important as it allows user space to know how much
3721 * progress scrub has done. For example, if scrub is canceled we get
3722 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3723 * space. Later user space can inspect the progress from the structure
3724 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3725 * previously (btrfs-progs does this).
3726 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3727 * then return -EFAULT to signal the structure was not copied or it may
3728 * be corrupt and unreliable due to a partial copy.
3729 */
3730 if (copy_to_user(arg, sa, sizeof(*sa)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003731 ret = -EFAULT;
3732
3733 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3734 mnt_drop_write_file(file);
3735out:
3736 kfree(sa);
3737 return ret;
3738}
3739
3740static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3741{
3742 if (!capable(CAP_SYS_ADMIN))
3743 return -EPERM;
3744
3745 return btrfs_scrub_cancel(fs_info);
3746}
3747
3748static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3749 void __user *arg)
3750{
3751 struct btrfs_ioctl_scrub_args *sa;
3752 int ret;
3753
3754 if (!capable(CAP_SYS_ADMIN))
3755 return -EPERM;
3756
3757 sa = memdup_user(arg, sizeof(*sa));
3758 if (IS_ERR(sa))
3759 return PTR_ERR(sa);
3760
3761 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3762
David Brazdil0f672f62019-12-10 10:32:29 +00003763 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003764 ret = -EFAULT;
3765
3766 kfree(sa);
3767 return ret;
3768}
3769
3770static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3771 void __user *arg)
3772{
3773 struct btrfs_ioctl_get_dev_stats *sa;
3774 int ret;
3775
3776 sa = memdup_user(arg, sizeof(*sa));
3777 if (IS_ERR(sa))
3778 return PTR_ERR(sa);
3779
3780 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3781 kfree(sa);
3782 return -EPERM;
3783 }
3784
3785 ret = btrfs_get_dev_stats(fs_info, sa);
3786
David Brazdil0f672f62019-12-10 10:32:29 +00003787 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003788 ret = -EFAULT;
3789
3790 kfree(sa);
3791 return ret;
3792}
3793
3794static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3795 void __user *arg)
3796{
3797 struct btrfs_ioctl_dev_replace_args *p;
3798 int ret;
3799
3800 if (!capable(CAP_SYS_ADMIN))
3801 return -EPERM;
3802
3803 p = memdup_user(arg, sizeof(*p));
3804 if (IS_ERR(p))
3805 return PTR_ERR(p);
3806
3807 switch (p->cmd) {
3808 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3809 if (sb_rdonly(fs_info->sb)) {
3810 ret = -EROFS;
3811 goto out;
3812 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003813 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003814 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3815 } else {
3816 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
Olivier Deprez157378f2022-04-04 15:47:50 +02003817 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003818 }
3819 break;
3820 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3821 btrfs_dev_replace_status(fs_info, p);
3822 ret = 0;
3823 break;
3824 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3825 p->result = btrfs_dev_replace_cancel(fs_info);
3826 ret = 0;
3827 break;
3828 default:
3829 ret = -EINVAL;
3830 break;
3831 }
3832
David Brazdil0f672f62019-12-10 10:32:29 +00003833 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003834 ret = -EFAULT;
3835out:
3836 kfree(p);
3837 return ret;
3838}
3839
3840static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3841{
3842 int ret = 0;
3843 int i;
3844 u64 rel_ptr;
3845 int size;
3846 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3847 struct inode_fs_paths *ipath = NULL;
3848 struct btrfs_path *path;
3849
3850 if (!capable(CAP_DAC_READ_SEARCH))
3851 return -EPERM;
3852
3853 path = btrfs_alloc_path();
3854 if (!path) {
3855 ret = -ENOMEM;
3856 goto out;
3857 }
3858
3859 ipa = memdup_user(arg, sizeof(*ipa));
3860 if (IS_ERR(ipa)) {
3861 ret = PTR_ERR(ipa);
3862 ipa = NULL;
3863 goto out;
3864 }
3865
3866 size = min_t(u32, ipa->size, 4096);
3867 ipath = init_ipath(size, root, path);
3868 if (IS_ERR(ipath)) {
3869 ret = PTR_ERR(ipath);
3870 ipath = NULL;
3871 goto out;
3872 }
3873
3874 ret = paths_from_inode(ipa->inum, ipath);
3875 if (ret < 0)
3876 goto out;
3877
3878 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3879 rel_ptr = ipath->fspath->val[i] -
3880 (u64)(unsigned long)ipath->fspath->val;
3881 ipath->fspath->val[i] = rel_ptr;
3882 }
3883
Olivier Deprez92d4c212022-12-06 15:05:30 +01003884 btrfs_free_path(path);
3885 path = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003886 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3887 ipath->fspath, size);
3888 if (ret) {
3889 ret = -EFAULT;
3890 goto out;
3891 }
3892
3893out:
3894 btrfs_free_path(path);
3895 free_ipath(ipath);
3896 kfree(ipa);
3897
3898 return ret;
3899}
3900
3901static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3902{
3903 struct btrfs_data_container *inodes = ctx;
3904 const size_t c = 3 * sizeof(u64);
3905
3906 if (inodes->bytes_left >= c) {
3907 inodes->bytes_left -= c;
3908 inodes->val[inodes->elem_cnt] = inum;
3909 inodes->val[inodes->elem_cnt + 1] = offset;
3910 inodes->val[inodes->elem_cnt + 2] = root;
3911 inodes->elem_cnt += 3;
3912 } else {
3913 inodes->bytes_missing += c - inodes->bytes_left;
3914 inodes->bytes_left = 0;
3915 inodes->elem_missed += 3;
3916 }
3917
3918 return 0;
3919}
3920
3921static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3922 void __user *arg, int version)
3923{
3924 int ret = 0;
3925 int size;
3926 struct btrfs_ioctl_logical_ino_args *loi;
3927 struct btrfs_data_container *inodes = NULL;
3928 struct btrfs_path *path = NULL;
3929 bool ignore_offset;
3930
3931 if (!capable(CAP_SYS_ADMIN))
3932 return -EPERM;
3933
3934 loi = memdup_user(arg, sizeof(*loi));
3935 if (IS_ERR(loi))
3936 return PTR_ERR(loi);
3937
3938 if (version == 1) {
3939 ignore_offset = false;
3940 size = min_t(u32, loi->size, SZ_64K);
3941 } else {
3942 /* All reserved bits must be 0 for now */
3943 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3944 ret = -EINVAL;
3945 goto out_loi;
3946 }
3947 /* Only accept flags we have defined so far */
3948 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3949 ret = -EINVAL;
3950 goto out_loi;
3951 }
3952 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3953 size = min_t(u32, loi->size, SZ_16M);
3954 }
3955
3956 path = btrfs_alloc_path();
3957 if (!path) {
3958 ret = -ENOMEM;
3959 goto out;
3960 }
3961
3962 inodes = init_data_container(size);
3963 if (IS_ERR(inodes)) {
3964 ret = PTR_ERR(inodes);
3965 inodes = NULL;
3966 goto out;
3967 }
3968
3969 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
3970 build_ino_list, inodes, ignore_offset);
3971 if (ret == -EINVAL)
3972 ret = -ENOENT;
3973 if (ret < 0)
3974 goto out;
3975
3976 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3977 size);
3978 if (ret)
3979 ret = -EFAULT;
3980
3981out:
3982 btrfs_free_path(path);
3983 kvfree(inodes);
3984out_loi:
3985 kfree(loi);
3986
3987 return ret;
3988}
3989
3990void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3991 struct btrfs_ioctl_balance_args *bargs)
3992{
3993 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3994
3995 bargs->flags = bctl->flags;
3996
3997 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3998 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3999 if (atomic_read(&fs_info->balance_pause_req))
4000 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
4001 if (atomic_read(&fs_info->balance_cancel_req))
4002 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
4003
4004 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4005 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4006 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
4007
4008 spin_lock(&fs_info->balance_lock);
4009 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4010 spin_unlock(&fs_info->balance_lock);
4011}
4012
4013static long btrfs_ioctl_balance(struct file *file, void __user *arg)
4014{
4015 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4016 struct btrfs_fs_info *fs_info = root->fs_info;
4017 struct btrfs_ioctl_balance_args *bargs;
4018 struct btrfs_balance_control *bctl;
4019 bool need_unlock; /* for mut. excl. ops lock */
4020 int ret;
4021
4022 if (!capable(CAP_SYS_ADMIN))
4023 return -EPERM;
4024
4025 ret = mnt_want_write_file(file);
4026 if (ret)
4027 return ret;
4028
4029again:
Olivier Deprez157378f2022-04-04 15:47:50 +02004030 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004031 mutex_lock(&fs_info->balance_mutex);
4032 need_unlock = true;
4033 goto locked;
4034 }
4035
4036 /*
4037 * mut. excl. ops lock is locked. Three possibilities:
4038 * (1) some other op is running
4039 * (2) balance is running
4040 * (3) balance is paused -- special case (think resume)
4041 */
4042 mutex_lock(&fs_info->balance_mutex);
4043 if (fs_info->balance_ctl) {
4044 /* this is either (2) or (3) */
4045 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4046 mutex_unlock(&fs_info->balance_mutex);
4047 /*
4048 * Lock released to allow other waiters to continue,
4049 * we'll reexamine the status again.
4050 */
4051 mutex_lock(&fs_info->balance_mutex);
4052
4053 if (fs_info->balance_ctl &&
4054 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
4055 /* this is (3) */
4056 need_unlock = false;
4057 goto locked;
4058 }
4059
4060 mutex_unlock(&fs_info->balance_mutex);
4061 goto again;
4062 } else {
4063 /* this is (2) */
4064 mutex_unlock(&fs_info->balance_mutex);
4065 ret = -EINPROGRESS;
4066 goto out;
4067 }
4068 } else {
4069 /* this is (1) */
4070 mutex_unlock(&fs_info->balance_mutex);
4071 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4072 goto out;
4073 }
4074
4075locked:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004076
4077 if (arg) {
4078 bargs = memdup_user(arg, sizeof(*bargs));
4079 if (IS_ERR(bargs)) {
4080 ret = PTR_ERR(bargs);
4081 goto out_unlock;
4082 }
4083
4084 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4085 if (!fs_info->balance_ctl) {
4086 ret = -ENOTCONN;
4087 goto out_bargs;
4088 }
4089
4090 bctl = fs_info->balance_ctl;
4091 spin_lock(&fs_info->balance_lock);
4092 bctl->flags |= BTRFS_BALANCE_RESUME;
4093 spin_unlock(&fs_info->balance_lock);
4094
4095 goto do_balance;
4096 }
4097 } else {
4098 bargs = NULL;
4099 }
4100
4101 if (fs_info->balance_ctl) {
4102 ret = -EINPROGRESS;
4103 goto out_bargs;
4104 }
4105
4106 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
4107 if (!bctl) {
4108 ret = -ENOMEM;
4109 goto out_bargs;
4110 }
4111
4112 if (arg) {
4113 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4114 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4115 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4116
4117 bctl->flags = bargs->flags;
4118 } else {
4119 /* balance everything - no filters */
4120 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
4121 }
4122
4123 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
4124 ret = -EINVAL;
4125 goto out_bctl;
4126 }
4127
4128do_balance:
4129 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02004130 * Ownership of bctl and exclusive operation goes to btrfs_balance.
4131 * bctl is freed in reset_balance_state, or, if restriper was paused
4132 * all the way until unmount, in free_fs_info. The flag should be
4133 * cleared after reset_balance_state.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004134 */
4135 need_unlock = false;
4136
4137 ret = btrfs_balance(fs_info, bctl, bargs);
4138 bctl = NULL;
4139
David Brazdil0f672f62019-12-10 10:32:29 +00004140 if ((ret == 0 || ret == -ECANCELED) && arg) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004141 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4142 ret = -EFAULT;
4143 }
4144
4145out_bctl:
4146 kfree(bctl);
4147out_bargs:
4148 kfree(bargs);
4149out_unlock:
4150 mutex_unlock(&fs_info->balance_mutex);
4151 if (need_unlock)
Olivier Deprez157378f2022-04-04 15:47:50 +02004152 btrfs_exclop_finish(fs_info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004153out:
4154 mnt_drop_write_file(file);
4155 return ret;
4156}
4157
4158static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4159{
4160 if (!capable(CAP_SYS_ADMIN))
4161 return -EPERM;
4162
4163 switch (cmd) {
4164 case BTRFS_BALANCE_CTL_PAUSE:
4165 return btrfs_pause_balance(fs_info);
4166 case BTRFS_BALANCE_CTL_CANCEL:
4167 return btrfs_cancel_balance(fs_info);
4168 }
4169
4170 return -EINVAL;
4171}
4172
4173static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4174 void __user *arg)
4175{
4176 struct btrfs_ioctl_balance_args *bargs;
4177 int ret = 0;
4178
4179 if (!capable(CAP_SYS_ADMIN))
4180 return -EPERM;
4181
4182 mutex_lock(&fs_info->balance_mutex);
4183 if (!fs_info->balance_ctl) {
4184 ret = -ENOTCONN;
4185 goto out;
4186 }
4187
4188 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
4189 if (!bargs) {
4190 ret = -ENOMEM;
4191 goto out;
4192 }
4193
4194 btrfs_update_ioctl_balance_args(fs_info, bargs);
4195
4196 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4197 ret = -EFAULT;
4198
4199 kfree(bargs);
4200out:
4201 mutex_unlock(&fs_info->balance_mutex);
4202 return ret;
4203}
4204
4205static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4206{
4207 struct inode *inode = file_inode(file);
4208 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4209 struct btrfs_ioctl_quota_ctl_args *sa;
4210 int ret;
4211
4212 if (!capable(CAP_SYS_ADMIN))
4213 return -EPERM;
4214
4215 ret = mnt_want_write_file(file);
4216 if (ret)
4217 return ret;
4218
4219 sa = memdup_user(arg, sizeof(*sa));
4220 if (IS_ERR(sa)) {
4221 ret = PTR_ERR(sa);
4222 goto drop_write;
4223 }
4224
4225 down_write(&fs_info->subvol_sem);
4226
4227 switch (sa->cmd) {
4228 case BTRFS_QUOTA_CTL_ENABLE:
4229 ret = btrfs_quota_enable(fs_info);
4230 break;
4231 case BTRFS_QUOTA_CTL_DISABLE:
4232 ret = btrfs_quota_disable(fs_info);
4233 break;
4234 default:
4235 ret = -EINVAL;
4236 break;
4237 }
4238
4239 kfree(sa);
4240 up_write(&fs_info->subvol_sem);
4241drop_write:
4242 mnt_drop_write_file(file);
4243 return ret;
4244}
4245
4246static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4247{
4248 struct inode *inode = file_inode(file);
4249 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4250 struct btrfs_root *root = BTRFS_I(inode)->root;
4251 struct btrfs_ioctl_qgroup_assign_args *sa;
4252 struct btrfs_trans_handle *trans;
4253 int ret;
4254 int err;
4255
4256 if (!capable(CAP_SYS_ADMIN))
4257 return -EPERM;
4258
4259 ret = mnt_want_write_file(file);
4260 if (ret)
4261 return ret;
4262
4263 sa = memdup_user(arg, sizeof(*sa));
4264 if (IS_ERR(sa)) {
4265 ret = PTR_ERR(sa);
4266 goto drop_write;
4267 }
4268
4269 trans = btrfs_join_transaction(root);
4270 if (IS_ERR(trans)) {
4271 ret = PTR_ERR(trans);
4272 goto out;
4273 }
4274
4275 if (sa->assign) {
4276 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
4277 } else {
4278 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
4279 }
4280
4281 /* update qgroup status and info */
4282 err = btrfs_run_qgroups(trans);
4283 if (err < 0)
4284 btrfs_handle_fs_error(fs_info, err,
4285 "failed to update qgroup status and info");
4286 err = btrfs_end_transaction(trans);
4287 if (err && !ret)
4288 ret = err;
4289
4290out:
4291 kfree(sa);
4292drop_write:
4293 mnt_drop_write_file(file);
4294 return ret;
4295}
4296
4297static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4298{
4299 struct inode *inode = file_inode(file);
4300 struct btrfs_root *root = BTRFS_I(inode)->root;
4301 struct btrfs_ioctl_qgroup_create_args *sa;
4302 struct btrfs_trans_handle *trans;
4303 int ret;
4304 int err;
4305
4306 if (!capable(CAP_SYS_ADMIN))
4307 return -EPERM;
4308
4309 ret = mnt_want_write_file(file);
4310 if (ret)
4311 return ret;
4312
4313 sa = memdup_user(arg, sizeof(*sa));
4314 if (IS_ERR(sa)) {
4315 ret = PTR_ERR(sa);
4316 goto drop_write;
4317 }
4318
4319 if (!sa->qgroupid) {
4320 ret = -EINVAL;
4321 goto out;
4322 }
4323
4324 trans = btrfs_join_transaction(root);
4325 if (IS_ERR(trans)) {
4326 ret = PTR_ERR(trans);
4327 goto out;
4328 }
4329
4330 if (sa->create) {
4331 ret = btrfs_create_qgroup(trans, sa->qgroupid);
4332 } else {
4333 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
4334 }
4335
4336 err = btrfs_end_transaction(trans);
4337 if (err && !ret)
4338 ret = err;
4339
4340out:
4341 kfree(sa);
4342drop_write:
4343 mnt_drop_write_file(file);
4344 return ret;
4345}
4346
4347static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4348{
4349 struct inode *inode = file_inode(file);
4350 struct btrfs_root *root = BTRFS_I(inode)->root;
4351 struct btrfs_ioctl_qgroup_limit_args *sa;
4352 struct btrfs_trans_handle *trans;
4353 int ret;
4354 int err;
4355 u64 qgroupid;
4356
4357 if (!capable(CAP_SYS_ADMIN))
4358 return -EPERM;
4359
4360 ret = mnt_want_write_file(file);
4361 if (ret)
4362 return ret;
4363
4364 sa = memdup_user(arg, sizeof(*sa));
4365 if (IS_ERR(sa)) {
4366 ret = PTR_ERR(sa);
4367 goto drop_write;
4368 }
4369
4370 trans = btrfs_join_transaction(root);
4371 if (IS_ERR(trans)) {
4372 ret = PTR_ERR(trans);
4373 goto out;
4374 }
4375
4376 qgroupid = sa->qgroupid;
4377 if (!qgroupid) {
4378 /* take the current subvol as qgroup */
4379 qgroupid = root->root_key.objectid;
4380 }
4381
4382 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
4383
4384 err = btrfs_end_transaction(trans);
4385 if (err && !ret)
4386 ret = err;
4387
4388out:
4389 kfree(sa);
4390drop_write:
4391 mnt_drop_write_file(file);
4392 return ret;
4393}
4394
4395static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4396{
4397 struct inode *inode = file_inode(file);
4398 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4399 struct btrfs_ioctl_quota_rescan_args *qsa;
4400 int ret;
4401
4402 if (!capable(CAP_SYS_ADMIN))
4403 return -EPERM;
4404
4405 ret = mnt_want_write_file(file);
4406 if (ret)
4407 return ret;
4408
4409 qsa = memdup_user(arg, sizeof(*qsa));
4410 if (IS_ERR(qsa)) {
4411 ret = PTR_ERR(qsa);
4412 goto drop_write;
4413 }
4414
4415 if (qsa->flags) {
4416 ret = -EINVAL;
4417 goto out;
4418 }
4419
4420 ret = btrfs_qgroup_rescan(fs_info);
4421
4422out:
4423 kfree(qsa);
4424drop_write:
4425 mnt_drop_write_file(file);
4426 return ret;
4427}
4428
Olivier Deprez157378f2022-04-04 15:47:50 +02004429static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
4430 void __user *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004431{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004432 struct btrfs_ioctl_quota_rescan_args *qsa;
4433 int ret = 0;
4434
4435 if (!capable(CAP_SYS_ADMIN))
4436 return -EPERM;
4437
4438 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
4439 if (!qsa)
4440 return -ENOMEM;
4441
4442 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4443 qsa->flags = 1;
4444 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
4445 }
4446
4447 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4448 ret = -EFAULT;
4449
4450 kfree(qsa);
4451 return ret;
4452}
4453
Olivier Deprez157378f2022-04-04 15:47:50 +02004454static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info,
4455 void __user *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004456{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004457 if (!capable(CAP_SYS_ADMIN))
4458 return -EPERM;
4459
4460 return btrfs_qgroup_wait_for_completion(fs_info, true);
4461}
4462
4463static long _btrfs_ioctl_set_received_subvol(struct file *file,
4464 struct btrfs_ioctl_received_subvol_args *sa)
4465{
4466 struct inode *inode = file_inode(file);
4467 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4468 struct btrfs_root *root = BTRFS_I(inode)->root;
4469 struct btrfs_root_item *root_item = &root->root_item;
4470 struct btrfs_trans_handle *trans;
4471 struct timespec64 ct = current_time(inode);
4472 int ret = 0;
4473 int received_uuid_changed;
4474
4475 if (!inode_owner_or_capable(inode))
4476 return -EPERM;
4477
4478 ret = mnt_want_write_file(file);
4479 if (ret < 0)
4480 return ret;
4481
4482 down_write(&fs_info->subvol_sem);
4483
4484 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
4485 ret = -EINVAL;
4486 goto out;
4487 }
4488
4489 if (btrfs_root_readonly(root)) {
4490 ret = -EROFS;
4491 goto out;
4492 }
4493
4494 /*
4495 * 1 - root item
4496 * 2 - uuid items (received uuid + subvol uuid)
4497 */
4498 trans = btrfs_start_transaction(root, 3);
4499 if (IS_ERR(trans)) {
4500 ret = PTR_ERR(trans);
4501 trans = NULL;
4502 goto out;
4503 }
4504
4505 sa->rtransid = trans->transid;
4506 sa->rtime.sec = ct.tv_sec;
4507 sa->rtime.nsec = ct.tv_nsec;
4508
4509 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4510 BTRFS_UUID_SIZE);
4511 if (received_uuid_changed &&
4512 !btrfs_is_empty_uuid(root_item->received_uuid)) {
4513 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
4514 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4515 root->root_key.objectid);
4516 if (ret && ret != -ENOENT) {
4517 btrfs_abort_transaction(trans, ret);
4518 btrfs_end_transaction(trans);
4519 goto out;
4520 }
4521 }
4522 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4523 btrfs_set_root_stransid(root_item, sa->stransid);
4524 btrfs_set_root_rtransid(root_item, sa->rtransid);
4525 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4526 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4527 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4528 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
4529
4530 ret = btrfs_update_root(trans, fs_info->tree_root,
4531 &root->root_key, &root->root_item);
4532 if (ret < 0) {
4533 btrfs_end_transaction(trans);
4534 goto out;
4535 }
4536 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4537 ret = btrfs_uuid_tree_add(trans, sa->uuid,
4538 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4539 root->root_key.objectid);
4540 if (ret < 0 && ret != -EEXIST) {
4541 btrfs_abort_transaction(trans, ret);
4542 btrfs_end_transaction(trans);
4543 goto out;
4544 }
4545 }
4546 ret = btrfs_commit_transaction(trans);
4547out:
4548 up_write(&fs_info->subvol_sem);
4549 mnt_drop_write_file(file);
4550 return ret;
4551}
4552
4553#ifdef CONFIG_64BIT
4554static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4555 void __user *arg)
4556{
4557 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4558 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4559 int ret = 0;
4560
4561 args32 = memdup_user(arg, sizeof(*args32));
4562 if (IS_ERR(args32))
4563 return PTR_ERR(args32);
4564
4565 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
4566 if (!args64) {
4567 ret = -ENOMEM;
4568 goto out;
4569 }
4570
4571 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4572 args64->stransid = args32->stransid;
4573 args64->rtransid = args32->rtransid;
4574 args64->stime.sec = args32->stime.sec;
4575 args64->stime.nsec = args32->stime.nsec;
4576 args64->rtime.sec = args32->rtime.sec;
4577 args64->rtime.nsec = args32->rtime.nsec;
4578 args64->flags = args32->flags;
4579
4580 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4581 if (ret)
4582 goto out;
4583
4584 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4585 args32->stransid = args64->stransid;
4586 args32->rtransid = args64->rtransid;
4587 args32->stime.sec = args64->stime.sec;
4588 args32->stime.nsec = args64->stime.nsec;
4589 args32->rtime.sec = args64->rtime.sec;
4590 args32->rtime.nsec = args64->rtime.nsec;
4591 args32->flags = args64->flags;
4592
4593 ret = copy_to_user(arg, args32, sizeof(*args32));
4594 if (ret)
4595 ret = -EFAULT;
4596
4597out:
4598 kfree(args32);
4599 kfree(args64);
4600 return ret;
4601}
4602#endif
4603
4604static long btrfs_ioctl_set_received_subvol(struct file *file,
4605 void __user *arg)
4606{
4607 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4608 int ret = 0;
4609
4610 sa = memdup_user(arg, sizeof(*sa));
4611 if (IS_ERR(sa))
4612 return PTR_ERR(sa);
4613
4614 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4615
4616 if (ret)
4617 goto out;
4618
4619 ret = copy_to_user(arg, sa, sizeof(*sa));
4620 if (ret)
4621 ret = -EFAULT;
4622
4623out:
4624 kfree(sa);
4625 return ret;
4626}
4627
Olivier Deprez157378f2022-04-04 15:47:50 +02004628static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
4629 void __user *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004630{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004631 size_t len;
4632 int ret;
4633 char label[BTRFS_LABEL_SIZE];
4634
4635 spin_lock(&fs_info->super_lock);
4636 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4637 spin_unlock(&fs_info->super_lock);
4638
4639 len = strnlen(label, BTRFS_LABEL_SIZE);
4640
4641 if (len == BTRFS_LABEL_SIZE) {
4642 btrfs_warn(fs_info,
4643 "label is too long, return the first %zu bytes",
4644 --len);
4645 }
4646
4647 ret = copy_to_user(arg, label, len);
4648
4649 return ret ? -EFAULT : 0;
4650}
4651
4652static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4653{
4654 struct inode *inode = file_inode(file);
4655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4656 struct btrfs_root *root = BTRFS_I(inode)->root;
4657 struct btrfs_super_block *super_block = fs_info->super_copy;
4658 struct btrfs_trans_handle *trans;
4659 char label[BTRFS_LABEL_SIZE];
4660 int ret;
4661
4662 if (!capable(CAP_SYS_ADMIN))
4663 return -EPERM;
4664
4665 if (copy_from_user(label, arg, sizeof(label)))
4666 return -EFAULT;
4667
4668 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4669 btrfs_err(fs_info,
4670 "unable to set label with more than %d bytes",
4671 BTRFS_LABEL_SIZE - 1);
4672 return -EINVAL;
4673 }
4674
4675 ret = mnt_want_write_file(file);
4676 if (ret)
4677 return ret;
4678
4679 trans = btrfs_start_transaction(root, 0);
4680 if (IS_ERR(trans)) {
4681 ret = PTR_ERR(trans);
4682 goto out_unlock;
4683 }
4684
4685 spin_lock(&fs_info->super_lock);
4686 strcpy(super_block->label, label);
4687 spin_unlock(&fs_info->super_lock);
4688 ret = btrfs_commit_transaction(trans);
4689
4690out_unlock:
4691 mnt_drop_write_file(file);
4692 return ret;
4693}
4694
4695#define INIT_FEATURE_FLAGS(suffix) \
4696 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4697 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4698 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4699
4700int btrfs_ioctl_get_supported_features(void __user *arg)
4701{
4702 static const struct btrfs_ioctl_feature_flags features[3] = {
4703 INIT_FEATURE_FLAGS(SUPP),
4704 INIT_FEATURE_FLAGS(SAFE_SET),
4705 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4706 };
4707
4708 if (copy_to_user(arg, &features, sizeof(features)))
4709 return -EFAULT;
4710
4711 return 0;
4712}
4713
Olivier Deprez157378f2022-04-04 15:47:50 +02004714static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4715 void __user *arg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004716{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004717 struct btrfs_super_block *super_block = fs_info->super_copy;
4718 struct btrfs_ioctl_feature_flags features;
4719
4720 features.compat_flags = btrfs_super_compat_flags(super_block);
4721 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4722 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4723
4724 if (copy_to_user(arg, &features, sizeof(features)))
4725 return -EFAULT;
4726
4727 return 0;
4728}
4729
4730static int check_feature_bits(struct btrfs_fs_info *fs_info,
4731 enum btrfs_feature_set set,
4732 u64 change_mask, u64 flags, u64 supported_flags,
4733 u64 safe_set, u64 safe_clear)
4734{
David Brazdil0f672f62019-12-10 10:32:29 +00004735 const char *type = btrfs_feature_set_name(set);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004736 char *names;
4737 u64 disallowed, unsupported;
4738 u64 set_mask = flags & change_mask;
4739 u64 clear_mask = ~flags & change_mask;
4740
4741 unsupported = set_mask & ~supported_flags;
4742 if (unsupported) {
4743 names = btrfs_printable_features(set, unsupported);
4744 if (names) {
4745 btrfs_warn(fs_info,
4746 "this kernel does not support the %s feature bit%s",
4747 names, strchr(names, ',') ? "s" : "");
4748 kfree(names);
4749 } else
4750 btrfs_warn(fs_info,
4751 "this kernel does not support %s bits 0x%llx",
4752 type, unsupported);
4753 return -EOPNOTSUPP;
4754 }
4755
4756 disallowed = set_mask & ~safe_set;
4757 if (disallowed) {
4758 names = btrfs_printable_features(set, disallowed);
4759 if (names) {
4760 btrfs_warn(fs_info,
4761 "can't set the %s feature bit%s while mounted",
4762 names, strchr(names, ',') ? "s" : "");
4763 kfree(names);
4764 } else
4765 btrfs_warn(fs_info,
4766 "can't set %s bits 0x%llx while mounted",
4767 type, disallowed);
4768 return -EPERM;
4769 }
4770
4771 disallowed = clear_mask & ~safe_clear;
4772 if (disallowed) {
4773 names = btrfs_printable_features(set, disallowed);
4774 if (names) {
4775 btrfs_warn(fs_info,
4776 "can't clear the %s feature bit%s while mounted",
4777 names, strchr(names, ',') ? "s" : "");
4778 kfree(names);
4779 } else
4780 btrfs_warn(fs_info,
4781 "can't clear %s bits 0x%llx while mounted",
4782 type, disallowed);
4783 return -EPERM;
4784 }
4785
4786 return 0;
4787}
4788
4789#define check_feature(fs_info, change_mask, flags, mask_base) \
4790check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4791 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4792 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4793 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4794
4795static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4796{
4797 struct inode *inode = file_inode(file);
4798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4799 struct btrfs_root *root = BTRFS_I(inode)->root;
4800 struct btrfs_super_block *super_block = fs_info->super_copy;
4801 struct btrfs_ioctl_feature_flags flags[2];
4802 struct btrfs_trans_handle *trans;
4803 u64 newflags;
4804 int ret;
4805
4806 if (!capable(CAP_SYS_ADMIN))
4807 return -EPERM;
4808
4809 if (copy_from_user(flags, arg, sizeof(flags)))
4810 return -EFAULT;
4811
4812 /* Nothing to do */
4813 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4814 !flags[0].incompat_flags)
4815 return 0;
4816
4817 ret = check_feature(fs_info, flags[0].compat_flags,
4818 flags[1].compat_flags, COMPAT);
4819 if (ret)
4820 return ret;
4821
4822 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4823 flags[1].compat_ro_flags, COMPAT_RO);
4824 if (ret)
4825 return ret;
4826
4827 ret = check_feature(fs_info, flags[0].incompat_flags,
4828 flags[1].incompat_flags, INCOMPAT);
4829 if (ret)
4830 return ret;
4831
4832 ret = mnt_want_write_file(file);
4833 if (ret)
4834 return ret;
4835
4836 trans = btrfs_start_transaction(root, 0);
4837 if (IS_ERR(trans)) {
4838 ret = PTR_ERR(trans);
4839 goto out_drop_write;
4840 }
4841
4842 spin_lock(&fs_info->super_lock);
4843 newflags = btrfs_super_compat_flags(super_block);
4844 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4845 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4846 btrfs_set_super_compat_flags(super_block, newflags);
4847
4848 newflags = btrfs_super_compat_ro_flags(super_block);
4849 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4850 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4851 btrfs_set_super_compat_ro_flags(super_block, newflags);
4852
4853 newflags = btrfs_super_incompat_flags(super_block);
4854 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4855 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4856 btrfs_set_super_incompat_flags(super_block, newflags);
4857 spin_unlock(&fs_info->super_lock);
4858
4859 ret = btrfs_commit_transaction(trans);
4860out_drop_write:
4861 mnt_drop_write_file(file);
4862
4863 return ret;
4864}
4865
4866static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
4867{
4868 struct btrfs_ioctl_send_args *arg;
4869 int ret;
4870
4871 if (compat) {
4872#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4873 struct btrfs_ioctl_send_args_32 args32;
4874
4875 ret = copy_from_user(&args32, argp, sizeof(args32));
4876 if (ret)
4877 return -EFAULT;
4878 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
4879 if (!arg)
4880 return -ENOMEM;
4881 arg->send_fd = args32.send_fd;
4882 arg->clone_sources_count = args32.clone_sources_count;
4883 arg->clone_sources = compat_ptr(args32.clone_sources);
4884 arg->parent_root = args32.parent_root;
4885 arg->flags = args32.flags;
4886 memcpy(arg->reserved, args32.reserved,
4887 sizeof(args32.reserved));
4888#else
4889 return -ENOTTY;
4890#endif
4891 } else {
4892 arg = memdup_user(argp, sizeof(*arg));
4893 if (IS_ERR(arg))
4894 return PTR_ERR(arg);
4895 }
4896 ret = btrfs_ioctl_send(file, arg);
4897 kfree(arg);
4898 return ret;
4899}
4900
4901long btrfs_ioctl(struct file *file, unsigned int
4902 cmd, unsigned long arg)
4903{
4904 struct inode *inode = file_inode(file);
4905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4906 struct btrfs_root *root = BTRFS_I(inode)->root;
4907 void __user *argp = (void __user *)arg;
4908
4909 switch (cmd) {
4910 case FS_IOC_GETFLAGS:
4911 return btrfs_ioctl_getflags(file, argp);
4912 case FS_IOC_SETFLAGS:
4913 return btrfs_ioctl_setflags(file, argp);
4914 case FS_IOC_GETVERSION:
4915 return btrfs_ioctl_getversion(file, argp);
David Brazdil0f672f62019-12-10 10:32:29 +00004916 case FS_IOC_GETFSLABEL:
Olivier Deprez157378f2022-04-04 15:47:50 +02004917 return btrfs_ioctl_get_fslabel(fs_info, argp);
David Brazdil0f672f62019-12-10 10:32:29 +00004918 case FS_IOC_SETFSLABEL:
4919 return btrfs_ioctl_set_fslabel(file, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004920 case FITRIM:
Olivier Deprez157378f2022-04-04 15:47:50 +02004921 return btrfs_ioctl_fitrim(fs_info, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004922 case BTRFS_IOC_SNAP_CREATE:
4923 return btrfs_ioctl_snap_create(file, argp, 0);
4924 case BTRFS_IOC_SNAP_CREATE_V2:
4925 return btrfs_ioctl_snap_create_v2(file, argp, 0);
4926 case BTRFS_IOC_SUBVOL_CREATE:
4927 return btrfs_ioctl_snap_create(file, argp, 1);
4928 case BTRFS_IOC_SUBVOL_CREATE_V2:
4929 return btrfs_ioctl_snap_create_v2(file, argp, 1);
4930 case BTRFS_IOC_SNAP_DESTROY:
Olivier Deprez157378f2022-04-04 15:47:50 +02004931 return btrfs_ioctl_snap_destroy(file, argp, false);
4932 case BTRFS_IOC_SNAP_DESTROY_V2:
4933 return btrfs_ioctl_snap_destroy(file, argp, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004934 case BTRFS_IOC_SUBVOL_GETFLAGS:
4935 return btrfs_ioctl_subvol_getflags(file, argp);
4936 case BTRFS_IOC_SUBVOL_SETFLAGS:
4937 return btrfs_ioctl_subvol_setflags(file, argp);
4938 case BTRFS_IOC_DEFAULT_SUBVOL:
4939 return btrfs_ioctl_default_subvol(file, argp);
4940 case BTRFS_IOC_DEFRAG:
4941 return btrfs_ioctl_defrag(file, NULL);
4942 case BTRFS_IOC_DEFRAG_RANGE:
4943 return btrfs_ioctl_defrag(file, argp);
4944 case BTRFS_IOC_RESIZE:
4945 return btrfs_ioctl_resize(file, argp);
4946 case BTRFS_IOC_ADD_DEV:
4947 return btrfs_ioctl_add_dev(fs_info, argp);
4948 case BTRFS_IOC_RM_DEV:
4949 return btrfs_ioctl_rm_dev(file, argp);
4950 case BTRFS_IOC_RM_DEV_V2:
4951 return btrfs_ioctl_rm_dev_v2(file, argp);
4952 case BTRFS_IOC_FS_INFO:
4953 return btrfs_ioctl_fs_info(fs_info, argp);
4954 case BTRFS_IOC_DEV_INFO:
4955 return btrfs_ioctl_dev_info(fs_info, argp);
4956 case BTRFS_IOC_BALANCE:
4957 return btrfs_ioctl_balance(file, NULL);
4958 case BTRFS_IOC_TREE_SEARCH:
4959 return btrfs_ioctl_tree_search(file, argp);
4960 case BTRFS_IOC_TREE_SEARCH_V2:
4961 return btrfs_ioctl_tree_search_v2(file, argp);
4962 case BTRFS_IOC_INO_LOOKUP:
4963 return btrfs_ioctl_ino_lookup(file, argp);
4964 case BTRFS_IOC_INO_PATHS:
4965 return btrfs_ioctl_ino_to_path(root, argp);
4966 case BTRFS_IOC_LOGICAL_INO:
4967 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
4968 case BTRFS_IOC_LOGICAL_INO_V2:
4969 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
4970 case BTRFS_IOC_SPACE_INFO:
4971 return btrfs_ioctl_space_info(fs_info, argp);
4972 case BTRFS_IOC_SYNC: {
4973 int ret;
4974
Olivier Deprez157378f2022-04-04 15:47:50 +02004975 ret = btrfs_start_delalloc_roots(fs_info, U64_MAX, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004976 if (ret)
4977 return ret;
4978 ret = btrfs_sync_fs(inode->i_sb, 1);
4979 /*
4980 * The transaction thread may want to do more work,
4981 * namely it pokes the cleaner kthread that will start
4982 * processing uncleaned subvols.
4983 */
4984 wake_up_process(fs_info->transaction_kthread);
4985 return ret;
4986 }
4987 case BTRFS_IOC_START_SYNC:
4988 return btrfs_ioctl_start_sync(root, argp);
4989 case BTRFS_IOC_WAIT_SYNC:
4990 return btrfs_ioctl_wait_sync(fs_info, argp);
4991 case BTRFS_IOC_SCRUB:
4992 return btrfs_ioctl_scrub(file, argp);
4993 case BTRFS_IOC_SCRUB_CANCEL:
4994 return btrfs_ioctl_scrub_cancel(fs_info);
4995 case BTRFS_IOC_SCRUB_PROGRESS:
4996 return btrfs_ioctl_scrub_progress(fs_info, argp);
4997 case BTRFS_IOC_BALANCE_V2:
4998 return btrfs_ioctl_balance(file, argp);
4999 case BTRFS_IOC_BALANCE_CTL:
5000 return btrfs_ioctl_balance_ctl(fs_info, arg);
5001 case BTRFS_IOC_BALANCE_PROGRESS:
5002 return btrfs_ioctl_balance_progress(fs_info, argp);
5003 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5004 return btrfs_ioctl_set_received_subvol(file, argp);
5005#ifdef CONFIG_64BIT
5006 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5007 return btrfs_ioctl_set_received_subvol_32(file, argp);
5008#endif
5009 case BTRFS_IOC_SEND:
5010 return _btrfs_ioctl_send(file, argp, false);
5011#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5012 case BTRFS_IOC_SEND_32:
5013 return _btrfs_ioctl_send(file, argp, true);
5014#endif
5015 case BTRFS_IOC_GET_DEV_STATS:
5016 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5017 case BTRFS_IOC_QUOTA_CTL:
5018 return btrfs_ioctl_quota_ctl(file, argp);
5019 case BTRFS_IOC_QGROUP_ASSIGN:
5020 return btrfs_ioctl_qgroup_assign(file, argp);
5021 case BTRFS_IOC_QGROUP_CREATE:
5022 return btrfs_ioctl_qgroup_create(file, argp);
5023 case BTRFS_IOC_QGROUP_LIMIT:
5024 return btrfs_ioctl_qgroup_limit(file, argp);
5025 case BTRFS_IOC_QUOTA_RESCAN:
5026 return btrfs_ioctl_quota_rescan(file, argp);
5027 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
Olivier Deprez157378f2022-04-04 15:47:50 +02005028 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005029 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
Olivier Deprez157378f2022-04-04 15:47:50 +02005030 return btrfs_ioctl_quota_rescan_wait(fs_info, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005031 case BTRFS_IOC_DEV_REPLACE:
5032 return btrfs_ioctl_dev_replace(fs_info, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005033 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5034 return btrfs_ioctl_get_supported_features(argp);
5035 case BTRFS_IOC_GET_FEATURES:
Olivier Deprez157378f2022-04-04 15:47:50 +02005036 return btrfs_ioctl_get_features(fs_info, argp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005037 case BTRFS_IOC_SET_FEATURES:
5038 return btrfs_ioctl_set_features(file, argp);
5039 case FS_IOC_FSGETXATTR:
5040 return btrfs_ioctl_fsgetxattr(file, argp);
5041 case FS_IOC_FSSETXATTR:
5042 return btrfs_ioctl_fssetxattr(file, argp);
5043 case BTRFS_IOC_GET_SUBVOL_INFO:
5044 return btrfs_ioctl_get_subvol_info(file, argp);
5045 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5046 return btrfs_ioctl_get_subvol_rootref(file, argp);
5047 case BTRFS_IOC_INO_LOOKUP_USER:
5048 return btrfs_ioctl_ino_lookup_user(file, argp);
5049 }
5050
5051 return -ENOTTY;
5052}
5053
5054#ifdef CONFIG_COMPAT
5055long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5056{
5057 /*
5058 * These all access 32-bit values anyway so no further
5059 * handling is necessary.
5060 */
5061 switch (cmd) {
5062 case FS_IOC32_GETFLAGS:
5063 cmd = FS_IOC_GETFLAGS;
5064 break;
5065 case FS_IOC32_SETFLAGS:
5066 cmd = FS_IOC_SETFLAGS;
5067 break;
5068 case FS_IOC32_GETVERSION:
5069 cmd = FS_IOC_GETVERSION;
5070 break;
5071 }
5072
5073 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5074}
5075#endif