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