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