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