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