[Ocfs2-commits] jlbec commits r1782 - in trunk: . usysfs
svn-commits at oss.oracle.com
svn-commits at oss.oracle.com
Mon Jan 17 19:36:26 CST 2005
Author: jlbec
Date: 2005-01-17 19:36:24 -0600 (Mon, 17 Jan 2005)
New Revision: 1782
Added:
trunk/usysfs/
trunk/usysfs/Kbuild
trunk/usysfs/Makefile
trunk/usysfs/bin.c
trunk/usysfs/bobtest.c
trunk/usysfs/dir.c
trunk/usysfs/file.c
trunk/usysfs/group.c
trunk/usysfs/inode.c
trunk/usysfs/mount.c
trunk/usysfs/symlink.c
trunk/usysfs/usysfs.h
trunk/usysfs/usysfs_internal.h
Modified:
trunk/Makefile
Log:
o Add usysfs sources to source control. They don't build on 2.4 yet
(the Makefiles enforce this).
Modified: trunk/Makefile
===================================================================
--- trunk/Makefile 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/Makefile 2005-01-18 01:36:24 UTC (rev 1782)
@@ -2,7 +2,7 @@
include $(TOPDIR)/Preamble.make
-SUBDIRS = cluster src docs patches vendor
+SUBDIRS = usysfs cluster src docs patches vendor
DIST_FILES = \
COPYING \
Added: trunk/usysfs/Kbuild
===================================================================
--- trunk/usysfs/Kbuild 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/Kbuild 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,9 @@
+#
+# Makefile for the usysfs virtual filesystem
+#
+
+STAMP_DIR := $(M)
+
+obj-m := usysfs.o bobtest.o
+usysfs-objs := inode.o file.o dir.o symlink.o mount.o bin.o
+
Added: trunk/usysfs/Makefile
===================================================================
--- trunk/usysfs/Makefile 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/Makefile 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,63 @@
+
+ifeq ($(KERNELRELEASE),)
+TOPDIR = ..
+
+include $(TOPDIR)/Preamble.make
+else
+# We are included by kbuild, and the is pre-"Kbuild"-files.
+
+USYSFS_SRC_DIR := $(M)
+
+include $(USYSFS_SRC_DIR)/../Config.make
+endif
+
+HEADERS = usysfs.h usysfs_internal.h
+SOURCES = bin.c dir.c file.c inode.c mount.c symlink.c
+OBJECTS = $(subst .c,.o,$(SOURCES))
+
+ifeq ($(KERNELRELEASE),)
+#
+# Called from a regular "make". We need to forward to kbuild.
+#
+
+# for now, no 2.4
+ifeq ($(VERSION).$(PATCHLEVEL),2.4)
+ALL_RULES = build-usysfs
+
+CLEAN_RULES = clean-usysfs
+
+build-usysfs:
+ $(MAKE) -C $(KERNELDIR) M=$(CURDIR) modules
+
+clean-usysfs:
+ $(MAKE) -C $(KERNELDIR) M=$(CURDIR) clean
+
+INSTALL_MODULE = usysfs.ko
+INSTALL_RULES = install-usysfs
+
+install-usysfs: $(INSTALL_MODULE)
+ $(TOPDIR)/mkinstaldirs $(DESTDIR)$(MODULEDIR)
+ $(INSTALL_DATA) $< $(DESTDIR)$(MODULEDIR)/$<
+endif
+
+DIST_FILES = $(SOURCES) $(HEADERS)
+
+include $(TOPDIR)/Postamble.make
+
+else
+ifeq ($(VERSION).$(PATCHLEVEL),2.4)
+
+O_TARGET := usysfs.o
+
+obj-y := $(OBJECTS)
+obj-m := $(O_TARGET)
+
+include $(TOPDIR)/Rules.make
+else
+#
+# Called from kbuild in a kernel that predates Kbuild files.
+#
+
+include $(USYSFS_SRC_DIR)/Kbuild
+endif
+endif
Added: trunk/usysfs/bin.c
===================================================================
--- trunk/usysfs/bin.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/bin.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,182 @@
+/*
+ * bin.c - binary file operations for usysfs.
+ */
+
+#undef DEBUG
+
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/kobject.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include <asm/uaccess.h>
+
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+static int
+fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
+{
+ struct bin_attribute * attr = to_bin_attr(dentry);
+ struct kobject * kobj = to_kobj(dentry->d_parent);
+
+ return attr->read(kobj, buffer, off, count);
+}
+
+static ssize_t
+read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
+{
+ char *buffer = file->private_data;
+ struct dentry *dentry = file->f_dentry;
+ int size = dentry->d_inode->i_size;
+ loff_t offs = *off;
+ int ret;
+
+ if (count > PAGE_SIZE)
+ count = PAGE_SIZE;
+
+ if (size) {
+ if (offs > size)
+ return 0;
+ if (offs + count > size)
+ count = size - offs;
+ }
+
+ ret = fill_read(dentry, buffer, offs, count);
+ if (ret < 0)
+ return ret;
+ count = ret;
+
+ if (copy_to_user(userbuf, buffer, count))
+ return -EFAULT;
+
+ pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
+
+ *off = offs + count;
+
+ return count;
+}
+
+static int
+flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
+{
+ struct bin_attribute *attr = to_bin_attr(dentry);
+ struct kobject *kobj = to_kobj(dentry->d_parent);
+
+ return attr->write(kobj, buffer, offset, count);
+}
+
+static ssize_t write(struct file * file, const char __user * userbuf,
+ size_t count, loff_t * off)
+{
+ char *buffer = file->private_data;
+ struct dentry *dentry = file->f_dentry;
+ int size = dentry->d_inode->i_size;
+ loff_t offs = *off;
+
+ if (count > PAGE_SIZE)
+ count = PAGE_SIZE;
+ if (size) {
+ if (offs > size)
+ return 0;
+ if (offs + count > size)
+ count = size - offs;
+ }
+
+ if (copy_from_user(buffer, userbuf, count))
+ return -EFAULT;
+
+ count = flush_write(dentry, buffer, offs, count);
+ if (count > 0)
+ *off = offs + count;
+ return count;
+}
+
+static int open(struct inode * inode, struct file * file)
+{
+ struct kobject *kobj = usysfs_get_kobject(file->f_dentry->d_parent);
+ struct bin_attribute * attr = to_bin_attr(file->f_dentry);
+ int error = -EINVAL;
+
+ if (!kobj || !attr)
+ goto Done;
+
+ /* Grab the module reference for this attribute if we have one */
+ error = -ENODEV;
+ if (!try_module_get(attr->attr.owner))
+ goto Done;
+
+ error = -EACCES;
+ if ((file->f_mode & FMODE_WRITE) && !attr->write)
+ goto Error;
+ if ((file->f_mode & FMODE_READ) && !attr->read)
+ goto Error;
+
+ error = -ENOMEM;
+ file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!file->private_data)
+ goto Error;
+
+ error = 0;
+ goto Done;
+
+ Error:
+ module_put(attr->attr.owner);
+ Done:
+ if (error && kobj)
+ kobject_put(kobj);
+ return error;
+}
+
+static int release(struct inode * inode, struct file * file)
+{
+ struct kobject * kobj = to_kobj(file->f_dentry->d_parent);
+ struct bin_attribute * attr = to_bin_attr(file->f_dentry);
+ u8 * buffer = file->private_data;
+
+ if (kobj)
+ kobject_put(kobj);
+ module_put(attr->attr.owner);
+ kfree(buffer);
+ return 0;
+}
+
+struct file_operations bin_fops = {
+ .read = read,
+ .write = write,
+ .llseek = generic_file_llseek,
+ .open = open,
+ .release = release,
+};
+
+/**
+ * usysfs_create_bin_file - create binary file for object.
+ * @kobj: object.
+ * @attr: attribute descriptor.
+ *
+ */
+
+int usysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
+{
+ BUG_ON(!kobj || !kobj->dentry || !attr);
+
+ return usysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
+}
+
+
+/**
+ * usysfs_remove_bin_file - remove binary file for object.
+ * @kobj: object.
+ * @attr: attribute descriptor.
+ *
+ */
+
+int usysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
+{
+ usysfs_hash_and_remove(kobj->dentry,attr->attr.name);
+ return 0;
+}
+
+EXPORT_SYMBOL_GPL(usysfs_create_bin_file);
+EXPORT_SYMBOL_GPL(usysfs_remove_bin_file);
Added: trunk/usysfs/bobtest.c
===================================================================
--- trunk/usysfs/bobtest.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/bobtest.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,231 @@
+/*
+ * vim: noexpandtab ts=8 sts=0 sw=8:
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kobject.h>
+#include "usysfs.h"
+
+
+struct bob {
+ struct kobject kobj;
+ int showme;
+ int storeme;
+};
+
+struct bob_attribute {
+ struct attribute attr;
+ ssize_t (*show)(struct bob *, char *);
+ ssize_t (*store)(struct bob *, const char *, size_t);
+};
+
+static inline struct bob *to_bob(struct kobject *kobj)
+{
+ return kobj ? container_of(kobj, struct bob, kobj) : NULL;
+}
+
+static ssize_t bob_showme_read(struct bob *bob, char *page)
+{
+ ssize_t pos;
+
+ pos = sprintf(page, "%d\n", bob->showme);
+ bob->showme++;
+
+ return pos;
+}
+
+static ssize_t bob_storeme_read(struct bob *bob, char *page)
+{
+ return sprintf(page, "%d\n", bob->storeme);
+}
+
+static ssize_t bob_storeme_write(struct bob *bob, const char *page,
+ size_t count)
+{
+ unsigned long tmp;
+ char *p = (char *) page;
+
+ tmp = simple_strtoul(p, &p, 10);
+ if (!p || (*p && (*p != '\n')))
+ return -EINVAL;
+
+ if (tmp > INT_MAX)
+ return -ERANGE;
+
+ bob->storeme = tmp;
+
+ return count;
+}
+
+static struct bob_attribute bob_attr_showme = {
+ .attr = { .name = "showme", .mode = S_IRUGO },
+ .show = bob_showme_read,
+};
+static struct bob_attribute bob_attr_storeme = {
+ .attr = { .name = "storeme", .mode = S_IRUGO | S_IWUSR },
+ .show = bob_storeme_read,
+ .store = bob_storeme_write,
+};
+static struct attribute *bob_attrs[] = {
+ &bob_attr_showme.attr,
+ &bob_attr_storeme.attr,
+ NULL,
+};
+
+static ssize_t bob_attr_show(struct kobject *kobj,
+ struct attribute *attr,
+ char *page)
+{
+ struct bob *bob = to_bob(kobj);
+ struct bob_attribute *bob_attr =
+ container_of(attr, struct bob_attribute, attr);
+ ssize_t ret = 0;
+
+ if (bob_attr->show)
+ ret = bob_attr->show(bob, page);
+ return ret;
+}
+
+static ssize_t bob_attr_store(struct kobject *kobj,
+ struct attribute *attr,
+ const char *page, size_t count)
+{
+ struct bob *bob = to_bob(kobj);
+ struct bob_attribute *bob_attr =
+ container_of(attr, struct bob_attribute, attr);
+ ssize_t ret = -EINVAL;
+
+ if (bob_attr->store)
+ ret = bob_attr->store(bob, page, count);
+ return ret;
+}
+
+struct sysfs_ops bob_sysfs_ops = {
+ .show = &bob_attr_show,
+ .store = &bob_attr_store,
+};
+
+static struct ukobj_type uktype_bob = {
+ .ktype = {
+ .sysfs_ops = &bob_sysfs_ops,
+ .default_attrs = bob_attrs,
+ },
+};
+
+
+struct bobset {
+ struct kset kset;
+ int n_bobs;
+};
+
+struct bobset_attribute {
+ struct attribute attr;
+ ssize_t (*show)(struct bobset *, char *);
+};
+
+static inline struct bobset *to_bobset(struct kset *kset)
+{
+ return kset ? container_of(kset, struct bobset, kset) : NULL;
+}
+
+static ssize_t bobset_attr_show(struct kobject *kobj,
+ struct attribute *attr,
+ char *page)
+{
+ struct bobset *bobset = to_bobset(to_kset(kobj));
+ struct bobset_attribute *bobset_attr =
+ container_of(attr, struct bobset_attribute, attr);
+ ssize_t ret = 0;
+
+ if (bobset_attr->show)
+ ret = bobset_attr->show(bobset, page);
+ return ret;
+}
+
+static struct sysfs_ops bobset_sysfs_ops = {
+ .show = &bobset_attr_show,
+};
+
+static ssize_t bobset_bobs_read(struct bobset *bobset, char *page)
+{
+ return sprintf(page, "%d Bobs love you\n", bobset->n_bobs);
+}
+
+static struct bobset_attribute bobset_attr_bobs = {
+ .attr = { .name = "bobs", .mode = S_IRUGO },
+ .show = bobset_bobs_read
+};
+
+static struct attribute *bobset_attrs[] = {
+ &bobset_attr_bobs.attr,
+ NULL,
+};
+
+static struct kobject *make_bob(struct kset *kset, const char *name)
+{
+ struct bobset *bobset = to_bobset(kset);
+ struct bob *bob;
+
+ bob = kmalloc(sizeof(struct bob), GFP_KERNEL);
+ if (!bob)
+ return NULL;
+
+ memset(bob, 0, sizeof(struct bob));
+
+ strcpy(bob->kobj.name, name);
+ bob->kobj.k_name = bob->kobj.name;
+ bob->kobj.ktype = &uktype_bob.ktype;
+ kobject_init(&bob->kobj);
+
+ bob->showme = 0;
+ bob->storeme = 0;
+ bobset->n_bobs++;
+
+ return &bob->kobj;
+}
+
+static struct ukobj_type uktype_bobset = {
+ .ktype = {
+ .sysfs_ops = &bobset_sysfs_ops,
+ .default_attrs = bobset_attrs,
+ },
+ .make_object = make_bob,
+};
+
+static struct bobset bobset = {
+ .kset = {
+ .kobj = {
+ .name = "bobset",
+ .ktype = &uktype_bobset.ktype,
+ },
+ },
+ .n_bobs = 0,
+};
+
+void kset_init(struct kset *k)
+{
+ kobject_init(&k->kobj);
+ INIT_LIST_HEAD(&k->list);
+}
+
+static int __init bobtest_init(void)
+{
+ int ret;
+
+ kset_init(&bobset.kset);
+ ret = usysfs_register_subsystem(&bobset.kset);
+ if (ret)
+ printk(KERN_ERR "bobtest: Registration returned %d\n", ret);
+
+ return ret;
+}
+
+static void __exit bobtest_exit(void)
+{
+ usysfs_unregister_subsystem(&bobset.kset);
+}
+
+module_init(bobtest_init);
+module_exit(bobtest_exit);
+MODULE_LICENSE("GPL");
Added: trunk/usysfs/dir.c
===================================================================
--- trunk/usysfs/dir.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/dir.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,612 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * dir.c - Operations for usysfs directories.
+ *
+ * Changes for usysfs Copyright (c) 2005 Oracle Corporation
+ */
+
+#undef DEBUG
+
+#include <linux/fs.h>
+#include <linux/mount.h>
+#include <linux/module.h>
+#include <linux/kobject.h>
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+DECLARE_RWSEM(usysfs_rename_sem);
+DECLARE_MUTEX(usysfs_linkage_sem);
+
+static void usysfs_d_iput(struct dentry * dentry, struct inode * inode)
+{
+ struct usysfs_dirent * sd = dentry->d_fsdata;
+
+ if (sd) {
+ BUG_ON(sd->s_dentry != dentry);
+ sd->s_dentry = NULL;
+ usysfs_put(sd);
+ }
+ iput(inode);
+}
+
+static struct dentry_operations usysfs_dentry_ops = {
+ .d_iput = usysfs_d_iput,
+};
+
+/*
+ * Allocates a new usysfs_dirent and links it to the parent usysfs_dirent
+ */
+static struct usysfs_dirent * usysfs_new_dirent(struct usysfs_dirent * parent_sd,
+ void * element)
+{
+ struct usysfs_dirent * sd;
+
+ sd = kmalloc(sizeof(*sd), GFP_KERNEL);
+ if (!sd)
+ return NULL;
+
+ memset(sd, 0, sizeof(*sd));
+ atomic_set(&sd->s_count, 1);
+ INIT_LIST_HEAD(&sd->s_children);
+ list_add(&sd->s_sibling, &parent_sd->s_children);
+ sd->s_element = element;
+
+ return sd;
+}
+
+int usysfs_make_dirent(struct usysfs_dirent * parent_sd, struct dentry * dentry,
+ void * element, umode_t mode, int type)
+{
+ struct usysfs_dirent * sd;
+
+ sd = usysfs_new_dirent(parent_sd, element);
+ if (!sd)
+ return -ENOMEM;
+
+ sd->s_mode = mode;
+ sd->s_type = type;
+ sd->s_dentry = dentry;
+ if (dentry) {
+ dentry->d_fsdata = usysfs_get(sd);
+ dentry->d_op = &usysfs_dentry_ops;
+ }
+
+ return 0;
+}
+
+static int init_dir(struct inode * inode)
+{
+ inode->i_op = &usysfs_dir_inode_operations;
+ inode->i_fop = &usysfs_dir_operations;
+
+ /* directory inodes start off with i_nlink == 2 (for "." entry) */
+ inode->i_nlink++;
+ return 0;
+}
+
+static int init_file(struct inode * inode)
+{
+ inode->i_size = PAGE_SIZE;
+ inode->i_fop = &usysfs_file_operations;
+ return 0;
+}
+
+static int init_symlink(struct inode * inode)
+{
+ inode->i_op = &usysfs_symlink_inode_operations;
+ return 0;
+}
+
+static int create_dir(struct kobject * k, struct dentry * p,
+ struct dentry * d)
+{
+ int error;
+ umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
+
+ error = usysfs_create(d, mode, init_dir);
+ if (!error) {
+ error = usysfs_make_dirent(p->d_fsdata, d, k, mode,
+ SYSFS_DIR);
+ if (!error) {
+ p->d_inode->i_nlink++;
+ (d)->d_op = &usysfs_dentry_ops;
+ }
+ }
+ return error;
+}
+
+
+/**
+ * usysfs_create_dir - create a directory for an object.
+ * @parent: parent parent object.
+ * @kobj: object we're creating directory for.
+ */
+
+static int usysfs_create_dir(struct kobject * kobj, struct dentry *dentry)
+{
+ struct dentry * parent;
+ int error = 0;
+
+ BUG_ON(!kobj);
+
+ if (kobj->parent)
+ parent = kobj->parent->dentry;
+ else if (usysfs_mount && usysfs_mount->mnt_sb)
+ parent = usysfs_mount->mnt_sb->s_root;
+ else
+ return -EFAULT;
+
+ error = create_dir(kobj,parent,dentry);
+ if (!error)
+ kobj->dentry = dentry;
+ return error;
+}
+
+/* attaches attribute's usysfs_dirent to the dentry corresponding to the
+ * attribute file
+ */
+static int usysfs_attach_attr(struct usysfs_dirent * sd, struct dentry * dentry)
+{
+ struct attribute * attr = NULL;
+ struct bin_attribute * bin_attr = NULL;
+ int (* init) (struct inode *) = NULL;
+ int error = 0;
+
+ if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
+ bin_attr = sd->s_element;
+ attr = &bin_attr->attr;
+ } else {
+ attr = sd->s_element;
+ init = init_file;
+ }
+
+ error = usysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
+ if (error)
+ return error;
+
+ if (bin_attr) {
+ dentry->d_inode->i_size = bin_attr->size;
+ dentry->d_inode->i_fop = &bin_fops;
+ }
+ dentry->d_op = &usysfs_dentry_ops;
+ dentry->d_fsdata = usysfs_get(sd);
+ sd->s_dentry = dentry;
+ d_rehash(dentry);
+
+ return 0;
+}
+
+static int usysfs_attach_link(struct usysfs_dirent * sd, struct dentry * dentry)
+{
+ int err = 0;
+
+ err = usysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
+ if (!err) {
+ dentry->d_op = &usysfs_dentry_ops;
+ dentry->d_fsdata = usysfs_get(sd);
+ sd->s_dentry = dentry;
+ d_rehash(dentry);
+ }
+ return err;
+}
+
+static struct dentry * usysfs_lookup(struct inode *dir, struct dentry *dentry,
+ struct nameidata *nd)
+{
+ struct usysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
+ struct usysfs_dirent * sd;
+ int found = 0;
+ int err = 0;
+
+ list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
+ if (sd->s_type & SYSFS_NOT_PINNED) {
+ const unsigned char * name = usysfs_get_name(sd);
+
+ if (strcmp(name, dentry->d_name.name))
+ continue;
+
+ found = 1;
+ if (sd->s_type & SYSFS_KOBJ_LINK)
+ err = usysfs_attach_link(sd, dentry);
+ else
+ err = usysfs_attach_attr(sd, dentry);
+ break;
+ }
+ }
+
+ if (!found) {
+ /*
+ * If it doesn't exist and it isn't a NOT_PINNED item,
+ * it must be negative
+ */
+ return simple_lookup(dir, dentry, nd);
+ }
+
+ return ERR_PTR(err);
+}
+
+static int populate_dir(struct kobject *kobj)
+{
+ struct kobj_type *t = get_ktype(kobj);
+ struct attribute *attr;
+ int error = 0;
+ int i;
+
+ if (!t)
+ return -EINVAL;
+ if (t->default_attrs) {
+ for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
+ if ((error = usysfs_create_file(kobj, attr)))
+ break;
+ }
+ }
+
+ return error;
+}
+
+/*
+ * Remove an object from its kset and decrement the refcount.
+ * This is used by both rmdir() and by mkdir() on error.
+ */
+static void unlink(struct kobject *kobj)
+{
+ if (kobj->kset) {
+ down(&usysfs_linkage_sem);
+ list_del_init(&kobj->entry);
+ up(&usysfs_linkage_sem);
+ }
+ kobject_put(kobj);
+}
+
+static int usysfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+{
+ int ret;
+ struct kset *kset;
+ struct kobject *kobj;
+ struct kobject *parent_kobj;
+ struct ukobj_type *uktype;
+ char *name;
+
+ if (dentry->d_parent == usysfs_sb->s_root)
+ return -EPERM;
+
+ parent_kobj = usysfs_get_kobject(dentry->d_parent);
+ uktype = to_uktype(parent_kobj->ktype);
+
+ printk("In mkdir %s\n", parent_kobj->k_name);
+ if (!uktype || (!uktype->make_kset && !uktype->make_object)) {
+ kobject_put(parent_kobj);
+ return -EPERM; /* What lack-of-mkdir returns */
+ }
+
+ name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
+ if (!name) {
+ kobject_put(parent_kobj);
+ return -ENOMEM;
+ }
+ snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
+ if (uktype->make_kset) {
+ kset = uktype->make_kset(to_kset(parent_kobj), name);
+ kobj = &kset->kobj;
+ } else {
+ kobj = uktype->make_object(to_kset(parent_kobj), name);
+ }
+ kfree(name);
+ if (!kobj) {
+ kobject_put(parent_kobj);
+ return -ENOMEM;
+ }
+
+ kobj->kset = to_kset(parent_kobj);
+ down(&usysfs_linkage_sem);
+ list_add_tail(&kobj->entry, &kobj->kset->list);
+ up(&usysfs_linkage_sem);
+ kobj->parent = parent_kobj;
+
+ ret = usysfs_create_dir(kobj, dentry);
+ if (ret) {
+ unlink(kobj);
+ kobject_put(parent_kobj);
+ return ret;
+ }
+
+ ret = populate_dir(kobj);
+ if (ret)
+ usysfs_remove_dir(kobj);
+
+ return ret;
+}
+
+struct inode_operations usysfs_dir_inode_operations = {
+ .mkdir = usysfs_mkdir,
+ .lookup = usysfs_lookup,
+};
+
+static void remove_dir(struct dentry * d)
+{
+ struct dentry * parent = dget(d->d_parent);
+ struct usysfs_dirent * sd;
+
+ down(&parent->d_inode->i_sem);
+ d_delete(d);
+ sd = d->d_fsdata;
+ list_del_init(&sd->s_sibling);
+ usysfs_put(sd);
+ if (d->d_inode)
+ simple_rmdir(parent->d_inode,d);
+
+ pr_debug(" o %s removing done (%d)\n",d->d_name.name,
+ atomic_read(&d->d_count));
+
+ up(&parent->d_inode->i_sem);
+ dput(parent);
+}
+
+void usysfs_remove_subdir(struct dentry * d)
+{
+ remove_dir(d);
+}
+
+
+/**
+ * usysfs_remove_dir - remove an object's directory.
+ * @kobj: object.
+ *
+ * The only thing special about this is that we remove any files in
+ * the directory before we remove the directory, and we've inlined
+ * what used to be usysfs_rmdir() below, instead of calling separately.
+ */
+
+void usysfs_remove_dir(struct kobject * kobj)
+{
+ struct dentry * dentry = dget(kobj->dentry);
+ struct usysfs_dirent * parent_sd;
+ struct usysfs_dirent * sd, * tmp;
+
+ if (!dentry)
+ return;
+
+ pr_debug("usysfs %s: removing dir\n",dentry->d_name.name);
+ down(&dentry->d_inode->i_sem);
+ parent_sd = dentry->d_fsdata;
+ list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
+ if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
+ continue;
+ list_del_init(&sd->s_sibling);
+ usysfs_drop_dentry(sd, dentry);
+ usysfs_put(sd);
+ }
+ up(&dentry->d_inode->i_sem);
+
+ remove_dir(dentry);
+ /**
+ * Drop reference from dget() on entrance.
+ */
+ dput(dentry);
+}
+
+int usysfs_rename_dir(struct kobject * kobj, const char *new_name)
+{
+ int error = 0;
+ struct dentry * new_dentry, * parent;
+
+ if (!strcmp(kobject_name(kobj), new_name))
+ return -EINVAL;
+
+ if (!kobj->parent)
+ return -EINVAL;
+
+ down_write(&usysfs_rename_sem);
+ parent = kobj->parent->dentry;
+
+ down(&parent->d_inode->i_sem);
+
+ new_dentry = usysfs_get_dentry(parent, new_name);
+ if (!IS_ERR(new_dentry)) {
+ if (!new_dentry->d_inode) {
+ error = kobject_set_name(kobj, "%s", new_name);
+ if (!error) {
+ d_add(new_dentry, NULL);
+ d_move(kobj->dentry, new_dentry);
+ }
+ else
+ d_drop(new_dentry);
+ } else
+ error = -EEXIST;
+ dput(new_dentry);
+ }
+ up(&parent->d_inode->i_sem);
+ up_write(&usysfs_rename_sem);
+
+ return error;
+}
+
+static int usysfs_dir_open(struct inode *inode, struct file *file)
+{
+ struct dentry * dentry = file->f_dentry;
+ struct usysfs_dirent * parent_sd = dentry->d_fsdata;
+
+ down(&dentry->d_inode->i_sem);
+ file->private_data = usysfs_new_dirent(parent_sd, NULL);
+ up(&dentry->d_inode->i_sem);
+
+ return file->private_data ? 0 : -ENOMEM;
+
+}
+
+static int usysfs_dir_close(struct inode *inode, struct file *file)
+{
+ struct dentry * dentry = file->f_dentry;
+ struct usysfs_dirent * cursor = file->private_data;
+
+ down(&dentry->d_inode->i_sem);
+ list_del_init(&cursor->s_sibling);
+ up(&dentry->d_inode->i_sem);
+
+ release_usysfs_dirent(cursor);
+
+ return 0;
+}
+
+/* Relationship between s_mode and the DT_xxx types */
+static inline unsigned char dt_type(struct usysfs_dirent *sd)
+{
+ return (sd->s_mode >> 12) & 15;
+}
+
+static int usysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
+{
+ struct dentry *dentry = filp->f_dentry;
+ struct usysfs_dirent * parent_sd = dentry->d_fsdata;
+ struct usysfs_dirent *cursor = filp->private_data;
+ struct list_head *p, *q = &cursor->s_sibling;
+ ino_t ino;
+ int i = filp->f_pos;
+
+ switch (i) {
+ case 0:
+ ino = dentry->d_inode->i_ino;
+ if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
+ break;
+ filp->f_pos++;
+ i++;
+ /* fallthrough */
+ case 1:
+ ino = parent_ino(dentry);
+ if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
+ break;
+ filp->f_pos++;
+ i++;
+ /* fallthrough */
+ default:
+ if (filp->f_pos == 2) {
+ list_del(q);
+ list_add(q, &parent_sd->s_children);
+ }
+ for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
+ struct usysfs_dirent *next;
+ const char * name;
+ int len;
+
+ next = list_entry(p, struct usysfs_dirent,
+ s_sibling);
+ if (!next->s_element)
+ continue;
+
+ name = usysfs_get_name(next);
+ len = strlen(name);
+ if (next->s_dentry)
+ ino = next->s_dentry->d_inode->i_ino;
+ else
+ ino = iunique(usysfs_sb, 2);
+
+ if (filldir(dirent, name, len, filp->f_pos, ino,
+ dt_type(next)) < 0)
+ return 0;
+
+ list_del(q);
+ list_add(q, p);
+ p = q;
+ filp->f_pos++;
+ }
+ }
+ return 0;
+}
+
+static loff_t usysfs_dir_lseek(struct file * file, loff_t offset, int origin)
+{
+ struct dentry * dentry = file->f_dentry;
+
+ down(&dentry->d_inode->i_sem);
+ switch (origin) {
+ case 1:
+ offset += file->f_pos;
+ case 0:
+ if (offset >= 0)
+ break;
+ default:
+ up(&file->f_dentry->d_inode->i_sem);
+ return -EINVAL;
+ }
+ if (offset != file->f_pos) {
+ file->f_pos = offset;
+ if (file->f_pos >= 2) {
+ struct usysfs_dirent *sd = dentry->d_fsdata;
+ struct usysfs_dirent *cursor = file->private_data;
+ struct list_head *p;
+ loff_t n = file->f_pos - 2;
+
+ list_del(&cursor->s_sibling);
+ p = sd->s_children.next;
+ while (n && p != &sd->s_children) {
+ struct usysfs_dirent *next;
+ next = list_entry(p, struct usysfs_dirent,
+ s_sibling);
+ if (next->s_element)
+ n--;
+ p = p->next;
+ }
+ list_add_tail(&cursor->s_sibling, p);
+ }
+ }
+ up(&dentry->d_inode->i_sem);
+ return offset;
+}
+
+struct file_operations usysfs_dir_operations = {
+ .open = usysfs_dir_open,
+ .release = usysfs_dir_close,
+ .llseek = usysfs_dir_lseek,
+ .read = generic_read_dir,
+ .readdir = usysfs_readdir,
+};
+
+int usysfs_register_subsystem(struct kset *k)
+{
+ int err;
+ struct qstr name;
+ struct dentry *dentry;
+
+ err = usysfs_pin_fs();
+ if (err)
+ return err;
+
+ err = -ENOMEM;
+ if (!k->kobj.k_name)
+ k->kobj.k_name = k->kobj.name;
+ name.name = k->kobj.k_name;
+ name.len = strlen(name.name);
+ name.hash = full_name_hash(name.name, name.len);
+ dentry = d_alloc(usysfs_sb->s_root, &name);
+ if (dentry) {
+ err = usysfs_create_dir(&k->kobj, dentry);
+ }
+
+ if (!err) {
+ d_rehash(dentry);
+ err = populate_dir(&k->kobj);
+ if (err)
+ usysfs_remove_dir(&k->kobj);
+ }
+
+ if (err)
+ usysfs_release_fs();
+
+ return err;
+}
+
+void usysfs_unregister_subsystem(struct kset *k)
+{
+ printk("Unregister is broken\n");
+ usysfs_remove_dir(&k->kobj);
+ usysfs_release_fs();
+}
+
+EXPORT_SYMBOL_GPL(usysfs_register_subsystem);
+EXPORT_SYMBOL_GPL(usysfs_unregister_subsystem);
+#if 0
+EXPORT_SYMBOL_GPL(usysfs_create_dir);
+EXPORT_SYMBOL_GPL(usysfs_remove_dir);
+EXPORT_SYMBOL_GPL(usysfs_rename_dir);
+#endif
Added: trunk/usysfs/file.c
===================================================================
--- trunk/usysfs/file.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/file.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,452 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ * file.c - operations for regular (text) files.
+ *
+ * Changes for usysfs Copyright (c) 2005 Oracle Corporation
+ */
+
+#include <linux/module.h>
+#include <linux/dnotify.h>
+#include <linux/kobject.h>
+#include <asm/uaccess.h>
+#include <asm/semaphore.h>
+
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+#define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
+#define to_sattr(a) container_of(a,struct subsys_attribute,attr)
+
+/**
+ * Subsystem file operations.
+ * These operations allow subsystems to have files that can be
+ * read/written.
+ */
+static ssize_t
+subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
+{
+ struct subsystem * s = to_subsys(kobj);
+ struct subsys_attribute * sattr = to_sattr(attr);
+ ssize_t ret = 0;
+
+ if (sattr->show)
+ ret = sattr->show(s,page);
+ return ret;
+}
+
+static ssize_t
+subsys_attr_store(struct kobject * kobj, struct attribute * attr,
+ const char * page, size_t count)
+{
+ struct subsystem * s = to_subsys(kobj);
+ struct subsys_attribute * sattr = to_sattr(attr);
+ ssize_t ret = 0;
+
+ if (sattr->store)
+ ret = sattr->store(s,page,count);
+ return ret;
+}
+
+static struct sysfs_ops subsys_sysfs_ops = {
+ .show = subsys_attr_show,
+ .store = subsys_attr_store,
+};
+
+
+struct usysfs_buffer {
+ size_t count;
+ loff_t pos;
+ char * page;
+ struct sysfs_ops * ops;
+ struct semaphore sem;
+ int needs_read_fill;
+};
+
+
+/**
+ * fill_read_buffer - allocate and fill buffer from object.
+ * @dentry: dentry pointer.
+ * @buffer: data buffer for file.
+ *
+ * Allocate @buffer->page, if it hasn't been already, then call the
+ * kobject's show() method to fill the buffer with this attribute's
+ * data.
+ * This is called only once, on the file's first read.
+ */
+static int fill_read_buffer(struct dentry * dentry, struct usysfs_buffer * buffer)
+{
+ struct attribute * attr = to_attr(dentry);
+ struct kobject * kobj = to_kobj(dentry->d_parent);
+ struct sysfs_ops * ops = buffer->ops;
+ int ret = 0;
+ ssize_t count;
+
+ if (!buffer->page)
+ buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
+ if (!buffer->page)
+ return -ENOMEM;
+
+ count = ops->show(kobj,attr,buffer->page);
+ buffer->needs_read_fill = 0;
+ BUG_ON(count > (ssize_t)PAGE_SIZE);
+ if (count >= 0)
+ buffer->count = count;
+ else
+ ret = count;
+ return ret;
+}
+
+
+/**
+ * flush_read_buffer - push buffer to userspace.
+ * @buffer: data buffer for file.
+ * @userbuf: user-passed buffer.
+ * @count: number of bytes requested.
+ * @ppos: file position.
+ *
+ * Copy the buffer we filled in fill_read_buffer() to userspace.
+ * This is done at the reader's leisure, copying and advancing
+ * the amount they specify each time.
+ * This may be called continuously until the buffer is empty.
+ */
+static int flush_read_buffer(struct usysfs_buffer * buffer, char __user * buf,
+ size_t count, loff_t * ppos)
+{
+ int error;
+
+ if (*ppos > buffer->count)
+ return 0;
+
+ if (count > (buffer->count - *ppos))
+ count = buffer->count - *ppos;
+
+ error = copy_to_user(buf,buffer->page + *ppos,count);
+ if (!error)
+ *ppos += count;
+ return error ? -EFAULT : count;
+}
+
+/**
+ * usysfs_read_file - read an attribute.
+ * @file: file pointer.
+ * @buf: buffer to fill.
+ * @count: number of bytes to read.
+ * @ppos: starting offset in file.
+ *
+ * Userspace wants to read an attribute file. The attribute descriptor
+ * is in the file's ->d_fsdata. The target object is in the directory's
+ * ->d_fsdata.
+ *
+ * We call fill_read_buffer() to allocate and fill the buffer from the
+ * object's show() method exactly once (if the read is happening from
+ * the beginning of the file). That should fill the entire buffer with
+ * all the data the object has to offer for that attribute.
+ * We then call flush_read_buffer() to copy the buffer to userspace
+ * in the increments specified.
+ */
+
+static ssize_t
+usysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+ struct usysfs_buffer * buffer = file->private_data;
+ ssize_t retval = 0;
+
+ down(&buffer->sem);
+ if (buffer->needs_read_fill) {
+ if ((retval = fill_read_buffer(file->f_dentry,buffer)))
+ goto out;
+ }
+ pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
+ __FUNCTION__,count,*ppos,buffer->page);
+ retval = flush_read_buffer(buffer,buf,count,ppos);
+out:
+ up(&buffer->sem);
+ return retval;
+}
+
+
+/**
+ * fill_write_buffer - copy buffer from userspace.
+ * @buffer: data buffer for file.
+ * @userbuf: data from user.
+ * @count: number of bytes in @userbuf.
+ *
+ * Allocate @buffer->page if it hasn't been already, then
+ * copy the user-supplied buffer into it.
+ */
+
+static int
+fill_write_buffer(struct usysfs_buffer * buffer, const char __user * buf, size_t count)
+{
+ int error;
+
+ if (!buffer->page)
+ buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
+ if (!buffer->page)
+ return -ENOMEM;
+
+ if (count >= PAGE_SIZE)
+ count = PAGE_SIZE - 1;
+ error = copy_from_user(buffer->page,buf,count);
+ buffer->needs_read_fill = 1;
+ return error ? -EFAULT : count;
+}
+
+
+/**
+ * flush_write_buffer - push buffer to kobject.
+ * @file: file pointer.
+ * @buffer: data buffer for file.
+ *
+ * Get the correct pointers for the kobject and the attribute we're
+ * dealing with, then call the store() method for the attribute,
+ * passing the buffer that we acquired in fill_write_buffer().
+ */
+
+static int
+flush_write_buffer(struct dentry * dentry, struct usysfs_buffer * buffer, size_t count)
+{
+ struct attribute * attr = to_attr(dentry);
+ struct kobject * kobj = to_kobj(dentry->d_parent);
+ struct sysfs_ops * ops = buffer->ops;
+
+ return ops->store(kobj,attr,buffer->page,count);
+}
+
+
+/**
+ * usysfs_write_file - write an attribute.
+ * @file: file pointer
+ * @buf: data to write
+ * @count: number of bytes
+ * @ppos: starting offset
+ *
+ * Similar to usysfs_read_file(), though working in the opposite direction.
+ * We allocate and fill the data from the user in fill_write_buffer(),
+ * then push it to the kobject in flush_write_buffer().
+ * There is no easy way for us to know if userspace is only doing a partial
+ * write, so we don't support them. We expect the entire buffer to come
+ * on the first write.
+ * Hint: if you're writing a value, first read the file, modify only the
+ * the value you're changing, then write entire buffer back.
+ */
+
+static ssize_t
+usysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
+{
+ struct usysfs_buffer * buffer = file->private_data;
+
+ down(&buffer->sem);
+ count = fill_write_buffer(buffer,buf,count);
+ if (count > 0)
+ count = flush_write_buffer(file->f_dentry,buffer,count);
+ if (count > 0)
+ *ppos += count;
+ up(&buffer->sem);
+ return count;
+}
+
+static int check_perm(struct inode * inode, struct file * file)
+{
+ struct kobject *kobj = usysfs_get_kobject(file->f_dentry->d_parent);
+ struct attribute * attr = to_attr(file->f_dentry);
+ struct usysfs_buffer * buffer;
+ struct sysfs_ops * ops = NULL;
+ int error = 0;
+
+ if (!kobj || !attr)
+ goto Einval;
+
+ /* Grab the module reference for this attribute if we have one */
+ if (!try_module_get(attr->owner)) {
+ error = -ENODEV;
+ goto Done;
+ }
+
+ /* if the kobject has no ktype, then we assume that it is a subsystem
+ * itself, and use ops for it.
+ */
+ if (kobj->kset && kobj->kset->ktype)
+ ops = kobj->kset->ktype->sysfs_ops;
+ else if (kobj->ktype)
+ ops = kobj->ktype->sysfs_ops;
+ else
+ ops = &subsys_sysfs_ops;
+
+ /* No usysfs operations, either from having no subsystem,
+ * or the subsystem have no operations.
+ */
+ if (!ops)
+ goto Eaccess;
+
+ /* File needs write support.
+ * The inode's perms must say it's ok,
+ * and we must have a store method.
+ */
+ if (file->f_mode & FMODE_WRITE) {
+
+ if (!(inode->i_mode & S_IWUGO) || !ops->store)
+ goto Eaccess;
+
+ }
+
+ /* File needs read support.
+ * The inode's perms must say it's ok, and we there
+ * must be a show method for it.
+ */
+ if (file->f_mode & FMODE_READ) {
+ if (!(inode->i_mode & S_IRUGO) || !ops->show)
+ goto Eaccess;
+ }
+
+ /* No error? Great, allocate a buffer for the file, and store it
+ * it in file->private_data for easy access.
+ */
+ buffer = kmalloc(sizeof(struct usysfs_buffer),GFP_KERNEL);
+ if (buffer) {
+ memset(buffer,0,sizeof(struct usysfs_buffer));
+ init_MUTEX(&buffer->sem);
+ buffer->needs_read_fill = 1;
+ buffer->ops = ops;
+ file->private_data = buffer;
+ } else
+ error = -ENOMEM;
+ goto Done;
+
+ Einval:
+ error = -EINVAL;
+ goto Done;
+ Eaccess:
+ error = -EACCES;
+ module_put(attr->owner);
+ Done:
+ if (error && kobj)
+ kobject_put(kobj);
+ return error;
+}
+
+static int usysfs_open_file(struct inode * inode, struct file * filp)
+{
+ return check_perm(inode,filp);
+}
+
+static int usysfs_release(struct inode * inode, struct file * filp)
+{
+ struct kobject * kobj = to_kobj(filp->f_dentry->d_parent);
+ struct attribute * attr = to_attr(filp->f_dentry);
+ struct module * owner = attr->owner;
+ struct usysfs_buffer * buffer = filp->private_data;
+
+ if (kobj)
+ kobject_put(kobj);
+ /* After this point, attr should not be accessed. */
+ module_put(owner);
+
+ if (buffer) {
+ if (buffer->page)
+ free_page((unsigned long)buffer->page);
+ kfree(buffer);
+ }
+ return 0;
+}
+
+struct file_operations usysfs_file_operations = {
+ .read = usysfs_read_file,
+ .write = usysfs_write_file,
+ .llseek = generic_file_llseek,
+ .open = usysfs_open_file,
+ .release = usysfs_release,
+};
+
+
+int usysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
+{
+ struct usysfs_dirent * parent_sd = dir->d_fsdata;
+ umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
+ int error = 0;
+
+ down(&dir->d_inode->i_sem);
+ error = usysfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
+ up(&dir->d_inode->i_sem);
+
+ return error;
+}
+
+
+/**
+ * usysfs_create_file - create an attribute file for an object.
+ * @kobj: object we're creating for.
+ * @attr: atrribute descriptor.
+ */
+
+int usysfs_create_file(struct kobject * kobj, const struct attribute * attr)
+{
+ BUG_ON(!kobj || !kobj->dentry || !attr);
+
+ return usysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
+
+}
+
+
+/**
+ * usysfs_update_file - update the modified timestamp on an object attribute.
+ * @kobj: object we're acting for.
+ * @attr: attribute descriptor.
+ *
+ * Also call dnotify for the dentry, which lots of userspace programs
+ * use.
+ */
+int usysfs_update_file(struct kobject * kobj, const struct attribute * attr)
+{
+ struct dentry * dir = kobj->dentry;
+ struct dentry * victim;
+ int res = -ENOENT;
+
+ down(&dir->d_inode->i_sem);
+ victim = usysfs_get_dentry(dir, attr->name);
+ if (!IS_ERR(victim)) {
+ /* make sure dentry is really there */
+ if (victim->d_inode &&
+ (victim->d_parent->d_inode == dir->d_inode)) {
+ victim->d_inode->i_mtime = CURRENT_TIME;
+ dnotify_parent(victim, DN_MODIFY);
+
+ /**
+ * Drop reference from initial usysfs_get_dentry().
+ */
+ dput(victim);
+ res = 0;
+ } else
+ d_drop(victim);
+
+ /**
+ * Drop the reference acquired from usysfs_get_dentry() above.
+ */
+ dput(victim);
+ }
+ up(&dir->d_inode->i_sem);
+
+ return res;
+}
+
+
+/**
+ * usysfs_remove_file - remove an object attribute.
+ * @kobj: object we're acting for.
+ * @attr: attribute descriptor.
+ *
+ * Hash the attribute name and kill the victim.
+ */
+
+void usysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
+{
+ usysfs_hash_and_remove(kobj->dentry,attr->name);
+}
+
+
+#if 0
+EXPORT_SYMBOL_GPL(usysfs_create_file);
+EXPORT_SYMBOL_GPL(usysfs_remove_file);
+EXPORT_SYMBOL_GPL(usysfs_update_file);
+#endif
+
Added: trunk/usysfs/group.c
===================================================================
--- trunk/usysfs/group.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/group.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,85 @@
+/*
+ * fs/usysfs/group.c - Operations for adding/removing multiple files at once.
+ *
+ * Copyright (c) 2003 Patrick Mochel
+ * Copyright (c) 2003 Open Source Development Lab
+ *
+ * This file is released undert the GPL v2.
+ *
+ */
+
+#include <linux/kobject.h>
+#include <linux/module.h>
+#include <linux/dcache.h>
+#include <linux/err.h>
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+
+static void remove_files(struct dentry * dir,
+ const struct attribute_group * grp)
+{
+ struct attribute *const* attr;
+
+ for (attr = grp->attrs; *attr; attr++)
+ usysfs_hash_and_remove(dir,(*attr)->name);
+}
+
+static int create_files(struct dentry * dir,
+ const struct attribute_group * grp)
+{
+ struct attribute *const* attr;
+ int error = 0;
+
+ for (attr = grp->attrs; *attr && !error; attr++) {
+ error = usysfs_add_file(dir, *attr, SYSFS_KOBJ_ATTR);
+ }
+ if (error)
+ remove_files(dir,grp);
+ return error;
+}
+
+
+int usysfs_create_group(struct kobject * kobj,
+ const struct attribute_group * grp)
+{
+ struct dentry * dir;
+ int error;
+
+ BUG_ON(!kobj || !kobj->dentry);
+
+ if (grp->name) {
+ error = usysfs_create_subdir(kobj,grp->name,&dir);
+ if (error)
+ return error;
+ } else
+ dir = kobj->dentry;
+ dir = dget(dir);
+ if ((error = create_files(dir,grp))) {
+ if (grp->name)
+ usysfs_remove_subdir(dir);
+ }
+ dput(dir);
+ return error;
+}
+
+void usysfs_remove_group(struct kobject * kobj,
+ const struct attribute_group * grp)
+{
+ struct dentry * dir;
+
+ if (grp->name)
+ dir = usysfs_get_dentry(kobj->dentry,grp->name);
+ else
+ dir = dget(kobj->dentry);
+
+ remove_files(dir,grp);
+ if (grp->name)
+ usysfs_remove_subdir(dir);
+ /* release the ref. taken in this routine */
+ dput(dir);
+}
+
+
+EXPORT_SYMBOL_GPL(usysfs_create_group);
+EXPORT_SYMBOL_GPL(usysfs_remove_group);
Added: trunk/usysfs/inode.c
===================================================================
--- trunk/usysfs/inode.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/inode.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,162 @@
+/*
+ * inode.c - basic inode and dentry operations.
+ *
+ * usysfs is Copyright (c) 2001-3 Patrick Mochel
+ *
+ * Please see Documentation/filesystems/usysfs.txt for more information.
+ */
+
+#undef DEBUG
+
+#include <linux/pagemap.h>
+#include <linux/namei.h>
+#include <linux/backing-dev.h>
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+extern struct super_block * usysfs_sb;
+
+static struct address_space_operations usysfs_aops = {
+ .readpage = simple_readpage,
+ .prepare_write = simple_prepare_write,
+ .commit_write = simple_commit_write
+};
+
+static struct backing_dev_info usysfs_backing_dev_info = {
+ .ra_pages = 0, /* No readahead */
+ .memory_backed = 1, /* Does not contribute to dirty memory */
+};
+
+struct inode * usysfs_new_inode(mode_t mode)
+{
+ struct inode * inode = new_inode(usysfs_sb);
+ if (inode) {
+ inode->i_mode = mode;
+ inode->i_uid = 0;
+ inode->i_gid = 0;
+ inode->i_blksize = PAGE_CACHE_SIZE;
+ inode->i_blocks = 0;
+ inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
+ inode->i_mapping->a_ops = &usysfs_aops;
+ inode->i_mapping->backing_dev_info = &usysfs_backing_dev_info;
+ }
+ return inode;
+}
+
+int usysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
+{
+ int error = 0;
+ struct inode * inode = NULL;
+ if (dentry) {
+ if (!dentry->d_inode) {
+ if ((inode = usysfs_new_inode(mode))) {
+ if (dentry->d_parent && dentry->d_parent->d_inode) {
+ struct inode *p_inode = dentry->d_parent->d_inode;
+ p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
+ }
+ goto Proceed;
+ }
+ else
+ error = -ENOMEM;
+ } else
+ error = -EEXIST;
+ } else
+ error = -ENOENT;
+ goto Done;
+
+ Proceed:
+ if (init)
+ error = init(inode);
+ if (!error) {
+ d_instantiate(dentry, inode);
+ if (S_ISDIR(mode))
+ dget(dentry); /* pin only directory dentry in core */
+ } else
+ iput(inode);
+ Done:
+ return error;
+}
+
+struct dentry * usysfs_get_dentry(struct dentry * parent, const char * name)
+{
+ struct qstr qstr;
+
+ qstr.name = name;
+ qstr.len = strlen(name);
+ qstr.hash = full_name_hash(name,qstr.len);
+ return lookup_hash(&qstr,parent);
+}
+
+/*
+ * Get the name for corresponding element represented by the given usysfs_dirent
+ */
+const unsigned char * usysfs_get_name(struct usysfs_dirent *sd)
+{
+ struct attribute * attr;
+ struct bin_attribute * bin_attr;
+ struct usysfs_symlink * sl;
+
+ if (!sd || !sd->s_element)
+ BUG();
+
+ switch (sd->s_type) {
+ case SYSFS_DIR:
+ /* Always have a dentry so use that */
+ return sd->s_dentry->d_name.name;
+
+ case SYSFS_KOBJ_ATTR:
+ attr = sd->s_element;
+ return attr->name;
+
+ case SYSFS_KOBJ_BIN_ATTR:
+ bin_attr = sd->s_element;
+ return bin_attr->attr.name;
+
+ case SYSFS_KOBJ_LINK:
+ sl = sd->s_element;
+ return sl->link_name;
+ }
+ return NULL;
+}
+
+
+/*
+ * Unhashes the dentry corresponding to given usysfs_dirent
+ * Called with parent inode's i_sem held.
+ */
+void usysfs_drop_dentry(struct usysfs_dirent * sd, struct dentry * parent)
+{
+ struct dentry * dentry = sd->s_dentry;
+
+ if (dentry) {
+ spin_lock(&dcache_lock);
+ if (!(d_unhashed(dentry) && dentry->d_inode)) {
+ dget_locked(dentry);
+ __d_drop(dentry);
+ spin_unlock(&dcache_lock);
+ simple_unlink(parent->d_inode, dentry);
+ } else
+ spin_unlock(&dcache_lock);
+ }
+}
+
+void usysfs_hash_and_remove(struct dentry * dir, const char * name)
+{
+ struct usysfs_dirent * sd;
+ struct usysfs_dirent * parent_sd = dir->d_fsdata;
+
+ down(&dir->d_inode->i_sem);
+ list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
+ if (!sd->s_element)
+ continue;
+ if (!strcmp(usysfs_get_name(sd), name)) {
+ list_del_init(&sd->s_sibling);
+ usysfs_drop_dentry(sd, dir);
+ usysfs_put(sd);
+ break;
+ }
+ }
+ up(&dir->d_inode->i_sem);
+}
+
+
Added: trunk/usysfs/mount.c
===================================================================
--- trunk/usysfs/mount.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/mount.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,118 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * mount.c - operations for initializing and mounting usysfs.
+ *
+ * Changes for usysfs Copyright (c) 2004 Oracle Corporation.
+ */
+
+#define DEBUG
+
+#include <linux/fs.h>
+#include <linux/module.h>
+#include <linux/mount.h>
+#include <linux/pagemap.h>
+#include <linux/init.h>
+
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+/* Random magic number */
+#define USYSFS_MAGIC 0x62656570
+
+struct vfsmount * usysfs_mount = NULL;
+struct super_block * usysfs_sb = NULL;
+static int usysfs_mnt_count = 0;
+
+static struct super_operations usysfs_ops = {
+ .statfs = simple_statfs,
+ .drop_inode = generic_delete_inode,
+};
+
+static struct usysfs_dirent usysfs_root = {
+ .s_sibling = LIST_HEAD_INIT(usysfs_root.s_sibling),
+ .s_children = LIST_HEAD_INIT(usysfs_root.s_children),
+ .s_element = NULL,
+ .s_type = SYSFS_ROOT,
+};
+
+static int usysfs_fill_super(struct super_block *sb, void *data, int silent)
+{
+ struct inode *inode;
+ struct dentry *root;
+
+ sb->s_blocksize = PAGE_CACHE_SIZE;
+ sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
+ sb->s_magic = USYSFS_MAGIC;
+ sb->s_op = &usysfs_ops;
+ usysfs_sb = sb;
+
+ inode = usysfs_new_inode(S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
+ if (inode) {
+ inode->i_op = &usysfs_dir_inode_operations;
+ inode->i_fop = &usysfs_dir_operations;
+ /* directory inodes start off with i_nlink == 2 (for "." entry) */
+ inode->i_nlink++;
+ } else {
+ pr_debug("usysfs: could not get root inode\n");
+ return -ENOMEM;
+ }
+
+ root = d_alloc_root(inode);
+ if (!root) {
+ pr_debug("%s: could not get root dentry!\n",__FUNCTION__);
+ iput(inode);
+ return -ENOMEM;
+ }
+ root->d_fsdata = &usysfs_root;
+ sb->s_root = root;
+ return 0;
+}
+
+static struct super_block *usysfs_get_sb(struct file_system_type *fs_type,
+ int flags, const char *dev_name, void *data)
+{
+ return get_sb_single(fs_type, flags, data, usysfs_fill_super);
+}
+
+static struct file_system_type usysfs_fs_type = {
+ .owner = THIS_MODULE,
+ .name = "usysfs",
+ .get_sb = usysfs_get_sb,
+ .kill_sb = kill_litter_super,
+};
+
+int usysfs_pin_fs(void)
+{
+ return simple_pin_fs("usysfs", &usysfs_mount,
+ &usysfs_mnt_count);
+}
+
+void usysfs_release_fs(void)
+{
+ simple_release_fs(&usysfs_mount, &usysfs_mnt_count);
+}
+
+static int __init usysfs_init(void)
+{
+ int err;
+
+ err = register_filesystem(&usysfs_fs_type);
+ if (err) {
+ printk(KERN_ERR "usysfs: Unable to register filesystem!\n");
+ }
+
+ return err;
+}
+
+static void __exit usysfs_exit(void)
+{
+ unregister_filesystem(&usysfs_fs_type);
+}
+
+module_init(usysfs_init);
+module_exit(usysfs_exit);
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Joel Becker <joel.becker at oracle.com>");
+MODULE_VERSION("0.0.1");
+MODULE_DESCRIPTION("Simple RAM filesystem for user created kobjects.");
Added: trunk/usysfs/symlink.c
===================================================================
--- trunk/usysfs/symlink.c 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/symlink.c 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,224 @@
+/*
+ * symlink.c - operations for usysfs symlinks.
+ */
+
+#include <linux/version.h>
+#include <linux/fs.h>
+#include <linux/module.h>
+#include <linux/kobject.h>
+#include <linux/namei.h>
+
+#include "usysfs.h"
+#include "usysfs_internal.h"
+
+static int object_depth(struct kobject * kobj)
+{
+ struct kobject * p = kobj;
+ int depth = 0;
+ do { depth++; } while ((p = p->parent));
+ return depth;
+}
+
+static int object_path_length(struct kobject * kobj)
+{
+ struct kobject * p = kobj;
+ int length = 1;
+ do {
+ length += strlen(kobject_name(p)) + 1;
+ p = p->parent;
+ } while (p);
+ return length;
+}
+
+static void fill_object_path(struct kobject * kobj, char * buffer, int length)
+{
+ struct kobject * p;
+
+ --length;
+ for (p = kobj; p; p = p->parent) {
+ int cur = strlen(kobject_name(p));
+
+ /* back up enough to print this bus id with '/' */
+ length -= cur;
+ strncpy(buffer + length,kobject_name(p),cur);
+ *(buffer + --length) = '/';
+ }
+}
+
+static int usysfs_add_link(struct dentry * parent, char * name, struct kobject * target)
+{
+ struct usysfs_dirent * parent_sd = parent->d_fsdata;
+ struct usysfs_symlink * sl;
+ int error = 0;
+
+ error = -ENOMEM;
+ sl = kmalloc(sizeof(*sl), GFP_KERNEL);
+ if (!sl)
+ goto exit1;
+
+ sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
+ if (!sl->link_name)
+ goto exit2;
+
+ strcpy(sl->link_name, name);
+ sl->target_kobj = kobject_get(target);
+
+ error = usysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
+ SYSFS_KOBJ_LINK);
+ if (!error)
+ return 0;
+
+ kfree(sl->link_name);
+exit2:
+ kfree(sl);
+exit1:
+ return error;
+}
+
+/**
+ * usysfs_create_link - create symlink between two objects.
+ * @kobj: object whose directory we're creating the link in.
+ * @target: object we're pointing to.
+ * @name: name of the symlink.
+ */
+int usysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
+{
+ struct dentry * dentry = kobj->dentry;
+ int error = 0;
+
+ BUG_ON(!kobj || !kobj->dentry || !name);
+
+ down(&dentry->d_inode->i_sem);
+ error = usysfs_add_link(dentry, name, target);
+ up(&dentry->d_inode->i_sem);
+ return error;
+}
+
+
+/**
+ * usysfs_remove_link - remove symlink in object's directory.
+ * @kobj: object we're acting for.
+ * @name: name of the symlink to remove.
+ */
+
+void usysfs_remove_link(struct kobject * kobj, char * name)
+{
+ usysfs_hash_and_remove(kobj->dentry,name);
+}
+
+static int usysfs_get_target_path(struct kobject * kobj, struct kobject * target,
+ char *path)
+{
+ char * s;
+ int depth, size;
+
+ depth = object_depth(kobj);
+ size = object_path_length(target) + depth * 3 - 1;
+ if (size > PATH_MAX)
+ return -ENAMETOOLONG;
+
+ pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
+
+ for (s = path; depth--; s += 3)
+ strcpy(s,"../");
+
+ fill_object_path(target, path, size);
+ pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
+
+ return 0;
+}
+
+static int usysfs_getlink(struct dentry *dentry, char * path)
+{
+ struct kobject *kobj, *target_kobj;
+ int error = 0;
+
+ kobj = usysfs_get_kobject(dentry->d_parent);
+ if (!kobj)
+ return -EINVAL;
+
+ target_kobj = usysfs_get_kobject(dentry);
+ if (!target_kobj) {
+ kobject_put(kobj);
+ return -EINVAL;
+ }
+
+ down_read(&usysfs_rename_sem);
+ error = usysfs_get_target_path(kobj, target_kobj, path);
+ up_read(&usysfs_rename_sem);
+
+ kobject_put(kobj);
+ kobject_put(target_kobj);
+ return error;
+
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
+int usysfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
+{
+ int error = 0;
+ unsigned long page = get_zeroed_page(GFP_KERNEL);
+
+ if (!page)
+ return -ENOMEM;
+
+ error = usysfs_getlink(dentry, (char *) page);
+ if (!error)
+ error = vfs_readlink(dentry, buffer, buflen, (char *) page);
+
+ free_page(page);
+
+ return error;
+}
+
+int usysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+ int error = 0;
+ unsigned long page = get_zeroed_page(GFP_KERNEL);
+
+ if (!page)
+ return -ENOMEM;
+
+ error = usysfs_getlink(dentry, (char *) page);
+ if (!error)
+ error = vfs_follow_link(nd, (char *) page);
+
+ free_page(page);
+
+ return error;
+}
+#else
+static int usysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+ int error = -ENOMEM;
+ unsigned long page = get_zeroed_page(GFP_KERNEL);
+ if (page)
+ error = usysfs_getlink(dentry, (char *) page);
+ nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
+ return 0;
+}
+
+static void usysfs_put_link(struct dentry *dentry, struct nameidata *nd)
+{
+ char *page = nd_get_link(nd);
+ if (!IS_ERR(page))
+ free_page((unsigned long)page);
+}
+#endif
+
+struct inode_operations usysfs_symlink_inode_operations = {
+ .follow_link = usysfs_follow_link,
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
+ .readlink = usysfs_readlink,
+#else
+ .readlink = generic_readlink,
+ .put_link = usysfs_put_link,
+#endif
+};
+
+
+#if 0
+EXPORT_SYMBOL_GPL(usysfs_create_link);
+EXPORT_SYMBOL_GPL(usysfs_remove_link);
+#endif
+
Added: trunk/usysfs/usysfs.h
===================================================================
--- trunk/usysfs/usysfs.h 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/usysfs.h 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,208 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * usysfs.h - definitions for the device driver filesystem
+ *
+ * Copyright (c) 2001,2002 Patrick Mochel
+ *
+ * Please see Documentation/filesystems/usysfs.txt for more information.
+ *
+ * Changes for usysfs Copyright (c) 2005 Oracle Corporation
+ */
+
+#ifndef _USYSFS_H_
+#define _USYSFS_H_
+
+#include <asm/atomic.h>
+
+struct kobject;
+struct module;
+
+/* sysfs.h always has these */
+#if 0
+struct attribute {
+ char * name;
+ struct module * owner;
+ mode_t mode;
+};
+
+struct attribute_group {
+ char * name;
+ struct attribute ** attrs;
+};
+#endif
+
+/*
+ * Usysfs objects must have a ->ktype of type ukobj_type.
+ * If allow_link() exists, the object can symlink(2) out to other
+ * objects. If the object is a kset, it supports mkdir(2). The object
+ * either supports make_kset() if its children are ksets or
+ * make_object() if its children are mere objects. If the kset has
+ * commit(), it supports pending and commited (active) objects.
+ */
+struct ukobj_type {
+ struct kobj_type ktype;
+ int (*allow_link)(struct kobject *src, struct kobject *target);
+ struct kobject *(*make_object)(struct kset *kset, const char *name);
+ struct kset *(*make_kset)(struct kset *kset, const char *name);
+ int (*commit)(struct kobject *kobj);
+};
+
+static inline struct ukobj_type *to_uktype(struct kobj_type *ktype)
+{
+ return ktype ?
+ container_of(ktype, struct ukobj_type, ktype) :
+ NULL;
+}
+
+
+/**
+ * Use these macros to make defining attributes easier. See include/linux/device.h
+ * for examples..
+ */
+
+#define __ATTR(_name,_mode,_show,_store) { \
+ .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
+ .show = _show, \
+ .store = _store, \
+}
+
+#define __ATTR_RO(_name) { \
+ .attr = { .name = __stringify(_name), .mode = 0444, .owner = THIS_MODULE }, \
+ .show = _name##_show, \
+}
+
+#define __ATTR_NULL { .attr = { .name = NULL } }
+
+#define attr_name(_attr) (_attr).attr.name
+
+/* Again, sysfs.h has these */
+#if 0
+struct bin_attribute {
+ struct attribute attr;
+ size_t size;
+ ssize_t (*read)(struct kobject *, char *, loff_t, size_t);
+ ssize_t (*write)(struct kobject *, char *, loff_t, size_t);
+};
+
+struct usysfs_ops {
+ ssize_t (*show)(struct kobject *, struct attribute *,char *);
+ ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
+};
+#endif
+
+struct usysfs_dirent {
+ atomic_t s_count;
+ struct list_head s_sibling;
+ struct list_head s_children;
+ void * s_element;
+ int s_type;
+ umode_t s_mode;
+ struct dentry * s_dentry;
+};
+
+#define SYSFS_ROOT 0x0001
+#define SYSFS_DIR 0x0002
+#define SYSFS_KOBJ_ATTR 0x0004
+#define SYSFS_KOBJ_BIN_ATTR 0x0008
+#define SYSFS_KOBJ_LINK 0x0020
+#define SYSFS_NOT_PINNED (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR | SYSFS_KOBJ_LINK)
+
+#define CONFIG_USYSFS
+#ifdef CONFIG_USYSFS
+
+extern void
+usysfs_remove_dir(struct kobject *);
+
+extern int
+usysfs_rename_dir(struct kobject *, const char *new_name);
+
+extern int
+usysfs_create_file(struct kobject *, const struct attribute *);
+
+extern int
+usysfs_update_file(struct kobject *, const struct attribute *);
+
+extern void
+usysfs_remove_file(struct kobject *, const struct attribute *);
+
+extern int
+usysfs_create_link(struct kobject * kobj, struct kobject * target, char * name);
+
+extern void
+usysfs_remove_link(struct kobject *, char * name);
+
+int usysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr);
+int usysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr);
+
+int usysfs_create_group(struct kobject *, const struct attribute_group *);
+void usysfs_remove_group(struct kobject *, const struct attribute_group *);
+
+int usysfs_register_subsystem(struct kset *k);
+void usysfs_unregister_subsystem(struct kset *k);
+#else /* CONFIG_SYSFS */
+
+static inline int usysfs_create_dir(struct kobject * k)
+{
+ return 0;
+}
+
+static inline void usysfs_remove_dir(struct kobject * k)
+{
+ ;
+}
+
+static inline int usysfs_rename_dir(struct kobject * k, const char *new_name)
+{
+ return 0;
+}
+
+static inline int usysfs_create_file(struct kobject * k, const struct attribute * a)
+{
+ return 0;
+}
+
+static inline int usysfs_update_file(struct kobject * k, const struct attribute * a)
+{
+ return 0;
+}
+
+static inline void usysfs_remove_file(struct kobject * k, const struct attribute * a)
+{
+ ;
+}
+
+static inline int usysfs_create_link(struct kobject * k, struct kobject * t, char * n)
+{
+ return 0;
+}
+
+static inline void usysfs_remove_link(struct kobject * k, char * name)
+{
+ ;
+}
+
+
+static inline int usysfs_create_bin_file(struct kobject * k, struct bin_attribute * a)
+{
+ return 0;
+}
+
+static inline int usysfs_remove_bin_file(struct kobject * k, struct bin_attribute * a)
+{
+ return 0;
+}
+
+static inline int usysfs_create_group(struct kobject * k, const struct attribute_group *g)
+{
+ return 0;
+}
+
+static inline void usysfs_remove_group(struct kobject * k, const struct attribute_group * g)
+{
+ ;
+}
+
+#endif /* CONFIG_USYSFS */
+
+#endif /* _USYSFS_H_ */
Added: trunk/usysfs/usysfs_internal.h
===================================================================
--- trunk/usysfs/usysfs_internal.h 2005-01-18 01:13:40 UTC (rev 1781)
+++ trunk/usysfs/usysfs_internal.h 2005-01-18 01:36:24 UTC (rev 1782)
@@ -0,0 +1,97 @@
+
+extern struct vfsmount * usysfs_mount;
+
+extern struct inode * usysfs_new_inode(mode_t mode);
+extern int usysfs_create(struct dentry *, int mode, int (*init)(struct inode *));
+
+extern int usysfs_make_dirent(struct usysfs_dirent *, struct dentry *, void *,
+ umode_t, int);
+extern struct dentry * usysfs_get_dentry(struct dentry *, const char *);
+
+extern int usysfs_add_file(struct dentry *, const struct attribute *, int);
+extern void usysfs_hash_and_remove(struct dentry * dir, const char * name);
+
+extern int usysfs_create_subdir(struct kobject *, const char *, struct dentry **);
+extern void usysfs_remove_subdir(struct dentry *);
+
+extern const unsigned char * usysfs_get_name(struct usysfs_dirent *sd);
+extern void usysfs_drop_dentry(struct usysfs_dirent *sd, struct dentry *parent);
+
+extern int usysfs_pin_fs(void);
+extern void usysfs_release_fs(void);
+
+extern struct rw_semaphore usysfs_rename_sem;
+extern struct super_block * usysfs_sb;
+extern struct file_operations usysfs_dir_operations;
+extern struct file_operations usysfs_file_operations;
+extern struct file_operations bin_fops;
+extern struct inode_operations usysfs_dir_inode_operations;
+extern struct inode_operations usysfs_symlink_inode_operations;
+
+struct usysfs_symlink {
+ char * link_name;
+ struct kobject * target_kobj;
+};
+
+static inline struct kobject * to_kobj(struct dentry * dentry)
+{
+ struct usysfs_dirent * sd = dentry->d_fsdata;
+ return ((struct kobject *) sd->s_element);
+}
+
+static inline struct attribute * to_attr(struct dentry * dentry)
+{
+ struct usysfs_dirent * sd = dentry->d_fsdata;
+ return ((struct attribute *) sd->s_element);
+}
+
+static inline struct bin_attribute * to_bin_attr(struct dentry * dentry)
+{
+ struct usysfs_dirent * sd = dentry->d_fsdata;
+ return ((struct bin_attribute *) sd->s_element);
+}
+
+static inline struct kobject *usysfs_get_kobject(struct dentry *dentry)
+{
+ struct kobject * kobj = NULL;
+
+ spin_lock(&dcache_lock);
+ if (!d_unhashed(dentry)) {
+ struct usysfs_dirent * sd = dentry->d_fsdata;
+ if (sd->s_type & SYSFS_KOBJ_LINK) {
+ struct usysfs_symlink * sl = sd->s_element;
+ kobj = kobject_get(sl->target_kobj);
+ } else
+ kobj = kobject_get(sd->s_element);
+ }
+ spin_unlock(&dcache_lock);
+
+ return kobj;
+}
+
+static inline void release_usysfs_dirent(struct usysfs_dirent * sd)
+{
+ if (sd->s_type & SYSFS_KOBJ_LINK) {
+ struct usysfs_symlink * sl = sd->s_element;
+ kfree(sl->link_name);
+ kobject_put(sl->target_kobj);
+ kfree(sl);
+ }
+ kfree(sd);
+}
+
+static inline struct usysfs_dirent * usysfs_get(struct usysfs_dirent * sd)
+{
+ if (sd) {
+ WARN_ON(!atomic_read(&sd->s_count));
+ atomic_inc(&sd->s_count);
+ }
+ return sd;
+}
+
+static inline void usysfs_put(struct usysfs_dirent * sd)
+{
+ if (atomic_dec_and_test(&sd->s_count))
+ release_usysfs_dirent(sd);
+}
+
More information about the Ocfs2-commits
mailing list