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