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