GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / ext4 / sysfs.c
1 // SPDX-License-Identifier: GPL-2.0
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>
14 #include <linux/slab.h>
15 #include <linux/proc_fs.h>
16
17 #include "ext4.h"
18 #include "ext4_jbd2.h"
19
20 typedef enum {
21         attr_noop,
22         attr_delayed_allocation_blocks,
23         attr_session_write_kbytes,
24         attr_lifetime_write_kbytes,
25         attr_reserved_clusters,
26         attr_sra_exceeded_retry_limit,
27         attr_inode_readahead,
28         attr_trigger_test_error,
29         attr_first_error_time,
30         attr_last_error_time,
31         attr_feature,
32         attr_pointer_ui,
33         attr_pointer_atomic,
34         attr_journal_task,
35 } attr_id_t;
36
37 typedef enum {
38         ptr_explicit,
39         ptr_ext4_sb_info_offset,
40         ptr_ext4_super_block_offset,
41 } attr_ptr_t;
42
43 static const char proc_dirname[] = "fs/ext4";
44 static struct proc_dir_entry *ext4_proc_root;
45
46 struct ext4_attr {
47         struct attribute attr;
48         short attr_id;
49         short attr_ptr;
50         union {
51                 int offset;
52                 void *explicit_ptr;
53         } u;
54 };
55
56 static ssize_t session_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
57 {
58         struct super_block *sb = sbi->s_buddy_cache->i_sb;
59
60         if (!sb->s_bdev->bd_part)
61                 return snprintf(buf, PAGE_SIZE, "0\n");
62         return snprintf(buf, PAGE_SIZE, "%lu\n",
63                         (part_stat_read(sb->s_bdev->bd_part,
64                                         sectors[STAT_WRITE]) -
65                          sbi->s_sectors_written_start) >> 1);
66 }
67
68 static ssize_t lifetime_write_kbytes_show(struct ext4_sb_info *sbi, char *buf)
69 {
70         struct super_block *sb = sbi->s_buddy_cache->i_sb;
71
72         if (!sb->s_bdev->bd_part)
73                 return snprintf(buf, PAGE_SIZE, "0\n");
74         return snprintf(buf, PAGE_SIZE, "%llu\n",
75                         (unsigned long long)(sbi->s_kbytes_written +
76                         ((part_stat_read(sb->s_bdev->bd_part,
77                                          sectors[STAT_WRITE]) -
78                           EXT4_SB(sb)->s_sectors_written_start) >> 1)));
79 }
80
81 static ssize_t inode_readahead_blks_store(struct ext4_sb_info *sbi,
82                                           const char *buf, size_t count)
83 {
84         unsigned long t;
85         int ret;
86
87         ret = kstrtoul(skip_spaces(buf), 0, &t);
88         if (ret)
89                 return ret;
90
91         if (t && (!is_power_of_2(t) || t > 0x40000000))
92                 return -EINVAL;
93
94         sbi->s_inode_readahead_blks = t;
95         return count;
96 }
97
98 static ssize_t reserved_clusters_store(struct ext4_sb_info *sbi,
99                                    const char *buf, size_t count)
100 {
101         unsigned long long val;
102         ext4_fsblk_t clusters = (ext4_blocks_count(sbi->s_es) >>
103                                  sbi->s_cluster_bits);
104         int ret;
105
106         ret = kstrtoull(skip_spaces(buf), 0, &val);
107         if (ret || val >= clusters)
108                 return -EINVAL;
109
110         atomic64_set(&sbi->s_resv_clusters, val);
111         return count;
112 }
113
114 static ssize_t trigger_test_error(struct ext4_sb_info *sbi,
115                                   const char *buf, size_t count)
116 {
117         int len = count;
118
119         if (!capable(CAP_SYS_ADMIN))
120                 return -EPERM;
121
122         if (len && buf[len-1] == '\n')
123                 len--;
124
125         if (len)
126                 ext4_error(sbi->s_sb, "%.*s", len, buf);
127         return count;
128 }
129
130 static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
131 {
132         if (!sbi->s_journal)
133                 return snprintf(buf, PAGE_SIZE, "<none>\n");
134         return snprintf(buf, PAGE_SIZE, "%d\n",
135                         task_pid_vnr(sbi->s_journal->j_task));
136 }
137
138 #define EXT4_ATTR(_name,_mode,_id)                                      \
139 static struct ext4_attr ext4_attr_##_name = {                           \
140         .attr = {.name = __stringify(_name), .mode = _mode },           \
141         .attr_id = attr_##_id,                                          \
142 }
143
144 #define EXT4_ATTR_FUNC(_name,_mode)  EXT4_ATTR(_name,_mode,_name)
145
146 #define EXT4_ATTR_FEATURE(_name)   EXT4_ATTR(_name, 0444, feature)
147
148 #define EXT4_ATTR_OFFSET(_name,_mode,_id,_struct,_elname)       \
149 static struct ext4_attr ext4_attr_##_name = {                   \
150         .attr = {.name = __stringify(_name), .mode = _mode },   \
151         .attr_id = attr_##_id,                                  \
152         .attr_ptr = ptr_##_struct##_offset,                     \
153         .u = {                                                  \
154                 .offset = offsetof(struct _struct, _elname),\
155         },                                                      \
156 }
157
158 #define EXT4_RO_ATTR_ES_UI(_name,_elname)                               \
159         EXT4_ATTR_OFFSET(_name, 0444, pointer_ui, ext4_super_block, _elname)
160
161 #define EXT4_RW_ATTR_SBI_UI(_name,_elname)      \
162         EXT4_ATTR_OFFSET(_name, 0644, pointer_ui, ext4_sb_info, _elname)
163
164 #define EXT4_ATTR_PTR(_name,_mode,_id,_ptr) \
165 static struct ext4_attr ext4_attr_##_name = {                   \
166         .attr = {.name = __stringify(_name), .mode = _mode },   \
167         .attr_id = attr_##_id,                                  \
168         .attr_ptr = ptr_explicit,                               \
169         .u = {                                                  \
170                 .explicit_ptr = _ptr,                           \
171         },                                                      \
172 }
173
174 #define ATTR_LIST(name) &ext4_attr_##name.attr
175
176 EXT4_ATTR_FUNC(delayed_allocation_blocks, 0444);
177 EXT4_ATTR_FUNC(session_write_kbytes, 0444);
178 EXT4_ATTR_FUNC(lifetime_write_kbytes, 0444);
179 EXT4_ATTR_FUNC(reserved_clusters, 0644);
180 EXT4_ATTR_FUNC(sra_exceeded_retry_limit, 0444);
181
182 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, inode_readahead,
183                  ext4_sb_info, s_inode_readahead_blks);
184 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
185 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
186 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
187 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
188 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
189 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
190 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
191 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
192 EXT4_ATTR(trigger_fs_error, 0200, trigger_test_error);
193 EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
194 EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
195 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
196 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
197 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
198 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
199 EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
200 EXT4_ATTR(first_error_time, 0444, first_error_time);
201 EXT4_ATTR(last_error_time, 0444, last_error_time);
202 EXT4_ATTR(journal_task, 0444, journal_task);
203
204 static unsigned int old_bump_val = 128;
205 EXT4_ATTR_PTR(max_writeback_mb_bump, 0444, pointer_ui, &old_bump_val);
206
207 static struct attribute *ext4_attrs[] = {
208         ATTR_LIST(delayed_allocation_blocks),
209         ATTR_LIST(session_write_kbytes),
210         ATTR_LIST(lifetime_write_kbytes),
211         ATTR_LIST(reserved_clusters),
212         ATTR_LIST(sra_exceeded_retry_limit),
213         ATTR_LIST(inode_readahead_blks),
214         ATTR_LIST(inode_goal),
215         ATTR_LIST(mb_stats),
216         ATTR_LIST(mb_max_to_scan),
217         ATTR_LIST(mb_min_to_scan),
218         ATTR_LIST(mb_order2_req),
219         ATTR_LIST(mb_stream_req),
220         ATTR_LIST(mb_group_prealloc),
221         ATTR_LIST(max_writeback_mb_bump),
222         ATTR_LIST(extent_max_zeroout_kb),
223         ATTR_LIST(trigger_fs_error),
224         ATTR_LIST(err_ratelimit_interval_ms),
225         ATTR_LIST(err_ratelimit_burst),
226         ATTR_LIST(warning_ratelimit_interval_ms),
227         ATTR_LIST(warning_ratelimit_burst),
228         ATTR_LIST(msg_ratelimit_interval_ms),
229         ATTR_LIST(msg_ratelimit_burst),
230         ATTR_LIST(errors_count),
231         ATTR_LIST(first_error_time),
232         ATTR_LIST(last_error_time),
233         ATTR_LIST(journal_task),
234         NULL,
235 };
236 ATTRIBUTE_GROUPS(ext4);
237
238 /* Features this copy of ext4 supports */
239 EXT4_ATTR_FEATURE(lazy_itable_init);
240 EXT4_ATTR_FEATURE(batched_discard);
241 EXT4_ATTR_FEATURE(meta_bg_resize);
242 #ifdef CONFIG_FS_ENCRYPTION
243 EXT4_ATTR_FEATURE(encryption);
244 #endif
245 #ifdef CONFIG_UNICODE
246 EXT4_ATTR_FEATURE(casefold);
247 #endif
248 #ifdef CONFIG_FS_VERITY
249 EXT4_ATTR_FEATURE(verity);
250 #endif
251 EXT4_ATTR_FEATURE(metadata_csum_seed);
252
253 static struct attribute *ext4_feat_attrs[] = {
254         ATTR_LIST(lazy_itable_init),
255         ATTR_LIST(batched_discard),
256         ATTR_LIST(meta_bg_resize),
257 #ifdef CONFIG_FS_ENCRYPTION
258         ATTR_LIST(encryption),
259 #endif
260 #ifdef CONFIG_UNICODE
261         ATTR_LIST(casefold),
262 #endif
263 #ifdef CONFIG_FS_VERITY
264         ATTR_LIST(verity),
265 #endif
266         ATTR_LIST(metadata_csum_seed),
267         NULL,
268 };
269 ATTRIBUTE_GROUPS(ext4_feat);
270
271 static void *calc_ptr(struct ext4_attr *a, struct ext4_sb_info *sbi)
272 {
273         switch (a->attr_ptr) {
274         case ptr_explicit:
275                 return a->u.explicit_ptr;
276         case ptr_ext4_sb_info_offset:
277                 return (void *) (((char *) sbi) + a->u.offset);
278         case ptr_ext4_super_block_offset:
279                 return (void *) (((char *) sbi->s_es) + a->u.offset);
280         }
281         return NULL;
282 }
283
284 static ssize_t __print_tstamp(char *buf, __le32 lo, __u8 hi)
285 {
286         return snprintf(buf, PAGE_SIZE, "%lld",
287                         ((time64_t)hi << 32) + le32_to_cpu(lo));
288 }
289
290 #define print_tstamp(buf, es, tstamp) \
291         __print_tstamp(buf, (es)->tstamp, (es)->tstamp ## _hi)
292
293 static ssize_t ext4_attr_show(struct kobject *kobj,
294                               struct attribute *attr, char *buf)
295 {
296         struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
297                                                 s_kobj);
298         struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
299         void *ptr = calc_ptr(a, sbi);
300
301         switch (a->attr_id) {
302         case attr_delayed_allocation_blocks:
303                 return snprintf(buf, PAGE_SIZE, "%llu\n",
304                                 (s64) EXT4_C2B(sbi,
305                        percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
306         case attr_session_write_kbytes:
307                 return session_write_kbytes_show(sbi, buf);
308         case attr_lifetime_write_kbytes:
309                 return lifetime_write_kbytes_show(sbi, buf);
310         case attr_reserved_clusters:
311                 return snprintf(buf, PAGE_SIZE, "%llu\n",
312                                 (unsigned long long)
313                                 atomic64_read(&sbi->s_resv_clusters));
314         case attr_sra_exceeded_retry_limit:
315                 return snprintf(buf, PAGE_SIZE, "%llu\n",
316                                 (unsigned long long)
317                         percpu_counter_sum(&sbi->s_sra_exceeded_retry_limit));
318         case attr_inode_readahead:
319         case attr_pointer_ui:
320                 if (!ptr)
321                         return 0;
322                 if (a->attr_ptr == ptr_ext4_super_block_offset)
323                         return snprintf(buf, PAGE_SIZE, "%u\n",
324                                         le32_to_cpup(ptr));
325                 else
326                         return snprintf(buf, PAGE_SIZE, "%u\n",
327                                         *((unsigned int *) ptr));
328         case attr_pointer_atomic:
329                 if (!ptr)
330                         return 0;
331                 return snprintf(buf, PAGE_SIZE, "%d\n",
332                                 atomic_read((atomic_t *) ptr));
333         case attr_feature:
334                 return snprintf(buf, PAGE_SIZE, "supported\n");
335         case attr_first_error_time:
336                 return print_tstamp(buf, sbi->s_es, s_first_error_time);
337         case attr_last_error_time:
338                 return print_tstamp(buf, sbi->s_es, s_last_error_time);
339         case attr_journal_task:
340                 return journal_task_show(sbi, buf);
341         }
342
343         return 0;
344 }
345
346 static ssize_t ext4_attr_store(struct kobject *kobj,
347                                struct attribute *attr,
348                                const char *buf, size_t len)
349 {
350         struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
351                                                 s_kobj);
352         struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
353         void *ptr = calc_ptr(a, sbi);
354         unsigned long t;
355         int ret;
356
357         switch (a->attr_id) {
358         case attr_reserved_clusters:
359                 return reserved_clusters_store(sbi, buf, len);
360         case attr_pointer_ui:
361                 if (!ptr)
362                         return 0;
363                 ret = kstrtoul(skip_spaces(buf), 0, &t);
364                 if (ret)
365                         return ret;
366                 if (a->attr_ptr == ptr_ext4_super_block_offset)
367                         *((__le32 *) ptr) = cpu_to_le32(t);
368                 else
369                         *((unsigned int *) ptr) = t;
370                 return len;
371         case attr_inode_readahead:
372                 return inode_readahead_blks_store(sbi, buf, len);
373         case attr_trigger_test_error:
374                 return trigger_test_error(sbi, buf, len);
375         }
376         return 0;
377 }
378
379 static void ext4_sb_release(struct kobject *kobj)
380 {
381         struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
382                                                 s_kobj);
383         complete(&sbi->s_kobj_unregister);
384 }
385
386 static void ext4_feat_release(struct kobject *kobj)
387 {
388         kfree(kobj);
389 }
390
391 static const struct sysfs_ops ext4_attr_ops = {
392         .show   = ext4_attr_show,
393         .store  = ext4_attr_store,
394 };
395
396 static struct kobj_type ext4_sb_ktype = {
397         .default_groups = ext4_groups,
398         .sysfs_ops      = &ext4_attr_ops,
399         .release        = ext4_sb_release,
400 };
401
402 static struct kobj_type ext4_feat_ktype = {
403         .default_groups = ext4_feat_groups,
404         .sysfs_ops      = &ext4_attr_ops,
405         .release        = ext4_feat_release,
406 };
407
408 static struct kobject *ext4_root;
409
410 static struct kobject *ext4_feat;
411
412 int ext4_register_sysfs(struct super_block *sb)
413 {
414         struct ext4_sb_info *sbi = EXT4_SB(sb);
415         int err;
416
417         init_completion(&sbi->s_kobj_unregister);
418         err = kobject_init_and_add(&sbi->s_kobj, &ext4_sb_ktype, ext4_root,
419                                    "%s", sb->s_id);
420         if (err) {
421                 kobject_put(&sbi->s_kobj);
422                 wait_for_completion(&sbi->s_kobj_unregister);
423                 return err;
424         }
425
426         if (ext4_proc_root)
427                 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
428         if (sbi->s_proc) {
429                 proc_create_single_data("options", S_IRUGO, sbi->s_proc,
430                                 ext4_seq_options_show, sb);
431                 proc_create_single_data("es_shrinker_info", S_IRUGO,
432                                 sbi->s_proc, ext4_seq_es_shrinker_info_show,
433                                 sb);
434                 proc_create_seq_data("mb_groups", S_IRUGO, sbi->s_proc,
435                                 &ext4_mb_seq_groups_ops, sb);
436         }
437         return 0;
438 }
439
440 void ext4_unregister_sysfs(struct super_block *sb)
441 {
442         struct ext4_sb_info *sbi = EXT4_SB(sb);
443
444         if (sbi->s_proc)
445                 remove_proc_subtree(sb->s_id, ext4_proc_root);
446         kobject_del(&sbi->s_kobj);
447 }
448
449 int __init ext4_init_sysfs(void)
450 {
451         int ret;
452
453         ext4_root = kobject_create_and_add("ext4", fs_kobj);
454         if (!ext4_root)
455                 return -ENOMEM;
456
457         ext4_feat = kzalloc(sizeof(*ext4_feat), GFP_KERNEL);
458         if (!ext4_feat) {
459                 ret = -ENOMEM;
460                 goto root_err;
461         }
462
463         ret = kobject_init_and_add(ext4_feat, &ext4_feat_ktype,
464                                    ext4_root, "features");
465         if (ret)
466                 goto feat_err;
467
468         ext4_proc_root = proc_mkdir(proc_dirname, NULL);
469         return ret;
470
471 feat_err:
472         kobject_put(ext4_feat);
473         ext4_feat = NULL;
474 root_err:
475         kobject_put(ext4_root);
476         ext4_root = NULL;
477         return ret;
478 }
479
480 void ext4_exit_sysfs(void)
481 {
482         kobject_put(ext4_feat);
483         ext4_feat = NULL;
484         kobject_put(ext4_root);
485         ext4_root = NULL;
486         remove_proc_entry(proc_dirname, NULL);
487         ext4_proc_root = NULL;
488 }
489