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