]> git.ipfire.org Git - people/ms/linux.git/blame - fs/btrfs/super.c
Merge branch 'for-6.0/dax' into libnvdimm-fixes
[people/ms/linux.git] / fs / btrfs / super.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
6cbd5570
CM
2/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
6cbd5570
CM
4 */
5
4b82d6e4 6#include <linux/blkdev.h>
2e635a27
CM
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>
a9572a15 13#include <linux/seq_file.h>
2e635a27 14#include <linux/string.h>
2e635a27 15#include <linux/backing-dev.h>
4b82d6e4 16#include <linux/mount.h>
75dfe396 17#include <linux/writeback.h>
8fd17795 18#include <linux/statfs.h>
08607c1b 19#include <linux/compat.h>
95e05289 20#include <linux/parser.h>
c59f8951 21#include <linux/ctype.h>
6da6abae 22#include <linux/namei.h>
a9218f6b 23#include <linux/miscdevice.h>
1bcbf313 24#include <linux/magic.h>
5a0e3ad6 25#include <linux/slab.h>
22c44fe6 26#include <linux/ratelimit.h>
9678c543 27#include <linux/crc32c.h>
55e301fd 28#include <linux/btrfs.h>
16cdcec7 29#include "delayed-inode.h"
2e635a27 30#include "ctree.h"
e20d96d6 31#include "disk-io.h"
d5719762 32#include "transaction.h"
2c90e5d6 33#include "btrfs_inode.h"
3a686375 34#include "print-tree.h"
63541927 35#include "props.h"
5103e947 36#include "xattr.h"
8a4b83cc 37#include "volumes.h"
be6e8dc0 38#include "export.h"
c8b97818 39#include "compression.h"
9c5085c1 40#include "rcu-string.h"
8dabb742 41#include "dev-replace.h"
74255aa0 42#include "free-space-cache.h"
b9e9a6cb 43#include "backref.h"
8719aaae 44#include "space-info.h"
89439109 45#include "sysfs.h"
b70f5097 46#include "zoned.h"
dc11dd5d 47#include "tests/btrfs-tests.h"
aac0023c 48#include "block-group.h"
b0643e59 49#include "discard.h"
d3982100 50#include "qgroup.h"
b8bea09a 51#include "raid56.h"
1abe9b8a 52#define CREATE_TRACE_POINTS
53#include <trace/events/btrfs.h>
54
b87221de 55static const struct super_operations btrfs_super_ops;
72fa39f5
MT
56
57/*
58 * Types for mounting the default subvolume and a subvolume explicitly
59 * requested by subvol=/path. That way the callchain is straightforward and we
60 * don't have to play tricks with the mount options and recursive calls to
61 * btrfs_mount.
312c89fb
MT
62 *
63 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
72fa39f5 64 */
830c4adb 65static struct file_system_type btrfs_fs_type;
72fa39f5 66static struct file_system_type btrfs_root_fs_type;
75dfe396 67
0723a047
HH
68static int btrfs_remount(struct super_block *sb, int *flags, char *data);
69
c067da87
STD
70#ifdef CONFIG_PRINTK
71
72#define STATE_STRING_PREFACE ": state "
73#define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
74
75/*
143823cf 76 * Characters to print to indicate error conditions or uncommon filesystem state.
c067da87
STD
77 * RO is not an error.
78 */
79static const char fs_state_chars[] = {
80 [BTRFS_FS_STATE_ERROR] = 'E',
81 [BTRFS_FS_STATE_REMOUNTING] = 'M',
82 [BTRFS_FS_STATE_RO] = 0,
83 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A',
84 [BTRFS_FS_STATE_DEV_REPLACING] = 'R',
85 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0,
86 [BTRFS_FS_STATE_NO_CSUMS] = 'C',
87 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L',
88};
89
90static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
91{
92 unsigned int bit;
93 bool states_printed = false;
94 unsigned long fs_state = READ_ONCE(info->fs_state);
95 char *curr = buf;
96
97 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
98 curr += sizeof(STATE_STRING_PREFACE) - 1;
99
100 for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
101 WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
102 if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
103 *curr++ = fs_state_chars[bit];
104 states_printed = true;
105 }
106 }
107
108 /* If no states were printed, reset the buffer */
109 if (!states_printed)
110 curr = buf;
111
112 *curr++ = 0;
113}
114#endif
115
59131393
JB
116/*
117 * Generally the error codes correspond to their respective errors, but there
118 * are a few special cases.
119 *
120 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
121 * instance will return EUCLEAN if any of the blocks are corrupted in
122 * a way that is problematic. We want to reserve EUCLEAN for these
123 * sort of corruptions.
124 *
125 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
126 * need to use EROFS for this case. We will have no idea of the
127 * original failure, that will have been reported at the time we tripped
128 * over the error. Each subsequent error that doesn't have any context
129 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
130 */
4143cb8b 131const char * __attribute_const__ btrfs_decode_error(int errno)
acce952b 132{
08748810 133 char *errstr = "unknown";
acce952b 134
135 switch (errno) {
d54f8144
DS
136 case -ENOENT: /* -2 */
137 errstr = "No such entry";
138 break;
139 case -EIO: /* -5 */
acce952b 140 errstr = "IO failure";
141 break;
d54f8144 142 case -ENOMEM: /* -12*/
acce952b 143 errstr = "Out of memory";
144 break;
d54f8144 145 case -EEXIST: /* -17 */
8c342930
JM
146 errstr = "Object already exists";
147 break;
d54f8144 148 case -ENOSPC: /* -28 */
94ef7280
DS
149 errstr = "No space left";
150 break;
d54f8144
DS
151 case -EROFS: /* -30 */
152 errstr = "Readonly filesystem";
94ef7280 153 break;
fb8521ca
DS
154 case -EOPNOTSUPP: /* -95 */
155 errstr = "Operation not supported";
156 break;
157 case -EUCLEAN: /* -117 */
158 errstr = "Filesystem corrupted";
159 break;
160 case -EDQUOT: /* -122 */
161 errstr = "Quota exceeded";
162 break;
acce952b 163 }
164
165 return errstr;
166}
167
acce952b 168/*
34d97007 169 * __btrfs_handle_fs_error decodes expected errors from the caller and
52042d8e 170 * invokes the appropriate error response.
acce952b 171 */
c0d19e2b 172__cold
34d97007 173void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
4da35113 174 unsigned int line, int errno, const char *fmt, ...)
acce952b 175{
176 struct super_block *sb = fs_info->sb;
57d816a1 177#ifdef CONFIG_PRINTK
c067da87 178 char statestr[STATE_STRING_BUF_LEN];
acce952b 179 const char *errstr;
57d816a1 180#endif
acce952b 181
182 /*
183 * Special case: if the error is EROFS, and we're already
1751e8a6 184 * under SB_RDONLY, then it is safe here.
acce952b 185 */
bc98a42c 186 if (errno == -EROFS && sb_rdonly(sb))
4da35113
JM
187 return;
188
57d816a1 189#ifdef CONFIG_PRINTK
08748810 190 errstr = btrfs_decode_error(errno);
c067da87 191 btrfs_state_to_string(fs_info, statestr);
4da35113 192 if (fmt) {
37252a66
ES
193 struct va_format vaf;
194 va_list args;
195
196 va_start(args, fmt);
197 vaf.fmt = fmt;
198 vaf.va = &args;
4da35113 199
c067da87
STD
200 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
201 sb->s_id, statestr, function, line, errno, errstr, &vaf);
37252a66 202 va_end(args);
4da35113 203 } else {
c067da87
STD
204 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
205 sb->s_id, statestr, function, line, errno, errstr);
4da35113 206 }
57d816a1 207#endif
acce952b 208
0713d90c
AJ
209 /*
210 * Today we only save the error info to memory. Long term we'll
211 * also send it down to the disk
212 */
213 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
214
4da35113 215 /* Don't go through full error handling during mount */
922ea899
AJ
216 if (!(sb->s_flags & SB_BORN))
217 return;
218
219 if (sb_rdonly(sb))
220 return;
221
b0643e59
DZ
222 btrfs_discard_stop(fs_info);
223
922ea899 224 /* btrfs handle error by forcing the filesystem readonly */
a0a1db70 225 btrfs_set_sb_rdonly(sb);
922ea899
AJ
226 btrfs_info(fs_info, "forced readonly");
227 /*
228 * Note that a running device replace operation is not canceled here
229 * although there is no way to update the progress. It would add the
230 * risk of a deadlock, therefore the canceling is omitted. The only
231 * penalty is that some I/O remains active until the procedure
52042d8e 232 * completes. The next time when the filesystem is mounted writable
922ea899
AJ
233 * again, the device replace operation continues.
234 */
4da35113 235}
acce952b 236
57d816a1 237#ifdef CONFIG_PRINTK
533574c6 238static const char * const logtypes[] = {
4da35113
JM
239 "emergency",
240 "alert",
241 "critical",
242 "error",
243 "warning",
244 "notice",
245 "info",
246 "debug",
247};
248
35f4e5e6
NB
249
250/*
251 * Use one ratelimit state per log level so that a flood of less important
252 * messages doesn't cause more important ones to be dropped.
253 */
254static struct ratelimit_state printk_limits[] = {
255 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
256 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
257 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
258 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
259 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
260 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
261 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
262 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
263};
264
b0a66a31 265void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
4da35113 266{
40f7828b 267 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
4da35113
JM
268 struct va_format vaf;
269 va_list args;
533574c6 270 int kern_level;
40f7828b
PM
271 const char *type = logtypes[4];
272 struct ratelimit_state *ratelimit = &printk_limits[4];
4da35113
JM
273
274 va_start(args, fmt);
275
262c5e86 276 while ((kern_level = printk_get_level(fmt)) != 0) {
533574c6 277 size_t size = printk_skip_level(fmt) - fmt;
262c5e86
PM
278
279 if (kern_level >= '0' && kern_level <= '7') {
280 memcpy(lvl, fmt, size);
281 lvl[size] = '\0';
282 type = logtypes[kern_level - '0'];
283 ratelimit = &printk_limits[kern_level - '0'];
284 }
533574c6 285 fmt += size;
262c5e86
PM
286 }
287
4da35113
JM
288 vaf.fmt = fmt;
289 vaf.va = &args;
533574c6 290
a0f6d924 291 if (__ratelimit(ratelimit)) {
c067da87
STD
292 if (fs_info) {
293 char statestr[STATE_STRING_BUF_LEN];
294
295 btrfs_state_to_string(fs_info, statestr);
b0a66a31 296 _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
c067da87
STD
297 fs_info->sb->s_id, statestr, &vaf);
298 } else {
b0a66a31 299 _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
c067da87 300 }
a0f6d924 301 }
533574c6
JP
302
303 va_end(args);
304}
533574c6 305#endif
acce952b 306
e9306ad4
QW
307#if BITS_PER_LONG == 32
308void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
309{
310 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
311 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
312 btrfs_warn(fs_info,
313"due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
314 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
315 btrfs_warn(fs_info,
316 "please consider upgrading to 64bit kernel/hardware");
317 }
318}
319
320void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
321{
322 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
323 btrfs_err(fs_info, "reached 32bit limit for logical addresses");
324 btrfs_err(fs_info,
325"due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
326 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
327 btrfs_err(fs_info,
328 "please consider upgrading to 64bit kernel/hardware");
329 }
330}
331#endif
332
49b25e05
JM
333/*
334 * We only mark the transaction aborted and then set the file system read-only.
335 * This will prevent new transactions from starting or trying to join this
336 * one.
337 *
338 * This means that error recovery at the call site is limited to freeing
339 * any local memory allocations and passing the error code up without
340 * further cleanup. The transaction should complete as it normally would
341 * in the call path but will return -EIO.
342 *
343 * We'll complete the cleanup in btrfs_end_transaction and
344 * btrfs_commit_transaction.
345 */
c0d19e2b 346__cold
49b25e05 347void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
66642832 348 const char *function,
49b25e05
JM
349 unsigned int line, int errno)
350{
66642832
JM
351 struct btrfs_fs_info *fs_info = trans->fs_info;
352
bf31f87f 353 WRITE_ONCE(trans->aborted, errno);
20c7bcec 354 WRITE_ONCE(trans->transaction->aborted, errno);
501407aa 355 /* Wake up anybody who may be waiting on this transaction */
66642832
JM
356 wake_up(&fs_info->transaction_wait);
357 wake_up(&fs_info->transaction_blocked_wait);
358 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
49b25e05 359}
8c342930
JM
360/*
361 * __btrfs_panic decodes unexpected, fatal errors from the caller,
362 * issues an alert, and either panics or BUGs, depending on mount options.
363 */
c0d19e2b 364__cold
8c342930
JM
365void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
366 unsigned int line, int errno, const char *fmt, ...)
367{
8c342930
JM
368 char *s_id = "<unknown>";
369 const char *errstr;
370 struct va_format vaf = { .fmt = fmt };
371 va_list args;
acce952b 372
8c342930
JM
373 if (fs_info)
374 s_id = fs_info->sb->s_id;
acce952b 375
8c342930
JM
376 va_start(args, fmt);
377 vaf.va = &args;
378
08748810 379 errstr = btrfs_decode_error(errno);
d8953d69 380 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
08748810
DS
381 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
382 s_id, function, line, &vaf, errno, errstr);
8c342930 383
efe120a0
FH
384 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
385 function, line, &vaf, errno, errstr);
8c342930
JM
386 va_end(args);
387 /* Caller calls BUG() */
acce952b 388}
389
d397712b 390static void btrfs_put_super(struct super_block *sb)
b18c6685 391{
6bccf3ab 392 close_ctree(btrfs_sb(sb));
75dfe396
CM
393}
394
95e05289 395enum {
416a7202
DS
396 Opt_acl, Opt_noacl,
397 Opt_clear_cache,
398 Opt_commit_interval,
399 Opt_compress,
400 Opt_compress_force,
401 Opt_compress_force_type,
402 Opt_compress_type,
403 Opt_degraded,
404 Opt_device,
405 Opt_fatal_errors,
406 Opt_flushoncommit, Opt_noflushoncommit,
416a7202
DS
407 Opt_max_inline,
408 Opt_barrier, Opt_nobarrier,
409 Opt_datacow, Opt_nodatacow,
410 Opt_datasum, Opt_nodatasum,
411 Opt_defrag, Opt_nodefrag,
412 Opt_discard, Opt_nodiscard,
b0643e59 413 Opt_discard_mode,
416a7202
DS
414 Opt_norecovery,
415 Opt_ratio,
416 Opt_rescan_uuid_tree,
417 Opt_skip_balance,
418 Opt_space_cache, Opt_no_space_cache,
419 Opt_space_cache_version,
420 Opt_ssd, Opt_nossd,
421 Opt_ssd_spread, Opt_nossd_spread,
422 Opt_subvol,
37becec9 423 Opt_subvol_empty,
416a7202
DS
424 Opt_subvolid,
425 Opt_thread_pool,
426 Opt_treelog, Opt_notreelog,
416a7202
DS
427 Opt_user_subvol_rm_allowed,
428
74ef0018
QW
429 /* Rescue options */
430 Opt_rescue,
431 Opt_usebackuproot,
432 Opt_nologreplay,
42437a63 433 Opt_ignorebadroots,
882dbe0c 434 Opt_ignoredatacsums,
9037d3cb 435 Opt_rescue_all,
74ef0018 436
416a7202 437 /* Deprecated options */
416a7202 438 Opt_recovery,
5297199a 439 Opt_inode_cache, Opt_noinode_cache,
416a7202
DS
440
441 /* Debugging options */
442 Opt_check_integrity,
70f6d82e 443 Opt_check_integrity_including_extent_data,
416a7202
DS
444 Opt_check_integrity_print_mask,
445 Opt_enospc_debug, Opt_noenospc_debug,
d0bd4560
JB
446#ifdef CONFIG_BTRFS_DEBUG
447 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
fb592373
JB
448#endif
449#ifdef CONFIG_BTRFS_FS_REF_VERIFY
450 Opt_ref_verify,
d0bd4560 451#endif
9555c6c1 452 Opt_err,
95e05289
CM
453};
454
4d4ab6d6 455static const match_table_t tokens = {
416a7202
DS
456 {Opt_acl, "acl"},
457 {Opt_noacl, "noacl"},
458 {Opt_clear_cache, "clear_cache"},
459 {Opt_commit_interval, "commit=%u"},
c8b97818 460 {Opt_compress, "compress"},
261507a0 461 {Opt_compress_type, "compress=%s"},
a555f810 462 {Opt_compress_force, "compress-force"},
261507a0 463 {Opt_compress_force_type, "compress-force=%s"},
416a7202
DS
464 {Opt_degraded, "degraded"},
465 {Opt_device, "device=%s"},
466 {Opt_fatal_errors, "fatal_errors=%s"},
dccae999 467 {Opt_flushoncommit, "flushoncommit"},
2c9ee856 468 {Opt_noflushoncommit, "noflushoncommit"},
416a7202
DS
469 {Opt_inode_cache, "inode_cache"},
470 {Opt_noinode_cache, "noinode_cache"},
471 {Opt_max_inline, "max_inline=%s"},
472 {Opt_barrier, "barrier"},
473 {Opt_nobarrier, "nobarrier"},
474 {Opt_datacow, "datacow"},
475 {Opt_nodatacow, "nodatacow"},
476 {Opt_datasum, "datasum"},
477 {Opt_nodatasum, "nodatasum"},
478 {Opt_defrag, "autodefrag"},
479 {Opt_nodefrag, "noautodefrag"},
e244a0ae 480 {Opt_discard, "discard"},
b0643e59 481 {Opt_discard_mode, "discard=%s"},
e07a2ade 482 {Opt_nodiscard, "nodiscard"},
416a7202
DS
483 {Opt_norecovery, "norecovery"},
484 {Opt_ratio, "metadata_ratio=%u"},
485 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
486 {Opt_skip_balance, "skip_balance"},
0af3d00b 487 {Opt_space_cache, "space_cache"},
8965593e 488 {Opt_no_space_cache, "nospace_cache"},
416a7202
DS
489 {Opt_space_cache_version, "space_cache=%s"},
490 {Opt_ssd, "ssd"},
491 {Opt_nossd, "nossd"},
492 {Opt_ssd_spread, "ssd_spread"},
493 {Opt_nossd_spread, "nossd_spread"},
494 {Opt_subvol, "subvol=%s"},
37becec9 495 {Opt_subvol_empty, "subvol="},
416a7202
DS
496 {Opt_subvolid, "subvolid=%s"},
497 {Opt_thread_pool, "thread_pool=%u"},
498 {Opt_treelog, "treelog"},
499 {Opt_notreelog, "notreelog"},
416a7202
DS
500 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
501
74ef0018
QW
502 /* Rescue options */
503 {Opt_rescue, "rescue=%s"},
504 /* Deprecated, with alias rescue=nologreplay */
505 {Opt_nologreplay, "nologreplay"},
506 /* Deprecated, with alias rescue=usebackuproot */
507 {Opt_usebackuproot, "usebackuproot"},
508
416a7202 509 /* Deprecated options */
416a7202 510 {Opt_recovery, "recovery"},
416a7202
DS
511
512 /* Debugging options */
21adbd5c
SB
513 {Opt_check_integrity, "check_int"},
514 {Opt_check_integrity_including_extent_data, "check_int_data"},
02453bde 515 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
416a7202
DS
516 {Opt_enospc_debug, "enospc_debug"},
517 {Opt_noenospc_debug, "noenospc_debug"},
d0bd4560
JB
518#ifdef CONFIG_BTRFS_DEBUG
519 {Opt_fragment_data, "fragment=data"},
520 {Opt_fragment_metadata, "fragment=metadata"},
521 {Opt_fragment_all, "fragment=all"},
fb592373
JB
522#endif
523#ifdef CONFIG_BTRFS_FS_REF_VERIFY
524 {Opt_ref_verify, "ref_verify"},
d0bd4560 525#endif
33268eaf 526 {Opt_err, NULL},
95e05289
CM
527};
528
74ef0018
QW
529static const match_table_t rescue_tokens = {
530 {Opt_usebackuproot, "usebackuproot"},
531 {Opt_nologreplay, "nologreplay"},
42437a63
JB
532 {Opt_ignorebadroots, "ignorebadroots"},
533 {Opt_ignorebadroots, "ibadroots"},
882dbe0c
JB
534 {Opt_ignoredatacsums, "ignoredatacsums"},
535 {Opt_ignoredatacsums, "idatacsums"},
9037d3cb 536 {Opt_rescue_all, "all"},
74ef0018
QW
537 {Opt_err, NULL},
538};
539
d70bf748
JB
540static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
541 const char *opt_name)
542{
543 if (fs_info->mount_opt & opt) {
544 btrfs_err(fs_info, "%s must be used with ro mount option",
545 opt_name);
546 return true;
547 }
548 return false;
549}
550
74ef0018
QW
551static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
552{
553 char *opts;
554 char *orig;
555 char *p;
556 substring_t args[MAX_OPT_ARGS];
557 int ret = 0;
558
559 opts = kstrdup(options, GFP_KERNEL);
560 if (!opts)
561 return -ENOMEM;
562 orig = opts;
563
564 while ((p = strsep(&opts, ":")) != NULL) {
565 int token;
566
567 if (!*p)
568 continue;
569 token = match_token(p, rescue_tokens, args);
570 switch (token){
571 case Opt_usebackuproot:
572 btrfs_info(info,
573 "trying to use backup root at mount time");
574 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
575 break;
576 case Opt_nologreplay:
577 btrfs_set_and_info(info, NOLOGREPLAY,
578 "disabling log replay at mount time");
579 break;
42437a63
JB
580 case Opt_ignorebadroots:
581 btrfs_set_and_info(info, IGNOREBADROOTS,
582 "ignoring bad roots");
583 break;
882dbe0c
JB
584 case Opt_ignoredatacsums:
585 btrfs_set_and_info(info, IGNOREDATACSUMS,
586 "ignoring data csums");
587 break;
9037d3cb
JB
588 case Opt_rescue_all:
589 btrfs_info(info, "enabling all of the rescue options");
590 btrfs_set_and_info(info, IGNOREDATACSUMS,
591 "ignoring data csums");
592 btrfs_set_and_info(info, IGNOREBADROOTS,
593 "ignoring bad roots");
594 btrfs_set_and_info(info, NOLOGREPLAY,
595 "disabling log replay at mount time");
596 break;
74ef0018
QW
597 case Opt_err:
598 btrfs_info(info, "unrecognized rescue option '%s'", p);
599 ret = -EINVAL;
600 goto out;
601 default:
602 break;
603 }
604
605 }
606out:
607 kfree(orig);
608 return ret;
609}
610
edf24abe
CH
611/*
612 * Regular mount options parser. Everything that is needed only when
613 * reading in a new superblock is parsed here.
49b25e05 614 * XXX JDM: This needs to be cleaned up for remount.
edf24abe 615 */
2ff7e61e 616int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
96da0919 617 unsigned long new_flags)
95e05289 618{
95e05289 619 substring_t args[MAX_OPT_ARGS];
e215772c 620 char *p, *num;
4543df7e 621 int intarg;
a7a3f7ca 622 int ret = 0;
261507a0
LZ
623 char *compress_type;
624 bool compress_force = false;
b7c47bbb 625 enum btrfs_compression_type saved_compress_type;
27942c99 626 int saved_compress_level;
b7c47bbb
TI
627 bool saved_compress_force;
628 int no_compress = 0;
b6cda9bc 629
0b246afa 630 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
70f6d82e 631 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
94846229 632 else if (btrfs_free_space_cache_v1_active(info)) {
5d1ab66c
NA
633 if (btrfs_is_zoned(info)) {
634 btrfs_info(info,
635 "zoned: clearing existing space cache");
636 btrfs_set_super_cache_generation(info->super_copy, 0);
637 } else {
638 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
639 }
640 }
73bc1876 641
96da0919
QW
642 /*
643 * Even the options are empty, we still need to do extra check
644 * against new flags
645 */
95e05289 646 if (!options)
96da0919 647 goto check;
95e05289 648
edf24abe 649 while ((p = strsep(&options, ",")) != NULL) {
95e05289
CM
650 int token;
651 if (!*p)
652 continue;
653
654 token = match_token(p, tokens, args);
655 switch (token) {
dfe25020 656 case Opt_degraded:
0b246afa 657 btrfs_info(info, "allowing degraded mounts");
edf24abe 658 btrfs_set_opt(info->mount_opt, DEGRADED);
dfe25020 659 break;
95e05289 660 case Opt_subvol:
37becec9 661 case Opt_subvol_empty:
73f73415 662 case Opt_subvolid:
43e570b0 663 case Opt_device:
edf24abe 664 /*
fa59f27c
AJ
665 * These are parsed by btrfs_parse_subvol_options or
666 * btrfs_parse_device_options and can be ignored here.
edf24abe 667 */
b6cda9bc
CM
668 break;
669 case Opt_nodatasum:
3cdde224 670 btrfs_set_and_info(info, NODATASUM,
07802534 671 "setting nodatasum");
be20aa9d 672 break;
d399167d 673 case Opt_datasum:
3cdde224
JM
674 if (btrfs_test_opt(info, NODATASUM)) {
675 if (btrfs_test_opt(info, NODATACOW))
0b246afa 676 btrfs_info(info,
5d163e0e 677 "setting datasum, datacow enabled");
07802534 678 else
0b246afa 679 btrfs_info(info, "setting datasum");
07802534 680 }
d399167d
QW
681 btrfs_clear_opt(info->mount_opt, NODATACOW);
682 btrfs_clear_opt(info->mount_opt, NODATASUM);
683 break;
be20aa9d 684 case Opt_nodatacow:
3cdde224
JM
685 if (!btrfs_test_opt(info, NODATACOW)) {
686 if (!btrfs_test_opt(info, COMPRESS) ||
687 !btrfs_test_opt(info, FORCE_COMPRESS)) {
0b246afa 688 btrfs_info(info,
07802534
QW
689 "setting nodatacow, compression disabled");
690 } else {
0b246afa 691 btrfs_info(info, "setting nodatacow");
07802534 692 }
bedb2cca 693 }
bedb2cca
AP
694 btrfs_clear_opt(info->mount_opt, COMPRESS);
695 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
edf24abe
CH
696 btrfs_set_opt(info->mount_opt, NODATACOW);
697 btrfs_set_opt(info->mount_opt, NODATASUM);
95e05289 698 break;
a258af7a 699 case Opt_datacow:
3cdde224 700 btrfs_clear_and_info(info, NODATACOW,
07802534 701 "setting datacow");
a258af7a 702 break;
a555f810 703 case Opt_compress_force:
261507a0
LZ
704 case Opt_compress_force_type:
705 compress_force = true;
c730ae0c 706 fallthrough;
261507a0
LZ
707 case Opt_compress:
708 case Opt_compress_type:
3cdde224
JM
709 saved_compress_type = btrfs_test_opt(info,
710 COMPRESS) ?
b7c47bbb
TI
711 info->compress_type : BTRFS_COMPRESS_NONE;
712 saved_compress_force =
3cdde224 713 btrfs_test_opt(info, FORCE_COMPRESS);
27942c99 714 saved_compress_level = info->compress_level;
261507a0
LZ
715 if (token == Opt_compress ||
716 token == Opt_compress_force ||
a7164fa4 717 strncmp(args[0].from, "zlib", 4) == 0) {
261507a0 718 compress_type = "zlib";
eae8d825 719
261507a0 720 info->compress_type = BTRFS_COMPRESS_ZLIB;
eae8d825
QW
721 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
722 /*
723 * args[0] contains uninitialized data since
724 * for these tokens we don't expect any
725 * parameter.
726 */
727 if (token != Opt_compress &&
728 token != Opt_compress_force)
729 info->compress_level =
d0ab62ce
DZ
730 btrfs_compress_str2level(
731 BTRFS_COMPRESS_ZLIB,
732 args[0].from + 4);
063849ea 733 btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca
AP
734 btrfs_clear_opt(info->mount_opt, NODATACOW);
735 btrfs_clear_opt(info->mount_opt, NODATASUM);
b7c47bbb 736 no_compress = 0;
a7164fa4 737 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
a6fa6fae
LZ
738 compress_type = "lzo";
739 info->compress_type = BTRFS_COMPRESS_LZO;
282dd7d7 740 info->compress_level = 0;
063849ea 741 btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca
AP
742 btrfs_clear_opt(info->mount_opt, NODATACOW);
743 btrfs_clear_opt(info->mount_opt, NODATASUM);
2b0ce2c2 744 btrfs_set_fs_incompat(info, COMPRESS_LZO);
b7c47bbb 745 no_compress = 0;
3f93aef5 746 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
5c1aab1d
NT
747 compress_type = "zstd";
748 info->compress_type = BTRFS_COMPRESS_ZSTD;
3f93aef5
DZ
749 info->compress_level =
750 btrfs_compress_str2level(
751 BTRFS_COMPRESS_ZSTD,
752 args[0].from + 4);
5c1aab1d
NT
753 btrfs_set_opt(info->mount_opt, COMPRESS);
754 btrfs_clear_opt(info->mount_opt, NODATACOW);
755 btrfs_clear_opt(info->mount_opt, NODATASUM);
756 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
757 no_compress = 0;
063849ea
AH
758 } else if (strncmp(args[0].from, "no", 2) == 0) {
759 compress_type = "no";
27942c99
DS
760 info->compress_level = 0;
761 info->compress_type = 0;
063849ea
AH
762 btrfs_clear_opt(info->mount_opt, COMPRESS);
763 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
764 compress_force = false;
b7c47bbb 765 no_compress++;
261507a0 766 } else {
e3a4167c
DS
767 btrfs_err(info, "unrecognized compression value %s",
768 args[0].from);
261507a0
LZ
769 ret = -EINVAL;
770 goto out;
771 }
772
261507a0 773 if (compress_force) {
b7c47bbb 774 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
143f3636 775 } else {
4027e0f4
WS
776 /*
777 * If we remount from compress-force=xxx to
778 * compress=xxx, we need clear FORCE_COMPRESS
779 * flag, otherwise, there is no way for users
780 * to disable forcible compression separately.
781 */
782 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
a7e252af 783 }
27942c99
DS
784 if (no_compress == 1) {
785 btrfs_info(info, "use no compression");
786 } else if ((info->compress_type != saved_compress_type) ||
787 (compress_force != saved_compress_force) ||
788 (info->compress_level != saved_compress_level)) {
f51d2b59 789 btrfs_info(info, "%s %s compression, level %d",
b7c47bbb 790 (compress_force) ? "force" : "use",
f51d2b59 791 compress_type, info->compress_level);
b7c47bbb
TI
792 }
793 compress_force = false;
a555f810 794 break;
e18e4809 795 case Opt_ssd:
3cdde224 796 btrfs_set_and_info(info, SSD,
583b7231 797 "enabling ssd optimizations");
951e7966 798 btrfs_clear_opt(info->mount_opt, NOSSD);
e18e4809 799 break;
451d7585 800 case Opt_ssd_spread:
583b7231
HK
801 btrfs_set_and_info(info, SSD,
802 "enabling ssd optimizations");
3cdde224 803 btrfs_set_and_info(info, SSD_SPREAD,
583b7231 804 "using spread ssd allocation scheme");
951e7966 805 btrfs_clear_opt(info->mount_opt, NOSSD);
451d7585 806 break;
3b30c22f 807 case Opt_nossd:
583b7231
HK
808 btrfs_set_opt(info->mount_opt, NOSSD);
809 btrfs_clear_and_info(info, SSD,
810 "not using ssd optimizations");
c730ae0c 811 fallthrough;
62b8e077 812 case Opt_nossd_spread:
583b7231
HK
813 btrfs_clear_and_info(info, SSD_SPREAD,
814 "not using spread ssd allocation scheme");
3b30c22f 815 break;
842bef58 816 case Opt_barrier:
3cdde224 817 btrfs_clear_and_info(info, NOBARRIER,
07802534 818 "turning on barriers");
842bef58 819 break;
21ad10cf 820 case Opt_nobarrier:
3cdde224 821 btrfs_set_and_info(info, NOBARRIER,
07802534 822 "turning off barriers");
21ad10cf 823 break;
4543df7e 824 case Opt_thread_pool:
2c334e87
WS
825 ret = match_int(&args[0], &intarg);
826 if (ret) {
e3a4167c
DS
827 btrfs_err(info, "unrecognized thread_pool value %s",
828 args[0].from);
2c334e87 829 goto out;
f7b885be 830 } else if (intarg == 0) {
e3a4167c 831 btrfs_err(info, "invalid value 0 for thread_pool");
2c334e87
WS
832 ret = -EINVAL;
833 goto out;
834 }
f7b885be 835 info->thread_pool_size = intarg;
4543df7e 836 break;
6f568d35 837 case Opt_max_inline:
edf24abe
CH
838 num = match_strdup(&args[0]);
839 if (num) {
91748467 840 info->max_inline = memparse(num, NULL);
edf24abe
CH
841 kfree(num);
842
15ada040 843 if (info->max_inline) {
feb5f965 844 info->max_inline = min_t(u64,
15ada040 845 info->max_inline,
0b246afa 846 info->sectorsize);
15ada040 847 }
0b246afa
JM
848 btrfs_info(info, "max_inline at %llu",
849 info->max_inline);
2c334e87
WS
850 } else {
851 ret = -ENOMEM;
852 goto out;
6f568d35
CM
853 }
854 break;
bd0330ad 855 case Opt_acl:
45ff35d6 856#ifdef CONFIG_BTRFS_FS_POSIX_ACL
1751e8a6 857 info->sb->s_flags |= SB_POSIXACL;
bd0330ad 858 break;
45ff35d6 859#else
0b246afa 860 btrfs_err(info, "support for ACL not compiled in!");
45ff35d6
GZ
861 ret = -EINVAL;
862 goto out;
863#endif
33268eaf 864 case Opt_noacl:
1751e8a6 865 info->sb->s_flags &= ~SB_POSIXACL;
33268eaf 866 break;
3a5e1404 867 case Opt_notreelog:
3cdde224 868 btrfs_set_and_info(info, NOTREELOG,
07802534 869 "disabling tree log");
a88998f2
QW
870 break;
871 case Opt_treelog:
3cdde224 872 btrfs_clear_and_info(info, NOTREELOG,
07802534 873 "enabling tree log");
3a5e1404 874 break;
fed8f166 875 case Opt_norecovery:
96da0919 876 case Opt_nologreplay:
74ef0018
QW
877 btrfs_warn(info,
878 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
3cdde224 879 btrfs_set_and_info(info, NOLOGREPLAY,
96da0919
QW
880 "disabling log replay at mount time");
881 break;
dccae999 882 case Opt_flushoncommit:
3cdde224 883 btrfs_set_and_info(info, FLUSHONCOMMIT,
07802534 884 "turning on flush-on-commit");
dccae999 885 break;
2c9ee856 886 case Opt_noflushoncommit:
3cdde224 887 btrfs_clear_and_info(info, FLUSHONCOMMIT,
07802534 888 "turning off flush-on-commit");
2c9ee856 889 break;
97e728d4 890 case Opt_ratio:
2c334e87 891 ret = match_int(&args[0], &intarg);
e3a4167c
DS
892 if (ret) {
893 btrfs_err(info, "unrecognized metadata_ratio value %s",
894 args[0].from);
2c334e87 895 goto out;
e3a4167c 896 }
764cb8b4
AJ
897 info->metadata_ratio = intarg;
898 btrfs_info(info, "metadata ratio %u",
899 info->metadata_ratio);
97e728d4 900 break;
e244a0ae 901 case Opt_discard:
b0643e59
DZ
902 case Opt_discard_mode:
903 if (token == Opt_discard ||
904 strcmp(args[0].from, "sync") == 0) {
905 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
906 btrfs_set_and_info(info, DISCARD_SYNC,
907 "turning on sync discard");
908 } else if (strcmp(args[0].from, "async") == 0) {
909 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
910 btrfs_set_and_info(info, DISCARD_ASYNC,
911 "turning on async discard");
912 } else {
e3a4167c
DS
913 btrfs_err(info, "unrecognized discard mode value %s",
914 args[0].from);
b0643e59
DZ
915 ret = -EINVAL;
916 goto out;
917 }
e244a0ae 918 break;
e07a2ade 919 case Opt_nodiscard:
46b27f50 920 btrfs_clear_and_info(info, DISCARD_SYNC,
07802534 921 "turning off discard");
b0643e59
DZ
922 btrfs_clear_and_info(info, DISCARD_ASYNC,
923 "turning off async discard");
e07a2ade 924 break;
0af3d00b 925 case Opt_space_cache:
70f6d82e 926 case Opt_space_cache_version:
63cd070d
JB
927 /*
928 * We already set FREE_SPACE_TREE above because we have
929 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
930 * to allow v1 to be set for extent tree v2, simply
931 * ignore this setting if we're extent tree v2.
932 */
933 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
934 break;
70f6d82e
OS
935 if (token == Opt_space_cache ||
936 strcmp(args[0].from, "v1") == 0) {
0b246afa 937 btrfs_clear_opt(info->mount_opt,
70f6d82e 938 FREE_SPACE_TREE);
3cdde224 939 btrfs_set_and_info(info, SPACE_CACHE,
0b246afa 940 "enabling disk space caching");
70f6d82e 941 } else if (strcmp(args[0].from, "v2") == 0) {
0b246afa 942 btrfs_clear_opt(info->mount_opt,
70f6d82e 943 SPACE_CACHE);
0b246afa 944 btrfs_set_and_info(info, FREE_SPACE_TREE,
70f6d82e
OS
945 "enabling free space tree");
946 } else {
e3a4167c
DS
947 btrfs_err(info, "unrecognized space_cache value %s",
948 args[0].from);
70f6d82e
OS
949 ret = -EINVAL;
950 goto out;
951 }
0de90876 952 break;
f420ee1e
SB
953 case Opt_rescan_uuid_tree:
954 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
955 break;
73bc1876 956 case Opt_no_space_cache:
63cd070d
JB
957 /*
958 * We cannot operate without the free space tree with
959 * extent tree v2, ignore this option.
960 */
961 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
962 break;
3cdde224 963 if (btrfs_test_opt(info, SPACE_CACHE)) {
0b246afa
JM
964 btrfs_clear_and_info(info, SPACE_CACHE,
965 "disabling disk space caching");
70f6d82e 966 }
3cdde224 967 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
0b246afa
JM
968 btrfs_clear_and_info(info, FREE_SPACE_TREE,
969 "disabling free space tree");
70f6d82e 970 }
73bc1876 971 break;
4b9465cb 972 case Opt_inode_cache:
3818aea2 973 case Opt_noinode_cache:
5297199a
NB
974 btrfs_warn(info,
975 "the 'inode_cache' option is deprecated and has no effect since 5.11");
4b9465cb 976 break;
88c2ba3b 977 case Opt_clear_cache:
63cd070d
JB
978 /*
979 * We cannot clear the free space tree with extent tree
980 * v2, ignore this option.
981 */
982 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
983 break;
3cdde224 984 btrfs_set_and_info(info, CLEAR_CACHE,
07802534 985 "force clearing of disk cache");
0af3d00b 986 break;
4260f7c7
SW
987 case Opt_user_subvol_rm_allowed:
988 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
989 break;
91435650
CM
990 case Opt_enospc_debug:
991 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
992 break;
53036293
QW
993 case Opt_noenospc_debug:
994 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
995 break;
4cb5300b 996 case Opt_defrag:
3cdde224 997 btrfs_set_and_info(info, AUTO_DEFRAG,
07802534 998 "enabling auto defrag");
4cb5300b 999 break;
fc0ca9af 1000 case Opt_nodefrag:
3cdde224 1001 btrfs_clear_and_info(info, AUTO_DEFRAG,
07802534 1002 "disabling auto defrag");
fc0ca9af 1003 break;
af31f5e5 1004 case Opt_recovery:
8dcddfa0 1005 case Opt_usebackuproot:
74ef0018
QW
1006 btrfs_warn(info,
1007 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
1008 token == Opt_recovery ? "recovery" :
1009 "usebackuproot");
0b246afa 1010 btrfs_info(info,
8dcddfa0
QW
1011 "trying to use backup root at mount time");
1012 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
af31f5e5 1013 break;
9555c6c1
ID
1014 case Opt_skip_balance:
1015 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
1016 break;
21adbd5c
SB
1017#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1018 case Opt_check_integrity_including_extent_data:
0b246afa 1019 btrfs_info(info,
efe120a0 1020 "enabling check integrity including extent data");
cbeaae4f 1021 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
21adbd5c
SB
1022 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1023 break;
1024 case Opt_check_integrity:
0b246afa 1025 btrfs_info(info, "enabling check integrity");
21adbd5c
SB
1026 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1027 break;
1028 case Opt_check_integrity_print_mask:
2c334e87 1029 ret = match_int(&args[0], &intarg);
e3a4167c
DS
1030 if (ret) {
1031 btrfs_err(info,
1032 "unrecognized check_integrity_print_mask value %s",
1033 args[0].from);
2c334e87 1034 goto out;
e3a4167c 1035 }
02453bde
AJ
1036 info->check_integrity_print_mask = intarg;
1037 btrfs_info(info, "check_integrity_print_mask 0x%x",
1038 info->check_integrity_print_mask);
21adbd5c
SB
1039 break;
1040#else
1041 case Opt_check_integrity_including_extent_data:
1042 case Opt_check_integrity:
1043 case Opt_check_integrity_print_mask:
0b246afa
JM
1044 btrfs_err(info,
1045 "support for check_integrity* not compiled in!");
21adbd5c
SB
1046 ret = -EINVAL;
1047 goto out;
1048#endif
8c342930 1049 case Opt_fatal_errors:
e3a4167c 1050 if (strcmp(args[0].from, "panic") == 0) {
8c342930
JM
1051 btrfs_set_opt(info->mount_opt,
1052 PANIC_ON_FATAL_ERROR);
e3a4167c 1053 } else if (strcmp(args[0].from, "bug") == 0) {
8c342930
JM
1054 btrfs_clear_opt(info->mount_opt,
1055 PANIC_ON_FATAL_ERROR);
e3a4167c
DS
1056 } else {
1057 btrfs_err(info, "unrecognized fatal_errors value %s",
1058 args[0].from);
8c342930
JM
1059 ret = -EINVAL;
1060 goto out;
1061 }
1062 break;
8b87dc17
DS
1063 case Opt_commit_interval:
1064 intarg = 0;
1065 ret = match_int(&args[0], &intarg);
e3a4167c
DS
1066 if (ret) {
1067 btrfs_err(info, "unrecognized commit_interval value %s",
1068 args[0].from);
1069 ret = -EINVAL;
8b87dc17 1070 goto out;
e3a4167c 1071 }
d3740608 1072 if (intarg == 0) {
0b246afa 1073 btrfs_info(info,
d3740608 1074 "using default commit interval %us",
5d163e0e 1075 BTRFS_DEFAULT_COMMIT_INTERVAL);
d3740608
AJ
1076 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1077 } else if (intarg > 300) {
1078 btrfs_warn(info, "excessive commit interval %d",
1079 intarg);
8b87dc17 1080 }
d3740608 1081 info->commit_interval = intarg;
8b87dc17 1082 break;
74ef0018
QW
1083 case Opt_rescue:
1084 ret = parse_rescue_options(info, args[0].from);
e3a4167c
DS
1085 if (ret < 0) {
1086 btrfs_err(info, "unrecognized rescue value %s",
1087 args[0].from);
74ef0018 1088 goto out;
e3a4167c 1089 }
74ef0018 1090 break;
d0bd4560
JB
1091#ifdef CONFIG_BTRFS_DEBUG
1092 case Opt_fragment_all:
0b246afa 1093 btrfs_info(info, "fragmenting all space");
d0bd4560
JB
1094 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1095 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1096 break;
1097 case Opt_fragment_metadata:
0b246afa 1098 btrfs_info(info, "fragmenting metadata");
d0bd4560
JB
1099 btrfs_set_opt(info->mount_opt,
1100 FRAGMENT_METADATA);
1101 break;
1102 case Opt_fragment_data:
0b246afa 1103 btrfs_info(info, "fragmenting data");
d0bd4560
JB
1104 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1105 break;
fb592373
JB
1106#endif
1107#ifdef CONFIG_BTRFS_FS_REF_VERIFY
1108 case Opt_ref_verify:
1109 btrfs_info(info, "doing ref verification");
1110 btrfs_set_opt(info->mount_opt, REF_VERIFY);
1111 break;
d0bd4560 1112#endif
a7a3f7ca 1113 case Opt_err:
7e8f19e5 1114 btrfs_err(info, "unrecognized mount option '%s'", p);
a7a3f7ca
SW
1115 ret = -EINVAL;
1116 goto out;
95e05289 1117 default:
be20aa9d 1118 break;
95e05289
CM
1119 }
1120 }
96da0919 1121check:
d70bf748
JB
1122 /* We're read-only, don't have to check. */
1123 if (new_flags & SB_RDONLY)
1124 goto out;
1125
42437a63 1126 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
882dbe0c
JB
1127 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1128 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
96da0919 1129 ret = -EINVAL;
a7a3f7ca 1130out:
0b246afa 1131 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
3cdde224
JM
1132 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
1133 !btrfs_test_opt(info, CLEAR_CACHE)) {
0b246afa 1134 btrfs_err(info, "cannot disable free space tree");
70f6d82e
OS
1135 ret = -EINVAL;
1136
1137 }
5d1ab66c
NA
1138 if (!ret)
1139 ret = btrfs_check_mountopts_zoned(info);
3cdde224 1140 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
0b246afa 1141 btrfs_info(info, "disk space caching is enabled");
3cdde224 1142 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
0b246afa 1143 btrfs_info(info, "using free space tree");
a7a3f7ca 1144 return ret;
edf24abe
CH
1145}
1146
1147/*
1148 * Parse mount options that are required early in the mount process.
1149 *
1150 * All other options will be parsed on much later in the mount process and
1151 * only when we need to allocate a new super block.
1152 */
fa59f27c
AJ
1153static int btrfs_parse_device_options(const char *options, fmode_t flags,
1154 void *holder)
edf24abe
CH
1155{
1156 substring_t args[MAX_OPT_ARGS];
83c8c9bd 1157 char *device_name, *opts, *orig, *p;
36350e95 1158 struct btrfs_device *device = NULL;
d7407606
MT
1159 int error = 0;
1160
5139cff5
DS
1161 lockdep_assert_held(&uuid_mutex);
1162
d7407606
MT
1163 if (!options)
1164 return 0;
1165
1166 /*
1167 * strsep changes the string, duplicate it because btrfs_parse_options
1168 * gets called later
1169 */
1170 opts = kstrdup(options, GFP_KERNEL);
1171 if (!opts)
1172 return -ENOMEM;
1173 orig = opts;
1174
1175 while ((p = strsep(&opts, ",")) != NULL) {
1176 int token;
1177
1178 if (!*p)
1179 continue;
1180
1181 token = match_token(p, tokens, args);
1182 if (token == Opt_device) {
1183 device_name = match_strdup(&args[0]);
1184 if (!device_name) {
1185 error = -ENOMEM;
1186 goto out;
1187 }
36350e95
GJ
1188 device = btrfs_scan_one_device(device_name, flags,
1189 holder);
d7407606 1190 kfree(device_name);
36350e95
GJ
1191 if (IS_ERR(device)) {
1192 error = PTR_ERR(device);
d7407606 1193 goto out;
36350e95 1194 }
d7407606
MT
1195 }
1196 }
1197
1198out:
1199 kfree(orig);
1200 return error;
1201}
1202
1203/*
1204 * Parse mount options that are related to subvolume id
1205 *
1206 * The value is later passed to mount_subvol()
1207 */
93b9bcdf
GJ
1208static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
1209 u64 *subvol_objectid)
d7407606
MT
1210{
1211 substring_t args[MAX_OPT_ARGS];
1212 char *opts, *orig, *p;
edf24abe 1213 int error = 0;
ccb0e7d1 1214 u64 subvolid;
edf24abe
CH
1215
1216 if (!options)
830c4adb 1217 return 0;
edf24abe
CH
1218
1219 /*
d7407606 1220 * strsep changes the string, duplicate it because
fa59f27c 1221 * btrfs_parse_device_options gets called later
edf24abe
CH
1222 */
1223 opts = kstrdup(options, GFP_KERNEL);
1224 if (!opts)
1225 return -ENOMEM;
3f3d0bc0 1226 orig = opts;
edf24abe
CH
1227
1228 while ((p = strsep(&opts, ",")) != NULL) {
1229 int token;
1230 if (!*p)
1231 continue;
1232
1233 token = match_token(p, tokens, args);
1234 switch (token) {
1235 case Opt_subvol:
a90e8b6f 1236 kfree(*subvol_name);
edf24abe 1237 *subvol_name = match_strdup(&args[0]);
2c334e87
WS
1238 if (!*subvol_name) {
1239 error = -ENOMEM;
1240 goto out;
1241 }
edf24abe 1242 break;
73f73415 1243 case Opt_subvolid:
ccb0e7d1
AJ
1244 error = match_u64(&args[0], &subvolid);
1245 if (error)
2c334e87 1246 goto out;
ccb0e7d1
AJ
1247
1248 /* we want the original fs_tree */
1249 if (subvolid == 0)
1250 subvolid = BTRFS_FS_TREE_OBJECTID;
1251
1252 *subvol_objectid = subvolid;
73f73415 1253 break;
edf24abe
CH
1254 default:
1255 break;
1256 }
1257 }
1258
830c4adb 1259out:
3f3d0bc0 1260 kfree(orig);
edf24abe 1261 return error;
95e05289
CM
1262}
1263
c0c907a4
MPS
1264char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1265 u64 subvol_objectid)
73f73415 1266{
815745cf 1267 struct btrfs_root *root = fs_info->tree_root;
5168489a 1268 struct btrfs_root *fs_root = NULL;
05dbe683
OS
1269 struct btrfs_root_ref *root_ref;
1270 struct btrfs_inode_ref *inode_ref;
1271 struct btrfs_key key;
1272 struct btrfs_path *path = NULL;
1273 char *name = NULL, *ptr;
1274 u64 dirid;
1275 int len;
1276 int ret;
1277
1278 path = btrfs_alloc_path();
1279 if (!path) {
1280 ret = -ENOMEM;
1281 goto err;
1282 }
05dbe683 1283
3ec83621 1284 name = kmalloc(PATH_MAX, GFP_KERNEL);
05dbe683
OS
1285 if (!name) {
1286 ret = -ENOMEM;
1287 goto err;
1288 }
1289 ptr = name + PATH_MAX - 1;
1290 ptr[0] = '\0';
73f73415
JB
1291
1292 /*
05dbe683
OS
1293 * Walk up the subvolume trees in the tree of tree roots by root
1294 * backrefs until we hit the top-level subvolume.
73f73415 1295 */
05dbe683
OS
1296 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1297 key.objectid = subvol_objectid;
1298 key.type = BTRFS_ROOT_BACKREF_KEY;
1299 key.offset = (u64)-1;
1300
0ff40a91 1301 ret = btrfs_search_backwards(root, &key, path);
05dbe683
OS
1302 if (ret < 0) {
1303 goto err;
1304 } else if (ret > 0) {
0ff40a91
MPS
1305 ret = -ENOENT;
1306 goto err;
05dbe683
OS
1307 }
1308
05dbe683
OS
1309 subvol_objectid = key.offset;
1310
1311 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1312 struct btrfs_root_ref);
1313 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1314 ptr -= len + 1;
1315 if (ptr < name) {
1316 ret = -ENAMETOOLONG;
1317 goto err;
1318 }
1319 read_extent_buffer(path->nodes[0], ptr + 1,
1320 (unsigned long)(root_ref + 1), len);
1321 ptr[0] = '/';
1322 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1323 btrfs_release_path(path);
1324
56e9357a 1325 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
05dbe683
OS
1326 if (IS_ERR(fs_root)) {
1327 ret = PTR_ERR(fs_root);
5168489a
JB
1328 fs_root = NULL;
1329 goto err;
1330 }
05dbe683
OS
1331
1332 /*
1333 * Walk up the filesystem tree by inode refs until we hit the
1334 * root directory.
1335 */
1336 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1337 key.objectid = dirid;
1338 key.type = BTRFS_INODE_REF_KEY;
1339 key.offset = (u64)-1;
1340
0ff40a91 1341 ret = btrfs_search_backwards(fs_root, &key, path);
05dbe683
OS
1342 if (ret < 0) {
1343 goto err;
1344 } else if (ret > 0) {
0ff40a91
MPS
1345 ret = -ENOENT;
1346 goto err;
05dbe683
OS
1347 }
1348
05dbe683
OS
1349 dirid = key.offset;
1350
1351 inode_ref = btrfs_item_ptr(path->nodes[0],
1352 path->slots[0],
1353 struct btrfs_inode_ref);
1354 len = btrfs_inode_ref_name_len(path->nodes[0],
1355 inode_ref);
1356 ptr -= len + 1;
1357 if (ptr < name) {
1358 ret = -ENAMETOOLONG;
1359 goto err;
1360 }
1361 read_extent_buffer(path->nodes[0], ptr + 1,
1362 (unsigned long)(inode_ref + 1), len);
1363 ptr[0] = '/';
1364 btrfs_release_path(path);
1365 }
00246528 1366 btrfs_put_root(fs_root);
5168489a 1367 fs_root = NULL;
73f73415
JB
1368 }
1369
05dbe683
OS
1370 btrfs_free_path(path);
1371 if (ptr == name + PATH_MAX - 1) {
1372 name[0] = '/';
1373 name[1] = '\0';
1374 } else {
1375 memmove(name, ptr, name + PATH_MAX - ptr);
1376 }
1377 return name;
1378
1379err:
00246528 1380 btrfs_put_root(fs_root);
05dbe683
OS
1381 btrfs_free_path(path);
1382 kfree(name);
1383 return ERR_PTR(ret);
1384}
1385
1386static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1387{
1388 struct btrfs_root *root = fs_info->tree_root;
1389 struct btrfs_dir_item *di;
1390 struct btrfs_path *path;
1391 struct btrfs_key location;
1392 u64 dir_id;
1393
73f73415
JB
1394 path = btrfs_alloc_path();
1395 if (!path)
05dbe683 1396 return -ENOMEM;
73f73415
JB
1397
1398 /*
1399 * Find the "default" dir item which points to the root item that we
1400 * will mount by default if we haven't been given a specific subvolume
1401 * to mount.
1402 */
815745cf 1403 dir_id = btrfs_super_root_dir(fs_info->super_copy);
73f73415 1404 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
b0839166
JL
1405 if (IS_ERR(di)) {
1406 btrfs_free_path(path);
05dbe683 1407 return PTR_ERR(di);
b0839166 1408 }
73f73415
JB
1409 if (!di) {
1410 /*
1411 * Ok the default dir item isn't there. This is weird since
1412 * it's always been there, but don't freak out, just try and
05dbe683 1413 * mount the top-level subvolume.
73f73415
JB
1414 */
1415 btrfs_free_path(path);
05dbe683
OS
1416 *objectid = BTRFS_FS_TREE_OBJECTID;
1417 return 0;
73f73415
JB
1418 }
1419
1420 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1421 btrfs_free_path(path);
05dbe683
OS
1422 *objectid = location.objectid;
1423 return 0;
73f73415
JB
1424}
1425
d397712b 1426static int btrfs_fill_super(struct super_block *sb,
8a4b83cc 1427 struct btrfs_fs_devices *fs_devices,
56e033a7 1428 void *data)
75dfe396 1429{
d397712b 1430 struct inode *inode;
815745cf 1431 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
39279cc3 1432 int err;
a429e513 1433
39279cc3
CM
1434 sb->s_maxbytes = MAX_LFS_FILESIZE;
1435 sb->s_magic = BTRFS_SUPER_MAGIC;
1436 sb->s_op = &btrfs_super_ops;
af53d29a 1437 sb->s_d_op = &btrfs_dentry_operations;
be6e8dc0 1438 sb->s_export_op = &btrfs_export_ops;
14605409
BB
1439#ifdef CONFIG_FS_VERITY
1440 sb->s_vop = &btrfs_verityops;
1441#endif
5103e947 1442 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 1443 sb->s_time_gran = 1;
0eda294d 1444#ifdef CONFIG_BTRFS_FS_POSIX_ACL
1751e8a6 1445 sb->s_flags |= SB_POSIXACL;
49cf6f45 1446#endif
357fdad0 1447 sb->s_flags |= SB_I_VERSION;
da2f0f74 1448 sb->s_iflags |= SB_I_CGROUPWB;
9e11ceee
JK
1449
1450 err = super_setup_bdi(sb);
1451 if (err) {
1452 btrfs_err(fs_info, "super_setup_bdi failed");
1453 return err;
1454 }
1455
ad2b2c80
AV
1456 err = open_ctree(sb, fs_devices, (char *)data);
1457 if (err) {
ab8d0fc4 1458 btrfs_err(fs_info, "open_ctree failed");
ad2b2c80 1459 return err;
a429e513
CM
1460 }
1461
0202e83f 1462 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
5d4f98a2
YZ
1463 if (IS_ERR(inode)) {
1464 err = PTR_ERR(inode);
39279cc3 1465 goto fail_close;
f254e52c 1466 }
f254e52c 1467
48fde701
AV
1468 sb->s_root = d_make_root(inode);
1469 if (!sb->s_root) {
39279cc3
CM
1470 err = -ENOMEM;
1471 goto fail_close;
f254e52c 1472 }
58176a96 1473
1751e8a6 1474 sb->s_flags |= SB_ACTIVE;
2619ba1f 1475 return 0;
39279cc3
CM
1476
1477fail_close:
6bccf3ab 1478 close_ctree(fs_info);
39279cc3 1479 return err;
2619ba1f
CM
1480}
1481
6bf13c0c 1482int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
1483{
1484 struct btrfs_trans_handle *trans;
815745cf
AV
1485 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1486 struct btrfs_root *root = fs_info->tree_root;
2619ba1f 1487
bc074524 1488 trace_btrfs_sync_fs(fs_info, wait);
1abe9b8a 1489
39279cc3 1490 if (!wait) {
815745cf 1491 filemap_flush(fs_info->btree_inode->i_mapping);
39279cc3
CM
1492 return 0;
1493 }
771ed689 1494
6374e57a 1495 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
771ed689 1496
d4edf39b 1497 trans = btrfs_attach_transaction_barrier(root);
60376ce4 1498 if (IS_ERR(trans)) {
354aa0fb 1499 /* no transaction, don't bother */
6b5fe46d
DS
1500 if (PTR_ERR(trans) == -ENOENT) {
1501 /*
1502 * Exit unless we have some pending changes
1503 * that need to go through commit
1504 */
1505 if (fs_info->pending_changes == 0)
1506 return 0;
a53f4f8e
QW
1507 /*
1508 * A non-blocking test if the fs is frozen. We must not
1509 * start a new transaction here otherwise a deadlock
1510 * happens. The pending operations are delayed to the
1511 * next commit after thawing.
1512 */
a7e3c5f2
RP
1513 if (sb_start_write_trylock(sb))
1514 sb_end_write(sb);
a53f4f8e
QW
1515 else
1516 return 0;
6b5fe46d 1517 trans = btrfs_start_transaction(root, 0);
6b5fe46d 1518 }
98bd5c54
DS
1519 if (IS_ERR(trans))
1520 return PTR_ERR(trans);
60376ce4 1521 }
3a45bb20 1522 return btrfs_commit_transaction(trans);
2c90e5d6
CM
1523}
1524
ab0b4a3e
JB
1525static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1526{
1527 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1528 *printed = true;
1529}
1530
34c80b1d 1531static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
a9572a15 1532{
815745cf 1533 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
0f628c63 1534 const char *compress_type;
3ef3959b 1535 const char *subvol_name;
ab0b4a3e 1536 bool printed = false;
a9572a15 1537
3cdde224 1538 if (btrfs_test_opt(info, DEGRADED))
a9572a15 1539 seq_puts(seq, ",degraded");
3cdde224 1540 if (btrfs_test_opt(info, NODATASUM))
a9572a15 1541 seq_puts(seq, ",nodatasum");
3cdde224 1542 if (btrfs_test_opt(info, NODATACOW))
a9572a15 1543 seq_puts(seq, ",nodatacow");
3cdde224 1544 if (btrfs_test_opt(info, NOBARRIER))
a9572a15 1545 seq_puts(seq, ",nobarrier");
95ac567a 1546 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
c1c9ff7c 1547 seq_printf(seq, ",max_inline=%llu", info->max_inline);
a9572a15
EP
1548 if (info->thread_pool_size != min_t(unsigned long,
1549 num_online_cpus() + 2, 8))
f7b885be 1550 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
3cdde224 1551 if (btrfs_test_opt(info, COMPRESS)) {
0f628c63 1552 compress_type = btrfs_compress_type2str(info->compress_type);
3cdde224 1553 if (btrfs_test_opt(info, FORCE_COMPRESS))
200da64e
TI
1554 seq_printf(seq, ",compress-force=%s", compress_type);
1555 else
1556 seq_printf(seq, ",compress=%s", compress_type);
f51d2b59 1557 if (info->compress_level)
fa4d885a 1558 seq_printf(seq, ":%d", info->compress_level);
200da64e 1559 }
3cdde224 1560 if (btrfs_test_opt(info, NOSSD))
c289811c 1561 seq_puts(seq, ",nossd");
3cdde224 1562 if (btrfs_test_opt(info, SSD_SPREAD))
451d7585 1563 seq_puts(seq, ",ssd_spread");
3cdde224 1564 else if (btrfs_test_opt(info, SSD))
a9572a15 1565 seq_puts(seq, ",ssd");
3cdde224 1566 if (btrfs_test_opt(info, NOTREELOG))
6b65c5c6 1567 seq_puts(seq, ",notreelog");
3cdde224 1568 if (btrfs_test_opt(info, NOLOGREPLAY))
ab0b4a3e 1569 print_rescue_option(seq, "nologreplay", &printed);
68319c18
JB
1570 if (btrfs_test_opt(info, USEBACKUPROOT))
1571 print_rescue_option(seq, "usebackuproot", &printed);
42437a63
JB
1572 if (btrfs_test_opt(info, IGNOREBADROOTS))
1573 print_rescue_option(seq, "ignorebadroots", &printed);
882dbe0c
JB
1574 if (btrfs_test_opt(info, IGNOREDATACSUMS))
1575 print_rescue_option(seq, "ignoredatacsums", &printed);
3cdde224 1576 if (btrfs_test_opt(info, FLUSHONCOMMIT))
6b65c5c6 1577 seq_puts(seq, ",flushoncommit");
46b27f50 1578 if (btrfs_test_opt(info, DISCARD_SYNC))
20a5239a 1579 seq_puts(seq, ",discard");
b0643e59
DZ
1580 if (btrfs_test_opt(info, DISCARD_ASYNC))
1581 seq_puts(seq, ",discard=async");
1751e8a6 1582 if (!(info->sb->s_flags & SB_POSIXACL))
a9572a15 1583 seq_puts(seq, ",noacl");
04c41559 1584 if (btrfs_free_space_cache_v1_active(info))
200da64e 1585 seq_puts(seq, ",space_cache");
04c41559 1586 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
70f6d82e 1587 seq_puts(seq, ",space_cache=v2");
73bc1876 1588 else
8965593e 1589 seq_puts(seq, ",nospace_cache");
3cdde224 1590 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
f420ee1e 1591 seq_puts(seq, ",rescan_uuid_tree");
3cdde224 1592 if (btrfs_test_opt(info, CLEAR_CACHE))
200da64e 1593 seq_puts(seq, ",clear_cache");
3cdde224 1594 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
200da64e 1595 seq_puts(seq, ",user_subvol_rm_allowed");
3cdde224 1596 if (btrfs_test_opt(info, ENOSPC_DEBUG))
0942caa3 1597 seq_puts(seq, ",enospc_debug");
3cdde224 1598 if (btrfs_test_opt(info, AUTO_DEFRAG))
0942caa3 1599 seq_puts(seq, ",autodefrag");
3cdde224 1600 if (btrfs_test_opt(info, SKIP_BALANCE))
9555c6c1 1601 seq_puts(seq, ",skip_balance");
8507d216 1602#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
cbeaae4f 1603 if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
8507d216 1604 seq_puts(seq, ",check_int_data");
3cdde224 1605 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
8507d216
WS
1606 seq_puts(seq, ",check_int");
1607 if (info->check_integrity_print_mask)
1608 seq_printf(seq, ",check_int_print_mask=%d",
1609 info->check_integrity_print_mask);
1610#endif
1611 if (info->metadata_ratio)
764cb8b4 1612 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
3cdde224 1613 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
8c342930 1614 seq_puts(seq, ",fatal_errors=panic");
8b87dc17 1615 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
d3740608 1616 seq_printf(seq, ",commit=%u", info->commit_interval);
d0bd4560 1617#ifdef CONFIG_BTRFS_DEBUG
3cdde224 1618 if (btrfs_test_opt(info, FRAGMENT_DATA))
d0bd4560 1619 seq_puts(seq, ",fragment=data");
3cdde224 1620 if (btrfs_test_opt(info, FRAGMENT_METADATA))
d0bd4560
JB
1621 seq_puts(seq, ",fragment=metadata");
1622#endif
fb592373
JB
1623 if (btrfs_test_opt(info, REF_VERIFY))
1624 seq_puts(seq, ",ref_verify");
c8d3fe02
OS
1625 seq_printf(seq, ",subvolid=%llu",
1626 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
3ef3959b
JB
1627 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1628 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1629 if (!IS_ERR(subvol_name)) {
1630 seq_puts(seq, ",subvol=");
1631 seq_escape(seq, subvol_name, " \t\n\\");
1632 kfree(subvol_name);
1633 }
a9572a15
EP
1634 return 0;
1635}
1636
a061fc8d 1637static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4 1638{
815745cf
AV
1639 struct btrfs_fs_info *p = data;
1640 struct btrfs_fs_info *fs_info = btrfs_sb(s);
4b82d6e4 1641
815745cf 1642 return fs_info->fs_devices == p->fs_devices;
4b82d6e4
Y
1643}
1644
450ba0ea
JB
1645static int btrfs_set_super(struct super_block *s, void *data)
1646{
6de1d09d
AV
1647 int err = set_anon_super(s, data);
1648 if (!err)
1649 s->s_fs_info = data;
1650 return err;
4b82d6e4
Y
1651}
1652
f9d9ef62
DS
1653/*
1654 * subvolumes are identified by ino 256
1655 */
1656static inline int is_subvolume_inode(struct inode *inode)
1657{
1658 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1659 return 1;
1660 return 0;
1661}
1662
bb289b7b 1663static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
ae0bc863 1664 struct vfsmount *mnt)
830c4adb 1665{
830c4adb 1666 struct dentry *root;
fa330659 1667 int ret;
830c4adb 1668
05dbe683
OS
1669 if (!subvol_name) {
1670 if (!subvol_objectid) {
1671 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1672 &subvol_objectid);
1673 if (ret) {
1674 root = ERR_PTR(ret);
1675 goto out;
1676 }
1677 }
c0c907a4
MPS
1678 subvol_name = btrfs_get_subvol_name_from_objectid(
1679 btrfs_sb(mnt->mnt_sb), subvol_objectid);
05dbe683
OS
1680 if (IS_ERR(subvol_name)) {
1681 root = ERR_CAST(subvol_name);
1682 subvol_name = NULL;
1683 goto out;
1684 }
1685
1686 }
1687
ea441d11 1688 root = mount_subtree(mnt, subvol_name);
fa330659
OS
1689 /* mount_subtree() drops our reference on the vfsmount. */
1690 mnt = NULL;
830c4adb 1691
bb289b7b 1692 if (!IS_ERR(root)) {
ea441d11 1693 struct super_block *s = root->d_sb;
ab8d0fc4 1694 struct btrfs_fs_info *fs_info = btrfs_sb(s);
bb289b7b
OS
1695 struct inode *root_inode = d_inode(root);
1696 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1697
1698 ret = 0;
1699 if (!is_subvolume_inode(root_inode)) {
ab8d0fc4 1700 btrfs_err(fs_info, "'%s' is not a valid subvolume",
bb289b7b
OS
1701 subvol_name);
1702 ret = -EINVAL;
1703 }
1704 if (subvol_objectid && root_objectid != subvol_objectid) {
05dbe683
OS
1705 /*
1706 * This will also catch a race condition where a
1707 * subvolume which was passed by ID is renamed and
1708 * another subvolume is renamed over the old location.
1709 */
ab8d0fc4
JM
1710 btrfs_err(fs_info,
1711 "subvol '%s' does not match subvolid %llu",
1712 subvol_name, subvol_objectid);
bb289b7b
OS
1713 ret = -EINVAL;
1714 }
1715 if (ret) {
1716 dput(root);
1717 root = ERR_PTR(ret);
1718 deactivate_locked_super(s);
1719 }
f9d9ef62
DS
1720 }
1721
fa330659
OS
1722out:
1723 mntput(mnt);
fa330659 1724 kfree(subvol_name);
830c4adb
JB
1725 return root;
1726}
450ba0ea 1727
312c89fb
MT
1728/*
1729 * Find a superblock for the given device / mount point.
1730 *
1731 * Note: This is based on mount_bdev from fs/super.c with a few additions
1732 * for multiple device setup. Make sure to keep it in sync.
1733 */
72fa39f5
MT
1734static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1735 int flags, const char *device_name, void *data)
1736{
1737 struct block_device *bdev = NULL;
1738 struct super_block *s;
36350e95 1739 struct btrfs_device *device = NULL;
72fa39f5
MT
1740 struct btrfs_fs_devices *fs_devices = NULL;
1741 struct btrfs_fs_info *fs_info = NULL;
204cc0cc 1742 void *new_sec_opts = NULL;
72fa39f5 1743 fmode_t mode = FMODE_READ;
72fa39f5
MT
1744 int error = 0;
1745
1746 if (!(flags & SB_RDONLY))
1747 mode |= FMODE_WRITE;
1748
72fa39f5 1749 if (data) {
a65001e8 1750 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
72fa39f5
MT
1751 if (error)
1752 return ERR_PTR(error);
1753 }
1754
72fa39f5
MT
1755 /*
1756 * Setup a dummy root and fs_info for test/set super. This is because
1757 * we don't actually fill this stuff out until open_ctree, but we need
8260edba
JB
1758 * then open_ctree will properly initialize the file system specific
1759 * settings later. btrfs_init_fs_info initializes the static elements
1760 * of the fs_info (locks and such) to make cleanup easier if we find a
1761 * superblock with our given fs_devices later on at sget() time.
72fa39f5 1762 */
a8fd1f71 1763 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
72fa39f5
MT
1764 if (!fs_info) {
1765 error = -ENOMEM;
1766 goto error_sec_opts;
1767 }
8260edba 1768 btrfs_init_fs_info(fs_info);
72fa39f5 1769
72fa39f5
MT
1770 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1771 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
72fa39f5
MT
1772 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1773 error = -ENOMEM;
1774 goto error_fs_info;
1775 }
1776
399f7f4c 1777 mutex_lock(&uuid_mutex);
fa59f27c 1778 error = btrfs_parse_device_options(data, mode, fs_type);
81ffd56b
DS
1779 if (error) {
1780 mutex_unlock(&uuid_mutex);
399f7f4c 1781 goto error_fs_info;
81ffd56b 1782 }
399f7f4c 1783
36350e95
GJ
1784 device = btrfs_scan_one_device(device_name, mode, fs_type);
1785 if (IS_ERR(device)) {
81ffd56b 1786 mutex_unlock(&uuid_mutex);
36350e95 1787 error = PTR_ERR(device);
399f7f4c 1788 goto error_fs_info;
81ffd56b 1789 }
399f7f4c 1790
36350e95 1791 fs_devices = device->fs_devices;
399f7f4c
DS
1792 fs_info->fs_devices = fs_devices;
1793
72fa39f5 1794 error = btrfs_open_devices(fs_devices, mode, fs_type);
f5194e34 1795 mutex_unlock(&uuid_mutex);
72fa39f5
MT
1796 if (error)
1797 goto error_fs_info;
1798
1799 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1800 error = -EACCES;
1801 goto error_close_devices;
1802 }
1803
d24fa5c1 1804 bdev = fs_devices->latest_dev->bdev;
72fa39f5
MT
1805 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1806 fs_info);
1807 if (IS_ERR(s)) {
1808 error = PTR_ERR(s);
1809 goto error_close_devices;
1810 }
1811
1812 if (s->s_root) {
1813 btrfs_close_devices(fs_devices);
0d4b0463 1814 btrfs_free_fs_info(fs_info);
72fa39f5
MT
1815 if ((flags ^ s->s_flags) & SB_RDONLY)
1816 error = -EBUSY;
1817 } else {
1818 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
e33c267a
RG
1819 shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1820 s->s_id);
72fa39f5 1821 btrfs_sb(s)->bdev_holder = fs_type;
9b4e675a
DS
1822 if (!strstr(crc32c_impl(), "generic"))
1823 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
72fa39f5
MT
1824 error = btrfs_fill_super(s, fs_devices, data);
1825 }
a65001e8 1826 if (!error)
204cc0cc 1827 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
a65001e8 1828 security_free_mnt_opts(&new_sec_opts);
72fa39f5
MT
1829 if (error) {
1830 deactivate_locked_super(s);
a65001e8 1831 return ERR_PTR(error);
72fa39f5
MT
1832 }
1833
1834 return dget(s->s_root);
1835
1836error_close_devices:
1837 btrfs_close_devices(fs_devices);
1838error_fs_info:
0d4b0463 1839 btrfs_free_fs_info(fs_info);
72fa39f5
MT
1840error_sec_opts:
1841 security_free_mnt_opts(&new_sec_opts);
1842 return ERR_PTR(error);
1843}
312c89fb 1844
edf24abe 1845/*
312c89fb 1846 * Mount function which is called by VFS layer.
edf24abe 1847 *
312c89fb
MT
1848 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1849 * which needs vfsmount* of device's root (/). This means device's root has to
1850 * be mounted internally in any case.
1851 *
1852 * Operation flow:
1853 * 1. Parse subvol id related options for later use in mount_subvol().
1854 *
1855 * 2. Mount device's root (/) by calling vfs_kern_mount().
1856 *
1857 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1858 * first place. In order to avoid calling btrfs_mount() again, we use
1859 * different file_system_type which is not registered to VFS by
1860 * register_filesystem() (btrfs_root_fs_type). As a result,
1861 * btrfs_mount_root() is called. The return value will be used by
1862 * mount_subtree() in mount_subvol().
1863 *
1864 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1865 * "btrfs subvolume set-default", mount_subvol() is called always.
edf24abe 1866 */
061dbc6b 1867static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
306e16ce 1868 const char *device_name, void *data)
4b82d6e4 1869{
312c89fb
MT
1870 struct vfsmount *mnt_root;
1871 struct dentry *root;
73f73415
JB
1872 char *subvol_name = NULL;
1873 u64 subvol_objectid = 0;
4b82d6e4
Y
1874 int error = 0;
1875
93b9bcdf
GJ
1876 error = btrfs_parse_subvol_options(data, &subvol_name,
1877 &subvol_objectid);
f23c8af8
ID
1878 if (error) {
1879 kfree(subvol_name);
061dbc6b 1880 return ERR_PTR(error);
f23c8af8 1881 }
edf24abe 1882
312c89fb
MT
1883 /* mount device's root (/) */
1884 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1885 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1886 if (flags & SB_RDONLY) {
1887 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1888 flags & ~SB_RDONLY, device_name, data);
1889 } else {
1890 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1891 flags | SB_RDONLY, device_name, data);
1892 if (IS_ERR(mnt_root)) {
1893 root = ERR_CAST(mnt_root);
532b618b 1894 kfree(subvol_name);
312c89fb
MT
1895 goto out;
1896 }
4b82d6e4 1897
312c89fb
MT
1898 down_write(&mnt_root->mnt_sb->s_umount);
1899 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1900 up_write(&mnt_root->mnt_sb->s_umount);
1901 if (error < 0) {
1902 root = ERR_PTR(error);
1903 mntput(mnt_root);
532b618b 1904 kfree(subvol_name);
312c89fb
MT
1905 goto out;
1906 }
1907 }
f667aef6 1908 }
312c89fb
MT
1909 if (IS_ERR(mnt_root)) {
1910 root = ERR_CAST(mnt_root);
532b618b 1911 kfree(subvol_name);
312c89fb 1912 goto out;
f667aef6 1913 }
4b82d6e4 1914
312c89fb 1915 /* mount_subvol() will free subvol_name and mnt_root */
ae0bc863 1916 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
4b82d6e4 1917
312c89fb
MT
1918out:
1919 return root;
4b82d6e4 1920}
2e635a27 1921
0d2450ab 1922static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
f7b885be 1923 u32 new_pool_size, u32 old_pool_size)
0d2450ab
ST
1924{
1925 if (new_pool_size == old_pool_size)
1926 return;
1927
1928 fs_info->thread_pool_size = new_pool_size;
1929
efe120a0 1930 btrfs_info(fs_info, "resize thread pool %d -> %d",
0d2450ab
ST
1931 old_pool_size, new_pool_size);
1932
5cdc7ad3 1933 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
a31b4a43 1934 btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size);
afe3d242 1935 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
e66f0bb1 1936 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
fccb5d86
QW
1937 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1938 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
5b3bc44e 1939 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
0d2450ab
ST
1940}
1941
f42a34b2
MX
1942static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1943 unsigned long old_opts, int flags)
1944{
dc81cdc5
MX
1945 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1946 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1751e8a6 1947 (flags & SB_RDONLY))) {
dc81cdc5
MX
1948 /* wait for any defraggers to finish */
1949 wait_event(fs_info->transaction_wait,
1950 (atomic_read(&fs_info->defrag_running) == 0));
1751e8a6 1951 if (flags & SB_RDONLY)
dc81cdc5
MX
1952 sync_filesystem(fs_info->sb);
1953 }
1954}
1955
1956static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1957 unsigned long old_opts)
1958{
94846229
BB
1959 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1960
dc81cdc5 1961 /*
180e4d47
LB
1962 * We need to cleanup all defragable inodes if the autodefragment is
1963 * close or the filesystem is read only.
dc81cdc5
MX
1964 */
1965 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
bc98a42c 1966 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
dc81cdc5
MX
1967 btrfs_cleanup_defrag_inodes(fs_info);
1968 }
1969
b0643e59
DZ
1970 /* If we toggled discard async */
1971 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1972 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1973 btrfs_discard_resume(fs_info);
1974 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1975 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1976 btrfs_discard_cleanup(fs_info);
94846229
BB
1977
1978 /* If we toggled space cache */
1979 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1980 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
dc81cdc5
MX
1981}
1982
c146afad
YZ
1983static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1984{
815745cf 1985 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
49b25e05
JM
1986 unsigned old_flags = sb->s_flags;
1987 unsigned long old_opts = fs_info->mount_opt;
1988 unsigned long old_compress_type = fs_info->compress_type;
1989 u64 old_max_inline = fs_info->max_inline;
f7b885be 1990 u32 old_thread_pool_size = fs_info->thread_pool_size;
d612ac59 1991 u32 old_metadata_ratio = fs_info->metadata_ratio;
c146afad
YZ
1992 int ret;
1993
02b9984d 1994 sync_filesystem(sb);
88c4703f 1995 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
dc81cdc5 1996
f667aef6 1997 if (data) {
204cc0cc 1998 void *new_sec_opts = NULL;
f667aef6 1999
a65001e8
AV
2000 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
2001 if (!ret)
204cc0cc 2002 ret = security_sb_remount(sb, new_sec_opts);
a65001e8 2003 security_free_mnt_opts(&new_sec_opts);
f667aef6
QW
2004 if (ret)
2005 goto restore;
f667aef6
QW
2006 }
2007
2ff7e61e 2008 ret = btrfs_parse_options(fs_info, data, *flags);
891f41cb 2009 if (ret)
49b25e05 2010 goto restore;
b288052e 2011
0591f040
QW
2012 /* V1 cache is not supported for subpage mount. */
2013 if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
2014 btrfs_warn(fs_info,
2015 "v1 space cache is not supported for page size %lu with sectorsize %u",
2016 PAGE_SIZE, fs_info->sectorsize);
2017 ret = -EINVAL;
2018 goto restore;
2019 }
f42a34b2 2020 btrfs_remount_begin(fs_info, old_opts, *flags);
0d2450ab
ST
2021 btrfs_resize_thread_pool(fs_info,
2022 fs_info->thread_pool_size, old_thread_pool_size);
2023
c55a4319
BB
2024 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
2025 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
2838d255
BB
2026 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
2027 btrfs_warn(fs_info,
2028 "remount supports changing free space tree only from ro to rw");
2029 /* Make sure free space cache options match the state on disk */
2030 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2031 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2032 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
2033 }
2034 if (btrfs_free_space_cache_v1_active(fs_info)) {
2035 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2036 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
2037 }
2038 }
2039
1751e8a6 2040 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
dc81cdc5 2041 goto out;
c146afad 2042
1751e8a6 2043 if (*flags & SB_RDONLY) {
8dabb742
SB
2044 /*
2045 * this also happens on 'umount -rf' or on shutdown, when
2046 * the filesystem is busy.
2047 */
21c7e756 2048 cancel_work_sync(&fs_info->async_reclaim_work);
57056740 2049 cancel_work_sync(&fs_info->async_data_reclaim_work);
361c093d 2050
b0643e59
DZ
2051 btrfs_discard_cleanup(fs_info);
2052
361c093d
SB
2053 /* wait for the uuid_scan task to finish */
2054 down(&fs_info->uuid_tree_rescan_sem);
2055 /* avoid complains from lockdep et al. */
2056 up(&fs_info->uuid_tree_rescan_sem);
2057
a0a1db70 2058 btrfs_set_sb_rdonly(sb);
c146afad 2059
e44163e1 2060 /*
1751e8a6 2061 * Setting SB_RDONLY will put the cleaner thread to
e44163e1
JM
2062 * sleep at the next loop if it's already active.
2063 * If it's already asleep, we'll leave unused block
2064 * groups on disk until we're mounted read-write again
2065 * unless we clean them up here.
2066 */
e44163e1 2067 btrfs_delete_unused_bgs(fs_info);
e44163e1 2068
a0a1db70
FM
2069 /*
2070 * The cleaner task could be already running before we set the
2071 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
2072 * We must make sure that after we finish the remount, i.e. after
2073 * we call btrfs_commit_super(), the cleaner can no longer start
2074 * a transaction - either because it was dropping a dead root,
2075 * running delayed iputs or deleting an unused block group (the
2076 * cleaner picked a block group from the list of unused block
2077 * groups before we were able to in the previous call to
2078 * btrfs_delete_unused_bgs()).
2079 */
2080 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
2081 TASK_UNINTERRUPTIBLE);
2082
a8cc263e
FM
2083 /*
2084 * We've set the superblock to RO mode, so we might have made
2085 * the cleaner task sleep without running all pending delayed
2086 * iputs. Go through all the delayed iputs here, so that if an
2087 * unmount happens without remounting RW we don't end up at
2088 * finishing close_ctree() with a non-empty list of delayed
2089 * iputs.
2090 */
2091 btrfs_run_delayed_iputs(fs_info);
2092
8dabb742
SB
2093 btrfs_dev_replace_suspend_for_unmount(fs_info);
2094 btrfs_scrub_cancel(fs_info);
061594ef 2095 btrfs_pause_balance(fs_info);
8dabb742 2096
cb13eea3
FM
2097 /*
2098 * Pause the qgroup rescan worker if it is running. We don't want
2099 * it to be still running after we are in RO mode, as after that,
2100 * by the time we unmount, it might have left a transaction open,
2101 * so we would leak the transaction and/or crash.
2102 */
2103 btrfs_qgroup_wait_for_completion(fs_info, false);
2104
6bccf3ab 2105 ret = btrfs_commit_super(fs_info);
49b25e05
JM
2106 if (ret)
2107 goto restore;
c146afad 2108 } else {
84961539 2109 if (BTRFS_FS_ERROR(fs_info)) {
6ef3de9c 2110 btrfs_err(fs_info,
efe120a0 2111 "Remounting read-write after error is not allowed");
6ef3de9c
DS
2112 ret = -EINVAL;
2113 goto restore;
2114 }
8a3db184 2115 if (fs_info->fs_devices->rw_devices == 0) {
49b25e05
JM
2116 ret = -EACCES;
2117 goto restore;
8a3db184 2118 }
2b82032c 2119
6528b99d 2120 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
efe120a0 2121 btrfs_warn(fs_info,
52042d8e 2122 "too many missing devices, writable remount is not allowed");
292fd7fc
SB
2123 ret = -EACCES;
2124 goto restore;
2125 }
2126
8a3db184 2127 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
10a3a3ed
DS
2128 btrfs_warn(fs_info,
2129 "mount required to replay tree-log, cannot remount read-write");
49b25e05
JM
2130 ret = -EINVAL;
2131 goto restore;
8a3db184 2132 }
c146afad 2133
44c0ca21
BB
2134 /*
2135 * NOTE: when remounting with a change that does writes, don't
2136 * put it anywhere above this point, as we are not sure to be
2137 * safe to write until we pass the above checks.
2138 */
2139 ret = btrfs_start_pre_rw_mount(fs_info);
2b6ba629
ID
2140 if (ret)
2141 goto restore;
2142
a0a1db70 2143 btrfs_clear_sb_rdonly(sb);
90c711ab 2144
afcdd129 2145 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
c146afad 2146 }
dc81cdc5 2147out:
faa00889
JB
2148 /*
2149 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2150 * since the absence of the flag means it can be toggled off by remount.
2151 */
2152 *flags |= SB_I_VERSION;
2153
2c6a92b0 2154 wake_up_process(fs_info->transaction_kthread);
dc81cdc5 2155 btrfs_remount_cleanup(fs_info, old_opts);
8cd29088 2156 btrfs_clear_oneshot_options(fs_info);
88c4703f
JT
2157 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2158
c146afad 2159 return 0;
49b25e05
JM
2160
2161restore:
1751e8a6 2162 /* We've hit an error - don't reset SB_RDONLY */
bc98a42c 2163 if (sb_rdonly(sb))
1751e8a6 2164 old_flags |= SB_RDONLY;
a0a1db70
FM
2165 if (!(old_flags & SB_RDONLY))
2166 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
49b25e05
JM
2167 sb->s_flags = old_flags;
2168 fs_info->mount_opt = old_opts;
2169 fs_info->compress_type = old_compress_type;
2170 fs_info->max_inline = old_max_inline;
0d2450ab
ST
2171 btrfs_resize_thread_pool(fs_info,
2172 old_thread_pool_size, fs_info->thread_pool_size);
49b25e05 2173 fs_info->metadata_ratio = old_metadata_ratio;
dc81cdc5 2174 btrfs_remount_cleanup(fs_info, old_opts);
88c4703f
JT
2175 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2176
49b25e05 2177 return ret;
c146afad
YZ
2178}
2179
bcd53741 2180/* Used to sort the devices by max_avail(descending sort) */
214cc184 2181static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
bcd53741 2182{
214cc184
DS
2183 const struct btrfs_device_info *dev_info1 = a;
2184 const struct btrfs_device_info *dev_info2 = b;
2185
2186 if (dev_info1->max_avail > dev_info2->max_avail)
bcd53741 2187 return -1;
214cc184 2188 else if (dev_info1->max_avail < dev_info2->max_avail)
bcd53741 2189 return 1;
bcd53741
AJ
2190 return 0;
2191}
2192
2193/*
2194 * sort the devices by max_avail, in which max free extent size of each device
2195 * is stored.(Descending Sort)
2196 */
2197static inline void btrfs_descending_sort_devices(
2198 struct btrfs_device_info *devices,
2199 size_t nr_devices)
2200{
2201 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2202 btrfs_cmp_device_free_bytes, NULL);
2203}
2204
6d07bcec
MX
2205/*
2206 * The helper to calc the free space on the devices that can be used to store
2207 * file data.
2208 */
7e17916b
AB
2209static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
2210 u64 *free_bytes)
6d07bcec 2211{
6d07bcec
MX
2212 struct btrfs_device_info *devices_info;
2213 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2214 struct btrfs_device *device;
6d07bcec
MX
2215 u64 type;
2216 u64 avail_space;
6d07bcec 2217 u64 min_stripe_size;
559ca6ea 2218 int num_stripes = 1;
6d07bcec 2219 int i = 0, nr_devices;
4f080f57 2220 const struct btrfs_raid_attr *rattr;
6d07bcec 2221
7e33fd99 2222 /*
01327610 2223 * We aren't under the device list lock, so this is racy-ish, but good
7e33fd99
JB
2224 * enough for our purposes.
2225 */
b772a86e 2226 nr_devices = fs_info->fs_devices->open_devices;
7e33fd99
JB
2227 if (!nr_devices) {
2228 smp_mb();
2229 nr_devices = fs_info->fs_devices->open_devices;
2230 ASSERT(nr_devices);
2231 if (!nr_devices) {
2232 *free_bytes = 0;
2233 return 0;
2234 }
2235 }
6d07bcec 2236
d9b0d9ba 2237 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
6a44517d 2238 GFP_KERNEL);
6d07bcec
MX
2239 if (!devices_info)
2240 return -ENOMEM;
2241
01327610 2242 /* calc min stripe number for data space allocation */
1b86826d 2243 type = btrfs_data_alloc_profile(fs_info);
4f080f57
DS
2244 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
2245
e1ea2bee 2246 if (type & BTRFS_BLOCK_GROUP_RAID0)
39fb26c3 2247 num_stripes = nr_devices;
d09cb9e1
DS
2248 else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
2249 num_stripes = rattr->ncopies;
e1ea2bee 2250 else if (type & BTRFS_BLOCK_GROUP_RAID10)
39fb26c3 2251 num_stripes = 4;
6d07bcec 2252
4f080f57
DS
2253 /* Adjust for more than 1 stripe per device */
2254 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
6d07bcec 2255
7e33fd99
JB
2256 rcu_read_lock();
2257 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
e12c9621
AJ
2258 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2259 &device->dev_state) ||
401e29c1
AJ
2260 !device->bdev ||
2261 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
6d07bcec
MX
2262 continue;
2263
7e33fd99
JB
2264 if (i >= nr_devices)
2265 break;
2266
6d07bcec
MX
2267 avail_space = device->total_bytes - device->bytes_used;
2268
2269 /* align with stripe_len */
559ca6ea 2270 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
6d07bcec
MX
2271
2272 /*
37f85ec3
QW
2273 * Ensure we have at least min_stripe_size on top of the
2274 * reserved space on the device.
6d07bcec 2275 */
37f85ec3 2276 if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
6d07bcec
MX
2277 continue;
2278
37f85ec3 2279 avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
559ca6ea 2280
6d07bcec
MX
2281 devices_info[i].dev = device;
2282 devices_info[i].max_avail = avail_space;
2283
2284 i++;
2285 }
7e33fd99 2286 rcu_read_unlock();
6d07bcec
MX
2287
2288 nr_devices = i;
2289
2290 btrfs_descending_sort_devices(devices_info, nr_devices);
2291
2292 i = nr_devices - 1;
2293 avail_space = 0;
559ca6ea
NB
2294 while (nr_devices >= rattr->devs_min) {
2295 num_stripes = min(num_stripes, nr_devices);
39fb26c3 2296
6d07bcec
MX
2297 if (devices_info[i].max_avail >= min_stripe_size) {
2298 int j;
2299 u64 alloc_size;
2300
39fb26c3 2301 avail_space += devices_info[i].max_avail * num_stripes;
6d07bcec 2302 alloc_size = devices_info[i].max_avail;
39fb26c3 2303 for (j = i + 1 - num_stripes; j <= i; j++)
6d07bcec
MX
2304 devices_info[j].max_avail -= alloc_size;
2305 }
2306 i--;
2307 nr_devices--;
2308 }
2309
2310 kfree(devices_info);
2311 *free_bytes = avail_space;
2312 return 0;
2313}
2314
ba7b6e62
DS
2315/*
2316 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2317 *
2318 * If there's a redundant raid level at DATA block groups, use the respective
2319 * multiplier to scale the sizes.
2320 *
2321 * Unused device space usage is based on simulating the chunk allocator
0d0c71b3
DS
2322 * algorithm that respects the device sizes and order of allocations. This is
2323 * a close approximation of the actual use but there are other factors that may
2324 * change the result (like a new metadata chunk).
ba7b6e62 2325 *
ca8a51b3 2326 * If metadata is exhausted, f_bavail will be 0.
ba7b6e62 2327 */
8fd17795
CM
2328static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2329{
815745cf
AV
2330 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2331 struct btrfs_super_block *disk_super = fs_info->super_copy;
bd4d1088
JB
2332 struct btrfs_space_info *found;
2333 u64 total_used = 0;
6d07bcec 2334 u64 total_free_data = 0;
ca8a51b3 2335 u64 total_free_meta = 0;
265fdfa6 2336 u32 bits = fs_info->sectorsize_bits;
de37aa51 2337 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
ba7b6e62
DS
2338 unsigned factor = 1;
2339 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
6d07bcec 2340 int ret;
ca8a51b3 2341 u64 thresh = 0;
ae02d1bd 2342 int mixed = 0;
8fd17795 2343
72804905 2344 list_for_each_entry(found, &fs_info->space_info, list) {
6d07bcec 2345 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
ba7b6e62
DS
2346 int i;
2347
6d07bcec
MX
2348 total_free_data += found->disk_total - found->disk_used;
2349 total_free_data -=
2350 btrfs_account_ro_block_groups_free_space(found);
ba7b6e62
DS
2351
2352 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
46df06b8
DS
2353 if (!list_empty(&found->block_groups[i]))
2354 factor = btrfs_bg_type_to_factor(
2355 btrfs_raid_array[i].bg_flag);
ba7b6e62 2356 }
6d07bcec 2357 }
ae02d1bd
LB
2358
2359 /*
2360 * Metadata in mixed block goup profiles are accounted in data
2361 */
2362 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2363 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2364 mixed = 1;
2365 else
2366 total_free_meta += found->disk_total -
2367 found->disk_used;
2368 }
6d07bcec 2369
b742bb82 2370 total_used += found->disk_used;
89a55897 2371 }
ba7b6e62 2372
ba7b6e62
DS
2373 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2374 buf->f_blocks >>= bits;
2375 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2376
2377 /* Account global block reserve as used, it's in logical size already */
2378 spin_lock(&block_rsv->lock);
41b34acc
LB
2379 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2380 if (buf->f_bfree >= block_rsv->size >> bits)
2381 buf->f_bfree -= block_rsv->size >> bits;
2382 else
2383 buf->f_bfree = 0;
ba7b6e62
DS
2384 spin_unlock(&block_rsv->lock);
2385
0d95c1be 2386 buf->f_bavail = div_u64(total_free_data, factor);
6bccf3ab 2387 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
7e33fd99 2388 if (ret)
6d07bcec 2389 return ret;
ba7b6e62 2390 buf->f_bavail += div_u64(total_free_data, factor);
6d07bcec 2391 buf->f_bavail = buf->f_bavail >> bits;
d397712b 2392
ca8a51b3
DS
2393 /*
2394 * We calculate the remaining metadata space minus global reserve. If
2395 * this is (supposedly) smaller than zero, there's no space. But this
2396 * does not hold in practice, the exhausted state happens where's still
2397 * some positive delta. So we apply some guesswork and compare the
2398 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2399 *
2400 * We probably cannot calculate the exact threshold value because this
2401 * depends on the internal reservations requested by various
2402 * operations, so some operations that consume a few metadata will
2403 * succeed even if the Avail is zero. But this is better than the other
2404 * way around.
2405 */
d4417e22 2406 thresh = SZ_4M;
ca8a51b3 2407
d55966c4
JB
2408 /*
2409 * We only want to claim there's no available space if we can no longer
2410 * allocate chunks for our metadata profile and our global reserve will
2411 * not fit in the free metadata space. If we aren't ->full then we
2412 * still can allocate chunks and thus are fine using the currently
2413 * calculated f_bavail.
2414 */
2415 if (!mixed && block_rsv->space_info->full &&
2416 total_free_meta - thresh < block_rsv->size)
ca8a51b3
DS
2417 buf->f_bavail = 0;
2418
ba7b6e62
DS
2419 buf->f_type = BTRFS_SUPER_MAGIC;
2420 buf->f_bsize = dentry->d_sb->s_blocksize;
2421 buf->f_namelen = BTRFS_NAME_LEN;
2422
9d03632e 2423 /* We treat it as constant endianness (it doesn't matter _which_)
d397712b 2424 because we want the fsid to come out the same whether mounted
9d03632e
DW
2425 on a big-endian or little-endian host */
2426 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2427 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
32d48fa1 2428 /* Mask in the root object ID too, to disambiguate subvols */
4fd786e6
MT
2429 buf->f_fsid.val[0] ^=
2430 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2431 buf->f_fsid.val[1] ^=
2432 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
32d48fa1 2433
8fd17795
CM
2434 return 0;
2435}
b5133862 2436
aea52e19
AV
2437static void btrfs_kill_super(struct super_block *sb)
2438{
815745cf 2439 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
aea52e19 2440 kill_anon_super(sb);
0d4b0463 2441 btrfs_free_fs_info(fs_info);
aea52e19
AV
2442}
2443
2e635a27
CM
2444static struct file_system_type btrfs_fs_type = {
2445 .owner = THIS_MODULE,
2446 .name = "btrfs",
061dbc6b 2447 .mount = btrfs_mount,
aea52e19 2448 .kill_sb = btrfs_kill_super,
f667aef6 2449 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2e635a27 2450};
72fa39f5
MT
2451
2452static struct file_system_type btrfs_root_fs_type = {
2453 .owner = THIS_MODULE,
2454 .name = "btrfs",
2455 .mount = btrfs_mount_root,
2456 .kill_sb = btrfs_kill_super,
5b9b26f5 2457 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
72fa39f5
MT
2458};
2459
7f78e035 2460MODULE_ALIAS_FS("btrfs");
a9218f6b 2461
d8620958
TVB
2462static int btrfs_control_open(struct inode *inode, struct file *file)
2463{
2464 /*
2465 * The control file's private_data is used to hold the
2466 * transaction when it is started and is used to keep
2467 * track of whether a transaction is already in progress.
2468 */
2469 file->private_data = NULL;
2470 return 0;
2471}
2472
d352ac68 2473/*
cfe953c8 2474 * Used by /dev/btrfs-control for devices ioctls.
d352ac68 2475 */
8a4b83cc
CM
2476static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2477 unsigned long arg)
2478{
2479 struct btrfs_ioctl_vol_args *vol;
36350e95 2480 struct btrfs_device *device = NULL;
16cab91a 2481 dev_t devt = 0;
c071fcfd 2482 int ret = -ENOTTY;
8a4b83cc 2483
e441d54d
CM
2484 if (!capable(CAP_SYS_ADMIN))
2485 return -EPERM;
2486
dae7b665
LZ
2487 vol = memdup_user((void __user *)arg, sizeof(*vol));
2488 if (IS_ERR(vol))
2489 return PTR_ERR(vol);
f505754f 2490 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
c071fcfd 2491
8a4b83cc
CM
2492 switch (cmd) {
2493 case BTRFS_IOC_SCAN_DEV:
899f9307 2494 mutex_lock(&uuid_mutex);
36350e95
GJ
2495 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2496 &btrfs_root_fs_type);
2497 ret = PTR_ERR_OR_ZERO(device);
899f9307 2498 mutex_unlock(&uuid_mutex);
8a4b83cc 2499 break;
228a73ab 2500 case BTRFS_IOC_FORGET_DEV:
16cab91a
AJ
2501 if (vol->name[0] != 0) {
2502 ret = lookup_bdev(vol->name, &devt);
2503 if (ret)
2504 break;
2505 }
2506 ret = btrfs_forget_devices(devt);
228a73ab 2507 break;
02db0844 2508 case BTRFS_IOC_DEVICES_READY:
899f9307 2509 mutex_lock(&uuid_mutex);
36350e95
GJ
2510 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2511 &btrfs_root_fs_type);
2512 if (IS_ERR(device)) {
899f9307 2513 mutex_unlock(&uuid_mutex);
36350e95 2514 ret = PTR_ERR(device);
02db0844 2515 break;
899f9307 2516 }
36350e95
GJ
2517 ret = !(device->fs_devices->num_devices ==
2518 device->fs_devices->total_devices);
899f9307 2519 mutex_unlock(&uuid_mutex);
02db0844 2520 break;
c5868f83 2521 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
d5131b65 2522 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
c5868f83 2523 break;
8a4b83cc 2524 }
dae7b665 2525
8a4b83cc 2526 kfree(vol);
f819d837 2527 return ret;
8a4b83cc
CM
2528}
2529
0176260f 2530static int btrfs_freeze(struct super_block *sb)
ed0dab6b 2531{
354aa0fb 2532 struct btrfs_trans_handle *trans;
0b246afa
JM
2533 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2534 struct btrfs_root *root = fs_info->tree_root;
354aa0fb 2535
fac03c8d 2536 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
9e7cc91a
WX
2537 /*
2538 * We don't need a barrier here, we'll wait for any transaction that
2539 * could be in progress on other threads (and do delayed iputs that
2540 * we want to avoid on a frozen filesystem), or do the commit
2541 * ourselves.
2542 */
d4edf39b 2543 trans = btrfs_attach_transaction_barrier(root);
354aa0fb
MX
2544 if (IS_ERR(trans)) {
2545 /* no transaction, don't bother */
2546 if (PTR_ERR(trans) == -ENOENT)
2547 return 0;
2548 return PTR_ERR(trans);
2549 }
3a45bb20 2550 return btrfs_commit_transaction(trans);
ed0dab6b
Y
2551}
2552
9e7cc91a
WX
2553static int btrfs_unfreeze(struct super_block *sb)
2554{
fac03c8d
DS
2555 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2556
2557 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
9e7cc91a
WX
2558 return 0;
2559}
2560
9c5085c1
JB
2561static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2562{
2563 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
9c5085c1 2564
88c14590 2565 /*
6605fd2f
AJ
2566 * There should be always a valid pointer in latest_dev, it may be stale
2567 * for a short moment in case it's being deleted but still valid until
2568 * the end of RCU grace period.
88c14590
DS
2569 */
2570 rcu_read_lock();
6605fd2f 2571 seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
88c14590 2572 rcu_read_unlock();
6605fd2f 2573
9c5085c1
JB
2574 return 0;
2575}
2576
b87221de 2577static const struct super_operations btrfs_super_ops = {
76dda93c 2578 .drop_inode = btrfs_drop_inode,
bd555975 2579 .evict_inode = btrfs_evict_inode,
e20d96d6 2580 .put_super = btrfs_put_super,
d5719762 2581 .sync_fs = btrfs_sync_fs,
a9572a15 2582 .show_options = btrfs_show_options,
9c5085c1 2583 .show_devname = btrfs_show_devname,
2c90e5d6
CM
2584 .alloc_inode = btrfs_alloc_inode,
2585 .destroy_inode = btrfs_destroy_inode,
26602cab 2586 .free_inode = btrfs_free_inode,
8fd17795 2587 .statfs = btrfs_statfs,
c146afad 2588 .remount_fs = btrfs_remount,
0176260f 2589 .freeze_fs = btrfs_freeze,
9e7cc91a 2590 .unfreeze_fs = btrfs_unfreeze,
e20d96d6 2591};
a9218f6b
CM
2592
2593static const struct file_operations btrfs_ctl_fops = {
d8620958 2594 .open = btrfs_control_open,
a9218f6b 2595 .unlocked_ioctl = btrfs_control_ioctl,
1832f2d8 2596 .compat_ioctl = compat_ptr_ioctl,
a9218f6b 2597 .owner = THIS_MODULE,
6038f373 2598 .llseek = noop_llseek,
a9218f6b
CM
2599};
2600
2601static struct miscdevice btrfs_misc = {
578454ff 2602 .minor = BTRFS_MINOR,
a9218f6b
CM
2603 .name = "btrfs-control",
2604 .fops = &btrfs_ctl_fops
2605};
2606
578454ff
KS
2607MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2608MODULE_ALIAS("devname:btrfs-control");
2609
f5c29bd9 2610static int __init btrfs_interface_init(void)
a9218f6b
CM
2611{
2612 return misc_register(&btrfs_misc);
2613}
2614
e67c718b 2615static __cold void btrfs_interface_exit(void)
a9218f6b 2616{
f368ed60 2617 misc_deregister(&btrfs_misc);
a9218f6b
CM
2618}
2619
f5c29bd9 2620static void __init btrfs_print_mod_info(void)
85965600 2621{
edf57cbf 2622 static const char options[] = ""
85965600
DS
2623#ifdef CONFIG_BTRFS_DEBUG
2624 ", debug=on"
2625#endif
79556c3d
SB
2626#ifdef CONFIG_BTRFS_ASSERT
2627 ", assert=on"
2628#endif
85965600
DS
2629#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2630 ", integrity-checker=on"
fb592373
JB
2631#endif
2632#ifdef CONFIG_BTRFS_FS_REF_VERIFY
2633 ", ref-verify=on"
5b316468
NA
2634#endif
2635#ifdef CONFIG_BLK_DEV_ZONED
2636 ", zoned=yes"
2637#else
2638 ", zoned=no"
ea3dc7d2
DS
2639#endif
2640#ifdef CONFIG_FS_VERITY
2641 ", fsverity=yes"
2642#else
2643 ", fsverity=no"
85965600 2644#endif
edf57cbf
BVA
2645 ;
2646 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
85965600
DS
2647}
2648
2e635a27
CM
2649static int __init init_btrfs_fs(void)
2650{
2c90e5d6 2651 int err;
58176a96 2652
63541927
FDBM
2653 btrfs_props_init();
2654
58176a96
JB
2655 err = btrfs_init_sysfs();
2656 if (err)
9678c543 2657 return err;
58176a96 2658
143bede5 2659 btrfs_init_compress();
d1310b2e 2660
261507a0
LZ
2661 err = btrfs_init_cachep();
2662 if (err)
2663 goto free_compress;
2664
d1310b2e 2665 err = extent_io_init();
2f4cbe64
WB
2666 if (err)
2667 goto free_cachep;
2668
6f0d04f8 2669 err = extent_state_cache_init();
d1310b2e
CM
2670 if (err)
2671 goto free_extent_io;
2672
6f0d04f8
JB
2673 err = extent_map_init();
2674 if (err)
2675 goto free_extent_state_cache;
2676
6352b91d 2677 err = ordered_data_init();
2f4cbe64
WB
2678 if (err)
2679 goto free_extent_map;
c8b97818 2680
6352b91d
MX
2681 err = btrfs_delayed_inode_init();
2682 if (err)
2683 goto free_ordered_data;
2684
9247f317 2685 err = btrfs_auto_defrag_init();
16cdcec7
MX
2686 if (err)
2687 goto free_delayed_inode;
2688
78a6184a 2689 err = btrfs_delayed_ref_init();
9247f317
MX
2690 if (err)
2691 goto free_auto_defrag;
2692
b9e9a6cb
WS
2693 err = btrfs_prelim_ref_init();
2694 if (err)
af13b492 2695 goto free_delayed_ref;
b9e9a6cb 2696
97eb6b69
DS
2697 err = btrfs_interface_init();
2698 if (err)
d7b9416f 2699 goto free_prelim_ref;
97eb6b69 2700
8ae1af3c 2701 btrfs_print_mod_info();
dc11dd5d
JB
2702
2703 err = btrfs_run_sanity_tests();
2704 if (err)
2705 goto unregister_ioctl;
2706
2707 err = register_filesystem(&btrfs_fs_type);
2708 if (err)
2709 goto unregister_ioctl;
74255aa0 2710
2f4cbe64
WB
2711 return 0;
2712
a9218f6b
CM
2713unregister_ioctl:
2714 btrfs_interface_exit();
b9e9a6cb
WS
2715free_prelim_ref:
2716 btrfs_prelim_ref_exit();
78a6184a
MX
2717free_delayed_ref:
2718 btrfs_delayed_ref_exit();
9247f317
MX
2719free_auto_defrag:
2720 btrfs_auto_defrag_exit();
16cdcec7
MX
2721free_delayed_inode:
2722 btrfs_delayed_inode_exit();
6352b91d
MX
2723free_ordered_data:
2724 ordered_data_exit();
2f4cbe64
WB
2725free_extent_map:
2726 extent_map_exit();
6f0d04f8
JB
2727free_extent_state_cache:
2728 extent_state_cache_exit();
d1310b2e
CM
2729free_extent_io:
2730 extent_io_exit();
2f4cbe64
WB
2731free_cachep:
2732 btrfs_destroy_cachep();
261507a0
LZ
2733free_compress:
2734 btrfs_exit_compress();
2f4cbe64 2735 btrfs_exit_sysfs();
9678c543 2736
2f4cbe64 2737 return err;
2e635a27
CM
2738}
2739
2740static void __exit exit_btrfs_fs(void)
2741{
39279cc3 2742 btrfs_destroy_cachep();
78a6184a 2743 btrfs_delayed_ref_exit();
9247f317 2744 btrfs_auto_defrag_exit();
16cdcec7 2745 btrfs_delayed_inode_exit();
b9e9a6cb 2746 btrfs_prelim_ref_exit();
6352b91d 2747 ordered_data_exit();
a52d9a80 2748 extent_map_exit();
6f0d04f8 2749 extent_state_cache_exit();
d1310b2e 2750 extent_io_exit();
a9218f6b 2751 btrfs_interface_exit();
2e635a27 2752 unregister_filesystem(&btrfs_fs_type);
58176a96 2753 btrfs_exit_sysfs();
8a4b83cc 2754 btrfs_cleanup_fs_uuids();
261507a0 2755 btrfs_exit_compress();
2e635a27
CM
2756}
2757
60efa5eb 2758late_initcall(init_btrfs_fs);
2e635a27
CM
2759module_exit(exit_btrfs_fs)
2760
2761MODULE_LICENSE("GPL");
d5178578 2762MODULE_SOFTDEP("pre: crc32c");
3951e7f0 2763MODULE_SOFTDEP("pre: xxhash64");
3831bf00 2764MODULE_SOFTDEP("pre: sha256");
352ae07b 2765MODULE_SOFTDEP("pre: blake2b-256");