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