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