Skip to contentSkip to conversation

Sysfs cleanups

PATCH New David Sterba 1 participant 14 messages Aug 2, 2019 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Sysfs cleanups

Handful of almost trivial changes that clean up the sysfs structures and helpers so the sysfs-related api is used only inside sysfs.c and that the API facing btrfs is minimized.

David Sterba (13):
btrfs: move sysfs declarations out of ctree.h
btrfs: move btrfs_add_raid_kobjects to sysfs.c
btrfs: factor sysfs code out of link_block_group
btrfs: sysfs: unexport btrfs_raid_ktype
btrfs: factor out sysfs code for creating space infos
btrfs: sysfs: unexport space_info_ktype
btrfs: sysfs: replace direct access to feature set names with a helper
btrfs: factor out sysfs code for sending device uevent
btrfs: factor out sysfs code for deleting block group and space infos
btrfs: factor out sysfs code for updating sprout fsid
btrfs: cleanup kobject.h includes
btrfs: sysfs: move helper macros to sysfs.c
btrfs: sysfs: move type conversion helpers to sysfs.c

 fs/btrfs/ctree.h       |  15 ---
 fs/btrfs/extent-tree.c |  59 +----------
 fs/btrfs/ioctl.c       |   2 +-
 fs/btrfs/space-info.c  |  25 +----
 fs/btrfs/super.c       |   1 +
 fs/btrfs/sysfs.c       | 217 ++++++++++++++++++++++++++++++++++++++++-
 fs/btrfs/sysfs.h       |  80 +++------------
 fs/btrfs/volumes.c     |  25 +----
 8 files changed, 238 insertions(+), 186 deletions(-)
Signature · 1 line
2.22.0
author maintainer
#1 lore raw

PATCH 1/13 btrfs: move sysfs declarations out of ctree.h

As the header for sysfs code already exists, use it to clean up ctree.h.

---
 fs/btrfs/ctree.h | 13 -------------
 fs/btrfs/super.c |  1 +
 fs/btrfs/sysfs.h | 12 ++++++++++++
 3 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/ctree.h +0 -13
index 1110bbd4bffb..894a249b2182 100644
@@ -397,13 +397,6 @@ struct btrfs_dev_replace {
 	wait_queue_head_t replace_wait;
 };
 
-/* For raid type sysfs entries */
-struct raid_kobject {
-	u64 flags;
-	struct kobject kobj;
-	struct list_head list;
-};
-
 /*
  * free clusters are used to claim free space in relatively large chunks,
  * allowing us to do less seeky writes. They are used for all metadata
@@ -3265,12 +3258,6 @@ loff_t btrfs_remap_file_range(struct file *file_in, loff_t pos_in,
 int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
 			struct btrfs_root *root);
 
-/* sysfs.c */
-int __init btrfs_init_sysfs(void);
-void __cold btrfs_exit_sysfs(void);
-int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
-void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
-
 /* super.c */
 int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
 			unsigned long new_flags);
fs/btrfs/super.c +1 -0
index 10bc7e6cca75..69eaa198e51e 100644
@@ -43,6 +43,7 @@
 #include "free-space-cache.h"
 #include "backref.h"
 #include "space-info.h"
+#include "sysfs.h"
 #include "tests/btrfs-tests.h"
 
 #include "qgroup.h"
fs/btrfs/sysfs.h +12 -0
index 40716b357c1d..f9fc7101e696 100644
@@ -40,6 +40,13 @@ struct btrfs_feature_attr {
 	u64 feature_bit;
 };
 
+/* For raid type sysfs entries */
+struct raid_kobject {
+	u64 flags;
+	struct kobject kobj;
+	struct list_head list;
+};
+
 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
 static struct btrfs_feature_attr btrfs_attr_features_##_name = {	     \
 	.kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,			     \
@@ -91,4 +98,9 @@ void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs);
 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
 		u64 bit, enum btrfs_feature_set set);
 
+int __init btrfs_init_sysfs(void);
+void __cold btrfs_exit_sysfs(void);
+int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
+void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
+
 #endif
