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