xref: /openbmc/linux/fs/configfs/file.c (revision e481ff3f)
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 #include <linux/uio.h>
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 static ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
81 {
82 	struct file *file = iocb->ki_filp;
83 	struct configfs_buffer *buffer = file->private_data;
84 	ssize_t retval = 0;
85 
86 	mutex_lock(&buffer->mutex);
87 	if (buffer->needs_read_fill) {
88 		retval = fill_read_buffer(file, buffer);
89 		if (retval)
90 			goto out;
91 	}
92 	pr_debug("%s: count = %zd, pos = %lld, buf = %s\n",
93 		 __func__, iov_iter_count(to), iocb->ki_pos, buffer->page);
94 	retval = copy_to_iter(buffer->page, buffer->count, to);
95 	iocb->ki_pos += retval;
96 	if (retval == 0)
97 		retval = -EFAULT;
98 out:
99 	mutex_unlock(&buffer->mutex);
100 	return retval;
101 }
102 
103 static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to)
104 {
105 	struct file *file = iocb->ki_filp;
106 	struct configfs_fragment *frag = to_frag(file);
107 	struct configfs_buffer *buffer = file->private_data;
108 	ssize_t retval = 0;
109 	ssize_t len;
110 
111 	mutex_lock(&buffer->mutex);
112 
113 	/* we don't support switching read/write modes */
114 	if (buffer->write_in_progress) {
115 		retval = -ETXTBSY;
116 		goto out;
117 	}
118 	buffer->read_in_progress = true;
119 
120 	if (buffer->needs_read_fill) {
121 		/* perform first read with buf == NULL to get extent */
122 		down_read(&frag->frag_sem);
123 		if (!frag->frag_dead)
124 			len = buffer->bin_attr->read(buffer->item, NULL, 0);
125 		else
126 			len = -ENOENT;
127 		up_read(&frag->frag_sem);
128 		if (len <= 0) {
129 			retval = len;
130 			goto out;
131 		}
132 
133 		/* do not exceed the maximum value */
134 		if (buffer->cb_max_size && len > buffer->cb_max_size) {
135 			retval = -EFBIG;
136 			goto out;
137 		}
138 
139 		buffer->bin_buffer = vmalloc(len);
140 		if (buffer->bin_buffer == NULL) {
141 			retval = -ENOMEM;
142 			goto out;
143 		}
144 		buffer->bin_buffer_size = len;
145 
146 		/* perform second read to fill buffer */
147 		down_read(&frag->frag_sem);
148 		if (!frag->frag_dead)
149 			len = buffer->bin_attr->read(buffer->item,
150 						     buffer->bin_buffer, len);
151 		else
152 			len = -ENOENT;
153 		up_read(&frag->frag_sem);
154 		if (len < 0) {
155 			retval = len;
156 			vfree(buffer->bin_buffer);
157 			buffer->bin_buffer_size = 0;
158 			buffer->bin_buffer = NULL;
159 			goto out;
160 		}
161 
162 		buffer->needs_read_fill = 0;
163 	}
164 
165 	retval = copy_to_iter(buffer->bin_buffer, buffer->bin_buffer_size, to);
166 	iocb->ki_pos += retval;
167 	if (retval == 0)
168 		retval = -EFAULT;
169 out:
170 	mutex_unlock(&buffer->mutex);
171 	return retval;
172 }
173 
174 static int fill_write_buffer(struct configfs_buffer *buffer,
175 			     struct iov_iter *from)
176 {
177 	int copied;
178 
179 	if (!buffer->page)
180 		buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
181 	if (!buffer->page)
182 		return -ENOMEM;
183 
184 	copied = copy_from_iter(buffer->page, SIMPLE_ATTR_SIZE - 1, from);
185 	buffer->needs_read_fill = 1;
186 	/* if buf is assumed to contain a string, terminate it by \0,
187 	 * so e.g. sscanf() can scan the string easily */
188 	buffer->page[copied] = 0;
189 	return copied ? : -EFAULT;
190 }
191 
192 static int
193 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
194 {
195 	struct configfs_fragment *frag = to_frag(file);
196 	int res = -ENOENT;
197 
198 	down_read(&frag->frag_sem);
199 	if (!frag->frag_dead)
200 		res = buffer->attr->store(buffer->item, buffer->page, count);
201 	up_read(&frag->frag_sem);
202 	return res;
203 }
204 
205 
206 /*
207  * There is no easy way for us to know if userspace is only doing a partial
208  * write, so we don't support them. We expect the entire buffer to come on the
209  * first write.
210  * Hint: if you're writing a value, first read the file, modify only the value
211  * you're changing, then write entire buffer back.
212  */
213 static ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
214 {
215 	struct file *file = iocb->ki_filp;
216 	struct configfs_buffer *buffer = file->private_data;
217 	ssize_t len;
218 
219 	mutex_lock(&buffer->mutex);
220 	len = fill_write_buffer(buffer, from);
221 	if (len > 0)
222 		len = flush_write_buffer(file, buffer, len);
223 	if (len > 0)
224 		iocb->ki_pos += len;
225 	mutex_unlock(&buffer->mutex);
226 	return len;
227 }
228 
229 static ssize_t configfs_bin_write_iter(struct kiocb *iocb,
230 				       struct iov_iter *from)
231 {
232 	struct file *file = iocb->ki_filp;
233 	struct configfs_buffer *buffer = file->private_data;
234 	void *tbuf = NULL;
235 	size_t end_offset;
236 	ssize_t len;
237 
238 	mutex_lock(&buffer->mutex);
239 
240 	/* we don't support switching read/write modes */
241 	if (buffer->read_in_progress) {
242 		len = -ETXTBSY;
243 		goto out;
244 	}
245 	buffer->write_in_progress = true;
246 
247 	/* buffer grows? */
248 	end_offset = iocb->ki_pos + iov_iter_count(from);
249 	if (end_offset > buffer->bin_buffer_size) {
250 		if (buffer->cb_max_size && end_offset > buffer->cb_max_size) {
251 			len = -EFBIG;
252 			goto out;
253 		}
254 
255 		tbuf = vmalloc(end_offset);
256 		if (tbuf == NULL) {
257 			len = -ENOMEM;
258 			goto out;
259 		}
260 
261 		/* copy old contents */
262 		if (buffer->bin_buffer) {
263 			memcpy(tbuf, buffer->bin_buffer,
264 				buffer->bin_buffer_size);
265 			vfree(buffer->bin_buffer);
266 		}
267 
268 		/* clear the new area */
269 		memset(tbuf + buffer->bin_buffer_size, 0,
270 			end_offset - buffer->bin_buffer_size);
271 		buffer->bin_buffer = tbuf;
272 		buffer->bin_buffer_size = end_offset;
273 	}
274 
275 	len = copy_from_iter(buffer->bin_buffer, buffer->bin_buffer_size, from);
276 out:
277 	mutex_unlock(&buffer->mutex);
278 	return len ? : -EFAULT;
279 }
280 
281 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
282 {
283 	struct dentry *dentry = file->f_path.dentry;
284 	struct configfs_fragment *frag = to_frag(file);
285 	struct configfs_attribute *attr;
286 	struct configfs_buffer *buffer;
287 	int error;
288 
289 	error = -ENOMEM;
290 	buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
291 	if (!buffer)
292 		goto out;
293 
294 	error = -ENOENT;
295 	down_read(&frag->frag_sem);
296 	if (unlikely(frag->frag_dead))
297 		goto out_free_buffer;
298 
299 	error = -EINVAL;
300 	buffer->item = to_item(dentry->d_parent);
301 	if (!buffer->item)
302 		goto out_free_buffer;
303 
304 	attr = to_attr(dentry);
305 	if (!attr)
306 		goto out_free_buffer;
307 
308 	if (type & CONFIGFS_ITEM_BIN_ATTR) {
309 		buffer->bin_attr = to_bin_attr(dentry);
310 		buffer->cb_max_size = buffer->bin_attr->cb_max_size;
311 	} else {
312 		buffer->attr = attr;
313 	}
314 
315 	buffer->owner = attr->ca_owner;
316 	/* Grab the module reference for this attribute if we have one */
317 	error = -ENODEV;
318 	if (!try_module_get(buffer->owner))
319 		goto out_free_buffer;
320 
321 	error = -EACCES;
322 	if (!buffer->item->ci_type)
323 		goto out_put_module;
324 
325 	buffer->ops = buffer->item->ci_type->ct_item_ops;
326 
327 	/* File needs write support.
328 	 * The inode's perms must say it's ok,
329 	 * and we must have a store method.
330 	 */
331 	if (file->f_mode & FMODE_WRITE) {
332 		if (!(inode->i_mode & S_IWUGO))
333 			goto out_put_module;
334 		if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
335 			goto out_put_module;
336 		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
337 			goto out_put_module;
338 	}
339 
340 	/* File needs read support.
341 	 * The inode's perms must say it's ok, and we there
342 	 * must be a show method for it.
343 	 */
344 	if (file->f_mode & FMODE_READ) {
345 		if (!(inode->i_mode & S_IRUGO))
346 			goto out_put_module;
347 		if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
348 			goto out_put_module;
349 		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
350 			goto out_put_module;
351 	}
352 
353 	mutex_init(&buffer->mutex);
354 	buffer->needs_read_fill = 1;
355 	buffer->read_in_progress = false;
356 	buffer->write_in_progress = false;
357 	file->private_data = buffer;
358 	up_read(&frag->frag_sem);
359 	return 0;
360 
361 out_put_module:
362 	module_put(buffer->owner);
363 out_free_buffer:
364 	up_read(&frag->frag_sem);
365 	kfree(buffer);
366 out:
367 	return error;
368 }
369 
370 static int configfs_release(struct inode *inode, struct file *filp)
371 {
372 	struct configfs_buffer *buffer = filp->private_data;
373 
374 	module_put(buffer->owner);
375 	if (buffer->page)
376 		free_page((unsigned long)buffer->page);
377 	mutex_destroy(&buffer->mutex);
378 	kfree(buffer);
379 	return 0;
380 }
381 
382 static int configfs_open_file(struct inode *inode, struct file *filp)
383 {
384 	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
385 }
386 
387 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
388 {
389 	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
390 }
391 
392 static int configfs_release_bin_file(struct inode *inode, struct file *file)
393 {
394 	struct configfs_buffer *buffer = file->private_data;
395 
396 	if (buffer->write_in_progress) {
397 		struct configfs_fragment *frag = to_frag(file);
398 
399 		down_read(&frag->frag_sem);
400 		if (!frag->frag_dead) {
401 			/* result of ->release() is ignored */
402 			buffer->bin_attr->write(buffer->item,
403 					buffer->bin_buffer,
404 					buffer->bin_buffer_size);
405 		}
406 		up_read(&frag->frag_sem);
407 	}
408 
409 	vfree(buffer->bin_buffer);
410 
411 	configfs_release(inode, file);
412 	return 0;
413 }
414 
415 
416 const struct file_operations configfs_file_operations = {
417 	.read_iter	= configfs_read_iter,
418 	.write_iter	= configfs_write_iter,
419 	.llseek		= generic_file_llseek,
420 	.open		= configfs_open_file,
421 	.release	= configfs_release,
422 };
423 
424 const struct file_operations configfs_bin_file_operations = {
425 	.read_iter	= configfs_bin_read_iter,
426 	.write_iter	= configfs_bin_write_iter,
427 	.llseek		= NULL,		/* bin file is not seekable */
428 	.open		= configfs_open_bin_file,
429 	.release	= configfs_release_bin_file,
430 };
431 
432 /**
433  *	configfs_create_file - create an attribute file for an item.
434  *	@item:	item we're creating for.
435  *	@attr:	atrribute descriptor.
436  */
437 
438 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
439 {
440 	struct dentry *dir = item->ci_dentry;
441 	struct configfs_dirent *parent_sd = dir->d_fsdata;
442 	umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
443 	int error = 0;
444 
445 	inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
446 	error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
447 				     CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
448 	inode_unlock(d_inode(dir));
449 
450 	return error;
451 }
452 
453 /**
454  *	configfs_create_bin_file - create a binary attribute file for an item.
455  *	@item:	item we're creating for.
456  *	@bin_attr: atrribute descriptor.
457  */
458 
459 int configfs_create_bin_file(struct config_item *item,
460 		const struct configfs_bin_attribute *bin_attr)
461 {
462 	struct dentry *dir = item->ci_dentry;
463 	struct configfs_dirent *parent_sd = dir->d_fsdata;
464 	umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
465 	int error = 0;
466 
467 	inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
468 	error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
469 				     CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
470 	inode_unlock(dir->d_inode);
471 
472 	return error;
473 }
474