1 ext4: refactor sysfs support code
3 Make the code more easily extensible as well as taking up less
6 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
8 fs/ext4/sysfs.c | 259 +++++++++++++++++++++++++++++++++++++++++------------------------------------------
9 1 file changed, 127 insertions(+), 132 deletions(-)
11 diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
12 index aeb90ca..1c3d32a 100644
16 static const char *proc_dirname = "fs/ext4";
17 static struct proc_dir_entry *ext4_proc_root;
21 + attr_delayed_allocation_blocks,
22 + attr_session_write_kbytes,
23 + attr_lifetime_write_kbytes,
24 + attr_reserved_clusters,
25 + attr_inode_readahead,
26 + attr_trigger_test_error,
29 + attr_pointer_atomic,
34 + ptr_ext4_sb_info_offset,
35 + ptr_ext4_super_block_offset,
39 struct attribute attr;
40 - ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
41 - ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
42 - const char *, size_t);
52 -static int parse_strtoull(const char *buf,
53 - unsigned long long max, unsigned long long *value)
57 - ret = kstrtoull(skip_spaces(buf), 0, value);
58 - if (!ret && *value > max)
63 -static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
64 - struct ext4_sb_info *sbi,
67 - return snprintf(buf, PAGE_SIZE, "%llu\n",
69 - percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
72 static ssize_t session_write_kbytes_show(struct ext4_attr *a,
73 struct ext4_sb_info *sbi, char *buf)
75 @@ -92,46 +90,6 @@ static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
79 -static ssize_t sbi_ui_show(struct ext4_attr *a,
80 - struct ext4_sb_info *sbi, char *buf)
82 - unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
84 - return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
87 -static ssize_t sbi_ui_store(struct ext4_attr *a,
88 - struct ext4_sb_info *sbi,
89 - const char *buf, size_t count)
91 - unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
95 - ret = kstrtoul(skip_spaces(buf), 0, &t);
102 -static ssize_t es_ui_show(struct ext4_attr *a,
103 - struct ext4_sb_info *sbi, char *buf)
106 - unsigned int *ui = (unsigned int *) (((char *) sbi->s_es) +
109 - return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
112 -static ssize_t reserved_clusters_show(struct ext4_attr *a,
113 - struct ext4_sb_info *sbi, char *buf)
115 - return snprintf(buf, PAGE_SIZE, "%llu\n",
116 - (unsigned long long) atomic64_read(&sbi->s_resv_clusters));
119 static ssize_t reserved_clusters_store(struct ext4_attr *a,
120 struct ext4_sb_info *sbi,
121 const char *buf, size_t count)
122 @@ -139,11 +97,10 @@ static ssize_t reserved_clusters_store(struct ext4_attr *a,
123 unsigned long long val;
124 ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
125 sbi->s_cluster_bits);
128 - if (parse_strtoull(buf, -1ULL, &val))
131 - if (val >= clusters)
132 + ret = kstrtoull(skip_spaces(buf), 0, &val);
133 + if (!ret || val >= clusters)
136 atomic64_set(&sbi->s_resv_clusters, val);
137 @@ -167,60 +124,51 @@ static ssize_t trigger_test_error(struct ext4_attr *a,
141 -static ssize_t sbi_deprecated_show(struct ext4_attr *a,
142 - struct ext4_sb_info *sbi, char *buf)
144 - return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
145 +#define EXT4_ATTR(_name,_mode,_id) \
146 +static struct ext4_attr ext4_attr_##_name = { \
147 + .attr = {.name = __stringify(_name), .mode = _mode }, \
148 + .attr_id = attr_##_id, \
151 -#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
152 +#define EXT4_ATTR_FUNC(_name,_mode) EXT4_ATTR(_name,_mode,_name)
154 +#define EXT4_ATTR_FEATURE(_name) EXT4_ATTR(_name, 0444, feature)
156 +#define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname) \
157 static struct ext4_attr ext4_attr_##_name = { \
158 .attr = {.name = __stringify(_name), .mode = _mode }, \
161 + .attr_id = attr_##_id, \
162 + .attr_ptr = ptr_##_struct##_offset, \
164 - .offset = offsetof(struct ext4_sb_info, _elname),\
165 + .offset = offsetof(struct _struct, _elname),\
169 -#define EXT4_ATTR_OFFSET_ES(_name,_mode,_show,_store,_elname) \
170 -static struct ext4_attr ext4_attr_##_name = { \
171 - .attr = {.name = __stringify(_name), .mode = _mode }, \
175 - .offset = offsetof(struct ext4_super_block, _elname), \
179 -#define EXT4_ATTR(name, mode, show, store) \
180 -static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
182 -#define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
183 -#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
184 -#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
185 +#define EXT4_RO_ATTR_ES_UI(_name,_elname) \
186 + EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
188 -#define EXT4_RO_ATTR_ES_UI(name, elname) \
189 - EXT4_ATTR_OFFSET_ES(name, 0444, es_ui_show, NULL, elname)
190 -#define EXT4_RW_ATTR_SBI_UI(name, elname) \
191 - EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
192 +#define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
193 + EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
195 -#define ATTR_LIST(name) &ext4_attr_##name.attr
196 -#define EXT4_DEPRECATED_ATTR(_name, _val) \
197 +#define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
198 static struct ext4_attr ext4_attr_##_name = { \
199 - .attr = {.name = __stringify(_name), .mode = 0444 }, \
200 - .show = sbi_deprecated_show, \
201 + .attr = {.name = __stringify(_name), .mode = _mode }, \
202 + .attr_id = attr_##_id, \
203 + .attr_ptr = ptr_explicit, \
205 - .deprecated_val = _val, \
206 + .explicit_ptr = _ptr, \
210 -EXT4_RO_ATTR(delayed_allocation_blocks);
211 -EXT4_RO_ATTR(session_write_kbytes);
212 -EXT4_RO_ATTR(lifetime_write_kbytes);
213 -EXT4_RW_ATTR(reserved_clusters);
214 -EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
215 - inode_readahead_blks_store, s_inode_readahead_blks);
216 +#define ATTR_LIST(name) &ext4_attr_##name.attr
218 +EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
219 +EXT4_ATTR_FUNC(session_write_kbytes, 0444);
220 +EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
221 +EXT4_ATTR_FUNC(reserved_clusters, 0644);
223 +EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
224 + ext4_sb_info, s_inode_readahead_blks);
225 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
226 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
227 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
228 @@ -228,9 +176,8 @@ EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
229 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
230 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
231 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
232 -EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
233 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
234 -EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
235 +EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
236 EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
237 EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
238 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
239 @@ -241,6 +188,9 @@ EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
240 EXT4_RO_ATTR_ES_UI(first_error_time, s_first_error_time);
241 EXT4_RO_ATTR_ES_UI(last_error_time, s_last_error_time);
243 +static unsigned int old_bump_val = 128;
244 +EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
246 static struct attribute *ext4_attrs[] = {
247 ATTR_LIST(delayed_allocation_blocks),
248 ATTR_LIST(session_write_kbytes),
249 @@ -270,10 +220,10 @@ static struct attribute *ext4_attrs[] = {
252 /* Features this copy of ext4 supports */
253 -EXT4_INFO_ATTR(lazy_itable_init);
254 -EXT4_INFO_ATTR(batched_discard);
255 -EXT4_INFO_ATTR(meta_bg_resize);
256 -EXT4_INFO_ATTR(encryption);
257 +EXT4_ATTR_FEATURE(lazy_itable_init);
258 +EXT4_ATTR_FEATURE(batched_discard);
259 +EXT4_ATTR_FEATURE(meta_bg_resize);
260 +EXT4_ATTR_FEATURE(encryption);
262 static struct attribute *ext4_feat_attrs[] = {
263 ATTR_LIST(lazy_itable_init),
264 @@ -283,14 +233,56 @@ static struct attribute *ext4_feat_attrs[] = {
268 +static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
270 + switch (a->attr_ptr) {
272 + return a->u.explicit_ptr;
273 + case ptr_ext4_sb_info_offset:
274 + return (void *) (((char *) sbi) + a->u.offset);
275 + case ptr_ext4_super_block_offset:
276 + return (void *) (((char *) sbi->s_es) + a->u.offset);
281 static ssize_t ext4_attr_show(struct kobject *kobj,
282 struct attribute *attr, char *buf)
284 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
286 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
287 + void *ptr = calc_ptr(a, sbi);
289 + switch (a->attr_id) {
290 + case attr_delayed_allocation_blocks:
291 + return snprintf(buf, PAGE_SIZE, "%llu\n",
292 + (s64) EXT4_C2B(sbi,
293 + percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
294 + case attr_session_write_kbytes:
295 + return session_write_kbytes_show(a, sbi, buf);
296 + case attr_lifetime_write_kbytes:
297 + return lifetime_write_kbytes_show(a, sbi, buf);
298 + case attr_reserved_clusters:
299 + return snprintf(buf, PAGE_SIZE, "%llu\n",
300 + (unsigned long long)
301 + atomic64_read(&sbi->s_resv_clusters));
302 + case attr_inode_readahead:
303 + case attr_pointer_ui:
306 + return snprintf(buf, PAGE_SIZE, "%u\n",
307 + *((unsigned int *) ptr));
308 + case attr_pointer_atomic:
311 + return snprintf(buf, PAGE_SIZE, "%d\n",
312 + atomic_read((atomic_t *) ptr));
314 + return snprintf(buf, PAGE_SIZE, "supported\n");
317 - return a->show ? a->show(a, sbi, buf) : 0;
321 static ssize_t ext4_attr_store(struct kobject *kobj,
322 @@ -300,8 +292,27 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
323 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
325 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
326 + void *ptr = calc_ptr(a, sbi);
330 - return a->store ? a->store(a, sbi, buf, len) : 0;
331 + switch (a->attr_id) {
332 + case attr_reserved_clusters:
333 + return reserved_clusters_store(a, sbi, buf, len);
334 + case attr_pointer_ui:
337 + ret = kstrtoul(skip_spaces(buf), 0, &t);
340 + *((unsigned int *) ptr) = t;
342 + case attr_inode_readahead:
343 + return inode_readahead_blks_store(a, sbi, buf, len);
344 + case attr_trigger_test_error:
345 + return trigger_test_error(a, sbi, buf, len);
350 static void ext4_sb_release(struct kobject *kobj)
351 @@ -330,25 +341,9 @@ static struct kset ext4_kset = {
352 .kobj = {.ktype = &ext4_ktype},
355 -static ssize_t ext4_feat_show(struct kobject *kobj,
356 - struct attribute *attr, char *buf)
358 - return snprintf(buf, PAGE_SIZE, "supported\n");
362 - * We can not use ext4_attr_show/store because it relies on the kobject
363 - * being embedded in the ext4_sb_info structure which is definitely not
364 - * true in this case.
366 -static const struct sysfs_ops ext4_feat_ops = {
367 - .show = ext4_feat_show,
371 static struct kobj_type ext4_feat_ktype = {
372 .default_attrs = ext4_feat_attrs,
373 - .sysfs_ops = &ext4_feat_ops,
374 + .sysfs_ops = &ext4_attr_ops,
377 static struct kobject ext4_feat = {