Skip to content

Commit 76d33bc

Browse files
committed
ext4: refactor sysfs support code
Make the code more easily extensible as well as taking up less compiled space. Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent b579901 commit 76d33bc

1 file changed

Lines changed: 128 additions & 133 deletions

File tree

‎fs/ext4/sysfs.c‎

Lines changed: 128 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,35 @@
1515
#include "ext4.h"
1616
#include "ext4_jbd2.h"
1717

18+
typedef enum {
19+
attr_noop,
20+
attr_delayed_allocation_blocks,
21+
attr_session_write_kbytes,
22+
attr_lifetime_write_kbytes,
23+
attr_reserved_clusters,
24+
attr_inode_readahead,
25+
attr_trigger_test_error,
26+
attr_feature,
27+
attr_pointer_ui,
28+
attr_pointer_atomic,
29+
} attr_id_t;
30+
31+
typedef enum {
32+
ptr_explicit,
33+
ptr_ext4_sb_info_offset,
34+
ptr_ext4_super_block_offset,
35+
} attr_ptr_t;
36+
1837
struct ext4_attr {
1938
struct attribute attr;
20-
ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
21-
ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
22-
const char *, size_t);
39+
short attr_id;
40+
short attr_ptr;
2341
union {
2442
int offset;
25-
int deprecated_val;
43+
void *explicit_ptr;
2644
} u;
2745
};
2846

29-
static int parse_strtoull(const char *buf,
30-
unsigned long long max, unsigned long long *value)
31-
{
32-
int ret;
33-
34-
ret = kstrtoull(skip_spaces(buf), 0, value);
35-
if (!ret && *value > max)
36-
ret = -EINVAL;
37-
return ret;
38-
}
39-
40-
static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
41-
struct ext4_sb_info *sbi,
42-
char *buf)
43-
{
44-
return snprintf(buf, PAGE_SIZE, "%llu\n",
45-
(s64) EXT4_C2B(sbi,
46-
percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
47-
}
48-
4947
static ssize_t session_write_kbytes_show(struct ext4_attr *a,
5048
struct ext4_sb_info *sbi, char *buf)
5149
{
@@ -89,58 +87,17 @@ static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
8987
return count;
9088
}
9189

92-
static ssize_t sbi_ui_show(struct ext4_attr *a,
93-
struct ext4_sb_info *sbi, char *buf)
94-
{
95-
unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
96-
97-
return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
98-
}
99-
100-
static ssize_t sbi_ui_store(struct ext4_attr *a,
101-
struct ext4_sb_info *sbi,
102-
const char *buf, size_t count)
103-
{
104-
unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
105-
unsigned long t;
106-
int ret;
107-
108-
ret = kstrtoul(skip_spaces(buf), 0, &t);
109-
if (ret)
110-
return ret;
111-
*ui = t;
112-
return count;
113-
}
114-
115-
static ssize_t es_ui_show(struct ext4_attr *a,
116-
struct ext4_sb_info *sbi, char *buf)
117-
{
118-
119-
unsigned int *ui = (unsigned int *) (((char *) sbi->s_es) +
120-
a->u.offset);
121-
122-
return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
123-
}
124-
125-
static ssize_t reserved_clusters_show(struct ext4_attr *a,
126-
struct ext4_sb_info *sbi, char *buf)
127-
{
128-
return snprintf(buf, PAGE_SIZE, "%llu\n",
129-
(unsigned long long) atomic64_read(&sbi->s_resv_clusters));
130-
}
131-
13290
static ssize_t reserved_clusters_store(struct ext4_attr *a,
13391
struct ext4_sb_info *sbi,
13492
const char *buf, size_t count)
13593
{
13694
unsigned long long val;
13795
ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
13896
sbi->s_cluster_bits);
97+
int ret;
13998

140-
if (parse_strtoull(buf, -1ULL, &val))
141-
return -EINVAL;
142-
143-
if (val >= clusters)
99+
ret = kstrtoull(skip_spaces(buf), 0, &val);
100+
if (!ret || val >= clusters)
144101
return -EINVAL;
145102

146103
atomic64_set(&sbi->s_resv_clusters, val);
@@ -164,70 +121,60 @@ static ssize_t trigger_test_error(struct ext4_attr *a,
164121
return count;
165122
}
166123

167-
static ssize_t sbi_deprecated_show(struct ext4_attr *a,
168-
struct ext4_sb_info *sbi, char *buf)
169-
{
170-
return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
124+
#define EXT4_ATTR(_name,_mode,_id) \
125+
static struct ext4_attr ext4_attr_##_name = { \
126+
.attr = {.name = __stringify(_name), .mode = _mode }, \
127+
.attr_id = attr_##_id, \
171128
}
172129

173-
#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
130+
#define EXT4_ATTR_FUNC(_name,_mode) EXT4_ATTR(_name,_mode,_name)
131+
132+
#define EXT4_ATTR_FEATURE(_name) EXT4_ATTR(_name, 0444, feature)
133+
134+
#define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname) \
174135
static struct ext4_attr ext4_attr_##_name = { \
175136
.attr = {.name = __stringify(_name), .mode = _mode }, \
176-
.show = _show, \
177-
.store = _store, \
137+
.attr_id = attr_##_id, \
138+
.attr_ptr = ptr_##_struct##_offset, \
178139
.u = { \
179-
.offset = offsetof(struct ext4_sb_info, _elname),\
140+
.offset = offsetof(struct _struct, _elname),\
180141
}, \
181142
}
182143

183-
#define EXT4_ATTR_OFFSET_ES(_name,_mode,_show,_store,_elname) \
184-
static struct ext4_attr ext4_attr_##_name = { \
185-
.attr = {.name = __stringify(_name), .mode = _mode }, \
186-
.show = _show, \
187-
.store = _store, \
188-
.u = { \
189-
.offset = offsetof(struct ext4_super_block, _elname), \
190-
}, \
191-
}
192-
193-
#define EXT4_ATTR(name, mode, show, store) \
194-
static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
195-
196-
#define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
197-
#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
198-
#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
144+
#define EXT4_RO_ATTR_ES_UI(_name,_elname) \
145+
EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
199146

