bin.c 3.76 KB
Newer Older
1 2 3 4
/*
 * bin.c - binary file operations for sysfs.
 */

5 6
#undef DEBUG

7
#include <linux/errno.h>
8 9
#include <linux/fs.h>
#include <linux/kobject.h>
10 11
#include <linux/module.h>
#include <linux/slab.h>
12

13 14
#include <asm/uaccess.h>

15 16
#include "sysfs.h"

17 18
static int
fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
19
{
20 21
	struct bin_attribute * attr = to_bin_attr(dentry);
	struct kobject * kobj = to_kobj(dentry->d_parent);
22

23
	return attr->read(kobj, buffer, off, count);
24 25 26
}

static ssize_t
27
read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
28
{
29 30 31 32
	char *buffer = file->private_data;
	struct dentry *dentry = file->f_dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
33 34
	int ret;

35 36 37 38 39 40 41 42 43
	if (count > PAGE_SIZE)
		count = PAGE_SIZE;

	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}
44

45 46
	ret = fill_read(dentry, buffer, offs, count);
	if (ret < 0) 
47
		return ret;
48
	count = ret;
49

Andrew Morton's avatar
Andrew Morton committed
50
	if (copy_to_user(userbuf, buffer, count))
51
		return -EFAULT;
52

53
	pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
54

55
	*off = offs + count;
56

57
	return count;
58 59
}

60 61
static int
flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
62
{
63 64
	struct bin_attribute *attr = to_bin_attr(dentry);
	struct kobject *kobj = to_kobj(dentry->d_parent);
65

66
	return attr->write(kobj, buffer, offset, count);
67 68
}

69
static ssize_t write(struct file * file, const char __user * userbuf,
70 71
		     size_t count, loff_t * off)
{
72 73 74 75
	char *buffer = file->private_data;
	struct dentry *dentry = file->f_dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
76

77 78 79 80 81 82 83 84
	if (count > PAGE_SIZE)
		count = PAGE_SIZE;
	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}
85

Andrew Morton's avatar
Andrew Morton committed
86
	if (copy_from_user(buffer, userbuf, count))
87
		return -EFAULT;
88

89 90 91
	count = flush_write(dentry, buffer, offs, count);
	if (count > 0)
		*off = offs + count;
92
	return count;
93 94
}

95
static int open(struct inode * inode, struct file * file)
96
{
97
	struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
98
	struct bin_attribute * attr = to_bin_attr(file->f_dentry);
99
	int error = -EINVAL;
100 101

	if (!kobj || !attr)
102 103
		goto Done;

104 105 106 107 108
	/* Grab the module reference for this attribute if we have one */
	error = -ENODEV;
	if (!try_module_get(attr->attr.owner)) 
		goto Done;

109
	error = -EACCES;
110
	if ((file->f_mode & FMODE_WRITE) && !attr->write)
111
		goto Error;
112
	if ((file->f_mode & FMODE_READ) && !attr->read)
113
		goto Error;
114 115

	error = -ENOMEM;
116
	file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
117
	if (!file->private_data)
118
		goto Error;
119 120

	error = 0;
121
    goto Done;
122

123 124
 Error:
	module_put(attr->attr.owner);
125 126 127 128 129 130 131 132
 Done:
	if (error && kobj)
		kobject_put(kobj);
	return error;
}

static int release(struct inode * inode, struct file * file)
{
133 134
	struct kobject * kobj = to_kobj(file->f_dentry->d_parent);
	struct bin_attribute * attr = to_bin_attr(file->f_dentry);
135 136 137 138
	u8 * buffer = file->private_data;

	if (kobj) 
		kobject_put(kobj);
139
	module_put(attr->attr.owner);
140
	kfree(buffer);
141 142 143
	return 0;
}

144
struct file_operations bin_fops = {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	.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)
{
161
	BUG_ON(!kobj || !kobj->dentry || !attr);
162

163
	return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
164 165 166 167 168 169 170 171 172 173 174 175 176
}


/**
 *	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);
Patrick Mochel's avatar
Patrick Mochel committed
177
	return 0;
178 179
}

180 181
EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);