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