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