xref: /linux/fs/configfs/file.c (revision 987b741c52c7c6c68d46fbaeb95b8d1087f10b7f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * file.c - operations for regular (text) files.
4  *
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>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/vmalloc.h>
16 #include <linux/uaccess.h>
17 
18 #include <linux/configfs.h>
19 #include "configfs_internal.h"
20 
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
28 
29 struct configfs_buffer {
30 	size_t			count;
31 	loff_t			pos;
32 	char			* page;
33 	struct configfs_item_operations	* ops;
34 	struct mutex		mutex;
35 	int			needs_read_fill;
36 	bool			read_in_progress;
37 	bool			write_in_progress;
38 	char			*bin_buffer;
39 	int			bin_buffer_size;
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 	};
47 };
48 
49 static 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 }
55 
56 static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
57 {
58 	struct configfs_fragment *frag = to_frag(file);
59 	ssize_t count = -ENOENT;
60 
61 	if (!buffer->page)
62 		buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
63 	if (!buffer->page)
64 		return -ENOMEM;
65 
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 
71 	if (count < 0)
72 		return count;
73 	if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
74 		return -EIO;
75 	buffer->needs_read_fill = 0;
76 	buffer->count = count;
77 	return 0;
78 }
79 
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 
99 static ssize_t
100 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
101 {
102 	struct configfs_buffer *buffer = file->private_data;
103 	ssize_t retval = 0;
104 
105 	mutex_lock(&buffer->mutex);
106 	if (buffer->needs_read_fill) {
107 		retval = fill_read_buffer(file, buffer);
108 		if (retval)
109 			goto out;
110 	}
111 	pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
112 		 __func__, count, *ppos, buffer->page);
113 	retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
114 					 buffer->count);
115 out:
116 	mutex_unlock(&buffer->mutex);
117 	return retval;
118 }
119 
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 
140 static ssize_t
141 configfs_read_bin_file(struct file *file, char __user *buf,
142 		       size_t count, loff_t *ppos)
143 {
144 	struct configfs_fragment *frag = to_frag(file);
145 	struct configfs_buffer *buffer = file->private_data;
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 	}
156 	buffer->read_in_progress = true;
157 
158 	if (buffer->needs_read_fill) {
159 		/* perform first read with buf == NULL to get extent */
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);
166 		if (len <= 0) {
167 			retval = len;
168 			goto out;
169 		}
170 
171 		/* do not exceed the maximum value */
172 		if (buffer->cb_max_size && len > buffer->cb_max_size) {
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 */
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);
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);
205 out:
206 	mutex_unlock(&buffer->mutex);
207 	return retval;
208 }
209 
210 
211 /**
212  *	fill_write_buffer - copy buffer from userspace.
213  *	@buffer:	data buffer for file.
214  *	@buf:		data from user.
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 
221 static int
222 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
223 {
224 	int error;
225 
226 	if (!buffer->page)
227 		buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
228 	if (!buffer->page)
229 		return -ENOMEM;
230 
231 	if (count >= SIMPLE_ATTR_SIZE)
232 		count = SIMPLE_ATTR_SIZE - 1;
233 	error = copy_from_user(buffer->page,buf,count);
234 	buffer->needs_read_fill = 1;
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;
238 	return error ? -EFAULT : count;
239 }
240 
241 static int
242 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
243 {
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;
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.
268  *	Hint: if you're writing a value, first read the file, modify only
269  *	the value you're changing, then write entire buffer back.
270  */
271 
272 static ssize_t
273 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
274 {
275 	struct configfs_buffer *buffer = file->private_data;
276 	ssize_t len;
277 
278 	mutex_lock(&buffer->mutex);
279 	len = fill_write_buffer(buffer, buf, count);
280 	if (len > 0)
281 		len = flush_write_buffer(file, buffer, len);
282 	if (len > 0)
283 		*ppos += len;
284 	mutex_unlock(&buffer->mutex);
285 	return len;
286 }
287 
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 
301 static ssize_t
302 configfs_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;
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 	}
316 	buffer->write_in_progress = true;
317 
318 	/* buffer grows? */
319 	if (*ppos + count > buffer->bin_buffer_size) {
320 
321 		if (buffer->cb_max_size &&
322 			*ppos + count > buffer->cb_max_size) {
323 			len = -EFBIG;
324 			goto out;
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);
349 out:
350 	mutex_unlock(&buffer->mutex);
351 	return len;
352 }
353 
354 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
355 {
356 	struct dentry *dentry = file->f_path.dentry;
357 	struct configfs_fragment *frag = to_frag(file);
358 	struct configfs_attribute *attr;
359 	struct configfs_buffer *buffer;
360 	int error;
361 
362 	error = -ENOMEM;
363 	buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
364 	if (!buffer)
365 		goto out;
366 
367 	error = -ENOENT;
368 	down_read(&frag->frag_sem);
369 	if (unlikely(frag->frag_dead))
370 		goto out_free_buffer;
371 
372 	error = -EINVAL;
373 	buffer->item = to_item(dentry->d_parent);
374 	if (!buffer->item)
375 		goto out_free_buffer;
376 
377 	attr = to_attr(dentry);
378 	if (!attr)
379 		goto out_free_buffer;
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 	}
387 
388 	buffer->owner = attr->ca_owner;
389 	/* Grab the module reference for this attribute if we have one */
390 	error = -ENODEV;
391 	if (!try_module_get(buffer->owner))
392 		goto out_free_buffer;
393 
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;
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) {
405 		if (!(inode->i_mode & S_IWUGO))
406 			goto out_put_module;
407 		if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
408 			goto out_put_module;
409 		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
410 			goto out_put_module;
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) {
418 		if (!(inode->i_mode & S_IRUGO))
419 			goto out_put_module;
420 		if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
421 			goto out_put_module;
422 		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
423 			goto out_put_module;
424 	}
425 
426 	mutex_init(&buffer->mutex);
427 	buffer->needs_read_fill = 1;
428 	buffer->read_in_progress = false;
429 	buffer->write_in_progress = false;
430 	file->private_data = buffer;
431 	up_read(&frag->frag_sem);
432 	return 0;
433 
434 out_put_module:
435 	module_put(buffer->owner);
436 out_free_buffer:
437 	up_read(&frag->frag_sem);
438 	kfree(buffer);
439 out:
440 	return error;
441 }
442 
443 static int configfs_release(struct inode *inode, struct file *filp)
444 {
445 	struct configfs_buffer *buffer = filp->private_data;
446 
447 	module_put(buffer->owner);
448 	if (buffer->page)
449 		free_page((unsigned long)buffer->page);
450 	mutex_destroy(&buffer->mutex);
451 	kfree(buffer);
452 	return 0;
453 }
454 
455 static int configfs_open_file(struct inode *inode, struct file *filp)
456 {
457 	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
458 }
459 
460 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
461 {
462 	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
463 }
464 
465 static int configfs_release_bin_file(struct inode *inode, struct file *file)
466 {
467 	struct configfs_buffer *buffer = file->private_data;
468 
469 	buffer->read_in_progress = false;
470 
471 	if (buffer->write_in_progress) {
472 		struct configfs_fragment *frag = to_frag(file);
473 		buffer->write_in_progress = false;
474 
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);
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 
490 	configfs_release(inode, file);
491 	return 0;
492 }
493 
494 
495 const struct file_operations configfs_file_operations = {
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 
503 const 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 
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 
517 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
518 {
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;
523 
524 	inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
525 	error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
526 				     CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
527 	inode_unlock(d_inode(dir));
528 
529 	return error;
530 }
531 
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 
538 int 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 
546 	inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
547 	error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
548 				     CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
549 	inode_unlock(dir->d_inode);
550 
551 	return error;
552 }
553