linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add generic file support to sysfs ...
@ 2003-04-25  4:20 Perez-Gonzalez, Inaky
  0 siblings, 0 replies; only message in thread
From: Perez-Gonzalez, Inaky @ 2003-04-25  4:20 UTC (permalink / raw)
  To: 'Kernel Mailing List'

[-- Attachment #1: Type: text/plain, Size: 4163 bytes --]


Hi Patrick

How opposed would you be to something like this? [see inlined patch below]

Basically I am adding the ability to create short of an "attribute"
that has specific file_operations and a private pointer. The struct
generic_file contains that. Once any of the fops is called, from
the file->f_dentry->d_fsdata it is possible to get to the 'struct
generic_file', from where the 'private' pointer can be obtained
for whatever purpose.

[as a matter of fact, I could just put the 'generic_file->private'
ptr in f_dentry->d_fsdata and things would be the same].

Usage for this: I am working in this 'kue' silly thing for event
delivery, and I wanted to be able to have a /sysfs/events/ directory,
and then, on there, different files for different message queues.

Some other people around here also voiced that it'd be interesting
to have it.

Only issue I see right now is that it is left to the fops to 
do kobject_{get,put}() management of the parent. Maybe they could be 
wrapped in tin foil, do the get(), call the actual method, do the put()
and return the obtained value ... but I am not sure this is really
needed.

Need to clean up this patch, though, this is just a draft. If 
you are ok, I want to add some doc in Documentation/ and whatever
else you/anyone else might need/want.

If not ok ... why? :)

Thanks,

PS: Patch is vs. 2.5.66 ... should patch fine in others as long
as the bin support is there already.

diff -u -r1.1.1.2 -r1.1.1.2.4.1
--- fs/sysfs/Makefile	8 Mar 2003 01:31:15 -0000	1.1.1.2
+++ fs/sysfs/Makefile	25 Apr 2003 04:03:27 -0000	1.1.1.2.4.1
@@ -2,4 +2,5 @@
 # Makefile for the sysfs virtual filesystem
 #
 
-obj-y		:= inode.o file.o dir.o symlink.o mount.o bin.o
+obj-y		:= inode.o file.o dir.o symlink.o mount.o bin.o gfile.o
+
diff -N fs/sysfs/gfile.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ fs/sysfs/gfile.c	25 Apr 2003 04:03:52 -0000	1.1.2.1
@@ -0,0 +1,62 @@
+/*
+ * gfile.c - general file operations for sysfs.
+ */
+
+#include <linux/fs.h>
+#include <linux/kobject.h>
+
+#include "sysfs.h"
+
+
+/**
+ *	sysfs_create_generic_file - create generic file for object.
+ *	@kobj:	object.
+ *	@gfile:	generic file descriptor.
+ *
+ */
+
+int sysfs_create_generic_file(struct kobject * kobj, struct generic_file *
gfile)
+{
+	struct dentry * dentry;
+	struct dentry * parent;
+	int error = 0;
+
+	if (!kobj || !gfile)
+		return -EINVAL;
+
+	parent = kobj->dentry;
+
+	down(&parent->d_inode->i_sem);
+	dentry = sysfs_get_dentry(parent,gfile->attr.name);
+	if (!IS_ERR(dentry)) {
+		dentry->d_fsdata = (void *)gfile;
+		error = sysfs_create(dentry,
+				     (gfile->attr.mode & S_IALLUGO) |
S_IFREG,
+				     NULL);
+		if (!error) {
+			dentry->d_inode->i_size = 0;
+			dentry->d_inode->i_fop = &gfile->fops;
+		}
+	} else
+		error = PTR_ERR(dentry);
+	up(&parent->d_inode->i_sem);
+	return error;
+}
+
+
+/**
+ *	sysfs_remove_generic_file - remove generic file for object.
+ *	@kobj:	object.
+ *	@gfile:	generic file descriptor.
+ *
+ */
+
+int sysfs_remove_generic_file (struct kobject * kobj,
+			       struct generic_file *gfile)
+{
+	sysfs_hash_and_remove(kobj->dentry,gfile->attr.name);
+	return 0;
+}
+
+EXPORT_SYMBOL(sysfs_create_generic_file);
+EXPORT_SYMBOL(sysfs_remove_generic_file);
diff -u -r1.1.1.2 -r1.1.1.2.4.1
--- include/linux/sysfs.h	8 Mar 2003 01:31:18 -0000	1.1.1.2
+++ include/linux/sysfs.h	25 Apr 2003 04:04:42 -0000	1.1.1.2.4.1
@@ -30,6 +30,12 @@
 	ssize_t (*write)(struct kobject *, struct sysfs_bin_buffer *);
 };
 