Signature · 1 line
2.22.0
author maintainer
#2 lore raw

PATCH 2/13 btrfs: move btrfs_add_raid_kobjects to sysfs.c

In reply to David Sterba

The function manipulates sysfs entries so this belongs to sysfs.c.

---
 fs/btrfs/ctree.h       |  1 -
 fs/btrfs/extent-tree.c | 27 ---------------------------
 fs/btrfs/sysfs.c       | 27 +++++++++++++++++++++++++++
 fs/btrfs/sysfs.h       |  1 +
 4 files changed, 28 insertions(+), 28 deletions(-)
fs/btrfs/ctree.h +0 -1
index 894a249b2182..a61bf19a7ef6 100644
@@ -2700,7 +2700,6 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info);
 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
 			   u64 bytes_used, u64 type, u64 chunk_offset,
 			   u64 size);
-void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
 				struct btrfs_fs_info *fs_info,
 				const u64 chunk_offset);
fs/btrfs/extent-tree.c +0 -27
index 81f109357050..7f7a0530b0e2 100644
@@ -7774,33 +7774,6 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 	return 0;
 }
 
-/* link_block_group will queue up kobjects to add when we're reclaim-safe */
-void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
-{
-	struct btrfs_space_info *space_info;
-	struct raid_kobject *rkobj;
-	LIST_HEAD(list);
-	int ret = 0;
-
-	spin_lock(&fs_info->pending_raid_kobjs_lock);
-	list_splice_init(&fs_info->pending_raid_kobjs, &list);
-	spin_unlock(&fs_info->pending_raid_kobjs_lock);
-
-	list_for_each_entry(rkobj, &list, list) {
-		space_info = btrfs_find_space_info(fs_info, rkobj->flags);
-
-		ret = kobject_add(&rkobj->kobj, &space_info->kobj,
-				"%s", btrfs_bg_type_to_raid_name(rkobj->flags));
-		if (ret) {
-			kobject_put(&rkobj->kobj);
-			break;
-		}
-	}
-	if (ret)
-		btrfs_warn(fs_info,
-			   "failed to add kobject for block cache, ignoring");
-}
-
 static void link_block_group(struct btrfs_block_group_cache *cache)
 {
 	struct btrfs_space_info *space_info = cache->space_info;
fs/btrfs/sysfs.c +27 -0
index 9539f8143b7a..a6894b6c2623 100644
@@ -730,6 +730,33 @@ static void init_feature_attrs(void)
 	}
 }
 
+/* link_block_group will queue up kobjects to add when we're reclaim-safe */
+void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
+{
+	struct btrfs_space_info *space_info;
+	struct raid_kobject *rkobj;
+	LIST_HEAD(list);
+	int ret = 0;
+
+	spin_lock(&fs_info->pending_raid_kobjs_lock);
+	list_splice_init(&fs_info->pending_raid_kobjs, &list);
+	spin_unlock(&fs_info->pending_raid_kobjs_lock);
+
+	list_for_each_entry(rkobj, &list, list) {
+		space_info = btrfs_find_space_info(fs_info, rkobj->flags);
+
+		ret = kobject_add(&rkobj->kobj, &space_info->kobj,
+				"%s", btrfs_bg_type_to_raid_name(rkobj->flags));
+		if (ret) {
+			kobject_put(&rkobj->kobj);
+			break;
+		}
+	}
+	if (ret)
+		btrfs_warn(fs_info,
+			   "failed to add kobject for block cache, ignoring");
+}
+
 /* when one_device is NULL, it removes all device links */
 
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
fs/btrfs/sysfs.h +1 -0
index f9fc7101e696..25f028b65316 100644
@@ -102,5 +102,6 @@ int __init btrfs_init_sysfs(void);
 void __cold btrfs_exit_sysfs(void);
 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
+void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
 
 #endif
Signature · 1 line
2.22.0
author maintainer
#3 lore raw

PATCH 3/13 btrfs: factor sysfs code out of link_block_group

