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