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