200-
#define EXT4_RO_ATTR_ES_UI(name, elname) \
201-
EXT4_ATTR_OFFSET_ES(name, 0444, es_ui_show, NULL, elname)
202-
#define EXT4_RW_ATTR_SBI_UI(name, elname) \
203-
EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
147+
#define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
148+
EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
204149

205-
#define ATTR_LIST(name) &ext4_attr_##name.attr
206-
#define EXT4_DEPRECATED_ATTR(_name, _val) \
150+
#define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
207151
static struct ext4_attr ext4_attr_##_name = { \
208-
.attr = {.name = __stringify(_name), .mode = 0444 }, \
209-
.show = sbi_deprecated_show, \
152+
.attr = {.name = __stringify(_name), .mode = _mode }, \
153+
.attr_id = attr_##_id, \
154+
.attr_ptr = ptr_explicit, \
210155
.u = { \
211-
.deprecated_val = _val, \
156+
.explicit_ptr = _ptr, \
212157
}, \
213158
}
214159

215-
EXT4_RO_ATTR(delayed_allocation_blocks);
216-
EXT4_RO_ATTR(session_write_kbytes);
217-
EXT4_RO_ATTR(lifetime_write_kbytes);
218-
EXT4_RW_ATTR(reserved_clusters);
219-
EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
220-
inode_readahead_blks_store, s_inode_readahead_blks);
160+
#define ATTR_LIST(name) &ext4_attr_##name.attr
161+
162+
EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
163+
EXT4_ATTR_FUNC(session_write_kbytes, 0444);
164+
EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
165+
EXT4_ATTR_FUNC(reserved_clusters, 0644);
166+
167+
EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
168+
ext4_sb_info, s_inode_readahead_blks);
221169
EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
222170
EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
223171
EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
224172
EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
225173
EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
226174
EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
227175
EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
228-
EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
229176
EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
230-
EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
177+
EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
231178
EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
232179
EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
233180
EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
@@ -238,6 +185,9 @@ EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
238185
EXT4_RO_ATTR_ES_UI(first_error_time, s_first_error_time);
239186
EXT4_RO_ATTR_ES_UI(last_error_time, s_last_error_time);
240187

