blob: 5a0be9985bae1e7184a12a956581b3a2b6bf300d [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Joel Becker7063fbf2005-12-15 14:29:43 -08003 * file.c - operations for regular (text) files.
4 *
Joel Becker7063fbf2005-12-15 14:29:43 -08005 * 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 Becker7063fbf2005-12-15 14:29:43 -080013#include <linux/slab.h>
Johannes Berg6d748922007-06-22 11:20:00 +020014#include <linux/mutex.h>
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030015#include <linux/vmalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080016#include <linux/uaccess.h>
Bart Van Assche7fe1e792021-05-25 10:30:21 +020017#include <linux/uio.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080018#include <linux/configfs.h>
19#include "configfs_internal.h"
20
Joel Beckerb23cdde2007-06-22 13:07:02 -070021/*
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 Becker7063fbf2005-12-15 14:29:43 -080028
29struct configfs_buffer {
30 size_t count;
31 loff_t pos;
32 char * page;
33 struct configfs_item_operations * ops;
Johannes Berg6d748922007-06-22 11:20:00 +020034 struct mutex mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -080035 int needs_read_fill;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030036 bool read_in_progress;
37 bool write_in_progress;
38 char *bin_buffer;
39 int bin_buffer_size;
Al Viroff4dd082019-08-30 11:30:03 -040040 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 Becker7063fbf2005-12-15 14:29:43 -080047};
48
Al Virob0841ee2019-08-31 09:43:43 +020049static inline struct configfs_fragment *to_frag(struct file *file)
Joel Becker7063fbf2005-12-15 14:29:43 -080050{
Al Virob0841ee2019-08-31 09:43:43 +020051 struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
52
53 return sd->s_frag;
54}
55
56static 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 Becker7063fbf2005-12-15 14:29:43 -080060
61 if (!buffer->page)
62 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
63 if (!buffer->page)
64 return -ENOMEM;
65
Al Virob0841ee2019-08-31 09:43:43 +020066 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 Viroff4dd082019-08-30 11:30:03 -040071 if (count < 0)
72 return count;
73 if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
74 return -EIO;
Al Viroff4dd082019-08-30 11:30:03 -040075 buffer->needs_read_fill = 0;
76 buffer->count = count;
77 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -080078}
79
Bart Van Assche7fe1e792021-05-25 10:30:21 +020080static ssize_t configfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
Joel Becker7063fbf2005-12-15 14:29:43 -080081{
Bart Van Assche7fe1e792021-05-25 10:30:21 +020082 struct file *file = iocb->ki_filp;
Al Viroff4dd082019-08-30 11:30:03 -040083 struct configfs_buffer *buffer = file->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -080084 ssize_t retval = 0;
85
Johannes Berg6d748922007-06-22 11:20:00 +020086 mutex_lock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -080087 if (buffer->needs_read_fill) {
Al Virob0841ee2019-08-31 09:43:43 +020088 retval = fill_read_buffer(file, buffer);
Al Viroff4dd082019-08-30 11:30:03 -040089 if (retval)
Joel Becker7063fbf2005-12-15 14:29:43 -080090 goto out;
91 }
Bart Van Assche7fe1e792021-05-25 10:30:21 +020092 pr_debug("%s: count = %zd, pos = %lld, buf = %s\n",
93 __func__, iov_iter_count(to), iocb->ki_pos, buffer->page);
Bart Van Assche420405e2021-07-13 10:49:26 -070094 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 Assche7fe1e792021-05-25 10:30:21 +020098 iocb->ki_pos += retval;
99 if (retval == 0)
100 retval = -EFAULT;
Joel Becker7063fbf2005-12-15 14:29:43 -0800101out:
Johannes Berg6d748922007-06-22 11:20:00 +0200102 mutex_unlock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800103 return retval;
104}
105
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200106static ssize_t configfs_bin_read_iter(struct kiocb *iocb, struct iov_iter *to)
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300107{
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200108 struct file *file = iocb->ki_filp;
Al Virob0841ee2019-08-31 09:43:43 +0200109 struct configfs_fragment *frag = to_frag(file);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300110 struct configfs_buffer *buffer = file->private_data;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300111 ssize_t retval = 0;
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200112 ssize_t len;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300113
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 Meyer3f6928c2017-10-07 16:02:21 +0200121 buffer->read_in_progress = true;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300122
123 if (buffer->needs_read_fill) {
124 /* perform first read with buf == NULL to get extent */
Al Virob0841ee2019-08-31 09:43:43 +0200125 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 Antoniou03607ac2015-10-22 23:30:04 +0300131 if (len <= 0) {
132 retval = len;
133 goto out;
134 }
135
136 /* do not exceed the maximum value */
Al Viroff4dd082019-08-30 11:30:03 -0400137 if (buffer->cb_max_size && len > buffer->cb_max_size) {
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300138 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 Virob0841ee2019-08-31 09:43:43 +0200150 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 Antoniou03607ac2015-10-22 23:30:04 +0300157 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 Assche420405e2021-07-13 10:49:26 -0700168 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 Assche7fe1e792021-05-25 10:30:21 +0200172 iocb->ki_pos += retval;
173 if (retval == 0)
174 retval = -EFAULT;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300175out:
176 mutex_unlock(&buffer->mutex);
177 return retval;
178}
179
Bart Van Assche420405e2021-07-13 10:49:26 -0700180/* Fill [buffer, buffer + pos) with data coming from @from. */
181static int fill_write_buffer(struct configfs_buffer *buffer, loff_t pos,
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200182 struct iov_iter *from)
Joel Becker7063fbf2005-12-15 14:29:43 -0800183{
Bart Van Assche420405e2021-07-13 10:49:26 -0700184 loff_t to_copy;
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200185 int copied;
Bart Van Assche420405e2021-07-13 10:49:26 -0700186 u8 *to;
Joel Becker7063fbf2005-12-15 14:29:43 -0800187
188 if (!buffer->page)
Joel Beckerff05d1c2007-01-23 17:00:45 -0800189 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
Joel Becker7063fbf2005-12-15 14:29:43 -0800190 if (!buffer->page)
191 return -ENOMEM;
192
Bart Van Assche420405e2021-07-13 10:49:26 -0700193 to_copy = SIMPLE_ATTR_SIZE - 1 - pos;
194 if (to_copy <= 0)
195 return 0;
196 to = buffer->page + pos;
197 copied = copy_from_iter(to, to_copy, from);
Joel Becker7063fbf2005-12-15 14:29:43 -0800198 buffer->needs_read_fill = 1;
Joel Beckerff05d1c2007-01-23 17:00:45 -0800199 /* if buf is assumed to contain a string, terminate it by \0,
200 * so e.g. sscanf() can scan the string easily */
Bart Van Assche420405e2021-07-13 10:49:26 -0700201 to[copied] = 0;
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200202 return copied ? : -EFAULT;
Joel Becker7063fbf2005-12-15 14:29:43 -0800203}
204
Joel Becker7063fbf2005-12-15 14:29:43 -0800205static int
Al Virob0841ee2019-08-31 09:43:43 +0200206flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
Joel Becker7063fbf2005-12-15 14:29:43 -0800207{
Al Virob0841ee2019-08-31 09:43:43 +0200208 struct configfs_fragment *frag = to_frag(file);
209 int res = -ENOENT;
210
211 down_read(&frag->frag_sem);
212 if (!frag->frag_dead)
213 res = buffer->attr->store(buffer->item, buffer->page, count);
214 up_read(&frag->frag_sem);
215 return res;
Joel Becker7063fbf2005-12-15 14:29:43 -0800216}
217
218
Christoph Hellwig44b9a002021-05-25 10:28:54 +0200219/*
220 * There is no easy way for us to know if userspace is only doing a partial
221 * write, so we don't support them. We expect the entire buffer to come on the
222 * first write.
223 * Hint: if you're writing a value, first read the file, modify only the value
224 * you're changing, then write entire buffer back.
Joel Becker7063fbf2005-12-15 14:29:43 -0800225 */
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200226static ssize_t configfs_write_iter(struct kiocb *iocb, struct iov_iter *from)
Joel Becker7063fbf2005-12-15 14:29:43 -0800227{
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200228 struct file *file = iocb->ki_filp;
Al Viroff4dd082019-08-30 11:30:03 -0400229 struct configfs_buffer *buffer = file->private_data;
Joel Becker3d0f89b2006-01-25 13:31:07 -0800230 ssize_t len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800231
Johannes Berg6d748922007-06-22 11:20:00 +0200232 mutex_lock(&buffer->mutex);
Bart Van Assche420405e2021-07-13 10:49:26 -0700233 len = fill_write_buffer(buffer, iocb->ki_pos, from);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800234 if (len > 0)
Al Virob0841ee2019-08-31 09:43:43 +0200235 len = flush_write_buffer(file, buffer, len);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800236 if (len > 0)
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200237 iocb->ki_pos += len;
Johannes Berg6d748922007-06-22 11:20:00 +0200238 mutex_unlock(&buffer->mutex);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800239 return len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800240}
241
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200242static ssize_t configfs_bin_write_iter(struct kiocb *iocb,
243 struct iov_iter *from)
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300244{
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200245 struct file *file = iocb->ki_filp;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300246 struct configfs_buffer *buffer = file->private_data;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300247 void *tbuf = NULL;
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200248 size_t end_offset;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300249 ssize_t len;
250
251 mutex_lock(&buffer->mutex);
252
253 /* we don't support switching read/write modes */
254 if (buffer->read_in_progress) {
255 len = -ETXTBSY;
256 goto out;
257 }
Thomas Meyer3f6928c2017-10-07 16:02:21 +0200258 buffer->write_in_progress = true;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300259
260 /* buffer grows? */
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200261 end_offset = iocb->ki_pos + iov_iter_count(from);
262 if (end_offset > buffer->bin_buffer_size) {
263 if (buffer->cb_max_size && end_offset > buffer->cb_max_size) {
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300264 len = -EFBIG;
Phil Turnbull42857cf2016-09-15 12:20:12 -0400265 goto out;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300266 }
267
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200268 tbuf = vmalloc(end_offset);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300269 if (tbuf == NULL) {
270 len = -ENOMEM;
271 goto out;
272 }
273
274 /* copy old contents */
275 if (buffer->bin_buffer) {
276 memcpy(tbuf, buffer->bin_buffer,
277 buffer->bin_buffer_size);
278 vfree(buffer->bin_buffer);
279 }
280
281 /* clear the new area */
282 memset(tbuf + buffer->bin_buffer_size, 0,
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200283 end_offset - buffer->bin_buffer_size);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300284 buffer->bin_buffer = tbuf;
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200285 buffer->bin_buffer_size = end_offset;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300286 }
287
Bart Van Assche420405e2021-07-13 10:49:26 -0700288 len = copy_from_iter(buffer->bin_buffer + iocb->ki_pos,
289 buffer->bin_buffer_size - iocb->ki_pos, from);
290 iocb->ki_pos += len;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300291out:
292 mutex_unlock(&buffer->mutex);
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200293 return len ? : -EFAULT;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300294}
295
Al Viroff4dd082019-08-30 11:30:03 -0400296static int __configfs_open_file(struct inode *inode, struct file *file, int type)
Joel Becker7063fbf2005-12-15 14:29:43 -0800297{
Al Viroff4dd082019-08-30 11:30:03 -0400298 struct dentry *dentry = file->f_path.dentry;
Al Virob0841ee2019-08-31 09:43:43 +0200299 struct configfs_fragment *frag = to_frag(file);
Al Viroff4dd082019-08-30 11:30:03 -0400300 struct configfs_attribute *attr;
301 struct configfs_buffer *buffer;
302 int error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800303
Al Viroff4dd082019-08-30 11:30:03 -0400304 error = -ENOMEM;
305 buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
306 if (!buffer)
307 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -0800308
Al Virob0841ee2019-08-31 09:43:43 +0200309 error = -ENOENT;
310 down_read(&frag->frag_sem);
311 if (unlikely(frag->frag_dead))
312 goto out_free_buffer;
313
Al Viroff4dd082019-08-30 11:30:03 -0400314 error = -EINVAL;
Al Virob0841ee2019-08-31 09:43:43 +0200315 buffer->item = to_item(dentry->d_parent);
Al Viroff4dd082019-08-30 11:30:03 -0400316 if (!buffer->item)
317 goto out_free_buffer;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300318
Al Viroff4dd082019-08-30 11:30:03 -0400319 attr = to_attr(dentry);
320 if (!attr)
Daiyue Zhang14fbbc82021-03-01 14:10:53 +0800321 goto out_free_buffer;
Al Viroff4dd082019-08-30 11:30:03 -0400322
323 if (type & CONFIGFS_ITEM_BIN_ATTR) {
324 buffer->bin_attr = to_bin_attr(dentry);
325 buffer->cb_max_size = buffer->bin_attr->cb_max_size;
326 } else {
327 buffer->attr = attr;
Joel Becker7063fbf2005-12-15 14:29:43 -0800328 }
329
Al Viroff4dd082019-08-30 11:30:03 -0400330 buffer->owner = attr->ca_owner;
331 /* Grab the module reference for this attribute if we have one */
332 error = -ENODEV;
333 if (!try_module_get(buffer->owner))
Daiyue Zhang14fbbc82021-03-01 14:10:53 +0800334 goto out_free_buffer;
Al Viroff4dd082019-08-30 11:30:03 -0400335
336 error = -EACCES;
337 if (!buffer->item->ci_type)
338 goto out_put_module;
339
340 buffer->ops = buffer->item->ci_type->ct_item_ops;
Joel Becker7063fbf2005-12-15 14:29:43 -0800341
342 /* File needs write support.
343 * The inode's perms must say it's ok,
344 * and we must have a store method.
345 */
346 if (file->f_mode & FMODE_WRITE) {
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300347 if (!(inode->i_mode & S_IWUGO))
Al Viroff4dd082019-08-30 11:30:03 -0400348 goto out_put_module;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300349 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
Al Viroff4dd082019-08-30 11:30:03 -0400350 goto out_put_module;
351 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
352 goto out_put_module;
Joel Becker7063fbf2005-12-15 14:29:43 -0800353 }
354
355 /* File needs read support.
356 * The inode's perms must say it's ok, and we there
357 * must be a show method for it.
358 */
359 if (file->f_mode & FMODE_READ) {
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300360 if (!(inode->i_mode & S_IRUGO))
Al Viroff4dd082019-08-30 11:30:03 -0400361 goto out_put_module;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300362 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
Al Viroff4dd082019-08-30 11:30:03 -0400363 goto out_put_module;
364 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
365 goto out_put_module;
Joel Becker7063fbf2005-12-15 14:29:43 -0800366 }
367
Johannes Berg6d748922007-06-22 11:20:00 +0200368 mutex_init(&buffer->mutex);
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700369 buffer->needs_read_fill = 1;
Thomas Meyer3f6928c2017-10-07 16:02:21 +0200370 buffer->read_in_progress = false;
371 buffer->write_in_progress = false;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700372 file->private_data = buffer;
Al Virob0841ee2019-08-31 09:43:43 +0200373 up_read(&frag->frag_sem);
Al Viroff4dd082019-08-30 11:30:03 -0400374 return 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800375
Al Viroff4dd082019-08-30 11:30:03 -0400376out_put_module:
377 module_put(buffer->owner);
Al Viroff4dd082019-08-30 11:30:03 -0400378out_free_buffer:
Al Virob0841ee2019-08-31 09:43:43 +0200379 up_read(&frag->frag_sem);
Al Viroff4dd082019-08-30 11:30:03 -0400380 kfree(buffer);
381out:
Joel Becker7063fbf2005-12-15 14:29:43 -0800382 return error;
383}
384
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300385static int configfs_release(struct inode *inode, struct file *filp)
Joel Becker7063fbf2005-12-15 14:29:43 -0800386{
Al Viroff4dd082019-08-30 11:30:03 -0400387 struct configfs_buffer *buffer = filp->private_data;
Joel Becker7063fbf2005-12-15 14:29:43 -0800388
Al Viroff4dd082019-08-30 11:30:03 -0400389 module_put(buffer->owner);
390 if (buffer->page)
391 free_page((unsigned long)buffer->page);
392 mutex_destroy(&buffer->mutex);
393 kfree(buffer);
Joel Becker7063fbf2005-12-15 14:29:43 -0800394 return 0;
395}
396
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300397static int configfs_open_file(struct inode *inode, struct file *filp)
398{
Al Viroff4dd082019-08-30 11:30:03 -0400399 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300400}
401
402static int configfs_open_bin_file(struct inode *inode, struct file *filp)
403{
Al Viroff4dd082019-08-30 11:30:03 -0400404 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300405}
406
Al Viroff4dd082019-08-30 11:30:03 -0400407static int configfs_release_bin_file(struct inode *inode, struct file *file)
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300408{
Al Viroff4dd082019-08-30 11:30:03 -0400409 struct configfs_buffer *buffer = file->private_data;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300410
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300411 if (buffer->write_in_progress) {
Al Virob0841ee2019-08-31 09:43:43 +0200412 struct configfs_fragment *frag = to_frag(file);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300413
Al Virob0841ee2019-08-31 09:43:43 +0200414 down_read(&frag->frag_sem);
415 if (!frag->frag_dead) {
416 /* result of ->release() is ignored */
417 buffer->bin_attr->write(buffer->item,
418 buffer->bin_buffer,
419 buffer->bin_buffer_size);
420 }
421 up_read(&frag->frag_sem);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300422 }
423
Chung-Chiang Cheng3c252b02021-06-18 15:59:25 +0800424 vfree(buffer->bin_buffer);
Chung-Chiang Cheng3c252b02021-06-18 15:59:25 +0800425
Al Viroff4dd082019-08-30 11:30:03 -0400426 configfs_release(inode, file);
427 return 0;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300428}
429
430
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800431const struct file_operations configfs_file_operations = {
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200432 .read_iter = configfs_read_iter,
433 .write_iter = configfs_write_iter,
Joel Becker7063fbf2005-12-15 14:29:43 -0800434 .llseek = generic_file_llseek,
435 .open = configfs_open_file,
436 .release = configfs_release,
437};
438
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300439const struct file_operations configfs_bin_file_operations = {
Bart Van Assche7fe1e792021-05-25 10:30:21 +0200440 .read_iter = configfs_bin_read_iter,
441 .write_iter = configfs_bin_write_iter,
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300442 .llseek = NULL, /* bin file is not seekable */
443 .open = configfs_open_bin_file,
444 .release = configfs_release_bin_file,
445};
446
Joel Becker7063fbf2005-12-15 14:29:43 -0800447/**
448 * configfs_create_file - create an attribute file for an item.
449 * @item: item we're creating for.
450 * @attr: atrribute descriptor.
451 */
452
453int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
454{
Al Viro28444a22015-01-29 00:27:57 -0500455 struct dentry *dir = item->ci_dentry;
456 struct configfs_dirent *parent_sd = dir->d_fsdata;
457 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
458 int error = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -0800459
Al Viro59551022016-01-22 15:40:57 -0500460 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
Al Viro28444a22015-01-29 00:27:57 -0500461 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
Al Viro47320fb2019-08-25 19:56:13 -0400462 CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
Al Viro59551022016-01-22 15:40:57 -0500463 inode_unlock(d_inode(dir));
Al Viro28444a22015-01-29 00:27:57 -0500464
465 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800466}
467
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300468/**
469 * configfs_create_bin_file - create a binary attribute file for an item.
470 * @item: item we're creating for.
Bart Van Asschedd33f1f2021-05-25 10:24:23 +0200471 * @bin_attr: atrribute descriptor.
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300472 */
473
474int configfs_create_bin_file(struct config_item *item,
475 const struct configfs_bin_attribute *bin_attr)
476{
477 struct dentry *dir = item->ci_dentry;
478 struct configfs_dirent *parent_sd = dir->d_fsdata;
479 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
480 int error = 0;
481
Al Viro59551022016-01-22 15:40:57 -0500482 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300483 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
Al Viro47320fb2019-08-25 19:56:13 -0400484 CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
Al Viro59551022016-01-22 15:40:57 -0500485 inode_unlock(dir->d_inode);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300486
487 return error;
488}