sysfs.c 11 KB

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