blob: 9394360ff137360de97885c0e2ea3f3c35b5b6de [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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,
Olivier Deprez0e641232021-09-23 10:07:05 +020026 attr_sra_exceeded_retry_limit,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027 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,
David Brazdil0f672f62019-12-10 10:32:29 +000034 attr_journal_task,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035} 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
David Brazdil0f672f62019-12-10 10:32:29 +0000130static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138#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);
Olivier Deprez0e641232021-09-23 10:07:05 +0200180EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181
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);
David Brazdil0f672f62019-12-10 10:32:29 +0000202EXT4_ATTR(journal_task, 0444, journal_task);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203
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),
Olivier Deprez0e641232021-09-23 10:07:05 +0200212 ATTR_LIST(sra_exceeded_retry_limit),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213 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),
David Brazdil0f672f62019-12-10 10:32:29 +0000233 ATTR_LIST(journal_task),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000234 NULL,
235};
David Brazdil0f672f62019-12-10 10:32:29 +0000236ATTRIBUTE_GROUPS(ext4);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237
238/* Features this copy of ext4 supports */
239EXT4_ATTR_FEATURE(lazy_itable_init);
240EXT4_ATTR_FEATURE(batched_discard);
241EXT4_ATTR_FEATURE(meta_bg_resize);
David Brazdil0f672f62019-12-10 10:32:29 +0000242#ifdef CONFIG_FS_ENCRYPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243EXT4_ATTR_FEATURE(encryption);
244#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000245#ifdef CONFIG_UNICODE
246EXT4_ATTR_FEATURE(casefold);
247#endif
248#ifdef CONFIG_FS_VERITY
249EXT4_ATTR_FEATURE(verity);
250#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251EXT4_ATTR_FEATURE(metadata_csum_seed);
252
253static struct attribute *ext4_feat_attrs[] = {
254 ATTR_LIST(lazy_itable_init),
255 ATTR_LIST(batched_discard),
256 ATTR_LIST(meta_bg_resize),
David Brazdil0f672f62019-12-10 10:32:29 +0000257#ifdef CONFIG_FS_ENCRYPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000258 ATTR_LIST(encryption),
259#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000260#ifdef CONFIG_UNICODE
261 ATTR_LIST(casefold),
262#endif
263#ifdef CONFIG_FS_VERITY
264 ATTR_LIST(verity),
265#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000266 ATTR_LIST(metadata_csum_seed),
267 NULL,
268};
David Brazdil0f672f62019-12-10 10:32:29 +0000269ATTRIBUTE_GROUPS(ext4_feat);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270
271static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
272{
273 switch (a->attr_ptr) {
274 case ptr_explicit:
275 return a->u.explicit_ptr;
276 case ptr_ext4_sb_info_offset:
277 return (void *) (((char *) sbi) + a->u.offset);
278 case ptr_ext4_super_block_offset:
279 return (void *) (((char *) sbi->s_es) + a->u.offset);
280 }
281 return NULL;
282}
283
284static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
285{
286 return snprintf(buf, PAGE_SIZE, "%lld",
287 ((time64_t)hi << 32) + le32_to_cpu(lo));
288}
289
290#define print_tstamp(buf, es, tstamp) \
291 __print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
292
293static ssize_t ext4_attr_show(struct kobject *kobj,
294 struct attribute *attr, char *buf)
295{
296 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
297 s_kobj);
298 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
299 void *ptr = calc_ptr(a, sbi);
300
301 switch (a->attr_id) {
302 case attr_delayed_allocation_blocks:
303 return snprintf(buf, PAGE_SIZE, "%llu\n",
304 (s64) EXT4_C2B(sbi,
305 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
306 case attr_session_write_kbytes:
307 return session_write_kbytes_show(sbi, buf);
308 case attr_lifetime_write_kbytes:
309 return lifetime_write_kbytes_show(sbi, buf);
310 case attr_reserved_clusters:
311 return snprintf(buf, PAGE_SIZE, "%llu\n",
312 (unsigned long long)
313 atomic64_read(&sbi->s_resv_clusters));
Olivier Deprez0e641232021-09-23 10:07:05 +0200314 case attr_sra_exceeded_retry_limit:
315 return snprintf(buf, PAGE_SIZE, "%llu\n",
316 (unsigned long long)
317 percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318 case attr_inode_readahead:
319 case attr_pointer_ui:
320 if (!ptr)
321 return 0;
322 if (a->attr_ptr == ptr_ext4_super_block_offset)
323 return snprintf(buf, PAGE_SIZE, "%u\n",
324 le32_to_cpup(ptr));
325 else
326 return snprintf(buf, PAGE_SIZE, "%u\n",
327 *((unsigned int *) ptr));
328 case attr_pointer_atomic:
329 if (!ptr)
330 return 0;
331 return snprintf(buf, PAGE_SIZE, "%d\n",
332 atomic_read((atomic_t *) ptr));
333 case attr_feature:
334 return snprintf(buf, PAGE_SIZE, "supported\n");
335 case attr_first_error_time:
336 return print_tstamp(buf, sbi->s_es, s_first_error_time);
337 case attr_last_error_time:
338 return print_tstamp(buf, sbi->s_es, s_last_error_time);
David Brazdil0f672f62019-12-10 10:32:29 +0000339 case attr_journal_task:
340 return journal_task_show(sbi, buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000341 }
342
343 return 0;
344}
345
346static ssize_t ext4_attr_store(struct kobject *kobj,
347 struct attribute *attr,
348 const char *buf, size_t len)
349{
350 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
351 s_kobj);
352 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
353 void *ptr = calc_ptr(a, sbi);
354 unsigned long t;
355 int ret;
356
357 switch (a->attr_id) {
358 case attr_reserved_clusters:
359 return reserved_clusters_store(sbi, buf, len);
360 case attr_pointer_ui:
361 if (!ptr)
362 return 0;
363 ret = kstrtoul(skip_spaces(buf), 0, &t);
364 if (ret)
365 return ret;
366 if (a->attr_ptr == ptr_ext4_super_block_offset)
367 *((__le32 *) ptr) = cpu_to_le32(t);
368 else
369 *((unsigned int *) ptr) = t;
370 return len;
371 case attr_inode_readahead:
372 return inode_readahead_blks_store(sbi, buf, len);
373 case attr_trigger_test_error:
374 return trigger_test_error(sbi, buf, len);
375 }
376 return 0;
377}
378
379static void ext4_sb_release(struct kobject *kobj)
380{
381 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
382 s_kobj);
383 complete(&sbi->s_kobj_unregister);
384}
385
386static const struct sysfs_ops ext4_attr_ops = {
387 .show = ext4_attr_show,
388 .store = ext4_attr_store,
389};
390
391static struct kobj_type ext4_sb_ktype = {
David Brazdil0f672f62019-12-10 10:32:29 +0000392 .default_groups = ext4_groups,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393 .sysfs_ops = &ext4_attr_ops,
394 .release = ext4_sb_release,
395};
396
397static struct kobj_type ext4_feat_ktype = {
David Brazdil0f672f62019-12-10 10:32:29 +0000398 .default_groups = ext4_feat_groups,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 .sysfs_ops = &ext4_attr_ops,
400 .release = (void (*)(struct kobject *))kfree,
401};
402
403static struct kobject *ext4_root;
404
405static struct kobject *ext4_feat;
406
407int ext4_register_sysfs(struct super_block *sb)
408{
409 struct ext4_sb_info *sbi = EXT4_SB(sb);
410 int err;
411
412 init_completion(&sbi->s_kobj_unregister);
413 err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
414 "%s", sb->s_id);
415 if (err) {
416 kobject_put(&sbi->s_kobj);
417 wait_for_completion(&sbi->s_kobj_unregister);
418 return err;
419 }
420
421 if (ext4_proc_root)
422 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
423 if (sbi->s_proc) {
424 proc_create_single_data("options", S_IRUGO, sbi->s_proc,
425 ext4_seq_options_show, sb);
426 proc_create_single_data("es_shrinker_info", S_IRUGO,
427 sbi->s_proc, ext4_seq_es_shrinker_info_show,
428 sb);
429 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
430 &ext4_mb_seq_groups_ops, sb);
431 }
432 return 0;
433}
434
435void ext4_unregister_sysfs(struct super_block *sb)
436{
437 struct ext4_sb_info *sbi = EXT4_SB(sb);
438
439 if (sbi->s_proc)
440 remove_proc_subtree(sb->s_id, ext4_proc_root);
441 kobject_del(&sbi->s_kobj);
442}
443
444int __init ext4_init_sysfs(void)
445{
446 int ret;
447
448 ext4_root = kobject_create_and_add("ext4", fs_kobj);
449 if (!ext4_root)
450 return -ENOMEM;
451
452 ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
453 if (!ext4_feat) {
454 ret = -ENOMEM;
455 goto root_err;
456 }
457
458 ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
459 ext4_root, "features");
460 if (ret)
461 goto feat_err;
462
463 ext4_proc_root = proc_mkdir(proc_dirname, NULL);
464 return ret;
465
466feat_err:
467 kobject_put(ext4_feat);
468 ext4_feat = NULL;
469root_err:
470 kobject_put(ext4_root);
471 ext4_root = NULL;
472 return ret;
473}
474
475void ext4_exit_sysfs(void)
476{
477 kobject_put(ext4_feat);
478 ext4_feat = NULL;
479 kobject_put(ext4_root);
480 ext4_root = NULL;
481 remove_proc_entry(proc_dirname, NULL);
482 ext4_proc_root = NULL;
483}
484