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