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