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