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