18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/fs/ext4/sysfs.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 1992, 1993, 1994, 1995
68c2ecf20Sopenharmony_ci * Remy Card (card@masi.ibp.fr)
78c2ecf20Sopenharmony_ci * Theodore Ts'o (tytso@mit.edu)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/time.h>
128c2ecf20Sopenharmony_ci#include <linux/fs.h>
138c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
168c2ecf20Sopenharmony_ci#include <linux/part_stat.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "ext4.h"
198c2ecf20Sopenharmony_ci#include "ext4_jbd2.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_citypedef enum {
228c2ecf20Sopenharmony_ci	attr_noop,
238c2ecf20Sopenharmony_ci	attr_delayed_allocation_blocks,
248c2ecf20Sopenharmony_ci	attr_session_write_kbytes,
258c2ecf20Sopenharmony_ci	attr_lifetime_write_kbytes,
268c2ecf20Sopenharmony_ci	attr_reserved_clusters,
278c2ecf20Sopenharmony_ci	attr_sra_exceeded_retry_limit,
288c2ecf20Sopenharmony_ci	attr_inode_readahead,
298c2ecf20Sopenharmony_ci	attr_trigger_test_error,
308c2ecf20Sopenharmony_ci	attr_first_error_time,
318c2ecf20Sopenharmony_ci	attr_last_error_time,
328c2ecf20Sopenharmony_ci	attr_feature,
338c2ecf20Sopenharmony_ci	attr_pointer_ui,
348c2ecf20Sopenharmony_ci	attr_pointer_ul,
358c2ecf20Sopenharmony_ci	attr_pointer_u64,
368c2ecf20Sopenharmony_ci	attr_pointer_u8,
378c2ecf20Sopenharmony_ci	attr_pointer_string,
388c2ecf20Sopenharmony_ci	attr_pointer_atomic,
398c2ecf20Sopenharmony_ci	attr_journal_task,
408c2ecf20Sopenharmony_ci} attr_id_t;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_citypedef enum {
438c2ecf20Sopenharmony_ci	ptr_explicit,
448c2ecf20Sopenharmony_ci	ptr_ext4_sb_info_offset,
458c2ecf20Sopenharmony_ci	ptr_ext4_super_block_offset,
468c2ecf20Sopenharmony_ci} attr_ptr_t;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic const char proc_dirname[] = "fs/ext4";
498c2ecf20Sopenharmony_cistatic struct proc_dir_entry *ext4_proc_root;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct ext4_attr {
528c2ecf20Sopenharmony_ci	struct attribute attr;
538c2ecf20Sopenharmony_ci	short attr_id;
548c2ecf20Sopenharmony_ci	short attr_ptr;
558c2ecf20Sopenharmony_ci	unsigned short attr_size;
568c2ecf20Sopenharmony_ci	union {
578c2ecf20Sopenharmony_ci		int offset;
588c2ecf20Sopenharmony_ci		void *explicit_ptr;
598c2ecf20Sopenharmony_ci	} u;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct super_block *sb = sbi->s_buddy_cache->i_sb;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	if (!sb->s_bdev->bd_part)
678c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "0\n");
688c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%lu\n",
698c2ecf20Sopenharmony_ci			(part_stat_read(sb->s_bdev->bd_part,
708c2ecf20Sopenharmony_ci					sectors[STAT_WRITE]) -
718c2ecf20Sopenharmony_ci			 sbi->s_sectors_written_start) >> 1);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct super_block *sb = sbi->s_buddy_cache->i_sb;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (!sb->s_bdev->bd_part)
798c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "0\n");
808c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%llu\n",
818c2ecf20Sopenharmony_ci			(unsigned long long)(sbi->s_kbytes_written +
828c2ecf20Sopenharmony_ci			((part_stat_read(sb->s_bdev->bd_part,
838c2ecf20Sopenharmony_ci					 sectors[STAT_WRITE]) -
848c2ecf20Sopenharmony_ci			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
888c2ecf20Sopenharmony_ci					  const char *buf, size_t count)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	unsigned long t;
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = kstrtoul(skip_spaces(buf), 0, &t);
948c2ecf20Sopenharmony_ci	if (ret)
958c2ecf20Sopenharmony_ci		return ret;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (t && (!is_power_of_2(t) || t > 0x40000000))
988c2ecf20Sopenharmony_ci		return -EINVAL;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	sbi->s_inode_readahead_blks = t;
1018c2ecf20Sopenharmony_ci	return count;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
1058c2ecf20Sopenharmony_ci				   const char *buf, size_t count)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	unsigned long long val;
1088c2ecf20Sopenharmony_ci	ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
1098c2ecf20Sopenharmony_ci				 sbi->s_cluster_bits);
1108c2ecf20Sopenharmony_ci	int ret;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	ret = kstrtoull(skip_spaces(buf), 0, &val);
1138c2ecf20Sopenharmony_ci	if (ret || val >= clusters)
1148c2ecf20Sopenharmony_ci		return -EINVAL;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	atomic64_set(&sbi->s_resv_clusters, val);
1178c2ecf20Sopenharmony_ci	return count;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic ssize_t trigger_test_error(struct ext4_sb_info *sbi,
1218c2ecf20Sopenharmony_ci				  const char *buf, size_t count)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	int len = count;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
1268c2ecf20Sopenharmony_ci		return -EPERM;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (len && buf[len-1] == '\n')
1298c2ecf20Sopenharmony_ci		len--;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (len)
1328c2ecf20Sopenharmony_ci		ext4_error(sbi->s_sb, "%.*s", len, buf);
1338c2ecf20Sopenharmony_ci	return count;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	if (!sbi->s_journal)
1398c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "<none>\n");
1408c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n",
1418c2ecf20Sopenharmony_ci			task_pid_vnr(sbi->s_journal->j_task));
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define EXT4_ATTR(_name,_mode,_id)					\
1458c2ecf20Sopenharmony_cistatic struct ext4_attr ext4_attr_##_name = {				\
1468c2ecf20Sopenharmony_ci	.attr = {.name = __stringify(_name), .mode = _mode },		\
1478c2ecf20Sopenharmony_ci	.attr_id = attr_##_id,						\
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#define EXT4_ATTR_FUNC(_name,_mode)  EXT4_ATTR(_name,_mode,_name)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define EXT4_ATTR_FEATURE(_name)   EXT4_ATTR(_name, 0444, feature)
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname)	\
1558c2ecf20Sopenharmony_cistatic struct ext4_attr ext4_attr_##_name = {			\
1568c2ecf20Sopenharmony_ci	.attr = {.name = __stringify(_name), .mode = _mode },	\
1578c2ecf20Sopenharmony_ci	.attr_id = attr_##_id,					\
1588c2ecf20Sopenharmony_ci	.attr_ptr = ptr_##_struct##_offset,			\
1598c2ecf20Sopenharmony_ci	.u = {							\
1608c2ecf20Sopenharmony_ci		.offset = offsetof(struct _struct, _elname),\
1618c2ecf20Sopenharmony_ci	},							\
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define EXT4_ATTR_STRING(_name,_mode,_size,_struct,_elname)	\
1658c2ecf20Sopenharmony_cistatic struct ext4_attr ext4_attr_##_name = {			\
1668c2ecf20Sopenharmony_ci	.attr = {.name = __stringify(_name), .mode = _mode },	\
1678c2ecf20Sopenharmony_ci	.attr_id = attr_pointer_string,				\
1688c2ecf20Sopenharmony_ci	.attr_size = _size,					\
1698c2ecf20Sopenharmony_ci	.attr_ptr = ptr_##_struct##_offset,			\
1708c2ecf20Sopenharmony_ci	.u = {							\
1718c2ecf20Sopenharmony_ci		.offset = offsetof(struct _struct, _elname),\
1728c2ecf20Sopenharmony_ci	},							\
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define EXT4_RO_ATTR_ES_UI(_name,_elname)				\
1768c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci#define EXT4_RO_ATTR_ES_U8(_name,_elname)				\
1798c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0444, pointer_u8, ext4_super_block, _elname)
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci#define EXT4_RO_ATTR_ES_U64(_name,_elname)				\
1828c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0444, pointer_u64, ext4_super_block, _elname)
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci#define EXT4_RO_ATTR_ES_STRING(_name,_elname,_size)			\
1858c2ecf20Sopenharmony_ci	EXT4_ATTR_STRING(_name, 0444, _size, ext4_super_block, _elname)
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#define EXT4_RW_ATTR_SBI_UI(_name,_elname)	\
1888c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci#define EXT4_RW_ATTR_SBI_UL(_name,_elname)	\
1918c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0644, pointer_ul, ext4_sb_info, _elname)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define EXT4_RO_ATTR_SBI_ATOMIC(_name,_elname)	\
1948c2ecf20Sopenharmony_ci	EXT4_ATTR_OFFSET(_name, 0444, pointer_atomic, ext4_sb_info, _elname)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
1978c2ecf20Sopenharmony_cistatic struct ext4_attr ext4_attr_##_name = {			\
1988c2ecf20Sopenharmony_ci	.attr = {.name = __stringify(_name), .mode = _mode },	\
1998c2ecf20Sopenharmony_ci	.attr_id = attr_##_id,					\
2008c2ecf20Sopenharmony_ci	.attr_ptr = ptr_explicit,				\
2018c2ecf20Sopenharmony_ci	.u = {							\
2028c2ecf20Sopenharmony_ci		.explicit_ptr = _ptr,				\
2038c2ecf20Sopenharmony_ci	},							\
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#define ATTR_LIST(name) &ext4_attr_##name.attr
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciEXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
2098c2ecf20Sopenharmony_ciEXT4_ATTR_FUNC(session_write_kbytes, 0444);
2108c2ecf20Sopenharmony_ciEXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
2118c2ecf20Sopenharmony_ciEXT4_ATTR_FUNC(reserved_clusters, 0644);
2128c2ecf20Sopenharmony_ciEXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciEXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
2158c2ecf20Sopenharmony_ci		 ext4_sb_info, s_inode_readahead_blks);
2168c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
2178c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2188c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2198c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2208c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2218c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2228c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2238c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_max_inode_prealloc, s_mb_max_inode_prealloc);
2248c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
2258c2ecf20Sopenharmony_ciEXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
2268c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
2278c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
2288c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
2298c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
2308c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
2318c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
2328c2ecf20Sopenharmony_ci#ifdef CONFIG_EXT4_DEBUG
2338c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UL(simulate_fail, s_simulate_fail);
2348c2ecf20Sopenharmony_ci#endif
2358c2ecf20Sopenharmony_ciEXT4_RO_ATTR_SBI_ATOMIC(warning_count, s_warning_count);
2368c2ecf20Sopenharmony_ciEXT4_RO_ATTR_SBI_ATOMIC(msg_count, s_msg_count);
2378c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
2388c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_U8(first_error_errcode, s_first_error_errcode);
2398c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_U8(last_error_errcode, s_last_error_errcode);
2408c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_UI(first_error_ino, s_first_error_ino);
2418c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_UI(last_error_ino, s_last_error_ino);
2428c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_U64(first_error_block, s_first_error_block);
2438c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_U64(last_error_block, s_last_error_block);
2448c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_UI(first_error_line, s_first_error_line);
2458c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_UI(last_error_line, s_last_error_line);
2468c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_STRING(first_error_func, s_first_error_func, 32);
2478c2ecf20Sopenharmony_ciEXT4_RO_ATTR_ES_STRING(last_error_func, s_last_error_func, 32);
2488c2ecf20Sopenharmony_ciEXT4_ATTR(first_error_time, 0444, first_error_time);
2498c2ecf20Sopenharmony_ciEXT4_ATTR(last_error_time, 0444, last_error_time);
2508c2ecf20Sopenharmony_ciEXT4_ATTR(journal_task, 0444, journal_task);
2518c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_prefetch, s_mb_prefetch);
2528c2ecf20Sopenharmony_ciEXT4_RW_ATTR_SBI_UI(mb_prefetch_limit, s_mb_prefetch_limit);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic unsigned int old_bump_val = 128;
2558c2ecf20Sopenharmony_ciEXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic struct attribute *ext4_attrs[] = {
2588c2ecf20Sopenharmony_ci	ATTR_LIST(delayed_allocation_blocks),
2598c2ecf20Sopenharmony_ci	ATTR_LIST(session_write_kbytes),
2608c2ecf20Sopenharmony_ci	ATTR_LIST(lifetime_write_kbytes),
2618c2ecf20Sopenharmony_ci	ATTR_LIST(reserved_clusters),
2628c2ecf20Sopenharmony_ci	ATTR_LIST(sra_exceeded_retry_limit),
2638c2ecf20Sopenharmony_ci	ATTR_LIST(inode_readahead_blks),
2648c2ecf20Sopenharmony_ci	ATTR_LIST(inode_goal),
2658c2ecf20Sopenharmony_ci	ATTR_LIST(mb_stats),
2668c2ecf20Sopenharmony_ci	ATTR_LIST(mb_max_to_scan),
2678c2ecf20Sopenharmony_ci	ATTR_LIST(mb_min_to_scan),
2688c2ecf20Sopenharmony_ci	ATTR_LIST(mb_order2_req),
2698c2ecf20Sopenharmony_ci	ATTR_LIST(mb_stream_req),
2708c2ecf20Sopenharmony_ci	ATTR_LIST(mb_group_prealloc),
2718c2ecf20Sopenharmony_ci	ATTR_LIST(mb_max_inode_prealloc),
2728c2ecf20Sopenharmony_ci	ATTR_LIST(max_writeback_mb_bump),
2738c2ecf20Sopenharmony_ci	ATTR_LIST(extent_max_zeroout_kb),
2748c2ecf20Sopenharmony_ci	ATTR_LIST(trigger_fs_error),
2758c2ecf20Sopenharmony_ci	ATTR_LIST(err_ratelimit_interval_ms),
2768c2ecf20Sopenharmony_ci	ATTR_LIST(err_ratelimit_burst),
2778c2ecf20Sopenharmony_ci	ATTR_LIST(warning_ratelimit_interval_ms),
2788c2ecf20Sopenharmony_ci	ATTR_LIST(warning_ratelimit_burst),
2798c2ecf20Sopenharmony_ci	ATTR_LIST(msg_ratelimit_interval_ms),
2808c2ecf20Sopenharmony_ci	ATTR_LIST(msg_ratelimit_burst),
2818c2ecf20Sopenharmony_ci	ATTR_LIST(errors_count),
2828c2ecf20Sopenharmony_ci	ATTR_LIST(warning_count),
2838c2ecf20Sopenharmony_ci	ATTR_LIST(msg_count),
2848c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_ino),
2858c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_ino),
2868c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_block),
2878c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_block),
2888c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_line),
2898c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_line),
2908c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_func),
2918c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_func),
2928c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_errcode),
2938c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_errcode),
2948c2ecf20Sopenharmony_ci	ATTR_LIST(first_error_time),
2958c2ecf20Sopenharmony_ci	ATTR_LIST(last_error_time),
2968c2ecf20Sopenharmony_ci	ATTR_LIST(journal_task),
2978c2ecf20Sopenharmony_ci#ifdef CONFIG_EXT4_DEBUG
2988c2ecf20Sopenharmony_ci	ATTR_LIST(simulate_fail),
2998c2ecf20Sopenharmony_ci#endif
3008c2ecf20Sopenharmony_ci	ATTR_LIST(mb_prefetch),
3018c2ecf20Sopenharmony_ci	ATTR_LIST(mb_prefetch_limit),
3028c2ecf20Sopenharmony_ci	NULL,
3038c2ecf20Sopenharmony_ci};
3048c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(ext4);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci/* Features this copy of ext4 supports */
3078c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(lazy_itable_init);
3088c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(batched_discard);
3098c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(meta_bg_resize);
3108c2ecf20Sopenharmony_ci#ifdef CONFIG_FS_ENCRYPTION
3118c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(encryption);
3128c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(test_dummy_encryption_v2);
3138c2ecf20Sopenharmony_ci#endif
3148c2ecf20Sopenharmony_ci#ifdef CONFIG_UNICODE
3158c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(casefold);
3168c2ecf20Sopenharmony_ci#endif
3178c2ecf20Sopenharmony_ci#ifdef CONFIG_FS_VERITY
3188c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(verity);
3198c2ecf20Sopenharmony_ci#endif
3208c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(metadata_csum_seed);
3218c2ecf20Sopenharmony_ciEXT4_ATTR_FEATURE(fast_commit);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic struct attribute *ext4_feat_attrs[] = {
3248c2ecf20Sopenharmony_ci	ATTR_LIST(lazy_itable_init),
3258c2ecf20Sopenharmony_ci	ATTR_LIST(batched_discard),
3268c2ecf20Sopenharmony_ci	ATTR_LIST(meta_bg_resize),
3278c2ecf20Sopenharmony_ci#ifdef CONFIG_FS_ENCRYPTION
3288c2ecf20Sopenharmony_ci	ATTR_LIST(encryption),
3298c2ecf20Sopenharmony_ci	ATTR_LIST(test_dummy_encryption_v2),
3308c2ecf20Sopenharmony_ci#endif
3318c2ecf20Sopenharmony_ci#ifdef CONFIG_UNICODE
3328c2ecf20Sopenharmony_ci	ATTR_LIST(casefold),
3338c2ecf20Sopenharmony_ci#endif
3348c2ecf20Sopenharmony_ci#ifdef CONFIG_FS_VERITY
3358c2ecf20Sopenharmony_ci	ATTR_LIST(verity),
3368c2ecf20Sopenharmony_ci#endif
3378c2ecf20Sopenharmony_ci	ATTR_LIST(metadata_csum_seed),
3388c2ecf20Sopenharmony_ci	ATTR_LIST(fast_commit),
3398c2ecf20Sopenharmony_ci	NULL,
3408c2ecf20Sopenharmony_ci};
3418c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(ext4_feat);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	switch (a->attr_ptr) {
3468c2ecf20Sopenharmony_ci	case ptr_explicit:
3478c2ecf20Sopenharmony_ci		return a->u.explicit_ptr;
3488c2ecf20Sopenharmony_ci	case ptr_ext4_sb_info_offset:
3498c2ecf20Sopenharmony_ci		return (void *) (((char *) sbi) + a->u.offset);
3508c2ecf20Sopenharmony_ci	case ptr_ext4_super_block_offset:
3518c2ecf20Sopenharmony_ci		return (void *) (((char *) sbi->s_es) + a->u.offset);
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci	return NULL;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%lld\n",
3598c2ecf20Sopenharmony_ci			((time64_t)hi << 32) + le32_to_cpu(lo));
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci#define print_tstamp(buf, es, tstamp) \
3638c2ecf20Sopenharmony_ci	__print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic ssize_t ext4_attr_show(struct kobject *kobj,
3668c2ecf20Sopenharmony_ci			      struct attribute *attr, char *buf)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
3698c2ecf20Sopenharmony_ci						s_kobj);
3708c2ecf20Sopenharmony_ci	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
3718c2ecf20Sopenharmony_ci	void *ptr = calc_ptr(a, sbi);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	switch (a->attr_id) {
3748c2ecf20Sopenharmony_ci	case attr_delayed_allocation_blocks:
3758c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%llu\n",
3768c2ecf20Sopenharmony_ci				(s64) EXT4_C2B(sbi,
3778c2ecf20Sopenharmony_ci		       percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
3788c2ecf20Sopenharmony_ci	case attr_session_write_kbytes:
3798c2ecf20Sopenharmony_ci		return session_write_kbytes_show(sbi, buf);
3808c2ecf20Sopenharmony_ci	case attr_lifetime_write_kbytes:
3818c2ecf20Sopenharmony_ci		return lifetime_write_kbytes_show(sbi, buf);
3828c2ecf20Sopenharmony_ci	case attr_reserved_clusters:
3838c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%llu\n",
3848c2ecf20Sopenharmony_ci				(unsigned long long)
3858c2ecf20Sopenharmony_ci				atomic64_read(&sbi->s_resv_clusters));
3868c2ecf20Sopenharmony_ci	case attr_sra_exceeded_retry_limit:
3878c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%llu\n",
3888c2ecf20Sopenharmony_ci				(unsigned long long)
3898c2ecf20Sopenharmony_ci			percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit));
3908c2ecf20Sopenharmony_ci	case attr_inode_readahead:
3918c2ecf20Sopenharmony_ci	case attr_pointer_ui:
3928c2ecf20Sopenharmony_ci		if (!ptr)
3938c2ecf20Sopenharmony_ci			return 0;
3948c2ecf20Sopenharmony_ci		if (a->attr_ptr == ptr_ext4_super_block_offset)
3958c2ecf20Sopenharmony_ci			return snprintf(buf, PAGE_SIZE, "%u\n",
3968c2ecf20Sopenharmony_ci					le32_to_cpup(ptr));
3978c2ecf20Sopenharmony_ci		else
3988c2ecf20Sopenharmony_ci			return snprintf(buf, PAGE_SIZE, "%u\n",
3998c2ecf20Sopenharmony_ci					*((unsigned int *) ptr));
4008c2ecf20Sopenharmony_ci	case attr_pointer_ul:
4018c2ecf20Sopenharmony_ci		if (!ptr)
4028c2ecf20Sopenharmony_ci			return 0;
4038c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%lu\n",
4048c2ecf20Sopenharmony_ci				*((unsigned long *) ptr));
4058c2ecf20Sopenharmony_ci	case attr_pointer_u8:
4068c2ecf20Sopenharmony_ci		if (!ptr)
4078c2ecf20Sopenharmony_ci			return 0;
4088c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%u\n",
4098c2ecf20Sopenharmony_ci				*((unsigned char *) ptr));
4108c2ecf20Sopenharmony_ci	case attr_pointer_u64:
4118c2ecf20Sopenharmony_ci		if (!ptr)
4128c2ecf20Sopenharmony_ci			return 0;
4138c2ecf20Sopenharmony_ci		if (a->attr_ptr == ptr_ext4_super_block_offset)
4148c2ecf20Sopenharmony_ci			return snprintf(buf, PAGE_SIZE, "%llu\n",
4158c2ecf20Sopenharmony_ci					le64_to_cpup(ptr));
4168c2ecf20Sopenharmony_ci		else
4178c2ecf20Sopenharmony_ci			return snprintf(buf, PAGE_SIZE, "%llu\n",
4188c2ecf20Sopenharmony_ci					*((unsigned long long *) ptr));
4198c2ecf20Sopenharmony_ci	case attr_pointer_string:
4208c2ecf20Sopenharmony_ci		if (!ptr)
4218c2ecf20Sopenharmony_ci			return 0;
4228c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%.*s\n", a->attr_size,
4238c2ecf20Sopenharmony_ci				(char *) ptr);
4248c2ecf20Sopenharmony_ci	case attr_pointer_atomic:
4258c2ecf20Sopenharmony_ci		if (!ptr)
4268c2ecf20Sopenharmony_ci			return 0;
4278c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "%d\n",
4288c2ecf20Sopenharmony_ci				atomic_read((atomic_t *) ptr));
4298c2ecf20Sopenharmony_ci	case attr_feature:
4308c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, "supported\n");
4318c2ecf20Sopenharmony_ci	case attr_first_error_time:
4328c2ecf20Sopenharmony_ci		return print_tstamp(buf, sbi->s_es, s_first_error_time);
4338c2ecf20Sopenharmony_ci	case attr_last_error_time:
4348c2ecf20Sopenharmony_ci		return print_tstamp(buf, sbi->s_es, s_last_error_time);
4358c2ecf20Sopenharmony_ci	case attr_journal_task:
4368c2ecf20Sopenharmony_ci		return journal_task_show(sbi, buf);
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	return 0;
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic ssize_t ext4_attr_store(struct kobject *kobj,
4438c2ecf20Sopenharmony_ci			       struct attribute *attr,
4448c2ecf20Sopenharmony_ci			       const char *buf, size_t len)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
4478c2ecf20Sopenharmony_ci						s_kobj);
4488c2ecf20Sopenharmony_ci	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
4498c2ecf20Sopenharmony_ci	void *ptr = calc_ptr(a, sbi);
4508c2ecf20Sopenharmony_ci	unsigned long t;
4518c2ecf20Sopenharmony_ci	int ret;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	switch (a->attr_id) {
4548c2ecf20Sopenharmony_ci	case attr_reserved_clusters:
4558c2ecf20Sopenharmony_ci		return reserved_clusters_store(sbi, buf, len);
4568c2ecf20Sopenharmony_ci	case attr_pointer_ui:
4578c2ecf20Sopenharmony_ci		if (!ptr)
4588c2ecf20Sopenharmony_ci			return 0;
4598c2ecf20Sopenharmony_ci		ret = kstrtoul(skip_spaces(buf), 0, &t);
4608c2ecf20Sopenharmony_ci		if (ret)
4618c2ecf20Sopenharmony_ci			return ret;
4628c2ecf20Sopenharmony_ci		if (a->attr_ptr == ptr_ext4_super_block_offset)
4638c2ecf20Sopenharmony_ci			*((__le32 *) ptr) = cpu_to_le32(t);
4648c2ecf20Sopenharmony_ci		else
4658c2ecf20Sopenharmony_ci			*((unsigned int *) ptr) = t;
4668c2ecf20Sopenharmony_ci		return len;
4678c2ecf20Sopenharmony_ci	case attr_pointer_ul:
4688c2ecf20Sopenharmony_ci		if (!ptr)
4698c2ecf20Sopenharmony_ci			return 0;
4708c2ecf20Sopenharmony_ci		ret = kstrtoul(skip_spaces(buf), 0, &t);
4718c2ecf20Sopenharmony_ci		if (ret)
4728c2ecf20Sopenharmony_ci			return ret;
4738c2ecf20Sopenharmony_ci		*((unsigned long *) ptr) = t;
4748c2ecf20Sopenharmony_ci		return len;
4758c2ecf20Sopenharmony_ci	case attr_inode_readahead:
4768c2ecf20Sopenharmony_ci		return inode_readahead_blks_store(sbi, buf, len);
4778c2ecf20Sopenharmony_ci	case attr_trigger_test_error:
4788c2ecf20Sopenharmony_ci		return trigger_test_error(sbi, buf, len);
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci	return 0;
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic void ext4_sb_release(struct kobject *kobj)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
4868c2ecf20Sopenharmony_ci						s_kobj);
4878c2ecf20Sopenharmony_ci	complete(&sbi->s_kobj_unregister);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cistatic void ext4_feat_release(struct kobject *kobj)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	kfree(kobj);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic const struct sysfs_ops ext4_attr_ops = {
4968c2ecf20Sopenharmony_ci	.show	= ext4_attr_show,
4978c2ecf20Sopenharmony_ci	.store	= ext4_attr_store,
4988c2ecf20Sopenharmony_ci};
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic struct kobj_type ext4_sb_ktype = {
5018c2ecf20Sopenharmony_ci	.default_groups = ext4_groups,
5028c2ecf20Sopenharmony_ci	.sysfs_ops	= &ext4_attr_ops,
5038c2ecf20Sopenharmony_ci	.release	= ext4_sb_release,
5048c2ecf20Sopenharmony_ci};
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic struct kobj_type ext4_feat_ktype = {
5078c2ecf20Sopenharmony_ci	.default_groups = ext4_feat_groups,
5088c2ecf20Sopenharmony_ci	.sysfs_ops	= &ext4_attr_ops,
5098c2ecf20Sopenharmony_ci	.release	= ext4_feat_release,
5108c2ecf20Sopenharmony_ci};
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic struct kobject *ext4_root;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic struct kobject *ext4_feat;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ciint ext4_register_sysfs(struct super_block *sb)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct ext4_sb_info *sbi = EXT4_SB(sb);
5198c2ecf20Sopenharmony_ci	int err;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	init_completion(&sbi->s_kobj_unregister);
5228c2ecf20Sopenharmony_ci	err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
5238c2ecf20Sopenharmony_ci				   "%s", sb->s_id);
5248c2ecf20Sopenharmony_ci	if (err) {
5258c2ecf20Sopenharmony_ci		kobject_put(&sbi->s_kobj);
5268c2ecf20Sopenharmony_ci		wait_for_completion(&sbi->s_kobj_unregister);
5278c2ecf20Sopenharmony_ci		return err;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (ext4_proc_root)
5318c2ecf20Sopenharmony_ci		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
5328c2ecf20Sopenharmony_ci	if (sbi->s_proc) {
5338c2ecf20Sopenharmony_ci		proc_create_single_data("options", S_IRUGO, sbi->s_proc,
5348c2ecf20Sopenharmony_ci				ext4_seq_options_show, sb);
5358c2ecf20Sopenharmony_ci		proc_create_single_data("es_shrinker_info", S_IRUGO,
5368c2ecf20Sopenharmony_ci				sbi->s_proc, ext4_seq_es_shrinker_info_show,
5378c2ecf20Sopenharmony_ci				sb);
5388c2ecf20Sopenharmony_ci		proc_create_single_data("fc_info", 0444, sbi->s_proc,
5398c2ecf20Sopenharmony_ci					ext4_fc_info_show, sb);
5408c2ecf20Sopenharmony_ci		proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
5418c2ecf20Sopenharmony_ci				&ext4_mb_seq_groups_ops, sb);
5428c2ecf20Sopenharmony_ci		proc_create_single_data("mb_stats", 0444, sbi->s_proc,
5438c2ecf20Sopenharmony_ci				ext4_seq_mb_stats_show, sb);
5448c2ecf20Sopenharmony_ci	}
5458c2ecf20Sopenharmony_ci	return 0;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_civoid ext4_unregister_sysfs(struct super_block *sb)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	struct ext4_sb_info *sbi = EXT4_SB(sb);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	if (sbi->s_proc)
5538c2ecf20Sopenharmony_ci		remove_proc_subtree(sb->s_id, ext4_proc_root);
5548c2ecf20Sopenharmony_ci	kobject_del(&sbi->s_kobj);
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ciint __init ext4_init_sysfs(void)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	int ret;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	ext4_root = kobject_create_and_add("ext4", fs_kobj);
5628c2ecf20Sopenharmony_ci	if (!ext4_root)
5638c2ecf20Sopenharmony_ci		return -ENOMEM;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
5668c2ecf20Sopenharmony_ci	if (!ext4_feat) {
5678c2ecf20Sopenharmony_ci		ret = -ENOMEM;
5688c2ecf20Sopenharmony_ci		goto root_err;
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
5728c2ecf20Sopenharmony_ci				   ext4_root, "features");
5738c2ecf20Sopenharmony_ci	if (ret)
5748c2ecf20Sopenharmony_ci		goto feat_err;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	ext4_proc_root = proc_mkdir(proc_dirname, NULL);
5778c2ecf20Sopenharmony_ci	return ret;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cifeat_err:
5808c2ecf20Sopenharmony_ci	kobject_put(ext4_feat);
5818c2ecf20Sopenharmony_ci	ext4_feat = NULL;
5828c2ecf20Sopenharmony_ciroot_err:
5838c2ecf20Sopenharmony_ci	kobject_put(ext4_root);
5848c2ecf20Sopenharmony_ci	ext4_root = NULL;
5858c2ecf20Sopenharmony_ci	return ret;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_civoid ext4_exit_sysfs(void)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	kobject_put(ext4_feat);
5918c2ecf20Sopenharmony_ci	ext4_feat = NULL;
5928c2ecf20Sopenharmony_ci	kobject_put(ext4_root);
5938c2ecf20Sopenharmony_ci	ext4_root = NULL;
5948c2ecf20Sopenharmony_ci	remove_proc_entry(proc_dirname, NULL);
5958c2ecf20Sopenharmony_ci	ext4_proc_root = NULL;
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
598