1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/bio.h>
8 #include <linux/file.h>
9 #include <linux/fs.h>
10 #include <linux/fsnotify.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/security.h>
21 #include <linux/xattr.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/blkdev.h>
25 #include <linux/uuid.h>
26 #include <linux/btrfs.h>
27 #include <linux/uaccess.h>
28 #include <linux/iversion.h>
29 #include <linux/fileattr.h>
30 #include <linux/fsverity.h>
31 #include <linux/sched/xacct.h>
32 #include <linux/io_uring/cmd.h>
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "export.h"
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "volumes.h"
39 #include "locking.h"
40 #include "backref.h"
41 #include "send.h"
42 #include "dev-replace.h"
43 #include "props.h"
44 #include "sysfs.h"
45 #include "qgroup.h"
46 #include "tree-log.h"
47 #include "compression.h"
48 #include "space-info.h"
49 #include "block-group.h"
50 #include "fs.h"
51 #include "accessors.h"
52 #include "extent-tree.h"
53 #include "root-tree.h"
54 #include "defrag.h"
55 #include "dir-item.h"
56 #include "uuid-tree.h"
57 #include "ioctl.h"
58 #include "file.h"
59 #include "scrub.h"
60 #include "super.h"
61
62 #ifdef CONFIG_64BIT
63 /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
64 * structures are incorrect, as the timespec structure from userspace
65 * is 4 bytes too small. We define these alternatives here to teach
66 * the kernel about the 32-bit struct packing.
67 */
68 struct btrfs_ioctl_timespec_32 {
69 __u64 sec;
70 __u32 nsec;
71 } __attribute__ ((__packed__));
72
73 struct btrfs_ioctl_received_subvol_args_32 {
74 char uuid[BTRFS_UUID_SIZE]; /* in */
75 __u64 stransid; /* in */
76 __u64 rtransid; /* out */
77 struct btrfs_ioctl_timespec_32 stime; /* in */
78 struct btrfs_ioctl_timespec_32 rtime; /* out */
79 __u64 flags; /* in */
80 __u64 reserved[16]; /* in */
81 } __attribute__ ((__packed__));
82
83 #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
84 struct btrfs_ioctl_received_subvol_args_32)
85 #endif
86
87 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
88 struct btrfs_ioctl_send_args_32 {
89 __s64 send_fd; /* in */
90 __u64 clone_sources_count; /* in */
91 compat_uptr_t clone_sources; /* in */
92 __u64 parent_root; /* in */
93 __u64 flags; /* in */
94 __u32 version; /* in */
95 __u8 reserved[28]; /* in */
96 } __attribute__ ((__packed__));
97
98 #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \
99 struct btrfs_ioctl_send_args_32)
100
101 struct btrfs_ioctl_encoded_io_args_32 {
102 compat_uptr_t iov;
103 compat_ulong_t iovcnt;
104 __s64 offset;
105 __u64 flags;
106 __u64 len;
107 __u64 unencoded_len;
108 __u64 unencoded_offset;
109 __u32 compression;
110 __u32 encryption;
111 __u8 reserved[64];
112 };
113
114 #define BTRFS_IOC_ENCODED_READ_32 _IOR(BTRFS_IOCTL_MAGIC, 64, \
115 struct btrfs_ioctl_encoded_io_args_32)
116 #define BTRFS_IOC_ENCODED_WRITE_32 _IOW(BTRFS_IOCTL_MAGIC, 64, \
117 struct btrfs_ioctl_encoded_io_args_32)
118 #endif
119
120 /* Mask out flags that are inappropriate for the given type of inode. */
btrfs_mask_fsflags_for_type(const struct inode * inode,unsigned int flags)121 static unsigned int btrfs_mask_fsflags_for_type(const struct inode *inode,
122 unsigned int flags)
123 {
124 if (S_ISDIR(inode->i_mode))
125 return flags;
126 else if (S_ISREG(inode->i_mode))
127 return flags & ~FS_DIRSYNC_FL;
128 else
129 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
130 }
131
132 /*
133 * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS
134 * ioctl.
135 */
btrfs_inode_flags_to_fsflags(const struct btrfs_inode * inode)136 static unsigned int btrfs_inode_flags_to_fsflags(const struct btrfs_inode *inode)
137 {
138 unsigned int iflags = 0;
139 u32 flags = inode->flags;
140 u32 ro_flags = inode->ro_flags;
141
142 if (flags & BTRFS_INODE_SYNC)
143 iflags |= FS_SYNC_FL;
144 if (flags & BTRFS_INODE_IMMUTABLE)
145 iflags |= FS_IMMUTABLE_FL;
146 if (flags & BTRFS_INODE_APPEND)
147 iflags |= FS_APPEND_FL;
148 if (flags & BTRFS_INODE_NODUMP)
149 iflags |= FS_NODUMP_FL;
150 if (flags & BTRFS_INODE_NOATIME)
151 iflags |= FS_NOATIME_FL;
152 if (flags & BTRFS_INODE_DIRSYNC)
153 iflags |= FS_DIRSYNC_FL;
154 if (flags & BTRFS_INODE_NODATACOW)
155 iflags |= FS_NOCOW_FL;
156 if (ro_flags & BTRFS_INODE_RO_VERITY)
157 iflags |= FS_VERITY_FL;
158
159 if (flags & BTRFS_INODE_NOCOMPRESS)
160 iflags |= FS_NOCOMP_FL;
161 else if (flags & BTRFS_INODE_COMPRESS)
162 iflags |= FS_COMPR_FL;
163
164 return iflags;
165 }
166
167 /*
168 * Update inode->i_flags based on the btrfs internal flags.
169 */
btrfs_sync_inode_flags_to_i_flags(struct btrfs_inode * inode)170 void btrfs_sync_inode_flags_to_i_flags(struct btrfs_inode *inode)
171 {
172 unsigned int new_fl = 0;
173
174 if (inode->flags & BTRFS_INODE_SYNC)
175 new_fl |= S_SYNC;
176 if (inode->flags & BTRFS_INODE_IMMUTABLE)
177 new_fl |= S_IMMUTABLE;
178 if (inode->flags & BTRFS_INODE_APPEND)
179 new_fl |= S_APPEND;
180 if (inode->flags & BTRFS_INODE_NOATIME)
181 new_fl |= S_NOATIME;
182 if (inode->flags & BTRFS_INODE_DIRSYNC)
183 new_fl |= S_DIRSYNC;
184 if (inode->ro_flags & BTRFS_INODE_RO_VERITY)
185 new_fl |= S_VERITY;
186
187 set_mask_bits(&inode->vfs_inode.i_flags,
188 S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC |
189 S_VERITY, new_fl);
190 }
191
192 /*
193 * Check if @flags are a supported and valid set of FS_*_FL flags and that
194 * the old and new flags are not conflicting
195 */
check_fsflags(unsigned int old_flags,unsigned int flags)196 static int check_fsflags(unsigned int old_flags, unsigned int flags)
197 {
198 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
199 FS_NOATIME_FL | FS_NODUMP_FL | \
200 FS_SYNC_FL | FS_DIRSYNC_FL | \
201 FS_NOCOMP_FL | FS_COMPR_FL |
202 FS_NOCOW_FL))
203 return -EOPNOTSUPP;
204
205 /* COMPR and NOCOMP on new/old are valid */
206 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
207 return -EINVAL;
208
209 if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL))
210 return -EINVAL;
211
212 /* NOCOW and compression options are mutually exclusive */
213 if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
214 return -EINVAL;
215 if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL)))
216 return -EINVAL;
217
218 return 0;
219 }
220
check_fsflags_compatible(const struct btrfs_fs_info * fs_info,unsigned int flags)221 static int check_fsflags_compatible(const struct btrfs_fs_info *fs_info,
222 unsigned int flags)
223 {
224 if (btrfs_is_zoned(fs_info) && (flags & FS_NOCOW_FL))
225 return -EPERM;
226
227 return 0;
228 }
229
btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args * vol_args)230 int btrfs_check_ioctl_vol_args_path(const struct btrfs_ioctl_vol_args *vol_args)
231 {
232 if (memchr(vol_args->name, 0, sizeof(vol_args->name)) == NULL)
233 return -ENAMETOOLONG;
234 return 0;
235 }
236
btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 * vol_args2)237 static int btrfs_check_ioctl_vol_args2_subvol_name(const struct btrfs_ioctl_vol_args_v2 *vol_args2)
238 {
239 if (memchr(vol_args2->name, 0, sizeof(vol_args2->name)) == NULL)
240 return -ENAMETOOLONG;
241 return 0;
242 }
243
244 /*
245 * Set flags/xflags from the internal inode flags. The remaining items of
246 * fsxattr are zeroed.
247 */
btrfs_fileattr_get(struct dentry * dentry,struct file_kattr * fa)248 int btrfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
249 {
250 const struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
251
252 fileattr_fill_flags(fa, btrfs_inode_flags_to_fsflags(inode));
253 return 0;
254 }
255
btrfs_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct file_kattr * fa)256 int btrfs_fileattr_set(struct mnt_idmap *idmap,
257 struct dentry *dentry, struct file_kattr *fa)
258 {
259 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
260 struct btrfs_root *root = inode->root;
261 struct btrfs_fs_info *fs_info = root->fs_info;
262 struct btrfs_trans_handle *trans;
263 unsigned int fsflags, old_fsflags;
264 int ret;
265 const char *comp = NULL;
266 u32 inode_flags;
267
268 if (btrfs_root_readonly(root))
269 return -EROFS;
270
271 if (fileattr_has_fsx(fa))
272 return -EOPNOTSUPP;
273
274 fsflags = btrfs_mask_fsflags_for_type(&inode->vfs_inode, fa->flags);
275 old_fsflags = btrfs_inode_flags_to_fsflags(inode);
276 ret = check_fsflags(old_fsflags, fsflags);
277 if (ret)
278 return ret;
279
280 ret = check_fsflags_compatible(fs_info, fsflags);
281 if (ret)
282 return ret;
283
284 inode_flags = inode->flags;
285 if (fsflags & FS_SYNC_FL)
286 inode_flags |= BTRFS_INODE_SYNC;
287 else
288 inode_flags &= ~BTRFS_INODE_SYNC;
289 if (fsflags & FS_IMMUTABLE_FL)
290 inode_flags |= BTRFS_INODE_IMMUTABLE;
291 else
292 inode_flags &= ~BTRFS_INODE_IMMUTABLE;
293 if (fsflags & FS_APPEND_FL)
294 inode_flags |= BTRFS_INODE_APPEND;
295 else
296 inode_flags &= ~BTRFS_INODE_APPEND;
297 if (fsflags & FS_NODUMP_FL)
298 inode_flags |= BTRFS_INODE_NODUMP;
299 else
300 inode_flags &= ~BTRFS_INODE_NODUMP;
301 if (fsflags & FS_NOATIME_FL)
302 inode_flags |= BTRFS_INODE_NOATIME;
303 else
304 inode_flags &= ~BTRFS_INODE_NOATIME;
305
306 /* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */
307 if (!fa->flags_valid) {
308 /* 1 item for the inode */
309 trans = btrfs_start_transaction(root, 1);
310 if (IS_ERR(trans))
311 return PTR_ERR(trans);
312 goto update_flags;
313 }
314
315 if (fsflags & FS_DIRSYNC_FL)
316 inode_flags |= BTRFS_INODE_DIRSYNC;
317 else
318 inode_flags &= ~BTRFS_INODE_DIRSYNC;
319 if (fsflags & FS_NOCOW_FL) {
320 if (S_ISREG(inode->vfs_inode.i_mode)) {
321 /*
322 * It's safe to turn csums off here, no extents exist.
323 * Otherwise we want the flag to reflect the real COW
324 * status of the file and will not set it.
325 */
326 if (inode->vfs_inode.i_size == 0)
327 inode_flags |= BTRFS_INODE_NODATACOW |
328 BTRFS_INODE_NODATASUM;
329 } else {
330 inode_flags |= BTRFS_INODE_NODATACOW;
331 }
332 } else {
333 /*
334 * Revert back under same assumptions as above
335 */
336 if (S_ISREG(inode->vfs_inode.i_mode)) {
337 if (inode->vfs_inode.i_size == 0)
338 inode_flags &= ~(BTRFS_INODE_NODATACOW |
339 BTRFS_INODE_NODATASUM);
340 } else {
341 inode_flags &= ~BTRFS_INODE_NODATACOW;
342 }
343 }
344
345 /*
346 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
347 * flag may be changed automatically if compression code won't make
348 * things smaller.
349 */
350 if (fsflags & FS_NOCOMP_FL) {
351 inode_flags &= ~BTRFS_INODE_COMPRESS;
352 inode_flags |= BTRFS_INODE_NOCOMPRESS;
353 } else if (fsflags & FS_COMPR_FL) {
354
355 if (IS_SWAPFILE(&inode->vfs_inode))
356 return -ETXTBSY;
357
358 inode_flags |= BTRFS_INODE_COMPRESS;
359 inode_flags &= ~BTRFS_INODE_NOCOMPRESS;
360
361 comp = btrfs_compress_type2str(fs_info->compress_type);
362 if (!comp || comp[0] == 0)
363 comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
364 } else {
365 inode_flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
366 }
367
368 /*
369 * 1 for inode item
370 * 2 for properties
371 */
372 trans = btrfs_start_transaction(root, 3);
373 if (IS_ERR(trans))
374 return PTR_ERR(trans);
375
376 if (comp) {
377 ret = btrfs_set_prop(trans, inode, "btrfs.compression",
378 comp, strlen(comp), 0);
379 if (unlikely(ret)) {
380 btrfs_abort_transaction(trans, ret);
381 goto out_end_trans;
382 }
383 } else {
384 ret = btrfs_set_prop(trans, inode, "btrfs.compression", NULL, 0, 0);
385 if (unlikely(ret && ret != -ENODATA)) {
386 btrfs_abort_transaction(trans, ret);
387 goto out_end_trans;
388 }
389 }
390
391 update_flags:
392 inode->flags = inode_flags;
393 btrfs_update_inode_mapping_flags(inode);
394 btrfs_sync_inode_flags_to_i_flags(inode);
395 inode_inc_iversion(&inode->vfs_inode);
396 inode_set_ctime_current(&inode->vfs_inode);
397 ret = btrfs_update_inode(trans, inode);
398
399 out_end_trans:
400 btrfs_end_transaction(trans);
401 return ret;
402 }
403
btrfs_ioctl_getversion(const struct inode * inode,int __user * arg)404 static int btrfs_ioctl_getversion(const struct inode *inode, int __user *arg)
405 {
406 return put_user(inode->i_generation, arg);
407 }
408
btrfs_ioctl_fitrim(struct btrfs_fs_info * fs_info,void __user * arg)409 static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
410 void __user *arg)
411 {
412 struct btrfs_device *device;
413 struct fstrim_range range;
414 u64 minlen = ULLONG_MAX;
415 u64 num_devices = 0;
416 int ret;
417
418 if (!capable(CAP_SYS_ADMIN))
419 return -EPERM;
420
421 /*
422 * btrfs_trim_block_group() depends on space cache, which is not
423 * available in zoned filesystem. So, disallow fitrim on a zoned
424 * filesystem for now.
425 */
426 if (btrfs_is_zoned(fs_info))
427 return -EOPNOTSUPP;
428
429 /*
430 * If the fs is mounted with nologreplay, which requires it to be
431 * mounted in RO mode as well, we can not allow discard on free space
432 * inside block groups, because log trees refer to extents that are not
433 * pinned in a block group's free space cache (pinning the extents is
434 * precisely the first phase of replaying a log tree).
435 */
436 if (btrfs_test_opt(fs_info, NOLOGREPLAY))
437 return -EROFS;
438
439 rcu_read_lock();
440 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
441 dev_list) {
442 if (!device->bdev || !bdev_max_discard_sectors(device->bdev))
443 continue;
444 num_devices++;
445 minlen = min_t(u64, bdev_discard_granularity(device->bdev),
446 minlen);
447 }
448 rcu_read_unlock();
449
450 if (!num_devices)
451 return -EOPNOTSUPP;
452 if (copy_from_user(&range, arg, sizeof(range)))
453 return -EFAULT;
454
455 /*
456 * NOTE: Don't truncate the range using super->total_bytes. Bytenr of
457 * block group is in the logical address space, which can be any
458 * sectorsize aligned bytenr in the range [0, U64_MAX].
459 */
460 if (range.len < fs_info->sectorsize)
461 return -EINVAL;
462
463 range.minlen = max(range.minlen, minlen);
464 ret = btrfs_trim_fs(fs_info, &range);
465
466 if (copy_to_user(arg, &range, sizeof(range)))
467 return -EFAULT;
468
469 return ret;
470 }
471
472 /*
473 * Calculate the number of transaction items to reserve for creating a subvolume
474 * or snapshot, not including the inode, directory entries, or parent directory.
475 */
create_subvol_num_items(const struct btrfs_qgroup_inherit * inherit)476 static unsigned int create_subvol_num_items(const struct btrfs_qgroup_inherit *inherit)
477 {
478 /*
479 * 1 to add root block
480 * 1 to add root item
481 * 1 to add root ref
482 * 1 to add root backref
483 * 1 to add UUID item
484 * 1 to add qgroup info
485 * 1 to add qgroup limit
486 *
487 * Ideally the last two would only be accounted if qgroups are enabled,
488 * but that can change between now and the time we would insert them.
489 */
490 unsigned int num_items = 7;
491
492 if (inherit) {
493 /* 2 to add qgroup relations for each inherited qgroup */
494 num_items += 2 * inherit->num_qgroups;
495 }
496 return num_items;
497 }
498
create_subvol(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,struct btrfs_qgroup_inherit * inherit)499 static noinline int create_subvol(struct mnt_idmap *idmap,
500 struct inode *dir, struct dentry *dentry,
501 struct btrfs_qgroup_inherit *inherit)
502 {
503 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
504 struct btrfs_trans_handle *trans;
505 struct btrfs_key key;
506 struct btrfs_root_item AUTO_KFREE(root_item);
507 struct btrfs_inode_item *inode_item;
508 struct extent_buffer *leaf;
509 struct btrfs_root *root = BTRFS_I(dir)->root;
510 struct btrfs_root *new_root;
511 struct btrfs_block_rsv block_rsv;
512 struct timespec64 cur_time = current_time(dir);
513 struct btrfs_new_inode_args new_inode_args = {
514 .dir = dir,
515 .dentry = dentry,
516 .subvol = true,
517 };
518 unsigned int trans_num_items;
519 int ret;
520 dev_t anon_dev;
521 u64 objectid;
522 u64 qgroup_reserved = 0;
523
524 root_item = kzalloc(sizeof(*root_item), GFP_KERNEL);
525 if (!root_item)
526 return -ENOMEM;
527
528 ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid);
529 if (ret)
530 return ret;
531
532 /*
533 * Don't create subvolume whose level is not zero. Or qgroup will be
534 * screwed up since it assumes subvolume qgroup's level to be 0.
535 */
536 if (btrfs_qgroup_level(objectid))
537 return -ENOSPC;
538
539 ret = get_anon_bdev(&anon_dev);
540 if (ret < 0)
541 return ret;
542
543 new_inode_args.inode = btrfs_new_subvol_inode(idmap, dir);
544 if (!new_inode_args.inode) {
545 ret = -ENOMEM;
546 goto out_anon_dev;
547 }
548 ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items);
549 if (ret)
550 goto out_inode;
551 trans_num_items += create_subvol_num_items(inherit);
552
553 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
554 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
555 trans_num_items, false);
556 if (ret)
557 goto out_new_inode_args;
558 qgroup_reserved = block_rsv.qgroup_rsv_reserved;
559
560 trans = btrfs_start_transaction(root, 0);
561 if (IS_ERR(trans)) {
562 ret = PTR_ERR(trans);
563 goto out_release_rsv;
564 }
565 btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
566 qgroup_reserved = 0;
567 trans->block_rsv = &block_rsv;
568 trans->bytes_reserved = block_rsv.size;
569
570 ret = btrfs_qgroup_inherit(trans, 0, objectid, btrfs_root_id(root), inherit);
571 if (ret)
572 goto out;
573
574 leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
575 0, BTRFS_NESTING_NORMAL);
576 if (IS_ERR(leaf)) {
577 ret = PTR_ERR(leaf);
578 goto out;
579 }
580
581 btrfs_mark_buffer_dirty(trans, leaf);
582
583 inode_item = &root_item->inode;
584 btrfs_set_stack_inode_generation(inode_item, 1);
585 btrfs_set_stack_inode_size(inode_item, 3);
586 btrfs_set_stack_inode_nlink(inode_item, 1);
587 btrfs_set_stack_inode_nbytes(inode_item,
588 fs_info->nodesize);
589 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
590
591 btrfs_set_root_flags(root_item, 0);
592 btrfs_set_root_limit(root_item, 0);
593 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
594
595 btrfs_set_root_bytenr(root_item, leaf->start);
596 btrfs_set_root_generation(root_item, trans->transid);
597 btrfs_set_root_level(root_item, 0);
598 btrfs_set_root_refs(root_item, 1);
599 btrfs_set_root_used(root_item, leaf->len);
600 btrfs_set_root_last_snapshot(root_item, 0);
601
602 btrfs_set_root_generation_v2(root_item,
603 btrfs_root_generation(root_item));
604 generate_random_guid(root_item->uuid);
605 btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec);
606 btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec);
607 root_item->ctime = root_item->otime;
608 btrfs_set_root_ctransid(root_item, trans->transid);
609 btrfs_set_root_otransid(root_item, trans->transid);
610
611 btrfs_tree_unlock(leaf);
612
613 btrfs_set_root_dirid(root_item, BTRFS_FIRST_FREE_OBJECTID);
614
615 key.objectid = objectid;
616 key.type = BTRFS_ROOT_ITEM_KEY;
617 key.offset = 0;
618 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
619 root_item);
620 if (ret) {
621 int ret2;
622
623 /*
624 * Since we don't abort the transaction in this case, free the
625 * tree block so that we don't leak space and leave the
626 * filesystem in an inconsistent state (an extent item in the
627 * extent tree with a backreference for a root that does not
628 * exists).
629 */
630 btrfs_tree_lock(leaf);
631 btrfs_clear_buffer_dirty(trans, leaf);
632 btrfs_tree_unlock(leaf);
633 ret2 = btrfs_free_tree_block(trans, objectid, leaf, 0, 1);
634 if (unlikely(ret2 < 0))
635 btrfs_abort_transaction(trans, ret2);
636 free_extent_buffer(leaf);
637 goto out;
638 }
639
640 free_extent_buffer(leaf);
641 leaf = NULL;
642
643 new_root = btrfs_get_new_fs_root(fs_info, objectid, &anon_dev);
644 if (IS_ERR(new_root)) {
645 ret = PTR_ERR(new_root);
646 btrfs_abort_transaction(trans, ret);
647 goto out;
648 }
649 /* anon_dev is owned by new_root now. */
650 anon_dev = 0;
651 BTRFS_I(new_inode_args.inode)->root = new_root;
652 /* ... and new_root is owned by new_inode_args.inode now. */
653
654 ret = btrfs_record_root_in_trans(trans, new_root);
655 if (unlikely(ret)) {
656 btrfs_abort_transaction(trans, ret);
657 goto out;
658 }
659
660 ret = btrfs_uuid_tree_add(trans, root_item->uuid,
661 BTRFS_UUID_KEY_SUBVOL, objectid);
662 if (unlikely(ret)) {
663 btrfs_abort_transaction(trans, ret);
664 goto out;
665 }
666
667 btrfs_record_new_subvolume(trans, BTRFS_I(dir));
668
669 ret = btrfs_create_new_inode(trans, &new_inode_args);
670 if (unlikely(ret)) {
671 btrfs_abort_transaction(trans, ret);
672 goto out;
673 }
674
675 d_instantiate_new(dentry, new_inode_args.inode);
676 new_inode_args.inode = NULL;
677
678 out:
679 trans->block_rsv = NULL;
680 trans->bytes_reserved = 0;
681 btrfs_end_transaction(trans);
682 out_release_rsv:
683 btrfs_block_rsv_release(fs_info, &block_rsv, (u64)-1, NULL);
684 if (qgroup_reserved)
685 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
686 out_new_inode_args:
687 btrfs_new_inode_args_destroy(&new_inode_args);
688 out_inode:
689 iput(new_inode_args.inode);
690 out_anon_dev:
691 if (anon_dev)
692 free_anon_bdev(anon_dev);
693
694 return ret;
695 }
696
create_snapshot(struct btrfs_root * root,struct inode * dir,struct dentry * dentry,bool readonly,struct btrfs_qgroup_inherit * inherit)697 static int create_snapshot(struct btrfs_root *root, struct inode *dir,
698 struct dentry *dentry, bool readonly,
699 struct btrfs_qgroup_inherit *inherit)
700 {
701 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
702 struct inode *inode;
703 struct btrfs_pending_snapshot *pending_snapshot;
704 unsigned int trans_num_items;
705 struct btrfs_trans_handle *trans;
706 struct btrfs_block_rsv *block_rsv;
707 u64 qgroup_reserved = 0;
708 int ret;
709
710 /* We do not support snapshotting right now. */
711 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
712 btrfs_warn(fs_info,
713 "extent tree v2 doesn't support snapshotting yet");
714 return -EOPNOTSUPP;
715 }
716
717 if (btrfs_root_refs(&root->root_item) == 0)
718 return -ENOENT;
719
720 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
721 return -EINVAL;
722
723 if (atomic_read(&root->nr_swapfiles)) {
724 btrfs_warn(fs_info,
725 "cannot snapshot subvolume with active swapfile");
726 return -ETXTBSY;
727 }
728
729 pending_snapshot = kzalloc_obj(*pending_snapshot);
730 if (!pending_snapshot)
731 return -ENOMEM;
732
733 ret = get_anon_bdev(&pending_snapshot->anon_dev);
734 if (ret < 0)
735 goto free_pending;
736 pending_snapshot->root_item = kzalloc_obj(struct btrfs_root_item);
737 pending_snapshot->path = btrfs_alloc_path();
738 if (!pending_snapshot->root_item || !pending_snapshot->path) {
739 ret = -ENOMEM;
740 goto free_pending;
741 }
742
743 block_rsv = &pending_snapshot->block_rsv;
744 btrfs_init_block_rsv(block_rsv, BTRFS_BLOCK_RSV_TEMP);
745 /*
746 * 1 to add dir item
747 * 1 to add dir index
748 * 1 to update parent inode item
749 */
750 trans_num_items = create_subvol_num_items(inherit) + 3;
751 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, block_rsv,
752 trans_num_items, false);
753 if (ret)
754 goto free_pending;
755 qgroup_reserved = block_rsv->qgroup_rsv_reserved;
756
757 pending_snapshot->dentry = dentry;
758 pending_snapshot->root = root;
759 pending_snapshot->readonly = readonly;
760 pending_snapshot->dir = BTRFS_I(dir);
761 pending_snapshot->inherit = inherit;
762
763 trans = btrfs_start_transaction(root, 0);
764 if (IS_ERR(trans)) {
765 ret = PTR_ERR(trans);
766 goto fail;
767 }
768 ret = btrfs_record_root_in_trans(trans, BTRFS_I(dir)->root);
769 if (ret) {
770 btrfs_end_transaction(trans);
771 goto fail;
772 }
773 btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
774 qgroup_reserved = 0;
775
776 trans->pending_snapshot = pending_snapshot;
777
778 ret = btrfs_commit_transaction(trans);
779 if (ret)
780 goto fail;
781
782 ret = pending_snapshot->error;
783 if (ret)
784 goto fail;
785
786 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
787 if (ret)
788 goto fail;
789
790 inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry);
791 if (IS_ERR(inode)) {
792 ret = PTR_ERR(inode);
793 goto fail;
794 }
795
796 d_instantiate(dentry, inode);
797 ret = 0;
798 pending_snapshot->anon_dev = 0;
799 fail:
800 /* Prevent double freeing of anon_dev */
801 if (ret && pending_snapshot->snap)
802 pending_snapshot->snap->anon_dev = 0;
803 btrfs_put_root(pending_snapshot->snap);
804 btrfs_block_rsv_release(fs_info, block_rsv, (u64)-1, NULL);
805 if (qgroup_reserved)
806 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
807 free_pending:
808 if (pending_snapshot->anon_dev)
809 free_anon_bdev(pending_snapshot->anon_dev);
810 kfree(pending_snapshot->root_item);
811 btrfs_free_path(pending_snapshot->path);
812 kfree(pending_snapshot);
813
814 return ret;
815 }
816
817 /*
818 * Create a new subvolume below @parent. This is largely modeled after
819 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
820 * inside this filesystem so it's quite a bit simpler.
821 */
btrfs_mksubvol(struct dentry * parent,struct mnt_idmap * idmap,struct qstr * qname,struct btrfs_root * snap_src,bool readonly,struct btrfs_qgroup_inherit * inherit)822 static noinline int btrfs_mksubvol(struct dentry *parent,
823 struct mnt_idmap *idmap,
824 struct qstr *qname, struct btrfs_root *snap_src,
825 bool readonly,
826 struct btrfs_qgroup_inherit *inherit)
827 {
828 struct inode *dir = d_inode(parent);
829 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
830 struct dentry *dentry;
831 struct fscrypt_str name_str = FSTR_INIT((char *)qname->name, qname->len);
832 int ret;
833
834 dentry = start_creating_killable(idmap, parent, qname);
835 if (IS_ERR(dentry))
836 return PTR_ERR(dentry);
837
838 ret = may_create_dentry(idmap, dir, dentry);
839 if (ret)
840 goto out_dput;
841
842 /*
843 * even if this name doesn't exist, we may get hash collisions.
844 * check for them now when we can safely fail
845 */
846 ret = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, dir->i_ino, &name_str);
847 if (ret)
848 goto out_dput;
849
850 down_read(&fs_info->subvol_sem);
851
852 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
853 goto out_up_read;
854
855 if (snap_src)
856 ret = create_snapshot(snap_src, dir, dentry, readonly, inherit);
857 else
858 ret = create_subvol(idmap, dir, dentry, inherit);
859
860 if (!ret)
861 fsnotify_mkdir(dir, dentry);
862 out_up_read:
863 up_read(&fs_info->subvol_sem);
864 out_dput:
865 end_creating(dentry);
866 return ret;
867 }
868
btrfs_mksnapshot(struct dentry * parent,struct mnt_idmap * idmap,struct qstr * qname,struct btrfs_root * root,bool readonly,struct btrfs_qgroup_inherit * inherit)869 static noinline int btrfs_mksnapshot(struct dentry *parent,
870 struct mnt_idmap *idmap,
871 struct qstr *qname,
872 struct btrfs_root *root,
873 bool readonly,
874 struct btrfs_qgroup_inherit *inherit)
875 {
876 int ret;
877
878 /*
879 * Force new buffered writes to reserve space even when NOCOW is
880 * possible. This is to avoid later writeback (running delalloc) to
881 * fallback to COW mode and unexpectedly fail with ENOSPC.
882 */
883 btrfs_drew_read_lock(&root->snapshot_lock);
884
885 ret = btrfs_start_delalloc_snapshot(root, false);
886 if (ret)
887 goto out;
888
889 /*
890 * All previous writes have started writeback in NOCOW mode, so now
891 * we force future writes to fallback to COW mode during snapshot
892 * creation.
893 */
894 atomic_inc(&root->snapshot_force_cow);
895
896 btrfs_wait_ordered_extents(root, U64_MAX, NULL);
897
898 ret = btrfs_mksubvol(parent, idmap, qname, root, readonly, inherit);
899
900 atomic_dec(&root->snapshot_force_cow);
901 out:
902 btrfs_drew_read_unlock(&root->snapshot_lock);
903 return ret;
904 }
905
906 /*
907 * Try to start exclusive operation @type or cancel it if it's running.
908 *
909 * Return:
910 * 0 - normal mode, newly claimed op started
911 * >0 - normal mode, something else is running,
912 * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space
913 * ECANCELED - cancel mode, successful cancel
914 * ENOTCONN - cancel mode, operation not running anymore
915 */
exclop_start_or_cancel_reloc(struct btrfs_fs_info * fs_info,enum btrfs_exclusive_operation type,bool cancel)916 static int exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info,
917 enum btrfs_exclusive_operation type, bool cancel)
918 {
919 if (!cancel) {
920 /* Start normal op */
921 if (!btrfs_exclop_start(fs_info, type))
922 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
923 /* Exclusive operation is now claimed */
924 return 0;
925 }
926
927 /* Cancel running op */
928 if (btrfs_exclop_start_try_lock(fs_info, type)) {
929 /*
930 * This blocks any exclop finish from setting it to NONE, so we
931 * request cancellation. Either it runs and we will wait for it,
932 * or it has finished and no waiting will happen.
933 */
934 atomic_inc(&fs_info->reloc_cancel_req);
935 btrfs_exclop_start_unlock(fs_info);
936
937 if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
938 wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
939 TASK_INTERRUPTIBLE);
940
941 return -ECANCELED;
942 }
943
944 /* Something else is running or none */
945 return -ENOTCONN;
946 }
947
btrfs_ioctl_resize(struct file * file,void __user * arg)948 static noinline int btrfs_ioctl_resize(struct file *file,
949 void __user *arg)
950 {
951 BTRFS_DEV_LOOKUP_ARGS(args);
952 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
953 struct btrfs_fs_info *fs_info = root->fs_info;
954 u64 new_size;
955 u64 old_size;
956 u64 devid = 1;
957 struct btrfs_ioctl_vol_args *vol_args;
958 struct btrfs_device *device = NULL;
959 char *sizestr;
960 char *devstr = NULL;
961 int ret = 0;
962 int mod = 0;
963 bool cancel;
964
965 if (!capable(CAP_SYS_ADMIN))
966 return -EPERM;
967
968 ret = mnt_want_write_file(file);
969 if (ret)
970 return ret;
971
972 /*
973 * Read the arguments before checking exclusivity to be able to
974 * distinguish regular resize and cancel
975 */
976 vol_args = memdup_user(arg, sizeof(*vol_args));
977 if (IS_ERR(vol_args)) {
978 ret = PTR_ERR(vol_args);
979 goto out_drop;
980 }
981 ret = btrfs_check_ioctl_vol_args_path(vol_args);
982 if (ret < 0)
983 goto out_free;
984
985 sizestr = vol_args->name;
986 cancel = (strcmp("cancel", sizestr) == 0);
987 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel);
988 if (ret)
989 goto out_free;
990 /* Exclusive operation is now claimed */
991
992 devstr = strchr(sizestr, ':');
993 if (devstr) {
994 sizestr = devstr + 1;
995 *devstr = '\0';
996 devstr = vol_args->name;
997 ret = kstrtoull(devstr, 10, &devid);
998 if (ret)
999 goto out_finish;
1000 if (!devid) {
1001 ret = -EINVAL;
1002 goto out_finish;
1003 }
1004 btrfs_info(fs_info, "resizing devid %llu", devid);
1005 }
1006
1007 args.devid = devid;
1008 device = btrfs_find_device(fs_info->fs_devices, &args);
1009 if (!device) {
1010 btrfs_info(fs_info, "resizer unable to find device %llu",
1011 devid);
1012 ret = -ENODEV;
1013 goto out_finish;
1014 }
1015
1016 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1017 btrfs_info(fs_info,
1018 "resizer unable to apply on readonly device %llu",
1019 devid);
1020 ret = -EPERM;
1021 goto out_finish;
1022 }
1023
1024 if (!strcmp(sizestr, "max"))
1025 new_size = bdev_nr_bytes(device->bdev);
1026 else {
1027 char *retptr;
1028
1029 if (sizestr[0] == '-') {
1030 mod = -1;
1031 sizestr++;
1032 } else if (sizestr[0] == '+') {
1033 mod = 1;
1034 sizestr++;
1035 }
1036 new_size = memparse(sizestr, &retptr);
1037 if (*retptr != '\0' || new_size == 0) {
1038 ret = -EINVAL;
1039 goto out_finish;
1040 }
1041 }
1042
1043 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1044 ret = -EPERM;
1045 goto out_finish;
1046 }
1047
1048 old_size = btrfs_device_get_total_bytes(device);
1049
1050 if (mod < 0) {
1051 if (new_size > old_size) {
1052 ret = -EINVAL;
1053 goto out_finish;
1054 }
1055 new_size = old_size - new_size;
1056 } else if (mod > 0) {
1057 if (new_size > ULLONG_MAX - old_size) {
1058 ret = -ERANGE;
1059 goto out_finish;
1060 }
1061 new_size = old_size + new_size;
1062 }
1063
1064 if (new_size < SZ_256M) {
1065 ret = -EINVAL;
1066 goto out_finish;
1067 }
1068 if (new_size > bdev_nr_bytes(device->bdev)) {
1069 ret = -EFBIG;
1070 goto out_finish;
1071 }
1072
1073 new_size = round_down(new_size, fs_info->sectorsize);
1074
1075 if (new_size > old_size) {
1076 struct btrfs_trans_handle *trans;
1077
1078 trans = btrfs_start_transaction(root, 0);
1079 if (IS_ERR(trans)) {
1080 ret = PTR_ERR(trans);
1081 goto out_finish;
1082 }
1083 ret = btrfs_grow_device(trans, device, new_size);
1084 btrfs_commit_transaction(trans);
1085 } else if (new_size < old_size) {
1086 ret = btrfs_shrink_device(device, new_size);
1087 } /* equal, nothing need to do */
1088
1089 if (ret == 0 && new_size != old_size)
1090 btrfs_info(fs_info,
1091 "resize device %s (devid %llu) from %llu to %llu",
1092 btrfs_dev_name(device), device->devid,
1093 old_size, new_size);
1094 out_finish:
1095 btrfs_exclop_finish(fs_info);
1096 out_free:
1097 kfree(vol_args);
1098 out_drop:
1099 mnt_drop_write_file(file);
1100 return ret;
1101 }
1102
__btrfs_ioctl_snap_create(struct file * file,struct mnt_idmap * idmap,const char * name,unsigned long fd,bool subvol,bool readonly,struct btrfs_qgroup_inherit * inherit)1103 static noinline int __btrfs_ioctl_snap_create(struct file *file,
1104 struct mnt_idmap *idmap,
1105 const char *name, unsigned long fd, bool subvol,
1106 bool readonly,
1107 struct btrfs_qgroup_inherit *inherit)
1108 {
1109 int ret;
1110 struct qstr qname = QSTR_INIT(name, strlen(name));
1111
1112 if (!S_ISDIR(file_inode(file)->i_mode))
1113 return -ENOTDIR;
1114
1115 ret = mnt_want_write_file(file);
1116 if (ret)
1117 return ret;
1118
1119 if (strchr(name, '/')) {
1120 ret = -EINVAL;
1121 goto out_drop_write;
1122 }
1123
1124 if (qname.name[0] == '.' &&
1125 (qname.len == 1 || (qname.name[1] == '.' && qname.len == 2))) {
1126 ret = -EEXIST;
1127 goto out_drop_write;
1128 }
1129
1130 if (subvol) {
1131 ret = btrfs_mksubvol(file_dentry(file), idmap, &qname, NULL,
1132 readonly, inherit);
1133 } else {
1134 CLASS(fd, src)(fd);
1135 struct inode *src_inode;
1136 if (fd_empty(src)) {
1137 ret = -EINVAL;
1138 goto out_drop_write;
1139 }
1140
1141 src_inode = file_inode(fd_file(src));
1142 if (src_inode->i_sb != file_inode(file)->i_sb) {
1143 btrfs_info(BTRFS_I(file_inode(file))->root->fs_info,
1144 "Snapshot src from another FS");
1145 ret = -EXDEV;
1146 } else if (!inode_owner_or_capable(idmap, src_inode)) {
1147 /*
1148 * Subvolume creation is not restricted, but snapshots
1149 * are limited to own subvolumes only
1150 */
1151 ret = -EPERM;
1152 } else if (btrfs_ino(BTRFS_I(src_inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1153 /*
1154 * Snapshots must be made with the src_inode referring
1155 * to the subvolume inode, otherwise the permission
1156 * checking above is useless because we may have
1157 * permission on a lower directory but not the subvol
1158 * itself.
1159 */
1160 ret = -EINVAL;
1161 } else {
1162 ret = btrfs_mksnapshot(file_dentry(file), idmap, &qname,
1163 BTRFS_I(src_inode)->root,
1164 readonly, inherit);
1165 }
1166 }
1167 out_drop_write:
1168 mnt_drop_write_file(file);
1169 return ret;
1170 }
1171
btrfs_ioctl_snap_create(struct file * file,void __user * arg,bool subvol)1172 static noinline int btrfs_ioctl_snap_create(struct file *file,
1173 void __user *arg, bool subvol)
1174 {
1175 struct btrfs_ioctl_vol_args *vol_args;
1176 int ret;
1177
1178 if (!S_ISDIR(file_inode(file)->i_mode))
1179 return -ENOTDIR;
1180
1181 vol_args = memdup_user(arg, sizeof(*vol_args));
1182 if (IS_ERR(vol_args))
1183 return PTR_ERR(vol_args);
1184 ret = btrfs_check_ioctl_vol_args_path(vol_args);
1185 if (ret < 0)
1186 goto out;
1187
1188 ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
1189 vol_args->name, vol_args->fd, subvol,
1190 false, NULL);
1191
1192 out:
1193 kfree(vol_args);
1194 return ret;
1195 }
1196
btrfs_ioctl_snap_create_v2(struct file * file,void __user * arg,bool subvol)1197 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1198 void __user *arg, bool subvol)
1199 {
1200 struct btrfs_ioctl_vol_args_v2 *vol_args;
1201 int ret;
1202 bool readonly = false;
1203 struct btrfs_qgroup_inherit *inherit = NULL;
1204
1205 if (!S_ISDIR(file_inode(file)->i_mode))
1206 return -ENOTDIR;
1207
1208 vol_args = memdup_user(arg, sizeof(*vol_args));
1209 if (IS_ERR(vol_args))
1210 return PTR_ERR(vol_args);
1211 ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
1212 if (ret < 0)
1213 goto free_args;
1214
1215 if (vol_args->flags & ~BTRFS_SUBVOL_CREATE_ARGS_MASK) {
1216 ret = -EOPNOTSUPP;
1217 goto free_args;
1218 }
1219
1220 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1221 readonly = true;
1222 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1223 struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
1224
1225 if (vol_args->size < sizeof(*inherit) ||
1226 vol_args->size > PAGE_SIZE) {
1227 ret = -EINVAL;
1228 goto free_args;
1229 }
1230 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1231 if (IS_ERR(inherit)) {
1232 ret = PTR_ERR(inherit);
1233 goto free_args;
1234 }
1235
1236 ret = btrfs_qgroup_check_inherit(fs_info, inherit, vol_args->size);
1237 if (ret < 0)
1238 goto free_inherit;
1239 }
1240
1241 ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
1242 vol_args->name, vol_args->fd, subvol,
1243 readonly, inherit);
1244 if (ret)
1245 goto free_inherit;
1246 free_inherit:
1247 kfree(inherit);
1248 free_args:
1249 kfree(vol_args);
1250 return ret;
1251 }
1252
btrfs_ioctl_subvol_getflags(struct btrfs_inode * inode,void __user * arg)1253 static noinline int btrfs_ioctl_subvol_getflags(struct btrfs_inode *inode,
1254 void __user *arg)
1255 {
1256 struct btrfs_root *root = inode->root;
1257 struct btrfs_fs_info *fs_info = root->fs_info;
1258 int ret = 0;
1259 u64 flags = 0;
1260
1261 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1262 return -EINVAL;
1263
1264 down_read(&fs_info->subvol_sem);
1265 if (btrfs_root_readonly(root))
1266 flags |= BTRFS_SUBVOL_RDONLY;
1267 up_read(&fs_info->subvol_sem);
1268
1269 if (copy_to_user(arg, &flags, sizeof(flags)))
1270 ret = -EFAULT;
1271
1272 return ret;
1273 }
1274
btrfs_ioctl_subvol_setflags(struct file * file,void __user * arg)1275 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1276 void __user *arg)
1277 {
1278 struct inode *inode = file_inode(file);
1279 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
1280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281 struct btrfs_trans_handle *trans;
1282 u64 root_flags;
1283 u64 flags;
1284 int ret;
1285
1286 if (!inode_owner_or_capable(file_mnt_idmap(file), inode))
1287 return -EPERM;
1288
1289 ret = mnt_want_write_file(file);
1290 if (ret)
1291 return ret;
1292
1293 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1294 ret = -EINVAL;
1295 goto out_drop_write;
1296 }
1297
1298 if (copy_from_user(&flags, arg, sizeof(flags))) {
1299 ret = -EFAULT;
1300 goto out_drop_write;
1301 }
1302
1303 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1304 ret = -EOPNOTSUPP;
1305 goto out_drop_write;
1306 }
1307
1308 down_write(&fs_info->subvol_sem);
1309
1310 /* nothing to do */
1311 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1312 goto out_drop_sem;
1313
1314 root_flags = btrfs_root_flags(&root->root_item);
1315 if (flags & BTRFS_SUBVOL_RDONLY) {
1316 btrfs_set_root_flags(&root->root_item,
1317 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1318 } else {
1319 /*
1320 * Block RO -> RW transition if this subvolume is involved in
1321 * send
1322 */
1323 spin_lock(&root->root_item_lock);
1324 if (root->send_in_progress == 0) {
1325 btrfs_set_root_flags(&root->root_item,
1326 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1327 spin_unlock(&root->root_item_lock);
1328 } else {
1329 spin_unlock(&root->root_item_lock);
1330 btrfs_warn(fs_info,
1331 "Attempt to set subvolume %llu read-write during send",
1332 btrfs_root_id(root));
1333 ret = -EPERM;
1334 goto out_drop_sem;
1335 }
1336 }
1337
1338 trans = btrfs_start_transaction(root, 1);
1339 if (IS_ERR(trans)) {
1340 ret = PTR_ERR(trans);
1341 goto out_reset;
1342 }
1343
1344 ret = btrfs_update_root(trans, fs_info->tree_root,
1345 &root->root_key, &root->root_item);
1346 if (ret < 0) {
1347 btrfs_end_transaction(trans);
1348 goto out_reset;
1349 }
1350
1351 ret = btrfs_commit_transaction(trans);
1352
1353 out_reset:
1354 if (ret)
1355 btrfs_set_root_flags(&root->root_item, root_flags);
1356 out_drop_sem:
1357 up_write(&fs_info->subvol_sem);
1358 out_drop_write:
1359 mnt_drop_write_file(file);
1360 return ret;
1361 }
1362
key_in_sk(const struct btrfs_key * key,const struct btrfs_ioctl_search_key * sk)1363 static noinline bool key_in_sk(const struct btrfs_key *key,
1364 const struct btrfs_ioctl_search_key *sk)
1365 {
1366 struct btrfs_key test;
1367 int ret;
1368
1369 test.objectid = sk->min_objectid;
1370 test.type = sk->min_type;
1371 test.offset = sk->min_offset;
1372
1373 ret = btrfs_comp_cpu_keys(key, &test);
1374 if (ret < 0)
1375 return false;
1376
1377 test.objectid = sk->max_objectid;
1378 test.type = sk->max_type;
1379 test.offset = sk->max_offset;
1380
1381 ret = btrfs_comp_cpu_keys(key, &test);
1382 if (ret > 0)
1383 return false;
1384 return true;
1385 }
1386
copy_to_sk(struct btrfs_path * path,struct btrfs_key * key,const struct btrfs_ioctl_search_key * sk,u64 * buf_size,char __user * ubuf,unsigned long * sk_offset,int * num_found)1387 static noinline int copy_to_sk(struct btrfs_path *path,
1388 struct btrfs_key *key,
1389 const struct btrfs_ioctl_search_key *sk,
1390 u64 *buf_size,
1391 char __user *ubuf,
1392 unsigned long *sk_offset,
1393 int *num_found)
1394 {
1395 u64 found_transid;
1396 struct extent_buffer *leaf;
1397 struct btrfs_ioctl_search_header sh;
1398 struct btrfs_key test;
1399 unsigned long item_off;
1400 unsigned long item_len;
1401 int nritems;
1402 int i;
1403 int slot;
1404 int ret = 0;
1405
1406 leaf = path->nodes[0];
1407 slot = path->slots[0];
1408 nritems = btrfs_header_nritems(leaf);
1409
1410 if (btrfs_header_generation(leaf) > sk->max_transid) {
1411 i = nritems;
1412 goto advance_key;
1413 }
1414 found_transid = btrfs_header_generation(leaf);
1415
1416 for (i = slot; i < nritems; i++) {
1417 item_off = btrfs_item_ptr_offset(leaf, i);
1418 item_len = btrfs_item_size(leaf, i);
1419
1420 btrfs_item_key_to_cpu(leaf, key, i);
1421 if (!key_in_sk(key, sk))
1422 continue;
1423
1424 if (sizeof(sh) + item_len > *buf_size) {
1425 if (*num_found)
1426 return 1;
1427
1428 /*
1429 * return one empty item back for v1, which does not
1430 * handle -EOVERFLOW
1431 */
1432
1433 *buf_size = sizeof(sh) + item_len;
1434 item_len = 0;
1435 ret = -EOVERFLOW;
1436 }
1437
1438 if (sizeof(sh) + item_len + *sk_offset > *buf_size)
1439 return 1;
1440
1441 sh.objectid = key->objectid;
1442 sh.type = key->type;
1443 sh.offset = key->offset;
1444 sh.len = item_len;
1445 sh.transid = found_transid;
1446
1447 /*
1448 * Copy search result header. If we fault then loop again so we
1449 * can fault in the pages and -EFAULT there if there's a
1450 * problem. Otherwise we'll fault and then copy the buffer in
1451 * properly this next time through
1452 */
1453 if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh)))
1454 return 0;
1455
1456 *sk_offset += sizeof(sh);
1457
1458 if (item_len) {
1459 char __user *up = ubuf + *sk_offset;
1460 /*
1461 * Copy the item, same behavior as above, but reset the
1462 * * sk_offset so we copy the full thing again.
1463 */
1464 if (read_extent_buffer_to_user_nofault(leaf, up,
1465 item_off, item_len)) {
1466 *sk_offset -= sizeof(sh);
1467 return 0;
1468 }
1469
1470 *sk_offset += item_len;
1471 }
1472 (*num_found)++;
1473
1474 /* -EOVERFLOW from above. */
1475 if (ret)
1476 return ret;
1477
1478 if (*num_found >= sk->nr_items)
1479 return 1;
1480 }
1481 advance_key:
1482 ret = 0;
1483 test.objectid = sk->max_objectid;
1484 test.type = sk->max_type;
1485 test.offset = sk->max_offset;
1486 if (btrfs_comp_cpu_keys(key, &test) >= 0)
1487 ret = 1;
1488 else if (key->offset < (u64)-1)
1489 key->offset++;
1490 else if (key->type < (u8)-1) {
1491 key->offset = 0;
1492 key->type++;
1493 } else if (key->objectid < (u64)-1) {
1494 key->offset = 0;
1495 key->type = 0;
1496 key->objectid++;
1497 } else
1498 ret = 1;
1499
1500 /*
1501 * 0: all items from this leaf copied, continue with next
1502 * 1: * more items can be copied, but unused buffer is too small
1503 * * all items were found
1504 * Either way, it will stops the loop which iterates to the next
1505 * leaf
1506 * -EOVERFLOW: item was to large for buffer
1507 * -EFAULT: could not copy extent buffer back to userspace
1508 */
1509 return ret;
1510 }
1511
search_ioctl(struct btrfs_root * root,struct btrfs_ioctl_search_key * sk,u64 * buf_size,char __user * ubuf)1512 static noinline int search_ioctl(struct btrfs_root *root,
1513 struct btrfs_ioctl_search_key *sk,
1514 u64 *buf_size,
1515 char __user *ubuf)
1516 {
1517 struct btrfs_fs_info *info = root->fs_info;
1518 struct btrfs_key key;
1519 BTRFS_PATH_AUTO_FREE(path);
1520 int ret;
1521 int num_found = 0;
1522 unsigned long sk_offset = 0;
1523
1524 if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) {
1525 *buf_size = sizeof(struct btrfs_ioctl_search_header);
1526 return -EOVERFLOW;
1527 }
1528
1529 path = btrfs_alloc_path();
1530 if (!path)
1531 return -ENOMEM;
1532
1533 if (sk->tree_id == 0) {
1534 /* Search the root that we got passed. */
1535 root = btrfs_grab_root(root);
1536 } else {
1537 /* Look up the root from the arguments. */
1538 root = btrfs_get_fs_root(info, sk->tree_id, true);
1539 if (IS_ERR(root))
1540 return PTR_ERR(root);
1541 }
1542
1543 key.objectid = sk->min_objectid;
1544 key.type = sk->min_type;
1545 key.offset = sk->min_offset;
1546
1547 while (1) {
1548 /*
1549 * Ensure that the whole user buffer is faulted in at sub-page
1550 * granularity, otherwise the loop may live-lock.
1551 */
1552 if (fault_in_subpage_writeable(ubuf + sk_offset, *buf_size - sk_offset)) {
1553 ret = -EFAULT;
1554 break;
1555 }
1556
1557 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
1558 if (ret)
1559 break;
1560
1561 ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
1562 &sk_offset, &num_found);
1563 btrfs_release_path(path);
1564 if (ret)
1565 break;
1566
1567 }
1568 /* Normalize return values from btrfs_search_forward() and copy_to_sk(). */
1569 if (ret > 0)
1570 ret = 0;
1571
1572 sk->nr_items = num_found;
1573 btrfs_put_root(root);
1574 return ret;
1575 }
1576
btrfs_ioctl_tree_search(struct btrfs_root * root,void __user * argp)1577 static noinline int btrfs_ioctl_tree_search(struct btrfs_root *root,
1578 void __user *argp)
1579 {
1580 struct btrfs_ioctl_search_args __user *uargs = argp;
1581 struct btrfs_ioctl_search_key sk;
1582 int ret;
1583 u64 buf_size;
1584
1585 if (!capable(CAP_SYS_ADMIN))
1586 return -EPERM;
1587
1588 if (copy_from_user(&sk, &uargs->key, sizeof(sk)))
1589 return -EFAULT;
1590
1591 buf_size = sizeof(uargs->buf);
1592
1593 ret = search_ioctl(root, &sk, &buf_size, uargs->buf);
1594
1595 /*
1596 * In the origin implementation an overflow is handled by returning a
1597 * search header with a len of zero, so reset ret.
1598 */
1599 if (ret == -EOVERFLOW)
1600 ret = 0;
1601
1602 if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
1603 ret = -EFAULT;
1604 return ret;
1605 }
1606
btrfs_ioctl_tree_search_v2(struct btrfs_root * root,void __user * argp)1607 static noinline int btrfs_ioctl_tree_search_v2(struct btrfs_root *root,
1608 void __user *argp)
1609 {
1610 struct btrfs_ioctl_search_args_v2 __user *uarg = argp;
1611 struct btrfs_ioctl_search_args_v2 args;
1612 int ret;
1613 u64 buf_size;
1614 const u64 buf_limit = SZ_16M;
1615
1616 if (!capable(CAP_SYS_ADMIN))
1617 return -EPERM;
1618
1619 /* copy search header and buffer size */
1620 if (copy_from_user(&args, uarg, sizeof(args)))
1621 return -EFAULT;
1622
1623 buf_size = args.buf_size;
1624
1625 /* limit result size to 16MB */
1626 if (buf_size > buf_limit)
1627 buf_size = buf_limit;
1628
1629 ret = search_ioctl(root, &args.key, &buf_size,
1630 (char __user *)(&uarg->buf[0]));
1631 if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
1632 ret = -EFAULT;
1633 else if (ret == -EOVERFLOW &&
1634 copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
1635 ret = -EFAULT;
1636
1637 return ret;
1638 }
1639
1640 /*
1641 * Search INODE_REFs to identify path name of 'dirid' directory
1642 * in a 'tree_id' tree. and sets path name to 'name'.
1643 */
btrfs_search_path_in_tree(struct btrfs_fs_info * info,u64 tree_id,u64 dirid,char * name)1644 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1645 u64 tree_id, u64 dirid, char *name)
1646 {
1647 struct btrfs_root *root;
1648 struct btrfs_key key;
1649 char *ptr;
1650 int ret = -1;
1651 int slot;
1652 int len;
1653 int total_len = 0;
1654 struct btrfs_inode_ref *iref;
1655 struct extent_buffer *l;
1656 BTRFS_PATH_AUTO_FREE(path);
1657
1658 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1659 name[0]='\0';
1660 return 0;
1661 }
1662
1663 path = btrfs_alloc_path();
1664 if (!path)
1665 return -ENOMEM;
1666
1667 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
1668
1669 root = btrfs_get_fs_root(info, tree_id, true);
1670 if (IS_ERR(root)) {
1671 ret = PTR_ERR(root);
1672 root = NULL;
1673 goto out;
1674 }
1675
1676 key.objectid = dirid;
1677 key.type = BTRFS_INODE_REF_KEY;
1678 key.offset = (u64)-1;
1679
1680 while (1) {
1681 ret = btrfs_search_backwards(root, &key, path);
1682 if (ret < 0)
1683 goto out;
1684 else if (ret > 0) {
1685 ret = -ENOENT;
1686 goto out;
1687 }
1688
1689 l = path->nodes[0];
1690 slot = path->slots[0];
1691
1692 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1693 len = btrfs_inode_ref_name_len(l, iref);
1694 ptr -= len + 1;
1695 total_len += len + 1;
1696 if (ptr < name) {
1697 ret = -ENAMETOOLONG;
1698 goto out;
1699 }
1700
1701 *(ptr + len) = '/';
1702 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
1703
1704 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1705 break;
1706
1707 btrfs_release_path(path);
1708 key.objectid = key.offset;
1709 key.offset = (u64)-1;
1710 dirid = key.objectid;
1711 }
1712 memmove(name, ptr, total_len);
1713 name[total_len] = '\0';
1714 ret = 0;
1715 out:
1716 btrfs_put_root(root);
1717 return ret;
1718 }
1719
btrfs_search_path_in_tree_user(struct mnt_idmap * idmap,struct inode * inode,struct btrfs_ioctl_ino_lookup_user_args * args)1720 static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
1721 struct inode *inode,
1722 struct btrfs_ioctl_ino_lookup_user_args *args)
1723 {
1724 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1725 u64 upper_limit = btrfs_ino(BTRFS_I(inode));
1726 u64 treeid = btrfs_root_id(BTRFS_I(inode)->root);
1727 u64 dirid = args->dirid;
1728 unsigned long item_off;
1729 unsigned long item_len;
1730 struct btrfs_inode_ref *iref;
1731 struct btrfs_root_ref *rref;
1732 struct btrfs_root *root = NULL;
1733 BTRFS_PATH_AUTO_FREE(path);
1734 struct btrfs_key key;
1735 struct extent_buffer *leaf;
1736 char *ptr;
1737 int slot;
1738 int len;
1739 int total_len = 0;
1740 int ret;
1741
1742 path = btrfs_alloc_path();
1743 if (!path)
1744 return -ENOMEM;
1745
1746 /*
1747 * If the bottom subvolume does not exist directly under upper_limit,
1748 * construct the path in from the bottom up.
1749 */
1750 if (dirid != upper_limit) {
1751 ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
1752
1753 root = btrfs_get_fs_root(fs_info, treeid, true);
1754 if (IS_ERR(root))
1755 return PTR_ERR(root);
1756
1757 key.objectid = dirid;
1758 key.type = BTRFS_INODE_REF_KEY;
1759 key.offset = (u64)-1;
1760 while (1) {
1761 struct btrfs_inode *temp_inode;
1762
1763 ret = btrfs_search_backwards(root, &key, path);
1764 if (ret < 0)
1765 goto out_put;
1766 else if (ret > 0) {
1767 ret = -ENOENT;
1768 goto out_put;
1769 }
1770
1771 leaf = path->nodes[0];
1772 slot = path->slots[0];
1773
1774 iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref);
1775 len = btrfs_inode_ref_name_len(leaf, iref);
1776 ptr -= len + 1;
1777 total_len += len + 1;
1778 if (ptr < args->path) {
1779 ret = -ENAMETOOLONG;
1780 goto out_put;
1781 }
1782
1783 *(ptr + len) = '/';
1784 read_extent_buffer(leaf, ptr,
1785 (unsigned long)(iref + 1), len);
1786
1787 /*
1788 * We don't need the path anymore, so release it and
1789 * avoid deadlocks and lockdep warnings in case
1790 * btrfs_iget() needs to lookup the inode from its root
1791 * btree and lock the same leaf.
1792 */
1793 btrfs_release_path(path);
1794 temp_inode = btrfs_iget(key.offset, root);
1795 if (IS_ERR(temp_inode)) {
1796 ret = PTR_ERR(temp_inode);
1797 goto out_put;
1798 }
1799 /* Check the read+exec permission of this directory. */
1800 ret = inode_permission(idmap, &temp_inode->vfs_inode,
1801 MAY_READ | MAY_EXEC);
1802 iput(&temp_inode->vfs_inode);
1803 if (ret)
1804 goto out_put;
1805
1806 if (key.offset == upper_limit)
1807 break;
1808 if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
1809 ret = -EACCES;
1810 goto out_put;
1811 }
1812
1813 key.objectid = key.offset;
1814 key.offset = (u64)-1;
1815 dirid = key.objectid;
1816 }
1817
1818 memmove(args->path, ptr, total_len);
1819 args->path[total_len] = '\0';
1820 btrfs_put_root(root);
1821 root = NULL;
1822 btrfs_release_path(path);
1823 }
1824
1825 /* Get the bottom subvolume's name from ROOT_REF */
1826 key.objectid = treeid;
1827 key.type = BTRFS_ROOT_REF_KEY;
1828 key.offset = args->treeid;
1829 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1830 if (ret < 0)
1831 return ret;
1832 else if (ret > 0)
1833 return -ENOENT;
1834
1835 leaf = path->nodes[0];
1836 slot = path->slots[0];
1837 btrfs_item_key_to_cpu(leaf, &key, slot);
1838
1839 item_off = btrfs_item_ptr_offset(leaf, slot);
1840 item_len = btrfs_item_size(leaf, slot);
1841 /* Check if dirid in ROOT_REF corresponds to passed dirid */
1842 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
1843 if (args->dirid != btrfs_root_ref_dirid(leaf, rref))
1844 return -EINVAL;
1845
1846 /* Copy subvolume's name */
1847 item_off += sizeof(struct btrfs_root_ref);
1848 item_len -= sizeof(struct btrfs_root_ref);
1849 read_extent_buffer(leaf, args->name, item_off, item_len);
1850 args->name[item_len] = 0;
1851
1852 out_put:
1853 btrfs_put_root(root);
1854
1855 return ret;
1856 }
1857
btrfs_ioctl_ino_lookup(struct btrfs_root * root,void __user * argp)1858 static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root,
1859 void __user *argp)
1860 {
1861 struct btrfs_ioctl_ino_lookup_args *args;
1862 int ret = 0;
1863
1864 args = memdup_user(argp, sizeof(*args));
1865 if (IS_ERR(args))
1866 return PTR_ERR(args);
1867
1868 /*
1869 * Unprivileged query to obtain the containing subvolume root id. The
1870 * path is reset so it's consistent with btrfs_search_path_in_tree.
1871 */
1872 if (args->treeid == 0)
1873 args->treeid = btrfs_root_id(root);
1874
1875 if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) {
1876 args->name[0] = 0;
1877 goto out;
1878 }
1879
1880 if (!capable(CAP_SYS_ADMIN)) {
1881 ret = -EPERM;
1882 goto out;
1883 }
1884
1885 ret = btrfs_search_path_in_tree(root->fs_info,
1886 args->treeid, args->objectid,
1887 args->name);
1888
1889 out:
1890 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1891 ret = -EFAULT;
1892
1893 kfree(args);
1894 return ret;
1895 }
1896
1897 /*
1898 * Version of ino_lookup ioctl (unprivileged)
1899 *
1900 * The main differences from ino_lookup ioctl are:
1901 *
1902 * 1. Read + Exec permission will be checked using inode_permission() during
1903 * path construction. -EACCES will be returned in case of failure.
1904 * 2. Path construction will be stopped at the inode number which corresponds
1905 * to the fd with which this ioctl is called. If constructed path does not
1906 * exist under fd's inode, -EACCES will be returned.
1907 * 3. The name of bottom subvolume is also searched and filled.
1908 */
btrfs_ioctl_ino_lookup_user(struct file * file,void __user * argp)1909 static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
1910 {
1911 struct btrfs_ioctl_ino_lookup_user_args *args;
1912 struct inode *inode;
1913 int ret;
1914
1915 args = memdup_user(argp, sizeof(*args));
1916 if (IS_ERR(args))
1917 return PTR_ERR(args);
1918
1919 inode = file_inode(file);
1920
1921 if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
1922 btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
1923 /*
1924 * The subvolume does not exist under fd with which this is
1925 * called
1926 */
1927 kfree(args);
1928 return -EACCES;
1929 }
1930
1931 ret = btrfs_search_path_in_tree_user(file_mnt_idmap(file), inode, args);
1932
1933 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1934 ret = -EFAULT;
1935
1936 kfree(args);
1937 return ret;
1938 }
1939
1940 /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */
btrfs_ioctl_get_subvol_info(struct inode * inode,void __user * argp)1941 static int btrfs_ioctl_get_subvol_info(struct inode *inode, void __user *argp)
1942 {
1943 struct btrfs_ioctl_get_subvol_info_args *subvol_info;
1944 struct btrfs_fs_info *fs_info;
1945 struct btrfs_root *root;
1946 struct btrfs_path *path;
1947 struct btrfs_key key;
1948 struct btrfs_root_item *root_item;
1949 struct btrfs_root_ref *rref;
1950 struct extent_buffer *leaf;
1951 unsigned long item_off;
1952 unsigned long item_len;
1953 int slot;
1954 int ret = 0;
1955
1956 path = btrfs_alloc_path();
1957 if (!path)
1958 return -ENOMEM;
1959
1960 subvol_info = kzalloc_obj(*subvol_info);
1961 if (!subvol_info) {
1962 btrfs_free_path(path);
1963 return -ENOMEM;
1964 }
1965
1966 fs_info = BTRFS_I(inode)->root->fs_info;
1967
1968 /* Get root_item of inode's subvolume */
1969 key.objectid = btrfs_root_id(BTRFS_I(inode)->root);
1970 root = btrfs_get_fs_root(fs_info, key.objectid, true);
1971 if (IS_ERR(root)) {
1972 ret = PTR_ERR(root);
1973 goto out_free;
1974 }
1975 root_item = &root->root_item;
1976
1977 subvol_info->treeid = key.objectid;
1978
1979 subvol_info->generation = btrfs_root_generation(root_item);
1980 subvol_info->flags = btrfs_root_flags(root_item);
1981
1982 memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE);
1983 memcpy(subvol_info->parent_uuid, root_item->parent_uuid,
1984 BTRFS_UUID_SIZE);
1985 memcpy(subvol_info->received_uuid, root_item->received_uuid,
1986 BTRFS_UUID_SIZE);
1987
1988 subvol_info->ctransid = btrfs_root_ctransid(root_item);
1989 subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime);
1990 subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime);
1991
1992 subvol_info->otransid = btrfs_root_otransid(root_item);
1993 subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime);
1994 subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime);
1995
1996 subvol_info->stransid = btrfs_root_stransid(root_item);
1997 subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime);
1998 subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime);
1999
2000 subvol_info->rtransid = btrfs_root_rtransid(root_item);
2001 subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime);
2002 subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime);
2003
2004 if (key.objectid != BTRFS_FS_TREE_OBJECTID) {
2005 /* Search root tree for ROOT_BACKREF of this subvolume */
2006 key.type = BTRFS_ROOT_BACKREF_KEY;
2007 key.offset = 0;
2008 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
2009 if (ret < 0) {
2010 goto out;
2011 } else if (path->slots[0] >=
2012 btrfs_header_nritems(path->nodes[0])) {
2013 ret = btrfs_next_leaf(fs_info->tree_root, path);
2014 if (ret < 0) {
2015 goto out;
2016 } else if (unlikely(ret > 0)) {
2017 ret = -EUCLEAN;
2018 goto out;
2019 }
2020 }
2021
2022 leaf = path->nodes[0];
2023 slot = path->slots[0];
2024 btrfs_item_key_to_cpu(leaf, &key, slot);
2025 if (key.objectid == subvol_info->treeid &&
2026 key.type == BTRFS_ROOT_BACKREF_KEY) {
2027 subvol_info->parent_id = key.offset;
2028
2029 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2030 subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref);
2031
2032 item_off = btrfs_item_ptr_offset(leaf, slot)
2033 + sizeof(struct btrfs_root_ref);
2034 item_len = btrfs_item_size(leaf, slot)
2035 - sizeof(struct btrfs_root_ref);
2036 read_extent_buffer(leaf, subvol_info->name,
2037 item_off, item_len);
2038 } else {
2039 ret = -ENOENT;
2040 goto out;
2041 }
2042 }
2043
2044 btrfs_free_path(path);
2045 path = NULL;
2046 if (copy_to_user(argp, subvol_info, sizeof(*subvol_info)))
2047 ret = -EFAULT;
2048
2049 out:
2050 btrfs_put_root(root);
2051 out_free:
2052 btrfs_free_path(path);
2053 kfree(subvol_info);
2054 return ret;
2055 }
2056
2057 /*
2058 * Return ROOT_REF information of the subvolume containing this inode
2059 * except the subvolume name.
2060 */
btrfs_ioctl_get_subvol_rootref(struct btrfs_root * root,void __user * argp)2061 static int btrfs_ioctl_get_subvol_rootref(struct btrfs_root *root,
2062 void __user *argp)
2063 {
2064 struct btrfs_ioctl_get_subvol_rootref_args *rootrefs;
2065 struct btrfs_root_ref *rref;
2066 struct btrfs_path *path;
2067 struct btrfs_key key;
2068 struct extent_buffer *leaf;
2069 u64 objectid;
2070 int slot;
2071 int ret;
2072 u8 found;
2073
2074 path = btrfs_alloc_path();
2075 if (!path)
2076 return -ENOMEM;
2077
2078 rootrefs = memdup_user(argp, sizeof(*rootrefs));
2079 if (IS_ERR(rootrefs)) {
2080 btrfs_free_path(path);
2081 return PTR_ERR(rootrefs);
2082 }
2083
2084 objectid = btrfs_root_id(root);
2085 key.objectid = objectid;
2086 key.type = BTRFS_ROOT_REF_KEY;
2087 key.offset = rootrefs->min_treeid;
2088 found = 0;
2089
2090 root = root->fs_info->tree_root;
2091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2092 if (ret < 0) {
2093 goto out;
2094 } else if (path->slots[0] >=
2095 btrfs_header_nritems(path->nodes[0])) {
2096 ret = btrfs_next_leaf(root, path);
2097 if (ret < 0) {
2098 goto out;
2099 } else if (unlikely(ret > 0)) {
2100 ret = -EUCLEAN;
2101 goto out;
2102 }
2103 }
2104 while (1) {
2105 leaf = path->nodes[0];
2106 slot = path->slots[0];
2107
2108 btrfs_item_key_to_cpu(leaf, &key, slot);
2109 if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) {
2110 ret = 0;
2111 goto out;
2112 }
2113
2114 if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) {
2115 ret = -EOVERFLOW;
2116 goto out;
2117 }
2118
2119 rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
2120 rootrefs->rootref[found].treeid = key.offset;
2121 rootrefs->rootref[found].dirid =
2122 btrfs_root_ref_dirid(leaf, rref);
2123 found++;
2124
2125 ret = btrfs_next_item(root, path);
2126 if (ret < 0) {
2127 goto out;
2128 } else if (unlikely(ret > 0)) {
2129 ret = -EUCLEAN;
2130 goto out;
2131 }
2132 }
2133
2134 out:
2135 btrfs_free_path(path);
2136
2137 if (!ret || ret == -EOVERFLOW) {
2138 rootrefs->num_items = found;
2139 /* update min_treeid for next search */
2140 if (found)
2141 rootrefs->min_treeid =
2142 rootrefs->rootref[found - 1].treeid + 1;
2143 if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
2144 ret = -EFAULT;
2145 }
2146
2147 kfree(rootrefs);
2148
2149 return ret;
2150 }
2151
btrfs_ioctl_snap_destroy(struct file * file,void __user * arg,bool destroy_v2)2152 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2153 void __user *arg,
2154 bool destroy_v2)
2155 {
2156 struct dentry *parent = file->f_path.dentry;
2157 struct dentry *dentry;
2158 struct inode *dir = d_inode(parent);
2159 struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
2160 struct inode *inode;
2161 struct btrfs_root *root = BTRFS_I(dir)->root;
2162 struct btrfs_root *dest = NULL;
2163 struct btrfs_ioctl_vol_args *vol_args = NULL;
2164 struct btrfs_ioctl_vol_args_v2 *vol_args2 = NULL;
2165 struct mnt_idmap *idmap = file_mnt_idmap(file);
2166 char *subvol_name, *subvol_name_ptr = NULL;
2167 int ret = 0;
2168 bool destroy_parent = false;
2169
2170 /* We don't support snapshots with extent tree v2 yet. */
2171 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
2172 btrfs_err(fs_info,
2173 "extent tree v2 doesn't support snapshot deletion yet");
2174 return -EOPNOTSUPP;
2175 }
2176
2177 if (destroy_v2) {
2178 vol_args2 = memdup_user(arg, sizeof(*vol_args2));
2179 if (IS_ERR(vol_args2))
2180 return PTR_ERR(vol_args2);
2181
2182 if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
2183 ret = -EOPNOTSUPP;
2184 goto out;
2185 }
2186
2187 /*
2188 * If SPEC_BY_ID is not set, we are looking for the subvolume by
2189 * name, same as v1 currently does.
2190 */
2191 if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
2192 ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2);
2193 if (ret < 0)
2194 goto out;
2195 subvol_name = vol_args2->name;
2196
2197 ret = mnt_want_write_file(file);
2198 if (ret)
2199 goto out;
2200 } else {
2201 struct inode *old_dir;
2202
2203 if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
2204 ret = -EINVAL;
2205 goto out;
2206 }
2207
2208 ret = mnt_want_write_file(file);
2209 if (ret)
2210 goto out;
2211
2212 dentry = btrfs_get_dentry(fs_info->sb,
2213 BTRFS_FIRST_FREE_OBJECTID,
2214 vol_args2->subvolid, 0);
2215 if (IS_ERR(dentry)) {
2216 ret = PTR_ERR(dentry);
2217 goto out_drop_write;
2218 }
2219
2220 /*
2221 * Change the default parent since the subvolume being
2222 * deleted can be outside of the current mount point.
2223 */
2224 parent = btrfs_get_parent(dentry);
2225
2226 /*
2227 * At this point dentry->d_name can point to '/' if the
2228 * subvolume we want to destroy is outsite of the
2229 * current mount point, so we need to release the
2230 * current dentry and execute the lookup to return a new
2231 * one with ->d_name pointing to the
2232 * <mount point>/subvol_name.
2233 */
2234 dput(dentry);
2235 if (IS_ERR(parent)) {
2236 ret = PTR_ERR(parent);
2237 goto out_drop_write;
2238 }
2239 old_dir = dir;
2240 dir = d_inode(parent);
2241
2242 /*
2243 * If v2 was used with SPEC_BY_ID, a new parent was
2244 * allocated since the subvolume can be outside of the
2245 * current mount point. Later on we need to release this
2246 * new parent dentry.
2247 */
2248 destroy_parent = true;
2249
2250 /*
2251 * On idmapped mounts, deletion via subvolid is
2252 * restricted to subvolumes that are immediate
2253 * ancestors of the inode referenced by the file
2254 * descriptor in the ioctl. Otherwise the idmapping
2255 * could potentially be abused to delete subvolumes
2256 * anywhere in the filesystem the user wouldn't be able
2257 * to delete without an idmapped mount.
2258 */
2259 if (old_dir != dir && idmap != &nop_mnt_idmap) {
2260 ret = -EOPNOTSUPP;
2261 goto free_parent;
2262 }
2263
2264 subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
2265 fs_info, vol_args2->subvolid);
2266 if (IS_ERR(subvol_name_ptr)) {
2267 ret = PTR_ERR(subvol_name_ptr);
2268 goto free_parent;
2269 }
2270 /* subvol_name_ptr is already nul terminated */
2271 subvol_name = (char *)kbasename(subvol_name_ptr);
2272 }
2273 } else {
2274 vol_args = memdup_user(arg, sizeof(*vol_args));
2275 if (IS_ERR(vol_args))
2276 return PTR_ERR(vol_args);
2277
2278 ret = btrfs_check_ioctl_vol_args_path(vol_args);
2279 if (ret < 0)
2280 goto out;
2281
2282 subvol_name = vol_args->name;
2283
2284 ret = mnt_want_write_file(file);
2285 if (ret)
2286 goto out;
2287 }
2288
2289 if (strchr(subvol_name, '/') ||
2290 strcmp(subvol_name, "..") == 0) {
2291 ret = -EINVAL;
2292 goto free_subvol_name;
2293 }
2294
2295 if (!S_ISDIR(dir->i_mode)) {
2296 ret = -ENOTDIR;
2297 goto free_subvol_name;
2298 }
2299
2300 dentry = start_removing_killable(idmap, parent, &QSTR(subvol_name));
2301 if (IS_ERR(dentry)) {
2302 ret = PTR_ERR(dentry);
2303 goto out_end_removing;
2304 }
2305
2306 inode = d_inode(dentry);
2307 dest = BTRFS_I(inode)->root;
2308 if (!capable(CAP_SYS_ADMIN)) {
2309 /*
2310 * Regular user. Only allow this with a special mount
2311 * option, when the user has write+exec access to the
2312 * subvol root, and when rmdir(2) would have been
2313 * allowed.
2314 *
2315 * Note that this is _not_ check that the subvol is
2316 * empty or doesn't contain data that we wouldn't
2317 * otherwise be able to delete.
2318 *
2319 * Users who want to delete empty subvols should try
2320 * rmdir(2).
2321 */
2322 ret = -EPERM;
2323 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2324 goto out_end_removing;
2325
2326 /*
2327 * Do not allow deletion if the parent dir is the same
2328 * as the dir to be deleted. That means the ioctl
2329 * must be called on the dentry referencing the root
2330 * of the subvol, not a random directory contained
2331 * within it.
2332 */
2333 ret = -EINVAL;
2334 if (root == dest)
2335 goto out_end_removing;
2336
2337 ret = inode_permission(idmap, inode, MAY_WRITE | MAY_EXEC);
2338 if (ret)
2339 goto out_end_removing;
2340 }
2341
2342 /* check if subvolume may be deleted by a user */
2343 ret = may_delete_dentry(idmap, dir, dentry, true);
2344 if (ret)
2345 goto out_end_removing;
2346
2347 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
2348 ret = -EINVAL;
2349 goto out_end_removing;
2350 }
2351
2352 btrfs_inode_lock(BTRFS_I(inode), 0);
2353 ret = btrfs_delete_subvolume(BTRFS_I(dir), dentry);
2354 btrfs_inode_unlock(BTRFS_I(inode), 0);
2355 if (!ret)
2356 d_delete_notify(dir, dentry);
2357
2358 out_end_removing:
2359 end_removing(dentry);
2360 free_subvol_name:
2361 kfree(subvol_name_ptr);
2362 free_parent:
2363 if (destroy_parent)
2364 dput(parent);
2365 out_drop_write:
2366 mnt_drop_write_file(file);
2367 out:
2368 kfree(vol_args2);
2369 kfree(vol_args);
2370 return ret;
2371 }
2372
btrfs_ioctl_defrag(struct file * file,void __user * argp)2373 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
2374 {
2375 struct inode *inode = file_inode(file);
2376 struct btrfs_root *root = BTRFS_I(inode)->root;
2377 struct btrfs_ioctl_defrag_range_args range = {0};
2378 int ret;
2379
2380 ret = mnt_want_write_file(file);
2381 if (ret)
2382 return ret;
2383
2384 if (btrfs_root_readonly(root)) {
2385 ret = -EROFS;
2386 goto out;
2387 }
2388
2389 switch (inode->i_mode & S_IFMT) {
2390 case S_IFDIR:
2391 if (!capable(CAP_SYS_ADMIN)) {
2392 ret = -EPERM;
2393 goto out;
2394 }
2395 ret = btrfs_defrag_root(root);
2396 break;
2397 case S_IFREG:
2398 /*
2399 * Note that this does not check the file descriptor for write
2400 * access. This prevents defragmenting executables that are
2401 * running and allows defrag on files open in read-only mode.
2402 */
2403 if (!capable(CAP_SYS_ADMIN) &&
2404 inode_permission(&nop_mnt_idmap, inode, MAY_WRITE)) {
2405 ret = -EPERM;
2406 goto out;
2407 }
2408
2409 /*
2410 * Don't allow defrag on pre-content watched files, as it could
2411 * populate the page cache with 0's via readahead.
2412 */
2413 if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
2414 ret = -EINVAL;
2415 goto out;
2416 }
2417
2418 if (argp) {
2419 if (copy_from_user(&range, argp, sizeof(range))) {
2420 ret = -EFAULT;
2421 goto out;
2422 }
2423 if (range.flags & ~BTRFS_DEFRAG_RANGE_FLAGS_SUPP) {
2424 ret = -EOPNOTSUPP;
2425 goto out;
2426 }
2427 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) &&
2428 (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
2429 ret = -EINVAL;
2430 goto out;
2431 }
2432 /* Compression or no-compression require to start the IO. */
2433 if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) ||
2434 (range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
2435 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
2436 range.extent_thresh = (u32)-1;
2437 }
2438 } else {
2439 /* the rest are all set to zero by kzalloc */
2440 range.len = (u64)-1;
2441 }
2442 ret = btrfs_defrag_file(BTRFS_I(file_inode(file)), &file->f_ra,
2443 &range, BTRFS_OLDEST_GENERATION, 0);
2444 if (ret > 0)
2445 ret = 0;
2446 break;
2447 default:
2448 ret = -EINVAL;
2449 }
2450 out:
2451 mnt_drop_write_file(file);
2452 return ret;
2453 }
2454
btrfs_ioctl_add_dev(struct btrfs_fs_info * fs_info,void __user * arg)2455 static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
2456 {
2457 struct btrfs_ioctl_vol_args *vol_args;
2458 bool restore_op = false;
2459 int ret;
2460
2461 if (!capable(CAP_SYS_ADMIN))
2462 return -EPERM;
2463
2464 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
2465 btrfs_err(fs_info, "device add not supported on extent tree v2 yet");
2466 return -EINVAL;
2467 }
2468
2469 if (fs_info->fs_devices->temp_fsid) {
2470 btrfs_err(fs_info,
2471 "device add not supported on cloned temp-fsid mount");
2472 return -EINVAL;
2473 }
2474
2475 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) {
2476 if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD))
2477 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2478
2479 /*
2480 * We can do the device add because we have a paused balanced,
2481 * change the exclusive op type and remember we should bring
2482 * back the paused balance
2483 */
2484 fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD;
2485 btrfs_exclop_start_unlock(fs_info);
2486 restore_op = true;
2487 }
2488
2489 vol_args = memdup_user(arg, sizeof(*vol_args));
2490 if (IS_ERR(vol_args)) {
2491 ret = PTR_ERR(vol_args);
2492 goto out;
2493 }
2494
2495 ret = btrfs_check_ioctl_vol_args_path(vol_args);
2496 if (ret < 0)
2497 goto out_free;
2498
2499 ret = btrfs_init_new_device(fs_info, vol_args->name);
2500
2501 if (!ret)
2502 btrfs_info(fs_info, "disk added %s", vol_args->name);
2503
2504 out_free:
2505 kfree(vol_args);
2506 out:
2507 if (restore_op)
2508 btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE_PAUSED);
2509 else
2510 btrfs_exclop_finish(fs_info);
2511 return ret;
2512 }
2513
btrfs_ioctl_rm_dev_v2(struct file * file,void __user * arg)2514 static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2515 {
2516 BTRFS_DEV_LOOKUP_ARGS(args);
2517 struct inode *inode = file_inode(file);
2518 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2519 struct btrfs_ioctl_vol_args_v2 *vol_args;
2520 struct file *bdev_file = NULL;
2521 int ret;
2522 bool cancel = false;
2523
2524 if (!capable(CAP_SYS_ADMIN))
2525 return -EPERM;
2526
2527 vol_args = memdup_user(arg, sizeof(*vol_args));
2528 if (IS_ERR(vol_args))
2529 return PTR_ERR(vol_args);
2530
2531 if (vol_args->flags & ~BTRFS_DEVICE_REMOVE_ARGS_MASK) {
2532 ret = -EOPNOTSUPP;
2533 goto out;
2534 }
2535
2536 ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args);
2537 if (ret < 0)
2538 goto out;
2539
2540 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
2541 args.devid = vol_args->devid;
2542 } else if (!strcmp("cancel", vol_args->name)) {
2543 cancel = true;
2544 } else {
2545 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
2546 if (ret)
2547 goto out;
2548 }
2549
2550 ret = mnt_want_write_file(file);
2551 if (ret)
2552 goto out;
2553
2554 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
2555 cancel);
2556 if (ret)
2557 goto err_drop;
2558
2559 /* Exclusive operation is now claimed */
2560 ret = btrfs_rm_device(fs_info, &args, &bdev_file);
2561
2562 btrfs_exclop_finish(fs_info);
2563
2564 if (!ret) {
2565 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
2566 btrfs_info(fs_info, "device deleted: id %llu",
2567 vol_args->devid);
2568 else
2569 btrfs_info(fs_info, "device deleted: %s",
2570 vol_args->name);
2571 }
2572 err_drop:
2573 mnt_drop_write_file(file);
2574 if (bdev_file)
2575 bdev_fput(bdev_file);
2576 out:
2577 btrfs_put_dev_args_from_path(&args);
2578 kfree(vol_args);
2579 return ret;
2580 }
2581
btrfs_ioctl_rm_dev(struct file * file,void __user * arg)2582 static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2583 {
2584 BTRFS_DEV_LOOKUP_ARGS(args);
2585 struct inode *inode = file_inode(file);
2586 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2587 struct btrfs_ioctl_vol_args *vol_args;
2588 struct file *bdev_file = NULL;
2589 int ret;
2590 bool cancel = false;
2591
2592 if (!capable(CAP_SYS_ADMIN))
2593 return -EPERM;
2594
2595 vol_args = memdup_user(arg, sizeof(*vol_args));
2596 if (IS_ERR(vol_args))
2597 return PTR_ERR(vol_args);
2598
2599 ret = btrfs_check_ioctl_vol_args_path(vol_args);
2600 if (ret < 0)
2601 goto out_free;
2602
2603 if (!strcmp("cancel", vol_args->name)) {
2604 cancel = true;
2605 } else {
2606 ret = btrfs_get_dev_args_from_path(fs_info, &args, vol_args->name);
2607 if (ret)
2608 goto out;
2609 }
2610
2611 ret = mnt_want_write_file(file);
2612 if (ret)
2613 goto out;
2614
2615 ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_DEV_REMOVE,
2616 cancel);
2617 if (ret == 0) {
2618 ret = btrfs_rm_device(fs_info, &args, &bdev_file);
2619 if (!ret)
2620 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
2621 btrfs_exclop_finish(fs_info);
2622 }
2623
2624 mnt_drop_write_file(file);
2625 if (bdev_file)
2626 bdev_fput(bdev_file);
2627 out:
2628 btrfs_put_dev_args_from_path(&args);
2629 out_free:
2630 kfree(vol_args);
2631 return ret;
2632 }
2633
btrfs_ioctl_fs_info(const struct btrfs_fs_info * fs_info,void __user * arg)2634 static long btrfs_ioctl_fs_info(const struct btrfs_fs_info *fs_info,
2635 void __user *arg)
2636 {
2637 struct btrfs_ioctl_fs_info_args *fi_args;
2638 struct btrfs_device *device;
2639 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2640 u64 flags_in;
2641 int ret = 0;
2642
2643 fi_args = memdup_user(arg, sizeof(*fi_args));
2644 if (IS_ERR(fi_args))
2645 return PTR_ERR(fi_args);
2646
2647 flags_in = fi_args->flags;
2648 memset(fi_args, 0, sizeof(*fi_args));
2649
2650 rcu_read_lock();
2651 fi_args->num_devices = fs_devices->num_devices;
2652
2653 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2654 if (device->devid > fi_args->max_id)
2655 fi_args->max_id = device->devid;
2656 }
2657 rcu_read_unlock();
2658
2659 memcpy(&fi_args->fsid, fs_devices->fsid, sizeof(fi_args->fsid));
2660 fi_args->nodesize = fs_info->nodesize;
2661 fi_args->sectorsize = fs_info->sectorsize;
2662 fi_args->clone_alignment = fs_info->sectorsize;
2663
2664 if (flags_in & BTRFS_FS_INFO_FLAG_CSUM_INFO) {
2665 fi_args->csum_type = btrfs_super_csum_type(fs_info->super_copy);
2666 fi_args->csum_size = btrfs_super_csum_size(fs_info->super_copy);
2667 fi_args->flags |= BTRFS_FS_INFO_FLAG_CSUM_INFO;
2668 }
2669
2670 if (flags_in & BTRFS_FS_INFO_FLAG_GENERATION) {
2671 fi_args->generation = btrfs_get_fs_generation(fs_info);
2672 fi_args->flags |= BTRFS_FS_INFO_FLAG_GENERATION;
2673 }
2674
2675 if (flags_in & BTRFS_FS_INFO_FLAG_METADATA_UUID) {
2676 memcpy(&fi_args->metadata_uuid, fs_devices->metadata_uuid,
2677 sizeof(fi_args->metadata_uuid));
2678 fi_args->flags |= BTRFS_FS_INFO_FLAG_METADATA_UUID;
2679 }
2680
2681 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2682 ret = -EFAULT;
2683
2684 kfree(fi_args);
2685 return ret;
2686 }
2687
btrfs_ioctl_dev_info(const struct btrfs_fs_info * fs_info,void __user * arg)2688 static long btrfs_ioctl_dev_info(const struct btrfs_fs_info *fs_info,
2689 void __user *arg)
2690 {
2691 BTRFS_DEV_LOOKUP_ARGS(args);
2692 struct btrfs_ioctl_dev_info_args *di_args;
2693 struct btrfs_device *dev;
2694 int ret = 0;
2695
2696 di_args = memdup_user(arg, sizeof(*di_args));
2697 if (IS_ERR(di_args))
2698 return PTR_ERR(di_args);
2699
2700 args.devid = di_args->devid;
2701 if (!btrfs_is_empty_uuid(di_args->uuid))
2702 args.uuid = di_args->uuid;
2703
2704 rcu_read_lock();
2705 dev = btrfs_find_device(fs_info->fs_devices, &args);
2706 if (!dev) {
2707 ret = -ENODEV;
2708 goto out;
2709 }
2710
2711 di_args->devid = dev->devid;
2712 di_args->bytes_used = btrfs_device_get_bytes_used(dev);
2713 di_args->total_bytes = btrfs_device_get_total_bytes(dev);
2714 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2715 memcpy(di_args->fsid, dev->fs_devices->fsid, BTRFS_UUID_SIZE);
2716 if (dev->name)
2717 strscpy(di_args->path, btrfs_dev_name(dev), sizeof(di_args->path));
2718 else
2719 di_args->path[0] = '\0';
2720
2721 out:
2722 rcu_read_unlock();
2723 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2724 ret = -EFAULT;
2725
2726 kfree(di_args);
2727 return ret;
2728 }
2729
btrfs_ioctl_default_subvol(struct file * file,void __user * argp)2730 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2731 {
2732 struct inode *inode = file_inode(file);
2733 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
2734 struct btrfs_root *root = BTRFS_I(inode)->root;
2735 struct btrfs_root *new_root;
2736 struct btrfs_dir_item *di;
2737 struct btrfs_trans_handle *trans;
2738 struct btrfs_path *path = NULL;
2739 struct btrfs_disk_key disk_key;
2740 struct fscrypt_str name = FSTR_INIT("default", 7);
2741 u64 objectid = 0;
2742 u64 dir_id;
2743 int ret;
2744
2745 if (!capable(CAP_SYS_ADMIN))
2746 return -EPERM;
2747
2748 ret = mnt_want_write_file(file);
2749 if (ret)
2750 return ret;
2751
2752 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2753 ret = -EFAULT;
2754 goto out;
2755 }
2756
2757 if (!objectid)
2758 objectid = BTRFS_FS_TREE_OBJECTID;
2759
2760 new_root = btrfs_get_fs_root(fs_info, objectid, true);
2761 if (IS_ERR(new_root)) {
2762 ret = PTR_ERR(new_root);
2763 goto out;
2764 }
2765 if (!btrfs_is_fstree(btrfs_root_id(new_root))) {
2766 ret = -ENOENT;
2767 goto out_free;
2768 }
2769
2770 path = btrfs_alloc_path();
2771 if (!path) {
2772 ret = -ENOMEM;
2773 goto out_free;
2774 }
2775
2776 trans = btrfs_start_transaction(root, 1);
2777 if (IS_ERR(trans)) {
2778 ret = PTR_ERR(trans);
2779 goto out_free;
2780 }
2781
2782 dir_id = btrfs_super_root_dir(fs_info->super_copy);
2783 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
2784 dir_id, &name, 1);
2785 if (IS_ERR_OR_NULL(di)) {
2786 btrfs_release_path(path);
2787 btrfs_end_transaction(trans);
2788 btrfs_err(fs_info,
2789 "Umm, you don't have the default diritem, this isn't going to work");
2790 ret = -ENOENT;
2791 goto out_free;
2792 }
2793
2794 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2795 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2796 btrfs_release_path(path);
2797
2798 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
2799 btrfs_end_transaction(trans);
2800 out_free:
2801 btrfs_put_root(new_root);
2802 btrfs_free_path(path);
2803 out:
2804 mnt_drop_write_file(file);
2805 return ret;
2806 }
2807
get_block_group_info(struct list_head * groups_list,struct btrfs_ioctl_space_info * space)2808 static void get_block_group_info(struct list_head *groups_list,
2809 struct btrfs_ioctl_space_info *space)
2810 {
2811 struct btrfs_block_group *block_group;
2812
2813 space->total_bytes = 0;
2814 space->used_bytes = 0;
2815 space->flags = 0;
2816 list_for_each_entry(block_group, groups_list, list) {
2817 space->flags = block_group->flags;
2818 space->total_bytes += block_group->length;
2819 space->used_bytes += block_group->used;
2820 }
2821 }
2822
btrfs_ioctl_space_info(struct btrfs_fs_info * fs_info,void __user * arg)2823 static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
2824 void __user *arg)
2825 {
2826 struct btrfs_ioctl_space_args space_args = { 0 };
2827 struct btrfs_ioctl_space_info space;
2828 struct btrfs_ioctl_space_info *dest;
2829 struct btrfs_ioctl_space_info AUTO_KFREE(dest_orig);
2830 struct btrfs_ioctl_space_info __user *user_dest;
2831 struct btrfs_space_info *info;
2832 static const u64 types[] = {
2833 BTRFS_BLOCK_GROUP_DATA,
2834 BTRFS_BLOCK_GROUP_SYSTEM,
2835 BTRFS_BLOCK_GROUP_METADATA,
2836 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA
2837 };
2838 int num_types = 4;
2839 int alloc_size;
2840 int ret = 0;
2841 u64 slot_count = 0;
2842 int i, c;
2843
2844 if (copy_from_user(&space_args,
2845 (struct btrfs_ioctl_space_args __user *)arg,
2846 sizeof(space_args)))
2847 return -EFAULT;
2848
2849 for (i = 0; i < num_types; i++) {
2850 struct btrfs_space_info *tmp;
2851
2852 info = NULL;
2853 list_for_each_entry(tmp, &fs_info->space_info, list) {
2854 if (tmp->flags == types[i]) {
2855 info = tmp;
2856 break;
2857 }
2858 }
2859
2860 if (!info)
2861 continue;
2862
2863 down_read(&info->groups_sem);
2864 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2865 if (!list_empty(&info->block_groups[c]))
2866 slot_count++;
2867 }
2868 up_read(&info->groups_sem);
2869 }
2870
2871 /*
2872 * Global block reserve, exported as a space_info
2873 */
2874 slot_count++;
2875
2876 /* space_slots == 0 means they are asking for a count */
2877 if (space_args.space_slots == 0) {
2878 space_args.total_spaces = slot_count;
2879 goto out;
2880 }
2881
2882 slot_count = min_t(u64, space_args.space_slots, slot_count);
2883
2884 alloc_size = sizeof(*dest) * slot_count;
2885
2886 /* we generally have at most 6 or so space infos, one for each raid
2887 * level. So, a whole page should be more than enough for everyone
2888 */
2889 if (alloc_size > PAGE_SIZE)
2890 return -ENOMEM;
2891
2892 space_args.total_spaces = 0;
2893 dest = kmalloc(alloc_size, GFP_KERNEL);
2894 if (!dest)
2895 return -ENOMEM;
2896 dest_orig = dest;
2897
2898 /* now we have a buffer to copy into */
2899 for (i = 0; i < num_types; i++) {
2900 struct btrfs_space_info *tmp;
2901
2902 if (!slot_count)
2903 break;
2904
2905 info = NULL;
2906 list_for_each_entry(tmp, &fs_info->space_info, list) {
2907 if (tmp->flags == types[i]) {
2908 info = tmp;
2909 break;
2910 }
2911 }
2912
2913 if (!info)
2914 continue;
2915 down_read(&info->groups_sem);
2916 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2917 if (!list_empty(&info->block_groups[c])) {
2918 get_block_group_info(&info->block_groups[c],
2919 &space);
2920 memcpy(dest, &space, sizeof(space));
2921 dest++;
2922 space_args.total_spaces++;
2923 slot_count--;
2924 }
2925 if (!slot_count)
2926 break;
2927 }
2928 up_read(&info->groups_sem);
2929 }
2930
2931 /*
2932 * Add global block reserve
2933 */
2934 if (slot_count) {
2935 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2936
2937 spin_lock(&block_rsv->lock);
2938 space.total_bytes = block_rsv->size;
2939 space.used_bytes = block_rsv->size - block_rsv->reserved;
2940 spin_unlock(&block_rsv->lock);
2941 space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV;
2942 memcpy(dest, &space, sizeof(space));
2943 space_args.total_spaces++;
2944 }
2945
2946 user_dest = (struct btrfs_ioctl_space_info __user *)
2947 (arg + sizeof(struct btrfs_ioctl_space_args));
2948
2949 if (copy_to_user(user_dest, dest_orig, alloc_size))
2950 return -EFAULT;
2951
2952 out:
2953 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2954 ret = -EFAULT;
2955
2956 return ret;
2957 }
2958
btrfs_ioctl_start_sync(struct btrfs_root * root,void __user * argp)2959 static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
2960 void __user *argp)
2961 {
2962 struct btrfs_trans_handle *trans;
2963 u64 transid;
2964
2965 /*
2966 * Start orphan cleanup here for the given root in case it hasn't been
2967 * started already by other means. Errors are handled in the other
2968 * functions during transaction commit.
2969 */
2970 btrfs_orphan_cleanup(root);
2971
2972 trans = btrfs_attach_transaction_barrier(root);
2973 if (IS_ERR(trans)) {
2974 if (PTR_ERR(trans) != -ENOENT)
2975 return PTR_ERR(trans);
2976
2977 /* No running transaction, don't bother */
2978 transid = btrfs_get_last_trans_committed(root->fs_info);
2979 goto out;
2980 }
2981 transid = trans->transid;
2982 btrfs_commit_transaction_async(trans);
2983 out:
2984 if (argp)
2985 if (copy_to_user(argp, &transid, sizeof(transid)))
2986 return -EFAULT;
2987 return 0;
2988 }
2989
btrfs_ioctl_wait_sync(struct btrfs_fs_info * fs_info,void __user * argp)2990 static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
2991 void __user *argp)
2992 {
2993 /* By default wait for the current transaction. */
2994 u64 transid = 0;
2995
2996 if (argp)
2997 if (copy_from_user(&transid, argp, sizeof(transid)))
2998 return -EFAULT;
2999
3000 return btrfs_wait_for_commit(fs_info, transid);
3001 }
3002
btrfs_ioctl_scrub(struct file * file,void __user * arg)3003 static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
3004 {
3005 struct btrfs_fs_info *fs_info = inode_to_fs_info(file_inode(file));
3006 struct btrfs_ioctl_scrub_args *sa;
3007 int ret;
3008
3009 if (!capable(CAP_SYS_ADMIN))
3010 return -EPERM;
3011
3012 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
3013 btrfs_err(fs_info, "scrub: extent tree v2 not yet supported");
3014 return -EINVAL;
3015 }
3016
3017 sa = memdup_user(arg, sizeof(*sa));
3018 if (IS_ERR(sa))
3019 return PTR_ERR(sa);
3020
3021 if (sa->flags & ~BTRFS_SCRUB_SUPPORTED_FLAGS) {
3022 ret = -EOPNOTSUPP;
3023 goto out;
3024 }
3025
3026 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3027 ret = mnt_want_write_file(file);
3028 if (ret)
3029 goto out;
3030 }
3031
3032 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
3033 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3034 0);
3035
3036 /*
3037 * Copy scrub args to user space even if btrfs_scrub_dev() returned an
3038 * error. This is important as it allows user space to know how much
3039 * progress scrub has done. For example, if scrub is canceled we get
3040 * -ECANCELED from btrfs_scrub_dev() and return that error back to user
3041 * space. Later user space can inspect the progress from the structure
3042 * btrfs_ioctl_scrub_args and resume scrub from where it left off
3043 * previously (btrfs-progs does this).
3044 * If we fail to copy the btrfs_ioctl_scrub_args structure to user space
3045 * then return -EFAULT to signal the structure was not copied or it may
3046 * be corrupt and unreliable due to a partial copy.
3047 */
3048 if (copy_to_user(arg, sa, sizeof(*sa)))
3049 ret = -EFAULT;
3050
3051 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3052 mnt_drop_write_file(file);
3053 out:
3054 kfree(sa);
3055 return ret;
3056 }
3057
btrfs_ioctl_scrub_cancel(struct btrfs_fs_info * fs_info)3058 static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
3059 {
3060 if (!capable(CAP_SYS_ADMIN))
3061 return -EPERM;
3062
3063 return btrfs_scrub_cancel(fs_info);
3064 }
3065
btrfs_ioctl_scrub_progress(struct btrfs_fs_info * fs_info,void __user * arg)3066 static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
3067 void __user *arg)
3068 {
3069 struct btrfs_ioctl_scrub_args *sa;
3070 int ret;
3071
3072 if (!capable(CAP_SYS_ADMIN))
3073 return -EPERM;
3074
3075 sa = memdup_user(arg, sizeof(*sa));
3076 if (IS_ERR(sa))
3077 return PTR_ERR(sa);
3078
3079 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
3080
3081 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3082 ret = -EFAULT;
3083
3084 kfree(sa);
3085 return ret;
3086 }
3087
btrfs_ioctl_get_dev_stats(struct btrfs_fs_info * fs_info,void __user * arg)3088 static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
3089 void __user *arg)
3090 {
3091 struct btrfs_ioctl_get_dev_stats *sa;
3092 int ret;
3093
3094 sa = memdup_user(arg, sizeof(*sa));
3095 if (IS_ERR(sa))
3096 return PTR_ERR(sa);
3097
3098 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3099 kfree(sa);
3100 return -EPERM;
3101 }
3102
3103 ret = btrfs_get_dev_stats(fs_info, sa);
3104
3105 if (ret == 0 && copy_to_user(arg, sa, sizeof(*sa)))
3106 ret = -EFAULT;
3107
3108 kfree(sa);
3109 return ret;
3110 }
3111
btrfs_ioctl_dev_replace(struct btrfs_fs_info * fs_info,void __user * arg)3112 static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
3113 void __user *arg)
3114 {
3115 struct btrfs_ioctl_dev_replace_args *p;
3116 int ret;
3117
3118 if (!capable(CAP_SYS_ADMIN))
3119 return -EPERM;
3120
3121 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
3122 btrfs_err(fs_info, "device replace not supported on extent tree v2 yet");
3123 return -EINVAL;
3124 }
3125
3126 p = memdup_user(arg, sizeof(*p));
3127 if (IS_ERR(p))
3128 return PTR_ERR(p);
3129
3130 switch (p->cmd) {
3131 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3132 if (sb_rdonly(fs_info->sb)) {
3133 ret = -EROFS;
3134 goto out;
3135 }
3136 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
3137 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3138 } else {
3139 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
3140 btrfs_exclop_finish(fs_info);
3141 }
3142 break;
3143 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3144 btrfs_dev_replace_status(fs_info, p);
3145 ret = 0;
3146 break;
3147 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3148 p->result = btrfs_dev_replace_cancel(fs_info);
3149 ret = 0;
3150 break;
3151 default:
3152 ret = -EINVAL;
3153 break;
3154 }
3155
3156 if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
3157 ret = -EFAULT;
3158 out:
3159 kfree(p);
3160 return ret;
3161 }
3162
btrfs_ioctl_ino_to_path(struct btrfs_root * root,void __user * arg)3163 static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3164 {
3165 int ret = 0;
3166 int i;
3167 u64 rel_ptr;
3168 int size;
3169 struct btrfs_ioctl_ino_path_args *ipa = NULL;
3170 struct inode_fs_paths *ipath __free(inode_fs_paths) = NULL;
3171 struct btrfs_path *path;
3172
3173 if (!capable(CAP_DAC_READ_SEARCH))
3174 return -EPERM;
3175
3176 path = btrfs_alloc_path();
3177 if (!path) {
3178 ret = -ENOMEM;
3179 goto out;
3180 }
3181
3182 ipa = memdup_user(arg, sizeof(*ipa));
3183 if (IS_ERR(ipa)) {
3184 ret = PTR_ERR(ipa);
3185 ipa = NULL;
3186 goto out;
3187 }
3188
3189 size = min_t(u32, ipa->size, 4096);
3190 ipath = init_ipath(size, root, path);
3191 if (IS_ERR(ipath)) {
3192 ret = PTR_ERR(ipath);
3193 ipath = NULL;
3194 goto out;
3195 }
3196
3197 ret = paths_from_inode(ipa->inum, ipath);
3198 if (ret < 0)
3199 goto out;
3200
3201 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
3202 rel_ptr = ipath->fspath->val[i] -
3203 (u64)(unsigned long)ipath->fspath->val;
3204 ipath->fspath->val[i] = rel_ptr;
3205 }
3206
3207 btrfs_free_path(path);
3208 path = NULL;
3209 ret = copy_to_user((void __user *)(unsigned long)ipa->fspath,
3210 ipath->fspath, size);
3211 if (ret) {
3212 ret = -EFAULT;
3213 goto out;
3214 }
3215
3216 out:
3217 btrfs_free_path(path);
3218 kfree(ipa);
3219
3220 return ret;
3221 }
3222
btrfs_ioctl_logical_to_ino(struct btrfs_fs_info * fs_info,void __user * arg,int version)3223 static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
3224 void __user *arg, int version)
3225 {
3226 int ret = 0;
3227 int size;
3228 struct btrfs_ioctl_logical_ino_args *loi;
3229 struct btrfs_data_container *inodes = NULL;
3230 bool ignore_offset;
3231
3232 if (!capable(CAP_SYS_ADMIN))
3233 return -EPERM;
3234
3235 loi = memdup_user(arg, sizeof(*loi));
3236 if (IS_ERR(loi))
3237 return PTR_ERR(loi);
3238
3239 if (version == 1) {
3240 ignore_offset = false;
3241 size = min_t(u32, loi->size, SZ_64K);
3242 } else {
3243 /* All reserved bits must be 0 for now */
3244 if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
3245 ret = -EINVAL;
3246 goto out_loi;
3247 }
3248 /* Only accept flags we have defined so far */
3249 if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
3250 ret = -EINVAL;
3251 goto out_loi;
3252 }
3253 ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
3254 size = min_t(u32, loi->size, SZ_16M);
3255 }
3256
3257 inodes = init_data_container(size);
3258 if (IS_ERR(inodes)) {
3259 ret = PTR_ERR(inodes);
3260 goto out_loi;
3261 }
3262
3263 ret = iterate_inodes_from_logical(loi->logical, fs_info, inodes, ignore_offset);
3264 if (ret == -EINVAL)
3265 ret = -ENOENT;
3266 if (ret < 0)
3267 goto out;
3268
3269 ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes,
3270 size);
3271 if (ret)
3272 ret = -EFAULT;
3273
3274 out:
3275 kvfree(inodes);
3276 out_loi:
3277 kfree(loi);
3278
3279 return ret;
3280 }
3281
btrfs_update_ioctl_balance_args(struct btrfs_fs_info * fs_info,struct btrfs_ioctl_balance_args * bargs)3282 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
3283 struct btrfs_ioctl_balance_args *bargs)
3284 {
3285 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3286
3287 bargs->flags = bctl->flags;
3288
3289 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags))
3290 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3291 if (atomic_read(&fs_info->balance_pause_req))
3292 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
3293 if (atomic_read(&fs_info->balance_cancel_req))
3294 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
3295
3296 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3297 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3298 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
3299
3300 spin_lock(&fs_info->balance_lock);
3301 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3302 spin_unlock(&fs_info->balance_lock);
3303 }
3304
3305 /*
3306 * Try to acquire fs_info::balance_mutex as well as set BTRFS_EXLCOP_BALANCE as
3307 * required.
3308 *
3309 * @fs_info: the filesystem
3310 * @excl_acquired: ptr to boolean value which is set to false in case balance
3311 * is being resumed
3312 *
3313 * Return 0 on success in which case both fs_info::balance is acquired as well
3314 * as exclusive ops are blocked. In case of failure return an error code.
3315 */
btrfs_try_lock_balance(struct btrfs_fs_info * fs_info,bool * excl_acquired)3316 static int btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
3317 {
3318 int ret;
3319
3320 /*
3321 * Exclusive operation is locked. Three possibilities:
3322 * (1) some other op is running
3323 * (2) balance is running
3324 * (3) balance is paused -- special case (think resume)
3325 */
3326 while (1) {
3327 if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
3328 *excl_acquired = true;
3329 mutex_lock(&fs_info->balance_mutex);
3330 return 0;
3331 }
3332
3333 mutex_lock(&fs_info->balance_mutex);
3334 if (fs_info->balance_ctl) {
3335 /* This is either (2) or (3) */
3336 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3337 /* This is (2) */
3338 ret = -EINPROGRESS;
3339 goto out_failure;
3340
3341 } else {
3342 mutex_unlock(&fs_info->balance_mutex);
3343 /*
3344 * Lock released to allow other waiters to
3345 * continue, we'll reexamine the status again.
3346 */
3347 mutex_lock(&fs_info->balance_mutex);
3348
3349 if (fs_info->balance_ctl &&
3350 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
3351 /* This is (3) */
3352 *excl_acquired = false;
3353 return 0;
3354 }
3355 }
3356 } else {
3357 /* This is (1) */
3358 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3359 goto out_failure;
3360 }
3361
3362 mutex_unlock(&fs_info->balance_mutex);
3363 }
3364
3365 out_failure:
3366 mutex_unlock(&fs_info->balance_mutex);
3367 *excl_acquired = false;
3368 return ret;
3369 }
3370
btrfs_ioctl_balance(struct file * file,void __user * arg)3371 static long btrfs_ioctl_balance(struct file *file, void __user *arg)
3372 {
3373 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
3374 struct btrfs_fs_info *fs_info = root->fs_info;
3375 struct btrfs_ioctl_balance_args *bargs;
3376 struct btrfs_balance_control *bctl;
3377 bool need_unlock = true;
3378 int ret;
3379
3380 if (!capable(CAP_SYS_ADMIN))
3381 return -EPERM;
3382
3383 ret = mnt_want_write_file(file);
3384 if (ret)
3385 return ret;
3386
3387 bargs = memdup_user(arg, sizeof(*bargs));
3388 if (IS_ERR(bargs)) {
3389 ret = PTR_ERR(bargs);
3390 bargs = NULL;
3391 goto out;
3392 }
3393
3394 ret = btrfs_try_lock_balance(fs_info, &need_unlock);
3395 if (ret)
3396 goto out;
3397
3398 lockdep_assert_held(&fs_info->balance_mutex);
3399
3400 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3401 if (!fs_info->balance_ctl) {
3402 ret = -ENOTCONN;
3403 goto out_unlock;
3404 }
3405
3406 bctl = fs_info->balance_ctl;
3407 spin_lock(&fs_info->balance_lock);
3408 bctl->flags |= BTRFS_BALANCE_RESUME;
3409 spin_unlock(&fs_info->balance_lock);
3410 btrfs_exclop_balance(fs_info, BTRFS_EXCLOP_BALANCE);
3411
3412 goto do_balance;
3413 }
3414
3415 if (bargs->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) {
3416 ret = -EINVAL;
3417 goto out_unlock;
3418 }
3419
3420 if (fs_info->balance_ctl) {
3421 ret = -EINPROGRESS;
3422 goto out_unlock;
3423 }
3424
3425 bctl = kzalloc_obj(*bctl);
3426 if (!bctl) {
3427 ret = -ENOMEM;
3428 goto out_unlock;
3429 }
3430
3431 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3432 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3433 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3434
3435 bctl->flags = bargs->flags;
3436 do_balance:
3437 /*
3438 * Ownership of bctl and exclusive operation goes to btrfs_balance.
3439 * bctl is freed in reset_balance_state, or, if restriper was paused
3440 * all the way until unmount, in free_fs_info. The flag should be
3441 * cleared after reset_balance_state.
3442 */
3443 need_unlock = false;
3444
3445 ret = btrfs_balance(fs_info, bctl, bargs);
3446 bctl = NULL;
3447
3448 if (ret == 0 || ret == -ECANCELED) {
3449 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3450 ret = -EFAULT;
3451 }
3452
3453 kfree(bctl);
3454 out_unlock:
3455 mutex_unlock(&fs_info->balance_mutex);
3456 if (need_unlock)
3457 btrfs_exclop_finish(fs_info);
3458 out:
3459 mnt_drop_write_file(file);
3460 kfree(bargs);
3461 return ret;
3462 }
3463
btrfs_ioctl_balance_ctl(struct btrfs_fs_info * fs_info,int cmd)3464 static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
3465 {
3466 if (!capable(CAP_SYS_ADMIN))
3467 return -EPERM;
3468
3469 switch (cmd) {
3470 case BTRFS_BALANCE_CTL_PAUSE:
3471 return btrfs_pause_balance(fs_info);
3472 case BTRFS_BALANCE_CTL_CANCEL:
3473 return btrfs_cancel_balance(fs_info);
3474 }
3475
3476 return -EINVAL;
3477 }
3478
btrfs_ioctl_balance_progress(struct btrfs_fs_info * fs_info,void __user * arg)3479 static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
3480 void __user *arg)
3481 {
3482 struct btrfs_ioctl_balance_args AUTO_KFREE(bargs);
3483 int ret = 0;
3484
3485 if (!capable(CAP_SYS_ADMIN))
3486 return -EPERM;
3487
3488 mutex_lock(&fs_info->balance_mutex);
3489 if (!fs_info->balance_ctl) {
3490 ret = -ENOTCONN;
3491 goto out;
3492 }
3493
3494 bargs = kzalloc(sizeof(*bargs), GFP_KERNEL);
3495 if (!bargs) {
3496 ret = -ENOMEM;
3497 goto out;
3498 }
3499
3500 btrfs_update_ioctl_balance_args(fs_info, bargs);
3501
3502 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3503 ret = -EFAULT;
3504 out:
3505 mutex_unlock(&fs_info->balance_mutex);
3506 return ret;
3507 }
3508
btrfs_ioctl_quota_ctl(struct file * file,void __user * arg)3509 static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
3510 {
3511 struct inode *inode = file_inode(file);
3512 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3513 struct btrfs_ioctl_quota_ctl_args *sa;
3514 int ret;
3515
3516 if (!capable(CAP_SYS_ADMIN))
3517 return -EPERM;
3518
3519 ret = mnt_want_write_file(file);
3520 if (ret)
3521 return ret;
3522
3523 sa = memdup_user(arg, sizeof(*sa));
3524 if (IS_ERR(sa)) {
3525 ret = PTR_ERR(sa);
3526 goto drop_write;
3527 }
3528
3529 switch (sa->cmd) {
3530 case BTRFS_QUOTA_CTL_ENABLE:
3531 case BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA:
3532 down_write(&fs_info->subvol_sem);
3533 ret = btrfs_quota_enable(fs_info, sa);
3534 up_write(&fs_info->subvol_sem);
3535 break;
3536 case BTRFS_QUOTA_CTL_DISABLE:
3537 /*
3538 * Lock the cleaner mutex to prevent races with concurrent
3539 * relocation, because relocation may be building backrefs for
3540 * blocks of the quota root while we are deleting the root. This
3541 * is like dropping fs roots of deleted snapshots/subvolumes, we
3542 * need the same protection.
3543 *
3544 * This also prevents races between concurrent tasks trying to
3545 * disable quotas, because we will unlock and relock
3546 * qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes.
3547 *
3548 * We take this here because we have the dependency of
3549 *
3550 * inode_lock -> subvol_sem
3551 *
3552 * because of rename. With relocation we can prealloc extents,
3553 * so that makes the dependency chain
3554 *
3555 * cleaner_mutex -> inode_lock -> subvol_sem
3556 *
3557 * so we must take the cleaner_mutex here before we take the
3558 * subvol_sem. The deadlock can't actually happen, but this
3559 * quiets lockdep.
3560 */
3561 mutex_lock(&fs_info->cleaner_mutex);
3562 down_write(&fs_info->subvol_sem);
3563 ret = btrfs_quota_disable(fs_info);
3564 up_write(&fs_info->subvol_sem);
3565 mutex_unlock(&fs_info->cleaner_mutex);
3566 break;
3567 default:
3568 ret = -EINVAL;
3569 break;
3570 }
3571
3572 kfree(sa);
3573 drop_write:
3574 mnt_drop_write_file(file);
3575 return ret;
3576 }
3577
btrfs_ioctl_qgroup_assign(struct file * file,void __user * arg)3578 static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
3579 {
3580 struct inode *inode = file_inode(file);
3581 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3582 struct btrfs_root *root = BTRFS_I(inode)->root;
3583 struct btrfs_ioctl_qgroup_assign_args *sa;
3584 struct btrfs_qgroup_list *prealloc = NULL;
3585 struct btrfs_trans_handle *trans;
3586 int ret;
3587 int err;
3588
3589 if (!capable(CAP_SYS_ADMIN))
3590 return -EPERM;
3591
3592 if (!btrfs_qgroup_enabled(fs_info))
3593 return -ENOTCONN;
3594
3595 ret = mnt_want_write_file(file);
3596 if (ret)
3597 return ret;
3598
3599 sa = memdup_user(arg, sizeof(*sa));
3600 if (IS_ERR(sa)) {
3601 ret = PTR_ERR(sa);
3602 goto drop_write;
3603 }
3604
3605 if (sa->assign) {
3606 prealloc = kzalloc_obj(*prealloc);
3607 if (!prealloc) {
3608 ret = -ENOMEM;
3609 goto out;
3610 }
3611 }
3612
3613 trans = btrfs_join_transaction(root);
3614 if (IS_ERR(trans)) {
3615 ret = PTR_ERR(trans);
3616 goto out;
3617 }
3618
3619 /*
3620 * Prealloc ownership is moved to the relation handler, there it's used
3621 * or freed on error.
3622 */
3623 if (sa->assign) {
3624 ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst, prealloc);
3625 prealloc = NULL;
3626 } else {
3627 ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
3628 }
3629
3630 /* update qgroup status and info */
3631 mutex_lock(&fs_info->qgroup_ioctl_lock);
3632 err = btrfs_run_qgroups(trans);
3633 mutex_unlock(&fs_info->qgroup_ioctl_lock);
3634 if (err < 0)
3635 btrfs_warn(fs_info,
3636 "qgroup status update failed after %s relation, marked as inconsistent",
3637 sa->assign ? "adding" : "deleting");
3638 err = btrfs_end_transaction(trans);
3639 if (err && !ret)
3640 ret = err;
3641
3642 out:
3643 kfree(prealloc);
3644 kfree(sa);
3645 drop_write:
3646 mnt_drop_write_file(file);
3647 return ret;
3648 }
3649
btrfs_ioctl_qgroup_create(struct file * file,void __user * arg)3650 static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
3651 {
3652 struct inode *inode = file_inode(file);
3653 struct btrfs_root *root = BTRFS_I(inode)->root;
3654 struct btrfs_ioctl_qgroup_create_args *sa;
3655 struct btrfs_trans_handle *trans;
3656 int ret;
3657 int err;
3658
3659 if (!capable(CAP_SYS_ADMIN))
3660 return -EPERM;
3661
3662 if (!btrfs_qgroup_enabled(root->fs_info))
3663 return -ENOTCONN;
3664
3665 ret = mnt_want_write_file(file);
3666 if (ret)
3667 return ret;
3668
3669 sa = memdup_user(arg, sizeof(*sa));
3670 if (IS_ERR(sa)) {
3671 ret = PTR_ERR(sa);
3672 goto drop_write;
3673 }
3674
3675 if (!sa->qgroupid) {
3676 ret = -EINVAL;
3677 goto out;
3678 }
3679
3680 if (sa->create && btrfs_is_fstree(sa->qgroupid)) {
3681 ret = -EINVAL;
3682 goto out;
3683 }
3684
3685 trans = btrfs_join_transaction(root);
3686 if (IS_ERR(trans)) {
3687 ret = PTR_ERR(trans);
3688 goto out;
3689 }
3690
3691 if (sa->create) {
3692 ret = btrfs_create_qgroup(trans, sa->qgroupid);
3693 } else {
3694 ret = btrfs_remove_qgroup(trans, sa->qgroupid);
3695 }
3696
3697 err = btrfs_end_transaction(trans);
3698 if (err && !ret)
3699 ret = err;
3700
3701 out:
3702 kfree(sa);
3703 drop_write:
3704 mnt_drop_write_file(file);
3705 return ret;
3706 }
3707
btrfs_ioctl_qgroup_limit(struct file * file,void __user * arg)3708 static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
3709 {
3710 struct inode *inode = file_inode(file);
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
3712 struct btrfs_ioctl_qgroup_limit_args *sa;
3713 struct btrfs_trans_handle *trans;
3714 int ret;
3715 int err;
3716 u64 qgroupid;
3717
3718 if (!capable(CAP_SYS_ADMIN))
3719 return -EPERM;
3720
3721 if (!btrfs_qgroup_enabled(root->fs_info))
3722 return -ENOTCONN;
3723
3724 ret = mnt_want_write_file(file);
3725 if (ret)
3726 return ret;
3727
3728 sa = memdup_user(arg, sizeof(*sa));
3729 if (IS_ERR(sa)) {
3730 ret = PTR_ERR(sa);
3731 goto drop_write;
3732 }
3733
3734 trans = btrfs_join_transaction(root);
3735 if (IS_ERR(trans)) {
3736 ret = PTR_ERR(trans);
3737 goto out;
3738 }
3739
3740 qgroupid = sa->qgroupid;
3741 if (!qgroupid) {
3742 /* take the current subvol as qgroup */
3743 qgroupid = btrfs_root_id(root);
3744 }
3745
3746 ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim);
3747
3748 err = btrfs_end_transaction(trans);
3749 if (err && !ret)
3750 ret = err;
3751
3752 out:
3753 kfree(sa);
3754 drop_write:
3755 mnt_drop_write_file(file);
3756 return ret;
3757 }
3758
btrfs_ioctl_quota_rescan(struct file * file,void __user * arg)3759 static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
3760 {
3761 struct inode *inode = file_inode(file);
3762 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3763 struct btrfs_ioctl_quota_rescan_args *qsa;
3764 int ret;
3765
3766 if (!capable(CAP_SYS_ADMIN))
3767 return -EPERM;
3768
3769 if (!btrfs_qgroup_enabled(fs_info))
3770 return -ENOTCONN;
3771
3772 ret = mnt_want_write_file(file);
3773 if (ret)
3774 return ret;
3775
3776 qsa = memdup_user(arg, sizeof(*qsa));
3777 if (IS_ERR(qsa)) {
3778 ret = PTR_ERR(qsa);
3779 goto drop_write;
3780 }
3781
3782 if (qsa->flags) {
3783 ret = -EINVAL;
3784 goto out;
3785 }
3786
3787 ret = btrfs_qgroup_rescan(fs_info);
3788
3789 out:
3790 kfree(qsa);
3791 drop_write:
3792 mnt_drop_write_file(file);
3793 return ret;
3794 }
3795
btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info * fs_info,void __user * arg)3796 static long btrfs_ioctl_quota_rescan_status(struct btrfs_fs_info *fs_info,
3797 void __user *arg)
3798 {
3799 struct btrfs_ioctl_quota_rescan_args qsa = {0};
3800
3801 if (!capable(CAP_SYS_ADMIN))
3802 return -EPERM;
3803
3804 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3805 qsa.flags = 1;
3806 qsa.progress = fs_info->qgroup_rescan_progress.objectid;
3807 }
3808
3809 if (copy_to_user(arg, &qsa, sizeof(qsa)))
3810 return -EFAULT;
3811
3812 return 0;
3813 }
3814
btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info * fs_info)3815 static long btrfs_ioctl_quota_rescan_wait(struct btrfs_fs_info *fs_info)
3816 {
3817 if (!capable(CAP_SYS_ADMIN))
3818 return -EPERM;
3819
3820 return btrfs_qgroup_wait_for_completion(fs_info, true);
3821 }
3822
_btrfs_ioctl_set_received_subvol(struct file * file,struct mnt_idmap * idmap,struct btrfs_ioctl_received_subvol_args * sa)3823 static long _btrfs_ioctl_set_received_subvol(struct file *file,
3824 struct mnt_idmap *idmap,
3825 struct btrfs_ioctl_received_subvol_args *sa)
3826 {
3827 struct inode *inode = file_inode(file);
3828 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3829 struct btrfs_root *root = BTRFS_I(inode)->root;
3830 struct btrfs_root_item *root_item = &root->root_item;
3831 struct btrfs_trans_handle *trans;
3832 struct timespec64 ct = current_time(inode);
3833 int ret = 0;
3834 int received_uuid_changed;
3835
3836 if (!inode_owner_or_capable(idmap, inode))
3837 return -EPERM;
3838
3839 ret = mnt_want_write_file(file);
3840 if (ret < 0)
3841 return ret;
3842
3843 down_write(&fs_info->subvol_sem);
3844
3845 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
3846 ret = -EINVAL;
3847 goto out;
3848 }
3849
3850 if (btrfs_root_readonly(root)) {
3851 ret = -EROFS;
3852 goto out;
3853 }
3854
3855 /*
3856 * 1 - root item
3857 * 2 - uuid items (received uuid + subvol uuid)
3858 */
3859 trans = btrfs_start_transaction(root, 3);
3860 if (IS_ERR(trans)) {
3861 ret = PTR_ERR(trans);
3862 trans = NULL;
3863 goto out;
3864 }
3865
3866 sa->rtransid = trans->transid;
3867 sa->rtime.sec = ct.tv_sec;
3868 sa->rtime.nsec = ct.tv_nsec;
3869
3870 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
3871 BTRFS_UUID_SIZE);
3872 if (received_uuid_changed &&
3873 !btrfs_is_empty_uuid(root_item->received_uuid)) {
3874 ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid,
3875 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3876 btrfs_root_id(root));
3877 if (unlikely(ret && ret != -ENOENT)) {
3878 btrfs_abort_transaction(trans, ret);
3879 btrfs_end_transaction(trans);
3880 goto out;
3881 }
3882 }
3883 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3884 btrfs_set_root_stransid(root_item, sa->stransid);
3885 btrfs_set_root_rtransid(root_item, sa->rtransid);
3886 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
3887 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
3888 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
3889 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
3890
3891 ret = btrfs_update_root(trans, fs_info->tree_root,
3892 &root->root_key, &root->root_item);
3893 if (ret < 0) {
3894 btrfs_end_transaction(trans);
3895 goto out;
3896 }
3897 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
3898 ret = btrfs_uuid_tree_add(trans, sa->uuid,
3899 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3900 btrfs_root_id(root));
3901 if (unlikely(ret < 0 && ret != -EEXIST)) {
3902 btrfs_abort_transaction(trans, ret);
3903 btrfs_end_transaction(trans);
3904 goto out;
3905 }
3906 }
3907 ret = btrfs_commit_transaction(trans);
3908 out:
3909 up_write(&fs_info->subvol_sem);
3910 mnt_drop_write_file(file);
3911 return ret;
3912 }
3913
3914 #ifdef CONFIG_64BIT
btrfs_ioctl_set_received_subvol_32(struct file * file,void __user * arg)3915 static long btrfs_ioctl_set_received_subvol_32(struct file *file,
3916 void __user *arg)
3917 {
3918 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
3919 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
3920 int ret = 0;
3921
3922 args32 = memdup_user(arg, sizeof(*args32));
3923 if (IS_ERR(args32))
3924 return PTR_ERR(args32);
3925
3926 args64 = kmalloc_obj(*args64);
3927 if (!args64) {
3928 ret = -ENOMEM;
3929 goto out;
3930 }
3931
3932 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
3933 args64->stransid = args32->stransid;
3934 args64->rtransid = args32->rtransid;
3935 args64->stime.sec = args32->stime.sec;
3936 args64->stime.nsec = args32->stime.nsec;
3937 args64->rtime.sec = args32->rtime.sec;
3938 args64->rtime.nsec = args32->rtime.nsec;
3939 args64->flags = args32->flags;
3940
3941 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), args64);
3942 if (ret)
3943 goto out;
3944
3945 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
3946 args32->stransid = args64->stransid;
3947 args32->rtransid = args64->rtransid;
3948 args32->stime.sec = args64->stime.sec;
3949 args32->stime.nsec = args64->stime.nsec;
3950 args32->rtime.sec = args64->rtime.sec;
3951 args32->rtime.nsec = args64->rtime.nsec;
3952 args32->flags = args64->flags;
3953
3954 ret = copy_to_user(arg, args32, sizeof(*args32));
3955 if (ret)
3956 ret = -EFAULT;
3957
3958 out:
3959 kfree(args32);
3960 kfree(args64);
3961 return ret;
3962 }
3963 #endif
3964
btrfs_ioctl_set_received_subvol(struct file * file,void __user * arg)3965 static long btrfs_ioctl_set_received_subvol(struct file *file,
3966 void __user *arg)
3967 {
3968 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3969 int ret = 0;
3970
3971 sa = memdup_user(arg, sizeof(*sa));
3972 if (IS_ERR(sa))
3973 return PTR_ERR(sa);
3974
3975 ret = _btrfs_ioctl_set_received_subvol(file, file_mnt_idmap(file), sa);
3976
3977 if (ret)
3978 goto out;
3979
3980 ret = copy_to_user(arg, sa, sizeof(*sa));
3981 if (ret)
3982 ret = -EFAULT;
3983
3984 out:
3985 kfree(sa);
3986 return ret;
3987 }
3988
btrfs_ioctl_get_fslabel(struct btrfs_fs_info * fs_info,void __user * arg)3989 static int btrfs_ioctl_get_fslabel(struct btrfs_fs_info *fs_info,
3990 void __user *arg)
3991 {
3992 size_t len;
3993 int ret;
3994 char label[BTRFS_LABEL_SIZE];
3995
3996 spin_lock(&fs_info->super_lock);
3997 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
3998 spin_unlock(&fs_info->super_lock);
3999
4000 len = strnlen(label, BTRFS_LABEL_SIZE);
4001
4002 if (len == BTRFS_LABEL_SIZE) {
4003 btrfs_warn(fs_info,
4004 "label is too long, return the first %zu bytes",
4005 --len);
4006 }
4007
4008 ret = copy_to_user(arg, label, len);
4009
4010 return ret ? -EFAULT : 0;
4011 }
4012
btrfs_ioctl_set_fslabel(struct file * file,void __user * arg)4013 static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4014 {
4015 struct inode *inode = file_inode(file);
4016 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
4017 struct btrfs_root *root = BTRFS_I(inode)->root;
4018 struct btrfs_super_block *super_block = fs_info->super_copy;
4019 struct btrfs_trans_handle *trans;
4020 char label[BTRFS_LABEL_SIZE];
4021 int ret;
4022
4023 if (!capable(CAP_SYS_ADMIN))
4024 return -EPERM;
4025
4026 if (copy_from_user(label, arg, sizeof(label)))
4027 return -EFAULT;
4028
4029 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4030 btrfs_err(fs_info,
4031 "unable to set label with more than %d bytes",
4032 BTRFS_LABEL_SIZE - 1);
4033 return -EINVAL;
4034 }
4035
4036 ret = mnt_want_write_file(file);
4037 if (ret)
4038 return ret;
4039
4040 trans = btrfs_start_transaction(root, 0);
4041 if (IS_ERR(trans)) {
4042 ret = PTR_ERR(trans);
4043 goto out_unlock;
4044 }
4045
4046 spin_lock(&fs_info->super_lock);
4047 strscpy(super_block->label, label);
4048 spin_unlock(&fs_info->super_lock);
4049 ret = btrfs_commit_transaction(trans);
4050
4051 out_unlock:
4052 mnt_drop_write_file(file);
4053 return ret;
4054 }
4055
4056 #define INIT_FEATURE_FLAGS(suffix) \
4057 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4058 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4059 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4060
btrfs_ioctl_get_supported_features(void __user * arg)4061 int btrfs_ioctl_get_supported_features(void __user *arg)
4062 {
4063 static const struct btrfs_ioctl_feature_flags features[3] = {
4064 INIT_FEATURE_FLAGS(SUPP),
4065 INIT_FEATURE_FLAGS(SAFE_SET),
4066 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4067 };
4068
4069 if (copy_to_user(arg, &features, sizeof(features)))
4070 return -EFAULT;
4071
4072 return 0;
4073 }
4074
btrfs_ioctl_get_features(struct btrfs_fs_info * fs_info,void __user * arg)4075 static int btrfs_ioctl_get_features(struct btrfs_fs_info *fs_info,
4076 void __user *arg)
4077 {
4078 struct btrfs_super_block *super_block = fs_info->super_copy;
4079 struct btrfs_ioctl_feature_flags features;
4080
4081 features.compat_flags = btrfs_super_compat_flags(super_block);
4082 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4083 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4084
4085 if (copy_to_user(arg, &features, sizeof(features)))
4086 return -EFAULT;
4087
4088 return 0;
4089 }
4090
check_feature_bits(const struct btrfs_fs_info * fs_info,enum btrfs_feature_set set,u64 change_mask,u64 flags,u64 supported_flags,u64 safe_set,u64 safe_clear)4091 static int check_feature_bits(const struct btrfs_fs_info *fs_info,
4092 enum btrfs_feature_set set,
4093 u64 change_mask, u64 flags, u64 supported_flags,
4094 u64 safe_set, u64 safe_clear)
4095 {
4096 const char *type = btrfs_feature_set_name(set);
4097 const char AUTO_KFREE(names);
4098 u64 disallowed, unsupported;
4099 u64 set_mask = flags & change_mask;
4100 u64 clear_mask = ~flags & change_mask;
4101
4102 unsupported = set_mask & ~supported_flags;
4103 if (unsupported) {
4104 names = btrfs_printable_features(set, unsupported);
4105 if (names)
4106 btrfs_warn(fs_info,
4107 "this kernel does not support the %s feature bit%s",
4108 names, strchr(names, ',') ? "s" : "");
4109 else
4110 btrfs_warn(fs_info,
4111 "this kernel does not support %s bits 0x%llx",
4112 type, unsupported);
4113 return -EOPNOTSUPP;
4114 }
4115
4116 disallowed = set_mask & ~safe_set;
4117 if (disallowed) {
4118 names = btrfs_printable_features(set, disallowed);
4119 if (names)
4120 btrfs_warn(fs_info,
4121 "can't set the %s feature bit%s while mounted",
4122 names, strchr(names, ',') ? "s" : "");
4123 else
4124 btrfs_warn(fs_info,
4125 "can't set %s bits 0x%llx while mounted",
4126 type, disallowed);
4127 return -EPERM;
4128 }
4129
4130 disallowed = clear_mask & ~safe_clear;
4131 if (disallowed) {
4132 names = btrfs_printable_features(set, disallowed);
4133 if (names)
4134 btrfs_warn(fs_info,
4135 "can't clear the %s feature bit%s while mounted",
4136 names, strchr(names, ',') ? "s" : "");
4137 else
4138 btrfs_warn(fs_info,
4139 "can't clear %s bits 0x%llx while mounted",
4140 type, disallowed);
4141 return -EPERM;
4142 }
4143
4144 return 0;
4145 }
4146
4147 #define check_feature(fs_info, change_mask, flags, mask_base) \
4148 check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
4149 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4150 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4151 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4152
btrfs_ioctl_set_features(struct file * file,void __user * arg)4153 static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4154 {
4155 struct inode *inode = file_inode(file);
4156 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
4157 struct btrfs_root *root = BTRFS_I(inode)->root;
4158 struct btrfs_super_block *super_block = fs_info->super_copy;
4159 struct btrfs_ioctl_feature_flags flags[2];
4160 struct btrfs_trans_handle *trans;
4161 u64 newflags;
4162 int ret;
4163
4164 if (!capable(CAP_SYS_ADMIN))
4165 return -EPERM;
4166
4167 if (copy_from_user(flags, arg, sizeof(flags)))
4168 return -EFAULT;
4169
4170 /* Nothing to do */
4171 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4172 !flags[0].incompat_flags)
4173 return 0;
4174
4175 ret = check_feature(fs_info, flags[0].compat_flags,
4176 flags[1].compat_flags, COMPAT);
4177 if (ret)
4178 return ret;
4179
4180 ret = check_feature(fs_info, flags[0].compat_ro_flags,
4181 flags[1].compat_ro_flags, COMPAT_RO);
4182 if (ret)
4183 return ret;
4184
4185 ret = check_feature(fs_info, flags[0].incompat_flags,
4186 flags[1].incompat_flags, INCOMPAT);
4187 if (ret)
4188 return ret;
4189
4190 ret = mnt_want_write_file(file);
4191 if (ret)
4192 return ret;
4193
4194 trans = btrfs_start_transaction(root, 0);
4195 if (IS_ERR(trans)) {
4196 ret = PTR_ERR(trans);
4197 goto out_drop_write;
4198 }
4199
4200 spin_lock(&fs_info->super_lock);
4201 newflags = btrfs_super_compat_flags(super_block);
4202 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4203 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4204 btrfs_set_super_compat_flags(super_block, newflags);
4205
4206 newflags = btrfs_super_compat_ro_flags(super_block);
4207 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4208 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4209 btrfs_set_super_compat_ro_flags(super_block, newflags);
4210
4211 newflags = btrfs_super_incompat_flags(super_block);
4212 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4213 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4214 btrfs_set_super_incompat_flags(super_block, newflags);
4215 spin_unlock(&fs_info->super_lock);
4216
4217 ret = btrfs_commit_transaction(trans);
4218 out_drop_write:
4219 mnt_drop_write_file(file);
4220
4221 return ret;
4222 }
4223
_btrfs_ioctl_send(struct btrfs_root * root,void __user * argp,bool compat)4224 static int _btrfs_ioctl_send(struct btrfs_root *root, void __user *argp, bool compat)
4225 {
4226 struct btrfs_ioctl_send_args *arg;
4227 int ret;
4228
4229 if (compat) {
4230 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4231 struct btrfs_ioctl_send_args_32 args32 = { 0 };
4232
4233 ret = copy_from_user(&args32, argp, sizeof(args32));
4234 if (ret)
4235 return -EFAULT;
4236 arg = kzalloc_obj(*arg);
4237 if (!arg)
4238 return -ENOMEM;
4239 arg->send_fd = args32.send_fd;
4240 arg->clone_sources_count = args32.clone_sources_count;
4241 arg->clone_sources = compat_ptr(args32.clone_sources);
4242 arg->parent_root = args32.parent_root;
4243 arg->flags = args32.flags;
4244 arg->version = args32.version;
4245 memcpy(arg->reserved, args32.reserved,
4246 sizeof(args32.reserved));
4247 #else
4248 return -ENOTTY;
4249 #endif
4250 } else {
4251 arg = memdup_user(argp, sizeof(*arg));
4252 if (IS_ERR(arg))
4253 return PTR_ERR(arg);
4254 }
4255 ret = btrfs_ioctl_send(root, arg);
4256 kfree(arg);
4257 return ret;
4258 }
4259
btrfs_ioctl_encoded_read(struct file * file,void __user * argp,bool compat)4260 static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp,
4261 bool compat)
4262 {
4263 struct btrfs_ioctl_encoded_io_args args = { 0 };
4264 size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args,
4265 flags);
4266 size_t copy_end;
4267 struct btrfs_inode *inode = BTRFS_I(file_inode(file));
4268 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4269 struct extent_io_tree *io_tree = &inode->io_tree;
4270 struct iovec iovstack[UIO_FASTIOV];
4271 struct iovec *iov = iovstack;
4272 struct iov_iter iter;
4273 loff_t pos;
4274 struct kiocb kiocb;
4275 ssize_t ret;
4276 u64 disk_bytenr, disk_io_size;
4277 struct extent_state *cached_state = NULL;
4278
4279 if (!capable(CAP_SYS_ADMIN)) {
4280 ret = -EPERM;
4281 goto out_acct;
4282 }
4283
4284 if (compat) {
4285 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4286 struct btrfs_ioctl_encoded_io_args_32 args32;
4287
4288 copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32,
4289 flags);
4290 if (copy_from_user(&args32, argp, copy_end)) {
4291 ret = -EFAULT;
4292 goto out_acct;
4293 }
4294 args.iov = compat_ptr(args32.iov);
4295 args.iovcnt = args32.iovcnt;
4296 args.offset = args32.offset;
4297 args.flags = args32.flags;
4298 #else
4299 return -ENOTTY;
4300 #endif
4301 } else {
4302 copy_end = copy_end_kernel;
4303 if (copy_from_user(&args, argp, copy_end)) {
4304 ret = -EFAULT;
4305 goto out_acct;
4306 }
4307 }
4308 if (args.flags != 0) {
4309 ret = -EINVAL;
4310 goto out_acct;
4311 }
4312
4313 ret = import_iovec(ITER_DEST, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
4314 &iov, &iter);
4315 if (ret < 0)
4316 goto out_acct;
4317
4318 if (iov_iter_count(&iter) == 0) {
4319 ret = 0;
4320 goto out_iov;
4321 }
4322 pos = args.offset;
4323 ret = rw_verify_area(READ, file, &pos, args.len);
4324 if (ret < 0)
4325 goto out_iov;
4326
4327 init_sync_kiocb(&kiocb, file);
4328 kiocb.ki_pos = pos;
4329
4330 ret = btrfs_encoded_read(&kiocb, &iter, &args, &cached_state,
4331 &disk_bytenr, &disk_io_size);
4332
4333 if (ret == -EIOCBQUEUED) {
4334 bool unlocked = false;
4335 u64 start, lockend, count;
4336
4337 start = ALIGN_DOWN(kiocb.ki_pos, fs_info->sectorsize);
4338 lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
4339
4340 if (args.compression)
4341 count = disk_io_size;
4342 else
4343 count = args.len;
4344
4345 ret = btrfs_encoded_read_regular(&kiocb, &iter, start, lockend,
4346 &cached_state, disk_bytenr,
4347 disk_io_size, count,
4348 args.compression, &unlocked);
4349
4350 if (!unlocked) {
4351 btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4352 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4353 }
4354 }
4355
4356 if (ret >= 0) {
4357 fsnotify_access(file);
4358 if (copy_to_user(argp + copy_end,
4359 (char *)&args + copy_end_kernel,
4360 sizeof(args) - copy_end_kernel))
4361 ret = -EFAULT;
4362 }
4363
4364 out_iov:
4365 kfree(iov);
4366 out_acct:
4367 if (ret > 0)
4368 add_rchar(current, ret);
4369 inc_syscr(current);
4370 return ret;
4371 }
4372
btrfs_ioctl_encoded_write(struct file * file,void __user * argp,bool compat)4373 static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool compat)
4374 {
4375 struct btrfs_ioctl_encoded_io_args args;
4376 struct iovec iovstack[UIO_FASTIOV];
4377 struct iovec *iov = iovstack;
4378 struct iov_iter iter;
4379 loff_t pos;
4380 struct kiocb kiocb;
4381 ssize_t ret;
4382
4383 if (!capable(CAP_SYS_ADMIN)) {
4384 ret = -EPERM;
4385 goto out_acct;
4386 }
4387
4388 if (!(file->f_mode & FMODE_WRITE)) {
4389 ret = -EBADF;
4390 goto out_acct;
4391 }
4392
4393 if (compat) {
4394 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4395 struct btrfs_ioctl_encoded_io_args_32 args32;
4396
4397 if (copy_from_user(&args32, argp, sizeof(args32))) {
4398 ret = -EFAULT;
4399 goto out_acct;
4400 }
4401 args.iov = compat_ptr(args32.iov);
4402 args.iovcnt = args32.iovcnt;
4403 args.offset = args32.offset;
4404 args.flags = args32.flags;
4405 args.len = args32.len;
4406 args.unencoded_len = args32.unencoded_len;
4407 args.unencoded_offset = args32.unencoded_offset;
4408 args.compression = args32.compression;
4409 args.encryption = args32.encryption;
4410 memcpy(args.reserved, args32.reserved, sizeof(args.reserved));
4411 #else
4412 return -ENOTTY;
4413 #endif
4414 } else {
4415 if (copy_from_user(&args, argp, sizeof(args))) {
4416 ret = -EFAULT;
4417 goto out_acct;
4418 }
4419 }
4420
4421 ret = -EINVAL;
4422 if (args.flags != 0)
4423 goto out_acct;
4424 if (memchr_inv(args.reserved, 0, sizeof(args.reserved)))
4425 goto out_acct;
4426 if (args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
4427 args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
4428 goto out_acct;
4429 if (args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
4430 args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
4431 goto out_acct;
4432 if (args.unencoded_offset > args.unencoded_len)
4433 goto out_acct;
4434 if (args.len > args.unencoded_len - args.unencoded_offset)
4435 goto out_acct;
4436
4437 ret = import_iovec(ITER_SOURCE, args.iov, args.iovcnt, ARRAY_SIZE(iovstack),
4438 &iov, &iter);
4439 if (ret < 0)
4440 goto out_acct;
4441
4442 if (iov_iter_count(&iter) == 0) {
4443 ret = 0;
4444 goto out_iov;
4445 }
4446 pos = args.offset;
4447 ret = rw_verify_area(WRITE, file, &pos, args.len);
4448 if (ret < 0)
4449 goto out_iov;
4450
4451 init_sync_kiocb(&kiocb, file);
4452 ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
4453 if (ret)
4454 goto out_iov;
4455 kiocb.ki_pos = pos;
4456
4457 file_start_write(file);
4458
4459 ret = btrfs_do_write_iter(&kiocb, &iter, &args);
4460 if (ret > 0)
4461 fsnotify_modify(file);
4462
4463 file_end_write(file);
4464 out_iov:
4465 kfree(iov);
4466 out_acct:
4467 if (ret > 0)
4468 add_wchar(current, ret);
4469 inc_syscw(current);
4470 return ret;
4471 }
4472
4473 struct btrfs_uring_encoded_data {
4474 struct btrfs_ioctl_encoded_io_args args;
4475 struct iovec iovstack[UIO_FASTIOV];
4476 struct iovec *iov;
4477 struct iov_iter iter;
4478 };
4479
4480 /*
4481 * Context that's attached to an encoded read io_uring command, in cmd->pdu. It
4482 * contains the fields in btrfs_uring_read_extent that are necessary to finish
4483 * off and cleanup the I/O in btrfs_uring_read_finished.
4484 */
4485 struct btrfs_uring_priv {
4486 struct io_uring_cmd *cmd;
4487 struct page **pages;
4488 unsigned long nr_pages;
4489 struct kiocb iocb;
4490 struct iovec *iov;
4491 struct iov_iter iter;
4492 struct extent_state *cached_state;
4493 u64 count;
4494 u64 start;
4495 u64 lockend;
4496 int err;
4497 bool compressed;
4498 };
4499
4500 struct io_btrfs_cmd {
4501 struct btrfs_uring_encoded_data *data;
4502 struct btrfs_uring_priv *priv;
4503 };
4504
btrfs_uring_read_finished(struct io_tw_req tw_req,io_tw_token_t tw)4505 static void btrfs_uring_read_finished(struct io_tw_req tw_req, io_tw_token_t tw)
4506 {
4507 struct io_uring_cmd *cmd = io_uring_cmd_from_tw(tw_req);
4508 struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4509 struct btrfs_uring_priv *priv = bc->priv;
4510 struct btrfs_inode *inode = BTRFS_I(file_inode(priv->iocb.ki_filp));
4511 struct extent_io_tree *io_tree = &inode->io_tree;
4512 pgoff_t index;
4513 u64 cur;
4514 size_t page_offset;
4515 ssize_t ret;
4516
4517 /* The inode lock has already been acquired in btrfs_uring_read_extent. */
4518 btrfs_lockdep_inode_acquire(inode, i_rwsem);
4519
4520 if (priv->err) {
4521 ret = priv->err;
4522 goto out;
4523 }
4524
4525 if (priv->compressed) {
4526 index = 0;
4527 page_offset = 0;
4528 } else {
4529 index = (priv->iocb.ki_pos - priv->start) >> PAGE_SHIFT;
4530 page_offset = offset_in_page(priv->iocb.ki_pos - priv->start);
4531 }
4532 cur = 0;
4533 while (cur < priv->count) {
4534 size_t bytes = min_t(size_t, priv->count - cur, PAGE_SIZE - page_offset);
4535
4536 if (copy_page_to_iter(priv->pages[index], page_offset, bytes,
4537 &priv->iter) != bytes) {
4538 ret = -EFAULT;
4539 goto out;
4540 }
4541
4542 index++;
4543 cur += bytes;
4544 page_offset = 0;
4545 }
4546 ret = priv->count;
4547
4548 out:
4549 btrfs_unlock_extent(io_tree, priv->start, priv->lockend, &priv->cached_state);
4550 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4551
4552 io_uring_cmd_done(cmd, ret, IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
4553 add_rchar(current, ret);
4554
4555 for (index = 0; index < priv->nr_pages; index++)
4556 __free_page(priv->pages[index]);
4557
4558 kfree(priv->pages);
4559 kfree(priv->iov);
4560 kfree(priv);
4561 kfree(bc->data);
4562 }
4563
btrfs_uring_read_extent_endio(void * ctx,int err)4564 void btrfs_uring_read_extent_endio(void *ctx, int err)
4565 {
4566 struct btrfs_uring_priv *priv = ctx;
4567 struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(priv->cmd, struct io_btrfs_cmd);
4568
4569 priv->err = err;
4570 bc->priv = priv;
4571
4572 io_uring_cmd_complete_in_task(priv->cmd, btrfs_uring_read_finished);
4573 }
4574
btrfs_uring_read_extent(struct kiocb * iocb,struct iov_iter * iter,u64 start,u64 lockend,struct extent_state * cached_state,u64 disk_bytenr,u64 disk_io_size,size_t count,bool compressed,struct iovec * iov,struct io_uring_cmd * cmd)4575 static int btrfs_uring_read_extent(struct kiocb *iocb, struct iov_iter *iter,
4576 u64 start, u64 lockend,
4577 struct extent_state *cached_state,
4578 u64 disk_bytenr, u64 disk_io_size,
4579 size_t count, bool compressed,
4580 struct iovec *iov, struct io_uring_cmd *cmd)
4581 {
4582 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
4583 struct extent_io_tree *io_tree = &inode->io_tree;
4584 struct page **pages;
4585 struct btrfs_uring_priv *priv = NULL;
4586 unsigned long nr_pages;
4587 int ret;
4588
4589 nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE);
4590 pages = kzalloc_objs(struct page *, nr_pages, GFP_NOFS);
4591 if (!pages)
4592 return -ENOMEM;
4593 ret = btrfs_alloc_page_array(nr_pages, pages, 0);
4594 if (ret) {
4595 ret = -ENOMEM;
4596 goto out_fail;
4597 }
4598
4599 priv = kmalloc_obj(*priv, GFP_NOFS);
4600 if (!priv) {
4601 ret = -ENOMEM;
4602 goto out_fail;
4603 }
4604
4605 priv->iocb = *iocb;
4606 priv->iov = iov;
4607 priv->iter = *iter;
4608 priv->count = count;
4609 priv->cmd = cmd;
4610 priv->cached_state = cached_state;
4611 priv->compressed = compressed;
4612 priv->nr_pages = nr_pages;
4613 priv->pages = pages;
4614 priv->start = start;
4615 priv->lockend = lockend;
4616 priv->err = 0;
4617
4618 ret = btrfs_encoded_read_regular_fill_pages(inode, disk_bytenr,
4619 disk_io_size, pages, priv);
4620 if (ret && ret != -EIOCBQUEUED)
4621 goto out_fail;
4622
4623 /*
4624 * If we return -EIOCBQUEUED, we're deferring the cleanup to
4625 * btrfs_uring_read_finished(), which will handle unlocking the extent
4626 * and inode and freeing the allocations.
4627 */
4628
4629 /*
4630 * We're returning to userspace with the inode lock held, and that's
4631 * okay - it'll get unlocked in a worker thread. Call
4632 * btrfs_lockdep_inode_release() to avoid confusing lockdep.
4633 */
4634 btrfs_lockdep_inode_release(inode, i_rwsem);
4635
4636 return -EIOCBQUEUED;
4637
4638 out_fail:
4639 btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4640 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4641 kfree(priv);
4642 return ret;
4643 }
4644
btrfs_uring_encoded_read(struct io_uring_cmd * cmd,unsigned int issue_flags)4645 static int btrfs_uring_encoded_read(struct io_uring_cmd *cmd, unsigned int issue_flags)
4646 {
4647 struct file *file = cmd->file;
4648 struct btrfs_inode *inode = BTRFS_I(file->f_inode);
4649 struct extent_io_tree *io_tree = &inode->io_tree;
4650 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4651 size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, flags);
4652 size_t copy_end;
4653 int ret;
4654 u64 disk_bytenr, disk_io_size;
4655 loff_t pos;
4656 struct kiocb kiocb;
4657 struct extent_state *cached_state = NULL;
4658 u64 start, lockend;
4659 void __user *sqe_addr;
4660 struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4661 struct btrfs_uring_encoded_data *data = NULL;
4662
4663 if (cmd->flags & IORING_URING_CMD_REISSUE)
4664 data = bc->data;
4665
4666 if (!capable(CAP_SYS_ADMIN)) {
4667 ret = -EPERM;
4668 goto out_acct;
4669 }
4670 sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
4671
4672 if (issue_flags & IO_URING_F_COMPAT) {
4673 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4674 copy_end = offsetofend(struct btrfs_ioctl_encoded_io_args_32, flags);
4675 #else
4676 ret = -ENOTTY;
4677 goto out_acct;
4678 #endif
4679 } else {
4680 copy_end = copy_end_kernel;
4681 }
4682
4683 if (!data) {
4684 data = kzalloc_obj(*data, GFP_NOFS);
4685 if (!data) {
4686 ret = -ENOMEM;
4687 goto out_acct;
4688 }
4689
4690 bc->data = data;
4691
4692 if (issue_flags & IO_URING_F_COMPAT) {
4693 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4694 struct btrfs_ioctl_encoded_io_args_32 args32;
4695
4696 if (copy_from_user(&args32, sqe_addr, copy_end)) {
4697 ret = -EFAULT;
4698 goto out_acct;
4699 }
4700
4701 data->args.iov = compat_ptr(args32.iov);
4702 data->args.iovcnt = args32.iovcnt;
4703 data->args.offset = args32.offset;
4704 data->args.flags = args32.flags;
4705 #endif
4706 } else {
4707 if (copy_from_user(&data->args, sqe_addr, copy_end)) {
4708 ret = -EFAULT;
4709 goto out_acct;
4710 }
4711 }
4712
4713 if (data->args.flags != 0) {
4714 ret = -EINVAL;
4715 goto out_acct;
4716 }
4717
4718 data->iov = data->iovstack;
4719 ret = import_iovec(ITER_DEST, data->args.iov, data->args.iovcnt,
4720 ARRAY_SIZE(data->iovstack), &data->iov,
4721 &data->iter);
4722 if (ret < 0)
4723 goto out_acct;
4724
4725 if (iov_iter_count(&data->iter) == 0) {
4726 ret = 0;
4727 goto out_free;
4728 }
4729 }
4730
4731 pos = data->args.offset;
4732 ret = rw_verify_area(READ, file, &pos, data->args.len);
4733 if (ret < 0)
4734 goto out_free;
4735
4736 init_sync_kiocb(&kiocb, file);
4737 kiocb.ki_pos = pos;
4738
4739 if (issue_flags & IO_URING_F_NONBLOCK)
4740 kiocb.ki_flags |= IOCB_NOWAIT;
4741
4742 start = ALIGN_DOWN(pos, fs_info->sectorsize);
4743 lockend = start + BTRFS_MAX_UNCOMPRESSED - 1;
4744
4745 ret = btrfs_encoded_read(&kiocb, &data->iter, &data->args, &cached_state,
4746 &disk_bytenr, &disk_io_size);
4747 if (ret == -EAGAIN)
4748 goto out_acct;
4749 if (ret < 0 && ret != -EIOCBQUEUED)
4750 goto out_free;
4751
4752 file_accessed(file);
4753
4754 if (copy_to_user(sqe_addr + copy_end,
4755 (const char *)&data->args + copy_end_kernel,
4756 sizeof(data->args) - copy_end_kernel)) {
4757 if (ret == -EIOCBQUEUED) {
4758 btrfs_unlock_extent(io_tree, start, lockend, &cached_state);
4759 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
4760 }
4761 ret = -EFAULT;
4762 goto out_free;
4763 }
4764
4765 if (ret == -EIOCBQUEUED) {
4766 u64 count = min_t(u64, iov_iter_count(&data->iter), disk_io_size);
4767
4768 /* Match ioctl by not returning past EOF if uncompressed. */
4769 if (!data->args.compression)
4770 count = min_t(u64, count, data->args.len);
4771
4772 ret = btrfs_uring_read_extent(&kiocb, &data->iter, start, lockend,
4773 cached_state, disk_bytenr, disk_io_size,
4774 count, data->args.compression,
4775 data->iov, cmd);
4776
4777 goto out_acct;
4778 }
4779
4780 out_free:
4781 kfree(data->iov);
4782
4783 out_acct:
4784 if (ret > 0)
4785 add_rchar(current, ret);
4786 inc_syscr(current);
4787
4788 if (ret != -EIOCBQUEUED && ret != -EAGAIN)
4789 kfree(data);
4790
4791 return ret;
4792 }
4793
btrfs_uring_encoded_write(struct io_uring_cmd * cmd,unsigned int issue_flags)4794 static int btrfs_uring_encoded_write(struct io_uring_cmd *cmd, unsigned int issue_flags)
4795 {
4796 struct file *file = cmd->file;
4797 loff_t pos;
4798 struct kiocb kiocb;
4799 ssize_t ret;
4800 void __user *sqe_addr;
4801 struct io_btrfs_cmd *bc = io_uring_cmd_to_pdu(cmd, struct io_btrfs_cmd);
4802 struct btrfs_uring_encoded_data *data = NULL;
4803
4804 if (cmd->flags & IORING_URING_CMD_REISSUE)
4805 data = bc->data;
4806
4807 if (!capable(CAP_SYS_ADMIN)) {
4808 ret = -EPERM;
4809 goto out_acct;
4810 }
4811 sqe_addr = u64_to_user_ptr(READ_ONCE(cmd->sqe->addr));
4812
4813 if (!(file->f_mode & FMODE_WRITE)) {
4814 ret = -EBADF;
4815 goto out_acct;
4816 }
4817
4818 if (!data) {
4819 data = kzalloc_obj(*data, GFP_NOFS);
4820 if (!data) {
4821 ret = -ENOMEM;
4822 goto out_acct;
4823 }
4824
4825 bc->data = data;
4826
4827 if (issue_flags & IO_URING_F_COMPAT) {
4828 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4829 struct btrfs_ioctl_encoded_io_args_32 args32;
4830
4831 if (copy_from_user(&args32, sqe_addr, sizeof(args32))) {
4832 ret = -EFAULT;
4833 goto out_acct;
4834 }
4835 data->args.iov = compat_ptr(args32.iov);
4836 data->args.iovcnt = args32.iovcnt;
4837 data->args.offset = args32.offset;
4838 data->args.flags = args32.flags;
4839 data->args.len = args32.len;
4840 data->args.unencoded_len = args32.unencoded_len;
4841 data->args.unencoded_offset = args32.unencoded_offset;
4842 data->args.compression = args32.compression;
4843 data->args.encryption = args32.encryption;
4844 memcpy(data->args.reserved, args32.reserved,
4845 sizeof(data->args.reserved));
4846 #else
4847 ret = -ENOTTY;
4848 goto out_acct;
4849 #endif
4850 } else {
4851 if (copy_from_user(&data->args, sqe_addr, sizeof(data->args))) {
4852 ret = -EFAULT;
4853 goto out_acct;
4854 }
4855 }
4856
4857 ret = -EINVAL;
4858 if (data->args.flags != 0)
4859 goto out_acct;
4860 if (memchr_inv(data->args.reserved, 0, sizeof(data->args.reserved)))
4861 goto out_acct;
4862 if (data->args.compression == BTRFS_ENCODED_IO_COMPRESSION_NONE &&
4863 data->args.encryption == BTRFS_ENCODED_IO_ENCRYPTION_NONE)
4864 goto out_acct;
4865 if (data->args.compression >= BTRFS_ENCODED_IO_COMPRESSION_TYPES ||
4866 data->args.encryption >= BTRFS_ENCODED_IO_ENCRYPTION_TYPES)
4867 goto out_acct;
4868 if (data->args.unencoded_offset > data->args.unencoded_len)
4869 goto out_acct;
4870 if (data->args.len > data->args.unencoded_len - data->args.unencoded_offset)
4871 goto out_acct;
4872
4873 data->iov = data->iovstack;
4874 ret = import_iovec(ITER_SOURCE, data->args.iov, data->args.iovcnt,
4875 ARRAY_SIZE(data->iovstack), &data->iov,
4876 &data->iter);
4877 if (ret < 0)
4878 goto out_acct;
4879
4880 if (iov_iter_count(&data->iter) == 0) {
4881 ret = 0;
4882 goto out_iov;
4883 }
4884 }
4885
4886 if (issue_flags & IO_URING_F_NONBLOCK) {
4887 ret = -EAGAIN;
4888 goto out_acct;
4889 }
4890
4891 pos = data->args.offset;
4892 ret = rw_verify_area(WRITE, file, &pos, data->args.len);
4893 if (ret < 0)
4894 goto out_iov;
4895
4896 init_sync_kiocb(&kiocb, file);
4897 ret = kiocb_set_rw_flags(&kiocb, 0, WRITE);
4898 if (ret)
4899 goto out_iov;
4900 kiocb.ki_pos = pos;
4901
4902 file_start_write(file);
4903
4904 ret = btrfs_do_write_iter(&kiocb, &data->iter, &data->args);
4905 if (ret > 0)
4906 fsnotify_modify(file);
4907
4908 file_end_write(file);
4909 out_iov:
4910 kfree(data->iov);
4911 out_acct:
4912 if (ret > 0)
4913 add_wchar(current, ret);
4914 inc_syscw(current);
4915
4916 if (ret != -EAGAIN)
4917 kfree(data);
4918 return ret;
4919 }
4920
btrfs_uring_cmd(struct io_uring_cmd * cmd,unsigned int issue_flags)4921 int btrfs_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
4922 {
4923 if (btrfs_is_shutdown(inode_to_fs_info(file_inode(cmd->file))))
4924 return -EIO;
4925
4926 switch (cmd->cmd_op) {
4927 case BTRFS_IOC_ENCODED_READ:
4928 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4929 case BTRFS_IOC_ENCODED_READ_32:
4930 #endif
4931 return btrfs_uring_encoded_read(cmd, issue_flags);
4932
4933 case BTRFS_IOC_ENCODED_WRITE:
4934 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
4935 case BTRFS_IOC_ENCODED_WRITE_32:
4936 #endif
4937 return btrfs_uring_encoded_write(cmd, issue_flags);
4938 }
4939
4940 return -EINVAL;
4941 }
4942
btrfs_ioctl_subvol_sync(struct btrfs_fs_info * fs_info,void __user * argp)4943 static int btrfs_ioctl_subvol_sync(struct btrfs_fs_info *fs_info, void __user *argp)
4944 {
4945 struct btrfs_root *root;
4946 struct btrfs_ioctl_subvol_wait args = { 0 };
4947 signed long sched_ret;
4948 int refs;
4949 u64 root_flags;
4950 bool wait_for_deletion = false;
4951 bool found = false;
4952
4953 if (copy_from_user(&args, argp, sizeof(args)))
4954 return -EFAULT;
4955
4956 switch (args.mode) {
4957 case BTRFS_SUBVOL_SYNC_WAIT_FOR_QUEUED:
4958 /*
4959 * Wait for the first one deleted that waits until all previous
4960 * are cleaned.
4961 */
4962 spin_lock(&fs_info->trans_lock);
4963 if (!list_empty(&fs_info->dead_roots)) {
4964 root = list_last_entry(&fs_info->dead_roots,
4965 struct btrfs_root, root_list);
4966 args.subvolid = btrfs_root_id(root);
4967 found = true;
4968 }
4969 spin_unlock(&fs_info->trans_lock);
4970 if (!found)
4971 return -ENOENT;
4972
4973 fallthrough;
4974 case BTRFS_SUBVOL_SYNC_WAIT_FOR_ONE:
4975 if ((0 < args.subvolid && args.subvolid < BTRFS_FIRST_FREE_OBJECTID) ||
4976 BTRFS_LAST_FREE_OBJECTID < args.subvolid)
4977 return -EINVAL;
4978 break;
4979 case BTRFS_SUBVOL_SYNC_COUNT:
4980 spin_lock(&fs_info->trans_lock);
4981 args.count = list_count_nodes(&fs_info->dead_roots);
4982 spin_unlock(&fs_info->trans_lock);
4983 if (copy_to_user(argp, &args, sizeof(args)))
4984 return -EFAULT;
4985 return 0;
4986 case BTRFS_SUBVOL_SYNC_PEEK_FIRST:
4987 spin_lock(&fs_info->trans_lock);
4988 /* Last in the list was deleted first. */
4989 if (!list_empty(&fs_info->dead_roots)) {
4990 root = list_last_entry(&fs_info->dead_roots,
4991 struct btrfs_root, root_list);
4992 args.subvolid = btrfs_root_id(root);
4993 } else {
4994 args.subvolid = 0;
4995 }
4996 spin_unlock(&fs_info->trans_lock);
4997 if (copy_to_user(argp, &args, sizeof(args)))
4998 return -EFAULT;
4999 return 0;
5000 case BTRFS_SUBVOL_SYNC_PEEK_LAST:
5001 spin_lock(&fs_info->trans_lock);
5002 /* First in the list was deleted last. */
5003 if (!list_empty(&fs_info->dead_roots)) {
5004 root = list_first_entry(&fs_info->dead_roots,
5005 struct btrfs_root, root_list);
5006 args.subvolid = btrfs_root_id(root);
5007 } else {
5008 args.subvolid = 0;
5009 }
5010 spin_unlock(&fs_info->trans_lock);
5011 if (copy_to_user(argp, &args, sizeof(args)))
5012 return -EFAULT;
5013 return 0;
5014 default:
5015 return -EINVAL;
5016 }
5017
5018 /* 32bit limitation: fs_roots_radix key is not wide enough. */
5019 if (sizeof(unsigned long) != sizeof(u64) && args.subvolid > U32_MAX)
5020 return -EOVERFLOW;
5021
5022 while (1) {
5023 /* Wait for the specific one. */
5024 if (down_read_interruptible(&fs_info->subvol_sem) == -EINTR)
5025 return -EINTR;
5026 refs = -1;
5027 spin_lock(&fs_info->fs_roots_radix_lock);
5028 root = radix_tree_lookup(&fs_info->fs_roots_radix,
5029 (unsigned long)args.subvolid);
5030 if (root) {
5031 spin_lock(&root->root_item_lock);
5032 refs = btrfs_root_refs(&root->root_item);
5033 root_flags = btrfs_root_flags(&root->root_item);
5034 spin_unlock(&root->root_item_lock);
5035 }
5036 spin_unlock(&fs_info->fs_roots_radix_lock);
5037 up_read(&fs_info->subvol_sem);
5038
5039 /* Subvolume does not exist. */
5040 if (!root)
5041 return -ENOENT;
5042
5043 /* Subvolume not deleted at all. */
5044 if (refs > 0)
5045 return -EEXIST;
5046 /* We've waited and now the subvolume is gone. */
5047 if (wait_for_deletion && refs == -1) {
5048 /* Return the one we waited for as the last one. */
5049 if (copy_to_user(argp, &args, sizeof(args)))
5050 return -EFAULT;
5051 return 0;
5052 }
5053
5054 /* Subvolume not found on the first try (deleted or never existed). */
5055 if (refs == -1)
5056 return -ENOENT;
5057
5058 wait_for_deletion = true;
5059 ASSERT(root_flags & BTRFS_ROOT_SUBVOL_DEAD);
5060 sched_ret = schedule_timeout_interruptible(HZ);
5061 /* Early wake up or error. */
5062 if (sched_ret != 0)
5063 return -EINTR;
5064 }
5065
5066 return 0;
5067 }
5068
5069 #ifdef CONFIG_BTRFS_EXPERIMENTAL
btrfs_ioctl_shutdown(struct btrfs_fs_info * fs_info,unsigned long arg)5070 static int btrfs_ioctl_shutdown(struct btrfs_fs_info *fs_info, unsigned long arg)
5071 {
5072 int ret = 0;
5073 u32 flags;
5074
5075 if (!capable(CAP_SYS_ADMIN))
5076 return -EPERM;
5077
5078 if (get_user(flags, (u32 __user *)arg))
5079 return -EFAULT;
5080
5081 if (flags >= BTRFS_SHUTDOWN_FLAGS_LAST)
5082 return -EINVAL;
5083
5084 if (btrfs_is_shutdown(fs_info))
5085 return 0;
5086
5087 switch (flags) {
5088 case BTRFS_SHUTDOWN_FLAGS_LOGFLUSH:
5089 case BTRFS_SHUTDOWN_FLAGS_DEFAULT:
5090 ret = freeze_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
5091 if (ret)
5092 return ret;
5093 btrfs_force_shutdown(fs_info);
5094 ret = thaw_super(fs_info->sb, FREEZE_HOLDER_KERNEL, NULL);
5095 if (ret)
5096 return ret;
5097 break;
5098 case BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH:
5099 btrfs_force_shutdown(fs_info);
5100 break;
5101 }
5102 return ret;
5103 }
5104 #endif
5105
btrfs_ioctl(struct file * file,unsigned int cmd,unsigned long arg)5106 long btrfs_ioctl(struct file *file, unsigned int
5107 cmd, unsigned long arg)
5108 {
5109 struct inode *inode = file_inode(file);
5110 struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
5111 struct btrfs_root *root = BTRFS_I(inode)->root;
5112 void __user *argp = (void __user *)arg;
5113
5114 switch (cmd) {
5115 case FS_IOC_GETVERSION:
5116 return btrfs_ioctl_getversion(inode, argp);
5117 case FS_IOC_GETFSLABEL:
5118 return btrfs_ioctl_get_fslabel(fs_info, argp);
5119 case FS_IOC_SETFSLABEL:
5120 return btrfs_ioctl_set_fslabel(file, argp);
5121 case FITRIM:
5122 return btrfs_ioctl_fitrim(fs_info, argp);
5123 case BTRFS_IOC_SNAP_CREATE:
5124 return btrfs_ioctl_snap_create(file, argp, false);
5125 case BTRFS_IOC_SNAP_CREATE_V2:
5126 return btrfs_ioctl_snap_create_v2(file, argp, false);
5127 case BTRFS_IOC_SUBVOL_CREATE:
5128 return btrfs_ioctl_snap_create(file, argp, true);
5129 case BTRFS_IOC_SUBVOL_CREATE_V2:
5130 return btrfs_ioctl_snap_create_v2(file, argp, true);
5131 case BTRFS_IOC_SNAP_DESTROY:
5132 return btrfs_ioctl_snap_destroy(file, argp, false);
5133 case BTRFS_IOC_SNAP_DESTROY_V2:
5134 return btrfs_ioctl_snap_destroy(file, argp, true);
5135 case BTRFS_IOC_SUBVOL_GETFLAGS:
5136 return btrfs_ioctl_subvol_getflags(BTRFS_I(inode), argp);
5137 case BTRFS_IOC_SUBVOL_SETFLAGS:
5138 return btrfs_ioctl_subvol_setflags(file, argp);
5139 case BTRFS_IOC_DEFAULT_SUBVOL:
5140 return btrfs_ioctl_default_subvol(file, argp);
5141 case BTRFS_IOC_DEFRAG:
5142 return btrfs_ioctl_defrag(file, NULL);
5143 case BTRFS_IOC_DEFRAG_RANGE:
5144 return btrfs_ioctl_defrag(file, argp);
5145 case BTRFS_IOC_RESIZE:
5146 return btrfs_ioctl_resize(file, argp);
5147 case BTRFS_IOC_ADD_DEV:
5148 return btrfs_ioctl_add_dev(fs_info, argp);
5149 case BTRFS_IOC_RM_DEV:
5150 return btrfs_ioctl_rm_dev(file, argp);
5151 case BTRFS_IOC_RM_DEV_V2:
5152 return btrfs_ioctl_rm_dev_v2(file, argp);
5153 case BTRFS_IOC_FS_INFO:
5154 return btrfs_ioctl_fs_info(fs_info, argp);
5155 case BTRFS_IOC_DEV_INFO:
5156 return btrfs_ioctl_dev_info(fs_info, argp);
5157 case BTRFS_IOC_TREE_SEARCH:
5158 return btrfs_ioctl_tree_search(root, argp);
5159 case BTRFS_IOC_TREE_SEARCH_V2:
5160 return btrfs_ioctl_tree_search_v2(root, argp);
5161 case BTRFS_IOC_INO_LOOKUP:
5162 return btrfs_ioctl_ino_lookup(root, argp);
5163 case BTRFS_IOC_INO_PATHS:
5164 return btrfs_ioctl_ino_to_path(root, argp);
5165 case BTRFS_IOC_LOGICAL_INO:
5166 return btrfs_ioctl_logical_to_ino(fs_info, argp, 1);
5167 case BTRFS_IOC_LOGICAL_INO_V2:
5168 return btrfs_ioctl_logical_to_ino(fs_info, argp, 2);
5169 case BTRFS_IOC_SPACE_INFO:
5170 return btrfs_ioctl_space_info(fs_info, argp);
5171 case BTRFS_IOC_SYNC: {
5172 int ret;
5173
5174 ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
5175 if (ret)
5176 return ret;
5177 ret = btrfs_sync_fs(inode->i_sb, 1);
5178 /*
5179 * There may be work for the cleaner kthread to do (subvolume
5180 * deletion, delayed iputs, defrag inodes, etc), so wake it up.
5181 */
5182 wake_up_process(fs_info->cleaner_kthread);
5183 return ret;
5184 }
5185 case BTRFS_IOC_START_SYNC:
5186 return btrfs_ioctl_start_sync(root, argp);
5187 case BTRFS_IOC_WAIT_SYNC:
5188 return btrfs_ioctl_wait_sync(fs_info, argp);
5189 case BTRFS_IOC_SCRUB:
5190 return btrfs_ioctl_scrub(file, argp);
5191 case BTRFS_IOC_SCRUB_CANCEL:
5192 return btrfs_ioctl_scrub_cancel(fs_info);
5193 case BTRFS_IOC_SCRUB_PROGRESS:
5194 return btrfs_ioctl_scrub_progress(fs_info, argp);
5195 case BTRFS_IOC_BALANCE_V2:
5196 return btrfs_ioctl_balance(file, argp);
5197 case BTRFS_IOC_BALANCE_CTL:
5198 return btrfs_ioctl_balance_ctl(fs_info, arg);
5199 case BTRFS_IOC_BALANCE_PROGRESS:
5200 return btrfs_ioctl_balance_progress(fs_info, argp);
5201 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5202 return btrfs_ioctl_set_received_subvol(file, argp);
5203 #ifdef CONFIG_64BIT
5204 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
5205 return btrfs_ioctl_set_received_subvol_32(file, argp);
5206 #endif
5207 case BTRFS_IOC_SEND:
5208 return _btrfs_ioctl_send(root, argp, false);
5209 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5210 case BTRFS_IOC_SEND_32:
5211 return _btrfs_ioctl_send(root, argp, true);
5212 #endif
5213 case BTRFS_IOC_GET_DEV_STATS:
5214 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5215 case BTRFS_IOC_QUOTA_CTL:
5216 return btrfs_ioctl_quota_ctl(file, argp);
5217 case BTRFS_IOC_QGROUP_ASSIGN:
5218 return btrfs_ioctl_qgroup_assign(file, argp);
5219 case BTRFS_IOC_QGROUP_CREATE:
5220 return btrfs_ioctl_qgroup_create(file, argp);
5221 case BTRFS_IOC_QGROUP_LIMIT:
5222 return btrfs_ioctl_qgroup_limit(file, argp);
5223 case BTRFS_IOC_QUOTA_RESCAN:
5224 return btrfs_ioctl_quota_rescan(file, argp);
5225 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
5226 return btrfs_ioctl_quota_rescan_status(fs_info, argp);
5227 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5228 return btrfs_ioctl_quota_rescan_wait(fs_info);
5229 case BTRFS_IOC_DEV_REPLACE:
5230 return btrfs_ioctl_dev_replace(fs_info, argp);
5231 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
5232 return btrfs_ioctl_get_supported_features(argp);
5233 case BTRFS_IOC_GET_FEATURES:
5234 return btrfs_ioctl_get_features(fs_info, argp);
5235 case BTRFS_IOC_SET_FEATURES:
5236 return btrfs_ioctl_set_features(file, argp);
5237 case BTRFS_IOC_GET_SUBVOL_INFO:
5238 return btrfs_ioctl_get_subvol_info(inode, argp);
5239 case BTRFS_IOC_GET_SUBVOL_ROOTREF:
5240 return btrfs_ioctl_get_subvol_rootref(root, argp);
5241 case BTRFS_IOC_INO_LOOKUP_USER:
5242 return btrfs_ioctl_ino_lookup_user(file, argp);
5243 case FS_IOC_ENABLE_VERITY:
5244 return fsverity_ioctl_enable(file, (const void __user *)argp);
5245 case FS_IOC_MEASURE_VERITY:
5246 return fsverity_ioctl_measure(file, argp);
5247 case FS_IOC_READ_VERITY_METADATA:
5248 return fsverity_ioctl_read_metadata(file, argp);
5249 case BTRFS_IOC_ENCODED_READ:
5250 return btrfs_ioctl_encoded_read(file, argp, false);
5251 case BTRFS_IOC_ENCODED_WRITE:
5252 return btrfs_ioctl_encoded_write(file, argp, false);
5253 #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT)
5254 case BTRFS_IOC_ENCODED_READ_32:
5255 return btrfs_ioctl_encoded_read(file, argp, true);
5256 case BTRFS_IOC_ENCODED_WRITE_32:
5257 return btrfs_ioctl_encoded_write(file, argp, true);
5258 #endif
5259 case BTRFS_IOC_SUBVOL_SYNC_WAIT:
5260 return btrfs_ioctl_subvol_sync(fs_info, argp);
5261 #ifdef CONFIG_BTRFS_EXPERIMENTAL
5262 case BTRFS_IOC_SHUTDOWN:
5263 return btrfs_ioctl_shutdown(fs_info, arg);
5264 #endif
5265 }
5266
5267 return -ENOTTY;
5268 }
5269
5270 #ifdef CONFIG_COMPAT
btrfs_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)5271 long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5272 {
5273 /*
5274 * These all access 32-bit values anyway so no further
5275 * handling is necessary.
5276 */
5277 switch (cmd) {
5278 case FS_IOC32_GETVERSION:
5279 cmd = FS_IOC_GETVERSION;
5280 break;
5281 }
5282
5283 return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
5284 }
5285 #endif
5286