162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * file.c - operations for regular (text) files.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Based on sysfs:
662306a36Sopenharmony_ci * 	sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * configfs Copyright (C) 2005 Oracle.  All rights reserved.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/fs.h>
1262306a36Sopenharmony_ci#include <linux/module.h>
1362306a36Sopenharmony_ci#include <linux/slab.h>
1462306a36Sopenharmony_ci#include <linux/mutex.h>
1562306a36Sopenharmony_ci#include <linux/vmalloc.h>
1662306a36Sopenharmony_ci#include <linux/uaccess.h>
1762306a36Sopenharmony_ci#include <linux/uio.h>
1862306a36Sopenharmony_ci#include <linux/configfs.h>
1962306a36Sopenharmony_ci#include "configfs_internal.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * A simple attribute can only be 4096 characters.  Why 4k?  Because the
2362306a36Sopenharmony_ci * original code limited it to PAGE_SIZE.  That's a bad idea, though,
2462306a36Sopenharmony_ci * because an attribute of 16k on ia64 won't work on x86.  So we limit to
2562306a36Sopenharmony_ci * 4k, our minimum common page size.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci#define SIMPLE_ATTR_SIZE 4096
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistruct configfs_buffer {
3062306a36Sopenharmony_ci	size_t			count;
3162306a36Sopenharmony_ci	loff_t			pos;
3262306a36Sopenharmony_ci	char			* page;
3362306a36Sopenharmony_ci	struct configfs_item_operations	* ops;
3462306a36Sopenharmony_ci	struct mutex		mutex;
3562306a36Sopenharmony_ci	int			needs_read_fill;
3662306a36Sopenharmony_ci	bool			read_in_progress;
3762306a36Sopenharmony_ci	bool			write_in_progress;
3862306a36Sopenharmony_ci	char			*bin_buffer;
3962306a36Sopenharmony_ci	int			bin_buffer_size;
4062306a36Sopenharmony_ci	int			cb_max_size;
4162306a36Sopenharmony_ci	struct config_item	*item;
4262306a36Sopenharmony_ci	struct module		*owner;
4362306a36Sopenharmony_ci	union {
4462306a36Sopenharmony_ci		struct configfs_attribute	*attr;
4562306a36Sopenharmony_ci		struct configfs_bin_attribute	*bin_attr;
4662306a36Sopenharmony_ci	};
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic inline struct configfs_fragment *to_frag(struct file *file)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	return sd->s_frag;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	struct configfs_fragment *frag = to_frag(file);
5962306a36Sopenharmony_ci	ssize_t count = -ENOENT;
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci	if (!buffer->page)
6262306a36Sopenharmony_ci		buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
6362306a36Sopenharmony_ci	if (!buffer->page)
6462306a36Sopenharmony_ci		return -ENOMEM;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	down_read(&frag->frag_sem);
6762306a36Sopenharmony_ci	if (!frag->frag_dead)
6862306a36Sopenharmony_ci		count = buffer->attr->show(buffer->item, buffer->page);
6962306a36Sopenharmony_ci	up_read(&frag->frag_sem);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	if (count < 0)
7262306a36Sopenharmony_ci		return count;
7362306a36Sopenharmony_ci	if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
7462306a36Sopenharmony_ci		return -EIO;
7562306a36Sopenharmony_ci	buffer->needs_read_fill = 0;
7662306a36Sopenharmony_ci	buffer->count = count;
7762306a36Sopenharmony_ci	return 0;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct file *file = iocb->ki_filp;
8362306a36Sopenharmony_ci	struct configfs_buffer *buffer = file->private_data;
8462306a36Sopenharmony_ci	ssize_t retval = 0;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	mutex_lock(&buffer->mutex);
8762306a36Sopenharmony_ci	if (buffer->needs_read_fill) {
8862306a36Sopenharmony_ci		retval = fill_read_buffer(file, buffer);
8962306a36Sopenharmony_ci		if (retval)
9062306a36Sopenharmony_ci			goto out;
9162306a36Sopenharmony_ci	}
9262306a36Sopenharmony_ci	pr_debug("%s: count = %zd, pos = %lld, buf = %s\n",
9362306a36Sopenharmony_ci		 __func__, iov_iter_count(to), iocb->ki_pos, buffer->page);
9462306a36Sopenharmony_ci	if (iocb->ki_pos >= buffer->count)
9562306a36Sopenharmony_ci		goto out;
9662306a36Sopenharmony_ci	retval = copy_to_iter(buffer->page + iocb->ki_pos,
9762306a36Sopenharmony_ci			      buffer->count - iocb->ki_pos, to);
9862306a36Sopenharmony_ci	iocb->ki_pos += retval;
9962306a36Sopenharmony_ci	if (retval == 0)
10062306a36Sopenharmony_ci		retval = -EFAULT;
10162306a36Sopenharmony_ciout:
10262306a36Sopenharmony_ci	mutex_unlock(&buffer->mutex);
10362306a36Sopenharmony_ci	return retval;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	struct file *file = iocb->ki_filp;
10962306a36Sopenharmony_ci	struct configfs_fragment *frag = to_frag(file);
11062306a36Sopenharmony_ci	struct configfs_buffer *buffer = file->private_data;
11162306a36Sopenharmony_ci	ssize_t retval = 0;
11262306a36Sopenharmony_ci	ssize_t len;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	mutex_lock(&buffer->mutex);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/* we don't support switching read/write modes */
11762306a36Sopenharmony_ci	if (buffer->write_in_progress) {
11862306a36Sopenharmony_ci		retval = -ETXTBSY;
11962306a36Sopenharmony_ci		goto out;
12062306a36Sopenharmony_ci	}
12162306a36Sopenharmony_ci	buffer->read_in_progress = true;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	if (buffer->needs_read_fill) {
12462306a36Sopenharmony_ci		/* perform first read with buf == NULL to get extent */
12562306a36Sopenharmony_ci		down_read(&frag->frag_sem);
12662306a36Sopenharmony_ci		if (!frag->frag_dead)
12762306a36Sopenharmony_ci			len = buffer->bin_attr->read(buffer->item, NULL, 0);
12862306a36Sopenharmony_ci		else
12962306a36Sopenharmony_ci			len = -ENOENT;
13062306a36Sopenharmony_ci		up_read(&frag->frag_sem);
13162306a36Sopenharmony_ci		if (len <= 0) {
13262306a36Sopenharmony_ci			retval = len;
13362306a36Sopenharmony_ci			goto out;
13462306a36Sopenharmony_ci		}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci		/* do not exceed the maximum value */
13762306a36Sopenharmony_ci		if (buffer->cb_max_size && len > buffer->cb_max_size) {
13862306a36Sopenharmony_ci			retval = -EFBIG;
13962306a36Sopenharmony_ci			goto out;
14062306a36Sopenharmony_ci		}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci		buffer->bin_buffer = vmalloc(len);
14362306a36Sopenharmony_ci		if (buffer->bin_buffer == NULL) {
14462306a36Sopenharmony_ci			retval = -ENOMEM;
14562306a36Sopenharmony_ci			goto out;
14662306a36Sopenharmony_ci		}
14762306a36Sopenharmony_ci		buffer->bin_buffer_size = len;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci		/* perform second read to fill buffer */
15062306a36Sopenharmony_ci		down_read(&frag->frag_sem);
15162306a36Sopenharmony_ci		if (!frag->frag_dead)
15262306a36Sopenharmony_ci			len = buffer->bin_attr->read(buffer->item,
15362306a36Sopenharmony_ci						     buffer->bin_buffer, len);
15462306a36Sopenharmony_ci		else
15562306a36Sopenharmony_ci			len = -ENOENT;
15662306a36Sopenharmony_ci		up_read(&frag->frag_sem);
15762306a36Sopenharmony_ci		if (len < 0) {
15862306a36Sopenharmony_ci			retval = len;
15962306a36Sopenharmony_ci			vfree(buffer->bin_buffer);
16062306a36Sopenharmony_ci			buffer->bin_buffer_size = 0;
16162306a36Sopenharmony_ci			buffer->bin_buffer = NULL;
16262306a36Sopenharmony_ci			goto out;
16362306a36Sopenharmony_ci		}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci		buffer->needs_read_fill = 0;
16662306a36Sopenharmony_ci	}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	if (iocb->ki_pos >= buffer->bin_buffer_size)
16962306a36Sopenharmony_ci		goto out;
17062306a36Sopenharmony_ci	retval = copy_to_iter(buffer->bin_buffer + iocb->ki_pos,
17162306a36Sopenharmony_ci			      buffer->bin_buffer_size - iocb->ki_pos, to);
17262306a36Sopenharmony_ci	iocb->ki_pos += retval;
17362306a36Sopenharmony_ci	if (retval == 0)
17462306a36Sopenharmony_ci		retval = -EFAULT;
17562306a36Sopenharmony_ciout:
17662306a36Sopenharmony_ci	mutex_unlock(&buffer->mutex);
17762306a36Sopenharmony_ci	return retval;
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/* Fill @buffer with data coming from @from. */
18162306a36Sopenharmony_cistatic int fill_write_buffer(struct configfs_buffer *buffer,
18262306a36Sopenharmony_ci			     struct iov_iter *from)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	int copied;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	if (!buffer->page)
18762306a36Sopenharmony_ci		buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
18862306a36Sopenharmony_ci	if (!buffer->page)
18962306a36Sopenharmony_ci		return -ENOMEM;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	copied = copy_from_iter(buffer->page, SIMPLE_ATTR_SIZE - 1, from);
19262306a36Sopenharmony_ci	buffer->needs_read_fill = 1;
19362306a36Sopenharmony_ci	/* if buf is assumed to contain a string, terminate it by \0,
19462306a36Sopenharmony_ci	 * so e.g. sscanf() can scan the string easily */
19562306a36Sopenharmony_ci	buffer->page[copied] = 0;
19662306a36Sopenharmony_ci	return copied ? : -EFAULT;
19762306a36Sopenharmony_ci}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic int
20062306a36Sopenharmony_ciflush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	struct configfs_fragment *frag = to_frag(file);
20362306a36Sopenharmony_ci	int res = -ENOENT;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	down_read(&frag->frag_sem);
20662306a36Sopenharmony_ci	if (!frag->frag_dead)
20762306a36Sopenharmony_ci		res = buffer->attr->store(buffer->item, buffer->page, count);
20862306a36Sopenharmony_ci	up_read(&frag->frag_sem);
20962306a36Sopenharmony_ci	return res;
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/*
21462306a36Sopenharmony_ci * There is no easy way for us to know if userspace is only doing a partial
21562306a36Sopenharmony_ci * write, so we don't support them. We expect the entire buffer to come on the
21662306a36Sopenharmony_ci * first write.
21762306a36Sopenharmony_ci * Hint: if you're writing a value, first read the file, modify only the value
21862306a36Sopenharmony_ci * you're changing, then write entire buffer back.
21962306a36Sopenharmony_ci */
22062306a36Sopenharmony_cistatic ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	struct file *file = iocb->ki_filp;
22362306a36Sopenharmony_ci	struct configfs_buffer *buffer = file->private_data;
22462306a36Sopenharmony_ci	int len;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	mutex_lock(&buffer->mutex);
22762306a36Sopenharmony_ci	len = fill_write_buffer(buffer, from);
22862306a36Sopenharmony_ci	if (len > 0)
22962306a36Sopenharmony_ci		len = flush_write_buffer(file, buffer, len);
23062306a36Sopenharmony_ci	if (len > 0)
23162306a36Sopenharmony_ci		iocb->ki_pos += len;
23262306a36Sopenharmony_ci	mutex_unlock(&buffer->mutex);
23362306a36Sopenharmony_ci	return len;
23462306a36Sopenharmony_ci}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistatic ssize_t configfs_bin_write_iter(struct kiocb *iocb,
23762306a36Sopenharmony_ci				       struct iov_iter *from)
23862306a36Sopenharmony_ci{
23962306a36Sopenharmony_ci	struct file *file = iocb->ki_filp;
24062306a36Sopenharmony_ci	struct configfs_buffer *buffer = file->private_data;
24162306a36Sopenharmony_ci	void *tbuf = NULL;
24262306a36Sopenharmony_ci	size_t end_offset;
24362306a36Sopenharmony_ci	ssize_t len;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	mutex_lock(&buffer->mutex);
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	/* we don't support switching read/write modes */
24862306a36Sopenharmony_ci	if (buffer->read_in_progress) {
24962306a36Sopenharmony_ci		len = -ETXTBSY;
25062306a36Sopenharmony_ci		goto out;
25162306a36Sopenharmony_ci	}
25262306a36Sopenharmony_ci	buffer->write_in_progress = true;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	/* buffer grows? */
25562306a36Sopenharmony_ci	end_offset = iocb->ki_pos + iov_iter_count(from);
25662306a36Sopenharmony_ci	if (end_offset > buffer->bin_buffer_size) {
25762306a36Sopenharmony_ci		if (buffer->cb_max_size && end_offset > buffer->cb_max_size) {
25862306a36Sopenharmony_ci			len = -EFBIG;
25962306a36Sopenharmony_ci			goto out;
26062306a36Sopenharmony_ci		}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci		tbuf = vmalloc(end_offset);
26362306a36Sopenharmony_ci		if (tbuf == NULL) {
26462306a36Sopenharmony_ci			len = -ENOMEM;
26562306a36Sopenharmony_ci			goto out;
26662306a36Sopenharmony_ci		}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci		/* copy old contents */
26962306a36Sopenharmony_ci		if (buffer->bin_buffer) {
27062306a36Sopenharmony_ci			memcpy(tbuf, buffer->bin_buffer,
27162306a36Sopenharmony_ci				buffer->bin_buffer_size);
27262306a36Sopenharmony_ci			vfree(buffer->bin_buffer);
27362306a36Sopenharmony_ci		}
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci		/* clear the new area */
27662306a36Sopenharmony_ci		memset(tbuf + buffer->bin_buffer_size, 0,
27762306a36Sopenharmony_ci			end_offset - buffer->bin_buffer_size);
27862306a36Sopenharmony_ci		buffer->bin_buffer = tbuf;
27962306a36Sopenharmony_ci		buffer->bin_buffer_size = end_offset;
28062306a36Sopenharmony_ci	}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	len = copy_from_iter(buffer->bin_buffer + iocb->ki_pos,
28362306a36Sopenharmony_ci			     buffer->bin_buffer_size - iocb->ki_pos, from);
28462306a36Sopenharmony_ci	iocb->ki_pos += len;
28562306a36Sopenharmony_ciout:
28662306a36Sopenharmony_ci	mutex_unlock(&buffer->mutex);
28762306a36Sopenharmony_ci	return len ? : -EFAULT;
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistatic int __configfs_open_file(struct inode *inode, struct file *file, int type)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	struct dentry *dentry = file->f_path.dentry;
29362306a36Sopenharmony_ci	struct configfs_fragment *frag = to_frag(file);
29462306a36Sopenharmony_ci	struct configfs_attribute *attr;
29562306a36Sopenharmony_ci	struct configfs_buffer *buffer;
29662306a36Sopenharmony_ci	int error;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	error = -ENOMEM;
29962306a36Sopenharmony_ci	buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
30062306a36Sopenharmony_ci	if (!buffer)
30162306a36Sopenharmony_ci		goto out;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	error = -ENOENT;
30462306a36Sopenharmony_ci	down_read(&frag->frag_sem);
30562306a36Sopenharmony_ci	if (unlikely(frag->frag_dead))
30662306a36Sopenharmony_ci		goto out_free_buffer;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	error = -EINVAL;
30962306a36Sopenharmony_ci	buffer->item = to_item(dentry->d_parent);
31062306a36Sopenharmony_ci	if (!buffer->item)
31162306a36Sopenharmony_ci		goto out_free_buffer;
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	attr = to_attr(dentry);
31462306a36Sopenharmony_ci	if (!attr)
31562306a36Sopenharmony_ci		goto out_free_buffer;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	if (type & CONFIGFS_ITEM_BIN_ATTR) {
31862306a36Sopenharmony_ci		buffer->bin_attr = to_bin_attr(dentry);
31962306a36Sopenharmony_ci		buffer->cb_max_size = buffer->bin_attr->cb_max_size;
32062306a36Sopenharmony_ci	} else {
32162306a36Sopenharmony_ci		buffer->attr = attr;
32262306a36Sopenharmony_ci	}
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	buffer->owner = attr->ca_owner;
32562306a36Sopenharmony_ci	/* Grab the module reference for this attribute if we have one */
32662306a36Sopenharmony_ci	error = -ENODEV;
32762306a36Sopenharmony_ci	if (!try_module_get(buffer->owner))
32862306a36Sopenharmony_ci		goto out_free_buffer;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	error = -EACCES;
33162306a36Sopenharmony_ci	if (!buffer->item->ci_type)
33262306a36Sopenharmony_ci		goto out_put_module;
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	buffer->ops = buffer->item->ci_type->ct_item_ops;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	/* File needs write support.
33762306a36Sopenharmony_ci	 * The inode's perms must say it's ok,
33862306a36Sopenharmony_ci	 * and we must have a store method.
33962306a36Sopenharmony_ci	 */
34062306a36Sopenharmony_ci	if (file->f_mode & FMODE_WRITE) {
34162306a36Sopenharmony_ci		if (!(inode->i_mode & S_IWUGO))
34262306a36Sopenharmony_ci			goto out_put_module;
34362306a36Sopenharmony_ci		if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
34462306a36Sopenharmony_ci			goto out_put_module;
34562306a36Sopenharmony_ci		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
34662306a36Sopenharmony_ci			goto out_put_module;
34762306a36Sopenharmony_ci	}
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	/* File needs read support.
35062306a36Sopenharmony_ci	 * The inode's perms must say it's ok, and we there
35162306a36Sopenharmony_ci	 * must be a show method for it.
35262306a36Sopenharmony_ci	 */
35362306a36Sopenharmony_ci	if (file->f_mode & FMODE_READ) {
35462306a36Sopenharmony_ci		if (!(inode->i_mode & S_IRUGO))
35562306a36Sopenharmony_ci			goto out_put_module;
35662306a36Sopenharmony_ci		if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
35762306a36Sopenharmony_ci			goto out_put_module;
35862306a36Sopenharmony_ci		if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
35962306a36Sopenharmony_ci			goto out_put_module;
36062306a36Sopenharmony_ci	}
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	mutex_init(&buffer->mutex);
36362306a36Sopenharmony_ci	buffer->needs_read_fill = 1;
36462306a36Sopenharmony_ci	buffer->read_in_progress = false;
36562306a36Sopenharmony_ci	buffer->write_in_progress = false;
36662306a36Sopenharmony_ci	file->private_data = buffer;
36762306a36Sopenharmony_ci	up_read(&frag->frag_sem);
36862306a36Sopenharmony_ci	return 0;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ciout_put_module:
37162306a36Sopenharmony_ci	module_put(buffer->owner);
37262306a36Sopenharmony_ciout_free_buffer:
37362306a36Sopenharmony_ci	up_read(&frag->frag_sem);
37462306a36Sopenharmony_ci	kfree(buffer);
37562306a36Sopenharmony_ciout:
37662306a36Sopenharmony_ci	return error;
37762306a36Sopenharmony_ci}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_cistatic int configfs_release(struct inode *inode, struct file *filp)
38062306a36Sopenharmony_ci{
38162306a36Sopenharmony_ci	struct configfs_buffer *buffer = filp->private_data;
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	module_put(buffer->owner);
38462306a36Sopenharmony_ci	if (buffer->page)
38562306a36Sopenharmony_ci		free_page((unsigned long)buffer->page);
38662306a36Sopenharmony_ci	mutex_destroy(&buffer->mutex);
38762306a36Sopenharmony_ci	kfree(buffer);
38862306a36Sopenharmony_ci	return 0;
38962306a36Sopenharmony_ci}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_cistatic int configfs_open_file(struct inode *inode, struct file *filp)
39262306a36Sopenharmony_ci{
39362306a36Sopenharmony_ci	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_cistatic int configfs_open_bin_file(struct inode *inode, struct file *filp)
39762306a36Sopenharmony_ci{
39862306a36Sopenharmony_ci	return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_cistatic int configfs_release_bin_file(struct inode *inode, struct file *file)
40262306a36Sopenharmony_ci{
40362306a36Sopenharmony_ci	struct configfs_buffer *buffer = file->private_data;
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	if (buffer->write_in_progress) {
40662306a36Sopenharmony_ci		struct configfs_fragment *frag = to_frag(file);
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci		down_read(&frag->frag_sem);
40962306a36Sopenharmony_ci		if (!frag->frag_dead) {
41062306a36Sopenharmony_ci			/* result of ->release() is ignored */
41162306a36Sopenharmony_ci			buffer->bin_attr->write(buffer->item,
41262306a36Sopenharmony_ci					buffer->bin_buffer,
41362306a36Sopenharmony_ci					buffer->bin_buffer_size);
41462306a36Sopenharmony_ci		}
41562306a36Sopenharmony_ci		up_read(&frag->frag_sem);
41662306a36Sopenharmony_ci	}
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	vfree(buffer->bin_buffer);
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	configfs_release(inode, file);
42162306a36Sopenharmony_ci	return 0;
42262306a36Sopenharmony_ci}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ciconst struct file_operations configfs_file_operations = {
42662306a36Sopenharmony_ci	.read_iter	= configfs_read_iter,
42762306a36Sopenharmony_ci	.write_iter	= configfs_write_iter,
42862306a36Sopenharmony_ci	.llseek		= generic_file_llseek,
42962306a36Sopenharmony_ci	.open		= configfs_open_file,
43062306a36Sopenharmony_ci	.release	= configfs_release,
43162306a36Sopenharmony_ci};
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ciconst struct file_operations configfs_bin_file_operations = {
43462306a36Sopenharmony_ci	.read_iter	= configfs_bin_read_iter,
43562306a36Sopenharmony_ci	.write_iter	= configfs_bin_write_iter,
43662306a36Sopenharmony_ci	.llseek		= NULL,		/* bin file is not seekable */
43762306a36Sopenharmony_ci	.open		= configfs_open_bin_file,
43862306a36Sopenharmony_ci	.release	= configfs_release_bin_file,
43962306a36Sopenharmony_ci};
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci/**
44262306a36Sopenharmony_ci *	configfs_create_file - create an attribute file for an item.
44362306a36Sopenharmony_ci *	@item:	item we're creating for.
44462306a36Sopenharmony_ci *	@attr:	atrribute descriptor.
44562306a36Sopenharmony_ci */
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ciint configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
44862306a36Sopenharmony_ci{
44962306a36Sopenharmony_ci	struct dentry *dir = item->ci_dentry;
45062306a36Sopenharmony_ci	struct configfs_dirent *parent_sd = dir->d_fsdata;
45162306a36Sopenharmony_ci	umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
45262306a36Sopenharmony_ci	int error = 0;
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
45562306a36Sopenharmony_ci	error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
45662306a36Sopenharmony_ci				     CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
45762306a36Sopenharmony_ci	inode_unlock(d_inode(dir));
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	return error;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci/**
46362306a36Sopenharmony_ci *	configfs_create_bin_file - create a binary attribute file for an item.
46462306a36Sopenharmony_ci *	@item:	item we're creating for.
46562306a36Sopenharmony_ci *	@bin_attr: atrribute descriptor.
46662306a36Sopenharmony_ci */
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ciint configfs_create_bin_file(struct config_item *item,
46962306a36Sopenharmony_ci		const struct configfs_bin_attribute *bin_attr)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	struct dentry *dir = item->ci_dentry;
47262306a36Sopenharmony_ci	struct configfs_dirent *parent_sd = dir->d_fsdata;
47362306a36Sopenharmony_ci	umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
47462306a36Sopenharmony_ci	int error = 0;
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
47762306a36Sopenharmony_ci	error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
47862306a36Sopenharmony_ci				     CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
47962306a36Sopenharmony_ci	inode_unlock(dir->d_inode);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	return error;
48262306a36Sopenharmony_ci}
483