1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/sysfs.c
5 * Copyright (C) 1992, 1993, 1994, 1995
11 #include <linux/time.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/proc_fs.h>
18 #include "ext4_jbd2.h"
22 attr_delayed_allocation_blocks,
23 attr_session_write_kbytes,
24 attr_lifetime_write_kbytes,
25 attr_reserved_clusters,
27 attr_trigger_test_error,
28 attr_first_error_time,
42 ptr_ext4_sb_info_offset,
43 ptr_ext4_super_block_offset,
46 static const char proc_dirname[] = "fs/ext4";
47 static struct proc_dir_entry *ext4_proc_root;
50 struct attribute attr;
53 unsigned short attr_size;
60 static ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
62 struct super_block *sb = sbi->s_buddy_cache->i_sb;
64 if (!sb->s_bdev->bd_part)
65 return snprintf(buf, PAGE_SIZE, "0\n");
66 return snprintf(buf, PAGE_SIZE, "%lu\n",
67 (part_stat_read(sb->s_bdev->bd_part,
68 sectors[STAT_WRITE]) -
69 sbi->s_sectors_written_start) >> 1);
72 static ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
74 struct super_block *sb = sbi->s_buddy_cache->i_sb;
76 if (!sb->s_bdev->bd_part)
77 return snprintf(buf, PAGE_SIZE, "0\n");
78 return snprintf(buf, PAGE_SIZE, "%llu\n",
79 (unsigned long long)(sbi->s_kbytes_written +
80 ((part_stat_read(sb->s_bdev->bd_part,
81 sectors[STAT_WRITE]) -
82 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
85 static ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
86 const char *buf, size_t count)
91 ret = kstrtoul(skip_spaces(buf), 0, &t);
95 if (t && (!is_power_of_2(t) || t > 0x40000000))
98 sbi->s_inode_readahead_blks = t;
102 static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
103 const char *buf, size_t count)
105 unsigned long long val;
106 ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
107 sbi->s_cluster_bits);
110 ret = kstrtoull(skip_spaces(buf), 0, &val);
111 if (ret || val >= clusters)
114 atomic64_set(&sbi->s_resv_clusters, val);
118 static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
119 const char *buf, size_t count)
123 if (!capable(CAP_SYS_ADMIN))
126 if (len && buf[len-1] == '\n')
130 ext4_error(sbi->s_sb, "%.*s", len, buf);
134 static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
137 return snprintf(buf, PAGE_SIZE, "<none>\n");
138 return snprintf(buf, PAGE_SIZE, "%d\n",
139 task_pid_vnr(sbi->s_journal->j_task));
142 #define EXT4_ATTR(_name,_mode,_id) \
143 static struct ext4_attr ext4_attr_##_name = { \
144 .attr = {.name = __stringify(_name), .mode = _mode }, \
145 .attr_id = attr_##_id, \
148 #define EXT4_ATTR_FUNC(_name,_mode) EXT4_ATTR(_name,_mode,_name)
150 #define EXT4_ATTR_FEATURE(_name) EXT4_ATTR(_name, 0444, feature)
152 #define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname) \
153 static struct ext4_attr ext4_attr_##_name = { \
154 .attr = {.name = __stringify(_name), .mode = _mode }, \
155 .attr_id = attr_##_id, \
156 .attr_ptr = ptr_##_struct##_offset, \
158 .offset = offsetof(struct _struct, _elname),\
162 #define EXT4_ATTR_STRING(_name,_mode,_size,_struct,_elname) \
163 static struct ext4_attr ext4_attr_##_name = { \
164 .attr = {.name = __stringify(_name), .mode = _mode }, \
165 .attr_id = attr_pointer_string, \
166 .attr_size = _size, \
167 .attr_ptr = ptr_##_struct##_offset, \
169 .offset = offsetof(struct _struct, _elname),\
173 #define EXT4_RO_ATTR_ES_UI(_name,_elname) \
174 EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
176 #define EXT4_RO_ATTR_ES_U8(_name,_elname) \
177 EXT4_ATTR_OFFSET(_name, 0444, pointer_u8, ext4_super_block, _elname)
179 #define EXT4_RO_ATTR_ES_U64(_name,_elname) \
180 EXT4_ATTR_OFFSET(_name, 0444, pointer_u64, ext4_super_block, _elname)
182 #define EXT4_RO_ATTR_ES_STRING(_name,_elname,_size) \
183 EXT4_ATTR_STRING(_name, 0444, _size, ext4_super_block, _elname)
185 #define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
186 EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
188 #define EXT4_RW_ATTR_SBI_UL(_name,_elname) \
189 EXT4_ATTR_OFFSET(_name, 0644, pointer_ul, ext4_sb_info, _elname)
191 #define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
192 static struct ext4_attr ext4_attr_##_name = { \
193 .attr = {.name = __stringify(_name), .mode = _mode }, \
194 .attr_id = attr_##_id, \
195 .attr_ptr = ptr_explicit, \
197 .explicit_ptr = _ptr, \
201 #define ATTR_LIST(name) &ext4_attr_##name.attr
203 EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
204 EXT4_ATTR_FUNC(session_write_kbytes, 0444);
205 EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
206 EXT4_ATTR_FUNC(reserved_clusters, 0644);
208 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
209 ext4_sb_info, s_inode_readahead_blks);
210 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
211 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
212 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
213 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
214 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
215 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
216 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
217 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
218 EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
219 EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
220 EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
221 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
222 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
223 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
224 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
225 #ifdef CONFIG_EXT4_DEBUG
226 EXT4_RW_ATTR_SBI_UL(simulate_fail, s_simulate_fail);
228 EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
229 EXT4_RO_ATTR_ES_U8(first_error_errcode, s_first_error_errcode);
230 EXT4_RO_ATTR_ES_U8(last_error_errcode, s_last_error_errcode);
231 EXT4_RO_ATTR_ES_UI(first_error_ino, s_first_error_ino);
232 EXT4_RO_ATTR_ES_UI(last_error_ino, s_last_error_ino);
233 EXT4_RO_ATTR_ES_U64(first_error_block, s_first_error_block);
234 EXT4_RO_ATTR_ES_U64(last_error_block, s_last_error_block);
235 EXT4_RO_ATTR_ES_UI(first_error_line, s_first_error_line);
236 EXT4_RO_ATTR_ES_UI(last_error_line, s_last_error_line);
237 EXT4_RO_ATTR_ES_STRING(first_error_func, s_first_error_func, 32);
238 EXT4_RO_ATTR_ES_STRING(last_error_func, s_last_error_func, 32);
239 EXT4_ATTR(first_error_time, 0444, first_error_time);
240 EXT4_ATTR(last_error_time, 0444, last_error_time);
241 EXT4_ATTR(journal_task, 0444, journal_task);
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 ATTR_LIST(lifetime_write_kbytes),
250 ATTR_LIST(reserved_clusters),
251 ATTR_LIST(inode_readahead_blks),
252 ATTR_LIST(inode_goal),
254 ATTR_LIST(mb_max_to_scan),
255 ATTR_LIST(mb_min_to_scan),
256 ATTR_LIST(mb_order2_req),
257 ATTR_LIST(mb_stream_req),
258 ATTR_LIST(mb_group_prealloc),
259 ATTR_LIST(max_writeback_mb_bump),
260 ATTR_LIST(extent_max_zeroout_kb),
261 ATTR_LIST(trigger_fs_error),
262 ATTR_LIST(err_ratelimit_interval_ms),
263 ATTR_LIST(err_ratelimit_burst),
264 ATTR_LIST(warning_ratelimit_interval_ms),
265 ATTR_LIST(warning_ratelimit_burst),
266 ATTR_LIST(msg_ratelimit_interval_ms),
267 ATTR_LIST(msg_ratelimit_burst),
268 ATTR_LIST(errors_count),
269 ATTR_LIST(first_error_ino),
270 ATTR_LIST(last_error_ino),
271 ATTR_LIST(first_error_block),
272 ATTR_LIST(last_error_block),
273 ATTR_LIST(first_error_line),
274 ATTR_LIST(last_error_line),
275 ATTR_LIST(first_error_func),
276 ATTR_LIST(last_error_func),
277 ATTR_LIST(first_error_errcode),
278 ATTR_LIST(last_error_errcode),
279 ATTR_LIST(first_error_time),
280 ATTR_LIST(last_error_time),
281 ATTR_LIST(journal_task),
282 #ifdef CONFIG_EXT4_DEBUG
283 ATTR_LIST(simulate_fail),
287 ATTRIBUTE_GROUPS(ext4);
289 /* Features this copy of ext4 supports */
290 EXT4_ATTR_FEATURE(lazy_itable_init);
291 EXT4_ATTR_FEATURE(batched_discard);
292 EXT4_ATTR_FEATURE(meta_bg_resize);
293 #ifdef CONFIG_FS_ENCRYPTION
294 EXT4_ATTR_FEATURE(encryption);
296 #ifdef CONFIG_UNICODE
297 EXT4_ATTR_FEATURE(casefold);
299 #ifdef CONFIG_FS_VERITY
300 EXT4_ATTR_FEATURE(verity);
302 EXT4_ATTR_FEATURE(metadata_csum_seed);
304 static struct attribute *ext4_feat_attrs[] = {
305 ATTR_LIST(lazy_itable_init),
306 ATTR_LIST(batched_discard),
307 ATTR_LIST(meta_bg_resize),
308 #ifdef CONFIG_FS_ENCRYPTION
309 ATTR_LIST(encryption),
311 #ifdef CONFIG_UNICODE
314 #ifdef CONFIG_FS_VERITY
317 ATTR_LIST(metadata_csum_seed),
320 ATTRIBUTE_GROUPS(ext4_feat);
322 static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
324 switch (a->attr_ptr) {
326 return a->u.explicit_ptr;
327 case ptr_ext4_sb_info_offset:
328 return (void *) (((char *) sbi) + a->u.offset);
329 case ptr_ext4_super_block_offset:
330 return (void *) (((char *) sbi->s_es) + a->u.offset);
335 static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
337 return snprintf(buf, PAGE_SIZE, "%lld\n",
338 ((time64_t)hi << 32) + le32_to_cpu(lo));
341 #define print_tstamp(buf, es, tstamp) \
342 __print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
344 static ssize_t ext4_attr_show(struct kobject *kobj,
345 struct attribute *attr, char *buf)
347 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
349 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
350 void *ptr = calc_ptr(a, sbi);
352 switch (a->attr_id) {
353 case attr_delayed_allocation_blocks:
354 return snprintf(buf, PAGE_SIZE, "%llu\n",
356 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
357 case attr_session_write_kbytes:
358 return session_write_kbytes_show(sbi, buf);
359 case attr_lifetime_write_kbytes:
360 return lifetime_write_kbytes_show(sbi, buf);
361 case attr_reserved_clusters:
362 return snprintf(buf, PAGE_SIZE, "%llu\n",
364 atomic64_read(&sbi->s_resv_clusters));
365 case attr_inode_readahead:
366 case attr_pointer_ui:
369 if (a->attr_ptr == ptr_ext4_super_block_offset)
370 return snprintf(buf, PAGE_SIZE, "%u\n",
373 return snprintf(buf, PAGE_SIZE, "%u\n",
374 *((unsigned int *) ptr));
375 case attr_pointer_ul:
378 return snprintf(buf, PAGE_SIZE, "%lu\n",
379 *((unsigned long *) ptr));
380 case attr_pointer_u8:
383 return snprintf(buf, PAGE_SIZE, "%u\n",
384 *((unsigned char *) ptr));
385 case attr_pointer_u64:
388 if (a->attr_ptr == ptr_ext4_super_block_offset)
389 return snprintf(buf, PAGE_SIZE, "%llu\n",
392 return snprintf(buf, PAGE_SIZE, "%llu\n",
393 *((unsigned long long *) ptr));
394 case attr_pointer_string:
397 return snprintf(buf, PAGE_SIZE, "%.*s\n", a->attr_size,
399 case attr_pointer_atomic:
402 return snprintf(buf, PAGE_SIZE, "%d\n",
403 atomic_read((atomic_t *) ptr));
405 return snprintf(buf, PAGE_SIZE, "supported\n");
406 case attr_first_error_time:
407 return print_tstamp(buf, sbi->s_es, s_first_error_time);
408 case attr_last_error_time:
409 return print_tstamp(buf, sbi->s_es, s_last_error_time);
410 case attr_journal_task:
411 return journal_task_show(sbi, buf);
417 static ssize_t ext4_attr_store(struct kobject *kobj,
418 struct attribute *attr,
419 const char *buf, size_t len)
421 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
423 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
424 void *ptr = calc_ptr(a, sbi);
428 switch (a->attr_id) {
429 case attr_reserved_clusters:
430 return reserved_clusters_store(sbi, buf, len);
431 case attr_pointer_ui:
434 ret = kstrtoul(skip_spaces(buf), 0, &t);
437 if (a->attr_ptr == ptr_ext4_super_block_offset)
438 *((__le32 *) ptr) = cpu_to_le32(t);
440 *((unsigned int *) ptr) = t;
442 case attr_pointer_ul:
445 ret = kstrtoul(skip_spaces(buf), 0, &t);
448 *((unsigned long *) ptr) = t;
450 case attr_inode_readahead:
451 return inode_readahead_blks_store(sbi, buf, len);
452 case attr_trigger_test_error:
453 return trigger_test_error(sbi, buf, len);
458 static void ext4_sb_release(struct kobject *kobj)
460 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
462 complete(&sbi->s_kobj_unregister);
465 static const struct sysfs_ops ext4_attr_ops = {
466 .show = ext4_attr_show,
467 .store = ext4_attr_store,
470 static struct kobj_type ext4_sb_ktype = {
471 .default_groups = ext4_groups,
472 .sysfs_ops = &ext4_attr_ops,
473 .release = ext4_sb_release,
476 static struct kobj_type ext4_feat_ktype = {
477 .default_groups = ext4_feat_groups,
478 .sysfs_ops = &ext4_attr_ops,
479 .release = (void (*)(struct kobject *))kfree,
482 static struct kobject *ext4_root;
484 static struct kobject *ext4_feat;
486 int ext4_register_sysfs(struct super_block *sb)
488 struct ext4_sb_info *sbi = EXT4_SB(sb);
491 init_completion(&sbi->s_kobj_unregister);
492 err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
495 kobject_put(&sbi->s_kobj);
496 wait_for_completion(&sbi->s_kobj_unregister);
501 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
503 proc_create_single_data("options", S_IRUGO, sbi->s_proc,
504 ext4_seq_options_show, sb);
505 proc_create_single_data("es_shrinker_info", S_IRUGO,
506 sbi->s_proc, ext4_seq_es_shrinker_info_show,
508 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
509 &ext4_mb_seq_groups_ops, sb);
514 void ext4_unregister_sysfs(struct super_block *sb)
516 struct ext4_sb_info *sbi = EXT4_SB(sb);
519 remove_proc_subtree(sb->s_id, ext4_proc_root);
520 kobject_del(&sbi->s_kobj);
523 int __init ext4_init_sysfs(void)
527 ext4_root = kobject_create_and_add("ext4", fs_kobj);
531 ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
537 ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
538 ext4_root, "features");
542 ext4_proc_root = proc_mkdir(proc_dirname, NULL);
546 kobject_put(ext4_feat);
549 kobject_put(ext4_root);
554 void ext4_exit_sysfs(void)
556 kobject_put(ext4_feat);
558 kobject_put(ext4_root);
560 remove_proc_entry(proc_dirname, NULL);
561 ext4_proc_root = NULL;