Blame view

fs/btrfs/super.c 65.2 KB
6cbd55707   Chris Mason   Btrfs: add GPLv2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * Copyright (C) 2007 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License v2 as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
4b82d6e4a   Yan   Btrfs: Add mount ...
18
  #include <linux/blkdev.h>
2e635a278   Chris Mason   Btrfs: initial mo...
19
  #include <linux/module.h>
e20d96d64   Chris Mason   Mountable btrfs, ...
20
  #include <linux/buffer_head.h>
2e635a278   Chris Mason   Btrfs: initial mo...
21
22
23
24
25
  #include <linux/fs.h>
  #include <linux/pagemap.h>
  #include <linux/highmem.h>
  #include <linux/time.h>
  #include <linux/init.h>
a9572a15a   Eric Paris   Btrfs: introduce ...
26
  #include <linux/seq_file.h>
2e635a278   Chris Mason   Btrfs: initial mo...
27
  #include <linux/string.h>
2e635a278   Chris Mason   Btrfs: initial mo...
28
  #include <linux/backing-dev.h>
4b82d6e4a   Yan   Btrfs: Add mount ...
29
  #include <linux/mount.h>
dee26a9f7   Chris Mason   btrfs_get_block, ...
30
  #include <linux/mpage.h>
75dfe3960   Chris Mason   btrfs_file_write ...
31
32
  #include <linux/swap.h>
  #include <linux/writeback.h>
8fd17795b   Chris Mason   Btrfs: early fsyn...
33
  #include <linux/statfs.h>
08607c1b1   Chris Mason   Btrfs: add compat...
34
  #include <linux/compat.h>
95e052891   Chris Mason   Btrfs: Use mount ...
35
  #include <linux/parser.h>
c59f8951d   Chris Mason   Btrfs: Add mount ...
36
  #include <linux/ctype.h>
6da6abae0   Chris Mason   Btrfs: Back port ...
37
  #include <linux/namei.h>
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
38
  #include <linux/miscdevice.h>
1bcbf3133   Qinghuang Feng   btrfs & squashfs:...
39
  #include <linux/magic.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
40
  #include <linux/slab.h>
90a887c9a   Dan Magenheimer   btrfs: add cleanc...
41
  #include <linux/cleancache.h>
22c44fe65   Josef Bacik   Btrfs: deal with ...
42
  #include <linux/ratelimit.h>
55e301fd5   Filipe Brandenburger   Btrfs: move fs/bt...
43
  #include <linux/btrfs.h>
16cdcec73   Miao Xie   btrfs: implement ...
44
  #include "delayed-inode.h"
2e635a278   Chris Mason   Btrfs: initial mo...
45
  #include "ctree.h"
e20d96d64   Chris Mason   Mountable btrfs, ...
46
  #include "disk-io.h"
d57197629   Chris Mason   btrfs_create, btr...
47
  #include "transaction.h"
2c90e5d65   Chris Mason   Btrfs: still corr...
48
  #include "btrfs_inode.h"
3a6863756   Chris Mason   Btrfs: sparse files!
49
  #include "print-tree.h"
14a958e67   Filipe David Borba Manana   Btrfs: fix btrfs ...
50
  #include "hash.h"
63541927c   Filipe David Borba Manana   Btrfs: add suppor...
51
  #include "props.h"
5103e947b   Josef Bacik   xattr support for...
52
  #include "xattr.h"
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
53
  #include "volumes.h"
be6e8dc0b   Balaji Rao   NFS support for b...
54
  #include "export.h"
c8b978188   Chris Mason   Btrfs: Add zlib c...
55
  #include "compression.h"
9c5085c14   Josef Bacik   Btrfs: implement ...
56
  #include "rcu-string.h"
8dabb7420   Stefan Behrens   Btrfs: change cor...
57
  #include "dev-replace.h"
74255aa07   Josef Bacik   Btrfs: add some f...
58
  #include "free-space-cache.h"
b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
59
  #include "backref.h"
dc11dd5d7   Josef Bacik   Btrfs: separate o...
60
  #include "tests/btrfs-tests.h"
2e635a278   Chris Mason   Btrfs: initial mo...
61

d3982100b   Mark Fasheh   btrfs: add trace ...
62
  #include "qgroup.h"
1abe9b8a1   liubo   Btrfs: add initia...
63
64
  #define CREATE_TRACE_POINTS
  #include <trace/events/btrfs.h>
b87221de6   Alexey Dobriyan   const: mark remai...
65
  static const struct super_operations btrfs_super_ops;
830c4adbd   Josef Bacik   Btrfs: fix how we...
66
  static struct file_system_type btrfs_fs_type;
75dfe3960   Chris Mason   btrfs_file_write ...
67

0723a0473   Harald Hoyer   btrfs: allow moun...
68
  static int btrfs_remount(struct super_block *sb, int *flags, char *data);
e33e17ee1   Jeff Mahoney   btrfs: add missin...
69
  const char *btrfs_decode_error(int errno)
acce952b0   liubo   Btrfs: forced rea...
70
  {
087488109   David Sterba   btrfs: clean up t...
71
  	char *errstr = "unknown";
acce952b0   liubo   Btrfs: forced rea...
72
73
74
75
76
77
78
79
80
81
82
  
  	switch (errno) {
  	case -EIO:
  		errstr = "IO failure";
  		break;
  	case -ENOMEM:
  		errstr = "Out of memory";
  		break;
  	case -EROFS:
  		errstr = "Readonly filesystem";
  		break;
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
83
84
85
  	case -EEXIST:
  		errstr = "Object already exists";
  		break;
94ef7280e   David Sterba   btrfs: cover more...
86
87
88
89
90
91
  	case -ENOSPC:
  		errstr = "No space left";
  		break;
  	case -ENOENT:
  		errstr = "No such entry";
  		break;
acce952b0   liubo   Btrfs: forced rea...
92
93
94
95
  	}
  
  	return errstr;
  }
acce952b0   liubo   Btrfs: forced rea...
96
97
98
99
100
101
102
  /* btrfs handle error by forcing the filesystem readonly */
  static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
  {
  	struct super_block *sb = fs_info->sb;
  
  	if (sb->s_flags & MS_RDONLY)
  		return;
87533c475   Miao Xie   Btrfs: use bit op...
103
  	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
acce952b0   liubo   Btrfs: forced rea...
104
  		sb->s_flags |= MS_RDONLY;
c2cf52eb7   Simon Kirby   Btrfs: Include th...
105
  		btrfs_info(fs_info, "forced readonly");
1acd6831d   Stefan Behrens   Btrfs: avoid risk...
106
107
108
109
  		/*
  		 * Note that a running device replace operation is not
  		 * canceled here although there is no way to update
  		 * the progress. It would add the risk of a deadlock,
013276101   Nicholas D Steeves   btrfs: fix string...
110
  		 * therefore the canceling is omitted. The only penalty
1acd6831d   Stefan Behrens   Btrfs: avoid risk...
111
112
113
114
115
  		 * is that some I/O remains active until the procedure
  		 * completes. The next time when the filesystem is
  		 * mounted writeable again, the device replace
  		 * operation continues.
  		 */
acce952b0   liubo   Btrfs: forced rea...
116
117
118
119
  	}
  }
  
  /*
34d970070   Anand Jain   btrfs: rename btr...
120
   * __btrfs_handle_fs_error decodes expected errors from the caller and
acce952b0   liubo   Btrfs: forced rea...
121
122
   * invokes the approciate error response.
   */
c0d19e2b9   David Sterba   btrfs: add 'cold'...
123
  __cold
34d970070   Anand Jain   btrfs: rename btr...
124
  void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
4da351134   Jeff Mahoney   btrfs: add vararg...
125
  		       unsigned int line, int errno, const char *fmt, ...)
acce952b0   liubo   Btrfs: forced rea...
126
127
  {
  	struct super_block *sb = fs_info->sb;
57d816a15   Anand Jain   Btrfs: __btrfs_st...
128
  #ifdef CONFIG_PRINTK
acce952b0   liubo   Btrfs: forced rea...
129
  	const char *errstr;
57d816a15   Anand Jain   Btrfs: __btrfs_st...
130
  #endif
acce952b0   liubo   Btrfs: forced rea...
131
132
133
134
135
136
  
  	/*
  	 * Special case: if the error is EROFS, and we're already
  	 * under MS_RDONLY, then it is safe here.
  	 */
  	if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
4da351134   Jeff Mahoney   btrfs: add vararg...
137
    		return;
57d816a15   Anand Jain   Btrfs: __btrfs_st...
138
  #ifdef CONFIG_PRINTK
087488109   David Sterba   btrfs: clean up t...
139
  	errstr = btrfs_decode_error(errno);
4da351134   Jeff Mahoney   btrfs: add vararg...
140
  	if (fmt) {
37252a66f   Eric Sandeen   btrfs: fix vararg...
141
142
143
144
145
146
  		struct va_format vaf;
  		va_list args;
  
  		va_start(args, fmt);
  		vaf.fmt = fmt;
  		vaf.va = &args;
4da351134   Jeff Mahoney   btrfs: add vararg...
147

62e855771   Jeff Mahoney   btrfs: convert pr...
148
149
  		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)
  ",
087488109   David Sterba   btrfs: clean up t...
150
  			sb->s_id, function, line, errno, errstr, &vaf);
37252a66f   Eric Sandeen   btrfs: fix vararg...
151
  		va_end(args);
4da351134   Jeff Mahoney   btrfs: add vararg...
152
  	} else {
62e855771   Jeff Mahoney   btrfs: convert pr...
153
154
  		pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s
  ",
087488109   David Sterba   btrfs: clean up t...
155
  			sb->s_id, function, line, errno, errstr);
4da351134   Jeff Mahoney   btrfs: add vararg...
156
  	}
57d816a15   Anand Jain   Btrfs: __btrfs_st...
157
  #endif
acce952b0   liubo   Btrfs: forced rea...
158

0713d90c7   Anand Jain   btrfs: remove sav...
159
160
161
162
163
  	/*
  	 * Today we only save the error info to memory.  Long term we'll
  	 * also send it down to the disk
  	 */
  	set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
4da351134   Jeff Mahoney   btrfs: add vararg...
164
  	/* Don't go through full error handling during mount */
cf79ffb5b   Josef Bacik   Btrfs: fix infini...
165
  	if (sb->s_flags & MS_BORN)
4da351134   Jeff Mahoney   btrfs: add vararg...
166
  		btrfs_handle_error(fs_info);
4da351134   Jeff Mahoney   btrfs: add vararg...
167
  }
acce952b0   liubo   Btrfs: forced rea...
168

57d816a15   Anand Jain   Btrfs: __btrfs_st...
169
  #ifdef CONFIG_PRINTK
533574c6b   Joe Perches   btrfs: use printk...
170
  static const char * const logtypes[] = {
4da351134   Jeff Mahoney   btrfs: add vararg...
171
172
173
174
175
176
177
178
179
  	"emergency",
  	"alert",
  	"critical",
  	"error",
  	"warning",
  	"notice",
  	"info",
  	"debug",
  };
35f4e5e6f   Nikolay Borisov   btrfs: Add rateli...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  
  /*
   * Use one ratelimit state per log level so that a flood of less important
   * messages doesn't cause more important ones to be dropped.
   */
  static struct ratelimit_state printk_limits[] = {
  	RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
  	RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
  };
c2cf52eb7   Simon Kirby   Btrfs: Include th...
195
  void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
4da351134   Jeff Mahoney   btrfs: add vararg...
196
197
198
199
200
201
  {
  	struct super_block *sb = fs_info->sb;
  	char lvl[4];
  	struct va_format vaf;
  	va_list args;
  	const char *type = logtypes[4];
533574c6b   Joe Perches   btrfs: use printk...
202
  	int kern_level;
35f4e5e6f   Nikolay Borisov   btrfs: Add rateli...
203
  	struct ratelimit_state *ratelimit;
4da351134   Jeff Mahoney   btrfs: add vararg...
204
205
  
  	va_start(args, fmt);
533574c6b   Joe Perches   btrfs: use printk...
206
207
208
209
210
211
212
  	kern_level = printk_get_level(fmt);
  	if (kern_level) {
  		size_t size = printk_skip_level(fmt) - fmt;
  		memcpy(lvl, fmt,  size);
  		lvl[size] = '\0';
  		fmt += size;
  		type = logtypes[kern_level - '0'];
35f4e5e6f   Nikolay Borisov   btrfs: Add rateli...
213
214
  		ratelimit = &printk_limits[kern_level - '0'];
  	} else {
4da351134   Jeff Mahoney   btrfs: add vararg...
215
  		*lvl = '\0';
35f4e5e6f   Nikolay Borisov   btrfs: Add rateli...
216
217
218
  		/* Default to debug output */
  		ratelimit = &printk_limits[7];
  	}
4da351134   Jeff Mahoney   btrfs: add vararg...
219
220
221
  
  	vaf.fmt = fmt;
  	vaf.va = &args;
533574c6b   Joe Perches   btrfs: use printk...
222

35f4e5e6f   Nikolay Borisov   btrfs: Add rateli...
223
224
225
  	if (__ratelimit(ratelimit))
  		printk("%sBTRFS %s (device %s): %pV
  ", lvl, type, sb->s_id, &vaf);
533574c6b   Joe Perches   btrfs: use printk...
226
227
228
  
  	va_end(args);
  }
533574c6b   Joe Perches   btrfs: use printk...
229
  #endif
acce952b0   liubo   Btrfs: forced rea...
230

8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
231
  /*
49b25e054   Jeff Mahoney   btrfs: enhance tr...
232
233
234
235
236
237
238
239
240
241
242
243
   * We only mark the transaction aborted and then set the file system read-only.
   * This will prevent new transactions from starting or trying to join this
   * one.
   *
   * This means that error recovery at the call site is limited to freeing
   * any local memory allocations and passing the error code up without
   * further cleanup. The transaction should complete as it normally would
   * in the call path but will return -EIO.
   *
   * We'll complete the cleanup in btrfs_end_transaction and
   * btrfs_commit_transaction.
   */
c0d19e2b9   David Sterba   btrfs: add 'cold'...
244
  __cold
49b25e054   Jeff Mahoney   btrfs: enhance tr...
245
  void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
246
  			       const char *function,
49b25e054   Jeff Mahoney   btrfs: enhance tr...
247
248
  			       unsigned int line, int errno)
  {
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
249
  	struct btrfs_fs_info *fs_info = trans->fs_info;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
250
251
252
  	trans->aborted = errno;
  	/* Nothing used. The other threads that have joined this
  	 * transaction may be able to continue. */
64c12921e   Jeff Mahoney   btrfs: account fo...
253
  	if (!trans->dirty && list_empty(&trans->new_bgs)) {
69ce977a1   Miao Xie   Btrfs: output mor...
254
  		const char *errstr;
087488109   David Sterba   btrfs: clean up t...
255
  		errstr = btrfs_decode_error(errno);
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
256
  		btrfs_warn(fs_info,
c2cf52eb7   Simon Kirby   Btrfs: Include th...
257
258
  		           "%s:%d: Aborting unused transaction(%s).",
  		           function, line, errstr);
acce952b0   liubo   Btrfs: forced rea...
259
  		return;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
260
  	}
8d25a086e   Miao Xie   Btrfs: Add ACCESS...
261
  	ACCESS_ONCE(trans->transaction->aborted) = errno;
501407aab   Josef Bacik   Btrfs: stop waiti...
262
  	/* Wake up anybody who may be waiting on this transaction */
66642832f   Jeff Mahoney   btrfs: btrfs_abor...
263
264
265
  	wake_up(&fs_info->transaction_wait);
  	wake_up(&fs_info->transaction_blocked_wait);
  	__btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
266
267
  }
  /*
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
268
269
270
   * __btrfs_panic decodes unexpected, fatal errors from the caller,
   * issues an alert, and either panics or BUGs, depending on mount options.
   */
c0d19e2b9   David Sterba   btrfs: add 'cold'...
271
  __cold
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
272
273
274
  void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  		   unsigned int line, int errno, const char *fmt, ...)
  {
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
275
276
277
278
  	char *s_id = "<unknown>";
  	const char *errstr;
  	struct va_format vaf = { .fmt = fmt };
  	va_list args;
acce952b0   liubo   Btrfs: forced rea...
279

8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
280
281
  	if (fs_info)
  		s_id = fs_info->sb->s_id;
acce952b0   liubo   Btrfs: forced rea...
282

8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
283
284
  	va_start(args, fmt);
  	vaf.va = &args;
087488109   David Sterba   btrfs: clean up t...
285
  	errstr = btrfs_decode_error(errno);
aa43a17c2   Eric Sandeen   btrfs: handle nul...
286
  	if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
087488109   David Sterba   btrfs: clean up t...
287
288
289
  		panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)
  ",
  			s_id, function, line, &vaf, errno, errstr);
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
290

efe120a06   Frank Holton   Btrfs: convert pr...
291
292
  	btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
  		   function, line, &vaf, errno, errstr);
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
293
294
  	va_end(args);
  	/* Caller calls BUG() */
acce952b0   liubo   Btrfs: forced rea...
295
  }
d397712bc   Chris Mason   Btrfs: Fix checkp...
296
  static void btrfs_put_super(struct super_block *sb)
b18c66858   Chris Mason   Btrfs: progress o...
297
  {
3abdbd780   David Sterba   btrfs: make close...
298
  	close_ctree(btrfs_sb(sb)->tree_root);
75dfe3960   Chris Mason   btrfs_file_write ...
299
  }
95e052891   Chris Mason   Btrfs: Use mount ...
300
  enum {
73f73415c   Josef Bacik   Btrfs: change how...
301
  	Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
287a0ab91   Josef Bacik   Btrfs: kill max_e...
302
303
  	Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
  	Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
261507a02   Li Zefan   btrfs: Allow to a...
304
305
  	Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
  	Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
70f6d82ec   Omar Sandoval   Btrfs: add free s...
306
307
308
309
310
  	Opt_space_cache, Opt_space_cache_version, Opt_clear_cache,
  	Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid,
  	Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery,
  	Opt_skip_balance, Opt_check_integrity,
  	Opt_check_integrity_including_extent_data,
f420ee1e9   Stefan Behrens   Btrfs: add mount ...
311
  	Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
e07a2ade4   Qu Wenruo   btrfs: Add nodisc...
312
  	Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard,
a258af7a3   Qu Wenruo   btrfs: Add dataco...
313
  	Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow,
8dcddfa04   Qu Wenruo   btrfs: Introduce ...
314
  	Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot,
fed8f166e   Qu Wenruo   btrfs: Introduce ...
315
  	Opt_nologreplay, Opt_norecovery,
d0bd45607   Josef Bacik   Btrfs: add fragme...
316
317
318
  #ifdef CONFIG_BTRFS_DEBUG
  	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
  #endif
9555c6c18   Ilya Dryomov   Btrfs: add skip_b...
319
  	Opt_err,
95e052891   Chris Mason   Btrfs: Use mount ...
320
  };
