sysfs.c 18 KB

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