Linux Audio

Check our new training course

Linux BSP development engineering services

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