blob: 6a2ae208ff80a47038ae3a9dec114719d06b5807 [file] [log] [blame]
Googler9398cc32022-12-02 17:21:52 +08001// SPDX-License-Identifier: GPL-2.0
Googleraf606d22022-10-26 21:40:12 -07002/*
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>
Googler9398cc32022-12-02 17:21:52 +080028#include <linux/crc32c.h>
Googleraf606d22022-10-26 21:40:12 -070029#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"
Googler9398cc32022-12-02 17:21:52 +080045#include "space-info.h"
46#include "sysfs.h"
Googleraf606d22022-10-26 21:40:12 -070047#include "tests/btrfs-tests.h"
Googler9398cc32022-12-02 17:21:52 +080048#include "block-group.h"
Googleraf606d22022-10-26 21:40:12 -070049
50#include "qgroup.h"
51#define CREATE_TRACE_POINTS
52#include <trace/events/btrfs.h>
53
54static const struct super_operations btrfs_super_ops;
Googler9398cc32022-12-02 17:21:52 +080055
56/*
57 * Types for mounting the default subvolume and a subvolume explicitly
58 * requested by subvol=/path. That way the callchain is straightforward and we
59 * don't have to play tricks with the mount options and recursive calls to
60 * btrfs_mount.
61 *
62 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
63 */
Googleraf606d22022-10-26 21:40:12 -070064static struct file_system_type btrfs_fs_type;
Googler9398cc32022-12-02 17:21:52 +080065static struct file_system_type btrfs_root_fs_type;
Googleraf606d22022-10-26 21:40:12 -070066
67static int btrfs_remount(struct super_block *sb, int *flags, char *data);
68
69const char *btrfs_decode_error(int errno)
70{
71 char *errstr = "unknown";
72
73 switch (errno) {
74 case -EIO:
75 errstr = "IO failure";
76 break;
77 case -ENOMEM:
78 errstr = "Out of memory";
79 break;
80 case -EROFS:
81 errstr = "Readonly filesystem";
82 break;
83 case -EEXIST:
84 errstr = "Object already exists";
85 break;
86 case -ENOSPC:
87 errstr = "No space left";
88 break;
89 case -ENOENT:
90 errstr = "No such entry";
91 break;
92 }
93
94 return errstr;
95}
96
97/*
98 * __btrfs_handle_fs_error decodes expected errors from the caller and
Googler9398cc32022-12-02 17:21:52 +080099 * invokes the appropriate error response.
Googleraf606d22022-10-26 21:40:12 -0700100 */
101__cold
102void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
103 unsigned int line, int errno, const char *fmt, ...)
104{
105 struct super_block *sb = fs_info->sb;
106#ifdef CONFIG_PRINTK
107 const char *errstr;
108#endif
109
110 /*
111 * Special case: if the error is EROFS, and we're already
Googler9398cc32022-12-02 17:21:52 +0800112 * under SB_RDONLY, then it is safe here.
Googleraf606d22022-10-26 21:40:12 -0700113 */
Googler9398cc32022-12-02 17:21:52 +0800114 if (errno == -EROFS && sb_rdonly(sb))
Googleraf606d22022-10-26 21:40:12 -0700115 return;
116
117#ifdef CONFIG_PRINTK
118 errstr = btrfs_decode_error(errno);
119 if (fmt) {
120 struct va_format vaf;
121 va_list args;
122
123 va_start(args, fmt);
124 vaf.fmt = fmt;
125 vaf.va = &args;
126
127 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
128 sb->s_id, function, line, errno, errstr, &vaf);
129 va_end(args);
130 } else {
131 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
132 sb->s_id, function, line, errno, errstr);
133 }
134#endif
135
136 /*
137 * Today we only save the error info to memory. Long term we'll
138 * also send it down to the disk
139 */
140 set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
141
142 /* Don't go through full error handling during mount */
Googler9398cc32022-12-02 17:21:52 +0800143 if (!(sb->s_flags & SB_BORN))
144 return;
145
146 if (sb_rdonly(sb))
147 return;
148
149 /* btrfs handle error by forcing the filesystem readonly */
150 sb->s_flags |= SB_RDONLY;
151 btrfs_info(fs_info, "forced readonly");
152 /*
153 * Note that a running device replace operation is not canceled here
154 * although there is no way to update the progress. It would add the
155 * risk of a deadlock, therefore the canceling is omitted. The only
156 * penalty is that some I/O remains active until the procedure
157 * completes. The next time when the filesystem is mounted writable
158 * again, the device replace operation continues.
159 */
Googleraf606d22022-10-26 21:40:12 -0700160}
161
162#ifdef CONFIG_PRINTK
163static const char * const logtypes[] = {
164 "emergency",
165 "alert",
166 "critical",
167 "error",
168 "warning",
169 "notice",
170 "info",
171 "debug",
172};
173
174
175/*
176 * Use one ratelimit state per log level so that a flood of less important
177 * messages doesn't cause more important ones to be dropped.
178 */
179static struct ratelimit_state printk_limits[] = {
180 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100),
181 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100),
182 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100),
183 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100),
184 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100),
185 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100),
186 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100),
187 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
188};
189
190void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
191{
Googler9398cc32022-12-02 17:21:52 +0800192 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
Googleraf606d22022-10-26 21:40:12 -0700193 struct va_format vaf;
194 va_list args;
Googler9726be62022-12-14 05:53:31 +0000195 int kern_level;
Googler9398cc32022-12-02 17:21:52 +0800196 const char *type = logtypes[4];
197 struct ratelimit_state *ratelimit = &printk_limits[4];
Googleraf606d22022-10-26 21:40:12 -0700198
199 va_start(args, fmt);
200
Googler9398cc32022-12-02 17:21:52 +0800201 while ((kern_level = printk_get_level(fmt)) != 0) {
Googleraf606d22022-10-26 21:40:12 -0700202 size_t size = printk_skip_level(fmt) - fmt;
Googler9398cc32022-12-02 17:21:52 +0800203
204 if (kern_level >= '0' && kern_level <= '7') {
205 memcpy(lvl, fmt, size);
206 lvl[size] = '\0';
207 type = logtypes[kern_level - '0'];
208 ratelimit = &printk_limits[kern_level - '0'];
209 }
Googleraf606d22022-10-26 21:40:12 -0700210 fmt += size;
211 }
212
213 vaf.fmt = fmt;
214 vaf.va = &args;
215
216 if (__ratelimit(ratelimit))
Googler9398cc32022-12-02 17:21:52 +0800217 printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
218 fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
Googleraf606d22022-10-26 21:40:12 -0700219
220 va_end(args);
221}
222#endif
223
224/*
225 * We only mark the transaction aborted and then set the file system read-only.
226 * This will prevent new transactions from starting or trying to join this
227 * one.
228 *
229 * This means that error recovery at the call site is limited to freeing
230 * any local memory allocations and passing the error code up without
231 * further cleanup. The transaction should complete as it normally would
232 * in the call path but will return -EIO.
233 *
234 * We'll complete the cleanup in btrfs_end_transaction and
235 * btrfs_commit_transaction.
236 */
237__cold
238void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
239 const char *function,
240 unsigned int line, int errno)
241{
242 struct btrfs_fs_info *fs_info = trans->fs_info;
243
Googler9398cc32022-12-02 17:21:52 +0800244 WRITE_ONCE(trans->aborted, errno);
Googleraf606d22022-10-26 21:40:12 -0700245 /* Nothing used. The other threads that have joined this
246 * transaction may be able to continue. */
247 if (!trans->dirty && list_empty(&trans->new_bgs)) {
248 const char *errstr;
249
250 errstr = btrfs_decode_error(errno);
251 btrfs_warn(fs_info,
252 "%s:%d: Aborting unused transaction(%s).",
253 function, line, errstr);
254 return;
255 }
Googler9398cc32022-12-02 17:21:52 +0800256 WRITE_ONCE(trans->transaction->aborted, errno);
Googleraf606d22022-10-26 21:40:12 -0700257 /* Wake up anybody who may be waiting on this transaction */
258 wake_up(&fs_info->transaction_wait);
259 wake_up(&fs_info->transaction_blocked_wait);
260 __btrfs_handle_fs_error(fs_info, function, line, errno, NULL);
261}
262/*
263 * __btrfs_panic decodes unexpected, fatal errors from the caller,
264 * issues an alert, and either panics or BUGs, depending on mount options.
265 */
266__cold
267void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
268 unsigned int line, int errno, const char *fmt, ...)
269{
270 char *s_id = "<unknown>";
271 const char *errstr;
272 struct va_format vaf = { .fmt = fmt };
273 va_list args;
274
275 if (fs_info)
276 s_id = fs_info->sb->s_id;
277
278 va_start(args, fmt);
279 vaf.va = &args;
280
281 errstr = btrfs_decode_error(errno);
Googler9398cc32022-12-02 17:21:52 +0800282 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
Googleraf606d22022-10-26 21:40:12 -0700283 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
284 s_id, function, line, &vaf, errno, errstr);
285
286 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
287 function, line, &vaf, errno, errstr);
288 va_end(args);
289 /* Caller calls BUG() */
290}
291
292static void btrfs_put_super(struct super_block *sb)
293{
Googler9398cc32022-12-02 17:21:52 +0800294 close_ctree(btrfs_sb(sb));
Googleraf606d22022-10-26 21:40:12 -0700295}
296
297enum {
Googler9398cc32022-12-02 17:21:52 +0800298 Opt_acl, Opt_noacl,
299 Opt_clear_cache,
300 Opt_commit_interval,
301 Opt_compress,
302 Opt_compress_force,
303 Opt_compress_force_type,
304 Opt_compress_type,
305 Opt_degraded,
306 Opt_device,
307 Opt_fatal_errors,
308 Opt_flushoncommit, Opt_noflushoncommit,
309 Opt_inode_cache, Opt_noinode_cache,
310 Opt_max_inline,
311 Opt_barrier, Opt_nobarrier,
312 Opt_datacow, Opt_nodatacow,
313 Opt_datasum, Opt_nodatasum,
314 Opt_defrag, Opt_nodefrag,
315 Opt_discard, Opt_nodiscard,
316 Opt_nologreplay,
317 Opt_norecovery,
318 Opt_ratio,
319 Opt_rescan_uuid_tree,
320 Opt_skip_balance,
321 Opt_space_cache, Opt_no_space_cache,
322 Opt_space_cache_version,
323 Opt_ssd, Opt_nossd,
324 Opt_ssd_spread, Opt_nossd_spread,
325 Opt_subvol,
326 Opt_subvol_empty,
327 Opt_subvolid,
328 Opt_thread_pool,
329 Opt_treelog, Opt_notreelog,
330 Opt_usebackuproot,
331 Opt_user_subvol_rm_allowed,
332
333 /* Deprecated options */
334 Opt_alloc_start,
335 Opt_recovery,
336 Opt_subvolrootid,
337
338 /* Debugging options */
339 Opt_check_integrity,
Googleraf606d22022-10-26 21:40:12 -0700340 Opt_check_integrity_including_extent_data,
Googler9398cc32022-12-02 17:21:52 +0800341 Opt_check_integrity_print_mask,
342 Opt_enospc_debug, Opt_noenospc_debug,
Googleraf606d22022-10-26 21:40:12 -0700343#ifdef CONFIG_BTRFS_DEBUG
344 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
345#endif
Googler9398cc32022-12-02 17:21:52 +0800346#ifdef CONFIG_BTRFS_FS_REF_VERIFY
347 Opt_ref_verify,
348#endif
Googleraf606d22022-10-26 21:40:12 -0700349 Opt_err,
350};
351
352static const match_table_t tokens = {
Googler9398cc32022-12-02 17:21:52 +0800353 {Opt_acl, "acl"},
354 {Opt_noacl, "noacl"},
355 {Opt_clear_cache, "clear_cache"},
356 {Opt_commit_interval, "commit=%u"},
Googleraf606d22022-10-26 21:40:12 -0700357 {Opt_compress, "compress"},
358 {Opt_compress_type, "compress=%s"},
359 {Opt_compress_force, "compress-force"},
360 {Opt_compress_force_type, "compress-force=%s"},
Googler9398cc32022-12-02 17:21:52 +0800361 {Opt_degraded, "degraded"},
362 {Opt_device, "device=%s"},
363 {Opt_fatal_errors, "fatal_errors=%s"},
Googler9726be62022-12-14 05:53:31 +0000364 {Opt_flushoncommit, "flushoncommit"},
365 {Opt_noflushoncommit, "noflushoncommit"},
Googler9726be62022-12-14 05:53:31 +0000366 {Opt_inode_cache, "inode_cache"},
367 {Opt_noinode_cache, "noinode_cache"},
Googler9398cc32022-12-02 17:21:52 +0800368 {Opt_max_inline, "max_inline=%s"},
369 {Opt_barrier, "barrier"},
370 {Opt_nobarrier, "nobarrier"},
371 {Opt_datacow, "datacow"},
372 {Opt_nodatacow, "nodatacow"},
373 {Opt_datasum, "datasum"},
374 {Opt_nodatasum, "nodatasum"},
375 {Opt_defrag, "autodefrag"},
376 {Opt_nodefrag, "noautodefrag"},
377 {Opt_discard, "discard"},
378 {Opt_nodiscard, "nodiscard"},
379 {Opt_nologreplay, "nologreplay"},
380 {Opt_norecovery, "norecovery"},
381 {Opt_ratio, "metadata_ratio=%u"},
382 {Opt_rescan_uuid_tree, "rescan_uuid_tree"},
Googler9726be62022-12-14 05:53:31 +0000383 {Opt_skip_balance, "skip_balance"},
Googler9398cc32022-12-02 17:21:52 +0800384 {Opt_space_cache, "space_cache"},
385 {Opt_no_space_cache, "nospace_cache"},
386 {Opt_space_cache_version, "space_cache=%s"},
387 {Opt_ssd, "ssd"},
388 {Opt_nossd, "nossd"},
389 {Opt_ssd_spread, "ssd_spread"},
390 {Opt_nossd_spread, "nossd_spread"},
391 {Opt_subvol, "subvol=%s"},
392 {Opt_subvol_empty, "subvol="},
393 {Opt_subvolid, "subvolid=%s"},
394 {Opt_thread_pool, "thread_pool=%u"},
395 {Opt_treelog, "treelog"},
396 {Opt_notreelog, "notreelog"},
397 {Opt_usebackuproot, "usebackuproot"},
398 {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
399
400 /* Deprecated options */
401 {Opt_alloc_start, "alloc_start=%s"},
402 {Opt_recovery, "recovery"},
403 {Opt_subvolrootid, "subvolrootid=%d"},
404
405 /* Debugging options */
Googler9726be62022-12-14 05:53:31 +0000406 {Opt_check_integrity, "check_int"},
407 {Opt_check_integrity_including_extent_data, "check_int_data"},
Googler9398cc32022-12-02 17:21:52 +0800408 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
409 {Opt_enospc_debug, "enospc_debug"},
410 {Opt_noenospc_debug, "noenospc_debug"},
Googleraf606d22022-10-26 21:40:12 -0700411#ifdef CONFIG_BTRFS_DEBUG
412 {Opt_fragment_data, "fragment=data"},
413 {Opt_fragment_metadata, "fragment=metadata"},
414 {Opt_fragment_all, "fragment=all"},
415#endif
Googler9398cc32022-12-02 17:21:52 +0800416#ifdef CONFIG_BTRFS_FS_REF_VERIFY
417 {Opt_ref_verify, "ref_verify"},
418#endif
Googleraf606d22022-10-26 21:40:12 -0700419 {Opt_err, NULL},
420};
421
422/*
423 * Regular mount options parser. Everything that is needed only when
424 * reading in a new superblock is parsed here.
425 * XXX JDM: This needs to be cleaned up for remount.
426 */
Googler9398cc32022-12-02 17:21:52 +0800427int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
Googleraf606d22022-10-26 21:40:12 -0700428 unsigned long new_flags)
429{
430 substring_t args[MAX_OPT_ARGS];
Googler9398cc32022-12-02 17:21:52 +0800431 char *p, *num;
Googleraf606d22022-10-26 21:40:12 -0700432 u64 cache_gen;
433 int intarg;
434 int ret = 0;
435 char *compress_type;
436 bool compress_force = false;
437 enum btrfs_compression_type saved_compress_type;
Googler9398cc32022-12-02 17:21:52 +0800438 int saved_compress_level;
Googleraf606d22022-10-26 21:40:12 -0700439 bool saved_compress_force;
440 int no_compress = 0;
441
Googler9398cc32022-12-02 17:21:52 +0800442 cache_gen = btrfs_super_cache_generation(info->super_copy);
443 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
Googleraf606d22022-10-26 21:40:12 -0700444 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
445 else if (cache_gen)
446 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
447
448 /*
449 * Even the options are empty, we still need to do extra check
450 * against new flags
451 */
452 if (!options)
453 goto check;
454
455 while ((p = strsep(&options, ",")) != NULL) {
456 int token;
457 if (!*p)
458 continue;
459
460 token = match_token(p, tokens, args);
461 switch (token) {
462 case Opt_degraded:
Googler9398cc32022-12-02 17:21:52 +0800463 btrfs_info(info, "allowing degraded mounts");
Googleraf606d22022-10-26 21:40:12 -0700464 btrfs_set_opt(info->mount_opt, DEGRADED);
465 break;
466 case Opt_subvol:
Googler9398cc32022-12-02 17:21:52 +0800467 case Opt_subvol_empty:
Googleraf606d22022-10-26 21:40:12 -0700468 case Opt_subvolid:
469 case Opt_subvolrootid:
470 case Opt_device:
471 /*
Googler9398cc32022-12-02 17:21:52 +0800472 * These are parsed by btrfs_parse_subvol_options or
473 * btrfs_parse_device_options and can be ignored here.
Googleraf606d22022-10-26 21:40:12 -0700474 */
475 break;
476 case Opt_nodatasum:
477 btrfs_set_and_info(info, NODATASUM,
478 "setting nodatasum");
479 break;
480 case Opt_datasum:
481 if (btrfs_test_opt(info, NODATASUM)) {
482 if (btrfs_test_opt(info, NODATACOW))
Googler9398cc32022-12-02 17:21:52 +0800483 btrfs_info(info,
Googleraf606d22022-10-26 21:40:12 -0700484 "setting datasum, datacow enabled");
485 else
Googler9398cc32022-12-02 17:21:52 +0800486 btrfs_info(info, "setting datasum");
Googleraf606d22022-10-26 21:40:12 -0700487 }
488 btrfs_clear_opt(info->mount_opt, NODATACOW);
489 btrfs_clear_opt(info->mount_opt, NODATASUM);
490 break;
491 case Opt_nodatacow:
492 if (!btrfs_test_opt(info, NODATACOW)) {
493 if (!btrfs_test_opt(info, COMPRESS) ||
494 !btrfs_test_opt(info, FORCE_COMPRESS)) {
Googler9398cc32022-12-02 17:21:52 +0800495 btrfs_info(info,
Googleraf606d22022-10-26 21:40:12 -0700496 "setting nodatacow, compression disabled");
497 } else {
Googler9398cc32022-12-02 17:21:52 +0800498 btrfs_info(info, "setting nodatacow");
Googleraf606d22022-10-26 21:40:12 -0700499 }
500 }
501 btrfs_clear_opt(info->mount_opt, COMPRESS);
502 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
503 btrfs_set_opt(info->mount_opt, NODATACOW);
504 btrfs_set_opt(info->mount_opt, NODATASUM);
505 break;
506 case Opt_datacow:
507 btrfs_clear_and_info(info, NODATACOW,
508 "setting datacow");
509 break;
510 case Opt_compress_force:
511 case Opt_compress_force_type:
512 compress_force = true;
513 /* Fallthrough */
514 case Opt_compress:
515 case Opt_compress_type:
516 saved_compress_type = btrfs_test_opt(info,
517 COMPRESS) ?
518 info->compress_type : BTRFS_COMPRESS_NONE;
519 saved_compress_force =
520 btrfs_test_opt(info, FORCE_COMPRESS);
Googler9398cc32022-12-02 17:21:52 +0800521 saved_compress_level = info->compress_level;
Googleraf606d22022-10-26 21:40:12 -0700522 if (token == Opt_compress ||
523 token == Opt_compress_force ||
Googler9398cc32022-12-02 17:21:52 +0800524 strncmp(args[0].from, "zlib", 4) == 0) {
Googleraf606d22022-10-26 21:40:12 -0700525 compress_type = "zlib";
Googler9398cc32022-12-02 17:21:52 +0800526
Googleraf606d22022-10-26 21:40:12 -0700527 info->compress_type = BTRFS_COMPRESS_ZLIB;
Googler9398cc32022-12-02 17:21:52 +0800528 info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL;
529 /*
530 * args[0] contains uninitialized data since
531 * for these tokens we don't expect any
532 * parameter.
533 */
534 if (token != Opt_compress &&
535 token != Opt_compress_force)
536 info->compress_level =
537 btrfs_compress_str2level(
538 BTRFS_COMPRESS_ZLIB,
539 args[0].from + 4);
Googleraf606d22022-10-26 21:40:12 -0700540 btrfs_set_opt(info->mount_opt, COMPRESS);
541 btrfs_clear_opt(info->mount_opt, NODATACOW);
542 btrfs_clear_opt(info->mount_opt, NODATASUM);
543 no_compress = 0;
Googler9398cc32022-12-02 17:21:52 +0800544 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
Googleraf606d22022-10-26 21:40:12 -0700545 compress_type = "lzo";
546 info->compress_type = BTRFS_COMPRESS_LZO;
Googler9398cc32022-12-02 17:21:52 +0800547 info->compress_level = 0;
Googleraf606d22022-10-26 21:40:12 -0700548 btrfs_set_opt(info->mount_opt, COMPRESS);
549 btrfs_clear_opt(info->mount_opt, NODATACOW);
550 btrfs_clear_opt(info->mount_opt, NODATASUM);
551 btrfs_set_fs_incompat(info, COMPRESS_LZO);
552 no_compress = 0;
Googler9398cc32022-12-02 17:21:52 +0800553 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
554 compress_type = "zstd";
555 info->compress_type = BTRFS_COMPRESS_ZSTD;
556 info->compress_level =
557 btrfs_compress_str2level(
558 BTRFS_COMPRESS_ZSTD,
559 args[0].from + 4);
560 btrfs_set_opt(info->mount_opt, COMPRESS);
561 btrfs_clear_opt(info->mount_opt, NODATACOW);
562 btrfs_clear_opt(info->mount_opt, NODATASUM);
563 btrfs_set_fs_incompat(info, COMPRESS_ZSTD);
564 no_compress = 0;
Googleraf606d22022-10-26 21:40:12 -0700565 } else if (strncmp(args[0].from, "no", 2) == 0) {
566 compress_type = "no";
Googler9398cc32022-12-02 17:21:52 +0800567 info->compress_level = 0;
568 info->compress_type = 0;
Googleraf606d22022-10-26 21:40:12 -0700569 btrfs_clear_opt(info->mount_opt, COMPRESS);
570 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
571 compress_force = false;
572 no_compress++;
573 } else {
574 ret = -EINVAL;
575 goto out;
576 }
577
578 if (compress_force) {
579 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
580 } else {
581 /*
582 * If we remount from compress-force=xxx to
583 * compress=xxx, we need clear FORCE_COMPRESS
584 * flag, otherwise, there is no way for users
585 * to disable forcible compression separately.
586 */
587 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
588 }
Googler9398cc32022-12-02 17:21:52 +0800589 if (no_compress == 1) {
590 btrfs_info(info, "use no compression");
591 } else if ((info->compress_type != saved_compress_type) ||
592 (compress_force != saved_compress_force) ||
593 (info->compress_level != saved_compress_level)) {
594 btrfs_info(info, "%s %s compression, level %d",
Googleraf606d22022-10-26 21:40:12 -0700595 (compress_force) ? "force" : "use",
Googler9398cc32022-12-02 17:21:52 +0800596 compress_type, info->compress_level);
Googleraf606d22022-10-26 21:40:12 -0700597 }
598 compress_force = false;
599 break;
600 case Opt_ssd:
601 btrfs_set_and_info(info, SSD,
Googler9398cc32022-12-02 17:21:52 +0800602 "enabling ssd optimizations");
603 btrfs_clear_opt(info->mount_opt, NOSSD);
Googleraf606d22022-10-26 21:40:12 -0700604 break;
605 case Opt_ssd_spread:
Googler9398cc32022-12-02 17:21:52 +0800606 btrfs_set_and_info(info, SSD,
607 "enabling ssd optimizations");
Googleraf606d22022-10-26 21:40:12 -0700608 btrfs_set_and_info(info, SSD_SPREAD,
Googler9398cc32022-12-02 17:21:52 +0800609 "using spread ssd allocation scheme");
610 btrfs_clear_opt(info->mount_opt, NOSSD);
Googleraf606d22022-10-26 21:40:12 -0700611 break;
612 case Opt_nossd:
Googler9398cc32022-12-02 17:21:52 +0800613 btrfs_set_opt(info->mount_opt, NOSSD);
614 btrfs_clear_and_info(info, SSD,
615 "not using ssd optimizations");
616 /* Fallthrough */
617 case Opt_nossd_spread:
618 btrfs_clear_and_info(info, SSD_SPREAD,
619 "not using spread ssd allocation scheme");
Googleraf606d22022-10-26 21:40:12 -0700620 break;
621 case Opt_barrier:
622 btrfs_clear_and_info(info, NOBARRIER,
623 "turning on barriers");
624 break;
625 case Opt_nobarrier:
626 btrfs_set_and_info(info, NOBARRIER,
627 "turning off barriers");
628 break;
629 case Opt_thread_pool:
630 ret = match_int(&args[0], &intarg);
631 if (ret) {
632 goto out;
Googler9398cc32022-12-02 17:21:52 +0800633 } else if (intarg == 0) {
Googleraf606d22022-10-26 21:40:12 -0700634 ret = -EINVAL;
635 goto out;
636 }
Googler9398cc32022-12-02 17:21:52 +0800637 info->thread_pool_size = intarg;
Googleraf606d22022-10-26 21:40:12 -0700638 break;
639 case Opt_max_inline:
640 num = match_strdup(&args[0]);
641 if (num) {
642 info->max_inline = memparse(num, NULL);
643 kfree(num);
644
645 if (info->max_inline) {
646 info->max_inline = min_t(u64,
647 info->max_inline,
Googler9398cc32022-12-02 17:21:52 +0800648 info->sectorsize);
Googleraf606d22022-10-26 21:40:12 -0700649 }
Googler9398cc32022-12-02 17:21:52 +0800650 btrfs_info(info, "max_inline at %llu",
651 info->max_inline);
Googleraf606d22022-10-26 21:40:12 -0700652 } else {
653 ret = -ENOMEM;
654 goto out;
655 }
656 break;
657 case Opt_alloc_start:
Googler9398cc32022-12-02 17:21:52 +0800658 btrfs_info(info,
659 "option alloc_start is obsolete, ignored");
Googleraf606d22022-10-26 21:40:12 -0700660 break;
661 case Opt_acl:
662#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Googler9398cc32022-12-02 17:21:52 +0800663 info->sb->s_flags |= SB_POSIXACL;
Googleraf606d22022-10-26 21:40:12 -0700664 break;
665#else
Googler9398cc32022-12-02 17:21:52 +0800666 btrfs_err(info, "support for ACL not compiled in!");
Googleraf606d22022-10-26 21:40:12 -0700667 ret = -EINVAL;
668 goto out;
669#endif
670 case Opt_noacl:
Googler9398cc32022-12-02 17:21:52 +0800671 info->sb->s_flags &= ~SB_POSIXACL;
Googleraf606d22022-10-26 21:40:12 -0700672 break;
673 case Opt_notreelog:
674 btrfs_set_and_info(info, NOTREELOG,
675 "disabling tree log");
676 break;
677 case Opt_treelog:
678 btrfs_clear_and_info(info, NOTREELOG,
679 "enabling tree log");
680 break;
681 case Opt_norecovery:
682 case Opt_nologreplay:
683 btrfs_set_and_info(info, NOLOGREPLAY,
684 "disabling log replay at mount time");
685 break;
686 case Opt_flushoncommit:
687 btrfs_set_and_info(info, FLUSHONCOMMIT,
688 "turning on flush-on-commit");
689 break;
690 case Opt_noflushoncommit:
691 btrfs_clear_and_info(info, FLUSHONCOMMIT,
692 "turning off flush-on-commit");
693 break;
694 case Opt_ratio:
695 ret = match_int(&args[0], &intarg);
Googler9398cc32022-12-02 17:21:52 +0800696 if (ret)
Googleraf606d22022-10-26 21:40:12 -0700697 goto out;
Googler9398cc32022-12-02 17:21:52 +0800698 info->metadata_ratio = intarg;
699 btrfs_info(info, "metadata ratio %u",
700 info->metadata_ratio);
Googleraf606d22022-10-26 21:40:12 -0700701 break;
702 case Opt_discard:
703 btrfs_set_and_info(info, DISCARD,
704 "turning on discard");
705 break;
706 case Opt_nodiscard:
707 btrfs_clear_and_info(info, DISCARD,
708 "turning off discard");
709 break;
710 case Opt_space_cache:
711 case Opt_space_cache_version:
712 if (token == Opt_space_cache ||
713 strcmp(args[0].from, "v1") == 0) {
Googler9398cc32022-12-02 17:21:52 +0800714 btrfs_clear_opt(info->mount_opt,
Googleraf606d22022-10-26 21:40:12 -0700715 FREE_SPACE_TREE);
716 btrfs_set_and_info(info, SPACE_CACHE,
Googler9398cc32022-12-02 17:21:52 +0800717 "enabling disk space caching");
Googleraf606d22022-10-26 21:40:12 -0700718 } else if (strcmp(args[0].from, "v2") == 0) {
Googler9398cc32022-12-02 17:21:52 +0800719 btrfs_clear_opt(info->mount_opt,
Googleraf606d22022-10-26 21:40:12 -0700720 SPACE_CACHE);
Googler9398cc32022-12-02 17:21:52 +0800721 btrfs_set_and_info(info, FREE_SPACE_TREE,
Googleraf606d22022-10-26 21:40:12 -0700722 "enabling free space tree");
723 } else {
724 ret = -EINVAL;
725 goto out;
726 }
727 break;
728 case Opt_rescan_uuid_tree:
729 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
730 break;
731 case Opt_no_space_cache:
732 if (btrfs_test_opt(info, SPACE_CACHE)) {
Googler9398cc32022-12-02 17:21:52 +0800733 btrfs_clear_and_info(info, SPACE_CACHE,
734 "disabling disk space caching");
Googleraf606d22022-10-26 21:40:12 -0700735 }
736 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
Googler9398cc32022-12-02 17:21:52 +0800737 btrfs_clear_and_info(info, FREE_SPACE_TREE,
738 "disabling free space tree");
Googleraf606d22022-10-26 21:40:12 -0700739 }
740 break;
741 case Opt_inode_cache:
742 btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
743 "enabling inode map caching");
744 break;
745 case Opt_noinode_cache:
746 btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
747 "disabling inode map caching");
748 break;
749 case Opt_clear_cache:
750 btrfs_set_and_info(info, CLEAR_CACHE,
751 "force clearing of disk cache");
752 break;
753 case Opt_user_subvol_rm_allowed:
754 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
755 break;
756 case Opt_enospc_debug:
757 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
758 break;
759 case Opt_noenospc_debug:
760 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
761 break;
762 case Opt_defrag:
763 btrfs_set_and_info(info, AUTO_DEFRAG,
764 "enabling auto defrag");
765 break;
766 case Opt_nodefrag:
767 btrfs_clear_and_info(info, AUTO_DEFRAG,
768 "disabling auto defrag");
769 break;
770 case Opt_recovery:
Googler9398cc32022-12-02 17:21:52 +0800771 btrfs_warn(info,
Googleraf606d22022-10-26 21:40:12 -0700772 "'recovery' is deprecated, use 'usebackuproot' instead");
Googler9398cc32022-12-02 17:21:52 +0800773 /* fall through */
Googleraf606d22022-10-26 21:40:12 -0700774 case Opt_usebackuproot:
Googler9398cc32022-12-02 17:21:52 +0800775 btrfs_info(info,
Googleraf606d22022-10-26 21:40:12 -0700776 "trying to use backup root at mount time");
777 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
778 break;
779 case Opt_skip_balance:
780 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
781 break;
782#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
783 case Opt_check_integrity_including_extent_data:
Googler9398cc32022-12-02 17:21:52 +0800784 btrfs_info(info,
Googleraf606d22022-10-26 21:40:12 -0700785 "enabling check integrity including extent data");
786 btrfs_set_opt(info->mount_opt,
787 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
788 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
789 break;
790 case Opt_check_integrity:
Googler9398cc32022-12-02 17:21:52 +0800791 btrfs_info(info, "enabling check integrity");
Googleraf606d22022-10-26 21:40:12 -0700792 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
793 break;
794 case Opt_check_integrity_print_mask:
795 ret = match_int(&args[0], &intarg);
Googler9398cc32022-12-02 17:21:52 +0800796 if (ret)
Googleraf606d22022-10-26 21:40:12 -0700797 goto out;
Googler9398cc32022-12-02 17:21:52 +0800798 info->check_integrity_print_mask = intarg;
799 btrfs_info(info, "check_integrity_print_mask 0x%x",
800 info->check_integrity_print_mask);
Googleraf606d22022-10-26 21:40:12 -0700801 break;
802#else
803 case Opt_check_integrity_including_extent_data:
804 case Opt_check_integrity:
805 case Opt_check_integrity_print_mask:
Googler9398cc32022-12-02 17:21:52 +0800806 btrfs_err(info,
807 "support for check_integrity* not compiled in!");
Googleraf606d22022-10-26 21:40:12 -0700808 ret = -EINVAL;
809 goto out;
810#endif
811 case Opt_fatal_errors:
812 if (strcmp(args[0].from, "panic") == 0)
813 btrfs_set_opt(info->mount_opt,
814 PANIC_ON_FATAL_ERROR);
815 else if (strcmp(args[0].from, "bug") == 0)
816 btrfs_clear_opt(info->mount_opt,
817 PANIC_ON_FATAL_ERROR);
818 else {
819 ret = -EINVAL;
820 goto out;
821 }
822 break;
823 case Opt_commit_interval:
824 intarg = 0;
825 ret = match_int(&args[0], &intarg);
Googler9398cc32022-12-02 17:21:52 +0800826 if (ret)
Googleraf606d22022-10-26 21:40:12 -0700827 goto out;
Googler9398cc32022-12-02 17:21:52 +0800828 if (intarg == 0) {
829 btrfs_info(info,
830 "using default commit interval %us",
Googler9726be62022-12-14 05:53:31 +0000831 BTRFS_DEFAULT_COMMIT_INTERVAL);
Googler9398cc32022-12-02 17:21:52 +0800832 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
833 } else if (intarg > 300) {
834 btrfs_warn(info, "excessive commit interval %d",
835 intarg);
Googler9726be62022-12-14 05:53:31 +0000836 }
Googler9398cc32022-12-02 17:21:52 +0800837 info->commit_interval = intarg;
Googleraf606d22022-10-26 21:40:12 -0700838 break;
839#ifdef CONFIG_BTRFS_DEBUG
840 case Opt_fragment_all:
Googler9398cc32022-12-02 17:21:52 +0800841 btrfs_info(info, "fragmenting all space");
Googleraf606d22022-10-26 21:40:12 -0700842 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
843 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
844 break;
845 case Opt_fragment_metadata:
Googler9398cc32022-12-02 17:21:52 +0800846 btrfs_info(info, "fragmenting metadata");
Googleraf606d22022-10-26 21:40:12 -0700847 btrfs_set_opt(info->mount_opt,
848 FRAGMENT_METADATA);
849 break;
850 case Opt_fragment_data:
Googler9398cc32022-12-02 17:21:52 +0800851 btrfs_info(info, "fragmenting data");
Googleraf606d22022-10-26 21:40:12 -0700852 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
853 break;
854#endif
Googler9398cc32022-12-02 17:21:52 +0800855#ifdef CONFIG_BTRFS_FS_REF_VERIFY
856 case Opt_ref_verify:
857 btrfs_info(info, "doing ref verification");
858 btrfs_set_opt(info->mount_opt, REF_VERIFY);
859 break;
860#endif
Googleraf606d22022-10-26 21:40:12 -0700861 case Opt_err:
Googler9398cc32022-12-02 17:21:52 +0800862 btrfs_info(info, "unrecognized mount option '%s'", p);
Googleraf606d22022-10-26 21:40:12 -0700863 ret = -EINVAL;
864 goto out;
865 default:
866 break;
867 }
868 }
869check:
870 /*
871 * Extra check for current option against current flag
872 */
Googler9398cc32022-12-02 17:21:52 +0800873 if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
874 btrfs_err(info,
Googleraf606d22022-10-26 21:40:12 -0700875 "nologreplay must be used with ro mount option");
876 ret = -EINVAL;
877 }
878out:
Googler9398cc32022-12-02 17:21:52 +0800879 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
Googleraf606d22022-10-26 21:40:12 -0700880 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
881 !btrfs_test_opt(info, CLEAR_CACHE)) {
Googler9398cc32022-12-02 17:21:52 +0800882 btrfs_err(info, "cannot disable free space tree");
Googleraf606d22022-10-26 21:40:12 -0700883 ret = -EINVAL;
884
885 }
886 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
Googler9398cc32022-12-02 17:21:52 +0800887 btrfs_info(info, "disk space caching is enabled");
Googleraf606d22022-10-26 21:40:12 -0700888 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
Googler9398cc32022-12-02 17:21:52 +0800889 btrfs_info(info, "using free space tree");
Googleraf606d22022-10-26 21:40:12 -0700890 return ret;
891}
892
893/*
894 * Parse mount options that are required early in the mount process.
895 *
896 * All other options will be parsed on much later in the mount process and
897 * only when we need to allocate a new super block.
898 */
Googler9398cc32022-12-02 17:21:52 +0800899static int btrfs_parse_device_options(const char *options, fmode_t flags,
900 void *holder)
Googleraf606d22022-10-26 21:40:12 -0700901{
902 substring_t args[MAX_OPT_ARGS];
903 char *device_name, *opts, *orig, *p;
Googler9398cc32022-12-02 17:21:52 +0800904 struct btrfs_device *device = NULL;
Googleraf606d22022-10-26 21:40:12 -0700905 int error = 0;
906
Googler9398cc32022-12-02 17:21:52 +0800907 lockdep_assert_held(&uuid_mutex);
908
Googleraf606d22022-10-26 21:40:12 -0700909 if (!options)
910 return 0;
911
912 /*
Googler9398cc32022-12-02 17:21:52 +0800913 * strsep changes the string, duplicate it because btrfs_parse_options
914 * gets called later
915 */
916 opts = kstrdup(options, GFP_KERNEL);
917 if (!opts)
918 return -ENOMEM;
919 orig = opts;
920
921 while ((p = strsep(&opts, ",")) != NULL) {
922 int token;
923
924 if (!*p)
925 continue;
926
927 token = match_token(p, tokens, args);
928 if (token == Opt_device) {
929 device_name = match_strdup(&args[0]);
930 if (!device_name) {
931 error = -ENOMEM;
932 goto out;
933 }
934 device = btrfs_scan_one_device(device_name, flags,
935 holder);
936 kfree(device_name);
937 if (IS_ERR(device)) {
938 error = PTR_ERR(device);
939 goto out;
940 }
941 }
942 }
943
944out:
945 kfree(orig);
946 return error;
947}
948
949/*
950 * Parse mount options that are related to subvolume id
951 *
952 * The value is later passed to mount_subvol()
953 */
954static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
955 u64 *subvol_objectid)
956{
957 substring_t args[MAX_OPT_ARGS];
958 char *opts, *orig, *p;
959 int error = 0;
960 u64 subvolid;
961
962 if (!options)
963 return 0;
964
965 /*
966 * strsep changes the string, duplicate it because
967 * btrfs_parse_device_options gets called later
Googleraf606d22022-10-26 21:40:12 -0700968 */
969 opts = kstrdup(options, GFP_KERNEL);
970 if (!opts)
971 return -ENOMEM;
972 orig = opts;
973
974 while ((p = strsep(&opts, ",")) != NULL) {
975 int token;
976 if (!*p)
977 continue;
978
979 token = match_token(p, tokens, args);
980 switch (token) {
981 case Opt_subvol:
982 kfree(*subvol_name);
983 *subvol_name = match_strdup(&args[0]);
984 if (!*subvol_name) {
985 error = -ENOMEM;
986 goto out;
987 }
988 break;
989 case Opt_subvolid:
Googler9398cc32022-12-02 17:21:52 +0800990 error = match_u64(&args[0], &subvolid);
991 if (error)
Googleraf606d22022-10-26 21:40:12 -0700992 goto out;
Googler9398cc32022-12-02 17:21:52 +0800993
994 /* we want the original fs_tree */
995 if (subvolid == 0)
996 subvolid = BTRFS_FS_TREE_OBJECTID;
997
998 *subvol_objectid = subvolid;
Googleraf606d22022-10-26 21:40:12 -0700999 break;
1000 case Opt_subvolrootid:
1001 pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
1002 break;
1003 default:
1004 break;
1005 }
1006 }
1007
1008out:
1009 kfree(orig);
1010 return error;
1011}
1012
1013char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1014 u64 subvol_objectid)
1015{
1016 struct btrfs_root *root = fs_info->tree_root;
1017 struct btrfs_root *fs_root;
1018 struct btrfs_root_ref *root_ref;
1019 struct btrfs_inode_ref *inode_ref;
1020 struct btrfs_key key;
1021 struct btrfs_path *path = NULL;
1022 char *name = NULL, *ptr;
1023 u64 dirid;
1024 int len;
1025 int ret;
1026
1027 path = btrfs_alloc_path();
1028 if (!path) {
1029 ret = -ENOMEM;
1030 goto err;
1031 }
1032 path->leave_spinning = 1;
1033
Googler9398cc32022-12-02 17:21:52 +08001034 name = kmalloc(PATH_MAX, GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07001035 if (!name) {
1036 ret = -ENOMEM;
1037 goto err;
1038 }
1039 ptr = name + PATH_MAX - 1;
1040 ptr[0] = '\0';
1041
1042 /*
1043 * Walk up the subvolume trees in the tree of tree roots by root
1044 * backrefs until we hit the top-level subvolume.
1045 */
1046 while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) {
1047 key.objectid = subvol_objectid;
1048 key.type = BTRFS_ROOT_BACKREF_KEY;
1049 key.offset = (u64)-1;
1050
1051 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1052 if (ret < 0) {
1053 goto err;
1054 } else if (ret > 0) {
1055 ret = btrfs_previous_item(root, path, subvol_objectid,
1056 BTRFS_ROOT_BACKREF_KEY);
1057 if (ret < 0) {
1058 goto err;
1059 } else if (ret > 0) {
1060 ret = -ENOENT;
1061 goto err;
1062 }
1063 }
1064
1065 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1066 subvol_objectid = key.offset;
1067
1068 root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1069 struct btrfs_root_ref);
1070 len = btrfs_root_ref_name_len(path->nodes[0], root_ref);
1071 ptr -= len + 1;
1072 if (ptr < name) {
1073 ret = -ENAMETOOLONG;
1074 goto err;
1075 }
1076 read_extent_buffer(path->nodes[0], ptr + 1,
1077 (unsigned long)(root_ref + 1), len);
1078 ptr[0] = '/';
1079 dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref);
1080 btrfs_release_path(path);
1081
1082 key.objectid = subvol_objectid;
1083 key.type = BTRFS_ROOT_ITEM_KEY;
1084 key.offset = (u64)-1;
1085 fs_root = btrfs_read_fs_root_no_name(fs_info, &key);
1086 if (IS_ERR(fs_root)) {
1087 ret = PTR_ERR(fs_root);
1088 goto err;
1089 }
1090
1091 /*
1092 * Walk up the filesystem tree by inode refs until we hit the
1093 * root directory.
1094 */
1095 while (dirid != BTRFS_FIRST_FREE_OBJECTID) {
1096 key.objectid = dirid;
1097 key.type = BTRFS_INODE_REF_KEY;
1098 key.offset = (u64)-1;
1099
1100 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
1101 if (ret < 0) {
1102 goto err;
1103 } else if (ret > 0) {
1104 ret = btrfs_previous_item(fs_root, path, dirid,
1105 BTRFS_INODE_REF_KEY);
1106 if (ret < 0) {
1107 goto err;
1108 } else if (ret > 0) {
1109 ret = -ENOENT;
1110 goto err;
1111 }
1112 }
1113
1114 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1115 dirid = key.offset;
1116
1117 inode_ref = btrfs_item_ptr(path->nodes[0],
1118 path->slots[0],
1119 struct btrfs_inode_ref);
1120 len = btrfs_inode_ref_name_len(path->nodes[0],
1121 inode_ref);
1122 ptr -= len + 1;
1123 if (ptr < name) {
1124 ret = -ENAMETOOLONG;
1125 goto err;
1126 }
1127 read_extent_buffer(path->nodes[0], ptr + 1,
1128 (unsigned long)(inode_ref + 1), len);
1129 ptr[0] = '/';
1130 btrfs_release_path(path);
1131 }
1132 }
1133
1134 btrfs_free_path(path);
1135 if (ptr == name + PATH_MAX - 1) {
1136 name[0] = '/';
1137 name[1] = '\0';
1138 } else {
1139 memmove(name, ptr, name + PATH_MAX - ptr);
1140 }
1141 return name;
1142
1143err:
1144 btrfs_free_path(path);
1145 kfree(name);
1146 return ERR_PTR(ret);
1147}
1148
1149static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid)
1150{
1151 struct btrfs_root *root = fs_info->tree_root;
1152 struct btrfs_dir_item *di;
1153 struct btrfs_path *path;
1154 struct btrfs_key location;
1155 u64 dir_id;
1156
1157 path = btrfs_alloc_path();
1158 if (!path)
1159 return -ENOMEM;
1160 path->leave_spinning = 1;
1161
1162 /*
1163 * Find the "default" dir item which points to the root item that we
1164 * will mount by default if we haven't been given a specific subvolume
1165 * to mount.
1166 */
1167 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1168 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
1169 if (IS_ERR(di)) {
1170 btrfs_free_path(path);
1171 return PTR_ERR(di);
1172 }
1173 if (!di) {
1174 /*
1175 * Ok the default dir item isn't there. This is weird since
1176 * it's always been there, but don't freak out, just try and
1177 * mount the top-level subvolume.
1178 */
1179 btrfs_free_path(path);
1180 *objectid = BTRFS_FS_TREE_OBJECTID;
1181 return 0;
1182 }
1183
1184 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1185 btrfs_free_path(path);
1186 *objectid = location.objectid;
1187 return 0;
1188}
1189
1190static int btrfs_fill_super(struct super_block *sb,
1191 struct btrfs_fs_devices *fs_devices,
Googler9398cc32022-12-02 17:21:52 +08001192 void *data)
Googleraf606d22022-10-26 21:40:12 -07001193{
1194 struct inode *inode;
1195 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1196 struct btrfs_key key;
1197 int err;
1198
1199 sb->s_maxbytes = MAX_LFS_FILESIZE;
1200 sb->s_magic = BTRFS_SUPER_MAGIC;
1201 sb->s_op = &btrfs_super_ops;
1202 sb->s_d_op = &btrfs_dentry_operations;
1203 sb->s_export_op = &btrfs_export_ops;
1204 sb->s_xattr = btrfs_xattr_handlers;
1205 sb->s_time_gran = 1;
1206#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Googler9398cc32022-12-02 17:21:52 +08001207 sb->s_flags |= SB_POSIXACL;
Googleraf606d22022-10-26 21:40:12 -07001208#endif
Googler9398cc32022-12-02 17:21:52 +08001209 sb->s_flags |= SB_I_VERSION;
Googleraf606d22022-10-26 21:40:12 -07001210 sb->s_iflags |= SB_I_CGROUPWB;
Googler9398cc32022-12-02 17:21:52 +08001211
1212 err = super_setup_bdi(sb);
1213 if (err) {
1214 btrfs_err(fs_info, "super_setup_bdi failed");
1215 return err;
1216 }
1217
Googleraf606d22022-10-26 21:40:12 -07001218 err = open_ctree(sb, fs_devices, (char *)data);
1219 if (err) {
1220 btrfs_err(fs_info, "open_ctree failed");
1221 return err;
1222 }
1223
1224 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1225 key.type = BTRFS_INODE_ITEM_KEY;
1226 key.offset = 0;
1227 inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
1228 if (IS_ERR(inode)) {
1229 err = PTR_ERR(inode);
1230 goto fail_close;
1231 }
1232
1233 sb->s_root = d_make_root(inode);
1234 if (!sb->s_root) {
1235 err = -ENOMEM;
1236 goto fail_close;
1237 }
1238
1239 cleancache_init_fs(sb);
Googler9398cc32022-12-02 17:21:52 +08001240 sb->s_flags |= SB_ACTIVE;
Googleraf606d22022-10-26 21:40:12 -07001241 return 0;
1242
1243fail_close:
Googler9398cc32022-12-02 17:21:52 +08001244 close_ctree(fs_info);
Googleraf606d22022-10-26 21:40:12 -07001245 return err;
1246}
1247
1248int btrfs_sync_fs(struct super_block *sb, int wait)
1249{
1250 struct btrfs_trans_handle *trans;
1251 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1252 struct btrfs_root *root = fs_info->tree_root;
1253
1254 trace_btrfs_sync_fs(fs_info, wait);
1255
1256 if (!wait) {
1257 filemap_flush(fs_info->btree_inode->i_mapping);
1258 return 0;
1259 }
1260
Googler9398cc32022-12-02 17:21:52 +08001261 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Googleraf606d22022-10-26 21:40:12 -07001262
1263 trans = btrfs_attach_transaction_barrier(root);
1264 if (IS_ERR(trans)) {
1265 /* no transaction, don't bother */
1266 if (PTR_ERR(trans) == -ENOENT) {
1267 /*
1268 * Exit unless we have some pending changes
1269 * that need to go through commit
1270 */
1271 if (fs_info->pending_changes == 0)
1272 return 0;
1273 /*
1274 * A non-blocking test if the fs is frozen. We must not
1275 * start a new transaction here otherwise a deadlock
1276 * happens. The pending operations are delayed to the
1277 * next commit after thawing.
1278 */
Googler9398cc32022-12-02 17:21:52 +08001279 if (sb_start_write_trylock(sb))
1280 sb_end_write(sb);
Googleraf606d22022-10-26 21:40:12 -07001281 else
1282 return 0;
1283 trans = btrfs_start_transaction(root, 0);
1284 }
1285 if (IS_ERR(trans))
1286 return PTR_ERR(trans);
1287 }
Googler9398cc32022-12-02 17:21:52 +08001288 return btrfs_commit_transaction(trans);
Googleraf606d22022-10-26 21:40:12 -07001289}
1290
1291static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1292{
1293 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
Googler9398cc32022-12-02 17:21:52 +08001294 const char *compress_type;
Googleraf606d22022-10-26 21:40:12 -07001295 const char *subvol_name;
1296
1297 if (btrfs_test_opt(info, DEGRADED))
1298 seq_puts(seq, ",degraded");
1299 if (btrfs_test_opt(info, NODATASUM))
1300 seq_puts(seq, ",nodatasum");
1301 if (btrfs_test_opt(info, NODATACOW))
1302 seq_puts(seq, ",nodatacow");
1303 if (btrfs_test_opt(info, NOBARRIER))
1304 seq_puts(seq, ",nobarrier");
1305 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
1306 seq_printf(seq, ",max_inline=%llu", info->max_inline);
1307 if (info->thread_pool_size != min_t(unsigned long,
1308 num_online_cpus() + 2, 8))
Googler9398cc32022-12-02 17:21:52 +08001309 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
Googleraf606d22022-10-26 21:40:12 -07001310 if (btrfs_test_opt(info, COMPRESS)) {
Googler9398cc32022-12-02 17:21:52 +08001311 compress_type = btrfs_compress_type2str(info->compress_type);
Googleraf606d22022-10-26 21:40:12 -07001312 if (btrfs_test_opt(info, FORCE_COMPRESS))
1313 seq_printf(seq, ",compress-force=%s", compress_type);
1314 else
1315 seq_printf(seq, ",compress=%s", compress_type);
Googler9398cc32022-12-02 17:21:52 +08001316 if (info->compress_level)
1317 seq_printf(seq, ":%d", info->compress_level);
Googleraf606d22022-10-26 21:40:12 -07001318 }
1319 if (btrfs_test_opt(info, NOSSD))
1320 seq_puts(seq, ",nossd");
1321 if (btrfs_test_opt(info, SSD_SPREAD))
1322 seq_puts(seq, ",ssd_spread");
1323 else if (btrfs_test_opt(info, SSD))
1324 seq_puts(seq, ",ssd");
1325 if (btrfs_test_opt(info, NOTREELOG))
1326 seq_puts(seq, ",notreelog");
1327 if (btrfs_test_opt(info, NOLOGREPLAY))
1328 seq_puts(seq, ",nologreplay");
1329 if (btrfs_test_opt(info, FLUSHONCOMMIT))
1330 seq_puts(seq, ",flushoncommit");
1331 if (btrfs_test_opt(info, DISCARD))
1332 seq_puts(seq, ",discard");
Googler9398cc32022-12-02 17:21:52 +08001333 if (!(info->sb->s_flags & SB_POSIXACL))
Googleraf606d22022-10-26 21:40:12 -07001334 seq_puts(seq, ",noacl");
1335 if (btrfs_test_opt(info, SPACE_CACHE))
1336 seq_puts(seq, ",space_cache");
1337 else if (btrfs_test_opt(info, FREE_SPACE_TREE))
1338 seq_puts(seq, ",space_cache=v2");
1339 else
1340 seq_puts(seq, ",nospace_cache");
1341 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
1342 seq_puts(seq, ",rescan_uuid_tree");
1343 if (btrfs_test_opt(info, CLEAR_CACHE))
1344 seq_puts(seq, ",clear_cache");
1345 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
1346 seq_puts(seq, ",user_subvol_rm_allowed");
1347 if (btrfs_test_opt(info, ENOSPC_DEBUG))
1348 seq_puts(seq, ",enospc_debug");
1349 if (btrfs_test_opt(info, AUTO_DEFRAG))
1350 seq_puts(seq, ",autodefrag");
1351 if (btrfs_test_opt(info, INODE_MAP_CACHE))
1352 seq_puts(seq, ",inode_cache");
1353 if (btrfs_test_opt(info, SKIP_BALANCE))
1354 seq_puts(seq, ",skip_balance");
1355#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
1356 if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
1357 seq_puts(seq, ",check_int_data");
1358 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
1359 seq_puts(seq, ",check_int");
1360 if (info->check_integrity_print_mask)
1361 seq_printf(seq, ",check_int_print_mask=%d",
1362 info->check_integrity_print_mask);
1363#endif
1364 if (info->metadata_ratio)
Googler9398cc32022-12-02 17:21:52 +08001365 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
Googleraf606d22022-10-26 21:40:12 -07001366 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
1367 seq_puts(seq, ",fatal_errors=panic");
1368 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
Googler9398cc32022-12-02 17:21:52 +08001369 seq_printf(seq, ",commit=%u", info->commit_interval);
Googleraf606d22022-10-26 21:40:12 -07001370#ifdef CONFIG_BTRFS_DEBUG
1371 if (btrfs_test_opt(info, FRAGMENT_DATA))
1372 seq_puts(seq, ",fragment=data");
1373 if (btrfs_test_opt(info, FRAGMENT_METADATA))
1374 seq_puts(seq, ",fragment=metadata");
1375#endif
Googler9398cc32022-12-02 17:21:52 +08001376 if (btrfs_test_opt(info, REF_VERIFY))
1377 seq_puts(seq, ",ref_verify");
Googleraf606d22022-10-26 21:40:12 -07001378 seq_printf(seq, ",subvolid=%llu",
1379 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1380 subvol_name = btrfs_get_subvol_name_from_objectid(info,
1381 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
1382 if (!IS_ERR(subvol_name)) {
1383 seq_puts(seq, ",subvol=");
1384 seq_escape(seq, subvol_name, " \t\n\\");
1385 kfree(subvol_name);
1386 }
1387 return 0;
1388}
1389
1390static int btrfs_test_super(struct super_block *s, void *data)
1391{
1392 struct btrfs_fs_info *p = data;
1393 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1394
1395 return fs_info->fs_devices == p->fs_devices;
1396}
1397
1398static int btrfs_set_super(struct super_block *s, void *data)
1399{
1400 int err = set_anon_super(s, data);
1401 if (!err)
1402 s->s_fs_info = data;
1403 return err;
1404}
1405
1406/*
1407 * subvolumes are identified by ino 256
1408 */
1409static inline int is_subvolume_inode(struct inode *inode)
1410{
1411 if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
1412 return 1;
1413 return 0;
1414}
1415
1416static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
Googler9398cc32022-12-02 17:21:52 +08001417 struct vfsmount *mnt)
Googleraf606d22022-10-26 21:40:12 -07001418{
1419 struct dentry *root;
1420 int ret;
1421
1422 if (!subvol_name) {
1423 if (!subvol_objectid) {
1424 ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb),
1425 &subvol_objectid);
1426 if (ret) {
1427 root = ERR_PTR(ret);
1428 goto out;
1429 }
1430 }
1431 subvol_name = btrfs_get_subvol_name_from_objectid(
1432 btrfs_sb(mnt->mnt_sb), subvol_objectid);
1433 if (IS_ERR(subvol_name)) {
1434 root = ERR_CAST(subvol_name);
1435 subvol_name = NULL;
1436 goto out;
1437 }
1438
1439 }
1440
1441 root = mount_subtree(mnt, subvol_name);
1442 /* mount_subtree() drops our reference on the vfsmount. */
1443 mnt = NULL;
1444
1445 if (!IS_ERR(root)) {
1446 struct super_block *s = root->d_sb;
1447 struct btrfs_fs_info *fs_info = btrfs_sb(s);
1448 struct inode *root_inode = d_inode(root);
1449 u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid;
1450
1451 ret = 0;
1452 if (!is_subvolume_inode(root_inode)) {
1453 btrfs_err(fs_info, "'%s' is not a valid subvolume",
1454 subvol_name);
1455 ret = -EINVAL;
1456 }
1457 if (subvol_objectid && root_objectid != subvol_objectid) {
1458 /*
1459 * This will also catch a race condition where a
1460 * subvolume which was passed by ID is renamed and
1461 * another subvolume is renamed over the old location.
1462 */
1463 btrfs_err(fs_info,
1464 "subvol '%s' does not match subvolid %llu",
1465 subvol_name, subvol_objectid);
1466 ret = -EINVAL;
1467 }
1468 if (ret) {
1469 dput(root);
1470 root = ERR_PTR(ret);
1471 deactivate_locked_super(s);
1472 }
1473 }
1474
1475out:
1476 mntput(mnt);
1477 kfree(subvol_name);
1478 return root;
1479}
1480
Googleraf606d22022-10-26 21:40:12 -07001481/*
1482 * Find a superblock for the given device / mount point.
1483 *
Googler9398cc32022-12-02 17:21:52 +08001484 * Note: This is based on mount_bdev from fs/super.c with a few additions
1485 * for multiple device setup. Make sure to keep it in sync.
Googleraf606d22022-10-26 21:40:12 -07001486 */
Googler9398cc32022-12-02 17:21:52 +08001487static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1488 int flags, const char *device_name, void *data)
Googleraf606d22022-10-26 21:40:12 -07001489{
1490 struct block_device *bdev = NULL;
1491 struct super_block *s;
Googler9398cc32022-12-02 17:21:52 +08001492 struct btrfs_device *device = NULL;
Googleraf606d22022-10-26 21:40:12 -07001493 struct btrfs_fs_devices *fs_devices = NULL;
1494 struct btrfs_fs_info *fs_info = NULL;
Googler9398cc32022-12-02 17:21:52 +08001495 void *new_sec_opts = NULL;
Googleraf606d22022-10-26 21:40:12 -07001496 fmode_t mode = FMODE_READ;
1497 int error = 0;
1498
Googler9398cc32022-12-02 17:21:52 +08001499 if (!(flags & SB_RDONLY))
Googleraf606d22022-10-26 21:40:12 -07001500 mode |= FMODE_WRITE;
1501
Googleraf606d22022-10-26 21:40:12 -07001502 if (data) {
Googler9398cc32022-12-02 17:21:52 +08001503 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
Googleraf606d22022-10-26 21:40:12 -07001504 if (error)
1505 return ERR_PTR(error);
1506 }
1507
1508 /*
1509 * Setup a dummy root and fs_info for test/set super. This is because
1510 * we don't actually fill this stuff out until open_ctree, but we need
1511 * it for searching for existing supers, so this lets us do that and
1512 * then open_ctree will properly initialize everything later.
1513 */
Googler9398cc32022-12-02 17:21:52 +08001514 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07001515 if (!fs_info) {
1516 error = -ENOMEM;
1517 goto error_sec_opts;
1518 }
1519
Googler9398cc32022-12-02 17:21:52 +08001520 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1521 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07001522 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1523 error = -ENOMEM;
1524 goto error_fs_info;
1525 }
1526
Googler9398cc32022-12-02 17:21:52 +08001527 mutex_lock(&uuid_mutex);
1528 error = btrfs_parse_device_options(data, mode, fs_type);
1529 if (error) {
1530 mutex_unlock(&uuid_mutex);
1531 goto error_fs_info;
1532 }
1533
1534 device = btrfs_scan_one_device(device_name, mode, fs_type);
1535 if (IS_ERR(device)) {
1536 mutex_unlock(&uuid_mutex);
1537 error = PTR_ERR(device);
1538 goto error_fs_info;
1539 }
1540
1541 fs_devices = device->fs_devices;
1542 fs_info->fs_devices = fs_devices;
1543
Googleraf606d22022-10-26 21:40:12 -07001544 error = btrfs_open_devices(fs_devices, mode, fs_type);
Googler9398cc32022-12-02 17:21:52 +08001545 mutex_unlock(&uuid_mutex);
Googleraf606d22022-10-26 21:40:12 -07001546 if (error)
1547 goto error_fs_info;
1548
Googler9398cc32022-12-02 17:21:52 +08001549 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
Googleraf606d22022-10-26 21:40:12 -07001550 error = -EACCES;
1551 goto error_close_devices;
1552 }
1553
1554 bdev = fs_devices->latest_bdev;
Googler9398cc32022-12-02 17:21:52 +08001555 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
Googleraf606d22022-10-26 21:40:12 -07001556 fs_info);
1557 if (IS_ERR(s)) {
1558 error = PTR_ERR(s);
1559 goto error_close_devices;
1560 }
1561
1562 if (s->s_root) {
1563 btrfs_close_devices(fs_devices);
1564 free_fs_info(fs_info);
Googler9398cc32022-12-02 17:21:52 +08001565 if ((flags ^ s->s_flags) & SB_RDONLY)
Googleraf606d22022-10-26 21:40:12 -07001566 error = -EBUSY;
1567 } else {
1568 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1569 btrfs_sb(s)->bdev_holder = fs_type;
Googler9398cc32022-12-02 17:21:52 +08001570 if (!strstr(crc32c_impl(), "generic"))
1571 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
1572 error = btrfs_fill_super(s, fs_devices, data);
Googleraf606d22022-10-26 21:40:12 -07001573 }
Googler9398cc32022-12-02 17:21:52 +08001574 if (!error)
1575 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
1576 security_free_mnt_opts(&new_sec_opts);
Googleraf606d22022-10-26 21:40:12 -07001577 if (error) {
1578 deactivate_locked_super(s);
Googler9398cc32022-12-02 17:21:52 +08001579 return ERR_PTR(error);
Googleraf606d22022-10-26 21:40:12 -07001580 }
1581
1582 return dget(s->s_root);
1583
1584error_close_devices:
1585 btrfs_close_devices(fs_devices);
1586error_fs_info:
1587 free_fs_info(fs_info);
1588error_sec_opts:
1589 security_free_mnt_opts(&new_sec_opts);
1590 return ERR_PTR(error);
1591}
1592
Googler9398cc32022-12-02 17:21:52 +08001593/*
1594 * Mount function which is called by VFS layer.
1595 *
1596 * In order to allow mounting a subvolume directly, btrfs uses mount_subtree()
1597 * which needs vfsmount* of device's root (/). This means device's root has to
1598 * be mounted internally in any case.
1599 *
1600 * Operation flow:
1601 * 1. Parse subvol id related options for later use in mount_subvol().
1602 *
1603 * 2. Mount device's root (/) by calling vfs_kern_mount().
1604 *
1605 * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the
1606 * first place. In order to avoid calling btrfs_mount() again, we use
1607 * different file_system_type which is not registered to VFS by
1608 * register_filesystem() (btrfs_root_fs_type). As a result,
1609 * btrfs_mount_root() is called. The return value will be used by
1610 * mount_subtree() in mount_subvol().
1611 *
1612 * 3. Call mount_subvol() to get the dentry of subvolume. Since there is
1613 * "btrfs subvolume set-default", mount_subvol() is called always.
1614 */
1615static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1616 const char *device_name, void *data)
1617{
1618 struct vfsmount *mnt_root;
1619 struct dentry *root;
1620 char *subvol_name = NULL;
1621 u64 subvol_objectid = 0;
1622 int error = 0;
1623
1624 error = btrfs_parse_subvol_options(data, &subvol_name,
1625 &subvol_objectid);
1626 if (error) {
1627 kfree(subvol_name);
1628 return ERR_PTR(error);
1629 }
1630
1631 /* mount device's root (/) */
1632 mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data);
1633 if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) {
1634 if (flags & SB_RDONLY) {
1635 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1636 flags & ~SB_RDONLY, device_name, data);
1637 } else {
1638 mnt_root = vfs_kern_mount(&btrfs_root_fs_type,
1639 flags | SB_RDONLY, device_name, data);
1640 if (IS_ERR(mnt_root)) {
1641 root = ERR_CAST(mnt_root);
1642 kfree(subvol_name);
1643 goto out;
1644 }
1645
1646 down_write(&mnt_root->mnt_sb->s_umount);
1647 error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL);
1648 up_write(&mnt_root->mnt_sb->s_umount);
1649 if (error < 0) {
1650 root = ERR_PTR(error);
1651 mntput(mnt_root);
1652 kfree(subvol_name);
1653 goto out;
1654 }
1655 }
1656 }
1657 if (IS_ERR(mnt_root)) {
1658 root = ERR_CAST(mnt_root);
1659 kfree(subvol_name);
1660 goto out;
1661 }
1662
1663 /* mount_subvol() will free subvol_name and mnt_root */
1664 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
1665
1666out:
1667 return root;
1668}
1669
Googleraf606d22022-10-26 21:40:12 -07001670static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
Googler9398cc32022-12-02 17:21:52 +08001671 u32 new_pool_size, u32 old_pool_size)
Googleraf606d22022-10-26 21:40:12 -07001672{
1673 if (new_pool_size == old_pool_size)
1674 return;
1675
1676 fs_info->thread_pool_size = new_pool_size;
1677
1678 btrfs_info(fs_info, "resize thread pool %d -> %d",
1679 old_pool_size, new_pool_size);
1680
1681 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
1682 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
1683 btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
1684 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
1685 btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size);
1686 btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size);
1687 btrfs_workqueue_set_max(fs_info->endio_meta_write_workers,
1688 new_pool_size);
1689 btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size);
1690 btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size);
1691 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
1692 btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
1693 btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1694 new_pool_size);
1695}
1696
1697static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1698{
1699 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1700}
1701
1702static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1703 unsigned long old_opts, int flags)
1704{
1705 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1706 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
Googler9398cc32022-12-02 17:21:52 +08001707 (flags & SB_RDONLY))) {
Googleraf606d22022-10-26 21:40:12 -07001708 /* wait for any defraggers to finish */
1709 wait_event(fs_info->transaction_wait,
1710 (atomic_read(&fs_info->defrag_running) == 0));
Googler9398cc32022-12-02 17:21:52 +08001711 if (flags & SB_RDONLY)
Googleraf606d22022-10-26 21:40:12 -07001712 sync_filesystem(fs_info->sb);
1713 }
1714}
1715
1716static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1717 unsigned long old_opts)
1718{
1719 /*
1720 * We need to cleanup all defragable inodes if the autodefragment is
1721 * close or the filesystem is read only.
1722 */
1723 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
Googler9398cc32022-12-02 17:21:52 +08001724 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
Googleraf606d22022-10-26 21:40:12 -07001725 btrfs_cleanup_defrag_inodes(fs_info);
1726 }
1727
1728 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1729}
1730
1731static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1732{
1733 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1734 struct btrfs_root *root = fs_info->tree_root;
1735 unsigned old_flags = sb->s_flags;
1736 unsigned long old_opts = fs_info->mount_opt;
1737 unsigned long old_compress_type = fs_info->compress_type;
1738 u64 old_max_inline = fs_info->max_inline;
Googler9398cc32022-12-02 17:21:52 +08001739 u32 old_thread_pool_size = fs_info->thread_pool_size;
1740 u32 old_metadata_ratio = fs_info->metadata_ratio;
Googleraf606d22022-10-26 21:40:12 -07001741 int ret;
1742
1743 sync_filesystem(sb);
1744 btrfs_remount_prepare(fs_info);
1745
1746 if (data) {
Googler9398cc32022-12-02 17:21:52 +08001747 void *new_sec_opts = NULL;
Googleraf606d22022-10-26 21:40:12 -07001748
Googler9398cc32022-12-02 17:21:52 +08001749 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1750 if (!ret)
1751 ret = security_sb_remount(sb, new_sec_opts);
1752 security_free_mnt_opts(&new_sec_opts);
Googleraf606d22022-10-26 21:40:12 -07001753 if (ret)
1754 goto restore;
Googleraf606d22022-10-26 21:40:12 -07001755 }
1756
Googler9398cc32022-12-02 17:21:52 +08001757 ret = btrfs_parse_options(fs_info, data, *flags);
1758 if (ret)
Googleraf606d22022-10-26 21:40:12 -07001759 goto restore;
1760
1761 btrfs_remount_begin(fs_info, old_opts, *flags);
1762 btrfs_resize_thread_pool(fs_info,
1763 fs_info->thread_pool_size, old_thread_pool_size);
1764
Googler9398cc32022-12-02 17:21:52 +08001765 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
Googleraf606d22022-10-26 21:40:12 -07001766 goto out;
1767
Googler9398cc32022-12-02 17:21:52 +08001768 if (*flags & SB_RDONLY) {
Googleraf606d22022-10-26 21:40:12 -07001769 /*
1770 * this also happens on 'umount -rf' or on shutdown, when
1771 * the filesystem is busy.
1772 */
1773 cancel_work_sync(&fs_info->async_reclaim_work);
1774
1775 /* wait for the uuid_scan task to finish */
1776 down(&fs_info->uuid_tree_rescan_sem);
1777 /* avoid complains from lockdep et al. */
1778 up(&fs_info->uuid_tree_rescan_sem);
1779
Googler9398cc32022-12-02 17:21:52 +08001780 sb->s_flags |= SB_RDONLY;
Googleraf606d22022-10-26 21:40:12 -07001781
1782 /*
Googler9398cc32022-12-02 17:21:52 +08001783 * Setting SB_RDONLY will put the cleaner thread to
Googleraf606d22022-10-26 21:40:12 -07001784 * sleep at the next loop if it's already active.
1785 * If it's already asleep, we'll leave unused block
1786 * groups on disk until we're mounted read-write again
1787 * unless we clean them up here.
1788 */
1789 btrfs_delete_unused_bgs(fs_info);
1790
1791 btrfs_dev_replace_suspend_for_unmount(fs_info);
1792 btrfs_scrub_cancel(fs_info);
1793 btrfs_pause_balance(fs_info);
1794
Googler9398cc32022-12-02 17:21:52 +08001795 ret = btrfs_commit_super(fs_info);
Googleraf606d22022-10-26 21:40:12 -07001796 if (ret)
1797 goto restore;
1798 } else {
Googler9398cc32022-12-02 17:21:52 +08001799 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
Googleraf606d22022-10-26 21:40:12 -07001800 btrfs_err(fs_info,
1801 "Remounting read-write after error is not allowed");
1802 ret = -EINVAL;
1803 goto restore;
1804 }
1805 if (fs_info->fs_devices->rw_devices == 0) {
1806 ret = -EACCES;
1807 goto restore;
1808 }
1809
Googler9398cc32022-12-02 17:21:52 +08001810 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
Googleraf606d22022-10-26 21:40:12 -07001811 btrfs_warn(fs_info,
Googler9398cc32022-12-02 17:21:52 +08001812 "too many missing devices, writable remount is not allowed");
Googleraf606d22022-10-26 21:40:12 -07001813 ret = -EACCES;
1814 goto restore;
1815 }
1816
1817 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1818 btrfs_warn(fs_info,
1819 "mount required to replay tree-log, cannot remount read-write");
1820 ret = -EINVAL;
1821 goto restore;
1822 }
1823
1824 ret = btrfs_cleanup_fs_roots(fs_info);
1825 if (ret)
1826 goto restore;
1827
1828 /* recover relocation */
1829 mutex_lock(&fs_info->cleaner_mutex);
1830 ret = btrfs_recover_relocation(root);
1831 mutex_unlock(&fs_info->cleaner_mutex);
1832 if (ret)
1833 goto restore;
1834
1835 ret = btrfs_resume_balance_async(fs_info);
1836 if (ret)
1837 goto restore;
1838
1839 ret = btrfs_resume_dev_replace_async(fs_info);
1840 if (ret) {
1841 btrfs_warn(fs_info, "failed to resume dev_replace");
1842 goto restore;
1843 }
1844
1845 btrfs_qgroup_rescan_resume(fs_info);
1846
1847 if (!fs_info->uuid_root) {
1848 btrfs_info(fs_info, "creating UUID tree");
1849 ret = btrfs_create_uuid_tree(fs_info);
1850 if (ret) {
1851 btrfs_warn(fs_info,
1852 "failed to create the UUID tree %d",
1853 ret);
1854 goto restore;
1855 }
1856 }
Googler9398cc32022-12-02 17:21:52 +08001857 sb->s_flags &= ~SB_RDONLY;
Googleraf606d22022-10-26 21:40:12 -07001858
1859 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
1860 }
1861out:
Googler9398cc32022-12-02 17:21:52 +08001862 /*
1863 * We need to set SB_I_VERSION here otherwise it'll get cleared by VFS,
1864 * since the absence of the flag means it can be toggled off by remount.
1865 */
1866 *flags |= SB_I_VERSION;
1867
Googleraf606d22022-10-26 21:40:12 -07001868 wake_up_process(fs_info->transaction_kthread);
1869 btrfs_remount_cleanup(fs_info, old_opts);
1870 return 0;
1871
1872restore:
Googler9398cc32022-12-02 17:21:52 +08001873 /* We've hit an error - don't reset SB_RDONLY */
1874 if (sb_rdonly(sb))
1875 old_flags |= SB_RDONLY;
Googleraf606d22022-10-26 21:40:12 -07001876 sb->s_flags = old_flags;
1877 fs_info->mount_opt = old_opts;
1878 fs_info->compress_type = old_compress_type;
1879 fs_info->max_inline = old_max_inline;
1880 btrfs_resize_thread_pool(fs_info,
1881 old_thread_pool_size, fs_info->thread_pool_size);
1882 fs_info->metadata_ratio = old_metadata_ratio;
1883 btrfs_remount_cleanup(fs_info, old_opts);
1884 return ret;
1885}
1886
1887/* Used to sort the devices by max_avail(descending sort) */
Googler9398cc32022-12-02 17:21:52 +08001888static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
Googleraf606d22022-10-26 21:40:12 -07001889 const void *dev_info2)
1890{
1891 if (((struct btrfs_device_info *)dev_info1)->max_avail >
1892 ((struct btrfs_device_info *)dev_info2)->max_avail)
1893 return -1;
1894 else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1895 ((struct btrfs_device_info *)dev_info2)->max_avail)
1896 return 1;
1897 else
1898 return 0;
1899}
1900
1901/*
1902 * sort the devices by max_avail, in which max free extent size of each device
1903 * is stored.(Descending Sort)
1904 */
1905static inline void btrfs_descending_sort_devices(
1906 struct btrfs_device_info *devices,
1907 size_t nr_devices)
1908{
1909 sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1910 btrfs_cmp_device_free_bytes, NULL);
1911}
1912
1913/*
1914 * The helper to calc the free space on the devices that can be used to store
1915 * file data.
1916 */
Googler9398cc32022-12-02 17:21:52 +08001917static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
1918 u64 *free_bytes)
Googleraf606d22022-10-26 21:40:12 -07001919{
1920 struct btrfs_device_info *devices_info;
1921 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1922 struct btrfs_device *device;
Googleraf606d22022-10-26 21:40:12 -07001923 u64 type;
1924 u64 avail_space;
1925 u64 min_stripe_size;
Googler9398cc32022-12-02 17:21:52 +08001926 int num_stripes = 1;
Googleraf606d22022-10-26 21:40:12 -07001927 int i = 0, nr_devices;
Googler9398cc32022-12-02 17:21:52 +08001928 const struct btrfs_raid_attr *rattr;
Googleraf606d22022-10-26 21:40:12 -07001929
1930 /*
1931 * We aren't under the device list lock, so this is racy-ish, but good
1932 * enough for our purposes.
1933 */
1934 nr_devices = fs_info->fs_devices->open_devices;
1935 if (!nr_devices) {
1936 smp_mb();
1937 nr_devices = fs_info->fs_devices->open_devices;
1938 ASSERT(nr_devices);
1939 if (!nr_devices) {
1940 *free_bytes = 0;
1941 return 0;
1942 }
1943 }
1944
1945 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
Googler9398cc32022-12-02 17:21:52 +08001946 GFP_KERNEL);
Googleraf606d22022-10-26 21:40:12 -07001947 if (!devices_info)
1948 return -ENOMEM;
1949
1950 /* calc min stripe number for data space allocation */
Googler9398cc32022-12-02 17:21:52 +08001951 type = btrfs_data_alloc_profile(fs_info);
1952 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
1953
1954 if (type & BTRFS_BLOCK_GROUP_RAID0)
Googleraf606d22022-10-26 21:40:12 -07001955 num_stripes = nr_devices;
Googler9398cc32022-12-02 17:21:52 +08001956 else if (type & BTRFS_BLOCK_GROUP_RAID1)
Googleraf606d22022-10-26 21:40:12 -07001957 num_stripes = 2;
Googler9398cc32022-12-02 17:21:52 +08001958 else if (type & BTRFS_BLOCK_GROUP_RAID10)
Googleraf606d22022-10-26 21:40:12 -07001959 num_stripes = 4;
Googleraf606d22022-10-26 21:40:12 -07001960
Googler9398cc32022-12-02 17:21:52 +08001961 /* Adjust for more than 1 stripe per device */
1962 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
Googleraf606d22022-10-26 21:40:12 -07001963
1964 rcu_read_lock();
1965 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Googler9398cc32022-12-02 17:21:52 +08001966 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1967 &device->dev_state) ||
1968 !device->bdev ||
1969 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Googleraf606d22022-10-26 21:40:12 -07001970 continue;
1971
1972 if (i >= nr_devices)
1973 break;
1974
1975 avail_space = device->total_bytes - device->bytes_used;
1976
1977 /* align with stripe_len */
Googler9398cc32022-12-02 17:21:52 +08001978 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
Googleraf606d22022-10-26 21:40:12 -07001979
1980 /*
1981 * In order to avoid overwriting the superblock on the drive,
1982 * btrfs starts at an offset of at least 1MB when doing chunk
1983 * allocation.
Googler9398cc32022-12-02 17:21:52 +08001984 *
1985 * This ensures we have at least min_stripe_size free space
1986 * after excluding 1MB.
Googleraf606d22022-10-26 21:40:12 -07001987 */
Googler9398cc32022-12-02 17:21:52 +08001988 if (avail_space <= SZ_1M + min_stripe_size)
Googler012a81c2022-09-15 14:55:24 +08001989 continue;
Googler38bda472022-08-19 10:07:08 -07001990
Googler9398cc32022-12-02 17:21:52 +08001991 avail_space -= SZ_1M;
1992
Googleraf606d22022-10-26 21:40:12 -07001993 devices_info[i].dev = device;
1994 devices_info[i].max_avail = avail_space;
1995
1996 i++;
1997 }
1998 rcu_read_unlock();
1999
2000 nr_devices = i;
2001
2002 btrfs_descending_sort_devices(devices_info, nr_devices);
2003
2004 i = nr_devices - 1;
2005 avail_space = 0;
Googler9398cc32022-12-02 17:21:52 +08002006 while (nr_devices >= rattr->devs_min) {
2007 num_stripes = min(num_stripes, nr_devices);
Googleraf606d22022-10-26 21:40:12 -07002008
2009 if (devices_info[i].max_avail >= min_stripe_size) {
2010 int j;
2011 u64 alloc_size;
2012
2013 avail_space += devices_info[i].max_avail * num_stripes;
2014 alloc_size = devices_info[i].max_avail;
2015 for (j = i + 1 - num_stripes; j <= i; j++)
2016 devices_info[j].max_avail -= alloc_size;
2017 }
2018 i--;
2019 nr_devices--;
2020 }
2021
2022 kfree(devices_info);
2023 *free_bytes = avail_space;
2024 return 0;
2025}
2026
2027/*
2028 * Calculate numbers for 'df', pessimistic in case of mixed raid profiles.
2029 *
2030 * If there's a redundant raid level at DATA block groups, use the respective
2031 * multiplier to scale the sizes.
2032 *
2033 * Unused device space usage is based on simulating the chunk allocator
Googler9398cc32022-12-02 17:21:52 +08002034 * algorithm that respects the device sizes and order of allocations. This is
2035 * a close approximation of the actual use but there are other factors that may
2036 * change the result (like a new metadata chunk).
Googleraf606d22022-10-26 21:40:12 -07002037 *
2038 * If metadata is exhausted, f_bavail will be 0.
2039 */
2040static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2041{
2042 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
2043 struct btrfs_super_block *disk_super = fs_info->super_copy;
2044 struct list_head *head = &fs_info->space_info;
2045 struct btrfs_space_info *found;
2046 u64 total_used = 0;
2047 u64 total_free_data = 0;
2048 u64 total_free_meta = 0;
2049 int bits = dentry->d_sb->s_blocksize_bits;
Googler9398cc32022-12-02 17:21:52 +08002050 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
Googleraf606d22022-10-26 21:40:12 -07002051 unsigned factor = 1;
2052 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
2053 int ret;
2054 u64 thresh = 0;
2055 int mixed = 0;
2056
2057 rcu_read_lock();
2058 list_for_each_entry_rcu(found, head, list) {
2059 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
2060 int i;
2061
2062 total_free_data += found->disk_total - found->disk_used;
2063 total_free_data -=
2064 btrfs_account_ro_block_groups_free_space(found);
2065
2066 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Googler9398cc32022-12-02 17:21:52 +08002067 if (!list_empty(&found->block_groups[i]))
2068 factor = btrfs_bg_type_to_factor(
2069 btrfs_raid_array[i].bg_flag);
Googleraf606d22022-10-26 21:40:12 -07002070 }
2071 }
2072
2073 /*
2074 * Metadata in mixed block goup profiles are accounted in data
2075 */
2076 if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) {
2077 if (found->flags & BTRFS_BLOCK_GROUP_DATA)
2078 mixed = 1;
2079 else
2080 total_free_meta += found->disk_total -
2081 found->disk_used;
2082 }
2083
2084 total_used += found->disk_used;
2085 }
2086
2087 rcu_read_unlock();
2088
2089 buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor);
2090 buf->f_blocks >>= bits;
2091 buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits);
2092
2093 /* Account global block reserve as used, it's in logical size already */
2094 spin_lock(&block_rsv->lock);
2095 /* Mixed block groups accounting is not byte-accurate, avoid overflow */
2096 if (buf->f_bfree >= block_rsv->size >> bits)
2097 buf->f_bfree -= block_rsv->size >> bits;
2098 else
2099 buf->f_bfree = 0;
2100 spin_unlock(&block_rsv->lock);
2101
2102 buf->f_bavail = div_u64(total_free_data, factor);
Googler9398cc32022-12-02 17:21:52 +08002103 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
Googleraf606d22022-10-26 21:40:12 -07002104 if (ret)
2105 return ret;
2106 buf->f_bavail += div_u64(total_free_data, factor);
2107 buf->f_bavail = buf->f_bavail >> bits;
2108
2109 /*
2110 * We calculate the remaining metadata space minus global reserve. If
2111 * this is (supposedly) smaller than zero, there's no space. But this
2112 * does not hold in practice, the exhausted state happens where's still
2113 * some positive delta. So we apply some guesswork and compare the
2114 * delta to a 4M threshold. (Practically observed delta was ~2M.)
2115 *
2116 * We probably cannot calculate the exact threshold value because this
2117 * depends on the internal reservations requested by various
2118 * operations, so some operations that consume a few metadata will
2119 * succeed even if the Avail is zero. But this is better than the other
2120 * way around.
2121 */
Googler9398cc32022-12-02 17:21:52 +08002122 thresh = SZ_4M;
Googleraf606d22022-10-26 21:40:12 -07002123
2124 /*
2125 * We only want to claim there's no available space if we can no longer
2126 * allocate chunks for our metadata profile and our global reserve will
2127 * not fit in the free metadata space. If we aren't ->full then we
2128 * still can allocate chunks and thus are fine using the currently
2129 * calculated f_bavail.
2130 */
2131 if (!mixed && block_rsv->space_info->full &&
2132 total_free_meta - thresh < block_rsv->size)
2133 buf->f_bavail = 0;
2134
2135 buf->f_type = BTRFS_SUPER_MAGIC;
2136 buf->f_bsize = dentry->d_sb->s_blocksize;
2137 buf->f_namelen = BTRFS_NAME_LEN;
2138
2139 /* We treat it as constant endianness (it doesn't matter _which_)
2140 because we want the fsid to come out the same whether mounted
2141 on a big-endian or little-endian host */
2142 buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
2143 buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
2144 /* Mask in the root object ID too, to disambiguate subvols */
Googler9398cc32022-12-02 17:21:52 +08002145 buf->f_fsid.val[0] ^=
2146 BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32;
2147 buf->f_fsid.val[1] ^=
2148 BTRFS_I(d_inode(dentry))->root->root_key.objectid;
Googleraf606d22022-10-26 21:40:12 -07002149
2150 return 0;
2151}
2152
2153static void btrfs_kill_super(struct super_block *sb)
2154{
2155 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2156 kill_anon_super(sb);
2157 free_fs_info(fs_info);
2158}
2159
2160static struct file_system_type btrfs_fs_type = {
2161 .owner = THIS_MODULE,
2162 .name = "btrfs",
2163 .mount = btrfs_mount,
2164 .kill_sb = btrfs_kill_super,
2165 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2166};
Googler9398cc32022-12-02 17:21:52 +08002167
2168static struct file_system_type btrfs_root_fs_type = {
2169 .owner = THIS_MODULE,
2170 .name = "btrfs",
2171 .mount = btrfs_mount_root,
2172 .kill_sb = btrfs_kill_super,
2173 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
2174};
2175
Googleraf606d22022-10-26 21:40:12 -07002176MODULE_ALIAS_FS("btrfs");
2177
2178static int btrfs_control_open(struct inode *inode, struct file *file)
2179{
2180 /*
2181 * The control file's private_data is used to hold the
2182 * transaction when it is started and is used to keep
2183 * track of whether a transaction is already in progress.
2184 */
2185 file->private_data = NULL;
2186 return 0;
2187}
2188
2189/*
2190 * used by btrfsctl to scan devices when no FS is mounted
2191 */
2192static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2193 unsigned long arg)
2194{
2195 struct btrfs_ioctl_vol_args *vol;
Googler9398cc32022-12-02 17:21:52 +08002196 struct btrfs_device *device = NULL;
Googleraf606d22022-10-26 21:40:12 -07002197 int ret = -ENOTTY;
2198
2199 if (!capable(CAP_SYS_ADMIN))
2200 return -EPERM;
2201
2202 vol = memdup_user((void __user *)arg, sizeof(*vol));
2203 if (IS_ERR(vol))
2204 return PTR_ERR(vol);
2205 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
2206
2207 switch (cmd) {
2208 case BTRFS_IOC_SCAN_DEV:
Googler9398cc32022-12-02 17:21:52 +08002209 mutex_lock(&uuid_mutex);
2210 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2211 &btrfs_root_fs_type);
2212 ret = PTR_ERR_OR_ZERO(device);
2213 mutex_unlock(&uuid_mutex);
2214 break;
2215 case BTRFS_IOC_FORGET_DEV:
2216 ret = btrfs_forget_devices(vol->name);
Googleraf606d22022-10-26 21:40:12 -07002217 break;
2218 case BTRFS_IOC_DEVICES_READY:
Googler9398cc32022-12-02 17:21:52 +08002219 mutex_lock(&uuid_mutex);
2220 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2221 &btrfs_root_fs_type);
2222 if (IS_ERR(device)) {
2223 mutex_unlock(&uuid_mutex);
2224 ret = PTR_ERR(device);
Googleraf606d22022-10-26 21:40:12 -07002225 break;
Googler9398cc32022-12-02 17:21:52 +08002226 }
2227 ret = !(device->fs_devices->num_devices ==
2228 device->fs_devices->total_devices);
2229 mutex_unlock(&uuid_mutex);
Googleraf606d22022-10-26 21:40:12 -07002230 break;
2231 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
2232 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
2233 break;
2234 }
2235
2236 kfree(vol);
2237 return ret;
2238}
2239
2240static int btrfs_freeze(struct super_block *sb)
2241{
2242 struct btrfs_trans_handle *trans;
Googler9398cc32022-12-02 17:21:52 +08002243 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2244 struct btrfs_root *root = fs_info->tree_root;
Googleraf606d22022-10-26 21:40:12 -07002245
Googler9398cc32022-12-02 17:21:52 +08002246 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
Googleraf606d22022-10-26 21:40:12 -07002247 /*
2248 * We don't need a barrier here, we'll wait for any transaction that
2249 * could be in progress on other threads (and do delayed iputs that
2250 * we want to avoid on a frozen filesystem), or do the commit
2251 * ourselves.
2252 */
2253 trans = btrfs_attach_transaction_barrier(root);
2254 if (IS_ERR(trans)) {
2255 /* no transaction, don't bother */
2256 if (PTR_ERR(trans) == -ENOENT)
2257 return 0;
2258 return PTR_ERR(trans);
2259 }
Googler9398cc32022-12-02 17:21:52 +08002260 return btrfs_commit_transaction(trans);
Googleraf606d22022-10-26 21:40:12 -07002261}
2262
2263static int btrfs_unfreeze(struct super_block *sb)
2264{
Googler9398cc32022-12-02 17:21:52 +08002265 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Googleraf606d22022-10-26 21:40:12 -07002266
Googler9398cc32022-12-02 17:21:52 +08002267 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
Googleraf606d22022-10-26 21:40:12 -07002268 return 0;
2269}
2270
2271static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2272{
2273 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
2274 struct btrfs_device *dev, *first_dev = NULL;
2275
Googler9398cc32022-12-02 17:21:52 +08002276 /*
2277 * Lightweight locking of the devices. We should not need
2278 * device_list_mutex here as we only read the device data and the list
2279 * is protected by RCU. Even if a device is deleted during the list
2280 * traversals, we'll get valid data, the freeing callback will wait at
2281 * least until the rcu_read_unlock.
2282 */
2283 rcu_read_lock();
2284 list_for_each_entry_rcu(dev, &fs_info->fs_devices->devices, dev_list) {
2285 if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
2286 continue;
2287 if (!dev->name)
2288 continue;
2289 if (!first_dev || dev->devid < first_dev->devid)
2290 first_dev = dev;
Googleraf606d22022-10-26 21:40:12 -07002291 }
2292
Googler9398cc32022-12-02 17:21:52 +08002293 if (first_dev)
2294 seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2295 else
Googleraf606d22022-10-26 21:40:12 -07002296 WARN_ON(1);
Googler9398cc32022-12-02 17:21:52 +08002297 rcu_read_unlock();
Googleraf606d22022-10-26 21:40:12 -07002298 return 0;
2299}
2300
2301static const struct super_operations btrfs_super_ops = {
2302 .drop_inode = btrfs_drop_inode,
2303 .evict_inode = btrfs_evict_inode,
2304 .put_super = btrfs_put_super,
2305 .sync_fs = btrfs_sync_fs,
2306 .show_options = btrfs_show_options,
2307 .show_devname = btrfs_show_devname,
2308 .alloc_inode = btrfs_alloc_inode,
2309 .destroy_inode = btrfs_destroy_inode,
Googler9398cc32022-12-02 17:21:52 +08002310 .free_inode = btrfs_free_inode,
Googleraf606d22022-10-26 21:40:12 -07002311 .statfs = btrfs_statfs,
2312 .remount_fs = btrfs_remount,
2313 .freeze_fs = btrfs_freeze,
2314 .unfreeze_fs = btrfs_unfreeze,
2315};
2316
2317static const struct file_operations btrfs_ctl_fops = {
2318 .open = btrfs_control_open,
2319 .unlocked_ioctl = btrfs_control_ioctl,
2320 .compat_ioctl = btrfs_control_ioctl,
2321 .owner = THIS_MODULE,
2322 .llseek = noop_llseek,
2323};
2324
2325static struct miscdevice btrfs_misc = {
2326 .minor = BTRFS_MINOR,
2327 .name = "btrfs-control",
2328 .fops = &btrfs_ctl_fops
2329};
2330
2331MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2332MODULE_ALIAS("devname:btrfs-control");
2333
Googler9398cc32022-12-02 17:21:52 +08002334static int __init btrfs_interface_init(void)
Googleraf606d22022-10-26 21:40:12 -07002335{
2336 return misc_register(&btrfs_misc);
2337}
2338
Googler9398cc32022-12-02 17:21:52 +08002339static __cold void btrfs_interface_exit(void)
Googleraf606d22022-10-26 21:40:12 -07002340{
2341 misc_deregister(&btrfs_misc);
2342}
2343
Googler9398cc32022-12-02 17:21:52 +08002344static void __init btrfs_print_mod_info(void)
Googleraf606d22022-10-26 21:40:12 -07002345{
Googler9398cc32022-12-02 17:21:52 +08002346 static const char options[] = ""
Googleraf606d22022-10-26 21:40:12 -07002347#ifdef CONFIG_BTRFS_DEBUG
2348 ", debug=on"
2349#endif
2350#ifdef CONFIG_BTRFS_ASSERT
2351 ", assert=on"
2352#endif
2353#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2354 ", integrity-checker=on"
2355#endif
Googler9398cc32022-12-02 17:21:52 +08002356#ifdef CONFIG_BTRFS_FS_REF_VERIFY
2357 ", ref-verify=on"
2358#endif
2359 ;
2360 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
Googleraf606d22022-10-26 21:40:12 -07002361}
2362
2363static int __init init_btrfs_fs(void)
2364{
2365 int err;
2366
2367 btrfs_props_init();
2368
2369 err = btrfs_init_sysfs();
2370 if (err)
Googler9398cc32022-12-02 17:21:52 +08002371 return err;
Googleraf606d22022-10-26 21:40:12 -07002372
2373 btrfs_init_compress();
2374
2375 err = btrfs_init_cachep();
2376 if (err)
2377 goto free_compress;
2378
2379 err = extent_io_init();
2380 if (err)
2381 goto free_cachep;
2382
2383 err = extent_map_init();
2384 if (err)
2385 goto free_extent_io;
2386
2387 err = ordered_data_init();
2388 if (err)
2389 goto free_extent_map;
2390
2391 err = btrfs_delayed_inode_init();
2392 if (err)
2393 goto free_ordered_data;
2394
2395 err = btrfs_auto_defrag_init();
2396 if (err)
2397 goto free_delayed_inode;
2398
2399 err = btrfs_delayed_ref_init();
2400 if (err)
2401 goto free_auto_defrag;
2402
2403 err = btrfs_prelim_ref_init();
2404 if (err)
2405 goto free_delayed_ref;
2406
2407 err = btrfs_end_io_wq_init();
2408 if (err)
2409 goto free_prelim_ref;
2410
2411 err = btrfs_interface_init();
2412 if (err)
2413 goto free_end_io_wq;
2414
2415 btrfs_init_lockdep();
2416
2417 btrfs_print_mod_info();
2418
2419 err = btrfs_run_sanity_tests();
2420 if (err)
2421 goto unregister_ioctl;
2422
2423 err = register_filesystem(&btrfs_fs_type);
2424 if (err)
2425 goto unregister_ioctl;
2426
2427 return 0;
2428
2429unregister_ioctl:
2430 btrfs_interface_exit();
2431free_end_io_wq:
2432 btrfs_end_io_wq_exit();
2433free_prelim_ref:
2434 btrfs_prelim_ref_exit();
2435free_delayed_ref:
2436 btrfs_delayed_ref_exit();
2437free_auto_defrag:
2438 btrfs_auto_defrag_exit();
2439free_delayed_inode:
2440 btrfs_delayed_inode_exit();
2441free_ordered_data:
2442 ordered_data_exit();
2443free_extent_map:
2444 extent_map_exit();
2445free_extent_io:
2446 extent_io_exit();
2447free_cachep:
2448 btrfs_destroy_cachep();
2449free_compress:
2450 btrfs_exit_compress();
2451 btrfs_exit_sysfs();
Googler9398cc32022-12-02 17:21:52 +08002452
Googleraf606d22022-10-26 21:40:12 -07002453 return err;
2454}
2455
2456static void __exit exit_btrfs_fs(void)
2457{
2458 btrfs_destroy_cachep();
2459 btrfs_delayed_ref_exit();
2460 btrfs_auto_defrag_exit();
2461 btrfs_delayed_inode_exit();
2462 btrfs_prelim_ref_exit();
2463 ordered_data_exit();
2464 extent_map_exit();
2465 extent_io_exit();
2466 btrfs_interface_exit();
2467 btrfs_end_io_wq_exit();
2468 unregister_filesystem(&btrfs_fs_type);
2469 btrfs_exit_sysfs();
2470 btrfs_cleanup_fs_uuids();
2471 btrfs_exit_compress();
Googleraf606d22022-10-26 21:40:12 -07002472}
2473
2474late_initcall(init_btrfs_fs);
2475module_exit(exit_btrfs_fs)
2476
2477MODULE_LICENSE("GPL");
Googler9398cc32022-12-02 17:21:52 +08002478MODULE_SOFTDEP("pre: crc32c");