[PATCH] sysfs: add filter function to groups

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Kay Sievers <kay.sievers@...>, Greg KH <greg@...>
Cc: linux-scsi <linux-scsi@...>, linux-kernel <linux-kernel@...>
Date: Monday, October 29, 2007 - 11:16 am

In the SCSI transport classes (and soon to be in the AEN event
subsystem) we have a lot of need for a grouping that doesn't include all
files in the group.  We basically want to show capability by which file
is present.  A classic example of this is the SPI transport class
connected to the 53c700 card.  It's incapable of doing all of the modern
LVD functions, so we don't show any of those capabilities in its sysfs
directory.  However, we have a lot of horrible logic to generate
separate per host groupings of attributes for this.  We would be able to
use the standard sysfs group attributes *if* there were a way of
filtering them so that certain attributes didn't appear.

This patch is a first pass at adding a filter function to the group
attributes, just to see how the idea flies.  If everyone's OK with this,
I think the next thing that we might do is add bitmap functions (so
every bit in the bitmap has a name, but also might not appear) to
groups.

The kzalloc in kernel/params.c is to stop the system crashing on boot
with a bogus filter function pointer.

Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>

----

There's one final thing; if you're OK with this, can you ack it and let
me take it through the SCSI tree?  we're building functionality that
will depend on this.

Thanks,

James

Index: BUILD-2.6/fs/sysfs/group.c
===================================================================
--- BUILD-2.6.orig/fs/sysfs/group.c	2007-10-28 17:27:04.000000000 -0500
+++ BUILD-2.6/fs/sysfs/group.c	2007-10-29 00:55:49.000000000 -0500
@@ -16,25 +16,31 @@
 #include "sysfs.h"
 
 
-static void remove_files(struct sysfs_dirent *dir_sd,
+static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
 			 const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
+	int i;
 
-	for (attr = grp->attrs; *attr; attr++)
-		sysfs_hash_and_remove(dir_sd, (*attr)->name);
+	for (i = 0, attr = grp->attrs; *attr; i++, attr++)
+		if (grp->filter_show &&
+		    grp->filter_show(kobj, i))
+			sysfs_hash_and_remove(dir_sd, (*attr)->name);
 }
 
-static int create_files(struct sysfs_dirent *dir_sd,
+static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
 			const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
-	int error = 0;
+	int error = 0, i;
 
-	for (attr = grp->attrs; *attr && !error; attr++)
-		error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
+	for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++)
+		if (grp->filter_show &&
+		    grp->filter_show(kobj, i))
+			error |=
+				sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
 	if (error)
-		remove_files(dir_sd, grp);
+		remove_files(dir_sd, kobj, grp);
 	return error;
 }
 
@@ -54,7 +60,7 @@ int sysfs_create_group(struct kobject * 
 	} else
 		sd = kobj->sd;
 	sysfs_get(sd);
-	error = create_files(sd, grp);
+	error = create_files(sd, kobj, grp);
 	if (error) {
 		if (grp->name)
 			sysfs_remove_subdir(sd);
@@ -75,7 +81,7 @@ void sysfs_remove_group(struct kobject *
 	} else
 		sd = sysfs_get(dir_sd);
 
-	remove_files(sd, grp);
+	remove_files(sd, kobj, grp);
 	if (grp->name)
 		sysfs_remove_subdir(sd);
 
Index: BUILD-2.6/include/linux/sysfs.h
===================================================================
--- BUILD-2.6.orig/include/linux/sysfs.h	2007-10-28 17:20:06.000000000 -0500
+++ BUILD-2.6/include/linux/sysfs.h	2007-10-29 00:37:18.000000000 -0500
@@ -32,6 +32,7 @@ struct attribute {
 
 struct attribute_group {
 	const char		*name;
+	int			(*filter_show)(struct kobject *, int);
 	struct attribute	**attrs;
 };
 
Index: BUILD-2.6/kernel/params.c
===================================================================
--- BUILD-2.6.orig/kernel/params.c	2007-10-29 01:18:43.000000000 -0500
+++ BUILD-2.6/kernel/params.c	2007-10-29 01:18:49.000000000 -0500
@@ -472,7 +472,7 @@ param_sysfs_setup(struct module_kobject 
 			sizeof(mp->grp.attrs[0]));
 	size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]);
 
-	mp = kmalloc(size[0] + size[1], GFP_KERNEL);
+	mp = kzalloc(size[0] + size[1], GFP_KERNEL);
 	if (!mp)
 		return ERR_PTR(-ENOMEM);
 


-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 11:16 am)
Re: [PATCH] sysfs: add filter function to groups, Kay Sievers, (Mon Oct 29, 12:54 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 12:57 pm)
Re: [PATCH] sysfs: add filter function to groups, Stefan Richter, (Mon Oct 29, 1:58 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Tue Oct 30, 2:25 pm)
Re: [PATCH] sysfs: add filter function to groups, Greg KH, (Tue Oct 30, 11:55 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Wed Oct 31, 10:38 am)
Re: [PATCH] sysfs: add filter function to groups, Greg KH, (Wed Oct 31, 1:29 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Sun Nov 4, 10:12 am)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Wed Oct 31, 5:41 am)
Re: [PATCH] sysfs: add filter function to groups, Stefan Richter, (Wed Oct 31, 5:52 am)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Wed Oct 31, 6:20 am)
Re: [PATCH] sysfs: add filter function to groups, Stefan Richter, (Wed Oct 31, 6:37 am)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Wed Oct 31, 8:19 am)
Re: [PATCH] sysfs: add filter function to groups, Mark M. Hoffman, (Tue Oct 30, 8:40 pm)
Re: [PATCH] sysfs: add filter function to groups, Kay Sievers, (Tue Oct 30, 10:01 pm)
Re: [PATCH] sysfs: add filter function to groups, Mark M. Hoffman, (Wed Oct 31, 7:28 am)
Re: [PATCH] sysfs: add filter function to groups, Kay Sievers, (Tue Oct 30, 3:47 pm)
Re: [PATCH] sysfs: add filter function to groups, Stefan Richter, (Tue Oct 30, 3:31 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 2:12 pm)
Re: [PATCH] sysfs: add filter function to groups, Kay Sievers, (Mon Oct 29, 1:27 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 1:28 pm)
Re: [PATCH] sysfs: add filter function to groups, Kay Sievers, (Mon Oct 29, 1:43 pm)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Mon Oct 29, 1:18 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 1:24 pm)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Tue Oct 30, 4:55 am)
Re: [PATCH] sysfs: add filter function to groups, Jeff Garzik, (Mon Oct 29, 1:27 pm)
Re: [PATCH] sysfs: add filter function to groups, James Bottomley, (Mon Oct 29, 1:29 pm)
Re: [PATCH] sysfs: add filter function to groups, Cornelia Huck, (Tue Oct 30, 5:00 am)