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" 17bae45de0SChris Mason #include "transaction.h" 18079b72bcSJeff Mahoney #include "sysfs.h" 1929e5be24SJeff Mahoney #include "volumes.h" 208719aaaeSJosef Bacik #include "space-info.h" 21aac0023cSJosef Bacik #include "block-group.h" 2249e5fb46SQu Wenruo #include "qgroup.h" 23079b72bcSJeff Mahoney 249188db61SDavid Sterba struct btrfs_feature_attr { 259188db61SDavid Sterba struct kobj_attribute kobj_attr; 269188db61SDavid Sterba enum btrfs_feature_set feature_set; 279188db61SDavid Sterba u64 feature_bit; 289188db61SDavid Sterba }; 299188db61SDavid Sterba 309188db61SDavid Sterba /* For raid type sysfs entries */ 319188db61SDavid Sterba struct raid_kobject { 329188db61SDavid Sterba u64 flags; 339188db61SDavid Sterba struct kobject kobj; 349188db61SDavid Sterba }; 359188db61SDavid Sterba 369188db61SDavid Sterba #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ 379188db61SDavid Sterba { \ 389188db61SDavid Sterba .attr = { .name = __stringify(_name), .mode = _mode }, \ 399188db61SDavid Sterba .show = _show, \ 409188db61SDavid Sterba .store = _store, \ 419188db61SDavid Sterba } 429188db61SDavid Sterba 439188db61SDavid Sterba #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ 449188db61SDavid Sterba static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 459188db61SDavid Sterba __INIT_KOBJ_ATTR(_name, 0644, _show, _store) 469188db61SDavid Sterba 479188db61SDavid Sterba #define BTRFS_ATTR(_prefix, _name, _show) \ 489188db61SDavid Sterba static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ 499188db61SDavid Sterba __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) 509188db61SDavid Sterba 519188db61SDavid Sterba #define BTRFS_ATTR_PTR(_prefix, _name) \ 529188db61SDavid Sterba (&btrfs_attr_##_prefix##_##_name.attr) 539188db61SDavid Sterba 549188db61SDavid Sterba #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ 559188db61SDavid Sterba static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ 569188db61SDavid Sterba .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ 579188db61SDavid Sterba btrfs_feature_attr_show, \ 589188db61SDavid Sterba btrfs_feature_attr_store), \ 599188db61SDavid Sterba .feature_set = _feature_set, \ 609188db61SDavid Sterba .feature_bit = _feature_prefix ##_## _feature_bit, \ 619188db61SDavid Sterba } 629188db61SDavid Sterba #define BTRFS_FEAT_ATTR_PTR(_name) \ 639188db61SDavid Sterba (&btrfs_attr_features_##_name.kobj_attr.attr) 649188db61SDavid Sterba 659188db61SDavid Sterba #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ 669188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) 679188db61SDavid Sterba #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ 689188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) 699188db61SDavid Sterba #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ 709188db61SDavid Sterba BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) 719188db61SDavid Sterba 72510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); 732e7910d6SAnand Jain static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); 745ac1d209SJeff Mahoney 758f52316cSDavid Sterba static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) 768f52316cSDavid Sterba { 778f52316cSDavid Sterba return container_of(a, struct btrfs_feature_attr, kobj_attr); 788f52316cSDavid Sterba } 798f52316cSDavid Sterba 808f52316cSDavid Sterba static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) 818f52316cSDavid Sterba { 828f52316cSDavid Sterba return container_of(attr, struct kobj_attribute, attr); 838f52316cSDavid Sterba } 848f52316cSDavid Sterba 858f52316cSDavid Sterba static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( 868f52316cSDavid Sterba struct attribute *attr) 878f52316cSDavid Sterba { 888f52316cSDavid Sterba return to_btrfs_feature_attr(attr_to_btrfs_attr(attr)); 898f52316cSDavid Sterba } 908f52316cSDavid Sterba 91510d7360SJeff Mahoney static u64 get_features(struct btrfs_fs_info *fs_info, 92510d7360SJeff Mahoney enum btrfs_feature_set set) 935ac1d209SJeff Mahoney { 94510d7360SJeff Mahoney struct btrfs_super_block *disk_super = fs_info->super_copy; 95510d7360SJeff Mahoney if (set == FEAT_COMPAT) 96510d7360SJeff Mahoney return btrfs_super_compat_flags(disk_super); 97510d7360SJeff Mahoney else if (set == FEAT_COMPAT_RO) 98510d7360SJeff Mahoney return btrfs_super_compat_ro_flags(disk_super); 99510d7360SJeff Mahoney else 100510d7360SJeff Mahoney return btrfs_super_incompat_flags(disk_super); 1015ac1d209SJeff Mahoney } 1025ac1d209SJeff Mahoney 103ba631941SJeff Mahoney static void set_features(struct btrfs_fs_info *fs_info, 104ba631941SJeff Mahoney enum btrfs_feature_set set, u64 features) 105ba631941SJeff Mahoney { 106ba631941SJeff Mahoney struct btrfs_super_block *disk_super = fs_info->super_copy; 107ba631941SJeff Mahoney if (set == FEAT_COMPAT) 108ba631941SJeff Mahoney btrfs_set_super_compat_flags(disk_super, features); 109ba631941SJeff Mahoney else if (set == FEAT_COMPAT_RO) 110ba631941SJeff Mahoney btrfs_set_super_compat_ro_flags(disk_super, features); 111ba631941SJeff Mahoney else 112ba631941SJeff Mahoney btrfs_set_super_incompat_flags(disk_super, features); 113ba631941SJeff Mahoney } 114ba631941SJeff Mahoney 115ba631941SJeff Mahoney static int can_modify_feature(struct btrfs_feature_attr *fa) 116ba631941SJeff Mahoney { 117ba631941SJeff Mahoney int val = 0; 118ba631941SJeff Mahoney u64 set, clear; 119ba631941SJeff Mahoney switch (fa->feature_set) { 120ba631941SJeff Mahoney case FEAT_COMPAT: 121ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_SAFE_SET; 122ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 123ba631941SJeff Mahoney break; 124ba631941SJeff Mahoney case FEAT_COMPAT_RO: 125ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 126ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 127ba631941SJeff Mahoney break; 128ba631941SJeff Mahoney case FEAT_INCOMPAT: 129ba631941SJeff Mahoney set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 130ba631941SJeff Mahoney clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 131ba631941SJeff Mahoney break; 132ba631941SJeff Mahoney default: 13362e85577SJeff Mahoney pr_warn("btrfs: sysfs: unknown feature set %d\n", 134cc37bb04SDavid Sterba fa->feature_set); 135cc37bb04SDavid Sterba return 0; 136ba631941SJeff Mahoney } 137ba631941SJeff Mahoney 138ba631941SJeff Mahoney if (set & fa->feature_bit) 139ba631941SJeff Mahoney val |= 1; 140ba631941SJeff Mahoney if (clear & fa->feature_bit) 141ba631941SJeff Mahoney val |= 2; 142ba631941SJeff Mahoney 143ba631941SJeff Mahoney return val; 144ba631941SJeff Mahoney } 145ba631941SJeff Mahoney 146079b72bcSJeff Mahoney static ssize_t btrfs_feature_attr_show(struct kobject *kobj, 147079b72bcSJeff Mahoney struct kobj_attribute *a, char *buf) 148079b72bcSJeff Mahoney { 149510d7360SJeff Mahoney int val = 0; 150510d7360SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 151510d7360SJeff Mahoney struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 152ba631941SJeff Mahoney if (fs_info) { 153510d7360SJeff Mahoney u64 features = get_features(fs_info, fa->feature_set); 154510d7360SJeff Mahoney if (features & fa->feature_bit) 155510d7360SJeff Mahoney val = 1; 156ba631941SJeff Mahoney } else 157ba631941SJeff Mahoney val = can_modify_feature(fa); 158510d7360SJeff Mahoney 159abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 160510d7360SJeff Mahoney } 161510d7360SJeff Mahoney 162ba631941SJeff Mahoney static ssize_t btrfs_feature_attr_store(struct kobject *kobj, 163ba631941SJeff Mahoney struct kobj_attribute *a, 164ba631941SJeff Mahoney const char *buf, size_t count) 165ba631941SJeff Mahoney { 166ba631941SJeff Mahoney struct btrfs_fs_info *fs_info; 167ba631941SJeff Mahoney struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); 168ba631941SJeff Mahoney u64 features, set, clear; 169ba631941SJeff Mahoney unsigned long val; 170ba631941SJeff Mahoney int ret; 171ba631941SJeff Mahoney 172ba631941SJeff Mahoney fs_info = to_fs_info(kobj); 173ba631941SJeff Mahoney if (!fs_info) 174ba631941SJeff Mahoney return -EPERM; 175ba631941SJeff Mahoney 176bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) 177ee611138SDavid Sterba return -EROFS; 178ee611138SDavid Sterba 179ba631941SJeff Mahoney ret = kstrtoul(skip_spaces(buf), 0, &val); 180ba631941SJeff Mahoney if (ret) 181ba631941SJeff Mahoney return ret; 182ba631941SJeff Mahoney 183ba631941SJeff Mahoney if (fa->feature_set == FEAT_COMPAT) { 184ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_SAFE_SET; 185ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; 186ba631941SJeff Mahoney } else if (fa->feature_set == FEAT_COMPAT_RO) { 187ba631941SJeff Mahoney set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; 188ba631941SJeff Mahoney clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; 189ba631941SJeff Mahoney } else { 190ba631941SJeff Mahoney set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; 191ba631941SJeff Mahoney clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; 192ba631941SJeff Mahoney } 193ba631941SJeff Mahoney 194ba631941SJeff Mahoney features = get_features(fs_info, fa->feature_set); 195ba631941SJeff Mahoney 196ba631941SJeff Mahoney /* Nothing to do */ 197ba631941SJeff Mahoney if ((val && (features & fa->feature_bit)) || 198ba631941SJeff Mahoney (!val && !(features & fa->feature_bit))) 199ba631941SJeff Mahoney return count; 200ba631941SJeff Mahoney 201ba631941SJeff Mahoney if ((val && !(set & fa->feature_bit)) || 202ba631941SJeff Mahoney (!val && !(clear & fa->feature_bit))) { 203ba631941SJeff Mahoney btrfs_info(fs_info, 204ba631941SJeff Mahoney "%sabling feature %s on mounted fs is not supported.", 205ba631941SJeff Mahoney val ? "En" : "Dis", fa->kobj_attr.attr.name); 206ba631941SJeff Mahoney return -EPERM; 207ba631941SJeff Mahoney } 208ba631941SJeff Mahoney 209ba631941SJeff Mahoney btrfs_info(fs_info, "%s %s feature flag", 210ba631941SJeff Mahoney val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); 211ba631941SJeff Mahoney 212ba631941SJeff Mahoney spin_lock(&fs_info->super_lock); 213ba631941SJeff Mahoney features = get_features(fs_info, fa->feature_set); 214ba631941SJeff Mahoney if (val) 215ba631941SJeff Mahoney features |= fa->feature_bit; 216ba631941SJeff Mahoney else 217ba631941SJeff Mahoney features &= ~fa->feature_bit; 218ba631941SJeff Mahoney set_features(fs_info, fa->feature_set, features); 219ba631941SJeff Mahoney spin_unlock(&fs_info->super_lock); 220ba631941SJeff Mahoney 2210eae2747SDavid Sterba /* 2220eae2747SDavid Sterba * We don't want to do full transaction commit from inside sysfs 2230eae2747SDavid Sterba */ 2240eae2747SDavid Sterba btrfs_set_pending(fs_info, COMMIT); 2250eae2747SDavid Sterba wake_up_process(fs_info->transaction_kthread); 226ba631941SJeff Mahoney 227ba631941SJeff Mahoney return count; 228ba631941SJeff Mahoney } 229ba631941SJeff Mahoney 230510d7360SJeff Mahoney static umode_t btrfs_feature_visible(struct kobject *kobj, 231510d7360SJeff Mahoney struct attribute *attr, int unused) 232510d7360SJeff Mahoney { 233510d7360SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 234510d7360SJeff Mahoney umode_t mode = attr->mode; 235510d7360SJeff Mahoney 236510d7360SJeff Mahoney if (fs_info) { 237510d7360SJeff Mahoney struct btrfs_feature_attr *fa; 238510d7360SJeff Mahoney u64 features; 239510d7360SJeff Mahoney 240510d7360SJeff Mahoney fa = attr_to_btrfs_feature_attr(attr); 241510d7360SJeff Mahoney features = get_features(fs_info, fa->feature_set); 242510d7360SJeff Mahoney 243ba631941SJeff Mahoney if (can_modify_feature(fa)) 244ba631941SJeff Mahoney mode |= S_IWUSR; 245ba631941SJeff Mahoney else if (!(features & fa->feature_bit)) 246510d7360SJeff Mahoney mode = 0; 247510d7360SJeff Mahoney } 248510d7360SJeff Mahoney 249510d7360SJeff Mahoney return mode; 250079b72bcSJeff Mahoney } 251079b72bcSJeff Mahoney 252079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); 253079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); 254079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); 255079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); 2565c1aab1dSNick Terrell BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); 257079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); 258079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); 259079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); 260079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); 261c736c095SDavid Sterba BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); 26256f20f40SNikolay Borisov BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); 2633b5bb73bSDavid Sterba BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); 264cfbb825cSDavid Sterba BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); 265079b72bcSJeff Mahoney 266079b72bcSJeff Mahoney static struct attribute *btrfs_supported_feature_attrs[] = { 267079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(mixed_backref), 268079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(default_subvol), 269079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(mixed_groups), 270079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(compress_lzo), 2715c1aab1dSNick Terrell BTRFS_FEAT_ATTR_PTR(compress_zstd), 272079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(big_metadata), 273079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(extended_iref), 274079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(raid56), 275079b72bcSJeff Mahoney BTRFS_FEAT_ATTR_PTR(skinny_metadata), 276c736c095SDavid Sterba BTRFS_FEAT_ATTR_PTR(no_holes), 27756f20f40SNikolay Borisov BTRFS_FEAT_ATTR_PTR(metadata_uuid), 2783b5bb73bSDavid Sterba BTRFS_FEAT_ATTR_PTR(free_space_tree), 279cfbb825cSDavid Sterba BTRFS_FEAT_ATTR_PTR(raid1c34), 280079b72bcSJeff Mahoney NULL 281079b72bcSJeff Mahoney }; 282079b72bcSJeff Mahoney 283f902bd3aSMisono Tomohiro /* 284f902bd3aSMisono Tomohiro * Features which depend on feature bits and may differ between each fs. 285f902bd3aSMisono Tomohiro * 286f902bd3aSMisono Tomohiro * /sys/fs/btrfs/features lists all available features of this kernel while 287f902bd3aSMisono Tomohiro * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or 288f902bd3aSMisono Tomohiro * can be changed online. 289f902bd3aSMisono Tomohiro */ 290079b72bcSJeff Mahoney static const struct attribute_group btrfs_feature_attr_group = { 291079b72bcSJeff Mahoney .name = "features", 292510d7360SJeff Mahoney .is_visible = btrfs_feature_visible, 293079b72bcSJeff Mahoney .attrs = btrfs_supported_feature_attrs, 294079b72bcSJeff Mahoney }; 29558176a96SJosef Bacik 296f902bd3aSMisono Tomohiro static ssize_t rmdir_subvol_show(struct kobject *kobj, 297f902bd3aSMisono Tomohiro struct kobj_attribute *ka, char *buf) 298f902bd3aSMisono Tomohiro { 299abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "0\n"); 300f902bd3aSMisono Tomohiro } 301f902bd3aSMisono Tomohiro BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); 302f902bd3aSMisono Tomohiro 303f7cea56cSDavid Sterba static ssize_t supported_checksums_show(struct kobject *kobj, 304f7cea56cSDavid Sterba struct kobj_attribute *a, char *buf) 305f7cea56cSDavid Sterba { 306f7cea56cSDavid Sterba ssize_t ret = 0; 307f7cea56cSDavid Sterba int i; 308f7cea56cSDavid Sterba 309f7cea56cSDavid Sterba for (i = 0; i < btrfs_get_num_csums(); i++) { 310f7cea56cSDavid Sterba /* 311f7cea56cSDavid Sterba * This "trick" only works as long as 'enum btrfs_csum_type' has 312f7cea56cSDavid Sterba * no holes in it 313f7cea56cSDavid Sterba */ 314abdd9febSTakashi Iwai ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s", 315f7cea56cSDavid Sterba (i == 0 ? "" : " "), btrfs_super_csum_name(i)); 316f7cea56cSDavid Sterba 317f7cea56cSDavid Sterba } 318f7cea56cSDavid Sterba 319abdd9febSTakashi Iwai ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n"); 320f7cea56cSDavid Sterba return ret; 321f7cea56cSDavid Sterba } 322f7cea56cSDavid Sterba BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); 323f7cea56cSDavid Sterba 324f902bd3aSMisono Tomohiro static struct attribute *btrfs_supported_static_feature_attrs[] = { 325f902bd3aSMisono Tomohiro BTRFS_ATTR_PTR(static_feature, rmdir_subvol), 326f7cea56cSDavid Sterba BTRFS_ATTR_PTR(static_feature, supported_checksums), 327f902bd3aSMisono Tomohiro NULL 328f902bd3aSMisono Tomohiro }; 329f902bd3aSMisono Tomohiro 330f902bd3aSMisono Tomohiro /* 331f902bd3aSMisono Tomohiro * Features which only depend on kernel version. 332f902bd3aSMisono Tomohiro * 333f902bd3aSMisono Tomohiro * These are listed in /sys/fs/btrfs/features along with 334f902bd3aSMisono Tomohiro * btrfs_feature_attr_group 335f902bd3aSMisono Tomohiro */ 336f902bd3aSMisono Tomohiro static const struct attribute_group btrfs_static_feature_attr_group = { 337f902bd3aSMisono Tomohiro .name = "features", 338f902bd3aSMisono Tomohiro .attrs = btrfs_supported_static_feature_attrs, 339f902bd3aSMisono Tomohiro }; 340f902bd3aSMisono Tomohiro 3416e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 3426e369febSDavid Sterba 3436e369febSDavid Sterba /* 344e4faab84SDennis Zhou * Discard statistics and tunables 345e4faab84SDennis Zhou */ 346dfb79ddbSDennis Zhou #define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent) 347dfb79ddbSDennis Zhou 3485dc7c10bSDennis Zhou static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, 3495dc7c10bSDennis Zhou struct kobj_attribute *a, 3505dc7c10bSDennis Zhou char *buf) 3515dc7c10bSDennis Zhou { 3525dc7c10bSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3535dc7c10bSDennis Zhou 354abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3555dc7c10bSDennis Zhou atomic64_read(&fs_info->discard_ctl.discardable_bytes)); 3565dc7c10bSDennis Zhou } 3575dc7c10bSDennis Zhou BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); 3585dc7c10bSDennis Zhou 359dfb79ddbSDennis Zhou static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, 360dfb79ddbSDennis Zhou struct kobj_attribute *a, 361dfb79ddbSDennis Zhou char *buf) 362dfb79ddbSDennis Zhou { 363dfb79ddbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 364dfb79ddbSDennis Zhou 365abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", 366dfb79ddbSDennis Zhou atomic_read(&fs_info->discard_ctl.discardable_extents)); 367dfb79ddbSDennis Zhou } 368dfb79ddbSDennis Zhou BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); 369dfb79ddbSDennis Zhou 3709ddf648fSDennis Zhou static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, 3719ddf648fSDennis Zhou struct kobj_attribute *a, 3729ddf648fSDennis Zhou char *buf) 3739ddf648fSDennis Zhou { 3749ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3759ddf648fSDennis Zhou 376abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3779ddf648fSDennis Zhou fs_info->discard_ctl.discard_bitmap_bytes); 3789ddf648fSDennis Zhou } 3799ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); 3809ddf648fSDennis Zhou 3819ddf648fSDennis Zhou static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, 3829ddf648fSDennis Zhou struct kobj_attribute *a, 3839ddf648fSDennis Zhou char *buf) 3849ddf648fSDennis Zhou { 3859ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3869ddf648fSDennis Zhou 387abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3889ddf648fSDennis Zhou atomic64_read(&fs_info->discard_ctl.discard_bytes_saved)); 3899ddf648fSDennis Zhou } 3909ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); 3919ddf648fSDennis Zhou 3929ddf648fSDennis Zhou static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, 3939ddf648fSDennis Zhou struct kobj_attribute *a, 3949ddf648fSDennis Zhou char *buf) 3959ddf648fSDennis Zhou { 3969ddf648fSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 3979ddf648fSDennis Zhou 398abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", 3999ddf648fSDennis Zhou fs_info->discard_ctl.discard_extent_bytes); 4009ddf648fSDennis Zhou } 4019ddf648fSDennis Zhou BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); 4029ddf648fSDennis Zhou 403a2309300SDennis Zhou static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, 404a2309300SDennis Zhou struct kobj_attribute *a, 405a2309300SDennis Zhou char *buf) 406a2309300SDennis Zhou { 407a2309300SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 408a2309300SDennis Zhou 409abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 410a2309300SDennis Zhou READ_ONCE(fs_info->discard_ctl.iops_limit)); 411a2309300SDennis Zhou } 412a2309300SDennis Zhou 413a2309300SDennis Zhou static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, 414a2309300SDennis Zhou struct kobj_attribute *a, 415a2309300SDennis Zhou const char *buf, size_t len) 416a2309300SDennis Zhou { 417a2309300SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 418a2309300SDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 419a2309300SDennis Zhou u32 iops_limit; 420a2309300SDennis Zhou int ret; 421a2309300SDennis Zhou 422a2309300SDennis Zhou ret = kstrtou32(buf, 10, &iops_limit); 423a2309300SDennis Zhou if (ret) 424a2309300SDennis Zhou return -EINVAL; 425a2309300SDennis Zhou 426a2309300SDennis Zhou WRITE_ONCE(discard_ctl->iops_limit, iops_limit); 427a2309300SDennis Zhou 428a2309300SDennis Zhou return len; 429a2309300SDennis Zhou } 430a2309300SDennis Zhou BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, 431a2309300SDennis Zhou btrfs_discard_iops_limit_store); 432a2309300SDennis Zhou 433e93591bbSDennis Zhou static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, 434e93591bbSDennis Zhou struct kobj_attribute *a, 435e93591bbSDennis Zhou char *buf) 436e93591bbSDennis Zhou { 437e93591bbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 438e93591bbSDennis Zhou 439abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 440e93591bbSDennis Zhou READ_ONCE(fs_info->discard_ctl.kbps_limit)); 441e93591bbSDennis Zhou } 442e93591bbSDennis Zhou 443e93591bbSDennis Zhou static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, 444e93591bbSDennis Zhou struct kobj_attribute *a, 445e93591bbSDennis Zhou const char *buf, size_t len) 446e93591bbSDennis Zhou { 447e93591bbSDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 448e93591bbSDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 449e93591bbSDennis Zhou u32 kbps_limit; 450e93591bbSDennis Zhou int ret; 451e93591bbSDennis Zhou 452e93591bbSDennis Zhou ret = kstrtou32(buf, 10, &kbps_limit); 453e93591bbSDennis Zhou if (ret) 454e93591bbSDennis Zhou return -EINVAL; 455e93591bbSDennis Zhou 456e93591bbSDennis Zhou WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); 457e93591bbSDennis Zhou 458e93591bbSDennis Zhou return len; 459e93591bbSDennis Zhou } 460e93591bbSDennis Zhou BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, 461e93591bbSDennis Zhou btrfs_discard_kbps_limit_store); 462e93591bbSDennis Zhou 46319b2a2c7SDennis Zhou static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, 46419b2a2c7SDennis Zhou struct kobj_attribute *a, 46519b2a2c7SDennis Zhou char *buf) 46619b2a2c7SDennis Zhou { 46719b2a2c7SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 46819b2a2c7SDennis Zhou 469abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", 47019b2a2c7SDennis Zhou READ_ONCE(fs_info->discard_ctl.max_discard_size)); 47119b2a2c7SDennis Zhou } 47219b2a2c7SDennis Zhou 47319b2a2c7SDennis Zhou static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, 47419b2a2c7SDennis Zhou struct kobj_attribute *a, 47519b2a2c7SDennis Zhou const char *buf, size_t len) 47619b2a2c7SDennis Zhou { 47719b2a2c7SDennis Zhou struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); 47819b2a2c7SDennis Zhou struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; 47919b2a2c7SDennis Zhou u64 max_discard_size; 48019b2a2c7SDennis Zhou int ret; 48119b2a2c7SDennis Zhou 48219b2a2c7SDennis Zhou ret = kstrtou64(buf, 10, &max_discard_size); 48319b2a2c7SDennis Zhou if (ret) 48419b2a2c7SDennis Zhou return -EINVAL; 48519b2a2c7SDennis Zhou 48619b2a2c7SDennis Zhou WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); 48719b2a2c7SDennis Zhou 48819b2a2c7SDennis Zhou return len; 48919b2a2c7SDennis Zhou } 49019b2a2c7SDennis Zhou BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, 49119b2a2c7SDennis Zhou btrfs_discard_max_discard_size_store); 49219b2a2c7SDennis Zhou 493e4faab84SDennis Zhou static const struct attribute *discard_debug_attrs[] = { 4945dc7c10bSDennis Zhou BTRFS_ATTR_PTR(discard, discardable_bytes), 495dfb79ddbSDennis Zhou BTRFS_ATTR_PTR(discard, discardable_extents), 4969ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), 4979ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_bytes_saved), 4989ddf648fSDennis Zhou BTRFS_ATTR_PTR(discard, discard_extent_bytes), 499a2309300SDennis Zhou BTRFS_ATTR_PTR(discard, iops_limit), 500e93591bbSDennis Zhou BTRFS_ATTR_PTR(discard, kbps_limit), 50119b2a2c7SDennis Zhou BTRFS_ATTR_PTR(discard, max_discard_size), 502e4faab84SDennis Zhou NULL, 503e4faab84SDennis Zhou }; 504e4faab84SDennis Zhou 505e4faab84SDennis Zhou /* 5066e369febSDavid Sterba * Runtime debugging exported via sysfs 5076e369febSDavid Sterba * 5086e369febSDavid Sterba * /sys/fs/btrfs/debug - applies to module or all filesystems 5096e369febSDavid Sterba * /sys/fs/btrfs/UUID - applies only to the given filesystem 5106e369febSDavid Sterba */ 51193945cb4SDennis Zhou static const struct attribute *btrfs_debug_mount_attrs[] = { 51293945cb4SDennis Zhou NULL, 51393945cb4SDennis Zhou }; 51493945cb4SDennis Zhou 5156e369febSDavid Sterba static struct attribute *btrfs_debug_feature_attrs[] = { 5166e369febSDavid Sterba NULL 5176e369febSDavid Sterba }; 5186e369febSDavid Sterba 5196e369febSDavid Sterba static const struct attribute_group btrfs_debug_feature_attr_group = { 5206e369febSDavid Sterba .name = "debug", 5216e369febSDavid Sterba .attrs = btrfs_debug_feature_attrs, 5226e369febSDavid Sterba }; 5236e369febSDavid Sterba 5246e369febSDavid Sterba #endif 5256e369febSDavid Sterba 5266ab0a202SJeff Mahoney static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) 5276ab0a202SJeff Mahoney { 5286ab0a202SJeff Mahoney u64 val; 5296ab0a202SJeff Mahoney if (lock) 5306ab0a202SJeff Mahoney spin_lock(lock); 5316ab0a202SJeff Mahoney val = *value_ptr; 5326ab0a202SJeff Mahoney if (lock) 5336ab0a202SJeff Mahoney spin_unlock(lock); 534abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", val); 5356ab0a202SJeff Mahoney } 5366ab0a202SJeff Mahoney 5376ab0a202SJeff Mahoney static ssize_t global_rsv_size_show(struct kobject *kobj, 5386ab0a202SJeff Mahoney struct kobj_attribute *ka, char *buf) 5396ab0a202SJeff Mahoney { 5406ab0a202SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); 5416ab0a202SJeff Mahoney struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 5426ab0a202SJeff Mahoney return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); 5436ab0a202SJeff Mahoney } 544a969f4ccSHans van Kranenburg BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); 5456ab0a202SJeff Mahoney 5466ab0a202SJeff Mahoney static ssize_t global_rsv_reserved_show(struct kobject *kobj, 5476ab0a202SJeff Mahoney struct kobj_attribute *a, 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->reserved, &block_rsv->lock, buf); 5526ab0a202SJeff Mahoney } 553a969f4ccSHans van Kranenburg BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); 5546ab0a202SJeff Mahoney 5556ab0a202SJeff Mahoney #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) 556c1895442SJeff Mahoney #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) 5576ab0a202SJeff Mahoney 5586ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj, 5596ab0a202SJeff Mahoney struct kobj_attribute *attr, char *buf); 560a969f4ccSHans van Kranenburg BTRFS_ATTR(raid, total_bytes, raid_bytes_show); 561a969f4ccSHans van Kranenburg BTRFS_ATTR(raid, used_bytes, raid_bytes_show); 5626ab0a202SJeff Mahoney 5636ab0a202SJeff Mahoney static ssize_t raid_bytes_show(struct kobject *kobj, 5646ab0a202SJeff Mahoney struct kobj_attribute *attr, char *buf) 5656ab0a202SJeff Mahoney 5666ab0a202SJeff Mahoney { 5676ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj->parent); 56832da5386SDavid Sterba struct btrfs_block_group *block_group; 56975cb379dSJeff Mahoney int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); 5706ab0a202SJeff Mahoney u64 val = 0; 5716ab0a202SJeff Mahoney 5726ab0a202SJeff Mahoney down_read(&sinfo->groups_sem); 5736ab0a202SJeff Mahoney list_for_each_entry(block_group, &sinfo->block_groups[index], list) { 574a969f4ccSHans van Kranenburg if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) 575b3470b5dSDavid Sterba val += block_group->length; 5766ab0a202SJeff Mahoney else 577bf38be65SDavid Sterba val += block_group->used; 5786ab0a202SJeff Mahoney } 5796ab0a202SJeff Mahoney up_read(&sinfo->groups_sem); 580abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%llu\n", val); 5816ab0a202SJeff Mahoney } 5826ab0a202SJeff Mahoney 5837c7e3014SKimberly Brown static struct attribute *raid_attrs[] = { 584a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(raid, total_bytes), 585a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(raid, used_bytes), 5866ab0a202SJeff Mahoney NULL 5876ab0a202SJeff Mahoney }; 5887c7e3014SKimberly Brown ATTRIBUTE_GROUPS(raid); 5896ab0a202SJeff Mahoney 5906ab0a202SJeff Mahoney static void release_raid_kobj(struct kobject *kobj) 5916ab0a202SJeff Mahoney { 592c1895442SJeff Mahoney kfree(to_raid_kobj(kobj)); 5936ab0a202SJeff Mahoney } 5946ab0a202SJeff Mahoney 595536ea45cSDavid Sterba static struct kobj_type btrfs_raid_ktype = { 5966ab0a202SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 5976ab0a202SJeff Mahoney .release = release_raid_kobj, 5987c7e3014SKimberly Brown .default_groups = raid_groups, 5996ab0a202SJeff Mahoney }; 6006ab0a202SJeff Mahoney 6016ab0a202SJeff Mahoney #define SPACE_INFO_ATTR(field) \ 6026ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ 6036ab0a202SJeff Mahoney struct kobj_attribute *a, \ 6046ab0a202SJeff Mahoney char *buf) \ 6056ab0a202SJeff Mahoney { \ 6066ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj); \ 6076ab0a202SJeff Mahoney return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ 6086ab0a202SJeff Mahoney } \ 609a969f4ccSHans van Kranenburg BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) 6106ab0a202SJeff Mahoney 6116ab0a202SJeff Mahoney static ssize_t btrfs_space_info_show_total_bytes_pinned(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 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); 617abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%lld\n", val); 6186ab0a202SJeff Mahoney } 6196ab0a202SJeff Mahoney 6206ab0a202SJeff Mahoney SPACE_INFO_ATTR(flags); 6216ab0a202SJeff Mahoney SPACE_INFO_ATTR(total_bytes); 6226ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_used); 6236ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_pinned); 6246ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_reserved); 6256ab0a202SJeff Mahoney SPACE_INFO_ATTR(bytes_may_use); 626c1fd5c30SWang Xiaoguang SPACE_INFO_ATTR(bytes_readonly); 6276ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_used); 6286ab0a202SJeff Mahoney SPACE_INFO_ATTR(disk_total); 629a969f4ccSHans van Kranenburg BTRFS_ATTR(space_info, total_bytes_pinned, 630a969f4ccSHans van Kranenburg btrfs_space_info_show_total_bytes_pinned); 6316ab0a202SJeff Mahoney 6326ab0a202SJeff Mahoney static struct attribute *space_info_attrs[] = { 633a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, flags), 634a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, total_bytes), 635a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_used), 636a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_pinned), 637a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_reserved), 638a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_may_use), 639a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, bytes_readonly), 640a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, disk_used), 641a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, disk_total), 642a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(space_info, total_bytes_pinned), 6436ab0a202SJeff Mahoney NULL, 6446ab0a202SJeff Mahoney }; 6457c7e3014SKimberly Brown ATTRIBUTE_GROUPS(space_info); 6466ab0a202SJeff Mahoney 6476ab0a202SJeff Mahoney static void space_info_release(struct kobject *kobj) 6486ab0a202SJeff Mahoney { 6496ab0a202SJeff Mahoney struct btrfs_space_info *sinfo = to_space_info(kobj); 6506ab0a202SJeff Mahoney percpu_counter_destroy(&sinfo->total_bytes_pinned); 6516ab0a202SJeff Mahoney kfree(sinfo); 6526ab0a202SJeff Mahoney } 6536ab0a202SJeff Mahoney 65427992d01SDavid Sterba static struct kobj_type space_info_ktype = { 6556ab0a202SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 6566ab0a202SJeff Mahoney .release = space_info_release, 6577c7e3014SKimberly Brown .default_groups = space_info_groups, 6586ab0a202SJeff Mahoney }; 6596ab0a202SJeff Mahoney 6606ab0a202SJeff Mahoney static const struct attribute *allocation_attrs[] = { 661a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(allocation, global_rsv_reserved), 662a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(allocation, global_rsv_size), 6636ab0a202SJeff Mahoney NULL, 6646ab0a202SJeff Mahoney }; 6656ab0a202SJeff Mahoney 666f8ba9c11SJeff Mahoney static ssize_t btrfs_label_show(struct kobject *kobj, 667f8ba9c11SJeff Mahoney struct kobj_attribute *a, char *buf) 668f8ba9c11SJeff Mahoney { 669f8ba9c11SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 67048fcc3ffSSatoru Takeuchi char *label = fs_info->super_copy->label; 671ee17fc80SDavid Sterba ssize_t ret; 672ee17fc80SDavid Sterba 673ee17fc80SDavid Sterba spin_lock(&fs_info->super_lock); 674abdd9febSTakashi Iwai ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); 675ee17fc80SDavid Sterba spin_unlock(&fs_info->super_lock); 676ee17fc80SDavid Sterba 677ee17fc80SDavid Sterba return ret; 678f8ba9c11SJeff Mahoney } 679f8ba9c11SJeff Mahoney 680f8ba9c11SJeff Mahoney static ssize_t btrfs_label_store(struct kobject *kobj, 681f8ba9c11SJeff Mahoney struct kobj_attribute *a, 682f8ba9c11SJeff Mahoney const char *buf, size_t len) 683f8ba9c11SJeff Mahoney { 684f8ba9c11SJeff Mahoney struct btrfs_fs_info *fs_info = to_fs_info(kobj); 68548fcc3ffSSatoru Takeuchi size_t p_len; 686f8ba9c11SJeff Mahoney 68766ac9fe7SDavid Sterba if (!fs_info) 68866ac9fe7SDavid Sterba return -EPERM; 68966ac9fe7SDavid Sterba 690bc98a42cSDavid Howells if (sb_rdonly(fs_info->sb)) 69179aec2b8SAnand Jain return -EROFS; 69279aec2b8SAnand Jain 69348fcc3ffSSatoru Takeuchi /* 69448fcc3ffSSatoru Takeuchi * p_len is the len until the first occurrence of either 69548fcc3ffSSatoru Takeuchi * '\n' or '\0' 69648fcc3ffSSatoru Takeuchi */ 69748fcc3ffSSatoru Takeuchi p_len = strcspn(buf, "\n"); 69848fcc3ffSSatoru Takeuchi 69948fcc3ffSSatoru Takeuchi if (p_len >= BTRFS_LABEL_SIZE) 700f8ba9c11SJeff Mahoney return -EINVAL; 701f8ba9c11SJeff Mahoney 702a6f69dc8SDavid Sterba spin_lock(&fs_info->super_lock); 70348fcc3ffSSatoru Takeuchi memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); 70448fcc3ffSSatoru Takeuchi memcpy(fs_info->super_copy->label, buf, p_len); 705a6f69dc8SDavid Sterba spin_unlock(&fs_info->super_lock); 706f8ba9c11SJeff Mahoney 707a6f69dc8SDavid Sterba /* 708a6f69dc8SDavid Sterba * We don't want to do full transaction commit from inside sysfs 709a6f69dc8SDavid Sterba */ 710a6f69dc8SDavid Sterba btrfs_set_pending(fs_info, COMMIT); 711a6f69dc8SDavid Sterba wake_up_process(fs_info->transaction_kthread); 712a6f69dc8SDavid Sterba 713f8ba9c11SJeff Mahoney return len; 714f8ba9c11SJeff Mahoney } 715a969f4ccSHans van Kranenburg BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); 716f8ba9c11SJeff Mahoney 717df93589aSDavid Sterba static ssize_t btrfs_nodesize_show(struct kobject *kobj, 718df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 719df93589aSDavid Sterba { 720df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 721df93589aSDavid Sterba 722abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize); 723df93589aSDavid Sterba } 724df93589aSDavid Sterba 725a969f4ccSHans van Kranenburg BTRFS_ATTR(, nodesize, btrfs_nodesize_show); 726df93589aSDavid Sterba 727df93589aSDavid Sterba static ssize_t btrfs_sectorsize_show(struct kobject *kobj, 728df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 729df93589aSDavid Sterba { 730df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 731df93589aSDavid Sterba 732abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", 733093e037cSDavid Sterba fs_info->super_copy->sectorsize); 734df93589aSDavid Sterba } 735df93589aSDavid Sterba 736a969f4ccSHans van Kranenburg BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); 737df93589aSDavid Sterba 738df93589aSDavid Sterba static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, 739df93589aSDavid Sterba struct kobj_attribute *a, char *buf) 740df93589aSDavid Sterba { 741df93589aSDavid Sterba struct btrfs_fs_info *fs_info = to_fs_info(kobj); 742df93589aSDavid Sterba 743abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize); 744df93589aSDavid Sterba } 745df93589aSDavid Sterba 746a969f4ccSHans van Kranenburg BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); 747df93589aSDavid Sterba 7482723480aSSargun Dhillon static ssize_t quota_override_show(struct kobject *kobj, 7492723480aSSargun Dhillon struct kobj_attribute *a, char *buf) 7502723480aSSargun Dhillon { 7512723480aSSargun Dhillon struct btrfs_fs_info *fs_info = to_fs_info(kobj); 7522723480aSSargun Dhillon int quota_override; 7532723480aSSargun Dhillon 7542723480aSSargun Dhillon quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 755abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override); 7562723480aSSargun Dhillon } 7572723480aSSargun Dhillon 7582723480aSSargun Dhillon static ssize_t quota_override_store(struct kobject *kobj, 7592723480aSSargun Dhillon struct kobj_attribute *a, 7602723480aSSargun Dhillon const char *buf, size_t len) 7612723480aSSargun Dhillon { 7622723480aSSargun Dhillon struct btrfs_fs_info *fs_info = to_fs_info(kobj); 7632723480aSSargun Dhillon unsigned long knob; 7642723480aSSargun Dhillon int err; 7652723480aSSargun Dhillon 7662723480aSSargun Dhillon if (!fs_info) 7672723480aSSargun Dhillon return -EPERM; 7682723480aSSargun Dhillon 7692723480aSSargun Dhillon if (!capable(CAP_SYS_RESOURCE)) 7702723480aSSargun Dhillon return -EPERM; 7712723480aSSargun Dhillon 7722723480aSSargun Dhillon err = kstrtoul(buf, 10, &knob); 7732723480aSSargun Dhillon if (err) 7742723480aSSargun Dhillon return err; 7752723480aSSargun Dhillon if (knob > 1) 7762723480aSSargun Dhillon return -EINVAL; 7772723480aSSargun Dhillon 7782723480aSSargun Dhillon if (knob) 7792723480aSSargun Dhillon set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 7802723480aSSargun Dhillon else 7812723480aSSargun Dhillon clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); 7822723480aSSargun Dhillon 7832723480aSSargun Dhillon return len; 7842723480aSSargun Dhillon } 7852723480aSSargun Dhillon 786a969f4ccSHans van Kranenburg BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); 7872723480aSSargun Dhillon 78856f20f40SNikolay Borisov static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, 78956f20f40SNikolay Borisov struct kobj_attribute *a, char *buf) 79056f20f40SNikolay Borisov { 79156f20f40SNikolay Borisov struct btrfs_fs_info *fs_info = to_fs_info(kobj); 79256f20f40SNikolay Borisov 793abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%pU\n", 79456f20f40SNikolay Borisov fs_info->fs_devices->metadata_uuid); 79556f20f40SNikolay Borisov } 79656f20f40SNikolay Borisov 79756f20f40SNikolay Borisov BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); 79856f20f40SNikolay Borisov 79941e6d2a8SJohannes Thumshirn static ssize_t btrfs_checksum_show(struct kobject *kobj, 80041e6d2a8SJohannes Thumshirn struct kobj_attribute *a, char *buf) 80141e6d2a8SJohannes Thumshirn { 80241e6d2a8SJohannes Thumshirn struct btrfs_fs_info *fs_info = to_fs_info(kobj); 80341e6d2a8SJohannes Thumshirn u16 csum_type = btrfs_super_csum_type(fs_info->super_copy); 80441e6d2a8SJohannes Thumshirn 805abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 80641e6d2a8SJohannes Thumshirn btrfs_super_csum_name(csum_type), 80741e6d2a8SJohannes Thumshirn crypto_shash_driver_name(fs_info->csum_shash)); 80841e6d2a8SJohannes Thumshirn } 80941e6d2a8SJohannes Thumshirn 81041e6d2a8SJohannes Thumshirn BTRFS_ATTR(, checksum, btrfs_checksum_show); 81141e6d2a8SJohannes Thumshirn 81266a2823cSGoldwyn Rodrigues static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, 81366a2823cSGoldwyn Rodrigues struct kobj_attribute *a, char *buf) 81466a2823cSGoldwyn Rodrigues { 81566a2823cSGoldwyn Rodrigues struct btrfs_fs_info *fs_info = to_fs_info(kobj); 81666a2823cSGoldwyn Rodrigues const char *str; 81766a2823cSGoldwyn Rodrigues 81866a2823cSGoldwyn Rodrigues switch (READ_ONCE(fs_info->exclusive_operation)) { 81966a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_NONE: 82066a2823cSGoldwyn Rodrigues str = "none\n"; 82166a2823cSGoldwyn Rodrigues break; 82266a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_BALANCE: 82366a2823cSGoldwyn Rodrigues str = "balance\n"; 82466a2823cSGoldwyn Rodrigues break; 82566a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_ADD: 82666a2823cSGoldwyn Rodrigues str = "device add\n"; 82766a2823cSGoldwyn Rodrigues break; 82866a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_REMOVE: 82966a2823cSGoldwyn Rodrigues str = "device remove\n"; 83066a2823cSGoldwyn Rodrigues break; 83166a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_DEV_REPLACE: 83266a2823cSGoldwyn Rodrigues str = "device replace\n"; 83366a2823cSGoldwyn Rodrigues break; 83466a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_RESIZE: 83566a2823cSGoldwyn Rodrigues str = "resize\n"; 83666a2823cSGoldwyn Rodrigues break; 83766a2823cSGoldwyn Rodrigues case BTRFS_EXCLOP_SWAP_ACTIVATE: 83866a2823cSGoldwyn Rodrigues str = "swap activate\n"; 83966a2823cSGoldwyn Rodrigues break; 84066a2823cSGoldwyn Rodrigues default: 84166a2823cSGoldwyn Rodrigues str = "UNKNOWN\n"; 84266a2823cSGoldwyn Rodrigues break; 84366a2823cSGoldwyn Rodrigues } 84466a2823cSGoldwyn Rodrigues return scnprintf(buf, PAGE_SIZE, "%s", str); 84566a2823cSGoldwyn Rodrigues } 84666a2823cSGoldwyn Rodrigues BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); 84766a2823cSGoldwyn Rodrigues 8480dd2906fSAnand Jain static const struct attribute *btrfs_attrs[] = { 849a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, label), 850a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, nodesize), 851a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, sectorsize), 852a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, clone_alignment), 853a969f4ccSHans van Kranenburg BTRFS_ATTR_PTR(, quota_override), 85456f20f40SNikolay Borisov BTRFS_ATTR_PTR(, metadata_uuid), 85541e6d2a8SJohannes Thumshirn BTRFS_ATTR_PTR(, checksum), 85666a2823cSGoldwyn Rodrigues BTRFS_ATTR_PTR(, exclusive_operation), 857f8ba9c11SJeff Mahoney NULL, 858f8ba9c11SJeff Mahoney }; 859f8ba9c11SJeff Mahoney 860c1b7e474SAnand Jain static void btrfs_release_fsid_kobj(struct kobject *kobj) 861510d7360SJeff Mahoney { 8622e7910d6SAnand Jain struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); 863248d200dSAnand Jain 864c1b7e474SAnand Jain memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); 8652e7910d6SAnand Jain complete(&fs_devs->kobj_unregister); 866510d7360SJeff Mahoney } 867510d7360SJeff Mahoney 868510d7360SJeff Mahoney static struct kobj_type btrfs_ktype = { 869510d7360SJeff Mahoney .sysfs_ops = &kobj_sysfs_ops, 870c1b7e474SAnand Jain .release = btrfs_release_fsid_kobj, 871510d7360SJeff Mahoney }; 872510d7360SJeff Mahoney 8732e7910d6SAnand Jain static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) 8742e7910d6SAnand Jain { 8752e7910d6SAnand Jain if (kobj->ktype != &btrfs_ktype) 8762e7910d6SAnand Jain return NULL; 877c1b7e474SAnand Jain return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); 8782e7910d6SAnand Jain } 8792e7910d6SAnand Jain 880510d7360SJeff Mahoney static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) 881510d7360SJeff Mahoney { 882510d7360SJeff Mahoney if (kobj->ktype != &btrfs_ktype) 883510d7360SJeff Mahoney return NULL; 8842e7910d6SAnand Jain return to_fs_devs(kobj)->fs_info; 885510d7360SJeff Mahoney } 88658176a96SJosef Bacik 887e453d989SJeff Mahoney #define NUM_FEATURE_BITS 64 8886c52157fSTomohiro Misono #define BTRFS_FEATURE_NAME_MAX 13 8896c52157fSTomohiro Misono static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; 8906c52157fSTomohiro Misono static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; 891e453d989SJeff Mahoney 8926c52157fSTomohiro Misono static const u64 supported_feature_masks[FEAT_MAX] = { 893e453d989SJeff Mahoney [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, 894e453d989SJeff Mahoney [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, 895e453d989SJeff Mahoney [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, 896e453d989SJeff Mahoney }; 897e453d989SJeff Mahoney 898e453d989SJeff Mahoney static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) 8995ac1d209SJeff Mahoney { 900e453d989SJeff Mahoney int set; 901e453d989SJeff Mahoney 902e453d989SJeff Mahoney for (set = 0; set < FEAT_MAX; set++) { 903e453d989SJeff Mahoney int i; 904e453d989SJeff Mahoney struct attribute *attrs[2]; 905e453d989SJeff Mahoney struct attribute_group agroup = { 906e453d989SJeff Mahoney .name = "features", 907e453d989SJeff Mahoney .attrs = attrs, 908e453d989SJeff Mahoney }; 909e453d989SJeff Mahoney u64 features = get_features(fs_info, set); 910e453d989SJeff Mahoney features &= ~supported_feature_masks[set]; 911e453d989SJeff Mahoney 912e453d989SJeff Mahoney if (!features) 913e453d989SJeff Mahoney continue; 914e453d989SJeff Mahoney 915e453d989SJeff Mahoney attrs[1] = NULL; 916e453d989SJeff Mahoney for (i = 0; i < NUM_FEATURE_BITS; i++) { 917e453d989SJeff Mahoney struct btrfs_feature_attr *fa; 918e453d989SJeff Mahoney 919e453d989SJeff Mahoney if (!(features & (1ULL << i))) 920e453d989SJeff Mahoney continue; 921e453d989SJeff Mahoney 922e453d989SJeff Mahoney fa = &btrfs_feature_attrs[set][i]; 923e453d989SJeff Mahoney attrs[0] = &fa->kobj_attr.attr; 924e453d989SJeff Mahoney if (add) { 925e453d989SJeff Mahoney int ret; 926c1b7e474SAnand Jain ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj, 927e453d989SJeff Mahoney &agroup); 928e453d989SJeff Mahoney if (ret) 929e453d989SJeff Mahoney return ret; 930e453d989SJeff Mahoney } else 931c1b7e474SAnand Jain sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj, 932e453d989SJeff Mahoney &agroup); 933e453d989SJeff Mahoney } 934e453d989SJeff Mahoney 935e453d989SJeff Mahoney } 936e453d989SJeff Mahoney return 0; 937e453d989SJeff Mahoney } 938e453d989SJeff Mahoney 9392e3e1281SAnand Jain static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 940e453d989SJeff Mahoney { 941a013d141SAnand Jain if (fs_devs->devinfo_kobj) { 942a013d141SAnand Jain kobject_del(fs_devs->devinfo_kobj); 943a013d141SAnand Jain kobject_put(fs_devs->devinfo_kobj); 944a013d141SAnand Jain fs_devs->devinfo_kobj = NULL; 945a013d141SAnand Jain } 946a013d141SAnand Jain 947b5501504SAnand Jain if (fs_devs->devices_kobj) { 948b5501504SAnand Jain kobject_del(fs_devs->devices_kobj); 949b5501504SAnand Jain kobject_put(fs_devs->devices_kobj); 950b5501504SAnand Jain fs_devs->devices_kobj = NULL; 951aaf13305SAnand Jain } 952aaf13305SAnand Jain 953c1b7e474SAnand Jain if (fs_devs->fsid_kobj.state_initialized) { 954c1b7e474SAnand Jain kobject_del(&fs_devs->fsid_kobj); 955c1b7e474SAnand Jain kobject_put(&fs_devs->fsid_kobj); 9562e7910d6SAnand Jain wait_for_completion(&fs_devs->kobj_unregister); 9575ac1d209SJeff Mahoney } 958f90fc547SAnand Jain } 9595ac1d209SJeff Mahoney 9602e3e1281SAnand Jain /* when fs_devs is NULL it will remove all fsid kobject */ 9611d1c1be3SAnand Jain void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) 9622e3e1281SAnand Jain { 9632e3e1281SAnand Jain struct list_head *fs_uuids = btrfs_get_fs_uuids(); 9642e3e1281SAnand Jain 9652e3e1281SAnand Jain if (fs_devs) { 9662e3e1281SAnand Jain __btrfs_sysfs_remove_fsid(fs_devs); 9672e3e1281SAnand Jain return; 9682e3e1281SAnand Jain } 9692e3e1281SAnand Jain 970c4babc5eSAnand Jain list_for_each_entry(fs_devs, fs_uuids, fs_list) { 9712e3e1281SAnand Jain __btrfs_sysfs_remove_fsid(fs_devs); 9722e3e1281SAnand Jain } 9732e3e1281SAnand Jain } 9742e3e1281SAnand Jain 97553f8a74cSAnand Jain static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) 97653f8a74cSAnand Jain { 97753f8a74cSAnand Jain struct btrfs_device *device; 97830b0e4e0SAnand Jain struct btrfs_fs_devices *seed; 97953f8a74cSAnand Jain 98053f8a74cSAnand Jain list_for_each_entry(device, &fs_devices->devices, dev_list) 98153f8a74cSAnand Jain btrfs_sysfs_remove_device(device); 98230b0e4e0SAnand Jain 98330b0e4e0SAnand Jain list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 98430b0e4e0SAnand Jain list_for_each_entry(device, &seed->devices, dev_list) 98530b0e4e0SAnand Jain btrfs_sysfs_remove_device(device); 98630b0e4e0SAnand Jain } 98753f8a74cSAnand Jain } 98853f8a74cSAnand Jain 9896618a59bSAnand Jain void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) 990e453d989SJeff Mahoney { 9913092c68fSNikolay Borisov struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 9923092c68fSNikolay Borisov 9933092c68fSNikolay Borisov sysfs_remove_link(fsid_kobj, "bdi"); 9943092c68fSNikolay Borisov 995e453d989SJeff Mahoney if (fs_info->space_info_kobj) { 996e453d989SJeff Mahoney sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); 997e453d989SJeff Mahoney kobject_del(fs_info->space_info_kobj); 998e453d989SJeff Mahoney kobject_put(fs_info->space_info_kobj); 999e453d989SJeff Mahoney } 100071e8978eSDennis Zhou #ifdef CONFIG_BTRFS_DEBUG 1001e4faab84SDennis Zhou if (fs_info->discard_debug_kobj) { 1002e4faab84SDennis Zhou sysfs_remove_files(fs_info->discard_debug_kobj, 1003e4faab84SDennis Zhou discard_debug_attrs); 1004e4faab84SDennis Zhou kobject_del(fs_info->discard_debug_kobj); 1005e4faab84SDennis Zhou kobject_put(fs_info->discard_debug_kobj); 1006e4faab84SDennis Zhou } 100793945cb4SDennis Zhou if (fs_info->debug_kobj) { 100893945cb4SDennis Zhou sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 100993945cb4SDennis Zhou kobject_del(fs_info->debug_kobj); 101093945cb4SDennis Zhou kobject_put(fs_info->debug_kobj); 101193945cb4SDennis Zhou } 101271e8978eSDennis Zhou #endif 1013e453d989SJeff Mahoney addrm_unknown_feature_attrs(fs_info, false); 10143092c68fSNikolay Borisov sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 10153092c68fSNikolay Borisov sysfs_remove_files(fsid_kobj, btrfs_attrs); 101653f8a74cSAnand Jain btrfs_sysfs_remove_fs_devices(fs_info->fs_devices); 1017e453d989SJeff Mahoney } 1018e453d989SJeff Mahoney 1019f10152bcSDavid Sterba static const char * const btrfs_feature_set_names[FEAT_MAX] = { 102079da4fa4SJeff Mahoney [FEAT_COMPAT] = "compat", 102179da4fa4SJeff Mahoney [FEAT_COMPAT_RO] = "compat_ro", 102279da4fa4SJeff Mahoney [FEAT_INCOMPAT] = "incompat", 102379da4fa4SJeff Mahoney }; 102479da4fa4SJeff Mahoney 10259e6df7ceSDavid Sterba const char *btrfs_feature_set_name(enum btrfs_feature_set set) 1026f10152bcSDavid Sterba { 1027f10152bcSDavid Sterba return btrfs_feature_set_names[set]; 1028f10152bcSDavid Sterba } 1029f10152bcSDavid Sterba 10303b02a68aSJeff Mahoney char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) 10313b02a68aSJeff Mahoney { 10323b02a68aSJeff Mahoney size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ 10333b02a68aSJeff Mahoney int len = 0; 10343b02a68aSJeff Mahoney int i; 10353b02a68aSJeff Mahoney char *str; 10363b02a68aSJeff Mahoney 10373b02a68aSJeff Mahoney str = kmalloc(bufsize, GFP_KERNEL); 10383b02a68aSJeff Mahoney if (!str) 10393b02a68aSJeff Mahoney return str; 10403b02a68aSJeff Mahoney 10413b02a68aSJeff Mahoney for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 10423b02a68aSJeff Mahoney const char *name; 10433b02a68aSJeff Mahoney 10443b02a68aSJeff Mahoney if (!(flags & (1ULL << i))) 10453b02a68aSJeff Mahoney continue; 10463b02a68aSJeff Mahoney 10473b02a68aSJeff Mahoney name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; 1048abdd9febSTakashi Iwai len += scnprintf(str + len, bufsize - len, "%s%s", 10493b02a68aSJeff Mahoney len ? "," : "", name); 10503b02a68aSJeff Mahoney } 10513b02a68aSJeff Mahoney 10523b02a68aSJeff Mahoney return str; 10533b02a68aSJeff Mahoney } 10543b02a68aSJeff Mahoney 105579da4fa4SJeff Mahoney static void init_feature_attrs(void) 105679da4fa4SJeff Mahoney { 105779da4fa4SJeff Mahoney struct btrfs_feature_attr *fa; 105879da4fa4SJeff Mahoney int set, i; 105979da4fa4SJeff Mahoney 106079da4fa4SJeff Mahoney BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != 106179da4fa4SJeff Mahoney ARRAY_SIZE(btrfs_feature_attrs)); 106279da4fa4SJeff Mahoney BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != 106379da4fa4SJeff Mahoney ARRAY_SIZE(btrfs_feature_attrs[0])); 106479da4fa4SJeff Mahoney 10653b02a68aSJeff Mahoney memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); 10663b02a68aSJeff Mahoney memset(btrfs_unknown_feature_names, 0, 10673b02a68aSJeff Mahoney sizeof(btrfs_unknown_feature_names)); 10683b02a68aSJeff Mahoney 106979da4fa4SJeff Mahoney for (i = 0; btrfs_supported_feature_attrs[i]; i++) { 107079da4fa4SJeff Mahoney struct btrfs_feature_attr *sfa; 107179da4fa4SJeff Mahoney struct attribute *a = btrfs_supported_feature_attrs[i]; 10723b02a68aSJeff Mahoney int bit; 107379da4fa4SJeff Mahoney sfa = attr_to_btrfs_feature_attr(a); 10743b02a68aSJeff Mahoney bit = ilog2(sfa->feature_bit); 10753b02a68aSJeff Mahoney fa = &btrfs_feature_attrs[sfa->feature_set][bit]; 107679da4fa4SJeff Mahoney 107779da4fa4SJeff Mahoney fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; 107879da4fa4SJeff Mahoney } 107979da4fa4SJeff Mahoney 108079da4fa4SJeff Mahoney for (set = 0; set < FEAT_MAX; set++) { 108179da4fa4SJeff Mahoney for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { 108279da4fa4SJeff Mahoney char *name = btrfs_unknown_feature_names[set][i]; 108379da4fa4SJeff Mahoney fa = &btrfs_feature_attrs[set][i]; 108479da4fa4SJeff Mahoney 108579da4fa4SJeff Mahoney if (fa->kobj_attr.attr.name) 108679da4fa4SJeff Mahoney continue; 108779da4fa4SJeff Mahoney 10886c52157fSTomohiro Misono snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u", 108979da4fa4SJeff Mahoney btrfs_feature_set_names[set], i); 109079da4fa4SJeff Mahoney 109179da4fa4SJeff Mahoney fa->kobj_attr.attr.name = name; 109279da4fa4SJeff Mahoney fa->kobj_attr.attr.mode = S_IRUGO; 109379da4fa4SJeff Mahoney fa->feature_set = set; 109479da4fa4SJeff Mahoney fa->feature_bit = 1ULL << i; 109579da4fa4SJeff Mahoney } 109679da4fa4SJeff Mahoney } 109779da4fa4SJeff Mahoney } 109879da4fa4SJeff Mahoney 109932a9991fSDavid Sterba /* 110032a9991fSDavid Sterba * Create a sysfs entry for a given block group type at path 110132a9991fSDavid Sterba * /sys/fs/btrfs/UUID/allocation/data/TYPE 110232a9991fSDavid Sterba */ 110332da5386SDavid Sterba void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) 110432a9991fSDavid Sterba { 110532a9991fSDavid Sterba struct btrfs_fs_info *fs_info = cache->fs_info; 110632a9991fSDavid Sterba struct btrfs_space_info *space_info = cache->space_info; 110732a9991fSDavid Sterba struct raid_kobject *rkobj; 110832a9991fSDavid Sterba const int index = btrfs_bg_flags_to_raid_index(cache->flags); 110932a9991fSDavid Sterba unsigned int nofs_flag; 111032a9991fSDavid Sterba int ret; 111132a9991fSDavid Sterba 111232a9991fSDavid Sterba /* 111332a9991fSDavid Sterba * Setup a NOFS context because kobject_add(), deep in its call chain, 111432a9991fSDavid Sterba * does GFP_KERNEL allocations, and we are often called in a context 111532a9991fSDavid Sterba * where if reclaim is triggered we can deadlock (we are either holding 111632a9991fSDavid Sterba * a transaction handle or some lock required for a transaction 111732a9991fSDavid Sterba * commit). 111832a9991fSDavid Sterba */ 111932a9991fSDavid Sterba nofs_flag = memalloc_nofs_save(); 112032a9991fSDavid Sterba 112132a9991fSDavid Sterba rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); 112232a9991fSDavid Sterba if (!rkobj) { 112332a9991fSDavid Sterba memalloc_nofs_restore(nofs_flag); 112432a9991fSDavid Sterba btrfs_warn(cache->fs_info, 112532a9991fSDavid Sterba "couldn't alloc memory for raid level kobject"); 112632a9991fSDavid Sterba return; 112732a9991fSDavid Sterba } 112832a9991fSDavid Sterba 112932a9991fSDavid Sterba rkobj->flags = cache->flags; 113032a9991fSDavid Sterba kobject_init(&rkobj->kobj, &btrfs_raid_ktype); 113132a9991fSDavid Sterba ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s", 113232a9991fSDavid Sterba btrfs_bg_type_to_raid_name(rkobj->flags)); 113332a9991fSDavid Sterba memalloc_nofs_restore(nofs_flag); 113432a9991fSDavid Sterba if (ret) { 113532a9991fSDavid Sterba kobject_put(&rkobj->kobj); 113632a9991fSDavid Sterba btrfs_warn(fs_info, 113732a9991fSDavid Sterba "failed to add kobject for block cache, ignoring"); 113832a9991fSDavid Sterba return; 113932a9991fSDavid Sterba } 114032a9991fSDavid Sterba 114132a9991fSDavid Sterba space_info->block_group_kobjs[index] = &rkobj->kobj; 114232a9991fSDavid Sterba } 114332a9991fSDavid Sterba 1144b5865babSDavid Sterba /* 1145b5865babSDavid Sterba * Remove sysfs directories for all block group types of a given space info and 1146b5865babSDavid Sterba * the space info as well 1147b5865babSDavid Sterba */ 1148b5865babSDavid Sterba void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) 1149b5865babSDavid Sterba { 1150b5865babSDavid Sterba int i; 1151b5865babSDavid Sterba 1152b5865babSDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 1153b5865babSDavid Sterba struct kobject *kobj; 1154b5865babSDavid Sterba 1155b5865babSDavid Sterba kobj = space_info->block_group_kobjs[i]; 1156b5865babSDavid Sterba space_info->block_group_kobjs[i] = NULL; 1157b5865babSDavid Sterba if (kobj) { 1158b5865babSDavid Sterba kobject_del(kobj); 1159b5865babSDavid Sterba kobject_put(kobj); 1160b5865babSDavid Sterba } 1161b5865babSDavid Sterba } 1162b5865babSDavid Sterba kobject_del(&space_info->kobj); 1163b5865babSDavid Sterba kobject_put(&space_info->kobj); 1164b5865babSDavid Sterba } 1165b5865babSDavid Sterba 1166b882327aSDavid Sterba static const char *alloc_name(u64 flags) 1167b882327aSDavid Sterba { 1168b882327aSDavid Sterba switch (flags) { 1169b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: 1170b882327aSDavid Sterba return "mixed"; 1171b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_METADATA: 1172b882327aSDavid Sterba return "metadata"; 1173b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_DATA: 1174b882327aSDavid Sterba return "data"; 1175b882327aSDavid Sterba case BTRFS_BLOCK_GROUP_SYSTEM: 1176b882327aSDavid Sterba return "system"; 1177b882327aSDavid Sterba default: 1178b882327aSDavid Sterba WARN_ON(1); 1179b882327aSDavid Sterba return "invalid-combination"; 1180b882327aSDavid Sterba }; 1181b882327aSDavid Sterba } 1182b882327aSDavid Sterba 1183b882327aSDavid Sterba /* 1184b882327aSDavid Sterba * Create a sysfs entry for a space info type at path 1185b882327aSDavid Sterba * /sys/fs/btrfs/UUID/allocation/TYPE 1186b882327aSDavid Sterba */ 1187b882327aSDavid Sterba int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info, 1188b882327aSDavid Sterba struct btrfs_space_info *space_info) 1189b882327aSDavid Sterba { 1190b882327aSDavid Sterba int ret; 1191b882327aSDavid Sterba 1192b882327aSDavid Sterba ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype, 1193b882327aSDavid Sterba fs_info->space_info_kobj, "%s", 1194b882327aSDavid Sterba alloc_name(space_info->flags)); 1195b882327aSDavid Sterba if (ret) { 1196b882327aSDavid Sterba kobject_put(&space_info->kobj); 1197b882327aSDavid Sterba return ret; 1198b882327aSDavid Sterba } 1199b882327aSDavid Sterba 1200b882327aSDavid Sterba return 0; 1201b882327aSDavid Sterba } 1202b882327aSDavid Sterba 120353f8a74cSAnand Jain void btrfs_sysfs_remove_device(struct btrfs_device *device) 120499994cdeSAnand Jain { 120599994cdeSAnand Jain struct hd_struct *disk; 120699994cdeSAnand Jain struct kobject *disk_kobj; 1207985e233eSAnand Jain struct kobject *devices_kobj; 120899994cdeSAnand Jain 1209985e233eSAnand Jain /* 1210985e233eSAnand Jain * Seed fs_devices devices_kobj aren't used, fetch kobject from the 1211985e233eSAnand Jain * fs_info::fs_devices. 1212985e233eSAnand Jain */ 1213985e233eSAnand Jain devices_kobj = device->fs_info->fs_devices->devices_kobj; 1214985e233eSAnand Jain ASSERT(devices_kobj); 121599994cdeSAnand Jain 1216985e233eSAnand Jain if (device->bdev) { 1217985e233eSAnand Jain disk = device->bdev->bd_part; 121899994cdeSAnand Jain disk_kobj = &part_to_dev(disk)->kobj; 1219985e233eSAnand Jain sysfs_remove_link(devices_kobj, disk_kobj->name); 122099994cdeSAnand Jain } 122199994cdeSAnand Jain 1222985e233eSAnand Jain if (device->devid_kobj.state_initialized) { 1223985e233eSAnand Jain kobject_del(&device->devid_kobj); 1224985e233eSAnand Jain kobject_put(&device->devid_kobj); 1225985e233eSAnand Jain wait_for_completion(&device->kobj_unregister); 1226985e233eSAnand Jain } 1227b5ddcffaSAnand Jain } 1228668e48afSAnand Jain 1229668e48afSAnand Jain static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, 1230668e48afSAnand Jain struct kobj_attribute *a, 1231668e48afSAnand Jain char *buf) 1232668e48afSAnand Jain { 1233668e48afSAnand Jain int val; 1234668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1235668e48afSAnand Jain devid_kobj); 1236668e48afSAnand Jain 1237668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); 1238668e48afSAnand Jain 1239abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1240668e48afSAnand Jain } 1241668e48afSAnand Jain BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); 1242668e48afSAnand Jain 124325864778SAnand Jain static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, 1244668e48afSAnand Jain struct kobj_attribute *a, char *buf) 1245668e48afSAnand Jain { 1246668e48afSAnand Jain int val; 1247668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1248668e48afSAnand Jain devid_kobj); 1249668e48afSAnand Jain 1250668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); 1251668e48afSAnand Jain 1252abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1253668e48afSAnand Jain } 125425864778SAnand Jain BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); 1255668e48afSAnand Jain 1256668e48afSAnand Jain static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, 1257668e48afSAnand Jain struct kobj_attribute *a, 1258668e48afSAnand Jain char *buf) 1259668e48afSAnand Jain { 1260668e48afSAnand Jain int val; 1261668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1262668e48afSAnand Jain devid_kobj); 1263668e48afSAnand Jain 1264668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); 1265668e48afSAnand Jain 1266abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1267668e48afSAnand Jain } 1268668e48afSAnand Jain BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); 1269668e48afSAnand Jain 1270668e48afSAnand Jain static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, 1271668e48afSAnand Jain struct kobj_attribute *a, char *buf) 1272668e48afSAnand Jain { 1273668e48afSAnand Jain int val; 1274668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1275668e48afSAnand Jain devid_kobj); 1276668e48afSAnand Jain 1277668e48afSAnand Jain val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); 1278668e48afSAnand Jain 1279abdd9febSTakashi Iwai return scnprintf(buf, PAGE_SIZE, "%d\n", val); 1280668e48afSAnand Jain } 1281668e48afSAnand Jain BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); 1282668e48afSAnand Jain 1283668e48afSAnand Jain static struct attribute *devid_attrs[] = { 1284668e48afSAnand Jain BTRFS_ATTR_PTR(devid, in_fs_metadata), 1285668e48afSAnand Jain BTRFS_ATTR_PTR(devid, missing), 1286668e48afSAnand Jain BTRFS_ATTR_PTR(devid, replace_target), 1287668e48afSAnand Jain BTRFS_ATTR_PTR(devid, writeable), 1288668e48afSAnand Jain NULL 1289668e48afSAnand Jain }; 1290668e48afSAnand Jain ATTRIBUTE_GROUPS(devid); 1291668e48afSAnand Jain 1292668e48afSAnand Jain static void btrfs_release_devid_kobj(struct kobject *kobj) 1293668e48afSAnand Jain { 1294668e48afSAnand Jain struct btrfs_device *device = container_of(kobj, struct btrfs_device, 1295668e48afSAnand Jain devid_kobj); 1296668e48afSAnand Jain 1297668e48afSAnand Jain memset(&device->devid_kobj, 0, sizeof(struct kobject)); 1298668e48afSAnand Jain complete(&device->kobj_unregister); 1299668e48afSAnand Jain } 1300668e48afSAnand Jain 1301668e48afSAnand Jain static struct kobj_type devid_ktype = { 1302668e48afSAnand Jain .sysfs_ops = &kobj_sysfs_ops, 1303668e48afSAnand Jain .default_groups = devid_groups, 1304668e48afSAnand Jain .release = btrfs_release_devid_kobj, 1305668e48afSAnand Jain }; 1306668e48afSAnand Jain 1307cd36da2eSAnand Jain int btrfs_sysfs_add_device(struct btrfs_device *device) 130829e5be24SJeff Mahoney { 1309178a16c9SAnand Jain int ret; 1310a47bd78dSJosef Bacik unsigned int nofs_flag; 1311178a16c9SAnand Jain struct kobject *devices_kobj; 1312178a16c9SAnand Jain struct kobject *devinfo_kobj; 1313178a16c9SAnand Jain 1314178a16c9SAnand Jain /* 1315178a16c9SAnand Jain * Make sure we use the fs_info::fs_devices to fetch the kobjects even 1316178a16c9SAnand Jain * for the seed fs_devices 1317178a16c9SAnand Jain */ 1318178a16c9SAnand Jain devices_kobj = device->fs_info->fs_devices->devices_kobj; 1319178a16c9SAnand Jain devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; 1320178a16c9SAnand Jain ASSERT(devices_kobj); 1321178a16c9SAnand Jain ASSERT(devinfo_kobj); 132229e5be24SJeff Mahoney 1323a47bd78dSJosef Bacik nofs_flag = memalloc_nofs_save(); 1324f085381eSAnand Jain 1325178a16c9SAnand Jain if (device->bdev) { 1326668e48afSAnand Jain struct hd_struct *disk; 1327668e48afSAnand Jain struct kobject *disk_kobj; 1328668e48afSAnand Jain 1329178a16c9SAnand Jain disk = device->bdev->bd_part; 1330f085381eSAnand Jain disk_kobj = &part_to_dev(disk)->kobj; 133129e5be24SJeff Mahoney 1332178a16c9SAnand Jain ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name); 1333178a16c9SAnand Jain if (ret) { 1334178a16c9SAnand Jain btrfs_warn(device->fs_info, 1335178a16c9SAnand Jain "creating sysfs device link for devid %llu failed: %d", 1336178a16c9SAnand Jain device->devid, ret); 1337178a16c9SAnand Jain goto out; 1338178a16c9SAnand Jain } 133929e5be24SJeff Mahoney } 134029e5be24SJeff Mahoney 1341178a16c9SAnand Jain init_completion(&device->kobj_unregister); 1342178a16c9SAnand Jain ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype, 1343178a16c9SAnand Jain devinfo_kobj, "%llu", device->devid); 1344178a16c9SAnand Jain if (ret) { 1345178a16c9SAnand Jain kobject_put(&device->devid_kobj); 1346178a16c9SAnand Jain btrfs_warn(device->fs_info, 1347178a16c9SAnand Jain "devinfo init for devid %llu failed: %d", 1348178a16c9SAnand Jain device->devid, ret); 1349668e48afSAnand Jain } 1350178a16c9SAnand Jain 1351178a16c9SAnand Jain out: 1352a47bd78dSJosef Bacik memalloc_nofs_restore(nofs_flag); 1353178a16c9SAnand Jain return ret; 1354178a16c9SAnand Jain } 1355668e48afSAnand Jain 1356cd36da2eSAnand Jain static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) 1357178a16c9SAnand Jain { 1358178a16c9SAnand Jain int ret; 1359cd36da2eSAnand Jain struct btrfs_device *device; 136030b0e4e0SAnand Jain struct btrfs_fs_devices *seed; 1361178a16c9SAnand Jain 1362178a16c9SAnand Jain list_for_each_entry(device, &fs_devices->devices, dev_list) { 1363178a16c9SAnand Jain ret = btrfs_sysfs_add_device(device); 1364178a16c9SAnand Jain if (ret) 13657ad3912aSAnand Jain goto fail; 1366178a16c9SAnand Jain } 1367178a16c9SAnand Jain 136830b0e4e0SAnand Jain list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { 136930b0e4e0SAnand Jain list_for_each_entry(device, &seed->devices, dev_list) { 137030b0e4e0SAnand Jain ret = btrfs_sysfs_add_device(device); 137130b0e4e0SAnand Jain if (ret) 13727ad3912aSAnand Jain goto fail; 137330b0e4e0SAnand Jain } 137430b0e4e0SAnand Jain } 137530b0e4e0SAnand Jain 1376178a16c9SAnand Jain return 0; 13777ad3912aSAnand Jain 13787ad3912aSAnand Jain fail: 13797ad3912aSAnand Jain btrfs_sysfs_remove_fs_devices(fs_devices); 13807ad3912aSAnand Jain return ret; 138129e5be24SJeff Mahoney } 138229e5be24SJeff Mahoney 13835b28692eSDavid Sterba void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) 13845b28692eSDavid Sterba { 13855b28692eSDavid Sterba int ret; 13865b28692eSDavid Sterba 13875b28692eSDavid Sterba ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); 13885b28692eSDavid Sterba if (ret) 13895b28692eSDavid Sterba pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", 13905b28692eSDavid Sterba action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), 13915b28692eSDavid Sterba &disk_to_dev(bdev->bd_disk)->kobj); 13925b28692eSDavid Sterba } 13935b28692eSDavid Sterba 13948e560081SNikolay Borisov void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) 13958e560081SNikolay Borisov 1396f93c3997SDavid Sterba { 1397f93c3997SDavid Sterba char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 1398f93c3997SDavid Sterba 1399f93c3997SDavid Sterba /* 1400f93c3997SDavid Sterba * Sprouting changes fsid of the mounted filesystem, rename the fsid 1401f93c3997SDavid Sterba * directory 1402f93c3997SDavid Sterba */ 14038e560081SNikolay Borisov snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid); 1404f93c3997SDavid Sterba if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) 1405f93c3997SDavid Sterba btrfs_warn(fs_devices->fs_info, 1406f93c3997SDavid Sterba "sysfs: failed to create fsid for sprout"); 1407f93c3997SDavid Sterba } 1408f93c3997SDavid Sterba 1409668e48afSAnand Jain void btrfs_sysfs_update_devid(struct btrfs_device *device) 1410668e48afSAnand Jain { 1411668e48afSAnand Jain char tmp[24]; 1412668e48afSAnand Jain 1413668e48afSAnand Jain snprintf(tmp, sizeof(tmp), "%llu", device->devid); 1414668e48afSAnand Jain 1415668e48afSAnand Jain if (kobject_rename(&device->devid_kobj, tmp)) 1416668e48afSAnand Jain btrfs_warn(device->fs_devices->fs_info, 1417668e48afSAnand Jain "sysfs: failed to update devid for %llu", 1418668e48afSAnand Jain device->devid); 1419668e48afSAnand Jain } 1420668e48afSAnand Jain 1421510d7360SJeff Mahoney /* /sys/fs/btrfs/ entry */ 1422510d7360SJeff Mahoney static struct kset *btrfs_kset; 1423510d7360SJeff Mahoney 142472059215SAnand Jain /* 1425c6761a9eSAnand Jain * Creates: 1426c6761a9eSAnand Jain * /sys/fs/btrfs/UUID 1427c6761a9eSAnand Jain * 142872059215SAnand Jain * Can be called by the device discovery thread. 142972059215SAnand Jain */ 1430c6761a9eSAnand Jain int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) 14315ac1d209SJeff Mahoney { 14325ac1d209SJeff Mahoney int error; 14335ac1d209SJeff Mahoney 14342e7910d6SAnand Jain init_completion(&fs_devs->kobj_unregister); 1435c1b7e474SAnand Jain fs_devs->fsid_kobj.kset = btrfs_kset; 1436c6761a9eSAnand Jain error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL, 1437c6761a9eSAnand Jain "%pU", fs_devs->fsid); 1438e3277335STobin C. Harding if (error) { 1439e3277335STobin C. Harding kobject_put(&fs_devs->fsid_kobj); 144072059215SAnand Jain return error; 144172059215SAnand Jain } 144272059215SAnand Jain 1443bc036bb3SAnand Jain fs_devs->devices_kobj = kobject_create_and_add("devices", 1444bc036bb3SAnand Jain &fs_devs->fsid_kobj); 1445bc036bb3SAnand Jain if (!fs_devs->devices_kobj) { 1446bc036bb3SAnand Jain btrfs_err(fs_devs->fs_info, 1447bc036bb3SAnand Jain "failed to init sysfs device interface"); 14481f6087e6SAnand Jain btrfs_sysfs_remove_fsid(fs_devs); 1449bc036bb3SAnand Jain return -ENOMEM; 1450bc036bb3SAnand Jain } 1451bc036bb3SAnand Jain 1452a013d141SAnand Jain fs_devs->devinfo_kobj = kobject_create_and_add("devinfo", 1453a013d141SAnand Jain &fs_devs->fsid_kobj); 1454a013d141SAnand Jain if (!fs_devs->devinfo_kobj) { 1455a013d141SAnand Jain btrfs_err(fs_devs->fs_info, 1456a013d141SAnand Jain "failed to init sysfs devinfo kobject"); 1457a013d141SAnand Jain btrfs_sysfs_remove_fsid(fs_devs); 1458a013d141SAnand Jain return -ENOMEM; 1459a013d141SAnand Jain } 1460a013d141SAnand Jain 1461e3277335STobin C. Harding return 0; 1462e3277335STobin C. Harding } 1463e3277335STobin C. Harding 146496f3136eSAnand Jain int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) 146572059215SAnand Jain { 146672059215SAnand Jain int error; 14672e7910d6SAnand Jain struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 1468c1b7e474SAnand Jain struct kobject *fsid_kobj = &fs_devs->fsid_kobj; 146972059215SAnand Jain 1470cd36da2eSAnand Jain error = btrfs_sysfs_add_fs_devices(fs_devs); 1471e453d989SJeff Mahoney if (error) 1472e453d989SJeff Mahoney return error; 1473510d7360SJeff Mahoney 1474c1b7e474SAnand Jain error = sysfs_create_files(fsid_kobj, btrfs_attrs); 1475e453d989SJeff Mahoney if (error) { 147653f8a74cSAnand Jain btrfs_sysfs_remove_fs_devices(fs_devs); 1477e453d989SJeff Mahoney return error; 1478e453d989SJeff Mahoney } 147979da4fa4SJeff Mahoney 1480c1b7e474SAnand Jain error = sysfs_create_group(fsid_kobj, 14810dd2906fSAnand Jain &btrfs_feature_attr_group); 14820dd2906fSAnand Jain if (error) 14830dd2906fSAnand Jain goto failure; 14840dd2906fSAnand Jain 14856e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 148693945cb4SDennis Zhou fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj); 148793945cb4SDennis Zhou if (!fs_info->debug_kobj) { 148893945cb4SDennis Zhou error = -ENOMEM; 148993945cb4SDennis Zhou goto failure; 149093945cb4SDennis Zhou } 149193945cb4SDennis Zhou 149293945cb4SDennis Zhou error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs); 14936e369febSDavid Sterba if (error) 14946e369febSDavid Sterba goto failure; 1495e4faab84SDennis Zhou 1496e4faab84SDennis Zhou /* Discard directory */ 1497e4faab84SDennis Zhou fs_info->discard_debug_kobj = kobject_create_and_add("discard", 1498e4faab84SDennis Zhou fs_info->debug_kobj); 1499e4faab84SDennis Zhou if (!fs_info->discard_debug_kobj) { 1500e4faab84SDennis Zhou error = -ENOMEM; 1501e4faab84SDennis Zhou goto failure; 1502e4faab84SDennis Zhou } 1503e4faab84SDennis Zhou 1504e4faab84SDennis Zhou error = sysfs_create_files(fs_info->discard_debug_kobj, 1505e4faab84SDennis Zhou discard_debug_attrs); 1506e4faab84SDennis Zhou if (error) 1507e4faab84SDennis Zhou goto failure; 15086e369febSDavid Sterba #endif 15096e369febSDavid Sterba 1510e453d989SJeff Mahoney error = addrm_unknown_feature_attrs(fs_info, true); 151179da4fa4SJeff Mahoney if (error) 151279da4fa4SJeff Mahoney goto failure; 151379da4fa4SJeff Mahoney 15143092c68fSNikolay Borisov error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi"); 15153092c68fSNikolay Borisov if (error) 15163092c68fSNikolay Borisov goto failure; 15173092c68fSNikolay Borisov 15186ab0a202SJeff Mahoney fs_info->space_info_kobj = kobject_create_and_add("allocation", 1519c1b7e474SAnand Jain fsid_kobj); 15206ab0a202SJeff Mahoney if (!fs_info->space_info_kobj) { 15216ab0a202SJeff Mahoney error = -ENOMEM; 15226ab0a202SJeff Mahoney goto failure; 15236ab0a202SJeff Mahoney } 15246ab0a202SJeff Mahoney 15256ab0a202SJeff Mahoney error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); 15266ab0a202SJeff Mahoney if (error) 15276ab0a202SJeff Mahoney goto failure; 15286ab0a202SJeff Mahoney 152979da4fa4SJeff Mahoney return 0; 153079da4fa4SJeff Mahoney failure: 15316618a59bSAnand Jain btrfs_sysfs_remove_mounted(fs_info); 15325ac1d209SJeff Mahoney return error; 15335ac1d209SJeff Mahoney } 15345ac1d209SJeff Mahoney 153549e5fb46SQu Wenruo static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) 153649e5fb46SQu Wenruo { 153749e5fb46SQu Wenruo return to_fs_info(kobj->parent->parent); 153849e5fb46SQu Wenruo } 153949e5fb46SQu Wenruo 154049e5fb46SQu Wenruo #define QGROUP_ATTR(_member, _show_name) \ 154149e5fb46SQu Wenruo static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ 154249e5fb46SQu Wenruo struct kobj_attribute *a, \ 154349e5fb46SQu Wenruo char *buf) \ 154449e5fb46SQu Wenruo { \ 154549e5fb46SQu Wenruo struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 154649e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 154749e5fb46SQu Wenruo struct btrfs_qgroup, kobj); \ 154849e5fb46SQu Wenruo return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ 154949e5fb46SQu Wenruo } \ 155049e5fb46SQu Wenruo BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) 155149e5fb46SQu Wenruo 155249e5fb46SQu Wenruo #define QGROUP_RSV_ATTR(_name, _type) \ 155349e5fb46SQu Wenruo static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ 155449e5fb46SQu Wenruo struct kobj_attribute *a, \ 155549e5fb46SQu Wenruo char *buf) \ 155649e5fb46SQu Wenruo { \ 155749e5fb46SQu Wenruo struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ 155849e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ 155949e5fb46SQu Wenruo struct btrfs_qgroup, kobj); \ 156049e5fb46SQu Wenruo return btrfs_show_u64(&qgroup->rsv.values[_type], \ 156149e5fb46SQu Wenruo &fs_info->qgroup_lock, buf); \ 156249e5fb46SQu Wenruo } \ 156349e5fb46SQu Wenruo BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) 156449e5fb46SQu Wenruo 156549e5fb46SQu Wenruo QGROUP_ATTR(rfer, referenced); 156649e5fb46SQu Wenruo QGROUP_ATTR(excl, exclusive); 156749e5fb46SQu Wenruo QGROUP_ATTR(max_rfer, max_referenced); 156849e5fb46SQu Wenruo QGROUP_ATTR(max_excl, max_exclusive); 156949e5fb46SQu Wenruo QGROUP_ATTR(lim_flags, limit_flags); 157049e5fb46SQu Wenruo QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); 157149e5fb46SQu Wenruo QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); 157249e5fb46SQu Wenruo QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); 157349e5fb46SQu Wenruo 157449e5fb46SQu Wenruo static struct attribute *qgroup_attrs[] = { 157549e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, referenced), 157649e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, exclusive), 157749e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, max_referenced), 157849e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, max_exclusive), 157949e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, limit_flags), 158049e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_data), 158149e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), 158249e5fb46SQu Wenruo BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), 158349e5fb46SQu Wenruo NULL 158449e5fb46SQu Wenruo }; 158549e5fb46SQu Wenruo ATTRIBUTE_GROUPS(qgroup); 158649e5fb46SQu Wenruo 158749e5fb46SQu Wenruo static void qgroup_release(struct kobject *kobj) 158849e5fb46SQu Wenruo { 158949e5fb46SQu Wenruo struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); 159049e5fb46SQu Wenruo 159149e5fb46SQu Wenruo memset(&qgroup->kobj, 0, sizeof(*kobj)); 159249e5fb46SQu Wenruo } 159349e5fb46SQu Wenruo 159449e5fb46SQu Wenruo static struct kobj_type qgroup_ktype = { 159549e5fb46SQu Wenruo .sysfs_ops = &kobj_sysfs_ops, 159649e5fb46SQu Wenruo .release = qgroup_release, 159749e5fb46SQu Wenruo .default_groups = qgroup_groups, 159849e5fb46SQu Wenruo }; 159949e5fb46SQu Wenruo 160049e5fb46SQu Wenruo int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, 160149e5fb46SQu Wenruo struct btrfs_qgroup *qgroup) 160249e5fb46SQu Wenruo { 160349e5fb46SQu Wenruo struct kobject *qgroups_kobj = fs_info->qgroups_kobj; 160449e5fb46SQu Wenruo int ret; 160549e5fb46SQu Wenruo 160649e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 160749e5fb46SQu Wenruo return 0; 160849e5fb46SQu Wenruo if (qgroup->kobj.state_initialized) 160949e5fb46SQu Wenruo return 0; 161049e5fb46SQu Wenruo if (!qgroups_kobj) 161149e5fb46SQu Wenruo return -EINVAL; 161249e5fb46SQu Wenruo 161349e5fb46SQu Wenruo ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj, 161449e5fb46SQu Wenruo "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid), 161549e5fb46SQu Wenruo btrfs_qgroup_subvolid(qgroup->qgroupid)); 161649e5fb46SQu Wenruo if (ret < 0) 161749e5fb46SQu Wenruo kobject_put(&qgroup->kobj); 161849e5fb46SQu Wenruo 161949e5fb46SQu Wenruo return ret; 162049e5fb46SQu Wenruo } 162149e5fb46SQu Wenruo 162249e5fb46SQu Wenruo void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) 162349e5fb46SQu Wenruo { 162449e5fb46SQu Wenruo struct btrfs_qgroup *qgroup; 162549e5fb46SQu Wenruo struct btrfs_qgroup *next; 162649e5fb46SQu Wenruo 162749e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 162849e5fb46SQu Wenruo return; 162949e5fb46SQu Wenruo 163049e5fb46SQu Wenruo rbtree_postorder_for_each_entry_safe(qgroup, next, 163149e5fb46SQu Wenruo &fs_info->qgroup_tree, node) 163249e5fb46SQu Wenruo btrfs_sysfs_del_one_qgroup(fs_info, qgroup); 163362ab2cc0SQu Wenruo if (fs_info->qgroups_kobj) { 163449e5fb46SQu Wenruo kobject_del(fs_info->qgroups_kobj); 163549e5fb46SQu Wenruo kobject_put(fs_info->qgroups_kobj); 163649e5fb46SQu Wenruo fs_info->qgroups_kobj = NULL; 163749e5fb46SQu Wenruo } 163862ab2cc0SQu Wenruo } 163949e5fb46SQu Wenruo 164049e5fb46SQu Wenruo /* Called when qgroups get initialized, thus there is no need for locking */ 164149e5fb46SQu Wenruo int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) 164249e5fb46SQu Wenruo { 164349e5fb46SQu Wenruo struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; 164449e5fb46SQu Wenruo struct btrfs_qgroup *qgroup; 164549e5fb46SQu Wenruo struct btrfs_qgroup *next; 164649e5fb46SQu Wenruo int ret = 0; 164749e5fb46SQu Wenruo 164849e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 164949e5fb46SQu Wenruo return 0; 165049e5fb46SQu Wenruo 165149e5fb46SQu Wenruo ASSERT(fsid_kobj); 165249e5fb46SQu Wenruo if (fs_info->qgroups_kobj) 165349e5fb46SQu Wenruo return 0; 165449e5fb46SQu Wenruo 165549e5fb46SQu Wenruo fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj); 165649e5fb46SQu Wenruo if (!fs_info->qgroups_kobj) { 165749e5fb46SQu Wenruo ret = -ENOMEM; 165849e5fb46SQu Wenruo goto out; 165949e5fb46SQu Wenruo } 166049e5fb46SQu Wenruo rbtree_postorder_for_each_entry_safe(qgroup, next, 166149e5fb46SQu Wenruo &fs_info->qgroup_tree, node) { 166249e5fb46SQu Wenruo ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); 166349e5fb46SQu Wenruo if (ret < 0) 166449e5fb46SQu Wenruo goto out; 166549e5fb46SQu Wenruo } 166649e5fb46SQu Wenruo 166749e5fb46SQu Wenruo out: 166849e5fb46SQu Wenruo if (ret < 0) 166949e5fb46SQu Wenruo btrfs_sysfs_del_qgroups(fs_info); 167049e5fb46SQu Wenruo return ret; 167149e5fb46SQu Wenruo } 167249e5fb46SQu Wenruo 167349e5fb46SQu Wenruo void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, 167449e5fb46SQu Wenruo struct btrfs_qgroup *qgroup) 167549e5fb46SQu Wenruo { 167649e5fb46SQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state)) 167749e5fb46SQu Wenruo return; 167849e5fb46SQu Wenruo 167949e5fb46SQu Wenruo if (qgroup->kobj.state_initialized) { 168049e5fb46SQu Wenruo kobject_del(&qgroup->kobj); 168149e5fb46SQu Wenruo kobject_put(&qgroup->kobj); 168249e5fb46SQu Wenruo } 168349e5fb46SQu Wenruo } 1684444e7516SDavid Sterba 1685444e7516SDavid Sterba /* 1686444e7516SDavid Sterba * Change per-fs features in /sys/fs/btrfs/UUID/features to match current 1687444e7516SDavid Sterba * values in superblock. Call after any changes to incompat/compat_ro flags 1688444e7516SDavid Sterba */ 1689444e7516SDavid Sterba void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info, 1690444e7516SDavid Sterba u64 bit, enum btrfs_feature_set set) 1691444e7516SDavid Sterba { 1692444e7516SDavid Sterba struct btrfs_fs_devices *fs_devs; 1693444e7516SDavid Sterba struct kobject *fsid_kobj; 169424646481SLeon Romanovsky u64 __maybe_unused features; 169524646481SLeon Romanovsky int __maybe_unused ret; 1696444e7516SDavid Sterba 1697444e7516SDavid Sterba if (!fs_info) 1698444e7516SDavid Sterba return; 1699444e7516SDavid Sterba 170024646481SLeon Romanovsky /* 170124646481SLeon Romanovsky * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not 170224646481SLeon Romanovsky * safe when called from some contexts (eg. balance) 170324646481SLeon Romanovsky */ 1704444e7516SDavid Sterba features = get_features(fs_info, set); 1705444e7516SDavid Sterba ASSERT(bit & supported_feature_masks[set]); 1706444e7516SDavid Sterba 1707444e7516SDavid Sterba fs_devs = fs_info->fs_devices; 1708444e7516SDavid Sterba fsid_kobj = &fs_devs->fsid_kobj; 1709444e7516SDavid Sterba 1710bf609206SDavid Sterba if (!fsid_kobj->state_initialized) 1711bf609206SDavid Sterba return; 1712bf609206SDavid Sterba 1713444e7516SDavid Sterba /* 1714444e7516SDavid Sterba * FIXME: this is too heavy to update just one value, ideally we'd like 1715444e7516SDavid Sterba * to use sysfs_update_group but some refactoring is needed first. 1716444e7516SDavid Sterba */ 1717444e7516SDavid Sterba sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group); 1718444e7516SDavid Sterba ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group); 1719444e7516SDavid Sterba } 1720444e7516SDavid Sterba 1721f5c29bd9SLiu Bo int __init btrfs_init_sysfs(void) 172258176a96SJosef Bacik { 1723079b72bcSJeff Mahoney int ret; 17241bae3098SDavid Sterba 1725e3fe4e71SGreg KH btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); 1726e3fe4e71SGreg KH if (!btrfs_kset) 1727e3fe4e71SGreg KH return -ENOMEM; 1728079b72bcSJeff Mahoney 17291bae3098SDavid Sterba init_feature_attrs(); 17301bae3098SDavid Sterba ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1731001a648dSFilipe Manana if (ret) 1732001a648dSFilipe Manana goto out2; 1733f902bd3aSMisono Tomohiro ret = sysfs_merge_group(&btrfs_kset->kobj, 1734f902bd3aSMisono Tomohiro &btrfs_static_feature_attr_group); 1735f902bd3aSMisono Tomohiro if (ret) 1736f902bd3aSMisono Tomohiro goto out_remove_group; 1737001a648dSFilipe Manana 17386e369febSDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 17396e369febSDavid Sterba ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 17406e369febSDavid Sterba if (ret) 17416e369febSDavid Sterba goto out2; 17426e369febSDavid Sterba #endif 17436e369febSDavid Sterba 1744001a648dSFilipe Manana return 0; 1745f902bd3aSMisono Tomohiro 1746f902bd3aSMisono Tomohiro out_remove_group: 1747f902bd3aSMisono Tomohiro sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 1748001a648dSFilipe Manana out2: 1749001a648dSFilipe Manana kset_unregister(btrfs_kset); 17501bae3098SDavid Sterba 17511bae3098SDavid Sterba return ret; 175258176a96SJosef Bacik } 175358176a96SJosef Bacik 1754e67c718bSDavid Sterba void __cold btrfs_exit_sysfs(void) 175558176a96SJosef Bacik { 1756f902bd3aSMisono Tomohiro sysfs_unmerge_group(&btrfs_kset->kobj, 1757f902bd3aSMisono Tomohiro &btrfs_static_feature_attr_group); 1758079b72bcSJeff Mahoney sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); 175971e8978eSDennis Zhou #ifdef CONFIG_BTRFS_DEBUG 176071e8978eSDennis Zhou sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group); 176171e8978eSDennis Zhou #endif 1762e3fe4e71SGreg KH kset_unregister(btrfs_kset); 176358176a96SJosef Bacik } 176455d47414SChris Mason 1765