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