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 <linux/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
;
57 struct config_item
*item
;
60 struct configfs_attribute
*attr
;
61 struct configfs_bin_attribute
*bin_attr
;
65 static inline struct configfs_fragment
*to_frag(struct file
*file
)
67 struct configfs_dirent
*sd
= file
->f_path
.dentry
->d_fsdata
;
72 static int fill_read_buffer(struct file
*file
, struct configfs_buffer
*buffer
)
74 struct configfs_fragment
*frag
= to_frag(file
);
75 ssize_t count
= -ENOENT
;
78 buffer
->page
= (char *) get_zeroed_page(GFP_KERNEL
);
82 down_read(&frag
->frag_sem
);
84 count
= buffer
->attr
->show(buffer
->item
, buffer
->page
);
85 up_read(&frag
->frag_sem
);
89 if (WARN_ON_ONCE(count
> (ssize_t
)SIMPLE_ATTR_SIZE
))
91 buffer
->needs_read_fill
= 0;
92 buffer
->count
= count
;
97 * configfs_read_file - read an attribute.
98 * @file: file pointer.
99 * @buf: buffer to fill.
100 * @count: number of bytes to read.
101 * @ppos: starting offset in file.
103 * Userspace wants to read an attribute file. The attribute descriptor
104 * is in the file's ->d_fsdata. The target item is in the directory's
107 * We call fill_read_buffer() to allocate and fill the buffer from the
108 * item's show() method exactly once (if the read is happening from
109 * the beginning of the file). That should fill the entire buffer with
110 * all the data the item has to offer for that attribute.
111 * We then call flush_read_buffer() to copy the buffer to userspace
112 * in the increments specified.
116 configfs_read_file(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
118 struct configfs_buffer
*buffer
= file
->private_data
;
121 mutex_lock(&buffer
->mutex
);
122 if (buffer
->needs_read_fill
) {
123 retval
= fill_read_buffer(file
, buffer
);
127 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
128 __func__
, count
, *ppos
, buffer
->page
);
129 retval
= simple_read_from_buffer(buf
, count
, ppos
, buffer
->page
,
132 mutex_unlock(&buffer
->mutex
);
137 * configfs_read_bin_file - read a binary attribute.
138 * @file: file pointer.
139 * @buf: buffer to fill.
140 * @count: number of bytes to read.
141 * @ppos: starting offset in file.
143 * Userspace wants to read a binary attribute file. The attribute
144 * descriptor is in the file's ->d_fsdata. The target item is in the
145 * directory's ->d_fsdata.
147 * We check whether we need to refill the buffer. If so we will
148 * call the attributes' attr->read() twice. The first time we
149 * will pass a NULL as a buffer pointer, which the attributes' method
150 * will use to return the size of the buffer required. If no error
151 * occurs we will allocate the buffer using vmalloc and call
152 * attr->read() again passing that buffer as an argument.
153 * Then we just copy to user-space using simple_read_from_buffer.
157 configfs_read_bin_file(struct file
*file
, char __user
*buf
,
158 size_t count
, loff_t
*ppos
)
160 struct configfs_fragment
*frag
= to_frag(file
);
161 struct configfs_buffer
*buffer
= file
->private_data
;
163 ssize_t len
= min_t(size_t, count
, PAGE_SIZE
);
165 mutex_lock(&buffer
->mutex
);
167 /* we don't support switching read/write modes */
168 if (buffer
->write_in_progress
) {
172 buffer
->read_in_progress
= true;
174 if (buffer
->needs_read_fill
) {
175 /* perform first read with buf == NULL to get extent */
176 down_read(&frag
->frag_sem
);
177 if (!frag
->frag_dead
)
178 len
= buffer
->bin_attr
->read(buffer
->item
, NULL
, 0);
181 up_read(&frag
->frag_sem
);
187 /* do not exceed the maximum value */
188 if (buffer
->cb_max_size
&& len
> buffer
->cb_max_size
) {
193 buffer
->bin_buffer
= vmalloc(len
);
194 if (buffer
->bin_buffer
== NULL
) {
198 buffer
->bin_buffer_size
= len
;
200 /* perform second read to fill buffer */
201 down_read(&frag
->frag_sem
);
202 if (!frag
->frag_dead
)
203 len
= buffer
->bin_attr
->read(buffer
->item
,
204 buffer
->bin_buffer
, len
);
207 up_read(&frag
->frag_sem
);
210 vfree(buffer
->bin_buffer
);
211 buffer
->bin_buffer_size
= 0;
212 buffer
->bin_buffer
= NULL
;
216 buffer
->needs_read_fill
= 0;
219 retval
= simple_read_from_buffer(buf
, count
, ppos
, buffer
->bin_buffer
,
220 buffer
->bin_buffer_size
);
222 mutex_unlock(&buffer
->mutex
);
228 * fill_write_buffer - copy buffer from userspace.
229 * @buffer: data buffer for file.
230 * @buf: data from user.
231 * @count: number of bytes in @userbuf.
233 * Allocate @buffer->page if it hasn't been already, then
234 * copy the user-supplied buffer into it.
238 fill_write_buffer(struct configfs_buffer
* buffer
, const char __user
* buf
, size_t count
)
243 buffer
->page
= (char *)__get_free_pages(GFP_KERNEL
, 0);
247 if (count
>= SIMPLE_ATTR_SIZE
)
248 count
= SIMPLE_ATTR_SIZE
- 1;
249 error
= copy_from_user(buffer
->page
,buf
,count
);
250 buffer
->needs_read_fill
= 1;
251 /* if buf is assumed to contain a string, terminate it by \0,
252 * so e.g. sscanf() can scan the string easily */
253 buffer
->page
[count
] = 0;
254 return error
? -EFAULT
: count
;
258 flush_write_buffer(struct file
*file
, struct configfs_buffer
*buffer
, size_t count
)
260 struct configfs_fragment
*frag
= to_frag(file
);
263 down_read(&frag
->frag_sem
);
264 if (!frag
->frag_dead
)
265 res
= buffer
->attr
->store(buffer
->item
, buffer
->page
, count
);
266 up_read(&frag
->frag_sem
);
272 * configfs_write_file - write an attribute.
273 * @file: file pointer
274 * @buf: data to write
275 * @count: number of bytes
276 * @ppos: starting offset
278 * Similar to configfs_read_file(), though working in the opposite direction.
279 * We allocate and fill the data from the user in fill_write_buffer(),
280 * then push it to the config_item in flush_write_buffer().
281 * There is no easy way for us to know if userspace is only doing a partial
282 * write, so we don't support them. We expect the entire buffer to come
283 * on the first write.
284 * Hint: if you're writing a value, first read the file, modify only the
285 * the value you're changing, then write entire buffer back.
289 configfs_write_file(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
291 struct configfs_buffer
*buffer
= file
->private_data
;
294 mutex_lock(&buffer
->mutex
);
295 len
= fill_write_buffer(buffer
, buf
, count
);
297 len
= flush_write_buffer(file
, buffer
, len
);
300 mutex_unlock(&buffer
->mutex
);
305 * configfs_write_bin_file - write a binary attribute.
306 * @file: file pointer
307 * @buf: data to write
308 * @count: number of bytes
309 * @ppos: starting offset
311 * Writing to a binary attribute file is similar to a normal read.
312 * We buffer the consecutive writes (binary attribute files do not
313 * support lseek) in a continuously growing buffer, but we don't
314 * commit until the close of the file.
318 configfs_write_bin_file(struct file
*file
, const char __user
*buf
,
319 size_t count
, loff_t
*ppos
)
321 struct configfs_buffer
*buffer
= file
->private_data
;
325 mutex_lock(&buffer
->mutex
);
327 /* we don't support switching read/write modes */
328 if (buffer
->read_in_progress
) {
332 buffer
->write_in_progress
= true;
335 if (*ppos
+ count
> buffer
->bin_buffer_size
) {
337 if (buffer
->cb_max_size
&&
338 *ppos
+ count
> buffer
->cb_max_size
) {
343 tbuf
= vmalloc(*ppos
+ count
);
349 /* copy old contents */
350 if (buffer
->bin_buffer
) {
351 memcpy(tbuf
, buffer
->bin_buffer
,
352 buffer
->bin_buffer_size
);
353 vfree(buffer
->bin_buffer
);
356 /* clear the new area */
357 memset(tbuf
+ buffer
->bin_buffer_size
, 0,
358 *ppos
+ count
- buffer
->bin_buffer_size
);
359 buffer
->bin_buffer
= tbuf
;
360 buffer
->bin_buffer_size
= *ppos
+ count
;
363 len
= simple_write_to_buffer(buffer
->bin_buffer
,
364 buffer
->bin_buffer_size
, ppos
, buf
, count
);
366 mutex_unlock(&buffer
->mutex
);
370 static int __configfs_open_file(struct inode
*inode
, struct file
*file
, int type
)
372 struct dentry
*dentry
= file
->f_path
.dentry
;
373 struct configfs_fragment
*frag
= to_frag(file
);
374 struct configfs_attribute
*attr
;
375 struct configfs_buffer
*buffer
;
379 buffer
= kzalloc(sizeof(struct configfs_buffer
), GFP_KERNEL
);
384 down_read(&frag
->frag_sem
);
385 if (unlikely(frag
->frag_dead
))
386 goto out_free_buffer
;
389 buffer
->item
= to_item(dentry
->d_parent
);
391 goto out_free_buffer
;
393 attr
= to_attr(dentry
);
397 if (type
& CONFIGFS_ITEM_BIN_ATTR
) {
398 buffer
->bin_attr
= to_bin_attr(dentry
);
399 buffer
->cb_max_size
= buffer
->bin_attr
->cb_max_size
;
404 buffer
->owner
= attr
->ca_owner
;
405 /* Grab the module reference for this attribute if we have one */
407 if (!try_module_get(buffer
->owner
))
411 if (!buffer
->item
->ci_type
)
414 buffer
->ops
= buffer
->item
->ci_type
->ct_item_ops
;
416 /* File needs write support.
417 * The inode's perms must say it's ok,
418 * and we must have a store method.
420 if (file
->f_mode
& FMODE_WRITE
) {
421 if (!(inode
->i_mode
& S_IWUGO
))
423 if ((type
& CONFIGFS_ITEM_ATTR
) && !attr
->store
)
425 if ((type
& CONFIGFS_ITEM_BIN_ATTR
) && !buffer
->bin_attr
->write
)
429 /* File needs read support.
430 * The inode's perms must say it's ok, and we there
431 * must be a show method for it.
433 if (file
->f_mode
& FMODE_READ
) {
434 if (!(inode
->i_mode
& S_IRUGO
))
436 if ((type
& CONFIGFS_ITEM_ATTR
) && !attr
->show
)
438 if ((type
& CONFIGFS_ITEM_BIN_ATTR
) && !buffer
->bin_attr
->read
)
442 mutex_init(&buffer
->mutex
);
443 buffer
->needs_read_fill
= 1;
444 buffer
->read_in_progress
= false;
445 buffer
->write_in_progress
= false;
446 file
->private_data
= buffer
;
447 up_read(&frag
->frag_sem
);
451 module_put(buffer
->owner
);
453 config_item_put(buffer
->item
);
455 up_read(&frag
->frag_sem
);
461 static int configfs_release(struct inode
*inode
, struct file
*filp
)
463 struct configfs_buffer
*buffer
= filp
->private_data
;
465 module_put(buffer
->owner
);
467 free_page((unsigned long)buffer
->page
);
468 mutex_destroy(&buffer
->mutex
);
473 static int configfs_open_file(struct inode
*inode
, struct file
*filp
)
475 return __configfs_open_file(inode
, filp
, CONFIGFS_ITEM_ATTR
);
478 static int configfs_open_bin_file(struct inode
*inode
, struct file
*filp
)
480 return __configfs_open_file(inode
, filp
, CONFIGFS_ITEM_BIN_ATTR
);
483 static int configfs_release_bin_file(struct inode
*inode
, struct file
*file
)
485 struct configfs_buffer
*buffer
= file
->private_data
;
487 buffer
->read_in_progress
= false;
489 if (buffer
->write_in_progress
) {
490 struct configfs_fragment
*frag
= to_frag(file
);
491 buffer
->write_in_progress
= false;
493 down_read(&frag
->frag_sem
);
494 if (!frag
->frag_dead
) {
495 /* result of ->release() is ignored */
496 buffer
->bin_attr
->write(buffer
->item
,
498 buffer
->bin_buffer_size
);
500 up_read(&frag
->frag_sem
);
501 /* vfree on NULL is safe */
502 vfree(buffer
->bin_buffer
);
503 buffer
->bin_buffer
= NULL
;
504 buffer
->bin_buffer_size
= 0;
505 buffer
->needs_read_fill
= 1;
508 configfs_release(inode
, file
);
513 const struct file_operations configfs_file_operations
= {
514 .read
= configfs_read_file
,
515 .write
= configfs_write_file
,
516 .llseek
= generic_file_llseek
,
517 .open
= configfs_open_file
,
518 .release
= configfs_release
,
521 const struct file_operations configfs_bin_file_operations
= {
522 .read
= configfs_read_bin_file
,
523 .write
= configfs_write_bin_file
,
524 .llseek
= NULL
, /* bin file is not seekable */
525 .open
= configfs_open_bin_file
,
526 .release
= configfs_release_bin_file
,
530 * configfs_create_file - create an attribute file for an item.
531 * @item: item we're creating for.
532 * @attr: atrribute descriptor.
535 int configfs_create_file(struct config_item
* item
, const struct configfs_attribute
* attr
)
537 struct dentry
*dir
= item
->ci_dentry
;
538 struct configfs_dirent
*parent_sd
= dir
->d_fsdata
;
539 umode_t mode
= (attr
->ca_mode
& S_IALLUGO
) | S_IFREG
;
542 inode_lock_nested(d_inode(dir
), I_MUTEX_NORMAL
);
543 error
= configfs_make_dirent(parent_sd
, NULL
, (void *) attr
, mode
,
544 CONFIGFS_ITEM_ATTR
, parent_sd
->s_frag
);
545 inode_unlock(d_inode(dir
));
551 * configfs_create_bin_file - create a binary attribute file for an item.
552 * @item: item we're creating for.
553 * @attr: atrribute descriptor.
556 int configfs_create_bin_file(struct config_item
*item
,
557 const struct configfs_bin_attribute
*bin_attr
)
559 struct dentry
*dir
= item
->ci_dentry
;
560 struct configfs_dirent
*parent_sd
= dir
->d_fsdata
;
561 umode_t mode
= (bin_attr
->cb_attr
.ca_mode
& S_IALLUGO
) | S_IFREG
;
564 inode_lock_nested(dir
->d_inode
, I_MUTEX_NORMAL
);
565 error
= configfs_make_dirent(parent_sd
, NULL
, (void *) bin_attr
, mode
,
566 CONFIGFS_ITEM_BIN_ATTR
, parent_sd
->s_frag
);
567 inode_unlock(dir
->d_inode
);