1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * file.c - operations for regular (text) files.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <linux/vmalloc.h>
32 #include <asm/uaccess.h>
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
38 * A simple attribute can only be 4096 characters. Why 4k? Because the
39 * original code limited it to PAGE_SIZE. That's a bad idea, though,
40 * because an attribute of 16k on ia64 won't work on x86. So we limit to
41 * 4k, our minimum common page size.
43 #define SIMPLE_ATTR_SIZE 4096
45 struct configfs_buffer {
49 struct configfs_item_operations * ops;
52 bool read_in_progress;
53 bool write_in_progress;
60 * fill_read_buffer - allocate and fill buffer from item.
61 * @dentry: dentry pointer.
62 * @buffer: data buffer for file.
64 * Allocate @buffer->page, if it hasn't been already, then call the
65 * config_item's show() method to fill the buffer with this attribute's
67 * This is called only once, on the file's first read.
69 static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
71 struct configfs_attribute * attr = to_attr(dentry);
72 struct config_item * item = to_item(dentry->d_parent);
77 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
81 count = attr->show(item, buffer->page);
83 BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
85 buffer->needs_read_fill = 0;
86 buffer->count = count;
93 * configfs_read_file - read an attribute.
94 * @file: file pointer.
95 * @buf: buffer to fill.
96 * @count: number of bytes to read.
97 * @ppos: starting offset in file.
99 * Userspace wants to read an attribute file. The attribute descriptor
100 * is in the file's ->d_fsdata. The target item is in the directory's
103 * We call fill_read_buffer() to allocate and fill the buffer from the
104 * item's show() method exactly once (if the read is happening from
105 * the beginning of the file). That should fill the entire buffer with
106 * all the data the item has to offer for that attribute.
107 * We then call flush_read_buffer() to copy the buffer to userspace
108 * in the increments specified.
112 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
114 struct configfs_buffer * buffer = file->private_data;
117 mutex_lock(&buffer->mutex);
118 if (buffer->needs_read_fill) {
119 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
122 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
123 __func__, count, *ppos, buffer->page);
124 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
127 mutex_unlock(&buffer->mutex);
132 * configfs_read_bin_file - read a binary attribute.
133 * @file: file pointer.
134 * @buf: buffer to fill.
135 * @count: number of bytes to read.
136 * @ppos: starting offset in file.
138 * Userspace wants to read a binary attribute file. The attribute
139 * descriptor is in the file's ->d_fsdata. The target item is in the
140 * directory's ->d_fsdata.
142 * We check whether we need to refill the buffer. If so we will
143 * call the attributes' attr->read() twice. The first time we
144 * will pass a NULL as a buffer pointer, which the attributes' method
145 * will use to return the size of the buffer required. If no error
146 * occurs we will allocate the buffer using vmalloc and call
147 * attr->read() again passing that buffer as an argument.
148 * Then we just copy to user-space using simple_read_from_buffer.
152 configfs_read_bin_file(struct file *file, char __user *buf,
153 size_t count, loff_t *ppos)
155 struct configfs_buffer *buffer = file->private_data;
156 struct dentry *dentry = file->f_path.dentry;
157 struct config_item *item = to_item(dentry->d_parent);
158 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
160 ssize_t len = min_t(size_t, count, PAGE_SIZE);
162 mutex_lock(&buffer->mutex);
164 /* we don't support switching read/write modes */
165 if (buffer->write_in_progress) {
169 buffer->read_in_progress = 1;
171 if (buffer->needs_read_fill) {
172 /* perform first read with buf == NULL to get extent */
173 len = bin_attr->read(item, NULL, 0);
179 /* do not exceed the maximum value */
180 if (bin_attr->cb_max_size && len > bin_attr->cb_max_size) {
185 buffer->bin_buffer = vmalloc(len);
186 if (buffer->bin_buffer == NULL) {
190 buffer->bin_buffer_size = len;
192 /* perform second read to fill buffer */
193 len = bin_attr->read(item, buffer->bin_buffer, len);
196 vfree(buffer->bin_buffer);
197 buffer->bin_buffer_size = 0;
198 buffer->bin_buffer = NULL;
202 buffer->needs_read_fill = 0;
205 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
206 buffer->bin_buffer_size);
208 mutex_unlock(&buffer->mutex);
214 * fill_write_buffer - copy buffer from userspace.
215 * @buffer: data buffer for file.
216 * @buf: data from user.
217 * @count: number of bytes in @userbuf.
219 * Allocate @buffer->page if it hasn't been already, then
220 * copy the user-supplied buffer into it.
224 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
229 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
233 if (count >= SIMPLE_ATTR_SIZE)
234 count = SIMPLE_ATTR_SIZE - 1;
235 error = copy_from_user(buffer->page,buf,count);
236 buffer->needs_read_fill = 1;
237 /* if buf is assumed to contain a string, terminate it by \0,
238 * so e.g. sscanf() can scan the string easily */
239 buffer->page[count] = 0;
240 return error ? -EFAULT : count;
245 * flush_write_buffer - push buffer to config_item.
246 * @dentry: dentry to the attribute
247 * @buffer: data buffer for file.
248 * @count: number of bytes
250 * Get the correct pointers for the config_item and the attribute we're
251 * dealing with, then call the store() method for the attribute,
252 * passing the buffer that we acquired in fill_write_buffer().
256 flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
258 struct configfs_attribute * attr = to_attr(dentry);
259 struct config_item * item = to_item(dentry->d_parent);
261 return attr->store(item, buffer->page, count);
266 * configfs_write_file - write an attribute.
267 * @file: file pointer
268 * @buf: data to write
269 * @count: number of bytes
270 * @ppos: starting offset
272 * Similar to configfs_read_file(), though working in the opposite direction.
273 * We allocate and fill the data from the user in fill_write_buffer(),
274 * then push it to the config_item in flush_write_buffer().
275 * There is no easy way for us to know if userspace is only doing a partial
276 * write, so we don't support them. We expect the entire buffer to come
277 * on the first write.
278 * Hint: if you're writing a value, first read the file, modify only the
279 * the value you're changing, then write entire buffer back.
283 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
285 struct configfs_buffer * buffer = file->private_data;
288 mutex_lock(&buffer->mutex);
289 len = fill_write_buffer(buffer, buf, count);
291 len = flush_write_buffer(file->f_path.dentry, buffer, len);
294 mutex_unlock(&buffer->mutex);
299 * configfs_write_bin_file - write a binary attribute.
300 * @file: file pointer
301 * @buf: data to write
302 * @count: number of bytes
303 * @ppos: starting offset
305 * Writing to a binary attribute file is similar to a normal read.
306 * We buffer the consecutive writes (binary attribute files do not
307 * support lseek) in a continuously growing buffer, but we don't
308 * commit until the close of the file.
312 configfs_write_bin_file(struct file *file, const char __user *buf,
313 size_t count, loff_t *ppos)
315 struct configfs_buffer *buffer = file->private_data;
316 struct dentry *dentry = file->f_path.dentry;
317 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
321 mutex_lock(&buffer->mutex);
323 /* we don't support switching read/write modes */
324 if (buffer->read_in_progress) {
328 buffer->write_in_progress = 1;
331 if (*ppos + count > buffer->bin_buffer_size) {
333 if (bin_attr->cb_max_size &&
334 *ppos + count > bin_attr->cb_max_size) {
338 tbuf = vmalloc(*ppos + count);
344 /* copy old contents */
345 if (buffer->bin_buffer) {
346 memcpy(tbuf, buffer->bin_buffer,
347 buffer->bin_buffer_size);
348 vfree(buffer->bin_buffer);
351 /* clear the new area */
352 memset(tbuf + buffer->bin_buffer_size, 0,
353 *ppos + count - buffer->bin_buffer_size);
354 buffer->bin_buffer = tbuf;
355 buffer->bin_buffer_size = *ppos + count;
358 len = simple_write_to_buffer(buffer->bin_buffer,
359 buffer->bin_buffer_size, ppos, buf, count);
361 mutex_unlock(&buffer->mutex);
365 static int check_perm(struct inode * inode, struct file * file, int type)
367 struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
368 struct configfs_attribute * attr = to_attr(file->f_path.dentry);
369 struct configfs_bin_attribute *bin_attr = NULL;
370 struct configfs_buffer * buffer;
371 struct configfs_item_operations * ops = NULL;
377 if (type & CONFIGFS_ITEM_BIN_ATTR)
378 bin_attr = to_bin_attr(file->f_path.dentry);
380 /* Grab the module reference for this attribute if we have one */
381 if (!try_module_get(attr->ca_owner)) {
387 ops = item->ci_type->ct_item_ops;
391 /* File needs write support.
392 * The inode's perms must say it's ok,
393 * and we must have a store method.
395 if (file->f_mode & FMODE_WRITE) {
396 if (!(inode->i_mode & S_IWUGO))
399 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
402 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write)
406 /* File needs read support.
407 * The inode's perms must say it's ok, and we there
408 * must be a show method for it.
410 if (file->f_mode & FMODE_READ) {
411 if (!(inode->i_mode & S_IRUGO))
414 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
417 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read)
421 /* No error? Great, allocate a buffer for the file, and store it
422 * it in file->private_data for easy access.
424 buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
429 mutex_init(&buffer->mutex);
430 buffer->needs_read_fill = 1;
431 buffer->read_in_progress = 0;
432 buffer->write_in_progress = 0;
434 file->private_data = buffer;
443 module_put(attr->ca_owner);
446 config_item_put(item);
450 static int configfs_release(struct inode *inode, struct file *filp)
452 struct config_item * item = to_item(filp->f_path.dentry->d_parent);
453 struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
454 struct module * owner = attr->ca_owner;
455 struct configfs_buffer * buffer = filp->private_data;
458 config_item_put(item);
459 /* After this point, attr should not be accessed. */
464 free_page((unsigned long)buffer->page);
465 mutex_destroy(&buffer->mutex);
471 static int configfs_open_file(struct inode *inode, struct file *filp)
473 return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
476 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
478 return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
481 static int configfs_release_bin_file(struct inode *inode, struct file *filp)
483 struct configfs_buffer *buffer = filp->private_data;
484 struct dentry *dentry = filp->f_path.dentry;
485 struct config_item *item = to_item(dentry->d_parent);
486 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
490 buffer->read_in_progress = 0;
492 if (buffer->write_in_progress) {
493 buffer->write_in_progress = 0;
495 len = bin_attr->write(item, buffer->bin_buffer,
496 buffer->bin_buffer_size);
498 /* vfree on NULL is safe */
499 vfree(buffer->bin_buffer);
500 buffer->bin_buffer = NULL;
501 buffer->bin_buffer_size = 0;
502 buffer->needs_read_fill = 1;
505 ret = configfs_release(inode, filp);
512 const struct file_operations configfs_file_operations = {
513 .read = configfs_read_file,
514 .write = configfs_write_file,
515 .llseek = generic_file_llseek,
516 .open = configfs_open_file,
517 .release = configfs_release,
520 const struct file_operations configfs_bin_file_operations = {
521 .read = configfs_read_bin_file,
522 .write = configfs_write_bin_file,
523 .llseek = NULL, /* bin file is not seekable */
524 .open = configfs_open_bin_file,
525 .release = configfs_release_bin_file,
529 * configfs_create_file - create an attribute file for an item.
530 * @item: item we're creating for.
531 * @attr: atrribute descriptor.
534 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
536 struct dentry *dir = item->ci_dentry;
537 struct configfs_dirent *parent_sd = dir->d_fsdata;
538 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
541 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
542 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
544 inode_unlock(d_inode(dir));
550 * configfs_create_bin_file - create a binary attribute file for an item.
551 * @item: item we're creating for.
552 * @attr: atrribute descriptor.
555 int configfs_create_bin_file(struct config_item *item,
556 const struct configfs_bin_attribute *bin_attr)
558 struct dentry *dir = item->ci_dentry;
559 struct configfs_dirent *parent_sd = dir->d_fsdata;
560 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
563 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
564 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
565 CONFIGFS_ITEM_BIN_ATTR);
566 inode_unlock(dir->d_inode);