configfs: Zero terminate data in configfs attribute writes.
[safe/jmp/linux-2.6] / fs / configfs / file.c
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>
29 #include <linux/slab.h>
30 #include <asm/uaccess.h>
31 #include <asm/semaphore.h>
32
33 #include <linux/configfs.h>
34 #include "configfs_internal.h"
35
36
37 struct configfs_buffer {
38         size_t                  count;
39         loff_t                  pos;
40         char                    * page;
41         struct configfs_item_operations * ops;
42         struct semaphore        sem;
43         int                     needs_read_fill;
44 };
45
46
47 /**
48  *      fill_read_buffer - allocate and fill buffer from item.
49  *      @dentry:        dentry pointer.
50  *      @buffer:        data buffer for file.
51  *
52  *      Allocate @buffer->page, if it hasn't been already, then call the
53  *      config_item's show() method to fill the buffer with this attribute's
54  *      data.
55  *      This is called only once, on the file's first read.
56  */
57 static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buffer)
58 {
59         struct configfs_attribute * attr = to_attr(dentry);
60         struct config_item * item = to_item(dentry->d_parent);
61         struct configfs_item_operations * ops = buffer->ops;
62         int ret = 0;
63         ssize_t count;
64
65         if (!buffer->page)
66                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
67         if (!buffer->page)
68                 return -ENOMEM;
69
70         count = ops->show_attribute(item,attr,buffer->page);
71         buffer->needs_read_fill = 0;
72         BUG_ON(count > (ssize_t)PAGE_SIZE);
73         if (count >= 0)
74                 buffer->count = count;
75         else
76                 ret = count;
77         return ret;
78 }
79
80
81 /**
82  *      flush_read_buffer - push buffer to userspace.
83  *      @buffer:        data buffer for file.
84  *      @userbuf:       user-passed buffer.
85  *      @count:         number of bytes requested.
86  *      @ppos:          file position.
87  *
88  *      Copy the buffer we filled in fill_read_buffer() to userspace.
89  *      This is done at the reader's leisure, copying and advancing
90  *      the amount they specify each time.
91  *      This may be called continuously until the buffer is empty.
92  */
93 static int flush_read_buffer(struct configfs_buffer * buffer, char __user * buf,
94                              size_t count, loff_t * ppos)
95 {
96         int error;
97
98         if (*ppos > buffer->count)
99                 return 0;
100
101         if (count > (buffer->count - *ppos))
102                 count = buffer->count - *ppos;
103
104         error = copy_to_user(buf,buffer->page + *ppos,count);
105         if (!error)
106                 *ppos += count;
107         return error ? -EFAULT : count;
108 }
109
110 /**
111  *      configfs_read_file - read an attribute.
112  *      @file:  file pointer.
113  *      @buf:   buffer to fill.
114  *      @count: number of bytes to read.
115  *      @ppos:  starting offset in file.
116  *
117  *      Userspace wants to read an attribute file. The attribute descriptor
118  *      is in the file's ->d_fsdata. The target item is in the directory's
119  *      ->d_fsdata.
120  *
121  *      We call fill_read_buffer() to allocate and fill the buffer from the
122  *      item's show() method exactly once (if the read is happening from
123  *      the beginning of the file). That should fill the entire buffer with
124  *      all the data the item has to offer for that attribute.
125  *      We then call flush_read_buffer() to copy the buffer to userspace
126  *      in the increments specified.
127  */
128
129 static ssize_t
130 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
131 {
132         struct configfs_buffer * buffer = file->private_data;
133         ssize_t retval = 0;
134
135         down(&buffer->sem);
136         if (buffer->needs_read_fill) {
137                 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
138                         goto out;
139         }
140         pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
141                  __FUNCTION__, count, *ppos, buffer->page);
142         retval = flush_read_buffer(buffer,buf,count,ppos);
143 out:
144         up(&buffer->sem);
145         return retval;
146 }
147
148
149 /**
150  *      fill_write_buffer - copy buffer from userspace.
151  *      @buffer:        data buffer for file.
152  *      @buf:           data from user.
153  *      @count:         number of bytes in @userbuf.
154  *
155  *      Allocate @buffer->page if it hasn't been already, then
156  *      copy the user-supplied buffer into it.
157  */
158
159 static int
160 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
161 {
162         int error;
163
164         if (!buffer->page)
165                 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
166         if (!buffer->page)
167                 return -ENOMEM;
168
169         if (count >= PAGE_SIZE)
170                 count = PAGE_SIZE - 1;
171         error = copy_from_user(buffer->page,buf,count);
172         buffer->needs_read_fill = 1;
173         /* if buf is assumed to contain a string, terminate it by \0,
174          * so e.g. sscanf() can scan the string easily */
175         buffer->page[count] = 0;
176         return error ? -EFAULT : count;
177 }
178
179
180 /**
181  *      flush_write_buffer - push buffer to config_item.
182  *      @dentry:        dentry to the attribute
183  *      @buffer:        data buffer for file.
184  *      @count:         number of bytes
185  *
186  *      Get the correct pointers for the config_item and the attribute we're
187  *      dealing with, then call the store() method for the attribute,
188  *      passing the buffer that we acquired in fill_write_buffer().
189  */
190
191 static int
192 flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size_t count)
193 {
194         struct configfs_attribute * attr = to_attr(dentry);
195         struct config_item * item = to_item(dentry->d_parent);
196         struct configfs_item_operations * ops = buffer->ops;
197
198         return ops->store_attribute(item,attr,buffer->page,count);
199 }
200
201
202 /**
203  *      configfs_write_file - write an attribute.
204  *      @file:  file pointer
205  *      @buf:   data to write
206  *      @count: number of bytes
207  *      @ppos:  starting offset
208  *
209  *      Similar to configfs_read_file(), though working in the opposite direction.
210  *      We allocate and fill the data from the user in fill_write_buffer(),
211  *      then push it to the config_item in flush_write_buffer().
212  *      There is no easy way for us to know if userspace is only doing a partial
213  *      write, so we don't support them. We expect the entire buffer to come
214  *      on the first write.
215  *      Hint: if you're writing a value, first read the file, modify only the
216  *      the value you're changing, then write entire buffer back.
217  */
218
219 static ssize_t
220 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
221 {
222         struct configfs_buffer * buffer = file->private_data;
223         ssize_t len;
224
225         down(&buffer->sem);
226         len = fill_write_buffer(buffer, buf, count);
227         if (len > 0)
228                 len = flush_write_buffer(file->f_path.dentry, buffer, count);
229         if (len > 0)
230                 *ppos += len;
231         up(&buffer->sem);
232         return len;
233 }
234
235 static int check_perm(struct inode * inode, struct file * file)
236 {
237         struct config_item *item = configfs_get_config_item(file->f_path.dentry->d_parent);
238         struct configfs_attribute * attr = to_attr(file->f_path.dentry);
239         struct configfs_buffer * buffer;
240         struct configfs_item_operations * ops = NULL;
241         int error = 0;
242
243         if (!item || !attr)
244                 goto Einval;
245
246         /* Grab the module reference for this attribute if we have one */
247         if (!try_module_get(attr->ca_owner)) {
248                 error = -ENODEV;
249                 goto Done;
250         }
251
252         if (item->ci_type)
253                 ops = item->ci_type->ct_item_ops;
254         else
255                 goto Eaccess;
256
257         /* File needs write support.
258          * The inode's perms must say it's ok,
259          * and we must have a store method.
260          */
261         if (file->f_mode & FMODE_WRITE) {
262
263                 if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute)
264                         goto Eaccess;
265
266         }
267
268         /* File needs read support.
269          * The inode's perms must say it's ok, and we there
270          * must be a show method for it.
271          */
272         if (file->f_mode & FMODE_READ) {
273                 if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute)
274                         goto Eaccess;
275         }
276
277         /* No error? Great, allocate a buffer for the file, and store it
278          * it in file->private_data for easy access.
279          */
280         buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
281         if (!buffer) {
282                 error = -ENOMEM;
283                 goto Enomem;
284         }
285         init_MUTEX(&buffer->sem);
286         buffer->needs_read_fill = 1;
287         buffer->ops = ops;
288         file->private_data = buffer;
289         goto Done;
290
291  Einval:
292         error = -EINVAL;
293         goto Done;
294  Eaccess:
295         error = -EACCES;
296  Enomem:
297         module_put(attr->ca_owner);
298  Done:
299         if (error && item)
300                 config_item_put(item);
301         return error;
302 }
303
304 static int configfs_open_file(struct inode * inode, struct file * filp)
305 {
306         return check_perm(inode,filp);
307 }
308
309 static int configfs_release(struct inode * inode, struct file * filp)
310 {
311         struct config_item * item = to_item(filp->f_path.dentry->d_parent);
312         struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
313         struct module * owner = attr->ca_owner;
314         struct configfs_buffer * buffer = filp->private_data;
315
316         if (item)
317                 config_item_put(item);
318         /* After this point, attr should not be accessed. */
319         module_put(owner);
320
321         if (buffer) {
322                 if (buffer->page)
323                         free_page((unsigned long)buffer->page);
324                 kfree(buffer);
325         }
326         return 0;
327 }
328
329 const struct file_operations configfs_file_operations = {
330         .read           = configfs_read_file,
331         .write          = configfs_write_file,
332         .llseek         = generic_file_llseek,
333         .open           = configfs_open_file,
334         .release        = configfs_release,
335 };
336
337
338 int configfs_add_file(struct dentry * dir, const struct configfs_attribute * attr, int type)
339 {
340         struct configfs_dirent * parent_sd = dir->d_fsdata;
341         umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
342         int error = 0;
343
344         mutex_lock(&dir->d_inode->i_mutex);
345         error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, type);
346         mutex_unlock(&dir->d_inode->i_mutex);
347
348         return error;
349 }
350
351
352 /**
353  *      configfs_create_file - create an attribute file for an item.
354  *      @item:  item we're creating for.
355  *      @attr:  atrribute descriptor.
356  */
357
358 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
359 {
360         BUG_ON(!item || !item->ci_dentry || !attr);
361
362         return configfs_add_file(item->ci_dentry, attr,
363                                  CONFIGFS_ITEM_ATTR);
364 }
365