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