]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/configfs/file.c
UBUNTU: Start new release
[mirror_ubuntu-artful-kernel.git] / fs / configfs / file.c
CommitLineData
7063fbf2
JB
1/* -*- 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>
7063fbf2 29#include <linux/slab.h>
6d748924 30#include <linux/mutex.h>
7063fbf2 31#include <asm/uaccess.h>
7063fbf2
JB
32
33#include <linux/configfs.h>
34#include "configfs_internal.h"
35
b23cdde4
JB
36/*
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
7063fbf2
JB
43
44struct configfs_buffer {
45 size_t count;
46 loff_t pos;
47 char * page;
48 struct configfs_item_operations * ops;
6d748924 49 struct mutex mutex;
7063fbf2
JB
50 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);
7063fbf2
JB
68 int ret = 0;
69 ssize_t count;
70
71 if (!buffer->page)
72 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
73 if (!buffer->page)
74 return -ENOMEM;
75
51798222 76 count = attr->show(item, buffer->page);
870823e6 77
7063fbf2 78 buffer->needs_read_fill = 0;
b23cdde4 79 BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
7063fbf2
JB
80 if (count >= 0)
81 buffer->count = count;
82 else
83 ret = count;
84 return ret;
85}
86
7063fbf2
JB
87/**
88 * configfs_read_file - read an attribute.
89 * @file: file pointer.
90 * @buf: buffer to fill.
91 * @count: number of bytes to read.
92 * @ppos: starting offset in file.
93 *
94 * Userspace wants to read an attribute file. The attribute descriptor
95 * is in the file's ->d_fsdata. The target item is in the directory's
96 * ->d_fsdata.
97 *
98 * We call fill_read_buffer() to allocate and fill the buffer from the
99 * item's show() method exactly once (if the read is happening from
100 * the beginning of the file). That should fill the entire buffer with
101 * all the data the item has to offer for that attribute.
102 * We then call flush_read_buffer() to copy the buffer to userspace
103 * in the increments specified.
104 */
105
106static ssize_t
107configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
108{
109 struct configfs_buffer * buffer = file->private_data;
110 ssize_t retval = 0;
111
6d748924 112 mutex_lock(&buffer->mutex);
7063fbf2 113 if (buffer->needs_read_fill) {
867fa491 114 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
7063fbf2
JB
115 goto out;
116 }
4779efca 117 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
8e24eea7 118 __func__, count, *ppos, buffer->page);
92f4c701
AM
119 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
120 buffer->count);
7063fbf2 121out:
6d748924 122 mutex_unlock(&buffer->mutex);
7063fbf2
JB
123 return retval;
124}
125
126
127/**
128 * fill_write_buffer - copy buffer from userspace.
129 * @buffer: data buffer for file.
3d0f89bb 130 * @buf: data from user.
7063fbf2
JB
131 * @count: number of bytes in @userbuf.
132 *
133 * Allocate @buffer->page if it hasn't been already, then
134 * copy the user-supplied buffer into it.
135 */
136
137static int
138fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
139{
140 int error;
141
142 if (!buffer->page)
ff05d1c4 143 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
7063fbf2
JB
144 if (!buffer->page)
145 return -ENOMEM;
146
b23cdde4
JB
147 if (count >= SIMPLE_ATTR_SIZE)
148 count = SIMPLE_ATTR_SIZE - 1;
7063fbf2
JB
149 error = copy_from_user(buffer->page,buf,count);
150 buffer->needs_read_fill = 1;
ff05d1c4
JB
151 /* if buf is assumed to contain a string, terminate it by \0,
152 * so e.g. sscanf() can scan the string easily */
153 buffer->page[count] = 0;
7063fbf2
JB
154 return error ? -EFAULT : count;
155}
156
157
158/**
159 * flush_write_buffer - push buffer to config_item.
3d0f89bb 160 * @dentry: dentry to the attribute
7063fbf2 161 * @buffer: data buffer for file.
3d0f89bb 162 * @count: number of bytes
7063fbf2
JB
163 *
164 * Get the correct pointers for the config_item and the attribute we're
165 * dealing with, then call the store() method for the attribute,
166 * passing the buffer that we acquired in fill_write_buffer().
167 */
168
169static int
170flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
171{
172 struct configfs_attribute * attr = to_attr(dentry);
173 struct config_item * item = to_item(dentry->d_parent);
7063fbf2 174
870823e6 175 return attr->store(item, buffer->page, count);
7063fbf2
JB
176}
177
178
179/**
180 * configfs_write_file - write an attribute.
181 * @file: file pointer
182 * @buf: data to write
183 * @count: number of bytes
184 * @ppos: starting offset
185 *
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 partial
190 * write, so we don't support them. We expect the entire buffer to come
191 * on the first write.
192 * Hint: if you're writing a value, first read the file, modify only the
193 * the value you're changing, then write entire buffer back.
194 */
195
196static ssize_t
197configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
198{
199 struct configfs_buffer * buffer = file->private_data;
3d0f89bb 200 ssize_t len;
7063fbf2 201
6d748924 202 mutex_lock(&buffer->mutex);
3d0f89bb
JB
203 len = fill_write_buffer(buffer, buf, count);
204 if (len > 0)
7121064b 205 len = flush_write_buffer(file->f_path.dentry, buffer, len);
3d0f89bb
JB
206 if (len > 0)
207 *ppos += len;
6d748924 208 mutex_unlock(&buffer->mutex);
3d0f89bb 209 return len;
7063fbf2
JB
210}
211
212static int check_perm(struct inode * inode, struct file * file)
213{
867fa491
JJS
214 struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
215 struct configfs_attribute * attr = to_attr(file->f_path.dentry);
7063fbf2
JB
216 struct configfs_buffer * buffer;
217 struct configfs_item_operations * ops = NULL;
218 int error = 0;
219
220 if (!item || !attr)
221 goto Einval;
222
223 /* Grab the module reference for this attribute if we have one */
224 if (!try_module_get(attr->ca_owner)) {
225 error = -ENODEV;
226 goto Done;
227 }
228
229 if (item->ci_type)
230 ops = item->ci_type->ct_item_ops;
231 else
232 goto Eaccess;
233
234 /* File needs write support.
235 * The inode's perms must say it's ok,
236 * and we must have a store method.
237 */
238 if (file->f_mode & FMODE_WRITE) {
51798222 239 if (!(inode->i_mode & S_IWUGO) || !attr->store)
7063fbf2
JB
240 goto Eaccess;
241
242 }
243
244 /* File needs read support.
245 * The inode's perms must say it's ok, and we there
246 * must be a show method for it.
247 */
248 if (file->f_mode & FMODE_READ) {
51798222 249 if (!(inode->i_mode & S_IRUGO) || !attr->show)
7063fbf2
JB
250 goto Eaccess;
251 }
252
253 /* No error? Great, allocate a buffer for the file, and store it
254 * it in file->private_data for easy access.
255 */
f8314dc6 256 buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
559c9ac3 257 if (!buffer) {
7063fbf2 258 error = -ENOMEM;
559c9ac3
CS
259 goto Enomem;
260 }
6d748924 261 mutex_init(&buffer->mutex);
559c9ac3
CS
262 buffer->needs_read_fill = 1;
263 buffer->ops = ops;
264 file->private_data = buffer;
7063fbf2
JB
265 goto Done;
266
267 Einval:
268 error = -EINVAL;
269 goto Done;
270 Eaccess:
271 error = -EACCES;
559c9ac3 272 Enomem:
7063fbf2
JB
273 module_put(attr->ca_owner);
274 Done:
275 if (error && item)
276 config_item_put(item);
277 return error;
278}
279
280static int configfs_open_file(struct inode * inode, struct file * filp)
281{
282 return check_perm(inode,filp);
283}
284
285static int configfs_release(struct inode * inode, struct file * filp)
286{
867fa491
JJS
287 struct config_item * item = to_item(filp->f_path.dentry->d_parent);
288 struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
7063fbf2
JB
289 struct module * owner = attr->ca_owner;
290 struct configfs_buffer * buffer = filp->private_data;
291
292 if (item)
293 config_item_put(item);
294 /* After this point, attr should not be accessed. */
295 module_put(owner);
296
297 if (buffer) {
298 if (buffer->page)
299 free_page((unsigned long)buffer->page);
6d748924 300 mutex_destroy(&buffer->mutex);
7063fbf2
JB
301 kfree(buffer);
302 }
303 return 0;
304}
305
4b6f5d20 306const struct file_operations configfs_file_operations = {
7063fbf2
JB
307 .read = configfs_read_file,
308 .write = configfs_write_file,
309 .llseek = generic_file_llseek,
310 .open = configfs_open_file,
311 .release = configfs_release,
312};
313
7063fbf2
JB
314/**
315 * configfs_create_file - create an attribute file for an item.
316 * @item: item we're creating for.
317 * @attr: atrribute descriptor.
318 */
319
320int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
321{
28444a2b
AV
322 struct dentry *dir = item->ci_dentry;
323 struct configfs_dirent *parent_sd = dir->d_fsdata;
324 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
325 int error = 0;
7063fbf2 326
2b0143b5 327 mutex_lock_nested(&d_inode(dir)->i_mutex, I_MUTEX_NORMAL);
28444a2b
AV
328 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
329 CONFIGFS_ITEM_ATTR);
2b0143b5 330 mutex_unlock(&d_inode(dir)->i_mutex);
28444a2b
AV
331
332 return error;
7063fbf2
JB
333}
334