sysfs.c 12 KB

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