1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include <linux/blkdev.h>
7 #include <linux/module.h>
8 #include <linux/fs.h>
9 #include <linux/pagemap.h>
10 #include <linux/highmem.h>
11 #include <linux/time.h>
12 #include <linux/init.h>
13 #include <linux/seq_file.h>
14 #include <linux/string.h>
15 #include <linux/backing-dev.h>
16 #include <linux/mount.h>
17 #include <linux/writeback.h>
18 #include <linux/statfs.h>
19 #include <linux/compat.h>
20 #include <linux/parser.h>
21 #include <linux/ctype.h>
22 #include <linux/namei.h>
23 #include <linux/miscdevice.h>
24 #include <linux/magic.h>
25 #include <linux/slab.h>
26 #include <linux/cleancache.h>
27 #include <linux/ratelimit.h>
28 #include <linux/crc32c.h>
29 #include <linux/btrfs.h>
30 #include "delayed-inode.h"
31 #include "ctree.h"
32 #include "disk-io.h"
33 #include "transaction.h"
34 #include "btrfs_inode.h"
35 #include "print-tree.h"
36 #include "props.h"
37 #include "xattr.h"
38 #include "volumes.h"
39 #include "export.h"
40 #include "compression.h"
41 #include "rcu-string.h"
42 #include "dev-replace.h"
43 #include "free-space-cache.h"
44 #include "backref.h"
45 #include "space-info.h"
46 #include "sysfs.h"
47 #include "zoned.h"
48 #include "tests/btrfs-tests.h"
49 #include "block-group.h"
50 #include "discard.h"
51 #include "qgroup.h"
52 #include "raid56.h"
53 #define CREATE_TRACE_POINTS
54 #include <trace/events/btrfs.h>
55
56 static const struct super_operations btrfs_super_ops;
57
58 /*
59 * Types for mounting the default subvolume and a subvolume explicitly
60 * requested by subvol=/path. That way the callchain is straightforward and we
61 * don't have to play tricks with the mount options and recursive calls to
62 * btrfs_mount.
63 *
64 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
65 */
66 static struct file_system_type btrfs_fs_type;
67 static struct file_system_type btrfs_root_fs_type;
68
69 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
70
71 #ifdef CONFIG_PRINTK
72
73 #define STATE_STRING_PREFACE ": state "
74 #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT)
75
76 /*
77 * Characters to print to indicate error conditions or uncommon filesystem state.
78 * RO is not an error.
79 */
80 static const char fs_state_chars[] = {
81 [BTRFS_FS_STATE_ERROR] = 'E',
82 [BTRFS_FS_STATE_REMOUNTING] = 'M',
83 [BTRFS_FS_STATE_RO] = 0,
84 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A',
85 [BTRFS_FS_STATE_DEV_REPLACING] = 'R',
86 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0,
87 [BTRFS_FS_STATE_NO_CSUMS] = 'C',
88 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L',
89 };
90
btrfs_state_to_string(const struct btrfs_fs_info * info,char * buf)91 static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf)
92 {
93 unsigned int bit;
94 bool states_printed = false;
95 unsigned long fs_state = READ_ONCE(info->fs_state);
96 char *curr = buf;
97
98 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE));
99 curr += sizeof(STATE_STRING_PREFACE) - 1;
100
101 for_each_set_bit(bit, &fs_state, sizeof(fs_state)) {
102 WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT);
103 if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) {
104 *curr++ = fs_state_chars[bit];
105 states_printed = true;
106 }
107 }
108
109 /* If no states were printed, reset the buffer */
110 if (!states_printed)
111 curr = buf;
112
113 *curr++ = 0;
114 }
115 #endif
116
117 /*
118 * Generally the error codes correspond to their respective errors, but there
119 * are a few special cases.
120 *
121 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for
122 * instance will return EUCLEAN if any of the blocks are corrupted in
123 * a way that is problematic. We want to reserve EUCLEAN for these
124 * sort of corruptions.
125 *
126 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we
127 * need to use EROFS for this case. We will have no idea of the
128 * original failure, that will have been reported at the time we tripped
129 * over the error. Each subsequent error that doesn't have any context
130 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR.
131 */
btrfs_decode_error(int errno)132 const char * __attribute_const__ btrfs_decode_error(int errno)
133 {
134 char *errstr = "unknown";
135
136 switch (errno) {
137 case -ENOENT: /* -2 */
138 errstr = "No such entry";
139 break;
140 case -EIO: /* -5 */
141 errstr = "IO failure";
142 break;
143 case -ENOMEM: /* -12*/
144 errstr = "Out of memory";
145 break;
146 case -EEXIST: /* -17 */
147 errstr = "Object already exists";
148 break;
149 case -ENOSPC: /* -28 */
150 errstr = "No space left";
151 break;
152 case -EROFS: /* -30 */
153 errstr = "Readonly filesystem";
154 break;
155 case -EOPNOTSUPP: /* -95 */
156 errstr = "Operation not supported";
157 break;
158 case -EUCLEAN: /* -117 */
159 errstr = "Filesystem corrupted";
160 break;
161 case -EDQUOT: /* -122 */
162 errstr = "Quota exceeded";
163 break;
164 }
165
166 return errstr;
167 }
168
169 /*
170 * __btrfs_handle_fs_error decodes expected errors from the caller and
171 * invokes the appropriate error response.
172 */
173 __cold
__btrfs_handle_fs_error(struct btrfs_fs_info * fs_info,const char * function,unsigned int line,int errno,const char * fmt,...)174 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
175 unsigned int line, int errno, const char *fmt, ...)
176 {
177 struct super_block *sb = fs_info->sb;
178 #ifdef CONFIG_PRINTK
179 char statestr[STATE_STRING_BUF_LEN];
180 const char *errstr;
181 #endif
182
183 /*
184 * Special case: if the error is EROFS, and we're already
185 * under SB_RDONLY, then it is safe here.
186 */
187 if (errno == -EROFS && sb_rdonly(sb))
188 return;
189
190 #ifdef CONFIG_PRINTK
191 errstr = btrfs_decode_error(errno);
192 btrfs_state_to_string(fs_info, statestr);
193 if (fmt) {
194 struct va_format vaf;
195 va_list args;
196
197 va_start(args, fmt);
198 vaf.fmt = fmt;
199 vaf.va = &args;
200
201 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n",
202 sb->s_id, statestr, function, line, errno, errstr, &vaf);
203 va_end(args);
204 } else {
205 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n",
206 sb->s_id, statestr, function, line, errno, errstr);
207 }
208 #endif
209
210 /*
211 * Today we only save the error info to memory. Long term we'll
212 * also send it down to the disk
213 */
214 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
215
216 /* Don't go through full error handling during mount */
217 if (!(sb->s_flags & SB_BORN))
218 return;
219
220 if (sb_rdonly(sb))
221 return;
222
223 btrfs_discard_stop(fs_info);
224
225 /* btrfs handle error by forcing the filesystem readonly */
226 btrfs_set_sb_rdonly(sb);
227 btrfs_info(fs_info, "forced readonly");
228 /*
229 * Note that a running device replace operation is not canceled here
230 * although there is no way to update the progress. It would add the
231 * risk of a deadlock, therefore the canceling is omitted. The only
232 * penalty is that some I/O remains active until the procedure
233 * completes. The next time when the filesystem is mounted writable
234 * again, the device replace operation continues.
235 */
236 }
237
238 #ifdef CONFIG_PRINTK
239 static const char * const logtypes[] = {
240 "emergency",
241 "alert",
242 "critical",
243 "error",
244 "warning",
245 "notice",
246 "info",
247 "debug",
248 };
249
250
251 /*
252 * Use one ratelimit state per log level so that a flood of less important
253 * messages doesn't cause more important ones to be dropped.
254 */
255 static struct ratelimit_state printk_limits[] = {
256 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
257 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
258 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
259 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
260 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
261 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
262 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
263 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
264 };
265
_btrfs_printk(const struct btrfs_fs_info * fs_info,const char * fmt,...)266 void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
267 {
268 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
269 struct va_format vaf;
270 va_list args;
271 int kern_level;
272 const char *type = logtypes[4];
273 struct ratelimit_state *ratelimit = &printk_limits[4];
274
275 va_start(args, fmt);
276
277 while ((kern_level = printk_get_level(fmt)) != 0) {
278 size_t size = printk_skip_level(fmt) - fmt;
279
280 if (kern_level >= '0' && kern_level <= '7') {
281 memcpy(lvl, fmt, size);
282 lvl[size] = '\0';
283 type = logtypes[kern_level - '0'];
284 ratelimit = &printk_limits[kern_level - '0'];
285 }
286 fmt += size;
287 }
288
289 vaf.fmt = fmt;
290 vaf.va = &args;
291
292 if (__ratelimit(ratelimit)) {
293 if (fs_info) {
294 char statestr[STATE_STRING_BUF_LEN];
295
296 btrfs_state_to_string(fs_info, statestr);
297 _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
298 fs_info->sb->s_id, statestr, &vaf);
299 } else {
300 _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
301 }
302 }
303
304 va_end(args);
305 }
306 #endif
307
308 #if BITS_PER_LONG == 32
btrfs_warn_32bit_limit(struct btrfs_fs_info * fs_info)309 void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info)
310 {
311 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) {
312 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses");
313 btrfs_warn(fs_info,
314 "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT",
315 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
316 btrfs_warn(fs_info,
317 "please consider upgrading to 64bit kernel/hardware");
318 }
319 }
320
btrfs_err_32bit_limit(struct btrfs_fs_info * fs_info)321 void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info)
322 {
323 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) {
324 btrfs_err(fs_info, "reached 32bit limit for logical addresses");
325 btrfs_err(fs_info,
326 "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed",
327 BTRFS_32BIT_MAX_FILE_SIZE >> 40);
328 btrfs_err(fs_info,
329 "please consider upgrading to 64bit kernel/hardware");
330 }
331 }
332 #endif
333
334 /*
335 * We only mark the transaction aborted and then set the file system read-only.
336 * This will prevent new transactions from starting or trying to join this
337 * one.
338 *
339 * This means that error recovery at the call site is limited to freeing
340 * any local memory allocations and passing the error code up without
341 * further cleanup. The transaction should complete as it normally would
342 * in the call path but will return -EIO.
343 *
344 * We'll complete the cleanup in btrfs_end_transaction and
345 * btrfs_commit_transaction.
346 */
347 __cold
__btrfs_abort_transaction(struct btrfs_trans_handle * trans,const char * function,unsigned int line,int errno,bool first_hit)348 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
349 const char *function,
350 unsigned int line, int errno, bool first_hit)
351 {
352 struct btrfs_fs_info *fs_info = trans->fs_info;
353
354 WRITE_ONCE(trans->aborted, errno);
355 WRITE_ONCE(trans->transaction->aborted, errno);
356 if (first_hit && errno == -ENOSPC)
357 btrfs_dump_space_info_for_trans_abort(fs_info);
358 /* Wake up anybody who may be waiting on this transaction */
359 wake_up(&fs_info->transaction_wait);
360 wake_up(&fs_info->transaction_blocked_wait);
361 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
362 }
363 /*
364 * __btrfs_panic decodes unexpected, fatal errors from the caller,
365 * issues an alert, and either panics or BUGs, depending on mount options.
366 */
367 __cold
__btrfs_panic(struct btrfs_fs_info * fs_info,const char * function,unsigned int line,int errno,const char * fmt,...)368 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
369 unsigned int line, int errno, const char *fmt, ...)
370 {
371 char *s_id = "<unknown>";
372 const char *errstr;
373 struct va_format vaf = { .fmt = fmt };
374 va_list args;
375
376 if (fs_info)
377 s_id = fs_info->sb->s_id;
378
379 va_start(args, fmt);
380 vaf.va = &args;
381
382 errstr = btrfs_decode_error(errno);
383 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
384 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
385 s_id, function, line, &vaf, errno, errstr);
386
387 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
388 function, line, &vaf, errno, errstr);
389 va_end(args);
390 /* Caller calls BUG() */
391 }
392
btrfs_put_super(struct super_block * sb)393 static void btrfs_put_super(struct super_block *sb)
394 {
395 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
396
397 btrfs_info(fs_info, "last unmount of filesystem %pU", fs_info->fs_devices->fsid);
398 close_ctree(fs_info);
399 }
400
401 enum {
402 Opt_acl, Opt_noacl,
403 Opt_clear_cache,
404 Opt_commit_interval,
405 Opt_compress,
406 Opt_compress_force,
407 Opt_compress_force_type,
408 Opt_compress_type,
409 Opt_degraded,
410 Opt_device,
411 Opt_fatal_errors,
412 Opt_flushoncommit, Opt_noflushoncommit,
413 Opt_max_inline,
414 Opt_barrier, Opt_nobarrier,
415 Opt_datacow, Opt_nodatacow,
416 Opt_datasum, Opt_nodatasum,
417 Opt_defrag, Opt_nodefrag,
418 Opt_discard, Opt_nodiscard,
419 Opt_discard_mode,
420 Opt_norecovery,
421 Opt_ratio,
422 Opt_rescan_uuid_tree,
423 Opt_skip_balance,
424 Opt_space_cache, Opt_no_space_cache,
425 Opt_space_cache_version,
426 Opt_ssd, Opt_nossd,
427 Opt_ssd_spread, Opt_nossd_spread,
428 Opt_subvol,
429 Opt_subvol_empty,
430 Opt_subvolid,
431 Opt_thread_pool,
432 Opt_treelog, Opt_notreelog,
433 Opt_user_subvol_rm_allowed,
434
435 /* Rescue options */
436 Opt_rescue,
437 Opt_usebackuproot,
438 Opt_nologreplay,
439 Opt_ignorebadroots,
440 Opt_ignoredatacsums,
441 Opt_rescue_all,
442
443 /* Deprecated options */
444 Opt_recovery,
445 Opt_inode_cache, Opt_noinode_cache,
446
447 /* Debugging options */
448 Opt_check_integrity,
449 Opt_check_integrity_including_extent_data,
450 Opt_check_integrity_print_mask,
451 Opt_enospc_debug, Opt_noenospc_debug,
452 #ifdef CONFIG_BTRFS_DEBUG
453 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
454 #endif
455 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
456 Opt_ref_verify,
457 #endif
458 Opt_err,
459 };
460
461 static const match_table_t tokens = {
462 {Opt_acl, "acl"},
463 {Opt_noacl, "noacl"},
464 {Opt_clear_cache, "clear_cache"},
465 {Opt_commit_interval, "commit=%u"},
466 {Opt_compress, "compress"},
467 {Opt_compress_type, "compress=%s"},
468 {Opt_compress_force, "compress-force"},
469 {Opt_compress_force_type, "compress-force=%s"},
470 {Opt_degraded, "degraded"},
471 {Opt_device, "device=%s"},
472 {Opt_fatal_errors, "fatal_errors=%s"},
473 {Opt_flushoncommit, "flushoncommit"},
474 {Opt_noflushoncommit, "noflushoncommit"},
475 {Opt_inode_cache, "inode_cache"},
476 {Opt_noinode_cache, "noinode_cache"},
477 {Opt_max_inline, "max_inline=%s"},
478 {Opt_barrier, "barrier"},
479 {Opt_nobarrier, "nobarrier"},
480 {Opt_datacow, "datacow"},
481 {Opt_nodatacow, "nodatacow"},
482 {Opt_datasum, "datasum"},
483 {Opt_nodatasum, "nodatasum"},
484 {Opt_defrag, "autodefrag"},
485 {Opt_nodefrag, "noautodefrag"},
486 {Opt_discard, "discard"},
487 {Opt_discard_mode, "discard=%s"},
488 {Opt_nodiscard, "nodiscard"},
489 {Opt_norecovery, "norecovery"},
490 {Opt_ratio, "metadata_ratio=%u"},
491 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
492 {Opt_skip_balance, "skip_balance"},
493 {Opt_space_cache, "space_cache"},
494 {Opt_no_space_cache, "nospace_cache"},
495 {Opt_space_cache_version, "space_cache=%s"},
496 {Opt_ssd, "ssd"},
497 {Opt_nossd, "nossd"},
498 {Opt_ssd_spread, "ssd_spread"},
499 {Opt_nossd_spread, "nossd_spread"},
500 {Opt_subvol, "subvol=%s"},
501 {Opt_subvol_empty, "subvol="},
502 {Opt_subvolid, "subvolid=%s"},
503 {Opt_thread_pool, "thread_pool=%u"},
504 {Opt_treelog, "treelog"},
505 {Opt_notreelog, "notreelog"},
506 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
507
508 /* Rescue options */
509 {Opt_rescue, "rescue=%s"},
510 /* Deprecated, with alias rescue=nologreplay */
511 {Opt_nologreplay, "nologreplay"},
512 /* Deprecated, with alias rescue=usebackuproot */
513 {Opt_usebackuproot, "usebackuproot"},
514
515 /* Deprecated options */
516 {Opt_recovery, "recovery"},
517
518 /* Debugging options */
519 {Opt_check_integrity, "check_int"},
520 {Opt_check_integrity_including_extent_data, "check_int_data"},
521 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
522 {Opt_enospc_debug, "enospc_debug"},
523 {Opt_noenospc_debug, "noenospc_debug"},
524 #ifdef CONFIG_BTRFS_DEBUG
525 {Opt_fragment_data, "fragment=data"},
526 {Opt_fragment_metadata, "fragment=metadata"},
527 {Opt_fragment_all, "fragment=all"},
528 #endif
529 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
530 {Opt_ref_verify, "ref_verify"},
531 #endif
532 {Opt_err, NULL},
533 };
534
535 static const match_table_t rescue_tokens = {
536 {Opt_usebackuproot, "usebackuproot"},
537 {Opt_nologreplay, "nologreplay"},
538 {Opt_ignorebadroots, "ignorebadroots"},
539 {Opt_ignorebadroots, "ibadroots"},
540 {Opt_ignoredatacsums, "ignoredatacsums"},
541 {Opt_ignoredatacsums, "idatacsums"},
542 {Opt_rescue_all, "all"},
543 {Opt_err, NULL},
544 };
545
check_ro_option(struct btrfs_fs_info * fs_info,unsigned long opt,const char * opt_name)546 static bool check_ro_option(struct btrfs_fs_info *fs_info, unsigned long opt,
547 const char *opt_name)
548 {
549 if (fs_info->mount_opt & opt) {
550 btrfs_err(fs_info, "%s must be used with ro mount option",
551 opt_name);
552 return true;
553 }
554 return false;
555 }
556
parse_rescue_options(struct btrfs_fs_info * info,const char * options)557 static int parse_rescue_options(struct btrfs_fs_info *info, const char *options)
558 {
559 char *opts;
560 char *orig;
561 char *p;
562 substring_t args[MAX_OPT_ARGS];
563 int ret = 0;
564
565 opts = kstrdup(options, GFP_KERNEL);
566 if (!opts)
567 return -ENOMEM;
568 orig = opts;
569
570 while ((p = strsep(&opts, ":")) != NULL) {
571 int token;
572
573 if (!*p)
574 continue;
575 token = match_token(p, rescue_tokens, args);
576 switch (token){
577 case Opt_usebackuproot:
578 btrfs_info(info,
579 "trying to use backup root at mount time");
580 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
581 break;
582 case Opt_nologreplay:
583 btrfs_set_and_info(info, NOLOGREPLAY,
584 "disabling log replay at mount time");
585 break;
586 case Opt_ignorebadroots:
587 btrfs_set_and_info(info, IGNOREBADROOTS,
588 "ignoring bad roots");
589 break;
590 case Opt_ignoredatacsums:
591 btrfs_set_and_info(info, IGNOREDATACSUMS,
592 "ignoring data csums");
593 break;
594 case Opt_rescue_all:
595 btrfs_info(info, "enabling all of the rescue options");
596 btrfs_set_and_info(info, IGNOREDATACSUMS,
597 "ignoring data csums");
598 btrfs_set_and_info(info, IGNOREBADROOTS,
599 "ignoring bad roots");
600 btrfs_set_and_info(info, NOLOGREPLAY,
601 "disabling log replay at mount time");
602 break;
603 case Opt_err:
604 btrfs_info(info, "unrecognized rescue option '%s'", p);
605 ret = -EINVAL;
606 goto out;
607 default:
608 break;
609 }
610
611 }
612 out:
613 kfree(orig);
614 return ret;
615 }
616
617 /*
618 * Regular mount options parser. Everything that is needed only when
619 * reading in a new superblock is parsed here.
620 * XXX JDM: This needs to be cleaned up for remount.
621 */
btrfs_parse_options(struct btrfs_fs_info * info,char * options,unsigned long new_flags)622 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
623 unsigned long new_flags)
624 {
625 substring_t args[MAX_OPT_ARGS];
626 char *p, *num;
627 int intarg;
628 int ret = 0;
629 char *compress_type;
630 bool compress_force = false;
631 enum btrfs_compression_type saved_compress_type;
632 int saved_compress_level;
633 bool saved_compress_force;
634 int no_compress = 0;
635 const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state);
636
637 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
638 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
639 else if (btrfs_free_space_cache_v1_active(info)) {
640 if (btrfs_is_zoned(info)) {
641 btrfs_info(info,
642 "zoned: clearing existing space cache");
643 btrfs_set_super_cache_generation(info->super_copy, 0);
644 } else {
645 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
646 }
647 }
648
649 /*
650 * Even the options are empty, we still need to do extra check
651 * against new flags
652 */
653 if (!options)
654 goto check;
655
656 while ((p = strsep(&options, ",")) != NULL) {
657 int token;
658 if (!*p)
659 continue;
660
661 token = match_token(p, tokens, args);
662 switch (token) {
663 case Opt_degraded:
664 btrfs_info(info, "allowing degraded mounts");
665 btrfs_set_opt(info->mount_opt, DEGRADED);
666 break;
667 case Opt_subvol:
668 case Opt_subvol_empty:
669 case Opt_subvolid:
670 case Opt_device:
671 /*
672 * These are parsed by btrfs_parse_subvol_options or
673 * btrfs_parse_device_options and can be ignored here.
674 */
675 break;
676 case Opt_nodatasum:
677 btrfs_set_and_info(info, NODATASUM,
678 "setting nodatasum");
679 break;
680 case Opt_datasum:
681 if (btrfs_test_opt(info, NODATASUM)) {
682 if (btrfs_test_opt(info, NODATACOW))
683 btrfs_info(info,
684 "setting datasum, datacow enabled");
685 else
686 btrfs_info(info, "setting datasum");
687 }
688 btrfs_clear_opt(info->mount_opt, NODATACOW);
689 btrfs_clear_opt(info->mount_opt, NODATASUM);
690 break;
691 case Opt_nodatacow:
692 if (!btrfs_test_opt(info, NODATACOW)) {
693 if (!btrfs_test_opt(info, COMPRESS) ||
694 !btrfs_test_opt(info, FORCE_COMPRESS)) {
695 btrfs_info(info,
696 "setting nodatacow, compression disabled");
697 } else {
698 btrfs_info(info, "setting nodatacow");
699 }
700 }
701 btrfs_clear_opt(info->mount_opt, COMPRESS);
702 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
703 btrfs_set_opt(info->mount_opt, NODATACOW);
704 btrfs_set_opt(info->mount_opt, NODATASUM);
705 break;
706 case Opt_datacow:
707 btrfs_clear_and_info(info, NODATACOW,
708 "setting datacow");
709 break;
710 case Opt_compress_force:
711 case Opt_compress_force_type:
712 compress_force = true;
713 fallthrough;
714 case Opt_compress:
715 case Opt_compress_type:
716 saved_compress_type = btrfs_test_opt(info,
717 COMPRESS) ?
718 info->compress_type : BTRFS_COMPRESS_NONE;
719 saved_compress_force =
720 btrfs_test_opt(info, FORCE_COMPRESS);
721 saved_compress_level = info->compress_level;
722 if (token == Opt_compress ||
723 token == Opt_compress_force ||
724 strncmp(args[0].from, "zlib", 4) == 0) {
725 compress_type = "zlib";
726
727 info->compress_type = BTRFS_COMPRESS_ZLIB;
728 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
729 /*
730 * args[0] contains uninitialized data since
731 * for these tokens we don't expect any
732 * parameter.
733 */
734 if (token != Opt_compress &&
735 token != Opt_compress_force)
736 info->compress_level =
737 btrfs_compress_str2level(
738 BTRFS_COMPRESS_ZLIB,
739 args[0].from + 4);
740 btrfs_set_opt(info->mount_opt, COMPRESS);
741 btrfs_clear_opt(info->mount_opt, NODATACOW);
742 btrfs_clear_opt(info->mount_opt, NODATASUM);
743 no_compress = 0;
744 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
745 compress_type = "lzo";
746 info->compress_type = BTRFS_COMPRESS_LZO;
747 info->compress_level = 0;
748 btrfs_set_opt(info->mount_opt, COMPRESS);
749 btrfs_clear_opt(info->mount_opt, NODATACOW);
750 btrfs_clear_opt(info->mount_opt, NODATASUM);
751 btrfs_set_fs_incompat(info, COMPRESS_LZO);
752 no_compress = 0;
753 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
754 compress_type = "zstd";
755 info->compress_type = BTRFS_COMPRESS_ZSTD;
756 info->compress_level =
757 btrfs_compress_str2level(
758 BTRFS_COMPRESS_ZSTD,
759 args[0].from + 4);
760 btrfs_set_opt(info->mount_opt, COMPRESS);
761 btrfs_clear_opt(info->mount_opt, NODATACOW);
762 btrfs_clear_opt(info->mount_opt, NODATASUM);
763 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
764 no_compress = 0;
765 } else if (strncmp(args[0].from, "no", 2) == 0) {
766 compress_type = "no";
767 info->compress_level = 0;
768 info->compress_type = 0;
769 btrfs_clear_opt(info->mount_opt, COMPRESS);
770 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
771 compress_force = false;
772 no_compress++;
773 } else {
774 btrfs_err(info, "unrecognized compression value %s",
775 args[0].from);
776 ret = -EINVAL;
777 goto out;
778 }
779
780 if (compress_force) {
781 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
782 } else {
783 /*
784 * If we remount from compress-force=xxx to
785 * compress=xxx, we need clear FORCE_COMPRESS
786 * flag, otherwise, there is no way for users
787 * to disable forcible compression separately.
788 */
789 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
790 }
791 if (no_compress == 1) {
792 btrfs_info(info, "use no compression");
793 } else if ((info->compress_type != saved_compress_type) ||
794 (compress_force != saved_compress_force) ||
795 (info->compress_level != saved_compress_level)) {
796 btrfs_info(info, "%s %s compression, level %d",
797 (compress_force) ? "force" : "use",
798 compress_type, info->compress_level);
799 }
800 compress_force = false;
801 break;
802 case Opt_ssd:
803 btrfs_set_and_info(info, SSD,
804 "enabling ssd optimizations");
805 btrfs_clear_opt(info->mount_opt, NOSSD);
806 break;
807 case Opt_ssd_spread:
808 btrfs_set_and_info(info, SSD,
809 "enabling ssd optimizations");
810 btrfs_set_and_info(info, SSD_SPREAD,
811 "using spread ssd allocation scheme");
812 btrfs_clear_opt(info->mount_opt, NOSSD);
813 break;
814 case Opt_nossd:
815 btrfs_set_opt(info->mount_opt, NOSSD);
816 btrfs_clear_and_info(info, SSD,
817 "not using ssd optimizations");
818 fallthrough;
819 case Opt_nossd_spread:
820 btrfs_clear_and_info(info, SSD_SPREAD,
821 "not using spread ssd allocation scheme");
822 break;
823 case Opt_barrier:
824 btrfs_clear_and_info(info, NOBARRIER,
825 "turning on barriers");
826 break;
827 case Opt_nobarrier:
828 btrfs_set_and_info(info, NOBARRIER,
829 "turning off barriers");
830 break;
831 case Opt_thread_pool:
832 ret = match_int(&args[0], &intarg);
833 if (ret) {
834 btrfs_err(info, "unrecognized thread_pool value %s",
835 args[0].from);
836 goto out;
837 } else if (intarg == 0) {
838 btrfs_err(info, "invalid value 0 for thread_pool");
839 ret = -EINVAL;
840 goto out;
841 }
842 info->thread_pool_size = intarg;
843 break;
844 case Opt_max_inline:
845 num = match_strdup(&args[0]);
846 if (num) {
847 info->max_inline = memparse(num, NULL);
848 kfree(num);
849
850 if (info->max_inline) {
851 info->max_inline = min_t(u64,
852 info->max_inline,
853 info->sectorsize);
854 }
855 btrfs_info(info, "max_inline at %llu",
856 info->max_inline);
857 } else {
858 ret = -ENOMEM;
859 goto out;
860 }
861 break;
862 case Opt_acl:
863 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
864 info->sb->s_flags |= SB_POSIXACL;
865 break;
866 #else
867 btrfs_err(info, "support for ACL not compiled in!");
868 ret = -EINVAL;
869 goto out;
870 #endif
871 case Opt_noacl:
872 info->sb->s_flags &= ~SB_POSIXACL;
873 break;
874 case Opt_notreelog:
875 btrfs_set_and_info(info, NOTREELOG,
876 "disabling tree log");
877 break;
878 case Opt_treelog:
879 btrfs_clear_and_info(info, NOTREELOG,
880 "enabling tree log");
881 break;
882 case Opt_norecovery:
883 case Opt_nologreplay:
884 btrfs_warn(info,
885 "'nologreplay' is deprecated, use 'rescue=nologreplay' instead");
886 btrfs_set_and_info(info, NOLOGREPLAY,
887 "disabling log replay at mount time");
888 break;
889 case Opt_flushoncommit:
890 btrfs_set_and_info(info, FLUSHONCOMMIT,
891 "turning on flush-on-commit");
892 break;
893 case Opt_noflushoncommit:
894 btrfs_clear_and_info(info, FLUSHONCOMMIT,
895 "turning off flush-on-commit");
896 break;
897 case Opt_ratio:
898 ret = match_int(&args[0], &intarg);
899 if (ret) {
900 btrfs_err(info, "unrecognized metadata_ratio value %s",
901 args[0].from);
902 goto out;
903 }
904 info->metadata_ratio = intarg;
905 btrfs_info(info, "metadata ratio %u",
906 info->metadata_ratio);
907 break;
908 case Opt_discard:
909 case Opt_discard_mode:
910 if (token == Opt_discard ||
911 strcmp(args[0].from, "sync") == 0) {
912 btrfs_clear_opt(info->mount_opt, DISCARD_ASYNC);
913 btrfs_set_and_info(info, DISCARD_SYNC,
914 "turning on sync discard");
915 } else if (strcmp(args[0].from, "async") == 0) {
916 btrfs_clear_opt(info->mount_opt, DISCARD_SYNC);
917 btrfs_set_and_info(info, DISCARD_ASYNC,
918 "turning on async discard");
919 } else {
920 btrfs_err(info, "unrecognized discard mode value %s",
921 args[0].from);
922 ret = -EINVAL;
923 goto out;
924 }
925 break;
926 case Opt_nodiscard:
927 btrfs_clear_and_info(info, DISCARD_SYNC,
928 "turning off discard");
929 btrfs_clear_and_info(info, DISCARD_ASYNC,
930 "turning off async discard");
931 break;
932 case Opt_space_cache:
933 case Opt_space_cache_version:
934 /*
935 * We already set FREE_SPACE_TREE above because we have
936 * compat_ro(FREE_SPACE_TREE) set, and we aren't going
937 * to allow v1 to be set for extent tree v2, simply
938 * ignore this setting if we're extent tree v2.
939 */
940 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
941 break;
942 if (token == Opt_space_cache ||
943 strcmp(args[0].from, "v1") == 0) {
944 btrfs_clear_opt(info->mount_opt,
945 FREE_SPACE_TREE);
946 btrfs_set_and_info(info, SPACE_CACHE,
947 "enabling disk space caching");
948 } else if (strcmp(args[0].from, "v2") == 0) {
949 btrfs_clear_opt(info->mount_opt,
950 SPACE_CACHE);
951 btrfs_set_and_info(info, FREE_SPACE_TREE,
952 "enabling free space tree");
953 } else {
954 btrfs_err(info, "unrecognized space_cache value %s",
955 args[0].from);
956 ret = -EINVAL;
957 goto out;
958 }
959 break;
960 case Opt_rescan_uuid_tree:
961 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
962 break;
963 case Opt_no_space_cache:
964 /*
965 * We cannot operate without the free space tree with
966 * extent tree v2, ignore this option.
967 */
968 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
969 break;
970 if (btrfs_test_opt(info, SPACE_CACHE)) {
971 btrfs_clear_and_info(info, SPACE_CACHE,
972 "disabling disk space caching");
973 }
974 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
975 btrfs_clear_and_info(info, FREE_SPACE_TREE,
976 "disabling free space tree");
977 }
978 break;
979 case Opt_inode_cache:
980 case Opt_noinode_cache:
981 btrfs_warn(info,
982 "the 'inode_cache' option is deprecated and has no effect since 5.11");
983 break;
984 case Opt_clear_cache:
985 /*
986 * We cannot clear the free space tree with extent tree
987 * v2, ignore this option.
988 */
989 if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
990 break;
991 btrfs_set_and_info(info, CLEAR_CACHE,
992 "force clearing of disk cache");
993 break;
994 case Opt_user_subvol_rm_allowed:
995 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
996 break;
997 case Opt_enospc_debug:
998 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
999 break;
1000 case Opt_noenospc_debug:
1001 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
1002 break;
1003 case Opt_defrag:
1004 btrfs_set_and_info(info, AUTO_DEFRAG,
1005 "enabling auto defrag");
1006 break;
1007 case Opt_nodefrag:
1008 btrfs_clear_and_info(info, AUTO_DEFRAG,
1009 "disabling auto defrag");
1010 break;
1011 case Opt_recovery:
1012 case Opt_usebackuproot:
1013 btrfs_warn(info,
1014 "'%s' is deprecated, use 'rescue=usebackuproot' instead",
1015 token == Opt_recovery ? "recovery" :
1016 "usebackuproot");
1017 btrfs_info(info,
1018 "trying to use backup root at mount time");
1019 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
1020 break;
1021 case Opt_skip_balance:
1022 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
1023 break;
1024 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1025 case Opt_check_integrity_including_extent_data:
1026 btrfs_info(info,
1027 "enabling check integrity including extent data");
1028 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY_DATA);
1029 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1030 break;
1031 case Opt_check_integrity:
1032 btrfs_info(info, "enabling check integrity");
1033 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
1034 break;
1035 case Opt_check_integrity_print_mask:
1036 ret = match_int(&args[0], &intarg);
1037 if (ret) {
1038 btrfs_err(info,
1039 "unrecognized check_integrity_print_mask value %s",
1040 args[0].from);
1041 goto out;
1042 }
1043 info->check_integrity_print_mask = intarg;
1044 btrfs_info(info, "check_integrity_print_mask 0x%x",
1045 info->check_integrity_print_mask);
1046 break;
1047 #else
1048 case Opt_check_integrity_including_extent_data:
1049 case Opt_check_integrity:
1050 case Opt_check_integrity_print_mask:
1051 btrfs_err(info,
1052 "support for check_integrity* not compiled in!");
1053 ret = -EINVAL;
1054 goto out;
1055 #endif
1056 case Opt_fatal_errors:
1057 if (strcmp(args[0].from, "panic") == 0) {
1058 btrfs_set_opt(info->mount_opt,
1059 PANIC_ON_FATAL_ERROR);
1060 } else if (strcmp(args[0].from, "bug") == 0) {
1061 btrfs_clear_opt(info->mount_opt,
1062 PANIC_ON_FATAL_ERROR);
1063 } else {
1064 btrfs_err(info, "unrecognized fatal_errors value %s",
1065 args[0].from);
1066 ret = -EINVAL;
1067 goto out;
1068 }
1069 break;
1070 case Opt_commit_interval:
1071 intarg = 0;
1072 ret = match_int(&args[0], &intarg);
1073 if (ret) {
1074 btrfs_err(info, "unrecognized commit_interval value %s",
1075 args[0].from);
1076 ret = -EINVAL;
1077 goto out;
1078 }
1079 if (intarg == 0) {
1080 btrfs_info(info,
1081 "using default commit interval %us",
1082 BTRFS_DEFAULT_COMMIT_INTERVAL);
1083 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
1084 } else if (intarg > 300) {
1085 btrfs_warn(info, "excessive commit interval %d",
1086 intarg);
1087 }
1088 info->commit_interval = intarg;
1089 break;
1090 case Opt_rescue:
1091 ret = parse_rescue_options(info, args[0].from);
1092 if (ret < 0) {
1093 btrfs_err(info, "unrecognized rescue value %s",
1094 args[0].from);
1095 goto out;
1096 }
1097 break;
1098 #ifdef CONFIG_BTRFS_DEBUG
1099 case Opt_fragment_all:
1100 btrfs_info(info, "fragmenting all space");
1101 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1102 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
1103 break;
1104 case Opt_fragment_metadata:
1105 btrfs_info(info, "fragmenting metadata");
1106 btrfs_set_opt(info->mount_opt,
1107 FRAGMENT_METADATA);
1108 break;
1109 case Opt_fragment_data:
1110 btrfs_info(info, "fragmenting data");
1111 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
1112 break;
1113 #endif
1114 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
1115 case Opt_ref_verify:
1116 btrfs_info(info, "doing ref verification");
1117 btrfs_set_opt(info->mount_opt, REF_VERIFY);
1118 break;
1119 #endif
1120 case Opt_err:
1121 btrfs_err(info, "unrecognized mount option '%s'", p);
1122 ret = -EINVAL;
1123 goto out;
1124 default:
1125 break;
1126 }
1127 }
1128 check:
1129 /* We're read-only, don't have to check. */
1130 if (new_flags & SB_RDONLY)
1131 goto out;
1132
1133 if (check_ro_option(info, BTRFS_MOUNT_NOLOGREPLAY, "nologreplay") ||
1134 check_ro_option(info, BTRFS_MOUNT_IGNOREBADROOTS, "ignorebadroots") ||
1135 check_ro_option(info, BTRFS_MOUNT_IGNOREDATACSUMS, "ignoredatacsums"))
1136 ret = -EINVAL;
1137 out:
1138 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
1139 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
1140 !btrfs_test_opt(info, CLEAR_CACHE)) {
1141 btrfs_err(info, "cannot disable free space tree");
1142 ret = -EINVAL;
1143 }
1144 if (btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE) &&
1145 !btrfs_test_opt(info, FREE_SPACE_TREE)) {
1146 btrfs_err(info, "cannot disable free space tree with block-group-tree feature");
1147 ret = -EINVAL;
1148 }
1149 if (!ret)
1150 ret = btrfs_check_mountopts_zoned(info);
1151 if (!ret && !remounting) {
1152 if (btrfs_test_opt(info, SPACE_CACHE))
1153 btrfs_info(info, "disk space caching is enabled");
1154 if (btrfs_test_opt(info, FREE_SPACE_TREE))
1155 btrfs_info(info, "using free space tree");
1156 }
1157 return ret;
1158 }
1159
1160 /*
1161 * Parse mount options that are required early in the mount process.
1162 *
1163 * All other options will be parsed on much later in the mount process and
1164 * only when we need to allocate a new super block.
1165 */
btrfs_parse_device_options(const char * options,fmode_t flags,void * holder)1166 static int btrfs_parse_device_options(const char *options, fmode_t flags,
1167 void *holder)
1168 {
1169 substring_t args[MAX_OPT_ARGS];
1170 char *device_name, *opts, *orig, *p;
1171 struct btrfs_device *device = NULL;
1172 int error = 0;
1173
1174 lockdep_assert_held(&uuid_mutex);
1175
1176 if (!options)
1177 return 0;
1178
1179 /*
1180 * strsep changes the string, duplicate it because btrfs_parse_options
1181 * gets called later
1182 */
1183 opts = kstrdup(options, GFP_KERNEL);
1184 if (!opts)
1185 return -ENOMEM;
1186 orig = opts;
1187
1188 while ((p = strsep(&opts, ",")) != NULL) {
1189 int token;
1190
1191 if (!*p)
1192 continue;
1193
1194 token = match_token(p, tokens, args);
1195 if (token == Opt_device) {
1196 device_name = match_strdup(&args[0]);
1197 if (!device_name) {
1198 error = -ENOMEM;
1199 goto out;
1200 }
1201 device = btrfs_scan_one_device(device_name, flags,
1202 holder);
1203 kfree(device_name);
1204 if (IS_ERR(device)) {
1205 error = PTR_ERR(device);
1206 goto out;
1207 }
1208 }
1209 }
1210
1211 out:
1212 kfree(orig);
1213 return error;
1214 }
1215
1216 /*
1217 * Parse mount options that are related to subvolume id
1218 *
1219 * The value is later passed to mount_subvol()
1220 */
btrfs_parse_subvol_options(const char * options,char ** subvol_name,u64 * subvol_objectid)1221 static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
1222 u64 *subvol_objectid)
1223 {
1224 substring_t args[MAX_OPT_ARGS];
1225 char *opts, *orig, *p;
1226 int error = 0;
1227 u64 subvolid;
1228
1229 if (!options)
1230 return 0;
1231
1232 /*
1233 * strsep changes the string, duplicate it because
1234 * btrfs_parse_device_options gets called later
1235 */
1236 opts = kstrdup(options, GFP_KERNEL);
1237 if (!opts)
1238 return -ENOMEM;
1239 orig = opts;
1240
1241 while ((p = strsep(&opts, ",")) != NULL) {
1242 int token;
1243 if (!*p)
1244 continue;
1245
1246 token = match_token(p, tokens, args);
1247 switch (token) {
1248 case Opt_subvol:
1249 kfree(*subvol_name);
1250 *subvol_name = match_strdup(&args[0]);
1251 if (!*subvol_name) {
1252 error = -ENOMEM;
1253 goto out;
1254 }
1255 break;
1256 case Opt_subvolid:
1257 error = match_u64(&args[0], &subvolid);
1258 if (error)
1259 goto out;
1260
1261 /* we want the original fs_tree */
1262 if (subvolid == 0)
1263 subvolid = BTRFS_FS_TREE_OBJECTID;
1264
1265 *subvol_objectid = subvolid;
1266 break;
1267 default:
1268 break;
1269 }
1270 }
1271
1272 out:
1273 kfree(orig);
1274 return error;
1275 }
1276
btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info * fs_info,u64 subvol_objectid)1277 char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1278 u64 subvol_objectid)
1279 {
1280 struct btrfs_root *root = fs_info->tree_root;
1281 struct btrfs_root *fs_root = NULL;
1282 struct btrfs_root_ref *root_ref;
1283 struct btrfs_inode_ref *inode_ref;
1284 struct btrfs_key key;
1285 struct btrfs_path *path = NULL;
1286 char *name = NULL, *ptr;
1287 u64 dirid;
1288 int len;
1289 int ret;
1290
1291 path = btrfs_alloc_path();
1292 if (!path) {
1293 ret = -ENOMEM;
1294 goto err;
1295 }
1296
1297 name = kmalloc(PATH_MAX, GFP_KERNEL);
1298 if (!name) {
1299 ret = -ENOMEM;
1300 goto err;
1301 }
1302 ptr = name + PATH_MAX - 1;
1303 ptr[0] = '\0';
1304
1305 /*
1306 * Walk up the subvolume trees in the tree of tree roots by root
1307 * backrefs until we hit the top-level subvolume.
1308 */
1309 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1310 key.objectid = subvol_objectid;
1311 key.type = BTRFS_ROOT_BACKREF_KEY;
1312 key.offset = (u64)-1;
1313
1314 ret = btrfs_search_backwards(root, &key, path);
1315 if (ret < 0) {
1316 goto err;
1317 } else if (ret > 0) {
1318 ret = -ENOENT;
1319 goto err;
1320 }
1321
1322 subvol_objectid = key.offset;
1323
1324 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1325 struct btrfs_root_ref);
1326 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1327 ptr -= len + 1;
1328 if (ptr < name) {
1329 ret = -ENAMETOOLONG;
1330 goto err;
1331 }
1332 read_extent_buffer(path->nodes[0], ptr + 1,
1333 (unsigned long)(root_ref + 1), len);
1334 ptr[0] = '/';
1335 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1336 btrfs_release_path(path);
1337
1338 fs_root = btrfs_get_fs_root(fs_info, subvol_objectid, true);
1339 if (IS_ERR(fs_root)) {
1340 ret = PTR_ERR(fs_root);
1341 fs_root = NULL;
1342 goto err;
1343 }
1344
1345 /*
1346 * Walk up the filesystem tree by inode refs until we hit the
1347 * root directory.
1348 */
1349 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1350 key.objectid = dirid;
1351 key.type = BTRFS_INODE_REF_KEY;
1352 key.offset = (u64)-1;
1353
1354 ret = btrfs_search_backwards(fs_root, &key, path);
1355 if (ret < 0) {
1356 goto err;
1357 } else if (ret > 0) {
1358 ret = -ENOENT;
1359 goto err;
1360 }
1361
1362 dirid = key.offset;
1363
1364 inode_ref = btrfs_item_ptr(path->nodes[0],
1365 path->slots[0],
1366 struct btrfs_inode_ref);
1367 len = btrfs_inode_ref_name_len(path->nodes[0],
1368 inode_ref);
1369 ptr -= len + 1;
1370 if (ptr < name) {
1371 ret = -ENAMETOOLONG;
1372 goto err;
1373 }
1374 read_extent_buffer(path->nodes[0], ptr + 1,
1375 (unsigned long)(inode_ref + 1), len);
1376 ptr[0] = '/';
1377 btrfs_release_path(path);
1378 }
1379 btrfs_put_root(fs_root);
1380 fs_root = NULL;
1381 }
1382
1383 btrfs_free_path(path);
1384 if (ptr == name + PATH_MAX - 1) {
1385 name[0] = '/';
1386 name[1] = '\0';
1387 } else {
1388 memmove(name, ptr, name + PATH_MAX - ptr);
1389 }
1390 return name;
1391
1392 err:
1393 btrfs_put_root(fs_root);
1394 btrfs_free_path(path);
1395 kfree(name);
1396 return ERR_PTR(ret);
1397 }
1398
get_default_subvol_objectid(struct btrfs_fs_info * fs_info,u64 * objectid)1399 static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1400 {
1401 struct btrfs_root *root = fs_info->tree_root;
1402 struct btrfs_dir_item *di;
1403 struct btrfs_path *path;
1404 struct btrfs_key location;
1405 struct fscrypt_str name = FSTR_INIT("default", 7);
1406 u64 dir_id;
1407
1408 path = btrfs_alloc_path();
1409 if (!path)
1410 return -ENOMEM;
1411
1412 /*
1413 * Find the "default" dir item which points to the root item that we
1414 * will mount by default if we haven't been given a specific subvolume
1415 * to mount.
1416 */
1417 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1418 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
1419 if (IS_ERR(di)) {
1420 btrfs_free_path(path);
1421 return PTR_ERR(di);
1422 }
1423 if (!di) {
1424 /*
1425 * Ok the default dir item isn't there. This is weird since
1426 * it's always been there, but don't freak out, just try and
1427 * mount the top-level subvolume.
1428 */
1429 btrfs_free_path(path);
1430 *objectid = BTRFS_FS_TREE_OBJECTID;
1431 return 0;
1432 }
1433
1434 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1435 btrfs_free_path(path);
1436 *objectid = location.objectid;
1437 return 0;
1438 }
1439
btrfs_fill_super(struct super_block * sb,struct btrfs_fs_devices * fs_devices,void * data)1440 static int btrfs_fill_super(struct super_block *sb,
1441 struct btrfs_fs_devices *fs_devices,
1442 void *data)
1443 {
1444 struct inode *inode;
1445 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1446 int err;
1447
1448 sb->s_maxbytes = MAX_LFS_FILESIZE;
1449 sb->s_magic = BTRFS_SUPER_MAGIC;
1450 sb->s_op = &btrfs_super_ops;
1451 sb->s_d_op = &btrfs_dentry_operations;
1452 sb->s_export_op = &btrfs_export_ops;
1453 #ifdef CONFIG_FS_VERITY
1454 sb->s_vop = &btrfs_verityops;
1455 #endif
1456 sb->s_xattr = btrfs_xattr_handlers;
1457 sb->s_time_gran = 1;
1458 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
1459 sb->s_flags |= SB_POSIXACL;
1460 #endif
1461 sb->s_flags |= SB_I_VERSION;
1462 sb->s_iflags |= SB_I_CGROUPWB;
1463
1464 err = super_setup_bdi(sb);
1465 if (err) {
1466 btrfs_err(fs_info, "super_setup_bdi failed");
1467 return err;
1468 }
1469
1470 err = open_ctree(sb, fs_devices, (char *)data);
1471 if (err) {
1472 btrfs_err(fs_info, "open_ctree failed");
1473 return err;
1474 }
1475
1476 inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
1477 if (IS_ERR(inode)) {
1478 err = PTR_ERR(inode);
1479 goto fail_close;
1480 }
1481
1482 sb->s_root = d_make_root(inode);
1483 if (!sb->s_root) {
1484 err = -ENOMEM;
1485 goto fail_close;
1486 }
1487
1488 cleancache_init_fs(sb);
1489 sb->s_flags |= SB_ACTIVE;
1490 return 0;
1491
1492 fail_close:
1493 close_ctree(fs_info);
1494 return err;
1495 }
1496
btrfs_sync_fs(struct super_block * sb,int wait)1497 int btrfs_sync_fs(struct super_block *sb, int wait)
1498 {
1499 struct btrfs_trans_handle *trans;
1500 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1501 struct btrfs_root *root = fs_info->tree_root;
1502
1503 trace_btrfs_sync_fs(fs_info, wait);
1504
1505 if (!wait) {
1506 filemap_flush(fs_info->btree_inode->i_mapping);
1507 return 0;
1508 }
1509
1510 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
1511
1512 trans = btrfs_attach_transaction_barrier(root);
1513 if (IS_ERR(trans)) {
1514 /* no transaction, don't bother */
1515 if (PTR_ERR(trans) == -ENOENT) {
1516 /*
1517 * Exit unless we have some pending changes
1518 * that need to go through commit
1519 */
1520 if (fs_info->pending_changes == 0)
1521 return 0;
1522 /*
1523 * A non-blocking test if the fs is frozen. We must not
1524 * start a new transaction here otherwise a deadlock
1525 * happens. The pending operations are delayed to the
1526 * next commit after thawing.
1527 */
1528 if (sb_start_write_trylock(sb))
1529 sb_end_write(sb);
1530 else
1531 return 0;
1532 trans = btrfs_start_transaction(root, 0);
1533 }
1534 if (IS_ERR(trans))
1535 return PTR_ERR(trans);
1536 }
1537 return btrfs_commit_transaction(trans);
1538 }
1539
print_rescue_option(struct seq_file * seq,const char * s,bool * printed)1540 static void print_rescue_option(struct seq_file *seq, const char *s, bool *printed)
1541 {
1542 seq_printf(seq, "%s%s", (*printed) ? ":" : ",rescue=", s);
1543 *printed = true;
1544 }
1545
btrfs_show_options(struct seq_file * seq,struct dentry * dentry)1546 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1547 {
1548 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1549 const char *compress_type;
1550 const char *subvol_name;
1551 bool printed = false;
1552
1553 if (btrfs_test_opt(info, DEGRADED))
1554 seq_puts(seq, ",degraded");
1555 if (btrfs_test_opt(info, NODATASUM))
1556 seq_puts(seq, ",nodatasum");
1557 if (btrfs_test_opt(info, NODATACOW))
1558 seq_puts(seq, ",nodatacow");
1559 if (btrfs_test_opt(info, NOBARRIER))
1560 seq_puts(seq, ",nobarrier");
1561 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1562 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1563 if (info->thread_pool_size != min_t(unsigned long,
1564 num_online_cpus() + 2, 8))
1565 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
1566 if (btrfs_test_opt(info, COMPRESS)) {
1567 compress_type = btrfs_compress_type2str(info->compress_type);
1568 if (btrfs_test_opt(info, FORCE_COMPRESS))
1569 seq_printf(seq, ",compress-force=%s", compress_type);
1570 else
1571 seq_printf(seq, ",compress=%s", compress_type);
1572 if (info->compress_level)
1573 seq_printf(seq, ":%d", info->compress_level);
1574 }
1575 if (btrfs_test_opt(info, NOSSD))
1576 seq_puts(seq, ",nossd");
1577 if (btrfs_test_opt(info, SSD_SPREAD))
1578 seq_puts(seq, ",ssd_spread");
1579 else if (btrfs_test_opt(info, SSD))
1580 seq_puts(seq, ",ssd");
1581 if (btrfs_test_opt(info, NOTREELOG))
1582 seq_puts(seq, ",notreelog");
1583 if (btrfs_test_opt(info, NOLOGREPLAY))
1584 print_rescue_option(seq, "nologreplay", &printed);
1585 if (btrfs_test_opt(info, USEBACKUPROOT))
1586 print_rescue_option(seq, "usebackuproot", &printed);
1587 if (btrfs_test_opt(info, IGNOREBADROOTS))
1588 print_rescue_option(seq, "ignorebadroots", &printed);
1589 if (btrfs_test_opt(info, IGNOREDATACSUMS))
1590 print_rescue_option(seq, "ignoredatacsums", &printed);
1591 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1592 seq_puts(seq, ",flushoncommit");
1593 if (btrfs_test_opt(info, DISCARD_SYNC))
1594 seq_puts(seq, ",discard");
1595 if (btrfs_test_opt(info, DISCARD_ASYNC))
1596 seq_puts(seq, ",discard=async");
1597 if (!(info->sb->s_flags & SB_POSIXACL))
1598 seq_puts(seq, ",noacl");
1599 if (btrfs_free_space_cache_v1_active(info))
1600 seq_puts(seq, ",space_cache");
1601 else if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
1602 seq_puts(seq, ",space_cache=v2");
1603 else
1604 seq_puts(seq, ",nospace_cache");
1605 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1606 seq_puts(seq, ",rescan_uuid_tree");
1607 if (btrfs_test_opt(info, CLEAR_CACHE))
1608 seq_puts(seq, ",clear_cache");
1609 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1610 seq_puts(seq, ",user_subvol_rm_allowed");
1611 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1612 seq_puts(seq, ",enospc_debug");
1613 if (btrfs_test_opt(info, AUTO_DEFRAG))
1614 seq_puts(seq, ",autodefrag");
1615 if (btrfs_test_opt(info, SKIP_BALANCE))
1616 seq_puts(seq, ",skip_balance");
1617 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1618 if (btrfs_test_opt(info, CHECK_INTEGRITY_DATA))
1619 seq_puts(seq, ",check_int_data");
1620 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1621 seq_puts(seq, ",check_int");
1622 if (info->check_integrity_print_mask)
1623 seq_printf(seq, ",check_int_print_mask=%d",
1624 info->check_integrity_print_mask);
1625 #endif
1626 if (info->metadata_ratio)
1627 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
1628 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1629 seq_puts(seq, ",fatal_errors=panic");
1630 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
1631 seq_printf(seq, ",commit=%u", info->commit_interval);
1632 #ifdef CONFIG_BTRFS_DEBUG
1633 if (btrfs_test_opt(info, FRAGMENT_DATA))
1634 seq_puts(seq, ",fragment=data");
1635 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1636 seq_puts(seq, ",fragment=metadata");
1637 #endif
1638 if (btrfs_test_opt(info, REF_VERIFY))
1639 seq_puts(seq, ",ref_verify");
1640 seq_printf(seq, ",subvolid=%llu",
1641 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1642 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1643 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1644 if (!IS_ERR(subvol_name)) {
1645 seq_puts(seq, ",subvol=");
1646 seq_escape(seq, subvol_name, " \t\n\\");
1647 kfree(subvol_name);
1648 }
1649 return 0;
1650 }
1651
btrfs_test_super(struct super_block * s,void * data)1652 static int btrfs_test_super(struct super_block *s, void *data)
1653 {
1654 struct btrfs_fs_info *p = data;
1655 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1656
1657 return fs_info->fs_devices == p->fs_devices;
1658 }
1659
btrfs_set_super(struct super_block * s,void * data)1660 static int btrfs_set_super(struct super_block *s, void *data)
1661 {
1662 int err = set_anon_super(s, data);
1663 if (!err)
1664 s->s_fs_info = data;
1665 return err;
1666 }
1667
1668 /*
1669 * subvolumes are identified by ino 256
1670 */
is_subvolume_inode(struct inode * inode)1671 static inline int is_subvolume_inode(struct inode *inode)
1672 {
1673 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1674 return 1;
1675 return 0;
1676 }
1677
mount_subvol(const char * subvol_name,u64 subvol_objectid,struct vfsmount * mnt)1678 static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
1679 struct vfsmount *mnt)
1680 {
1681 struct dentry *root;
1682 int ret;
1683
1684 if (!subvol_name) {
1685 if (!subvol_objectid) {
1686 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1687 &subvol_objectid);
1688 if (ret) {
1689 root = ERR_PTR(ret);
1690 goto out;
1691 }
1692 }
1693 subvol_name = btrfs_get_subvol_name_from_objectid(
1694 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1695 if (IS_ERR(subvol_name)) {
1696 root = ERR_CAST(subvol_name);
1697 subvol_name = NULL;
1698 goto out;
1699 }
1700
1701 }
1702
1703 root = mount_subtree(mnt, subvol_name);
1704 /* mount_subtree() drops our reference on the vfsmount. */
1705 mnt = NULL;
1706
1707 if (!IS_ERR(root)) {
1708 struct super_block *s = root->d_sb;
1709 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1710 struct inode *root_inode = d_inode(root);
1711 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1712
1713 ret = 0;
1714 if (!is_subvolume_inode(root_inode)) {
1715 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1716 subvol_name);
1717 ret = -EINVAL;
1718 }
1719 if (subvol_objectid && root_objectid != subvol_objectid) {
1720 /*
1721 * This will also catch a race condition where a
1722 * subvolume which was passed by ID is renamed and
1723 * another subvolume is renamed over the old location.
1724 */
1725 btrfs_err(fs_info,
1726 "subvol '%s' does not match subvolid %llu",
1727 subvol_name, subvol_objectid);
1728 ret = -EINVAL;
1729 }
1730 if (ret) {
1731 dput(root);
1732 root = ERR_PTR(ret);
1733 deactivate_locked_super(s);
1734 }
1735 }
1736
1737 out:
1738 mntput(mnt);
1739 kfree(subvol_name);
1740 return root;
1741 }
1742
1743 /*
1744 * Find a superblock for the given device / mount point.
1745 *
1746 * Note: This is based on mount_bdev from fs/super.c with a few additions
1747 * for multiple device setup. Make sure to keep it in sync.
1748 */
btrfs_mount_root(struct file_system_type * fs_type,int flags,const char * device_name,void * data)1749 static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1750 int flags, const char *device_name, void *data)
1751 {
1752 struct block_device *bdev = NULL;
1753 struct super_block *s;
1754 struct btrfs_device *device = NULL;
1755 struct btrfs_fs_devices *fs_devices = NULL;
1756 struct btrfs_fs_info *fs_info = NULL;
1757 void *new_sec_opts = NULL;
1758 fmode_t mode = FMODE_READ;
1759 int error = 0;
1760
1761 if (!(flags & SB_RDONLY))
1762 mode |= FMODE_WRITE;
1763
1764 if (data) {
1765 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
1766 if (error)
1767 return ERR_PTR(error);
1768 }
1769
1770 /*
1771 * Setup a dummy root and fs_info for test/set super. This is because
1772 * we don't actually fill this stuff out until open_ctree, but we need
1773 * then open_ctree will properly initialize the file system specific
1774 * settings later. btrfs_init_fs_info initializes the static elements
1775 * of the fs_info (locks and such) to make cleanup easier if we find a
1776 * superblock with our given fs_devices later on at sget() time.
1777 */
1778 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
1779 if (!fs_info) {
1780 error = -ENOMEM;
1781 goto error_sec_opts;
1782 }
1783 btrfs_init_fs_info(fs_info);
1784
1785 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1786 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1787 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1788 error = -ENOMEM;
1789 goto error_fs_info;
1790 }
1791
1792 mutex_lock(&uuid_mutex);
1793 error = btrfs_parse_device_options(data, mode, fs_type);
1794 if (error) {
1795 mutex_unlock(&uuid_mutex);
1796 goto error_fs_info;
1797 }
1798
1799 device = btrfs_scan_one_device(device_name, mode, fs_type);
1800 if (IS_ERR(device)) {
1801 mutex_unlock(&uuid_mutex);
1802 error = PTR_ERR(device);
1803 goto error_fs_info;
1804 }
1805
1806 fs_devices = device->fs_devices;
1807 fs_info->fs_devices = fs_devices;
1808
1809 error = btrfs_open_devices(fs_devices, mode, fs_type);
1810 mutex_unlock(&uuid_mutex);
1811 if (error)
1812 goto error_fs_info;
1813
1814 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1815 error = -EACCES;
1816 goto error_close_devices;
1817 }
1818
1819 bdev = fs_devices->latest_dev->bdev;
1820 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1821 fs_info);
1822 if (IS_ERR(s)) {
1823 error = PTR_ERR(s);
1824 goto error_close_devices;
1825 }
1826
1827 if (s->s_root) {
1828 btrfs_close_devices(fs_devices);
1829 btrfs_free_fs_info(fs_info);
1830 if ((flags ^ s->s_flags) & SB_RDONLY)
1831 error = -EBUSY;
1832 } else {
1833 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1834 shrinker_debugfs_rename(&s->s_shrink, "sb-%s:%s", fs_type->name,
1835 s->s_id);
1836 btrfs_sb(s)->bdev_holder = fs_type;
1837 error = btrfs_fill_super(s, fs_devices, data);
1838 }
1839 if (!error)
1840 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1841 security_free_mnt_opts(&new_sec_opts);
1842 if (error) {
1843 deactivate_locked_super(s);
1844 return ERR_PTR(error);
1845 }
1846
1847 return dget(s->s_root);
1848
1849 error_close_devices:
1850 btrfs_close_devices(fs_devices);
1851 error_fs_info:
1852 btrfs_free_fs_info(fs_info);
1853 error_sec_opts:
1854 security_free_mnt_opts(&new_sec_opts);
1855 return ERR_PTR(error);
1856 }
1857
1858 /*
1859 * Mount function which is called by VFS layer.
1860 *
1861 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1862 * which needs vfsmount* of device's root (/). This means device's root has to
1863 * be mounted internally in any case.
1864 *
1865 * Operation flow:
1866 * 1. Parse subvol id related options for later use in mount_subvol().
1867 *
1868 * 2. Mount device's root (/) by calling vfs_kern_mount().
1869 *
1870 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1871 * first place. In order to avoid calling btrfs_mount() again, we use
1872 * different file_system_type which is not registered to VFS by
1873 * register_filesystem() (btrfs_root_fs_type). As a result,
1874 * btrfs_mount_root() is called. The return value will be used by
1875 * mount_subtree() in mount_subvol().
1876 *
1877 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1878 * "btrfs subvolume set-default", mount_subvol() is called always.
1879 */
btrfs_mount(struct file_system_type * fs_type,int flags,const char * device_name,void * data)1880 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1881 const char *device_name, void *data)
1882 {
1883 struct vfsmount *mnt_root;
1884 struct dentry *root;
1885 char *subvol_name = NULL;
1886 u64 subvol_objectid = 0;
1887 int error = 0;
1888
1889 error = btrfs_parse_subvol_options(data, &subvol_name,
1890 &subvol_objectid);
1891 if (error) {
1892 kfree(subvol_name);
1893 return ERR_PTR(error);
1894 }
1895
1896 /* mount device's root (/) */
1897 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1898 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1899 if (flags & SB_RDONLY) {
1900 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1901 flags & ~SB_RDONLY, device_name, data);
1902 } else {
1903 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1904 flags | SB_RDONLY, device_name, data);
1905 if (IS_ERR(mnt_root)) {
1906 root = ERR_CAST(mnt_root);
1907 kfree(subvol_name);
1908 goto out;
1909 }
1910
1911 down_write(&mnt_root->mnt_sb->s_umount);
1912 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1913 up_write(&mnt_root->mnt_sb->s_umount);
1914 if (error < 0) {
1915 root = ERR_PTR(error);
1916 mntput(mnt_root);
1917 kfree(subvol_name);
1918 goto out;
1919 }
1920 }
1921 }
1922 if (IS_ERR(mnt_root)) {
1923 root = ERR_CAST(mnt_root);
1924 kfree(subvol_name);
1925 goto out;
1926 }
1927
1928 /* mount_subvol() will free subvol_name and mnt_root */
1929 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1930
1931 out:
1932 return root;
1933 }
1934
btrfs_resize_thread_pool(struct btrfs_fs_info * fs_info,u32 new_pool_size,u32 old_pool_size)1935 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1936 u32 new_pool_size, u32 old_pool_size)
1937 {
1938 if (new_pool_size == old_pool_size)
1939 return;
1940
1941 fs_info->thread_pool_size = new_pool_size;
1942
1943 btrfs_info(fs_info, "resize thread pool %d -> %d",
1944 old_pool_size, new_pool_size);
1945
1946 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1947 btrfs_workqueue_set_max(fs_info->hipri_workers, new_pool_size);
1948 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1949 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1950 workqueue_set_max_active(fs_info->endio_workers, new_pool_size);
1951 workqueue_set_max_active(fs_info->endio_meta_workers, new_pool_size);
1952 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1953 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1954 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1955 }
1956
btrfs_remount_begin(struct btrfs_fs_info * fs_info,unsigned long old_opts,int flags)1957 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1958 unsigned long old_opts, int flags)
1959 {
1960 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1961 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1962 (flags & SB_RDONLY))) {
1963 /* wait for any defraggers to finish */
1964 wait_event(fs_info->transaction_wait,
1965 (atomic_read(&fs_info->defrag_running) == 0));
1966 if (flags & SB_RDONLY)
1967 sync_filesystem(fs_info->sb);
1968 }
1969 }
1970
btrfs_remount_cleanup(struct btrfs_fs_info * fs_info,unsigned long old_opts)1971 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1972 unsigned long old_opts)
1973 {
1974 const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
1975
1976 /*
1977 * We need to cleanup all defragable inodes if the autodefragment is
1978 * close or the filesystem is read only.
1979 */
1980 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1981 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
1982 btrfs_cleanup_defrag_inodes(fs_info);
1983 }
1984
1985 /* If we toggled discard async */
1986 if (!btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1987 btrfs_test_opt(fs_info, DISCARD_ASYNC))
1988 btrfs_discard_resume(fs_info);
1989 else if (btrfs_raw_test_opt(old_opts, DISCARD_ASYNC) &&
1990 !btrfs_test_opt(fs_info, DISCARD_ASYNC))
1991 btrfs_discard_cleanup(fs_info);
1992
1993 /* If we toggled space cache */
1994 if (cache_opt != btrfs_free_space_cache_v1_active(fs_info))
1995 btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
1996 }
1997
btrfs_remount(struct super_block * sb,int * flags,char * data)1998 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1999 {
2000 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2001 unsigned old_flags = sb->s_flags;
2002 unsigned long old_opts = fs_info->mount_opt;
2003 unsigned long old_compress_type = fs_info->compress_type;
2004 u64 old_max_inline = fs_info->max_inline;
2005 u32 old_thread_pool_size = fs_info->thread_pool_size;
2006 u32 old_metadata_ratio = fs_info->metadata_ratio;
2007 int ret;
2008
2009 sync_filesystem(sb);
2010 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2011
2012 if (data) {
2013 void *new_sec_opts = NULL;
2014
2015 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
2016 if (!ret)
2017 ret = security_sb_remount(sb, new_sec_opts);
2018 security_free_mnt_opts(&new_sec_opts);
2019 if (ret)
2020 goto restore;
2021 }
2022
2023 ret = btrfs_parse_options(fs_info, data, *flags);
2024 if (ret)
2025 goto restore;
2026
2027 ret = btrfs_check_features(fs_info, !(*flags & SB_RDONLY));
2028 if (ret < 0)
2029 goto restore;
2030
2031 btrfs_remount_begin(fs_info, old_opts, *flags);
2032 btrfs_resize_thread_pool(fs_info,
2033 fs_info->thread_pool_size, old_thread_pool_size);
2034
2035 if ((bool)btrfs_test_opt(fs_info, FREE_SPACE_TREE) !=
2036 (bool)btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
2037 (!sb_rdonly(sb) || (*flags & SB_RDONLY))) {
2038 btrfs_warn(fs_info,
2039 "remount supports changing free space tree only from ro to rw");
2040 /* Make sure free space cache options match the state on disk */
2041 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
2042 btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2043 btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
2044 }
2045 if (btrfs_free_space_cache_v1_active(fs_info)) {
2046 btrfs_clear_opt(fs_info->mount_opt, FREE_SPACE_TREE);
2047 btrfs_set_opt(fs_info->mount_opt, SPACE_CACHE);
2048 }
2049 }
2050
2051 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
2052 goto out;
2053
2054 if (*flags & SB_RDONLY) {
2055 /*
2056 * this also happens on 'umount -rf' or on shutdown, when
2057 * the filesystem is busy.
2058 */
2059 cancel_work_sync(&fs_info->async_reclaim_work);
2060 cancel_work_sync(&fs_info->async_data_reclaim_work);
2061
2062 btrfs_discard_cleanup(fs_info);
2063
2064 /* wait for the uuid_scan task to finish */
2065 down(&fs_info->uuid_tree_rescan_sem);
2066 /* avoid complains from lockdep et al. */
2067 up(&fs_info->uuid_tree_rescan_sem);
2068
2069 btrfs_set_sb_rdonly(sb);
2070
2071 /*
2072 * Setting SB_RDONLY will put the cleaner thread to
2073 * sleep at the next loop if it's already active.
2074 * If it's already asleep, we'll leave unused block
2075 * groups on disk until we're mounted read-write again
2076 * unless we clean them up here.
2077 */
2078 btrfs_delete_unused_bgs(fs_info);
2079
2080 /*
2081 * The cleaner task could be already running before we set the
2082 * flag BTRFS_FS_STATE_RO (and SB_RDONLY in the superblock).
2083 * We must make sure that after we finish the remount, i.e. after
2084 * we call btrfs_commit_super(), the cleaner can no longer start
2085 * a transaction - either because it was dropping a dead root,
2086 * running delayed iputs or deleting an unused block group (the
2087 * cleaner picked a block group from the list of unused block
2088 * groups before we were able to in the previous call to
2089 * btrfs_delete_unused_bgs()).
2090 */
2091 wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING,
2092 TASK_UNINTERRUPTIBLE);
2093
2094 /*
2095 * We've set the superblock to RO mode, so we might have made
2096 * the cleaner task sleep without running all pending delayed
2097 * iputs. Go through all the delayed iputs here, so that if an
2098 * unmount happens without remounting RW we don't end up at
2099 * finishing close_ctree() with a non-empty list of delayed
2100 * iputs.
2101 */
2102 btrfs_run_delayed_iputs(fs_info);
2103
2104 btrfs_dev_replace_suspend_for_unmount(fs_info);
2105 btrfs_scrub_cancel(fs_info);
2106 btrfs_pause_balance(fs_info);
2107
2108 /*
2109 * Pause the qgroup rescan worker if it is running. We don't want
2110 * it to be still running after we are in RO mode, as after that,
2111 * by the time we unmount, it might have left a transaction open,
2112 * so we would leak the transaction and/or crash.
2113 */
2114 btrfs_qgroup_wait_for_completion(fs_info, false);
2115
2116 ret = btrfs_commit_super(fs_info);
2117 if (ret)
2118 goto restore;
2119 } else {
2120 if (BTRFS_FS_ERROR(fs_info)) {
2121 btrfs_err(fs_info,
2122 "Remounting read-write after error is not allowed");
2123 ret = -EINVAL;
2124 goto restore;
2125 }
2126 if (fs_info->fs_devices->rw_devices == 0) {
2127 ret = -EACCES;
2128 goto restore;
2129 }
2130
2131 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
2132 btrfs_warn(fs_info,
2133 "too many missing devices, writable remount is not allowed");
2134 ret = -EACCES;
2135 goto restore;
2136 }
2137
2138 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
2139 btrfs_warn(fs_info,
2140 "mount required to replay tree-log, cannot remount read-write");
2141 ret = -EINVAL;
2142 goto restore;
2143 }
2144
2145 /*
2146 * NOTE: when remounting with a change that does writes, don't
2147 * put it anywhere above this point, as we are not sure to be
2148 * safe to write until we pass the above checks.
2149 */
2150 ret = btrfs_start_pre_rw_mount(fs_info);
2151 if (ret)
2152 goto restore;
2153
2154 btrfs_clear_sb_rdonly(sb);
2155
2156 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
2157 }
2158 out:
2159 /*
2160 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
2161 * since the absence of the flag means it can be toggled off by remount.
2162 */
2163 *flags |= SB_I_VERSION;
2164
2165 wake_up_process(fs_info->transaction_kthread);
2166 btrfs_remount_cleanup(fs_info, old_opts);
2167 btrfs_clear_oneshot_options(fs_info);
2168 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2169
2170 return 0;
2171
2172 restore:
2173 /* We've hit an error - don't reset SB_RDONLY */
2174 if (sb_rdonly(sb))
2175 old_flags |= SB_RDONLY;
2176 if (!(old_flags & SB_RDONLY))
2177 clear_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
2178 sb->s_flags = old_flags;
2179 fs_info->mount_opt = old_opts;
2180 fs_info->compress_type = old_compress_type;
2181 fs_info->max_inline = old_max_inline;
2182 btrfs_resize_thread_pool(fs_info,
2183 old_thread_pool_size, fs_info->thread_pool_size);
2184 fs_info->metadata_ratio = old_metadata_ratio;
2185 btrfs_remount_cleanup(fs_info, old_opts);
2186 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
2187
2188 return ret;
2189 }
2190
2191 /* Used to sort the devices by max_avail(descending sort) */
btrfs_cmp_device_free_bytes(const void * a,const void * b)2192 static int btrfs_cmp_device_free_bytes(const void *a, const void *b)
2193 {
2194 const struct btrfs_device_info *dev_info1 = a;
2195 const struct btrfs_device_info *dev_info2 = b;
2196
2197 if (dev_info1->max_avail > dev_info2->max_avail)
2198 return -1;
2199 else if (dev_info1->max_avail < dev_info2->max_avail)
2200 return 1;
2201 return 0;
2202 }
2203
2204 /*
2205 * sort the devices by max_avail, in which max free extent size of each device
2206 * is stored.(Descending Sort)
2207 */
btrfs_descending_sort_devices(struct btrfs_device_info * devices,size_t nr_devices)2208 static inline void btrfs_descending_sort_devices(
2209 struct btrfs_device_info *devices,
2210 size_t nr_devices)
2211 {
2212 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
2213 btrfs_cmp_device_free_bytes, NULL);
2214 }
2215
2216 /*
2217 * The helper to calc the free space on the devices that can be used to store
2218 * file data.
2219 */
btrfs_calc_avail_data_space(struct btrfs_fs_info * fs_info,u64 * free_bytes)2220 static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
2221 u64 *free_bytes)
2222 {
2223 struct btrfs_device_info *devices_info;
2224 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2225 struct btrfs_device *device;
2226 u64 type;
2227 u64 avail_space;
2228 u64 min_stripe_size;
2229 int num_stripes = 1;
2230 int i = 0, nr_devices;
2231 const struct btrfs_raid_attr *rattr;
2232
2233 /*
2234 * We aren't under the device list lock, so this is racy-ish, but good
2235 * enough for our purposes.
2236 */
2237 nr_devices = fs_info->fs_devices->open_devices;
2238 if (!nr_devices) {
2239 smp_mb();
2240 nr_devices = fs_info->fs_devices->open_devices;
2241 ASSERT(nr_devices);
2242 if (!nr_devices) {
2243 *free_bytes = 0;
2244 return 0;
2245 }
2246 }
2247
2248 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
2249 GFP_KERNEL);
2250 if (!devices_info)
2251 return -ENOMEM;
2252
2253 /* calc min stripe number for data space allocation */
2254 type = btrfs_data_alloc_profile(fs_info);
2255 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
2256
2257 if (type & BTRFS_BLOCK_GROUP_RAID0)
2258 num_stripes = nr_devices;
2259 else if (type & BTRFS_BLOCK_GROUP_RAID1_MASK)
2260 num_stripes = rattr->ncopies;
2261 else if (type & BTRFS_BLOCK_GROUP_RAID10)
2262 num_stripes = 4;
2263
2264 /* Adjust for more than 1 stripe per device */
2265 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
2266
2267 rcu_read_lock();
2268 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
2269 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2270 &device->dev_state) ||
2271 !device->bdev ||
2272 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
2273 continue;
2274
2275 if (i >= nr_devices)
2276 break;
2277
2278 avail_space = device->total_bytes - device->bytes_used;
2279
2280 /* align with stripe_len */
2281 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
2282
2283 /*
2284 * Ensure we have at least min_stripe_size on top of the
2285 * reserved space on the device.
2286 */
2287 if (avail_space <= BTRFS_DEVICE_RANGE_RESERVED + min_stripe_size)
2288 continue;
2289
2290 avail_space -= BTRFS_DEVICE_RANGE_RESERVED;
2291
2292 devices_info[i].dev = device;
2293 devices_info[i].max_avail = avail_space;
2294
2295 i++;
2296 }
2297 rcu_read_unlock();
2298
2299 nr_devices = i;
2300
2301 btrfs_descending_sort_devices(devices_info, nr_devices);
2302
2303 i = nr_devices - 1;
2304 avail_space = 0;
2305 while (nr_devices >= rattr->devs_min) {
2306 num_stripes = min(num_stripes, nr_devices);
2307
2308 if (devices_info[i].max_avail >= min_stripe_size) {
2309 int j;
2310 u64 alloc_size;
2311
2312 avail_space += devices_info[i].max_avail * num_stripes;
2313 alloc_size = devices_info[i].max_avail;
2314 for (j = i + 1 - num_stripes; j <= i; j++)
2315 devices_info[j].max_avail -= alloc_size;
2316 }
2317 i--;
2318 nr_devices--;
2319 }
2320
2321 kfree(devices_info);
2322 *free_bytes = avail_space;
2323 return 0;
2324 }
2325
2326 /*
2327 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2328 *
2329 * If there's a redundant raid level at DATA block groups, use the respective
2330 * multiplier to scale the sizes.
2331 *
2332 * Unused device space usage is based on simulating the chunk allocator
2333 * algorithm that respects the device sizes and order of allocations. This is
2334 * a close approximation of the actual use but there are other factors that may
2335 * change the result (like a new metadata chunk).
2336 *
2337 * If metadata is exhausted, f_bavail will be 0.
2338 */
btrfs_statfs(struct dentry * dentry,struct kstatfs * buf)2339 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2340 {
2341 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2342 struct btrfs_super_block *disk_super = fs_info->super_copy;
2343 struct btrfs_space_info *found;
2344 u64 total_used = 0;
2345 u64 total_free_data = 0;
2346 u64 total_free_meta = 0;
2347 u32 bits = fs_info->sectorsize_bits;
2348 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
2349 unsigned factor = 1;
2350 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2351 int ret;
2352 u64 thresh = 0;
2353 int mixed = 0;
2354
2355 list_for_each_entry(found, &fs_info->space_info, list) {
2356 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2357 int i;
2358
2359 total_free_data += found->disk_total - found->disk_used;
2360 total_free_data -=
2361 btrfs_account_ro_block_groups_free_space(found);
2362
2363 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
2364 if (!list_empty(&found->block_groups[i]))
2365 factor = btrfs_bg_type_to_factor(
2366 btrfs_raid_array[i].bg_flag);
2367 }
2368 }
2369
2370 /*
2371 * Metadata in mixed block goup profiles are accounted in data
2372 */
2373 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2374 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2375 mixed = 1;
2376 else
2377 total_free_meta += found->disk_total -
2378 found->disk_used;
2379 }
2380
2381 total_used += found->disk_used;
2382 }
2383
2384 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2385 buf->f_blocks >>= bits;
2386 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2387
2388 /* Account global block reserve as used, it's in logical size already */
2389 spin_lock(&block_rsv->lock);
2390 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2391 if (buf->f_bfree >= block_rsv->size >> bits)
2392 buf->f_bfree -= block_rsv->size >> bits;
2393 else
2394 buf->f_bfree = 0;
2395 spin_unlock(&block_rsv->lock);
2396
2397 buf->f_bavail = div_u64(total_free_data, factor);
2398 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
2399 if (ret)
2400 return ret;
2401 buf->f_bavail += div_u64(total_free_data, factor);
2402 buf->f_bavail = buf->f_bavail >> bits;
2403
2404 /*
2405 * We calculate the remaining metadata space minus global reserve. If
2406 * this is (supposedly) smaller than zero, there's no space. But this
2407 * does not hold in practice, the exhausted state happens where's still
2408 * some positive delta. So we apply some guesswork and compare the
2409 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2410 *
2411 * We probably cannot calculate the exact threshold value because this
2412 * depends on the internal reservations requested by various
2413 * operations, so some operations that consume a few metadata will
2414 * succeed even if the Avail is zero. But this is better than the other
2415 * way around.
2416 */
2417 thresh = SZ_4M;
2418
2419 /*
2420 * We only want to claim there's no available space if we can no longer
2421 * allocate chunks for our metadata profile and our global reserve will
2422 * not fit in the free metadata space. If we aren't ->full then we
2423 * still can allocate chunks and thus are fine using the currently
2424 * calculated f_bavail.
2425 */
2426 if (!mixed && block_rsv->space_info->full &&
2427 (total_free_meta < thresh || total_free_meta - thresh < block_rsv->size))
2428 buf->f_bavail = 0;
2429
2430 buf->f_type = BTRFS_SUPER_MAGIC;
2431 buf->f_bsize = dentry->d_sb->s_blocksize;
2432 buf->f_namelen = BTRFS_NAME_LEN;
2433
2434 /* We treat it as constant endianness (it doesn't matter _which_)
2435 because we want the fsid to come out the same whether mounted
2436 on a big-endian or little-endian host */
2437 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2438 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2439 /* Mask in the root object ID too, to disambiguate subvols */
2440 buf->f_fsid.val[0] ^=
2441 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2442 buf->f_fsid.val[1] ^=
2443 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
2444
2445 return 0;
2446 }
2447
btrfs_kill_super(struct super_block * sb)2448 static void btrfs_kill_super(struct super_block *sb)
2449 {
2450 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2451 kill_anon_super(sb);
2452 btrfs_free_fs_info(fs_info);
2453 }
2454
2455 static struct file_system_type btrfs_fs_type = {
2456 .owner = THIS_MODULE,
2457 .name = "btrfs",
2458 .mount = btrfs_mount,
2459 .kill_sb = btrfs_kill_super,
2460 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2461 };
2462
2463 static struct file_system_type btrfs_root_fs_type = {
2464 .owner = THIS_MODULE,
2465 .name = "btrfs",
2466 .mount = btrfs_mount_root,
2467 .kill_sb = btrfs_kill_super,
2468 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA | FS_ALLOW_IDMAP,
2469 };
2470
2471 MODULE_ALIAS_FS("btrfs");
2472
btrfs_control_open(struct inode * inode,struct file * file)2473 static int btrfs_control_open(struct inode *inode, struct file *file)
2474 {
2475 /*
2476 * The control file's private_data is used to hold the
2477 * transaction when it is started and is used to keep
2478 * track of whether a transaction is already in progress.
2479 */
2480 file->private_data = NULL;
2481 return 0;
2482 }
2483
2484 /*
2485 * Used by /dev/btrfs-control for devices ioctls.
2486 */
btrfs_control_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2487 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2488 unsigned long arg)
2489 {
2490 struct btrfs_ioctl_vol_args *vol;
2491 struct btrfs_device *device = NULL;
2492 dev_t devt = 0;
2493 int ret = -ENOTTY;
2494
2495 if (!capable(CAP_SYS_ADMIN))
2496 return -EPERM;
2497
2498 vol = memdup_user((void __user *)arg, sizeof(*vol));
2499 if (IS_ERR(vol))
2500 return PTR_ERR(vol);
2501 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2502
2503 switch (cmd) {
2504 case BTRFS_IOC_SCAN_DEV:
2505 mutex_lock(&uuid_mutex);
2506 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2507 &btrfs_root_fs_type);
2508 ret = PTR_ERR_OR_ZERO(device);
2509 mutex_unlock(&uuid_mutex);
2510 break;
2511 case BTRFS_IOC_FORGET_DEV:
2512 if (vol->name[0] != 0) {
2513 ret = lookup_bdev(vol->name, &devt);
2514 if (ret)
2515 break;
2516 }
2517 ret = btrfs_forget_devices(devt);
2518 break;
2519 case BTRFS_IOC_DEVICES_READY:
2520 mutex_lock(&uuid_mutex);
2521 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2522 &btrfs_root_fs_type);
2523 if (IS_ERR(device)) {
2524 mutex_unlock(&uuid_mutex);
2525 ret = PTR_ERR(device);
2526 break;
2527 }
2528 ret = !(device->fs_devices->num_devices ==
2529 device->fs_devices->total_devices);
2530 mutex_unlock(&uuid_mutex);
2531 break;
2532 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2533 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2534 break;
2535 }
2536
2537 kfree(vol);
2538 return ret;
2539 }
2540
btrfs_freeze(struct super_block * sb)2541 static int btrfs_freeze(struct super_block *sb)
2542 {
2543 struct btrfs_trans_handle *trans;
2544 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2545 struct btrfs_root *root = fs_info->tree_root;
2546
2547 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2548 /*
2549 * We don't need a barrier here, we'll wait for any transaction that
2550 * could be in progress on other threads (and do delayed iputs that
2551 * we want to avoid on a frozen filesystem), or do the commit
2552 * ourselves.
2553 */
2554 trans = btrfs_attach_transaction_barrier(root);
2555 if (IS_ERR(trans)) {
2556 /* no transaction, don't bother */
2557 if (PTR_ERR(trans) == -ENOENT)
2558 return 0;
2559 return PTR_ERR(trans);
2560 }
2561 return btrfs_commit_transaction(trans);
2562 }
2563
check_dev_super(struct btrfs_device * dev)2564 static int check_dev_super(struct btrfs_device *dev)
2565 {
2566 struct btrfs_fs_info *fs_info = dev->fs_info;
2567 struct btrfs_super_block *sb;
2568 u16 csum_type;
2569 int ret = 0;
2570
2571 /* This should be called with fs still frozen. */
2572 ASSERT(test_bit(BTRFS_FS_FROZEN, &fs_info->flags));
2573
2574 /* Missing dev, no need to check. */
2575 if (!dev->bdev)
2576 return 0;
2577
2578 /* Only need to check the primary super block. */
2579 sb = btrfs_read_dev_one_super(dev->bdev, 0, true);
2580 if (IS_ERR(sb))
2581 return PTR_ERR(sb);
2582
2583 /* Verify the checksum. */
2584 csum_type = btrfs_super_csum_type(sb);
2585 if (csum_type != btrfs_super_csum_type(fs_info->super_copy)) {
2586 btrfs_err(fs_info, "csum type changed, has %u expect %u",
2587 csum_type, btrfs_super_csum_type(fs_info->super_copy));
2588 ret = -EUCLEAN;
2589 goto out;
2590 }
2591
2592 if (btrfs_check_super_csum(fs_info, sb)) {
2593 btrfs_err(fs_info, "csum for on-disk super block no longer matches");
2594 ret = -EUCLEAN;
2595 goto out;
2596 }
2597
2598 /* Btrfs_validate_super() includes fsid check against super->fsid. */
2599 ret = btrfs_validate_super(fs_info, sb, 0);
2600 if (ret < 0)
2601 goto out;
2602
2603 if (btrfs_super_generation(sb) != fs_info->last_trans_committed) {
2604 btrfs_err(fs_info, "transid mismatch, has %llu expect %llu",
2605 btrfs_super_generation(sb),
2606 fs_info->last_trans_committed);
2607 ret = -EUCLEAN;
2608 goto out;
2609 }
2610 out:
2611 btrfs_release_disk_super(sb);
2612 return ret;
2613 }
2614
btrfs_unfreeze(struct super_block * sb)2615 static int btrfs_unfreeze(struct super_block *sb)
2616 {
2617 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2618 struct btrfs_device *device;
2619 int ret = 0;
2620
2621 /*
2622 * Make sure the fs is not changed by accident (like hibernation then
2623 * modified by other OS).
2624 * If we found anything wrong, we mark the fs error immediately.
2625 *
2626 * And since the fs is frozen, no one can modify the fs yet, thus
2627 * we don't need to hold device_list_mutex.
2628 */
2629 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
2630 ret = check_dev_super(device);
2631 if (ret < 0) {
2632 btrfs_handle_fs_error(fs_info, ret,
2633 "super block on devid %llu got modified unexpectedly",
2634 device->devid);
2635 break;
2636 }
2637 }
2638 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
2639
2640 /*
2641 * We still return 0, to allow VFS layer to unfreeze the fs even the
2642 * above checks failed. Since the fs is either fine or read-only, we're
2643 * safe to continue, without causing further damage.
2644 */
2645 return 0;
2646 }
2647
btrfs_show_devname(struct seq_file * m,struct dentry * root)2648 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2649 {
2650 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2651
2652 /*
2653 * There should be always a valid pointer in latest_dev, it may be stale
2654 * for a short moment in case it's being deleted but still valid until
2655 * the end of RCU grace period.
2656 */
2657 rcu_read_lock();
2658 seq_escape(m, rcu_str_deref(fs_info->fs_devices->latest_dev->name), " \t\n\\");
2659 rcu_read_unlock();
2660
2661 return 0;
2662 }
2663
2664 static const struct super_operations btrfs_super_ops = {
2665 .drop_inode = btrfs_drop_inode,
2666 .evict_inode = btrfs_evict_inode,
2667 .put_super = btrfs_put_super,
2668 .sync_fs = btrfs_sync_fs,
2669 .show_options = btrfs_show_options,
2670 .show_devname = btrfs_show_devname,
2671 .alloc_inode = btrfs_alloc_inode,
2672 .destroy_inode = btrfs_destroy_inode,
2673 .free_inode = btrfs_free_inode,
2674 .statfs = btrfs_statfs,
2675 .remount_fs = btrfs_remount,
2676 .freeze_fs = btrfs_freeze,
2677 .unfreeze_fs = btrfs_unfreeze,
2678 };
2679
2680 static const struct file_operations btrfs_ctl_fops = {
2681 .open = btrfs_control_open,
2682 .unlocked_ioctl = btrfs_control_ioctl,
2683 .compat_ioctl = compat_ptr_ioctl,
2684 .owner = THIS_MODULE,
2685 .llseek = noop_llseek,
2686 };
2687
2688 static struct miscdevice btrfs_misc = {
2689 .minor = BTRFS_MINOR,
2690 .name = "btrfs-control",
2691 .fops = &btrfs_ctl_fops
2692 };
2693
2694 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2695 MODULE_ALIAS("devname:btrfs-control");
2696
btrfs_interface_init(void)2697 static int __init btrfs_interface_init(void)
2698 {
2699 return misc_register(&btrfs_misc);
2700 }
2701
btrfs_interface_exit(void)2702 static __cold void btrfs_interface_exit(void)
2703 {
2704 misc_deregister(&btrfs_misc);
2705 }
2706
btrfs_print_mod_info(void)2707 static void __init btrfs_print_mod_info(void)
2708 {
2709 static const char options[] = ""
2710 #ifdef CONFIG_BTRFS_DEBUG
2711 ", debug=on"
2712 #endif
2713 #ifdef CONFIG_BTRFS_ASSERT
2714 ", assert=on"
2715 #endif
2716 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2717 ", integrity-checker=on"
2718 #endif
2719 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
2720 ", ref-verify=on"
2721 #endif
2722 #ifdef CONFIG_BLK_DEV_ZONED
2723 ", zoned=yes"
2724 #else
2725 ", zoned=no"
2726 #endif
2727 #ifdef CONFIG_FS_VERITY
2728 ", fsverity=yes"
2729 #else
2730 ", fsverity=no"
2731 #endif
2732 ;
2733 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
2734 }
2735
init_btrfs_fs(void)2736 static int __init init_btrfs_fs(void)
2737 {
2738 int err;
2739
2740 btrfs_props_init();
2741
2742 err = btrfs_init_sysfs();
2743 if (err)
2744 return err;
2745
2746 btrfs_init_compress();
2747
2748 err = btrfs_init_cachep();
2749 if (err)
2750 goto free_compress;
2751
2752 err = extent_state_init_cachep();
2753 if (err)
2754 goto free_cachep;
2755
2756 err = extent_buffer_init_cachep();
2757 if (err)
2758 goto free_extent_cachep;
2759
2760 err = btrfs_bioset_init();
2761 if (err)
2762 goto free_eb_cachep;
2763
2764 err = extent_map_init();
2765 if (err)
2766 goto free_bioset;
2767
2768 err = ordered_data_init();
2769 if (err)
2770 goto free_extent_map;
2771
2772 err = btrfs_delayed_inode_init();
2773 if (err)
2774 goto free_ordered_data;
2775
2776 err = btrfs_auto_defrag_init();
2777 if (err)
2778 goto free_delayed_inode;
2779
2780 err = btrfs_delayed_ref_init();
2781 if (err)
2782 goto free_auto_defrag;
2783
2784 err = btrfs_prelim_ref_init();
2785 if (err)
2786 goto free_delayed_ref;
2787
2788 err = btrfs_interface_init();
2789 if (err)
2790 goto free_prelim_ref;
2791
2792 btrfs_print_mod_info();
2793
2794 err = btrfs_run_sanity_tests();
2795 if (err)
2796 goto unregister_ioctl;
2797
2798 err = register_filesystem(&btrfs_fs_type);
2799 if (err)
2800 goto unregister_ioctl;
2801
2802 return 0;
2803
2804 unregister_ioctl:
2805 btrfs_interface_exit();
2806 free_prelim_ref:
2807 btrfs_prelim_ref_exit();
2808 free_delayed_ref:
2809 btrfs_delayed_ref_exit();
2810 free_auto_defrag:
2811 btrfs_auto_defrag_exit();
2812 free_delayed_inode:
2813 btrfs_delayed_inode_exit();
2814 free_ordered_data:
2815 ordered_data_exit();
2816 free_extent_map:
2817 extent_map_exit();
2818 free_bioset:
2819 btrfs_bioset_exit();
2820 free_eb_cachep:
2821 extent_buffer_free_cachep();
2822 free_extent_cachep:
2823 extent_state_free_cachep();
2824 free_cachep:
2825 btrfs_destroy_cachep();
2826 free_compress:
2827 btrfs_exit_compress();
2828 btrfs_exit_sysfs();
2829
2830 return err;
2831 }
2832
exit_btrfs_fs(void)2833 static void __exit exit_btrfs_fs(void)
2834 {
2835 btrfs_destroy_cachep();
2836 btrfs_delayed_ref_exit();
2837 btrfs_auto_defrag_exit();
2838 btrfs_delayed_inode_exit();
2839 btrfs_prelim_ref_exit();
2840 ordered_data_exit();
2841 extent_map_exit();
2842 btrfs_bioset_exit();
2843 extent_state_free_cachep();
2844 extent_buffer_free_cachep();
2845 btrfs_interface_exit();
2846 unregister_filesystem(&btrfs_fs_type);
2847 btrfs_exit_sysfs();
2848 btrfs_cleanup_fs_uuids();
2849 btrfs_exit_compress();
2850 }
2851
2852 late_initcall(init_btrfs_fs);
2853 module_exit(exit_btrfs_fs)
2854
2855 MODULE_LICENSE("GPL");
2856 MODULE_SOFTDEP("pre: crc32c");
2857 MODULE_SOFTDEP("pre: xxhash64");
2858 MODULE_SOFTDEP("pre: sha256");
2859 MODULE_SOFTDEP("pre: blake2b-256");
2860