blob: 106ca589e90a2b3389db0157190247a7aac5fdcc [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * file.c - operations for regular (text) files.
5 *
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.
10 *
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.
15 *
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.
20 *
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 */
26
27#include <linux/fs.h>
28#include <linux/module.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080029#include <linux/slab.h>
Johannes Berg6d748922007-06-22 11:20:00 +020030#include <linux/mutex.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080031#include <asm/uaccess.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080032
33#include <linux/configfs.h>
34#include "configfs_internal.h"
35
Joel Beckerb23cdde2007-06-22 13:07:02 -070036/*
37 * A simple attribute can only be 4096 characters. Why 4k? Because the
38 * original code limited it to PAGE_SIZE. That's a bad idea, though,
39 * because an attribute of 16k on ia64 won't work on x86. So we limit to
40 * 4k, our minimum common page size.
41 */
42#define SIMPLE_ATTR_SIZE 4096
Joel Becker7063fbf2005-12-15 14:29:43 -080043
44struct configfs_buffer {
45 size_t count;
46 loff_t pos;
47 char * page;
48 struct configfs_item_operations * ops;
Johannes Berg6d748922007-06-22 11:20:00 +020049 struct mutex mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -080050 int needs_read_fill;
51};
52
53
54/**
55 * fill_read_buffer - allocate and fill buffer from item.
56 * @dentry: dentry pointer.
57 * @buffer: data buffer for file.
58 *
59 * Allocate @buffer->page, if it hasn't been already, then call the
60 * config_item's show() method to fill the buffer with this attribute's
61 * data.
62 * This is called only once, on the file's first read.
63 */
64static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
65{
66 struct configfs_attribute * attr = to_attr(dentry);
67 struct config_item * item = to_item(dentry->d_parent);
68 struct configfs_item_operations * ops = buffer->ops;
69 int ret = 0;
70 ssize_t count;
71
72 if (!buffer->page)
73 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
74 if (!buffer->page)
75 return -ENOMEM;
76
Christoph Hellwig870823e2015-10-03 15:32:37 +020077 if (ops->show_attribute)
78 count = ops->show_attribute(item, attr, buffer->page);
79 else
80 count = attr->show(item, buffer->page);
81
Joel Becker7063fbf2005-12-15 14:29:43 -080082 buffer->needs_read_fill = 0;
Joel Beckerb23cdde2007-06-22 13:07:02 -070083 BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
Joel Becker7063fbf2005-12-15 14:29:43 -080084 if (count >= 0)
85 buffer->count = count;
86 else
87 ret = count;
88 return ret;
89}
90
Joel Becker7063fbf2005-12-15 14:29:43 -080091/**
92 * configfs_read_file - read an attribute.
93 * @file: file pointer.
94 * @buf: buffer to fill.
95 * @count: number of bytes to read.
96 * @ppos: starting offset in file.
97 *
98 * Userspace wants to read an attribute file. The attribute descriptor
99 * is in the file's ->d_fsdata. The target item is in the directory's
100 * ->d_fsdata.
101 *
102 * We call fill_read_buffer() to allocate and fill the buffer from the
103 * item's show() method exactly once (if the read is happening from
104 * the beginning of the file). That should fill the entire buffer with
105 * all the data the item has to offer for that attribute.
106 * We then call flush_read_buffer() to copy the buffer to userspace
107 * in the increments specified.
108 */
109
110static ssize_t
111configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
112{
113 struct configfs_buffer * buffer = file->private_data;
114 ssize_t retval = 0;
115
Johannes Berg6d748922007-06-22 11:20:00 +0200116 mutex_lock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800117 if (buffer->needs_read_fill) {
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800118 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
Joel Becker7063fbf2005-12-15 14:29:43 -0800119 goto out;
120 }
Zach Brown4779efc2006-10-03 01:16:05 -0700121 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700122 __func__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700123 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
124 buffer->count);
Joel Becker7063fbf2005-12-15 14:29:43 -0800125out:
Johannes Berg6d748922007-06-22 11:20:00 +0200126 mutex_unlock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800127 return retval;
128}
129
130
131/**
132 * fill_write_buffer - copy buffer from userspace.
133 * @buffer: data buffer for file.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800134 * @buf: data from user.
Joel Becker7063fbf2005-12-15 14:29:43 -0800135 * @count: number of bytes in @userbuf.
136 *
137 * Allocate @buffer->page if it hasn't been already, then
138 * copy the user-supplied buffer into it.
139 */
140
141static int
142fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
143{
144 int error;
145
146 if (!buffer->page)
Joel Beckerff05d1c2007-01-23 17:00:45 -0800147 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
Joel Becker7063fbf2005-12-15 14:29:43 -0800148 if (!buffer->page)
149 return -ENOMEM;
150
Joel Beckerb23cdde2007-06-22 13:07:02 -0700151 if (count >= SIMPLE_ATTR_SIZE)
152 count = SIMPLE_ATTR_SIZE - 1;
Joel Becker7063fbf2005-12-15 14:29:43 -0800153 error = copy_from_user(buffer->page,buf,count);
154 buffer->needs_read_fill = 1;
Joel Beckerff05d1c2007-01-23 17:00:45 -0800155 /* if buf is assumed to contain a string, terminate it by \0,
156 * so e.g. sscanf() can scan the string easily */
157 buffer->page[count] = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800158 return error ? -EFAULT : count;
159}
160
161
162/**
163 * flush_write_buffer - push buffer to config_item.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800164 * @dentry: dentry to the attribute
Joel Becker7063fbf2005-12-15 14:29:43 -0800165 * @buffer: data buffer for file.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800166 * @count: number of bytes
Joel Becker7063fbf2005-12-15 14:29:43 -0800167 *
168 * Get the correct pointers for the config_item and the attribute we're
169 * dealing with, then call the store() method for the attribute,
170 * passing the buffer that we acquired in fill_write_buffer().
171 */
172
173static int
174flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
175{
176 struct configfs_attribute * attr = to_attr(dentry);
177 struct config_item * item = to_item(dentry->d_parent);
178 struct configfs_item_operations * ops = buffer->ops;
179
Christoph Hellwig870823e2015-10-03 15:32:37 +0200180 if (ops->store_attribute)
181 return ops->store_attribute(item, attr, buffer->page, count);
182 return attr->store(item, buffer->page, count);
Joel Becker7063fbf2005-12-15 14:29:43 -0800183}
184
185
186/**
187 * configfs_write_file - write an attribute.
188 * @file: file pointer
189 * @buf: data to write
190 * @count: number of bytes
191 * @ppos: starting offset
192 *
193 * Similar to configfs_read_file(), though working in the opposite direction.
194 * We allocate and fill the data from the user in fill_write_buffer(),
195 * then push it to the config_item in flush_write_buffer().
196 * There is no easy way for us to know if userspace is only doing a partial
197 * write, so we don't support them. We expect the entire buffer to come
198 * on the first write.
199 * Hint: if you're writing a value, first read the file, modify only the
200 * the value you're changing, then write entire buffer back.
201 */
202
203static ssize_t
204configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
205{
206 struct configfs_buffer * buffer = file->private_data;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800207 ssize_t len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800208
Johannes Berg6d748922007-06-22 11:20:00 +0200209 mutex_lock(&buffer->mutex);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800210 len = fill_write_buffer(buffer, buf, count);
211 if (len > 0)
Dan Carpenter71210642013-07-03 15:00:45 -0700212 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800213 if (len > 0)
214 *ppos += len;
Johannes Berg6d748922007-06-22 11:20:00 +0200215 mutex_unlock(&buffer->mutex);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800216 return len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800217}
218
219static int check_perm(struct inode * inode, struct file * file)
220{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800221 struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
222 struct configfs_attribute * attr = to_attr(file->f_path.dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800223 struct configfs_buffer * buffer;
224 struct configfs_item_operations * ops = NULL;
225 int error = 0;
226
227 if (!item || !attr)
228 goto Einval;
229
230 /* Grab the module reference for this attribute if we have one */
231 if (!try_module_get(attr->ca_owner)) {
232 error = -ENODEV;
233 goto Done;
234 }
235
236 if (item->ci_type)
237 ops = item->ci_type->ct_item_ops;
238 else
239 goto Eaccess;
240
241 /* File needs write support.
242 * The inode's perms must say it's ok,
243 * and we must have a store method.
244 */
245 if (file->f_mode & FMODE_WRITE) {
Christoph Hellwig870823e2015-10-03 15:32:37 +0200246 if (!(inode->i_mode & S_IWUGO) ||
247 (!ops->store_attribute && !attr->store))
Joel Becker7063fbf2005-12-15 14:29:43 -0800248 goto Eaccess;
249
250 }
251
252 /* File needs read support.
253 * The inode's perms must say it's ok, and we there
254 * must be a show method for it.
255 */
256 if (file->f_mode & FMODE_READ) {
Christoph Hellwig870823e2015-10-03 15:32:37 +0200257 if (!(inode->i_mode & S_IRUGO) ||
258 (!ops->show_attribute && !attr->show))
Joel Becker7063fbf2005-12-15 14:29:43 -0800259 goto Eaccess;
260 }
261
262 /* No error? Great, allocate a buffer for the file, and store it
263 * it in file->private_data for easy access.
264 */
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700265 buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700266 if (!buffer) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800267 error = -ENOMEM;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700268 goto Enomem;
269 }
Johannes Berg6d748922007-06-22 11:20:00 +0200270 mutex_init(&buffer->mutex);
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700271 buffer->needs_read_fill = 1;
272 buffer->ops = ops;
273 file->private_data = buffer;
Joel Becker7063fbf2005-12-15 14:29:43 -0800274 goto Done;
275
276 Einval:
277 error = -EINVAL;
278 goto Done;
279 Eaccess:
280 error = -EACCES;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700281 Enomem:
Joel Becker7063fbf2005-12-15 14:29:43 -0800282 module_put(attr->ca_owner);
283 Done:
284 if (error && item)
285 config_item_put(item);
286 return error;
287}
288
289static int configfs_open_file(struct inode * inode, struct file * filp)
290{
291 return check_perm(inode,filp);
292}
293
294static int configfs_release(struct inode * inode, struct file * filp)
295{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800296 struct config_item * item = to_item(filp->f_path.dentry->d_parent);
297 struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800298 struct module * owner = attr->ca_owner;
299 struct configfs_buffer * buffer = filp->private_data;
300
301 if (item)
302 config_item_put(item);
303 /* After this point, attr should not be accessed. */
304 module_put(owner);
305
306 if (buffer) {
307 if (buffer->page)
308 free_page((unsigned long)buffer->page);
Johannes Berg6d748922007-06-22 11:20:00 +0200309 mutex_destroy(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800310 kfree(buffer);
311 }
312 return 0;
313}
314
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800315const struct file_operations configfs_file_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -0800316 .read = configfs_read_file,
317 .write = configfs_write_file,
318 .llseek = generic_file_llseek,
319 .open = configfs_open_file,
320 .release = configfs_release,
321};
322
Joel Becker7063fbf2005-12-15 14:29:43 -0800323/**
324 * configfs_create_file - create an attribute file for an item.
325 * @item: item we're creating for.
326 * @attr: atrribute descriptor.
327 */
328
329int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
330{
Al Viro28444a22015-01-29 00:27:57 -0500331 struct dentry *dir = item->ci_dentry;
332 struct configfs_dirent *parent_sd = dir->d_fsdata;
333 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
334 int error = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800335
David Howells2b0143b2015-03-17 22:25:59 +0000336 mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_NORMAL);
Al Viro28444a22015-01-29 00:27:57 -0500337 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
338 CONFIGFS_ITEM_ATTR);
David Howells2b0143b2015-03-17 22:25:59 +0000339 mutex_unlock(&d_inode(dir)->i_mutex);
Al Viro28444a22015-01-29 00:27:57 -0500340
341 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800342}
343