/* -*- mode: c; c-basic-offset: 8; -*-1* vim: noexpandtab sw=8 ts=8 sts=0:2*3* file.c - operations for regular (text) files.4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public7* License as published by the Free Software Foundation; either8* version 2 of the License, or (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* General Public License for more details.14*15* You should have received a copy of the GNU General Public16* License along with this program; if not, write to the17* Free Software Foundation, Inc., 59 Temple Place - Suite 330,18* Boston, MA 021110-1307, USA.19*20* Based on sysfs:21* sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel22*23* configfs Copyright (C) 2005 Oracle. All rights reserved.24*/2526#include <linux/fs.h>27#include <linux/module.h>28#include <linux/slab.h>29#include <linux/mutex.h>30#include <asm/uaccess.h>3132#include <linux/configfs.h>33#include "configfs_internal.h"3435/*36* A simple attribute can only be 4096 characters. Why 4k? Because the37* original code limited it to PAGE_SIZE. That's a bad idea, though,38* because an attribute of 16k on ia64 won't work on x86. So we limit to39* 4k, our minimum common page size.40*/41#define SIMPLE_ATTR_SIZE 40964243struct configfs_buffer {44size_t count;45loff_t pos;46char * page;47struct configfs_item_operations * ops;48struct mutex mutex;49int needs_read_fill;50};515253/**54* fill_read_buffer - allocate and fill buffer from item.55* @dentry: dentry pointer.56* @buffer: data buffer for file.57*58* Allocate @buffer->page, if it hasn't been already, then call the59* config_item's show() method to fill the buffer with this attribute's60* data.61* This is called only once, on the file's first read.62*/63static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)64{65struct configfs_attribute * attr = to_attr(dentry);66struct config_item * item = to_item(dentry->d_parent);67struct configfs_item_operations * ops = buffer->ops;68int ret = 0;69ssize_t count;7071if (!buffer->page)72buffer->page = (char *) get_zeroed_page(GFP_KERNEL);73if (!buffer->page)74return -ENOMEM;7576count = ops->show_attribute(item,attr,buffer->page);77buffer->needs_read_fill = 0;78BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);79if (count >= 0)80buffer->count = count;81else82ret = count;83return ret;84}8586/**87* configfs_read_file - read an attribute.88* @file: file pointer.89* @buf: buffer to fill.90* @count: number of bytes to read.91* @ppos: starting offset in file.92*93* Userspace wants to read an attribute file. The attribute descriptor94* is in the file's ->d_fsdata. The target item is in the directory's95* ->d_fsdata.96*97* We call fill_read_buffer() to allocate and fill the buffer from the98* item's show() method exactly once (if the read is happening from99* the beginning of the file). That should fill the entire buffer with100* all the data the item has to offer for that attribute.101* We then call flush_read_buffer() to copy the buffer to userspace102* in the increments specified.103*/104105static ssize_t106configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)107{108struct configfs_buffer * buffer = file->private_data;109ssize_t retval = 0;110111mutex_lock(&buffer->mutex);112if (buffer->needs_read_fill) {113if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))114goto out;115}116pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",117__func__, count, *ppos, buffer->page);118retval = simple_read_from_buffer(buf, count, ppos, buffer->page,119buffer->count);120out:121mutex_unlock(&buffer->mutex);122return retval;123}124125126/**127* fill_write_buffer - copy buffer from userspace.128* @buffer: data buffer for file.129* @buf: data from user.130* @count: number of bytes in @userbuf.131*132* Allocate @buffer->page if it hasn't been already, then133* copy the user-supplied buffer into it.134*/135136static int137fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)138{139int error;140141if (!buffer->page)142buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);143if (!buffer->page)144return -ENOMEM;145146if (count >= SIMPLE_ATTR_SIZE)147count = SIMPLE_ATTR_SIZE - 1;148error = copy_from_user(buffer->page,buf,count);149buffer->needs_read_fill = 1;150/* if buf is assumed to contain a string, terminate it by \0,151* so e.g. sscanf() can scan the string easily */152buffer->page[count] = 0;153return error ? -EFAULT : count;154}155156157/**158* flush_write_buffer - push buffer to config_item.159* @dentry: dentry to the attribute160* @buffer: data buffer for file.161* @count: number of bytes162*163* Get the correct pointers for the config_item and the attribute we're164* dealing with, then call the store() method for the attribute,165* passing the buffer that we acquired in fill_write_buffer().166*/167168static int169flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)170{171struct configfs_attribute * attr = to_attr(dentry);172struct config_item * item = to_item(dentry->d_parent);173struct configfs_item_operations * ops = buffer->ops;174175return ops->store_attribute(item,attr,buffer->page,count);176}177178179/**180* configfs_write_file - write an attribute.181* @file: file pointer182* @buf: data to write183* @count: number of bytes184* @ppos: starting offset185*186* Similar to configfs_read_file(), though working in the opposite direction.187* We allocate and fill the data from the user in fill_write_buffer(),188* then push it to the config_item in flush_write_buffer().189* There is no easy way for us to know if userspace is only doing a partial190* write, so we don't support them. We expect the entire buffer to come191* on the first write.192* Hint: if you're writing a value, first read the file, modify only the193* the value you're changing, then write entire buffer back.194*/195196static ssize_t197configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)198{199struct configfs_buffer * buffer = file->private_data;200ssize_t len;201202mutex_lock(&buffer->mutex);203len = fill_write_buffer(buffer, buf, count);204if (len > 0)205len = flush_write_buffer(file->f_path.dentry, buffer, count);206if (len > 0)207*ppos += len;208mutex_unlock(&buffer->mutex);209return len;210}211212static int check_perm(struct inode * inode, struct file * file)213{214struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);215struct configfs_attribute * attr = to_attr(file->f_path.dentry);216struct configfs_buffer * buffer;217struct configfs_item_operations * ops = NULL;218int error = 0;219220if (!item || !attr)221goto Einval;222223/* Grab the module reference for this attribute if we have one */224if (!try_module_get(attr->ca_owner)) {225error = -ENODEV;226goto Done;227}228229if (item->ci_type)230ops = item->ci_type->ct_item_ops;231else232goto Eaccess;233234/* File needs write support.235* The inode's perms must say it's ok,236* and we must have a store method.237*/238if (file->f_mode & FMODE_WRITE) {239240if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute)241goto Eaccess;242243}244245/* File needs read support.246* The inode's perms must say it's ok, and we there247* must be a show method for it.248*/249if (file->f_mode & FMODE_READ) {250if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute)251goto Eaccess;252}253254/* No error? Great, allocate a buffer for the file, and store it255* it in file->private_data for easy access.256*/257buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);258if (!buffer) {259error = -ENOMEM;260goto Enomem;261}262mutex_init(&buffer->mutex);263buffer->needs_read_fill = 1;264buffer->ops = ops;265file->private_data = buffer;266goto Done;267268Einval:269error = -EINVAL;270goto Done;271Eaccess:272error = -EACCES;273Enomem:274module_put(attr->ca_owner);275Done:276if (error && item)277config_item_put(item);278return error;279}280281static int configfs_open_file(struct inode * inode, struct file * filp)282{283return check_perm(inode,filp);284}285286static int configfs_release(struct inode * inode, struct file * filp)287{288struct config_item * item = to_item(filp->f_path.dentry->d_parent);289struct configfs_attribute * attr = to_attr(filp->f_path.dentry);290struct module * owner = attr->ca_owner;291struct configfs_buffer * buffer = filp->private_data;292293if (item)294config_item_put(item);295/* After this point, attr should not be accessed. */296module_put(owner);297298if (buffer) {299if (buffer->page)300free_page((unsigned long)buffer->page);301mutex_destroy(&buffer->mutex);302kfree(buffer);303}304return 0;305}306307const struct file_operations configfs_file_operations = {308.read = configfs_read_file,309.write = configfs_write_file,310.llseek = generic_file_llseek,311.open = configfs_open_file,312.release = configfs_release,313};314315316int configfs_add_file(struct dentry * dir, const struct configfs_attribute * attr, int type)317{318struct configfs_dirent * parent_sd = dir->d_fsdata;319umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;320int error = 0;321322mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_NORMAL);323error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);324mutex_unlock(&dir->d_inode->i_mutex);325326return error;327}328329330/**331* configfs_create_file - create an attribute file for an item.332* @item: item we're creating for.333* @attr: atrribute descriptor.334*/335336int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)337{338BUG_ON(!item || !item->ci_dentry || !attr);339340return configfs_add_file(item->ci_dentry, attr,341CONFIGFS_ITEM_ATTR);342}343344345346