In reply to David Sterba

The part of link_block_group that just creates the sysfs object is independent and can be factored out to a helper.

---
 fs/btrfs/extent-tree.c | 18 ++----------------
 fs/btrfs/sysfs.c       | 27 +++++++++++++++++++++++++++
 fs/btrfs/sysfs.h       |  1 +
 3 files changed, 30 insertions(+), 16 deletions(-)
fs/btrfs/extent-tree.c +2 -16
index 7f7a0530b0e2..8ac496fddc59 100644
@@ -7777,7 +7777,6 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 static void link_block_group(struct btrfs_block_group_cache *cache)
 {
 	struct btrfs_space_info *space_info = cache->space_info;
-	struct btrfs_fs_info *fs_info = cache->fs_info;
 	int index = btrfs_bg_flags_to_raid_index(cache->flags);
 	bool first = false;
 
@@ -7787,21 +7786,8 @@ static void link_block_group(struct btrfs_block_group_cache *cache)
 	list_add_tail(&cache->list, &space_info->block_groups[index]);
 	up_write(&space_info->groups_sem);
 
-	if (first) {
-		struct raid_kobject *rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
-		if (!rkobj) {
-			btrfs_warn(cache->fs_info,
-				"couldn't alloc memory for raid level kobject");
-			return;
-		}
-		rkobj->flags = cache->flags;
-		kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
-
-		spin_lock(&fs_info->pending_raid_kobjs_lock);
-		list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
-		spin_unlock(&fs_info->pending_raid_kobjs_lock);
-		space_info->block_group_kobjs[index] = &rkobj->kobj;
-	}
+	if (first)
+		btrfs_sysfs_add_block_group_type(cache);
 }
 
 static struct btrfs_block_group_cache *
fs/btrfs/sysfs.c +27 -0
index a6894b6c2623..008d58f618cd 100644
@@ -757,6 +757,33 @@ void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info)
 			   "failed to add kobject for block cache, ignoring");
 }
 
+/*
+ * Create a sysfs entry for a given block group type at path
+ * /sys/fs/btrfs/UUID/allocation/data/TYPE
+ */
+void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
+{
+	struct btrfs_fs_info *fs_info = cache->fs_info;
+	struct btrfs_space_info *space_info = cache->space_info;
+	struct raid_kobject *rkobj;
+	const int index = btrfs_bg_flags_to_raid_index(cache->flags);
+
+	rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
+	if (!rkobj) {
+		btrfs_warn(cache->fs_info,
+				"couldn't alloc memory for raid level kobject");
+		return;
+	}
+
+	rkobj->flags = cache->flags;
+	kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
+
+	spin_lock(&fs_info->pending_raid_kobjs_lock);
+	list_add_tail(&rkobj->list, &fs_info->pending_raid_kobjs);
+	spin_unlock(&fs_info->pending_raid_kobjs_lock);
+	space_info->block_group_kobjs[index] = &rkobj->kobj;
+}
+
 /* when one_device is NULL, it removes all device links */
 
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
fs/btrfs/sysfs.h +1 -0
index 25f028b65316..fa1b9bb5d320 100644
@@ -103,5 +103,6 @@ void __cold btrfs_exit_sysfs(void);
 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
 void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
+void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache);
 
 #endif
Signature · 1 line
2.22.0
author maintainer
#4 lore raw

PATCH 4/13 btrfs: sysfs: unexport btrfs_raid_ktype

In reply to David Sterba

The last non-sysfs usage of btrfs_raid_ktype has been moved to a private helper in previous patch so the variable can be made static.

---
 fs/btrfs/sysfs.c | 2 +-
 fs/btrfs/sysfs.h | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/sysfs.c +1 -1
index 008d58f618cd..25097d6cf2ca 100644
@@ -316,7 +316,7 @@ static void release_raid_kobj(struct kobject *kobj)
 	kfree(to_raid_kobj(kobj));
 }
 
