xref: /openbmc/linux/fs/btrfs/super.c (revision e67c718b)
16cbd5570SChris Mason /*
26cbd5570SChris Mason  * Copyright (C) 2007 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
194b82d6e4SYan #include <linux/blkdev.h>
202e635a27SChris Mason #include <linux/module.h>
21e20d96d6SChris Mason #include <linux/buffer_head.h>
222e635a27SChris Mason #include <linux/fs.h>
232e635a27SChris Mason #include <linux/pagemap.h>
242e635a27SChris Mason #include <linux/highmem.h>
252e635a27SChris Mason #include <linux/time.h>
262e635a27SChris Mason #include <linux/init.h>
27a9572a15SEric Paris #include <linux/seq_file.h>
282e635a27SChris Mason #include <linux/string.h>
292e635a27SChris Mason #include <linux/backing-dev.h>
304b82d6e4SYan #include <linux/mount.h>
31dee26a9fSChris Mason #include <linux/mpage.h>
3275dfe396SChris Mason #include <linux/swap.h>
3375dfe396SChris Mason #include <linux/writeback.h>
348fd17795SChris Mason #include <linux/statfs.h>
3508607c1bSChris Mason #include <linux/compat.h>
3695e05289SChris Mason #include <linux/parser.h>
37c59f8951SChris Mason #include <linux/ctype.h>
386da6abaeSChris Mason #include <linux/namei.h>
39a9218f6bSChris Mason #include <linux/miscdevice.h>
401bcbf313SQinghuang Feng #include <linux/magic.h>
415a0e3ad6STejun Heo #include <linux/slab.h>
4290a887c9SDan Magenheimer #include <linux/cleancache.h>
4322c44fe6SJosef Bacik #include <linux/ratelimit.h>
449678c543SNikolay Borisov #include <linux/crc32c.h>
4555e301fdSFilipe Brandenburger #include <linux/btrfs.h>
4616cdcec7SMiao Xie #include "delayed-inode.h"
472e635a27SChris Mason #include "ctree.h"
48e20d96d6SChris Mason #include "disk-io.h"
49d5719762SChris Mason #include "transaction.h"
502c90e5d6SChris Mason #include "btrfs_inode.h"
513a686375SChris Mason #include "print-tree.h"
5263541927SFilipe David Borba Manana #include "props.h"
535103e947SJosef Bacik #include "xattr.h"
548a4b83ccSChris Mason #include "volumes.h"
55be6e8dc0SBalaji Rao #include "export.h"
56c8b97818SChris Mason #include "compression.h"
579c5085c1SJosef Bacik #include "rcu-string.h"
588dabb742SStefan Behrens #include "dev-replace.h"
5974255aa0SJosef Bacik #include "free-space-cache.h"
60b9e9a6cbSWang Shilong #include "backref.h"
61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h"
622e635a27SChris Mason 
63d3982100SMark Fasheh #include "qgroup.h"
641abe9b8aSliubo #define CREATE_TRACE_POINTS
651abe9b8aSliubo #include <trace/events/btrfs.h>
661abe9b8aSliubo 
67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops;
6872fa39f5SMisono, Tomohiro 
6972fa39f5SMisono, Tomohiro /*
7072fa39f5SMisono, Tomohiro  * Types for mounting the default subvolume and a subvolume explicitly
7172fa39f5SMisono, Tomohiro  * requested by subvol=/path. That way the callchain is straightforward and we
7272fa39f5SMisono, Tomohiro  * don't have to play tricks with the mount options and recursive calls to
7372fa39f5SMisono, Tomohiro  * btrfs_mount.
74312c89fbSMisono, Tomohiro  *
75312c89fbSMisono, Tomohiro  * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
7672fa39f5SMisono, Tomohiro  */
77830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type;
7872fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type;
79e20d96d6SChris Mason 
800723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data);
810723a047SHarald Hoyer 
82e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno)
83acce952bSliubo {
8408748810SDavid Sterba 	char *errstr = "unknown";
85acce952bSliubo 
86acce952bSliubo 	switch (errno) {
87acce952bSliubo 	case -EIO:
88acce952bSliubo 		errstr = "IO failure";
89acce952bSliubo 		break;
90acce952bSliubo 	case -ENOMEM:
91acce952bSliubo 		errstr = "Out of memory";
92acce952bSliubo 		break;
93acce952bSliubo 	case -EROFS:
94acce952bSliubo 		errstr = "Readonly filesystem";
95acce952bSliubo 		break;
968c342930SJeff Mahoney 	case -EEXIST:
978c342930SJeff Mahoney 		errstr = "Object already exists";
988c342930SJeff Mahoney 		break;
9994ef7280SDavid Sterba 	case -ENOSPC:
10094ef7280SDavid Sterba 		errstr = "No space left";
10194ef7280SDavid Sterba 		break;
10294ef7280SDavid Sterba 	case -ENOENT:
10394ef7280SDavid Sterba 		errstr = "No such entry";
10494ef7280SDavid Sterba 		break;
105acce952bSliubo 	}
106acce952bSliubo 
107acce952bSliubo 	return errstr;
108acce952bSliubo }
109acce952bSliubo 
110acce952bSliubo /*
11134d97007SAnand Jain  * __btrfs_handle_fs_error decodes expected errors from the caller and
112acce952bSliubo  * invokes the approciate error response.
113acce952bSliubo  */
114c0d19e2bSDavid Sterba __cold
11534d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
1164da35113SJeff Mahoney 		       unsigned int line, int errno, const char *fmt, ...)
117acce952bSliubo {
118acce952bSliubo 	struct super_block *sb = fs_info->sb;
11957d816a1SAnand Jain #ifdef CONFIG_PRINTK
120acce952bSliubo 	const char *errstr;
12157d816a1SAnand Jain #endif
122acce952bSliubo 
123acce952bSliubo 	/*
124acce952bSliubo 	 * Special case: if the error is EROFS, and we're already
1251751e8a6SLinus Torvalds 	 * under SB_RDONLY, then it is safe here.
126acce952bSliubo 	 */
127bc98a42cSDavid Howells 	if (errno == -EROFS && sb_rdonly(sb))
128acce952bSliubo   		return;
129acce952bSliubo 
13057d816a1SAnand Jain #ifdef CONFIG_PRINTK
13108748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
1324da35113SJeff Mahoney 	if (fmt) {
13337252a66SEric Sandeen 		struct va_format vaf;
13437252a66SEric Sandeen 		va_list args;
13537252a66SEric Sandeen 
13637252a66SEric Sandeen 		va_start(args, fmt);
13737252a66SEric Sandeen 		vaf.fmt = fmt;
13837252a66SEric Sandeen 		vaf.va = &args;
1394da35113SJeff Mahoney 
14062e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
14108748810SDavid Sterba 			sb->s_id, function, line, errno, errstr, &vaf);
14237252a66SEric Sandeen 		va_end(args);
1434da35113SJeff Mahoney 	} else {
14462e85577SJeff Mahoney 		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
14508748810SDavid Sterba 			sb->s_id, function, line, errno, errstr);
1464da35113SJeff Mahoney 	}
14757d816a1SAnand Jain #endif
148acce952bSliubo 
1490713d90cSAnand Jain 	/*
1500713d90cSAnand Jain 	 * Today we only save the error info to memory.  Long term we'll
1510713d90cSAnand Jain 	 * also send it down to the disk
1520713d90cSAnand Jain 	 */
1530713d90cSAnand Jain 	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
1540713d90cSAnand Jain 
1554da35113SJeff Mahoney 	/* Don't go through full error handling during mount */
156922ea899SAnand Jain 	if (!(sb->s_flags & SB_BORN))
157922ea899SAnand Jain 		return;
158922ea899SAnand Jain 
159922ea899SAnand Jain 	if (sb_rdonly(sb))
160922ea899SAnand Jain 		return;
161922ea899SAnand Jain 
162922ea899SAnand Jain 	/* btrfs handle error by forcing the filesystem readonly */
163922ea899SAnand Jain 	sb->s_flags |= SB_RDONLY;
164922ea899SAnand Jain 	btrfs_info(fs_info, "forced readonly");
165922ea899SAnand Jain 	/*
166922ea899SAnand Jain 	 * Note that a running device replace operation is not canceled here
167922ea899SAnand Jain 	 * although there is no way to update the progress. It would add the
168922ea899SAnand Jain 	 * risk of a deadlock, therefore the canceling is omitted. The only
169922ea899SAnand Jain 	 * penalty is that some I/O remains active until the procedure
170922ea899SAnand Jain 	 * completes. The next time when the filesystem is mounted writeable
171922ea899SAnand Jain 	 * again, the device replace operation continues.
172922ea899SAnand Jain 	 */
173acce952bSliubo }
1744da35113SJeff Mahoney 
17557d816a1SAnand Jain #ifdef CONFIG_PRINTK
176533574c6SJoe Perches static const char * const logtypes[] = {
1774da35113SJeff Mahoney 	"emergency",
1784da35113SJeff Mahoney 	"alert",
1794da35113SJeff Mahoney 	"critical",
1804da35113SJeff Mahoney 	"error",
1814da35113SJeff Mahoney 	"warning",
1824da35113SJeff Mahoney 	"notice",
1834da35113SJeff Mahoney 	"info",
1844da35113SJeff Mahoney 	"debug",
1854da35113SJeff Mahoney };
1864da35113SJeff Mahoney 
18735f4e5e6SNikolay Borisov 
18835f4e5e6SNikolay Borisov /*
18935f4e5e6SNikolay Borisov  * Use one ratelimit state per log level so that a flood of less important
19035f4e5e6SNikolay Borisov  * messages doesn't cause more important ones to be dropped.
19135f4e5e6SNikolay Borisov  */
19235f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = {
19335f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
19435f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
19535f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
19635f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
19735f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
19835f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
19935f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
20035f4e5e6SNikolay Borisov 	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
20135f4e5e6SNikolay Borisov };
20235f4e5e6SNikolay Borisov 
203c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2044da35113SJeff Mahoney {
20540f7828bSPetr Mladek 	char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
2064da35113SJeff Mahoney 	struct va_format vaf;
2074da35113SJeff Mahoney 	va_list args;
208533574c6SJoe Perches 	int kern_level;
20940f7828bSPetr Mladek 	const char *type = logtypes[4];
21040f7828bSPetr Mladek 	struct ratelimit_state *ratelimit = &printk_limits[4];
2114da35113SJeff Mahoney 
2124da35113SJeff Mahoney 	va_start(args, fmt);
2134da35113SJeff Mahoney 
214262c5e86SPetr Mladek 	while ((kern_level = printk_get_level(fmt)) != 0) {
215533574c6SJoe Perches 		size_t size = printk_skip_level(fmt) - fmt;
216262c5e86SPetr Mladek 
217262c5e86SPetr Mladek 		if (kern_level >= '0' && kern_level <= '7') {
218533574c6SJoe Perches 			memcpy(lvl, fmt,  size);
219533574c6SJoe Perches 			lvl[size] = '\0';
220533574c6SJoe Perches 			type = logtypes[kern_level - '0'];
22135f4e5e6SNikolay Borisov 			ratelimit = &printk_limits[kern_level - '0'];
222262c5e86SPetr Mladek 		}
223262c5e86SPetr Mladek 		fmt += size;
224262c5e86SPetr Mladek 	}
225262c5e86SPetr Mladek 
2264da35113SJeff Mahoney 	vaf.fmt = fmt;
2274da35113SJeff Mahoney 	vaf.va = &args;
228533574c6SJoe Perches 
22935f4e5e6SNikolay Borisov 	if (__ratelimit(ratelimit))
2303993b112SColin Ian King 		printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
2313993b112SColin Ian King 			fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
232533574c6SJoe Perches 
233533574c6SJoe Perches 	va_end(args);
2344da35113SJeff Mahoney }
235533574c6SJoe Perches #endif
236533574c6SJoe Perches 
2378c342930SJeff Mahoney /*
23849b25e05SJeff Mahoney  * We only mark the transaction aborted and then set the file system read-only.
23949b25e05SJeff Mahoney  * This will prevent new transactions from starting or trying to join this
24049b25e05SJeff Mahoney  * one.
24149b25e05SJeff Mahoney  *
24249b25e05SJeff Mahoney  * This means that error recovery at the call site is limited to freeing
24349b25e05SJeff Mahoney  * any local memory allocations and passing the error code up without
24449b25e05SJeff Mahoney  * further cleanup. The transaction should complete as it normally would
24549b25e05SJeff Mahoney  * in the call path but will return -EIO.
24649b25e05SJeff Mahoney  *
24749b25e05SJeff Mahoney  * We'll complete the cleanup in btrfs_end_transaction and
24849b25e05SJeff Mahoney  * btrfs_commit_transaction.
24949b25e05SJeff Mahoney  */
250c0d19e2bSDavid Sterba __cold
25149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
25266642832SJeff Mahoney 			       const char *function,
25349b25e05SJeff Mahoney 			       unsigned int line, int errno)
25449b25e05SJeff Mahoney {
25566642832SJeff Mahoney 	struct btrfs_fs_info *fs_info = trans->fs_info;
25666642832SJeff Mahoney 
25749b25e05SJeff Mahoney 	trans->aborted = errno;
25849b25e05SJeff Mahoney 	/* Nothing used. The other threads that have joined this
25949b25e05SJeff Mahoney 	 * transaction may be able to continue. */
26064c12921SJeff Mahoney 	if (!trans->dirty && list_empty(&trans->new_bgs)) {
26169ce977aSMiao Xie 		const char *errstr;
26269ce977aSMiao Xie 
26308748810SDavid Sterba 		errstr = btrfs_decode_error(errno);
26466642832SJeff Mahoney 		btrfs_warn(fs_info,
265c2cf52ebSSimon Kirby 		           "%s:%d: Aborting unused transaction(%s).",
26669ce977aSMiao Xie 		           function, line, errstr);
26749b25e05SJeff Mahoney 		return;
26849b25e05SJeff Mahoney 	}
26920c7bcecSSeraphime Kirkovski 	WRITE_ONCE(trans->transaction->aborted, errno);
270501407aaSJosef Bacik 	/* Wake up anybody who may be waiting on this transaction */
27166642832SJeff Mahoney 	wake_up(&fs_info->transaction_wait);
27266642832SJeff Mahoney 	wake_up(&fs_info->transaction_blocked_wait);
27366642832SJeff Mahoney 	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
27449b25e05SJeff Mahoney }
27549b25e05SJeff Mahoney /*
2768c342930SJeff Mahoney  * __btrfs_panic decodes unexpected, fatal errors from the caller,
2778c342930SJeff Mahoney  * issues an alert, and either panics or BUGs, depending on mount options.
2788c342930SJeff Mahoney  */
279c0d19e2bSDavid Sterba __cold
2808c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
2818c342930SJeff Mahoney 		   unsigned int line, int errno, const char *fmt, ...)
2828c342930SJeff Mahoney {
2838c342930SJeff Mahoney 	char *s_id = "<unknown>";
2848c342930SJeff Mahoney 	const char *errstr;
2858c342930SJeff Mahoney 	struct va_format vaf = { .fmt = fmt };
2868c342930SJeff Mahoney 	va_list args;
2878c342930SJeff Mahoney 
2888c342930SJeff Mahoney 	if (fs_info)
2898c342930SJeff Mahoney 		s_id = fs_info->sb->s_id;
2908c342930SJeff Mahoney 
2918c342930SJeff Mahoney 	va_start(args, fmt);
2928c342930SJeff Mahoney 	vaf.va = &args;
2938c342930SJeff Mahoney 
29408748810SDavid Sterba 	errstr = btrfs_decode_error(errno);
295d8953d69SSatoru Takeuchi 	if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
29608748810SDavid Sterba 		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
29708748810SDavid Sterba 			s_id, function, line, &vaf, errno, errstr);
2988c342930SJeff Mahoney 
299efe120a0SFrank Holton 	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
300efe120a0SFrank Holton 		   function, line, &vaf, errno, errstr);
3018c342930SJeff Mahoney 	va_end(args);
3028c342930SJeff Mahoney 	/* Caller calls BUG() */
3038c342930SJeff Mahoney }
304acce952bSliubo 
305e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb)
306e20d96d6SChris Mason {
3076bccf3abSJeff Mahoney 	close_ctree(btrfs_sb(sb));
308e20d96d6SChris Mason }
3092e635a27SChris Mason 
31095e05289SChris Mason enum {
31173f73415SJosef Bacik 	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
312287a0ab9SJosef Bacik 	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
313287a0ab9SJosef Bacik 	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
314261507a0SLi Zefan 	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
315261507a0SLi Zefan 	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
31670f6d82eSOmar Sandoval 	Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
31770f6d82eSOmar Sandoval 	Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
31870f6d82eSOmar Sandoval 	Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
31970f6d82eSOmar Sandoval 	Opt_skip_balance, Opt_check_integrity,
32070f6d82eSOmar Sandoval 	Opt_check_integrity_including_extent_data,
321f420ee1eSStefan Behrens 	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
322e07a2adeSQu Wenruo 	Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
323a258af7aSQu Wenruo 	Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
3248dcddfa0SQu Wenruo 	Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
325fed8f166SQu Wenruo 	Opt_nologreplay, Opt_norecovery,
326d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
327d0bd4560SJosef Bacik 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
328d0bd4560SJosef Bacik #endif
329fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
330fb592373SJosef Bacik 	Opt_ref_verify,
331fb592373SJosef Bacik #endif
3329555c6c1SIlya Dryomov 	Opt_err,
33395e05289SChris Mason };
33495e05289SChris Mason 
3354d4ab6d6SDavid Sterba static const match_table_t tokens = {
336dfe25020SChris Mason 	{Opt_degraded, "degraded"},
33795e05289SChris Mason 	{Opt_subvol, "subvol=%s"},
3381493381fSWang Shilong 	{Opt_subvolid, "subvolid=%s"},
33943e570b0SChristoph Hellwig 	{Opt_device, "device=%s"},
340b6cda9bcSChris Mason 	{Opt_nodatasum, "nodatasum"},
341d399167dSQu Wenruo 	{Opt_datasum, "datasum"},
342be20aa9dSChris Mason 	{Opt_nodatacow, "nodatacow"},
343a258af7aSQu Wenruo 	{Opt_datacow, "datacow"},
34421ad10cfSChris Mason 	{Opt_nobarrier, "nobarrier"},
345842bef58SQu Wenruo 	{Opt_barrier, "barrier"},
3466f568d35SChris Mason 	{Opt_max_inline, "max_inline=%s"},
347eceff22aSAnand Jain 	{Opt_alloc_start, "alloc_start=%s"},		/* deprecated */
348f7b885beSAnand Jain 	{Opt_thread_pool, "thread_pool=%u"},
349c8b97818SChris Mason 	{Opt_compress, "compress"},
350261507a0SLi Zefan 	{Opt_compress_type, "compress=%s"},
351a555f810SChris Mason 	{Opt_compress_force, "compress-force"},
352261507a0SLi Zefan 	{Opt_compress_force_type, "compress-force=%s"},
353e18e4809SChris Mason 	{Opt_ssd, "ssd"},
354451d7585SChris Mason 	{Opt_ssd_spread, "ssd_spread"},
3553b30c22fSChris Mason 	{Opt_nossd, "nossd"},
356bd0330adSQu Wenruo 	{Opt_acl, "acl"},
35733268eafSJosef Bacik 	{Opt_noacl, "noacl"},
3583a5e1404SSage Weil 	{Opt_notreelog, "notreelog"},
359a88998f2SQu Wenruo 	{Opt_treelog, "treelog"},
36096da0919SQu Wenruo 	{Opt_nologreplay, "nologreplay"},
361fed8f166SQu Wenruo 	{Opt_norecovery, "norecovery"},
362dccae999SSage Weil 	{Opt_flushoncommit, "flushoncommit"},
3632c9ee856SQu Wenruo 	{Opt_noflushoncommit, "noflushoncommit"},
364764cb8b4SAnand Jain 	{Opt_ratio, "metadata_ratio=%u"},
365e244a0aeSChristoph Hellwig 	{Opt_discard, "discard"},
366e07a2adeSQu Wenruo 	{Opt_nodiscard, "nodiscard"},
3670af3d00bSJosef Bacik 	{Opt_space_cache, "space_cache"},
36870f6d82eSOmar Sandoval 	{Opt_space_cache_version, "space_cache=%s"},
36988c2ba3bSJosef Bacik 	{Opt_clear_cache, "clear_cache"},
3704260f7c7SSage Weil 	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
37191435650SChris Mason 	{Opt_enospc_debug, "enospc_debug"},
37253036293SQu Wenruo 	{Opt_noenospc_debug, "noenospc_debug"},
373eceff22aSAnand Jain 	{Opt_subvolrootid, "subvolrootid=%d"},		/* deprecated */
3744cb5300bSChris Mason 	{Opt_defrag, "autodefrag"},
375fc0ca9afSQu Wenruo 	{Opt_nodefrag, "noautodefrag"},
3764b9465cbSChris Mason 	{Opt_inode_cache, "inode_cache"},
3773818aea2SQu Wenruo 	{Opt_noinode_cache, "noinode_cache"},
3788965593eSDavid Sterba 	{Opt_no_space_cache, "nospace_cache"},
3798dcddfa0SQu Wenruo 	{Opt_recovery, "recovery"}, /* deprecated */
3808dcddfa0SQu Wenruo 	{Opt_usebackuproot, "usebackuproot"},
3819555c6c1SIlya Dryomov 	{Opt_skip_balance, "skip_balance"},
38221adbd5cSStefan Behrens 	{Opt_check_integrity, "check_int"},
38321adbd5cSStefan Behrens 	{Opt_check_integrity_including_extent_data, "check_int_data"},
38402453bdeSAnand Jain 	{Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
385f420ee1eSStefan Behrens 	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
3868c342930SJeff Mahoney 	{Opt_fatal_errors, "fatal_errors=%s"},
387d3740608SAnand Jain 	{Opt_commit_interval, "commit=%u"},
388d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
389d0bd4560SJosef Bacik 	{Opt_fragment_data, "fragment=data"},
390d0bd4560SJosef Bacik 	{Opt_fragment_metadata, "fragment=metadata"},
391d0bd4560SJosef Bacik 	{Opt_fragment_all, "fragment=all"},
392d0bd4560SJosef Bacik #endif
393fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
394fb592373SJosef Bacik 	{Opt_ref_verify, "ref_verify"},
395fb592373SJosef Bacik #endif
39633268eafSJosef Bacik 	{Opt_err, NULL},
39795e05289SChris Mason };
39895e05289SChris Mason 
399edf24abeSChristoph Hellwig /*
400edf24abeSChristoph Hellwig  * Regular mount options parser.  Everything that is needed only when
401edf24abeSChristoph Hellwig  * reading in a new superblock is parsed here.
40249b25e05SJeff Mahoney  * XXX JDM: This needs to be cleaned up for remount.
403edf24abeSChristoph Hellwig  */
4042ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
40596da0919SQu Wenruo 			unsigned long new_flags)
40695e05289SChris Mason {
40795e05289SChris Mason 	substring_t args[MAX_OPT_ARGS];
408e215772cSMisono, Tomohiro 	char *p, *num;
40973bc1876SJosef Bacik 	u64 cache_gen;
4104543df7eSChris Mason 	int intarg;
411a7a3f7caSSage Weil 	int ret = 0;
412261507a0SLi Zefan 	char *compress_type;
413261507a0SLi Zefan 	bool compress_force = false;
414b7c47bbbSTsutomu Itoh 	enum btrfs_compression_type saved_compress_type;
415b7c47bbbSTsutomu Itoh 	bool saved_compress_force;
416b7c47bbbSTsutomu Itoh 	int no_compress = 0;
417b6cda9bcSChris Mason 
4180b246afaSJeff Mahoney 	cache_gen = btrfs_super_cache_generation(info->super_copy);
4190b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
42070f6d82eSOmar Sandoval 		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
42170f6d82eSOmar Sandoval 	else if (cache_gen)
42273bc1876SJosef Bacik 		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
42373bc1876SJosef Bacik 
42496da0919SQu Wenruo 	/*
42596da0919SQu Wenruo 	 * Even the options are empty, we still need to do extra check
42696da0919SQu Wenruo 	 * against new flags
42796da0919SQu Wenruo 	 */
42895e05289SChris Mason 	if (!options)
42996da0919SQu Wenruo 		goto check;
43095e05289SChris Mason 
43195e05289SChris Mason 	while ((p = strsep(&options, ",")) != NULL) {
43295e05289SChris Mason 		int token;
43395e05289SChris Mason 		if (!*p)
43495e05289SChris Mason 			continue;
43595e05289SChris Mason 
43695e05289SChris Mason 		token = match_token(p, tokens, args);
43795e05289SChris Mason 		switch (token) {
438dfe25020SChris Mason 		case Opt_degraded:
4390b246afaSJeff Mahoney 			btrfs_info(info, "allowing degraded mounts");
440dfe25020SChris Mason 			btrfs_set_opt(info->mount_opt, DEGRADED);
441dfe25020SChris Mason 			break;
44295e05289SChris Mason 		case Opt_subvol:
44373f73415SJosef Bacik 		case Opt_subvolid:
444e15d0542SXin Zhong 		case Opt_subvolrootid:
44543e570b0SChristoph Hellwig 		case Opt_device:
446edf24abeSChristoph Hellwig 			/*
447d7407606SMisono, Tomohiro 			 * These are parsed by btrfs_parse_subvol_options
448d7407606SMisono, Tomohiro 			 * and btrfs_parse_early_options
449edf24abeSChristoph Hellwig 			 * and can be happily ignored here.
450edf24abeSChristoph Hellwig 			 */
45195e05289SChris Mason 			break;
452b6cda9bcSChris Mason 		case Opt_nodatasum:
4533cdde224SJeff Mahoney 			btrfs_set_and_info(info, NODATASUM,
45407802534SQu Wenruo 					   "setting nodatasum");
455be20aa9dSChris Mason 			break;
456d399167dSQu Wenruo 		case Opt_datasum:
4573cdde224SJeff Mahoney 			if (btrfs_test_opt(info, NODATASUM)) {
4583cdde224SJeff Mahoney 				if (btrfs_test_opt(info, NODATACOW))
4590b246afaSJeff Mahoney 					btrfs_info(info,
4605d163e0eSJeff Mahoney 						   "setting datasum, datacow enabled");
461d399167dSQu Wenruo 				else
4620b246afaSJeff Mahoney 					btrfs_info(info, "setting datasum");
46307802534SQu Wenruo 			}
464d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATACOW);
465d399167dSQu Wenruo 			btrfs_clear_opt(info->mount_opt, NODATASUM);
466d399167dSQu Wenruo 			break;
467be20aa9dSChris Mason 		case Opt_nodatacow:
4683cdde224SJeff Mahoney 			if (!btrfs_test_opt(info, NODATACOW)) {
4693cdde224SJeff Mahoney 				if (!btrfs_test_opt(info, COMPRESS) ||
4703cdde224SJeff Mahoney 				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
4710b246afaSJeff Mahoney 					btrfs_info(info,
472efe120a0SFrank Holton 						   "setting nodatacow, compression disabled");
473bedb2ccaSAndrei Popa 				} else {
4740b246afaSJeff Mahoney 					btrfs_info(info, "setting nodatacow");
475bedb2ccaSAndrei Popa 				}
47607802534SQu Wenruo 			}
477bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, COMPRESS);
478bedb2ccaSAndrei Popa 			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
479be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATACOW);
480be20aa9dSChris Mason 			btrfs_set_opt(info->mount_opt, NODATASUM);
481b6cda9bcSChris Mason 			break;
482a258af7aSQu Wenruo 		case Opt_datacow:
4833cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NODATACOW,
48407802534SQu Wenruo 					     "setting datacow");
485a258af7aSQu Wenruo 			break;
486a555f810SChris Mason 		case Opt_compress_force:
487261507a0SLi Zefan 		case Opt_compress_force_type:
488261507a0SLi Zefan 			compress_force = true;
4891c697d4aSEric Sandeen 			/* Fallthrough */
490261507a0SLi Zefan 		case Opt_compress:
491261507a0SLi Zefan 		case Opt_compress_type:
4923cdde224SJeff Mahoney 			saved_compress_type = btrfs_test_opt(info,
4933cdde224SJeff Mahoney 							     COMPRESS) ?
494b7c47bbbSTsutomu Itoh 				info->compress_type : BTRFS_COMPRESS_NONE;
495b7c47bbbSTsutomu Itoh 			saved_compress_force =
4963cdde224SJeff Mahoney 				btrfs_test_opt(info, FORCE_COMPRESS);
497261507a0SLi Zefan 			if (token == Opt_compress ||
498261507a0SLi Zefan 			    token == Opt_compress_force ||
499a7164fa4SDavid Sterba 			    strncmp(args[0].from, "zlib", 4) == 0) {
500261507a0SLi Zefan 				compress_type = "zlib";
501eae8d825SQu Wenruo 
502261507a0SLi Zefan 				info->compress_type = BTRFS_COMPRESS_ZLIB;
503eae8d825SQu Wenruo 				info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
504eae8d825SQu Wenruo 				/*
505eae8d825SQu Wenruo 				 * args[0] contains uninitialized data since
506eae8d825SQu Wenruo 				 * for these tokens we don't expect any
507eae8d825SQu Wenruo 				 * parameter.
508eae8d825SQu Wenruo 				 */
509eae8d825SQu Wenruo 				if (token != Opt_compress &&
510eae8d825SQu Wenruo 				    token != Opt_compress_force)
511f51d2b59SDavid Sterba 					info->compress_level =
512f51d2b59SDavid Sterba 					  btrfs_compress_str2level(args[0].from);
513063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
514bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
515bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
516b7c47bbbSTsutomu Itoh 				no_compress = 0;
517a7164fa4SDavid Sterba 			} else if (strncmp(args[0].from, "lzo", 3) == 0) {
518a6fa6faeSLi Zefan 				compress_type = "lzo";
519a6fa6faeSLi Zefan 				info->compress_type = BTRFS_COMPRESS_LZO;
520063849eaSArnd Hannemann 				btrfs_set_opt(info->mount_opt, COMPRESS);
521bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATACOW);
522bedb2ccaSAndrei Popa 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5232b0ce2c2SMitch Harder 				btrfs_set_fs_incompat(info, COMPRESS_LZO);
524b7c47bbbSTsutomu Itoh 				no_compress = 0;
5255c1aab1dSNick Terrell 			} else if (strcmp(args[0].from, "zstd") == 0) {
5265c1aab1dSNick Terrell 				compress_type = "zstd";
5275c1aab1dSNick Terrell 				info->compress_type = BTRFS_COMPRESS_ZSTD;
5285c1aab1dSNick Terrell 				btrfs_set_opt(info->mount_opt, COMPRESS);
5295c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATACOW);
5305c1aab1dSNick Terrell 				btrfs_clear_opt(info->mount_opt, NODATASUM);
5315c1aab1dSNick Terrell 				btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
5325c1aab1dSNick Terrell 				no_compress = 0;
533063849eaSArnd Hannemann 			} else if (strncmp(args[0].from, "no", 2) == 0) {
534063849eaSArnd Hannemann 				compress_type = "no";
535063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, COMPRESS);
536063849eaSArnd Hannemann 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
537063849eaSArnd Hannemann 				compress_force = false;
538b7c47bbbSTsutomu Itoh 				no_compress++;
539261507a0SLi Zefan 			} else {
540261507a0SLi Zefan 				ret = -EINVAL;
541261507a0SLi Zefan 				goto out;
542261507a0SLi Zefan 			}
543261507a0SLi Zefan 
544261507a0SLi Zefan 			if (compress_force) {
545b7c47bbbSTsutomu Itoh 				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
546143f3636SDavid Sterba 			} else {
5474027e0f4SWang Shilong 				/*
5484027e0f4SWang Shilong 				 * If we remount from compress-force=xxx to
5494027e0f4SWang Shilong 				 * compress=xxx, we need clear FORCE_COMPRESS
5504027e0f4SWang Shilong 				 * flag, otherwise, there is no way for users
5514027e0f4SWang Shilong 				 * to disable forcible compression separately.
5524027e0f4SWang Shilong 				 */
5534027e0f4SWang Shilong 				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
554a7e252afSMiao Xie 			}
5553cdde224SJeff Mahoney 			if ((btrfs_test_opt(info, COMPRESS) &&
556b7c47bbbSTsutomu Itoh 			     (info->compress_type != saved_compress_type ||
557b7c47bbbSTsutomu Itoh 			      compress_force != saved_compress_force)) ||
5583cdde224SJeff Mahoney 			    (!btrfs_test_opt(info, COMPRESS) &&
559b7c47bbbSTsutomu Itoh 			     no_compress == 1)) {
560f51d2b59SDavid Sterba 				btrfs_info(info, "%s %s compression, level %d",
561b7c47bbbSTsutomu Itoh 					   (compress_force) ? "force" : "use",
562f51d2b59SDavid Sterba 					   compress_type, info->compress_level);
563b7c47bbbSTsutomu Itoh 			}
564b7c47bbbSTsutomu Itoh 			compress_force = false;
565a555f810SChris Mason 			break;
566e18e4809SChris Mason 		case Opt_ssd:
5673cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD,
568583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
569951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
570e18e4809SChris Mason 			break;
571451d7585SChris Mason 		case Opt_ssd_spread:
572583b7231SHans van Kranenburg 			btrfs_set_and_info(info, SSD,
573583b7231SHans van Kranenburg 					   "enabling ssd optimizations");
5743cdde224SJeff Mahoney 			btrfs_set_and_info(info, SSD_SPREAD,
575583b7231SHans van Kranenburg 					   "using spread ssd allocation scheme");
576951e7966SAdam Borowski 			btrfs_clear_opt(info->mount_opt, NOSSD);
577451d7585SChris Mason 			break;
5783b30c22fSChris Mason 		case Opt_nossd:
579583b7231SHans van Kranenburg 			btrfs_set_opt(info->mount_opt, NOSSD);
580583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD,
581583b7231SHans van Kranenburg 					     "not using ssd optimizations");
582583b7231SHans van Kranenburg 			btrfs_clear_and_info(info, SSD_SPREAD,
583583b7231SHans van Kranenburg 					     "not using spread ssd allocation scheme");
5843b30c22fSChris Mason 			break;
585842bef58SQu Wenruo 		case Opt_barrier:
5863cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOBARRIER,
58707802534SQu Wenruo 					     "turning on barriers");
588842bef58SQu Wenruo 			break;
58921ad10cfSChris Mason 		case Opt_nobarrier:
5903cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOBARRIER,
59107802534SQu Wenruo 					   "turning off barriers");
59221ad10cfSChris Mason 			break;
5934543df7eSChris Mason 		case Opt_thread_pool:
5942c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
5952c334e87SWang Shilong 			if (ret) {
5962c334e87SWang Shilong 				goto out;
597f7b885beSAnand Jain 			} else if (intarg == 0) {
5982c334e87SWang Shilong 				ret = -EINVAL;
5992c334e87SWang Shilong 				goto out;
6002c334e87SWang Shilong 			}
601f7b885beSAnand Jain 			info->thread_pool_size = intarg;
6024543df7eSChris Mason 			break;
6036f568d35SChris Mason 		case Opt_max_inline:
604edf24abeSChristoph Hellwig 			num = match_strdup(&args[0]);
6056f568d35SChris Mason 			if (num) {
60691748467SAkinobu Mita 				info->max_inline = memparse(num, NULL);
6076f568d35SChris Mason 				kfree(num);
6086f568d35SChris Mason 
60915ada040SChris Mason 				if (info->max_inline) {
610feb5f965SMitch Harder 					info->max_inline = min_t(u64,
61115ada040SChris Mason 						info->max_inline,
6120b246afaSJeff Mahoney 						info->sectorsize);
61315ada040SChris Mason 				}
6140b246afaSJeff Mahoney 				btrfs_info(info, "max_inline at %llu",
615c1c9ff7cSGeert Uytterhoeven 					   info->max_inline);
6162c334e87SWang Shilong 			} else {
6172c334e87SWang Shilong 				ret = -ENOMEM;
6182c334e87SWang Shilong 				goto out;
6196f568d35SChris Mason 			}
6206f568d35SChris Mason 			break;
6218f662a76SChris Mason 		case Opt_alloc_start:
6220d0c71b3SDavid Sterba 			btrfs_info(info,
6230d0c71b3SDavid Sterba 				"option alloc_start is obsolete, ignored");
6248f662a76SChris Mason 			break;
625bd0330adSQu Wenruo 		case Opt_acl:
62645ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL
6271751e8a6SLinus Torvalds 			info->sb->s_flags |= SB_POSIXACL;
628bd0330adSQu Wenruo 			break;
62945ff35d6SGuangliang Zhao #else
6300b246afaSJeff Mahoney 			btrfs_err(info, "support for ACL not compiled in!");
63145ff35d6SGuangliang Zhao 			ret = -EINVAL;
63245ff35d6SGuangliang Zhao 			goto out;
63345ff35d6SGuangliang Zhao #endif
63433268eafSJosef Bacik 		case Opt_noacl:
6351751e8a6SLinus Torvalds 			info->sb->s_flags &= ~SB_POSIXACL;
63633268eafSJosef Bacik 			break;
6373a5e1404SSage Weil 		case Opt_notreelog:
6383cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOTREELOG,
63907802534SQu Wenruo 					   "disabling tree log");
6403a5e1404SSage Weil 			break;
641a88998f2SQu Wenruo 		case Opt_treelog:
6423cdde224SJeff Mahoney 			btrfs_clear_and_info(info, NOTREELOG,
64307802534SQu Wenruo 					     "enabling tree log");
644a88998f2SQu Wenruo 			break;
645fed8f166SQu Wenruo 		case Opt_norecovery:
64696da0919SQu Wenruo 		case Opt_nologreplay:
6473cdde224SJeff Mahoney 			btrfs_set_and_info(info, NOLOGREPLAY,
64896da0919SQu Wenruo 					   "disabling log replay at mount time");
64996da0919SQu Wenruo 			break;
650dccae999SSage Weil 		case Opt_flushoncommit:
6513cdde224SJeff Mahoney 			btrfs_set_and_info(info, FLUSHONCOMMIT,
65207802534SQu Wenruo 					   "turning on flush-on-commit");
653dccae999SSage Weil 			break;
6542c9ee856SQu Wenruo 		case Opt_noflushoncommit:
6553cdde224SJeff Mahoney 			btrfs_clear_and_info(info, FLUSHONCOMMIT,
65607802534SQu Wenruo 					     "turning off flush-on-commit");
6572c9ee856SQu Wenruo 			break;
65897e728d4SJosef Bacik 		case Opt_ratio:
6592c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
660764cb8b4SAnand Jain 			if (ret)
6612c334e87SWang Shilong 				goto out;
66297e728d4SJosef Bacik 			info->metadata_ratio = intarg;
663764cb8b4SAnand Jain 			btrfs_info(info, "metadata ratio %u",
66497e728d4SJosef Bacik 				   info->metadata_ratio);
66597e728d4SJosef Bacik 			break;
666e244a0aeSChristoph Hellwig 		case Opt_discard:
6673cdde224SJeff Mahoney 			btrfs_set_and_info(info, DISCARD,
66807802534SQu Wenruo 					   "turning on discard");
669e244a0aeSChristoph Hellwig 			break;
670e07a2adeSQu Wenruo 		case Opt_nodiscard:
6713cdde224SJeff Mahoney 			btrfs_clear_and_info(info, DISCARD,
67207802534SQu Wenruo 					     "turning off discard");
673e07a2adeSQu Wenruo 			break;
6740af3d00bSJosef Bacik 		case Opt_space_cache:
67570f6d82eSOmar Sandoval 		case Opt_space_cache_version:
67670f6d82eSOmar Sandoval 			if (token == Opt_space_cache ||
67770f6d82eSOmar Sandoval 			    strcmp(args[0].from, "v1") == 0) {
6780b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
67970f6d82eSOmar Sandoval 						FREE_SPACE_TREE);
6803cdde224SJeff Mahoney 				btrfs_set_and_info(info, SPACE_CACHE,
68107802534SQu Wenruo 					   "enabling disk space caching");
68270f6d82eSOmar Sandoval 			} else if (strcmp(args[0].from, "v2") == 0) {
6830b246afaSJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
68470f6d82eSOmar Sandoval 						SPACE_CACHE);
6850b246afaSJeff Mahoney 				btrfs_set_and_info(info, FREE_SPACE_TREE,
68670f6d82eSOmar Sandoval 						   "enabling free space tree");
68770f6d82eSOmar Sandoval 			} else {
68870f6d82eSOmar Sandoval 				ret = -EINVAL;
68970f6d82eSOmar Sandoval 				goto out;
69070f6d82eSOmar Sandoval 			}
6910de90876SJosef Bacik 			break;
692f420ee1eSStefan Behrens 		case Opt_rescan_uuid_tree:
693f420ee1eSStefan Behrens 			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
694f420ee1eSStefan Behrens 			break;
69573bc1876SJosef Bacik 		case Opt_no_space_cache:
6963cdde224SJeff Mahoney 			if (btrfs_test_opt(info, SPACE_CACHE)) {
6970b246afaSJeff Mahoney 				btrfs_clear_and_info(info, SPACE_CACHE,
69807802534SQu Wenruo 					     "disabling disk space caching");
69970f6d82eSOmar Sandoval 			}
7003cdde224SJeff Mahoney 			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
7010b246afaSJeff Mahoney 				btrfs_clear_and_info(info, FREE_SPACE_TREE,
70270f6d82eSOmar Sandoval 					     "disabling free space tree");
70370f6d82eSOmar Sandoval 			}
70473bc1876SJosef Bacik 			break;
7054b9465cbSChris Mason 		case Opt_inode_cache:
7067e1876acSDavid Sterba 			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
70707802534SQu Wenruo 					   "enabling inode map caching");
7083818aea2SQu Wenruo 			break;
7093818aea2SQu Wenruo 		case Opt_noinode_cache:
7107e1876acSDavid Sterba 			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
71107802534SQu Wenruo 					     "disabling inode map caching");
7124b9465cbSChris Mason 			break;
71388c2ba3bSJosef Bacik 		case Opt_clear_cache:
7143cdde224SJeff Mahoney 			btrfs_set_and_info(info, CLEAR_CACHE,
71507802534SQu Wenruo 					   "force clearing of disk cache");
7160af3d00bSJosef Bacik 			break;
7174260f7c7SSage Weil 		case Opt_user_subvol_rm_allowed:
7184260f7c7SSage Weil 			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
7194260f7c7SSage Weil 			break;
72091435650SChris Mason 		case Opt_enospc_debug:
72191435650SChris Mason 			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
72291435650SChris Mason 			break;
72353036293SQu Wenruo 		case Opt_noenospc_debug:
72453036293SQu Wenruo 			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
72553036293SQu Wenruo 			break;
7264cb5300bSChris Mason 		case Opt_defrag:
7273cdde224SJeff Mahoney 			btrfs_set_and_info(info, AUTO_DEFRAG,
72807802534SQu Wenruo 					   "enabling auto defrag");
7294cb5300bSChris Mason 			break;
730fc0ca9afSQu Wenruo 		case Opt_nodefrag:
7313cdde224SJeff Mahoney 			btrfs_clear_and_info(info, AUTO_DEFRAG,
73207802534SQu Wenruo 					     "disabling auto defrag");
733fc0ca9afSQu Wenruo 			break;
734af31f5e5SChris Mason 		case Opt_recovery:
7350b246afaSJeff Mahoney 			btrfs_warn(info,
7368dcddfa0SQu Wenruo 				   "'recovery' is deprecated, use 'usebackuproot' instead");
7378dcddfa0SQu Wenruo 		case Opt_usebackuproot:
7380b246afaSJeff Mahoney 			btrfs_info(info,
7398dcddfa0SQu Wenruo 				   "trying to use backup root at mount time");
7408dcddfa0SQu Wenruo 			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
741af31f5e5SChris Mason 			break;
7429555c6c1SIlya Dryomov 		case Opt_skip_balance:
7439555c6c1SIlya Dryomov 			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
7449555c6c1SIlya Dryomov 			break;
74521adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
74621adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
7470b246afaSJeff Mahoney 			btrfs_info(info,
748efe120a0SFrank Holton 				   "enabling check integrity including extent data");
74921adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt,
75021adbd5cSStefan Behrens 				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
75121adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
75221adbd5cSStefan Behrens 			break;
75321adbd5cSStefan Behrens 		case Opt_check_integrity:
7540b246afaSJeff Mahoney 			btrfs_info(info, "enabling check integrity");
75521adbd5cSStefan Behrens 			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
75621adbd5cSStefan Behrens 			break;
75721adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7582c334e87SWang Shilong 			ret = match_int(&args[0], &intarg);
75902453bdeSAnand Jain 			if (ret)
7602c334e87SWang Shilong 				goto out;
76121adbd5cSStefan Behrens 			info->check_integrity_print_mask = intarg;
76202453bdeSAnand Jain 			btrfs_info(info, "check_integrity_print_mask 0x%x",
76321adbd5cSStefan Behrens 				   info->check_integrity_print_mask);
76421adbd5cSStefan Behrens 			break;
76521adbd5cSStefan Behrens #else
76621adbd5cSStefan Behrens 		case Opt_check_integrity_including_extent_data:
76721adbd5cSStefan Behrens 		case Opt_check_integrity:
76821adbd5cSStefan Behrens 		case Opt_check_integrity_print_mask:
7690b246afaSJeff Mahoney 			btrfs_err(info,
770efe120a0SFrank Holton 				  "support for check_integrity* not compiled in!");
77121adbd5cSStefan Behrens 			ret = -EINVAL;
77221adbd5cSStefan Behrens 			goto out;
77321adbd5cSStefan Behrens #endif
7748c342930SJeff Mahoney 		case Opt_fatal_errors:
7758c342930SJeff Mahoney 			if (strcmp(args[0].from, "panic") == 0)
7768c342930SJeff Mahoney 				btrfs_set_opt(info->mount_opt,
7778c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7788c342930SJeff Mahoney 			else if (strcmp(args[0].from, "bug") == 0)
7798c342930SJeff Mahoney 				btrfs_clear_opt(info->mount_opt,
7808c342930SJeff Mahoney 					      PANIC_ON_FATAL_ERROR);
7818c342930SJeff Mahoney 			else {
7828c342930SJeff Mahoney 				ret = -EINVAL;
7838c342930SJeff Mahoney 				goto out;
7848c342930SJeff Mahoney 			}
7858c342930SJeff Mahoney 			break;
7868b87dc17SDavid Sterba 		case Opt_commit_interval:
7878b87dc17SDavid Sterba 			intarg = 0;
7888b87dc17SDavid Sterba 			ret = match_int(&args[0], &intarg);
789d3740608SAnand Jain 			if (ret)
7908b87dc17SDavid Sterba 				goto out;
791d3740608SAnand Jain 			if (intarg == 0) {
792d3740608SAnand Jain 				btrfs_info(info,
793d3740608SAnand Jain 					   "using default commit interval %us",
794d3740608SAnand Jain 					   BTRFS_DEFAULT_COMMIT_INTERVAL);
795d3740608SAnand Jain 				intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
796d3740608SAnand Jain 			} else if (intarg > 300) {
797d3740608SAnand Jain 				btrfs_warn(info, "excessive commit interval %d",
7988b87dc17SDavid Sterba 					   intarg);
7998b87dc17SDavid Sterba 			}
8008b87dc17SDavid Sterba 			info->commit_interval = intarg;
8018b87dc17SDavid Sterba 			break;
802d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
803d0bd4560SJosef Bacik 		case Opt_fragment_all:
8040b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting all space");
805d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
806d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
807d0bd4560SJosef Bacik 			break;
808d0bd4560SJosef Bacik 		case Opt_fragment_metadata:
8090b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting metadata");
810d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt,
811d0bd4560SJosef Bacik 				      FRAGMENT_METADATA);
812d0bd4560SJosef Bacik 			break;
813d0bd4560SJosef Bacik 		case Opt_fragment_data:
8140b246afaSJeff Mahoney 			btrfs_info(info, "fragmenting data");
815d0bd4560SJosef Bacik 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
816d0bd4560SJosef Bacik 			break;
817d0bd4560SJosef Bacik #endif
818fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
819fb592373SJosef Bacik 		case Opt_ref_verify:
820fb592373SJosef Bacik 			btrfs_info(info, "doing ref verification");
821fb592373SJosef Bacik 			btrfs_set_opt(info->mount_opt, REF_VERIFY);
822fb592373SJosef Bacik 			break;
823fb592373SJosef Bacik #endif
824a7a3f7caSSage Weil 		case Opt_err:
8250b246afaSJeff Mahoney 			btrfs_info(info, "unrecognized mount option '%s'", p);
826a7a3f7caSSage Weil 			ret = -EINVAL;
827a7a3f7caSSage Weil 			goto out;
82895e05289SChris Mason 		default:
829be20aa9dSChris Mason 			break;
83095e05289SChris Mason 		}
83195e05289SChris Mason 	}
83296da0919SQu Wenruo check:
83396da0919SQu Wenruo 	/*
83496da0919SQu Wenruo 	 * Extra check for current option against current flag
83596da0919SQu Wenruo 	 */
8361751e8a6SLinus Torvalds 	if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
8370b246afaSJeff Mahoney 		btrfs_err(info,
83896da0919SQu Wenruo 			  "nologreplay must be used with ro mount option");
83996da0919SQu Wenruo 		ret = -EINVAL;
84096da0919SQu Wenruo 	}
841a7a3f7caSSage Weil out:
8420b246afaSJeff Mahoney 	if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
8433cdde224SJeff Mahoney 	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
8443cdde224SJeff Mahoney 	    !btrfs_test_opt(info, CLEAR_CACHE)) {
8450b246afaSJeff Mahoney 		btrfs_err(info, "cannot disable free space tree");
84670f6d82eSOmar Sandoval 		ret = -EINVAL;
84770f6d82eSOmar Sandoval 
84870f6d82eSOmar Sandoval 	}
8493cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
8500b246afaSJeff Mahoney 		btrfs_info(info, "disk space caching is enabled");
8513cdde224SJeff Mahoney 	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
8520b246afaSJeff Mahoney 		btrfs_info(info, "using free space tree");
853a7a3f7caSSage Weil 	return ret;
854edf24abeSChristoph Hellwig }
855edf24abeSChristoph Hellwig 
856edf24abeSChristoph Hellwig /*
857edf24abeSChristoph Hellwig  * Parse mount options that are required early in the mount process.
858edf24abeSChristoph Hellwig  *
859edf24abeSChristoph Hellwig  * All other options will be parsed on much later in the mount process and
860edf24abeSChristoph Hellwig  * only when we need to allocate a new super block.
861edf24abeSChristoph Hellwig  */
86297288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags,
863d7407606SMisono, Tomohiro 		void *holder, struct btrfs_fs_devices **fs_devices)
864edf24abeSChristoph Hellwig {
865edf24abeSChristoph Hellwig 	substring_t args[MAX_OPT_ARGS];
86683c8c9bdSJeff Liu 	char *device_name, *opts, *orig, *p;
867d7407606SMisono, Tomohiro 	int error = 0;
868d7407606SMisono, Tomohiro 
869d7407606SMisono, Tomohiro 	if (!options)
870d7407606SMisono, Tomohiro 		return 0;
871d7407606SMisono, Tomohiro 
872d7407606SMisono, Tomohiro 	/*
873d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because btrfs_parse_options
874d7407606SMisono, Tomohiro 	 * gets called later
875d7407606SMisono, Tomohiro 	 */
876d7407606SMisono, Tomohiro 	opts = kstrdup(options, GFP_KERNEL);
877d7407606SMisono, Tomohiro 	if (!opts)
878d7407606SMisono, Tomohiro 		return -ENOMEM;
879d7407606SMisono, Tomohiro 	orig = opts;
880d7407606SMisono, Tomohiro 
881d7407606SMisono, Tomohiro 	while ((p = strsep(&opts, ",")) != NULL) {
882d7407606SMisono, Tomohiro 		int token;
883d7407606SMisono, Tomohiro 
884d7407606SMisono, Tomohiro 		if (!*p)
885d7407606SMisono, Tomohiro 			continue;
886d7407606SMisono, Tomohiro 
887d7407606SMisono, Tomohiro 		token = match_token(p, tokens, args);
888d7407606SMisono, Tomohiro 		if (token == Opt_device) {
889d7407606SMisono, Tomohiro 			device_name = match_strdup(&args[0]);
890d7407606SMisono, Tomohiro 			if (!device_name) {
891d7407606SMisono, Tomohiro 				error = -ENOMEM;
892d7407606SMisono, Tomohiro 				goto out;
893d7407606SMisono, Tomohiro 			}
894d7407606SMisono, Tomohiro 			error = btrfs_scan_one_device(device_name,
895d7407606SMisono, Tomohiro 					flags, holder, fs_devices);
896d7407606SMisono, Tomohiro 			kfree(device_name);
897d7407606SMisono, Tomohiro 			if (error)
898d7407606SMisono, Tomohiro 				goto out;
899d7407606SMisono, Tomohiro 		}
900d7407606SMisono, Tomohiro 	}
901d7407606SMisono, Tomohiro 
902d7407606SMisono, Tomohiro out:
903d7407606SMisono, Tomohiro 	kfree(orig);
904d7407606SMisono, Tomohiro 	return error;
905d7407606SMisono, Tomohiro }
906d7407606SMisono, Tomohiro 
907d7407606SMisono, Tomohiro /*
908d7407606SMisono, Tomohiro  * Parse mount options that are related to subvolume id
909d7407606SMisono, Tomohiro  *
910d7407606SMisono, Tomohiro  * The value is later passed to mount_subvol()
911d7407606SMisono, Tomohiro  */
912d7407606SMisono, Tomohiro static int btrfs_parse_subvol_options(const char *options, fmode_t flags,
91378f6beacSMisono, Tomohiro 		char **subvol_name, u64 *subvol_objectid)
914d7407606SMisono, Tomohiro {
915d7407606SMisono, Tomohiro 	substring_t args[MAX_OPT_ARGS];
916d7407606SMisono, Tomohiro 	char *opts, *orig, *p;
917edf24abeSChristoph Hellwig 	int error = 0;
918ccb0e7d1SAnand Jain 	u64 subvolid;
919edf24abeSChristoph Hellwig 
920edf24abeSChristoph Hellwig 	if (!options)
921830c4adbSJosef Bacik 		return 0;
922edf24abeSChristoph Hellwig 
923edf24abeSChristoph Hellwig 	/*
924d7407606SMisono, Tomohiro 	 * strsep changes the string, duplicate it because
925d7407606SMisono, Tomohiro 	 * btrfs_parse_early_options gets called later
926edf24abeSChristoph Hellwig 	 */
927edf24abeSChristoph Hellwig 	opts = kstrdup(options, GFP_KERNEL);
928edf24abeSChristoph Hellwig 	if (!opts)
929edf24abeSChristoph Hellwig 		return -ENOMEM;
9303f3d0bc0STero Roponen 	orig = opts;
931edf24abeSChristoph Hellwig 
932edf24abeSChristoph Hellwig 	while ((p = strsep(&opts, ",")) != NULL) {
933edf24abeSChristoph Hellwig 		int token;
934edf24abeSChristoph Hellwig 		if (!*p)
935edf24abeSChristoph Hellwig 			continue;
936edf24abeSChristoph Hellwig 
937edf24abeSChristoph Hellwig 		token = match_token(p, tokens, args);
938edf24abeSChristoph Hellwig 		switch (token) {
939edf24abeSChristoph Hellwig 		case Opt_subvol:
940a90e8b6fSIlya Dryomov 			kfree(*subvol_name);
941edf24abeSChristoph Hellwig 			*subvol_name = match_strdup(&args[0]);
9422c334e87SWang Shilong 			if (!*subvol_name) {
9432c334e87SWang Shilong 				error = -ENOMEM;
9442c334e87SWang Shilong 				goto out;
9452c334e87SWang Shilong 			}
946edf24abeSChristoph Hellwig 			break;
94773f73415SJosef Bacik 		case Opt_subvolid:
948ccb0e7d1SAnand Jain 			error = match_u64(&args[0], &subvolid);
949ccb0e7d1SAnand Jain 			if (error)
9502c334e87SWang Shilong 				goto out;
951ccb0e7d1SAnand Jain 
952ccb0e7d1SAnand Jain 			/* we want the original fs_tree */
953ccb0e7d1SAnand Jain 			if (subvolid == 0)
954ccb0e7d1SAnand Jain 				subvolid = BTRFS_FS_TREE_OBJECTID;
955ccb0e7d1SAnand Jain 
956ccb0e7d1SAnand Jain 			*subvol_objectid = subvolid;
95773f73415SJosef Bacik 			break;
958e15d0542SXin Zhong 		case Opt_subvolrootid:
95962e85577SJeff Mahoney 			pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
960e15d0542SXin Zhong 			break;
961edf24abeSChristoph Hellwig 		default:
962edf24abeSChristoph Hellwig 			break;
963edf24abeSChristoph Hellwig 		}
964edf24abeSChristoph Hellwig 	}
965edf24abeSChristoph Hellwig 
966edf24abeSChristoph Hellwig out:
967830c4adbSJosef Bacik 	kfree(orig);
968edf24abeSChristoph Hellwig 	return error;
96995e05289SChris Mason }
97095e05289SChris Mason 
97105dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
97273f73415SJosef Bacik 					   u64 subvol_objectid)
97373f73415SJosef Bacik {
974815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
97505dbe683SOmar Sandoval 	struct btrfs_root *fs_root;
97605dbe683SOmar Sandoval 	struct btrfs_root_ref *root_ref;
97705dbe683SOmar Sandoval 	struct btrfs_inode_ref *inode_ref;
97805dbe683SOmar Sandoval 	struct btrfs_key key;
97905dbe683SOmar Sandoval 	struct btrfs_path *path = NULL;
98005dbe683SOmar Sandoval 	char *name = NULL, *ptr;
98105dbe683SOmar Sandoval 	u64 dirid;
98205dbe683SOmar Sandoval 	int len;
98305dbe683SOmar Sandoval 	int ret;
98405dbe683SOmar Sandoval 
98505dbe683SOmar Sandoval 	path = btrfs_alloc_path();
98605dbe683SOmar Sandoval 	if (!path) {
98705dbe683SOmar Sandoval 		ret = -ENOMEM;
98805dbe683SOmar Sandoval 		goto err;
98905dbe683SOmar Sandoval 	}
99005dbe683SOmar Sandoval 	path->leave_spinning = 1;
99105dbe683SOmar Sandoval 
9923ec83621SDavid Sterba 	name = kmalloc(PATH_MAX, GFP_KERNEL);
99305dbe683SOmar Sandoval 	if (!name) {
99405dbe683SOmar Sandoval 		ret = -ENOMEM;
99505dbe683SOmar Sandoval 		goto err;
99605dbe683SOmar Sandoval 	}
99705dbe683SOmar Sandoval 	ptr = name + PATH_MAX - 1;
99805dbe683SOmar Sandoval 	ptr[0] = '\0';
99905dbe683SOmar Sandoval 
100005dbe683SOmar Sandoval 	/*
100105dbe683SOmar Sandoval 	 * Walk up the subvolume trees in the tree of tree roots by root
100205dbe683SOmar Sandoval 	 * backrefs until we hit the top-level subvolume.
100305dbe683SOmar Sandoval 	 */
100405dbe683SOmar Sandoval 	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
100505dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
100605dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_BACKREF_KEY;
100705dbe683SOmar Sandoval 		key.offset = (u64)-1;
100805dbe683SOmar Sandoval 
100905dbe683SOmar Sandoval 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
101005dbe683SOmar Sandoval 		if (ret < 0) {
101105dbe683SOmar Sandoval 			goto err;
101205dbe683SOmar Sandoval 		} else if (ret > 0) {
101305dbe683SOmar Sandoval 			ret = btrfs_previous_item(root, path, subvol_objectid,
101405dbe683SOmar Sandoval 						  BTRFS_ROOT_BACKREF_KEY);
101505dbe683SOmar Sandoval 			if (ret < 0) {
101605dbe683SOmar Sandoval 				goto err;
101705dbe683SOmar Sandoval 			} else if (ret > 0) {
101805dbe683SOmar Sandoval 				ret = -ENOENT;
101905dbe683SOmar Sandoval 				goto err;
102005dbe683SOmar Sandoval 			}
102105dbe683SOmar Sandoval 		}
102205dbe683SOmar Sandoval 
102305dbe683SOmar Sandoval 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
102405dbe683SOmar Sandoval 		subvol_objectid = key.offset;
102505dbe683SOmar Sandoval 
102605dbe683SOmar Sandoval 		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
102705dbe683SOmar Sandoval 					  struct btrfs_root_ref);
102805dbe683SOmar Sandoval 		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
102905dbe683SOmar Sandoval 		ptr -= len + 1;
103005dbe683SOmar Sandoval 		if (ptr < name) {
103105dbe683SOmar Sandoval 			ret = -ENAMETOOLONG;
103205dbe683SOmar Sandoval 			goto err;
103305dbe683SOmar Sandoval 		}
103405dbe683SOmar Sandoval 		read_extent_buffer(path->nodes[0], ptr + 1,
103505dbe683SOmar Sandoval 				   (unsigned long)(root_ref + 1), len);
103605dbe683SOmar Sandoval 		ptr[0] = '/';
103705dbe683SOmar Sandoval 		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
103805dbe683SOmar Sandoval 		btrfs_release_path(path);
103905dbe683SOmar Sandoval 
104005dbe683SOmar Sandoval 		key.objectid = subvol_objectid;
104105dbe683SOmar Sandoval 		key.type = BTRFS_ROOT_ITEM_KEY;
104205dbe683SOmar Sandoval 		key.offset = (u64)-1;
104305dbe683SOmar Sandoval 		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
104405dbe683SOmar Sandoval 		if (IS_ERR(fs_root)) {
104505dbe683SOmar Sandoval 			ret = PTR_ERR(fs_root);
104605dbe683SOmar Sandoval 			goto err;
104705dbe683SOmar Sandoval 		}
104805dbe683SOmar Sandoval 
104905dbe683SOmar Sandoval 		/*
105005dbe683SOmar Sandoval 		 * Walk up the filesystem tree by inode refs until we hit the
105105dbe683SOmar Sandoval 		 * root directory.
105205dbe683SOmar Sandoval 		 */
105305dbe683SOmar Sandoval 		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
105405dbe683SOmar Sandoval 			key.objectid = dirid;
105505dbe683SOmar Sandoval 			key.type = BTRFS_INODE_REF_KEY;
105605dbe683SOmar Sandoval 			key.offset = (u64)-1;
105705dbe683SOmar Sandoval 
105805dbe683SOmar Sandoval 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
105905dbe683SOmar Sandoval 			if (ret < 0) {
106005dbe683SOmar Sandoval 				goto err;
106105dbe683SOmar Sandoval 			} else if (ret > 0) {
106205dbe683SOmar Sandoval 				ret = btrfs_previous_item(fs_root, path, dirid,
106305dbe683SOmar Sandoval 							  BTRFS_INODE_REF_KEY);
106405dbe683SOmar Sandoval 				if (ret < 0) {
106505dbe683SOmar Sandoval 					goto err;
106605dbe683SOmar Sandoval 				} else if (ret > 0) {
106705dbe683SOmar Sandoval 					ret = -ENOENT;
106805dbe683SOmar Sandoval 					goto err;
106905dbe683SOmar Sandoval 				}
107005dbe683SOmar Sandoval 			}
107105dbe683SOmar Sandoval 
107205dbe683SOmar Sandoval 			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
107305dbe683SOmar Sandoval 			dirid = key.offset;
107405dbe683SOmar Sandoval 
107505dbe683SOmar Sandoval 			inode_ref = btrfs_item_ptr(path->nodes[0],
107605dbe683SOmar Sandoval 						   path->slots[0],
107705dbe683SOmar Sandoval 						   struct btrfs_inode_ref);
107805dbe683SOmar Sandoval 			len = btrfs_inode_ref_name_len(path->nodes[0],
107905dbe683SOmar Sandoval 						       inode_ref);
108005dbe683SOmar Sandoval 			ptr -= len + 1;
108105dbe683SOmar Sandoval 			if (ptr < name) {
108205dbe683SOmar Sandoval 				ret = -ENAMETOOLONG;
108305dbe683SOmar Sandoval 				goto err;
108405dbe683SOmar Sandoval 			}
108505dbe683SOmar Sandoval 			read_extent_buffer(path->nodes[0], ptr + 1,
108605dbe683SOmar Sandoval 					   (unsigned long)(inode_ref + 1), len);
108705dbe683SOmar Sandoval 			ptr[0] = '/';
108805dbe683SOmar Sandoval 			btrfs_release_path(path);
108905dbe683SOmar Sandoval 		}
109005dbe683SOmar Sandoval 	}
109105dbe683SOmar Sandoval 
109205dbe683SOmar Sandoval 	btrfs_free_path(path);
109305dbe683SOmar Sandoval 	if (ptr == name + PATH_MAX - 1) {
109405dbe683SOmar Sandoval 		name[0] = '/';
109505dbe683SOmar Sandoval 		name[1] = '\0';
109605dbe683SOmar Sandoval 	} else {
109705dbe683SOmar Sandoval 		memmove(name, ptr, name + PATH_MAX - ptr);
109805dbe683SOmar Sandoval 	}
109905dbe683SOmar Sandoval 	return name;
110005dbe683SOmar Sandoval 
110105dbe683SOmar Sandoval err:
110205dbe683SOmar Sandoval 	btrfs_free_path(path);
110305dbe683SOmar Sandoval 	kfree(name);
110405dbe683SOmar Sandoval 	return ERR_PTR(ret);
110505dbe683SOmar Sandoval }
110605dbe683SOmar Sandoval 
110705dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
110805dbe683SOmar Sandoval {
110905dbe683SOmar Sandoval 	struct btrfs_root *root = fs_info->tree_root;
111073f73415SJosef Bacik 	struct btrfs_dir_item *di;
111173f73415SJosef Bacik 	struct btrfs_path *path;
111273f73415SJosef Bacik 	struct btrfs_key location;
111373f73415SJosef Bacik 	u64 dir_id;
111473f73415SJosef Bacik 
111573f73415SJosef Bacik 	path = btrfs_alloc_path();
111673f73415SJosef Bacik 	if (!path)
111705dbe683SOmar Sandoval 		return -ENOMEM;
111873f73415SJosef Bacik 	path->leave_spinning = 1;
111973f73415SJosef Bacik 
112073f73415SJosef Bacik 	/*
112173f73415SJosef Bacik 	 * Find the "default" dir item which points to the root item that we
112273f73415SJosef Bacik 	 * will mount by default if we haven't been given a specific subvolume
112373f73415SJosef Bacik 	 * to mount.
112473f73415SJosef Bacik 	 */
1125815745cfSAl Viro 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
112673f73415SJosef Bacik 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1127b0839166SJulia Lawall 	if (IS_ERR(di)) {
1128b0839166SJulia Lawall 		btrfs_free_path(path);
112905dbe683SOmar Sandoval 		return PTR_ERR(di);
1130b0839166SJulia Lawall 	}
113173f73415SJosef Bacik 	if (!di) {
113273f73415SJosef Bacik 		/*
113373f73415SJosef Bacik 		 * Ok the default dir item isn't there.  This is weird since
113473f73415SJosef Bacik 		 * it's always been there, but don't freak out, just try and
113505dbe683SOmar Sandoval 		 * mount the top-level subvolume.
113673f73415SJosef Bacik 		 */
113773f73415SJosef Bacik 		btrfs_free_path(path);
113805dbe683SOmar Sandoval 		*objectid = BTRFS_FS_TREE_OBJECTID;
113905dbe683SOmar Sandoval 		return 0;
114073f73415SJosef Bacik 	}
114173f73415SJosef Bacik 
114273f73415SJosef Bacik 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
114373f73415SJosef Bacik 	btrfs_free_path(path);
114405dbe683SOmar Sandoval 	*objectid = location.objectid;
114505dbe683SOmar Sandoval 	return 0;
114673f73415SJosef Bacik }
114773f73415SJosef Bacik 
11488a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb,
11498a4b83ccSChris Mason 			    struct btrfs_fs_devices *fs_devices,
115056e033a7SDavid Sterba 			    void *data)
11512e635a27SChris Mason {
11522e635a27SChris Mason 	struct inode *inode;
1153815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
11545d4f98a2SYan Zheng 	struct btrfs_key key;
115539279cc3SChris Mason 	int err;
11562e635a27SChris Mason 
11572e635a27SChris Mason 	sb->s_maxbytes = MAX_LFS_FILESIZE;
11582e635a27SChris Mason 	sb->s_magic = BTRFS_SUPER_MAGIC;
1159e20d96d6SChris Mason 	sb->s_op = &btrfs_super_ops;
1160af53d29aSAl Viro 	sb->s_d_op = &btrfs_dentry_operations;
1161be6e8dc0SBalaji Rao 	sb->s_export_op = &btrfs_export_ops;
11625103e947SJosef Bacik 	sb->s_xattr = btrfs_xattr_handlers;
11632e635a27SChris Mason 	sb->s_time_gran = 1;
11640eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL
11651751e8a6SLinus Torvalds 	sb->s_flags |= SB_POSIXACL;
116649cf6f45SChris Ball #endif
1167357fdad0SMatthew Garrett 	sb->s_flags |= SB_I_VERSION;
1168da2f0f74SChris Mason 	sb->s_iflags |= SB_I_CGROUPWB;
11699e11ceeeSJan Kara 
11709e11ceeeSJan Kara 	err = super_setup_bdi(sb);
11719e11ceeeSJan Kara 	if (err) {
11729e11ceeeSJan Kara 		btrfs_err(fs_info, "super_setup_bdi failed");
11739e11ceeeSJan Kara 		return err;
11749e11ceeeSJan Kara 	}
11759e11ceeeSJan Kara 
1176ad2b2c80SAl Viro 	err = open_ctree(sb, fs_devices, (char *)data);
1177ad2b2c80SAl Viro 	if (err) {
1178ab8d0fc4SJeff Mahoney 		btrfs_err(fs_info, "open_ctree failed");
1179ad2b2c80SAl Viro 		return err;
1180e20d96d6SChris Mason 	}
1181b888db2bSChris Mason 
11825d4f98a2SYan Zheng 	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
11835d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
11845d4f98a2SYan Zheng 	key.offset = 0;
118598c7089cSAl Viro 	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
11865d4f98a2SYan Zheng 	if (IS_ERR(inode)) {
11875d4f98a2SYan Zheng 		err = PTR_ERR(inode);
118839279cc3SChris Mason 		goto fail_close;
118939279cc3SChris Mason 	}
11902e635a27SChris Mason 
119148fde701SAl Viro 	sb->s_root = d_make_root(inode);
119248fde701SAl Viro 	if (!sb->s_root) {
119339279cc3SChris Mason 		err = -ENOMEM;
119439279cc3SChris Mason 		goto fail_close;
11952e635a27SChris Mason 	}
119658176a96SJosef Bacik 
119790a887c9SDan Magenheimer 	cleancache_init_fs(sb);
11981751e8a6SLinus Torvalds 	sb->s_flags |= SB_ACTIVE;
11992e635a27SChris Mason 	return 0;
12002e635a27SChris Mason 
120139279cc3SChris Mason fail_close:
12026bccf3abSJeff Mahoney 	close_ctree(fs_info);
1203d5719762SChris Mason 	return err;
1204d5719762SChris Mason }
1205d5719762SChris Mason 
12066bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait)
1207d5719762SChris Mason {
1208d5719762SChris Mason 	struct btrfs_trans_handle *trans;
1209815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1210815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
1211df2ce34cSChris Mason 
1212bc074524SJeff Mahoney 	trace_btrfs_sync_fs(fs_info, wait);
12131abe9b8aSliubo 
1214d561c025SChris Mason 	if (!wait) {
1215815745cfSAl Viro 		filemap_flush(fs_info->btree_inode->i_mapping);
1216df2ce34cSChris Mason 		return 0;
1217d561c025SChris Mason 	}
1218771ed689SChris Mason 
12196374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1220771ed689SChris Mason 
1221d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
122260376ce4SJosef Bacik 	if (IS_ERR(trans)) {
1223354aa0fbSMiao Xie 		/* no transaction, don't bother */
12246b5fe46dSDavid Sterba 		if (PTR_ERR(trans) == -ENOENT) {
12256b5fe46dSDavid Sterba 			/*
12266b5fe46dSDavid Sterba 			 * Exit unless we have some pending changes
12276b5fe46dSDavid Sterba 			 * that need to go through commit
12286b5fe46dSDavid Sterba 			 */
12296b5fe46dSDavid Sterba 			if (fs_info->pending_changes == 0)
1230bd7de2c9SJosef Bacik 				return 0;
1231a53f4f8eSQu Wenruo 			/*
1232a53f4f8eSQu Wenruo 			 * A non-blocking test if the fs is frozen. We must not
1233a53f4f8eSQu Wenruo 			 * start a new transaction here otherwise a deadlock
1234a53f4f8eSQu Wenruo 			 * happens. The pending operations are delayed to the
1235a53f4f8eSQu Wenruo 			 * next commit after thawing.
1236a53f4f8eSQu Wenruo 			 */
1237a7e3c5f2SRakesh Pandit 			if (sb_start_write_trylock(sb))
1238a7e3c5f2SRakesh Pandit 				sb_end_write(sb);
1239a53f4f8eSQu Wenruo 			else
1240a53f4f8eSQu Wenruo 				return 0;
12416b5fe46dSDavid Sterba 			trans = btrfs_start_transaction(root, 0);
124260376ce4SJosef Bacik 		}
124398bd5c54SDavid Sterba 		if (IS_ERR(trans))
124498bd5c54SDavid Sterba 			return PTR_ERR(trans);
12456b5fe46dSDavid Sterba 	}
12463a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
1247d5719762SChris Mason }
1248d5719762SChris Mason 
124934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1250a9572a15SEric Paris {
1251815745cfSAl Viro 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
12520f628c63SDavid Sterba 	const char *compress_type;
1253a9572a15SEric Paris 
12543cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DEGRADED))
1255a9572a15SEric Paris 		seq_puts(seq, ",degraded");
12563cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATASUM))
1257a9572a15SEric Paris 		seq_puts(seq, ",nodatasum");
12583cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NODATACOW))
1259a9572a15SEric Paris 		seq_puts(seq, ",nodatacow");
12603cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOBARRIER))
1261a9572a15SEric Paris 		seq_puts(seq, ",nobarrier");
126295ac567aSFilipe David Borba Manana 	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1263c1c9ff7cSGeert Uytterhoeven 		seq_printf(seq, ",max_inline=%llu", info->max_inline);
1264a9572a15SEric Paris 	if (info->thread_pool_size !=  min_t(unsigned long,
1265a9572a15SEric Paris 					     num_online_cpus() + 2, 8))
1266f7b885beSAnand Jain 		seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
12673cdde224SJeff Mahoney 	if (btrfs_test_opt(info, COMPRESS)) {
12680f628c63SDavid Sterba 		compress_type = btrfs_compress_type2str(info->compress_type);
12693cdde224SJeff Mahoney 		if (btrfs_test_opt(info, FORCE_COMPRESS))
1270200da64eSTsutomu Itoh 			seq_printf(seq, ",compress-force=%s", compress_type);
1271200da64eSTsutomu Itoh 		else
1272200da64eSTsutomu Itoh 			seq_printf(seq, ",compress=%s", compress_type);
1273f51d2b59SDavid Sterba 		if (info->compress_level)
1274fa4d885aSAdam Borowski 			seq_printf(seq, ":%d", info->compress_level);
1275200da64eSTsutomu Itoh 	}
12763cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOSSD))
1277c289811cSChris Mason 		seq_puts(seq, ",nossd");
12783cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SSD_SPREAD))
1279451d7585SChris Mason 		seq_puts(seq, ",ssd_spread");
12803cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, SSD))
1281a9572a15SEric Paris 		seq_puts(seq, ",ssd");
12823cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOTREELOG))
12836b65c5c6SSage Weil 		seq_puts(seq, ",notreelog");
12843cdde224SJeff Mahoney 	if (btrfs_test_opt(info, NOLOGREPLAY))
128596da0919SQu Wenruo 		seq_puts(seq, ",nologreplay");
12863cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FLUSHONCOMMIT))
12876b65c5c6SSage Weil 		seq_puts(seq, ",flushoncommit");
12883cdde224SJeff Mahoney 	if (btrfs_test_opt(info, DISCARD))
128920a5239aSMatthew Wilcox 		seq_puts(seq, ",discard");
12901751e8a6SLinus Torvalds 	if (!(info->sb->s_flags & SB_POSIXACL))
1291a9572a15SEric Paris 		seq_puts(seq, ",noacl");
12923cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SPACE_CACHE))
1293200da64eSTsutomu Itoh 		seq_puts(seq, ",space_cache");
12943cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
129570f6d82eSOmar Sandoval 		seq_puts(seq, ",space_cache=v2");
129673bc1876SJosef Bacik 	else
12978965593eSDavid Sterba 		seq_puts(seq, ",nospace_cache");
12983cdde224SJeff Mahoney 	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1299f420ee1eSStefan Behrens 		seq_puts(seq, ",rescan_uuid_tree");
13003cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CLEAR_CACHE))
1301200da64eSTsutomu Itoh 		seq_puts(seq, ",clear_cache");
13023cdde224SJeff Mahoney 	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1303200da64eSTsutomu Itoh 		seq_puts(seq, ",user_subvol_rm_allowed");
13043cdde224SJeff Mahoney 	if (btrfs_test_opt(info, ENOSPC_DEBUG))
13050942caa3SDavid Sterba 		seq_puts(seq, ",enospc_debug");
13063cdde224SJeff Mahoney 	if (btrfs_test_opt(info, AUTO_DEFRAG))
13070942caa3SDavid Sterba 		seq_puts(seq, ",autodefrag");
13083cdde224SJeff Mahoney 	if (btrfs_test_opt(info, INODE_MAP_CACHE))
13090942caa3SDavid Sterba 		seq_puts(seq, ",inode_cache");
13103cdde224SJeff Mahoney 	if (btrfs_test_opt(info, SKIP_BALANCE))
13119555c6c1SIlya Dryomov 		seq_puts(seq, ",skip_balance");
13128507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
13133cdde224SJeff Mahoney 	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
13148507d216SWang Shilong 		seq_puts(seq, ",check_int_data");
13153cdde224SJeff Mahoney 	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
13168507d216SWang Shilong 		seq_puts(seq, ",check_int");
13178507d216SWang Shilong 	if (info->check_integrity_print_mask)
13188507d216SWang Shilong 		seq_printf(seq, ",check_int_print_mask=%d",
13198507d216SWang Shilong 				info->check_integrity_print_mask);
13208507d216SWang Shilong #endif
13218507d216SWang Shilong 	if (info->metadata_ratio)
1322764cb8b4SAnand Jain 		seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
13233cdde224SJeff Mahoney 	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
13248c342930SJeff Mahoney 		seq_puts(seq, ",fatal_errors=panic");
13258b87dc17SDavid Sterba 	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1326d3740608SAnand Jain 		seq_printf(seq, ",commit=%u", info->commit_interval);
1327d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG
13283cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_DATA))
1329d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=data");
13303cdde224SJeff Mahoney 	if (btrfs_test_opt(info, FRAGMENT_METADATA))
1331d0bd4560SJosef Bacik 		seq_puts(seq, ",fragment=metadata");
1332d0bd4560SJosef Bacik #endif
1333fb592373SJosef Bacik 	if (btrfs_test_opt(info, REF_VERIFY))
1334fb592373SJosef Bacik 		seq_puts(seq, ",ref_verify");
1335c8d3fe02SOmar Sandoval 	seq_printf(seq, ",subvolid=%llu",
1336c8d3fe02SOmar Sandoval 		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1337c8d3fe02SOmar Sandoval 	seq_puts(seq, ",subvol=");
1338c8d3fe02SOmar Sandoval 	seq_dentry(seq, dentry, " \t\n\\");
1339a9572a15SEric Paris 	return 0;
1340a9572a15SEric Paris }
1341a9572a15SEric Paris 
1342a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data)
13432e635a27SChris Mason {
1344815745cfSAl Viro 	struct btrfs_fs_info *p = data;
1345815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(s);
13464b82d6e4SYan 
1347815745cfSAl Viro 	return fs_info->fs_devices == p->fs_devices;
13484b82d6e4SYan }
13494b82d6e4SYan 
1350450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data)
1351450ba0eaSJosef Bacik {
13526de1d09dSAl Viro 	int err = set_anon_super(s, data);
13536de1d09dSAl Viro 	if (!err)
1354450ba0eaSJosef Bacik 		s->s_fs_info = data;
13556de1d09dSAl Viro 	return err;
1356450ba0eaSJosef Bacik }
1357450ba0eaSJosef Bacik 
1358830c4adbSJosef Bacik /*
1359f9d9ef62SDavid Sterba  * subvolumes are identified by ino 256
1360f9d9ef62SDavid Sterba  */
1361f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode)
1362f9d9ef62SDavid Sterba {
1363f9d9ef62SDavid Sterba 	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1364f9d9ef62SDavid Sterba 		return 1;
1365f9d9ef62SDavid Sterba 	return 0;
1366f9d9ef62SDavid Sterba }
1367f9d9ef62SDavid Sterba 
1368bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
13695bedc48aSDavid Sterba 				   const char *device_name, struct vfsmount *mnt)
1370830c4adbSJosef Bacik {
1371830c4adbSJosef Bacik 	struct dentry *root;
1372fa330659SOmar Sandoval 	int ret;
1373830c4adbSJosef Bacik 
137405dbe683SOmar Sandoval 	if (!subvol_name) {
137505dbe683SOmar Sandoval 		if (!subvol_objectid) {
137605dbe683SOmar Sandoval 			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
137705dbe683SOmar Sandoval 							  &subvol_objectid);
137805dbe683SOmar Sandoval 			if (ret) {
137905dbe683SOmar Sandoval 				root = ERR_PTR(ret);
138005dbe683SOmar Sandoval 				goto out;
138105dbe683SOmar Sandoval 			}
138205dbe683SOmar Sandoval 		}
138305dbe683SOmar Sandoval 		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
138405dbe683SOmar Sandoval 							    subvol_objectid);
138505dbe683SOmar Sandoval 		if (IS_ERR(subvol_name)) {
138605dbe683SOmar Sandoval 			root = ERR_CAST(subvol_name);
138705dbe683SOmar Sandoval 			subvol_name = NULL;
138805dbe683SOmar Sandoval 			goto out;
138905dbe683SOmar Sandoval 		}
139005dbe683SOmar Sandoval 
139105dbe683SOmar Sandoval 	}
139205dbe683SOmar Sandoval 
1393ea441d11SAl Viro 	root = mount_subtree(mnt, subvol_name);
1394fa330659SOmar Sandoval 	/* mount_subtree() drops our reference on the vfsmount. */
1395fa330659SOmar Sandoval 	mnt = NULL;
1396830c4adbSJosef Bacik 
1397bb289b7bSOmar Sandoval 	if (!IS_ERR(root)) {
1398ea441d11SAl Viro 		struct super_block *s = root->d_sb;
1399ab8d0fc4SJeff Mahoney 		struct btrfs_fs_info *fs_info = btrfs_sb(s);
1400bb289b7bSOmar Sandoval 		struct inode *root_inode = d_inode(root);
1401bb289b7bSOmar Sandoval 		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1402bb289b7bSOmar Sandoval 
1403bb289b7bSOmar Sandoval 		ret = 0;
1404bb289b7bSOmar Sandoval 		if (!is_subvolume_inode(root_inode)) {
1405ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info, "'%s' is not a valid subvolume",
1406bb289b7bSOmar Sandoval 			       subvol_name);
1407bb289b7bSOmar Sandoval 			ret = -EINVAL;
1408bb289b7bSOmar Sandoval 		}
1409bb289b7bSOmar Sandoval 		if (subvol_objectid && root_objectid != subvol_objectid) {
141005dbe683SOmar Sandoval 			/*
141105dbe683SOmar Sandoval 			 * This will also catch a race condition where a
141205dbe683SOmar Sandoval 			 * subvolume which was passed by ID is renamed and
141305dbe683SOmar Sandoval 			 * another subvolume is renamed over the old location.
141405dbe683SOmar Sandoval 			 */
1415ab8d0fc4SJeff Mahoney 			btrfs_err(fs_info,
1416ab8d0fc4SJeff Mahoney 				  "subvol '%s' does not match subvolid %llu",
1417bb289b7bSOmar Sandoval 				  subvol_name, subvol_objectid);
1418bb289b7bSOmar Sandoval 			ret = -EINVAL;
1419bb289b7bSOmar Sandoval 		}
1420bb289b7bSOmar Sandoval 		if (ret) {
1421ea441d11SAl Viro 			dput(root);
1422bb289b7bSOmar Sandoval 			root = ERR_PTR(ret);
1423ea441d11SAl Viro 			deactivate_locked_super(s);
1424bb289b7bSOmar Sandoval 		}
1425f9d9ef62SDavid Sterba 	}
1426f9d9ef62SDavid Sterba 
1427fa330659SOmar Sandoval out:
1428fa330659SOmar Sandoval 	mntput(mnt);
1429fa330659SOmar Sandoval 	kfree(subvol_name);
1430830c4adbSJosef Bacik 	return root;
1431830c4adbSJosef Bacik }
1432450ba0eaSJosef Bacik 
1433f667aef6SQu Wenruo static int parse_security_options(char *orig_opts,
1434f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1435f667aef6SQu Wenruo {
1436f667aef6SQu Wenruo 	char *secdata = NULL;
1437f667aef6SQu Wenruo 	int ret = 0;
1438f667aef6SQu Wenruo 
1439f667aef6SQu Wenruo 	secdata = alloc_secdata();
1440f667aef6SQu Wenruo 	if (!secdata)
1441f667aef6SQu Wenruo 		return -ENOMEM;
1442f667aef6SQu Wenruo 	ret = security_sb_copy_data(orig_opts, secdata);
1443f667aef6SQu Wenruo 	if (ret) {
1444f667aef6SQu Wenruo 		free_secdata(secdata);
1445f667aef6SQu Wenruo 		return ret;
1446f667aef6SQu Wenruo 	}
1447f667aef6SQu Wenruo 	ret = security_sb_parse_opts_str(secdata, sec_opts);
1448f667aef6SQu Wenruo 	free_secdata(secdata);
1449f667aef6SQu Wenruo 	return ret;
1450f667aef6SQu Wenruo }
1451f667aef6SQu Wenruo 
1452f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info,
1453f667aef6SQu Wenruo 				  struct super_block *sb,
1454f667aef6SQu Wenruo 				  struct security_mnt_opts *sec_opts)
1455f667aef6SQu Wenruo {
1456f667aef6SQu Wenruo 	int ret = 0;
1457f667aef6SQu Wenruo 
1458f667aef6SQu Wenruo 	/*
1459f667aef6SQu Wenruo 	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
1460f667aef6SQu Wenruo 	 * is valid.
1461f667aef6SQu Wenruo 	 */
1462f667aef6SQu Wenruo 	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
1463f667aef6SQu Wenruo 	if (ret)
1464f667aef6SQu Wenruo 		return ret;
1465f667aef6SQu Wenruo 
1466a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY
1467f667aef6SQu Wenruo 	if (!fs_info->security_opts.num_mnt_opts) {
1468f667aef6SQu Wenruo 		/* first time security setup, copy sec_opts to fs_info */
1469f667aef6SQu Wenruo 		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
1470f667aef6SQu Wenruo 	} else {
1471f667aef6SQu Wenruo 		/*
1472180e4d47SLuis de Bethencourt 		 * Since SELinux (the only one supporting security_mnt_opts)
1473180e4d47SLuis de Bethencourt 		 * does NOT support changing context during remount/mount of
1474180e4d47SLuis de Bethencourt 		 * the same sb, this must be the same or part of the same
1475180e4d47SLuis de Bethencourt 		 * security options, just free it.
1476f667aef6SQu Wenruo 		 */
1477f667aef6SQu Wenruo 		security_free_mnt_opts(sec_opts);
1478f667aef6SQu Wenruo 	}
1479a43bb39bSQu Wenruo #endif
1480f667aef6SQu Wenruo 	return ret;
1481f667aef6SQu Wenruo }
1482f667aef6SQu Wenruo 
1483312c89fbSMisono, Tomohiro /*
1484312c89fbSMisono, Tomohiro  * Find a superblock for the given device / mount point.
1485312c89fbSMisono, Tomohiro  *
1486312c89fbSMisono, Tomohiro  * Note: This is based on mount_bdev from fs/super.c with a few additions
1487312c89fbSMisono, Tomohiro  *       for multiple device setup.  Make sure to keep it in sync.
1488312c89fbSMisono, Tomohiro  */
148972fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
149072fa39f5SMisono, Tomohiro 		int flags, const char *device_name, void *data)
149172fa39f5SMisono, Tomohiro {
149272fa39f5SMisono, Tomohiro 	struct block_device *bdev = NULL;
149372fa39f5SMisono, Tomohiro 	struct super_block *s;
149472fa39f5SMisono, Tomohiro 	struct btrfs_fs_devices *fs_devices = NULL;
149572fa39f5SMisono, Tomohiro 	struct btrfs_fs_info *fs_info = NULL;
149672fa39f5SMisono, Tomohiro 	struct security_mnt_opts new_sec_opts;
149772fa39f5SMisono, Tomohiro 	fmode_t mode = FMODE_READ;
149872fa39f5SMisono, Tomohiro 	int error = 0;
149972fa39f5SMisono, Tomohiro 
150072fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY))
150172fa39f5SMisono, Tomohiro 		mode |= FMODE_WRITE;
150272fa39f5SMisono, Tomohiro 
150372fa39f5SMisono, Tomohiro 	error = btrfs_parse_early_options(data, mode, fs_type,
150472fa39f5SMisono, Tomohiro 					  &fs_devices);
150572fa39f5SMisono, Tomohiro 	if (error) {
150672fa39f5SMisono, Tomohiro 		return ERR_PTR(error);
150772fa39f5SMisono, Tomohiro 	}
150872fa39f5SMisono, Tomohiro 
150972fa39f5SMisono, Tomohiro 	security_init_mnt_opts(&new_sec_opts);
151072fa39f5SMisono, Tomohiro 	if (data) {
151172fa39f5SMisono, Tomohiro 		error = parse_security_options(data, &new_sec_opts);
151272fa39f5SMisono, Tomohiro 		if (error)
151372fa39f5SMisono, Tomohiro 			return ERR_PTR(error);
151472fa39f5SMisono, Tomohiro 	}
151572fa39f5SMisono, Tomohiro 
151672fa39f5SMisono, Tomohiro 	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
151772fa39f5SMisono, Tomohiro 	if (error)
151872fa39f5SMisono, Tomohiro 		goto error_sec_opts;
151972fa39f5SMisono, Tomohiro 
152072fa39f5SMisono, Tomohiro 	/*
152172fa39f5SMisono, Tomohiro 	 * Setup a dummy root and fs_info for test/set super.  This is because
152272fa39f5SMisono, Tomohiro 	 * we don't actually fill this stuff out until open_ctree, but we need
152372fa39f5SMisono, Tomohiro 	 * it for searching for existing supers, so this lets us do that and
152472fa39f5SMisono, Tomohiro 	 * then open_ctree will properly initialize everything later.
152572fa39f5SMisono, Tomohiro 	 */
1526a8fd1f71SJeff Mahoney 	fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
152772fa39f5SMisono, Tomohiro 	if (!fs_info) {
152872fa39f5SMisono, Tomohiro 		error = -ENOMEM;
152972fa39f5SMisono, Tomohiro 		goto error_sec_opts;
153072fa39f5SMisono, Tomohiro 	}
153172fa39f5SMisono, Tomohiro 
153272fa39f5SMisono, Tomohiro 	fs_info->fs_devices = fs_devices;
153372fa39f5SMisono, Tomohiro 
153472fa39f5SMisono, Tomohiro 	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153572fa39f5SMisono, Tomohiro 	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
153672fa39f5SMisono, Tomohiro 	security_init_mnt_opts(&fs_info->security_opts);
153772fa39f5SMisono, Tomohiro 	if (!fs_info->super_copy || !fs_info->super_for_commit) {
153872fa39f5SMisono, Tomohiro 		error = -ENOMEM;
153972fa39f5SMisono, Tomohiro 		goto error_fs_info;
154072fa39f5SMisono, Tomohiro 	}
154172fa39f5SMisono, Tomohiro 
154272fa39f5SMisono, Tomohiro 	error = btrfs_open_devices(fs_devices, mode, fs_type);
154372fa39f5SMisono, Tomohiro 	if (error)
154472fa39f5SMisono, Tomohiro 		goto error_fs_info;
154572fa39f5SMisono, Tomohiro 
154672fa39f5SMisono, Tomohiro 	if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
154772fa39f5SMisono, Tomohiro 		error = -EACCES;
154872fa39f5SMisono, Tomohiro 		goto error_close_devices;
154972fa39f5SMisono, Tomohiro 	}
155072fa39f5SMisono, Tomohiro 
155172fa39f5SMisono, Tomohiro 	bdev = fs_devices->latest_bdev;
155272fa39f5SMisono, Tomohiro 	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
155372fa39f5SMisono, Tomohiro 		 fs_info);
155472fa39f5SMisono, Tomohiro 	if (IS_ERR(s)) {
155572fa39f5SMisono, Tomohiro 		error = PTR_ERR(s);
155672fa39f5SMisono, Tomohiro 		goto error_close_devices;
155772fa39f5SMisono, Tomohiro 	}
155872fa39f5SMisono, Tomohiro 
155972fa39f5SMisono, Tomohiro 	if (s->s_root) {
156072fa39f5SMisono, Tomohiro 		btrfs_close_devices(fs_devices);
156172fa39f5SMisono, Tomohiro 		free_fs_info(fs_info);
156272fa39f5SMisono, Tomohiro 		if ((flags ^ s->s_flags) & SB_RDONLY)
156372fa39f5SMisono, Tomohiro 			error = -EBUSY;
156472fa39f5SMisono, Tomohiro 	} else {
156572fa39f5SMisono, Tomohiro 		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
156672fa39f5SMisono, Tomohiro 		btrfs_sb(s)->bdev_holder = fs_type;
156772fa39f5SMisono, Tomohiro 		error = btrfs_fill_super(s, fs_devices, data);
156872fa39f5SMisono, Tomohiro 	}
156972fa39f5SMisono, Tomohiro 	if (error) {
157072fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
157172fa39f5SMisono, Tomohiro 		goto error_sec_opts;
157272fa39f5SMisono, Tomohiro 	}
157372fa39f5SMisono, Tomohiro 
157472fa39f5SMisono, Tomohiro 	fs_info = btrfs_sb(s);
157572fa39f5SMisono, Tomohiro 	error = setup_security_options(fs_info, s, &new_sec_opts);
157672fa39f5SMisono, Tomohiro 	if (error) {
157772fa39f5SMisono, Tomohiro 		deactivate_locked_super(s);
157872fa39f5SMisono, Tomohiro 		goto error_sec_opts;
157972fa39f5SMisono, Tomohiro 	}
158072fa39f5SMisono, Tomohiro 
158172fa39f5SMisono, Tomohiro 	return dget(s->s_root);
158272fa39f5SMisono, Tomohiro 
158372fa39f5SMisono, Tomohiro error_close_devices:
158472fa39f5SMisono, Tomohiro 	btrfs_close_devices(fs_devices);
158572fa39f5SMisono, Tomohiro error_fs_info:
158672fa39f5SMisono, Tomohiro 	free_fs_info(fs_info);
158772fa39f5SMisono, Tomohiro error_sec_opts:
158872fa39f5SMisono, Tomohiro 	security_free_mnt_opts(&new_sec_opts);
158972fa39f5SMisono, Tomohiro 	return ERR_PTR(error);
159072fa39f5SMisono, Tomohiro }
1591312c89fbSMisono, Tomohiro 
1592edf24abeSChristoph Hellwig /*
1593312c89fbSMisono, Tomohiro  * Mount function which is called by VFS layer.
1594edf24abeSChristoph Hellwig  *
1595312c89fbSMisono, Tomohiro  * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1596312c89fbSMisono, Tomohiro  * which needs vfsmount* of device's root (/).  This means device's root has to
1597312c89fbSMisono, Tomohiro  * be mounted internally in any case.
1598312c89fbSMisono, Tomohiro  *
1599312c89fbSMisono, Tomohiro  * Operation flow:
1600312c89fbSMisono, Tomohiro  *   1. Parse subvol id related options for later use in mount_subvol().
1601312c89fbSMisono, Tomohiro  *
1602312c89fbSMisono, Tomohiro  *   2. Mount device's root (/) by calling vfs_kern_mount().
1603312c89fbSMisono, Tomohiro  *
1604312c89fbSMisono, Tomohiro  *      NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1605312c89fbSMisono, Tomohiro  *      first place. In order to avoid calling btrfs_mount() again, we use
1606312c89fbSMisono, Tomohiro  *      different file_system_type which is not registered to VFS by
1607312c89fbSMisono, Tomohiro  *      register_filesystem() (btrfs_root_fs_type). As a result,
1608312c89fbSMisono, Tomohiro  *      btrfs_mount_root() is called. The return value will be used by
1609312c89fbSMisono, Tomohiro  *      mount_subtree() in mount_subvol().
1610312c89fbSMisono, Tomohiro  *
1611312c89fbSMisono, Tomohiro  *   3. Call mount_subvol() to get the dentry of subvolume. Since there is
1612312c89fbSMisono, Tomohiro  *      "btrfs subvolume set-default", mount_subvol() is called always.
1613edf24abeSChristoph Hellwig  */
1614061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1615306e16ceSDavid Sterba 		const char *device_name, void *data)
16164b82d6e4SYan {
1617312c89fbSMisono, Tomohiro 	struct vfsmount *mnt_root;
1618312c89fbSMisono, Tomohiro 	struct dentry *root;
161997288f2cSChristoph Hellwig 	fmode_t mode = FMODE_READ;
162073f73415SJosef Bacik 	char *subvol_name = NULL;
162173f73415SJosef Bacik 	u64 subvol_objectid = 0;
16224b82d6e4SYan 	int error = 0;
16234b82d6e4SYan 
16241751e8a6SLinus Torvalds 	if (!(flags & SB_RDONLY))
162597288f2cSChristoph Hellwig 		mode |= FMODE_WRITE;
162697288f2cSChristoph Hellwig 
162778f6beacSMisono, Tomohiro 	error = btrfs_parse_subvol_options(data, mode,
1628d7407606SMisono, Tomohiro 					  &subvol_name, &subvol_objectid);
1629f23c8af8SIlya Dryomov 	if (error) {
1630f23c8af8SIlya Dryomov 		kfree(subvol_name);
1631061dbc6bSAl Viro 		return ERR_PTR(error);
1632f23c8af8SIlya Dryomov 	}
1633edf24abeSChristoph Hellwig 
1634312c89fbSMisono, Tomohiro 	/* mount device's root (/) */
1635312c89fbSMisono, Tomohiro 	mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1636312c89fbSMisono, Tomohiro 	if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1637312c89fbSMisono, Tomohiro 		if (flags & SB_RDONLY) {
1638312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1639312c89fbSMisono, Tomohiro 				flags & ~SB_RDONLY, device_name, data);
16404b82d6e4SYan 		} else {
1641312c89fbSMisono, Tomohiro 			mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1642312c89fbSMisono, Tomohiro 				flags | SB_RDONLY, device_name, data);
1643312c89fbSMisono, Tomohiro 			if (IS_ERR(mnt_root)) {
1644312c89fbSMisono, Tomohiro 				root = ERR_CAST(mnt_root);
1645312c89fbSMisono, Tomohiro 				goto out;
1646f667aef6SQu Wenruo 			}
1647f667aef6SQu Wenruo 
1648312c89fbSMisono, Tomohiro 			down_write(&mnt_root->mnt_sb->s_umount);
1649312c89fbSMisono, Tomohiro 			error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1650312c89fbSMisono, Tomohiro 			up_write(&mnt_root->mnt_sb->s_umount);
1651312c89fbSMisono, Tomohiro 			if (error < 0) {
1652312c89fbSMisono, Tomohiro 				root = ERR_PTR(error);
1653312c89fbSMisono, Tomohiro 				mntput(mnt_root);
1654312c89fbSMisono, Tomohiro 				goto out;
1655312c89fbSMisono, Tomohiro 			}
1656312c89fbSMisono, Tomohiro 		}
1657312c89fbSMisono, Tomohiro 	}
1658312c89fbSMisono, Tomohiro 	if (IS_ERR(mnt_root)) {
1659312c89fbSMisono, Tomohiro 		root = ERR_CAST(mnt_root);
1660312c89fbSMisono, Tomohiro 		goto out;
1661f667aef6SQu Wenruo 	}
16624b82d6e4SYan 
1663312c89fbSMisono, Tomohiro 	/* mount_subvol() will free subvol_name and mnt_root */
16645bedc48aSDavid Sterba 	root = mount_subvol(subvol_name, subvol_objectid, device_name, mnt_root);
16654b82d6e4SYan 
1666312c89fbSMisono, Tomohiro out:
1667312c89fbSMisono, Tomohiro 	return root;
16684b82d6e4SYan }
16692e635a27SChris Mason 
16700d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1671f7b885beSAnand Jain 				     u32 new_pool_size, u32 old_pool_size)
16720d2450abSSergei Trofimovich {
16730d2450abSSergei Trofimovich 	if (new_pool_size == old_pool_size)
16740d2450abSSergei Trofimovich 		return;
16750d2450abSSergei Trofimovich 
16760d2450abSSergei Trofimovich 	fs_info->thread_pool_size = new_pool_size;
16770d2450abSSergei Trofimovich 
1678efe120a0SFrank Holton 	btrfs_info(fs_info, "resize thread pool %d -> %d",
16790d2450abSSergei Trofimovich 	       old_pool_size, new_pool_size);
16800d2450abSSergei Trofimovich 
16815cdc7ad3SQu Wenruo 	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1682afe3d242SQu Wenruo 	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1683a8c93d4eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1684e66f0bb1SQu Wenruo 	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1685fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1686fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1687fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1688fccb5d86SQu Wenruo 				new_pool_size);
1689fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1690fccb5d86SQu Wenruo 	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
16915b3bc44eSQu Wenruo 	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1692736cfa15SQu Wenruo 	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
16930339ef2fSQu Wenruo 	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1694ff023aacSStefan Behrens 				new_pool_size);
16950d2450abSSergei Trofimovich }
16960d2450abSSergei Trofimovich 
1697f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1698dc81cdc5SMiao Xie {
1699dc81cdc5SMiao Xie 	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1700f42a34b2SMiao Xie }
1701dc81cdc5SMiao Xie 
1702f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1703f42a34b2SMiao Xie 				       unsigned long old_opts, int flags)
1704f42a34b2SMiao Xie {
1705dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1706dc81cdc5SMiao Xie 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
17071751e8a6SLinus Torvalds 	     (flags & SB_RDONLY))) {
1708dc81cdc5SMiao Xie 		/* wait for any defraggers to finish */
1709dc81cdc5SMiao Xie 		wait_event(fs_info->transaction_wait,
1710dc81cdc5SMiao Xie 			   (atomic_read(&fs_info->defrag_running) == 0));
17111751e8a6SLinus Torvalds 		if (flags & SB_RDONLY)
1712dc81cdc5SMiao Xie 			sync_filesystem(fs_info->sb);
1713dc81cdc5SMiao Xie 	}
1714dc81cdc5SMiao Xie }
1715dc81cdc5SMiao Xie 
1716dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1717dc81cdc5SMiao Xie 					 unsigned long old_opts)
1718dc81cdc5SMiao Xie {
1719dc81cdc5SMiao Xie 	/*
1720180e4d47SLuis de Bethencourt 	 * We need to cleanup all defragable inodes if the autodefragment is
1721180e4d47SLuis de Bethencourt 	 * close or the filesystem is read only.
1722dc81cdc5SMiao Xie 	 */
1723dc81cdc5SMiao Xie 	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1724bc98a42cSDavid Howells 	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1725dc81cdc5SMiao Xie 		btrfs_cleanup_defrag_inodes(fs_info);
1726dc81cdc5SMiao Xie 	}
1727dc81cdc5SMiao Xie 
1728dc81cdc5SMiao Xie 	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1729dc81cdc5SMiao Xie }
1730dc81cdc5SMiao Xie 
1731c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1732c146afadSYan Zheng {
1733815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1734815745cfSAl Viro 	struct btrfs_root *root = fs_info->tree_root;
173549b25e05SJeff Mahoney 	unsigned old_flags = sb->s_flags;
173649b25e05SJeff Mahoney 	unsigned long old_opts = fs_info->mount_opt;
173749b25e05SJeff Mahoney 	unsigned long old_compress_type = fs_info->compress_type;
173849b25e05SJeff Mahoney 	u64 old_max_inline = fs_info->max_inline;
1739f7b885beSAnand Jain 	u32 old_thread_pool_size = fs_info->thread_pool_size;
174049b25e05SJeff Mahoney 	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1741c146afadSYan Zheng 	int ret;
1742c146afadSYan Zheng 
174302b9984dSTheodore Ts'o 	sync_filesystem(sb);
1744f42a34b2SMiao Xie 	btrfs_remount_prepare(fs_info);
1745dc81cdc5SMiao Xie 
1746f667aef6SQu Wenruo 	if (data) {
1747f667aef6SQu Wenruo 		struct security_mnt_opts new_sec_opts;
1748f667aef6SQu Wenruo 
1749f667aef6SQu Wenruo 		security_init_mnt_opts(&new_sec_opts);
1750f667aef6SQu Wenruo 		ret = parse_security_options(data, &new_sec_opts);
1751f667aef6SQu Wenruo 		if (ret)
1752f667aef6SQu Wenruo 			goto restore;
1753f667aef6SQu Wenruo 		ret = setup_security_options(fs_info, sb,
1754f667aef6SQu Wenruo 					     &new_sec_opts);
1755f667aef6SQu Wenruo 		if (ret) {
1756f667aef6SQu Wenruo 			security_free_mnt_opts(&new_sec_opts);
1757f667aef6SQu Wenruo 			goto restore;
1758f667aef6SQu Wenruo 		}
1759f667aef6SQu Wenruo 	}
1760f667aef6SQu Wenruo 
17612ff7e61eSJeff Mahoney 	ret = btrfs_parse_options(fs_info, data, *flags);
176249b25e05SJeff Mahoney 	if (ret) {
176349b25e05SJeff Mahoney 		ret = -EINVAL;
176449b25e05SJeff Mahoney 		goto restore;
176549b25e05SJeff Mahoney 	}
1766b288052eSChris Mason 
1767f42a34b2SMiao Xie 	btrfs_remount_begin(fs_info, old_opts, *flags);
17680d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
17690d2450abSSergei Trofimovich 		fs_info->thread_pool_size, old_thread_pool_size);
17700d2450abSSergei Trofimovich 
17711751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1772dc81cdc5SMiao Xie 		goto out;
1773c146afadSYan Zheng 
17741751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
17758dabb742SStefan Behrens 		/*
17768dabb742SStefan Behrens 		 * this also happens on 'umount -rf' or on shutdown, when
17778dabb742SStefan Behrens 		 * the filesystem is busy.
17788dabb742SStefan Behrens 		 */
177921c7e756SMiao Xie 		cancel_work_sync(&fs_info->async_reclaim_work);
1780361c093dSStefan Behrens 
1781361c093dSStefan Behrens 		/* wait for the uuid_scan task to finish */
1782361c093dSStefan Behrens 		down(&fs_info->uuid_tree_rescan_sem);
1783361c093dSStefan Behrens 		/* avoid complains from lockdep et al. */
1784361c093dSStefan Behrens 		up(&fs_info->uuid_tree_rescan_sem);
1785361c093dSStefan Behrens 
17861751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1787c146afadSYan Zheng 
1788e44163e1SJeff Mahoney 		/*
17891751e8a6SLinus Torvalds 		 * Setting SB_RDONLY will put the cleaner thread to
1790e44163e1SJeff Mahoney 		 * sleep at the next loop if it's already active.
1791e44163e1SJeff Mahoney 		 * If it's already asleep, we'll leave unused block
1792e44163e1SJeff Mahoney 		 * groups on disk until we're mounted read-write again
1793e44163e1SJeff Mahoney 		 * unless we clean them up here.
1794e44163e1SJeff Mahoney 		 */
1795e44163e1SJeff Mahoney 		btrfs_delete_unused_bgs(fs_info);
1796e44163e1SJeff Mahoney 
17978dabb742SStefan Behrens 		btrfs_dev_replace_suspend_for_unmount(fs_info);
17988dabb742SStefan Behrens 		btrfs_scrub_cancel(fs_info);
1799061594efSMiao Xie 		btrfs_pause_balance(fs_info);
18008dabb742SStefan Behrens 
18016bccf3abSJeff Mahoney 		ret = btrfs_commit_super(fs_info);
180249b25e05SJeff Mahoney 		if (ret)
180349b25e05SJeff Mahoney 			goto restore;
1804c146afadSYan Zheng 	} else {
18050b246afaSJeff Mahoney 		if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
18066ef3de9cSDavid Sterba 			btrfs_err(fs_info,
1807efe120a0SFrank Holton 				"Remounting read-write after error is not allowed");
18086ef3de9cSDavid Sterba 			ret = -EINVAL;
18096ef3de9cSDavid Sterba 			goto restore;
18106ef3de9cSDavid Sterba 		}
18118a3db184SSergei Trofimovich 		if (fs_info->fs_devices->rw_devices == 0) {
181249b25e05SJeff Mahoney 			ret = -EACCES;
181349b25e05SJeff Mahoney 			goto restore;
18148a3db184SSergei Trofimovich 		}
18152b82032cSYan Zheng 
18166528b99dSAnand Jain 		if (!btrfs_check_rw_degradable(fs_info, NULL)) {
1817efe120a0SFrank Holton 			btrfs_warn(fs_info,
1818efe120a0SFrank Holton 				"too many missing devices, writeable remount is not allowed");
1819292fd7fcSStefan Behrens 			ret = -EACCES;
1820292fd7fcSStefan Behrens 			goto restore;
1821292fd7fcSStefan Behrens 		}
1822292fd7fcSStefan Behrens 
18238a3db184SSergei Trofimovich 		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
182449b25e05SJeff Mahoney 			ret = -EINVAL;
182549b25e05SJeff Mahoney 			goto restore;
18268a3db184SSergei Trofimovich 		}
1827c146afadSYan Zheng 
1828815745cfSAl Viro 		ret = btrfs_cleanup_fs_roots(fs_info);
182949b25e05SJeff Mahoney 		if (ret)
183049b25e05SJeff Mahoney 			goto restore;
1831c146afadSYan Zheng 
1832d68fc57bSYan, Zheng 		/* recover relocation */
18335f316481SWang Shilong 		mutex_lock(&fs_info->cleaner_mutex);
1834d68fc57bSYan, Zheng 		ret = btrfs_recover_relocation(root);
18355f316481SWang Shilong 		mutex_unlock(&fs_info->cleaner_mutex);
183649b25e05SJeff Mahoney 		if (ret)
183749b25e05SJeff Mahoney 			goto restore;
1838c146afadSYan Zheng 
18392b6ba629SIlya Dryomov 		ret = btrfs_resume_balance_async(fs_info);
18402b6ba629SIlya Dryomov 		if (ret)
18412b6ba629SIlya Dryomov 			goto restore;
18422b6ba629SIlya Dryomov 
18438dabb742SStefan Behrens 		ret = btrfs_resume_dev_replace_async(fs_info);
18448dabb742SStefan Behrens 		if (ret) {
1845efe120a0SFrank Holton 			btrfs_warn(fs_info, "failed to resume dev_replace");
18468dabb742SStefan Behrens 			goto restore;
18478dabb742SStefan Behrens 		}
184894aebfb2SJosef Bacik 
18496c6b5a39SAleksa Sarai 		btrfs_qgroup_rescan_resume(fs_info);
18506c6b5a39SAleksa Sarai 
185194aebfb2SJosef Bacik 		if (!fs_info->uuid_root) {
1852efe120a0SFrank Holton 			btrfs_info(fs_info, "creating UUID tree");
185394aebfb2SJosef Bacik 			ret = btrfs_create_uuid_tree(fs_info);
185494aebfb2SJosef Bacik 			if (ret) {
18555d163e0eSJeff Mahoney 				btrfs_warn(fs_info,
18565d163e0eSJeff Mahoney 					   "failed to create the UUID tree %d",
18575d163e0eSJeff Mahoney 					   ret);
185894aebfb2SJosef Bacik 				goto restore;
185994aebfb2SJosef Bacik 			}
186094aebfb2SJosef Bacik 		}
18611751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
186290c711abSZygo Blaxell 
1863afcdd129SJosef Bacik 		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1864c146afadSYan Zheng 	}
1865dc81cdc5SMiao Xie out:
18662c6a92b0SJustin Maggard 	wake_up_process(fs_info->transaction_kthread);
1867dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
1868c146afadSYan Zheng 	return 0;
186949b25e05SJeff Mahoney 
187049b25e05SJeff Mahoney restore:
18711751e8a6SLinus Torvalds 	/* We've hit an error - don't reset SB_RDONLY */
1872bc98a42cSDavid Howells 	if (sb_rdonly(sb))
18731751e8a6SLinus Torvalds 		old_flags |= SB_RDONLY;
187449b25e05SJeff Mahoney 	sb->s_flags = old_flags;
187549b25e05SJeff Mahoney 	fs_info->mount_opt = old_opts;
187649b25e05SJeff Mahoney 	fs_info->compress_type = old_compress_type;
187749b25e05SJeff Mahoney 	fs_info->max_inline = old_max_inline;
18780d2450abSSergei Trofimovich 	btrfs_resize_thread_pool(fs_info,
18790d2450abSSergei Trofimovich 		old_thread_pool_size, fs_info->thread_pool_size);
188049b25e05SJeff Mahoney 	fs_info->metadata_ratio = old_metadata_ratio;
1881dc81cdc5SMiao Xie 	btrfs_remount_cleanup(fs_info, old_opts);
188249b25e05SJeff Mahoney 	return ret;
1883c146afadSYan Zheng }
1884c146afadSYan Zheng 
1885bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */
1886bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1887bcd53741SArne Jansen 				       const void *dev_info2)
1888bcd53741SArne Jansen {
1889bcd53741SArne Jansen 	if (((struct btrfs_device_info *)dev_info1)->max_avail >
1890bcd53741SArne Jansen 	    ((struct btrfs_device_info *)dev_info2)->max_avail)
1891bcd53741SArne Jansen 		return -1;
1892bcd53741SArne Jansen 	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1893bcd53741SArne Jansen 		 ((struct btrfs_device_info *)dev_info2)->max_avail)
1894bcd53741SArne Jansen 		return 1;
1895bcd53741SArne Jansen 	else
1896bcd53741SArne Jansen 	return 0;
1897bcd53741SArne Jansen }
1898bcd53741SArne Jansen 
1899bcd53741SArne Jansen /*
1900bcd53741SArne Jansen  * sort the devices by max_avail, in which max free extent size of each device
1901bcd53741SArne Jansen  * is stored.(Descending Sort)
1902bcd53741SArne Jansen  */
1903bcd53741SArne Jansen static inline void btrfs_descending_sort_devices(
1904bcd53741SArne Jansen 					struct btrfs_device_info *devices,
1905bcd53741SArne Jansen 					size_t nr_devices)
1906bcd53741SArne Jansen {
1907bcd53741SArne Jansen 	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1908bcd53741SArne Jansen 	     btrfs_cmp_device_free_bytes, NULL);
1909bcd53741SArne Jansen }
1910bcd53741SArne Jansen 
19116d07bcecSMiao Xie /*
19126d07bcecSMiao Xie  * The helper to calc the free space on the devices that can be used to store
19136d07bcecSMiao Xie  * file data.
19146d07bcecSMiao Xie  */
19156bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
19166bccf3abSJeff Mahoney 				       u64 *free_bytes)
19176d07bcecSMiao Xie {
19186d07bcecSMiao Xie 	struct btrfs_device_info *devices_info;
19196d07bcecSMiao Xie 	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
19206d07bcecSMiao Xie 	struct btrfs_device *device;
19216d07bcecSMiao Xie 	u64 skip_space;
19226d07bcecSMiao Xie 	u64 type;
19236d07bcecSMiao Xie 	u64 avail_space;
19246d07bcecSMiao Xie 	u64 min_stripe_size;
192539fb26c3SMiao Xie 	int min_stripes = 1, num_stripes = 1;
19266d07bcecSMiao Xie 	int i = 0, nr_devices;
19276d07bcecSMiao Xie 
19287e33fd99SJosef Bacik 	/*
192901327610SNicholas D Steeves 	 * We aren't under the device list lock, so this is racy-ish, but good
19307e33fd99SJosef Bacik 	 * enough for our purposes.
19317e33fd99SJosef Bacik 	 */
1932b772a86eSLi Zefan 	nr_devices = fs_info->fs_devices->open_devices;
19337e33fd99SJosef Bacik 	if (!nr_devices) {
19347e33fd99SJosef Bacik 		smp_mb();
19357e33fd99SJosef Bacik 		nr_devices = fs_info->fs_devices->open_devices;
19367e33fd99SJosef Bacik 		ASSERT(nr_devices);
19377e33fd99SJosef Bacik 		if (!nr_devices) {
19387e33fd99SJosef Bacik 			*free_bytes = 0;
19397e33fd99SJosef Bacik 			return 0;
19407e33fd99SJosef Bacik 		}
19417e33fd99SJosef Bacik 	}
19426d07bcecSMiao Xie 
1943d9b0d9baSDulshani Gunawardhana 	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
19446a44517dSDavid Sterba 			       GFP_KERNEL);
19456d07bcecSMiao Xie 	if (!devices_info)
19466d07bcecSMiao Xie 		return -ENOMEM;
19476d07bcecSMiao Xie 
194801327610SNicholas D Steeves 	/* calc min stripe number for data space allocation */
19491b86826dSJeff Mahoney 	type = btrfs_data_alloc_profile(fs_info);
195039fb26c3SMiao Xie 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
19516d07bcecSMiao Xie 		min_stripes = 2;
195239fb26c3SMiao Xie 		num_stripes = nr_devices;
195339fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
19546d07bcecSMiao Xie 		min_stripes = 2;
195539fb26c3SMiao Xie 		num_stripes = 2;
195639fb26c3SMiao Xie 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
19576d07bcecSMiao Xie 		min_stripes = 4;
195839fb26c3SMiao Xie 		num_stripes = 4;
195939fb26c3SMiao Xie 	}
19606d07bcecSMiao Xie 
19616d07bcecSMiao Xie 	if (type & BTRFS_BLOCK_GROUP_DUP)
19626d07bcecSMiao Xie 		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
19636d07bcecSMiao Xie 	else
19646d07bcecSMiao Xie 		min_stripe_size = BTRFS_STRIPE_LEN;
19656d07bcecSMiao Xie 
19667e33fd99SJosef Bacik 	rcu_read_lock();
19677e33fd99SJosef Bacik 	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
1968e12c9621SAnand Jain 		if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1969e12c9621SAnand Jain 						&device->dev_state) ||
1970401e29c1SAnand Jain 		    !device->bdev ||
1971401e29c1SAnand Jain 		    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
19726d07bcecSMiao Xie 			continue;
19736d07bcecSMiao Xie 
19747e33fd99SJosef Bacik 		if (i >= nr_devices)
19757e33fd99SJosef Bacik 			break;
19767e33fd99SJosef Bacik 
19776d07bcecSMiao Xie 		avail_space = device->total_bytes - device->bytes_used;
19786d07bcecSMiao Xie 
19796d07bcecSMiao Xie 		/* align with stripe_len */
1980f8c269d7SDavid Sterba 		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
19816d07bcecSMiao Xie 		avail_space *= BTRFS_STRIPE_LEN;
19826d07bcecSMiao Xie 
19836d07bcecSMiao Xie 		/*
198401327610SNicholas D Steeves 		 * In order to avoid overwriting the superblock on the drive,
19856d07bcecSMiao Xie 		 * btrfs starts at an offset of at least 1MB when doing chunk
19866d07bcecSMiao Xie 		 * allocation.
19876d07bcecSMiao Xie 		 */
1988ee22184bSByongho Lee 		skip_space = SZ_1M;
19896d07bcecSMiao Xie 
19906d07bcecSMiao Xie 		/*
19916d07bcecSMiao Xie 		 * we can use the free space in [0, skip_space - 1], subtract
19926d07bcecSMiao Xie 		 * it from the total.
19936d07bcecSMiao Xie 		 */
19946d07bcecSMiao Xie 		if (avail_space && avail_space >= skip_space)
19956d07bcecSMiao Xie 			avail_space -= skip_space;
19966d07bcecSMiao Xie 		else
19976d07bcecSMiao Xie 			avail_space = 0;
19986d07bcecSMiao Xie 
19996d07bcecSMiao Xie 		if (avail_space < min_stripe_size)
20006d07bcecSMiao Xie 			continue;
20016d07bcecSMiao Xie 
20026d07bcecSMiao Xie 		devices_info[i].dev = device;
20036d07bcecSMiao Xie 		devices_info[i].max_avail = avail_space;
20046d07bcecSMiao Xie 
20056d07bcecSMiao Xie 		i++;
20066d07bcecSMiao Xie 	}
20077e33fd99SJosef Bacik 	rcu_read_unlock();
20086d07bcecSMiao Xie 
20096d07bcecSMiao Xie 	nr_devices = i;
20106d07bcecSMiao Xie 
20116d07bcecSMiao Xie 	btrfs_descending_sort_devices(devices_info, nr_devices);
20126d07bcecSMiao Xie 
20136d07bcecSMiao Xie 	i = nr_devices - 1;
20146d07bcecSMiao Xie 	avail_space = 0;
20156d07bcecSMiao Xie 	while (nr_devices >= min_stripes) {
201639fb26c3SMiao Xie 		if (num_stripes > nr_devices)
201739fb26c3SMiao Xie 			num_stripes = nr_devices;
201839fb26c3SMiao Xie 
20196d07bcecSMiao Xie 		if (devices_info[i].max_avail >= min_stripe_size) {
20206d07bcecSMiao Xie 			int j;
20216d07bcecSMiao Xie 			u64 alloc_size;
20226d07bcecSMiao Xie 
202339fb26c3SMiao Xie 			avail_space += devices_info[i].max_avail * num_stripes;
20246d07bcecSMiao Xie 			alloc_size = devices_info[i].max_avail;
202539fb26c3SMiao Xie 			for (j = i + 1 - num_stripes; j <= i; j++)
20266d07bcecSMiao Xie 				devices_info[j].max_avail -= alloc_size;
20276d07bcecSMiao Xie 		}
20286d07bcecSMiao Xie 		i--;
20296d07bcecSMiao Xie 		nr_devices--;
20306d07bcecSMiao Xie 	}
20316d07bcecSMiao Xie 
20326d07bcecSMiao Xie 	kfree(devices_info);
20336d07bcecSMiao Xie 	*free_bytes = avail_space;
20346d07bcecSMiao Xie 	return 0;
20356d07bcecSMiao Xie }
20366d07bcecSMiao Xie 
2037ba7b6e62SDavid Sterba /*
2038ba7b6e62SDavid Sterba  * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2039ba7b6e62SDavid Sterba  *
2040ba7b6e62SDavid Sterba  * If there's a redundant raid level at DATA block groups, use the respective
2041ba7b6e62SDavid Sterba  * multiplier to scale the sizes.
2042ba7b6e62SDavid Sterba  *
2043ba7b6e62SDavid Sterba  * Unused device space usage is based on simulating the chunk allocator
20440d0c71b3SDavid Sterba  * algorithm that respects the device sizes and order of allocations.  This is
20450d0c71b3SDavid Sterba  * a close approximation of the actual use but there are other factors that may
20460d0c71b3SDavid Sterba  * change the result (like a new metadata chunk).
2047ba7b6e62SDavid Sterba  *
2048ca8a51b3SDavid Sterba  * If metadata is exhausted, f_bavail will be 0.
2049ba7b6e62SDavid Sterba  */
20508fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
20518fd17795SChris Mason {
2052815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2053815745cfSAl Viro 	struct btrfs_super_block *disk_super = fs_info->super_copy;
2054815745cfSAl Viro 	struct list_head *head = &fs_info->space_info;
2055bd4d1088SJosef Bacik 	struct btrfs_space_info *found;
2056bd4d1088SJosef Bacik 	u64 total_used = 0;
20576d07bcecSMiao Xie 	u64 total_free_data = 0;
2058ca8a51b3SDavid Sterba 	u64 total_free_meta = 0;
2059db94535dSChris Mason 	int bits = dentry->d_sb->s_blocksize_bits;
2060815745cfSAl Viro 	__be32 *fsid = (__be32 *)fs_info->fsid;
2061ba7b6e62SDavid Sterba 	unsigned factor = 1;
2062ba7b6e62SDavid Sterba 	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
20636d07bcecSMiao Xie 	int ret;
2064ca8a51b3SDavid Sterba 	u64 thresh = 0;
2065ae02d1bdSLuis de Bethencourt 	int mixed = 0;
20668fd17795SChris Mason 
2067bd4d1088SJosef Bacik 	rcu_read_lock();
206889a55897SJosef Bacik 	list_for_each_entry_rcu(found, head, list) {
20696d07bcecSMiao Xie 		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2070ba7b6e62SDavid Sterba 			int i;
2071ba7b6e62SDavid Sterba 
20726d07bcecSMiao Xie 			total_free_data += found->disk_total - found->disk_used;
20736d07bcecSMiao Xie 			total_free_data -=
20746d07bcecSMiao Xie 				btrfs_account_ro_block_groups_free_space(found);
2075ba7b6e62SDavid Sterba 
2076ba7b6e62SDavid Sterba 			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2077ba7b6e62SDavid Sterba 				if (!list_empty(&found->block_groups[i])) {
2078ba7b6e62SDavid Sterba 					switch (i) {
2079ba7b6e62SDavid Sterba 					case BTRFS_RAID_DUP:
2080ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID1:
2081ba7b6e62SDavid Sterba 					case BTRFS_RAID_RAID10:
2082ba7b6e62SDavid Sterba 						factor = 2;
2083ba7b6e62SDavid Sterba 					}
2084ba7b6e62SDavid Sterba 				}
2085ba7b6e62SDavid Sterba 			}
20866d07bcecSMiao Xie 		}
2087ae02d1bdSLuis de Bethencourt 
2088ae02d1bdSLuis de Bethencourt 		/*
2089ae02d1bdSLuis de Bethencourt 		 * Metadata in mixed block goup profiles are accounted in data
2090ae02d1bdSLuis de Bethencourt 		 */
2091ae02d1bdSLuis de Bethencourt 		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2092ae02d1bdSLuis de Bethencourt 			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2093ae02d1bdSLuis de Bethencourt 				mixed = 1;
2094ae02d1bdSLuis de Bethencourt 			else
2095ae02d1bdSLuis de Bethencourt 				total_free_meta += found->disk_total -
2096ae02d1bdSLuis de Bethencourt 					found->disk_used;
2097ae02d1bdSLuis de Bethencourt 		}
20986d07bcecSMiao Xie 
2099b742bb82SYan, Zheng 		total_used += found->disk_used;
210089a55897SJosef Bacik 	}
2101ba7b6e62SDavid Sterba 
2102bd4d1088SJosef Bacik 	rcu_read_unlock();
2103bd4d1088SJosef Bacik 
2104ba7b6e62SDavid Sterba 	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2105ba7b6e62SDavid Sterba 	buf->f_blocks >>= bits;
2106ba7b6e62SDavid Sterba 	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2107ba7b6e62SDavid Sterba 
2108ba7b6e62SDavid Sterba 	/* Account global block reserve as used, it's in logical size already */
2109ba7b6e62SDavid Sterba 	spin_lock(&block_rsv->lock);
211041b34accSLuis de Bethencourt 	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
211141b34accSLuis de Bethencourt 	if (buf->f_bfree >= block_rsv->size >> bits)
2112ba7b6e62SDavid Sterba 		buf->f_bfree -= block_rsv->size >> bits;
211341b34accSLuis de Bethencourt 	else
211441b34accSLuis de Bethencourt 		buf->f_bfree = 0;
2115ba7b6e62SDavid Sterba 	spin_unlock(&block_rsv->lock);
2116ba7b6e62SDavid Sterba 
21170d95c1beSDavid Sterba 	buf->f_bavail = div_u64(total_free_data, factor);
21186bccf3abSJeff Mahoney 	ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
21197e33fd99SJosef Bacik 	if (ret)
21206d07bcecSMiao Xie 		return ret;
2121ba7b6e62SDavid Sterba 	buf->f_bavail += div_u64(total_free_data, factor);
21226d07bcecSMiao Xie 	buf->f_bavail = buf->f_bavail >> bits;
2123d397712bSChris Mason 
2124ca8a51b3SDavid Sterba 	/*
2125ca8a51b3SDavid Sterba 	 * We calculate the remaining metadata space minus global reserve. If
2126ca8a51b3SDavid Sterba 	 * this is (supposedly) smaller than zero, there's no space. But this
2127ca8a51b3SDavid Sterba 	 * does not hold in practice, the exhausted state happens where's still
2128ca8a51b3SDavid Sterba 	 * some positive delta. So we apply some guesswork and compare the
2129ca8a51b3SDavid Sterba 	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
2130ca8a51b3SDavid Sterba 	 *
2131ca8a51b3SDavid Sterba 	 * We probably cannot calculate the exact threshold value because this
2132ca8a51b3SDavid Sterba 	 * depends on the internal reservations requested by various
2133ca8a51b3SDavid Sterba 	 * operations, so some operations that consume a few metadata will
2134ca8a51b3SDavid Sterba 	 * succeed even if the Avail is zero. But this is better than the other
2135ca8a51b3SDavid Sterba 	 * way around.
2136ca8a51b3SDavid Sterba 	 */
2137d4417e22SNikolay Borisov 	thresh = SZ_4M;
2138ca8a51b3SDavid Sterba 
2139ae02d1bdSLuis de Bethencourt 	if (!mixed && total_free_meta - thresh < block_rsv->size)
2140ca8a51b3SDavid Sterba 		buf->f_bavail = 0;
2141ca8a51b3SDavid Sterba 
2142ba7b6e62SDavid Sterba 	buf->f_type = BTRFS_SUPER_MAGIC;
2143ba7b6e62SDavid Sterba 	buf->f_bsize = dentry->d_sb->s_blocksize;
2144ba7b6e62SDavid Sterba 	buf->f_namelen = BTRFS_NAME_LEN;
2145ba7b6e62SDavid Sterba 
21469d03632eSDavid Woodhouse 	/* We treat it as constant endianness (it doesn't matter _which_)
21479d03632eSDavid Woodhouse 	   because we want the fsid to come out the same whether mounted
21489d03632eSDavid Woodhouse 	   on a big-endian or little-endian host */
21499d03632eSDavid Woodhouse 	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
21509d03632eSDavid Woodhouse 	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
215132d48fa1SDavid Woodhouse 	/* Mask in the root object ID too, to disambiguate subvols */
21522b0143b5SDavid Howells 	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
21532b0143b5SDavid Howells 	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
215432d48fa1SDavid Woodhouse 
21558fd17795SChris Mason 	return 0;
21568fd17795SChris Mason }
2157b5133862SChris Mason 
2158aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb)
2159aea52e19SAl Viro {
2160815745cfSAl Viro 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2161aea52e19SAl Viro 	kill_anon_super(sb);
2162aea52e19SAl Viro 	free_fs_info(fs_info);
2163aea52e19SAl Viro }
2164aea52e19SAl Viro 
21652e635a27SChris Mason static struct file_system_type btrfs_fs_type = {
21662e635a27SChris Mason 	.owner		= THIS_MODULE,
21672e635a27SChris Mason 	.name		= "btrfs",
2168061dbc6bSAl Viro 	.mount		= btrfs_mount,
2169aea52e19SAl Viro 	.kill_sb	= btrfs_kill_super,
2170f667aef6SQu Wenruo 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
21712e635a27SChris Mason };
217272fa39f5SMisono, Tomohiro 
217372fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = {
217472fa39f5SMisono, Tomohiro 	.owner		= THIS_MODULE,
217572fa39f5SMisono, Tomohiro 	.name		= "btrfs",
217672fa39f5SMisono, Tomohiro 	.mount		= btrfs_mount_root,
217772fa39f5SMisono, Tomohiro 	.kill_sb	= btrfs_kill_super,
217872fa39f5SMisono, Tomohiro 	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
217972fa39f5SMisono, Tomohiro };
218072fa39f5SMisono, Tomohiro 
21817f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs");
2182a9218f6bSChris Mason 
2183d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file)
2184d8620958STom Van Braeckel {
2185d8620958STom Van Braeckel 	/*
2186d8620958STom Van Braeckel 	 * The control file's private_data is used to hold the
2187d8620958STom Van Braeckel 	 * transaction when it is started and is used to keep
2188d8620958STom Van Braeckel 	 * track of whether a transaction is already in progress.
2189d8620958STom Van Braeckel 	 */
2190d8620958STom Van Braeckel 	file->private_data = NULL;
2191d8620958STom Van Braeckel 	return 0;
2192d8620958STom Van Braeckel }
2193d8620958STom Van Braeckel 
2194d352ac68SChris Mason /*
2195d352ac68SChris Mason  * used by btrfsctl to scan devices when no FS is mounted
2196d352ac68SChris Mason  */
21978a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
21988a4b83ccSChris Mason 				unsigned long arg)
21998a4b83ccSChris Mason {
22008a4b83ccSChris Mason 	struct btrfs_ioctl_vol_args *vol;
22018a4b83ccSChris Mason 	struct btrfs_fs_devices *fs_devices;
2202c071fcfdSChris Mason 	int ret = -ENOTTY;
22038a4b83ccSChris Mason 
2204e441d54dSChris Mason 	if (!capable(CAP_SYS_ADMIN))
2205e441d54dSChris Mason 		return -EPERM;
2206e441d54dSChris Mason 
2207dae7b665SLi Zefan 	vol = memdup_user((void __user *)arg, sizeof(*vol));
2208dae7b665SLi Zefan 	if (IS_ERR(vol))
2209dae7b665SLi Zefan 		return PTR_ERR(vol);
2210c071fcfdSChris Mason 
22118a4b83ccSChris Mason 	switch (cmd) {
22128a4b83ccSChris Mason 	case BTRFS_IOC_SCAN_DEV:
221397288f2cSChristoph Hellwig 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
2214312c89fbSMisono, Tomohiro 					    &btrfs_root_fs_type, &fs_devices);
22158a4b83ccSChris Mason 		break;
221602db0844SJosef Bacik 	case BTRFS_IOC_DEVICES_READY:
221702db0844SJosef Bacik 		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
2218312c89fbSMisono, Tomohiro 					    &btrfs_root_fs_type, &fs_devices);
221902db0844SJosef Bacik 		if (ret)
222002db0844SJosef Bacik 			break;
222102db0844SJosef Bacik 		ret = !(fs_devices->num_devices == fs_devices->total_devices);
222202db0844SJosef Bacik 		break;
2223c5868f83SDavid Sterba 	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2224d5131b65SDavid Sterba 		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2225c5868f83SDavid Sterba 		break;
22268a4b83ccSChris Mason 	}
2227dae7b665SLi Zefan 
22288a4b83ccSChris Mason 	kfree(vol);
2229f819d837SLinda Knippers 	return ret;
22308a4b83ccSChris Mason }
22318a4b83ccSChris Mason 
22320176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb)
2233ed0dab6bSYan {
2234354aa0fbSMiao Xie 	struct btrfs_trans_handle *trans;
22350b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
22360b246afaSJeff Mahoney 	struct btrfs_root *root = fs_info->tree_root;
2237354aa0fbSMiao Xie 
2238fac03c8dSDavid Sterba 	set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22399e7cc91aSWang Xiaoguang 	/*
22409e7cc91aSWang Xiaoguang 	 * We don't need a barrier here, we'll wait for any transaction that
22419e7cc91aSWang Xiaoguang 	 * could be in progress on other threads (and do delayed iputs that
22429e7cc91aSWang Xiaoguang 	 * we want to avoid on a frozen filesystem), or do the commit
22439e7cc91aSWang Xiaoguang 	 * ourselves.
22449e7cc91aSWang Xiaoguang 	 */
2245d4edf39bSMiao Xie 	trans = btrfs_attach_transaction_barrier(root);
2246354aa0fbSMiao Xie 	if (IS_ERR(trans)) {
2247354aa0fbSMiao Xie 		/* no transaction, don't bother */
2248354aa0fbSMiao Xie 		if (PTR_ERR(trans) == -ENOENT)
22490176260fSLinus Torvalds 			return 0;
2250354aa0fbSMiao Xie 		return PTR_ERR(trans);
2251354aa0fbSMiao Xie 	}
22523a45bb20SJeff Mahoney 	return btrfs_commit_transaction(trans);
2253ed0dab6bSYan }
2254ed0dab6bSYan 
22559e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb)
22569e7cc91aSWang Xiaoguang {
2257fac03c8dSDavid Sterba 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2258fac03c8dSDavid Sterba 
2259fac03c8dSDavid Sterba 	clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
22609e7cc91aSWang Xiaoguang 	return 0;
22619e7cc91aSWang Xiaoguang }
22629e7cc91aSWang Xiaoguang 
22639c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
22649c5085c1SJosef Bacik {
22659c5085c1SJosef Bacik 	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
22669c5085c1SJosef Bacik 	struct btrfs_fs_devices *cur_devices;
22679c5085c1SJosef Bacik 	struct btrfs_device *dev, *first_dev = NULL;
22689c5085c1SJosef Bacik 	struct list_head *head;
22699c5085c1SJosef Bacik 	struct rcu_string *name;
22709c5085c1SJosef Bacik 
22719c5085c1SJosef Bacik 	mutex_lock(&fs_info->fs_devices->device_list_mutex);
22729c5085c1SJosef Bacik 	cur_devices = fs_info->fs_devices;
22739c5085c1SJosef Bacik 	while (cur_devices) {
22749c5085c1SJosef Bacik 		head = &cur_devices->devices;
22759c5085c1SJosef Bacik 		list_for_each_entry(dev, head, dev_list) {
2276e6e674bdSAnand Jain 			if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2277aa9ddcd4SJosef Bacik 				continue;
22780aeb8a6eSAnand Jain 			if (!dev->name)
22790aeb8a6eSAnand Jain 				continue;
22809c5085c1SJosef Bacik 			if (!first_dev || dev->devid < first_dev->devid)
22819c5085c1SJosef Bacik 				first_dev = dev;
22829c5085c1SJosef Bacik 		}
22839c5085c1SJosef Bacik 		cur_devices = cur_devices->seed;
22849c5085c1SJosef Bacik 	}
22859c5085c1SJosef Bacik 
22869c5085c1SJosef Bacik 	if (first_dev) {
22879c5085c1SJosef Bacik 		rcu_read_lock();
22889c5085c1SJosef Bacik 		name = rcu_dereference(first_dev->name);
22899c5085c1SJosef Bacik 		seq_escape(m, name->str, " \t\n\\");
22909c5085c1SJosef Bacik 		rcu_read_unlock();
22919c5085c1SJosef Bacik 	} else {
22929c5085c1SJosef Bacik 		WARN_ON(1);
22939c5085c1SJosef Bacik 	}
22949c5085c1SJosef Bacik 	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
22959c5085c1SJosef Bacik 	return 0;
22969c5085c1SJosef Bacik }
22979c5085c1SJosef Bacik 
2298b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = {
229976dda93cSYan, Zheng 	.drop_inode	= btrfs_drop_inode,
2300bd555975SAl Viro 	.evict_inode	= btrfs_evict_inode,
2301e20d96d6SChris Mason 	.put_super	= btrfs_put_super,
2302d5719762SChris Mason 	.sync_fs	= btrfs_sync_fs,
2303a9572a15SEric Paris 	.show_options	= btrfs_show_options,
23049c5085c1SJosef Bacik 	.show_devname	= btrfs_show_devname,
23054730a4bcSChris Mason 	.write_inode	= btrfs_write_inode,
23062c90e5d6SChris Mason 	.alloc_inode	= btrfs_alloc_inode,
23072c90e5d6SChris Mason 	.destroy_inode	= btrfs_destroy_inode,
23088fd17795SChris Mason 	.statfs		= btrfs_statfs,
2309c146afadSYan Zheng 	.remount_fs	= btrfs_remount,
23100176260fSLinus Torvalds 	.freeze_fs	= btrfs_freeze,
23119e7cc91aSWang Xiaoguang 	.unfreeze_fs	= btrfs_unfreeze,
2312e20d96d6SChris Mason };
2313a9218f6bSChris Mason 
2314a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = {
2315d8620958STom Van Braeckel 	.open = btrfs_control_open,
2316a9218f6bSChris Mason 	.unlocked_ioctl	 = btrfs_control_ioctl,
2317a9218f6bSChris Mason 	.compat_ioctl = btrfs_control_ioctl,
2318a9218f6bSChris Mason 	.owner	 = THIS_MODULE,
23196038f373SArnd Bergmann 	.llseek = noop_llseek,
2320a9218f6bSChris Mason };
2321a9218f6bSChris Mason 
2322a9218f6bSChris Mason static struct miscdevice btrfs_misc = {
2323578454ffSKay Sievers 	.minor		= BTRFS_MINOR,
2324a9218f6bSChris Mason 	.name		= "btrfs-control",
2325a9218f6bSChris Mason 	.fops		= &btrfs_ctl_fops
2326a9218f6bSChris Mason };
2327a9218f6bSChris Mason 
2328578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2329578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control");
2330578454ffSKay Sievers 
2331f5c29bd9SLiu Bo static int __init btrfs_interface_init(void)
2332a9218f6bSChris Mason {
2333a9218f6bSChris Mason 	return misc_register(&btrfs_misc);
2334a9218f6bSChris Mason }
2335a9218f6bSChris Mason 
2336*e67c718bSDavid Sterba static __cold void btrfs_interface_exit(void)
2337a9218f6bSChris Mason {
2338f368ed60SGreg Kroah-Hartman 	misc_deregister(&btrfs_misc);
2339a9218f6bSChris Mason }
2340a9218f6bSChris Mason 
2341f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void)
234285965600SDavid Sterba {
234362e85577SJeff Mahoney 	pr_info("Btrfs loaded, crc32c=%s"
234485965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
234585965600SDavid Sterba 			", debug=on"
234685965600SDavid Sterba #endif
234779556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT
234879556c3dSStefan Behrens 			", assert=on"
234979556c3dSStefan Behrens #endif
235085965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
235185965600SDavid Sterba 			", integrity-checker=on"
235285965600SDavid Sterba #endif
2353fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2354fb592373SJosef Bacik 			", ref-verify=on"
2355fb592373SJosef Bacik #endif
23565f9e1059SJeff Mahoney 			"\n",
23579678c543SNikolay Borisov 			crc32c_impl());
235885965600SDavid Sterba }
235985965600SDavid Sterba 
23602e635a27SChris Mason static int __init init_btrfs_fs(void)
23612e635a27SChris Mason {
23622c90e5d6SChris Mason 	int err;
236358176a96SJosef Bacik 
236463541927SFilipe David Borba Manana 	btrfs_props_init();
236563541927SFilipe David Borba Manana 
236658176a96SJosef Bacik 	err = btrfs_init_sysfs();
236758176a96SJosef Bacik 	if (err)
23689678c543SNikolay Borisov 		return err;
236958176a96SJosef Bacik 
2370143bede5SJeff Mahoney 	btrfs_init_compress();
2371d1310b2eSChris Mason 
2372261507a0SLi Zefan 	err = btrfs_init_cachep();
2373261507a0SLi Zefan 	if (err)
2374261507a0SLi Zefan 		goto free_compress;
2375261507a0SLi Zefan 
2376d1310b2eSChris Mason 	err = extent_io_init();
23772f4cbe64SWyatt Banks 	if (err)
23782f4cbe64SWyatt Banks 		goto free_cachep;
23792f4cbe64SWyatt Banks 
2380d1310b2eSChris Mason 	err = extent_map_init();
2381d1310b2eSChris Mason 	if (err)
2382d1310b2eSChris Mason 		goto free_extent_io;
2383d1310b2eSChris Mason 
23846352b91dSMiao Xie 	err = ordered_data_init();
23852f4cbe64SWyatt Banks 	if (err)
23862f4cbe64SWyatt Banks 		goto free_extent_map;
2387c8b97818SChris Mason 
23886352b91dSMiao Xie 	err = btrfs_delayed_inode_init();
23896352b91dSMiao Xie 	if (err)
23906352b91dSMiao Xie 		goto free_ordered_data;
23916352b91dSMiao Xie 
23929247f317SMiao Xie 	err = btrfs_auto_defrag_init();
239316cdcec7SMiao Xie 	if (err)
239416cdcec7SMiao Xie 		goto free_delayed_inode;
239516cdcec7SMiao Xie 
239678a6184aSMiao Xie 	err = btrfs_delayed_ref_init();
23979247f317SMiao Xie 	if (err)
23989247f317SMiao Xie 		goto free_auto_defrag;
23999247f317SMiao Xie 
2400b9e9a6cbSWang Shilong 	err = btrfs_prelim_ref_init();
2401b9e9a6cbSWang Shilong 	if (err)
2402af13b492SDavid Sterba 		goto free_delayed_ref;
2403b9e9a6cbSWang Shilong 
240497eb6b69SDavid Sterba 	err = btrfs_end_io_wq_init();
240578a6184aSMiao Xie 	if (err)
2406af13b492SDavid Sterba 		goto free_prelim_ref;
240778a6184aSMiao Xie 
240897eb6b69SDavid Sterba 	err = btrfs_interface_init();
240997eb6b69SDavid Sterba 	if (err)
241097eb6b69SDavid Sterba 		goto free_end_io_wq;
241197eb6b69SDavid Sterba 
2412e565d4b9SJan Schmidt 	btrfs_init_lockdep();
2413e565d4b9SJan Schmidt 
24148ae1af3cSAnand Jain 	btrfs_print_mod_info();
2415dc11dd5dSJosef Bacik 
2416dc11dd5dSJosef Bacik 	err = btrfs_run_sanity_tests();
2417dc11dd5dSJosef Bacik 	if (err)
2418dc11dd5dSJosef Bacik 		goto unregister_ioctl;
2419dc11dd5dSJosef Bacik 
2420dc11dd5dSJosef Bacik 	err = register_filesystem(&btrfs_fs_type);
2421dc11dd5dSJosef Bacik 	if (err)
2422dc11dd5dSJosef Bacik 		goto unregister_ioctl;
242374255aa0SJosef Bacik 
24242f4cbe64SWyatt Banks 	return 0;
24252f4cbe64SWyatt Banks 
2426a9218f6bSChris Mason unregister_ioctl:
2427a9218f6bSChris Mason 	btrfs_interface_exit();
242897eb6b69SDavid Sterba free_end_io_wq:
242997eb6b69SDavid Sterba 	btrfs_end_io_wq_exit();
2430b9e9a6cbSWang Shilong free_prelim_ref:
2431b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
243278a6184aSMiao Xie free_delayed_ref:
243378a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24349247f317SMiao Xie free_auto_defrag:
24359247f317SMiao Xie 	btrfs_auto_defrag_exit();
243616cdcec7SMiao Xie free_delayed_inode:
243716cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
24386352b91dSMiao Xie free_ordered_data:
24396352b91dSMiao Xie 	ordered_data_exit();
24402f4cbe64SWyatt Banks free_extent_map:
24412f4cbe64SWyatt Banks 	extent_map_exit();
2442d1310b2eSChris Mason free_extent_io:
2443d1310b2eSChris Mason 	extent_io_exit();
24442f4cbe64SWyatt Banks free_cachep:
24452f4cbe64SWyatt Banks 	btrfs_destroy_cachep();
2446261507a0SLi Zefan free_compress:
2447261507a0SLi Zefan 	btrfs_exit_compress();
24482f4cbe64SWyatt Banks 	btrfs_exit_sysfs();
24499678c543SNikolay Borisov 
24502c90e5d6SChris Mason 	return err;
24512e635a27SChris Mason }
24522e635a27SChris Mason 
24532e635a27SChris Mason static void __exit exit_btrfs_fs(void)
24542e635a27SChris Mason {
245539279cc3SChris Mason 	btrfs_destroy_cachep();
245678a6184aSMiao Xie 	btrfs_delayed_ref_exit();
24579247f317SMiao Xie 	btrfs_auto_defrag_exit();
245816cdcec7SMiao Xie 	btrfs_delayed_inode_exit();
2459b9e9a6cbSWang Shilong 	btrfs_prelim_ref_exit();
24606352b91dSMiao Xie 	ordered_data_exit();
2461a52d9a80SChris Mason 	extent_map_exit();
2462d1310b2eSChris Mason 	extent_io_exit();
2463a9218f6bSChris Mason 	btrfs_interface_exit();
24645ed5f588SJosef Bacik 	btrfs_end_io_wq_exit();
24652e635a27SChris Mason 	unregister_filesystem(&btrfs_fs_type);
246658176a96SJosef Bacik 	btrfs_exit_sysfs();
24678a4b83ccSChris Mason 	btrfs_cleanup_fs_uuids();
2468261507a0SLi Zefan 	btrfs_exit_compress();
24692e635a27SChris Mason }
24702e635a27SChris Mason 
247160efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs);
24722e635a27SChris Mason module_exit(exit_btrfs_fs)
24732e635a27SChris Mason 
24742e635a27SChris Mason MODULE_LICENSE("GPL");
2475