4d4ab6d6b   David Sterba   btrfs: constify s...
321
  static const match_table_t tokens = {
dfe250206   Chris Mason   Btrfs: Add mount ...
322
  	{Opt_degraded, "degraded"},
95e052891   Chris Mason   Btrfs: Use mount ...
323
  	{Opt_subvol, "subvol=%s"},
1493381f2   Wang Shilong   Btrfs: use u64 fo...
324
  	{Opt_subvolid, "subvolid=%s"},
43e570b08   Christoph Hellwig   btrfs: allow scan...
325
  	{Opt_device, "device=%s"},
b6cda9bcb   Chris Mason   Btrfs: Add mount ...
326
  	{Opt_nodatasum, "nodatasum"},
d399167d8   Qu Wenruo   btrfs: Add datasu...
327
  	{Opt_datasum, "datasum"},
be20aa9db   Chris Mason   Btrfs: Add mount ...
328
  	{Opt_nodatacow, "nodatacow"},
a258af7a3   Qu Wenruo   btrfs: Add dataco...
329
  	{Opt_datacow, "datacow"},
21ad10cf3   Chris Mason   Btrfs: Add flush ...
330
  	{Opt_nobarrier, "nobarrier"},
842bef589   Qu Wenruo   btrfs: Add "barri...
331
  	{Opt_barrier, "barrier"},
6f568d35a   Chris Mason   Btrfs: mount -o m...
332
  	{Opt_max_inline, "max_inline=%s"},
8f662a76c   Chris Mason   Btrfs: Add readah...
333
  	{Opt_alloc_start, "alloc_start=%s"},
4543df7ec   Chris Mason   Btrfs: Add a moun...
334
  	{Opt_thread_pool, "thread_pool=%d"},
c8b978188   Chris Mason   Btrfs: Add zlib c...
335
  	{Opt_compress, "compress"},
261507a02   Li Zefan   btrfs: Allow to a...
336
  	{Opt_compress_type, "compress=%s"},
a555f810a   Chris Mason   Btrfs: Add mount ...
337
  	{Opt_compress_force, "compress-force"},
261507a02   Li Zefan   btrfs: Allow to a...
338
  	{Opt_compress_force_type, "compress-force=%s"},
e18e4809b   Chris Mason   Btrfs: Add mount ...
339
  	{Opt_ssd, "ssd"},
451d7585a   Chris Mason   Btrfs: add mount ...
340
  	{Opt_ssd_spread, "ssd_spread"},
3b30c22f6   Chris Mason   Btrfs: Add mount ...
341
  	{Opt_nossd, "nossd"},
bd0330ad2   Qu Wenruo   btrfs: Add acl mo...
342
  	{Opt_acl, "acl"},
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
343
  	{Opt_noacl, "noacl"},
3a5e14048   Sage Weil   Btrfs: notreelog ...
344
  	{Opt_notreelog, "notreelog"},
a88998f29   Qu Wenruo   btrfs: Add treelo...
345
  	{Opt_treelog, "treelog"},
96da09192   Qu Wenruo   btrfs: Introduce ...
346
  	{Opt_nologreplay, "nologreplay"},
fed8f166e   Qu Wenruo   btrfs: Introduce ...
347
  	{Opt_norecovery, "norecovery"},
dccae9999   Sage Weil   Btrfs: add flusho...
348
  	{Opt_flushoncommit, "flushoncommit"},
2c9ee8567   Qu Wenruo   btrfs: Add noflus...
349
  	{Opt_noflushoncommit, "noflushoncommit"},
97e728d43   Josef Bacik   Btrfs: try to kee...
350
  	{Opt_ratio, "metadata_ratio=%d"},
e244a0aeb   Christoph Hellwig   Btrfs: add -o dis...
351
  	{Opt_discard, "discard"},
e07a2ade4   Qu Wenruo   btrfs: Add nodisc...
352
  	{Opt_nodiscard, "nodiscard"},
0af3d00ba   Josef Bacik   Btrfs: create spe...
353
  	{Opt_space_cache, "space_cache"},
70f6d82ec   Omar Sandoval   Btrfs: add free s...
354
  	{Opt_space_cache_version, "space_cache=%s"},
88c2ba3b0   Josef Bacik   Btrfs: Add a clea...
355
  	{Opt_clear_cache, "clear_cache"},
4260f7c75   Sage Weil   Btrfs: allow subv...
356
  	{Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
91435650c   Chris Mason   Btrfs: put ENOSPC...
357
  	{Opt_enospc_debug, "enospc_debug"},
530362934   Qu Wenruo   btrfs: Add noenos...
358
  	{Opt_noenospc_debug, "noenospc_debug"},
e15d05424   Xin Zhong   Btrfs: fix subvol...
359
  	{Opt_subvolrootid, "subvolrootid=%d"},
4cb5300bc   Chris Mason   Btrfs: add mount ...
360
  	{Opt_defrag, "autodefrag"},
fc0ca9af1   Qu Wenruo   btrfs: Add noauto...
361
  	{Opt_nodefrag, "noautodefrag"},
4b9465cb9   Chris Mason   Btrfs: add mount ...
362
  	{Opt_inode_cache, "inode_cache"},
3818aea27   Qu Wenruo   btrfs: Add noinod...
363
  	{Opt_noinode_cache, "noinode_cache"},
8965593e4   David Sterba   btrfs: rename the...
364
  	{Opt_no_space_cache, "nospace_cache"},
8dcddfa04   Qu Wenruo   btrfs: Introduce ...
365
366
  	{Opt_recovery, "recovery"}, /* deprecated */
  	{Opt_usebackuproot, "usebackuproot"},
9555c6c18   Ilya Dryomov   Btrfs: add skip_b...
367
  	{Opt_skip_balance, "skip_balance"},
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
368
369
370
  	{Opt_check_integrity, "check_int"},
  	{Opt_check_integrity_including_extent_data, "check_int_data"},
  	{Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
f420ee1e9   Stefan Behrens   Btrfs: add mount ...
371
  	{Opt_rescan_uuid_tree, "rescan_uuid_tree"},
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
372
  	{Opt_fatal_errors, "fatal_errors=%s"},
8b87dc17f   David Sterba   btrfs: add mount ...
373
  	{Opt_commit_interval, "commit=%d"},
d0bd45607   Josef Bacik   Btrfs: add fragme...
374
375
376
377
378
  #ifdef CONFIG_BTRFS_DEBUG
  	{Opt_fragment_data, "fragment=data"},
  	{Opt_fragment_metadata, "fragment=metadata"},
  	{Opt_fragment_all, "fragment=all"},
  #endif
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
379
  	{Opt_err, NULL},
95e052891   Chris Mason   Btrfs: Use mount ...
380
  };
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
381
382
383
  /*
   * Regular mount options parser.  Everything that is needed only when
   * reading in a new superblock is parsed here.
49b25e054   Jeff Mahoney   btrfs: enhance tr...
384
   * XXX JDM: This needs to be cleaned up for remount.
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
385
   */
96da09192   Qu Wenruo   btrfs: Introduce ...
386
387
  int btrfs_parse_options(struct btrfs_root *root, char *options,
  			unsigned long new_flags)
95e052891   Chris Mason   Btrfs: Use mount ...
388
  {
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
389
  	struct btrfs_fs_info *info = root->fs_info;
95e052891   Chris Mason   Btrfs: Use mount ...
390
  	substring_t args[MAX_OPT_ARGS];
73bc18768   Josef Bacik   Btrfs: introduce ...
391
392
  	char *p, *num, *orig = NULL;
  	u64 cache_gen;
4543df7ec   Chris Mason   Btrfs: Add a moun...
393
  	int intarg;
a7a3f7cad   Sage Weil   Btrfs: fail mount...
394
  	int ret = 0;
261507a02   Li Zefan   btrfs: Allow to a...
395
396
  	char *compress_type;
  	bool compress_force = false;
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
397
398
399
  	enum btrfs_compression_type saved_compress_type;
  	bool saved_compress_force;
  	int no_compress = 0;
b6cda9bcb   Chris Mason   Btrfs: Add mount ...
400

6c41761fc   David Sterba   btrfs: separate s...
401
  	cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
70f6d82ec   Omar Sandoval   Btrfs: add free s...
402
403
404
  	if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE))
  		btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
  	else if (cache_gen)
73bc18768   Josef Bacik   Btrfs: introduce ...
405
  		btrfs_set_opt(info->mount_opt, SPACE_CACHE);
96da09192   Qu Wenruo   btrfs: Introduce ...
406
407
408
409
  	/*
  	 * Even the options are empty, we still need to do extra check
  	 * against new flags
  	 */
95e052891   Chris Mason   Btrfs: Use mount ...
410
  	if (!options)
96da09192   Qu Wenruo   btrfs: Introduce ...
411
  		goto check;
95e052891   Chris Mason   Btrfs: Use mount ...
412

be20aa9db   Chris Mason   Btrfs: Add mount ...
413
414
415
416
417
418
419
  	/*
  	 * strsep changes the string, duplicate it because parse_options
  	 * gets called twice
  	 */
  	options = kstrdup(options, GFP_NOFS);
  	if (!options)
  		return -ENOMEM;
da495ecc0   Josef Bacik   Btrfs: kfree corr...
420
  	orig = options;
be20aa9db   Chris Mason   Btrfs: Add mount ...
421

edf24abe5   Christoph Hellwig   btrfs: sanity mou...
422
  	while ((p = strsep(&options, ",")) != NULL) {
95e052891   Chris Mason   Btrfs: Use mount ...
423
424
425
426
427
428
  		int token;
  		if (!*p)
  			continue;
  
  		token = match_token(p, tokens, args);
  		switch (token) {
dfe250206   Chris Mason   Btrfs: Add mount ...
429
  		case Opt_degraded:
efe120a06   Frank Holton   Btrfs: convert pr...
430
  			btrfs_info(root->fs_info, "allowing degraded mounts");
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
431
  			btrfs_set_opt(info->mount_opt, DEGRADED);
dfe250206   Chris Mason   Btrfs: Add mount ...
432
  			break;
95e052891   Chris Mason   Btrfs: Use mount ...
433
  		case Opt_subvol:
73f73415c   Josef Bacik   Btrfs: change how...
434
  		case Opt_subvolid:
e15d05424   Xin Zhong   Btrfs: fix subvol...
435
  		case Opt_subvolrootid:
43e570b08   Christoph Hellwig   btrfs: allow scan...
436
  		case Opt_device:
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
437
  			/*
43e570b08   Christoph Hellwig   btrfs: allow scan...
438
  			 * These are parsed by btrfs_parse_early_options
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
439
440
  			 * and can be happily ignored here.
  			 */
b6cda9bcb   Chris Mason   Btrfs: Add mount ...
441
442
  			break;
  		case Opt_nodatasum:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
443
  			btrfs_set_and_info(info, NODATASUM,
078025347   Qu Wenruo   btrfs: Cleanup th...
444
  					   "setting nodatasum");
be20aa9db   Chris Mason   Btrfs: Add mount ...
445
  			break;
d399167d8   Qu Wenruo   btrfs: Add datasu...
446
  		case Opt_datasum:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
447
448
  			if (btrfs_test_opt(info, NODATASUM)) {
  				if (btrfs_test_opt(info, NODATACOW))
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
449
450
  					btrfs_info(root->fs_info,
  						   "setting datasum, datacow enabled");
078025347   Qu Wenruo   btrfs: Cleanup th...
451
  				else
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
452
453
  					btrfs_info(root->fs_info,
  						   "setting datasum");
078025347   Qu Wenruo   btrfs: Cleanup th...
454
  			}
d399167d8   Qu Wenruo   btrfs: Add datasu...
455
456
457
  			btrfs_clear_opt(info->mount_opt, NODATACOW);
  			btrfs_clear_opt(info->mount_opt, NODATASUM);
  			break;
be20aa9db   Chris Mason   Btrfs: Add mount ...
458
  		case Opt_nodatacow:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
459
460
461
  			if (!btrfs_test_opt(info, NODATACOW)) {
  				if (!btrfs_test_opt(info, COMPRESS) ||
  				    !btrfs_test_opt(info, FORCE_COMPRESS)) {
efe120a06   Frank Holton   Btrfs: convert pr...
462
  					btrfs_info(root->fs_info,
078025347   Qu Wenruo   btrfs: Cleanup th...
463
464
  						   "setting nodatacow, compression disabled");
  				} else {
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
465
466
  					btrfs_info(root->fs_info,
  						   "setting nodatacow");
078025347   Qu Wenruo   btrfs: Cleanup th...
467
  				}
bedb2cca7   Andrei Popa   Btrfs: make compr...
468
  			}
bedb2cca7   Andrei Popa   Btrfs: make compr...
469
470
  			btrfs_clear_opt(info->mount_opt, COMPRESS);
  			btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
471
472
  			btrfs_set_opt(info->mount_opt, NODATACOW);
  			btrfs_set_opt(info->mount_opt, NODATASUM);
95e052891   Chris Mason   Btrfs: Use mount ...
473
  			break;
a258af7a3   Qu Wenruo   btrfs: Add dataco...
474
  		case Opt_datacow:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
475
  			btrfs_clear_and_info(info, NODATACOW,
078025347   Qu Wenruo   btrfs: Cleanup th...
476
  					     "setting datacow");
a258af7a3   Qu Wenruo   btrfs: Add dataco...
477
  			break;
a555f810a   Chris Mason   Btrfs: Add mount ...
478
  		case Opt_compress_force:
261507a02   Li Zefan   btrfs: Allow to a...
479
480
  		case Opt_compress_force_type:
  			compress_force = true;
1c697d4ac   Eric Sandeen   btrfs: annotate i...
481
  			/* Fallthrough */
261507a02   Li Zefan   btrfs: Allow to a...
482
483
  		case Opt_compress:
  		case Opt_compress_type:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
484
485
  			saved_compress_type = btrfs_test_opt(info,
  							     COMPRESS) ?
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
486
487
  				info->compress_type : BTRFS_COMPRESS_NONE;
  			saved_compress_force =
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
488
  				btrfs_test_opt(info, FORCE_COMPRESS);
261507a02   Li Zefan   btrfs: Allow to a...
489
490
491
492
493
  			if (token == Opt_compress ||
  			    token == Opt_compress_force ||
  			    strcmp(args[0].from, "zlib") == 0) {
  				compress_type = "zlib";
  				info->compress_type = BTRFS_COMPRESS_ZLIB;
063849eaf   Arnd Hannemann   Btrfs: allow moun...
494
  				btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca7   Andrei Popa   Btrfs: make compr...
495
496
  				btrfs_clear_opt(info->mount_opt, NODATACOW);
  				btrfs_clear_opt(info->mount_opt, NODATASUM);
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
497
  				no_compress = 0;
a6fa6fae4   Li Zefan   btrfs: Add lzo co...
498
499
500
  			} else if (strcmp(args[0].from, "lzo") == 0) {
  				compress_type = "lzo";
  				info->compress_type = BTRFS_COMPRESS_LZO;
063849eaf   Arnd Hannemann   Btrfs: allow moun...
501
  				btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca7   Andrei Popa   Btrfs: make compr...
502
503
  				btrfs_clear_opt(info->mount_opt, NODATACOW);
  				btrfs_clear_opt(info->mount_opt, NODATASUM);
2b0ce2c29   Mitch Harder   Btrfs: Check INCO...
504
  				btrfs_set_fs_incompat(info, COMPRESS_LZO);
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
505
  				no_compress = 0;
063849eaf   Arnd Hannemann   Btrfs: allow moun...
506
507
  			} else if (strncmp(args[0].from, "no", 2) == 0) {
  				compress_type = "no";
063849eaf   Arnd Hannemann   Btrfs: allow moun...
508
509
510
  				btrfs_clear_opt(info->mount_opt, COMPRESS);
  				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
  				compress_force = false;
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
511
  				no_compress++;
261507a02   Li Zefan   btrfs: Allow to a...
512
513
514
515
  			} else {
  				ret = -EINVAL;
  				goto out;
  			}
261507a02   Li Zefan   btrfs: Allow to a...
516
  			if (compress_force) {
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
517
  				btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
143f36361   David Sterba   btrfs: remove unu...
518
  			} else {
4027e0f4c   Wang Shilong   Btrfs: clear comp...
519
520
521
522
523
524
525
  				/*
  				 * If we remount from compress-force=xxx to
  				 * compress=xxx, we need clear FORCE_COMPRESS
  				 * flag, otherwise, there is no way for users
  				 * to disable forcible compression separately.
  				 */
  				btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
a7e252af5   Miao Xie   Btrfs: don't clea...
526
  			}
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
527
  			if ((btrfs_test_opt(info, COMPRESS) &&
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
528
529
  			     (info->compress_type != saved_compress_type ||
  			      compress_force != saved_compress_force)) ||
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
530
  			    (!btrfs_test_opt(info, COMPRESS) &&
b7c47bbb2   Tsutomu Itoh   Btrfs: fix output...
531
532
533
534
535
536
537
  			     no_compress == 1)) {
  				btrfs_info(root->fs_info,
  					   "%s %s compression",
  					   (compress_force) ? "force" : "use",
  					   compress_type);
  			}
  			compress_force = false;
a555f810a   Chris Mason   Btrfs: Add mount ...
538
  			break;
e18e4809b   Chris Mason   Btrfs: Add mount ...
539
  		case Opt_ssd:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
540
  			btrfs_set_and_info(info, SSD,
078025347   Qu Wenruo   btrfs: Cleanup th...
541
  					   "use ssd allocation scheme");
e18e4809b   Chris Mason   Btrfs: Add mount ...
542
  			break;
451d7585a   Chris Mason   Btrfs: add mount ...
543
  		case Opt_ssd_spread:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
544
  			btrfs_set_and_info(info, SSD_SPREAD,
078025347   Qu Wenruo   btrfs: Cleanup th...
545
  					   "use spread ssd allocation scheme");
2aa06a35d   Eric Sandeen   btrfs: fix nossd ...
546
  			btrfs_set_opt(info->mount_opt, SSD);
451d7585a   Chris Mason   Btrfs: add mount ...
547
  			break;
3b30c22f6   Chris Mason   Btrfs: Add mount ...
548
  		case Opt_nossd:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
549
  			btrfs_set_and_info(info, NOSSD,
078025347   Qu Wenruo   btrfs: Cleanup th...
550
  					     "not using ssd allocation scheme");
3b30c22f6   Chris Mason   Btrfs: Add mount ...
551
552
  			btrfs_clear_opt(info->mount_opt, SSD);
  			break;
842bef589   Qu Wenruo   btrfs: Add "barri...
553
  		case Opt_barrier:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
554
  			btrfs_clear_and_info(info, NOBARRIER,
078025347   Qu Wenruo   btrfs: Cleanup th...
555
  					     "turning on barriers");
842bef589   Qu Wenruo   btrfs: Add "barri...
556
  			break;
21ad10cf3   Chris Mason   Btrfs: Add flush ...
557
  		case Opt_nobarrier:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
558
  			btrfs_set_and_info(info, NOBARRIER,
078025347   Qu Wenruo   btrfs: Cleanup th...
559
  					   "turning off barriers");
21ad10cf3   Chris Mason   Btrfs: Add flush ...
560
  			break;
4543df7ec   Chris Mason   Btrfs: Add a moun...
561
  		case Opt_thread_pool:
2c334e87f   Wang Shilong   Btrfs: add sanity...
562
563
564
565
  			ret = match_int(&args[0], &intarg);
  			if (ret) {
  				goto out;
  			} else if (intarg > 0) {
4543df7ec   Chris Mason   Btrfs: Add a moun...
566
  				info->thread_pool_size = intarg;
2c334e87f   Wang Shilong   Btrfs: add sanity...
567
568
569
570
  			} else {
  				ret = -EINVAL;
  				goto out;
  			}
4543df7ec   Chris Mason   Btrfs: Add a moun...
571
  			break;
6f568d35a   Chris Mason   Btrfs: mount -o m...
572
  		case Opt_max_inline:
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
573
574
  			num = match_strdup(&args[0]);
  			if (num) {
91748467a   Akinobu Mita   btrfs: use memparse
575
  				info->max_inline = memparse(num, NULL);
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
576
  				kfree(num);
15ada040d   Chris Mason   Btrfs: Fix mount ...
577
  				if (info->max_inline) {
feb5f9658   Mitch Harder   Btrfs: fix max_in...
578
  					info->max_inline = min_t(u64,
15ada040d   Chris Mason   Btrfs: Fix mount ...
579
580
581
  						info->max_inline,
  						root->sectorsize);
  				}
efe120a06   Frank Holton   Btrfs: convert pr...
582
  				btrfs_info(root->fs_info, "max_inline at %llu",
c1c9ff7c9   Geert Uytterhoeven   Btrfs: Remove sup...
583
  					info->max_inline);
2c334e87f   Wang Shilong   Btrfs: add sanity...
584
585
586
  			} else {
  				ret = -ENOMEM;
  				goto out;
6f568d35a   Chris Mason   Btrfs: mount -o m...
587
588
  			}
  			break;
8f662a76c   Chris Mason   Btrfs: Add readah...
589
  		case Opt_alloc_start:
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
590
591
  			num = match_strdup(&args[0]);
  			if (num) {
c018daece   Miao Xie   Btrfs: protect fs...
592
  				mutex_lock(&info->chunk_mutex);
91748467a   Akinobu Mita   btrfs: use memparse
593
  				info->alloc_start = memparse(num, NULL);
c018daece   Miao Xie   Btrfs: protect fs...
594
  				mutex_unlock(&info->chunk_mutex);
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
595
  				kfree(num);
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
596
597
598
  				btrfs_info(root->fs_info,
  					   "allocations start at %llu",
  					   info->alloc_start);
2c334e87f   Wang Shilong   Btrfs: add sanity...
599
600
601
  			} else {
  				ret = -ENOMEM;
  				goto out;
8f662a76c   Chris Mason   Btrfs: Add readah...
602
603
  			}
  			break;
bd0330ad2   Qu Wenruo   btrfs: Add acl mo...
604
  		case Opt_acl:
45ff35d6b   Guangliang Zhao   Btrfs: remove OPT...
605
  #ifdef CONFIG_BTRFS_FS_POSIX_ACL
bd0330ad2   Qu Wenruo   btrfs: Add acl mo...
606
607
  			root->fs_info->sb->s_flags |= MS_POSIXACL;
  			break;
45ff35d6b   Guangliang Zhao   Btrfs: remove OPT...
608
609
610
611
612
613
  #else
  			btrfs_err(root->fs_info,
  				"support for ACL not compiled in!");
  			ret = -EINVAL;
  			goto out;
  #endif
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
614
615
616
  		case Opt_noacl:
  			root->fs_info->sb->s_flags &= ~MS_POSIXACL;
  			break;
3a5e14048   Sage Weil   Btrfs: notreelog ...
617
  		case Opt_notreelog:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
618
  			btrfs_set_and_info(info, NOTREELOG,
078025347   Qu Wenruo   btrfs: Cleanup th...
619
  					   "disabling tree log");
a88998f29   Qu Wenruo   btrfs: Add treelo...
620
621
  			break;
  		case Opt_treelog:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
622
  			btrfs_clear_and_info(info, NOTREELOG,
078025347   Qu Wenruo   btrfs: Cleanup th...
623
  					     "enabling tree log");
3a5e14048   Sage Weil   Btrfs: notreelog ...
624
  			break;
fed8f166e   Qu Wenruo   btrfs: Introduce ...
625
  		case Opt_norecovery:
96da09192   Qu Wenruo   btrfs: Introduce ...
626
  		case Opt_nologreplay:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
627
  			btrfs_set_and_info(info, NOLOGREPLAY,
96da09192   Qu Wenruo   btrfs: Introduce ...
628
629
  					   "disabling log replay at mount time");
  			break;
dccae9999   Sage Weil   Btrfs: add flusho...
630
  		case Opt_flushoncommit:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
631
  			btrfs_set_and_info(info, FLUSHONCOMMIT,
078025347   Qu Wenruo   btrfs: Cleanup th...
632
  					   "turning on flush-on-commit");
dccae9999   Sage Weil   Btrfs: add flusho...
633
  			break;
2c9ee8567   Qu Wenruo   btrfs: Add noflus...
634
  		case Opt_noflushoncommit:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
635
  			btrfs_clear_and_info(info, FLUSHONCOMMIT,
078025347   Qu Wenruo   btrfs: Cleanup th...
636
  					     "turning off flush-on-commit");
2c9ee8567   Qu Wenruo   btrfs: Add noflus...
637
  			break;
97e728d43   Josef Bacik   Btrfs: try to kee...
638
  		case Opt_ratio:
2c334e87f   Wang Shilong   Btrfs: add sanity...
639
640
641
642
  			ret = match_int(&args[0], &intarg);
  			if (ret) {
  				goto out;
  			} else if (intarg >= 0) {
97e728d43   Josef Bacik   Btrfs: try to kee...
643
  				info->metadata_ratio = intarg;
efe120a06   Frank Holton   Btrfs: convert pr...
644
  				btrfs_info(root->fs_info, "metadata ratio %d",
97e728d43   Josef Bacik   Btrfs: try to kee...
645
  				       info->metadata_ratio);
2c334e87f   Wang Shilong   Btrfs: add sanity...
646
647
648
  			} else {
  				ret = -EINVAL;
  				goto out;
97e728d43   Josef Bacik   Btrfs: try to kee...
649
650
  			}
  			break;
e244a0aeb   Christoph Hellwig   Btrfs: add -o dis...
651
  		case Opt_discard:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
652
  			btrfs_set_and_info(info, DISCARD,
078025347   Qu Wenruo   btrfs: Cleanup th...
653
  					   "turning on discard");
e244a0aeb   Christoph Hellwig   Btrfs: add -o dis...
654
  			break;
e07a2ade4   Qu Wenruo   btrfs: Add nodisc...
655
  		case Opt_nodiscard:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
656
  			btrfs_clear_and_info(info, DISCARD,
078025347   Qu Wenruo   btrfs: Cleanup th...
657
  					     "turning off discard");
e07a2ade4   Qu Wenruo   btrfs: Add nodisc...
658
  			break;
0af3d00ba   Josef Bacik   Btrfs: create spe...
659
  		case Opt_space_cache:
70f6d82ec   Omar Sandoval   Btrfs: add free s...
660
661
662
663
664
  		case Opt_space_cache_version:
  			if (token == Opt_space_cache ||
  			    strcmp(args[0].from, "v1") == 0) {
  				btrfs_clear_opt(root->fs_info->mount_opt,
  						FREE_SPACE_TREE);
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
665
  				btrfs_set_and_info(info, SPACE_CACHE,
70f6d82ec   Omar Sandoval   Btrfs: add free s...
666
667
668
669
  						   "enabling disk space caching");
  			} else if (strcmp(args[0].from, "v2") == 0) {
  				btrfs_clear_opt(root->fs_info->mount_opt,
  						SPACE_CACHE);
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
670
671
  				btrfs_set_and_info(info,
  						   FREE_SPACE_TREE,
70f6d82ec   Omar Sandoval   Btrfs: add free s...
672
673
674
675
676
  						   "enabling free space tree");
  			} else {
  				ret = -EINVAL;
  				goto out;
  			}
0de90876c   Josef Bacik   Btrfs: handle the...
677
  			break;
f420ee1e9   Stefan Behrens   Btrfs: add mount ...
678
679
680
  		case Opt_rescan_uuid_tree:
  			btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
  			break;
73bc18768   Josef Bacik   Btrfs: introduce ...
681
  		case Opt_no_space_cache:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
682
683
684
  			if (btrfs_test_opt(info, SPACE_CACHE)) {
  				btrfs_clear_and_info(info,
  						     SPACE_CACHE,
70f6d82ec   Omar Sandoval   Btrfs: add free s...
685
686
  						     "disabling disk space caching");
  			}
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
687
688
689
  			if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
  				btrfs_clear_and_info(info,
  						     FREE_SPACE_TREE,
70f6d82ec   Omar Sandoval   Btrfs: add free s...
690
691
  						     "disabling free space tree");
  			}
73bc18768   Josef Bacik   Btrfs: introduce ...
692
  			break;
4b9465cb9   Chris Mason   Btrfs: add mount ...
693
  		case Opt_inode_cache:
7e1876aca   David Sterba   btrfs: switch ino...
694
  			btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
078025347   Qu Wenruo   btrfs: Cleanup th...
695
  					   "enabling inode map caching");
3818aea27   Qu Wenruo   btrfs: Add noinod...
696
697
  			break;
  		case Opt_noinode_cache:
7e1876aca   David Sterba   btrfs: switch ino...
698
  			btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
078025347   Qu Wenruo   btrfs: Cleanup th...
699
  					     "disabling inode map caching");
