]> git.ipfire.org Git - people/ms/linux.git/blame - fs/btrfs/ioctl.c
btrfs: qgroup: Drop fs_info parameter from btrfs_create_qgroup
[people/ms/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
0b246afa 604 ret = btrfs_qgroup_inherit(trans, fs_info, 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:
e441d54d
CM
2931 if (!(file->f_mode & FMODE_WRITE)) {
2932 ret = -EINVAL;
2933 goto out;
2934 }
1e701a32
CM
2935
2936 range = kzalloc(sizeof(*range), GFP_KERNEL);
2937 if (!range) {
2938 ret = -ENOMEM;
2939 goto out;
2940 }
2941
2942 if (argp) {
2943 if (copy_from_user(range, argp,
2944 sizeof(*range))) {
2945 ret = -EFAULT;
2946 kfree(range);
683be16e 2947 goto out;
1e701a32
CM
2948 }
2949 /* compression requires us to start the IO */
2950 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2951 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2952 range->extent_thresh = (u32)-1;
2953 }
2954 } else {
2955 /* the rest are all set to zero by kzalloc */
2956 range->len = (u64)-1;
2957 }
496ad9aa 2958 ret = btrfs_defrag_file(file_inode(file), file,
7c829b72 2959 range, BTRFS_OLDEST_GENERATION, 0);
4cb5300b
CM
2960 if (ret > 0)
2961 ret = 0;
1e701a32 2962 kfree(range);
f46b5a66 2963 break;
8929ecfa
YZ
2964 default:
2965 ret = -EINVAL;
f46b5a66 2966 }
e441d54d 2967out:
25122d15 2968 mnt_drop_write_file(file);
e441d54d 2969 return ret;
f46b5a66
CH
2970}
2971
2ff7e61e 2972static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
f46b5a66
CH
2973{
2974 struct btrfs_ioctl_vol_args *vol_args;
2975 int ret;
2976
e441d54d
CM
2977 if (!capable(CAP_SYS_ADMIN))
2978 return -EPERM;
2979
171938e5 2980 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
e57138b3 2981 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
c9e9f97b 2982
dae7b665 2983 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2984 if (IS_ERR(vol_args)) {
2985 ret = PTR_ERR(vol_args);
2986 goto out;
2987 }
f46b5a66 2988
5516e595 2989 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2ff7e61e 2990 ret = btrfs_init_new_device(fs_info, vol_args->name);
f46b5a66 2991
43d20761 2992 if (!ret)
0b246afa 2993 btrfs_info(fs_info, "disk added %s", vol_args->name);
43d20761 2994
f46b5a66 2995 kfree(vol_args);
c9e9f97b 2996out:
171938e5 2997 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
f46b5a66
CH
2998 return ret;
2999}
3000
6b526ed7 3001static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
f46b5a66 3002{
0b246afa
JM
3003 struct inode *inode = file_inode(file);
3004 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6b526ed7 3005 struct btrfs_ioctl_vol_args_v2 *vol_args;
f46b5a66
CH
3006 int ret;
3007
e441d54d
CM
3008 if (!capable(CAP_SYS_ADMIN))
3009 return -EPERM;
3010
da24927b
MX
3011 ret = mnt_want_write_file(file);
3012 if (ret)
3013 return ret;
c146afad 3014
dae7b665 3015 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
3016 if (IS_ERR(vol_args)) {
3017 ret = PTR_ERR(vol_args);
c47ca32d 3018 goto err_drop;
c9e9f97b 3019 }
f46b5a66 3020
6b526ed7 3021 /* Check for compatibility reject unknown flags */
fd4e994b
OS
3022 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) {
3023 ret = -EOPNOTSUPP;
3024 goto out;
3025 }
f46b5a66 3026
171938e5 3027 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
183860f6
AJ
3028 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3029 goto out;
3030 }
3031
735654ea 3032 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
2ff7e61e 3033 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
6b526ed7
AJ
3034 } else {
3035 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2ff7e61e 3036 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
6b526ed7 3037 }
171938e5 3038 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
183860f6 3039
6b526ed7 3040 if (!ret) {
735654ea 3041 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
0b246afa 3042 btrfs_info(fs_info, "device deleted: id %llu",
6b526ed7
AJ
3043 vol_args->devid);
3044 else
0b246afa 3045 btrfs_info(fs_info, "device deleted: %s",
6b526ed7
AJ
3046 vol_args->name);
3047 }
183860f6
AJ
3048out:
3049 kfree(vol_args);
c47ca32d 3050err_drop:
4ac20c70 3051 mnt_drop_write_file(file);
f46b5a66
CH
3052 return ret;
3053}
3054
da24927b 3055static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
f46b5a66 3056{
0b246afa
JM
3057 struct inode *inode = file_inode(file);
3058 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
f46b5a66
CH
3059 struct btrfs_ioctl_vol_args *vol_args;
3060 int ret;
3061
e441d54d
CM
3062 if (!capable(CAP_SYS_ADMIN))
3063 return -EPERM;
3064
da24927b
MX
3065 ret = mnt_want_write_file(file);
3066 if (ret)
3067 return ret;
c146afad 3068
171938e5 3069 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
183860f6 3070 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
58d7bbf8
DS
3071 goto out_drop_write;
3072 }
3073
3074 vol_args = memdup_user(arg, sizeof(*vol_args));
3075 if (IS_ERR(vol_args)) {
3076 ret = PTR_ERR(vol_args);
183860f6
AJ
3077 goto out;
3078 }
3079
58d7bbf8 3080 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2ff7e61e 3081 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
183860f6 3082
ec95d491 3083 if (!ret)
0b246afa 3084 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
183860f6 3085 kfree(vol_args);
58d7bbf8 3086out:
171938e5 3087 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
58d7bbf8 3088out_drop_write:
4ac20c70 3089 mnt_drop_write_file(file);
58d7bbf8 3090
f46b5a66
CH
3091 return ret;
3092}
3093
2ff7e61e
JM
3094static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
3095 void __user *arg)
475f6387 3096{
027ed2f0 3097 struct btrfs_ioctl_fs_info_args *fi_args;
475f6387 3098 struct btrfs_device *device;
0b246afa 3099 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
027ed2f0 3100 int ret = 0;
475f6387 3101
027ed2f0
LZ
3102 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
3103 if (!fi_args)
3104 return -ENOMEM;
3105
d03262c7 3106 rcu_read_lock();
027ed2f0 3107 fi_args->num_devices = fs_devices->num_devices;
475f6387 3108
d03262c7 3109 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
027ed2f0
LZ
3110 if (device->devid > fi_args->max_id)
3111 fi_args->max_id = device->devid;
475f6387 3112 }
d03262c7 3113 rcu_read_unlock();
475f6387 3114
d03262c7 3115 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
bea7eafd
OS
3116 fi_args->nodesize = fs_info->nodesize;
3117 fi_args->sectorsize = fs_info->sectorsize;
3118 fi_args->clone_alignment = fs_info->sectorsize;
80a773fb 3119
027ed2f0
LZ
3120 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
3121 ret = -EFAULT;
475f6387 3122
027ed2f0
LZ
3123 kfree(fi_args);
3124 return ret;
475f6387
JS
3125}
3126
2ff7e61e
JM
3127static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
3128 void __user *arg)
475f6387
JS
3129{
3130 struct btrfs_ioctl_dev_info_args *di_args;
3131 struct btrfs_device *dev;
475f6387
JS
3132 int ret = 0;
3133 char *s_uuid = NULL;
475f6387 3134
475f6387
JS
3135 di_args = memdup_user(arg, sizeof(*di_args));
3136 if (IS_ERR(di_args))
3137 return PTR_ERR(di_args);
3138
dd5f9615 3139 if (!btrfs_is_empty_uuid(di_args->uuid))
475f6387
JS
3140 s_uuid = di_args->uuid;
3141
c5593ca3 3142 rcu_read_lock();
0b246afa 3143 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
475f6387
JS
3144
3145 if (!dev) {
3146 ret = -ENODEV;
3147 goto out;
3148 }
3149
3150 di_args->devid = dev->devid;
7cc8e58d
MX
3151 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
3152 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
475f6387 3153 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
a27202fb 3154 if (dev->name) {
606686ee
JB
3155 struct rcu_string *name;
3156
606686ee 3157 name = rcu_dereference(dev->name);
6670d4c2 3158 strncpy(di_args->path, name->str, sizeof(di_args->path) - 1);
a27202fb
JM
3159 di_args->path[sizeof(di_args->path) - 1] = 0;
3160 } else {
99ba55ad 3161 di_args->path[0] = '\0';
a27202fb 3162 }
475f6387
JS
3163
3164out:
c5593ca3 3165 rcu_read_unlock();
475f6387
JS
3166 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
3167 ret = -EFAULT;
3168
3169 kfree(di_args);
3170 return ret;
3171}
3172
f4414602 3173static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
416161db
MF
3174{
3175 struct page *page;
416161db 3176
416161db
MF
3177 page = grab_cache_page(inode->i_mapping, index);
3178 if (!page)
31314002 3179 return ERR_PTR(-ENOMEM);
416161db
MF
3180
3181 if (!PageUptodate(page)) {
31314002
FM
3182 int ret;
3183
3184 ret = btrfs_readpage(NULL, page);
3185 if (ret)
3186 return ERR_PTR(ret);
416161db
MF
3187 lock_page(page);
3188 if (!PageUptodate(page)) {
3189 unlock_page(page);
09cbfeaf 3190 put_page(page);
31314002
FM
3191 return ERR_PTR(-EIO);
3192 }
3193 if (page->mapping != inode->i_mapping) {
3194 unlock_page(page);
09cbfeaf 3195 put_page(page);
31314002 3196 return ERR_PTR(-EAGAIN);
416161db
MF
3197 }
3198 }
416161db
MF
3199
3200 return page;
3201}
3202
f4414602
MF
3203static int gather_extent_pages(struct inode *inode, struct page **pages,
3204 int num_pages, u64 off)
3205{
3206 int i;
09cbfeaf 3207 pgoff_t index = off >> PAGE_SHIFT;
f4414602
MF
3208
3209 for (i = 0; i < num_pages; i++) {
31314002 3210again:
f4414602 3211 pages[i] = extent_same_get_page(inode, index + i);
31314002
FM
3212 if (IS_ERR(pages[i])) {
3213 int err = PTR_ERR(pages[i]);
3214
3215 if (err == -EAGAIN)
3216 goto again;
3217 pages[i] = NULL;
3218 return err;
3219 }
f4414602
MF
3220 }
3221 return 0;
3222}
3223
e0bd70c6
FM
3224static int lock_extent_range(struct inode *inode, u64 off, u64 len,
3225 bool retry_range_locking)
77fe20dc 3226{
e0bd70c6
FM
3227 /*
3228 * Do any pending delalloc/csum calculations on inode, one way or
3229 * another, and lock file content.
3230 * The locking order is:
3231 *
3232 * 1) pages
3233 * 2) range in the inode's io tree
3234 */
77fe20dc
MF
3235 while (1) {
3236 struct btrfs_ordered_extent *ordered;
3237 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3238 ordered = btrfs_lookup_first_ordered_extent(inode,
3239 off + len - 1);
ff5df9b8
FM
3240 if ((!ordered ||
3241 ordered->file_offset + ordered->len <= off ||
3242 ordered->file_offset >= off + len) &&
77fe20dc 3243 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
ff5df9b8
FM
3244 off + len - 1, EXTENT_DELALLOC, 0, NULL)) {
3245 if (ordered)
3246 btrfs_put_ordered_extent(ordered);
77fe20dc 3247 break;
ff5df9b8 3248 }
77fe20dc
MF
3249 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
3250 if (ordered)
3251 btrfs_put_ordered_extent(ordered);
e0bd70c6
FM
3252 if (!retry_range_locking)
3253 return -EAGAIN;
77fe20dc
MF
3254 btrfs_wait_ordered_range(inode, off, len);
3255 }
e0bd70c6 3256 return 0;
77fe20dc
MF
3257}
3258
f4414602 3259static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2)
416161db 3260{
5955102c
AV
3261 inode_unlock(inode1);
3262 inode_unlock(inode2);
416161db
MF
3263}
3264
f4414602
MF
3265static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2)
3266{
3267 if (inode1 < inode2)
3268 swap(inode1, inode2);
3269
5955102c
AV
3270 inode_lock_nested(inode1, I_MUTEX_PARENT);
3271 inode_lock_nested(inode2, I_MUTEX_CHILD);
f4414602
MF
3272}
3273
3274static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1,
3275 struct inode *inode2, u64 loff2, u64 len)
3276{
3277 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
3278 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
3279}
3280
e0bd70c6
FM
3281static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
3282 struct inode *inode2, u64 loff2, u64 len,
3283 bool retry_range_locking)
416161db 3284{
e0bd70c6
FM
3285 int ret;
3286
416161db
MF
3287 if (inode1 < inode2) {
3288 swap(inode1, inode2);
3289 swap(loff1, loff2);
3290 }
e0bd70c6
FM
3291 ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
3292 if (ret)
3293 return ret;
3294 ret = lock_extent_range(inode2, loff2, len, retry_range_locking);
3295 if (ret)
3296 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1,
3297 loff1 + len - 1);
3298 return ret;
f4414602
MF
3299}
3300
3301struct cmp_pages {
3302 int num_pages;
3303 struct page **src_pages;
3304 struct page **dst_pages;
3305};
3306
3307static void btrfs_cmp_data_free(struct cmp_pages *cmp)
3308{
3309 int i;
3310 struct page *pg;
3311
3312 for (i = 0; i < cmp->num_pages; i++) {
3313 pg = cmp->src_pages[i];
e0bd70c6
FM
3314 if (pg) {
3315 unlock_page(pg);
09cbfeaf 3316 put_page(pg);
97b19170 3317 cmp->src_pages[i] = NULL;
e0bd70c6 3318 }
f4414602 3319 pg = cmp->dst_pages[i];
e0bd70c6
FM
3320 if (pg) {
3321 unlock_page(pg);
09cbfeaf 3322 put_page(pg);
97b19170 3323 cmp->dst_pages[i] = NULL;
e0bd70c6 3324 }
f4414602 3325 }
f4414602
MF
3326}
3327
3328static int btrfs_cmp_data_prepare(struct inode *src, u64 loff,
3329 struct inode *dst, u64 dst_loff,
3330 u64 len, struct cmp_pages *cmp)
3331{
3332 int ret;
09cbfeaf 3333 int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT;
f4414602 3334
f4414602 3335 cmp->num_pages = num_pages;
b1517622 3336
67b07bd4 3337 ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff);
f4414602
MF
3338 if (ret)
3339 goto out;
3340
67b07bd4 3341 ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff);
f4414602
MF
3342
3343out:
3344 if (ret)
3345 btrfs_cmp_data_free(cmp);
78ad4ce0 3346 return ret;
416161db
MF
3347}
3348
1a287cfe 3349static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp)
416161db
MF
3350{
3351 int ret = 0;
f4414602 3352 int i;
416161db 3353 struct page *src_page, *dst_page;
09cbfeaf 3354 unsigned int cmp_len = PAGE_SIZE;
416161db
MF
3355 void *addr, *dst_addr;
3356
f4414602 3357 i = 0;
416161db 3358 while (len) {
09cbfeaf 3359 if (len < PAGE_SIZE)
416161db
MF
3360 cmp_len = len;
3361
f4414602
MF
3362 BUG_ON(i >= cmp->num_pages);
3363
3364 src_page = cmp->src_pages[i];
3365 dst_page = cmp->dst_pages[i];
e0bd70c6
FM
3366 ASSERT(PageLocked(src_page));
3367 ASSERT(PageLocked(dst_page));
f4414602 3368
416161db
MF
3369 addr = kmap_atomic(src_page);
3370 dst_addr = kmap_atomic(dst_page);
3371
3372 flush_dcache_page(src_page);
3373 flush_dcache_page(dst_page);
3374
3375 if (memcmp(addr, dst_addr, cmp_len))
2b3909f8 3376 ret = -EBADE;
416161db
MF
3377
3378 kunmap_atomic(addr);
3379 kunmap_atomic(dst_addr);
416161db
MF
3380
3381 if (ret)
3382 break;
3383
416161db 3384 len -= cmp_len;
f4414602 3385 i++;
416161db
MF
3386 }
3387
3388 return ret;
3389}
3390
e1d227a4
MF
3391static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen,
3392 u64 olen)
416161db 3393{
e1d227a4 3394 u64 len = *plen;
416161db
MF
3395 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
3396
e1d227a4 3397 if (off + olen > inode->i_size || off + olen < off)
416161db 3398 return -EINVAL;
e1d227a4
MF
3399
3400 /* if we extend to eof, continue to block boundary */
3401 if (off + len == inode->i_size)
3402 *plen = len = ALIGN(inode->i_size, bs) - off;
3403
416161db
MF
3404 /* Check that we are block aligned - btrfs_clone() requires this */
3405 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
3406 return -EINVAL;
3407
3408 return 0;
3409}
3410
3973909d 3411static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen,
67b07bd4
TT
3412 struct inode *dst, u64 dst_loff,
3413 struct cmp_pages *cmp)
416161db
MF
3414{
3415 int ret;
e1d227a4 3416 u64 len = olen;
fc4badd9 3417 bool same_inode = (src == dst);
0efa9f48
MF
3418 u64 same_lock_start = 0;
3419 u64 same_lock_len = 0;
416161db 3420
fc4badd9
OS
3421 ret = extent_same_check_offsets(src, loff, &len, olen);
3422 if (ret)
3973909d 3423 return ret;
416161db 3424
fc4badd9
OS
3425 ret = extent_same_check_offsets(dst, dst_loff, &len, olen);
3426 if (ret)
3973909d 3427 return ret;
fc4badd9
OS
3428
3429 if (same_inode) {
0efa9f48
MF
3430 /*
3431 * Single inode case wants the same checks, except we
3432 * don't want our length pushed out past i_size as
3433 * comparing that data range makes no sense.
3434 *
3435 * extent_same_check_offsets() will do this for an
3436 * unaligned length at i_size, so catch it here and
3437 * reject the request.
3438 *
3439 * This effectively means we require aligned extents
3440 * for the single-inode case, whereas the other cases
3441 * allow an unaligned length so long as it ends at
3442 * i_size.
3443 */
3973909d
TT
3444 if (len != olen)
3445 return -EINVAL;
0efa9f48
MF
3446
3447 /* Check for overlapping ranges */
3973909d
TT
3448 if (dst_loff + len > loff && dst_loff < loff + len)
3449 return -EINVAL;
0efa9f48
MF
3450
3451 same_lock_start = min_t(u64, loff, dst_loff);
3452 same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start;
0efa9f48 3453 }
416161db 3454
e0bd70c6 3455again:
67b07bd4 3456 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp);
f4414602 3457 if (ret)
3973909d 3458 return ret;
f4414602 3459
0efa9f48 3460 if (same_inode)
e0bd70c6
FM
3461 ret = lock_extent_range(src, same_lock_start, same_lock_len,
3462 false);
0efa9f48 3463 else
e0bd70c6
FM
3464 ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len,
3465 false);
3466 /*
3467 * If one of the inodes has dirty pages in the respective range or
3468 * ordered extents, we need to flush dellaloc and wait for all ordered
3469 * extents in the range. We must unlock the pages and the ranges in the
3470 * io trees to avoid deadlocks when flushing delalloc (requires locking
3471 * pages) and when waiting for ordered extents to complete (they require
3472 * range locking).
3473 */
3474 if (ret == -EAGAIN) {
3475 /*
3476 * Ranges in the io trees already unlocked. Now unlock all
3477 * pages before waiting for all IO to complete.
3478 */
67b07bd4 3479 btrfs_cmp_data_free(cmp);
e0bd70c6
FM
3480 if (same_inode) {
3481 btrfs_wait_ordered_range(src, same_lock_start,
3482 same_lock_len);
3483 } else {
3484 btrfs_wait_ordered_range(src, loff, len);
3485 btrfs_wait_ordered_range(dst, dst_loff, len);
3486 }
3487 goto again;
3488 }
3489 ASSERT(ret == 0);
3490 if (WARN_ON(ret)) {
3491 /* ranges in the io trees already unlocked */
67b07bd4 3492 btrfs_cmp_data_free(cmp);
e0bd70c6
FM
3493 return ret;
3494 }
f4414602 3495
207910dd 3496 /* pass original length for comparison so we stay within i_size */
67b07bd4 3497 ret = btrfs_cmp_data(olen, cmp);
416161db 3498 if (ret == 0)
1c919a5e 3499 ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1);
416161db 3500
0efa9f48
MF
3501 if (same_inode)
3502 unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start,
3503 same_lock_start + same_lock_len - 1);
3504 else
3505 btrfs_double_extent_unlock(src, loff, dst, dst_loff, len);
f4414602 3506
67b07bd4 3507 btrfs_cmp_data_free(cmp);
3973909d
TT
3508
3509 return ret;
3510}
3511
b6728768
TT
3512#define BTRFS_MAX_DEDUPE_LEN SZ_16M
3513
3973909d
TT
3514static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen,
3515 struct inode *dst, u64 dst_loff)
3516{
3517 int ret;
67b07bd4
TT
3518 struct cmp_pages cmp;
3519 int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT;
3973909d 3520 bool same_inode = (src == dst);
b6728768 3521 u64 i, tail_len, chunk_count;
3973909d
TT
3522
3523 if (olen == 0)
3524 return 0;
3525
3526 if (same_inode)
3527 inode_lock(src);
3528 else
3529 btrfs_double_inode_lock(src, dst);
3530
3531 /* don't make the dst file partly checksummed */
3532 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3533 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
3534 ret = -EINVAL;
3535 goto out_unlock;
3536 }
3537
b6728768
TT
3538 tail_len = olen % BTRFS_MAX_DEDUPE_LEN;
3539 chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN);
67b07bd4
TT
3540 if (chunk_count == 0)
3541 num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT;
3542
3543 /*
3544 * If deduping ranges in the same inode, locking rules make it
3545 * mandatory to always lock pages in ascending order to avoid deadlocks
3546 * with concurrent tasks (such as starting writeback/delalloc).
3547 */
3548 if (same_inode && dst_loff < loff)
3549 swap(loff, dst_loff);
3550
3551 /*
3552 * We must gather up all the pages before we initiate our extent
3553 * locking. We use an array for the page pointers. Size of the array is
3554 * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN.
3555 */
bf5091c8
DS
3556 cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3557 GFP_KERNEL | __GFP_ZERO);
3558 cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *),
3559 GFP_KERNEL | __GFP_ZERO);
67b07bd4 3560 if (!cmp.src_pages || !cmp.dst_pages) {
bf5091c8
DS
3561 ret = -ENOMEM;
3562 goto out_free;
67b07bd4 3563 }
b6728768
TT
3564
3565 for (i = 0; i < chunk_count; i++) {
3566 ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN,
67b07bd4 3567 dst, dst_loff, &cmp);
b6728768 3568 if (ret)
22883ddc 3569 goto out_free;
b6728768
TT
3570
3571 loff += BTRFS_MAX_DEDUPE_LEN;
3572 dst_loff += BTRFS_MAX_DEDUPE_LEN;
3573 }
3574
3575 if (tail_len > 0)
67b07bd4
TT
3576 ret = btrfs_extent_same_range(src, loff, tail_len, dst,
3577 dst_loff, &cmp);
3973909d 3578
22883ddc
LF
3579out_free:
3580 kvfree(cmp.src_pages);
3581 kvfree(cmp.dst_pages);
3582
416161db 3583out_unlock:
0efa9f48 3584 if (same_inode)
5955102c 3585 inode_unlock(src);
0efa9f48
MF
3586 else
3587 btrfs_double_inode_unlock(src, dst);
416161db
MF
3588
3589 return ret;
3590}
3591
2b3909f8
DW
3592ssize_t btrfs_dedupe_file_range(struct file *src_file, u64 loff, u64 olen,
3593 struct file *dst_file, u64 dst_loff)
416161db 3594{
2b3909f8
DW
3595 struct inode *src = file_inode(src_file);
3596 struct inode *dst = file_inode(dst_file);
416161db 3597 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2b3909f8 3598 ssize_t res;
416161db 3599
09cbfeaf 3600 if (WARN_ON_ONCE(bs < PAGE_SIZE)) {
416161db
MF
3601 /*
3602 * Btrfs does not support blocksize < page_size. As a
3603 * result, btrfs_cmp_data() won't correctly handle
3604 * this situation without an update.
3605 */
2b3909f8 3606 return -EINVAL;
416161db
MF
3607 }
3608
2b3909f8
DW
3609 res = btrfs_extent_same(src, loff, olen, dst, dst_loff);
3610 if (res)
3611 return res;
3612 return olen;
416161db
MF
3613}
3614
f82a9901
FM
3615static int clone_finish_inode_update(struct btrfs_trans_handle *trans,
3616 struct inode *inode,
3617 u64 endoff,
3618 const u64 destoff,
1c919a5e
MF
3619 const u64 olen,
3620 int no_time_update)
f82a9901
FM
3621{
3622 struct btrfs_root *root = BTRFS_I(inode)->root;
3623 int ret;
3624
3625 inode_inc_iversion(inode);
1c919a5e 3626 if (!no_time_update)
c2050a45 3627 inode->i_mtime = inode->i_ctime = current_time(inode);
f82a9901
FM
3628 /*
3629 * We round up to the block size at eof when determining which
3630 * extents to clone above, but shouldn't round up the file size.
3631 */
3632 if (endoff > destoff + olen)
3633 endoff = destoff + olen;
3634 if (endoff > inode->i_size)
6ef06d27 3635 btrfs_i_size_write(BTRFS_I(inode), endoff);
f82a9901
FM
3636
3637 ret = btrfs_update_inode(trans, root, inode);
3638 if (ret) {
66642832 3639 btrfs_abort_transaction(trans, ret);
3a45bb20 3640 btrfs_end_transaction(trans);
f82a9901
FM
3641 goto out;
3642 }
3a45bb20 3643 ret = btrfs_end_transaction(trans);
f82a9901
FM
3644out:
3645 return ret;
3646}
3647
a2f392e4 3648static void clone_update_extent_map(struct btrfs_inode *inode,
7ffbb598
FM
3649 const struct btrfs_trans_handle *trans,
3650 const struct btrfs_path *path,
7ffbb598
FM
3651 const u64 hole_offset,
3652 const u64 hole_len)
3653{
a2f392e4 3654 struct extent_map_tree *em_tree = &inode->extent_tree;
7ffbb598
FM
3655 struct extent_map *em;
3656 int ret;
3657
3658 em = alloc_extent_map();
3659 if (!em) {
a2f392e4 3660 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
7ffbb598
FM
3661 return;
3662 }
3663
14f59796
FM
3664 if (path) {
3665 struct btrfs_file_extent_item *fi;
3666
3667 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3668 struct btrfs_file_extent_item);
a2f392e4 3669 btrfs_extent_item_to_extent_map(inode, path, fi, false, em);
7ffbb598
FM
3670 em->generation = -1;
3671 if (btrfs_file_extent_type(path->nodes[0], fi) ==
3672 BTRFS_FILE_EXTENT_INLINE)
3673 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
a2f392e4 3674 &inode->runtime_flags);
7ffbb598
FM
3675 } else {
3676 em->start = hole_offset;
3677 em->len = hole_len;
3678 em->ram_bytes = em->len;
3679 em->orig_start = hole_offset;
3680 em->block_start = EXTENT_MAP_HOLE;
3681 em->block_len = 0;
3682 em->orig_block_len = 0;
3683 em->compress_type = BTRFS_COMPRESS_NONE;
3684 em->generation = trans->transid;
3685 }
3686
3687 while (1) {
3688 write_lock(&em_tree->lock);
3689 ret = add_extent_mapping(em_tree, em, 1);
3690 write_unlock(&em_tree->lock);
3691 if (ret != -EEXIST) {
3692 free_extent_map(em);
3693 break;
3694 }
a2f392e4 3695 btrfs_drop_extent_cache(inode, em->start,
7ffbb598
FM
3696 em->start + em->len - 1, 0);
3697 }
3698
ee39b432 3699 if (ret)
a2f392e4 3700 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
7ffbb598
FM
3701}
3702
8039d87d
FM
3703/*
3704 * Make sure we do not end up inserting an inline extent into a file that has
3705 * already other (non-inline) extents. If a file has an inline extent it can
3706 * not have any other extents and the (single) inline extent must start at the
3707 * file offset 0. Failing to respect these rules will lead to file corruption,
3708 * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc
3709 *
3710 * We can have extents that have been already written to disk or we can have
3711 * dirty ranges still in delalloc, in which case the extent maps and items are
3712 * created only when we run delalloc, and the delalloc ranges might fall outside
3713 * the range we are currently locking in the inode's io tree. So we check the
3714 * inode's i_size because of that (i_size updates are done while holding the
3715 * i_mutex, which we are holding here).
3716 * We also check to see if the inode has a size not greater than "datal" but has
3717 * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are
3718 * protected against such concurrent fallocate calls by the i_mutex).
3719 *
3720 * If the file has no extents but a size greater than datal, do not allow the
3721 * copy because we would need turn the inline extent into a non-inline one (even
3722 * with NO_HOLES enabled). If we find our destination inode only has one inline
3723 * extent, just overwrite it with the source inline extent if its size is less
3724 * than the source extent's size, or we could copy the source inline extent's
3725 * data into the destination inode's inline extent if the later is greater then
3726 * the former.
3727 */
4a0ab9d7 3728static int clone_copy_inline_extent(struct inode *dst,
8039d87d
FM
3729 struct btrfs_trans_handle *trans,
3730 struct btrfs_path *path,
3731 struct btrfs_key *new_key,
3732 const u64 drop_start,
3733 const u64 datal,
3734 const u64 skip,
3735 const u64 size,
3736 char *inline_data)
3737{
0b246afa 3738 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
8039d87d
FM
3739 struct btrfs_root *root = BTRFS_I(dst)->root;
3740 const u64 aligned_end = ALIGN(new_key->offset + datal,
0b246afa 3741 fs_info->sectorsize);
8039d87d
FM
3742 int ret;
3743 struct btrfs_key key;
3744
3745 if (new_key->offset > 0)
3746 return -EOPNOTSUPP;
3747
4a0cc7ca 3748 key.objectid = btrfs_ino(BTRFS_I(dst));
8039d87d
FM
3749 key.type = BTRFS_EXTENT_DATA_KEY;
3750 key.offset = 0;
3751 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3752 if (ret < 0) {
3753 return ret;
3754 } else if (ret > 0) {
3755 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3756 ret = btrfs_next_leaf(root, path);
3757 if (ret < 0)
3758 return ret;
3759 else if (ret > 0)
3760 goto copy_inline_extent;
3761 }
3762 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4a0cc7ca 3763 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
8039d87d
FM
3764 key.type == BTRFS_EXTENT_DATA_KEY) {
3765 ASSERT(key.offset > 0);
3766 return -EOPNOTSUPP;
3767 }
3768 } else if (i_size_read(dst) <= datal) {
3769 struct btrfs_file_extent_item *ei;
3770 u64 ext_len;
3771
3772 /*
3773 * If the file size is <= datal, make sure there are no other
3774 * extents following (can happen do to an fallocate call with
3775 * the flag FALLOC_FL_KEEP_SIZE).
3776 */
3777 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3778 struct btrfs_file_extent_item);
3779 /*
3780 * If it's an inline extent, it can not have other extents
3781 * following it.
3782 */
3783 if (btrfs_file_extent_type(path->nodes[0], ei) ==
3784 BTRFS_FILE_EXTENT_INLINE)
3785 goto copy_inline_extent;
3786
3787 ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3788 if (ext_len > aligned_end)
3789 return -EOPNOTSUPP;
3790
3791 ret = btrfs_next_item(root, path);
3792 if (ret < 0) {
3793 return ret;
3794 } else if (ret == 0) {
3795 btrfs_item_key_to_cpu(path->nodes[0], &key,
3796 path->slots[0]);
4a0cc7ca 3797 if (key.objectid == btrfs_ino(BTRFS_I(dst)) &&
8039d87d
FM
3798 key.type == BTRFS_EXTENT_DATA_KEY)
3799 return -EOPNOTSUPP;
3800 }
3801 }
3802
3803copy_inline_extent:
3804 /*
3805 * We have no extent items, or we have an extent at offset 0 which may
3806 * or may not be inlined. All these cases are dealt the same way.
3807 */
3808 if (i_size_read(dst) > datal) {
3809 /*
3810 * If the destination inode has an inline extent...
3811 * This would require copying the data from the source inline
3812 * extent into the beginning of the destination's inline extent.
3813 * But this is really complex, both extents can be compressed
3814 * or just one of them, which would require decompressing and
3815 * re-compressing data (which could increase the new compressed
3816 * size, not allowing the compressed data to fit anymore in an
3817 * inline extent).
3818 * So just don't support this case for now (it should be rare,
3819 * we are not really saving space when cloning inline extents).
3820 */
3821 return -EOPNOTSUPP;
3822 }
3823
3824 btrfs_release_path(path);
3825 ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1);
3826 if (ret)
3827 return ret;
3828 ret = btrfs_insert_empty_item(trans, root, path, new_key, size);
3829 if (ret)
3830 return ret;
3831
3832 if (skip) {
3833 const u32 start = btrfs_file_extent_calc_inline_size(0);
3834
3835 memmove(inline_data + start, inline_data + start + skip, datal);
3836 }
3837
3838 write_extent_buffer(path->nodes[0], inline_data,
3839 btrfs_item_ptr_offset(path->nodes[0],
3840 path->slots[0]),
3841 size);
3842 inode_add_bytes(dst, datal);
3843
3844 return 0;
3845}
3846
32b7c687
MF
3847/**
3848 * btrfs_clone() - clone a range from inode file to another
3849 *
3850 * @src: Inode to clone from
3851 * @inode: Inode to clone to
3852 * @off: Offset within source to start clone from
3853 * @olen: Original length, passed by user, of range to clone
1c919a5e 3854 * @olen_aligned: Block-aligned value of olen
32b7c687 3855 * @destoff: Offset within @inode to start clone
1c919a5e 3856 * @no_time_update: Whether to update mtime/ctime on the target inode
32b7c687
MF
3857 */
3858static int btrfs_clone(struct inode *src, struct inode *inode,
f82a9901 3859 const u64 off, const u64 olen, const u64 olen_aligned,
1c919a5e 3860 const u64 destoff, int no_time_update)
f46b5a66 3861{
0b246afa 3862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
f46b5a66 3863 struct btrfs_root *root = BTRFS_I(inode)->root;
32b7c687 3864 struct btrfs_path *path = NULL;
f46b5a66 3865 struct extent_buffer *leaf;
32b7c687
MF
3866 struct btrfs_trans_handle *trans;
3867 char *buf = NULL;
ae01a0ab 3868 struct btrfs_key key;
f46b5a66
CH
3869 u32 nritems;
3870 int slot;
ae01a0ab 3871 int ret;
f82a9901 3872 const u64 len = olen_aligned;
f82a9901 3873 u64 last_dest_end = destoff;
ae01a0ab
YZ
3874
3875 ret = -ENOMEM;
752ade68
MH
3876 buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
3877 if (!buf)
3878 return ret;
ae01a0ab
YZ
3879
3880 path = btrfs_alloc_path();
3881 if (!path) {
15351955 3882 kvfree(buf);
32b7c687 3883 return ret;
d525e8ab
LZ
3884 }
3885
e4058b54 3886 path->reada = READA_FORWARD;
c5c9cd4d 3887 /* clone data */
4a0cc7ca 3888 key.objectid = btrfs_ino(BTRFS_I(src));
ae01a0ab 3889 key.type = BTRFS_EXTENT_DATA_KEY;
2c463823 3890 key.offset = off;
f46b5a66
CH
3891
3892 while (1) {
de249e66 3893 u64 next_key_min_offset = key.offset + 1;
df858e76 3894
f46b5a66
CH
3895 /*
3896 * note the key will change type as we walk through the
3897 * tree.
3898 */
e4355f34 3899 path->leave_spinning = 1;
362a20c5
DS
3900 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
3901 0, 0);
f46b5a66
CH
3902 if (ret < 0)
3903 goto out;
2c463823
FM
3904 /*
3905 * First search, if no extent item that starts at offset off was
3906 * found but the previous item is an extent item, it's possible
3907 * it might overlap our target range, therefore process it.
3908 */
3909 if (key.offset == off && ret > 0 && path->slots[0] > 0) {
3910 btrfs_item_key_to_cpu(path->nodes[0], &key,
3911 path->slots[0] - 1);
3912 if (key.type == BTRFS_EXTENT_DATA_KEY)
3913 path->slots[0]--;
3914 }
f46b5a66 3915
ae01a0ab 3916 nritems = btrfs_header_nritems(path->nodes[0]);
e4355f34 3917process_slot:
ae01a0ab 3918 if (path->slots[0] >= nritems) {
362a20c5 3919 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
f46b5a66
CH
3920 if (ret < 0)
3921 goto out;
3922 if (ret > 0)
3923 break;
ae01a0ab 3924 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
3925 }
3926 leaf = path->nodes[0];
3927 slot = path->slots[0];
f46b5a66 3928
ae01a0ab 3929 btrfs_item_key_to_cpu(leaf, &key, slot);
962a298f 3930 if (key.type > BTRFS_EXTENT_DATA_KEY ||
4a0cc7ca 3931 key.objectid != btrfs_ino(BTRFS_I(src)))
f46b5a66
CH
3932 break;
3933
962a298f 3934 if (key.type == BTRFS_EXTENT_DATA_KEY) {
c5c9cd4d
SW
3935 struct btrfs_file_extent_item *extent;
3936 int type;
31840ae1
ZY
3937 u32 size;
3938 struct btrfs_key new_key;
c5c9cd4d
SW
3939 u64 disko = 0, diskl = 0;
3940 u64 datao = 0, datal = 0;
3941 u8 comp;
f82a9901 3942 u64 drop_start;
31840ae1 3943
c5c9cd4d
SW
3944 extent = btrfs_item_ptr(leaf, slot,
3945 struct btrfs_file_extent_item);
3946 comp = btrfs_file_extent_compression(leaf, extent);
3947 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
3948 if (type == BTRFS_FILE_EXTENT_REG ||
3949 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
3950 disko = btrfs_file_extent_disk_bytenr(leaf,
3951 extent);
3952 diskl = btrfs_file_extent_disk_num_bytes(leaf,
3953 extent);
c5c9cd4d 3954 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
3955 datal = btrfs_file_extent_num_bytes(leaf,
3956 extent);
c5c9cd4d
SW
3957 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3958 /* take upper bound, may be compressed */
3959 datal = btrfs_file_extent_ram_bytes(leaf,
3960 extent);
3961 }
31840ae1 3962
2c463823
FM
3963 /*
3964 * The first search might have left us at an extent
3965 * item that ends before our target range's start, can
3966 * happen if we have holes and NO_HOLES feature enabled.
3967 */
3968 if (key.offset + datal <= off) {
e4355f34
FDBM
3969 path->slots[0]++;
3970 goto process_slot;
2c463823
FM
3971 } else if (key.offset >= off + len) {
3972 break;
e4355f34 3973 }
df858e76 3974 next_key_min_offset = key.offset + datal;
e4355f34
FDBM
3975 size = btrfs_item_size_nr(leaf, slot);
3976 read_extent_buffer(leaf, buf,
3977 btrfs_item_ptr_offset(leaf, slot),
3978 size);
3979
3980 btrfs_release_path(path);
3981 path->leave_spinning = 0;
c5c9cd4d 3982
31840ae1 3983 memcpy(&new_key, &key, sizeof(new_key));
4a0cc7ca 3984 new_key.objectid = btrfs_ino(BTRFS_I(inode));
4d728ec7
LZ
3985 if (off <= key.offset)
3986 new_key.offset = key.offset + destoff - off;
3987 else
3988 new_key.offset = destoff;
31840ae1 3989
f82a9901
FM
3990 /*
3991 * Deal with a hole that doesn't have an extent item
3992 * that represents it (NO_HOLES feature enabled).
3993 * This hole is either in the middle of the cloning
3994 * range or at the beginning (fully overlaps it or
3995 * partially overlaps it).
3996 */
3997 if (new_key.offset != last_dest_end)
3998 drop_start = last_dest_end;
3999 else
4000 drop_start = new_key.offset;
4001
b6f3409b
SW
4002 /*
4003 * 1 - adjusting old extent (we may have to split it)
4004 * 1 - add new extent
4005 * 1 - inode update
4006 */
4007 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
4008 if (IS_ERR(trans)) {
4009 ret = PTR_ERR(trans);
4010 goto out;
4011 }
4012
c8a894d7
CM
4013 if (type == BTRFS_FILE_EXTENT_REG ||
4014 type == BTRFS_FILE_EXTENT_PREALLOC) {
d72c0842
LZ
4015 /*
4016 * a | --- range to clone ---| b
4017 * | ------------- extent ------------- |
4018 */
4019
93915584 4020 /* subtract range b */
d72c0842
LZ
4021 if (key.offset + datal > off + len)
4022 datal = off + len - key.offset;
4023
93915584 4024 /* subtract range a */
a22285a6
YZ
4025 if (off > key.offset) {
4026 datao += off - key.offset;
4027 datal -= off - key.offset;
4028 }
4029
5dc562c5 4030 ret = btrfs_drop_extents(trans, root, inode,
f82a9901 4031 drop_start,
a22285a6 4032 new_key.offset + datal,
2671485d 4033 1);
79787eaa 4034 if (ret) {
3f9e3df8 4035 if (ret != -EOPNOTSUPP)
00fdf13a 4036 btrfs_abort_transaction(trans,
66642832 4037 ret);
3a45bb20 4038 btrfs_end_transaction(trans);
79787eaa
JM
4039 goto out;
4040 }
a22285a6 4041
c5c9cd4d
SW
4042 ret = btrfs_insert_empty_item(trans, root, path,
4043 &new_key, size);
79787eaa 4044 if (ret) {
66642832 4045 btrfs_abort_transaction(trans, ret);
3a45bb20 4046 btrfs_end_transaction(trans);
79787eaa
JM
4047 goto out;
4048 }
c5c9cd4d
SW
4049
4050 leaf = path->nodes[0];
4051 slot = path->slots[0];
4052 write_extent_buffer(leaf, buf,
31840ae1
ZY
4053 btrfs_item_ptr_offset(leaf, slot),
4054 size);
ae01a0ab 4055
c5c9cd4d 4056 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 4057 struct btrfs_file_extent_item);
c5c9cd4d 4058
c5c9cd4d
SW
4059 /* disko == 0 means it's a hole */
4060 if (!disko)
4061 datao = 0;
c5c9cd4d
SW
4062
4063 btrfs_set_file_extent_offset(leaf, extent,
4064 datao);
4065 btrfs_set_file_extent_num_bytes(leaf, extent,
4066 datal);
fcebe456 4067
c5c9cd4d
SW
4068 if (disko) {
4069 inode_add_bytes(inode, datal);
2ff7e61e 4070 ret = btrfs_inc_extent_ref(trans,
84f7d8e6 4071 root,
5d4f98a2
YZ
4072 disko, diskl, 0,
4073 root->root_key.objectid,
4a0cc7ca 4074 btrfs_ino(BTRFS_I(inode)),
b06c4bf5 4075 new_key.offset - datao);
79787eaa
JM
4076 if (ret) {
4077 btrfs_abort_transaction(trans,
79787eaa 4078 ret);
3a45bb20 4079 btrfs_end_transaction(trans);
79787eaa
JM
4080 goto out;
4081
4082 }
f46b5a66 4083 }
c5c9cd4d
SW
4084 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
4085 u64 skip = 0;
4086 u64 trim = 0;
ed958762 4087
c5c9cd4d
SW
4088 if (off > key.offset) {
4089 skip = off - key.offset;
4090 new_key.offset += skip;
4091 }
d397712b 4092
aa42ffd9
LB
4093 if (key.offset + datal > off + len)
4094 trim = key.offset + datal - (off + len);
d397712b 4095
c5c9cd4d 4096 if (comp && (skip || trim)) {
c5c9cd4d 4097 ret = -EINVAL;
3a45bb20 4098 btrfs_end_transaction(trans);
c5c9cd4d
SW
4099 goto out;
4100 }
4101 size -= skip + trim;
4102 datal -= skip + trim;
a22285a6 4103
4a0ab9d7 4104 ret = clone_copy_inline_extent(inode,
8039d87d
FM
4105 trans, path,
4106 &new_key,
4107 drop_start,
4108 datal,
4109 skip, size, buf);
79787eaa 4110 if (ret) {
3f9e3df8 4111 if (ret != -EOPNOTSUPP)
3a29bc09 4112 btrfs_abort_transaction(trans,
8039d87d 4113 ret);
3a45bb20 4114 btrfs_end_transaction(trans);
79787eaa
JM
4115 goto out;
4116 }
c5c9cd4d
SW
4117 leaf = path->nodes[0];
4118 slot = path->slots[0];
f46b5a66 4119 }
c5c9cd4d 4120
7ffbb598
FM
4121 /* If we have an implicit hole (NO_HOLES feature). */
4122 if (drop_start < new_key.offset)
a2f392e4 4123 clone_update_extent_map(BTRFS_I(inode), trans,
14f59796 4124 NULL, drop_start,
7ffbb598
FM
4125 new_key.offset - drop_start);
4126
a2f392e4
NB
4127 clone_update_extent_map(BTRFS_I(inode), trans,
4128 path, 0, 0);
7ffbb598 4129
c5c9cd4d 4130 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 4131 btrfs_release_path(path);
c5c9cd4d 4132
62e2390e 4133 last_dest_end = ALIGN(new_key.offset + datal,
0b246afa 4134 fs_info->sectorsize);
f82a9901
FM
4135 ret = clone_finish_inode_update(trans, inode,
4136 last_dest_end,
1c919a5e
MF
4137 destoff, olen,
4138 no_time_update);
f82a9901 4139 if (ret)
79787eaa 4140 goto out;
2c463823
FM
4141 if (new_key.offset + datal >= destoff + len)
4142 break;
a22285a6 4143 }
b3b4aa74 4144 btrfs_release_path(path);
df858e76 4145 key.offset = next_key_min_offset;
69ae5e44
WX
4146
4147 if (fatal_signal_pending(current)) {
4148 ret = -EINTR;
4149 goto out;
4150 }
f46b5a66 4151 }
f46b5a66 4152 ret = 0;
32b7c687 4153
f82a9901
FM
4154 if (last_dest_end < destoff + len) {
4155 /*
4156 * We have an implicit hole (NO_HOLES feature is enabled) that
4157 * fully or partially overlaps our cloning range at its end.
4158 */
4159 btrfs_release_path(path);
4160
4161 /*
4162 * 1 - remove extent(s)
4163 * 1 - inode update
4164 */
4165 trans = btrfs_start_transaction(root, 2);
4166 if (IS_ERR(trans)) {
4167 ret = PTR_ERR(trans);
4168 goto out;
4169 }
4170 ret = btrfs_drop_extents(trans, root, inode,
4171 last_dest_end, destoff + len, 1);
4172 if (ret) {
4173 if (ret != -EOPNOTSUPP)
66642832 4174 btrfs_abort_transaction(trans, ret);
3a45bb20 4175 btrfs_end_transaction(trans);
f82a9901
FM
4176 goto out;
4177 }
a2f392e4
NB
4178 clone_update_extent_map(BTRFS_I(inode), trans, NULL,
4179 last_dest_end,
4180 destoff + len - last_dest_end);
f82a9901 4181 ret = clone_finish_inode_update(trans, inode, destoff + len,
1c919a5e 4182 destoff, olen, no_time_update);
f82a9901
FM
4183 }
4184
f46b5a66 4185out:
32b7c687 4186 btrfs_free_path(path);
15351955 4187 kvfree(buf);
32b7c687
MF
4188 return ret;
4189}
4190
3db11b2e
ZB
4191static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
4192 u64 off, u64 olen, u64 destoff)
32b7c687 4193{
54563d41 4194 struct inode *inode = file_inode(file);
3db11b2e 4195 struct inode *src = file_inode(file_src);
0b246afa 4196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
32b7c687 4197 struct btrfs_root *root = BTRFS_I(inode)->root;
32b7c687
MF
4198 int ret;
4199 u64 len = olen;
0b246afa 4200 u64 bs = fs_info->sb->s_blocksize;
3db11b2e 4201 int same_inode = src == inode;
32b7c687
MF
4202
4203 /*
4204 * TODO:
4205 * - split compressed inline extents. annoying: we need to
4206 * decompress into destination's address_space (the file offset
4207 * may change, so source mapping won't do), then recompress (or
4208 * otherwise reinsert) a subrange.
00fdf13a
LB
4209 *
4210 * - split destination inode's inline extents. The inline extents can
4211 * be either compressed or non-compressed.
32b7c687
MF
4212 */
4213
32b7c687
MF
4214 if (btrfs_root_readonly(root))
4215 return -EROFS;
4216
3db11b2e
ZB
4217 if (file_src->f_path.mnt != file->f_path.mnt ||
4218 src->i_sb != inode->i_sb)
4219 return -EXDEV;
32b7c687 4220
32b7c687 4221 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3db11b2e 4222 return -EISDIR;
32b7c687
MF
4223
4224 if (!same_inode) {
293a8489 4225 btrfs_double_inode_lock(src, inode);
32b7c687 4226 } else {
5955102c 4227 inode_lock(src);
32b7c687
MF
4228 }
4229
b5c40d59
OS
4230 /* don't make the dst file partly checksummed */
4231 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
4232 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
4233 ret = -EINVAL;
4234 goto out_unlock;
4235 }
4236
32b7c687
MF
4237 /* determine range to clone */
4238 ret = -EINVAL;
4239 if (off + len > src->i_size || off + len < off)
4240 goto out_unlock;
4241 if (len == 0)
4242 olen = len = src->i_size - off;
4243 /* if we extend to eof, continue to block boundary */
4244 if (off + len == src->i_size)
4245 len = ALIGN(src->i_size, bs) - off;
4246
ccccf3d6
FM
4247 if (len == 0) {
4248 ret = 0;
4249 goto out_unlock;
4250 }
4251
32b7c687
MF
4252 /* verify the end result is block aligned */
4253 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
4254 !IS_ALIGNED(destoff, bs))
4255 goto out_unlock;
4256
4257 /* verify if ranges are overlapped within the same file */
4258 if (same_inode) {
4259 if (destoff + len > off && destoff < off + len)
4260 goto out_unlock;
4261 }
4262
4263 if (destoff > inode->i_size) {
4264 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
4265 if (ret)
4266 goto out_unlock;
4267 }
4268
c125b8bf
FM
4269 /*
4270 * Lock the target range too. Right after we replace the file extent
4271 * items in the fs tree (which now point to the cloned data), we might
4272 * have a worker replace them with extent items relative to a write
4273 * operation that was issued before this clone operation (i.e. confront
4274 * with inode.c:btrfs_finish_ordered_io).
4275 */
4276 if (same_inode) {
4277 u64 lock_start = min_t(u64, off, destoff);
4278 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
32b7c687 4279
e0bd70c6 4280 ret = lock_extent_range(src, lock_start, lock_len, true);
c125b8bf 4281 } else {
e0bd70c6
FM
4282 ret = btrfs_double_extent_lock(src, off, inode, destoff, len,
4283 true);
4284 }
4285 ASSERT(ret == 0);
4286 if (WARN_ON(ret)) {
4287 /* ranges in the io trees already unlocked */
4288 goto out_unlock;
c125b8bf 4289 }
32b7c687 4290
1c919a5e 4291 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
32b7c687 4292
c125b8bf
FM
4293 if (same_inode) {
4294 u64 lock_start = min_t(u64, off, destoff);
4295 u64 lock_end = max_t(u64, off, destoff) + len - 1;
4296
4297 unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end);
4298 } else {
293a8489 4299 btrfs_double_extent_unlock(src, off, inode, destoff, len);
c125b8bf
FM
4300 }
4301 /*
4302 * Truncate page cache pages so that future reads will see the cloned
4303 * data immediately and not the previous data.
4304 */
65bfa658 4305 truncate_inode_pages_range(&inode->i_data,
09cbfeaf
KS
4306 round_down(destoff, PAGE_SIZE),
4307 round_up(destoff + len, PAGE_SIZE) - 1);
f46b5a66 4308out_unlock:
293a8489
MF
4309 if (!same_inode)
4310 btrfs_double_inode_unlock(src, inode);
4311 else
5955102c 4312 inode_unlock(src);
3db11b2e
ZB
4313 return ret;
4314}
4315
04b38d60
CH
4316int btrfs_clone_file_range(struct file *src_file, loff_t off,
4317 struct file *dst_file, loff_t destoff, u64 len)
3db11b2e 4318{
04b38d60 4319 return btrfs_clone_files(dst_file, src_file, off, len, destoff);
c5c9cd4d
SW
4320}
4321
6ef5ed0d
JB
4322static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4323{
496ad9aa 4324 struct inode *inode = file_inode(file);
0b246afa 4325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6ef5ed0d
JB
4326 struct btrfs_root *root = BTRFS_I(inode)->root;
4327 struct btrfs_root *new_root;
4328 struct btrfs_dir_item *di;
4329 struct btrfs_trans_handle *trans;
4330 struct btrfs_path *path;
4331 struct btrfs_key location;
4332 struct btrfs_disk_key disk_key;
6ef5ed0d
JB
4333 u64 objectid = 0;
4334 u64 dir_id;
3c04ce01 4335 int ret;
6ef5ed0d
JB
4336
4337 if (!capable(CAP_SYS_ADMIN))
4338 return -EPERM;
4339
3c04ce01
MX
4340 ret = mnt_want_write_file(file);
4341 if (ret)
4342 return ret;
4343
4344 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
4345 ret = -EFAULT;
4346 goto out;
4347 }
6ef5ed0d
JB
4348
4349 if (!objectid)
1cecf579 4350 objectid = BTRFS_FS_TREE_OBJECTID;
6ef5ed0d
JB
4351
4352 location.objectid = objectid;
4353 location.type = BTRFS_ROOT_ITEM_KEY;
4354 location.offset = (u64)-1;
4355
0b246afa 4356 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
3c04ce01
MX
4357 if (IS_ERR(new_root)) {
4358 ret = PTR_ERR(new_root);
4359 goto out;
4360 }
6d6d2829 4361 if (!is_fstree(new_root->objectid)) {
4362 ret = -ENOENT;
4363 goto out;
4364 }
6ef5ed0d 4365
6ef5ed0d 4366 path = btrfs_alloc_path();
3c04ce01
MX
4367 if (!path) {
4368 ret = -ENOMEM;
4369 goto out;
4370 }
6ef5ed0d
JB
4371 path->leave_spinning = 1;
4372
4373 trans = btrfs_start_transaction(root, 1);
98d5dc13 4374 if (IS_ERR(trans)) {
6ef5ed0d 4375 btrfs_free_path(path);
3c04ce01
MX
4376 ret = PTR_ERR(trans);
4377 goto out;
6ef5ed0d
JB
4378 }
4379
0b246afa
JM
4380 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4381 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
6ef5ed0d 4382 dir_id, "default", 7, 1);
cf1e99a4 4383 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d 4384 btrfs_free_path(path);
3a45bb20 4385 btrfs_end_transaction(trans);
0b246afa 4386 btrfs_err(fs_info,
5d163e0e 4387 "Umm, you don't have the default diritem, this isn't going to work");
3c04ce01
MX
4388 ret = -ENOENT;
4389 goto out;
6ef5ed0d
JB
4390 }
4391
4392 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
4393 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
4394 btrfs_mark_buffer_dirty(path->nodes[0]);
4395 btrfs_free_path(path);
4396
0b246afa 4397 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
3a45bb20 4398 btrfs_end_transaction(trans);
3c04ce01
MX
4399out:
4400 mnt_drop_write_file(file);
4401 return ret;
6ef5ed0d
JB
4402}
4403
c065f5b1
SY
4404static void get_block_group_info(struct list_head *groups_list,
4405 struct btrfs_ioctl_space_info *space)
bf5fc093
JB
4406{
4407 struct btrfs_block_group_cache *block_group;
4408
4409 space->total_bytes = 0;
4410 space->used_bytes = 0;
4411 space->flags = 0;
4412 list_for_each_entry(block_group, groups_list, list) {
4413 space->flags = block_group->flags;
4414 space->total_bytes += block_group->key.offset;
4415 space->used_bytes +=
4416 btrfs_block_group_used(&block_group->item);
4417 }
4418}
4419
2ff7e61e
JM
4420static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4421 void __user *arg)
1406e432
JB
4422{
4423 struct btrfs_ioctl_space_args space_args;
4424 struct btrfs_ioctl_space_info space;
4425 struct btrfs_ioctl_space_info *dest;
7fde62bf 4426 struct btrfs_ioctl_space_info *dest_orig;
13f2696f 4427 struct btrfs_ioctl_space_info __user *user_dest;
1406e432 4428 struct btrfs_space_info *info;
315d8e98
CIK
4429 static const u64 types[] = {
4430 BTRFS_BLOCK_GROUP_DATA,
4431 BTRFS_BLOCK_GROUP_SYSTEM,
4432 BTRFS_BLOCK_GROUP_METADATA,
4433 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
4434 };
bf5fc093 4435 int num_types = 4;
7fde62bf 4436 int alloc_size;
1406e432 4437 int ret = 0;
51788b1b 4438 u64 slot_count = 0;
bf5fc093 4439 int i, c;
1406e432
JB
4440
4441 if (copy_from_user(&space_args,
4442 (struct btrfs_ioctl_space_args __user *)arg,
4443 sizeof(space_args)))
4444 return -EFAULT;
4445
bf5fc093
JB
4446 for (i = 0; i < num_types; i++) {
4447 struct btrfs_space_info *tmp;
4448
4449 info = NULL;
4450 rcu_read_lock();
0b246afa 4451 list_for_each_entry_rcu(tmp, &fs_info->space_info,
bf5fc093
JB
4452 list) {
4453 if (tmp->flags == types[i]) {
4454 info = tmp;
4455 break;
4456 }
4457 }
4458 rcu_read_unlock();
4459
4460 if (!info)
4461 continue;
4462
4463 down_read(&info->groups_sem);
4464 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4465 if (!list_empty(&info->block_groups[c]))
4466 slot_count++;
4467 }
4468 up_read(&info->groups_sem);
4469 }
7fde62bf 4470
36523e95
DS
4471 /*
4472 * Global block reserve, exported as a space_info
4473 */
4474 slot_count++;
4475
7fde62bf
CM
4476 /* space_slots == 0 means they are asking for a count */
4477 if (space_args.space_slots == 0) {
4478 space_args.total_spaces = slot_count;
4479 goto out;
4480 }
bf5fc093 4481
51788b1b 4482 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 4483
7fde62bf 4484 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 4485
7fde62bf
CM
4486 /* we generally have at most 6 or so space infos, one for each raid
4487 * level. So, a whole page should be more than enough for everyone
4488 */
09cbfeaf 4489 if (alloc_size > PAGE_SIZE)
7fde62bf
CM
4490 return -ENOMEM;
4491
1406e432 4492 space_args.total_spaces = 0;
8d2db785 4493 dest = kmalloc(alloc_size, GFP_KERNEL);
7fde62bf
CM
4494 if (!dest)
4495 return -ENOMEM;
4496 dest_orig = dest;
1406e432 4497
7fde62bf 4498 /* now we have a buffer to copy into */
bf5fc093
JB
4499 for (i = 0; i < num_types; i++) {
4500 struct btrfs_space_info *tmp;
4501
51788b1b
DR
4502 if (!slot_count)
4503 break;
4504
bf5fc093
JB
4505 info = NULL;
4506 rcu_read_lock();
0b246afa 4507 list_for_each_entry_rcu(tmp, &fs_info->space_info,
bf5fc093
JB
4508 list) {
4509 if (tmp->flags == types[i]) {
4510 info = tmp;
4511 break;
4512 }
4513 }
4514 rcu_read_unlock();
7fde62bf 4515
bf5fc093
JB
4516 if (!info)
4517 continue;
4518 down_read(&info->groups_sem);
4519 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
4520 if (!list_empty(&info->block_groups[c])) {
c065f5b1
SY
4521 get_block_group_info(&info->block_groups[c],
4522 &space);
bf5fc093
JB
4523 memcpy(dest, &space, sizeof(space));
4524 dest++;
4525 space_args.total_spaces++;
51788b1b 4526 slot_count--;
bf5fc093 4527 }
51788b1b
DR
4528 if (!slot_count)
4529 break;
bf5fc093
JB
4530 }
4531 up_read(&info->groups_sem);
1406e432 4532 }
1406e432 4533
36523e95
DS
4534 /*
4535 * Add global block reserve
4536 */
4537 if (slot_count) {
0b246afa 4538 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
36523e95
DS
4539
4540 spin_lock(&block_rsv->lock);
4541 space.total_bytes = block_rsv->size;
4542 space.used_bytes = block_rsv->size - block_rsv->reserved;
4543 spin_unlock(&block_rsv->lock);
4544 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
4545 memcpy(dest, &space, sizeof(space));
4546 space_args.total_spaces++;
4547 }
4548
2eec6c81 4549 user_dest = (struct btrfs_ioctl_space_info __user *)
7fde62bf
CM
4550 (arg + sizeof(struct btrfs_ioctl_space_args));
4551
4552 if (copy_to_user(user_dest, dest_orig, alloc_size))
4553 ret = -EFAULT;
4554
4555 kfree(dest_orig);
4556out:
4557 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
4558 ret = -EFAULT;
4559
4560 return ret;
4561}
4562
9a8c28be
MX
4563static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
4564 void __user *argp)
46204592 4565{
46204592
SW
4566 struct btrfs_trans_handle *trans;
4567 u64 transid;
db5b493a 4568 int ret;
46204592 4569
d4edf39b 4570 trans = btrfs_attach_transaction_barrier(root);
ff7c1d33
MX
4571 if (IS_ERR(trans)) {
4572 if (PTR_ERR(trans) != -ENOENT)
4573 return PTR_ERR(trans);
4574
4575 /* No running transaction, don't bother */
4576 transid = root->fs_info->last_trans_committed;
4577 goto out;
4578 }
46204592 4579 transid = trans->transid;
3a45bb20 4580 ret = btrfs_commit_transaction_async(trans, 0);
8b2b2d3c 4581 if (ret) {
3a45bb20 4582 btrfs_end_transaction(trans);
db5b493a 4583 return ret;
8b2b2d3c 4584 }
ff7c1d33 4585out:
46204592
SW
4586 if (argp)
4587 if (copy_to_user(argp, &transid, sizeof(transid)))
4588 return -EFAULT;
4589 return 0;
4590}
4591
2ff7e61e 4592static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
9a8c28be 4593 void __user *argp)
46204592 4594{
46204592
SW
4595 u64 transid;
4596
4597 if (argp) {
4598 if (copy_from_user(&transid, argp, sizeof(transid)))
4599 return -EFAULT;
4600 } else {
4601 transid = 0; /* current trans */
4602 }
2ff7e61e 4603 return btrfs_wait_for_commit(fs_info, transid);
46204592
SW
4604}
4605
b8e95489 4606static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
475f6387 4607{
0b246afa 4608 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
475f6387 4609 struct btrfs_ioctl_scrub_args *sa;
b8e95489 4610 int ret;
475f6387
JS
4611
4612 if (!capable(CAP_SYS_ADMIN))
4613 return -EPERM;
4614
4615 sa = memdup_user(arg, sizeof(*sa));
4616 if (IS_ERR(sa))
4617 return PTR_ERR(sa);
4618
b8e95489
MX
4619 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
4620 ret = mnt_want_write_file(file);
4621 if (ret)
4622 goto out;
4623 }
4624
0b246afa 4625 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
63a212ab
SB
4626 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4627 0);
475f6387
JS
4628
4629 if (copy_to_user(arg, sa, sizeof(*sa)))
4630 ret = -EFAULT;
4631
b8e95489
MX
4632 if (!(sa->flags & BTRFS_SCRUB_READONLY))
4633 mnt_drop_write_file(file);
4634out:
475f6387
JS
4635 kfree(sa);
4636 return ret;
4637}
4638
2ff7e61e 4639static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
475f6387
JS
4640{
4641 if (!capable(CAP_SYS_ADMIN))
4642 return -EPERM;
4643
2ff7e61e 4644 return btrfs_scrub_cancel(fs_info);
475f6387
JS
4645}
4646
2ff7e61e 4647static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
475f6387
JS
4648 void __user *arg)
4649{
4650 struct btrfs_ioctl_scrub_args *sa;
4651 int ret;
4652
4653 if (!capable(CAP_SYS_ADMIN))
4654 return -EPERM;
4655
4656 sa = memdup_user(arg, sizeof(*sa));
4657 if (IS_ERR(sa))
4658 return PTR_ERR(sa);
4659
2ff7e61e 4660 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
475f6387
JS
4661
4662 if (copy_to_user(arg, sa, sizeof(*sa)))
4663 ret = -EFAULT;
4664
4665 kfree(sa);
4666 return ret;
4667}
4668
2ff7e61e 4669static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
b27f7c0c 4670 void __user *arg)
c11d2c23
SB
4671{
4672 struct btrfs_ioctl_get_dev_stats *sa;
4673 int ret;
4674
c11d2c23
SB
4675 sa = memdup_user(arg, sizeof(*sa));
4676 if (IS_ERR(sa))
4677 return PTR_ERR(sa);
4678
b27f7c0c
DS
4679 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
4680 kfree(sa);
4681 return -EPERM;
4682 }
4683
2ff7e61e 4684 ret = btrfs_get_dev_stats(fs_info, sa);
c11d2c23
SB
4685
4686 if (copy_to_user(arg, sa, sizeof(*sa)))
4687 ret = -EFAULT;
4688
4689 kfree(sa);
4690 return ret;
4691}
4692
2ff7e61e
JM
4693static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4694 void __user *arg)
3f6bcfbd
SB
4695{
4696 struct btrfs_ioctl_dev_replace_args *p;
4697 int ret;
4698
4699 if (!capable(CAP_SYS_ADMIN))
4700 return -EPERM;
4701
4702 p = memdup_user(arg, sizeof(*p));
4703 if (IS_ERR(p))
4704 return PTR_ERR(p);
4705
4706 switch (p->cmd) {
4707 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
bc98a42c 4708 if (sb_rdonly(fs_info->sb)) {
adfa97cb
ID
4709 ret = -EROFS;
4710 goto out;
4711 }
171938e5 4712 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
e57138b3 4713 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3f6bcfbd 4714 } else {
2ff7e61e 4715 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
171938e5 4716 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3f6bcfbd
SB
4717 }
4718 break;
4719 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
0b246afa 4720 btrfs_dev_replace_status(fs_info, p);
3f6bcfbd
SB
4721 ret = 0;
4722 break;
4723 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
17d202b9 4724 p->result = btrfs_dev_replace_cancel(fs_info);
97282031 4725 ret = 0;
3f6bcfbd
SB
4726 break;
4727 default:
4728 ret = -EINVAL;
4729 break;
4730 }
4731
4732 if (copy_to_user(arg, p, sizeof(*p)))
4733 ret = -EFAULT;
adfa97cb 4734out:
3f6bcfbd
SB
4735 kfree(p);
4736 return ret;
4737}
4738
d7728c96
JS
4739static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
4740{
4741 int ret = 0;
4742 int i;
740c3d22 4743 u64 rel_ptr;
d7728c96 4744 int size;
806468f8 4745 struct btrfs_ioctl_ino_path_args *ipa = NULL;
d7728c96
JS
4746 struct inode_fs_paths *ipath = NULL;
4747 struct btrfs_path *path;
4748
82b22ac8 4749 if (!capable(CAP_DAC_READ_SEARCH))
d7728c96
JS
4750 return -EPERM;
4751
4752 path = btrfs_alloc_path();
4753 if (!path) {
4754 ret = -ENOMEM;
4755 goto out;
4756 }
4757
4758 ipa = memdup_user(arg, sizeof(*ipa));
4759 if (IS_ERR(ipa)) {
4760 ret = PTR_ERR(ipa);
4761 ipa = NULL;
4762 goto out;
4763 }
4764
4765 size = min_t(u32, ipa->size, 4096);
4766 ipath = init_ipath(size, root, path);
4767 if (IS_ERR(ipath)) {
4768 ret = PTR_ERR(ipath);
4769 ipath = NULL;
4770 goto out;
4771 }
4772
4773 ret = paths_from_inode(ipa->inum, ipath);
4774 if (ret < 0)
4775 goto out;
4776
4777 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
745c4d8e
JM
4778 rel_ptr = ipath->fspath->val[i] -
4779 (u64)(unsigned long)ipath->fspath->val;
740c3d22 4780 ipath->fspath->val[i] = rel_ptr;
d7728c96
JS
4781 }
4782
718dc5fa
OS
4783 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
4784 ipath->fspath, size);
d7728c96
JS
4785 if (ret) {
4786 ret = -EFAULT;
4787 goto out;
4788 }
4789
4790out:
4791 btrfs_free_path(path);
4792 free_ipath(ipath);
4793 kfree(ipa);
4794
4795 return ret;
4796}
4797
4798static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4799{
4800 struct btrfs_data_container *inodes = ctx;
4801 const size_t c = 3 * sizeof(u64);
4802
4803 if (inodes->bytes_left >= c) {
4804 inodes->bytes_left -= c;
4805 inodes->val[inodes->elem_cnt] = inum;
4806 inodes->val[inodes->elem_cnt + 1] = offset;
4807 inodes->val[inodes->elem_cnt + 2] = root;
4808 inodes->elem_cnt += 3;
4809 } else {
4810 inodes->bytes_missing += c - inodes->bytes_left;
4811 inodes->bytes_left = 0;
4812 inodes->elem_missed += 3;
4813 }
4814
4815 return 0;
4816}
4817
2ff7e61e 4818static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
d24a67b2 4819 void __user *arg, int version)
d7728c96
JS
4820{
4821 int ret = 0;
4822 int size;
d7728c96
JS
4823 struct btrfs_ioctl_logical_ino_args *loi;
4824 struct btrfs_data_container *inodes = NULL;
4825 struct btrfs_path *path = NULL;
d24a67b2 4826 bool ignore_offset;
d7728c96
JS
4827
4828 if (!capable(CAP_SYS_ADMIN))
4829 return -EPERM;
4830
4831 loi = memdup_user(arg, sizeof(*loi));
7b9ea627
SV
4832 if (IS_ERR(loi))
4833 return PTR_ERR(loi);
d7728c96 4834
d24a67b2
ZB
4835 if (version == 1) {
4836 ignore_offset = false;
b115e3bc 4837 size = min_t(u32, loi->size, SZ_64K);
d24a67b2
ZB
4838 } else {
4839 /* All reserved bits must be 0 for now */
4840 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
4841 ret = -EINVAL;
4842 goto out_loi;
4843 }
4844 /* Only accept flags we have defined so far */
4845 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
4846 ret = -EINVAL;
4847 goto out_loi;
4848 }
4849 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
b115e3bc 4850 size = min_t(u32, loi->size, SZ_16M);
d24a67b2
ZB
4851 }
4852
d7728c96
JS
4853 path = btrfs_alloc_path();
4854 if (!path) {
4855 ret = -ENOMEM;
4856 goto out;
4857 }
4858
d7728c96
JS
4859 inodes = init_data_container(size);
4860 if (IS_ERR(inodes)) {
4861 ret = PTR_ERR(inodes);
4862 inodes = NULL;
4863 goto out;
4864 }
4865
2ff7e61e 4866 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
d24a67b2 4867 build_ino_list, inodes, ignore_offset);
df031f07 4868 if (ret == -EINVAL)
d7728c96
JS
4869 ret = -ENOENT;
4870 if (ret < 0)
4871 goto out;
4872
718dc5fa
OS
4873 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
4874 size);
d7728c96
JS
4875 if (ret)
4876 ret = -EFAULT;
4877
4878out:
4879 btrfs_free_path(path);
f54de068 4880 kvfree(inodes);
d24a67b2 4881out_loi:
d7728c96
JS
4882 kfree(loi);
4883
4884 return ret;
4885}
4886
008ef096 4887void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
c9e9f97b
ID
4888 struct btrfs_ioctl_balance_args *bargs)
4889{
4890 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4891
4892 bargs->flags = bctl->flags;
4893
3009a62f 4894 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
837d5b6e
ID
4895 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
4896 if (atomic_read(&fs_info->balance_pause_req))
4897 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
a7e99c69
ID
4898 if (atomic_read(&fs_info->balance_cancel_req))
4899 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
837d5b6e 4900
c9e9f97b
ID
4901 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
4902 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
4903 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
19a39dce 4904
008ef096
DS
4905 spin_lock(&fs_info->balance_lock);
4906 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
4907 spin_unlock(&fs_info->balance_lock);
c9e9f97b
ID
4908}
4909
9ba1f6e4 4910static long btrfs_ioctl_balance(struct file *file, void __user *arg)
c9e9f97b 4911{
496ad9aa 4912 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
c9e9f97b
ID
4913 struct btrfs_fs_info *fs_info = root->fs_info;
4914 struct btrfs_ioctl_balance_args *bargs;
4915 struct btrfs_balance_control *bctl;
ed0fb78f 4916 bool need_unlock; /* for mut. excl. ops lock */
c9e9f97b
ID
4917 int ret;
4918
4919 if (!capable(CAP_SYS_ADMIN))
4920 return -EPERM;
4921
e54bfa31 4922 ret = mnt_want_write_file(file);
9ba1f6e4
LB
4923 if (ret)
4924 return ret;
4925
ed0fb78f 4926again:
171938e5 4927 if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
ed0fb78f
ID
4928 mutex_lock(&fs_info->balance_mutex);
4929 need_unlock = true;
4930 goto locked;
4931 }
4932
4933 /*
01327610 4934 * mut. excl. ops lock is locked. Three possibilities:
ed0fb78f
ID
4935 * (1) some other op is running
4936 * (2) balance is running
4937 * (3) balance is paused -- special case (think resume)
4938 */
c9e9f97b 4939 mutex_lock(&fs_info->balance_mutex);
ed0fb78f
ID
4940 if (fs_info->balance_ctl) {
4941 /* this is either (2) or (3) */
3009a62f 4942 if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
ed0fb78f 4943 mutex_unlock(&fs_info->balance_mutex);
dccdb07b
DS
4944 /*
4945 * Lock released to allow other waiters to continue,
4946 * we'll reexamine the status again.
4947 */
ed0fb78f
ID
4948 mutex_lock(&fs_info->balance_mutex);
4949
4950 if (fs_info->balance_ctl &&
3009a62f 4951 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
ed0fb78f
ID
4952 /* this is (3) */
4953 need_unlock = false;
4954 goto locked;
4955 }
4956
4957 mutex_unlock(&fs_info->balance_mutex);
ed0fb78f
ID
4958 goto again;
4959 } else {
4960 /* this is (2) */
4961 mutex_unlock(&fs_info->balance_mutex);
4962 ret = -EINPROGRESS;
4963 goto out;
4964 }
4965 } else {
4966 /* this is (1) */
4967 mutex_unlock(&fs_info->balance_mutex);
e57138b3 4968 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
ed0fb78f
ID
4969 goto out;
4970 }
4971
4972locked:
171938e5 4973 BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
c9e9f97b
ID
4974
4975 if (arg) {
4976 bargs = memdup_user(arg, sizeof(*bargs));
4977 if (IS_ERR(bargs)) {
4978 ret = PTR_ERR(bargs);
ed0fb78f 4979 goto out_unlock;
c9e9f97b 4980 }
de322263
ID
4981
4982 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4983 if (!fs_info->balance_ctl) {
4984 ret = -ENOTCONN;
4985 goto out_bargs;
4986 }
4987
4988 bctl = fs_info->balance_ctl;
4989 spin_lock(&fs_info->balance_lock);
4990 bctl->flags |= BTRFS_BALANCE_RESUME;
4991 spin_unlock(&fs_info->balance_lock);
4992
4993 goto do_balance;
4994 }
c9e9f97b
ID
4995 } else {
4996 bargs = NULL;
4997 }
4998
ed0fb78f 4999 if (fs_info->balance_ctl) {
837d5b6e
ID
5000 ret = -EINPROGRESS;
5001 goto out_bargs;
5002 }
5003
8d2db785 5004 bctl = kzalloc(sizeof(*bctl), GFP_KERNEL);
c9e9f97b
ID
5005 if (!bctl) {
5006 ret = -ENOMEM;
5007 goto out_bargs;
5008 }
5009
c9e9f97b
ID
5010 if (arg) {
5011 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
5012 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
5013 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
5014
5015 bctl->flags = bargs->flags;
f43ffb60
ID
5016 } else {
5017 /* balance everything - no filters */
5018 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
c9e9f97b
ID
5019 }
5020
8eb93459
DS
5021 if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
5022 ret = -EINVAL;
0f89abf5 5023 goto out_bctl;
8eb93459
DS
5024 }
5025
de322263 5026do_balance:
c9e9f97b 5027 /*
149196a2
DS
5028 * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to
5029 * btrfs_balance. bctl is freed in reset_balance_state, or, if
5030 * restriper was paused all the way until unmount, in free_fs_info.
5031 * The flag should be cleared after reset_balance_state.
c9e9f97b 5032 */
ed0fb78f
ID
5033 need_unlock = false;
5034
6fcf6e2b 5035 ret = btrfs_balance(fs_info, bctl, bargs);
0f89abf5 5036 bctl = NULL;
ed0fb78f 5037
c9e9f97b
ID
5038 if (arg) {
5039 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5040 ret = -EFAULT;
5041 }
5042
0f89abf5
CE
5043out_bctl:
5044 kfree(bctl);
c9e9f97b
ID
5045out_bargs:
5046 kfree(bargs);
ed0fb78f 5047out_unlock:
c9e9f97b 5048 mutex_unlock(&fs_info->balance_mutex);
ed0fb78f 5049 if (need_unlock)
171938e5 5050 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
ed0fb78f 5051out:
e54bfa31 5052 mnt_drop_write_file(file);
c9e9f97b
ID
5053 return ret;
5054}
5055
2ff7e61e 5056static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
837d5b6e
ID
5057{
5058 if (!capable(CAP_SYS_ADMIN))
5059 return -EPERM;
5060
5061 switch (cmd) {
5062 case BTRFS_BALANCE_CTL_PAUSE:
0b246afa 5063 return btrfs_pause_balance(fs_info);
a7e99c69 5064 case BTRFS_BALANCE_CTL_CANCEL:
0b246afa 5065 return btrfs_cancel_balance(fs_info);
837d5b6e
ID
5066 }
5067
5068 return -EINVAL;
5069}
5070
2ff7e61e 5071static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
19a39dce
ID
5072 void __user *arg)
5073{
19a39dce
ID
5074 struct btrfs_ioctl_balance_args *bargs;
5075 int ret = 0;
5076
5077 if (!capable(CAP_SYS_ADMIN))
5078 return -EPERM;
5079
5080 mutex_lock(&fs_info->balance_mutex);
5081 if (!fs_info->balance_ctl) {
5082 ret = -ENOTCONN;
5083 goto out;
5084 }
5085
8d2db785 5086 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
19a39dce
ID
5087 if (!bargs) {
5088 ret = -ENOMEM;
5089 goto out;
5090 }
5091
008ef096 5092 btrfs_update_ioctl_balance_args(fs_info, bargs);
19a39dce
ID
5093
5094 if (copy_to_user(arg, bargs, sizeof(*bargs)))
5095 ret = -EFAULT;
5096
5097 kfree(bargs);
5098out:
5099 mutex_unlock(&fs_info->balance_mutex);
5100 return ret;
5101}
5102
905b0dda 5103static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
5d13a37b 5104{
0b246afa
JM
5105 struct inode *inode = file_inode(file);
5106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5d13a37b 5107 struct btrfs_ioctl_quota_ctl_args *sa;
5d13a37b 5108 int ret;
5d13a37b
AJ
5109
5110 if (!capable(CAP_SYS_ADMIN))
5111 return -EPERM;
5112
905b0dda
MX
5113 ret = mnt_want_write_file(file);
5114 if (ret)
5115 return ret;
5d13a37b
AJ
5116
5117 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
5118 if (IS_ERR(sa)) {
5119 ret = PTR_ERR(sa);
5120 goto drop_write;
5121 }
5d13a37b 5122
0b246afa 5123 down_write(&fs_info->subvol_sem);
5d13a37b
AJ
5124
5125 switch (sa->cmd) {
5126 case BTRFS_QUOTA_CTL_ENABLE:
340f1aa2 5127 ret = btrfs_quota_enable(fs_info);
5d13a37b
AJ
5128 break;
5129 case BTRFS_QUOTA_CTL_DISABLE:
340f1aa2 5130 ret = btrfs_quota_disable(fs_info);
5d13a37b 5131 break;
5d13a37b
AJ
5132 default:
5133 ret = -EINVAL;
5134 break;
5135 }
5136
5d13a37b 5137 kfree(sa);
0b246afa 5138 up_write(&fs_info->subvol_sem);
905b0dda
MX
5139drop_write:
5140 mnt_drop_write_file(file);
5d13a37b
AJ
5141 return ret;
5142}
5143
905b0dda 5144static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
5d13a37b 5145{
0b246afa
JM
5146 struct inode *inode = file_inode(file);
5147 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5148 struct btrfs_root *root = BTRFS_I(inode)->root;
5d13a37b
AJ
5149 struct btrfs_ioctl_qgroup_assign_args *sa;
5150 struct btrfs_trans_handle *trans;
5151 int ret;
5152 int err;
5153
5154 if (!capable(CAP_SYS_ADMIN))
5155 return -EPERM;
5156
905b0dda
MX
5157 ret = mnt_want_write_file(file);
5158 if (ret)
5159 return ret;
5d13a37b
AJ
5160
5161 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
5162 if (IS_ERR(sa)) {
5163 ret = PTR_ERR(sa);
5164 goto drop_write;
5165 }
5d13a37b
AJ
5166
5167 trans = btrfs_join_transaction(root);
5168 if (IS_ERR(trans)) {
5169 ret = PTR_ERR(trans);
5170 goto out;
5171 }
5172
5d13a37b 5173 if (sa->assign) {
9f8a6ce6 5174 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst);
5d13a37b 5175 } else {
39616c27 5176 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
5d13a37b
AJ
5177 }
5178
e082f563 5179 /* update qgroup status and info */
0b246afa 5180 err = btrfs_run_qgroups(trans, fs_info);
e082f563 5181 if (err < 0)
0b246afa
JM
5182 btrfs_handle_fs_error(fs_info, err,
5183 "failed to update qgroup status and info");
3a45bb20 5184 err = btrfs_end_transaction(trans);
5d13a37b
AJ
5185 if (err && !ret)
5186 ret = err;
5187
5188out:
5189 kfree(sa);
905b0dda
MX
5190drop_write:
5191 mnt_drop_write_file(file);
5d13a37b
AJ
5192 return ret;
5193}
5194
905b0dda 5195static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
5d13a37b 5196{
0b246afa
JM
5197 struct inode *inode = file_inode(file);
5198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5199 struct btrfs_root *root = BTRFS_I(inode)->root;
5d13a37b
AJ
5200 struct btrfs_ioctl_qgroup_create_args *sa;
5201 struct btrfs_trans_handle *trans;
5202 int ret;
5203 int err;
5204
5205 if (!capable(CAP_SYS_ADMIN))
5206 return -EPERM;
5207
905b0dda
MX
5208 ret = mnt_want_write_file(file);
5209 if (ret)
5210 return ret;
5d13a37b
AJ
5211
5212 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
5213 if (IS_ERR(sa)) {
5214 ret = PTR_ERR(sa);
5215 goto drop_write;
5216 }
5d13a37b 5217
d86e56cf
MX
5218 if (!sa->qgroupid) {
5219 ret = -EINVAL;
5220 goto out;
5221 }
5222
5d13a37b
AJ
5223 trans = btrfs_join_transaction(root);
5224 if (IS_ERR(trans)) {
5225 ret = PTR_ERR(trans);
5226 goto out;
5227 }
5228
5d13a37b 5229 if (sa->create) {
49a05ecd 5230 ret = btrfs_create_qgroup(trans, sa->qgroupid);
5d13a37b 5231 } else {
0b246afa 5232 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
5d13a37b
AJ
5233 }
5234
3a45bb20 5235 err = btrfs_end_transaction(trans);
5d13a37b
AJ
5236 if (err && !ret)
5237 ret = err;
5238
5239out:
5240 kfree(sa);
905b0dda
MX
5241drop_write:
5242 mnt_drop_write_file(file);
5d13a37b
AJ
5243 return ret;
5244}
5245
905b0dda 5246static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
5d13a37b 5247{
0b246afa
JM
5248 struct inode *inode = file_inode(file);
5249 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5250 struct btrfs_root *root = BTRFS_I(inode)->root;
5d13a37b
AJ
5251 struct btrfs_ioctl_qgroup_limit_args *sa;
5252 struct btrfs_trans_handle *trans;
5253 int ret;
5254 int err;
5255 u64 qgroupid;
5256
5257 if (!capable(CAP_SYS_ADMIN))
5258 return -EPERM;
5259
905b0dda
MX
5260 ret = mnt_want_write_file(file);
5261 if (ret)
5262 return ret;
5d13a37b
AJ
5263
5264 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
5265 if (IS_ERR(sa)) {
5266 ret = PTR_ERR(sa);
5267 goto drop_write;
5268 }
5d13a37b
AJ
5269
5270 trans = btrfs_join_transaction(root);
5271 if (IS_ERR(trans)) {
5272 ret = PTR_ERR(trans);
5273 goto out;
5274 }
5275
5276 qgroupid = sa->qgroupid;
5277 if (!qgroupid) {
5278 /* take the current subvol as qgroup */
5279 qgroupid = root->root_key.objectid;
5280 }
5281
0b246afa 5282 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
5d13a37b 5283
3a45bb20 5284 err = btrfs_end_transaction(trans);
5d13a37b
AJ
5285 if (err && !ret)
5286 ret = err;
5287
5288out:
5289 kfree(sa);
905b0dda
MX
5290drop_write:
5291 mnt_drop_write_file(file);
5d13a37b
AJ
5292 return ret;
5293}
5294
2f232036
JS
5295static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5296{
0b246afa
JM
5297 struct inode *inode = file_inode(file);
5298 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2f232036
JS
5299 struct btrfs_ioctl_quota_rescan_args *qsa;
5300 int ret;
5301
5302 if (!capable(CAP_SYS_ADMIN))
5303 return -EPERM;
5304
5305 ret = mnt_want_write_file(file);
5306 if (ret)
5307 return ret;
5308
5309 qsa = memdup_user(arg, sizeof(*qsa));
5310 if (IS_ERR(qsa)) {
5311 ret = PTR_ERR(qsa);
5312 goto drop_write;
5313 }
5314
5315 if (qsa->flags) {
5316 ret = -EINVAL;
5317 goto out;
5318 }
5319
0b246afa 5320 ret = btrfs_qgroup_rescan(fs_info);
2f232036
JS
5321
5322out:
5323 kfree(qsa);
5324drop_write:
5325 mnt_drop_write_file(file);
5326 return ret;
5327}
5328
5329static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5330{
0b246afa
JM
5331 struct inode *inode = file_inode(file);
5332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2f232036
JS
5333 struct btrfs_ioctl_quota_rescan_args *qsa;
5334 int ret = 0;
5335
5336 if (!capable(CAP_SYS_ADMIN))
5337 return -EPERM;
5338
8d2db785 5339 qsa = kzalloc(sizeof(*qsa), GFP_KERNEL);
2f232036
JS
5340 if (!qsa)
5341 return -ENOMEM;
5342
0b246afa 5343 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
2f232036 5344 qsa->flags = 1;
0b246afa 5345 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
2f232036
JS
5346 }
5347
5348 if (copy_to_user(arg, qsa, sizeof(*qsa)))
5349 ret = -EFAULT;
5350
5351 kfree(qsa);
5352 return ret;
5353}
5354
57254b6e
JS
5355static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5356{
0b246afa
JM
5357 struct inode *inode = file_inode(file);
5358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
57254b6e
JS
5359
5360 if (!capable(CAP_SYS_ADMIN))
5361 return -EPERM;
5362
0b246afa 5363 return btrfs_qgroup_wait_for_completion(fs_info, true);
57254b6e
JS
5364}
5365
abccd00f
HM
5366static long _btrfs_ioctl_set_received_subvol(struct file *file,
5367 struct btrfs_ioctl_received_subvol_args *sa)
8ea05e3a 5368{
496ad9aa 5369 struct inode *inode = file_inode(file);
0b246afa 5370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8ea05e3a
AB
5371 struct btrfs_root *root = BTRFS_I(inode)->root;
5372 struct btrfs_root_item *root_item = &root->root_item;
5373 struct btrfs_trans_handle *trans;
95582b00 5374 struct timespec64 ct = current_time(inode);
8ea05e3a 5375 int ret = 0;
dd5f9615 5376 int received_uuid_changed;
8ea05e3a 5377
bd60ea0f
DS
5378 if (!inode_owner_or_capable(inode))
5379 return -EPERM;
5380
8ea05e3a
AB
5381 ret = mnt_want_write_file(file);
5382 if (ret < 0)
5383 return ret;
5384
0b246afa 5385 down_write(&fs_info->subvol_sem);
8ea05e3a 5386
4a0cc7ca 5387 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
8ea05e3a
AB
5388 ret = -EINVAL;
5389 goto out;
5390 }
5391
5392 if (btrfs_root_readonly(root)) {
5393 ret = -EROFS;
5394 goto out;
5395 }
5396
dd5f9615
SB
5397 /*
5398 * 1 - root item
5399 * 2 - uuid items (received uuid + subvol uuid)
5400 */
5401 trans = btrfs_start_transaction(root, 3);
8ea05e3a
AB
5402 if (IS_ERR(trans)) {
5403 ret = PTR_ERR(trans);
5404 trans = NULL;
5405 goto out;
5406 }
5407
5408 sa->rtransid = trans->transid;
5409 sa->rtime.sec = ct.tv_sec;
5410 sa->rtime.nsec = ct.tv_nsec;
5411
dd5f9615
SB
5412 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
5413 BTRFS_UUID_SIZE);
5414 if (received_uuid_changed &&
d87ff758 5415 !btrfs_is_empty_uuid(root_item->received_uuid)) {
d1957791 5416 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
d87ff758
NB
5417 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5418 root->root_key.objectid);
5419 if (ret && ret != -ENOENT) {
5420 btrfs_abort_transaction(trans, ret);
5421 btrfs_end_transaction(trans);
5422 goto out;
5423 }
5424 }
8ea05e3a
AB
5425 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
5426 btrfs_set_root_stransid(root_item, sa->stransid);
5427 btrfs_set_root_rtransid(root_item, sa->rtransid);
3cae210f
QW
5428 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
5429 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
5430 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5431 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
8ea05e3a 5432
0b246afa 5433 ret = btrfs_update_root(trans, fs_info->tree_root,
8ea05e3a
AB
5434 &root->root_key, &root->root_item);
5435 if (ret < 0) {
3a45bb20 5436 btrfs_end_transaction(trans);
8ea05e3a 5437 goto out;
dd5f9615
SB
5438 }
5439 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
cdb345a8 5440 ret = btrfs_uuid_tree_add(trans, sa->uuid,
dd5f9615
SB
5441 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5442 root->root_key.objectid);
5443 if (ret < 0 && ret != -EEXIST) {
66642832 5444 btrfs_abort_transaction(trans, ret);
efd38150 5445 btrfs_end_transaction(trans);
8ea05e3a 5446 goto out;
dd5f9615
SB
5447 }
5448 }
3a45bb20 5449 ret = btrfs_commit_transaction(trans);
abccd00f 5450out:
0b246afa 5451 up_write(&fs_info->subvol_sem);
abccd00f
HM
5452 mnt_drop_write_file(file);
5453 return ret;
5454}
5455
5456#ifdef CONFIG_64BIT
5457static long btrfs_ioctl_set_received_subvol_32(struct file *file,
5458 void __user *arg)
5459{
5460 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
5461 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
5462 int ret = 0;
5463
5464 args32 = memdup_user(arg, sizeof(*args32));
7b9ea627
SV
5465 if (IS_ERR(args32))
5466 return PTR_ERR(args32);
abccd00f 5467
8d2db785 5468 args64 = kmalloc(sizeof(*args64), GFP_KERNEL);
84dbeb87
DC
5469 if (!args64) {
5470 ret = -ENOMEM;
abccd00f
HM
5471 goto out;
5472 }
5473
5474 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
5475 args64->stransid = args32->stransid;
5476 args64->rtransid = args32->rtransid;
5477 args64->stime.sec = args32->stime.sec;
5478 args64->stime.nsec = args32->stime.nsec;
5479 args64->rtime.sec = args32->rtime.sec;
5480 args64->rtime.nsec = args32->rtime.nsec;
5481 args64->flags = args32->flags;
5482
5483 ret = _btrfs_ioctl_set_received_subvol(file, args64);
5484 if (ret)
5485 goto out;
5486
5487 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
5488 args32->stransid = args64->stransid;
5489 args32->rtransid = args64->rtransid;
5490 args32->stime.sec = args64->stime.sec;
5491 args32->stime.nsec = args64->stime.nsec;
5492 args32->rtime.sec = args64->rtime.sec;
5493 args32->rtime.nsec = args64->rtime.nsec;
5494 args32->flags = args64->flags;
5495
5496 ret = copy_to_user(arg, args32, sizeof(*args32));
5497 if (ret)
5498 ret = -EFAULT;
5499
5500out:
5501 kfree(args32);
5502 kfree(args64);
5503 return ret;
5504}
5505#endif
5506
5507static long btrfs_ioctl_set_received_subvol(struct file *file,
5508 void __user *arg)
5509{
5510 struct btrfs_ioctl_received_subvol_args *sa = NULL;
5511 int ret = 0;
5512
5513 sa = memdup_user(arg, sizeof(*sa));
7b9ea627
SV
5514 if (IS_ERR(sa))
5515 return PTR_ERR(sa);
abccd00f
HM
5516
5517 ret = _btrfs_ioctl_set_received_subvol(file, sa);
5518
5519 if (ret)
5520 goto out;
5521
8ea05e3a
AB
5522 ret = copy_to_user(arg, sa, sizeof(*sa));
5523 if (ret)
5524 ret = -EFAULT;
5525
5526out:
5527 kfree(sa);
8ea05e3a
AB
5528 return ret;
5529}
5530
867ab667 5531static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5532{
0b246afa
JM
5533 struct inode *inode = file_inode(file);
5534 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
a1b83ac5 5535 size_t len;
867ab667 5536 int ret;
a1b83ac5
AJ
5537 char label[BTRFS_LABEL_SIZE];
5538
0b246afa
JM
5539 spin_lock(&fs_info->super_lock);
5540 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5541 spin_unlock(&fs_info->super_lock);
a1b83ac5
AJ
5542
5543 len = strnlen(label, BTRFS_LABEL_SIZE);
867ab667 5544
5545 if (len == BTRFS_LABEL_SIZE) {
0b246afa
JM
5546 btrfs_warn(fs_info,
5547 "label is too long, return the first %zu bytes",
5548 --len);
867ab667 5549 }
5550
867ab667 5551 ret = copy_to_user(arg, label, len);
867ab667 5552
5553 return ret ? -EFAULT : 0;
5554}
5555
a8bfd4ab 5556static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5557{
0b246afa
JM
5558 struct inode *inode = file_inode(file);
5559 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5560 struct btrfs_root *root = BTRFS_I(inode)->root;
5561 struct btrfs_super_block *super_block = fs_info->super_copy;
a8bfd4ab 5562 struct btrfs_trans_handle *trans;
5563 char label[BTRFS_LABEL_SIZE];
5564 int ret;
5565
5566 if (!capable(CAP_SYS_ADMIN))
5567 return -EPERM;
5568
5569 if (copy_from_user(label, arg, sizeof(label)))
5570 return -EFAULT;
5571
5572 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
0b246afa 5573 btrfs_err(fs_info,
5d163e0e
JM
5574 "unable to set label with more than %d bytes",
5575 BTRFS_LABEL_SIZE - 1);
a8bfd4ab 5576 return -EINVAL;
5577 }
5578
5579 ret = mnt_want_write_file(file);
5580 if (ret)
5581 return ret;
5582
a8bfd4ab 5583 trans = btrfs_start_transaction(root, 0);
5584 if (IS_ERR(trans)) {
5585 ret = PTR_ERR(trans);
5586 goto out_unlock;
5587 }
5588
0b246afa 5589 spin_lock(&fs_info->super_lock);
a8bfd4ab 5590 strcpy(super_block->label, label);
0b246afa 5591 spin_unlock(&fs_info->super_lock);
3a45bb20 5592 ret = btrfs_commit_transaction(trans);
a8bfd4ab 5593
5594out_unlock:
a8bfd4ab 5595 mnt_drop_write_file(file);
5596 return ret;
5597}
5598
2eaa055f
JM
5599#define INIT_FEATURE_FLAGS(suffix) \
5600 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
5601 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
5602 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
5603
d5131b65 5604int btrfs_ioctl_get_supported_features(void __user *arg)
2eaa055f 5605{
4d4ab6d6 5606 static const struct btrfs_ioctl_feature_flags features[3] = {
2eaa055f
JM
5607 INIT_FEATURE_FLAGS(SUPP),
5608 INIT_FEATURE_FLAGS(SAFE_SET),
5609 INIT_FEATURE_FLAGS(SAFE_CLEAR)
5610 };
5611
5612 if (copy_to_user(arg, &features, sizeof(features)))
5613 return -EFAULT;
5614
5615 return 0;
5616}
5617
5618static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5619{
0b246afa
JM
5620 struct inode *inode = file_inode(file);
5621 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5622 struct btrfs_super_block *super_block = fs_info->super_copy;
2eaa055f
JM
5623 struct btrfs_ioctl_feature_flags features;
5624
5625 features.compat_flags = btrfs_super_compat_flags(super_block);
5626 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
5627 features.incompat_flags = btrfs_super_incompat_flags(super_block);
5628
5629 if (copy_to_user(arg, &features, sizeof(features)))
5630 return -EFAULT;
5631
5632 return 0;
5633}
5634
2ff7e61e 5635static int check_feature_bits(struct btrfs_fs_info *fs_info,
3b02a68a 5636 enum btrfs_feature_set set,
2eaa055f
JM
5637 u64 change_mask, u64 flags, u64 supported_flags,
5638 u64 safe_set, u64 safe_clear)
5639{
3b02a68a
JM
5640 const char *type = btrfs_feature_set_names[set];
5641 char *names;
2eaa055f
JM
5642 u64 disallowed, unsupported;
5643 u64 set_mask = flags & change_mask;
5644 u64 clear_mask = ~flags & change_mask;
5645
5646 unsupported = set_mask & ~supported_flags;
5647 if (unsupported) {
3b02a68a
JM
5648 names = btrfs_printable_features(set, unsupported);
5649 if (names) {
0b246afa
JM
5650 btrfs_warn(fs_info,
5651 "this kernel does not support the %s feature bit%s",
5652 names, strchr(names, ',') ? "s" : "");
3b02a68a
JM
5653 kfree(names);
5654 } else
0b246afa
JM
5655 btrfs_warn(fs_info,
5656 "this kernel does not support %s bits 0x%llx",
5657 type, unsupported);
2eaa055f
JM
5658 return -EOPNOTSUPP;
5659 }
5660
5661 disallowed = set_mask & ~safe_set;
5662 if (disallowed) {
3b02a68a
JM
5663 names = btrfs_printable_features(set, disallowed);
5664 if (names) {
0b246afa
JM
5665 btrfs_warn(fs_info,
5666 "can't set the %s feature bit%s while mounted",
5667 names, strchr(names, ',') ? "s" : "");
3b02a68a
JM
5668 kfree(names);
5669 } else
0b246afa
JM
5670 btrfs_warn(fs_info,
5671 "can't set %s bits 0x%llx while mounted",
5672 type, disallowed);
2eaa055f
JM
5673 return -EPERM;
5674 }
5675
5676 disallowed = clear_mask & ~safe_clear;
5677 if (disallowed) {
3b02a68a
JM
5678 names = btrfs_printable_features(set, disallowed);
5679 if (names) {
0b246afa
JM
5680 btrfs_warn(fs_info,
5681 "can't clear the %s feature bit%s while mounted",
5682 names, strchr(names, ',') ? "s" : "");
3b02a68a
JM
5683 kfree(names);
5684 } else
0b246afa
JM
5685 btrfs_warn(fs_info,
5686 "can't clear %s bits 0x%llx while mounted",
5687 type, disallowed);
2eaa055f
JM
5688 return -EPERM;
5689 }
5690
5691 return 0;
5692}
5693
2ff7e61e
JM
5694#define check_feature(fs_info, change_mask, flags, mask_base) \
5695check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
2eaa055f
JM
5696 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5697 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5698 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
5699
5700static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5701{
0b246afa
JM
5702 struct inode *inode = file_inode(file);
5703 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5704 struct btrfs_root *root = BTRFS_I(inode)->root;
5705 struct btrfs_super_block *super_block = fs_info->super_copy;
2eaa055f
JM
5706 struct btrfs_ioctl_feature_flags flags[2];
5707 struct btrfs_trans_handle *trans;
5708 u64 newflags;
5709 int ret;
5710
5711 if (!capable(CAP_SYS_ADMIN))
5712 return -EPERM;
5713
5714 if (copy_from_user(flags, arg, sizeof(flags)))
5715 return -EFAULT;
5716
5717 /* Nothing to do */
5718 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
5719 !flags[0].incompat_flags)
5720 return 0;
5721
2ff7e61e 5722 ret = check_feature(fs_info, flags[0].compat_flags,
2eaa055f
JM
5723 flags[1].compat_flags, COMPAT);
5724 if (ret)
5725 return ret;
5726
2ff7e61e 5727 ret = check_feature(fs_info, flags[0].compat_ro_flags,
2eaa055f
JM
5728 flags[1].compat_ro_flags, COMPAT_RO);
5729 if (ret)
5730 return ret;
5731
2ff7e61e 5732 ret = check_feature(fs_info, flags[0].incompat_flags,
2eaa055f
JM
5733 flags[1].incompat_flags, INCOMPAT);
5734 if (ret)
5735 return ret;
5736
7ab19625
DS
5737 ret = mnt_want_write_file(file);
5738 if (ret)
5739 return ret;
5740
8051aa1a 5741 trans = btrfs_start_transaction(root, 0);
7ab19625
DS
5742 if (IS_ERR(trans)) {
5743 ret = PTR_ERR(trans);
5744 goto out_drop_write;
5745 }
2eaa055f 5746
0b246afa 5747 spin_lock(&fs_info->super_lock);
2eaa055f
JM
5748 newflags = btrfs_super_compat_flags(super_block);
5749 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5750 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
5751 btrfs_set_super_compat_flags(super_block, newflags);
5752
5753 newflags = btrfs_super_compat_ro_flags(super_block);
5754 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
5755 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
5756 btrfs_set_super_compat_ro_flags(super_block, newflags);
5757
5758 newflags = btrfs_super_incompat_flags(super_block);
5759 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5760 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5761 btrfs_set_super_incompat_flags(super_block, newflags);
0b246afa 5762 spin_unlock(&fs_info->super_lock);
2eaa055f 5763
3a45bb20 5764 ret = btrfs_commit_transaction(trans);
7ab19625
DS
5765out_drop_write:
5766 mnt_drop_write_file(file);
5767
5768 return ret;
2eaa055f
JM
5769}
5770
2351f431
JB
5771static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat)
5772{
5773 struct btrfs_ioctl_send_args *arg;
5774 int ret;
5775
5776 if (compat) {
5777#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5778 struct btrfs_ioctl_send_args_32 args32;
5779
5780 ret = copy_from_user(&args32, argp, sizeof(args32));
5781 if (ret)
5782 return -EFAULT;
5783 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5784 if (!arg)
5785 return -ENOMEM;
5786 arg->send_fd = args32.send_fd;
5787 arg->clone_sources_count = args32.clone_sources_count;
5788 arg->clone_sources = compat_ptr(args32.clone_sources);
5789 arg->parent_root = args32.parent_root;
5790 arg->flags = args32.flags;
5791 memcpy(arg->reserved, args32.reserved,
5792 sizeof(args32.reserved));
5793#else
5794 return -ENOTTY;
5795#endif
5796 } else {
5797 arg = memdup_user(argp, sizeof(*arg));
5798 if (IS_ERR(arg))
5799 return PTR_ERR(arg);
5800 }
5801 ret = btrfs_ioctl_send(file, arg);
5802 kfree(arg);
5803 return ret;
5804}
5805
f46b5a66
CH
5806long btrfs_ioctl(struct file *file, unsigned int
5807 cmd, unsigned long arg)
5808{
0b246afa
JM
5809 struct inode *inode = file_inode(file);
5810 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5811 struct btrfs_root *root = BTRFS_I(inode)->root;
4bcabaa3 5812 void __user *argp = (void __user *)arg;
f46b5a66
CH
5813
5814 switch (cmd) {
6cbff00f
CH
5815 case FS_IOC_GETFLAGS:
5816 return btrfs_ioctl_getflags(file, argp);
5817 case FS_IOC_SETFLAGS:
5818 return btrfs_ioctl_setflags(file, argp);
5819 case FS_IOC_GETVERSION:
5820 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
5821 case FITRIM:
5822 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 5823 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 5824 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 5825 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 5826 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 5827 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 5828 return btrfs_ioctl_snap_create(file, argp, 1);
6f72c7e2
AJ
5829 case BTRFS_IOC_SUBVOL_CREATE_V2:
5830 return btrfs_ioctl_snap_create_v2(file, argp, 1);
76dda93c
YZ
5831 case BTRFS_IOC_SNAP_DESTROY:
5832 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
5833 case BTRFS_IOC_SUBVOL_GETFLAGS:
5834 return btrfs_ioctl_subvol_getflags(file, argp);
5835 case BTRFS_IOC_SUBVOL_SETFLAGS:
5836 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
5837 case BTRFS_IOC_DEFAULT_SUBVOL:
5838 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 5839 case BTRFS_IOC_DEFRAG:
1e701a32
CM
5840 return btrfs_ioctl_defrag(file, NULL);
5841 case BTRFS_IOC_DEFRAG_RANGE:
5842 return btrfs_ioctl_defrag(file, argp);
f46b5a66 5843 case BTRFS_IOC_RESIZE:
198605a8 5844 return btrfs_ioctl_resize(file, argp);
f46b5a66 5845 case BTRFS_IOC_ADD_DEV:
2ff7e61e 5846 return btrfs_ioctl_add_dev(fs_info, argp);
f46b5a66 5847 case BTRFS_IOC_RM_DEV:
da24927b 5848 return btrfs_ioctl_rm_dev(file, argp);
6b526ed7
AJ
5849 case BTRFS_IOC_RM_DEV_V2:
5850 return btrfs_ioctl_rm_dev_v2(file, argp);
475f6387 5851 case BTRFS_IOC_FS_INFO:
2ff7e61e 5852 return btrfs_ioctl_fs_info(fs_info, argp);
475f6387 5853 case BTRFS_IOC_DEV_INFO:
2ff7e61e 5854 return btrfs_ioctl_dev_info(fs_info, argp);
f46b5a66 5855 case BTRFS_IOC_BALANCE:
9ba1f6e4 5856 return btrfs_ioctl_balance(file, NULL);
ac8e9819
CM
5857 case BTRFS_IOC_TREE_SEARCH:
5858 return btrfs_ioctl_tree_search(file, argp);
cc68a8a5
GH
5859 case BTRFS_IOC_TREE_SEARCH_V2:
5860 return btrfs_ioctl_tree_search_v2(file, argp);
ac8e9819
CM
5861 case BTRFS_IOC_INO_LOOKUP:
5862 return btrfs_ioctl_ino_lookup(file, argp);
d7728c96
JS
5863 case BTRFS_IOC_INO_PATHS:
5864 return btrfs_ioctl_ino_to_path(root, argp);
5865 case BTRFS_IOC_LOGICAL_INO:
d24a67b2
ZB
5866 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5867 case BTRFS_IOC_LOGICAL_INO_V2:
5868 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
1406e432 5869 case BTRFS_IOC_SPACE_INFO:
2ff7e61e 5870 return btrfs_ioctl_space_info(fs_info, argp);
9b199859
FDBM
5871 case BTRFS_IOC_SYNC: {
5872 int ret;
5873
82b3e53b 5874 ret = btrfs_start_delalloc_roots(fs_info, -1);
9b199859
FDBM
5875 if (ret)
5876 return ret;
0b246afa 5877 ret = btrfs_sync_fs(inode->i_sb, 1);
2fad4e83
DS
5878 /*
5879 * The transaction thread may want to do more work,
01327610 5880 * namely it pokes the cleaner kthread that will start
2fad4e83
DS
5881 * processing uncleaned subvols.
5882 */
0b246afa 5883 wake_up_process(fs_info->transaction_kthread);
9b199859
FDBM
5884 return ret;
5885 }
46204592 5886 case BTRFS_IOC_START_SYNC:
9a8c28be 5887 return btrfs_ioctl_start_sync(root, argp);
46204592 5888 case BTRFS_IOC_WAIT_SYNC:
2ff7e61e 5889 return btrfs_ioctl_wait_sync(fs_info, argp);
475f6387 5890 case BTRFS_IOC_SCRUB:
b8e95489 5891 return btrfs_ioctl_scrub(file, argp);
475f6387 5892 case BTRFS_IOC_SCRUB_CANCEL:
2ff7e61e 5893 return btrfs_ioctl_scrub_cancel(fs_info);
475f6387 5894 case BTRFS_IOC_SCRUB_PROGRESS:
2ff7e61e 5895 return btrfs_ioctl_scrub_progress(fs_info, argp);
c9e9f97b 5896 case BTRFS_IOC_BALANCE_V2:
9ba1f6e4 5897 return btrfs_ioctl_balance(file, argp);
837d5b6e 5898 case BTRFS_IOC_BALANCE_CTL:
2ff7e61e 5899 return btrfs_ioctl_balance_ctl(fs_info, arg);
19a39dce 5900 case BTRFS_IOC_BALANCE_PROGRESS:
2ff7e61e 5901 return btrfs_ioctl_balance_progress(fs_info, argp);
8ea05e3a
AB
5902 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5903 return btrfs_ioctl_set_received_subvol(file, argp);
abccd00f
HM
5904#ifdef CONFIG_64BIT
5905 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5906 return btrfs_ioctl_set_received_subvol_32(file, argp);
5907#endif
31db9f7c 5908 case BTRFS_IOC_SEND:
2351f431
JB
5909 return _btrfs_ioctl_send(file, argp, false);
5910#if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5911 case BTRFS_IOC_SEND_32:
5912 return _btrfs_ioctl_send(file, argp, true);
5913#endif
c11d2c23 5914 case BTRFS_IOC_GET_DEV_STATS:
2ff7e61e 5915 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5d13a37b 5916 case BTRFS_IOC_QUOTA_CTL:
905b0dda 5917 return btrfs_ioctl_quota_ctl(file, argp);
5d13a37b 5918 case BTRFS_IOC_QGROUP_ASSIGN:
905b0dda 5919 return btrfs_ioctl_qgroup_assign(file, argp);
5d13a37b 5920 case BTRFS_IOC_QGROUP_CREATE:
905b0dda 5921 return btrfs_ioctl_qgroup_create(file, argp);
5d13a37b 5922 case BTRFS_IOC_QGROUP_LIMIT:
905b0dda 5923 return btrfs_ioctl_qgroup_limit(file, argp);
2f232036
JS
5924 case BTRFS_IOC_QUOTA_RESCAN:
5925 return btrfs_ioctl_quota_rescan(file, argp);
5926 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5927 return btrfs_ioctl_quota_rescan_status(file, argp);
57254b6e
JS
5928 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5929 return btrfs_ioctl_quota_rescan_wait(file, argp);
3f6bcfbd 5930 case BTRFS_IOC_DEV_REPLACE:
2ff7e61e 5931 return btrfs_ioctl_dev_replace(fs_info, argp);
867ab667 5932 case BTRFS_IOC_GET_FSLABEL:
5933 return btrfs_ioctl_get_fslabel(file, argp);
a8bfd4ab 5934 case BTRFS_IOC_SET_FSLABEL:
5935 return btrfs_ioctl_set_fslabel(file, argp);
2eaa055f 5936 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
d5131b65 5937 return btrfs_ioctl_get_supported_features(argp);
2eaa055f
JM
5938 case BTRFS_IOC_GET_FEATURES:
5939 return btrfs_ioctl_get_features(file, argp);
5940 case BTRFS_IOC_SET_FEATURES:
5941 return btrfs_ioctl_set_features(file, argp);
e4202ac9
DS
5942 case FS_IOC_FSGETXATTR:
5943 return btrfs_ioctl_fsgetxattr(file, argp);
025f2121
DS
5944 case FS_IOC_FSSETXATTR:
5945 return btrfs_ioctl_fssetxattr(file, argp);
b64ec075
TM
5946 case BTRFS_IOC_GET_SUBVOL_INFO:
5947 return btrfs_ioctl_get_subvol_info(file, argp);
42e4b520
TM
5948 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5949 return btrfs_ioctl_get_subvol_rootref(file, argp);
23d0b79d
TM
5950 case BTRFS_IOC_INO_LOOKUP_USER:
5951 return btrfs_ioctl_ino_lookup_user(file, argp);
f46b5a66
CH
5952 }
5953
5954 return -ENOTTY;
5955}
4c63c245
LD
5956
5957#ifdef CONFIG_COMPAT
5958long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5959{
2a362249
JM
5960 /*
5961 * These all access 32-bit values anyway so no further
5962 * handling is necessary.
5963 */
4c63c245
LD
5964 switch (cmd) {
5965 case FS_IOC32_GETFLAGS:
5966 cmd = FS_IOC_GETFLAGS;
5967 break;
5968 case FS_IOC32_SETFLAGS:
5969 cmd = FS_IOC_SETFLAGS;
5970 break;
5971 case FS_IOC32_GETVERSION:
5972 cmd = FS_IOC_GETVERSION;
5973 break;
4c63c245
LD
5974 }
5975
5976 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5977}
5978#endif