bin.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * bin.c - binary file operations for sysfs.
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Matthew Wilcox
 * Copyright (c) 2004 Silicon Graphics, Inc.
 */

#undef DEBUG

#include <linux/errno.h>
#include <linux/fs.h>
13
#include <linux/kernel.h>
L
Linus Torvalds 已提交
14 15 16 17 18
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/slab.h>

#include <asm/uaccess.h>
19
#include <asm/semaphore.h>
L
Linus Torvalds 已提交
20 21 22

#include "sysfs.h"

T
Tejun Heo 已提交
23 24 25 26 27
struct bin_buffer {
	struct mutex	mutex;
	void		*buffer;
};

L
Linus Torvalds 已提交
28 29 30
static int
fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
{
31 32
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
L
Linus Torvalds 已提交
33 34 35
	struct kobject * kobj = to_kobj(dentry->d_parent);

	if (!attr->read)
36
		return -EIO;
L
Linus Torvalds 已提交
37 38 39 40 41

	return attr->read(kobj, buffer, off, count);
}

static ssize_t
42
read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
L
Linus Torvalds 已提交
43
{
T
Tejun Heo 已提交
44
	struct bin_buffer *bb = file->private_data;
45
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
46 47
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
48
	int count = min_t(size_t, bytes, PAGE_SIZE);
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56

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

T
Tejun Heo 已提交
57 58 59
	mutex_lock(&bb->mutex);

	count = fill_read(dentry, bb->buffer, offs, count);
60
	if (count < 0)
T
Tejun Heo 已提交
61
		goto out_unlock;
L
Linus Torvalds 已提交
62

T
Tejun Heo 已提交
63 64 65 66
	if (copy_to_user(userbuf, bb->buffer, count)) {
		count = -EFAULT;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
67

68
	pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
L
Linus Torvalds 已提交
69 70 71

	*off = offs + count;

T
Tejun Heo 已提交
72 73
 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
74 75 76 77 78 79
	return count;
}

static int
flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
{
80 81
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
L
Linus Torvalds 已提交
82 83 84
	struct kobject *kobj = to_kobj(dentry->d_parent);

	if (!attr->write)
85
		return -EIO;
L
Linus Torvalds 已提交
86 87 88 89

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

90 91
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
L
Linus Torvalds 已提交
92
{
T
Tejun Heo 已提交
93
	struct bin_buffer *bb = file->private_data;
94
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
95 96
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
97
	int count = min_t(size_t, bytes, PAGE_SIZE);
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105

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

T
Tejun Heo 已提交
106 107 108 109 110 111
	mutex_lock(&bb->mutex);

	if (copy_from_user(bb->buffer, userbuf, count)) {
		count = -EFAULT;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
112

T
Tejun Heo 已提交
113
	count = flush_write(dentry, bb->buffer, offs, count);
L
Linus Torvalds 已提交
114 115
	if (count > 0)
		*off = offs + count;
T
Tejun Heo 已提交
116 117 118

 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
119 120 121 122 123
	return count;
}

static int mmap(struct file *file, struct vm_area_struct *vma)
{
T
Tejun Heo 已提交
124
	struct bin_buffer *bb = file->private_data;
125 126 127
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
	struct kobject *kobj = to_kobj(file->f_path.dentry->d_parent);
T
Tejun Heo 已提交
128
	int rc;
L
Linus Torvalds 已提交
129 130 131 132

	if (!attr->mmap)
		return -EINVAL;

T
Tejun Heo 已提交
133 134 135 136 137
	mutex_lock(&bb->mutex);
	rc = attr->mmap(kobj, attr, vma);
	mutex_unlock(&bb->mutex);

	return rc;
L
Linus Torvalds 已提交
138 139 140 141
}

static int open(struct inode * inode, struct file * file)
{
142
	struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
143 144
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
T
Tejun Heo 已提交
145
	struct bin_buffer *bb = NULL;
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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 || attr->mmap))
		goto Error;
	if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
		goto Error;

	error = -ENOMEM;
T
Tejun Heo 已提交
163 164
	bb = kzalloc(sizeof(*bb), GFP_KERNEL);
	if (!bb)
L
Linus Torvalds 已提交
165 166
		goto Error;

T
Tejun Heo 已提交
167 168 169 170 171 172 173
	bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!bb->buffer)
		goto Error;

	mutex_init(&bb->mutex);
	file->private_data = bb;

L
Linus Torvalds 已提交
174
	error = 0;
T
Tejun Heo 已提交
175
	goto Done;
L
Linus Torvalds 已提交
176 177

 Error:
T
Tejun Heo 已提交
178
	kfree(bb);
L
Linus Torvalds 已提交
179 180
	module_put(attr->attr.owner);
 Done:
M
Mariusz Kozlowski 已提交
181
	if (error)
L
Linus Torvalds 已提交
182 183 184 185 186 187
		kobject_put(kobj);
	return error;
}

static int release(struct inode * inode, struct file * file)
{
188
	struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
189 190
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
T
Tejun Heo 已提交
191
	struct bin_buffer *bb = file->private_data;
L
Linus Torvalds 已提交
192

M
Mariusz Kozlowski 已提交
193
	kobject_put(kobj);
L
Linus Torvalds 已提交
194
	module_put(attr->attr.owner);
T
Tejun Heo 已提交
195 196
	kfree(bb->buffer);
	kfree(bb);
L
Linus Torvalds 已提交
197 198 199
	return 0;
}

200
const struct file_operations bin_fops = {
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	.read		= read,
	.write		= write,
	.mmap		= mmap,
	.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)
{
	BUG_ON(!kobj || !kobj->dentry || !attr);

	return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
}


/**
 *	sysfs_remove_bin_file - remove binary file for object.
 *	@kobj:	object.
 *	@attr:	attribute descriptor.
 */

229
void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
L
Linus Torvalds 已提交
230
{
231 232 233 234 235 236
	if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
		printk(KERN_ERR "%s: "
			"bad dentry or inode or no such file: \"%s\"\n",
			__FUNCTION__, attr->attr.name);
		dump_stack();
	}
L
Linus Torvalds 已提交
237 238 239 240
}

EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);