4b9465cb9   Chris Mason   Btrfs: add mount ...
700
  			break;
88c2ba3b0   Josef Bacik   Btrfs: Add a clea...
701
  		case Opt_clear_cache:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
702
  			btrfs_set_and_info(info, CLEAR_CACHE,
078025347   Qu Wenruo   btrfs: Cleanup th...
703
  					   "force clearing of disk cache");
0af3d00ba   Josef Bacik   Btrfs: create spe...
704
  			break;
4260f7c75   Sage Weil   Btrfs: allow subv...
705
706
707
  		case Opt_user_subvol_rm_allowed:
  			btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
  			break;
91435650c   Chris Mason   Btrfs: put ENOSPC...
708
709
710
  		case Opt_enospc_debug:
  			btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
  			break;
530362934   Qu Wenruo   btrfs: Add noenos...
711
712
713
  		case Opt_noenospc_debug:
  			btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
  			break;
4cb5300bc   Chris Mason   Btrfs: add mount ...
714
  		case Opt_defrag:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
715
  			btrfs_set_and_info(info, AUTO_DEFRAG,
078025347   Qu Wenruo   btrfs: Cleanup th...
716
  					   "enabling auto defrag");
4cb5300bc   Chris Mason   Btrfs: add mount ...
717
  			break;
fc0ca9af1   Qu Wenruo   btrfs: Add noauto...
718
  		case Opt_nodefrag:
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
719
  			btrfs_clear_and_info(info, AUTO_DEFRAG,
078025347   Qu Wenruo   btrfs: Cleanup th...
720
  					     "disabling auto defrag");
fc0ca9af1   Qu Wenruo   btrfs: Add noauto...
721
  			break;
af31f5e5b   Chris Mason   Btrfs: add a log ...
722
  		case Opt_recovery:
8dcddfa04   Qu Wenruo   btrfs: Introduce ...
723
724
725
726
727
728
  			btrfs_warn(root->fs_info,
  				   "'recovery' is deprecated, use 'usebackuproot' instead");
  		case Opt_usebackuproot:
  			btrfs_info(root->fs_info,
  				   "trying to use backup root at mount time");
  			btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
af31f5e5b   Chris Mason   Btrfs: add a log ...
729
  			break;
9555c6c18   Ilya Dryomov   Btrfs: add skip_b...
730
731
732
  		case Opt_skip_balance:
  			btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
  			break;
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
733
734
  #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  		case Opt_check_integrity_including_extent_data:
efe120a06   Frank Holton   Btrfs: convert pr...
735
736
  			btrfs_info(root->fs_info,
  				   "enabling check integrity including extent data");
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
737
738
739
740
741
  			btrfs_set_opt(info->mount_opt,
  				      CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
  			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  			break;
  		case Opt_check_integrity:
efe120a06   Frank Holton   Btrfs: convert pr...
742
  			btrfs_info(root->fs_info, "enabling check integrity");
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
743
744
745
  			btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
  			break;
  		case Opt_check_integrity_print_mask:
2c334e87f   Wang Shilong   Btrfs: add sanity...
746
747
748
749
  			ret = match_int(&args[0], &intarg);
  			if (ret) {
  				goto out;
  			} else if (intarg >= 0) {
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
750
  				info->check_integrity_print_mask = intarg;
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
751
752
753
  				btrfs_info(root->fs_info,
  					   "check_integrity_print_mask 0x%x",
  					   info->check_integrity_print_mask);
2c334e87f   Wang Shilong   Btrfs: add sanity...
754
755
756
  			} else {
  				ret = -EINVAL;
  				goto out;
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
757
758
759
760
761
762
  			}
  			break;
  #else
  		case Opt_check_integrity_including_extent_data:
  		case Opt_check_integrity:
  		case Opt_check_integrity_print_mask:
efe120a06   Frank Holton   Btrfs: convert pr...
763
764
  			btrfs_err(root->fs_info,
  				"support for check_integrity* not compiled in!");
21adbd5cb   Stefan Behrens   Btrfs: integrate ...
765
766
767
  			ret = -EINVAL;
  			goto out;
  #endif
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
768
769
770
771
772
773
774
775
776
777
778
779
  		case Opt_fatal_errors:
  			if (strcmp(args[0].from, "panic") == 0)
  				btrfs_set_opt(info->mount_opt,
  					      PANIC_ON_FATAL_ERROR);
  			else if (strcmp(args[0].from, "bug") == 0)
  				btrfs_clear_opt(info->mount_opt,
  					      PANIC_ON_FATAL_ERROR);
  			else {
  				ret = -EINVAL;
  				goto out;
  			}
  			break;
8b87dc17f   David Sterba   btrfs: add mount ...
780
781
782
783
  		case Opt_commit_interval:
  			intarg = 0;
  			ret = match_int(&args[0], &intarg);
  			if (ret < 0) {
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
784
785
  				btrfs_err(root->fs_info,
  					  "invalid commit interval");
8b87dc17f   David Sterba   btrfs: add mount ...
786
787
788
789
790
  				ret = -EINVAL;
  				goto out;
  			}
  			if (intarg > 0) {
  				if (intarg > 300) {
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
791
792
793
  					btrfs_warn(root->fs_info,
  						"excessive commit interval %d",
  						intarg);
8b87dc17f   David Sterba   btrfs: add mount ...
794
795
796
  				}
  				info->commit_interval = intarg;
  			} else {
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
797
798
799
  				btrfs_info(root->fs_info,
  					   "using default commit interval %ds",
  					   BTRFS_DEFAULT_COMMIT_INTERVAL);
8b87dc17f   David Sterba   btrfs: add mount ...
800
801
802
  				info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  			}
  			break;
d0bd45607   Josef Bacik   Btrfs: add fragme...
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
  #ifdef CONFIG_BTRFS_DEBUG
  		case Opt_fragment_all:
  			btrfs_info(root->fs_info, "fragmenting all space");
  			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  			btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
  			break;
  		case Opt_fragment_metadata:
  			btrfs_info(root->fs_info, "fragmenting metadata");
  			btrfs_set_opt(info->mount_opt,
  				      FRAGMENT_METADATA);
  			break;
  		case Opt_fragment_data:
  			btrfs_info(root->fs_info, "fragmenting data");
  			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
  			break;
  #endif
a7a3f7cad   Sage Weil   Btrfs: fail mount...
819
  		case Opt_err:
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
820
821
  			btrfs_info(root->fs_info,
  				   "unrecognized mount option '%s'", p);
a7a3f7cad   Sage Weil   Btrfs: fail mount...
822
823
  			ret = -EINVAL;
  			goto out;
95e052891   Chris Mason   Btrfs: Use mount ...
824
  		default:
be20aa9db   Chris Mason   Btrfs: Add mount ...
825
  			break;
95e052891   Chris Mason   Btrfs: Use mount ...
826
827
  		}
  	}
96da09192   Qu Wenruo   btrfs: Introduce ...
828
829
830
831
  check:
  	/*
  	 * Extra check for current option against current flag
  	 */
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
832
  	if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) {
96da09192   Qu Wenruo   btrfs: Introduce ...
833
834
835
836
  		btrfs_err(root->fs_info,
  			  "nologreplay must be used with ro mount option");
  		ret = -EINVAL;
  	}
a7a3f7cad   Sage Weil   Btrfs: fail mount...
837
  out:
