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/cdefs.h>
29 #include <sys/types.h>
30 #include <linux/fs.h>
31 
32 MALLOC_DEFINE(M_LSATTR, "simple_attr", "Linux Simple Attribute File");
33 
34 struct simple_attr {
35 	int (*get)(void *, uint64_t *);
36 	int (*set)(void *, uint64_t);
37 	void *data;
38 	const char *fmt;
39 	struct mutex mutex;
40 };
41 
42 /*
43  * simple_attr_open: open and populate simple attribute data
44  *
45  * @inode: file inode
46  * @filp: file pointer
47  * @get: ->get() for reading file data
48  * @set: ->set() for writing file data
49  * @fmt: format specifier for data returned by @get
50  *
51  * Memory allocate a simple_attr and appropriately initialize its members.
52  * The simple_attr must be stored in filp->private_data.
53  * Simple attr files do not support seeking. Open the file as nonseekable.
54  *
55  * Return value: simple attribute file descriptor
56  */
57 int
58 simple_attr_open(struct inode *inode, struct file *filp,
59     int (*get)(void *, uint64_t *), int (*set)(void *, uint64_t),
60     const char *fmt)
61 {
62 	struct simple_attr *sattr;
63 	sattr = malloc(sizeof(*sattr), M_LSATTR, M_ZERO | M_NOWAIT);
64 	if (sattr == NULL)
65 		return (-ENOMEM);
66 
67 	sattr->get = get;
68 	sattr->set = set;
69 	sattr->data = inode->i_private;
70 	sattr->fmt = fmt;
71 	mutex_init(&sattr->mutex);
72 
73 	filp->private_data = (void *) sattr;
74 
75 	return (nonseekable_open(inode, filp));
76 }
77 
78 int
79 simple_attr_release(struct inode *inode, struct file *filp)
80 {
81 	free(filp->private_data, M_LSATTR);
82 	return (0);
83 }
84 
85 /*
86  * simple_attr_read: read simple attr data and transfer into buffer
87  *
88  * @filp: file pointer
89  * @buf: kernel space buffer
90  * @read_size: number of bytes to be transferred
91  * @ppos: starting pointer position for transfer
92  *
93  * The simple_attr structure is stored in filp->private_data.
94  * ->get() retrieves raw file data.
95  * The ->fmt specifier can format this data to be human readable.
96  * This output is then transferred into the @buf buffer.
97  *
98  * Return value:
99  * On success, number of bytes transferred
100  * On failure, negative signed ERRNO
101  */
102 ssize_t
103 simple_attr_read(struct file *filp, char *buf, size_t read_size, loff_t *ppos)
104 {
105 	struct simple_attr *sattr;
106 	uint64_t data;
107 	ssize_t ret;
108 	char prebuf[24];
109 
110 	sattr = filp->private_data;
111 
112 	if (sattr->get == NULL)
113 		return (-EFAULT);
114 
115 	mutex_lock(&sattr->mutex);
116 
117 	ret = sattr->get(sattr->data, &data);
118 	if (ret)
119 		goto unlock;
120 
121 	scnprintf(prebuf, sizeof(prebuf), sattr->fmt, data);
122 
123 	ret = strlen(prebuf) + 1;
124 	if (*ppos >= ret || read_size < 1) {
125 		ret = -EINVAL;
126 		goto unlock;
127 	}
128 
129 	read_size = min(ret - *ppos, read_size);
130 	ret = strscpy(buf, prebuf + *ppos, read_size);
131 
132 	/* add 1 for null terminator */
133 	if (ret > 0)
134 		ret += 1;
135 
136 unlock:
137 	mutex_unlock(&sattr->mutex);
138 	return (ret);
139 }
140 
141 /*
142  * simple_attr_write: write contents of buffer into simple attribute file
143  *
144  * @filp: file pointer
145  * @buf: kernel space buffer
146  * @write_size: number bytes to be transferred
147  * @ppos: starting pointer position for transfer
148  *
149  * The simple_attr structure is stored in filp->private_data.
150  * Convert the @buf string to unsigned long long.
151  * ->set() writes unsigned long long data into the simple attr file.
152  *
153  * Return value:
154  * On success, number of bytes written to simple attr
155  * On failure, negative signed ERRNO
156  */
157 ssize_t
158 simple_attr_write(struct file *filp, const char *buf, size_t write_size, loff_t *ppos)
159 {
160 	struct simple_attr *sattr;
161 	unsigned long long data;
162 	size_t bufsize;
163 	ssize_t ret;
164 
165 	sattr = filp->private_data;
166 	bufsize = strlen(buf) + 1;
167 
168 	if (sattr->set == NULL)
169 		return (-EFAULT);
170 
171 	if (*ppos >= bufsize || write_size < 1)
172 		return (-EINVAL);
173 
174 	mutex_lock(&sattr->mutex);
175 
176 	ret = kstrtoull(buf + *ppos, 0, &data);
177 	if (ret)
178 		goto unlock;
179 
180 	ret = sattr->set(sattr->data, data);
181 	if (ret)
182 		goto unlock;
183 
184 	ret = bufsize - *ppos;
185 
186 unlock:
187 	mutex_unlock(&sattr->mutex);
188 	return (ret);
189 }
190