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