70f6d82ec   Omar Sandoval   Btrfs: add free s...
838
  	if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) &&
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
839
840
  	    !btrfs_test_opt(info, FREE_SPACE_TREE) &&
  	    !btrfs_test_opt(info, CLEAR_CACHE)) {
70f6d82ec   Omar Sandoval   Btrfs: add free s...
841
842
843
844
  		btrfs_err(root->fs_info, "cannot disable free space tree");
  		ret = -EINVAL;
  
  	}
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
845
  	if (!ret && btrfs_test_opt(info, SPACE_CACHE))
efe120a06   Frank Holton   Btrfs: convert pr...
846
  		btrfs_info(root->fs_info, "disk space caching is enabled");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
847
  	if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
70f6d82ec   Omar Sandoval   Btrfs: add free s...
848
  		btrfs_info(root->fs_info, "using free space tree");
da495ecc0   Josef Bacik   Btrfs: kfree corr...
849
  	kfree(orig);
a7a3f7cad   Sage Weil   Btrfs: fail mount...
850
  	return ret;
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
851
852
853
854
855
856
857
858
  }
  
  /*
   * Parse mount options that are required early in the mount process.
   *
   * All other options will be parsed on much later in the mount process and
   * only when we need to allocate a new super block.
   */
97288f2c7   Christoph Hellwig   Btrfs: corret fmo...
859
  static int btrfs_parse_early_options(const char *options, fmode_t flags,
73f73415c   Josef Bacik   Btrfs: change how...
860
  		void *holder, char **subvol_name, u64 *subvol_objectid,
5e2a4b25d   David Sterba   btrfs: deprecate ...
861
  		struct btrfs_fs_devices **fs_devices)
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
862
863
  {
  	substring_t args[MAX_OPT_ARGS];
83c8c9bde   Jeff Liu   btrfs: trivial fi...
864
  	char *device_name, *opts, *orig, *p;
1493381f2   Wang Shilong   Btrfs: use u64 fo...
865
  	char *num = NULL;
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
866
867
868
  	int error = 0;
  
  	if (!options)
830c4adbd   Josef Bacik   Btrfs: fix how we...
869
  		return 0;
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
870
871
872
873
874
875
876
877
  
  	/*
  	 * strsep changes the string, duplicate it because parse_options
  	 * gets called twice
  	 */
  	opts = kstrdup(options, GFP_KERNEL);
  	if (!opts)
  		return -ENOMEM;
3f3d0bc0d   Tero Roponen   Btrfs: Free corre...
878
  	orig = opts;
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
879
880
881
882
883
884
885
886
887
  
  	while ((p = strsep(&opts, ",")) != NULL) {
  		int token;
  		if (!*p)
  			continue;
  
  		token = match_token(p, tokens, args);
  		switch (token) {
  		case Opt_subvol:
a90e8b6fb   Ilya Dryomov   Btrfs: fix memory...
888
  			kfree(*subvol_name);
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
889
  			*subvol_name = match_strdup(&args[0]);
2c334e87f   Wang Shilong   Btrfs: add sanity...
890
891
892
893
  			if (!*subvol_name) {
  				error = -ENOMEM;
  				goto out;
  			}
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
894
  			break;
73f73415c   Josef Bacik   Btrfs: change how...
895
  		case Opt_subvolid:
1493381f2   Wang Shilong   Btrfs: use u64 fo...
896
897
898
899
  			num = match_strdup(&args[0]);
  			if (num) {
  				*subvol_objectid = memparse(num, NULL);
  				kfree(num);
4849f01d1   Josef Bacik   Btrfs: make subvo...
900
  				/* we want the original fs_tree */
1493381f2   Wang Shilong   Btrfs: use u64 fo...
901
  				if (!*subvol_objectid)
4849f01d1   Josef Bacik   Btrfs: make subvo...
902
903
  					*subvol_objectid =
  						BTRFS_FS_TREE_OBJECTID;
2c334e87f   Wang Shilong   Btrfs: add sanity...
904
905
906
  			} else {
  				error = -EINVAL;
  				goto out;
4849f01d1   Josef Bacik   Btrfs: make subvo...
907
  			}
73f73415c   Josef Bacik   Btrfs: change how...
908
  			break;
e15d05424   Xin Zhong   Btrfs: fix subvol...
909
  		case Opt_subvolrootid:
62e855771   Jeff Mahoney   btrfs: convert pr...
910
911
  			pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect
  ");
e15d05424   Xin Zhong   Btrfs: fix subvol...
912
  			break;
43e570b08   Christoph Hellwig   btrfs: allow scan...
913
  		case Opt_device:
83c8c9bde   Jeff Liu   btrfs: trivial fi...
914
915
916
917
918
919
  			device_name = match_strdup(&args[0]);
  			if (!device_name) {
  				error = -ENOMEM;
  				goto out;
  			}
  			error = btrfs_scan_one_device(device_name,
43e570b08   Christoph Hellwig   btrfs: allow scan...
920
  					flags, holder, fs_devices);
83c8c9bde   Jeff Liu   btrfs: trivial fi...
921
  			kfree(device_name);
43e570b08   Christoph Hellwig   btrfs: allow scan...
922
  			if (error)
830c4adbd   Josef Bacik   Btrfs: fix how we...
923
  				goto out;
43e570b08   Christoph Hellwig   btrfs: allow scan...
924
  			break;
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
925
926
927
928
  		default:
  			break;
  		}
  	}
830c4adbd   Josef Bacik   Btrfs: fix how we...
929
  out:
3f3d0bc0d   Tero Roponen   Btrfs: Free corre...
930
  	kfree(orig);
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
931
  	return error;
95e052891   Chris Mason   Btrfs: Use mount ...
932
  }
05dbe6837   Omar Sandoval   Btrfs: unify subv...
933
934
  static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
  					   u64 subvol_objectid)
73f73415c   Josef Bacik   Btrfs: change how...
935
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
936
  	struct btrfs_root *root = fs_info->tree_root;
05dbe6837   Omar Sandoval   Btrfs: unify subv...
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
  	struct btrfs_root *fs_root;
  	struct btrfs_root_ref *root_ref;
  	struct btrfs_inode_ref *inode_ref;
  	struct btrfs_key key;
  	struct btrfs_path *path = NULL;
  	char *name = NULL, *ptr;
  	u64 dirid;
  	int len;
  	int ret;
  
  	path = btrfs_alloc_path();
  	if (!path) {
  		ret = -ENOMEM;
  		goto err;
  	}
  	path->leave_spinning = 1;
  
  	name = kmalloc(PATH_MAX, GFP_NOFS);
  	if (!name) {
  		ret = -ENOMEM;
  		goto err;
  	}
  	ptr = name + PATH_MAX - 1;
  	ptr[0] = '\0';
73f73415c   Josef Bacik   Btrfs: change how...
961
962
  
  	/*
05dbe6837   Omar Sandoval   Btrfs: unify subv...
963
964
  	 * Walk up the subvolume trees in the tree of tree roots by root
  	 * backrefs until we hit the top-level subvolume.
73f73415c   Josef Bacik   Btrfs: change how...
965
  	 */
05dbe6837   Omar Sandoval   Btrfs: unify subv...
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  	while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
  		key.objectid = subvol_objectid;
  		key.type = BTRFS_ROOT_BACKREF_KEY;
  		key.offset = (u64)-1;
  
  		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  		if (ret < 0) {
  			goto err;
  		} else if (ret > 0) {
  			ret = btrfs_previous_item(root, path, subvol_objectid,
  						  BTRFS_ROOT_BACKREF_KEY);
  			if (ret < 0) {
  				goto err;
  			} else if (ret > 0) {
  				ret = -ENOENT;
  				goto err;
  			}
  		}
  
  		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  		subvol_objectid = key.offset;
  
  		root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  					  struct btrfs_root_ref);
  		len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
  		ptr -= len + 1;
  		if (ptr < name) {
  			ret = -ENAMETOOLONG;
  			goto err;
  		}
  		read_extent_buffer(path->nodes[0], ptr + 1,
  				   (unsigned long)(root_ref + 1), len);
  		ptr[0] = '/';
  		dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
  		btrfs_release_path(path);
  
  		key.objectid = subvol_objectid;
  		key.type = BTRFS_ROOT_ITEM_KEY;
  		key.offset = (u64)-1;
  		fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
  		if (IS_ERR(fs_root)) {
  			ret = PTR_ERR(fs_root);
  			goto err;
  		}
  
  		/*
  		 * Walk up the filesystem tree by inode refs until we hit the
  		 * root directory.
  		 */
  		while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
  			key.objectid = dirid;
  			key.type = BTRFS_INODE_REF_KEY;
  			key.offset = (u64)-1;
  
  			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  			if (ret < 0) {
  				goto err;
  			} else if (ret > 0) {
  				ret = btrfs_previous_item(fs_root, path, dirid,
  							  BTRFS_INODE_REF_KEY);
  				if (ret < 0) {
  					goto err;
  				} else if (ret > 0) {
  					ret = -ENOENT;
  					goto err;
  				}
  			}
  
  			btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  			dirid = key.offset;
  
  			inode_ref = btrfs_item_ptr(path->nodes[0],
  						   path->slots[0],
  						   struct btrfs_inode_ref);
  			len = btrfs_inode_ref_name_len(path->nodes[0],
  						       inode_ref);
  			ptr -= len + 1;
  			if (ptr < name) {
  				ret = -ENAMETOOLONG;
  				goto err;
  			}
  			read_extent_buffer(path->nodes[0], ptr + 1,
  					   (unsigned long)(inode_ref + 1), len);
  			ptr[0] = '/';
  			btrfs_release_path(path);
  		}
73f73415c   Josef Bacik   Btrfs: change how...
1052
  	}
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
  	btrfs_free_path(path);
  	if (ptr == name + PATH_MAX - 1) {
  		name[0] = '/';
  		name[1] = '\0';
  	} else {
  		memmove(name, ptr, name + PATH_MAX - ptr);
  	}
  	return name;
  
  err:
  	btrfs_free_path(path);
  	kfree(name);
  	return ERR_PTR(ret);
  }
  
  static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
  {
  	struct btrfs_root *root = fs_info->tree_root;
  	struct btrfs_dir_item *di;
  	struct btrfs_path *path;
  	struct btrfs_key location;
  	u64 dir_id;
73f73415c   Josef Bacik   Btrfs: change how...
1075
1076
  	path = btrfs_alloc_path();
  	if (!path)
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1077
  		return -ENOMEM;
73f73415c   Josef Bacik   Btrfs: change how...
1078
1079
1080
1081
1082
1083
1084
  	path->leave_spinning = 1;
  
  	/*
  	 * Find the "default" dir item which points to the root item that we
  	 * will mount by default if we haven't been given a specific subvolume
  	 * to mount.
  	 */
815745cf3   Al Viro   btrfs: let ->s_fs...
1085
  	dir_id = btrfs_super_root_dir(fs_info->super_copy);
73f73415c   Josef Bacik   Btrfs: change how...
1086
  	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
b08391663   Julia Lawall   fs/btrfs: Add mis...
1087
1088
  	if (IS_ERR(di)) {
  		btrfs_free_path(path);
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1089
  		return PTR_ERR(di);
b08391663   Julia Lawall   fs/btrfs: Add mis...
1090
  	}
73f73415c   Josef Bacik   Btrfs: change how...
1091
1092
1093
1094
  	if (!di) {
  		/*
  		 * Ok the default dir item isn't there.  This is weird since
  		 * it's always been there, but don't freak out, just try and
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1095
  		 * mount the top-level subvolume.
73f73415c   Josef Bacik   Btrfs: change how...
1096
1097
  		 */
  		btrfs_free_path(path);
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1098
1099
  		*objectid = BTRFS_FS_TREE_OBJECTID;
  		return 0;
73f73415c   Josef Bacik   Btrfs: change how...
1100
1101
1102
1103
  	}
  
  	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  	btrfs_free_path(path);
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1104
1105
  	*objectid = location.objectid;
  	return 0;
73f73415c   Josef Bacik   Btrfs: change how...
1106
  }
d397712bc   Chris Mason   Btrfs: Fix checkp...
1107
  static int btrfs_fill_super(struct super_block *sb,
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
1108
  			    struct btrfs_fs_devices *fs_devices,
d397712bc   Chris Mason   Btrfs: Fix checkp...
1109
  			    void *data, int silent)
75dfe3960   Chris Mason   btrfs_file_write ...
1110
  {
d397712bc   Chris Mason   Btrfs: Fix checkp...
1111
  	struct inode *inode;
815745cf3   Al Viro   btrfs: let ->s_fs...
1112
  	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1113
  	struct btrfs_key key;
39279cc3d   Chris Mason   Btrfs: split up s...
1114
  	int err;
a429e5137   Chris Mason   Btrfs: working fi...
1115

39279cc3d   Chris Mason   Btrfs: split up s...
1116
1117
1118
  	sb->s_maxbytes = MAX_LFS_FILESIZE;
  	sb->s_magic = BTRFS_SUPER_MAGIC;
  	sb->s_op = &btrfs_super_ops;
af53d29ac   Al Viro   switch btrfs, clo...
1119
  	sb->s_d_op = &btrfs_dentry_operations;
be6e8dc0b   Balaji Rao   NFS support for b...
1120
  	sb->s_export_op = &btrfs_export_ops;
5103e947b   Josef Bacik   xattr support for...
1121
  	sb->s_xattr = btrfs_xattr_handlers;
39279cc3d   Chris Mason   Btrfs: split up s...
1122
  	sb->s_time_gran = 1;
0eda294df   Chris Mason   Btrfs: fix btrfs ...
1123
  #ifdef CONFIG_BTRFS_FS_POSIX_ACL
33268eaf0   Josef Bacik   Btrfs: Add ACL su...
1124
  	sb->s_flags |= MS_POSIXACL;
49cf6f452   Chris Ball   Btrfs: Fix settin...
1125
  #endif
0c4d2d95d   Josef Bacik   Btrfs: use i_vers...
1126
  	sb->s_flags |= MS_I_VERSION;
da2f0f74c   Chris Mason   Btrfs: add suppor...
1127
  	sb->s_iflags |= SB_I_CGROUPWB;
ad2b2c802   Al Viro   btrfs: make open_...
1128
1129
  	err = open_ctree(sb, fs_devices, (char *)data);
  	if (err) {
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1130
  		btrfs_err(fs_info, "open_ctree failed");
ad2b2c802   Al Viro   btrfs: make open_...
1131
  		return err;
a429e5137   Chris Mason   Btrfs: working fi...
1132
  	}
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1133
1134
1135
  	key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  	key.type = BTRFS_INODE_ITEM_KEY;
  	key.offset = 0;
98c7089c7   Al Viro   btrfs: preparatio...
1136
  	inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
5d4f98a28   Yan Zheng   Btrfs: Mixed back...
1137
1138
  	if (IS_ERR(inode)) {
  		err = PTR_ERR(inode);
39279cc3d   Chris Mason   Btrfs: split up s...
1139
  		goto fail_close;
f254e52c1   Chris Mason   Btrfs: verify csu...
1140
  	}
f254e52c1   Chris Mason   Btrfs: verify csu...
1141

48fde701a   Al Viro   switch open-coded...
1142
1143
  	sb->s_root = d_make_root(inode);
  	if (!sb->s_root) {
39279cc3d   Chris Mason   Btrfs: split up s...
1144
1145
  		err = -ENOMEM;
  		goto fail_close;
f254e52c1   Chris Mason   Btrfs: verify csu...
1146
  	}
58176a960   Josef Bacik   Btrfs: Add per-ro...
1147

6885f308b   Chris Mason   Btrfs: Misc 2.6.2...
1148
  	save_mount_options(sb, data);
90a887c9a   Dan Magenheimer   btrfs: add cleanc...
1149
  	cleancache_init_fs(sb);
59553edf1   Al Viro   btrfs: consolidat...
1150
  	sb->s_flags |= MS_ACTIVE;
2619ba1f0   Chris Mason   Btrfs: subvolumes
1151
  	return 0;
39279cc3d   Chris Mason   Btrfs: split up s...
1152
1153
  
  fail_close:
815745cf3   Al Viro   btrfs: let ->s_fs...
1154
  	close_ctree(fs_info->tree_root);
39279cc3d   Chris Mason   Btrfs: split up s...
1155
  	return err;
2619ba1f0   Chris Mason   Btrfs: subvolumes
1156
  }
6bf13c0cc   Sage Weil   Btrfs: transactio...
1157
  int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba5   Chris Mason   Btrfs: snapshot p...
1158
1159
  {
  	struct btrfs_trans_handle *trans;
815745cf3   Al Viro   btrfs: let ->s_fs...
1160
1161
  	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  	struct btrfs_root *root = fs_info->tree_root;
2619ba1f0   Chris Mason   Btrfs: subvolumes
1162

bc074524e   Jeff Mahoney   btrfs: prefix fsi...
1163
  	trace_btrfs_sync_fs(fs_info, wait);
1abe9b8a1   liubo   Btrfs: add initia...
1164

39279cc3d   Chris Mason   Btrfs: split up s...
1165
  	if (!wait) {
815745cf3   Al Viro   btrfs: let ->s_fs...
1166
  		filemap_flush(fs_info->btree_inode->i_mapping);
39279cc3d   Chris Mason   Btrfs: split up s...
1167
1168
  		return 0;
  	}
771ed689d   Chris Mason   Btrfs: Optimize c...
1169

578def7c5   Filipe Manana   Btrfs: don't wait...
1170
  	btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
771ed689d   Chris Mason   Btrfs: Optimize c...
1171

d4edf39bd   Miao Xie   Btrfs: fix uncomp...
1172
  	trans = btrfs_attach_transaction_barrier(root);
60376ce4a   Josef Bacik   Btrfs: fix race i...
1173
  	if (IS_ERR(trans)) {
354aa0fb6   Miao Xie   Btrfs: fix orphan...
1174
  		/* no transaction, don't bother */
6b5fe46df   David Sterba   btrfs: do commit ...
1175
1176
1177
1178
1179
1180
1181
  		if (PTR_ERR(trans) == -ENOENT) {
  			/*
  			 * Exit unless we have some pending changes
  			 * that need to go through commit
  			 */
  			if (fs_info->pending_changes == 0)
  				return 0;
a53f4f8e9   Qu Wenruo   btrfs: Don't call...
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
  			/*
  			 * A non-blocking test if the fs is frozen. We must not
  			 * start a new transaction here otherwise a deadlock
  			 * happens. The pending operations are delayed to the
  			 * next commit after thawing.
  			 */
  			if (__sb_start_write(sb, SB_FREEZE_WRITE, false))
  				__sb_end_write(sb, SB_FREEZE_WRITE);
  			else
  				return 0;
6b5fe46df   David Sterba   btrfs: do commit ...
1192
  			trans = btrfs_start_transaction(root, 0);
6b5fe46df   David Sterba   btrfs: do commit ...
1193
  		}
98bd5c547   David Sterba   btrfs: sync ioctl...
1194
1195
  		if (IS_ERR(trans))
  			return PTR_ERR(trans);
60376ce4a   Josef Bacik   Btrfs: fix race i...
1196
  	}
bd7de2c9a   Josef Bacik   Btrfs: fix deadlo...
1197
  	return btrfs_commit_transaction(trans, root);
2c90e5d65   Chris Mason   Btrfs: still corr...
1198
  }
