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