linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Perez-Gonzalez, Inaky" <inaky.perez-gonzalez@intel.com>
To: "'Kernel Mailing List'" <linux-kernel@vger.kernel.org>
Subject: [PATCH] Add generic file support to sysfs ...
Date: Thu, 24 Apr 2003 21:20:27 -0700	[thread overview]
Message-ID: <A46BBDB345A7D5118EC90002A5072C780C592420@orsmsx116.jf.intel.com> (raw)

[-- 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_ */

                 reply	other threads:[~2003-04-25  4:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A46BBDB345A7D5118EC90002A5072C780C592420@orsmsx116.jf.intel.com \
    --to=inaky.perez-gonzalez@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).