1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022, Jake Freeland <[email protected]>
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
simple_attr_open(struct inode * inode,struct file * filp,int (* get)(void *,uint64_t *),int (* set)(void *,uint64_t),const char * fmt)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
simple_attr_release(struct inode * inode,struct file * filp)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
simple_attr_read(struct file * filp,char * buf,size_t read_size,loff_t * ppos)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_common: 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 * @is_signed: signedness of data in @buf
149 *
150 * The simple_attr structure is stored in filp->private_data.
151 * Convert the @buf string to unsigned long long.
152 * ->set() writes unsigned long long data into the simple attr file.
153 *
154 * Return value:
155 * On success, number of bytes written to simple attr
156 * On failure, negative signed ERRNO
157 */
158 static ssize_t
simple_attr_write_common(struct file * filp,const char * buf,size_t write_size,loff_t * ppos,bool is_signed)159 simple_attr_write_common(struct file *filp, const char *buf, size_t write_size,
160 loff_t *ppos, bool is_signed)
161 {
162 struct simple_attr *sattr;
163 unsigned long long data;
164 size_t bufsize;
165 ssize_t ret;
166
167 sattr = filp->private_data;
168 bufsize = strlen(buf) + 1;
169
170 if (sattr->set == NULL)
171 return (-EFAULT);
172
173 if (*ppos >= bufsize || write_size < 1)
174 return (-EINVAL);
175
176 mutex_lock(&sattr->mutex);
177
178 if (is_signed)
179 ret = kstrtoll(buf + *ppos, 0, &data);
180 else
181 ret = kstrtoull(buf + *ppos, 0, &data);
182 if (ret)
183 goto unlock;
184
185 ret = sattr->set(sattr->data, data);
186 if (ret)
187 goto unlock;
188
189 ret = bufsize - *ppos;
190
191 unlock:
192 mutex_unlock(&sattr->mutex);
193 return (ret);
194 }
195
196 ssize_t
simple_attr_write(struct file * filp,const char * buf,size_t write_size,loff_t * ppos)197 simple_attr_write(struct file *filp, const char *buf, size_t write_size,
198 loff_t *ppos)
199 {
200 return (simple_attr_write_common(filp, buf, write_size, ppos, false));
201 }
202
203 ssize_t
simple_attr_write_signed(struct file * filp,const char * buf,size_t write_size,loff_t * ppos)204 simple_attr_write_signed(struct file *filp, const char *buf, size_t write_size,
205 loff_t *ppos)
206 {
207 return (simple_attr_write_common(filp, buf, write_size, ppos, true));
208 }
209