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> |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 17 | #include <linux/uio.h> |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 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 | |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 80 | static ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 81 | { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 82 | struct file *file = iocb->ki_filp; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 83 | struct configfs_buffer *buffer = file->private_data; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 84 | ssize_t retval = 0; |
| 85 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 86 | mutex_lock(&buffer->mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 87 | if (buffer->needs_read_fill) { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 88 | retval = fill_read_buffer(file, buffer); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 89 | if (retval) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 90 | goto out; |
| 91 | } |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 92 | pr_debug("%s: count = %zd, pos = %lld, buf = %s\n", |
| 93 | __func__, iov_iter_count(to), iocb->ki_pos, buffer->page); |
Bart Van Assche | 420405e | 2021-07-13 10:49:26 -0700 | [diff] [blame] | 94 | if (iocb->ki_pos >= buffer->count) |
| 95 | goto out; |
| 96 | retval = copy_to_iter(buffer->page + iocb->ki_pos, |
| 97 | buffer->count - iocb->ki_pos, to); |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 98 | iocb->ki_pos += retval; |
| 99 | if (retval == 0) |
| 100 | retval = -EFAULT; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 101 | out: |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 102 | mutex_unlock(&buffer->mutex); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 103 | return retval; |
| 104 | } |
| 105 | |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 106 | static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 107 | { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 108 | struct file *file = iocb->ki_filp; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 109 | struct configfs_fragment *frag = to_frag(file); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 110 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 111 | ssize_t retval = 0; |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 112 | ssize_t len; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 113 | |
| 114 | mutex_lock(&buffer->mutex); |
| 115 | |
| 116 | /* we don't support switching read/write modes */ |
| 117 | if (buffer->write_in_progress) { |
| 118 | retval = -ETXTBSY; |
| 119 | goto out; |
| 120 | } |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 121 | buffer->read_in_progress = true; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 122 | |
| 123 | if (buffer->needs_read_fill) { |
| 124 | /* perform first read with buf == NULL to get extent */ |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 125 | down_read(&frag->frag_sem); |
| 126 | if (!frag->frag_dead) |
| 127 | len = buffer->bin_attr->read(buffer->item, NULL, 0); |
| 128 | else |
| 129 | len = -ENOENT; |
| 130 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 131 | if (len <= 0) { |
| 132 | retval = len; |
| 133 | goto out; |
| 134 | } |
| 135 | |
| 136 | /* do not exceed the maximum value */ |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 137 | if (buffer->cb_max_size && len > buffer->cb_max_size) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 138 | retval = -EFBIG; |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | buffer->bin_buffer = vmalloc(len); |
| 143 | if (buffer->bin_buffer == NULL) { |
| 144 | retval = -ENOMEM; |
| 145 | goto out; |
| 146 | } |
| 147 | buffer->bin_buffer_size = len; |
| 148 | |
| 149 | /* perform second read to fill buffer */ |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 150 | down_read(&frag->frag_sem); |
| 151 | if (!frag->frag_dead) |
| 152 | len = buffer->bin_attr->read(buffer->item, |
| 153 | buffer->bin_buffer, len); |
| 154 | else |
| 155 | len = -ENOENT; |
| 156 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 157 | if (len < 0) { |
| 158 | retval = len; |
| 159 | vfree(buffer->bin_buffer); |
| 160 | buffer->bin_buffer_size = 0; |
| 161 | buffer->bin_buffer = NULL; |
| 162 | goto out; |
| 163 | } |
| 164 | |
| 165 | buffer->needs_read_fill = 0; |
| 166 | } |
| 167 | |
Bart Van Assche | 420405e | 2021-07-13 10:49:26 -0700 | [diff] [blame] | 168 | if (iocb->ki_pos >= buffer->bin_buffer_size) |
| 169 | goto out; |
| 170 | retval = copy_to_iter(buffer->bin_buffer + iocb->ki_pos, |
| 171 | buffer->bin_buffer_size - iocb->ki_pos, to); |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 172 | iocb->ki_pos += retval; |
| 173 | if (retval == 0) |
| 174 | retval = -EFAULT; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 175 | out: |
| 176 | mutex_unlock(&buffer->mutex); |
| 177 | return retval; |
| 178 | } |
| 179 | |
Bart Van Assche | 769f526 | 2021-08-04 21:35:01 -0700 | [diff] [blame] | 180 | /* Fill @buffer with data coming from @from. */ |
| 181 | static int fill_write_buffer(struct configfs_buffer *buffer, |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 182 | struct iov_iter *from) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 183 | { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 184 | int copied; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 185 | |
| 186 | if (!buffer->page) |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 187 | buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 188 | if (!buffer->page) |
| 189 | return -ENOMEM; |
| 190 | |
Bart Van Assche | 769f526 | 2021-08-04 21:35:01 -0700 | [diff] [blame] | 191 | copied = copy_from_iter(buffer->page, SIMPLE_ATTR_SIZE - 1, from); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 192 | buffer->needs_read_fill = 1; |
Joel Becker | ff05d1c | 2007-01-23 17:00:45 -0800 | [diff] [blame] | 193 | /* if buf is assumed to contain a string, terminate it by \0, |
| 194 | * so e.g. sscanf() can scan the string easily */ |
Bart Van Assche | 769f526 | 2021-08-04 21:35:01 -0700 | [diff] [blame] | 195 | buffer->page[copied] = 0; |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 196 | return copied ? : -EFAULT; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 199 | static int |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 200 | 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] | 201 | { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 202 | struct configfs_fragment *frag = to_frag(file); |
| 203 | int res = -ENOENT; |
| 204 | |
| 205 | down_read(&frag->frag_sem); |
| 206 | if (!frag->frag_dead) |
| 207 | res = buffer->attr->store(buffer->item, buffer->page, count); |
| 208 | up_read(&frag->frag_sem); |
| 209 | return res; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
Christoph Hellwig | 44b9a00 | 2021-05-25 10:28:54 +0200 | [diff] [blame] | 213 | /* |
| 214 | * There is no easy way for us to know if userspace is only doing a partial |
| 215 | * write, so we don't support them. We expect the entire buffer to come on the |
| 216 | * first write. |
| 217 | * Hint: if you're writing a value, first read the file, modify only the value |
| 218 | * you're changing, then write entire buffer back. |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 219 | */ |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 220 | static ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 221 | { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 222 | struct file *file = iocb->ki_filp; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 223 | struct configfs_buffer *buffer = file->private_data; |
Bart Van Assche | 769f526 | 2021-08-04 21:35:01 -0700 | [diff] [blame] | 224 | int len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 225 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 226 | mutex_lock(&buffer->mutex); |
Bart Van Assche | 769f526 | 2021-08-04 21:35:01 -0700 | [diff] [blame] | 227 | len = fill_write_buffer(buffer, from); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 228 | if (len > 0) |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 229 | len = flush_write_buffer(file, buffer, len); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 230 | if (len > 0) |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 231 | iocb->ki_pos += len; |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 232 | mutex_unlock(&buffer->mutex); |
Joel Becker | 3d0f89b | 2006-01-25 13:31:07 -0800 | [diff] [blame] | 233 | return len; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 236 | static ssize_t configfs_bin_write_iter(struct kiocb *iocb, |
| 237 | struct iov_iter *from) |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 238 | { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 239 | struct file *file = iocb->ki_filp; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 240 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 241 | void *tbuf = NULL; |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 242 | size_t end_offset; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 243 | ssize_t len; |
| 244 | |
| 245 | mutex_lock(&buffer->mutex); |
| 246 | |
| 247 | /* we don't support switching read/write modes */ |
| 248 | if (buffer->read_in_progress) { |
| 249 | len = -ETXTBSY; |
| 250 | goto out; |
| 251 | } |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 252 | buffer->write_in_progress = true; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 253 | |
| 254 | /* buffer grows? */ |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 255 | end_offset = iocb->ki_pos + iov_iter_count(from); |
| 256 | if (end_offset > buffer->bin_buffer_size) { |
| 257 | if (buffer->cb_max_size && end_offset > buffer->cb_max_size) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 258 | len = -EFBIG; |
Phil Turnbull | 42857cf | 2016-09-15 12:20:12 -0400 | [diff] [blame] | 259 | goto out; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 260 | } |
| 261 | |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 262 | tbuf = vmalloc(end_offset); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 263 | if (tbuf == NULL) { |
| 264 | len = -ENOMEM; |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | /* copy old contents */ |
| 269 | if (buffer->bin_buffer) { |
| 270 | memcpy(tbuf, buffer->bin_buffer, |
| 271 | buffer->bin_buffer_size); |
| 272 | vfree(buffer->bin_buffer); |
| 273 | } |
| 274 | |
| 275 | /* clear the new area */ |
| 276 | memset(tbuf + buffer->bin_buffer_size, 0, |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 277 | end_offset - buffer->bin_buffer_size); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 278 | buffer->bin_buffer = tbuf; |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 279 | buffer->bin_buffer_size = end_offset; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 280 | } |
| 281 | |
Bart Van Assche | 420405e | 2021-07-13 10:49:26 -0700 | [diff] [blame] | 282 | len = copy_from_iter(buffer->bin_buffer + iocb->ki_pos, |
| 283 | buffer->bin_buffer_size - iocb->ki_pos, from); |
| 284 | iocb->ki_pos += len; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 285 | out: |
| 286 | mutex_unlock(&buffer->mutex); |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 287 | return len ? : -EFAULT; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 288 | } |
| 289 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 290 | 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] | 291 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 292 | struct dentry *dentry = file->f_path.dentry; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 293 | struct configfs_fragment *frag = to_frag(file); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 294 | struct configfs_attribute *attr; |
| 295 | struct configfs_buffer *buffer; |
| 296 | int error; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 297 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 298 | error = -ENOMEM; |
| 299 | buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL); |
| 300 | if (!buffer) |
| 301 | goto out; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 302 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 303 | error = -ENOENT; |
| 304 | down_read(&frag->frag_sem); |
| 305 | if (unlikely(frag->frag_dead)) |
| 306 | goto out_free_buffer; |
| 307 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 308 | error = -EINVAL; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 309 | buffer->item = to_item(dentry->d_parent); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 310 | if (!buffer->item) |
| 311 | goto out_free_buffer; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 312 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 313 | attr = to_attr(dentry); |
| 314 | if (!attr) |
Daiyue Zhang | 14fbbc8 | 2021-03-01 14:10:53 +0800 | [diff] [blame] | 315 | goto out_free_buffer; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 316 | |
| 317 | if (type & CONFIGFS_ITEM_BIN_ATTR) { |
| 318 | buffer->bin_attr = to_bin_attr(dentry); |
| 319 | buffer->cb_max_size = buffer->bin_attr->cb_max_size; |
| 320 | } else { |
| 321 | buffer->attr = attr; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 324 | buffer->owner = attr->ca_owner; |
| 325 | /* Grab the module reference for this attribute if we have one */ |
| 326 | error = -ENODEV; |
| 327 | if (!try_module_get(buffer->owner)) |
Daiyue Zhang | 14fbbc8 | 2021-03-01 14:10:53 +0800 | [diff] [blame] | 328 | goto out_free_buffer; |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 329 | |
| 330 | error = -EACCES; |
| 331 | if (!buffer->item->ci_type) |
| 332 | goto out_put_module; |
| 333 | |
| 334 | buffer->ops = buffer->item->ci_type->ct_item_ops; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 335 | |
| 336 | /* File needs write support. |
| 337 | * The inode's perms must say it's ok, |
| 338 | * and we must have a store method. |
| 339 | */ |
| 340 | if (file->f_mode & FMODE_WRITE) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 341 | if (!(inode->i_mode & S_IWUGO)) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 342 | goto out_put_module; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 343 | if ((type & CONFIGFS_ITEM_ATTR) && !attr->store) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 344 | goto out_put_module; |
| 345 | if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write) |
| 346 | goto out_put_module; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* File needs read support. |
| 350 | * The inode's perms must say it's ok, and we there |
| 351 | * must be a show method for it. |
| 352 | */ |
| 353 | if (file->f_mode & FMODE_READ) { |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 354 | if (!(inode->i_mode & S_IRUGO)) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 355 | goto out_put_module; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 356 | if ((type & CONFIGFS_ITEM_ATTR) && !attr->show) |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 357 | goto out_put_module; |
| 358 | if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read) |
| 359 | goto out_put_module; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Johannes Berg | 6d74892 | 2007-06-22 11:20:00 +0200 | [diff] [blame] | 362 | mutex_init(&buffer->mutex); |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 363 | buffer->needs_read_fill = 1; |
Thomas Meyer | 3f6928c | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 364 | buffer->read_in_progress = false; |
| 365 | buffer->write_in_progress = false; |
Chandra Seetharaman | 559c9ac | 2006-10-10 15:15:55 -0700 | [diff] [blame] | 366 | file->private_data = buffer; |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 367 | up_read(&frag->frag_sem); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 368 | return 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 369 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 370 | out_put_module: |
| 371 | module_put(buffer->owner); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 372 | out_free_buffer: |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 373 | up_read(&frag->frag_sem); |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 374 | kfree(buffer); |
| 375 | out: |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 376 | return error; |
| 377 | } |
| 378 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 379 | static int configfs_release(struct inode *inode, struct file *filp) |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 380 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 381 | struct configfs_buffer *buffer = filp->private_data; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 382 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 383 | module_put(buffer->owner); |
| 384 | if (buffer->page) |
| 385 | free_page((unsigned long)buffer->page); |
| 386 | mutex_destroy(&buffer->mutex); |
| 387 | kfree(buffer); |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 391 | static int configfs_open_file(struct inode *inode, struct file *filp) |
| 392 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 393 | return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int configfs_open_bin_file(struct inode *inode, struct file *filp) |
| 397 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 398 | return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 399 | } |
| 400 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 401 | static int configfs_release_bin_file(struct inode *inode, struct file *file) |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 402 | { |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 403 | struct configfs_buffer *buffer = file->private_data; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 404 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 405 | if (buffer->write_in_progress) { |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 406 | struct configfs_fragment *frag = to_frag(file); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 407 | |
Al Viro | b0841ee | 2019-08-31 09:43:43 +0200 | [diff] [blame] | 408 | down_read(&frag->frag_sem); |
| 409 | if (!frag->frag_dead) { |
| 410 | /* result of ->release() is ignored */ |
| 411 | buffer->bin_attr->write(buffer->item, |
| 412 | buffer->bin_buffer, |
| 413 | buffer->bin_buffer_size); |
| 414 | } |
| 415 | up_read(&frag->frag_sem); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 416 | } |
| 417 | |
Chung-Chiang Cheng | 3c252b0 | 2021-06-18 15:59:25 +0800 | [diff] [blame] | 418 | vfree(buffer->bin_buffer); |
Chung-Chiang Cheng | 3c252b0 | 2021-06-18 15:59:25 +0800 | [diff] [blame] | 419 | |
Al Viro | ff4dd08 | 2019-08-30 11:30:03 -0400 | [diff] [blame] | 420 | configfs_release(inode, file); |
| 421 | return 0; |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 425 | const struct file_operations configfs_file_operations = { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 426 | .read_iter = configfs_read_iter, |
| 427 | .write_iter = configfs_write_iter, |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 428 | .llseek = generic_file_llseek, |
| 429 | .open = configfs_open_file, |
| 430 | .release = configfs_release, |
| 431 | }; |
| 432 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 433 | const struct file_operations configfs_bin_file_operations = { |
Bart Van Assche | 7fe1e79 | 2021-05-25 10:30:21 +0200 | [diff] [blame] | 434 | .read_iter = configfs_bin_read_iter, |
| 435 | .write_iter = configfs_bin_write_iter, |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 436 | .llseek = NULL, /* bin file is not seekable */ |
| 437 | .open = configfs_open_bin_file, |
| 438 | .release = configfs_release_bin_file, |
| 439 | }; |
| 440 | |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 441 | /** |
| 442 | * configfs_create_file - create an attribute file for an item. |
| 443 | * @item: item we're creating for. |
| 444 | * @attr: atrribute descriptor. |
| 445 | */ |
| 446 | |
| 447 | int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr) |
| 448 | { |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 449 | struct dentry *dir = item->ci_dentry; |
| 450 | struct configfs_dirent *parent_sd = dir->d_fsdata; |
| 451 | umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG; |
| 452 | int error = 0; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 453 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 454 | inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL); |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 455 | error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode, |
Al Viro | 47320fb | 2019-08-25 19:56:13 -0400 | [diff] [blame] | 456 | CONFIGFS_ITEM_ATTR, parent_sd->s_frag); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 457 | inode_unlock(d_inode(dir)); |
Al Viro | 28444a2 | 2015-01-29 00:27:57 -0500 | [diff] [blame] | 458 | |
| 459 | return error; |
Joel Becker | 7063fbf | 2005-12-15 14:29:43 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 462 | /** |
| 463 | * configfs_create_bin_file - create a binary attribute file for an item. |
| 464 | * @item: item we're creating for. |
Bart Van Assche | dd33f1f | 2021-05-25 10:24:23 +0200 | [diff] [blame] | 465 | * @bin_attr: atrribute descriptor. |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 466 | */ |
| 467 | |
| 468 | int configfs_create_bin_file(struct config_item *item, |
| 469 | const struct configfs_bin_attribute *bin_attr) |
| 470 | { |
| 471 | struct dentry *dir = item->ci_dentry; |
| 472 | struct configfs_dirent *parent_sd = dir->d_fsdata; |
| 473 | umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG; |
| 474 | int error = 0; |
| 475 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 476 | inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 477 | error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode, |
Al Viro | 47320fb | 2019-08-25 19:56:13 -0400 | [diff] [blame] | 478 | CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 479 | inode_unlock(dir->d_inode); |
Pantelis Antoniou | 03607ac | 2015-10-22 23:30:04 +0300 | [diff] [blame] | 480 | |
| 481 | return error; |
| 482 | } |