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