1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * file.c - operations for regular (text) files.
6 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
8 * configfs Copyright (C) 2005 Oracle. All rights reserved.
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/vmalloc.h>
16 #include <linux/uaccess.h>
18 #include <linux/configfs.h>
19 #include "configfs_internal.h"
22 * A simple attribute can only be 4096 characters. Why 4k? Because the
23 * original code limited it to PAGE_SIZE. That's a bad idea, though,
24 * because an attribute of 16k on ia64 won't work on x86. So we limit to
25 * 4k, our minimum common page size.
27 #define SIMPLE_ATTR_SIZE 4096
29 struct configfs_buffer {
33 struct configfs_item_operations * ops;
36 bool read_in_progress;
37 bool write_in_progress;
41 struct config_item *item;
44 struct configfs_attribute *attr;
45 struct configfs_bin_attribute *bin_attr;
49 static inline struct configfs_fragment *to_frag(struct file *file)
51 struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
56 static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
58 struct configfs_fragment *frag = to_frag(file);
59 ssize_t count = -ENOENT;
62 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
66 down_read(&frag->frag_sem);
68 count = buffer->attr->show(buffer->item, buffer->page);
69 up_read(&frag->frag_sem);
73 if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
75 buffer->needs_read_fill = 0;
76 buffer->count = count;
81 * configfs_read_file - read an attribute.
82 * @file: file pointer.
83 * @buf: buffer to fill.
84 * @count: number of bytes to read.
85 * @ppos: starting offset in file.
87 * Userspace wants to read an attribute file. The attribute descriptor
88 * is in the file's ->d_fsdata. The target item is in the directory's
91 * We call fill_read_buffer() to allocate and fill the buffer from the
92 * item's show() method exactly once (if the read is happening from
93 * the beginning of the file). That should fill the entire buffer with
94 * all the data the item has to offer for that attribute.
95 * We then call flush_read_buffer() to copy the buffer to userspace
96 * in the increments specified.
100 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
102 struct configfs_buffer *buffer = file->private_data;
105 mutex_lock(&buffer->mutex);
106 if (buffer->needs_read_fill) {
107 retval = fill_read_buffer(file, buffer);
111 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
112 __func__, count, *ppos, buffer->page);
113 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
116 mutex_unlock(&buffer->mutex);
121 * configfs_read_bin_file - read a binary attribute.
122 * @file: file pointer.
123 * @buf: buffer to fill.
124 * @count: number of bytes to read.
125 * @ppos: starting offset in file.
127 * Userspace wants to read a binary attribute file. The attribute
128 * descriptor is in the file's ->d_fsdata. The target item is in the
129 * directory's ->d_fsdata.
131 * We check whether we need to refill the buffer. If so we will
132 * call the attributes' attr->read() twice. The first time we
133 * will pass a NULL as a buffer pointer, which the attributes' method
134 * will use to return the size of the buffer required. If no error
135 * occurs we will allocate the buffer using vmalloc and call
136 * attr->read() again passing that buffer as an argument.
137 * Then we just copy to user-space using simple_read_from_buffer.
141 configfs_read_bin_file(struct file *file, char __user *buf,
142 size_t count, loff_t *ppos)
144 struct configfs_fragment *frag = to_frag(file);
145 struct configfs_buffer *buffer = file->private_data;
147 ssize_t len = min_t(size_t, count, PAGE_SIZE);
149 mutex_lock(&buffer->mutex);
151 /* we don't support switching read/write modes */
152 if (buffer->write_in_progress) {
156 buffer->read_in_progress = true;
158 if (buffer->needs_read_fill) {
159 /* perform first read with buf == NULL to get extent */
160 down_read(&frag->frag_sem);
161 if (!frag->frag_dead)
162 len = buffer->bin_attr->read(buffer->item, NULL, 0);
165 up_read(&frag->frag_sem);
171 /* do not exceed the maximum value */
172 if (buffer->cb_max_size && len > buffer->cb_max_size) {
177 buffer->bin_buffer = vmalloc(len);
178 if (buffer->bin_buffer == NULL) {
182 buffer->bin_buffer_size = len;
184 /* perform second read to fill buffer */
185 down_read(&frag->frag_sem);
186 if (!frag->frag_dead)
187 len = buffer->bin_attr->read(buffer->item,
188 buffer->bin_buffer, len);
191 up_read(&frag->frag_sem);
194 vfree(buffer->bin_buffer);
195 buffer->bin_buffer_size = 0;
196 buffer->bin_buffer = NULL;
200 buffer->needs_read_fill = 0;
203 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
204 buffer->bin_buffer_size);
206 mutex_unlock(&buffer->mutex);
212 * fill_write_buffer - copy buffer from userspace.
213 * @buffer: data buffer for file.
214 * @buf: data from user.
215 * @count: number of bytes in @userbuf.
217 * Allocate @buffer->page if it hasn't been already, then
218 * copy the user-supplied buffer into it.
222 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
227 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
231 if (count >= SIMPLE_ATTR_SIZE)
232 count = SIMPLE_ATTR_SIZE - 1;
233 error = copy_from_user(buffer->page,buf,count);
234 buffer->needs_read_fill = 1;
235 /* if buf is assumed to contain a string, terminate it by \0,
236 * so e.g. sscanf() can scan the string easily */
237 buffer->page[count] = 0;
238 return error ? -EFAULT : count;
242 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
244 struct configfs_fragment *frag = to_frag(file);
247 down_read(&frag->frag_sem);
248 if (!frag->frag_dead)
249 res = buffer->attr->store(buffer->item, buffer->page, count);
250 up_read(&frag->frag_sem);
256 * configfs_write_file - write an attribute.
257 * @file: file pointer
258 * @buf: data to write
259 * @count: number of bytes
260 * @ppos: starting offset
262 * Similar to configfs_read_file(), though working in the opposite direction.
263 * We allocate and fill the data from the user in fill_write_buffer(),
264 * then push it to the config_item in flush_write_buffer().
265 * There is no easy way for us to know if userspace is only doing a partial
266 * write, so we don't support them. We expect the entire buffer to come
267 * on the first write.
268 * Hint: if you're writing a value, first read the file, modify only
269 * the value you're changing, then write entire buffer back.
273 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
275 struct configfs_buffer *buffer = file->private_data;
278 mutex_lock(&buffer->mutex);
279 len = fill_write_buffer(buffer, buf, count);
281 len = flush_write_buffer(file, buffer, len);
284 mutex_unlock(&buffer->mutex);
289 * configfs_write_bin_file - write a binary attribute.
290 * @file: file pointer
291 * @buf: data to write
292 * @count: number of bytes
293 * @ppos: starting offset
295 * Writing to a binary attribute file is similar to a normal read.
296 * We buffer the consecutive writes (binary attribute files do not
297 * support lseek) in a continuously growing buffer, but we don't
298 * commit until the close of the file.
302 configfs_write_bin_file(struct file *file, const char __user *buf,
303 size_t count, loff_t *ppos)
305 struct configfs_buffer *buffer = file->private_data;
309 mutex_lock(&buffer->mutex);
311 /* we don't support switching read/write modes */
312 if (buffer->read_in_progress) {
316 buffer->write_in_progress = true;
319 if (*ppos + count > buffer->bin_buffer_size) {
321 if (buffer->cb_max_size &&
322 *ppos + count > buffer->cb_max_size) {
327 tbuf = vmalloc(*ppos + count);
333 /* copy old contents */
334 if (buffer->bin_buffer) {
335 memcpy(tbuf, buffer->bin_buffer,
336 buffer->bin_buffer_size);
337 vfree(buffer->bin_buffer);
340 /* clear the new area */
341 memset(tbuf + buffer->bin_buffer_size, 0,
342 *ppos + count - buffer->bin_buffer_size);
343 buffer->bin_buffer = tbuf;
344 buffer->bin_buffer_size = *ppos + count;
347 len = simple_write_to_buffer(buffer->bin_buffer,
348 buffer->bin_buffer_size, ppos, buf, count);
350 mutex_unlock(&buffer->mutex);
354 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
356 struct dentry *dentry = file->f_path.dentry;
357 struct configfs_fragment *frag = to_frag(file);
358 struct configfs_attribute *attr;
359 struct configfs_buffer *buffer;
363 buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
368 down_read(&frag->frag_sem);
369 if (unlikely(frag->frag_dead))
370 goto out_free_buffer;
373 buffer->item = to_item(dentry->d_parent);
375 goto out_free_buffer;
377 attr = to_attr(dentry);
379 goto out_free_buffer;
381 if (type & CONFIGFS_ITEM_BIN_ATTR) {
382 buffer->bin_attr = to_bin_attr(dentry);
383 buffer->cb_max_size = buffer->bin_attr->cb_max_size;
388 buffer->owner = attr->ca_owner;
389 /* Grab the module reference for this attribute if we have one */
391 if (!try_module_get(buffer->owner))
392 goto out_free_buffer;
395 if (!buffer->item->ci_type)
398 buffer->ops = buffer->item->ci_type->ct_item_ops;
400 /* File needs write support.
401 * The inode's perms must say it's ok,
402 * and we must have a store method.
404 if (file->f_mode & FMODE_WRITE) {
405 if (!(inode->i_mode & S_IWUGO))
407 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
409 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
413 /* File needs read support.
414 * The inode's perms must say it's ok, and we there
415 * must be a show method for it.
417 if (file->f_mode & FMODE_READ) {
418 if (!(inode->i_mode & S_IRUGO))
420 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
422 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
426 mutex_init(&buffer->mutex);
427 buffer->needs_read_fill = 1;
428 buffer->read_in_progress = false;
429 buffer->write_in_progress = false;
430 file->private_data = buffer;
431 up_read(&frag->frag_sem);
435 module_put(buffer->owner);
437 up_read(&frag->frag_sem);
443 static int configfs_release(struct inode *inode, struct file *filp)
445 struct configfs_buffer *buffer = filp->private_data;
447 module_put(buffer->owner);
449 free_page((unsigned long)buffer->page);
450 mutex_destroy(&buffer->mutex);
455 static int configfs_open_file(struct inode *inode, struct file *filp)
457 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
460 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
462 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
465 static int configfs_release_bin_file(struct inode *inode, struct file *file)
467 struct configfs_buffer *buffer = file->private_data;
469 buffer->read_in_progress = false;
471 if (buffer->write_in_progress) {
472 struct configfs_fragment *frag = to_frag(file);
473 buffer->write_in_progress = false;
475 down_read(&frag->frag_sem);
476 if (!frag->frag_dead) {
477 /* result of ->release() is ignored */
478 buffer->bin_attr->write(buffer->item,
480 buffer->bin_buffer_size);
482 up_read(&frag->frag_sem);
483 /* vfree on NULL is safe */
484 vfree(buffer->bin_buffer);
485 buffer->bin_buffer = NULL;
486 buffer->bin_buffer_size = 0;
487 buffer->needs_read_fill = 1;
490 configfs_release(inode, file);
495 const struct file_operations configfs_file_operations = {
496 .read = configfs_read_file,
497 .write = configfs_write_file,
498 .llseek = generic_file_llseek,
499 .open = configfs_open_file,
500 .release = configfs_release,
503 const struct file_operations configfs_bin_file_operations = {
504 .read = configfs_read_bin_file,
505 .write = configfs_write_bin_file,
506 .llseek = NULL, /* bin file is not seekable */
507 .open = configfs_open_bin_file,
508 .release = configfs_release_bin_file,
512 * configfs_create_file - create an attribute file for an item.
513 * @item: item we're creating for.
514 * @attr: atrribute descriptor.
517 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
519 struct dentry *dir = item->ci_dentry;
520 struct configfs_dirent *parent_sd = dir->d_fsdata;
521 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
524 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
525 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
526 CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
527 inode_unlock(d_inode(dir));
533 * configfs_create_bin_file - create a binary attribute file for an item.
534 * @item: item we're creating for.
535 * @attr: atrribute descriptor.
538 int configfs_create_bin_file(struct config_item *item,
539 const struct configfs_bin_attribute *bin_attr)
541 struct dentry *dir = item->ci_dentry;
542 struct configfs_dirent *parent_sd = dir->d_fsdata;
543 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
546 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
547 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
548 CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
549 inode_unlock(dir->d_inode);