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