bin.c 5.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
T
Tejun Heo 已提交
2
 * fs/sysfs/bin.c - sysfs binary file implementation
L
Linus Torvalds 已提交
3 4 5 6
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Matthew Wilcox
 * Copyright (c) 2004 Silicon Graphics, Inc.
T
Tejun Heo 已提交
7 8 9 10 11 12
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
 *
 * This file is released under the GPLv2.
 *
 * Please see Documentation/filesystems/sysfs.txt for more information.
L
Linus Torvalds 已提交
13 14 15 16 17 18
 */

#undef DEBUG

#include <linux/errno.h>
#include <linux/fs.h>
19
#include <linux/kernel.h>
L
Linus Torvalds 已提交
20 21 22
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/slab.h>
D
Dave Young 已提交
23
#include <linux/mutex.h>
L
Linus Torvalds 已提交
24 25 26 27 28

#include <asm/uaccess.h>

#include "sysfs.h"

T
Tejun Heo 已提交
29 30 31
struct bin_buffer {
	struct mutex	mutex;
	void		*buffer;
32
	int		mmapped;
T
Tejun Heo 已提交
33 34
};

L
Linus Torvalds 已提交
35 36 37
static int
fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
{
38
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
T
Tejun Heo 已提交
39 40
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
	struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
41 42 43 44 45
	int rc;

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

47 48
	rc = -EIO;
	if (attr->read)
49
		rc = attr->read(kobj, attr, buffer, off, count);
L
Linus Torvalds 已提交
50

51 52 53
	sysfs_put_active_two(attr_sd);

	return rc;
L
Linus Torvalds 已提交
54 55 56
}

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

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

T
Tejun Heo 已提交
72 73 74
	mutex_lock(&bb->mutex);

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

T
Tejun Heo 已提交
78 79 80 81
	if (copy_to_user(userbuf, bb->buffer, count)) {
		count = -EFAULT;
		goto out_unlock;
	}
L
Linus Torvalds 已提交
82

83
	pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
L
Linus Torvalds 已提交
84 85 86

	*off = offs + count;

T
Tejun Heo 已提交
87 88
 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
89 90 91 92 93 94
	return count;
}

static int
flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
{
95
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
T
Tejun Heo 已提交
96 97
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
	struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
98 99 100 101 102
	int rc;

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

104 105
	rc = -EIO;
	if (attr->write)
106
		rc = attr->write(kobj, attr, buffer, offset, count);
L
Linus Torvalds 已提交
107

108 109 110
	sysfs_put_active_two(attr_sd);

	return rc;
L
Linus Torvalds 已提交
111 112
}

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

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

T
Tejun Heo 已提交
129 130 131 132 133 134
	mutex_lock(&bb->mutex);

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

T
Tejun Heo 已提交
136
	count = flush_write(dentry, bb->buffer, offs, count);
L
Linus Torvalds 已提交
137 138
	if (count > 0)
		*off = offs + count;
T
Tejun Heo 已提交
139 140 141

 out_unlock:
	mutex_unlock(&bb->mutex);
L
Linus Torvalds 已提交
142 143 144 145 146
	return count;
}

static int mmap(struct file *file, struct vm_area_struct *vma)
{
T
Tejun Heo 已提交
147
	struct bin_buffer *bb = file->private_data;
148
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
T
Tejun Heo 已提交
149 150
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
	struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
T
Tejun Heo 已提交
151
	int rc;
L
Linus Torvalds 已提交
152

T
Tejun Heo 已提交
153
	mutex_lock(&bb->mutex);
154 155 156 157 158 159 160 161 162 163 164 165 166 167

	/* 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 已提交
168 169 170
	mutex_unlock(&bb->mutex);

	return rc;
L
Linus Torvalds 已提交
171 172 173 174
}

static int open(struct inode * inode, struct file * file)
{
175
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
T
Tejun Heo 已提交
176
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
T
Tejun Heo 已提交
177
	struct bin_buffer *bb = NULL;
178
	int error;
L
Linus Torvalds 已提交
179

180 181
	/* binary file operations requires both @sd and its parent */
	if (!sysfs_get_active_two(attr_sd))
182
		return -ENODEV;
L
Linus Torvalds 已提交
183 184 185

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

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

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

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

202 203
	/* open succeeded, put active references */
	sysfs_put_active_two(attr_sd);
204
	return 0;
L
Linus Torvalds 已提交
205

206
 err_out:
207
	sysfs_put_active_two(attr_sd);
208
	kfree(bb);
L
Linus Torvalds 已提交
209 210 211 212 213
	return error;
}

static int release(struct inode * inode, struct file * file)
{
214
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
T
Tejun Heo 已提交
215
	struct bin_buffer *bb = file->private_data;
L
Linus Torvalds 已提交
216

217 218
	if (bb->mmapped)
		sysfs_put_active_two(attr_sd);
T
Tejun Heo 已提交
219 220
	kfree(bb->buffer);
	kfree(bb);
L
Linus Torvalds 已提交
221 222 223
	return 0;
}

224
const struct file_operations bin_fops = {
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	.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)
{
241
	BUG_ON(!kobj || !kobj->sd || !attr);
L
Linus Torvalds 已提交
242

243
	return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252
}


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

253
void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
L
Linus Torvalds 已提交
254
{
255
	sysfs_hash_and_remove(kobj->sd, attr->attr.name);
L
Linus Torvalds 已提交
256 257 258 259
}

EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);