-struct kobj_type btrfs_raid_ktype = {
+static struct kobj_type btrfs_raid_ktype = {
 	.sysfs_ops = &kobj_sysfs_ops,
 	.release = release_raid_kobj,
 	.default_groups = raid_groups,
fs/btrfs/sysfs.h +0 -1
index fa1b9bb5d320..7f659ce7be84 100644
@@ -86,7 +86,6 @@ attr_to_btrfs_feature_attr(struct attribute *attr)
 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags);
 extern const char * const btrfs_feature_set_names[FEAT_MAX];
 extern struct kobj_type space_info_ktype;
-extern struct kobj_type btrfs_raid_ktype;
 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
 		struct btrfs_device *one_device);
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Signature · 1 line
2.22.0
author maintainer
#5 lore raw

PATCH 5/13 btrfs: factor out sysfs code for creating space infos

In reply to David Sterba

Move creation of data/metadata/system space info directories to sysfs.c.

---
 fs/btrfs/space-info.c | 25 ++-----------------------
 fs/btrfs/sysfs.c      | 37 +++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h      |  2 ++
 3 files changed, 41 insertions(+), 23 deletions(-)
fs/btrfs/space-info.c +2 -23
index ab7b9ec4c240..061a216ff966 100644
@@ -33,23 +33,6 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
 	rcu_read_unlock();
 }
 
-static const char *alloc_name(u64 flags)
-{
-	switch (flags) {
-	case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
-		return "mixed";
-	case BTRFS_BLOCK_GROUP_METADATA:
-		return "metadata";
-	case BTRFS_BLOCK_GROUP_DATA:
-		return "data";
-	case BTRFS_BLOCK_GROUP_SYSTEM:
-		return "system";
-	default:
-		WARN_ON(1);
-		return "invalid-combination";
-	};
-}
-
 static int create_space_info(struct btrfs_fs_info *info, u64 flags)
 {
 
@@ -79,13 +62,9 @@ static int create_space_info(struct btrfs_fs_info *info, u64 flags)
 	INIT_LIST_HEAD(&space_info->tickets);
 	INIT_LIST_HEAD(&space_info->priority_tickets);
 
-	ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
-				    info->space_info_kobj, "%s",
-				    alloc_name(space_info->flags));
-	if (ret) {
-		kobject_put(&space_info->kobj);
+	ret = btrfs_sysfs_add_space_info_type(info, space_info);
+	if (ret)
 		return ret;
-	}
 
 	list_add_rcu(&space_info->list, &info->space_info);
 	if (flags & BTRFS_BLOCK_GROUP_DATA)
fs/btrfs/sysfs.c +37 -0
index 25097d6cf2ca..24aaac281580 100644
@@ -784,6 +784,43 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
 	space_info->block_group_kobjs[index] = &rkobj->kobj;
 }
 
+static const char *alloc_name(u64 flags)
+{
+	switch (flags) {
+	case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
+		return "mixed";
+	case BTRFS_BLOCK_GROUP_METADATA:
+		return "metadata";
+	case BTRFS_BLOCK_GROUP_DATA:
+		return "data";
+	case BTRFS_BLOCK_GROUP_SYSTEM:
+		return "system";
+	default:
+		WARN_ON(1);
+		return "invalid-combination";
+	};
+}
+
+/*
+ * Create a sysfs entry for a space info type at path
+ * /sys/fs/btrfs/UUID/allocation/TYPE
+ */
+int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
+				    struct btrfs_space_info *space_info)
+{
+	int ret;
+
+	ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
+				   fs_info->space_info_kobj, "%s",
+				   alloc_name(space_info->flags));
+	if (ret) {
+		kobject_put(&space_info->kobj);
+		return ret;
+	}
+
+	return 0;
+}
+
 /* when one_device is NULL, it removes all device links */
 
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
fs/btrfs/sysfs.h +2 -0
index 7f659ce7be84..928c8ab37fd9 100644
@@ -103,5 +103,7 @@ int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
 void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache);
+int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
+				    struct btrfs_space_info *space_info);
 
 #endif
