Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Masahiro Yamada | fa60ce2 | 2021-05-06 18:06:44 -0700 | [diff] [blame^] | 2 | /* |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 3 | * file.c - operations for regular (text) files. |
| 4 | * |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 5 | * Based on sysfs: |
| 6 | * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel |
| 7 | * |
| 8 | * configfs Copyright (C) 2005 Oracle. All rights reserved. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/module.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 13 | #include <linux/slab.h> |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 17 | |
| 18 | #include <linux/configfs.h> |
| 19 | #include "configfs_internal.h" |
| 20 | |
Joel Becker | b23cdde | 2007-06-22 13:07:02 -0700 | [diff] [blame] | 21 | /* |
| 22 | * A simple attribute can only be 4096 characters. Why 4k? Because the |
| 23 | * original code limited it to PAGE_SIZE. That's a bad idea, though, |
| 24 | * because an attribute of 16k on ia64 won't work on x86. So we limit to |
| 25 | * 4k, our minimum common page size. |
| 26 | */ |
| 27 | #define SIMPLE_ATTR_SIZE 4096 |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 28 | |
| 29 | struct configfs_buffer { |
| 30 | size_t count; |
| 31 | loff_t pos; |
| 32 | char * page; |
| 33 | struct configfs_item_operations * ops; |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 34 | struct mutex mutex; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 35 | int needs_read_fill; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 36 | bool read_in_progress; |
| 37 | bool write_in_progress; |
| 38 | char *bin_buffer; |
| 39 | int bin_buffer_size; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 40 | int cb_max_size; |
| 41 | struct config_item *item; |
| 42 | struct module *owner; |
| 43 | union { |
| 44 | struct configfs_attribute *attr; |
| 45 | struct configfs_bin_attribute *bin_attr; |
| 46 | }; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 49 | static inline struct configfs_fragment *to_frag(struct file *file) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 50 | { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 51 | struct configfs_dirent *sd = file->f_path.dentry->d_fsdata; |
| 52 | |
| 53 | return sd->s_frag; |
| 54 | } |
| 55 | |
| 56 | static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer) |
| 57 | { |
| 58 | struct configfs_fragment *frag = to_frag(file); |
| 59 | ssize_t count = -ENOENT; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 60 | |
| 61 | if (!buffer->page) |
| 62 | buffer->page = (char *) get_zeroed_page(GFP_KERNEL); |
| 63 | if (!buffer->page) |
| 64 | return -ENOMEM; |
| 65 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 66 | down_read(&frag->frag_sem); |
| 67 | if (!frag->frag_dead) |
| 68 | count = buffer->attr->show(buffer->item, buffer->page); |
| 69 | up_read(&frag->frag_sem); |
| 70 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 71 | if (count < 0) |
| 72 | return count; |
| 73 | if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE)) |
| 74 | return -EIO; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 75 | buffer->needs_read_fill = 0; |
| 76 | buffer->count = count; |
| 77 | return 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 80 | /** |
| 81 | * configfs_read_file - read an attribute. |
| 82 | * @file: file pointer. |
| 83 | * @buf: buffer to fill. |
| 84 | * @count: number of bytes to read. |
| 85 | * @ppos: starting offset in file. |
| 86 | * |
| 87 | * Userspace wants to read an attribute file. The attribute descriptor |
| 88 | * is in the file's ->d_fsdata. The target item is in the directory's |
| 89 | * ->d_fsdata. |
| 90 | * |
| 91 | * We call fill_read_buffer() to allocate and fill the buffer from the |
| 92 | * item's show() method exactly once (if the read is happening from |
| 93 | * the beginning of the file). That should fill the entire buffer with |
| 94 | * all the data the item has to offer for that attribute. |
| 95 | * We then call flush_read_buffer() to copy the buffer to userspace |
| 96 | * in the increments specified. |
| 97 | */ |
| 98 | |
| 99 | static ssize_t |
| 100 | configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 101 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 102 | struct configfs_buffer *buffer = file->private_data; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 103 | ssize_t retval = 0; |
| 104 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 105 | mutex_lock(&buffer->mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 106 | if (buffer->needs_read_fill) { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 107 | retval = fill_read_buffer(file, buffer); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 108 | if (retval) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 109 | goto out; |
| 110 | } |
Zach Brown | 4779efc | 2006-10-03 01:16:05 -0700 | [diff] [blame] | 111 | pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n", |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 112 | __func__, count, *ppos, buffer->page); |
Akinobu Mita | 92f4c70 | 2007-05-09 02:33:32 -0700 | [diff] [blame] | 113 | retval = simple_read_from_buffer(buf, count, ppos, buffer->page, |
| 114 | buffer->count); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 115 | out: |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 116 | mutex_unlock(&buffer->mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 117 | return retval; |
| 118 | } |
| 119 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 120 | /** |
| 121 | * configfs_read_bin_file - read a binary attribute. |
| 122 | * @file: file pointer. |
| 123 | * @buf: buffer to fill. |
| 124 | * @count: number of bytes to read. |
| 125 | * @ppos: starting offset in file. |
| 126 | * |
| 127 | * Userspace wants to read a binary attribute file. The attribute |
| 128 | * descriptor is in the file's ->d_fsdata. The target item is in the |
| 129 | * directory's ->d_fsdata. |
| 130 | * |
| 131 | * We check whether we need to refill the buffer. If so we will |
| 132 | * call the attributes' attr->read() twice. The first time we |
| 133 | * will pass a NULL as a buffer pointer, which the attributes' method |
| 134 | * will use to return the size of the buffer required. If no error |
| 135 | * occurs we will allocate the buffer using vmalloc and call |
| 136 | * attr->read() again passing that buffer as an argument. |
| 137 | * Then we just copy to user-space using simple_read_from_buffer. |
| 138 | */ |
| 139 | |
| 140 | static ssize_t |
| 141 | configfs_read_bin_file(struct file *file, char __user *buf, |
| 142 | size_t count, loff_t *ppos) |
| 143 | { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 144 | struct configfs_fragment *frag = to_frag(file); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 145 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 146 | ssize_t retval = 0; |
| 147 | ssize_t len = min_t(size_t, count, PAGE_SIZE); |
| 148 | |
| 149 | mutex_lock(&buffer->mutex); |
| 150 | |
| 151 | /* we don't support switching read/write modes */ |
| 152 | if (buffer->write_in_progress) { |
| 153 | retval = -ETXTBSY; |
| 154 | goto out; |
| 155 | } |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 156 | buffer->read_in_progress = true; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 157 | |
| 158 | if (buffer->needs_read_fill) { |
| 159 | /* perform first read with buf == NULL to get extent */ |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 160 | down_read(&frag->frag_sem); |
| 161 | if (!frag->frag_dead) |
| 162 | len = buffer->bin_attr->read(buffer->item, NULL, 0); |
| 163 | else |
| 164 | len = -ENOENT; |
| 165 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 166 | if (len <= 0) { |
| 167 | retval = len; |
| 168 | goto out; |
| 169 | } |
| 170 | |
| 171 | /* do not exceed the maximum value */ |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 172 | if (buffer->cb_max_size && len > buffer->cb_max_size) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 173 | retval = -EFBIG; |
| 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | buffer->bin_buffer = vmalloc(len); |
| 178 | if (buffer->bin_buffer == NULL) { |
| 179 | retval = -ENOMEM; |
| 180 | goto out; |
| 181 | } |
| 182 | buffer->bin_buffer_size = len; |
| 183 | |
| 184 | /* perform second read to fill buffer */ |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 185 | down_read(&frag->frag_sem); |
| 186 | if (!frag->frag_dead) |
| 187 | len = buffer->bin_attr->read(buffer->item, |
| 188 | buffer->bin_buffer, len); |
| 189 | else |
| 190 | len = -ENOENT; |
| 191 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 192 | if (len < 0) { |
| 193 | retval = len; |
| 194 | vfree(buffer->bin_buffer); |
| 195 | buffer->bin_buffer_size = 0; |
| 196 | buffer->bin_buffer = NULL; |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | buffer->needs_read_fill = 0; |
| 201 | } |
| 202 | |
| 203 | retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer, |
| 204 | buffer->bin_buffer_size); |
| 205 | out: |
| 206 | mutex_unlock(&buffer->mutex); |
| 207 | return retval; |
| 208 | } |
| 209 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * fill_write_buffer - copy buffer from userspace. |
| 213 | * @buffer: data buffer for file. |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 214 | * @buf: data from user. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 215 | * @count: number of bytes in @userbuf. |
| 216 | * |
| 217 | * Allocate @buffer->page if it hasn't been already, then |
| 218 | * copy the user-supplied buffer into it. |
| 219 | */ |
| 220 | |
| 221 | static int |
| 222 | fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count) |
| 223 | { |
| 224 | int error; |
| 225 | |
| 226 | if (!buffer->page) |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 227 | buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 228 | if (!buffer->page) |
| 229 | return -ENOMEM; |
| 230 | |
Joel Becker | b23cdde | 2007-06-22 13:07:02 -0700 | [diff] [blame] | 231 | if (count >= SIMPLE_ATTR_SIZE) |
| 232 | count = SIMPLE_ATTR_SIZE - 1; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 233 | error = copy_from_user(buffer->page,buf,count); |
| 234 | buffer->needs_read_fill = 1; |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 235 | /* if buf is assumed to contain a string, terminate it by \0, |
| 236 | * so e.g. sscanf() can scan the string easily */ |
| 237 | buffer->page[count] = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 238 | return error ? -EFAULT : count; |
| 239 | } |
| 240 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 241 | static int |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 242 | flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 243 | { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 244 | struct configfs_fragment *frag = to_frag(file); |
| 245 | int res = -ENOENT; |
| 246 | |
| 247 | down_read(&frag->frag_sem); |
| 248 | if (!frag->frag_dead) |
| 249 | res = buffer->attr->store(buffer->item, buffer->page, count); |
| 250 | up_read(&frag->frag_sem); |
| 251 | return res; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * configfs_write_file - write an attribute. |
| 257 | * @file: file pointer |
| 258 | * @buf: data to write |
| 259 | * @count: number of bytes |
| 260 | * @ppos: starting offset |
| 261 | * |
| 262 | * Similar to configfs_read_file(), though working in the opposite direction. |
| 263 | * We allocate and fill the data from the user in fill_write_buffer(), |
| 264 | * then push it to the config_item in flush_write_buffer(). |
| 265 | * There is no easy way for us to know if userspace is only doing a partial |
| 266 | * write, so we don't support them. We expect the entire buffer to come |
| 267 | * on the first write. |
Randy Dunlap | ce9bebe | 2020-10-15 20:10:18 -0700 | [diff] [blame] | 268 | * Hint: if you're writing a value, first read the file, modify only |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 269 | * the value you're changing, then write entire buffer back. |
| 270 | */ |
| 271 | |
| 272 | static ssize_t |
| 273 | configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 274 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 275 | struct configfs_buffer *buffer = file->private_data; |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 276 | ssize_t len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 277 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 278 | mutex_lock(&buffer->mutex); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 279 | len = fill_write_buffer(buffer, buf, count); |
| 280 | if (len > 0) |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 281 | len = flush_write_buffer(file, buffer, len); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 282 | if (len > 0) |
| 283 | *ppos += len; |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 284 | mutex_unlock(&buffer->mutex); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 285 | return len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 288 | /** |
| 289 | * configfs_write_bin_file - write a binary attribute. |
| 290 | * @file: file pointer |
| 291 | * @buf: data to write |
| 292 | * @count: number of bytes |
| 293 | * @ppos: starting offset |
| 294 | * |
| 295 | * Writing to a binary attribute file is similar to a normal read. |
| 296 | * We buffer the consecutive writes (binary attribute files do not |
| 297 | * support lseek) in a continuously growing buffer, but we don't |
| 298 | * commit until the close of the file. |
| 299 | */ |
| 300 | |
| 301 | static ssize_t |
| 302 | configfs_write_bin_file(struct file *file, const char __user *buf, |
| 303 | size_t count, loff_t *ppos) |
| 304 | { |
| 305 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 306 | void *tbuf = NULL; |
| 307 | ssize_t len; |
| 308 | |
| 309 | mutex_lock(&buffer->mutex); |
| 310 | |
| 311 | /* we don't support switching read/write modes */ |
| 312 | if (buffer->read_in_progress) { |
| 313 | len = -ETXTBSY; |
| 314 | goto out; |
| 315 | } |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 316 | buffer->write_in_progress = true; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 317 | |
| 318 | /* buffer grows? */ |
| 319 | if (*ppos + count > buffer->bin_buffer_size) { |
| 320 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 321 | if (buffer->cb_max_size && |
| 322 | *ppos + count > buffer->cb_max_size) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 323 | len = -EFBIG; |
Phil Turnbull | 42857cf | 2016-09-15 12:20:12 -0400 | [diff] [blame] | 324 | goto out; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | tbuf = vmalloc(*ppos + count); |
| 328 | if (tbuf == NULL) { |
| 329 | len = -ENOMEM; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | /* copy old contents */ |
| 334 | if (buffer->bin_buffer) { |
| 335 | memcpy(tbuf, buffer->bin_buffer, |
| 336 | buffer->bin_buffer_size); |
| 337 | vfree(buffer->bin_buffer); |
| 338 | } |
| 339 | |
| 340 | /* clear the new area */ |
| 341 | memset(tbuf + buffer->bin_buffer_size, 0, |
| 342 | *ppos + count - buffer->bin_buffer_size); |
| 343 | buffer->bin_buffer = tbuf; |
| 344 | buffer->bin_buffer_size = *ppos + count; |
| 345 | } |
| 346 | |
| 347 | len = simple_write_to_buffer(buffer->bin_buffer, |
| 348 | buffer->bin_buffer_size, ppos, buf, count); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 349 | out: |
| 350 | mutex_unlock(&buffer->mutex); |
| 351 | return len; |
| 352 | } |
| 353 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 354 | static int __configfs_open_file(struct inode *inode, struct file *file, int type) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 355 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 356 | struct dentry *dentry = file->f_path.dentry; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 357 | struct configfs_fragment *frag = to_frag(file); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 358 | struct configfs_attribute *attr; |
| 359 | struct configfs_buffer *buffer; |
| 360 | int error; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 361 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 362 | error = -ENOMEM; |
| 363 | buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL); |
| 364 | if (!buffer) |
| 365 | goto out; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 366 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 367 | error = -ENOENT; |
| 368 | down_read(&frag->frag_sem); |
| 369 | if (unlikely(frag->frag_dead)) |
| 370 | goto out_free_buffer; |
| 371 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 372 | error = -EINVAL; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 373 | buffer->item = to_item(dentry->d_parent); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 374 | if (!buffer->item) |
| 375 | goto out_free_buffer; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 376 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 377 | attr = to_attr(dentry); |
| 378 | if (!attr) |
Daiyue Zhang | 14fbbc8 | 2021-03-01 14:10:53 +0800 | [diff] [blame] | 379 | goto out_free_buffer; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 380 | |
| 381 | if (type & CONFIGFS_ITEM_BIN_ATTR) { |
| 382 | buffer->bin_attr = to_bin_attr(dentry); |
| 383 | buffer->cb_max_size = buffer->bin_attr->cb_max_size; |
| 384 | } else { |
| 385 | buffer->attr = attr; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 388 | buffer->owner = attr->ca_owner; |
| 389 | /* Grab the module reference for this attribute if we have one */ |
| 390 | error = -ENODEV; |
| 391 | if (!try_module_get(buffer->owner)) |
Daiyue Zhang | 14fbbc8 | 2021-03-01 14:10:53 +0800 | [diff] [blame] | 392 | goto out_free_buffer; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 393 | |
| 394 | error = -EACCES; |
| 395 | if (!buffer->item->ci_type) |
| 396 | goto out_put_module; |
| 397 | |
| 398 | buffer->ops = buffer->item->ci_type->ct_item_ops; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 399 | |
| 400 | /* File needs write support. |
| 401 | * The inode's perms must say it's ok, |
| 402 | * and we must have a store method. |
| 403 | */ |
| 404 | if (file->f_mode & FMODE_WRITE) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 405 | if (!(inode->i_mode & S_IWUGO)) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 406 | goto out_put_module; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 407 | if ((type & CONFIGFS_ITEM_ATTR) && !attr->store) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 408 | goto out_put_module; |
| 409 | if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write) |
| 410 | goto out_put_module; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* File needs read support. |
| 414 | * The inode's perms must say it's ok, and we there |
| 415 | * must be a show method for it. |
| 416 | */ |
| 417 | if (file->f_mode & FMODE_READ) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 418 | if (!(inode->i_mode & S_IRUGO)) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 419 | goto out_put_module; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 420 | if ((type & CONFIGFS_ITEM_ATTR) && !attr->show) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 421 | goto out_put_module; |
| 422 | if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read) |
| 423 | goto out_put_module; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 426 | mutex_init(&buffer->mutex); |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 427 | buffer->needs_read_fill = 1; |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 428 | buffer->read_in_progress = false; |
| 429 | buffer->write_in_progress = false; |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 430 | file->private_data = buffer; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 431 | up_read(&frag->frag_sem); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 432 | return 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 433 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 434 | out_put_module: |
| 435 | module_put(buffer->owner); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 436 | out_free_buffer: |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 437 | up_read(&frag->frag_sem); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 438 | kfree(buffer); |
| 439 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 440 | return error; |
| 441 | } |
| 442 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 443 | static int configfs_release(struct inode *inode, struct file *filp) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 444 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 445 | struct configfs_buffer *buffer = filp->private_data; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 446 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 447 | module_put(buffer->owner); |
| 448 | if (buffer->page) |
| 449 | free_page((unsigned long)buffer->page); |
| 450 | mutex_destroy(&buffer->mutex); |
| 451 | kfree(buffer); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 455 | static int configfs_open_file(struct inode *inode, struct file *filp) |
| 456 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 457 | return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static int configfs_open_bin_file(struct inode *inode, struct file *filp) |
| 461 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 462 | return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 463 | } |
| 464 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 465 | static int configfs_release_bin_file(struct inode *inode, struct file *file) |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 466 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 467 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 468 | |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 469 | buffer->read_in_progress = false; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 470 | |
| 471 | if (buffer->write_in_progress) { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 472 | struct configfs_fragment *frag = to_frag(file); |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 473 | buffer->write_in_progress = false; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 474 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 475 | down_read(&frag->frag_sem); |
| 476 | if (!frag->frag_dead) { |
| 477 | /* result of ->release() is ignored */ |
| 478 | buffer->bin_attr->write(buffer->item, |
| 479 | buffer->bin_buffer, |
| 480 | buffer->bin_buffer_size); |
| 481 | } |
| 482 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 483 | /* vfree on NULL is safe */ |
| 484 | vfree(buffer->bin_buffer); |
| 485 | buffer->bin_buffer = NULL; |
| 486 | buffer->bin_buffer_size = 0; |
| 487 | buffer->needs_read_fill = 1; |
| 488 | } |
| 489 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 490 | configfs_release(inode, file); |
| 491 | return 0; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 495 | const struct file_operations configfs_file_operations = { |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 496 | .read = configfs_read_file, |
| 497 | .write = configfs_write_file, |
| 498 | .llseek = generic_file_llseek, |
| 499 | .open = configfs_open_file, |
| 500 | .release = configfs_release, |
| 501 | }; |
| 502 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 503 | const struct file_operations configfs_bin_file_operations = { |
| 504 | .read = configfs_read_bin_file, |
| 505 | .write = configfs_write_bin_file, |
| 506 | .llseek = NULL, /* bin file is not seekable */ |
| 507 | .open = configfs_open_bin_file, |
| 508 | .release = configfs_release_bin_file, |
| 509 | }; |
| 510 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 511 | /** |
| 512 | * configfs_create_file - create an attribute file for an item. |
| 513 | * @item: item we're creating for. |
| 514 | * @attr: atrribute descriptor. |
| 515 | */ |
| 516 | |
| 517 | int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr) |
| 518 | { |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 519 | struct dentry *dir = item->ci_dentry; |
| 520 | struct configfs_dirent *parent_sd = dir->d_fsdata; |
| 521 | umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG; |
| 522 | int error = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 523 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 524 | inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL); |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 525 | error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, |
Al Viro | 47320fb | 2019-08-25 19:56:13 -0400 | [diff] [blame] | 526 | CONFIGFS_ITEM_ATTR, parent_sd->s_frag); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 527 | inode_unlock(d_inode(dir)); |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 528 | |
| 529 | return error; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 532 | /** |
| 533 | * configfs_create_bin_file - create a binary attribute file for an item. |
| 534 | * @item: item we're creating for. |
| 535 | * @attr: atrribute descriptor. |
| 536 | */ |
| 537 | |
| 538 | int configfs_create_bin_file(struct config_item *item, |
| 539 | const struct configfs_bin_attribute *bin_attr) |
| 540 | { |
| 541 | struct dentry *dir = item->ci_dentry; |
| 542 | struct configfs_dirent *parent_sd = dir->d_fsdata; |
| 543 | umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG; |
| 544 | int error = 0; |
| 545 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 546 | inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 547 | error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode, |
Al Viro | 47320fb | 2019-08-25 19:56:13 -0400 | [diff] [blame] | 548 | CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 549 | inode_unlock(dir->d_inode); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 550 | |
| 551 | return error; |
| 552 | } |