xref: /openbmc/linux/fs/ext4/sysfs.c (revision 1cf006ed)
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,
2776d33bcaSTheodore Ts'o 	attr_inode_readahead,
2876d33bcaSTheodore Ts'o 	attr_trigger_test_error,
296a0678a7SArnd Bergmann 	attr_first_error_time,
306a0678a7SArnd Bergmann 	attr_last_error_time,
3176d33bcaSTheodore Ts'o 	attr_feature,
3276d33bcaSTheodore Ts'o 	attr_pointer_ui,
3346f870d6STheodore Ts'o 	attr_pointer_ul,
344549b49fSTheodore Ts'o 	attr_pointer_u64,
354549b49fSTheodore Ts'o 	attr_pointer_u8,
364549b49fSTheodore Ts'o 	attr_pointer_string,
3776d33bcaSTheodore Ts'o 	attr_pointer_atomic,
38bc1d69d6SKonstantin Khlebnikov 	attr_journal_task,
3976d33bcaSTheodore Ts'o } attr_id_t;
4076d33bcaSTheodore Ts'o 
4176d33bcaSTheodore Ts'o typedef enum {
4276d33bcaSTheodore Ts'o 	ptr_explicit,
4376d33bcaSTheodore Ts'o 	ptr_ext4_sb_info_offset,
4476d33bcaSTheodore Ts'o 	ptr_ext4_super_block_offset,
4576d33bcaSTheodore Ts'o } attr_ptr_t;
4676d33bcaSTheodore Ts'o 
47d6006186SEric Biggers static const char proc_dirname[] = "fs/ext4";
48ebd173beSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root;
49ebd173beSTheodore Ts'o 
50b5799018STheodore Ts'o struct ext4_attr {
51b5799018STheodore Ts'o 	struct attribute attr;
5276d33bcaSTheodore Ts'o 	short attr_id;
5376d33bcaSTheodore Ts'o 	short attr_ptr;
544549b49fSTheodore Ts'o 	unsigned short attr_size;
55b5799018STheodore Ts'o 	union {
56b5799018STheodore Ts'o 		int offset;
5776d33bcaSTheodore Ts'o 		void *explicit_ptr;
58b5799018STheodore Ts'o 	} u;
59b5799018STheodore Ts'o };
60b5799018STheodore Ts'o 
616ca06829STyson Nottingham static ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
62b5799018STheodore Ts'o {
63b5799018STheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
64b5799018STheodore Ts'o 
65b5799018STheodore Ts'o 	if (!sb->s_bdev->bd_part)
66b5799018STheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
67b5799018STheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lu\n",
68dbae2c55SMichael Callahan 			(part_stat_read(sb->s_bdev->bd_part,
69dbae2c55SMichael Callahan 					sectors[STAT_WRITE]) -
70b5799018STheodore Ts'o 			 sbi->s_sectors_written_start) >> 1);
71b5799018STheodore Ts'o }
72b5799018STheodore Ts'o 
736ca06829STyson Nottingham static ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
74b5799018STheodore Ts'o {
75b5799018STheodore Ts'o 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
76b5799018STheodore Ts'o 
77b5799018STheodore Ts'o 	if (!sb->s_bdev->bd_part)
78b5799018STheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "0\n");
79b5799018STheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%llu\n",
80b5799018STheodore Ts'o 			(unsigned long long)(sbi->s_kbytes_written +
81dbae2c55SMichael Callahan 			((part_stat_read(sb->s_bdev->bd_part,
82dbae2c55SMichael Callahan 					 sectors[STAT_WRITE]) -
83b5799018STheodore Ts'o 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
84b5799018STheodore Ts'o }
85b5799018STheodore Ts'o 
866ca06829STyson Nottingham static ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
87b5799018STheodore Ts'o 					  const char *buf, size_t count)
88b5799018STheodore Ts'o {
89b5799018STheodore Ts'o 	unsigned long t;
90b5799018STheodore Ts'o 	int ret;
91b5799018STheodore Ts'o 
92b5799018STheodore Ts'o 	ret = kstrtoul(skip_spaces(buf), 0, &t);
93b5799018STheodore Ts'o 	if (ret)
94b5799018STheodore Ts'o 		return ret;
95b5799018STheodore Ts'o 
96b5799018STheodore Ts'o 	if (t && (!is_power_of_2(t) || t > 0x40000000))
97b5799018STheodore Ts'o 		return -EINVAL;
98b5799018STheodore Ts'o 
99b5799018STheodore Ts'o 	sbi->s_inode_readahead_blks = t;
100b5799018STheodore Ts'o 	return count;
101b5799018STheodore Ts'o }
102b5799018STheodore Ts'o 
1036ca06829STyson Nottingham static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
104b5799018STheodore Ts'o 				   const char *buf, size_t count)
105b5799018STheodore Ts'o {
106b5799018STheodore Ts'o 	unsigned long long val;
107b5799018STheodore Ts'o 	ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
108b5799018STheodore Ts'o 				 sbi->s_cluster_bits);
10976d33bcaSTheodore Ts'o 	int ret;
110b5799018STheodore Ts'o 
11176d33bcaSTheodore Ts'o 	ret = kstrtoull(skip_spaces(buf), 0, &val);
1121ea1516fSChao Yu 	if (ret || val >= clusters)
113b5799018STheodore Ts'o 		return -EINVAL;
114b5799018STheodore Ts'o 
115b5799018STheodore Ts'o 	atomic64_set(&sbi->s_resv_clusters, val);
116b5799018STheodore Ts'o 	return count;
117b5799018STheodore Ts'o }
118b5799018STheodore Ts'o 
1196ca06829STyson Nottingham static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
120b5799018STheodore Ts'o 				  const char *buf, size_t count)
121b5799018STheodore Ts'o {
122b5799018STheodore Ts'o 	int len = count;
123b5799018STheodore Ts'o 
124b5799018STheodore Ts'o 	if (!capable(CAP_SYS_ADMIN))
125b5799018STheodore Ts'o 		return -EPERM;
126b5799018STheodore Ts'o 
127b5799018STheodore Ts'o 	if (len && buf[len-1] == '\n')
128b5799018STheodore Ts'o 		len--;
129b5799018STheodore Ts'o 
130b5799018STheodore Ts'o 	if (len)
131b5799018STheodore Ts'o 		ext4_error(sbi->s_sb, "%.*s", len, buf);
132b5799018STheodore Ts'o 	return count;
133b5799018STheodore Ts'o }
134b5799018STheodore Ts'o 
135bc1d69d6SKonstantin Khlebnikov static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
136bc1d69d6SKonstantin Khlebnikov {
137bc1d69d6SKonstantin Khlebnikov 	if (!sbi->s_journal)
138bc1d69d6SKonstantin Khlebnikov 		return snprintf(buf, PAGE_SIZE, "<none>\n");
139bc1d69d6SKonstantin Khlebnikov 	return snprintf(buf, PAGE_SIZE, "%d\n",
140bc1d69d6SKonstantin Khlebnikov 			task_pid_vnr(sbi->s_journal->j_task));
141bc1d69d6SKonstantin Khlebnikov }
142bc1d69d6SKonstantin Khlebnikov 
14376d33bcaSTheodore Ts'o #define EXT4_ATTR(_name,_mode,_id)					\
144b5799018STheodore Ts'o static struct ext4_attr ext4_attr_##_name = {				\
145b5799018STheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },		\
14676d33bcaSTheodore Ts'o 	.attr_id = attr_##_id,						\
14776d33bcaSTheodore Ts'o }
14876d33bcaSTheodore Ts'o 
14976d33bcaSTheodore Ts'o #define EXT4_ATTR_FUNC(_name,_mode)  EXT4_ATTR(_name,_mode,_name)
15076d33bcaSTheodore Ts'o 
15176d33bcaSTheodore Ts'o #define EXT4_ATTR_FEATURE(_name)   EXT4_ATTR(_name, 0444, feature)
15276d33bcaSTheodore Ts'o 
15376d33bcaSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname)	\
15476d33bcaSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
15576d33bcaSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
15676d33bcaSTheodore Ts'o 	.attr_id = attr_##_id,					\
15776d33bcaSTheodore Ts'o 	.attr_ptr = ptr_##_struct##_offset,			\
158b5799018STheodore Ts'o 	.u = {							\
15976d33bcaSTheodore Ts'o 		.offset = offsetof(struct _struct, _elname),\
160b5799018STheodore Ts'o 	},							\
161b5799018STheodore Ts'o }
162b5799018STheodore Ts'o 
1634549b49fSTheodore Ts'o #define EXT4_ATTR_STRING(_name,_mode,_size,_struct,_elname)	\
1644549b49fSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
1654549b49fSTheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
1664549b49fSTheodore Ts'o 	.attr_id = attr_pointer_string,				\
1674549b49fSTheodore Ts'o 	.attr_size = _size,					\
1684549b49fSTheodore Ts'o 	.attr_ptr = ptr_##_struct##_offset,			\
1694549b49fSTheodore Ts'o 	.u = {							\
1704549b49fSTheodore Ts'o 		.offset = offsetof(struct _struct, _elname),\
1714549b49fSTheodore Ts'o 	},							\
1724549b49fSTheodore Ts'o }
1734549b49fSTheodore Ts'o 
17476d33bcaSTheodore Ts'o #define EXT4_RO_ATTR_ES_UI(_name,_elname)				\
17576d33bcaSTheodore Ts'o 	EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
17676d33bcaSTheodore Ts'o 
1774549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_U8(_name,_elname)				\
1784549b49fSTheodore Ts'o 	EXT4_ATTR_OFFSET(_name, 0444, pointer_u8, ext4_super_block, _elname)
1794549b49fSTheodore Ts'o 
1804549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_U64(_name,_elname)				\
1814549b49fSTheodore Ts'o 	EXT4_ATTR_OFFSET(_name, 0444, pointer_u64, ext4_super_block, _elname)
1824549b49fSTheodore Ts'o 
1834549b49fSTheodore Ts'o #define EXT4_RO_ATTR_ES_STRING(_name,_elname,_size)			\
1844549b49fSTheodore Ts'o 	EXT4_ATTR_STRING(_name, 0444, _size, ext4_super_block, _elname)
1854549b49fSTheodore Ts'o 
18676d33bcaSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(_name,_elname)	\
18776d33bcaSTheodore Ts'o 	EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
18876d33bcaSTheodore Ts'o 
18946f870d6STheodore Ts'o #define EXT4_RW_ATTR_SBI_UL(_name,_elname)	\
19046f870d6STheodore Ts'o 	EXT4_ATTR_OFFSET(_name, 0644, pointer_ul, ext4_sb_info, _elname)
19146f870d6STheodore Ts'o 
1921cf006edSDmitry Monakhov #define EXT4_RO_ATTR_SBI_ATOMIC(_name,_elname)	\
1931cf006edSDmitry Monakhov 	EXT4_ATTR_OFFSET(_name, 0444, pointer_atomic, ext4_sb_info, _elname)
1941cf006edSDmitry Monakhov 
19576d33bcaSTheodore Ts'o #define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
196b5799018STheodore Ts'o static struct ext4_attr ext4_attr_##_name = {			\
197b5799018STheodore Ts'o 	.attr = {.name = __stringify(_name), .mode = _mode },	\
19876d33bcaSTheodore Ts'o 	.attr_id = attr_##_id,					\
19976d33bcaSTheodore Ts'o 	.attr_ptr = ptr_explicit,				\
200b5799018STheodore Ts'o 	.u = {							\
20176d33bcaSTheodore Ts'o 		.explicit_ptr = _ptr,				\
202b5799018STheodore Ts'o 	},							\
203b5799018STheodore Ts'o }
204b5799018STheodore Ts'o 
205b5799018STheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr
206b5799018STheodore Ts'o 
20776d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
20876d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(session_write_kbytes, 0444);
20976d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
21076d33bcaSTheodore Ts'o EXT4_ATTR_FUNC(reserved_clusters, 0644);
21176d33bcaSTheodore Ts'o 
21276d33bcaSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
21376d33bcaSTheodore Ts'o 		 ext4_sb_info, s_inode_readahead_blks);
214b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
215b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
216b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
217b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
218b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
219b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
220b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
221b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
22276d33bcaSTheodore Ts'o EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
223b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
224b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
225b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
226b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
227b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
228b5799018STheodore Ts'o EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
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);
250b5799018STheodore Ts'o 
25176d33bcaSTheodore Ts'o static unsigned int old_bump_val = 128;
25276d33bcaSTheodore Ts'o EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
25376d33bcaSTheodore Ts'o 
254b5799018STheodore Ts'o static struct attribute *ext4_attrs[] = {
255b5799018STheodore Ts'o 	ATTR_LIST(delayed_allocation_blocks),
256b5799018STheodore Ts'o 	ATTR_LIST(session_write_kbytes),
257b5799018STheodore Ts'o 	ATTR_LIST(lifetime_write_kbytes),
258b5799018STheodore Ts'o 	ATTR_LIST(reserved_clusters),
259b5799018STheodore Ts'o 	ATTR_LIST(inode_readahead_blks),
260b5799018STheodore Ts'o 	ATTR_LIST(inode_goal),
261b5799018STheodore Ts'o 	ATTR_LIST(mb_stats),
262b5799018STheodore Ts'o 	ATTR_LIST(mb_max_to_scan),
263b5799018STheodore Ts'o 	ATTR_LIST(mb_min_to_scan),
264b5799018STheodore Ts'o 	ATTR_LIST(mb_order2_req),
265b5799018STheodore Ts'o 	ATTR_LIST(mb_stream_req),
266b5799018STheodore Ts'o 	ATTR_LIST(mb_group_prealloc),
267b5799018STheodore Ts'o 	ATTR_LIST(max_writeback_mb_bump),
268b5799018STheodore Ts'o 	ATTR_LIST(extent_max_zeroout_kb),
269b5799018STheodore Ts'o 	ATTR_LIST(trigger_fs_error),
270b5799018STheodore Ts'o 	ATTR_LIST(err_ratelimit_interval_ms),
271b5799018STheodore Ts'o 	ATTR_LIST(err_ratelimit_burst),
272b5799018STheodore Ts'o 	ATTR_LIST(warning_ratelimit_interval_ms),
273b5799018STheodore Ts'o 	ATTR_LIST(warning_ratelimit_burst),
274b5799018STheodore Ts'o 	ATTR_LIST(msg_ratelimit_interval_ms),
275b5799018STheodore Ts'o 	ATTR_LIST(msg_ratelimit_burst),
276b5799018STheodore Ts'o 	ATTR_LIST(errors_count),
2771cf006edSDmitry Monakhov 	ATTR_LIST(warning_count),
2781cf006edSDmitry Monakhov 	ATTR_LIST(msg_count),
2794549b49fSTheodore Ts'o 	ATTR_LIST(first_error_ino),
2804549b49fSTheodore Ts'o 	ATTR_LIST(last_error_ino),
2814549b49fSTheodore Ts'o 	ATTR_LIST(first_error_block),
2824549b49fSTheodore Ts'o 	ATTR_LIST(last_error_block),
2834549b49fSTheodore Ts'o 	ATTR_LIST(first_error_line),
2844549b49fSTheodore Ts'o 	ATTR_LIST(last_error_line),
2854549b49fSTheodore Ts'o 	ATTR_LIST(first_error_func),
2864549b49fSTheodore Ts'o 	ATTR_LIST(last_error_func),
2874549b49fSTheodore Ts'o 	ATTR_LIST(first_error_errcode),
2884549b49fSTheodore Ts'o 	ATTR_LIST(last_error_errcode),
289b5799018STheodore Ts'o 	ATTR_LIST(first_error_time),
290b5799018STheodore Ts'o 	ATTR_LIST(last_error_time),
291bc1d69d6SKonstantin Khlebnikov 	ATTR_LIST(journal_task),
29246f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
29346f870d6STheodore Ts'o 	ATTR_LIST(simulate_fail),
29446f870d6STheodore Ts'o #endif
295cfd73237SAlex Zhuravlev 	ATTR_LIST(mb_prefetch),
296cfd73237SAlex Zhuravlev 	ATTR_LIST(mb_prefetch_limit),
297b5799018STheodore Ts'o 	NULL,
298b5799018STheodore Ts'o };
29978e9605dSKimberly Brown ATTRIBUTE_GROUPS(ext4);
300b5799018STheodore Ts'o 
301b5799018STheodore Ts'o /* Features this copy of ext4 supports */
30276d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(lazy_itable_init);
30376d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(batched_discard);
30476d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(meta_bg_resize);
305643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
30676d33bcaSTheodore Ts'o EXT4_ATTR_FEATURE(encryption);
307ed318a6cSEric Biggers EXT4_ATTR_FEATURE(test_dummy_encryption_v2);
308c4704a4fSEric Biggers #endif
309db90f419STheodore Ts'o #ifdef CONFIG_UNICODE
310db90f419STheodore Ts'o EXT4_ATTR_FEATURE(casefold);
311db90f419STheodore Ts'o #endif
312c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
313c93d8f88SEric Biggers EXT4_ATTR_FEATURE(verity);
314c93d8f88SEric Biggers #endif
3158c81bd8fSDarrick J. Wong EXT4_ATTR_FEATURE(metadata_csum_seed);
316b5799018STheodore Ts'o 
317b5799018STheodore Ts'o static struct attribute *ext4_feat_attrs[] = {
318b5799018STheodore Ts'o 	ATTR_LIST(lazy_itable_init),
319b5799018STheodore Ts'o 	ATTR_LIST(batched_discard),
320b5799018STheodore Ts'o 	ATTR_LIST(meta_bg_resize),
321643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
322b5799018STheodore Ts'o 	ATTR_LIST(encryption),
323ed318a6cSEric Biggers 	ATTR_LIST(test_dummy_encryption_v2),
324c4704a4fSEric Biggers #endif
325db90f419STheodore Ts'o #ifdef CONFIG_UNICODE
326db90f419STheodore Ts'o 	ATTR_LIST(casefold),
327db90f419STheodore Ts'o #endif
328c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY
329c93d8f88SEric Biggers 	ATTR_LIST(verity),
330c93d8f88SEric Biggers #endif
3318c81bd8fSDarrick J. Wong 	ATTR_LIST(metadata_csum_seed),
332b5799018STheodore Ts'o 	NULL,
333b5799018STheodore Ts'o };
33478e9605dSKimberly Brown ATTRIBUTE_GROUPS(ext4_feat);
335b5799018STheodore Ts'o 
33676d33bcaSTheodore Ts'o static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
33776d33bcaSTheodore Ts'o {
33876d33bcaSTheodore Ts'o 	switch (a->attr_ptr) {
33976d33bcaSTheodore Ts'o 	case ptr_explicit:
34076d33bcaSTheodore Ts'o 		return a->u.explicit_ptr;
34176d33bcaSTheodore Ts'o 	case ptr_ext4_sb_info_offset:
34276d33bcaSTheodore Ts'o 		return (void *) (((char *) sbi) + a->u.offset);
34376d33bcaSTheodore Ts'o 	case ptr_ext4_super_block_offset:
34476d33bcaSTheodore Ts'o 		return (void *) (((char *) sbi->s_es) + a->u.offset);
34576d33bcaSTheodore Ts'o 	}
34676d33bcaSTheodore Ts'o 	return NULL;
34776d33bcaSTheodore Ts'o }
34876d33bcaSTheodore Ts'o 
3496a0678a7SArnd Bergmann static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
3506a0678a7SArnd Bergmann {
3514549b49fSTheodore Ts'o 	return snprintf(buf, PAGE_SIZE, "%lld\n",
3526a0678a7SArnd Bergmann 			((time64_t)hi << 32) + le32_to_cpu(lo));
3536a0678a7SArnd Bergmann }
3546a0678a7SArnd Bergmann 
3556a0678a7SArnd Bergmann #define print_tstamp(buf, es, tstamp) \
3566a0678a7SArnd Bergmann 	__print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
3576a0678a7SArnd Bergmann 
358b5799018STheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj,
359b5799018STheodore Ts'o 			      struct attribute *attr, char *buf)
360b5799018STheodore Ts'o {
361b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
362b5799018STheodore Ts'o 						s_kobj);
363b5799018STheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
36476d33bcaSTheodore Ts'o 	void *ptr = calc_ptr(a, sbi);
365b5799018STheodore Ts'o 
36676d33bcaSTheodore Ts'o 	switch (a->attr_id) {
36776d33bcaSTheodore Ts'o 	case attr_delayed_allocation_blocks:
36876d33bcaSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%llu\n",
36976d33bcaSTheodore Ts'o 				(s64) EXT4_C2B(sbi,
37076d33bcaSTheodore Ts'o 		       percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
37176d33bcaSTheodore Ts'o 	case attr_session_write_kbytes:
3726ca06829STyson Nottingham 		return session_write_kbytes_show(sbi, buf);
37376d33bcaSTheodore Ts'o 	case attr_lifetime_write_kbytes:
3746ca06829STyson Nottingham 		return lifetime_write_kbytes_show(sbi, buf);
37576d33bcaSTheodore Ts'o 	case attr_reserved_clusters:
37676d33bcaSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%llu\n",
37776d33bcaSTheodore Ts'o 				(unsigned long long)
37876d33bcaSTheodore Ts'o 				atomic64_read(&sbi->s_resv_clusters));
37976d33bcaSTheodore Ts'o 	case attr_inode_readahead:
38076d33bcaSTheodore Ts'o 	case attr_pointer_ui:
38176d33bcaSTheodore Ts'o 		if (!ptr)
38276d33bcaSTheodore Ts'o 			return 0;
383a4d2aadcSArnd Bergmann 		if (a->attr_ptr == ptr_ext4_super_block_offset)
384a4d2aadcSArnd Bergmann 			return snprintf(buf, PAGE_SIZE, "%u\n",
385a4d2aadcSArnd Bergmann 					le32_to_cpup(ptr));
386a4d2aadcSArnd Bergmann 		else
38776d33bcaSTheodore Ts'o 			return snprintf(buf, PAGE_SIZE, "%u\n",
38876d33bcaSTheodore Ts'o 					*((unsigned int *) ptr));
38946f870d6STheodore Ts'o 	case attr_pointer_ul:
39046f870d6STheodore Ts'o 		if (!ptr)
39146f870d6STheodore Ts'o 			return 0;
39246f870d6STheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%lu\n",
39346f870d6STheodore Ts'o 				*((unsigned long *) ptr));
3944549b49fSTheodore Ts'o 	case attr_pointer_u8:
3954549b49fSTheodore Ts'o 		if (!ptr)
3964549b49fSTheodore Ts'o 			return 0;
3974549b49fSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%u\n",
3984549b49fSTheodore Ts'o 				*((unsigned char *) ptr));
3994549b49fSTheodore Ts'o 	case attr_pointer_u64:
4004549b49fSTheodore Ts'o 		if (!ptr)
4014549b49fSTheodore Ts'o 			return 0;
4024549b49fSTheodore Ts'o 		if (a->attr_ptr == ptr_ext4_super_block_offset)
4034549b49fSTheodore Ts'o 			return snprintf(buf, PAGE_SIZE, "%llu\n",
4044549b49fSTheodore Ts'o 					le64_to_cpup(ptr));
4054549b49fSTheodore Ts'o 		else
4064549b49fSTheodore Ts'o 			return snprintf(buf, PAGE_SIZE, "%llu\n",
4074549b49fSTheodore Ts'o 					*((unsigned long long *) ptr));
4084549b49fSTheodore Ts'o 	case attr_pointer_string:
4094549b49fSTheodore Ts'o 		if (!ptr)
4104549b49fSTheodore Ts'o 			return 0;
4114549b49fSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%.*s\n", a->attr_size,
4124549b49fSTheodore Ts'o 				(char *) ptr);
41376d33bcaSTheodore Ts'o 	case attr_pointer_atomic:
41476d33bcaSTheodore Ts'o 		if (!ptr)
41576d33bcaSTheodore Ts'o 			return 0;
41676d33bcaSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "%d\n",
41776d33bcaSTheodore Ts'o 				atomic_read((atomic_t *) ptr));
41876d33bcaSTheodore Ts'o 	case attr_feature:
41976d33bcaSTheodore Ts'o 		return snprintf(buf, PAGE_SIZE, "supported\n");
4206a0678a7SArnd Bergmann 	case attr_first_error_time:
4216a0678a7SArnd Bergmann 		return print_tstamp(buf, sbi->s_es, s_first_error_time);
4226a0678a7SArnd Bergmann 	case attr_last_error_time:
4236a0678a7SArnd Bergmann 		return print_tstamp(buf, sbi->s_es, s_last_error_time);
424bc1d69d6SKonstantin Khlebnikov 	case attr_journal_task:
425bc1d69d6SKonstantin Khlebnikov 		return journal_task_show(sbi, buf);
42676d33bcaSTheodore Ts'o 	}
42776d33bcaSTheodore Ts'o 
42876d33bcaSTheodore Ts'o 	return 0;
429b5799018STheodore Ts'o }
430b5799018STheodore Ts'o 
431b5799018STheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj,
432b5799018STheodore Ts'o 			       struct attribute *attr,
433b5799018STheodore Ts'o 			       const char *buf, size_t len)
434b5799018STheodore Ts'o {
435b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
436b5799018STheodore Ts'o 						s_kobj);
437b5799018STheodore Ts'o 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
43876d33bcaSTheodore Ts'o 	void *ptr = calc_ptr(a, sbi);
43976d33bcaSTheodore Ts'o 	unsigned long t;
44076d33bcaSTheodore Ts'o 	int ret;
441b5799018STheodore Ts'o 
44276d33bcaSTheodore Ts'o 	switch (a->attr_id) {
44376d33bcaSTheodore Ts'o 	case attr_reserved_clusters:
4446ca06829STyson Nottingham 		return reserved_clusters_store(sbi, buf, len);
44576d33bcaSTheodore Ts'o 	case attr_pointer_ui:
44676d33bcaSTheodore Ts'o 		if (!ptr)
44776d33bcaSTheodore Ts'o 			return 0;
44876d33bcaSTheodore Ts'o 		ret = kstrtoul(skip_spaces(buf), 0, &t);
44976d33bcaSTheodore Ts'o 		if (ret)
45076d33bcaSTheodore Ts'o 			return ret;
451a4d2aadcSArnd Bergmann 		if (a->attr_ptr == ptr_ext4_super_block_offset)
452a4d2aadcSArnd Bergmann 			*((__le32 *) ptr) = cpu_to_le32(t);
453a4d2aadcSArnd Bergmann 		else
45476d33bcaSTheodore Ts'o 			*((unsigned int *) ptr) = t;
45576d33bcaSTheodore Ts'o 		return len;
45646f870d6STheodore Ts'o 	case attr_pointer_ul:
45746f870d6STheodore Ts'o 		if (!ptr)
45846f870d6STheodore Ts'o 			return 0;
45946f870d6STheodore Ts'o 		ret = kstrtoul(skip_spaces(buf), 0, &t);
46046f870d6STheodore Ts'o 		if (ret)
46146f870d6STheodore Ts'o 			return ret;
46246f870d6STheodore Ts'o 		*((unsigned long *) ptr) = t;
46346f870d6STheodore Ts'o 		return len;
46476d33bcaSTheodore Ts'o 	case attr_inode_readahead:
4656ca06829STyson Nottingham 		return inode_readahead_blks_store(sbi, buf, len);
46676d33bcaSTheodore Ts'o 	case attr_trigger_test_error:
4676ca06829STyson Nottingham 		return trigger_test_error(sbi, buf, len);
46876d33bcaSTheodore Ts'o 	}
46976d33bcaSTheodore Ts'o 	return 0;
470b5799018STheodore Ts'o }
471b5799018STheodore Ts'o 
472b5799018STheodore Ts'o static void ext4_sb_release(struct kobject *kobj)
473b5799018STheodore Ts'o {
474b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
475b5799018STheodore Ts'o 						s_kobj);
476b5799018STheodore Ts'o 	complete(&sbi->s_kobj_unregister);
477b5799018STheodore Ts'o }
478b5799018STheodore Ts'o 
479b5799018STheodore Ts'o static const struct sysfs_ops ext4_attr_ops = {
480b5799018STheodore Ts'o 	.show	= ext4_attr_show,
481b5799018STheodore Ts'o 	.store	= ext4_attr_store,
482b5799018STheodore Ts'o };
483b5799018STheodore Ts'o 
484b5799018STheodore Ts'o static struct kobj_type ext4_sb_ktype = {
48578e9605dSKimberly Brown 	.default_groups = ext4_groups,
486b5799018STheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
487b5799018STheodore Ts'o 	.release	= ext4_sb_release,
488b5799018STheodore Ts'o };
489b5799018STheodore Ts'o 
490b5799018STheodore Ts'o static struct kobj_type ext4_feat_ktype = {
49178e9605dSKimberly Brown 	.default_groups = ext4_feat_groups,
49276d33bcaSTheodore Ts'o 	.sysfs_ops	= &ext4_attr_ops,
493b99fee58SRiccardo Schirone 	.release	= (void (*)(struct kobject *))kfree,
494b5799018STheodore Ts'o };
495b5799018STheodore Ts'o 
496bc1420aeSTyson Nottingham static struct kobject *ext4_root;
497bc1420aeSTyson Nottingham 
498b99fee58SRiccardo Schirone static struct kobject *ext4_feat;
499b5799018STheodore Ts'o 
500b5799018STheodore Ts'o int ext4_register_sysfs(struct super_block *sb)
501b5799018STheodore Ts'o {
502b5799018STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
503ebd173beSTheodore Ts'o 	int err;
504b5799018STheodore Ts'o 
505b5799018STheodore Ts'o 	init_completion(&sbi->s_kobj_unregister);
506bc1420aeSTyson Nottingham 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
507b5799018STheodore Ts'o 				   "%s", sb->s_id);
50895c4df02SRiccardo Schirone 	if (err) {
50995c4df02SRiccardo Schirone 		kobject_put(&sbi->s_kobj);
51095c4df02SRiccardo Schirone 		wait_for_completion(&sbi->s_kobj_unregister);
511ebd173beSTheodore Ts'o 		return err;
51295c4df02SRiccardo Schirone 	}
513ebd173beSTheodore Ts'o 
514ebd173beSTheodore Ts'o 	if (ext4_proc_root)
515ebd173beSTheodore Ts'o 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
516ebd173beSTheodore Ts'o 	if (sbi->s_proc) {
517247dbed8SChristoph Hellwig 		proc_create_single_data("options", S_IRUGO, sbi->s_proc,
518247dbed8SChristoph Hellwig 				ext4_seq_options_show, sb);
519247dbed8SChristoph Hellwig 		proc_create_single_data("es_shrinker_info", S_IRUGO,
520247dbed8SChristoph Hellwig 				sbi->s_proc, ext4_seq_es_shrinker_info_show,
521247dbed8SChristoph Hellwig 				sb);
522247dbed8SChristoph Hellwig 		proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
523247dbed8SChristoph Hellwig 				&ext4_mb_seq_groups_ops, sb);
524ebd173beSTheodore Ts'o 	}
525ebd173beSTheodore Ts'o 	return 0;
526ebd173beSTheodore Ts'o }
527ebd173beSTheodore Ts'o 
528ebd173beSTheodore Ts'o void ext4_unregister_sysfs(struct super_block *sb)
529ebd173beSTheodore Ts'o {
530ebd173beSTheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
531ebd173beSTheodore Ts'o 
532247dbed8SChristoph Hellwig 	if (sbi->s_proc)
533247dbed8SChristoph Hellwig 		remove_proc_subtree(sb->s_id, ext4_proc_root);
534ebd173beSTheodore Ts'o 	kobject_del(&sbi->s_kobj);
535b5799018STheodore Ts'o }
536b5799018STheodore Ts'o 
537b5799018STheodore Ts'o int __init ext4_init_sysfs(void)
538b5799018STheodore Ts'o {
539b5799018STheodore Ts'o 	int ret;
540b5799018STheodore Ts'o 
541bc1420aeSTyson Nottingham 	ext4_root = kobject_create_and_add("ext4", fs_kobj);
542bc1420aeSTyson Nottingham 	if (!ext4_root)
5435dc39711SRiccardo Schirone 		return -ENOMEM;
5445dc39711SRiccardo Schirone 
545b99fee58SRiccardo Schirone 	ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
546b99fee58SRiccardo Schirone 	if (!ext4_feat) {
547b99fee58SRiccardo Schirone 		ret = -ENOMEM;
548bc1420aeSTyson Nottingham 		goto root_err;
54995c4df02SRiccardo Schirone 	}
550b99fee58SRiccardo Schirone 
551b99fee58SRiccardo Schirone 	ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
552bc1420aeSTyson Nottingham 				   ext4_root, "features");
553b99fee58SRiccardo Schirone 	if (ret)
554b99fee58SRiccardo Schirone 		goto feat_err;
555b99fee58SRiccardo Schirone 
556b99fee58SRiccardo Schirone 	ext4_proc_root = proc_mkdir(proc_dirname, NULL);
557b99fee58SRiccardo Schirone 	return ret;
558b99fee58SRiccardo Schirone 
559b99fee58SRiccardo Schirone feat_err:
560b99fee58SRiccardo Schirone 	kobject_put(ext4_feat);
561c2e5df76STyson Nottingham 	ext4_feat = NULL;
562bc1420aeSTyson Nottingham root_err:
563bc1420aeSTyson Nottingham 	kobject_put(ext4_root);
564bc1420aeSTyson Nottingham 	ext4_root = NULL;
565b5799018STheodore Ts'o 	return ret;
566b5799018STheodore Ts'o }
567b5799018STheodore Ts'o 
568b5799018STheodore Ts'o void ext4_exit_sysfs(void)
569b5799018STheodore Ts'o {
570b99fee58SRiccardo Schirone 	kobject_put(ext4_feat);
571c2e5df76STyson Nottingham 	ext4_feat = NULL;
572bc1420aeSTyson Nottingham 	kobject_put(ext4_root);
573bc1420aeSTyson Nottingham 	ext4_root = NULL;
574ebd173beSTheodore Ts'o 	remove_proc_entry(proc_dirname, NULL);
575ebd173beSTheodore Ts'o 	ext4_proc_root = NULL;
576b5799018STheodore Ts'o }
577b5799018STheodore Ts'o 
578