1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #include <linux/blkdev.h> 7 #include <linux/module.h> 8 #include <linux/fs.h> 9 #include <linux/pagemap.h> 10 #include <linux/highmem.h> 11 #include <linux/time.h> 12 #include <linux/init.h> 13 #include <linux/seq_file.h> 14 #include <linux/string.h> 15 #include <linux/backing-dev.h> 16 #include <linux/mount.h> 17 #include <linux/writeback.h> 18 #include <linux/statfs.h> 19 #include <linux/compat.h> 20 #include <linux/parser.h> 21 #include <linux/ctype.h> 22 #include <linux/namei.h> 23 #include <linux/miscdevice.h> 24 #include <linux/magic.h> 25 #include <linux/slab.h> 26 #include <linux/ratelimit.h> 27 #include <linux/crc32c.h> 28 #include <linux/btrfs.h> 29 #include "delayed-inode.h" 30 #include "ctree.h" 31 #include "disk-io.h" 32 #include "transaction.h" 33 #include "btrfs_inode.h" 34 #include "print-tree.h" 35 #include "props.h" 36 #include "xattr.h" 37 #include "volumes.h" 38 #include "export.h" 39 #include "compression.h" 40 #include "rcu-string.h" 41 #include "dev-replace.h" 42 #include "free-space-cache.h" 43 #include "backref.h" 44 #include "space-info.h" 45 #include "sysfs.h" 46 #include "zoned.h" 47 #include "tests/btrfs-tests.h" 48 #include "block-group.h" 49 #include "discard.h" 50 #include "qgroup.h" 51 #include "raid56.h" 52 #define CREATE_TRACE_POINTS 53 #include <trace/events/btrfs.h> 54 55 static const struct super_operations btrfs_super_ops; 56 57 /* 58 * Types for mounting the default subvolume and a subvolume explicitly 59 * requested by subvol=/path. That way the callchain is straightforward and we 60 * don't have to play tricks with the mount options and recursive calls to 61 * btrfs_mount. 62 * 63 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. 64 */ 65 static struct file_system_type btrfs_fs_type; 66 static struct file_system_type btrfs_root_fs_type; 67 68 static int btrfs_remount(struct super_block *sb, int *flags, char *data); 69 70 #ifdef CONFIG_PRINTK 71 72 #define STATE_STRING_PREFACE ": state " 73 #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT) 74 75 /* 76 * Characters to print to indicate error conditions or uncommon filesystem state. 77 * RO is not an error. 78 */ 79 static const char fs_state_chars[] = { 80 [BTRFS_FS_STATE_ERROR] = 'E', 81 [BTRFS_FS_STATE_REMOUNTING] = 'M', 82 [BTRFS_FS_STATE_RO] = 0, 83 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A', 84 [BTRFS_FS_STATE_DEV_REPLACING] = 'R', 85 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0, 86 [BTRFS_FS_STATE_NO_CSUMS] = 'C', 87 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L', 88 }; 89 90 static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf) 91 { 92 unsigned int bit; 93 bool states_printed = false; 94 unsigned long fs_state = READ_ONCE(info->fs_state); 95 char *curr = buf; 96 97 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE)); 98 curr += sizeof(STATE_STRING_PREFACE) - 1; 99 100 for_each_set_bit(bit, &fs_state, sizeof(fs_state)) { 101 WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT); 102 if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) { 103 *curr++ = fs_state_chars[bit]; 104 states_printed = true; 105 } 106 } 107 108 /* If no states were printed, reset the buffer */ 109 if (!states_printed) 110 curr = buf; 111 112 *curr++ = 0; 113 } 114 #endif 115 116 /* 117 * Generally the error codes correspond to their respective errors, but there 118 * are a few special cases. 119 * 120 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for 121 * instance will return EUCLEAN if any of the blocks are corrupted in 122 * a way that is problematic. We want to reserve EUCLEAN for these 123 * sort of corruptions. 124 * 125 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we 126 * need to use EROFS for this case. We will have no idea of the 127 * original failure, that will have been reported at the time we tripped 128 * over the error. Each subsequent error that doesn't have any context 129 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR. 130 */ 131 const char * __attribute_const__ btrfs_decode_error(int errno) 132 { 133 char *errstr = "unknown"; 134 135 switch (errno) { 136 case -ENOENT: /* -2 */ 137 errstr = "No such entry"; 138 break; 139 case -EIO: /* -5 */ 140 errstr = "IO failure"; 141 break; 142 case -ENOMEM: /* -12*/ 143 errstr = "Out of memory"; 144 break; 145 case -EEXIST: /* -17 */ 146 errstr = "Object already exists"; 147 break; 148 case -ENOSPC: /* -28 */ 149 errstr = "No space left"; 150 break; 151 case -EROFS: /* -30 */ 152 errstr = "Readonly filesystem"; 153 break; 154 case -EOPNOTSUPP: /* -95 */ 155 errstr = "Operation not supported"; 156 break; 157 case -EUCLEAN: /* -117 */ 158 errstr = "Filesystem corrupted"; 159 break; 160 case -EDQUOT: /* -122 */ 161 errstr = "Quota exceeded"; 162 break; 163 } 164 165 return errstr; 166 } 167 168 /* 169 * __btrfs_handle_fs_error decodes expected errors from the caller and 170 * invokes the appropriate error response. 171 */ 172 __cold 173 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 174 unsigned int line, int errno, const char *fmt, ...) 175 { 176 struct super_block *sb = fs_info->sb; 177 #ifdef CONFIG_PRINTK 178 char statestr[STATE_STRING_BUF_LEN]; 179 const char *errstr; 180 #endif 181 182 /* 183 * Special case: if the error is EROFS, and we're already 184 * under SB_RDONLY, then it is safe here. 185 */ 186 if (errno == -EROFS && sb_rdonly(sb)) 187 return; 188 189 #ifdef CONFIG_PRINTK 190 errstr = btrfs_decode_error(errno); 191 btrfs_state_to_string(fs_info, statestr); 192 if (fmt) { 193 struct va_format vaf; 194 va_list args; 195 196 va_start(args, fmt); 197 vaf.fmt = fmt; 198 vaf.va = &args; 199 200 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n", 201 sb->s_id, statestr, function, line, errno, errstr, &vaf); 202 va_end(args); 203 } else { 204 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n", 205 sb->s_id, statestr, function, line, errno, errstr); 206 } 207 #endif 208 209 /* 210 * Today we only save the error info to memory. Long term we'll 211 * also send it down to the disk 212 */ 213 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 214 215 /* Don't go through full error handling during mount */ 216 if (!(sb->s_flags & SB_BORN)) 217 return; 218 219 if (sb_rdonly(sb)) 220 return; 221 222 btrfs_discard_stop(fs_info); 223 224 /* btrfs handle error by forcing the filesystem readonly */ 225 btrfs_set_sb_rdonly(sb); 226 btrfs_info(fs_info, "forced readonly"); 227 /* 228 * Note that a running device replace operation is not canceled here 229 * although there is no way to update the progress. It would add the 230 * risk of a deadlock, therefore the canceling is omitted. The only 231 * penalty is that some I/O remains active until the procedure 232 * completes. The next time when the filesystem is mounted writable 233 * again, the device replace operation continues. 234 */ 235 } 236 237 #ifdef CONFIG_PRINTK 238 static const char * const logtypes[] = { 239 "emergency", 240 "alert", 241 "critical", 242 "error", 243 "warning", 244 "notice", 245 "info", 246 "debug", 247 }; 248 249 250 /* 251 * Use one ratelimit state per log level so that a flood of less important 252 * messages doesn't cause more important ones to be dropped. 253 */ 254 static struct ratelimit_state printk_limits[] = { 255 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 256 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 257 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 258 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 259 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 260 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 261 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 262 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 263 }; 264 265 void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 266 { 267 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 268 struct va_format vaf; 269 va_list args; 270 int kern_level; 271 const char *type = logtypes[4]; 272 struct ratelimit_state *ratelimit = &printk_limits[4]; 273 274 va_start(args, fmt); 275 276 while ((kern_level = printk_get_level(fmt)) != 0) { 277 size_t size = printk_skip_level(fmt) - fmt; 278 279 if (kern_level >= '0' && kern_level <= '7') { 280 memcpy(lvl, fmt, size); 281 lvl[size] = '\0'; 282 type = logtypes[kern_level - '0']; 283 ratelimit = &printk_limits[kern_level - '0']; 284 } 285 fmt += size; 286 } 287 288 vaf.fmt = fmt; 289 vaf.va = &args; 290 291 if (__ratelimit(ratelimit)) { 292 if (fs_info) { 293 char statestr[STATE_STRING_BUF_LEN]; 294 295 btrfs_state_to_string(fs_info, statestr); 296 _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type, 297 fs_info->sb->s_id, statestr, &vaf); 298 } else { 299 _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf); 300 } 301 } 302 303 va_end(args); 304 } 305 #endif 306 307 #if BITS_PER_LONG == 32 308 void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info) 309 { 310 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) { 311 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses"); 312 btrfs_warn(fs_info, 313 "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT", 314 BTRFS_32BIT_MAX_FILE_SIZE >> 40); 315 btrfs_warn(fs_info, 316 "please consider upgrading to 64bit kernel/hardware"); 317 } 318 } 319 320 void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) 321 { 322 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) { 323 btrfs_err(fs_info, "reached 32bit limit for logical addresses"); 324 btrfs_err(fs_info, 325 "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed", 326 BTRFS_32BIT_MAX_FILE_SIZE >> 40); 327 btrfs_err(fs_info, 328 "please consider upgrading to 64bit kernel/hardware"); 329 } 330 } 331 #endif 332 333 /* 334 * We only mark the transaction aborted and then set the file system read-only. 335 * This will prevent new transactions from starting or trying to join this 336 * one. 337 * 338 * This means that error recovery at the call site is limited to freeing 339 * any local memory allocations and passing the error code up without 340 * further cleanup. The transaction should complete as it normally would 341 * in the call path but will return -EIO. 342 * 343 * We'll complete the cleanup in btrfs_end_transaction and 344 * btrfs_commit_transaction. 345 */ 346 __cold 347 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 348 const char *function, 349 unsigned int line, int errno, bool first_hit) 350 { 351 struct btrfs_fs_info *fs_info = trans->fs_info; 352 353 WRITE_ONCE(trans->aborted, errno); 354 WRITE_ONCE(trans->transaction->aborted, errno); 355 if (first_hit && errno == -ENOSPC) 356 btrfs_dump_space_info_for_trans_abort(fs_info); 357 /* Wake up anybody who may be waiting on this transaction */ 358 wake_up(&fs_info->transaction_wait); 359 wake_up(&fs_info->transaction_blocked_wait); 360 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 361 } 362 /* 363 * __btrfs_panic decodes unexpected, fatal errors from the caller, 364 * issues an alert, and either panics or BUGs, depending on mount options. 365 */ 366 __cold 367 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 368 unsigned int line, int errno, const char *fmt, ...) 369 { 370 char *s_id = "<unknown>"; 371 const char *errstr; 372 struct va_format vaf = { .fmt = fmt }; 373 va_list args; 374 375 if (fs_info) 376 s_id = fs_info->sb->s_id; 377 378 va_start(args, fmt); 379 vaf.va = &args; 380 381 errstr = btrfs_decode_error(errno); 382 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 383 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 384 s_id, function, line, &vaf, errno, errstr); 385 386 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 387 function, line, &vaf, errno, errstr); 388 va_end(args); 389 /* Caller calls BUG() */ 390 } 391 392 static void btrfs_put_super(struct super_block *sb) 393 { 394 close_ctree(btrfs_sb(sb)); 395 } 396 397 enum { 398 Opt_acl, Opt_noacl, 399 Opt_clear_cache, 400 Opt_commit_interval, 401 Opt_compress, 402 Opt_compress_force, 403 Opt_compress_force_type, 404 Opt_compress_type, 405 Opt_degraded, 406 Opt_device, 407 Opt_fatal_errors, 408 Opt_flushoncommit, Opt_noflushoncommit, 409 Opt_max_inline, 410 Opt_barrier, Opt_nobarrier, 411 Opt_datacow, Opt_nodatacow, 412 Opt_datasum, Opt_nodatasum, 413 Opt_defrag, Opt_nodefrag, 414 Opt_discard, Opt_nodiscard, 415 Opt_discard_mode, 416 Opt_norecovery, 417 Opt_ratio, 418 Opt_rescan_uuid_tree, 419 Opt_skip_balance, 420 Opt_space_cache, Opt_no_space_cache, 421 Opt_space_cache_version, 422 Opt_ssd, Opt_nossd, 423 Opt_ssd_spread, Opt_nossd_spread, 424 Opt_subvol, 425 Opt_subvol_empty, 426 Opt_subvolid, 427 Opt_thread_pool, 428 Opt_treelog, Opt_notreelog, 429 Opt_user_subvol_rm_allowed, 430 431 /* Rescue options */ 432 Opt_rescue, 433 Opt_usebackuproot, 434 Opt_nologreplay, 435 Opt_ignorebadroots, 436 Opt_ignoredatacsums, 437 Opt_rescue_all, 438 439 /* Deprecated options */ 440 Opt_recovery, 441 Opt_inode_cache, Opt_noinode_cache, 442 443 /* Debugging options */ 444 Opt_check_integrity, 445 Opt_check_integrity_including_extent_data, 446 Opt_check_integrity_print_mask, 447 Opt_enospc_debug, Opt_noenospc_debug, 448 #ifdef CONFIG_BTRFS_DEBUG 449 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 450 #endif 451 #ifdef CONFIG_BTRFS_FS_REF_VERIFY 452 Opt_ref_verify, 453 #endif 454 Opt_err, 455 }; 456 457 static const match_table_t tokens = { 458 {Opt_acl, "acl"}, 459 {Opt_noacl, "noacl"}, 460 {Opt_clear_cache, "clear_cache"}, 461 {Opt_commit_interval, "commit=%u"}, 462 {Opt_compress, "compress"}, 463 {Opt_compress_type, "compress=%s"}, 464 {Opt_compress_force, "compress-force"}, 465 {Opt_compress_force_type, "compress-force=%s"}, 466 {Opt_degraded, "degraded"}, 467 {Opt_device, "device=%s"}, 468 {Opt_fatal_errors, "fatal_errors=%s"}, 469 {Opt_flushoncommit, "flushoncommit"}, 470 {Opt_noflushoncommit, "noflushoncommit"}, 471 {Opt_inode_cache, "inode_cache"}, 472 {Opt_noinode_cache, "noinode_cache"}, 473 {Opt_max_inline, "max_inline=%s"}, 474 {Opt_barrier, "barrier"}, 475 {Opt_nobarrier, "nobarrier"}, 476 {Opt_datacow, "datacow"}, 477 {Opt_nodatacow, "nodatacow"}, 478 {Opt_datasum, "datasum"}, 479 {Opt_nodatasum, "nodatasum"}, 480 {Opt_defrag, "autodefrag"}, 481 {Opt_nodefrag, "noautodefrag"}, 482 {Opt_discard, "discard"}, 483 {Opt_discard_mode, "discard=%s"}, 484 {Opt_nodiscard, "nodiscard"}, 485 {Opt_norecovery, "norecovery"}, 486 {Opt_ratio, "metadata_ratio=%u"}, 487 {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 488 {Opt_skip_balance, "skip_balance"}, 489 {Opt_space_cache, "space_cache"}, 490 {Opt_no_space_cache, "nospace_cache"}, 491 {Opt_space_cache_version, "space_cache=%s"}, 492 {Opt_ssd, "ssd"}, 493 {Opt_nossd, "nossd"}, 494 {Opt_ssd_spread, "ssd_spread"}, 495 {Opt_nossd_spread, "nossd_spread"}, 496 {Opt_subvol, "subvol=%s"}, 497 {Opt_subvol_empty, "subvol="}, 498 {Opt_subvolid, "subvolid=%s"}, 499 {Opt_thread_pool, "thread_pool=%u"}, 500 {Opt_treelog, "treelog"}, 501 {Opt_notreelog, "notreelog"}, 502 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 503 504 /* Rescue options */ 505 {Opt_rescue, "rescue=%s"}, 506 /* Deprecated, with alias rescue=nologreplay */ 507 {Opt_nologreplay, "nologreplay"}, 508 /* Deprecated, with alias rescue=usebackuproot */ 509 {Opt_usebackuproot, "usebackuproot"}, 510 511 /* Deprecated options */ 512 {Opt_recovery, "recovery"}, 513 514 /* Debugging options */ 515 {Opt_check_integrity, "check_int"}, 516 {Opt_check_integrity_including_extent_data, "check_int_data"}, 517 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"}, 518 {Opt_enospc_debug, "enospc_debug"}, 519 {Opt_noenospc_debug, "noenospc_debug"}, 520 #ifdef CONFIG_BTRFS_DEBUG 521 {Opt_fragment_data, "fragment=data"}, 522 {Opt_fragment_metadata, "fragment=metadata"}, 523 {Opt_fragment_all, "fragment=all"}, 524 #endif 525 #ifdef CONFIG_BTRFS_FS_REF_VERIFY 526 {Opt_ref_verify, "ref_verify"}, 527 #endif 528 {Opt_err, NULL}, 529 }; 530 531 static const match_table_t rescue_tokens = { 532 {Opt_usebackuproot, "usebackuproot"}, 533 {Opt_nologreplay, "nologreplay"}, 534 {Opt_ignorebadroots, "ignorebadroots"}, 535 {Opt_ignorebadroots, "ibadroots"}, 536 {Opt_ignoredatacsums, "ignoredatacsums"}, 537 {Opt_ignoredatacsums, "idatacsums"}, 538 {Opt_rescue_all, "all"}, 539 {Opt_err, NULL}, 540 }; 541 542 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt, 543 const char *opt_name) 544 { 545 if (fs_info->mount_opt & opt) { 546 btrfs_err(fs_info, "%s must be used with ro mount option", 547 opt_name); 548 return true; 549 } 550 return false; 551 } 552 553 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options) 554 { 555 char *opts; 556 char *orig; 557 char *p; 558 substring_t args[MAX_OPT_ARGS]; 559 int ret = 0; 560 561 opts = kstrdup(options, GFP_KERNEL); 562 if (!opts) 563 return -ENOMEM; 564 orig = opts; 565 566 while ((p = strsep(&opts, ":")) != NULL) { 567 int token; 568 569 if (!*p) 570 continue; 571 token = match_token(p, rescue_tokens, args); 572 switch (token){ 573 case Opt_usebackuproot: 574 btrfs_info(info, 575 "trying to use backup root at mount time"); 576 btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 577 break; 578 case Opt_nologreplay: 579 btrfs_set_and_info(info, NOLOGREPLAY, 580 "disabling log replay at mount time"); 581 break; 582 case Opt_ignorebadroots: 583 btrfs_set_and_info(info, IGNOREBADROOTS, 584 "ignoring bad roots"); 585 break; 586 case Opt_ignoredatacsums: 587 btrfs_set_and_info(info, IGNOREDATACSUMS, 588 "ignoring data csums"); 589 break; 590 case Opt_rescue_all: 591 btrfs_info(info, "enabling all of the rescue options"); 592 btrfs_set_and_info(info, IGNOREDATACSUMS, 593 "ignoring data csums"); 594 btrfs_set_and_info(info, IGNOREBADROOTS, 595 "ignoring bad roots"); 596 btrfs_set_and_info(info, NOLOGREPLAY, 597 "disabling log replay at mount time"); 598 break; 599 case Opt_err: 600 btrfs_info(info, "unrecognized rescue option '%s'", p); 601 ret = -EINVAL; 602 goto out; 603 default: 604 break; 605 } 606 607 } 608 out: 609 kfree(orig); 610 return ret; 611 } 612 613 /* 614 * Regular mount options parser. Everything that is needed only when 615 * reading in a new superblock is parsed here. 616 * XXX JDM: This needs to be cleaned up for remount. 617 */ 618 int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 619 unsigned long new_flags) 620 { 621 substring_t args[MAX_OPT_ARGS]; 622 char *p, *num; 623 int intarg; 624 int ret = 0; 625 char *compress_type; 626 bool compress_force = false; 627 enum btrfs_compression_type saved_compress_type; 628 int saved_compress_level; 629 bool saved_compress_force; 630 int no_compress = 0; 631 const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state); 632 633 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 634 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 635 else if (btrfs_free_space_cache_v1_active(info)) { 636 if (btrfs_is_zoned(info)) { 637 btrfs_info(info, 638 "zoned: clearing existing space cache"); 639 btrfs_set_super_cache_generation(info->super_copy, 0); 640 } else { 641 btrfs_set_opt(info->mount_opt, SPACE_CACHE); 642 } 643 } 644 645 /* 646 * Even the options are empty, we still need to do extra check 647 * against new flags 648 */ 649 if (!options) 650 goto check; 651 652 while ((p = strsep(&options, ",")) != NULL) { 653 int token; 654 if (!*p) 655 continue; 656 657 token = match_token(p, tokens, args); 658 switch (token) { 659 case Opt_degraded: 660 btrfs_info(info, "allowing degraded mounts"); 661 btrfs_set_opt(info->mount_opt, DEGRADED); 662 break; 663 case Opt_subvol: 664 case Opt_subvol_empty: 665 case Opt_subvolid: 666 case Opt_device: 667 /* 668 * These are parsed by btrfs_parse_subvol_options or 669 * btrfs_parse_device_options and can be ignored here. 670 */ 671 break; 672 case Opt_nodatasum: 673 btrfs_set_and_info(info, NODATASUM, 674 "setting nodatasum"); 675 break; 676 case Opt_datasum: 677 if (btrfs_test_opt(info, NODATASUM)) { 678 if (btrfs_test_opt(info, NODATACOW)) 679 btrfs_info(info, 680 "setting datasum, datacow enabled"); 681 else 682 btrfs_info(info, "setting datasum"); 683 } 684 btrfs_clear_opt(info->mount_opt, NODATACOW); 685 btrfs_clear_opt(info->mount_opt, NODATASUM); 686 break; 687 case Opt_nodatacow: 688 if (!btrfs_test_opt(info, NODATACOW)) { 689 if (!btrfs_test_opt(info, COMPRESS) || 690 !btrfs_test_opt(info, FORCE_COMPRESS)) { 691 btrfs_info(info, 692 "setting nodatacow, compression disabled"); 693 } else { 694 btrfs_info(info, "setting nodatacow"); 695 } 696 } 697 btrfs_clear_opt(info->mount_opt, COMPRESS); 698 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 699 btrfs_set_opt(info->mount_opt, NODATACOW); 700 btrfs_set_opt(info->mount_opt, NODATASUM); 701 break; 702 case Opt_datacow: 703 btrfs_clear_and_info(info, NODATACOW, 704 "setting datacow"); 705 break; 706 case Opt_compress_force: 707 case Opt_compress_force_type: 708 compress_force = true; 709 fallthrough; 710 case Opt_compress: 711 case Opt_compress_type: 712 saved_compress_type = btrfs_test_opt(info, 713 COMPRESS) ? 714 info->compress_type : BTRFS_COMPRESS_NONE; 715 saved_compress_force = 716 btrfs_test_opt(info, FORCE_COMPRESS); 717 saved_compress_level = info->compress_level; 718 if (token == Opt_compress || 719 token == Opt_compress_force || 720 strncmp(args[0].from, "zlib", 4) == 0) { 721 compress_type = "zlib"; 722 723 info->compress_type = BTRFS_COMPRESS_ZLIB; 724 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 725 /* 726 * args[0] contains uninitialized data since 727 * for these tokens we don't expect any 728 * parameter. 729 */ 730 if (token != Opt_compress && 731 token != Opt_compress_force) 732 info->compress_level = 733 btrfs_compress_str2level( 734 BTRFS_COMPRESS_ZLIB, 735 args[0].from + 4); 736 btrfs_set_opt(info->mount_opt, COMPRESS); 737 btrfs_clear_opt(info->mount_opt, NODATACOW); 738 btrfs_clear_opt(info->mount_opt, NODATASUM); 739 no_compress = 0; 740 } else if (strncmp(args[0].from, "lzo", 3) == 0) { 741 compress_type = "lzo"; 742 info->compress_type = BTRFS_COMPRESS_LZO; 743 info->compress_level = 0; 744 btrfs_set_opt(info->mount_opt, COMPRESS); 745 btrfs_clear_opt(info->mount_opt, NODATACOW); 746 btrfs_clear_opt(info->mount_opt, NODATASUM); 747 btrfs_set_fs_incompat(info, COMPRESS_LZO); 748 no_compress = 0; 749 } else if (strncmp(args[0].from, "zstd", 4) == 0) { 750 compress_type = "zstd"; 751 info->compress_type = BTRFS_COMPRESS_ZSTD; 752 info->compress_level = 753 btrfs_compress_str2level( 754 BTRFS_COMPRESS_ZSTD, 755 args[0].from + 4); 756 btrfs_set_opt(info->mount_opt, COMPRESS); 757 btrfs_clear_opt(info->mount_opt, NODATACOW); 758 btrfs_clear_opt(info->mount_opt, NODATASUM); 759 btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 760 no_compress = 0; 761 } else if (strncmp(args[0].from, "no", 2) == 0) { 762 compress_type = "no"; 763 info->compress_level = 0; 764 info->compress_type = 0; 765 btrfs_clear_opt(info->mount_opt, COMPRESS); 766 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 767 compress_force = false; 768 no_compress++; 769 } else { 770 btrfs_err(info, "unrecognized compression value %s", 771 args[0].from); 772 ret = -EINVAL; 773 goto out; 774 } 775 776 if (compress_force) { 777 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 778 } else { 779 /* 780 * If we remount from compress-force=xxx to 781 * compress=xxx, we need clear FORCE_COMPRESS 782 * flag, otherwise, there is no way for users 783 * to disable forcible compression separately. 784 */ 785 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 786 } 787 if (no_compress == 1) { 788 btrfs_info(info, "use no compression"); 789 } else if ((info->compress_type != saved_compress_type) || 790 (compress_force != saved_compress_force) || 791 (info->compress_level != saved_compress_level)) { 792 btrfs_info(info, "%s %s compression, level %d", 793 (compress_force) ? "force" : "use", 794 compress_type, info->compress_level); 795 } 796 compress_force = false; 797 break; 798 case Opt_ssd: 799 btrfs_set_and_info(info, SSD, 800 "enabling ssd optimizations"); 801 btrfs_clear_opt(info->mount_opt, NOSSD); 802 break; 803 case Opt_ssd_spread: 804 btrfs_set_and_info(info, SSD, 805 "enabling ssd optimizations"); 806 btrfs_set_and_info(info, SSD_SPREAD, 807 "using spread ssd allocation scheme"); 808 btrfs_clear_opt(info->mount_opt, NOSSD); 809 break; 810 case Opt_nossd: 811 btrfs_set_opt(info->mount_opt, NOSSD); 812 btrfs_clear_and_info(info, SSD, 813 "not using ssd optimizations"); 814 fallthrough; 815 case Opt_nossd_spread: 816 btrfs_clear_and_info(info, SSD_SPREAD, 817 "not using spread ssd allocation scheme"); 818 break; 819 case Opt_barrier: 820 btrfs_clear_and_info(info, NOBARRIER, 821 "turning on barriers"); 822 break; 823 case Opt_nobarrier: 824 btrfs_set_and_info(info, NOBARRIER, 825 "turning off barriers"); 826 break; 827 case Opt_thread_pool: 828 ret = match_int(&args[0], &intarg); 829 if (ret) { 830 btrfs_err(info, "unrecognized thread_pool value %s", 831 args[0].from); 832 goto out; 833 } else if (intarg == 0) { 834 btrfs_err(info, "invalid value 0 for thread_pool"); 835 ret = -EINVAL; 836 goto out; 837 } 838 info->thread_pool_size = intarg; 839 break; 840 case Opt_max_inline: 841 num = match_strdup(&args[0]); 842 if (num) { 843 info->max_inline = memparse(num, NULL); 844 kfree(num); 845 846 if (info->max_inline) { 847 info->max_inline = min_t(u64, 848 info->max_inline, 849 info->sectorsize); 850 } 851 btrfs_info(info, "max_inline at %llu", 852 info->max_inline); 853 } else { 854 ret = -ENOMEM; 855 goto out; 856 } 857 break; 858 case Opt_acl: 859 #ifdef CONFIG_BTRFS_FS_POSIX_ACL 860 info->sb->s_flags |= SB_POSIXACL; 861 break; 862 #else 863 btrfs_err(info, "support for ACL not compiled in!"); 864 ret = -EINVAL; 865 goto out; 866 #endif 867 case Opt_noacl: 868 info->sb->s_flags &= ~SB_POSIXACL; 869 break; 870 case Opt_notreelog: 871 btrfs_set_and_info(info, NOTREELOG, 872 "disabling tree log"); 873 break; 874 case Opt_treelog: 875 btrfs_clear_and_info(info, NOTREELOG, 876 "enabling tree log"); 877 break; 878 case Opt_norecovery: 879 case Opt_nologreplay: 880 btrfs_warn(info, 881 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead"); 882 btrfs_set_and_info(info, NOLOGREPLAY, 883 "disabling log replay at mount time"); 884 break; 885 case Opt_flushoncommit: 886 btrfs_set_and_info(info, FLUSHONCOMMIT, 887 "turning on flush-on-commit"); 888 break; 889 case Opt_noflushoncommit: 890 btrfs_clear_and_info(info, FLUSHONCOMMIT, 891 "turning off flush-on-commit"); 892 break; 893 case Opt_ratio: 894 ret = match_int(&args[0], &intarg); 895 if (ret) { 896 btrfs_err(info, "unrecognized metadata_ratio value %s", 897 args[0].from); 898 goto out; 899 } 900 info->metadata_ratio = intarg; 901 btrfs_info(info, "metadata ratio %u", 902 info->metadata_ratio); 903 break; 904 case Opt_discard: 905 case Opt_discard_mode: 906 if (token == Opt_discard || 907 strcmp(args[0].from, "sync") == 0) { 908 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC); 909 btrfs_set_and_info(info, DISCARD_SYNC, 910 "turning on sync discard"); 911 } else if (strcmp(args[0].from, "async") == 0) { 912 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC); 913 btrfs_set_and_info(info, DISCARD_ASYNC, 914 "turning on async discard"); 915 } else { 916 btrfs_err(info, "unrecognized discard mode value %s", 917 args[0].from); 918 ret = -EINVAL; 919 goto out; 920 } 921 btrfs_clear_opt(info->mount_opt, NODISCARD); 922 break; 923 case Opt_nodiscard: 924 btrfs_clear_and_info(info, DISCARD_SYNC, 925 "turning off discard"); 926 btrfs_clear_and_info(info, DISCARD_ASYNC, 927 "turning off async discard"); 928 btrfs_set_opt(info->mount_opt, NODISCARD); 929 break; 930 case Opt_space_cache: 931 case Opt_space_cache_version: 932 /* 933 * We already set FREE_SPACE_TREE above because we have 934 * compat_ro(FREE_SPACE_TREE) set, and we aren't going 935 * to allow v1 to be set for extent tree v2, simply 936 * ignore this setting if we're extent tree v2. 937 */ 938 if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) 939 break; 940 if (token == Opt_space_cache || 941 strcmp(args[0].from, "v1") == 0) { 942 btrfs_clear_opt(info->mount_opt, 943 FREE_SPACE_TREE); 944 btrfs_set_and_info(info, SPACE_CACHE, 945 "enabling disk space caching"); 946 } else if (strcmp(args[0].from, "v2") == 0) { 947 btrfs_clear_opt(info->mount_opt, 948 SPACE_CACHE); 949 btrfs_set_and_info(info, FREE_SPACE_TREE, 950 "enabling free space tree"); 951 } else { 952 btrfs_err(info, "unrecognized space_cache value %s", 953 args[0].from); 954 ret = -EINVAL; 955 goto out; 956 } 957 break; 958 case Opt_rescan_uuid_tree: 959 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 960 break; 961 case Opt_no_space_cache: 962 /* 963 * We cannot operate without the free space tree with 964 * extent tree v2, ignore this option. 965 */ 966 if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) 967 break; 968 if (btrfs_test_opt(info, SPACE_CACHE)) { 969 btrfs_clear_and_info(info, SPACE_CACHE, 970 "disabling disk space caching"); 971 } 972 if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 973 btrfs_clear_and_info(info, FREE_SPACE_TREE, 974 "disabling free space tree"); 975 } 976 break; 977 case Opt_inode_cache: 978 case Opt_noinode_cache: 979 btrfs_warn(info, 980 "the 'inode_cache' option is deprecated and has no effect since 5.11"); 981 break; 982 case Opt_clear_cache: 983 /* 984 * We cannot clear the free space tree with extent tree 985 * v2, ignore this option. 986 */ 987 if (btrfs_fs_incompat(info, EXTENT_TREE_V2)) 988 break; 989 btrfs_set_and_info(info, CLEAR_CACHE, 990 "force clearing of disk cache"); 991 break; 992 case Opt_user_subvol_rm_allowed: 993 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 994 break; 995 case Opt_enospc_debug: 996 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 997 break; 998 case Opt_noenospc_debug: 999 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 1000 break; 1001 case Opt_defrag: 1002 btrfs_set_and_info(info, AUTO_DEFRAG, 1003 "enabling auto defrag"); 1004 break; 1005 case Opt_nodefrag: 1006 btrfs_clear_and_info(info, AUTO_DEFRAG, 1007 "disabling auto defrag"); 1008 break; 1009 case Opt_recovery: 1010 case Opt_usebackuproot: 1011 btrfs_warn(info, 1012 "'%s' is deprecated, use 'rescue=usebackuproot' instead", 1013 token == Opt_recovery ? "recovery" : 1014 "usebackuproot"); 1015 btrfs_info(info, 1016 "trying to use backup root at mount time"); 1017 btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 1018 break; 1019 case Opt_skip_balance: 1020 btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 1021 break; 1022 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 1023 case Opt_check_integrity_including_extent_data: 1024 btrfs_info(info, 1025 "enabling check integrity including extent data"); 1026 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA); 1027 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 1028 break; 1029 case Opt_check_integrity: 1030 btrfs_info(info, "enabling check integrity"); 1031 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 1032 break; 1033 case Opt_check_integrity_print_mask: 1034 ret = match_int(&args[0], &intarg); 1035 if (ret) { 1036 btrfs_err(info, 1037 "unrecognized check_integrity_print_mask value %s", 1038 args[0].from); 1039 goto out; 1040 } 1041 info->check_integrity_print_mask = intarg; 1042 btrfs_info(info, "check_integrity_print_mask 0x%x", 1043 info->check_integrity_print_mask); 1044 break; 1045 #else 1046 case Opt_check_integrity_including_extent_data: 1047 case Opt_check_integrity: 1048 case Opt_check_integrity_print_mask: 1049 btrfs_err(info, 1050 "support for check_integrity* not compiled in!"); 1051 ret = -EINVAL; 1052 goto out; 1053 #endif 1054 case Opt_fatal_errors: 1055 if (strcmp(args[0].from, "panic") == 0) { 1056 btrfs_set_opt(info->mount_opt, 1057 PANIC_ON_FATAL_ERROR); 1058 } else if (strcmp(args[0].from, "bug") == 0) { 1059 btrfs_clear_opt(info->mount_opt, 1060 PANIC_ON_FATAL_ERROR); 1061 } else { 1062 btrfs_err(info, "unrecognized fatal_errors value %s", 1063 args[0].from); 1064 ret = -EINVAL; 1065 goto out; 1066 } 1067 break; 1068 case Opt_commit_interval: 1069 intarg = 0; 1070 ret = match_int(&args[0], &intarg); 1071 if (ret) { 1072 btrfs_err(info, "unrecognized commit_interval value %s", 1073 args[0].from); 1074 ret = -EINVAL; 1075 goto out; 1076 } 1077 if (intarg == 0) { 1078 btrfs_info(info, 1079 "using default commit interval %us", 1080 BTRFS_DEFAULT_COMMIT_INTERVAL); 1081 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL; 1082 } else if (intarg > 300) { 1083 btrfs_warn(info, "excessive commit interval %d", 1084 intarg); 1085 } 1086 info->commit_interval = intarg; 1087 break; 1088 case Opt_rescue: 1089 ret = parse_rescue_options(info, args[0].from); 1090 if (ret < 0) { 1091 btrfs_err(info, "unrecognized rescue value %s", 1092 args[0].from); 1093 goto out; 1094 } 1095 break; 1096 #ifdef CONFIG_BTRFS_DEBUG 1097 case Opt_fragment_all: 1098 btrfs_info(info, "fragmenting all space"); 1099 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 1100 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 1101 break; 1102 case Opt_fragment_metadata: 1103 btrfs_info(info, "fragmenting metadata"); 1104 btrfs_set_opt(info->mount_opt, 1105 FRAGMENT_METADATA); 1106 break; 1107 case Opt_fragment_data: 1108 btrfs_info(info, "fragmenting data"); 1109 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 1110 break; 1111 #endif 1112 #ifdef CONFIG_BTRFS_FS_REF_VERIFY 1113 case Opt_ref_verify: 1114 btrfs_info(info, "doing ref verification"); 1115 btrfs_set_opt(info->mount_opt, REF_VERIFY); 1116 break; 1117 #endif 1118 case Opt_err: 1119 btrfs_err(info, "unrecognized mount option '%s'", p); 1120 ret = -EINVAL; 1121 goto out; 1122 default: 1123 break; 1124 } 1125 } 1126 check: 1127 /* We're read-only, don't have to check. */ 1128 if (new_flags & SB_RDONLY) 1129 goto out; 1130 1131 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") || 1132 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") || 1133 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums")) 1134 ret = -EINVAL; 1135 out: 1136 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 1137 !btrfs_test_opt(info, FREE_SPACE_TREE) && 1138 !btrfs_test_opt(info, CLEAR_CACHE)) { 1139 btrfs_err(info, "cannot disable free space tree"); 1140 ret = -EINVAL; 1141 1142 } 1143 if (!ret) 1144 ret = btrfs_check_mountopts_zoned(info); 1145 if (!ret && !remounting) { 1146 if (btrfs_test_opt(info, SPACE_CACHE)) 1147 btrfs_info(info, "disk space caching is enabled"); 1148 if (btrfs_test_opt(info, FREE_SPACE_TREE)) 1149 btrfs_info(info, "using free space tree"); 1150 } 1151 return ret; 1152 } 1153 1154 /* 1155 * Parse mount options that are required early in the mount process. 1156 * 1157 * All other options will be parsed on much later in the mount process and 1158 * only when we need to allocate a new super block. 1159 */ 1160 static int btrfs_parse_device_options(const char *options, fmode_t flags, 1161 void *holder) 1162 { 1163 substring_t args[MAX_OPT_ARGS]; 1164 char *device_name, *opts, *orig, *p; 1165 struct btrfs_device *device = NULL; 1166 int error = 0; 1167 1168 lockdep_assert_held(&uuid_mutex); 1169 1170 if (!options) 1171 return 0; 1172 1173 /* 1174 * strsep changes the string, duplicate it because btrfs_parse_options 1175 * gets called later 1176 */ 1177 opts = kstrdup(options, GFP_KERNEL); 1178 if (!opts) 1179 return -ENOMEM; 1180 orig = opts; 1181 1182 while ((p = strsep(&opts, ",")) != NULL) { 1183 int token; 1184 1185 if (!*p) 1186 continue; 1187 1188 token = match_token(p, tokens, args); 1189 if (token == Opt_device) { 1190 device_name = match_strdup(&args[0]); 1191 if (!device_name) { 1192 error = -ENOMEM; 1193 goto out; 1194 } 1195 device = btrfs_scan_one_device(device_name, flags, 1196 holder); 1197 kfree(device_name); 1198 if (IS_ERR(device)) { 1199 error = PTR_ERR(device); 1200 goto out; 1201 } 1202 } 1203 } 1204 1205 out: 1206 kfree(orig); 1207 return error; 1208 } 1209 1210 /* 1211 * Parse mount options that are related to subvolume id 1212 * 1213 * The value is later passed to mount_subvol() 1214 */ 1215 static int btrfs_parse_subvol_options(const char *options, char **subvol_name, 1216 u64 *subvol_objectid) 1217 { 1218 substring_t args[MAX_OPT_ARGS]; 1219 char *opts, *orig, *p; 1220 int error = 0; 1221 u64 subvolid; 1222 1223 if (!options) 1224 return 0; 1225 1226 /* 1227 * strsep changes the string, duplicate it because 1228 * btrfs_parse_device_options gets called later 1229 */ 1230 opts = kstrdup(options, GFP_KERNEL); 1231 if (!opts) 1232 return -ENOMEM; 1233 orig = opts; 1234 1235 while ((p = strsep(&opts, ",")) != NULL) { 1236 int token; 1237 if (!*p) 1238 continue; 1239 1240 token = match_token(p, tokens, args); 1241 switch (token) { 1242 case Opt_subvol: 1243 kfree(*subvol_name); 1244 *subvol_name = match_strdup(&args[0]); 1245 if (!*subvol_name) { 1246 error = -ENOMEM; 1247 goto out; 1248 } 1249 break; 1250 case Opt_subvolid: 1251 error = match_u64(&args[0], &subvolid); 1252 if (error) 1253 goto out; 1254 1255 /* we want the original fs_tree */ 1256 if (subvolid == 0) 1257 subvolid = BTRFS_FS_TREE_OBJECTID; 1258 1259 *subvol_objectid = subvolid; 1260 break; 1261 default: 1262 break; 1263 } 1264 } 1265 1266 out: 1267 kfree(orig); 1268 return error; 1269 } 1270 1271 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 1272 u64 subvol_objectid) 1273 { 1274 struct btrfs_root *root = fs_info->tree_root; 1275 struct btrfs_root *fs_root = NULL; 1276 struct btrfs_root_ref *root_ref; 1277 struct btrfs_inode_ref *inode_ref; 1278 struct btrfs_key key; 1279 struct btrfs_path *path = NULL; 1280 char *name = NULL, *ptr; 1281 u64 dirid; 1282 int len; 1283 int ret; 1284 1285 path = btrfs_alloc_path(); 1286 if (!path) { 1287 ret = -ENOMEM; 1288 goto err; 1289 } 1290 1291 name = kmalloc(PATH_MAX, GFP_KERNEL); 1292 if (!name) { 1293 ret = -ENOMEM; 1294 goto err; 1295 } 1296 ptr = name + PATH_MAX - 1; 1297 ptr[0] = '\0'; 1298 1299 /* 1300 * Walk up the subvolume trees in the tree of tree roots by root 1301 * backrefs until we hit the top-level subvolume. 1302 */ 1303 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 1304 key.objectid = subvol_objectid; 1305 key.type = BTRFS_ROOT_BACKREF_KEY; 1306 key.offset = (u64)-1; 1307 1308 ret = btrfs_search_backwards(root, &key, path); 1309 if (ret < 0) { 1310 goto err; 1311 } else if (ret > 0) { 1312 ret = -ENOENT; 1313 goto err; 1314 } 1315 1316 subvol_objectid = key.offset; 1317 1318 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 1319 struct btrfs_root_ref); 1320 len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 1321 ptr -= len + 1; 1322 if (ptr < name) { 1323 ret = -ENAMETOOLONG; 1324 goto err; 1325 } 1326 read_extent_buffer(path->nodes[0], ptr + 1, 1327 (unsigned long)(root_ref + 1), len); 1328 ptr[0] = '/'; 1329 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 1330 btrfs_release_path(path); 1331 1332 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true); 1333 if (IS_ERR(fs_root)) { 1334 ret = PTR_ERR(fs_root); 1335 fs_root = NULL; 1336 goto err; 1337 } 1338 1339 /* 1340 * Walk up the filesystem tree by inode refs until we hit the 1341 * root directory. 1342 */ 1343 while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 1344 key.objectid = dirid; 1345 key.type = BTRFS_INODE_REF_KEY; 1346 key.offset = (u64)-1; 1347 1348 ret = btrfs_search_backwards(fs_root, &key, path); 1349 if (ret < 0) { 1350 goto err; 1351 } else if (ret > 0) { 1352 ret = -ENOENT; 1353 goto err; 1354 } 1355 1356 dirid = key.offset; 1357 1358 inode_ref = btrfs_item_ptr(path->nodes[0], 1359 path->slots[0], 1360 struct btrfs_inode_ref); 1361 len = btrfs_inode_ref_name_len(path->nodes[0], 1362 inode_ref); 1363 ptr -= len + 1; 1364 if (ptr < name) { 1365 ret = -ENAMETOOLONG; 1366 goto err; 1367 } 1368 read_extent_buffer(path->nodes[0], ptr + 1, 1369 (unsigned long)(inode_ref + 1), len); 1370 ptr[0] = '/'; 1371 btrfs_release_path(path); 1372 } 1373 btrfs_put_root(fs_root); 1374 fs_root = NULL; 1375 } 1376 1377 btrfs_free_path(path); 1378 if (ptr == name + PATH_MAX - 1) { 1379 name[0] = '/'; 1380 name[1] = '\0'; 1381 } else { 1382 memmove(name, ptr, name + PATH_MAX - ptr); 1383 } 1384 return name; 1385 1386 err: 1387 btrfs_put_root(fs_root); 1388 btrfs_free_path(path); 1389 kfree(name); 1390 return ERR_PTR(ret); 1391 } 1392 1393 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 1394 { 1395 struct btrfs_root *root = fs_info->tree_root; 1396 struct btrfs_dir_item *di; 1397 struct btrfs_path *path; 1398 struct btrfs_key location; 1399 u64 dir_id; 1400 1401 path = btrfs_alloc_path(); 1402 if (!path) 1403 return -ENOMEM; 1404 1405 /* 1406 * Find the "default" dir item which points to the root item that we 1407 * will mount by default if we haven't been given a specific subvolume 1408 * to mount. 1409 */ 1410 dir_id = btrfs_super_root_dir(fs_info->super_copy); 1411 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1412 if (IS_ERR(di)) { 1413 btrfs_free_path(path); 1414 return PTR_ERR(di); 1415 } 1416 if (!di) { 1417 /* 1418 * Ok the default dir item isn't there. This is weird since 1419 * it's always been there, but don't freak out, just try and 1420 * mount the top-level subvolume. 1421 */ 1422 btrfs_free_path(path); 1423 *objectid = BTRFS_FS_TREE_OBJECTID; 1424 return 0; 1425 } 1426 1427 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 1428 btrfs_free_path(path); 1429 *objectid = location.objectid; 1430 return 0; 1431 } 1432 1433 static int btrfs_fill_super(struct super_block *sb, 1434 struct btrfs_fs_devices *fs_devices, 1435 void *data) 1436 { 1437 struct inode *inode; 1438 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1439 int err; 1440 1441 sb->s_maxbytes = MAX_LFS_FILESIZE; 1442 sb->s_magic = BTRFS_SUPER_MAGIC; 1443 sb->s_op = &btrfs_super_ops; 1444 sb->s_d_op = &btrfs_dentry_operations; 1445 sb->s_export_op = &btrfs_export_ops; 1446 #ifdef CONFIG_FS_VERITY 1447 sb->s_vop = &btrfs_verityops; 1448 #endif 1449 sb->s_xattr = btrfs_xattr_handlers; 1450 sb->s_time_gran = 1; 1451 #ifdef CONFIG_BTRFS_FS_POSIX_ACL 1452 sb->s_flags |= SB_POSIXACL; 1453 #endif 1454 sb->s_flags |= SB_I_VERSION; 1455 sb->s_iflags |= SB_I_CGROUPWB; 1456 1457 err = super_setup_bdi(sb); 1458 if (err) { 1459 btrfs_err(fs_info, "super_setup_bdi failed"); 1460 return err; 1461 } 1462 1463 err = open_ctree(sb, fs_devices, (char *)data); 1464 if (err) { 1465 btrfs_err(fs_info, "open_ctree failed"); 1466 return err; 1467 } 1468 1469 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root); 1470 if (IS_ERR(inode)) { 1471 err = PTR_ERR(inode); 1472 goto fail_close; 1473 } 1474 1475 sb->s_root = d_make_root(inode); 1476 if (!sb->s_root) { 1477 err = -ENOMEM; 1478 goto fail_close; 1479 } 1480 1481 sb->s_flags |= SB_ACTIVE; 1482 return 0; 1483 1484 fail_close: 1485 close_ctree(fs_info); 1486 return err; 1487 } 1488 1489 int btrfs_sync_fs(struct super_block *sb, int wait) 1490 { 1491 struct btrfs_trans_handle *trans; 1492 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1493 struct btrfs_root *root = fs_info->tree_root; 1494 1495 trace_btrfs_sync_fs(fs_info, wait); 1496 1497 if (!wait) { 1498 filemap_flush(fs_info->btree_inode->i_mapping); 1499 return 0; 1500 } 1501 1502 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1503 1504 trans = btrfs_attach_transaction_barrier(root); 1505 if (IS_ERR(trans)) { 1506 /* no transaction, don't bother */ 1507 if (PTR_ERR(trans) == -ENOENT) { 1508 /* 1509 * Exit unless we have some pending changes 1510 * that need to go through commit 1511 */ 1512 if (fs_info->pending_changes == 0) 1513 return 0; 1514 /* 1515 * A non-blocking test if the fs is frozen. We must not 1516 * start a new transaction here otherwise a deadlock 1517 * happens. The pending operations are delayed to the 1518 * next commit after thawing. 1519 */ 1520 if (sb_start_write_trylock(sb)) 1521 sb_end_write(sb); 1522 else 1523 return 0; 1524 trans = btrfs_start_transaction(root, 0); 1525 } 1526 if (IS_ERR(trans)) 1527 return PTR_ERR(trans); 1528 } 1529 return btrfs_commit_transaction(trans); 1530 } 1531 1532 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed) 1533 { 1534 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s); 1535 *printed = true; 1536 } 1537 1538 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1539 { 1540 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1541 const char *compress_type; 1542 const char *subvol_name; 1543 bool printed = false; 1544 1545 if (btrfs_test_opt(info, DEGRADED)) 1546 seq_puts(seq, ",degraded"); 1547 if (btrfs_test_opt(info, NODATASUM)) 1548 seq_puts(seq, ",nodatasum"); 1549 if (btrfs_test_opt(info, NODATACOW)) 1550 seq_puts(seq, ",nodatacow"); 1551 if (btrfs_test_opt(info, NOBARRIER)) 1552 seq_puts(seq, ",nobarrier"); 1553 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1554 seq_printf(seq, ",max_inline=%llu", info->max_inline); 1555 if (info->thread_pool_size != min_t(unsigned long, 1556 num_online_cpus() + 2, 8)) 1557 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 1558 if (btrfs_test_opt(info, COMPRESS)) { 1559 compress_type = btrfs_compress_type2str(info->compress_type); 1560 if (btrfs_test_opt(info, FORCE_COMPRESS)) 1561 seq_printf(seq, ",compress-force=%s", compress_type); 1562 else 1563 seq_printf(seq, ",compress=%s", compress_type); 1564 if (info->compress_level) 1565 seq_printf(seq, ":%d", info->compress_level); 1566 } 1567 if (btrfs_test_opt(info, NOSSD)) 1568 seq_puts(seq, ",nossd"); 1569 if (btrfs_test_opt(info, SSD_SPREAD)) 1570 seq_puts(seq, ",ssd_spread"); 1571 else if (btrfs_test_opt(info, SSD)) 1572 seq_puts(seq, ",ssd"); 1573 if (btrfs_test_opt(info, NOTREELOG)) 1574 seq_puts(seq, ",notreelog"); 1575 if (btrfs_test_opt(info, NOLOGREPLAY)) 1576 print_rescue_option(seq, "nologreplay", &printed); 1577 if (btrfs_test_opt(info, USEBACKUPROOT)) 1578 print_rescue_option(seq, "usebackuproot", &printed); 1579 if (btrfs_test_opt(info, IGNOREBADROOTS)) 1580 print_rescue_option(seq, "ignorebadroots", &printed); 1581 if (btrfs_test_opt(info, IGNOREDATACSUMS)) 1582 print_rescue_option(seq, "ignoredatacsums", &printed); 1583 if (btrfs_test_opt(info, FLUSHONCOMMIT)) 1584 seq_puts(seq, ",flushoncommit"); 1585 if (btrfs_test_opt(info, DISCARD_SYNC)) 1586 seq_puts(seq, ",discard"); 1587 if (btrfs_test_opt(info, DISCARD_ASYNC)) 1588 seq_puts(seq, ",discard=async"); 1589 if (!(info->sb->s_flags & SB_POSIXACL)) 1590 seq_puts(seq, ",noacl"); 1591 if (btrfs_free_space_cache_v1_active(info)) 1592 seq_puts(seq, ",space_cache"); 1593 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 1594 seq_puts(seq, ",space_cache=v2"); 1595 else 1596 seq_puts(seq, ",nospace_cache"); 1597 if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1598 seq_puts(seq, ",rescan_uuid_tree"); 1599 if (btrfs_test_opt(info, CLEAR_CACHE)) 1600 seq_puts(seq, ",clear_cache"); 1601 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1602 seq_puts(seq, ",user_subvol_rm_allowed"); 1603 if (btrfs_test_opt(info, ENOSPC_DEBUG)) 1604 seq_puts(seq, ",enospc_debug"); 1605 if (btrfs_test_opt(info, AUTO_DEFRAG)) 1606 seq_puts(seq, ",autodefrag"); 1607 if (btrfs_test_opt(info, SKIP_BALANCE)) 1608 seq_puts(seq, ",skip_balance"); 1609 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 1610 if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA)) 1611 seq_puts(seq, ",check_int_data"); 1612 else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 1613 seq_puts(seq, ",check_int"); 1614 if (info->check_integrity_print_mask) 1615 seq_printf(seq, ",check_int_print_mask=%d", 1616 info->check_integrity_print_mask); 1617 #endif 1618 if (info->metadata_ratio) 1619 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio); 1620 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 1621 seq_puts(seq, ",fatal_errors=panic"); 1622 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 1623 seq_printf(seq, ",commit=%u", info->commit_interval); 1624 #ifdef CONFIG_BTRFS_DEBUG 1625 if (btrfs_test_opt(info, FRAGMENT_DATA)) 1626 seq_puts(seq, ",fragment=data"); 1627 if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1628 seq_puts(seq, ",fragment=metadata"); 1629 #endif 1630 if (btrfs_test_opt(info, REF_VERIFY)) 1631 seq_puts(seq, ",ref_verify"); 1632 seq_printf(seq, ",subvolid=%llu", 1633 BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1634 subvol_name = btrfs_get_subvol_name_from_objectid(info, 1635 BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1636 if (!IS_ERR(subvol_name)) { 1637 seq_puts(seq, ",subvol="); 1638 seq_escape(seq, subvol_name, " \t\n\\"); 1639 kfree(subvol_name); 1640 } 1641 return 0; 1642 } 1643 1644 static int btrfs_test_super(struct super_block *s, void *data) 1645 { 1646 struct btrfs_fs_info *p = data; 1647 struct btrfs_fs_info *fs_info = btrfs_sb(s); 1648 1649 return fs_info->fs_devices == p->fs_devices; 1650 } 1651 1652 static int btrfs_set_super(struct super_block *s, void *data) 1653 { 1654 int err = set_anon_super(s, data); 1655 if (!err) 1656 s->s_fs_info = data; 1657 return err; 1658 } 1659 1660 /* 1661 * subvolumes are identified by ino 256 1662 */ 1663 static inline int is_subvolume_inode(struct inode *inode) 1664 { 1665 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1666 return 1; 1667 return 0; 1668 } 1669 1670 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1671 struct vfsmount *mnt) 1672 { 1673 struct dentry *root; 1674 int ret; 1675 1676 if (!subvol_name) { 1677 if (!subvol_objectid) { 1678 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 1679 &subvol_objectid); 1680 if (ret) { 1681 root = ERR_PTR(ret); 1682 goto out; 1683 } 1684 } 1685 subvol_name = btrfs_get_subvol_name_from_objectid( 1686 btrfs_sb(mnt->mnt_sb), subvol_objectid); 1687 if (IS_ERR(subvol_name)) { 1688 root = ERR_CAST(subvol_name); 1689 subvol_name = NULL; 1690 goto out; 1691 } 1692 1693 } 1694 1695 root = mount_subtree(mnt, subvol_name); 1696 /* mount_subtree() drops our reference on the vfsmount. */ 1697 mnt = NULL; 1698 1699 if (!IS_ERR(root)) { 1700 struct super_block *s = root->d_sb; 1701 struct btrfs_fs_info *fs_info = btrfs_sb(s); 1702 struct inode *root_inode = d_inode(root); 1703 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1704 1705 ret = 0; 1706 if (!is_subvolume_inode(root_inode)) { 1707 btrfs_err(fs_info, "'%s' is not a valid subvolume", 1708 subvol_name); 1709 ret = -EINVAL; 1710 } 1711 if (subvol_objectid && root_objectid != subvol_objectid) { 1712 /* 1713 * This will also catch a race condition where a 1714 * subvolume which was passed by ID is renamed and 1715 * another subvolume is renamed over the old location. 1716 */ 1717 btrfs_err(fs_info, 1718 "subvol '%s' does not match subvolid %llu", 1719 subvol_name, subvol_objectid); 1720 ret = -EINVAL; 1721 } 1722 if (ret) { 1723 dput(root); 1724 root = ERR_PTR(ret); 1725 deactivate_locked_super(s); 1726 } 1727 } 1728 1729 out: 1730 mntput(mnt); 1731 kfree(subvol_name); 1732 return root; 1733 } 1734 1735 /* 1736 * Find a superblock for the given device / mount point. 1737 * 1738 * Note: This is based on mount_bdev from fs/super.c with a few additions 1739 * for multiple device setup. Make sure to keep it in sync. 1740 */ 1741 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 1742 int flags, const char *device_name, void *data) 1743 { 1744 struct block_device *bdev = NULL; 1745 struct super_block *s; 1746 struct btrfs_device *device = NULL; 1747 struct btrfs_fs_devices *fs_devices = NULL; 1748 struct btrfs_fs_info *fs_info = NULL; 1749 void *new_sec_opts = NULL; 1750 fmode_t mode = FMODE_READ; 1751 int error = 0; 1752 1753 if (!(flags & SB_RDONLY)) 1754 mode |= FMODE_WRITE; 1755 1756 if (data) { 1757 error = security_sb_eat_lsm_opts(data, &new_sec_opts); 1758 if (error) 1759 return ERR_PTR(error); 1760 } 1761 1762 /* 1763 * Setup a dummy root and fs_info for test/set super. This is because 1764 * we don't actually fill this stuff out until open_ctree, but we need 1765 * then open_ctree will properly initialize the file system specific 1766 * settings later. btrfs_init_fs_info initializes the static elements 1767 * of the fs_info (locks and such) to make cleanup easier if we find a 1768 * superblock with our given fs_devices later on at sget() time. 1769 */ 1770 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 1771 if (!fs_info) { 1772 error = -ENOMEM; 1773 goto error_sec_opts; 1774 } 1775 btrfs_init_fs_info(fs_info); 1776 1777 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 1778 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 1779 if (!fs_info->super_copy || !fs_info->super_for_commit) { 1780 error = -ENOMEM; 1781 goto error_fs_info; 1782 } 1783 1784 mutex_lock(&uuid_mutex); 1785 error = btrfs_parse_device_options(data, mode, fs_type); 1786 if (error) { 1787 mutex_unlock(&uuid_mutex); 1788 goto error_fs_info; 1789 } 1790 1791 device = btrfs_scan_one_device(device_name, mode, fs_type); 1792 if (IS_ERR(device)) { 1793 mutex_unlock(&uuid_mutex); 1794 error = PTR_ERR(device); 1795 goto error_fs_info; 1796 } 1797 1798 fs_devices = device->fs_devices; 1799 fs_info->fs_devices = fs_devices; 1800 1801 error = btrfs_open_devices(fs_devices, mode, fs_type); 1802 mutex_unlock(&uuid_mutex); 1803 if (error) 1804 goto error_fs_info; 1805 1806 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 1807 error = -EACCES; 1808 goto error_close_devices; 1809 } 1810 1811 bdev = fs_devices->latest_dev->bdev; 1812 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 1813 fs_info); 1814 if (IS_ERR(s)) { 1815 error = PTR_ERR(s); 1816 goto error_close_devices; 1817 } 1818 1819 if (s->s_root) { 1820 btrfs_close_devices(fs_devices); 1821 btrfs_free_fs_info(fs_info); 1822 if ((flags ^ s->s_flags) & SB_RDONLY) 1823 error = -EBUSY; 1824 } else { 1825 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 1826 shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name, 1827 s->s_id); 1828 btrfs_sb(s)->bdev_holder = fs_type; 1829 if (!strstr(crc32c_impl(), "generic")) 1830 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags); 1831 error = btrfs_fill_super(s, fs_devices, data); 1832 } 1833 if (!error) 1834 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL); 1835 security_free_mnt_opts(&new_sec_opts); 1836 if (error) { 1837 deactivate_locked_super(s); 1838 return ERR_PTR(error); 1839 } 1840 1841 return dget(s->s_root); 1842 1843 error_close_devices: 1844 btrfs_close_devices(fs_devices); 1845 error_fs_info: 1846 btrfs_free_fs_info(fs_info); 1847 error_sec_opts: 1848 security_free_mnt_opts(&new_sec_opts); 1849 return ERR_PTR(error); 1850 } 1851 1852 /* 1853 * Mount function which is called by VFS layer. 1854 * 1855 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1856 * which needs vfsmount* of device's root (/). This means device's root has to 1857 * be mounted internally in any case. 1858 * 1859 * Operation flow: 1860 * 1. Parse subvol id related options for later use in mount_subvol(). 1861 * 1862 * 2. Mount device's root (/) by calling vfs_kern_mount(). 1863 * 1864 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1865 * first place. In order to avoid calling btrfs_mount() again, we use 1866 * different file_system_type which is not registered to VFS by 1867 * register_filesystem() (btrfs_root_fs_type). As a result, 1868 * btrfs_mount_root() is called. The return value will be used by 1869 * mount_subtree() in mount_subvol(). 1870 * 1871 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1872 * "btrfs subvolume set-default", mount_subvol() is called always. 1873 */ 1874 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1875 const char *device_name, void *data) 1876 { 1877 struct vfsmount *mnt_root; 1878 struct dentry *root; 1879 char *subvol_name = NULL; 1880 u64 subvol_objectid = 0; 1881 int error = 0; 1882 1883 error = btrfs_parse_subvol_options(data, &subvol_name, 1884 &subvol_objectid); 1885 if (error) { 1886 kfree(subvol_name); 1887 return ERR_PTR(error); 1888 } 1889 1890 /* mount device's root (/) */ 1891 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1892 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1893 if (flags & SB_RDONLY) { 1894 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1895 flags & ~SB_RDONLY, device_name, data); 1896 } else { 1897 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1898 flags | SB_RDONLY, device_name, data); 1899 if (IS_ERR(mnt_root)) { 1900 root = ERR_CAST(mnt_root); 1901 kfree(subvol_name); 1902 goto out; 1903 } 1904 1905 down_write(&mnt_root->mnt_sb->s_umount); 1906 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1907 up_write(&mnt_root->mnt_sb->s_umount); 1908 if (error < 0) { 1909 root = ERR_PTR(error); 1910 mntput(mnt_root); 1911 kfree(subvol_name); 1912 goto out; 1913 } 1914 } 1915 } 1916 if (IS_ERR(mnt_root)) { 1917 root = ERR_CAST(mnt_root); 1918 kfree(subvol_name); 1919 goto out; 1920 } 1921 1922 /* mount_subvol() will free subvol_name and mnt_root */ 1923 root = mount_subvol(subvol_name, subvol_objectid, mnt_root); 1924 1925 out: 1926 return root; 1927 } 1928 1929 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1930 u32 new_pool_size, u32 old_pool_size) 1931 { 1932 if (new_pool_size == old_pool_size) 1933 return; 1934 1935 fs_info->thread_pool_size = new_pool_size; 1936 1937 btrfs_info(fs_info, "resize thread pool %d -> %d", 1938 old_pool_size, new_pool_size); 1939 1940 btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1941 btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size); 1942 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1943 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1944 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1945 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 1946 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1947 } 1948 1949 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1950 unsigned long old_opts, int flags) 1951 { 1952 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1953 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1954 (flags & SB_RDONLY))) { 1955 /* wait for any defraggers to finish */ 1956 wait_event(fs_info->transaction_wait, 1957 (atomic_read(&fs_info->defrag_running) == 0)); 1958 if (flags & SB_RDONLY) 1959 sync_filesystem(fs_info->sb); 1960 } 1961 } 1962 1963 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1964 unsigned long old_opts) 1965 { 1966 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE); 1967 1968 /* 1969 * We need to cleanup all defragable inodes if the autodefragment is 1970 * close or the filesystem is read only. 1971 */ 1972 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1973 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1974 btrfs_cleanup_defrag_inodes(fs_info); 1975 } 1976 1977 /* If we toggled discard async */ 1978 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1979 btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1980 btrfs_discard_resume(fs_info); 1981 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) && 1982 !btrfs_test_opt(fs_info, DISCARD_ASYNC)) 1983 btrfs_discard_cleanup(fs_info); 1984 1985 /* If we toggled space cache */ 1986 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) 1987 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt); 1988 } 1989 1990 static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1991 { 1992 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1993 unsigned old_flags = sb->s_flags; 1994 unsigned long old_opts = fs_info->mount_opt; 1995 unsigned long old_compress_type = fs_info->compress_type; 1996 u64 old_max_inline = fs_info->max_inline; 1997 u32 old_thread_pool_size = fs_info->thread_pool_size; 1998 u32 old_metadata_ratio = fs_info->metadata_ratio; 1999 int ret; 2000 2001 sync_filesystem(sb); 2002 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 2003 2004 if (data) { 2005 void *new_sec_opts = NULL; 2006 2007 ret = security_sb_eat_lsm_opts(data, &new_sec_opts); 2008 if (!ret) 2009 ret = security_sb_remount(sb, new_sec_opts); 2010 security_free_mnt_opts(&new_sec_opts); 2011 if (ret) 2012 goto restore; 2013 } 2014 2015 ret = btrfs_parse_options(fs_info, data, *flags); 2016 if (ret) 2017 goto restore; 2018 2019 ret = btrfs_check_features(fs_info, sb); 2020 if (ret < 0) 2021 goto restore; 2022 2023 btrfs_remount_begin(fs_info, old_opts, *flags); 2024 btrfs_resize_thread_pool(fs_info, 2025 fs_info->thread_pool_size, old_thread_pool_size); 2026 2027 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) != 2028 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) && 2029 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) { 2030 btrfs_warn(fs_info, 2031 "remount supports changing free space tree only from ro to rw"); 2032 /* Make sure free space cache options match the state on disk */ 2033 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 2034 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE); 2035 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE); 2036 } 2037 if (btrfs_free_space_cache_v1_active(fs_info)) { 2038 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE); 2039 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE); 2040 } 2041 } 2042 2043 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 2044 goto out; 2045 2046 if (*flags & SB_RDONLY) { 2047 /* 2048 * this also happens on 'umount -rf' or on shutdown, when 2049 * the filesystem is busy. 2050 */ 2051 cancel_work_sync(&fs_info->async_reclaim_work); 2052 cancel_work_sync(&fs_info->async_data_reclaim_work); 2053 2054 btrfs_discard_cleanup(fs_info); 2055 2056 /* wait for the uuid_scan task to finish */ 2057 down(&fs_info->uuid_tree_rescan_sem); 2058 /* avoid complains from lockdep et al. */ 2059 up(&fs_info->uuid_tree_rescan_sem); 2060 2061 btrfs_set_sb_rdonly(sb); 2062 2063 /* 2064 * Setting SB_RDONLY will put the cleaner thread to 2065 * sleep at the next loop if it's already active. 2066 * If it's already asleep, we'll leave unused block 2067 * groups on disk until we're mounted read-write again 2068 * unless we clean them up here. 2069 */ 2070 btrfs_delete_unused_bgs(fs_info); 2071 2072 /* 2073 * The cleaner task could be already running before we set the 2074 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock). 2075 * We must make sure that after we finish the remount, i.e. after 2076 * we call btrfs_commit_super(), the cleaner can no longer start 2077 * a transaction - either because it was dropping a dead root, 2078 * running delayed iputs or deleting an unused block group (the 2079 * cleaner picked a block group from the list of unused block 2080 * groups before we were able to in the previous call to 2081 * btrfs_delete_unused_bgs()). 2082 */ 2083 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, 2084 TASK_UNINTERRUPTIBLE); 2085 2086 /* 2087 * We've set the superblock to RO mode, so we might have made 2088 * the cleaner task sleep without running all pending delayed 2089 * iputs. Go through all the delayed iputs here, so that if an 2090 * unmount happens without remounting RW we don't end up at 2091 * finishing close_ctree() with a non-empty list of delayed 2092 * iputs. 2093 */ 2094 btrfs_run_delayed_iputs(fs_info); 2095 2096 btrfs_dev_replace_suspend_for_unmount(fs_info); 2097 btrfs_scrub_cancel(fs_info); 2098 btrfs_pause_balance(fs_info); 2099 2100 /* 2101 * Pause the qgroup rescan worker if it is running. We don't want 2102 * it to be still running after we are in RO mode, as after that, 2103 * by the time we unmount, it might have left a transaction open, 2104 * so we would leak the transaction and/or crash. 2105 */ 2106 btrfs_qgroup_wait_for_completion(fs_info, false); 2107 2108 ret = btrfs_commit_super(fs_info); 2109 if (ret) 2110 goto restore; 2111 } else { 2112 if (BTRFS_FS_ERROR(fs_info)) { 2113 btrfs_err(fs_info, 2114 "Remounting read-write after error is not allowed"); 2115 ret = -EINVAL; 2116 goto restore; 2117 } 2118 if (fs_info->fs_devices->rw_devices == 0) { 2119 ret = -EACCES; 2120 goto restore; 2121 } 2122 2123 if (!btrfs_check_rw_degradable(fs_info, NULL)) { 2124 btrfs_warn(fs_info, 2125 "too many missing devices, writable remount is not allowed"); 2126 ret = -EACCES; 2127 goto restore; 2128 } 2129 2130 if (btrfs_super_log_root(fs_info->super_copy) != 0) { 2131 btrfs_warn(fs_info, 2132 "mount required to replay tree-log, cannot remount read-write"); 2133 ret = -EINVAL; 2134 goto restore; 2135 } 2136 2137 /* 2138 * NOTE: when remounting with a change that does writes, don't 2139 * put it anywhere above this point, as we are not sure to be 2140 * safe to write until we pass the above checks. 2141 */ 2142 ret = btrfs_start_pre_rw_mount(fs_info); 2143 if (ret) 2144 goto restore; 2145 2146 btrfs_clear_sb_rdonly(sb); 2147 2148 set_bit(BTRFS_FS_OPEN, &fs_info->flags); 2149 } 2150 out: 2151 /* 2152 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS, 2153 * since the absence of the flag means it can be toggled off by remount. 2154 */ 2155 *flags |= SB_I_VERSION; 2156 2157 wake_up_process(fs_info->transaction_kthread); 2158 btrfs_remount_cleanup(fs_info, old_opts); 2159 btrfs_clear_oneshot_options(fs_info); 2160 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 2161 2162 return 0; 2163 2164 restore: 2165 /* We've hit an error - don't reset SB_RDONLY */ 2166 if (sb_rdonly(sb)) 2167 old_flags |= SB_RDONLY; 2168 if (!(old_flags & SB_RDONLY)) 2169 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state); 2170 sb->s_flags = old_flags; 2171 fs_info->mount_opt = old_opts; 2172 fs_info->compress_type = old_compress_type; 2173 fs_info->max_inline = old_max_inline; 2174 btrfs_resize_thread_pool(fs_info, 2175 old_thread_pool_size, fs_info->thread_pool_size); 2176 fs_info->metadata_ratio = old_metadata_ratio; 2177 btrfs_remount_cleanup(fs_info, old_opts); 2178 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 2179 2180 return ret; 2181 } 2182 2183 /* Used to sort the devices by max_avail(descending sort) */ 2184 static int btrfs_cmp_device_free_bytes(const void *a, const void *b) 2185 { 2186 const struct btrfs_device_info *dev_info1 = a; 2187 const struct btrfs_device_info *dev_info2 = b; 2188 2189 if (dev_info1->max_avail > dev_info2->max_avail) 2190 return -1; 2191 else if (dev_info1->max_avail < dev_info2->max_avail) 2192 return 1; 2193 return 0; 2194 } 2195 2196 /* 2197 * sort the devices by max_avail, in which max free extent size of each device 2198 * is stored.(Descending Sort) 2199 */ 2200 static inline void btrfs_descending_sort_devices( 2201 struct btrfs_device_info *devices, 2202 size_t nr_devices) 2203 { 2204 sort(devices, nr_devices, sizeof(struct btrfs_device_info), 2205 btrfs_cmp_device_free_bytes, NULL); 2206 } 2207 2208 /* 2209 * The helper to calc the free space on the devices that can be used to store 2210 * file data. 2211 */ 2212 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 2213 u64 *free_bytes) 2214 { 2215 struct btrfs_device_info *devices_info; 2216 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2217 struct btrfs_device *device; 2218 u64 type; 2219 u64 avail_space; 2220 u64 min_stripe_size; 2221 int num_stripes = 1; 2222 int i = 0, nr_devices; 2223 const struct btrfs_raid_attr *rattr; 2224 2225 /* 2226 * We aren't under the device list lock, so this is racy-ish, but good 2227 * enough for our purposes. 2228 */ 2229 nr_devices = fs_info->fs_devices->open_devices; 2230 if (!nr_devices) { 2231 smp_mb(); 2232 nr_devices = fs_info->fs_devices->open_devices; 2233 ASSERT(nr_devices); 2234 if (!nr_devices) { 2235 *free_bytes = 0; 2236 return 0; 2237 } 2238 } 2239 2240 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 2241 GFP_KERNEL); 2242 if (!devices_info) 2243 return -ENOMEM; 2244 2245 /* calc min stripe number for data space allocation */ 2246 type = btrfs_data_alloc_profile(fs_info); 2247 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)]; 2248 2249 if (type & BTRFS_BLOCK_GROUP_RAID0) 2250 num_stripes = nr_devices; 2251 else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK) 2252 num_stripes = rattr->ncopies; 2253 else if (type & BTRFS_BLOCK_GROUP_RAID10) 2254 num_stripes = 4; 2255 2256 /* Adjust for more than 1 stripe per device */ 2257 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN; 2258 2259 rcu_read_lock(); 2260 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2261 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 2262 &device->dev_state) || 2263 !device->bdev || 2264 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 2265 continue; 2266 2267 if (i >= nr_devices) 2268 break; 2269 2270 avail_space = device->total_bytes - device->bytes_used; 2271 2272 /* align with stripe_len */ 2273 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN); 2274 2275 /* 2276 * Ensure we have at least min_stripe_size on top of the 2277 * reserved space on the device. 2278 */ 2279 if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size) 2280 continue; 2281 2282 avail_space -= BTRFS_DEVICE_RANGE_RESERVED; 2283 2284 devices_info[i].dev = device; 2285 devices_info[i].max_avail = avail_space; 2286 2287 i++; 2288 } 2289 rcu_read_unlock(); 2290 2291 nr_devices = i; 2292 2293 btrfs_descending_sort_devices(devices_info, nr_devices); 2294 2295 i = nr_devices - 1; 2296 avail_space = 0; 2297 while (nr_devices >= rattr->devs_min) { 2298 num_stripes = min(num_stripes, nr_devices); 2299 2300 if (devices_info[i].max_avail >= min_stripe_size) { 2301 int j; 2302 u64 alloc_size; 2303 2304 avail_space += devices_info[i].max_avail * num_stripes; 2305 alloc_size = devices_info[i].max_avail; 2306 for (j = i + 1 - num_stripes; j <= i; j++) 2307 devices_info[j].max_avail -= alloc_size; 2308 } 2309 i--; 2310 nr_devices--; 2311 } 2312 2313 kfree(devices_info); 2314 *free_bytes = avail_space; 2315 return 0; 2316 } 2317 2318 /* 2319 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2320 * 2321 * If there's a redundant raid level at DATA block groups, use the respective 2322 * multiplier to scale the sizes. 2323 * 2324 * Unused device space usage is based on simulating the chunk allocator 2325 * algorithm that respects the device sizes and order of allocations. This is 2326 * a close approximation of the actual use but there are other factors that may 2327 * change the result (like a new metadata chunk). 2328 * 2329 * If metadata is exhausted, f_bavail will be 0. 2330 */ 2331 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 2332 { 2333 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2334 struct btrfs_super_block *disk_super = fs_info->super_copy; 2335 struct btrfs_space_info *found; 2336 u64 total_used = 0; 2337 u64 total_free_data = 0; 2338 u64 total_free_meta = 0; 2339 u32 bits = fs_info->sectorsize_bits; 2340 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; 2341 unsigned factor = 1; 2342 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 2343 int ret; 2344 u64 thresh = 0; 2345 int mixed = 0; 2346 2347 list_for_each_entry(found, &fs_info->space_info, list) { 2348 if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2349 int i; 2350 2351 total_free_data += found->disk_total - found->disk_used; 2352 total_free_data -= 2353 btrfs_account_ro_block_groups_free_space(found); 2354 2355 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2356 if (!list_empty(&found->block_groups[i])) 2357 factor = btrfs_bg_type_to_factor( 2358 btrfs_raid_array[i].bg_flag); 2359 } 2360 } 2361 2362 /* 2363 * Metadata in mixed block goup profiles are accounted in data 2364 */ 2365 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2366 if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2367 mixed = 1; 2368 else 2369 total_free_meta += found->disk_total - 2370 found->disk_used; 2371 } 2372 2373 total_used += found->disk_used; 2374 } 2375 2376 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2377 buf->f_blocks >>= bits; 2378 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2379 2380 /* Account global block reserve as used, it's in logical size already */ 2381 spin_lock(&block_rsv->lock); 2382 /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 2383 if (buf->f_bfree >= block_rsv->size >> bits) 2384 buf->f_bfree -= block_rsv->size >> bits; 2385 else 2386 buf->f_bfree = 0; 2387 spin_unlock(&block_rsv->lock); 2388 2389 buf->f_bavail = div_u64(total_free_data, factor); 2390 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 2391 if (ret) 2392 return ret; 2393 buf->f_bavail += div_u64(total_free_data, factor); 2394 buf->f_bavail = buf->f_bavail >> bits; 2395 2396 /* 2397 * We calculate the remaining metadata space minus global reserve. If 2398 * this is (supposedly) smaller than zero, there's no space. But this 2399 * does not hold in practice, the exhausted state happens where's still 2400 * some positive delta. So we apply some guesswork and compare the 2401 * delta to a 4M threshold. (Practically observed delta was ~2M.) 2402 * 2403 * We probably cannot calculate the exact threshold value because this 2404 * depends on the internal reservations requested by various 2405 * operations, so some operations that consume a few metadata will 2406 * succeed even if the Avail is zero. But this is better than the other 2407 * way around. 2408 */ 2409 thresh = SZ_4M; 2410 2411 /* 2412 * We only want to claim there's no available space if we can no longer 2413 * allocate chunks for our metadata profile and our global reserve will 2414 * not fit in the free metadata space. If we aren't ->full then we 2415 * still can allocate chunks and thus are fine using the currently 2416 * calculated f_bavail. 2417 */ 2418 if (!mixed && block_rsv->space_info->full && 2419 total_free_meta - thresh < block_rsv->size) 2420 buf->f_bavail = 0; 2421 2422 buf->f_type = BTRFS_SUPER_MAGIC; 2423 buf->f_bsize = dentry->d_sb->s_blocksize; 2424 buf->f_namelen = BTRFS_NAME_LEN; 2425 2426 /* We treat it as constant endianness (it doesn't matter _which_) 2427 because we want the fsid to come out the same whether mounted 2428 on a big-endian or little-endian host */ 2429 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 2430 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 2431 /* Mask in the root object ID too, to disambiguate subvols */ 2432 buf->f_fsid.val[0] ^= 2433 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; 2434 buf->f_fsid.val[1] ^= 2435 BTRFS_I(d_inode(dentry))->root->root_key.objectid; 2436 2437 return 0; 2438 } 2439 2440 static void btrfs_kill_super(struct super_block *sb) 2441 { 2442 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2443 kill_anon_super(sb); 2444 btrfs_free_fs_info(fs_info); 2445 } 2446 2447 static struct file_system_type btrfs_fs_type = { 2448 .owner = THIS_MODULE, 2449 .name = "btrfs", 2450 .mount = btrfs_mount, 2451 .kill_sb = btrfs_kill_super, 2452 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 2453 }; 2454 2455 static struct file_system_type btrfs_root_fs_type = { 2456 .owner = THIS_MODULE, 2457 .name = "btrfs", 2458 .mount = btrfs_mount_root, 2459 .kill_sb = btrfs_kill_super, 2460 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP, 2461 }; 2462 2463 MODULE_ALIAS_FS("btrfs"); 2464 2465 static int btrfs_control_open(struct inode *inode, struct file *file) 2466 { 2467 /* 2468 * The control file's private_data is used to hold the 2469 * transaction when it is started and is used to keep 2470 * track of whether a transaction is already in progress. 2471 */ 2472 file->private_data = NULL; 2473 return 0; 2474 } 2475 2476 /* 2477 * Used by /dev/btrfs-control for devices ioctls. 2478 */ 2479 static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 2480 unsigned long arg) 2481 { 2482 struct btrfs_ioctl_vol_args *vol; 2483 struct btrfs_device *device = NULL; 2484 dev_t devt = 0; 2485 int ret = -ENOTTY; 2486 2487 if (!capable(CAP_SYS_ADMIN)) 2488 return -EPERM; 2489 2490 vol = memdup_user((void __user *)arg, sizeof(*vol)); 2491 if (IS_ERR(vol)) 2492 return PTR_ERR(vol); 2493 vol->name[BTRFS_PATH_NAME_MAX] = '\0'; 2494 2495 switch (cmd) { 2496 case BTRFS_IOC_SCAN_DEV: 2497 mutex_lock(&uuid_mutex); 2498 device = btrfs_scan_one_device(vol->name, FMODE_READ, 2499 &btrfs_root_fs_type); 2500 ret = PTR_ERR_OR_ZERO(device); 2501 mutex_unlock(&uuid_mutex); 2502 break; 2503 case BTRFS_IOC_FORGET_DEV: 2504 if (vol->name[0] != 0) { 2505 ret = lookup_bdev(vol->name, &devt); 2506 if (ret) 2507 break; 2508 } 2509 ret = btrfs_forget_devices(devt); 2510 break; 2511 case BTRFS_IOC_DEVICES_READY: 2512 mutex_lock(&uuid_mutex); 2513 device = btrfs_scan_one_device(vol->name, FMODE_READ, 2514 &btrfs_root_fs_type); 2515 if (IS_ERR(device)) { 2516 mutex_unlock(&uuid_mutex); 2517 ret = PTR_ERR(device); 2518 break; 2519 } 2520 ret = !(device->fs_devices->num_devices == 2521 device->fs_devices->total_devices); 2522 mutex_unlock(&uuid_mutex); 2523 break; 2524 case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2525 ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2526 break; 2527 } 2528 2529 kfree(vol); 2530 return ret; 2531 } 2532 2533 static int btrfs_freeze(struct super_block *sb) 2534 { 2535 struct btrfs_trans_handle *trans; 2536 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2537 struct btrfs_root *root = fs_info->tree_root; 2538 2539 set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 2540 /* 2541 * We don't need a barrier here, we'll wait for any transaction that 2542 * could be in progress on other threads (and do delayed iputs that 2543 * we want to avoid on a frozen filesystem), or do the commit 2544 * ourselves. 2545 */ 2546 trans = btrfs_attach_transaction_barrier(root); 2547 if (IS_ERR(trans)) { 2548 /* no transaction, don't bother */ 2549 if (PTR_ERR(trans) == -ENOENT) 2550 return 0; 2551 return PTR_ERR(trans); 2552 } 2553 return btrfs_commit_transaction(trans); 2554 } 2555 2556 static int check_dev_super(struct btrfs_device *dev) 2557 { 2558 struct btrfs_fs_info *fs_info = dev->fs_info; 2559 struct btrfs_super_block *sb; 2560 u16 csum_type; 2561 int ret = 0; 2562 2563 /* This should be called with fs still frozen. */ 2564 ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags)); 2565 2566 /* Missing dev, no need to check. */ 2567 if (!dev->bdev) 2568 return 0; 2569 2570 /* Only need to check the primary super block. */ 2571 sb = btrfs_read_dev_one_super(dev->bdev, 0, true); 2572 if (IS_ERR(sb)) 2573 return PTR_ERR(sb); 2574 2575 /* Verify the checksum. */ 2576 csum_type = btrfs_super_csum_type(sb); 2577 if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) { 2578 btrfs_err(fs_info, "csum type changed, has %u expect %u", 2579 csum_type, btrfs_super_csum_type(fs_info->super_copy)); 2580 ret = -EUCLEAN; 2581 goto out; 2582 } 2583 2584 if (btrfs_check_super_csum(fs_info, sb)) { 2585 btrfs_err(fs_info, "csum for on-disk super block no longer matches"); 2586 ret = -EUCLEAN; 2587 goto out; 2588 } 2589 2590 /* Btrfs_validate_super() includes fsid check against super->fsid. */ 2591 ret = btrfs_validate_super(fs_info, sb, 0); 2592 if (ret < 0) 2593 goto out; 2594 2595 if (btrfs_super_generation(sb) != fs_info->last_trans_committed) { 2596 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu", 2597 btrfs_super_generation(sb), 2598 fs_info->last_trans_committed); 2599 ret = -EUCLEAN; 2600 goto out; 2601 } 2602 out: 2603 btrfs_release_disk_super(sb); 2604 return ret; 2605 } 2606 2607 static int btrfs_unfreeze(struct super_block *sb) 2608 { 2609 struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2610 struct btrfs_device *device; 2611 int ret = 0; 2612 2613 /* 2614 * Make sure the fs is not changed by accident (like hibernation then 2615 * modified by other OS). 2616 * If we found anything wrong, we mark the fs error immediately. 2617 * 2618 * And since the fs is frozen, no one can modify the fs yet, thus 2619 * we don't need to hold device_list_mutex. 2620 */ 2621 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { 2622 ret = check_dev_super(device); 2623 if (ret < 0) { 2624 btrfs_handle_fs_error(fs_info, ret, 2625 "super block on devid %llu got modified unexpectedly", 2626 device->devid); 2627 break; 2628 } 2629 } 2630 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 2631 2632 /* 2633 * We still return 0, to allow VFS layer to unfreeze the fs even the 2634 * above checks failed. Since the fs is either fine or read-only, we're 2635 * safe to continue, without causing further damage. 2636 */ 2637 return 0; 2638 } 2639 2640 static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 2641 { 2642 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 2643 2644 /* 2645 * There should be always a valid pointer in latest_dev, it may be stale 2646 * for a short moment in case it's being deleted but still valid until 2647 * the end of RCU grace period. 2648 */ 2649 rcu_read_lock(); 2650 seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\"); 2651 rcu_read_unlock(); 2652 2653 return 0; 2654 } 2655 2656 static const struct super_operations btrfs_super_ops = { 2657 .drop_inode = btrfs_drop_inode, 2658 .evict_inode = btrfs_evict_inode, 2659 .put_super = btrfs_put_super, 2660 .sync_fs = btrfs_sync_fs, 2661 .show_options = btrfs_show_options, 2662 .show_devname = btrfs_show_devname, 2663 .alloc_inode = btrfs_alloc_inode, 2664 .destroy_inode = btrfs_destroy_inode, 2665 .free_inode = btrfs_free_inode, 2666 .statfs = btrfs_statfs, 2667 .remount_fs = btrfs_remount, 2668 .freeze_fs = btrfs_freeze, 2669 .unfreeze_fs = btrfs_unfreeze, 2670 }; 2671 2672 static const struct file_operations btrfs_ctl_fops = { 2673 .open = btrfs_control_open, 2674 .unlocked_ioctl = btrfs_control_ioctl, 2675 .compat_ioctl = compat_ptr_ioctl, 2676 .owner = THIS_MODULE, 2677 .llseek = noop_llseek, 2678 }; 2679 2680 static struct miscdevice btrfs_misc = { 2681 .minor = BTRFS_MINOR, 2682 .name = "btrfs-control", 2683 .fops = &btrfs_ctl_fops 2684 }; 2685 2686 MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2687 MODULE_ALIAS("devname:btrfs-control"); 2688 2689 static int __init btrfs_interface_init(void) 2690 { 2691 return misc_register(&btrfs_misc); 2692 } 2693 2694 static __cold void btrfs_interface_exit(void) 2695 { 2696 misc_deregister(&btrfs_misc); 2697 } 2698 2699 static int __init btrfs_print_mod_info(void) 2700 { 2701 static const char options[] = "" 2702 #ifdef CONFIG_BTRFS_DEBUG 2703 ", debug=on" 2704 #endif 2705 #ifdef CONFIG_BTRFS_ASSERT 2706 ", assert=on" 2707 #endif 2708 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 2709 ", integrity-checker=on" 2710 #endif 2711 #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2712 ", ref-verify=on" 2713 #endif 2714 #ifdef CONFIG_BLK_DEV_ZONED 2715 ", zoned=yes" 2716 #else 2717 ", zoned=no" 2718 #endif 2719 #ifdef CONFIG_FS_VERITY 2720 ", fsverity=yes" 2721 #else 2722 ", fsverity=no" 2723 #endif 2724 ; 2725 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options); 2726 return 0; 2727 } 2728 2729 static int register_btrfs(void) 2730 { 2731 return register_filesystem(&btrfs_fs_type); 2732 } 2733 2734 static void unregister_btrfs(void) 2735 { 2736 unregister_filesystem(&btrfs_fs_type); 2737 } 2738 2739 /* Helper structure for long init/exit functions. */ 2740 struct init_sequence { 2741 int (*init_func)(void); 2742 /* Can be NULL if the init_func doesn't need cleanup. */ 2743 void (*exit_func)(void); 2744 }; 2745 2746 static const struct init_sequence mod_init_seq[] = { 2747 { 2748 .init_func = btrfs_props_init, 2749 .exit_func = NULL, 2750 }, { 2751 .init_func = btrfs_init_sysfs, 2752 .exit_func = btrfs_exit_sysfs, 2753 }, { 2754 .init_func = btrfs_init_compress, 2755 .exit_func = btrfs_exit_compress, 2756 }, { 2757 .init_func = btrfs_init_cachep, 2758 .exit_func = btrfs_destroy_cachep, 2759 }, { 2760 .init_func = btrfs_transaction_init, 2761 .exit_func = btrfs_transaction_exit, 2762 }, { 2763 .init_func = btrfs_ctree_init, 2764 .exit_func = btrfs_ctree_exit, 2765 }, { 2766 .init_func = btrfs_free_space_init, 2767 .exit_func = btrfs_free_space_exit, 2768 }, { 2769 .init_func = extent_state_init_cachep, 2770 .exit_func = extent_state_free_cachep, 2771 }, { 2772 .init_func = extent_buffer_init_cachep, 2773 .exit_func = extent_buffer_free_cachep, 2774 }, { 2775 .init_func = btrfs_bioset_init, 2776 .exit_func = btrfs_bioset_exit, 2777 }, { 2778 .init_func = extent_map_init, 2779 .exit_func = extent_map_exit, 2780 }, { 2781 .init_func = ordered_data_init, 2782 .exit_func = ordered_data_exit, 2783 }, { 2784 .init_func = btrfs_delayed_inode_init, 2785 .exit_func = btrfs_delayed_inode_exit, 2786 }, { 2787 .init_func = btrfs_auto_defrag_init, 2788 .exit_func = btrfs_auto_defrag_exit, 2789 }, { 2790 .init_func = btrfs_delayed_ref_init, 2791 .exit_func = btrfs_delayed_ref_exit, 2792 }, { 2793 .init_func = btrfs_prelim_ref_init, 2794 .exit_func = btrfs_prelim_ref_exit, 2795 }, { 2796 .init_func = btrfs_interface_init, 2797 .exit_func = btrfs_interface_exit, 2798 }, { 2799 .init_func = btrfs_print_mod_info, 2800 .exit_func = NULL, 2801 }, { 2802 .init_func = btrfs_run_sanity_tests, 2803 .exit_func = NULL, 2804 }, { 2805 .init_func = register_btrfs, 2806 .exit_func = unregister_btrfs, 2807 } 2808 }; 2809 2810 static bool mod_init_result[ARRAY_SIZE(mod_init_seq)]; 2811 2812 static void __exit exit_btrfs_fs(void) 2813 { 2814 int i; 2815 2816 for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) { 2817 if (!mod_init_result[i]) 2818 continue; 2819 if (mod_init_seq[i].exit_func) 2820 mod_init_seq[i].exit_func(); 2821 mod_init_result[i] = false; 2822 } 2823 } 2824 2825 static int __init init_btrfs_fs(void) 2826 { 2827 int ret; 2828 int i; 2829 2830 for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) { 2831 ASSERT(!mod_init_result[i]); 2832 ret = mod_init_seq[i].init_func(); 2833 if (ret < 0) 2834 goto error; 2835 mod_init_result[i] = true; 2836 } 2837 return 0; 2838 2839 error: 2840 /* 2841 * If we call exit_btrfs_fs() it would cause section mismatch. 2842 * As init_btrfs_fs() belongs to .init.text, while exit_btrfs_fs() 2843 * belongs to .exit.text. 2844 */ 2845 for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) { 2846 if (!mod_init_result[i]) 2847 continue; 2848 if (mod_init_seq[i].exit_func) 2849 mod_init_seq[i].exit_func(); 2850 mod_init_result[i] = false; 2851 } 2852 return ret; 2853 } 2854 2855 late_initcall(init_btrfs_fs); 2856 module_exit(exit_btrfs_fs) 2857 2858 MODULE_LICENSE("GPL"); 2859 MODULE_SOFTDEP("pre: crc32c"); 2860 MODULE_SOFTDEP("pre: xxhash64"); 2861 MODULE_SOFTDEP("pre: sha256"); 2862 MODULE_SOFTDEP("pre: blake2b-256"); 2863