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 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 7 | #include <linux/sched/mm.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 8 | #include <linux/slab.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/completion.h> |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 11 | #include <linux/bug.h> |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 12 | #include <crypto/hash.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 13 | |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 14 | #include "ctree.h" |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 15 | #include "discard.h" |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 16 | #include "disk-io.h" |
Omar Sandoval | 7573df5 | 2020-08-21 00:39:54 -0700 | [diff] [blame] | 17 | #include "send.h" |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 18 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 19 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 20 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 21 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 22 | #include "block-group.h" |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 23 | #include "qgroup.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 24 | |
David Sterba | 9188db6 | 2019-08-02 12:52:48 +0200 | [diff] [blame] | 25 | struct btrfs_feature_attr { |
| 26 | struct kobj_attribute kobj_attr; |
| 27 | enum btrfs_feature_set feature_set; |
| 28 | u64 feature_bit; |
| 29 | }; |
| 30 | |
| 31 | /* For raid type sysfs entries */ |
| 32 | struct raid_kobject { |
| 33 | u64 flags; |
| 34 | struct kobject kobj; |
| 35 | }; |
| 36 | |
| 37 | #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ |
| 38 | { \ |
| 39 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 40 | .show = _show, \ |
| 41 | .store = _store, \ |
| 42 | } |
| 43 | |
| 44 | #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ |
| 45 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 46 | __INIT_KOBJ_ATTR(_name, 0644, _show, _store) |
| 47 | |
| 48 | #define BTRFS_ATTR(_prefix, _name, _show) \ |
| 49 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 50 | __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) |
| 51 | |
| 52 | #define BTRFS_ATTR_PTR(_prefix, _name) \ |
| 53 | (&btrfs_attr_##_prefix##_##_name.attr) |
| 54 | |
| 55 | #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ |
| 56 | static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ |
| 57 | .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ |
| 58 | btrfs_feature_attr_show, \ |
| 59 | btrfs_feature_attr_store), \ |
| 60 | .feature_set = _feature_set, \ |
| 61 | .feature_bit = _feature_prefix ##_## _feature_bit, \ |
| 62 | } |
| 63 | #define BTRFS_FEAT_ATTR_PTR(_name) \ |
| 64 | (&btrfs_attr_features_##_name.kobj_attr.attr) |
| 65 | |
| 66 | #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ |
| 67 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) |
| 68 | #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ |
| 69 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) |
| 70 | #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ |
| 71 | BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) |
| 72 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 73 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 74 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 75 | |
David Sterba | 8f52316 | 2019-08-02 13:07:38 +0200 | [diff] [blame] | 76 | static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) |
| 77 | { |
| 78 | return container_of(a, struct btrfs_feature_attr, kobj_attr); |
| 79 | } |
| 80 | |
| 81 | static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) |
| 82 | { |
| 83 | return container_of(attr, struct kobj_attribute, attr); |
| 84 | } |
| 85 | |
| 86 | static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( |
| 87 | struct attribute *attr) |
| 88 | { |
| 89 | return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); |
| 90 | } |
| 91 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 92 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 93 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 94 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 95 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 96 | if (set == FEAT_COMPAT) |
| 97 | return btrfs_super_compat_flags(disk_super); |
| 98 | else if (set == FEAT_COMPAT_RO) |
| 99 | return btrfs_super_compat_ro_flags(disk_super); |
| 100 | else |
| 101 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 104 | static void set_features(struct btrfs_fs_info *fs_info, |
| 105 | enum btrfs_feature_set set, u64 features) |
| 106 | { |
| 107 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 108 | if (set == FEAT_COMPAT) |
| 109 | btrfs_set_super_compat_flags(disk_super, features); |
| 110 | else if (set == FEAT_COMPAT_RO) |
| 111 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 112 | else |
| 113 | btrfs_set_super_incompat_flags(disk_super, features); |
| 114 | } |
| 115 | |
| 116 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 117 | { |
| 118 | int val = 0; |
| 119 | u64 set, clear; |
| 120 | switch (fa->feature_set) { |
| 121 | case FEAT_COMPAT: |
| 122 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 123 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 124 | break; |
| 125 | case FEAT_COMPAT_RO: |
| 126 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 127 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 128 | break; |
| 129 | case FEAT_INCOMPAT: |
| 130 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 131 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 132 | break; |
| 133 | default: |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 134 | pr_warn("btrfs: sysfs: unknown feature set %d\n", |
David Sterba | cc37bb0 | 2013-11-19 13:36:21 +0100 | [diff] [blame] | 135 | fa->feature_set); |
| 136 | return 0; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (set & fa->feature_bit) |
| 140 | val |= 1; |
| 141 | if (clear & fa->feature_bit) |
| 142 | val |= 2; |
| 143 | |
| 144 | return val; |
| 145 | } |
| 146 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 147 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 148 | struct kobj_attribute *a, char *buf) |
| 149 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 150 | int val = 0; |
| 151 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 152 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 153 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 154 | u64 features = get_features(fs_info, fa->feature_set); |
| 155 | if (features & fa->feature_bit) |
| 156 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 157 | } else |
| 158 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 159 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 160 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 163 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 164 | struct kobj_attribute *a, |
| 165 | const char *buf, size_t count) |
| 166 | { |
| 167 | struct btrfs_fs_info *fs_info; |
| 168 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 169 | u64 features, set, clear; |
| 170 | unsigned long val; |
| 171 | int ret; |
| 172 | |
| 173 | fs_info = to_fs_info(kobj); |
| 174 | if (!fs_info) |
| 175 | return -EPERM; |
| 176 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 177 | if (sb_rdonly(fs_info->sb)) |
David Sterba | ee61113 | 2015-01-23 18:43:31 +0100 | [diff] [blame] | 178 | return -EROFS; |
| 179 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 180 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | |
| 184 | if (fa->feature_set == FEAT_COMPAT) { |
| 185 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 186 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 187 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 188 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 189 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 190 | } else { |
| 191 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 192 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 193 | } |
| 194 | |
| 195 | features = get_features(fs_info, fa->feature_set); |
| 196 | |
| 197 | /* Nothing to do */ |
| 198 | if ((val && (features & fa->feature_bit)) || |
| 199 | (!val && !(features & fa->feature_bit))) |
| 200 | return count; |
| 201 | |
| 202 | if ((val && !(set & fa->feature_bit)) || |
| 203 | (!val && !(clear & fa->feature_bit))) { |
| 204 | btrfs_info(fs_info, |
| 205 | "%sabling feature %s on mounted fs is not supported.", |
| 206 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 207 | return -EPERM; |
| 208 | } |
| 209 | |
| 210 | btrfs_info(fs_info, "%s %s feature flag", |
| 211 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 212 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 213 | spin_lock(&fs_info->super_lock); |
| 214 | features = get_features(fs_info, fa->feature_set); |
| 215 | if (val) |
| 216 | features |= fa->feature_bit; |
| 217 | else |
| 218 | features &= ~fa->feature_bit; |
| 219 | set_features(fs_info, fa->feature_set, features); |
| 220 | spin_unlock(&fs_info->super_lock); |
| 221 | |
David Sterba | 0eae274 | 2014-11-12 14:22:21 +0100 | [diff] [blame] | 222 | /* |
| 223 | * We don't want to do full transaction commit from inside sysfs |
| 224 | */ |
| 225 | btrfs_set_pending(fs_info, COMMIT); |
| 226 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 227 | |
| 228 | return count; |
| 229 | } |
| 230 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 231 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 232 | struct attribute *attr, int unused) |
| 233 | { |
| 234 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 235 | umode_t mode = attr->mode; |
| 236 | |
| 237 | if (fs_info) { |
| 238 | struct btrfs_feature_attr *fa; |
| 239 | u64 features; |
| 240 | |
| 241 | fa = attr_to_btrfs_feature_attr(attr); |
| 242 | features = get_features(fs_info, fa->feature_set); |
| 243 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 244 | if (can_modify_feature(fa)) |
| 245 | mode |= S_IWUSR; |
| 246 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 247 | mode = 0; |
| 248 | } |
| 249 | |
| 250 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 254 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 255 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 256 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 257 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 258 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 259 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 260 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 261 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 262 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 263 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 264 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 265 | BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); |
Naohiro Aota | 7b3d5a9 | 2020-11-10 20:26:06 +0900 | [diff] [blame^] | 266 | /* Remove once support for zoned allocation is feature complete */ |
| 267 | #ifdef CONFIG_BTRFS_DEBUG |
| 268 | BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED); |
| 269 | #endif |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 270 | |
| 271 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 272 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 273 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 274 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 275 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 276 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 277 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 278 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 279 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 280 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 281 | BTRFS_FEAT_ATTR_PTR(no_holes), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 282 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 283 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 284 | BTRFS_FEAT_ATTR_PTR(raid1c34), |
Naohiro Aota | 7b3d5a9 | 2020-11-10 20:26:06 +0900 | [diff] [blame^] | 285 | #ifdef CONFIG_BTRFS_DEBUG |
| 286 | BTRFS_FEAT_ATTR_PTR(zoned), |
| 287 | #endif |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 288 | NULL |
| 289 | }; |
| 290 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 291 | /* |
| 292 | * Features which depend on feature bits and may differ between each fs. |
| 293 | * |
| 294 | * /sys/fs/btrfs/features lists all available features of this kernel while |
| 295 | * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or |
| 296 | * can be changed online. |
| 297 | */ |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 298 | static const struct attribute_group btrfs_feature_attr_group = { |
| 299 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 300 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 301 | .attrs = btrfs_supported_feature_attrs, |
| 302 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 303 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 304 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 305 | struct kobj_attribute *ka, char *buf) |
| 306 | { |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 307 | return scnprintf(buf, PAGE_SIZE, "0\n"); |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 308 | } |
| 309 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 310 | |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 311 | static ssize_t supported_checksums_show(struct kobject *kobj, |
| 312 | struct kobj_attribute *a, char *buf) |
| 313 | { |
| 314 | ssize_t ret = 0; |
| 315 | int i; |
| 316 | |
| 317 | for (i = 0; i < btrfs_get_num_csums(); i++) { |
| 318 | /* |
| 319 | * This "trick" only works as long as 'enum btrfs_csum_type' has |
| 320 | * no holes in it |
| 321 | */ |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 322 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 323 | (i == 0 ? "" : " "), btrfs_super_csum_name(i)); |
| 324 | |
| 325 | } |
| 326 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 327 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); |
| 331 | |
Omar Sandoval | 7573df5 | 2020-08-21 00:39:54 -0700 | [diff] [blame] | 332 | static ssize_t send_stream_version_show(struct kobject *kobj, |
| 333 | struct kobj_attribute *ka, char *buf) |
| 334 | { |
| 335 | return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION); |
| 336 | } |
| 337 | BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show); |
| 338 | |
Josef Bacik | ceafe3c | 2020-10-16 11:29:15 -0400 | [diff] [blame] | 339 | static const char *rescue_opts[] = { |
| 340 | "usebackuproot", |
| 341 | "nologreplay", |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 342 | "ignorebadroots", |
Josef Bacik | 882dbe0 | 2020-10-16 11:29:19 -0400 | [diff] [blame] | 343 | "ignoredatacsums", |
Josef Bacik | 9037d3c | 2020-10-16 11:29:20 -0400 | [diff] [blame] | 344 | "all", |
Josef Bacik | ceafe3c | 2020-10-16 11:29:15 -0400 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | static ssize_t supported_rescue_options_show(struct kobject *kobj, |
| 348 | struct kobj_attribute *a, |
| 349 | char *buf) |
| 350 | { |
| 351 | ssize_t ret = 0; |
| 352 | int i; |
| 353 | |
| 354 | for (i = 0; i < ARRAY_SIZE(rescue_opts); i++) |
| 355 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
| 356 | (i ? " " : ""), rescue_opts[i]); |
| 357 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 358 | return ret; |
| 359 | } |
| 360 | BTRFS_ATTR(static_feature, supported_rescue_options, |
| 361 | supported_rescue_options_show); |
| 362 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 363 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 364 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 365 | BTRFS_ATTR_PTR(static_feature, supported_checksums), |
Omar Sandoval | 7573df5 | 2020-08-21 00:39:54 -0700 | [diff] [blame] | 366 | BTRFS_ATTR_PTR(static_feature, send_stream_version), |
Josef Bacik | ceafe3c | 2020-10-16 11:29:15 -0400 | [diff] [blame] | 367 | BTRFS_ATTR_PTR(static_feature, supported_rescue_options), |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 368 | NULL |
| 369 | }; |
| 370 | |
| 371 | /* |
| 372 | * Features which only depend on kernel version. |
| 373 | * |
| 374 | * These are listed in /sys/fs/btrfs/features along with |
| 375 | * btrfs_feature_attr_group |
| 376 | */ |
| 377 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 378 | .name = "features", |
| 379 | .attrs = btrfs_supported_static_feature_attrs, |
| 380 | }; |
| 381 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 382 | #ifdef CONFIG_BTRFS_DEBUG |
| 383 | |
| 384 | /* |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 385 | * Discard statistics and tunables |
| 386 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 387 | #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) |
| 388 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 389 | static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, |
| 390 | struct kobj_attribute *a, |
| 391 | char *buf) |
| 392 | { |
| 393 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 394 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 395 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 396 | atomic64_read(&fs_info->discard_ctl.discardable_bytes)); |
| 397 | } |
| 398 | BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); |
| 399 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 400 | static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, |
| 401 | struct kobj_attribute *a, |
| 402 | char *buf) |
| 403 | { |
| 404 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 405 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 406 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 407 | atomic_read(&fs_info->discard_ctl.discardable_extents)); |
| 408 | } |
| 409 | BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); |
| 410 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 411 | static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, |
| 412 | struct kobj_attribute *a, |
| 413 | char *buf) |
| 414 | { |
| 415 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 416 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 417 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 418 | fs_info->discard_ctl.discard_bitmap_bytes); |
| 419 | } |
| 420 | BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); |
| 421 | |
| 422 | static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, |
| 423 | struct kobj_attribute *a, |
| 424 | char *buf) |
| 425 | { |
| 426 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 427 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 428 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 429 | atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); |
| 430 | } |
| 431 | BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); |
| 432 | |
| 433 | static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, |
| 434 | struct kobj_attribute *a, |
| 435 | char *buf) |
| 436 | { |
| 437 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 438 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 439 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 440 | fs_info->discard_ctl.discard_extent_bytes); |
| 441 | } |
| 442 | BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); |
| 443 | |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 444 | static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, |
| 445 | struct kobj_attribute *a, |
| 446 | char *buf) |
| 447 | { |
| 448 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 449 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 450 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 451 | READ_ONCE(fs_info->discard_ctl.iops_limit)); |
| 452 | } |
| 453 | |
| 454 | static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, |
| 455 | struct kobj_attribute *a, |
| 456 | const char *buf, size_t len) |
| 457 | { |
| 458 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 459 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 460 | u32 iops_limit; |
| 461 | int ret; |
| 462 | |
| 463 | ret = kstrtou32(buf, 10, &iops_limit); |
| 464 | if (ret) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | WRITE_ONCE(discard_ctl->iops_limit, iops_limit); |
Pavel Begunkov | 3e48d8d | 2020-11-04 09:45:54 +0000 | [diff] [blame] | 468 | btrfs_discard_calc_delay(discard_ctl); |
| 469 | btrfs_discard_schedule_work(discard_ctl, true); |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 470 | return len; |
| 471 | } |
| 472 | BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, |
| 473 | btrfs_discard_iops_limit_store); |
| 474 | |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 475 | static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, |
| 476 | struct kobj_attribute *a, |
| 477 | char *buf) |
| 478 | { |
| 479 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 480 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 481 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 482 | READ_ONCE(fs_info->discard_ctl.kbps_limit)); |
| 483 | } |
| 484 | |
| 485 | static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, |
| 486 | struct kobj_attribute *a, |
| 487 | const char *buf, size_t len) |
| 488 | { |
| 489 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 490 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 491 | u32 kbps_limit; |
| 492 | int ret; |
| 493 | |
| 494 | ret = kstrtou32(buf, 10, &kbps_limit); |
| 495 | if (ret) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); |
Pavel Begunkov | 3e48d8d | 2020-11-04 09:45:54 +0000 | [diff] [blame] | 499 | btrfs_discard_schedule_work(discard_ctl, true); |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 500 | return len; |
| 501 | } |
| 502 | BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, |
| 503 | btrfs_discard_kbps_limit_store); |
| 504 | |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 505 | static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, |
| 506 | struct kobj_attribute *a, |
| 507 | char *buf) |
| 508 | { |
| 509 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 510 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 511 | return scnprintf(buf, PAGE_SIZE, "%llu\n", |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 512 | READ_ONCE(fs_info->discard_ctl.max_discard_size)); |
| 513 | } |
| 514 | |
| 515 | static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, |
| 516 | struct kobj_attribute *a, |
| 517 | const char *buf, size_t len) |
| 518 | { |
| 519 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 520 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 521 | u64 max_discard_size; |
| 522 | int ret; |
| 523 | |
| 524 | ret = kstrtou64(buf, 10, &max_discard_size); |
| 525 | if (ret) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); |
| 529 | |
| 530 | return len; |
| 531 | } |
| 532 | BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, |
| 533 | btrfs_discard_max_discard_size_store); |
| 534 | |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 535 | static const struct attribute *discard_debug_attrs[] = { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 536 | BTRFS_ATTR_PTR(discard, discardable_bytes), |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 537 | BTRFS_ATTR_PTR(discard, discardable_extents), |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame] | 538 | BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), |
| 539 | BTRFS_ATTR_PTR(discard, discard_bytes_saved), |
| 540 | BTRFS_ATTR_PTR(discard, discard_extent_bytes), |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 541 | BTRFS_ATTR_PTR(discard, iops_limit), |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 542 | BTRFS_ATTR_PTR(discard, kbps_limit), |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 543 | BTRFS_ATTR_PTR(discard, max_discard_size), |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 544 | NULL, |
| 545 | }; |
| 546 | |
| 547 | /* |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 548 | * Runtime debugging exported via sysfs |
| 549 | * |
| 550 | * /sys/fs/btrfs/debug - applies to module or all filesystems |
| 551 | * /sys/fs/btrfs/UUID - applies only to the given filesystem |
| 552 | */ |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 553 | static const struct attribute *btrfs_debug_mount_attrs[] = { |
| 554 | NULL, |
| 555 | }; |
| 556 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 557 | static struct attribute *btrfs_debug_feature_attrs[] = { |
| 558 | NULL |
| 559 | }; |
| 560 | |
| 561 | static const struct attribute_group btrfs_debug_feature_attr_group = { |
| 562 | .name = "debug", |
| 563 | .attrs = btrfs_debug_feature_attrs, |
| 564 | }; |
| 565 | |
| 566 | #endif |
| 567 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 568 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 569 | { |
| 570 | u64 val; |
| 571 | if (lock) |
| 572 | spin_lock(lock); |
| 573 | val = *value_ptr; |
| 574 | if (lock) |
| 575 | spin_unlock(lock); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 576 | return scnprintf(buf, PAGE_SIZE, "%llu\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 580 | struct kobj_attribute *ka, char *buf) |
| 581 | { |
| 582 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 583 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 584 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 585 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 586 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 587 | |
| 588 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 589 | struct kobj_attribute *a, char *buf) |
| 590 | { |
| 591 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 592 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 593 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 594 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 595 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 596 | |
| 597 | #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 598 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 599 | |
| 600 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 601 | struct kobj_attribute *attr, char *buf); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 602 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 603 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 604 | |
| 605 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 606 | struct kobj_attribute *attr, char *buf) |
| 607 | |
| 608 | { |
| 609 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 610 | struct btrfs_block_group *block_group; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 611 | int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 612 | u64 val = 0; |
| 613 | |
| 614 | down_read(&sinfo->groups_sem); |
| 615 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 616 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 617 | val += block_group->length; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 618 | else |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 619 | val += block_group->used; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 620 | } |
| 621 | up_read(&sinfo->groups_sem); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 622 | return scnprintf(buf, PAGE_SIZE, "%llu\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 625 | static struct attribute *raid_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 626 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 627 | BTRFS_ATTR_PTR(raid, used_bytes), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 628 | NULL |
| 629 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 630 | ATTRIBUTE_GROUPS(raid); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 631 | |
| 632 | static void release_raid_kobj(struct kobject *kobj) |
| 633 | { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 634 | kfree(to_raid_kobj(kobj)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 635 | } |
| 636 | |
David Sterba | 536ea45 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 637 | static struct kobj_type btrfs_raid_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 638 | .sysfs_ops = &kobj_sysfs_ops, |
| 639 | .release = release_raid_kobj, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 640 | .default_groups = raid_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | #define SPACE_INFO_ATTR(field) \ |
| 644 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 645 | struct kobj_attribute *a, \ |
| 646 | char *buf) \ |
| 647 | { \ |
| 648 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 649 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 650 | } \ |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 651 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 652 | |
| 653 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 654 | struct kobj_attribute *a, |
| 655 | char *buf) |
| 656 | { |
| 657 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 658 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 659 | return scnprintf(buf, PAGE_SIZE, "%lld\n", val); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | SPACE_INFO_ATTR(flags); |
| 663 | SPACE_INFO_ATTR(total_bytes); |
| 664 | SPACE_INFO_ATTR(bytes_used); |
| 665 | SPACE_INFO_ATTR(bytes_pinned); |
| 666 | SPACE_INFO_ATTR(bytes_reserved); |
| 667 | SPACE_INFO_ATTR(bytes_may_use); |
Wang Xiaoguang | c1fd5c3 | 2016-06-21 11:12:56 +0800 | [diff] [blame] | 668 | SPACE_INFO_ATTR(bytes_readonly); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 669 | SPACE_INFO_ATTR(disk_used); |
| 670 | SPACE_INFO_ATTR(disk_total); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 671 | BTRFS_ATTR(space_info, total_bytes_pinned, |
| 672 | btrfs_space_info_show_total_bytes_pinned); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 673 | |
| 674 | static struct attribute *space_info_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 675 | BTRFS_ATTR_PTR(space_info, flags), |
| 676 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 677 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 678 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 679 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 680 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 681 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 682 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 683 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 684 | BTRFS_ATTR_PTR(space_info, total_bytes_pinned), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 685 | NULL, |
| 686 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 687 | ATTRIBUTE_GROUPS(space_info); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 688 | |
| 689 | static void space_info_release(struct kobject *kobj) |
| 690 | { |
| 691 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 692 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 693 | kfree(sinfo); |
| 694 | } |
| 695 | |
David Sterba | 27992d0 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 696 | static struct kobj_type space_info_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 697 | .sysfs_ops = &kobj_sysfs_ops, |
| 698 | .release = space_info_release, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 699 | .default_groups = space_info_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 700 | }; |
| 701 | |
| 702 | static const struct attribute *allocation_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 703 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 704 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 705 | NULL, |
| 706 | }; |
| 707 | |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 708 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 709 | struct kobj_attribute *a, char *buf) |
| 710 | { |
| 711 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 712 | char *label = fs_info->super_copy->label; |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 713 | ssize_t ret; |
| 714 | |
| 715 | spin_lock(&fs_info->super_lock); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 716 | ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 717 | spin_unlock(&fs_info->super_lock); |
| 718 | |
| 719 | return ret; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 723 | struct kobj_attribute *a, |
| 724 | const char *buf, size_t len) |
| 725 | { |
| 726 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 727 | size_t p_len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 728 | |
David Sterba | 66ac9fe | 2016-04-26 16:03:57 +0200 | [diff] [blame] | 729 | if (!fs_info) |
| 730 | return -EPERM; |
| 731 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 732 | if (sb_rdonly(fs_info->sb)) |
Anand Jain | 79aec2b | 2014-07-30 20:04:10 +0800 | [diff] [blame] | 733 | return -EROFS; |
| 734 | |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 735 | /* |
| 736 | * p_len is the len until the first occurrence of either |
| 737 | * '\n' or '\0' |
| 738 | */ |
| 739 | p_len = strcspn(buf, "\n"); |
| 740 | |
| 741 | if (p_len >= BTRFS_LABEL_SIZE) |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 742 | return -EINVAL; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 743 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 744 | spin_lock(&fs_info->super_lock); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 745 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 746 | memcpy(fs_info->super_copy->label, buf, p_len); |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 747 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 748 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 749 | /* |
| 750 | * We don't want to do full transaction commit from inside sysfs |
| 751 | */ |
| 752 | btrfs_set_pending(fs_info, COMMIT); |
| 753 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 754 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 755 | return len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 756 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 757 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 758 | |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 759 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 760 | struct kobj_attribute *a, char *buf) |
| 761 | { |
| 762 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 763 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 764 | return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 765 | } |
| 766 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 767 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 768 | |
| 769 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 770 | struct kobj_attribute *a, char *buf) |
| 771 | { |
| 772 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 773 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 774 | return scnprintf(buf, PAGE_SIZE, "%u\n", |
| 775 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 778 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 779 | |
| 780 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 781 | struct kobj_attribute *a, char *buf) |
| 782 | { |
| 783 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 784 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 785 | return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 786 | } |
| 787 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 788 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 789 | |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 790 | static ssize_t quota_override_show(struct kobject *kobj, |
| 791 | struct kobj_attribute *a, char *buf) |
| 792 | { |
| 793 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 794 | int quota_override; |
| 795 | |
| 796 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 797 | return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static ssize_t quota_override_store(struct kobject *kobj, |
| 801 | struct kobj_attribute *a, |
| 802 | const char *buf, size_t len) |
| 803 | { |
| 804 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 805 | unsigned long knob; |
| 806 | int err; |
| 807 | |
| 808 | if (!fs_info) |
| 809 | return -EPERM; |
| 810 | |
| 811 | if (!capable(CAP_SYS_RESOURCE)) |
| 812 | return -EPERM; |
| 813 | |
| 814 | err = kstrtoul(buf, 10, &knob); |
| 815 | if (err) |
| 816 | return err; |
| 817 | if (knob > 1) |
| 818 | return -EINVAL; |
| 819 | |
| 820 | if (knob) |
| 821 | set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 822 | else |
| 823 | clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 824 | |
| 825 | return len; |
| 826 | } |
| 827 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 828 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 829 | |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 830 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 831 | struct kobj_attribute *a, char *buf) |
| 832 | { |
| 833 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 834 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 835 | return scnprintf(buf, PAGE_SIZE, "%pU\n", |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 836 | fs_info->fs_devices->metadata_uuid); |
| 837 | } |
| 838 | |
| 839 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 840 | |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 841 | static ssize_t btrfs_checksum_show(struct kobject *kobj, |
| 842 | struct kobj_attribute *a, char *buf) |
| 843 | { |
| 844 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 845 | u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); |
| 846 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 847 | return scnprintf(buf, PAGE_SIZE, "%s (%s)\n", |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 848 | btrfs_super_csum_name(csum_type), |
| 849 | crypto_shash_driver_name(fs_info->csum_shash)); |
| 850 | } |
| 851 | |
| 852 | BTRFS_ATTR(, checksum, btrfs_checksum_show); |
| 853 | |
Goldwyn Rodrigues | 66a2823 | 2020-08-25 10:02:33 -0500 | [diff] [blame] | 854 | static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, |
| 855 | struct kobj_attribute *a, char *buf) |
| 856 | { |
| 857 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 858 | const char *str; |
| 859 | |
| 860 | switch (READ_ONCE(fs_info->exclusive_operation)) { |
| 861 | case BTRFS_EXCLOP_NONE: |
| 862 | str = "none\n"; |
| 863 | break; |
| 864 | case BTRFS_EXCLOP_BALANCE: |
| 865 | str = "balance\n"; |
| 866 | break; |
| 867 | case BTRFS_EXCLOP_DEV_ADD: |
| 868 | str = "device add\n"; |
| 869 | break; |
| 870 | case BTRFS_EXCLOP_DEV_REMOVE: |
| 871 | str = "device remove\n"; |
| 872 | break; |
| 873 | case BTRFS_EXCLOP_DEV_REPLACE: |
| 874 | str = "device replace\n"; |
| 875 | break; |
| 876 | case BTRFS_EXCLOP_RESIZE: |
| 877 | str = "resize\n"; |
| 878 | break; |
| 879 | case BTRFS_EXCLOP_SWAP_ACTIVATE: |
| 880 | str = "swap activate\n"; |
| 881 | break; |
| 882 | default: |
| 883 | str = "UNKNOWN\n"; |
| 884 | break; |
| 885 | } |
| 886 | return scnprintf(buf, PAGE_SIZE, "%s", str); |
| 887 | } |
| 888 | BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); |
| 889 | |
Anand Jain | 089c8b0 | 2020-10-07 15:20:03 +0800 | [diff] [blame] | 890 | static ssize_t btrfs_generation_show(struct kobject *kobj, |
| 891 | struct kobj_attribute *a, char *buf) |
| 892 | { |
| 893 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 894 | |
| 895 | return scnprintf(buf, PAGE_SIZE, "%llu\n", fs_info->generation); |
| 896 | } |
| 897 | BTRFS_ATTR(, generation, btrfs_generation_show); |
| 898 | |
Anand Jain | aaefed2 | 2020-10-28 21:14:45 +0800 | [diff] [blame] | 899 | /* |
| 900 | * Look for an exact string @string in @buffer with possible leading or |
| 901 | * trailing whitespace |
| 902 | */ |
| 903 | static bool strmatch(const char *buffer, const char *string) |
| 904 | { |
| 905 | const size_t len = strlen(string); |
| 906 | |
| 907 | /* Skip leading whitespace */ |
| 908 | buffer = skip_spaces(buffer); |
| 909 | |
| 910 | /* Match entire string, check if the rest is whitespace or empty */ |
| 911 | if (strncmp(string, buffer, len) == 0 && |
| 912 | strlen(skip_spaces(buffer + len)) == 0) |
| 913 | return true; |
| 914 | |
| 915 | return false; |
| 916 | } |
| 917 | |
Anand Jain | 3d8cc17 | 2020-10-28 21:14:47 +0800 | [diff] [blame] | 918 | static const char * const btrfs_read_policy_name[] = { "pid" }; |
| 919 | |
| 920 | static ssize_t btrfs_read_policy_show(struct kobject *kobj, |
| 921 | struct kobj_attribute *a, char *buf) |
| 922 | { |
| 923 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 924 | ssize_t ret = 0; |
| 925 | int i; |
| 926 | |
| 927 | for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { |
| 928 | if (fs_devices->read_policy == i) |
| 929 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]", |
| 930 | (ret == 0 ? "" : " "), |
| 931 | btrfs_read_policy_name[i]); |
| 932 | else |
| 933 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
| 934 | (ret == 0 ? "" : " "), |
| 935 | btrfs_read_policy_name[i]); |
| 936 | } |
| 937 | |
| 938 | ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 939 | |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | static ssize_t btrfs_read_policy_store(struct kobject *kobj, |
| 944 | struct kobj_attribute *a, |
| 945 | const char *buf, size_t len) |
| 946 | { |
| 947 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 948 | int i; |
| 949 | |
| 950 | for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { |
| 951 | if (strmatch(buf, btrfs_read_policy_name[i])) { |
| 952 | if (i != fs_devices->read_policy) { |
| 953 | fs_devices->read_policy = i; |
| 954 | btrfs_info(fs_devices->fs_info, |
| 955 | "read policy set to '%s'", |
| 956 | btrfs_read_policy_name[i]); |
| 957 | } |
| 958 | return len; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store); |
| 965 | |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 966 | static const struct attribute *btrfs_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 967 | BTRFS_ATTR_PTR(, label), |
| 968 | BTRFS_ATTR_PTR(, nodesize), |
| 969 | BTRFS_ATTR_PTR(, sectorsize), |
| 970 | BTRFS_ATTR_PTR(, clone_alignment), |
| 971 | BTRFS_ATTR_PTR(, quota_override), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 972 | BTRFS_ATTR_PTR(, metadata_uuid), |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 973 | BTRFS_ATTR_PTR(, checksum), |
Goldwyn Rodrigues | 66a2823 | 2020-08-25 10:02:33 -0500 | [diff] [blame] | 974 | BTRFS_ATTR_PTR(, exclusive_operation), |
Anand Jain | 089c8b0 | 2020-10-07 15:20:03 +0800 | [diff] [blame] | 975 | BTRFS_ATTR_PTR(, generation), |
Anand Jain | 3d8cc17 | 2020-10-28 21:14:47 +0800 | [diff] [blame] | 976 | BTRFS_ATTR_PTR(, read_policy), |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 977 | NULL, |
| 978 | }; |
| 979 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 980 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 981 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 982 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
Anand Jain | 248d200 | 2015-03-10 06:38:19 +0800 | [diff] [blame] | 983 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 984 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 985 | complete(&fs_devs->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | static struct kobj_type btrfs_ktype = { |
| 989 | .sysfs_ops = &kobj_sysfs_ops, |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 990 | .release = btrfs_release_fsid_kobj, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 991 | }; |
| 992 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 993 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 994 | { |
| 995 | if (kobj->ktype != &btrfs_ktype) |
| 996 | return NULL; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 997 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 998 | } |
| 999 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1000 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 1001 | { |
| 1002 | if (kobj->ktype != &btrfs_ktype) |
| 1003 | return NULL; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1004 | return to_fs_devs(kobj)->fs_info; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1005 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1006 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1007 | #define NUM_FEATURE_BITS 64 |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 1008 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 1009 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 1010 | static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1011 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 1012 | static const u64 supported_feature_masks[FEAT_MAX] = { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1013 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 1014 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 1015 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 1016 | }; |
| 1017 | |
| 1018 | static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1019 | { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1020 | int set; |
| 1021 | |
| 1022 | for (set = 0; set < FEAT_MAX; set++) { |
| 1023 | int i; |
| 1024 | struct attribute *attrs[2]; |
| 1025 | struct attribute_group agroup = { |
| 1026 | .name = "features", |
| 1027 | .attrs = attrs, |
| 1028 | }; |
| 1029 | u64 features = get_features(fs_info, set); |
| 1030 | features &= ~supported_feature_masks[set]; |
| 1031 | |
| 1032 | if (!features) |
| 1033 | continue; |
| 1034 | |
| 1035 | attrs[1] = NULL; |
| 1036 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 1037 | struct btrfs_feature_attr *fa; |
| 1038 | |
| 1039 | if (!(features & (1ULL << i))) |
| 1040 | continue; |
| 1041 | |
| 1042 | fa = &btrfs_feature_attrs[set][i]; |
| 1043 | attrs[0] = &fa->kobj_attr.attr; |
| 1044 | if (add) { |
| 1045 | int ret; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1046 | ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1047 | &agroup); |
| 1048 | if (ret) |
| 1049 | return ret; |
| 1050 | } else |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1051 | sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1052 | &agroup); |
| 1053 | } |
| 1054 | |
| 1055 | } |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 1059 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1060 | { |
Anand Jain | a013d14 | 2020-02-12 17:28:10 +0800 | [diff] [blame] | 1061 | if (fs_devs->devinfo_kobj) { |
| 1062 | kobject_del(fs_devs->devinfo_kobj); |
| 1063 | kobject_put(fs_devs->devinfo_kobj); |
| 1064 | fs_devs->devinfo_kobj = NULL; |
| 1065 | } |
| 1066 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1067 | if (fs_devs->devices_kobj) { |
| 1068 | kobject_del(fs_devs->devices_kobj); |
| 1069 | kobject_put(fs_devs->devices_kobj); |
| 1070 | fs_devs->devices_kobj = NULL; |
Anand Jain | aaf1330 | 2015-03-10 06:38:24 +0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1073 | if (fs_devs->fsid_kobj.state_initialized) { |
| 1074 | kobject_del(&fs_devs->fsid_kobj); |
| 1075 | kobject_put(&fs_devs->fsid_kobj); |
Anand Jain | f90fc54 | 2015-06-22 18:18:32 +0800 | [diff] [blame] | 1076 | wait_for_completion(&fs_devs->kobj_unregister); |
| 1077 | } |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 1080 | /* when fs_devs is NULL it will remove all fsid kobject */ |
Anand Jain | 1d1c1be | 2015-03-10 06:38:37 +0800 | [diff] [blame] | 1081 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 1082 | { |
| 1083 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 1084 | |
| 1085 | if (fs_devs) { |
| 1086 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 1090 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 1091 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 1092 | } |
| 1093 | } |
| 1094 | |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1095 | static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 1096 | { |
| 1097 | struct btrfs_device *device; |
Anand Jain | 30b0e4e | 2020-09-05 01:34:28 +0800 | [diff] [blame] | 1098 | struct btrfs_fs_devices *seed; |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1099 | |
| 1100 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 1101 | btrfs_sysfs_remove_device(device); |
Anand Jain | 30b0e4e | 2020-09-05 01:34:28 +0800 | [diff] [blame] | 1102 | |
| 1103 | list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { |
| 1104 | list_for_each_entry(device, &seed->devices, dev_list) |
| 1105 | btrfs_sysfs_remove_device(device); |
| 1106 | } |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 1109 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1110 | { |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 1111 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 1112 | |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 1113 | sysfs_remove_link(fsid_kobj, "bdi"); |
| 1114 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1115 | if (fs_info->space_info_kobj) { |
| 1116 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
| 1117 | kobject_del(fs_info->space_info_kobj); |
| 1118 | kobject_put(fs_info->space_info_kobj); |
| 1119 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 1120 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 1121 | if (fs_info->discard_debug_kobj) { |
| 1122 | sysfs_remove_files(fs_info->discard_debug_kobj, |
| 1123 | discard_debug_attrs); |
| 1124 | kobject_del(fs_info->discard_debug_kobj); |
| 1125 | kobject_put(fs_info->discard_debug_kobj); |
| 1126 | } |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 1127 | if (fs_info->debug_kobj) { |
| 1128 | sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
| 1129 | kobject_del(fs_info->debug_kobj); |
| 1130 | kobject_put(fs_info->debug_kobj); |
| 1131 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 1132 | #endif |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1133 | addrm_unknown_feature_attrs(fs_info, false); |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 1134 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1135 | sysfs_remove_files(fsid_kobj, btrfs_attrs); |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1136 | btrfs_sysfs_remove_fs_devices(fs_info->fs_devices); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1137 | } |
| 1138 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 1139 | static const char * const btrfs_feature_set_names[FEAT_MAX] = { |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1140 | [FEAT_COMPAT] = "compat", |
| 1141 | [FEAT_COMPAT_RO] = "compat_ro", |
| 1142 | [FEAT_INCOMPAT] = "incompat", |
| 1143 | }; |
| 1144 | |
David Sterba | 9e6df7c | 2020-08-17 10:56:00 +0200 | [diff] [blame] | 1145 | const char *btrfs_feature_set_name(enum btrfs_feature_set set) |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 1146 | { |
| 1147 | return btrfs_feature_set_names[set]; |
| 1148 | } |
| 1149 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1150 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 1151 | { |
| 1152 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 1153 | int len = 0; |
| 1154 | int i; |
| 1155 | char *str; |
| 1156 | |
| 1157 | str = kmalloc(bufsize, GFP_KERNEL); |
| 1158 | if (!str) |
| 1159 | return str; |
| 1160 | |
| 1161 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1162 | const char *name; |
| 1163 | |
| 1164 | if (!(flags & (1ULL << i))) |
| 1165 | continue; |
| 1166 | |
| 1167 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1168 | len += scnprintf(str + len, bufsize - len, "%s%s", |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1169 | len ? "," : "", name); |
| 1170 | } |
| 1171 | |
| 1172 | return str; |
| 1173 | } |
| 1174 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1175 | static void init_feature_attrs(void) |
| 1176 | { |
| 1177 | struct btrfs_feature_attr *fa; |
| 1178 | int set, i; |
| 1179 | |
| 1180 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 1181 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 1182 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 1183 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 1184 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1185 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 1186 | memset(btrfs_unknown_feature_names, 0, |
| 1187 | sizeof(btrfs_unknown_feature_names)); |
| 1188 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1189 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 1190 | struct btrfs_feature_attr *sfa; |
| 1191 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1192 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1193 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1194 | bit = ilog2(sfa->feature_bit); |
| 1195 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1196 | |
| 1197 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 1198 | } |
| 1199 | |
| 1200 | for (set = 0; set < FEAT_MAX; set++) { |
| 1201 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1202 | char *name = btrfs_unknown_feature_names[set][i]; |
| 1203 | fa = &btrfs_feature_attrs[set][i]; |
| 1204 | |
| 1205 | if (fa->kobj_attr.attr.name) |
| 1206 | continue; |
| 1207 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 1208 | snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1209 | btrfs_feature_set_names[set], i); |
| 1210 | |
| 1211 | fa->kobj_attr.attr.name = name; |
| 1212 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 1213 | fa->feature_set = set; |
| 1214 | fa->feature_bit = 1ULL << i; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1219 | /* |
| 1220 | * Create a sysfs entry for a given block group type at path |
| 1221 | * /sys/fs/btrfs/UUID/allocation/data/TYPE |
| 1222 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1223 | void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1224 | { |
| 1225 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 1226 | struct btrfs_space_info *space_info = cache->space_info; |
| 1227 | struct raid_kobject *rkobj; |
| 1228 | const int index = btrfs_bg_flags_to_raid_index(cache->flags); |
| 1229 | unsigned int nofs_flag; |
| 1230 | int ret; |
| 1231 | |
| 1232 | /* |
| 1233 | * Setup a NOFS context because kobject_add(), deep in its call chain, |
| 1234 | * does GFP_KERNEL allocations, and we are often called in a context |
| 1235 | * where if reclaim is triggered we can deadlock (we are either holding |
| 1236 | * a transaction handle or some lock required for a transaction |
| 1237 | * commit). |
| 1238 | */ |
| 1239 | nofs_flag = memalloc_nofs_save(); |
| 1240 | |
| 1241 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 1242 | if (!rkobj) { |
| 1243 | memalloc_nofs_restore(nofs_flag); |
| 1244 | btrfs_warn(cache->fs_info, |
| 1245 | "couldn't alloc memory for raid level kobject"); |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | rkobj->flags = cache->flags; |
| 1250 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
Josef Bacik | 49ea112 | 2020-09-01 17:40:38 -0400 | [diff] [blame] | 1251 | |
| 1252 | /* |
| 1253 | * We call this either on mount, or if we've created a block group for a |
| 1254 | * new index type while running (i.e. when restriping). The running |
| 1255 | * case is tricky because we could race with other threads, so we need |
| 1256 | * to have this check to make sure we didn't already init the kobject. |
| 1257 | * |
| 1258 | * We don't have to protect on the free side because it only happens on |
| 1259 | * unmount. |
| 1260 | */ |
| 1261 | spin_lock(&space_info->lock); |
| 1262 | if (space_info->block_group_kobjs[index]) { |
| 1263 | spin_unlock(&space_info->lock); |
| 1264 | kobject_put(&rkobj->kobj); |
| 1265 | return; |
| 1266 | } else { |
| 1267 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
| 1268 | } |
| 1269 | spin_unlock(&space_info->lock); |
| 1270 | |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1271 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", |
| 1272 | btrfs_bg_type_to_raid_name(rkobj->flags)); |
| 1273 | memalloc_nofs_restore(nofs_flag); |
| 1274 | if (ret) { |
Josef Bacik | 49ea112 | 2020-09-01 17:40:38 -0400 | [diff] [blame] | 1275 | spin_lock(&space_info->lock); |
| 1276 | space_info->block_group_kobjs[index] = NULL; |
| 1277 | spin_unlock(&space_info->lock); |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1278 | kobject_put(&rkobj->kobj); |
| 1279 | btrfs_warn(fs_info, |
| 1280 | "failed to add kobject for block cache, ignoring"); |
| 1281 | return; |
| 1282 | } |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
David Sterba | b5865ba | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1285 | /* |
| 1286 | * Remove sysfs directories for all block group types of a given space info and |
| 1287 | * the space info as well |
| 1288 | */ |
| 1289 | void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) |
| 1290 | { |
| 1291 | int i; |
| 1292 | |
| 1293 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 1294 | struct kobject *kobj; |
| 1295 | |
| 1296 | kobj = space_info->block_group_kobjs[i]; |
| 1297 | space_info->block_group_kobjs[i] = NULL; |
| 1298 | if (kobj) { |
| 1299 | kobject_del(kobj); |
| 1300 | kobject_put(kobj); |
| 1301 | } |
| 1302 | } |
| 1303 | kobject_del(&space_info->kobj); |
| 1304 | kobject_put(&space_info->kobj); |
| 1305 | } |
| 1306 | |
David Sterba | b882327 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1307 | static const char *alloc_name(u64 flags) |
| 1308 | { |
| 1309 | switch (flags) { |
| 1310 | case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 1311 | return "mixed"; |
| 1312 | case BTRFS_BLOCK_GROUP_METADATA: |
| 1313 | return "metadata"; |
| 1314 | case BTRFS_BLOCK_GROUP_DATA: |
| 1315 | return "data"; |
| 1316 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 1317 | return "system"; |
| 1318 | default: |
| 1319 | WARN_ON(1); |
| 1320 | return "invalid-combination"; |
Tom Rix | 445d8ab | 2020-11-01 07:30:08 -0800 | [diff] [blame] | 1321 | } |
David Sterba | b882327 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /* |
| 1325 | * Create a sysfs entry for a space info type at path |
| 1326 | * /sys/fs/btrfs/UUID/allocation/TYPE |
| 1327 | */ |
| 1328 | int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, |
| 1329 | struct btrfs_space_info *space_info) |
| 1330 | { |
| 1331 | int ret; |
| 1332 | |
| 1333 | ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, |
| 1334 | fs_info->space_info_kobj, "%s", |
| 1335 | alloc_name(space_info->flags)); |
| 1336 | if (ret) { |
| 1337 | kobject_put(&space_info->kobj); |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1344 | void btrfs_sysfs_remove_device(struct btrfs_device *device) |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1345 | { |
| 1346 | struct hd_struct *disk; |
| 1347 | struct kobject *disk_kobj; |
Anand Jain | 985e233 | 2020-09-05 01:34:24 +0800 | [diff] [blame] | 1348 | struct kobject *devices_kobj; |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1349 | |
Anand Jain | 985e233 | 2020-09-05 01:34:24 +0800 | [diff] [blame] | 1350 | /* |
| 1351 | * Seed fs_devices devices_kobj aren't used, fetch kobject from the |
| 1352 | * fs_info::fs_devices. |
| 1353 | */ |
| 1354 | devices_kobj = device->fs_info->fs_devices->devices_kobj; |
| 1355 | ASSERT(devices_kobj); |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1356 | |
Anand Jain | 985e233 | 2020-09-05 01:34:24 +0800 | [diff] [blame] | 1357 | if (device->bdev) { |
| 1358 | disk = device->bdev->bd_part; |
| 1359 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1360 | sysfs_remove_link(devices_kobj, disk_kobj->name); |
| 1361 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1362 | |
Anand Jain | 985e233 | 2020-09-05 01:34:24 +0800 | [diff] [blame] | 1363 | if (device->devid_kobj.state_initialized) { |
| 1364 | kobject_del(&device->devid_kobj); |
| 1365 | kobject_put(&device->devid_kobj); |
| 1366 | wait_for_completion(&device->kobj_unregister); |
| 1367 | } |
| 1368 | } |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1369 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1370 | static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, |
| 1371 | struct kobj_attribute *a, |
| 1372 | char *buf) |
| 1373 | { |
| 1374 | int val; |
| 1375 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1376 | devid_kobj); |
| 1377 | |
| 1378 | val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| 1379 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1380 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1381 | } |
| 1382 | BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); |
| 1383 | |
Anand Jain | 2586477 | 2020-02-13 16:40:53 +0800 | [diff] [blame] | 1384 | static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1385 | struct kobj_attribute *a, char *buf) |
| 1386 | { |
| 1387 | int val; |
| 1388 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1389 | devid_kobj); |
| 1390 | |
| 1391 | val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
| 1392 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1393 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1394 | } |
Anand Jain | 2586477 | 2020-02-13 16:40:53 +0800 | [diff] [blame] | 1395 | BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1396 | |
| 1397 | static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, |
| 1398 | struct kobj_attribute *a, |
| 1399 | char *buf) |
| 1400 | { |
| 1401 | int val; |
| 1402 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1403 | devid_kobj); |
| 1404 | |
| 1405 | val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| 1406 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1407 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1408 | } |
| 1409 | BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); |
| 1410 | |
| 1411 | static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, |
| 1412 | struct kobj_attribute *a, char *buf) |
| 1413 | { |
| 1414 | int val; |
| 1415 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1416 | devid_kobj); |
| 1417 | |
| 1418 | val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 1419 | |
Takashi Iwai | abdd9fe | 2020-03-22 10:09:11 +0100 | [diff] [blame] | 1420 | return scnprintf(buf, PAGE_SIZE, "%d\n", val); |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1421 | } |
| 1422 | BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); |
| 1423 | |
| 1424 | static struct attribute *devid_attrs[] = { |
| 1425 | BTRFS_ATTR_PTR(devid, in_fs_metadata), |
| 1426 | BTRFS_ATTR_PTR(devid, missing), |
| 1427 | BTRFS_ATTR_PTR(devid, replace_target), |
| 1428 | BTRFS_ATTR_PTR(devid, writeable), |
| 1429 | NULL |
| 1430 | }; |
| 1431 | ATTRIBUTE_GROUPS(devid); |
| 1432 | |
| 1433 | static void btrfs_release_devid_kobj(struct kobject *kobj) |
| 1434 | { |
| 1435 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 1436 | devid_kobj); |
| 1437 | |
| 1438 | memset(&device->devid_kobj, 0, sizeof(struct kobject)); |
| 1439 | complete(&device->kobj_unregister); |
| 1440 | } |
| 1441 | |
| 1442 | static struct kobj_type devid_ktype = { |
| 1443 | .sysfs_ops = &kobj_sysfs_ops, |
| 1444 | .default_groups = devid_groups, |
| 1445 | .release = btrfs_release_devid_kobj, |
| 1446 | }; |
| 1447 | |
Anand Jain | cd36da2 | 2020-09-05 01:34:26 +0800 | [diff] [blame] | 1448 | int btrfs_sysfs_add_device(struct btrfs_device *device) |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1449 | { |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1450 | int ret; |
Josef Bacik | a47bd78 | 2020-07-21 10:17:50 -0400 | [diff] [blame] | 1451 | unsigned int nofs_flag; |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1452 | struct kobject *devices_kobj; |
| 1453 | struct kobject *devinfo_kobj; |
| 1454 | |
| 1455 | /* |
| 1456 | * Make sure we use the fs_info::fs_devices to fetch the kobjects even |
| 1457 | * for the seed fs_devices |
| 1458 | */ |
| 1459 | devices_kobj = device->fs_info->fs_devices->devices_kobj; |
| 1460 | devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; |
| 1461 | ASSERT(devices_kobj); |
| 1462 | ASSERT(devinfo_kobj); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1463 | |
Josef Bacik | a47bd78 | 2020-07-21 10:17:50 -0400 | [diff] [blame] | 1464 | nofs_flag = memalloc_nofs_save(); |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1465 | |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1466 | if (device->bdev) { |
| 1467 | struct hd_struct *disk; |
| 1468 | struct kobject *disk_kobj; |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 1469 | |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1470 | disk = device->bdev->bd_part; |
| 1471 | disk_kobj = &part_to_dev(disk)->kobj; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1472 | |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1473 | ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name); |
| 1474 | if (ret) { |
| 1475 | btrfs_warn(device->fs_info, |
| 1476 | "creating sysfs device link for devid %llu failed: %d", |
| 1477 | device->devid, ret); |
| 1478 | goto out; |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1479 | } |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1480 | } |
| 1481 | |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1482 | init_completion(&device->kobj_unregister); |
| 1483 | ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype, |
| 1484 | devinfo_kobj, "%llu", device->devid); |
| 1485 | if (ret) { |
| 1486 | kobject_put(&device->devid_kobj); |
| 1487 | btrfs_warn(device->fs_info, |
| 1488 | "devinfo init for devid %llu failed: %d", |
| 1489 | device->devid, ret); |
| 1490 | } |
| 1491 | |
| 1492 | out: |
| 1493 | memalloc_nofs_restore(nofs_flag); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
Anand Jain | cd36da2 | 2020-09-05 01:34:26 +0800 | [diff] [blame] | 1497 | static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1498 | { |
| 1499 | int ret; |
Anand Jain | cd36da2 | 2020-09-05 01:34:26 +0800 | [diff] [blame] | 1500 | struct btrfs_device *device; |
Anand Jain | 30b0e4e | 2020-09-05 01:34:28 +0800 | [diff] [blame] | 1501 | struct btrfs_fs_devices *seed; |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1502 | |
| 1503 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 1504 | ret = btrfs_sysfs_add_device(device); |
| 1505 | if (ret) |
Anand Jain | 7ad3912 | 2020-09-05 01:34:29 +0800 | [diff] [blame] | 1506 | goto fail; |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Anand Jain | 30b0e4e | 2020-09-05 01:34:28 +0800 | [diff] [blame] | 1509 | list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { |
| 1510 | list_for_each_entry(device, &seed->devices, dev_list) { |
| 1511 | ret = btrfs_sysfs_add_device(device); |
| 1512 | if (ret) |
Anand Jain | 7ad3912 | 2020-09-05 01:34:29 +0800 | [diff] [blame] | 1513 | goto fail; |
Anand Jain | 30b0e4e | 2020-09-05 01:34:28 +0800 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
Anand Jain | 178a16c | 2020-09-05 01:34:23 +0800 | [diff] [blame] | 1517 | return 0; |
Anand Jain | 7ad3912 | 2020-09-05 01:34:29 +0800 | [diff] [blame] | 1518 | |
| 1519 | fail: |
| 1520 | btrfs_sysfs_remove_fs_devices(fs_devices); |
| 1521 | return ret; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1522 | } |
| 1523 | |
David Sterba | 5b28692 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1524 | void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) |
| 1525 | { |
| 1526 | int ret; |
| 1527 | |
| 1528 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 1529 | if (ret) |
| 1530 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 1531 | action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 1532 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 1533 | } |
| 1534 | |
Nikolay Borisov | 8e56008 | 2020-08-12 16:18:51 +0300 | [diff] [blame] | 1535 | void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) |
| 1536 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1537 | { |
| 1538 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 1539 | |
| 1540 | /* |
| 1541 | * Sprouting changes fsid of the mounted filesystem, rename the fsid |
| 1542 | * directory |
| 1543 | */ |
Nikolay Borisov | 8e56008 | 2020-08-12 16:18:51 +0300 | [diff] [blame] | 1544 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1545 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
| 1546 | btrfs_warn(fs_devices->fs_info, |
| 1547 | "sysfs: failed to create fsid for sprout"); |
| 1548 | } |
| 1549 | |
Anand Jain | 668e48af | 2020-01-06 19:38:31 +0800 | [diff] [blame] | 1550 | void btrfs_sysfs_update_devid(struct btrfs_device *device) |
| 1551 | { |
| 1552 | char tmp[24]; |
| 1553 | |
| 1554 | snprintf(tmp, sizeof(tmp), "%llu", device->devid); |
| 1555 | |
| 1556 | if (kobject_rename(&device->devid_kobj, tmp)) |
| 1557 | btrfs_warn(device->fs_devices->fs_info, |
| 1558 | "sysfs: failed to update devid for %llu", |
| 1559 | device->devid); |
| 1560 | } |
| 1561 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1562 | /* /sys/fs/btrfs/ entry */ |
| 1563 | static struct kset *btrfs_kset; |
| 1564 | |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1565 | /* |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1566 | * Creates: |
| 1567 | * /sys/fs/btrfs/UUID |
| 1568 | * |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1569 | * Can be called by the device discovery thread. |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1570 | */ |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1571 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1572 | { |
| 1573 | int error; |
| 1574 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1575 | init_completion(&fs_devs->kobj_unregister); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1576 | fs_devs->fsid_kobj.kset = btrfs_kset; |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1577 | error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, |
| 1578 | "%pU", fs_devs->fsid); |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1579 | if (error) { |
| 1580 | kobject_put(&fs_devs->fsid_kobj); |
| 1581 | return error; |
| 1582 | } |
| 1583 | |
Anand Jain | bc036bb | 2019-11-21 17:33:34 +0800 | [diff] [blame] | 1584 | fs_devs->devices_kobj = kobject_create_and_add("devices", |
| 1585 | &fs_devs->fsid_kobj); |
| 1586 | if (!fs_devs->devices_kobj) { |
| 1587 | btrfs_err(fs_devs->fs_info, |
| 1588 | "failed to init sysfs device interface"); |
Anand Jain | 1f6087e | 2020-02-12 17:28:12 +0800 | [diff] [blame] | 1589 | btrfs_sysfs_remove_fsid(fs_devs); |
Anand Jain | bc036bb | 2019-11-21 17:33:34 +0800 | [diff] [blame] | 1590 | return -ENOMEM; |
| 1591 | } |
| 1592 | |
Anand Jain | a013d14 | 2020-02-12 17:28:10 +0800 | [diff] [blame] | 1593 | fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", |
| 1594 | &fs_devs->fsid_kobj); |
| 1595 | if (!fs_devs->devinfo_kobj) { |
| 1596 | btrfs_err(fs_devs->fs_info, |
| 1597 | "failed to init sysfs devinfo kobject"); |
| 1598 | btrfs_sysfs_remove_fsid(fs_devs); |
| 1599 | return -ENOMEM; |
| 1600 | } |
| 1601 | |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1602 | return 0; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Anand Jain | 96f3136 | 2015-08-14 18:32:46 +0800 | [diff] [blame] | 1605 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1606 | { |
| 1607 | int error; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1608 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1609 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1610 | |
Anand Jain | cd36da2 | 2020-09-05 01:34:26 +0800 | [diff] [blame] | 1611 | error = btrfs_sysfs_add_fs_devices(fs_devs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1612 | if (error) |
| 1613 | return error; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1614 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1615 | error = sysfs_create_files(fsid_kobj, btrfs_attrs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1616 | if (error) { |
Anand Jain | 53f8a74 | 2020-09-05 01:34:27 +0800 | [diff] [blame] | 1617 | btrfs_sysfs_remove_fs_devices(fs_devs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1618 | return error; |
| 1619 | } |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1620 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1621 | error = sysfs_create_group(fsid_kobj, |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 1622 | &btrfs_feature_attr_group); |
| 1623 | if (error) |
| 1624 | goto failure; |
| 1625 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1626 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 1627 | fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); |
| 1628 | if (!fs_info->debug_kobj) { |
| 1629 | error = -ENOMEM; |
| 1630 | goto failure; |
| 1631 | } |
| 1632 | |
| 1633 | error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1634 | if (error) |
| 1635 | goto failure; |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 1636 | |
| 1637 | /* Discard directory */ |
| 1638 | fs_info->discard_debug_kobj = kobject_create_and_add("discard", |
| 1639 | fs_info->debug_kobj); |
| 1640 | if (!fs_info->discard_debug_kobj) { |
| 1641 | error = -ENOMEM; |
| 1642 | goto failure; |
| 1643 | } |
| 1644 | |
| 1645 | error = sysfs_create_files(fs_info->discard_debug_kobj, |
| 1646 | discard_debug_attrs); |
| 1647 | if (error) |
| 1648 | goto failure; |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1649 | #endif |
| 1650 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1651 | error = addrm_unknown_feature_attrs(fs_info, true); |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1652 | if (error) |
| 1653 | goto failure; |
| 1654 | |
Nikolay Borisov | 3092c68 | 2020-07-03 11:13:15 +0300 | [diff] [blame] | 1655 | error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); |
| 1656 | if (error) |
| 1657 | goto failure; |
| 1658 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1659 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1660 | fsid_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1661 | if (!fs_info->space_info_kobj) { |
| 1662 | error = -ENOMEM; |
| 1663 | goto failure; |
| 1664 | } |
| 1665 | |
| 1666 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 1667 | if (error) |
| 1668 | goto failure; |
| 1669 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1670 | return 0; |
| 1671 | failure: |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 1672 | btrfs_sysfs_remove_mounted(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1673 | return error; |
| 1674 | } |
| 1675 | |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1676 | static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) |
| 1677 | { |
| 1678 | return to_fs_info(kobj->parent->parent); |
| 1679 | } |
| 1680 | |
| 1681 | #define QGROUP_ATTR(_member, _show_name) \ |
| 1682 | static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ |
| 1683 | struct kobj_attribute *a, \ |
| 1684 | char *buf) \ |
| 1685 | { \ |
| 1686 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 1687 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 1688 | struct btrfs_qgroup, kobj); \ |
| 1689 | return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ |
| 1690 | } \ |
| 1691 | BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) |
| 1692 | |
| 1693 | #define QGROUP_RSV_ATTR(_name, _type) \ |
| 1694 | static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ |
| 1695 | struct kobj_attribute *a, \ |
| 1696 | char *buf) \ |
| 1697 | { \ |
| 1698 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 1699 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 1700 | struct btrfs_qgroup, kobj); \ |
| 1701 | return btrfs_show_u64(&qgroup->rsv.values[_type], \ |
| 1702 | &fs_info->qgroup_lock, buf); \ |
| 1703 | } \ |
| 1704 | BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) |
| 1705 | |
| 1706 | QGROUP_ATTR(rfer, referenced); |
| 1707 | QGROUP_ATTR(excl, exclusive); |
| 1708 | QGROUP_ATTR(max_rfer, max_referenced); |
| 1709 | QGROUP_ATTR(max_excl, max_exclusive); |
| 1710 | QGROUP_ATTR(lim_flags, limit_flags); |
| 1711 | QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); |
| 1712 | QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); |
| 1713 | QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); |
| 1714 | |
| 1715 | static struct attribute *qgroup_attrs[] = { |
| 1716 | BTRFS_ATTR_PTR(qgroup, referenced), |
| 1717 | BTRFS_ATTR_PTR(qgroup, exclusive), |
| 1718 | BTRFS_ATTR_PTR(qgroup, max_referenced), |
| 1719 | BTRFS_ATTR_PTR(qgroup, max_exclusive), |
| 1720 | BTRFS_ATTR_PTR(qgroup, limit_flags), |
| 1721 | BTRFS_ATTR_PTR(qgroup, rsv_data), |
| 1722 | BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), |
| 1723 | BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), |
| 1724 | NULL |
| 1725 | }; |
| 1726 | ATTRIBUTE_GROUPS(qgroup); |
| 1727 | |
| 1728 | static void qgroup_release(struct kobject *kobj) |
| 1729 | { |
| 1730 | struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); |
| 1731 | |
| 1732 | memset(&qgroup->kobj, 0, sizeof(*kobj)); |
| 1733 | } |
| 1734 | |
| 1735 | static struct kobj_type qgroup_ktype = { |
| 1736 | .sysfs_ops = &kobj_sysfs_ops, |
| 1737 | .release = qgroup_release, |
| 1738 | .default_groups = qgroup_groups, |
| 1739 | }; |
| 1740 | |
| 1741 | int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, |
| 1742 | struct btrfs_qgroup *qgroup) |
| 1743 | { |
| 1744 | struct kobject *qgroups_kobj = fs_info->qgroups_kobj; |
| 1745 | int ret; |
| 1746 | |
| 1747 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1748 | return 0; |
| 1749 | if (qgroup->kobj.state_initialized) |
| 1750 | return 0; |
| 1751 | if (!qgroups_kobj) |
| 1752 | return -EINVAL; |
| 1753 | |
| 1754 | ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, |
| 1755 | "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), |
| 1756 | btrfs_qgroup_subvolid(qgroup->qgroupid)); |
| 1757 | if (ret < 0) |
| 1758 | kobject_put(&qgroup->kobj); |
| 1759 | |
| 1760 | return ret; |
| 1761 | } |
| 1762 | |
| 1763 | void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) |
| 1764 | { |
| 1765 | struct btrfs_qgroup *qgroup; |
| 1766 | struct btrfs_qgroup *next; |
| 1767 | |
| 1768 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1769 | return; |
| 1770 | |
| 1771 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 1772 | &fs_info->qgroup_tree, node) |
| 1773 | btrfs_sysfs_del_one_qgroup(fs_info, qgroup); |
Qu Wenruo | 62ab2cc | 2020-08-03 14:20:11 +0800 | [diff] [blame] | 1774 | if (fs_info->qgroups_kobj) { |
| 1775 | kobject_del(fs_info->qgroups_kobj); |
| 1776 | kobject_put(fs_info->qgroups_kobj); |
| 1777 | fs_info->qgroups_kobj = NULL; |
| 1778 | } |
Qu Wenruo | 49e5fb4 | 2020-06-28 13:07:15 +0800 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /* Called when qgroups get initialized, thus there is no need for locking */ |
| 1782 | int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) |
| 1783 | { |
| 1784 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 1785 | struct btrfs_qgroup *qgroup; |
| 1786 | struct btrfs_qgroup *next; |
| 1787 | int ret = 0; |
| 1788 | |
| 1789 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1790 | return 0; |
| 1791 | |
| 1792 | ASSERT(fsid_kobj); |
| 1793 | if (fs_info->qgroups_kobj) |
| 1794 | return 0; |
| 1795 | |
| 1796 | fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); |
| 1797 | if (!fs_info->qgroups_kobj) { |
| 1798 | ret = -ENOMEM; |
| 1799 | goto out; |
| 1800 | } |
| 1801 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 1802 | &fs_info->qgroup_tree, node) { |
| 1803 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 1804 | if (ret < 0) |
| 1805 | goto out; |
| 1806 | } |
| 1807 | |
| 1808 | out: |
| 1809 | if (ret < 0) |
| 1810 | btrfs_sysfs_del_qgroups(fs_info); |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, |
| 1815 | struct btrfs_qgroup *qgroup) |
| 1816 | { |
| 1817 | if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) |
| 1818 | return; |
| 1819 | |
| 1820 | if (qgroup->kobj.state_initialized) { |
| 1821 | kobject_del(&qgroup->kobj); |
| 1822 | kobject_put(&qgroup->kobj); |
| 1823 | } |
| 1824 | } |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1825 | |
| 1826 | /* |
| 1827 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 1828 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 1829 | */ |
| 1830 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, |
| 1831 | u64 bit, enum btrfs_feature_set set) |
| 1832 | { |
| 1833 | struct btrfs_fs_devices *fs_devs; |
| 1834 | struct kobject *fsid_kobj; |
Leon Romanovsky | 2464648 | 2020-08-19 17:16:28 +0300 | [diff] [blame] | 1835 | u64 __maybe_unused features; |
| 1836 | int __maybe_unused ret; |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1837 | |
| 1838 | if (!fs_info) |
| 1839 | return; |
| 1840 | |
Leon Romanovsky | 2464648 | 2020-08-19 17:16:28 +0300 | [diff] [blame] | 1841 | /* |
| 1842 | * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not |
| 1843 | * safe when called from some contexts (eg. balance) |
| 1844 | */ |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1845 | features = get_features(fs_info, set); |
| 1846 | ASSERT(bit & supported_feature_masks[set]); |
| 1847 | |
| 1848 | fs_devs = fs_info->fs_devices; |
| 1849 | fsid_kobj = &fs_devs->fsid_kobj; |
| 1850 | |
David Sterba | bf60920 | 2016-01-27 14:06:29 +0100 | [diff] [blame] | 1851 | if (!fsid_kobj->state_initialized) |
| 1852 | return; |
| 1853 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1854 | /* |
| 1855 | * FIXME: this is too heavy to update just one value, ideally we'd like |
| 1856 | * to use sysfs_update_group but some refactoring is needed first. |
| 1857 | */ |
| 1858 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1859 | ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1860 | } |
| 1861 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 1862 | int __init btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1863 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1864 | int ret; |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1865 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1866 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 1867 | if (!btrfs_kset) |
| 1868 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1869 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1870 | init_feature_attrs(); |
| 1871 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1872 | if (ret) |
| 1873 | goto out2; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1874 | ret = sysfs_merge_group(&btrfs_kset->kobj, |
| 1875 | &btrfs_static_feature_attr_group); |
| 1876 | if (ret) |
| 1877 | goto out_remove_group; |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1878 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1879 | #ifdef CONFIG_BTRFS_DEBUG |
| 1880 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1881 | if (ret) |
| 1882 | goto out2; |
| 1883 | #endif |
| 1884 | |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1885 | return 0; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1886 | |
| 1887 | out_remove_group: |
| 1888 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1889 | out2: |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1890 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1891 | |
| 1892 | return ret; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1893 | } |
| 1894 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1895 | void __cold btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1896 | { |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1897 | sysfs_unmerge_group(&btrfs_kset->kobj, |
| 1898 | &btrfs_static_feature_attr_group); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1899 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 1900 | #ifdef CONFIG_BTRFS_DEBUG |
| 1901 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1902 | #endif |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1903 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1904 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 1905 | |