treewide: remove editor modelines and cruft
[linux-block.git] / fs / configfs / file.c
CommitLineData
328970de 1// SPDX-License-Identifier: GPL-2.0-or-later
fa60ce2c 2/*
7063fbf2
JB
3 * file.c - operations for regular (text) files.
4 *
7063fbf2
JB
5 * Based on sysfs:
6 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
7 *
8 * configfs Copyright (C) 2005 Oracle. All rights reserved.
9 */
10
11#include <linux/fs.h>
12#include <linux/module.h>
7063fbf2 13#include <linux/slab.h>
6d748924 14#include <linux/mutex.h>
03607ace 15#include <linux/vmalloc.h>
7c0f6ba6 16#include <linux/uaccess.h>
7063fbf2
JB
17
18#include <linux/configfs.h>
19#include "configfs_internal.h"
20
b23cdde4
JB
21/*
22 * A simple attribute can only be 4096 characters. Why 4k? Because the
23 * original code limited it to PAGE_SIZE. That's a bad idea, though,
24 * because an attribute of 16k on ia64 won't work on x86. So we limit to
25 * 4k, our minimum common page size.
26 */
27#define SIMPLE_ATTR_SIZE 4096
7063fbf2
JB
28
29struct configfs_buffer {
30 size_t count;
31 loff_t pos;
32 char * page;
33 struct configfs_item_operations * ops;
6d748924 34 struct mutex mutex;
7063fbf2 35 int needs_read_fill;
03607ace
PA
36 bool read_in_progress;
37 bool write_in_progress;
38 char *bin_buffer;
39 int bin_buffer_size;
ff4dd081
AV
40 int cb_max_size;
41 struct config_item *item;
42 struct module *owner;
43 union {
44 struct configfs_attribute *attr;
45 struct configfs_bin_attribute *bin_attr;
46 };
7063fbf2
JB
47};
48
b0841eef
AV
49static inline struct configfs_fragment *to_frag(struct file *file)
50{
51 struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
52
53 return sd->s_frag;
54}
7063fbf2 55
b0841eef 56static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
7063fbf2 57{
b0841eef
AV
58 struct configfs_fragment *frag = to_frag(file);
59 ssize_t count = -ENOENT;
7063fbf2
JB
60
61 if (!buffer->page)
62 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
63 if (!buffer->page)
64 return -ENOMEM;
65
b0841eef
AV
66 down_read(&frag->frag_sem);
67 if (!frag->frag_dead)
68 count = buffer->attr->show(buffer->item, buffer->page);
69 up_read(&frag->frag_sem);
70
ff4dd081
AV
71 if (count < 0)
72 return count;
73 if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
74 return -EIO;
ff4dd081
AV
75 buffer->needs_read_fill = 0;
76 buffer->count = count;
77 return 0;
7063fbf2
JB
78}
79
7063fbf2
JB
80/**
81 * configfs_read_file - read an attribute.
82 * @file: file pointer.
83 * @buf: buffer to fill.
84 * @count: number of bytes to read.
85 * @ppos: starting offset in file.
86 *
87 * Userspace wants to read an attribute file. The attribute descriptor
88 * is in the file's ->d_fsdata. The target item is in the directory's
89 * ->d_fsdata.
90 *
91 * We call fill_read_buffer() to allocate and fill the buffer from the
92 * item's show() method exactly once (if the read is happening from
93 * the beginning of the file). That should fill the entire buffer with
94 * all the data the item has to offer for that attribute.
95 * We then call flush_read_buffer() to copy the buffer to userspace
96 * in the increments specified.
97 */
98
99static ssize_t
100configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
101{
ff4dd081 102 struct configfs_buffer *buffer = file->private_data;
7063fbf2
JB
103 ssize_t retval = 0;
104
6d748924 105 mutex_lock(&buffer->mutex);
7063fbf2 106 if (buffer->needs_read_fill) {
b0841eef 107 retval = fill_read_buffer(file, buffer);
ff4dd081 108 if (retval)
7063fbf2
JB
109 goto out;
110 }
4779efca 111 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
8e24eea7 112 __func__, count, *ppos, buffer->page);
92f4c701
AM
113 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
114 buffer->count);
7063fbf2 115out:
6d748924 116 mutex_unlock(&buffer->mutex);
7063fbf2
JB
117 return retval;
118}
119
03607ace
PA
120/**
121 * configfs_read_bin_file - read a binary attribute.
122 * @file: file pointer.
123 * @buf: buffer to fill.
124 * @count: number of bytes to read.
125 * @ppos: starting offset in file.
126 *
127 * Userspace wants to read a binary attribute file. The attribute
128 * descriptor is in the file's ->d_fsdata. The target item is in the
129 * directory's ->d_fsdata.
130 *
131 * We check whether we need to refill the buffer. If so we will
132 * call the attributes' attr->read() twice. The first time we
133 * will pass a NULL as a buffer pointer, which the attributes' method
134 * will use to return the size of the buffer required. If no error
135 * occurs we will allocate the buffer using vmalloc and call
136 * attr->read() again passing that buffer as an argument.
137 * Then we just copy to user-space using simple_read_from_buffer.
138 */
139
140static ssize_t
141configfs_read_bin_file(struct file *file, char __user *buf,
142 size_t count, loff_t *ppos)
143{
b0841eef 144 struct configfs_fragment *frag = to_frag(file);
03607ace 145 struct configfs_buffer *buffer = file->private_data;
03607ace
PA
146 ssize_t retval = 0;
147 ssize_t len = min_t(size_t, count, PAGE_SIZE);
148
149 mutex_lock(&buffer->mutex);
150
151 /* we don't support switching read/write modes */
152 if (buffer->write_in_progress) {
153 retval = -ETXTBSY;
154 goto out;
155 }
3f6928c3 156 buffer->read_in_progress = true;
03607ace
PA
157
158 if (buffer->needs_read_fill) {
159 /* perform first read with buf == NULL to get extent */
b0841eef
AV
160 down_read(&frag->frag_sem);
161 if (!frag->frag_dead)
162 len = buffer->bin_attr->read(buffer->item, NULL, 0);
163 else
164 len = -ENOENT;
165 up_read(&frag->frag_sem);
03607ace
PA
166 if (len <= 0) {
167 retval = len;
168 goto out;
169 }
170
171 /* do not exceed the maximum value */
ff4dd081 172 if (buffer->cb_max_size && len > buffer->cb_max_size) {
03607ace
PA
173 retval = -EFBIG;
174 goto out;
175 }
176
177 buffer->bin_buffer = vmalloc(len);
178 if (buffer->bin_buffer == NULL) {
179 retval = -ENOMEM;
180 goto out;
181 }
182 buffer->bin_buffer_size = len;
183
184 /* perform second read to fill buffer */
b0841eef
AV
185 down_read(&frag->frag_sem);
186 if (!frag->frag_dead)
187 len = buffer->bin_attr->read(buffer->item,
188 buffer->bin_buffer, len);
189 else
190 len = -ENOENT;
191 up_read(&frag->frag_sem);
03607ace
PA
192 if (len < 0) {
193 retval = len;
194 vfree(buffer->bin_buffer);
195 buffer->bin_buffer_size = 0;
196 buffer->bin_buffer = NULL;
197 goto out;
198 }
199
200 buffer->needs_read_fill = 0;
201 }
202
203 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
204 buffer->bin_buffer_size);
205out:
206 mutex_unlock(&buffer->mutex);
207 return retval;
208}
209
7063fbf2
JB
210
211/**
212 * fill_write_buffer - copy buffer from userspace.
213 * @buffer: data buffer for file.
3d0f89bb 214 * @buf: data from user.
7063fbf2
JB
215 * @count: number of bytes in @userbuf.
216 *
217 * Allocate @buffer->page if it hasn't been already, then
218 * copy the user-supplied buffer into it.
219 */
220
221static int
222fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
223{
224 int error;
225
226 if (!buffer->page)
ff05d1c4 227 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
7063fbf2
JB
228 if (!buffer->page)
229 return -ENOMEM;
230
b23cdde4
JB
231 if (count >= SIMPLE_ATTR_SIZE)
232 count = SIMPLE_ATTR_SIZE - 1;
7063fbf2
JB
233 error = copy_from_user(buffer->page,buf,count);
234 buffer->needs_read_fill = 1;
ff05d1c4
JB
235 /* if buf is assumed to contain a string, terminate it by \0,
236 * so e.g. sscanf() can scan the string easily */
237 buffer->page[count] = 0;
7063fbf2
JB
238 return error ? -EFAULT : count;
239}
240
7063fbf2 241static int
b0841eef 242flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
7063fbf2 243{
b0841eef
AV
244 struct configfs_fragment *frag = to_frag(file);
245 int res = -ENOENT;
246
247 down_read(&frag->frag_sem);
248 if (!frag->frag_dead)
249 res = buffer->attr->store(buffer->item, buffer->page, count);
250 up_read(&frag->frag_sem);
251 return res;
7063fbf2
JB
252}
253
254
255/**
256 * configfs_write_file - write an attribute.
257 * @file: file pointer
258 * @buf: data to write
259 * @count: number of bytes
260 * @ppos: starting offset
261 *
262 * Similar to configfs_read_file(), though working in the opposite direction.
263 * We allocate and fill the data from the user in fill_write_buffer(),
264 * then push it to the config_item in flush_write_buffer().
265 * There is no easy way for us to know if userspace is only doing a partial
266 * write, so we don't support them. We expect the entire buffer to come
267 * on the first write.
ce9bebe6 268 * Hint: if you're writing a value, first read the file, modify only
7063fbf2
JB
269 * the value you're changing, then write entire buffer back.
270 */
271
272static ssize_t
273configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
274{
ff4dd081 275 struct configfs_buffer *buffer = file->private_data;
3d0f89bb 276 ssize_t len;
7063fbf2 277
6d748924 278 mutex_lock(&buffer->mutex);
3d0f89bb
JB
279 len = fill_write_buffer(buffer, buf, count);
280 if (len > 0)
b0841eef 281 len = flush_write_buffer(file, buffer, len);
3d0f89bb
JB
282 if (len > 0)
283 *ppos += len;
6d748924 284 mutex_unlock(&buffer->mutex);
3d0f89bb 285 return len;
7063fbf2
JB
286}
287
03607ace
PA
288/**
289 * configfs_write_bin_file - write a binary attribute.
290 * @file: file pointer
291 * @buf: data to write
292 * @count: number of bytes
293 * @ppos: starting offset
294 *
295 * Writing to a binary attribute file is similar to a normal read.
296 * We buffer the consecutive writes (binary attribute files do not
297 * support lseek) in a continuously growing buffer, but we don't
298 * commit until the close of the file.
299 */
300
301static ssize_t
302configfs_write_bin_file(struct file *file, const char __user *buf,
303 size_t count, loff_t *ppos)
304{
305 struct configfs_buffer *buffer = file->private_data;
03607ace
PA
306 void *tbuf = NULL;
307 ssize_t len;
308
309 mutex_lock(&buffer->mutex);
310
311 /* we don't support switching read/write modes */
312 if (buffer->read_in_progress) {
313 len = -ETXTBSY;
314 goto out;
315 }
3f6928c3 316 buffer->write_in_progress = true;
03607ace
PA
317
318 /* buffer grows? */
319 if (*ppos + count > buffer->bin_buffer_size) {
320
ff4dd081
AV
321 if (buffer->cb_max_size &&
322 *ppos + count > buffer->cb_max_size) {
03607ace 323 len = -EFBIG;
42857cf5 324 goto out;
03607ace
PA
325 }
326
327 tbuf = vmalloc(*ppos + count);
328 if (tbuf == NULL) {
329 len = -ENOMEM;
330 goto out;
331 }
332
333 /* copy old contents */
334 if (buffer->bin_buffer) {
335 memcpy(tbuf, buffer->bin_buffer,
336 buffer->bin_buffer_size);
337 vfree(buffer->bin_buffer);
338 }
339
340 /* clear the new area */
341 memset(tbuf + buffer->bin_buffer_size, 0,
342 *ppos + count - buffer->bin_buffer_size);
343 buffer->bin_buffer = tbuf;
344 buffer->bin_buffer_size = *ppos + count;
345 }
346
347 len = simple_write_to_buffer(buffer->bin_buffer,
348 buffer->bin_buffer_size, ppos, buf, count);
03607ace
PA
349out:
350 mutex_unlock(&buffer->mutex);
351 return len;
352}
353
ff4dd081 354static int __configfs_open_file(struct inode *inode, struct file *file, int type)
7063fbf2 355{
ff4dd081 356 struct dentry *dentry = file->f_path.dentry;
b0841eef 357 struct configfs_fragment *frag = to_frag(file);
ff4dd081
AV
358 struct configfs_attribute *attr;
359 struct configfs_buffer *buffer;
360 int error;
7063fbf2 361
ff4dd081
AV
362 error = -ENOMEM;
363 buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
364 if (!buffer)
365 goto out;
7063fbf2 366
b0841eef
AV
367 error = -ENOENT;
368 down_read(&frag->frag_sem);
369 if (unlikely(frag->frag_dead))
370 goto out_free_buffer;
371
ff4dd081 372 error = -EINVAL;
b0841eef 373 buffer->item = to_item(dentry->d_parent);
ff4dd081
AV
374 if (!buffer->item)
375 goto out_free_buffer;
376
377 attr = to_attr(dentry);
378 if (!attr)
14fbbc82 379 goto out_free_buffer;
ff4dd081
AV
380
381 if (type & CONFIGFS_ITEM_BIN_ATTR) {
382 buffer->bin_attr = to_bin_attr(dentry);
383 buffer->cb_max_size = buffer->bin_attr->cb_max_size;
384 } else {
385 buffer->attr = attr;
386 }
03607ace 387
ff4dd081 388 buffer->owner = attr->ca_owner;
7063fbf2 389 /* Grab the module reference for this attribute if we have one */
ff4dd081
AV
390 error = -ENODEV;
391 if (!try_module_get(buffer->owner))
14fbbc82 392 goto out_free_buffer;
7063fbf2 393
ff4dd081
AV
394 error = -EACCES;
395 if (!buffer->item->ci_type)
396 goto out_put_module;
397
398 buffer->ops = buffer->item->ci_type->ct_item_ops;
7063fbf2
JB
399
400 /* File needs write support.
401 * The inode's perms must say it's ok,
402 * and we must have a store method.
403 */
404 if (file->f_mode & FMODE_WRITE) {
03607ace 405 if (!(inode->i_mode & S_IWUGO))
ff4dd081 406 goto out_put_module;
03607ace 407 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
ff4dd081
AV
408 goto out_put_module;
409 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
410 goto out_put_module;
7063fbf2
JB
411 }
412
413 /* File needs read support.
414 * The inode's perms must say it's ok, and we there
415 * must be a show method for it.
416 */
417 if (file->f_mode & FMODE_READ) {
03607ace 418 if (!(inode->i_mode & S_IRUGO))
ff4dd081 419 goto out_put_module;
03607ace 420 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
ff4dd081
AV
421 goto out_put_module;
422 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
423 goto out_put_module;
7063fbf2
JB
424 }
425
6d748924 426 mutex_init(&buffer->mutex);
559c9ac3 427 buffer->needs_read_fill = 1;
3f6928c3
TM
428 buffer->read_in_progress = false;
429 buffer->write_in_progress = false;
559c9ac3 430 file->private_data = buffer;
b0841eef 431 up_read(&frag->frag_sem);
ff4dd081 432 return 0;
7063fbf2 433
ff4dd081
AV
434out_put_module:
435 module_put(buffer->owner);
ff4dd081 436out_free_buffer:
b0841eef 437 up_read(&frag->frag_sem);
ff4dd081
AV
438 kfree(buffer);
439out:
7063fbf2
JB
440 return error;
441}
442
03607ace 443static int configfs_release(struct inode *inode, struct file *filp)
7063fbf2 444{
ff4dd081
AV
445 struct configfs_buffer *buffer = filp->private_data;
446
ff4dd081
AV
447 module_put(buffer->owner);
448 if (buffer->page)
449 free_page((unsigned long)buffer->page);
450 mutex_destroy(&buffer->mutex);
451 kfree(buffer);
7063fbf2
JB
452 return 0;
453}
454
03607ace
PA
455static int configfs_open_file(struct inode *inode, struct file *filp)
456{
ff4dd081 457 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
03607ace
PA
458}
459
460static int configfs_open_bin_file(struct inode *inode, struct file *filp)
461{
ff4dd081 462 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
03607ace
PA
463}
464
ff4dd081 465static int configfs_release_bin_file(struct inode *inode, struct file *file)
03607ace 466{
ff4dd081 467 struct configfs_buffer *buffer = file->private_data;
03607ace 468
3f6928c3 469 buffer->read_in_progress = false;
03607ace
PA
470
471 if (buffer->write_in_progress) {
b0841eef 472 struct configfs_fragment *frag = to_frag(file);
3f6928c3 473 buffer->write_in_progress = false;
03607ace 474
b0841eef
AV
475 down_read(&frag->frag_sem);
476 if (!frag->frag_dead) {
477 /* result of ->release() is ignored */
478 buffer->bin_attr->write(buffer->item,
479 buffer->bin_buffer,
480 buffer->bin_buffer_size);
481 }
482 up_read(&frag->frag_sem);
03607ace
PA
483 /* vfree on NULL is safe */
484 vfree(buffer->bin_buffer);
485 buffer->bin_buffer = NULL;
486 buffer->bin_buffer_size = 0;
487 buffer->needs_read_fill = 1;
488 }
489
ff4dd081
AV
490 configfs_release(inode, file);
491 return 0;
03607ace
PA
492}
493
494
4b6f5d20 495const struct file_operations configfs_file_operations = {
7063fbf2
JB
496 .read = configfs_read_file,
497 .write = configfs_write_file,
498 .llseek = generic_file_llseek,
499 .open = configfs_open_file,
500 .release = configfs_release,
501};
502
03607ace
PA
503const struct file_operations configfs_bin_file_operations = {
504 .read = configfs_read_bin_file,
505 .write = configfs_write_bin_file,
506 .llseek = NULL, /* bin file is not seekable */
507 .open = configfs_open_bin_file,
508 .release = configfs_release_bin_file,
509};
510
7063fbf2
JB
511/**
512 * configfs_create_file - create an attribute file for an item.
513 * @item: item we're creating for.
514 * @attr: atrribute descriptor.
515 */
516
517int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
518{
28444a2b
AV
519 struct dentry *dir = item->ci_dentry;
520 struct configfs_dirent *parent_sd = dir->d_fsdata;
521 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
522 int error = 0;
7063fbf2 523
5955102c 524 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
28444a2b 525 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
47320fbe 526 CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
5955102c 527 inode_unlock(d_inode(dir));
28444a2b
AV
528
529 return error;
7063fbf2
JB
530}
531
03607ace
PA
532/**
533 * configfs_create_bin_file - create a binary attribute file for an item.
534 * @item: item we're creating for.
535 * @attr: atrribute descriptor.
536 */
537
538int configfs_create_bin_file(struct config_item *item,
539 const struct configfs_bin_attribute *bin_attr)
540{
541 struct dentry *dir = item->ci_dentry;
542 struct configfs_dirent *parent_sd = dir->d_fsdata;
543 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
544 int error = 0;
545
5955102c 546 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
03607ace 547 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
47320fbe 548 CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
5955102c 549 inode_unlock(dir->d_inode);
03607ace
PA
550
551 return error;
552}