LCOV - code coverage report
Current view: top level - fs/btrfs - sysfs.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 41 851 4.8 %
Date: 2023-07-31 20:08:07 Functions: 3 109 2.8 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (C) 2007 Oracle.  All rights reserved.
       4             :  */
       5             : 
       6             : #include <linux/sched.h>
       7             : #include <linux/sched/mm.h>
       8             : #include <linux/slab.h>
       9             : #include <linux/spinlock.h>
      10             : #include <linux/completion.h>
      11             : #include <linux/bug.h>
      12             : #include <linux/list.h>
      13             : #include <crypto/hash.h>
      14             : #include "messages.h"
      15             : #include "ctree.h"
      16             : #include "discard.h"
      17             : #include "disk-io.h"
      18             : #include "send.h"
      19             : #include "transaction.h"
      20             : #include "sysfs.h"
      21             : #include "volumes.h"
      22             : #include "space-info.h"
      23             : #include "block-group.h"
      24             : #include "qgroup.h"
      25             : #include "misc.h"
      26             : #include "fs.h"
      27             : #include "accessors.h"
      28             : 
      29             : /*
      30             :  * Structure name                       Path
      31             :  * --------------------------------------------------------------------------
      32             :  * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
      33             :  * btrfs_supported_feature_attrs        /sys/fs/btrfs/features and
      34             :  *                                      /sys/fs/btrfs/<uuid>/features
      35             :  * btrfs_attrs                          /sys/fs/btrfs/<uuid>
      36             :  * devid_attrs                          /sys/fs/btrfs/<uuid>/devinfo/<devid>
      37             :  * allocation_attrs                     /sys/fs/btrfs/<uuid>/allocation
      38             :  * qgroup_attrs                         /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
      39             :  * space_info_attrs                     /sys/fs/btrfs/<uuid>/allocation/<bg-type>
      40             :  * raid_attrs                           /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
      41             :  * discard_attrs                        /sys/fs/btrfs/<uuid>/discard
      42             :  *
      43             :  * When built with BTRFS_CONFIG_DEBUG:
      44             :  *
      45             :  * btrfs_debug_feature_attrs            /sys/fs/btrfs/debug
      46             :  * btrfs_debug_mount_attrs              /sys/fs/btrfs/<uuid>/debug
      47             :  */
      48             : 
      49             : struct btrfs_feature_attr {
      50             :         struct kobj_attribute kobj_attr;
      51             :         enum btrfs_feature_set feature_set;
      52             :         u64 feature_bit;
      53             : };
      54             : 
      55             : /* For raid type sysfs entries */
      56             : struct raid_kobject {
      57             :         u64 flags;
      58             :         struct kobject kobj;
      59             : };
      60             : 
      61             : #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)                   \
      62             : {                                                                       \
      63             :         .attr   = { .name = __stringify(_name), .mode = _mode },        \
      64             :         .show   = _show,                                                \
      65             :         .store  = _store,                                               \
      66             : }
      67             : 
      68             : #define BTRFS_ATTR_W(_prefix, _name, _store)                            \
      69             :         static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
      70             :                         __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
      71             : 
      72             : #define BTRFS_ATTR_RW(_prefix, _name, _show, _store)                    \
      73             :         static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
      74             :                         __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
      75             : 
      76             : #define BTRFS_ATTR(_prefix, _name, _show)                               \
      77             :         static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
      78             :                         __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
      79             : 
      80             : #define BTRFS_ATTR_PTR(_prefix, _name)                                  \
      81             :         (&btrfs_attr_##_prefix##_##_name.attr)
      82             : 
      83             : #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
      84             : static struct btrfs_feature_attr btrfs_attr_features_##_name = {             \
      85             :         .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,                        \
      86             :                                       btrfs_feature_attr_show,               \
      87             :                                       btrfs_feature_attr_store),             \
      88             :         .feature_set    = _feature_set,                                      \
      89             :         .feature_bit    = _feature_prefix ##_## _feature_bit,                \
      90             : }
      91             : #define BTRFS_FEAT_ATTR_PTR(_name)                                           \
      92             :         (&btrfs_attr_features_##_name.kobj_attr.attr)
      93             : 
      94             : #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
      95             :         BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
      96             : #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
      97             :         BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
      98             : #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
      99             :         BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
     100             : 
     101             : static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
     102             : static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
     103             : static struct kobject *get_btrfs_kobj(struct kobject *kobj);
     104             : 
     105             : static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
     106             : {
     107             :         return container_of(a, struct btrfs_feature_attr, kobj_attr);
     108             : }
     109             : 
     110             : static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
     111             : {
     112             :         return container_of(attr, struct kobj_attribute, attr);
     113             : }
     114             : 
     115             : static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
     116             :                 struct attribute *attr)
     117             : {
     118             :         return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
     119             : }
     120             : 
     121             : static u64 get_features(struct btrfs_fs_info *fs_info,
     122             :                         enum btrfs_feature_set set)
     123             : {
     124           0 :         struct btrfs_super_block *disk_super = fs_info->super_copy;
     125           0 :         if (set == FEAT_COMPAT)
     126           0 :                 return btrfs_super_compat_flags(disk_super);
     127           0 :         else if (set == FEAT_COMPAT_RO)
     128           0 :                 return btrfs_super_compat_ro_flags(disk_super);
     129             :         else
     130           0 :                 return btrfs_super_incompat_flags(disk_super);
     131             : }
     132             : 
     133             : static void set_features(struct btrfs_fs_info *fs_info,
     134             :                          enum btrfs_feature_set set, u64 features)
     135             : {
     136           0 :         struct btrfs_super_block *disk_super = fs_info->super_copy;
     137           0 :         if (set == FEAT_COMPAT)
     138           0 :                 btrfs_set_super_compat_flags(disk_super, features);
     139           0 :         else if (set == FEAT_COMPAT_RO)
     140           0 :                 btrfs_set_super_compat_ro_flags(disk_super, features);
     141             :         else
     142           0 :                 btrfs_set_super_incompat_flags(disk_super, features);
     143             : }
     144             : 
     145           0 : static int can_modify_feature(struct btrfs_feature_attr *fa)
     146             : {
     147           0 :         int val = 0;
     148           0 :         u64 set, clear;
     149           0 :         switch (fa->feature_set) {
     150             :         case FEAT_COMPAT:
     151             :                 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
     152             :                 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
     153             :                 break;
     154             :         case FEAT_COMPAT_RO:
     155             :                 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
     156             :                 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
     157             :                 break;
     158           0 :         case FEAT_INCOMPAT:
     159           0 :                 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
     160           0 :                 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
     161           0 :                 break;
     162           0 :         default:
     163           0 :                 pr_warn("btrfs: sysfs: unknown feature set %d\n",
     164             :                                 fa->feature_set);
     165           0 :                 return 0;
     166             :         }
     167             : 
     168           0 :         if (set & fa->feature_bit)
     169           0 :                 val |= 1;
     170             :         if (clear & fa->feature_bit)
     171             :                 val |= 2;
     172             : 
     173             :         return val;
     174             : }
     175             : 
     176           0 : static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
     177             :                                        struct kobj_attribute *a, char *buf)
     178             : {
     179           0 :         int val = 0;
     180           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
     181           0 :         struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
     182           0 :         if (fs_info) {
     183           0 :                 u64 features = get_features(fs_info, fa->feature_set);
     184           0 :                 if (features & fa->feature_bit)
     185           0 :                         val = 1;
     186             :         } else
     187           0 :                 val = can_modify_feature(fa);
     188             : 
     189           0 :         return sysfs_emit(buf, "%d\n", val);
     190             : }
     191             : 
     192           0 : static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
     193             :                                         struct kobj_attribute *a,
     194             :                                         const char *buf, size_t count)
     195             : {
     196           0 :         struct btrfs_fs_info *fs_info;
     197           0 :         struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
     198           0 :         u64 features, set, clear;
     199           0 :         unsigned long val;
     200           0 :         int ret;
     201             : 
     202           0 :         fs_info = to_fs_info(kobj);
     203           0 :         if (!fs_info)
     204             :                 return -EPERM;
     205             : 
     206           0 :         if (sb_rdonly(fs_info->sb))
     207             :                 return -EROFS;
     208             : 
     209           0 :         ret = kstrtoul(skip_spaces(buf), 0, &val);
     210           0 :         if (ret)
     211           0 :                 return ret;
     212             : 
     213           0 :         if (fa->feature_set == FEAT_COMPAT) {
     214             :                 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
     215             :                 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
     216           0 :         } else if (fa->feature_set == FEAT_COMPAT_RO) {
     217             :                 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
     218             :                 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
     219             :         } else {
     220           0 :                 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
     221           0 :                 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
     222             :         }
     223             : 
     224           0 :         features = get_features(fs_info, fa->feature_set);
     225             : 
     226             :         /* Nothing to do */
     227           0 :         if ((val && (features & fa->feature_bit)) ||
     228           0 :             (!val && !(features & fa->feature_bit)))
     229           0 :                 return count;
     230             : 
     231           0 :         if ((val && !(set & fa->feature_bit)) ||
     232             :             (!val && !(clear & fa->feature_bit))) {
     233           0 :                 btrfs_info(fs_info,
     234             :                         "%sabling feature %s on mounted fs is not supported.",
     235             :                         val ? "En" : "Dis", fa->kobj_attr.attr.name);
     236           0 :                 return -EPERM;
     237             :         }
     238             : 
     239           0 :         btrfs_info(fs_info, "%s %s feature flag",
     240             :                    val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
     241             : 
     242           0 :         spin_lock(&fs_info->super_lock);
     243           0 :         features = get_features(fs_info, fa->feature_set);
     244           0 :         if (val)
     245           0 :                 features |= fa->feature_bit;
     246             :         else
     247           0 :                 features &= ~fa->feature_bit;
     248           0 :         set_features(fs_info, fa->feature_set, features);
     249           0 :         spin_unlock(&fs_info->super_lock);
     250             : 
     251             :         /*
     252             :          * We don't want to do full transaction commit from inside sysfs
     253             :          */
     254           0 :         set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
     255           0 :         wake_up_process(fs_info->transaction_kthread);
     256             : 
     257           0 :         return count;
     258             : }
     259             : 
     260          26 : static umode_t btrfs_feature_visible(struct kobject *kobj,
     261             :                                      struct attribute *attr, int unused)
     262             : {
     263          26 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
     264          26 :         umode_t mode = attr->mode;
     265             : 
     266          26 :         if (fs_info) {
     267           0 :                 struct btrfs_feature_attr *fa;
     268           0 :                 u64 features;
     269             : 
     270           0 :                 fa = attr_to_btrfs_feature_attr(attr);
     271           0 :                 features = get_features(fs_info, fa->feature_set);
     272             : 
     273           0 :                 if (can_modify_feature(fa))
     274           0 :                         mode |= S_IWUSR;
     275           0 :                 else if (!(features & fa->feature_bit))
     276           0 :                         mode = 0;
     277             :         }
     278             : 
     279          26 :         return mode;
     280             : }
     281             : 
     282             : BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
     283             : BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
     284             : BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
     285             : BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
     286             : BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
     287             : BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
     288             : BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
     289             : BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
     290             : BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
     291             : BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
     292             : BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
     293             : BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
     294             : #ifdef CONFIG_BLK_DEV_ZONED
     295             : BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
     296             : #endif
     297             : #ifdef CONFIG_BTRFS_DEBUG
     298             : /* Remove once support for extent tree v2 is feature complete */
     299             : BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
     300             : #endif
     301             : #ifdef CONFIG_FS_VERITY
     302             : BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
     303             : #endif
     304             : 
     305             : /*
     306             :  * Features which depend on feature bits and may differ between each fs.
     307             :  *
     308             :  * /sys/fs/btrfs/features      - all available features implemented by this version
     309             :  * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
     310             :  *                               can be changed on a mounted filesystem.
     311             :  */
     312             : static struct attribute *btrfs_supported_feature_attrs[] = {
     313             :         BTRFS_FEAT_ATTR_PTR(default_subvol),
     314             :         BTRFS_FEAT_ATTR_PTR(mixed_groups),
     315             :         BTRFS_FEAT_ATTR_PTR(compress_lzo),
     316             :         BTRFS_FEAT_ATTR_PTR(compress_zstd),
     317             :         BTRFS_FEAT_ATTR_PTR(extended_iref),
     318             :         BTRFS_FEAT_ATTR_PTR(raid56),
     319             :         BTRFS_FEAT_ATTR_PTR(skinny_metadata),
     320             :         BTRFS_FEAT_ATTR_PTR(no_holes),
     321             :         BTRFS_FEAT_ATTR_PTR(metadata_uuid),
     322             :         BTRFS_FEAT_ATTR_PTR(free_space_tree),
     323             :         BTRFS_FEAT_ATTR_PTR(raid1c34),
     324             :         BTRFS_FEAT_ATTR_PTR(block_group_tree),
     325             : #ifdef CONFIG_BLK_DEV_ZONED
     326             :         BTRFS_FEAT_ATTR_PTR(zoned),
     327             : #endif
     328             : #ifdef CONFIG_BTRFS_DEBUG
     329             :         BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
     330             : #endif
     331             : #ifdef CONFIG_FS_VERITY
     332             :         BTRFS_FEAT_ATTR_PTR(verity),
     333             : #endif
     334             :         NULL
     335             : };
     336             : 
     337             : static const struct attribute_group btrfs_feature_attr_group = {
     338             :         .name = "features",
     339             :         .is_visible = btrfs_feature_visible,
     340             :         .attrs = btrfs_supported_feature_attrs,
     341             : };
     342             : 
     343           0 : static ssize_t rmdir_subvol_show(struct kobject *kobj,
     344             :                                  struct kobj_attribute *ka, char *buf)
     345             : {
     346           0 :         return sysfs_emit(buf, "0\n");
     347             : }
     348             : BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
     349             : 
     350           0 : static ssize_t supported_checksums_show(struct kobject *kobj,
     351             :                                         struct kobj_attribute *a, char *buf)
     352             : {
     353           0 :         ssize_t ret = 0;
     354           0 :         int i;
     355             : 
     356           0 :         for (i = 0; i < btrfs_get_num_csums(); i++) {
     357             :                 /*
     358             :                  * This "trick" only works as long as 'enum btrfs_csum_type' has
     359             :                  * no holes in it
     360             :                  */
     361           0 :                 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
     362             :                                      btrfs_super_csum_name(i));
     363             : 
     364             :         }
     365             : 
     366           0 :         ret += sysfs_emit_at(buf, ret, "\n");
     367           0 :         return ret;
     368             : }
     369             : BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
     370             : 
     371           0 : static ssize_t send_stream_version_show(struct kobject *kobj,
     372             :                                         struct kobj_attribute *ka, char *buf)
     373             : {
     374           0 :         return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
     375             : }
     376             : BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
     377             : 
     378             : static const char *rescue_opts[] = {
     379             :         "usebackuproot",
     380             :         "nologreplay",
     381             :         "ignorebadroots",
     382             :         "ignoredatacsums",
     383             :         "all",
     384             : };
     385             : 
     386           0 : static ssize_t supported_rescue_options_show(struct kobject *kobj,
     387             :                                              struct kobj_attribute *a,
     388             :                                              char *buf)
     389             : {
     390           0 :         ssize_t ret = 0;
     391           0 :         int i;
     392             : 
     393           0 :         for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
     394           0 :                 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
     395           0 :         ret += sysfs_emit_at(buf, ret, "\n");
     396           0 :         return ret;
     397             : }
     398             : BTRFS_ATTR(static_feature, supported_rescue_options,
     399             :            supported_rescue_options_show);
     400             : 
     401           0 : static ssize_t supported_sectorsizes_show(struct kobject *kobj,
     402             :                                           struct kobj_attribute *a,
     403             :                                           char *buf)
     404             : {
     405           0 :         ssize_t ret = 0;
     406             : 
     407             :         /* An artificial limit to only support 4K and PAGE_SIZE */
     408           0 :         if (PAGE_SIZE > SZ_4K)
     409           0 :                 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
     410           0 :         ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
     411             : 
     412           0 :         return ret;
     413             : }
     414             : BTRFS_ATTR(static_feature, supported_sectorsizes,
     415             :            supported_sectorsizes_show);
     416             : 
     417             : /*
     418             :  * Features which only depend on kernel version.
     419             :  *
     420             :  * These are listed in /sys/fs/btrfs/features along with
     421             :  * btrfs_supported_feature_attrs.
     422             :  */
     423             : static struct attribute *btrfs_supported_static_feature_attrs[] = {
     424             :         BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
     425             :         BTRFS_ATTR_PTR(static_feature, supported_checksums),
     426             :         BTRFS_ATTR_PTR(static_feature, send_stream_version),
     427             :         BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
     428             :         BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
     429             :         NULL
     430             : };
     431             : 
     432             : static const struct attribute_group btrfs_static_feature_attr_group = {
     433             :         .name = "features",
     434             :         .attrs = btrfs_supported_static_feature_attrs,
     435             : };
     436             : 
     437             : /*
     438             :  * Discard statistics and tunables
     439             :  */
     440             : #define discard_to_fs_info(_kobj)       to_fs_info(get_btrfs_kobj(_kobj))
     441             : 
     442           0 : static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
     443             :                                             struct kobj_attribute *a,
     444             :                                             char *buf)
     445             : {
     446           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     447             : 
     448           0 :         return sysfs_emit(buf, "%lld\n",
     449             :                         atomic64_read(&fs_info->discard_ctl.discardable_bytes));
     450             : }
     451             : BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
     452             : 
     453           0 : static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
     454             :                                               struct kobj_attribute *a,
     455             :                                               char *buf)
     456             : {
     457           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     458             : 
     459           0 :         return sysfs_emit(buf, "%d\n",
     460             :                         atomic_read(&fs_info->discard_ctl.discardable_extents));
     461             : }
     462             : BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
     463             : 
     464           0 : static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
     465             :                                                struct kobj_attribute *a,
     466             :                                                char *buf)
     467             : {
     468           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     469             : 
     470           0 :         return sysfs_emit(buf, "%llu\n",
     471             :                           fs_info->discard_ctl.discard_bitmap_bytes);
     472             : }
     473             : BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
     474             : 
     475           0 : static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
     476             :                                               struct kobj_attribute *a,
     477             :                                               char *buf)
     478             : {
     479           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     480             : 
     481           0 :         return sysfs_emit(buf, "%lld\n",
     482             :                 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
     483             : }
     484             : BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
     485             : 
     486           0 : static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
     487             :                                                struct kobj_attribute *a,
     488             :                                                char *buf)
     489             : {
     490           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     491             : 
     492           0 :         return sysfs_emit(buf, "%llu\n",
     493             :                           fs_info->discard_ctl.discard_extent_bytes);
     494             : }
     495             : BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
     496             : 
     497           0 : static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
     498             :                                              struct kobj_attribute *a,
     499             :                                              char *buf)
     500             : {
     501           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     502             : 
     503           0 :         return sysfs_emit(buf, "%u\n",
     504           0 :                           READ_ONCE(fs_info->discard_ctl.iops_limit));
     505             : }
     506             : 
     507           0 : static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
     508             :                                               struct kobj_attribute *a,
     509             :                                               const char *buf, size_t len)
     510             : {
     511           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     512           0 :         struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
     513           0 :         u32 iops_limit;
     514           0 :         int ret;
     515             : 
     516           0 :         ret = kstrtou32(buf, 10, &iops_limit);
     517           0 :         if (ret)
     518             :                 return -EINVAL;
     519             : 
     520           0 :         WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
     521           0 :         btrfs_discard_calc_delay(discard_ctl);
     522           0 :         btrfs_discard_schedule_work(discard_ctl, true);
     523           0 :         return len;
     524             : }
     525             : BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
     526             :               btrfs_discard_iops_limit_store);
     527             : 
     528           0 : static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
     529             :                                              struct kobj_attribute *a,
     530             :                                              char *buf)
     531             : {
     532           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     533             : 
     534           0 :         return sysfs_emit(buf, "%u\n",
     535           0 :                           READ_ONCE(fs_info->discard_ctl.kbps_limit));
     536             : }
     537             : 
     538           0 : static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
     539             :                                               struct kobj_attribute *a,
     540             :                                               const char *buf, size_t len)
     541             : {
     542           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     543           0 :         struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
     544           0 :         u32 kbps_limit;
     545           0 :         int ret;
     546             : 
     547           0 :         ret = kstrtou32(buf, 10, &kbps_limit);
     548           0 :         if (ret)
     549             :                 return -EINVAL;
     550             : 
     551           0 :         WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
     552           0 :         btrfs_discard_schedule_work(discard_ctl, true);
     553           0 :         return len;
     554             : }
     555             : BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
     556             :               btrfs_discard_kbps_limit_store);
     557             : 
     558           0 : static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
     559             :                                                    struct kobj_attribute *a,
     560             :                                                    char *buf)
     561             : {
     562           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     563             : 
     564           0 :         return sysfs_emit(buf, "%llu\n",
     565           0 :                           READ_ONCE(fs_info->discard_ctl.max_discard_size));
     566             : }
     567             : 
     568           0 : static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
     569             :                                                     struct kobj_attribute *a,
     570             :                                                     const char *buf, size_t len)
     571             : {
     572           0 :         struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
     573           0 :         struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
     574           0 :         u64 max_discard_size;
     575           0 :         int ret;
     576             : 
     577           0 :         ret = kstrtou64(buf, 10, &max_discard_size);
     578           0 :         if (ret)
     579             :                 return -EINVAL;
     580             : 
     581           0 :         WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
     582             : 
     583           0 :         return len;
     584             : }
     585             : BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
     586             :               btrfs_discard_max_discard_size_store);
     587             : 
     588             : /*
     589             :  * Per-filesystem stats for discard (when mounted with discard=async).
     590             :  *
     591             :  * Path: /sys/fs/btrfs/<uuid>/discard/
     592             :  */
     593             : static const struct attribute *discard_attrs[] = {
     594             :         BTRFS_ATTR_PTR(discard, discardable_bytes),
     595             :         BTRFS_ATTR_PTR(discard, discardable_extents),
     596             :         BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
     597             :         BTRFS_ATTR_PTR(discard, discard_bytes_saved),
     598             :         BTRFS_ATTR_PTR(discard, discard_extent_bytes),
     599             :         BTRFS_ATTR_PTR(discard, iops_limit),
     600             :         BTRFS_ATTR_PTR(discard, kbps_limit),
     601             :         BTRFS_ATTR_PTR(discard, max_discard_size),
     602             :         NULL,
     603             : };
     604             : 
     605             : #ifdef CONFIG_BTRFS_DEBUG
     606             : 
     607             : /*
     608             :  * Per-filesystem runtime debugging exported via sysfs.
     609             :  *
     610             :  * Path: /sys/fs/btrfs/UUID/debug/
     611             :  */
     612             : static const struct attribute *btrfs_debug_mount_attrs[] = {
     613             :         NULL,
     614             : };
     615             : 
     616             : /*
     617             :  * Runtime debugging exported via sysfs, applies to all mounted filesystems.
     618             :  *
     619             :  * Path: /sys/fs/btrfs/debug
     620             :  */
     621             : static struct attribute *btrfs_debug_feature_attrs[] = {
     622             :         NULL
     623             : };
     624             : 
     625             : static const struct attribute_group btrfs_debug_feature_attr_group = {
     626             :         .name = "debug",
     627             :         .attrs = btrfs_debug_feature_attrs,
     628             : };
     629             : 
     630             : #endif
     631             : 
     632           0 : static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
     633             : {
     634           0 :         u64 val;
     635           0 :         if (lock)
     636           0 :                 spin_lock(lock);
     637           0 :         val = *value_ptr;
     638           0 :         if (lock)
     639           0 :                 spin_unlock(lock);
     640           0 :         return sysfs_emit(buf, "%llu\n", val);
     641             : }
     642             : 
     643           0 : static ssize_t global_rsv_size_show(struct kobject *kobj,
     644             :                                     struct kobj_attribute *ka, char *buf)
     645             : {
     646           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
     647           0 :         struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
     648           0 :         return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
     649             : }
     650             : BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
     651             : 
     652           0 : static ssize_t global_rsv_reserved_show(struct kobject *kobj,
     653             :                                         struct kobj_attribute *a, char *buf)
     654             : {
     655           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
     656           0 :         struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
     657           0 :         return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
     658             : }
     659             : BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
     660             : 
     661             : #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
     662             : #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
     663             : 
     664             : static ssize_t raid_bytes_show(struct kobject *kobj,
     665             :                                struct kobj_attribute *attr, char *buf);
     666             : BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
     667             : BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
     668             : 
     669           0 : static ssize_t raid_bytes_show(struct kobject *kobj,
     670             :                                struct kobj_attribute *attr, char *buf)
     671             : 
     672             : {
     673           0 :         struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
     674           0 :         struct btrfs_block_group *block_group;
     675           0 :         int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
     676           0 :         u64 val = 0;
     677             : 
     678           0 :         down_read(&sinfo->groups_sem);
     679           0 :         list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
     680           0 :                 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
     681           0 :                         val += block_group->length;
     682             :                 else
     683           0 :                         val += block_group->used;
     684             :         }
     685           0 :         up_read(&sinfo->groups_sem);
     686           0 :         return sysfs_emit(buf, "%llu\n", val);
     687             : }
     688             : 
     689             : /*
     690             :  * Allocation information about block group profiles.
     691             :  *
     692             :  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
     693             :  */
     694             : static struct attribute *raid_attrs[] = {
     695             :         BTRFS_ATTR_PTR(raid, total_bytes),
     696             :         BTRFS_ATTR_PTR(raid, used_bytes),
     697             :         NULL
     698             : };
     699             : ATTRIBUTE_GROUPS(raid);
     700             : 
     701           0 : static void release_raid_kobj(struct kobject *kobj)
     702             : {
     703           0 :         kfree(to_raid_kobj(kobj));
     704           0 : }
     705             : 
     706             : static const struct kobj_type btrfs_raid_ktype = {
     707             :         .sysfs_ops = &kobj_sysfs_ops,
     708             :         .release = release_raid_kobj,
     709             :         .default_groups = raid_groups,
     710             : };
     711             : 
     712             : #define SPACE_INFO_ATTR(field)                                          \
     713             : static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,      \
     714             :                                              struct kobj_attribute *a,  \
     715             :                                              char *buf)                 \
     716             : {                                                                       \
     717             :         struct btrfs_space_info *sinfo = to_space_info(kobj);           \
     718             :         return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);  \
     719             : }                                                                       \
     720             : BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
     721             : 
     722           0 : static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
     723             :                                      struct kobj_attribute *a, char *buf)
     724             : {
     725           0 :         struct btrfs_space_info *sinfo = to_space_info(kobj);
     726             : 
     727           0 :         return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
     728             : }
     729             : 
     730             : /*
     731             :  * Store new chunk size in space info. Can be called on a read-only filesystem.
     732             :  *
     733             :  * If the new chunk size value is larger than 10% of free space it is reduced
     734             :  * to match that limit. Alignment must be to 256M and the system chunk size
     735             :  * cannot be set.
     736             :  */
     737           0 : static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
     738             :                                       struct kobj_attribute *a,
     739             :                                       const char *buf, size_t len)
     740             : {
     741           0 :         struct btrfs_space_info *space_info = to_space_info(kobj);
     742           0 :         struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
     743           0 :         char *retptr;
     744           0 :         u64 val;
     745             : 
     746           0 :         if (!capable(CAP_SYS_ADMIN))
     747             :                 return -EPERM;
     748             : 
     749           0 :         if (!fs_info->fs_devices)
     750             :                 return -EINVAL;
     751             : 
     752           0 :         if (btrfs_is_zoned(fs_info))
     753             :                 return -EINVAL;
     754             : 
     755             :         /* System block type must not be changed. */
     756           0 :         if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
     757             :                 return -EPERM;
     758             : 
     759           0 :         val = memparse(buf, &retptr);
     760             :         /* There could be trailing '\n', also catch any typos after the value */
     761           0 :         retptr = skip_spaces(retptr);
     762           0 :         if (*retptr != 0 || val == 0)
     763             :                 return -EINVAL;
     764             : 
     765           0 :         val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
     766             : 
     767             :         /* Limit stripe size to 10% of available space. */
     768           0 :         val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val);
     769             : 
     770             :         /* Must be multiple of 256M. */
     771           0 :         val &= ~((u64)SZ_256M - 1);
     772             : 
     773             :         /* Must be at least 256M. */
     774           0 :         if (val < SZ_256M)
     775             :                 return -EINVAL;
     776             : 
     777           0 :         btrfs_update_space_info_chunk_size(space_info, val);
     778             : 
     779           0 :         return len;
     780             : }
     781             : 
     782           0 : static ssize_t btrfs_size_classes_show(struct kobject *kobj,
     783             :                                        struct kobj_attribute *a, char *buf)
     784             : {
     785           0 :         struct btrfs_space_info *sinfo = to_space_info(kobj);
     786           0 :         struct btrfs_block_group *bg;
     787           0 :         u32 none = 0;
     788           0 :         u32 small = 0;
     789           0 :         u32 medium = 0;
     790           0 :         u32 large = 0;
     791             : 
     792           0 :         for (int i = 0; i < BTRFS_NR_RAID_TYPES; ++i) {
     793           0 :                 down_read(&sinfo->groups_sem);
     794           0 :                 list_for_each_entry(bg, &sinfo->block_groups[i], list) {
     795           0 :                         if (!btrfs_block_group_should_use_size_class(bg))
     796           0 :                                 continue;
     797           0 :                         switch (bg->size_class) {
     798           0 :                         case BTRFS_BG_SZ_NONE:
     799           0 :                                 none++;
     800           0 :                                 break;
     801           0 :                         case BTRFS_BG_SZ_SMALL:
     802           0 :                                 small++;
     803           0 :                                 break;
     804           0 :                         case BTRFS_BG_SZ_MEDIUM:
     805           0 :                                 medium++;
     806           0 :                                 break;
     807           0 :                         case BTRFS_BG_SZ_LARGE:
     808           0 :                                 large++;
     809           0 :                                 break;
     810             :                         }
     811             :                 }
     812           0 :                 up_read(&sinfo->groups_sem);
     813             :         }
     814           0 :         return sysfs_emit(buf, "none %u\n"
     815             :                                "small %u\n"
     816             :                                "medium %u\n"
     817             :                                "large %u\n",
     818             :                                none, small, medium, large);
     819             : }
     820             : 
     821             : #ifdef CONFIG_BTRFS_DEBUG
     822             : /*
     823             :  * Request chunk allocation with current chunk size.
     824             :  */
     825             : static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
     826             :                                              struct kobj_attribute *a,
     827             :                                              const char *buf, size_t len)
     828             : {
     829             :         struct btrfs_space_info *space_info = to_space_info(kobj);
     830             :         struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
     831             :         struct btrfs_trans_handle *trans;
     832             :         bool val;
     833             :         int ret;
     834             : 
     835             :         if (!capable(CAP_SYS_ADMIN))
     836             :                 return -EPERM;
     837             : 
     838             :         if (sb_rdonly(fs_info->sb))
     839             :                 return -EROFS;
     840             : 
     841             :         ret = kstrtobool(buf, &val);
     842             :         if (ret)
     843             :                 return ret;
     844             : 
     845             :         if (!val)
     846             :                 return -EINVAL;
     847             : 
     848             :         /*
     849             :          * This is unsafe to be called from sysfs context and may cause
     850             :          * unexpected problems.
     851             :          */
     852             :         trans = btrfs_start_transaction(fs_info->tree_root, 0);
     853             :         if (IS_ERR(trans))
     854             :                 return PTR_ERR(trans);
     855             :         ret = btrfs_force_chunk_alloc(trans, space_info->flags);
     856             :         btrfs_end_transaction(trans);
     857             : 
     858             :         if (ret == 1)
     859             :                 return len;
     860             : 
     861             :         return -ENOSPC;
     862             : }
     863             : BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
     864             : 
     865             : #endif
     866             : 
     867           0 : SPACE_INFO_ATTR(flags);
     868           0 : SPACE_INFO_ATTR(total_bytes);
     869           0 : SPACE_INFO_ATTR(bytes_used);
     870           0 : SPACE_INFO_ATTR(bytes_pinned);
     871           0 : SPACE_INFO_ATTR(bytes_reserved);
     872           0 : SPACE_INFO_ATTR(bytes_may_use);
     873           0 : SPACE_INFO_ATTR(bytes_readonly);
     874           0 : SPACE_INFO_ATTR(bytes_zone_unusable);
     875           0 : SPACE_INFO_ATTR(disk_used);
     876           0 : SPACE_INFO_ATTR(disk_total);
     877             : BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
     878             : BTRFS_ATTR(space_info, size_classes, btrfs_size_classes_show);
     879             : 
     880           0 : static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
     881             :                                                      struct kobj_attribute *a,
     882             :                                                      char *buf)
     883             : {
     884           0 :         struct btrfs_space_info *space_info = to_space_info(kobj);
     885             : 
     886           0 :         return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
     887             : }
     888             : 
     889           0 : static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
     890             :                                                       struct kobj_attribute *a,
     891             :                                                       const char *buf, size_t len)
     892             : {
     893           0 :         struct btrfs_space_info *space_info = to_space_info(kobj);
     894           0 :         int thresh;
     895           0 :         int ret;
     896             : 
     897           0 :         ret = kstrtoint(buf, 10, &thresh);
     898           0 :         if (ret)
     899           0 :                 return ret;
     900             : 
     901           0 :         if (thresh < 0 || thresh > 100)
     902             :                 return -EINVAL;
     903             : 
     904           0 :         WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
     905             : 
     906           0 :         return len;
     907             : }
     908             : 
     909             : BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
     910             :               btrfs_sinfo_bg_reclaim_threshold_show,
     911             :               btrfs_sinfo_bg_reclaim_threshold_store);
     912             : 
     913             : /*
     914             :  * Allocation information about block group types.
     915             :  *
     916             :  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
     917             :  */
     918             : static struct attribute *space_info_attrs[] = {
     919             :         BTRFS_ATTR_PTR(space_info, flags),
     920             :         BTRFS_ATTR_PTR(space_info, total_bytes),
     921             :         BTRFS_ATTR_PTR(space_info, bytes_used),
     922             :         BTRFS_ATTR_PTR(space_info, bytes_pinned),
     923             :         BTRFS_ATTR_PTR(space_info, bytes_reserved),
     924             :         BTRFS_ATTR_PTR(space_info, bytes_may_use),
     925             :         BTRFS_ATTR_PTR(space_info, bytes_readonly),
     926             :         BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
     927             :         BTRFS_ATTR_PTR(space_info, disk_used),
     928             :         BTRFS_ATTR_PTR(space_info, disk_total),
     929             :         BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
     930             :         BTRFS_ATTR_PTR(space_info, chunk_size),
     931             :         BTRFS_ATTR_PTR(space_info, size_classes),
     932             : #ifdef CONFIG_BTRFS_DEBUG
     933             :         BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
     934             : #endif
     935             :         NULL,
     936             : };
     937             : ATTRIBUTE_GROUPS(space_info);
     938             : 
     939           0 : static void space_info_release(struct kobject *kobj)
     940             : {
     941           0 :         struct btrfs_space_info *sinfo = to_space_info(kobj);
     942           0 :         kfree(sinfo);
     943           0 : }
     944             : 
     945             : static const struct kobj_type space_info_ktype = {
     946             :         .sysfs_ops = &kobj_sysfs_ops,
     947             :         .release = space_info_release,
     948             :         .default_groups = space_info_groups,
     949             : };
     950             : 
     951             : /*
     952             :  * Allocation information about block groups.
     953             :  *
     954             :  * Path: /sys/fs/btrfs/<uuid>/allocation/
     955             :  */
     956             : static const struct attribute *allocation_attrs[] = {
     957             :         BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
     958             :         BTRFS_ATTR_PTR(allocation, global_rsv_size),
     959             :         NULL,
     960             : };
     961             : 
     962           0 : static ssize_t btrfs_label_show(struct kobject *kobj,
     963             :                                 struct kobj_attribute *a, char *buf)
     964             : {
     965           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
     966           0 :         char *label = fs_info->super_copy->label;
     967           0 :         ssize_t ret;
     968             : 
     969           0 :         spin_lock(&fs_info->super_lock);
     970           0 :         ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
     971           0 :         spin_unlock(&fs_info->super_lock);
     972             : 
     973           0 :         return ret;
     974             : }
     975             : 
     976           0 : static ssize_t btrfs_label_store(struct kobject *kobj,
     977             :                                  struct kobj_attribute *a,
     978             :                                  const char *buf, size_t len)
     979             : {
     980           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
     981           0 :         size_t p_len;
     982             : 
     983           0 :         if (!fs_info)
     984             :                 return -EPERM;
     985             : 
     986           0 :         if (sb_rdonly(fs_info->sb))
     987             :                 return -EROFS;
     988             : 
     989             :         /*
     990             :          * p_len is the len until the first occurrence of either
     991             :          * '\n' or '\0'
     992             :          */
     993           0 :         p_len = strcspn(buf, "\n");
     994             : 
     995           0 :         if (p_len >= BTRFS_LABEL_SIZE)
     996             :                 return -EINVAL;
     997             : 
     998           0 :         spin_lock(&fs_info->super_lock);
     999           0 :         memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
    1000           0 :         memcpy(fs_info->super_copy->label, buf, p_len);
    1001           0 :         spin_unlock(&fs_info->super_lock);
    1002             : 
    1003             :         /*
    1004             :          * We don't want to do full transaction commit from inside sysfs
    1005             :          */
    1006           0 :         set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
    1007           0 :         wake_up_process(fs_info->transaction_kthread);
    1008             : 
    1009           0 :         return len;
    1010             : }
    1011             : BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
    1012             : 
    1013           0 : static ssize_t btrfs_nodesize_show(struct kobject *kobj,
    1014             :                                 struct kobj_attribute *a, char *buf)
    1015             : {
    1016           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1017             : 
    1018           0 :         return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
    1019             : }
    1020             : 
    1021             : BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
    1022             : 
    1023           0 : static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
    1024             :                                 struct kobj_attribute *a, char *buf)
    1025             : {
    1026           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1027             : 
    1028           0 :         return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
    1029             : }
    1030             : 
    1031             : BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
    1032             : 
    1033           0 : static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
    1034             :                                        struct kobj_attribute *a, char *buf)
    1035             : {
    1036           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1037             : 
    1038           0 :         return sysfs_emit(buf,
    1039             :                 "commits %llu\n"
    1040             :                 "last_commit_ms %llu\n"
    1041             :                 "max_commit_ms %llu\n"
    1042             :                 "total_commit_ms %llu\n",
    1043             :                 fs_info->commit_stats.commit_count,
    1044             :                 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
    1045             :                 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
    1046             :                 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
    1047             : }
    1048             : 
    1049           0 : static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
    1050             :                                         struct kobj_attribute *a,
    1051             :                                         const char *buf, size_t len)
    1052             : {
    1053           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1054           0 :         unsigned long val;
    1055           0 :         int ret;
    1056             : 
    1057           0 :         if (!fs_info)
    1058             :                 return -EPERM;
    1059             : 
    1060           0 :         if (!capable(CAP_SYS_RESOURCE))
    1061             :                 return -EPERM;
    1062             : 
    1063           0 :         ret = kstrtoul(buf, 10, &val);
    1064           0 :         if (ret)
    1065           0 :                 return ret;
    1066           0 :         if (val)
    1067             :                 return -EINVAL;
    1068             : 
    1069           0 :         WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
    1070             : 
    1071           0 :         return len;
    1072             : }
    1073             : BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
    1074             : 
    1075           0 : static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
    1076             :                                 struct kobj_attribute *a, char *buf)
    1077             : {
    1078           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1079             : 
    1080           0 :         return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
    1081             : }
    1082             : 
    1083             : BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
    1084             : 
    1085           0 : static ssize_t quota_override_show(struct kobject *kobj,
    1086             :                                    struct kobj_attribute *a, char *buf)
    1087             : {
    1088           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1089           0 :         int quota_override;
    1090             : 
    1091           0 :         quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
    1092           0 :         return sysfs_emit(buf, "%d\n", quota_override);
    1093             : }
    1094             : 
    1095           0 : static ssize_t quota_override_store(struct kobject *kobj,
    1096             :                                     struct kobj_attribute *a,
    1097             :                                     const char *buf, size_t len)
    1098             : {
    1099           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1100           0 :         unsigned long knob;
    1101           0 :         int err;
    1102             : 
    1103           0 :         if (!fs_info)
    1104             :                 return -EPERM;
    1105             : 
    1106           0 :         if (!capable(CAP_SYS_RESOURCE))
    1107             :                 return -EPERM;
    1108             : 
    1109           0 :         err = kstrtoul(buf, 10, &knob);
    1110           0 :         if (err)
    1111           0 :                 return err;
    1112           0 :         if (knob > 1)
    1113             :                 return -EINVAL;
    1114             : 
    1115           0 :         if (knob)
    1116           0 :                 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
    1117             :         else
    1118           0 :                 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
    1119             : 
    1120           0 :         return len;
    1121             : }
    1122             : 
    1123             : BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
    1124             : 
    1125           0 : static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
    1126             :                                 struct kobj_attribute *a, char *buf)
    1127             : {
    1128           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1129             : 
    1130           0 :         return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
    1131             : }
    1132             : 
    1133             : BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
    1134             : 
    1135           0 : static ssize_t btrfs_checksum_show(struct kobject *kobj,
    1136             :                                    struct kobj_attribute *a, char *buf)
    1137             : {
    1138           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1139           0 :         u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
    1140             : 
    1141           0 :         return sysfs_emit(buf, "%s (%s)\n",
    1142             :                           btrfs_super_csum_name(csum_type),
    1143             :                           crypto_shash_driver_name(fs_info->csum_shash));
    1144             : }
    1145             : 
    1146             : BTRFS_ATTR(, checksum, btrfs_checksum_show);
    1147             : 
    1148           0 : static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
    1149             :                 struct kobj_attribute *a, char *buf)
    1150             : {
    1151           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1152           0 :         const char *str;
    1153             : 
    1154           0 :         switch (READ_ONCE(fs_info->exclusive_operation)) {
    1155             :                 case  BTRFS_EXCLOP_NONE:
    1156             :                         str = "none\n";
    1157             :                         break;
    1158             :                 case BTRFS_EXCLOP_BALANCE:
    1159             :                         str = "balance\n";
    1160             :                         break;
    1161             :                 case BTRFS_EXCLOP_BALANCE_PAUSED:
    1162             :                         str = "balance paused\n";
    1163             :                         break;
    1164             :                 case BTRFS_EXCLOP_DEV_ADD:
    1165             :                         str = "device add\n";
    1166             :                         break;
    1167             :                 case BTRFS_EXCLOP_DEV_REMOVE:
    1168             :                         str = "device remove\n";
    1169             :                         break;
    1170             :                 case BTRFS_EXCLOP_DEV_REPLACE:
    1171             :                         str = "device replace\n";
    1172             :                         break;
    1173             :                 case BTRFS_EXCLOP_RESIZE:
    1174             :                         str = "resize\n";
    1175             :                         break;
    1176             :                 case BTRFS_EXCLOP_SWAP_ACTIVATE:
    1177             :                         str = "swap activate\n";
    1178             :                         break;
    1179             :                 default:
    1180             :                         str = "UNKNOWN\n";
    1181             :                         break;
    1182             :         }
    1183           0 :         return sysfs_emit(buf, "%s", str);
    1184             : }
    1185             : BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
    1186             : 
    1187           0 : static ssize_t btrfs_generation_show(struct kobject *kobj,
    1188             :                                      struct kobj_attribute *a, char *buf)
    1189             : {
    1190           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1191             : 
    1192           0 :         return sysfs_emit(buf, "%llu\n", fs_info->generation);
    1193             : }
    1194             : BTRFS_ATTR(, generation, btrfs_generation_show);
    1195             : 
    1196             : static const char * const btrfs_read_policy_name[] = { "pid" };
    1197             : 
    1198           0 : static ssize_t btrfs_read_policy_show(struct kobject *kobj,
    1199             :                                       struct kobj_attribute *a, char *buf)
    1200             : {
    1201           0 :         struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
    1202           0 :         ssize_t ret = 0;
    1203           0 :         int i;
    1204             : 
    1205           0 :         for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
    1206           0 :                 if (fs_devices->read_policy == i)
    1207           0 :                         ret += sysfs_emit_at(buf, ret, "%s[%s]",
    1208             :                                          (ret == 0 ? "" : " "),
    1209             :                                          btrfs_read_policy_name[i]);
    1210             :                 else
    1211           0 :                         ret += sysfs_emit_at(buf, ret, "%s%s",
    1212             :                                          (ret == 0 ? "" : " "),
    1213             :                                          btrfs_read_policy_name[i]);
    1214             :         }
    1215             : 
    1216           0 :         ret += sysfs_emit_at(buf, ret, "\n");
    1217             : 
    1218           0 :         return ret;
    1219             : }
    1220             : 
    1221           0 : static ssize_t btrfs_read_policy_store(struct kobject *kobj,
    1222             :                                        struct kobj_attribute *a,
    1223             :                                        const char *buf, size_t len)
    1224             : {
    1225           0 :         struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
    1226           0 :         int i;
    1227             : 
    1228           0 :         for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
    1229           0 :                 if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
    1230           0 :                         if (i != fs_devices->read_policy) {
    1231           0 :                                 fs_devices->read_policy = i;
    1232           0 :                                 btrfs_info(fs_devices->fs_info,
    1233             :                                            "read policy set to '%s'",
    1234             :                                            btrfs_read_policy_name[i]);
    1235             :                         }
    1236           0 :                         return len;
    1237             :                 }
    1238             :         }
    1239             : 
    1240             :         return -EINVAL;
    1241             : }
    1242             : BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
    1243             : 
    1244           0 : static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
    1245             :                                                struct kobj_attribute *a,
    1246             :                                                char *buf)
    1247             : {
    1248           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1249             : 
    1250           0 :         return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
    1251             : }
    1252             : 
    1253           0 : static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
    1254             :                                                 struct kobj_attribute *a,
    1255             :                                                 const char *buf, size_t len)
    1256             : {
    1257           0 :         struct btrfs_fs_info *fs_info = to_fs_info(kobj);
    1258           0 :         int thresh;
    1259           0 :         int ret;
    1260             : 
    1261           0 :         ret = kstrtoint(buf, 10, &thresh);
    1262           0 :         if (ret)
    1263           0 :                 return ret;
    1264             : 
    1265             : #ifdef CONFIG_BTRFS_DEBUG
    1266             :         if (thresh != 0 && (thresh > 100))
    1267             :                 return -EINVAL;
    1268             : #else
    1269           0 :         if (thresh != 0 && (thresh <= 50 || thresh > 100))
    1270             :                 return -EINVAL;
    1271             : #endif
    1272             : 
    1273           0 :         WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
    1274             : 
    1275           0 :         return len;
    1276             : }
    1277             : BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
    1278             :               btrfs_bg_reclaim_threshold_store);
    1279             : 
    1280             : /*
    1281             :  * Per-filesystem information and stats.
    1282             :  *
    1283             :  * Path: /sys/fs/btrfs/<uuid>/
    1284             :  */
    1285             : static const struct attribute *btrfs_attrs[] = {
    1286             :         BTRFS_ATTR_PTR(, label),
    1287             :         BTRFS_ATTR_PTR(, nodesize),
    1288             :         BTRFS_ATTR_PTR(, sectorsize),
    1289             :         BTRFS_ATTR_PTR(, clone_alignment),
    1290             :         BTRFS_ATTR_PTR(, quota_override),
    1291             :         BTRFS_ATTR_PTR(, metadata_uuid),
    1292             :         BTRFS_ATTR_PTR(, checksum),
    1293             :         BTRFS_ATTR_PTR(, exclusive_operation),
    1294             :         BTRFS_ATTR_PTR(, generation),
    1295             :         BTRFS_ATTR_PTR(, read_policy),
    1296             :         BTRFS_ATTR_PTR(, bg_reclaim_threshold),
    1297             :         BTRFS_ATTR_PTR(, commit_stats),
    1298             :         NULL,
    1299             : };
    1300             : 
    1301           0 : static void btrfs_release_fsid_kobj(struct kobject *kobj)
    1302             : {
    1303           0 :         struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
    1304             : 
    1305           0 :         memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
    1306           0 :         complete(&fs_devs->kobj_unregister);
    1307           0 : }
    1308             : 
    1309             : static const struct kobj_type btrfs_ktype = {
    1310             :         .sysfs_ops      = &kobj_sysfs_ops,
    1311             :         .release        = btrfs_release_fsid_kobj,
    1312             : };
    1313             : 
    1314             : static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
    1315             : {
    1316           0 :         if (kobj->ktype != &btrfs_ktype)
    1317             :                 return NULL;
    1318           0 :         return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
    1319             : }
    1320             : 
    1321             : static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
    1322             : {
    1323          26 :         if (kobj->ktype != &btrfs_ktype)
    1324             :                 return NULL;
    1325           0 :         return to_fs_devs(kobj)->fs_info;
    1326             : }
    1327             : 
    1328             : static struct kobject *get_btrfs_kobj(struct kobject *kobj)
    1329             : {
    1330           0 :         while (kobj) {
    1331           0 :                 if (kobj->ktype == &btrfs_ktype)
    1332             :                         return kobj;
    1333           0 :                 kobj = kobj->parent;
    1334             :         }
    1335             :         return NULL;
    1336             : }
    1337             : 
    1338             : #define NUM_FEATURE_BITS 64
    1339             : #define BTRFS_FEATURE_NAME_MAX 13
    1340             : static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
    1341             : static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
    1342             : 
    1343             : static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
    1344             :               ARRAY_SIZE(btrfs_feature_attrs));
    1345             : static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
    1346             :               ARRAY_SIZE(btrfs_feature_attrs[0]));
    1347             : 
    1348             : static const u64 supported_feature_masks[FEAT_MAX] = {
    1349             :         [FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
    1350             :         [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
    1351             :         [FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
    1352             : };
    1353             : 
    1354           0 : static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
    1355             : {
    1356           0 :         int set;
    1357             : 
    1358           0 :         for (set = 0; set < FEAT_MAX; set++) {
    1359           0 :                 int i;
    1360           0 :                 struct attribute *attrs[2];
    1361           0 :                 struct attribute_group agroup = {
    1362             :                         .name = "features",
    1363             :                         .attrs = attrs,
    1364             :                 };
    1365           0 :                 u64 features = get_features(fs_info, set);
    1366           0 :                 features &= ~supported_feature_masks[set];
    1367             : 
    1368           0 :                 if (!features)
    1369           0 :                         continue;
    1370             : 
    1371           0 :                 attrs[1] = NULL;
    1372           0 :                 for (i = 0; i < NUM_FEATURE_BITS; i++) {
    1373           0 :                         struct btrfs_feature_attr *fa;
    1374             : 
    1375           0 :                         if (!(features & (1ULL << i)))
    1376           0 :                                 continue;
    1377             : 
    1378           0 :                         fa = &btrfs_feature_attrs[set][i];
    1379           0 :                         attrs[0] = &fa->kobj_attr.attr;
    1380           0 :                         if (add) {
    1381           0 :                                 int ret;
    1382           0 :                                 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
    1383             :                                                         &agroup);
    1384           0 :                                 if (ret)
    1385           0 :                                         return ret;
    1386             :                         } else
    1387           0 :                                 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
    1388             :                                                     &agroup);
    1389             :                 }
    1390             : 
    1391             :         }
    1392             :         return 0;
    1393             : }
    1394             : 
    1395           0 : static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
    1396             : {
    1397           0 :         if (fs_devs->devinfo_kobj) {
    1398           0 :                 kobject_del(fs_devs->devinfo_kobj);
    1399           0 :                 kobject_put(fs_devs->devinfo_kobj);
    1400           0 :                 fs_devs->devinfo_kobj = NULL;
    1401             :         }
    1402             : 
    1403           0 :         if (fs_devs->devices_kobj) {
    1404           0 :                 kobject_del(fs_devs->devices_kobj);
    1405           0 :                 kobject_put(fs_devs->devices_kobj);
    1406           0 :                 fs_devs->devices_kobj = NULL;
    1407             :         }
    1408             : 
    1409           0 :         if (fs_devs->fsid_kobj.state_initialized) {
    1410           0 :                 kobject_del(&fs_devs->fsid_kobj);
    1411           0 :                 kobject_put(&fs_devs->fsid_kobj);
    1412           0 :                 wait_for_completion(&fs_devs->kobj_unregister);
    1413             :         }
    1414           0 : }
    1415             : 
    1416             : /* when fs_devs is NULL it will remove all fsid kobject */
    1417           0 : void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
    1418             : {
    1419           0 :         struct list_head *fs_uuids = btrfs_get_fs_uuids();
    1420             : 
    1421           0 :         if (fs_devs) {
    1422           0 :                 __btrfs_sysfs_remove_fsid(fs_devs);
    1423           0 :                 return;
    1424             :         }
    1425             : 
    1426           0 :         list_for_each_entry(fs_devs, fs_uuids, fs_list) {
    1427           0 :                 __btrfs_sysfs_remove_fsid(fs_devs);
    1428             :         }
    1429             : }
    1430             : 
    1431           0 : static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
    1432             : {
    1433           0 :         struct btrfs_device *device;
    1434           0 :         struct btrfs_fs_devices *seed;
    1435             : 
    1436           0 :         list_for_each_entry(device, &fs_devices->devices, dev_list)
    1437           0 :                 btrfs_sysfs_remove_device(device);
    1438             : 
    1439           0 :         list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
    1440           0 :                 list_for_each_entry(device, &seed->devices, dev_list)
    1441           0 :                         btrfs_sysfs_remove_device(device);
    1442             :         }
    1443           0 : }
    1444             : 
    1445           0 : void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
    1446             : {
    1447           0 :         struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
    1448             : 
    1449           0 :         sysfs_remove_link(fsid_kobj, "bdi");
    1450             : 
    1451           0 :         if (fs_info->space_info_kobj) {
    1452           0 :                 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
    1453           0 :                 kobject_del(fs_info->space_info_kobj);
    1454           0 :                 kobject_put(fs_info->space_info_kobj);
    1455             :         }
    1456           0 :         if (fs_info->discard_kobj) {
    1457           0 :                 sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
    1458           0 :                 kobject_del(fs_info->discard_kobj);
    1459           0 :                 kobject_put(fs_info->discard_kobj);
    1460             :         }
    1461             : #ifdef CONFIG_BTRFS_DEBUG
    1462             :         if (fs_info->debug_kobj) {
    1463             :                 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
    1464             :                 kobject_del(fs_info->debug_kobj);
    1465             :                 kobject_put(fs_info->debug_kobj);
    1466             :         }
    1467             : #endif
    1468           0 :         addrm_unknown_feature_attrs(fs_info, false);
    1469           0 :         sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
    1470           0 :         sysfs_remove_files(fsid_kobj, btrfs_attrs);
    1471           0 :         btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
    1472           0 : }
    1473             : 
    1474             : static const char * const btrfs_feature_set_names[FEAT_MAX] = {
    1475             :         [FEAT_COMPAT]    = "compat",
    1476             :         [FEAT_COMPAT_RO] = "compat_ro",
    1477             :         [FEAT_INCOMPAT]  = "incompat",
    1478             : };
    1479             : 
    1480           0 : const char *btrfs_feature_set_name(enum btrfs_feature_set set)
    1481             : {
    1482           0 :         return btrfs_feature_set_names[set];
    1483             : }
    1484             : 
    1485           0 : char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
    1486             : {
    1487           0 :         size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
    1488           0 :         int len = 0;
    1489           0 :         int i;
    1490           0 :         char *str;
    1491             : 
    1492           0 :         str = kmalloc(bufsize, GFP_KERNEL);
    1493           0 :         if (!str)
    1494             :                 return str;
    1495             : 
    1496           0 :         for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
    1497           0 :                 const char *name;
    1498             : 
    1499           0 :                 if (!(flags & (1ULL << i)))
    1500           0 :                         continue;
    1501             : 
    1502           0 :                 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
    1503           0 :                 len += scnprintf(str + len, bufsize - len, "%s%s",
    1504             :                                 len ? "," : "", name);
    1505             :         }
    1506             : 
    1507             :         return str;
    1508             : }
    1509             : 
    1510           2 : static void init_feature_attrs(void)
    1511             : {
    1512           2 :         struct btrfs_feature_attr *fa;
    1513           2 :         int set, i;
    1514             : 
    1515           2 :         memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
    1516           2 :         memset(btrfs_unknown_feature_names, 0,
    1517             :                sizeof(btrfs_unknown_feature_names));
    1518             : 
    1519          28 :         for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
    1520          26 :                 struct btrfs_feature_attr *sfa;
    1521          26 :                 struct attribute *a = btrfs_supported_feature_attrs[i];
    1522          26 :                 int bit;
    1523          26 :                 sfa = attr_to_btrfs_feature_attr(a);
    1524          26 :                 bit = ilog2(sfa->feature_bit);
    1525          26 :                 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
    1526             : 
    1527          26 :                 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
    1528             :         }
    1529             : 
    1530           8 :         for (set = 0; set < FEAT_MAX; set++) {
    1531         390 :                 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
    1532         384 :                         char *name = btrfs_unknown_feature_names[set][i];
    1533         384 :                         fa = &btrfs_feature_attrs[set][i];
    1534             : 
    1535         384 :                         if (fa->kobj_attr.attr.name)
    1536          26 :                                 continue;
    1537             : 
    1538         358 :                         snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
    1539         358 :                                  btrfs_feature_set_names[set], i);
    1540             : 
    1541         358 :                         fa->kobj_attr.attr.name = name;
    1542         358 :                         fa->kobj_attr.attr.mode = S_IRUGO;
    1543         358 :                         fa->feature_set = set;
    1544         358 :                         fa->feature_bit = 1ULL << i;
    1545             :                 }
    1546             :         }
    1547           2 : }
    1548             : 
    1549             : /*
    1550             :  * Create a sysfs entry for a given block group type at path
    1551             :  * /sys/fs/btrfs/UUID/allocation/data/TYPE
    1552             :  */
    1553           0 : void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
    1554             : {
    1555           0 :         struct btrfs_fs_info *fs_info = cache->fs_info;
    1556           0 :         struct btrfs_space_info *space_info = cache->space_info;
    1557           0 :         struct raid_kobject *rkobj;
    1558           0 :         const int index = btrfs_bg_flags_to_raid_index(cache->flags);
    1559           0 :         unsigned int nofs_flag;
    1560           0 :         int ret;
    1561             : 
    1562             :         /*
    1563             :          * Setup a NOFS context because kobject_add(), deep in its call chain,
    1564             :          * does GFP_KERNEL allocations, and we are often called in a context
    1565             :          * where if reclaim is triggered we can deadlock (we are either holding
    1566             :          * a transaction handle or some lock required for a transaction
    1567             :          * commit).
    1568             :          */
    1569           0 :         nofs_flag = memalloc_nofs_save();
    1570             : 
    1571           0 :         rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
    1572           0 :         if (!rkobj) {
    1573           0 :                 memalloc_nofs_restore(nofs_flag);
    1574           0 :                 btrfs_warn(cache->fs_info,
    1575             :                                 "couldn't alloc memory for raid level kobject");
    1576           0 :                 return;
    1577             :         }
    1578             : 
    1579           0 :         rkobj->flags = cache->flags;
    1580           0 :         kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
    1581             : 
    1582             :         /*
    1583             :          * We call this either on mount, or if we've created a block group for a
    1584             :          * new index type while running (i.e. when restriping).  The running
    1585             :          * case is tricky because we could race with other threads, so we need
    1586             :          * to have this check to make sure we didn't already init the kobject.
    1587             :          *
    1588             :          * We don't have to protect on the free side because it only happens on
    1589             :          * unmount.
    1590             :          */
    1591           0 :         spin_lock(&space_info->lock);
    1592           0 :         if (space_info->block_group_kobjs[index]) {
    1593           0 :                 spin_unlock(&space_info->lock);
    1594           0 :                 kobject_put(&rkobj->kobj);
    1595           0 :                 return;
    1596             :         } else {
    1597           0 :                 space_info->block_group_kobjs[index] = &rkobj->kobj;
    1598             :         }
    1599           0 :         spin_unlock(&space_info->lock);
    1600             : 
    1601           0 :         ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
    1602             :                           btrfs_bg_type_to_raid_name(rkobj->flags));
    1603           0 :         memalloc_nofs_restore(nofs_flag);
    1604           0 :         if (ret) {
    1605           0 :                 spin_lock(&space_info->lock);
    1606           0 :                 space_info->block_group_kobjs[index] = NULL;
    1607           0 :                 spin_unlock(&space_info->lock);
    1608           0 :                 kobject_put(&rkobj->kobj);
    1609           0 :                 btrfs_warn(fs_info,
    1610             :                         "failed to add kobject for block cache, ignoring");
    1611           0 :                 return;
    1612             :         }
    1613             : }
    1614             : 
    1615             : /*
    1616             :  * Remove sysfs directories for all block group types of a given space info and
    1617             :  * the space info as well
    1618             :  */
    1619           0 : void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
    1620             : {
    1621           0 :         int i;
    1622             : 
    1623           0 :         for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
    1624           0 :                 struct kobject *kobj;
    1625             : 
    1626           0 :                 kobj = space_info->block_group_kobjs[i];
    1627           0 :                 space_info->block_group_kobjs[i] = NULL;
    1628           0 :                 if (kobj) {
    1629           0 :                         kobject_del(kobj);
    1630           0 :                         kobject_put(kobj);
    1631             :                 }
    1632             :         }
    1633           0 :         kobject_del(&space_info->kobj);
    1634           0 :         kobject_put(&space_info->kobj);
    1635           0 : }
    1636             : 
    1637           0 : static const char *alloc_name(u64 flags)
    1638             : {
    1639           0 :         switch (flags) {
    1640             :         case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
    1641             :                 return "mixed";
    1642           0 :         case BTRFS_BLOCK_GROUP_METADATA:
    1643           0 :                 return "metadata";
    1644           0 :         case BTRFS_BLOCK_GROUP_DATA:
    1645           0 :                 return "data";
    1646           0 :         case BTRFS_BLOCK_GROUP_SYSTEM:
    1647           0 :                 return "system";
    1648             :         default:
    1649           0 :                 WARN_ON(1);
    1650           0 :                 return "invalid-combination";
    1651             :         }
    1652             : }
    1653             : 
    1654             : /*
    1655             :  * Create a sysfs entry for a space info type at path
    1656             :  * /sys/fs/btrfs/UUID/allocation/TYPE
    1657             :  */
    1658           0 : int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
    1659             :                                     struct btrfs_space_info *space_info)
    1660             : {
    1661           0 :         int ret;
    1662             : 
    1663           0 :         ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
    1664             :                                    fs_info->space_info_kobj, "%s",
    1665             :                                    alloc_name(space_info->flags));
    1666           0 :         if (ret) {
    1667           0 :                 kobject_put(&space_info->kobj);
    1668           0 :                 return ret;
    1669             :         }
    1670             : 
    1671             :         return 0;
    1672             : }
    1673             : 
    1674           0 : void btrfs_sysfs_remove_device(struct btrfs_device *device)
    1675             : {
    1676           0 :         struct kobject *devices_kobj;
    1677             : 
    1678             :         /*
    1679             :          * Seed fs_devices devices_kobj aren't used, fetch kobject from the
    1680             :          * fs_info::fs_devices.
    1681             :          */
    1682           0 :         devices_kobj = device->fs_info->fs_devices->devices_kobj;
    1683           0 :         ASSERT(devices_kobj);
    1684             : 
    1685           0 :         if (device->bdev)
    1686           0 :                 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
    1687             : 
    1688           0 :         if (device->devid_kobj.state_initialized) {
    1689           0 :                 kobject_del(&device->devid_kobj);
    1690           0 :                 kobject_put(&device->devid_kobj);
    1691           0 :                 wait_for_completion(&device->kobj_unregister);
    1692             :         }
    1693           0 : }
    1694             : 
    1695           0 : static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
    1696             :                                                  struct kobj_attribute *a,
    1697             :                                                  char *buf)
    1698             : {
    1699           0 :         int val;
    1700           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1701             :                                                    devid_kobj);
    1702             : 
    1703           0 :         val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
    1704             : 
    1705           0 :         return sysfs_emit(buf, "%d\n", val);
    1706             : }
    1707             : BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
    1708             : 
    1709           0 : static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
    1710             :                                         struct kobj_attribute *a, char *buf)
    1711             : {
    1712           0 :         int val;
    1713           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1714             :                                                    devid_kobj);
    1715             : 
    1716           0 :         val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
    1717             : 
    1718           0 :         return sysfs_emit(buf, "%d\n", val);
    1719             : }
    1720             : BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
    1721             : 
    1722           0 : static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
    1723             :                                                  struct kobj_attribute *a,
    1724             :                                                  char *buf)
    1725             : {
    1726           0 :         int val;
    1727           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1728             :                                                    devid_kobj);
    1729             : 
    1730           0 :         val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
    1731             : 
    1732           0 :         return sysfs_emit(buf, "%d\n", val);
    1733             : }
    1734             : BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
    1735             : 
    1736           0 : static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
    1737             :                                              struct kobj_attribute *a,
    1738             :                                              char *buf)
    1739             : {
    1740           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1741             :                                                    devid_kobj);
    1742             : 
    1743           0 :         return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
    1744             : }
    1745             : 
    1746           0 : static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
    1747             :                                               struct kobj_attribute *a,
    1748             :                                               const char *buf, size_t len)
    1749             : {
    1750           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1751             :                                                    devid_kobj);
    1752           0 :         char *endptr;
    1753           0 :         unsigned long long limit;
    1754             : 
    1755           0 :         limit = memparse(buf, &endptr);
    1756           0 :         WRITE_ONCE(device->scrub_speed_max, limit);
    1757           0 :         return len;
    1758             : }
    1759             : BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
    1760             :               btrfs_devinfo_scrub_speed_max_store);
    1761             : 
    1762           0 : static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
    1763             :                                             struct kobj_attribute *a, char *buf)
    1764             : {
    1765           0 :         int val;
    1766           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1767             :                                                    devid_kobj);
    1768             : 
    1769           0 :         val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
    1770             : 
    1771           0 :         return sysfs_emit(buf, "%d\n", val);
    1772             : }
    1773             : BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
    1774             : 
    1775           0 : static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
    1776             :                                        struct kobj_attribute *a, char *buf)
    1777             : {
    1778           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1779             :                                                    devid_kobj);
    1780             : 
    1781           0 :         return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
    1782             : }
    1783             : BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
    1784             : 
    1785           0 : static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
    1786             :                 struct kobj_attribute *a, char *buf)
    1787             : {
    1788           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1789             :                                                    devid_kobj);
    1790             : 
    1791           0 :         if (!device->dev_stats_valid)
    1792           0 :                 return sysfs_emit(buf, "invalid\n");
    1793             : 
    1794             :         /*
    1795             :          * Print all at once so we get a snapshot of all values from the same
    1796             :          * time. Keep them in sync and in order of definition of
    1797             :          * btrfs_dev_stat_values.
    1798             :          */
    1799           0 :         return sysfs_emit(buf,
    1800             :                 "write_errs %d\n"
    1801             :                 "read_errs %d\n"
    1802             :                 "flush_errs %d\n"
    1803             :                 "corruption_errs %d\n"
    1804             :                 "generation_errs %d\n",
    1805             :                 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
    1806             :                 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
    1807             :                 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
    1808             :                 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
    1809             :                 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
    1810             : }
    1811             : BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
    1812             : 
    1813             : /*
    1814             :  * Information about one device.
    1815             :  *
    1816             :  * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
    1817             :  */
    1818             : static struct attribute *devid_attrs[] = {
    1819             :         BTRFS_ATTR_PTR(devid, error_stats),
    1820             :         BTRFS_ATTR_PTR(devid, fsid),
    1821             :         BTRFS_ATTR_PTR(devid, in_fs_metadata),
    1822             :         BTRFS_ATTR_PTR(devid, missing),
    1823             :         BTRFS_ATTR_PTR(devid, replace_target),
    1824             :         BTRFS_ATTR_PTR(devid, scrub_speed_max),
    1825             :         BTRFS_ATTR_PTR(devid, writeable),
    1826             :         NULL
    1827             : };
    1828             : ATTRIBUTE_GROUPS(devid);
    1829             : 
    1830           0 : static void btrfs_release_devid_kobj(struct kobject *kobj)
    1831             : {
    1832           0 :         struct btrfs_device *device = container_of(kobj, struct btrfs_device,
    1833             :                                                    devid_kobj);
    1834             : 
    1835           0 :         memset(&device->devid_kobj, 0, sizeof(struct kobject));
    1836           0 :         complete(&device->kobj_unregister);
    1837           0 : }
    1838             : 
    1839             : static const struct kobj_type devid_ktype = {
    1840             :         .sysfs_ops      = &kobj_sysfs_ops,
    1841             :         .default_groups = devid_groups,
    1842             :         .release        = btrfs_release_devid_kobj,
    1843             : };
    1844             : 
    1845           0 : int btrfs_sysfs_add_device(struct btrfs_device *device)
    1846             : {
    1847           0 :         int ret;
    1848           0 :         unsigned int nofs_flag;
    1849           0 :         struct kobject *devices_kobj;
    1850           0 :         struct kobject *devinfo_kobj;
    1851             : 
    1852             :         /*
    1853             :          * Make sure we use the fs_info::fs_devices to fetch the kobjects even
    1854             :          * for the seed fs_devices
    1855             :          */
    1856           0 :         devices_kobj = device->fs_info->fs_devices->devices_kobj;
    1857           0 :         devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
    1858           0 :         ASSERT(devices_kobj);
    1859           0 :         ASSERT(devinfo_kobj);
    1860             : 
    1861           0 :         nofs_flag = memalloc_nofs_save();
    1862             : 
    1863           0 :         if (device->bdev) {
    1864           0 :                 struct kobject *disk_kobj = bdev_kobj(device->bdev);
    1865             : 
    1866           0 :                 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
    1867           0 :                 if (ret) {
    1868           0 :                         btrfs_warn(device->fs_info,
    1869             :                                 "creating sysfs device link for devid %llu failed: %d",
    1870             :                                 device->devid, ret);
    1871           0 :                         goto out;
    1872             :                 }
    1873             :         }
    1874             : 
    1875           0 :         init_completion(&device->kobj_unregister);
    1876           0 :         ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
    1877             :                                    devinfo_kobj, "%llu", device->devid);
    1878           0 :         if (ret) {
    1879           0 :                 kobject_put(&device->devid_kobj);
    1880           0 :                 btrfs_warn(device->fs_info,
    1881             :                            "devinfo init for devid %llu failed: %d",
    1882             :                            device->devid, ret);
    1883             :         }
    1884             : 
    1885           0 : out:
    1886           0 :         memalloc_nofs_restore(nofs_flag);
    1887           0 :         return ret;
    1888             : }
    1889             : 
    1890           0 : static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
    1891             : {
    1892           0 :         int ret;
    1893           0 :         struct btrfs_device *device;
    1894           0 :         struct btrfs_fs_devices *seed;
    1895             : 
    1896           0 :         list_for_each_entry(device, &fs_devices->devices, dev_list) {
    1897           0 :                 ret = btrfs_sysfs_add_device(device);
    1898           0 :                 if (ret)
    1899           0 :                         goto fail;
    1900             :         }
    1901             : 
    1902           0 :         list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
    1903           0 :                 list_for_each_entry(device, &seed->devices, dev_list) {
    1904           0 :                         ret = btrfs_sysfs_add_device(device);
    1905           0 :                         if (ret)
    1906           0 :                                 goto fail;
    1907             :                 }
    1908             :         }
    1909             : 
    1910             :         return 0;
    1911             : 
    1912           0 : fail:
    1913           0 :         btrfs_sysfs_remove_fs_devices(fs_devices);
    1914           0 :         return ret;
    1915             : }
    1916             : 
    1917           0 : void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
    1918             : {
    1919           0 :         int ret;
    1920             : 
    1921           0 :         ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
    1922           0 :         if (ret)
    1923           0 :                 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
    1924             :                         action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
    1925             :                         &disk_to_dev(bdev->bd_disk)->kobj);
    1926           0 : }
    1927             : 
    1928           0 : void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
    1929             : 
    1930             : {
    1931           0 :         char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
    1932             : 
    1933             :         /*
    1934             :          * Sprouting changes fsid of the mounted filesystem, rename the fsid
    1935             :          * directory
    1936             :          */
    1937           0 :         snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
    1938           0 :         if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
    1939           0 :                 btrfs_warn(fs_devices->fs_info,
    1940             :                                 "sysfs: failed to create fsid for sprout");
    1941           0 : }
    1942             : 
    1943           0 : void btrfs_sysfs_update_devid(struct btrfs_device *device)
    1944             : {
    1945           0 :         char tmp[24];
    1946             : 
    1947           0 :         snprintf(tmp, sizeof(tmp), "%llu", device->devid);
    1948             : 
    1949           0 :         if (kobject_rename(&device->devid_kobj, tmp))
    1950           0 :                 btrfs_warn(device->fs_devices->fs_info,
    1951             :                            "sysfs: failed to update devid for %llu",
    1952             :                            device->devid);
    1953           0 : }
    1954             : 
    1955             : /* /sys/fs/btrfs/ entry */
    1956             : static struct kset *btrfs_kset;
    1957             : 
    1958             : /*
    1959             :  * Creates:
    1960             :  *              /sys/fs/btrfs/UUID
    1961             :  *
    1962             :  * Can be called by the device discovery thread.
    1963             :  */
    1964           0 : int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
    1965             : {
    1966           0 :         int error;
    1967             : 
    1968           0 :         init_completion(&fs_devs->kobj_unregister);
    1969           0 :         fs_devs->fsid_kobj.kset = btrfs_kset;
    1970           0 :         error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
    1971           0 :                                      "%pU", fs_devs->fsid);
    1972           0 :         if (error) {
    1973           0 :                 kobject_put(&fs_devs->fsid_kobj);
    1974           0 :                 return error;
    1975             :         }
    1976             : 
    1977           0 :         fs_devs->devices_kobj = kobject_create_and_add("devices",
    1978             :                                                        &fs_devs->fsid_kobj);
    1979           0 :         if (!fs_devs->devices_kobj) {
    1980           0 :                 btrfs_err(fs_devs->fs_info,
    1981             :                           "failed to init sysfs device interface");
    1982           0 :                 btrfs_sysfs_remove_fsid(fs_devs);
    1983           0 :                 return -ENOMEM;
    1984             :         }
    1985             : 
    1986           0 :         fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
    1987             :                                                        &fs_devs->fsid_kobj);
    1988           0 :         if (!fs_devs->devinfo_kobj) {
    1989           0 :                 btrfs_err(fs_devs->fs_info,
    1990             :                           "failed to init sysfs devinfo kobject");
    1991           0 :                 btrfs_sysfs_remove_fsid(fs_devs);
    1992           0 :                 return -ENOMEM;
    1993             :         }
    1994             : 
    1995             :         return 0;
    1996             : }
    1997             : 
    1998           0 : int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
    1999             : {
    2000           0 :         int error;
    2001           0 :         struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
    2002           0 :         struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
    2003             : 
    2004           0 :         error = btrfs_sysfs_add_fs_devices(fs_devs);
    2005           0 :         if (error)
    2006             :                 return error;
    2007             : 
    2008           0 :         error = sysfs_create_files(fsid_kobj, btrfs_attrs);
    2009           0 :         if (error) {
    2010           0 :                 btrfs_sysfs_remove_fs_devices(fs_devs);
    2011           0 :                 return error;
    2012             :         }
    2013             : 
    2014           0 :         error = sysfs_create_group(fsid_kobj,
    2015             :                                    &btrfs_feature_attr_group);
    2016           0 :         if (error)
    2017           0 :                 goto failure;
    2018             : 
    2019             : #ifdef CONFIG_BTRFS_DEBUG
    2020             :         fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
    2021             :         if (!fs_info->debug_kobj) {
    2022             :                 error = -ENOMEM;
    2023             :                 goto failure;
    2024             :         }
    2025             : 
    2026             :         error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
    2027             :         if (error)
    2028             :                 goto failure;
    2029             : #endif
    2030             : 
    2031             :         /* Discard directory */
    2032           0 :         fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
    2033           0 :         if (!fs_info->discard_kobj) {
    2034           0 :                 error = -ENOMEM;
    2035           0 :                 goto failure;
    2036             :         }
    2037             : 
    2038           0 :         error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
    2039           0 :         if (error)
    2040           0 :                 goto failure;
    2041             : 
    2042           0 :         error = addrm_unknown_feature_attrs(fs_info, true);
    2043           0 :         if (error)
    2044           0 :                 goto failure;
    2045             : 
    2046           0 :         error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
    2047           0 :         if (error)
    2048           0 :                 goto failure;
    2049             : 
    2050           0 :         fs_info->space_info_kobj = kobject_create_and_add("allocation",
    2051             :                                                   fsid_kobj);
    2052           0 :         if (!fs_info->space_info_kobj) {
    2053           0 :                 error = -ENOMEM;
    2054           0 :                 goto failure;
    2055             :         }
    2056             : 
    2057           0 :         error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
    2058           0 :         if (error)
    2059           0 :                 goto failure;
    2060             : 
    2061             :         return 0;
    2062           0 : failure:
    2063           0 :         btrfs_sysfs_remove_mounted(fs_info);
    2064           0 :         return error;
    2065             : }
    2066             : 
    2067           0 : static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
    2068             :                                    struct kobj_attribute *a,
    2069             :                                    char *buf)
    2070             : {
    2071           0 :         struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
    2072           0 :         bool enabled;
    2073             : 
    2074           0 :         spin_lock(&fs_info->qgroup_lock);
    2075           0 :         enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
    2076           0 :         spin_unlock(&fs_info->qgroup_lock);
    2077             : 
    2078           0 :         return sysfs_emit(buf, "%d\n", enabled);
    2079             : }
    2080             : BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
    2081             : 
    2082           0 : static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
    2083             :                                         struct kobj_attribute *a,
    2084             :                                         char *buf)
    2085             : {
    2086           0 :         struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
    2087           0 :         bool inconsistent;
    2088             : 
    2089           0 :         spin_lock(&fs_info->qgroup_lock);
    2090           0 :         inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
    2091           0 :         spin_unlock(&fs_info->qgroup_lock);
    2092             : 
    2093           0 :         return sysfs_emit(buf, "%d\n", inconsistent);
    2094             : }
    2095             : BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
    2096             : 
    2097           0 : static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
    2098             :                                               struct kobj_attribute *a,
    2099             :                                               char *buf)
    2100             : {
    2101           0 :         struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
    2102           0 :         u8 result;
    2103             : 
    2104           0 :         spin_lock(&fs_info->qgroup_lock);
    2105           0 :         result = fs_info->qgroup_drop_subtree_thres;
    2106           0 :         spin_unlock(&fs_info->qgroup_lock);
    2107             : 
    2108           0 :         return sysfs_emit(buf, "%d\n", result);
    2109             : }
    2110             : 
    2111           0 : static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
    2112             :                                                struct kobj_attribute *a,
    2113             :                                                const char *buf, size_t len)
    2114             : {
    2115           0 :         struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
    2116           0 :         u8 new_thres;
    2117           0 :         int ret;
    2118             : 
    2119           0 :         ret = kstrtou8(buf, 10, &new_thres);
    2120           0 :         if (ret)
    2121             :                 return -EINVAL;
    2122             : 
    2123           0 :         if (new_thres > BTRFS_MAX_LEVEL)
    2124             :                 return -EINVAL;
    2125             : 
    2126           0 :         spin_lock(&fs_info->qgroup_lock);
    2127           0 :         fs_info->qgroup_drop_subtree_thres = new_thres;
    2128           0 :         spin_unlock(&fs_info->qgroup_lock);
    2129             : 
    2130           0 :         return len;
    2131             : }
    2132             : BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
    2133             :               qgroup_drop_subtree_thres_store);
    2134             : 
    2135             : /*
    2136             :  * Qgroups global info
    2137             :  *
    2138             :  * Path: /sys/fs/btrfs/<uuid>/qgroups/
    2139             :  */
    2140             : static struct attribute *qgroups_attrs[] = {
    2141             :         BTRFS_ATTR_PTR(qgroups, enabled),
    2142             :         BTRFS_ATTR_PTR(qgroups, inconsistent),
    2143             :         BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
    2144             :         NULL
    2145             : };
    2146             : ATTRIBUTE_GROUPS(qgroups);
    2147             : 
    2148           0 : static void qgroups_release(struct kobject *kobj)
    2149             : {
    2150           0 :         kfree(kobj);
    2151           0 : }
    2152             : 
    2153             : static const struct kobj_type qgroups_ktype = {
    2154             :         .sysfs_ops = &kobj_sysfs_ops,
    2155             :         .default_groups = qgroups_groups,
    2156             :         .release = qgroups_release,
    2157             : };
    2158             : 
    2159             : static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
    2160             : {
    2161           0 :         return to_fs_info(kobj->parent->parent);
    2162             : }
    2163             : 
    2164             : #define QGROUP_ATTR(_member, _show_name)                                        \
    2165             : static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj,         \
    2166             :                                            struct kobj_attribute *a,            \
    2167             :                                            char *buf)                           \
    2168             : {                                                                               \
    2169             :         struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);    \
    2170             :         struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,                 \
    2171             :                         struct btrfs_qgroup, kobj);                             \
    2172             :         return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf);      \
    2173             : }                                                                               \
    2174             : BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
    2175             : 
    2176             : #define QGROUP_RSV_ATTR(_name, _type)                                           \
    2177             : static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj,       \
    2178             :                                              struct kobj_attribute *a,          \
    2179             :                                              char *buf)                         \
    2180             : {                                                                               \
    2181             :         struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);    \
    2182             :         struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,                 \
    2183             :                         struct btrfs_qgroup, kobj);                             \
    2184             :         return btrfs_show_u64(&qgroup->rsv.values[_type],                        \
    2185             :                         &fs_info->qgroup_lock, buf);                             \
    2186             : }                                                                               \
    2187             : BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
    2188             : 
    2189           0 : QGROUP_ATTR(rfer, referenced);
    2190           0 : QGROUP_ATTR(excl, exclusive);
    2191           0 : QGROUP_ATTR(max_rfer, max_referenced);
    2192           0 : QGROUP_ATTR(max_excl, max_exclusive);
    2193           0 : QGROUP_ATTR(lim_flags, limit_flags);
    2194           0 : QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
    2195           0 : QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
    2196           0 : QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
    2197             : 
    2198             : /*
    2199             :  * Qgroup information.
    2200             :  *
    2201             :  * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
    2202             :  */
    2203             : static struct attribute *qgroup_attrs[] = {
    2204             :         BTRFS_ATTR_PTR(qgroup, referenced),
    2205             :         BTRFS_ATTR_PTR(qgroup, exclusive),
    2206             :         BTRFS_ATTR_PTR(qgroup, max_referenced),
    2207             :         BTRFS_ATTR_PTR(qgroup, max_exclusive),
    2208             :         BTRFS_ATTR_PTR(qgroup, limit_flags),
    2209             :         BTRFS_ATTR_PTR(qgroup, rsv_data),
    2210             :         BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
    2211             :         BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
    2212             :         NULL
    2213             : };
    2214             : ATTRIBUTE_GROUPS(qgroup);
    2215             : 
    2216           0 : static void qgroup_release(struct kobject *kobj)
    2217             : {
    2218           0 :         struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
    2219             : 
    2220           0 :         memset(&qgroup->kobj, 0, sizeof(*kobj));
    2221           0 : }
    2222             : 
    2223             : static const struct kobj_type qgroup_ktype = {
    2224             :         .sysfs_ops = &kobj_sysfs_ops,
    2225             :         .release = qgroup_release,
    2226             :         .default_groups = qgroup_groups,
    2227             : };
    2228             : 
    2229           0 : int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
    2230             :                                 struct btrfs_qgroup *qgroup)
    2231             : {
    2232           0 :         struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
    2233           0 :         int ret;
    2234             : 
    2235           0 :         if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
    2236             :                 return 0;
    2237           0 :         if (qgroup->kobj.state_initialized)
    2238             :                 return 0;
    2239           0 :         if (!qgroups_kobj)
    2240             :                 return -EINVAL;
    2241             : 
    2242           0 :         ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
    2243           0 :                         "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
    2244             :                         btrfs_qgroup_subvolid(qgroup->qgroupid));
    2245           0 :         if (ret < 0)
    2246           0 :                 kobject_put(&qgroup->kobj);
    2247             : 
    2248             :         return ret;
    2249             : }
    2250             : 
    2251           0 : void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
    2252             : {
    2253           0 :         struct btrfs_qgroup *qgroup;
    2254           0 :         struct btrfs_qgroup *next;
    2255             : 
    2256           0 :         if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
    2257             :                 return;
    2258             : 
    2259           0 :         rbtree_postorder_for_each_entry_safe(qgroup, next,
    2260             :                                              &fs_info->qgroup_tree, node)
    2261           0 :                 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
    2262           0 :         if (fs_info->qgroups_kobj) {
    2263           0 :                 kobject_del(fs_info->qgroups_kobj);
    2264           0 :                 kobject_put(fs_info->qgroups_kobj);
    2265           0 :                 fs_info->qgroups_kobj = NULL;
    2266             :         }
    2267             : }
    2268             : 
    2269             : /* Called when qgroups get initialized, thus there is no need for locking */
    2270           0 : int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
    2271             : {
    2272           0 :         struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
    2273           0 :         struct btrfs_qgroup *qgroup;
    2274           0 :         struct btrfs_qgroup *next;
    2275           0 :         int ret = 0;
    2276             : 
    2277           0 :         if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
    2278             :                 return 0;
    2279             : 
    2280           0 :         ASSERT(fsid_kobj);
    2281           0 :         if (fs_info->qgroups_kobj)
    2282             :                 return 0;
    2283             : 
    2284           0 :         fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    2285           0 :         if (!fs_info->qgroups_kobj)
    2286             :                 return -ENOMEM;
    2287             : 
    2288           0 :         ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
    2289             :                                    fsid_kobj, "qgroups");
    2290           0 :         if (ret < 0)
    2291           0 :                 goto out;
    2292             : 
    2293           0 :         rbtree_postorder_for_each_entry_safe(qgroup, next,
    2294             :                                              &fs_info->qgroup_tree, node) {
    2295           0 :                 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
    2296           0 :                 if (ret < 0)
    2297           0 :                         goto out;
    2298             :         }
    2299             : 
    2300           0 : out:
    2301           0 :         if (ret < 0)
    2302           0 :                 btrfs_sysfs_del_qgroups(fs_info);
    2303             :         return ret;
    2304             : }
    2305             : 
    2306           0 : void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
    2307             :                                 struct btrfs_qgroup *qgroup)
    2308             : {
    2309           0 :         if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
    2310             :                 return;
    2311             : 
    2312           0 :         if (qgroup->kobj.state_initialized) {
    2313           0 :                 kobject_del(&qgroup->kobj);
    2314           0 :                 kobject_put(&qgroup->kobj);
    2315             :         }
    2316             : }
    2317             : 
    2318             : /*
    2319             :  * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
    2320             :  * values in superblock. Call after any changes to incompat/compat_ro flags
    2321             :  */
    2322           0 : void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info)
    2323             : {
    2324           0 :         struct kobject *fsid_kobj;
    2325           0 :         int ret;
    2326             : 
    2327           0 :         if (!fs_info)
    2328             :                 return;
    2329             : 
    2330           0 :         fsid_kobj = &fs_info->fs_devices->fsid_kobj;
    2331           0 :         if (!fsid_kobj->state_initialized)
    2332             :                 return;
    2333             : 
    2334           0 :         ret = sysfs_update_group(fsid_kobj, &btrfs_feature_attr_group);
    2335           0 :         if (ret < 0)
    2336           0 :                 btrfs_warn(fs_info,
    2337             :                            "failed to update /sys/fs/btrfs/%pU/features: %d",
    2338             :                            fs_info->fs_devices->fsid, ret);
    2339             : }
    2340             : 
    2341           2 : int __init btrfs_init_sysfs(void)
    2342             : {
    2343           2 :         int ret;
    2344             : 
    2345           2 :         btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
    2346           2 :         if (!btrfs_kset)
    2347             :                 return -ENOMEM;
    2348             : 
    2349           2 :         init_feature_attrs();
    2350           2 :         ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
    2351           2 :         if (ret)
    2352           0 :                 goto out2;
    2353           2 :         ret = sysfs_merge_group(&btrfs_kset->kobj,
    2354             :                                 &btrfs_static_feature_attr_group);
    2355           2 :         if (ret)
    2356           0 :                 goto out_remove_group;
    2357             : 
    2358             : #ifdef CONFIG_BTRFS_DEBUG
    2359             :         ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
    2360             :         if (ret) {
    2361             :                 sysfs_unmerge_group(&btrfs_kset->kobj,
    2362             :                                     &btrfs_static_feature_attr_group);
    2363             :                 goto out_remove_group;
    2364             :         }
    2365             : #endif
    2366             : 
    2367             :         return 0;
    2368             : 
    2369             : out_remove_group:
    2370           0 :         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
    2371           0 : out2:
    2372           0 :         kset_unregister(btrfs_kset);
    2373             : 
    2374           0 :         return ret;
    2375             : }
    2376             : 
    2377           0 : void __cold btrfs_exit_sysfs(void)
    2378             : {
    2379           0 :         sysfs_unmerge_group(&btrfs_kset->kobj,
    2380             :                             &btrfs_static_feature_attr_group);
    2381           0 :         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
    2382             : #ifdef CONFIG_BTRFS_DEBUG
    2383             :         sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
    2384             : #endif
    2385           0 :         kset_unregister(btrfs_kset);
    2386           0 : }

Generated by: LCOV version 1.14