1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b5799018STheodore Ts'o /*
3b5799018STheodore Ts'o * linux/fs/ext4/sysfs.c
4b5799018STheodore Ts'o *
5b5799018STheodore Ts'o * Copyright (C) 1992, 1993, 1994, 1995
6b5799018STheodore Ts'o * Remy Card (card@masi.ibp.fr)
7b5799018STheodore Ts'o * Theodore Ts'o (tytso@mit.edu)
8b5799018STheodore Ts'o *
9b5799018STheodore Ts'o */
10b5799018STheodore Ts'o
11b5799018STheodore Ts'o #include <linux/time.h>
12b5799018STheodore Ts'o #include <linux/fs.h>
13b5799018STheodore Ts'o #include <linux/seq_file.h>
14b99fee58SRiccardo Schirone #include <linux/slab.h>
15b5799018STheodore Ts'o #include <linux/proc_fs.h>
16c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
17b5799018STheodore Ts'o
18b5799018STheodore Ts'o #include "ext4.h"
19b5799018STheodore Ts'o #include "ext4_jbd2.h"
20b5799018STheodore Ts'o
2176d33bcaSTheodore Ts'o typedef enum {
2276d33bcaSTheodore Ts'o attr_noop,
2376d33bcaSTheodore Ts'o attr_delayed_allocation_blocks,
2476d33bcaSTheodore Ts'o attr_session_write_kbytes,
2576d33bcaSTheodore Ts'o attr_lifetime_write_kbytes,
2676d33bcaSTheodore Ts'o attr_reserved_clusters,
27efc61345SEric Whitney attr_sra_exceeded_retry_limit,
2876d33bcaSTheodore Ts'o attr_inode_readahead,
2976d33bcaSTheodore Ts'o attr_trigger_test_error,
306a0678a7SArnd Bergmann attr_first_error_time,
316a0678a7SArnd Bergmann attr_last_error_time,
32677ff458SBaokun Li attr_clusters_in_group,
3376d33bcaSTheodore Ts'o attr_feature,
3476d33bcaSTheodore Ts'o attr_pointer_ui,
3546f870d6STheodore Ts'o attr_pointer_ul,
364549b49fSTheodore Ts'o attr_pointer_u64,
374549b49fSTheodore Ts'o attr_pointer_u8,
384549b49fSTheodore Ts'o attr_pointer_string,
3976d33bcaSTheodore Ts'o attr_pointer_atomic,
40bc1d69d6SKonstantin Khlebnikov attr_journal_task,
4176d33bcaSTheodore Ts'o } attr_id_t;
4276d33bcaSTheodore Ts'o
4376d33bcaSTheodore Ts'o typedef enum {
4476d33bcaSTheodore Ts'o ptr_explicit,
4576d33bcaSTheodore Ts'o ptr_ext4_sb_info_offset,
4676d33bcaSTheodore Ts'o ptr_ext4_super_block_offset,
4776d33bcaSTheodore Ts'o } attr_ptr_t;
4876d33bcaSTheodore Ts'o
49d6006186SEric Biggers static const char proc_dirname[] = "fs/ext4";
50ebd173beSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
51ebd173beSTheodore Ts'o
52b5799018STheodore Ts'o struct ext4_attr {
53b5799018STheodore Ts'o struct attribute attr;
5476d33bcaSTheodore Ts'o short attr_id;
5576d33bcaSTheodore Ts'o short attr_ptr;
564549b49fSTheodore Ts'o unsigned short attr_size;
57b5799018STheodore Ts'o union {
58b5799018STheodore Ts'o int offset;
5976d33bcaSTheodore Ts'o void *explicit_ptr;
60b5799018STheodore Ts'o } u;
61b5799018STheodore Ts'o };
62b5799018STheodore Ts'o
session_write_kbytes_show(struct ext4_sb_info * sbi,char * buf)636ca06829STyson Nottingham static ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
64b5799018STheodore Ts'o {
65b5799018STheodore Ts'o struct super_block *sb = sbi->s_buddy_cache->i_sb;
66b5799018STheodore Ts'o
67dfac1a16SQing Wang return sysfs_emit(buf, "%lu\n",
688446fe92SChristoph Hellwig (part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
69b5799018STheodore Ts'o sbi->s_sectors_written_start) >> 1);
70b5799018STheodore Ts'o }
71b5799018STheodore Ts'o
lifetime_write_kbytes_show(struct ext4_sb_info * sbi,char * buf)726ca06829STyson Nottingham static ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
73b5799018STheodore Ts'o {
74b5799018STheodore Ts'o struct super_block *sb = sbi->s_buddy_cache->i_sb;
75b5799018STheodore Ts'o
76dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
77b5799018STheodore Ts'o (unsigned long long)(sbi->s_kbytes_written +
788446fe92SChristoph Hellwig ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
79b5799018STheodore Ts'o EXT4_SB(sb)->s_sectors_written_start) >> 1)));
80b5799018STheodore Ts'o }
81b5799018STheodore Ts'o
inode_readahead_blks_store(struct ext4_sb_info * sbi,const char * buf,size_t count)826ca06829STyson Nottingham static ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
83b5799018STheodore Ts'o const char *buf, size_t count)
84b5799018STheodore Ts'o {
85b5799018STheodore Ts'o unsigned long t;
86b5799018STheodore Ts'o int ret;
87b5799018STheodore Ts'o
88b5799018STheodore Ts'o ret = kstrtoul(skip_spaces(buf), 0, &t);
89b5799018STheodore Ts'o if (ret)
90b5799018STheodore Ts'o return ret;
91b5799018STheodore Ts'o
92b5799018STheodore Ts'o if (t && (!is_power_of_2(t) || t > 0x40000000))
93b5799018STheodore Ts'o return -EINVAL;
94b5799018STheodore Ts'o
95b5799018STheodore Ts'o sbi->s_inode_readahead_blks = t;
96b5799018STheodore Ts'o return count;
97b5799018STheodore Ts'o }
98b5799018STheodore Ts'o
reserved_clusters_store(struct ext4_sb_info * sbi,const char * buf,size_t count)996ca06829STyson Nottingham static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
100b5799018STheodore Ts'o const char *buf, size_t count)
101b5799018STheodore Ts'o {
102b5799018STheodore Ts'o unsigned long long val;
103b5799018STheodore Ts'o ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
104b5799018STheodore Ts'o sbi->s_cluster_bits);
10576d33bcaSTheodore Ts'o int ret;
106b5799018STheodore Ts'o
10776d33bcaSTheodore Ts'o ret = kstrtoull(skip_spaces(buf), 0, &val);
10882d5a4baSBaokun Li if (ret || val >= clusters || (s64)val < 0)
109b5799018STheodore Ts'o return -EINVAL;
110b5799018STheodore Ts'o
111b5799018STheodore Ts'o atomic64_set(&sbi->s_resv_clusters, val);
112b5799018STheodore Ts'o return count;
113b5799018STheodore Ts'o }
114b5799018STheodore Ts'o
trigger_test_error(struct ext4_sb_info * sbi,const char * buf,size_t count)1156ca06829STyson Nottingham static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
116b5799018STheodore Ts'o const char *buf, size_t count)
117b5799018STheodore Ts'o {
118b5799018STheodore Ts'o int len = count;
119b5799018STheodore Ts'o
120b5799018STheodore Ts'o if (!capable(CAP_SYS_ADMIN))
121b5799018STheodore Ts'o return -EPERM;
122b5799018STheodore Ts'o
123b5799018STheodore Ts'o if (len && buf[len-1] == '\n')
124b5799018STheodore Ts'o len--;
125b5799018STheodore Ts'o
126b5799018STheodore Ts'o if (len)
127b5799018STheodore Ts'o ext4_error(sbi->s_sb, "%.*s", len, buf);
128b5799018STheodore Ts'o return count;
129b5799018STheodore Ts'o }
130b5799018STheodore Ts'o
journal_task_show(struct ext4_sb_info * sbi,char * buf)131bc1d69d6SKonstantin Khlebnikov static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
132bc1d69d6SKonstantin Khlebnikov {
133bc1d69d6SKonstantin Khlebnikov if (!sbi->s_journal)
134dfac1a16SQing Wang return sysfs_emit(buf, "<none>\n");
135dfac1a16SQing Wang return sysfs_emit(buf, "%d\n",
136bc1d69d6SKonstantin Khlebnikov task_pid_vnr(sbi->s_journal->j_task));
137bc1d69d6SKonstantin Khlebnikov }
138bc1d69d6SKonstantin Khlebnikov
13976d33bcaSTheodore Ts'o #define EXT4_ATTR(_name,_mode,_id) \
140b5799018STheodore Ts'o static struct ext4_attr ext4_attr_##_name = { \
141b5799018STheodore Ts'o .attr = {.name = __stringify(_name), .mode = _mode }, \
14276d33bcaSTheodore Ts'o .attr_id = attr_##_id, \
14376d33bcaSTheodore Ts'o }
14476d33bcaSTheodore Ts'o
14576d33bcaSTheodore Ts'o #define EXT4_ATTR_FUNC(_name,_mode) EXT4_ATTR(_name,_mode,_name)
14676d33bcaSTheodore Ts'o
14776d33bcaSTheodore Ts'o #define EXT4_ATTR_FEATURE(_name) EXT4_ATTR(_name, 0444, feature)
14876d33bcaSTheodore Ts'o
14976d33bcaSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname) \
15076d33bcaSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = { \
15176d33bcaSTheodore Ts'o .attr = {.name = __stringify(_name), .mode = _mode }, \
15276d33bcaSTheodore Ts'o .attr_id = attr_##_id, \
15376d33bcaSTheodore Ts'o .attr_ptr = ptr_##_struct##_offset, \
154b5799018STheodore Ts'o .u = { \
15576d33bcaSTheodore Ts'o .offset = offsetof(struct _struct, _elname),\
156b5799018STheodore Ts'o }, \
157b5799018STheodore Ts'o }
158b5799018STheodore Ts'o
1594549b49fSTheodore Ts'o #define EXT4_ATTR_STRING(_name,_mode,_size,_struct,_elname) \
1604549b49fSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = { \
1614549b49fSTheodore Ts'o .attr = {.name = __stringify(_name), .mode = _mode }, \
1624549b49fSTheodore Ts'o .attr_id = attr_pointer_string, \
1634549b49fSTheodore Ts'o .attr_size = _size, \
1644549b49fSTheodore Ts'o .attr_ptr = ptr_##_struct##_offset, \
1654549b49fSTheodore Ts'o .u = { \
1664549b49fSTheodore Ts'o .offset = offsetof(struct _struct, _elname),\
1674549b49fSTheodore Ts'o }, \
1684549b49fSTheodore Ts'o }
1694549b49fSTheodore Ts'o
17076d33bcaSTheodore Ts'o #define EXT4_RO_ATTR_ES_UI(_name,_elname) \
17176d33bcaSTheodore Ts'o EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
17276d33bcaSTheodore Ts'o
1734549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_U8(_name,_elname) \
1744549b49fSTheodore Ts'o EXT4_ATTR_OFFSET(_name, 0444, pointer_u8, ext4_super_block, _elname)
1754549b49fSTheodore Ts'o
1764549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_U64(_name,_elname) \
1774549b49fSTheodore Ts'o EXT4_ATTR_OFFSET(_name, 0444, pointer_u64, ext4_super_block, _elname)
1784549b49fSTheodore Ts'o
1794549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_STRING(_name,_elname,_size) \
1804549b49fSTheodore Ts'o EXT4_ATTR_STRING(_name, 0444, _size, ext4_super_block, _elname)
1814549b49fSTheodore Ts'o
18276d33bcaSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(_name,_elname) \
18376d33bcaSTheodore Ts'o EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
18476d33bcaSTheodore Ts'o
18546f870d6STheodore Ts'o #define EXT4_RW_ATTR_SBI_UL(_name,_elname) \
18646f870d6STheodore Ts'o EXT4_ATTR_OFFSET(_name, 0644, pointer_ul, ext4_sb_info, _elname)
18746f870d6STheodore Ts'o
1881cf006edSDmitry Monakhov #define EXT4_RO_ATTR_SBI_ATOMIC(_name,_elname) \
1891cf006edSDmitry Monakhov EXT4_ATTR_OFFSET(_name, 0444, pointer_atomic, ext4_sb_info, _elname)
1901cf006edSDmitry Monakhov
19176d33bcaSTheodore Ts'o #define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
192b5799018STheodore Ts'o static struct ext4_attr ext4_attr_##_name = { \
193b5799018STheodore Ts'o .attr = {.name = __stringify(_name), .mode = _mode }, \
19476d33bcaSTheodore Ts'o .attr_id = attr_##_id, \
19576d33bcaSTheodore Ts'o .attr_ptr = ptr_explicit, \
196b5799018STheodore Ts'o .u = { \
19776d33bcaSTheodore Ts'o .explicit_ptr = _ptr, \
198b5799018STheodore Ts'o }, \
199b5799018STheodore Ts'o }
200b5799018STheodore Ts'o
201b5799018STheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
202b5799018STheodore Ts'o
20376d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
20476d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(session_write_kbytes, 0444);
20576d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
20676d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(reserved_clusters, 0644);
207efc61345SEric Whitney EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
20876d33bcaSTheodore Ts'o
20976d33bcaSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
21076d33bcaSTheodore Ts'o ext4_sb_info, s_inode_readahead_blks);
211677ff458SBaokun Li EXT4_ATTR_OFFSET(mb_group_prealloc, 0644, clusters_in_group,
212677ff458SBaokun Li ext4_sb_info, s_mb_group_prealloc);
213b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
214b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
215b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
216b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
217b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
218b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
219196e402aSHarshad Shirwadkar EXT4_RW_ATTR_SBI_UI(mb_max_linear_groups, s_mb_max_linear_groups);
220b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
22176d33bcaSTheodore Ts'o EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
222b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
223b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
224b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
225b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
226b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
227b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
228f52f3d2bSOjaswin Mujoo EXT4_RW_ATTR_SBI_UI(mb_best_avail_max_trim_order, s_mb_best_avail_max_trim_order);
22946f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
23046f870d6STheodore Ts'o EXT4_RW_ATTR_SBI_UL(simulate_fail, s_simulate_fail);
23146f870d6STheodore Ts'o #endif
2321cf006edSDmitry Monakhov EXT4_RO_ATTR_SBI_ATOMIC(warning_count, s_warning_count);
2331cf006edSDmitry Monakhov EXT4_RO_ATTR_SBI_ATOMIC(msg_count, s_msg_count);
234b5799018STheodore Ts'o EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
2354549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_U8(first_error_errcode, s_first_error_errcode);
2364549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_U8(last_error_errcode, s_last_error_errcode);
2374549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_UI(first_error_ino, s_first_error_ino);
2384549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_UI(last_error_ino, s_last_error_ino);
2394549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_U64(first_error_block, s_first_error_block);
2404549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_U64(last_error_block, s_last_error_block);
2414549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_UI(first_error_line, s_first_error_line);
2424549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_UI(last_error_line, s_last_error_line);
2434549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_STRING(first_error_func, s_first_error_func, 32);
2444549b49fSTheodore Ts'o EXT4_RO_ATTR_ES_STRING(last_error_func, s_last_error_func, 32);
2456a0678a7SArnd Bergmann EXT4_ATTR(first_error_time, 0444, first_error_time);
2466a0678a7SArnd Bergmann EXT4_ATTR(last_error_time, 0444, last_error_time);
247bc1d69d6SKonstantin Khlebnikov EXT4_ATTR(journal_task, 0444, journal_task);
248cfd73237SAlex Zhuravlev EXT4_RW_ATTR_SBI_UI(mb_prefetch, s_mb_prefetch);
249cfd73237SAlex Zhuravlev EXT4_RW_ATTR_SBI_UI(mb_prefetch_limit, s_mb_prefetch_limit);
2504a69aecbSLukas Czerner EXT4_RW_ATTR_SBI_UL(last_trim_minblks, s_last_trim_minblks);
251b5799018STheodore Ts'o
25276d33bcaSTheodore Ts'o static unsigned int old_bump_val = 128;
25376d33bcaSTheodore Ts'o EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
25476d33bcaSTheodore Ts'o
255b5799018STheodore Ts'o static struct attribute *ext4_attrs[] = {
256b5799018STheodore Ts'o ATTR_LIST(delayed_allocation_blocks),
257b5799018STheodore Ts'o ATTR_LIST(session_write_kbytes),
258b5799018STheodore Ts'o ATTR_LIST(lifetime_write_kbytes),
259b5799018STheodore Ts'o ATTR_LIST(reserved_clusters),
260efc61345SEric Whitney ATTR_LIST(sra_exceeded_retry_limit),
261b5799018STheodore Ts'o ATTR_LIST(inode_readahead_blks),
262b5799018STheodore Ts'o ATTR_LIST(inode_goal),
263b5799018STheodore Ts'o ATTR_LIST(mb_stats),
264b5799018STheodore Ts'o ATTR_LIST(mb_max_to_scan),
265b5799018STheodore Ts'o ATTR_LIST(mb_min_to_scan),
266b5799018STheodore Ts'o ATTR_LIST(mb_order2_req),
267b5799018STheodore Ts'o ATTR_LIST(mb_stream_req),
268b5799018STheodore Ts'o ATTR_LIST(mb_group_prealloc),
269196e402aSHarshad Shirwadkar ATTR_LIST(mb_max_linear_groups),
270b5799018STheodore Ts'o ATTR_LIST(max_writeback_mb_bump),
271b5799018STheodore Ts'o ATTR_LIST(extent_max_zeroout_kb),
272b5799018STheodore Ts'o ATTR_LIST(trigger_fs_error),
273b5799018STheodore Ts'o ATTR_LIST(err_ratelimit_interval_ms),
274b5799018STheodore Ts'o ATTR_LIST(err_ratelimit_burst),
275b5799018STheodore Ts'o ATTR_LIST(warning_ratelimit_interval_ms),
276b5799018STheodore Ts'o ATTR_LIST(warning_ratelimit_burst),
277b5799018STheodore Ts'o ATTR_LIST(msg_ratelimit_interval_ms),
278b5799018STheodore Ts'o ATTR_LIST(msg_ratelimit_burst),
279f52f3d2bSOjaswin Mujoo ATTR_LIST(mb_best_avail_max_trim_order),
280b5799018STheodore Ts'o ATTR_LIST(errors_count),
2811cf006edSDmitry Monakhov ATTR_LIST(warning_count),
2821cf006edSDmitry Monakhov ATTR_LIST(msg_count),
2834549b49fSTheodore Ts'o ATTR_LIST(first_error_ino),
2844549b49fSTheodore Ts'o ATTR_LIST(last_error_ino),
2854549b49fSTheodore Ts'o ATTR_LIST(first_error_block),
2864549b49fSTheodore Ts'o ATTR_LIST(last_error_block),
2874549b49fSTheodore Ts'o ATTR_LIST(first_error_line),
2884549b49fSTheodore Ts'o ATTR_LIST(last_error_line),
2894549b49fSTheodore Ts'o ATTR_LIST(first_error_func),
2904549b49fSTheodore Ts'o ATTR_LIST(last_error_func),
2914549b49fSTheodore Ts'o ATTR_LIST(first_error_errcode),
2924549b49fSTheodore Ts'o ATTR_LIST(last_error_errcode),
293b5799018STheodore Ts'o ATTR_LIST(first_error_time),
294b5799018STheodore Ts'o ATTR_LIST(last_error_time),
295bc1d69d6SKonstantin Khlebnikov ATTR_LIST(journal_task),
29646f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
29746f870d6STheodore Ts'o ATTR_LIST(simulate_fail),
29846f870d6STheodore Ts'o #endif
299cfd73237SAlex Zhuravlev ATTR_LIST(mb_prefetch),
300cfd73237SAlex Zhuravlev ATTR_LIST(mb_prefetch_limit),
3014a69aecbSLukas Czerner ATTR_LIST(last_trim_minblks),
302b5799018STheodore Ts'o NULL,
303b5799018STheodore Ts'o };
30478e9605dSKimberly Brown ATTRIBUTE_GROUPS(ext4);
305b5799018STheodore Ts'o
306b5799018STheodore Ts'o /* Features this copy of ext4 supports */
30776d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(lazy_itable_init);
30876d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(batched_discard);
30976d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(meta_bg_resize);
310643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
31176d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(encryption);
312ed318a6cSEric Biggers EXT4_ATTR_FEATURE(test_dummy_encryption_v2);
313c4704a4fSEric Biggers #endif
3145298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
315db90f419STheodore Ts'o EXT4_ATTR_FEATURE(casefold);
316db90f419STheodore Ts'o #endif
317c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
318c93d8f88SEric Biggers EXT4_ATTR_FEATURE(verity);
319c93d8f88SEric Biggers #endif
3208c81bd8fSDarrick J. Wong EXT4_ATTR_FEATURE(metadata_csum_seed);
3216694875eSTheodore Ts'o EXT4_ATTR_FEATURE(fast_commit);
3225298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
323471fbbeaSDaniel Rosenberg EXT4_ATTR_FEATURE(encrypted_casefold);
324e71f99f2SDaniel Rosenberg #endif
325b5799018STheodore Ts'o
326b5799018STheodore Ts'o static struct attribute *ext4_feat_attrs[] = {
327b5799018STheodore Ts'o ATTR_LIST(lazy_itable_init),
328b5799018STheodore Ts'o ATTR_LIST(batched_discard),
329b5799018STheodore Ts'o ATTR_LIST(meta_bg_resize),
330643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
331b5799018STheodore Ts'o ATTR_LIST(encryption),
332ed318a6cSEric Biggers ATTR_LIST(test_dummy_encryption_v2),
333c4704a4fSEric Biggers #endif
3345298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
335db90f419STheodore Ts'o ATTR_LIST(casefold),
336db90f419STheodore Ts'o #endif
337c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
338c93d8f88SEric Biggers ATTR_LIST(verity),
339c93d8f88SEric Biggers #endif
3408c81bd8fSDarrick J. Wong ATTR_LIST(metadata_csum_seed),
3416694875eSTheodore Ts'o ATTR_LIST(fast_commit),
3425298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_FS_ENCRYPTION)
343471fbbeaSDaniel Rosenberg ATTR_LIST(encrypted_casefold),
344e71f99f2SDaniel Rosenberg #endif
345b5799018STheodore Ts'o NULL,
346b5799018STheodore Ts'o };
34778e9605dSKimberly Brown ATTRIBUTE_GROUPS(ext4_feat);
348b5799018STheodore Ts'o
calc_ptr(struct ext4_attr * a,struct ext4_sb_info * sbi)34976d33bcaSTheodore Ts'o static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
35076d33bcaSTheodore Ts'o {
35176d33bcaSTheodore Ts'o switch (a->attr_ptr) {
35276d33bcaSTheodore Ts'o case ptr_explicit:
35376d33bcaSTheodore Ts'o return a->u.explicit_ptr;
35476d33bcaSTheodore Ts'o case ptr_ext4_sb_info_offset:
35576d33bcaSTheodore Ts'o return (void *) (((char *) sbi) + a->u.offset);
35676d33bcaSTheodore Ts'o case ptr_ext4_super_block_offset:
35776d33bcaSTheodore Ts'o return (void *) (((char *) sbi->s_es) + a->u.offset);
35876d33bcaSTheodore Ts'o }
35976d33bcaSTheodore Ts'o return NULL;
36076d33bcaSTheodore Ts'o }
36176d33bcaSTheodore Ts'o
__print_tstamp(char * buf,__le32 lo,__u8 hi)3626a0678a7SArnd Bergmann static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
3636a0678a7SArnd Bergmann {
364dfac1a16SQing Wang return sysfs_emit(buf, "%lld\n",
3656a0678a7SArnd Bergmann ((time64_t)hi << 32) + le32_to_cpu(lo));
3666a0678a7SArnd Bergmann }
3676a0678a7SArnd Bergmann
3686a0678a7SArnd Bergmann #define print_tstamp(buf, es, tstamp) \
3696a0678a7SArnd Bergmann __print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
3706a0678a7SArnd Bergmann
ext4_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)371b5799018STheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
372b5799018STheodore Ts'o struct attribute *attr, char *buf)
373b5799018STheodore Ts'o {
374b5799018STheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
375b5799018STheodore Ts'o s_kobj);
376b5799018STheodore Ts'o struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
37776d33bcaSTheodore Ts'o void *ptr = calc_ptr(a, sbi);
378b5799018STheodore Ts'o
37976d33bcaSTheodore Ts'o switch (a->attr_id) {
38076d33bcaSTheodore Ts'o case attr_delayed_allocation_blocks:
381dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
38276d33bcaSTheodore Ts'o (s64) EXT4_C2B(sbi,
38376d33bcaSTheodore Ts'o percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
38476d33bcaSTheodore Ts'o case attr_session_write_kbytes:
3856ca06829STyson Nottingham return session_write_kbytes_show(sbi, buf);
38676d33bcaSTheodore Ts'o case attr_lifetime_write_kbytes:
3876ca06829STyson Nottingham return lifetime_write_kbytes_show(sbi, buf);
38876d33bcaSTheodore Ts'o case attr_reserved_clusters:
389dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
39076d33bcaSTheodore Ts'o (unsigned long long)
39176d33bcaSTheodore Ts'o atomic64_read(&sbi->s_resv_clusters));
392efc61345SEric Whitney case attr_sra_exceeded_retry_limit:
393dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
394efc61345SEric Whitney (unsigned long long)
395efc61345SEric Whitney percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit));
39676d33bcaSTheodore Ts'o case attr_inode_readahead:
397677ff458SBaokun Li case attr_clusters_in_group:
39876d33bcaSTheodore Ts'o case attr_pointer_ui:
39976d33bcaSTheodore Ts'o if (!ptr)
40076d33bcaSTheodore Ts'o return 0;
401a4d2aadcSArnd Bergmann if (a->attr_ptr == ptr_ext4_super_block_offset)
402dfac1a16SQing Wang return sysfs_emit(buf, "%u\n",
403a4d2aadcSArnd Bergmann le32_to_cpup(ptr));
404a4d2aadcSArnd Bergmann else
405dfac1a16SQing Wang return sysfs_emit(buf, "%u\n",
40676d33bcaSTheodore Ts'o *((unsigned int *) ptr));
40746f870d6STheodore Ts'o case attr_pointer_ul:
40846f870d6STheodore Ts'o if (!ptr)
40946f870d6STheodore Ts'o return 0;
410dfac1a16SQing Wang return sysfs_emit(buf, "%lu\n",
41146f870d6STheodore Ts'o *((unsigned long *) ptr));
4124549b49fSTheodore Ts'o case attr_pointer_u8:
4134549b49fSTheodore Ts'o if (!ptr)
4144549b49fSTheodore Ts'o return 0;
415dfac1a16SQing Wang return sysfs_emit(buf, "%u\n",
4164549b49fSTheodore Ts'o *((unsigned char *) ptr));
4174549b49fSTheodore Ts'o case attr_pointer_u64:
4184549b49fSTheodore Ts'o if (!ptr)
4194549b49fSTheodore Ts'o return 0;
4204549b49fSTheodore Ts'o if (a->attr_ptr == ptr_ext4_super_block_offset)
421dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
4224549b49fSTheodore Ts'o le64_to_cpup(ptr));
4234549b49fSTheodore Ts'o else
424dfac1a16SQing Wang return sysfs_emit(buf, "%llu\n",
4254549b49fSTheodore Ts'o *((unsigned long long *) ptr));
4264549b49fSTheodore Ts'o case attr_pointer_string:
4274549b49fSTheodore Ts'o if (!ptr)
4284549b49fSTheodore Ts'o return 0;
429dfac1a16SQing Wang return sysfs_emit(buf, "%.*s\n", a->attr_size,
4304549b49fSTheodore Ts'o (char *) ptr);
43176d33bcaSTheodore Ts'o case attr_pointer_atomic:
43276d33bcaSTheodore Ts'o if (!ptr)
43376d33bcaSTheodore Ts'o return 0;
434dfac1a16SQing Wang return sysfs_emit(buf, "%d\n",
43576d33bcaSTheodore Ts'o atomic_read((atomic_t *) ptr));
43676d33bcaSTheodore Ts'o case attr_feature:
437dfac1a16SQing Wang return sysfs_emit(buf, "supported\n");
4386a0678a7SArnd Bergmann case attr_first_error_time:
4396a0678a7SArnd Bergmann return print_tstamp(buf, sbi->s_es, s_first_error_time);
4406a0678a7SArnd Bergmann case attr_last_error_time:
4416a0678a7SArnd Bergmann return print_tstamp(buf, sbi->s_es, s_last_error_time);
442bc1d69d6SKonstantin Khlebnikov case attr_journal_task:
443bc1d69d6SKonstantin Khlebnikov return journal_task_show(sbi, buf);
44476d33bcaSTheodore Ts'o }
44576d33bcaSTheodore Ts'o
44676d33bcaSTheodore Ts'o return 0;
447b5799018STheodore Ts'o }
448b5799018STheodore Ts'o
ext4_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)449b5799018STheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
450b5799018STheodore Ts'o struct attribute *attr,
451b5799018STheodore Ts'o const char *buf, size_t len)
452b5799018STheodore Ts'o {
453b5799018STheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
454b5799018STheodore Ts'o s_kobj);
455b5799018STheodore Ts'o struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
45676d33bcaSTheodore Ts'o void *ptr = calc_ptr(a, sbi);
45782d5a4baSBaokun Li unsigned int t;
45882d5a4baSBaokun Li unsigned long lt;
45976d33bcaSTheodore Ts'o int ret;
460b5799018STheodore Ts'o
46176d33bcaSTheodore Ts'o switch (a->attr_id) {
46276d33bcaSTheodore Ts'o case attr_reserved_clusters:
4636ca06829STyson Nottingham return reserved_clusters_store(sbi, buf, len);
46476d33bcaSTheodore Ts'o case attr_pointer_ui:
46576d33bcaSTheodore Ts'o if (!ptr)
46676d33bcaSTheodore Ts'o return 0;
46782d5a4baSBaokun Li ret = kstrtouint(skip_spaces(buf), 0, &t);
46876d33bcaSTheodore Ts'o if (ret)
46976d33bcaSTheodore Ts'o return ret;
470a4d2aadcSArnd Bergmann if (a->attr_ptr == ptr_ext4_super_block_offset)
471a4d2aadcSArnd Bergmann *((__le32 *) ptr) = cpu_to_le32(t);
472a4d2aadcSArnd Bergmann else
47376d33bcaSTheodore Ts'o *((unsigned int *) ptr) = t;
47476d33bcaSTheodore Ts'o return len;
475677ff458SBaokun Li case attr_clusters_in_group:
476*4dc200e3SBaokun Li if (!ptr)
477*4dc200e3SBaokun Li return 0;
478677ff458SBaokun Li ret = kstrtouint(skip_spaces(buf), 0, &t);
479677ff458SBaokun Li if (ret)
480677ff458SBaokun Li return ret;
481677ff458SBaokun Li if (t > sbi->s_clusters_per_group)
482677ff458SBaokun Li return -EINVAL;
483677ff458SBaokun Li *((unsigned int *) ptr) = t;
484677ff458SBaokun Li return len;
48546f870d6STheodore Ts'o case attr_pointer_ul:
48646f870d6STheodore Ts'o if (!ptr)
48746f870d6STheodore Ts'o return 0;
48882d5a4baSBaokun Li ret = kstrtoul(skip_spaces(buf), 0, <);
48946f870d6STheodore Ts'o if (ret)
49046f870d6STheodore Ts'o return ret;
49182d5a4baSBaokun Li *((unsigned long *) ptr) = lt;
49246f870d6STheodore Ts'o return len;
49376d33bcaSTheodore Ts'o case attr_inode_readahead:
4946ca06829STyson Nottingham return inode_readahead_blks_store(sbi, buf, len);
49576d33bcaSTheodore Ts'o case attr_trigger_test_error:
4966ca06829STyson Nottingham return trigger_test_error(sbi, buf, len);
49776d33bcaSTheodore Ts'o }
49876d33bcaSTheodore Ts'o return 0;
499b5799018STheodore Ts'o }
500b5799018STheodore Ts'o
ext4_sb_release(struct kobject * kobj)501b5799018STheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
502b5799018STheodore Ts'o {
503b5799018STheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
504b5799018STheodore Ts'o s_kobj);
505b5799018STheodore Ts'o complete(&sbi->s_kobj_unregister);
506b5799018STheodore Ts'o }
507b5799018STheodore Ts'o
ext4_feat_release(struct kobject * kobj)508d99a55a9SKees Cook static void ext4_feat_release(struct kobject *kobj)
509d99a55a9SKees Cook {
510d99a55a9SKees Cook kfree(kobj);
511d99a55a9SKees Cook }
512d99a55a9SKees Cook
513b5799018STheodore Ts'o static const struct sysfs_ops ext4_attr_ops = {
514b5799018STheodore Ts'o .show = ext4_attr_show,
515b5799018STheodore Ts'o .store = ext4_attr_store,
516b5799018STheodore Ts'o };
517b5799018STheodore Ts'o
51860db00e0SThomas Weißschuh static const struct kobj_type ext4_sb_ktype = {
51978e9605dSKimberly Brown .default_groups = ext4_groups,
520b5799018STheodore Ts'o .sysfs_ops = &ext4_attr_ops,
521b5799018STheodore Ts'o .release = ext4_sb_release,
522b5799018STheodore Ts'o };
523b5799018STheodore Ts'o
52460db00e0SThomas Weißschuh static const struct kobj_type ext4_feat_ktype = {
52578e9605dSKimberly Brown .default_groups = ext4_feat_groups,
52676d33bcaSTheodore Ts'o .sysfs_ops = &ext4_attr_ops,
527d99a55a9SKees Cook .release = ext4_feat_release,
528b5799018STheodore Ts'o };
529b5799018STheodore Ts'o
ext4_notify_error_sysfs(struct ext4_sb_info * sbi)530d578b994SJonathan Davies void ext4_notify_error_sysfs(struct ext4_sb_info *sbi)
531d578b994SJonathan Davies {
532d578b994SJonathan Davies sysfs_notify(&sbi->s_kobj, NULL, "errors_count");
533d578b994SJonathan Davies }
534d578b994SJonathan Davies
535bc1420aeSTyson Nottingham static struct kobject *ext4_root;
536bc1420aeSTyson Nottingham
537b99fee58SRiccardo Schirone static struct kobject *ext4_feat;
538b5799018STheodore Ts'o
ext4_register_sysfs(struct super_block * sb)539b5799018STheodore Ts'o int ext4_register_sysfs(struct super_block *sb)
540b5799018STheodore Ts'o {
541b5799018STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb);
542ebd173beSTheodore Ts'o int err;
543b5799018STheodore Ts'o
544b5799018STheodore Ts'o init_completion(&sbi->s_kobj_unregister);
545bc1420aeSTyson Nottingham err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
546b5799018STheodore Ts'o "%s", sb->s_id);
54795c4df02SRiccardo Schirone if (err) {
54895c4df02SRiccardo Schirone kobject_put(&sbi->s_kobj);
54995c4df02SRiccardo Schirone wait_for_completion(&sbi->s_kobj_unregister);
550ebd173beSTheodore Ts'o return err;
55195c4df02SRiccardo Schirone }
552ebd173beSTheodore Ts'o
553ebd173beSTheodore Ts'o if (ext4_proc_root)
554ebd173beSTheodore Ts'o sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
555ebd173beSTheodore Ts'o if (sbi->s_proc) {
556247dbed8SChristoph Hellwig proc_create_single_data("options", S_IRUGO, sbi->s_proc,
557247dbed8SChristoph Hellwig ext4_seq_options_show, sb);
558247dbed8SChristoph Hellwig proc_create_single_data("es_shrinker_info", S_IRUGO,
559247dbed8SChristoph Hellwig sbi->s_proc, ext4_seq_es_shrinker_info_show,
560247dbed8SChristoph Hellwig sb);
561ce8c59d1SHarshad Shirwadkar proc_create_single_data("fc_info", 0444, sbi->s_proc,
562ce8c59d1SHarshad Shirwadkar ext4_fc_info_show, sb);
563247dbed8SChristoph Hellwig proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
564247dbed8SChristoph Hellwig &ext4_mb_seq_groups_ops, sb);
565a6c75eafSHarshad Shirwadkar proc_create_single_data("mb_stats", 0444, sbi->s_proc,
566a6c75eafSHarshad Shirwadkar ext4_seq_mb_stats_show, sb);
567f68f4063SHarshad Shirwadkar proc_create_seq_data("mb_structs_summary", 0444, sbi->s_proc,
568f68f4063SHarshad Shirwadkar &ext4_mb_seq_structs_summary_ops, sb);
569ebd173beSTheodore Ts'o }
570ebd173beSTheodore Ts'o return 0;
571ebd173beSTheodore Ts'o }
572ebd173beSTheodore Ts'o
ext4_unregister_sysfs(struct super_block * sb)573ebd173beSTheodore Ts'o void ext4_unregister_sysfs(struct super_block *sb)
574ebd173beSTheodore Ts'o {
575ebd173beSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb);
576ebd173beSTheodore Ts'o
577247dbed8SChristoph Hellwig if (sbi->s_proc)
578247dbed8SChristoph Hellwig remove_proc_subtree(sb->s_id, ext4_proc_root);
579ebd173beSTheodore Ts'o kobject_del(&sbi->s_kobj);
580b5799018STheodore Ts'o }
581b5799018STheodore Ts'o
ext4_init_sysfs(void)582b5799018STheodore Ts'o int __init ext4_init_sysfs(void)
583b5799018STheodore Ts'o {
584b5799018STheodore Ts'o int ret;
585b5799018STheodore Ts'o
586bc1420aeSTyson Nottingham ext4_root = kobject_create_and_add("ext4", fs_kobj);
587bc1420aeSTyson Nottingham if (!ext4_root)
5885dc39711SRiccardo Schirone return -ENOMEM;
5895dc39711SRiccardo Schirone
590b99fee58SRiccardo Schirone ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
591b99fee58SRiccardo Schirone if (!ext4_feat) {
592b99fee58SRiccardo Schirone ret = -ENOMEM;
593bc1420aeSTyson Nottingham goto root_err;
59495c4df02SRiccardo Schirone }
595b99fee58SRiccardo Schirone
596b99fee58SRiccardo Schirone ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
597bc1420aeSTyson Nottingham ext4_root, "features");
598b99fee58SRiccardo Schirone if (ret)
599b99fee58SRiccardo Schirone goto feat_err;
600b99fee58SRiccardo Schirone
601b99fee58SRiccardo Schirone ext4_proc_root = proc_mkdir(proc_dirname, NULL);
602b99fee58SRiccardo Schirone return ret;
603b99fee58SRiccardo Schirone
604b99fee58SRiccardo Schirone feat_err:
605b99fee58SRiccardo Schirone kobject_put(ext4_feat);
606c2e5df76STyson Nottingham ext4_feat = NULL;
607bc1420aeSTyson Nottingham root_err:
608bc1420aeSTyson Nottingham kobject_put(ext4_root);
609bc1420aeSTyson Nottingham ext4_root = NULL;
610b5799018STheodore Ts'o return ret;
611b5799018STheodore Ts'o }
612b5799018STheodore Ts'o
ext4_exit_sysfs(void)613b5799018STheodore Ts'o void ext4_exit_sysfs(void)
614b5799018STheodore Ts'o {
615b99fee58SRiccardo Schirone kobject_put(ext4_feat);
616c2e5df76STyson Nottingham ext4_feat = NULL;
617bc1420aeSTyson Nottingham kobject_put(ext4_root);
618bc1420aeSTyson Nottingham ext4_root = NULL;
619ebd173beSTheodore Ts'o remove_proc_entry(proc_dirname, NULL);
620ebd173beSTheodore Ts'o ext4_proc_root = NULL;
621b5799018STheodore Ts'o }
622b5799018STheodore Ts'o
623