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