Commit 004ced81 authored by Patrick Mochel's avatar Patrick Mochel

sysfs: fixup merge

parents 29b21c55 21c8dcc7
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
# Makefile for the sysfs virtual filesystem # Makefile for the sysfs virtual filesystem
# #
obj-y := inode.o obj-y := inode.o file.o dir.o symlink.o mount.o bin.o
/*
* bin.c - binary file operations for sysfs.
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include "sysfs.h"
static struct file_operations bin_fops;
static int fill_read(struct file * file, struct sysfs_bin_buffer * buffer)
{
struct bin_attribute * attr = file->f_dentry->d_fsdata;
struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
if (!buffer->data)
attr->read(kobj,buffer);
return buffer->size ? 0 : -ENOENT;
}
static int flush_read(struct file * file, char * userbuf,
struct sysfs_bin_buffer * buffer)
{
return copy_to_user(userbuf,buffer->data + buffer->offset,buffer->count) ?
-EFAULT : 0;
}
static ssize_t
read(struct file * file, char * userbuf, size_t count, loff_t * off)
{
struct sysfs_bin_buffer * buffer = file->private_data;
int ret;
ret = fill_read(file,buffer);
if (ret)
goto Done;
buffer->offset = *off;
if (count > (buffer->size - *off))
count = buffer->size - *off;
buffer->count = count;
ret = flush_read(file,userbuf,buffer);
if (!ret) {
*off += count;
ret = count;
}
Done:
return ret;
}
int alloc_buf_data(struct sysfs_bin_buffer * buffer)
{
buffer->data = kmalloc(buffer->count,GFP_KERNEL);
if (buffer->data) {
memset(buffer->data,0,buffer->count);
return 0;
} else
return -ENOMEM;
}
static int fill_write(struct file * file, const char * userbuf,
struct sysfs_bin_buffer * buffer)
{
return copy_from_user(buffer,userbuf,buffer->count) ?
-EFAULT : 0;
}
static int flush_write(struct file * file, const char * userbuf,
struct sysfs_bin_buffer * buffer)
{
struct bin_attribute * attr = file->f_dentry->d_fsdata;
struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
return attr->write(kobj,buffer);
}
static ssize_t write(struct file * file, const char * userbuf,
size_t count, loff_t * off)
{
struct sysfs_bin_buffer * buffer = file->private_data;
int ret;
if (count > PAGE_SIZE)
count = PAGE_SIZE;
buffer->count = count;
ret = alloc_buf_data(buffer);
if (ret)
goto Done;
ret = fill_write(file,userbuf,buffer);
if (ret)
goto Done;
ret = flush_write(file,userbuf,buffer);
if (ret > 0)
*off += count;
Done:
if (buffer->data) {
kfree(buffer->data);
buffer->data = NULL;
}
return ret;
}
static int check_perm(struct inode * inode, struct file * file)
{
struct kobject * kobj = kobject_get(file->f_dentry->d_parent->d_fsdata);
struct bin_attribute * attr = file->f_dentry->d_fsdata;
struct sysfs_bin_buffer * buffer;
int error = 0;
if (!kobj || !attr)
goto Einval;
/* 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) || !attr->write)
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) || !attr->read)
goto Eaccess;
}
buffer = kmalloc(sizeof(struct sysfs_bin_buffer),GFP_KERNEL);
if (buffer) {
memset(buffer,0,sizeof(struct sysfs_bin_buffer));
file->private_data = buffer;
} else
error = -ENOMEM;
goto Done;
Einval:
error = -EINVAL;
goto Done;
Eaccess:
error = -EACCES;
Done:
if (error && kobj)
kobject_put(kobj);
return error;
}
static int open(struct inode * inode, struct file * file)
{
return check_perm(inode,file);
}
static int release(struct inode * inode, struct file * file)
{
struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
u8 * buffer = file->private_data;
if (kobj)
kobject_put(kobj);
if (buffer)
kfree(buffer);
return 0;
}
static struct file_operations bin_fops = {
.read = read,
.write = write,
.llseek = generic_file_llseek,
.open = open,
.release = release,
};
/**
* sysfs_create_bin_file - create binary file for object.
* @kobj: object.
* @attr: attribute descriptor.
*
*/
int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
{
struct dentry * dentry;
struct dentry * parent;
int error = 0;
if (!kobj || !attr)
return -EINVAL;
parent = kobj->dentry;
down(&parent->d_inode->i_sem);
dentry = sysfs_get_dentry(parent,attr->attr.name);
if (!IS_ERR(dentry)) {
dentry->d_fsdata = (void *)attr;
error = sysfs_create(dentry,
(attr->attr.mode & S_IALLUGO) | S_IFREG,
NULL);
if (!error) {
dentry->d_inode->i_size = attr->size;
dentry->d_inode->i_fop = &bin_fops;
}
} else
error = PTR_ERR(dentry);
up(&parent->d_inode->i_sem);
return error;
}
/**
* sysfs_remove_bin_file - remove binary file for object.
* @kobj: object.
* @attr: attribute descriptor.
*
*/
int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
{
sysfs_hash_and_remove(kobj->dentry,attr->attr.name);
}
EXPORT_SYMBOL(sysfs_create_bin_file);
EXPORT_SYMBOL(sysfs_remove_bin_file);
/*
* dir.c - Operations for sysfs directories.
*/
#undef DEBUG
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include "sysfs.h"
static int init_dir(struct inode * inode)
{
inode->i_op = &simple_dir_inode_operations;
inode->i_fop = &simple_dir_operations;
/* directory inodes start off with i_nlink == 2 (for "." entry) */
inode->i_nlink++;
return 0;
}
/**
* sysfs_create_dir - create a directory for an object.
* @parent: parent parent object.
* @kobj: object we're creating directory for.
*/
int sysfs_create_dir(struct kobject * kobj)
{
struct dentry * dentry = NULL;
struct dentry * parent;
int error = 0;
if (!kobj)
return -EINVAL;
if (kobj->parent)
parent = kobj->parent->dentry;
else if (sysfs_mount && sysfs_mount->mnt_sb)
parent = sysfs_mount->mnt_sb->s_root;
else
return -EFAULT;
down(&parent->d_inode->i_sem);
dentry = sysfs_get_dentry(parent,kobj->name);
if (!IS_ERR(dentry)) {
dentry->d_fsdata = (void *)kobj;
kobj->dentry = dentry;
error = sysfs_create(dentry,(S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO),
init_dir);
if (!error)
parent->d_inode->i_nlink++;
} else
error = PTR_ERR(dentry);
up(&parent->d_inode->i_sem);
return error;
}
/**
* sysfs_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 sysfs_rmdir() below, instead of calling separately.
*/
void sysfs_remove_dir(struct kobject * kobj)
{
struct list_head * node, * next;
struct dentry * dentry = dget(kobj->dentry);
struct dentry * parent;
if (!dentry)
return;
pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
parent = dget(dentry->d_parent);
down(&parent->d_inode->i_sem);
down(&dentry->d_inode->i_sem);
list_for_each_safe(node,next,&dentry->d_subdirs) {
struct dentry * d = dget(list_entry(node,struct dentry,d_child));
/**
* Make sure dentry is still there
*/
pr_debug(" o %s: ",d->d_name.name);
if (d->d_inode) {
pr_debug("removing");
/**
* Unlink and unhash.
*/
simple_unlink(dentry->d_inode,d);
d_delete(d);
/**
* Drop reference from initial sysfs_get_dentry().
*/
dput(d);
}
pr_debug(" done (%d)\n",atomic_read(&d->d_count));
/**
* drop reference from dget() above.
*/
dput(d);
}
up(&dentry->d_inode->i_sem);
d_invalidate(dentry);
simple_rmdir(parent->d_inode,dentry);
d_delete(dentry);
pr_debug(" o %s removing done (%d)\n",dentry->d_name.name,
atomic_read(&dentry->d_count));
/**
* Drop reference from initial sysfs_get_dentry().
*/
dput(dentry);
/**
* Drop reference from dget() on entrance.
*/
dput(dentry);
up(&parent->d_inode->i_sem);
dput(parent);
}
EXPORT_SYMBOL(sysfs_create_dir);
EXPORT_SYMBOL(sysfs_remove_dir);
This diff is collapsed.
This diff is collapsed.
/*
* mount.c - operations for initializing and mounting sysfs.
*/
#define DEBUG
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include "sysfs.h"
/* Random magic number */
#define SYSFS_MAGIC 0x62656572
struct vfsmount *sysfs_mount;
struct super_block * sysfs_sb = NULL;
static struct super_operations sysfs_ops = {
.statfs = simple_statfs,
.drop_inode = generic_delete_inode,
};
static int sysfs_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 = SYSFS_MAGIC;
sb->s_op = &sysfs_ops;
sysfs_sb = sb;
inode = sysfs_new_inode(S_IFDIR | S_IRUGO | S_IWUSR);
if (inode) {
inode->i_op = &simple_dir_inode_operations;
inode->i_fop = &simple_dir_operations;
/* directory inodes start off with i_nlink == 2 (for "." entry) */
inode->i_nlink++;
} else {
pr_debug("sysfs: 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;
}
sb->s_root = root;
return 0;
}
static struct super_block *sysfs_get_sb(struct file_system_type *fs_type,
int flags, char *dev_name, void *data)
{
return get_sb_single(fs_type, flags, data, sysfs_fill_super);
}
static struct file_system_type sysfs_fs_type = {
.name = "sysfs",
.get_sb = sysfs_get_sb,
.kill_sb = kill_litter_super,
};
static int __init sysfs_init(void)
{
int err;
err = register_filesystem(&sysfs_fs_type);
if (!err) {
sysfs_mount = kern_mount(&sysfs_fs_type);
if (IS_ERR(sysfs_mount)) {
printk(KERN_ERR "sysfs: could not mount!\n");
err = PTR_ERR(sysfs_mount);
sysfs_mount = NULL;
}
}
return err;
}
core_initcall(sysfs_init);
/*
* symlink.c - operations for sysfs symlinks.
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include "sysfs.h"
static int init_symlink(struct inode * inode)
{
inode->i_op = &page_symlink_inode_operations;
return 0;
}
static int sysfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
{
int error;
error = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
if (!error) {
int l = strlen(symname)+1;
error = page_symlink(dentry->d_inode, symname, l);
if (error)
iput(dentry->d_inode);
}
return error;
}
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(p->name) + 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(p->name);
/* back up enough to print this bus id with '/' */
length -= cur;
strncpy(buffer + length,p->name,cur);
*(buffer + --length) = '/';
}
}
/**
* sysfs_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 sysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
{
struct dentry * dentry = kobj->dentry;
struct dentry * d;
int error = 0;
int size;
int depth;
char * path;
char * s;
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);
path = kmalloc(size,GFP_KERNEL);
if (!path)
return -ENOMEM;
memset(path,0,size);
for (s = path; depth--; s += 3)
strcpy(s,"../");
fill_object_path(target,path,size);
pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
down(&dentry->d_inode->i_sem);
d = sysfs_get_dentry(dentry,name);
if (!IS_ERR(d))
error = sysfs_symlink(dentry->d_inode,d,path);
else
error = PTR_ERR(d);
up(&dentry->d_inode->i_sem);
kfree(path);
return error;
}
/**
* sysfs_remove_link - remove symlink in object's directory.
* @kobj: object we're acting for.
* @name: name of the symlink to remove.
*/
void sysfs_remove_link(struct kobject * kobj, char * name)
{
sysfs_hash_and_remove(kobj->dentry,name);
}
EXPORT_SYMBOL(sysfs_create_link);
EXPORT_SYMBOL(sysfs_remove_link);
extern struct vfsmount * sysfs_mount;
extern struct inode * sysfs_new_inode(mode_t mode);
extern int sysfs_create(struct dentry *, int mode, int (*init)(struct inode *));
extern struct dentry * sysfs_get_dentry(struct dentry *, char *);
extern void sysfs_hash_and_remove(struct dentry * dir, const char * name);
...@@ -16,6 +16,20 @@ struct attribute { ...@@ -16,6 +16,20 @@ struct attribute {
mode_t mode; mode_t mode;
}; };
struct sysfs_bin_buffer {
u8 * data;
size_t size;
size_t count;
loff_t offset;
};
struct bin_attribute {
struct attribute attr;
size_t size;
ssize_t (*read)(struct kobject *, struct sysfs_bin_buffer *);
ssize_t (*write)(struct kobject *, struct sysfs_bin_buffer *);
};
struct sysfs_ops { struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *,char *); ssize_t (*show)(struct kobject *, struct attribute *,char *);
ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment