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