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