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