1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 26cbd5570SChris Mason /* 36cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 46cbd5570SChris Mason */ 56cbd5570SChris Mason 658176a96SJosef Bacik #include <linux/sched.h> 732a9991fSDavid Sterba #include <linux/sched/mm.h> 858176a96SJosef Bacik #include <linux/slab.h> 958176a96SJosef Bacik #include <linux/spinlock.h> 1058176a96SJosef Bacik #include <linux/completion.h> 1179da4fa4SJeff Mahoney #include <linux/bug.h> 1241e6d2a8SJohannes Thumshirn #include <crypto/hash.h> 1358176a96SJosef Bacik 14bae45de0SChris Mason #include "ctree.h" 15dfb79ddbSDennis Zhou #include "discard.h" 16bae45de0SChris Mason #include "disk-io.h" 177573df55SOmar Sandoval #include "send.h" 18bae45de0SChris Mason #include "transaction.h" 19079b72bcSJeff Mahoney #include "sysfs.h" 2029e5be24SJeff Mahoney #include "volumes.h" 218719aaaeSJosef Bacik #include "space-info.h" 22aac0023cSJosef Bacik #include "block-group.h" 2349e5fb46SQu Wenruo #include "qgroup.h" 24079b72bcSJeff Mahoney 259188db61SDavid Sterba struct btrfs_feature_attr { 269188db61SDavid Sterba struct kobj_attribute kobj_attr; 279188db61SDavid Sterba enum btrfs_feature_set feature_set; 289188db61SDavid Sterba u64 feature_bit; 299188db61SDavid Sterba }; 309188db61SDavid Sterba 319188db61SDavid Sterba /* For raid type sysfs entries */ 329188db61SDavid Sterba struct raid_kobject { 339188db61SDavid Sterba u64 flags; 349188db61SDavid Sterba struct kobject kobj; 359188db61SDavid Sterba }; 369188db61SDavid Sterba 379188db61SDavid Sterba #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ 389188db61SDavid Sterba { \ 399188db61SDavid Sterba .attr = { .name = __stringify(_name), .mode = _mode }, \ 409188db61SDavid Sterba .show = _show, \ 419188db61SDavid Sterba .store = _store, \ 429188db61SDavid Sterba } 439188db61SDavid Sterba 449188db61SDavid Sterba #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ 459188db61SDavid Sterba static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 469188db61SDavid Sterba __INIT_KOBJ_ATTR(_name, 0644, _show, _store) 479188db61SDavid Sterba 489188db61SDavid Sterba #define BTRFS_ATTR(_prefix, _name, _show) \ 499188db61SDavid Sterba static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 509188db61SDavid Sterba __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) 519188db61SDavid Sterba 529188db61SDavid Sterba #define BTRFS_ATTR_PTR(_prefix, _name) \ 539188db61SDavid Sterba (&btrfs_attr_##_prefix##_##_name.attr) 549188db61SDavid Sterba 559188db61SDavid Sterba #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ 569188db61SDavid Sterba static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ 579188db61SDavid Sterba .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ 589188db61SDavid Sterba btrfs_feature_attr_show, \ 599188db61SDavid Sterba btrfs_feature_attr_store), \ 609188db61SDavid Sterba .feature_set = _feature_set, \ 619188db61SDavid Sterba .feature_bit = _feature_prefix ##_## _feature_bit, \ 629188db61SDavid Sterba } 639188db61SDavid Sterba #define BTRFS_FEAT_ATTR_PTR(_name) \ 649188db61SDavid Sterba (&btrfs_attr_features_##_name.kobj_attr.attr) 659188db61SDavid Sterba 669188db61SDavid Sterba #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ 679188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) 689188db61SDavid Sterba #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ 699188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) 709188db61SDavid Sterba #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ 719188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) 729188db61SDavid Sterba 73510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); 742e7910d6SAnand Jain static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); 755ac1d209SJeff Mahoney 768f52316cSDavid Sterba static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) 778f52316cSDavid Sterba { 788f52316cSDavid Sterba return container_of(a, struct btrfs_feature_attr, kobj_attr); 798f52316cSDavid Sterba } 808f52316cSDavid Sterba 818f52316cSDavid Sterba static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) 828f52316cSDavid Sterba { 838f52316cSDavid Sterba return container_of(attr, struct kobj_attribute, attr); 848f52316cSDavid Sterba } 858f52316cSDavid Sterba 868f52316cSDavid Sterba static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( 878f52316cSDavid Sterba struct attribute *attr) 888f52316cSDavid Sterba { 898f52316cSDavid Sterba return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); 908f52316cSDavid Sterba } 918f52316cSDavid Sterba 92510d7360SJeff Mahoney static u64 get_features(struct btrfs_fs_info *fs_info, 93510d7360SJeff Mahoney enum btrfs_feature_set set) 945ac1d209SJeff Mahoney { 95510d7360SJeff Mahoney struct btrfs_super_block *disk_super = fs_info->super_copy; 96510d7360SJeff Mahoney if (set == FEAT_COMPAT) 97510d7360SJeff Mahoney return btrfs_super_compat_flags(disk_super); 98510d7360SJeff Mahoney else if (set == FEAT_COMPAT_RO) 99510d7360SJeff Mahoney return btrfs_super_compat_ro_flags(disk_super); 100510d7360SJeff Mahoney else 101510d7360SJeff Mahoney return btrfs_super_incompat_flags(disk_super); 1025ac1d209SJeff Mahoney } 1035ac1d209SJeff Mahoney 104ba631941SJeff Mahoney static void set_features(struct btrfs_fs_info *fs_info, 105ba631941SJeff Mahoney enum btrfs_feature_set set, u64 features) 106ba631941SJeff Mahoney { 107ba631941SJeff Mahoney struct btrfs_super_block *disk_super = fs_info->super_copy; 108ba631941SJeff Mahoney if (set == FEAT_COMPAT) 109ba631941SJeff Mahoney btrfs_set_super_compat_flags(disk_super, features); 110ba631941SJeff Mahoney else if (set == FEAT_COMPAT_RO) 111ba631941SJeff Mahoney btrfs_set_super_compat_ro_flags(disk_super, features); 112ba631941SJeff Mahoney else 113ba631941SJeff Mahoney btrfs_set_super_incompat_flags(disk_super, features); 114ba631941SJeff Mahoney } 115ba631941SJeff Mahoney 116ba631941SJeff Mahoney static int can_modify_feature(struct btrfs_feature_attr *fa) 117ba631941SJeff Mahoney { 118ba631941SJeff Mahoney int val = 0; 119ba631941SJeff Mahoney u64 set, clear; 120ba631941SJeff Mahoney switch (fa->feature_set) { 121ba631941SJeff Mahoney case FEAT_COMPAT: 122ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_SAFE_SET; 123ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 124ba631941SJeff Mahoney break; 125ba631941SJeff Mahoney case FEAT_COMPAT_RO: 126ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 127ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 128ba631941SJeff Mahoney break; 129ba631941SJeff Mahoney case FEAT_INCOMPAT: 130ba631941SJeff Mahoney set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 131ba631941SJeff Mahoney clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 132ba631941SJeff Mahoney break; 133ba631941SJeff Mahoney default: 13462e85577SJeff Mahoney pr_warn("btrfs: sysfs: unknown feature set %d\n", 135cc37bb04SDavid Sterba fa->feature_set); 136cc37bb04SDavid Sterba return 0; 137ba631941SJeff Mahoney } 138ba631941SJeff Mahoney 139ba631941SJeff Mahoney if (set & fa->feature_bit) 140ba631941SJeff Mahoney val |= 1; 141ba631941SJeff Mahoney if (clear & fa->feature_bit) 142ba631941SJeff Mahoney val |= 2; 143ba631941SJeff Mahoney 144ba631941SJeff Mahoney return val; 145ba631941SJeff Mahoney } 146ba631941SJeff Mahoney 147079b72bcSJeff Mahoney static ssize_t btrfs_feature_attr_show(struct kobject *kobj, 148079b72bcSJeff Mahoney struct kobj_attribute *a, char *buf) 149079b72bcSJeff Mahoney { 150510d7360SJeff Mahoney int val = 0; 151510d7360SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 152510d7360SJeff Mahoney struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 153ba631941SJeff Mahoney if (fs_info) { 154510d7360SJeff Mahoney u64 features = get_features(fs_info, fa->feature_set); 155510d7360SJeff Mahoney if (features & fa->feature_bit) 156510d7360SJeff Mahoney val = 1; 157ba631941SJeff Mahoney } else 158ba631941SJeff Mahoney val = can_modify_feature(fa); 159510d7360SJeff Mahoney 160abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 161510d7360SJeff Mahoney } 162510d7360SJeff Mahoney 163ba631941SJeff Mahoney static ssize_t btrfs_feature_attr_store(struct kobject *kobj, 164ba631941SJeff Mahoney struct kobj_attribute *a, 165ba631941SJeff Mahoney const char *buf, size_t count) 166ba631941SJeff Mahoney { 167ba631941SJeff Mahoney struct btrfs_fs_info *fs_info; 168ba631941SJeff Mahoney struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 169ba631941SJeff Mahoney u64 features, set, clear; 170ba631941SJeff Mahoney unsigned long val; 171ba631941SJeff Mahoney int ret; 172ba631941SJeff Mahoney 173ba631941SJeff Mahoney fs_info = to_fs_info(kobj); 174ba631941SJeff Mahoney if (!fs_info) 175ba631941SJeff Mahoney return -EPERM; 176ba631941SJeff Mahoney 177bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) 178ee611138SDavid Sterba return -EROFS; 179ee611138SDavid Sterba 180ba631941SJeff Mahoney ret = kstrtoul(skip_spaces(buf), 0, &val); 181ba631941SJeff Mahoney if (ret) 182ba631941SJeff Mahoney return ret; 183ba631941SJeff Mahoney 184ba631941SJeff Mahoney if (fa->feature_set == FEAT_COMPAT) { 185ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_SAFE_SET; 186ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 187ba631941SJeff Mahoney } else if (fa->feature_set == FEAT_COMPAT_RO) { 188ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 189ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 190ba631941SJeff Mahoney } else { 191ba631941SJeff Mahoney set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 192ba631941SJeff Mahoney clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 193ba631941SJeff Mahoney } 194ba631941SJeff Mahoney 195ba631941SJeff Mahoney features = get_features(fs_info, fa->feature_set); 196ba631941SJeff Mahoney 197ba631941SJeff Mahoney /* Nothing to do */ 198ba631941SJeff Mahoney if ((val && (features & fa->feature_bit)) || 199ba631941SJeff Mahoney (!val && !(features & fa->feature_bit))) 200ba631941SJeff Mahoney return count; 201ba631941SJeff Mahoney 202ba631941SJeff Mahoney if ((val && !(set & fa->feature_bit)) || 203ba631941SJeff Mahoney (!val && !(clear & fa->feature_bit))) { 204ba631941SJeff Mahoney btrfs_info(fs_info, 205ba631941SJeff Mahoney "%sabling feature %s on mounted fs is not supported.", 206ba631941SJeff Mahoney val ? "En" : "Dis", fa->kobj_attr.attr.name); 207ba631941SJeff Mahoney return -EPERM; 208ba631941SJeff Mahoney } 209ba631941SJeff Mahoney 210ba631941SJeff Mahoney btrfs_info(fs_info, "%s %s feature flag", 211ba631941SJeff Mahoney val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); 212ba631941SJeff Mahoney 213ba631941SJeff Mahoney spin_lock(&fs_info->super_lock); 214ba631941SJeff Mahoney features = get_features(fs_info, fa->feature_set); 215ba631941SJeff Mahoney if (val) 216ba631941SJeff Mahoney features |= fa->feature_bit; 217ba631941SJeff Mahoney else 218ba631941SJeff Mahoney features &= ~fa->feature_bit; 219ba631941SJeff Mahoney set_features(fs_info, fa->feature_set, features); 220ba631941SJeff Mahoney spin_unlock(&fs_info->super_lock); 221ba631941SJeff Mahoney 2220eae2747SDavid Sterba /* 2230eae2747SDavid Sterba * We don't want to do full transaction commit from inside sysfs 2240eae2747SDavid Sterba */ 2250eae2747SDavid Sterba btrfs_set_pending(fs_info, COMMIT); 2260eae2747SDavid Sterba wake_up_process(fs_info->transaction_kthread); 227ba631941SJeff Mahoney 228ba631941SJeff Mahoney return count; 229ba631941SJeff Mahoney } 230ba631941SJeff Mahoney 231510d7360SJeff Mahoney static umode_t btrfs_feature_visible(struct kobject *kobj, 232510d7360SJeff Mahoney struct attribute *attr, int unused) 233510d7360SJeff Mahoney { 234510d7360SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 235510d7360SJeff Mahoney umode_t mode = attr->mode; 236510d7360SJeff Mahoney 237510d7360SJeff Mahoney if (fs_info) { 238510d7360SJeff Mahoney struct btrfs_feature_attr *fa; 239510d7360SJeff Mahoney u64 features; 240510d7360SJeff Mahoney 241510d7360SJeff Mahoney fa = attr_to_btrfs_feature_attr(attr); 242510d7360SJeff Mahoney features = get_features(fs_info, fa->feature_set); 243510d7360SJeff Mahoney 244ba631941SJeff Mahoney if (can_modify_feature(fa)) 245ba631941SJeff Mahoney mode |= S_IWUSR; 246ba631941SJeff Mahoney else if (!(features & fa->feature_bit)) 247510d7360SJeff Mahoney mode = 0; 248510d7360SJeff Mahoney } 249510d7360SJeff Mahoney 250510d7360SJeff Mahoney return mode; 251079b72bcSJeff Mahoney } 252079b72bcSJeff Mahoney 253079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); 254079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); 255079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); 256079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); 2575c1aab1dSNick Terrell BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); 258079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); 259079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); 260079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); 261079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); 262c736c095SDavid Sterba BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); 26356f20f40SNikolay Borisov BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); 2643b5bb73bSDavid Sterba BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); 265cfbb825cSDavid Sterba BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); 266079b72bcSJeff Mahoney 267079b72bcSJeff Mahoney static struct attribute *btrfs_supported_feature_attrs[] = { 268079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(mixed_backref), 269079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(default_subvol), 270079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(mixed_groups), 271079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(compress_lzo), 2725c1aab1dSNick Terrell BTRFS_FEAT_ATTR_PTR(compress_zstd), 273079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(big_metadata), 274079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(extended_iref), 275079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(raid56), 276079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(skinny_metadata), 277c736c095SDavid Sterba BTRFS_FEAT_ATTR_PTR(no_holes), 27856f20f40SNikolay Borisov BTRFS_FEAT_ATTR_PTR(metadata_uuid), 2793b5bb73bSDavid Sterba BTRFS_FEAT_ATTR_PTR(free_space_tree), 280cfbb825cSDavid Sterba BTRFS_FEAT_ATTR_PTR(raid1c34), 281079b72bcSJeff Mahoney NULL 282079b72bcSJeff Mahoney }; 283079b72bcSJeff Mahoney 284f902bd3aSMisono Tomohiro /* 285f902bd3aSMisono Tomohiro * Features which depend on feature bits and may differ between each fs. 286f902bd3aSMisono Tomohiro * 287f902bd3aSMisono Tomohiro * /sys/fs/btrfs/features lists all available features of this kernel while 288f902bd3aSMisono Tomohiro * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or 289f902bd3aSMisono Tomohiro * can be changed online. 290f902bd3aSMisono Tomohiro */ 291079b72bcSJeff Mahoney static const struct attribute_group btrfs_feature_attr_group = { 292079b72bcSJeff Mahoney .name = "features", 293510d7360SJeff Mahoney .is_visible = btrfs_feature_visible, 294079b72bcSJeff Mahoney .attrs = btrfs_supported_feature_attrs, 295079b72bcSJeff Mahoney }; 29658176a96SJosef Bacik 297f902bd3aSMisono Tomohiro static ssize_t rmdir_subvol_show(struct kobject *kobj, 298f902bd3aSMisono Tomohiro struct kobj_attribute *ka, char *buf) 299f902bd3aSMisono Tomohiro { 300abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "0\n"); 301f902bd3aSMisono Tomohiro } 302f902bd3aSMisono Tomohiro BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); 303f902bd3aSMisono Tomohiro 304f7cea56cSDavid Sterba static ssize_t supported_checksums_show(struct kobject *kobj, 305f7cea56cSDavid Sterba struct kobj_attribute *a, char *buf) 306f7cea56cSDavid Sterba { 307f7cea56cSDavid Sterba ssize_t ret = 0; 308f7cea56cSDavid Sterba int i; 309f7cea56cSDavid Sterba 310f7cea56cSDavid Sterba for (i = 0; i < btrfs_get_num_csums(); i++) { 311f7cea56cSDavid Sterba /* 312f7cea56cSDavid Sterba * This "trick" only works as long as 'enum btrfs_csum_type' has 313f7cea56cSDavid Sterba * no holes in it 314f7cea56cSDavid Sterba */ 315abdd9febSTakashi Iwai ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", 316f7cea56cSDavid Sterba (i == 0 ? "" : " "), btrfs_super_csum_name(i)); 317f7cea56cSDavid Sterba 318f7cea56cSDavid Sterba } 319f7cea56cSDavid Sterba 320abdd9febSTakashi Iwai ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); 321f7cea56cSDavid Sterba return ret; 322f7cea56cSDavid Sterba } 323f7cea56cSDavid Sterba BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); 324f7cea56cSDavid Sterba 3257573df55SOmar Sandoval static ssize_t send_stream_version_show(struct kobject *kobj, 3267573df55SOmar Sandoval struct kobj_attribute *ka, char *buf) 3277573df55SOmar Sandoval { 3287573df55SOmar Sandoval return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION); 3297573df55SOmar Sandoval } 3307573df55SOmar Sandoval BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show); 3317573df55SOmar Sandoval 332f902bd3aSMisono Tomohiro static struct attribute *btrfs_supported_static_feature_attrs[] = { 333f902bd3aSMisono Tomohiro BTRFS_ATTR_PTR(static_feature, rmdir_subvol), 334f7cea56cSDavid Sterba BTRFS_ATTR_PTR(static_feature, supported_checksums), 3357573df55SOmar Sandoval BTRFS_ATTR_PTR(static_feature, send_stream_version), 336f902bd3aSMisono Tomohiro NULL 337f902bd3aSMisono Tomohiro }; 338f902bd3aSMisono Tomohiro 339f902bd3aSMisono Tomohiro /* 340f902bd3aSMisono Tomohiro * Features which only depend on kernel version. 341f902bd3aSMisono Tomohiro * 342f902bd3aSMisono Tomohiro * These are listed in /sys/fs/btrfs/features along with 343f902bd3aSMisono Tomohiro * btrfs_feature_attr_group 344f902bd3aSMisono Tomohiro */ 345f902bd3aSMisono Tomohiro static const struct attribute_group btrfs_static_feature_attr_group = { 346f902bd3aSMisono Tomohiro .name = "features", 347f902bd3aSMisono Tomohiro .attrs = btrfs_supported_static_feature_attrs, 348f902bd3aSMisono Tomohiro }; 349f902bd3aSMisono Tomohiro 3506e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 3516e369febSDavid Sterba 3526e369febSDavid Sterba /* 353e4faab84SDennis Zhou * Discard statistics and tunables 354e4faab84SDennis Zhou */ 355dfb79ddbSDennis Zhou #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) 356dfb79ddbSDennis Zhou 3575dc7c10bSDennis Zhou static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, 3585dc7c10bSDennis Zhou struct kobj_attribute *a, 3595dc7c10bSDennis Zhou char *buf) 3605dc7c10bSDennis Zhou { 3615dc7c10bSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3625dc7c10bSDennis Zhou 363abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3645dc7c10bSDennis Zhou atomic64_read(&fs_info->discard_ctl.discardable_bytes)); 3655dc7c10bSDennis Zhou } 3665dc7c10bSDennis Zhou BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); 3675dc7c10bSDennis Zhou 368dfb79ddbSDennis Zhou static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, 369dfb79ddbSDennis Zhou struct kobj_attribute *a, 370dfb79ddbSDennis Zhou char *buf) 371dfb79ddbSDennis Zhou { 372dfb79ddbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 373dfb79ddbSDennis Zhou 374abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", 375dfb79ddbSDennis Zhou atomic_read(&fs_info->discard_ctl.discardable_extents)); 376dfb79ddbSDennis Zhou } 377dfb79ddbSDennis Zhou BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); 378dfb79ddbSDennis Zhou 3799ddf648fSDennis Zhou static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, 3809ddf648fSDennis Zhou struct kobj_attribute *a, 3819ddf648fSDennis Zhou char *buf) 3829ddf648fSDennis Zhou { 3839ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3849ddf648fSDennis Zhou 385abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3869ddf648fSDennis Zhou fs_info->discard_ctl.discard_bitmap_bytes); 3879ddf648fSDennis Zhou } 3889ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); 3899ddf648fSDennis Zhou 3909ddf648fSDennis Zhou static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, 3919ddf648fSDennis Zhou struct kobj_attribute *a, 3929ddf648fSDennis Zhou char *buf) 3939ddf648fSDennis Zhou { 3949ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3959ddf648fSDennis Zhou 396abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3979ddf648fSDennis Zhou atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); 3989ddf648fSDennis Zhou } 3999ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); 4009ddf648fSDennis Zhou 4019ddf648fSDennis Zhou static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, 4029ddf648fSDennis Zhou struct kobj_attribute *a, 4039ddf648fSDennis Zhou char *buf) 4049ddf648fSDennis Zhou { 4059ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 4069ddf648fSDennis Zhou 407abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 4089ddf648fSDennis Zhou fs_info->discard_ctl.discard_extent_bytes); 4099ddf648fSDennis Zhou } 4109ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); 4119ddf648fSDennis Zhou 412a2309300SDennis Zhou static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, 413a2309300SDennis Zhou struct kobj_attribute *a, 414a2309300SDennis Zhou char *buf) 415a2309300SDennis Zhou { 416a2309300SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 417a2309300SDennis Zhou 418abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 419a2309300SDennis Zhou READ_ONCE(fs_info->discard_ctl.iops_limit)); 420a2309300SDennis Zhou } 421a2309300SDennis Zhou 422a2309300SDennis Zhou static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, 423a2309300SDennis Zhou struct kobj_attribute *a, 424a2309300SDennis Zhou const char *buf, size_t len) 425a2309300SDennis Zhou { 426a2309300SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 427a2309300SDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 428a2309300SDennis Zhou u32 iops_limit; 429a2309300SDennis Zhou int ret; 430a2309300SDennis Zhou 431a2309300SDennis Zhou ret = kstrtou32(buf, 10, &iops_limit); 432a2309300SDennis Zhou if (ret) 433a2309300SDennis Zhou return -EINVAL; 434a2309300SDennis Zhou 435a2309300SDennis Zhou WRITE_ONCE(discard_ctl->iops_limit, iops_limit); 436a2309300SDennis Zhou 437a2309300SDennis Zhou return len; 438a2309300SDennis Zhou } 439a2309300SDennis Zhou BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, 440a2309300SDennis Zhou btrfs_discard_iops_limit_store); 441a2309300SDennis Zhou 442e93591bbSDennis Zhou static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, 443e93591bbSDennis Zhou struct kobj_attribute *a, 444e93591bbSDennis Zhou char *buf) 445e93591bbSDennis Zhou { 446e93591bbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 447e93591bbSDennis Zhou 448abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 449e93591bbSDennis Zhou READ_ONCE(fs_info->discard_ctl.kbps_limit)); 450e93591bbSDennis Zhou } 451e93591bbSDennis Zhou 452e93591bbSDennis Zhou static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, 453e93591bbSDennis Zhou struct kobj_attribute *a, 454e93591bbSDennis Zhou const char *buf, size_t len) 455e93591bbSDennis Zhou { 456e93591bbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 457e93591bbSDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 458e93591bbSDennis Zhou u32 kbps_limit; 459e93591bbSDennis Zhou int ret; 460e93591bbSDennis Zhou 461e93591bbSDennis Zhou ret = kstrtou32(buf, 10, &kbps_limit); 462e93591bbSDennis Zhou if (ret) 463e93591bbSDennis Zhou return -EINVAL; 464e93591bbSDennis Zhou 465e93591bbSDennis Zhou WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); 466e93591bbSDennis Zhou 467e93591bbSDennis Zhou return len; 468e93591bbSDennis Zhou } 469e93591bbSDennis Zhou BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, 470e93591bbSDennis Zhou btrfs_discard_kbps_limit_store); 471e93591bbSDennis Zhou 47219b2a2c7SDennis Zhou static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, 47319b2a2c7SDennis Zhou struct kobj_attribute *a, 47419b2a2c7SDennis Zhou char *buf) 47519b2a2c7SDennis Zhou { 47619b2a2c7SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 47719b2a2c7SDennis Zhou 478abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", 47919b2a2c7SDennis Zhou READ_ONCE(fs_info->discard_ctl.max_discard_size)); 48019b2a2c7SDennis Zhou } 48119b2a2c7SDennis Zhou 48219b2a2c7SDennis Zhou static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, 48319b2a2c7SDennis Zhou struct kobj_attribute *a, 48419b2a2c7SDennis Zhou const char *buf, size_t len) 48519b2a2c7SDennis Zhou { 48619b2a2c7SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 48719b2a2c7SDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 48819b2a2c7SDennis Zhou u64 max_discard_size; 48919b2a2c7SDennis Zhou int ret; 49019b2a2c7SDennis Zhou 49119b2a2c7SDennis Zhou ret = kstrtou64(buf, 10, &max_discard_size); 49219b2a2c7SDennis Zhou if (ret) 49319b2a2c7SDennis Zhou return -EINVAL; 49419b2a2c7SDennis Zhou 49519b2a2c7SDennis Zhou WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); 49619b2a2c7SDennis Zhou 49719b2a2c7SDennis Zhou return len; 49819b2a2c7SDennis Zhou } 49919b2a2c7SDennis Zhou BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, 50019b2a2c7SDennis Zhou btrfs_discard_max_discard_size_store); 50119b2a2c7SDennis Zhou 502e4faab84SDennis Zhou static const struct attribute *discard_debug_attrs[] = { 5035dc7c10bSDennis Zhou BTRFS_ATTR_PTR(discard, discardable_bytes), 504dfb79ddbSDennis Zhou BTRFS_ATTR_PTR(discard, discardable_extents), 5059ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), 5069ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_bytes_saved), 5079ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_extent_bytes), 508a2309300SDennis Zhou BTRFS_ATTR_PTR(discard, iops_limit), 509e93591bbSDennis Zhou BTRFS_ATTR_PTR(discard, kbps_limit), 51019b2a2c7SDennis Zhou BTRFS_ATTR_PTR(discard, max_discard_size), 511e4faab84SDennis Zhou NULL, 512e4faab84SDennis Zhou }; 513e4faab84SDennis Zhou 514e4faab84SDennis Zhou /* 5156e369febSDavid Sterba * Runtime debugging exported via sysfs 5166e369febSDavid Sterba * 5176e369febSDavid Sterba * /sys/fs/btrfs/debug - applies to module or all filesystems 5186e369febSDavid Sterba * /sys/fs/btrfs/UUID - applies only to the given filesystem 5196e369febSDavid Sterba */ 52093945cb4SDennis Zhou static const struct attribute *btrfs_debug_mount_attrs[] = { 52193945cb4SDennis Zhou NULL, 52293945cb4SDennis Zhou }; 52393945cb4SDennis Zhou 5246e369febSDavid Sterba static struct attribute *btrfs_debug_feature_attrs[] = { 5256e369febSDavid Sterba NULL 5266e369febSDavid Sterba }; 5276e369febSDavid Sterba 5286e369febSDavid Sterba static const struct attribute_group btrfs_debug_feature_attr_group = { 5296e369febSDavid Sterba .name = "debug", 5306e369febSDavid Sterba .attrs = btrfs_debug_feature_attrs, 5316e369febSDavid Sterba }; 5326e369febSDavid Sterba 5336e369febSDavid Sterba #endif 5346e369febSDavid Sterba 5356ab0a202SJeff Mahoney static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) 5366ab0a202SJeff Mahoney { 5376ab0a202SJeff Mahoney u64 val; 5386ab0a202SJeff Mahoney if (lock) 5396ab0a202SJeff Mahoney spin_lock(lock); 5406ab0a202SJeff Mahoney val = *value_ptr; 5416ab0a202SJeff Mahoney if (lock) 5426ab0a202SJeff Mahoney spin_unlock(lock); 543abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", val); 5446ab0a202SJeff Mahoney } 5456ab0a202SJeff Mahoney 5466ab0a202SJeff Mahoney static ssize_t global_rsv_size_show(struct kobject *kobj, 5476ab0a202SJeff Mahoney struct kobj_attribute *ka, char *buf) 5486ab0a202SJeff Mahoney { 5496ab0a202SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); 5506ab0a202SJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 5516ab0a202SJeff Mahoney return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); 5526ab0a202SJeff Mahoney } 553a969f4ccSHans van Kranenburg BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); 5546ab0a202SJeff Mahoney 5556ab0a202SJeff Mahoney static ssize_t global_rsv_reserved_show(struct kobject *kobj, 5566ab0a202SJeff Mahoney struct kobj_attribute *a, char *buf) 5576ab0a202SJeff Mahoney { 5586ab0a202SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); 5596ab0a202SJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 5606ab0a202SJeff Mahoney return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); 5616ab0a202SJeff Mahoney } 562a969f4ccSHans van Kranenburg BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); 5636ab0a202SJeff Mahoney 5646ab0a202SJeff Mahoney #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) 565c1895442SJeff Mahoney #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) 5666ab0a202SJeff Mahoney 5676ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj, 5686ab0a202SJeff Mahoney struct kobj_attribute *attr, char *buf); 569a969f4ccSHans van Kranenburg BTRFS_ATTR(raid, total_bytes, raid_bytes_show); 570a969f4ccSHans van Kranenburg BTRFS_ATTR(raid, used_bytes, raid_bytes_show); 5716ab0a202SJeff Mahoney 5726ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj, 5736ab0a202SJeff Mahoney struct kobj_attribute *attr, char *buf) 5746ab0a202SJeff Mahoney 5756ab0a202SJeff Mahoney { 5766ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj->parent); 57732da5386SDavid Sterba struct btrfs_block_group *block_group; 57875cb379dSJeff Mahoney int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); 5796ab0a202SJeff Mahoney u64 val = 0; 5806ab0a202SJeff Mahoney 5816ab0a202SJeff Mahoney down_read(&sinfo->groups_sem); 5826ab0a202SJeff Mahoney list_for_each_entry(block_group, &sinfo->block_groups[index], list) { 583a969f4ccSHans van Kranenburg if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) 584b3470b5dSDavid Sterba val += block_group->length; 5856ab0a202SJeff Mahoney else 586bf38be65SDavid Sterba val += block_group->used; 5876ab0a202SJeff Mahoney } 5886ab0a202SJeff Mahoney up_read(&sinfo->groups_sem); 589abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", val); 5906ab0a202SJeff Mahoney } 5916ab0a202SJeff Mahoney 5927c7e3014SKimberly Brown static struct attribute *raid_attrs[] = { 593a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(raid, total_bytes), 594a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(raid, used_bytes), 5956ab0a202SJeff Mahoney NULL 5966ab0a202SJeff Mahoney }; 5977c7e3014SKimberly Brown ATTRIBUTE_GROUPS(raid); 5986ab0a202SJeff Mahoney 5996ab0a202SJeff Mahoney static void release_raid_kobj(struct kobject *kobj) 6006ab0a202SJeff Mahoney { 601c1895442SJeff Mahoney kfree(to_raid_kobj(kobj)); 6026ab0a202SJeff Mahoney } 6036ab0a202SJeff Mahoney 604536ea45cSDavid Sterba static struct kobj_type btrfs_raid_ktype = { 6056ab0a202SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 6066ab0a202SJeff Mahoney .release = release_raid_kobj, 6077c7e3014SKimberly Brown .default_groups = raid_groups, 6086ab0a202SJeff Mahoney }; 6096ab0a202SJeff Mahoney 6106ab0a202SJeff Mahoney #define SPACE_INFO_ATTR(field) \ 6116ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ 6126ab0a202SJeff Mahoney struct kobj_attribute *a, \ 6136ab0a202SJeff Mahoney char *buf) \ 6146ab0a202SJeff Mahoney { \ 6156ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj); \ 6166ab0a202SJeff Mahoney return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ 6176ab0a202SJeff Mahoney } \ 618a969f4ccSHans van Kranenburg BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) 6196ab0a202SJeff Mahoney 6206ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, 6216ab0a202SJeff Mahoney struct kobj_attribute *a, 6226ab0a202SJeff Mahoney char *buf) 6236ab0a202SJeff Mahoney { 6246ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj); 6256ab0a202SJeff Mahoney s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); 626abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", val); 6276ab0a202SJeff Mahoney } 6286ab0a202SJeff Mahoney 6296ab0a202SJeff Mahoney SPACE_INFO_ATTR(flags); 6306ab0a202SJeff Mahoney SPACE_INFO_ATTR(total_bytes); 6316ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_used); 6326ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_pinned); 6336ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_reserved); 6346ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_may_use); 635c1fd5c30SWang Xiaoguang SPACE_INFO_ATTR(bytes_readonly); 6366ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_used); 6376ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_total); 638a969f4ccSHans van Kranenburg BTRFS_ATTR(space_info, total_bytes_pinned, 639a969f4ccSHans van Kranenburg btrfs_space_info_show_total_bytes_pinned); 6406ab0a202SJeff Mahoney 6416ab0a202SJeff Mahoney static struct attribute *space_info_attrs[] = { 642a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, flags), 643a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, total_bytes), 644a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_used), 645a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_pinned), 646a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_reserved), 647a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_may_use), 648a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_readonly), 649a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, disk_used), 650a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, disk_total), 651a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, total_bytes_pinned), 6526ab0a202SJeff Mahoney NULL, 6536ab0a202SJeff Mahoney }; 6547c7e3014SKimberly Brown ATTRIBUTE_GROUPS(space_info); 6556ab0a202SJeff Mahoney 6566ab0a202SJeff Mahoney static void space_info_release(struct kobject *kobj) 6576ab0a202SJeff Mahoney { 6586ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj); 6596ab0a202SJeff Mahoney percpu_counter_destroy(&sinfo->total_bytes_pinned); 6606ab0a202SJeff Mahoney kfree(sinfo); 6616ab0a202SJeff Mahoney } 6626ab0a202SJeff Mahoney 66327992d01SDavid Sterba static struct kobj_type space_info_ktype = { 6646ab0a202SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 6656ab0a202SJeff Mahoney .release = space_info_release, 6667c7e3014SKimberly Brown .default_groups = space_info_groups, 6676ab0a202SJeff Mahoney }; 6686ab0a202SJeff Mahoney 6696ab0a202SJeff Mahoney static const struct attribute *allocation_attrs[] = { 670a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(allocation, global_rsv_reserved), 671a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(allocation, global_rsv_size), 6726ab0a202SJeff Mahoney NULL, 6736ab0a202SJeff Mahoney }; 6746ab0a202SJeff Mahoney 675f8ba9c11SJeff Mahoney static ssize_t btrfs_label_show(struct kobject *kobj, 676f8ba9c11SJeff Mahoney struct kobj_attribute *a, char *buf) 677f8ba9c11SJeff Mahoney { 678f8ba9c11SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 67948fcc3ffSSatoru Takeuchi char *label = fs_info->super_copy->label; 680ee17fc80SDavid Sterba ssize_t ret; 681ee17fc80SDavid Sterba 682ee17fc80SDavid Sterba spin_lock(&fs_info->super_lock); 683abdd9febSTakashi Iwai ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); 684ee17fc80SDavid Sterba spin_unlock(&fs_info->super_lock); 685ee17fc80SDavid Sterba 686ee17fc80SDavid Sterba return ret; 687f8ba9c11SJeff Mahoney } 688f8ba9c11SJeff Mahoney 689f8ba9c11SJeff Mahoney static ssize_t btrfs_label_store(struct kobject *kobj, 690f8ba9c11SJeff Mahoney struct kobj_attribute *a, 691f8ba9c11SJeff Mahoney const char *buf, size_t len) 692f8ba9c11SJeff Mahoney { 693f8ba9c11SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 69448fcc3ffSSatoru Takeuchi size_t p_len; 695f8ba9c11SJeff Mahoney 69666ac9fe7SDavid Sterba if (!fs_info) 69766ac9fe7SDavid Sterba return -EPERM; 69866ac9fe7SDavid Sterba 699bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) 70079aec2b8SAnand Jain return -EROFS; 70179aec2b8SAnand Jain 70248fcc3ffSSatoru Takeuchi /* 70348fcc3ffSSatoru Takeuchi * p_len is the len until the first occurrence of either 70448fcc3ffSSatoru Takeuchi * '\n' or '\0' 70548fcc3ffSSatoru Takeuchi */ 70648fcc3ffSSatoru Takeuchi p_len = strcspn(buf, "\n"); 70748fcc3ffSSatoru Takeuchi 70848fcc3ffSSatoru Takeuchi if (p_len >= BTRFS_LABEL_SIZE) 709f8ba9c11SJeff Mahoney return -EINVAL; 710f8ba9c11SJeff Mahoney 711a6f69dc8SDavid Sterba spin_lock(&fs_info->super_lock); 71248fcc3ffSSatoru Takeuchi memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); 71348fcc3ffSSatoru Takeuchi memcpy(fs_info->super_copy->label, buf, p_len); 714a6f69dc8SDavid Sterba spin_unlock(&fs_info->super_lock); 715f8ba9c11SJeff Mahoney 716a6f69dc8SDavid Sterba /* 717a6f69dc8SDavid Sterba * We don't want to do full transaction commit from inside sysfs 718a6f69dc8SDavid Sterba */ 719a6f69dc8SDavid Sterba btrfs_set_pending(fs_info, COMMIT); 720a6f69dc8SDavid Sterba wake_up_process(fs_info->transaction_kthread); 721a6f69dc8SDavid Sterba 722f8ba9c11SJeff Mahoney return len; 723f8ba9c11SJeff Mahoney } 724a969f4ccSHans van Kranenburg BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); 725f8ba9c11SJeff Mahoney 726df93589aSDavid Sterba static ssize_t btrfs_nodesize_show(struct kobject *kobj, 727df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 728df93589aSDavid Sterba { 729df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 730df93589aSDavid Sterba 731abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); 732df93589aSDavid Sterba } 733df93589aSDavid Sterba 734a969f4ccSHans van Kranenburg BTRFS_ATTR(, nodesize, btrfs_nodesize_show); 735df93589aSDavid Sterba 736df93589aSDavid Sterba static ssize_t btrfs_sectorsize_show(struct kobject *kobj, 737df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 738df93589aSDavid Sterba { 739df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 740df93589aSDavid Sterba 741abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 742093e037cSDavid Sterba fs_info->super_copy->sectorsize); 743df93589aSDavid Sterba } 744df93589aSDavid Sterba 745a969f4ccSHans van Kranenburg BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); 746df93589aSDavid Sterba 747df93589aSDavid Sterba static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, 748df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 749df93589aSDavid Sterba { 750df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 751df93589aSDavid Sterba 752abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize); 753df93589aSDavid Sterba } 754df93589aSDavid Sterba 755a969f4ccSHans van Kranenburg BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); 756df93589aSDavid Sterba 7572723480aSSargun Dhillon static ssize_t quota_override_show(struct kobject *kobj, 7582723480aSSargun Dhillon struct kobj_attribute *a, char *buf) 7592723480aSSargun Dhillon { 7602723480aSSargun Dhillon struct btrfs_fs_info *fs_info = to_fs_info(kobj); 7612723480aSSargun Dhillon int quota_override; 7622723480aSSargun Dhillon 7632723480aSSargun Dhillon quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 764abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override); 7652723480aSSargun Dhillon } 7662723480aSSargun Dhillon 7672723480aSSargun Dhillon static ssize_t quota_override_store(struct kobject *kobj, 7682723480aSSargun Dhillon struct kobj_attribute *a, 7692723480aSSargun Dhillon const char *buf, size_t len) 7702723480aSSargun Dhillon { 7712723480aSSargun Dhillon struct btrfs_fs_info *fs_info = to_fs_info(kobj); 7722723480aSSargun Dhillon unsigned long knob; 7732723480aSSargun Dhillon int err; 7742723480aSSargun Dhillon 7752723480aSSargun Dhillon if (!fs_info) 7762723480aSSargun Dhillon return -EPERM; 7772723480aSSargun Dhillon 7782723480aSSargun Dhillon if (!capable(CAP_SYS_RESOURCE)) 7792723480aSSargun Dhillon return -EPERM; 7802723480aSSargun Dhillon 7812723480aSSargun Dhillon err = kstrtoul(buf, 10, &knob); 7822723480aSSargun Dhillon if (err) 7832723480aSSargun Dhillon return err; 7842723480aSSargun Dhillon if (knob > 1) 7852723480aSSargun Dhillon return -EINVAL; 7862723480aSSargun Dhillon 7872723480aSSargun Dhillon if (knob) 7882723480aSSargun Dhillon set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 7892723480aSSargun Dhillon else 7902723480aSSargun Dhillon clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 7912723480aSSargun Dhillon 7922723480aSSargun Dhillon return len; 7932723480aSSargun Dhillon } 7942723480aSSargun Dhillon 795a969f4ccSHans van Kranenburg BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); 7962723480aSSargun Dhillon 79756f20f40SNikolay Borisov static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, 79856f20f40SNikolay Borisov struct kobj_attribute *a, char *buf) 79956f20f40SNikolay Borisov { 80056f20f40SNikolay Borisov struct btrfs_fs_info *fs_info = to_fs_info(kobj); 80156f20f40SNikolay Borisov 802abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%pU\n", 80356f20f40SNikolay Borisov fs_info->fs_devices->metadata_uuid); 80456f20f40SNikolay Borisov } 80556f20f40SNikolay Borisov 80656f20f40SNikolay Borisov BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); 80756f20f40SNikolay Borisov 80841e6d2a8SJohannes Thumshirn static ssize_t btrfs_checksum_show(struct kobject *kobj, 80941e6d2a8SJohannes Thumshirn struct kobj_attribute *a, char *buf) 81041e6d2a8SJohannes Thumshirn { 81141e6d2a8SJohannes Thumshirn struct btrfs_fs_info *fs_info = to_fs_info(kobj); 81241e6d2a8SJohannes Thumshirn u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); 81341e6d2a8SJohannes Thumshirn 814abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 81541e6d2a8SJohannes Thumshirn btrfs_super_csum_name(csum_type), 81641e6d2a8SJohannes Thumshirn crypto_shash_driver_name(fs_info->csum_shash)); 81741e6d2a8SJohannes Thumshirn } 81841e6d2a8SJohannes Thumshirn 81941e6d2a8SJohannes Thumshirn BTRFS_ATTR(, checksum, btrfs_checksum_show); 82041e6d2a8SJohannes Thumshirn 82166a2823cSGoldwyn Rodrigues static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, 82266a2823cSGoldwyn Rodrigues struct kobj_attribute *a, char *buf) 82366a2823cSGoldwyn Rodrigues { 82466a2823cSGoldwyn Rodrigues struct btrfs_fs_info *fs_info = to_fs_info(kobj); 82566a2823cSGoldwyn Rodrigues const char *str; 82666a2823cSGoldwyn Rodrigues 82766a2823cSGoldwyn Rodrigues switch (READ_ONCE(fs_info->exclusive_operation)) { 82866a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_NONE: 82966a2823cSGoldwyn Rodrigues str = "none\n"; 83066a2823cSGoldwyn Rodrigues break; 83166a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_BALANCE: 83266a2823cSGoldwyn Rodrigues str = "balance\n"; 83366a2823cSGoldwyn Rodrigues break; 83466a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_ADD: 83566a2823cSGoldwyn Rodrigues str = "device add\n"; 83666a2823cSGoldwyn Rodrigues break; 83766a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_REMOVE: 83866a2823cSGoldwyn Rodrigues str = "device remove\n"; 83966a2823cSGoldwyn Rodrigues break; 84066a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_REPLACE: 84166a2823cSGoldwyn Rodrigues str = "device replace\n"; 84266a2823cSGoldwyn Rodrigues break; 84366a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_RESIZE: 84466a2823cSGoldwyn Rodrigues str = "resize\n"; 84566a2823cSGoldwyn Rodrigues break; 84666a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_SWAP_ACTIVATE: 84766a2823cSGoldwyn Rodrigues str = "swap activate\n"; 84866a2823cSGoldwyn Rodrigues break; 84966a2823cSGoldwyn Rodrigues default: 85066a2823cSGoldwyn Rodrigues str = "UNKNOWN\n"; 85166a2823cSGoldwyn Rodrigues break; 85266a2823cSGoldwyn Rodrigues } 85366a2823cSGoldwyn Rodrigues return scnprintf(buf, PAGE_SIZE, "%s", str); 85466a2823cSGoldwyn Rodrigues } 85566a2823cSGoldwyn Rodrigues BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); 85666a2823cSGoldwyn Rodrigues 8570dd2906fSAnand Jain static const struct attribute *btrfs_attrs[] = { 858a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, label), 859a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, nodesize), 860a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, sectorsize), 861a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, clone_alignment), 862a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, quota_override), 86356f20f40SNikolay Borisov BTRFS_ATTR_PTR(, metadata_uuid), 86441e6d2a8SJohannes Thumshirn BTRFS_ATTR_PTR(, checksum), 86566a2823cSGoldwyn Rodrigues BTRFS_ATTR_PTR(, exclusive_operation), 866f8ba9c11SJeff Mahoney NULL, 867f8ba9c11SJeff Mahoney }; 868f8ba9c11SJeff Mahoney 869c1b7e474SAnand Jain static void btrfs_release_fsid_kobj(struct kobject *kobj) 870510d7360SJeff Mahoney { 8712e7910d6SAnand Jain struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); 872248d200dSAnand Jain 873c1b7e474SAnand Jain memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); 8742e7910d6SAnand Jain complete(&fs_devs->kobj_unregister); 875510d7360SJeff Mahoney } 876510d7360SJeff Mahoney 877510d7360SJeff Mahoney static struct kobj_type btrfs_ktype = { 878510d7360SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 879c1b7e474SAnand Jain .release = btrfs_release_fsid_kobj, 880510d7360SJeff Mahoney }; 881510d7360SJeff Mahoney 8822e7910d6SAnand Jain static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) 8832e7910d6SAnand Jain { 8842e7910d6SAnand Jain if (kobj->ktype != &btrfs_ktype) 8852e7910d6SAnand Jain return NULL; 886c1b7e474SAnand Jain return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); 8872e7910d6SAnand Jain } 8882e7910d6SAnand Jain 889510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) 890510d7360SJeff Mahoney { 891510d7360SJeff Mahoney if (kobj->ktype != &btrfs_ktype) 892510d7360SJeff Mahoney return NULL; 8932e7910d6SAnand Jain return to_fs_devs(kobj)->fs_info; 894510d7360SJeff Mahoney } 89558176a96SJosef Bacik 896e453d989SJeff Mahoney #define NUM_FEATURE_BITS 64 8976c52157fSTomohiro Misono #define BTRFS_FEATURE_NAME_MAX 13 8986c52157fSTomohiro Misono static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; 8996c52157fSTomohiro Misono static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; 900e453d989SJeff Mahoney 9016c52157fSTomohiro Misono static const u64 supported_feature_masks[FEAT_MAX] = { 902e453d989SJeff Mahoney [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, 903e453d989SJeff Mahoney [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, 904e453d989SJeff Mahoney [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, 905e453d989SJeff Mahoney }; 906e453d989SJeff Mahoney 907e453d989SJeff Mahoney static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) 9085ac1d209SJeff Mahoney { 909e453d989SJeff Mahoney int set; 910e453d989SJeff Mahoney 911e453d989SJeff Mahoney for (set = 0; set < FEAT_MAX; set++) { 912e453d989SJeff Mahoney int i; 913e453d989SJeff Mahoney struct attribute *attrs[2]; 914e453d989SJeff Mahoney struct attribute_group agroup = { 915e453d989SJeff Mahoney .name = "features", 916e453d989SJeff Mahoney .attrs = attrs, 917e453d989SJeff Mahoney }; 918e453d989SJeff Mahoney u64 features = get_features(fs_info, set); 919e453d989SJeff Mahoney features &= ~supported_feature_masks[set]; 920e453d989SJeff Mahoney 921e453d989SJeff Mahoney if (!features) 922e453d989SJeff Mahoney continue; 923e453d989SJeff Mahoney 924e453d989SJeff Mahoney attrs[1] = NULL; 925e453d989SJeff Mahoney for (i = 0; i < NUM_FEATURE_BITS; i++) { 926e453d989SJeff Mahoney struct btrfs_feature_attr *fa; 927e453d989SJeff Mahoney 928e453d989SJeff Mahoney if (!(features & (1ULL << i))) 929e453d989SJeff Mahoney continue; 930e453d989SJeff Mahoney 931e453d989SJeff Mahoney fa = &btrfs_feature_attrs[set][i]; 932e453d989SJeff Mahoney attrs[0] = &fa->kobj_attr.attr; 933e453d989SJeff Mahoney if (add) { 934e453d989SJeff Mahoney int ret; 935c1b7e474SAnand Jain ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, 936e453d989SJeff Mahoney &agroup); 937e453d989SJeff Mahoney if (ret) 938e453d989SJeff Mahoney return ret; 939e453d989SJeff Mahoney } else 940c1b7e474SAnand Jain sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, 941e453d989SJeff Mahoney &agroup); 942e453d989SJeff Mahoney } 943e453d989SJeff Mahoney 944e453d989SJeff Mahoney } 945e453d989SJeff Mahoney return 0; 946e453d989SJeff Mahoney } 947e453d989SJeff Mahoney 9482e3e1281SAnand Jain static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 949e453d989SJeff Mahoney { 950a013d141SAnand Jain if (fs_devs->devinfo_kobj) { 951a013d141SAnand Jain kobject_del(fs_devs->devinfo_kobj); 952a013d141SAnand Jain kobject_put(fs_devs->devinfo_kobj); 953a013d141SAnand Jain fs_devs->devinfo_kobj = NULL; 954a013d141SAnand Jain } 955a013d141SAnand Jain 956b5501504SAnand Jain if (fs_devs->devices_kobj) { 957b5501504SAnand Jain kobject_del(fs_devs->devices_kobj); 958b5501504SAnand Jain kobject_put(fs_devs->devices_kobj); 959b5501504SAnand Jain fs_devs->devices_kobj = NULL; 960aaf13305SAnand Jain } 961aaf13305SAnand Jain 962c1b7e474SAnand Jain if (fs_devs->fsid_kobj.state_initialized) { 963c1b7e474SAnand Jain kobject_del(&fs_devs->fsid_kobj); 964c1b7e474SAnand Jain kobject_put(&fs_devs->fsid_kobj); 9652e7910d6SAnand Jain wait_for_completion(&fs_devs->kobj_unregister); 9665ac1d209SJeff Mahoney } 967f90fc547SAnand Jain } 9685ac1d209SJeff Mahoney 9692e3e1281SAnand Jain /* when fs_devs is NULL it will remove all fsid kobject */ 9701d1c1be3SAnand Jain void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 9712e3e1281SAnand Jain { 9722e3e1281SAnand Jain struct list_head *fs_uuids = btrfs_get_fs_uuids(); 9732e3e1281SAnand Jain 9742e3e1281SAnand Jain if (fs_devs) { 9752e3e1281SAnand Jain __btrfs_sysfs_remove_fsid(fs_devs); 9762e3e1281SAnand Jain return; 9772e3e1281SAnand Jain } 9782e3e1281SAnand Jain 979c4babc5eSAnand Jain list_for_each_entry(fs_devs, fs_uuids, fs_list) { 9802e3e1281SAnand Jain __btrfs_sysfs_remove_fsid(fs_devs); 9812e3e1281SAnand Jain } 9822e3e1281SAnand Jain } 9832e3e1281SAnand Jain 98453f8a74cSAnand Jain static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) 98553f8a74cSAnand Jain { 98653f8a74cSAnand Jain struct btrfs_device *device; 98730b0e4e0SAnand Jain struct btrfs_fs_devices *seed; 98853f8a74cSAnand Jain 98953f8a74cSAnand Jain list_for_each_entry(device, &fs_devices->devices, dev_list) 99053f8a74cSAnand Jain btrfs_sysfs_remove_device(device); 99130b0e4e0SAnand Jain 99230b0e4e0SAnand Jain list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 99330b0e4e0SAnand Jain list_for_each_entry(device, &seed->devices, dev_list) 99430b0e4e0SAnand Jain btrfs_sysfs_remove_device(device); 99530b0e4e0SAnand Jain } 99653f8a74cSAnand Jain } 99753f8a74cSAnand Jain 9986618a59bSAnand Jain void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) 999e453d989SJeff Mahoney { 10003092c68fSNikolay Borisov struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 10013092c68fSNikolay Borisov 10023092c68fSNikolay Borisov sysfs_remove_link(fsid_kobj, "bdi"); 10033092c68fSNikolay Borisov 1004e453d989SJeff Mahoney if (fs_info->space_info_kobj) { 1005e453d989SJeff Mahoney sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); 1006e453d989SJeff Mahoney kobject_del(fs_info->space_info_kobj); 1007e453d989SJeff Mahoney kobject_put(fs_info->space_info_kobj); 1008e453d989SJeff Mahoney } 100971e8978eSDennis Zhou #ifdef CONFIG_BTRFS_DEBUG 1010e4faab84SDennis Zhou if (fs_info->discard_debug_kobj) { 1011e4faab84SDennis Zhou sysfs_remove_files(fs_info->discard_debug_kobj, 1012e4faab84SDennis Zhou discard_debug_attrs); 1013e4faab84SDennis Zhou kobject_del(fs_info->discard_debug_kobj); 1014e4faab84SDennis Zhou kobject_put(fs_info->discard_debug_kobj); 1015e4faab84SDennis Zhou } 101693945cb4SDennis Zhou if (fs_info->debug_kobj) { 101793945cb4SDennis Zhou sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 101893945cb4SDennis Zhou kobject_del(fs_info->debug_kobj); 101993945cb4SDennis Zhou kobject_put(fs_info->debug_kobj); 102093945cb4SDennis Zhou } 102171e8978eSDennis Zhou #endif 1022e453d989SJeff Mahoney addrm_unknown_feature_attrs(fs_info, false); 10233092c68fSNikolay Borisov sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 10243092c68fSNikolay Borisov sysfs_remove_files(fsid_kobj, btrfs_attrs); 102553f8a74cSAnand Jain btrfs_sysfs_remove_fs_devices(fs_info->fs_devices); 1026e453d989SJeff Mahoney } 1027e453d989SJeff Mahoney 1028f10152bcSDavid Sterba static const char * const btrfs_feature_set_names[FEAT_MAX] = { 102979da4fa4SJeff Mahoney [FEAT_COMPAT] = "compat", 103079da4fa4SJeff Mahoney [FEAT_COMPAT_RO] = "compat_ro", 103179da4fa4SJeff Mahoney [FEAT_INCOMPAT] = "incompat", 103279da4fa4SJeff Mahoney }; 103379da4fa4SJeff Mahoney 10349e6df7ceSDavid Sterba const char *btrfs_feature_set_name(enum btrfs_feature_set set) 1035f10152bcSDavid Sterba { 1036f10152bcSDavid Sterba return btrfs_feature_set_names[set]; 1037f10152bcSDavid Sterba } 1038f10152bcSDavid Sterba 10393b02a68aSJeff Mahoney char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) 10403b02a68aSJeff Mahoney { 10413b02a68aSJeff Mahoney size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ 10423b02a68aSJeff Mahoney int len = 0; 10433b02a68aSJeff Mahoney int i; 10443b02a68aSJeff Mahoney char *str; 10453b02a68aSJeff Mahoney 10463b02a68aSJeff Mahoney str = kmalloc(bufsize, GFP_KERNEL); 10473b02a68aSJeff Mahoney if (!str) 10483b02a68aSJeff Mahoney return str; 10493b02a68aSJeff Mahoney 10503b02a68aSJeff Mahoney for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 10513b02a68aSJeff Mahoney const char *name; 10523b02a68aSJeff Mahoney 10533b02a68aSJeff Mahoney if (!(flags & (1ULL << i))) 10543b02a68aSJeff Mahoney continue; 10553b02a68aSJeff Mahoney 10563b02a68aSJeff Mahoney name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; 1057abdd9febSTakashi Iwai len += scnprintf(str + len, bufsize - len, "%s%s", 10583b02a68aSJeff Mahoney len ? "," : "", name); 10593b02a68aSJeff Mahoney } 10603b02a68aSJeff Mahoney 10613b02a68aSJeff Mahoney return str; 10623b02a68aSJeff Mahoney } 10633b02a68aSJeff Mahoney 106479da4fa4SJeff Mahoney static void init_feature_attrs(void) 106579da4fa4SJeff Mahoney { 106679da4fa4SJeff Mahoney struct btrfs_feature_attr *fa; 106779da4fa4SJeff Mahoney int set, i; 106879da4fa4SJeff Mahoney 106979da4fa4SJeff Mahoney BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != 107079da4fa4SJeff Mahoney ARRAY_SIZE(btrfs_feature_attrs)); 107179da4fa4SJeff Mahoney BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != 107279da4fa4SJeff Mahoney ARRAY_SIZE(btrfs_feature_attrs[0])); 107379da4fa4SJeff Mahoney 10743b02a68aSJeff Mahoney memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); 10753b02a68aSJeff Mahoney memset(btrfs_unknown_feature_names, 0, 10763b02a68aSJeff Mahoney sizeof(btrfs_unknown_feature_names)); 10773b02a68aSJeff Mahoney 107879da4fa4SJeff Mahoney for (i = 0; btrfs_supported_feature_attrs[i]; i++) { 107979da4fa4SJeff Mahoney struct btrfs_feature_attr *sfa; 108079da4fa4SJeff Mahoney struct attribute *a = btrfs_supported_feature_attrs[i]; 10813b02a68aSJeff Mahoney int bit; 108279da4fa4SJeff Mahoney sfa = attr_to_btrfs_feature_attr(a); 10833b02a68aSJeff Mahoney bit = ilog2(sfa->feature_bit); 10843b02a68aSJeff Mahoney fa = &btrfs_feature_attrs[sfa->feature_set][bit]; 108579da4fa4SJeff Mahoney 108679da4fa4SJeff Mahoney fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; 108779da4fa4SJeff Mahoney } 108879da4fa4SJeff Mahoney 108979da4fa4SJeff Mahoney for (set = 0; set < FEAT_MAX; set++) { 109079da4fa4SJeff Mahoney for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 109179da4fa4SJeff Mahoney char *name = btrfs_unknown_feature_names[set][i]; 109279da4fa4SJeff Mahoney fa = &btrfs_feature_attrs[set][i]; 109379da4fa4SJeff Mahoney 109479da4fa4SJeff Mahoney if (fa->kobj_attr.attr.name) 109579da4fa4SJeff Mahoney continue; 109679da4fa4SJeff Mahoney 10976c52157fSTomohiro Misono snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", 109879da4fa4SJeff Mahoney btrfs_feature_set_names[set], i); 109979da4fa4SJeff Mahoney 110079da4fa4SJeff Mahoney fa->kobj_attr.attr.name = name; 110179da4fa4SJeff Mahoney fa->kobj_attr.attr.mode = S_IRUGO; 110279da4fa4SJeff Mahoney fa->feature_set = set; 110379da4fa4SJeff Mahoney fa->feature_bit = 1ULL << i; 110479da4fa4SJeff Mahoney } 110579da4fa4SJeff Mahoney } 110679da4fa4SJeff Mahoney } 110779da4fa4SJeff Mahoney 110832a9991fSDavid Sterba /* 110932a9991fSDavid Sterba * Create a sysfs entry for a given block group type at path 111032a9991fSDavid Sterba * /sys/fs/btrfs/UUID/allocation/data/TYPE 111132a9991fSDavid Sterba */ 111232da5386SDavid Sterba void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) 111332a9991fSDavid Sterba { 111432a9991fSDavid Sterba struct btrfs_fs_info *fs_info = cache->fs_info; 111532a9991fSDavid Sterba struct btrfs_space_info *space_info = cache->space_info; 111632a9991fSDavid Sterba struct raid_kobject *rkobj; 111732a9991fSDavid Sterba const int index = btrfs_bg_flags_to_raid_index(cache->flags); 111832a9991fSDavid Sterba unsigned int nofs_flag; 111932a9991fSDavid Sterba int ret; 112032a9991fSDavid Sterba 112132a9991fSDavid Sterba /* 112232a9991fSDavid Sterba * Setup a NOFS context because kobject_add(), deep in its call chain, 112332a9991fSDavid Sterba * does GFP_KERNEL allocations, and we are often called in a context 112432a9991fSDavid Sterba * where if reclaim is triggered we can deadlock (we are either holding 112532a9991fSDavid Sterba * a transaction handle or some lock required for a transaction 112632a9991fSDavid Sterba * commit). 112732a9991fSDavid Sterba */ 112832a9991fSDavid Sterba nofs_flag = memalloc_nofs_save(); 112932a9991fSDavid Sterba 113032a9991fSDavid Sterba rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); 113132a9991fSDavid Sterba if (!rkobj) { 113232a9991fSDavid Sterba memalloc_nofs_restore(nofs_flag); 113332a9991fSDavid Sterba btrfs_warn(cache->fs_info, 113432a9991fSDavid Sterba "couldn't alloc memory for raid level kobject"); 113532a9991fSDavid Sterba return; 113632a9991fSDavid Sterba } 113732a9991fSDavid Sterba 113832a9991fSDavid Sterba rkobj->flags = cache->flags; 113932a9991fSDavid Sterba kobject_init(&rkobj->kobj, &btrfs_raid_ktype); 114032a9991fSDavid Sterba ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", 114132a9991fSDavid Sterba btrfs_bg_type_to_raid_name(rkobj->flags)); 114232a9991fSDavid Sterba memalloc_nofs_restore(nofs_flag); 114332a9991fSDavid Sterba if (ret) { 114432a9991fSDavid Sterba kobject_put(&rkobj->kobj); 114532a9991fSDavid Sterba btrfs_warn(fs_info, 114632a9991fSDavid Sterba "failed to add kobject for block cache, ignoring"); 114732a9991fSDavid Sterba return; 114832a9991fSDavid Sterba } 114932a9991fSDavid Sterba 115032a9991fSDavid Sterba space_info->block_group_kobjs[index] = &rkobj->kobj; 115132a9991fSDavid Sterba } 115232a9991fSDavid Sterba 1153b5865babSDavid Sterba /* 1154b5865babSDavid Sterba * Remove sysfs directories for all block group types of a given space info and 1155b5865babSDavid Sterba * the space info as well 1156b5865babSDavid Sterba */ 1157b5865babSDavid Sterba void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) 1158b5865babSDavid Sterba { 1159b5865babSDavid Sterba int i; 1160b5865babSDavid Sterba 1161b5865babSDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 1162b5865babSDavid Sterba struct kobject *kobj; 1163b5865babSDavid Sterba 1164b5865babSDavid Sterba kobj = space_info->block_group_kobjs[i]; 1165b5865babSDavid Sterba space_info->block_group_kobjs[i] = NULL; 1166b5865babSDavid Sterba if (kobj) { 1167b5865babSDavid Sterba kobject_del(kobj); 1168b5865babSDavid Sterba kobject_put(kobj); 1169b5865babSDavid Sterba } 1170b5865babSDavid Sterba } 1171b5865babSDavid Sterba kobject_del(&space_info->kobj); 1172b5865babSDavid Sterba kobject_put(&space_info->kobj); 1173b5865babSDavid Sterba } 1174b5865babSDavid Sterba 1175b882327aSDavid Sterba static const char *alloc_name(u64 flags) 1176b882327aSDavid Sterba { 1177b882327aSDavid Sterba switch (flags) { 1178b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: 1179b882327aSDavid Sterba return "mixed"; 1180b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_METADATA: 1181b882327aSDavid Sterba return "metadata"; 1182b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_DATA: 1183b882327aSDavid Sterba return "data"; 1184b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_SYSTEM: 1185b882327aSDavid Sterba return "system"; 1186b882327aSDavid Sterba default: 1187b882327aSDavid Sterba WARN_ON(1); 1188b882327aSDavid Sterba return "invalid-combination"; 1189b882327aSDavid Sterba }; 1190b882327aSDavid Sterba } 1191b882327aSDavid Sterba 1192b882327aSDavid Sterba /* 1193b882327aSDavid Sterba * Create a sysfs entry for a space info type at path 1194b882327aSDavid Sterba * /sys/fs/btrfs/UUID/allocation/TYPE 1195b882327aSDavid Sterba */ 1196b882327aSDavid Sterba int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, 1197b882327aSDavid Sterba struct btrfs_space_info *space_info) 1198b882327aSDavid Sterba { 1199b882327aSDavid Sterba int ret; 1200b882327aSDavid Sterba 1201b882327aSDavid Sterba ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, 1202b882327aSDavid Sterba fs_info->space_info_kobj, "%s", 1203b882327aSDavid Sterba alloc_name(space_info->flags)); 1204b882327aSDavid Sterba if (ret) { 1205b882327aSDavid Sterba kobject_put(&space_info->kobj); 1206b882327aSDavid Sterba return ret; 1207b882327aSDavid Sterba } 1208b882327aSDavid Sterba 1209b882327aSDavid Sterba return 0; 1210b882327aSDavid Sterba } 1211b882327aSDavid Sterba 121253f8a74cSAnand Jain void btrfs_sysfs_remove_device(struct btrfs_device *device) 121399994cdeSAnand Jain { 121499994cdeSAnand Jain struct hd_struct *disk; 121599994cdeSAnand Jain struct kobject *disk_kobj; 1216985e233eSAnand Jain struct kobject *devices_kobj; 121799994cdeSAnand Jain 1218985e233eSAnand Jain /* 1219985e233eSAnand Jain * Seed fs_devices devices_kobj aren't used, fetch kobject from the 1220985e233eSAnand Jain * fs_info::fs_devices. 1221985e233eSAnand Jain */ 1222985e233eSAnand Jain devices_kobj = device->fs_info->fs_devices->devices_kobj; 1223985e233eSAnand Jain ASSERT(devices_kobj); 122499994cdeSAnand Jain 1225985e233eSAnand Jain if (device->bdev) { 1226985e233eSAnand Jain disk = device->bdev->bd_part; 122799994cdeSAnand Jain disk_kobj = &part_to_dev(disk)->kobj; 1228985e233eSAnand Jain sysfs_remove_link(devices_kobj, disk_kobj->name); 122999994cdeSAnand Jain } 123099994cdeSAnand Jain 1231985e233eSAnand Jain if (device->devid_kobj.state_initialized) { 1232985e233eSAnand Jain kobject_del(&device->devid_kobj); 1233985e233eSAnand Jain kobject_put(&device->devid_kobj); 1234985e233eSAnand Jain wait_for_completion(&device->kobj_unregister); 1235985e233eSAnand Jain } 1236b5ddcffaSAnand Jain } 1237668e48afSAnand Jain 1238668e48afSAnand Jain static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, 1239668e48afSAnand Jain struct kobj_attribute *a, 1240668e48afSAnand Jain char *buf) 1241668e48afSAnand Jain { 1242668e48afSAnand Jain int val; 1243668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1244668e48afSAnand Jain devid_kobj); 1245668e48afSAnand Jain 1246668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); 1247668e48afSAnand Jain 1248abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1249668e48afSAnand Jain } 1250668e48afSAnand Jain BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); 1251668e48afSAnand Jain 125225864778SAnand Jain static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, 1253668e48afSAnand Jain struct kobj_attribute *a, char *buf) 1254668e48afSAnand Jain { 1255668e48afSAnand Jain int val; 1256668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1257668e48afSAnand Jain devid_kobj); 1258668e48afSAnand Jain 1259668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); 1260668e48afSAnand Jain 1261abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1262668e48afSAnand Jain } 126325864778SAnand Jain BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); 1264668e48afSAnand Jain 1265668e48afSAnand Jain static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, 1266668e48afSAnand Jain struct kobj_attribute *a, 1267668e48afSAnand Jain char *buf) 1268668e48afSAnand Jain { 1269668e48afSAnand Jain int val; 1270668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1271668e48afSAnand Jain devid_kobj); 1272668e48afSAnand Jain 1273668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); 1274668e48afSAnand Jain 1275abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1276668e48afSAnand Jain } 1277668e48afSAnand Jain BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); 1278668e48afSAnand Jain 1279668e48afSAnand Jain static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, 1280668e48afSAnand Jain struct kobj_attribute *a, char *buf) 1281668e48afSAnand Jain { 1282668e48afSAnand Jain int val; 1283668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1284668e48afSAnand Jain devid_kobj); 1285668e48afSAnand Jain 1286668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); 1287668e48afSAnand Jain 1288abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1289668e48afSAnand Jain } 1290668e48afSAnand Jain BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); 1291668e48afSAnand Jain 1292668e48afSAnand Jain static struct attribute *devid_attrs[] = { 1293668e48afSAnand Jain BTRFS_ATTR_PTR(devid, in_fs_metadata), 1294668e48afSAnand Jain BTRFS_ATTR_PTR(devid, missing), 1295668e48afSAnand Jain BTRFS_ATTR_PTR(devid, replace_target), 1296668e48afSAnand Jain BTRFS_ATTR_PTR(devid, writeable), 1297668e48afSAnand Jain NULL 1298668e48afSAnand Jain }; 1299668e48afSAnand Jain ATTRIBUTE_GROUPS(devid); 1300668e48afSAnand Jain 1301668e48afSAnand Jain static void btrfs_release_devid_kobj(struct kobject *kobj) 1302668e48afSAnand Jain { 1303668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1304668e48afSAnand Jain devid_kobj); 1305668e48afSAnand Jain 1306668e48afSAnand Jain memset(&device->devid_kobj, 0, sizeof(struct kobject)); 1307668e48afSAnand Jain complete(&device->kobj_unregister); 1308668e48afSAnand Jain } 1309668e48afSAnand Jain 1310668e48afSAnand Jain static struct kobj_type devid_ktype = { 1311668e48afSAnand Jain .sysfs_ops = &kobj_sysfs_ops, 1312668e48afSAnand Jain .default_groups = devid_groups, 1313668e48afSAnand Jain .release = btrfs_release_devid_kobj, 1314668e48afSAnand Jain }; 1315668e48afSAnand Jain 1316cd36da2eSAnand Jain int btrfs_sysfs_add_device(struct btrfs_device *device) 131729e5be24SJeff Mahoney { 1318178a16c9SAnand Jain int ret; 1319a47bd78dSJosef Bacik unsigned int nofs_flag; 1320178a16c9SAnand Jain struct kobject *devices_kobj; 1321178a16c9SAnand Jain struct kobject *devinfo_kobj; 1322178a16c9SAnand Jain 1323178a16c9SAnand Jain /* 1324178a16c9SAnand Jain * Make sure we use the fs_info::fs_devices to fetch the kobjects even 1325178a16c9SAnand Jain * for the seed fs_devices 1326178a16c9SAnand Jain */ 1327178a16c9SAnand Jain devices_kobj = device->fs_info->fs_devices->devices_kobj; 1328178a16c9SAnand Jain devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; 1329178a16c9SAnand Jain ASSERT(devices_kobj); 1330178a16c9SAnand Jain ASSERT(devinfo_kobj); 133129e5be24SJeff Mahoney 1332a47bd78dSJosef Bacik nofs_flag = memalloc_nofs_save(); 1333f085381eSAnand Jain 1334178a16c9SAnand Jain if (device->bdev) { 1335668e48afSAnand Jain struct hd_struct *disk; 1336668e48afSAnand Jain struct kobject *disk_kobj; 1337668e48afSAnand Jain 1338178a16c9SAnand Jain disk = device->bdev->bd_part; 1339f085381eSAnand Jain disk_kobj = &part_to_dev(disk)->kobj; 134029e5be24SJeff Mahoney 1341178a16c9SAnand Jain ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name); 1342178a16c9SAnand Jain if (ret) { 1343178a16c9SAnand Jain btrfs_warn(device->fs_info, 1344178a16c9SAnand Jain "creating sysfs device link for devid %llu failed: %d", 1345178a16c9SAnand Jain device->devid, ret); 1346178a16c9SAnand Jain goto out; 1347178a16c9SAnand Jain } 134829e5be24SJeff Mahoney } 134929e5be24SJeff Mahoney 1350178a16c9SAnand Jain init_completion(&device->kobj_unregister); 1351178a16c9SAnand Jain ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype, 1352178a16c9SAnand Jain devinfo_kobj, "%llu", device->devid); 1353178a16c9SAnand Jain if (ret) { 1354178a16c9SAnand Jain kobject_put(&device->devid_kobj); 1355178a16c9SAnand Jain btrfs_warn(device->fs_info, 1356178a16c9SAnand Jain "devinfo init for devid %llu failed: %d", 1357178a16c9SAnand Jain device->devid, ret); 1358668e48afSAnand Jain } 1359178a16c9SAnand Jain 1360178a16c9SAnand Jain out: 1361a47bd78dSJosef Bacik memalloc_nofs_restore(nofs_flag); 1362178a16c9SAnand Jain return ret; 1363178a16c9SAnand Jain } 1364668e48afSAnand Jain 1365cd36da2eSAnand Jain static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) 1366178a16c9SAnand Jain { 1367178a16c9SAnand Jain int ret; 1368cd36da2eSAnand Jain struct btrfs_device *device; 136930b0e4e0SAnand Jain struct btrfs_fs_devices *seed; 1370178a16c9SAnand Jain 1371178a16c9SAnand Jain list_for_each_entry(device, &fs_devices->devices, dev_list) { 1372178a16c9SAnand Jain ret = btrfs_sysfs_add_device(device); 1373178a16c9SAnand Jain if (ret) 13747ad3912aSAnand Jain goto fail; 1375178a16c9SAnand Jain } 1376178a16c9SAnand Jain 137730b0e4e0SAnand Jain list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 137830b0e4e0SAnand Jain list_for_each_entry(device, &seed->devices, dev_list) { 137930b0e4e0SAnand Jain ret = btrfs_sysfs_add_device(device); 138030b0e4e0SAnand Jain if (ret) 13817ad3912aSAnand Jain goto fail; 138230b0e4e0SAnand Jain } 138330b0e4e0SAnand Jain } 138430b0e4e0SAnand Jain 1385178a16c9SAnand Jain return 0; 13867ad3912aSAnand Jain 13877ad3912aSAnand Jain fail: 13887ad3912aSAnand Jain btrfs_sysfs_remove_fs_devices(fs_devices); 13897ad3912aSAnand Jain return ret; 139029e5be24SJeff Mahoney } 139129e5be24SJeff Mahoney 13925b28692eSDavid Sterba void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) 13935b28692eSDavid Sterba { 13945b28692eSDavid Sterba int ret; 13955b28692eSDavid Sterba 13965b28692eSDavid Sterba ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); 13975b28692eSDavid Sterba if (ret) 13985b28692eSDavid Sterba pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", 13995b28692eSDavid Sterba action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), 14005b28692eSDavid Sterba &disk_to_dev(bdev->bd_disk)->kobj); 14015b28692eSDavid Sterba } 14025b28692eSDavid Sterba 14038e560081SNikolay Borisov void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) 14048e560081SNikolay Borisov 1405f93c3997SDavid Sterba { 1406f93c3997SDavid Sterba char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 1407f93c3997SDavid Sterba 1408f93c3997SDavid Sterba /* 1409f93c3997SDavid Sterba * Sprouting changes fsid of the mounted filesystem, rename the fsid 1410f93c3997SDavid Sterba * directory 1411f93c3997SDavid Sterba */ 14128e560081SNikolay Borisov snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); 1413f93c3997SDavid Sterba if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) 1414f93c3997SDavid Sterba btrfs_warn(fs_devices->fs_info, 1415f93c3997SDavid Sterba "sysfs: failed to create fsid for sprout"); 1416f93c3997SDavid Sterba } 1417f93c3997SDavid Sterba 1418668e48afSAnand Jain void btrfs_sysfs_update_devid(struct btrfs_device *device) 1419668e48afSAnand Jain { 1420668e48afSAnand Jain char tmp[24]; 1421668e48afSAnand Jain 1422668e48afSAnand Jain snprintf(tmp, sizeof(tmp), "%llu", device->devid); 1423668e48afSAnand Jain 1424668e48afSAnand Jain if (kobject_rename(&device->devid_kobj, tmp)) 1425668e48afSAnand Jain btrfs_warn(device->fs_devices->fs_info, 1426668e48afSAnand Jain "sysfs: failed to update devid for %llu", 1427668e48afSAnand Jain device->devid); 1428668e48afSAnand Jain } 1429668e48afSAnand Jain 1430510d7360SJeff Mahoney /* /sys/fs/btrfs/ entry */ 1431510d7360SJeff Mahoney static struct kset *btrfs_kset; 1432510d7360SJeff Mahoney 143372059215SAnand Jain /* 1434c6761a9eSAnand Jain * Creates: 1435c6761a9eSAnand Jain * /sys/fs/btrfs/UUID 1436c6761a9eSAnand Jain * 143772059215SAnand Jain * Can be called by the device discovery thread. 143872059215SAnand Jain */ 1439c6761a9eSAnand Jain int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) 14405ac1d209SJeff Mahoney { 14415ac1d209SJeff Mahoney int error; 14425ac1d209SJeff Mahoney 14432e7910d6SAnand Jain init_completion(&fs_devs->kobj_unregister); 1444c1b7e474SAnand Jain fs_devs->fsid_kobj.kset = btrfs_kset; 1445c6761a9eSAnand Jain error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, 1446c6761a9eSAnand Jain "%pU", fs_devs->fsid); 1447e3277335STobin C. Harding if (error) { 1448e3277335STobin C. Harding kobject_put(&fs_devs->fsid_kobj); 144972059215SAnand Jain return error; 145072059215SAnand Jain } 145172059215SAnand Jain 1452bc036bb3SAnand Jain fs_devs->devices_kobj = kobject_create_and_add("devices", 1453bc036bb3SAnand Jain &fs_devs->fsid_kobj); 1454bc036bb3SAnand Jain if (!fs_devs->devices_kobj) { 1455bc036bb3SAnand Jain btrfs_err(fs_devs->fs_info, 1456bc036bb3SAnand Jain "failed to init sysfs device interface"); 14571f6087e6SAnand Jain btrfs_sysfs_remove_fsid(fs_devs); 1458bc036bb3SAnand Jain return -ENOMEM; 1459bc036bb3SAnand Jain } 1460bc036bb3SAnand Jain 1461a013d141SAnand Jain fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", 1462a013d141SAnand Jain &fs_devs->fsid_kobj); 1463a013d141SAnand Jain if (!fs_devs->devinfo_kobj) { 1464a013d141SAnand Jain btrfs_err(fs_devs->fs_info, 1465a013d141SAnand Jain "failed to init sysfs devinfo kobject"); 1466a013d141SAnand Jain btrfs_sysfs_remove_fsid(fs_devs); 1467a013d141SAnand Jain return -ENOMEM; 1468a013d141SAnand Jain } 1469a013d141SAnand Jain 1470e3277335STobin C. Harding return 0; 1471e3277335STobin C. Harding } 1472e3277335STobin C. Harding 147396f3136eSAnand Jain int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) 147472059215SAnand Jain { 147572059215SAnand Jain int error; 14762e7910d6SAnand Jain struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 1477c1b7e474SAnand Jain struct kobject *fsid_kobj = &fs_devs->fsid_kobj; 147872059215SAnand Jain 1479cd36da2eSAnand Jain error = btrfs_sysfs_add_fs_devices(fs_devs); 1480e453d989SJeff Mahoney if (error) 1481e453d989SJeff Mahoney return error; 1482510d7360SJeff Mahoney 1483c1b7e474SAnand Jain error = sysfs_create_files(fsid_kobj, btrfs_attrs); 1484e453d989SJeff Mahoney if (error) { 148553f8a74cSAnand Jain btrfs_sysfs_remove_fs_devices(fs_devs); 1486e453d989SJeff Mahoney return error; 1487e453d989SJeff Mahoney } 148879da4fa4SJeff Mahoney 1489c1b7e474SAnand Jain error = sysfs_create_group(fsid_kobj, 14900dd2906fSAnand Jain &btrfs_feature_attr_group); 14910dd2906fSAnand Jain if (error) 14920dd2906fSAnand Jain goto failure; 14930dd2906fSAnand Jain 14946e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 149593945cb4SDennis Zhou fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); 149693945cb4SDennis Zhou if (!fs_info->debug_kobj) { 149793945cb4SDennis Zhou error = -ENOMEM; 149893945cb4SDennis Zhou goto failure; 149993945cb4SDennis Zhou } 150093945cb4SDennis Zhou 150193945cb4SDennis Zhou error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 15026e369febSDavid Sterba if (error) 15036e369febSDavid Sterba goto failure; 1504e4faab84SDennis Zhou 1505e4faab84SDennis Zhou /* Discard directory */ 1506e4faab84SDennis Zhou fs_info->discard_debug_kobj = kobject_create_and_add("discard", 1507e4faab84SDennis Zhou fs_info->debug_kobj); 1508e4faab84SDennis Zhou if (!fs_info->discard_debug_kobj) { 1509e4faab84SDennis Zhou error = -ENOMEM; 1510e4faab84SDennis Zhou goto failure; 1511e4faab84SDennis Zhou } 1512e4faab84SDennis Zhou 1513e4faab84SDennis Zhou error = sysfs_create_files(fs_info->discard_debug_kobj, 1514e4faab84SDennis Zhou discard_debug_attrs); 1515e4faab84SDennis Zhou if (error) 1516e4faab84SDennis Zhou goto failure; 15176e369febSDavid Sterba #endif 15186e369febSDavid Sterba 1519e453d989SJeff Mahoney error = addrm_unknown_feature_attrs(fs_info, true); 152079da4fa4SJeff Mahoney if (error) 152179da4fa4SJeff Mahoney goto failure; 152279da4fa4SJeff Mahoney 15233092c68fSNikolay Borisov error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); 15243092c68fSNikolay Borisov if (error) 15253092c68fSNikolay Borisov goto failure; 15263092c68fSNikolay Borisov 15276ab0a202SJeff Mahoney fs_info->space_info_kobj = kobject_create_and_add("allocation", 1528c1b7e474SAnand Jain fsid_kobj); 15296ab0a202SJeff Mahoney if (!fs_info->space_info_kobj) { 15306ab0a202SJeff Mahoney error = -ENOMEM; 15316ab0a202SJeff Mahoney goto failure; 15326ab0a202SJeff Mahoney } 15336ab0a202SJeff Mahoney 15346ab0a202SJeff Mahoney error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); 15356ab0a202SJeff Mahoney if (error) 15366ab0a202SJeff Mahoney goto failure; 15376ab0a202SJeff Mahoney 153879da4fa4SJeff Mahoney return 0; 153979da4fa4SJeff Mahoney failure: 15406618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 15415ac1d209SJeff Mahoney return error; 15425ac1d209SJeff Mahoney } 15435ac1d209SJeff Mahoney 154449e5fb46SQu Wenruo static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) 154549e5fb46SQu Wenruo { 154649e5fb46SQu Wenruo return to_fs_info(kobj->parent->parent); 154749e5fb46SQu Wenruo } 154849e5fb46SQu Wenruo 154949e5fb46SQu Wenruo #define QGROUP_ATTR(_member, _show_name) \ 155049e5fb46SQu Wenruo static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ 155149e5fb46SQu Wenruo struct kobj_attribute *a, \ 155249e5fb46SQu Wenruo char *buf) \ 155349e5fb46SQu Wenruo { \ 155449e5fb46SQu Wenruo struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 155549e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 155649e5fb46SQu Wenruo struct btrfs_qgroup, kobj); \ 155749e5fb46SQu Wenruo return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ 155849e5fb46SQu Wenruo } \ 155949e5fb46SQu Wenruo BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) 156049e5fb46SQu Wenruo 156149e5fb46SQu Wenruo #define QGROUP_RSV_ATTR(_name, _type) \ 156249e5fb46SQu Wenruo static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ 156349e5fb46SQu Wenruo struct kobj_attribute *a, \ 156449e5fb46SQu Wenruo char *buf) \ 156549e5fb46SQu Wenruo { \ 156649e5fb46SQu Wenruo struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 156749e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 156849e5fb46SQu Wenruo struct btrfs_qgroup, kobj); \ 156949e5fb46SQu Wenruo return btrfs_show_u64(&qgroup->rsv.values[_type], \ 157049e5fb46SQu Wenruo &fs_info->qgroup_lock, buf); \ 157149e5fb46SQu Wenruo } \ 157249e5fb46SQu Wenruo BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) 157349e5fb46SQu Wenruo 157449e5fb46SQu Wenruo QGROUP_ATTR(rfer, referenced); 157549e5fb46SQu Wenruo QGROUP_ATTR(excl, exclusive); 157649e5fb46SQu Wenruo QGROUP_ATTR(max_rfer, max_referenced); 157749e5fb46SQu Wenruo QGROUP_ATTR(max_excl, max_exclusive); 157849e5fb46SQu Wenruo QGROUP_ATTR(lim_flags, limit_flags); 157949e5fb46SQu Wenruo QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); 158049e5fb46SQu Wenruo QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); 158149e5fb46SQu Wenruo QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); 158249e5fb46SQu Wenruo 158349e5fb46SQu Wenruo static struct attribute *qgroup_attrs[] = { 158449e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, referenced), 158549e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, exclusive), 158649e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, max_referenced), 158749e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, max_exclusive), 158849e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, limit_flags), 158949e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_data), 159049e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), 159149e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), 159249e5fb46SQu Wenruo NULL 159349e5fb46SQu Wenruo }; 159449e5fb46SQu Wenruo ATTRIBUTE_GROUPS(qgroup); 159549e5fb46SQu Wenruo 159649e5fb46SQu Wenruo static void qgroup_release(struct kobject *kobj) 159749e5fb46SQu Wenruo { 159849e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); 159949e5fb46SQu Wenruo 160049e5fb46SQu Wenruo memset(&qgroup->kobj, 0, sizeof(*kobj)); 160149e5fb46SQu Wenruo } 160249e5fb46SQu Wenruo 160349e5fb46SQu Wenruo static struct kobj_type qgroup_ktype = { 160449e5fb46SQu Wenruo .sysfs_ops = &kobj_sysfs_ops, 160549e5fb46SQu Wenruo .release = qgroup_release, 160649e5fb46SQu Wenruo .default_groups = qgroup_groups, 160749e5fb46SQu Wenruo }; 160849e5fb46SQu Wenruo 160949e5fb46SQu Wenruo int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, 161049e5fb46SQu Wenruo struct btrfs_qgroup *qgroup) 161149e5fb46SQu Wenruo { 161249e5fb46SQu Wenruo struct kobject *qgroups_kobj = fs_info->qgroups_kobj; 161349e5fb46SQu Wenruo int ret; 161449e5fb46SQu Wenruo 161549e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 161649e5fb46SQu Wenruo return 0; 161749e5fb46SQu Wenruo if (qgroup->kobj.state_initialized) 161849e5fb46SQu Wenruo return 0; 161949e5fb46SQu Wenruo if (!qgroups_kobj) 162049e5fb46SQu Wenruo return -EINVAL; 162149e5fb46SQu Wenruo 162249e5fb46SQu Wenruo ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, 162349e5fb46SQu Wenruo "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), 162449e5fb46SQu Wenruo btrfs_qgroup_subvolid(qgroup->qgroupid)); 162549e5fb46SQu Wenruo if (ret < 0) 162649e5fb46SQu Wenruo kobject_put(&qgroup->kobj); 162749e5fb46SQu Wenruo 162849e5fb46SQu Wenruo return ret; 162949e5fb46SQu Wenruo } 163049e5fb46SQu Wenruo 163149e5fb46SQu Wenruo void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) 163249e5fb46SQu Wenruo { 163349e5fb46SQu Wenruo struct btrfs_qgroup *qgroup; 163449e5fb46SQu Wenruo struct btrfs_qgroup *next; 163549e5fb46SQu Wenruo 163649e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 163749e5fb46SQu Wenruo return; 163849e5fb46SQu Wenruo 163949e5fb46SQu Wenruo rbtree_postorder_for_each_entry_safe(qgroup, next, 164049e5fb46SQu Wenruo &fs_info->qgroup_tree, node) 164149e5fb46SQu Wenruo btrfs_sysfs_del_one_qgroup(fs_info, qgroup); 164262ab2cc0SQu Wenruo if (fs_info->qgroups_kobj) { 164349e5fb46SQu Wenruo kobject_del(fs_info->qgroups_kobj); 164449e5fb46SQu Wenruo kobject_put(fs_info->qgroups_kobj); 164549e5fb46SQu Wenruo fs_info->qgroups_kobj = NULL; 164649e5fb46SQu Wenruo } 164762ab2cc0SQu Wenruo } 164849e5fb46SQu Wenruo 164949e5fb46SQu Wenruo /* Called when qgroups get initialized, thus there is no need for locking */ 165049e5fb46SQu Wenruo int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) 165149e5fb46SQu Wenruo { 165249e5fb46SQu Wenruo struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 165349e5fb46SQu Wenruo struct btrfs_qgroup *qgroup; 165449e5fb46SQu Wenruo struct btrfs_qgroup *next; 165549e5fb46SQu Wenruo int ret = 0; 165649e5fb46SQu Wenruo 165749e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 165849e5fb46SQu Wenruo return 0; 165949e5fb46SQu Wenruo 166049e5fb46SQu Wenruo ASSERT(fsid_kobj); 166149e5fb46SQu Wenruo if (fs_info->qgroups_kobj) 166249e5fb46SQu Wenruo return 0; 166349e5fb46SQu Wenruo 166449e5fb46SQu Wenruo fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); 166549e5fb46SQu Wenruo if (!fs_info->qgroups_kobj) { 166649e5fb46SQu Wenruo ret = -ENOMEM; 166749e5fb46SQu Wenruo goto out; 166849e5fb46SQu Wenruo } 166949e5fb46SQu Wenruo rbtree_postorder_for_each_entry_safe(qgroup, next, 167049e5fb46SQu Wenruo &fs_info->qgroup_tree, node) { 167149e5fb46SQu Wenruo ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); 167249e5fb46SQu Wenruo if (ret < 0) 167349e5fb46SQu Wenruo goto out; 167449e5fb46SQu Wenruo } 167549e5fb46SQu Wenruo 167649e5fb46SQu Wenruo out: 167749e5fb46SQu Wenruo if (ret < 0) 167849e5fb46SQu Wenruo btrfs_sysfs_del_qgroups(fs_info); 167949e5fb46SQu Wenruo return ret; 168049e5fb46SQu Wenruo } 168149e5fb46SQu Wenruo 168249e5fb46SQu Wenruo void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 168349e5fb46SQu Wenruo struct btrfs_qgroup *qgroup) 168449e5fb46SQu Wenruo { 168549e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 168649e5fb46SQu Wenruo return; 168749e5fb46SQu Wenruo 168849e5fb46SQu Wenruo if (qgroup->kobj.state_initialized) { 168949e5fb46SQu Wenruo kobject_del(&qgroup->kobj); 169049e5fb46SQu Wenruo kobject_put(&qgroup->kobj); 169149e5fb46SQu Wenruo } 169249e5fb46SQu Wenruo } 1693444e7516SDavid Sterba 1694444e7516SDavid Sterba /* 1695444e7516SDavid Sterba * Change per-fs features in /sys/fs/btrfs/UUID/features to match current 1696444e7516SDavid Sterba * values in superblock. Call after any changes to incompat/compat_ro flags 1697444e7516SDavid Sterba */ 1698444e7516SDavid Sterba void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, 1699444e7516SDavid Sterba u64 bit, enum btrfs_feature_set set) 1700444e7516SDavid Sterba { 1701444e7516SDavid Sterba struct btrfs_fs_devices *fs_devs; 1702444e7516SDavid Sterba struct kobject *fsid_kobj; 170324646481SLeon Romanovsky u64 __maybe_unused features; 170424646481SLeon Romanovsky int __maybe_unused ret; 1705444e7516SDavid Sterba 1706444e7516SDavid Sterba if (!fs_info) 1707444e7516SDavid Sterba return; 1708444e7516SDavid Sterba 170924646481SLeon Romanovsky /* 171024646481SLeon Romanovsky * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not 171124646481SLeon Romanovsky * safe when called from some contexts (eg. balance) 171224646481SLeon Romanovsky */ 1713444e7516SDavid Sterba features = get_features(fs_info, set); 1714444e7516SDavid Sterba ASSERT(bit & supported_feature_masks[set]); 1715444e7516SDavid Sterba 1716444e7516SDavid Sterba fs_devs = fs_info->fs_devices; 1717444e7516SDavid Sterba fsid_kobj = &fs_devs->fsid_kobj; 1718444e7516SDavid Sterba 1719bf609206SDavid Sterba if (!fsid_kobj->state_initialized) 1720bf609206SDavid Sterba return; 1721bf609206SDavid Sterba 1722444e7516SDavid Sterba /* 1723444e7516SDavid Sterba * FIXME: this is too heavy to update just one value, ideally we'd like 1724444e7516SDavid Sterba * to use sysfs_update_group but some refactoring is needed first. 1725444e7516SDavid Sterba */ 1726444e7516SDavid Sterba sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 1727444e7516SDavid Sterba ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); 1728444e7516SDavid Sterba } 1729444e7516SDavid Sterba 1730f5c29bd9SLiu Bo int __init btrfs_init_sysfs(void) 173158176a96SJosef Bacik { 1732079b72bcSJeff Mahoney int ret; 17331bae3098SDavid Sterba 1734e3fe4e71SGreg KH btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); 1735e3fe4e71SGreg KH if (!btrfs_kset) 1736e3fe4e71SGreg KH return -ENOMEM; 1737079b72bcSJeff Mahoney 17381bae3098SDavid Sterba init_feature_attrs(); 17391bae3098SDavid Sterba ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1740001a648dSFilipe Manana if (ret) 1741001a648dSFilipe Manana goto out2; 1742f902bd3aSMisono Tomohiro ret = sysfs_merge_group(&btrfs_kset->kobj, 1743f902bd3aSMisono Tomohiro &btrfs_static_feature_attr_group); 1744f902bd3aSMisono Tomohiro if (ret) 1745f902bd3aSMisono Tomohiro goto out_remove_group; 1746001a648dSFilipe Manana 17476e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 17486e369febSDavid Sterba ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 17496e369febSDavid Sterba if (ret) 17506e369febSDavid Sterba goto out2; 17516e369febSDavid Sterba #endif 17526e369febSDavid Sterba 1753001a648dSFilipe Manana return 0; 1754f902bd3aSMisono Tomohiro 1755f902bd3aSMisono Tomohiro out_remove_group: 1756f902bd3aSMisono Tomohiro sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1757001a648dSFilipe Manana out2: 1758001a648dSFilipe Manana kset_unregister(btrfs_kset); 17591bae3098SDavid Sterba 17601bae3098SDavid Sterba return ret; 176158176a96SJosef Bacik } 176258176a96SJosef Bacik 1763e67c718bSDavid Sterba void __cold btrfs_exit_sysfs(void) 176458176a96SJosef Bacik { 1765f902bd3aSMisono Tomohiro sysfs_unmerge_group(&btrfs_kset->kobj, 1766f902bd3aSMisono Tomohiro &btrfs_static_feature_attr_group); 1767079b72bcSJeff Mahoney sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 176871e8978eSDennis Zhou #ifdef CONFIG_BTRFS_DEBUG 176971e8978eSDennis Zhou sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 177071e8978eSDennis Zhou #endif 1771e3fe4e71SGreg KH kset_unregister(btrfs_kset); 177258176a96SJosef Bacik } 177355d47414SChris Mason 1774