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