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