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