Btrfs: free reserved space on error in a few places
[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>
4b4e25f2 45#include "compat.h"
16cdcec7 46#include "delayed-inode.h"
2e635a27 47#include "ctree.h"
e20d96d6 48#include "disk-io.h"
d5719762 49#include "transaction.h"
2c90e5d6 50#include "btrfs_inode.h"
3a686375 51#include "print-tree.h"
5103e947 52#include "xattr.h"
8a4b83cc 53#include "volumes.h"
be6e8dc0 54#include "export.h"
c8b97818 55#include "compression.h"
9c5085c1 56#include "rcu-string.h"
8dabb742 57#include "dev-replace.h"
74255aa0 58#include "free-space-cache.h"
b9e9a6cb 59#include "backref.h"
dc11dd5d 60#include "tests/btrfs-tests.h"
2e635a27 61
1abe9b8a 62#define CREATE_TRACE_POINTS
63#include <trace/events/btrfs.h>
64
b87221de 65static const struct super_operations btrfs_super_ops;
830c4adb 66static struct file_system_type btrfs_fs_type;
75dfe396 67
08748810 68static const char *btrfs_decode_error(int errno)
acce952b 69{
08748810 70 char *errstr = "unknown";
acce952b 71
72 switch (errno) {
73 case -EIO:
74 errstr = "IO failure";
75 break;
76 case -ENOMEM:
77 errstr = "Out of memory";
78 break;
79 case -EROFS:
80 errstr = "Readonly filesystem";
81 break;
8c342930
JM
82 case -EEXIST:
83 errstr = "Object already exists";
84 break;
94ef7280
DS
85 case -ENOSPC:
86 errstr = "No space left";
87 break;
88 case -ENOENT:
89 errstr = "No such entry";
90 break;
acce952b 91 }
92
93 return errstr;
94}
95
bbece8a3 96static void save_error_info(struct btrfs_fs_info *fs_info)
acce952b 97{
98 /*
99 * today we only save the error info into ram. Long term we'll
100 * also send it down to the disk
101 */
87533c47 102 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
acce952b 103}
104
acce952b 105/* btrfs handle error by forcing the filesystem readonly */
106static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
107{
108 struct super_block *sb = fs_info->sb;
109
110 if (sb->s_flags & MS_RDONLY)
111 return;
112
87533c47 113 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
acce952b 114 sb->s_flags |= MS_RDONLY;
c2cf52eb 115 btrfs_info(fs_info, "forced readonly");
1acd6831
SB
116 /*
117 * Note that a running device replace operation is not
118 * canceled here although there is no way to update
119 * the progress. It would add the risk of a deadlock,
120 * therefore the canceling is ommited. The only penalty
121 * is that some I/O remains active until the procedure
122 * completes. The next time when the filesystem is
123 * mounted writeable again, the device replace
124 * operation continues.
125 */
acce952b 126 }
127}
128
533574c6 129#ifdef CONFIG_PRINTK
acce952b 130/*
131 * __btrfs_std_error decodes expected errors from the caller and
132 * invokes the approciate error response.
133 */
134void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
4da35113 135 unsigned int line, int errno, const char *fmt, ...)
acce952b 136{
137 struct super_block *sb = fs_info->sb;
acce952b 138 const char *errstr;
139
140 /*
141 * Special case: if the error is EROFS, and we're already
142 * under MS_RDONLY, then it is safe here.
143 */
144 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
4da35113
JM
145 return;
146
08748810 147 errstr = btrfs_decode_error(errno);
4da35113 148 if (fmt) {
37252a66
ES
149 struct va_format vaf;
150 va_list args;
151
152 va_start(args, fmt);
153 vaf.fmt = fmt;
154 vaf.va = &args;
4da35113 155
08748810
DS
156 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
157 sb->s_id, function, line, errno, errstr, &vaf);
37252a66 158 va_end(args);
4da35113 159 } else {
08748810
DS
160 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
161 sb->s_id, function, line, errno, errstr);
4da35113 162 }
acce952b 163
4da35113 164 /* Don't go through full error handling during mount */
cf79ffb5
JB
165 save_error_info(fs_info);
166 if (sb->s_flags & MS_BORN)
4da35113 167 btrfs_handle_error(fs_info);
4da35113 168}
acce952b 169
533574c6 170static const char * const logtypes[] = {
4da35113
JM
171 "emergency",
172 "alert",
173 "critical",
174 "error",
175 "warning",
176 "notice",
177 "info",
178 "debug",
179};
180
c2cf52eb 181void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
4da35113
JM
182{
183 struct super_block *sb = fs_info->sb;
184 char lvl[4];
185 struct va_format vaf;
186 va_list args;
187 const char *type = logtypes[4];
533574c6 188 int kern_level;
4da35113
JM
189
190 va_start(args, fmt);
191
533574c6
JP
192 kern_level = printk_get_level(fmt);
193 if (kern_level) {
194 size_t size = printk_skip_level(fmt) - fmt;
195 memcpy(lvl, fmt, size);
196 lvl[size] = '\0';
197 fmt += size;
198 type = logtypes[kern_level - '0'];
4da35113
JM
199 } else
200 *lvl = '\0';
201
202 vaf.fmt = fmt;
203 vaf.va = &args;
533574c6 204
c2cf52eb 205 printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
533574c6
JP
206
207 va_end(args);
208}
209
210#else
211
212void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
213 unsigned int line, int errno, const char *fmt, ...)
214{
215 struct super_block *sb = fs_info->sb;
216
217 /*
218 * Special case: if the error is EROFS, and we're already
219 * under MS_RDONLY, then it is safe here.
220 */
221 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
222 return;
223
224 /* Don't go through full error handling during mount */
225 if (sb->s_flags & MS_BORN) {
226 save_error_info(fs_info);
227 btrfs_handle_error(fs_info);
228 }
acce952b 229}
533574c6 230#endif
acce952b 231
49b25e05
JM
232/*
233 * We only mark the transaction aborted and then set the file system read-only.
234 * This will prevent new transactions from starting or trying to join this
235 * one.
236 *
237 * This means that error recovery at the call site is limited to freeing
238 * any local memory allocations and passing the error code up without
239 * further cleanup. The transaction should complete as it normally would
240 * in the call path but will return -EIO.
241 *
242 * We'll complete the cleanup in btrfs_end_transaction and
243 * btrfs_commit_transaction.
244 */
245void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
246 struct btrfs_root *root, const char *function,
247 unsigned int line, int errno)
248{
08748810
DS
249 /*
250 * Report first abort since mount
251 */
252 if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
253 &root->fs_info->fs_state)) {
254 WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
255 errno);
256 }
49b25e05
JM
257 trans->aborted = errno;
258 /* Nothing used. The other threads that have joined this
259 * transaction may be able to continue. */
260 if (!trans->blocks_used) {
69ce977a
MX
261 const char *errstr;
262
08748810 263 errstr = btrfs_decode_error(errno);
c2cf52eb
SK
264 btrfs_warn(root->fs_info,
265 "%s:%d: Aborting unused transaction(%s).",
266 function, line, errstr);
acce952b 267 return;
49b25e05 268 }
8d25a086 269 ACCESS_ONCE(trans->transaction->aborted) = errno;
501407aa
JB
270 /* Wake up anybody who may be waiting on this transaction */
271 wake_up(&root->fs_info->transaction_wait);
272 wake_up(&root->fs_info->transaction_blocked_wait);
49b25e05
JM
273 __btrfs_std_error(root->fs_info, function, line, errno, NULL);
274}
8c342930
JM
275/*
276 * __btrfs_panic decodes unexpected, fatal errors from the caller,
277 * issues an alert, and either panics or BUGs, depending on mount options.
278 */
279void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
280 unsigned int line, int errno, const char *fmt, ...)
281{
8c342930
JM
282 char *s_id = "<unknown>";
283 const char *errstr;
284 struct va_format vaf = { .fmt = fmt };
285 va_list args;
acce952b 286
8c342930
JM
287 if (fs_info)
288 s_id = fs_info->sb->s_id;
acce952b 289
8c342930
JM
290 va_start(args, fmt);
291 vaf.va = &args;
292
08748810 293 errstr = btrfs_decode_error(errno);
aa43a17c 294 if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
08748810
DS
295 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
296 s_id, function, line, &vaf, errno, errstr);
8c342930 297
08748810
DS
298 printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
299 s_id, function, line, &vaf, errno, errstr);
8c342930
JM
300 va_end(args);
301 /* Caller calls BUG() */
acce952b 302}
303
d397712b 304static void btrfs_put_super(struct super_block *sb)
b18c6685 305{
815745cf 306 (void)close_ctree(btrfs_sb(sb)->tree_root);
aea52e19
AV
307 /* FIXME: need to fix VFS to return error? */
308 /* AV: return it _where_? ->put_super() can be triggered by any number
309 * of async events, up to and including delivery of SIGKILL to the
310 * last process that kept it busy. Or segfault in the aforementioned
311 * process... Whom would you report that to?
312 */
75dfe396
CM
313}
314
95e05289 315enum {
73f73415 316 Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
287a0ab9
JB
317 Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
318 Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
261507a0
LZ
319 Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
320 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
91435650 321 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
9555c6c1
ID
322 Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
323 Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
21adbd5c 324 Opt_check_integrity, Opt_check_integrity_including_extent_data,
f420ee1e 325 Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree,
8b87dc17 326 Opt_commit_interval,
9555c6c1 327 Opt_err,
95e05289
CM
328};
329
330static match_table_t tokens = {
dfe25020 331 {Opt_degraded, "degraded"},
95e05289 332 {Opt_subvol, "subvol=%s"},
1493381f 333 {Opt_subvolid, "subvolid=%s"},
43e570b0 334 {Opt_device, "device=%s"},
b6cda9bc 335 {Opt_nodatasum, "nodatasum"},
be20aa9d 336 {Opt_nodatacow, "nodatacow"},
21ad10cf 337 {Opt_nobarrier, "nobarrier"},
6f568d35 338 {Opt_max_inline, "max_inline=%s"},
8f662a76 339 {Opt_alloc_start, "alloc_start=%s"},
4543df7e 340 {Opt_thread_pool, "thread_pool=%d"},
c8b97818 341 {Opt_compress, "compress"},
261507a0 342 {Opt_compress_type, "compress=%s"},
a555f810 343 {Opt_compress_force, "compress-force"},
261507a0 344 {Opt_compress_force_type, "compress-force=%s"},
e18e4809 345 {Opt_ssd, "ssd"},
451d7585 346 {Opt_ssd_spread, "ssd_spread"},
3b30c22f 347 {Opt_nossd, "nossd"},
33268eaf 348 {Opt_noacl, "noacl"},
3a5e1404 349 {Opt_notreelog, "notreelog"},
dccae999 350 {Opt_flushoncommit, "flushoncommit"},
97e728d4 351 {Opt_ratio, "metadata_ratio=%d"},
e244a0ae 352 {Opt_discard, "discard"},
0af3d00b 353 {Opt_space_cache, "space_cache"},
88c2ba3b 354 {Opt_clear_cache, "clear_cache"},
4260f7c7 355 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
91435650 356 {Opt_enospc_debug, "enospc_debug"},
e15d0542 357 {Opt_subvolrootid, "subvolrootid=%d"},
4cb5300b 358 {Opt_defrag, "autodefrag"},
4b9465cb 359 {Opt_inode_cache, "inode_cache"},
8965593e 360 {Opt_no_space_cache, "nospace_cache"},
af31f5e5 361 {Opt_recovery, "recovery"},
9555c6c1 362 {Opt_skip_balance, "skip_balance"},
21adbd5c
SB
363 {Opt_check_integrity, "check_int"},
364 {Opt_check_integrity_including_extent_data, "check_int_data"},
365 {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
f420ee1e 366 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
8c342930 367 {Opt_fatal_errors, "fatal_errors=%s"},
8b87dc17 368 {Opt_commit_interval, "commit=%d"},
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);
73bc1876
JB
389 if (cache_gen)
390 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
391
95e05289 392 if (!options)
73bc1876 393 goto out;
95e05289 394
be20aa9d
CM
395 /*
396 * strsep changes the string, duplicate it because parse_options
397 * gets called twice
398 */
399 options = kstrdup(options, GFP_NOFS);
400 if (!options)
401 return -ENOMEM;
402
da495ecc 403 orig = options;
be20aa9d 404
edf24abe 405 while ((p = strsep(&options, ",")) != NULL) {
95e05289
CM
406 int token;
407 if (!*p)
408 continue;
409
410 token = match_token(p, tokens, args);
411 switch (token) {
dfe25020 412 case Opt_degraded:
edf24abe
CH
413 printk(KERN_INFO "btrfs: allowing degraded mounts\n");
414 btrfs_set_opt(info->mount_opt, DEGRADED);
dfe25020 415 break;
95e05289 416 case Opt_subvol:
73f73415 417 case Opt_subvolid:
e15d0542 418 case Opt_subvolrootid:
43e570b0 419 case Opt_device:
edf24abe 420 /*
43e570b0 421 * These are parsed by btrfs_parse_early_options
edf24abe
CH
422 * and can be happily ignored here.
423 */
b6cda9bc
CM
424 break;
425 case Opt_nodatasum:
067c28ad 426 printk(KERN_INFO "btrfs: setting nodatasum\n");
edf24abe 427 btrfs_set_opt(info->mount_opt, NODATASUM);
be20aa9d
CM
428 break;
429 case Opt_nodatacow:
bedb2cca
AP
430 if (!btrfs_test_opt(root, COMPRESS) ||
431 !btrfs_test_opt(root, FORCE_COMPRESS)) {
432 printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
433 } else {
434 printk(KERN_INFO "btrfs: setting nodatacow\n");
435 }
436 info->compress_type = BTRFS_COMPRESS_NONE;
437 btrfs_clear_opt(info->mount_opt, COMPRESS);
438 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
edf24abe
CH
439 btrfs_set_opt(info->mount_opt, NODATACOW);
440 btrfs_set_opt(info->mount_opt, NODATASUM);
95e05289 441 break;
a555f810 442 case Opt_compress_force:
261507a0
LZ
443 case Opt_compress_force_type:
444 compress_force = true;
1c697d4a 445 /* Fallthrough */
261507a0
LZ
446 case Opt_compress:
447 case Opt_compress_type:
448 if (token == Opt_compress ||
449 token == Opt_compress_force ||
450 strcmp(args[0].from, "zlib") == 0) {
451 compress_type = "zlib";
452 info->compress_type = BTRFS_COMPRESS_ZLIB;
063849ea 453 btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca
AP
454 btrfs_clear_opt(info->mount_opt, NODATACOW);
455 btrfs_clear_opt(info->mount_opt, NODATASUM);
a6fa6fae
LZ
456 } else if (strcmp(args[0].from, "lzo") == 0) {
457 compress_type = "lzo";
458 info->compress_type = BTRFS_COMPRESS_LZO;
063849ea 459 btrfs_set_opt(info->mount_opt, COMPRESS);
bedb2cca
AP
460 btrfs_clear_opt(info->mount_opt, NODATACOW);
461 btrfs_clear_opt(info->mount_opt, NODATASUM);
2b0ce2c2 462 btrfs_set_fs_incompat(info, COMPRESS_LZO);
063849ea
AH
463 } else if (strncmp(args[0].from, "no", 2) == 0) {
464 compress_type = "no";
465 info->compress_type = BTRFS_COMPRESS_NONE;
466 btrfs_clear_opt(info->mount_opt, COMPRESS);
467 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
468 compress_force = false;
261507a0
LZ
469 } else {
470 ret = -EINVAL;
471 goto out;
472 }
473
261507a0
LZ
474 if (compress_force) {
475 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
476 pr_info("btrfs: force %s compression\n",
477 compress_type);
478 } else
479 pr_info("btrfs: use %s compression\n",
480 compress_type);
a555f810 481 break;
e18e4809 482 case Opt_ssd:
edf24abe
CH
483 printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
484 btrfs_set_opt(info->mount_opt, SSD);
e18e4809 485 break;
451d7585
CM
486 case Opt_ssd_spread:
487 printk(KERN_INFO "btrfs: use spread ssd "
488 "allocation scheme\n");
489 btrfs_set_opt(info->mount_opt, SSD);
490 btrfs_set_opt(info->mount_opt, SSD_SPREAD);
491 break;
3b30c22f 492 case Opt_nossd:
451d7585
CM
493 printk(KERN_INFO "btrfs: not using ssd allocation "
494 "scheme\n");
c289811c 495 btrfs_set_opt(info->mount_opt, NOSSD);
3b30c22f 496 btrfs_clear_opt(info->mount_opt, SSD);
451d7585 497 btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
3b30c22f 498 break;
21ad10cf 499 case Opt_nobarrier:
edf24abe
CH
500 printk(KERN_INFO "btrfs: turning off barriers\n");
501 btrfs_set_opt(info->mount_opt, NOBARRIER);
21ad10cf 502 break;
4543df7e 503 case Opt_thread_pool:
2c334e87
WS
504 ret = match_int(&args[0], &intarg);
505 if (ret) {
506 goto out;
507 } else if (intarg > 0) {
4543df7e 508 info->thread_pool_size = intarg;
2c334e87
WS
509 } else {
510 ret = -EINVAL;
511 goto out;
512 }
4543df7e 513 break;
6f568d35 514 case Opt_max_inline:
edf24abe
CH
515 num = match_strdup(&args[0]);
516 if (num) {
91748467 517 info->max_inline = memparse(num, NULL);
edf24abe
CH
518 kfree(num);
519
15ada040
CM
520 if (info->max_inline) {
521 info->max_inline = max_t(u64,
522 info->max_inline,
523 root->sectorsize);
524 }
edf24abe 525 printk(KERN_INFO "btrfs: max_inline at %llu\n",
c1c9ff7c 526 info->max_inline);
2c334e87
WS
527 } else {
528 ret = -ENOMEM;
529 goto out;
6f568d35
CM
530 }
531 break;
8f662a76 532 case Opt_alloc_start:
edf24abe
CH
533 num = match_strdup(&args[0]);
534 if (num) {
c018daec 535 mutex_lock(&info->chunk_mutex);
91748467 536 info->alloc_start = memparse(num, NULL);
c018daec 537 mutex_unlock(&info->chunk_mutex);
edf24abe
CH
538 kfree(num);
539 printk(KERN_INFO
540 "btrfs: allocations start at %llu\n",
c1c9ff7c 541 info->alloc_start);
2c334e87
WS
542 } else {
543 ret = -ENOMEM;
544 goto out;
8f662a76
CM
545 }
546 break;
33268eaf
JB
547 case Opt_noacl:
548 root->fs_info->sb->s_flags &= ~MS_POSIXACL;
549 break;
3a5e1404
SW
550 case Opt_notreelog:
551 printk(KERN_INFO "btrfs: disabling tree log\n");
552 btrfs_set_opt(info->mount_opt, NOTREELOG);
553 break;
dccae999
SW
554 case Opt_flushoncommit:
555 printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
556 btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
557 break;
97e728d4 558 case Opt_ratio:
2c334e87
WS
559 ret = match_int(&args[0], &intarg);
560 if (ret) {
561 goto out;
562 } else if (intarg >= 0) {
97e728d4
JB
563 info->metadata_ratio = intarg;
564 printk(KERN_INFO "btrfs: metadata ratio %d\n",
565 info->metadata_ratio);
2c334e87
WS
566 } else {
567 ret = -EINVAL;
568 goto out;
97e728d4
JB
569 }
570 break;
e244a0ae
CH
571 case Opt_discard:
572 btrfs_set_opt(info->mount_opt, DISCARD);
573 break;
0af3d00b 574 case Opt_space_cache:
0af3d00b 575 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
0de90876 576 break;
f420ee1e
SB
577 case Opt_rescan_uuid_tree:
578 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
579 break;
73bc1876
JB
580 case Opt_no_space_cache:
581 printk(KERN_INFO "btrfs: disabling disk space caching\n");
582 btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
583 break;
4b9465cb
CM
584 case Opt_inode_cache:
585 printk(KERN_INFO "btrfs: enabling inode map caching\n");
586 btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
587 break;
88c2ba3b
JB
588 case Opt_clear_cache:
589 printk(KERN_INFO "btrfs: force clearing of disk cache\n");
590 btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
0af3d00b 591 break;
4260f7c7
SW
592 case Opt_user_subvol_rm_allowed:
593 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
594 break;
91435650
CM
595 case Opt_enospc_debug:
596 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
597 break;
4cb5300b 598 case Opt_defrag:
48940662 599 printk(KERN_INFO "btrfs: enabling auto defrag\n");
4cb5300b
CM
600 btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
601 break;
af31f5e5 602 case Opt_recovery:
48940662 603 printk(KERN_INFO "btrfs: enabling auto recovery\n");
af31f5e5
CM
604 btrfs_set_opt(info->mount_opt, RECOVERY);
605 break;
9555c6c1
ID
606 case Opt_skip_balance:
607 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
608 break;
21adbd5c
SB
609#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
610 case Opt_check_integrity_including_extent_data:
611 printk(KERN_INFO "btrfs: enabling check integrity"
612 " including extent data\n");
613 btrfs_set_opt(info->mount_opt,
614 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
615 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
616 break;
617 case Opt_check_integrity:
618 printk(KERN_INFO "btrfs: enabling check integrity\n");
619 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
620 break;
621 case Opt_check_integrity_print_mask:
2c334e87
WS
622 ret = match_int(&args[0], &intarg);
623 if (ret) {
624 goto out;
625 } else if (intarg >= 0) {
21adbd5c
SB
626 info->check_integrity_print_mask = intarg;
627 printk(KERN_INFO "btrfs:"
628 " check_integrity_print_mask 0x%x\n",
629 info->check_integrity_print_mask);
2c334e87
WS
630 } else {
631 ret = -EINVAL;
632 goto out;
21adbd5c
SB
633 }
634 break;
635#else
636 case Opt_check_integrity_including_extent_data:
637 case Opt_check_integrity:
638 case Opt_check_integrity_print_mask:
639 printk(KERN_ERR "btrfs: support for check_integrity*"
640 " not compiled in!\n");
641 ret = -EINVAL;
642 goto out;
643#endif
8c342930
JM
644 case Opt_fatal_errors:
645 if (strcmp(args[0].from, "panic") == 0)
646 btrfs_set_opt(info->mount_opt,
647 PANIC_ON_FATAL_ERROR);
648 else if (strcmp(args[0].from, "bug") == 0)
649 btrfs_clear_opt(info->mount_opt,
650 PANIC_ON_FATAL_ERROR);
651 else {
652 ret = -EINVAL;
653 goto out;
654 }
655 break;
8b87dc17
DS
656 case Opt_commit_interval:
657 intarg = 0;
658 ret = match_int(&args[0], &intarg);
659 if (ret < 0) {
660 printk(KERN_ERR
661 "btrfs: invalid commit interval\n");
662 ret = -EINVAL;
663 goto out;
664 }
665 if (intarg > 0) {
666 if (intarg > 300) {
667 printk(KERN_WARNING
668 "btrfs: excessive commit interval %d\n",
669 intarg);
670 }
671 info->commit_interval = intarg;
672 } else {
673 printk(KERN_INFO
674 "btrfs: using default commit interval %ds\n",
675 BTRFS_DEFAULT_COMMIT_INTERVAL);
676 info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
677 }
678 break;
a7a3f7ca
SW
679 case Opt_err:
680 printk(KERN_INFO "btrfs: unrecognized mount option "
681 "'%s'\n", p);
682 ret = -EINVAL;
683 goto out;
95e05289 684 default:
be20aa9d 685 break;
95e05289
CM
686 }
687 }
a7a3f7ca 688out:
73bc1876
JB
689 if (!ret && btrfs_test_opt(root, SPACE_CACHE))
690 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
da495ecc 691 kfree(orig);
a7a3f7ca 692 return ret;
edf24abe
CH
693}
694
695/*
696 * Parse mount options that are required early in the mount process.
697 *
698 * All other options will be parsed on much later in the mount process and
699 * only when we need to allocate a new super block.
700 */
97288f2c 701static int btrfs_parse_early_options(const char *options, fmode_t flags,
73f73415 702 void *holder, char **subvol_name, u64 *subvol_objectid,
5e2a4b25 703 struct btrfs_fs_devices **fs_devices)
edf24abe
CH
704{
705 substring_t args[MAX_OPT_ARGS];
83c8c9bd 706 char *device_name, *opts, *orig, *p;
1493381f 707 char *num = NULL;
edf24abe
CH
708 int error = 0;
709
710 if (!options)
830c4adb 711 return 0;
edf24abe
CH
712
713 /*
714 * strsep changes the string, duplicate it because parse_options
715 * gets called twice
716 */
717 opts = kstrdup(options, GFP_KERNEL);
718 if (!opts)
719 return -ENOMEM;
3f3d0bc0 720 orig = opts;
edf24abe
CH
721
722 while ((p = strsep(&opts, ",")) != NULL) {
723 int token;
724 if (!*p)
725 continue;
726
727 token = match_token(p, tokens, args);
728 switch (token) {
729 case Opt_subvol:
a90e8b6f 730 kfree(*subvol_name);
edf24abe 731 *subvol_name = match_strdup(&args[0]);
2c334e87
WS
732 if (!*subvol_name) {
733 error = -ENOMEM;
734 goto out;
735 }
edf24abe 736 break;
73f73415 737 case Opt_subvolid:
1493381f
WS
738 num = match_strdup(&args[0]);
739 if (num) {
740 *subvol_objectid = memparse(num, NULL);
741 kfree(num);
4849f01d 742 /* we want the original fs_tree */
1493381f 743 if (!*subvol_objectid)
4849f01d
JB
744 *subvol_objectid =
745 BTRFS_FS_TREE_OBJECTID;
2c334e87
WS
746 } else {
747 error = -EINVAL;
748 goto out;
4849f01d 749 }
73f73415 750 break;
e15d0542 751 case Opt_subvolrootid:
5e2a4b25
DS
752 printk(KERN_WARNING
753 "btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
e15d0542 754 break;
43e570b0 755 case Opt_device:
83c8c9bd
JL
756 device_name = match_strdup(&args[0]);
757 if (!device_name) {
758 error = -ENOMEM;
759 goto out;
760 }
761 error = btrfs_scan_one_device(device_name,
43e570b0 762 flags, holder, fs_devices);
83c8c9bd 763 kfree(device_name);
43e570b0 764 if (error)
830c4adb 765 goto out;
43e570b0 766 break;
edf24abe
CH
767 default:
768 break;
769 }
770 }
771
830c4adb 772out:
3f3d0bc0 773 kfree(orig);
edf24abe 774 return error;
95e05289
CM
775}
776
73f73415
JB
777static struct dentry *get_default_root(struct super_block *sb,
778 u64 subvol_objectid)
779{
815745cf
AV
780 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
781 struct btrfs_root *root = fs_info->tree_root;
73f73415
JB
782 struct btrfs_root *new_root;
783 struct btrfs_dir_item *di;
784 struct btrfs_path *path;
785 struct btrfs_key location;
786 struct inode *inode;
73f73415
JB
787 u64 dir_id;
788 int new = 0;
789
790 /*
791 * We have a specific subvol we want to mount, just setup location and
792 * go look up the root.
793 */
794 if (subvol_objectid) {
795 location.objectid = subvol_objectid;
796 location.type = BTRFS_ROOT_ITEM_KEY;
797 location.offset = (u64)-1;
798 goto find_root;
799 }
800
801 path = btrfs_alloc_path();
802 if (!path)
803 return ERR_PTR(-ENOMEM);
804 path->leave_spinning = 1;
805
806 /*
807 * Find the "default" dir item which points to the root item that we
808 * will mount by default if we haven't been given a specific subvolume
809 * to mount.
810 */
815745cf 811 dir_id = btrfs_super_root_dir(fs_info->super_copy);
73f73415 812 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
b0839166
JL
813 if (IS_ERR(di)) {
814 btrfs_free_path(path);
fb4f6f91 815 return ERR_CAST(di);
b0839166 816 }
73f73415
JB
817 if (!di) {
818 /*
819 * Ok the default dir item isn't there. This is weird since
820 * it's always been there, but don't freak out, just try and
821 * mount to root most subvolume.
822 */
823 btrfs_free_path(path);
824 dir_id = BTRFS_FIRST_FREE_OBJECTID;
815745cf 825 new_root = fs_info->fs_root;
73f73415
JB
826 goto setup_root;
827 }
828
829 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
830 btrfs_free_path(path);
831
832find_root:
815745cf 833 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
73f73415 834 if (IS_ERR(new_root))
d0b678cb 835 return ERR_CAST(new_root);
73f73415 836
73f73415
JB
837 dir_id = btrfs_root_dirid(&new_root->root_item);
838setup_root:
839 location.objectid = dir_id;
840 location.type = BTRFS_INODE_ITEM_KEY;
841 location.offset = 0;
842
843 inode = btrfs_iget(sb, &location, new_root, &new);
4cbd1149
DC
844 if (IS_ERR(inode))
845 return ERR_CAST(inode);
73f73415
JB
846
847 /*
848 * If we're just mounting the root most subvol put the inode and return
849 * a reference to the dentry. We will have already gotten a reference
850 * to the inode in btrfs_fill_super so we're good to go.
851 */
852 if (!new && sb->s_root->d_inode == inode) {
853 iput(inode);
854 return dget(sb->s_root);
855 }
856
ba5b8958 857 return d_obtain_alias(inode);
73f73415
JB
858}
859
d397712b 860static int btrfs_fill_super(struct super_block *sb,
8a4b83cc 861 struct btrfs_fs_devices *fs_devices,
d397712b 862 void *data, int silent)
75dfe396 863{
d397712b 864 struct inode *inode;
815745cf 865 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
5d4f98a2 866 struct btrfs_key key;
39279cc3 867 int err;
a429e513 868
39279cc3
CM
869 sb->s_maxbytes = MAX_LFS_FILESIZE;
870 sb->s_magic = BTRFS_SUPER_MAGIC;
871 sb->s_op = &btrfs_super_ops;
af53d29a 872 sb->s_d_op = &btrfs_dentry_operations;
be6e8dc0 873 sb->s_export_op = &btrfs_export_ops;
5103e947 874 sb->s_xattr = btrfs_xattr_handlers;
39279cc3 875 sb->s_time_gran = 1;
0eda294d 876#ifdef CONFIG_BTRFS_FS_POSIX_ACL
33268eaf 877 sb->s_flags |= MS_POSIXACL;
49cf6f45 878#endif
0c4d2d95 879 sb->s_flags |= MS_I_VERSION;
ad2b2c80
AV
880 err = open_ctree(sb, fs_devices, (char *)data);
881 if (err) {
39279cc3 882 printk("btrfs: open_ctree failed\n");
ad2b2c80 883 return err;
a429e513
CM
884 }
885
5d4f98a2
YZ
886 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
887 key.type = BTRFS_INODE_ITEM_KEY;
888 key.offset = 0;
98c7089c 889 inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
5d4f98a2
YZ
890 if (IS_ERR(inode)) {
891 err = PTR_ERR(inode);
39279cc3 892 goto fail_close;
f254e52c 893 }
f254e52c 894
48fde701
AV
895 sb->s_root = d_make_root(inode);
896 if (!sb->s_root) {
39279cc3
CM
897 err = -ENOMEM;
898 goto fail_close;
f254e52c 899 }
58176a96 900
6885f308 901 save_mount_options(sb, data);
90a887c9 902 cleancache_init_fs(sb);
59553edf 903 sb->s_flags |= MS_ACTIVE;
2619ba1f 904 return 0;
39279cc3
CM
905
906fail_close:
815745cf 907 close_ctree(fs_info->tree_root);
39279cc3 908 return err;
2619ba1f
CM
909}
910
6bf13c0c 911int btrfs_sync_fs(struct super_block *sb, int wait)
c5739bba
CM
912{
913 struct btrfs_trans_handle *trans;
815745cf
AV
914 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
915 struct btrfs_root *root = fs_info->tree_root;
2619ba1f 916
1abe9b8a 917 trace_btrfs_sync_fs(wait);
918
39279cc3 919 if (!wait) {
815745cf 920 filemap_flush(fs_info->btree_inode->i_mapping);
39279cc3
CM
921 return 0;
922 }
771ed689 923
f0de181c 924 btrfs_wait_all_ordered_extents(fs_info);
771ed689 925
d4edf39b 926 trans = btrfs_attach_transaction_barrier(root);
60376ce4 927 if (IS_ERR(trans)) {
354aa0fb
MX
928 /* no transaction, don't bother */
929 if (PTR_ERR(trans) == -ENOENT)
60376ce4 930 return 0;
98d5dc13 931 return PTR_ERR(trans);
60376ce4 932 }
bd7de2c9 933 return btrfs_commit_transaction(trans, root);
2c90e5d6
CM
934}
935
34c80b1d 936static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
a9572a15 937{
815745cf
AV
938 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
939 struct btrfs_root *root = info->tree_root;
200da64e 940 char *compress_type;
a9572a15
EP
941
942 if (btrfs_test_opt(root, DEGRADED))
943 seq_puts(seq, ",degraded");
944 if (btrfs_test_opt(root, NODATASUM))
945 seq_puts(seq, ",nodatasum");
946 if (btrfs_test_opt(root, NODATACOW))
947 seq_puts(seq, ",nodatacow");
948 if (btrfs_test_opt(root, NOBARRIER))
949 seq_puts(seq, ",nobarrier");
a9572a15 950 if (info->max_inline != 8192 * 1024)
c1c9ff7c 951 seq_printf(seq, ",max_inline=%llu", info->max_inline);
a9572a15 952 if (info->alloc_start != 0)
c1c9ff7c 953 seq_printf(seq, ",alloc_start=%llu", info->alloc_start);
a9572a15
EP
954 if (info->thread_pool_size != min_t(unsigned long,
955 num_online_cpus() + 2, 8))
956 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
200da64e
TI
957 if (btrfs_test_opt(root, COMPRESS)) {
958 if (info->compress_type == BTRFS_COMPRESS_ZLIB)
959 compress_type = "zlib";
960 else
961 compress_type = "lzo";
962 if (btrfs_test_opt(root, FORCE_COMPRESS))
963 seq_printf(seq, ",compress-force=%s", compress_type);
964 else
965 seq_printf(seq, ",compress=%s", compress_type);
966 }
c289811c
CM
967 if (btrfs_test_opt(root, NOSSD))
968 seq_puts(seq, ",nossd");
451d7585
CM
969 if (btrfs_test_opt(root, SSD_SPREAD))
970 seq_puts(seq, ",ssd_spread");
971 else if (btrfs_test_opt(root, SSD))
a9572a15 972 seq_puts(seq, ",ssd");
3a5e1404 973 if (btrfs_test_opt(root, NOTREELOG))
6b65c5c6 974 seq_puts(seq, ",notreelog");
dccae999 975 if (btrfs_test_opt(root, FLUSHONCOMMIT))
6b65c5c6 976 seq_puts(seq, ",flushoncommit");
20a5239a
MW
977 if (btrfs_test_opt(root, DISCARD))
978 seq_puts(seq, ",discard");
a9572a15
EP
979 if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
980 seq_puts(seq, ",noacl");
200da64e
TI
981 if (btrfs_test_opt(root, SPACE_CACHE))
982 seq_puts(seq, ",space_cache");
73bc1876 983 else
8965593e 984 seq_puts(seq, ",nospace_cache");
f420ee1e
SB
985 if (btrfs_test_opt(root, RESCAN_UUID_TREE))
986 seq_puts(seq, ",rescan_uuid_tree");
200da64e
TI
987 if (btrfs_test_opt(root, CLEAR_CACHE))
988 seq_puts(seq, ",clear_cache");
989 if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
990 seq_puts(seq, ",user_subvol_rm_allowed");
0942caa3
DS
991 if (btrfs_test_opt(root, ENOSPC_DEBUG))
992 seq_puts(seq, ",enospc_debug");
993 if (btrfs_test_opt(root, AUTO_DEFRAG))
994 seq_puts(seq, ",autodefrag");
995 if (btrfs_test_opt(root, INODE_MAP_CACHE))
996 seq_puts(seq, ",inode_cache");
9555c6c1
ID
997 if (btrfs_test_opt(root, SKIP_BALANCE))
998 seq_puts(seq, ",skip_balance");
8507d216
WS
999 if (btrfs_test_opt(root, RECOVERY))
1000 seq_puts(seq, ",recovery");
1001#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1002 if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
1003 seq_puts(seq, ",check_int_data");
1004 else if (btrfs_test_opt(root, CHECK_INTEGRITY))
1005 seq_puts(seq, ",check_int");
1006 if (info->check_integrity_print_mask)
1007 seq_printf(seq, ",check_int_print_mask=%d",
1008 info->check_integrity_print_mask);
1009#endif
1010 if (info->metadata_ratio)
1011 seq_printf(seq, ",metadata_ratio=%d",
1012 info->metadata_ratio);
8c342930
JM
1013 if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
1014 seq_puts(seq, ",fatal_errors=panic");
8b87dc17
DS
1015 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1016 seq_printf(seq, ",commit=%d", info->commit_interval);
a9572a15
EP
1017 return 0;
1018}
1019
a061fc8d 1020static int btrfs_test_super(struct super_block *s, void *data)
4b82d6e4 1021{
815745cf
AV
1022 struct btrfs_fs_info *p = data;
1023 struct btrfs_fs_info *fs_info = btrfs_sb(s);
4b82d6e4 1024
815745cf 1025 return fs_info->fs_devices == p->fs_devices;
4b82d6e4
Y
1026}
1027
450ba0ea
JB
1028static int btrfs_set_super(struct super_block *s, void *data)
1029{
6de1d09d
AV
1030 int err = set_anon_super(s, data);
1031 if (!err)
1032 s->s_fs_info = data;
1033 return err;
4b82d6e4
Y
1034}
1035
f9d9ef62
DS
1036/*
1037 * subvolumes are identified by ino 256
1038 */
1039static inline int is_subvolume_inode(struct inode *inode)
1040{
1041 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1042 return 1;
1043 return 0;
1044}
1045
830c4adb
JB
1046/*
1047 * This will strip out the subvol=%s argument for an argument string and add
1048 * subvolid=0 to make sure we get the actual tree root for path walking to the
1049 * subvol we want.
1050 */
1051static char *setup_root_args(char *args)
1052{
f60d16a8
JM
1053 unsigned len = strlen(args) + 2 + 1;
1054 char *src, *dst, *buf;
830c4adb
JB
1055
1056 /*
f60d16a8
JM
1057 * We need the same args as before, but with this substitution:
1058 * s!subvol=[^,]+!subvolid=0!
830c4adb 1059 *
f60d16a8
JM
1060 * Since the replacement string is up to 2 bytes longer than the
1061 * original, allocate strlen(args) + 2 + 1 bytes.
830c4adb 1062 */
830c4adb 1063
f60d16a8 1064 src = strstr(args, "subvol=");
830c4adb 1065 /* This shouldn't happen, but just in case.. */
f60d16a8
JM
1066 if (!src)
1067 return NULL;
1068
1069 buf = dst = kmalloc(len, GFP_NOFS);
1070 if (!buf)
830c4adb 1071 return NULL;
830c4adb
JB
1072
1073 /*
f60d16a8
JM
1074 * If the subvol= arg is not at the start of the string,
1075 * copy whatever precedes it into buf.
830c4adb 1076 */
f60d16a8
JM
1077 if (src != args) {
1078 *src++ = '\0';
1079 strcpy(buf, args);
1080 dst += strlen(args);
830c4adb
JB
1081 }
1082
f60d16a8
JM
1083 strcpy(dst, "subvolid=0");
1084 dst += strlen("subvolid=0");
830c4adb
JB
1085
1086 /*
f60d16a8
JM
1087 * If there is a "," after the original subvol=... string,
1088 * copy that suffix into our buffer. Otherwise, we're done.
830c4adb 1089 */
f60d16a8
JM
1090 src = strchr(src, ',');
1091 if (src)
1092 strcpy(dst, src);
830c4adb 1093
f60d16a8 1094 return buf;
830c4adb
JB
1095}
1096
1097static struct dentry *mount_subvol(const char *subvol_name, int flags,
1098 const char *device_name, char *data)
1099{
830c4adb
JB
1100 struct dentry *root;
1101 struct vfsmount *mnt;
830c4adb 1102 char *newargs;
830c4adb
JB
1103
1104 newargs = setup_root_args(data);
1105 if (!newargs)
1106 return ERR_PTR(-ENOMEM);
1107 mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
1108 newargs);
1109 kfree(newargs);
1110 if (IS_ERR(mnt))
1111 return ERR_CAST(mnt);
1112
ea441d11 1113 root = mount_subtree(mnt, subvol_name);
830c4adb 1114
ea441d11
AV
1115 if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1116 struct super_block *s = root->d_sb;
1117 dput(root);
1118 root = ERR_PTR(-EINVAL);
1119 deactivate_locked_super(s);
f9d9ef62
DS
1120 printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1121 subvol_name);
f9d9ef62
DS
1122 }
1123
830c4adb
JB
1124 return root;
1125}
450ba0ea 1126
edf24abe
CH
1127/*
1128 * Find a superblock for the given device / mount point.
1129 *
1130 * Note: This is based on get_sb_bdev from fs/super.c with a few additions
1131 * for multiple device setup. Make sure to keep it in sync.
1132 */
061dbc6b 1133static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
306e16ce 1134 const char *device_name, void *data)
4b82d6e4
Y
1135{
1136 struct block_device *bdev = NULL;
1137 struct super_block *s;
1138 struct dentry *root;
8a4b83cc 1139 struct btrfs_fs_devices *fs_devices = NULL;
450ba0ea 1140 struct btrfs_fs_info *fs_info = NULL;
97288f2c 1141 fmode_t mode = FMODE_READ;
73f73415
JB
1142 char *subvol_name = NULL;
1143 u64 subvol_objectid = 0;
4b82d6e4
Y
1144 int error = 0;
1145
97288f2c
CH
1146 if (!(flags & MS_RDONLY))
1147 mode |= FMODE_WRITE;
1148
1149 error = btrfs_parse_early_options(data, mode, fs_type,
73f73415 1150 &subvol_name, &subvol_objectid,
5e2a4b25 1151 &fs_devices);
f23c8af8
ID
1152 if (error) {
1153 kfree(subvol_name);
061dbc6b 1154 return ERR_PTR(error);
f23c8af8 1155 }
edf24abe 1156
830c4adb
JB
1157 if (subvol_name) {
1158 root = mount_subvol(subvol_name, flags, device_name, data);
1159 kfree(subvol_name);
1160 return root;
1161 }
1162
306e16ce 1163 error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
8a4b83cc 1164 if (error)
830c4adb 1165 return ERR_PTR(error);
4b82d6e4 1166
450ba0ea
JB
1167 /*
1168 * Setup a dummy root and fs_info for test/set super. This is because
1169 * we don't actually fill this stuff out until open_ctree, but we need
1170 * it for searching for existing supers, so this lets us do that and
1171 * then open_ctree will properly initialize everything later.
1172 */
1173 fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
04d21a24
ID
1174 if (!fs_info)
1175 return ERR_PTR(-ENOMEM);
1176
450ba0ea 1177 fs_info->fs_devices = fs_devices;
450ba0ea 1178
6c41761f
DS
1179 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1180 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1181 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1182 error = -ENOMEM;
04d21a24
ID
1183 goto error_fs_info;
1184 }
1185
1186 error = btrfs_open_devices(fs_devices, mode, fs_type);
1187 if (error)
1188 goto error_fs_info;
1189
1190 if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
1191 error = -EACCES;
6c41761f
DS
1192 goto error_close_devices;
1193 }
1194
dfe25020 1195 bdev = fs_devices->latest_bdev;
9249e17f
DH
1196 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
1197 fs_info);
830c4adb
JB
1198 if (IS_ERR(s)) {
1199 error = PTR_ERR(s);
1200 goto error_close_devices;
1201 }
4b82d6e4
Y
1202
1203 if (s->s_root) {
2b82032c 1204 btrfs_close_devices(fs_devices);
6c41761f 1205 free_fs_info(fs_info);
59553edf
AV
1206 if ((flags ^ s->s_flags) & MS_RDONLY)
1207 error = -EBUSY;
4b82d6e4
Y
1208 } else {
1209 char b[BDEVNAME_SIZE];
1210
4b82d6e4 1211 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
815745cf 1212 btrfs_sb(s)->bdev_holder = fs_type;
8a4b83cc
CM
1213 error = btrfs_fill_super(s, fs_devices, data,
1214 flags & MS_SILENT ? 1 : 0);
4b82d6e4
Y
1215 }
1216
59553edf
AV
1217 root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
1218 if (IS_ERR(root))
830c4adb 1219 deactivate_locked_super(s);
4b82d6e4 1220
061dbc6b 1221 return root;
4b82d6e4 1222
c146afad 1223error_close_devices:
8a4b83cc 1224 btrfs_close_devices(fs_devices);
04d21a24 1225error_fs_info:
6c41761f 1226 free_fs_info(fs_info);
061dbc6b 1227 return ERR_PTR(error);
4b82d6e4 1228}
2e635a27 1229
0d2450ab
ST
1230static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
1231{
1232 spin_lock_irq(&workers->lock);
1233 workers->max_workers = new_limit;
1234 spin_unlock_irq(&workers->lock);
1235}
1236
1237static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1238 int new_pool_size, int old_pool_size)
1239{
1240 if (new_pool_size == old_pool_size)
1241 return;
1242
1243 fs_info->thread_pool_size = new_pool_size;
1244
1245 printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
1246 old_pool_size, new_pool_size);
1247
1248 btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
1249 btrfs_set_max_workers(&fs_info->workers, new_pool_size);
1250 btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
1251 btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
1252 btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
1253 btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
1254 btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
1255 btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
1256 btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
1257 btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
1258 btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
1259 btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
1260 btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
ff023aac
SB
1261 btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
1262 new_pool_size);
0d2450ab
ST
1263}
1264
f42a34b2 1265static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
dc81cdc5
MX
1266{
1267 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
f42a34b2 1268}
dc81cdc5 1269
f42a34b2
MX
1270static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1271 unsigned long old_opts, int flags)
1272{
dc81cdc5
MX
1273 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1274 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1275 (flags & MS_RDONLY))) {
1276 /* wait for any defraggers to finish */
1277 wait_event(fs_info->transaction_wait,
1278 (atomic_read(&fs_info->defrag_running) == 0));
1279 if (flags & MS_RDONLY)
1280 sync_filesystem(fs_info->sb);
1281 }
1282}
1283
1284static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1285 unsigned long old_opts)
1286{
1287 /*
1288 * We need cleanup all defragable inodes if the autodefragment is
1289 * close or the fs is R/O.
1290 */
1291 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1292 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1293 (fs_info->sb->s_flags & MS_RDONLY))) {
1294 btrfs_cleanup_defrag_inodes(fs_info);
1295 }
1296
1297 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1298}
1299
c146afad
YZ
1300static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1301{
815745cf
AV
1302 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1303 struct btrfs_root *root = fs_info->tree_root;
49b25e05
JM
1304 unsigned old_flags = sb->s_flags;
1305 unsigned long old_opts = fs_info->mount_opt;
1306 unsigned long old_compress_type = fs_info->compress_type;
1307 u64 old_max_inline = fs_info->max_inline;
1308 u64 old_alloc_start = fs_info->alloc_start;
1309 int old_thread_pool_size = fs_info->thread_pool_size;
1310 unsigned int old_metadata_ratio = fs_info->metadata_ratio;
c146afad
YZ
1311 int ret;
1312
f42a34b2 1313 btrfs_remount_prepare(fs_info);
dc81cdc5 1314
b288052e 1315 ret = btrfs_parse_options(root, data);
49b25e05
JM
1316 if (ret) {
1317 ret = -EINVAL;
1318 goto restore;
1319 }
b288052e 1320
f42a34b2 1321 btrfs_remount_begin(fs_info, old_opts, *flags);
0d2450ab
ST
1322 btrfs_resize_thread_pool(fs_info,
1323 fs_info->thread_pool_size, old_thread_pool_size);
1324
c146afad 1325 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
dc81cdc5 1326 goto out;
c146afad
YZ
1327
1328 if (*flags & MS_RDONLY) {
8dabb742
SB
1329 /*
1330 * this also happens on 'umount -rf' or on shutdown, when
1331 * the filesystem is busy.
1332 */
c146afad
YZ
1333 sb->s_flags |= MS_RDONLY;
1334
8dabb742
SB
1335 btrfs_dev_replace_suspend_for_unmount(fs_info);
1336 btrfs_scrub_cancel(fs_info);
061594ef 1337 btrfs_pause_balance(fs_info);
8dabb742 1338
49b25e05
JM
1339 ret = btrfs_commit_super(root);
1340 if (ret)
1341 goto restore;
c146afad 1342 } else {
6ef3de9c
DS
1343 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
1344 btrfs_err(fs_info,
1345 "Remounting read-write after error is not allowed\n");
1346 ret = -EINVAL;
1347 goto restore;
1348 }
8a3db184 1349 if (fs_info->fs_devices->rw_devices == 0) {
49b25e05
JM
1350 ret = -EACCES;
1351 goto restore;
8a3db184 1352 }
2b82032c 1353
292fd7fc
SB
1354 if (fs_info->fs_devices->missing_devices >
1355 fs_info->num_tolerated_disk_barrier_failures &&
1356 !(*flags & MS_RDONLY)) {
1357 printk(KERN_WARNING
1358 "Btrfs: too many missing devices, writeable remount is not allowed\n");
1359 ret = -EACCES;
1360 goto restore;
1361 }
1362
8a3db184 1363 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
49b25e05
JM
1364 ret = -EINVAL;
1365 goto restore;
8a3db184 1366 }
c146afad 1367
815745cf 1368 ret = btrfs_cleanup_fs_roots(fs_info);
49b25e05
JM
1369 if (ret)
1370 goto restore;
c146afad 1371
d68fc57b
YZ
1372 /* recover relocation */
1373 ret = btrfs_recover_relocation(root);
49b25e05
JM
1374 if (ret)
1375 goto restore;
c146afad 1376
2b6ba629
ID
1377 ret = btrfs_resume_balance_async(fs_info);
1378 if (ret)
1379 goto restore;
1380
8dabb742
SB
1381 ret = btrfs_resume_dev_replace_async(fs_info);
1382 if (ret) {
1383 pr_warn("btrfs: failed to resume dev_replace\n");
1384 goto restore;
1385 }
94aebfb2
JB
1386
1387 if (!fs_info->uuid_root) {
1388 pr_info("btrfs: creating UUID tree\n");
1389 ret = btrfs_create_uuid_tree(fs_info);
1390 if (ret) {
1391 pr_warn("btrfs: failed to create the uuid tree"
1392 "%d\n", ret);
1393 goto restore;
1394 }
1395 }
c146afad
YZ
1396 sb->s_flags &= ~MS_RDONLY;
1397 }
dc81cdc5
MX
1398out:
1399 btrfs_remount_cleanup(fs_info, old_opts);
c146afad 1400 return 0;
49b25e05
JM
1401
1402restore:
1403 /* We've hit an error - don't reset MS_RDONLY */
1404 if (sb->s_flags & MS_RDONLY)
1405 old_flags |= MS_RDONLY;
1406 sb->s_flags = old_flags;
1407 fs_info->mount_opt = old_opts;
1408 fs_info->compress_type = old_compress_type;
1409 fs_info->max_inline = old_max_inline;
c018daec 1410 mutex_lock(&fs_info->chunk_mutex);
49b25e05 1411 fs_info->alloc_start = old_alloc_start;
c018daec 1412 mutex_unlock(&fs_info->chunk_mutex);
0d2450ab
ST
1413 btrfs_resize_thread_pool(fs_info,
1414 old_thread_pool_size, fs_info->thread_pool_size);
49b25e05 1415 fs_info->metadata_ratio = old_metadata_ratio;
dc81cdc5 1416 btrfs_remount_cleanup(fs_info, old_opts);
49b25e05 1417 return ret;
c146afad
YZ
1418}
1419
bcd53741
AJ
1420/* Used to sort the devices by max_avail(descending sort) */
1421static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1422 const void *dev_info2)
1423{
1424 if (((struct btrfs_device_info *)dev_info1)->max_avail >
1425 ((struct btrfs_device_info *)dev_info2)->max_avail)
1426 return -1;
1427 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1428 ((struct btrfs_device_info *)dev_info2)->max_avail)
1429 return 1;
1430 else
1431 return 0;
1432}
1433
1434/*
1435 * sort the devices by max_avail, in which max free extent size of each device
1436 * is stored.(Descending Sort)
1437 */
1438static inline void btrfs_descending_sort_devices(
1439 struct btrfs_device_info *devices,
1440 size_t nr_devices)
1441{
1442 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1443 btrfs_cmp_device_free_bytes, NULL);
1444}
1445
6d07bcec
MX
1446/*
1447 * The helper to calc the free space on the devices that can be used to store
1448 * file data.
1449 */
1450static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
1451{
1452 struct btrfs_fs_info *fs_info = root->fs_info;
1453 struct btrfs_device_info *devices_info;
1454 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1455 struct btrfs_device *device;
1456 u64 skip_space;
1457 u64 type;
1458 u64 avail_space;
1459 u64 used_space;
1460 u64 min_stripe_size;
39fb26c3 1461 int min_stripes = 1, num_stripes = 1;
6d07bcec
MX
1462 int i = 0, nr_devices;
1463 int ret;
1464
b772a86e 1465 nr_devices = fs_info->fs_devices->open_devices;
6d07bcec
MX
1466 BUG_ON(!nr_devices);
1467
1468 devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
1469 GFP_NOFS);
1470 if (!devices_info)
1471 return -ENOMEM;
1472
1473 /* calc min stripe number for data space alloction */
1474 type = btrfs_get_alloc_profile(root, 1);
39fb26c3 1475 if (type & BTRFS_BLOCK_GROUP_RAID0) {
6d07bcec 1476 min_stripes = 2;
39fb26c3
MX
1477 num_stripes = nr_devices;
1478 } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
6d07bcec 1479 min_stripes = 2;
39fb26c3
MX
1480 num_stripes = 2;
1481 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6d07bcec 1482 min_stripes = 4;
39fb26c3
MX
1483 num_stripes = 4;
1484 }
6d07bcec
MX
1485
1486 if (type & BTRFS_BLOCK_GROUP_DUP)
1487 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
1488 else
1489 min_stripe_size = BTRFS_STRIPE_LEN;
1490
b772a86e 1491 list_for_each_entry(device, &fs_devices->devices, dev_list) {
63a212ab
SB
1492 if (!device->in_fs_metadata || !device->bdev ||
1493 device->is_tgtdev_for_dev_replace)
6d07bcec
MX
1494 continue;
1495
1496 avail_space = device->total_bytes - device->bytes_used;
1497
1498 /* align with stripe_len */
1499 do_div(avail_space, BTRFS_STRIPE_LEN);
1500 avail_space *= BTRFS_STRIPE_LEN;
1501
1502 /*
1503 * In order to avoid overwritting the superblock on the drive,
1504 * btrfs starts at an offset of at least 1MB when doing chunk
1505 * allocation.
1506 */
1507 skip_space = 1024 * 1024;
1508
1509 /* user can set the offset in fs_info->alloc_start. */
1510 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
1511 device->total_bytes)
1512 skip_space = max(fs_info->alloc_start, skip_space);
1513
1514 /*
1515 * btrfs can not use the free space in [0, skip_space - 1],
1516 * we must subtract it from the total. In order to implement
1517 * it, we account the used space in this range first.
1518 */
1519 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
1520 &used_space);
1521 if (ret) {
1522 kfree(devices_info);
1523 return ret;
1524 }
1525
1526 /* calc the free space in [0, skip_space - 1] */
1527 skip_space -= used_space;
1528
1529 /*
1530 * we can use the free space in [0, skip_space - 1], subtract
1531 * it from the total.
1532 */
1533 if (avail_space && avail_space >= skip_space)
1534 avail_space -= skip_space;
1535 else
1536 avail_space = 0;
1537
1538 if (avail_space < min_stripe_size)
1539 continue;
1540
1541 devices_info[i].dev = device;
1542 devices_info[i].max_avail = avail_space;
1543
1544 i++;
1545 }
1546
1547 nr_devices = i;
1548
1549 btrfs_descending_sort_devices(devices_info, nr_devices);
1550
1551 i = nr_devices - 1;
1552 avail_space = 0;
1553 while (nr_devices >= min_stripes) {
39fb26c3
MX
1554 if (num_stripes > nr_devices)
1555 num_stripes = nr_devices;
1556
6d07bcec
MX
1557 if (devices_info[i].max_avail >= min_stripe_size) {
1558 int j;
1559 u64 alloc_size;
1560
39fb26c3 1561 avail_space += devices_info[i].max_avail * num_stripes;
6d07bcec 1562 alloc_size = devices_info[i].max_avail;
39fb26c3 1563 for (j = i + 1 - num_stripes; j <= i; j++)
6d07bcec
MX
1564 devices_info[j].max_avail -= alloc_size;
1565 }
1566 i--;
1567 nr_devices--;
1568 }
1569
1570 kfree(devices_info);
1571 *free_bytes = avail_space;
1572 return 0;
1573}
1574
8fd17795
CM
1575static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1576{
815745cf
AV
1577 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1578 struct btrfs_super_block *disk_super = fs_info->super_copy;
1579 struct list_head *head = &fs_info->space_info;
bd4d1088
JB
1580 struct btrfs_space_info *found;
1581 u64 total_used = 0;
6d07bcec 1582 u64 total_free_data = 0;
db94535d 1583 int bits = dentry->d_sb->s_blocksize_bits;
815745cf 1584 __be32 *fsid = (__be32 *)fs_info->fsid;
6d07bcec 1585 int ret;
8fd17795 1586
6d07bcec 1587 /* holding chunk_muext to avoid allocating new chunks */
815745cf 1588 mutex_lock(&fs_info->chunk_mutex);
bd4d1088 1589 rcu_read_lock();
89a55897 1590 list_for_each_entry_rcu(found, head, list) {
6d07bcec
MX
1591 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1592 total_free_data += found->disk_total - found->disk_used;
1593 total_free_data -=
1594 btrfs_account_ro_block_groups_free_space(found);
1595 }
1596
b742bb82 1597 total_used += found->disk_used;
89a55897 1598 }
bd4d1088
JB
1599 rcu_read_unlock();
1600
8fd17795 1601 buf->f_namelen = BTRFS_NAME_LEN;
db94535d 1602 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
bd4d1088 1603 buf->f_bfree = buf->f_blocks - (total_used >> bits);
8fd17795
CM
1604 buf->f_bsize = dentry->d_sb->s_blocksize;
1605 buf->f_type = BTRFS_SUPER_MAGIC;
6d07bcec 1606 buf->f_bavail = total_free_data;
815745cf 1607 ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
6d07bcec 1608 if (ret) {
815745cf 1609 mutex_unlock(&fs_info->chunk_mutex);
6d07bcec
MX
1610 return ret;
1611 }
1612 buf->f_bavail += total_free_data;
1613 buf->f_bavail = buf->f_bavail >> bits;
815745cf 1614 mutex_unlock(&fs_info->chunk_mutex);
d397712b 1615
9d03632e 1616 /* We treat it as constant endianness (it doesn't matter _which_)
d397712b 1617 because we want the fsid to come out the same whether mounted
9d03632e
DW
1618 on a big-endian or little-endian host */
1619 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
1620 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
32d48fa1
DW
1621 /* Mask in the root object ID too, to disambiguate subvols */
1622 buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
1623 buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
1624
8fd17795
CM
1625 return 0;
1626}
b5133862 1627
aea52e19
AV
1628static void btrfs_kill_super(struct super_block *sb)
1629{
815745cf 1630 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
aea52e19 1631 kill_anon_super(sb);
d22ca7de 1632 free_fs_info(fs_info);
aea52e19
AV
1633}
1634
2e635a27
CM
1635static struct file_system_type btrfs_fs_type = {
1636 .owner = THIS_MODULE,
1637 .name = "btrfs",
061dbc6b 1638 .mount = btrfs_mount,
aea52e19 1639 .kill_sb = btrfs_kill_super,
2e635a27
CM
1640 .fs_flags = FS_REQUIRES_DEV,
1641};
7f78e035 1642MODULE_ALIAS_FS("btrfs");
a9218f6b 1643
d352ac68
CM
1644/*
1645 * used by btrfsctl to scan devices when no FS is mounted
1646 */
8a4b83cc
CM
1647static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
1648 unsigned long arg)
1649{
1650 struct btrfs_ioctl_vol_args *vol;
1651 struct btrfs_fs_devices *fs_devices;
c071fcfd 1652 int ret = -ENOTTY;
8a4b83cc 1653
e441d54d
CM
1654 if (!capable(CAP_SYS_ADMIN))
1655 return -EPERM;
1656
dae7b665
LZ
1657 vol = memdup_user((void __user *)arg, sizeof(*vol));
1658 if (IS_ERR(vol))
1659 return PTR_ERR(vol);
c071fcfd 1660
8a4b83cc
CM
1661 switch (cmd) {
1662 case BTRFS_IOC_SCAN_DEV:
97288f2c 1663 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
8a4b83cc
CM
1664 &btrfs_fs_type, &fs_devices);
1665 break;
02db0844
JB
1666 case BTRFS_IOC_DEVICES_READY:
1667 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
1668 &btrfs_fs_type, &fs_devices);
1669 if (ret)
1670 break;
1671 ret = !(fs_devices->num_devices == fs_devices->total_devices);
1672 break;
8a4b83cc 1673 }
dae7b665 1674
8a4b83cc 1675 kfree(vol);
f819d837 1676 return ret;
8a4b83cc
CM
1677}
1678
0176260f 1679static int btrfs_freeze(struct super_block *sb)
ed0dab6b 1680{
354aa0fb
MX
1681 struct btrfs_trans_handle *trans;
1682 struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1683
d4edf39b 1684 trans = btrfs_attach_transaction_barrier(root);
354aa0fb
MX
1685 if (IS_ERR(trans)) {
1686 /* no transaction, don't bother */
1687 if (PTR_ERR(trans) == -ENOENT)
1688 return 0;
1689 return PTR_ERR(trans);
1690 }
1691 return btrfs_commit_transaction(trans, root);
ed0dab6b
Y
1692}
1693
0176260f 1694static int btrfs_unfreeze(struct super_block *sb)
ed0dab6b 1695{
0176260f 1696 return 0;
ed0dab6b 1697}
2e635a27 1698
9c5085c1
JB
1699static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
1700{
1701 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
1702 struct btrfs_fs_devices *cur_devices;
1703 struct btrfs_device *dev, *first_dev = NULL;
1704 struct list_head *head;
1705 struct rcu_string *name;
1706
1707 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1708 cur_devices = fs_info->fs_devices;
1709 while (cur_devices) {
1710 head = &cur_devices->devices;
1711 list_for_each_entry(dev, head, dev_list) {
aa9ddcd4
JB
1712 if (dev->missing)
1713 continue;
9c5085c1
JB
1714 if (!first_dev || dev->devid < first_dev->devid)
1715 first_dev = dev;
1716 }
1717 cur_devices = cur_devices->seed;
1718 }
1719
1720 if (first_dev) {
1721 rcu_read_lock();
1722 name = rcu_dereference(first_dev->name);
1723 seq_escape(m, name->str, " \t\n\\");
1724 rcu_read_unlock();
1725 } else {
1726 WARN_ON(1);
1727 }
1728 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1729 return 0;
1730}
1731
b87221de 1732static const struct super_operations btrfs_super_ops = {
76dda93c 1733 .drop_inode = btrfs_drop_inode,
bd555975 1734 .evict_inode = btrfs_evict_inode,
e20d96d6 1735 .put_super = btrfs_put_super,
d5719762 1736 .sync_fs = btrfs_sync_fs,
a9572a15 1737 .show_options = btrfs_show_options,
9c5085c1 1738 .show_devname = btrfs_show_devname,
4730a4bc 1739 .write_inode = btrfs_write_inode,
2c90e5d6
CM
1740 .alloc_inode = btrfs_alloc_inode,
1741 .destroy_inode = btrfs_destroy_inode,
8fd17795 1742 .statfs = btrfs_statfs,
c146afad 1743 .remount_fs = btrfs_remount,
0176260f
LT
1744 .freeze_fs = btrfs_freeze,
1745 .unfreeze_fs = btrfs_unfreeze,
e20d96d6 1746};
a9218f6b
CM
1747
1748static const struct file_operations btrfs_ctl_fops = {
1749 .unlocked_ioctl = btrfs_control_ioctl,
1750 .compat_ioctl = btrfs_control_ioctl,
1751 .owner = THIS_MODULE,
6038f373 1752 .llseek = noop_llseek,
a9218f6b
CM
1753};
1754
1755static struct miscdevice btrfs_misc = {
578454ff 1756 .minor = BTRFS_MINOR,
a9218f6b
CM
1757 .name = "btrfs-control",
1758 .fops = &btrfs_ctl_fops
1759};
1760
578454ff
KS
1761MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1762MODULE_ALIAS("devname:btrfs-control");
1763
a9218f6b
CM
1764static int btrfs_interface_init(void)
1765{
1766 return misc_register(&btrfs_misc);
1767}
1768
b2950863 1769static void btrfs_interface_exit(void)
a9218f6b
CM
1770{
1771 if (misc_deregister(&btrfs_misc) < 0)
48940662 1772 printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
a9218f6b
CM
1773}
1774
85965600
DS
1775static void btrfs_print_info(void)
1776{
1777 printk(KERN_INFO "Btrfs loaded"
1778#ifdef CONFIG_BTRFS_DEBUG
1779 ", debug=on"
1780#endif
79556c3d
SB
1781#ifdef CONFIG_BTRFS_ASSERT
1782 ", assert=on"
1783#endif
85965600
DS
1784#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1785 ", integrity-checker=on"
1786#endif
1787 "\n");
1788}
1789
dc11dd5d
JB
1790static int btrfs_run_sanity_tests(void)
1791{
06ea65a3
JB
1792 int ret;
1793
1794 ret = btrfs_test_free_space_cache();
1795 if (ret)
1796 return ret;
1797 return btrfs_test_extent_buffer_operations();
dc11dd5d
JB
1798}
1799
2e635a27
CM
1800static int __init init_btrfs_fs(void)
1801{
2c90e5d6 1802 int err;
58176a96
JB
1803
1804 err = btrfs_init_sysfs();
1805 if (err)
1806 return err;
1807
143bede5 1808 btrfs_init_compress();
d1310b2e 1809
261507a0
LZ
1810 err = btrfs_init_cachep();
1811 if (err)
1812 goto free_compress;
1813
d1310b2e 1814 err = extent_io_init();
2f4cbe64
WB
1815 if (err)
1816 goto free_cachep;
1817
d1310b2e
CM
1818 err = extent_map_init();
1819 if (err)
1820 goto free_extent_io;
1821
6352b91d 1822 err = ordered_data_init();
2f4cbe64
WB
1823 if (err)
1824 goto free_extent_map;
c8b97818 1825
6352b91d
MX
1826 err = btrfs_delayed_inode_init();
1827 if (err)
1828 goto free_ordered_data;
1829
9247f317 1830 err = btrfs_auto_defrag_init();
16cdcec7
MX
1831 if (err)
1832 goto free_delayed_inode;
1833
78a6184a 1834 err = btrfs_delayed_ref_init();
9247f317
MX
1835 if (err)
1836 goto free_auto_defrag;
1837
b9e9a6cb
WS
1838 err = btrfs_prelim_ref_init();
1839 if (err)
1840 goto free_prelim_ref;
1841
78a6184a
MX
1842 err = btrfs_interface_init();
1843 if (err)
1844 goto free_delayed_ref;
1845
e565d4b9
JS
1846 btrfs_init_lockdep();
1847
85965600 1848 btrfs_print_info();
dc11dd5d
JB
1849
1850 err = btrfs_run_sanity_tests();
1851 if (err)
1852 goto unregister_ioctl;
1853
1854 err = register_filesystem(&btrfs_fs_type);
1855 if (err)
1856 goto unregister_ioctl;
74255aa0 1857
2f4cbe64
WB
1858 return 0;
1859
a9218f6b
CM
1860unregister_ioctl:
1861 btrfs_interface_exit();
b9e9a6cb
WS
1862free_prelim_ref:
1863 btrfs_prelim_ref_exit();
78a6184a
MX
1864free_delayed_ref:
1865 btrfs_delayed_ref_exit();
9247f317
MX
1866free_auto_defrag:
1867 btrfs_auto_defrag_exit();
16cdcec7
MX
1868free_delayed_inode:
1869 btrfs_delayed_inode_exit();
6352b91d
MX
1870free_ordered_data:
1871 ordered_data_exit();
2f4cbe64
WB
1872free_extent_map:
1873 extent_map_exit();
d1310b2e
CM
1874free_extent_io:
1875 extent_io_exit();
2f4cbe64
WB
1876free_cachep:
1877 btrfs_destroy_cachep();
261507a0
LZ
1878free_compress:
1879 btrfs_exit_compress();
2f4cbe64
WB
1880 btrfs_exit_sysfs();
1881 return err;
2e635a27
CM
1882}
1883
1884static void __exit exit_btrfs_fs(void)
1885{
39279cc3 1886 btrfs_destroy_cachep();
78a6184a 1887 btrfs_delayed_ref_exit();
9247f317 1888 btrfs_auto_defrag_exit();
16cdcec7 1889 btrfs_delayed_inode_exit();
b9e9a6cb 1890 btrfs_prelim_ref_exit();
6352b91d 1891 ordered_data_exit();
a52d9a80 1892 extent_map_exit();
d1310b2e 1893 extent_io_exit();
a9218f6b 1894 btrfs_interface_exit();
2e635a27 1895 unregister_filesystem(&btrfs_fs_type);
58176a96 1896 btrfs_exit_sysfs();
8a4b83cc 1897 btrfs_cleanup_fs_uuids();
261507a0 1898 btrfs_exit_compress();
2e635a27
CM
1899}
1900
1901module_init(init_btrfs_fs)
1902module_exit(exit_btrfs_fs)
1903
1904MODULE_LICENSE("GPL");
This page took 0.399028 seconds and 5 git commands to generate.