188+
static unsigned int old_bump_val = 128;
189+
EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
190+
241191
static struct attribute *ext4_attrs[] = {
242192
ATTR_LIST(delayed_allocation_blocks),
243193
ATTR_LIST(session_write_kbytes),
@@ -267,10 +217,10 @@ static struct attribute *ext4_attrs[] = {
267217
};
268218

269219
/* Features this copy of ext4 supports */
270-
EXT4_INFO_ATTR(lazy_itable_init);
271-
EXT4_INFO_ATTR(batched_discard);
272-
EXT4_INFO_ATTR(meta_bg_resize);
273-
EXT4_INFO_ATTR(encryption);
220+
EXT4_ATTR_FEATURE(lazy_itable_init);
221+
EXT4_ATTR_FEATURE(batched_discard);
222+
EXT4_ATTR_FEATURE(meta_bg_resize);
223+
EXT4_ATTR_FEATURE(encryption);
274224

275225
static struct attribute *ext4_feat_attrs[] = {
276226
ATTR_LIST(lazy_itable_init),
@@ -280,14 +230,56 @@ static struct attribute *ext4_feat_attrs[] = {
280230
NULL,
281231
};
282232

233+
static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
234+
{
235+
switch (a->attr_ptr) {
236+
case ptr_explicit:
237+
return a->u.explicit_ptr;
238+
case ptr_ext4_sb_info_offset:
239+
return (void *) (((char *) sbi) + a->u.offset);
240+
case ptr_ext4_super_block_offset:
241+
return (void *) (((char *) sbi->s_es) + a->u.offset);
242+
}
243+
return NULL;
244+
}
245+
283246
static ssize_t ext4_attr_show(struct kobject *kobj,
284247
struct attribute *attr, char *buf)
285248
{
286249
struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
287250
s_kobj);
288251
struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
289-
290-
return a->show ? a->show(a, sbi, buf) : 0;
252+
void *ptr = calc_ptr(a, sbi);
253+
254+
switch (a->attr_id) {
255+
case attr_delayed_allocation_blocks:
256+
return snprintf(buf, PAGE_SIZE, "%llu\n",
257+
(s64) EXT4_C2B(sbi,
258+
percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
259+
case attr_session_write_kbytes:
260+
return session_write_kbytes_show(a, sbi, buf);
261+
case attr_lifetime_write_kbytes:
262+
return lifetime_write_kbytes_show(a, sbi, buf);
263+
case attr_reserved_clusters:
264+
return snprintf(buf, PAGE_SIZE, "%llu\n",
265+
(unsigned long long)
266+
atomic64_read(&sbi->s_resv_clusters));
267+
case attr_inode_readahead:
268+
case attr_pointer_ui:
269+
if (!ptr)
270+
return 0;
271+
return snprintf(buf, PAGE_SIZE, "%u\n",
272+
*((unsigned int *) ptr));
273+
case attr_pointer_atomic:
274+
if (!ptr)
275+
return 0;
276+
return snprintf(buf, PAGE_SIZE, "%d\n",
277+
atomic_read((atomic_t *) ptr));
278+
case attr_feature:
279+
return snprintf(buf, PAGE_SIZE, "supported\n");
280+
}
281+
282+
return 0;
291283
}
292284

293285
static ssize_t ext4_attr_store(struct kobject *kobj,
@@ -297,8 +289,27 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
297289
struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
298290
s_kobj);
299291
struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
292+
void *ptr = calc_ptr(a, sbi);
293+
unsigned long t;
294+
int ret;
300295

301-
return a->store ? a->store(a, sbi, buf, len) : 0;
296+
switch (a->attr_id) {
297+
case attr_reserved_clusters:
298+
return reserved_clusters_store(a, sbi, buf, len);
299+
case attr_pointer_ui:
300+
if (!ptr)
301+
return 0;
302+
ret = kstrtoul(skip_spaces(buf), 0, &t);
303+
if (ret)
304+
return ret;
305+
*((unsigned int *) ptr) = t;
306+
return len;
307+
case attr_inode_readahead:
308+
return inode_readahead_blks_store(a, sbi, buf, len);
309+
case attr_trigger_test_error:
310+
return trigger_test_error(a, sbi, buf, len);
311+
}
312+
return 0;
302313
}
303314

304315
static void ext4_sb_release(struct kobject *kobj)
@@ -327,25 +338,9 @@ static struct kset ext4_kset = {
327338
.kobj = {.ktype = &ext4_ktype},
328339
};
329340

330-
static ssize_t ext4_feat_show(struct kobject *kobj,
331-
struct attribute *attr, char *buf)
332-
{
333-
return snprintf(buf, PAGE_SIZE, "supported\n");
334-
}
335-
336-
/*
337-
* We can not use ext4_attr_show/store because it relies on the kobject
338-
* being embedded in the ext4_sb_info structure which is definitely not
339-
* true in this case.
340-
*/
341-
static const struct sysfs_ops ext4_feat_ops = {
342-
.show = ext4_feat_show,
343-
.store = NULL,
344-
};
345-
346341
static struct kobj_type ext4_feat_ktype = {
347342
.default_attrs = ext4_feat_attrs,
348-
.sysfs_ops = &ext4_feat_ops,
343+
.sysfs_ops = &ext4_attr_ops,
349344
};
350345

351346
static struct kobject ext4_feat = {

0 commit comments

Comments
 (0)