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