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