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