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