Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2007 Oracle.  All rights reserved.
0004  */
0005 
0006 #include <linux/sched.h>
0007 #include <linux/sched/mm.h>
0008 #include <linux/slab.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/completion.h>
0011 #include <linux/bug.h>
0012 #include <crypto/hash.h>
0013 
0014 #include "ctree.h"
0015 #include "discard.h"
0016 #include "disk-io.h"
0017 #include "send.h"
0018 #include "transaction.h"
0019 #include "sysfs.h"
0020 #include "volumes.h"
0021 #include "space-info.h"
0022 #include "block-group.h"
0023 #include "qgroup.h"
0024 #include "misc.h"
0025 
0026 /*
0027  * Structure name                       Path
0028  * --------------------------------------------------------------------------
0029  * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
0030  * btrfs_supported_feature_attrs    /sys/fs/btrfs/features and
0031  *                  /sys/fs/btrfs/<uuid>/features
0032  * btrfs_attrs              /sys/fs/btrfs/<uuid>
0033  * devid_attrs              /sys/fs/btrfs/<uuid>/devinfo/<devid>
0034  * allocation_attrs         /sys/fs/btrfs/<uuid>/allocation
0035  * qgroup_attrs             /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
0036  * space_info_attrs         /sys/fs/btrfs/<uuid>/allocation/<bg-type>
0037  * raid_attrs               /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
0038  *
0039  * When built with BTRFS_CONFIG_DEBUG:
0040  *
0041  * btrfs_debug_feature_attrs        /sys/fs/btrfs/debug
0042  * btrfs_debug_mount_attrs      /sys/fs/btrfs/<uuid>/debug
0043  * discard_debug_attrs          /sys/fs/btrfs/<uuid>/debug/discard
0044  */
0045 
0046 struct btrfs_feature_attr {
0047     struct kobj_attribute kobj_attr;
0048     enum btrfs_feature_set feature_set;
0049     u64 feature_bit;
0050 };
0051 
0052 /* For raid type sysfs entries */
0053 struct raid_kobject {
0054     u64 flags;
0055     struct kobject kobj;
0056 };
0057 
0058 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)           \
0059 {                                   \
0060     .attr   = { .name = __stringify(_name), .mode = _mode },    \
0061     .show   = _show,                        \
0062     .store  = _store,                       \
0063 }
0064 
0065 #define BTRFS_ATTR_W(_prefix, _name, _store)                    \
0066     static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
0067             __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
0068 
0069 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store)            \
0070     static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
0071             __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
0072 
0073 #define BTRFS_ATTR(_prefix, _name, _show)               \
0074     static struct kobj_attribute btrfs_attr_##_prefix##_##_name =   \
0075             __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
0076 
0077 #define BTRFS_ATTR_PTR(_prefix, _name)                  \
0078     (&btrfs_attr_##_prefix##_##_name.attr)
0079 
0080 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
0081 static struct btrfs_feature_attr btrfs_attr_features_##_name = {         \
0082     .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,                \
0083                       btrfs_feature_attr_show,           \
0084                       btrfs_feature_attr_store),         \
0085     .feature_set    = _feature_set,                      \
0086     .feature_bit    = _feature_prefix ##_## _feature_bit,            \
0087 }
0088 #define BTRFS_FEAT_ATTR_PTR(_name)                       \
0089     (&btrfs_attr_features_##_name.kobj_attr.attr)
0090 
0091 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
0092     BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
0093 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
0094     BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
0095 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
0096     BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
0097 
0098 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
0099 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
0100 static struct kobject *get_btrfs_kobj(struct kobject *kobj);
0101 
0102 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
0103 {
0104     return container_of(a, struct btrfs_feature_attr, kobj_attr);
0105 }
0106 
0107 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
0108 {
0109     return container_of(attr, struct kobj_attribute, attr);
0110 }
0111 
0112 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
0113         struct attribute *attr)
0114 {
0115     return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
0116 }
0117 
0118 static u64 get_features(struct btrfs_fs_info *fs_info,
0119             enum btrfs_feature_set set)
0120 {
0121     struct btrfs_super_block *disk_super = fs_info->super_copy;
0122     if (set == FEAT_COMPAT)
0123         return btrfs_super_compat_flags(disk_super);
0124     else if (set == FEAT_COMPAT_RO)
0125         return btrfs_super_compat_ro_flags(disk_super);
0126     else
0127         return btrfs_super_incompat_flags(disk_super);
0128 }
0129 
0130 static void set_features(struct btrfs_fs_info *fs_info,
0131              enum btrfs_feature_set set, u64 features)
0132 {
0133     struct btrfs_super_block *disk_super = fs_info->super_copy;
0134     if (set == FEAT_COMPAT)
0135         btrfs_set_super_compat_flags(disk_super, features);
0136     else if (set == FEAT_COMPAT_RO)
0137         btrfs_set_super_compat_ro_flags(disk_super, features);
0138     else
0139         btrfs_set_super_incompat_flags(disk_super, features);
0140 }
0141 
0142 static int can_modify_feature(struct btrfs_feature_attr *fa)
0143 {
0144     int val = 0;
0145     u64 set, clear;
0146     switch (fa->feature_set) {
0147     case FEAT_COMPAT:
0148         set = BTRFS_FEATURE_COMPAT_SAFE_SET;
0149         clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
0150         break;
0151     case FEAT_COMPAT_RO:
0152         set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
0153         clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
0154         break;
0155     case FEAT_INCOMPAT:
0156         set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
0157         clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
0158         break;
0159     default:
0160         pr_warn("btrfs: sysfs: unknown feature set %d\n",
0161                 fa->feature_set);
0162         return 0;
0163     }
0164 
0165     if (set & fa->feature_bit)
0166         val |= 1;
0167     if (clear & fa->feature_bit)
0168         val |= 2;
0169 
0170     return val;
0171 }
0172 
0173 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
0174                        struct kobj_attribute *a, char *buf)
0175 {
0176     int val = 0;
0177     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0178     struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
0179     if (fs_info) {
0180         u64 features = get_features(fs_info, fa->feature_set);
0181         if (features & fa->feature_bit)
0182             val = 1;
0183     } else
0184         val = can_modify_feature(fa);
0185 
0186     return sysfs_emit(buf, "%d\n", val);
0187 }
0188 
0189 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
0190                     struct kobj_attribute *a,
0191                     const char *buf, size_t count)
0192 {
0193     struct btrfs_fs_info *fs_info;
0194     struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
0195     u64 features, set, clear;
0196     unsigned long val;
0197     int ret;
0198 
0199     fs_info = to_fs_info(kobj);
0200     if (!fs_info)
0201         return -EPERM;
0202 
0203     if (sb_rdonly(fs_info->sb))
0204         return -EROFS;
0205 
0206     ret = kstrtoul(skip_spaces(buf), 0, &val);
0207     if (ret)
0208         return ret;
0209 
0210     if (fa->feature_set == FEAT_COMPAT) {
0211         set = BTRFS_FEATURE_COMPAT_SAFE_SET;
0212         clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
0213     } else if (fa->feature_set == FEAT_COMPAT_RO) {
0214         set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
0215         clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
0216     } else {
0217         set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
0218         clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
0219     }
0220 
0221     features = get_features(fs_info, fa->feature_set);
0222 
0223     /* Nothing to do */
0224     if ((val && (features & fa->feature_bit)) ||
0225         (!val && !(features & fa->feature_bit)))
0226         return count;
0227 
0228     if ((val && !(set & fa->feature_bit)) ||
0229         (!val && !(clear & fa->feature_bit))) {
0230         btrfs_info(fs_info,
0231             "%sabling feature %s on mounted fs is not supported.",
0232             val ? "En" : "Dis", fa->kobj_attr.attr.name);
0233         return -EPERM;
0234     }
0235 
0236     btrfs_info(fs_info, "%s %s feature flag",
0237            val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
0238 
0239     spin_lock(&fs_info->super_lock);
0240     features = get_features(fs_info, fa->feature_set);
0241     if (val)
0242         features |= fa->feature_bit;
0243     else
0244         features &= ~fa->feature_bit;
0245     set_features(fs_info, fa->feature_set, features);
0246     spin_unlock(&fs_info->super_lock);
0247 
0248     /*
0249      * We don't want to do full transaction commit from inside sysfs
0250      */
0251     btrfs_set_pending(fs_info, COMMIT);
0252     wake_up_process(fs_info->transaction_kthread);
0253 
0254     return count;
0255 }
0256 
0257 static umode_t btrfs_feature_visible(struct kobject *kobj,
0258                      struct attribute *attr, int unused)
0259 {
0260     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0261     umode_t mode = attr->mode;
0262 
0263     if (fs_info) {
0264         struct btrfs_feature_attr *fa;
0265         u64 features;
0266 
0267         fa = attr_to_btrfs_feature_attr(attr);
0268         features = get_features(fs_info, fa->feature_set);
0269 
0270         if (can_modify_feature(fa))
0271             mode |= S_IWUSR;
0272         else if (!(features & fa->feature_bit))
0273             mode = 0;
0274     }
0275 
0276     return mode;
0277 }
0278 
0279 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
0280 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
0281 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
0282 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
0283 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
0284 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
0285 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
0286 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
0287 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
0288 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
0289 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
0290 #ifdef CONFIG_BLK_DEV_ZONED
0291 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
0292 #endif
0293 #ifdef CONFIG_BTRFS_DEBUG
0294 /* Remove once support for extent tree v2 is feature complete */
0295 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
0296 #endif
0297 #ifdef CONFIG_FS_VERITY
0298 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
0299 #endif
0300 
0301 /*
0302  * Features which depend on feature bits and may differ between each fs.
0303  *
0304  * /sys/fs/btrfs/features      - all available features implemented by this version
0305  * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
0306  *                               can be changed on a mounted filesystem.
0307  */
0308 static struct attribute *btrfs_supported_feature_attrs[] = {
0309     BTRFS_FEAT_ATTR_PTR(default_subvol),
0310     BTRFS_FEAT_ATTR_PTR(mixed_groups),
0311     BTRFS_FEAT_ATTR_PTR(compress_lzo),
0312     BTRFS_FEAT_ATTR_PTR(compress_zstd),
0313     BTRFS_FEAT_ATTR_PTR(extended_iref),
0314     BTRFS_FEAT_ATTR_PTR(raid56),
0315     BTRFS_FEAT_ATTR_PTR(skinny_metadata),
0316     BTRFS_FEAT_ATTR_PTR(no_holes),
0317     BTRFS_FEAT_ATTR_PTR(metadata_uuid),
0318     BTRFS_FEAT_ATTR_PTR(free_space_tree),
0319     BTRFS_FEAT_ATTR_PTR(raid1c34),
0320 #ifdef CONFIG_BLK_DEV_ZONED
0321     BTRFS_FEAT_ATTR_PTR(zoned),
0322 #endif
0323 #ifdef CONFIG_BTRFS_DEBUG
0324     BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
0325 #endif
0326 #ifdef CONFIG_FS_VERITY
0327     BTRFS_FEAT_ATTR_PTR(verity),
0328 #endif
0329     NULL
0330 };
0331 
0332 static const struct attribute_group btrfs_feature_attr_group = {
0333     .name = "features",
0334     .is_visible = btrfs_feature_visible,
0335     .attrs = btrfs_supported_feature_attrs,
0336 };
0337 
0338 static ssize_t rmdir_subvol_show(struct kobject *kobj,
0339                  struct kobj_attribute *ka, char *buf)
0340 {
0341     return sysfs_emit(buf, "0\n");
0342 }
0343 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
0344 
0345 static ssize_t supported_checksums_show(struct kobject *kobj,
0346                     struct kobj_attribute *a, char *buf)
0347 {
0348     ssize_t ret = 0;
0349     int i;
0350 
0351     for (i = 0; i < btrfs_get_num_csums(); i++) {
0352         /*
0353          * This "trick" only works as long as 'enum btrfs_csum_type' has
0354          * no holes in it
0355          */
0356         ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
0357                      btrfs_super_csum_name(i));
0358 
0359     }
0360 
0361     ret += sysfs_emit_at(buf, ret, "\n");
0362     return ret;
0363 }
0364 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
0365 
0366 static ssize_t send_stream_version_show(struct kobject *kobj,
0367                     struct kobj_attribute *ka, char *buf)
0368 {
0369     return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
0370 }
0371 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
0372 
0373 static const char *rescue_opts[] = {
0374     "usebackuproot",
0375     "nologreplay",
0376     "ignorebadroots",
0377     "ignoredatacsums",
0378     "all",
0379 };
0380 
0381 static ssize_t supported_rescue_options_show(struct kobject *kobj,
0382                          struct kobj_attribute *a,
0383                          char *buf)
0384 {
0385     ssize_t ret = 0;
0386     int i;
0387 
0388     for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
0389         ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
0390     ret += sysfs_emit_at(buf, ret, "\n");
0391     return ret;
0392 }
0393 BTRFS_ATTR(static_feature, supported_rescue_options,
0394        supported_rescue_options_show);
0395 
0396 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
0397                       struct kobj_attribute *a,
0398                       char *buf)
0399 {
0400     ssize_t ret = 0;
0401 
0402     /* An artificial limit to only support 4K and PAGE_SIZE */
0403     if (PAGE_SIZE > SZ_4K)
0404         ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
0405     ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
0406 
0407     return ret;
0408 }
0409 BTRFS_ATTR(static_feature, supported_sectorsizes,
0410        supported_sectorsizes_show);
0411 
0412 /*
0413  * Features which only depend on kernel version.
0414  *
0415  * These are listed in /sys/fs/btrfs/features along with
0416  * btrfs_supported_feature_attrs.
0417  */
0418 static struct attribute *btrfs_supported_static_feature_attrs[] = {
0419     BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
0420     BTRFS_ATTR_PTR(static_feature, supported_checksums),
0421     BTRFS_ATTR_PTR(static_feature, send_stream_version),
0422     BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
0423     BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
0424     NULL
0425 };
0426 
0427 static const struct attribute_group btrfs_static_feature_attr_group = {
0428     .name = "features",
0429     .attrs = btrfs_supported_static_feature_attrs,
0430 };
0431 
0432 #ifdef CONFIG_BTRFS_DEBUG
0433 
0434 /*
0435  * Discard statistics and tunables
0436  */
0437 #define discard_to_fs_info(_kobj)   to_fs_info((_kobj)->parent->parent)
0438 
0439 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
0440                         struct kobj_attribute *a,
0441                         char *buf)
0442 {
0443     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0444 
0445     return sysfs_emit(buf, "%lld\n",
0446             atomic64_read(&fs_info->discard_ctl.discardable_bytes));
0447 }
0448 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
0449 
0450 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
0451                           struct kobj_attribute *a,
0452                           char *buf)
0453 {
0454     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0455 
0456     return sysfs_emit(buf, "%d\n",
0457             atomic_read(&fs_info->discard_ctl.discardable_extents));
0458 }
0459 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
0460 
0461 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
0462                            struct kobj_attribute *a,
0463                            char *buf)
0464 {
0465     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0466 
0467     return sysfs_emit(buf, "%llu\n",
0468               fs_info->discard_ctl.discard_bitmap_bytes);
0469 }
0470 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
0471 
0472 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
0473                           struct kobj_attribute *a,
0474                           char *buf)
0475 {
0476     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0477 
0478     return sysfs_emit(buf, "%lld\n",
0479         atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
0480 }
0481 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
0482 
0483 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
0484                            struct kobj_attribute *a,
0485                            char *buf)
0486 {
0487     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0488 
0489     return sysfs_emit(buf, "%llu\n",
0490               fs_info->discard_ctl.discard_extent_bytes);
0491 }
0492 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
0493 
0494 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
0495                          struct kobj_attribute *a,
0496                          char *buf)
0497 {
0498     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0499 
0500     return sysfs_emit(buf, "%u\n",
0501               READ_ONCE(fs_info->discard_ctl.iops_limit));
0502 }
0503 
0504 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
0505                           struct kobj_attribute *a,
0506                           const char *buf, size_t len)
0507 {
0508     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0509     struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
0510     u32 iops_limit;
0511     int ret;
0512 
0513     ret = kstrtou32(buf, 10, &iops_limit);
0514     if (ret)
0515         return -EINVAL;
0516 
0517     WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
0518     btrfs_discard_calc_delay(discard_ctl);
0519     btrfs_discard_schedule_work(discard_ctl, true);
0520     return len;
0521 }
0522 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
0523           btrfs_discard_iops_limit_store);
0524 
0525 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
0526                          struct kobj_attribute *a,
0527                          char *buf)
0528 {
0529     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0530 
0531     return sysfs_emit(buf, "%u\n",
0532               READ_ONCE(fs_info->discard_ctl.kbps_limit));
0533 }
0534 
0535 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
0536                           struct kobj_attribute *a,
0537                           const char *buf, size_t len)
0538 {
0539     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0540     struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
0541     u32 kbps_limit;
0542     int ret;
0543 
0544     ret = kstrtou32(buf, 10, &kbps_limit);
0545     if (ret)
0546         return -EINVAL;
0547 
0548     WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
0549     btrfs_discard_schedule_work(discard_ctl, true);
0550     return len;
0551 }
0552 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
0553           btrfs_discard_kbps_limit_store);
0554 
0555 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
0556                            struct kobj_attribute *a,
0557                            char *buf)
0558 {
0559     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0560 
0561     return sysfs_emit(buf, "%llu\n",
0562               READ_ONCE(fs_info->discard_ctl.max_discard_size));
0563 }
0564 
0565 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
0566                             struct kobj_attribute *a,
0567                             const char *buf, size_t len)
0568 {
0569     struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
0570     struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
0571     u64 max_discard_size;
0572     int ret;
0573 
0574     ret = kstrtou64(buf, 10, &max_discard_size);
0575     if (ret)
0576         return -EINVAL;
0577 
0578     WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
0579 
0580     return len;
0581 }
0582 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
0583           btrfs_discard_max_discard_size_store);
0584 
0585 /*
0586  * Per-filesystem debugging of discard (when mounted with discard=async).
0587  *
0588  * Path: /sys/fs/btrfs/<uuid>/debug/discard/
0589  */
0590 static const struct attribute *discard_debug_attrs[] = {
0591     BTRFS_ATTR_PTR(discard, discardable_bytes),
0592     BTRFS_ATTR_PTR(discard, discardable_extents),
0593     BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
0594     BTRFS_ATTR_PTR(discard, discard_bytes_saved),
0595     BTRFS_ATTR_PTR(discard, discard_extent_bytes),
0596     BTRFS_ATTR_PTR(discard, iops_limit),
0597     BTRFS_ATTR_PTR(discard, kbps_limit),
0598     BTRFS_ATTR_PTR(discard, max_discard_size),
0599     NULL,
0600 };
0601 
0602 /*
0603  * Per-filesystem runtime debugging exported via sysfs.
0604  *
0605  * Path: /sys/fs/btrfs/UUID/debug/
0606  */
0607 static const struct attribute *btrfs_debug_mount_attrs[] = {
0608     NULL,
0609 };
0610 
0611 /*
0612  * Runtime debugging exported via sysfs, applies to all mounted filesystems.
0613  *
0614  * Path: /sys/fs/btrfs/debug
0615  */
0616 static struct attribute *btrfs_debug_feature_attrs[] = {
0617     NULL
0618 };
0619 
0620 static const struct attribute_group btrfs_debug_feature_attr_group = {
0621     .name = "debug",
0622     .attrs = btrfs_debug_feature_attrs,
0623 };
0624 
0625 #endif
0626 
0627 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
0628 {
0629     u64 val;
0630     if (lock)
0631         spin_lock(lock);
0632     val = *value_ptr;
0633     if (lock)
0634         spin_unlock(lock);
0635     return sysfs_emit(buf, "%llu\n", val);
0636 }
0637 
0638 static ssize_t global_rsv_size_show(struct kobject *kobj,
0639                     struct kobj_attribute *ka, char *buf)
0640 {
0641     struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
0642     struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
0643     return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
0644 }
0645 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
0646 
0647 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
0648                     struct kobj_attribute *a, char *buf)
0649 {
0650     struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
0651     struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
0652     return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
0653 }
0654 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
0655 
0656 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
0657 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
0658 
0659 static ssize_t raid_bytes_show(struct kobject *kobj,
0660                    struct kobj_attribute *attr, char *buf);
0661 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
0662 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
0663 
0664 static ssize_t raid_bytes_show(struct kobject *kobj,
0665                    struct kobj_attribute *attr, char *buf)
0666 
0667 {
0668     struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
0669     struct btrfs_block_group *block_group;
0670     int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
0671     u64 val = 0;
0672 
0673     down_read(&sinfo->groups_sem);
0674     list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
0675         if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
0676             val += block_group->length;
0677         else
0678             val += block_group->used;
0679     }
0680     up_read(&sinfo->groups_sem);
0681     return sysfs_emit(buf, "%llu\n", val);
0682 }
0683 
0684 /*
0685  * Allocation information about block group profiles.
0686  *
0687  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
0688  */
0689 static struct attribute *raid_attrs[] = {
0690     BTRFS_ATTR_PTR(raid, total_bytes),
0691     BTRFS_ATTR_PTR(raid, used_bytes),
0692     NULL
0693 };
0694 ATTRIBUTE_GROUPS(raid);
0695 
0696 static void release_raid_kobj(struct kobject *kobj)
0697 {
0698     kfree(to_raid_kobj(kobj));
0699 }
0700 
0701 static struct kobj_type btrfs_raid_ktype = {
0702     .sysfs_ops = &kobj_sysfs_ops,
0703     .release = release_raid_kobj,
0704     .default_groups = raid_groups,
0705 };
0706 
0707 #define SPACE_INFO_ATTR(field)                      \
0708 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj,  \
0709                          struct kobj_attribute *a,  \
0710                          char *buf)         \
0711 {                                   \
0712     struct btrfs_space_info *sinfo = to_space_info(kobj);       \
0713     return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf);    \
0714 }                                   \
0715 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
0716 
0717 static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
0718                      struct kobj_attribute *a, char *buf)
0719 {
0720     struct btrfs_space_info *sinfo = to_space_info(kobj);
0721 
0722     return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
0723 }
0724 
0725 /*
0726  * Store new chunk size in space info. Can be called on a read-only filesystem.
0727  *
0728  * If the new chunk size value is larger than 10% of free space it is reduced
0729  * to match that limit. Alignment must be to 256M and the system chunk size
0730  * cannot be set.
0731  */
0732 static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
0733                       struct kobj_attribute *a,
0734                       const char *buf, size_t len)
0735 {
0736     struct btrfs_space_info *space_info = to_space_info(kobj);
0737     struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
0738     char *retptr;
0739     u64 val;
0740 
0741     if (!capable(CAP_SYS_ADMIN))
0742         return -EPERM;
0743 
0744     if (!fs_info->fs_devices)
0745         return -EINVAL;
0746 
0747     if (btrfs_is_zoned(fs_info))
0748         return -EINVAL;
0749 
0750     /* System block type must not be changed. */
0751     if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
0752         return -EPERM;
0753 
0754     val = memparse(buf, &retptr);
0755     /* There could be trailing '\n', also catch any typos after the value */
0756     retptr = skip_spaces(retptr);
0757     if (*retptr != 0 || val == 0)
0758         return -EINVAL;
0759 
0760     val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
0761 
0762     /* Limit stripe size to 10% of available space. */
0763     val = min(div_factor(fs_info->fs_devices->total_rw_bytes, 1), val);
0764 
0765     /* Must be multiple of 256M. */
0766     val &= ~((u64)SZ_256M - 1);
0767 
0768     /* Must be at least 256M. */
0769     if (val < SZ_256M)
0770         return -EINVAL;
0771 
0772     btrfs_update_space_info_chunk_size(space_info, val);
0773 
0774     return len;
0775 }
0776 
0777 #ifdef CONFIG_BTRFS_DEBUG
0778 /*
0779  * Request chunk allocation with current chunk size.
0780  */
0781 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
0782                          struct kobj_attribute *a,
0783                          const char *buf, size_t len)
0784 {
0785     struct btrfs_space_info *space_info = to_space_info(kobj);
0786     struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
0787     struct btrfs_trans_handle *trans;
0788     bool val;
0789     int ret;
0790 
0791     if (!capable(CAP_SYS_ADMIN))
0792         return -EPERM;
0793 
0794     if (sb_rdonly(fs_info->sb))
0795         return -EROFS;
0796 
0797     ret = kstrtobool(buf, &val);
0798     if (ret)
0799         return ret;
0800 
0801     if (!val)
0802         return -EINVAL;
0803 
0804     /*
0805      * This is unsafe to be called from sysfs context and may cause
0806      * unexpected problems.
0807      */
0808     trans = btrfs_start_transaction(fs_info->tree_root, 0);
0809     if (IS_ERR(trans))
0810         return PTR_ERR(trans);
0811     ret = btrfs_force_chunk_alloc(trans, space_info->flags);
0812     btrfs_end_transaction(trans);
0813 
0814     if (ret == 1)
0815         return len;
0816 
0817     return -ENOSPC;
0818 }
0819 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
0820 
0821 #endif
0822 
0823 SPACE_INFO_ATTR(flags);
0824 SPACE_INFO_ATTR(total_bytes);
0825 SPACE_INFO_ATTR(bytes_used);
0826 SPACE_INFO_ATTR(bytes_pinned);
0827 SPACE_INFO_ATTR(bytes_reserved);
0828 SPACE_INFO_ATTR(bytes_may_use);
0829 SPACE_INFO_ATTR(bytes_readonly);
0830 SPACE_INFO_ATTR(bytes_zone_unusable);
0831 SPACE_INFO_ATTR(disk_used);
0832 SPACE_INFO_ATTR(disk_total);
0833 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
0834 
0835 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
0836                              struct kobj_attribute *a,
0837                              char *buf)
0838 {
0839     struct btrfs_space_info *space_info = to_space_info(kobj);
0840     ssize_t ret;
0841 
0842     ret = sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
0843 
0844     return ret;
0845 }
0846 
0847 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
0848                               struct kobj_attribute *a,
0849                               const char *buf, size_t len)
0850 {
0851     struct btrfs_space_info *space_info = to_space_info(kobj);
0852     int thresh;
0853     int ret;
0854 
0855     ret = kstrtoint(buf, 10, &thresh);
0856     if (ret)
0857         return ret;
0858 
0859     if (thresh < 0 || thresh > 100)
0860         return -EINVAL;
0861 
0862     WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
0863 
0864     return len;
0865 }
0866 
0867 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
0868           btrfs_sinfo_bg_reclaim_threshold_show,
0869           btrfs_sinfo_bg_reclaim_threshold_store);
0870 
0871 /*
0872  * Allocation information about block group types.
0873  *
0874  * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
0875  */
0876 static struct attribute *space_info_attrs[] = {
0877     BTRFS_ATTR_PTR(space_info, flags),
0878     BTRFS_ATTR_PTR(space_info, total_bytes),
0879     BTRFS_ATTR_PTR(space_info, bytes_used),
0880     BTRFS_ATTR_PTR(space_info, bytes_pinned),
0881     BTRFS_ATTR_PTR(space_info, bytes_reserved),
0882     BTRFS_ATTR_PTR(space_info, bytes_may_use),
0883     BTRFS_ATTR_PTR(space_info, bytes_readonly),
0884     BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
0885     BTRFS_ATTR_PTR(space_info, disk_used),
0886     BTRFS_ATTR_PTR(space_info, disk_total),
0887     BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
0888     BTRFS_ATTR_PTR(space_info, chunk_size),
0889 #ifdef CONFIG_BTRFS_DEBUG
0890     BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
0891 #endif
0892     NULL,
0893 };
0894 ATTRIBUTE_GROUPS(space_info);
0895 
0896 static void space_info_release(struct kobject *kobj)
0897 {
0898     struct btrfs_space_info *sinfo = to_space_info(kobj);
0899     kfree(sinfo);
0900 }
0901 
0902 static struct kobj_type space_info_ktype = {
0903     .sysfs_ops = &kobj_sysfs_ops,
0904     .release = space_info_release,
0905     .default_groups = space_info_groups,
0906 };
0907 
0908 /*
0909  * Allocation information about block groups.
0910  *
0911  * Path: /sys/fs/btrfs/<uuid>/allocation/
0912  */
0913 static const struct attribute *allocation_attrs[] = {
0914     BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
0915     BTRFS_ATTR_PTR(allocation, global_rsv_size),
0916     NULL,
0917 };
0918 
0919 static ssize_t btrfs_label_show(struct kobject *kobj,
0920                 struct kobj_attribute *a, char *buf)
0921 {
0922     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0923     char *label = fs_info->super_copy->label;
0924     ssize_t ret;
0925 
0926     spin_lock(&fs_info->super_lock);
0927     ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
0928     spin_unlock(&fs_info->super_lock);
0929 
0930     return ret;
0931 }
0932 
0933 static ssize_t btrfs_label_store(struct kobject *kobj,
0934                  struct kobj_attribute *a,
0935                  const char *buf, size_t len)
0936 {
0937     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0938     size_t p_len;
0939 
0940     if (!fs_info)
0941         return -EPERM;
0942 
0943     if (sb_rdonly(fs_info->sb))
0944         return -EROFS;
0945 
0946     /*
0947      * p_len is the len until the first occurrence of either
0948      * '\n' or '\0'
0949      */
0950     p_len = strcspn(buf, "\n");
0951 
0952     if (p_len >= BTRFS_LABEL_SIZE)
0953         return -EINVAL;
0954 
0955     spin_lock(&fs_info->super_lock);
0956     memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
0957     memcpy(fs_info->super_copy->label, buf, p_len);
0958     spin_unlock(&fs_info->super_lock);
0959 
0960     /*
0961      * We don't want to do full transaction commit from inside sysfs
0962      */
0963     btrfs_set_pending(fs_info, COMMIT);
0964     wake_up_process(fs_info->transaction_kthread);
0965 
0966     return len;
0967 }
0968 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
0969 
0970 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
0971                 struct kobj_attribute *a, char *buf)
0972 {
0973     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0974 
0975     return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
0976 }
0977 
0978 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
0979 
0980 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
0981                 struct kobj_attribute *a, char *buf)
0982 {
0983     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0984 
0985     return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
0986 }
0987 
0988 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
0989 
0990 static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
0991                        struct kobj_attribute *a, char *buf)
0992 {
0993     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
0994 
0995     return sysfs_emit(buf,
0996         "commits %llu\n"
0997         "last_commit_ms %llu\n"
0998         "max_commit_ms %llu\n"
0999         "total_commit_ms %llu\n",
1000         fs_info->commit_stats.commit_count,
1001         div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1002         div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1003         div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1004 }
1005 
1006 static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1007                     struct kobj_attribute *a,
1008                     const char *buf, size_t len)
1009 {
1010     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1011     unsigned long val;
1012     int ret;
1013 
1014     if (!fs_info)
1015         return -EPERM;
1016 
1017     if (!capable(CAP_SYS_RESOURCE))
1018         return -EPERM;
1019 
1020     ret = kstrtoul(buf, 10, &val);
1021     if (ret)
1022         return ret;
1023     if (val)
1024         return -EINVAL;
1025 
1026     WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1027 
1028     return len;
1029 }
1030 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1031 
1032 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1033                 struct kobj_attribute *a, char *buf)
1034 {
1035     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1036 
1037     return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1038 }
1039 
1040 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1041 
1042 static ssize_t quota_override_show(struct kobject *kobj,
1043                    struct kobj_attribute *a, char *buf)
1044 {
1045     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1046     int quota_override;
1047 
1048     quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1049     return sysfs_emit(buf, "%d\n", quota_override);
1050 }
1051 
1052 static ssize_t quota_override_store(struct kobject *kobj,
1053                     struct kobj_attribute *a,
1054                     const char *buf, size_t len)
1055 {
1056     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1057     unsigned long knob;
1058     int err;
1059 
1060     if (!fs_info)
1061         return -EPERM;
1062 
1063     if (!capable(CAP_SYS_RESOURCE))
1064         return -EPERM;
1065 
1066     err = kstrtoul(buf, 10, &knob);
1067     if (err)
1068         return err;
1069     if (knob > 1)
1070         return -EINVAL;
1071 
1072     if (knob)
1073         set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1074     else
1075         clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1076 
1077     return len;
1078 }
1079 
1080 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1081 
1082 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1083                 struct kobj_attribute *a, char *buf)
1084 {
1085     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1086 
1087     return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1088 }
1089 
1090 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1091 
1092 static ssize_t btrfs_checksum_show(struct kobject *kobj,
1093                    struct kobj_attribute *a, char *buf)
1094 {
1095     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1096     u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1097 
1098     return sysfs_emit(buf, "%s (%s)\n",
1099               btrfs_super_csum_name(csum_type),
1100               crypto_shash_driver_name(fs_info->csum_shash));
1101 }
1102 
1103 BTRFS_ATTR(, checksum, btrfs_checksum_show);
1104 
1105 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1106         struct kobj_attribute *a, char *buf)
1107 {
1108     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1109     const char *str;
1110 
1111     switch (READ_ONCE(fs_info->exclusive_operation)) {
1112         case  BTRFS_EXCLOP_NONE:
1113             str = "none\n";
1114             break;
1115         case BTRFS_EXCLOP_BALANCE:
1116             str = "balance\n";
1117             break;
1118         case BTRFS_EXCLOP_BALANCE_PAUSED:
1119             str = "balance paused\n";
1120             break;
1121         case BTRFS_EXCLOP_DEV_ADD:
1122             str = "device add\n";
1123             break;
1124         case BTRFS_EXCLOP_DEV_REMOVE:
1125             str = "device remove\n";
1126             break;
1127         case BTRFS_EXCLOP_DEV_REPLACE:
1128             str = "device replace\n";
1129             break;
1130         case BTRFS_EXCLOP_RESIZE:
1131             str = "resize\n";
1132             break;
1133         case BTRFS_EXCLOP_SWAP_ACTIVATE:
1134             str = "swap activate\n";
1135             break;
1136         default:
1137             str = "UNKNOWN\n";
1138             break;
1139     }
1140     return sysfs_emit(buf, "%s", str);
1141 }
1142 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1143 
1144 static ssize_t btrfs_generation_show(struct kobject *kobj,
1145                      struct kobj_attribute *a, char *buf)
1146 {
1147     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1148 
1149     return sysfs_emit(buf, "%llu\n", fs_info->generation);
1150 }
1151 BTRFS_ATTR(, generation, btrfs_generation_show);
1152 
1153 /*
1154  * Look for an exact string @string in @buffer with possible leading or
1155  * trailing whitespace
1156  */
1157 static bool strmatch(const char *buffer, const char *string)
1158 {
1159     const size_t len = strlen(string);
1160 
1161     /* Skip leading whitespace */
1162     buffer = skip_spaces(buffer);
1163 
1164     /* Match entire string, check if the rest is whitespace or empty */
1165     if (strncmp(string, buffer, len) == 0 &&
1166         strlen(skip_spaces(buffer + len)) == 0)
1167         return true;
1168 
1169     return false;
1170 }
1171 
1172 static const char * const btrfs_read_policy_name[] = { "pid" };
1173 
1174 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1175                       struct kobj_attribute *a, char *buf)
1176 {
1177     struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1178     ssize_t ret = 0;
1179     int i;
1180 
1181     for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1182         if (fs_devices->read_policy == i)
1183             ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
1184                      (ret == 0 ? "" : " "),
1185                      btrfs_read_policy_name[i]);
1186         else
1187             ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
1188                      (ret == 0 ? "" : " "),
1189                      btrfs_read_policy_name[i]);
1190     }
1191 
1192     ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
1193 
1194     return ret;
1195 }
1196 
1197 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1198                        struct kobj_attribute *a,
1199                        const char *buf, size_t len)
1200 {
1201     struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1202     int i;
1203 
1204     for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1205         if (strmatch(buf, btrfs_read_policy_name[i])) {
1206             if (i != fs_devices->read_policy) {
1207                 fs_devices->read_policy = i;
1208                 btrfs_info(fs_devices->fs_info,
1209                        "read policy set to '%s'",
1210                        btrfs_read_policy_name[i]);
1211             }
1212             return len;
1213         }
1214     }
1215 
1216     return -EINVAL;
1217 }
1218 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1219 
1220 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1221                            struct kobj_attribute *a,
1222                            char *buf)
1223 {
1224     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1225     ssize_t ret;
1226 
1227     ret = sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1228 
1229     return ret;
1230 }
1231 
1232 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1233                         struct kobj_attribute *a,
1234                         const char *buf, size_t len)
1235 {
1236     struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1237     int thresh;
1238     int ret;
1239 
1240     ret = kstrtoint(buf, 10, &thresh);
1241     if (ret)
1242         return ret;
1243 
1244     if (thresh != 0 && (thresh <= 50 || thresh > 100))
1245         return -EINVAL;
1246 
1247     WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1248 
1249     return len;
1250 }
1251 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1252           btrfs_bg_reclaim_threshold_store);
1253 
1254 /*
1255  * Per-filesystem information and stats.
1256  *
1257  * Path: /sys/fs/btrfs/<uuid>/
1258  */
1259 static const struct attribute *btrfs_attrs[] = {
1260     BTRFS_ATTR_PTR(, label),
1261     BTRFS_ATTR_PTR(, nodesize),
1262     BTRFS_ATTR_PTR(, sectorsize),
1263     BTRFS_ATTR_PTR(, clone_alignment),
1264     BTRFS_ATTR_PTR(, quota_override),
1265     BTRFS_ATTR_PTR(, metadata_uuid),
1266     BTRFS_ATTR_PTR(, checksum),
1267     BTRFS_ATTR_PTR(, exclusive_operation),
1268     BTRFS_ATTR_PTR(, generation),
1269     BTRFS_ATTR_PTR(, read_policy),
1270     BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1271     BTRFS_ATTR_PTR(, commit_stats),
1272     NULL,
1273 };
1274 
1275 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1276 {
1277     struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1278 
1279     memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1280     complete(&fs_devs->kobj_unregister);
1281 }
1282 
1283 static struct kobj_type btrfs_ktype = {
1284     .sysfs_ops  = &kobj_sysfs_ops,
1285     .release    = btrfs_release_fsid_kobj,
1286 };
1287 
1288 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1289 {
1290     if (kobj->ktype != &btrfs_ktype)
1291         return NULL;
1292     return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1293 }
1294 
1295 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1296 {
1297     if (kobj->ktype != &btrfs_ktype)
1298         return NULL;
1299     return to_fs_devs(kobj)->fs_info;
1300 }
1301 
1302 static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1303 {
1304     while (kobj) {
1305         if (kobj->ktype == &btrfs_ktype)
1306             return kobj;
1307         kobj = kobj->parent;
1308     }
1309     return NULL;
1310 }
1311 
1312 #define NUM_FEATURE_BITS 64
1313 #define BTRFS_FEATURE_NAME_MAX 13
1314 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1315 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1316 
1317 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1318           ARRAY_SIZE(btrfs_feature_attrs));
1319 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1320           ARRAY_SIZE(btrfs_feature_attrs[0]));
1321 
1322 static const u64 supported_feature_masks[FEAT_MAX] = {
1323     [FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
1324     [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1325     [FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
1326 };
1327 
1328 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1329 {
1330     int set;
1331 
1332     for (set = 0; set < FEAT_MAX; set++) {
1333         int i;
1334         struct attribute *attrs[2];
1335         struct attribute_group agroup = {
1336             .name = "features",
1337             .attrs = attrs,
1338         };
1339         u64 features = get_features(fs_info, set);
1340         features &= ~supported_feature_masks[set];
1341 
1342         if (!features)
1343             continue;
1344 
1345         attrs[1] = NULL;
1346         for (i = 0; i < NUM_FEATURE_BITS; i++) {
1347             struct btrfs_feature_attr *fa;
1348 
1349             if (!(features & (1ULL << i)))
1350                 continue;
1351 
1352             fa = &btrfs_feature_attrs[set][i];
1353             attrs[0] = &fa->kobj_attr.attr;
1354             if (add) {
1355                 int ret;
1356                 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1357                             &agroup);
1358                 if (ret)
1359                     return ret;
1360             } else
1361                 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1362                             &agroup);
1363         }
1364 
1365     }
1366     return 0;
1367 }
1368 
1369 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1370 {
1371     if (fs_devs->devinfo_kobj) {
1372         kobject_del(fs_devs->devinfo_kobj);
1373         kobject_put(fs_devs->devinfo_kobj);
1374         fs_devs->devinfo_kobj = NULL;
1375     }
1376 
1377     if (fs_devs->devices_kobj) {
1378         kobject_del(fs_devs->devices_kobj);
1379         kobject_put(fs_devs->devices_kobj);
1380         fs_devs->devices_kobj = NULL;
1381     }
1382 
1383     if (fs_devs->fsid_kobj.state_initialized) {
1384         kobject_del(&fs_devs->fsid_kobj);
1385         kobject_put(&fs_devs->fsid_kobj);
1386         wait_for_completion(&fs_devs->kobj_unregister);
1387     }
1388 }
1389 
1390 /* when fs_devs is NULL it will remove all fsid kobject */
1391 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1392 {
1393     struct list_head *fs_uuids = btrfs_get_fs_uuids();
1394 
1395     if (fs_devs) {
1396         __btrfs_sysfs_remove_fsid(fs_devs);
1397         return;
1398     }
1399 
1400     list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1401         __btrfs_sysfs_remove_fsid(fs_devs);
1402     }
1403 }
1404 
1405 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1406 {
1407     struct btrfs_device *device;
1408     struct btrfs_fs_devices *seed;
1409 
1410     list_for_each_entry(device, &fs_devices->devices, dev_list)
1411         btrfs_sysfs_remove_device(device);
1412 
1413     list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1414         list_for_each_entry(device, &seed->devices, dev_list)
1415             btrfs_sysfs_remove_device(device);
1416     }
1417 }
1418 
1419 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1420 {
1421     struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1422 
1423     sysfs_remove_link(fsid_kobj, "bdi");
1424 
1425     if (fs_info->space_info_kobj) {
1426         sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1427         kobject_del(fs_info->space_info_kobj);
1428         kobject_put(fs_info->space_info_kobj);
1429     }
1430 #ifdef CONFIG_BTRFS_DEBUG
1431     if (fs_info->discard_debug_kobj) {
1432         sysfs_remove_files(fs_info->discard_debug_kobj,
1433                    discard_debug_attrs);
1434         kobject_del(fs_info->discard_debug_kobj);
1435         kobject_put(fs_info->discard_debug_kobj);
1436     }
1437     if (fs_info->debug_kobj) {
1438         sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1439         kobject_del(fs_info->debug_kobj);
1440         kobject_put(fs_info->debug_kobj);
1441     }
1442 #endif
1443     addrm_unknown_feature_attrs(fs_info, false);
1444     sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1445     sysfs_remove_files(fsid_kobj, btrfs_attrs);
1446     btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1447 }
1448 
1449 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1450     [FEAT_COMPAT]    = "compat",
1451     [FEAT_COMPAT_RO] = "compat_ro",
1452     [FEAT_INCOMPAT]  = "incompat",
1453 };
1454 
1455 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1456 {
1457     return btrfs_feature_set_names[set];
1458 }
1459 
1460 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1461 {
1462     size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1463     int len = 0;
1464     int i;
1465     char *str;
1466 
1467     str = kmalloc(bufsize, GFP_KERNEL);
1468     if (!str)
1469         return str;
1470 
1471     for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1472         const char *name;
1473 
1474         if (!(flags & (1ULL << i)))
1475             continue;
1476 
1477         name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1478         len += scnprintf(str + len, bufsize - len, "%s%s",
1479                 len ? "," : "", name);
1480     }
1481 
1482     return str;
1483 }
1484 
1485 static void init_feature_attrs(void)
1486 {
1487     struct btrfs_feature_attr *fa;
1488     int set, i;
1489 
1490     memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1491     memset(btrfs_unknown_feature_names, 0,
1492            sizeof(btrfs_unknown_feature_names));
1493 
1494     for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1495         struct btrfs_feature_attr *sfa;
1496         struct attribute *a = btrfs_supported_feature_attrs[i];
1497         int bit;
1498         sfa = attr_to_btrfs_feature_attr(a);
1499         bit = ilog2(sfa->feature_bit);
1500         fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1501 
1502         fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1503     }
1504 
1505     for (set = 0; set < FEAT_MAX; set++) {
1506         for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1507             char *name = btrfs_unknown_feature_names[set][i];
1508             fa = &btrfs_feature_attrs[set][i];
1509 
1510             if (fa->kobj_attr.attr.name)
1511                 continue;
1512 
1513             snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1514                  btrfs_feature_set_names[set], i);
1515 
1516             fa->kobj_attr.attr.name = name;
1517             fa->kobj_attr.attr.mode = S_IRUGO;
1518             fa->feature_set = set;
1519             fa->feature_bit = 1ULL << i;
1520         }
1521     }
1522 }
1523 
1524 /*
1525  * Create a sysfs entry for a given block group type at path
1526  * /sys/fs/btrfs/UUID/allocation/data/TYPE
1527  */
1528 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1529 {
1530     struct btrfs_fs_info *fs_info = cache->fs_info;
1531     struct btrfs_space_info *space_info = cache->space_info;
1532     struct raid_kobject *rkobj;
1533     const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1534     unsigned int nofs_flag;
1535     int ret;
1536 
1537     /*
1538      * Setup a NOFS context because kobject_add(), deep in its call chain,
1539      * does GFP_KERNEL allocations, and we are often called in a context
1540      * where if reclaim is triggered we can deadlock (we are either holding
1541      * a transaction handle or some lock required for a transaction
1542      * commit).
1543      */
1544     nofs_flag = memalloc_nofs_save();
1545 
1546     rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1547     if (!rkobj) {
1548         memalloc_nofs_restore(nofs_flag);
1549         btrfs_warn(cache->fs_info,
1550                 "couldn't alloc memory for raid level kobject");
1551         return;
1552     }
1553 
1554     rkobj->flags = cache->flags;
1555     kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1556 
1557     /*
1558      * We call this either on mount, or if we've created a block group for a
1559      * new index type while running (i.e. when restriping).  The running
1560      * case is tricky because we could race with other threads, so we need
1561      * to have this check to make sure we didn't already init the kobject.
1562      *
1563      * We don't have to protect on the free side because it only happens on
1564      * unmount.
1565      */
1566     spin_lock(&space_info->lock);
1567     if (space_info->block_group_kobjs[index]) {
1568         spin_unlock(&space_info->lock);
1569         kobject_put(&rkobj->kobj);
1570         return;
1571     } else {
1572         space_info->block_group_kobjs[index] = &rkobj->kobj;
1573     }
1574     spin_unlock(&space_info->lock);
1575 
1576     ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1577               btrfs_bg_type_to_raid_name(rkobj->flags));
1578     memalloc_nofs_restore(nofs_flag);
1579     if (ret) {
1580         spin_lock(&space_info->lock);
1581         space_info->block_group_kobjs[index] = NULL;
1582         spin_unlock(&space_info->lock);
1583         kobject_put(&rkobj->kobj);
1584         btrfs_warn(fs_info,
1585             "failed to add kobject for block cache, ignoring");
1586         return;
1587     }
1588 }
1589 
1590 /*
1591  * Remove sysfs directories for all block group types of a given space info and
1592  * the space info as well
1593  */
1594 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1595 {
1596     int i;
1597 
1598     for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1599         struct kobject *kobj;
1600 
1601         kobj = space_info->block_group_kobjs[i];
1602         space_info->block_group_kobjs[i] = NULL;
1603         if (kobj) {
1604             kobject_del(kobj);
1605             kobject_put(kobj);
1606         }
1607     }
1608     kobject_del(&space_info->kobj);
1609     kobject_put(&space_info->kobj);
1610 }
1611 
1612 static const char *alloc_name(u64 flags)
1613 {
1614     switch (flags) {
1615     case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1616         return "mixed";
1617     case BTRFS_BLOCK_GROUP_METADATA:
1618         return "metadata";
1619     case BTRFS_BLOCK_GROUP_DATA:
1620         return "data";
1621     case BTRFS_BLOCK_GROUP_SYSTEM:
1622         return "system";
1623     default:
1624         WARN_ON(1);
1625         return "invalid-combination";
1626     }
1627 }
1628 
1629 /*
1630  * Create a sysfs entry for a space info type at path
1631  * /sys/fs/btrfs/UUID/allocation/TYPE
1632  */
1633 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1634                     struct btrfs_space_info *space_info)
1635 {
1636     int ret;
1637 
1638     ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1639                    fs_info->space_info_kobj, "%s",
1640                    alloc_name(space_info->flags));
1641     if (ret) {
1642         kobject_put(&space_info->kobj);
1643         return ret;
1644     }
1645 
1646     return 0;
1647 }
1648 
1649 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1650 {
1651     struct kobject *devices_kobj;
1652 
1653     /*
1654      * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1655      * fs_info::fs_devices.
1656      */
1657     devices_kobj = device->fs_info->fs_devices->devices_kobj;
1658     ASSERT(devices_kobj);
1659 
1660     if (device->bdev)
1661         sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1662 
1663     if (device->devid_kobj.state_initialized) {
1664         kobject_del(&device->devid_kobj);
1665         kobject_put(&device->devid_kobj);
1666         wait_for_completion(&device->kobj_unregister);
1667     }
1668 }
1669 
1670 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1671                              struct kobj_attribute *a,
1672                              char *buf)
1673 {
1674     int val;
1675     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1676                            devid_kobj);
1677 
1678     val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1679 
1680     return sysfs_emit(buf, "%d\n", val);
1681 }
1682 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1683 
1684 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1685                     struct kobj_attribute *a, char *buf)
1686 {
1687     int val;
1688     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1689                            devid_kobj);
1690 
1691     val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1692 
1693     return sysfs_emit(buf, "%d\n", val);
1694 }
1695 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1696 
1697 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1698                              struct kobj_attribute *a,
1699                              char *buf)
1700 {
1701     int val;
1702     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1703                            devid_kobj);
1704 
1705     val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1706 
1707     return sysfs_emit(buf, "%d\n", val);
1708 }
1709 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1710 
1711 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1712                          struct kobj_attribute *a,
1713                          char *buf)
1714 {
1715     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1716                            devid_kobj);
1717 
1718     return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1719 }
1720 
1721 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1722                           struct kobj_attribute *a,
1723                           const char *buf, size_t len)
1724 {
1725     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1726                            devid_kobj);
1727     char *endptr;
1728     unsigned long long limit;
1729 
1730     limit = memparse(buf, &endptr);
1731     WRITE_ONCE(device->scrub_speed_max, limit);
1732     return len;
1733 }
1734 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1735           btrfs_devinfo_scrub_speed_max_store);
1736 
1737 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1738                         struct kobj_attribute *a, char *buf)
1739 {
1740     int val;
1741     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1742                            devid_kobj);
1743 
1744     val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1745 
1746     return sysfs_emit(buf, "%d\n", val);
1747 }
1748 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1749 
1750 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1751                        struct kobj_attribute *a, char *buf)
1752 {
1753     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1754                            devid_kobj);
1755 
1756     return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1757 }
1758 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1759 
1760 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1761         struct kobj_attribute *a, char *buf)
1762 {
1763     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1764                            devid_kobj);
1765 
1766     if (!device->dev_stats_valid)
1767         return sysfs_emit(buf, "invalid\n");
1768 
1769     /*
1770      * Print all at once so we get a snapshot of all values from the same
1771      * time. Keep them in sync and in order of definition of
1772      * btrfs_dev_stat_values.
1773      */
1774     return sysfs_emit(buf,
1775         "write_errs %d\n"
1776         "read_errs %d\n"
1777         "flush_errs %d\n"
1778         "corruption_errs %d\n"
1779         "generation_errs %d\n",
1780         btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1781         btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1782         btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1783         btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1784         btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1785 }
1786 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1787 
1788 /*
1789  * Information about one device.
1790  *
1791  * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1792  */
1793 static struct attribute *devid_attrs[] = {
1794     BTRFS_ATTR_PTR(devid, error_stats),
1795     BTRFS_ATTR_PTR(devid, fsid),
1796     BTRFS_ATTR_PTR(devid, in_fs_metadata),
1797     BTRFS_ATTR_PTR(devid, missing),
1798     BTRFS_ATTR_PTR(devid, replace_target),
1799     BTRFS_ATTR_PTR(devid, scrub_speed_max),
1800     BTRFS_ATTR_PTR(devid, writeable),
1801     NULL
1802 };
1803 ATTRIBUTE_GROUPS(devid);
1804 
1805 static void btrfs_release_devid_kobj(struct kobject *kobj)
1806 {
1807     struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1808                            devid_kobj);
1809 
1810     memset(&device->devid_kobj, 0, sizeof(struct kobject));
1811     complete(&device->kobj_unregister);
1812 }
1813 
1814 static struct kobj_type devid_ktype = {
1815     .sysfs_ops  = &kobj_sysfs_ops,
1816     .default_groups = devid_groups,
1817     .release    = btrfs_release_devid_kobj,
1818 };
1819 
1820 int btrfs_sysfs_add_device(struct btrfs_device *device)
1821 {
1822     int ret;
1823     unsigned int nofs_flag;
1824     struct kobject *devices_kobj;
1825     struct kobject *devinfo_kobj;
1826 
1827     /*
1828      * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1829      * for the seed fs_devices
1830      */
1831     devices_kobj = device->fs_info->fs_devices->devices_kobj;
1832     devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1833     ASSERT(devices_kobj);
1834     ASSERT(devinfo_kobj);
1835 
1836     nofs_flag = memalloc_nofs_save();
1837 
1838     if (device->bdev) {
1839         struct kobject *disk_kobj = bdev_kobj(device->bdev);
1840 
1841         ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1842         if (ret) {
1843             btrfs_warn(device->fs_info,
1844                 "creating sysfs device link for devid %llu failed: %d",
1845                 device->devid, ret);
1846             goto out;
1847         }
1848     }
1849 
1850     init_completion(&device->kobj_unregister);
1851     ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1852                    devinfo_kobj, "%llu", device->devid);
1853     if (ret) {
1854         kobject_put(&device->devid_kobj);
1855         btrfs_warn(device->fs_info,
1856                "devinfo init for devid %llu failed: %d",
1857                device->devid, ret);
1858     }
1859 
1860 out:
1861     memalloc_nofs_restore(nofs_flag);
1862     return ret;
1863 }
1864 
1865 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1866 {
1867     int ret;
1868     struct btrfs_device *device;
1869     struct btrfs_fs_devices *seed;
1870 
1871     list_for_each_entry(device, &fs_devices->devices, dev_list) {
1872         ret = btrfs_sysfs_add_device(device);
1873         if (ret)
1874             goto fail;
1875     }
1876 
1877     list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1878         list_for_each_entry(device, &seed->devices, dev_list) {
1879             ret = btrfs_sysfs_add_device(device);
1880             if (ret)
1881                 goto fail;
1882         }
1883     }
1884 
1885     return 0;
1886 
1887 fail:
1888     btrfs_sysfs_remove_fs_devices(fs_devices);
1889     return ret;
1890 }
1891 
1892 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1893 {
1894     int ret;
1895 
1896     ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1897     if (ret)
1898         pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1899             action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1900             &disk_to_dev(bdev->bd_disk)->kobj);
1901 }
1902 
1903 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1904 
1905 {
1906     char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1907 
1908     /*
1909      * Sprouting changes fsid of the mounted filesystem, rename the fsid
1910      * directory
1911      */
1912     snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1913     if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1914         btrfs_warn(fs_devices->fs_info,
1915                 "sysfs: failed to create fsid for sprout");
1916 }
1917 
1918 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1919 {
1920     char tmp[24];
1921 
1922     snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1923 
1924     if (kobject_rename(&device->devid_kobj, tmp))
1925         btrfs_warn(device->fs_devices->fs_info,
1926                "sysfs: failed to update devid for %llu",
1927                device->devid);
1928 }
1929 
1930 /* /sys/fs/btrfs/ entry */
1931 static struct kset *btrfs_kset;
1932 
1933 /*
1934  * Creates:
1935  *      /sys/fs/btrfs/UUID
1936  *
1937  * Can be called by the device discovery thread.
1938  */
1939 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1940 {
1941     int error;
1942 
1943     init_completion(&fs_devs->kobj_unregister);
1944     fs_devs->fsid_kobj.kset = btrfs_kset;
1945     error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1946                      "%pU", fs_devs->fsid);
1947     if (error) {
1948         kobject_put(&fs_devs->fsid_kobj);
1949         return error;
1950     }
1951 
1952     fs_devs->devices_kobj = kobject_create_and_add("devices",
1953                                &fs_devs->fsid_kobj);
1954     if (!fs_devs->devices_kobj) {
1955         btrfs_err(fs_devs->fs_info,
1956               "failed to init sysfs device interface");
1957         btrfs_sysfs_remove_fsid(fs_devs);
1958         return -ENOMEM;
1959     }
1960 
1961     fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1962                                &fs_devs->fsid_kobj);
1963     if (!fs_devs->devinfo_kobj) {
1964         btrfs_err(fs_devs->fs_info,
1965               "failed to init sysfs devinfo kobject");
1966         btrfs_sysfs_remove_fsid(fs_devs);
1967         return -ENOMEM;
1968     }
1969 
1970     return 0;
1971 }
1972 
1973 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1974 {
1975     int error;
1976     struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1977     struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1978 
1979     error = btrfs_sysfs_add_fs_devices(fs_devs);
1980     if (error)
1981         return error;
1982 
1983     error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1984     if (error) {
1985         btrfs_sysfs_remove_fs_devices(fs_devs);
1986         return error;
1987     }
1988 
1989     error = sysfs_create_group(fsid_kobj,
1990                    &btrfs_feature_attr_group);
1991     if (error)
1992         goto failure;
1993 
1994 #ifdef CONFIG_BTRFS_DEBUG
1995     fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1996     if (!fs_info->debug_kobj) {
1997         error = -ENOMEM;
1998         goto failure;
1999     }
2000 
2001     error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
2002     if (error)
2003         goto failure;
2004 
2005     /* Discard directory */
2006     fs_info->discard_debug_kobj = kobject_create_and_add("discard",
2007                              fs_info->debug_kobj);
2008     if (!fs_info->discard_debug_kobj) {
2009         error = -ENOMEM;
2010         goto failure;
2011     }
2012 
2013     error = sysfs_create_files(fs_info->discard_debug_kobj,
2014                    discard_debug_attrs);
2015     if (error)
2016         goto failure;
2017 #endif
2018 
2019     error = addrm_unknown_feature_attrs(fs_info, true);
2020     if (error)
2021         goto failure;
2022 
2023     error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2024     if (error)
2025         goto failure;
2026 
2027     fs_info->space_info_kobj = kobject_create_and_add("allocation",
2028                           fsid_kobj);
2029     if (!fs_info->space_info_kobj) {
2030         error = -ENOMEM;
2031         goto failure;
2032     }
2033 
2034     error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2035     if (error)
2036         goto failure;
2037 
2038     return 0;
2039 failure:
2040     btrfs_sysfs_remove_mounted(fs_info);
2041     return error;
2042 }
2043 
2044 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2045 {
2046     return to_fs_info(kobj->parent->parent);
2047 }
2048 
2049 #define QGROUP_ATTR(_member, _show_name)                    \
2050 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj,     \
2051                        struct kobj_attribute *a,        \
2052                        char *buf)               \
2053 {                                       \
2054     struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);    \
2055     struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,         \
2056             struct btrfs_qgroup, kobj);             \
2057     return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf);    \
2058 }                                       \
2059 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2060 
2061 #define QGROUP_RSV_ATTR(_name, _type)                       \
2062 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj,   \
2063                          struct kobj_attribute *a,      \
2064                          char *buf)             \
2065 {                                       \
2066     struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);    \
2067     struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,         \
2068             struct btrfs_qgroup, kobj);             \
2069     return btrfs_show_u64(&qgroup->rsv.values[_type],           \
2070             &fs_info->qgroup_lock, buf);                \
2071 }                                       \
2072 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2073 
2074 QGROUP_ATTR(rfer, referenced);
2075 QGROUP_ATTR(excl, exclusive);
2076 QGROUP_ATTR(max_rfer, max_referenced);
2077 QGROUP_ATTR(max_excl, max_exclusive);
2078 QGROUP_ATTR(lim_flags, limit_flags);
2079 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2080 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2081 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2082 
2083 /*
2084  * Qgroup information.
2085  *
2086  * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2087  */
2088 static struct attribute *qgroup_attrs[] = {
2089     BTRFS_ATTR_PTR(qgroup, referenced),
2090     BTRFS_ATTR_PTR(qgroup, exclusive),
2091     BTRFS_ATTR_PTR(qgroup, max_referenced),
2092     BTRFS_ATTR_PTR(qgroup, max_exclusive),
2093     BTRFS_ATTR_PTR(qgroup, limit_flags),
2094     BTRFS_ATTR_PTR(qgroup, rsv_data),
2095     BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2096     BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2097     NULL
2098 };
2099 ATTRIBUTE_GROUPS(qgroup);
2100 
2101 static void qgroup_release(struct kobject *kobj)
2102 {
2103     struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2104 
2105     memset(&qgroup->kobj, 0, sizeof(*kobj));
2106 }
2107 
2108 static struct kobj_type qgroup_ktype = {
2109     .sysfs_ops = &kobj_sysfs_ops,
2110     .release = qgroup_release,
2111     .default_groups = qgroup_groups,
2112 };
2113 
2114 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2115                 struct btrfs_qgroup *qgroup)
2116 {
2117     struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2118     int ret;
2119 
2120     if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2121         return 0;
2122     if (qgroup->kobj.state_initialized)
2123         return 0;
2124     if (!qgroups_kobj)
2125         return -EINVAL;
2126 
2127     ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2128             "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2129             btrfs_qgroup_subvolid(qgroup->qgroupid));
2130     if (ret < 0)
2131         kobject_put(&qgroup->kobj);
2132 
2133     return ret;
2134 }
2135 
2136 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2137 {
2138     struct btrfs_qgroup *qgroup;
2139     struct btrfs_qgroup *next;
2140 
2141     if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2142         return;
2143 
2144     rbtree_postorder_for_each_entry_safe(qgroup, next,
2145                          &fs_info->qgroup_tree, node)
2146         btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2147     if (fs_info->qgroups_kobj) {
2148         kobject_del(fs_info->qgroups_kobj);
2149         kobject_put(fs_info->qgroups_kobj);
2150         fs_info->qgroups_kobj = NULL;
2151     }
2152 }
2153 
2154 /* Called when qgroups get initialized, thus there is no need for locking */
2155 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2156 {
2157     struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2158     struct btrfs_qgroup *qgroup;
2159     struct btrfs_qgroup *next;
2160     int ret = 0;
2161 
2162     if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2163         return 0;
2164 
2165     ASSERT(fsid_kobj);
2166     if (fs_info->qgroups_kobj)
2167         return 0;
2168 
2169     fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
2170     if (!fs_info->qgroups_kobj) {
2171         ret = -ENOMEM;
2172         goto out;
2173     }
2174     rbtree_postorder_for_each_entry_safe(qgroup, next,
2175                          &fs_info->qgroup_tree, node) {
2176         ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2177         if (ret < 0)
2178             goto out;
2179     }
2180 
2181 out:
2182     if (ret < 0)
2183         btrfs_sysfs_del_qgroups(fs_info);
2184     return ret;
2185 }
2186 
2187 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2188                 struct btrfs_qgroup *qgroup)
2189 {
2190     if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2191         return;
2192 
2193     if (qgroup->kobj.state_initialized) {
2194         kobject_del(&qgroup->kobj);
2195         kobject_put(&qgroup->kobj);
2196     }
2197 }
2198 
2199 /*
2200  * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2201  * values in superblock. Call after any changes to incompat/compat_ro flags
2202  */
2203 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
2204         u64 bit, enum btrfs_feature_set set)
2205 {
2206     struct btrfs_fs_devices *fs_devs;
2207     struct kobject *fsid_kobj;
2208     u64 __maybe_unused features;
2209     int __maybe_unused ret;
2210 
2211     if (!fs_info)
2212         return;
2213 
2214     /*
2215      * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2216      * safe when called from some contexts (eg. balance)
2217      */
2218     features = get_features(fs_info, set);
2219     ASSERT(bit & supported_feature_masks[set]);
2220 
2221     fs_devs = fs_info->fs_devices;
2222     fsid_kobj = &fs_devs->fsid_kobj;
2223 
2224     if (!fsid_kobj->state_initialized)
2225         return;
2226 
2227     /*
2228      * FIXME: this is too heavy to update just one value, ideally we'd like
2229      * to use sysfs_update_group but some refactoring is needed first.
2230      */
2231     sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2232     ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2233 }
2234 
2235 int __init btrfs_init_sysfs(void)
2236 {
2237     int ret;
2238 
2239     btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2240     if (!btrfs_kset)
2241         return -ENOMEM;
2242 
2243     init_feature_attrs();
2244     ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2245     if (ret)
2246         goto out2;
2247     ret = sysfs_merge_group(&btrfs_kset->kobj,
2248                 &btrfs_static_feature_attr_group);
2249     if (ret)
2250         goto out_remove_group;
2251 
2252 #ifdef CONFIG_BTRFS_DEBUG
2253     ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2254     if (ret)
2255         goto out2;
2256 #endif
2257 
2258     return 0;
2259 
2260 out_remove_group:
2261     sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2262 out2:
2263     kset_unregister(btrfs_kset);
2264 
2265     return ret;
2266 }
2267 
2268 void __cold btrfs_exit_sysfs(void)
2269 {
2270     sysfs_unmerge_group(&btrfs_kset->kobj,
2271                 &btrfs_static_feature_attr_group);
2272     sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2273 #ifdef CONFIG_BTRFS_DEBUG
2274     sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2275 #endif
2276     kset_unregister(btrfs_kset);
2277 }