Signature · 1 line
2.22.0
author maintainer
#6 lore raw

PATCH 6/13 btrfs: sysfs: unexport space_info_ktype

In reply to David Sterba

The last non-sysfs usage of space_info_ktype has been moved to a private helper in previous patch so the variable can be made static.

---
 fs/btrfs/sysfs.c | 2 +-
 fs/btrfs/sysfs.h | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/sysfs.c +1 -1
index 24aaac281580..90822cdbf59e 100644
@@ -375,7 +375,7 @@ static void space_info_release(struct kobject *kobj)
 	kfree(sinfo);
 }
 
-struct kobj_type space_info_ktype = {
+static struct kobj_type space_info_ktype = {
 	.sysfs_ops = &kobj_sysfs_ops,
 	.release = space_info_release,
 	.default_groups = space_info_groups,
fs/btrfs/sysfs.h +0 -1
index 928c8ab37fd9..0550358eb24e 100644
@@ -85,7 +85,6 @@ attr_to_btrfs_feature_attr(struct attribute *attr)
 
 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags);
 extern const char * const btrfs_feature_set_names[FEAT_MAX];
-extern struct kobj_type space_info_ktype;
 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
 		struct btrfs_device *one_device);
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Signature · 1 line
2.22.0
author maintainer
#7 lore raw

PATCH 7/13 btrfs: sysfs: replace direct access to feature set names with a helper

In reply to David Sterba

In order to unexport the feature type array, add a helper for the enum-to-string conversion.

---
 fs/btrfs/ioctl.c | 2 +-
 fs/btrfs/sysfs.c | 7 ++++++-
 fs/btrfs/sysfs.h | 2 +-
 3 files changed, 8 insertions(+), 3 deletions(-)
fs/btrfs/ioctl.c +1 -1
index ccac62d40dd2..fa091b6cc0ba 100644
@@ -5258,7 +5258,7 @@ static int check_feature_bits(struct btrfs_fs_info *fs_info,
 			      u64 change_mask, u64 flags, u64 supported_flags,
 			      u64 safe_set, u64 safe_clear)
 {
-	const char *type = btrfs_feature_set_names[set];
+	const char *type = btrfs_feature_set_name(set);
 	char *names;
 	u64 disallowed, unsupported;
 	u64 set_mask = flags & change_mask;
fs/btrfs/sysfs.c +6 -1
index 90822cdbf59e..0d37403a4733 100644
@@ -655,12 +655,17 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
 	btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
 }
 
-const char * const btrfs_feature_set_names[FEAT_MAX] = {
+static const char * const btrfs_feature_set_names[FEAT_MAX] = {
 	[FEAT_COMPAT]	 = "compat",
 	[FEAT_COMPAT_RO] = "compat_ro",
 	[FEAT_INCOMPAT]	 = "incompat",
 };
 
+const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
+{
+	return btrfs_feature_set_names[set];
+}
+
 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
 {
 	size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
fs/btrfs/sysfs.h +1 -1
index 0550358eb24e..f17faa5d5264 100644
@@ -84,7 +84,7 @@ attr_to_btrfs_feature_attr(struct attribute *attr)
 }
 
 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags);
-extern const char * const btrfs_feature_set_names[FEAT_MAX];
+const char * const btrfs_feature_set_name(enum btrfs_feature_set set);
 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
 		struct btrfs_device *one_device);
 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Signature · 1 line
2.22.0
author maintainer
#8 lore raw

PATCH 8/13 btrfs: factor out sysfs code for sending device uevent

In reply to David Sterba

The device uevent belongs to the sysfs API.

---
 fs/btrfs/sysfs.c   | 11 +++++++++++
 fs/btrfs/sysfs.h   |  1 +
 fs/btrfs/volumes.c | 13 -------------
 3 files changed, 12 insertions(+), 13 deletions(-)
fs/btrfs/sysfs.c +11 -0
index 0d37403a4733..0f7e97ceec4e 100644
@@ -902,6 +902,17 @@ int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
 	return error;
 }
 