34c80b1d9   Al Viro   vfs: switch ->sho...
1199
  static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
a9572a15a   Eric Paris   Btrfs: introduce ...
1200
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
1201
1202
  	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
  	struct btrfs_root *root = info->tree_root;
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1203
  	char *compress_type;
a9572a15a   Eric Paris   Btrfs: introduce ...
1204

3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1205
  	if (btrfs_test_opt(info, DEGRADED))
a9572a15a   Eric Paris   Btrfs: introduce ...
1206
  		seq_puts(seq, ",degraded");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1207
  	if (btrfs_test_opt(info, NODATASUM))
a9572a15a   Eric Paris   Btrfs: introduce ...
1208
  		seq_puts(seq, ",nodatasum");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1209
  	if (btrfs_test_opt(info, NODATACOW))
a9572a15a   Eric Paris   Btrfs: introduce ...
1210
  		seq_puts(seq, ",nodatacow");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1211
  	if (btrfs_test_opt(info, NOBARRIER))
a9572a15a   Eric Paris   Btrfs: introduce ...
1212
  		seq_puts(seq, ",nobarrier");
95ac567af   Filipe David Borba Manana   Btrfs: set defaul...
1213
  	if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
c1c9ff7c9   Geert Uytterhoeven   Btrfs: Remove sup...
1214
  		seq_printf(seq, ",max_inline=%llu", info->max_inline);
a9572a15a   Eric Paris   Btrfs: introduce ...
1215
  	if (info->alloc_start != 0)
c1c9ff7c9   Geert Uytterhoeven   Btrfs: Remove sup...
1216
  		seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
a9572a15a   Eric Paris   Btrfs: introduce ...
1217
1218
1219
  	if (info->thread_pool_size !=  min_t(unsigned long,
  					     num_online_cpus() + 2, 8))
  		seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1220
  	if (btrfs_test_opt(info, COMPRESS)) {
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1221
1222
1223
1224
  		if (info->compress_type == BTRFS_COMPRESS_ZLIB)
  			compress_type = "zlib";
  		else
  			compress_type = "lzo";
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1225
  		if (btrfs_test_opt(info, FORCE_COMPRESS))
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1226
1227
1228
1229
  			seq_printf(seq, ",compress-force=%s", compress_type);
  		else
  			seq_printf(seq, ",compress=%s", compress_type);
  	}
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1230
  	if (btrfs_test_opt(info, NOSSD))
c289811cc   Chris Mason   Btrfs: autodetect...
1231
  		seq_puts(seq, ",nossd");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1232
  	if (btrfs_test_opt(info, SSD_SPREAD))
451d7585a   Chris Mason   Btrfs: add mount ...
1233
  		seq_puts(seq, ",ssd_spread");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1234
  	else if (btrfs_test_opt(info, SSD))
a9572a15a   Eric Paris   Btrfs: introduce ...
1235
  		seq_puts(seq, ",ssd");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1236
  	if (btrfs_test_opt(info, NOTREELOG))
6b65c5c61   Sage Weil   Btrfs: make show_...
1237
  		seq_puts(seq, ",notreelog");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1238
  	if (btrfs_test_opt(info, NOLOGREPLAY))
96da09192   Qu Wenruo   btrfs: Introduce ...
1239
  		seq_puts(seq, ",nologreplay");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1240
  	if (btrfs_test_opt(info, FLUSHONCOMMIT))
6b65c5c61   Sage Weil   Btrfs: make show_...
1241
  		seq_puts(seq, ",flushoncommit");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1242
  	if (btrfs_test_opt(info, DISCARD))
20a5239a5   Matthew Wilcox   Btrfs: Show disca...
1243
  		seq_puts(seq, ",discard");
a9572a15a   Eric Paris   Btrfs: introduce ...
1244
1245
  	if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
  		seq_puts(seq, ",noacl");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1246
  	if (btrfs_test_opt(info, SPACE_CACHE))
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1247
  		seq_puts(seq, ",space_cache");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1248
  	else if (btrfs_test_opt(info, FREE_SPACE_TREE))
70f6d82ec   Omar Sandoval   Btrfs: add free s...
1249
  		seq_puts(seq, ",space_cache=v2");
73bc18768   Josef Bacik   Btrfs: introduce ...
1250
  	else
8965593e4   David Sterba   btrfs: rename the...
1251
  		seq_puts(seq, ",nospace_cache");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1252
  	if (btrfs_test_opt(info, RESCAN_UUID_TREE))
f420ee1e9   Stefan Behrens   Btrfs: add mount ...
1253
  		seq_puts(seq, ",rescan_uuid_tree");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1254
  	if (btrfs_test_opt(info, CLEAR_CACHE))
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1255
  		seq_puts(seq, ",clear_cache");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1256
  	if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
200da64e0   Tsutomu Itoh   Btrfs: fix /proc/...
1257
  		seq_puts(seq, ",user_subvol_rm_allowed");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1258
  	if (btrfs_test_opt(info, ENOSPC_DEBUG))
0942caa37   David Sterba   btrfs: add missin...
1259
  		seq_puts(seq, ",enospc_debug");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1260
  	if (btrfs_test_opt(info, AUTO_DEFRAG))
0942caa37   David Sterba   btrfs: add missin...
1261
  		seq_puts(seq, ",autodefrag");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1262
  	if (btrfs_test_opt(info, INODE_MAP_CACHE))
0942caa37   David Sterba   btrfs: add missin...
1263
  		seq_puts(seq, ",inode_cache");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1264
  	if (btrfs_test_opt(info, SKIP_BALANCE))
9555c6c18   Ilya Dryomov   Btrfs: add skip_b...
1265
  		seq_puts(seq, ",skip_balance");
8507d216a   Wang Shilong   Btrfs: add missin...
1266
  #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1267
  	if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
8507d216a   Wang Shilong   Btrfs: add missin...
1268
  		seq_puts(seq, ",check_int_data");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1269
  	else if (btrfs_test_opt(info, CHECK_INTEGRITY))
8507d216a   Wang Shilong   Btrfs: add missin...
1270
1271
1272
1273
1274
1275
1276
1277
  		seq_puts(seq, ",check_int");
  	if (info->check_integrity_print_mask)
  		seq_printf(seq, ",check_int_print_mask=%d",
  				info->check_integrity_print_mask);
  #endif
  	if (info->metadata_ratio)
  		seq_printf(seq, ",metadata_ratio=%d",
  				info->metadata_ratio);
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1278
  	if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
8c3429300   Jeff Mahoney   btrfs: Add btrfs_...
1279
  		seq_puts(seq, ",fatal_errors=panic");
8b87dc17f   David Sterba   btrfs: add mount ...
1280
1281
  	if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
  		seq_printf(seq, ",commit=%d", info->commit_interval);
d0bd45607   Josef Bacik   Btrfs: add fragme...
1282
  #ifdef CONFIG_BTRFS_DEBUG
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1283
  	if (btrfs_test_opt(info, FRAGMENT_DATA))
d0bd45607   Josef Bacik   Btrfs: add fragme...
1284
  		seq_puts(seq, ",fragment=data");
3cdde2240   Jeff Mahoney   btrfs: btrfs_test...
1285
  	if (btrfs_test_opt(info, FRAGMENT_METADATA))
d0bd45607   Josef Bacik   Btrfs: add fragme...
1286
1287
  		seq_puts(seq, ",fragment=metadata");
  #endif
c8d3fe028   Omar Sandoval   Btrfs: show subvo...
1288
1289
1290
1291
1292
  	seq_printf(seq, ",subvolid=%llu",
  		  BTRFS_I(d_inode(dentry))->root->root_key.objectid);
  	seq_puts(seq, ",subvol=");
  	seq_dentry(seq, dentry, " \t
  \\");
a9572a15a   Eric Paris   Btrfs: introduce ...
1293
1294
  	return 0;
  }
a061fc8da   Chris Mason   Btrfs: Add suppor...
1295
  static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4a   Yan   Btrfs: Add mount ...
1296
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
1297
1298
  	struct btrfs_fs_info *p = data;
  	struct btrfs_fs_info *fs_info = btrfs_sb(s);
4b82d6e4a   Yan   Btrfs: Add mount ...
1299

815745cf3   Al Viro   btrfs: let ->s_fs...
1300
  	return fs_info->fs_devices == p->fs_devices;
4b82d6e4a   Yan   Btrfs: Add mount ...
1301
  }
450ba0ea0   Josef Bacik   Btrfs: setup blan...
1302
1303
  static int btrfs_set_super(struct super_block *s, void *data)
  {
6de1d09d9   Al Viro   btrfs: fix mount/...
1304
1305
1306
1307
  	int err = set_anon_super(s, data);
  	if (!err)
  		s->s_fs_info = data;
  	return err;
4b82d6e4a   Yan   Btrfs: Add mount ...
1308
  }
830c4adbd   Josef Bacik   Btrfs: fix how we...
1309
  /*
f9d9ef62c   David Sterba   btrfs: do not all...
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
   * subvolumes are identified by ino 256
   */
  static inline int is_subvolume_inode(struct inode *inode)
  {
  	if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  		return 1;
  	return 0;
  }
  
  /*
e6e4dbe89   Omar Sandoval   Btrfs: remove all...
1320
1321
1322
   * This will add subvolid=0 to the argument string while removing any subvol=
   * and subvolid= arguments to make sure we get the top-level root for path
   * walking to the subvol we want.
830c4adbd   Josef Bacik   Btrfs: fix how we...
1323
1324
1325
   */
  static char *setup_root_args(char *args)
  {
e6e4dbe89   Omar Sandoval   Btrfs: remove all...
1326
  	char *buf, *dst, *sep;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1327

e6e4dbe89   Omar Sandoval   Btrfs: remove all...
1328
1329
  	if (!args)
  		return kstrdup("subvolid=0", GFP_NOFS);
f60d16a89   Jim Meyering   Btrfs: avoid buff...
1330

e6e4dbe89   Omar Sandoval   Btrfs: remove all...
1331
1332
  	/* The worst case is that we add ",subvolid=0" to the end. */
  	buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS);
f60d16a89   Jim Meyering   Btrfs: avoid buff...
1333
  	if (!buf)
830c4adbd   Josef Bacik   Btrfs: fix how we...
1334
  		return NULL;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1335

e6e4dbe89   Omar Sandoval   Btrfs: remove all...
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
  	while (1) {
  		sep = strchrnul(args, ',');
  		if (!strstarts(args, "subvol=") &&
  		    !strstarts(args, "subvolid=")) {
  			memcpy(dst, args, sep - args);
  			dst += sep - args;
  			*dst++ = ',';
  		}
  		if (*sep)
  			args = sep + 1;
  		else
  			break;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1348
  	}
f60d16a89   Jim Meyering   Btrfs: avoid buff...
1349
  	strcpy(dst, "subvolid=0");
830c4adbd   Josef Bacik   Btrfs: fix how we...
1350

f60d16a89   Jim Meyering   Btrfs: avoid buff...
1351
  	return buf;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1352
  }
bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1353
1354
1355
  static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
  				   int flags, const char *device_name,
  				   char *data)
830c4adbd   Josef Bacik   Btrfs: fix how we...
1356
  {
830c4adbd   Josef Bacik   Btrfs: fix how we...
1357
  	struct dentry *root;
fa3306595   Omar Sandoval   Btrfs: clean up e...
1358
  	struct vfsmount *mnt = NULL;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1359
  	char *newargs;
fa3306595   Omar Sandoval   Btrfs: clean up e...
1360
  	int ret;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1361
1362
  
  	newargs = setup_root_args(data);
fa3306595   Omar Sandoval   Btrfs: clean up e...
1363
1364
1365
1366
  	if (!newargs) {
  		root = ERR_PTR(-ENOMEM);
  		goto out;
  	}
0723a0473   Harald Hoyer   btrfs: allow moun...
1367

fa3306595   Omar Sandoval   Btrfs: clean up e...
1368
1369
  	mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, newargs);
  	if (PTR_ERR_OR_ZERO(mnt) == -EBUSY) {
0723a0473   Harald Hoyer   btrfs: allow moun...
1370
  		if (flags & MS_RDONLY) {
fa3306595   Omar Sandoval   Btrfs: clean up e...
1371
1372
  			mnt = vfs_kern_mount(&btrfs_fs_type, flags & ~MS_RDONLY,
  					     device_name, newargs);
0723a0473   Harald Hoyer   btrfs: allow moun...
1373
  		} else {
fa3306595   Omar Sandoval   Btrfs: clean up e...
1374
1375
  			mnt = vfs_kern_mount(&btrfs_fs_type, flags | MS_RDONLY,
  					     device_name, newargs);
0040e606e   Christoph Jaeger   btrfs: fix use-af...
1376
  			if (IS_ERR(mnt)) {
fa3306595   Omar Sandoval   Btrfs: clean up e...
1377
1378
1379
  				root = ERR_CAST(mnt);
  				mnt = NULL;
  				goto out;
0040e606e   Christoph Jaeger   btrfs: fix use-af...
1380
  			}
0723a0473   Harald Hoyer   btrfs: allow moun...
1381

773cd04ec   Omar Sandoval   Btrfs: lock super...
1382
  			down_write(&mnt->mnt_sb->s_umount);
fa3306595   Omar Sandoval   Btrfs: clean up e...
1383
  			ret = btrfs_remount(mnt->mnt_sb, &flags, NULL);
773cd04ec   Omar Sandoval   Btrfs: lock super...
1384
  			up_write(&mnt->mnt_sb->s_umount);
fa3306595   Omar Sandoval   Btrfs: clean up e...
1385
1386
1387
  			if (ret < 0) {
  				root = ERR_PTR(ret);
  				goto out;
0723a0473   Harald Hoyer   btrfs: allow moun...
1388
1389
1390
  			}
  		}
  	}
fa3306595   Omar Sandoval   Btrfs: clean up e...
1391
1392
1393
1394
1395
  	if (IS_ERR(mnt)) {
  		root = ERR_CAST(mnt);
  		mnt = NULL;
  		goto out;
  	}
830c4adbd   Josef Bacik   Btrfs: fix how we...
1396

05dbe6837   Omar Sandoval   Btrfs: unify subv...
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
  	if (!subvol_name) {
  		if (!subvol_objectid) {
  			ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
  							  &subvol_objectid);
  			if (ret) {
  				root = ERR_PTR(ret);
  				goto out;
  			}
  		}
  		subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb),
  							    subvol_objectid);
  		if (IS_ERR(subvol_name)) {
  			root = ERR_CAST(subvol_name);
  			subvol_name = NULL;
  			goto out;
  		}
  
  	}
ea441d110   Al Viro   new helper: mount...
1415
  	root = mount_subtree(mnt, subvol_name);
fa3306595   Omar Sandoval   Btrfs: clean up e...
1416
1417
  	/* mount_subtree() drops our reference on the vfsmount. */
  	mnt = NULL;
830c4adbd   Josef Bacik   Btrfs: fix how we...
1418

bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1419
  	if (!IS_ERR(root)) {
ea441d110   Al Viro   new helper: mount...
1420
  		struct super_block *s = root->d_sb;
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1421
  		struct btrfs_fs_info *fs_info = btrfs_sb(s);
bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1422
1423
1424
1425
1426
  		struct inode *root_inode = d_inode(root);
  		u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
  
  		ret = 0;
  		if (!is_subvolume_inode(root_inode)) {
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1427
  			btrfs_err(fs_info, "'%s' is not a valid subvolume",
bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1428
1429
1430
1431
  			       subvol_name);
  			ret = -EINVAL;
  		}
  		if (subvol_objectid && root_objectid != subvol_objectid) {
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1432
1433
1434
1435
1436
  			/*
  			 * This will also catch a race condition where a
  			 * subvolume which was passed by ID is renamed and
  			 * another subvolume is renamed over the old location.
  			 */
ab8d0fc48   Jeff Mahoney   btrfs: convert pr...
1437
1438
1439
  			btrfs_err(fs_info,
  				  "subvol '%s' does not match subvolid %llu",
  				  subvol_name, subvol_objectid);
bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1440
1441
1442
1443
1444
1445
1446
  			ret = -EINVAL;
  		}
  		if (ret) {
  			dput(root);
  			root = ERR_PTR(ret);
  			deactivate_locked_super(s);
  		}
f9d9ef62c   David Sterba   btrfs: do not all...
1447
  	}
fa3306595   Omar Sandoval   Btrfs: clean up e...
1448
1449
1450
1451
  out:
  	mntput(mnt);
  	kfree(newargs);
  	kfree(subvol_name);
830c4adbd   Josef Bacik   Btrfs: fix how we...
1452
1453
  	return root;
  }
450ba0ea0   Josef Bacik   Btrfs: setup blan...
1454

f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
  static int parse_security_options(char *orig_opts,
  				  struct security_mnt_opts *sec_opts)
  {
  	char *secdata = NULL;
  	int ret = 0;
  
  	secdata = alloc_secdata();
  	if (!secdata)
  		return -ENOMEM;
  	ret = security_sb_copy_data(orig_opts, secdata);
  	if (ret) {
  		free_secdata(secdata);
  		return ret;
  	}
  	ret = security_sb_parse_opts_str(secdata, sec_opts);
  	free_secdata(secdata);
  	return ret;
  }
  
  static int setup_security_options(struct btrfs_fs_info *fs_info,
  				  struct super_block *sb,
  				  struct security_mnt_opts *sec_opts)
  {
  	int ret = 0;
  
  	/*
  	 * Call security_sb_set_mnt_opts() to check whether new sec_opts
  	 * is valid.
  	 */
  	ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL);
  	if (ret)
  		return ret;
