blob: 61e4db4390a1f2878a4a3bcb9c7fabcd129309dd [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Becker7063fbf2005-12-15 14:29:43 -08002/* -*- 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 Becker7063fbf2005-12-15 14:29:43 -08007 * 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 Becker7063fbf2005-12-15 14:29:43 -080015#include <linux/slab.h>
Johannes Berg6d748922007-06-22 11:20:00 +020016#include <linux/mutex.h>
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030017#include <linux/vmalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080019
20#include <linux/configfs.h>
21#include "configfs_internal.h"
22
Joel Beckerb23cdde2007-06-22 13:07:02 -070023/*
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 Becker7063fbf2005-12-15 14:29:43 -080030
31struct configfs_buffer {
32 size_t count;
33 loff_t pos;
34 char * page;
35 struct configfs_item_operations * ops;
Johannes Berg6d748922007-06-22 11:20:00 +020036 struct mutex mutex;
Joel Becker7063fbf2005-12-15 14:29:43 -080037 int needs_read_fill;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +030038 bool read_in_progress;
39 bool write_in_progress;
40 char *bin_buffer;
41 int bin_buffer_size;
Joel Becker7063fbf2005-12-15 14:29:43 -080042};
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 */
55static 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 Becker7063fbf2005-12-15 14:29:43 -080059 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 Hellwig51798222015-10-03 15:32:59 +020067 count = attr->show(item, buffer->page);
Christoph Hellwig870823e2015-10-03 15:32:37 +020068
Joel Beckerb23cdde2007-06-22 13:07:02 -070069 BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
Tal Shorer3dc3afa2016-07-01 12:28:57 +030070 if (count >= 0) {
71 buffer->needs_read_fill = 0;
Joel Becker7063fbf2005-12-15 14:29:43 -080072 buffer->count = count;
Tal Shorer3dc3afa2016-07-01 12:28:57 +030073 } else
Joel Becker7063fbf2005-12-15 14:29:43 -080074 ret = count;
75 return ret;
76}
77
Joel Becker7063fbf2005-12-15 14:29:43 -080078/**
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
97static ssize_t
98configfs_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 Berg6d748922007-06-22 11:20:00 +0200103 mutex_lock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800104 if (buffer->needs_read_fill) {
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800105 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
Joel Becker7063fbf2005-12-15 14:29:43 -0800106 goto out;
107 }
Zach Brown4779efc2006-10-03 01:16:05 -0700108 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700109 __func__, count, *ppos, buffer->page);
Akinobu Mita92f4c702007-05-09 02:33:32 -0700110 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
111 buffer->count);
Joel Becker7063fbf2005-12-15 14:29:43 -0800112out:
Johannes Berg6d748922007-06-22 11:20:00 +0200113 mutex_unlock(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800114 return retval;
115}
116
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300117/**
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
137static ssize_t
138configfs_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 Meyer3f6928c2017-10-07 16:02:21 +0200155 buffer->read_in_progress = true;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300156
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);
193out:
194 mutex_unlock(&buffer->mutex);
195 return retval;
196}
197
Joel Becker7063fbf2005-12-15 14:29:43 -0800198
199/**
200 * fill_write_buffer - copy buffer from userspace.
201 * @buffer: data buffer for file.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800202 * @buf: data from user.
Joel Becker7063fbf2005-12-15 14:29:43 -0800203 * @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
209static int
210fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
211{
212 int error;
213
214 if (!buffer->page)
Joel Beckerff05d1c2007-01-23 17:00:45 -0800215 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
Joel Becker7063fbf2005-12-15 14:29:43 -0800216 if (!buffer->page)
217 return -ENOMEM;
218
Joel Beckerb23cdde2007-06-22 13:07:02 -0700219 if (count >= SIMPLE_ATTR_SIZE)
220 count = SIMPLE_ATTR_SIZE - 1;
Joel Becker7063fbf2005-12-15 14:29:43 -0800221 error = copy_from_user(buffer->page,buf,count);
222 buffer->needs_read_fill = 1;
Joel Beckerff05d1c2007-01-23 17:00:45 -0800223 /* 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 Becker7063fbf2005-12-15 14:29:43 -0800226 return error ? -EFAULT : count;
227}
228
229
230/**
231 * flush_write_buffer - push buffer to config_item.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800232 * @dentry: dentry to the attribute
Joel Becker7063fbf2005-12-15 14:29:43 -0800233 * @buffer: data buffer for file.
Joel Becker3d0f89b2006-01-25 13:31:07 -0800234 * @count: number of bytes
Joel Becker7063fbf2005-12-15 14:29:43 -0800235 *
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
241static int
242flush_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 Becker7063fbf2005-12-15 14:29:43 -0800246
Christoph Hellwig870823e2015-10-03 15:32:37 +0200247 return attr->store(item, buffer->page, count);
Joel Becker7063fbf2005-12-15 14:29:43 -0800248}
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
268static ssize_t
269configfs_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 Becker3d0f89b2006-01-25 13:31:07 -0800272 ssize_t len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800273
Johannes Berg6d748922007-06-22 11:20:00 +0200274 mutex_lock(&buffer->mutex);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800275 len = fill_write_buffer(buffer, buf, count);
276 if (len > 0)
Dan Carpenter71210642013-07-03 15:00:45 -0700277 len = flush_write_buffer(file->f_path.dentry, buffer, len);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800278 if (len > 0)
279 *ppos += len;
Johannes Berg6d748922007-06-22 11:20:00 +0200280 mutex_unlock(&buffer->mutex);
Joel Becker3d0f89b2006-01-25 13:31:07 -0800281 return len;
Joel Becker7063fbf2005-12-15 14:29:43 -0800282}
283
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300284/**
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
297static ssize_t
298configfs_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 Meyer3f6928c2017-10-07 16:02:21 +0200314 buffer->write_in_progress = true;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300315
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 Turnbull42857cf2016-09-15 12:20:12 -0400322 goto out;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300323 }
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 Antoniou03607ac2015-10-22 23:30:04 +0300347out:
348 mutex_unlock(&buffer->mutex);
349 return len;
350}
351
352static int check_perm(struct inode * inode, struct file * file, int type)
Joel Becker7063fbf2005-12-15 14:29:43 -0800353{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800354 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 Antoniou03607ac2015-10-22 23:30:04 +0300356 struct configfs_bin_attribute *bin_attr = NULL;
Joel Becker7063fbf2005-12-15 14:29:43 -0800357 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 Antoniou03607ac2015-10-22 23:30:04 +0300364 if (type & CONFIGFS_ITEM_BIN_ATTR)
365 bin_attr = to_bin_attr(file->f_path.dentry);
366
Joel Becker7063fbf2005-12-15 14:29:43 -0800367 /* 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 Antoniou03607ac2015-10-22 23:30:04 +0300383 if (!(inode->i_mode & S_IWUGO))
Joel Becker7063fbf2005-12-15 14:29:43 -0800384 goto Eaccess;
385
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300386 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 Becker7063fbf2005-12-15 14:29:43 -0800391 }
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 Antoniou03607ac2015-10-22 23:30:04 +0300398 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 Becker7063fbf2005-12-15 14:29:43 -0800405 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 Issarisf8314dc2006-09-27 01:49:37 -0700411 buffer = kzalloc(sizeof(struct configfs_buffer),GFP_KERNEL);
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700412 if (!buffer) {
Joel Becker7063fbf2005-12-15 14:29:43 -0800413 error = -ENOMEM;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700414 goto Enomem;
415 }
Johannes Berg6d748922007-06-22 11:20:00 +0200416 mutex_init(&buffer->mutex);
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700417 buffer->needs_read_fill = 1;
Thomas Meyer3f6928c2017-10-07 16:02:21 +0200418 buffer->read_in_progress = false;
419 buffer->write_in_progress = false;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700420 buffer->ops = ops;
421 file->private_data = buffer;
Joel Becker7063fbf2005-12-15 14:29:43 -0800422 goto Done;
423
424 Einval:
425 error = -EINVAL;
426 goto Done;
427 Eaccess:
428 error = -EACCES;
Chandra Seetharaman559c9ac2006-10-10 15:15:55 -0700429 Enomem:
Joel Becker7063fbf2005-12-15 14:29:43 -0800430 module_put(attr->ca_owner);
431 Done:
432 if (error && item)
433 config_item_put(item);
434 return error;
435}
436
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300437static int configfs_release(struct inode *inode, struct file *filp)
Joel Becker7063fbf2005-12-15 14:29:43 -0800438{
Josef "Jeff" Sipek867fa492006-12-08 02:36:47 -0800439 struct config_item * item = to_item(filp->f_path.dentry->d_parent);
440 struct configfs_attribute * attr = to_attr(filp->f_path.dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800441 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 Berg6d748922007-06-22 11:20:00 +0200452 mutex_destroy(&buffer->mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800453 kfree(buffer);
454 }
455 return 0;
456}
457
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300458static int configfs_open_file(struct inode *inode, struct file *filp)
459{
460 return check_perm(inode, filp, CONFIGFS_ITEM_ATTR);
461}
462
463static int configfs_open_bin_file(struct inode *inode, struct file *filp)
464{
465 return check_perm(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
466}
467
468static 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 Meyer3f6928c2017-10-07 16:02:21 +0200477 buffer->read_in_progress = false;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300478
479 if (buffer->write_in_progress) {
Thomas Meyer3f6928c2017-10-07 16:02:21 +0200480 buffer->write_in_progress = false;
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300481
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 Ven4b6f5d22006-03-28 01:56:42 -0800499const struct file_operations configfs_file_operations = {
Joel Becker7063fbf2005-12-15 14:29:43 -0800500 .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 Antoniou03607ac2015-10-22 23:30:04 +0300507const 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 Becker7063fbf2005-12-15 14:29:43 -0800515/**
516 * configfs_create_file - create an attribute file for an item.
517 * @item: item we're creating for.
518 * @attr: atrribute descriptor.
519 */
520
521int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
522{
Al Viro28444a22015-01-29 00:27:57 -0500523 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 Becker7063fbf2005-12-15 14:29:43 -0800527
Al Viro59551022016-01-22 15:40:57 -0500528 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
Al Viro28444a22015-01-29 00:27:57 -0500529 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
530 CONFIGFS_ITEM_ATTR);
Al Viro59551022016-01-22 15:40:57 -0500531 inode_unlock(d_inode(dir));
Al Viro28444a22015-01-29 00:27:57 -0500532
533 return error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800534}
535
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300536/**
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
542int 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 Viro59551022016-01-22 15:40:57 -0500550 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300551 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
552 CONFIGFS_ITEM_BIN_ATTR);
Al Viro59551022016-01-22 15:40:57 -0500553 inode_unlock(dir->d_inode);
Pantelis Antoniou03607ac2015-10-22 23:30:04 +0300554
555 return error;
556}