+void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
+{
+	int ret;
+
+	ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
+	if (ret)
+		pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
+			action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
+			&disk_to_dev(bdev->bd_disk)->kobj);
+}
+
 /* /sys/fs/btrfs/ entry */
 static struct kset *btrfs_kset;
 
fs/btrfs/sysfs.h +1 -0
index f17faa5d5264..371fa9db5bbd 100644
@@ -95,6 +95,7 @@ int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs);
 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs);
 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
 		u64 bit, enum btrfs_feature_set set);
+void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action);
 
 int __init btrfs_init_sysfs(void);
 void __cold btrfs_exit_sysfs(void);
fs/btrfs/volumes.c +0 -13
index f85bbc819ab6..bc20e01f2f93 100644
@@ -358,19 +358,6 @@ static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
 	kfree(fs_devices);
 }
 
-static void btrfs_kobject_uevent(struct block_device *bdev,
-				 enum kobject_action action)
-{
-	int ret;
-
-	ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
-	if (ret)
-		pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
-			action,
-			kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
-			&disk_to_dev(bdev->bd_disk)->kobj);
-}
-
 void __exit btrfs_cleanup_fs_uuids(void)
 {
 	struct btrfs_fs_devices *fs_devices;
Signature · 1 line
2.22.0
author maintainer
#9 lore raw

PATCH 9/13 btrfs: factor out sysfs code for deleting block group and space infos

In reply to David Sterba

The helpers to create block group and space info directories already live in sysfs.c, move the deletion part there too.

---
 fs/btrfs/extent-tree.c | 14 +-------------
 fs/btrfs/sysfs.c       | 22 ++++++++++++++++++++++
 fs/btrfs/sysfs.h       |  1 +
 3 files changed, 24 insertions(+), 13 deletions(-)
fs/btrfs/extent-tree.c +1 -13
index 8ac496fddc59..3a711f5e7919 100644
@@ -7744,8 +7744,6 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 	btrfs_release_global_block_rsv(info);
 
 	while (!list_empty(&info->space_info)) {
-		int i;
-
 		space_info = list_entry(info->space_info.next,
 					struct btrfs_space_info,
 					list);
@@ -7759,17 +7757,7 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
 			    space_info->bytes_may_use > 0))
 			btrfs_dump_space_info(info, space_info, 0, 0);
 		list_del(&space_info->list);
-		for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
-			struct kobject *kobj;
-			kobj = space_info->block_group_kobjs[i];
-			space_info->block_group_kobjs[i] = NULL;
-			if (kobj) {
-				kobject_del(kobj);
-				kobject_put(kobj);
-			}
-		}
-		kobject_del(&space_info->kobj);
-		kobject_put(&space_info->kobj);
+		btrfs_sysfs_remove_space_info(space_info);
 	}
 	return 0;
 }
fs/btrfs/sysfs.c +22 -0
index 0f7e97ceec4e..2490144863ae 100644
@@ -789,6 +789,28 @@ void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
 	space_info->block_group_kobjs[index] = &rkobj->kobj;
 }
 
+/*
+ * Remove sysfs directories for all block group types of a given space info and
+ * the space info as well
+ */
+void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
+{
+	int i;
+
+	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
+		struct kobject *kobj;
+
+		kobj = space_info->block_group_kobjs[i];
+		space_info->block_group_kobjs[i] = NULL;
+		if (kobj) {
+			kobject_del(kobj);
+			kobject_put(kobj);
+		}
+	}
+	kobject_del(&space_info->kobj);
+	kobject_put(&space_info->kobj);
+}
+
 static const char *alloc_name(u64 flags)
 {
 	switch (flags) {
fs/btrfs/sysfs.h +1 -0
index 371fa9db5bbd..857710e77775 100644
@@ -105,5 +105,6 @@ void btrfs_add_raid_kobjects(struct btrfs_fs_info *fs_info);
 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache);
 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
 				    struct btrfs_space_info *space_info);
+void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info);
 
 #endif
