blob: f30d5d34069f093cbff67625935a4a74de59bc7c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/fs/ext4/sysfs.c
4 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Theodore Ts'o (tytso@mit.edu)
8 *
9 */
10
11#include <linux/time.h>
12#include <linux/fs.h>
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <linux/proc_fs.h>
16
17#include "ext4.h"
18#include "ext4_jbd2.h"
19
20typedef enum {
21 attr_noop,
22 attr_delayed_allocation_blocks,
23 attr_session_write_kbytes,
24 attr_lifetime_write_kbytes,
25 attr_reserved_clusters,
26 attr_sra_exceeded_retry_limit,
27 attr_inode_readahead,
28 attr_trigger_test_error,
29 attr_first_error_time,
30 attr_last_error_time,
31 attr_feature,
32 attr_pointer_ui,
33 attr_pointer_atomic,
34 attr_journal_task,
35} attr_id_t;
36
37typedef enum {
38 ptr_explicit,
39 ptr_ext4_sb_info_offset,
40 ptr_ext4_super_block_offset,
41} attr_ptr_t;
42
43static const char proc_dirname[] = "fs/ext4";
44static struct proc_dir_entry *ext4_proc_root;
45
46struct ext4_attr {
47 struct attribute attr;
48 short attr_id;
49 short attr_ptr;
50 union {
51 int offset;
52 void *explicit_ptr;
53 } u;
54};
55
56static ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
57{
58 struct super_block *sb = sbi->s_buddy_cache->i_sb;
59
60 if (!sb->s_bdev->bd_part)
61 return snprintf(buf, PAGE_SIZE, "0\n");
62 return snprintf(buf, PAGE_SIZE, "%lu\n",
63 (part_stat_read(sb->s_bdev->bd_part,
64 sectors[STAT_WRITE]) -
65 sbi->s_sectors_written_start) >> 1);
66}
67
68static ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
69{
70 struct super_block *sb = sbi->s_buddy_cache->i_sb;
71
72 if (!sb->s_bdev->bd_part)
73 return snprintf(buf, PAGE_SIZE, "0\n");
74 return snprintf(buf, PAGE_SIZE, "%llu\n",
75 (unsigned long long)(sbi->s_kbytes_written +
76 ((part_stat_read(sb->s_bdev->bd_part,
77 sectors[STAT_WRITE]) -
78 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
79}
80
81static ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
82 const char *buf, size_t count)
83{
84 unsigned long t;
85 int ret;
86
87 ret = kstrtoul(skip_spaces(buf), 0, &t);
88 if (ret)
89 return ret;
90
91 if (t && (!is_power_of_2(t) || t > 0x40000000))
92 return -EINVAL;
93
94 sbi->s_inode_readahead_blks = t;
95 return count;
96}
97
98static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
99 const char *buf, size_t count)
100{
101 unsigned long long val;
102 ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
103 sbi->s_cluster_bits);
104 int ret;
105
106 ret = kstrtoull(skip_spaces(buf), 0, &val);
107 if (ret || val >= clusters)
108 return -EINVAL;
109
110 atomic64_set(&sbi->s_resv_clusters, val);
111 return count;
112}
113
114static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
115 const char *buf, size_t count)
116{
117 int len = count;
118
119 if (!capable(CAP_SYS_ADMIN))
120 return -EPERM;
121
122 if (len && buf[len-1] == '\n')
123 len--;
124
125 if (len)
126 ext4_error(sbi->s_sb, "%.*s", len, buf);
127 return count;
128}
129
130static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
131{
132 if (!sbi->s_journal)
133 return snprintf(buf, PAGE_SIZE, "<none>\n");
134 return snprintf(buf, PAGE_SIZE, "%d\n",
135 task_pid_vnr(sbi->s_journal->j_task));
136}
137
138#define EXT4_ATTR(_name,_mode,_id) \
139static struct ext4_attr ext4_attr_##_name = { \
140 .attr = {.name = __stringify(_name), .mode = _mode }, \
141 .attr_id = attr_##_id, \
142}
143
144#define EXT4_ATTR_FUNC(_name,_mode) EXT4_ATTR(_name,_mode,_name)
145
146#define EXT4_ATTR_FEATURE(_name) EXT4_ATTR(_name, 0444, feature)
147
148#define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname) \
149static struct ext4_attr ext4_attr_##_name = { \
150 .attr = {.name = __stringify(_name), .mode = _mode }, \
151 .attr_id = attr_##_id, \
152 .attr_ptr = ptr_##_struct##_offset, \
153 .u = { \
154 .offset = offsetof(struct _struct, _elname),\
155 }, \
156}
157
158#define EXT4_RO_ATTR_ES_UI(_name,_elname) \
159 EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
160
161#define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
162 EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
163
164#define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
165static struct ext4_attr ext4_attr_##_name = { \
166 .attr = {.name = __stringify(_name), .mode = _mode }, \
167 .attr_id = attr_##_id, \
168 .attr_ptr = ptr_explicit, \
169 .u = { \
170 .explicit_ptr = _ptr, \
171 }, \
172}
173
174#define ATTR_LIST(name) &ext4_attr_##name.attr
175
176EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
177EXT4_ATTR_FUNC(session_write_kbytes, 0444);
178EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
179EXT4_ATTR_FUNC(reserved_clusters, 0644);
180EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
181
182EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
183 ext4_sb_info, s_inode_readahead_blks);
184EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
185EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
186EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
187EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
188EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
189EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
190EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
191EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
192EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
193EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
194EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
195EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
196EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
197EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
198EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
199EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
200EXT4_ATTR(first_error_time, 0444, first_error_time);
201EXT4_ATTR(last_error_time, 0444, last_error_time);
202EXT4_ATTR(journal_task, 0444, journal_task);
203
204static unsigned int old_bump_val = 128;
205EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
206
207static struct attribute *ext4_attrs[] = {
208 ATTR_LIST(delayed_allocation_blocks),
209 ATTR_LIST(session_write_kbytes),
210 ATTR_LIST(lifetime_write_kbytes),
211 ATTR_LIST(reserved_clusters),
212 ATTR_LIST(sra_exceeded_retry_limit),
213 ATTR_LIST(inode_readahead_blks),
214 ATTR_LIST(inode_goal),
215 ATTR_LIST(mb_stats),
216 ATTR_LIST(mb_max_to_scan),
217 ATTR_LIST(mb_min_to_scan),
218 ATTR_LIST(mb_order2_req),
219 ATTR_LIST(mb_stream_req),
220 ATTR_LIST(mb_group_prealloc),
221 ATTR_LIST(max_writeback_mb_bump),
222 ATTR_LIST(extent_max_zeroout_kb),
223 ATTR_LIST(trigger_fs_error),
224 ATTR_LIST(err_ratelimit_interval_ms),
225 ATTR_LIST(err_ratelimit_burst),
226 ATTR_LIST(warning_ratelimit_interval_ms),
227 ATTR_LIST(warning_ratelimit_burst),
228 ATTR_LIST(msg_ratelimit_interval_ms),
229 ATTR_LIST(msg_ratelimit_burst),
230 ATTR_LIST(errors_count),
231 ATTR_LIST(first_error_time),
232 ATTR_LIST(last_error_time),
233 ATTR_LIST(journal_task),
234 NULL,
235};
236ATTRIBUTE_GROUPS(ext4);
237
238/* Features this copy of ext4 supports */
239EXT4_ATTR_FEATURE(lazy_itable_init);
240EXT4_ATTR_FEATURE(batched_discard);
241EXT4_ATTR_FEATURE(meta_bg_resize);
242#ifdef CONFIG_FS_ENCRYPTION
243EXT4_ATTR_FEATURE(encryption);
244EXT4_ATTR_FEATURE(test_dummy_encryption_v2);
245#endif
246#ifdef CONFIG_UNICODE
247EXT4_ATTR_FEATURE(casefold);
248#endif
249#ifdef CONFIG_FS_VERITY
250EXT4_ATTR_FEATURE(verity);
251#endif
252EXT4_ATTR_FEATURE(metadata_csum_seed);
253#if defined(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
254EXT4_ATTR_FEATURE(encrypted_casefold);
255#endif
256
257static struct attribute *ext4_feat_attrs[] = {
258 ATTR_LIST(lazy_itable_init),
259 ATTR_LIST(batched_discard),
260 ATTR_LIST(meta_bg_resize),
261#ifdef CONFIG_FS_ENCRYPTION
262 ATTR_LIST(encryption),
263 ATTR_LIST(test_dummy_encryption_v2),
264#endif
265#ifdef CONFIG_UNICODE
266 ATTR_LIST(casefold),
267#endif
268#ifdef CONFIG_FS_VERITY
269 ATTR_LIST(verity),
270#endif
271 ATTR_LIST(metadata_csum_seed),
272#if defined(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
273 ATTR_LIST(encrypted_casefold),
274#endif
275 NULL,
276};
277ATTRIBUTE_GROUPS(ext4_feat);
278
279static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
280{
281 switch (a->attr_ptr) {
282 case ptr_explicit:
283 return a->u.explicit_ptr;
284 case ptr_ext4_sb_info_offset:
285 return (void *) (((char *) sbi) + a->u.offset);
286 case ptr_ext4_super_block_offset:
287 return (void *) (((char *) sbi->s_es) + a->u.offset);
288 }
289 return NULL;
290}
291
292static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
293{
294 return snprintf(buf, PAGE_SIZE, "%lld",
295 ((time64_t)hi << 32) + le32_to_cpu(lo));
296}
297
298#define print_tstamp(buf, es, tstamp) \
299 __print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
300
301static ssize_t ext4_attr_show(struct kobject *kobj,
302 struct attribute *attr, char *buf)
303{
304 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
305 s_kobj);
306 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
307 void *ptr = calc_ptr(a, sbi);
308
309 switch (a->attr_id) {
310 case attr_delayed_allocation_blocks:
311 return snprintf(buf, PAGE_SIZE, "%llu\n",
312 (s64) EXT4_C2B(sbi,
313 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
314 case attr_session_write_kbytes:
315 return session_write_kbytes_show(sbi, buf);
316 case attr_lifetime_write_kbytes:
317 return lifetime_write_kbytes_show(sbi, buf);
318 case attr_reserved_clusters:
319 return snprintf(buf, PAGE_SIZE, "%llu\n",
320 (unsigned long long)
321 atomic64_read(&sbi->s_resv_clusters));
322 case attr_sra_exceeded_retry_limit:
323 return snprintf(buf, PAGE_SIZE, "%llu\n",
324 (unsigned long long)
325 percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit));
326 case attr_inode_readahead:
327 case attr_pointer_ui:
328 if (!ptr)
329 return 0;
330 if (a->attr_ptr == ptr_ext4_super_block_offset)
331 return snprintf(buf, PAGE_SIZE, "%u\n",
332 le32_to_cpup(ptr));
333 else
334 return snprintf(buf, PAGE_SIZE, "%u\n",
335 *((unsigned int *) ptr));
336 case attr_pointer_atomic:
337 if (!ptr)
338 return 0;
339 return snprintf(buf, PAGE_SIZE, "%d\n",
340 atomic_read((atomic_t *) ptr));
341 case attr_feature:
342 return snprintf(buf, PAGE_SIZE, "supported\n");
343 case attr_first_error_time:
344 return print_tstamp(buf, sbi->s_es, s_first_error_time);
345 case attr_last_error_time:
346 return print_tstamp(buf, sbi->s_es, s_last_error_time);
347 case attr_journal_task:
348 return journal_task_show(sbi, buf);
349 }
350
351 return 0;
352}
353
354static ssize_t ext4_attr_store(struct kobject *kobj,
355 struct attribute *attr,
356 const char *buf, size_t len)
357{
358 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
359 s_kobj);
360 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
361 void *ptr = calc_ptr(a, sbi);
362 unsigned long t;
363 int ret;
364
365 switch (a->attr_id) {
366 case attr_reserved_clusters:
367 return reserved_clusters_store(sbi, buf, len);
368 case attr_pointer_ui:
369 if (!ptr)
370 return 0;
371 ret = kstrtoul(skip_spaces(buf), 0, &t);
372 if (ret)
373 return ret;
374 if (a->attr_ptr == ptr_ext4_super_block_offset)
375 *((__le32 *) ptr) = cpu_to_le32(t);
376 else
377 *((unsigned int *) ptr) = t;
378 return len;
379 case attr_inode_readahead:
380 return inode_readahead_blks_store(sbi, buf, len);
381 case attr_trigger_test_error:
382 return trigger_test_error(sbi, buf, len);
383 }
384 return 0;
385}
386
387static void ext4_sb_release(struct kobject *kobj)
388{
389 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
390 s_kobj);
391 complete(&sbi->s_kobj_unregister);
392}
393
394static void ext4_feat_release(struct kobject *kobj)
395{
396 kfree(kobj);
397}
398
399static const struct sysfs_ops ext4_attr_ops = {
400 .show = ext4_attr_show,
401 .store = ext4_attr_store,
402};
403
404static struct kobj_type ext4_sb_ktype = {
405 .default_groups = ext4_groups,
406 .sysfs_ops = &ext4_attr_ops,
407 .release = ext4_sb_release,
408};
409
410static struct kobj_type ext4_feat_ktype = {
411 .default_groups = ext4_feat_groups,
412 .sysfs_ops = &ext4_attr_ops,
413 .release = ext4_feat_release,
414};
415
416static struct kobject *ext4_root;
417
418static struct kobject *ext4_feat;
419
420int ext4_register_sysfs(struct super_block *sb)
421{
422 struct ext4_sb_info *sbi = EXT4_SB(sb);
423 int err;
424
425 init_completion(&sbi->s_kobj_unregister);
426 err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
427 "%s", sb->s_id);
428 if (err) {
429 kobject_put(&sbi->s_kobj);
430 wait_for_completion(&sbi->s_kobj_unregister);
431 return err;
432 }
433
434 if (ext4_proc_root)
435 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
436 if (sbi->s_proc) {
437 proc_create_single_data("options", S_IRUGO, sbi->s_proc,
438 ext4_seq_options_show, sb);
439 proc_create_single_data("es_shrinker_info", S_IRUGO,
440 sbi->s_proc, ext4_seq_es_shrinker_info_show,
441 sb);
442 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
443 &ext4_mb_seq_groups_ops, sb);
444 }
445 return 0;
446}
447
448void ext4_unregister_sysfs(struct super_block *sb)
449{
450 struct ext4_sb_info *sbi = EXT4_SB(sb);
451
452 if (sbi->s_proc)
453 remove_proc_subtree(sb->s_id, ext4_proc_root);
454 kobject_del(&sbi->s_kobj);
455}
456
457int __init ext4_init_sysfs(void)
458{
459 int ret;
460
461 ext4_root = kobject_create_and_add("ext4", fs_kobj);
462 if (!ext4_root)
463 return -ENOMEM;
464
465 ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
466 if (!ext4_feat) {
467 ret = -ENOMEM;
468 goto root_err;
469 }
470
471 ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
472 ext4_root, "features");
473 if (ret)
474 goto feat_err;
475
476 ext4_proc_root = proc_mkdir(proc_dirname, NULL);
477 return ret;
478
479feat_err:
480 kobject_put(ext4_feat);
481 ext4_feat = NULL;
482root_err:
483 kobject_put(ext4_root);
484 ext4_root = NULL;
485 return ret;
486}
487
488void ext4_exit_sysfs(void)
489{
490 kobject_put(ext4_feat);
491 ext4_feat = NULL;
492 kobject_put(ext4_root);
493 ext4_root = NULL;
494 remove_proc_entry(proc_dirname, NULL);
495 ext4_proc_root = NULL;
496}
497