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" |
| 17 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 18 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 19 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 20 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 21 | #include "block-group.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 22 | |
David Sterba | 9188db6 | 2019-08-02 12:52:48 +0200 | [diff] [blame] | 23 | struct btrfs_feature_attr { |
| 24 | struct kobj_attribute kobj_attr; |
| 25 | enum btrfs_feature_set feature_set; |
| 26 | u64 feature_bit; |
| 27 | }; |
| 28 | |
| 29 | /* For raid type sysfs entries */ |
| 30 | struct raid_kobject { |
| 31 | u64 flags; |
| 32 | struct kobject kobj; |
| 33 | }; |
| 34 | |
| 35 | #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ |
| 36 | { \ |
| 37 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 38 | .show = _show, \ |
| 39 | .store = _store, \ |
| 40 | } |
| 41 | |
| 42 | #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ |
| 43 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 44 | __INIT_KOBJ_ATTR(_name, 0644, _show, _store) |
| 45 | |
| 46 | #define BTRFS_ATTR(_prefix, _name, _show) \ |
| 47 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 48 | __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) |
| 49 | |
| 50 | #define BTRFS_ATTR_PTR(_prefix, _name) \ |
| 51 | (&btrfs_attr_##_prefix##_##_name.attr) |
| 52 | |
| 53 | #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ |
| 54 | static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ |
| 55 | .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ |
| 56 | btrfs_feature_attr_show, \ |
| 57 | btrfs_feature_attr_store), \ |
| 58 | .feature_set = _feature_set, \ |
| 59 | .feature_bit = _feature_prefix ##_## _feature_bit, \ |
| 60 | } |
| 61 | #define BTRFS_FEAT_ATTR_PTR(_name) \ |
| 62 | (&btrfs_attr_features_##_name.kobj_attr.attr) |
| 63 | |
| 64 | #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ |
| 65 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) |
| 66 | #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ |
| 67 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) |
| 68 | #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ |
| 69 | BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) |
| 70 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 71 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 72 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 73 | |
David Sterba | 8f52316 | 2019-08-02 13:07:38 +0200 | [diff] [blame] | 74 | static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) |
| 75 | { |
| 76 | return container_of(a, struct btrfs_feature_attr, kobj_attr); |
| 77 | } |
| 78 | |
| 79 | static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) |
| 80 | { |
| 81 | return container_of(attr, struct kobj_attribute, attr); |
| 82 | } |
| 83 | |
| 84 | static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( |
| 85 | struct attribute *attr) |
| 86 | { |
| 87 | return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); |
| 88 | } |
| 89 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 90 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 91 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 92 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 93 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 94 | if (set == FEAT_COMPAT) |
| 95 | return btrfs_super_compat_flags(disk_super); |
| 96 | else if (set == FEAT_COMPAT_RO) |
| 97 | return btrfs_super_compat_ro_flags(disk_super); |
| 98 | else |
| 99 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 102 | static void set_features(struct btrfs_fs_info *fs_info, |
| 103 | enum btrfs_feature_set set, u64 features) |
| 104 | { |
| 105 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 106 | if (set == FEAT_COMPAT) |
| 107 | btrfs_set_super_compat_flags(disk_super, features); |
| 108 | else if (set == FEAT_COMPAT_RO) |
| 109 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 110 | else |
| 111 | btrfs_set_super_incompat_flags(disk_super, features); |
| 112 | } |
| 113 | |
| 114 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 115 | { |
| 116 | int val = 0; |
| 117 | u64 set, clear; |
| 118 | switch (fa->feature_set) { |
| 119 | case FEAT_COMPAT: |
| 120 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 121 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 122 | break; |
| 123 | case FEAT_COMPAT_RO: |
| 124 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 125 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 126 | break; |
| 127 | case FEAT_INCOMPAT: |
| 128 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 129 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 130 | break; |
| 131 | default: |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 132 | pr_warn("btrfs: sysfs: unknown feature set %d\n", |
David Sterba | cc37bb0 | 2013-11-19 13:36:21 +0100 | [diff] [blame] | 133 | fa->feature_set); |
| 134 | return 0; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (set & fa->feature_bit) |
| 138 | val |= 1; |
| 139 | if (clear & fa->feature_bit) |
| 140 | val |= 2; |
| 141 | |
| 142 | return val; |
| 143 | } |
| 144 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 145 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 146 | struct kobj_attribute *a, char *buf) |
| 147 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 148 | int val = 0; |
| 149 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 150 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 151 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 152 | u64 features = get_features(fs_info, fa->feature_set); |
| 153 | if (features & fa->feature_bit) |
| 154 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 155 | } else |
| 156 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 157 | |
| 158 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 159 | } |
| 160 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 161 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 162 | struct kobj_attribute *a, |
| 163 | const char *buf, size_t count) |
| 164 | { |
| 165 | struct btrfs_fs_info *fs_info; |
| 166 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 167 | u64 features, set, clear; |
| 168 | unsigned long val; |
| 169 | int ret; |
| 170 | |
| 171 | fs_info = to_fs_info(kobj); |
| 172 | if (!fs_info) |
| 173 | return -EPERM; |
| 174 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 175 | if (sb_rdonly(fs_info->sb)) |
David Sterba | ee61113 | 2015-01-23 18:43:31 +0100 | [diff] [blame] | 176 | return -EROFS; |
| 177 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 178 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | if (fa->feature_set == FEAT_COMPAT) { |
| 183 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 184 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 185 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 186 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 187 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 188 | } else { |
| 189 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 190 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 191 | } |
| 192 | |
| 193 | features = get_features(fs_info, fa->feature_set); |
| 194 | |
| 195 | /* Nothing to do */ |
| 196 | if ((val && (features & fa->feature_bit)) || |
| 197 | (!val && !(features & fa->feature_bit))) |
| 198 | return count; |
| 199 | |
| 200 | if ((val && !(set & fa->feature_bit)) || |
| 201 | (!val && !(clear & fa->feature_bit))) { |
| 202 | btrfs_info(fs_info, |
| 203 | "%sabling feature %s on mounted fs is not supported.", |
| 204 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 205 | return -EPERM; |
| 206 | } |
| 207 | |
| 208 | btrfs_info(fs_info, "%s %s feature flag", |
| 209 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 210 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 211 | spin_lock(&fs_info->super_lock); |
| 212 | features = get_features(fs_info, fa->feature_set); |
| 213 | if (val) |
| 214 | features |= fa->feature_bit; |
| 215 | else |
| 216 | features &= ~fa->feature_bit; |
| 217 | set_features(fs_info, fa->feature_set, features); |
| 218 | spin_unlock(&fs_info->super_lock); |
| 219 | |
David Sterba | 0eae274 | 2014-11-12 14:22:21 +0100 | [diff] [blame] | 220 | /* |
| 221 | * We don't want to do full transaction commit from inside sysfs |
| 222 | */ |
| 223 | btrfs_set_pending(fs_info, COMMIT); |
| 224 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 225 | |
| 226 | return count; |
| 227 | } |
| 228 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 229 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 230 | struct attribute *attr, int unused) |
| 231 | { |
| 232 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 233 | umode_t mode = attr->mode; |
| 234 | |
| 235 | if (fs_info) { |
| 236 | struct btrfs_feature_attr *fa; |
| 237 | u64 features; |
| 238 | |
| 239 | fa = attr_to_btrfs_feature_attr(attr); |
| 240 | features = get_features(fs_info, fa->feature_set); |
| 241 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 242 | if (can_modify_feature(fa)) |
| 243 | mode |= S_IWUSR; |
| 244 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 245 | mode = 0; |
| 246 | } |
| 247 | |
| 248 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 252 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 253 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 254 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 255 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 256 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 257 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 258 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 259 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 260 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 261 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 262 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 263 | BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 264 | |
| 265 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 266 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 267 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 268 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 269 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 270 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 271 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 272 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 273 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 274 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 275 | BTRFS_FEAT_ATTR_PTR(no_holes), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 276 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 277 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 278 | BTRFS_FEAT_ATTR_PTR(raid1c34), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 279 | NULL |
| 280 | }; |
| 281 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 282 | /* |
| 283 | * Features which depend on feature bits and may differ between each fs. |
| 284 | * |
| 285 | * /sys/fs/btrfs/features lists all available features of this kernel while |
| 286 | * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or |
| 287 | * can be changed online. |
| 288 | */ |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 289 | static const struct attribute_group btrfs_feature_attr_group = { |
| 290 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 291 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 292 | .attrs = btrfs_supported_feature_attrs, |
| 293 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 294 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 295 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 296 | struct kobj_attribute *ka, char *buf) |
| 297 | { |
| 298 | return snprintf(buf, PAGE_SIZE, "0\n"); |
| 299 | } |
| 300 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 301 | |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 302 | static ssize_t supported_checksums_show(struct kobject *kobj, |
| 303 | struct kobj_attribute *a, char *buf) |
| 304 | { |
| 305 | ssize_t ret = 0; |
| 306 | int i; |
| 307 | |
| 308 | for (i = 0; i < btrfs_get_num_csums(); i++) { |
| 309 | /* |
| 310 | * This "trick" only works as long as 'enum btrfs_csum_type' has |
| 311 | * no holes in it |
| 312 | */ |
| 313 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s", |
| 314 | (i == 0 ? "" : " "), btrfs_super_csum_name(i)); |
| 315 | |
| 316 | } |
| 317 | |
| 318 | ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); |
| 319 | return ret; |
| 320 | } |
| 321 | BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); |
| 322 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 323 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 324 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 325 | BTRFS_ATTR_PTR(static_feature, supported_checksums), |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 326 | NULL |
| 327 | }; |
| 328 | |
| 329 | /* |
| 330 | * Features which only depend on kernel version. |
| 331 | * |
| 332 | * These are listed in /sys/fs/btrfs/features along with |
| 333 | * btrfs_feature_attr_group |
| 334 | */ |
| 335 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 336 | .name = "features", |
| 337 | .attrs = btrfs_supported_static_feature_attrs, |
| 338 | }; |
| 339 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 340 | #ifdef CONFIG_BTRFS_DEBUG |
| 341 | |
| 342 | /* |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 343 | * Discard statistics and tunables |
| 344 | */ |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 345 | #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) |
| 346 | |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 347 | static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, |
| 348 | struct kobj_attribute *a, |
| 349 | char *buf) |
| 350 | { |
| 351 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 352 | |
| 353 | return snprintf(buf, PAGE_SIZE, "%lld\n", |
| 354 | atomic64_read(&fs_info->discard_ctl.discardable_bytes)); |
| 355 | } |
| 356 | BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); |
| 357 | |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 358 | static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, |
| 359 | struct kobj_attribute *a, |
| 360 | char *buf) |
| 361 | { |
| 362 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 363 | |
| 364 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 365 | atomic_read(&fs_info->discard_ctl.discardable_extents)); |
| 366 | } |
| 367 | BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); |
| 368 | |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame^] | 369 | static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, |
| 370 | struct kobj_attribute *a, |
| 371 | char *buf) |
| 372 | { |
| 373 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 374 | |
| 375 | return snprintf(buf, PAGE_SIZE, "%lld\n", |
| 376 | fs_info->discard_ctl.discard_bitmap_bytes); |
| 377 | } |
| 378 | BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); |
| 379 | |
| 380 | static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, |
| 381 | struct kobj_attribute *a, |
| 382 | char *buf) |
| 383 | { |
| 384 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 385 | |
| 386 | return snprintf(buf, PAGE_SIZE, "%lld\n", |
| 387 | atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); |
| 388 | } |
| 389 | BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); |
| 390 | |
| 391 | static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, |
| 392 | struct kobj_attribute *a, |
| 393 | char *buf) |
| 394 | { |
| 395 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 396 | |
| 397 | return snprintf(buf, PAGE_SIZE, "%lld\n", |
| 398 | fs_info->discard_ctl.discard_extent_bytes); |
| 399 | } |
| 400 | BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); |
| 401 | |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 402 | static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, |
| 403 | struct kobj_attribute *a, |
| 404 | char *buf) |
| 405 | { |
| 406 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 407 | |
| 408 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 409 | READ_ONCE(fs_info->discard_ctl.iops_limit)); |
| 410 | } |
| 411 | |
| 412 | static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, |
| 413 | struct kobj_attribute *a, |
| 414 | const char *buf, size_t len) |
| 415 | { |
| 416 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 417 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 418 | u32 iops_limit; |
| 419 | int ret; |
| 420 | |
| 421 | ret = kstrtou32(buf, 10, &iops_limit); |
| 422 | if (ret) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | WRITE_ONCE(discard_ctl->iops_limit, iops_limit); |
| 426 | |
| 427 | return len; |
| 428 | } |
| 429 | BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, |
| 430 | btrfs_discard_iops_limit_store); |
| 431 | |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 432 | static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, |
| 433 | struct kobj_attribute *a, |
| 434 | char *buf) |
| 435 | { |
| 436 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 437 | |
| 438 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 439 | READ_ONCE(fs_info->discard_ctl.kbps_limit)); |
| 440 | } |
| 441 | |
| 442 | static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, |
| 443 | struct kobj_attribute *a, |
| 444 | const char *buf, size_t len) |
| 445 | { |
| 446 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 447 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 448 | u32 kbps_limit; |
| 449 | int ret; |
| 450 | |
| 451 | ret = kstrtou32(buf, 10, &kbps_limit); |
| 452 | if (ret) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); |
| 456 | |
| 457 | return len; |
| 458 | } |
| 459 | BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, |
| 460 | btrfs_discard_kbps_limit_store); |
| 461 | |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 462 | static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, |
| 463 | struct kobj_attribute *a, |
| 464 | char *buf) |
| 465 | { |
| 466 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 467 | |
| 468 | return snprintf(buf, PAGE_SIZE, "%llu\n", |
| 469 | READ_ONCE(fs_info->discard_ctl.max_discard_size)); |
| 470 | } |
| 471 | |
| 472 | static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, |
| 473 | struct kobj_attribute *a, |
| 474 | const char *buf, size_t len) |
| 475 | { |
| 476 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 477 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 478 | u64 max_discard_size; |
| 479 | int ret; |
| 480 | |
| 481 | ret = kstrtou64(buf, 10, &max_discard_size); |
| 482 | if (ret) |
| 483 | return -EINVAL; |
| 484 | |
| 485 | WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); |
| 486 | |
| 487 | return len; |
| 488 | } |
| 489 | BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, |
| 490 | btrfs_discard_max_discard_size_store); |
| 491 | |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 492 | static const struct attribute *discard_debug_attrs[] = { |
Dennis Zhou | 5dc7c10 | 2019-12-13 16:22:21 -0800 | [diff] [blame] | 493 | BTRFS_ATTR_PTR(discard, discardable_bytes), |
Dennis Zhou | dfb79dd | 2019-12-13 16:22:20 -0800 | [diff] [blame] | 494 | BTRFS_ATTR_PTR(discard, discardable_extents), |
Dennis Zhou | 9ddf648 | 2020-01-02 16:26:41 -0500 | [diff] [blame^] | 495 | BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), |
| 496 | BTRFS_ATTR_PTR(discard, discard_bytes_saved), |
| 497 | BTRFS_ATTR_PTR(discard, discard_extent_bytes), |
Dennis Zhou | a230930 | 2020-01-02 16:26:35 -0500 | [diff] [blame] | 498 | BTRFS_ATTR_PTR(discard, iops_limit), |
Dennis Zhou | e93591b | 2020-01-02 16:26:36 -0500 | [diff] [blame] | 499 | BTRFS_ATTR_PTR(discard, kbps_limit), |
Dennis Zhou | 19b2a2c | 2020-01-02 16:26:38 -0500 | [diff] [blame] | 500 | BTRFS_ATTR_PTR(discard, max_discard_size), |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 501 | NULL, |
| 502 | }; |
| 503 | |
| 504 | /* |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 505 | * Runtime debugging exported via sysfs |
| 506 | * |
| 507 | * /sys/fs/btrfs/debug - applies to module or all filesystems |
| 508 | * /sys/fs/btrfs/UUID - applies only to the given filesystem |
| 509 | */ |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 510 | static const struct attribute *btrfs_debug_mount_attrs[] = { |
| 511 | NULL, |
| 512 | }; |
| 513 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 514 | static struct attribute *btrfs_debug_feature_attrs[] = { |
| 515 | NULL |
| 516 | }; |
| 517 | |
| 518 | static const struct attribute_group btrfs_debug_feature_attr_group = { |
| 519 | .name = "debug", |
| 520 | .attrs = btrfs_debug_feature_attrs, |
| 521 | }; |
| 522 | |
| 523 | #endif |
| 524 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 525 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 526 | { |
| 527 | u64 val; |
| 528 | if (lock) |
| 529 | spin_lock(lock); |
| 530 | val = *value_ptr; |
| 531 | if (lock) |
| 532 | spin_unlock(lock); |
| 533 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 534 | } |
| 535 | |
| 536 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 537 | struct kobj_attribute *ka, char *buf) |
| 538 | { |
| 539 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 540 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 541 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 542 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 543 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 544 | |
| 545 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 546 | struct kobj_attribute *a, char *buf) |
| 547 | { |
| 548 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 549 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 550 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 551 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 552 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 553 | |
| 554 | #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] | 555 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 556 | |
| 557 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 558 | struct kobj_attribute *attr, char *buf); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 559 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 560 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 561 | |
| 562 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 563 | struct kobj_attribute *attr, char *buf) |
| 564 | |
| 565 | { |
| 566 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 567 | struct btrfs_block_group *block_group; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 568 | 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] | 569 | u64 val = 0; |
| 570 | |
| 571 | down_read(&sinfo->groups_sem); |
| 572 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 573 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 574 | val += block_group->length; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 575 | else |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 576 | val += block_group->used; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 577 | } |
| 578 | up_read(&sinfo->groups_sem); |
| 579 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 580 | } |
| 581 | |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 582 | static struct attribute *raid_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 583 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 584 | BTRFS_ATTR_PTR(raid, used_bytes), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 585 | NULL |
| 586 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 587 | ATTRIBUTE_GROUPS(raid); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 588 | |
| 589 | static void release_raid_kobj(struct kobject *kobj) |
| 590 | { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 591 | kfree(to_raid_kobj(kobj)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 592 | } |
| 593 | |
David Sterba | 536ea45 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 594 | static struct kobj_type btrfs_raid_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 595 | .sysfs_ops = &kobj_sysfs_ops, |
| 596 | .release = release_raid_kobj, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 597 | .default_groups = raid_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 598 | }; |
| 599 | |
| 600 | #define SPACE_INFO_ATTR(field) \ |
| 601 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 602 | struct kobj_attribute *a, \ |
| 603 | char *buf) \ |
| 604 | { \ |
| 605 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 606 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 607 | } \ |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 608 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 609 | |
| 610 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 611 | struct kobj_attribute *a, |
| 612 | char *buf) |
| 613 | { |
| 614 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 615 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
| 616 | return snprintf(buf, PAGE_SIZE, "%lld\n", val); |
| 617 | } |
| 618 | |
| 619 | SPACE_INFO_ATTR(flags); |
| 620 | SPACE_INFO_ATTR(total_bytes); |
| 621 | SPACE_INFO_ATTR(bytes_used); |
| 622 | SPACE_INFO_ATTR(bytes_pinned); |
| 623 | SPACE_INFO_ATTR(bytes_reserved); |
| 624 | SPACE_INFO_ATTR(bytes_may_use); |
Wang Xiaoguang | c1fd5c3 | 2016-06-21 11:12:56 +0800 | [diff] [blame] | 625 | SPACE_INFO_ATTR(bytes_readonly); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 626 | SPACE_INFO_ATTR(disk_used); |
| 627 | SPACE_INFO_ATTR(disk_total); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 628 | BTRFS_ATTR(space_info, total_bytes_pinned, |
| 629 | btrfs_space_info_show_total_bytes_pinned); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 630 | |
| 631 | static struct attribute *space_info_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 632 | BTRFS_ATTR_PTR(space_info, flags), |
| 633 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 634 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 635 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 636 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 637 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 638 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 639 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 640 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 641 | BTRFS_ATTR_PTR(space_info, total_bytes_pinned), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 642 | NULL, |
| 643 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 644 | ATTRIBUTE_GROUPS(space_info); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 645 | |
| 646 | static void space_info_release(struct kobject *kobj) |
| 647 | { |
| 648 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 649 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 650 | kfree(sinfo); |
| 651 | } |
| 652 | |
David Sterba | 27992d0 | 2019-08-01 17:55:55 +0200 | [diff] [blame] | 653 | static struct kobj_type space_info_ktype = { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 654 | .sysfs_ops = &kobj_sysfs_ops, |
| 655 | .release = space_info_release, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 656 | .default_groups = space_info_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 657 | }; |
| 658 | |
| 659 | static const struct attribute *allocation_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 660 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 661 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 662 | NULL, |
| 663 | }; |
| 664 | |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 665 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 666 | struct kobj_attribute *a, char *buf) |
| 667 | { |
| 668 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 669 | char *label = fs_info->super_copy->label; |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 670 | ssize_t ret; |
| 671 | |
| 672 | spin_lock(&fs_info->super_lock); |
| 673 | ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); |
| 674 | spin_unlock(&fs_info->super_lock); |
| 675 | |
| 676 | return ret; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 680 | struct kobj_attribute *a, |
| 681 | const char *buf, size_t len) |
| 682 | { |
| 683 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 684 | size_t p_len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 685 | |
David Sterba | 66ac9fe | 2016-04-26 16:03:57 +0200 | [diff] [blame] | 686 | if (!fs_info) |
| 687 | return -EPERM; |
| 688 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 689 | if (sb_rdonly(fs_info->sb)) |
Anand Jain | 79aec2b | 2014-07-30 20:04:10 +0800 | [diff] [blame] | 690 | return -EROFS; |
| 691 | |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 692 | /* |
| 693 | * p_len is the len until the first occurrence of either |
| 694 | * '\n' or '\0' |
| 695 | */ |
| 696 | p_len = strcspn(buf, "\n"); |
| 697 | |
| 698 | if (p_len >= BTRFS_LABEL_SIZE) |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 699 | return -EINVAL; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 700 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 701 | spin_lock(&fs_info->super_lock); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 702 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 703 | memcpy(fs_info->super_copy->label, buf, p_len); |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 704 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 705 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 706 | /* |
| 707 | * We don't want to do full transaction commit from inside sysfs |
| 708 | */ |
| 709 | btrfs_set_pending(fs_info, COMMIT); |
| 710 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 711 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 712 | return len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 713 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 714 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 715 | |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 716 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 717 | struct kobj_attribute *a, char *buf) |
| 718 | { |
| 719 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 720 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 721 | return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 724 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 725 | |
| 726 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 727 | struct kobj_attribute *a, char *buf) |
| 728 | { |
| 729 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 730 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 731 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 732 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 735 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 736 | |
| 737 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 738 | struct kobj_attribute *a, char *buf) |
| 739 | { |
| 740 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 741 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 742 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 743 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 746 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 747 | |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 748 | static ssize_t quota_override_show(struct kobject *kobj, |
| 749 | struct kobj_attribute *a, char *buf) |
| 750 | { |
| 751 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 752 | int quota_override; |
| 753 | |
| 754 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 755 | return snprintf(buf, PAGE_SIZE, "%d\n", quota_override); |
| 756 | } |
| 757 | |
| 758 | static ssize_t quota_override_store(struct kobject *kobj, |
| 759 | struct kobj_attribute *a, |
| 760 | const char *buf, size_t len) |
| 761 | { |
| 762 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 763 | unsigned long knob; |
| 764 | int err; |
| 765 | |
| 766 | if (!fs_info) |
| 767 | return -EPERM; |
| 768 | |
| 769 | if (!capable(CAP_SYS_RESOURCE)) |
| 770 | return -EPERM; |
| 771 | |
| 772 | err = kstrtoul(buf, 10, &knob); |
| 773 | if (err) |
| 774 | return err; |
| 775 | if (knob > 1) |
| 776 | return -EINVAL; |
| 777 | |
| 778 | if (knob) |
| 779 | set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 780 | else |
| 781 | clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 782 | |
| 783 | return len; |
| 784 | } |
| 785 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 786 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 787 | |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 788 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 789 | struct kobj_attribute *a, char *buf) |
| 790 | { |
| 791 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 792 | |
| 793 | return snprintf(buf, PAGE_SIZE, "%pU\n", |
| 794 | fs_info->fs_devices->metadata_uuid); |
| 795 | } |
| 796 | |
| 797 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 798 | |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 799 | static ssize_t btrfs_checksum_show(struct kobject *kobj, |
| 800 | struct kobj_attribute *a, char *buf) |
| 801 | { |
| 802 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 803 | u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); |
| 804 | |
| 805 | return snprintf(buf, PAGE_SIZE, "%s (%s)\n", |
| 806 | btrfs_super_csum_name(csum_type), |
| 807 | crypto_shash_driver_name(fs_info->csum_shash)); |
| 808 | } |
| 809 | |
| 810 | BTRFS_ATTR(, checksum, btrfs_checksum_show); |
| 811 | |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 812 | static const struct attribute *btrfs_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 813 | BTRFS_ATTR_PTR(, label), |
| 814 | BTRFS_ATTR_PTR(, nodesize), |
| 815 | BTRFS_ATTR_PTR(, sectorsize), |
| 816 | BTRFS_ATTR_PTR(, clone_alignment), |
| 817 | BTRFS_ATTR_PTR(, quota_override), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 818 | BTRFS_ATTR_PTR(, metadata_uuid), |
Johannes Thumshirn | 41e6d2a | 2019-10-07 11:11:04 +0200 | [diff] [blame] | 819 | BTRFS_ATTR_PTR(, checksum), |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 820 | NULL, |
| 821 | }; |
| 822 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 823 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 824 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 825 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
Anand Jain | 248d200 | 2015-03-10 06:38:19 +0800 | [diff] [blame] | 826 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 827 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 828 | complete(&fs_devs->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | static struct kobj_type btrfs_ktype = { |
| 832 | .sysfs_ops = &kobj_sysfs_ops, |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 833 | .release = btrfs_release_fsid_kobj, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 834 | }; |
| 835 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 836 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 837 | { |
| 838 | if (kobj->ktype != &btrfs_ktype) |
| 839 | return NULL; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 840 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 841 | } |
| 842 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 843 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 844 | { |
| 845 | if (kobj->ktype != &btrfs_ktype) |
| 846 | return NULL; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 847 | return to_fs_devs(kobj)->fs_info; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 848 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 849 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 850 | #define NUM_FEATURE_BITS 64 |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 851 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 852 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 853 | 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] | 854 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 855 | static const u64 supported_feature_masks[FEAT_MAX] = { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 856 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 857 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 858 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 859 | }; |
| 860 | |
| 861 | 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] | 862 | { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 863 | int set; |
| 864 | |
| 865 | for (set = 0; set < FEAT_MAX; set++) { |
| 866 | int i; |
| 867 | struct attribute *attrs[2]; |
| 868 | struct attribute_group agroup = { |
| 869 | .name = "features", |
| 870 | .attrs = attrs, |
| 871 | }; |
| 872 | u64 features = get_features(fs_info, set); |
| 873 | features &= ~supported_feature_masks[set]; |
| 874 | |
| 875 | if (!features) |
| 876 | continue; |
| 877 | |
| 878 | attrs[1] = NULL; |
| 879 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 880 | struct btrfs_feature_attr *fa; |
| 881 | |
| 882 | if (!(features & (1ULL << i))) |
| 883 | continue; |
| 884 | |
| 885 | fa = &btrfs_feature_attrs[set][i]; |
| 886 | attrs[0] = &fa->kobj_attr.attr; |
| 887 | if (add) { |
| 888 | int ret; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 889 | ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 890 | &agroup); |
| 891 | if (ret) |
| 892 | return ret; |
| 893 | } else |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 894 | sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 895 | &agroup); |
| 896 | } |
| 897 | |
| 898 | } |
| 899 | return 0; |
| 900 | } |
| 901 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 902 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 903 | { |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 904 | if (fs_devs->devices_kobj) { |
| 905 | kobject_del(fs_devs->devices_kobj); |
| 906 | kobject_put(fs_devs->devices_kobj); |
| 907 | fs_devs->devices_kobj = NULL; |
Anand Jain | aaf1330 | 2015-03-10 06:38:24 +0800 | [diff] [blame] | 908 | } |
| 909 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 910 | if (fs_devs->fsid_kobj.state_initialized) { |
| 911 | kobject_del(&fs_devs->fsid_kobj); |
| 912 | kobject_put(&fs_devs->fsid_kobj); |
Anand Jain | f90fc54 | 2015-06-22 18:18:32 +0800 | [diff] [blame] | 913 | wait_for_completion(&fs_devs->kobj_unregister); |
| 914 | } |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 915 | } |
| 916 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 917 | /* when fs_devs is NULL it will remove all fsid kobject */ |
Anand Jain | 1d1c1be | 2015-03-10 06:38:37 +0800 | [diff] [blame] | 918 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 919 | { |
| 920 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 921 | |
| 922 | if (fs_devs) { |
| 923 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 924 | return; |
| 925 | } |
| 926 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 927 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 928 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 929 | } |
| 930 | } |
| 931 | |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 932 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 933 | { |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 934 | btrfs_reset_fs_info_ptr(fs_info); |
| 935 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 936 | if (fs_info->space_info_kobj) { |
| 937 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
| 938 | kobject_del(fs_info->space_info_kobj); |
| 939 | kobject_put(fs_info->space_info_kobj); |
| 940 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 941 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 942 | if (fs_info->discard_debug_kobj) { |
| 943 | sysfs_remove_files(fs_info->discard_debug_kobj, |
| 944 | discard_debug_attrs); |
| 945 | kobject_del(fs_info->discard_debug_kobj); |
| 946 | kobject_put(fs_info->discard_debug_kobj); |
| 947 | } |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 948 | if (fs_info->debug_kobj) { |
| 949 | sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
| 950 | kobject_del(fs_info->debug_kobj); |
| 951 | kobject_put(fs_info->debug_kobj); |
| 952 | } |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 953 | #endif |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 954 | addrm_unknown_feature_attrs(fs_info, false); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 955 | sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group); |
| 956 | sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs); |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 957 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 958 | } |
| 959 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 960 | static const char * const btrfs_feature_set_names[FEAT_MAX] = { |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 961 | [FEAT_COMPAT] = "compat", |
| 962 | [FEAT_COMPAT_RO] = "compat_ro", |
| 963 | [FEAT_INCOMPAT] = "incompat", |
| 964 | }; |
| 965 | |
David Sterba | f10152b | 2019-08-01 19:07:55 +0200 | [diff] [blame] | 966 | const char * const btrfs_feature_set_name(enum btrfs_feature_set set) |
| 967 | { |
| 968 | return btrfs_feature_set_names[set]; |
| 969 | } |
| 970 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 971 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 972 | { |
| 973 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 974 | int len = 0; |
| 975 | int i; |
| 976 | char *str; |
| 977 | |
| 978 | str = kmalloc(bufsize, GFP_KERNEL); |
| 979 | if (!str) |
| 980 | return str; |
| 981 | |
| 982 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 983 | const char *name; |
| 984 | |
| 985 | if (!(flags & (1ULL << i))) |
| 986 | continue; |
| 987 | |
| 988 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 989 | len += snprintf(str + len, bufsize - len, "%s%s", |
| 990 | len ? "," : "", name); |
| 991 | } |
| 992 | |
| 993 | return str; |
| 994 | } |
| 995 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 996 | static void init_feature_attrs(void) |
| 997 | { |
| 998 | struct btrfs_feature_attr *fa; |
| 999 | int set, i; |
| 1000 | |
| 1001 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 1002 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 1003 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 1004 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 1005 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1006 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 1007 | memset(btrfs_unknown_feature_names, 0, |
| 1008 | sizeof(btrfs_unknown_feature_names)); |
| 1009 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1010 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 1011 | struct btrfs_feature_attr *sfa; |
| 1012 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1013 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1014 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 1015 | bit = ilog2(sfa->feature_bit); |
| 1016 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1017 | |
| 1018 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 1019 | } |
| 1020 | |
| 1021 | for (set = 0; set < FEAT_MAX; set++) { |
| 1022 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1023 | char *name = btrfs_unknown_feature_names[set][i]; |
| 1024 | fa = &btrfs_feature_attrs[set][i]; |
| 1025 | |
| 1026 | if (fa->kobj_attr.attr.name) |
| 1027 | continue; |
| 1028 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 1029 | snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1030 | btrfs_feature_set_names[set], i); |
| 1031 | |
| 1032 | fa->kobj_attr.attr.name = name; |
| 1033 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 1034 | fa->feature_set = set; |
| 1035 | fa->feature_bit = 1ULL << i; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1040 | /* |
| 1041 | * Create a sysfs entry for a given block group type at path |
| 1042 | * /sys/fs/btrfs/UUID/allocation/data/TYPE |
| 1043 | */ |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 1044 | void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) |
David Sterba | 32a9991 | 2019-08-01 17:49:55 +0200 | [diff] [blame] | 1045 | { |
| 1046 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 1047 | struct btrfs_space_info *space_info = cache->space_info; |
| 1048 | struct raid_kobject *rkobj; |
| 1049 | const int index = btrfs_bg_flags_to_raid_index(cache->flags); |
| 1050 | unsigned int nofs_flag; |
| 1051 | int ret; |
| 1052 | |
| 1053 | /* |
| 1054 | * Setup a NOFS context because kobject_add(), deep in its call chain, |
| 1055 | * does GFP_KERNEL allocations, and we are often called in a context |
| 1056 | * where if reclaim is triggered we can deadlock (we are either holding |
| 1057 | * a transaction handle or some lock required for a transaction |
| 1058 | * commit). |
| 1059 | */ |
| 1060 | nofs_flag = memalloc_nofs_save(); |
| 1061 | |
| 1062 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 1063 | if (!rkobj) { |
| 1064 | memalloc_nofs_restore(nofs_flag); |
| 1065 | btrfs_warn(cache->fs_info, |
| 1066 | "couldn't alloc memory for raid level kobject"); |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | rkobj->flags = cache->flags; |
| 1071 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); |
| 1072 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", |
| 1073 | btrfs_bg_type_to_raid_name(rkobj->flags)); |
| 1074 | memalloc_nofs_restore(nofs_flag); |
| 1075 | if (ret) { |
| 1076 | kobject_put(&rkobj->kobj); |
| 1077 | btrfs_warn(fs_info, |
| 1078 | "failed to add kobject for block cache, ignoring"); |
| 1079 | return; |
| 1080 | } |
| 1081 | |
| 1082 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
| 1083 | } |
| 1084 | |
David Sterba | b5865ba | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1085 | /* |
| 1086 | * Remove sysfs directories for all block group types of a given space info and |
| 1087 | * the space info as well |
| 1088 | */ |
| 1089 | void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) |
| 1090 | { |
| 1091 | int i; |
| 1092 | |
| 1093 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 1094 | struct kobject *kobj; |
| 1095 | |
| 1096 | kobj = space_info->block_group_kobjs[i]; |
| 1097 | space_info->block_group_kobjs[i] = NULL; |
| 1098 | if (kobj) { |
| 1099 | kobject_del(kobj); |
| 1100 | kobject_put(kobj); |
| 1101 | } |
| 1102 | } |
| 1103 | kobject_del(&space_info->kobj); |
| 1104 | kobject_put(&space_info->kobj); |
| 1105 | } |
| 1106 | |
David Sterba | b882327 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1107 | static const char *alloc_name(u64 flags) |
| 1108 | { |
| 1109 | switch (flags) { |
| 1110 | case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 1111 | return "mixed"; |
| 1112 | case BTRFS_BLOCK_GROUP_METADATA: |
| 1113 | return "metadata"; |
| 1114 | case BTRFS_BLOCK_GROUP_DATA: |
| 1115 | return "data"; |
| 1116 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 1117 | return "system"; |
| 1118 | default: |
| 1119 | WARN_ON(1); |
| 1120 | return "invalid-combination"; |
| 1121 | }; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Create a sysfs entry for a space info type at path |
| 1126 | * /sys/fs/btrfs/UUID/allocation/TYPE |
| 1127 | */ |
| 1128 | int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, |
| 1129 | struct btrfs_space_info *space_info) |
| 1130 | { |
| 1131 | int ret; |
| 1132 | |
| 1133 | ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, |
| 1134 | fs_info->space_info_kobj, "%s", |
| 1135 | alloc_name(space_info->flags)); |
| 1136 | if (ret) { |
| 1137 | kobject_put(&space_info->kobj); |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1144 | /* when one_device is NULL, it removes all device links */ |
| 1145 | |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1146 | int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1147 | struct btrfs_device *one_device) |
| 1148 | { |
| 1149 | struct hd_struct *disk; |
| 1150 | struct kobject *disk_kobj; |
| 1151 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1152 | if (!fs_devices->devices_kobj) |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1153 | return -EINVAL; |
| 1154 | |
Liu Bo | 87fa3bb | 2014-07-29 19:09:39 +0800 | [diff] [blame] | 1155 | if (one_device && one_device->bdev) { |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1156 | disk = one_device->bdev->bd_part; |
| 1157 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1158 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1159 | sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name); |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1160 | } |
| 1161 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1162 | if (one_device) |
| 1163 | return 0; |
| 1164 | |
| 1165 | list_for_each_entry(one_device, |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 1166 | &fs_devices->devices, dev_list) { |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1167 | if (!one_device->bdev) |
| 1168 | continue; |
| 1169 | disk = one_device->bdev->bd_part; |
| 1170 | disk_kobj = &part_to_dev(disk)->kobj; |
| 1171 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1172 | sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name); |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 1178 | int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 1179 | struct btrfs_device *one_device) |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1180 | { |
| 1181 | int error = 0; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1182 | struct btrfs_device *dev; |
| 1183 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1184 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1185 | struct hd_struct *disk; |
| 1186 | struct kobject *disk_kobj; |
| 1187 | |
| 1188 | if (!dev->bdev) |
| 1189 | continue; |
| 1190 | |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 1191 | if (one_device && one_device != dev) |
| 1192 | continue; |
| 1193 | |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 1194 | disk = dev->bdev->bd_part; |
| 1195 | disk_kobj = &part_to_dev(disk)->kobj; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1196 | |
Anand Jain | b550150 | 2019-11-21 17:33:30 +0800 | [diff] [blame] | 1197 | error = sysfs_create_link(fs_devices->devices_kobj, |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 1198 | disk_kobj, disk_kobj->name); |
| 1199 | if (error) |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | return error; |
| 1204 | } |
| 1205 | |
David Sterba | 5b28692 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1206 | void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) |
| 1207 | { |
| 1208 | int ret; |
| 1209 | |
| 1210 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 1211 | if (ret) |
| 1212 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
| 1213 | action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 1214 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 1215 | } |
| 1216 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 1217 | void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices, |
| 1218 | const u8 *fsid) |
| 1219 | { |
| 1220 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 1221 | |
| 1222 | /* |
| 1223 | * Sprouting changes fsid of the mounted filesystem, rename the fsid |
| 1224 | * directory |
| 1225 | */ |
| 1226 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid); |
| 1227 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
| 1228 | btrfs_warn(fs_devices->fs_info, |
| 1229 | "sysfs: failed to create fsid for sprout"); |
| 1230 | } |
| 1231 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1232 | /* /sys/fs/btrfs/ entry */ |
| 1233 | static struct kset *btrfs_kset; |
| 1234 | |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1235 | /* |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1236 | * Creates: |
| 1237 | * /sys/fs/btrfs/UUID |
| 1238 | * |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1239 | * Can be called by the device discovery thread. |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1240 | */ |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1241 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1242 | { |
| 1243 | int error; |
| 1244 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1245 | init_completion(&fs_devs->kobj_unregister); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1246 | fs_devs->fsid_kobj.kset = btrfs_kset; |
Anand Jain | c6761a9 | 2019-11-21 17:33:32 +0800 | [diff] [blame] | 1247 | error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, |
| 1248 | "%pU", fs_devs->fsid); |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1249 | if (error) { |
| 1250 | kobject_put(&fs_devs->fsid_kobj); |
| 1251 | return error; |
| 1252 | } |
| 1253 | |
Anand Jain | bc036bb | 2019-11-21 17:33:34 +0800 | [diff] [blame] | 1254 | fs_devs->devices_kobj = kobject_create_and_add("devices", |
| 1255 | &fs_devs->fsid_kobj); |
| 1256 | if (!fs_devs->devices_kobj) { |
| 1257 | btrfs_err(fs_devs->fs_info, |
| 1258 | "failed to init sysfs device interface"); |
| 1259 | kobject_put(&fs_devs->fsid_kobj); |
| 1260 | return -ENOMEM; |
| 1261 | } |
| 1262 | |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 1263 | return 0; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Anand Jain | 96f3136 | 2015-08-14 18:32:46 +0800 | [diff] [blame] | 1266 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1267 | { |
| 1268 | int error; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 1269 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1270 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 1271 | |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 1272 | btrfs_set_fs_info_ptr(fs_info); |
| 1273 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 1274 | error = btrfs_sysfs_add_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1275 | if (error) |
| 1276 | return error; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 1277 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1278 | error = sysfs_create_files(fsid_kobj, btrfs_attrs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1279 | if (error) { |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1280 | btrfs_sysfs_rm_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1281 | return error; |
| 1282 | } |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1283 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1284 | error = sysfs_create_group(fsid_kobj, |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 1285 | &btrfs_feature_attr_group); |
| 1286 | if (error) |
| 1287 | goto failure; |
| 1288 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1289 | #ifdef CONFIG_BTRFS_DEBUG |
Dennis Zhou | 93945cb | 2019-12-13 16:22:18 -0800 | [diff] [blame] | 1290 | fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); |
| 1291 | if (!fs_info->debug_kobj) { |
| 1292 | error = -ENOMEM; |
| 1293 | goto failure; |
| 1294 | } |
| 1295 | |
| 1296 | error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1297 | if (error) |
| 1298 | goto failure; |
Dennis Zhou | e4faab8 | 2019-12-13 16:22:19 -0800 | [diff] [blame] | 1299 | |
| 1300 | /* Discard directory */ |
| 1301 | fs_info->discard_debug_kobj = kobject_create_and_add("discard", |
| 1302 | fs_info->debug_kobj); |
| 1303 | if (!fs_info->discard_debug_kobj) { |
| 1304 | error = -ENOMEM; |
| 1305 | goto failure; |
| 1306 | } |
| 1307 | |
| 1308 | error = sysfs_create_files(fs_info->discard_debug_kobj, |
| 1309 | discard_debug_attrs); |
| 1310 | if (error) |
| 1311 | goto failure; |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1312 | #endif |
| 1313 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 1314 | error = addrm_unknown_feature_attrs(fs_info, true); |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1315 | if (error) |
| 1316 | goto failure; |
| 1317 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1318 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 1319 | fsid_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 1320 | if (!fs_info->space_info_kobj) { |
| 1321 | error = -ENOMEM; |
| 1322 | goto failure; |
| 1323 | } |
| 1324 | |
| 1325 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 1326 | if (error) |
| 1327 | goto failure; |
| 1328 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 1329 | return 0; |
| 1330 | failure: |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 1331 | btrfs_sysfs_remove_mounted(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 1332 | return error; |
| 1333 | } |
| 1334 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1335 | |
| 1336 | /* |
| 1337 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 1338 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 1339 | */ |
| 1340 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, |
| 1341 | u64 bit, enum btrfs_feature_set set) |
| 1342 | { |
| 1343 | struct btrfs_fs_devices *fs_devs; |
| 1344 | struct kobject *fsid_kobj; |
| 1345 | u64 features; |
| 1346 | int ret; |
| 1347 | |
| 1348 | if (!fs_info) |
| 1349 | return; |
| 1350 | |
| 1351 | features = get_features(fs_info, set); |
| 1352 | ASSERT(bit & supported_feature_masks[set]); |
| 1353 | |
| 1354 | fs_devs = fs_info->fs_devices; |
| 1355 | fsid_kobj = &fs_devs->fsid_kobj; |
| 1356 | |
David Sterba | bf60920 | 2016-01-27 14:06:29 +0100 | [diff] [blame] | 1357 | if (!fsid_kobj->state_initialized) |
| 1358 | return; |
| 1359 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 1360 | /* |
| 1361 | * FIXME: this is too heavy to update just one value, ideally we'd like |
| 1362 | * to use sysfs_update_group but some refactoring is needed first. |
| 1363 | */ |
| 1364 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1365 | ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); |
| 1366 | } |
| 1367 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 1368 | int __init btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1369 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1370 | int ret; |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1371 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1372 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 1373 | if (!btrfs_kset) |
| 1374 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1375 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1376 | init_feature_attrs(); |
| 1377 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1378 | if (ret) |
| 1379 | goto out2; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1380 | ret = sysfs_merge_group(&btrfs_kset->kobj, |
| 1381 | &btrfs_static_feature_attr_group); |
| 1382 | if (ret) |
| 1383 | goto out_remove_group; |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1384 | |
David Sterba | 6e369fe | 2019-06-13 17:23:02 +0200 | [diff] [blame] | 1385 | #ifdef CONFIG_BTRFS_DEBUG |
| 1386 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1387 | if (ret) |
| 1388 | goto out2; |
| 1389 | #endif |
| 1390 | |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1391 | return 0; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1392 | |
| 1393 | out_remove_group: |
| 1394 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1395 | out2: |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 1396 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 1397 | |
| 1398 | return ret; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1399 | } |
| 1400 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1401 | void __cold btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1402 | { |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 1403 | sysfs_unmerge_group(&btrfs_kset->kobj, |
| 1404 | &btrfs_static_feature_attr_group); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 1405 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Dennis Zhou | 71e8978 | 2019-12-13 16:22:17 -0800 | [diff] [blame] | 1406 | #ifdef CONFIG_BTRFS_DEBUG |
| 1407 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); |
| 1408 | #endif |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 1409 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1410 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 1411 | |