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 __user *buf, size_t read_size, 103 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 /* add 1 for null terminator */ 124 ret = simple_read_from_buffer(buf, read_size, ppos, prebuf, 125 strlen(prebuf) + 1); 126 127 unlock: 128 mutex_unlock(&sattr->mutex); 129 return (ret); 130 } 131 132 /* 133 * simple_attr_write_common: write contents of buffer into simple attribute file 134 * 135 * @filp: file pointer 136 * @buf: kernel space buffer 137 * @write_size: number bytes to be transferred 138 * @ppos: starting pointer position for transfer 139 * @is_signed: signedness of data in @buf 140 * 141 * The simple_attr structure is stored in filp->private_data. 142 * Convert the @buf string to unsigned long long. 143 * ->set() writes unsigned long long data into the simple attr file. 144 * 145 * Return value: 146 * On success, number of bytes written to simple attr 147 * On failure, negative signed ERRNO 148 */ 149 static ssize_t 150 simple_attr_write_common(struct file *filp, const char __user *ubuf, 151 size_t write_size, loff_t *ppos, bool is_signed) 152 { 153 struct simple_attr *sattr; 154 unsigned long long data; 155 char *buf; 156 ssize_t ret; 157 158 sattr = filp->private_data; 159 160 if (sattr->set == NULL) 161 return (-EFAULT); 162 163 if (*ppos != 0 || write_size < 1) 164 return (-EINVAL); 165 166 buf = malloc(write_size, M_LSATTR, M_WAITOK); 167 if (copy_from_user(buf, ubuf, write_size) != 0) { 168 free(buf, M_LSATTR); 169 return (-EFAULT); 170 } 171 if (strnlen(buf, write_size) == write_size) { 172 free(buf, M_LSATTR); 173 return (-EINVAL); 174 } 175 176 mutex_lock(&sattr->mutex); 177 178 if (is_signed) 179 ret = kstrtoll(buf, 0, &data); 180 else 181 ret = kstrtoull(buf, 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 = write_size; 190 191 unlock: 192 mutex_unlock(&sattr->mutex); 193 free(buf, M_LSATTR); 194 return (ret); 195 } 196 197 ssize_t 198 simple_attr_write(struct file *filp, const char __user *buf, size_t write_size, 199 loff_t *ppos) 200 { 201 return (simple_attr_write_common(filp, buf, write_size, ppos, false)); 202 } 203 204 ssize_t 205 simple_attr_write_signed(struct file *filp, const char __user *buf, 206 size_t write_size, loff_t *ppos) 207 { 208 return (simple_attr_write_common(filp, buf, write_size, ppos, true)); 209 } 210