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> |
| 7 | #include <linux/slab.h> |
| 8 | #include <linux/spinlock.h> |
| 9 | #include <linux/completion.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 10 | #include <linux/kobject.h> |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 11 | #include <linux/bug.h> |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 12 | #include <linux/debugfs.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" |
| 15 | #include "disk-io.h" |
| 16 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 17 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 18 | #include "volumes.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 19 | #include "space-info.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 20 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 21 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 22 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 23 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 24 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 25 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 26 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 27 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 28 | if (set == FEAT_COMPAT) |
| 29 | return btrfs_super_compat_flags(disk_super); |
| 30 | else if (set == FEAT_COMPAT_RO) |
| 31 | return btrfs_super_compat_ro_flags(disk_super); |
| 32 | else |
| 33 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 36 | static void set_features(struct btrfs_fs_info *fs_info, |
| 37 | enum btrfs_feature_set set, u64 features) |
| 38 | { |
| 39 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 40 | if (set == FEAT_COMPAT) |
| 41 | btrfs_set_super_compat_flags(disk_super, features); |
| 42 | else if (set == FEAT_COMPAT_RO) |
| 43 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 44 | else |
| 45 | btrfs_set_super_incompat_flags(disk_super, features); |
| 46 | } |
| 47 | |
| 48 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 49 | { |
| 50 | int val = 0; |
| 51 | u64 set, clear; |
| 52 | switch (fa->feature_set) { |
| 53 | case FEAT_COMPAT: |
| 54 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 55 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 56 | break; |
| 57 | case FEAT_COMPAT_RO: |
| 58 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 59 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 60 | break; |
| 61 | case FEAT_INCOMPAT: |
| 62 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 63 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 64 | break; |
| 65 | default: |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 66 | pr_warn("btrfs: sysfs: unknown feature set %d\n", |
David Sterba | cc37bb0 | 2013-11-19 13:36:21 +0100 | [diff] [blame] | 67 | fa->feature_set); |
| 68 | return 0; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (set & fa->feature_bit) |
| 72 | val |= 1; |
| 73 | if (clear & fa->feature_bit) |
| 74 | val |= 2; |
| 75 | |
| 76 | return val; |
| 77 | } |
| 78 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 79 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 80 | struct kobj_attribute *a, char *buf) |
| 81 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 82 | int val = 0; |
| 83 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 84 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 85 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 86 | u64 features = get_features(fs_info, fa->feature_set); |
| 87 | if (features & fa->feature_bit) |
| 88 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 89 | } else |
| 90 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 91 | |
| 92 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 93 | } |
| 94 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 95 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 96 | struct kobj_attribute *a, |
| 97 | const char *buf, size_t count) |
| 98 | { |
| 99 | struct btrfs_fs_info *fs_info; |
| 100 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 101 | u64 features, set, clear; |
| 102 | unsigned long val; |
| 103 | int ret; |
| 104 | |
| 105 | fs_info = to_fs_info(kobj); |
| 106 | if (!fs_info) |
| 107 | return -EPERM; |
| 108 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 109 | if (sb_rdonly(fs_info->sb)) |
David Sterba | ee61113 | 2015-01-23 18:43:31 +0100 | [diff] [blame] | 110 | return -EROFS; |
| 111 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 112 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | if (fa->feature_set == FEAT_COMPAT) { |
| 117 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 118 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 119 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 120 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 121 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 122 | } else { |
| 123 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 124 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 125 | } |
| 126 | |
| 127 | features = get_features(fs_info, fa->feature_set); |
| 128 | |
| 129 | /* Nothing to do */ |
| 130 | if ((val && (features & fa->feature_bit)) || |
| 131 | (!val && !(features & fa->feature_bit))) |
| 132 | return count; |
| 133 | |
| 134 | if ((val && !(set & fa->feature_bit)) || |
| 135 | (!val && !(clear & fa->feature_bit))) { |
| 136 | btrfs_info(fs_info, |
| 137 | "%sabling feature %s on mounted fs is not supported.", |
| 138 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 139 | return -EPERM; |
| 140 | } |
| 141 | |
| 142 | btrfs_info(fs_info, "%s %s feature flag", |
| 143 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 144 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 145 | spin_lock(&fs_info->super_lock); |
| 146 | features = get_features(fs_info, fa->feature_set); |
| 147 | if (val) |
| 148 | features |= fa->feature_bit; |
| 149 | else |
| 150 | features &= ~fa->feature_bit; |
| 151 | set_features(fs_info, fa->feature_set, features); |
| 152 | spin_unlock(&fs_info->super_lock); |
| 153 | |
David Sterba | 0eae274 | 2014-11-12 14:22:21 +0100 | [diff] [blame] | 154 | /* |
| 155 | * We don't want to do full transaction commit from inside sysfs |
| 156 | */ |
| 157 | btrfs_set_pending(fs_info, COMMIT); |
| 158 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 159 | |
| 160 | return count; |
| 161 | } |
| 162 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 163 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 164 | struct attribute *attr, int unused) |
| 165 | { |
| 166 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 167 | umode_t mode = attr->mode; |
| 168 | |
| 169 | if (fs_info) { |
| 170 | struct btrfs_feature_attr *fa; |
| 171 | u64 features; |
| 172 | |
| 173 | fa = attr_to_btrfs_feature_attr(attr); |
| 174 | features = get_features(fs_info, fa->feature_set); |
| 175 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 176 | if (can_modify_feature(fa)) |
| 177 | mode |= S_IWUSR; |
| 178 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 179 | mode = 0; |
| 180 | } |
| 181 | |
| 182 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 186 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 187 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 188 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 189 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 190 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 191 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 192 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 193 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 194 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 195 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 196 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 197 | |
| 198 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 199 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 200 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 201 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 202 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 203 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 204 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 205 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 206 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 207 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
David Sterba | c736c09 | 2014-01-21 18:56:09 +0100 | [diff] [blame] | 208 | BTRFS_FEAT_ATTR_PTR(no_holes), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 209 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
David Sterba | 3b5bb73 | 2016-01-21 18:36:46 +0100 | [diff] [blame] | 210 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 211 | NULL |
| 212 | }; |
| 213 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 214 | /* |
| 215 | * Features which depend on feature bits and may differ between each fs. |
| 216 | * |
| 217 | * /sys/fs/btrfs/features lists all available features of this kernel while |
| 218 | * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or |
| 219 | * can be changed online. |
| 220 | */ |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 221 | static const struct attribute_group btrfs_feature_attr_group = { |
| 222 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 223 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 224 | .attrs = btrfs_supported_feature_attrs, |
| 225 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 226 | |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 227 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 228 | struct kobj_attribute *ka, char *buf) |
| 229 | { |
| 230 | return snprintf(buf, PAGE_SIZE, "0\n"); |
| 231 | } |
| 232 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 233 | |
| 234 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 235 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
| 236 | NULL |
| 237 | }; |
| 238 | |
| 239 | /* |
| 240 | * Features which only depend on kernel version. |
| 241 | * |
| 242 | * These are listed in /sys/fs/btrfs/features along with |
| 243 | * btrfs_feature_attr_group |
| 244 | */ |
| 245 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 246 | .name = "features", |
| 247 | .attrs = btrfs_supported_static_feature_attrs, |
| 248 | }; |
| 249 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 250 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 251 | { |
| 252 | u64 val; |
| 253 | if (lock) |
| 254 | spin_lock(lock); |
| 255 | val = *value_ptr; |
| 256 | if (lock) |
| 257 | spin_unlock(lock); |
| 258 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 259 | } |
| 260 | |
| 261 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 262 | struct kobj_attribute *ka, char *buf) |
| 263 | { |
| 264 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 265 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 266 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 267 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 268 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 269 | |
| 270 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 271 | struct kobj_attribute *a, char *buf) |
| 272 | { |
| 273 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 274 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 275 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 276 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 277 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 278 | |
| 279 | #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] | 280 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 281 | |
| 282 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 283 | struct kobj_attribute *attr, char *buf); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 284 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 285 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 286 | |
| 287 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 288 | struct kobj_attribute *attr, char *buf) |
| 289 | |
| 290 | { |
| 291 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
| 292 | struct btrfs_block_group_cache *block_group; |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 293 | 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] | 294 | u64 val = 0; |
| 295 | |
| 296 | down_read(&sinfo->groups_sem); |
| 297 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 298 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 299 | val += block_group->key.offset; |
| 300 | else |
| 301 | val += btrfs_block_group_used(&block_group->item); |
| 302 | } |
| 303 | up_read(&sinfo->groups_sem); |
| 304 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 305 | } |
| 306 | |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 307 | static struct attribute *raid_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 308 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 309 | BTRFS_ATTR_PTR(raid, used_bytes), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 310 | NULL |
| 311 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 312 | ATTRIBUTE_GROUPS(raid); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 313 | |
| 314 | static void release_raid_kobj(struct kobject *kobj) |
| 315 | { |
Jeff Mahoney | c189544 | 2014-05-27 12:59:57 -0400 | [diff] [blame] | 316 | kfree(to_raid_kobj(kobj)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | struct kobj_type btrfs_raid_ktype = { |
| 320 | .sysfs_ops = &kobj_sysfs_ops, |
| 321 | .release = release_raid_kobj, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 322 | .default_groups = raid_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | #define SPACE_INFO_ATTR(field) \ |
| 326 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 327 | struct kobj_attribute *a, \ |
| 328 | char *buf) \ |
| 329 | { \ |
| 330 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 331 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 332 | } \ |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 333 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 334 | |
| 335 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 336 | struct kobj_attribute *a, |
| 337 | char *buf) |
| 338 | { |
| 339 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 340 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
| 341 | return snprintf(buf, PAGE_SIZE, "%lld\n", val); |
| 342 | } |
| 343 | |
| 344 | SPACE_INFO_ATTR(flags); |
| 345 | SPACE_INFO_ATTR(total_bytes); |
| 346 | SPACE_INFO_ATTR(bytes_used); |
| 347 | SPACE_INFO_ATTR(bytes_pinned); |
| 348 | SPACE_INFO_ATTR(bytes_reserved); |
| 349 | SPACE_INFO_ATTR(bytes_may_use); |
Wang Xiaoguang | c1fd5c3 | 2016-06-21 11:12:56 +0800 | [diff] [blame] | 350 | SPACE_INFO_ATTR(bytes_readonly); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 351 | SPACE_INFO_ATTR(disk_used); |
| 352 | SPACE_INFO_ATTR(disk_total); |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 353 | BTRFS_ATTR(space_info, total_bytes_pinned, |
| 354 | btrfs_space_info_show_total_bytes_pinned); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 355 | |
| 356 | static struct attribute *space_info_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 357 | BTRFS_ATTR_PTR(space_info, flags), |
| 358 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 359 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 360 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 361 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 362 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 363 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 364 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 365 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 366 | BTRFS_ATTR_PTR(space_info, total_bytes_pinned), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 367 | NULL, |
| 368 | }; |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 369 | ATTRIBUTE_GROUPS(space_info); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 370 | |
| 371 | static void space_info_release(struct kobject *kobj) |
| 372 | { |
| 373 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 374 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 375 | kfree(sinfo); |
| 376 | } |
| 377 | |
| 378 | struct kobj_type space_info_ktype = { |
| 379 | .sysfs_ops = &kobj_sysfs_ops, |
| 380 | .release = space_info_release, |
Kimberly Brown | 7c7e301 | 2019-05-02 13:34:45 -0400 | [diff] [blame] | 381 | .default_groups = space_info_groups, |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | static const struct attribute *allocation_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 385 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 386 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 387 | NULL, |
| 388 | }; |
| 389 | |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 390 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 391 | struct kobj_attribute *a, char *buf) |
| 392 | { |
| 393 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 394 | char *label = fs_info->super_copy->label; |
David Sterba | ee17fc8 | 2016-04-26 16:22:06 +0200 | [diff] [blame] | 395 | ssize_t ret; |
| 396 | |
| 397 | spin_lock(&fs_info->super_lock); |
| 398 | ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); |
| 399 | spin_unlock(&fs_info->super_lock); |
| 400 | |
| 401 | return ret; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 405 | struct kobj_attribute *a, |
| 406 | const char *buf, size_t len) |
| 407 | { |
| 408 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 409 | size_t p_len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 410 | |
David Sterba | 66ac9fe | 2016-04-26 16:03:57 +0200 | [diff] [blame] | 411 | if (!fs_info) |
| 412 | return -EPERM; |
| 413 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 414 | if (sb_rdonly(fs_info->sb)) |
Anand Jain | 79aec2b | 2014-07-30 20:04:10 +0800 | [diff] [blame] | 415 | return -EROFS; |
| 416 | |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 417 | /* |
| 418 | * p_len is the len until the first occurrence of either |
| 419 | * '\n' or '\0' |
| 420 | */ |
| 421 | p_len = strcspn(buf, "\n"); |
| 422 | |
| 423 | if (p_len >= BTRFS_LABEL_SIZE) |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 424 | return -EINVAL; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 425 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 426 | spin_lock(&fs_info->super_lock); |
Satoru Takeuchi | 48fcc3f | 2014-07-01 17:00:07 +0900 | [diff] [blame] | 427 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 428 | memcpy(fs_info->super_copy->label, buf, p_len); |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 429 | spin_unlock(&fs_info->super_lock); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 430 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 431 | /* |
| 432 | * We don't want to do full transaction commit from inside sysfs |
| 433 | */ |
| 434 | btrfs_set_pending(fs_info, COMMIT); |
| 435 | wake_up_process(fs_info->transaction_kthread); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 436 | |
David Sterba | a6f69dc | 2014-05-30 19:29:05 +0200 | [diff] [blame] | 437 | return len; |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 438 | } |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 439 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 440 | |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 441 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 442 | struct kobj_attribute *a, char *buf) |
| 443 | { |
| 444 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 445 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 446 | return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 449 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 450 | |
| 451 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 452 | struct kobj_attribute *a, char *buf) |
| 453 | { |
| 454 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 455 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 456 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 457 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 460 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 461 | |
| 462 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 463 | struct kobj_attribute *a, char *buf) |
| 464 | { |
| 465 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 466 | |
David Sterba | 093e037 | 2018-03-16 14:31:43 +0100 | [diff] [blame] | 467 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 468 | fs_info->super_copy->sectorsize); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 471 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
David Sterba | df93589 | 2014-05-07 18:17:16 +0200 | [diff] [blame] | 472 | |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 473 | static ssize_t quota_override_show(struct kobject *kobj, |
| 474 | struct kobj_attribute *a, char *buf) |
| 475 | { |
| 476 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 477 | int quota_override; |
| 478 | |
| 479 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 480 | return snprintf(buf, PAGE_SIZE, "%d\n", quota_override); |
| 481 | } |
| 482 | |
| 483 | static ssize_t quota_override_store(struct kobject *kobj, |
| 484 | struct kobj_attribute *a, |
| 485 | const char *buf, size_t len) |
| 486 | { |
| 487 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 488 | unsigned long knob; |
| 489 | int err; |
| 490 | |
| 491 | if (!fs_info) |
| 492 | return -EPERM; |
| 493 | |
| 494 | if (!capable(CAP_SYS_RESOURCE)) |
| 495 | return -EPERM; |
| 496 | |
| 497 | err = kstrtoul(buf, 10, &knob); |
| 498 | if (err) |
| 499 | return err; |
| 500 | if (knob > 1) |
| 501 | return -EINVAL; |
| 502 | |
| 503 | if (knob) |
| 504 | set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 505 | else |
| 506 | clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 507 | |
| 508 | return len; |
| 509 | } |
| 510 | |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 511 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
Sargun Dhillon | 2723480 | 2017-05-11 21:18:03 +0000 | [diff] [blame] | 512 | |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 513 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 514 | struct kobj_attribute *a, char *buf) |
| 515 | { |
| 516 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 517 | |
| 518 | return snprintf(buf, PAGE_SIZE, "%pU\n", |
| 519 | fs_info->fs_devices->metadata_uuid); |
| 520 | } |
| 521 | |
| 522 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 523 | |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 524 | static const struct attribute *btrfs_attrs[] = { |
Hans van Kranenburg | a969f4c | 2017-10-08 22:30:58 +0200 | [diff] [blame] | 525 | BTRFS_ATTR_PTR(, label), |
| 526 | BTRFS_ATTR_PTR(, nodesize), |
| 527 | BTRFS_ATTR_PTR(, sectorsize), |
| 528 | BTRFS_ATTR_PTR(, clone_alignment), |
| 529 | BTRFS_ATTR_PTR(, quota_override), |
Nikolay Borisov | 56f20f4 | 2018-11-19 17:37:45 +0200 | [diff] [blame] | 530 | BTRFS_ATTR_PTR(, metadata_uuid), |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame] | 531 | NULL, |
| 532 | }; |
| 533 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 534 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 535 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 536 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
Anand Jain | 248d200 | 2015-03-10 06:38:19 +0800 | [diff] [blame] | 537 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 538 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 539 | complete(&fs_devs->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static struct kobj_type btrfs_ktype = { |
| 543 | .sysfs_ops = &kobj_sysfs_ops, |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 544 | .release = btrfs_release_fsid_kobj, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 545 | }; |
| 546 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 547 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 548 | { |
| 549 | if (kobj->ktype != &btrfs_ktype) |
| 550 | return NULL; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 551 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 552 | } |
| 553 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 554 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 555 | { |
| 556 | if (kobj->ktype != &btrfs_ktype) |
| 557 | return NULL; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 558 | return to_fs_devs(kobj)->fs_info; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 559 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 560 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 561 | #define NUM_FEATURE_BITS 64 |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 562 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 563 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 564 | 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] | 565 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 566 | static const u64 supported_feature_masks[FEAT_MAX] = { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 567 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 568 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 569 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 570 | }; |
| 571 | |
| 572 | 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] | 573 | { |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 574 | int set; |
| 575 | |
| 576 | for (set = 0; set < FEAT_MAX; set++) { |
| 577 | int i; |
| 578 | struct attribute *attrs[2]; |
| 579 | struct attribute_group agroup = { |
| 580 | .name = "features", |
| 581 | .attrs = attrs, |
| 582 | }; |
| 583 | u64 features = get_features(fs_info, set); |
| 584 | features &= ~supported_feature_masks[set]; |
| 585 | |
| 586 | if (!features) |
| 587 | continue; |
| 588 | |
| 589 | attrs[1] = NULL; |
| 590 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 591 | struct btrfs_feature_attr *fa; |
| 592 | |
| 593 | if (!(features & (1ULL << i))) |
| 594 | continue; |
| 595 | |
| 596 | fa = &btrfs_feature_attrs[set][i]; |
| 597 | attrs[0] = &fa->kobj_attr.attr; |
| 598 | if (add) { |
| 599 | int ret; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 600 | ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 601 | &agroup); |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | } else |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 605 | sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 606 | &agroup); |
| 607 | } |
| 608 | |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 613 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 614 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 615 | if (fs_devs->device_dir_kobj) { |
| 616 | kobject_del(fs_devs->device_dir_kobj); |
| 617 | kobject_put(fs_devs->device_dir_kobj); |
| 618 | fs_devs->device_dir_kobj = NULL; |
Anand Jain | aaf1330 | 2015-03-10 06:38:24 +0800 | [diff] [blame] | 619 | } |
| 620 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 621 | if (fs_devs->fsid_kobj.state_initialized) { |
| 622 | kobject_del(&fs_devs->fsid_kobj); |
| 623 | kobject_put(&fs_devs->fsid_kobj); |
Anand Jain | f90fc54 | 2015-06-22 18:18:32 +0800 | [diff] [blame] | 624 | wait_for_completion(&fs_devs->kobj_unregister); |
| 625 | } |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 628 | /* when fs_devs is NULL it will remove all fsid kobject */ |
Anand Jain | 1d1c1be | 2015-03-10 06:38:37 +0800 | [diff] [blame] | 629 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 630 | { |
| 631 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 632 | |
| 633 | if (fs_devs) { |
| 634 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 635 | return; |
| 636 | } |
| 637 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 638 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
Anand Jain | 2e3e128 | 2015-03-10 06:38:32 +0800 | [diff] [blame] | 639 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 640 | } |
| 641 | } |
| 642 | |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 643 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 644 | { |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 645 | btrfs_reset_fs_info_ptr(fs_info); |
| 646 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 647 | if (fs_info->space_info_kobj) { |
| 648 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
| 649 | kobject_del(fs_info->space_info_kobj); |
| 650 | kobject_put(fs_info->space_info_kobj); |
| 651 | } |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 652 | addrm_unknown_feature_attrs(fs_info, false); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 653 | sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group); |
| 654 | sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs); |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 655 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 658 | const char * const btrfs_feature_set_names[FEAT_MAX] = { |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 659 | [FEAT_COMPAT] = "compat", |
| 660 | [FEAT_COMPAT_RO] = "compat_ro", |
| 661 | [FEAT_INCOMPAT] = "incompat", |
| 662 | }; |
| 663 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 664 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 665 | { |
| 666 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 667 | int len = 0; |
| 668 | int i; |
| 669 | char *str; |
| 670 | |
| 671 | str = kmalloc(bufsize, GFP_KERNEL); |
| 672 | if (!str) |
| 673 | return str; |
| 674 | |
| 675 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 676 | const char *name; |
| 677 | |
| 678 | if (!(flags & (1ULL << i))) |
| 679 | continue; |
| 680 | |
| 681 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 682 | len += snprintf(str + len, bufsize - len, "%s%s", |
| 683 | len ? "," : "", name); |
| 684 | } |
| 685 | |
| 686 | return str; |
| 687 | } |
| 688 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 689 | static void init_feature_attrs(void) |
| 690 | { |
| 691 | struct btrfs_feature_attr *fa; |
| 692 | int set, i; |
| 693 | |
| 694 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 695 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 696 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 697 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 698 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 699 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 700 | memset(btrfs_unknown_feature_names, 0, |
| 701 | sizeof(btrfs_unknown_feature_names)); |
| 702 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 703 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 704 | struct btrfs_feature_attr *sfa; |
| 705 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 706 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 707 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 708 | bit = ilog2(sfa->feature_bit); |
| 709 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 710 | |
| 711 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 712 | } |
| 713 | |
| 714 | for (set = 0; set < FEAT_MAX; set++) { |
| 715 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 716 | char *name = btrfs_unknown_feature_names[set][i]; |
| 717 | fa = &btrfs_feature_attrs[set][i]; |
| 718 | |
| 719 | if (fa->kobj_attr.attr.name) |
| 720 | continue; |
| 721 | |
Tomohiro Misono | 6c52157 | 2018-05-16 17:09:26 +0900 | [diff] [blame] | 722 | snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 723 | btrfs_feature_set_names[set], i); |
| 724 | |
| 725 | fa->kobj_attr.attr.name = name; |
| 726 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 727 | fa->feature_set = set; |
| 728 | fa->feature_bit = 1ULL << i; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 733 | /* when one_device is NULL, it removes all device links */ |
| 734 | |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 735 | int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 736 | struct btrfs_device *one_device) |
| 737 | { |
| 738 | struct hd_struct *disk; |
| 739 | struct kobject *disk_kobj; |
| 740 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 741 | if (!fs_devices->device_dir_kobj) |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 742 | return -EINVAL; |
| 743 | |
Liu Bo | 87fa3bb | 2014-07-29 19:09:39 +0800 | [diff] [blame] | 744 | if (one_device && one_device->bdev) { |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 745 | disk = one_device->bdev->bd_part; |
| 746 | disk_kobj = &part_to_dev(disk)->kobj; |
| 747 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 748 | sysfs_remove_link(fs_devices->device_dir_kobj, |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 749 | disk_kobj->name); |
| 750 | } |
| 751 | |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 752 | if (one_device) |
| 753 | return 0; |
| 754 | |
| 755 | list_for_each_entry(one_device, |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 756 | &fs_devices->devices, dev_list) { |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 757 | if (!one_device->bdev) |
| 758 | continue; |
| 759 | disk = one_device->bdev->bd_part; |
| 760 | disk_kobj = &part_to_dev(disk)->kobj; |
| 761 | |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 762 | sysfs_remove_link(fs_devices->device_dir_kobj, |
Anand Jain | e7e1aa9 | 2015-03-10 06:38:21 +0800 | [diff] [blame] | 763 | disk_kobj->name); |
| 764 | } |
| 765 | |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 769 | int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs) |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 770 | { |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 771 | if (!fs_devs->device_dir_kobj) |
| 772 | fs_devs->device_dir_kobj = kobject_create_and_add("devices", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 773 | &fs_devs->fsid_kobj); |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 774 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 775 | if (!fs_devs->device_dir_kobj) |
Anand Jain | 00c921c | 2015-03-10 06:38:28 +0800 | [diff] [blame] | 776 | return -ENOMEM; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 781 | int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 782 | struct btrfs_device *one_device) |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 783 | { |
| 784 | int error = 0; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 785 | struct btrfs_device *dev; |
| 786 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 787 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 788 | struct hd_struct *disk; |
| 789 | struct kobject *disk_kobj; |
| 790 | |
| 791 | if (!dev->bdev) |
| 792 | continue; |
| 793 | |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 794 | if (one_device && one_device != dev) |
| 795 | continue; |
| 796 | |
Anand Jain | f085381 | 2014-01-15 17:22:28 +0800 | [diff] [blame] | 797 | disk = dev->bdev->bd_part; |
| 798 | disk_kobj = &part_to_dev(disk)->kobj; |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 799 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 800 | error = sysfs_create_link(fs_devices->device_dir_kobj, |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 801 | disk_kobj, disk_kobj->name); |
| 802 | if (error) |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | return error; |
| 807 | } |
| 808 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 809 | /* /sys/fs/btrfs/ entry */ |
| 810 | static struct kset *btrfs_kset; |
| 811 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 812 | /* /sys/kernel/debug/btrfs */ |
| 813 | static struct dentry *btrfs_debugfs_root_dentry; |
| 814 | |
| 815 | /* Debugging tunables and exported data */ |
| 816 | u64 btrfs_debugfs_test; |
| 817 | |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 818 | /* |
| 819 | * Can be called by the device discovery thread. |
| 820 | * And parent can be specified for seed device |
| 821 | */ |
Anand Jain | 0c10e2d | 2015-03-10 06:38:35 +0800 | [diff] [blame] | 822 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 823 | struct kobject *parent) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 824 | { |
| 825 | int error; |
| 826 | |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 827 | init_completion(&fs_devs->kobj_unregister); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 828 | fs_devs->fsid_kobj.kset = btrfs_kset; |
| 829 | error = kobject_init_and_add(&fs_devs->fsid_kobj, |
Anand Jain | 24bd69c | 2015-03-10 06:38:39 +0800 | [diff] [blame] | 830 | &btrfs_ktype, parent, "%pU", fs_devs->fsid); |
Tobin C. Harding | e327733 | 2019-05-13 13:39:12 +1000 | [diff] [blame] | 831 | if (error) { |
| 832 | kobject_put(&fs_devs->fsid_kobj); |
| 833 | return error; |
| 834 | } |
| 835 | |
| 836 | return 0; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 837 | } |
| 838 | |
Anand Jain | 96f3136 | 2015-08-14 18:32:46 +0800 | [diff] [blame] | 839 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 840 | { |
| 841 | int error; |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 842 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 843 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
Anand Jain | 7205921 | 2015-03-10 06:38:26 +0800 | [diff] [blame] | 844 | |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 845 | btrfs_set_fs_info_ptr(fs_info); |
| 846 | |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 847 | error = btrfs_sysfs_add_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 848 | if (error) |
| 849 | return error; |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 850 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 851 | error = sysfs_create_files(fsid_kobj, btrfs_attrs); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 852 | if (error) { |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 853 | btrfs_sysfs_rm_device_link(fs_devs, NULL); |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 854 | return error; |
| 855 | } |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 856 | |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 857 | error = sysfs_create_group(fsid_kobj, |
Anand Jain | 0dd2906 | 2015-03-10 06:38:27 +0800 | [diff] [blame] | 858 | &btrfs_feature_attr_group); |
| 859 | if (error) |
| 860 | goto failure; |
| 861 | |
Jeff Mahoney | e453d98 | 2013-11-21 10:37:16 -0500 | [diff] [blame] | 862 | error = addrm_unknown_feature_attrs(fs_info, true); |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 863 | if (error) |
| 864 | goto failure; |
| 865 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 866 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 867 | fsid_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 868 | if (!fs_info->space_info_kobj) { |
| 869 | error = -ENOMEM; |
| 870 | goto failure; |
| 871 | } |
| 872 | |
| 873 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 874 | if (error) |
| 875 | goto failure; |
| 876 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 877 | return 0; |
| 878 | failure: |
Anand Jain | 6618a59 | 2015-08-14 18:32:47 +0800 | [diff] [blame] | 879 | btrfs_sysfs_remove_mounted(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 880 | return error; |
| 881 | } |
| 882 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 883 | |
| 884 | /* |
| 885 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 886 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 887 | */ |
| 888 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, |
| 889 | u64 bit, enum btrfs_feature_set set) |
| 890 | { |
| 891 | struct btrfs_fs_devices *fs_devs; |
| 892 | struct kobject *fsid_kobj; |
| 893 | u64 features; |
| 894 | int ret; |
| 895 | |
| 896 | if (!fs_info) |
| 897 | return; |
| 898 | |
| 899 | features = get_features(fs_info, set); |
| 900 | ASSERT(bit & supported_feature_masks[set]); |
| 901 | |
| 902 | fs_devs = fs_info->fs_devices; |
| 903 | fsid_kobj = &fs_devs->fsid_kobj; |
| 904 | |
David Sterba | bf60920 | 2016-01-27 14:06:29 +0100 | [diff] [blame] | 905 | if (!fsid_kobj->state_initialized) |
| 906 | return; |
| 907 | |
David Sterba | 444e751 | 2016-01-21 18:50:40 +0100 | [diff] [blame] | 908 | /* |
| 909 | * FIXME: this is too heavy to update just one value, ideally we'd like |
| 910 | * to use sysfs_update_group but some refactoring is needed first. |
| 911 | */ |
| 912 | sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); |
| 913 | ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); |
| 914 | } |
| 915 | |
Greg Kroah-Hartman | 36b7ee4 | 2019-01-04 14:25:20 +0100 | [diff] [blame] | 916 | static void btrfs_init_debugfs(void) |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 917 | { |
| 918 | #ifdef CONFIG_DEBUG_FS |
| 919 | btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 920 | |
David Sterba | b0de6c4 | 2016-09-01 14:37:53 +0200 | [diff] [blame] | 921 | /* |
| 922 | * Example code, how to export data through debugfs. |
| 923 | * |
| 924 | * file: /sys/kernel/debug/btrfs/test |
| 925 | * contents of: btrfs_debugfs_test |
| 926 | */ |
| 927 | #ifdef CONFIG_BTRFS_DEBUG |
Eric Sandeen | 07f6a48 | 2016-08-31 16:49:29 -0500 | [diff] [blame] | 928 | debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry, |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 929 | &btrfs_debugfs_test); |
| 930 | #endif |
David Sterba | b0de6c4 | 2016-09-01 14:37:53 +0200 | [diff] [blame] | 931 | |
| 932 | #endif |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Liu Bo | f5c29bd | 2017-11-02 17:21:50 -0600 | [diff] [blame] | 935 | int __init btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 936 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 937 | int ret; |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 938 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 939 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 940 | if (!btrfs_kset) |
| 941 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 942 | |
Greg Kroah-Hartman | 36b7ee4 | 2019-01-04 14:25:20 +0100 | [diff] [blame] | 943 | btrfs_init_debugfs(); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 944 | |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 945 | init_feature_attrs(); |
| 946 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 947 | if (ret) |
| 948 | goto out2; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 949 | ret = sysfs_merge_group(&btrfs_kset->kobj, |
| 950 | &btrfs_static_feature_attr_group); |
| 951 | if (ret) |
| 952 | goto out_remove_group; |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 953 | |
| 954 | return 0; |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 955 | |
| 956 | out_remove_group: |
| 957 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 958 | out2: |
| 959 | debugfs_remove_recursive(btrfs_debugfs_root_dentry); |
Filipe Manana | 001a648 | 2015-01-23 18:27:00 +0000 | [diff] [blame] | 960 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 961 | |
| 962 | return ret; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 963 | } |
| 964 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 965 | void __cold btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 966 | { |
Misono Tomohiro | f902bd3 | 2018-05-17 14:24:51 +0900 | [diff] [blame] | 967 | sysfs_unmerge_group(&btrfs_kset->kobj, |
| 968 | &btrfs_static_feature_attr_group); |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 969 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 970 | kset_unregister(btrfs_kset); |
David Sterba | 1bae309 | 2014-02-05 15:36:18 +0100 | [diff] [blame] | 971 | debugfs_remove_recursive(btrfs_debugfs_root_dentry); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 972 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 973 | |