arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / configfs / file.c
blob7285440bc62e83049dab488fabe0ad6dc096e38f
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * file.c - operations for regular (text) files.
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.
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.
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.
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
27 #include <linux/fs.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 #include <linux/vmalloc.h>
32 #include <asm/uaccess.h>
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
38 * A simple attribute can only be 4096 characters. Why 4k? Because the
39 * original code limited it to PAGE_SIZE. That's a bad idea, though,
40 * because an attribute of 16k on ia64 won't work on x86. So we limit to
41 * 4k, our minimum common page size.
43 #define SIMPLE_ATTR_SIZE 4096
45 struct configfs_buffer {
46 size_t count;
47 loff_t pos;
48 char * page;
49 struct configfs_item_operations * ops;
50 struct mutex mutex;
51 int needs_read_fill;
52 bool read_in_progress;
53 bool write_in_progress;
54 char *bin_buffer;
55 int bin_buffer_size;
56 int cb_max_size;
57 struct config_item *item;
58 struct module *owner;
59 union {
60 struct configfs_attribute *attr;
61 struct configfs_bin_attribute *bin_attr;
65 static inline struct configfs_fragment *to_frag(struct file *file)
67 struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
69 return sd->s_frag;
72 static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
74 struct configfs_fragment *frag = to_frag(file);
75 ssize_t count = -ENOENT;
77 if (!buffer->page)
78 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
79 if (!buffer->page)
80 return -ENOMEM;
82 down_read(&frag->frag_sem);
83 if (!frag->frag_dead)
84 count = buffer->attr->show(buffer->item, buffer->page);
85 up_read(&frag->frag_sem);
87 if (count < 0)
88 return count;
89 if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
90 return -EIO;
91 buffer->needs_read_fill = 0;
92 buffer->count = count;
93 return 0;
96 /**
97 * configfs_read_file - read an attribute.
98 * @file: file pointer.
99 * @buf: buffer to fill.
100 * @count: number of bytes to read.
101 * @ppos: starting offset in file.
103 * Userspace wants to read an attribute file. The attribute descriptor
104 * is in the file's ->d_fsdata. The target item is in the directory's
105 * ->d_fsdata.
107 * We call fill_read_buffer() to allocate and fill the buffer from the
108 * item's show() method exactly once (if the read is happening from
109 * the beginning of the file). That should fill the entire buffer with
110 * all the data the item has to offer for that attribute.
111 * We then call flush_read_buffer() to copy the buffer to userspace
112 * in the increments specified.
115 static ssize_t
116 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
118 struct configfs_buffer *buffer = file->private_data;
119 ssize_t retval = 0;
121 mutex_lock(&buffer->mutex);
122 if (buffer->needs_read_fill) {
123 retval = fill_read_buffer(file, buffer);
124 if (retval)
125 goto out;
127 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
128 __func__, count, *ppos, buffer->page);
129 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
130 buffer->count);
131 out:
132 mutex_unlock(&buffer->mutex);
133 return retval;
137 * configfs_read_bin_file - read a binary attribute.
138 * @file: file pointer.
139 * @buf: buffer to fill.
140 * @count: number of bytes to read.
141 * @ppos: starting offset in file.
143 * Userspace wants to read a binary attribute file. The attribute
144 * descriptor is in the file's ->d_fsdata. The target item is in the
145 * directory's ->d_fsdata.
147 * We check whether we need to refill the buffer. If so we will
148 * call the attributes' attr->read() twice. The first time we
149 * will pass a NULL as a buffer pointer, which the attributes' method
150 * will use to return the size of the buffer required. If no error
151 * occurs we will allocate the buffer using vmalloc and call
152 * attr->read() again passing that buffer as an argument.
153 * Then we just copy to user-space using simple_read_from_buffer.
156 static ssize_t
157 configfs_read_bin_file(struct file *file, char __user *buf,
158 size_t count, loff_t *ppos)
160 struct configfs_fragment *frag = to_frag(file);
161 struct configfs_buffer *buffer = file->private_data;
162 ssize_t retval = 0;
163 ssize_t len = min_t(size_t, count, PAGE_SIZE);
165 mutex_lock(&buffer->mutex);
167 /* we don't support switching read/write modes */
168 if (buffer->write_in_progress) {
169 retval = -ETXTBSY;
170 goto out;
172 buffer->read_in_progress = true;
174 if (buffer->needs_read_fill) {
175 /* perform first read with buf == NULL to get extent */
176 down_read(&frag->frag_sem);
177 if (!frag->frag_dead)
178 len = buffer->bin_attr->read(buffer->item, NULL, 0);
179 else
180 len = -ENOENT;
181 up_read(&frag->frag_sem);
182 if (len <= 0) {
183 retval = len;
184 goto out;
187 /* do not exceed the maximum value */
188 if (buffer->cb_max_size && len > buffer->cb_max_size) {
189 retval = -EFBIG;
190 goto out;
193 buffer->bin_buffer = vmalloc(len);
194 if (buffer->bin_buffer == NULL) {
195 retval = -ENOMEM;
196 goto out;
198 buffer->bin_buffer_size = len;
200 /* perform second read to fill buffer */
201 down_read(&frag->frag_sem);
202 if (!frag->frag_dead)
203 len = buffer->bin_attr->read(buffer->item,
204 buffer->bin_buffer, len);
205 else
206 len = -ENOENT;
207 up_read(&frag->frag_sem);
208 if (len < 0) {
209 retval = len;
210 vfree(buffer->bin_buffer);
211 buffer->bin_buffer_size = 0;
212 buffer->bin_buffer = NULL;
213 goto out;
216 buffer->needs_read_fill = 0;
219 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
220 buffer->bin_buffer_size);
221 out:
222 mutex_unlock(&buffer->mutex);
223 return retval;
228 * fill_write_buffer - copy buffer from userspace.
229 * @buffer: data buffer for file.
230 * @buf: data from user.
231 * @count: number of bytes in @userbuf.
233 * Allocate @buffer->page if it hasn't been already, then
234 * copy the user-supplied buffer into it.
237 static int
238 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
240 int error;
242 if (!buffer->page)
243 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
244 if (!buffer->page)
245 return -ENOMEM;
247 if (count >= SIMPLE_ATTR_SIZE)
248 count = SIMPLE_ATTR_SIZE - 1;
249 error = copy_from_user(buffer->page,buf,count);
250 buffer->needs_read_fill = 1;
251 /* if buf is assumed to contain a string, terminate it by \0,
252 * so e.g. sscanf() can scan the string easily */
253 buffer->page[count] = 0;
254 return error ? -EFAULT : count;
257 static int
258 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
260 struct configfs_fragment *frag = to_frag(file);
261 int res = -ENOENT;
263 down_read(&frag->frag_sem);
264 if (!frag->frag_dead)
265 res = buffer->attr->store(buffer->item, buffer->page, count);
266 up_read(&frag->frag_sem);
267 return res;
272 * configfs_write_file - write an attribute.
273 * @file: file pointer
274 * @buf: data to write
275 * @count: number of bytes
276 * @ppos: starting offset
278 * Similar to configfs_read_file(), though working in the opposite direction.
279 * We allocate and fill the data from the user in fill_write_buffer(),
280 * then push it to the config_item in flush_write_buffer().
281 * There is no easy way for us to know if userspace is only doing a partial
282 * write, so we don't support them. We expect the entire buffer to come
283 * on the first write.
284 * Hint: if you're writing a value, first read the file, modify only the
285 * the value you're changing, then write entire buffer back.
288 static ssize_t
289 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
291 struct configfs_buffer *buffer = file->private_data;
292 ssize_t len;
294 mutex_lock(&buffer->mutex);
295 len = fill_write_buffer(buffer, buf, count);
296 if (len > 0)
297 len = flush_write_buffer(file, buffer, len);
298 if (len > 0)
299 *ppos += len;
300 mutex_unlock(&buffer->mutex);
301 return len;
305 * configfs_write_bin_file - write a binary attribute.
306 * @file: file pointer
307 * @buf: data to write
308 * @count: number of bytes
309 * @ppos: starting offset
311 * Writing to a binary attribute file is similar to a normal read.
312 * We buffer the consecutive writes (binary attribute files do not
313 * support lseek) in a continuously growing buffer, but we don't
314 * commit until the close of the file.
317 static ssize_t
318 configfs_write_bin_file(struct file *file, const char __user *buf,
319 size_t count, loff_t *ppos)
321 struct configfs_buffer *buffer = file->private_data;
322 void *tbuf = NULL;
323 ssize_t len;
325 mutex_lock(&buffer->mutex);
327 /* we don't support switching read/write modes */
328 if (buffer->read_in_progress) {
329 len = -ETXTBSY;
330 goto out;
332 buffer->write_in_progress = true;
334 /* buffer grows? */
335 if (*ppos + count > buffer->bin_buffer_size) {
337 if (buffer->cb_max_size &&
338 *ppos + count > buffer->cb_max_size) {
339 len = -EFBIG;
340 goto out;
343 tbuf = vmalloc(*ppos + count);
344 if (tbuf == NULL) {
345 len = -ENOMEM;
346 goto out;
349 /* copy old contents */
350 if (buffer->bin_buffer) {
351 memcpy(tbuf, buffer->bin_buffer,
352 buffer->bin_buffer_size);
353 vfree(buffer->bin_buffer);
356 /* clear the new area */
357 memset(tbuf + buffer->bin_buffer_size, 0,
358 *ppos + count - buffer->bin_buffer_size);
359 buffer->bin_buffer = tbuf;
360 buffer->bin_buffer_size = *ppos + count;
363 len = simple_write_to_buffer(buffer->bin_buffer,
364 buffer->bin_buffer_size, ppos, buf, count);
365 out:
366 mutex_unlock(&buffer->mutex);
367 return len;
370 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
372 struct dentry *dentry = file->f_path.dentry;
373 struct configfs_fragment *frag = to_frag(file);
374 struct configfs_attribute *attr;
375 struct configfs_buffer *buffer;
376 int error;
378 error = -ENOMEM;
379 buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
380 if (!buffer)
381 goto out;
383 error = -ENOENT;
384 down_read(&frag->frag_sem);
385 if (unlikely(frag->frag_dead))
386 goto out_free_buffer;
388 error = -EINVAL;
389 buffer->item = to_item(dentry->d_parent);
390 if (!buffer->item)
391 goto out_free_buffer;
393 attr = to_attr(dentry);
394 if (!attr)
395 goto out_put_item;
397 if (type & CONFIGFS_ITEM_BIN_ATTR) {
398 buffer->bin_attr = to_bin_attr(dentry);
399 buffer->cb_max_size = buffer->bin_attr->cb_max_size;
400 } else {
401 buffer->attr = attr;
404 buffer->owner = attr->ca_owner;
405 /* Grab the module reference for this attribute if we have one */
406 error = -ENODEV;
407 if (!try_module_get(buffer->owner))
408 goto out_put_item;
410 error = -EACCES;
411 if (!buffer->item->ci_type)
412 goto out_put_module;
414 buffer->ops = buffer->item->ci_type->ct_item_ops;
416 /* File needs write support.
417 * The inode's perms must say it's ok,
418 * and we must have a store method.
420 if (file->f_mode & FMODE_WRITE) {
421 if (!(inode->i_mode & S_IWUGO))
422 goto out_put_module;
423 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
424 goto out_put_module;
425 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
426 goto out_put_module;
429 /* File needs read support.
430 * The inode's perms must say it's ok, and we there
431 * must be a show method for it.
433 if (file->f_mode & FMODE_READ) {
434 if (!(inode->i_mode & S_IRUGO))
435 goto out_put_module;
436 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
437 goto out_put_module;
438 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
439 goto out_put_module;
442 mutex_init(&buffer->mutex);
443 buffer->needs_read_fill = 1;
444 buffer->read_in_progress = false;
445 buffer->write_in_progress = false;
446 file->private_data = buffer;
447 up_read(&frag->frag_sem);
448 return 0;
450 out_put_module:
451 module_put(buffer->owner);
452 out_put_item:
453 config_item_put(buffer->item);
454 out_free_buffer:
455 up_read(&frag->frag_sem);
456 kfree(buffer);
457 out:
458 return error;
461 static int configfs_release(struct inode *inode, struct file *filp)
463 struct configfs_buffer *buffer = filp->private_data;
465 module_put(buffer->owner);
466 if (buffer->page)
467 free_page((unsigned long)buffer->page);
468 mutex_destroy(&buffer->mutex);
469 kfree(buffer);
470 return 0;
473 static int configfs_open_file(struct inode *inode, struct file *filp)
475 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
478 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
480 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
483 static int configfs_release_bin_file(struct inode *inode, struct file *file)
485 struct configfs_buffer *buffer = file->private_data;
487 buffer->read_in_progress = false;
489 if (buffer->write_in_progress) {
490 struct configfs_fragment *frag = to_frag(file);
491 buffer->write_in_progress = false;
493 down_read(&frag->frag_sem);
494 if (!frag->frag_dead) {
495 /* result of ->release() is ignored */
496 buffer->bin_attr->write(buffer->item,
497 buffer->bin_buffer,
498 buffer->bin_buffer_size);
500 up_read(&frag->frag_sem);
501 /* vfree on NULL is safe */
502 vfree(buffer->bin_buffer);
503 buffer->bin_buffer = NULL;
504 buffer->bin_buffer_size = 0;
505 buffer->needs_read_fill = 1;
508 configfs_release(inode, file);
509 return 0;
513 const struct file_operations configfs_file_operations = {
514 .read = configfs_read_file,
515 .write = configfs_write_file,
516 .llseek = generic_file_llseek,
517 .open = configfs_open_file,
518 .release = configfs_release,
521 const struct file_operations configfs_bin_file_operations = {
522 .read = configfs_read_bin_file,
523 .write = configfs_write_bin_file,
524 .llseek = NULL, /* bin file is not seekable */
525 .open = configfs_open_bin_file,
526 .release = configfs_release_bin_file,
530 * configfs_create_file - create an attribute file for an item.
531 * @item: item we're creating for.
532 * @attr: atrribute descriptor.
535 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
537 struct dentry *dir = item->ci_dentry;
538 struct configfs_dirent *parent_sd = dir->d_fsdata;
539 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
540 int error = 0;
542 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
543 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
544 CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
545 inode_unlock(d_inode(dir));
547 return error;
551 * configfs_create_bin_file - create a binary attribute file for an item.
552 * @item: item we're creating for.
553 * @attr: atrribute descriptor.
556 int configfs_create_bin_file(struct config_item *item,
557 const struct configfs_bin_attribute *bin_attr)
559 struct dentry *dir = item->ci_dentry;
560 struct configfs_dirent *parent_sd = dir->d_fsdata;
561 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
562 int error = 0;
564 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
565 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
566 CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
567 inode_unlock(dir->d_inode);
569 return error;