Signature · 1 line
2.22.0
author maintainer
#10 lore raw

PATCH 10/13 btrfs: factor out sysfs code for updating sprout fsid

In reply to David Sterba

Wrap the fsid renaming code and move it to sysfs.c.

---
 fs/btrfs/sysfs.c   | 15 +++++++++++++++
 fs/btrfs/sysfs.h   |  2 ++
 fs/btrfs/volumes.c | 12 ++----------
 3 files changed, 19 insertions(+), 10 deletions(-)
fs/btrfs/sysfs.c +15 -0
index 2490144863ae..b7eb921e3fd3 100644
@@ -935,6 +935,21 @@ void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
 			&disk_to_dev(bdev->bd_disk)->kobj);
 }
 
+void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
+				    const u8 *fsid)
+{
+	char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
+
+	/*
+	 * Sprouting changes fsid of the mounted filesystem, rename the fsid
+	 * directory
+	 */
+	snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
+	if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
+		btrfs_warn(fs_devices->fs_info,
+				"sysfs: failed to create fsid for sprout");
+}
+
 /* /sys/fs/btrfs/ entry */
 static struct kset *btrfs_kset;
 
fs/btrfs/sysfs.h +2 -0
index 857710e77775..aabc67a20ce5 100644
@@ -93,6 +93,8 @@ int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
 				struct kobject *parent);
 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs);
 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs);
+void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
+				    const u8 *fsid);
 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
 		u64 bit, enum btrfs_feature_set set);
 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action);
fs/btrfs/volumes.c +2 -10
index bc20e01f2f93..d32eaffbbcef 100644
@@ -2680,22 +2680,14 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	}
 
 	if (seeding_dev) {
-		char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
-
 		ret = btrfs_finish_sprout(trans);
 		if (ret) {
 			btrfs_abort_transaction(trans, ret);
 			goto error_sysfs;
 		}
 
-		/* Sprouting would change fsid of the mounted root,
-		 * so rename the fsid on the sysfs
-		 */
-		snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
-						fs_info->fs_devices->fsid);
-		if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
-			btrfs_warn(fs_info,
-				   "sysfs: failed to create fsid for sprout");
+		btrfs_sysfs_update_sprout_fsid(fs_devices,
+				fs_info->fs_devices->fsid);
 	}
 
 	ret = btrfs_commit_transaction(trans);
Signature · 1 line
2.22.0
author maintainer
#11 lore raw

PATCH 11/13 btrfs: cleanup kobject.h includes

In reply to David Sterba

The kobject should be pulled in via sysfs.h and that needs to include it because it needs various definitions like kobj_attribute or kobject.

---
 fs/btrfs/ctree.h | 1 -
 fs/btrfs/sysfs.c | 1 -
 fs/btrfs/sysfs.h | 2 ++
 3 files changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/ctree.h +0 -1
index a61bf19a7ef6..e32d992a3597 100644
@@ -16,7 +16,6 @@
 #include <linux/backing-dev.h>
 #include <linux/wait.h>
 #include <linux/slab.h>
-#include <linux/kobject.h>
 #include <trace/events/btrfs.h>
 #include <asm/kmap_types.h>
 #include <asm/unaligned.h>
fs/btrfs/sysfs.c +0 -1
index b7eb921e3fd3..624ab9e29e21 100644
@@ -7,7 +7,6 @@
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/completion.h>
-#include <linux/kobject.h>
 #include <linux/bug.h>
 #include <linux/debugfs.h>
 
fs/btrfs/sysfs.h +2 -0
index aabc67a20ce5..ab5e39b5496a 100644
@@ -3,6 +3,8 @@
 #ifndef BTRFS_SYSFS_H
 #define BTRFS_SYSFS_H
 
+#include <linux/kobject.h>
+
 /*
  * Data exported through sysfs
  */
Signature · 1 line
2.22.0
author maintainer
#12 lore raw

PATCH 12/13 btrfs: sysfs: move helper macros to sysfs.c

In reply to David Sterba

