GNU Linux-libre 4.9.296-gnu1
[releases.git] / 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 <linux/mutex.h>
31 #include <linux/vmalloc.h>
32 #include <asm/uaccess.h>
33
34 #include <linux/configfs.h>
35 #include "configfs_internal.h"
36
37 /*
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.
42  */
43 #define SIMPLE_ATTR_SIZE 4096
44
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;
62         };
63 };
64
65 static inline struct configfs_fragment *to_frag(struct file *file)
66 {
67         struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
68
69         return sd->s_frag;
70 }
71
72 static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
73 {
74         struct configfs_fragment *frag = to_frag(file);
75         ssize_t count = -ENOENT;
76
77         if (!buffer->page)
78                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
79         if (!buffer->page)
80                 return -ENOMEM;
81
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);
86
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;
94 }
95
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.
102  *
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.
106  *
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.
113  */
114
115 static ssize_t
116 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
117 {
118         struct configfs_buffer *buffer = file->private_data;
119         ssize_t retval = 0;
120
121         mutex_lock(&buffer->mutex);
122         if (buffer->needs_read_fill) {
123                 retval = fill_read_buffer(file, buffer);
124                 if (retval)
125                         goto out;
126         }
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;
134 }
135
136 /**
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.
142  *
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.
146  *
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.
154  */
155
156 static ssize_t
157 configfs_read_bin_file(struct file *file, char __user *buf,
158                        size_t count, loff_t *ppos)
159 {
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);
164
165         mutex_lock(&buffer->mutex);
166
167         /* we don't support switching read/write modes */
168         if (buffer->write_in_progress) {
169                 retval = -ETXTBSY;
170                 goto out;
171         }
172         buffer->read_in_progress = true;
173
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;
185                 }
186
187                 /* do not exceed the maximum value */
188                 if (buffer->cb_max_size && len > buffer->cb_max_size) {
189                         retval = -EFBIG;
190                         goto out;
191                 }
192
193                 buffer->bin_buffer = vmalloc(len);
194                 if (buffer->bin_buffer == NULL) {
195                         retval = -ENOMEM;
196                         goto out;
197                 }
198                 buffer->bin_buffer_size = len;
199
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;
214                 }
215
216                 buffer->needs_read_fill = 0;
217         }
218
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;
224 }
225
226
227 /**
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.
232  *
233  *      Allocate @buffer->page if it hasn't been already, then
234  *      copy the user-supplied buffer into it.
235  */
236
237 static int
238 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
239 {
240         int error;
241
242         if (!buffer->page)
243                 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
244         if (!buffer->page)
245                 return -ENOMEM;
246
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;
255 }
256
257 static int
258 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
259 {
260         struct configfs_fragment *frag = to_frag(file);
261         int res = -ENOENT;
262
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;
268 }
269
270
271 /**
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
277  *
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.
286  */
287
288 static ssize_t
289 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
290 {
291         struct configfs_buffer *buffer = file->private_data;
292         ssize_t len;
293
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;
302 }
303
304 /**
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
310  *
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.
315  */
316
317 static ssize_t
318 configfs_write_bin_file(struct file *file, const char __user *buf,
319                         size_t count, loff_t *ppos)
320 {
321         struct configfs_buffer *buffer = file->private_data;
322         void *tbuf = NULL;
323         ssize_t len;
324
325         mutex_lock(&buffer->mutex);
326
327         /* we don't support switching read/write modes */
328         if (buffer->read_in_progress) {
329                 len = -ETXTBSY;
330                 goto out;
331         }
332         buffer->write_in_progress = true;
333
334         /* buffer grows? */
335         if (*ppos + count > buffer->bin_buffer_size) {
336
337                 if (buffer->cb_max_size &&
338                         *ppos + count > buffer->cb_max_size) {
339                         len = -EFBIG;
340                         goto out;
341                 }
342
343                 tbuf = vmalloc(*ppos + count);
344                 if (tbuf == NULL) {
345                         len = -ENOMEM;
346                         goto out;
347                 }
348
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);
354                 }
355
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;
361         }
362
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;
368 }
369
370 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
371 {
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;
377
378         error = -ENOMEM;
379         buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
380         if (!buffer)
381                 goto out;
382
383         error = -ENOENT;
384         down_read(&frag->frag_sem);
385         if (unlikely(frag->frag_dead))
386                 goto out_free_buffer;
387
388         error = -EINVAL;
389         buffer->item = to_item(dentry->d_parent);
390         if (!buffer->item)
391                 goto out_free_buffer;
392
393         attr = to_attr(dentry);
394         if (!attr)
395                 goto out_free_buffer;
396
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;
402         }
403
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_free_buffer;
409
410         error = -EACCES;
411         if (!buffer->item->ci_type)
412                 goto out_put_module;
413
414         buffer->ops = buffer->item->ci_type->ct_item_ops;
415
416         /* File needs write support.
417          * The inode's perms must say it's ok,
418          * and we must have a store method.
419          */
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;
427         }
428
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.
432          */
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;
440         }
441
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;
449
450 out_put_module:
451         module_put(buffer->owner);
452 out_free_buffer:
453         up_read(&frag->frag_sem);
454         kfree(buffer);
455 out:
456         return error;
457 }
458
459 static int configfs_release(struct inode *inode, struct file *filp)
460 {
461         struct configfs_buffer *buffer = filp->private_data;
462
463         module_put(buffer->owner);
464         if (buffer->page)
465                 free_page((unsigned long)buffer->page);
466         mutex_destroy(&buffer->mutex);
467         kfree(buffer);
468         return 0;
469 }
470
471 static int configfs_open_file(struct inode *inode, struct file *filp)
472 {
473         return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
474 }
475
476 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
477 {
478         return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
479 }
480
481 static int configfs_release_bin_file(struct inode *inode, struct file *file)
482 {
483         struct configfs_buffer *buffer = file->private_data;
484
485         buffer->read_in_progress = false;
486
487         if (buffer->write_in_progress) {
488                 struct configfs_fragment *frag = to_frag(file);
489                 buffer->write_in_progress = false;
490
491                 down_read(&frag->frag_sem);
492                 if (!frag->frag_dead) {
493                         /* result of ->release() is ignored */
494                         buffer->bin_attr->write(buffer->item,
495                                         buffer->bin_buffer,
496                                         buffer->bin_buffer_size);
497                 }
498                 up_read(&frag->frag_sem);
499         }
500
501         vfree(buffer->bin_buffer);
502         buffer->bin_buffer = NULL;
503         buffer->bin_buffer_size = 0;
504         buffer->needs_read_fill = 1;
505
506         configfs_release(inode, file);
507         return 0;
508 }
509
510
511 const struct file_operations configfs_file_operations = {
512         .read           = configfs_read_file,
513         .write          = configfs_write_file,
514         .llseek         = generic_file_llseek,
515         .open           = configfs_open_file,
516         .release        = configfs_release,
517 };
518
519 const struct file_operations configfs_bin_file_operations = {
520         .read           = configfs_read_bin_file,
521         .write          = configfs_write_bin_file,
522         .llseek         = NULL,         /* bin file is not seekable */
523         .open           = configfs_open_bin_file,
524         .release        = configfs_release_bin_file,
525 };
526
527 /**
528  *      configfs_create_file - create an attribute file for an item.
529  *      @item:  item we're creating for.
530  *      @attr:  atrribute descriptor.
531  */
532
533 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
534 {
535         struct dentry *dir = item->ci_dentry;
536         struct configfs_dirent *parent_sd = dir->d_fsdata;
537         umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
538         int error = 0;
539
540         inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
541         error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
542                                      CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
543         inode_unlock(d_inode(dir));
544
545         return error;
546 }
547
548 /**
549  *      configfs_create_bin_file - create a binary attribute file for an item.
550  *      @item:  item we're creating for.
551  *      @attr:  atrribute descriptor.
552  */
553
554 int configfs_create_bin_file(struct config_item *item,
555                 const struct configfs_bin_attribute *bin_attr)
556 {
557         struct dentry *dir = item->ci_dentry;
558         struct configfs_dirent *parent_sd = dir->d_fsdata;
559         umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
560         int error = 0;
561
562         inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
563         error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
564                                      CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
565         inode_unlock(dir->d_inode);
566
567         return error;
568 }