Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/completion.h> |
| 23 | #include <linux/buffer_head.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 24 | #include <linux/kobject.h> |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 25 | #include <linux/bug.h> |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame^] | 26 | #include <linux/genhd.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 27 | |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 31 | #include "sysfs.h" |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame^] | 32 | #include "volumes.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 33 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 34 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 35 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 36 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 37 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 38 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 39 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 40 | if (set == FEAT_COMPAT) |
| 41 | return btrfs_super_compat_flags(disk_super); |
| 42 | else if (set == FEAT_COMPAT_RO) |
| 43 | return btrfs_super_compat_ro_flags(disk_super); |
| 44 | else |
| 45 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 48 | static void set_features(struct btrfs_fs_info *fs_info, |
| 49 | enum btrfs_feature_set set, u64 features) |
| 50 | { |
| 51 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 52 | if (set == FEAT_COMPAT) |
| 53 | btrfs_set_super_compat_flags(disk_super, features); |
| 54 | else if (set == FEAT_COMPAT_RO) |
| 55 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 56 | else |
| 57 | btrfs_set_super_incompat_flags(disk_super, features); |
| 58 | } |
| 59 | |
| 60 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 61 | { |
| 62 | int val = 0; |
| 63 | u64 set, clear; |
| 64 | switch (fa->feature_set) { |
| 65 | case FEAT_COMPAT: |
| 66 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 67 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 68 | break; |
| 69 | case FEAT_COMPAT_RO: |
| 70 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 71 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 72 | break; |
| 73 | case FEAT_INCOMPAT: |
| 74 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 75 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 76 | break; |
| 77 | default: |
| 78 | BUG(); |
| 79 | } |
| 80 | |
| 81 | if (set & fa->feature_bit) |
| 82 | val |= 1; |
| 83 | if (clear & fa->feature_bit) |
| 84 | val |= 2; |
| 85 | |
| 86 | return val; |
| 87 | } |
| 88 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 89 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 90 | struct kobj_attribute *a, char *buf) |
| 91 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 92 | int val = 0; |
| 93 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 94 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 95 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 96 | u64 features = get_features(fs_info, fa->feature_set); |
| 97 | if (features & fa->feature_bit) |
| 98 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 99 | } else |
| 100 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 101 | |
| 102 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 103 | } |
| 104 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 105 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 106 | struct kobj_attribute *a, |
| 107 | const char *buf, size_t count) |
| 108 | { |
| 109 | struct btrfs_fs_info *fs_info; |
| 110 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
| 111 | struct btrfs_trans_handle *trans; |
| 112 | u64 features, set, clear; |
| 113 | unsigned long val; |
| 114 | int ret; |
| 115 | |
| 116 | fs_info = to_fs_info(kobj); |
| 117 | if (!fs_info) |
| 118 | return -EPERM; |
| 119 | |
| 120 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | if (fa->feature_set == FEAT_COMPAT) { |
| 125 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 126 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 127 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 128 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 129 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 130 | } else { |
| 131 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 132 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 133 | } |
| 134 | |
| 135 | features = get_features(fs_info, fa->feature_set); |
| 136 | |
| 137 | /* Nothing to do */ |
| 138 | if ((val && (features & fa->feature_bit)) || |
| 139 | (!val && !(features & fa->feature_bit))) |
| 140 | return count; |
| 141 | |
| 142 | if ((val && !(set & fa->feature_bit)) || |
| 143 | (!val && !(clear & fa->feature_bit))) { |
| 144 | btrfs_info(fs_info, |
| 145 | "%sabling feature %s on mounted fs is not supported.", |
| 146 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 147 | return -EPERM; |
| 148 | } |
| 149 | |
| 150 | btrfs_info(fs_info, "%s %s feature flag", |
| 151 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 152 | |
| 153 | trans = btrfs_start_transaction(fs_info->fs_root, 1); |
| 154 | if (IS_ERR(trans)) |
| 155 | return PTR_ERR(trans); |
| 156 | |
| 157 | spin_lock(&fs_info->super_lock); |
| 158 | features = get_features(fs_info, fa->feature_set); |
| 159 | if (val) |
| 160 | features |= fa->feature_bit; |
| 161 | else |
| 162 | features &= ~fa->feature_bit; |
| 163 | set_features(fs_info, fa->feature_set, features); |
| 164 | spin_unlock(&fs_info->super_lock); |
| 165 | |
| 166 | ret = btrfs_commit_transaction(trans, fs_info->fs_root); |
| 167 | if (ret) |
| 168 | return ret; |
| 169 | |
| 170 | return count; |
| 171 | } |
| 172 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 173 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 174 | struct attribute *attr, int unused) |
| 175 | { |
| 176 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 177 | umode_t mode = attr->mode; |
| 178 | |
| 179 | if (fs_info) { |
| 180 | struct btrfs_feature_attr *fa; |
| 181 | u64 features; |
| 182 | |
| 183 | fa = attr_to_btrfs_feature_attr(attr); |
| 184 | features = get_features(fs_info, fa->feature_set); |
| 185 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame] | 186 | if (can_modify_feature(fa)) |
| 187 | mode |= S_IWUSR; |
| 188 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 189 | mode = 0; |
| 190 | } |
| 191 | |
| 192 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 196 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 197 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 198 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
| 199 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2); |
| 200 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 201 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 202 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 203 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
| 204 | |
| 205 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 206 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 207 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 208 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 209 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
| 210 | BTRFS_FEAT_ATTR_PTR(compress_lzov2), |
| 211 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 212 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 213 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 214 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
| 215 | NULL |
| 216 | }; |
| 217 | |
| 218 | static const struct attribute_group btrfs_feature_attr_group = { |
| 219 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 220 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 221 | .attrs = btrfs_supported_feature_attrs, |
| 222 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 223 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 224 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 225 | { |
| 226 | u64 val; |
| 227 | if (lock) |
| 228 | spin_lock(lock); |
| 229 | val = *value_ptr; |
| 230 | if (lock) |
| 231 | spin_unlock(lock); |
| 232 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 233 | } |
| 234 | |
| 235 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 236 | struct kobj_attribute *ka, char *buf) |
| 237 | { |
| 238 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 239 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 240 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); |
| 241 | } |
| 242 | BTRFS_ATTR(global_rsv_size, 0444, global_rsv_size_show); |
| 243 | |
| 244 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 245 | struct kobj_attribute *a, char *buf) |
| 246 | { |
| 247 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); |
| 248 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 249 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); |
| 250 | } |
| 251 | BTRFS_ATTR(global_rsv_reserved, 0444, global_rsv_reserved_show); |
| 252 | |
| 253 | #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) |
| 254 | |
| 255 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 256 | struct kobj_attribute *attr, char *buf); |
| 257 | BTRFS_RAID_ATTR(total_bytes, raid_bytes_show); |
| 258 | BTRFS_RAID_ATTR(used_bytes, raid_bytes_show); |
| 259 | |
| 260 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 261 | struct kobj_attribute *attr, char *buf) |
| 262 | |
| 263 | { |
| 264 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
| 265 | struct btrfs_block_group_cache *block_group; |
| 266 | int index = kobj - sinfo->block_group_kobjs; |
| 267 | u64 val = 0; |
| 268 | |
| 269 | down_read(&sinfo->groups_sem); |
| 270 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
| 271 | if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes)) |
| 272 | val += block_group->key.offset; |
| 273 | else |
| 274 | val += btrfs_block_group_used(&block_group->item); |
| 275 | } |
| 276 | up_read(&sinfo->groups_sem); |
| 277 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); |
| 278 | } |
| 279 | |
| 280 | static struct attribute *raid_attributes[] = { |
| 281 | BTRFS_RAID_ATTR_PTR(total_bytes), |
| 282 | BTRFS_RAID_ATTR_PTR(used_bytes), |
| 283 | NULL |
| 284 | }; |
| 285 | |
| 286 | static void release_raid_kobj(struct kobject *kobj) |
| 287 | { |
| 288 | kobject_put(kobj->parent); |
| 289 | } |
| 290 | |
| 291 | struct kobj_type btrfs_raid_ktype = { |
| 292 | .sysfs_ops = &kobj_sysfs_ops, |
| 293 | .release = release_raid_kobj, |
| 294 | .default_attrs = raid_attributes, |
| 295 | }; |
| 296 | |
| 297 | #define SPACE_INFO_ATTR(field) \ |
| 298 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 299 | struct kobj_attribute *a, \ |
| 300 | char *buf) \ |
| 301 | { \ |
| 302 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 303 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 304 | } \ |
| 305 | BTRFS_ATTR(field, 0444, btrfs_space_info_show_##field) |
| 306 | |
| 307 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, |
| 308 | struct kobj_attribute *a, |
| 309 | char *buf) |
| 310 | { |
| 311 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 312 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); |
| 313 | return snprintf(buf, PAGE_SIZE, "%lld\n", val); |
| 314 | } |
| 315 | |
| 316 | SPACE_INFO_ATTR(flags); |
| 317 | SPACE_INFO_ATTR(total_bytes); |
| 318 | SPACE_INFO_ATTR(bytes_used); |
| 319 | SPACE_INFO_ATTR(bytes_pinned); |
| 320 | SPACE_INFO_ATTR(bytes_reserved); |
| 321 | SPACE_INFO_ATTR(bytes_may_use); |
| 322 | SPACE_INFO_ATTR(disk_used); |
| 323 | SPACE_INFO_ATTR(disk_total); |
| 324 | BTRFS_ATTR(total_bytes_pinned, 0444, btrfs_space_info_show_total_bytes_pinned); |
| 325 | |
| 326 | static struct attribute *space_info_attrs[] = { |
| 327 | BTRFS_ATTR_PTR(flags), |
| 328 | BTRFS_ATTR_PTR(total_bytes), |
| 329 | BTRFS_ATTR_PTR(bytes_used), |
| 330 | BTRFS_ATTR_PTR(bytes_pinned), |
| 331 | BTRFS_ATTR_PTR(bytes_reserved), |
| 332 | BTRFS_ATTR_PTR(bytes_may_use), |
| 333 | BTRFS_ATTR_PTR(disk_used), |
| 334 | BTRFS_ATTR_PTR(disk_total), |
| 335 | BTRFS_ATTR_PTR(total_bytes_pinned), |
| 336 | NULL, |
| 337 | }; |
| 338 | |
| 339 | static void space_info_release(struct kobject *kobj) |
| 340 | { |
| 341 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 342 | percpu_counter_destroy(&sinfo->total_bytes_pinned); |
| 343 | kfree(sinfo); |
| 344 | } |
| 345 | |
| 346 | struct kobj_type space_info_ktype = { |
| 347 | .sysfs_ops = &kobj_sysfs_ops, |
| 348 | .release = space_info_release, |
| 349 | .default_attrs = space_info_attrs, |
| 350 | }; |
| 351 | |
| 352 | static const struct attribute *allocation_attrs[] = { |
| 353 | BTRFS_ATTR_PTR(global_rsv_reserved), |
| 354 | BTRFS_ATTR_PTR(global_rsv_size), |
| 355 | NULL, |
| 356 | }; |
| 357 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 358 | static void btrfs_release_super_kobj(struct kobject *kobj) |
| 359 | { |
| 360 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 361 | complete(&fs_info->kobj_unregister); |
| 362 | } |
| 363 | |
| 364 | static struct kobj_type btrfs_ktype = { |
| 365 | .sysfs_ops = &kobj_sysfs_ops, |
| 366 | .release = btrfs_release_super_kobj, |
| 367 | }; |
| 368 | |
| 369 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 370 | { |
| 371 | if (kobj->ktype != &btrfs_ktype) |
| 372 | return NULL; |
| 373 | return container_of(kobj, struct btrfs_fs_info, super_kobj); |
| 374 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 375 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 376 | void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info) |
| 377 | { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 378 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame^] | 379 | kobject_del(fs_info->device_dir_kobj); |
| 380 | kobject_put(fs_info->device_dir_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 381 | kobject_del(fs_info->space_info_kobj); |
| 382 | kobject_put(fs_info->space_info_kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 383 | kobject_del(&fs_info->super_kobj); |
| 384 | kobject_put(&fs_info->super_kobj); |
| 385 | wait_for_completion(&fs_info->kobj_unregister); |
| 386 | } |
| 387 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 388 | const char * const btrfs_feature_set_names[3] = { |
| 389 | [FEAT_COMPAT] = "compat", |
| 390 | [FEAT_COMPAT_RO] = "compat_ro", |
| 391 | [FEAT_INCOMPAT] = "incompat", |
| 392 | }; |
| 393 | |
| 394 | #define NUM_FEATURE_BITS 64 |
| 395 | static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13]; |
| 396 | static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS]; |
| 397 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 398 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 399 | { |
| 400 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 401 | int len = 0; |
| 402 | int i; |
| 403 | char *str; |
| 404 | |
| 405 | str = kmalloc(bufsize, GFP_KERNEL); |
| 406 | if (!str) |
| 407 | return str; |
| 408 | |
| 409 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 410 | const char *name; |
| 411 | |
| 412 | if (!(flags & (1ULL << i))) |
| 413 | continue; |
| 414 | |
| 415 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 416 | len += snprintf(str + len, bufsize - len, "%s%s", |
| 417 | len ? "," : "", name); |
| 418 | } |
| 419 | |
| 420 | return str; |
| 421 | } |
| 422 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 423 | static void init_feature_attrs(void) |
| 424 | { |
| 425 | struct btrfs_feature_attr *fa; |
| 426 | int set, i; |
| 427 | |
| 428 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 429 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 430 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 431 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 432 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 433 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 434 | memset(btrfs_unknown_feature_names, 0, |
| 435 | sizeof(btrfs_unknown_feature_names)); |
| 436 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 437 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 438 | struct btrfs_feature_attr *sfa; |
| 439 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 440 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 441 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 442 | bit = ilog2(sfa->feature_bit); |
| 443 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 444 | |
| 445 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 446 | } |
| 447 | |
| 448 | for (set = 0; set < FEAT_MAX; set++) { |
| 449 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 450 | char *name = btrfs_unknown_feature_names[set][i]; |
| 451 | fa = &btrfs_feature_attrs[set][i]; |
| 452 | |
| 453 | if (fa->kobj_attr.attr.name) |
| 454 | continue; |
| 455 | |
| 456 | snprintf(name, 13, "%s:%u", |
| 457 | btrfs_feature_set_names[set], i); |
| 458 | |
| 459 | fa->kobj_attr.attr.name = name; |
| 460 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 461 | fa->feature_set = set; |
| 462 | fa->feature_bit = 1ULL << i; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | static u64 supported_feature_masks[3] = { |
| 468 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 469 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 470 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 471 | }; |
| 472 | |
| 473 | static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info) |
| 474 | { |
| 475 | int set; |
| 476 | |
| 477 | for (set = 0; set < FEAT_MAX; set++) { |
| 478 | int i, count, ret, index = 0; |
| 479 | struct attribute **attrs; |
| 480 | struct attribute_group agroup = { |
| 481 | .name = "features", |
| 482 | }; |
| 483 | u64 features = get_features(fs_info, set); |
| 484 | features &= ~supported_feature_masks[set]; |
| 485 | |
| 486 | count = hweight64(features); |
| 487 | |
| 488 | if (!count) |
| 489 | continue; |
| 490 | |
| 491 | attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL); |
| 492 | |
| 493 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 494 | struct btrfs_feature_attr *fa; |
| 495 | |
| 496 | if (!(features & (1ULL << i))) |
| 497 | continue; |
| 498 | |
| 499 | fa = &btrfs_feature_attrs[set][i]; |
| 500 | attrs[index++] = &fa->kobj_attr.attr; |
| 501 | } |
| 502 | |
| 503 | attrs[index] = NULL; |
| 504 | agroup.attrs = attrs; |
| 505 | |
| 506 | ret = sysfs_merge_group(&fs_info->super_kobj, &agroup); |
| 507 | kfree(attrs); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | } |
| 511 | return 0; |
| 512 | } |
| 513 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame^] | 514 | static int add_device_membership(struct btrfs_fs_info *fs_info) |
| 515 | { |
| 516 | int error = 0; |
| 517 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 518 | struct btrfs_device *dev; |
| 519 | |
| 520 | fs_info->device_dir_kobj = kobject_create_and_add("devices", |
| 521 | &fs_info->super_kobj); |
| 522 | if (!fs_info->device_dir_kobj) |
| 523 | return -ENOMEM; |
| 524 | |
| 525 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
| 526 | struct hd_struct *disk = dev->bdev->bd_part; |
| 527 | struct kobject *disk_kobj = &part_to_dev(disk)->kobj; |
| 528 | |
| 529 | error = sysfs_create_link(fs_info->device_dir_kobj, |
| 530 | disk_kobj, disk_kobj->name); |
| 531 | if (error) |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | return error; |
| 536 | } |
| 537 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 538 | /* /sys/fs/btrfs/ entry */ |
| 539 | static struct kset *btrfs_kset; |
| 540 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 541 | int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info) |
| 542 | { |
| 543 | int error; |
| 544 | |
| 545 | init_completion(&fs_info->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 546 | fs_info->super_kobj.kset = btrfs_kset; |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 547 | error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL, |
| 548 | "%pU", fs_info->fsid); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 549 | |
| 550 | error = sysfs_create_group(&fs_info->super_kobj, |
| 551 | &btrfs_feature_attr_group); |
| 552 | if (error) |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 553 | goto failure; |
| 554 | |
| 555 | error = add_unknown_feature_attrs(fs_info); |
| 556 | if (error) |
| 557 | goto failure; |
| 558 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame^] | 559 | error = add_device_membership(fs_info); |
| 560 | if (error) |
| 561 | goto failure; |
| 562 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 563 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
| 564 | &fs_info->super_kobj); |
| 565 | if (!fs_info->space_info_kobj) { |
| 566 | error = -ENOMEM; |
| 567 | goto failure; |
| 568 | } |
| 569 | |
| 570 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 571 | if (error) |
| 572 | goto failure; |
| 573 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 574 | return 0; |
| 575 | failure: |
| 576 | btrfs_sysfs_remove_one(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 577 | return error; |
| 578 | } |
| 579 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 580 | int btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 581 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 582 | int ret; |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 583 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 584 | if (!btrfs_kset) |
| 585 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 586 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 587 | init_feature_attrs(); |
| 588 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 589 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
| 590 | if (ret) { |
| 591 | kset_unregister(btrfs_kset); |
| 592 | return ret; |
| 593 | } |
| 594 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 595 | return 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 598 | void btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 599 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 600 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 601 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 602 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 603 | |