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