None of the macros is used outside of sysfs.c.

---
 fs/btrfs/sysfs.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/sysfs.h | 50 ------------------------------------------------
 2 files changed, 49 insertions(+), 50 deletions(-)
fs/btrfs/sysfs.c +49 -0
index 624ab9e29e21..e306e6e173cf 100644
@@ -17,6 +17,55 @@
 #include "volumes.h"
 #include "space-info.h"
 
+struct btrfs_feature_attr {
+	struct kobj_attribute kobj_attr;
+	enum btrfs_feature_set feature_set;
+	u64 feature_bit;
+};
+
+/* For raid type sysfs entries */
+struct raid_kobject {
+	u64 flags;
+	struct kobject kobj;
+	struct list_head list;
+};
+
+#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store)			\
+{									\
+	.attr	= { .name = __stringify(_name), .mode = _mode },	\
+	.show	= _show,						\
+	.store	= _store,						\
+}
+
+#define BTRFS_ATTR_RW(_prefix, _name, _show, _store)			\
+	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
+			__INIT_KOBJ_ATTR(_name, 0644, _show, _store)
+
+#define BTRFS_ATTR(_prefix, _name, _show)				\
+	static struct kobj_attribute btrfs_attr_##_prefix##_##_name =	\
+			__INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
+
+#define BTRFS_ATTR_PTR(_prefix, _name)					\
+	(&btrfs_attr_##_prefix##_##_name.attr)
+
+#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit)  \
+static struct btrfs_feature_attr btrfs_attr_features_##_name = {	     \
+	.kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO,			     \
+				      btrfs_feature_attr_show,		     \
+				      btrfs_feature_attr_store),	     \
+	.feature_set	= _feature_set,					     \
+	.feature_bit	= _feature_prefix ##_## _feature_bit,		     \
+}
+#define BTRFS_FEAT_ATTR_PTR(_name)					     \
+	(&btrfs_attr_features_##_name.kobj_attr.attr)
+
+#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
+	BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
+#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
+	BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
+#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
+	BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
+
 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
 
fs/btrfs/sysfs.h +0 -50

56 lines not on this page · raw message

Signature · 1 line
2.22.0
author maintainer
#13 lore raw

PATCH 13/13 btrfs: sysfs: move type conversion helpers to sysfs.c

In reply to David Sterba
---
 fs/btrfs/sysfs.c | 17 +++++++++++++++++
 fs/btrfs/sysfs.h | 18 ------------------
 2 files changed, 17 insertions(+), 18 deletions(-)
fs/btrfs/sysfs.c +17 -0
index e306e6e173cf..335450ad9d2d 100644
@@ -69,6 +69,23 @@ static struct btrfs_feature_attr btrfs_attr_features_##_name = {	     \
 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
 
+/* convert from attribute */
+static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
+{
+	return container_of(a, struct btrfs_feature_attr, kobj_attr);
+}
+
+static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
+{
+	return container_of(attr, struct kobj_attribute, attr);
+}
+
+static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
+		struct attribute *attr)
+{
+	return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
+}
+
 static u64 get_features(struct btrfs_fs_info *fs_info,
 			enum btrfs_feature_set set)
 {
fs/btrfs/sysfs.h +0 -18
index ab0ea53b7d4b..b8513c5fb2b0 100644
@@ -17,24 +17,6 @@ enum btrfs_feature_set {
 	FEAT_MAX
 };
 
-/* convert from attribute */
-static inline struct btrfs_feature_attr *
-to_btrfs_feature_attr(struct kobj_attribute *a)
-{
-	return container_of(a, struct btrfs_feature_attr, kobj_attr);
-}
-
-static inline struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
-{
-	return container_of(attr, struct kobj_attribute, attr);
-}
-
-static inline struct btrfs_feature_attr *
-attr_to_btrfs_feature_attr(struct attribute *attr)
-{
-	return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
-}
-
 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags);
 const char * const btrfs_feature_set_name(enum btrfs_feature_set set);
 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Signature · 1 line
2.22.0