Merge commit 'gcl/gcl-next' into next
[deliverable/linux.git] / fs / sysfs / bin.c
CommitLineData
1da177e4 1/*
6d66f5cd 2 * fs/sysfs/bin.c - sysfs binary file implementation
1da177e4
LT
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
6d66f5cd
TH
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
9 *
10 * This file is released under the GPLv2.
11 *
12 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
13 */
14
15#undef DEBUG
16
17#include <linux/errno.h>
18#include <linux/fs.h>
995982ca 19#include <linux/kernel.h>
1da177e4
LT
20#include <linux/kobject.h>
21#include <linux/module.h>
22#include <linux/slab.h>
869512ab 23#include <linux/mutex.h>
1da177e4
LT
24
25#include <asm/uaccess.h>
26
27#include "sysfs.h"
28
eb361653
TH
29struct bin_buffer {
30 struct mutex mutex;
31 void *buffer;
0ab66088 32 int mmapped;
eb361653
TH
33};
34
1da177e4
LT
35static int
36fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
37{
3e519038 38 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61
TH
39 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
40 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
0ab66088
TH
41 int rc;
42
43 /* need attr_sd for attr, its parent for kobj */
44 if (!sysfs_get_active_two(attr_sd))
45 return -ENODEV;
1da177e4 46
0ab66088
TH
47 rc = -EIO;
48 if (attr->read)
91a69029 49 rc = attr->read(kobj, attr, buffer, off, count);
1da177e4 50
0ab66088
TH
51 sysfs_put_active_two(attr_sd);
52
53 return rc;
1da177e4
LT
54}
55
56static ssize_t
93e3cd82 57read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
1da177e4 58{
eb361653 59 struct bin_buffer *bb = file->private_data;
f427f5d5 60 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
61 int size = dentry->d_inode->i_size;
62 loff_t offs = *off;
93e3cd82 63 int count = min_t(size_t, bytes, PAGE_SIZE);
b31ca3f5 64 char *temp;
1da177e4
LT
65
66 if (size) {
67 if (offs > size)
68 return 0;
69 if (offs + count > size)
70 count = size - offs;
71 }
72
b31ca3f5
NP
73 temp = kmalloc(count, GFP_KERNEL);
74 if (!temp)
75 return -ENOMEM;
76
eb361653
TH
77 mutex_lock(&bb->mutex);
78
79 count = fill_read(dentry, bb->buffer, offs, count);
b31ca3f5
NP
80 if (count < 0) {
81 mutex_unlock(&bb->mutex);
82 goto out_free;
83 }
1da177e4 84
b31ca3f5
NP
85 memcpy(temp, bb->buffer, count);
86
87 mutex_unlock(&bb->mutex);
88
89 if (copy_to_user(userbuf, temp, count)) {
eb361653 90 count = -EFAULT;
b31ca3f5 91 goto out_free;
eb361653 92 }
1da177e4 93
93e3cd82 94 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
1da177e4
LT
95
96 *off = offs + count;
97
b31ca3f5
NP
98 out_free:
99 kfree(temp);
1da177e4
LT
100 return count;
101}
102
103static int
104flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
105{
3e519038 106 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
b1fc3d61
TH
107 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
108 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
0ab66088
TH
109 int rc;
110
111 /* need attr_sd for attr, its parent for kobj */
112 if (!sysfs_get_active_two(attr_sd))
113 return -ENODEV;
1da177e4 114
0ab66088
TH
115 rc = -EIO;
116 if (attr->write)
91a69029 117 rc = attr->write(kobj, attr, buffer, offset, count);
1da177e4 118
0ab66088
TH
119 sysfs_put_active_two(attr_sd);
120
121 return rc;
1da177e4
LT
122}
123
93e3cd82
TH
124static ssize_t write(struct file *file, const char __user *userbuf,
125 size_t bytes, loff_t *off)
1da177e4 126{
eb361653 127 struct bin_buffer *bb = file->private_data;
f427f5d5 128 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
129 int size = dentry->d_inode->i_size;
130 loff_t offs = *off;
93e3cd82 131 int count = min_t(size_t, bytes, PAGE_SIZE);
b31ca3f5 132 char *temp;
1da177e4 133
1da177e4
LT
134 if (size) {
135 if (offs > size)
136 return 0;
137 if (offs + count > size)
138 count = size - offs;
139 }
140
b31ca3f5
NP
141 temp = kmalloc(count, GFP_KERNEL);
142 if (!temp)
143 return -ENOMEM;
eb361653 144
b31ca3f5 145 if (copy_from_user(temp, userbuf, count)) {
eb361653 146 count = -EFAULT;
b31ca3f5 147 goto out_free;
eb361653 148 }
1da177e4 149
b31ca3f5
NP
150 mutex_lock(&bb->mutex);
151
152 memcpy(bb->buffer, temp, count);
153
eb361653 154 count = flush_write(dentry, bb->buffer, offs, count);
b31ca3f5
NP
155 mutex_unlock(&bb->mutex);
156
1da177e4
LT
157 if (count > 0)
158 *off = offs + count;
eb361653 159
b31ca3f5
NP
160out_free:
161 kfree(temp);
1da177e4
LT
162 return count;
163}
164
165static int mmap(struct file *file, struct vm_area_struct *vma)
166{
eb361653 167 struct bin_buffer *bb = file->private_data;
3e519038 168 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61
TH
169 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
170 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
eb361653 171 int rc;
1da177e4 172
eb361653 173 mutex_lock(&bb->mutex);
0ab66088
TH
174
175 /* need attr_sd for attr, its parent for kobj */
176 if (!sysfs_get_active_two(attr_sd))
177 return -ENODEV;
178
179 rc = -EINVAL;
180 if (attr->mmap)
181 rc = attr->mmap(kobj, attr, vma);
182
183 if (rc == 0 && !bb->mmapped)
184 bb->mmapped = 1;
185 else
186 sysfs_put_active_two(attr_sd);
187
eb361653
TH
188 mutex_unlock(&bb->mutex);
189
190 return rc;
1da177e4
LT
191}
192
193static int open(struct inode * inode, struct file * file)
194{
3e519038 195 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
b1fc3d61 196 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
eb361653 197 struct bin_buffer *bb = NULL;
0ab66088 198 int error;
1da177e4 199
078ce640
TH
200 /* binary file operations requires both @sd and its parent */
201 if (!sysfs_get_active_two(attr_sd))
0ab66088 202 return -ENODEV;
1da177e4 203
1da177e4
LT
204 error = -EACCES;
205 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
7b595756 206 goto err_out;
1da177e4 207 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
7b595756 208 goto err_out;
1da177e4
LT
209
210 error = -ENOMEM;
eb361653
TH
211 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
212 if (!bb)
7b595756 213 goto err_out;
1da177e4 214
eb361653
TH
215 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
216 if (!bb->buffer)
7b595756 217 goto err_out;
eb361653
TH
218
219 mutex_init(&bb->mutex);
220 file->private_data = bb;
221
078ce640
TH
222 /* open succeeded, put active references */
223 sysfs_put_active_two(attr_sd);
0ab66088 224 return 0;
1da177e4 225
7b595756 226 err_out:
078ce640 227 sysfs_put_active_two(attr_sd);
0ab66088 228 kfree(bb);
1da177e4
LT
229 return error;
230}
231
232static int release(struct inode * inode, struct file * file)
233{
3e519038 234 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
eb361653 235 struct bin_buffer *bb = file->private_data;
1da177e4 236
0ab66088
TH
237 if (bb->mmapped)
238 sysfs_put_active_two(attr_sd);
eb361653
TH
239 kfree(bb->buffer);
240 kfree(bb);
1da177e4
LT
241 return 0;
242}
243
4b6f5d20 244const struct file_operations bin_fops = {
1da177e4
LT
245 .read = read,
246 .write = write,
247 .mmap = mmap,
248 .llseek = generic_file_llseek,
249 .open = open,
250 .release = release,
251};
252
253/**
254 * sysfs_create_bin_file - create binary file for object.
255 * @kobj: object.
256 * @attr: attribute descriptor.
1da177e4
LT
257 */
258
259int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
260{
608e266a 261 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 262
608e266a 263 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
1da177e4
LT
264}
265
266
267/**
268 * sysfs_remove_bin_file - remove binary file for object.
269 * @kobj: object.
270 * @attr: attribute descriptor.
1da177e4
LT
271 */
272
995982ca 273void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
1da177e4 274{
5f1835da 275 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
1da177e4
LT
276}
277
278EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
279EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
This page took 0.476615 seconds and 5 git commands to generate.