+struct generic_file {
+	struct attribute         attr;
+	struct file_operations * fops;
+	void *                   private;
+};
+
 struct sysfs_ops {
 	ssize_t	(*show)(struct kobject *, struct attribute *,char *);
 	ssize_t	(*store)(struct kobject *,struct attribute *,const char *,
size_t);
@@ -55,5 +61,13 @@
 
 extern void
 sysfs_remove_link(struct kobject *, char * name);
+
+extern int
+sysfs_create_generic_file (struct kobject *,
+			   struct generic_file *);
+
+extern int
+sysfs_remove_generic_file (struct kobject *,
+			   struct generic_file *);
 
 #endif /* _SYSFS_H_ */

Iñaky Pérez-González -- Not speaking for Intel -- all opinions are my own
(and my fault)


[-- Attachment #2: gfile-2.5.66-draft.patch --]
[-- Type: application/octet-stream, Size: 2746 bytes --]

diff -u -r1.1.1.2 -r1.1.1.2.4.1
--- fs/sysfs/Makefile	8 Mar 2003 01:31:15 -0000	1.1.1.2
+++ fs/sysfs/Makefile	25 Apr 2003 04:03:27 -0000	1.1.1.2.4.1
@@ -2,4 +2,5 @@
 # Makefile for the sysfs virtual filesystem
 #
 
-obj-y		:= inode.o file.o dir.o symlink.o mount.o bin.o
+obj-y		:= inode.o file.o dir.o symlink.o mount.o bin.o gfile.o
+
diff -N fs/sysfs/gfile.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ fs/sysfs/gfile.c	25 Apr 2003 04:03:52 -0000	1.1.2.1
@@ -0,0 +1,62 @@
+/*
+ * gfile.c - general file operations for sysfs.
+ */
+
+#include <linux/fs.h>
+#include <linux/kobject.h>
+
+#include "sysfs.h"
+
+
+/**
+ *	sysfs_create_generic_file - create generic file for object.
+ *	@kobj:	object.
+ *	@gfile:	generic file descriptor.
+ *
+ */
+
+int sysfs_create_generic_file(struct kobject * kobj, struct generic_file * gfile)
+{
+	struct dentry * dentry;
+	struct dentry * parent;
+	int error = 0;
+
+	if (!kobj || !gfile)
+		return -EINVAL;
+
+	parent = kobj->dentry;
+
+	down(&parent->d_inode->i_sem);
+	dentry = sysfs_get_dentry(parent,gfile->attr.name);
+	if (!IS_ERR(dentry)) {
+		dentry->d_fsdata = (void *)gfile;
+		error = sysfs_create(dentry,
+				     (gfile->attr.mode & S_IALLUGO) | S_IFREG,
+				     NULL);
+		if (!error) {
+			dentry->d_inode->i_size = 0;
+			dentry->d_inode->i_fop = &gfile->fops;
+		}
+	} else
+		error = PTR_ERR(dentry);
+	up(&parent->d_inode->i_sem);
+	return error;
+}
+
+
+/**
+ *	sysfs_remove_generic_file - remove generic file for object.
+ *	@kobj:	object.
+ *	@gfile:	generic file descriptor.
+ *
+ */
+
+int sysfs_remove_generic_file (struct kobject * kobj,
+			       struct generic_file *gfile)
+{
+	sysfs_hash_and_remove(kobj->dentry,gfile->attr.name);
+	return 0;
+}
+
+EXPORT_SYMBOL(sysfs_create_generic_file);
+EXPORT_SYMBOL(sysfs_remove_generic_file);
diff -u -r1.1.1.2 -r1.1.1.2.4.1
--- include/linux/sysfs.h	8 Mar 2003 01:31:18 -0000	1.1.1.2
+++ include/linux/sysfs.h	25 Apr 2003 04:04:42 -0000	1.1.1.2.4.1
@@ -30,6 +30,12 @@
 	ssize_t (*write)(struct kobject *, struct sysfs_bin_buffer *);
 };
 
+struct generic_file {
+	struct attribute         attr;
+	struct file_operations * fops;
+	void *                   private;
+};
+
 struct sysfs_ops {
 	ssize_t	(*show)(struct kobject *, struct attribute *,char *);
 	ssize_t	(*store)(struct kobject *,struct attribute *,const char *, size_t);
@@ -55,5 +61,13 @@
 
 extern void
 sysfs_remove_link(struct kobject *, char * name);
+
+extern int
+sysfs_create_generic_file (struct kobject *,
+			   struct generic_file *);
+
+extern int
+sysfs_remove_generic_file (struct kobject *,
+			   struct generic_file *);
 
 #endif /* _SYSFS_H_ */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-04-25  4:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-25  4:20 [PATCH] Add generic file support to sysfs Perez-Gonzalez, Inaky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).