sysfs.c 12 KB

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