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