a43bb39b5   Qu Wenruo   btrfs: Fix compil...
1487
  #ifdef CONFIG_SECURITY
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1488
1489
1490
1491
1492
  	if (!fs_info->security_opts.num_mnt_opts) {
  		/* first time security setup, copy sec_opts to fs_info */
  		memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts));
  	} else {
  		/*
180e4d470   Luis de Bethencourt   btrfs: fix typos ...
1493
1494
1495
1496
  		 * Since SELinux (the only one supporting security_mnt_opts)
  		 * does NOT support changing context during remount/mount of
  		 * the same sb, this must be the same or part of the same
  		 * security options, just free it.
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1497
1498
1499
  		 */
  		security_free_mnt_opts(sec_opts);
  	}
a43bb39b5   Qu Wenruo   btrfs: Fix compil...
1500
  #endif
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1501
1502
  	return ret;
  }
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
1503
1504
1505
1506
1507
1508
  /*
   * Find a superblock for the given device / mount point.
   *
   * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
   *	  for multiple device setup.  Make sure to keep it in sync.
   */
061dbc6b9   Al Viro   convert btrfs
1509
  static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
306e16ce1   David Sterba   btrfs: rename var...
1510
  		const char *device_name, void *data)
4b82d6e4a   Yan   Btrfs: Add mount ...
1511
1512
1513
  {
  	struct block_device *bdev = NULL;
  	struct super_block *s;
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
1514
  	struct btrfs_fs_devices *fs_devices = NULL;
450ba0ea0   Josef Bacik   Btrfs: setup blan...
1515
  	struct btrfs_fs_info *fs_info = NULL;
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1516
  	struct security_mnt_opts new_sec_opts;
97288f2c7   Christoph Hellwig   Btrfs: corret fmo...
1517
  	fmode_t mode = FMODE_READ;
73f73415c   Josef Bacik   Btrfs: change how...
1518
1519
  	char *subvol_name = NULL;
  	u64 subvol_objectid = 0;
4b82d6e4a   Yan   Btrfs: Add mount ...
1520
  	int error = 0;
97288f2c7   Christoph Hellwig   Btrfs: corret fmo...
1521
1522
1523
1524
  	if (!(flags & MS_RDONLY))
  		mode |= FMODE_WRITE;
  
  	error = btrfs_parse_early_options(data, mode, fs_type,
73f73415c   Josef Bacik   Btrfs: change how...
1525
  					  &subvol_name, &subvol_objectid,
5e2a4b25d   David Sterba   btrfs: deprecate ...
1526
  					  &fs_devices);
f23c8af8c   Ilya Dryomov   Btrfs: fix subvol...
1527
1528
  	if (error) {
  		kfree(subvol_name);
061dbc6b9   Al Viro   convert btrfs
1529
  		return ERR_PTR(error);
f23c8af8c   Ilya Dryomov   Btrfs: fix subvol...
1530
  	}
edf24abe5   Christoph Hellwig   btrfs: sanity mou...
1531

05dbe6837   Omar Sandoval   Btrfs: unify subv...
1532
  	if (subvol_name || subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
fa3306595   Omar Sandoval   Btrfs: clean up e...
1533
  		/* mount_subvol() will free subvol_name. */
bb289b7be   Omar Sandoval   Btrfs: fail on mi...
1534
1535
  		return mount_subvol(subvol_name, subvol_objectid, flags,
  				    device_name, data);
830c4adbd   Josef Bacik   Btrfs: fix how we...
1536
  	}
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1537
1538
1539
1540
1541
1542
  	security_init_mnt_opts(&new_sec_opts);
  	if (data) {
  		error = parse_security_options(data, &new_sec_opts);
  		if (error)
  			return ERR_PTR(error);
  	}
306e16ce1   David Sterba   btrfs: rename var...
1543
  	error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
1544
  	if (error)
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1545
  		goto error_sec_opts;
4b82d6e4a   Yan   Btrfs: Add mount ...
1546

450ba0ea0   Josef Bacik   Btrfs: setup blan...
1547
1548
1549
1550
1551
1552
1553
  	/*
  	 * Setup a dummy root and fs_info for test/set super.  This is because
  	 * we don't actually fill this stuff out until open_ctree, but we need
  	 * it for searching for existing supers, so this lets us do that and
  	 * then open_ctree will properly initialize everything later.
  	 */
  	fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1554
1555
1556
1557
  	if (!fs_info) {
  		error = -ENOMEM;
  		goto error_sec_opts;
  	}
04d21a244   Ilya Dryomov   Btrfs: rework err...
1558

450ba0ea0   Josef Bacik   Btrfs: setup blan...
1559
  	fs_info->fs_devices = fs_devices;
450ba0ea0   Josef Bacik   Btrfs: setup blan...
1560

6c41761fc   David Sterba   btrfs: separate s...
1561
1562
  	fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
  	fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1563
  	security_init_mnt_opts(&fs_info->security_opts);
6c41761fc   David Sterba   btrfs: separate s...
1564
1565
  	if (!fs_info->super_copy || !fs_info->super_for_commit) {
  		error = -ENOMEM;
04d21a244   Ilya Dryomov   Btrfs: rework err...
1566
1567
1568
1569
1570
1571
1572
1573
1574
  		goto error_fs_info;
  	}
  
  	error = btrfs_open_devices(fs_devices, mode, fs_type);
  	if (error)
  		goto error_fs_info;
  
  	if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
  		error = -EACCES;
6c41761fc   David Sterba   btrfs: separate s...
1575
1576
  		goto error_close_devices;
  	}
dfe250206   Chris Mason   Btrfs: Add mount ...
1577
  	bdev = fs_devices->latest_bdev;
9249e17fe   David Howells   VFS: Pass mount f...
1578
1579
  	s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
  		 fs_info);
830c4adbd   Josef Bacik   Btrfs: fix how we...
1580
1581
1582
1583
  	if (IS_ERR(s)) {
  		error = PTR_ERR(s);
  		goto error_close_devices;
  	}
4b82d6e4a   Yan   Btrfs: Add mount ...
1584
1585
  
  	if (s->s_root) {
2b82032c3   Yan Zheng   Btrfs: Seed devic...
1586
  		btrfs_close_devices(fs_devices);
6c41761fc   David Sterba   btrfs: separate s...
1587
  		free_fs_info(fs_info);
59553edf1   Al Viro   btrfs: consolidat...
1588
1589
  		if ((flags ^ s->s_flags) & MS_RDONLY)
  			error = -EBUSY;
4b82d6e4a   Yan   Btrfs: Add mount ...
1590
  	} else {
a1c6f0573   Dmitry Monakhov   fs: use block_dev...
1591
  		snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
815745cf3   Al Viro   btrfs: let ->s_fs...
1592
  		btrfs_sb(s)->bdev_holder = fs_type;
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
1593
1594
  		error = btrfs_fill_super(s, fs_devices, data,
  					 flags & MS_SILENT ? 1 : 0);
4b82d6e4a   Yan   Btrfs: Add mount ...
1595
  	}
05dbe6837   Omar Sandoval   Btrfs: unify subv...
1596
  	if (error) {
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1597
  		deactivate_locked_super(s);
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1598
1599
1600
1601
1602
1603
  		goto error_sec_opts;
  	}
  
  	fs_info = btrfs_sb(s);
  	error = setup_security_options(fs_info, s, &new_sec_opts);
  	if (error) {
830c4adbd   Josef Bacik   Btrfs: fix how we...
1604
  		deactivate_locked_super(s);
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1605
1606
  		goto error_sec_opts;
  	}
4b82d6e4a   Yan   Btrfs: Add mount ...
1607

05dbe6837   Omar Sandoval   Btrfs: unify subv...
1608
  	return dget(s->s_root);
4b82d6e4a   Yan   Btrfs: Add mount ...
1609

c146afad2   Yan Zheng   Btrfs: mount ro a...
1610
  error_close_devices:
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
1611
  	btrfs_close_devices(fs_devices);
04d21a244   Ilya Dryomov   Btrfs: rework err...
1612
  error_fs_info:
6c41761fc   David Sterba   btrfs: separate s...
1613
  	free_fs_info(fs_info);
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1614
1615
  error_sec_opts:
  	security_free_mnt_opts(&new_sec_opts);
061dbc6b9   Al Viro   convert btrfs
1616
  	return ERR_PTR(error);
4b82d6e4a   Yan   Btrfs: Add mount ...
1617
  }
2e635a278   Chris Mason   Btrfs: initial mo...
1618

0d2450abf   Sergei Trofimovich   btrfs: allow chan...
1619
1620
1621
1622
1623
1624
1625
  static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
  				     int new_pool_size, int old_pool_size)
  {
  	if (new_pool_size == old_pool_size)
  		return;
  
  	fs_info->thread_pool_size = new_pool_size;
efe120a06   Frank Holton   Btrfs: convert pr...
1626
  	btrfs_info(fs_info, "resize thread pool %d -> %d",
0d2450abf   Sergei Trofimovich   btrfs: allow chan...
1627
  	       old_pool_size, new_pool_size);
5cdc7ad33   Qu Wenruo   btrfs: Replace fs...
1628
  	btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
afe3d2426   Qu Wenruo   btrfs: Replace fs...
1629
  	btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
a8c93d4ef   Qu Wenruo   btrfs: Replace fs...
1630
  	btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
e66f0bb14   Qu Wenruo   btrfs: Replace fs...
1631
  	btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
fccb5d86d   Qu Wenruo   btrfs: Replace fs...
1632
1633
1634
1635
1636
1637
  	btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
  	btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
  	btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
  				new_pool_size);
  	btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
  	btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
5b3bc44e2   Qu Wenruo   btrfs: Replace fs...
1638
  	btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
736cfa15e   Qu Wenruo   btrfs: Replace fs...
1639
  	btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
0339ef2f4   Qu Wenruo   btrfs: Replace fs...
1640
1641
  	btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
  				new_pool_size);
0d2450abf   Sergei Trofimovich   btrfs: allow chan...
1642
  }
f42a34b2f   Miao Xie   Btrfs: fix unbloc...
1643
  static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1644
1645
  {
  	set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
f42a34b2f   Miao Xie   Btrfs: fix unbloc...
1646
  }
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1647

f42a34b2f   Miao Xie   Btrfs: fix unbloc...
1648
1649
1650
  static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
  				       unsigned long old_opts, int flags)
  {
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
  	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  	     (flags & MS_RDONLY))) {
  		/* wait for any defraggers to finish */
  		wait_event(fs_info->transaction_wait,
  			   (atomic_read(&fs_info->defrag_running) == 0));
  		if (flags & MS_RDONLY)
  			sync_filesystem(fs_info->sb);
  	}
  }
  
  static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
  					 unsigned long old_opts)
  {
  	/*
180e4d470   Luis de Bethencourt   btrfs: fix typos ...
1666
1667
  	 * We need to cleanup all defragable inodes if the autodefragment is
  	 * close or the filesystem is read only.
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1668
1669
1670
1671
1672
1673
1674
1675
1676
  	 */
  	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
  	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
  	     (fs_info->sb->s_flags & MS_RDONLY))) {
  		btrfs_cleanup_defrag_inodes(fs_info);
  	}
  
  	clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
  }
c146afad2   Yan Zheng   Btrfs: mount ro a...
1677
1678
  static int btrfs_remount(struct super_block *sb, int *flags, char *data)
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
1679
1680
  	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  	struct btrfs_root *root = fs_info->tree_root;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1681
1682
1683
1684
1685
1686
1687
  	unsigned old_flags = sb->s_flags;
  	unsigned long old_opts = fs_info->mount_opt;
  	unsigned long old_compress_type = fs_info->compress_type;
  	u64 old_max_inline = fs_info->max_inline;
  	u64 old_alloc_start = fs_info->alloc_start;
  	int old_thread_pool_size = fs_info->thread_pool_size;
  	unsigned int old_metadata_ratio = fs_info->metadata_ratio;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1688
  	int ret;
02b9984d6   Theodore Ts'o   fs: push sync_fil...
1689
  	sync_filesystem(sb);
f42a34b2f   Miao Xie   Btrfs: fix unbloc...
1690
  	btrfs_remount_prepare(fs_info);
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1691

f667aef6a   Qu Wenruo   btrfs: Make btrfs...
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
  	if (data) {
  		struct security_mnt_opts new_sec_opts;
  
  		security_init_mnt_opts(&new_sec_opts);
  		ret = parse_security_options(data, &new_sec_opts);
  		if (ret)
  			goto restore;
  		ret = setup_security_options(fs_info, sb,
  					     &new_sec_opts);
  		if (ret) {
  			security_free_mnt_opts(&new_sec_opts);
  			goto restore;
  		}
  	}
96da09192   Qu Wenruo   btrfs: Introduce ...
1706
  	ret = btrfs_parse_options(root, data, *flags);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1707
1708
1709
1710
  	if (ret) {
  		ret = -EINVAL;
  		goto restore;
  	}
b288052e1   Chris Mason   Btrfs: process mo...
1711

f42a34b2f   Miao Xie   Btrfs: fix unbloc...
1712
  	btrfs_remount_begin(fs_info, old_opts, *flags);
0d2450abf   Sergei Trofimovich   btrfs: allow chan...
1713
1714
  	btrfs_resize_thread_pool(fs_info,
  		fs_info->thread_pool_size, old_thread_pool_size);
c146afad2   Yan Zheng   Btrfs: mount ro a...
1715
  	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1716
  		goto out;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1717
1718
  
  	if (*flags & MS_RDONLY) {
8dabb7420   Stefan Behrens   Btrfs: change cor...
1719
1720
1721
1722
  		/*
  		 * this also happens on 'umount -rf' or on shutdown, when
  		 * the filesystem is busy.
  		 */
21c7e7565   Miao Xie   Btrfs: reclaim th...
1723
  		cancel_work_sync(&fs_info->async_reclaim_work);
361c093d7   Stefan Behrens   Btrfs: Wait for u...
1724
1725
1726
1727
1728
  
  		/* wait for the uuid_scan task to finish */
  		down(&fs_info->uuid_tree_rescan_sem);
  		/* avoid complains from lockdep et al. */
  		up(&fs_info->uuid_tree_rescan_sem);
c146afad2   Yan Zheng   Btrfs: mount ro a...
1729
  		sb->s_flags |= MS_RDONLY;
e44163e17   Jeff Mahoney   btrfs: explictly ...
1730
1731
1732
1733
1734
1735
1736
  		/*
  		 * Setting MS_RDONLY will put the cleaner thread to
  		 * sleep at the next loop if it's already active.
  		 * If it's already asleep, we'll leave unused block
  		 * groups on disk until we're mounted read-write again
  		 * unless we clean them up here.
  		 */
e44163e17   Jeff Mahoney   btrfs: explictly ...
1737
  		btrfs_delete_unused_bgs(fs_info);
e44163e17   Jeff Mahoney   btrfs: explictly ...
1738

8dabb7420   Stefan Behrens   Btrfs: change cor...
1739
1740
  		btrfs_dev_replace_suspend_for_unmount(fs_info);
  		btrfs_scrub_cancel(fs_info);
061594ef1   Miao Xie   Btrfs: pause the ...
1741
  		btrfs_pause_balance(fs_info);
8dabb7420   Stefan Behrens   Btrfs: change cor...
1742

49b25e054   Jeff Mahoney   btrfs: enhance tr...
1743
1744
1745
  		ret = btrfs_commit_super(root);
  		if (ret)
  			goto restore;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1746
  	} else {
6ef3de9c9   David Sterba   btrfs: refuse to ...
1747
1748
  		if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
  			btrfs_err(fs_info,
efe120a06   Frank Holton   Btrfs: convert pr...
1749
  				"Remounting read-write after error is not allowed");
6ef3de9c9   David Sterba   btrfs: refuse to ...
1750
1751
1752
  			ret = -EINVAL;
  			goto restore;
  		}
8a3db1849   Sergei Trofimovich   btrfs: fix early ...
1753
  		if (fs_info->fs_devices->rw_devices == 0) {
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1754
1755
  			ret = -EACCES;
  			goto restore;
8a3db1849   Sergei Trofimovich   btrfs: fix early ...
1756
  		}
2b82032c3   Yan Zheng   Btrfs: Seed devic...
1757

292fd7fc3   Stefan Behrens   Btrfs: don't allo...
1758
1759
1760
  		if (fs_info->fs_devices->missing_devices >
  		     fs_info->num_tolerated_disk_barrier_failures &&
  		    !(*flags & MS_RDONLY)) {
efe120a06   Frank Holton   Btrfs: convert pr...
1761
1762
  			btrfs_warn(fs_info,
  				"too many missing devices, writeable remount is not allowed");
292fd7fc3   Stefan Behrens   Btrfs: don't allo...
1763
1764
1765
  			ret = -EACCES;
  			goto restore;
  		}
8a3db1849   Sergei Trofimovich   btrfs: fix early ...
1766
  		if (btrfs_super_log_root(fs_info->super_copy) != 0) {
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1767
1768
  			ret = -EINVAL;
  			goto restore;
8a3db1849   Sergei Trofimovich   btrfs: fix early ...
1769
  		}
c146afad2   Yan Zheng   Btrfs: mount ro a...
1770

815745cf3   Al Viro   btrfs: let ->s_fs...
1771
  		ret = btrfs_cleanup_fs_roots(fs_info);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1772
1773
  		if (ret)
  			goto restore;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1774

d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1775
  		/* recover relocation */
5f3164813   Wang Shilong   Btrfs: fix race b...
1776
  		mutex_lock(&fs_info->cleaner_mutex);
d68fc57b7   Yan, Zheng   Btrfs: Metadata r...
1777
  		ret = btrfs_recover_relocation(root);
5f3164813   Wang Shilong   Btrfs: fix race b...
1778
  		mutex_unlock(&fs_info->cleaner_mutex);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1779
1780
  		if (ret)
  			goto restore;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1781

2b6ba629b   Ilya Dryomov   Btrfs: resume bal...
1782
1783
1784
  		ret = btrfs_resume_balance_async(fs_info);
  		if (ret)
  			goto restore;
