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