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