bin.c 5.9 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);
N
Nick Piggin 已提交
64
	char *temp;
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72

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

N
Nick Piggin 已提交
73 74 75 76
	temp = kmalloc(count, GFP_KERNEL);
	if (!temp)
		return -ENOMEM;

T
Tejun Heo 已提交
77 78 79
	mutex_lock(&bb->mutex);

	count = fill_read(dentry, bb->buffer, offs, count);
N
Nick Piggin 已提交
80 81 82 83
	if (count < 0) {
		mutex_unlock(&bb->mutex);
		goto out_free;
	}
L
Linus Torvalds 已提交
84

N
Nick Piggin 已提交
85 86 87 88 89
	memcpy(temp, bb->buffer, count);

	mutex_unlock(&bb->mutex);

	if (copy_to_user(userbuf, temp, count)) {
T
Tejun Heo 已提交
90
		count = -EFAULT;
N
Nick Piggin 已提交
91
		goto out_free;
T
Tejun Heo 已提交
92
	}
L
Linus Torvalds 已提交
93

94
	pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
L
Linus Torvalds 已提交
95 96 97

	*off = offs + count;

N
Nick Piggin 已提交
98 99
 out_free:
	kfree(temp);
L
Linus Torvalds 已提交
100 101 102 103 104 105
	return count;
}

static int
flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
{
106
	struct sysfs_dirent *attr_sd = dentry->d_fsdata;
T
Tejun Heo 已提交
107 108
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
	struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
109 110 111 112 113
	int rc;

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

115 116
	rc = -EIO;
	if (attr->write)
117
		rc = attr->write(kobj, attr, buffer, offset, count);
L
Linus Torvalds 已提交
118

119 120 121
	sysfs_put_active_two(attr_sd);

	return rc;
L
Linus Torvalds 已提交
122 123
}

124 125
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
L
Linus Torvalds 已提交
126
{
T
Tejun Heo 已提交
127
	struct bin_buffer *bb = file->private_data;
128
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
129 130
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
131
	int count = min_t(size_t, bytes, PAGE_SIZE);
N
Nick Piggin 已提交
132
	char *temp;
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140

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

N
Nick Piggin 已提交
141 142 143
	temp = kmalloc(count, GFP_KERNEL);
	if (!temp)
		return -ENOMEM;
T
Tejun Heo 已提交
144

N
Nick Piggin 已提交
145
	if (copy_from_user(temp, userbuf, count)) {
T
Tejun Heo 已提交
146
		count = -EFAULT;
N
Nick Piggin 已提交
147
		goto out_free;
T
Tejun Heo 已提交
148
	}
L
Linus Torvalds 已提交
149

N
Nick Piggin 已提交
150 151 152 153
	mutex_lock(&bb->mutex);

	memcpy(bb->buffer, temp, count);

T
Tejun Heo 已提交
154
	count = flush_write(dentry, bb->buffer, offs, count);
N
Nick Piggin 已提交
155 156
	mutex_unlock(&bb->mutex);

L
Linus Torvalds 已提交
157 158
	if (count > 0)
		*off = offs + count;
T
Tejun Heo 已提交
159

N
Nick Piggin 已提交
160 161
out_free:
	kfree(temp);
L
Linus Torvalds 已提交
162 163 164 165 166
	return count;
}

static int mmap(struct file *file, struct vm_area_struct *vma)
{
T
Tejun Heo 已提交
167
	struct bin_buffer *bb = file->private_data;
168
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
T
Tejun Heo 已提交
169 170
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
	struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
T
Tejun Heo 已提交
171
	int rc;
L
Linus Torvalds 已提交
172

T
Tejun Heo 已提交
173
	mutex_lock(&bb->mutex);
174 175 176 177 178 179 180 181 182 183 184 185 186 187

	/* 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 已提交
188 189 190
	mutex_unlock(&bb->mutex);

	return rc;
L
Linus Torvalds 已提交
191 192 193 194
}

static int open(struct inode * inode, struct file * file)
{
195
	struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
T
Tejun Heo 已提交
196
	struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
T
Tejun Heo 已提交
197
	struct bin_buffer *bb = NULL;
198
	int error;
L
Linus Torvalds 已提交
199

200 201
	/* binary file operations requires both @sd and its parent */
	if (!sysfs_get_active_two(attr_sd))
202
		return -ENODEV;
L
Linus Torvalds 已提交
203 204 205

	error = -EACCES;
	if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
206
		goto err_out;
L
Linus Torvalds 已提交
207
	if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
208
		goto err_out;
L
Linus Torvalds 已提交
209 210

	error = -ENOMEM;
T
Tejun Heo 已提交
211 212
	bb = kzalloc(sizeof(*bb), GFP_KERNEL);
	if (!bb)
213
		goto err_out;
L
Linus Torvalds 已提交
214

T
Tejun Heo 已提交
215 216
	bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!bb->buffer)
217
		goto err_out;
T
Tejun Heo 已提交
218 219 220 221

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

222 223
	/* open succeeded, put active references */
	sysfs_put_active_two(attr_sd);
224
	return 0;
L
Linus Torvalds 已提交
225

226
 err_out:
227
	sysfs_put_active_two(attr_sd);
228
	kfree(bb);
L
Linus Torvalds 已提交
229 230 231 232 233
	return error;
}

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

237 238
	if (bb->mmapped)
		sysfs_put_active_two(attr_sd);
T
Tejun Heo 已提交
239 240
	kfree(bb->buffer);
	kfree(bb);
L
Linus Torvalds 已提交
241 242 243
	return 0;
}

244
const struct file_operations bin_fops = {
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	.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)
{
261
	BUG_ON(!kobj || !kobj->sd || !attr);
L
Linus Torvalds 已提交
262

263
	return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272
}


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

273
void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
L
Linus Torvalds 已提交
274
{
275
	sysfs_hash_and_remove(kobj->sd, attr->attr.name);
L
Linus Torvalds 已提交
276 277 278 279
}

EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);