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 | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame^] | 358 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 359 | struct kobj_attribute *a, char *buf) |
| 360 | { |
| 361 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 362 | return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label); |
| 363 | } |
| 364 | |
| 365 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 366 | struct kobj_attribute *a, |
| 367 | const char *buf, size_t len) |
| 368 | { |
| 369 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 370 | struct btrfs_trans_handle *trans; |
| 371 | struct btrfs_root *root = fs_info->fs_root; |
| 372 | int ret; |
| 373 | |
| 374 | if (len >= BTRFS_LABEL_SIZE) { |
| 375 | pr_err("btrfs: unable to set label with more than %d bytes\n", |
| 376 | BTRFS_LABEL_SIZE - 1); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | trans = btrfs_start_transaction(root, 0); |
| 381 | if (IS_ERR(trans)) |
| 382 | return PTR_ERR(trans); |
| 383 | |
| 384 | spin_lock(&root->fs_info->super_lock); |
| 385 | strcpy(fs_info->super_copy->label, buf); |
| 386 | spin_unlock(&root->fs_info->super_lock); |
| 387 | ret = btrfs_commit_transaction(trans, root); |
| 388 | |
| 389 | if (!ret) |
| 390 | return len; |
| 391 | |
| 392 | return ret; |
| 393 | } |
| 394 | BTRFS_ATTR_RW(label, 0644, btrfs_label_show, btrfs_label_store); |
| 395 | |
| 396 | static struct attribute *btrfs_attrs[] = { |
| 397 | BTRFS_ATTR_PTR(label), |
| 398 | NULL, |
| 399 | }; |
| 400 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 401 | static void btrfs_release_super_kobj(struct kobject *kobj) |
| 402 | { |
| 403 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 404 | complete(&fs_info->kobj_unregister); |
| 405 | } |
| 406 | |
| 407 | static struct kobj_type btrfs_ktype = { |
| 408 | .sysfs_ops = &kobj_sysfs_ops, |
| 409 | .release = btrfs_release_super_kobj, |
Jeff Mahoney | f8ba9c1 | 2013-11-01 13:07:06 -0400 | [diff] [blame^] | 410 | .default_attrs = btrfs_attrs, |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 414 | { |
| 415 | if (kobj->ktype != &btrfs_ktype) |
| 416 | return NULL; |
| 417 | return container_of(kobj, struct btrfs_fs_info, super_kobj); |
| 418 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 419 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 420 | void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info) |
| 421 | { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 422 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 423 | kobject_del(fs_info->device_dir_kobj); |
| 424 | kobject_put(fs_info->device_dir_kobj); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 425 | kobject_del(fs_info->space_info_kobj); |
| 426 | kobject_put(fs_info->space_info_kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 427 | kobject_del(&fs_info->super_kobj); |
| 428 | kobject_put(&fs_info->super_kobj); |
| 429 | wait_for_completion(&fs_info->kobj_unregister); |
| 430 | } |
| 431 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 432 | const char * const btrfs_feature_set_names[3] = { |
| 433 | [FEAT_COMPAT] = "compat", |
| 434 | [FEAT_COMPAT_RO] = "compat_ro", |
| 435 | [FEAT_INCOMPAT] = "incompat", |
| 436 | }; |
| 437 | |
| 438 | #define NUM_FEATURE_BITS 64 |
| 439 | static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13]; |
| 440 | static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS]; |
| 441 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 442 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 443 | { |
| 444 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 445 | int len = 0; |
| 446 | int i; |
| 447 | char *str; |
| 448 | |
| 449 | str = kmalloc(bufsize, GFP_KERNEL); |
| 450 | if (!str) |
| 451 | return str; |
| 452 | |
| 453 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 454 | const char *name; |
| 455 | |
| 456 | if (!(flags & (1ULL << i))) |
| 457 | continue; |
| 458 | |
| 459 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 460 | len += snprintf(str + len, bufsize - len, "%s%s", |
| 461 | len ? "," : "", name); |
| 462 | } |
| 463 | |
| 464 | return str; |
| 465 | } |
| 466 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 467 | static void init_feature_attrs(void) |
| 468 | { |
| 469 | struct btrfs_feature_attr *fa; |
| 470 | int set, i; |
| 471 | |
| 472 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 473 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 474 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 475 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 476 | |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 477 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 478 | memset(btrfs_unknown_feature_names, 0, |
| 479 | sizeof(btrfs_unknown_feature_names)); |
| 480 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 481 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 482 | struct btrfs_feature_attr *sfa; |
| 483 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 484 | int bit; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 485 | sfa = attr_to_btrfs_feature_attr(a); |
Jeff Mahoney | 3b02a68 | 2013-11-01 13:07:02 -0400 | [diff] [blame] | 486 | bit = ilog2(sfa->feature_bit); |
| 487 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 488 | |
| 489 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 490 | } |
| 491 | |
| 492 | for (set = 0; set < FEAT_MAX; set++) { |
| 493 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 494 | char *name = btrfs_unknown_feature_names[set][i]; |
| 495 | fa = &btrfs_feature_attrs[set][i]; |
| 496 | |
| 497 | if (fa->kobj_attr.attr.name) |
| 498 | continue; |
| 499 | |
| 500 | snprintf(name, 13, "%s:%u", |
| 501 | btrfs_feature_set_names[set], i); |
| 502 | |
| 503 | fa->kobj_attr.attr.name = name; |
| 504 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 505 | fa->feature_set = set; |
| 506 | fa->feature_bit = 1ULL << i; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | static u64 supported_feature_masks[3] = { |
| 512 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 513 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 514 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 515 | }; |
| 516 | |
| 517 | static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info) |
| 518 | { |
| 519 | int set; |
| 520 | |
| 521 | for (set = 0; set < FEAT_MAX; set++) { |
| 522 | int i, count, ret, index = 0; |
| 523 | struct attribute **attrs; |
| 524 | struct attribute_group agroup = { |
| 525 | .name = "features", |
| 526 | }; |
| 527 | u64 features = get_features(fs_info, set); |
| 528 | features &= ~supported_feature_masks[set]; |
| 529 | |
| 530 | count = hweight64(features); |
| 531 | |
| 532 | if (!count) |
| 533 | continue; |
| 534 | |
| 535 | attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL); |
| 536 | |
| 537 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 538 | struct btrfs_feature_attr *fa; |
| 539 | |
| 540 | if (!(features & (1ULL << i))) |
| 541 | continue; |
| 542 | |
| 543 | fa = &btrfs_feature_attrs[set][i]; |
| 544 | attrs[index++] = &fa->kobj_attr.attr; |
| 545 | } |
| 546 | |
| 547 | attrs[index] = NULL; |
| 548 | agroup.attrs = attrs; |
| 549 | |
| 550 | ret = sysfs_merge_group(&fs_info->super_kobj, &agroup); |
| 551 | kfree(attrs); |
| 552 | if (ret) |
| 553 | return ret; |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 558 | static int add_device_membership(struct btrfs_fs_info *fs_info) |
| 559 | { |
| 560 | int error = 0; |
| 561 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 562 | struct btrfs_device *dev; |
| 563 | |
| 564 | fs_info->device_dir_kobj = kobject_create_and_add("devices", |
| 565 | &fs_info->super_kobj); |
| 566 | if (!fs_info->device_dir_kobj) |
| 567 | return -ENOMEM; |
| 568 | |
| 569 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
| 570 | struct hd_struct *disk = dev->bdev->bd_part; |
| 571 | struct kobject *disk_kobj = &part_to_dev(disk)->kobj; |
| 572 | |
| 573 | error = sysfs_create_link(fs_info->device_dir_kobj, |
| 574 | disk_kobj, disk_kobj->name); |
| 575 | if (error) |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | return error; |
| 580 | } |
| 581 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 582 | /* /sys/fs/btrfs/ entry */ |
| 583 | static struct kset *btrfs_kset; |
| 584 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 585 | int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info) |
| 586 | { |
| 587 | int error; |
| 588 | |
| 589 | init_completion(&fs_info->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 590 | fs_info->super_kobj.kset = btrfs_kset; |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 591 | error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL, |
| 592 | "%pU", fs_info->fsid); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 593 | |
| 594 | error = sysfs_create_group(&fs_info->super_kobj, |
| 595 | &btrfs_feature_attr_group); |
| 596 | if (error) |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 597 | goto failure; |
| 598 | |
| 599 | error = add_unknown_feature_attrs(fs_info); |
| 600 | if (error) |
| 601 | goto failure; |
| 602 | |
Jeff Mahoney | 29e5be2 | 2013-11-01 13:07:05 -0400 | [diff] [blame] | 603 | error = add_device_membership(fs_info); |
| 604 | if (error) |
| 605 | goto failure; |
| 606 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 607 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
| 608 | &fs_info->super_kobj); |
| 609 | if (!fs_info->space_info_kobj) { |
| 610 | error = -ENOMEM; |
| 611 | goto failure; |
| 612 | } |
| 613 | |
| 614 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); |
| 615 | if (error) |
| 616 | goto failure; |
| 617 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 618 | return 0; |
| 619 | failure: |
| 620 | btrfs_sysfs_remove_one(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 621 | return error; |
| 622 | } |
| 623 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 624 | int btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 625 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 626 | int ret; |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 627 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 628 | if (!btrfs_kset) |
| 629 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 630 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 631 | init_feature_attrs(); |
| 632 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 633 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
| 634 | if (ret) { |
| 635 | kset_unregister(btrfs_kset); |
| 636 | return ret; |
| 637 | } |
| 638 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 639 | return 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 640 | } |
| 641 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 642 | void btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 643 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 644 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 645 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 646 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 647 | |