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