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