8dabb7420   Stefan Behrens   Btrfs: change cor...
1785
1786
  		ret = btrfs_resume_dev_replace_async(fs_info);
  		if (ret) {
efe120a06   Frank Holton   Btrfs: convert pr...
1787
  			btrfs_warn(fs_info, "failed to resume dev_replace");
8dabb7420   Stefan Behrens   Btrfs: change cor...
1788
1789
  			goto restore;
  		}
94aebfb2e   Josef Bacik   Btrfs: create the...
1790
1791
  
  		if (!fs_info->uuid_root) {
efe120a06   Frank Holton   Btrfs: convert pr...
1792
  			btrfs_info(fs_info, "creating UUID tree");
94aebfb2e   Josef Bacik   Btrfs: create the...
1793
1794
  			ret = btrfs_create_uuid_tree(fs_info);
  			if (ret) {
5d163e0e6   Jeff Mahoney   btrfs: unsplit pr...
1795
1796
1797
  				btrfs_warn(fs_info,
  					   "failed to create the UUID tree %d",
  					   ret);
94aebfb2e   Josef Bacik   Btrfs: create the...
1798
1799
1800
  				goto restore;
  			}
  		}
c146afad2   Yan Zheng   Btrfs: mount ro a...
1801
  		sb->s_flags &= ~MS_RDONLY;
90c711ab3   Zygo Blaxell   btrfs: avoid bloc...
1802

afcdd129e   Josef Bacik   Btrfs: add a flag...
1803
  		set_bit(BTRFS_FS_OPEN, &fs_info->flags);
c146afad2   Yan Zheng   Btrfs: mount ro a...
1804
  	}
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1805
  out:
2c6a92b00   Justin Maggard   btrfs: wake up tr...
1806
  	wake_up_process(fs_info->transaction_kthread);
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1807
  	btrfs_remount_cleanup(fs_info, old_opts);
c146afad2   Yan Zheng   Btrfs: mount ro a...
1808
  	return 0;
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1809
1810
1811
1812
1813
1814
1815
1816
1817
  
  restore:
  	/* We've hit an error - don't reset MS_RDONLY */
  	if (sb->s_flags & MS_RDONLY)
  		old_flags |= MS_RDONLY;
  	sb->s_flags = old_flags;
  	fs_info->mount_opt = old_opts;
  	fs_info->compress_type = old_compress_type;
  	fs_info->max_inline = old_max_inline;
c018daece   Miao Xie   Btrfs: protect fs...
1818
  	mutex_lock(&fs_info->chunk_mutex);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1819
  	fs_info->alloc_start = old_alloc_start;
c018daece   Miao Xie   Btrfs: protect fs...
1820
  	mutex_unlock(&fs_info->chunk_mutex);
0d2450abf   Sergei Trofimovich   btrfs: allow chan...
1821
1822
  	btrfs_resize_thread_pool(fs_info,
  		old_thread_pool_size, fs_info->thread_pool_size);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1823
  	fs_info->metadata_ratio = old_metadata_ratio;
dc81cdc58   Miao Xie   Btrfs: fix remoun...
1824
  	btrfs_remount_cleanup(fs_info, old_opts);
49b25e054   Jeff Mahoney   btrfs: enhance tr...
1825
  	return ret;
c146afad2   Yan Zheng   Btrfs: mount ro a...
1826
  }
bcd53741c   Arne Jansen   btrfs: move btrfs...
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
  /* Used to sort the devices by max_avail(descending sort) */
  static int btrfs_cmp_device_free_bytes(const void *dev_info1,
  				       const void *dev_info2)
  {
  	if (((struct btrfs_device_info *)dev_info1)->max_avail >
  	    ((struct btrfs_device_info *)dev_info2)->max_avail)
  		return -1;
  	else if (((struct btrfs_device_info *)dev_info1)->max_avail <
  		 ((struct btrfs_device_info *)dev_info2)->max_avail)
  		return 1;
  	else
  	return 0;
  }
  
  /*
   * sort the devices by max_avail, in which max free extent size of each device
   * is stored.(Descending Sort)
   */
  static inline void btrfs_descending_sort_devices(
  					struct btrfs_device_info *devices,
  					size_t nr_devices)
  {
  	sort(devices, nr_devices, sizeof(struct btrfs_device_info),
  	     btrfs_cmp_device_free_bytes, NULL);
  }
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
  /*
   * The helper to calc the free space on the devices that can be used to store
   * file data.
   */
  static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
  {
  	struct btrfs_fs_info *fs_info = root->fs_info;
  	struct btrfs_device_info *devices_info;
  	struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  	struct btrfs_device *device;
  	u64 skip_space;
  	u64 type;
  	u64 avail_space;
  	u64 used_space;
  	u64 min_stripe_size;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1867
  	int min_stripes = 1, num_stripes = 1;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1868
1869
  	int i = 0, nr_devices;
  	int ret;
7e33fd993   Josef Bacik   Btrfs: don't take...
1870
  	/*
013276101   Nicholas D Steeves   btrfs: fix string...
1871
  	 * We aren't under the device list lock, so this is racy-ish, but good
7e33fd993   Josef Bacik   Btrfs: don't take...
1872
1873
  	 * enough for our purposes.
  	 */
b772a86ea   Li Zefan   Btrfs: fix oops w...
1874
  	nr_devices = fs_info->fs_devices->open_devices;
7e33fd993   Josef Bacik   Btrfs: don't take...
1875
1876
1877
1878
1879
1880
1881
1882
1883
  	if (!nr_devices) {
  		smp_mb();
  		nr_devices = fs_info->fs_devices->open_devices;
  		ASSERT(nr_devices);
  		if (!nr_devices) {
  			*free_bytes = 0;
  			return 0;
  		}
  	}
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1884

d9b0d9ba0   Dulshani Gunawardhana   btrfs: Replace km...
1885
  	devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1886
1887
1888
  			       GFP_NOFS);
  	if (!devices_info)
  		return -ENOMEM;
013276101   Nicholas D Steeves   btrfs: fix string...
1889
  	/* calc min stripe number for data space allocation */
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1890
  	type = btrfs_get_alloc_profile(root, 1);
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1891
  	if (type & BTRFS_BLOCK_GROUP_RAID0) {
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1892
  		min_stripes = 2;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1893
1894
  		num_stripes = nr_devices;
  	} else if (type & BTRFS_BLOCK_GROUP_RAID1) {
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1895
  		min_stripes = 2;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1896
1897
  		num_stripes = 2;
  	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1898
  		min_stripes = 4;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1899
1900
  		num_stripes = 4;
  	}
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1901
1902
1903
1904
1905
  
  	if (type & BTRFS_BLOCK_GROUP_DUP)
  		min_stripe_size = 2 * BTRFS_STRIPE_LEN;
  	else
  		min_stripe_size = BTRFS_STRIPE_LEN;
7e33fd993   Josef Bacik   Btrfs: don't take...
1906
1907
1908
1909
  	if (fs_info->alloc_start)
  		mutex_lock(&fs_devices->device_list_mutex);
  	rcu_read_lock();
  	list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
63a212abc   Stefan Behrens   Btrfs: disallow s...
1910
1911
  		if (!device->in_fs_metadata || !device->bdev ||
  		    device->is_tgtdev_for_dev_replace)
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1912
  			continue;
7e33fd993   Josef Bacik   Btrfs: don't take...
1913
1914
  		if (i >= nr_devices)
  			break;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1915
1916
1917
  		avail_space = device->total_bytes - device->bytes_used;
  
  		/* align with stripe_len */
f8c269d72   David Sterba   btrfs: cleanup 64...
1918
  		avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN);
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1919
1920
1921
  		avail_space *= BTRFS_STRIPE_LEN;
  
  		/*
013276101   Nicholas D Steeves   btrfs: fix string...
1922
  		 * In order to avoid overwriting the superblock on the drive,
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1923
1924
1925
  		 * btrfs starts at an offset of at least 1MB when doing chunk
  		 * allocation.
  		 */
ee22184b5   Byongho Lee   Btrfs: use linux/...
1926
  		skip_space = SZ_1M;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1927
1928
  
  		/* user can set the offset in fs_info->alloc_start. */
7e33fd993   Josef Bacik   Btrfs: don't take...
1929
1930
1931
1932
  		if (fs_info->alloc_start &&
  		    fs_info->alloc_start + BTRFS_STRIPE_LEN <=
  		    device->total_bytes) {
  			rcu_read_unlock();
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1933
  			skip_space = max(fs_info->alloc_start, skip_space);
7e33fd993   Josef Bacik   Btrfs: don't take...
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
  			/*
  			 * btrfs can not use the free space in
  			 * [0, skip_space - 1], we must subtract it from the
  			 * total. In order to implement it, we account the used
  			 * space in this range first.
  			 */
  			ret = btrfs_account_dev_extents_size(device, 0,
  							     skip_space - 1,
  							     &used_space);
  			if (ret) {
  				kfree(devices_info);
  				mutex_unlock(&fs_devices->device_list_mutex);
  				return ret;
  			}
  
  			rcu_read_lock();
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1950

7e33fd993   Josef Bacik   Btrfs: don't take...
1951
1952
1953
  			/* calc the free space in [0, skip_space - 1] */
  			skip_space -= used_space;
  		}
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
  
  		/*
  		 * we can use the free space in [0, skip_space - 1], subtract
  		 * it from the total.
  		 */
  		if (avail_space && avail_space >= skip_space)
  			avail_space -= skip_space;
  		else
  			avail_space = 0;
  
  		if (avail_space < min_stripe_size)
  			continue;
  
  		devices_info[i].dev = device;
  		devices_info[i].max_avail = avail_space;
  
  		i++;
  	}
7e33fd993   Josef Bacik   Btrfs: don't take...
1972
1973
1974
  	rcu_read_unlock();
  	if (fs_info->alloc_start)
  		mutex_unlock(&fs_devices->device_list_mutex);
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1975
1976
1977
1978
1979
1980
1981
1982
  
  	nr_devices = i;
  
  	btrfs_descending_sort_devices(devices_info, nr_devices);
  
  	i = nr_devices - 1;
  	avail_space = 0;
  	while (nr_devices >= min_stripes) {
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1983
1984
  		if (num_stripes > nr_devices)
  			num_stripes = nr_devices;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1985
1986
1987
  		if (devices_info[i].max_avail >= min_stripe_size) {
  			int j;
  			u64 alloc_size;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1988
  			avail_space += devices_info[i].max_avail * num_stripes;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1989
  			alloc_size = devices_info[i].max_avail;
39fb26c39   Miao Xie   Btrfs: fix inaccu...
1990
  			for (j = i + 1 - num_stripes; j <= i; j++)
6d07bcec9   Miao Xie   btrfs: fix wrong ...
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
  				devices_info[j].max_avail -= alloc_size;
  		}
  		i--;
  		nr_devices--;
  	}
  
  	kfree(devices_info);
  	*free_bytes = avail_space;
  	return 0;
  }
ba7b6e62f   David Sterba   btrfs: adjust sta...
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
  /*
   * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
   *
   * If there's a redundant raid level at DATA block groups, use the respective
   * multiplier to scale the sizes.
   *
   * Unused device space usage is based on simulating the chunk allocator
   * algorithm that respects the device sizes, order of allocations and the
   * 'alloc_start' value, this is a close approximation of the actual use but
   * there are other factors that may change the result (like a new metadata
   * chunk).
   *
ca8a51b3a   David Sterba   btrfs: statfs: re...
2013
   * If metadata is exhausted, f_bavail will be 0.
ba7b6e62f   David Sterba   btrfs: adjust sta...
2014
   */
8fd17795b   Chris Mason   Btrfs: early fsyn...
2015
2016
  static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
2017
2018
2019
  	struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
  	struct btrfs_super_block *disk_super = fs_info->super_copy;
  	struct list_head *head = &fs_info->space_info;
bd4d10888   Josef Bacik   Btrfs: make df be...
2020
2021
  	struct btrfs_space_info *found;
  	u64 total_used = 0;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2022
  	u64 total_free_data = 0;
ca8a51b3a   David Sterba   btrfs: statfs: re...
2023
  	u64 total_free_meta = 0;
db94535db   Chris Mason   Btrfs: Allow tree...
2024
  	int bits = dentry->d_sb->s_blocksize_bits;
815745cf3   Al Viro   btrfs: let ->s_fs...
2025
  	__be32 *fsid = (__be32 *)fs_info->fsid;
ba7b6e62f   David Sterba   btrfs: adjust sta...
2026
2027
  	unsigned factor = 1;
  	struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2028
  	int ret;
ca8a51b3a   David Sterba   btrfs: statfs: re...
2029
  	u64 thresh = 0;
ae02d1bd0   Luis de Bethencourt   btrfs: fix mixed ...
2030
  	int mixed = 0;
8fd17795b   Chris Mason   Btrfs: early fsyn...
2031

15484377f   Miao Xie   Btrfs: fix unprot...
2032
  	/*
180e4d470   Luis de Bethencourt   btrfs: fix typos ...
2033
  	 * holding chunk_mutex to avoid allocating new chunks, holding
15484377f   Miao Xie   Btrfs: fix unprot...
2034
2035
  	 * device_list_mutex to avoid the device being removed
  	 */
bd4d10888   Josef Bacik   Btrfs: make df be...
2036
  	rcu_read_lock();
89a55897a   Josef Bacik   Btrfs: fix df reg...
2037
  	list_for_each_entry_rcu(found, head, list) {
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2038
  		if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
ba7b6e62f   David Sterba   btrfs: adjust sta...
2039
  			int i;
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2040
2041
2042
  			total_free_data += found->disk_total - found->disk_used;
  			total_free_data -=
  				btrfs_account_ro_block_groups_free_space(found);
ba7b6e62f   David Sterba   btrfs: adjust sta...
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
  
  			for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
  				if (!list_empty(&found->block_groups[i])) {
  					switch (i) {
  					case BTRFS_RAID_DUP:
  					case BTRFS_RAID_RAID1:
  					case BTRFS_RAID_RAID10:
  						factor = 2;
  					}
  				}
  			}
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2054
  		}
ae02d1bd0   Luis de Bethencourt   btrfs: fix mixed ...
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
  
  		/*
  		 * Metadata in mixed block goup profiles are accounted in data
  		 */
  		if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
  			if (found->flags & BTRFS_BLOCK_GROUP_DATA)
  				mixed = 1;
  			else
  				total_free_meta += found->disk_total -
  					found->disk_used;
  		}
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2066

b742bb82f   Yan, Zheng   Btrfs: Link block...
2067
  		total_used += found->disk_used;
89a55897a   Josef Bacik   Btrfs: fix df reg...
2068
  	}
ba7b6e62f   David Sterba   btrfs: adjust sta...
2069

bd4d10888   Josef Bacik   Btrfs: make df be...
2070
  	rcu_read_unlock();
ba7b6e62f   David Sterba   btrfs: adjust sta...
2071
2072
2073
2074
2075
2076
  	buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
  	buf->f_blocks >>= bits;
  	buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
  
  	/* Account global block reserve as used, it's in logical size already */
  	spin_lock(&block_rsv->lock);
41b34accb   Luis de Bethencourt   btrfs: avoid over...
2077
2078
2079
2080
2081
  	/* Mixed block groups accounting is not byte-accurate, avoid overflow */
  	if (buf->f_bfree >= block_rsv->size >> bits)
  		buf->f_bfree -= block_rsv->size >> bits;
  	else
  		buf->f_bfree = 0;
ba7b6e62f   David Sterba   btrfs: adjust sta...
2082
  	spin_unlock(&block_rsv->lock);
0d95c1bec   David Sterba   btrfs: fix wrong ...
2083
  	buf->f_bavail = div_u64(total_free_data, factor);
815745cf3   Al Viro   btrfs: let ->s_fs...
2084
  	ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
7e33fd993   Josef Bacik   Btrfs: don't take...
2085
  	if (ret)
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2086
  		return ret;
ba7b6e62f   David Sterba   btrfs: adjust sta...
2087
  	buf->f_bavail += div_u64(total_free_data, factor);
6d07bcec9   Miao Xie   btrfs: fix wrong ...
2088
  	buf->f_bavail = buf->f_bavail >> bits;
d397712bc   Chris Mason   Btrfs: Fix checkp...
2089

ca8a51b3a   David Sterba   btrfs: statfs: re...
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
  	/*
  	 * We calculate the remaining metadata space minus global reserve. If
  	 * this is (supposedly) smaller than zero, there's no space. But this
  	 * does not hold in practice, the exhausted state happens where's still
  	 * some positive delta. So we apply some guesswork and compare the
  	 * delta to a 4M threshold.  (Practically observed delta was ~2M.)
  	 *
  	 * We probably cannot calculate the exact threshold value because this
  	 * depends on the internal reservations requested by various
  	 * operations, so some operations that consume a few metadata will
  	 * succeed even if the Avail is zero. But this is better than the other
  	 * way around.
  	 */
  	thresh = 4 * 1024 * 1024;
ae02d1bd0   Luis de Bethencourt   btrfs: fix mixed ...
2104
  	if (!mixed && total_free_meta - thresh < block_rsv->size)
ca8a51b3a   David Sterba   btrfs: statfs: re...
2105
  		buf->f_bavail = 0;
ba7b6e62f   David Sterba   btrfs: adjust sta...
2106
2107
2108
  	buf->f_type = BTRFS_SUPER_MAGIC;
  	buf->f_bsize = dentry->d_sb->s_blocksize;
  	buf->f_namelen = BTRFS_NAME_LEN;
9d03632e2   David Woodhouse   Fill f_fsid field...
2109
  	/* We treat it as constant endianness (it doesn't matter _which_)
d397712bc   Chris Mason   Btrfs: Fix checkp...
2110
  	   because we want the fsid to come out the same whether mounted
9d03632e2   David Woodhouse   Fill f_fsid field...
2111
2112
2113
  	   on a big-endian or little-endian host */
  	buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
  	buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
32d48fa1a   David Woodhouse   Mask root object ...
2114
  	/* Mask in the root object ID too, to disambiguate subvols */
2b0143b5c   David Howells   VFS: normal files...
2115
2116
  	buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32;
  	buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid;
32d48fa1a   David Woodhouse   Mask root object ...
2117

8fd17795b   Chris Mason   Btrfs: early fsyn...
2118
2119
  	return 0;
  }
b51338628   Chris Mason   Btrfs: add dirty_...
2120

