Skip to content

Commit 870823e

Browse files
Christoph HellwigNicholas Bellinger
authored andcommitted
configfs: add show and store methods to struct configfs_attribute
Add methods to struct configfs_attribute to directly show and store attributes without adding boilerplate code to every user. In addition to the methods this also adds 3 helper macros to define read/write, read-only and write-only attributes with a single line of code. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Nicholas Bellinger <nab@linux-iscsi.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 6ff33f3 commit 870823e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

fs/configfs/file.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ static int fill_read_buffer(struct dentry * dentry, struct configfs_buffer * buf
7474
if (!buffer->page)
7575
return -ENOMEM;
7676

77-
count = ops->show_attribute(item,attr,buffer->page);
77+
if (ops->show_attribute)
78+
count = ops->show_attribute(item, attr, buffer->page);
79+
else
80+
count = attr->show(item, buffer->page);
81+
7882
buffer->needs_read_fill = 0;
7983
BUG_ON(count > (ssize_t)SIMPLE_ATTR_SIZE);
8084
if (count >= 0)
@@ -173,7 +177,9 @@ flush_write_buffer(struct dentry * dentry, struct configfs_buffer * buffer, size
173177
struct config_item * item = to_item(dentry->d_parent);
174178
struct configfs_item_operations * ops = buffer->ops;
175179

176-
return ops->store_attribute(item,attr,buffer->page,count);
180+
if (ops->store_attribute)
181+
return ops->store_attribute(item, attr, buffer->page, count);
182+
return attr->store(item, buffer->page, count);
177183
}
178184

179185

@@ -237,8 +243,8 @@ static int check_perm(struct inode * inode, struct file * file)
237243
* and we must have a store method.
238244
*/
239245
if (file->f_mode & FMODE_WRITE) {
240-
241-
if (!(inode->i_mode & S_IWUGO) || !ops->store_attribute)
246+
if (!(inode->i_mode & S_IWUGO) ||
247+
(!ops->store_attribute && !attr->store))
242248
goto Eaccess;
243249

244250
}
@@ -248,7 +254,8 @@ static int check_perm(struct inode * inode, struct file * file)
248254
* must be a show method for it.
249255
*/
250256
if (file->f_mode & FMODE_READ) {
251-
if (!(inode->i_mode & S_IRUGO) || !ops->show_attribute)
257+
if (!(inode->i_mode & S_IRUGO) ||
258+
(!ops->show_attribute && !attr->show))
252259
goto Eaccess;
253260
}
254261

include/linux/configfs.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,35 @@ struct configfs_attribute {
125125
const char *ca_name;
126126
struct module *ca_owner;
127127
umode_t ca_mode;
128+
ssize_t (*show)(struct config_item *, char *);
129+
ssize_t (*store)(struct config_item *, const char *, size_t);
128130
};
129131

132+
#define CONFIGFS_ATTR(_pfx, _name) \
133+
static struct configfs_attribute _pfx##attr_##_name = { \
134+
.ca_name = __stringify(_name), \
135+
.ca_mode = S_IRUGO | S_IWUSR, \
136+
.ca_owner = THIS_MODULE, \
137+
.show = _pfx##_name##_show, \
138+
.store = _pfx##_name##_store, \
139+
}
140+
141+
#define CONFIGFS_ATTR_RO(_pfx, _name) \
142+
static struct configfs_attribute _pfx##attr_##_name = { \
143+
.ca_name = __stringify(_name), \
144+
.ca_mode = S_IRUGO, \
145+
.ca_owner = THIS_MODULE, \
146+
.show = _pfx##_name##_show, \
147+
}
148+
149+
#define CONFIGFS_ATTR_WO(_pfx, _name) \
150+
static struct configfs_attribute _pfx##attr_##_name = { \
151+
.ca_name = __stringify(_name), \
152+
.ca_mode = S_IWUSR, \
153+
.ca_owner = THIS_MODULE, \
154+
.store = _pfx##_name##_store, \
155+
}
156+
130157
/*
131158
* Users often need to create attribute structures for their configurable
132159
* attributes, containing a configfs_attribute member and function pointers

0 commit comments

Comments
 (0)