sysfs.c 12 KB

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