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