bin.c 5.8 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
struct bin_buffer {
	struct mutex	mutex;
	void		*buffer;
26
	int		mmapped;
T
Tejun Heo 已提交
27 28
};

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

	/* need attr_sd for attr, its parent for kobj */
	if (!sysfs_get_active_two(attr_sd))
		return -ENODEV;
L
Linus Torvalds 已提交
40

41 42 43
	rc = -EIO;
	if (attr->read)
		rc = attr->read(kobj, buffer, off, count);
L
Linus Torvalds 已提交
44

45 46 47
	sysfs_put_active_two(attr_sd);

	return rc;
L
Linus Torvalds 已提交
48 49 50
}

static ssize_t
51
read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
L
Linus Torvalds 已提交
52
{
T
Tejun Heo 已提交
53
	struct bin_buffer *bb = file->private_data;
54
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
55 56
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
57
	int count = min_t(size_t, bytes, PAGE_SIZE);
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65

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

T
Tejun Heo 已提交
66 67 68
	mutex_lock(&bb->mutex);

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

T
Tejun Heo 已提交
72 73 74 75
	if (copy_to_user(userbuf, bb->buffer, count)) {
		count = -EFAULT;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
76

77
	pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
L
Linus Torvalds 已提交
78 79 80

	*off = offs + count;

T
Tejun Heo 已提交
81 82
 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
83 84 85 86 87 88
	return count;
}

static int
flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
{
89 90
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
91 92 93 94 95 96
	struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
	int rc;

	/* need attr_sd for attr, its parent for kobj */
	if (!sysfs_get_active_two(attr_sd))
		return -ENODEV;
L
Linus Torvalds 已提交
97

98 99 100
	rc = -EIO;
	if (attr->write)
		rc = attr->write(kobj, buffer, offset, count);
L
Linus Torvalds 已提交
101

102 103 104
	sysfs_put_active_two(attr_sd);

	return rc;
L
Linus Torvalds 已提交
105 106
}

107 108
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
L
Linus Torvalds 已提交
109
{
T
Tejun Heo 已提交
110
	struct bin_buffer *bb = file->private_data;
111
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
112 113
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
114
	int count = min_t(size_t, bytes, PAGE_SIZE);
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122

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

T
Tejun Heo 已提交
123 124 125 126 127 128
	mutex_lock(&bb->mutex);

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

T
Tejun Heo 已提交
130
	count = flush_write(dentry, bb->buffer, offs, count);
L
Linus Torvalds 已提交
131 132
	if (count > 0)
		*off = offs + count;
T
Tejun Heo 已提交
133 134 135

 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
136 137 138 139 140
	return count;
}

static int mmap(struct file *file, struct vm_area_struct *vma)
{
T
Tejun Heo 已提交
141
	struct bin_buffer *bb = file->private_data;
142 143
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
	struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
144
	struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
T
Tejun Heo 已提交
145
	int rc;
L
Linus Torvalds 已提交
146

T
Tejun Heo 已提交
147
	mutex_lock(&bb->mutex);
148 149 150 151 152 153 154 155 156 157 158 159 160 161

	/* need attr_sd for attr, its parent for kobj */
	if (!sysfs_get_active_two(attr_sd))
		return -ENODEV;

	rc = -EINVAL;
	if (attr->mmap)
		rc = attr->mmap(kobj, attr, vma);

	if (rc == 0 && !bb->mmapped)
		bb->mmapped = 1;
	else
		sysfs_put_active_two(attr_sd);

T
Tejun Heo 已提交
162 163 164
	mutex_unlock(&bb->mutex);

	return rc;
L
Linus Torvalds 已提交
165 166 167 168
}

static int open(struct inode * inode, struct file * file)
{
169 170
	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 已提交
171
	struct bin_buffer *bb = NULL;
172
	int error;
L
Linus Torvalds 已提交
173

174 175 176
	/* need attr_sd for attr */
	if (!sysfs_get_active(attr_sd))
		return -ENODEV;
L
Linus Torvalds 已提交
177

178
	/* Grab the module reference for this attribute */
L
Linus Torvalds 已提交
179
	error = -ENODEV;
180 181
	if (!try_module_get(attr->attr.owner))
		goto err_sput;
L
Linus Torvalds 已提交
182 183 184

	error = -EACCES;
	if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
185
		goto err_mput;
L
Linus Torvalds 已提交
186
	if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
187
		goto err_mput;
L
Linus Torvalds 已提交
188 189

	error = -ENOMEM;
T
Tejun Heo 已提交
190 191
	bb = kzalloc(sizeof(*bb), GFP_KERNEL);
	if (!bb)
192
		goto err_mput;
L
Linus Torvalds 已提交
193

T
Tejun Heo 已提交
194 195
	bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!bb->buffer)
196
		goto err_mput;
T
Tejun Heo 已提交
197 198 199 200

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

201 202 203 204
	/* open succeeded, put active reference and pin attr_sd */
	sysfs_put_active(attr_sd);
	sysfs_get(attr_sd);
	return 0;
L
Linus Torvalds 已提交
205

206
 err_mput:
L
Linus Torvalds 已提交
207
	module_put(attr->attr.owner);
208 209 210
 err_sput:
	sysfs_put_active(attr_sd);
	kfree(bb);
L
Linus Torvalds 已提交
211 212 213 214 215
	return error;
}

static int release(struct inode * inode, struct file * file)
{
216 217
	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 已提交
218
	struct bin_buffer *bb = file->private_data;
L
Linus Torvalds 已提交
219

220 221 222
	if (bb->mmapped)
		sysfs_put_active_two(attr_sd);
	sysfs_put(attr_sd);
L
Linus Torvalds 已提交
223
	module_put(attr->attr.owner);
T
Tejun Heo 已提交
224 225
	kfree(bb->buffer);
	kfree(bb);
L
Linus Torvalds 已提交
226 227 228
	return 0;
}

229
const struct file_operations bin_fops = {
L
Linus Torvalds 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	.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.
 */

258
void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
L
Linus Torvalds 已提交
259
{
260 261 262 263 264 265
	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 已提交
266 267 268 269
}

EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);