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