blob: 6a2ae208ff80a47038ae3a9dec114719d06b5807 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Yan4b82d6e2007-08-29 09:11:44 -04006#include <linux/blkdev.h>
Chris Mason2e635a22007-03-21 11:12:56 -04007#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>
Eric Parisa9572a12009-04-02 16:46:06 -040013#include <linux/seq_file.h>
Chris Mason2e635a22007-03-21 11:12:56 -040014#include <linux/string.h>
Chris Mason2e635a22007-03-21 11:12:56 -040015#include <linux/backing-dev.h>
Yan4b82d6e2007-08-29 09:11:44 -040016#include <linux/mount.h>
Chris Mason75dfe392007-03-29 11:56:46 -040017#include <linux/writeback.h>
Chris Mason8fd17792007-04-19 21:01:03 -040018#include <linux/statfs.h>
Chris Mason08607c12007-06-08 15:33:54 -040019#include <linux/compat.h>
Chris Mason95e05282007-08-29 09:11:44 -040020#include <linux/parser.h>
Chris Masonc59f8952007-12-17 20:14:04 -050021#include <linux/ctype.h>
Chris Mason6da6aba2007-12-18 16:15:09 -050022#include <linux/namei.h>
Chris Masona9218f62008-03-24 15:02:04 -040023#include <linux/miscdevice.h>
Qinghuang Feng1bcbf312009-01-15 13:51:03 -080024#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060026#include <linux/cleancache.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050027#include <linux/ratelimit.h>
Nikolay Borisov9678c542018-01-08 11:45:05 +020028#include <linux/crc32c.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000029#include <linux/btrfs.h>
Miao Xie16cdcec2011-04-22 18:12:22 +080030#include "delayed-inode.h"
Chris Mason2e635a22007-03-21 11:12:56 -040031#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040032#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040033#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040034#include "btrfs_inode.h"
Chris Mason3a686372007-05-24 13:35:57 -040035#include "print-tree.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000036#include "props.h"
Josef Bacik5103e942007-11-16 11:45:54 -050037#include "xattr.h"
Chris Mason8a4b83c2008-03-24 15:02:07 -040038#include "volumes.h"
Balaji Raobe6e8dc2008-07-21 02:01:56 +053039#include "export.h"
Chris Masonc8b97812008-10-29 14:49:59 -040040#include "compression.h"
Josef Bacik9c5085c2012-06-05 14:13:12 -040041#include "rcu-string.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Josef Bacik74255aa2013-03-15 09:47:08 -040043#include "free-space-cache.h"
Wang Shilongb9e9a6c2013-08-09 13:25:36 +080044#include "backref.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040045#include "space-info.h"
David Sterba89439102019-08-01 17:34:41 +020046#include "sysfs.h"
Josef Bacikdc11dd52013-08-14 15:05:12 -040047#include "tests/btrfs-tests.h"
Josef Bacikaac00232019-06-20 15:37:44 -040048#include "block-group.h"
Chris Mason2e635a22007-03-21 11:12:56 -040049
Mark Fashehd3982102014-07-17 12:39:00 -070050#include "qgroup.h"
liubo1abe9b82011-03-24 11:18:59 +000051#define CREATE_TRACE_POINTS
52#include <trace/events/btrfs.h>
53
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070054static const struct super_operations btrfs_super_ops;
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +090055
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.
Misono, Tomohiro312c89f2017-12-14 17:25:01 +090061 *
62 * The new btrfs_root_fs_type also servers as a tag for the bdev_holder.
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +090063 */
Josef Bacik830c4ad2011-07-25 15:55:42 -040064static struct file_system_type btrfs_fs_type;
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +090065static struct file_system_type btrfs_root_fs_type;
Chris Masone20d96d2007-03-22 12:13:20 -040066
Harald Hoyer0723a042013-11-19 11:36:05 +010067static int btrfs_remount(struct super_block *sb, int *flags, char *data);
68
Jeff Mahoneye33e17e2015-06-15 09:41:19 -040069const char *btrfs_decode_error(int errno)
liuboacce9522011-01-06 19:30:25 +080070{
David Sterba08748812013-03-12 14:46:08 +000071 char *errstr = "unknown";
liuboacce9522011-01-06 19:30:25 +080072
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;
Jeff Mahoney8c342932011-10-03 23:22:31 -040083 case -EEXIST:
84 errstr = "Object already exists";
85 break;
David Sterba94ef7282013-03-20 14:29:47 +000086 case -ENOSPC:
87 errstr = "No space left";
88 break;
89 case -ENOENT:
90 errstr = "No such entry";
91 break;
liuboacce9522011-01-06 19:30:25 +080092 }
93
94 return errstr;
95}
96
liuboacce9522011-01-06 19:30:25 +080097/*
Anand Jain34d97002016-03-16 16:43:06 +080098 * __btrfs_handle_fs_error decodes expected errors from the caller and
Andrea Gelmini52042d82018-11-28 12:05:13 +010099 * invokes the appropriate error response.
liuboacce9522011-01-06 19:30:25 +0800100 */
David Sterbac0d19e22015-04-24 19:11:57 +0200101__cold
Anand Jain34d97002016-03-16 16:43:06 +0800102void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
Jeff Mahoney4da35112012-03-01 14:57:30 +0100103 unsigned int line, int errno, const char *fmt, ...)
liuboacce9522011-01-06 19:30:25 +0800104{
105 struct super_block *sb = fs_info->sb;
Anand Jain57d816a2015-08-14 18:32:52 +0800106#ifdef CONFIG_PRINTK
liuboacce9522011-01-06 19:30:25 +0800107 const char *errstr;
Anand Jain57d816a2015-08-14 18:32:52 +0800108#endif
liuboacce9522011-01-06 19:30:25 +0800109
110 /*
111 * Special case: if the error is EROFS, and we're already
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800112 * under SB_RDONLY, then it is safe here.
liuboacce9522011-01-06 19:30:25 +0800113 */
David Howellsbc98a422017-07-17 08:45:34 +0100114 if (errno == -EROFS && sb_rdonly(sb))
Jeff Mahoney4da35112012-03-01 14:57:30 +0100115 return;
liuboacce9522011-01-06 19:30:25 +0800116
Anand Jain57d816a2015-08-14 18:32:52 +0800117#ifdef CONFIG_PRINTK
David Sterba08748812013-03-12 14:46:08 +0000118 errstr = btrfs_decode_error(errno);
Jeff Mahoney4da35112012-03-01 14:57:30 +0100119 if (fmt) {
Eric Sandeen37252a62013-01-31 00:54:58 +0000120 struct va_format vaf;
121 va_list args;
122
123 va_start(args, fmt);
124 vaf.fmt = fmt;
125 vaf.va = &args;
liuboacce9522011-01-06 19:30:25 +0800126
Jeff Mahoney62e85572016-09-20 10:05:01 -0400127 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n",
David Sterba08748812013-03-12 14:46:08 +0000128 sb->s_id, function, line, errno, errstr, &vaf);
Eric Sandeen37252a62013-01-31 00:54:58 +0000129 va_end(args);
Jeff Mahoney4da35112012-03-01 14:57:30 +0100130 } else {
Jeff Mahoney62e85572016-09-20 10:05:01 -0400131 pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
David Sterba08748812013-03-12 14:46:08 +0000132 sb->s_id, function, line, errno, errstr);
Jeff Mahoney4da35112012-03-01 14:57:30 +0100133 }
Anand Jain57d816a2015-08-14 18:32:52 +0800134#endif
Jeff Mahoney4da35112012-03-01 14:57:30 +0100135
Anand Jain0713d902016-03-17 10:38:57 +0800136 /*
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
Jeff Mahoney4da35112012-03-01 14:57:30 +0100142 /* Don't go through full error handling during mount */
Anand Jain922ea892018-01-04 18:01:55 +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
Andrea Gelmini52042d82018-11-28 12:05:13 +0100157 * completes. The next time when the filesystem is mounted writable
Anand Jain922ea892018-01-04 18:01:55 +0800158 * again, the device replace operation continues.
159 */
Jeff Mahoney4da35112012-03-01 14:57:30 +0100160}
161
Anand Jain57d816a2015-08-14 18:32:52 +0800162#ifdef CONFIG_PRINTK
Joe Perches533574c2012-07-30 14:40:13 -0700163static const char * const logtypes[] = {
Jeff Mahoney4da35112012-03-01 14:57:30 +0100164 "emergency",
165 "alert",
166 "critical",
167 "error",
168 "warning",
169 "notice",
170 "info",
171 "debug",
172};
173
Nikolay Borisov35f4e5e2016-07-13 16:19:15 +0300174
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
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000190void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
Jeff Mahoney4da35112012-03-01 14:57:30 +0100191{
Petr Mladek40f78282016-12-14 15:04:01 -0800192 char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
Jeff Mahoney4da35112012-03-01 14:57:30 +0100193 struct va_format vaf;
194 va_list args;
Joe Perches533574c2012-07-30 14:40:13 -0700195 int kern_level;
Petr Mladek40f78282016-12-14 15:04:01 -0800196 const char *type = logtypes[4];
197 struct ratelimit_state *ratelimit = &printk_limits[4];
Jeff Mahoney4da35112012-03-01 14:57:30 +0100198
199 va_start(args, fmt);
200
Petr Mladek262c5e82016-12-12 16:45:50 -0800201 while ((kern_level = printk_get_level(fmt)) != 0) {
Joe Perches533574c2012-07-30 14:40:13 -0700202 size_t size = printk_skip_level(fmt) - fmt;
Petr Mladek262c5e82016-12-12 16:45:50 -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 }
Joe Perches533574c2012-07-30 14:40:13 -0700210 fmt += size;
Petr Mladek262c5e82016-12-12 16:45:50 -0800211 }
212
Jeff Mahoney4da35112012-03-01 14:57:30 +0100213 vaf.fmt = fmt;
214 vaf.va = &args;
Joe Perches533574c2012-07-30 14:40:13 -0700215
Nikolay Borisov35f4e5e2016-07-13 16:19:15 +0300216 if (__ratelimit(ratelimit))
Colin Ian King3993b112017-09-11 16:15:28 +0100217 printk("%sBTRFS %s (device %s): %pV\n", lvl, type,
218 fs_info ? fs_info->sb->s_id : "<unknown>", &vaf);
Joe Perches533574c2012-07-30 14:40:13 -0700219
220 va_end(args);
liuboacce9522011-01-06 19:30:25 +0800221}
Joe Perches533574c2012-07-30 14:40:13 -0700222#endif
223
Jeff Mahoney8c342932011-10-03 23:22:31 -0400224/*
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100225 * 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 */
David Sterbac0d19e22015-04-24 19:11:57 +0200237__cold
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100238void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
Jeff Mahoney66642832016-06-10 18:19:25 -0400239 const char *function,
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100240 unsigned int line, int errno)
241{
Jeff Mahoney66642832016-06-10 18:19:25 -0400242 struct btrfs_fs_info *fs_info = trans->fs_info;
243
David Sterba84bfb4b2020-02-05 17:34:34 +0100244 WRITE_ONCE(trans->aborted, errno);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100245 /* Nothing used. The other threads that have joined this
246 * transaction may be able to continue. */
Jeff Mahoney64c12922016-06-08 00:36:38 -0400247 if (!trans->dirty && list_empty(&trans->new_bgs)) {
Miao Xie69ce9772012-09-06 04:04:44 -0600248 const char *errstr;
249
David Sterba08748812013-03-12 14:46:08 +0000250 errstr = btrfs_decode_error(errno);
Jeff Mahoney66642832016-06-10 18:19:25 -0400251 btrfs_warn(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +0000252 "%s:%d: Aborting unused transaction(%s).",
253 function, line, errstr);
liuboacce9522011-01-06 19:30:25 +0800254 return;
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100255 }
Seraphime Kirkovski20c7bce2016-12-15 14:38:16 +0100256 WRITE_ONCE(trans->transaction->aborted, errno);
Josef Bacik501407a2013-06-10 16:47:23 -0400257 /* Wake up anybody who may be waiting on this transaction */
Jeff Mahoney66642832016-06-10 18:19:25 -0400258 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);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100261}
262/*
Jeff Mahoney8c342932011-10-03 23:22:31 -0400263 * __btrfs_panic decodes unexpected, fatal errors from the caller,
264 * issues an alert, and either panics or BUGs, depending on mount options.
265 */
David Sterbac0d19e22015-04-24 19:11:57 +0200266__cold
Jeff Mahoney8c342932011-10-03 23:22:31 -0400267void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
268 unsigned int line, int errno, const char *fmt, ...)
269{
Jeff Mahoney8c342932011-10-03 23:22:31 -0400270 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;
liuboacce9522011-01-06 19:30:25 +0800280
David Sterba08748812013-03-12 14:46:08 +0000281 errstr = btrfs_decode_error(errno);
Satoru Takeuchid8953d62017-09-12 20:08:08 +0900282 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR)))
David Sterba08748812013-03-12 14:46:08 +0000283 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
284 s_id, function, line, &vaf, errno, errstr);
liuboacce9522011-01-06 19:30:25 +0800285
Frank Holtonefe120a2013-12-20 11:37:06 -0500286 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)",
287 function, line, &vaf, errno, errstr);
Jeff Mahoney8c342932011-10-03 23:22:31 -0400288 va_end(args);
289 /* Caller calls BUG() */
liuboacce9522011-01-06 19:30:25 +0800290}
291
Chris Masond3977122009-01-05 21:25:51 -0500292static void btrfs_put_super(struct super_block *sb)
Chris Masone20d96d2007-03-22 12:13:20 -0400293{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -0400294 close_ctree(btrfs_sb(sb));
Chris Masone20d96d2007-03-22 12:13:20 -0400295}
Chris Mason2e635a22007-03-21 11:12:56 -0400296
Chris Mason95e05282007-08-29 09:11:44 -0400297enum {
David Sterba416a7202018-03-09 14:37:01 +0100298 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,
Omar Sandoval37becec2018-05-21 17:07:19 -0700326 Opt_subvol_empty,
David Sterba416a7202018-03-09 14:37:01 +0100327 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,
Omar Sandoval70f6d822015-09-29 20:50:38 -0700340 Opt_check_integrity_including_extent_data,
David Sterba416a7202018-03-09 14:37:01 +0100341 Opt_check_integrity_print_mask,
342 Opt_enospc_debug, Opt_noenospc_debug,
Josef Bacikd0bd4562015-09-23 14:54:14 -0400343#ifdef CONFIG_BTRFS_DEBUG
344 Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
345#endif
Josef Bacikfb592372017-09-29 15:43:48 -0400346#ifdef CONFIG_BTRFS_FS_REF_VERIFY
347 Opt_ref_verify,
348#endif
Ilya Dryomov9555c6c2012-01-16 22:04:48 +0200349 Opt_err,
Chris Mason95e05282007-08-29 09:11:44 -0400350};
351
David Sterba4d4ab6d2015-11-19 11:42:31 +0100352static const match_table_t tokens = {
David Sterba416a7202018-03-09 14:37:01 +0100353 {Opt_acl, "acl"},
354 {Opt_noacl, "noacl"},
355 {Opt_clear_cache, "clear_cache"},
356 {Opt_commit_interval, "commit=%u"},
Chris Masonc8b97812008-10-29 14:49:59 -0400357 {Opt_compress, "compress"},
Li Zefan261507a02010-12-17 14:21:50 +0800358 {Opt_compress_type, "compress=%s"},
Chris Masona555f812010-01-28 16:18:15 -0500359 {Opt_compress_force, "compress-force"},
Li Zefan261507a02010-12-17 14:21:50 +0800360 {Opt_compress_force_type, "compress-force=%s"},
David Sterba416a7202018-03-09 14:37:01 +0100361 {Opt_degraded, "degraded"},
362 {Opt_device, "device=%s"},
363 {Opt_fatal_errors, "fatal_errors=%s"},
Sage Weildccae992009-04-02 16:59:01 -0400364 {Opt_flushoncommit, "flushoncommit"},
Qu Wenruo2c9ee852014-01-06 09:58:29 +0800365 {Opt_noflushoncommit, "noflushoncommit"},
Chris Mason4b9465c2011-06-03 09:36:29 -0400366 {Opt_inode_cache, "inode_cache"},
Qu Wenruo3818aea2014-01-13 13:36:06 +0800367 {Opt_noinode_cache, "noinode_cache"},
David Sterba416a7202018-03-09 14:37:01 +0100368 {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"},
Ilya Dryomov9555c6c2012-01-16 22:04:48 +0200383 {Opt_skip_balance, "skip_balance"},
David Sterba416a7202018-03-09 14:37:01 +0100384 {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"},
Omar Sandoval37becec2018-05-21 17:07:19 -0700392 {Opt_subvol_empty, "subvol="},
David Sterba416a7202018-03-09 14:37:01 +0100393 {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 */
Stefan Behrens21adbd52011-11-09 13:44:05 +0100406 {Opt_check_integrity, "check_int"},
407 {Opt_check_integrity_including_extent_data, "check_int_data"},
Anand Jain02453bd2018-02-13 17:50:45 +0800408 {Opt_check_integrity_print_mask, "check_int_print_mask=%u"},
David Sterba416a7202018-03-09 14:37:01 +0100409 {Opt_enospc_debug, "enospc_debug"},
410 {Opt_noenospc_debug, "noenospc_debug"},
Josef Bacikd0bd4562015-09-23 14:54:14 -0400411#ifdef CONFIG_BTRFS_DEBUG
412 {Opt_fragment_data, "fragment=data"},
413 {Opt_fragment_metadata, "fragment=metadata"},
414 {Opt_fragment_all, "fragment=all"},
415#endif
Josef Bacikfb592372017-09-29 15:43:48 -0400416#ifdef CONFIG_BTRFS_FS_REF_VERIFY
417 {Opt_ref_verify, "ref_verify"},
418#endif
Josef Bacik33268ea2008-07-24 12:16:36 -0400419 {Opt_err, NULL},
Chris Mason95e05282007-08-29 09:11:44 -0400420};
421
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400422/*
423 * Regular mount options parser. Everything that is needed only when
424 * reading in a new superblock is parsed here.
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100425 * XXX JDM: This needs to be cleaned up for remount.
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400426 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400427int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
Qu Wenruo96da0912016-01-19 10:23:03 +0800428 unsigned long new_flags)
Chris Mason95e05282007-08-29 09:11:44 -0400429{
Chris Mason95e05282007-08-29 09:11:44 -0400430 substring_t args[MAX_OPT_ARGS];
Misono, Tomohiroe2157722017-12-14 17:28:00 +0900431 char *p, *num;
Josef Bacik73bc1872011-10-03 14:07:49 -0400432 u64 cache_gen;
Chris Mason4543df72008-06-11 21:47:56 -0400433 int intarg;
Sage Weila7a3f7c2009-11-07 06:19:16 +0000434 int ret = 0;
Li Zefan261507a02010-12-17 14:21:50 +0800435 char *compress_type;
436 bool compress_force = false;
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900437 enum btrfs_compression_type saved_compress_type;
David Sterba38c82552020-07-23 19:08:55 +0200438 int saved_compress_level;
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900439 bool saved_compress_force;
440 int no_compress = 0;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500441
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400442 cache_gen = btrfs_super_cache_generation(info->super_copy);
443 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
Omar Sandoval70f6d822015-09-29 20:50:38 -0700444 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
445 else if (cache_gen)
Josef Bacik73bc1872011-10-03 14:07:49 -0400446 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
447
Qu Wenruo96da0912016-01-19 10:23:03 +0800448 /*
449 * Even the options are empty, we still need to do extra check
450 * against new flags
451 */
Chris Mason95e05282007-08-29 09:11:44 -0400452 if (!options)
Qu Wenruo96da0912016-01-19 10:23:03 +0800453 goto check;
Chris Mason95e05282007-08-29 09:11:44 -0400454
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400455 while ((p = strsep(&options, ",")) != NULL) {
Chris Mason95e05282007-08-29 09:11:44 -0400456 int token;
457 if (!*p)
458 continue;
459
460 token = match_token(p, tokens, args);
461 switch (token) {
Chris Masondfe25022008-05-13 13:46:40 -0400462 case Opt_degraded:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400463 btrfs_info(info, "allowing degraded mounts");
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400464 btrfs_set_opt(info->mount_opt, DEGRADED);
Chris Masondfe25022008-05-13 13:46:40 -0400465 break;
Chris Mason95e05282007-08-29 09:11:44 -0400466 case Opt_subvol:
Omar Sandoval37becec2018-05-21 17:07:19 -0700467 case Opt_subvol_empty:
Josef Bacik73f73412009-12-04 17:38:27 +0000468 case Opt_subvolid:
Xin Zhonge15d0542011-04-06 07:33:51 +0000469 case Opt_subvolrootid:
Christoph Hellwig43e570b2008-06-10 10:40:46 -0400470 case Opt_device:
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400471 /*
Anand Jainfa59f272018-07-16 22:18:07 +0800472 * These are parsed by btrfs_parse_subvol_options or
473 * btrfs_parse_device_options and can be ignored here.
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400474 */
Chris Masonb6cda9b2007-12-14 15:30:32 -0500475 break;
476 case Opt_nodatasum:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400477 btrfs_set_and_info(info, NODATASUM,
Qu Wenruo07802532014-01-13 13:36:07 +0800478 "setting nodatasum");
Chris Masonbe20aa92007-12-17 20:14:01 -0500479 break;
Qu Wenruod3991672014-01-06 09:58:32 +0800480 case Opt_datasum:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400481 if (btrfs_test_opt(info, NODATASUM)) {
482 if (btrfs_test_opt(info, NODATACOW))
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 btrfs_info(info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400484 "setting datasum, datacow enabled");
Qu Wenruo07802532014-01-13 13:36:07 +0800485 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400486 btrfs_info(info, "setting datasum");
Qu Wenruo07802532014-01-13 13:36:07 +0800487 }
Qu Wenruod3991672014-01-06 09:58:32 +0800488 btrfs_clear_opt(info->mount_opt, NODATACOW);
489 btrfs_clear_opt(info->mount_opt, NODATASUM);
490 break;
Chris Masonbe20aa92007-12-17 20:14:01 -0500491 case Opt_nodatacow:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400492 if (!btrfs_test_opt(info, NODATACOW)) {
493 if (!btrfs_test_opt(info, COMPRESS) ||
494 !btrfs_test_opt(info, FORCE_COMPRESS)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400495 btrfs_info(info,
Qu Wenruo07802532014-01-13 13:36:07 +0800496 "setting nodatacow, compression disabled");
497 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400498 btrfs_info(info, "setting nodatacow");
Qu Wenruo07802532014-01-13 13:36:07 +0800499 }
Andrei Popabedb2cc2012-09-20 08:42:11 -0600500 }
Andrei Popabedb2cc2012-09-20 08:42:11 -0600501 btrfs_clear_opt(info->mount_opt, COMPRESS);
502 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400503 btrfs_set_opt(info->mount_opt, NODATACOW);
504 btrfs_set_opt(info->mount_opt, NODATASUM);
Chris Mason95e05282007-08-29 09:11:44 -0400505 break;
Qu Wenruoa258af72014-01-06 09:58:31 +0800506 case Opt_datacow:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400507 btrfs_clear_and_info(info, NODATACOW,
Qu Wenruo07802532014-01-13 13:36:07 +0800508 "setting datacow");
Qu Wenruoa258af72014-01-06 09:58:31 +0800509 break;
Chris Masona555f812010-01-28 16:18:15 -0500510 case Opt_compress_force:
Li Zefan261507a02010-12-17 14:21:50 +0800511 case Opt_compress_force_type:
512 compress_force = true;
Eric Sandeen1c697d42013-01-31 00:54:56 +0000513 /* Fallthrough */
Li Zefan261507a02010-12-17 14:21:50 +0800514 case Opt_compress:
515 case Opt_compress_type:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400516 saved_compress_type = btrfs_test_opt(info,
517 COMPRESS) ?
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900518 info->compress_type : BTRFS_COMPRESS_NONE;
519 saved_compress_force =
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400520 btrfs_test_opt(info, FORCE_COMPRESS);
David Sterba38c82552020-07-23 19:08:55 +0200521 saved_compress_level = info->compress_level;
Li Zefan261507a02010-12-17 14:21:50 +0800522 if (token == Opt_compress ||
523 token == Opt_compress_force ||
David Sterbaa7164fa2017-07-17 18:11:10 +0200524 strncmp(args[0].from, "zlib", 4) == 0) {
Li Zefan261507a02010-12-17 14:21:50 +0800525 compress_type = "zlib";
Qu Wenruoeae8d822017-11-06 10:43:18 +0800526
Li Zefan261507a02010-12-17 14:21:50 +0800527 info->compress_type = BTRFS_COMPRESS_ZLIB;
Qu Wenruoeae8d822017-11-06 10:43:18 +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 =
Dennis Zhoud0ab62c2019-02-04 15:20:05 -0500537 btrfs_compress_str2level(
538 BTRFS_COMPRESS_ZLIB,
539 args[0].from + 4);
Arnd Hannemann063849e2012-04-16 15:27:51 +0200540 btrfs_set_opt(info->mount_opt, COMPRESS);
Andrei Popabedb2cc2012-09-20 08:42:11 -0600541 btrfs_clear_opt(info->mount_opt, NODATACOW);
542 btrfs_clear_opt(info->mount_opt, NODATASUM);
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900543 no_compress = 0;
David Sterbaa7164fa2017-07-17 18:11:10 +0200544 } else if (strncmp(args[0].from, "lzo", 3) == 0) {
Li Zefana6fa6fa2010-10-25 15:12:26 +0800545 compress_type = "lzo";
546 info->compress_type = BTRFS_COMPRESS_LZO;
Marcos Paulo de Souza8a350942020-08-03 16:55:01 -0300547 info->compress_level = 0;
Arnd Hannemann063849e2012-04-16 15:27:51 +0200548 btrfs_set_opt(info->mount_opt, COMPRESS);
Andrei Popabedb2cc2012-09-20 08:42:11 -0600549 btrfs_clear_opt(info->mount_opt, NODATACOW);
550 btrfs_clear_opt(info->mount_opt, NODATASUM);
Mitch Harder2b0ce2c2012-07-24 11:58:43 -0600551 btrfs_set_fs_incompat(info, COMPRESS_LZO);
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900552 no_compress = 0;
Dennis Zhou3f93aef2019-02-04 15:20:08 -0500553 } else if (strncmp(args[0].from, "zstd", 4) == 0) {
Nick Terrell5c1aab12017-08-09 19:39:02 -0700554 compress_type = "zstd";
555 info->compress_type = BTRFS_COMPRESS_ZSTD;
Dennis Zhou3f93aef2019-02-04 15:20:08 -0500556 info->compress_level =
557 btrfs_compress_str2level(
558 BTRFS_COMPRESS_ZSTD,
559 args[0].from + 4);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700560 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;
Arnd Hannemann063849e2012-04-16 15:27:51 +0200565 } else if (strncmp(args[0].from, "no", 2) == 0) {
566 compress_type = "no";
David Sterba38c82552020-07-23 19:08:55 +0200567 info->compress_level = 0;
568 info->compress_type = 0;
Arnd Hannemann063849e2012-04-16 15:27:51 +0200569 btrfs_clear_opt(info->mount_opt, COMPRESS);
570 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
571 compress_force = false;
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900572 no_compress++;
Li Zefan261507a02010-12-17 14:21:50 +0800573 } else {
574 ret = -EINVAL;
575 goto out;
576 }
577
Li Zefan261507a02010-12-17 14:21:50 +0800578 if (compress_force) {
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900579 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
David Sterba143f3632014-07-29 17:41:08 +0200580 } else {
Wang Shilong4027e0f2014-06-30 10:51:25 +0800581 /*
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);
Miao Xiea7e252a2013-11-22 18:47:59 +0800588 }
David Sterba38c82552020-07-23 19:08:55 +0200589 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)) {
David Sterbaf51d2b52017-09-15 17:36:57 +0200594 btrfs_info(info, "%s %s compression, level %d",
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900595 (compress_force) ? "force" : "use",
David Sterbaf51d2b52017-09-15 17:36:57 +0200596 compress_type, info->compress_level);
Tsutomu Itohb7c47bb2016-01-06 17:03:40 +0900597 }
598 compress_force = false;
Chris Masona555f812010-01-28 16:18:15 -0500599 break;
Chris Masone18e4802008-01-18 10:54:22 -0500600 case Opt_ssd:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400601 btrfs_set_and_info(info, SSD,
Hans van Kranenburg583b7232017-07-28 08:31:28 +0200602 "enabling ssd optimizations");
Adam Borowski951e7962017-03-31 17:19:04 +0200603 btrfs_clear_opt(info->mount_opt, NOSSD);
Chris Masone18e4802008-01-18 10:54:22 -0500604 break;
Chris Mason451d7582009-06-09 20:28:34 -0400605 case Opt_ssd_spread:
Hans van Kranenburg583b7232017-07-28 08:31:28 +0200606 btrfs_set_and_info(info, SSD,
607 "enabling ssd optimizations");
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400608 btrfs_set_and_info(info, SSD_SPREAD,
Hans van Kranenburg583b7232017-07-28 08:31:28 +0200609 "using spread ssd allocation scheme");
Adam Borowski951e7962017-03-31 17:19:04 +0200610 btrfs_clear_opt(info->mount_opt, NOSSD);
Chris Mason451d7582009-06-09 20:28:34 -0400611 break;
Chris Mason3b30c222009-06-09 16:42:22 -0400612 case Opt_nossd:
Hans van Kranenburg583b7232017-07-28 08:31:28 +0200613 btrfs_set_opt(info->mount_opt, NOSSD);
614 btrfs_clear_and_info(info, SSD,
615 "not using ssd optimizations");
Howard McLauchlan62b8e072018-03-08 10:48:48 -0800616 /* Fallthrough */
617 case Opt_nossd_spread:
Hans van Kranenburg583b7232017-07-28 08:31:28 +0200618 btrfs_clear_and_info(info, SSD_SPREAD,
619 "not using spread ssd allocation scheme");
Chris Mason3b30c222009-06-09 16:42:22 -0400620 break;
Qu Wenruo842bef52014-01-06 09:58:25 +0800621 case Opt_barrier:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400622 btrfs_clear_and_info(info, NOBARRIER,
Qu Wenruo07802532014-01-13 13:36:07 +0800623 "turning on barriers");
Qu Wenruo842bef52014-01-06 09:58:25 +0800624 break;
Chris Mason21ad10c2008-01-09 09:23:21 -0500625 case Opt_nobarrier:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400626 btrfs_set_and_info(info, NOBARRIER,
Qu Wenruo07802532014-01-13 13:36:07 +0800627 "turning off barriers");
Chris Mason21ad10c2008-01-09 09:23:21 -0500628 break;
Chris Mason4543df72008-06-11 21:47:56 -0400629 case Opt_thread_pool:
Wang Shilong2c334e82013-07-24 10:29:05 +0800630 ret = match_int(&args[0], &intarg);
631 if (ret) {
632 goto out;
Anand Jainf7b885b2018-02-13 17:50:42 +0800633 } else if (intarg == 0) {
Wang Shilong2c334e82013-07-24 10:29:05 +0800634 ret = -EINVAL;
635 goto out;
636 }
Anand Jainf7b885b2018-02-13 17:50:42 +0800637 info->thread_pool_size = intarg;
Chris Mason4543df72008-06-11 21:47:56 -0400638 break;
Chris Mason6f568d32008-01-29 16:03:38 -0500639 case Opt_max_inline:
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400640 num = match_strdup(&args[0]);
641 if (num) {
Akinobu Mita91748462010-02-28 10:59:11 +0000642 info->max_inline = memparse(num, NULL);
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400643 kfree(num);
Chris Mason6f568d32008-01-29 16:03:38 -0500644
Chris Mason15ada042008-06-11 16:51:38 -0400645 if (info->max_inline) {
Mitch Harderfeb5f962014-02-13 09:13:16 -0600646 info->max_inline = min_t(u64,
Chris Mason15ada042008-06-11 16:51:38 -0400647 info->max_inline,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400648 info->sectorsize);
Chris Mason15ada042008-06-11 16:51:38 -0400649 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400650 btrfs_info(info, "max_inline at %llu",
651 info->max_inline);
Wang Shilong2c334e82013-07-24 10:29:05 +0800652 } else {
653 ret = -ENOMEM;
654 goto out;
Chris Mason6f568d32008-01-29 16:03:38 -0500655 }
656 break;
Chris Mason8f662a72008-01-02 10:01:11 -0500657 case Opt_alloc_start:
David Sterba0d0c71b2017-06-15 01:30:06 +0200658 btrfs_info(info,
659 "option alloc_start is obsolete, ignored");
Chris Mason8f662a72008-01-02 10:01:11 -0500660 break;
Qu Wenruobd0330a2014-01-06 09:58:30 +0800661 case Opt_acl:
Guangliang Zhao45ff35d2014-05-12 11:04:33 +0800662#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800663 info->sb->s_flags |= SB_POSIXACL;
Qu Wenruobd0330a2014-01-06 09:58:30 +0800664 break;
Guangliang Zhao45ff35d2014-05-12 11:04:33 +0800665#else
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400666 btrfs_err(info, "support for ACL not compiled in!");
Guangliang Zhao45ff35d2014-05-12 11:04:33 +0800667 ret = -EINVAL;
668 goto out;
669#endif
Josef Bacik33268ea2008-07-24 12:16:36 -0400670 case Opt_noacl:
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800671 info->sb->s_flags &= ~SB_POSIXACL;
Josef Bacik33268ea2008-07-24 12:16:36 -0400672 break;
Sage Weil3a5e1402009-04-02 16:49:40 -0400673 case Opt_notreelog:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400674 btrfs_set_and_info(info, NOTREELOG,
Qu Wenruo07802532014-01-13 13:36:07 +0800675 "disabling tree log");
Sage Weil3a5e1402009-04-02 16:49:40 -0400676 break;
Qu Wenruoa88998f2014-01-06 09:58:33 +0800677 case Opt_treelog:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400678 btrfs_clear_and_info(info, NOTREELOG,
Qu Wenruo07802532014-01-13 13:36:07 +0800679 "enabling tree log");
Qu Wenruoa88998f2014-01-06 09:58:33 +0800680 break;
Qu Wenruofed8f162016-01-19 10:23:04 +0800681 case Opt_norecovery:
Qu Wenruo96da0912016-01-19 10:23:03 +0800682 case Opt_nologreplay:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400683 btrfs_set_and_info(info, NOLOGREPLAY,
Qu Wenruo96da0912016-01-19 10:23:03 +0800684 "disabling log replay at mount time");
685 break;
Sage Weildccae992009-04-02 16:59:01 -0400686 case Opt_flushoncommit:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400687 btrfs_set_and_info(info, FLUSHONCOMMIT,
Qu Wenruo07802532014-01-13 13:36:07 +0800688 "turning on flush-on-commit");
Sage Weildccae992009-04-02 16:59:01 -0400689 break;
Qu Wenruo2c9ee852014-01-06 09:58:29 +0800690 case Opt_noflushoncommit:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400691 btrfs_clear_and_info(info, FLUSHONCOMMIT,
Qu Wenruo07802532014-01-13 13:36:07 +0800692 "turning off flush-on-commit");
Qu Wenruo2c9ee852014-01-06 09:58:29 +0800693 break;
Josef Bacik97e728d2009-04-21 17:40:57 -0400694 case Opt_ratio:
Wang Shilong2c334e82013-07-24 10:29:05 +0800695 ret = match_int(&args[0], &intarg);
Anand Jain764cb8b2018-02-13 17:50:44 +0800696 if (ret)
Wang Shilong2c334e82013-07-24 10:29:05 +0800697 goto out;
Anand Jain764cb8b2018-02-13 17:50:44 +0800698 info->metadata_ratio = intarg;
699 btrfs_info(info, "metadata ratio %u",
700 info->metadata_ratio);
Josef Bacik97e728d2009-04-21 17:40:57 -0400701 break;
Christoph Hellwige244a0a2009-10-14 09:24:59 -0400702 case Opt_discard:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400703 btrfs_set_and_info(info, DISCARD,
Qu Wenruo07802532014-01-13 13:36:07 +0800704 "turning on discard");
Christoph Hellwige244a0a2009-10-14 09:24:59 -0400705 break;
Qu Wenruoe07a2ad2014-01-06 09:58:27 +0800706 case Opt_nodiscard:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400707 btrfs_clear_and_info(info, DISCARD,
Qu Wenruo07802532014-01-13 13:36:07 +0800708 "turning off discard");
Qu Wenruoe07a2ad2014-01-06 09:58:27 +0800709 break;
Josef Bacik0af3d002010-06-21 14:48:16 -0400710 case Opt_space_cache:
Omar Sandoval70f6d822015-09-29 20:50:38 -0700711 case Opt_space_cache_version:
712 if (token == Opt_space_cache ||
713 strcmp(args[0].from, "v1") == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400714 btrfs_clear_opt(info->mount_opt,
Omar Sandoval70f6d822015-09-29 20:50:38 -0700715 FREE_SPACE_TREE);
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400716 btrfs_set_and_info(info, SPACE_CACHE,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400717 "enabling disk space caching");
Omar Sandoval70f6d822015-09-29 20:50:38 -0700718 } else if (strcmp(args[0].from, "v2") == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400719 btrfs_clear_opt(info->mount_opt,
Omar Sandoval70f6d822015-09-29 20:50:38 -0700720 SPACE_CACHE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 btrfs_set_and_info(info, FREE_SPACE_TREE,
Omar Sandoval70f6d822015-09-29 20:50:38 -0700722 "enabling free space tree");
723 } else {
724 ret = -EINVAL;
725 goto out;
726 }
Josef Bacik0de90872010-11-19 13:40:41 +0000727 break;
Stefan Behrensf420ee12013-08-15 17:11:24 +0200728 case Opt_rescan_uuid_tree:
729 btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE);
730 break;
Josef Bacik73bc1872011-10-03 14:07:49 -0400731 case Opt_no_space_cache:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400732 if (btrfs_test_opt(info, SPACE_CACHE)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400733 btrfs_clear_and_info(info, SPACE_CACHE,
734 "disabling disk space caching");
Omar Sandoval70f6d822015-09-29 20:50:38 -0700735 }
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400736 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400737 btrfs_clear_and_info(info, FREE_SPACE_TREE,
738 "disabling free space tree");
Omar Sandoval70f6d822015-09-29 20:50:38 -0700739 }
Josef Bacik73bc1872011-10-03 14:07:49 -0400740 break;
Chris Mason4b9465c2011-06-03 09:36:29 -0400741 case Opt_inode_cache:
David Sterba7e1876a2014-02-05 15:26:17 +0100742 btrfs_set_pending_and_info(info, INODE_MAP_CACHE,
Qu Wenruo07802532014-01-13 13:36:07 +0800743 "enabling inode map caching");
Qu Wenruo3818aea2014-01-13 13:36:06 +0800744 break;
745 case Opt_noinode_cache:
David Sterba7e1876a2014-02-05 15:26:17 +0100746 btrfs_clear_pending_and_info(info, INODE_MAP_CACHE,
Qu Wenruo07802532014-01-13 13:36:07 +0800747 "disabling inode map caching");
Chris Mason4b9465c2011-06-03 09:36:29 -0400748 break;
Josef Bacik88c2ba32010-09-21 14:21:34 -0400749 case Opt_clear_cache:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400750 btrfs_set_and_info(info, CLEAR_CACHE,
Qu Wenruo07802532014-01-13 13:36:07 +0800751 "force clearing of disk cache");
Josef Bacik0af3d002010-06-21 14:48:16 -0400752 break;
Sage Weil4260f7c2010-10-29 15:46:43 -0400753 case Opt_user_subvol_rm_allowed:
754 btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
755 break;
Chris Mason91435652011-02-16 13:10:41 -0500756 case Opt_enospc_debug:
757 btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
758 break;
Qu Wenruo53036292014-01-06 09:58:28 +0800759 case Opt_noenospc_debug:
760 btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG);
761 break;
Chris Mason4cb53002011-05-24 15:35:30 -0400762 case Opt_defrag:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400763 btrfs_set_and_info(info, AUTO_DEFRAG,
Qu Wenruo07802532014-01-13 13:36:07 +0800764 "enabling auto defrag");
Chris Mason4cb53002011-05-24 15:35:30 -0400765 break;
Qu Wenruofc0ca9af2014-01-06 09:58:26 +0800766 case Opt_nodefrag:
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400767 btrfs_clear_and_info(info, AUTO_DEFRAG,
Qu Wenruo07802532014-01-13 13:36:07 +0800768 "disabling auto defrag");
Qu Wenruofc0ca9af2014-01-06 09:58:26 +0800769 break;
Chris Masonaf31f5e2011-11-03 15:17:42 -0400770 case Opt_recovery:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400771 btrfs_warn(info,
Qu Wenruo8dcddfa2016-01-19 10:23:02 +0800772 "'recovery' is deprecated, use 'usebackuproot' instead");
Bart Van Asscheacd43e32018-06-20 10:03:32 -0700773 /* fall through */
Qu Wenruo8dcddfa2016-01-19 10:23:02 +0800774 case Opt_usebackuproot:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400775 btrfs_info(info,
Qu Wenruo8dcddfa2016-01-19 10:23:02 +0800776 "trying to use backup root at mount time");
777 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
Chris Masonaf31f5e2011-11-03 15:17:42 -0400778 break;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +0200779 case Opt_skip_balance:
780 btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
781 break;
Stefan Behrens21adbd52011-11-09 13:44:05 +0100782#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
783 case Opt_check_integrity_including_extent_data:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400784 btrfs_info(info,
Frank Holtonefe120a2013-12-20 11:37:06 -0500785 "enabling check integrity including extent data");
Stefan Behrens21adbd52011-11-09 13:44:05 +0100786 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:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400791 btrfs_info(info, "enabling check integrity");
Stefan Behrens21adbd52011-11-09 13:44:05 +0100792 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
793 break;
794 case Opt_check_integrity_print_mask:
Wang Shilong2c334e82013-07-24 10:29:05 +0800795 ret = match_int(&args[0], &intarg);
Anand Jain02453bd2018-02-13 17:50:45 +0800796 if (ret)
Wang Shilong2c334e82013-07-24 10:29:05 +0800797 goto out;
Anand Jain02453bd2018-02-13 17:50:45 +0800798 info->check_integrity_print_mask = intarg;
799 btrfs_info(info, "check_integrity_print_mask 0x%x",
800 info->check_integrity_print_mask);
Stefan Behrens21adbd52011-11-09 13:44:05 +0100801 break;
802#else
803 case Opt_check_integrity_including_extent_data:
804 case Opt_check_integrity:
805 case Opt_check_integrity_print_mask:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400806 btrfs_err(info,
807 "support for check_integrity* not compiled in!");
Stefan Behrens21adbd52011-11-09 13:44:05 +0100808 ret = -EINVAL;
809 goto out;
810#endif
Jeff Mahoney8c342932011-10-03 23:22:31 -0400811 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;
David Sterba8b87dc12013-08-01 18:14:52 +0200823 case Opt_commit_interval:
824 intarg = 0;
825 ret = match_int(&args[0], &intarg);
Anand Jaind3740602018-02-13 17:50:46 +0800826 if (ret)
David Sterba8b87dc12013-08-01 18:14:52 +0200827 goto out;
Anand Jaind3740602018-02-13 17:50:46 +0800828 if (intarg == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400829 btrfs_info(info,
Anand Jaind3740602018-02-13 17:50:46 +0800830 "using default commit interval %us",
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400831 BTRFS_DEFAULT_COMMIT_INTERVAL);
Anand Jaind3740602018-02-13 17:50:46 +0800832 intarg = BTRFS_DEFAULT_COMMIT_INTERVAL;
833 } else if (intarg > 300) {
834 btrfs_warn(info, "excessive commit interval %d",
835 intarg);
David Sterba8b87dc12013-08-01 18:14:52 +0200836 }
Anand Jaind3740602018-02-13 17:50:46 +0800837 info->commit_interval = intarg;
David Sterba8b87dc12013-08-01 18:14:52 +0200838 break;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400839#ifdef CONFIG_BTRFS_DEBUG
840 case Opt_fragment_all:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 btrfs_info(info, "fragmenting all space");
Josef Bacikd0bd4562015-09-23 14:54:14 -0400842 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
843 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
844 break;
845 case Opt_fragment_metadata:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400846 btrfs_info(info, "fragmenting metadata");
Josef Bacikd0bd4562015-09-23 14:54:14 -0400847 btrfs_set_opt(info->mount_opt,
848 FRAGMENT_METADATA);
849 break;
850 case Opt_fragment_data:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400851 btrfs_info(info, "fragmenting data");
Josef Bacikd0bd4562015-09-23 14:54:14 -0400852 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
853 break;
854#endif
Josef Bacikfb592372017-09-29 15:43:48 -0400855#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
Sage Weila7a3f7c2009-11-07 06:19:16 +0000861 case Opt_err:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400862 btrfs_info(info, "unrecognized mount option '%s'", p);
Sage Weila7a3f7c2009-11-07 06:19:16 +0000863 ret = -EINVAL;
864 goto out;
Chris Mason95e05282007-08-29 09:11:44 -0400865 default:
Chris Masonbe20aa92007-12-17 20:14:01 -0500866 break;
Chris Mason95e05282007-08-29 09:11:44 -0400867 }
868 }
Qu Wenruo96da0912016-01-19 10:23:03 +0800869check:
870 /*
871 * Extra check for current option against current flag
872 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800873 if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 btrfs_err(info,
Qu Wenruo96da0912016-01-19 10:23:03 +0800875 "nologreplay must be used with ro mount option");
876 ret = -EINVAL;
877 }
Sage Weila7a3f7c2009-11-07 06:19:16 +0000878out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400879 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400880 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
881 !btrfs_test_opt(info, CLEAR_CACHE)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400882 btrfs_err(info, "cannot disable free space tree");
Omar Sandoval70f6d822015-09-29 20:50:38 -0700883 ret = -EINVAL;
884
885 }
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400886 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400887 btrfs_info(info, "disk space caching is enabled");
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400888 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400889 btrfs_info(info, "using free space tree");
Sage Weila7a3f7c2009-11-07 06:19:16 +0000890 return ret;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400891}
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 */
Anand Jainfa59f272018-07-16 22:18:07 +0800899static int btrfs_parse_device_options(const char *options, fmode_t flags,
900 void *holder)
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400901{
902 substring_t args[MAX_OPT_ARGS];
Jeff Liu83c8c9b2011-09-14 14:11:21 +0800903 char *device_name, *opts, *orig, *p;
Gu Jinxiang36350e92018-07-12 14:23:16 +0800904 struct btrfs_device *device = NULL;
Misono, Tomohirod7407602017-12-14 17:25:28 +0900905 int error = 0;
906
David Sterba5139cff2018-06-19 17:50:25 +0200907 lockdep_assert_held(&uuid_mutex);
908
Misono, Tomohirod7407602017-12-14 17:25:28 +0900909 if (!options)
910 return 0;
911
912 /*
913 * 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 }
Gu Jinxiang36350e92018-07-12 14:23:16 +0800934 device = btrfs_scan_one_device(device_name, flags,
935 holder);
Misono, Tomohirod7407602017-12-14 17:25:28 +0900936 kfree(device_name);
Gu Jinxiang36350e92018-07-12 14:23:16 +0800937 if (IS_ERR(device)) {
938 error = PTR_ERR(device);
Misono, Tomohirod7407602017-12-14 17:25:28 +0900939 goto out;
Gu Jinxiang36350e92018-07-12 14:23:16 +0800940 }
Misono, Tomohirod7407602017-12-14 17:25:28 +0900941 }
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 */
Gu Jinxiang93b9bcd2018-07-09 14:39:15 +0800954static int btrfs_parse_subvol_options(const char *options, char **subvol_name,
955 u64 *subvol_objectid)
Misono, Tomohirod7407602017-12-14 17:25:28 +0900956{
957 substring_t args[MAX_OPT_ARGS];
958 char *opts, *orig, *p;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400959 int error = 0;
Anand Jainccb0e7d2018-02-15 01:11:37 +0800960 u64 subvolid;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400961
962 if (!options)
Josef Bacik830c4ad2011-07-25 15:55:42 -0400963 return 0;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400964
965 /*
Misono, Tomohirod7407602017-12-14 17:25:28 +0900966 * strsep changes the string, duplicate it because
Anand Jainfa59f272018-07-16 22:18:07 +0800967 * btrfs_parse_device_options gets called later
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400968 */
969 opts = kstrdup(options, GFP_KERNEL);
970 if (!opts)
971 return -ENOMEM;
Tero Roponen3f3d0bc2010-12-27 16:43:13 +0800972 orig = opts;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400973
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:
Ilya Dryomova90e8b62011-11-08 16:47:55 +0200982 kfree(*subvol_name);
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400983 *subvol_name = match_strdup(&args[0]);
Wang Shilong2c334e82013-07-24 10:29:05 +0800984 if (!*subvol_name) {
985 error = -ENOMEM;
986 goto out;
987 }
Christoph Hellwigedf24ab2008-06-10 10:40:29 -0400988 break;
Josef Bacik73f73412009-12-04 17:38:27 +0000989 case Opt_subvolid:
Anand Jainccb0e7d2018-02-15 01:11:37 +0800990 error = match_u64(&args[0], &subvolid);
991 if (error)
Wang Shilong2c334e82013-07-24 10:29:05 +0800992 goto out;
Anand Jainccb0e7d2018-02-15 01:11:37 +0800993
994 /* we want the original fs_tree */
995 if (subvolid == 0)
996 subvolid = BTRFS_FS_TREE_OBJECTID;
997
998 *subvol_objectid = subvolid;
Josef Bacik73f73412009-12-04 17:38:27 +0000999 break;
Xin Zhonge15d0542011-04-06 07:33:51 +00001000 case Opt_subvolrootid:
Jeff Mahoney62e85572016-09-20 10:05:01 -04001001 pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n");
Xin Zhonge15d0542011-04-06 07:33:51 +00001002 break;
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001003 default:
1004 break;
1005 }
1006 }
1007
Josef Bacik830c4ad2011-07-25 15:55:42 -04001008out:
Tero Roponen3f3d0bc2010-12-27 16:43:13 +08001009 kfree(orig);
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001010 return error;
Chris Mason95e05282007-08-29 09:11:44 -04001011}
1012
Marcos Paulo de Souza038580b2020-02-21 14:56:12 +01001013char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
1014 u64 subvol_objectid)
Josef Bacik73f73412009-12-04 17:38:27 +00001015{
Al Viro815745c2011-11-17 15:40:49 -05001016 struct btrfs_root *root = fs_info->tree_root;
Omar Sandoval05dbe682015-05-18 02:16:30 -07001017 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
David Sterba3ec83622017-06-22 02:26:54 +02001034 name = kmalloc(PATH_MAX, GFP_KERNEL);
Omar Sandoval05dbe682015-05-18 02:16:30 -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;
Josef Bacik73f73412009-12-04 17:38:27 +00001152 struct btrfs_dir_item *di;
1153 struct btrfs_path *path;
1154 struct btrfs_key location;
Josef Bacik73f73412009-12-04 17:38:27 +00001155 u64 dir_id;
Josef Bacik73f73412009-12-04 17:38:27 +00001156
1157 path = btrfs_alloc_path();
1158 if (!path)
Omar Sandoval05dbe682015-05-18 02:16:30 -07001159 return -ENOMEM;
Josef Bacik73f73412009-12-04 17:38:27 +00001160 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 */
Al Viro815745c2011-11-17 15:40:49 -05001167 dir_id = btrfs_super_root_dir(fs_info->super_copy);
Josef Bacik73f73412009-12-04 17:38:27 +00001168 di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
Julia Lawallb0839162011-05-14 07:10:51 +00001169 if (IS_ERR(di)) {
1170 btrfs_free_path(path);
Omar Sandoval05dbe682015-05-18 02:16:30 -07001171 return PTR_ERR(di);
Julia Lawallb0839162011-05-14 07:10:51 +00001172 }
Josef Bacik73f73412009-12-04 17:38:27 +00001173 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
Omar Sandoval05dbe682015-05-18 02:16:30 -07001177 * mount the top-level subvolume.
Josef Bacik73f73412009-12-04 17:38:27 +00001178 */
1179 btrfs_free_path(path);
Omar Sandoval05dbe682015-05-18 02:16:30 -07001180 *objectid = BTRFS_FS_TREE_OBJECTID;
1181 return 0;
Josef Bacik73f73412009-12-04 17:38:27 +00001182 }
1183
1184 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
1185 btrfs_free_path(path);
Omar Sandoval05dbe682015-05-18 02:16:30 -07001186 *objectid = location.objectid;
1187 return 0;
Josef Bacik73f73412009-12-04 17:38:27 +00001188}
1189
Chris Masond3977122009-01-05 21:25:51 -05001190static int btrfs_fill_super(struct super_block *sb,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001191 struct btrfs_fs_devices *fs_devices,
David Sterba56e033a2017-02-10 19:44:31 +01001192 void *data)
Chris Mason2e635a22007-03-21 11:12:56 -04001193{
Chris Masond3977122009-01-05 21:25:51 -05001194 struct inode *inode;
Al Viro815745c2011-11-17 15:40:49 -05001195 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001196 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -04001197 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001198
1199 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -04001200 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -04001201 sb->s_op = &btrfs_super_ops;
Al Viroaf53d292010-12-20 10:56:06 -05001202 sb->s_d_op = &btrfs_dentry_operations;
Balaji Raobe6e8dc2008-07-21 02:01:56 +05301203 sb->s_export_op = &btrfs_export_ops;
Josef Bacik5103e942007-11-16 11:45:54 -05001204 sb->s_xattr = btrfs_xattr_handlers;
Chris Mason2e635a22007-03-21 11:12:56 -04001205 sb->s_time_gran = 1;
Chris Mason0eda2942009-10-13 13:50:18 -04001206#ifdef CONFIG_BTRFS_FS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001207 sb->s_flags |= SB_POSIXACL;
Chris Ball49cf6f42009-09-29 13:51:04 -04001208#endif
Matthew Garrett357fdad2017-10-18 13:56:26 -07001209 sb->s_flags |= SB_I_VERSION;
Chris Masonda2f0f72015-07-02 13:57:22 -07001210 sb->s_iflags |= SB_I_CGROUPWB;
Jan Kara9e11cee2017-04-12 12:24:32 +02001211
1212 err = super_setup_bdi(sb);
1213 if (err) {
1214 btrfs_err(fs_info, "super_setup_bdi failed");
1215 return err;
1216 }
1217
Al Viroad2b2c82011-11-17 01:10:02 -05001218 err = open_ctree(sb, fs_devices, (char *)data);
1219 if (err) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001220 btrfs_err(fs_info, "open_ctree failed");
Al Viroad2b2c82011-11-17 01:10:02 -05001221 return err;
Chris Masone20d96d2007-03-22 12:13:20 -04001222 }
Chris Masonb888db22007-08-27 16:49:44 -04001223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001224 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1225 key.type = BTRFS_INODE_ITEM_KEY;
1226 key.offset = 0;
Al Viro98c70892011-11-17 01:00:31 -05001227 inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 if (IS_ERR(inode)) {
1229 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001230 goto fail_close;
1231 }
Chris Mason2e635a22007-03-21 11:12:56 -04001232
Al Viro48fde702012-01-08 22:15:13 -05001233 sb->s_root = d_make_root(inode);
1234 if (!sb->s_root) {
Chris Mason39279cc2007-06-12 06:35:45 -04001235 err = -ENOMEM;
1236 goto fail_close;
Chris Mason2e635a22007-03-21 11:12:56 -04001237 }
Josef Bacik58176a92007-08-29 15:47:34 -04001238
Dan Magenheimer90a887c2011-05-26 10:01:56 -06001239 cleancache_init_fs(sb);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001240 sb->s_flags |= SB_ACTIVE;
Chris Mason2e635a22007-03-21 11:12:56 -04001241 return 0;
Chris Mason2e635a22007-03-21 11:12:56 -04001242
Chris Mason39279cc2007-06-12 06:35:45 -04001243fail_close:
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04001244 close_ctree(fs_info);
Chris Masond5719762007-03-23 10:01:08 -04001245 return err;
1246}
1247
Sage Weil6bf13c02008-06-10 10:07:39 -04001248int btrfs_sync_fs(struct super_block *sb, int wait)
Chris Masond5719762007-03-23 10:01:08 -04001249{
1250 struct btrfs_trans_handle *trans;
Al Viro815745c2011-11-17 15:40:49 -05001251 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1252 struct btrfs_root *root = fs_info->tree_root;
Chris Masondf2ce342007-03-23 11:00:45 -04001253
Jeff Mahoneybc074522016-06-09 17:27:55 -04001254 trace_btrfs_sync_fs(fs_info, wait);
liubo1abe9b82011-03-24 11:18:59 +00001255
Chris Masond561c022007-03-23 19:47:49 -04001256 if (!wait) {
Al Viro815745c2011-11-17 15:40:49 -05001257 filemap_flush(fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -04001258 return 0;
1259 }
Chris Mason771ed682008-11-06 22:02:51 -05001260
Chris Mason6374e57a2017-06-23 09:48:21 -07001261 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1);
Chris Mason771ed682008-11-06 22:02:51 -05001262
Miao Xied4edf392013-02-20 09:17:06 +00001263 trans = btrfs_attach_transaction_barrier(root);
Josef Bacik60376ce2012-09-14 10:34:40 -04001264 if (IS_ERR(trans)) {
Miao Xie354aa0f2012-09-20 01:54:00 -06001265 /* no transaction, don't bother */
David Sterba6b5fe462014-03-28 17:38:48 +01001266 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;
Qu Wenruoa53f4f82015-01-19 15:42:41 +08001273 /*
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 */
Rakesh Pandita7e3c5f2017-10-10 13:48:05 +03001279 if (sb_start_write_trylock(sb))
1280 sb_end_write(sb);
Qu Wenruoa53f4f82015-01-19 15:42:41 +08001281 else
1282 return 0;
David Sterba6b5fe462014-03-28 17:38:48 +01001283 trans = btrfs_start_transaction(root, 0);
David Sterba6b5fe462014-03-28 17:38:48 +01001284 }
David Sterba98bd5c52015-01-19 14:21:02 +01001285 if (IS_ERR(trans))
1286 return PTR_ERR(trans);
Josef Bacik60376ce2012-09-14 10:34:40 -04001287 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001288 return btrfs_commit_transaction(trans);
Chris Masond5719762007-03-23 10:01:08 -04001289}
1290
Al Viro34c80b12011-12-08 21:32:45 -05001291static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
Eric Parisa9572a12009-04-02 16:46:06 -04001292{
Al Viro815745c2011-11-17 15:40:49 -05001293 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
David Sterba0f628c62017-10-31 18:06:34 +01001294 const char *compress_type;
Josef Bacikde88b7e2020-07-22 11:12:46 -04001295 const char *subvol_name;
Eric Parisa9572a12009-04-02 16:46:06 -04001296
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001297 if (btrfs_test_opt(info, DEGRADED))
Eric Parisa9572a12009-04-02 16:46:06 -04001298 seq_puts(seq, ",degraded");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001299 if (btrfs_test_opt(info, NODATASUM))
Eric Parisa9572a12009-04-02 16:46:06 -04001300 seq_puts(seq, ",nodatasum");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001301 if (btrfs_test_opt(info, NODATACOW))
Eric Parisa9572a12009-04-02 16:46:06 -04001302 seq_puts(seq, ",nodatacow");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001303 if (btrfs_test_opt(info, NOBARRIER))
Eric Parisa9572a12009-04-02 16:46:06 -04001304 seq_puts(seq, ",nobarrier");
Filipe David Borba Manana95ac5672013-08-08 22:45:48 +01001305 if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE)
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001306 seq_printf(seq, ",max_inline=%llu", info->max_inline);
Eric Parisa9572a12009-04-02 16:46:06 -04001307 if (info->thread_pool_size != min_t(unsigned long,
1308 num_online_cpus() + 2, 8))
Anand Jainf7b885b2018-02-13 17:50:42 +08001309 seq_printf(seq, ",thread_pool=%u", info->thread_pool_size);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001310 if (btrfs_test_opt(info, COMPRESS)) {
David Sterba0f628c62017-10-31 18:06:34 +01001311 compress_type = btrfs_compress_type2str(info->compress_type);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001312 if (btrfs_test_opt(info, FORCE_COMPRESS))
Tsutomu Itoh200da642011-03-31 00:44:29 +00001313 seq_printf(seq, ",compress-force=%s", compress_type);
1314 else
1315 seq_printf(seq, ",compress=%s", compress_type);
David Sterbaf51d2b52017-09-15 17:36:57 +02001316 if (info->compress_level)
Adam Borowskifa4d8852017-09-15 17:36:58 +02001317 seq_printf(seq, ":%d", info->compress_level);
Tsutomu Itoh200da642011-03-31 00:44:29 +00001318 }
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001319 if (btrfs_test_opt(info, NOSSD))
Chris Masonc2898112009-06-10 09:51:32 -04001320 seq_puts(seq, ",nossd");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001321 if (btrfs_test_opt(info, SSD_SPREAD))
Chris Mason451d7582009-06-09 20:28:34 -04001322 seq_puts(seq, ",ssd_spread");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001323 else if (btrfs_test_opt(info, SSD))
Eric Parisa9572a12009-04-02 16:46:06 -04001324 seq_puts(seq, ",ssd");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001325 if (btrfs_test_opt(info, NOTREELOG))
Sage Weil6b65c5c2009-05-14 13:52:21 -04001326 seq_puts(seq, ",notreelog");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001327 if (btrfs_test_opt(info, NOLOGREPLAY))
Qu Wenruo96da0912016-01-19 10:23:03 +08001328 seq_puts(seq, ",nologreplay");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001329 if (btrfs_test_opt(info, FLUSHONCOMMIT))
Sage Weil6b65c5c2009-05-14 13:52:21 -04001330 seq_puts(seq, ",flushoncommit");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001331 if (btrfs_test_opt(info, DISCARD))
Matthew Wilcox20a52392009-12-14 22:01:12 +00001332 seq_puts(seq, ",discard");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001333 if (!(info->sb->s_flags & SB_POSIXACL))
Eric Parisa9572a12009-04-02 16:46:06 -04001334 seq_puts(seq, ",noacl");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001335 if (btrfs_test_opt(info, SPACE_CACHE))
Tsutomu Itoh200da642011-03-31 00:44:29 +00001336 seq_puts(seq, ",space_cache");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001337 else if (btrfs_test_opt(info, FREE_SPACE_TREE))
Omar Sandoval70f6d822015-09-29 20:50:38 -07001338 seq_puts(seq, ",space_cache=v2");
Josef Bacik73bc1872011-10-03 14:07:49 -04001339 else
David Sterba89655932011-11-11 10:14:57 -05001340 seq_puts(seq, ",nospace_cache");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001341 if (btrfs_test_opt(info, RESCAN_UUID_TREE))
Stefan Behrensf420ee12013-08-15 17:11:24 +02001342 seq_puts(seq, ",rescan_uuid_tree");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001343 if (btrfs_test_opt(info, CLEAR_CACHE))
Tsutomu Itoh200da642011-03-31 00:44:29 +00001344 seq_puts(seq, ",clear_cache");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001345 if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED))
Tsutomu Itoh200da642011-03-31 00:44:29 +00001346 seq_puts(seq, ",user_subvol_rm_allowed");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001347 if (btrfs_test_opt(info, ENOSPC_DEBUG))
David Sterba0942caa2011-06-28 15:10:37 +00001348 seq_puts(seq, ",enospc_debug");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001349 if (btrfs_test_opt(info, AUTO_DEFRAG))
David Sterba0942caa2011-06-28 15:10:37 +00001350 seq_puts(seq, ",autodefrag");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001351 if (btrfs_test_opt(info, INODE_MAP_CACHE))
David Sterba0942caa2011-06-28 15:10:37 +00001352 seq_puts(seq, ",inode_cache");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001353 if (btrfs_test_opt(info, SKIP_BALANCE))
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02001354 seq_puts(seq, ",skip_balance");
Wang Shilong8507d212013-07-24 10:30:02 +08001355#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001356 if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA))
Wang Shilong8507d212013-07-24 10:30:02 +08001357 seq_puts(seq, ",check_int_data");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001358 else if (btrfs_test_opt(info, CHECK_INTEGRITY))
Wang Shilong8507d212013-07-24 10:30:02 +08001359 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)
Anand Jain764cb8b2018-02-13 17:50:44 +08001365 seq_printf(seq, ",metadata_ratio=%u", info->metadata_ratio);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001366 if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR))
Jeff Mahoney8c342932011-10-03 23:22:31 -04001367 seq_puts(seq, ",fatal_errors=panic");
David Sterba8b87dc12013-08-01 18:14:52 +02001368 if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL)
Anand Jaind3740602018-02-13 17:50:46 +08001369 seq_printf(seq, ",commit=%u", info->commit_interval);
Josef Bacikd0bd4562015-09-23 14:54:14 -04001370#ifdef CONFIG_BTRFS_DEBUG
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001371 if (btrfs_test_opt(info, FRAGMENT_DATA))
Josef Bacikd0bd4562015-09-23 14:54:14 -04001372 seq_puts(seq, ",fragment=data");
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001373 if (btrfs_test_opt(info, FRAGMENT_METADATA))
Josef Bacikd0bd4562015-09-23 14:54:14 -04001374 seq_puts(seq, ",fragment=metadata");
1375#endif
Josef Bacikfb592372017-09-29 15:43:48 -04001376 if (btrfs_test_opt(info, REF_VERIFY))
1377 seq_puts(seq, ",ref_verify");
Omar Sandovalc8d3fe02015-05-18 02:16:31 -07001378 seq_printf(seq, ",subvolid=%llu",
1379 BTRFS_I(d_inode(dentry))->root->root_key.objectid);
Josef Bacikde88b7e2020-07-22 11:12:46 -04001380 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 }
Eric Parisa9572a12009-04-02 16:46:06 -04001387 return 0;
1388}
1389
Chris Masona061fc82008-05-07 11:43:44 -04001390static int btrfs_test_super(struct super_block *s, void *data)
Chris Mason2e635a22007-03-21 11:12:56 -04001391{
Al Viro815745c2011-11-17 15:40:49 -05001392 struct btrfs_fs_info *p = data;
1393 struct btrfs_fs_info *fs_info = btrfs_sb(s);
Yan4b82d6e2007-08-29 09:11:44 -04001394
Al Viro815745c2011-11-17 15:40:49 -05001395 return fs_info->fs_devices == p->fs_devices;
Yan4b82d6e2007-08-29 09:11:44 -04001396}
1397
Josef Bacik450ba0e2010-11-19 14:59:15 -05001398static int btrfs_set_super(struct super_block *s, void *data)
1399{
Al Viro6de1d092011-11-17 01:29:09 -05001400 int err = set_anon_super(s, data);
1401 if (!err)
1402 s->s_fs_info = data;
1403 return err;
Josef Bacik450ba0e2010-11-19 14:59:15 -05001404}
1405
Josef Bacik830c4ad2011-07-25 15:55:42 -04001406/*
David Sterbaf9d9ef62011-09-29 13:11:33 +02001407 * 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
Omar Sandovalbb289b72015-05-18 02:16:29 -07001416static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid,
Anand Jainae0bc862019-03-29 14:03:17 +08001417 struct vfsmount *mnt)
Josef Bacik830c4ad2011-07-25 15:55:42 -04001418{
Josef Bacik830c4ad2011-07-25 15:55:42 -04001419 struct dentry *root;
Omar Sandovalfa330652015-05-18 02:16:28 -07001420 int ret;
Josef Bacik830c4ad2011-07-25 15:55:42 -04001421
Omar Sandoval05dbe682015-05-18 02:16:30 -07001422 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 }
Marcos Paulo de Souza038580b2020-02-21 14:56:12 +01001431 subvol_name = btrfs_get_subvol_name_from_objectid(
1432 btrfs_sb(mnt->mnt_sb), subvol_objectid);
Omar Sandoval05dbe682015-05-18 02:16:30 -07001433 if (IS_ERR(subvol_name)) {
1434 root = ERR_CAST(subvol_name);
1435 subvol_name = NULL;
1436 goto out;
1437 }
1438
1439 }
1440
Al Viroea441d12011-11-16 21:43:59 -05001441 root = mount_subtree(mnt, subvol_name);
Omar Sandovalfa330652015-05-18 02:16:28 -07001442 /* mount_subtree() drops our reference on the vfsmount. */
1443 mnt = NULL;
Josef Bacik830c4ad2011-07-25 15:55:42 -04001444
Omar Sandovalbb289b72015-05-18 02:16:29 -07001445 if (!IS_ERR(root)) {
Al Viroea441d12011-11-16 21:43:59 -05001446 struct super_block *s = root->d_sb;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001447 struct btrfs_fs_info *fs_info = btrfs_sb(s);
Omar Sandovalbb289b72015-05-18 02:16:29 -07001448 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)) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001453 btrfs_err(fs_info, "'%s' is not a valid subvolume",
Omar Sandovalbb289b72015-05-18 02:16:29 -07001454 subvol_name);
1455 ret = -EINVAL;
1456 }
1457 if (subvol_objectid && root_objectid != subvol_objectid) {
Omar Sandoval05dbe682015-05-18 02:16:30 -07001458 /*
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 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04001463 btrfs_err(fs_info,
1464 "subvol '%s' does not match subvolid %llu",
1465 subvol_name, subvol_objectid);
Omar Sandovalbb289b72015-05-18 02:16:29 -07001466 ret = -EINVAL;
1467 }
1468 if (ret) {
1469 dput(root);
1470 root = ERR_PTR(ret);
1471 deactivate_locked_super(s);
1472 }
David Sterbaf9d9ef62011-09-29 13:11:33 +02001473 }
1474
Omar Sandovalfa330652015-05-18 02:16:28 -07001475out:
1476 mntput(mnt);
Omar Sandovalfa330652015-05-18 02:16:28 -07001477 kfree(subvol_name);
Josef Bacik830c4ad2011-07-25 15:55:42 -04001478 return root;
1479}
Josef Bacik450ba0e2010-11-19 14:59:15 -05001480
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001481/*
1482 * Find a superblock for the given device / mount point.
1483 *
1484 * 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.
1486 */
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001487static struct dentry *btrfs_mount_root(struct file_system_type *fs_type,
1488 int flags, const char *device_name, void *data)
1489{
1490 struct block_device *bdev = NULL;
1491 struct super_block *s;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001492 struct btrfs_device *device = NULL;
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001493 struct btrfs_fs_devices *fs_devices = NULL;
1494 struct btrfs_fs_info *fs_info = NULL;
Al Viro204cc0c2018-12-13 13:41:47 -05001495 void *new_sec_opts = NULL;
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001496 fmode_t mode = FMODE_READ;
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001497 int error = 0;
1498
1499 if (!(flags & SB_RDONLY))
1500 mode |= FMODE_WRITE;
1501
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001502 if (data) {
Al Viroa65001e2018-12-10 17:19:21 -05001503 error = security_sb_eat_lsm_opts(data, &new_sec_opts);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001504 if (error)
1505 return ERR_PTR(error);
1506 }
1507
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001508 /*
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 */
Jeff Mahoneya8fd1f72018-02-15 22:59:47 -05001514 fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001515 if (!fs_info) {
1516 error = -ENOMEM;
1517 goto error_sec_opts;
1518 }
1519
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001520 fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
1521 fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001522 if (!fs_info->super_copy || !fs_info->super_for_commit) {
1523 error = -ENOMEM;
1524 goto error_fs_info;
1525 }
1526
David Sterbaf5194e32018-06-19 17:09:47 +02001527 mutex_lock(&uuid_mutex);
Anand Jainfa59f272018-07-16 22:18:07 +08001528 error = btrfs_parse_device_options(data, mode, fs_type);
David Sterba81ffd562018-06-19 18:04:07 +02001529 if (error) {
1530 mutex_unlock(&uuid_mutex);
David Sterba399f7f42018-06-19 18:01:24 +02001531 goto error_fs_info;
David Sterba81ffd562018-06-19 18:04:07 +02001532 }
David Sterba399f7f42018-06-19 18:01:24 +02001533
Gu Jinxiang36350e92018-07-12 14:23:16 +08001534 device = btrfs_scan_one_device(device_name, mode, fs_type);
1535 if (IS_ERR(device)) {
David Sterba81ffd562018-06-19 18:04:07 +02001536 mutex_unlock(&uuid_mutex);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001537 error = PTR_ERR(device);
David Sterba399f7f42018-06-19 18:01:24 +02001538 goto error_fs_info;
David Sterba81ffd562018-06-19 18:04:07 +02001539 }
David Sterba399f7f42018-06-19 18:01:24 +02001540
Gu Jinxiang36350e92018-07-12 14:23:16 +08001541 fs_devices = device->fs_devices;
David Sterba399f7f42018-06-19 18:01:24 +02001542 fs_info->fs_devices = fs_devices;
1543
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001544 error = btrfs_open_devices(fs_devices, mode, fs_type);
David Sterbaf5194e32018-06-19 17:09:47 +02001545 mutex_unlock(&uuid_mutex);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001546 if (error)
1547 goto error_fs_info;
1548
1549 if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
1550 error = -EACCES;
1551 goto error_close_devices;
1552 }
1553
1554 bdev = fs_devices->latest_bdev;
1555 s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC,
1556 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);
1565 if ((flags ^ s->s_flags) & SB_RDONLY)
1566 error = -EBUSY;
1567 } else {
1568 snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev);
1569 btrfs_sb(s)->bdev_holder = fs_type;
David Sterba9b4e6752019-05-16 13:39:59 +02001570 if (!strstr(crc32c_impl(), "generic"))
1571 set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001572 error = btrfs_fill_super(s, fs_devices, data);
1573 }
Al Viroa65001e2018-12-10 17:19:21 -05001574 if (!error)
Al Viro204cc0c2018-12-13 13:41:47 -05001575 error = security_sb_set_mnt_opts(s, new_sec_opts, 0, NULL);
Al Viroa65001e2018-12-10 17:19:21 -05001576 security_free_mnt_opts(&new_sec_opts);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001577 if (error) {
1578 deactivate_locked_super(s);
Al Viroa65001e2018-12-10 17:19:21 -05001579 return ERR_PTR(error);
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09001580 }
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}
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001592
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001593/*
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001594 * Mount function which is called by VFS layer.
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001595 *
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001596 * 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.
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001614 */
Al Viro061dbc62010-07-26 16:21:33 +04001615static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
David Sterba306e16c2011-04-19 14:29:38 +02001616 const char *device_name, void *data)
Yan4b82d6e2007-08-29 09:11:44 -04001617{
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001618 struct vfsmount *mnt_root;
1619 struct dentry *root;
Josef Bacik73f73412009-12-04 17:38:27 +00001620 char *subvol_name = NULL;
1621 u64 subvol_objectid = 0;
Yan4b82d6e2007-08-29 09:11:44 -04001622 int error = 0;
1623
Gu Jinxiang93b9bcd2018-07-09 14:39:15 +08001624 error = btrfs_parse_subvol_options(data, &subvol_name,
1625 &subvol_objectid);
Ilya Dryomovf23c8af2011-11-08 19:15:05 +02001626 if (error) {
1627 kfree(subvol_name);
Al Viro061dbc62010-07-26 16:21:33 +04001628 return ERR_PTR(error);
Ilya Dryomovf23c8af2011-11-08 19:15:05 +02001629 }
Christoph Hellwigedf24ab2008-06-10 10:40:29 -04001630
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001631 /* 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);
Eric W. Biederman532b6182019-01-30 07:54:12 -06001642 kfree(subvol_name);
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001643 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);
Eric W. Biederman532b6182019-01-30 07:54:12 -06001652 kfree(subvol_name);
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001653 goto out;
1654 }
1655 }
1656 }
1657 if (IS_ERR(mnt_root)) {
1658 root = ERR_CAST(mnt_root);
Eric W. Biederman532b6182019-01-30 07:54:12 -06001659 kfree(subvol_name);
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001660 goto out;
Josef Bacik830c4ad2011-07-25 15:55:42 -04001661 }
1662
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001663 /* mount_subvol() will free subvol_name and mnt_root */
Anand Jainae0bc862019-03-29 14:03:17 +08001664 root = mount_subvol(subvol_name, subvol_objectid, mnt_root);
Qu Wenruof667aef2014-09-23 13:40:08 +08001665
Misono, Tomohiro312c89f2017-12-14 17:25:01 +09001666out:
1667 return root;
Yan4b82d6e2007-08-29 09:11:44 -04001668}
Chris Mason2e635a22007-03-21 11:12:56 -04001669
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001670static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
Anand Jainf7b885b2018-02-13 17:50:42 +08001671 u32 new_pool_size, u32 old_pool_size)
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001672{
1673 if (new_pool_size == old_pool_size)
1674 return;
1675
1676 fs_info->thread_pool_size = new_pool_size;
1677
Frank Holtonefe120a2013-12-20 11:37:06 -05001678 btrfs_info(fs_info, "resize thread pool %d -> %d",
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001679 old_pool_size, new_pool_size);
1680
Qu Wenruo5cdc7ad32014-02-28 10:46:06 +08001681 btrfs_workqueue_set_max(fs_info->workers, new_pool_size);
Qu Wenruoafe3d242014-02-28 10:46:07 +08001682 btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08001683 btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +08001684 btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size);
Qu Wenruofccb5d82014-02-28 10:46:10 +08001685 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);
Qu Wenruo5b3bc442014-02-28 10:46:15 +08001691 btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size);
Qu Wenruo736cfa12014-02-28 10:46:13 +08001692 btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001693 btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers,
1694 new_pool_size);
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001695}
1696
Miao Xief42a34b2013-04-11 10:29:35 +00001697static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
Miao Xiedc81cdc2013-02-20 23:32:52 -07001698{
1699 set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
Miao Xief42a34b2013-04-11 10:29:35 +00001700}
Miao Xiedc81cdc2013-02-20 23:32:52 -07001701
Miao Xief42a34b2013-04-11 10:29:35 +00001702static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1703 unsigned long old_opts, int flags)
1704{
Miao Xiedc81cdc2013-02-20 23:32:52 -07001705 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1706 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001707 (flags & SB_RDONLY))) {
Miao Xiedc81cdc2013-02-20 23:32:52 -07001708 /* wait for any defraggers to finish */
1709 wait_event(fs_info->transaction_wait,
1710 (atomic_read(&fs_info->defrag_running) == 0));
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001711 if (flags & SB_RDONLY)
Miao Xiedc81cdc2013-02-20 23:32:52 -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 /*
Luis de Bethencourt180e4d42016-04-04 15:31:22 +01001720 * We need to cleanup all defragable inodes if the autodefragment is
1721 * close or the filesystem is read only.
Miao Xiedc81cdc2013-02-20 23:32:52 -07001722 */
1723 if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
David Howellsbc98a422017-07-17 08:45:34 +01001724 (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) {
Miao Xiedc81cdc2013-02-20 23:32:52 -07001725 btrfs_cleanup_defrag_inodes(fs_info);
1726 }
1727
1728 clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1729}
1730
Yan Zhengc146afa2008-11-12 14:34:12 -05001731static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1732{
Al Viro815745c2011-11-17 15:40:49 -05001733 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1734 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001735 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;
Anand Jainf7b885b2018-02-13 17:50:42 +08001739 u32 old_thread_pool_size = fs_info->thread_pool_size;
Anand Jaind612ac52018-02-26 16:46:05 +08001740 u32 old_metadata_ratio = fs_info->metadata_ratio;
Yan Zhengc146afa2008-11-12 14:34:12 -05001741 int ret;
1742
Theodore Ts'o02b99842014-03-13 10:14:33 -04001743 sync_filesystem(sb);
Miao Xief42a34b2013-04-11 10:29:35 +00001744 btrfs_remount_prepare(fs_info);
Miao Xiedc81cdc2013-02-20 23:32:52 -07001745
Qu Wenruof667aef2014-09-23 13:40:08 +08001746 if (data) {
Al Viro204cc0c2018-12-13 13:41:47 -05001747 void *new_sec_opts = NULL;
Qu Wenruof667aef2014-09-23 13:40:08 +08001748
Al Viroa65001e2018-12-10 17:19:21 -05001749 ret = security_sb_eat_lsm_opts(data, &new_sec_opts);
1750 if (!ret)
Al Viro204cc0c2018-12-13 13:41:47 -05001751 ret = security_sb_remount(sb, new_sec_opts);
Al Viroa65001e2018-12-10 17:19:21 -05001752 security_free_mnt_opts(&new_sec_opts);
Qu Wenruof667aef2014-09-23 13:40:08 +08001753 if (ret)
1754 goto restore;
Qu Wenruof667aef2014-09-23 13:40:08 +08001755 }
1756
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001757 ret = btrfs_parse_options(fs_info, data, *flags);
Chengguang Xu891f41c2018-05-09 21:08:23 +08001758 if (ret)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001759 goto restore;
Chris Masonb2880522009-02-12 09:37:35 -05001760
Miao Xief42a34b2013-04-11 10:29:35 +00001761 btrfs_remount_begin(fs_info, old_opts, *flags);
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001762 btrfs_resize_thread_pool(fs_info,
1763 fs_info->thread_pool_size, old_thread_pool_size);
1764
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001765 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
Miao Xiedc81cdc2013-02-20 23:32:52 -07001766 goto out;
Yan Zhengc146afa2008-11-12 14:34:12 -05001767
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001768 if (*flags & SB_RDONLY) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001769 /*
1770 * this also happens on 'umount -rf' or on shutdown, when
1771 * the filesystem is busy.
1772 */
Miao Xie21c7e752014-05-13 17:29:04 -07001773 cancel_work_sync(&fs_info->async_reclaim_work);
Stefan Behrens361c0932013-10-11 17:14:58 +02001774
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
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001780 sb->s_flags |= SB_RDONLY;
Yan Zhengc146afa2008-11-12 14:34:12 -05001781
Jeff Mahoneye44163e2015-06-15 09:41:18 -04001782 /*
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001783 * Setting SB_RDONLY will put the cleaner thread to
Jeff Mahoneye44163e2015-06-15 09:41:18 -04001784 * 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 */
Jeff Mahoneye44163e2015-06-15 09:41:18 -04001789 btrfs_delete_unused_bgs(fs_info);
Jeff Mahoneye44163e2015-06-15 09:41:18 -04001790
Stefan Behrens8dabb742012-11-06 13:15:27 +01001791 btrfs_dev_replace_suspend_for_unmount(fs_info);
1792 btrfs_scrub_cancel(fs_info);
Miao Xie061594e2013-05-15 07:48:17 +00001793 btrfs_pause_balance(fs_info);
Stefan Behrens8dabb742012-11-06 13:15:27 +01001794
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04001795 ret = btrfs_commit_super(fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001796 if (ret)
1797 goto restore;
Yan Zhengc146afa2008-11-12 14:34:12 -05001798 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001799 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
David Sterba6ef3de92013-09-13 17:41:20 +02001800 btrfs_err(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -05001801 "Remounting read-write after error is not allowed");
David Sterba6ef3de92013-09-13 17:41:20 +02001802 ret = -EINVAL;
1803 goto restore;
1804 }
Sergei Trofimovich8a3db182012-04-16 06:44:37 +03001805 if (fs_info->fs_devices->rw_devices == 0) {
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001806 ret = -EACCES;
1807 goto restore;
Sergei Trofimovich8a3db182012-04-16 06:44:37 +03001808 }
Yan Zheng2b820322008-11-17 21:11:30 -05001809
Anand Jain6528b992017-12-18 17:08:59 +08001810 if (!btrfs_check_rw_degradable(fs_info, NULL)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001811 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01001812 "too many missing devices, writable remount is not allowed");
Stefan Behrens292fd7f2012-10-30 17:16:16 +00001813 ret = -EACCES;
1814 goto restore;
1815 }
1816
Sergei Trofimovich8a3db182012-04-16 06:44:37 +03001817 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
David Sterbacafaf6b2020-02-05 17:12:28 +01001818 btrfs_warn(fs_info,
1819 "mount required to replay tree-log, cannot remount read-write");
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001820 ret = -EINVAL;
1821 goto restore;
Sergei Trofimovich8a3db182012-04-16 06:44:37 +03001822 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001823
Al Viro815745c2011-11-17 15:40:49 -05001824 ret = btrfs_cleanup_fs_roots(fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001825 if (ret)
1826 goto restore;
Yan Zhengc146afa2008-11-12 14:34:12 -05001827
Yan, Zhengd68fc572010-05-16 10:49:58 -04001828 /* recover relocation */
Wang Shilong5f316482014-06-26 11:08:16 +08001829 mutex_lock(&fs_info->cleaner_mutex);
Yan, Zhengd68fc572010-05-16 10:49:58 -04001830 ret = btrfs_recover_relocation(root);
Wang Shilong5f316482014-06-26 11:08:16 +08001831 mutex_unlock(&fs_info->cleaner_mutex);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001832 if (ret)
1833 goto restore;
Yan Zhengc146afa2008-11-12 14:34:12 -05001834
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06001835 ret = btrfs_resume_balance_async(fs_info);
1836 if (ret)
1837 goto restore;
1838
Stefan Behrens8dabb742012-11-06 13:15:27 +01001839 ret = btrfs_resume_dev_replace_async(fs_info);
1840 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001841 btrfs_warn(fs_info, "failed to resume dev_replace");
Stefan Behrens8dabb742012-11-06 13:15:27 +01001842 goto restore;
1843 }
Josef Bacik94aebfb2013-09-20 22:33:20 -04001844
Aleksa Sarai6c6b5a32017-07-04 21:49:06 +10001845 btrfs_qgroup_rescan_resume(fs_info);
1846
Josef Bacik94aebfb2013-09-20 22:33:20 -04001847 if (!fs_info->uuid_root) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001848 btrfs_info(fs_info, "creating UUID tree");
Josef Bacik94aebfb2013-09-20 22:33:20 -04001849 ret = btrfs_create_uuid_tree(fs_info);
1850 if (ret) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001851 btrfs_warn(fs_info,
1852 "failed to create the UUID tree %d",
1853 ret);
Josef Bacik94aebfb2013-09-20 22:33:20 -04001854 goto restore;
1855 }
1856 }
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001857 sb->s_flags &= ~SB_RDONLY;
Zygo Blaxell90c711a2016-06-12 23:39:58 -04001858
Josef Bacikafcdd122016-09-02 15:40:02 -04001859 set_bit(BTRFS_FS_OPEN, &fs_info->flags);
Yan Zhengc146afa2008-11-12 14:34:12 -05001860 }
Miao Xiedc81cdc2013-02-20 23:32:52 -07001861out:
Josef Bacikd2569922020-07-30 11:18:09 -04001862 /*
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
Justin Maggard2c6a92b02014-02-20 08:48:07 -08001868 wake_up_process(fs_info->transaction_kthread);
Miao Xiedc81cdc2013-02-20 23:32:52 -07001869 btrfs_remount_cleanup(fs_info, old_opts);
Yan Zhengc146afa2008-11-12 14:34:12 -05001870 return 0;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001871
1872restore:
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001873 /* We've hit an error - don't reset SB_RDONLY */
David Howellsbc98a422017-07-17 08:45:34 +01001874 if (sb_rdonly(sb))
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001875 old_flags |= SB_RDONLY;
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001876 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;
Sergei Trofimovich0d2450a2012-04-24 22:59:16 +03001880 btrfs_resize_thread_pool(fs_info,
1881 old_thread_pool_size, fs_info->thread_pool_size);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001882 fs_info->metadata_ratio = old_metadata_ratio;
Miao Xiedc81cdc2013-02-20 23:32:52 -07001883 btrfs_remount_cleanup(fs_info, old_opts);
Jeff Mahoney49b25e02012-03-01 17:24:58 +01001884 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05001885}
1886
Arne Jansenbcd53742011-04-12 10:43:21 +02001887/* Used to sort the devices by max_avail(descending sort) */
Arnd Bergmann7e179162018-11-03 16:39:28 +01001888static inline int btrfs_cmp_device_free_bytes(const void *dev_info1,
Arne Jansenbcd53742011-04-12 10:43:21 +02001889 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
Miao Xie6d07bce2011-01-05 10:07:31 +00001913/*
1914 * The helper to calc the free space on the devices that can be used to store
1915 * file data.
1916 */
Arnd Bergmann7e179162018-11-03 16:39:28 +01001917static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
1918 u64 *free_bytes)
Miao Xie6d07bce2011-01-05 10:07:31 +00001919{
Miao Xie6d07bce2011-01-05 10:07:31 +00001920 struct btrfs_device_info *devices_info;
1921 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1922 struct btrfs_device *device;
Miao Xie6d07bce2011-01-05 10:07:31 +00001923 u64 type;
1924 u64 avail_space;
Miao Xie6d07bce2011-01-05 10:07:31 +00001925 u64 min_stripe_size;
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03001926 int num_stripes = 1;
Miao Xie6d07bce2011-01-05 10:07:31 +00001927 int i = 0, nr_devices;
David Sterba4f080f52019-06-18 20:00:11 +02001928 const struct btrfs_raid_attr *rattr;
Miao Xie6d07bce2011-01-05 10:07:31 +00001929
Josef Bacik7e33fd92014-11-03 08:56:50 -05001930 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001931 * We aren't under the device list lock, so this is racy-ish, but good
Josef Bacik7e33fd92014-11-03 08:56:50 -05001932 * enough for our purposes.
1933 */
Li Zefanb772a862011-11-28 16:43:00 +08001934 nr_devices = fs_info->fs_devices->open_devices;
Josef Bacik7e33fd92014-11-03 08:56:50 -05001935 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 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001944
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301945 devices_info = kmalloc_array(nr_devices, sizeof(*devices_info),
David Sterba6a445172017-06-15 15:04:04 +02001946 GFP_KERNEL);
Miao Xie6d07bce2011-01-05 10:07:31 +00001947 if (!devices_info)
1948 return -ENOMEM;
1949
Nicholas D Steeves01327612016-05-19 21:18:45 -04001950 /* calc min stripe number for data space allocation */
Jeff Mahoney1b868262017-05-17 11:38:35 -04001951 type = btrfs_data_alloc_profile(fs_info);
David Sterba4f080f52019-06-18 20:00:11 +02001952 rattr = &btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)];
1953
David Sterbae1ea2be2019-06-18 20:00:13 +02001954 if (type & BTRFS_BLOCK_GROUP_RAID0)
Miao Xie39fb26c2011-12-14 20:12:02 -05001955 num_stripes = nr_devices;
David Sterbae1ea2be2019-06-18 20:00:13 +02001956 else if (type & BTRFS_BLOCK_GROUP_RAID1)
Miao Xie39fb26c2011-12-14 20:12:02 -05001957 num_stripes = 2;
David Sterbae1ea2be2019-06-18 20:00:13 +02001958 else if (type & BTRFS_BLOCK_GROUP_RAID10)
Miao Xie39fb26c2011-12-14 20:12:02 -05001959 num_stripes = 4;
Miao Xie6d07bce2011-01-05 10:07:31 +00001960
David Sterba4f080f52019-06-18 20:00:11 +02001961 /* Adjust for more than 1 stripe per device */
1962 min_stripe_size = rattr->dev_stripes * BTRFS_STRIPE_LEN;
Miao Xie6d07bce2011-01-05 10:07:31 +00001963
Josef Bacik7e33fd92014-11-03 08:56:50 -05001964 rcu_read_lock();
1965 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001966 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1967 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08001968 !device->bdev ||
1969 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001970 continue;
1971
Josef Bacik7e33fd92014-11-03 08:56:50 -05001972 if (i >= nr_devices)
1973 break;
1974
Miao Xie6d07bce2011-01-05 10:07:31 +00001975 avail_space = device->total_bytes - device->bytes_used;
1976
1977 /* align with stripe_len */
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03001978 avail_space = rounddown(avail_space, BTRFS_STRIPE_LEN);
Miao Xie6d07bce2011-01-05 10:07:31 +00001979
1980 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001981 * In order to avoid overwriting the superblock on the drive,
Miao Xie6d07bce2011-01-05 10:07:31 +00001982 * btrfs starts at an offset of at least 1MB when doing chunk
1983 * allocation.
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03001984 *
1985 * This ensures we have at least min_stripe_size free space
1986 * after excluding 1MB.
Miao Xie6d07bce2011-01-05 10:07:31 +00001987 */
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03001988 if (avail_space <= SZ_1M + min_stripe_size)
Miao Xie6d07bce2011-01-05 10:07:31 +00001989 continue;
1990
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03001991 avail_space -= SZ_1M;
1992
Miao Xie6d07bce2011-01-05 10:07:31 +00001993 devices_info[i].dev = device;
1994 devices_info[i].max_avail = avail_space;
1995
1996 i++;
1997 }
Josef Bacik7e33fd92014-11-03 08:56:50 -05001998 rcu_read_unlock();
Miao Xie6d07bce2011-01-05 10:07:31 +00001999
2000 nr_devices = i;
2001
2002 btrfs_descending_sort_devices(devices_info, nr_devices);
2003
2004 i = nr_devices - 1;
2005 avail_space = 0;
Nikolay Borisov559ca6e2019-07-03 15:32:59 +03002006 while (nr_devices >= rattr->devs_min) {
2007 num_stripes = min(num_stripes, nr_devices);
Miao Xie39fb26c2011-12-14 20:12:02 -05002008
Miao Xie6d07bce2011-01-05 10:07:31 +00002009 if (devices_info[i].max_avail >= min_stripe_size) {
2010 int j;
2011 u64 alloc_size;
2012
Miao Xie39fb26c2011-12-14 20:12:02 -05002013 avail_space += devices_info[i].max_avail * num_stripes;
Miao Xie6d07bce2011-01-05 10:07:31 +00002014 alloc_size = devices_info[i].max_avail;
Miao Xie39fb26c2011-12-14 20:12:02 -05002015 for (j = i + 1 - num_stripes; j <= i; j++)
Miao Xie6d07bce2011-01-05 10:07:31 +00002016 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
David Sterbaba7b6e62014-07-01 16:21:33 +02002027/*
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
David Sterba0d0c71b2017-06-15 01:30:06 +02002034 * 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).
David Sterbaba7b6e62014-07-01 16:21:33 +02002037 *
David Sterbaca8a51b2015-10-10 17:59:53 +02002038 * If metadata is exhausted, f_bavail will be 0.
David Sterbaba7b6e62014-07-01 16:21:33 +02002039 */
Chris Mason8fd17792007-04-19 21:01:03 -04002040static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
2041{
Al Viro815745c2011-11-17 15:40:49 -05002042 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;
Josef Bacikbd4d1082010-03-05 21:59:21 +00002045 struct btrfs_space_info *found;
2046 u64 total_used = 0;
Miao Xie6d07bce2011-01-05 10:07:31 +00002047 u64 total_free_data = 0;
David Sterbaca8a51b2015-10-10 17:59:53 +02002048 u64 total_free_meta = 0;
Chris Masondb945352007-10-15 16:15:53 -04002049 int bits = dentry->d_sb->s_blocksize_bits;
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002050 __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid;
David Sterbaba7b6e62014-07-01 16:21:33 +02002051 unsigned factor = 1;
2052 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
Miao Xie6d07bce2011-01-05 10:07:31 +00002053 int ret;
David Sterbaca8a51b2015-10-10 17:59:53 +02002054 u64 thresh = 0;
Luis de Bethencourtae02d1b2016-03-30 21:53:38 +01002055 int mixed = 0;
Chris Mason8fd17792007-04-19 21:01:03 -04002056
Josef Bacikbd4d1082010-03-05 21:59:21 +00002057 rcu_read_lock();
Josef Bacik89a55892010-10-14 14:52:27 -04002058 list_for_each_entry_rcu(found, head, list) {
Miao Xie6d07bce2011-01-05 10:07:31 +00002059 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
David Sterbaba7b6e62014-07-01 16:21:33 +02002060 int i;
2061
Miao Xie6d07bce2011-01-05 10:07:31 +00002062 total_free_data += found->disk_total - found->disk_used;
2063 total_free_data -=
2064 btrfs_account_ro_block_groups_free_space(found);
David Sterbaba7b6e62014-07-01 16:21:33 +02002065
2066 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
David Sterba46df06b2018-07-13 20:46:30 +02002067 if (!list_empty(&found->block_groups[i]))
2068 factor = btrfs_bg_type_to_factor(
2069 btrfs_raid_array[i].bg_flag);
David Sterbaba7b6e62014-07-01 16:21:33 +02002070 }
Miao Xie6d07bce2011-01-05 10:07:31 +00002071 }
Luis de Bethencourtae02d1b2016-03-30 21:53:38 +01002072
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 }
Miao Xie6d07bce2011-01-05 10:07:31 +00002083
Yan, Zhengb742bb82010-05-16 10:46:24 -04002084 total_used += found->disk_used;
Josef Bacik89a55892010-10-14 14:52:27 -04002085 }
David Sterbaba7b6e62014-07-01 16:21:33 +02002086
Josef Bacikbd4d1082010-03-05 21:59:21 +00002087 rcu_read_unlock();
2088
David Sterbaba7b6e62014-07-01 16:21:33 +02002089 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);
Luis de Bethencourt41b34ac2016-03-30 23:18:14 +01002095 /* 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;
David Sterbaba7b6e62014-07-01 16:21:33 +02002100 spin_unlock(&block_rsv->lock);
2101
David Sterba0d95c1b2014-11-14 15:05:06 +01002102 buf->f_bavail = div_u64(total_free_data, factor);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002103 ret = btrfs_calc_avail_data_space(fs_info, &total_free_data);
Josef Bacik7e33fd92014-11-03 08:56:50 -05002104 if (ret)
Miao Xie6d07bce2011-01-05 10:07:31 +00002105 return ret;
David Sterbaba7b6e62014-07-01 16:21:33 +02002106 buf->f_bavail += div_u64(total_free_data, factor);
Miao Xie6d07bce2011-01-05 10:07:31 +00002107 buf->f_bavail = buf->f_bavail >> bits;
Chris Masond3977122009-01-05 21:25:51 -05002108
David Sterbaca8a51b2015-10-10 17:59:53 +02002109 /*
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 */
Nikolay Borisovd4417e22017-10-16 16:48:40 +03002122 thresh = SZ_4M;
David Sterbaca8a51b2015-10-10 17:59:53 +02002123
Josef Bacikc5e3c882020-01-31 09:31:05 -05002124 /*
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)
David Sterbaca8a51b2015-10-10 17:59:53 +02002133 buf->f_bavail = 0;
2134
David Sterbaba7b6e62014-07-01 16:21:33 +02002135 buf->f_type = BTRFS_SUPER_MAGIC;
2136 buf->f_bsize = dentry->d_sb->s_blocksize;
2137 buf->f_namelen = BTRFS_NAME_LEN;
2138
David Woodhouse9d036322008-08-18 12:01:52 +01002139 /* We treat it as constant endianness (it doesn't matter _which_)
Chris Masond3977122009-01-05 21:25:51 -05002140 because we want the fsid to come out the same whether mounted
David Woodhouse9d036322008-08-18 12:01:52 +01002141 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]);
David Woodhouse32d48fa2008-08-18 13:10:20 +01002144 /* Mask in the root object ID too, to disambiguate subvols */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09002145 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;
David Woodhouse32d48fa2008-08-18 13:10:20 +01002149
Chris Mason8fd17792007-04-19 21:01:03 -04002150 return 0;
2151}
Chris Masonb5133862007-04-24 11:52:22 -04002152
Al Viroaea52e12011-11-17 01:22:46 -05002153static void btrfs_kill_super(struct super_block *sb)
2154{
Al Viro815745c2011-11-17 15:40:49 -05002155 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Al Viroaea52e12011-11-17 01:22:46 -05002156 kill_anon_super(sb);
Al Virod22ca7d2011-11-17 01:46:50 -05002157 free_fs_info(fs_info);
Al Viroaea52e12011-11-17 01:22:46 -05002158}
2159
Chris Mason2e635a22007-03-21 11:12:56 -04002160static struct file_system_type btrfs_fs_type = {
2161 .owner = THIS_MODULE,
2162 .name = "btrfs",
Al Viro061dbc62010-07-26 16:21:33 +04002163 .mount = btrfs_mount,
Al Viroaea52e12011-11-17 01:22:46 -05002164 .kill_sb = btrfs_kill_super,
Qu Wenruof667aef2014-09-23 13:40:08 +08002165 .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA,
Chris Mason2e635a22007-03-21 11:12:56 -04002166};
Misono, Tomohiro72fa39f2017-12-14 17:24:30 +09002167
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
Eric W. Biederman7f78e032013-03-02 19:39:14 -08002176MODULE_ALIAS_FS("btrfs");
Chris Masona9218f62008-03-24 15:02:04 -04002177
Tom Van Braeckeld8620952015-03-24 16:35:49 +01002178static 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
Chris Masond352ac62008-09-29 15:18:18 -04002189/*
2190 * used by btrfsctl to scan devices when no FS is mounted
2191 */
Chris Mason8a4b83c2008-03-24 15:02:07 -04002192static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2193 unsigned long arg)
2194{
2195 struct btrfs_ioctl_vol_args *vol;
Gu Jinxiang36350e92018-07-12 14:23:16 +08002196 struct btrfs_device *device = NULL;
Chris Masonc071fcf2009-01-16 11:59:08 -05002197 int ret = -ENOTTY;
Chris Mason8a4b83c2008-03-24 15:02:07 -04002198
Chris Masone441d542009-01-05 16:57:23 -05002199 if (!capable(CAP_SYS_ADMIN))
2200 return -EPERM;
2201
Li Zefandae7b662009-04-08 15:06:54 +08002202 vol = memdup_user((void __user *)arg, sizeof(*vol));
2203 if (IS_ERR(vol))
2204 return PTR_ERR(vol);
Filipe Mananaf5057542018-11-14 11:35:24 +00002205 vol->name[BTRFS_PATH_NAME_MAX] = '\0';
Chris Masonc071fcf2009-01-16 11:59:08 -05002206
Chris Mason8a4b83c2008-03-24 15:02:07 -04002207 switch (cmd) {
2208 case BTRFS_IOC_SCAN_DEV:
David Sterba899f9302018-06-19 16:37:36 +02002209 mutex_lock(&uuid_mutex);
Gu Jinxiang36350e92018-07-12 14:23:16 +08002210 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2211 &btrfs_root_fs_type);
2212 ret = PTR_ERR_OR_ZERO(device);
David Sterba899f9302018-06-19 16:37:36 +02002213 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04002214 break;
Anand Jain228a73a2019-01-04 13:31:54 +08002215 case BTRFS_IOC_FORGET_DEV:
2216 ret = btrfs_forget_devices(vol->name);
2217 break;
Josef Bacik02db0842012-06-21 16:03:58 -04002218 case BTRFS_IOC_DEVICES_READY:
David Sterba899f9302018-06-19 16:37:36 +02002219 mutex_lock(&uuid_mutex);
Gu Jinxiang36350e92018-07-12 14:23:16 +08002220 device = btrfs_scan_one_device(vol->name, FMODE_READ,
2221 &btrfs_root_fs_type);
2222 if (IS_ERR(device)) {
David Sterba899f9302018-06-19 16:37:36 +02002223 mutex_unlock(&uuid_mutex);
Gu Jinxiang36350e92018-07-12 14:23:16 +08002224 ret = PTR_ERR(device);
Josef Bacik02db0842012-06-21 16:03:58 -04002225 break;
David Sterba899f9302018-06-19 16:37:36 +02002226 }
Gu Jinxiang36350e92018-07-12 14:23:16 +08002227 ret = !(device->fs_devices->num_devices ==
2228 device->fs_devices->total_devices);
David Sterba899f9302018-06-19 16:37:36 +02002229 mutex_unlock(&uuid_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04002230 break;
David Sterbac5868f82016-02-17 15:24:14 +01002231 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
David Sterbad5131b62016-02-17 15:26:27 +01002232 ret = btrfs_ioctl_get_supported_features((void __user*)arg);
David Sterbac5868f82016-02-17 15:24:14 +01002233 break;
Chris Mason8a4b83c2008-03-24 15:02:07 -04002234 }
Li Zefandae7b662009-04-08 15:06:54 +08002235
Chris Mason8a4b83c2008-03-24 15:02:07 -04002236 kfree(vol);
Linda Knippersf819d832008-06-09 22:17:11 -04002237 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -04002238}
2239
Linus Torvalds01762602009-01-10 06:09:52 -08002240static int btrfs_freeze(struct super_block *sb)
Yaned0dab62008-01-22 12:46:56 -05002241{
Miao Xie354aa0f2012-09-20 01:54:00 -06002242 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002243 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2244 struct btrfs_root *root = fs_info->tree_root;
Miao Xie354aa0f2012-09-20 01:54:00 -06002245
David Sterbafac03c82017-06-15 19:10:03 +02002246 set_bit(BTRFS_FS_FROZEN, &fs_info->flags);
Wang Xiaoguang9e7cc912016-08-01 13:28:08 +08002247 /*
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 */
Miao Xied4edf392013-02-20 09:17:06 +00002253 trans = btrfs_attach_transaction_barrier(root);
Miao Xie354aa0f2012-09-20 01:54:00 -06002254 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 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002260 return btrfs_commit_transaction(trans);
Yaned0dab62008-01-22 12:46:56 -05002261}
2262
Wang Xiaoguang9e7cc912016-08-01 13:28:08 +08002263static int btrfs_unfreeze(struct super_block *sb)
2264{
David Sterbafac03c82017-06-15 19:10:03 +02002265 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2266
2267 clear_bit(BTRFS_FS_FROZEN, &fs_info->flags);
Wang Xiaoguang9e7cc912016-08-01 13:28:08 +08002268 return 0;
2269}
2270
Josef Bacik9c5085c2012-06-05 14:13:12 -04002271static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
2272{
2273 struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
Josef Bacik9c5085c2012-06-05 14:13:12 -04002274 struct btrfs_device *dev, *first_dev = NULL;
Josef Bacik9c5085c2012-06-05 14:13:12 -04002275
David Sterba88c14592018-03-16 03:27:02 +01002276 /*
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
Andrea Gelmini52042d82018-11-28 12:05:13 +01002281 * least until the rcu_read_unlock.
David Sterba88c14592018-03-16 03:27:02 +01002282 */
2283 rcu_read_lock();
Anand Jain3d345292020-07-10 14:37:38 +08002284 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;
Josef Bacik9c5085c2012-06-05 14:13:12 -04002291 }
2292
Misono Tomohiro672d5992018-08-02 16:19:07 +09002293 if (first_dev)
2294 seq_escape(m, rcu_str_deref(first_dev->name), " \t\n\\");
2295 else
Josef Bacik9c5085c2012-06-05 14:13:12 -04002296 WARN_ON(1);
David Sterba88c14592018-03-16 03:27:02 +01002297 rcu_read_unlock();
Josef Bacik9c5085c2012-06-05 14:13:12 -04002298 return 0;
2299}
2300
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07002301static const struct super_operations btrfs_super_ops = {
Yan, Zheng76dda932009-09-21 16:00:26 -04002302 .drop_inode = btrfs_drop_inode,
Al Virobd555972010-06-07 11:35:40 -04002303 .evict_inode = btrfs_evict_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04002304 .put_super = btrfs_put_super,
Chris Masond5719762007-03-23 10:01:08 -04002305 .sync_fs = btrfs_sync_fs,
Eric Parisa9572a12009-04-02 16:46:06 -04002306 .show_options = btrfs_show_options,
Josef Bacik9c5085c2012-06-05 14:13:12 -04002307 .show_devname = btrfs_show_devname,
Chris Mason2c90e5d2007-04-02 10:50:19 -04002308 .alloc_inode = btrfs_alloc_inode,
2309 .destroy_inode = btrfs_destroy_inode,
Al Viro26602ca2019-04-10 15:14:41 -04002310 .free_inode = btrfs_free_inode,
Chris Mason8fd17792007-04-19 21:01:03 -04002311 .statfs = btrfs_statfs,
Yan Zhengc146afa2008-11-12 14:34:12 -05002312 .remount_fs = btrfs_remount,
Linus Torvalds01762602009-01-10 06:09:52 -08002313 .freeze_fs = btrfs_freeze,
Wang Xiaoguang9e7cc912016-08-01 13:28:08 +08002314 .unfreeze_fs = btrfs_unfreeze,
Chris Masone20d96d2007-03-22 12:13:20 -04002315};
Chris Masona9218f62008-03-24 15:02:04 -04002316
2317static const struct file_operations btrfs_ctl_fops = {
Tom Van Braeckeld8620952015-03-24 16:35:49 +01002318 .open = btrfs_control_open,
Chris Masona9218f62008-03-24 15:02:04 -04002319 .unlocked_ioctl = btrfs_control_ioctl,
2320 .compat_ioctl = btrfs_control_ioctl,
2321 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002322 .llseek = noop_llseek,
Chris Masona9218f62008-03-24 15:02:04 -04002323};
2324
2325static struct miscdevice btrfs_misc = {
Kay Sievers578454f2010-05-20 18:07:20 +02002326 .minor = BTRFS_MINOR,
Chris Masona9218f62008-03-24 15:02:04 -04002327 .name = "btrfs-control",
2328 .fops = &btrfs_ctl_fops
2329};
2330
Kay Sievers578454f2010-05-20 18:07:20 +02002331MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
2332MODULE_ALIAS("devname:btrfs-control");
2333
Liu Bof5c29bd2017-11-02 17:21:50 -06002334static int __init btrfs_interface_init(void)
Chris Masona9218f62008-03-24 15:02:04 -04002335{
2336 return misc_register(&btrfs_misc);
2337}
2338
David Sterbae67c7182018-02-19 17:24:18 +01002339static __cold void btrfs_interface_exit(void)
Chris Masona9218f62008-03-24 15:02:04 -04002340{
Greg Kroah-Hartmanf368ed62015-07-30 15:59:57 -07002341 misc_deregister(&btrfs_misc);
Chris Masona9218f62008-03-24 15:02:04 -04002342}
2343
Liu Bof5c29bd2017-11-02 17:21:50 -06002344static void __init btrfs_print_mod_info(void)
David Sterba85965602013-04-30 16:51:59 +00002345{
Bart Van Asscheedf57cb2018-06-20 10:03:33 -07002346 static const char options[] = ""
David Sterba85965602013-04-30 16:51:59 +00002347#ifdef CONFIG_BTRFS_DEBUG
2348 ", debug=on"
2349#endif
Stefan Behrens79556c32013-09-03 15:25:27 +02002350#ifdef CONFIG_BTRFS_ASSERT
2351 ", assert=on"
2352#endif
David Sterba85965602013-04-30 16:51:59 +00002353#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
2354 ", integrity-checker=on"
2355#endif
Josef Bacikfb592372017-09-29 15:43:48 -04002356#ifdef CONFIG_BTRFS_FS_REF_VERIFY
2357 ", ref-verify=on"
2358#endif
Bart Van Asscheedf57cb2018-06-20 10:03:33 -07002359 ;
2360 pr_info("Btrfs loaded, crc32c=%s%s\n", crc32c_impl(), options);
David Sterba85965602013-04-30 16:51:59 +00002361}
2362
Chris Mason2e635a22007-03-21 11:12:56 -04002363static int __init init_btrfs_fs(void)
2364{
Chris Mason2c90e5d2007-04-02 10:50:19 -04002365 int err;
Josef Bacik58176a92007-08-29 15:47:34 -04002366
Filipe David Borba Manana63541922014-01-07 11:47:46 +00002367 btrfs_props_init();
2368
Josef Bacik58176a92007-08-29 15:47:34 -04002369 err = btrfs_init_sysfs();
2370 if (err)
Nikolay Borisov9678c542018-01-08 11:45:05 +02002371 return err;
Josef Bacik58176a92007-08-29 15:47:34 -04002372
Jeff Mahoney143bede2012-03-01 14:56:26 +01002373 btrfs_init_compress();
Chris Masond1310b22008-01-24 16:13:08 -05002374
Li Zefan261507a02010-12-17 14:21:50 +08002375 err = btrfs_init_cachep();
2376 if (err)
2377 goto free_compress;
2378
Chris Masond1310b22008-01-24 16:13:08 -05002379 err = extent_io_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002380 if (err)
2381 goto free_cachep;
2382
Chris Masond1310b22008-01-24 16:13:08 -05002383 err = extent_map_init();
2384 if (err)
2385 goto free_extent_io;
2386
Miao Xie6352b912012-09-06 04:01:51 -06002387 err = ordered_data_init();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002388 if (err)
2389 goto free_extent_map;
Chris Masonc8b97812008-10-29 14:49:59 -04002390
Miao Xie6352b912012-09-06 04:01:51 -06002391 err = btrfs_delayed_inode_init();
2392 if (err)
2393 goto free_ordered_data;
2394
Miao Xie9247f312012-11-26 09:24:43 +00002395 err = btrfs_auto_defrag_init();
Miao Xie16cdcec2011-04-22 18:12:22 +08002396 if (err)
2397 goto free_delayed_inode;
2398
Miao Xie78a61842012-11-21 02:21:28 +00002399 err = btrfs_delayed_ref_init();
Miao Xie9247f312012-11-26 09:24:43 +00002400 if (err)
2401 goto free_auto_defrag;
2402
Wang Shilongb9e9a6c2013-08-09 13:25:36 +08002403 err = btrfs_prelim_ref_init();
2404 if (err)
David Sterbaaf13b4922014-07-30 00:58:37 +02002405 goto free_delayed_ref;
Wang Shilongb9e9a6c2013-08-09 13:25:36 +08002406
David Sterba97eb6b62014-07-30 00:55:42 +02002407 err = btrfs_end_io_wq_init();
Miao Xie78a61842012-11-21 02:21:28 +00002408 if (err)
David Sterbaaf13b4922014-07-30 00:58:37 +02002409 goto free_prelim_ref;
Miao Xie78a61842012-11-21 02:21:28 +00002410
David Sterba97eb6b62014-07-30 00:55:42 +02002411 err = btrfs_interface_init();
2412 if (err)
2413 goto free_end_io_wq;
2414
Jan Schmidte565d4b2012-03-23 17:14:20 +01002415 btrfs_init_lockdep();
2416
Anand Jain8ae1af32016-03-10 12:49:14 +08002417 btrfs_print_mod_info();
Josef Bacikdc11dd52013-08-14 15:05:12 -04002418
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;
Josef Bacik74255aa2013-03-15 09:47:08 -04002426
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002427 return 0;
2428
Chris Masona9218f62008-03-24 15:02:04 -04002429unregister_ioctl:
2430 btrfs_interface_exit();
David Sterba97eb6b62014-07-30 00:55:42 +02002431free_end_io_wq:
2432 btrfs_end_io_wq_exit();
Wang Shilongb9e9a6c2013-08-09 13:25:36 +08002433free_prelim_ref:
2434 btrfs_prelim_ref_exit();
Miao Xie78a61842012-11-21 02:21:28 +00002435free_delayed_ref:
2436 btrfs_delayed_ref_exit();
Miao Xie9247f312012-11-26 09:24:43 +00002437free_auto_defrag:
2438 btrfs_auto_defrag_exit();
Miao Xie16cdcec2011-04-22 18:12:22 +08002439free_delayed_inode:
2440 btrfs_delayed_inode_exit();
Miao Xie6352b912012-09-06 04:01:51 -06002441free_ordered_data:
2442 ordered_data_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002443free_extent_map:
2444 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -05002445free_extent_io:
2446 extent_io_exit();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002447free_cachep:
2448 btrfs_destroy_cachep();
Li Zefan261507a02010-12-17 14:21:50 +08002449free_compress:
2450 btrfs_exit_compress();
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002451 btrfs_exit_sysfs();
Nikolay Borisov9678c542018-01-08 11:45:05 +02002452
Wyatt Banks2f4cbe62007-11-19 10:22:33 -05002453 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04002454}
2455
2456static void __exit exit_btrfs_fs(void)
2457{
Chris Mason39279cc2007-06-12 06:35:45 -04002458 btrfs_destroy_cachep();
Miao Xie78a61842012-11-21 02:21:28 +00002459 btrfs_delayed_ref_exit();
Miao Xie9247f312012-11-26 09:24:43 +00002460 btrfs_auto_defrag_exit();
Miao Xie16cdcec2011-04-22 18:12:22 +08002461 btrfs_delayed_inode_exit();
Wang Shilongb9e9a6c2013-08-09 13:25:36 +08002462 btrfs_prelim_ref_exit();
Miao Xie6352b912012-09-06 04:01:51 -06002463 ordered_data_exit();
Chris Masona52d9a82007-08-27 16:49:44 -04002464 extent_map_exit();
Chris Masond1310b22008-01-24 16:13:08 -05002465 extent_io_exit();
Chris Masona9218f62008-03-24 15:02:04 -04002466 btrfs_interface_exit();
Josef Bacik5ed5f582014-10-15 17:19:59 -04002467 btrfs_end_io_wq_exit();
Chris Mason2e635a22007-03-21 11:12:56 -04002468 unregister_filesystem(&btrfs_fs_type);
Josef Bacik58176a92007-08-29 15:47:34 -04002469 btrfs_exit_sysfs();
Chris Mason8a4b83c2008-03-24 15:02:07 -04002470 btrfs_cleanup_fs_uuids();
Li Zefan261507a02010-12-17 14:21:50 +08002471 btrfs_exit_compress();
Chris Mason2e635a22007-03-21 11:12:56 -04002472}
2473
Filipe David Borba Manana60efa5e2014-02-01 21:27:56 +00002474late_initcall(init_btrfs_fs);
Chris Mason2e635a22007-03-21 11:12:56 -04002475module_exit(exit_btrfs_fs)
2476
2477MODULE_LICENSE("GPL");
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002478MODULE_SOFTDEP("pre: crc32c");