sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * f2fs sysfs interface
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. * Copyright (c) 2017 Chao Yu <chao@kernel.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/proc_fs.h>
  13. #include <linux/f2fs_fs.h>
  14. #include <linux/seq_file.h>
  15. #include "f2fs.h"
  16. #include "segment.h"
  17. #include "gc.h"
  18. static struct proc_dir_entry *f2fs_proc_root;
  19. /* Sysfs support for f2fs */
  20. enum {
  21. GC_THREAD, /* struct f2fs_gc_thread */
  22. SM_INFO, /* struct f2fs_sm_info */
  23. DCC_INFO, /* struct discard_cmd_control */
  24. NM_INFO, /* struct f2fs_nm_info */
  25. F2FS_SBI, /* struct f2fs_sb_info */
  26. #ifdef CONFIG_F2FS_FAULT_INJECTION
  27. FAULT_INFO_RATE, /* struct f2fs_fault_info */
  28. FAULT_INFO_TYPE, /* struct f2fs_fault_info */
  29. #endif
  30. RESERVED_BLOCKS, /* struct f2fs_sb_info */
  31. };
  32. struct f2fs_attr {
  33. struct attribute attr;
  34. ssize_t (*show)(struct f2fs_attr *, struct f2fs_sb_info *, char *);
  35. ssize_t (*store)(struct f2fs_attr *, struct f2fs_sb_info *,
  36. const char *, size_t);
  37. int struct_type;
  38. int offset;
  39. int id;
  40. };
  41. static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  42. {
  43. if (struct_type == GC_THREAD)
  44. return (unsigned char *)sbi->gc_thread;
  45. else if (struct_type == SM_INFO)
  46. return (unsigned char *)SM_I(sbi);
  47. else if (struct_type == DCC_INFO)
  48. return (unsigned char *)SM_I(sbi)->dcc_info;
  49. else if (struct_type == NM_INFO)
  50. return (unsigned char *)NM_I(sbi);
  51. else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
  52. return (unsigned char *)sbi;
  53. #ifdef CONFIG_F2FS_FAULT_INJECTION
  54. else if (struct_type == FAULT_INFO_RATE ||
  55. struct_type == FAULT_INFO_TYPE)
  56. return (unsigned char *)&sbi->fault_info;
  57. #endif
  58. return NULL;
  59. }
  60. static ssize_t dirty_segments_show(struct f2fs_attr *a,
  61. struct f2fs_sb_info *sbi, char *buf)
  62. {
  63. return snprintf(buf, PAGE_SIZE, "%llu\n",
  64. (unsigned long long)(dirty_segments(sbi)));
  65. }
  66. static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
  67. struct f2fs_sb_info *sbi, char *buf)
  68. {
  69. struct super_block *sb = sbi->sb;
  70. if (!sb->s_bdev->bd_part)
  71. return snprintf(buf, PAGE_SIZE, "0\n");
  72. return snprintf(buf, PAGE_SIZE, "%llu\n",
  73. (unsigned long long)(sbi->kbytes_written +
  74. BD_PART_WRITTEN(sbi)));
  75. }
  76. static ssize_t features_show(struct f2fs_attr *a,
  77. struct f2fs_sb_info *sbi, char *buf)
  78. {
  79. struct super_block *sb = sbi->sb;
  80. int len = 0;
  81. if (!sb->s_bdev->bd_part)
  82. return snprintf(buf, PAGE_SIZE, "0\n");
  83. if (f2fs_sb_has_crypto(sb))
  84. len += snprintf(buf, PAGE_SIZE - len, "%s",
  85. "encryption");
  86. if (f2fs_sb_mounted_blkzoned(sb))
  87. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  88. len ? ", " : "", "blkzoned");
  89. if (f2fs_sb_has_extra_attr(sb))
  90. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  91. len ? ", " : "", "extra_attr");
  92. if (f2fs_sb_has_project_quota(sb))
  93. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  94. len ? ", " : "", "projquota");
  95. if (f2fs_sb_has_inode_chksum(sb))
  96. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  97. len ? ", " : "", "inode_checksum");
  98. if (f2fs_sb_has_flexible_inline_xattr(sb))
  99. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  100. len ? ", " : "", "flexible_inline_xattr");
  101. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  102. return len;
  103. }
  104. static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
  105. struct f2fs_sb_info *sbi, char *buf)
  106. {
  107. return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
  108. }
  109. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  110. struct f2fs_sb_info *sbi, char *buf)
  111. {
  112. unsigned char *ptr = NULL;
  113. unsigned int *ui;
  114. ptr = __struct_ptr(sbi, a->struct_type);
  115. if (!ptr)
  116. return -EINVAL;
  117. ui = (unsigned int *)(ptr + a->offset);
  118. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  119. }
  120. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  121. struct f2fs_sb_info *sbi,
  122. const char *buf, size_t count)
  123. {
  124. unsigned char *ptr;
  125. unsigned long t;
  126. unsigned int *ui;
  127. ssize_t ret;
  128. ptr = __struct_ptr(sbi, a->struct_type);
  129. if (!ptr)
  130. return -EINVAL;
  131. ui = (unsigned int *)(ptr + a->offset);
  132. ret = kstrtoul(skip_spaces(buf), 0, &t);
  133. if (ret < 0)
  134. return ret;
  135. #ifdef CONFIG_F2FS_FAULT_INJECTION
  136. if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
  137. return -EINVAL;
  138. #endif
  139. if (a->struct_type == RESERVED_BLOCKS) {
  140. spin_lock(&sbi->stat_lock);
  141. if (t > (unsigned long)sbi->user_block_count) {
  142. spin_unlock(&sbi->stat_lock);
  143. return -EINVAL;
  144. }
  145. *ui = t;
  146. sbi->current_reserved_blocks = min(sbi->reserved_blocks,
  147. sbi->user_block_count - valid_user_blocks(sbi));
  148. spin_unlock(&sbi->stat_lock);
  149. return count;
  150. }
  151. if (!strcmp(a->attr.name, "discard_granularity")) {
  152. if (t == 0 || t > MAX_PLIST_NUM)
  153. return -EINVAL;
  154. if (t == *ui)
  155. return count;
  156. *ui = t;
  157. return count;
  158. }
  159. *ui = t;
  160. if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
  161. f2fs_reset_iostat(sbi);
  162. if (!strcmp(a->attr.name, "gc_urgent") && t == 1 && sbi->gc_thread) {
  163. sbi->gc_thread->gc_wake = 1;
  164. wake_up_interruptible_all(&sbi->gc_thread->gc_wait_queue_head);
  165. wake_up_discard_thread(sbi, true);
  166. }
  167. return count;
  168. }
  169. static ssize_t f2fs_attr_show(struct kobject *kobj,
  170. struct attribute *attr, char *buf)
  171. {
  172. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  173. s_kobj);
  174. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  175. return a->show ? a->show(a, sbi, buf) : 0;
  176. }
  177. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  178. const char *buf, size_t len)
  179. {
  180. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  181. s_kobj);
  182. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  183. return a->store ? a->store(a, sbi, buf, len) : 0;
  184. }
  185. static void f2fs_sb_release(struct kobject *kobj)
  186. {
  187. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  188. s_kobj);
  189. complete(&sbi->s_kobj_unregister);
  190. }
  191. enum feat_id {
  192. FEAT_CRYPTO = 0,
  193. FEAT_BLKZONED,
  194. FEAT_ATOMIC_WRITE,
  195. FEAT_EXTRA_ATTR,
  196. FEAT_PROJECT_QUOTA,
  197. FEAT_INODE_CHECKSUM,
  198. FEAT_FLEXIBLE_INLINE_XATTR,
  199. };
  200. static ssize_t f2fs_feature_show(struct f2fs_attr *a,
  201. struct f2fs_sb_info *sbi, char *buf)
  202. {
  203. switch (a->id) {
  204. case FEAT_CRYPTO:
  205. case FEAT_BLKZONED:
  206. case FEAT_ATOMIC_WRITE:
  207. case FEAT_EXTRA_ATTR:
  208. case FEAT_PROJECT_QUOTA:
  209. case FEAT_INODE_CHECKSUM:
  210. case FEAT_FLEXIBLE_INLINE_XATTR:
  211. return snprintf(buf, PAGE_SIZE, "supported\n");
  212. }
  213. return 0;
  214. }
  215. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  216. static struct f2fs_attr f2fs_attr_##_name = { \
  217. .attr = {.name = __stringify(_name), .mode = _mode }, \
  218. .show = _show, \
  219. .store = _store, \
  220. .struct_type = _struct_type, \
  221. .offset = _offset \
  222. }
  223. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  224. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  225. f2fs_sbi_show, f2fs_sbi_store, \
  226. offsetof(struct struct_name, elname))
  227. #define F2FS_GENERAL_RO_ATTR(name) \
  228. static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  229. #define F2FS_FEATURE_RO_ATTR(_name, _id) \
  230. static struct f2fs_attr f2fs_attr_##_name = { \
  231. .attr = {.name = __stringify(_name), .mode = 0444 }, \
  232. .show = f2fs_feature_show, \
  233. .id = _id, \
  234. }
  235. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
  236. urgent_sleep_time);
  237. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
  238. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
  239. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
  240. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_idle, gc_idle);
  241. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent, gc_urgent);
  242. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
  243. F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
  244. F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
  245. F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
  246. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
  247. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
  248. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
  249. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
  250. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
  251. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
  252. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
  253. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
  254. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
  255. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
  256. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
  257. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
  258. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
  259. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
  260. #ifdef CONFIG_F2FS_FAULT_INJECTION
  261. F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
  262. F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
  263. #endif
  264. F2FS_GENERAL_RO_ATTR(dirty_segments);
  265. F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
  266. F2FS_GENERAL_RO_ATTR(features);
  267. F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
  268. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  269. F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
  270. #endif
  271. #ifdef CONFIG_BLK_DEV_ZONED
  272. F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
  273. #endif
  274. F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
  275. F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
  276. F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
  277. F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
  278. F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
  279. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  280. static struct attribute *f2fs_attrs[] = {
  281. ATTR_LIST(gc_urgent_sleep_time),
  282. ATTR_LIST(gc_min_sleep_time),
  283. ATTR_LIST(gc_max_sleep_time),
  284. ATTR_LIST(gc_no_gc_sleep_time),
  285. ATTR_LIST(gc_idle),
  286. ATTR_LIST(gc_urgent),
  287. ATTR_LIST(reclaim_segments),
  288. ATTR_LIST(max_small_discards),
  289. ATTR_LIST(discard_granularity),
  290. ATTR_LIST(batched_trim_sections),
  291. ATTR_LIST(ipu_policy),
  292. ATTR_LIST(min_ipu_util),
  293. ATTR_LIST(min_fsync_blocks),
  294. ATTR_LIST(min_hot_blocks),
  295. ATTR_LIST(min_ssr_sections),
  296. ATTR_LIST(max_victim_search),
  297. ATTR_LIST(dir_level),
  298. ATTR_LIST(ram_thresh),
  299. ATTR_LIST(ra_nid_pages),
  300. ATTR_LIST(dirty_nats_ratio),
  301. ATTR_LIST(cp_interval),
  302. ATTR_LIST(idle_interval),
  303. ATTR_LIST(iostat_enable),
  304. #ifdef CONFIG_F2FS_FAULT_INJECTION
  305. ATTR_LIST(inject_rate),
  306. ATTR_LIST(inject_type),
  307. #endif
  308. ATTR_LIST(dirty_segments),
  309. ATTR_LIST(lifetime_write_kbytes),
  310. ATTR_LIST(features),
  311. ATTR_LIST(reserved_blocks),
  312. ATTR_LIST(current_reserved_blocks),
  313. NULL,
  314. };
  315. static struct attribute *f2fs_feat_attrs[] = {
  316. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  317. ATTR_LIST(encryption),
  318. #endif
  319. #ifdef CONFIG_BLK_DEV_ZONED
  320. ATTR_LIST(block_zoned),
  321. #endif
  322. ATTR_LIST(atomic_write),
  323. ATTR_LIST(extra_attr),
  324. ATTR_LIST(project_quota),
  325. ATTR_LIST(inode_checksum),
  326. ATTR_LIST(flexible_inline_xattr),
  327. NULL,
  328. };
  329. static const struct sysfs_ops f2fs_attr_ops = {
  330. .show = f2fs_attr_show,
  331. .store = f2fs_attr_store,
  332. };
  333. static struct kobj_type f2fs_sb_ktype = {
  334. .default_attrs = f2fs_attrs,
  335. .sysfs_ops = &f2fs_attr_ops,
  336. .release = f2fs_sb_release,
  337. };
  338. static struct kobj_type f2fs_ktype = {
  339. .sysfs_ops = &f2fs_attr_ops,
  340. };
  341. static struct kset f2fs_kset = {
  342. .kobj = {.ktype = &f2fs_ktype},
  343. };
  344. static struct kobj_type f2fs_feat_ktype = {
  345. .default_attrs = f2fs_feat_attrs,
  346. .sysfs_ops = &f2fs_attr_ops,
  347. };
  348. static struct kobject f2fs_feat = {
  349. .kset = &f2fs_kset,
  350. };
  351. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  352. {
  353. struct super_block *sb = seq->private;
  354. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  355. unsigned int total_segs =
  356. le32_to_cpu(sbi->raw_super->segment_count_main);
  357. int i;
  358. seq_puts(seq, "format: segment_type|valid_blocks\n"
  359. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  360. for (i = 0; i < total_segs; i++) {
  361. struct seg_entry *se = get_seg_entry(sbi, i);
  362. if ((i % 10) == 0)
  363. seq_printf(seq, "%-10d", i);
  364. seq_printf(seq, "%d|%-3u", se->type,
  365. get_valid_blocks(sbi, i, false));
  366. if ((i % 10) == 9 || i == (total_segs - 1))
  367. seq_putc(seq, '\n');
  368. else
  369. seq_putc(seq, ' ');
  370. }
  371. return 0;
  372. }
  373. static int segment_bits_seq_show(struct seq_file *seq, void *offset)
  374. {
  375. struct super_block *sb = seq->private;
  376. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  377. unsigned int total_segs =
  378. le32_to_cpu(sbi->raw_super->segment_count_main);
  379. int i, j;
  380. seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
  381. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  382. for (i = 0; i < total_segs; i++) {
  383. struct seg_entry *se = get_seg_entry(sbi, i);
  384. seq_printf(seq, "%-10d", i);
  385. seq_printf(seq, "%d|%-3u|", se->type,
  386. get_valid_blocks(sbi, i, false));
  387. for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
  388. seq_printf(seq, " %.2x", se->cur_valid_map[j]);
  389. seq_putc(seq, '\n');
  390. }
  391. return 0;
  392. }
  393. static int iostat_info_seq_show(struct seq_file *seq, void *offset)
  394. {
  395. struct super_block *sb = seq->private;
  396. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  397. time64_t now = ktime_get_real_seconds();
  398. if (!sbi->iostat_enable)
  399. return 0;
  400. seq_printf(seq, "time: %-16llu\n", now);
  401. /* print app IOs */
  402. seq_printf(seq, "app buffered: %-16llu\n",
  403. sbi->write_iostat[APP_BUFFERED_IO]);
  404. seq_printf(seq, "app direct: %-16llu\n",
  405. sbi->write_iostat[APP_DIRECT_IO]);
  406. seq_printf(seq, "app mapped: %-16llu\n",
  407. sbi->write_iostat[APP_MAPPED_IO]);
  408. /* print fs IOs */
  409. seq_printf(seq, "fs data: %-16llu\n",
  410. sbi->write_iostat[FS_DATA_IO]);
  411. seq_printf(seq, "fs node: %-16llu\n",
  412. sbi->write_iostat[FS_NODE_IO]);
  413. seq_printf(seq, "fs meta: %-16llu\n",
  414. sbi->write_iostat[FS_META_IO]);
  415. seq_printf(seq, "fs gc data: %-16llu\n",
  416. sbi->write_iostat[FS_GC_DATA_IO]);
  417. seq_printf(seq, "fs gc node: %-16llu\n",
  418. sbi->write_iostat[FS_GC_NODE_IO]);
  419. seq_printf(seq, "fs cp data: %-16llu\n",
  420. sbi->write_iostat[FS_CP_DATA_IO]);
  421. seq_printf(seq, "fs cp node: %-16llu\n",
  422. sbi->write_iostat[FS_CP_NODE_IO]);
  423. seq_printf(seq, "fs cp meta: %-16llu\n",
  424. sbi->write_iostat[FS_CP_META_IO]);
  425. seq_printf(seq, "fs discard: %-16llu\n",
  426. sbi->write_iostat[FS_DISCARD]);
  427. return 0;
  428. }
  429. #define F2FS_PROC_FILE_DEF(_name) \
  430. static int _name##_open_fs(struct inode *inode, struct file *file) \
  431. { \
  432. return single_open(file, _name##_seq_show, PDE_DATA(inode)); \
  433. } \
  434. \
  435. static const struct file_operations f2fs_seq_##_name##_fops = { \
  436. .open = _name##_open_fs, \
  437. .read = seq_read, \
  438. .llseek = seq_lseek, \
  439. .release = single_release, \
  440. };
  441. F2FS_PROC_FILE_DEF(segment_info);
  442. F2FS_PROC_FILE_DEF(segment_bits);
  443. F2FS_PROC_FILE_DEF(iostat_info);
  444. int __init f2fs_init_sysfs(void)
  445. {
  446. int ret;
  447. kobject_set_name(&f2fs_kset.kobj, "f2fs");
  448. f2fs_kset.kobj.parent = fs_kobj;
  449. ret = kset_register(&f2fs_kset);
  450. if (ret)
  451. return ret;
  452. ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
  453. NULL, "features");
  454. if (ret)
  455. kset_unregister(&f2fs_kset);
  456. else
  457. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  458. return ret;
  459. }
  460. void f2fs_exit_sysfs(void)
  461. {
  462. kobject_put(&f2fs_feat);
  463. kset_unregister(&f2fs_kset);
  464. remove_proc_entry("fs/f2fs", NULL);
  465. f2fs_proc_root = NULL;
  466. }
  467. int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
  468. {
  469. struct super_block *sb = sbi->sb;
  470. int err;
  471. sbi->s_kobj.kset = &f2fs_kset;
  472. init_completion(&sbi->s_kobj_unregister);
  473. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
  474. "%s", sb->s_id);
  475. if (err)
  476. return err;
  477. if (f2fs_proc_root)
  478. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  479. if (sbi->s_proc) {
  480. proc_create_data("segment_info", S_IRUGO, sbi->s_proc,
  481. &f2fs_seq_segment_info_fops, sb);
  482. proc_create_data("segment_bits", S_IRUGO, sbi->s_proc,
  483. &f2fs_seq_segment_bits_fops, sb);
  484. proc_create_data("iostat_info", S_IRUGO, sbi->s_proc,
  485. &f2fs_seq_iostat_info_fops, sb);
  486. }
  487. return 0;
  488. }
  489. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
  490. {
  491. if (sbi->s_proc) {
  492. remove_proc_entry("iostat_info", sbi->s_proc);
  493. remove_proc_entry("segment_info", sbi->s_proc);
  494. remove_proc_entry("segment_bits", sbi->s_proc);
  495. remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
  496. }
  497. kobject_del(&sbi->s_kobj);
  498. }