Linux Audio

Check our new training course

Loading...
v6.8
   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	ssize_t ret = 0;
1232	int i;
1233
1234	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1235		if (fs_devices->read_policy == i)
1236			ret += sysfs_emit_at(buf, ret, "%s[%s]",
1237					 (ret == 0 ? "" : " "),
1238					 btrfs_read_policy_name[i]);
1239		else
1240			ret += sysfs_emit_at(buf, ret, "%s%s",
1241					 (ret == 0 ? "" : " "),
1242					 btrfs_read_policy_name[i]);
1243	}
1244
1245	ret += sysfs_emit_at(buf, ret, "\n");
1246
1247	return ret;
1248}
1249
1250static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1251				       struct kobj_attribute *a,
1252				       const char *buf, size_t len)
1253{
1254	struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1255	int i;
1256
1257	for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1258		if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
1259			if (i != fs_devices->read_policy) {
1260				fs_devices->read_policy = i;
1261				btrfs_info(fs_devices->fs_info,
1262					   "read policy set to '%s'",
1263					   btrfs_read_policy_name[i]);
1264			}
1265			return len;
1266		}
1267	}
1268
1269	return -EINVAL;
1270}
1271BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1272
1273static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1274					       struct kobj_attribute *a,
1275					       char *buf)
1276{
1277	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1278
1279	return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1280}
1281
1282static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1283						struct kobj_attribute *a,
1284						const char *buf, size_t len)
1285{
1286	struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1287	int thresh;
1288	int ret;
1289
1290	ret = kstrtoint(buf, 10, &thresh);
1291	if (ret)
1292		return ret;
1293
1294#ifdef CONFIG_BTRFS_DEBUG
1295	if (thresh != 0 && (thresh > 100))
1296		return -EINVAL;
1297#else
1298	if (thresh != 0 && (thresh <= 50 || thresh > 100))
1299		return -EINVAL;
1300#endif
1301
1302	WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1303
1304	return len;
1305}
1306BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1307	      btrfs_bg_reclaim_threshold_store);
1308
1309/*
1310 * Per-filesystem information and stats.
1311 *
1312 * Path: /sys/fs/btrfs/<uuid>/
1313 */
1314static const struct attribute *btrfs_attrs[] = {
1315	BTRFS_ATTR_PTR(, label),
1316	BTRFS_ATTR_PTR(, nodesize),
1317	BTRFS_ATTR_PTR(, sectorsize),
1318	BTRFS_ATTR_PTR(, clone_alignment),
1319	BTRFS_ATTR_PTR(, quota_override),
1320	BTRFS_ATTR_PTR(, metadata_uuid),
1321	BTRFS_ATTR_PTR(, checksum),
1322	BTRFS_ATTR_PTR(, exclusive_operation),
1323	BTRFS_ATTR_PTR(, generation),
1324	BTRFS_ATTR_PTR(, read_policy),
1325	BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1326	BTRFS_ATTR_PTR(, commit_stats),
1327	BTRFS_ATTR_PTR(, temp_fsid),
1328	NULL,
1329};
1330
1331static void btrfs_release_fsid_kobj(struct kobject *kobj)
1332{
1333	struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1334
1335	memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1336	complete(&fs_devs->kobj_unregister);
1337}
1338
1339static const struct kobj_type btrfs_ktype = {
1340	.sysfs_ops	= &kobj_sysfs_ops,
1341	.release	= btrfs_release_fsid_kobj,
1342};
1343
1344static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1345{
1346	if (kobj->ktype != &btrfs_ktype)
1347		return NULL;
1348	return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1349}
1350
1351static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1352{
1353	if (kobj->ktype != &btrfs_ktype)
1354		return NULL;
1355	return to_fs_devs(kobj)->fs_info;
1356}
1357
1358static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1359{
1360	while (kobj) {
1361		if (kobj->ktype == &btrfs_ktype)
1362			return kobj;
1363		kobj = kobj->parent;
1364	}
1365	return NULL;
1366}
1367
1368#define NUM_FEATURE_BITS 64
1369#define BTRFS_FEATURE_NAME_MAX 13
1370static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1371static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1372
1373static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1374	      ARRAY_SIZE(btrfs_feature_attrs));
1375static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1376	      ARRAY_SIZE(btrfs_feature_attrs[0]));
1377
1378static const u64 supported_feature_masks[FEAT_MAX] = {
1379	[FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
1380	[FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1381	[FEAT_INCOMPAT]  = BTRFS_FEATURE_INCOMPAT_SUPP,
1382};
1383
1384static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1385{
1386	int set;
1387
1388	for (set = 0; set < FEAT_MAX; set++) {
1389		int i;
1390		struct attribute *attrs[2];
1391		struct attribute_group agroup = {
1392			.name = "features",
1393			.attrs = attrs,
1394		};
1395		u64 features = get_features(fs_info, set);
1396		features &= ~supported_feature_masks[set];
1397
1398		if (!features)
1399			continue;
1400
1401		attrs[1] = NULL;
1402		for (i = 0; i < NUM_FEATURE_BITS; i++) {
1403			struct btrfs_feature_attr *fa;
1404
1405			if (!(features & (1ULL << i)))
1406				continue;
1407
1408			fa = &btrfs_feature_attrs[set][i];
1409			attrs[0] = &fa->kobj_attr.attr;
1410			if (add) {
1411				int ret;
1412				ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1413							&agroup);
1414				if (ret)
1415					return ret;
1416			} else
1417				sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1418						    &agroup);
1419		}
1420
1421	}
1422	return 0;
1423}
1424
1425static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1426{
1427	if (fs_devs->devinfo_kobj) {
1428		kobject_del(fs_devs->devinfo_kobj);
1429		kobject_put(fs_devs->devinfo_kobj);
1430		fs_devs->devinfo_kobj = NULL;
1431	}
1432
1433	if (fs_devs->devices_kobj) {
1434		kobject_del(fs_devs->devices_kobj);
1435		kobject_put(fs_devs->devices_kobj);
1436		fs_devs->devices_kobj = NULL;
1437	}
1438
1439	if (fs_devs->fsid_kobj.state_initialized) {
1440		kobject_del(&fs_devs->fsid_kobj);
1441		kobject_put(&fs_devs->fsid_kobj);
1442		wait_for_completion(&fs_devs->kobj_unregister);
1443	}
1444}
1445
1446/* when fs_devs is NULL it will remove all fsid kobject */
1447void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1448{
1449	struct list_head *fs_uuids = btrfs_get_fs_uuids();
1450
1451	if (fs_devs) {
1452		__btrfs_sysfs_remove_fsid(fs_devs);
1453		return;
1454	}
1455
1456	list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1457		__btrfs_sysfs_remove_fsid(fs_devs);
1458	}
1459}
1460
1461static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1462{
1463	struct btrfs_device *device;
1464	struct btrfs_fs_devices *seed;
1465
1466	list_for_each_entry(device, &fs_devices->devices, dev_list)
1467		btrfs_sysfs_remove_device(device);
1468
1469	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1470		list_for_each_entry(device, &seed->devices, dev_list)
1471			btrfs_sysfs_remove_device(device);
1472	}
1473}
1474
1475void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1476{
1477	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1478
1479	sysfs_remove_link(fsid_kobj, "bdi");
1480
1481	if (fs_info->space_info_kobj) {
1482		sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1483		kobject_del(fs_info->space_info_kobj);
1484		kobject_put(fs_info->space_info_kobj);
1485	}
1486	if (fs_info->discard_kobj) {
1487		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1488		kobject_del(fs_info->discard_kobj);
1489		kobject_put(fs_info->discard_kobj);
1490	}
1491#ifdef CONFIG_BTRFS_DEBUG
1492	if (fs_info->debug_kobj) {
1493		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1494		kobject_del(fs_info->debug_kobj);
1495		kobject_put(fs_info->debug_kobj);
1496	}
1497#endif
1498	addrm_unknown_feature_attrs(fs_info, false);
1499	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1500	sysfs_remove_files(fsid_kobj, btrfs_attrs);
1501	btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1502}
1503
1504static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1505	[FEAT_COMPAT]	 = "compat",
1506	[FEAT_COMPAT_RO] = "compat_ro",
1507	[FEAT_INCOMPAT]	 = "incompat",
1508};
1509
1510const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1511{
1512	return btrfs_feature_set_names[set];
1513}
1514
1515char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1516{
1517	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1518	int len = 0;
1519	int i;
1520	char *str;
1521
1522	str = kmalloc(bufsize, GFP_KERNEL);
1523	if (!str)
1524		return str;
1525
1526	for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1527		const char *name;
1528
1529		if (!(flags & (1ULL << i)))
1530			continue;
1531
1532		name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1533		len += scnprintf(str + len, bufsize - len, "%s%s",
1534				len ? "," : "", name);
1535	}
1536
1537	return str;
1538}
1539
1540static void init_feature_attrs(void)
1541{
1542	struct btrfs_feature_attr *fa;
1543	int set, i;
1544
1545	memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1546	memset(btrfs_unknown_feature_names, 0,
1547	       sizeof(btrfs_unknown_feature_names));
1548
1549	for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1550		struct btrfs_feature_attr *sfa;
1551		struct attribute *a = btrfs_supported_feature_attrs[i];
1552		int bit;
1553		sfa = attr_to_btrfs_feature_attr(a);
1554		bit = ilog2(sfa->feature_bit);
1555		fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1556
1557		fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1558	}
1559
1560	for (set = 0; set < FEAT_MAX; set++) {
1561		for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1562			char *name = btrfs_unknown_feature_names[set][i];
1563			fa = &btrfs_feature_attrs[set][i];
1564
1565			if (fa->kobj_attr.attr.name)
1566				continue;
1567
1568			snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1569				 btrfs_feature_set_names[set], i);
1570
1571			fa->kobj_attr.attr.name = name;
1572			fa->kobj_attr.attr.mode = S_IRUGO;
1573			fa->feature_set = set;
1574			fa->feature_bit = 1ULL << i;
1575		}
1576	}
1577}
1578
1579/*
1580 * Create a sysfs entry for a given block group type at path
1581 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1582 */
1583void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1584{
1585	struct btrfs_fs_info *fs_info = cache->fs_info;
1586	struct btrfs_space_info *space_info = cache->space_info;
1587	struct raid_kobject *rkobj;
1588	const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1589	unsigned int nofs_flag;
1590	int ret;
1591
1592	/*
1593	 * Setup a NOFS context because kobject_add(), deep in its call chain,
1594	 * does GFP_KERNEL allocations, and we are often called in a context
1595	 * where if reclaim is triggered we can deadlock (we are either holding
1596	 * a transaction handle or some lock required for a transaction
1597	 * commit).
1598	 */
1599	nofs_flag = memalloc_nofs_save();
1600
1601	rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1602	if (!rkobj) {
1603		memalloc_nofs_restore(nofs_flag);
1604		btrfs_warn(cache->fs_info,
1605				"couldn't alloc memory for raid level kobject");
1606		return;
1607	}
1608
1609	rkobj->flags = cache->flags;
1610	kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1611
1612	/*
1613	 * We call this either on mount, or if we've created a block group for a
1614	 * new index type while running (i.e. when restriping).  The running
1615	 * case is tricky because we could race with other threads, so we need
1616	 * to have this check to make sure we didn't already init the kobject.
1617	 *
1618	 * We don't have to protect on the free side because it only happens on
1619	 * unmount.
1620	 */
1621	spin_lock(&space_info->lock);
1622	if (space_info->block_group_kobjs[index]) {
1623		spin_unlock(&space_info->lock);
1624		kobject_put(&rkobj->kobj);
1625		return;
1626	} else {
1627		space_info->block_group_kobjs[index] = &rkobj->kobj;
1628	}
1629	spin_unlock(&space_info->lock);
1630
1631	ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1632			  btrfs_bg_type_to_raid_name(rkobj->flags));
1633	memalloc_nofs_restore(nofs_flag);
1634	if (ret) {
1635		spin_lock(&space_info->lock);
1636		space_info->block_group_kobjs[index] = NULL;
1637		spin_unlock(&space_info->lock);
1638		kobject_put(&rkobj->kobj);
1639		btrfs_warn(fs_info,
1640			"failed to add kobject for block cache, ignoring");
1641		return;
1642	}
1643}
1644
1645/*
1646 * Remove sysfs directories for all block group types of a given space info and
1647 * the space info as well
1648 */
1649void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1650{
1651	int i;
1652
1653	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1654		struct kobject *kobj;
1655
1656		kobj = space_info->block_group_kobjs[i];
1657		space_info->block_group_kobjs[i] = NULL;
1658		if (kobj) {
1659			kobject_del(kobj);
1660			kobject_put(kobj);
1661		}
1662	}
1663	kobject_del(&space_info->kobj);
1664	kobject_put(&space_info->kobj);
1665}
1666
1667static const char *alloc_name(u64 flags)
1668{
1669	switch (flags) {
1670	case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1671		return "mixed";
1672	case BTRFS_BLOCK_GROUP_METADATA:
1673		return "metadata";
1674	case BTRFS_BLOCK_GROUP_DATA:
1675		return "data";
1676	case BTRFS_BLOCK_GROUP_SYSTEM:
1677		return "system";
1678	default:
1679		WARN_ON(1);
1680		return "invalid-combination";
1681	}
1682}
1683
1684/*
1685 * Create a sysfs entry for a space info type at path
1686 * /sys/fs/btrfs/UUID/allocation/TYPE
1687 */
1688int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1689				    struct btrfs_space_info *space_info)
1690{
1691	int ret;
1692
1693	ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1694				   fs_info->space_info_kobj, "%s",
1695				   alloc_name(space_info->flags));
1696	if (ret) {
1697		kobject_put(&space_info->kobj);
1698		return ret;
1699	}
1700
1701	return 0;
1702}
1703
1704void btrfs_sysfs_remove_device(struct btrfs_device *device)
1705{
1706	struct kobject *devices_kobj;
1707
1708	/*
1709	 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1710	 * fs_info::fs_devices.
1711	 */
1712	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1713	ASSERT(devices_kobj);
1714
1715	if (device->bdev)
1716		sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1717
1718	if (device->devid_kobj.state_initialized) {
1719		kobject_del(&device->devid_kobj);
1720		kobject_put(&device->devid_kobj);
1721		wait_for_completion(&device->kobj_unregister);
1722	}
1723}
1724
1725static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1726					         struct kobj_attribute *a,
1727					         char *buf)
1728{
1729	int val;
1730	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1731						   devid_kobj);
1732
1733	val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1734
1735	return sysfs_emit(buf, "%d\n", val);
1736}
1737BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1738
1739static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1740					struct kobj_attribute *a, char *buf)
1741{
1742	int val;
1743	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1744						   devid_kobj);
1745
1746	val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1747
1748	return sysfs_emit(buf, "%d\n", val);
1749}
1750BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1751
1752static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1753					         struct kobj_attribute *a,
1754					         char *buf)
1755{
1756	int val;
1757	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1758						   devid_kobj);
1759
1760	val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1761
1762	return sysfs_emit(buf, "%d\n", val);
1763}
1764BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1765
1766static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1767					     struct kobj_attribute *a,
1768					     char *buf)
1769{
1770	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1771						   devid_kobj);
1772
1773	return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1774}
1775
1776static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1777					      struct kobj_attribute *a,
1778					      const char *buf, size_t len)
1779{
1780	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1781						   devid_kobj);
1782	char *endptr;
1783	unsigned long long limit;
1784
1785	limit = memparse(buf, &endptr);
1786	/* There could be trailing '\n', also catch any typos after the value. */
1787	endptr = skip_spaces(endptr);
1788	if (*endptr != 0)
1789		return -EINVAL;
1790	WRITE_ONCE(device->scrub_speed_max, limit);
1791	return len;
1792}
1793BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1794	      btrfs_devinfo_scrub_speed_max_store);
1795
1796static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1797					    struct kobj_attribute *a, char *buf)
1798{
1799	int val;
1800	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1801						   devid_kobj);
1802
1803	val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1804
1805	return sysfs_emit(buf, "%d\n", val);
1806}
1807BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1808
1809static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1810				       struct kobj_attribute *a, char *buf)
1811{
1812	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1813						   devid_kobj);
1814
1815	return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1816}
1817BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1818
1819static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1820		struct kobj_attribute *a, char *buf)
1821{
1822	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1823						   devid_kobj);
1824
1825	if (!device->dev_stats_valid)
1826		return sysfs_emit(buf, "invalid\n");
1827
1828	/*
1829	 * Print all at once so we get a snapshot of all values from the same
1830	 * time. Keep them in sync and in order of definition of
1831	 * btrfs_dev_stat_values.
1832	 */
1833	return sysfs_emit(buf,
1834		"write_errs %d\n"
1835		"read_errs %d\n"
1836		"flush_errs %d\n"
1837		"corruption_errs %d\n"
1838		"generation_errs %d\n",
1839		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1840		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1841		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1842		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1843		btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1844}
1845BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1846
1847/*
1848 * Information about one device.
1849 *
1850 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1851 */
1852static struct attribute *devid_attrs[] = {
1853	BTRFS_ATTR_PTR(devid, error_stats),
1854	BTRFS_ATTR_PTR(devid, fsid),
1855	BTRFS_ATTR_PTR(devid, in_fs_metadata),
1856	BTRFS_ATTR_PTR(devid, missing),
1857	BTRFS_ATTR_PTR(devid, replace_target),
1858	BTRFS_ATTR_PTR(devid, scrub_speed_max),
1859	BTRFS_ATTR_PTR(devid, writeable),
1860	NULL
1861};
1862ATTRIBUTE_GROUPS(devid);
1863
1864static void btrfs_release_devid_kobj(struct kobject *kobj)
1865{
1866	struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1867						   devid_kobj);
1868
1869	memset(&device->devid_kobj, 0, sizeof(struct kobject));
1870	complete(&device->kobj_unregister);
1871}
1872
1873static const struct kobj_type devid_ktype = {
1874	.sysfs_ops	= &kobj_sysfs_ops,
1875	.default_groups = devid_groups,
1876	.release	= btrfs_release_devid_kobj,
1877};
1878
1879int btrfs_sysfs_add_device(struct btrfs_device *device)
1880{
1881	int ret;
1882	unsigned int nofs_flag;
1883	struct kobject *devices_kobj;
1884	struct kobject *devinfo_kobj;
1885
1886	/*
1887	 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1888	 * for the seed fs_devices
1889	 */
1890	devices_kobj = device->fs_info->fs_devices->devices_kobj;
1891	devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1892	ASSERT(devices_kobj);
1893	ASSERT(devinfo_kobj);
1894
1895	nofs_flag = memalloc_nofs_save();
1896
1897	if (device->bdev) {
1898		struct kobject *disk_kobj = bdev_kobj(device->bdev);
1899
1900		ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1901		if (ret) {
1902			btrfs_warn(device->fs_info,
1903				"creating sysfs device link for devid %llu failed: %d",
1904				device->devid, ret);
1905			goto out;
1906		}
1907	}
1908
1909	init_completion(&device->kobj_unregister);
1910	ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1911				   devinfo_kobj, "%llu", device->devid);
1912	if (ret) {
1913		kobject_put(&device->devid_kobj);
1914		btrfs_warn(device->fs_info,
1915			   "devinfo init for devid %llu failed: %d",
1916			   device->devid, ret);
1917	}
1918
1919out:
1920	memalloc_nofs_restore(nofs_flag);
1921	return ret;
1922}
1923
1924static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1925{
1926	int ret;
1927	struct btrfs_device *device;
1928	struct btrfs_fs_devices *seed;
1929
1930	list_for_each_entry(device, &fs_devices->devices, dev_list) {
1931		ret = btrfs_sysfs_add_device(device);
1932		if (ret)
1933			goto fail;
1934	}
1935
1936	list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1937		list_for_each_entry(device, &seed->devices, dev_list) {
1938			ret = btrfs_sysfs_add_device(device);
1939			if (ret)
1940				goto fail;
1941		}
1942	}
1943
1944	return 0;
1945
1946fail:
1947	btrfs_sysfs_remove_fs_devices(fs_devices);
1948	return ret;
1949}
1950
1951void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1952{
1953	int ret;
1954
1955	ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1956	if (ret)
1957		pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1958			action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1959			&disk_to_dev(bdev->bd_disk)->kobj);
1960}
1961
1962void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1963
1964{
1965	char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1966
1967	/*
1968	 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1969	 * directory
1970	 */
1971	snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1972	if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1973		btrfs_warn(fs_devices->fs_info,
1974				"sysfs: failed to create fsid for sprout");
1975}
1976
1977void btrfs_sysfs_update_devid(struct btrfs_device *device)
1978{
1979	char tmp[24];
1980
1981	snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1982
1983	if (kobject_rename(&device->devid_kobj, tmp))
1984		btrfs_warn(device->fs_devices->fs_info,
1985			   "sysfs: failed to update devid for %llu",
1986			   device->devid);
1987}
1988
1989/* /sys/fs/btrfs/ entry */
1990static struct kset *btrfs_kset;
1991
1992/*
1993 * Creates:
1994 *		/sys/fs/btrfs/UUID
1995 *
1996 * Can be called by the device discovery thread.
1997 */
1998int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1999{
2000	int error;
2001
2002	init_completion(&fs_devs->kobj_unregister);
2003	fs_devs->fsid_kobj.kset = btrfs_kset;
2004	error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
2005				     "%pU", fs_devs->fsid);
2006	if (error) {
2007		kobject_put(&fs_devs->fsid_kobj);
2008		return error;
2009	}
2010
2011	fs_devs->devices_kobj = kobject_create_and_add("devices",
2012						       &fs_devs->fsid_kobj);
2013	if (!fs_devs->devices_kobj) {
2014		btrfs_err(fs_devs->fs_info,
2015			  "failed to init sysfs device interface");
2016		btrfs_sysfs_remove_fsid(fs_devs);
2017		return -ENOMEM;
2018	}
2019
2020	fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
2021						       &fs_devs->fsid_kobj);
2022	if (!fs_devs->devinfo_kobj) {
2023		btrfs_err(fs_devs->fs_info,
2024			  "failed to init sysfs devinfo kobject");
2025		btrfs_sysfs_remove_fsid(fs_devs);
2026		return -ENOMEM;
2027	}
2028
2029	return 0;
2030}
2031
2032int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
2033{
2034	int error;
2035	struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
2036	struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
2037
2038	error = btrfs_sysfs_add_fs_devices(fs_devs);
2039	if (error)
2040		return error;
2041
2042	error = sysfs_create_files(fsid_kobj, btrfs_attrs);
2043	if (error) {
2044		btrfs_sysfs_remove_fs_devices(fs_devs);
2045		return error;
2046	}
2047
2048	error = sysfs_create_group(fsid_kobj,
2049				   &btrfs_feature_attr_group);
2050	if (error)
2051		goto failure;
2052
2053#ifdef CONFIG_BTRFS_DEBUG
2054	fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
2055	if (!fs_info->debug_kobj) {
2056		error = -ENOMEM;
2057		goto failure;
2058	}
2059
2060	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
2061	if (error)
2062		goto failure;
2063#endif
2064
2065	/* Discard directory */
2066	fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
2067	if (!fs_info->discard_kobj) {
2068		error = -ENOMEM;
2069		goto failure;
2070	}
2071
2072	error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
2073	if (error)
2074		goto failure;
2075
2076	error = addrm_unknown_feature_attrs(fs_info, true);
2077	if (error)
2078		goto failure;
2079
2080	error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
2081	if (error)
2082		goto failure;
2083
2084	fs_info->space_info_kobj = kobject_create_and_add("allocation",
2085						  fsid_kobj);
2086	if (!fs_info->space_info_kobj) {
2087		error = -ENOMEM;
2088		goto failure;
2089	}
2090
2091	error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2092	if (error)
2093		goto failure;
2094
2095	return 0;
2096failure:
2097	btrfs_sysfs_remove_mounted(fs_info);
2098	return error;
2099}
2100
2101static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2102				   struct kobj_attribute *a,
2103				   char *buf)
2104{
2105	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2106	bool enabled;
2107
2108	spin_lock(&fs_info->qgroup_lock);
2109	enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
2110	spin_unlock(&fs_info->qgroup_lock);
2111
2112	return sysfs_emit(buf, "%d\n", enabled);
2113}
2114BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2115
2116static ssize_t qgroup_mode_show(struct kobject *qgroups_kobj,
2117				struct kobj_attribute *a,
2118				char *buf)
2119{
2120	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2121	ssize_t ret = 0;
2122
2123	spin_lock(&fs_info->qgroup_lock);
2124	ASSERT(btrfs_qgroup_enabled(fs_info));
2125	switch (btrfs_qgroup_mode(fs_info)) {
2126	case BTRFS_QGROUP_MODE_FULL:
2127		ret = sysfs_emit(buf, "qgroup\n");
2128		break;
2129	case BTRFS_QGROUP_MODE_SIMPLE:
2130		ret = sysfs_emit(buf, "squota\n");
2131		break;
2132	default:
2133		btrfs_warn(fs_info, "unexpected qgroup mode %d\n",
2134			   btrfs_qgroup_mode(fs_info));
2135		break;
2136	}
2137	spin_unlock(&fs_info->qgroup_lock);
2138
2139	return ret;
2140}
2141BTRFS_ATTR(qgroups, mode, qgroup_mode_show);
2142
2143static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2144					struct kobj_attribute *a,
2145					char *buf)
2146{
2147	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2148	bool inconsistent;
2149
2150	spin_lock(&fs_info->qgroup_lock);
2151	inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
2152	spin_unlock(&fs_info->qgroup_lock);
2153
2154	return sysfs_emit(buf, "%d\n", inconsistent);
2155}
2156BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2157
2158static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2159					      struct kobj_attribute *a,
2160					      char *buf)
2161{
2162	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2163	u8 result;
2164
2165	spin_lock(&fs_info->qgroup_lock);
2166	result = fs_info->qgroup_drop_subtree_thres;
2167	spin_unlock(&fs_info->qgroup_lock);
2168
2169	return sysfs_emit(buf, "%d\n", result);
2170}
2171
2172static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2173					       struct kobj_attribute *a,
2174					       const char *buf, size_t len)
2175{
2176	struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2177	u8 new_thres;
2178	int ret;
2179
2180	ret = kstrtou8(buf, 10, &new_thres);
2181	if (ret)
2182		return -EINVAL;
2183
2184	if (new_thres > BTRFS_MAX_LEVEL)
2185		return -EINVAL;
2186
2187	spin_lock(&fs_info->qgroup_lock);
2188	fs_info->qgroup_drop_subtree_thres = new_thres;
2189	spin_unlock(&fs_info->qgroup_lock);
2190
2191	return len;
2192}
2193BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2194	      qgroup_drop_subtree_thres_store);
2195
2196/*
2197 * Qgroups global info
2198 *
2199 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2200 */
2201static struct attribute *qgroups_attrs[] = {
2202	BTRFS_ATTR_PTR(qgroups, enabled),
2203	BTRFS_ATTR_PTR(qgroups, inconsistent),
2204	BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2205	BTRFS_ATTR_PTR(qgroups, mode),
2206	NULL
2207};
2208ATTRIBUTE_GROUPS(qgroups);
2209
2210static void qgroups_release(struct kobject *kobj)
2211{
2212	kfree(kobj);
2213}
2214
2215static const struct kobj_type qgroups_ktype = {
2216	.sysfs_ops = &kobj_sysfs_ops,
2217	.default_groups = qgroups_groups,
2218	.release = qgroups_release,
2219};
2220
2221static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2222{
2223	return to_fs_info(kobj->parent->parent);
2224}
2225
2226#define QGROUP_ATTR(_member, _show_name)					\
2227static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj,		\
2228					   struct kobj_attribute *a,		\
2229					   char *buf)				\
2230{										\
2231	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
2232	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
2233			struct btrfs_qgroup, kobj);				\
2234	return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf);	\
2235}										\
2236BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2237
2238#define QGROUP_RSV_ATTR(_name, _type)						\
2239static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj,	\
2240					     struct kobj_attribute *a,		\
2241					     char *buf)				\
2242{										\
2243	struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj);	\
2244	struct btrfs_qgroup *qgroup = container_of(qgroup_kobj,			\
2245			struct btrfs_qgroup, kobj);				\
2246	return btrfs_show_u64(&qgroup->rsv.values[_type],			\
2247			&fs_info->qgroup_lock, buf);				\
2248}										\
2249BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2250
2251QGROUP_ATTR(rfer, referenced);
2252QGROUP_ATTR(excl, exclusive);
2253QGROUP_ATTR(max_rfer, max_referenced);
2254QGROUP_ATTR(max_excl, max_exclusive);
2255QGROUP_ATTR(lim_flags, limit_flags);
2256QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2257QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2258QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2259
2260/*
2261 * Qgroup information.
2262 *
2263 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2264 */
2265static struct attribute *qgroup_attrs[] = {
2266	BTRFS_ATTR_PTR(qgroup, referenced),
2267	BTRFS_ATTR_PTR(qgroup, exclusive),
2268	BTRFS_ATTR_PTR(qgroup, max_referenced),
2269	BTRFS_ATTR_PTR(qgroup, max_exclusive),
2270	BTRFS_ATTR_PTR(qgroup, limit_flags),
2271	BTRFS_ATTR_PTR(qgroup, rsv_data),
2272	BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2273	BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2274	NULL
2275};
2276ATTRIBUTE_GROUPS(qgroup);
2277
2278static void qgroup_release(struct kobject *kobj)
2279{
2280	struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2281
2282	memset(&qgroup->kobj, 0, sizeof(*kobj));
2283}
2284
2285static const struct kobj_type qgroup_ktype = {
2286	.sysfs_ops = &kobj_sysfs_ops,
2287	.release = qgroup_release,
2288	.default_groups = qgroup_groups,
2289};
2290
2291int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2292				struct btrfs_qgroup *qgroup)
2293{
2294	struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2295	int ret;
2296
2297	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2298		return 0;
2299	if (qgroup->kobj.state_initialized)
2300		return 0;
2301	if (!qgroups_kobj)
2302		return -EINVAL;
2303
2304	ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2305			"%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2306			btrfs_qgroup_subvolid(qgroup->qgroupid));
2307	if (ret < 0)
2308		kobject_put(&qgroup->kobj);
2309
2310	return ret;
2311}
2312
2313void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2314{
2315	struct btrfs_qgroup *qgroup;
2316	struct btrfs_qgroup *next;
2317
2318	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2319		return;
2320
2321	rbtree_postorder_for_each_entry_safe(qgroup, next,
2322					     &fs_info->qgroup_tree, node)
2323		btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2324	if (fs_info->qgroups_kobj) {
2325		kobject_del(fs_info->qgroups_kobj);
2326		kobject_put(fs_info->qgroups_kobj);
2327		fs_info->qgroups_kobj = NULL;
2328	}
2329}
2330
2331/* Called when qgroups get initialized, thus there is no need for locking */
2332int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2333{
2334	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2335	struct btrfs_qgroup *qgroup;
2336	struct btrfs_qgroup *next;
2337	int ret = 0;
2338
2339	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2340		return 0;
2341
2342	ASSERT(fsid_kobj);
2343	if (fs_info->qgroups_kobj)
2344		return 0;
2345
2346	fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
2347	if (!fs_info->qgroups_kobj)
2348		return -ENOMEM;
2349
2350	ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2351				   fsid_kobj, "qgroups");
2352	if (ret < 0)
2353		goto out;
2354
2355	rbtree_postorder_for_each_entry_safe(qgroup, next,
2356					     &fs_info->qgroup_tree, node) {
2357		ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2358		if (ret < 0)
2359			goto out;
2360	}
2361
2362out:
2363	if (ret < 0)
2364		btrfs_sysfs_del_qgroups(fs_info);
2365	return ret;
2366}
2367
2368void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2369				struct btrfs_qgroup *qgroup)
2370{
2371	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2372		return;
2373
2374	if (qgroup->kobj.state_initialized) {
2375		kobject_del(&qgroup->kobj);
2376		kobject_put(&qgroup->kobj);
2377	}
2378}
2379
2380/*
2381 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2382 * values in superblock. Call after any changes to incompat/compat_ro flags
2383 */
2384void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info)
2385{
2386	struct kobject *fsid_kobj;
2387	int ret;
2388
2389	if (!fs_info)
2390		return;
2391
2392	fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2393	if (!fsid_kobj->state_initialized)
2394		return;
2395
2396	ret = sysfs_update_group(fsid_kobj, &btrfs_feature_attr_group);
2397	if (ret < 0)
2398		btrfs_warn(fs_info,
2399			   "failed to update /sys/fs/btrfs/%pU/features: %d",
2400			   fs_info->fs_devices->fsid, ret);
2401}
2402
2403int __init btrfs_init_sysfs(void)
2404{
2405	int ret;
2406
2407	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2408	if (!btrfs_kset)
2409		return -ENOMEM;
2410
2411	init_feature_attrs();
2412	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2413	if (ret)
2414		goto out2;
2415	ret = sysfs_merge_group(&btrfs_kset->kobj,
2416				&btrfs_static_feature_attr_group);
2417	if (ret)
2418		goto out_remove_group;
2419
2420#ifdef CONFIG_BTRFS_DEBUG
2421	ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2422	if (ret) {
2423		sysfs_unmerge_group(&btrfs_kset->kobj,
2424				    &btrfs_static_feature_attr_group);
2425		goto out_remove_group;
2426	}
2427#endif
2428
2429	return 0;
2430
2431out_remove_group:
2432	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2433out2:
2434	kset_unregister(btrfs_kset);
2435
2436	return ret;
2437}
2438
2439void __cold btrfs_exit_sysfs(void)
2440{
2441	sysfs_unmerge_group(&btrfs_kset->kobj,
2442			    &btrfs_static_feature_attr_group);
2443	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2444#ifdef CONFIG_BTRFS_DEBUG
2445	sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2446#endif
2447	kset_unregister(btrfs_kset);
2448}
v3.5.6
 
 1/*
 2 * Copyright (C) 2007 Oracle.  All rights reserved.
 3 *
 4 * This program is free software; you can redistribute it and/or
 5 * modify it under the terms of the GNU General Public
 6 * License v2 as published by the Free Software Foundation.
 7 *
 8 * This program is distributed in the hope that it will be useful,
 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
 
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/completion.h>
23#include <linux/buffer_head.h>
24#include <linux/module.h>
25#include <linux/kobject.h>
26
27#include "ctree.h"
 
28#include "disk-io.h"
 
29#include "transaction.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31/* /sys/fs/btrfs/ entry */
32static struct kset *btrfs_kset;
33
34int btrfs_init_sysfs(void)
 
 
 
 
 
 
35{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36	btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
37	if (!btrfs_kset)
38		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39	return 0;
 
 
 
 
 
 
 
40}
41
42void btrfs_exit_sysfs(void)
43{
 
 
 
 
 
 
44	kset_unregister(btrfs_kset);
45}
46