aea52e19d   Al Viro   btrfs: get ->kill...
2121
2122
  static void btrfs_kill_super(struct super_block *sb)
  {
815745cf3   Al Viro   btrfs: let ->s_fs...
2123
  	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
aea52e19d   Al Viro   btrfs: get ->kill...
2124
  	kill_anon_super(sb);
d22ca7de7   Al Viro   btrfs: make free_...
2125
  	free_fs_info(fs_info);
aea52e19d   Al Viro   btrfs: get ->kill...
2126
  }
2e635a278   Chris Mason   Btrfs: initial mo...
2127
2128
2129
  static struct file_system_type btrfs_fs_type = {
  	.owner		= THIS_MODULE,
  	.name		= "btrfs",
061dbc6b9   Al Viro   convert btrfs
2130
  	.mount		= btrfs_mount,
aea52e19d   Al Viro   btrfs: get ->kill...
2131
  	.kill_sb	= btrfs_kill_super,
f667aef6a   Qu Wenruo   btrfs: Make btrfs...
2132
  	.fs_flags	= FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2e635a278   Chris Mason   Btrfs: initial mo...
2133
  };
7f78e0351   Eric W. Biederman   fs: Limit sys_mou...
2134
  MODULE_ALIAS_FS("btrfs");
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2135

d86209582   Tom Van Braeckel   btrfs: explicitly...
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
  static int btrfs_control_open(struct inode *inode, struct file *file)
  {
  	/*
  	 * The control file's private_data is used to hold the
  	 * transaction when it is started and is used to keep
  	 * track of whether a transaction is already in progress.
  	 */
  	file->private_data = NULL;
  	return 0;
  }
d352ac681   Chris Mason   Btrfs: add and im...
2146
2147
2148
  /*
   * used by btrfsctl to scan devices when no FS is mounted
   */
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2149
2150
2151
2152
2153
  static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
  				unsigned long arg)
  {
  	struct btrfs_ioctl_vol_args *vol;
  	struct btrfs_fs_devices *fs_devices;
c071fcfdb   Chris Mason   Btrfs: fix ioctl ...
2154
  	int ret = -ENOTTY;
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2155

e441d54de   Chris Mason   Btrfs: add permis...
2156
2157
  	if (!capable(CAP_SYS_ADMIN))
  		return -EPERM;
dae7b665c   Li Zefan   btrfs: use memdup...
2158
2159
2160
  	vol = memdup_user((void __user *)arg, sizeof(*vol));
  	if (IS_ERR(vol))
  		return PTR_ERR(vol);
c071fcfdb   Chris Mason   Btrfs: fix ioctl ...
2161

8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2162
2163
  	switch (cmd) {
  	case BTRFS_IOC_SCAN_DEV:
97288f2c7   Christoph Hellwig   Btrfs: corret fmo...
2164
  		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2165
2166
  					    &btrfs_fs_type, &fs_devices);
  		break;
02db0844b   Josef Bacik   Btrfs: add DEVICE...
2167
2168
2169
2170
2171
2172
2173
  	case BTRFS_IOC_DEVICES_READY:
  		ret = btrfs_scan_one_device(vol->name, FMODE_READ,
  					    &btrfs_fs_type, &fs_devices);
  		if (ret)
  			break;
  		ret = !(fs_devices->num_devices == fs_devices->total_devices);
  		break;
c5868f836   David Sterba   btrfs: add GET_SU...
2174
  	case BTRFS_IOC_GET_SUPPORTED_FEATURES:
d5131b658   David Sterba   btrfs: drop unuse...
2175
  		ret = btrfs_ioctl_get_supported_features((void __user*)arg);
c5868f836   David Sterba   btrfs: add GET_SU...
2176
  		break;
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2177
  	}
dae7b665c   Li Zefan   btrfs: use memdup...
2178

8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2179
  	kfree(vol);
f819d837e   Linda Knippers   btrfsctl -A error...
2180
  	return ret;
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2181
  }
0176260fc   Linus Torvalds   btrfs: fix for wr...
2182
  static int btrfs_freeze(struct super_block *sb)
ed0dab6b8   Yan   Btrfs: Add basic ...
2183
  {
354aa0fb6   Miao Xie   Btrfs: fix orphan...
2184
2185
  	struct btrfs_trans_handle *trans;
  	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
9e7cc91a6   Wang Xiaoguang   btrfs: fix fsfree...
2186
2187
2188
2189
2190
2191
2192
  	root->fs_info->fs_frozen = 1;
  	/*
  	 * We don't need a barrier here, we'll wait for any transaction that
  	 * could be in progress on other threads (and do delayed iputs that
  	 * we want to avoid on a frozen filesystem), or do the commit
  	 * ourselves.
  	 */
d4edf39bd   Miao Xie   Btrfs: fix uncomp...
2193
  	trans = btrfs_attach_transaction_barrier(root);
354aa0fb6   Miao Xie   Btrfs: fix orphan...
2194
2195
2196
2197
2198
2199
2200
  	if (IS_ERR(trans)) {
  		/* no transaction, don't bother */
  		if (PTR_ERR(trans) == -ENOENT)
  			return 0;
  		return PTR_ERR(trans);
  	}
  	return btrfs_commit_transaction(trans, root);
ed0dab6b8   Yan   Btrfs: Add basic ...
2201
  }
9e7cc91a6   Wang Xiaoguang   btrfs: fix fsfree...
2202
2203
2204
2205
2206
2207
2208
  static int btrfs_unfreeze(struct super_block *sb)
  {
  	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
  
  	root->fs_info->fs_frozen = 0;
  	return 0;
  }
9c5085c14   Josef Bacik   Btrfs: implement ...
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
  static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
  {
  	struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
  	struct btrfs_fs_devices *cur_devices;
  	struct btrfs_device *dev, *first_dev = NULL;
  	struct list_head *head;
  	struct rcu_string *name;
  
  	mutex_lock(&fs_info->fs_devices->device_list_mutex);
  	cur_devices = fs_info->fs_devices;
  	while (cur_devices) {
  		head = &cur_devices->devices;
  		list_for_each_entry(dev, head, dev_list) {
aa9ddcd4b   Josef Bacik   Btrfs: do not use...
2222
2223
  			if (dev->missing)
  				continue;
0aeb8a6e6   Anand Jain   btrfs: fix null p...
2224
2225
  			if (!dev->name)
  				continue;
9c5085c14   Josef Bacik   Btrfs: implement ...
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
  			if (!first_dev || dev->devid < first_dev->devid)
  				first_dev = dev;
  		}
  		cur_devices = cur_devices->seed;
  	}
  
  	if (first_dev) {
  		rcu_read_lock();
  		name = rcu_dereference(first_dev->name);
  		seq_escape(m, name->str, " \t
  \\");
  		rcu_read_unlock();
  	} else {
  		WARN_ON(1);
  	}
  	mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  	return 0;
  }
b87221de6   Alexey Dobriyan   const: mark remai...
2244
  static const struct super_operations btrfs_super_ops = {
76dda93c6   Yan, Zheng   Btrfs: add snapsh...
2245
  	.drop_inode	= btrfs_drop_inode,
bd5559752   Al Viro   convert btrfs to ...
2246
  	.evict_inode	= btrfs_evict_inode,
e20d96d64   Chris Mason   Mountable btrfs, ...
2247
  	.put_super	= btrfs_put_super,
d57197629   Chris Mason   btrfs_create, btr...
2248
  	.sync_fs	= btrfs_sync_fs,
a9572a15a   Eric Paris   Btrfs: introduce ...
2249
  	.show_options	= btrfs_show_options,
9c5085c14   Josef Bacik   Btrfs: implement ...
2250
  	.show_devname	= btrfs_show_devname,
4730a4bc5   Chris Mason   btrfs_dirty_inode
2251
  	.write_inode	= btrfs_write_inode,
2c90e5d65   Chris Mason   Btrfs: still corr...
2252
2253
  	.alloc_inode	= btrfs_alloc_inode,
  	.destroy_inode	= btrfs_destroy_inode,
8fd17795b   Chris Mason   Btrfs: early fsyn...
2254
  	.statfs		= btrfs_statfs,
c146afad2   Yan Zheng   Btrfs: mount ro a...
2255
  	.remount_fs	= btrfs_remount,
0176260fc   Linus Torvalds   btrfs: fix for wr...
2256
  	.freeze_fs	= btrfs_freeze,
9e7cc91a6   Wang Xiaoguang   btrfs: fix fsfree...
2257
  	.unfreeze_fs	= btrfs_unfreeze,
e20d96d64   Chris Mason   Mountable btrfs, ...
2258
  };
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2259
2260
  
  static const struct file_operations btrfs_ctl_fops = {
d86209582   Tom Van Braeckel   btrfs: explicitly...
2261
  	.open = btrfs_control_open,
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2262
2263
2264
  	.unlocked_ioctl	 = btrfs_control_ioctl,
  	.compat_ioctl = btrfs_control_ioctl,
  	.owner	 = THIS_MODULE,
6038f373a   Arnd Bergmann   llseek: automatic...
2265
  	.llseek = noop_llseek,
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2266
2267
2268
  };
  
  static struct miscdevice btrfs_misc = {
578454ff7   Kay Sievers   driver core: add ...
2269
  	.minor		= BTRFS_MINOR,
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2270
2271
2272
  	.name		= "btrfs-control",
  	.fops		= &btrfs_ctl_fops
  };
578454ff7   Kay Sievers   driver core: add ...
2273
2274
  MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
  MODULE_ALIAS("devname:btrfs-control");
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2275
2276
2277
2278
  static int btrfs_interface_init(void)
  {
  	return misc_register(&btrfs_misc);
  }
b2950863c   Christoph Hellwig   Btrfs: make thing...
2279
  static void btrfs_interface_exit(void)
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2280
  {
f368ed608   Greg Kroah-Hartman   char: make misc_d...
2281
  	misc_deregister(&btrfs_misc);
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2282
  }
8ae1af3cd   Anand Jain   btrfs: rename btr...
2283
  static void btrfs_print_mod_info(void)
85965600f   David Sterba   btrfs: show compi...
2284
  {
62e855771   Jeff Mahoney   btrfs: convert pr...
2285
  	pr_info("Btrfs loaded, crc32c=%s"
85965600f   David Sterba   btrfs: show compi...
2286
2287
2288
  #ifdef CONFIG_BTRFS_DEBUG
  			", debug=on"
  #endif
79556c3d8   Stefan Behrens   btrfs: show compi...
2289
2290
2291
  #ifdef CONFIG_BTRFS_ASSERT
  			", assert=on"
  #endif
85965600f   David Sterba   btrfs: show compi...
2292
2293
2294
  #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  			", integrity-checker=on"
  #endif
5f9e1059d   Jeff Mahoney   btrfs: advertise ...
2295
2296
2297
  			"
  ",
  			btrfs_crc32c_impl());
85965600f   David Sterba   btrfs: show compi...
2298
  }
2e635a278   Chris Mason   Btrfs: initial mo...
2299
2300
  static int __init init_btrfs_fs(void)
  {
2c90e5d65   Chris Mason   Btrfs: still corr...
2301
  	int err;
58176a960   Josef Bacik   Btrfs: Add per-ro...
2302

14a958e67   Filipe David Borba Manana   Btrfs: fix btrfs ...
2303
2304
2305
  	err = btrfs_hash_init();
  	if (err)
  		return err;
63541927c   Filipe David Borba Manana   Btrfs: add suppor...
2306
  	btrfs_props_init();
58176a960   Josef Bacik   Btrfs: Add per-ro...
2307
2308
  	err = btrfs_init_sysfs();
  	if (err)
14a958e67   Filipe David Borba Manana   Btrfs: fix btrfs ...
2309
  		goto free_hash;
58176a960   Josef Bacik   Btrfs: Add per-ro...
2310

143bede52   Jeff Mahoney   btrfs: return voi...
2311
  	btrfs_init_compress();
d1310b2e0   Chris Mason   Btrfs: Split the ...
2312

261507a02   Li Zefan   btrfs: Allow to a...
2313
2314
2315
  	err = btrfs_init_cachep();
  	if (err)
  		goto free_compress;
d1310b2e0   Chris Mason   Btrfs: Split the ...
2316
  	err = extent_io_init();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2317
2318
  	if (err)
  		goto free_cachep;
d1310b2e0   Chris Mason   Btrfs: Split the ...
2319
2320
2321
  	err = extent_map_init();
  	if (err)
  		goto free_extent_io;
6352b91da   Miao Xie   Btrfs: use a slab...
2322
  	err = ordered_data_init();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2323
2324
  	if (err)
  		goto free_extent_map;
c8b978188   Chris Mason   Btrfs: Add zlib c...
2325

6352b91da   Miao Xie   Btrfs: use a slab...
2326
2327
2328
  	err = btrfs_delayed_inode_init();
  	if (err)
  		goto free_ordered_data;
9247f3170   Miao Xie   Btrfs: use slabs ...
2329
  	err = btrfs_auto_defrag_init();
16cdcec73   Miao Xie   btrfs: implement ...
2330
2331
  	if (err)
  		goto free_delayed_inode;
78a6184a3   Miao Xie   Btrfs: use slabs ...
2332
  	err = btrfs_delayed_ref_init();
9247f3170   Miao Xie   Btrfs: use slabs ...
2333
2334
  	if (err)
  		goto free_auto_defrag;
b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
2335
2336
  	err = btrfs_prelim_ref_init();
  	if (err)
af13b4922   David Sterba   btrfs: fix error ...
2337
  		goto free_delayed_ref;
b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
2338

97eb6b69d   David Sterba   btrfs: use slab f...
2339
  	err = btrfs_end_io_wq_init();
78a6184a3   Miao Xie   Btrfs: use slabs ...
2340
  	if (err)
af13b4922   David Sterba   btrfs: fix error ...
2341
  		goto free_prelim_ref;
78a6184a3   Miao Xie   Btrfs: use slabs ...
2342

97eb6b69d   David Sterba   btrfs: use slab f...
2343
2344
2345
  	err = btrfs_interface_init();
  	if (err)
  		goto free_end_io_wq;
e565d4b96   Jan Schmidt   Btrfs: actually c...
2346
  	btrfs_init_lockdep();
8ae1af3cd   Anand Jain   btrfs: rename btr...
2347
  	btrfs_print_mod_info();
dc11dd5d7   Josef Bacik   Btrfs: separate o...
2348
2349
2350
2351
2352
2353
2354
2355
  
  	err = btrfs_run_sanity_tests();
  	if (err)
  		goto unregister_ioctl;
  
  	err = register_filesystem(&btrfs_fs_type);
  	if (err)
  		goto unregister_ioctl;
74255aa07   Josef Bacik   Btrfs: add some f...
2356

2f4cbe644   Wyatt Banks   Btrfs: Return val...
2357
  	return 0;
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2358
2359
  unregister_ioctl:
  	btrfs_interface_exit();
97eb6b69d   David Sterba   btrfs: use slab f...
2360
2361
  free_end_io_wq:
  	btrfs_end_io_wq_exit();
b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
2362
2363
  free_prelim_ref:
  	btrfs_prelim_ref_exit();
78a6184a3   Miao Xie   Btrfs: use slabs ...
2364
2365
  free_delayed_ref:
  	btrfs_delayed_ref_exit();
9247f3170   Miao Xie   Btrfs: use slabs ...
2366
2367
  free_auto_defrag:
  	btrfs_auto_defrag_exit();
16cdcec73   Miao Xie   btrfs: implement ...
2368
2369
  free_delayed_inode:
  	btrfs_delayed_inode_exit();
6352b91da   Miao Xie   Btrfs: use a slab...
2370
2371
  free_ordered_data:
  	ordered_data_exit();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2372
2373
  free_extent_map:
  	extent_map_exit();
d1310b2e0   Chris Mason   Btrfs: Split the ...
2374
2375
  free_extent_io:
  	extent_io_exit();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2376
2377
  free_cachep:
  	btrfs_destroy_cachep();
261507a02   Li Zefan   btrfs: Allow to a...
2378
2379
  free_compress:
  	btrfs_exit_compress();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2380
  	btrfs_exit_sysfs();
14a958e67   Filipe David Borba Manana   Btrfs: fix btrfs ...
2381
2382
  free_hash:
  	btrfs_hash_exit();
2f4cbe644   Wyatt Banks   Btrfs: Return val...
2383
  	return err;
2e635a278   Chris Mason   Btrfs: initial mo...
2384
2385
2386
2387
  }
  
  static void __exit exit_btrfs_fs(void)
  {
39279cc3d   Chris Mason   Btrfs: split up s...
2388
  	btrfs_destroy_cachep();
78a6184a3   Miao Xie   Btrfs: use slabs ...
2389
  	btrfs_delayed_ref_exit();
9247f3170   Miao Xie   Btrfs: use slabs ...
2390
  	btrfs_auto_defrag_exit();
16cdcec73   Miao Xie   btrfs: implement ...
2391
  	btrfs_delayed_inode_exit();
b9e9a6cbc   Wang Shilong   Btrfs: allocate p...
2392
  	btrfs_prelim_ref_exit();
6352b91da   Miao Xie   Btrfs: use a slab...
2393
  	ordered_data_exit();
a52d9a803   Chris Mason   Btrfs: Extent bas...
2394
  	extent_map_exit();
d1310b2e0   Chris Mason   Btrfs: Split the ...
2395
  	extent_io_exit();
a9218f6b0   Chris Mason   Add /dev/btrfs-co...
2396
  	btrfs_interface_exit();
5ed5f5884   Josef Bacik   Btrfs: properly c...
2397
  	btrfs_end_io_wq_exit();
2e635a278   Chris Mason   Btrfs: initial mo...
2398
  	unregister_filesystem(&btrfs_fs_type);
58176a960   Josef Bacik   Btrfs: Add per-ro...
2399
  	btrfs_exit_sysfs();
8a4b83cc8   Chris Mason   Btrfs: Add suppor...
2400
  	btrfs_cleanup_fs_uuids();
261507a02   Li Zefan   btrfs: Allow to a...
2401
  	btrfs_exit_compress();
14a958e67   Filipe David Borba Manana   Btrfs: fix btrfs ...
2402
  	btrfs_hash_exit();
2e635a278   Chris Mason   Btrfs: initial mo...
2403
  }
60efa5eb2   Filipe David Borba Manana   Btrfs: use late_i...
2404
  late_initcall(init_btrfs_fs);
2e635a278   Chris Mason   Btrfs: initial mo...
2405
2406
2407
  module_exit(exit_btrfs_fs)
  
  MODULE_LICENSE("GPL");