sysfs.c 19 KB

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