1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
5 * file.c - operations for regular (text) files.
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
20 #include <linux/configfs.h>
21 #include "configfs_internal.h"
24 * A simple attribute can only be 4096 characters. Why 4k? Because the
25 * original code limited it to PAGE_SIZE. That's a bad idea, though,
26 * because an attribute of 16k on ia64 won't work on x86. So we limit to
27 * 4k, our minimum common page size.
29 #define SIMPLE_ATTR_SIZE 4096
31 struct configfs_buffer {
35 struct configfs_item_operations * ops;
38 bool read_in_progress;
39 bool write_in_progress;
46 * fill_read_buffer - allocate and fill buffer from item.
47 * @dentry: dentry pointer.
48 * @buffer: data buffer for file.
50 * Allocate @buffer->page, if it hasn't been already, then call the
51 * config_item's show() method to fill the buffer with this attribute's
53 * This is called only once, on the file's first read.
55 static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
57 struct configfs_attribute * attr = to_attr(dentry);
58 struct config_item * item = to_item(dentry->d_parent);
63 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
67 count = attr->show(item, buffer->page);
69 BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
71 buffer->needs_read_fill = 0;
72 buffer->count = count;
79 * configfs_read_file - read an attribute.
80 * @file: file pointer.
81 * @buf: buffer to fill.
82 * @count: number of bytes to read.
83 * @ppos: starting offset in file.
85 * Userspace wants to read an attribute file. The attribute descriptor
86 * is in the file's ->d_fsdata. The target item is in the directory's
89 * We call fill_read_buffer() to allocate and fill the buffer from the
90 * item's show() method exactly once (if the read is happening from
91 * the beginning of the file). That should fill the entire buffer with
92 * all the data the item has to offer for that attribute.
93 * We then call flush_read_buffer() to copy the buffer to userspace
94 * in the increments specified.
98 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
100 struct configfs_buffer * buffer = file->private_data;
103 mutex_lock(&buffer->mutex);
104 if (buffer->needs_read_fill) {
105 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
108 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
109 __func__, count, *ppos, buffer->page);
110 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
113 mutex_unlock(&buffer->mutex);
118 * configfs_read_bin_file - read a binary attribute.
119 * @file: file pointer.
120 * @buf: buffer to fill.
121 * @count: number of bytes to read.
122 * @ppos: starting offset in file.
124 * Userspace wants to read a binary attribute file. The attribute
125 * descriptor is in the file's ->d_fsdata. The target item is in the
126 * directory's ->d_fsdata.
128 * We check whether we need to refill the buffer. If so we will
129 * call the attributes' attr->read() twice. The first time we
130 * will pass a NULL as a buffer pointer, which the attributes' method
131 * will use to return the size of the buffer required. If no error
132 * occurs we will allocate the buffer using vmalloc and call
133 * attr->read() again passing that buffer as an argument.
134 * Then we just copy to user-space using simple_read_from_buffer.
138 configfs_read_bin_file(struct file *file, char __user *buf,
139 size_t count, loff_t *ppos)
141 struct configfs_buffer *buffer = file->private_data;
142 struct dentry *dentry = file->f_path.dentry;
143 struct config_item *item = to_item(dentry->d_parent);
144 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
146 ssize_t len = min_t(size_t, count, PAGE_SIZE);
148 mutex_lock(&buffer->mutex);
150 /* we don't support switching read/write modes */
151 if (buffer->write_in_progress) {
155 buffer->read_in_progress = true;
157 if (buffer->needs_read_fill) {
158 /* perform first read with buf == NULL to get extent */
159 len = bin_attr->read(item, NULL, 0);
165 /* do not exceed the maximum value */
166 if (bin_attr->cb_max_size && len > bin_attr->cb_max_size) {
171 buffer->bin_buffer = vmalloc(len);
172 if (buffer->bin_buffer == NULL) {
176 buffer->bin_buffer_size = len;
178 /* perform second read to fill buffer */
179 len = bin_attr->read(item, buffer->bin_buffer, len);
182 vfree(buffer->bin_buffer);
183 buffer->bin_buffer_size = 0;
184 buffer->bin_buffer = NULL;
188 buffer->needs_read_fill = 0;
191 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
192 buffer->bin_buffer_size);
194 mutex_unlock(&buffer->mutex);
200 * fill_write_buffer - copy buffer from userspace.
201 * @buffer: data buffer for file.
202 * @buf: data from user.
203 * @count: number of bytes in @userbuf.
205 * Allocate @buffer->page if it hasn't been already, then
206 * copy the user-supplied buffer into it.
210 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
215 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
219 if (count >= SIMPLE_ATTR_SIZE)
220 count = SIMPLE_ATTR_SIZE - 1;
221 error = copy_from_user(buffer->page,buf,count);
222 buffer->needs_read_fill = 1;
223 /* if buf is assumed to contain a string, terminate it by \0,
224 * so e.g. sscanf() can scan the string easily */
225 buffer->page[count] = 0;
226 return error ? -EFAULT : count;
231 * flush_write_buffer - push buffer to config_item.
232 * @dentry: dentry to the attribute
233 * @buffer: data buffer for file.
234 * @count: number of bytes
236 * Get the correct pointers for the config_item and the attribute we're
237 * dealing with, then call the store() method for the attribute,
238 * passing the buffer that we acquired in fill_write_buffer().
242 flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
244 struct configfs_attribute * attr = to_attr(dentry);
245 struct config_item * item = to_item(dentry->d_parent);
247 return attr->store(item, buffer->page, count);
252 * configfs_write_file - write an attribute.
253 * @file: file pointer
254 * @buf: data to write
255 * @count: number of bytes
256 * @ppos: starting offset
258 * Similar to configfs_read_file(), though working in the opposite direction.
259 * We allocate and fill the data from the user in fill_write_buffer(),
260 * then push it to the config_item in flush_write_buffer().
261 * There is no easy way for us to know if userspace is only doing a partial
262 * write, so we don't support them. We expect the entire buffer to come
263 * on the first write.
264 * Hint: if you're writing a value, first read the file, modify only the
265 * the value you're changing, then write entire buffer back.
269 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
271 struct configfs_buffer * buffer = file->private_data;
274 mutex_lock(&buffer->mutex);
275 len = fill_write_buffer(buffer, buf, count);
277 len = flush_write_buffer(file->f_path.dentry, buffer, len);
280 mutex_unlock(&buffer->mutex);
285 * configfs_write_bin_file - write a binary attribute.
286 * @file: file pointer
287 * @buf: data to write
288 * @count: number of bytes
289 * @ppos: starting offset
291 * Writing to a binary attribute file is similar to a normal read.
292 * We buffer the consecutive writes (binary attribute files do not
293 * support lseek) in a continuously growing buffer, but we don't
294 * commit until the close of the file.
298 configfs_write_bin_file(struct file *file, const char __user *buf,
299 size_t count, loff_t *ppos)
301 struct configfs_buffer *buffer = file->private_data;
302 struct dentry *dentry = file->f_path.dentry;
303 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
307 mutex_lock(&buffer->mutex);
309 /* we don't support switching read/write modes */
310 if (buffer->read_in_progress) {
314 buffer->write_in_progress = true;
317 if (*ppos + count > buffer->bin_buffer_size) {
319 if (bin_attr->cb_max_size &&
320 *ppos + count > bin_attr->cb_max_size) {
325 tbuf = vmalloc(*ppos + count);
331 /* copy old contents */
332 if (buffer->bin_buffer) {
333 memcpy(tbuf, buffer->bin_buffer,
334 buffer->bin_buffer_size);
335 vfree(buffer->bin_buffer);
338 /* clear the new area */
339 memset(tbuf + buffer->bin_buffer_size, 0,
340 *ppos + count - buffer->bin_buffer_size);
341 buffer->bin_buffer = tbuf;
342 buffer->bin_buffer_size = *ppos + count;
345 len = simple_write_to_buffer(buffer->bin_buffer,
346 buffer->bin_buffer_size, ppos, buf, count);
348 mutex_unlock(&buffer->mutex);
352 static int check_perm(struct inode * inode, struct file * file, int type)
354 struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
355 struct configfs_attribute * attr = to_attr(file->f_path.dentry);
356 struct configfs_bin_attribute *bin_attr = NULL;
357 struct configfs_buffer * buffer;
358 struct configfs_item_operations * ops = NULL;
364 if (type & CONFIGFS_ITEM_BIN_ATTR)
365 bin_attr = to_bin_attr(file->f_path.dentry);
367 /* Grab the module reference for this attribute if we have one */
368 if (!try_module_get(attr->ca_owner)) {
374 ops = item->ci_type->ct_item_ops;
378 /* File needs write support.
379 * The inode's perms must say it's ok,
380 * and we must have a store method.
382 if (file->f_mode & FMODE_WRITE) {
383 if (!(inode->i_mode & S_IWUGO))
386 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
389 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->write)
393 /* File needs read support.
394 * The inode's perms must say it's ok, and we there
395 * must be a show method for it.
397 if (file->f_mode & FMODE_READ) {
398 if (!(inode->i_mode & S_IRUGO))
401 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
404 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !bin_attr->read)
408 /* No error? Great, allocate a buffer for the file, and store it
409 * it in file->private_data for easy access.
411 buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
416 mutex_init(&buffer->mutex);
417 buffer->needs_read_fill = 1;
418 buffer->read_in_progress = false;
419 buffer->write_in_progress = false;
421 file->private_data = buffer;
430 module_put(attr->ca_owner);
433 config_item_put(item);
437 static int configfs_release(struct inode *inode, struct file *filp)
439 struct config_item * item = to_item(filp->f_path.dentry->d_parent);
440 struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
441 struct module * owner = attr->ca_owner;
442 struct configfs_buffer * buffer = filp->private_data;
445 config_item_put(item);
446 /* After this point, attr should not be accessed. */
451 free_page((unsigned long)buffer->page);
452 mutex_destroy(&buffer->mutex);
458 static int configfs_open_file(struct inode *inode, struct file *filp)
460 return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
463 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
465 return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
468 static int configfs_release_bin_file(struct inode *inode, struct file *filp)
470 struct configfs_buffer *buffer = filp->private_data;
471 struct dentry *dentry = filp->f_path.dentry;
472 struct config_item *item = to_item(dentry->d_parent);
473 struct configfs_bin_attribute *bin_attr = to_bin_attr(dentry);
477 buffer->read_in_progress = false;
479 if (buffer->write_in_progress) {
480 buffer->write_in_progress = false;
482 len = bin_attr->write(item, buffer->bin_buffer,
483 buffer->bin_buffer_size);
485 /* vfree on NULL is safe */
486 vfree(buffer->bin_buffer);
487 buffer->bin_buffer = NULL;
488 buffer->bin_buffer_size = 0;
489 buffer->needs_read_fill = 1;
492 ret = configfs_release(inode, filp);
499 const struct file_operations configfs_file_operations = {
500 .read = configfs_read_file,
501 .write = configfs_write_file,
502 .llseek = generic_file_llseek,
503 .open = configfs_open_file,
504 .release = configfs_release,
507 const struct file_operations configfs_bin_file_operations = {
508 .read = configfs_read_bin_file,
509 .write = configfs_write_bin_file,
510 .llseek = NULL, /* bin file is not seekable */
511 .open = configfs_open_bin_file,
512 .release = configfs_release_bin_file,
516 * configfs_create_file - create an attribute file for an item.
517 * @item: item we're creating for.
518 * @attr: atrribute descriptor.
521 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
523 struct dentry *dir = item->ci_dentry;
524 struct configfs_dirent *parent_sd = dir->d_fsdata;
525 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
528 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
529 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
531 inode_unlock(d_inode(dir));
537 * configfs_create_bin_file - create a binary attribute file for an item.
538 * @item: item we're creating for.
539 * @attr: atrribute descriptor.
542 int configfs_create_bin_file(struct config_item *item,
543 const struct configfs_bin_attribute *bin_attr)
545 struct dentry *dir = item->ci_dentry;
546 struct configfs_dirent *parent_sd = dir->d_fsdata;
547 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
550 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
551 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
552 CONFIGFS_ITEM_BIN_ATTR);
553 inode_unlock(dir->d_inode);