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