1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022, Jake Freeland <jfree@freebsd.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29#include <linux/fs.h>
30
31MALLOC_DEFINE(M_LSATTR, "simple_attr", "Linux Simple Attribute File");
32
33struct simple_attr {
34	int (*get)(void *, uint64_t *);
35	int (*set)(void *, uint64_t);
36	void *data;
37	const char *fmt;
38	struct mutex mutex;
39};
40
41/*
42 * simple_attr_open: open and populate simple attribute data
43 *
44 * @inode: file inode
45 * @filp: file pointer
46 * @get: ->get() for reading file data
47 * @set: ->set() for writing file data
48 * @fmt: format specifier for data returned by @get
49 *
50 * Memory allocate a simple_attr and appropriately initialize its members.
51 * The simple_attr must be stored in filp->private_data.
52 * Simple attr files do not support seeking. Open the file as nonseekable.
53 *
54 * Return value: simple attribute file descriptor
55 */
56int
57simple_attr_open(struct inode *inode, struct file *filp,
58    int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t),
59    const char *fmt)
60{
61	struct simple_attr *sattr;
62	sattr = malloc(sizeof(*sattr), M_LSATTR, M_ZERO | M_NOWAIT);
63	if (sattr == NULL)
64		return (-ENOMEM);
65
66	sattr->get = get;
67	sattr->set = set;
68	sattr->data = inode->i_private;
69	sattr->fmt = fmt;
70	mutex_init(&sattr->mutex);
71
72	filp->private_data = (void *) sattr;
73
74	return (nonseekable_open(inode, filp));
75}
76
77int
78simple_attr_release(struct inode *inode, struct file *filp)
79{
80	free(filp->private_data, M_LSATTR);
81	return (0);
82}
83
84/*
85 * simple_attr_read: read simple attr data and transfer into buffer
86 *
87 * @filp: file pointer
88 * @buf: kernel space buffer
89 * @read_size: number of bytes to be transferred
90 * @ppos: starting pointer position for transfer
91 *
92 * The simple_attr structure is stored in filp->private_data.
93 * ->get() retrieves raw file data.
94 * The ->fmt specifier can format this data to be human readable.
95 * This output is then transferred into the @buf buffer.
96 *
97 * Return value:
98 * On success, number of bytes transferred
99 * On failure, negative signed ERRNO
100 */
101ssize_t
102simple_attr_read(struct file *filp, char *buf, size_t read_size, loff_t *ppos)
103{
104	struct simple_attr *sattr;
105	uint64_t data;
106	ssize_t ret;
107	char prebuf[24];
108
109	sattr = filp->private_data;
110
111	if (sattr->get == NULL)
112		return (-EFAULT);
113
114	mutex_lock(&sattr->mutex);
115
116	ret = sattr->get(sattr->data, &data);
117	if (ret)
118		goto unlock;
119
120	scnprintf(prebuf, sizeof(prebuf), sattr->fmt, data);
121
122	ret = strlen(prebuf) + 1;
123	if (*ppos >= ret || read_size < 1) {
124		ret = -EINVAL;
125		goto unlock;
126	}
127
128	read_size = min(ret - *ppos, read_size);
129	ret = strscpy(buf, prebuf + *ppos, read_size);
130
131	/* add 1 for null terminator */
132	if (ret > 0)
133		ret += 1;
134
135unlock:
136	mutex_unlock(&sattr->mutex);
137	return (ret);
138}
139
140/*
141 * simple_attr_write: write contents of buffer into simple attribute file
142 *
143 * @filp: file pointer
144 * @buf: kernel space buffer
145 * @write_size: number bytes to be transferred
146 * @ppos: starting pointer position for transfer
147 *
148 * The simple_attr structure is stored in filp->private_data.
149 * Convert the @buf string to unsigned long long.
150 * ->set() writes unsigned long long data into the simple attr file.
151 *
152 * Return value:
153 * On success, number of bytes written to simple attr
154 * On failure, negative signed ERRNO
155 */
156ssize_t
157simple_attr_write(struct file *filp, const char *buf, size_t write_size, loff_t *ppos)
158{
159	struct simple_attr *sattr;
160	unsigned long long data;
161	size_t bufsize;
162	ssize_t ret;
163
164	sattr = filp->private_data;
165	bufsize = strlen(buf) + 1;
166
167	if (sattr->set == NULL)
168		return (-EFAULT);
169
170	if (*ppos >= bufsize || write_size < 1)
171		return (-EINVAL);
172
173	mutex_lock(&sattr->mutex);
174
175	ret = kstrtoull(buf + *ppos, 0, &data);
176	if (ret)
177		goto unlock;
178
179	ret = sattr->set(sattr->data, data);
180	if (ret)
181		goto unlock;
182
183	ret = bufsize - *ppos;
184
185unlock:
186	mutex_unlock(&sattr->mutex);
187	return (ret);
188}
189