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