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