blob: 9fdc21b9b2654f4c3c91334565fe88e12eeecc18 [file] [log] [blame]
Theodore Ts'ob5799012015-09-23 12:44:17 -04001/*
2 * linux/fs/ext4/sysfs.c
3 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Theodore Ts'o (tytso@mit.edu)
7 *
8 */
9
10#include <linux/time.h>
11#include <linux/fs.h>
12#include <linux/seq_file.h>
13#include <linux/proc_fs.h>
14
15#include "ext4.h"
16#include "ext4_jbd2.h"
17
18struct ext4_attr {
19 struct attribute attr;
20 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
21 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
22 const char *, size_t);
23 union {
24 int offset;
25 int deprecated_val;
26 } u;
27};
28
29static int parse_strtoull(const char *buf,
30 unsigned long long max, unsigned long long *value)
31{
32 int ret;
33
34 ret = kstrtoull(skip_spaces(buf), 0, value);
35 if (!ret && *value > max)
36 ret = -EINVAL;
37 return ret;
38}
39
40static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
41 struct ext4_sb_info *sbi,
42 char *buf)
43{
44 return snprintf(buf, PAGE_SIZE, "%llu\n",
45 (s64) EXT4_C2B(sbi,
46 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
47}
48
49static ssize_t session_write_kbytes_show(struct ext4_attr *a,
50 struct ext4_sb_info *sbi, char *buf)
51{
52 struct super_block *sb = sbi->s_buddy_cache->i_sb;
53
54 if (!sb->s_bdev->bd_part)
55 return snprintf(buf, PAGE_SIZE, "0\n");
56 return snprintf(buf, PAGE_SIZE, "%lu\n",
57 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
58 sbi->s_sectors_written_start) >> 1);
59}
60
61static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
62 struct ext4_sb_info *sbi, char *buf)
63{
64 struct super_block *sb = sbi->s_buddy_cache->i_sb;
65
66 if (!sb->s_bdev->bd_part)
67 return snprintf(buf, PAGE_SIZE, "0\n");
68 return snprintf(buf, PAGE_SIZE, "%llu\n",
69 (unsigned long long)(sbi->s_kbytes_written +
70 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
71 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
72}
73
74static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
75 struct ext4_sb_info *sbi,
76 const char *buf, size_t count)
77{
78 unsigned long t;
79 int ret;
80
81 ret = kstrtoul(skip_spaces(buf), 0, &t);
82 if (ret)
83 return ret;
84
85 if (t && (!is_power_of_2(t) || t > 0x40000000))
86 return -EINVAL;
87
88 sbi->s_inode_readahead_blks = t;
89 return count;
90}
91
92static ssize_t sbi_ui_show(struct ext4_attr *a,
93 struct ext4_sb_info *sbi, char *buf)
94{
95 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
96
97 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
98}
99
100static ssize_t sbi_ui_store(struct ext4_attr *a,
101 struct ext4_sb_info *sbi,
102 const char *buf, size_t count)
103{
104 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
105 unsigned long t;
106 int ret;
107
108 ret = kstrtoul(skip_spaces(buf), 0, &t);
109 if (ret)
110 return ret;
111 *ui = t;
112 return count;
113}
114
115static ssize_t es_ui_show(struct ext4_attr *a,
116 struct ext4_sb_info *sbi, char *buf)
117{
118
119 unsigned int *ui = (unsigned int *) (((char *) sbi->s_es) +
120 a->u.offset);
121
122 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
123}
124
125static ssize_t reserved_clusters_show(struct ext4_attr *a,
126 struct ext4_sb_info *sbi, char *buf)
127{
128 return snprintf(buf, PAGE_SIZE, "%llu\n",
129 (unsigned long long) atomic64_read(&sbi->s_resv_clusters));
130}
131
132static ssize_t reserved_clusters_store(struct ext4_attr *a,
133 struct ext4_sb_info *sbi,
134 const char *buf, size_t count)
135{
136 unsigned long long val;
137 ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
138 sbi->s_cluster_bits);
139
140 if (parse_strtoull(buf, -1ULL, &val))
141 return -EINVAL;
142
143 if (val >= clusters)
144 return -EINVAL;
145
146 atomic64_set(&sbi->s_resv_clusters, val);
147 return count;
148}
149
150static ssize_t trigger_test_error(struct ext4_attr *a,
151 struct ext4_sb_info *sbi,
152 const char *buf, size_t count)
153{
154 int len = count;
155
156 if (!capable(CAP_SYS_ADMIN))
157 return -EPERM;
158
159 if (len && buf[len-1] == '\n')
160 len--;
161
162 if (len)
163 ext4_error(sbi->s_sb, "%.*s", len, buf);
164 return count;
165}
166
167static ssize_t sbi_deprecated_show(struct ext4_attr *a,
168 struct ext4_sb_info *sbi, char *buf)
169{
170 return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
171}
172
173#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
174static struct ext4_attr ext4_attr_##_name = { \
175 .attr = {.name = __stringify(_name), .mode = _mode }, \
176 .show = _show, \
177 .store = _store, \
178 .u = { \
179 .offset = offsetof(struct ext4_sb_info, _elname),\
180 }, \
181}
182
183#define EXT4_ATTR_OFFSET_ES(_name,_mode,_show,_store,_elname) \
184static struct ext4_attr ext4_attr_##_name = { \
185 .attr = {.name = __stringify(_name), .mode = _mode }, \
186 .show = _show, \
187 .store = _store, \
188 .u = { \
189 .offset = offsetof(struct ext4_super_block, _elname), \
190 }, \
191}
192
193#define EXT4_ATTR(name, mode, show, store) \
194static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
195
196#define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
197#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
198#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
199
200#define EXT4_RO_ATTR_ES_UI(name, elname) \
201 EXT4_ATTR_OFFSET_ES(name, 0444, es_ui_show, NULL, elname)
202#define EXT4_RW_ATTR_SBI_UI(name, elname) \
203 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
204
205#define ATTR_LIST(name) &ext4_attr_##name.attr
206#define EXT4_DEPRECATED_ATTR(_name, _val) \
207static struct ext4_attr ext4_attr_##_name = { \
208 .attr = {.name = __stringify(_name), .mode = 0444 }, \
209 .show = sbi_deprecated_show, \
210 .u = { \
211 .deprecated_val = _val, \
212 }, \
213}
214
215EXT4_RO_ATTR(delayed_allocation_blocks);
216EXT4_RO_ATTR(session_write_kbytes);
217EXT4_RO_ATTR(lifetime_write_kbytes);
218EXT4_RW_ATTR(reserved_clusters);
219EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
220 inode_readahead_blks_store, s_inode_readahead_blks);
221EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
222EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
223EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
224EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
225EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
226EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
227EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
228EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
229EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
230EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
231EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
232EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
233EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
234EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
235EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
236EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
237EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
238EXT4_RO_ATTR_ES_UI(first_error_time, s_first_error_time);
239EXT4_RO_ATTR_ES_UI(last_error_time, s_last_error_time);
240
241static struct attribute *ext4_attrs[] = {
242 ATTR_LIST(delayed_allocation_blocks),
243 ATTR_LIST(session_write_kbytes),
244 ATTR_LIST(lifetime_write_kbytes),
245 ATTR_LIST(reserved_clusters),
246 ATTR_LIST(inode_readahead_blks),
247 ATTR_LIST(inode_goal),
248 ATTR_LIST(mb_stats),
249 ATTR_LIST(mb_max_to_scan),
250 ATTR_LIST(mb_min_to_scan),
251 ATTR_LIST(mb_order2_req),
252 ATTR_LIST(mb_stream_req),
253 ATTR_LIST(mb_group_prealloc),
254 ATTR_LIST(max_writeback_mb_bump),
255 ATTR_LIST(extent_max_zeroout_kb),
256 ATTR_LIST(trigger_fs_error),
257 ATTR_LIST(err_ratelimit_interval_ms),
258 ATTR_LIST(err_ratelimit_burst),
259 ATTR_LIST(warning_ratelimit_interval_ms),
260 ATTR_LIST(warning_ratelimit_burst),
261 ATTR_LIST(msg_ratelimit_interval_ms),
262 ATTR_LIST(msg_ratelimit_burst),
263 ATTR_LIST(errors_count),
264 ATTR_LIST(first_error_time),
265 ATTR_LIST(last_error_time),
266 NULL,
267};
268
269/* Features this copy of ext4 supports */
270EXT4_INFO_ATTR(lazy_itable_init);
271EXT4_INFO_ATTR(batched_discard);
272EXT4_INFO_ATTR(meta_bg_resize);
273EXT4_INFO_ATTR(encryption);
274
275static struct attribute *ext4_feat_attrs[] = {
276 ATTR_LIST(lazy_itable_init),
277 ATTR_LIST(batched_discard),
278 ATTR_LIST(meta_bg_resize),
279 ATTR_LIST(encryption),
280 NULL,
281};
282
283static ssize_t ext4_attr_show(struct kobject *kobj,
284 struct attribute *attr, char *buf)
285{
286 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
287 s_kobj);
288 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
289
290 return a->show ? a->show(a, sbi, buf) : 0;
291}
292
293static ssize_t ext4_attr_store(struct kobject *kobj,
294 struct attribute *attr,
295 const char *buf, size_t len)
296{
297 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
298 s_kobj);
299 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
300
301 return a->store ? a->store(a, sbi, buf, len) : 0;
302}
303
304static void ext4_sb_release(struct kobject *kobj)
305{
306 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
307 s_kobj);
308 complete(&sbi->s_kobj_unregister);
309}
310
311static const struct sysfs_ops ext4_attr_ops = {
312 .show = ext4_attr_show,
313 .store = ext4_attr_store,
314};
315
316static struct kobj_type ext4_sb_ktype = {
317 .default_attrs = ext4_attrs,
318 .sysfs_ops = &ext4_attr_ops,
319 .release = ext4_sb_release,
320};
321
322static struct kobj_type ext4_ktype = {
323 .sysfs_ops = &ext4_attr_ops,
324};
325
326static struct kset ext4_kset = {
327 .kobj = {.ktype = &ext4_ktype},
328};
329
330static ssize_t ext4_feat_show(struct kobject *kobj,
331 struct attribute *attr, char *buf)
332{
333 return snprintf(buf, PAGE_SIZE, "supported\n");
334}
335
336/*
337 * We can not use ext4_attr_show/store because it relies on the kobject
338 * being embedded in the ext4_sb_info structure which is definitely not
339 * true in this case.
340 */
341static const struct sysfs_ops ext4_feat_ops = {
342 .show = ext4_feat_show,
343 .store = NULL,
344};
345
346static struct kobj_type ext4_feat_ktype = {
347 .default_attrs = ext4_feat_attrs,
348 .sysfs_ops = &ext4_feat_ops,
349};
350
351static struct kobject ext4_feat = {
352 .kset = &ext4_kset,
353};
354
355int ext4_register_sysfs(struct super_block *sb)
356{
357 struct ext4_sb_info *sbi = EXT4_SB(sb);
358
359 sbi->s_kobj.kset = &ext4_kset;
360 init_completion(&sbi->s_kobj_unregister);
361 return kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, NULL,
362 "%s", sb->s_id);
363}
364
365int __init ext4_init_sysfs(void)
366{
367 int ret;
368
369 kobject_set_name(&ext4_kset.kobj, "ext4");
370 ext4_kset.kobj.parent = fs_kobj;
371 ret = kset_register(&ext4_kset);
372 if (ret)
373 return ret;
374
375 ret = kobject_init_and_add(&ext4_feat, &ext4_feat_ktype,
376 NULL, "features");
377 if (ret)
378 kset_unregister(&ext4_kset);
379 return ret;
380}
381
382void ext4_exit_sysfs(void)
383{
384 kobject_put(&ext4_feat);
385 kset_unregister(&ext4_kset);
386}
387