Skip to contentSkip to conversation

Allow sysfs attribute files to be pollable.

PATCH-RESEND NeilBrown 1 participant 1 message Mar 20, 2006 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

NeilBrown

author
#1 lore raw
In reply to a message not in the archive

This is a resend of my 'make sysfs pollable' patch, which was dropped from -mm for independant reasons.

Changes:
Previously there was an original patch and a fix. Now there is just
one patch.
The changelog mentions .../md/sync_action, but that bit was missing
from the patch before, it is now present.
It has been suggested that it is unfortunate that poll on an attribute
that doesn't support polling will block forever. This may be unfortunate, but there is no real alternative as poll and select to not support an error status of 'one of those fds cannot be polled'. A comment above sysfs_poll explains the situation in a little more detail.

NeilBrown

### Comments for Changeset

It works like this:
Open the file
Read all the contents.
Call poll requesting POLLERR or POLLPRI (so select/exceptfds works) When poll returns,
close the file and go to top of loop.
or lseek to start of file and go back to the 'read'.

Events are signaled by an object manager calling
sysfs_notify(kobj, dir, attr);

If the dir is non-NULL, it is used to find a subdirectory which contains the attribute (presumably created by sysfs_create_group).

This has a cost of one int per attribute, one wait_queuehead per kobject, one int per open file.

The name "sysfs_notify" may be confused with the inotify functionality. Maybe it would be nice to support inotify for sysfs attributes as well?

This patch also uses sysfs_notify to allow /sys/block/md*/md/sync_action to be pollable

### Diffstat output

 ./drivers/md/md.c         |    1 
 ./fs/sysfs/dir.c          |    1 
 ./fs/sysfs/file.c         |   76 ++++++++++++++++++++++++++++++++++++++++++++++
 ./fs/sysfs/sysfs.h        |    1 
 ./include/linux/kobject.h |    2 +
 ./include/linux/sysfs.h   |    6 +++
 ./lib/kobject.c           |    1 
 7 files changed, 88 insertions(+)
drivers/md/md.c renamed from drivers/md/md.c~current~ +1 -0
diff ./drivers/md/md.c~current~ ./drivers/md/md.c
@@ -163,6 +163,7 @@ void md_new_event(mddev_t *mddev)
 {
 	atomic_inc(&md_event_count);
 	wake_up(&md_event_waiters);
+	sysfs_notify(&mddev->kobj, NULL, "sync_action");
 }
 EXPORT_SYMBOL_GPL(md_new_event);
 /*
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~current~ +1 -0
diff ./fs/sysfs/dir.c~current~ ./fs/sysfs/dir.c
@@ -43,6 +43,7 @@ static struct sysfs_dirent * sysfs_new_d
 
 	memset(sd, 0, sizeof(*sd));
 	atomic_set(&sd->s_count, 1);
+	atomic_set(&sd->s_event, 0);
 	INIT_LIST_HEAD(&sd->s_children);
 	list_add(&sd->s_sibling, &parent_sd->s_children);
 	sd->s_element = element;
fs/sysfs/file.c renamed from fs/sysfs/file.c~current~ +76 -0

112 lines not on this page · raw message

fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~current~ +1 -0
diff ./fs/sysfs/sysfs.h~current~ ./fs/sysfs/sysfs.h
@@ -11,6 +11,7 @@ extern int sysfs_make_dirent(struct sysf
 
 extern int sysfs_add_file(struct dentry *, const struct attribute *, int);
 extern void sysfs_hash_and_remove(struct dentry * dir, const char * name);
+extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
 
 extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
 extern void sysfs_remove_subdir(struct dentry *);
include/linux/kobject.h renamed from include/linux/kobject.h~current~ +2 -0
diff ./include/linux/kobject.h~current~ ./include/linux/kobject.h
@@ -24,6 +24,7 @@
 #include <linux/rwsem.h>
 #include <linux/kref.h>
 #include <linux/kernel.h>
+#include <linux/wait.h>
 #include <asm/atomic.h>
 
 #define KOBJ_NAME_LEN			20
@@ -56,6 +57,7 @@ struct kobject {
 	struct kset		* kset;
 	struct kobj_type	* ktype;
 	struct dentry		* dentry;
+	wait_queue_head_t	poll;
 };
 
 extern int kobject_set_name(struct kobject *, const char *, ...)
include/linux/sysfs.h renamed from include/linux/sysfs.h~current~ +6 -0
diff ./include/linux/sysfs.h~current~ ./include/linux/sysfs.h
@@ -103,6 +103,7 @@ struct sysfs_dirent {
 	umode_t			s_mode;
 	struct dentry		*s_dentry;
 	struct iattr		*s_iattr;
+	atomic_t		s_event;
 };
 
 #define SYSFS_ROOT		0x0001
@@ -149,6 +150,7 @@ int sysfs_create_group(struct kobject *,
 void sysfs_remove_group(struct kobject *, const struct attribute_group *);
 
 void sysfs_printk_last_file(void);
+void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
 #else /* CONFIG_SYSFS */
 
@@ -227,5 +229,9 @@ static inline void sysfs_printk_last_fil
 {
 }
 
+static inline void sysfs_notify(struct kobject * k, char *dir, char *attr)
+{
+}
+
 #endif /* CONFIG_SYSFS */
 #endif /* _SYSFS_H_ */
lib/kobject.c renamed from lib/kobject.c~current~ +1 -0
diff ./lib/kobject.c~current~ ./lib/kobject.c
@@ -128,6 +128,7 @@ void kobject_init(struct kobject * kobj)
 {
 	kref_init(&kobj->kref);
 	INIT_LIST_HEAD(&kobj->entry);
+	init_waitqueue_head(&kobj->poll);
 	kobj->kset = kset_get(kobj->kset);
 }