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