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