xref: /freebsd/sys/compat/linuxkpi/common/src/linux_simple_attr.c (revision 8c5c572125665e3fd8a033d1a8b0a370e5a43e24)
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 
31 MALLOC_DEFINE(M_LSATTR, "simple_attr", "Linux Simple Attribute File");
32 
33 struct 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  */
56 int
57 simple_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 
77 int
78 simple_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  */
101 ssize_t
102 simple_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 
135 unlock:
136 	mutex_unlock(&sattr->mutex);
137 	return (ret);
138 }
139 
140 /*
141  * simple_attr_write_common: 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  * @is_signed: signedness of data in @buf
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 static ssize_t
158 simple_attr_write_common(struct file *filp, const char *buf, size_t write_size,
159     loff_t *ppos, bool is_signed)
160 {
161 	struct simple_attr *sattr;
162 	unsigned long long data;
163 	size_t bufsize;
164 	ssize_t ret;
165 
166 	sattr = filp->private_data;
167 	bufsize = strlen(buf) + 1;
168 
169 	if (sattr->set == NULL)
170 		return (-EFAULT);
171 
172 	if (*ppos >= bufsize || write_size < 1)
173 		return (-EINVAL);
174 
175 	mutex_lock(&sattr->mutex);
176 
177 	if (is_signed)
178 		ret = kstrtoll(buf + *ppos, 0, &data);
179 	else
180 		ret = kstrtoull(buf + *ppos, 0, &data);
181 	if (ret)
182 		goto unlock;
183 
184 	ret = sattr->set(sattr->data, data);
185 	if (ret)
186 		goto unlock;
187 
188 	ret = bufsize - *ppos;
189 
190 unlock:
191 	mutex_unlock(&sattr->mutex);
192 	return (ret);
193 }
194 
195 ssize_t
196 simple_attr_write(struct file *filp, const char *buf, size_t write_size,
197     loff_t *ppos)
198 {
199 	return (simple_attr_write_common(filp, buf,  write_size, ppos, false));
200 }
201 
202 ssize_t
203 simple_attr_write_signed(struct file *filp, const char *buf, size_t write_size,
204     loff_t *ppos)
205 {
206 	return (simple_attr_write_common(filp, buf,  write_size, ppos, true));
207 }
208